Skip to content

Commit 5270950

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) <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/bpf/[email protected] Signed-off-by: Sasha Levin <[email protected]>
1 parent c5dc2d8 commit 5270950

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

tools/testing/selftests/bpf/test_tc_tunnel.sh

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

7877
client_connect() {
@@ -93,6 +92,16 @@ verify_data() {
9392
fi
9493
}
9594

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

98107
# no arguments: automated test, run all
@@ -193,6 +202,7 @@ setup
193202
# basic communication works
194203
echo "test basic connectivity"
195204
server_listen
205+
wait_for_port ${port} ${netcat_opt}
196206
client_connect
197207
verify_data
198208

@@ -204,6 +214,7 @@ ip netns exec "${ns1}" tc filter add dev veth1 egress \
204214
section "encap_${tuntype}_${mac}"
205215
echo "test bpf encap without decap (expect failure)"
206216
server_listen
217+
wait_for_port ${port} ${netcat_opt}
207218
! client_connect
208219

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

0 commit comments

Comments
 (0)