4
0

uptime.rs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Copyright 2017 6WIND S.A. <quentin.monnet@6wind.com>
  2. //
  3. // Licensed under the Apache License, Version 2.0 <http://www.apache.org/licenses/LICENSE-2.0> or
  4. // the MIT license <http://opensource.org/licenses/MIT>, at your option. This file may not be
  5. // copied, modified, or distributed except according to those terms.
  6. extern crate rbpf;
  7. use rbpf::helpers;
  8. // The main objectives of this example is to show:
  9. //
  10. // * the use of EbpfVmNoData function,
  11. // * and the use of a helper.
  12. //
  13. // The two eBPF programs are independent and are not related to one another.
  14. fn main() {
  15. let prog1 = &[
  16. 0xb4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // mov32 r0, 0
  17. 0xb4, 0x01, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, // mov32 r1, 2
  18. 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, // add32 r0, 1
  19. 0x0c, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // add32 r0, r1
  20. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // exit and return r0
  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. // Create a VM: this one takes no data. Load prog1 in it.
  37. let mut vm = rbpf::EbpfVmNoData::new(prog1);
  38. // Execute prog1.
  39. assert_eq!(vm.prog_exec(), 0x3);
  40. // As struct EbpfVmNoData does not takes any memory area, its return value is mostly
  41. // deterministic. So we know prog1 will always return 3. There is an exception: when it uses
  42. // helpers, the latter may have non-deterministic values, and all calls may not return the same
  43. // value.
  44. //
  45. // In the following example we use a helper to get the elapsed time since boot time: we
  46. // reimplement uptime in eBPF, in Rust. Because why not.
  47. vm.set_prog(prog2);
  48. vm.register_helper(helpers::BPF_KTIME_GETNS_IDX, helpers::bpf_time_getns);
  49. let time = vm.prog_exec();
  50. let days = time / 10u64.pow(9) / 60 / 60 / 24;
  51. let hours = (time / 10u64.pow(9) / 60 / 60) % 24;
  52. let minutes = (time / 10u64.pow(9) / 60 ) % 60;
  53. let seconds = (time / 10u64.pow(9)) % 60;
  54. let nanosec = time % 10u64.pow(9);
  55. println!("Uptime: {:#x} ns == {} days {:02}:{:02}:{:02}, {} ns",
  56. time, days, hours, minutes, seconds, nanosec);
  57. }