Skip to contentSkip to conversation

selftests: forwarding: fix race between packet receive and tc check

PATCH In review Jiri Pirko 2 participants 2 messages Nov 28, 2019 – Nov 30, 2019

Subsystems (4)
lore
Share & tools
lore

Jiri Pirko

author
#0 lore raw

PATCH 1/1 selftests: forwarding: fix race between packet receive and tc check

From: Jiri Pirko <[email protected]>

It is possible that tc stats get checked before the packet we check for actually arrived into the interface and accounted for.
Fix it by checking for the expected result in a loop until timeout is reached (by default 1 second).

Fixes 07e5c75184a1 ("selftests: forwarding: Introduce tc flower matching tests")
---
 .../selftests/net/forwarding/tc_common.sh     | 39 +++++++++++++++----
 1 file changed, 31 insertions(+), 8 deletions(-)
tools/testing/selftests/net/forwarding/tc_common.sh +31 -8
index d93589bd4d1d..64f652633585 100644
@@ -3,16 +3,42 @@
 
 CHECK_TC="yes"
 
+# Can be overridden by the configuration file. See lib.sh
+TC_HIT_TIMEOUT=${TC_HIT_TIMEOUT:=1000} # ms
+
+__tc_check_packets()
+{
+	local id=$1
+	local handle=$2
+	local count=$3
+	local operator=$4
+
+	start_time="$(date -u +%s%3N)"
+	while true
+	do
+		cmd_jq "tc -j -s filter show $id" \
+		       ".[] | select(.options.handle == $handle) | \
+			    select(.options.actions[0].stats.packets $operator $count)" \
+		    &> /dev/null
+		ret=$?
+		if [[ $ret -eq 0 ]]; then
+			return $ret
+		fi
+		current_time="$(date -u +%s%3N)"
+		diff=$(expr $current_time - $start_time)
+		if [ "$diff" -gt "$TC_HIT_TIMEOUT" ]; then
+			return 1
+		fi
+	done
+}
+
 tc_check_packets()
 {
 	local id=$1
 	local handle=$2
 	local count=$3
 
-	cmd_jq "tc -j -s filter show $id" \
-	       ".[] | select(.options.handle == $handle) | \
-	              select(.options.actions[0].stats.packets == $count)" \
-	       &> /dev/null
+	__tc_check_packets "$id" "$handle" "$count" "=="
 }
 
 tc_check_packets_hitting()
@@ -20,8 +46,5 @@ tc_check_packets_hitting()
 	local id=$1
 	local handle=$2
 
-	cmd_jq "tc -j -s filter show $id" \
-	       ".[] | select(.options.handle == $handle) | \
-		      select(.options.actions[0].stats.packets > 0)" \
-	       &> /dev/null
+	__tc_check_packets "$id" "$handle" 0 ">"
 }
Signature · 1 line
2.20.1
maintainer
#1 lore raw

From: Jiri Pirko <[email protected]>
Date: Thu, 28 Nov 2019 13:38:57 +0100

Quoted 9 lines

From: Jiri Pirko <[email protected]>

It is possible that tc stats get checked before the packet we check for actually arrived into the interface and accounted for.
Fix it by checking for the expected result in a loop until timeout is reached (by default 1 second).

Fixes 07e5c75184a1 ("selftests: forwarding: Introduce tc flower matching tests")

Applied and queued up for -stable, thanks Jiri.