diff --git a/bpf/aya-bpf/src/maps/array.rs b/bpf/aya-bpf/src/maps/array.rs index bbee2f75..349e7efe 100644 --- a/bpf/aya-bpf/src/maps/array.rs +++ b/bpf/aya-bpf/src/maps/array.rs @@ -5,6 +5,7 @@ use aya_bpf_cty::c_void; use crate::{ bindings::{bpf_map_def, bpf_map_type::BPF_MAP_TYPE_ARRAY}, helpers::bpf_map_lookup_elem, + maps::PinningType, }; #[repr(transparent)] @@ -23,7 +24,22 @@ impl Array { max_entries, map_flags: flags, id: 0, - pinning: 0, + pinning: PinningType::None as u32, + }, + _t: PhantomData, + } + } + + pub const fn pinned(max_entries: u32, flags: u32) -> Array { + Array { + def: bpf_map_def { + type_: BPF_MAP_TYPE_ARRAY, + key_size: mem::size_of::() as u32, + value_size: mem::size_of::() as u32, + max_entries, + map_flags: flags, + id: 0, + pinning: PinningType::ByName as u32, }, _t: PhantomData, } diff --git a/bpf/aya-bpf/src/maps/hash_map.rs b/bpf/aya-bpf/src/maps/hash_map.rs index 3f3a803d..b2fc50ef 100644 --- a/bpf/aya-bpf/src/maps/hash_map.rs +++ b/bpf/aya-bpf/src/maps/hash_map.rs @@ -5,6 +5,7 @@ use aya_bpf_cty::{c_long, c_void}; use crate::{ bindings::{bpf_map_def, bpf_map_type::BPF_MAP_TYPE_HASH}, helpers::{bpf_map_delete_elem, bpf_map_lookup_elem, bpf_map_update_elem}, + maps::PinningType, }; #[repr(transparent)] @@ -24,14 +25,14 @@ impl HashMap { max_entries, map_flags: flags, id: 0, - pinning: 0, + pinning: PinningType::None as u32, }, _k: PhantomData, _v: PhantomData, } } - pub const fn pinned(max_entries: u32, flags: u32, pinning: u32) -> HashMap { + pub const fn pinned(max_entries: u32, flags: u32) -> HashMap { HashMap { def: bpf_map_def { type_: BPF_MAP_TYPE_HASH, @@ -40,7 +41,7 @@ impl HashMap { max_entries, map_flags: flags, id: 0, - pinning, + pinning: PinningType::ByName as u32, }, _k: PhantomData, _v: PhantomData, diff --git a/bpf/aya-bpf/src/maps/mod.rs b/bpf/aya-bpf/src/maps/mod.rs index 71db366a..b6cd3c65 100644 --- a/bpf/aya-bpf/src/maps/mod.rs +++ b/bpf/aya-bpf/src/maps/mod.rs @@ -1,3 +1,10 @@ +#[repr(u32)] +#[derive(Copy, Clone, Debug, PartialEq)] +pub(crate) enum PinningType { + None = 0, + ByName = 1, +} + pub mod array; pub mod hash_map; pub mod per_cpu_array; diff --git a/bpf/aya-bpf/src/maps/per_cpu_array.rs b/bpf/aya-bpf/src/maps/per_cpu_array.rs index cfa264ad..5019e93f 100644 --- a/bpf/aya-bpf/src/maps/per_cpu_array.rs +++ b/bpf/aya-bpf/src/maps/per_cpu_array.rs @@ -5,6 +5,7 @@ use aya_bpf_cty::c_void; use crate::{ bindings::{bpf_map_def, bpf_map_type::BPF_MAP_TYPE_PERCPU_ARRAY}, helpers::bpf_map_lookup_elem, + maps::PinningType, }; #[repr(transparent)] @@ -23,7 +24,22 @@ impl PerCpuArray { max_entries, map_flags: flags, id: 0, - pinning: 0, + pinning: PinningType::None as u32, + }, + _t: PhantomData, + } + } + + pub const fn pinned(max_entries: u32, flags: u32) -> PerCpuArray { + PerCpuArray { + def: bpf_map_def { + type_: BPF_MAP_TYPE_PERCPU_ARRAY, + key_size: mem::size_of::() as u32, + value_size: mem::size_of::() as u32, + max_entries, + map_flags: flags, + id: 0, + pinning: PinningType::ByName as u32, }, _t: PhantomData, } diff --git a/bpf/aya-bpf/src/maps/perf_map.rs b/bpf/aya-bpf/src/maps/perf_map.rs index 3776f2f2..bd3d7fd4 100644 --- a/bpf/aya-bpf/src/maps/perf_map.rs +++ b/bpf/aya-bpf/src/maps/perf_map.rs @@ -3,6 +3,7 @@ use core::{marker::PhantomData, mem}; use crate::{ bindings::{bpf_map_def, bpf_map_type::BPF_MAP_TYPE_PERF_EVENT_ARRAY, BPF_F_CURRENT_CPU}, helpers::bpf_perf_event_output, + maps::PinningType, BpfContext, }; @@ -26,7 +27,22 @@ impl PerfMap { max_entries, map_flags: flags, id: 0, - pinning: 0, + pinning: PinningType::None as u32, + }, + _t: PhantomData, + } + } + + pub const fn pinned(max_entries: u32, flags: u32) -> PerfMap { + PerfMap { + def: bpf_map_def { + type_: BPF_MAP_TYPE_PERF_EVENT_ARRAY, + key_size: mem::size_of::() as u32, + value_size: mem::size_of::() as u32, + max_entries, + map_flags: flags, + id: 0, + pinning: PinningType::ByName as u32, }, _t: PhantomData, } diff --git a/bpf/aya-bpf/src/maps/queue.rs b/bpf/aya-bpf/src/maps/queue.rs index b0d86a40..783c5bbd 100644 --- a/bpf/aya-bpf/src/maps/queue.rs +++ b/bpf/aya-bpf/src/maps/queue.rs @@ -3,6 +3,7 @@ use core::{marker::PhantomData, mem}; use crate::{ bindings::{bpf_map_def, bpf_map_type::BPF_MAP_TYPE_QUEUE}, helpers::{bpf_map_pop_elem, bpf_map_push_elem}, + maps::PinningType, }; #[repr(transparent)] @@ -21,7 +22,22 @@ impl Queue { max_entries, map_flags: flags, id: 0, - pinning: 0, + pinning: PinningType::None as u32, + }, + _t: PhantomData, + } + } + + pub const fn pinned(max_entries: u32, flags: u32) -> Queue { + Queue { + def: bpf_map_def { + type_: BPF_MAP_TYPE_QUEUE, + key_size: 0, + value_size: mem::size_of::() as u32, + max_entries, + map_flags: flags, + id: 0, + pinning: PinningType::ByName as u32, }, _t: PhantomData, } diff --git a/bpf/aya-bpf/src/maps/sock_hash.rs b/bpf/aya-bpf/src/maps/sock_hash.rs index 6b2c30b5..d381b3cb 100644 --- a/bpf/aya-bpf/src/maps/sock_hash.rs +++ b/bpf/aya-bpf/src/maps/sock_hash.rs @@ -5,6 +5,7 @@ use aya_bpf_cty::c_void; use crate::{ bindings::{bpf_map_def, bpf_map_type::BPF_MAP_TYPE_SOCKHASH, bpf_sock_ops}, helpers::{bpf_msg_redirect_hash, bpf_sock_hash_update}, + maps::PinningType, programs::SkMsgContext, BpfContext, }; @@ -25,7 +26,22 @@ impl SockHash { max_entries, map_flags: flags, id: 0, - pinning: 0, + pinning: PinningType::None as u32, + }, + _k: PhantomData, + } + } + + pub const fn pinned(max_entries: u32, flags: u32) -> SockHash { + SockHash { + def: bpf_map_def { + type_: BPF_MAP_TYPE_SOCKHASH, + key_size: mem::size_of::() as u32, + value_size: mem::size_of::() as u32, + max_entries, + map_flags: flags, + id: 0, + pinning: PinningType::ByName as u32, }, _k: PhantomData, } diff --git a/bpf/aya-bpf/src/maps/sock_map.rs b/bpf/aya-bpf/src/maps/sock_map.rs index 2952ad37..97ddd008 100644 --- a/bpf/aya-bpf/src/maps/sock_map.rs +++ b/bpf/aya-bpf/src/maps/sock_map.rs @@ -5,6 +5,7 @@ use aya_bpf_cty::c_void; use crate::{ bindings::{bpf_map_def, bpf_map_type::BPF_MAP_TYPE_SOCKMAP, bpf_sock_ops}, helpers::{bpf_msg_redirect_map, bpf_sock_map_update}, + maps::PinningType, programs::SkMsgContext, BpfContext, }; @@ -24,7 +25,21 @@ impl SockMap { max_entries, map_flags: flags, id: 0, - pinning: 0, + pinning: PinningType::None as u32, + }, + } + } + + pub const fn pinned(max_entries: u32, flags: u32) -> SockMap { + SockMap { + def: bpf_map_def { + type_: BPF_MAP_TYPE_SOCKMAP, + key_size: mem::size_of::() as u32, + value_size: mem::size_of::() as u32, + max_entries, + map_flags: flags, + id: 0, + pinning: PinningType::ByName as u32, }, } } diff --git a/bpf/aya-bpf/src/maps/stack_trace.rs b/bpf/aya-bpf/src/maps/stack_trace.rs index 9557976d..d87e6667 100644 --- a/bpf/aya-bpf/src/maps/stack_trace.rs +++ b/bpf/aya-bpf/src/maps/stack_trace.rs @@ -2,42 +2,53 @@ use core::mem; use crate::{ bindings::{bpf_map_def, bpf_map_type::BPF_MAP_TYPE_STACK_TRACE}, - helpers::{bpf_get_stackid}, + helpers::bpf_get_stackid, + maps::PinningType, BpfContext, }; #[repr(transparent)] pub struct StackTrace { - def: bpf_map_def, + 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 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: PinningType::None as u32, + }, + } + } - 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) - } - } -} \ No newline at end of file + pub const fn pinned(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: PinningType::ByName as u32, + }, + } + } + + 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) + } + } +}