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

selftests/bpf: fix race in test_tcp_rtt test

There is a race in this test between receiving the ACK for the
single-byte packet sent in the test, and reading the values from the
map.

This patch fixes this by having the client wait until there are no more
unacknowledged packets.

Before:
for i in {1..1000}; do ../net/in_netns.sh ./test_tcp_rtt; \
done | grep -c PASSED
< trimmed error messages >
993

After:
for i in {1..10000}; do ../net/in_netns.sh ./test_tcp_rtt; \
done | grep -c PASSED
10000

Fixes: b55873984dab ("selftests/bpf: test BPF_SOCK_OPS_RTT_CB")
Signed-off-by: Petar Penkov <ppenkov@google.com>
Reviewed-by: Stanislav Fomichev <sdf@google.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>

authored by

Petar Penkov and committed by
Daniel Borkmann
fae55527 929ffa6e

+31
+31
tools/testing/selftests/bpf/test_tcp_rtt.c
··· 6 6 #include <sys/types.h> 7 7 #include <sys/socket.h> 8 8 #include <netinet/in.h> 9 + #include <netinet/tcp.h> 9 10 #include <pthread.h> 10 11 11 12 #include <linux/filter.h> ··· 33 32 34 33 if (write(fd, &b, sizeof(b)) != 1) 35 34 error(1, errno, "Failed to send single byte"); 35 + } 36 + 37 + static int wait_for_ack(int fd, int retries) 38 + { 39 + struct tcp_info info; 40 + socklen_t optlen; 41 + int i, err; 42 + 43 + for (i = 0; i < retries; i++) { 44 + optlen = sizeof(info); 45 + err = getsockopt(fd, SOL_TCP, TCP_INFO, &info, &optlen); 46 + if (err < 0) { 47 + log_err("Failed to lookup TCP stats"); 48 + return err; 49 + } 50 + 51 + if (info.tcpi_unacked == 0) 52 + return 0; 53 + 54 + usleep(10); 55 + } 56 + 57 + log_err("Did not receive ACK"); 58 + return -1; 36 59 } 37 60 38 61 static int verify_sk(int map_fd, int client_fd, const char *msg, __u32 invoked, ··· 174 149 /*icsk_retransmits=*/0); 175 150 176 151 send_byte(client_fd); 152 + if (wait_for_ack(client_fd, 100) < 0) { 153 + err = -1; 154 + goto close_client_fd; 155 + } 156 + 177 157 178 158 err += verify_sk(map_fd, client_fd, "first payload byte", 179 159 /*invoked=*/2, ··· 187 157 /*delivered_ce=*/0, 188 158 /*icsk_retransmits=*/0); 189 159 160 + close_client_fd: 190 161 close(client_fd); 191 162 192 163 close_bpf_object: