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

Improve AF_XDP test #846

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
13 changes: 11 additions & 2 deletions test/integration-ebpf/src/redirect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,17 @@ static CPUS: CpuMap = CpuMap::with_max_entries(1, 0);
static mut HITS: Array<u32> = Array::with_max_entries(2, 0);

#[xdp]
pub fn redirect_sock(_ctx: XdpContext) -> u32 {
SOCKS.redirect(0, 0).unwrap_or(xdp_action::XDP_ABORTED)
pub fn redirect_sock(ctx: XdpContext) -> u32 {
// Retrieve queue ID of incoming packet.
let queue_id = unsafe { *ctx.ctx }.rx_queue_index;
// Check whether incoming packet's queue ID matches the queue ID of the socket in XSKMAP at index `queue_id`.
if SOCKS.get(queue_id) == Some(queue_id) {
// Queue ID matches, redirect to AF_XDP socket.
SOCKS.redirect(0, 0).unwrap_or(xdp_action::XDP_ABORTED)
} else {
// Queue ID did not match, pass packet to kernel network stack.
xdp_action::XDP_PASS
}
}

#[xdp]
Expand Down
4 changes: 2 additions & 2 deletions test/integration-test/src/tests/xdp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ fn af_xdp() {
struct PacketMap(MaybeUninit<[u8; 4096]>);

// Safety: don't access alloc down the line.
let mut alloc = Box::new(PacketMap(MaybeUninit::uninit()));
let mut alloc = Box::new(PacketMap(MaybeUninit::zeroed()));
let umem = {
// Safety: this is a shared buffer between the kernel and us, uninitialized memory is valid.
// Safety: Zeroed memory is valid for u8 arrays.
let mem = unsafe { alloc.0.assume_init_mut() }.as_mut().into();
// Safety: we cannot access `mem` further down the line because it falls out of scope.
unsafe { Umem::new(UmemConfig::default(), mem).unwrap() }
Expand Down
Loading