Skip to content

Commit 65b4414

Browse files
fomichevAlexei Starovoitov
authored andcommitted
selftests/bpf: add sockopt test that exercises BPF_F_ALLOW_MULTI
sockopt test that verifies chaining behavior. v9: * setsockopt chaining example v7: * rework the test to verify cgroup getsockopt chaining Cc: Andrii Nakryiko <[email protected]> Cc: Martin Lau <[email protected]> Signed-off-by: Stanislav Fomichev <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 8a027dc commit 65b4414

File tree

4 files changed

+449
-1
lines changed

4 files changed

+449
-1
lines changed

tools/testing/selftests/bpf/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,4 @@ test_btf_dump
4141
xdping
4242
test_sockopt
4343
test_sockopt_sk
44+
test_sockopt_multi

tools/testing/selftests/bpf/Makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ TEST_GEN_PROGS = test_verifier test_tag test_maps test_lru_map test_lpm_map test
2626
test_sock test_btf test_sockmap get_cgroup_id_user test_socket_cookie \
2727
test_cgroup_storage test_select_reuseport test_section_names \
2828
test_netcnt test_tcpnotify_user test_sock_fields test_sysctl test_hashmap \
29-
test_btf_dump test_cgroup_attach xdping test_sockopt test_sockopt_sk
29+
test_btf_dump test_cgroup_attach xdping test_sockopt test_sockopt_sk \
30+
test_sockopt_multi
3031

3132
BPF_OBJ_FILES = $(patsubst %.c,%.o, $(notdir $(wildcard progs/*.c)))
3233
TEST_GEN_FILES = $(BPF_OBJ_FILES)
@@ -105,6 +106,7 @@ $(OUTPUT)/test_sysctl: cgroup_helpers.c
105106
$(OUTPUT)/test_cgroup_attach: cgroup_helpers.c
106107
$(OUTPUT)/test_sockopt: cgroup_helpers.c
107108
$(OUTPUT)/test_sockopt_sk: cgroup_helpers.c
109+
$(OUTPUT)/test_sockopt_multi: cgroup_helpers.c
108110

109111
.PHONY: force
110112

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
#include <netinet/in.h>
3+
#include <linux/bpf.h>
4+
#include "bpf_helpers.h"
5+
6+
char _license[] SEC("license") = "GPL";
7+
__u32 _version SEC("version") = 1;
8+
9+
SEC("cgroup/getsockopt/child")
10+
int _getsockopt_child(struct bpf_sockopt *ctx)
11+
{
12+
__u8 *optval_end = ctx->optval_end;
13+
__u8 *optval = ctx->optval;
14+
15+
if (ctx->level != SOL_IP || ctx->optname != IP_TOS)
16+
return 1;
17+
18+
if (optval + 1 > optval_end)
19+
return 0; /* EPERM, bounds check */
20+
21+
if (optval[0] != 0x80)
22+
return 0; /* EPERM, unexpected optval from the kernel */
23+
24+
ctx->retval = 0; /* Reset system call return value to zero */
25+
26+
optval[0] = 0x90;
27+
ctx->optlen = 1;
28+
29+
return 1;
30+
}
31+
32+
SEC("cgroup/getsockopt/parent")
33+
int _getsockopt_parent(struct bpf_sockopt *ctx)
34+
{
35+
__u8 *optval_end = ctx->optval_end;
36+
__u8 *optval = ctx->optval;
37+
38+
if (ctx->level != SOL_IP || ctx->optname != IP_TOS)
39+
return 1;
40+
41+
if (optval + 1 > optval_end)
42+
return 0; /* EPERM, bounds check */
43+
44+
if (optval[0] != 0x90)
45+
return 0; /* EPERM, unexpected optval from the kernel */
46+
47+
ctx->retval = 0; /* Reset system call return value to zero */
48+
49+
optval[0] = 0xA0;
50+
ctx->optlen = 1;
51+
52+
return 1;
53+
}
54+
55+
SEC("cgroup/setsockopt")
56+
int _setsockopt(struct bpf_sockopt *ctx)
57+
{
58+
__u8 *optval_end = ctx->optval_end;
59+
__u8 *optval = ctx->optval;
60+
61+
if (ctx->level != SOL_IP || ctx->optname != IP_TOS)
62+
return 1;
63+
64+
if (optval + 1 > optval_end)
65+
return 0; /* EPERM, bounds check */
66+
67+
optval[0] += 0x10;
68+
ctx->optlen = 1;
69+
70+
return 1;
71+
}

0 commit comments

Comments
 (0)