Skip to content

Commit 17c9105

Browse files
committed
selftests/bpf: validate generic bpf_object and subskel APIs work together
JIRA: https://issues.redhat.com/browse/RHEL-85485 commit 80a5456 Author: Andrii Nakryiko <andrii@kernel.org> Date: Tue Oct 22 21:39:08 2024 -0700 selftests/bpf: validate generic bpf_object and subskel APIs work together Add a new subtest validating that bpf_object loaded and initialized through generic APIs is still interoperable with BPF subskeleton, including initialization and reading of global variables. Signed-off-by: Andrii Nakryiko <andrii@kernel.org> Link: https://lore.kernel.org/r/20241023043908.3834423-4-andrii@kernel.org Signed-off-by: Alexei Starovoitov <ast@kernel.org> Signed-off-by: Viktor Malik <vmalik@redhat.com>
1 parent dbc73dc commit 17c9105

File tree

1 file changed

+75
-1
lines changed

1 file changed

+75
-1
lines changed

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

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ static int subskeleton_lib_subresult(struct bpf_object *obj)
4646
return result;
4747
}
4848

49-
void test_subskeleton(void)
49+
/* initialize and load through skeleton, then instantiate subskeleton out of it */
50+
static void subtest_skel_subskeleton(void)
5051
{
5152
int err, result;
5253
struct test_subskeleton *skel;
@@ -76,3 +77,76 @@ void test_subskeleton(void)
7677
cleanup:
7778
test_subskeleton__destroy(skel);
7879
}
80+
81+
/* initialize and load through generic bpf_object API, then instantiate subskeleton out of it */
82+
static void subtest_obj_subskeleton(void)
83+
{
84+
int err, result;
85+
const void *elf_bytes;
86+
size_t elf_bytes_sz = 0, rodata_sz = 0, bss_sz = 0;
87+
struct bpf_object *obj;
88+
const struct bpf_map *map;
89+
const struct bpf_program *prog;
90+
struct bpf_link *link = NULL;
91+
struct test_subskeleton__rodata *rodata;
92+
struct test_subskeleton__bss *bss;
93+
94+
elf_bytes = test_subskeleton__elf_bytes(&elf_bytes_sz);
95+
if (!ASSERT_OK_PTR(elf_bytes, "elf_bytes"))
96+
return;
97+
98+
obj = bpf_object__open_mem(elf_bytes, elf_bytes_sz, NULL);
99+
if (!ASSERT_OK_PTR(obj, "obj_open_mem"))
100+
return;
101+
102+
map = bpf_object__find_map_by_name(obj, ".rodata");
103+
if (!ASSERT_OK_PTR(map, "rodata_map_by_name"))
104+
goto cleanup;
105+
106+
rodata = bpf_map__initial_value(map, &rodata_sz);
107+
if (!ASSERT_OK_PTR(rodata, "rodata_get"))
108+
goto cleanup;
109+
110+
rodata->rovar1 = 10;
111+
rodata->var1 = 1;
112+
subskeleton_lib_setup(obj);
113+
114+
err = bpf_object__load(obj);
115+
if (!ASSERT_OK(err, "obj_load"))
116+
goto cleanup;
117+
118+
prog = bpf_object__find_program_by_name(obj, "handler1");
119+
if (!ASSERT_OK_PTR(prog, "prog_by_name"))
120+
goto cleanup;
121+
122+
link = bpf_program__attach(prog);
123+
if (!ASSERT_OK_PTR(link, "prog_attach"))
124+
goto cleanup;
125+
126+
/* trigger tracepoint */
127+
usleep(1);
128+
129+
map = bpf_object__find_map_by_name(obj, ".bss");
130+
if (!ASSERT_OK_PTR(map, "bss_map_by_name"))
131+
goto cleanup;
132+
133+
bss = bpf_map__initial_value(map, &bss_sz);
134+
if (!ASSERT_OK_PTR(rodata, "rodata_get"))
135+
goto cleanup;
136+
137+
result = subskeleton_lib_subresult(obj) * 10;
138+
ASSERT_EQ(bss->out1, result, "out1");
139+
140+
cleanup:
141+
bpf_link__destroy(link);
142+
bpf_object__close(obj);
143+
}
144+
145+
146+
void test_subskeleton(void)
147+
{
148+
if (test__start_subtest("skel_subskel"))
149+
subtest_skel_subskeleton();
150+
if (test__start_subtest("obj_subskel"))
151+
subtest_obj_subskeleton();
152+
}

0 commit comments

Comments
 (0)