aya: Add `bpf_map_delete_elem` for `XskMap`

pull/848/head
arctic-alpaca 1 year ago
parent 5075adc01d
commit 01a74db5bd
No known key found for this signature in database
GPG Key ID: 5556CF6C310CFACD

@ -7,7 +7,7 @@ use std::{
use crate::{ use crate::{
maps::{check_bounds, check_kv_size, MapData, MapError}, maps::{check_bounds, check_kv_size, MapData, MapError},
sys::{bpf_map_update_elem, SyscallError}, sys::{bpf_map_delete_elem, bpf_map_update_elem, SyscallError},
}; };
/// An array of AF_XDP sockets. /// An array of AF_XDP sockets.
@ -78,4 +78,23 @@ impl<T: BorrowMut<MapData>> XskMap<T> {
)?; )?;
Ok(()) Ok(())
} }
/// Removes the `AF_XDP` socket stored at `index` from the map.
///
/// Returns [`MapError::OutOfBounds`] if `index` is out of bounds, [`MapError::SyscallError`]
/// if `bpf_map_update_elem` fails.
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(|_| ())
.map_err(|(_, io_error)| {
SyscallError {
call: "bpf_map_delete_elem",
io_error,
}
.into()
})
}
} }

@ -29,7 +29,7 @@ fn af_xdp() {
// So this needs to be page aligned. Pages are 4k on all mainstream architectures except for // So this needs to be page aligned. Pages are 4k on all mainstream architectures except for
// Apple Silicon which uses 16k pages. So let's align on that for tests to run natively there. // Apple Silicon which uses 16k pages. So let's align on that for tests to run natively there.
#[repr(C, align(16384))] #[repr(C, align(16384))]
struct PacketMap(MaybeUninit<[u8; 4096]>); struct PacketMap(MaybeUninit<[u8; 2 * 4096]>);
// Safety: don't access alloc down the line. // Safety: don't access alloc down the line.
let mut alloc = Box::new(PacketMap(MaybeUninit::uninit())); let mut alloc = Box::new(PacketMap(MaybeUninit::uninit()));
@ -58,10 +58,12 @@ fn af_xdp() {
socks.set(0, rx.as_raw_fd(), 0).unwrap(); socks.set(0, rx.as_raw_fd(), 0).unwrap();
let frame = umem.frame(BufIdx(0)).unwrap(); let frame = umem.frame(BufIdx(0)).unwrap();
let frame1 = umem.frame(BufIdx(1)).unwrap();
// Produce a frame to be filled by the kernel // Produce two frames to be filled by the kernel
let mut writer = fq_cq.fill(1); let mut writer = fq_cq.fill(2);
writer.insert_once(frame.offset); writer.insert_once(frame.offset);
writer.insert_once(frame1.offset);
writer.commit(); writer.commit();
let sock = UdpSocket::bind("127.0.0.1:0").unwrap(); let sock = UdpSocket::bind("127.0.0.1:0").unwrap();
@ -82,6 +84,17 @@ fn af_xdp() {
assert_eq!(&udp[0..2], port.to_be_bytes().as_slice()); // Source assert_eq!(&udp[0..2], port.to_be_bytes().as_slice()); // Source
assert_eq!(&udp[2..4], 1777u16.to_be_bytes().as_slice()); // Dest assert_eq!(&udp[2..4], 1777u16.to_be_bytes().as_slice()); // Dest
assert_eq!(payload, b"hello AF_XDP"); assert_eq!(payload, b"hello AF_XDP");
assert_eq!(rx.available(), 1);
// Removes socket from map, no more packets will be redirected.
socks.clear_index(0).unwrap();
assert_eq!(rx.available(), 1);
sock.send_to(b"hello AF_XDP", "127.0.0.1:1777").unwrap();
assert_eq!(rx.available(), 1);
// Adds socket to map again, packets will be redirected again.
socks.set(0, rx.as_raw_fd(), 0).unwrap();
sock.send_to(b"hello AF_XDP", "127.0.0.1:1777").unwrap();
assert_eq!(rx.available(), 2);
} }
#[test] #[test]

@ -1083,6 +1083,7 @@ pub fn aya::maps::XskMap<T>::len(&self) -> u32
impl<T: core::borrow::Borrow<aya::maps::MapData>> aya::maps::XskMap<T> impl<T: core::borrow::Borrow<aya::maps::MapData>> aya::maps::XskMap<T>
pub fn aya::maps::XskMap<T>::pin<P: core::convert::AsRef<std::path::Path>>(self, path: P) -> core::result::Result<(), aya::pin::PinError> pub fn aya::maps::XskMap<T>::pin<P: core::convert::AsRef<std::path::Path>>(self, path: P) -> core::result::Result<(), aya::pin::PinError>
impl<T: core::borrow::BorrowMut<aya::maps::MapData>> aya::maps::XskMap<T> impl<T: core::borrow::BorrowMut<aya::maps::MapData>> aya::maps::XskMap<T>
pub fn aya::maps::XskMap<T>::clear_index(&mut self, index: u32) -> core::result::Result<(), aya::maps::MapError>
pub fn aya::maps::XskMap<T>::set(&mut self, index: u32, socket_fd: impl std::os::fd::raw::AsRawFd, flags: u64) -> core::result::Result<(), aya::maps::MapError> pub fn aya::maps::XskMap<T>::set(&mut self, index: u32, socket_fd: impl std::os::fd::raw::AsRawFd, flags: u64) -> core::result::Result<(), aya::maps::MapError>
impl core::convert::TryFrom<aya::maps::Map> for aya::maps::XskMap<aya::maps::MapData> impl core::convert::TryFrom<aya::maps::Map> for aya::maps::XskMap<aya::maps::MapData>
pub type aya::maps::XskMap<aya::maps::MapData>::Error = aya::maps::MapError pub type aya::maps::XskMap<aya::maps::MapData>::Error = aya::maps::MapError
@ -2422,6 +2423,7 @@ pub fn aya::maps::XskMap<T>::len(&self) -> u32
impl<T: core::borrow::Borrow<aya::maps::MapData>> aya::maps::XskMap<T> impl<T: core::borrow::Borrow<aya::maps::MapData>> aya::maps::XskMap<T>
pub fn aya::maps::XskMap<T>::pin<P: core::convert::AsRef<std::path::Path>>(self, path: P) -> core::result::Result<(), aya::pin::PinError> pub fn aya::maps::XskMap<T>::pin<P: core::convert::AsRef<std::path::Path>>(self, path: P) -> core::result::Result<(), aya::pin::PinError>
impl<T: core::borrow::BorrowMut<aya::maps::MapData>> aya::maps::XskMap<T> impl<T: core::borrow::BorrowMut<aya::maps::MapData>> aya::maps::XskMap<T>
pub fn aya::maps::XskMap<T>::clear_index(&mut self, index: u32) -> core::result::Result<(), aya::maps::MapError>
pub fn aya::maps::XskMap<T>::set(&mut self, index: u32, socket_fd: impl std::os::fd::raw::AsRawFd, flags: u64) -> core::result::Result<(), aya::maps::MapError> pub fn aya::maps::XskMap<T>::set(&mut self, index: u32, socket_fd: impl std::os::fd::raw::AsRawFd, flags: u64) -> core::result::Result<(), aya::maps::MapError>
impl core::convert::TryFrom<aya::maps::Map> for aya::maps::XskMap<aya::maps::MapData> impl core::convert::TryFrom<aya::maps::Map> for aya::maps::XskMap<aya::maps::MapData>
pub type aya::maps::XskMap<aya::maps::MapData>::Error = aya::maps::MapError pub type aya::maps::XskMap<aya::maps::MapData>::Error = aya::maps::MapError

Loading…
Cancel
Save