From c67ffc599e286b2edb039984e7ee1f6f177a7c0f Mon Sep 17 00:00:00 2001 From: Geliang Tang Date: Sat, 1 Jul 2023 20:56:59 +0800 Subject: [PATCH] selftests/bpf: Test bpf_mptcpify helper This patch tests the new helper bpf_mptcpify(). Store the new protocol value after invoking the helper to a local BSS variable. This is defined in a 'sock_create' SEC, so it will be invoked in BPF_CGROUP_RUN_PROG_INET_SOCK() in inet_create(). Signed-off-by: Geliang Tang --- tools/testing/selftests/bpf/bpf_tcp_helpers.h | 1 + tools/testing/selftests/bpf/progs/mptcpify.c | 26 +++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/mptcpify.c diff --git a/tools/testing/selftests/bpf/bpf_tcp_helpers.h b/tools/testing/selftests/bpf/bpf_tcp_helpers.h index 72c6180373862e..f846d5d62529e0 100644 --- a/tools/testing/selftests/bpf/bpf_tcp_helpers.h +++ b/tools/testing/selftests/bpf/bpf_tcp_helpers.h @@ -38,6 +38,7 @@ struct sock { #define sk_state __sk_common.skc_state unsigned long sk_pacing_rate; __u32 sk_pacing_status; /* see enum sk_pacing */ + __u16 sk_protocol; } __attribute__((preserve_access_index)); struct inet_sock { diff --git a/tools/testing/selftests/bpf/progs/mptcpify.c b/tools/testing/selftests/bpf/progs/mptcpify.c new file mode 100644 index 00000000000000..f04b186b9c2619 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/mptcpify.c @@ -0,0 +1,26 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2023, SUSE. */ + +#include +#include +#include +#include "bpf_tcp_helpers.h" + +char _license[] SEC("license") = "GPL"; +__u16 protocol = 0; + +SEC("cgroup/sock_create") +int sock(struct bpf_sock *ctx) +{ + struct sock *sk; + + if (ctx->type != SOCK_STREAM) + return 1; + + sk = bpf_mptcpify(ctx); + if (!sk) + return 1; + + protocol = sk->sk_protocol; + return 1; +}