1 use std::io;
2 use std::path::PathBuf;
3
4 use libbpf_rs::Map;
5 use libbpf_rs::MapCore;
6 use libbpf_rs::MapMut;
7 use libbpf_rs::Object;
8 use libbpf_rs::ObjectBuilder;
9 use libbpf_rs::OpenObject;
10 use libbpf_rs::ProgramMut;
11
12
get_test_object_path(filename: &str) -> PathBuf13 pub fn get_test_object_path(filename: &str) -> PathBuf {
14 let mut path = PathBuf::new();
15 // env!() macro fails at compile time if var not found
16 path.push(env!("CARGO_MANIFEST_DIR"));
17 path.push("tests/bin");
18 path.push(filename);
19 path
20 }
21
open_test_object(filename: &str) -> OpenObject22 pub fn open_test_object(filename: &str) -> OpenObject {
23 let obj_path = get_test_object_path(filename);
24 let obj = ObjectBuilder::default()
25 .debug(true)
26 .open_file(obj_path)
27 .expect("failed to open object");
28 obj
29 }
30
bump_rlimit_mlock()31 pub fn bump_rlimit_mlock() {
32 let rlimit = libc::rlimit {
33 rlim_cur: 128 << 20,
34 rlim_max: 128 << 20,
35 };
36
37 let ret = unsafe { libc::setrlimit(libc::RLIMIT_MEMLOCK, &rlimit) };
38 assert_eq!(
39 ret,
40 0,
41 "Setting RLIMIT_MEMLOCK failed with errno: {}",
42 io::Error::last_os_error()
43 );
44 }
45
get_test_object(filename: &str) -> Object46 pub fn get_test_object(filename: &str) -> Object {
47 open_test_object(filename)
48 .load()
49 .expect("failed to load object")
50 }
51
52 /// Find the BPF map with the given name, panic if it does not exist.
53 #[track_caller]
get_map<'obj>(object: &'obj Object, name: &str) -> Map<'obj>54 pub fn get_map<'obj>(object: &'obj Object, name: &str) -> Map<'obj> {
55 object
56 .maps()
57 .find(|map| map.name() == name)
58 .unwrap_or_else(|| panic!("failed to find map `{name}`"))
59 }
60
61 /// Find the BPF map with the given name, panic if it does not exist.
62 #[track_caller]
get_map_mut<'obj>(object: &'obj mut Object, name: &str) -> MapMut<'obj>63 pub fn get_map_mut<'obj>(object: &'obj mut Object, name: &str) -> MapMut<'obj> {
64 object
65 .maps_mut()
66 .find(|map| map.name() == name)
67 .unwrap_or_else(|| panic!("failed to find map `{name}`"))
68 }
69
70 /// Find the BPF program with the given name, panic if it does not exist.
71 #[track_caller]
get_prog_mut<'obj>(object: &'obj mut Object, name: &str) -> ProgramMut<'obj>72 pub fn get_prog_mut<'obj>(object: &'obj mut Object, name: &str) -> ProgramMut<'obj> {
73 object
74 .progs_mut()
75 .find(|map| map.name() == name)
76 .unwrap_or_else(|| panic!("failed to find program `{name}`"))
77 }
78