Skip to content

Commit

Permalink
bpf/devmap: don't expose bpf_devmap_value
Browse files Browse the repository at this point in the history
Use our own type that:
- is stable as not from bindgen
- does not have an union inside
  • Loading branch information
Tuetuopay committed Sep 22, 2023
1 parent bf33823 commit 35d84a6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
15 changes: 13 additions & 2 deletions bpf/aya-bpf/src/maps/xdp/dev_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
}
}

Expand Down Expand Up @@ -142,3 +147,9 @@ impl DevMap {
}
}
}

#[derive(Clone, Copy)]
pub struct DevMapValue {
pub ifindex: u32,
pub prog_id: u32,
}
11 changes: 9 additions & 2 deletions bpf/aya-bpf/src/maps/xdp/dev_map_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
})
}
}

Expand Down

0 comments on commit 35d84a6

Please sign in to comment.