]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
selftests/bpf: Replace TCP CC string comparisons with bpf_strncmp
authorHoyeon Lee <hoyeon.lee@suse.com>
Sat, 15 Nov 2025 22:55:39 +0000 (07:55 +0900)
committerMartin KaFai Lau <martin.lau@kernel.org>
Tue, 18 Nov 2025 22:57:53 +0000 (14:57 -0800)
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
tools/testing/selftests/bpf/progs/bpf_iter_setsockopt.c
tools/testing/selftests/bpf/progs/connect4_prog.c

index 774d4dbe81893530c745465f66e2131828916c85..a8aa5a71d846f66dc64d4ed074892ee2471e72fb 100644 (file)
 
 unsigned short reuse_listen_hport = 0;
 unsigned short listen_hport = 0;
-char cubic_cc[TCP_CA_NAME_MAX] = "bpf_cubic";
+const char cubic_cc[] = "bpf_cubic";
 char dctcp_cc[TCP_CA_NAME_MAX] = "bpf_dctcp";
 bool random_retry = false;
 
-static bool tcp_cc_eq(const char *a, const char *b)
-{
-       int i;
-
-       for (i = 0; i < TCP_CA_NAME_MAX; i++) {
-               if (a[i] != b[i])
-                       return false;
-               if (!a[i])
-                       break;
-       }
-
-       return true;
-}
 
 SEC("iter/tcp")
 int change_tcp_cc(struct bpf_iter__tcp *ctx)
@@ -58,7 +45,7 @@ int change_tcp_cc(struct bpf_iter__tcp *ctx)
                           cur_cc, sizeof(cur_cc)))
                return 0;
 
-       if (!tcp_cc_eq(cur_cc, cubic_cc))
+       if (bpf_strncmp(cur_cc, TCP_CA_NAME_MAX, cubic_cc))
                return 0;
 
        if (random_retry && bpf_get_prandom_u32() % 4 == 1)
index 9e9ebf27b8784ed31750d4cb6bfeb35d74715306..9d158cfad981749239f6f8184804894df6b8224c 100644 (file)
@@ -34,6 +34,9 @@
 #define SOL_TCP 6
 #endif
 
+const char reno[] = "reno";
+const char cubic[] = "cubic";
+
 __attribute__ ((noinline)) __weak
 int do_bind(struct bpf_sock_addr *ctx)
 {
@@ -50,35 +53,27 @@ int do_bind(struct bpf_sock_addr *ctx)
 }
 
 static __inline int verify_cc(struct bpf_sock_addr *ctx,
-                             char expected[TCP_CA_NAME_MAX])
+                             const char expected[])
 {
        char buf[TCP_CA_NAME_MAX];
-       int i;
 
        if (bpf_getsockopt(ctx, SOL_TCP, TCP_CONGESTION, &buf, sizeof(buf)))
                return 1;
 
-       for (i = 0; i < TCP_CA_NAME_MAX; i++) {
-               if (buf[i] != expected[i])
-                       return 1;
-               if (buf[i] == 0)
-                       break;
-       }
+       if (bpf_strncmp(buf, TCP_CA_NAME_MAX, expected))
+               return 1;
 
        return 0;
 }
 
 static __inline int set_cc(struct bpf_sock_addr *ctx)
 {
-       char reno[TCP_CA_NAME_MAX] = "reno";
-       char cubic[TCP_CA_NAME_MAX] = "cubic";
-
-       if (bpf_setsockopt(ctx, SOL_TCP, TCP_CONGESTION, &reno, sizeof(reno)))
+       if (bpf_setsockopt(ctx, SOL_TCP, TCP_CONGESTION, (void *)reno, sizeof(reno)))
                return 1;
        if (verify_cc(ctx, reno))
                return 1;
 
-       if (bpf_setsockopt(ctx, SOL_TCP, TCP_CONGESTION, &cubic, sizeof(cubic)))
+       if (bpf_setsockopt(ctx, SOL_TCP, TCP_CONGESTION, (void *)cubic, sizeof(cubic)))
                return 1;
        if (verify_cc(ctx, cubic))
                return 1;