Skip to main content

aya_ebpf/maps/
mod.rs

1pub(crate) mod def {
2    use core::cell::UnsafeCell;
3
4    use aya_ebpf_cty::c_void;
5
6    use crate::bindings::bpf_map_def;
7
8    #[repr(u32)]
9    pub(crate) enum PinningType {
10        None = 0,
11        ByName = 1,
12    }
13
14    #[repr(transparent)]
15    pub(crate) struct MapDef(UnsafeCell<bpf_map_def>);
16
17    unsafe impl Sync for MapDef {}
18
19    impl MapDef {
20        /// Creates a new map definition with key type `K` and value type `V`.
21        pub(crate) const fn new<K, V>(
22            type_: u32,
23            max_entries: u32,
24            map_flags: u32,
25            pinning: PinningType,
26        ) -> Self {
27            let key_size = size_of::<K>() as u32;
28            let value_size = size_of::<V>() as u32;
29            Self(UnsafeCell::new(bpf_map_def {
30                type_,
31                key_size,
32                value_size,
33                max_entries,
34                map_flags,
35                id: 0,
36                pinning: pinning as u32,
37            }))
38        }
39
40        pub(crate) const fn as_ptr(&self) -> *mut c_void {
41            self.0.get().cast()
42        }
43    }
44}
45
46pub(crate) use def::{MapDef, PinningType};
47
48macro_rules! map_constructors {
49    (
50        $key:ty,
51        $value:ty,
52        $map_type:expr
53        $(, extra_flags $extra_flags:expr)?
54        $(, phantom $phantom:ident)?
55        $(, with_docs { $($with_doc:tt)* })?
56        $(, pinned_docs { $($pinned_doc:tt)* })?
57        $(,)?
58    ) => {
59        $($($with_doc)*)?
60        pub const fn with_max_entries(max_entries: u32, flags: u32) -> Self {
61            Self::new(max_entries, flags, PinningType::None)
62        }
63
64        $($($pinned_doc)*)?
65        pub const fn pinned(max_entries: u32, flags: u32) -> Self {
66            Self::new(max_entries, flags, PinningType::ByName)
67        }
68
69        const fn new(max_entries: u32, flags: u32, pinning: PinningType) -> Self {
70            $(let flags = flags | $extra_flags;)?
71            Self {
72                def: MapDef::new::<$key, $value>($map_type, max_entries, flags, pinning),
73                $($phantom: core::marker::PhantomData,)?
74            }
75        }
76    };
77}
78
79pub mod array;
80pub mod bloom_filter;
81pub mod cgroup_array;
82pub mod cgroup_storage;
83pub mod hash_map;
84pub mod lpm_trie;
85pub mod per_cpu_array;
86pub mod perf;
87pub mod program_array;
88pub mod queue;
89pub mod reuseport_sock_array;
90pub mod ring_buf;
91pub mod sock_hash;
92pub mod sock_map;
93pub mod stack;
94pub mod stack_trace;
95pub mod xdp;
96
97pub use array::Array;
98pub use bloom_filter::BloomFilter;
99pub use cgroup_array::CgroupArray;
100#[expect(
101    deprecated,
102    reason = "re-exporting the deprecated cgroup storage map types"
103)]
104pub use cgroup_storage::{CgroupStorage, PerCpuCgroupStorage};
105pub use hash_map::{HashMap, LruHashMap, LruPerCpuHashMap, PerCpuHashMap};
106pub use lpm_trie::LpmTrie;
107pub use per_cpu_array::PerCpuArray;
108pub use perf::{PerfEventArray, PerfEventByteArray};
109pub use program_array::ProgramArray;
110pub use queue::Queue;
111pub use reuseport_sock_array::ReusePortSockArray;
112pub use ring_buf::RingBuf;
113pub use sock_hash::SockHash;
114pub use sock_map::SockMap;
115pub use stack::Stack;
116pub use stack_trace::StackTrace;
117pub use xdp::{CpuMap, DevMap, DevMapHash, XskMap};
118
119mod private {
120    /// Sealed trait to prevent external implementations of [`super::Map`].
121    #[expect(
122        unnameable_types,
123        reason = "sealed trait pattern requires pub trait in private mod"
124    )]
125    pub trait Map {
126        /// The key type declared in this map's definition.
127        type Key;
128        /// The value type declared in this map's definition.
129        type Value;
130    }
131}
132
133/// Marker trait for all eBPF maps that can be used in a map of maps.
134///
135/// This trait is sealed and cannot be implemented outside this crate.
136pub trait Map: private::Map {}
137
138impl<T: private::Map> Map for T {}