Skip to main content

PerCpuArray

Struct PerCpuArray 

Source
pub struct PerCpuArray<T, V: Pod> { /* private fields */ }
Expand description

A per-CPU fixed-size array.

The size of the array is defined on the eBPF side using the bpf_map_def::max_entries field. All the entries are zero-initialized when the map is created.

§Minimum kernel version

The minimum kernel version required to use this feature is 4.6.

§Examples

use aya::maps::{PerCpuArray, PerCpuValues};
use aya::util::nr_cpus;

let mut array = PerCpuArray::try_from(bpf.map_mut("ARRAY").unwrap())?;

// set array[1] = 42 for all cpus
let nr_cpus = nr_cpus().map_err(|(_, error)| error)?;
array.set(1, PerCpuValues::try_from(vec![42u32; nr_cpus])?, 0)?;

// retrieve the values at index 1 for all cpus
let values = array.get(&1, 0)?;
assert_eq!(values.len(), nr_cpus);
for cpu_val in values.iter() {
    assert_eq!(*cpu_val, 42u32);
}

Implementations§

Source§

impl<T: Borrow<MapData>, V: Pod> PerCpuArray<T, V>

Source

pub fn len(&self) -> u32

Returns the number of elements in the array.

This corresponds to the value of bpf_map_def::max_entries on the eBPF side.

Source

pub fn get(&self, index: &u32, flags: u64) -> Result<PerCpuValues<V>, MapError>

Returns a slice of values - one for each CPU - stored at the given index.

§Errors

Returns MapError::OutOfBounds if index is out of bounds, MapError::SyscallError if bpf_map_lookup_elem fails.

Source

pub fn iter( &self, ) -> impl Iterator<Item = Result<PerCpuValues<V>, MapError>> + '_

An iterator over the elements of the array. The iterator item type is Result<PerCpuValues<V>, MapError>.

Source§

impl<T: BorrowMut<MapData>, V: Pod> PerCpuArray<T, V>

Source

pub fn set( &mut self, index: u32, values: PerCpuValues<V>, flags: u64, ) -> Result<(), MapError>

Sets the values - one for each CPU - at the given index.

§Errors

Returns MapError::OutOfBounds if index is out of bounds, MapError::SyscallError if bpf_map_update_elem fails.

Source§

impl<T: Borrow<MapData>, V: Pod> PerCpuArray<T, V>

Source

pub fn pin<P: AsRef<Path>>(self, path: P) -> Result<(), PinError>

Pins the map to a BPF filesystem.

When a map is pinned it will remain loaded until the corresponding file is deleted. All parent directories in the given path must already exist.

Source§

impl<V: Pod> PerCpuArray<MapData, V>

Source

pub fn create(max_entries: u32, flags: u32) -> Result<Self, MapError>

Creates a standalone map with the given max_entries capacity and flags.

Trait Implementations§

Source§

impl<T: Borrow<MapData>, V: Pod> IterableMap<u32, PerCpuValues<V>> for PerCpuArray<T, V>

Source§

fn map(&self) -> &MapData

Get a generic map handle
Source§

fn get(&self, key: &u32) -> Result<PerCpuValues<V>, MapError>

Get the value for the provided key
Source§

impl<'a, V: Pod> TryFrom<&'a Map> for PerCpuArray<&'a MapData, V>

Source§

type Error = MapError

The type returned in the event of a conversion error.
Source§

fn try_from(map: &'a Map) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<'a, V: Pod> TryFrom<&'a mut Map> for PerCpuArray<&'a mut MapData, V>

Source§

type Error = MapError

The type returned in the event of a conversion error.
Source§

fn try_from(map: &'a mut Map) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<V: Pod> TryFrom<Map> for PerCpuArray<MapData, V>

Source§

type Error = MapError

The type returned in the event of a conversion error.
Source§

fn try_from(map: Map) -> Result<Self, Self::Error>

Performs the conversion.

Auto Trait Implementations§

§

impl<T, V> Freeze for PerCpuArray<T, V>
where T: Freeze,

§

impl<T, V> RefUnwindSafe for PerCpuArray<T, V>

§

impl<T, V> Send for PerCpuArray<T, V>
where T: Send, V: Send,

§

impl<T, V> Sync for PerCpuArray<T, V>
where T: Sync, V: Sync,

§

impl<T, V> Unpin for PerCpuArray<T, V>
where T: Unpin, V: Unpin,

§

impl<T, V> UnsafeUnpin for PerCpuArray<T, V>
where T: UnsafeUnpin,

§

impl<T, V> UnwindSafe for PerCpuArray<T, V>
where T: UnwindSafe, V: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> FromMapData for T
where T: FromMapData,

Source§

impl<T> InnerMap for T
where T: InnerMap,