perf_event_array.rs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. use core::{cell::UnsafeCell, marker::PhantomData, mem};
  2. use crate::{
  3. bindings::{bpf_map_def, bpf_map_type::BPF_MAP_TYPE_PERF_EVENT_ARRAY, BPF_F_CURRENT_CPU},
  4. helpers::bpf_perf_event_output,
  5. maps::PinningType,
  6. EbpfContext,
  7. };
  8. #[repr(transparent)]
  9. pub struct PerfEventArray<T> {
  10. def: UnsafeCell<bpf_map_def>,
  11. _t: PhantomData<T>,
  12. }
  13. unsafe impl<T: Sync> Sync for PerfEventArray<T> {}
  14. impl<T> PerfEventArray<T> {
  15. pub const fn new(flags: u32) -> PerfEventArray<T> {
  16. PerfEventArray {
  17. def: UnsafeCell::new(bpf_map_def {
  18. type_: BPF_MAP_TYPE_PERF_EVENT_ARRAY,
  19. key_size: mem::size_of::<u32>() as u32,
  20. value_size: mem::size_of::<u32>() as u32,
  21. max_entries: 0,
  22. map_flags: flags,
  23. id: 0,
  24. pinning: PinningType::None as u32,
  25. }),
  26. _t: PhantomData,
  27. }
  28. }
  29. pub const fn pinned(flags: u32) -> PerfEventArray<T> {
  30. PerfEventArray {
  31. def: UnsafeCell::new(bpf_map_def {
  32. type_: BPF_MAP_TYPE_PERF_EVENT_ARRAY,
  33. key_size: mem::size_of::<u32>() as u32,
  34. value_size: mem::size_of::<u32>() as u32,
  35. max_entries: 0,
  36. map_flags: flags,
  37. id: 0,
  38. pinning: PinningType::ByName as u32,
  39. }),
  40. _t: PhantomData,
  41. }
  42. }
  43. pub fn output<C: EbpfContext>(&self, ctx: &C, data: &T, flags: u32) {
  44. self.output_at_index(ctx, BPF_F_CURRENT_CPU as u32, data, flags)
  45. }
  46. pub fn output_at_index<C: EbpfContext>(&self, ctx: &C, index: u32, data: &T, flags: u32) {
  47. let flags = (u64::from(flags) << 32) | u64::from(index);
  48. unsafe {
  49. bpf_perf_event_output(
  50. ctx.as_ptr(),
  51. self.def.get() as *mut _,
  52. flags,
  53. data as *const _ as *mut _,
  54. mem::size_of::<T>() as u64,
  55. );
  56. }
  57. }
  58. }