diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1b69a51..beac01a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -47,15 +47,15 @@ jobs: - tp_btf - tracepoint include: - - runner: macos-13 # x86 + - runner: macos-15-intel # x86 program: kprobe - - runner: macos-14 # arm64 + - runner: macos-15 # arm64 program: kprobe runs-on: ${{ matrix.runner }} steps: - - uses: actions/checkout@v4 + - uses: actions/checkout@v5 - uses: dtolnay/rust-toolchain@nightly with: @@ -83,7 +83,7 @@ jobs: - run: brew update && brew install filosottile/musl-cross/musl-cross llvm if: runner.os == 'macos' - - run: cargo install bpf-linker --git https://github.com/aya-rs/bpf-linker.git --no-default-features + - run: cargo install bpf-linker --git https://github.com/aya-rs/bpf-linker.git --no-default-features --features llvm-21 if: runner.os == 'macos' - run: cargo install bpf-linker --git https://github.com/aya-rs/bpf-linker.git diff --git a/Cargo.toml b/Cargo.toml index d0d3678..68dd41f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,11 +12,11 @@ license = "MIT OR Apache-2.0" edition = "2024" [workspace.dependencies] -aya = { version = "0.13.1", default-features = false } -aya-build = { version = "0.1.2", default-features = false } -aya-ebpf = { version = "0.1.1", default-features = false } -aya-log = { version = "0.2.1", default-features = false } -aya-log-ebpf = { version = "0.1.1", default-features = false } +aya = { git = "https://github.com/aya-rs/aya", default-features = false } +aya-build = { git = "https://github.com/aya-rs/aya", default-features = false } +aya-ebpf = { git = "https://github.com/aya-rs/aya", default-features = false } +aya-log = { git = "https://github.com/aya-rs/aya", default-features = false } +aya-log-ebpf = { git = "https://github.com/aya-rs/aya", default-features = false } anyhow = { version = "1", default-features = false } # `std` feature is currently required to build `clap`. diff --git a/cargo-generate.toml b/cargo-generate.toml index efee760..493fb1c 100644 --- a/cargo-generate.toml +++ b/cargo-generate.toml @@ -28,6 +28,10 @@ choices = [ ] default = "xdp" +[conditional.'program_type == "classifier" || program_type == "xdp"'.placeholders.default_iface] +type = "string" +prompt = "Which interface to attach to by default? (e.g eth0)" + [conditional.'program_type == "kprobe" || program_type == "kretprobe"'.placeholders.kprobe] type = "string" prompt = "Where to attach the (k|kret)probe? (e.g try_to_wake_up)" diff --git a/test.sh b/test.sh index 8198d7e..6c0aad8 100755 --- a/test.sh +++ b/test.sh @@ -18,9 +18,15 @@ case ${PROG_TYPE} in "cgroup_sockopt") ADDITIONAL_ARGS=(-d sockopt_target=getsockopt) ;; -"classifier" | "cgroup_skb") +"cgroup_skb") ADDITIONAL_ARGS=(-d direction=Ingress) ;; +"classifier") + ADDITIONAL_ARGS=(-d direction=Ingress -d default_iface=eth0) + ;; +"xdp") + ADDITIONAL_ARGS=(-d default_iface=eth0) + ;; "fentry" | "fexit") ADDITIONAL_ARGS=(-d fn_name=try_to_wake_up) ;; diff --git a/{{project-name}}-ebpf/src/main.rs b/{{project-name}}-ebpf/src/main.rs index 18233ae..7a780d4 100644 --- a/{{project-name}}-ebpf/src/main.rs +++ b/{{project-name}}-ebpf/src/main.rs @@ -183,7 +183,7 @@ pub fn {{crate_name}}(ctx: SkBuffContext) -> i32 { fn try_{{crate_name}}(ctx: SkBuffContext) -> Result { info!(&ctx, "received a packet"); - Ok(0) + Ok(1) } {%- when "tracepoint" %} use aya_ebpf::{macros::tracepoint, programs::TracePointContext}; diff --git a/{{project-name}}/build.rs b/{{project-name}}/build.rs index 6e2ce43..c30640a 100644 --- a/{{project-name}}/build.rs +++ b/{{project-name}}/build.rs @@ -1,5 +1,5 @@ use anyhow::{Context as _, anyhow}; -use aya_build::cargo_metadata; +use aya_build::{Toolchain, cargo_metadata}; fn main() -> anyhow::Result<()> { let cargo_metadata::Metadata { packages, .. } = cargo_metadata::MetadataCommand::new() @@ -8,7 +8,7 @@ fn main() -> anyhow::Result<()> { .context("MetadataCommand::exec")?; let ebpf_package = packages .into_iter() - .find(|cargo_metadata::Package { name, .. }| name == "{{project-name}}-ebpf") + .find(|cargo_metadata::Package { name, .. }| name.as_str() == "{{project-name}}-ebpf") .ok_or_else(|| anyhow!("{{project-name}}-ebpf package not found"))?; - aya_build::build_ebpf([ebpf_package]) + aya_build::build_ebpf([ebpf_package], Toolchain::default()) } diff --git a/{{project-name}}/src/main.rs b/{{project-name}}/src/main.rs index 5c6e8b9..86cf0cf 100644 --- a/{{project-name}}/src/main.rs +++ b/{{project-name}}/src/main.rs @@ -58,7 +58,7 @@ use tokio::signal; struct Opt { {%- case program_type -%} {%- when "xdp", "classifier" %} - #[clap(short, long, default_value = "eth0")] + #[clap(short, long, default_value = "{{default_iface}}")] iface: String, {%- when "sock_ops", "cgroup_skb", "cgroup_sysctl", "cgroup_sockopt" %} #[clap(short, long, default_value = "/sys/fs/cgroup")] @@ -96,9 +96,22 @@ async fn main() -> anyhow::Result<()> { env!("OUT_DIR"), "/{{project-name}}" )))?; - if let Err(e) = aya_log::EbpfLogger::init(&mut ebpf) { - // This can happen if you remove all log statements from your eBPF program. - warn!("failed to initialize eBPF logger: {e}"); + match aya_log::EbpfLogger::init(&mut ebpf) { + Err(e) => { + // This can happen if you remove all log statements from your eBPF program. + warn!("failed to initialize eBPF logger: {e}"); + } + Ok(logger) => { + let mut logger = + tokio::io::unix::AsyncFd::with_interest(logger, tokio::io::Interest::READABLE)?; + tokio::task::spawn(async move { + loop { + let mut guard = logger.readable_mut().await.unwrap(); + guard.get_inner_mut().flush(); + guard.clear_ready(); + } + }); + } } {%- case program_type -%} {%- when "kprobe", "kretprobe" %} @@ -119,7 +132,7 @@ async fn main() -> anyhow::Result<()> { let Opt { pid } = opt; let program: &mut UProbe = ebpf.program_mut("{{crate_name}}").unwrap().try_into()?; program.load()?; - program.attach(Some("{{uprobe_fn_name}}"), 0, "{{uprobe_target}}", pid)?; + program.attach("{{uprobe_fn_name}}", "{{uprobe_target}}", pid, None /* cookie */)?; {%- when "sock_ops", "cgroup_skb", "cgroup_sysctl", "cgroup_sockopt" %} let Opt { cgroup_path } = opt; let cgroup =