selftests/bpf: Remove the bpf_tcp_helpers.h usages from other non tcp-cc tests
authorMartin KaFai Lau <martin.lau@kernel.org>
Thu, 9 May 2024 17:50:25 +0000 (10:50 -0700)
committerAlexei Starovoitov <ast@kernel.org>
Thu, 9 May 2024 18:13:12 +0000 (11:13 -0700)
The patch removes the remaining bpf_tcp_helpers.h usages in the
non tcp-cc networking tests. It either replaces it with bpf_tracing_net.h
or just removed it because the test is not actually using any
kernel sockets. For the later, the missing macro (mainly SOL_TCP) is
defined locally.

An exception is the test_sock_fields which is testing
the "struct bpf_sock" type instead of the kernel sock type.
Whenever "vmlinux.h" is used instead, it hits a verifier
error on doing arithmetic on the sock_common pointer:

; return !a6[0] && !a6[1] && !a6[2] && a6[3] == bpf_htonl(1); @ test_sock_fields.c:54
21: (61) r2 = *(u32 *)(r1 +28)        ; R1_w=sock_common() R2_w=scalar(smin=0,smax=umax=0xffffffff,var_off=(0x0; 0xffffffff))
22: (56) if w2 != 0x0 goto pc-6       ; R2_w=0
23: (b7) r3 = 28                      ; R3_w=28
24: (bf) r2 = r1                      ; R1_w=sock_common() R2_w=sock_common()
25: (0f) r2 += r3
R2 pointer arithmetic on sock_common prohibited

Hence, instead of including bpf_tracing_net.h, the test_sock_fields test
defines a tcp_sock with one lsndtime field in it.

Another highlight is, in sockopt_qos_to_cc.c, the tcp_cc_eq()
is replaced by bpf_strncmp(). tcp_cc_eq() was a workaround
in bpf_tcp_helpers.h before bpf_strncmp had been added.

The SOL_IPV6 addition to bpf_tracing_net.h is needed by the
test_tcpbpf_kern test.

Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>
Link: https://lore.kernel.org/r/20240509175026.3423614-10-martin.lau@linux.dev
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
tools/testing/selftests/bpf/progs/bpf_tracing_net.h
tools/testing/selftests/bpf/progs/connect4_prog.c
tools/testing/selftests/bpf/progs/mptcp_sock.c
tools/testing/selftests/bpf/progs/sockopt_qos_to_cc.c
tools/testing/selftests/bpf/progs/test_btf_skc_cls_ingress.c
tools/testing/selftests/bpf/progs/test_sock_fields.c
tools/testing/selftests/bpf/progs/test_tcpbpf_kern.c

index ba4ca033458674ac6960c492ce4453cd4a1eed95..59843b430f76aae153fc8c17063cc5d85f1d8ec0 100644 (file)
@@ -25,6 +25,7 @@
 
 #define IP_TOS                 1
 
+#define SOL_IPV6               41
 #define IPV6_TCLASS            67
 #define IPV6_AUTOFLOWLABEL     70
 
index 7ef49ec04838d176ffc2ef94394bfc02624d2cc8..bec529da7c9d2ecd1f9181361e9397b4004771da 100644 (file)
@@ -14,8 +14,6 @@
 #include <bpf/bpf_helpers.h>
 #include <bpf/bpf_endian.h>
 
-#include "bpf_tcp_helpers.h"
-
 #define SRC_REWRITE_IP4                0x7f000004U
 #define DST_REWRITE_IP4                0x7f000001U
 #define DST_REWRITE_PORT4      4444
 #define IFNAMSIZ 16
 #endif
 
