@@ -3,6 +3,7 @@ use std::ffi::c_void;
3
3
use std:: fs;
4
4
use std:: hint;
5
5
use std:: io:: Read ;
6
+ use std:: mem:: size_of;
6
7
use std:: os:: fd:: AsRawFd ;
7
8
use std:: path:: Path ;
8
9
use std:: path:: PathBuf ;
@@ -23,6 +24,7 @@ use libbpf_rs::Iter;
23
24
use libbpf_rs:: Linker ;
24
25
use libbpf_rs:: Map ;
25
26
use libbpf_rs:: MapFlags ;
27
+ use libbpf_rs:: MapInfo ;
26
28
use libbpf_rs:: MapType ;
27
29
use libbpf_rs:: Object ;
28
30
use libbpf_rs:: ObjectBuilder ;
@@ -226,6 +228,41 @@ fn test_object_map_delete_batch() {
226
228
assert ! ( start. keys( ) . collect:: <Vec <_>>( ) . is_empty( ) )
227
229
}
228
230
231
+ /// Test whether `MapInfo` works properly
232
+ #[ test]
233
+ pub fn test_object_map_info ( ) {
234
+ let opts = libbpf_sys:: bpf_map_create_opts {
235
+ sz : size_of :: < libbpf_sys:: bpf_map_create_opts > ( ) as libbpf_sys:: size_t ,
236
+ map_flags : libbpf_sys:: BPF_ANY ,
237
+ btf_fd : 0 ,
238
+ btf_key_type_id : 0 ,
239
+ btf_value_type_id : 0 ,
240
+ btf_vmlinux_value_type_id : 0 ,
241
+ inner_map_fd : 0 ,
242
+ map_extra : 0 ,
243
+ numa_node : 0 ,
244
+ map_ifindex : 0 ,
245
+ } ;
246
+
247
+ let map = Map :: create ( MapType :: Hash , Some ( "simple_map" ) , 8 , 64 , 1024 , & opts) . unwrap ( ) ;
248
+ let map_info = MapInfo :: new ( map. fd ( ) ) . unwrap ( ) ;
249
+ let name_received = map_info. name ( ) . unwrap ( ) ;
250
+ assert_eq ! ( name_received, "simple_map" ) ;
251
+ assert_eq ! ( map_info. map_type( ) , MapType :: Hash ) ;
252
+ assert_eq ! ( map_info. flags( ) & MapFlags :: ANY , MapFlags :: ANY ) ;
253
+
254
+ let map_info = & map_info. info ;
255
+ assert_eq ! ( map_info. key_size, 8 ) ;
256
+ assert_eq ! ( map_info. value_size, 64 ) ;
257
+ assert_eq ! ( map_info. max_entries, 1024 ) ;
258
+ assert_eq ! ( map_info. btf_id, 0 ) ;
259
+ assert_eq ! ( map_info. btf_key_type_id, 0 ) ;
260
+ assert_eq ! ( map_info. btf_value_type_id, 0 ) ;
261
+ assert_eq ! ( map_info. btf_vmlinux_value_type_id, 0 ) ;
262
+ assert_eq ! ( map_info. map_extra, 0 ) ;
263
+ assert_eq ! ( map_info. ifindex, 0 ) ;
264
+ }
265
+
229
266
#[ test]
230
267
fn test_object_percpu_lookup ( ) {
231
268
bump_rlimit_mlock ( ) ;
@@ -242,7 +279,7 @@ fn test_object_percpu_lookup() {
242
279
res. len( ) ,
243
280
num_possible_cpus( ) . expect( "must be one value per cpu" )
244
281
) ;
245
- assert_eq ! ( res[ 0 ] . len( ) , std :: mem :: size_of:: <u32 >( ) ) ;
282
+ assert_eq ! ( res[ 0 ] . len( ) , size_of:: <u32 >( ) ) ;
246
283
}
247
284
248
285
#[ test]
@@ -783,7 +820,7 @@ fn test_object_task_iter() {
783
820
. expect ( "Failed to read from iterator" ) ;
784
821
785
822
assert ! ( bytes_read > 0 ) ;
786
- assert_eq ! ( bytes_read % std :: mem :: size_of:: <IndexPidPair >( ) , 0 ) ;
823
+ assert_eq ! ( bytes_read % size_of:: <IndexPidPair >( ) , 0 ) ;
787
824
let items: & [ IndexPidPair ] =
788
825
plain:: slice_from_bytes ( buf. as_slice ( ) ) . expect ( "Input slice cannot satisfy length" ) ;
789
826
@@ -800,7 +837,7 @@ fn test_object_map_iter() {
800
837
801
838
// Create a map for iteration test.
802
839
let opts = libbpf_sys:: bpf_map_create_opts {
803
- sz : std :: mem :: size_of :: < libbpf_sys:: bpf_map_create_opts > ( ) as libbpf_sys:: size_t ,
840
+ sz : size_of :: < libbpf_sys:: bpf_map_create_opts > ( ) as libbpf_sys:: size_t ,
804
841
map_flags : libbpf_sys:: BPF_F_NO_PREALLOC ,
805
842
..Default :: default ( )
806
843
} ;
@@ -836,7 +873,7 @@ fn test_object_map_iter() {
836
873
. expect ( "Failed to read from iterator" ) ;
837
874
838
875
assert ! ( bytes_read > 0 ) ;
839
- assert_eq ! ( bytes_read % std :: mem :: size_of:: <u32 >( ) , 0 ) ;
876
+ assert_eq ! ( bytes_read % size_of:: <u32 >( ) , 0 ) ;
840
877
// Convert buf to &[u32]
841
878
let buf =
842
879
plain:: slice_from_bytes :: < u32 > ( buf. as_slice ( ) ) . expect ( "Input slice cannot satisfy length" ) ;
@@ -850,7 +887,7 @@ fn test_object_map_create_and_pin() {
850
887
bump_rlimit_mlock ( ) ;
851
888
852
889
let opts = libbpf_sys:: bpf_map_create_opts {
853
- sz : std :: mem :: size_of :: < libbpf_sys:: bpf_map_create_opts > ( ) as libbpf_sys:: size_t ,
890
+ sz : size_of :: < libbpf_sys:: bpf_map_create_opts > ( ) as libbpf_sys:: size_t ,
854
891
map_flags : libbpf_sys:: BPF_F_NO_PREALLOC ,
855
892
..Default :: default ( )
856
893
} ;
@@ -895,7 +932,7 @@ fn test_object_map_create_without_name() {
895
932
bump_rlimit_mlock ( ) ;
896
933
897
934
let opts = libbpf_sys:: bpf_map_create_opts {
898
- sz : std :: mem :: size_of :: < libbpf_sys:: bpf_map_create_opts > ( ) as libbpf_sys:: size_t ,
935
+ sz : size_of :: < libbpf_sys:: bpf_map_create_opts > ( ) as libbpf_sys:: size_t ,
899
936
map_flags : libbpf_sys:: BPF_F_NO_PREALLOC ,
900
937
btf_fd : 0 ,
901
938
btf_key_type_id : 0 ,
0 commit comments