use core::mem;
use cty::c_void;
use crate::xdp::{
bpf_map_def, bpf_map_type_BPF_MAP_TYPE_DEVMAP, XdpAction,
prelude::bpf_redirect_map,
};
#[repr(transparent)]
pub struct DevMap {
def: bpf_map_def,
}
impl DevMap {
pub const fn with_max_entries(max_entries: u32) -> Self {
Self {
def: bpf_map_def {
type_: bpf_map_type_BPF_MAP_TYPE_DEVMAP,
key_size: mem::size_of::<u32>() as u32,
value_size: mem::size_of::<u32>() as u32,
max_entries,
map_flags: 0,
},
}
}
#[inline]
pub fn redirect(&mut self, key: u32) -> Result<(), ()> {
let res = bpf_redirect_map(
&mut self.def as *mut _ as *mut c_void,
key, XdpAction::Aborted as u64
);
if res == XdpAction::Redirect as i64 {
Ok(())
} else {
Err(())
}
}
}