From ba5bf6f91240948bf7f748df05836a89d993ea21 Mon Sep 17 00:00:00 2001 From: Alessandro Decina Date: Sun, 21 Apr 2024 19:37:06 +1000 Subject: [PATCH] maps: make LegacyMap hold MapDef Now both BtfMap and LegacyMap hold MapDef --- aya-obj/src/maps.rs | 17 ++++++++++++++++- aya-obj/src/obj.rs | 34 +++++++++++++++++++--------------- aya/src/maps/bloom_filter.rs | 6 ++++-- aya/src/maps/hash_map/mod.rs | 3 ++- aya/src/maps/lpm_trie.rs | 6 ++++-- aya/src/maps/mod.rs | 3 ++- aya/src/sys/bpf.rs | 3 ++- 7 files changed, 49 insertions(+), 23 deletions(-) diff --git a/aya-obj/src/maps.rs b/aya-obj/src/maps.rs index fc6bfcc3..d3ab998c 100644 --- a/aya-obj/src/maps.rs +++ b/aya-obj/src/maps.rs @@ -78,6 +78,21 @@ pub struct MapDef { pub btf_value_type_id: Option, } +impl From for MapDef { + fn from(def: bpf_map_def) -> Self { + Self { + map_type: def.map_type, + key_size: def.key_size, + value_size: def.value_size, + max_entries: def.max_entries, + map_flags: def.map_flags, + pinning: def.pinning, + btf_key_type_id: None, + btf_value_type_id: None, + } + } +} + /// The pinning type /// /// Upon pinning a map, a file representation is created for the map, @@ -266,7 +281,7 @@ impl Map { #[derive(Debug, Clone)] pub struct LegacyMap { /// The definition of the map - pub def: bpf_map_def, + pub def: MapDef, /// The section index pub section_index: usize, /// The section kind diff --git a/aya-obj/src/obj.rs b/aya-obj/src/obj.rs index 92aefacc..8868fe5a 100644 --- a/aya-obj/src/obj.rs +++ b/aya-obj/src/obj.rs @@ -794,14 +794,14 @@ impl Object { .name .as_ref() .ok_or(ParseError::MapSymbolNameNotFound { i: *i })?; - let def = parse_map_def(name, data)?; + let def = parse_legacy_map_def(name, data)?; maps.insert( name.to_string(), Map::Legacy(LegacyMap { section_index: section.index.0, section_kind: section.kind, symbol_index: Some(sym.index), - def, + def: def.into(), data: Vec::new(), }), ); @@ -1192,12 +1192,12 @@ fn parse_data_map_section(section: &Section) -> Result { section_kind: section.kind, // Data maps don't require symbols to be relocated symbol_index: None, - def, + def: def.into(), data, })) } -fn parse_map_def(name: &str, data: &[u8]) -> Result { +fn parse_legacy_map_def(name: &str, data: &[u8]) -> Result { if data.len() < MINIMUM_MAP_SIZE { return Err(ParseError::InvalidMapDefinition { name: name.to_owned(), @@ -1324,7 +1324,8 @@ pub fn parse_map_info(info: bpf_map_info, pinned: PinningType) -> Map { map_flags: info.map_flags, pinning: pinned, id: info.id, - }, + } + .into(), section_index: 0, symbol_index: None, section_kind: EbpfSectionKind::Undefined, @@ -1502,7 +1503,7 @@ mod tests { #[test] fn test_parse_map_def_error() { assert_matches!( - parse_map_def("foo", &[]), + parse_legacy_map_def("foo", &[]), Err(ParseError::InvalidMapDefinition { .. }) ); } @@ -1520,7 +1521,7 @@ mod tests { }; assert_eq!( - parse_map_def("foo", &bytes_of(&def)[..MINIMUM_MAP_SIZE]).unwrap(), + parse_legacy_map_def("foo", &bytes_of(&def)[..MINIMUM_MAP_SIZE]).unwrap(), def ); } @@ -1537,7 +1538,7 @@ mod tests { pinning: PinningType::ByName, }; - assert_eq!(parse_map_def("foo", bytes_of(&def)).unwrap(), def); + assert_eq!(parse_legacy_map_def("foo", bytes_of(&def)).unwrap(), def); } #[test] @@ -1554,7 +1555,7 @@ mod tests { let mut buf = [0u8; 128]; unsafe { ptr::write_unaligned(buf.as_mut_ptr() as *mut _, def) }; - assert_eq!(parse_map_def("foo", &buf).unwrap(), def); + assert_eq!(parse_legacy_map_def("foo", &buf).unwrap(), def); } #[test] @@ -1573,14 +1574,15 @@ mod tests { section_index: 0, section_kind: EbpfSectionKind::Data, symbol_index: None, - def: bpf_map_def { + def: MapDef { map_type: _map_type, key_size: 4, value_size, max_entries: 1, map_flags: 0, - id: 0, pinning: PinningType::None, + btf_key_type_id: None, + btf_value_type_id: None, }, data, })) if data == map_data && value_size == map_data.len() as u32 @@ -1755,7 +1757,7 @@ mod tests { fake_sym(&mut obj, 0, 0, "foo", mem::size_of::() as u64); fake_sym(&mut obj, 0, 28, "bar", mem::size_of::() as u64); fake_sym(&mut obj, 0, 60, "baz", mem::size_of::() as u64); - let def = &bpf_map_def { + let def = bpf_map_def { map_type: 1, key_size: 2, value_size: 3, @@ -1763,7 +1765,7 @@ mod tests { map_flags: 5, ..Default::default() }; - let map_data = bytes_of(def).to_vec(); + let map_data = bytes_of(&def).to_vec(); let mut buf = vec![]; buf.extend(&map_data); buf.extend(&map_data); @@ -1782,9 +1784,10 @@ mod tests { assert!(obj.maps.contains_key("foo")); assert!(obj.maps.contains_key("bar")); assert!(obj.maps.contains_key("baz")); + let def = MapDef::from(def); for map in obj.maps.values() { assert_matches!(map, Map::Legacy(m) => { - assert_eq!(&m.def, def); + assert_eq!(m.def, def); }) } } @@ -2549,7 +2552,8 @@ mod tests { map_flags: BPF_F_RDONLY_PROG, id: 1, pinning: PinningType::None, - }, + } + .into(), section_index: 1, section_kind: EbpfSectionKind::Rodata, symbol_index: Some(1), diff --git a/aya/src/maps/bloom_filter.rs b/aya/src/maps/bloom_filter.rs index 817c785c..a8b8d4d9 100644 --- a/aya/src/maps/bloom_filter.rs +++ b/aya/src/maps/bloom_filter.rs @@ -104,7 +104,8 @@ mod tests { value_size: 4, max_entries: 1024, ..Default::default() - }, + } + .into(), section_index: 0, section_kind: EbpfSectionKind::Maps, symbol_index: None, @@ -148,7 +149,8 @@ mod tests { value_size: 4, max_entries: 1024, ..Default::default() - }, + } + .into(), section_index: 0, section_kind: EbpfSectionKind::Maps, symbol_index: None, diff --git a/aya/src/maps/hash_map/mod.rs b/aya/src/maps/hash_map/mod.rs index 93f345bd..901c39aa 100644 --- a/aya/src/maps/hash_map/mod.rs +++ b/aya/src/maps/hash_map/mod.rs @@ -73,7 +73,8 @@ mod test_utils { value_size: 4, max_entries: 1024, ..Default::default() - }, + } + .into(), section_index: 0, section_kind: EbpfSectionKind::Maps, data: Vec::new(), diff --git a/aya/src/maps/lpm_trie.rs b/aya/src/maps/lpm_trie.rs index c3dd528a..4fc867d5 100644 --- a/aya/src/maps/lpm_trie.rs +++ b/aya/src/maps/lpm_trie.rs @@ -221,7 +221,8 @@ mod tests { value_size: 4, max_entries: 1024, ..Default::default() - }, + } + .into(), section_index: 0, section_kind: EbpfSectionKind::Maps, symbol_index: None, @@ -277,7 +278,8 @@ mod tests { value_size: 4, max_entries: 1024, ..Default::default() - }, + } + .into(), section_index: 0, section_kind: EbpfSectionKind::Maps, symbol_index: None, diff --git a/aya/src/maps/mod.rs b/aya/src/maps/mod.rs index 7c1d2a84..24c223f2 100644 --- a/aya/src/maps/mod.rs +++ b/aya/src/maps/mod.rs @@ -1043,7 +1043,8 @@ mod tests { value_size: 4, max_entries: 1024, ..Default::default() - }, + } + .into(), section_index: 0, section_kind: EbpfSectionKind::Maps, symbol_index: Some(0), diff --git a/aya/src/sys/bpf.rs b/aya/src/sys/bpf.rs index 1351a236..2d35d805 100644 --- a/aya/src/sys/bpf.rs +++ b/aya/src/sys/bpf.rs @@ -764,7 +764,8 @@ pub(crate) fn is_bpf_global_data_supported() -> bool { value_size: 32, max_entries: 1, ..Default::default() - }, + } + .into(), section_index: 0, section_kind: EbpfSectionKind::Maps, symbol_index: None,