accel/qaic: Add AIC200 support
[drm/drm-misc.git] / tools / testing / selftests / net / netdevice.sh
blob438f7b2acc5fc6597855028a80a3d5ec151cb513
1 #!/bin/sh
2 # SPDX-License-Identifier: GPL-2.0
4 # This test is for checking network interface
5 # For the moment it tests only ethernet interface (but wifi could be easily added)
7 # We assume that all network driver are loaded
8 # if not they probably have failed earlier in the boot process and their logged error will be catched by another test
11 # Kselftest framework requirement - SKIP code is 4.
12 ksft_skip=4
14 # this function will try to up the interface
15 # if already up, nothing done
16 # arg1: network interface name
17 kci_net_start()
19 netdev=$1
21 ip link show "$netdev" |grep -q UP
22 if [ $? -eq 0 ];then
23 echo "SKIP: $netdev: interface already up"
24 return $ksft_skip
27 ip link set "$netdev" up
28 if [ $? -ne 0 ];then
29 echo "FAIL: $netdev: Fail to up interface"
30 return 1
31 else
32 echo "PASS: $netdev: set interface up"
33 NETDEV_STARTED=1
35 return 0
38 # this function will try to setup an IP and MAC address on a network interface
39 # Doing nothing if the interface was already up
40 # arg1: network interface name
41 kci_net_setup()
43 netdev=$1
45 # do nothing if the interface was already up
46 if [ $NETDEV_STARTED -eq 0 ];then
47 return 0
50 MACADDR='02:03:04:05:06:07'
51 ip link set dev $netdev address "$MACADDR"
52 if [ $? -ne 0 ];then
53 echo "FAIL: $netdev: Cannot set MAC address"
54 else
55 ip link show $netdev |grep -q "$MACADDR"
56 if [ $? -eq 0 ];then
57 echo "PASS: $netdev: set MAC address"
58 else
59 echo "FAIL: $netdev: Cannot set MAC address"
63 #check that the interface did not already have an IP
64 ip address show "$netdev" |grep '^[[:space:]]*inet'
65 if [ $? -eq 0 ];then
66 echo "SKIP: $netdev: already have an IP"
67 return $ksft_skip
70 if [ "$veth_created" ]; then
71 echo "XFAIL: $netdev: set IP address unsupported for veth*"
72 else
73 # TODO what ipaddr to set ? DHCP ?
74 echo "SKIP: $netdev: set IP address"
76 return $ksft_skip
79 # test an ethtool command
80 # arg1: return code for not supported (see ethtool code source)
81 # arg2: summary of the command
82 # arg3: command to execute
83 kci_netdev_ethtool_test()
85 if [ $# -le 2 ];then
86 echo "SKIP: $netdev: ethtool: invalid number of arguments"
87 return 1
89 $3 >/dev/null
90 ret=$?
91 if [ $ret -ne 0 ];then
92 if [ $ret -eq "$1" ];then
93 echo "XFAIL: $netdev: ethtool $2 not supported"
94 return $ksft_skip
95 else
96 echo "FAIL: $netdev: ethtool $2"
97 return 1
99 else
100 echo "PASS: $netdev: ethtool $2"
102 return 0
105 # test ethtool commands
106 # arg1: network interface name
107 kci_netdev_ethtool()
109 netdev=$1
111 #check presence of ethtool
112 ethtool --version 2>/dev/null >/dev/null
113 if [ $? -ne 0 ];then
114 echo "SKIP: ethtool not present"
115 return $ksft_skip
118 TMP_ETHTOOL_FEATURES="$(mktemp)"
119 if [ ! -e "$TMP_ETHTOOL_FEATURES" ];then
120 echo "SKIP: Cannot create a tmp file"
121 return 1
124 ethtool -k "$netdev" > "$TMP_ETHTOOL_FEATURES"
125 if [ $? -ne 0 ];then
126 echo "FAIL: $netdev: ethtool list features"
127 rm "$TMP_ETHTOOL_FEATURES"
128 return 1
130 echo "PASS: $netdev: ethtool list features"
132 while read -r FEATURE VALUE FIXED; do
133 [ "$FEATURE" != "Features" ] || continue # Skip "Features"
134 [ "$FIXED" != "[fixed]" ] || continue # Skip fixed features
135 feature="${FEATURE%:*}"
137 ethtool --offload "$netdev" "$feature" off
138 if [ $? -eq 0 ]; then
139 echo "PASS: $netdev: Turned off feature: $feature"
140 else
141 echo "FAIL: $netdev: Failed to turn off feature:" \
142 "$feature"
145 ethtool --offload "$netdev" "$feature" on
146 if [ $? -eq 0 ]; then
147 echo "PASS: $netdev: Turned on feature: $feature"
148 else
149 echo "FAIL: $netdev: Failed to turn on feature:" \
150 "$feature"
153 #restore the feature to its initial state
154 ethtool --offload "$netdev" "$feature" "$VALUE"
155 if [ $? -eq 0 ]; then
156 echo "PASS: $netdev: Restore feature $feature" \
157 "to initial state $VALUE"
158 else
159 echo "FAIL: $netdev: Failed to restore feature" \
160 "$feature to initial state $VALUE"
163 done < "$TMP_ETHTOOL_FEATURES"
165 rm "$TMP_ETHTOOL_FEATURES"
167 kci_netdev_ethtool_test 74 'dump' "ethtool -d $netdev"
168 kci_netdev_ethtool_test 94 'stats' "ethtool -S $netdev"
170 return 0
173 # stop a netdev
174 # arg1: network interface name
175 kci_netdev_stop()
177 netdev=$1
179 if [ $NETDEV_STARTED -eq 0 ];then
180 echo "SKIP: $netdev: interface kept up"
181 return 0
184 ip link set "$netdev" down
185 if [ $? -ne 0 ];then
186 echo "FAIL: $netdev: stop interface"
187 return 1
189 echo "PASS: $netdev: stop interface"
190 return 0
193 # run all test on a netdev
194 # arg1: network interface name
195 kci_test_netdev()
197 NETDEV_STARTED=0
198 IFACE_TO_UPDOWN="$1"
199 IFACE_TO_TEST="$1"
200 #check for VLAN interface
201 MASTER_IFACE="$(echo $1 | cut -d@ -f2)"
202 if [ ! -z "$MASTER_IFACE" ];then
203 IFACE_TO_UPDOWN="$MASTER_IFACE"
204 IFACE_TO_TEST="$(echo $1 | cut -d@ -f1)"
207 NETDEV_STARTED=0
208 kci_net_start "$IFACE_TO_UPDOWN"
210 kci_net_setup "$IFACE_TO_TEST"
212 kci_netdev_ethtool "$IFACE_TO_TEST"
214 kci_netdev_stop "$IFACE_TO_UPDOWN"
215 return 0
218 #check for needed privileges
219 if [ "$(id -u)" -ne 0 ];then
220 echo "SKIP: Need root privileges"
221 exit $ksft_skip
224 ip link show 2>/dev/null >/dev/null
225 if [ $? -ne 0 ];then
226 echo "SKIP: Could not run test without the ip tool"
227 exit $ksft_skip
230 TMP_LIST_NETDEV="$(mktemp)"
231 if [ ! -e "$TMP_LIST_NETDEV" ];then
232 echo "FAIL: Cannot create a tmp file"
233 exit 1
236 ip link show |grep '^[0-9]' | grep -oE '[[:space:]].*eth[0-9]*:|[[:space:]].*enp[0-9]s[0-9]:' | cut -d\ -f2 | cut -d: -f1> "$TMP_LIST_NETDEV"
238 if [ ! -s "$TMP_LIST_NETDEV" ]; then
239 echo "No valid network device found, creating veth pair"
240 ip link add veth0 type veth peer name veth1
241 echo "veth0" > "$TMP_LIST_NETDEV"
242 veth_created=1
245 while read netdev
247 kci_test_netdev "$netdev"
248 done < "$TMP_LIST_NETDEV"
250 #clean up veth interface pair if it was created
251 if [ "$veth_created" ]; then
252 ip link delete veth0
253 echo "Removed veth pair"
256 rm "$TMP_LIST_NETDEV"
257 exit 0