4
0

uptime.rs 3.2 KB

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