From 30d2b25f1173904b2542a25792d7dff5b97b837f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alessandro=C2=A0Decina?= Date: Wed, 28 Apr 2021 08:29:19 +0000 Subject: [PATCH] aya: xdp: fix detaching on kernels older than 5.7 XDP_FLAGS_REPLACE was added in 5.7. Now for kernels >= 5.7 whenever we detach an XDP program we pass along the program fd we expect to be detaching. For older kernels, we just detach whatever is attached, which is not great but it's the way the API worked pre XDP_FLAGS_REPLACE. --- aya/src/programs/xdp.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/aya/src/programs/xdp.rs b/aya/src/programs/xdp.rs index c37587de..131b3efa 100644 --- a/aya/src/programs/xdp.rs +++ b/aya/src/programs/xdp.rs @@ -110,6 +110,7 @@ impl Xdp { Ok(self.data.link(XdpLink::NlLink(NlLink { if_index, prog_fd: Some(prog_fd), + flags, }))) } } @@ -119,12 +120,19 @@ impl Xdp { struct NlLink { if_index: i32, prog_fd: Option, + flags: XdpFlags, } impl Link for NlLink { fn detach(&mut self) -> Result<(), ProgramError> { if let Some(fd) = self.prog_fd.take() { - let _ = unsafe { netlink_set_xdp_fd(self.if_index, -1, Some(fd), XDP_FLAGS_REPLACE) }; + let k_ver = kernel_version().unwrap(); + let flags = if k_ver >= (5, 7, 0) { + self.flags.bits | XDP_FLAGS_REPLACE + } else { + self.flags.bits + }; + let _ = unsafe { netlink_set_xdp_fd(self.if_index, -1, Some(fd), flags) }; Ok(()) } else { Err(ProgramError::AlreadyDetached)