From ec1910fffd95dac1d0f30eb8c68b13fc3d202420 Mon Sep 17 00:00:00 2001 From: Michal Rostecki Date: Tue, 29 Mar 2022 14:51:43 +0200 Subject: [PATCH 1/2] Revert "Use release profile for eBPF programs by default" This reverts commit ad9b8ee8dc2b3bcc3f4e203fb46c152b1d228bbb. --- xtask/src/build_ebpf.rs | 13 +++++++------ xtask/src/run.rs | 22 +++++++++++----------- {{project-name}}-ebpf/Cargo.toml | 6 ++++++ {{project-name}}/src/main.rs | 5 +++++ 4 files changed, 29 insertions(+), 17 deletions(-) diff --git a/xtask/src/build_ebpf.rs b/xtask/src/build_ebpf.rs index 4c1fea2..85facdc 100644 --- a/xtask/src/build_ebpf.rs +++ b/xtask/src/build_ebpf.rs @@ -35,24 +35,25 @@ pub struct Options { /// Set the endianness of the BPF target #[structopt(default_value = "bpfel-unknown-none", long)] pub target: Architecture, - /// Build profile for eBPF programs - #[structopt(default_value = "release", long)] - pub profile: String, + /// Build the release target + #[structopt(long)] + pub release: bool, } pub fn build_ebpf(opts: Options) -> Result<(), anyhow::Error> { let dir = PathBuf::from("{{project-name}}-ebpf"); let target = format!("--target={}", opts.target); - let args = vec![ + let mut args = vec![ "+nightly", "build", "--verbose", target.as_str(), "-Z", "build-std=core", - "--profile", - opts.profile.as_str(), ]; + if opts.release { + args.push("--release") + } let status = Command::new("cargo") .current_dir(&dir) .args(&args) diff --git a/xtask/src/run.rs b/xtask/src/run.rs index e717f98..175c410 100644 --- a/xtask/src/run.rs +++ b/xtask/src/run.rs @@ -10,9 +10,9 @@ pub struct Options { /// Set the endianness of the BPF target #[structopt(default_value = "bpfel-unknown-none", long)] pub bpf_target: Architecture, - /// Build profile for userspace program - #[structopt(default_value = "dev", long)] - pub profile: String, + /// Build and run the release target + #[structopt(long)] + pub release: bool, /// The command used to wrap your application #[structopt(short, long, default_value = "sudo -E")] pub runner: String, @@ -23,7 +23,10 @@ pub struct Options { /// Build the project fn build(opts: &Options) -> Result<(), anyhow::Error> { - let args = vec!["build", "--profile", opts.profile.as_str()]; + let mut args = vec!["build"]; + if opts.release { + args.push("--release") + } let status = Command::new("cargo") .args(&args) .status() @@ -37,17 +40,14 @@ pub fn run(opts: Options) -> Result<(), anyhow::Error> { // build our ebpf program followed by our application build_ebpf(BuildOptions { target: opts.bpf_target, - profile: opts.profile.clone(), + release: opts.release, }) .context("Error while building eBPF program")?; build(&opts).context("Error while building userspace application")?; - let target_dir = match opts.profile.as_str() { - "dev" | "test" => "debug", - "bench" | "release" => "release", - _ => opts.profile.as_str(), - }; - let bin_path = format!("target/{}/{{project-name}}", target_dir); + // profile we are building (release or debug) + let profile = if opts.release { "release" } else { "debug" }; + let bin_path = format!("target/{}/{{project-name}}", profile); // arguments to pass to the application let mut run_args: Vec<_> = opts.run_args.iter().map(String::as_str).collect(); diff --git a/{{project-name}}-ebpf/Cargo.toml b/{{project-name}}-ebpf/Cargo.toml index c8ed8b3..4d07f31 100644 --- a/{{project-name}}-ebpf/Cargo.toml +++ b/{{project-name}}-ebpf/Cargo.toml @@ -11,6 +11,12 @@ aya-bpf = { git = "https://github.com/aya-rs/aya", branch = "main" } name = "{{ project-name }}" path = "src/main.rs" +[profile.dev] +panic = "abort" +debug = 1 +opt-level = 2 +overflow-checks = false + [profile.release] panic = "abort" diff --git a/{{project-name}}/src/main.rs b/{{project-name}}/src/main.rs index 1c533c0..1579092 100644 --- a/{{project-name}}/src/main.rs +++ b/{{project-name}}/src/main.rs @@ -65,6 +65,11 @@ async fn main() -> Result<(), anyhow::Error> { // 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}}" ))?; From 04584fe9c5fa2b8b5946275f12548bef295ec55c Mon Sep 17 00:00:00 2001 From: Michal Rostecki Date: Tue, 29 Mar 2022 14:53:04 +0200 Subject: [PATCH 2/2] ebpf: Make the dev target identical to release eBPF programs cannot be debugged and those ones built with the default dev profile are often annoying the verifier. Therefore it doesn't make sense to compile not optimized eBPF objects. However, we still want to let people to use the dev profile, especially in the future when we want to get rid of xtask by using cargo binary dependencies[0]. The trick is to have no real difference between dev and release profile in eBPF. This change doesn't affect the userspace part which still is going to contain debug symbols when built with dev profile. [0] https://rust-lang.github.io/rfcs/3028-cargo-binary-dependencies.html Signed-off-by: Michal Rostecki --- {{project-name}}-ebpf/Cargo.toml | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/{{project-name}}-ebpf/Cargo.toml b/{{project-name}}-ebpf/Cargo.toml index 4d07f31..b83f4cc 100644 --- a/{{project-name}}-ebpf/Cargo.toml +++ b/{{project-name}}-ebpf/Cargo.toml @@ -12,13 +12,20 @@ name = "{{ project-name }}" path = "src/main.rs" [profile.dev] -panic = "abort" -debug = 1 -opt-level = 2 +opt-level = 3 +debug = false +debug-assertions = false overflow-checks = false +lto = true +panic = "abort" +incremental = false +codegen-units = 1 +rpath = false [profile.release] +lto = true panic = "abort" +codegen-units = 1 [workspace] members = []