Merge pull request #21 from willfindlay/include_bytes_aligned

userspace: use include_bytes_aligned! instead of --path flag
pull/23/head
Dave Tucker 3 years ago committed by GitHub
commit 8be4317287
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -24,5 +24,5 @@ cargo build
## Run
```bash
cargo xtask run -p
cargo xtask run
```

@ -42,7 +42,7 @@ esac
cargo generate -v --path "${TEMPLATE_DIR}" -n test -d program_type="${PROG_TYPE}" ${ADDITIONAL_ARGS}
pushd test
cargo build
cargo xtask build-ebpf
cargo build
popd
exit 0

@ -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<String>,
@ -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();

@ -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()?;

Loading…
Cancel
Save