-
Notifications
You must be signed in to change notification settings - Fork 287
/
load.rs
166 lines (133 loc) · 5.13 KB
/
load.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
use std::{process::Command, thread, time};
use aya::{
include_bytes_aligned,
maps::Array,
programs::{
links::{FdLink, PinnedLink},
TracePoint, Xdp, XdpFlags,
},
Bpf,
};
use super::{integration_test, IntegrationTest};
const MAX_RETRIES: u32 = 100;
const RETRY_DURATION_MS: u64 = 10;
#[integration_test]
fn long_name() -> anyhow::Result<()> {
let bytes = include_bytes_aligned!("../../../../target/bpfel-unknown-none/debug/name_test");
let mut bpf = Bpf::load(bytes)?;
let name_prog: &mut Xdp = bpf.program_mut("ihaveaverylongname").unwrap().try_into()?;
name_prog.load().unwrap();
name_prog.attach("lo", XdpFlags::default())?;
// 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.
Ok(())
}
#[integration_test]
fn multiple_btf_maps() -> anyhow::Result<()> {
let bytes =
include_bytes_aligned!("../../../../target/bpfel-unknown-none/debug/multimap-btf.bpf.o");
let mut bpf = Bpf::load(bytes)?;
let map_1: Array<_, u64> = bpf.take_map("map_1").unwrap().try_into()?;
let map_2: Array<_, u64> = bpf.take_map("map_2").unwrap().try_into()?;
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;
let val_1 = map_1.get(&key, 0)?;
let val_2 = map_2.get(&key, 0)?;
assert_eq!(val_1, 24);
assert_eq!(val_2, 42);
Ok(())
}
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(name)
}
macro_rules! assert_loaded {
($name:literal, $loaded:expr) => {
for i in 0..(MAX_RETRIES + 1) {
let state = is_loaded($name);
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));
}
};
}
#[integration_test]
fn unload() -> anyhow::Result<()> {
let bytes = include_bytes_aligned!("../../../../target/bpfel-unknown-none/debug/test");
let mut bpf = Bpf::load(bytes)?;
let prog: &mut Xdp = bpf.program_mut("test_unload").unwrap().try_into().unwrap();
prog.load().unwrap();
let link = prog.attach("lo", XdpFlags::default()).unwrap();
{
let _link_owned = prog.take_link(link);
prog.unload().unwrap();
assert_loaded!("test_unload", true);
};
assert_loaded!("test_unload", false);
prog.load().unwrap();
assert_loaded!("test_unload", true);
prog.attach("lo", XdpFlags::default()).unwrap();
assert_loaded!("test_unload", true);
prog.unload().unwrap();
assert_loaded!("test_unload", false);
Ok(())
}
#[integration_test]
fn pin_link() -> anyhow::Result<()> {
let bytes = include_bytes_aligned!("../../../../target/bpfel-unknown-none/debug/test");
let mut bpf = Bpf::load(bytes)?;
let prog: &mut Xdp = bpf.program_mut("test_unload").unwrap().try_into().unwrap();
prog.load().unwrap();
let link_id = prog.attach("lo", XdpFlags::default()).unwrap();
let link = prog.take_link(link_id)?;
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!("test_unload", true);
// delete the pin, but the program is still attached
let new_link = pinned.unpin()?;
assert_loaded!("test_unload", true);
// finally when new_link is dropped we're detached
drop(new_link);
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_pin("/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(())
}