From ab7eed2759d062fbe267e7e96f84c7a3f477ef11 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 31 Jan 2022 13:12:39 +0000 Subject: [PATCH 1/7] build(deps): update parking_lot requirement from 0.11.1 to 0.12.0 Updates the requirements on [parking_lot](https://github.com/Amanieu/parking_lot) to permit the latest version. - [Release notes](https://github.com/Amanieu/parking_lot/releases) - [Changelog](https://github.com/Amanieu/parking_lot/blob/master/CHANGELOG.md) - [Commits](https://github.com/Amanieu/parking_lot/compare/0.11.1...0.12.0) --- updated-dependencies: - dependency-name: parking_lot dependency-type: direct:production ... Signed-off-by: dependabot[bot] <support@github.com> --- aya/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aya/Cargo.toml b/aya/Cargo.toml index cba636ca..da744867 100644 --- a/aya/Cargo.toml +++ b/aya/Cargo.toml @@ -17,7 +17,7 @@ object = { version = "0.28", default-features = false, features = ["std", "read_ bitflags = "1.2.1" bytes = "1" lazy_static = "1" -parking_lot = { version = "0.11.1", features = ["send_guard"] } +parking_lot = { version = "0.12.0", features = ["send_guard"] } futures = { version = "0.3.12", optional = true, default-features = false, features = ["std"] } tokio = { version = "1.2.0", features = ["macros", "rt", "rt-multi-thread", "net"], optional = true } async-std = { version = "1.9.0", optional = true } From 1f047f0dbe8d915ee645c043a616753d2ba45131 Mon Sep 17 00:00:00 2001 From: Dave Tucker <dave@dtucker.co.uk> Date: Wed, 9 Feb 2022 16:32:01 +0000 Subject: [PATCH 2/7] ci: Fix aya-bpf workflow This was testing the aya crate with the nightly toolchain which wasn't what was intented Signed-off-by: Dave Tucker <dave@dtucker.co.uk> --- .github/workflows/build-aya-bpf.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build-aya-bpf.yml b/.github/workflows/build-aya-bpf.yml index d331aec3..e54054cb 100644 --- a/.github/workflows/build-aya-bpf.yml +++ b/.github/workflows/build-aya-bpf.yml @@ -28,7 +28,7 @@ jobs: - uses: Swatinem/rust-cache@v1 - name: Build - run: cargo build --verbose + run: cargo build --manifest-path bpf/Cargo.toml --verbose - name: Run tests - run: RUST_BACKTRACE=full cargo test --verbose + run: RUST_BACKTRACE=full cargo test --manifest-path bpf/Cargo.toml --verbose From 3d820ee4731547497cf10e95fc8652df93b28105 Mon Sep 17 00:00:00 2001 From: Dave Tucker <dave@dtucker.co.uk> Date: Thu, 10 Feb 2022 13:20:49 +0000 Subject: [PATCH 3/7] ci: lint: aya: Skip doctests with miri Signed-off-by: Dave Tucker <dave@dtucker.co.uk> --- .github/workflows/lint.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 674a9b16..06d58809 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -47,7 +47,7 @@ jobs: env: MIRIFLAGS: -Zmiri-disable-stacked-borrows run: | - cargo miri test + cargo miri test --all-targets pushd bpf cargo miri test popd \ No newline at end of file From d0b6daa0914348110db63be3ef7350ffb31a99a6 Mon Sep 17 00:00:00 2001 From: Nimrod Shneor <nimrodshn@gmail.com> Date: Thu, 10 Feb 2022 12:02:15 +0000 Subject: [PATCH 4/7] Add LPMTrie Map to aya-bpf for BPF programs --- bpf/aya-bpf/src/maps/lpm_trie.rs | 101 +++++++++++++++++++++++++++++++ bpf/aya-bpf/src/maps/mod.rs | 2 + 2 files changed, 103 insertions(+) create mode 100644 bpf/aya-bpf/src/maps/lpm_trie.rs diff --git a/bpf/aya-bpf/src/maps/lpm_trie.rs b/bpf/aya-bpf/src/maps/lpm_trie.rs new file mode 100644 index 00000000..41888f77 --- /dev/null +++ b/bpf/aya-bpf/src/maps/lpm_trie.rs @@ -0,0 +1,101 @@ +use core::{marker::PhantomData, mem, ptr::NonNull}; + +use aya_bpf_cty::{c_long, c_void}; + +use crate::{ + bindings::{bpf_map_def, bpf_map_type::BPF_MAP_TYPE_LPM_TRIE}, + helpers::{bpf_map_delete_elem, bpf_map_lookup_elem, bpf_map_update_elem}, + maps::PinningType, +}; + +#[repr(transparent)] +pub struct LpmTrie<K, V> { + def: bpf_map_def, + _k: PhantomData<K>, + _v: PhantomData<V>, +} + +#[repr(packed)] +pub struct Key<K> { + /// Represents the number of bytes matched against. + pub prefix_len: u32, + /// Represents arbitrary data stored in the LpmTrie. + pub data: K, +} + +impl<K> Key<K> { + pub fn new(prefix_len: u32, data: K) -> Self { + Self { prefix_len, data } + } +} + +impl<K, V> LpmTrie<K, V> { + pub const fn with_max_entries(max_entries: u32, flags: u32) -> LpmTrie<K, V> { + LpmTrie { + def: build_def::<K, V>(BPF_MAP_TYPE_LPM_TRIE, max_entries, flags, PinningType::None), + _k: PhantomData, + _v: PhantomData, + } + } + + pub const fn pinned(max_entries: u32, flags: u32) -> LpmTrie<K, V> { + LpmTrie { + def: build_def::<K, V>( + BPF_MAP_TYPE_LPM_TRIE, + max_entries, + flags, + PinningType::ByName, + ), + _k: PhantomData, + _v: PhantomData, + } + } + + #[inline] + pub fn get(&mut self, key: &Key<K>) -> Option<&V> { + unsafe { + let value = bpf_map_lookup_elem( + &mut self.def as *mut _ as *mut _, + key as *const _ as *const c_void, + ); + // FIXME: alignment + NonNull::new(value as *mut V).map(|p| p.as_ref()) + } + } + + #[inline] + pub fn insert(&mut self, key: &Key<K>, value: &V, flags: u64) -> Result<(), c_long> { + let ret = unsafe { + bpf_map_update_elem( + &mut self.def as *mut _ as *mut _, + key as *const _ as *const _, + value as *const _ as *const _, + flags, + ) + }; + (ret >= 0).then(|| ()).ok_or(ret) + } + + #[inline] + pub fn remove(&mut self, key: &Key<K>) -> Result<(), c_long> { + let ret = unsafe { + bpf_map_delete_elem( + &mut self.def as *mut _ as *mut _, + key as *const _ as *const c_void, + ) + }; + (ret >= 0).then(|| ()).ok_or(ret) + } +} + +const fn build_def<K, V>(ty: u32, max_entries: u32, flags: u32, pin: PinningType) -> bpf_map_def { + bpf_map_def { + type_: ty, + key_size: mem::size_of::<Key<K>>() as u32, + value_size: mem::size_of::<V>() as u32, + max_entries, + map_flags: flags, + id: 0, + pinning: pin as u32, + } +} diff --git a/bpf/aya-bpf/src/maps/mod.rs b/bpf/aya-bpf/src/maps/mod.rs index c3ae05ed..2d8ba4c9 100644 --- a/bpf/aya-bpf/src/maps/mod.rs +++ b/bpf/aya-bpf/src/maps/mod.rs @@ -7,6 +7,7 @@ pub(crate) enum PinningType { pub mod array; pub mod hash_map; +pub mod lpm_trie; pub mod per_cpu_array; pub mod perf; pub mod program_array; @@ -17,6 +18,7 @@ pub mod stack_trace; pub use array::Array; pub use hash_map::{HashMap, LruHashMap, LruPerCpuHashMap, PerCpuHashMap}; +pub use lpm_trie::LpmTrie; pub use per_cpu_array::PerCpuArray; pub use perf::{PerfEventArray, PerfEventByteArray}; pub use program_array::ProgramArray; From ebb91f3c6f1931f52f8068a3098aad804b0e8b86 Mon Sep 17 00:00:00 2001 From: Daniil Bondarev <xonatius@gmail.com> Date: Wed, 16 Feb 2022 16:24:24 -0800 Subject: [PATCH 5/7] Fix typo in README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 6dfa3ce6..478c8e90 100644 --- a/README.md +++ b/README.md @@ -72,7 +72,7 @@ let ingress: &mut CgroupSkb = bpf.program_mut("ingress_filter")?.try_into()?; // load the program into the kernel ingress.load()?; -// attach th program to the root cgroup. `ingress_filter` will be called for all +// attach the program to the root cgroup. `ingress_filter` will be called for all // incoming packets. let cgroup = File::open("/sys/fs/cgroup/unified")?; ingress.attach(cgroup, CgroupSkbAttachType::Ingress)?; From 49e998dc7eb248b5fe31a03bc1ca4db7e736bac7 Mon Sep 17 00:00:00 2001 From: Daniil Bondarev <xonatius@gmail.com> Date: Wed, 16 Feb 2022 16:27:10 -0800 Subject: [PATCH 6/7] Fix typo in aya/README.md --- aya/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aya/README.md b/aya/README.md index 46788dd4..e7798ca5 100644 --- a/aya/README.md +++ b/aya/README.md @@ -71,7 +71,7 @@ let ingress: &mut CgroupSkb = bpf.program_mut("ingress_filter")?.try_into()?; // load the program into the kernel ingress.load()?; -// attach th program to the root cgroup. `ingress_filter` will be called for all +// attach the program to the root cgroup. `ingress_filter` will be called for all // incoming packets. let cgroup = File::open("/sys/fs/cgroup/unified")?; ingress.attach(cgroup, CgroupSkbAttachType::Ingress)?; From ecd6a6e96bf2a316a62f92adb910b04c6198a000 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alessandro=C2=A0Decina?= <alessandro.d@gmail.com> Date: Sat, 19 Feb 2022 10:07:24 +0000 Subject: [PATCH 7/7] bpf: hash_map: add get_mut() method --- bpf/aya-bpf/src/maps/hash_map.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/bpf/aya-bpf/src/maps/hash_map.rs b/bpf/aya-bpf/src/maps/hash_map.rs index e77b4135..90fb467e 100644 --- a/bpf/aya-bpf/src/maps/hash_map.rs +++ b/bpf/aya-bpf/src/maps/hash_map.rs @@ -40,6 +40,11 @@ impl<K, V> HashMap<K, V> { get(&mut self.def, key) } + #[inline] + pub fn get_mut(&mut self, key: &K) -> Option<&mut V> { + get_mut(&mut self.def, key) + } + #[inline] pub fn insert(&mut self, key: &K, value: &V, flags: u64) -> Result<(), c_long> { insert(&mut self.def, key, value, flags) @@ -85,6 +90,11 @@ impl<K, V> LruHashMap<K, V> { get(&mut self.def, key) } + #[inline] + pub fn get_mut(&mut self, key: &K) -> Option<&mut V> { + get_mut(&mut self.def, key) + } + #[inline] pub fn insert(&mut self, key: &K, value: &V, flags: u64) -> Result<(), c_long> { insert(&mut self.def, key, value, flags) @@ -135,6 +145,11 @@ impl<K, V> PerCpuHashMap<K, V> { get(&mut self.def, key) } + #[inline] + pub fn get_mut(&mut self, key: &K) -> Option<&mut V> { + get_mut(&mut self.def, key) + } + #[inline] pub fn insert(&mut self, key: &K, value: &V, flags: u64) -> Result<(), c_long> { insert(&mut self.def, key, value, flags) @@ -185,6 +200,11 @@ impl<K, V> LruPerCpuHashMap<K, V> { get(&mut self.def, key) } + #[inline] + pub fn get_mut(&mut self, key: &K) -> Option<&mut V> { + get_mut(&mut self.def, key) + } + #[inline] pub fn insert(&mut self, key: &K, value: &V, flags: u64) -> Result<(), c_long> { insert(&mut self.def, key, value, flags) @@ -217,6 +237,15 @@ fn get<'a, K, V>(def: &mut bpf_map_def, key: &K) -> Option<&'a V> { } } +#[inline] +fn get_mut<'a, K, V>(def: &mut bpf_map_def, key: &K) -> Option<&'a mut V> { + unsafe { + let value = bpf_map_lookup_elem(def as *mut _ as *mut _, key as *const _ as *const c_void); + // FIXME: alignment + NonNull::new(value as *mut V).map(|mut p| p.as_mut()) + } +} + #[inline] fn insert<K, V>(def: &mut bpf_map_def, key: &K, value: &V, flags: u64) -> Result<(), c_long> { let ret = unsafe {