@ -1,49 +1,107 @@
use core ::{ cell ::UnsafeCell , marker::PhantomData , mem , ptr::NonNull } ;
use core ::{ cell ::UnsafeCell , ptr::NonNull } ;
use aya_bpf_bindings ::bindings ::bpf_map_type ::{
use aya_bpf_bindings ::bindings ::bpf_map_type ::{
BPF_MAP_TYPE_LRU_HASH , BPF_MAP_TYPE_LRU_PERCPU_HASH , BPF_MAP_TYPE_PERCPU_HASH ,
BPF_MAP_TYPE_LRU_HASH , BPF_MAP_TYPE_LRU_PERCPU_HASH , BPF_MAP_TYPE_PERCPU_HASH ,
} ;
} ;
use aya_bpf_cty ::{ c_long , c_void } ;
use aya_bpf_cty ::{ c_long , c_void } ;
#[ cfg(not(feature = " btf-maps " )) ]
use crate ::{ bindings ::bpf_map_def , maps ::PinningType } ;
use crate ::{
use crate ::{
bindings ::{ bpf_map_def , bpf_map_type ::BPF_MAP_TYPE_HASH } ,
bindings ::bpf_map_type ::BPF_MAP_TYPE_HASH ,
helpers ::{ bpf_map_delete_elem , bpf_map_lookup_elem , bpf_map_update_elem } ,
helpers ::{ bpf_map_delete_elem , bpf_map_lookup_elem , bpf_map_update_elem } ,
maps ::PinningType ,
} ;
} ;
#[ cfg(feature = " btf-maps " ) ]
#[ repr(transparent) ]
pub struct HashMap < K , V , const MAX_ENTRIES : usize , const FLAGS : usize = 0 > {
def : UnsafeCell < super ::MapDef < K , V , BPF_MAP_TYPE_HASH , MAX_ENTRIES , FLAGS > > ,
}
#[ cfg(not(feature = " btf-maps " )) ]
#[ repr(transparent) ]
#[ repr(transparent) ]
pub struct HashMap < K , V > {
pub struct HashMap < K , V > {
def : UnsafeCell < bpf_map_def > ,
def : UnsafeCell < bpf_map_def > ,
_k : PhantomData < K > ,
_k : core::marker :: PhantomData< K > ,
_v : PhantomData < V > ,
_v : core::marker :: PhantomData< V > ,
}
}
#[ cfg(feature = " btf-maps " ) ]
unsafe impl < K : Sync , V : Sync , const MAX_ENTRIES : usize , const FLAGS : usize > Sync
for HashMap < K , V , MAX_ENTRIES , FLAGS >
{
}
#[ cfg(not(feature = " btf-maps " )) ]
unsafe impl < K : Sync , V : Sync > Sync for HashMap < K , V > { }
unsafe impl < K : Sync , V : Sync > Sync for HashMap < K , V > { }
#[ cfg(feature = " btf-maps " ) ]
impl < K , V , const MAX_ENTRIES : usize , const FLAGS : usize > HashMap < K , V , MAX_ENTRIES , FLAGS > {
pub const fn new ( ) -> Self {
Self {
def : UnsafeCell ::new ( super ::MapDef ::new ( ) ) ,
}
}
/// Retrieve the value associate with `key` from the map.
/// This function is unsafe. Unless the map flag `BPF_F_NO_PREALLOC` is used, the kernel does not
/// make guarantee on the atomicity of `insert` or `remove`, and any element removed from the
/// map might get aliased by another element in the map, causing garbage to be read, or
/// corruption in case of writes.
#[ inline ]
pub unsafe fn get ( & self , key : & K ) -> Option < & V > {
get ( self . def . get ( ) as * mut _ , key )
}
/// Retrieve the value associate with `key` from the map.
/// The same caveat as `get` applies, but this returns a raw pointer and it's up to the caller
/// to decide whether it's safe to dereference the pointer or not.
#[ inline ]
pub fn get_ptr ( & self , key : & K ) -> Option < * const V > {
get_ptr ( self . def . get ( ) as * mut _ , key )
}
/// Retrieve the value associate with `key` from the map.
/// The same caveat as `get` applies, and additionally cares should be taken to avoid
/// concurrent writes, but it's up to the caller to decide whether it's safe to dereference the
/// pointer or not.
#[ inline ]
pub fn get_ptr_mut ( & self , key : & K ) -> Option < * mut V > {
get_ptr_mut ( self . def . get ( ) as * mut _ , key )
}
#[ inline ]
pub fn insert ( & self , key : & K , value : & V , flags : u64 ) -> Result < ( ) , c_long > {
insert ( self . def . get ( ) as * mut _ , key , value , flags )
}
#[ inline ]
pub fn remove ( & self , key : & K ) -> Result < ( ) , c_long > {
remove ( self . def . get ( ) as * mut _ , key )
}
}
#[ cfg(not(feature = " btf-maps " )) ]
impl < K , V > HashMap < K , V > {
impl < K , V > HashMap < K , V > {
pub const fn with_max_entries ( max_entries : u32 , flags : u32 ) -> HashMap < K , V > {
pub const fn with_max_entries ( max_entries : u32 , flags : u32 ) -> HashMap < K , V > {
HashMap {
HashMap {
def : UnsafeCell ::new ( build_def ::< K , V > (
def : UnsafeCell ::new ( build_def ::< K , V > (
BPF_MAP_TYPE_HASH ,
BPF_MAP_TYPE_HASH as u32 ,
max_entries ,
max_entries ,
flags ,
flags ,
PinningType ::None ,
PinningType ::None ,
) ) ,
) ) ,
_k : PhantomData ,
_k : core::marker :: PhantomData,
_v : PhantomData ,
_v : core::marker :: PhantomData,
}
}
}
}
pub const fn pinned ( max_entries : u32 , flags : u32 ) -> HashMap < K , V > {
pub const fn pinned ( max_entries : u32 , flags : u32 ) -> HashMap < K , V > {
HashMap {
HashMap {
def : UnsafeCell ::new ( build_def ::< K , V > (
def : UnsafeCell ::new ( build_def ::< K , V > (
BPF_MAP_TYPE_HASH ,
BPF_MAP_TYPE_HASH as u32 ,
max_entries ,
max_entries ,
flags ,
flags ,
PinningType ::ByName ,
PinningType ::ByName ,
) ) ,
) ) ,
_k : PhantomData ,
_k : core::marker :: PhantomData,
_v : PhantomData ,
_v : core::marker :: PhantomData,
}
}
}
}
@ -54,7 +112,7 @@ impl<K, V> HashMap<K, V> {
/// corruption in case of writes.
/// corruption in case of writes.
#[ inline ]
#[ inline ]
pub unsafe fn get ( & self , key : & K ) -> Option < & V > {
pub unsafe fn get ( & self , key : & K ) -> Option < & V > {
get ( self . def . get ( ) , key )
get ( self . def . get ( ) as * mut _ , key )
}
}
/// Retrieve the value associate with `key` from the map.
/// Retrieve the value associate with `key` from the map.
@ -62,7 +120,7 @@ impl<K, V> HashMap<K, V> {
/// to decide whether it's safe to dereference the pointer or not.
/// to decide whether it's safe to dereference the pointer or not.
#[ inline ]
#[ inline ]
pub fn get_ptr ( & self , key : & K ) -> Option < * const V > {
pub fn get_ptr ( & self , key : & K ) -> Option < * const V > {
get_ptr ( self . def . get ( ) , key )
get_ptr ( self . def . get ( ) as * mut _ , key )
}
}
/// Retrieve the value associate with `key` from the map.
/// Retrieve the value associate with `key` from the map.
@ -71,53 +129,105 @@ impl<K, V> HashMap<K, V> {
/// pointer or not.
/// pointer or not.
#[ inline ]
#[ inline ]
pub fn get_ptr_mut ( & self , key : & K ) -> Option < * mut V > {
pub fn get_ptr_mut ( & self , key : & K ) -> Option < * mut V > {
get_ptr_mut ( self . def . get ( ) , key )
get_ptr_mut ( self . def . get ( ) as * mut _ , key )
}
}
#[ inline ]
#[ inline ]
pub fn insert ( & self , key : & K , value : & V , flags : u64 ) -> Result < ( ) , c_long > {
pub fn insert ( & self , key : & K , value : & V , flags : u64 ) -> Result < ( ) , c_long > {
insert ( self . def . get ( ) , key , value , flags )
insert ( self . def . get ( ) as * mut _ , key , value , flags )
}
}
#[ inline ]
#[ inline ]
pub fn remove ( & self , key : & K ) -> Result < ( ) , c_long > {
pub fn remove ( & self , key : & K ) -> Result < ( ) , c_long > {
remove ( self . def . get ( ) , key )
remove ( self . def . get ( ) as * mut _ , key )
}
}
}
}
#[ cfg(feature = " btf-maps " ) ]
#[ repr(transparent) ]
pub struct LruHashMap < K , V , const MAX_ENTRIES : usize , const FLAGS : usize > {
def : UnsafeCell < super ::MapDef < K , V , BPF_MAP_TYPE_LRU_HASH , MAX_ENTRIES , FLAGS > > ,
}
#[ cfg(not(feature = " btf-maps " )) ]
#[ repr(transparent) ]
#[ repr(transparent) ]
pub struct LruHashMap < K , V > {
pub struct LruHashMap < K , V > {
def : UnsafeCell < bpf_map_def > ,
def : UnsafeCell < bpf_map_def > ,
_k : PhantomData < K > ,
_k : core::marker :: PhantomData< K > ,
_v : PhantomData < V > ,
_v : core::marker :: PhantomData< V > ,
}
}
#[ cfg(feature = " btf-maps " ) ]
unsafe impl < K : Sync , V : Sync , const MAX_ENTRIES : usize , const FLAGS : usize > Sync
for LruHashMap < K , V , MAX_ENTRIES , FLAGS >
{
}
#[ cfg(not(feature = " btf-maps " )) ]
unsafe impl < K : Sync , V : Sync > Sync for LruHashMap < K , V > { }
unsafe impl < K : Sync , V : Sync > Sync for LruHashMap < K , V > { }
#[ cfg(feature = " btf-maps " ) ]
impl < K , V , const MAX_ENTRIES : usize , const FLAGS : usize > LruHashMap < K , V , MAX_ENTRIES , FLAGS > {
/// Retrieve the value associate with `key` from the map.
/// This function is unsafe. Unless the map flag `BPF_F_NO_PREALLOC` is used, the kernel does not
/// make guarantee on the atomicity of `insert` or `remove`, and any element removed from the
/// map might get aliased by another element in the map, causing garbage to be read, or
/// corruption in case of writes.
#[ inline ]
pub unsafe fn get ( & self , key : & K ) -> Option < & V > {
get ( self . def . get ( ) as * mut _ , key )
}
/// Retrieve the value associate with `key` from the map.
/// The same caveat as `get` applies, but this returns a raw pointer and it's up to the caller
/// to decide whether it's safe to dereference the pointer or not.
#[ inline ]
pub fn get_ptr ( & self , key : & K ) -> Option < * const V > {
get_ptr ( self . def . get ( ) as * mut _ , key )
}
/// Retrieve the value associate with `key` from the map.
/// The same caveat as `get` applies, and additionally cares should be taken to avoid
/// concurrent writes, but it's up to the caller to decide whether it's safe to dereference the
/// pointer or not.
#[ inline ]
pub fn get_ptr_mut ( & self , key : & K ) -> Option < * mut V > {
get_ptr_mut ( self . def . get ( ) as * mut _ , key )
}
#[ inline ]
pub fn insert ( & self , key : & K , value : & V , flags : u64 ) -> Result < ( ) , c_long > {
insert ( self . def . get ( ) as * mut _ , key , value , flags )
}
#[ inline ]
pub fn remove ( & self , key : & K ) -> Result < ( ) , c_long > {
remove ( self . def . get ( ) as * mut _ , key )
}
}
#[ cfg(not(feature = " btf-maps " )) ]
impl < K , V > LruHashMap < K , V > {
impl < K , V > LruHashMap < K , V > {
pub const fn with_max_entries ( max_entries : u32 , flags : u32 ) -> LruHashMap < K , V > {
pub const fn with_max_entries ( max_entries : u32 , flags : u32 ) -> LruHashMap < K , V > {
LruHashMap {
LruHashMap {
def : UnsafeCell ::new ( build_def ::< K , V > (
def : UnsafeCell ::new ( build_def ::< K , V > (
BPF_MAP_TYPE_LRU_HASH ,
BPF_MAP_TYPE_LRU_HASH as u32 ,
max_entries ,
max_entries ,
flags ,
flags ,
PinningType ::None ,
PinningType ::None ,
) ) ,
) ) ,
_k : PhantomData ,
_k : core::marker :: PhantomData,
_v : PhantomData ,
_v : core::marker :: PhantomData,
}
}
}
}
pub const fn pinned ( max_entries : u32 , flags : u32 ) -> LruHashMap < K , V > {
pub const fn pinned ( max_entries : u32 , flags : u32 ) -> LruHashMap < K , V > {
LruHashMap {
LruHashMap {
def : UnsafeCell ::new ( build_def ::< K , V > (
def : UnsafeCell ::new ( build_def ::< K , V > (
BPF_MAP_TYPE_LRU_HASH ,
BPF_MAP_TYPE_LRU_HASH as u32 ,
max_entries ,
max_entries ,
flags ,
flags ,
PinningType ::ByName ,
PinningType ::ByName ,
) ) ,
) ) ,
_k : PhantomData ,
_k : core::marker :: PhantomData,
_v : PhantomData ,
_v : core::marker :: PhantomData,
}
}
}
}
@ -128,7 +238,7 @@ impl<K, V> LruHashMap<K, V> {
/// corruption in case of writes.
/// corruption in case of writes.
#[ inline ]
#[ inline ]
pub unsafe fn get ( & self , key : & K ) -> Option < & V > {
pub unsafe fn get ( & self , key : & K ) -> Option < & V > {
get ( self . def . get ( ) , key )
get ( self . def . get ( ) as * mut _ , key )
}
}
/// Retrieve the value associate with `key` from the map.
/// Retrieve the value associate with `key` from the map.
@ -136,7 +246,7 @@ impl<K, V> LruHashMap<K, V> {
/// to decide whether it's safe to dereference the pointer or not.
/// to decide whether it's safe to dereference the pointer or not.
#[ inline ]
#[ inline ]
pub fn get_ptr ( & self , key : & K ) -> Option < * const V > {
pub fn get_ptr ( & self , key : & K ) -> Option < * const V > {
get_ptr ( self . def . get ( ) , key )
get_ptr ( self . def . get ( ) as * mut _ , key )
}
}
/// Retrieve the value associate with `key` from the map.
/// Retrieve the value associate with `key` from the map.
@ -145,53 +255,105 @@ impl<K, V> LruHashMap<K, V> {
/// pointer or not.
/// pointer or not.
#[ inline ]
#[ inline ]
pub fn get_ptr_mut ( & self , key : & K ) -> Option < * mut V > {
pub fn get_ptr_mut ( & self , key : & K ) -> Option < * mut V > {
get_ptr_mut ( self . def . get ( ) , key )
get_ptr_mut ( self . def . get ( ) as * mut _ , key )
}
}
#[ inline ]
#[ inline ]
pub fn insert ( & self , key : & K , value : & V , flags : u64 ) -> Result < ( ) , c_long > {
pub fn insert ( & self , key : & K , value : & V , flags : u64 ) -> Result < ( ) , c_long > {
insert ( self . def . get ( ) , key , value , flags )
insert ( self . def . get ( ) as * mut _ , key , value , flags )
}
}
#[ inline ]
#[ inline ]
pub fn remove ( & self , key : & K ) -> Result < ( ) , c_long > {
pub fn remove ( & self , key : & K ) -> Result < ( ) , c_long > {
remove ( self . def . get ( ) , key )
remove ( self . def . get ( ) as * mut _ , key )
}
}
}
}
#[ cfg(feature = " btf-maps " ) ]
#[ repr(transparent) ]
pub struct PerCpuHashMap < K , V , const MAX_ENTRIES : usize , const FLAGS : usize > {
def : UnsafeCell < super ::MapDef < K , V , BPF_MAP_TYPE_PERCPU_HASH , MAX_ENTRIES , FLAGS > > ,
}
#[ cfg(not(feature = " btf-maps " )) ]
#[ repr(transparent) ]
#[ repr(transparent) ]
pub struct PerCpuHashMap < K , V > {
pub struct PerCpuHashMap < K , V > {
def : UnsafeCell < bpf_map_def > ,
def : UnsafeCell < bpf_map_def > ,
_k : PhantomData < K > ,
_k : core::marker :: PhantomData< K > ,
_v : PhantomData < V > ,
_v : core::marker :: PhantomData< V > ,
}
}
#[ cfg(feature = " btf-maps " ) ]
unsafe impl < K , V , const MAX_ENTRIES : usize , const FLAGS : usize > Sync
for PerCpuHashMap < K , V , MAX_ENTRIES , FLAGS >
{
}
#[ cfg(not(feature = " btf-maps " )) ]
unsafe impl < K , V > Sync for PerCpuHashMap < K , V > { }
unsafe impl < K , V > Sync for PerCpuHashMap < K , V > { }
#[ cfg(feature = " btf-maps " ) ]
impl < K , V , const MAX_ENTRIES : usize , const FLAGS : usize > PerCpuHashMap < K , V , MAX_ENTRIES , FLAGS > {
/// Retrieve the value associate with `key` from the map.
/// This function is unsafe. Unless the map flag `BPF_F_NO_PREALLOC` is used, the kernel does not
/// make guarantee on the atomicity of `insert` or `remove`, and any element removed from the
/// map might get aliased by another element in the map, causing garbage to be read, or
/// corruption in case of writes.
#[ inline ]
pub unsafe fn get ( & self , key : & K ) -> Option < & V > {
get ( self . def . get ( ) as * mut _ , key )
}
/// Retrieve the value associate with `key` from the map.
/// The same caveat as `get` applies, but this returns a raw pointer and it's up to the caller
/// to decide whether it's safe to dereference the pointer or not.
#[ inline ]
pub fn get_ptr ( & self , key : & K ) -> Option < * const V > {
get_ptr ( self . def . get ( ) as * mut _ , key )
}
/// Retrieve the value associate with `key` from the map.
/// The same caveat as `get` applies, and additionally cares should be taken to avoid
/// concurrent writes, but it's up to the caller to decide whether it's safe to dereference the
/// pointer or not.
#[ inline ]
pub fn get_ptr_mut ( & self , key : & K ) -> Option < * mut V > {
get_ptr_mut ( self . def . get ( ) as * mut _ , key )
}
#[ inline ]
pub fn insert ( & self , key : & K , value : & V , flags : u64 ) -> Result < ( ) , c_long > {
insert ( self . def . get ( ) as * mut _ , key , value , flags )
}
#[ inline ]
pub fn remove ( & self , key : & K ) -> Result < ( ) , c_long > {
remove ( self . def . get ( ) as * mut _ , key )
}
}
#[ cfg(not(feature = " btf-maps " )) ]
impl < K , V > PerCpuHashMap < K , V > {
impl < K , V > PerCpuHashMap < K , V > {
pub const fn with_max_entries ( max_entries : u32 , flags : u32 ) -> PerCpuHashMap < K , V > {
pub const fn with_max_entries ( max_entries : u32 , flags : u32 ) -> PerCpuHashMap < K , V > {
PerCpuHashMap {
PerCpuHashMap {
def : UnsafeCell ::new ( build_def ::< K , V > (
def : UnsafeCell ::new ( build_def ::< K , V > (
BPF_MAP_TYPE_PERCPU_HASH ,
BPF_MAP_TYPE_PERCPU_HASH as u32 ,
max_entries ,
max_entries ,
flags ,
flags ,
PinningType ::None ,
PinningType ::None ,
) ) ,
) ) ,
_k : PhantomData ,
_k : core::marker :: PhantomData,
_v : PhantomData ,
_v : core::marker :: PhantomData,
}
}
}
}
pub const fn pinned ( max_entries : u32 , flags : u32 ) -> PerCpuHashMap < K , V > {
pub const fn pinned ( max_entries : u32 , flags : u32 ) -> PerCpuHashMap < K , V > {
PerCpuHashMap {
PerCpuHashMap {
def : UnsafeCell ::new ( build_def ::< K , V > (
def : UnsafeCell ::new ( build_def ::< K , V > (
BPF_MAP_TYPE_PERCPU_HASH ,
BPF_MAP_TYPE_PERCPU_HASH as u32 ,
max_entries ,
max_entries ,
flags ,
flags ,
PinningType ::ByName ,
PinningType ::ByName ,
) ) ,
) ) ,
_k : PhantomData ,
_k : core::marker :: PhantomData,
_v : PhantomData ,
_v : core::marker :: PhantomData,
}
}
}
}
@ -202,7 +364,7 @@ impl<K, V> PerCpuHashMap<K, V> {
/// corruption in case of writes.
/// corruption in case of writes.
#[ inline ]
#[ inline ]
pub unsafe fn get ( & self , key : & K ) -> Option < & V > {
pub unsafe fn get ( & self , key : & K ) -> Option < & V > {
get ( self . def . get ( ) , key )
get ( self . def . get ( ) as * mut _ , key )
}
}
/// Retrieve the value associate with `key` from the map.
/// Retrieve the value associate with `key` from the map.
@ -210,7 +372,7 @@ impl<K, V> PerCpuHashMap<K, V> {
/// to decide whether it's safe to dereference the pointer or not.
/// to decide whether it's safe to dereference the pointer or not.
#[ inline ]
#[ inline ]
pub fn get_ptr ( & self , key : & K ) -> Option < * const V > {
pub fn get_ptr ( & self , key : & K ) -> Option < * const V > {
get_ptr ( self . def . get ( ) , key )
get_ptr ( self . def . get ( ) as * mut _ , key )
}
}
/// Retrieve the value associate with `key` from the map.
/// Retrieve the value associate with `key` from the map.
@ -219,53 +381,107 @@ impl<K, V> PerCpuHashMap<K, V> {
/// pointer or not.
/// pointer or not.
#[ inline ]
#[ inline ]
pub fn get_ptr_mut ( & self , key : & K ) -> Option < * mut V > {
pub fn get_ptr_mut ( & self , key : & K ) -> Option < * mut V > {
get_ptr_mut ( self . def . get ( ) , key )
get_ptr_mut ( self . def . get ( ) as * mut _ , key )
}
}
#[ inline ]
#[ inline ]
pub fn insert ( & self , key : & K , value : & V , flags : u64 ) -> Result < ( ) , c_long > {
pub fn insert ( & self , key : & K , value : & V , flags : u64 ) -> Result < ( ) , c_long > {
insert ( self . def . get ( ) , key , value , flags )
insert ( self . def . get ( ) as * mut _ , key , value , flags )
}
}
#[ inline ]
#[ inline ]
pub fn remove ( & self , key : & K ) -> Result < ( ) , c_long > {
pub fn remove ( & self , key : & K ) -> Result < ( ) , c_long > {
remove ( self . def . get ( ) , key )
remove ( self . def . get ( ) as * mut _ , key )
}
}
}
}
#[ cfg(feature = " btf-maps " ) ]
#[ repr(transparent) ]
pub struct LruPerCpuHashMap < K , V , const MAX_ENTRIES : usize , const FLAGS : usize > {
def : UnsafeCell < super ::MapDef < K , V , BPF_MAP_TYPE_LRU_PERCPU_HASH , MAX_ENTRIES , FLAGS > > ,
}
#[ cfg(not(feature = " btf-maps " )) ]
#[ repr(transparent) ]
#[ repr(transparent) ]
pub struct LruPerCpuHashMap < K , V > {
pub struct LruPerCpuHashMap < K , V > {
def : UnsafeCell < bpf_map_def > ,
def : UnsafeCell < bpf_map_def > ,
_k : PhantomData < K > ,
_k : core::marker :: PhantomData< K > ,
_v : PhantomData < V > ,
_v : core::marker :: PhantomData< V > ,
}
}
#[ cfg(feature = " btf-maps " ) ]
unsafe impl < K : Sync , V : Sync , const MAX_ENTRIES : usize , const FLAGS : usize > Sync
for LruPerCpuHashMap < K , V , MAX_ENTRIES , FLAGS >
{
}
#[ cfg(not(feature = " btf-maps " )) ]
unsafe impl < K , V > Sync for LruPerCpuHashMap < K , V > { }
unsafe impl < K , V > Sync for LruPerCpuHashMap < K , V > { }
#[ cfg(feature = " btf-maps " ) ]
impl < K , V , const MAX_ENTRIES : usize , const FLAGS : usize >
LruPerCpuHashMap < K , V , MAX_ENTRIES , FLAGS >
{
/// Retrieve the value associate with `key` from the map.
/// This function is unsafe. Unless the map flag `BPF_F_NO_PREALLOC` is used, the kernel does not
/// make guarantee on the atomicity of `insert` or `remove`, and any element removed from the
/// map might get aliased by another element in the map, causing garbage to be read, or
/// corruption in case of writes.
#[ inline ]
pub unsafe fn get ( & self , key : & K ) -> Option < & V > {
get ( self . def . get ( ) as * mut _ , key )
}
/// Retrieve the value associate with `key` from the map.
/// The same caveat as `get` applies, but this returns a raw pointer and it's up to the caller
/// to decide whether it's safe to dereference the pointer or not.
#[ inline ]
pub fn get_ptr ( & self , key : & K ) -> Option < * const V > {
get_ptr ( self . def . get ( ) as * mut _ , key )
}
/// Retrieve the value associate with `key` from the map.
/// The same caveat as `get` applies, and additionally cares should be taken to avoid
/// concurrent writes, but it's up to the caller to decide whether it's safe to dereference the
/// pointer or not.
#[ inline ]
pub fn get_ptr_mut ( & self , key : & K ) -> Option < * mut V > {
get_ptr_mut ( self . def . get ( ) as * mut _ , key )
}
#[ inline ]
pub fn insert ( & self , key : & K , value : & V , flags : u64 ) -> Result < ( ) , c_long > {
insert ( self . def . get ( ) as * mut _ , key , value , flags )
}
#[ inline ]
pub fn remove ( & self , key : & K ) -> Result < ( ) , c_long > {
remove ( self . def . get ( ) as * mut _ , key )
}
}
#[ cfg(not(feature = " btf-maps " )) ]
impl < K , V > LruPerCpuHashMap < K , V > {
impl < K , V > LruPerCpuHashMap < K , V > {
pub const fn with_max_entries ( max_entries : u32 , flags : u32 ) -> LruPerCpuHashMap < K , V > {
pub const fn with_max_entries ( max_entries : u32 , flags : u32 ) -> LruPerCpuHashMap < K , V > {
LruPerCpuHashMap {
LruPerCpuHashMap {
def : UnsafeCell ::new ( build_def ::< K , V > (
def : UnsafeCell ::new ( build_def ::< K , V > (
BPF_MAP_TYPE_LRU_PERCPU_HASH ,
BPF_MAP_TYPE_LRU_PERCPU_HASH as u32 ,
max_entries ,
max_entries ,
flags ,
flags ,
PinningType ::None ,
PinningType ::None ,
) ) ,
) ) ,
_k : PhantomData ,
_k : core::marker :: PhantomData,
_v : PhantomData ,
_v : core::marker :: PhantomData,
}
}
}
}
pub const fn pinned ( max_entries : u32 , flags : u32 ) -> LruPerCpuHashMap < K , V > {
pub const fn pinned ( max_entries : u32 , flags : u32 ) -> LruPerCpuHashMap < K , V > {
LruPerCpuHashMap {
LruPerCpuHashMap {
def : UnsafeCell ::new ( build_def ::< K , V > (
def : UnsafeCell ::new ( build_def ::< K , V > (
BPF_MAP_TYPE_LRU_PERCPU_HASH ,
BPF_MAP_TYPE_LRU_PERCPU_HASH as u32 ,
max_entries ,
max_entries ,
flags ,
flags ,
PinningType ::ByName ,
PinningType ::ByName ,
) ) ,
) ) ,
_k : PhantomData ,
_k : core::marker :: PhantomData,
_v : PhantomData ,
_v : core::marker :: PhantomData,
}
}
}
}
@ -276,7 +492,7 @@ impl<K, V> LruPerCpuHashMap<K, V> {
/// corruption in case of writes.
/// corruption in case of writes.
#[ inline ]
#[ inline ]
pub unsafe fn get ( & self , key : & K ) -> Option < & V > {
pub unsafe fn get ( & self , key : & K ) -> Option < & V > {
get ( self . def . get ( ) , key )
get ( self . def . get ( ) as * mut _ , key )
}
}
/// Retrieve the value associate with `key` from the map.
/// Retrieve the value associate with `key` from the map.
@ -284,7 +500,7 @@ impl<K, V> LruPerCpuHashMap<K, V> {
/// to decide whether it's safe to dereference the pointer or not.
/// to decide whether it's safe to dereference the pointer or not.
#[ inline ]
#[ inline ]
pub fn get_ptr ( & self , key : & K ) -> Option < * const V > {
pub fn get_ptr ( & self , key : & K ) -> Option < * const V > {
get_ptr ( self . def . get ( ) , key )
get_ptr ( self . def . get ( ) as * mut _ , key )
}
}
/// Retrieve the value associate with `key` from the map.
/// Retrieve the value associate with `key` from the map.
@ -293,25 +509,26 @@ impl<K, V> LruPerCpuHashMap<K, V> {
/// pointer or not.
/// pointer or not.
#[ inline ]
#[ inline ]
pub fn get_ptr_mut ( & self , key : & K ) -> Option < * mut V > {
pub fn get_ptr_mut ( & self , key : & K ) -> Option < * mut V > {
get_ptr_mut ( self . def . get ( ) , key )
get_ptr_mut ( self . def . get ( ) as * mut _ , key )
}
}
#[ inline ]
#[ inline ]
pub fn insert ( & self , key : & K , value : & V , flags : u64 ) -> Result < ( ) , c_long > {
pub fn insert ( & self , key : & K , value : & V , flags : u64 ) -> Result < ( ) , c_long > {
insert ( self . def . get ( ) , key , value , flags )
insert ( self . def . get ( ) as * mut _ , key , value , flags )
}
}
#[ inline ]
#[ inline ]
pub fn remove ( & self , key : & K ) -> Result < ( ) , c_long > {
pub fn remove ( & self , key : & K ) -> Result < ( ) , c_long > {
remove ( self . def . get ( ) , key )
remove ( self . def . get ( ) as * mut _ , key )
}
}
}
}
#[ cfg(not(feature = " btf-maps " )) ]
const fn build_def < K , V > ( ty : u32 , max_entries : u32 , flags : u32 , pin : PinningType ) -> bpf_map_def {
const fn build_def < K , V > ( ty : u32 , max_entries : u32 , flags : u32 , pin : PinningType ) -> bpf_map_def {
bpf_map_def {
bpf_map_def {
type_ : ty ,
type_ : ty ,
key_size : mem::size_of ::< K > ( ) as u32 ,
key_size : core:: mem::size_of ::< K > ( ) as u32 ,
value_size : mem::size_of ::< V > ( ) as u32 ,
value_size : core:: mem::size_of ::< V > ( ) as u32 ,
max_entries ,
max_entries ,
map_flags : flags ,
map_flags : flags ,
id : 0 ,
id : 0 ,
@ -320,7 +537,7 @@ const fn build_def<K, V>(ty: u32, max_entries: u32, flags: u32, pin: PinningType
}
}
#[ inline ]
#[ inline ]
fn get_ptr_mut < K , V > ( def : * mut bpf_map_def , key : & K ) -> Option < * mut V > {
fn get_ptr_mut < K , V > ( def : * mut c_void , key : & K ) -> Option < * mut V > {
unsafe {
unsafe {
let value = bpf_map_lookup_elem ( def as * mut _ , key as * const _ as * const c_void ) ;
let value = bpf_map_lookup_elem ( def as * mut _ , key as * const _ as * const c_void ) ;
// FIXME: alignment
// FIXME: alignment
@ -329,20 +546,20 @@ fn get_ptr_mut<K, V>(def: *mut bpf_map_def, key: &K) -> Option<*mut V> {
}
}
#[ inline ]
#[ inline ]
fn get_ptr < K , V > ( def : * mut bpf_map_def , key : & K ) -> Option < * const V > {
fn get_ptr < K , V > ( def : * mut c_void , key : & K ) -> Option < * const V > {
get_ptr_mut ( def , key ) . map ( | p | p as * const V )
get_ptr_mut ( def , key ) . map ( | p | p as * const V )
}
}
#[ inline ]
#[ inline ]
unsafe fn get < ' a , K , V > ( def : * mut bpf_map_def , key : & K ) -> Option < & ' a V > {
unsafe fn get < ' a , K , V > ( def : * mut c_void , key : & K ) -> Option < & ' a V > {
get_ptr ( def , key ) . map ( | p | & * p )
get_ptr ( def , key ) . map ( | p | & * p )
}
}
#[ 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 c_void , key : & K , value : & V , flags : u64 ) -> Result < ( ) , c_long > {
let ret = unsafe {
let ret = unsafe {
bpf_map_update_elem (
bpf_map_update_elem (
def as * mut _ ,
def ,
key as * const _ as * const _ ,
key as * const _ as * const _ ,
value as * const _ as * const _ ,
value as * const _ as * const _ ,
flags ,
flags ,
@ -352,7 +569,7 @@ fn insert<K, V>(def: *mut bpf_map_def, key: &K, value: &V, flags: u64) -> Result
}
}
#[ inline ]
#[ inline ]
fn remove < K > ( def : * mut bpf_map_def , key : & K ) -> Result < ( ) , c_long > {
fn remove < K > ( def : * mut c_void , key : & K ) -> Result < ( ) , c_long > {
let ret = unsafe { bpf_map_delete_elem ( def as * mut _ , key as * const _ as * const c_void ) } ;
let ret = unsafe { bpf_map_delete_elem ( def as * mut _ , key as * const _ as * const c_void ) } ;
( ret = = 0 ) . then_some ( ( ) ) . ok_or ( ret )
( ret = = 0 ) . then_some ( ( ) ) . ok_or ( ret )
}
}