Skip to content

Commit 574b52a

Browse files
committed
aya, aya-obj: seal CreatableMap and rename new_legacy
- Rename new_legacy to new_from_params to reflect actual usage - Move CreatableMap into sealed module with public wrapper trait - Replace impl_create_map macro and 8 manual trait impls with impl_creatable_map macro
1 parent 87f9634 commit 574b52a

File tree

5 files changed

+82
-149
lines changed

5 files changed

+82
-149
lines changed

aya-obj/src/maps.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,8 @@ impl Map {
281281
}
282282
}
283283

284-
/// Creates a new legacy map definition programmatically.
285-
pub const fn new_legacy(
284+
/// Creates a new map definition from raw parameters.
285+
pub const fn new_from_params(
286286
map_type: u32,
287287
key_size: u32,
288288
value_size: u32,

aya/src/maps/mod.rs

Lines changed: 41 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,20 @@ pub trait InnerMap: sealed::InnerMap {}
115115

116116
impl<T: sealed::InnerMap> InnerMap for T {}
117117

118+
/// Trait for map types that can be created standalone from userspace.
119+
///
120+
/// This is used to create inner maps for map-of-maps types.
121+
///
122+
/// This trait is sealed and cannot be implemented outside of this crate.
123+
pub trait CreatableMap: sealed::CreatableMap {
124+
/// Creates a standalone map with the given `max_entries` capacity and `flags`.
125+
fn create(max_entries: u32, flags: u32) -> Result<Self, MapError> {
126+
<Self as sealed::CreatableMap>::create(max_entries, flags)
127+
}
128+
}
129+
130+
impl<T: sealed::CreatableMap> CreatableMap for T {}
131+
118132
mod sealed {
119133
use super::{MapData, MapError, MapFd};
120134

@@ -130,24 +144,9 @@ mod sealed {
130144
fn fd(&self) -> &MapFd;
131145
}
132146

133-
pub(super) trait CreatableMap: Sized {
134-
const MAP_TYPE: u32;
135-
const KEY_SIZE: u32;
136-
const VALUE_SIZE: u32;
137-
const CREATE_NAME: &'static str;
138-
139-
fn new_from_map_data(map_data: MapData) -> Result<Self, MapError>;
140-
141-
fn create(max_entries: u32, flags: u32) -> Result<Self, MapError> {
142-
let obj = aya_obj::Map::new_legacy(
143-
Self::MAP_TYPE,
144-
Self::KEY_SIZE,
145-
Self::VALUE_SIZE,
146-
max_entries,
147-
flags,
148-
);
149-
Self::new_from_map_data(MapData::create(obj, Self::CREATE_NAME, None)?)
150-
}
147+
#[expect(unnameable_types, reason = "intentionally unnameable sealed trait")]
148+
pub trait CreatableMap: Sized {
149+
fn create(max_entries: u32, flags: u32) -> Result<Self, MapError>;
151150
}
152151
}
153152

@@ -718,105 +717,35 @@ impl sealed::InnerMap for MapFd {
718717
}
719718
}
720719

721-
impl<V: Pod> sealed::CreatableMap for Array<MapData, V> {
722-
const MAP_TYPE: u32 = bpf_map_type::BPF_MAP_TYPE_ARRAY as u32;
723-
const KEY_SIZE: u32 = size_of::<u32>() as u32;
724-
const VALUE_SIZE: u32 = size_of::<V>() as u32;
725-
const CREATE_NAME: &'static str = "standalone_array";
726-
fn new_from_map_data(map_data: MapData) -> Result<Self, MapError> {
727-
Self::new(map_data)
728-
}
729-
}
730-
731-
impl<V: Pod> sealed::CreatableMap for PerCpuArray<MapData, V> {
732-
const MAP_TYPE: u32 = bpf_map_type::BPF_MAP_TYPE_PERCPU_ARRAY as u32;
733-
const KEY_SIZE: u32 = size_of::<u32>() as u32;
734-
const VALUE_SIZE: u32 = size_of::<V>() as u32;
735-
const CREATE_NAME: &'static str = "standalone_percpu_array";
736-
fn new_from_map_data(map_data: MapData) -> Result<Self, MapError> {
737-
Self::new(map_data)
738-
}
739-
}
740-
741-
impl<V: Pod> sealed::CreatableMap for BloomFilter<MapData, V> {
742-
const MAP_TYPE: u32 = bpf_map_type::BPF_MAP_TYPE_BLOOM_FILTER as u32;
743-
const KEY_SIZE: u32 = 0;
744-
const VALUE_SIZE: u32 = size_of::<V>() as u32;
745-
const CREATE_NAME: &'static str = "standalone_bloom_filter";
746-
fn new_from_map_data(map_data: MapData) -> Result<Self, MapError> {
747-
Self::new(map_data)
748-
}
749-
}
750-
751-
impl<V: Pod> sealed::CreatableMap for Queue<MapData, V> {
752-
const MAP_TYPE: u32 = bpf_map_type::BPF_MAP_TYPE_QUEUE as u32;
753-
const KEY_SIZE: u32 = 0;
754-
const VALUE_SIZE: u32 = size_of::<V>() as u32;
755-
const CREATE_NAME: &'static str = "standalone_queue";
756-
fn new_from_map_data(map_data: MapData) -> Result<Self, MapError> {
757-
Self::new(map_data)
758-
}
759-
}
760-
761-
impl<V: Pod> sealed::CreatableMap for Stack<MapData, V> {
762-
const MAP_TYPE: u32 = bpf_map_type::BPF_MAP_TYPE_STACK as u32;
763-
const KEY_SIZE: u32 = 0;
764-
const VALUE_SIZE: u32 = size_of::<V>() as u32;
765-
const CREATE_NAME: &'static str = "standalone_stack";
766-
fn new_from_map_data(map_data: MapData) -> Result<Self, MapError> {
767-
Self::new(map_data)
768-
}
769-
}
770-
771-
impl<K: Pod, V: Pod> sealed::CreatableMap for HashMap<MapData, K, V> {
772-
const MAP_TYPE: u32 = bpf_map_type::BPF_MAP_TYPE_HASH as u32;
773-
const KEY_SIZE: u32 = size_of::<K>() as u32;
774-
const VALUE_SIZE: u32 = size_of::<V>() as u32;
775-
const CREATE_NAME: &'static str = "standalone_hash";
776-
fn new_from_map_data(map_data: MapData) -> Result<Self, MapError> {
777-
Self::new(map_data)
778-
}
779-
}
780-
781-
impl<K: Pod, V: Pod> sealed::CreatableMap for PerCpuHashMap<MapData, K, V> {
782-
const MAP_TYPE: u32 = bpf_map_type::BPF_MAP_TYPE_PERCPU_HASH as u32;
783-
const KEY_SIZE: u32 = size_of::<K>() as u32;
784-
const VALUE_SIZE: u32 = size_of::<V>() as u32;
785-
const CREATE_NAME: &'static str = "standalone_percpu_hash";
786-
fn new_from_map_data(map_data: MapData) -> Result<Self, MapError> {
787-
Self::new(map_data)
788-
}
789-
}
790-
791-
impl<K: Pod, V: Pod> sealed::CreatableMap for LpmTrie<MapData, K, V> {
792-
const MAP_TYPE: u32 = bpf_map_type::BPF_MAP_TYPE_LPM_TRIE as u32;
793-
const KEY_SIZE: u32 = size_of::<lpm_trie::Key<K>>() as u32;
794-
const VALUE_SIZE: u32 = size_of::<V>() as u32;
795-
const CREATE_NAME: &'static str = "standalone_lpm_trie";
796-
fn new_from_map_data(map_data: MapData) -> Result<Self, MapError> {
797-
Self::new(map_data)
798-
}
799-
}
800-
801-
macro_rules! impl_create_map {
802-
($ty:ident < MapData $(, $param:ident : Pod)* >) => {
803-
impl<$($param: Pod),*> $ty<MapData, $($param),*> {
804-
/// Creates a standalone map with the given `max_entries` capacity and `flags`.
805-
pub fn create(max_entries: u32, flags: u32) -> Result<Self, MapError> {
806-
<Self as sealed::CreatableMap>::create(max_entries, flags)
720+
macro_rules! impl_creatable_map {
721+
($ty:ident<MapData $(, $p:ident: Pod)*>, $map_type:expr, $key_size:expr, $value_size:expr, $name:expr) => {
722+
impl<$($p: Pod),*> sealed::CreatableMap for $ty<MapData, $($p),*> {
723+
fn create(max_entries: u32, flags: u32) -> Result<Self, MapError> {
724+
let obj = aya_obj::Map::new_from_params(
725+
$map_type as u32, $key_size, $value_size, max_entries, flags,
726+
);
727+
Self::new(MapData::create(obj, $name, None)?)
807728
}
808729
}
809730
};
810731
}
811732

812-
impl_create_map!(Array<MapData, V: Pod>);
813-
impl_create_map!(PerCpuArray<MapData, V: Pod>);
814-
impl_create_map!(BloomFilter<MapData, V: Pod>);
815-
impl_create_map!(Queue<MapData, V: Pod>);
816-
impl_create_map!(Stack<MapData, V: Pod>);
817-
impl_create_map!(HashMap<MapData, K: Pod, V: Pod>);
818-
impl_create_map!(PerCpuHashMap<MapData, K: Pod, V: Pod>);
819-
impl_create_map!(LpmTrie<MapData, K: Pod, V: Pod>);
733+
impl_creatable_map!(Array<MapData, V: Pod>,
734+
bpf_map_type::BPF_MAP_TYPE_ARRAY, size_of::<u32>() as u32, size_of::<V>() as u32, "standalone_array");
735+
impl_creatable_map!(PerCpuArray<MapData, V: Pod>,
736+
bpf_map_type::BPF_MAP_TYPE_PERCPU_ARRAY, size_of::<u32>() as u32, size_of::<V>() as u32, "standalone_percpu_array");
737+
impl_creatable_map!(BloomFilter<MapData, V: Pod>,
738+
bpf_map_type::BPF_MAP_TYPE_BLOOM_FILTER, 0, size_of::<V>() as u32, "standalone_bloom_filter");
739+
impl_creatable_map!(Queue<MapData, V: Pod>,
740+
bpf_map_type::BPF_MAP_TYPE_QUEUE, 0, size_of::<V>() as u32, "standalone_queue");
741+
impl_creatable_map!(Stack<MapData, V: Pod>,
742+
bpf_map_type::BPF_MAP_TYPE_STACK, 0, size_of::<V>() as u32, "standalone_stack");
743+
impl_creatable_map!(HashMap<MapData, K: Pod, V: Pod>,
744+
bpf_map_type::BPF_MAP_TYPE_HASH, size_of::<K>() as u32, size_of::<V>() as u32, "standalone_hash");
745+
impl_creatable_map!(PerCpuHashMap<MapData, K: Pod, V: Pod>,
746+
bpf_map_type::BPF_MAP_TYPE_PERCPU_HASH, size_of::<K>() as u32, size_of::<V>() as u32, "standalone_percpu_hash");
747+
impl_creatable_map!(LpmTrie<MapData, K: Pod, V: Pod>,
748+
bpf_map_type::BPF_MAP_TYPE_LPM_TRIE, size_of::<lpm_trie::Key<K>>() as u32, size_of::<V>() as u32, "standalone_lpm_trie");
820749

821750
pub(crate) const fn check_bounds(map: &MapData, index: u32) -> Result<(), MapError> {
822751
let max_entries = map.obj.max_entries();

test/integration-test/src/tests/btf_map_of_maps.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use aya::{
22
Ebpf,
3-
maps::{Array, ArrayOfMaps, HashOfMaps, MapData},
3+
maps::{Array, ArrayOfMaps, CreatableMap, HashOfMaps, MapData},
44
programs::UProbe,
55
};
66

xtask/public-api/aya-obj.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8394,7 +8394,7 @@ pub const fn aya_obj::maps::Map::key_size(&self) -> u32
83948394
pub const fn aya_obj::maps::Map::map_flags(&self) -> u32
83958395
pub const fn aya_obj::maps::Map::map_type(&self) -> u32
83968396
pub const fn aya_obj::maps::Map::max_entries(&self) -> u32
8397-
pub const fn aya_obj::maps::Map::new_legacy(map_type: u32, key_size: u32, value_size: u32, max_entries: u32, flags: u32) -> Self
8397+
pub const fn aya_obj::maps::Map::new_from_params(map_type: u32, key_size: u32, value_size: u32, max_entries: u32, flags: u32) -> Self
83988398
pub const fn aya_obj::maps::Map::pinning(&self) -> aya_obj::maps::PinningType
83998399
pub const fn aya_obj::maps::Map::section_index(&self) -> usize
84008400
pub const fn aya_obj::maps::Map::section_kind(&self) -> aya_obj::EbpfSectionKind
@@ -9575,7 +9575,7 @@ pub const fn aya_obj::maps::Map::key_size(&self) -> u32
95759575
pub const fn aya_obj::maps::Map::map_flags(&self) -> u32
95769576
pub const fn aya_obj::maps::Map::map_type(&self) -> u32
95779577
pub const fn aya_obj::maps::Map::max_entries(&self) -> u32
9578-
pub const fn aya_obj::maps::Map::new_legacy(map_type: u32, key_size: u32, value_size: u32, max_entries: u32, flags: u32) -> Self
9578+
pub const fn aya_obj::maps::Map::new_from_params(map_type: u32, key_size: u32, value_size: u32, max_entries: u32, flags: u32) -> Self
95799579
pub const fn aya_obj::maps::Map::pinning(&self) -> aya_obj::maps::PinningType
95809580
pub const fn aya_obj::maps::Map::section_index(&self) -> usize
95819581
pub const fn aya_obj::maps::Map::section_kind(&self) -> aya_obj::EbpfSectionKind

0 commit comments

Comments
 (0)