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

selftests/bpf: Move common TCP helpers into bpf_tracing_net.h

Some BPF selftests contain identical copies of the min(), max(),
before(), and after() helpers. These repeated snippets are the same
across the tests and do not need to be defined separately.

Move these helpers into bpf_tracing_net.h so they can be shared by
TCP related BPF programs. This removes repeated code and keeps the
helpers in a single place.

Reviewed-by: Amery Hung <ameryhung@gmail.com>
Signed-off-by: Hoyeon Lee <hoyeon.lee@suse.com>
Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>
Link: https://patch.msgid.link/20251115225550.1086693-4-hoyeon.lee@suse.com

authored by

Hoyeon Lee and committed by
Martin KaFai Lau
f700b373 7dc211c1

+14 -24
-9
tools/testing/selftests/bpf/progs/bpf_cc_cubic.c
··· 22 22 #define TCP_PACING_CA_RATIO (120) 23 23 #define TCP_REORDERING (12) 24 24 25 - #define min(a, b) ((a) < (b) ? (a) : (b)) 26 - #define max(a, b) ((a) > (b) ? (a) : (b)) 27 - #define after(seq2, seq1) before(seq1, seq2) 28 - 29 25 extern void cubictcp_init(struct sock *sk) __ksym; 30 26 extern void cubictcp_cwnd_event(struct sock *sk, enum tcp_ca_event event) __ksym; 31 27 extern __u32 cubictcp_recalc_ssthresh(struct sock *sk) __ksym; ··· 29 33 extern __u32 tcp_reno_undo_cwnd(struct sock *sk) __ksym; 30 34 extern void cubictcp_acked(struct sock *sk, const struct ack_sample *sample) __ksym; 31 35 extern void cubictcp_cong_avoid(struct sock *sk, __u32 ack, __u32 acked) __ksym; 32 - 33 - static bool before(__u32 seq1, __u32 seq2) 34 - { 35 - return (__s32)(seq1-seq2) < 0; 36 - } 37 36 38 37 static __u64 div64_u64(__u64 dividend, __u64 divisor) 39 38 {
-7
tools/testing/selftests/bpf/progs/bpf_cubic.c
··· 20 20 char _license[] SEC("license") = "GPL"; 21 21 22 22 #define clamp(val, lo, hi) min((typeof(val))max(val, lo), hi) 23 - #define min(a, b) ((a) < (b) ? (a) : (b)) 24 - #define max(a, b) ((a) > (b) ? (a) : (b)) 25 - static bool before(__u32 seq1, __u32 seq2) 26 - { 27 - return (__s32)(seq1-seq2) < 0; 28 - } 29 - #define after(seq2, seq1) before(seq1, seq2) 30 23 31 24 extern __u32 tcp_slow_start(struct tcp_sock *tp, __u32 acked) __ksym; 32 25 extern void tcp_cong_avoid_ai(struct tcp_sock *tp, __u32 w, __u32 acked) __ksym;
-6
tools/testing/selftests/bpf/progs/bpf_dctcp.c
··· 13 13 #ifndef EBUSY 14 14 #define EBUSY 16 15 15 #endif 16 - #define min(a, b) ((a) < (b) ? (a) : (b)) 17 - #define max(a, b) ((a) > (b) ? (a) : (b)) 18 16 #define min_not_zero(x, y) ({ \ 19 17 typeof(x) __x = (x); \ 20 18 typeof(y) __y = (y); \ 21 19 __x == 0 ? __y : ((__y == 0) ? __x : min(__x, __y)); }) 22 - static bool before(__u32 seq1, __u32 seq2) 23 - { 24 - return (__s32)(seq1-seq2) < 0; 25 - } 26 20 27 21 char _license[] SEC("license") = "GPL"; 28 22
+14
tools/testing/selftests/bpf/progs/bpf_tracing_net.h
··· 146 146 147 147 #define tcp_jiffies32 ((__u32)bpf_jiffies64()) 148 148 149 + #ifndef min 150 + #define min(a, b) ((a) < (b) ? (a) : (b)) 151 + #endif 152 + #ifndef max 153 + #define max(a, b) ((a) > (b) ? (a) : (b)) 154 + #endif 155 + 156 + static inline bool before(__u32 seq1, __u32 seq2) 157 + { 158 + return (__s32)(seq1 - seq2) < 0; 159 + } 160 + 161 + #define after(seq2, seq1) before(seq1, seq2) 162 + 149 163 static inline struct inet_connection_sock *inet_csk(const struct sock *sk) 150 164 { 151 165 return (struct inet_connection_sock *)sk;
-2
tools/testing/selftests/bpf/progs/tcp_ca_write_sk_pacing.c
··· 8 8 9 9 #define USEC_PER_SEC 1000000UL 10 10 11 - #define min(a, b) ((a) < (b) ? (a) : (b)) 12 - 13 11 static unsigned int tcp_left_out(const struct tcp_sock *tp) 14 12 { 15 13 return tp->sacked_out + tp->lost_out;