|
@@ -1,15 +1,10 @@
|
|
//! An array of available CPUs.
|
|
//! An array of available CPUs.
|
|
|
|
|
|
-use std::{
|
|
|
|
- convert::TryFrom,
|
|
|
|
- mem,
|
|
|
|
- ops::{Deref, DerefMut},
|
|
|
|
-};
|
|
|
|
|
|
+use std::borrow::{Borrow, BorrowMut};
|
|
|
|
|
|
use crate::{
|
|
use crate::{
|
|
- generated::bpf_map_type::BPF_MAP_TYPE_CPUMAP,
|
|
|
|
- maps::{Map, MapError, MapRef, MapRefMut},
|
|
|
|
- sys::bpf_map_update_elem,
|
|
|
|
|
|
+ maps::{check_bounds, check_kv_size, IterableMap, MapData, MapError},
|
|
|
|
+ sys::{bpf_map_lookup_elem, bpf_map_update_elem, SyscallError},
|
|
};
|
|
};
|
|
|
|
|
|
/// An array of available CPUs.
|
|
/// An array of available CPUs.
|
|
@@ -19,15 +14,18 @@ use crate::{
|
|
///
|
|
///
|
|
/// # Minimum kernel version
|
|
/// # Minimum kernel version
|
|
///
|
|
///
|
|
-/// The minimum kernel version required to use this feature is 4.2.
|
|
|
|
|
|
+/// The minimum kernel version required to use this feature is 4.15.
|
|
///
|
|
///
|
|
/// # Examples
|
|
/// # Examples
|
|
/// ```no_run
|
|
/// ```no_run
|
|
-/// # let bpf = aya::Bpf::load(&[])?;
|
|
|
|
|
|
+/// # let elf_bytes = &[];
|
|
/// use aya::maps::xdp::CpuMap;
|
|
/// use aya::maps::xdp::CpuMap;
|
|
-/// use std::convert::{TryFrom, TryInto};
|
|
|
|
///
|
|
///
|
|
-/// let mut cpumap = CpuMap::try_from(bpf.map_mut("CPUS")?)?;
|
|
|
|
|
|
+/// 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 flags = 0;
|
|
/// let queue_size = 2048;
|
|
/// let queue_size = 2048;
|
|
/// for i in 0u32..8u32 {
|
|
/// for i in 0u32..8u32 {
|
|
@@ -37,52 +35,51 @@ use crate::{
|
|
/// # Ok::<(), aya::BpfError>(())
|
|
/// # Ok::<(), aya::BpfError>(())
|
|
/// ```
|
|
/// ```
|
|
#[doc(alias = "BPF_MAP_TYPE_CPUMAP")]
|
|
#[doc(alias = "BPF_MAP_TYPE_CPUMAP")]
|
|
-pub struct CpuMap<T: Deref<Target = Map>> {
|
|
|
|
|
|
+pub struct CpuMap<T> {
|
|
inner: T,
|
|
inner: T,
|
|
}
|
|
}
|
|
|
|
|
|
-impl<T: Deref<Target = Map>> CpuMap<T> {
|
|
|
|
- fn new(map: T) -> Result<CpuMap<T>, MapError> {
|
|
|
|
- let map_type = map.obj.def.map_type;
|
|
|
|
- if map_type != BPF_MAP_TYPE_CPUMAP as u32 {
|
|
|
|
- return Err(MapError::InvalidMapType {
|
|
|
|
- map_type: map_type as u32,
|
|
|
|
- });
|
|
|
|
- }
|
|
|
|
- let expected = mem::size_of::<u32>();
|
|
|
|
- let size = map.obj.def.key_size as usize;
|
|
|
|
- if size != expected {
|
|
|
|
- return Err(MapError::InvalidKeySize { size, expected });
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- let expected = mem::size_of::<u32>();
|
|
|
|
- let size = map.obj.def.value_size as usize;
|
|
|
|
- if size != expected {
|
|
|
|
- return Err(MapError::InvalidValueSize { size, expected });
|
|
|
|
- }
|
|
|
|
- let _fd = map.fd_or_err()?;
|
|
|
|
|
|
+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(CpuMap { inner: map })
|
|
|
|
|
|
+ Ok(Self { inner: map })
|
|
}
|
|
}
|
|
|
|
|
|
/// Returns the number of elements in the array.
|
|
/// Returns the number of elements in the array.
|
|
///
|
|
///
|
|
/// This corresponds to the value of `bpf_map_def::max_entries` on the eBPF side.
|
|
/// This corresponds to the value of `bpf_map_def::max_entries` on the eBPF side.
|
|
pub fn len(&self) -> u32 {
|
|
pub fn len(&self) -> u32 {
|
|
- self.inner.obj.def.max_entries
|
|
|
|
|
|
+ self.inner.borrow().obj.max_entries()
|
|
}
|
|
}
|
|
|
|
|
|
- fn check_bounds(&self, index: u32) -> Result<(), MapError> {
|
|
|
|
- let max_entries = self.inner.obj.def.max_entries;
|
|
|
|
- if index >= self.inner.obj.def.max_entries {
|
|
|
|
- Err(MapError::OutOfBounds { index, max_entries })
|
|
|
|
- } else {
|
|
|
|
- Ok(())
|
|
|
|
- }
|
|
|
|
|
|
+ /// 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().as_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: Deref<Target = Map> + DerefMut<Target = Map>> CpuMap<T> {
|
|
|
|
|
|
+impl<T: BorrowMut<MapData>> CpuMap<T> {
|
|
/// Sets the value of the element at the given index.
|
|
/// Sets the value of the element at the given index.
|
|
///
|
|
///
|
|
/// # Errors
|
|
/// # Errors
|
|
@@ -90,12 +87,12 @@ impl<T: Deref<Target = Map> + DerefMut<Target = Map>> CpuMap<T> {
|
|
/// Returns [`MapError::OutOfBounds`] if `index` is out of bounds, [`MapError::SyscallError`]
|
|
/// Returns [`MapError::OutOfBounds`] if `index` is out of bounds, [`MapError::SyscallError`]
|
|
/// if `bpf_map_update_elem` fails.
|
|
/// 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, flags: u64) -> Result<(), MapError> {
|
|
- let fd = self.inner.fd_or_err()?;
|
|
|
|
- self.check_bounds(index)?;
|
|
|
|
- bpf_map_update_elem(fd, &index, &value, flags).map_err(|(code, io_error)| {
|
|
|
|
- MapError::SyscallError {
|
|
|
|
- call: "bpf_map_update_elem".to_owned(),
|
|
|
|
- code,
|
|
|
|
|
|
+ let data = self.inner.borrow_mut();
|
|
|
|
+ check_bounds(data, index)?;
|
|
|
|
+ let fd = data.fd().as_fd();
|
|
|
|
+ bpf_map_update_elem(fd, Some(&index), &value, flags).map_err(|(_, io_error)| {
|
|
|
|
+ SyscallError {
|
|
|
|
+ call: "bpf_map_update_elem",
|
|
io_error,
|
|
io_error,
|
|
}
|
|
}
|
|
})?;
|
|
})?;
|
|
@@ -103,18 +100,12 @@ impl<T: Deref<Target = Map> + DerefMut<Target = Map>> CpuMap<T> {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-impl TryFrom<MapRef> for CpuMap<MapRef> {
|
|
|
|
- type Error = MapError;
|
|
|
|
-
|
|
|
|
- fn try_from(a: MapRef) -> Result<CpuMap<MapRef>, MapError> {
|
|
|
|
- CpuMap::new(a)
|
|
|
|
|
|
+impl<T: Borrow<MapData>> IterableMap<u32, u32> for CpuMap<T> {
|
|
|
|
+ fn map(&self) -> &MapData {
|
|
|
|
+ self.inner.borrow()
|
|
}
|
|
}
|
|
-}
|
|
|
|
-
|
|
|
|
-impl TryFrom<MapRefMut> for CpuMap<MapRefMut> {
|
|
|
|
- type Error = MapError;
|
|
|
|
|
|
|
|
- fn try_from(a: MapRefMut) -> Result<CpuMap<MapRefMut>, MapError> {
|
|
|
|
- CpuMap::new(a)
|
|
|
|
|
|
+ fn get(&self, key: &u32) -> Result<u32, MapError> {
|
|
|
|
+ self.get(*key, 0)
|
|
}
|
|
}
|
|
}
|
|
}
|