aya: Remove MapData::pinned

BPF objects can be pinned mutliple times, to multiple different places.
Tracking whether or not a map is pinned in a bool is therefore not sufficient.
We could track this in a HashSet<PathBuf>, but there is really no reason
to track it at all.

Signed-off-by: Dave Tucker <dave@dtucker.co.uk>
pull/789/head
Dave Tucker 2 years ago
parent 41d01f638b
commit 3ffb1ed061

@ -409,8 +409,6 @@ pub(crate) fn check_v_size<V>(map: &MapData) -> Result<(), MapError> {
pub struct MapData { pub struct MapData {
obj: obj::Map, obj: obj::Map,
fd: MapFd, fd: MapFd,
/// Indicates if this map has been pinned to bpffs
pub pinned: bool,
} }
impl MapData { impl MapData {
@ -439,11 +437,7 @@ impl MapData {
} }
})?; })?;
let fd = MapFd(fd); let fd = MapFd(fd);
Ok(Self { Ok(Self { obj, fd })
obj,
fd,
pinned: false,
})
} }
pub(crate) fn create_pinned<P: AsRef<Path>>( pub(crate) fn create_pinned<P: AsRef<Path>>(
@ -471,11 +465,7 @@ impl MapData {
}) { }) {
Ok(fd) => { Ok(fd) => {
let fd = MapFd(fd); let fd = MapFd(fd);
Ok(Self { Ok(Self { obj, fd })
obj,
fd,
pinned: false,
})
} }
Err(_) => { Err(_) => {
let mut map = Self::create(obj, name, btf_fd)?; let mut map = Self::create(obj, name, btf_fd)?;
@ -489,7 +479,7 @@ impl MapData {
} }
pub(crate) fn finalize(&mut self) -> Result<(), MapError> { pub(crate) fn finalize(&mut self) -> Result<(), MapError> {
let Self { obj, fd, pinned: _ } = self; let Self { obj, fd } = self;
if !obj.data().is_empty() && obj.section_kind() != BpfSectionKind::Bss { if !obj.data().is_empty() && obj.section_kind() != BpfSectionKind::Bss {
bpf_map_update_elem_ptr(fd.as_fd(), &0 as *const _, obj.data_mut().as_mut_ptr(), 0) bpf_map_update_elem_ptr(fd.as_fd(), &0 as *const _, obj.data_mut().as_mut_ptr(), 0)
.map_err(|(_, io_error)| SyscallError { .map_err(|(_, io_error)| SyscallError {
@ -534,7 +524,6 @@ impl MapData {
Ok(Self { Ok(Self {
obj: parse_map_info(info, PinningType::ByName), obj: parse_map_info(info, PinningType::ByName),
fd, fd,
pinned: true,
}) })
} }
@ -550,44 +539,34 @@ impl MapData {
Ok(Self { Ok(Self {
obj: parse_map_info(info, PinningType::None), obj: parse_map_info(info, PinningType::None),
fd, fd,
pinned: false,
}) })
} }
pub(crate) fn pin<P: AsRef<Path>>(&mut self, name: &str, path: P) -> Result<(), PinError> { pub(crate) fn pin<P: AsRef<Path>>(&mut self, name: &str, path: P) -> Result<(), PinError> {
use std::os::unix::ffi::OsStrExt as _; use std::os::unix::ffi::OsStrExt as _;
let Self { fd, obj: _ } = self;
let Self { fd, pinned, obj: _ } = self;
if *pinned {
return Err(PinError::AlreadyPinned { name: name.into() });
}
let path = path.as_ref().join(name); let path = path.as_ref().join(name);
let path_string = CString::new(path.as_os_str().as_bytes()) let path_string = CString::new(path.as_os_str().as_bytes()).map_err(|error| {
.map_err(|error| PinError::InvalidPinPath { path, error })?; PinError::InvalidPinPath {
path: path.clone(),
error,
}
})?;
bpf_pin_object(fd.as_fd(), &path_string).map_err(|(_, io_error)| SyscallError { bpf_pin_object(fd.as_fd(), &path_string).map_err(|(_, io_error)| SyscallError {
call: "BPF_OBJ_PIN", call: "BPF_OBJ_PIN",
io_error, io_error,
})?; })?;
*pinned = true;
Ok(()) Ok(())
} }
/// Returns the file descriptor of the map. /// Returns the file descriptor of the map.
pub fn fd(&self) -> &MapFd { pub fn fd(&self) -> &MapFd {
let Self { let Self { obj: _, fd } = self;
obj: _,
fd,
pinned: _,
} = self;
fd fd
} }
pub(crate) fn obj(&self) -> &obj::Map { pub(crate) fn obj(&self) -> &obj::Map {
let Self { let Self { obj, fd: _ } = self;
obj,
fd: _,
pinned: _,
} = self;
obj obj
} }
} }
@ -825,7 +804,6 @@ mod tests {
Ok(MapData { Ok(MapData {
obj: _, obj: _,
fd, fd,
pinned: false
}) => assert_eq!(fd.as_fd().as_raw_fd(), 42) }) => assert_eq!(fd.as_fd().as_raw_fd(), 42)
); );
} }

@ -6,12 +6,6 @@ use thiserror::Error;
/// An error ocurred working with a pinned BPF object. /// An error ocurred working with a pinned BPF object.
#[derive(Error, Debug)] #[derive(Error, Debug)]
pub enum PinError { pub enum PinError {
/// The object has already been pinned.
#[error("the BPF object `{name}` has already been pinned")]
AlreadyPinned {
/// Object name.
name: String,
},
/// The object FD is not known by Aya. /// The object FD is not known by Aya.
#[error("the BPF object `{name}`'s FD is not known")] #[error("the BPF object `{name}`'s FD is not known")]
NoFd { NoFd {

Loading…
Cancel
Save