59
59
#define MAX_TEST_RUNS 8
60
60
#define POINTER_VALUE 0xcafe4all
61
61
#define TEST_DATA_LEN 64
62
+ #define MAX_FUNC_INFOS 8
63
+ #define MAX_BTF_STRINGS 256
64
+ #define MAX_BTF_TYPES 256
62
65
63
66
#define INSN_OFF_MASK ((__s16)0xFFFF)
64
67
#define INSN_IMM_MASK ((__s32)0xFFFFFFFF)
65
68
#define SKIP_INSNS () BPF_RAW_INSN(0xde, 0xa, 0xd, 0xbeef, 0xdeadbeef)
66
69
70
+ #define DEFAULT_LIBBPF_LOG_LEVEL 4
71
+ #define VERBOSE_LIBBPF_LOG_LEVEL 1
72
+
67
73
#define F_NEEDS_EFFICIENT_UNALIGNED_ACCESS (1 << 0)
68
74
#define F_LOAD_WITH_STRICT_ALIGNMENT (1 << 1)
69
75
@@ -158,6 +164,14 @@ struct bpf_test {
158
164
};
159
165
enum bpf_attach_type expected_attach_type ;
160
166
const char * kfunc ;
167
+ struct bpf_func_info func_info [MAX_FUNC_INFOS ];
168
+ int func_info_cnt ;
169
+ char btf_strings [MAX_BTF_STRINGS ];
170
+ /* A set of BTF types to load when specified,
171
+ * use macro definitions from test_btf.h,
172
+ * must end with BTF_END_RAW
173
+ */
174
+ __u32 btf_types [MAX_BTF_TYPES ];
161
175
};
162
176
163
177
/* Note we want this to be 64 bit aligned so that the end of our array is
@@ -687,34 +701,66 @@ static __u32 btf_raw_types[] = {
687
701
BTF_MEMBER_ENC (71 , 13 , 128 ), /* struct prog_test_member __kptr_ref *ptr; */
688
702
};
689
703
690
- static int load_btf (void )
704
+ static char bpf_vlog [UINT_MAX >> 8 ];
705
+
706
+ static int load_btf_spec (__u32 * types , int types_len ,
707
+ const char * strings , int strings_len )
691
708
{
692
709
struct btf_header hdr = {
693
710
.magic = BTF_MAGIC ,
694
711
.version = BTF_VERSION ,
695
712
.hdr_len = sizeof (struct btf_header ),
696
- .type_len = sizeof ( btf_raw_types ) ,
697
- .str_off = sizeof ( btf_raw_types ) ,
698
- .str_len = sizeof ( btf_str_sec ) ,
713
+ .type_len = types_len ,
714
+ .str_off = types_len ,
715
+ .str_len = strings_len ,
699
716
};
700
717
void * ptr , * raw_btf ;
701
718
int btf_fd ;
719
+ LIBBPF_OPTS (bpf_btf_load_opts , opts ,
720
+ .log_buf = bpf_vlog ,
721
+ .log_size = sizeof (bpf_vlog ),
722
+ .log_level = (verbose
723
+ ? VERBOSE_LIBBPF_LOG_LEVEL
724
+ : DEFAULT_LIBBPF_LOG_LEVEL ),
725
+ );
702
726
703
- ptr = raw_btf = malloc (sizeof (hdr ) + sizeof (btf_raw_types ) +
704
- sizeof (btf_str_sec ));
727
+ raw_btf = malloc (sizeof (hdr ) + types_len + strings_len );
705
728
729
+ ptr = raw_btf ;
706
730
memcpy (ptr , & hdr , sizeof (hdr ));
707
731
ptr += sizeof (hdr );
708
- memcpy (ptr , btf_raw_types , hdr .type_len );
732
+ memcpy (ptr , types , hdr .type_len );
709
733
ptr += hdr .type_len ;
710
- memcpy (ptr , btf_str_sec , hdr .str_len );
734
+ memcpy (ptr , strings , hdr .str_len );
711
735
ptr += hdr .str_len ;
712
736
713
- btf_fd = bpf_btf_load (raw_btf , ptr - raw_btf , NULL );
714
- free (raw_btf );
737
+ btf_fd = bpf_btf_load (raw_btf , ptr - raw_btf , & opts );
715
738
if (btf_fd < 0 )
716
- return -1 ;
717
- return btf_fd ;
739
+ printf ("Failed to load BTF spec: '%s'\n" , strerror (errno ));
740
+
741
+ free (raw_btf );
742
+
743
+ return btf_fd < 0 ? -1 : btf_fd ;
744
+ }
745
+
746
+ static int load_btf (void )
747
+ {
748
+ return load_btf_spec (btf_raw_types , sizeof (btf_raw_types ),
749
+ btf_str_sec , sizeof (btf_str_sec ));
750
+ }
751
+
752
+ static int load_btf_for_test (struct bpf_test * test )
753
+ {
754
+ int types_num = 0 ;
755
+
756
+ while (types_num < MAX_BTF_TYPES &&
757
+ test -> btf_types [types_num ] != BTF_END_RAW )
758
+ ++ types_num ;
759
+
760
+ int types_len = types_num * sizeof (test -> btf_types [0 ]);
761
+
762
+ return load_btf_spec (test -> btf_types , types_len ,
763
+ test -> btf_strings , sizeof (test -> btf_strings ));
718
764
}
719
765
720
766
static int create_map_spin_lock (void )
@@ -793,8 +839,6 @@ static int create_map_kptr(void)
793
839
return fd ;
794
840
}
795
841
796
- static char bpf_vlog [UINT_MAX >> 8 ];
797
-
798
842
static void do_test_fixup (struct bpf_test * test , enum bpf_prog_type prog_type ,
799
843
struct bpf_insn * prog , int * map_fds )
800
844
{
@@ -1360,7 +1404,7 @@ static bool check_xlated_program(struct bpf_test *test, int fd_prog)
1360
1404
static void do_test_single (struct bpf_test * test , bool unpriv ,
1361
1405
int * passes , int * errors )
1362
1406
{
1363
- int fd_prog , expected_ret , alignment_prevented_execution ;
1407
+ int fd_prog , btf_fd , expected_ret , alignment_prevented_execution ;
1364
1408
int prog_len , prog_type = test -> prog_type ;
1365
1409
struct bpf_insn * prog = test -> insns ;
1366
1410
LIBBPF_OPTS (bpf_prog_load_opts , opts );
@@ -1372,8 +1416,10 @@ static void do_test_single(struct bpf_test *test, bool unpriv,
1372
1416
__u32 pflags ;
1373
1417
int i , err ;
1374
1418
1419
+ fd_prog = -1 ;
1375
1420
for (i = 0 ; i < MAX_NR_MAPS ; i ++ )
1376
1421
map_fds [i ] = -1 ;
1422
+ btf_fd = -1 ;
1377
1423
1378
1424
if (!prog_type )
1379
1425
prog_type = BPF_PROG_TYPE_SOCKET_FILTER ;
@@ -1406,11 +1452,11 @@ static void do_test_single(struct bpf_test *test, bool unpriv,
1406
1452
1407
1453
opts .expected_attach_type = test -> expected_attach_type ;
1408
1454
if (verbose )
1409
- opts .log_level = 1 ;
1455
+ opts .log_level = VERBOSE_LIBBPF_LOG_LEVEL ;
1410
1456
else if (expected_ret == VERBOSE_ACCEPT )
1411
1457
opts .log_level = 2 ;
1412
1458
else
1413
- opts .log_level = 4 ;
1459
+ opts .log_level = DEFAULT_LIBBPF_LOG_LEVEL ;
1414
1460
opts .prog_flags = pflags ;
1415
1461
1416
1462
if (prog_type == BPF_PROG_TYPE_TRACING && test -> kfunc ) {
@@ -1428,6 +1474,19 @@ static void do_test_single(struct bpf_test *test, bool unpriv,
1428
1474
opts .attach_btf_id = attach_btf_id ;
1429
1475
}
1430
1476
1477
+ if (test -> btf_types [0 ] != 0 ) {
1478
+ btf_fd = load_btf_for_test (test );
1479
+ if (btf_fd < 0 )
1480
+ goto fail_log ;
1481
+ opts .prog_btf_fd = btf_fd ;
1482
+ }
1483
+
1484
+ if (test -> func_info_cnt != 0 ) {
1485
+ opts .func_info = test -> func_info ;
1486
+ opts .func_info_cnt = test -> func_info_cnt ;
1487
+ opts .func_info_rec_size = sizeof (test -> func_info [0 ]);
1488
+ }
1489
+
1431
1490
opts .log_buf = bpf_vlog ;
1432
1491
opts .log_size = sizeof (bpf_vlog );
1433
1492
fd_prog = bpf_prog_load (prog_type , NULL , "GPL" , prog , prog_len , & opts );
@@ -1539,6 +1598,7 @@ static void do_test_single(struct bpf_test *test, bool unpriv,
1539
1598
if (test -> fill_insns )
1540
1599
free (test -> fill_insns );
1541
1600
close (fd_prog );
1601
+ close (btf_fd );
1542
1602
for (i = 0 ; i < MAX_NR_MAPS ; i ++ )
1543
1603
close (map_fds [i ]);
1544
1604
sched_yield ();
0 commit comments