Skip to content

Commit

Permalink
aya/maps: pin for (async)perf_event_array
Browse files Browse the repository at this point in the history
Implement pinning for perf_event_array and async_perf_event_array.
Additionally make the core MapData.pin method operate on a reference
rather than a mutable reference.

Signed-off-by: astoycos <[email protected]>
  • Loading branch information
astoycos committed Oct 25, 2023
1 parent cc29c8a commit 42c9d76
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 82 deletions.
14 changes: 7 additions & 7 deletions aya/src/maps/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
//! versa. Because of that, all map values must be plain old data and therefore
//! implement the [Pod] trait.
use std::{
borrow::BorrowMut,
borrow::Borrow,
ffi::{c_long, CString},
fmt, io,
marker::PhantomData,
Expand Down Expand Up @@ -330,7 +330,7 @@ impl Map {
///
/// 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.
pub fn pin<P: AsRef<Path>>(&mut self, path: P) -> Result<(), PinError> {
pub fn pin<P: AsRef<Path>>(&self, path: P) -> Result<(), PinError> {
match self {
Self::Array(map) => map.pin(path),
Self::BloomFilter(map) => map.pin(path),
Expand Down Expand Up @@ -369,14 +369,14 @@ macro_rules! impl_map_pin {
<($($ty_param:ident),*)>
$ty:ident
) => {
impl<T: BorrowMut<MapData>, $($ty_param: Pod),*> $ty<T, $($ty_param),*>
impl<T: Borrow<MapData>, $($ty_param: Pod),*> $ty<T, $($ty_param),*>
{
/// 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.
pub fn pin<P: AsRef<Path>>(&mut self, path: P) -> Result<(), PinError> {
let data = self.inner.borrow_mut();
pub fn pin<P: AsRef<Path>>(self, path: P) -> Result<(), PinError> {
let data = self.inner.borrow();
data.pin(path)
}
}
Expand Down Expand Up @@ -589,7 +589,7 @@ impl MapData {
Ok(Self { obj, fd })
}
Err(_) => {
let mut map = Self::create(obj, name, btf_fd)?;
let map = Self::create(obj, name, btf_fd)?;
map.pin(&path).map_err(|error| MapError::PinError {
name: Some(name.into()),
error,
Expand Down Expand Up @@ -687,7 +687,7 @@ impl MapData {
///
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn pin<P: AsRef<Path>>(&mut self, path: P) -> Result<(), PinError> {
pub fn pin<P: AsRef<Path>>(&self, path: P) -> Result<(), PinError> {
use std::os::unix::ffi::OsStrExt as _;

let Self { fd, obj: _ } = self;
Expand Down
15 changes: 13 additions & 2 deletions aya/src/maps/perf/async_perf_event_array.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::borrow::{Borrow, BorrowMut};
use std::{
borrow::{Borrow, BorrowMut},
path::Path,
};

// See https://doc.rust-lang.org/cargo/reference/features.html#mutually-exclusive-features.
//
Expand All @@ -12,7 +15,7 @@ use tokio::io::unix::AsyncFd;

use crate::maps::{
perf::{Events, PerfBufferError, PerfEventArray, PerfEventArrayBuffer},
MapData, MapError,
MapData, MapError, PinError,
};

/// A `Future` based map that can be used to receive events from eBPF programs using the linux
Expand Down Expand Up @@ -105,6 +108,14 @@ impl<T: BorrowMut<MapData>> AsyncPerfEventArray<T> {
let buf = Async::new(buf)?;
Ok(AsyncPerfEventArrayBuffer { buf })
}

/// 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.
pub fn pin<P: AsRef<Path>>(&self, path: P) -> Result<(), PinError> {
self.perf_map.pin(path)
}
}

impl<T: Borrow<MapData>> AsyncPerfEventArray<T> {
Expand Down
14 changes: 12 additions & 2 deletions aya/src/maps/perf/perf_event_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::{
borrow::{Borrow, BorrowMut},
ops::Deref,
os::fd::{AsFd, AsRawFd, BorrowedFd, RawFd},
path::Path,
sync::Arc,
};

Expand All @@ -13,7 +14,7 @@ use bytes::BytesMut;
use crate::{
maps::{
perf::{Events, PerfBuffer, PerfBufferError},
MapData, MapError,
MapData, MapError, PinError,
},
sys::bpf_map_update_elem,
util::page_size,
Expand Down Expand Up @@ -175,7 +176,7 @@ impl<T: Borrow<MapData>> PerfEventArray<T> {
}
}

impl<T: BorrowMut<MapData>> PerfEventArray<T> {
impl<T: Borrow<MapData>> PerfEventArray<T> {
/// Opens the perf buffer at the given index.
///
/// The returned buffer will receive all the events eBPF programs send at the given index.
Expand All @@ -197,4 +198,13 @@ impl<T: BorrowMut<MapData>> PerfEventArray<T> {
_map: self.map.clone(),
})
}

/// 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.
pub fn pin<P: AsRef<Path>>(&self, path: P) -> Result<(), PinError> {
let data: &MapData = self.map.deref().borrow();
data.pin(path)
}
}
6 changes: 3 additions & 3 deletions test/integration-test/src/tests/load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ fn pin_lifecycle_multiple_btf_maps() {
remove_file(map_pin_path).unwrap();
}

let mut map_1: Array<_, u64> = bpf.take_map("map_1").unwrap().try_into().unwrap();
let mut map_2: Array<_, u64> = bpf.take_map("map_2").unwrap().try_into().unwrap();
let mut map_pin_by_name: Array<_, u64> =
let map_1: Array<_, u64> = bpf.take_map("map_1").unwrap().try_into().unwrap();
let map_2: Array<_, u64> = bpf.take_map("map_2").unwrap().try_into().unwrap();
let map_pin_by_name: Array<_, u64> =
bpf.take_map("map_pin_by_name").unwrap().try_into().unwrap();

let prog: &mut UProbe = bpf.program_mut("bpf_prog").unwrap().try_into().unwrap();
Expand Down
Loading

0 comments on commit 42c9d76

Please sign in to comment.