Skip to content

Commit

Permalink
ci: run clippy,rustfmt over examples
Browse files Browse the repository at this point in the history
  • Loading branch information
tamird authored and vadorovsky committed Nov 27, 2024
1 parent eb64c7c commit cfaed29
Show file tree
Hide file tree
Showing 78 changed files with 246 additions and 355 deletions.
22 changes: 7 additions & 15 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,14 @@ jobs:
with:
globs: '**/*.md'

- name: Install latest stable
uses: dtolnay/rust-toolchain@master
- uses: dtolnay/rust-toolchain@nightly
with:
toolchain: stable
components: rustfmt,rust-src

- name: Install latest nightly
uses: dtolnay/rust-toolchain@master
# Installed *after* nightly so it is the default.
- uses: dtolnay/rust-toolchain@stable
with:
toolchain: nightly
components: rust-src
toolchain: stable

- uses: Swatinem/rust-cache@v2
with:
Expand Down Expand Up @@ -67,11 +65,5 @@ jobs:
run: mkdocs build

- name: Check Examples
run: |
for dir in ./examples/*/ ; do
pushd "${dir}"
echo "Example: ${dir}."
cargo xtask build-ebpf --release
cargo build --release
popd
done
working-directory: examples
run: ./test.sh
2 changes: 1 addition & 1 deletion docs/book/aya/lifecycle.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use aya::programs::{Xdp, XdpFlags};
fn main() {
{
// (1)
let mut bpf = Bpf::load_file("bpf.o"))?;
let mut bpf = Ebpf::load_file("bpf.o"))?;

let program: &mut Xdp = bpf.program_mut("xdp").unwrap().try_into().unwrap();
// (2)
Expand Down
4 changes: 2 additions & 2 deletions docs/book/programs/xdp.md
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ provide our network interface name.
let mut blocklist: HashMap<_, u32, u32> =
HashMap::try_from(bpf.map_mut("BLOCKLIST").unwrap())?;

let block_addr: u32 = Ipv4Addr::new(1, 1, 1, 1).try_into()?;
let block_addr: u32 = Ipv4Addr::new(1, 1, 1, 1).into();

blocklist.insert(block_addr, 0, 0)?;

Expand Down Expand Up @@ -599,7 +599,7 @@ The program awaits the `CTRL+C` signal asynchronously using
let mut blocklist: HashMap<_, u32, u32> =
HashMap::try_from(bpf.map_mut("BLOCKLIST").unwrap())?;

let block_addr: u32 = Ipv4Addr::new(1, 1, 1, 1).try_into()?;
let block_addr: u32 = Ipv4Addr::new(1, 1, 1, 1).into();

blocklist.insert(block_addr, 0, 0)?;

Expand Down
2 changes: 1 addition & 1 deletion docs/book/start/hello-xdp.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ Let's look at the details of our generated user-space application:
1. Here's our main entry point
1. `include_bytes_aligned!()` copies the contents of the BPF ELF object file at
the compile time
1. `Bpf::load()` reads the BPF ELF object file contents from the output of the
1. `Ebpf::load()` reads the BPF ELF object file contents from the output of the
previous command, creates any maps, performs BTF relocations
1. We extract the XDP program
1. And then load it in to the kernel
Expand Down
2 changes: 1 addition & 1 deletion examples/aya-tool/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[alias]
xtask = "run --package xtask --"
xtask = "run --package xtask --"
7 changes: 7 additions & 0 deletions examples/aya-tool/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
[workspace]
members = ["xtask", "myapp", "myapp-common", "myapp-ebpf"]

resolver = "2"

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

[profile.release.package.myapp-ebpf]
debug = 2
codegen-units = 1
2 changes: 1 addition & 1 deletion examples/aya-tool/myapp-ebpf/.cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ target-dir = "../target"
target = "bpfel-unknown-none"

[unstable]
build-std = ["core"]
build-std = ["core"]
9 changes: 0 additions & 9 deletions examples/aya-tool/myapp-ebpf/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,3 @@ myapp-common = { path = "../myapp-common" }
[[bin]]
name = "myapp"
path = "src/main.rs"

[profile.dev]
panic = "abort"
debug = 1
opt-level = 2
overflow-checks = false

[profile.release]
panic = "abort"
2 changes: 0 additions & 2 deletions examples/aya-tool/myapp-ebpf/rust-toolchain.toml

This file was deleted.

8 changes: 5 additions & 3 deletions examples/aya-tool/myapp-ebpf/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#![no_std]
#![no_main]

#[allow(non_upper_case_globals)]
#[allow(non_snake_case)]
#[allow(non_camel_case_types)]
#[allow(clippy::all)]
#[allow(dead_code)]
#[allow(non_camel_case_types)]
#[allow(non_snake_case)]
#[allow(non_upper_case_globals)]
#[rustfmt::skip]
mod vmlinux;

use aya_ebpf::{
Expand Down
10 changes: 5 additions & 5 deletions examples/aya-tool/myapp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ myapp-common = { path = "../myapp-common", features = ["user"] }
anyhow = "1"
ctrlc = "3.2"
tokio = { version = "1.25", features = [
"macros",
"rt",
"rt-multi-thread",
"net",
"signal",
"macros",
"rt",
"rt-multi-thread",
"net",
"signal",
] }
env_logger = "0.11"
log = "0.4"
Expand Down
8 changes: 4 additions & 4 deletions examples/aya-tool/myapp/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use aya::{include_bytes_aligned, programs::Lsm, Bpf, Btf};
use aya::{include_bytes_aligned, programs::Lsm, Btf, Ebpf};
use log::info;
use std::{
sync::{
Expand All @@ -21,13 +21,13 @@ fn try_main() -> Result<(), anyhow::Error> {
// This will include your 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.
// reach for `Ebpf::load_file` instead.
#[cfg(debug_assertions)]
let mut bpf = Bpf::load(include_bytes_aligned!(
let mut bpf = Ebpf::load(include_bytes_aligned!(
"../../target/bpfel-unknown-none/debug/myapp"
))?;
#[cfg(not(debug_assertions))]
let mut bpf = Bpf::load(include_bytes_aligned!(
let mut bpf = Ebpf::load(include_bytes_aligned!(
"../../target/bpfel-unknown-none/release/myapp"
))?;

Expand Down
13 changes: 2 additions & 11 deletions examples/aya-tool/xtask/src/build_ebpf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,14 @@ pub struct Options {
pub fn build_ebpf(opts: Options) -> Result<(), anyhow::Error> {
let dir = PathBuf::from("myapp-ebpf");
let target = format!("--target={}", opts.target);
let mut args = vec![
"build",
target.as_str(),
"-Z",
"build-std=core",
];
let mut args = vec!["build", target.as_str(), "-Z", "build-std=core"];
if opts.release {
args.push("--release")
}

// Command::new creates a child process which inherits all env variables. This means env
// vars set by the cargo xtask command are also inherited. RUSTUP_TOOLCHAIN is removed
// so the rust-toolchain.toml file in the -ebpf folder is honored.

let status = Command::new("cargo")
.arg("+nightly")
.current_dir(dir)
.env_remove("RUSTUP_TOOLCHAIN")
.args(&args)
.status()
.expect("failed to build bpf program");
Expand Down
2 changes: 1 addition & 1 deletion examples/cgroup-skb-egress/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[alias]
xtask = "run --package xtask --"
xtask = "run --package xtask --"
14 changes: 13 additions & 1 deletion examples/cgroup-skb-egress/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
[workspace]
members = ["xtask", "cgroup-skb-egress", "cgroup-skb-egress-common", "cgroup-skb-egress-ebpf"]
members = [
"xtask",
"cgroup-skb-egress",
"cgroup-skb-egress-common",
"cgroup-skb-egress-ebpf",
]

resolver = "2"

default-members = ["xtask", "cgroup-skb-egress", "cgroup-skb-egress-common"]

[profile.release.package.cgroup-skb-egress-ebpf]
debug = 2
codegen-units = 1
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ edition = "2021"

[features]
default = []
user = [ "aya" ]
user = ["aya"]

[dependencies]
aya = { git = "https://github.com/aya-rs/aya", optional=true }
aya = { git = "https://github.com/aya-rs/aya", optional = true }

[lib]
path = "src/lib.rs"
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ target-dir = "../target"
target = "bpfel-unknown-none"

[unstable]
build-std = ["core"]
build-std = ["core"]
16 changes: 0 additions & 16 deletions examples/cgroup-skb-egress/cgroup-skb-egress-ebpf/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,3 @@ memoffset = "0.9"
[[bin]]
name = "cgroup-skb-egress"
path = "src/main.rs"

[profile.dev]
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

This file was deleted.

9 changes: 4 additions & 5 deletions examples/cgroup-skb-egress/cgroup-skb-egress-ebpf/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ use memoffset::offset_of;

use cgroup_skb_egress_common::PacketLog;

#[allow(clippy::all)]
#[allow(non_upper_case_globals)]
#[allow(non_snake_case)]
#[allow(non_camel_case_types)]
#[allow(dead_code)]
#[rustfmt::skip]
mod bindings;
use bindings::iphdr;

Expand All @@ -25,10 +27,7 @@ static BLOCKLIST: HashMap<u32, u32> = HashMap::with_max_entries(1024, 0);

#[cgroup_skb]
pub fn cgroup_skb_egress(ctx: SkBuffContext) -> i32 {
match { try_cgroup_skb_egress(ctx) } {
Ok(ret) => ret,
Err(_) => 0,
}
try_cgroup_skb_egress(ctx).unwrap_or(0)
}

// (2)
Expand All @@ -49,7 +48,7 @@ fn try_cgroup_skb_egress(ctx: SkBuffContext) -> Result<i32, i64> {

let log_entry = PacketLog {
ipv4_address: destination,
action: action,
action,
};
EVENTS.output(&ctx, &log_entry, 0);
Ok(action)
Expand Down
12 changes: 6 additions & 6 deletions examples/cgroup-skb-egress/cgroup-skb-egress/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ publish = false
aya = { git = "https://github.com/aya-rs/aya", features = ["async_tokio"] }
aya-log = { git = "https://github.com/aya-rs/aya" }
cgroup-skb-egress-common = { path = "../cgroup-skb-egress-common", features = [
"user",
"user",
] }
anyhow = "1"
clap = { version = "4.1", features = ["derive"] }
env_logger = "0.11"
log = "0.4"
tokio = { version = "1.25", features = [
"macros",
"rt",
"rt-multi-thread",
"net",
"signal",
"macros",
"rt",
"rt-multi-thread",
"net",
"signal",
] }
bytes = "1"

Expand Down
2 changes: 1 addition & 1 deletion examples/cgroup-skb-egress/cgroup-skb-egress/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ async fn main() -> Result<(), anyhow::Error> {
let mut blocklist: HashMap<_, u32, u32> =
HashMap::try_from(bpf.map_mut("BLOCKLIST").unwrap())?;

let block_addr: u32 = Ipv4Addr::new(1, 1, 1, 1).try_into()?;
let block_addr: u32 = Ipv4Addr::new(1, 1, 1, 1).into();

// (3)
blocklist.insert(block_addr, 0, 0)?;
Expand Down
13 changes: 2 additions & 11 deletions examples/cgroup-skb-egress/xtask/src/build_ebpf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,14 @@ pub struct Options {
pub fn build_ebpf(opts: Options) -> Result<(), anyhow::Error> {
let dir = PathBuf::from("cgroup-skb-egress-ebpf");
let target = format!("--target={}", opts.target);
let mut args = vec![
"build",
target.as_str(),
"-Z",
"build-std=core",
];
let mut args = vec!["build", target.as_str(), "-Z", "build-std=core"];
if opts.release {
args.push("--release")
}

// Command::new creates a child process which inherits all env variables. This means env
// vars set by the cargo xtask command are also inherited. RUSTUP_TOOLCHAIN is removed
// so the rust-toolchain.toml file in the -ebpf folder is honored.

let status = Command::new("cargo")
.arg("+nightly")
.current_dir(&dir)
.env_remove("RUSTUP_TOOLCHAIN")
.args(&args)
.status()
.expect("failed to build bpf program");
Expand Down
2 changes: 1 addition & 1 deletion examples/kprobetcp/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[alias]
xtask = "run --package xtask --"
xtask = "run --package xtask --"
8 changes: 8 additions & 0 deletions examples/kprobetcp/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
[workspace]
members = ["xtask", "kprobetcp", "kprobetcp-common", "kprobetcp-ebpf"]

resolver = "2"

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


[profile.release.package.kprobetcp-ebpf]
debug = 2
codegen-units = 1
2 changes: 1 addition & 1 deletion examples/kprobetcp/kprobetcp-ebpf/.cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ target-dir = "../target"
target = "bpfel-unknown-none"

[unstable]
build-std = ["core"]
build-std = ["core"]
16 changes: 0 additions & 16 deletions examples/kprobetcp/kprobetcp-ebpf/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,3 @@ kprobetcp-common = { path = "../kprobetcp-common" }
[[bin]]
name = "kprobetcp"
path = "src/main.rs"

[profile.dev]
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
2 changes: 0 additions & 2 deletions examples/kprobetcp/kprobetcp-ebpf/rust-toolchain.toml

This file was deleted.

Loading

0 comments on commit cfaed29

Please sign in to comment.