Merge pull request #431 from 0b01/refs

aya: use impl Borrow<T> instead of T for maps
pull/444/head
Alessandro Decina 2 years ago committed by GitHub
commit 88d7777553
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,4 +1,5 @@
use std::{ use std::{
borrow::Borrow,
convert::{AsMut, AsRef}, convert::{AsMut, AsRef},
marker::PhantomData, marker::PhantomData,
}; };
@ -88,11 +89,11 @@ impl<T: AsMut<MapData>, V: Pod> Array<T, V> {
/// ///
/// Returns [`MapError::OutOfBounds`] if `index` is out of bounds, [`MapError::SyscallError`] /// Returns [`MapError::OutOfBounds`] if `index` is out of bounds, [`MapError::SyscallError`]
/// if `bpf_map_update_elem` fails. /// 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<V>, flags: u64) -> Result<(), MapError> {
let data = self.inner.as_mut(); let data = self.inner.as_mut();
check_bounds(data, index)?; check_bounds(data, index)?;
let fd = data.fd_or_err()?; let fd = data.fd_or_err()?;
bpf_map_update_elem(fd, Some(&index), &value, flags).map_err(|(_, io_error)| { bpf_map_update_elem(fd, Some(&index), value.borrow(), flags).map_err(|(_, io_error)| {
MapError::SyscallError { MapError::SyscallError {
call: "bpf_map_update_elem".to_owned(), call: "bpf_map_update_elem".to_owned(),
io_error, io_error,

@ -1,5 +1,5 @@
//! A Bloom Filter. //! A Bloom Filter.
use std::{convert::AsRef, marker::PhantomData}; use std::{borrow::Borrow, convert::AsRef, marker::PhantomData};
use crate::{ use crate::{
maps::{check_v_size, MapData, MapError}, maps::{check_v_size, MapData, MapError},
@ -62,11 +62,13 @@ impl<T: AsRef<MapData>, V: Pod> BloomFilter<T, V> {
} }
/// Inserts a value into the map. /// Inserts a value into the map.
pub fn insert(&self, value: V, flags: u64) -> Result<(), MapError> { pub fn insert(&self, value: impl Borrow<V>, flags: u64) -> Result<(), MapError> {
let fd = self.inner.as_ref().fd_or_err()?; let fd = self.inner.as_ref().fd_or_err()?;
bpf_map_push_elem(fd, &value, flags).map_err(|(_, io_error)| MapError::SyscallError { bpf_map_push_elem(fd, value.borrow(), flags).map_err(|(_, io_error)| {
MapError::SyscallError {
call: "bpf_map_push_elem".to_owned(), call: "bpf_map_push_elem".to_owned(),
io_error, io_error,
}
})?; })?;
Ok(()) Ok(())
} }

@ -1,4 +1,5 @@
use std::{ use std::{
borrow::Borrow,
convert::{AsMut, AsRef}, convert::{AsMut, AsRef},
marker::PhantomData, marker::PhantomData,
}; };
@ -78,8 +79,13 @@ impl<T: AsRef<MapData>, K: Pod, V: Pod> HashMap<T, K, V> {
impl<T: AsMut<MapData>, K: Pod, V: Pod> HashMap<T, K, V> { impl<T: AsMut<MapData>, K: Pod, V: Pod> HashMap<T, K, V> {
/// Inserts a key-value pair into the map. /// Inserts a key-value pair into the map.
pub fn insert(&mut self, key: K, value: V, flags: u64) -> Result<(), MapError> { pub fn insert(
hash_map::insert(self.inner.as_mut(), key, value, flags) &mut self,
key: impl Borrow<K>,
value: impl Borrow<V>,
flags: u64,
) -> Result<(), MapError> {
hash_map::insert(self.inner.as_mut(), key.borrow(), value.borrow(), flags)
} }
/// Removes a key from the map. /// Removes a key from the map.
@ -311,6 +317,27 @@ mod tests {
assert!(hm.insert(1, 42, 0).is_ok()); 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] #[test]
fn test_remove_syscall_error() { fn test_remove_syscall_error() {
override_syscall(|_| sys_error(EFAULT)); override_syscall(|_| sys_error(EFAULT));

@ -2,6 +2,7 @@
use crate::{ use crate::{
maps::MapError, maps::MapError,
sys::{bpf_map_delete_elem, bpf_map_update_elem}, sys::{bpf_map_delete_elem, bpf_map_update_elem},
Pod,
}; };
#[allow(clippy::module_inception)] #[allow(clippy::module_inception)]
@ -13,14 +14,14 @@ pub use per_cpu_hash_map::*;
use super::MapData; use super::MapData;
pub(crate) fn insert<K, V>( pub(crate) fn insert<K: Pod, V: Pod>(
map: &mut MapData, map: &mut MapData,
key: K, key: &K,
value: V, value: &V,
flags: u64, flags: u64,
) -> Result<(), MapError> { ) -> Result<(), MapError> {
let fd = map.fd_or_err()?; let fd = map.fd_or_err()?;
bpf_map_update_elem(fd, Some(&key), &value, flags).map_err(|(_, io_error)| { bpf_map_update_elem(fd, Some(key), value, flags).map_err(|(_, io_error)| {
MapError::SyscallError { MapError::SyscallError {
call: "bpf_map_update_elem".to_owned(), call: "bpf_map_update_elem".to_owned(),
io_error, io_error,
@ -30,7 +31,7 @@ pub(crate) fn insert<K, V>(
Ok(()) Ok(())
} }
pub(crate) fn remove<K>(map: &mut MapData, key: &K) -> Result<(), MapError> { pub(crate) fn remove<K: Pod>(map: &mut MapData, key: &K) -> Result<(), MapError> {
let fd = map.fd_or_err()?; let fd = map.fd_or_err()?;
bpf_map_delete_elem(fd, key) bpf_map_delete_elem(fd, key)
.map(|_| ()) .map(|_| ())

@ -1,5 +1,6 @@
//! Per-CPU hash map. //! Per-CPU hash map.
use std::{ use std::{
borrow::Borrow,
convert::{AsMut, AsRef}, convert::{AsMut, AsRef},
marker::PhantomData, marker::PhantomData,
}; };
@ -115,14 +116,19 @@ impl<T: AsMut<MapData>, K: Pod, V: Pod> PerCpuHashMap<T, K, V> {
/// )?; /// )?;
/// # Ok::<(), Error>(()) /// # Ok::<(), Error>(())
/// ``` /// ```
pub fn insert(&mut self, key: K, values: PerCpuValues<V>, flags: u64) -> Result<(), MapError> { pub fn insert(
&mut self,
key: impl Borrow<K>,
values: PerCpuValues<V>,
flags: u64,
) -> Result<(), MapError> {
let fd = self.inner.as_mut().fd_or_err()?; let fd = self.inner.as_mut().fd_or_err()?;
bpf_map_update_elem_per_cpu(fd, &key, &values, flags).map_err(|(_, io_error)| { bpf_map_update_elem_per_cpu(fd, key.borrow(), &values, flags).map_err(
MapError::SyscallError { |(_, io_error)| MapError::SyscallError {
call: "bpf_map_update_elem".to_owned(), call: "bpf_map_update_elem".to_owned(),
io_error, io_error,
} },
})?; )?;
Ok(()) Ok(())
} }

@ -1,5 +1,6 @@
//! A LPM Trie. //! A LPM Trie.
use std::{ use std::{
borrow::Borrow,
convert::{AsMut, AsRef}, convert::{AsMut, AsRef},
marker::PhantomData, marker::PhantomData,
mem, mem,
@ -128,9 +129,14 @@ impl<T: AsRef<MapData>, K: Pod, V: Pod> LpmTrie<T, K, V> {
impl<T: AsMut<MapData>, K: Pod, V: Pod> LpmTrie<T, K, V> { impl<T: AsMut<MapData>, K: Pod, V: Pod> LpmTrie<T, K, V> {
/// Inserts a key value pair into the map. /// Inserts a key value pair into the map.
pub fn insert(&mut self, key: &Key<K>, value: V, flags: u64) -> Result<(), MapError> { pub fn insert(
&mut self,
key: &Key<K>,
value: impl Borrow<V>,
flags: u64,
) -> Result<(), MapError> {
let fd = self.inner.as_mut().fd_or_err()?; let fd = self.inner.as_mut().fd_or_err()?;
bpf_map_update_elem(fd, Some(key), &value, flags).map_err(|(_, io_error)| { bpf_map_update_elem(fd, Some(key), value.borrow(), flags).map_err(|(_, io_error)| {
MapError::SyscallError { MapError::SyscallError {
call: "bpf_map_update_elem".to_owned(), call: "bpf_map_update_elem".to_owned(),
io_error, io_error,

@ -1,5 +1,6 @@
//! A FIFO queue. //! A FIFO queue.
use std::{ use std::{
borrow::Borrow,
convert::{AsMut, AsRef}, convert::{AsMut, AsRef},
marker::PhantomData, marker::PhantomData,
}; };
@ -78,11 +79,13 @@ impl<T: AsMut<MapData>, V: Pod> Queue<T, V> {
/// # Errors /// # Errors
/// ///
/// [`MapError::SyscallError`] if `bpf_map_update_elem` fails. /// [`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<V>, flags: u64) -> Result<(), MapError> {
let fd = self.inner.as_mut().fd_or_err()?; let fd = self.inner.as_mut().fd_or_err()?;
bpf_map_push_elem(fd, &value, flags).map_err(|(_, io_error)| MapError::SyscallError { bpf_map_push_elem(fd, value.borrow(), flags).map_err(|(_, io_error)| {
MapError::SyscallError {
call: "bpf_map_push_elem".to_owned(), call: "bpf_map_push_elem".to_owned(),
io_error, io_error,
}
})?; })?;
Ok(()) Ok(())
} }

@ -1,4 +1,5 @@
use std::{ use std::{
borrow::Borrow,
convert::{AsMut, AsRef}, convert::{AsMut, AsRef},
marker::PhantomData, marker::PhantomData,
os::unix::io::{AsRawFd, RawFd}, os::unix::io::{AsRawFd, RawFd},
@ -115,8 +116,13 @@ impl<T: AsRef<MapData>, K: Pod> SockHash<T, K> {
impl<T: AsMut<MapData>, K: Pod> SockHash<T, K> { impl<T: AsMut<MapData>, K: Pod> SockHash<T, K> {
/// Inserts a socket under the given key. /// Inserts a socket under the given key.
pub fn insert<I: AsRawFd>(&mut self, key: K, value: I, flags: u64) -> Result<(), MapError> { pub fn insert<I: AsRawFd>(
hash_map::insert(self.inner.as_mut(), key, value.as_raw_fd(), flags) &mut self,
key: impl Borrow<K>,
value: I,
flags: u64,
) -> Result<(), MapError> {
hash_map::insert(self.inner.as_mut(), key.borrow(), &value.as_raw_fd(), flags)
} }
/// Removes a socket from the map. /// Removes a socket from the map.

@ -1,5 +1,6 @@
//! A LIFO stack. //! A LIFO stack.
use std::{ use std::{
borrow::Borrow,
convert::{AsMut, AsRef}, convert::{AsMut, AsRef},
marker::PhantomData, marker::PhantomData,
}; };
@ -78,9 +79,9 @@ impl<T: AsMut<MapData>, V: Pod> Stack<T, V> {
/// # Errors /// # Errors
/// ///
/// [`MapError::SyscallError`] if `bpf_map_update_elem` fails. /// [`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<V>, flags: u64) -> Result<(), MapError> {
let fd = self.inner.as_mut().fd_or_err()?; let fd = self.inner.as_mut().fd_or_err()?;
bpf_map_update_elem(fd, None::<&u32>, &value, flags).map_err(|(_, io_error)| { bpf_map_update_elem(fd, None::<&u32>, value.borrow(), flags).map_err(|(_, io_error)| {
MapError::SyscallError { MapError::SyscallError {
call: "bpf_map_update_elem".to_owned(), call: "bpf_map_update_elem".to_owned(),
io_error, io_error,

@ -232,7 +232,7 @@ pub(crate) fn bpf_map_lookup_elem_ptr<K: Pod, V>(
} }
} }
pub(crate) fn bpf_map_update_elem<K, V>( pub(crate) fn bpf_map_update_elem<K: Pod, V: Pod>(
fd: RawFd, fd: RawFd,
key: Option<&K>, key: Option<&K>,
value: &V, value: &V,
@ -251,7 +251,7 @@ pub(crate) fn bpf_map_update_elem<K, V>(
sys_bpf(bpf_cmd::BPF_MAP_UPDATE_ELEM, &attr) sys_bpf(bpf_cmd::BPF_MAP_UPDATE_ELEM, &attr)
} }
pub(crate) fn bpf_map_push_elem<V>(fd: RawFd, value: &V, flags: u64) -> SysResult { pub(crate) fn bpf_map_push_elem<V: Pod>(fd: RawFd, value: &V, flags: u64) -> SysResult {
let mut attr = unsafe { mem::zeroed::<bpf_attr>() }; let mut attr = unsafe { mem::zeroed::<bpf_attr>() };
let u = unsafe { &mut attr.__bindgen_anon_2 }; let u = unsafe { &mut attr.__bindgen_anon_2 };
@ -279,7 +279,7 @@ pub(crate) fn bpf_map_update_elem_ptr<K, V>(
sys_bpf(bpf_cmd::BPF_MAP_UPDATE_ELEM, &attr) sys_bpf(bpf_cmd::BPF_MAP_UPDATE_ELEM, &attr)
} }
pub(crate) fn bpf_map_update_elem_per_cpu<K, V: Pod>( pub(crate) fn bpf_map_update_elem_per_cpu<K: Pod, V: Pod>(
fd: RawFd, fd: RawFd,
key: &K, key: &K,
values: &PerCpuValues<V>, values: &PerCpuValues<V>,
@ -289,7 +289,7 @@ pub(crate) fn bpf_map_update_elem_per_cpu<K, V: Pod>(
bpf_map_update_elem_ptr(fd, key, mem.as_mut_ptr(), flags) bpf_map_update_elem_ptr(fd, key, mem.as_mut_ptr(), flags)
} }
pub(crate) fn bpf_map_delete_elem<K>(fd: RawFd, key: &K) -> SysResult { pub(crate) fn bpf_map_delete_elem<K: Pod>(fd: RawFd, key: &K) -> SysResult {
let mut attr = unsafe { mem::zeroed::<bpf_attr>() }; let mut attr = unsafe { mem::zeroed::<bpf_attr>() };
let u = unsafe { &mut attr.__bindgen_anon_2 }; let u = unsafe { &mut attr.__bindgen_anon_2 };
@ -299,7 +299,7 @@ pub(crate) fn bpf_map_delete_elem<K>(fd: RawFd, key: &K) -> SysResult {
sys_bpf(bpf_cmd::BPF_MAP_DELETE_ELEM, &attr) sys_bpf(bpf_cmd::BPF_MAP_DELETE_ELEM, &attr)
} }
pub(crate) fn bpf_map_get_next_key<K>( pub(crate) fn bpf_map_get_next_key<K: Pod>(
fd: RawFd, fd: RawFd,
key: Option<&K>, key: Option<&K>,
) -> Result<Option<K>, (c_long, io::Error)> { ) -> Result<Option<K>, (c_long, io::Error)> {

@ -1,4 +1,4 @@
use core::{cell::UnsafeCell, marker::PhantomData, mem}; use core::{borrow::Borrow, cell::UnsafeCell, marker::PhantomData, mem};
use aya_bpf_cty::c_void; use aya_bpf_cty::c_void;
@ -89,7 +89,7 @@ impl<K> SockHash<K> {
pub fn redirect_sk_lookup( pub fn redirect_sk_lookup(
&mut self, &mut self,
ctx: &SkLookupContext, ctx: &SkLookupContext,
key: K, key: impl Borrow<K>,
flags: u64, flags: u64,
) -> Result<(), u32> { ) -> Result<(), u32> {
unsafe { unsafe {

Loading…
Cancel
Save