diff --git a/aya/src/maps/array/array.rs b/aya/src/maps/array/array.rs index 218ecd58..ede317eb 100644 --- a/aya/src/maps/array/array.rs +++ b/aya/src/maps/array/array.rs @@ -125,8 +125,8 @@ impl + DerefMut, V: Pod> Array { } impl, V: Pod> IterableMap for Array { - fn fd(&self) -> Result { - self.inner.fd_or_err() + fn map(&self) -> &Map { + &self.inner } unsafe fn get(&self, index: &u32) -> Result { diff --git a/aya/src/maps/array/program_array.rs b/aya/src/maps/array/program_array.rs index e94481bf..07e084b0 100644 --- a/aya/src/maps/array/program_array.rs +++ b/aya/src/maps/array/program_array.rs @@ -46,24 +46,8 @@ impl> ProgramArray { Ok(ProgramArray { inner: map }) } - pub unsafe fn get(&self, key: &u32, flags: u64) -> Result { - let fd = self.inner.fd_or_err()?; - let fd = bpf_map_lookup_elem(fd, key, flags).map_err(|(code, io_error)| { - MapError::SyscallError { - call: "bpf_map_lookup_elem".to_owned(), - code, - io_error, - } - })?; - fd.ok_or(MapError::KeyNotFound) - } - - pub unsafe fn iter<'coll>(&'coll self) -> MapIter<'coll, u32, RawFd> { - MapIter::new(self) - } - - pub unsafe fn keys<'coll>(&'coll self) -> MapKeys<'coll, u32, RawFd> { - MapKeys::new(self) + pub unsafe fn keys<'coll>(&'coll self) -> MapKeys<'coll, u32> { + MapKeys::new(&self.inner) } fn check_bounds(&self, index: u32) -> Result<(), MapError> { @@ -77,10 +61,10 @@ impl> ProgramArray { } impl + DerefMut> ProgramArray { - /// Insert a program file descriptor in the jump table. + /// Sets the target program file descriptor for the given index in the jump table. /// - /// When an eBPF program calls `bpf_tail_call(prog_array, index)`, `program` - /// will be executed. + /// When an eBPF program calls `bpf_tail_call(prog_array, index)`, control + /// flow will jump to `program`. /// /// # Example /// ```no_run @@ -91,15 +75,10 @@ impl + DerefMut> ProgramArray { /// /// let mut prog_array = ProgramArray::try_from(bpf.map_mut("JUMP_TABLE")?)?; /// let prog: &KProbe = bpf.program("example_prog")?.try_into()?; - /// prog_array.insert(0, prog, 0 /* flags */); + /// prog_array.set(0, prog, 0 /* flags */); /// # Ok::<(), aya::BpfError>(()) /// ``` - pub fn insert( - &mut self, - index: u32, - program: &dyn ProgramFd, - flags: u64, - ) -> Result<(), MapError> { + pub fn set(&mut self, index: u32, program: &dyn ProgramFd, flags: u64) -> Result<(), MapError> { let fd = self.inner.fd_or_err()?; self.check_bounds(index)?; let prog_fd = program.fd().ok_or(MapError::ProgramNotLoaded)?; @@ -114,8 +93,10 @@ impl + DerefMut> ProgramArray { Ok(()) } - /// Remove an entry from the jump table. - pub fn remove(&mut self, index: &u32) -> Result<(), MapError> { + /// Clears the value at index in the jump table. + /// + /// Calling `bpf_tail_call(prog_array, index)` on an index that has been results in a failure. + pub fn unset(&mut self, index: &u32) -> Result<(), MapError> { let fd = self.inner.fd_or_err()?; self.check_bounds(*index)?; bpf_map_delete_elem(fd, index) @@ -128,16 +109,6 @@ impl + DerefMut> ProgramArray { } } -impl> IterableMap for ProgramArray { - fn fd(&self) -> Result { - self.inner.fd_or_err() - } - - unsafe fn get(&self, index: &u32) -> Result { - self.get(index, 0) - } -} - impl TryFrom for ProgramArray { type Error = MapError; diff --git a/aya/src/maps/hash_map/hash_map.rs b/aya/src/maps/hash_map/hash_map.rs index cd65636a..fd46ef77 100644 --- a/aya/src/maps/hash_map/hash_map.rs +++ b/aya/src/maps/hash_map/hash_map.rs @@ -80,8 +80,8 @@ impl, K: Pod, V: Pod> HashMap { /// An iterator visiting all keys in arbitrary order. The iterator element /// type is `Result`. - pub unsafe fn keys(&self) -> MapKeys<'_, K, V> { - MapKeys::new(self) + pub unsafe fn keys(&self) -> MapKeys<'_, K> { + MapKeys::new(&self.inner) } } @@ -98,8 +98,8 @@ impl, K: Pod, V: Pod> HashMap { } impl, K: Pod, V: Pod> IterableMap for HashMap { - fn fd(&self) -> Result { - self.inner.deref().fd_or_err() + fn map(&self) -> &Map { + &self.inner } unsafe fn get(&self, key: &K) -> Result { diff --git a/aya/src/maps/hash_map/per_cpu_hash_map.rs b/aya/src/maps/hash_map/per_cpu_hash_map.rs index a61fb7ed..f761813a 100644 --- a/aya/src/maps/hash_map/per_cpu_hash_map.rs +++ b/aya/src/maps/hash_map/per_cpu_hash_map.rs @@ -90,8 +90,8 @@ impl, K: Pod, V: Pod> PerCpuHashMap { /// An iterator visiting all keys in arbitrary order. The iterator element /// type is `Result`. - pub unsafe fn keys(&self) -> MapKeys<'_, K, PerCpuValues> { - MapKeys::new(self) + pub unsafe fn keys(&self) -> MapKeys<'_, K> { + MapKeys::new(&self.inner) } } @@ -147,8 +147,8 @@ impl, K: Pod, V: Pod> PerCpuHashMap { impl, K: Pod, V: Pod> IterableMap> for PerCpuHashMap { - fn fd(&self) -> Result { - self.inner.deref().fd_or_err() + fn map(&self) -> &Map { + &self.inner } unsafe fn get(&self, key: &K) -> Result, MapError> { diff --git a/aya/src/maps/mod.rs b/aya/src/maps/mod.rs index ee279c44..17064a9a 100644 --- a/aya/src/maps/mod.rs +++ b/aya/src/maps/mod.rs @@ -29,7 +29,10 @@ //! //! The code above uses `HashMap`, but all the concrete map types implement the //! `TryFrom` trait. -use std::{convert::TryFrom, ffi::CString, io, mem, ops::Deref, os::unix::io::RawFd, ptr}; +use std::{ + convert::TryFrom, ffi::CString, io, marker::PhantomData, mem, ops::Deref, os::unix::io::RawFd, + ptr, +}; use thiserror::Error; use crate::{ @@ -148,19 +151,20 @@ impl Map { } pub(crate) trait IterableMap { - fn fd(&self) -> Result; + fn map(&self) -> ⤅ + unsafe fn get(&self, key: &K) -> Result; } /// Iterator returned by `map.keys()`. -pub struct MapKeys<'coll, K: Pod, V> { - map: &'coll dyn IterableMap, +pub struct MapKeys<'coll, K: Pod> { + map: &'coll Map, err: bool, key: Option, } -impl<'coll, K: Pod, V> MapKeys<'coll, K, V> { - fn new(map: &'coll dyn IterableMap) -> MapKeys<'coll, K, V> { +impl<'coll, K: Pod> MapKeys<'coll, K> { + fn new(map: &'coll Map) -> MapKeys<'coll, K> { MapKeys { map, err: false, @@ -169,7 +173,7 @@ impl<'coll, K: Pod, V> MapKeys<'coll, K, V> { } } -impl Iterator for MapKeys<'_, K, V> { +impl Iterator for MapKeys<'_, K> { type Item = Result; fn next(&mut self) -> Option> { @@ -177,7 +181,7 @@ impl Iterator for MapKeys<'_, K, V> { return None; } - let fd = match self.map.fd() { + let fd = match self.map.fd_or_err() { Ok(fd) => fd, Err(e) => { self.err = true; @@ -208,13 +212,17 @@ impl Iterator for MapKeys<'_, K, V> { /// Iterator returned by `map.iter()`. pub struct MapIter<'coll, K: Pod, V> { - inner: MapKeys<'coll, K, V>, + keys: MapKeys<'coll, K>, + map: &'coll dyn IterableMap, + _v: PhantomData, } impl<'coll, K: Pod, V> MapIter<'coll, K, V> { fn new(map: &'coll dyn IterableMap) -> MapIter<'coll, K, V> { MapIter { - inner: MapKeys::new(map), + keys: MapKeys::new(map.map()), + map, + _v: PhantomData, } } } @@ -224,9 +232,9 @@ impl Iterator for MapIter<'_, K, V> { fn next(&mut self) -> Option { loop { - match self.inner.next() { + match self.keys.next() { Some(Ok(key)) => { - let value = unsafe { self.inner.map.get(&key) }; + let value = unsafe { self.map.get(&key) }; match value { Ok(value) => return Some(Ok((key, value))), Err(MapError::KeyNotFound) => continue,