From 35d84a6b140789b5baacce2e953b9097e66ca0ab Mon Sep 17 00:00:00 2001 From: Tuetuopay Date: Sat, 5 Aug 2023 00:16:40 +0200 Subject: [PATCH] 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 --- bpf/aya-bpf/src/maps/xdp/dev_map.rs | 15 +++++++++++++-- bpf/aya-bpf/src/maps/xdp/dev_map_hash.rs | 11 +++++++++-- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/bpf/aya-bpf/src/maps/xdp/dev_map.rs b/bpf/aya-bpf/src/maps/xdp/dev_map.rs index 66103758c..18a767909 100644 --- a/bpf/aya-bpf/src/maps/xdp/dev_map.rs +++ b/bpf/aya-bpf/src/maps/xdp/dev_map.rs @@ -104,13 +104,18 @@ impl DevMap { /// // redirect to ifindex /// ``` #[inline(always)] - pub fn get(&self, index: u32) -> Option { + pub fn get(&self, index: u32) -> Option { 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, +} diff --git a/bpf/aya-bpf/src/maps/xdp/dev_map_hash.rs b/bpf/aya-bpf/src/maps/xdp/dev_map_hash.rs index 600aa9dff..a6533e76c 100644 --- a/bpf/aya-bpf/src/maps/xdp/dev_map_hash.rs +++ b/bpf/aya-bpf/src/maps/xdp/dev_map_hash.rs @@ -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 { + pub fn get(&self, key: u32) -> Option { 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, + }) } }