From fd20bd5e2304748d79a00c83479016f776fdcf2b Mon Sep 17 00:00:00 2001 From: Tw Date: Fri, 2 Jul 2021 11:17:57 +0800 Subject: [PATCH] bpf: add support for STACK_TRACE map This path support initial support for BPF_MAP_TYPE_STACK_TRACE. Signed-off-by: Tw --- bpf/aya-bpf/src/maps/mod.rs | 2 ++ bpf/aya-bpf/src/maps/stack_trace.rs | 43 +++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 bpf/aya-bpf/src/maps/stack_trace.rs diff --git a/bpf/aya-bpf/src/maps/mod.rs b/bpf/aya-bpf/src/maps/mod.rs index 2bdd11cf..9a8dd632 100644 --- a/bpf/aya-bpf/src/maps/mod.rs +++ b/bpf/aya-bpf/src/maps/mod.rs @@ -4,6 +4,7 @@ pub mod perf_map; pub mod queue; pub mod sock_hash; pub mod sock_map; +pub mod stack_trace; pub use array::Array; pub use hash_map::HashMap; @@ -11,3 +12,4 @@ pub use perf_map::PerfMap; pub use queue::Queue; pub use sock_hash::SockHash; pub use sock_map::SockMap; +pub use stack_trace::StackTrace; diff --git a/bpf/aya-bpf/src/maps/stack_trace.rs b/bpf/aya-bpf/src/maps/stack_trace.rs new file mode 100644 index 00000000..cf16e02a --- /dev/null +++ b/bpf/aya-bpf/src/maps/stack_trace.rs @@ -0,0 +1,43 @@ +use core::mem; + +use crate::{ + bindings::{bpf_map_def, bpf_map_type::BPF_MAP_TYPE_STACK_TRACE}, + helpers::{bpf_get_stackid}, + BpfContext, +}; + +#[repr(transparent)] +pub struct StackTrace { + def: bpf_map_def, +} + +const PERF_MAX_STACK_DEPTH: u32 = 127; + +impl StackTrace { + pub const fn with_max_entries(max_entries: u32, flags: u32) -> StackTrace { + StackTrace { + def: bpf_map_def { + type_: BPF_MAP_TYPE_STACK_TRACE, + key_size: mem::size_of::() as u32, + value_size: mem::size_of::() as u32 * PERF_MAX_STACK_DEPTH, + max_entries, + map_flags: flags, + id: 0, + pinning: 0, + }, + } + } + + pub unsafe fn get_stackid(&mut self, ctx: &C, flags: u64) -> Result { + let ret = bpf_get_stackid( + ctx.as_ptr(), + &mut self.def as *mut _ as *mut _, + flags, + ); + if ret < 0 { + Err(ret) + } else { + Ok(ret as u32) + } + } +} \ No newline at end of file