Skip to content

Commit

Permalink
selftests/bpf: add sockopt test that exercises BPF_F_ALLOW_MULTI
Browse files Browse the repository at this point in the history
sockopt test that verifies chaining behavior.

v9:
* setsockopt chaining example

v7:
* rework the test to verify cgroup getsockopt chaining

Cc: Andrii Nakryiko <andriin@fb.com>
Cc: Martin Lau <kafai@fb.com>
Signed-off-by: Stanislav Fomichev <sdf@google.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
  • Loading branch information
Stanislav Fomichev authored and Alexei Starovoitov committed Jun 27, 2019
1 parent 8a027dc commit 65b4414
Show file tree
Hide file tree
Showing 4 changed files with 449 additions and 1 deletion.
1 change: 1 addition & 0 deletions tools/testing/selftests/bpf/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,4 @@ test_btf_dump
xdping
test_sockopt
test_sockopt_sk
test_sockopt_multi
4 changes: 3 additions & 1 deletion tools/testing/selftests/bpf/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ TEST_GEN_PROGS = test_verifier test_tag test_maps test_lru_map test_lpm_map test
test_sock test_btf test_sockmap get_cgroup_id_user test_socket_cookie \
test_cgroup_storage test_select_reuseport test_section_names \
test_netcnt test_tcpnotify_user test_sock_fields test_sysctl test_hashmap \
test_btf_dump test_cgroup_attach xdping test_sockopt test_sockopt_sk
test_btf_dump test_cgroup_attach xdping test_sockopt test_sockopt_sk \
test_sockopt_multi

BPF_OBJ_FILES = $(patsubst %.c,%.o, $(notdir $(wildcard progs/*.c)))
TEST_GEN_FILES = $(BPF_OBJ_FILES)
Expand Down Expand Up @@ -105,6 +106,7 @@ $(OUTPUT)/test_sysctl: cgroup_helpers.c
$(OUTPUT)/test_cgroup_attach: cgroup_helpers.c
$(OUTPUT)/test_sockopt: cgroup_helpers.c
$(OUTPUT)/test_sockopt_sk: cgroup_helpers.c
$(OUTPUT)/test_sockopt_multi: cgroup_helpers.c

.PHONY: force

Expand Down
71 changes: 71 additions & 0 deletions tools/testing/selftests/bpf/progs/sockopt_multi.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// SPDX-License-Identifier: GPL-2.0
#include <netinet/in.h>
#include <linux/bpf.h>
#include "bpf_helpers.h"

char _license[] SEC("license") = "GPL";
__u32 _version SEC("version") = 1;

SEC("cgroup/getsockopt/child")
int _getsockopt_child(struct bpf_sockopt *ctx)
{
__u8 *optval_end = ctx->optval_end;
__u8 *optval = ctx->optval;

if (ctx->level != SOL_IP || ctx->optname != IP_TOS)
return 1;

if (optval + 1 > optval_end)
return 0; /* EPERM, bounds check */

if (optval[0] != 0x80)
return 0; /* EPERM, unexpected optval from the kernel */

ctx->retval = 0; /* Reset system call return value to zero */

optval[0] = 0x90;
ctx->optlen = 1;

return 1;
}

SEC("cgroup/getsockopt/parent")
int _getsockopt_parent(struct bpf_sockopt *ctx)
{
__u8 *optval_end = ctx->optval_end;
__u8 *optval = ctx->optval;

if (ctx->level != SOL_IP || ctx->optname != IP_TOS)
return 1;

if (optval + 1 > optval_end)
return 0; /* EPERM, bounds check */

if (optval[0] != 0x90)
return 0; /* EPERM, unexpected optval from the kernel */

ctx->retval = 0; /* Reset system call return value to zero */

optval[0] = 0xA0;
ctx->optlen = 1;

return 1;
}

SEC("cgroup/setsockopt")
int _setsockopt(struct bpf_sockopt *ctx)
{
__u8 *optval_end = ctx->optval_end;
__u8 *optval = ctx->optval;

if (ctx->level != SOL_IP || ctx->optname != IP_TOS)
return 1;

if (optval + 1 > optval_end)
return 0; /* EPERM, bounds check */

optval[0] += 0x10;
ctx->optlen = 1;

return 1;
}
Loading

0 comments on commit 65b4414

Please sign in to comment.