Add cgroup_sysctl template for BPF_PROG_TYPE_CGROUP_SYSCTL

Since https://github.com/aya-rs/aya/pull/256 supports `BPF_PROG_TYPE_CGROUP_SYSCTL`,
this patch adds cgroup_sysctl template.
pull/46/head
Kenjiro Nakayama 3 years ago
parent 0df4d8f969
commit 3bf8e87b3e

@ -18,6 +18,7 @@ choices = [
"xdp", "xdp",
"classifier", "classifier",
"cgroup_skb", "cgroup_skb",
"cgroup_sysctl",
"tracepoint", "tracepoint",
"lsm", "lsm",
"tp_btf" "tp_btf"

@ -284,6 +284,25 @@ use aya_bpf::{
pub fn {{crate_name}}(_ctx: SkBuffContext) -> i64 { pub fn {{crate_name}}(_ctx: SkBuffContext) -> i64 {
return 0 return 0
} }
{%- when "cgroup_sysctl" %}
use aya_bpf::{
macros::cgroup_sysctl,
programs::SysctlContext,
};
use aya_log_ebpf::info;
#[cgroup_sysctl(name="{{crate_name}}")]
pub fn {{crate_name}}(ctx: SysctlContext) -> i32 {
match unsafe { try_{{crate_name}}(ctx) } {
Ok(ret) => ret,
Err(ret) => ret,
}
}
unsafe fn try_{{crate_name}}(ctx: SysctlContext) -> Result<i32, i32> {
info!(&ctx, "sysctl operation called");
Ok(0)
}
{%- endcase %} {%- endcase %}
#[panic_handler] #[panic_handler]

@ -21,6 +21,8 @@ use aya::programs::{Xdp, XdpFlags};
use aya::programs::{tc, SchedClassifier, TcAttachType}; use aya::programs::{tc, SchedClassifier, TcAttachType};
{%- when "cgroup_skb" -%} {%- when "cgroup_skb" -%}
use aya::programs::{CgroupSkb, CgroupSkbAttachType}; use aya::programs::{CgroupSkb, CgroupSkbAttachType};
{%- when "cgroup_sysctl" -%}
use aya::programs::{CgroupSysctl};
{%- when "tracepoint" -%} {%- when "tracepoint" -%}
use aya::programs::TracePoint; use aya::programs::TracePoint;
{%- when "lsm" -%} {%- when "lsm" -%}
@ -43,7 +45,7 @@ struct Opt {
{% if program_type == "xdp" or program_type == "classifier" -%} {% if program_type == "xdp" or program_type == "classifier" -%}
#[clap(short, long, default_value = "eth0")] #[clap(short, long, default_value = "eth0")]
iface: String, iface: String,
{%- elsif program_type == "sock_ops" or program_type == "cgroup_skb" -%} {%- elsif program_type == "sock_ops" or program_type == "cgroup_skb" or program_type == "cgroup_sysctl" -%}
#[clap(short, long, default_value = "/sys/fs/cgroup/unified")] #[clap(short, long, default_value = "/sys/fs/cgroup/unified")]
cgroup_path: String, cgroup_path: String,
{%- elsif program_type == "uprobe" or program_type == "uretprobe" -%} {%- elsif program_type == "uprobe" or program_type == "uretprobe" -%}
@ -145,6 +147,11 @@ async fn main() -> Result<(), anyhow::Error> {
let prog: &mut SocketFilter = bpf.program_mut("{{crate_name}}").unwrap().try_into()?; let prog: &mut SocketFilter = bpf.program_mut("{{crate_name}}").unwrap().try_into()?;
prog.load()?; prog.load()?;
prog.attach(client.as_raw_fd())?; prog.attach(client.as_raw_fd())?;
{%- when "cgroup_sysctl" -%}
let program: &mut CgroupSysctl = bpf.program_mut("{{crate_name}}").unwrap().try_into()?;
let cgroup = std::fs::File::open(opt.cgroup_path)?;
program.load()?;
program.attach(cgroup)?;
{%- endcase %} {%- endcase %}
info!("Waiting for Ctrl-C..."); info!("Waiting for Ctrl-C...");

Loading…
Cancel
Save