xref: /DragonOS/kernel/crates/rbpf/examples/uptime.rs (revision fae6e9ade46a52976ad5d099643d51cc20876448)
1 // SPDX-License-Identifier: (Apache-2.0 OR MIT)
2 // Copyright 2017 6WIND S.A. <quentin.monnet@6wind.com>
3 
4 extern crate rbpf;
5 use rbpf::helpers;
6 
7 // The main objectives of this example is to show:
8 //
9 // * the use of EbpfVmNoData function,
10 // * and the use of a helper.
11 //
12 // The two eBPF programs are independent and are not related to one another.
main()13 fn main() {
14     let prog1 = &[
15         0xb4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // mov32 r0, 0
16         0xb4, 0x01, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, // mov32 r1, 2
17         0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, // add32 r0, 1
18         0x0c, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // add32 r0, r1
19         0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // exit and return r0
20     ];
21 
22     // We use helper `bpf_time_getns()`, which is similar to helper `bpf_ktime_getns()` from Linux
23     // kernel. Hence rbpf::helpers module provides the index of this in-kernel helper as a
24     // constant, so that we can remain compatible with programs for the kernel. Here we also cast
25     // it to a u8 so as to use it directly in program instructions.
26     let hkey = helpers::BPF_KTIME_GETNS_IDX as u8;
27     let prog2 = &[
28         0xb7, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // mov64 r1, 0
29         0xb7, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // mov64 r1, 0
30         0xb7, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // mov64 r1, 0
31         0xb7, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // mov64 r1, 0
32         0xb7, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // mov64 r1, 0
33         0x85, 0x00, 0x00, 0x00, hkey, 0x00, 0x00, 0x00, // call helper <hkey>
34         0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // exit and return r0
35     ];
36 
37     // Create a VM: this one takes no data. Load prog1 in it.
38     let mut vm = rbpf::EbpfVmNoData::new(Some(prog1)).unwrap();
39     // Execute prog1.
40     assert_eq!(vm.execute_program().unwrap(), 0x3);
41 
42     // As struct EbpfVmNoData does not takes any memory area, its return value is mostly
43     // deterministic. So we know prog1 will always return 3. There is an exception: when it uses
44     // helpers, the latter may have non-deterministic values, and all calls may not return the same
45     // value.
46     //
47     // In the following example we use a helper to get the elapsed time since boot time: we
48     // reimplement uptime in eBPF, in Rust. Because why not.
49 
50     vm.set_program(prog2).unwrap();
51     vm.register_helper(helpers::BPF_KTIME_GETNS_IDX, helpers::bpf_time_getns)
52         .unwrap();
53 
54     let time;
55 
56     #[cfg(all(not(windows), feature = "std"))]
57     {
58         vm.jit_compile().unwrap();
59 
60         time = unsafe { vm.execute_program_jit().unwrap() };
61     }
62 
63     #[cfg(any(windows, not(feature = "std")))]
64     {
65         time = vm.execute_program().unwrap();
66     }
67 
68     let days = time / 10u64.pow(9) / 60 / 60 / 24;
69     let hours = (time / 10u64.pow(9) / 60 / 60) % 24;
70     let minutes = (time / 10u64.pow(9) / 60) % 60;
71     let seconds = (time / 10u64.pow(9)) % 60;
72     let nanosec = time % 10u64.pow(9);
73 
74     println!(
75         "Uptime: {:#x} ns == {} days {:02}:{:02}:{:02}, {} ns",
76         time, days, hours, minutes, seconds, nanosec
77     );
78 }
79