bpf/devmap: don't expose `bpf_devmap_value`

Use our own type that:
- is stable as not from bindgen
- does not have an union inside
reviewable/pr527/r18
Tuetuopay 1 year ago
parent c6754c614e
commit 63ce2f013a

@ -104,13 +104,18 @@ impl DevMap {
/// // redirect to ifindex
/// ```
#[inline(always)]
pub fn get(&self, index: u32) -> Option<bpf_devmap_val> {
pub fn get(&self, index: u32) -> Option<DevMapValue> {
unsafe {
let value = bpf_map_lookup_elem(
self.def.get() as *mut _,
&index as *const _ as *const c_void,
);
NonNull::new(value as *mut bpf_devmap_val).map(|p| *p.as_ref())
NonNull::new(value as *mut bpf_devmap_val).map(|p| DevMapValue {
ifindex: p.as_ref().ifindex,
// SAFETY: map writes use fd, map reads use id.
// https://elixir.bootlin.com/linux/v6.2/source/include/uapi/linux/bpf.h#L6136
prog_id: p.as_ref().bpf_prog.id,
})
}
}
@ -142,3 +147,9 @@ impl DevMap {
}
}
}
#[derive(Clone, Copy)]
pub struct DevMapValue {
pub ifindex: u32,
pub prog_id: u32,
}

@ -9,6 +9,8 @@ use crate::{
maps::PinningType,
};
use super::dev_map::DevMapValue;
/// A map of network devices.
///
/// XDP programs can use this map to redirect packets to other network devices. It is similar to
@ -106,11 +108,16 @@ impl DevMapHash {
/// // redirect to ifindex
/// ```
#[inline(always)]
pub fn get(&self, key: u32) -> Option<bpf_devmap_val> {
pub fn get(&self, key: u32) -> Option<DevMapValue> {
unsafe {
let value =
bpf_map_lookup_elem(self.def.get() as *mut _, &key as *const _ as *const c_void);
NonNull::new(value as *mut bpf_devmap_val).map(|p| *p.as_ref())
NonNull::new(value as *mut bpf_devmap_val).map(|p| DevMapValue {
ifindex: p.as_ref().ifindex,
// SAFETY: map writes use fd, map reads use id.
// https://elixir.bootlin.com/linux/v6.2/source/include/uapi/linux/bpf.h#L6136
prog_id: p.as_ref().bpf_prog.id,
})
}
}

Loading…
Cancel
Save