mirror of https://github.com/aya-rs/aya
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
112 lines
3.1 KiB
Rust
112 lines
3.1 KiB
Rust
//! An array of available CPUs.
|
|
|
|
use std::borrow::{Borrow, BorrowMut};
|
|
|
|
use crate::{
|
|
maps::{check_bounds, check_kv_size, IterableMap, MapData, MapError},
|
|
sys::{bpf_map_lookup_elem, bpf_map_update_elem, SyscallError},
|
|
};
|
|
|
|
/// An array of available CPUs.
|
|
///
|
|
/// XDP programs can use this map to redirect packets to a target
|
|
/// CPU for processing.
|
|
///
|
|
/// # Minimum kernel version
|
|
///
|
|
/// The minimum kernel version required to use this feature is 4.15.
|
|
///
|
|
/// # Examples
|
|
/// ```no_run
|
|
/// # let elf_bytes = &[];
|
|
/// use aya::maps::xdp::CpuMap;
|
|
///
|
|
/// let mut bpf = aya::BpfLoader::new()
|
|
/// .set_max_entries("CPUS", aya::util::nr_cpus().unwrap() as u32)
|
|
/// .load(elf_bytes)
|
|
/// .unwrap();
|
|
/// let mut cpumap = CpuMap::try_from(bpf.map_mut("CPUS").unwrap())?;
|
|
/// let flags = 0;
|
|
/// let queue_size = 2048;
|
|
/// for i in 0u32..8u32 {
|
|
/// cpumap.set(i, queue_size, flags);
|
|
/// }
|
|
///
|
|
/// # Ok::<(), aya::BpfError>(())
|
|
/// ```
|
|
#[doc(alias = "BPF_MAP_TYPE_CPUMAP")]
|
|
pub struct CpuMap<T> {
|
|
inner: T,
|
|
}
|
|
|
|
impl<T: Borrow<MapData>> CpuMap<T> {
|
|
pub(crate) fn new(map: T) -> Result<Self, MapError> {
|
|
let data = map.borrow();
|
|
check_kv_size::<u32, u32>(data)?;
|
|
|
|
Ok(Self { inner: map })
|
|
}
|
|
|
|
/// Returns the number of elements in the array.
|
|
///
|
|
/// This corresponds to the value of `bpf_map_def::max_entries` on the eBPF side.
|
|
pub fn len(&self) -> u32 {
|
|
self.inner.borrow().obj.max_entries()
|
|
}
|
|
|
|
/// Returns the value stored at the given index.
|
|
///
|
|
/// # Errors
|
|
///
|
|
/// 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> {
|
|
let data = self.inner.borrow();
|
|
check_bounds(data, index)?;
|
|
let fd = data.fd;
|
|
|
|
let value =
|
|
bpf_map_lookup_elem(fd, &index, flags).map_err(|(_, io_error)| SyscallError {
|
|
call: "bpf_map_lookup_elem",
|
|
io_error,
|
|
})?;
|
|
value.ok_or(MapError::KeyNotFound)
|
|
}
|
|
|
|
/// An iterator over the elements of the map.
|
|
pub fn iter(&self) -> impl Iterator<Item = Result<u32, MapError>> + '_ {
|
|
(0..self.len()).map(move |i| self.get(i, 0))
|
|
}
|
|
}
|
|
|
|
impl<T: BorrowMut<MapData>> CpuMap<T> {
|
|
/// Sets the value of the element at the given index.
|
|
///
|
|
/// # Errors
|
|
///
|
|
/// 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> {
|
|
let data = self.inner.borrow_mut();
|
|
check_bounds(data, index)?;
|
|
let fd = data.fd;
|
|
bpf_map_update_elem(fd, Some(&index), &value, flags).map_err(|(_, io_error)| {
|
|
SyscallError {
|
|
call: "bpf_map_update_elem",
|
|
io_error,
|
|
}
|
|
})?;
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
impl<T: Borrow<MapData>> IterableMap<u32, u32> for CpuMap<T> {
|
|
fn map(&self) -> &MapData {
|
|
self.inner.borrow()
|
|
}
|
|
|
|
fn get(&self, key: &u32) -> Result<u32, MapError> {
|
|
self.get(*key, 0)
|
|
}
|
|
}
|