Skip to content

Commit

Permalink
aya: Allow to attach XDP probe by interface index
Browse files Browse the repository at this point in the history
  • Loading branch information
kriomant committed Mar 20, 2023
1 parent 113e3ef commit 2e3c177
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions aya/src/programs/xdp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,29 @@ impl Xdp {
/// [`XdpError::NetlinkError`] is returned for older
/// kernels.
pub fn attach(&mut self, interface: &str, flags: XdpFlags) -> Result<XdpLinkId, ProgramError> {
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<XdpLinkId, ProgramError> {
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) {
Expand Down

0 comments on commit 2e3c177

Please sign in to comment.