tests: Add integration test for pinning lifecycle

Signed-off-by: Dave Tucker <dave@dtucker.co.uk>
pull/378/head
Dave Tucker 2 years ago
parent 4ed587becc
commit 7c244e1f65

@ -187,7 +187,7 @@ impl PinnedLink {
}
}
/// Creates a [`PinnedLink`] from a valid path on bpffs
/// Creates a [`PinnedLink`] from a valid path on bpffs.
pub fn from_path<P: AsRef<Path>>(path: P) -> Result<Self, LinkError> {
let path_string = CString::new(path.as_ref().to_string_lossy().to_string()).unwrap();
let fd =

@ -3,7 +3,10 @@ use std::{process::Command, thread, time};
use aya::{
include_bytes_aligned,
maps::{Array, MapRefMut},
programs::{links::FdLink, TracePoint, Xdp, XdpFlags},
programs::{
links::{FdLink, PinnedLink},
TracePoint, Xdp, XdpFlags,
},
Bpf,
};
@ -60,14 +63,14 @@ fn multiple_btf_maps() -> anyhow::Result<()> {
Ok(())
}
fn is_loaded() -> bool {
fn is_loaded(name: &str) -> bool {
let output = Command::new("bpftool").args(["prog"]).output().unwrap();
let stdout = String::from_utf8(output.stdout).unwrap();
stdout.contains("test_unload")
stdout.contains(name)
}
fn assert_loaded(loaded: bool) {
let state = is_loaded();
fn assert_loaded(name: &str, loaded: bool) {
let state = is_loaded(name);
if state == loaded {
return;
}
@ -84,19 +87,19 @@ fn unload() -> anyhow::Result<()> {
{
let _link_owned = prog.take_link(link);
prog.unload().unwrap();
assert_loaded(true);
assert_loaded("test_unload", true);
};
assert_loaded(false);
assert_loaded("test_unload", false);
prog.load().unwrap();
assert_loaded(true);
assert_loaded("test_unload", true);
prog.attach("lo", XdpFlags::default()).unwrap();
assert_loaded(true);
assert_loaded("test_unload", true);
prog.unload().unwrap();
assert_loaded(false);
assert_loaded("test_unload", false);
Ok(())
}
@ -108,24 +111,57 @@ fn pin_link() -> anyhow::Result<()> {
prog.load().unwrap();
let link_id = prog.attach("lo", XdpFlags::default()).unwrap();
let link = prog.take_link(link_id)?;
assert_loaded(true);
assert_loaded("test_unload", true);
let fd_link: FdLink = link.try_into()?;
let pinned = fd_link.pin("/sys/fs/bpf/aya-xdp-test-lo")?;
// because of the pin, the program is still attached
prog.unload()?;
assert_loaded(true);
assert_loaded("test_unload", true);
// delete the pin, but the program is still attached
let new_link = pinned.unpin()?;
println!("third assert");
assert_loaded(true);
assert_loaded("test_unload", true);
// finally when new_link is dropped we're detached
drop(new_link);
println!("final assert");
assert_loaded(false);
assert_loaded("test_unload", false);
Ok(())
}
#[integration_test]
fn pin_lifecycle() -> anyhow::Result<()> {
let bytes = include_bytes_aligned!("../../../../target/bpfel-unknown-none/debug/pass");
// 1. Load Program and Pin
{
let mut bpf = Bpf::load(bytes)?;
let prog: &mut Xdp = bpf.program_mut("pass").unwrap().try_into().unwrap();
prog.load().unwrap();
let link_id = prog.attach("lo", XdpFlags::default()).unwrap();
let link = prog.take_link(link_id)?;
let fd_link: FdLink = link.try_into()?;
fd_link.pin("/sys/fs/bpf/aya-xdp-test-lo")?;
}
// should still be loaded since link was pinned
assert_loaded("pass", true);
// 2. Load a new version of the program, unpin link, and atomically replace old program
{
let mut bpf = Bpf::load(bytes)?;
let prog: &mut Xdp = bpf.program_mut("pass").unwrap().try_into().unwrap();
prog.load().unwrap();
let link = PinnedLink::from_path("/sys/fs/bpf/aya-xdp-test-lo")?.unpin()?;
prog.attach_to_link(link.try_into()?)?;
assert_loaded("pass", true);
}
// program should be unloaded
assert_loaded("pass", false);
Ok(())
}

Loading…
Cancel
Save