1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3#
4# Regression tests for the SO_TXTIME interface
5
6set -e
7
8readonly ksft_skip=4
9readonly DEV="veth0"
10readonly BIN="./so_txtime"
11
12readonly RAND="$(mktemp -u XXXXXX)"
13readonly NSPREFIX="ns-${RAND}"
14readonly NS1="${NSPREFIX}1"
15readonly NS2="${NSPREFIX}2"
16
17readonly SADDR4='192.168.1.1'
18readonly DADDR4='192.168.1.2'
19readonly SADDR6='fd::1'
20readonly DADDR6='fd::2'
21
22cleanup() {
23	ip netns del "${NS2}"
24	ip netns del "${NS1}"
25}
26
27trap cleanup EXIT
28
29# Create virtual ethernet pair between network namespaces
30ip netns add "${NS1}"
31ip netns add "${NS2}"
32
33ip link add "${DEV}" netns "${NS1}" type veth \
34  peer name "${DEV}" netns "${NS2}"
35
36# Bring the devices up
37ip -netns "${NS1}" link set "${DEV}" up
38ip -netns "${NS2}" link set "${DEV}" up
39
40# Set fixed MAC addresses on the devices
41ip -netns "${NS1}" link set dev "${DEV}" address 02:02:02:02:02:02
42ip -netns "${NS2}" link set dev "${DEV}" address 06:06:06:06:06:06
43
44# Add fixed IP addresses to the devices
45ip -netns "${NS1}" addr add 192.168.1.1/24 dev "${DEV}"
46ip -netns "${NS2}" addr add 192.168.1.2/24 dev "${DEV}"
47ip -netns "${NS1}" addr add       fd::1/64 dev "${DEV}" nodad
48ip -netns "${NS2}" addr add       fd::2/64 dev "${DEV}" nodad
49
50run_test() {
51	local readonly IP="$1"
52	local readonly CLOCK="$2"
53	local readonly TXARGS="$3"
54	local readonly RXARGS="$4"
55
56	if [[ "${IP}" == "4" ]]; then
57		local readonly SADDR="${SADDR4}"
58		local readonly DADDR="${DADDR4}"
59	elif [[ "${IP}" == "6" ]]; then
60		local readonly SADDR="${SADDR6}"
61		local readonly DADDR="${DADDR6}"
62	else
63		echo "Invalid IP version ${IP}"
64		exit 1
65	fi
66
67	local readonly START="$(date +%s%N --date="+ 0.1 seconds")"
68
69	ip netns exec "${NS2}" "${BIN}" -"${IP}" -c "${CLOCK}" -t "${START}" -S "${SADDR}" -D "${DADDR}" "${RXARGS}" -r &
70	ip netns exec "${NS1}" "${BIN}" -"${IP}" -c "${CLOCK}" -t "${START}" -S "${SADDR}" -D "${DADDR}" "${TXARGS}"
71	wait "$!"
72}
73
74do_test() {
75	run_test $@
76	[ $? -ne 0 ] && ret=1
77}
78
79do_fail_test() {
80	run_test $@
81	[ $? -eq 0 ] && ret=1
82}
83
84ip netns exec "${NS1}" tc qdisc add dev "${DEV}" root fq
85set +e
86ret=0
87do_test 4 mono a,-1 a,-1
88do_test 6 mono a,0 a,0
89do_test 6 mono a,10 a,10
90do_test 4 mono a,10,b,20 a,10,b,20
91do_test 6 mono a,20,b,10 b,20,a,20
92
93if ip netns exec "${NS1}" tc qdisc replace dev "${DEV}" root etf clockid CLOCK_TAI delta 400000; then
94	do_fail_test 4 tai a,-1 a,-1
95	do_fail_test 6 tai a,0 a,0
96	do_test 6 tai a,10 a,10
97	do_test 4 tai a,10,b,20 a,10,b,20
98	do_test 6 tai a,20,b,10 b,10,a,20
99else
100	echo "tc ($(tc -V)) does not support qdisc etf. skipping"
101	[ $ret -eq 0 ] && ret=$ksft_skip
102fi
103
104if [ $ret -eq 0 ]; then
105	echo OK. All tests passed
106elif [[ $ret -ne $ksft_skip && -n "$KSFT_MACHINE_SLOW" ]]; then
107	echo "Ignoring errors due to slow environment" 1>&2
108	ret=0
109fi
110exit $ret
111