Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

aya/maps: pin for (async)perf_event_array #822

Merged
merged 1 commit into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 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 @@ -332,7 +332,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 @@ -360,7 +360,6 @@ impl Map {
}

// Implements map pinning for different map implementations
// TODO add support for PerfEventArrays and AsyncPerfEventArrays
macro_rules! impl_map_pin {
($ty_param:tt {
$($ty:ident),+ $(,)?
Expand All @@ -371,14 +370,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),*>
astoycos marked this conversation as resolved.
Show resolved Hide resolved
{
/// 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 @@ -591,7 +590,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 +686,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
12 changes: 11 additions & 1 deletion 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 @@ -173,6 +174,15 @@ impl<T: Borrow<MapData>> PerfEventArray<T> {
page_size: page_size(),
})
}

/// 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)
}
}

impl<T: BorrowMut<MapData>> PerfEventArray<T> {
Expand Down
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