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

Update for build scripts #188

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
13 changes: 4 additions & 9 deletions docs/book/programs/cgroup-skb.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,6 @@ easily reproduce it in the future by adding the following code:

<!-- markdownlint-disable MD046 -->

=== "xtask/src/codegen.rs"

```rust linenums="1"
--8<-- "examples/cgroup-skb-egress/xtask/src/codegen.rs"
```

=== "xtask/Cargo.toml"

```toml linenums="1"
Expand All @@ -68,8 +62,9 @@ easily reproduce it in the future by adding the following code:

<!-- markdownlint-enable MD046 -->

Once we've generated our file using `cargo xtask codegen` from the root of the
project, we can access it by including `mod bindings` from eBPF code.
Once we've generated our file using
`cargo xtask codegen cgroup-skb-egress-ebpf/src/bindings.rs` we can access it by
including `mod bindings` from eBPF code.

## eBPF code

Expand Down Expand Up @@ -131,7 +126,7 @@ mkdir /sys/fs/cgroup/foo
Then run the program with:

```console
RUST_LOG=info cargo xtask run
RUST_LOG=info cargo run --config 'target."cfg(all())".runner="sudo -E"'
```

And then, in a separate terminal, as root, try to access `1.1.1.1`:
Expand Down
2 changes: 1 addition & 1 deletion docs/book/programs/classifiers.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ is an appropriate type to use in eBPF maps.
## Running the program

```console
$ RUST_LOG=info cargo xtask run
$ RUST_LOG=info cargo run --config 'target."cfg(all())".runner="sudo -E"'
LOG: SRC 1.1.1.1, ACTION 2
LOG: SRC 35.186.224.47, ACTION 3
LOG: SRC 35.186.224.47, ACTION 3
Expand Down
2 changes: 1 addition & 1 deletion docs/book/programs/lsm.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ The final result should look like:
After that, we can build and run our project with:

```console
RUST_LOG=info cargo xtask run
RUST_LOG=info cargo run --config 'target."cfg(all())".runner="sudo -E"'
```

The output should contain our log line showing the PID of the userspace
Expand Down
2 changes: 1 addition & 1 deletion docs/book/programs/probes.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ Here's how the code looks like:
<!-- markdownlint-disable MD013 -->

