mirror of https://github.com/aya-rs/aya
parent
6b1f47323b
commit
e2685c98d8
@ -0,0 +1,26 @@
|
||||
//! ```cargo
|
||||
//! [dependencies]
|
||||
//! aya-bpf = { path = "../../../../bpf/aya-bpf" }
|
||||
//! ```
|
||||
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
|
||||
use aya_bpf::{bindings::xdp_action, macros::xdp, programs::XdpContext};
|
||||
|
||||
#[xdp(name = "test_unload")]
|
||||
pub fn pass(ctx: XdpContext) -> u32 {
|
||||
match unsafe { try_pass(ctx) } {
|
||||
Ok(ret) => ret,
|
||||
Err(_) => xdp_action::XDP_ABORTED,
|
||||
}
|
||||
}
|
||||
|
||||
unsafe fn try_pass(_ctx: XdpContext) -> Result<u32, u32> {
|
||||
Ok(xdp_action::XDP_PASS)
|
||||
}
|
||||
|
||||
#[panic_handler]
|
||||
fn panic(_info: &core::panic::PanicInfo) -> ! {
|
||||
unsafe { core::hint::unreachable_unchecked() }
|
||||
}
|
@ -0,0 +1,57 @@
|
||||
//! ```cargo
|
||||
//! [dependencies]
|
||||
//! aya = { path = "../../../../aya" }
|
||||
//! ```
|
||||
|
||||
use aya::{
|
||||
programs::{Xdp, XdpFlags},
|
||||
Bpf,
|
||||
};
|
||||
use std::convert::TryInto;
|
||||
use std::process::Command;
|
||||
|
||||
fn is_loaded() -> bool {
|
||||
let output = Command::new("bpftool").args(&["prog"]).output().unwrap();
|
||||
let stdout = String::from_utf8(output.stdout).unwrap();
|
||||
stdout.contains("test_unload")
|
||||
}
|
||||
|
||||
fn assert_loaded(loaded: bool) {
|
||||
let state = is_loaded();
|
||||
if state == loaded {
|
||||
return;
|
||||
}
|
||||
panic!("Expected loaded: {} but was loaded: {}", loaded, state);
|
||||
}
|
||||
|
||||
fn main() {
|
||||
println!("Loading XDP program");
|
||||
let mut bpf = Bpf::load_file("test.o").unwrap();
|
||||
let dispatcher: &mut Xdp = bpf.program_mut("test_unload").unwrap().try_into().unwrap();
|
||||
|
||||
dispatcher.load().unwrap();
|
||||
|
||||
let link = dispatcher.attach("eth0", XdpFlags::default()).unwrap();
|
||||
|
||||
{
|
||||
let link_owned = dispatcher.forget_link(link);
|
||||
|
||||
dispatcher.unload().unwrap();
|
||||
|
||||
assert_loaded(true);
|
||||
};
|
||||
|
||||
assert_loaded(false);
|
||||
|
||||
dispatcher.load().unwrap();
|
||||
|
||||
assert_loaded(true);
|
||||
|
||||
dispatcher.attach("eth0", XdpFlags::default()).unwrap();
|
||||
|
||||
assert_loaded(true);
|
||||
|
||||
dispatcher.unload().unwrap();
|
||||
|
||||
assert_loaded(false);
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
#!/bin/sh
|
||||
# SUMMARY: Check that the program can be unloaded
|
||||
# LABELS:
|
||||
|
||||
set -ex
|
||||
|
||||
# Source libraries. Uncomment if needed/defined
|
||||
#. "${RT_LIB}"
|
||||
. "${RT_PROJECT_ROOT}/_lib/lib.sh"
|
||||
|
||||
NAME=test
|
||||
|
||||
clean_up() {
|
||||
rm -rf ebpf user ${NAME}.o ${NAME}
|
||||
exec_vm rm ${NAME} ${NAME}.o
|
||||
}
|
||||
|
||||
trap clean_up EXIT
|
||||
|
||||
# Test code goes here
|
||||
compile_ebpf ${NAME}.ebpf.rs
|
||||
compile_user ${NAME}.rs
|
||||
|
||||
scp_vm ${NAME}.o
|
||||
scp_vm ${NAME}
|
||||
|
||||
exec_vm sudo ./${NAME}
|
Loading…
Reference in New Issue