Skip to content

Commit 733e5e8

Browse files
eddyz87anakryiko
authored andcommitted
selftests/bpf: Test case for SEC("?.struct_ops")
Check that "?.struct_ops" and "?.struct_ops.link" section names define struct_ops maps with autocreate == false after open. Signed-off-by: Eduard Zingerman <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 6ebaa3f commit 733e5e8

File tree

2 files changed

+56
-6
lines changed

2 files changed

+56
-6
lines changed

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

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,24 @@ static void cant_load_full_object(void)
3434
struct_ops_autocreate__destroy(skel);
3535
}
3636

37+
static int check_test_1_link(struct struct_ops_autocreate *skel, struct bpf_map *map)
38+
{
39+
struct bpf_link *link;
40+
int err;
41+
42+
link = bpf_map__attach_struct_ops(skel->maps.testmod_1);
43+
if (!ASSERT_OK_PTR(link, "bpf_map__attach_struct_ops"))
44+
return -1;
45+
46+
/* test_1() would be called from bpf_dummy_reg2() in bpf_testmod.c */
47+
err = ASSERT_EQ(skel->bss->test_1_result, 42, "test_1_result");
48+
bpf_link__destroy(link);
49+
return err;
50+
}
51+
3752
static void can_load_partial_object(void)
3853
{
3954
struct struct_ops_autocreate *skel;
40-
struct bpf_link *link = NULL;
4155
int err;
4256

4357
skel = struct_ops_autocreate__open();
@@ -58,15 +72,39 @@ static void can_load_partial_object(void)
5872
ASSERT_TRUE(bpf_program__autoload(skel->progs.test_1), "test_1 actual autoload");
5973
ASSERT_FALSE(bpf_program__autoload(skel->progs.test_2), "test_2 actual autoload");
6074

61-
link = bpf_map__attach_struct_ops(skel->maps.testmod_1);
62-
if (!ASSERT_OK_PTR(link, "bpf_map__attach_struct_ops"))
75+
check_test_1_link(skel, skel->maps.testmod_1);
76+
77+
cleanup:
78+
struct_ops_autocreate__destroy(skel);
79+
}
80+
81+
static void optional_maps(void)
82+
{
83+
struct struct_ops_autocreate *skel;
84+
int err;
85+
86+
skel = struct_ops_autocreate__open();
87+
if (!ASSERT_OK_PTR(skel, "struct_ops_autocreate__open"))
88+
return;
89+
90+
ASSERT_TRUE(bpf_map__autocreate(skel->maps.testmod_1), "testmod_1 autocreate");
91+
ASSERT_TRUE(bpf_map__autocreate(skel->maps.testmod_2), "testmod_2 autocreate");
92+
ASSERT_FALSE(bpf_map__autocreate(skel->maps.optional_map), "optional_map autocreate");
93+
ASSERT_FALSE(bpf_map__autocreate(skel->maps.optional_map2), "optional_map2 autocreate");
94+
95+
err = bpf_map__set_autocreate(skel->maps.testmod_1, false);
96+
err |= bpf_map__set_autocreate(skel->maps.testmod_2, false);
97+
err |= bpf_map__set_autocreate(skel->maps.optional_map2, true);
98+
if (!ASSERT_OK(err, "bpf_map__set_autocreate"))
6399
goto cleanup;
64100

65-
/* test_1() would be called from bpf_dummy_reg2() in bpf_testmod.c */
66-
ASSERT_EQ(skel->bss->test_1_result, 42, "test_1_result");
101+
err = struct_ops_autocreate__load(skel);
102+
if (ASSERT_OK(err, "struct_ops_autocreate__load"))
103+
goto cleanup;
104+
105+
check_test_1_link(skel, skel->maps.optional_map2);
67106

68107
cleanup:
69-
bpf_link__destroy(link);
70108
struct_ops_autocreate__destroy(skel);
71109
}
72110

@@ -116,4 +154,6 @@ void test_struct_ops_autocreate(void)
116154
can_load_partial_object();
117155
if (test__start_subtest("autoload_and_shadow_vars"))
118156
autoload_and_shadow_vars();
157+
if (test__start_subtest("optional_maps"))
158+
optional_maps();
119159
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,13 @@ struct bpf_testmod_ops___v2 testmod_2 = {
4040
.test_1 = (void *)test_1,
4141
.does_not_exist = (void *)test_2
4242
};
43+
44+
SEC("?.struct_ops")
45+
struct bpf_testmod_ops___v1 optional_map = {
46+
.test_1 = (void *)test_1,
47+
};
48+
49+
SEC("?.struct_ops.link")
50+
struct bpf_testmod_ops___v1 optional_map2 = {
51+
.test_1 = (void *)test_1,
52+
};

0 commit comments

Comments
 (0)