aya: Fix PerCpuHashMap NotFound

PerCpuHashMap was never returning MapError::KeyNotFound because
bpf_map_lookup_elem_per_cpu was replacing Ok(None) with
Ok(Some(zeroed_value)).

Update bpf_map_lookup_elem_per_cpu to map the Option value.
reviewable/pr1182/r4
aorhant 2 months ago committed by Tamir Duberstein
parent 39e40ba5c7
commit 43666b2fb2
No known key found for this signature in database

@ -151,12 +151,23 @@ impl<T: Borrow<MapData>, K: Pod, V: Pod> IterableMap<K, PerCpuValues<V>>
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use std::{ffi::c_long, io};
use assert_matches::assert_matches;
use aya_obj::generated::bpf_map_type::{ use aya_obj::generated::bpf_map_type::{
BPF_MAP_TYPE_LRU_PERCPU_HASH, BPF_MAP_TYPE_PERCPU_HASH, BPF_MAP_TYPE_LRU_PERCPU_HASH, BPF_MAP_TYPE_PERCPU_HASH,
}; };
use libc::ENOENT;
use super::*; use super::*;
use crate::maps::{test_utils, Map}; use crate::{
maps::{test_utils, Map},
sys::{override_syscall, SysResult},
};
fn sys_error(value: i32) -> SysResult<c_long> {
Err((-1, io::Error::from_raw_os_error(value)))
}
#[test] #[test]
fn test_try_from_ok() { fn test_try_from_ok() {
@ -174,4 +185,16 @@ mod tests {
let map = Map::PerCpuLruHashMap(map_data()); let map = Map::PerCpuLruHashMap(map_data());
assert!(PerCpuHashMap::<_, u32, u32>::try_from(&map).is_ok()) assert!(PerCpuHashMap::<_, u32, u32>::try_from(&map).is_ok())
} }
#[test]
fn test_get_not_found() {
let map_data =
|| test_utils::new_map(test_utils::new_obj_map::<u32>(BPF_MAP_TYPE_LRU_PERCPU_HASH));
let map = Map::PerCpuHashMap(map_data());
let map = PerCpuHashMap::<_, u32, u32>::try_from(&map).unwrap();
override_syscall(|_| sys_error(ENOENT));
assert_matches!(map.get(&1, 0), Err(MapError::KeyNotFound));
}
} }

@ -249,7 +249,7 @@ pub(crate) fn bpf_map_lookup_elem_per_cpu<K: Pod, V: Pod>(
) -> SysResult<Option<PerCpuValues<V>>> { ) -> SysResult<Option<PerCpuValues<V>>> {
let mut mem = PerCpuValues::<V>::alloc_kernel_mem().map_err(|io_error| (-1, io_error))?; let mut mem = PerCpuValues::<V>::alloc_kernel_mem().map_err(|io_error| (-1, io_error))?;
match bpf_map_lookup_elem_ptr(fd, Some(key), mem.as_mut_ptr(), flags) { match bpf_map_lookup_elem_ptr(fd, Some(key), mem.as_mut_ptr(), flags) {
Ok(_) => Ok(Some(unsafe { PerCpuValues::from_kernel_mem(mem) })), Ok(v) => Ok(v.map(|()| unsafe { PerCpuValues::from_kernel_mem(mem) })),
Err((_, io_error)) if io_error.raw_os_error() == Some(ENOENT) => Ok(None), Err((_, io_error)) if io_error.raw_os_error() == Some(ENOENT) => Ok(None),
Err(e) => Err(e), Err(e) => Err(e),
} }

@ -194,6 +194,12 @@ pub fn online_cpus() -> Result<Vec<u32>, (&'static str, io::Error)> {
/// ///
/// See `/sys/devices/system/cpu/possible`. /// See `/sys/devices/system/cpu/possible`.
pub fn nr_cpus() -> Result<usize, (&'static str, io::Error)> { pub fn nr_cpus() -> Result<usize, (&'static str, io::Error)> {
#[cfg(miri)]
{
Ok(1)
}
#[cfg(not(miri))]
{
thread_local! { thread_local! {
// TODO(https://github.com/rust-lang/rust/issues/109737): Use // TODO(https://github.com/rust-lang/rust/issues/109737): Use
// `std::cell::OnceCell` when `get_or_try_init` is stabilized. // `std::cell::OnceCell` when `get_or_try_init` is stabilized.
@ -203,6 +209,7 @@ pub fn nr_cpus() -> Result<usize, (&'static str, io::Error)> {
cell.get_or_try_init(|| read_cpu_ranges(POSSIBLE_CPUS).map(|cpus| cpus.len())) cell.get_or_try_init(|| read_cpu_ranges(POSSIBLE_CPUS).map(|cpus| cpus.len()))
.copied() .copied()
}) })
}
} }
fn read_cpu_ranges(path: &'static str) -> Result<Vec<u32>, (&'static str, io::Error)> { fn read_cpu_ranges(path: &'static str) -> Result<Vec<u32>, (&'static str, io::Error)> {

Loading…
Cancel
Save