aya: MapData should be Borrow, not AsRef

We don't ever do ref-to-ref conversion for MapData so Borrow should
suffice.

Signed-off-by: Dave Tucker <dave@dtucker.co.uk>
pull/525/head
Dave Tucker 2 years ago
parent 56c143831e
commit b1a70fc6e4

@ -1,6 +1,5 @@
use std::{ use std::{
borrow::Borrow, borrow::{Borrow, BorrowMut},
convert::{AsMut, AsRef},
marker::PhantomData, marker::PhantomData,
}; };
@ -35,9 +34,9 @@ pub struct Array<T, V: Pod> {
_v: PhantomData<V>, _v: PhantomData<V>,
} }
impl<T: AsRef<MapData>, V: Pod> Array<T, V> { impl<T: Borrow<MapData>, V: Pod> Array<T, V> {
pub(crate) fn new(map: T) -> Result<Array<T, V>, MapError> { pub(crate) fn new(map: T) -> Result<Array<T, V>, MapError> {
let data = map.as_ref(); let data = map.borrow();
check_kv_size::<u32, V>(data)?; check_kv_size::<u32, V>(data)?;
let _fd = data.fd_or_err()?; let _fd = data.fd_or_err()?;
@ -52,7 +51,7 @@ impl<T: AsRef<MapData>, V: Pod> Array<T, V> {
/// ///
/// This corresponds to the value of `bpf_map_def::max_entries` on the eBPF side. /// This corresponds to the value of `bpf_map_def::max_entries` on the eBPF side.
pub fn len(&self) -> u32 { pub fn len(&self) -> u32 {
self.inner.as_ref().obj.max_entries() self.inner.borrow().obj.max_entries()
} }
/// Returns the value stored at the given index. /// Returns the value stored at the given index.
@ -62,7 +61,7 @@ impl<T: AsRef<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_lookup_elem` fails. /// if `bpf_map_lookup_elem` fails.
pub fn get(&self, index: &u32, flags: u64) -> Result<V, MapError> { pub fn get(&self, index: &u32, flags: u64) -> Result<V, MapError> {
let data = self.inner.as_ref(); let data = self.inner.borrow();
check_bounds(data, *index)?; check_bounds(data, *index)?;
let fd = data.fd_or_err()?; let fd = data.fd_or_err()?;
@ -82,7 +81,7 @@ impl<T: AsRef<MapData>, V: Pod> Array<T, V> {
} }
} }
impl<T: AsMut<MapData>, V: Pod> Array<T, V> { impl<T: BorrowMut<MapData>, V: Pod> Array<T, V> {
/// Sets the value of the element at the given index. /// Sets the value of the element at the given index.
/// ///
/// # Errors /// # Errors
@ -90,7 +89,7 @@ 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: impl Borrow<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.borrow_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.borrow(), flags).map_err(|(_, io_error)| { bpf_map_update_elem(fd, Some(&index), value.borrow(), flags).map_err(|(_, io_error)| {
@ -103,9 +102,9 @@ impl<T: AsMut<MapData>, V: Pod> Array<T, V> {
} }
} }
impl<T: AsRef<MapData>, V: Pod> IterableMap<u32, V> for Array<T, V> { impl<T: Borrow<MapData>, V: Pod> IterableMap<u32, V> for Array<T, V> {
fn map(&self) -> &MapData { fn map(&self) -> &MapData {
self.inner.as_ref() self.inner.borrow()
} }
fn get(&self, index: &u32) -> Result<V, MapError> { fn get(&self, index: &u32) -> Result<V, MapError> {

@ -1,5 +1,5 @@
use std::{ use std::{
convert::{AsMut, AsRef}, borrow::{Borrow, BorrowMut},
marker::PhantomData, marker::PhantomData,
}; };
@ -53,9 +53,9 @@ pub struct PerCpuArray<T, V: Pod> {
_v: PhantomData<V>, _v: PhantomData<V>,
} }
impl<T: AsRef<MapData>, V: Pod> PerCpuArray<T, V> { impl<T: Borrow<MapData>, V: Pod> PerCpuArray<T, V> {
pub(crate) fn new(map: T) -> Result<PerCpuArray<T, V>, MapError> { pub(crate) fn new(map: T) -> Result<PerCpuArray<T, V>, MapError> {
let data = map.as_ref(); let data = map.borrow();
check_kv_size::<u32, V>(data)?; check_kv_size::<u32, V>(data)?;
let _fd = data.fd_or_err()?; let _fd = data.fd_or_err()?;
@ -70,7 +70,7 @@ impl<T: AsRef<MapData>, V: Pod> PerCpuArray<T, V> {
/// ///
/// This corresponds to the value of `bpf_map_def::max_entries` on the eBPF side. /// This corresponds to the value of `bpf_map_def::max_entries` on the eBPF side.
pub fn len(&self) -> u32 { pub fn len(&self) -> u32 {
self.inner.as_ref().obj.max_entries() self.inner.borrow().obj.max_entries()
} }
/// Returns a slice of values - one for each CPU - stored at the given index. /// Returns a slice of values - one for each CPU - stored at the given index.
@ -80,7 +80,7 @@ impl<T: AsRef<MapData>, V: Pod> PerCpuArray<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_lookup_elem` fails. /// if `bpf_map_lookup_elem` fails.
pub fn get(&self, index: &u32, flags: u64) -> Result<PerCpuValues<V>, MapError> { pub fn get(&self, index: &u32, flags: u64) -> Result<PerCpuValues<V>, MapError> {
let data = self.inner.as_ref(); let data = self.inner.borrow();
check_bounds(data, *index)?; check_bounds(data, *index)?;
let fd = data.fd_or_err()?; let fd = data.fd_or_err()?;
@ -100,7 +100,7 @@ impl<T: AsRef<MapData>, V: Pod> PerCpuArray<T, V> {
} }
} }
impl<T: AsMut<MapData>, V: Pod> PerCpuArray<T, V> { impl<T: BorrowMut<MapData>, V: Pod> PerCpuArray<T, V> {
/// Sets the values - one for each CPU - at the given index. /// Sets the values - one for each CPU - at the given index.
/// ///
/// # Errors /// # Errors
@ -108,7 +108,7 @@ impl<T: AsMut<MapData>, V: Pod> PerCpuArray<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, values: PerCpuValues<V>, flags: u64) -> Result<(), MapError> { pub fn set(&mut self, index: u32, values: PerCpuValues<V>, flags: u64) -> Result<(), MapError> {
let data = self.inner.as_mut(); let data = self.inner.borrow_mut();
check_bounds(data, index)?; check_bounds(data, index)?;
let fd = data.fd_or_err()?; let fd = data.fd_or_err()?;
@ -122,9 +122,9 @@ impl<T: AsMut<MapData>, V: Pod> PerCpuArray<T, V> {
} }
} }
impl<T: AsRef<MapData>, V: Pod> IterableMap<u32, PerCpuValues<V>> for PerCpuArray<T, V> { impl<T: Borrow<MapData>, V: Pod> IterableMap<u32, PerCpuValues<V>> for PerCpuArray<T, V> {
fn map(&self) -> &MapData { fn map(&self) -> &MapData {
self.inner.as_ref() self.inner.borrow()
} }
fn get(&self, index: &u32) -> Result<PerCpuValues<V>, MapError> { fn get(&self, index: &u32) -> Result<PerCpuValues<V>, MapError> {

@ -1,7 +1,7 @@
//! An array of eBPF program file descriptors used as a jump table. //! An array of eBPF program file descriptors used as a jump table.
use std::{ use std::{
convert::{AsMut, AsRef}, borrow::{Borrow, BorrowMut},
os::unix::prelude::{AsRawFd, RawFd}, os::unix::prelude::{AsRawFd, RawFd},
}; };
@ -51,9 +51,9 @@ pub struct ProgramArray<T> {
inner: T, inner: T,
} }
impl<T: AsRef<MapData>> ProgramArray<T> { impl<T: Borrow<MapData>> ProgramArray<T> {
pub(crate) fn new(map: T) -> Result<ProgramArray<T>, MapError> { pub(crate) fn new(map: T) -> Result<ProgramArray<T>, MapError> {
let data = map.as_ref(); let data = map.borrow();
check_kv_size::<u32, RawFd>(data)?; check_kv_size::<u32, RawFd>(data)?;
let _fd = data.fd_or_err()?; let _fd = data.fd_or_err()?;
@ -64,17 +64,17 @@ impl<T: AsRef<MapData>> ProgramArray<T> {
/// An iterator over the indices of the array that point to a program. The iterator item type /// An iterator over the indices of the array that point to a program. The iterator item type
/// is `Result<u32, MapError>`. /// is `Result<u32, MapError>`.
pub fn indices(&self) -> MapKeys<'_, u32> { pub fn indices(&self) -> MapKeys<'_, u32> {
MapKeys::new(self.inner.as_ref()) MapKeys::new(self.inner.borrow())
} }
} }
impl<T: AsMut<MapData>> ProgramArray<T> { impl<T: BorrowMut<MapData>> ProgramArray<T> {
/// Sets the target program file descriptor for the given index 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(ctx, prog_array, index)`, control /// When an eBPF program calls `bpf_tail_call(ctx, prog_array, index)`, control
/// flow will jump to `program`. /// flow will jump to `program`.
pub fn set(&mut self, index: u32, program: ProgramFd, flags: u64) -> Result<(), MapError> { pub fn set(&mut self, index: u32, program: ProgramFd, flags: u64) -> Result<(), MapError> {
let data = self.inner.as_mut(); let data = self.inner.borrow_mut();
check_bounds(data, index)?; check_bounds(data, index)?;
let fd = data.fd_or_err()?; let fd = data.fd_or_err()?;
let prog_fd = program.as_raw_fd(); let prog_fd = program.as_raw_fd();
@ -93,9 +93,9 @@ impl<T: AsMut<MapData>> ProgramArray<T> {
/// Calling `bpf_tail_call(ctx, prog_array, index)` on an index that has been cleared returns an /// Calling `bpf_tail_call(ctx, prog_array, index)` on an index that has been cleared returns an
/// error. /// error.
pub fn clear_index(&mut self, index: &u32) -> Result<(), MapError> { pub fn clear_index(&mut self, index: &u32) -> Result<(), MapError> {
let data = self.inner.as_mut(); let data = self.inner.borrow_mut();
check_bounds(data, *index)?; check_bounds(data, *index)?;
let fd = self.inner.as_mut().fd_or_err()?; let fd = self.inner.borrow_mut().fd_or_err()?;
bpf_map_delete_elem(fd, index) bpf_map_delete_elem(fd, index)
.map(|_| ()) .map(|_| ())

@ -1,5 +1,5 @@
//! A Bloom Filter. //! A Bloom Filter.
use std::{borrow::Borrow, convert::AsRef, marker::PhantomData}; use std::{borrow::Borrow, marker::PhantomData};
use crate::{ use crate::{
maps::{check_v_size, MapData, MapError}, maps::{check_v_size, MapData, MapError},
@ -35,9 +35,9 @@ pub struct BloomFilter<T, V: Pod> {
_v: PhantomData<V>, _v: PhantomData<V>,
} }
impl<T: AsRef<MapData>, V: Pod> BloomFilter<T, V> { impl<T: Borrow<MapData>, V: Pod> BloomFilter<T, V> {
pub(crate) fn new(map: T) -> Result<BloomFilter<T, V>, MapError> { pub(crate) fn new(map: T) -> Result<BloomFilter<T, V>, MapError> {
let data = map.as_ref(); let data = map.borrow();
check_v_size::<V>(data)?; check_v_size::<V>(data)?;
let _ = data.fd_or_err()?; let _ = data.fd_or_err()?;
@ -50,7 +50,7 @@ impl<T: AsRef<MapData>, V: Pod> BloomFilter<T, V> {
/// Query the existence of the element. /// Query the existence of the element.
pub fn contains(&self, mut value: &V, flags: u64) -> Result<(), MapError> { pub fn contains(&self, mut value: &V, flags: u64) -> Result<(), MapError> {
let fd = self.inner.as_ref().fd_or_err()?; let fd = self.inner.borrow().fd_or_err()?;
bpf_map_lookup_elem_ptr::<u32, _>(fd, None, &mut value, flags) bpf_map_lookup_elem_ptr::<u32, _>(fd, None, &mut value, flags)
.map_err(|(_, io_error)| MapError::SyscallError { .map_err(|(_, io_error)| MapError::SyscallError {
@ -63,7 +63,7 @@ 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: impl Borrow<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.borrow().fd_or_err()?;
bpf_map_push_elem(fd, value.borrow(), flags).map_err(|(_, io_error)| { bpf_map_push_elem(fd, value.borrow(), flags).map_err(|(_, io_error)| {
MapError::SyscallError { MapError::SyscallError {
call: "bpf_map_push_elem".to_owned(), call: "bpf_map_push_elem".to_owned(),

@ -1,6 +1,5 @@
use std::{ use std::{
borrow::Borrow, borrow::{Borrow, BorrowMut},
convert::{AsMut, AsRef},
marker::PhantomData, marker::PhantomData,
}; };
@ -39,9 +38,9 @@ pub struct HashMap<T, K, V> {
_v: PhantomData<V>, _v: PhantomData<V>,
} }
impl<T: AsRef<MapData>, K: Pod, V: Pod> HashMap<T, K, V> { impl<T: Borrow<MapData>, K: Pod, V: Pod> HashMap<T, K, V> {
pub(crate) fn new(map: T) -> Result<HashMap<T, K, V>, MapError> { pub(crate) fn new(map: T) -> Result<HashMap<T, K, V>, MapError> {
let data = map.as_ref(); let data = map.borrow();
check_kv_size::<K, V>(data)?; check_kv_size::<K, V>(data)?;
let _ = data.fd_or_err()?; let _ = data.fd_or_err()?;
@ -54,7 +53,7 @@ impl<T: AsRef<MapData>, K: Pod, V: Pod> HashMap<T, K, V> {
/// Returns a copy of the value associated with the key. /// Returns a copy of the value associated with the key.
pub fn get(&self, key: &K, flags: u64) -> Result<V, MapError> { pub fn get(&self, key: &K, flags: u64) -> Result<V, MapError> {
let fd = self.inner.as_ref().fd_or_err()?; let fd = self.inner.borrow().fd_or_err()?;
let value = bpf_map_lookup_elem(fd, key, flags).map_err(|(_, io_error)| { let value = bpf_map_lookup_elem(fd, key, flags).map_err(|(_, io_error)| {
MapError::SyscallError { MapError::SyscallError {
call: "bpf_map_lookup_elem".to_owned(), call: "bpf_map_lookup_elem".to_owned(),
@ -73,11 +72,11 @@ impl<T: AsRef<MapData>, K: Pod, V: Pod> HashMap<T, K, V> {
/// An iterator visiting all keys in arbitrary order. The iterator element /// An iterator visiting all keys in arbitrary order. The iterator element
/// type is `Result<K, MapError>`. /// type is `Result<K, MapError>`.
pub fn keys(&self) -> MapKeys<'_, K> { pub fn keys(&self) -> MapKeys<'_, K> {
MapKeys::new(self.inner.as_ref()) MapKeys::new(self.inner.borrow())
} }
} }
impl<T: AsMut<MapData>, K: Pod, V: Pod> HashMap<T, K, V> { impl<T: BorrowMut<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( pub fn insert(
&mut self, &mut self,
@ -85,18 +84,18 @@ impl<T: AsMut<MapData>, K: Pod, V: Pod> HashMap<T, K, V> {
value: impl Borrow<V>, value: impl Borrow<V>,
flags: u64, flags: u64,
) -> Result<(), MapError> { ) -> Result<(), MapError> {
hash_map::insert(self.inner.as_mut(), key.borrow(), value.borrow(), flags) hash_map::insert(self.inner.borrow_mut(), key.borrow(), value.borrow(), flags)
} }
/// Removes a key from the map. /// Removes a key from the map.
pub fn remove(&mut self, key: &K) -> Result<(), MapError> { pub fn remove(&mut self, key: &K) -> Result<(), MapError> {
hash_map::remove(self.inner.as_mut(), key) hash_map::remove(self.inner.borrow_mut(), key)
} }
} }
impl<T: AsRef<MapData>, K: Pod, V: Pod> IterableMap<K, V> for HashMap<T, K, V> { impl<T: Borrow<MapData>, K: Pod, V: Pod> IterableMap<K, V> for HashMap<T, K, V> {
fn map(&self) -> &MapData { fn map(&self) -> &MapData {
self.inner.as_ref() self.inner.borrow()
} }
fn get(&self, key: &K) -> Result<V, MapError> { fn get(&self, key: &K) -> Result<V, MapError> {

@ -1,7 +1,6 @@
//! Per-CPU hash map. //! Per-CPU hash map.
use std::{ use std::{
borrow::Borrow, borrow::{Borrow, BorrowMut},
convert::{AsMut, AsRef},
marker::PhantomData, marker::PhantomData,
}; };
@ -48,9 +47,9 @@ pub struct PerCpuHashMap<T, K: Pod, V: Pod> {
_v: PhantomData<V>, _v: PhantomData<V>,
} }
impl<T: AsRef<MapData>, K: Pod, V: Pod> PerCpuHashMap<T, K, V> { impl<T: Borrow<MapData>, K: Pod, V: Pod> PerCpuHashMap<T, K, V> {
pub(crate) fn new(map: T) -> Result<PerCpuHashMap<T, K, V>, MapError> { pub(crate) fn new(map: T) -> Result<PerCpuHashMap<T, K, V>, MapError> {
let data = map.as_ref(); let data = map.borrow();
check_kv_size::<K, V>(data)?; check_kv_size::<K, V>(data)?;
let _ = data.fd_or_err()?; let _ = data.fd_or_err()?;
@ -64,7 +63,7 @@ impl<T: AsRef<MapData>, K: Pod, V: Pod> PerCpuHashMap<T, K, V> {
/// Returns a slice of values - one for each CPU - associated with the key. /// Returns a slice of values - one for each CPU - associated with the key.
pub fn get(&self, key: &K, flags: u64) -> Result<PerCpuValues<V>, MapError> { pub fn get(&self, key: &K, flags: u64) -> Result<PerCpuValues<V>, MapError> {
let fd = self.inner.as_ref().fd_or_err()?; let fd = self.inner.borrow().fd_or_err()?;
let values = bpf_map_lookup_elem_per_cpu(fd, key, flags).map_err(|(_, io_error)| { let values = bpf_map_lookup_elem_per_cpu(fd, key, flags).map_err(|(_, io_error)| {
MapError::SyscallError { MapError::SyscallError {
call: "bpf_map_lookup_elem".to_owned(), call: "bpf_map_lookup_elem".to_owned(),
@ -83,11 +82,11 @@ impl<T: AsRef<MapData>, K: Pod, V: Pod> PerCpuHashMap<T, K, V> {
/// An iterator visiting all keys in arbitrary order. The iterator element /// An iterator visiting all keys in arbitrary order. The iterator element
/// type is `Result<K, MapError>`. /// type is `Result<K, MapError>`.
pub fn keys(&self) -> MapKeys<'_, K> { pub fn keys(&self) -> MapKeys<'_, K> {
MapKeys::new(self.inner.as_ref()) MapKeys::new(self.inner.borrow())
} }
} }
impl<T: AsMut<MapData>, K: Pod, V: Pod> PerCpuHashMap<T, K, V> { impl<T: BorrowMut<MapData>, K: Pod, V: Pod> PerCpuHashMap<T, K, V> {
/// Inserts a slice of values - one for each CPU - for the given key. /// Inserts a slice of values - one for each CPU - for the given key.
/// ///
/// # Examples /// # Examples
@ -122,7 +121,7 @@ impl<T: AsMut<MapData>, K: Pod, V: Pod> PerCpuHashMap<T, K, V> {
values: PerCpuValues<V>, values: PerCpuValues<V>,
flags: u64, flags: u64,
) -> Result<(), MapError> { ) -> Result<(), MapError> {
let fd = self.inner.as_mut().fd_or_err()?; let fd = self.inner.borrow_mut().fd_or_err()?;
bpf_map_update_elem_per_cpu(fd, key.borrow(), &values, flags).map_err( bpf_map_update_elem_per_cpu(fd, key.borrow(), &values, flags).map_err(
|(_, io_error)| MapError::SyscallError { |(_, io_error)| MapError::SyscallError {
call: "bpf_map_update_elem".to_owned(), call: "bpf_map_update_elem".to_owned(),
@ -135,13 +134,15 @@ impl<T: AsMut<MapData>, K: Pod, V: Pod> PerCpuHashMap<T, K, V> {
/// Removes a key from the map. /// Removes a key from the map.
pub fn remove(&mut self, key: &K) -> Result<(), MapError> { pub fn remove(&mut self, key: &K) -> Result<(), MapError> {
hash_map::remove(self.inner.as_mut(), key) hash_map::remove(self.inner.borrow_mut(), key)
} }
} }
impl<T: AsRef<MapData>, K: Pod, V: Pod> IterableMap<K, PerCpuValues<V>> for PerCpuHashMap<T, K, V> { impl<T: Borrow<MapData>, K: Pod, V: Pod> IterableMap<K, PerCpuValues<V>>
for PerCpuHashMap<T, K, V>
{
fn map(&self) -> &MapData { fn map(&self) -> &MapData {
self.inner.as_ref() self.inner.borrow()
} }
fn get(&self, key: &K) -> Result<PerCpuValues<V>, MapError> { fn get(&self, key: &K) -> Result<PerCpuValues<V>, MapError> {

@ -1,7 +1,6 @@
//! A LPM Trie. //! A LPM Trie.
use std::{ use std::{
borrow::Borrow, borrow::{Borrow, BorrowMut},
convert::{AsMut, AsRef},
marker::PhantomData, marker::PhantomData,
}; };
@ -99,9 +98,9 @@ impl<K: Pod> Clone for Key<K> {
// A Pod impl is required as Key struct is a key for a map. // A Pod impl is required as Key struct is a key for a map.
unsafe impl<K: Pod> Pod for Key<K> {} unsafe impl<K: Pod> Pod for Key<K> {}
impl<T: AsRef<MapData>, K: Pod, V: Pod> LpmTrie<T, K, V> { impl<T: Borrow<MapData>, K: Pod, V: Pod> LpmTrie<T, K, V> {
pub(crate) fn new(map: T) -> Result<LpmTrie<T, K, V>, MapError> { pub(crate) fn new(map: T) -> Result<LpmTrie<T, K, V>, MapError> {
let data = map.as_ref(); let data = map.borrow();
check_kv_size::<Key<K>, V>(data)?; check_kv_size::<Key<K>, V>(data)?;
let _ = data.fd_or_err()?; let _ = data.fd_or_err()?;
@ -115,7 +114,7 @@ impl<T: AsRef<MapData>, K: Pod, V: Pod> LpmTrie<T, K, V> {
/// Returns a copy of the value associated with the longest prefix matching key in the LpmTrie. /// Returns a copy of the value associated with the longest prefix matching key in the LpmTrie.
pub fn get(&self, key: &Key<K>, flags: u64) -> Result<V, MapError> { pub fn get(&self, key: &Key<K>, flags: u64) -> Result<V, MapError> {
let fd = self.inner.as_ref().fd_or_err()?; let fd = self.inner.borrow().fd_or_err()?;
let value = bpf_map_lookup_elem(fd, key, flags).map_err(|(_, io_error)| { let value = bpf_map_lookup_elem(fd, key, flags).map_err(|(_, io_error)| {
MapError::SyscallError { MapError::SyscallError {
call: "bpf_map_lookup_elem".to_owned(), call: "bpf_map_lookup_elem".to_owned(),
@ -134,17 +133,17 @@ impl<T: AsRef<MapData>, K: Pod, V: Pod> LpmTrie<T, K, V> {
/// An iterator visiting all keys in arbitrary order. The iterator element /// An iterator visiting all keys in arbitrary order. The iterator element
/// type is `Result<Key<K>, MapError>`. /// type is `Result<Key<K>, MapError>`.
pub fn keys(&self) -> MapKeys<'_, Key<K>> { pub fn keys(&self) -> MapKeys<'_, Key<K>> {
MapKeys::new(self.inner.as_ref()) MapKeys::new(self.inner.borrow())
} }
/// An iterator visiting all keys matching key. The /// An iterator visiting all keys matching key. The
/// iterator item type is `Result<Key<K>, MapError>`. /// iterator item type is `Result<Key<K>, MapError>`.
pub fn iter_key(&self, key: Key<K>) -> LpmTrieKeys<'_, K> { pub fn iter_key(&self, key: Key<K>) -> LpmTrieKeys<'_, K> {
LpmTrieKeys::new(self.inner.as_ref(), key) LpmTrieKeys::new(self.inner.borrow(), key)
} }
} }
impl<T: AsMut<MapData>, K: Pod, V: Pod> LpmTrie<T, K, V> { impl<T: BorrowMut<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( pub fn insert(
&mut self, &mut self,
@ -152,7 +151,7 @@ impl<T: AsMut<MapData>, K: Pod, V: Pod> LpmTrie<T, K, V> {
value: impl Borrow<V>, value: impl Borrow<V>,
flags: u64, flags: u64,
) -> Result<(), MapError> { ) -> Result<(), MapError> {
let fd = self.inner.as_mut().fd_or_err()?; let fd = self.inner.borrow().fd_or_err()?;
bpf_map_update_elem(fd, Some(key), value.borrow(), 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(),
@ -167,7 +166,7 @@ impl<T: AsMut<MapData>, K: Pod, V: Pod> LpmTrie<T, K, V> {
/// ///
/// Both the prefix and data must match exactly - this method does not do a longest prefix match. /// Both the prefix and data must match exactly - this method does not do a longest prefix match.
pub fn remove(&mut self, key: &Key<K>) -> Result<(), MapError> { pub fn remove(&mut self, key: &Key<K>) -> Result<(), MapError> {
let fd = self.inner.as_mut().fd_or_err()?; let fd = self.inner.borrow().fd_or_err()?;
bpf_map_delete_elem(fd, key) bpf_map_delete_elem(fd, key)
.map(|_| ()) .map(|_| ())
.map_err(|(_, io_error)| MapError::SyscallError { .map_err(|(_, io_error)| MapError::SyscallError {
@ -177,9 +176,9 @@ impl<T: AsMut<MapData>, K: Pod, V: Pod> LpmTrie<T, K, V> {
} }
} }
impl<T: AsRef<MapData>, K: Pod, V: Pod> IterableMap<Key<K>, V> for LpmTrie<T, K, V> { impl<T: Borrow<MapData>, K: Pod, V: Pod> IterableMap<Key<K>, V> for LpmTrie<T, K, V> {
fn map(&self) -> &MapData { fn map(&self) -> &MapData {
self.inner.as_ref() self.inner.borrow()
} }
fn get(&self, key: &Key<K>) -> Result<V, MapError> { fn get(&self, key: &Key<K>) -> Result<V, MapError> {

@ -37,7 +37,6 @@
//! versa. Because of that, all map values must be plain old data and therefore //! versa. Because of that, all map values must be plain old data and therefore
//! implement the [Pod] trait. //! implement the [Pod] trait.
use std::{ use std::{
convert::{AsMut, AsRef},
ffi::CString, ffi::CString,
fmt, io, fmt, io,
marker::PhantomData, marker::PhantomData,
@ -481,18 +480,6 @@ pub struct MapData {
pub pinned: bool, pub pinned: bool,
} }
impl AsRef<MapData> for MapData {
fn as_ref(&self) -> &MapData {
self
}
}
impl AsMut<MapData> for MapData {
fn as_mut(&mut self) -> &mut MapData {
self
}
}
impl MapData { impl MapData {
/// Creates a new map with the provided `name` /// Creates a new map with the provided `name`
pub fn create(&mut self, name: &str) -> Result<RawFd, MapError> { pub fn create(&mut self, name: &str) -> Result<RawFd, MapError> {

@ -1,6 +1,6 @@
use bytes::BytesMut; use bytes::BytesMut;
use std::{ use std::{
convert::AsMut, borrow::{Borrow, BorrowMut},
os::unix::prelude::{AsRawFd, RawFd}, os::unix::prelude::{AsRawFd, RawFd},
}; };
@ -89,7 +89,7 @@ pub struct AsyncPerfEventArray<T> {
perf_map: PerfEventArray<T>, perf_map: PerfEventArray<T>,
} }
impl<T: AsMut<MapData> + AsRef<MapData>> AsyncPerfEventArray<T> { impl<T: BorrowMut<MapData> + Borrow<MapData>> AsyncPerfEventArray<T> {
/// Opens the perf buffer at the given index. /// Opens the perf buffer at the given index.
/// ///
/// The returned buffer will receive all the events eBPF programs send at the given index. /// The returned buffer will receive all the events eBPF programs send at the given index.
@ -112,7 +112,7 @@ impl<T: AsMut<MapData> + AsRef<MapData>> AsyncPerfEventArray<T> {
} }
} }
impl<T: AsRef<MapData>> AsyncPerfEventArray<T> { impl<T: Borrow<MapData>> AsyncPerfEventArray<T> {
pub(crate) fn new(map: T) -> Result<AsyncPerfEventArray<T>, MapError> { pub(crate) fn new(map: T) -> Result<AsyncPerfEventArray<T>, MapError> {
Ok(AsyncPerfEventArray { Ok(AsyncPerfEventArray {
perf_map: PerfEventArray::new(map)?, perf_map: PerfEventArray::new(map)?,
@ -138,7 +138,7 @@ pub struct AsyncPerfEventArrayBuffer<T> {
} }
#[cfg(any(feature = "async_tokio"))] #[cfg(any(feature = "async_tokio"))]
impl<T: AsMut<MapData> + AsRef<MapData>> AsyncPerfEventArrayBuffer<T> { impl<T: BorrowMut<MapData> + Borrow<MapData>> AsyncPerfEventArrayBuffer<T> {
/// Reads events from the buffer. /// Reads events from the buffer.
/// ///
/// This method reads events into the provided slice of buffers, filling /// This method reads events into the provided slice of buffers, filling
@ -168,7 +168,7 @@ impl<T: AsMut<MapData> + AsRef<MapData>> AsyncPerfEventArrayBuffer<T> {
} }
#[cfg(all(not(feature = "async_tokio"), feature = "async_std"))] #[cfg(all(not(feature = "async_tokio"), feature = "async_std"))]
impl<T: AsMut<MapData> + AsRef<MapData>> AsyncPerfEventArrayBuffer<T> { impl<T: BorrowMut<MapData> + Borrow<MapData>> AsyncPerfEventArrayBuffer<T> {
/// Reads events from the buffer. /// Reads events from the buffer.
/// ///
/// This method reads events into the provided slice of buffers, filling /// This method reads events into the provided slice of buffers, filling

@ -2,7 +2,7 @@
//! //!
//! [`perf`]: https://perf.wiki.kernel.org/index.php/Main_Page. //! [`perf`]: https://perf.wiki.kernel.org/index.php/Main_Page.
use std::{ use std::{
convert::AsMut, borrow::{Borrow, BorrowMut},
ops::Deref, ops::Deref,
os::unix::io::{AsRawFd, RawFd}, os::unix::io::{AsRawFd, RawFd},
sync::Arc, sync::Arc,
@ -31,7 +31,7 @@ pub struct PerfEventArrayBuffer<T> {
buf: PerfBuffer, buf: PerfBuffer,
} }
impl<T: AsMut<MapData> + AsRef<MapData>> PerfEventArrayBuffer<T> { impl<T: BorrowMut<MapData> + Borrow<MapData>> PerfEventArrayBuffer<T> {
/// Returns true if the buffer contains events that haven't been read. /// Returns true if the buffer contains events that haven't been read.
pub fn readable(&self) -> bool { pub fn readable(&self) -> bool {
self.buf.readable() self.buf.readable()
@ -55,7 +55,7 @@ impl<T: AsMut<MapData> + AsRef<MapData>> PerfEventArrayBuffer<T> {
} }
} }
impl<T: AsMut<MapData> + AsRef<MapData>> AsRawFd for PerfEventArrayBuffer<T> { impl<T: BorrowMut<MapData> + Borrow<MapData>> AsRawFd for PerfEventArrayBuffer<T> {
fn as_raw_fd(&self) -> RawFd { fn as_raw_fd(&self) -> RawFd {
self.buf.as_raw_fd() self.buf.as_raw_fd()
} }
@ -84,14 +84,14 @@ impl<T: AsMut<MapData> + AsRef<MapData>> AsRawFd for PerfEventArrayBuffer<T> {
/// ```no_run /// ```no_run
/// # use aya::maps::perf::PerfEventArrayBuffer; /// # use aya::maps::perf::PerfEventArrayBuffer;
/// # use aya::maps::MapData; /// # use aya::maps::MapData;
/// # use std::convert::AsMut; /// # use std::borrow::BorrowMut;
/// # struct Poll<T> { _t: std::marker::PhantomData<T> }; /// # struct Poll<T> { _t: std::marker::PhantomData<T> };
/// # impl<T: AsMut<MapData>> Poll<T> { /// # impl<T: BorrowMut<MapData>> Poll<T> {
/// # fn poll_readable(&self) -> &mut [PerfEventArrayBuffer<T>] { /// # fn poll_readable(&self) -> &mut [PerfEventArrayBuffer<T>] {
/// # &mut [] /// # &mut []
/// # } /// # }
/// # } /// # }
/// # fn poll_buffers<T: AsMut<MapData>>(bufs: Vec<PerfEventArrayBuffer<T>>) -> Poll<T> { /// # fn poll_buffers<T: BorrowMut<MapData>>(bufs: Vec<PerfEventArrayBuffer<T>>) -> Poll<T> {
/// # Poll { _t: std::marker::PhantomData } /// # Poll { _t: std::marker::PhantomData }
/// # } /// # }
/// # #[derive(thiserror::Error, Debug)] /// # #[derive(thiserror::Error, Debug)]
@ -160,9 +160,9 @@ pub struct PerfEventArray<T> {
page_size: usize, page_size: usize,
} }
impl<T: AsRef<MapData>> PerfEventArray<T> { impl<T: Borrow<MapData>> PerfEventArray<T> {
pub(crate) fn new(map: T) -> Result<PerfEventArray<T>, MapError> { pub(crate) fn new(map: T) -> Result<PerfEventArray<T>, MapError> {
let _fd = map.as_ref().fd_or_err()?; let _fd = map.borrow().fd_or_err()?;
Ok(PerfEventArray { Ok(PerfEventArray {
map: Arc::new(map), map: Arc::new(map),
@ -171,7 +171,7 @@ impl<T: AsRef<MapData>> PerfEventArray<T> {
} }
} }
impl<T: AsMut<MapData> + AsRef<MapData>> PerfEventArray<T> { impl<T: BorrowMut<MapData> + Borrow<MapData>> PerfEventArray<T> {
/// Opens the perf buffer at the given index. /// Opens the perf buffer at the given index.
/// ///
/// The returned buffer will receive all the events eBPF programs send at the given index. /// The returned buffer will receive all the events eBPF programs send at the given index.
@ -183,7 +183,7 @@ impl<T: AsMut<MapData> + AsRef<MapData>> PerfEventArray<T> {
// FIXME: keep track of open buffers // FIXME: keep track of open buffers
// this cannot fail as new() checks that the fd is open // this cannot fail as new() checks that the fd is open
let map_data: &MapData = self.map.deref().as_ref(); let map_data: &MapData = self.map.deref().borrow();
let map_fd = map_data.fd_or_err().unwrap(); let map_fd = map_data.fd_or_err().unwrap();
let buf = PerfBuffer::open(index, self.page_size, page_count.unwrap_or(2))?; let buf = PerfBuffer::open(index, self.page_size, page_count.unwrap_or(2))?;
bpf_map_update_elem(map_fd, Some(&index), &buf.as_raw_fd(), 0) bpf_map_update_elem(map_fd, Some(&index), &buf.as_raw_fd(), 0)

@ -1,7 +1,6 @@
//! A FIFO queue. //! A FIFO queue.
use std::{ use std::{
borrow::Borrow, borrow::{Borrow, BorrowMut},
convert::{AsMut, AsRef},
marker::PhantomData, marker::PhantomData,
}; };
@ -34,9 +33,9 @@ pub struct Queue<T, V: Pod> {
_v: PhantomData<V>, _v: PhantomData<V>,
} }
impl<T: AsRef<MapData>, V: Pod> Queue<T, V> { impl<T: Borrow<MapData>, V: Pod> Queue<T, V> {
pub(crate) fn new(map: T) -> Result<Queue<T, V>, MapError> { pub(crate) fn new(map: T) -> Result<Queue<T, V>, MapError> {
let data = map.as_ref(); let data = map.borrow();
check_kv_size::<(), V>(data)?; check_kv_size::<(), V>(data)?;
let _fd = data.fd_or_err()?; let _fd = data.fd_or_err()?;
@ -51,11 +50,11 @@ impl<T: AsRef<MapData>, V: Pod> Queue<T, V> {
/// ///
/// This corresponds to the value of `bpf_map_def::max_entries` on the eBPF side. /// This corresponds to the value of `bpf_map_def::max_entries` on the eBPF side.
pub fn capacity(&self) -> u32 { pub fn capacity(&self) -> u32 {
self.inner.as_ref().obj.max_entries() self.inner.borrow().obj.max_entries()
} }
} }
impl<T: AsMut<MapData>, V: Pod> Queue<T, V> { impl<T: BorrowMut<MapData>, V: Pod> Queue<T, V> {
/// Removes the first element and returns it. /// Removes the first element and returns it.
/// ///
/// # Errors /// # Errors
@ -63,7 +62,7 @@ impl<T: AsMut<MapData>, V: Pod> Queue<T, V> {
/// Returns [`MapError::ElementNotFound`] if the queue is empty, [`MapError::SyscallError`] /// Returns [`MapError::ElementNotFound`] if the queue is empty, [`MapError::SyscallError`]
/// if `bpf_map_lookup_and_delete_elem` fails. /// if `bpf_map_lookup_and_delete_elem` fails.
pub fn pop(&mut self, flags: u64) -> Result<V, MapError> { pub fn pop(&mut self, flags: u64) -> Result<V, MapError> {
let fd = self.inner.as_mut().fd_or_err()?; let fd = self.inner.borrow().fd_or_err()?;
let value = bpf_map_lookup_and_delete_elem::<u32, _>(fd, None, flags).map_err( let value = bpf_map_lookup_and_delete_elem::<u32, _>(fd, None, flags).map_err(
|(_, io_error)| MapError::SyscallError { |(_, io_error)| MapError::SyscallError {
@ -80,7 +79,7 @@ impl<T: AsMut<MapData>, V: Pod> Queue<T, V> {
/// ///
/// [`MapError::SyscallError`] if `bpf_map_update_elem` fails. /// [`MapError::SyscallError`] if `bpf_map_update_elem` fails.
pub fn push(&mut self, value: impl Borrow<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.borrow().fd_or_err()?;
bpf_map_push_elem(fd, value.borrow(), flags).map_err(|(_, io_error)| { bpf_map_push_elem(fd, value.borrow(), flags).map_err(|(_, io_error)| {
MapError::SyscallError { MapError::SyscallError {
call: "bpf_map_push_elem".to_owned(), call: "bpf_map_push_elem".to_owned(),

@ -1,6 +1,5 @@
use std::{ use std::{
borrow::Borrow, borrow::{Borrow, BorrowMut},
convert::{AsMut, AsRef},
marker::PhantomData, marker::PhantomData,
os::unix::io::{AsRawFd, RawFd}, os::unix::io::{AsRawFd, RawFd},
}; };
@ -69,9 +68,9 @@ pub struct SockHash<T, K> {
_k: PhantomData<K>, _k: PhantomData<K>,
} }
impl<T: AsRef<MapData>, K: Pod> SockHash<T, K> { impl<T: Borrow<MapData>, K: Pod> SockHash<T, K> {
pub(crate) fn new(map: T) -> Result<SockHash<T, K>, MapError> { pub(crate) fn new(map: T) -> Result<SockHash<T, K>, MapError> {
let data = map.as_ref(); let data = map.borrow();
check_kv_size::<K, u32>(data)?; check_kv_size::<K, u32>(data)?;
let _ = data.fd_or_err()?; let _ = data.fd_or_err()?;
@ -83,7 +82,7 @@ impl<T: AsRef<MapData>, K: Pod> SockHash<T, K> {
/// Returns the fd of the socket stored at the given key. /// Returns the fd of the socket stored at the given key.
pub fn get(&self, key: &K, flags: u64) -> Result<RawFd, MapError> { pub fn get(&self, key: &K, flags: u64) -> Result<RawFd, MapError> {
let fd = self.inner.as_ref().fd_or_err()?; let fd = self.inner.borrow().fd_or_err()?;
let value = bpf_map_lookup_elem(fd, key, flags).map_err(|(_, io_error)| { let value = bpf_map_lookup_elem(fd, key, flags).map_err(|(_, io_error)| {
MapError::SyscallError { MapError::SyscallError {
call: "bpf_map_lookup_elem".to_owned(), call: "bpf_map_lookup_elem".to_owned(),
@ -102,7 +101,7 @@ impl<T: AsRef<MapData>, K: Pod> SockHash<T, K> {
/// An iterator visiting all keys in arbitrary order. The iterator element /// An iterator visiting all keys in arbitrary order. The iterator element
/// type is `Result<K, MapError>`. /// type is `Result<K, MapError>`.
pub fn keys(&self) -> MapKeys<'_, K> { pub fn keys(&self) -> MapKeys<'_, K> {
MapKeys::new(self.inner.as_ref()) MapKeys::new(self.inner.borrow())
} }
/// Returns the map's file descriptor. /// Returns the map's file descriptor.
@ -110,11 +109,11 @@ impl<T: AsRef<MapData>, K: Pod> SockHash<T, K> {
/// The returned file descriptor can be used to attach programs that work with /// The returned file descriptor can be used to attach programs that work with
/// socket maps, like [`SkMsg`](crate::programs::SkMsg) and [`SkSkb`](crate::programs::SkSkb). /// socket maps, like [`SkMsg`](crate::programs::SkMsg) and [`SkSkb`](crate::programs::SkSkb).
pub fn fd(&self) -> Result<SockMapFd, MapError> { pub fn fd(&self) -> Result<SockMapFd, MapError> {
Ok(SockMapFd(self.inner.as_ref().fd_or_err()?)) Ok(SockMapFd(self.inner.borrow().fd_or_err()?))
} }
} }
impl<T: AsMut<MapData>, K: Pod> SockHash<T, K> { impl<T: BorrowMut<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>( pub fn insert<I: AsRawFd>(
&mut self, &mut self,
@ -122,18 +121,23 @@ impl<T: AsMut<MapData>, K: Pod> SockHash<T, K> {
value: I, value: I,
flags: u64, flags: u64,
) -> Result<(), MapError> { ) -> Result<(), MapError> {
hash_map::insert(self.inner.as_mut(), key.borrow(), &value.as_raw_fd(), flags) hash_map::insert(
self.inner.borrow_mut(),
key.borrow(),
&value.as_raw_fd(),
flags,
)
} }
/// Removes a socket from the map. /// Removes a socket from the map.
pub fn remove(&mut self, key: &K) -> Result<(), MapError> { pub fn remove(&mut self, key: &K) -> Result<(), MapError> {
hash_map::remove(self.inner.as_mut(), key) hash_map::remove(self.inner.borrow_mut(), key)
} }
} }
impl<T: AsRef<MapData>, K: Pod> IterableMap<K, RawFd> for SockHash<T, K> { impl<T: Borrow<MapData>, K: Pod> IterableMap<K, RawFd> for SockHash<T, K> {
fn map(&self) -> &MapData { fn map(&self) -> &MapData {
self.inner.as_ref() self.inner.borrow()
} }
fn get(&self, key: &K) -> Result<RawFd, MapError> { fn get(&self, key: &K) -> Result<RawFd, MapError> {

@ -1,7 +1,7 @@
//! An array of eBPF program file descriptors used as a jump table. //! An array of eBPF program file descriptors used as a jump table.
use std::{ use std::{
convert::{AsMut, AsRef}, borrow::{Borrow, BorrowMut},
os::unix::{io::AsRawFd, prelude::RawFd}, os::unix::{io::AsRawFd, prelude::RawFd},
}; };
@ -44,9 +44,9 @@ pub struct SockMap<T> {
pub(crate) inner: T, pub(crate) inner: T,
} }
impl<T: AsRef<MapData>> SockMap<T> { impl<T: Borrow<MapData>> SockMap<T> {
pub(crate) fn new(map: T) -> Result<SockMap<T>, MapError> { pub(crate) fn new(map: T) -> Result<SockMap<T>, MapError> {
let data = map.as_ref(); let data = map.borrow();
check_kv_size::<u32, RawFd>(data)?; check_kv_size::<u32, RawFd>(data)?;
let _fd = data.fd_or_err()?; let _fd = data.fd_or_err()?;
@ -57,7 +57,7 @@ impl<T: AsRef<MapData>> SockMap<T> {
/// An iterator over the indices of the array that point to a program. The iterator item type /// An iterator over the indices of the array that point to a program. The iterator item type
/// is `Result<u32, MapError>`. /// is `Result<u32, MapError>`.
pub fn indices(&self) -> MapKeys<'_, u32> { pub fn indices(&self) -> MapKeys<'_, u32> {
MapKeys::new(self.inner.as_ref()) MapKeys::new(self.inner.borrow())
} }
/// Returns the map's file descriptor. /// Returns the map's file descriptor.
@ -65,14 +65,14 @@ impl<T: AsRef<MapData>> SockMap<T> {
/// The returned file descriptor can be used to attach programs that work with /// The returned file descriptor can be used to attach programs that work with
/// socket maps, like [`SkMsg`](crate::programs::SkMsg) and [`SkSkb`](crate::programs::SkSkb). /// socket maps, like [`SkMsg`](crate::programs::SkMsg) and [`SkSkb`](crate::programs::SkSkb).
pub fn fd(&self) -> Result<SockMapFd, MapError> { pub fn fd(&self) -> Result<SockMapFd, MapError> {
Ok(SockMapFd(self.inner.as_ref().fd_or_err()?)) Ok(SockMapFd(self.inner.borrow().fd_or_err()?))
} }
} }
impl<T: AsMut<MapData>> SockMap<T> { impl<T: BorrowMut<MapData>> SockMap<T> {
/// Stores a socket into the map. /// Stores a socket into the map.
pub fn set<I: AsRawFd>(&mut self, index: u32, socket: &I, flags: u64) -> Result<(), MapError> { pub fn set<I: AsRawFd>(&mut self, index: u32, socket: &I, flags: u64) -> Result<(), MapError> {
let data = self.inner.as_mut(); let data = self.inner.borrow_mut();
let fd = data.fd_or_err()?; let fd = data.fd_or_err()?;
check_bounds(data, index)?; check_bounds(data, index)?;
bpf_map_update_elem(fd, Some(&index), &socket.as_raw_fd(), flags).map_err( bpf_map_update_elem(fd, Some(&index), &socket.as_raw_fd(), flags).map_err(
@ -86,7 +86,7 @@ impl<T: AsMut<MapData>> SockMap<T> {
/// Removes the socket stored at `index` from the map. /// Removes the socket stored at `index` from the map.
pub fn clear_index(&mut self, index: &u32) -> Result<(), MapError> { pub fn clear_index(&mut self, index: &u32) -> Result<(), MapError> {
let data = self.inner.as_mut(); let data = self.inner.borrow_mut();
let fd = data.fd_or_err()?; let fd = data.fd_or_err()?;
check_bounds(data, *index)?; check_bounds(data, *index)?;
bpf_map_delete_elem(fd, index) bpf_map_delete_elem(fd, index)

@ -1,7 +1,6 @@
//! A LIFO stack. //! A LIFO stack.
use std::{ use std::{
borrow::Borrow, borrow::{Borrow, BorrowMut},
convert::{AsMut, AsRef},
marker::PhantomData, marker::PhantomData,
}; };
@ -34,9 +33,9 @@ pub struct Stack<T, V: Pod> {
_v: PhantomData<V>, _v: PhantomData<V>,
} }
impl<T: AsRef<MapData>, V: Pod> Stack<T, V> { impl<T: Borrow<MapData>, V: Pod> Stack<T, V> {
pub(crate) fn new(map: T) -> Result<Stack<T, V>, MapError> { pub(crate) fn new(map: T) -> Result<Stack<T, V>, MapError> {
let data = map.as_ref(); let data = map.borrow();
check_kv_size::<(), V>(data)?; check_kv_size::<(), V>(data)?;
let _fd = data.fd_or_err()?; let _fd = data.fd_or_err()?;
@ -51,11 +50,11 @@ impl<T: AsRef<MapData>, V: Pod> Stack<T, V> {
/// ///
/// This corresponds to the value of `bpf_map_def::max_entries` on the eBPF side. /// This corresponds to the value of `bpf_map_def::max_entries` on the eBPF side.
pub fn capacity(&self) -> u32 { pub fn capacity(&self) -> u32 {
self.inner.as_ref().obj.max_entries() self.inner.borrow().obj.max_entries()
} }
} }
impl<T: AsMut<MapData>, V: Pod> Stack<T, V> { impl<T: BorrowMut<MapData>, V: Pod> Stack<T, V> {
/// Removes the last element and returns it. /// Removes the last element and returns it.
/// ///
/// # Errors /// # Errors
@ -63,7 +62,7 @@ impl<T: AsMut<MapData>, V: Pod> Stack<T, V> {
/// Returns [`MapError::ElementNotFound`] if the stack is empty, [`MapError::SyscallError`] /// Returns [`MapError::ElementNotFound`] if the stack is empty, [`MapError::SyscallError`]
/// if `bpf_map_lookup_and_delete_elem` fails. /// if `bpf_map_lookup_and_delete_elem` fails.
pub fn pop(&mut self, flags: u64) -> Result<V, MapError> { pub fn pop(&mut self, flags: u64) -> Result<V, MapError> {
let fd = self.inner.as_mut().fd_or_err()?; let fd = self.inner.borrow().fd_or_err()?;
let value = bpf_map_lookup_and_delete_elem::<u32, _>(fd, None, flags).map_err( let value = bpf_map_lookup_and_delete_elem::<u32, _>(fd, None, flags).map_err(
|(_, io_error)| MapError::SyscallError { |(_, io_error)| MapError::SyscallError {
@ -80,7 +79,7 @@ impl<T: AsMut<MapData>, V: Pod> Stack<T, V> {
/// ///
/// [`MapError::SyscallError`] if `bpf_map_update_elem` fails. /// [`MapError::SyscallError`] if `bpf_map_update_elem` fails.
pub fn push(&mut self, value: impl Borrow<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.borrow().fd_or_err()?;
bpf_map_update_elem(fd, None::<&u32>, value.borrow(), 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(),

@ -1,7 +1,7 @@
//! A hash map of kernel or user space stack traces. //! A hash map of kernel or user space stack traces.
//! //!
//! See [`StackTraceMap`] for documentation and examples. //! See [`StackTraceMap`] for documentation and examples.
use std::{collections::BTreeMap, convert::AsRef, fs, io, mem, path::Path, str::FromStr}; use std::{borrow::Borrow, collections::BTreeMap, fs, io, mem, path::Path, str::FromStr};
use crate::{ use crate::{
maps::{IterableMap, MapData, MapError, MapIter, MapKeys}, maps::{IterableMap, MapData, MapError, MapIter, MapKeys},
@ -67,9 +67,9 @@ pub struct StackTraceMap<T> {
max_stack_depth: usize, max_stack_depth: usize,
} }
impl<T: AsRef<MapData>> StackTraceMap<T> { impl<T: Borrow<MapData>> StackTraceMap<T> {
pub(crate) fn new(map: T) -> Result<StackTraceMap<T>, MapError> { pub(crate) fn new(map: T) -> Result<StackTraceMap<T>, MapError> {
let data = map.as_ref(); let data = map.borrow();
let expected = mem::size_of::<u32>(); let expected = mem::size_of::<u32>();
let size = data.obj.key_size() as usize; let size = data.obj.key_size() as usize;
if size != expected { if size != expected {
@ -102,7 +102,7 @@ impl<T: AsRef<MapData>> StackTraceMap<T> {
/// Returns [`MapError::KeyNotFound`] if there is no stack trace with the /// Returns [`MapError::KeyNotFound`] if there is no stack trace with the
/// given `stack_id`, or [`MapError::SyscallError`] if `bpf_map_lookup_elem` fails. /// given `stack_id`, or [`MapError::SyscallError`] if `bpf_map_lookup_elem` fails.
pub fn get(&self, stack_id: &u32, flags: u64) -> Result<StackTrace, MapError> { pub fn get(&self, stack_id: &u32, flags: u64) -> Result<StackTrace, MapError> {
let fd = self.inner.as_ref().fd_or_err()?; let fd = self.inner.borrow().fd_or_err()?;
let mut frames = vec![0; self.max_stack_depth]; let mut frames = vec![0; self.max_stack_depth];
bpf_map_lookup_elem_ptr(fd, Some(stack_id), frames.as_mut_ptr(), flags) bpf_map_lookup_elem_ptr(fd, Some(stack_id), frames.as_mut_ptr(), flags)
@ -136,13 +136,13 @@ impl<T: AsRef<MapData>> StackTraceMap<T> {
/// An iterator visiting all the stack_ids in arbitrary order. The iterator element /// An iterator visiting all the stack_ids in arbitrary order. The iterator element
/// type is `Result<u32, MapError>`. /// type is `Result<u32, MapError>`.
pub fn stack_ids(&self) -> MapKeys<'_, u32> { pub fn stack_ids(&self) -> MapKeys<'_, u32> {
MapKeys::new(self.inner.as_ref()) MapKeys::new(self.inner.borrow())
} }
} }
impl<T: AsRef<MapData>> IterableMap<u32, StackTrace> for StackTraceMap<T> { impl<T: Borrow<MapData>> IterableMap<u32, StackTrace> for StackTraceMap<T> {
fn map(&self) -> &MapData { fn map(&self) -> &MapData {
self.inner.as_ref() self.inner.borrow()
} }
fn get(&self, index: &u32) -> Result<StackTrace, MapError> { fn get(&self, index: &u32) -> Result<StackTrace, MapError> {
@ -150,7 +150,7 @@ impl<T: AsRef<MapData>> IterableMap<u32, StackTrace> for StackTraceMap<T> {
} }
} }
impl<'a, T: AsRef<MapData>> IntoIterator for &'a StackTraceMap<T> { impl<'a, T: Borrow<MapData>> IntoIterator for &'a StackTraceMap<T> {
type Item = Result<(u32, StackTrace), MapError>; type Item = Result<(u32, StackTrace), MapError>;
type IntoIter = MapIter<'a, u32, StackTrace, StackTraceMap<T>>; type IntoIter = MapIter<'a, u32, StackTrace, StackTraceMap<T>>;

Loading…
Cancel
Save