bpf_api/lib.rs
1//! [](https://github.com/arcjustin/bpf-api/actions?query=workflow%3Abuild)
2//! [](https://crates.io/crates/bpf-api)
3//! [](https://docs.rs/bpf-api/)
4//! [](https://tokei.rs/b1/github/arcjustin/bpf-api?category=code)
5//!
6//! Idomatic Rust bindings for eBPF programs, probes, and maps. Want write in-line eBPF without relying on external dependencies, like shelling out to bcc/llvm? Check out this crate's sister crates:
7//!
8//! * [btf](https://crates.io/crates/btf)
9//! * [bpf-script](https://crates.io/crates/bpf-script)
10//!
11//! ## Usage
12//! ```
13//! use bpf_api::collections::Array;
14//!
15//! const ARRAY_SIZE: u32 = 10;
16//! let array = Array::<u32>::with_capacity(ARRAY_SIZE).unwrap();
17//!
18//! for i in 0..ARRAY_SIZE {
19//! let val = i + 100;
20//! assert!(matches!(array.get(i), Ok(0)));
21//! assert!(array.set(i, val).is_ok());
22//! match array.get(i) {
23//! Ok(v) => assert_eq!(v, val),
24//! Err(e) => panic!("array.get() failed: {}", e),
25//! }
26//! }
27//! ```
28//!
29//! ## License
30//!
31//! * [MIT license](http://opensource.org/licenses/MIT)
32
33pub mod collections;
34pub mod error;
35pub mod probes;
36pub mod prog;
37
38mod platform;
39
40#[cfg(test)]
41mod tests {
42 use crate::collections::{Array, HashMap, Queue};
43
44 #[test]
45 fn hashmap_insert_get() {
46 let list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16];
47 let map = HashMap::<u32, [u8; 16]>::with_capacity(10).unwrap();
48 assert!(map.insert(300, list).is_ok());
49 assert!(map.get(300).is_ok());
50 assert_eq!(map.get(300).unwrap(), list);
51 }
52
53 #[test]
54 fn queue_push_pop() {
55 const QUEUE_SIZE: u32 = 10;
56 let queue = Queue::<u32>::with_capacity(QUEUE_SIZE).unwrap();
57
58 /* Fill the queue to the max */
59 for i in 0..10 {
60 assert!(queue.push(i + 100).is_ok());
61 }
62
63 /* make sure the next push fails */
64 assert!(queue.push(1000).is_err());
65
66 /* test front and make sure it doesn't consume */
67 assert!(matches!(queue.front(), Ok(100)));
68 assert!(matches!(queue.front(), Ok(100)));
69
70 /* pop all items and check values */
71 for i in 0..10 {
72 match queue.pop() {
73 Ok(val) => assert_eq!(val, i + 100),
74 Err(e) => panic!("queue.pop() failed: {}", e),
75 }
76 }
77
78 /* make sure queue is now empty */
79 assert!(queue.pop().is_err());
80 }
81
82 #[test]
83 fn array_set_get() {
84 const ARRAY_SIZE: u32 = 10;
85 let array = Array::<u32>::with_capacity(ARRAY_SIZE).unwrap();
86
87 for i in 0..ARRAY_SIZE {
88 let val = i + 100;
89 assert!(matches!(array.get(i), Ok(0)));
90 assert!(array.set(i, val).is_ok());
91 match array.get(i) {
92 Ok(v) => assert_eq!(v, val),
93 Err(e) => panic!("array.get() failed: {}", e),
94 }
95 }
96 }
97}