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