Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace xtask builds with build scripts #124

Merged
merged 4 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ jobs:
strategy:
fail-fast: false
matrix:
rust:
- stable
- 1.80.1
program:
- kprobe
- kretprobe
Expand All @@ -47,10 +50,11 @@ jobs:

- uses: dtolnay/rust-toolchain@nightly
with:
components: rust-src
components: rust-src,rustfmt

- uses: dtolnay/rust-toolchain@stable
- uses: dtolnay/rust-toolchain@master
with:
toolchain: ${{ matrix.rust }}
components: clippy

- uses: Swatinem/rust-cache@v2
Expand Down
13 changes: 5 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ aya-log = { version = "0.2.1", default-features = false }
aya-log-ebpf = { version = "0.1.1", default-features = false }

anyhow = { version = "1", default-features = false }
cargo_metadata = { version = "0.18.0", default-features = false }
# `std` feature is currently required to build `clap`.
#
# See https://github.com/clap-rs/clap/blob/61f5ee5/clap_builder/src/lib.rs#L15.
Expand All @@ -18,18 +19,14 @@ env_logger = { version = "0.11.5", default-features = false }
libc = { version = "0.2.159", default-features = false }
log = { version = "0.4.22", default-features = false }
tokio = { version = "1.40.0", default-features = false }
which = { version = "6.0.0", default-features = false }

[profile.dev]
opt-level = 3
debug = false
overflow-checks = false
lto = true
panic = "abort"
incremental = false
codegen-units = 1
rpath = false

[profile.release]
lto = true
panic = "abort"

[profile.release.package.{{project-name}}-ebpf]
debug = 2
codegen-units = 1
29 changes: 5 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,10 @@

1. Install bpf-linker: `cargo install bpf-linker`

## Build eBPF
## Build & Run

```bash
cargo xtask build-ebpf
```
Use `cargo build`, `cargo check`, etc. as normal. Run your program with `xtask run`.

To perform a release build you can use the `--release` flag.
You may also change the target architecture with the `--target` flag.

## Build Userspace

```bash
cargo build
```

## Build eBPF and Userspace

```bash
cargo xtask build
```

## Run

```bash
RUST_LOG=info cargo xtask run
```
Cargo build scripts are used to automatically build the eBPF correctly and include it in the
program. When not using `xtask run`, eBPF code generation is skipped for a faster developer
experience; this compromise necessitates the use of `xtask` to actually build the eBPF.
4 changes: 4 additions & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
group_imports = "StdExternalCrate"
imports_granularity = "Crate"
reorder_imports = true
unstable_features = true
43 changes: 22 additions & 21 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,60 +3,61 @@
set -ex

TEMPLATE_DIR=$1
if [ -z "$TEMPLATE_DIR" ]; then echo "template dir required"; exit 1; fi
if [ -z "${TEMPLATE_DIR}" ]; then echo "template dir required"; exit 1; fi
PROG_TYPE=$2
if [ -z "$PROG_TYPE" ]; then echo "program type required"; exit 1; fi
if [ -z "${PROG_TYPE}" ]; then echo "program type required"; exit 1; fi

TMP_DIR=$(mktemp -d)
clean_up() {
# shellcheck disable=SC2317
rm -rf "${TMP_DIR}"
}
trap clean_up EXIT

