Skip to content

Commit 96d28b6

Browse files
ameryhungKernel Patches Daemon
authored andcommitted
selftests/bpf: Test multi_st_ops and calling kfuncs from different programs
Test multi_st_ops and demonstrate how different bpf programs can call a kfuncs that refers to the struct_ops instance in the same source file by id. The id is defined as a global vairable and initialized before attaching the skeleton. Kfuncs that take the id can hide the argument with a macro to make it almost transparent to bpf program developers. The test involves two struct_ops returning different values from .test_1. In syscall and tracing programs, check if the correct value is returned by a kfunc that calls .test_1. Signed-off-by: Amery Hung <[email protected]>
1 parent 3d41992 commit 96d28b6

File tree

3 files changed

+191
-0
lines changed

3 files changed

+191
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
#include <test_progs.h>
4+
#include "struct_ops_id_ops_mapping1.skel.h"
5+
#include "struct_ops_id_ops_mapping2.skel.h"
6+
7+
static void test_st_ops_id_ops_mapping(void)
8+
{
9+
struct struct_ops_id_ops_mapping1 *skel1 = NULL;
10+
struct struct_ops_id_ops_mapping2 *skel2 = NULL;
11+
LIBBPF_OPTS(bpf_test_run_opts, topts);
12+
struct bpf_map_info info = {};
13+
__u32 len = sizeof(info);
14+
int err, pid, prog1_fd, prog2_fd;
15+
16+
skel1 = struct_ops_id_ops_mapping1__open_and_load();
17+
if (!ASSERT_OK_PTR(skel1, "struct_ops_id_ops_mapping1__open"))
18+
goto out;
19+
20+
skel2 = struct_ops_id_ops_mapping2__open_and_load();
21+
if (!ASSERT_OK_PTR(skel2, "struct_ops_id_ops_mapping2__open"))
22+
goto out;
23+
24+
err = bpf_map_get_info_by_fd(bpf_map__fd(skel1->maps.st_ops_map),
25+
&info, &len);
26+
if (!ASSERT_OK(err, "bpf_map_get_info_by_fd"))
27+
goto out;
28+
29+
skel1->bss->st_ops_id = info.id;
30+
31+
err = bpf_map_get_info_by_fd(bpf_map__fd(skel2->maps.st_ops_map),
32+
&info, &len);
33+
if (!ASSERT_OK(err, "bpf_map_get_info_by_fd"))
34+
goto out;
35+
36+
skel2->bss->st_ops_id = info.id;
37+
38+
err = struct_ops_id_ops_mapping1__attach(skel1);
39+
if (!ASSERT_OK(err, "struct_ops_id_ops_mapping1__attach"))
40+
goto out;
41+
42+
err = struct_ops_id_ops_mapping2__attach(skel2);
43+
if (!ASSERT_OK(err, "struct_ops_id_ops_mapping2__attach"))
44+
goto out;
45+
46+
/* run tracing prog that calls .test_1 and checks return */
47+
pid = getpid();
48+
skel1->bss->test_pid = pid;
49+
skel2->bss->test_pid = pid;
50+
sys_gettid();
51+
skel1->bss->test_pid = 0;
52+
skel2->bss->test_pid = 0;
53+
54+
/* run syscall_prog that calls .test_1 and checks return */
55+
prog1_fd = bpf_program__fd(skel1->progs.syscall_prog);
56+
err = bpf_prog_test_run_opts(prog1_fd, &topts);
57+
ASSERT_OK(err, "bpf_prog_test_run_opts");
58+
59+
prog2_fd = bpf_program__fd(skel2->progs.syscall_prog);
60+
err = bpf_prog_test_run_opts(prog2_fd, &topts);
61+
ASSERT_OK(err, "bpf_prog_test_run_opts");
62+
63+
ASSERT_EQ(skel1->bss->test_err, 0, "skel1->bss->test_err");
64+
ASSERT_EQ(skel2->bss->test_err, 0, "skel2->bss->test_err");
65+
66+
out:
67+
if (skel1)
68+
struct_ops_id_ops_mapping1__destroy(skel1);
69+
if (skel2)
70+
struct_ops_id_ops_mapping2__destroy(skel2);
71+
}
72+
73+
void test_struct_ops_id_ops_mapping(void)
74+
{
75+
if (test__start_subtest("st_ops_id_ops_mapping"))
76+
test_st_ops_id_ops_mapping();
77+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#include <vmlinux.h>
2+
#include <bpf/bpf_tracing.h>
3+
#include "bpf_misc.h"
4+
#include "../test_kmods/bpf_testmod.h"
5+
#include "../test_kmods/bpf_testmod_kfunc.h"
6+
7+
char _license[] SEC("license") = "GPL";
8+
9+
#define bpf_kfunc_multi_st_ops_test_1(args) bpf_kfunc_multi_st_ops_test_1(args, st_ops_id)
10+
int st_ops_id;
11+
12+
int test_pid;
13+
int test_err;
14+
15+
#define MAP1_MAGIC 1234
16+
17+
SEC("struct_ops")
18+
int BPF_PROG(test_1, struct st_ops_args *args)
19+
{
20+
return MAP1_MAGIC;
21+
}
22+
23+
SEC("tp_btf/sys_enter")
24+
int BPF_PROG(sys_enter, struct pt_regs *regs, long id)
25+
{
26+
struct st_ops_args args = {};
27+
struct task_struct *task;
28+
int ret;
29+
30+
task = bpf_get_current_task_btf();
31+
if (!test_pid || task->pid != test_pid)
32+
return 0;
33+
34+
ret = bpf_kfunc_multi_st_ops_test_1(&args);
35+
if (ret != MAP1_MAGIC)
36+
test_err++;
37+
38+
return 0;
39+
}
40+
41+
SEC("syscall")
42+
int syscall_prog(void *ctx)
43+
{
44+
struct st_ops_args args = {};
45+
int ret;
46+
47+
ret = bpf_kfunc_multi_st_ops_test_1(&args);
48+
if (ret != MAP1_MAGIC)
49+
test_err++;
50+
51+
return 0;
52+
}
53+
54+
SEC(".struct_ops.link")
55+
struct bpf_testmod_multi_st_ops st_ops_map = {
56+
.test_1 = (void *)test_1,
57+
};
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#include <vmlinux.h>
2+
#include <bpf/bpf_tracing.h>
3+
#include "bpf_misc.h"
4+
#include "../test_kmods/bpf_testmod.h"
5+
#include "../test_kmods/bpf_testmod_kfunc.h"
6+
7+
char _license[] SEC("license") = "GPL";
8+
9+
#define bpf_kfunc_multi_st_ops_test_1(args) bpf_kfunc_multi_st_ops_test_1(args, st_ops_id)
10+
int st_ops_id;
11+
12+
int test_pid;
13+
int test_err;
14+
15+
#define MAP2_MAGIC 4567
16+
17+
SEC("struct_ops")
18+
int BPF_PROG(test_1, struct st_ops_args *args)
19+
{
20+
return MAP2_MAGIC;
21+
}
22+
23+
SEC("tp_btf/sys_enter")
24+
int BPF_PROG(sys_enter, struct pt_regs *regs, long id)
25+
{
26+
struct st_ops_args args = {};
27+
struct task_struct *task;
28+
int ret;
29+
30+
task = bpf_get_current_task_btf();
31+
if (!test_pid || task->pid != test_pid)
32+
return 0;
33+
34+
ret = bpf_kfunc_multi_st_ops_test_1(&args);
35+
if (ret != MAP2_MAGIC)
36+
test_err++;
37+
38+
return 0;
39+
}
40+
41+
SEC("syscall")
42+
int syscall_prog(void *ctx)
43+
{
44+
struct st_ops_args args = {};
45+
int ret;
46+
47+
ret = bpf_kfunc_multi_st_ops_test_1(&args);
48+
if (ret != MAP2_MAGIC)
49+
test_err++;
50+
51+
return 0;
52+
}
53+
54+
SEC(".struct_ops.link")
55+
struct bpf_testmod_multi_st_ops st_ops_map = {
56+
.test_1 = (void *)test_1,
57+
};

0 commit comments

Comments
 (0)