diff --git a/aya/src/maps/mod.rs b/aya/src/maps/mod.rs index a934d3617..0691316a3 100644 --- a/aya/src/maps/mod.rs +++ b/aya/src/maps/mod.rs @@ -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, @@ -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>(&mut self, path: P) -> Result<(), PinError> { + pub fn pin>(&self, path: P) -> Result<(), PinError> { match self { Self::Array(map) => map.pin(path), Self::BloomFilter(map) => map.pin(path), @@ -369,14 +369,14 @@ macro_rules! impl_map_pin { <($($ty_param:ident),*)> $ty:ident ) => { - impl, $($ty_param: Pod),*> $ty + impl, $($ty_param: Pod),*> $ty { /// 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>(&mut self, path: P) -> Result<(), PinError> { - let data = self.inner.borrow_mut(); + pub fn pin>(self, path: P) -> Result<(), PinError> { + let data = self.inner.borrow(); data.pin(path) } } @@ -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, @@ -687,7 +687,7 @@ impl MapData { /// /// # Ok::<(), Box>(()) /// ``` - pub fn pin>(&mut self, path: P) -> Result<(), PinError> { + pub fn pin>(&self, path: P) -> Result<(), PinError> { use std::os::unix::ffi::OsStrExt as _; let Self { fd, obj: _ } = self; diff --git a/aya/src/maps/perf/async_perf_event_array.rs b/aya/src/maps/perf/async_perf_event_array.rs index 4b7d097e7..d34563c9f 100644 --- a/aya/src/maps/perf/async_perf_event_array.rs +++ b/aya/src/maps/perf/async_perf_event_array.rs @@ -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. // @@ -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 @@ -105,6 +108,14 @@ impl> AsyncPerfEventArray { 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>(&mut self, path: P) -> Result<(), PinError> { + self.perf_map.pin(path) + } } impl> AsyncPerfEventArray { diff --git a/aya/src/maps/perf/perf_event_array.rs b/aya/src/maps/perf/perf_event_array.rs index ebeef6726..e76abdc25 100644 --- a/aya/src/maps/perf/perf_event_array.rs +++ b/aya/src/maps/perf/perf_event_array.rs @@ -5,6 +5,7 @@ use std::{ borrow::{Borrow, BorrowMut}, ops::Deref, os::fd::{AsFd as _, AsRawFd, RawFd}, + path::Path, sync::Arc, }; @@ -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, @@ -169,7 +170,7 @@ impl> PerfEventArray { } } -impl> PerfEventArray { +impl> PerfEventArray { /// Opens the perf buffer at the given index. /// /// The returned buffer will receive all the events eBPF programs send at the given index. @@ -191,4 +192,13 @@ impl> PerfEventArray { _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>(&mut self, path: P) -> Result<(), PinError> { + let data: &MapData = self.map.deref().borrow(); + data.pin(path) + } }