diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2fc7790..d1de7ad 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -30,6 +30,7 @@ jobs: - cgroup_skb - tracepoint - lsm + - tp_btf steps: - uses: actions/checkout@v2 diff --git a/cargo-generate.toml b/cargo-generate.toml index 83fca28..ae12a41 100644 --- a/cargo-generate.toml +++ b/cargo-generate.toml @@ -5,7 +5,7 @@ ignore = [".github", "test.sh"] [placeholders.program_type] type = "string" prompt = "Which type of eBPF program?" -choices = ["kprobe", "kretprobe", "uprobe", "uretprobe", "sock_ops", "sk_msg", "xdp", "classifier", "cgroup_skb", "tracepoint", "lsm"] +choices = ["kprobe", "kretprobe", "uprobe", "uretprobe", "sock_ops", "sk_msg", "xdp", "classifier", "cgroup_skb", "tracepoint", "lsm", "tp_btf"] default = "xdp" [conditional.'program_type == "kprobe" || program_type == "kretprobe"'.placeholders.kprobe] @@ -35,7 +35,7 @@ type = "string" prompt = "Which tracepoint category? (e.g sched, net etc...)" regex = "[a-z]+" -[conditional.'program_type == "tracepoint"'.placeholders.tracepoint_name] +[conditional.'program_type == "tracepoint" || program_type == "tp_btf"'.placeholders.tracepoint_name] type = "string" prompt = "Which tracepoint name? (e.g sched_switch, net_dev_queue)" regex = "[a-z]+" diff --git a/test.sh b/test.sh index 353450f..6993537 100755 --- a/test.sh +++ b/test.sh @@ -33,6 +33,9 @@ case "$PROG_TYPE" in "lsm") ADDITIONAL_ARGS="-d lsm_hook=file_open" ;; + "tp_btf") + ADDITIONAL_ARGS="-d tracepoint_name=net_dev_queue" + ;; *) ADDITIONAL_ARGS='' esac diff --git a/{{project-name}}-ebpf/src/main.rs b/{{project-name}}-ebpf/src/main.rs index c373228..76f50c4 100644 --- a/{{project-name}}-ebpf/src/main.rs +++ b/{{project-name}}-ebpf/src/main.rs @@ -194,6 +194,23 @@ pub fn {{lsm_hook}}(ctx: LsmContext) -> i32 { unsafe fn try_{{lsm_hook}}(_ctx: LsmContext) -> Result { Ok(0) } +{%- when "tp_btf" %} +use aya_bpf::{ + macros::btf_tracepoint, + programs::BtfTracePointContext, +}; + +#[btf_tracepoint(name="{{tracepoint_name}}")] +pub fn {{tracepoint_name}}(ctx: BtfTracePointContext) -> i32 { + match unsafe { try_{{tracepoint_name}}(ctx) } { + Ok(ret) => ret, + Err(ret) => ret, + } +} + +unsafe fn try_{{tracepoint_name}}(_ctx: BtfTracePointContext) -> Result { + Ok(0) +} {%- endcase %} #[panic_handler] diff --git a/{{project-name}}/src/main.rs b/{{project-name}}/src/main.rs index c25a66d..ec72a00 100644 --- a/{{project-name}}/src/main.rs +++ b/{{project-name}}/src/main.rs @@ -20,6 +20,8 @@ use aya::programs::{CgroupSkb, CgroupSkbAttachType}; use aya::programs::TracePoint; {%- when "lsm" -%} use aya::{programs::Lsm, Btf}; +{%- when "tp_btf" -%} +use aya::{programs::BtfTracePoint, Btf}; {%- endcase %} use std::{ convert::{TryFrom,TryInto}, @@ -98,6 +100,11 @@ fn try_main() -> Result<(), anyhow::Error> { let program: &mut Lsm = bpf.program_mut("{{lsm_hook}}")?.try_into()?; program.load("{{lsm_hook}}", &btf)?; program.attach()?; + {%- when "tp_btf" -%} + let btf = Btf::from_sys_fs()?; + let program: &mut BtfTracePoint = bpf.program_mut("{{tracepoint_name}}")?.try_into()?; + program.load("{{tracepoint_name}}", &btf)?; + program.attach()?; {%- endcase %} let running = Arc::new(AtomicBool::new(true));