xdp: add support for attaching with custom xdp flags

pull/1/head
Alessandro Decina 4 years ago
parent d326038cf4
commit 55d8bcf386

@ -8,6 +8,7 @@ edition = "2018"
libc = { version = "0.2" }
thiserror = "1"
object = { version = "0.23", default-features = false, features = ["std", "read_core", "elf"] }
bitflags = "1.2.1"
bytes = "1"
lazy_static = "1"
parking_lot = { version = "0.11.1", features = ["send_guard"] }

@ -2,6 +2,8 @@
#[macro_use]
extern crate lazy_static;
#[macro_use]
extern crate bitflags;
mod bpf;
mod generated;

@ -1,18 +1,18 @@
mod perf_attach;
mod probe;
mod socket_filter;
mod trace_point;
mod xdp;
pub mod probe;
pub mod socket_filter;
pub mod trace_point;
pub mod xdp;
use libc::{close, ENOSPC};
use std::{cell::RefCell, cmp, convert::TryFrom, ffi::CStr, io, os::raw::c_uint, rc::Rc};
use thiserror::Error;
use perf_attach::*;
pub use probe::*;
pub use socket_filter::*;
pub use trace_point::*;
pub use xdp::*;
pub use probe::{KProbe, KProbeError, UProbe, UProbeError};
pub use socket_filter::{SocketFilter, SocketFilterError};
pub use trace_point::{TracePoint, TracePointError};
pub use xdp::{Xdp, XdpError};
use crate::{obj, sys::bpf_load_program, RawFd};
#[derive(Debug, Error)]

@ -1,9 +1,13 @@
use bitflags;
use libc::if_nametoindex;
use std::{ffi::CString, io};
use thiserror::Error;
use crate::{
generated::{bpf_attach_type::BPF_XDP, bpf_prog_type::BPF_PROG_TYPE_XDP, XDP_FLAGS_REPLACE},
generated::{
bpf_attach_type::BPF_XDP, bpf_prog_type::BPF_PROG_TYPE_XDP, XDP_FLAGS_DRV_MODE,
XDP_FLAGS_HW_MODE, XDP_FLAGS_REPLACE, XDP_FLAGS_SKB_MODE, XDP_FLAGS_UPDATE_IF_NOEXIST,
},
programs::{load_program, FdLink, Link, LinkRef, ProgramData, ProgramError},
sys::bpf_link_create,
sys::kernel_version,
@ -20,6 +24,17 @@ pub enum XdpError {
},
}
bitflags! {
#[derive(Default)]
pub struct XdpFlags: u32 {
const SKB_MODE = XDP_FLAGS_SKB_MODE;
const DRV_MODE = XDP_FLAGS_DRV_MODE;
const HW_MODE = XDP_FLAGS_HW_MODE;
const REPLACE = XDP_FLAGS_REPLACE;
const UPDATE_IF_NOEXIST = XDP_FLAGS_UPDATE_IF_NOEXIST;
}
}
#[derive(Debug)]
pub struct Xdp {
pub(crate) data: ProgramData,
@ -34,7 +49,7 @@ impl Xdp {
self.data.name.to_string()
}
pub fn attach(&mut self, interface: &str) -> Result<LinkRef, ProgramError> {
pub fn attach(&mut self, interface: &str, flags: XdpFlags) -> Result<LinkRef, ProgramError> {
let prog_fd = self.data.fd_or_err()?;
let c_interface = CString::new(interface).unwrap();
@ -47,7 +62,7 @@ impl Xdp {
let k_ver = kernel_version().unwrap();
if k_ver >= (5, 7, 0) {
let link_fd = bpf_link_create(prog_fd, if_index + 42, BPF_XDP, 0)
let link_fd = bpf_link_create(prog_fd, if_index, BPF_XDP, flags.bits)
.map_err(|(_, io_error)| ProgramError::BpfLinkCreateError { io_error })?
as RawFd;
Ok(self

Loading…
Cancel
Save