|
|
@ -9,6 +9,7 @@ use std::{
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
use libc::if_nametoindex;
|
|
|
|
use libc::if_nametoindex;
|
|
|
|
|
|
|
|
use log::warn;
|
|
|
|
use thiserror::Error;
|
|
|
|
use thiserror::Error;
|
|
|
|
|
|
|
|
|
|
|
|
use crate::{
|
|
|
|
use crate::{
|
|
|
@ -135,7 +136,9 @@ impl Xdp {
|
|
|
|
let prog_fd = self.fd()?;
|
|
|
|
let prog_fd = self.fd()?;
|
|
|
|
let prog_fd = prog_fd.as_fd();
|
|
|
|
let prog_fd = prog_fd.as_fd();
|
|
|
|
|
|
|
|
|
|
|
|
if KernelVersion::current().unwrap() >= KernelVersion::new(5, 9, 0) {
|
|
|
|
match KernelVersion::current() {
|
|
|
|
|
|
|
|
Ok(kernel_version) => {
|
|
|
|
|
|
|
|
if kernel_version >= KernelVersion::new(5, 9, 0) {
|
|
|
|
// Unwrap safety: the function starts with `self.fd()?` that will succeed if and only
|
|
|
|
// Unwrap safety: the function starts with `self.fd()?` that will succeed if and only
|
|
|
|
// if the program has been loaded, i.e. there is an fd. We get one by:
|
|
|
|
// if the program has been loaded, i.e. there is an fd. We get one by:
|
|
|
|
// - Using `Xdp::from_pin` that sets `expected_attach_type`
|
|
|
|
// - Using `Xdp::from_pin` that sets `expected_attach_type`
|
|
|
@ -173,6 +176,23 @@ impl Xdp {
|
|
|
|
})))
|
|
|
|
})))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Err(e) => {
|
|
|
|
|
|
|
|
eprintln!("Error getting the current kernel version");
|
|
|
|
|
|
|
|
let if_index = if_index as i32;
|
|
|
|
|
|
|
|
unsafe { netlink_set_xdp_fd(if_index, Some(prog_fd), None, flags.bits()) }
|
|
|
|
|
|
|
|
.map_err(|io_error| XdpError::NetlinkError { io_error })?;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let prog_fd = prog_fd.as_raw_fd();
|
|
|
|
|
|
|
|
self.data
|
|
|
|
|
|
|
|
.links
|
|
|
|
|
|
|
|
.insert(XdpLink::new(XdpLinkInner::NlLink(NlLink {
|
|
|
|
|
|
|
|
if_index,
|
|
|
|
|
|
|
|
prog_fd,
|
|
|
|
|
|
|
|
flags,
|
|
|
|
|
|
|
|
})))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Creates a program from a pinned entry on a bpffs.
|
|
|
|
/// Creates a program from a pinned entry on a bpffs.
|
|
|
|
///
|
|
|
|
///
|
|
|
@ -269,11 +289,20 @@ impl Link for NlLink {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn detach(self) -> Result<(), ProgramError> {
|
|
|
|
fn detach(self) -> Result<(), ProgramError> {
|
|
|
|
let flags = if KernelVersion::current().unwrap() >= KernelVersion::new(5, 7, 0) {
|
|
|
|
let flags = match KernelVersion::current() {
|
|
|
|
|
|
|
|
Ok(kernel_version) => {
|
|
|
|
|
|
|
|
if kernel_version >= KernelVersion::new(5, 7, 0) {
|
|
|
|
self.flags.bits() | XDP_FLAGS_REPLACE
|
|
|
|
self.flags.bits() | XDP_FLAGS_REPLACE
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
self.flags.bits()
|
|
|
|
self.flags.bits()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Err(_) => {
|
|
|
|
|
|
|
|
warn!("Warning: Can not get the current kernel version");
|
|
|
|
|
|
|
|
self.flags.bits()
|
|
|
|
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// SAFETY: TODO(https://github.com/aya-rs/aya/issues/612): make this safe by not holding `RawFd`s.
|
|
|
|
// SAFETY: TODO(https://github.com/aya-rs/aya/issues/612): make this safe by not holding `RawFd`s.
|
|
|
|
let prog_fd = unsafe { BorrowedFd::borrow_raw(self.prog_fd) };
|
|
|
|
let prog_fd = unsafe { BorrowedFd::borrow_raw(self.prog_fd) };
|
|
|
|
let _ = unsafe { netlink_set_xdp_fd(self.if_index, None, Some(prog_fd), flags) };
|
|
|
|
let _ = unsafe { netlink_set_xdp_fd(self.if_index, None, Some(prog_fd), flags) };
|
|
|
|