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 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 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)?; 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 } 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)?; 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 { 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;