Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

aya: Add bpf_map_delete_elem for XskMap #848

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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_delete_elem` fails.
pub fn clear_index(&mut self, index: u32) -> Result<(), MapError> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should likely be either remove or delete. Hold please while we hear from @alessandrod.

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 @@ -58,10 +58,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 @@ -82,6 +84,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
2 changes: 2 additions & 0 deletions xtask/public-api/aya.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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>
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>
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>
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
Expand Down Expand Up @@ -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>
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>
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>
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
Expand Down
Loading