mirror of https://github.com/aya-rs/aya
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
229 lines
6.9 KiB
Rust
229 lines
6.9 KiB
Rust
2 years ago
|
use std::{convert::TryInto as _, thread, time};
|
||
3 years ago
|
|
||
|
use aya::{
|
||
|
include_bytes_aligned,
|
||
2 years ago
|
maps::Array,
|
||
2 years ago
|
programs::{
|
||
|
links::{FdLink, PinnedLink},
|
||
2 years ago
|
loaded_programs, KProbe, TracePoint, Xdp, XdpFlags,
|
||
2 years ago
|
},
|
||
3 years ago
|
Bpf,
|
||
|
};
|
||
2 years ago
|
|
||
2 years ago
|
mod common;
|
||
|
use common::kernel_version;
|
||
3 years ago
|
|
||
2 years ago
|
const MAX_RETRIES: u32 = 100;
|
||
|
const RETRY_DURATION_MS: u64 = 10;
|
||
|
|
||
2 years ago
|
#[test]
|
||
2 years ago
|
fn long_name() {
|
||
2 years ago
|
let bytes = include_bytes_aligned!("../../../target/bpfel-unknown-none/release/name_test");
|
||
2 years ago
|
let mut bpf = Bpf::load(bytes).unwrap();
|
||
|
let name_prog: &mut Xdp = bpf
|
||
|
.program_mut("ihaveaverylongname")
|
||
|
.unwrap()
|
||
|
.try_into()
|
||
|
.unwrap();
|
||
3 years ago
|
name_prog.load().unwrap();
|
||
2 years ago
|
name_prog.attach("lo", XdpFlags::default()).unwrap();
|
||
3 years ago
|
|
||
|
// We used to be able to assert with bpftool that the program name was short.
|
||
|
// It seem though that it now uses the name from the ELF symbol table instead.
|
||
|
// Therefore, as long as we were able to load the program, this is good enough.
|
||
|
}
|
||
|
|
||
2 years ago
|
#[test]
|
||
2 years ago
|
fn multiple_btf_maps() {
|
||
3 years ago
|
let bytes =
|
||
2 years ago
|
include_bytes_aligned!("../../../target/bpfel-unknown-none/release/multimap-btf.bpf.o");
|
||
2 years ago
|
let mut bpf = Bpf::load(bytes).unwrap();
|
||
3 years ago
|
|
||
2 years ago
|
let map_1: Array<_, u64> = bpf.take_map("map_1").unwrap().try_into().unwrap();
|
||
|
let map_2: Array<_, u64> = bpf.take_map("map_2").unwrap().try_into().unwrap();
|
||
3 years ago
|
|
||
|
let prog: &mut TracePoint = bpf.program_mut("tracepoint").unwrap().try_into().unwrap();
|
||
|
prog.load().unwrap();
|
||
|
prog.attach("sched", "sched_switch").unwrap();
|
||
|
|
||
|
thread::sleep(time::Duration::from_secs(3));
|
||
|
|
||
|
let key = 0;
|
||
2 years ago
|
let val_1 = map_1.get(&key, 0).unwrap();
|
||
|
let val_2 = map_2.get(&key, 0).unwrap();
|
||
3 years ago
|
|
||
|
assert_eq!(val_1, 24);
|
||
|
assert_eq!(val_2, 42);
|
||
|
}
|
||
|
|
||
2 years ago
|
macro_rules! assert_loaded {
|
||
|
($name:literal, $loaded:expr) => {
|
||
|
for i in 0..(MAX_RETRIES + 1) {
|
||
2 years ago
|
let state = loaded_programs().any(|prog| prog.unwrap().name() == $name.as_bytes());
|
||
2 years ago
|
if state == $loaded {
|
||
|
break;
|
||
|
}
|
||
|
if i == MAX_RETRIES {
|
||
|
panic!("Expected loaded: {} but was loaded: {}", $loaded, state);
|
||
|
}
|
||
|
thread::sleep(time::Duration::from_millis(RETRY_DURATION_MS));
|
||
|
}
|
||
|
};
|
||
3 years ago
|
}
|
||
|
|
||
2 years ago
|
#[test]
|
||
2 years ago
|
fn unload_xdp() {
|
||
2 years ago
|
let bytes = include_bytes_aligned!("../../../target/bpfel-unknown-none/release/test");
|
||
2 years ago
|
let mut bpf = Bpf::load(bytes).unwrap();
|
||
2 years ago
|
let prog: &mut Xdp = bpf
|
||
|
.program_mut("test_unload_xdp")
|
||
|
.unwrap()
|
||
|
.try_into()
|
||
|
.unwrap();
|
||
3 years ago
|
prog.load().unwrap();
|
||
2 years ago
|
assert_loaded!("test_unload_xdp", true);
|
||
3 years ago
|
let link = prog.attach("lo", XdpFlags::default()).unwrap();
|
||
|
{
|
||
2 years ago
|
let _link_owned = prog.take_link(link).unwrap();
|
||
3 years ago
|
prog.unload().unwrap();
|
||
2 years ago
|
assert_loaded!("test_unload_xdp", true);
|
||
3 years ago
|
};
|
||
|
|
||
2 years ago
|
assert_loaded!("test_unload_xdp", false);
|
||
3 years ago
|
prog.load().unwrap();
|
||
|
|
||
2 years ago
|
assert_loaded!("test_unload_xdp", true);
|
||
3 years ago
|
prog.attach("lo", XdpFlags::default()).unwrap();
|
||
|
|
||
2 years ago
|
assert_loaded!("test_unload_xdp", true);
|
||
|
prog.unload().unwrap();
|
||
|
|
||
|
assert_loaded!("test_unload_xdp", false);
|
||
|
}
|
||
|
|
||
2 years ago
|
#[test]
|
||
2 years ago
|
fn unload_kprobe() {
|
||
2 years ago
|
let bytes = include_bytes_aligned!("../../../target/bpfel-unknown-none/release/test");
|
||
2 years ago
|
let mut bpf = Bpf::load(bytes).unwrap();
|
||
|
let prog: &mut KProbe = bpf
|
||
|
.program_mut("test_unload_kpr")
|
||
|
.unwrap()
|
||
|
.try_into()
|
||
|
.unwrap();
|
||
|
prog.load().unwrap();
|
||
|
assert_loaded!("test_unload_kpr", true);
|
||
|
let link = prog.attach("try_to_wake_up", 0).unwrap();
|
||
|
{
|
||
|
let _link_owned = prog.take_link(link).unwrap();
|
||
|
prog.unload().unwrap();
|
||
|
assert_loaded!("test_unload_kpr", true);
|
||
|
};
|
||
|
|
||
|
assert_loaded!("test_unload_kpr", false);
|
||
|
prog.load().unwrap();
|
||
|
|
||
|
assert_loaded!("test_unload_kpr", true);
|
||
|
prog.attach("try_to_wake_up", 0).unwrap();
|
||
|
|
||
|
assert_loaded!("test_unload_kpr", true);
|
||
3 years ago
|
prog.unload().unwrap();
|
||
|
|
||
2 years ago
|
assert_loaded!("test_unload_kpr", false);
|
||
3 years ago
|
}
|
||
2 years ago
|
|
||
2 years ago
|
#[test]
|
||
2 years ago
|
fn pin_link() {
|
||
2 years ago
|
if kernel_version().unwrap() < (5, 9, 0) {
|
||
2 years ago
|
eprintln!("skipping test, XDP uses netlink");
|
||
2 years ago
|
return;
|
||
|
}
|
||
|
|
||
2 years ago
|
let bytes = include_bytes_aligned!("../../../target/bpfel-unknown-none/release/test");
|
||
2 years ago
|
let mut bpf = Bpf::load(bytes).unwrap();
|
||
2 years ago
|
let prog: &mut Xdp = bpf
|
||
|
.program_mut("test_unload_xdp")
|
||
|
.unwrap()
|
||
|
.try_into()
|
||
|
.unwrap();
|
||
2 years ago
|
prog.load().unwrap();
|
||
|
let link_id = prog.attach("lo", XdpFlags::default()).unwrap();
|
||
2 years ago
|
let link = prog.take_link(link_id).unwrap();
|
||
2 years ago
|
assert_loaded!("test_unload_xdp", true);
|
||
2 years ago
|
|
||
2 years ago
|
let fd_link: FdLink = link.try_into().unwrap();
|
||
|
let pinned = fd_link.pin("/sys/fs/bpf/aya-xdp-test-lo").unwrap();
|
||
2 years ago
|
|
||
|
// because of the pin, the program is still attached
|
||
2 years ago
|
prog.unload().unwrap();
|
||
2 years ago
|
assert_loaded!("test_unload_xdp", true);
|
||
2 years ago
|
|
||
|
// delete the pin, but the program is still attached
|
||
2 years ago
|
let new_link = pinned.unpin().unwrap();
|
||
2 years ago
|
assert_loaded!("test_unload_xdp", true);
|
||
2 years ago
|
|
||
|
// finally when new_link is dropped we're detached
|
||
|
drop(new_link);
|
||
2 years ago
|
assert_loaded!("test_unload_xdp", false);
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
#[test]
|
||
2 years ago
|
fn pin_lifecycle() {
|
||
2 years ago
|
if kernel_version().unwrap() < (5, 9, 0) {
|
||
2 years ago
|
eprintln!("skipping test, XDP uses netlink");
|
||
2 years ago
|
return;
|
||
|
}
|
||
|
|
||
2 years ago
|
let bytes = include_bytes_aligned!("../../../target/bpfel-unknown-none/release/pass");
|
||
2 years ago
|
|
||
|
// 1. Load Program and Pin
|
||
|
{
|
||
2 years ago
|
let mut bpf = Bpf::load(bytes).unwrap();
|
||
2 years ago
|
let prog: &mut Xdp = bpf.program_mut("pass").unwrap().try_into().unwrap();
|
||
|
prog.load().unwrap();
|
||
2 years ago
|
prog.pin("/sys/fs/bpf/aya-xdp-test-prog").unwrap();
|
||
|
}
|
||
|
|
||
|
// should still be loaded since prog was pinned
|
||
|
assert_loaded!("pass", true);
|
||
|
|
||
|
// 2. Load program from bpffs but don't attach it
|
||
|
{
|
||
|
let _ = Xdp::from_pin("/sys/fs/bpf/aya-xdp-test-prog").unwrap();
|
||
|
}
|
||
|
|
||
|
// should still be loaded since prog was pinned
|
||
|
assert_loaded!("pass", true);
|
||
|
|
||
|
// 3. Load program from bpffs and attach
|
||
|
{
|
||
|
let mut prog = Xdp::from_pin("/sys/fs/bpf/aya-xdp-test-prog").unwrap();
|
||
2 years ago
|
let link_id = prog.attach("lo", XdpFlags::default()).unwrap();
|
||
2 years ago
|
let link = prog.take_link(link_id).unwrap();
|
||
|
let fd_link: FdLink = link.try_into().unwrap();
|
||
|
fd_link.pin("/sys/fs/bpf/aya-xdp-test-lo").unwrap();
|
||
2 years ago
|
|
||
|
// Unpin the program. It will stay attached since its links were pinned.
|
||
|
prog.unpin().unwrap();
|
||
2 years ago
|
}
|
||
|
|
||
|
// should still be loaded since link was pinned
|
||
2 years ago
|
assert_loaded!("pass", true);
|
||
2 years ago
|
|
||
2 years ago
|
// 4. Load a new version of the program, unpin link, and atomically replace old program
|
||
2 years ago
|
{
|
||
2 years ago
|
let mut bpf = Bpf::load(bytes).unwrap();
|
||
2 years ago
|
let prog: &mut Xdp = bpf.program_mut("pass").unwrap().try_into().unwrap();
|
||
|
prog.load().unwrap();
|
||
|
|
||
2 years ago
|
let link = PinnedLink::from_pin("/sys/fs/bpf/aya-xdp-test-lo")
|
||
|
.unwrap()
|
||
|
.unpin()
|
||
|
.unwrap();
|
||
|
prog.attach_to_link(link.try_into().unwrap()).unwrap();
|
||
2 years ago
|
assert_loaded!("pass", true);
|
||
2 years ago
|
}
|
||
|
|
||
|
// program should be unloaded
|
||
2 years ago
|
assert_loaded!("pass", false);
|
||
2 years ago
|
}
|