+#ifndef SOL_TCP
+#define SOL_TCP 6
+#endif
+
 __attribute__ ((noinline)) __weak
 int do_bind(struct bpf_sock_addr *ctx)
 {
index 91a0d7eff2ac7915d2993b942dc857cd5b74d32d..f3acb90588c72eab0a701614eebcc23d87ce31bc 100644 (file)
@@ -2,9 +2,9 @@
 /* Copyright (c) 2020, Tessares SA. */
 /* Copyright (c) 2022, SUSE. */
 
-#include <linux/bpf.h>
+#include "bpf_tracing_net.h"
 #include <bpf/bpf_helpers.h>
-#include "bpf_tcp_helpers.h"
+#include <bpf/bpf_tracing.h>
 
 char _license[] SEC("license") = "GPL";
 __u32 token = 0;
index 83753b00a55693cb8e578fd1b85697b9ac595ba0..5c3614333b0164c5d7685f602c5c16d90e550b1f 100644 (file)
@@ -1,24 +1,20 @@
 // SPDX-License-Identifier: GPL-2.0
 /* Copyright (c) 2021 Facebook */
-#include <string.h>
-#include <linux/tcp.h>
-#include <netinet/in.h>
-#include <linux/bpf.h>
-#include <bpf/bpf_helpers.h>
-#include "bpf_tcp_helpers.h"
+#include "bpf_tracing_net.h"
 
 char _license[] SEC("license") = "GPL";
 
 __s32 page_size = 0;
 
+const char cc_reno[TCP_CA_NAME_MAX] = "reno";
+const char cc_cubic[TCP_CA_NAME_MAX] = "cubic";
+
 SEC("cgroup/setsockopt")
 int sockopt_qos_to_cc(struct bpf_sockopt *ctx)
 {
        void *optval_end = ctx->optval_end;
        int *optval = ctx->optval;
        char buf[TCP_CA_NAME_MAX];
-       char cc_reno[TCP_CA_NAME_MAX] = "reno";
-       char cc_cubic[TCP_CA_NAME_MAX] = "cubic";
 
        if (ctx->level != SOL_IPV6 || ctx->optname != IPV6_TCLASS)
                goto out;
@@ -29,11 +25,11 @@ int sockopt_qos_to_cc(struct bpf_sockopt *ctx)
        if (bpf_getsockopt(ctx->sk, SOL_TCP, TCP_CONGESTION, &buf, sizeof(buf)))
                return 0;
 
-       if (!tcp_cc_eq(buf, cc_cubic))
+       if (bpf_strncmp(buf, sizeof(buf), cc_cubic))
                return 0;
 
        if (*optval == 0x2d) {
-               if (bpf_setsockopt(ctx->sk, SOL_TCP, TCP_CONGESTION, &cc_reno,
+               if (bpf_setsockopt(ctx->sk, SOL_TCP, TCP_CONGESTION, (void *)&cc_reno,
                                sizeof(cc_reno)))
                        return 0;
        }
index e2bea4da194bd6cfba3fe816b3a21a970ee3379f..f0759efff6ef15d2663927400dd064c53b020f78 100644 (file)
@@ -1,19 +1,13 @@
 // SPDX-License-Identifier: GPL-2.0
 /* Copyright (c) 2020 Facebook */
 
-#include <string.h>
-#include <errno.h>
-#include <netinet/in.h>
-#include <linux/stddef.h>
-#include <linux/bpf.h>
-#include <linux/ipv6.h>
-#include <linux/tcp.h>
-#include <linux/if_ether.h>
-#include <linux/pkt_cls.h>
-
+#include "bpf_tracing_net.h"
 #include <bpf/bpf_helpers.h>
 #include <bpf/bpf_endian.h>
-#include "bpf_tcp_helpers.h"
+
+#ifndef ENOENT
+#define ENOENT 2
+#endif
 
 struct sockaddr_in6 srv_sa6 = {};
 __u16 listen_tp_sport = 0;
index f75e531bf36fc27291b7c0df540bc1876ad115b5..196844be349cec19ae013299e710a3911a5cab34 100644 (file)
@@ -7,7 +7,6 @@
 
 #include <bpf/bpf_helpers.h>
 #include <bpf/bpf_endian.h>
-#include "bpf_tcp_helpers.h"
 
 enum bpf_linum_array_idx {
        EGRESS_LINUM_IDX,
@@ -42,6 +41,10 @@ struct {
        __type(value, struct bpf_spinlock_cnt);
 } sk_pkt_out_cnt10 SEC(".maps");
 
+struct tcp_sock {
+       __u32   lsndtime;
+} __attribute__((preserve_access_index));
+
 struct bpf_tcp_sock listen_tp = {};
 struct sockaddr_in6 srv_sa6 = {};
 struct bpf_tcp_sock cli_tp = {};
index a3f3f43fc195e944d316754edd01654ebef6df6a..6935f32eeb8fd89efcc5b3e37b2bd95424d30bfa 100644 (file)
@@ -1,18 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0
-#include <stddef.h>
-#include <string.h>
-#include <netinet/in.h>
-#include <linux/bpf.h>
-#include <linux/if_ether.h>
-#include <linux/if_packet.h>
-#include <linux/ip.h>
-#include <linux/ipv6.h>
-#include <linux/types.h>
-#include <linux/socket.h>
-#include <linux/tcp.h>
+#include "bpf_tracing_net.h"
 #include <bpf/bpf_helpers.h>
 #include <bpf/bpf_endian.h>
-#include "bpf_tcp_helpers.h"
 #include "test_tcpbpf.h"
 
 struct tcpbpf_globals global = {};