Skip to content

Commit

Permalink
Add CHANGELOG entry for #625
Browse files Browse the repository at this point in the history
Add a CHANGELOG entry for #625 and fix up various minor details.

Signed-off-by: Daniel Müller <[email protected]>
  • Loading branch information
d-e-s-o authored and danielocfb committed Dec 20, 2023
1 parent 3d93298 commit ee49133
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 17 deletions.
5 changes: 5 additions & 0 deletions libbpf-rs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
Unreleased
----------
- Introduced `Xdp` type for working with XDP programs


0.22.0
------
- Reworked `Error` type:
Expand Down
28 changes: 13 additions & 15 deletions libbpf-rs/src/xdp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ use crate::Result;
bitflags! {
/// Flags to configure the `XDP` operations
pub struct XdpFlags: u32 {
/// No flags.
const NONE = 0;
/// See [`libbpf_sys::XDP_FLAGS_UPDATE_IF_NOEXIST`].
const UPDATE_IF_NOEXIST = libbpf_sys::XDP_FLAGS_UPDATE_IF_NOEXIST as _;
/// See [`libbpf_sys::XDP_FLAGS_SKB_MODE`].
Expand All @@ -23,8 +25,6 @@ bitflags! {
const MODES = libbpf_sys::XDP_FLAGS_MODES as _;
/// See [`libbpf_sys::XDP_FLAGS_MASK`].
const MASK = libbpf_sys::XDP_FLAGS_MASK as _;
/// Support for no flags. Refer: https://github.com/libbpf/libbpf-rs/pull/625#discussion_r1427057344
const ANY = 0;
}

}
Expand All @@ -40,7 +40,8 @@ pub struct Xdp<'fd> {
}

impl<'fd> Xdp<'fd> {
/// Create a new xdp instance with the given file descriptor of the `SEC("xdp")` [`Program`][crate::Program].
/// Create a new XDP instance with the given file descriptor of the
/// `SEC("xdp")` [`Program`][crate::Program].
pub fn new(fd: BorrowedFd<'fd>) -> Self {
let mut xdp = Xdp {
fd,
Expand All @@ -52,9 +53,12 @@ impl<'fd> Xdp<'fd> {
xdp
}

/// Attach the XDP program to the given interface to start processing the packets
/// Note: Once a program is attached, it will outlive the userspace program.
/// Make sure to detach the program if its not desired.
/// Attach the XDP program to the given interface to start processing the
/// packets
///
/// # Notes
/// Once a program is attached, it will outlive the userspace program. Make
/// sure to detach the program if its not desired.
pub fn attach(&self, ifindex: i32, flags: XdpFlags) -> Result<()> {
let ret = unsafe {
libbpf_sys::bpf_xdp_attach(
Expand All @@ -77,20 +81,14 @@ impl<'fd> Xdp<'fd> {
pub fn query(&self, ifindex: i32, flags: XdpFlags) -> Result<libbpf_sys::bpf_xdp_query_opts> {
let mut opts = self.query_opts;
let err = unsafe { libbpf_sys::bpf_xdp_query(ifindex, flags.bits() as i32, &mut opts) };
match util::parse_ret(err) {
Ok(_) => Ok(opts),
Err(e) => Err(e),
}
util::parse_ret(err).map(|()| opts)
}

/// Query to inspect the program identifier (prog_id)
pub fn query_id(&self, ifindex: i32, flags: XdpFlags) -> Result<u32> {
let mut prog_id: u32 = 0;
let mut prog_id = 0;
let err =
unsafe { libbpf_sys::bpf_xdp_query_id(ifindex, flags.bits() as i32, &mut prog_id) };
match util::parse_ret(err) {
Ok(_) => Ok(prog_id),
Err(e) => Err(e),
}
util::parse_ret(err).map(|()| prog_id)
}
}
6 changes: 4 additions & 2 deletions libbpf-rs/tests/test_xdp.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
mod test;
use scopeguard::defer;
use std::os::fd::AsFd;

use scopeguard::defer;

mod test;
use test::bump_rlimit_mlock;
use test::get_test_object;

Expand Down

0 comments on commit ee49133

Please sign in to comment.