pushd $TMP_DIR
case "$PROG_TYPE" in
pushd "${TMP_DIR}"
case "${PROG_TYPE}" in
"cgroup_sockopt")
ADDITIONAL_ARGS="-d sockopt_target=getsockopt"
ADDITIONAL_ARGS=(-d sockopt_target=getsockopt)
;;
"classifier"|"cgroup_skb")
ADDITIONAL_ARGS="-d direction=Ingress"
ADDITIONAL_ARGS=(-d direction=Ingress)
;;
"fentry"|"fexit")
ADDITIONAL_ARGS="-d fn_name=try_to_wake_up"
ADDITIONAL_ARGS=(-d fn_name=try_to_wake_up)
;;
"kprobe"|"kretprobe")
ADDITIONAL_ARGS="-d kprobe=test"
ADDITIONAL_ARGS=(-d kprobe=test)
;;
"lsm")
ADDITIONAL_ARGS="-d lsm_hook=file_open"
ADDITIONAL_ARGS=(-d lsm_hook=file_open)
;;
"raw_tracepoint")
ADDITIONAL_ARGS="-d tracepoint_name=sys_enter"
ADDITIONAL_ARGS=(-d tracepoint_name=sys_enter)
;;
"sk_msg")
ADDITIONAL_ARGS="-d sock_map=SOCK_MAP"
ADDITIONAL_ARGS=(-d sock_map=SOCK_MAP)
;;
"tp_btf")
ADDITIONAL_ARGS="-d tracepoint_name=net_dev_queue"
ADDITIONAL_ARGS=(-d tracepoint_name=net_dev_queue)
;;
"tracepoint")
ADDITIONAL_ARGS="-d tracepoint_category=net -d tracepoint_name=net_dev_queue"
ADDITIONAL_ARGS=(-d tracepoint_category=net -d tracepoint_name=net_dev_queue)
;;
"uprobe"|"uretprobe")
ADDITIONAL_ARGS="-d uprobe_target=testlib -d uprobe_fn_name=testfn"
ADDITIONAL_ARGS=(-d uprobe_target=testlib -d uprobe_fn_name=testfn)
;;
*)
ADDITIONAL_ARGS=''
ADDITIONAL_ARGS=()
esac

cargo generate --path "${TEMPLATE_DIR}" -n test -d program_type="${PROG_TYPE}" ${ADDITIONAL_ARGS}
cargo generate --path "${TEMPLATE_DIR}" -n test -d program_type="${PROG_TYPE}" "${ADDITIONAL_ARGS[@]}"
pushd test
cargo xtask build
cargo xtask build --release
cargo +nightly fmt --all -- --check
cargo build --package test
cargo build --package test --release
# We cannot run clippy over the whole workspace at once due to feature unification. Since both test
# and test-ebpf both depend on test-common and test activates test-common's aya dependency, we end
# up trying to compile the panic handler twice: once from the bpf program, and again from std via
# aya.
# and test-ebpf depend on test-common and test activates test-common's aya dependency, we end up
# trying to compile the panic handler twice: once from the bpf program, and again from std via aya.
cargo clippy --exclude test-ebpf --all-targets --workspace -- --deny warnings
cargo clippy --package test-ebpf --all-targets -- --deny warnings
popd
Expand Down
2 changes: 1 addition & 1 deletion xtask/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ version = "0.1.0"
edition = "2021"

[dependencies]
anyhow = { workspace = true }
anyhow = { workspace = true, default-features = true }
clap = { workspace = true, default-features = true, features = ["derive"] }
41 changes: 0 additions & 41 deletions xtask/src/build.rs

This file was deleted.

68 changes: 0 additions & 68 deletions xtask/src/build_ebpf.rs

This file was deleted.

1 change: 1 addition & 0 deletions xtask/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub const AYA_BUILD_EBPF: &str = "AYA_BUILD_EBPF";
23 changes: 5 additions & 18 deletions xtask/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
mod build_ebpf;
mod build;
mod run;

use std::process::exit;

use anyhow::Result;
use clap::Parser;

#[derive(Debug, Parser)]
Expand All @@ -14,23 +11,13 @@ pub struct Options {

#[derive(Debug, Parser)]
enum Command {
BuildEbpf(build_ebpf::Options),
Build(build::Options),
Run(run::Options),
}

fn main() {
let opts = Options::parse();

use Command::*;
let ret = match opts.command {
BuildEbpf(opts) => build_ebpf::build_ebpf(opts),
Run(opts) => run::run(opts),
Build(opts) => build::build(opts),
};
fn main() -> Result<()> {
let Options { command } = Parser::parse();

if let Err(e) = ret {
eprintln!("{e:#}");
exit(1);
match command {
Command::Run(opts) => run::run(opts),
}
}
Loading