maps: use shared helpers

reviewable/pr1357/r17
Tamir Duberstein 2 weeks ago
parent c9b5f119a3
commit 6babf17969
No known key found for this signature in database

@ -1,13 +1,11 @@
use std::{
borrow::{Borrow, BorrowMut},
marker::PhantomData,
os::fd::AsFd as _,
};
use crate::{
Pod,
maps::{IterableMap, MapData, MapError, check_bounds, check_kv_size},
sys::{SyscallError, bpf_map_lookup_elem, bpf_map_update_elem},
maps::{IterableMap, MapData, MapError, check_bounds, check_kv_size, hash_map},
};
/// A fixed-size array.
@ -63,13 +61,7 @@ impl<T: Borrow<MapData>, V: Pod> Array<T, V> {
pub fn get(&self, index: &u32, flags: u64) -> Result<V, MapError> {
let data = self.inner.borrow();
check_bounds(data, *index)?;
let fd = data.fd().as_fd();
let value = bpf_map_lookup_elem(fd, index, flags).map_err(|io_error| SyscallError {
call: "bpf_map_lookup_elem",
io_error,
})?;
value.ok_or(MapError::KeyNotFound)
hash_map::get(data, index, flags)
}
/// An iterator over the elements of the array. The iterator item type is `Result<V,
@ -89,14 +81,7 @@ impl<T: BorrowMut<MapData>, V: Pod> Array<T, V> {
pub fn set(&mut self, index: u32, value: impl Borrow<V>, flags: u64) -> Result<(), MapError> {
let data = self.inner.borrow_mut();
check_bounds(data, index)?;
let fd = data.fd().as_fd();
bpf_map_update_elem(fd, Some(&index), value.borrow(), flags).map_err(|io_error| {
SyscallError {
call: "bpf_map_update_elem",
io_error,
}
})?;
Ok(())
hash_map::insert(data, &index, value.borrow(), flags)
}
}

@ -6,9 +6,8 @@ use std::{
};
use crate::{
maps::{MapData, MapError, MapKeys, check_bounds, check_kv_size},
maps::{MapData, MapError, MapKeys, check_bounds, check_kv_size, hash_map},
programs::ProgramFd,
sys::{SyscallError, bpf_map_delete_elem, bpf_map_update_elem},
};
/// An array of eBPF program file descriptors used as a jump table.
@ -74,16 +73,7 @@ impl<T: BorrowMut<MapData>> ProgramArray<T> {
pub fn set(&mut self, index: u32, program: &ProgramFd, flags: u64) -> Result<(), MapError> {
let data = self.inner.borrow_mut();
check_bounds(data, index)?;
let fd = data.fd().as_fd();
let prog_fd = program.as_fd();
let prog_fd = prog_fd.as_raw_fd();
bpf_map_update_elem(fd, Some(&index), &prog_fd, flags)
.map_err(|io_error| SyscallError {
call: "bpf_map_update_elem",
io_error,
})
.map_err(Into::into)
hash_map::insert(data, &index, &program.as_fd().as_raw_fd(), flags)
}
/// Clears the value at index in the jump table.
@ -93,13 +83,6 @@ impl<T: BorrowMut<MapData>> ProgramArray<T> {
pub fn clear_index(&mut self, index: &u32) -> Result<(), MapError> {
let data = self.inner.borrow_mut();
check_bounds(data, *index)?;
let fd = data.fd().as_fd();
bpf_map_delete_elem(fd, index)
.map_err(|io_error| SyscallError {
call: "bpf_map_delete_elem",
io_error,
})
.map_err(Into::into)
hash_map::remove(data, index)
}
}

@ -1,13 +1,11 @@
use std::{
borrow::{Borrow, BorrowMut},
marker::PhantomData,
os::fd::AsFd as _,
};
use crate::{
Pod,
maps::{IterableMap, MapData, MapError, MapIter, MapKeys, check_kv_size, hash_map},
sys::{SyscallError, bpf_map_lookup_elem},
};
/// A hash map that can be shared between eBPF programs and user space.
@ -53,12 +51,7 @@ impl<T: Borrow<MapData>, K: Pod, V: Pod> HashMap<T, K, V> {
/// Returns a copy of the value associated with the key.
pub fn get(&self, key: &K, flags: u64) -> Result<V, MapError> {
let fd = self.inner.borrow().fd().as_fd();
let value = bpf_map_lookup_elem(fd, key, flags).map_err(|io_error| SyscallError {
call: "bpf_map_lookup_elem",
io_error,
})?;
value.ok_or(MapError::KeyNotFound)
hash_map::get(self.inner.borrow(), key, flags)
}
/// An iterator visiting all key-value pairs in arbitrary order. The
@ -118,7 +111,7 @@ mod tests {
Map,
test_utils::{self, new_map},
},
sys::{SysResult, Syscall, override_syscall},
sys::{SysResult, Syscall, SyscallError, override_syscall},
};
fn new_obj_map() -> aya_obj::Map {

@ -4,7 +4,7 @@ use std::os::fd::AsFd as _;
use crate::{
Pod,
maps::MapError,
sys::{SyscallError, bpf_map_delete_elem, bpf_map_update_elem},
sys::{SyscallError, bpf_map_delete_elem, bpf_map_lookup_elem, bpf_map_update_elem},
};
#[expect(clippy::module_inception)]
@ -16,6 +16,15 @@ pub use per_cpu_hash_map::*;
use super::MapData;
pub(crate) fn get<K: Pod, V: Pod>(map: &MapData, key: &K, flags: u64) -> Result<V, MapError> {
let fd = map.fd().as_fd();
let value = bpf_map_lookup_elem(fd, key, flags).map_err(|io_error| SyscallError {
call: "bpf_map_lookup_elem",
io_error,
})?;
value.ok_or(MapError::KeyNotFound)
}
pub(crate) fn insert<K: Pod, V: Pod>(
map: &MapData,
key: &K,

@ -3,13 +3,11 @@
use std::{
borrow::{Borrow, BorrowMut},
marker::PhantomData,
os::fd::AsFd as _,
};
use crate::{
Pod,
maps::{IterableMap, MapData, MapError, MapIter, MapKeys, check_kv_size},
sys::{SyscallError, bpf_map_delete_elem, bpf_map_lookup_elem, bpf_map_update_elem},
maps::{IterableMap, MapData, MapError, MapIter, MapKeys, check_kv_size, hash_map},
};
/// A Longest Prefix Match Trie.
@ -120,12 +118,7 @@ impl<T: Borrow<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.
pub fn get(&self, key: &Key<K>, flags: u64) -> Result<V, MapError> {
let fd = self.inner.borrow().fd().as_fd();
let value = bpf_map_lookup_elem(fd, key, flags).map_err(|io_error| SyscallError {
call: "bpf_map_lookup_elem",
io_error,
})?;
value.ok_or(MapError::KeyNotFound)
hash_map::get(self.inner.borrow(), key, flags)
}
/// An iterator visiting all key-value pairs. The
@ -149,26 +142,14 @@ impl<T: BorrowMut<MapData>, K: Pod, V: Pod> LpmTrie<T, K, V> {
value: impl Borrow<V>,
flags: u64,
) -> Result<(), MapError> {
let fd = self.inner.borrow().fd().as_fd();
bpf_map_update_elem(fd, Some(key), value.borrow(), flags)
.map_err(|io_error| SyscallError {
call: "bpf_map_update_elem",
io_error,
})
.map_err(Into::into)
hash_map::insert(self.inner.borrow_mut(), key, value.borrow(), flags)
}
/// Removes an element from the map.
///
/// 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> {
let fd = self.inner.borrow().fd().as_fd();
bpf_map_delete_elem(fd, key)
.map_err(|io_error| SyscallError {
call: "bpf_map_delete_elem",
io_error,
})
.map_err(Into::into)
hash_map::remove(self.inner.borrow_mut(), key)
}
}
@ -199,7 +180,7 @@ mod tests {
Map,
test_utils::{self, new_map},
},
sys::{SysResult, Syscall, override_syscall},
sys::{SysResult, Syscall, SyscallError, override_syscall},
};
fn new_obj_map() -> aya_obj::Map {

@ -1,7 +1,7 @@
use std::{
borrow::{Borrow, BorrowMut},
marker::PhantomData,
os::fd::{AsFd as _, AsRawFd, RawFd},
os::fd::{AsRawFd, RawFd},
};
use crate::{
@ -10,7 +10,6 @@ use crate::{
IterableMap, MapData, MapError, MapFd, MapIter, MapKeys, check_kv_size, hash_map,
sock::SockMapFd,
},
sys::{SyscallError, bpf_map_lookup_elem},
};
/// A hash map of TCP or UDP sockets.
@ -82,12 +81,7 @@ impl<T: Borrow<MapData>, K: Pod> SockHash<T, K> {
/// Returns the fd of the socket stored at the given key.
pub fn get(&self, key: &K, flags: u64) -> Result<RawFd, MapError> {
let fd = self.inner.borrow().fd().as_fd();
let value = bpf_map_lookup_elem(fd, key, flags).map_err(|io_error| SyscallError {
call: "bpf_map_lookup_elem",
io_error,
})?;
value.ok_or(MapError::KeyNotFound)
hash_map::get(self.inner.borrow(), key, flags)
}
/// An iterator visiting all key-value pairs in arbitrary order. The

@ -2,12 +2,11 @@
use std::{
borrow::{Borrow, BorrowMut},
os::fd::{AsFd as _, AsRawFd, RawFd},
os::fd::{AsRawFd, RawFd},
};
use crate::{
maps::{MapData, MapError, MapFd, MapKeys, check_bounds, check_kv_size, sock::SockMapFd},
sys::{SyscallError, bpf_map_delete_elem, bpf_map_update_elem},
use crate::maps::{
MapData, MapError, MapFd, MapKeys, check_bounds, check_kv_size, hash_map, sock::SockMapFd,
};
/// An array of TCP or UDP sockets.
@ -85,26 +84,14 @@ impl<T: BorrowMut<MapData>> SockMap<T> {
/// Stores a socket into the map.
pub fn set<I: AsRawFd>(&mut self, index: u32, socket: &I, flags: u64) -> Result<(), MapError> {
let data = self.inner.borrow_mut();
let fd = data.fd().as_fd();
check_bounds(data, index)?;
bpf_map_update_elem(fd, Some(&index), &socket.as_raw_fd(), flags)
.map_err(|io_error| SyscallError {
call: "bpf_map_update_elem",
io_error,
})
.map_err(Into::into)
hash_map::insert(data, &index, &socket.as_raw_fd(), flags)
}
/// Removes the socket stored at `index` from the map.
pub fn clear_index(&mut self, index: &u32) -> Result<(), MapError> {
let data = self.inner.borrow_mut();
let fd = data.fd().as_fd();
check_bounds(data, *index)?;
bpf_map_delete_elem(fd, index)
.map_err(|io_error| SyscallError {
call: "bpf_map_delete_elem",
io_error,
})
.map_err(Into::into)
hash_map::remove(data, index)
}
}

@ -11,8 +11,8 @@ use std::{
};
use crate::{
maps::{IterableMap, MapData, MapError, MapIter, MapKeys},
sys::{SyscallError, bpf_map_delete_elem, bpf_map_lookup_elem_ptr},
maps::{IterableMap, MapData, MapError, MapIter, MapKeys, hash_map},
sys::{SyscallError, bpf_map_lookup_elem_ptr},
};
/// A hash map of kernel or user space stack traces.
@ -167,13 +167,7 @@ impl<'a, T: Borrow<MapData>> IntoIterator for &'a StackTraceMap<T> {
impl<T: BorrowMut<MapData>> StackTraceMap<T> {
/// Removes the stack trace with the given stack_id.
pub fn remove(&mut self, stack_id: &u32) -> Result<(), MapError> {
let fd = self.inner.borrow().fd().as_fd();
bpf_map_delete_elem(fd, stack_id)
.map_err(|io_error| SyscallError {
call: "bpf_map_delete_elem",
io_error,
})
.map_err(Into::into)
hash_map::remove(self.inner.borrow_mut(), stack_id)
}
}

@ -2,13 +2,10 @@
use std::{
borrow::{Borrow, BorrowMut},
os::fd::{AsFd as _, AsRawFd, BorrowedFd, RawFd},
os::fd::{AsRawFd, RawFd},
};
use crate::{
maps::{MapData, MapError, check_bounds, check_kv_size},
sys::{SyscallError, bpf_map_delete_elem, bpf_map_update_elem},
};
use crate::maps::{MapData, MapError, check_bounds, check_kv_size, hash_map};
/// An array of AF_XDP sockets.
///
@ -57,16 +54,6 @@ impl<T: Borrow<MapData>> XskMap<T> {
}
impl<T: BorrowMut<MapData>> XskMap<T> {
fn with_fd(
&mut self,
index: u32,
f: impl FnOnce(BorrowedFd<'_>) -> Result<(), SyscallError>,
) -> Result<(), MapError> {
let data = self.inner.borrow_mut();
check_bounds(data, index)?;
f(data.fd().as_fd()).map_err(Into::into)
}
/// Sets the `AF_XDP` socket at a given index.
///
/// When redirecting a packet, the `AF_XDP` socket at `index` will recieve the packet. Note
@ -78,14 +65,9 @@ impl<T: BorrowMut<MapData>> XskMap<T> {
/// Returns [`MapError::OutOfBounds`] if `index` is out of bounds, [`MapError::SyscallError`]
/// if `bpf_map_update_elem` fails.
pub fn set(&mut self, index: u32, socket_fd: impl AsRawFd, flags: u64) -> Result<(), MapError> {
self.with_fd(index, |fd| {
bpf_map_update_elem(fd, Some(&index), &socket_fd.as_raw_fd(), flags).map_err(
|io_error| SyscallError {
call: "bpf_map_update_elem",
io_error,
},
)
})
let data = self.inner.borrow_mut();
check_bounds(data, index)?;
hash_map::insert(data, &index, &socket_fd.as_raw_fd(), flags)
}
/// Un-sets the `AF_XDP` socket at a given index.
@ -95,11 +77,8 @@ impl<T: BorrowMut<MapData>> XskMap<T> {
/// Returns [`MapError::OutOfBounds`] if `index` is out of bounds, [`MapError::SyscallError`]
/// if `bpf_map_delete_elem` fails.
pub fn unset(&mut self, index: u32) -> Result<(), MapError> {
self.with_fd(index, |fd| {
bpf_map_delete_elem(fd, &index).map_err(|io_error| SyscallError {
call: "bpf_map_delete_elem",
io_error,
})
})
let data = self.inner.borrow_mut();
check_bounds(data, index)?;
hash_map::remove(data, &index)
}
}

Loading…
Cancel
Save