Skip to content

Commit

Permalink
xdp: add support for attaching with custom xdp flags
Browse files Browse the repository at this point in the history
  • Loading branch information
alessandrod committed Mar 2, 2021
1 parent d326038 commit 55d8bcf
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 11 deletions.
1 change: 1 addition & 0 deletions aya/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
2 changes: 2 additions & 0 deletions aya/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#[macro_use]
extern crate lazy_static;
#[macro_use]
extern crate bitflags;

mod bpf;
mod generated;
Expand Down
16 changes: 8 additions & 8 deletions aya/src/programs/mod.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down
21 changes: 18 additions & 3 deletions aya/src/programs/xdp.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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,
Expand All @@ -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();
Expand All @@ -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
Expand Down

0 comments on commit 55d8bcf

Please sign in to comment.