rbpf.rs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. use core::{mem::size_of, ptr::null_mut, slice::from_raw_parts};
  2. use std::collections::HashMap;
  3. use assert_matches::assert_matches;
  4. use aya_obj::{generated::bpf_insn, Object, ProgramSection};
  5. #[test]
  6. fn run_with_rbpf() {
  7. let object = Object::parse(crate::PASS).unwrap();
  8. assert_eq!(object.programs.len(), 1);
  9. assert_matches!(
  10. object.programs["pass"].section,
  11. ProgramSection::Xdp { frags: true }
  12. );
  13. let instructions = &object
  14. .functions
  15. .get(&object.programs["pass"].function_key())
  16. .unwrap()
  17. .instructions;
  18. let data = unsafe {
  19. from_raw_parts(
  20. instructions.as_ptr() as *const u8,
  21. instructions.len() * size_of::<bpf_insn>(),
  22. )
  23. };
  24. // Use rbpf interpreter instead of JIT compiler to ensure platform compatibility.
  25. let vm = rbpf::EbpfVmNoData::new(Some(data)).unwrap();
  26. const XDP_PASS: u64 = 2;
  27. assert_eq!(vm.execute_program().unwrap(), XDP_PASS);
  28. }
  29. static mut MULTIMAP_MAPS: [*mut Vec<u64>; 2] = [null_mut(), null_mut()];
  30. #[test]
  31. fn use_map_with_rbpf() {
  32. let mut object = Object::parse(crate::MULTIMAP_BTF).unwrap();
  33. assert_eq!(object.programs.len(), 1);
  34. assert_matches!(
  35. object.programs["bpf_prog"].section,
  36. ProgramSection::UProbe { .. }
  37. );
  38. // Initialize maps:
  39. // - fd: 0xCAFE00 or 0xCAFE01 (the 0xCAFE00 part is used to distinguish fds from indices),
  40. // - Note that rbpf does not convert fds into real pointers,
  41. // so we keeps the pointers to our maps in MULTIMAP_MAPS, to be used in helpers.
  42. let mut maps = HashMap::new();
  43. let mut map_instances = vec![vec![0u64], vec![0u64]];
  44. for (name, map) in object.maps.iter() {
  45. assert_eq!(map.key_size(), size_of::<u32>() as u32);
  46. assert_eq!(map.value_size(), size_of::<u64>() as u32);
  47. assert_eq!(
  48. map.map_type(),
  49. aya_obj::generated::bpf_map_type::BPF_MAP_TYPE_ARRAY as u32
  50. );
  51. let map_id = if name == "map_1" { 0 } else { 1 };
  52. let fd = map_id as std::os::fd::RawFd | 0xCAFE00;
  53. maps.insert(name.to_owned(), (fd, map.clone()));
  54. unsafe {
  55. MULTIMAP_MAPS[map_id] = &mut map_instances[map_id] as *mut _;
  56. }
  57. }
  58. let text_sections = object
  59. .functions
  60. .iter()
  61. .map(|((section_index, _), _)| *section_index)
  62. .collect();
  63. object
  64. .relocate_maps(
  65. maps.iter()
  66. .map(|(s, (fd, map))| (s.as_ref() as &str, *fd, map)),
  67. &text_sections,
  68. )
  69. .expect("Relocation failed");
  70. // Actually there is no local function call involved.
  71. object.relocate_calls(&text_sections).unwrap();
  72. // Executes the program
  73. assert_eq!(object.programs.len(), 1);
  74. let instructions = &object
  75. .functions
  76. .get(&object.programs["bpf_prog"].function_key())
  77. .unwrap()
  78. .instructions;
  79. let data = unsafe {
  80. from_raw_parts(
  81. instructions.as_ptr() as *const u8,
  82. instructions.len() * size_of::<bpf_insn>(),
  83. )
  84. };
  85. let mut vm = rbpf::EbpfVmNoData::new(Some(data)).unwrap();
  86. vm.register_helper(2, bpf_map_update_elem_multimap)
  87. .expect("Helper failed");
  88. assert_eq!(vm.execute_program().unwrap(), 0);
  89. assert_eq!(map_instances[0][0], 24);
  90. assert_eq!(map_instances[1][0], 42);
  91. unsafe {
  92. MULTIMAP_MAPS[0] = null_mut();
  93. MULTIMAP_MAPS[1] = null_mut();
  94. }
  95. }
  96. #[track_caller]
  97. fn bpf_map_update_elem_multimap(map: u64, key: u64, value: u64, _: u64, _: u64) -> u64 {
  98. assert_matches!(map, 0xCAFE00 | 0xCAFE01);
  99. let key = *unsafe { (key as usize as *const u32).as_ref().unwrap() };
  100. let value = *unsafe { (value as usize as *const u64).as_ref().unwrap() };
  101. assert_eq!(key, 0);
  102. unsafe {
  103. let map_instance = MULTIMAP_MAPS[map as usize & 0xFF].as_mut().unwrap();
  104. map_instance[0] = value;
  105. }
  106. 0
  107. }