diff --git a/aya/src/maps/array/array.rs b/aya/src/maps/array/array.rs index 1402d57d..f5a3d449 100644 --- a/aya/src/maps/array/array.rs +++ b/aya/src/maps/array/array.rs @@ -1,6 +1,6 @@ use std::{ convert::{AsMut, AsRef}, - marker::PhantomData, + marker::PhantomData, borrow::Borrow, }; use crate::{ @@ -88,7 +88,7 @@ impl, V: Pod> Array { /// /// Returns [`MapError::OutOfBounds`] if `index` is out of bounds, [`MapError::SyscallError`] /// if `bpf_map_update_elem` fails. - pub fn set(&mut self, index: u32, value: V, flags: u64) -> Result<(), MapError> { + pub fn set(&mut self, index: u32, value: impl Borrow, flags: u64) -> Result<(), MapError> { let data = self.inner.as_mut(); check_bounds(data, index)?; let fd = data.fd_or_err()?; diff --git a/aya/src/maps/bloom_filter.rs b/aya/src/maps/bloom_filter.rs index 5e94e247..78c71e2f 100644 --- a/aya/src/maps/bloom_filter.rs +++ b/aya/src/maps/bloom_filter.rs @@ -1,5 +1,5 @@ //! A Bloom Filter. -use std::{convert::AsRef, marker::PhantomData}; +use std::{convert::AsRef, marker::PhantomData, borrow::Borrow}; use crate::{ maps::{check_v_size, MapData, MapError}, @@ -62,7 +62,7 @@ impl, V: Pod> BloomFilter { } /// Inserts a value into the map. - pub fn insert(&self, value: V, flags: u64) -> Result<(), MapError> { + pub fn insert(&self, value: impl Borrow, flags: u64) -> Result<(), MapError> { let fd = self.inner.as_ref().fd_or_err()?; bpf_map_push_elem(fd, &value, flags).map_err(|(_, io_error)| MapError::SyscallError { call: "bpf_map_push_elem".to_owned(), diff --git a/aya/src/maps/hash_map/hash_map.rs b/aya/src/maps/hash_map/hash_map.rs index d28e3266..b00174bc 100644 --- a/aya/src/maps/hash_map/hash_map.rs +++ b/aya/src/maps/hash_map/hash_map.rs @@ -285,7 +285,7 @@ mod tests { let mut hm = HashMap::<_, u32, u32>::new(&mut map).unwrap(); assert!(matches!( - hm.insert(1u32, 42u32, 0), + hm.insert(1, 42, 0), Err(MapError::SyscallError { call, io_error }) if call == "bpf_map_update_elem" && io_error.raw_os_error() == Some(EFAULT) )); } @@ -311,6 +311,27 @@ mod tests { assert!(hm.insert(1, 42, 0).is_ok()); } + #[test] + fn test_insert_boxed_ok() { + override_syscall(|call| match call { + Syscall::Bpf { + cmd: bpf_cmd::BPF_MAP_UPDATE_ELEM, + .. + } => Ok(1), + _ => sys_error(EFAULT), + }); + + let mut map = MapData { + obj: new_obj_map(), + fd: Some(42), + pinned: false, + btf_fd: None, + }; + let mut hm = HashMap::<_, u32, u32>::new(&mut map).unwrap(); + + assert!(hm.insert(Box::new(1), Box::new(42), 0).is_ok()); + } + #[test] fn test_remove_syscall_error() { override_syscall(|_| sys_error(EFAULT)); diff --git a/aya/src/maps/hash_map/mod.rs b/aya/src/maps/hash_map/mod.rs index 6c102712..b272d23f 100644 --- a/aya/src/maps/hash_map/mod.rs +++ b/aya/src/maps/hash_map/mod.rs @@ -1,5 +1,4 @@ //! Hash map types. - use crate::{ maps::MapError, sys::{bpf_map_delete_elem, bpf_map_update_elem}, 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 0f38f6c6..194f66c4 100644 --- a/aya/src/maps/hash_map/per_cpu_hash_map.rs +++ b/aya/src/maps/hash_map/per_cpu_hash_map.rs @@ -1,7 +1,7 @@ //! Per-CPU hash map. use std::{ convert::{AsMut, AsRef}, - marker::PhantomData, + marker::PhantomData, borrow::Borrow, }; use crate::{ @@ -115,7 +115,7 @@ impl, K: Pod, V: Pod> PerCpuHashMap { /// )?; /// # Ok::<(), Error>(()) /// ``` - pub fn insert(&mut self, key: K, values: PerCpuValues, flags: u64) -> Result<(), MapError> { + pub fn insert(&mut self, key: impl Borrow, values: PerCpuValues, flags: u64) -> Result<(), MapError> { let fd = self.inner.as_mut().fd_or_err()?; bpf_map_update_elem_per_cpu(fd, &key, &values, flags).map_err(|(_, io_error)| { MapError::SyscallError { diff --git a/aya/src/maps/lpm_trie.rs b/aya/src/maps/lpm_trie.rs index ea6d8ffc..3555d036 100644 --- a/aya/src/maps/lpm_trie.rs +++ b/aya/src/maps/lpm_trie.rs @@ -2,7 +2,7 @@ use std::{ convert::{AsMut, AsRef}, marker::PhantomData, - mem, + mem, borrow::Borrow, }; use crate::{ @@ -128,7 +128,7 @@ impl, K: Pod, V: Pod> LpmTrie { impl, K: Pod, V: Pod> LpmTrie { /// Inserts a key value pair into the map. - pub fn insert(&mut self, key: &Key, value: V, flags: u64) -> Result<(), MapError> { + pub fn insert(&mut self, key: &Key, value: impl Borrow, flags: u64) -> Result<(), MapError> { let fd = self.inner.as_mut().fd_or_err()?; bpf_map_update_elem(fd, Some(key), &value, flags).map_err(|(_, io_error)| { MapError::SyscallError { diff --git a/aya/src/maps/queue.rs b/aya/src/maps/queue.rs index e7233773..383659f1 100644 --- a/aya/src/maps/queue.rs +++ b/aya/src/maps/queue.rs @@ -1,7 +1,7 @@ //! A FIFO queue. use std::{ convert::{AsMut, AsRef}, - marker::PhantomData, + marker::PhantomData, borrow::Borrow, }; use crate::{ @@ -78,7 +78,7 @@ impl, V: Pod> Queue { /// # Errors /// /// [`MapError::SyscallError`] if `bpf_map_update_elem` fails. - pub fn push(&mut self, value: V, flags: u64) -> Result<(), MapError> { + pub fn push(&mut self, value: impl Borrow, flags: u64) -> Result<(), MapError> { let fd = self.inner.as_mut().fd_or_err()?; bpf_map_push_elem(fd, &value, flags).map_err(|(_, io_error)| MapError::SyscallError { call: "bpf_map_push_elem".to_owned(), diff --git a/aya/src/maps/sock/sock_hash.rs b/aya/src/maps/sock/sock_hash.rs index 943cfddc..74659fb2 100644 --- a/aya/src/maps/sock/sock_hash.rs +++ b/aya/src/maps/sock/sock_hash.rs @@ -1,7 +1,7 @@ use std::{ convert::{AsMut, AsRef}, marker::PhantomData, - os::unix::io::{AsRawFd, RawFd}, + os::unix::io::{AsRawFd, RawFd}, borrow::Borrow, }; use crate::{ @@ -115,7 +115,7 @@ impl, K: Pod> SockHash { impl, K: Pod> SockHash { /// Inserts a socket under the given key. - pub fn insert(&mut self, key: K, value: I, flags: u64) -> Result<(), MapError> { + pub fn insert(&mut self, key: impl Borrow, value: I, flags: u64) -> Result<(), MapError> { hash_map::insert(self.inner.as_mut(), &key, &value.as_raw_fd(), flags) } diff --git a/aya/src/maps/stack.rs b/aya/src/maps/stack.rs index 1350ef30..9d48e1c3 100644 --- a/aya/src/maps/stack.rs +++ b/aya/src/maps/stack.rs @@ -1,7 +1,7 @@ //! A LIFO stack. use std::{ convert::{AsMut, AsRef}, - marker::PhantomData, + marker::PhantomData, borrow::Borrow, }; use crate::{ @@ -78,7 +78,7 @@ impl, V: Pod> Stack { /// # Errors /// /// [`MapError::SyscallError`] if `bpf_map_update_elem` fails. - pub fn push(&mut self, value: V, flags: u64) -> Result<(), MapError> { + pub fn push(&mut self, value: impl Borrow, flags: u64) -> Result<(), MapError> { let fd = self.inner.as_mut().fd_or_err()?; bpf_map_update_elem(fd, None::<&u32>, &value, flags).map_err(|(_, io_error)| { MapError::SyscallError { diff --git a/bpf/aya-bpf/src/maps/sock_hash.rs b/bpf/aya-bpf/src/maps/sock_hash.rs index a43fcc81..aacfda66 100644 --- a/bpf/aya-bpf/src/maps/sock_hash.rs +++ b/bpf/aya-bpf/src/maps/sock_hash.rs @@ -1,4 +1,4 @@ -use core::{cell::UnsafeCell, marker::PhantomData, mem}; +use core::{cell::UnsafeCell, marker::PhantomData, mem, borrow::Borrow}; use aya_bpf_cty::c_void; @@ -89,7 +89,7 @@ impl SockHash { pub fn redirect_sk_lookup( &mut self, ctx: &SkLookupContext, - key: K, + key: impl Borrow, flags: u64, ) -> Result<(), u32> { unsafe {