-
Notifications
You must be signed in to change notification settings - Fork 247
/
test.rs
executable file
·57 lines (41 loc) · 1.21 KB
/
test.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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.take_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);
}