Skip to content

Commit acda5af

Browse files
committed
map: Add max_entries() for Map objects
Signed-off-by: Yonatan Goldschmidt <yonatan.goldschmidt@wiz.io>
1 parent d1fedf4 commit acda5af

File tree

2 files changed

+62
-2
lines changed

2 files changed

+62
-2
lines changed

libbpf-rs/src/map.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@ impl<'obj> OpenMap<'obj> {
103103
Some(data)
104104
}
105105
}
106+
107+
/// Retrieve max_entries of the map.
108+
pub fn max_entries(&self) -> u32 {
109+
unsafe { libbpf_sys::bpf_map__max_entries(self.ptr.as_ptr()) }
110+
}
106111
}
107112

108113
impl<'obj> OpenMapMut<'obj> {
@@ -438,6 +443,9 @@ pub trait MapCore: Debug + AsFd + private::Sealed {
438443
/// Retrieve the size of the map's values.
439444
fn value_size(&self) -> u32;
440445

446+
/// Retrieve max_entries of the map.
447+
fn max_entries(&self) -> u32;
448+
441449
/// Fetch extra map information
442450
#[inline]
443451
fn info(&self) -> Result<MapInfo> {
@@ -934,6 +942,11 @@ where
934942
fn value_size(&self) -> u32 {
935943
unsafe { libbpf_sys::bpf_map__value_size(self.ptr.as_ptr()) }
936944
}
945+
946+
#[inline]
947+
fn max_entries(&self) -> u32 {
948+
unsafe { libbpf_sys::bpf_map__max_entries(self.ptr.as_ptr()) }
949+
}
937950
}
938951

939952
impl AsRawLibbpf for Map<'_> {
@@ -967,6 +980,7 @@ pub struct MapHandle {
967980
ty: MapType,
968981
key_size: u32,
969982
value_size: u32,
983+
max_entries: u32,
970984
}
971985

972986
impl MapHandle {
@@ -1014,6 +1028,7 @@ impl MapHandle {
10141028
ty: map_type,
10151029
key_size,
10161030
value_size,
1031+
max_entries,
10171032
})
10181033
}
10191034

@@ -1064,6 +1079,7 @@ impl MapHandle {
10641079
ty: info.map_type(),
10651080
key_size: info.info.key_size,
10661081
value_size: info.info.value_size,
1082+
max_entries: info.info.max_entries,
10671083
})
10681084
}
10691085

@@ -1116,6 +1132,11 @@ impl MapCore for MapHandle {
11161132
fn value_size(&self) -> u32 {
11171133
self.value_size
11181134
}
1135+
1136+
#[inline]
1137+
fn max_entries(&self) -> u32 {
1138+
self.max_entries
1139+
}
11191140
}
11201141

11211142
impl AsFd for MapHandle {
@@ -1141,6 +1162,7 @@ where
11411162
ty: other.map_type(),
11421163
key_size: other.key_size(),
11431164
value_size: other.value_size(),
1165+
max_entries: other.max_entries(),
11441166
})
11451167
}
11461168
}
@@ -1158,6 +1180,7 @@ impl TryFrom<&MapHandle> for MapHandle {
11581180
ty: other.map_type(),
11591181
key_size: other.key_size(),
11601182
value_size: other.value_size(),
1183+
max_entries: other.max_entries(),
11611184
})
11621185
}
11631186
}

libbpf-rs/tests/test.rs

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1576,12 +1576,14 @@ fn test_object_map_handle_clone() {
15761576
assert_eq!(map.map_type(), handle1.map_type());
15771577
assert_eq!(map.key_size(), handle1.key_size());
15781578
assert_eq!(map.value_size(), handle1.value_size());
1579+
assert_eq!(map.max_entries(), handle1.max_entries());
15791580

15801581
let handle2 = MapHandle::try_from(&handle1).expect("failed to duplicate existing handle");
15811582
assert_eq!(handle1.name(), handle2.name());
15821583
assert_eq!(handle1.map_type(), handle2.map_type());
15831584
assert_eq!(handle1.key_size(), handle2.key_size());
15841585
assert_eq!(handle1.value_size(), handle2.value_size());
1586+
assert_eq!(handle1.max_entries(), handle2.max_entries());
15851587

15861588
let info1 = map.info().expect("failed to get map info from map");
15871589
let info2 = handle2.info().expect("failed to get map info from handle");
@@ -1999,10 +2001,10 @@ fn test_map_autocreate_disable() {
19992001
open_obj.load().expect("failed to load object");
20002002
}
20012003

2002-
/// Check that we can resize a map.
2004+
/// Check that we can resize a map value.
20032005
#[tag(root)]
20042006
#[test]
2005-
fn test_map_resize() {
2007+
fn test_map_resize_value() {
20062008
bump_rlimit_mlock();
20072009

20082010
let mut open_obj = open_test_object("map_auto_pin.bpf.o");
@@ -2021,6 +2023,41 @@ fn test_map_resize() {
20212023
assert_eq!(new_len, len * 2);
20222024
}
20232025

2026+
/// Check that we can resize map max entries.
2027+
#[tag(root)]
2028+
#[test]
2029+
fn test_object_map_max_entries() {
2030+
bump_rlimit_mlock();
2031+
2032+
let mut obj = open_test_object("runqslower.bpf.o");
2033+
2034+
// resize the map to have twice the number of entries
2035+
let mut start = obj
2036+
.maps_mut()
2037+
.find(|map| map.name() == OsStr::new("start"))
2038+
.expect("failed to find `start` map");
2039+
let initial_max_entries = start.max_entries();
2040+
let new_max_entries = initial_max_entries * 2;
2041+
start
2042+
.set_max_entries(new_max_entries)
2043+
.expect("failed to set max entries");
2044+
// check that it reflects on the open map
2045+
assert_eq!(start.max_entries(), new_max_entries);
2046+
2047+
// check that it reflects after loading the map
2048+
let obj = obj.load().expect("failed to load object");
2049+
let start = obj
2050+
.maps()
2051+
.find(|map| map.name() == OsStr::new("start"))
2052+
.expect("failed to find `start` map");
2053+
assert_eq!(start.max_entries(), new_max_entries);
2054+
2055+
// check that it reflects after recreating the map handle from map id
2056+
let start = MapHandle::from_map_id(start.info().expect("failed to get map info").info.id)
2057+
.expect("failed to get map handle from id");
2058+
assert!(start.max_entries() == new_max_entries);
2059+
}
2060+
20242061
/// Check that we are able to attach using ksyscall
20252062
#[tag(root)]
20262063
#[test]

0 commit comments

Comments
 (0)