@@ -131,6 +131,8 @@ struct btf_raw_test {
131
131
__u32 max_entries ;
132
132
bool btf_load_err ;
133
133
bool map_create_err ;
134
+ bool ordered_map ;
135
+ bool lossless_map ;
134
136
int hdr_len_delta ;
135
137
int type_off_delta ;
136
138
int str_off_delta ;
@@ -2093,8 +2095,7 @@ struct pprint_mapv {
2093
2095
} aenum ;
2094
2096
};
2095
2097
2096
- static struct btf_raw_test pprint_test = {
2097
- .descr = "BTF pretty print test #1" ,
2098
+ static struct btf_raw_test pprint_test_template = {
2098
2099
.raw_types = {
2099
2100
/* unsighed char */ /* [1] */
2100
2101
BTF_TYPE_INT_ENC (NAME_TBD , 0 , 0 , 8 , 1 ),
@@ -2146,15 +2147,47 @@ static struct btf_raw_test pprint_test = {
2146
2147
},
2147
2148
.str_sec = "\0unsigned char\0unsigned short\0unsigned int\0int\0unsigned long long\0uint8_t\0uint16_t\0uint32_t\0int32_t\0uint64_t\0ui64\0ui8a\0ENUM_ZERO\0ENUM_ONE\0ENUM_TWO\0ENUM_THREE\0pprint_mapv\0ui32\0ui16\0si32\0unused_bits2a\0bits28\0unused_bits2b\0aenum" ,
2148
2149
.str_sec_size = sizeof ("\0unsigned char\0unsigned short\0unsigned int\0int\0unsigned long long\0uint8_t\0uint16_t\0uint32_t\0int32_t\0uint64_t\0ui64\0ui8a\0ENUM_ZERO\0ENUM_ONE\0ENUM_TWO\0ENUM_THREE\0pprint_mapv\0ui32\0ui16\0si32\0unused_bits2a\0bits28\0unused_bits2b\0aenum" ),
2149
- .map_type = BPF_MAP_TYPE_ARRAY ,
2150
- .map_name = "pprint_test" ,
2151
2150
.key_size = sizeof (unsigned int ),
2152
2151
.value_size = sizeof (struct pprint_mapv ),
2153
2152
.key_type_id = 3 , /* unsigned int */
2154
2153
.value_type_id = 16 , /* struct pprint_mapv */
2155
2154
.max_entries = 128 * 1024 ,
2156
2155
};
2157
2156
2157
+ static struct btf_pprint_test_meta {
2158
+ const char * descr ;
2159
+ enum bpf_map_type map_type ;
2160
+ const char * map_name ;
2161
+ bool ordered_map ;
2162
+ bool lossless_map ;
2163
+ } pprint_tests_meta [] = {
2164
+ {
2165
+ .descr = "BTF pretty print array" ,
2166
+ .map_type = BPF_MAP_TYPE_ARRAY ,
2167
+ .map_name = "pprint_test_array" ,
2168
+ .ordered_map = true,
2169
+ .lossless_map = true,
2170
+ },
2171
+
2172
+ {
2173
+ .descr = "BTF pretty print hash" ,
2174
+ .map_type = BPF_MAP_TYPE_HASH ,
2175
+ .map_name = "pprint_test_hash" ,
2176
+ .ordered_map = false,
2177
+ .lossless_map = true,
2178
+ },
2179
+
2180
+ {
2181
+ .descr = "BTF pretty print lru hash" ,
2182
+ .map_type = BPF_MAP_TYPE_LRU_HASH ,
2183
+ .map_name = "pprint_test_lru_hash" ,
2184
+ .ordered_map = false,
2185
+ .lossless_map = false,
2186
+ },
2187
+
2188
+ };
2189
+
2190
+
2158
2191
static void set_pprint_mapv (struct pprint_mapv * v , uint32_t i )
2159
2192
{
2160
2193
v -> ui32 = i ;
@@ -2166,10 +2199,12 @@ static void set_pprint_mapv(struct pprint_mapv *v, uint32_t i)
2166
2199
v -> aenum = i & 0x03 ;
2167
2200
}
2168
2201
2169
- static int test_pprint (void )
2202
+ static int do_test_pprint (void )
2170
2203
{
2171
- const struct btf_raw_test * test = & pprint_test ;
2204
+ const struct btf_raw_test * test = & pprint_test_template ;
2172
2205
struct bpf_create_map_attr create_attr = {};
2206
+ unsigned int key , nr_read_elems ;
2207
+ bool ordered_map , lossless_map ;
2173
2208
int map_fd = -1 , btf_fd = -1 ;
2174
2209
struct pprint_mapv mapv = {};
2175
2210
unsigned int raw_btf_size ;
@@ -2178,7 +2213,6 @@ static int test_pprint(void)
2178
2213
char pin_path [255 ];
2179
2214
size_t line_len = 0 ;
2180
2215
char * line = NULL ;
2181
- unsigned int key ;
2182
2216
uint8_t * raw_btf ;
2183
2217
ssize_t nread ;
2184
2218
int err , ret ;
@@ -2251,14 +2285,18 @@ static int test_pprint(void)
2251
2285
goto done ;
2252
2286
}
2253
2287
2254
- key = 0 ;
2288
+ nr_read_elems = 0 ;
2289
+ ordered_map = test -> ordered_map ;
2290
+ lossless_map = test -> lossless_map ;
2255
2291
do {
2256
2292
ssize_t nexpected_line ;
2293
+ unsigned int next_key ;
2257
2294
2258
- set_pprint_mapv (& mapv , key );
2295
+ next_key = ordered_map ? nr_read_elems : atoi (line );
2296
+ set_pprint_mapv (& mapv , next_key );
2259
2297
nexpected_line = snprintf (expected_line , sizeof (expected_line ),
2260
2298
"%u: {%u,0,%d,0x%x,0x%x,0x%x,{%lu|[%u,%u,%u,%u,%u,%u,%u,%u]},%s}\n" ,
2261
- key ,
2299
+ next_key ,
2262
2300
mapv .ui32 , mapv .si32 ,
2263
2301
mapv .unused_bits2a , mapv .bits28 , mapv .unused_bits2b ,
2264
2302
mapv .ui64 ,
@@ -2281,11 +2319,12 @@ static int test_pprint(void)
2281
2319
}
2282
2320
2283
2321
nread = getline (& line , & line_len , pin_file );
2284
- } while (++ key < test -> max_entries && nread > 0 );
2322
+ } while (++ nr_read_elems < test -> max_entries && nread > 0 );
2285
2323
2286
- if (CHECK (key < test -> max_entries ,
2287
- "Unexpected EOF. key:%u test->max_entries:%u" ,
2288
- key , test -> max_entries )) {
2324
+ if (lossless_map &&
2325
+ CHECK (nr_read_elems < test -> max_entries ,
2326
+ "Unexpected EOF. nr_read_elems:%u test->max_entries:%u" ,
2327
+ nr_read_elems , test -> max_entries )) {
2289
2328
err = -1 ;
2290
2329
goto done ;
2291
2330
}
@@ -2314,6 +2353,24 @@ static int test_pprint(void)
2314
2353
return err ;
2315
2354
}
2316
2355
2356
+ static int test_pprint (void )
2357
+ {
2358
+ unsigned int i ;
2359
+ int err = 0 ;
2360
+
2361
+ for (i = 0 ; i < ARRAY_SIZE (pprint_tests_meta ); i ++ ) {
2362
+ pprint_test_template .descr = pprint_tests_meta [i ].descr ;
2363
+ pprint_test_template .map_type = pprint_tests_meta [i ].map_type ;
2364
+ pprint_test_template .map_name = pprint_tests_meta [i ].map_name ;
2365
+ pprint_test_template .ordered_map = pprint_tests_meta [i ].ordered_map ;
2366
+ pprint_test_template .lossless_map = pprint_tests_meta [i ].lossless_map ;
2367
+
2368
+ err |= count_result (do_test_pprint ());
2369
+ }
2370
+
2371
+ return err ;
2372
+ }
2373
+
2317
2374
static void usage (const char * cmd )
2318
2375
{
2319
2376
fprintf (stderr , "Usage: %s [-l] [[-r test_num (1 - %zu)] | [-g test_num (1 - %zu)] | [-f test_num (1 - %zu)] | [-p]]\n" ,
@@ -2409,7 +2466,7 @@ int main(int argc, char **argv)
2409
2466
err |= test_file ();
2410
2467
2411
2468
if (args .pprint_test )
2412
- err |= count_result ( test_pprint () );
2469
+ err |= test_pprint ();
2413
2470
2414
2471
if (args .raw_test || args .get_info_test || args .file_test ||
2415
2472
args .pprint_test )
0 commit comments