uptime.rs 3.1 KB

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