From a80359acca78fcede3fa834b2619857252747c0b Mon Sep 17 00:00:00 2001 From: William Findlay Date: Wed, 10 Nov 2021 09:03:39 -0500 Subject: [PATCH] userspace: use include_bytes_aligned! instead of --path flag --- README.md | 2 +- xtask/src/run.rs | 8 -------- {{project-name}}/src/main.rs | 17 +++++++++++++---- 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 81bedf9..a90f31f 100644 --- a/README.md +++ b/README.md @@ -24,5 +24,5 @@ cargo build ## Run ```bash -cargo xtask run -p +cargo xtask run ``` diff --git a/xtask/src/run.rs b/xtask/src/run.rs index 63474ee..ca88fc7 100644 --- a/xtask/src/run.rs +++ b/xtask/src/run.rs @@ -16,9 +16,6 @@ pub struct Options { /// The command used to wrap your application #[structopt(short, long, default_value = "sudo -E")] pub runner: String, - /// A convenience flag that will supply `--path /path/to/bpf/object` to your application - #[structopt(short = "p", long)] - pub supply_path: bool, /// Arguments to pass to your application #[structopt(name = "args", last = true)] pub run_args: Vec, @@ -51,14 +48,9 @@ pub fn run(opts: Options) -> Result<(), anyhow::Error> { // profile we are building (release or debug) let profile = if opts.release { "release" } else { "debug" }; let bin_path = format!("target/{}/{{project-name}}", profile); - let bpf_path = format!("target/{}/{}/{{project-name}}", opts.bpf_target, profile); // arguments to pass to the application let mut run_args: Vec<_> = opts.run_args.iter().map(String::as_str).collect(); - if opts.supply_path { - run_args.push("--path"); - run_args.push(bpf_path.as_str()); - }; // configure args let mut args: Vec<_> = opts.runner.trim().split_terminator(" ").collect(); diff --git a/{{project-name}}/src/main.rs b/{{project-name}}/src/main.rs index ec72a00..dab02c3 100644 --- a/{{project-name}}/src/main.rs +++ b/{{project-name}}/src/main.rs @@ -1,4 +1,4 @@ -use aya::Bpf; +use aya::{Bpf, include_bytes_aligned}; {% case program_type -%} {%- when "kprobe", "kretprobe" -%} use aya::programs::KProbe; @@ -40,8 +40,6 @@ fn main() { #[derive(Debug, StructOpt)] struct Opt { - #[structopt(short, long)] - path: String, {% if program_type == "xdp" or program_type == "classifier" -%} #[structopt(short, long, default_value = "eth0")] iface: String, @@ -56,7 +54,18 @@ struct Opt { fn try_main() -> Result<(), anyhow::Error> { let opt = Opt::from_args(); - let mut bpf = Bpf::load_file(&opt.path)?; + // This will include youe eBPF object file as raw bytes at compile-time and load it at + // runtime. This approach is recommended for most real-world use cases. If you would + // like to specify the eBPF program at runtime rather than at compile-time, you can + // reach for `Bpf::load_file` instead. + #[cfg(debug_assertions)] + let mut bpf = Bpf::load(include_bytes_aligned!( + "../../target/bpfel-unknown-none/debug/{{project-name}}" + ))?; + #[cfg(not(debug_assertions))] + let mut bpf = Bpf::load(include_bytes_aligned!( + "../../target/bpfel-unknown-none/release/{{project-name}}" + ))?; {% case program_type -%} {%- when "kprobe", "kretprobe" -%} let program: &mut KProbe = bpf.program_mut("{{crate_name}}")?.try_into()?;