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