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

Configure Feed

Select the types of activity you want to include in your feed.

selftests/bpf: Replace TCP CC string comparisons with bpf_strncmp

The connect4_prog and bpf_iter_setsockopt tests duplicate the same
open-coded TCP congestion control string comparison logic. Since
bpf_strncmp() provides the same functionality, use it instead to
avoid repeated open-coded loops.

This change applies only to functional BPF tests and does not affect
the verifier performance benchmarks (veristat.cfg). No functional
changes intended.

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-5-hoyeon.lee@suse.com

authored by

Hoyeon Lee and committed by
Martin KaFai Lau
ec12ab2c f700b373

+10 -28
+2 -15
tools/testing/selftests/bpf/progs/bpf_iter_setsockopt.c
··· 18 18 19 19 unsigned short reuse_listen_hport = 0; 20 20 unsigned short listen_hport = 0; 21 - char cubic_cc[TCP_CA_NAME_MAX] = "bpf_cubic"; 21 + const char cubic_cc[] = "bpf_cubic"; 22 22 char dctcp_cc[TCP_CA_NAME_MAX] = "bpf_dctcp"; 23 23 bool random_retry = false; 24 24 25 - static bool tcp_cc_eq(const char *a, const char *b) 26 - { 27 - int i; 28 - 29 - for (i = 0; i < TCP_CA_NAME_MAX; i++) { 30 - if (a[i] != b[i]) 31 - return false; 32 - if (!a[i]) 33 - break; 34 - } 35 - 36 - return true; 37 - } 38 25 39 26 SEC("iter/tcp") 40 27 int change_tcp_cc(struct bpf_iter__tcp *ctx) ··· 45 58 cur_cc, sizeof(cur_cc))) 46 59 return 0; 47 60 48 - if (!tcp_cc_eq(cur_cc, cubic_cc)) 61 + if (bpf_strncmp(cur_cc, TCP_CA_NAME_MAX, cubic_cc)) 49 62 return 0; 50 63 51 64 if (random_retry && bpf_get_prandom_u32() % 4 == 1)
+8 -13
tools/testing/selftests/bpf/progs/connect4_prog.c
··· 34 34 #define SOL_TCP 6 35 35 #endif 36 36 37 + const char reno[] = "reno"; 38 + const char cubic[] = "cubic"; 39 + 37 40 __attribute__ ((noinline)) __weak 38 41 int do_bind(struct bpf_sock_addr *ctx) 39 42 { ··· 53 50 } 54 51 55 52 static __inline int verify_cc(struct bpf_sock_addr *ctx, 56 - char expected[TCP_CA_NAME_MAX]) 53 + const char expected[]) 57 54 { 58 55 char buf[TCP_CA_NAME_MAX]; 59 - int i; 60 56 61 57 if (bpf_getsockopt(ctx, SOL_TCP, TCP_CONGESTION, &buf, sizeof(buf))) 62 58 return 1; 63 59 64 - for (i = 0; i < TCP_CA_NAME_MAX; i++) { 65 - if (buf[i] != expected[i]) 66 - return 1; 67 - if (buf[i] == 0) 68 - break; 69 - } 60 + if (bpf_strncmp(buf, TCP_CA_NAME_MAX, expected)) 61 + return 1; 70 62 71 63 return 0; 72 64 } 73 65 74 66 static __inline int set_cc(struct bpf_sock_addr *ctx) 75 67 { 76 - char reno[TCP_CA_NAME_MAX] = "reno"; 77 - char cubic[TCP_CA_NAME_MAX] = "cubic"; 78 - 79 - if (bpf_setsockopt(ctx, SOL_TCP, TCP_CONGESTION, &reno, sizeof(reno))) 68 + if (bpf_setsockopt(ctx, SOL_TCP, TCP_CONGESTION, (void *)reno, sizeof(reno))) 80 69 return 1; 81 70 if (verify_cc(ctx, reno)) 82 71 return 1; 83 72 84 - if (bpf_setsockopt(ctx, SOL_TCP, TCP_CONGESTION, &cubic, sizeof(cubic))) 73 + if (bpf_setsockopt(ctx, SOL_TCP, TCP_CONGESTION, (void *)cubic, sizeof(cubic))) 85 74 return 1; 86 75 if (verify_cc(ctx, cubic)) 87 76 return 1;