use core::{num::NonZeroU32, ptr::NonNull};
use aya_ebpf_bindings::bindings::bpf_devmap_val;
use super::try_redirect_map;
use crate::{
bindings::bpf_map_type::BPF_MAP_TYPE_DEVMAP,
lookup,
maps::{MapDef, PinningType},
};
#[repr(transparent)]
pub struct DevMap {
def: MapDef,
}
impl super::super::private::Map for DevMap {
type Key = u32;
type Value = bpf_devmap_val;
}
impl DevMap {
map_constructors!(
u32,
bpf_devmap_val,
BPF_MAP_TYPE_DEVMAP,
with_docs {
},
pinned_docs {
},
);
#[inline(always)]
pub fn get(&self, index: u32) -> Option<DevMapValue> {
let value = lookup(self.def.as_ptr(), &index)?;
let value: &bpf_devmap_val = unsafe { value.as_ref() };
Some(DevMapValue {
if_index: value.ifindex,
prog_id: NonZeroU32::new(unsafe { value.bpf_prog.id }),
})
}
#[inline(always)]
pub fn get_ifindex(&self, index: u32) -> Option<u32> {
let value: NonNull<u32> = lookup(self.def.as_ptr(), &index)?;
Some(unsafe { *value.as_ptr() })
}
#[inline(always)]
pub fn redirect(&self, index: u32, flags: u64) -> Result<u32, u32> {
try_redirect_map(self.def.as_ptr(), index, flags)
}
}
#[derive(Clone, Copy)]
pub struct DevMapValue {
pub if_index: u32,
pub prog_id: Option<NonZeroU32>,
}