Skip to content

Commit ec12ab2

Browse files
HoyeonRheeMartin KaFai Lau
authored andcommitted
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 <[email protected]> Signed-off-by: Hoyeon Lee <[email protected]> Signed-off-by: Martin KaFai Lau <[email protected]> Link: https://patch.msgid.link/[email protected]
1 parent f700b37 commit ec12ab2

File tree

2 files changed

+10
-28
lines changed

2 files changed

+10
-28
lines changed

tools/testing/selftests/bpf/progs/bpf_iter_setsockopt.c

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,10 @@
1818

1919
unsigned short reuse_listen_hport = 0;
2020
unsigned short listen_hport = 0;
21-
char cubic_cc[TCP_CA_NAME_MAX] = "bpf_cubic";
21+
const char cubic_cc[] = "bpf_cubic";
2222
char dctcp_cc[TCP_CA_NAME_MAX] = "bpf_dctcp";
2323
bool random_retry = false;
2424

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-
}
3825

3926
SEC("iter/tcp")
4027
int change_tcp_cc(struct bpf_iter__tcp *ctx)
@@ -58,7 +45,7 @@ int change_tcp_cc(struct bpf_iter__tcp *ctx)
5845
cur_cc, sizeof(cur_cc)))
5946
return 0;
6047

61-
if (!tcp_cc_eq(cur_cc, cubic_cc))
48+
if (bpf_strncmp(cur_cc, TCP_CA_NAME_MAX, cubic_cc))
6249
return 0;
6350

6451
if (random_retry && bpf_get_prandom_u32() % 4 == 1)

tools/testing/selftests/bpf/progs/connect4_prog.c

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434
#define SOL_TCP 6
3535
#endif
3636

37+
const char reno[] = "reno";
38+
const char cubic[] = "cubic";
39+
3740
__attribute__ ((noinline)) __weak
3841
int do_bind(struct bpf_sock_addr *ctx)
3942
{
@@ -50,35 +53,27 @@ int do_bind(struct bpf_sock_addr *ctx)
5053
}
5154

5255
static __inline int verify_cc(struct bpf_sock_addr *ctx,
53-
char expected[TCP_CA_NAME_MAX])
56+
const char expected[])
5457
{
5558
char buf[TCP_CA_NAME_MAX];
56-
int i;
5759

5860
if (bpf_getsockopt(ctx, SOL_TCP, TCP_CONGESTION, &buf, sizeof(buf)))
5961
return 1;
6062

61-
for (i = 0; i < TCP_CA_NAME_MAX; i++) {
62-
if (buf[i] != expected[i])
63-
return 1;
64-
if (buf[i] == 0)
65-
break;
66-
}
63+
if (bpf_strncmp(buf, TCP_CA_NAME_MAX, expected))
64+
return 1;
6765

6866
return 0;
6967
}
7068

7169
static __inline int set_cc(struct bpf_sock_addr *ctx)
7270
{
73-
char reno[TCP_CA_NAME_MAX] = "reno";
74-
char cubic[TCP_CA_NAME_MAX] = "cubic";
75-
76-
if (bpf_setsockopt(ctx, SOL_TCP, TCP_CONGESTION, &reno, sizeof(reno)))
71+
if (bpf_setsockopt(ctx, SOL_TCP, TCP_CONGESTION, (void *)reno, sizeof(reno)))
7772
return 1;
7873
if (verify_cc(ctx, reno))
7974
return 1;
8075

81-
if (bpf_setsockopt(ctx, SOL_TCP, TCP_CONGESTION, &cubic, sizeof(cubic)))
76+
if (bpf_setsockopt(ctx, SOL_TCP, TCP_CONGESTION, (void *)cubic, sizeof(cubic)))
8277
return 1;
8378
if (verify_cc(ctx, cubic))
8479
return 1;

0 commit comments

Comments
 (0)