Skip to content

Commit 1863acc

Browse files
eddyz87anakryiko
authored andcommitted
selftests/bpf: Test autocreate behavior for struct_ops maps
Check that bpf_map__set_autocreate() can be used to disable automatic creation for struct_ops maps. Signed-off-by: Eduard Zingerman <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent c1b93c0 commit 1863acc

File tree

2 files changed

+118
-0
lines changed

2 files changed

+118
-0
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
#include <test_progs.h>
4+
#include "struct_ops_autocreate.skel.h"
5+
6+
static void cant_load_full_object(void)
7+
{
8+
struct struct_ops_autocreate *skel;
9+
char *log = NULL;
10+
int err;
11+
12+
skel = struct_ops_autocreate__open();
13+
if (!ASSERT_OK_PTR(skel, "struct_ops_autocreate__open"))
14+
return;
15+
16+
if (start_libbpf_log_capture())
17+
goto cleanup;
18+
/* The testmod_2 map BTF type (struct bpf_testmod_ops___v2) doesn't
19+
* match the BTF of the actual struct bpf_testmod_ops defined in the
20+
* kernel, so we should fail to load it if we don't disable autocreate
21+
* for that map.
22+
*/
23+
err = struct_ops_autocreate__load(skel);
24+
log = stop_libbpf_log_capture();
25+
if (!ASSERT_ERR(err, "struct_ops_autocreate__load"))
26+
goto cleanup;
27+
28+
ASSERT_HAS_SUBSTR(log, "libbpf: struct_ops init_kern", "init_kern message");
29+
ASSERT_EQ(err, -ENOTSUP, "errno should be ENOTSUP");
30+
31+
cleanup:
32+
free(log);
33+
struct_ops_autocreate__destroy(skel);
34+
}
35+
36+
static void can_load_partial_object(void)
37+
{
38+
struct struct_ops_autocreate *skel;
39+
struct bpf_link *link = NULL;
40+
int err;
41+
42+
skel = struct_ops_autocreate__open();
43+
if (!ASSERT_OK_PTR(skel, "struct_ops_autocreate__open_opts"))
44+
return;
45+
46+
err = bpf_program__set_autoload(skel->progs.test_2, false);
47+
if (!ASSERT_OK(err, "bpf_program__set_autoload"))
48+
goto cleanup;
49+
50+
err = bpf_map__set_autocreate(skel->maps.testmod_2, false);
51+
if (!ASSERT_OK(err, "bpf_map__set_autocreate"))
52+
goto cleanup;
53+
54+
err = struct_ops_autocreate__load(skel);
55+
if (ASSERT_OK(err, "struct_ops_autocreate__load"))
56+
goto cleanup;
57+
58+
link = bpf_map__attach_struct_ops(skel->maps.testmod_1);
59+
if (!ASSERT_OK_PTR(link, "bpf_map__attach_struct_ops"))
60+
goto cleanup;
61+
62+
/* test_1() would be called from bpf_dummy_reg2() in bpf_testmod.c */
63+
ASSERT_EQ(skel->bss->test_1_result, 42, "test_1_result");
64+
65+
cleanup:
66+
bpf_link__destroy(link);
67+
struct_ops_autocreate__destroy(skel);
68+
}
69+
70+
void test_struct_ops_autocreate(void)
71+
{
72+
if (test__start_subtest("cant_load_full_object"))
73+
cant_load_full_object();
74+
if (test__start_subtest("can_load_partial_object"))
75+
can_load_partial_object();
76+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
#include <linux/bpf.h>
4+
#include <bpf/bpf_helpers.h>
5+
#include <bpf/bpf_tracing.h>
6+
7+
char _license[] SEC("license") = "GPL";
8+
9+
int test_1_result = 0;
10+
11+
SEC("struct_ops/test_1")
12+
int BPF_PROG(test_1)
13+
{
14+
test_1_result = 42;
15+
return 0;
16+
}
17+
18+
SEC("struct_ops/test_1")
19+
int BPF_PROG(test_2)
20+
{
21+
return 0;
22+
}
23+
24+
struct bpf_testmod_ops___v1 {
25+
int (*test_1)(void);
26+
};
27+
28+
struct bpf_testmod_ops___v2 {
29+
int (*test_1)(void);
30+
int (*does_not_exist)(void);
31+
};
32+
33+
SEC(".struct_ops.link")
34+
struct bpf_testmod_ops___v1 testmod_1 = {
35+
.test_1 = (void *)test_1
36+
};
37+
38+
SEC(".struct_ops.link")
39+
struct bpf_testmod_ops___v2 testmod_2 = {
40+
.test_1 = (void *)test_1,
41+
.does_not_exist = (void *)test_2
42+
};

0 commit comments

Comments
 (0)