Skip to content

Commit

Permalink
add: replace support for xdp
Browse files Browse the repository at this point in the history
Signed-off-by: yogaraj.s <[email protected]>
  • Loading branch information
yogaraj.s committed Jan 7, 2024
1 parent da7e037 commit 74183ba
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
11 changes: 11 additions & 0 deletions libbpf-rs/src/xdp.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use bitflags::bitflags;
use std::mem::size_of;
use std::os::fd::RawFd;
use std::os::unix::io::AsRawFd;
use std::os::unix::io::BorrowedFd;

Expand Down Expand Up @@ -91,4 +92,14 @@ impl<'fd> Xdp<'fd> {
unsafe { libbpf_sys::bpf_xdp_query_id(ifindex, flags.bits() as i32, &mut prog_id) };
util::parse_ret(err).map(|()| prog_id)
}

/// Replace an existing xdp program (identified by old_prog_fd) with this xdp program
pub fn replace(&self, ifindex: i32, old_prog_fd: RawFd, flags: XdpFlags) -> Result<()> {
let mut opts = self.attach_opts;
opts.old_prog_fd = old_prog_fd;
let ret = unsafe {
libbpf_sys::bpf_xdp_attach(ifindex, self.fd.as_raw_fd(), flags.bits(), &opts)
};
util::parse_ret(ret)
}
}
9 changes: 9 additions & 0 deletions libbpf-rs/tests/test_xdp.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::os::fd::AsFd;
use std::os::fd::AsRawFd;

use scopeguard::defer;

Expand All @@ -18,7 +19,11 @@ fn test_sudo_xdp() {
let obj = get_test_object("xdp.bpf.o");
let fd = obj.prog("xdp_filter").unwrap().as_fd();

let obj1 = get_test_object("xdp.bpf.o");
let fd1 = obj1.prog("xdp_filter").unwrap().as_fd();

let xdp_prog = Xdp::new(fd);
let xdp_prog1 = Xdp::new(fd1);

defer! {
xdp_prog.detach(LO_IFINDEX, XdpFlags::UPDATE_IF_NOEXIST).unwrap();
Expand All @@ -41,6 +46,10 @@ fn test_sudo_xdp() {
.query(LO_IFINDEX, XdpFlags::UPDATE_IF_NOEXIST)
.is_ok());

assert!(xdp_prog1
.replace(LO_IFINDEX, fd.as_raw_fd(), XdpFlags::REPLACE)
.is_ok());

assert!(xdp_prog
.detach(LO_IFINDEX, XdpFlags::UPDATE_IF_NOEXIST)
.is_ok());
Expand Down

0 comments on commit 74183ba

Please sign in to comment.