From 2e3c1779be03898dd6a01644012ef21b2475ad63 Mon Sep 17 00:00:00 2001 From: Mikhail Trishchenkov Date: Mon, 20 Mar 2023 12:20:46 +0700 Subject: [PATCH 1/4] aya: Allow to attach XDP probe by interface index --- aya/src/programs/xdp.rs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/aya/src/programs/xdp.rs b/aya/src/programs/xdp.rs index d36d5f27..52805bc7 100644 --- a/aya/src/programs/xdp.rs +++ b/aya/src/programs/xdp.rs @@ -98,14 +98,29 @@ impl Xdp { /// [`XdpError::NetlinkError`] is returned for older /// kernels. pub fn attach(&mut self, interface: &str, flags: XdpFlags) -> Result { - let prog_fd = self.data.fd_or_err()?; let c_interface = CString::new(interface).unwrap(); - let if_index = unsafe { if_nametoindex(c_interface.as_ptr()) } as RawFd; + let if_index = unsafe { if_nametoindex(c_interface.as_ptr()) }; if if_index == 0 { return Err(ProgramError::UnknownInterface { name: interface.to_string(), }); } + self.attach_by_ifindex(if_index, flags) + } + + /// Attaches the program to the given `interface`. + /// + /// The returned value can be used to detach, see [Xdp::detach]. + /// + /// # Errors + /// + /// When attaching fails, [`ProgramError::SyscallError`] is returned for + /// kernels `>= 5.9.0`, and instead + /// [`XdpError::NetlinkError`] is returned for older + /// kernels. + pub fn attach_by_ifindex(&mut self, if_index: libc::c_uint, flags: XdpFlags) -> Result { + let prog_fd = self.data.fd_or_err()?; + let if_index = if_index as RawFd; let k_ver = kernel_version().unwrap(); if k_ver >= (5, 9, 0) { From 676b5cdc0df380471090153ab4ff2e641e4e1d03 Mon Sep 17 00:00:00 2001 From: Mikhail Trishchenkov Date: Mon, 20 Mar 2023 14:00:55 +0700 Subject: [PATCH 2/4] Rename method and fix comment --- aya/src/programs/xdp.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/aya/src/programs/xdp.rs b/aya/src/programs/xdp.rs index 52805bc7..909f95c4 100644 --- a/aya/src/programs/xdp.rs +++ b/aya/src/programs/xdp.rs @@ -105,10 +105,10 @@ impl Xdp { name: interface.to_string(), }); } - self.attach_by_ifindex(if_index, flags) + self.attach_to_if_index(if_index, flags) } - /// Attaches the program to the given `interface`. + /// Attaches the program to the given interface index. /// /// The returned value can be used to detach, see [Xdp::detach]. /// @@ -118,7 +118,7 @@ impl Xdp { /// kernels `>= 5.9.0`, and instead /// [`XdpError::NetlinkError`] is returned for older /// kernels. - pub fn attach_by_ifindex(&mut self, if_index: libc::c_uint, flags: XdpFlags) -> Result { + pub fn attach_to_if_index(&mut self, if_index: libc::c_uint, flags: XdpFlags) -> Result { let prog_fd = self.data.fd_or_err()?; let if_index = if_index as RawFd; From 896e3ab3130c4de44e8c0f4f974163c13aa50ff0 Mon Sep 17 00:00:00 2001 From: Mikhail Trishchenkov Date: Mon, 20 Mar 2023 16:11:13 +0700 Subject: [PATCH 3/4] Fix formatting --- aya/src/programs/xdp.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/aya/src/programs/xdp.rs b/aya/src/programs/xdp.rs index 909f95c4..f88b0e17 100644 --- a/aya/src/programs/xdp.rs +++ b/aya/src/programs/xdp.rs @@ -118,7 +118,11 @@ impl Xdp { /// kernels `>= 5.9.0`, and instead /// [`XdpError::NetlinkError`] is returned for older /// kernels. - pub fn attach_to_if_index(&mut self, if_index: libc::c_uint, flags: XdpFlags) -> Result { + pub fn attach_to_if_index( + &mut self, + if_index: libc::c_uint, + flags: XdpFlags, + ) -> Result { let prog_fd = self.data.fd_or_err()?; let if_index = if_index as RawFd; From ce60854934312c9ebb75178f44faf9369febf6ad Mon Sep 17 00:00:00 2001 From: Mikhail Trishchenkov Date: Mon, 20 Mar 2023 21:47:24 +0700 Subject: [PATCH 4/4] Don't leak libc types --- aya/src/programs/xdp.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aya/src/programs/xdp.rs b/aya/src/programs/xdp.rs index f88b0e17..63f00987 100644 --- a/aya/src/programs/xdp.rs +++ b/aya/src/programs/xdp.rs @@ -120,7 +120,7 @@ impl Xdp { /// kernels. pub fn attach_to_if_index( &mut self, - if_index: libc::c_uint, + if_index: u32, flags: XdpFlags, ) -> Result { let prog_fd = self.data.fd_or_err()?;