-
Notifications
You must be signed in to change notification settings - Fork 0
/
bpf.rs
56 lines (49 loc) · 1.32 KB
/
bpf.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
use std::{
ffi::CStr,
os::raw::{c_int, c_void},
};
use libbpf_sys::{bpf_obj_get, BPF_ANY};
const BPF_MAP_NAME: &CStr = c"/sys/fs/bpf/tc/globals/map_keymash";
#[derive(Debug)]
pub struct BpfHandle {
map_fd: c_int,
}
#[derive(Debug, Clone, Copy)]
pub enum BpfError {
LoadMap(c_int),
MapWrite(c_int),
}
/// Opens the eBPF map.
pub unsafe fn init() -> Result<BpfHandle, BpfError> {
let res = bpf_obj_get(BPF_MAP_NAME.as_ptr());
if res < 0 {
log::error!("Failed to load BPF map {BPF_MAP_NAME:?}: {}", res);
return Err(BpfError::LoadMap(res));
}
Ok(BpfHandle { map_fd: res })
}
impl BpfHandle {
/// Write a key-value pair to the eBPF map.
pub fn write_to_map(&self, key: u32, value: u32) -> Result<(), BpfError> {
unsafe {
let res = libbpf_sys::bpf_map_update_elem(
self.map_fd,
&key as *const u32 as *const c_void,
&value as *const u32 as *const c_void,
BPF_ANY.into(),
);
if res != 0 {
log::error!("Failed to write to BPF map: {}", res);
return Err(BpfError::MapWrite(res));
}
}
Ok(())
}
}
impl Drop for BpfHandle {
fn drop(&mut self) {
unsafe {
libc::close(self.map_fd);
}
}
}