Skip to content

Commit 973b320

Browse files
alessandrocarminatigregkh
authored andcommitted
selftests/bpf: Prevent client connect before server bind in test_tc_tunnel.sh
[ Upstream commit f803bcf ] In some systems, the netcat server can incur in delay to start listening. When this happens, the test can randomly fail in various points. This is an example error message: # ip gre none gso # encap 192.168.1.1 to 192.168.1.2, type gre, mac none len 2000 # test basic connectivity # Ncat: Connection refused. The issue stems from a race condition between the netcat client and server. The test author had addressed this problem by implementing a sleep, which I have removed in this patch. This patch introduces a function capable of sleeping for up to two seconds. However, it can terminate the waiting period early if the port is reported to be listening. Signed-off-by: Alessandro Carminati (Red Hat) <alessandro.carminati@gmail.com> Signed-off-by: Andrii Nakryiko <andrii@kernel.org> Link: https://lore.kernel.org/bpf/20240314105911.213411-1-alessandro.carminati@gmail.com Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 58706e4 commit 973b320

1 file changed

Lines changed: 12 additions & 1 deletion

File tree

tools/testing/selftests/bpf/test_tc_tunnel.sh

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ cleanup() {
7171
server_listen() {
7272
ip netns exec "${ns2}" nc "${netcat_opt}" -l -p "${port}" > "${outfile}" &
7373
server_pid=$!
74-
sleep 0.2
7574
}
7675

7776
client_connect() {
@@ -92,6 +91,16 @@ verify_data() {
9291
fi
9392
}
9493

94+
wait_for_port() {
95+
for i in $(seq 20); do
96+
if ip netns exec "${ns2}" ss ${2:--4}OHntl | grep -q "$1"; then
97+
return 0
98+
fi
99+
sleep 0.1
100+
done
101+
return 1
102+
}
103+
95104
set -e
96105

97106
# no arguments: automated test, run all
@@ -183,6 +192,7 @@ setup
183192
# basic communication works
184193
echo "test basic connectivity"
185194
server_listen
195+
wait_for_port ${port} ${netcat_opt}
186196
client_connect
187197
verify_data
188198

@@ -194,6 +204,7 @@ ip netns exec "${ns1}" tc filter add dev veth1 egress \
194204
section "encap_${tuntype}_${mac}"
195205
echo "test bpf encap without decap (expect failure)"
196206
server_listen
207+
wait_for_port ${port} ${netcat_opt}
197208
! client_connect
198209

199210
if [[ "$tuntype" =~ "udp" ]]; then

0 commit comments

Comments
 (0)