Skip to content

Commit a0dbf6d

Browse files
eddyz87Alexei Starovoitov
authored andcommitted
selftests/bpf: attach struct_ops maps before test prog runs
In test_loader based tests to bpf_map__attach_struct_ops() before call to bpf_prog_test_run_opts() in order to trigger bpf_struct_ops->reg() callbacks on kernel side. This allows to use __retval macro for struct_ops tests. Signed-off-by: Eduard Zingerman <[email protected]> Signed-off-by: Martin KaFai Lau <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 866d571 commit a0dbf6d

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

tools/testing/selftests/bpf/test_loader.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -890,11 +890,13 @@ void run_subtest(struct test_loader *tester,
890890
{
891891
struct test_subspec *subspec = unpriv ? &spec->unpriv : &spec->priv;
892892
struct bpf_program *tprog = NULL, *tprog_iter;
893+
struct bpf_link *link, *links[32] = {};
893894
struct test_spec *spec_iter;
894895
struct cap_state caps = {};
895896
struct bpf_object *tobj;
896897
struct bpf_map *map;
897898
int retval, err, i;
899+
int links_cnt = 0;
898900
bool should_load;
899901

900902
if (!test__start_subtest(subspec->name))
@@ -999,6 +1001,26 @@ void run_subtest(struct test_loader *tester,
9991001
if (restore_capabilities(&caps))
10001002
goto tobj_cleanup;
10011003

1004+
/* Do bpf_map__attach_struct_ops() for each struct_ops map.
1005+
* This should trigger bpf_struct_ops->reg callback on kernel side.
1006+
*/
1007+
bpf_object__for_each_map(map, tobj) {
1008+
if (!bpf_map__autocreate(map) ||
1009+
bpf_map__type(map) != BPF_MAP_TYPE_STRUCT_OPS)
1010+
continue;
1011+
if (links_cnt >= ARRAY_SIZE(links)) {
1012+
PRINT_FAIL("too many struct_ops maps");
1013+
goto tobj_cleanup;
1014+
}
1015+
link = bpf_map__attach_struct_ops(map);
1016+
if (!link) {
1017+
PRINT_FAIL("bpf_map__attach_struct_ops failed for map %s: err=%d\n",
1018+
bpf_map__name(map), err);
1019+
goto tobj_cleanup;
1020+
}
1021+
links[links_cnt++] = link;
1022+
}
1023+
10021024
if (tester->pre_execution_cb) {
10031025
err = tester->pre_execution_cb(tobj);
10041026
if (err) {
@@ -1013,9 +1035,14 @@ void run_subtest(struct test_loader *tester,
10131035
PRINT_FAIL("Unexpected retval: %d != %d\n", retval, subspec->retval);
10141036
goto tobj_cleanup;
10151037
}
1038+
/* redo bpf_map__attach_struct_ops for each test */
1039+
while (links_cnt > 0)
1040+
bpf_link__destroy(links[--links_cnt]);
10161041
}
10171042

10181043
tobj_cleanup:
1044+
while (links_cnt > 0)
1045+
bpf_link__destroy(links[--links_cnt]);
10191046
bpf_object__close(tobj);
10201047
subtest_cleanup:
10211048
test__end_subtest();

0 commit comments

Comments
 (0)