Skip to content

Commit ee103e9

Browse files
jrfastabAlexei Starovoitov
authored andcommitted
bpf, selftests: Test probe_* helpers from SCHED_CLS
Lets test using probe* in SCHED_CLS network programs as well just to be sure these keep working. Its cheap to add the extra test and provides a second context to test outside of sk_msg after we generalized probe* helpers to all networking types. Signed-off-by: John Fastabend <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Acked-by: Yonghong Song <[email protected]> Acked-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/bpf/159033911685.12355.15951980509828906214.stgit@john-Precision-5820-Tower Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 1d9c037 commit ee103e9

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
#include <test_progs.h>
3+
#include <network_helpers.h>
4+
5+
void test_skb_helpers(void)
6+
{
7+
struct __sk_buff skb = {
8+
.wire_len = 100,
9+
.gso_segs = 8,
10+
.gso_size = 10,
11+
};
12+
struct bpf_prog_test_run_attr tattr = {
13+
.data_in = &pkt_v4,
14+
.data_size_in = sizeof(pkt_v4),
15+
.ctx_in = &skb,
16+
.ctx_size_in = sizeof(skb),
17+
.ctx_out = &skb,
18+
.ctx_size_out = sizeof(skb),
19+
};
20+
struct bpf_object *obj;
21+
int err;
22+
23+
err = bpf_prog_load("./test_skb_helpers.o", BPF_PROG_TYPE_SCHED_CLS, &obj,
24+
&tattr.prog_fd);
25+
if (CHECK_ATTR(err, "load", "err %d errno %d\n", err, errno))
26+
return;
27+
err = bpf_prog_test_run_xattr(&tattr);
28+
CHECK_ATTR(err, "len", "err %d errno %d\n", err, errno);
29+
bpf_object__close(obj);
30+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
#include "vmlinux.h"
3+
#include <bpf/bpf_helpers.h>
4+
#include <bpf/bpf_endian.h>
5+
6+
#define TEST_COMM_LEN 16
7+
8+
struct {
9+
__uint(type, BPF_MAP_TYPE_CGROUP_ARRAY);
10+
__uint(max_entries, 1);
11+
__type(key, u32);
12+
__type(value, u32);
13+
} cgroup_map SEC(".maps");
14+
15+
char _license[] SEC("license") = "GPL";
16+
17+
SEC("classifier/test_skb_helpers")
18+
int test_skb_helpers(struct __sk_buff *skb)
19+
{
20+
struct task_struct *task;
21+
char comm[TEST_COMM_LEN];
22+
__u32 tpid;
23+
24+
task = (struct task_struct *)bpf_get_current_task();
25+
bpf_probe_read_kernel(&tpid , sizeof(tpid), &task->tgid);
26+
bpf_probe_read_kernel_str(&comm, sizeof(comm), &task->comm);
27+
return 0;
28+
}

0 commit comments

Comments
 (0)