diff mbox series

[RFC,for-faizal,4/4] selftests: net: tsn: add tc-taprio test cases (WIP)

Message ID 20231221132521.2314811-5-vladimir.oltean@nxp.com (mailing list archive)
State RFC
Delegated to: Netdev Maintainers
Headers show
Series tc-taprio selftests | expand

Checks

Context Check Description
netdev/series_format success Posting correctly formatted
netdev/tree_selection success Guessed tree name to be net-next
netdev/ynl success Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 8 this patch: 8
netdev/cc_maintainers warning 8 maintainers not CCed: edumazet@google.com jnixdorf-oss@avm.de danieller@nvidia.com shuah@kernel.org pabeni@redhat.com kuba@kernel.org petrm@nvidia.com linux-kselftest@vger.kernel.org
netdev/build_clang fail Errors and warnings before: 12 this patch: 12
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest fail Script tsn_lib.sh not found in tools/testing/selftests/net/forwarding/Makefile
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 8 this patch: 8
netdev/checkpatch warning WARNING: added, moved or deleted file(s), does MAINTAINERS need updating? WARNING: line length of 92 exceeds 80 columns WARNING: line length of 96 exceeds 80 columns
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

Vladimir Oltean Dec. 21, 2023, 1:25 p.m. UTC
Obviously this is unfinished. While we were discussing about tc-taprio
behavior during schedule changes in particular, it would be much better
if the tests could slowly build up towards that complicated case, and
make sure that the simpler cases work well first: a packet gets sent
when it should (when it's sent in band with its time slot), gets blocked
when it's sent out of band with its time slot, etc.

Signed-off-by: Vladimir Oltean <vladimir.oltean@nxp.com>
---
 .../testing/selftests/net/forwarding/Makefile |   1 +
 .../selftests/net/forwarding/tc_taprio.sh     | 143 ++++++++++++++++++
 .../selftests/net/forwarding/tsn_lib.sh       |  42 +++++
 3 files changed, 186 insertions(+)
 create mode 100755 tools/testing/selftests/net/forwarding/tc_taprio.sh
diff mbox series

Patch

diff --git a/tools/testing/selftests/net/forwarding/Makefile b/tools/testing/selftests/net/forwarding/Makefile
index 452693514be4..8e8ed75a3aac 100644
--- a/tools/testing/selftests/net/forwarding/Makefile
+++ b/tools/testing/selftests/net/forwarding/Makefile
@@ -97,6 +97,7 @@  TEST_PROGS = bridge_fdb_learning_limit.sh \
 	tc_mpls_l2vpn.sh \
 	tc_police.sh \
 	tc_shblocks.sh \
+	tc_taprio.sh \
 	tc_tunnel_key.sh \
 	tc_vlan_modify.sh \
 	vxlan_asymmetric_ipv6.sh \
diff --git a/tools/testing/selftests/net/forwarding/tc_taprio.sh b/tools/testing/selftests/net/forwarding/tc_taprio.sh
new file mode 100755
index 000000000000..387cc0860d4f
--- /dev/null
+++ b/tools/testing/selftests/net/forwarding/tc_taprio.sh
@@ -0,0 +1,143 @@ 
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+
+ALL_TESTS="in_band out_of_band cycle_extension"
+NUM_NETIFS=2
+VETH_OPTS="numtxqueues 8 numrxqueues 8"
+source lib.sh
+source tsn_lib.sh
+
+in_band()
+{
+	local basetime=$(clock_gettime CLOCK_REALTIME)
+	local window_size=$((NSEC_PER_SEC / 2))
+	local cycletime=$((2 * window_size))
+	local expected=1
+	local isochron_dat="$(mktemp)"
+	local window_start
+	local window_end
+
+	basetime=$((basetime + UTC_TAI_OFFSET * NSEC_PER_SEC))
+	basetime=$(round_up_with_margin $basetime $NSEC_PER_SEC $NSEC_PER_SEC)
+
+	tc qdisc replace dev $h1 root stab overhead 24 taprio num_tc 2 \
+		map 0 1 \
+		queues 1@0 1@1 \
+		base-time $basetime \
+		sched-entry S 0x3 500000000 \
+		sched-entry S 0x0 500000000 \
+		clockid CLOCK_TAI \
+		flags 0x0
+
+	isochron_do \
+		$h1 $h2 \
+		"" "" \
+		$((basetime + 2 * cycletime)) \
+		$cycletime \
+		0 \
+		${expected} \
+		"" \
+		1 \
+		"" \
+		"--omit-hwts --taprio --window-size $window_size" \
+		"--omit-hwts" \
+		"${isochron_dat}"
+
+	# Count all received packets by looking at the non-zero RX timestamps
+	received=$(isochron report \
+		--input-file "${isochron_dat}" \
+		--printf-format "%u\n" --printf-args "r" | \
+		grep -w -v '0' | wc -l)
+
+	if [ "${received}" = "${expected}" ]; then
+		RET=0
+	else
+		RET=1
+		echo "Expected isochron to receive ${expected} packets but received ${received}"
+	fi
+
+	tx_tstamp=$(isochron report \
+		--input-file "${isochron_dat}" \
+		--printf-format "%u\n" --printf-args "t")
+
+	window_start=$((basetime + 2 * cycletime))
+	window_end=$((window_start + window_size))
+
+	if (( tx_tstamp >= window_start && tx_tstamp <= window_end )); then
+		RET=0
+	else
+		RET=1
+		printf "Isochron TX timestamp %s sent outside expected window (%s - %s)\n" \
+			$(ns_to_time $tx_tstamp) \
+			$(ns_to_time $window_start) \
+			$(ns_to_time $window_end)
+	fi
+
+	log_test "${test_name}"
+
+	rm ${isochron_dat} 2> /dev/null
+
+	tc qdisc del dev $h1 root
+}
+
+out_of_band()
+{
+	:
+}
+
+cycle_extension()
+{
+	:
+}
+
+h1_create()
+{
+	simple_if_init $h1 192.0.2.1/24
+}
+
+h1_destroy()
+{
+	simple_if_fini $h1 192.0.2.1/24
+}
+
+h2_create()
+{
+	simple_if_init $h2 192.0.2.2/24
+}
+
+h2_destroy()
+{
+	simple_if_fini $h2 192.0.2.2/24
+}
+
+setup_prepare()
+{
+	h1=${NETIFS[p1]}
+	h2=${NETIFS[p2]}
+
+	vrf_prepare
+
+	h1_create
+	h2_create
+}
+
+cleanup()
+{
+	pre_cleanup
+
+	isochron_recv_stop
+
+	h2_destroy
+	h1_destroy
+
+	vrf_cleanup
+}
+
+trap cleanup EXIT
+
+setup_prepare
+setup_wait
+
+tests_run
+
+exit $EXIT_STATUS
diff --git a/tools/testing/selftests/net/forwarding/tsn_lib.sh b/tools/testing/selftests/net/forwarding/tsn_lib.sh
index 189bf27bad76..e72a18a1dee0 100644
--- a/tools/testing/selftests/net/forwarding/tsn_lib.sh
+++ b/tools/testing/selftests/net/forwarding/tsn_lib.sh
@@ -5,6 +5,8 @@ 
 REQUIRE_ISOCHRON=${REQUIRE_ISOCHRON:=yes}
 REQUIRE_LINUXPTP=${REQUIRE_LINUXPTP:=yes}
 
+NSEC_PER_SEC=1000000000
+
 # Tunables
 UTC_TAI_OFFSET=37
 ISOCHRON_CPU=1
@@ -18,6 +20,7 @@  fi
 if [[ "$REQUIRE_LINUXPTP" = "yes" ]]; then
 	require_command phc2sys
 	require_command ptp4l
+	require_command phc_ctl
 fi
 
 phc2sys_start()
@@ -251,3 +254,42 @@  isochron_do()
 
 	cpufreq_restore ${ISOCHRON_CPU}
 }
+
+# Convert a time specifier from 1.23456789 format to nanoseconds
+time_to_ns()
+{
+	local time="$1"
+	local sec=${time%%.*}
+	local nsec=${time##*.}
+
+	echo $((sec * NSEC_PER_SEC + 10#$nsec))
+}
+
+ns_to_time()
+{
+	local nsec="$1"
+	local sec=$((nsec / NSEC_PER_SEC))
+
+	nsec=$((nsec - (sec * NSEC_PER_SEC)))
+
+	printf "%d.%09lld" $sec $nsec
+}
+
+clock_gettime()
+{
+	local clkid=$1; shift
+	local time=$(phc_ctl $clkid get | awk '/clock time is/ { print $5 }')
+
+	echo $(time_to_ns $time)
+}
+
+# Round up value to next multiple, leaving a specified margin
+round_up_with_margin()
+{
+	local val=$1; shift
+	local multiple=$1; shift
+	local margin=$1; shift
+
+	val=$((val + margin))
+	echo $((((val + margin - 1) / margin) * margin))
+}