|
|
|
@ -1,10 +1,17 @@
|
|
|
|
|
//! An array of network devices.
|
|
|
|
|
|
|
|
|
|
use std::borrow::{Borrow, BorrowMut};
|
|
|
|
|
use std::{
|
|
|
|
|
borrow::{Borrow, BorrowMut},
|
|
|
|
|
num::NonZeroU32,
|
|
|
|
|
os::fd::AsRawFd,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
use aya_obj::generated::{bpf_devmap_val, bpf_devmap_val__bindgen_ty_1};
|
|
|
|
|
|
|
|
|
|
use crate::{
|
|
|
|
|
maps::{check_bounds, check_kv_size, IterableMap, MapData, MapError},
|
|
|
|
|
sys::{bpf_map_lookup_elem, bpf_map_update_elem, SyscallError},
|
|
|
|
|
Pod,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/// An array of network devices.
|
|
|
|
@ -24,7 +31,7 @@ use crate::{
|
|
|
|
|
/// let mut devmap = DevMap::try_from(bpf.map_mut("IFACES").unwrap())?;
|
|
|
|
|
/// let source = 32u32;
|
|
|
|
|
/// let dest = 42u32;
|
|
|
|
|
/// devmap.set(source, dest, 0);
|
|
|
|
|
/// devmap.set(source, dest, None::<i32>, 0);
|
|
|
|
|
///
|
|
|
|
|
/// # Ok::<(), aya::BpfError>(())
|
|
|
|
|
/// ```
|
|
|
|
@ -36,7 +43,7 @@ pub struct DevMap<T> {
|
|
|
|
|
impl<T: Borrow<MapData>> DevMap<T> {
|
|
|
|
|
pub(crate) fn new(map: T) -> Result<Self, MapError> {
|
|
|
|
|
let data = map.borrow();
|
|
|
|
|
check_kv_size::<u32, u32>(data)?;
|
|
|
|
|
check_kv_size::<u32, bpf_devmap_val>(data)?;
|
|
|
|
|
|
|
|
|
|
Ok(Self { inner: map })
|
|
|
|
|
}
|
|
|
|
@ -54,7 +61,7 @@ impl<T: Borrow<MapData>> DevMap<T> {
|
|
|
|
|
///
|
|
|
|
|
/// Returns [`MapError::OutOfBounds`] if `index` is out of bounds, [`MapError::SyscallError`]
|
|
|
|
|
/// if `bpf_map_lookup_elem` fails.
|
|
|
|
|
pub fn get(&self, index: u32, flags: u64) -> Result<u32, MapError> {
|
|
|
|
|
pub fn get(&self, index: u32, flags: u64) -> Result<DevMapValue, MapError> {
|
|
|
|
|
let data = self.inner.borrow();
|
|
|
|
|
check_bounds(data, index)?;
|
|
|
|
|
let fd = data.fd().as_fd();
|
|
|
|
@ -64,11 +71,18 @@ impl<T: Borrow<MapData>> DevMap<T> {
|
|
|
|
|
call: "bpf_map_lookup_elem",
|
|
|
|
|
io_error,
|
|
|
|
|
})?;
|
|
|
|
|
value.ok_or(MapError::KeyNotFound)
|
|
|
|
|
let value: bpf_devmap_val = value.ok_or(MapError::KeyNotFound)?;
|
|
|
|
|
|
|
|
|
|
// SAFETY: map writes use fd, map reads use id.
|
|
|
|
|
// https://github.com/torvalds/linux/blob/2dde18cd1d8fac735875f2e4987f11817cc0bc2c/include/uapi/linux/bpf.h#L6228
|
|
|
|
|
Ok(DevMapValue {
|
|
|
|
|
ifindex: value.ifindex,
|
|
|
|
|
prog_id: NonZeroU32::new(unsafe { value.bpf_prog.id }),
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// An iterator over the elements of the array.
|
|
|
|
|
pub fn iter(&self) -> impl Iterator<Item = Result<u32, MapError>> + '_ {
|
|
|
|
|
pub fn iter(&self) -> impl Iterator<Item = Result<DevMapValue, MapError>> + '_ {
|
|
|
|
|
(0..self.len()).map(move |i| self.get(i, 0))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -80,10 +94,26 @@ impl<T: BorrowMut<MapData>> DevMap<T> {
|
|
|
|
|
///
|
|
|
|
|
/// Returns [`MapError::OutOfBounds`] if `index` is out of bounds, [`MapError::SyscallError`]
|
|
|
|
|
/// if `bpf_map_update_elem` fails.
|
|
|
|
|
pub fn set(&mut self, index: u32, value: u32, flags: u64) -> Result<(), MapError> {
|
|
|
|
|
pub fn set(
|
|
|
|
|
&mut self,
|
|
|
|
|
index: u32,
|
|
|
|
|
value: u32,
|
|
|
|
|
program: Option<impl AsRawFd>,
|
|
|
|
|
flags: u64,
|
|
|
|
|
) -> Result<(), MapError> {
|
|
|
|
|
let data = self.inner.borrow_mut();
|
|
|
|
|
check_bounds(data, index)?;
|
|
|
|
|
let fd = data.fd().as_fd();
|
|
|
|
|
|
|
|
|
|
let value = bpf_devmap_val {
|
|
|
|
|
ifindex: value,
|
|
|
|
|
bpf_prog: bpf_devmap_val__bindgen_ty_1 {
|
|
|
|
|
// Default is valid as the kernel will only consider fd > 0:
|
|
|
|
|
// https://github.com/torvalds/linux/blob/2dde18cd1d8fac735875f2e4987f11817cc0bc2c/kernel/bpf/devmap.c#L866
|
|
|
|
|
// https://github.com/torvalds/linux/blob/2dde18cd1d8fac735875f2e4987f11817cc0bc2c/kernel/bpf/devmap.c#L918
|
|
|
|
|
fd: program.map(|prog| prog.as_raw_fd()).unwrap_or_default(),
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
bpf_map_update_elem(fd, Some(&index), &value, flags).map_err(|(_, io_error)| {
|
|
|
|
|
SyscallError {
|
|
|
|
|
call: "bpf_map_update_elem",
|
|
|
|
@ -94,12 +124,20 @@ impl<T: BorrowMut<MapData>> DevMap<T> {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T: Borrow<MapData>> IterableMap<u32, u32> for DevMap<T> {
|
|
|
|
|
impl<T: Borrow<MapData>> IterableMap<u32, DevMapValue> for DevMap<T> {
|
|
|
|
|
fn map(&self) -> &MapData {
|
|
|
|
|
self.inner.borrow()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn get(&self, key: &u32) -> Result<u32, MapError> {
|
|
|
|
|
fn get(&self, key: &u32) -> Result<DevMapValue, MapError> {
|
|
|
|
|
self.get(*key, 0)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unsafe impl Pod for bpf_devmap_val {}
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, Debug)]
|
|
|
|
|
pub struct DevMapValue {
|
|
|
|
|
pub ifindex: u32,
|
|
|
|
|
pub prog_id: Option<NonZeroU32>,
|
|
|
|
|
}
|
|
|
|
|