Struct oxidebpf::BpfHashMap
source · [−]pub struct BpfHashMap {
pub base: Map,
}
Fields
base: Map
Implementations
pub unsafe fn new(
map_name: &str,
key_size: u32,
value_size: u32,
max_entries: u32
) -> Result<BpfHashMap, OxidebpfError>
pub unsafe fn new(
map_name: &str,
key_size: u32,
value_size: u32,
max_entries: u32
) -> Result<BpfHashMap, OxidebpfError>
Create a new BpfHashMap
Calling new will create a new BPF_MAP_TYPE_HASH map. It stores some meta data to track it. The array map supports read and write operations to access the members of the map
Safety
The value_size
and key_size
you pass in needs to match exactly with the size of the struct/type
used by any other BPF program that might be using this map. Any T
or U
you use in subsequent
read()
and write()
calls needs to match exactly (e.g., with #[repr(C)]
) with the struct/type
used by the BPF program as well. Additionally, std::mem::size_of::<T>()
must match the given
value_size
here exactly and std::mem::size_of::<U>() for the key
. If this conditions are not met,
the BpfHashMap
behavior is undefined.
Examples
use oxidebpf::BpfHashMap;
let map: BpfHashMap = unsafe {BpfHashMap::new(
"mymap",
std::mem::size_of::<u64>() as u32,
std::mem::size_of::<u64>() as u32,
1024,
).expect("Failed to create map") };
Trait Implementations
Reads an index from a map of type BPF_MAP_TYPE_HASH
Initiates a read from key
. Read verifies that the map has been initialized.
The value returned will be of the same type that was used when the BpfHashMap
was created
NOTE: This method calls will read a certain amount of memory based on what the
size of T
and U
is. Make sure that T
and U
matches the type of the value and key
(e.g., with #[repr(C)]
) that is being used in the map.
Example
use oxidebpf::{BpfHashMap, RWMap};
// this is safe because we are reading and writing a u64, and the value_size we
// pass into new() is a u64
unsafe {
let map: BpfHashMap = BpfHashMap::new(
"mymap",
std::mem::size_of::<u64>() as u32,
std::mem::size_of::<u64>() as u32,
1024,
).expect("Failed to create map");
let _ = map.write(87654321u64, 12345u64);
assert_eq!(
12345u64,
unsafe { map.read(87654321u64).expect("Failed to read value from map") }
);
}
Writes an index to and index of a map of type BPF_MAP_TYPE_ARRAY
Initiates a write to key
of value
. The value needs to match the array
type that was used when the map was created
NOTE: This method calls will write a certain amount of memory based on what the
size of T
is. Make sure that T
matches the type of the value (e.g., with #[repr(C)]
)
that is being used in the map.
Example
use oxidebpf::{BpfHashMap, RWMap};
use std::process;
// this is safe because we are reading and writing a u64, and the value_size we
// pass into new() is a u64
unsafe {
let map: BpfHashMap = BpfHashMap::new(
"mymap",
std::mem::size_of::<u32>() as u32,
std::mem::size_of::<u64>() as u32,
1024,
).expect("Failed to create map");
let _ = map.write(process::id(), 12345u64);
assert_eq!(
12345u64,
map.read(process::id()).expect("Failed to read value from map")
);
}
Auto Trait Implementations
impl RefUnwindSafe for BpfHashMap
impl Send for BpfHashMap
impl Sync for BpfHashMap
impl Unpin for BpfHashMap
impl UnwindSafe for BpfHashMap
Blanket Implementations
Mutably borrows from an owned value. Read more