]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blob - tools/testing/selftests/net/netdevice.sh
Merge tag 'media/v4.14-2' of git://git.kernel.org/pub/scm/linux/kernel/git/mchehab...
[mirror_ubuntu-focal-kernel.git] / tools / testing / selftests / net / netdevice.sh
1 #!/bin/sh
2 #
3 # This test is for checking network interface
4 # For the moment it tests only ethernet interface (but wifi could be easily added)
5 #
6 # We assume that all network driver are loaded
7 # if not they probably have failed earlier in the boot process and their logged error will be catched by another test
8 #
9
10 # this function will try to up the interface
11 # if already up, nothing done
12 # arg1: network interface name
13 kci_net_start()
14 {
15 netdev=$1
16
17 ip link show "$netdev" |grep -q UP
18 if [ $? -eq 0 ];then
19 echo "SKIP: $netdev: interface already up"
20 return 0
21 fi
22
23 ip link set "$netdev" up
24 if [ $? -ne 0 ];then
25 echo "FAIL: $netdev: Fail to up interface"
26 return 1
27 else
28 echo "PASS: $netdev: set interface up"
29 NETDEV_STARTED=1
30 fi
31 return 0
32 }
33
34 # this function will try to setup an IP and MAC address on a network interface
35 # Doing nothing if the interface was already up
36 # arg1: network interface name
37 kci_net_setup()
38 {
39 netdev=$1
40
41 # do nothing if the interface was already up
42 if [ $NETDEV_STARTED -eq 0 ];then
43 return 0
44 fi
45
46 MACADDR='02:03:04:05:06:07'
47 ip link set dev $netdev address "$MACADDR"
48 if [ $? -ne 0 ];then
49 echo "FAIL: $netdev: Cannot set MAC address"
50 else
51 ip link show $netdev |grep -q "$MACADDR"
52 if [ $? -eq 0 ];then
53 echo "PASS: $netdev: set MAC address"
54 else
55 echo "FAIL: $netdev: Cannot set MAC address"
56 fi
57 fi
58
59 #check that the interface did not already have an IP
60 ip address show "$netdev" |grep '^[[:space:]]*inet'
61 if [ $? -eq 0 ];then
62 echo "SKIP: $netdev: already have an IP"
63 return 0
64 fi
65
66 # TODO what ipaddr to set ? DHCP ?
67 echo "SKIP: $netdev: set IP address"
68 return 0
69 }
70
71 # test an ethtool command
72 # arg1: return code for not supported (see ethtool code source)
73 # arg2: summary of the command
74 # arg3: command to execute
75 kci_netdev_ethtool_test()
76 {
77 if [ $# -le 2 ];then
78 echo "SKIP: $netdev: ethtool: invalid number of arguments"
79 return 1
80 fi
81 $3 >/dev/null
82 ret=$?
83 if [ $ret -ne 0 ];then
84 if [ $ret -eq "$1" ];then
85 echo "SKIP: $netdev: ethtool $2 not supported"
86 else
87 echo "FAIL: $netdev: ethtool $2"
88 return 1
89 fi
90 else
91 echo "PASS: $netdev: ethtool $2"
92 fi
93 return 0
94 }
95
96 # test ethtool commands
97 # arg1: network interface name
98 kci_netdev_ethtool()
99 {
100 netdev=$1
101
102 #check presence of ethtool
103 ethtool --version 2>/dev/null >/dev/null
104 if [ $? -ne 0 ];then
105 echo "SKIP: ethtool not present"
106 return 1
107 fi
108
109 TMP_ETHTOOL_FEATURES="$(mktemp)"
110 if [ ! -e "$TMP_ETHTOOL_FEATURES" ];then
111 echo "SKIP: Cannot create a tmp file"
112 return 1
113 fi
114
115 ethtool -k "$netdev" > "$TMP_ETHTOOL_FEATURES"
116 if [ $? -ne 0 ];then
117 echo "FAIL: $netdev: ethtool list features"
118 rm "$TMP_ETHTOOL_FEATURES"
119 return 1
120 fi
121 echo "PASS: $netdev: ethtool list features"
122 #TODO for each non fixed features, try to turn them on/off
123 rm "$TMP_ETHTOOL_FEATURES"
124
125 kci_netdev_ethtool_test 74 'dump' "ethtool -d $netdev"
126 kci_netdev_ethtool_test 94 'stats' "ethtool -S $netdev"
127 return 0
128 }
129
130 # stop a netdev
131 # arg1: network interface name
132 kci_netdev_stop()
133 {
134 netdev=$1
135
136 if [ $NETDEV_STARTED -eq 0 ];then
137 echo "SKIP: $netdev: interface kept up"
138 return 0
139 fi
140
141 ip link set "$netdev" down
142 if [ $? -ne 0 ];then
143 echo "FAIL: $netdev: stop interface"
144 return 1
145 fi
146 echo "PASS: $netdev: stop interface"
147 return 0
148 }
149
150 # run all test on a netdev
151 # arg1: network interface name
152 kci_test_netdev()
153 {
154 NETDEV_STARTED=0
155 IFACE_TO_UPDOWN="$1"
156 IFACE_TO_TEST="$1"
157 #check for VLAN interface
158 MASTER_IFACE="$(echo $1 | cut -d@ -f2)"
159 if [ ! -z "$MASTER_IFACE" ];then
160 IFACE_TO_UPDOWN="$MASTER_IFACE"
161 IFACE_TO_TEST="$(echo $1 | cut -d@ -f1)"
162 fi
163
164 NETDEV_STARTED=0
165 kci_net_start "$IFACE_TO_UPDOWN"
166
167 kci_net_setup "$IFACE_TO_TEST"
168
169 kci_netdev_ethtool "$IFACE_TO_TEST"
170
171 kci_netdev_stop "$IFACE_TO_UPDOWN"
172 return 0
173 }
174
175 #check for needed privileges
176 if [ "$(id -u)" -ne 0 ];then
177 echo "SKIP: Need root privileges"
178 exit 0
179 fi
180
181 ip link show 2>/dev/null >/dev/null
182 if [ $? -ne 0 ];then
183 echo "SKIP: Could not run test without the ip tool"
184 exit 0
185 fi
186
187 TMP_LIST_NETDEV="$(mktemp)"
188 if [ ! -e "$TMP_LIST_NETDEV" ];then
189 echo "FAIL: Cannot create a tmp file"
190 exit 1
191 fi
192
193 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"
194 while read netdev
195 do
196 kci_test_netdev "$netdev"
197 done < "$TMP_LIST_NETDEV"
198
199 rm "$TMP_LIST_NETDEV"
200 exit 0