From 70475f3b2ac2e846166c742605786c5ea88ef0ef Mon Sep 17 00:00:00 2001 From: Michal Rostecki Date: Wed, 28 Dec 2022 11:13:58 +0100 Subject: [PATCH] Add raw_tracepoint program type Signed-off-by: Michal Rostecki --- .github/workflows/ci.yml | 1 + cargo-generate.toml | 5 +++-- test.sh | 3 +++ {{project-name}}-ebpf/src/main.rs | 19 +++++++++++++++++++ {{project-name}}/src/main.rs | 6 ++++++ 5 files changed, 32 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f06bf15..39648fb 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -36,6 +36,7 @@ jobs: - tracepoint - lsm - tp_btf + - raw_tracepoint steps: - uses: actions/checkout@v2 diff --git a/cargo-generate.toml b/cargo-generate.toml index 567397a..46e34a8 100644 --- a/cargo-generate.toml +++ b/cargo-generate.toml @@ -22,7 +22,8 @@ choices = [ "cgroup_sockopt", "tracepoint", "lsm", - "tp_btf" + "tp_btf", + "raw_tracepoint" ] default = "xdp" @@ -62,7 +63,7 @@ type = "string" prompt = "Which tracepoint category? (e.g sched, net etc...)" regex = "^[a-z_]+$" -[conditional.'program_type == "tracepoint" || program_type == "tp_btf"'.placeholders.tracepoint_name] +[conditional.'program_type == "tracepoint" || program_type == "tp_btf" || program_type == "raw_tracepoint"'.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 5fd2e4a..1028a8f 100755 --- a/test.sh +++ b/test.sh @@ -42,6 +42,9 @@ case "$PROG_TYPE" in "cgroup_sockopt") ADDITIONAL_ARGS="-d sockopt_target=getsockopt" ;; + "raw_tracepoint") + ADDITIONAL_ARGS="-d tracepoint_name=sys_enter" + ;; *) ADDITIONAL_ARGS='' esac diff --git a/{{project-name}}-ebpf/src/main.rs b/{{project-name}}-ebpf/src/main.rs index edd0f38..eee2730 100644 --- a/{{project-name}}-ebpf/src/main.rs +++ b/{{project-name}}-ebpf/src/main.rs @@ -322,6 +322,25 @@ fn try_{{crate_name}}(ctx: SockoptContext) -> Result { info!(&ctx, "{{sockopt_target}} called"); Ok(0) } +{%- when "raw_tracepoint" %} +use aya_bpf::{ + macros::raw_tracepoint, + programs::RawTracePointContext, +}; +use aya_log_ebpf::info; + +#[raw_tracepoint(name="{{crate_name}}")] +pub fn {{crate_name}}(ctx: RawTracePointContext) -> i32 { + match try_{{crate_name}}(ctx) { + Ok(ret) => ret, + Err(ret) => ret, + } +} + +fn try_{{crate_name}}(ctx: RawTracePointContext) -> Result { + info!(&ctx, "tracepoint {{tracepoint_name}} called"); + Ok(0) +} {%- endcase %} #[panic_handler] diff --git a/{{project-name}}/src/main.rs b/{{project-name}}/src/main.rs index a16a6ef..0e96502 100644 --- a/{{project-name}}/src/main.rs +++ b/{{project-name}}/src/main.rs @@ -35,6 +35,8 @@ use aya::{programs::BtfTracePoint, Btf}; use std::net::TcpStream; use std::os::unix::io::AsRawFd; use aya::programs::SocketFilter; +{%- when "raw_tracepoint" -%} +use aya::programs::RawTracePoint; {%- endcase %} use aya_log::BpfLogger; use clap::Parser; @@ -153,6 +155,10 @@ async fn main() -> Result<(), anyhow::Error> { let cgroup = std::fs::File::open(opt.cgroup_path)?; program.load()?; program.attach(cgroup)?; + {%- when "raw_tracepoint" -%} + let program: &mut RawTracePoint = bpf.program_mut("{{crate_name}}").unwrap().try_into()?; + program.load()?; + program.attach("{{tracepoint_name}}")?; {%- endcase %} info!("Waiting for Ctrl-C...");