|
|
@ -68,8 +68,9 @@ use crate::{
|
|
|
|
obj::{self, parse_map_info, BpfSectionKind},
|
|
|
|
obj::{self, parse_map_info, BpfSectionKind},
|
|
|
|
pin::PinError,
|
|
|
|
pin::PinError,
|
|
|
|
sys::{
|
|
|
|
sys::{
|
|
|
|
bpf_create_map, bpf_get_object, bpf_map_freeze, bpf_map_get_info_by_fd,
|
|
|
|
bpf_create_map, bpf_get_object, bpf_map_freeze, bpf_map_get_fd_by_id,
|
|
|
|
bpf_map_get_next_key, bpf_map_update_elem_ptr, bpf_pin_object, SyscallError,
|
|
|
|
bpf_map_get_info_by_fd, bpf_map_get_next_key, bpf_map_update_elem_ptr, bpf_pin_object,
|
|
|
|
|
|
|
|
SyscallError,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
util::{nr_cpus, KernelVersion},
|
|
|
|
util::{nr_cpus, KernelVersion},
|
|
|
|
PinningType, Pod,
|
|
|
|
PinningType, Pod,
|
|
|
@ -648,6 +649,13 @@ impl MapData {
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Loads a map from a map id.
|
|
|
|
|
|
|
|
pub fn from_id(id: u32) -> Result<Self, MapError> {
|
|
|
|
|
|
|
|
bpf_map_get_fd_by_id(id)
|
|
|
|
|
|
|
|
.map_err(MapError::from)
|
|
|
|
|
|
|
|
.and_then(Self::from_fd)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Loads a map from a file descriptor.
|
|
|
|
/// Loads a map from a file descriptor.
|
|
|
|
///
|
|
|
|
///
|
|
|
|
/// If loading from a BPF Filesystem (bpffs) you should use [`Map::from_pin`](crate::maps::MapData::from_pin).
|
|
|
|
/// If loading from a BPF Filesystem (bpffs) you should use [`Map::from_pin`](crate::maps::MapData::from_pin).
|
|
|
@ -935,6 +943,38 @@ mod tests {
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
|
|
|
fn test_from_map_id() {
|
|
|
|
|
|
|
|
override_syscall(|call| match call {
|
|
|
|
|
|
|
|
Syscall::Bpf {
|
|
|
|
|
|
|
|
cmd: bpf_cmd::BPF_MAP_GET_FD_BY_ID,
|
|
|
|
|
|
|
|
attr,
|
|
|
|
|
|
|
|
} => {
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
|
|
|
|
unsafe { attr.__bindgen_anon_6.__bindgen_anon_1.map_id },
|
|
|
|
|
|
|
|
1234
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
Ok(42)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Syscall::Bpf {
|
|
|
|
|
|
|
|
cmd: bpf_cmd::BPF_OBJ_GET_INFO_BY_FD,
|
|
|
|
|
|
|
|
attr,
|
|
|
|
|
|
|
|
} => {
|
|
|
|
|
|
|
|
assert_eq!(unsafe { attr.info.bpf_fd }, 42);
|
|
|
|
|
|
|
|
Ok(0)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
_ => Err((-1, io::Error::from_raw_os_error(EFAULT))),
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assert_matches!(
|
|
|
|
|
|
|
|
MapData::from_id(1234),
|
|
|
|
|
|
|
|
Ok(MapData {
|
|
|
|
|
|
|
|
obj: _,
|
|
|
|
|
|
|
|
fd,
|
|
|
|
|
|
|
|
}) => assert_eq!(fd.as_fd().as_raw_fd(), 42)
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[test]
|
|
|
|
fn test_create() {
|
|
|
|
fn test_create() {
|
|
|
|
override_syscall(|call| match call {
|
|
|
|
override_syscall(|call| match call {
|
|
|
|