Skip to content

Commit 5bab7a2

Browse files
eddyz87anakryiko
authored andcommitted
selftests/bpf: Test struct_ops map definition with type suffix
Extend struct_ops_module test case to check if it is possible to use '___' suffixes for struct_ops type specification. Signed-off-by: Eduard Zingerman <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Acked-by: David Vernet <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 8db0526 commit 5bab7a2

File tree

3 files changed

+46
-10
lines changed

3 files changed

+46
-10
lines changed

tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,8 @@ static int bpf_dummy_reg(void *kdata)
564564
{
565565
struct bpf_testmod_ops *ops = kdata;
566566

567+
if (ops->test_1)
568+
ops->test_1();
567569
/* Some test cases (ex. struct_ops_maybe_null) may not have test_2
568570
* initialized, so we need to check for NULL.
569571
*/

tools/testing/selftests/bpf/prog_tests/test_struct_ops_module.c

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,29 @@ static void check_map_info(struct bpf_map_info *info)
3030
close(fd);
3131
}
3232

33+
static int attach_ops_and_check(struct struct_ops_module *skel,
34+
struct bpf_map *map,
35+
int expected_test_2_result)
36+
{
37+
struct bpf_link *link;
38+
39+
link = bpf_map__attach_struct_ops(map);
40+
ASSERT_OK_PTR(link, "attach_test_mod_1");
41+
if (!link)
42+
return -1;
43+
44+
/* test_{1,2}() would be called from bpf_dummy_reg() in bpf_testmod.c */
45+
ASSERT_EQ(skel->bss->test_1_result, 0xdeadbeef, "test_1_result");
46+
ASSERT_EQ(skel->bss->test_2_result, expected_test_2_result, "test_2_result");
47+
48+
bpf_link__destroy(link);
49+
return 0;
50+
}
51+
3352
static void test_struct_ops_load(void)
3453
{
3554
struct struct_ops_module *skel;
3655
struct bpf_map_info info = {};
37-
struct bpf_link *link;
3856
int err;
3957
u32 len;
4058

@@ -59,20 +77,17 @@ static void test_struct_ops_load(void)
5977
if (!ASSERT_OK(err, "bpf_map_get_info_by_fd"))
6078
goto cleanup;
6179

62-
link = bpf_map__attach_struct_ops(skel->maps.testmod_1);
63-
ASSERT_OK_PTR(link, "attach_test_mod_1");
64-
80+
check_map_info(&info);
6581
/* test_3() will be called from bpf_dummy_reg() in bpf_testmod.c
6682
*
6783
* In bpf_testmod.c it will pass 4 and 13 (the value of data) to
6884
* .test_2. So, the value of test_2_result should be 20 (4 + 13 +
6985
* 3).
7086
*/
71-
ASSERT_EQ(skel->bss->test_2_result, 20, "check_shadow_variables");
72-
73-
bpf_link__destroy(link);
74-
75-
check_map_info(&info);
87+
if (!attach_ops_and_check(skel, skel->maps.testmod_1, 20))
88+
goto cleanup;
89+
if (!attach_ops_and_check(skel, skel->maps.testmod_2, 12))
90+
goto cleanup;
7691

7792
cleanup:
7893
struct_ops_module__destroy(skel);

tools/testing/selftests/bpf/progs/struct_ops_module.c

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77

88
char _license[] SEC("license") = "GPL";
99

10+
int test_1_result = 0;
1011
int test_2_result = 0;
1112

1213
SEC("struct_ops/test_1")
1314
int BPF_PROG(test_1)
1415
{
15-
return 0xdeadbeef;
16+
test_1_result = 0xdeadbeef;
17+
return 0;
1618
}
1719

1820
SEC("struct_ops/test_2")
@@ -35,3 +37,20 @@ struct bpf_testmod_ops testmod_1 = {
3537
.data = 0x1,
3638
};
3739

40+
SEC("struct_ops/test_2")
41+
void BPF_PROG(test_2_v2, int a, int b)
42+
{
43+
test_2_result = a * b;
44+
}
45+
46+
struct bpf_testmod_ops___v2 {
47+
int (*test_1)(void);
48+
void (*test_2)(int a, int b);
49+
int (*test_maybe_null)(int dummy, struct task_struct *task);
50+
};
51+
52+
SEC(".struct_ops.link")
53+
struct bpf_testmod_ops___v2 testmod_2 = {
54+
.test_1 = (void *)test_1,
55+
.test_2 = (void *)test_2_v2,
56+
};

0 commit comments

Comments
 (0)