```console
$ RUST_LOG=info cargo xtask run --release
$ RUST_LOG=info cargo run --config 'target."cfg(all())".runner="sudo -E"'
[2022-12-28T20:50:00Z INFO kprobetcp] Waiting for Ctrl-C...
[2022-12-28T20:50:05Z INFO kprobetcp] AF_INET6 src addr: 2001:4998:efeb:282::249, dest addr: 2606:2800:220:1:248:1893:25c8:1946
[2022-12-28T20:50:11Z INFO kprobetcp] AF_INET src address: 10.53.149.148, dest address: 10.87.116.72
Expand Down
35 changes: 12 additions & 23 deletions docs/book/programs/xdp.md
Original file line number Diff line number Diff line change
Expand Up @@ -429,10 +429,8 @@ Let's begin with writing the user-space code:
```rust
use anyhow::Context;
use aya::{
include_bytes_aligned,
maps::HashMap,
programs::{Xdp, XdpFlags},
Ebpf,
};
use aya_log::EbpfLogger;
use clap::Parser;
Expand Down Expand Up @@ -482,14 +480,10 @@ async fn main() -> Result<(), anyhow::Error> {

env_logger::init();

#[cfg(debug_assertions)]
let mut bpf = Ebpf::load(include_bytes_aligned!(
"../../target/bpfel-unknown-none/debug/simple-xdp-program"
))?;
#[cfg(not(debug_assertions))]
let mut bpf = Ebpf::load(include_bytes_aligned!(
"../../target/bpfel-unknown-none/release/xdp-simple-xdp-program"
))?;
let mut bpf = aya::Ebpf::load(aya::include_bytes_aligned!(concat!(
env!("OUT_DIR"),
"/simple-xdp-program"
)))?;
if let Err(e) = EbpfLogger::init(&mut bpf) {
warn!("failed to initialize eBPF logger: {}", e);
}
Expand Down Expand Up @@ -552,10 +546,8 @@ The program awaits the `CTRL+C` signal asynchronously using
```rust
use anyhow::Context;
use aya::{
include_bytes_aligned,
maps::HashMap,
programs::{Xdp, XdpFlags},
Ebpf,
};
use aya_log::EbpfLogger;
use clap::Parser;
Expand All @@ -575,14 +567,10 @@ async fn main() -> Result<(), anyhow::Error> {

env_logger::init();

#[cfg(debug_assertions)]
let mut bpf = Ebpf::load(include_bytes_aligned!(
"../../target/bpfel-unknown-none/debug/simple-xdp-program"
))?;
#[cfg(not(debug_assertions))]
let mut bpf = Ebpf::load(include_bytes_aligned!(
"../../target/bpfel-unknown-none/release/xdp-simple-xdp-program"
))?;
let mut bpf = aya::Ebpf::load(aya::include_bytes_aligned!(concat!(
env!("OUT_DIR"),
"/simple-xdp-program"
)))?;
if let Err(e) = EbpfLogger::init(&mut bpf) {
warn!("failed to initialize eBPF logger: {}", e);
}
Expand Down Expand Up @@ -614,17 +602,18 @@ async fn main() -> Result<(), anyhow::Error> {
Now that we have all the pieces for our eBPF program, we can run it using:

```console
RUST_LOG=info cargo xtask run
RUST_LOG=info cargo run --config 'target."cfg(all())".runner="sudo -E"'
```

or

```console
RUST_LOG=info cargo xtask run -- --iface <interface>
RUST_LOG=info cargo run --config 'target."cfg(all())".runner="sudo -E"' -- \
--iface <interface>
```

if you want to provide another network interface name. note that you can also
use `cargo xtask run` without the rest, but you won't get any logging.
omit `RUST_LOG=info`, but you won't get any logging.

[source-code]: https://github.com/aya-rs/book/tree/main/examples/xdp-drop
[af-xdp]: https://www.kernel.org/doc/html/latest/networking/af_xdp.html
Expand Down
2 changes: 1 addition & 1 deletion docs/book/start/dropping-packets.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ Here's how the userspace code looks:
## Running the program

```console
$ RUST_LOG=info cargo xtask run
$ RUST_LOG=info cargo run --config 'target."cfg(all())".runner="sudo -E"'
[2022-10-04T12:46:05Z INFO xdp_drop] SRC: 1.1.1.1, ACTION: 1
[2022-10-04T12:46:05Z INFO xdp_drop] SRC: 192.168.1.21, ACTION: 2
[2022-10-04T12:46:05Z INFO xdp_drop] SRC: 192.168.1.21, ACTION: 2
Expand Down
18 changes: 10 additions & 8 deletions docs/book/start/hello-xdp.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ currently looks like this:
1. Write a log entry every time a packet is received.
1. This function returns a `Result` that permits all traffic.

Now we can compile this using `cargo xtask build-ebpf`.
Now we can compile this using `cargo build`.

### Verifying The Program

Expand Down Expand Up @@ -141,10 +141,7 @@ Let's look at the details of our generated user-space application:
Let's try it out!

```console
$ cargo xtask run -- -h
Finished dev [unoptimized + debuginfo] target(s) in 0.05s
Running `target/debug/xtask run -- -h`
:
$ cargo run -- -h
Finished dev [optimized] target(s) in 0.90s
Finished dev [unoptimized + debuginfo] target(s) in 0.60s
xdp-hello
Expand All @@ -160,11 +157,16 @@ OPTIONS:
> [!NOTE] Interface Name
> This command assumes the interface is `eth0` by default. If you wish to
> attach to an interface with another name, use
> `RUST_LOG=info cargo xtask run -- --iface wlp2s0`, where `wlp2s0` is your
> interface.
>
> ```console
> RUST_LOG=info cargo run --config 'target."cfg(all())".runner="sudo -E"' -- \
> --iface wlp2s0
> ```
>
> where `wlp2s0` is your interface.

```console
$ RUST_LOG=info cargo xtask run
$ RUST_LOG=info cargo run --config 'target."cfg(all())".runner="sudo -E"'
[2022-12-21T18:03:09Z INFO xdp_hello] Waiting for Ctrl-C...
[2022-12-21T18:03:11Z INFO xdp_hello] received a packet
[2022-12-21T18:03:11Z INFO xdp_hello] received a packet
Expand Down
2 changes: 0 additions & 2 deletions examples/aya-tool/.cargo/config.toml

This file was deleted.

4 changes: 2 additions & 2 deletions examples/aya-tool/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
[workspace]
members = ["xtask", "myapp", "myapp-common", "myapp-ebpf"]
members = ["myapp", "myapp-common", "myapp-ebpf"]

resolver = "2"

default-members = ["xtask", "myapp", "myapp-common"]
default-members = ["myapp", "myapp-common"]

[profile.release.package.myapp-ebpf]
debug = 2
Expand Down
21 changes: 4 additions & 17 deletions examples/aya-tool/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,10 @@
1. Install a rust nightly toolchain: `rustup install nightly`
1. Install bpf-linker: `cargo install bpf-linker`

## Build eBPF
## Build & Run

```console
cargo xtask build-ebpf
```

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

## Build Userspace

```console
cargo build
```

## Run
Use `cargo build`, `cargo check`, etc. as normal. Run your program with:

```console
cargo xtask run
```shell
cargo run --config 'target."cfg(all())".runner="sudo -E"'
```
6 changes: 0 additions & 6 deletions examples/aya-tool/myapp-ebpf/.cargo/config.toml

This file was deleted.

3 changes: 3 additions & 0 deletions examples/aya-tool/myapp-ebpf/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ aya-ebpf = { git = "https://github.com/aya-rs/aya" }
aya-log-ebpf = { git = "https://github.com/aya-rs/aya" }
myapp-common = { path = "../myapp-common" }

[build-dependencies]
which = { version = "6.0.0", default-features = false }

[[bin]]
name = "myapp"
path = "src/main.rs"
17 changes: 17 additions & 0 deletions examples/aya-tool/myapp-ebpf/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use which::which;

/// Building this crate has an undeclared dependency on the `bpf-linker` binary. This would be
/// better expressed by [artifact-dependencies][bindeps] but issues such as
/// https://github.com/rust-lang/cargo/issues/12385 make their use impractical for the time being.
///
/// This file implements an imperfect solution: it causes cargo to rebuild the crate whenever the
/// mtime of `which bpf-linker` changes. Note that possibility that a new bpf-linker is added to
/// $PATH ahead of the one used as the cache key still exists. Solving this in the general case
/// would require rebuild-if-changed-env=PATH *and* rebuild-if-changed={every-directory-in-PATH}
/// which would likely mean far too much cache invalidation.
///
/// [bindeps]: https://doc.rust-lang.org/nightly/cargo/reference/unstable.html?highlight=feature#artifact-dependencies
fn main() {
let bpf_linker = which("bpf-linker").unwrap();
println!("cargo:rerun-if-changed={}", bpf_linker.to_str().unwrap());
}
16 changes: 16 additions & 0 deletions examples/aya-tool/myapp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,22 @@ tokio = { version = "1.25", features = [
env_logger = "0.11"
log = "0.4"

[build-dependencies]
cargo_metadata = { version = "0.18.0", default-features = false }
# TODO(https://github.com/rust-lang/cargo/issues/12375): this should be an artifact dependency, but
# it's not possible to tell cargo to use `-Z build-std` to build it. We cargo-in-cargo in the build
# script to build this, but we want to teach cargo about the dependecy so that cache invalidation
# works properly.
#
# Note also that https://github.com/rust-lang/cargo/issues/10593 occurs when `target = ...` is added
# to an artifact dependency; it seems possible to work around that by setting `resolver = "1"` in
# Cargo.toml in the workspace root.
#
# Finally note that *any* usage of `artifact = ...` in *any* Cargo.toml in the workspace breaks
# workflows with stable cargo; stable cargo outright refuses to load manifests that use unstable
# features.
myapp-ebpf = { path = "../myapp-ebpf" }

[[bin]]
name = "myapp"
path = "src/main.rs"
Loading