Merge branch 'aya-rs:main' into main

pull/185/head
Tarun Tiwari 3 years ago committed by GitHub
commit 5833dbabe1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -28,7 +28,7 @@ jobs:
- uses: Swatinem/rust-cache@v1 - uses: Swatinem/rust-cache@v1
- name: Build - name: Build
run: cargo build --verbose run: cargo build --manifest-path bpf/Cargo.toml --verbose
- name: Run tests - name: Run tests
run: RUST_BACKTRACE=full cargo test --verbose run: RUST_BACKTRACE=full cargo test --manifest-path bpf/Cargo.toml --verbose

@ -47,7 +47,7 @@ jobs:
env: env:
MIRIFLAGS: -Zmiri-disable-stacked-borrows MIRIFLAGS: -Zmiri-disable-stacked-borrows
run: | run: |
cargo miri test cargo miri test --all-targets
pushd bpf pushd bpf
cargo miri test cargo miri test
popd popd

@ -72,7 +72,7 @@ let ingress: &mut CgroupSkb = bpf.program_mut("ingress_filter")?.try_into()?;
// load the program into the kernel // load the program into the kernel
ingress.load()?; 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. // incoming packets.
let cgroup = File::open("/sys/fs/cgroup/unified")?; let cgroup = File::open("/sys/fs/cgroup/unified")?;
ingress.attach(cgroup, CgroupSkbAttachType::Ingress)?; ingress.attach(cgroup, CgroupSkbAttachType::Ingress)?;

@ -17,7 +17,7 @@ object = { version = "0.28", default-features = false, features = ["std", "read_
bitflags = "1.2.1" bitflags = "1.2.1"
bytes = "1" bytes = "1"
lazy_static = "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"] } 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 } tokio = { version = "1.2.0", features = ["macros", "rt", "rt-multi-thread", "net"], optional = true }
async-std = { version = "1.9.0", optional = true } async-std = { version = "1.9.0", optional = true }

@ -71,7 +71,7 @@ let ingress: &mut CgroupSkb = bpf.program_mut("ingress_filter")?.try_into()?;
// load the program into the kernel // load the program into the kernel
ingress.load()?; 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. // incoming packets.
let cgroup = File::open("/sys/fs/cgroup/unified")?; let cgroup = File::open("/sys/fs/cgroup/unified")?;
ingress.attach(cgroup, CgroupSkbAttachType::Ingress)?; ingress.attach(cgroup, CgroupSkbAttachType::Ingress)?;

@ -40,6 +40,11 @@ impl<K, V> HashMap<K, V> {
get(&mut self.def, key) get(&mut self.def, key)
} }
#[inline]
pub fn get_mut(&mut self, key: &K) -> Option<&mut V> {
get_mut(&mut self.def, key)
}
#[inline] #[inline]
pub fn insert(&mut self, key: &K, value: &V, flags: u64) -> Result<(), c_long> { pub fn insert(&mut self, key: &K, value: &V, flags: u64) -> Result<(), c_long> {
insert(&mut self.def, key, value, flags) insert(&mut self.def, key, value, flags)
@ -85,6 +90,11 @@ impl<K, V> LruHashMap<K, V> {
get(&mut self.def, key) get(&mut self.def, key)
} }
#[inline]
pub fn get_mut(&mut self, key: &K) -> Option<&mut V> {
get_mut(&mut self.def, key)
}
#[inline] #[inline]
pub fn insert(&mut self, key: &K, value: &V, flags: u64) -> Result<(), c_long> { pub fn insert(&mut self, key: &K, value: &V, flags: u64) -> Result<(), c_long> {
insert(&mut self.def, key, value, flags) insert(&mut self.def, key, value, flags)
@ -135,6 +145,11 @@ impl<K, V> PerCpuHashMap<K, V> {
get(&mut self.def, key) get(&mut self.def, key)
} }
#[inline]
pub fn get_mut(&mut self, key: &K) -> Option<&mut V> {
get_mut(&mut self.def, key)
}
#[inline] #[inline]
pub fn insert(&mut self, key: &K, value: &V, flags: u64) -> Result<(), c_long> { pub fn insert(&mut self, key: &K, value: &V, flags: u64) -> Result<(), c_long> {
insert(&mut self.def, key, value, flags) insert(&mut self.def, key, value, flags)
@ -185,6 +200,11 @@ impl<K, V> LruPerCpuHashMap<K, V> {
get(&mut self.def, key) get(&mut self.def, key)
} }
#[inline]
pub fn get_mut(&mut self, key: &K) -> Option<&mut V> {
get_mut(&mut self.def, key)
}
#[inline] #[inline]
pub fn insert(&mut self, key: &K, value: &V, flags: u64) -> Result<(), c_long> { pub fn insert(&mut self, key: &K, value: &V, flags: u64) -> Result<(), c_long> {
insert(&mut self.def, key, value, flags) 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] #[inline]
fn insert<K, V>(def: &mut bpf_map_def, key: &K, value: &V, flags: u64) -> Result<(), c_long> { fn insert<K, V>(def: &mut bpf_map_def, key: &K, value: &V, flags: u64) -> Result<(), c_long> {
let ret = unsafe { let ret = unsafe {

@ -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,
}
}

@ -7,6 +7,7 @@ pub(crate) enum PinningType {
pub mod array; pub mod array;
pub mod hash_map; pub mod hash_map;
pub mod lpm_trie;
pub mod per_cpu_array; pub mod per_cpu_array;
pub mod perf; pub mod perf;
pub mod program_array; pub mod program_array;
@ -17,6 +18,7 @@ pub mod stack_trace;
pub use array::Array; pub use array::Array;
pub use hash_map::{HashMap, LruHashMap, LruPerCpuHashMap, PerCpuHashMap}; pub use hash_map::{HashMap, LruHashMap, LruPerCpuHashMap, PerCpuHashMap};
pub use lpm_trie::LpmTrie;
pub use per_cpu_array::PerCpuArray; pub use per_cpu_array::PerCpuArray;
pub use perf::{PerfEventArray, PerfEventByteArray}; pub use perf::{PerfEventArray, PerfEventByteArray};
pub use program_array::ProgramArray; pub use program_array::ProgramArray;

Loading…
Cancel
Save