maps: use shared helpers

reviewable/pr1357/r7
Tamir Duberstein 1 month ago
parent 34c5328dcf
commit c5328305a1
No known key found for this signature in database

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

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

@ -1,13 +1,11 @@
use std::{ use std::{
borrow::{Borrow, BorrowMut}, borrow::{Borrow, BorrowMut},
marker::PhantomData, marker::PhantomData,
os::fd::AsFd as _,
}; };
use crate::{ use crate::{
Pod, Pod,
maps::{IterableMap, MapData, MapError, MapIter, MapKeys, check_kv_size, hash_map}, 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. /// 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. /// 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.borrow().fd().as_fd(); hash_map::get(self.inner.borrow(), key, flags)
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)
} }
/// An iterator visiting all key-value pairs in arbitrary order. The /// An iterator visiting all key-value pairs in arbitrary order. The
@ -118,7 +111,7 @@ mod tests {
Map, Map,
test_utils::{self, new_map}, test_utils::{self, new_map},
}, },
sys::{SysResult, Syscall, override_syscall}, sys::{SysResult, Syscall, SyscallError, override_syscall},
}; };
fn new_obj_map() -> aya_obj::Map { fn new_obj_map() -> aya_obj::Map {

@ -4,7 +4,7 @@ use std::os::fd::AsFd as _;
use crate::{ use crate::{
Pod, Pod,
maps::MapError, 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)] #[expect(clippy::module_inception)]
@ -16,6 +16,15 @@ pub use per_cpu_hash_map::*;
use super::MapData; 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>( pub(crate) fn insert<K: Pod, V: Pod>(
map: &MapData, map: &MapData,
key: &K, key: &K,

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

@ -1,7 +1,7 @@
use std::{ use std::{
borrow::{Borrow, BorrowMut}, borrow::{Borrow, BorrowMut},
marker::PhantomData, marker::PhantomData,
os::fd::{AsFd as _, AsRawFd, RawFd}, os::fd::{AsRawFd, RawFd},
}; };
use crate::{ use crate::{
@ -10,7 +10,6 @@ use crate::{
IterableMap, MapData, MapError, MapFd, MapIter, MapKeys, check_kv_size, hash_map, IterableMap, MapData, MapError, MapFd, MapIter, MapKeys, check_kv_size, hash_map,
sock::SockMapFd, sock::SockMapFd,
}, },
sys::{SyscallError, bpf_map_lookup_elem},
}; };
/// A hash map of TCP or UDP sockets. /// 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. /// 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.borrow().fd().as_fd(); hash_map::get(self.inner.borrow(), key, flags)
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)
} }
/// An iterator visiting all key-value pairs in arbitrary order. The /// An iterator visiting all key-value pairs in arbitrary order. The

@ -2,12 +2,11 @@
use std::{ use std::{
borrow::{Borrow, BorrowMut}, borrow::{Borrow, BorrowMut},
os::fd::{AsFd as _, AsRawFd, RawFd}, os::fd::{AsRawFd, RawFd},
}; };
use crate::{ use crate::maps::{
maps::{MapData, MapError, MapFd, MapKeys, check_bounds, check_kv_size, sock::SockMapFd}, MapData, MapError, MapFd, MapKeys, check_bounds, check_kv_size, hash_map, sock::SockMapFd,
sys::{SyscallError, bpf_map_delete_elem, bpf_map_update_elem},
}; };
/// An array of TCP or UDP sockets. /// An array of TCP or UDP sockets.
@ -85,26 +84,14 @@ 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.borrow_mut(); let data = self.inner.borrow_mut();
let fd = data.fd().as_fd();
check_bounds(data, index)?; check_bounds(data, index)?;
bpf_map_update_elem(fd, Some(&index), &socket.as_raw_fd(), flags) hash_map::insert(data, &index, &socket.as_raw_fd(), flags)
.map_err(|io_error| SyscallError {
call: "bpf_map_update_elem",
io_error,
})
.map_err(Into::into)
} }
/// 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.borrow_mut(); let data = self.inner.borrow_mut();
let fd = data.fd().as_fd();
check_bounds(data, *index)?; check_bounds(data, *index)?;
bpf_map_delete_elem(fd, index) hash_map::remove(data, index)
.map_err(|io_error| SyscallError {
call: "bpf_map_delete_elem",
io_error,
})
.map_err(Into::into)
} }
} }

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

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

Loading…
Cancel
Save