Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

selftests/bpf: Remove the bpf_tcp_helpers.h usages from other non tcp-cc tests

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>

authored by

Martin KaFai Lau and committed by
Alexei Starovoitov
c075c9c4 6eee55aa

+23 -38
+1
tools/testing/selftests/bpf/progs/bpf_tracing_net.h
··· 25 25 26 26 #define IP_TOS 1 27 27 28 + #define SOL_IPV6 41 28 29 #define IPV6_TCLASS 67 29 30 #define IPV6_AUTOFLOWLABEL 70 30 31
+4 -2
tools/testing/selftests/bpf/progs/connect4_prog.c
··· 14 14 #include <bpf/bpf_helpers.h> 15 15 #include <bpf/bpf_endian.h> 16 16 17 - #include "bpf_tcp_helpers.h" 18 - 19 17 #define SRC_REWRITE_IP4 0x7f000004U 20 18 #define DST_REWRITE_IP4 0x7f000001U 21 19 #define DST_REWRITE_PORT4 4444 ··· 28 30 29 31 #ifndef IFNAMSIZ 30 32 #define IFNAMSIZ 16 33 + #endif 34 + 35 + #ifndef SOL_TCP 36 + #define SOL_TCP 6 31 37 #endif 32 38 33 39 __attribute__ ((noinline)) __weak
+2 -2
tools/testing/selftests/bpf/progs/mptcp_sock.c
··· 2 2 /* Copyright (c) 2020, Tessares SA. */ 3 3 /* Copyright (c) 2022, SUSE. */ 4 4 5 - #include <linux/bpf.h> 5 + #include "bpf_tracing_net.h" 6 6 #include <bpf/bpf_helpers.h> 7 - #include "bpf_tcp_helpers.h" 7 + #include <bpf/bpf_tracing.h> 8 8 9 9 char _license[] SEC("license") = "GPL"; 10 10 __u32 token = 0;
+6 -10
tools/testing/selftests/bpf/progs/sockopt_qos_to_cc.c
··· 1 1 // SPDX-License-Identifier: GPL-2.0 2 2 /* Copyright (c) 2021 Facebook */ 3 - #include <string.h> 4 - #include <linux/tcp.h> 5 - #include <netinet/in.h> 6 - #include <linux/bpf.h> 7 - #include <bpf/bpf_helpers.h> 8 - #include "bpf_tcp_helpers.h" 3 + #include "bpf_tracing_net.h" 9 4 10 5 char _license[] SEC("license") = "GPL"; 11 6 12 7 __s32 page_size = 0; 8 + 9 + const char cc_reno[TCP_CA_NAME_MAX] = "reno"; 10 + const char cc_cubic[TCP_CA_NAME_MAX] = "cubic"; 13 11 14 12 SEC("cgroup/setsockopt") 15 13 int sockopt_qos_to_cc(struct bpf_sockopt *ctx) ··· 15 17 void *optval_end = ctx->optval_end; 16 18 int *optval = ctx->optval; 17 19 char buf[TCP_CA_NAME_MAX]; 18 - char cc_reno[TCP_CA_NAME_MAX] = "reno"; 19 - char cc_cubic[TCP_CA_NAME_MAX] = "cubic"; 20 20 21 21 if (ctx->level != SOL_IPV6 || ctx->optname != IPV6_TCLASS) 22 22 goto out; ··· 25 29 if (bpf_getsockopt(ctx->sk, SOL_TCP, TCP_CONGESTION, &buf, sizeof(buf))) 26 30 return 0; 27 31 28 - if (!tcp_cc_eq(buf, cc_cubic)) 32 + if (bpf_strncmp(buf, sizeof(buf), cc_cubic)) 29 33 return 0; 30 34 31 35 if (*optval == 0x2d) { 32 - if (bpf_setsockopt(ctx->sk, SOL_TCP, TCP_CONGESTION, &cc_reno, 36 + if (bpf_setsockopt(ctx->sk, SOL_TCP, TCP_CONGESTION, (void *)&cc_reno, 33 37 sizeof(cc_reno))) 34 38 return 0; 35 39 }
+5 -11
tools/testing/selftests/bpf/progs/test_btf_skc_cls_ingress.c
··· 1 1 // SPDX-License-Identifier: GPL-2.0 2 2 /* Copyright (c) 2020 Facebook */ 3 3 4 - #include <string.h> 5 - #include <errno.h> 6 - #include <netinet/in.h> 7 - #include <linux/stddef.h> 8 - #include <linux/bpf.h> 9 - #include <linux/ipv6.h> 10 - #include <linux/tcp.h> 11 - #include <linux/if_ether.h> 12 - #include <linux/pkt_cls.h> 13 - 4 + #include "bpf_tracing_net.h" 14 5 #include <bpf/bpf_helpers.h> 15 6 #include <bpf/bpf_endian.h> 16 - #include "bpf_tcp_helpers.h" 7 + 8 + #ifndef ENOENT 9 + #define ENOENT 2 10 + #endif 17 11 18 12 struct sockaddr_in6 srv_sa6 = {}; 19 13 __u16 listen_tp_sport = 0;
+4 -1
tools/testing/selftests/bpf/progs/test_sock_fields.c
··· 7 7 8 8 #include <bpf/bpf_helpers.h> 9 9 #include <bpf/bpf_endian.h> 10 - #include "bpf_tcp_helpers.h" 11 10 12 11 enum bpf_linum_array_idx { 13 12 EGRESS_LINUM_IDX, ··· 40 41 __type(key, int); 41 42 __type(value, struct bpf_spinlock_cnt); 42 43 } sk_pkt_out_cnt10 SEC(".maps"); 44 + 45 + struct tcp_sock { 46 + __u32 lsndtime; 47 + } __attribute__((preserve_access_index)); 43 48 44 49 struct bpf_tcp_sock listen_tp = {}; 45 50 struct sockaddr_in6 srv_sa6 = {};
+1 -12
tools/testing/selftests/bpf/progs/test_tcpbpf_kern.c
··· 1 1 // SPDX-License-Identifier: GPL-2.0 2 - #include <stddef.h> 3 - #include <string.h> 4 - #include <netinet/in.h> 5 - #include <linux/bpf.h> 6 - #include <linux/if_ether.h> 7 - #include <linux/if_packet.h> 8 - #include <linux/ip.h> 9 - #include <linux/ipv6.h> 10 - #include <linux/types.h> 11 - #include <linux/socket.h> 12 - #include <linux/tcp.h> 2 + #include "bpf_tracing_net.h" 13 3 #include <bpf/bpf_helpers.h> 14 4 #include <bpf/bpf_endian.h> 15 - #include "bpf_tcp_helpers.h" 16 5 #include "test_tcpbpf.h" 17 6 18 7 struct tcpbpf_globals global = {};