diff --git a/ebpf/aya-ebpf/src/programs/xdp.rs b/ebpf/aya-ebpf/src/programs/xdp.rs index 26c3369a..35d22a0c 100644 --- a/ebpf/aya-ebpf/src/programs/xdp.rs +++ b/ebpf/aya-ebpf/src/programs/xdp.rs @@ -21,12 +21,6 @@ impl XdpContext { unsafe { (*self.ctx).data_end as usize } } - /// Return the interface of the index as determined by the OS. - #[inline] - pub fn ingress_ifindex(&self) -> usize { - unsafe { (*self.ctx).ingress_ifindex as usize } - } - /// Return the raw address of the XdpContext metadata. #[inline(always)] pub fn metadata(&self) -> usize { @@ -38,6 +32,18 @@ impl XdpContext { pub fn metadata_end(&self) -> usize { self.data() } + + /// Return the index of the ingress interface. + #[inline] + pub fn ingress_ifindex(&self) -> usize { + unsafe { (*self.ctx).ingress_ifindex as usize } + } + + /// Return the index of the receive queue. + #[inline] + pub fn rx_queue_index(&self) -> u32 { + unsafe { (*self.ctx).rx_queue_index } + } } impl EbpfContext for XdpContext { diff --git a/test/integration-ebpf/src/redirect.rs b/test/integration-ebpf/src/redirect.rs index 85a0a0a8..f885f5de 100644 --- a/test/integration-ebpf/src/redirect.rs +++ b/test/integration-ebpf/src/redirect.rs @@ -25,8 +25,15 @@ static CPUS: CpuMap = CpuMap::with_max_entries(1, 0); static HITS: Array = 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 { + let queue_id = ctx.rx_queue_index(); + 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] diff --git a/xtask/public-api/aya-ebpf.txt b/xtask/public-api/aya-ebpf.txt index 1cb7cc4e..dd37b049 100644 --- a/xtask/public-api/aya-ebpf.txt +++ b/xtask/public-api/aya-ebpf.txt @@ -2024,6 +2024,7 @@ pub fn aya_ebpf::programs::xdp::XdpContext::ingress_ifindex(&self) -> usize pub fn aya_ebpf::programs::xdp::XdpContext::metadata(&self) -> usize pub fn aya_ebpf::programs::xdp::XdpContext::metadata_end(&self) -> usize pub fn aya_ebpf::programs::xdp::XdpContext::new(ctx: *mut aya_ebpf_bindings::x86_64::bindings::xdp_md) -> aya_ebpf::programs::xdp::XdpContext +pub fn aya_ebpf::programs::xdp::XdpContext::rx_queue_index(&self) -> u32 impl aya_ebpf::EbpfContext for aya_ebpf::programs::xdp::XdpContext pub fn aya_ebpf::programs::xdp::XdpContext::as_ptr(&self) -> *mut core::ffi::c_void impl core::marker::Freeze for aya_ebpf::programs::xdp::XdpContext @@ -2638,6 +2639,7 @@ pub fn aya_ebpf::programs::xdp::XdpContext::ingress_ifindex(&self) -> usize pub fn aya_ebpf::programs::xdp::XdpContext::metadata(&self) -> usize pub fn aya_ebpf::programs::xdp::XdpContext::metadata_end(&self) -> usize pub fn aya_ebpf::programs::xdp::XdpContext::new(ctx: *mut aya_ebpf_bindings::x86_64::bindings::xdp_md) -> aya_ebpf::programs::xdp::XdpContext +pub fn aya_ebpf::programs::xdp::XdpContext::rx_queue_index(&self) -> u32 impl aya_ebpf::EbpfContext for aya_ebpf::programs::xdp::XdpContext pub fn aya_ebpf::programs::xdp::XdpContext::as_ptr(&self) -> *mut core::ffi::c_void impl core::marker::Freeze for aya_ebpf::programs::xdp::XdpContext