rbpf.rs 4.0 KB

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