bpf: add PerCpuArray

perf-event
Alessandro Decina 3 years ago
parent c56a6b16aa
commit 269be03a89

@ -1,5 +1,6 @@
pub mod array; pub mod array;
pub mod hash_map; pub mod hash_map;
pub mod per_cpu_array;
pub mod perf_map; pub mod perf_map;
pub mod queue; pub mod queue;
pub mod sock_hash; pub mod sock_hash;
@ -8,6 +9,7 @@ pub mod stack_trace;
pub use array::Array; pub use array::Array;
pub use hash_map::HashMap; pub use hash_map::HashMap;
pub use per_cpu_array::PerCpuArray;
pub use perf_map::PerfMap; pub use perf_map::PerfMap;
pub use queue::Queue; pub use queue::Queue;
pub use sock_hash::SockHash; pub use sock_hash::SockHash;

@ -0,0 +1,59 @@
use core::{marker::PhantomData, mem};
use aya_bpf_cty::c_void;
use crate::{
bindings::{bpf_map_def, bpf_map_type::BPF_MAP_TYPE_PERCPU_ARRAY},
helpers::bpf_map_lookup_elem,
};
#[repr(transparent)]
pub struct PerCpuArray<T> {
def: bpf_map_def,
_t: PhantomData<T>,
}
impl<T> PerCpuArray<T> {
pub const fn with_max_entries(max_entries: u32, flags: u32) -> PerCpuArray<T> {
PerCpuArray {
def: bpf_map_def {
type_: BPF_MAP_TYPE_PERCPU_ARRAY,
key_size: mem::size_of::<u32>() as u32,
value_size: mem::size_of::<T>() as u32,
max_entries,
map_flags: flags,
id: 0,
pinning: 0,
},
_t: PhantomData,
}
}
#[inline(always)]
pub unsafe fn get(&mut self, index: u32) -> Option<&T> {
let value = bpf_map_lookup_elem(
&mut self.def as *mut _ as *mut _,
&index as *const _ as *const c_void,
);
if value.is_null() {
None
} else {
// FIXME: alignment
Some(&*(value as *const T))
}
}
#[inline(always)]
pub unsafe fn get_mut(&mut self, index: u32) -> Option<&mut T> {
let value = bpf_map_lookup_elem(
&mut self.def as *mut _ as *mut _,
&index as *const _ as *const c_void,
);
if value.is_null() {
None
} else {
// FIXME: alignment
Some(&mut *(value as *mut T))
}
}
}
Loading…
Cancel
Save