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