aya: add helper to return map name

reviewable/pr675/r3
Adam Preuss 2 years ago
parent ec797b91ef
commit 76af948329

@ -49,7 +49,7 @@
//! implement the [Pod] trait. //! implement the [Pod] trait.
use std::{ use std::{
borrow::BorrowMut, borrow::BorrowMut,
ffi::{c_long, CString}, ffi::{c_long, CStr, CString},
fmt, io, fmt, io,
marker::PhantomData, marker::PhantomData,
mem, mem,
@ -57,6 +57,7 @@ use std::{
os::fd::{AsFd, BorrowedFd, OwnedFd}, os::fd::{AsFd, BorrowedFd, OwnedFd},
path::Path, path::Path,
ptr, ptr,
str::Utf8Error,
}; };
use libc::{getrlimit, rlim_t, rlimit, RLIMIT_MEMLOCK, RLIM_INFINITY}; use libc::{getrlimit, rlim_t, rlimit, RLIMIT_MEMLOCK, RLIM_INFINITY};
@ -120,6 +121,10 @@ pub enum MapError {
name: String, name: String,
}, },
/// Map name is not valid UTF-8
#[error("map name is not valid UTF-8")]
Utf8(#[from] Utf8Error),
/// Failed to create map /// Failed to create map
#[error("failed to create map `{name}` with code {code}")] #[error("failed to create map `{name}` with code {code}")]
CreateError { CreateError {
@ -719,6 +724,13 @@ impl MapData {
fd fd
} }
/// Returns the name of the map.
pub fn name(&self) -> Result<String, MapError> {
let info = bpf_map_get_info_by_fd(self.fd.as_fd())?;
let name_str = unsafe { CStr::from_ptr(info.name.as_ptr()) }.to_str()?;
Ok(name_str.to_string())
}
pub(crate) fn obj(&self) -> &obj::Map { pub(crate) fn obj(&self) -> &obj::Map {
let Self { obj, fd: _ } = self; let Self { obj, fd: _ } = self;
obj obj
@ -994,6 +1006,37 @@ mod tests {
); );
} }
#[test]
fn test_name() {
use crate::generated::bpf_map_info;
const TEST_NAME: &str = "foo";
override_syscall(|call| match call {
Syscall::Bpf {
cmd: bpf_cmd::BPF_MAP_CREATE,
..
} => Ok(42),
Syscall::Bpf {
cmd: bpf_cmd::BPF_OBJ_GET_INFO_BY_FD,
attr,
} => {
assert_eq!(
unsafe { attr.info.info_len },
mem::size_of::<bpf_map_info>() as u32
);
let map_info = unsafe { &mut *(attr.info.info as *mut bpf_map_info) };
map_info.name[..TEST_NAME.len()]
.copy_from_slice(unsafe { std::mem::transmute(TEST_NAME) });
Ok(0)
}
_ => Err((-1, io::Error::from_raw_os_error(EFAULT))),
});
let map_data = MapData::create(new_obj_map(), TEST_NAME, None).unwrap();
assert_eq!(TEST_NAME, map_data.name().unwrap());
}
#[test] #[test]
fn test_create_failed() { fn test_create_failed() {
override_syscall(|_| Err((-42, io::Error::from_raw_os_error(EFAULT)))); override_syscall(|_| Err((-42, io::Error::from_raw_os_error(EFAULT))));

Loading…
Cancel
Save