Add cgroup_sockopt template for BPF_PROG_TYPE_CGROUP_SOCKOPT

pull/47/head
Kenjiro Nakayama 3 years ago
parent c6b6f1ed3d
commit 8621ea7d5f

@ -32,6 +32,7 @@ jobs:
- classifier - classifier
- cgroup_skb - cgroup_skb
- cgroup_sysctl - cgroup_sysctl
- cgroup_sockopt
- tracepoint - tracepoint
- lsm - lsm
- tp_btf - tp_btf

@ -19,6 +19,7 @@ choices = [
"classifier", "classifier",
"cgroup_skb", "cgroup_skb",
"cgroup_sysctl", "cgroup_sysctl",
"cgroup_sockopt",
"tracepoint", "tracepoint",
"lsm", "lsm",
"tp_btf" "tp_btf"
@ -46,6 +47,11 @@ type = "string"
prompt = "Attach direction?" prompt = "Attach direction?"
choices = [ "Ingress", "Egress" ] choices = [ "Ingress", "Egress" ]
[conditional.'program_type == "cgroup_sockopt"'.placeholders.sockopt_target]
type = "string"
prompt = "Which socket option?"
choices = [ "getsockopt", "setsockopt" ]
[conditional.'program_type == "sk_msg"'.placeholders.sock_map] [conditional.'program_type == "sk_msg"'.placeholders.sock_map]
type = "string" type = "string"
prompt = "Map Name (UPPER_CASE)?" prompt = "Map Name (UPPER_CASE)?"

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

@ -22,7 +22,9 @@ 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" -%} {%- when "cgroup_sysctl" -%}
use aya::programs::{CgroupSysctl}; use aya::programs::CgroupSysctl;
{%- when "cgroup_sockopt" -%}
use aya::programs::CgroupSockopt;
{%- when "tracepoint" -%} {%- when "tracepoint" -%}
use aya::programs::TracePoint; use aya::programs::TracePoint;
{%- when "lsm" -%} {%- when "lsm" -%}
@ -45,7 +47,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" or program_type == "cgroup_sysctl" -%} {%- elsif program_type == "sock_ops" or program_type == "cgroup_skb" or program_type == "cgroup_sysctl" or "cgroup_sockopt" -%}
#[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" -%}
@ -152,6 +154,11 @@ async fn main() -> Result<(), anyhow::Error> {
let cgroup = std::fs::File::open(opt.cgroup_path)?; let cgroup = std::fs::File::open(opt.cgroup_path)?;
program.load()?; program.load()?;
program.attach(cgroup)?; program.attach(cgroup)?;
{%- when "cgroup_sockopt" -%}
let program: &mut CgroupSockopt = 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