Skip to content

Commit

Permalink
aya: Add bpf_map_delete_elem for XskMap
Browse files Browse the repository at this point in the history
  • Loading branch information
arctic-alpaca committed Dec 6, 2023
1 parent b176967 commit 8eb1cd3
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
21 changes: 20 additions & 1 deletion aya/src/maps/xdp/xsk_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::{

use crate::{
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.
Expand Down Expand Up @@ -78,4 +78,23 @@ impl<T: BorrowMut<MapData>> XskMap<T> {
)?;
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()
})
}
}
19 changes: 16 additions & 3 deletions test/integration-test/src/tests/xdp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ fn af_xdp() {
// 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.
#[repr(C, align(16384))]
struct PacketMap(MaybeUninit<[u8; 4096]>);
struct PacketMap(MaybeUninit<[u8; 2 * 4096]>);

// Safety: don't access alloc down the line.
let mut alloc = Box::new(PacketMap(MaybeUninit::uninit()));
Expand Down Expand Up @@ -60,10 +60,12 @@ fn af_xdp() {
socks.set(0, rx.as_raw_fd(), 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
let mut writer = fq_cq.fill(1);
// Produce two frames to be filled by the kernel
let mut writer = fq_cq.fill(2);
writer.insert_once(frame.offset);
writer.insert_once(frame1.offset);
writer.commit();

let sock = UdpSocket::bind("127.0.0.1:0").unwrap();
Expand All @@ -84,6 +86,17 @@ fn af_xdp() {
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!(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]
Expand Down

0 comments on commit 8eb1cd3

Please sign in to comment.