1 #!/bin/sh 2 # SPDX-License-Identifier: GPL-2.0 3 # 4 # This test is for checking network interface 5 # For the moment it tests only ethernet interf 6 # 7 # We assume that all network driver are loaded 8 # if not they probably have failed earlier in 9 # 10 11 # Kselftest framework requirement - SKIP code 12 ksft_skip=4 13 14 # this function will try to up the interface 15 # if already up, nothing done 16 # arg1: network interface name 17 kci_net_start() 18 { 19 netdev=$1 20 21 ip link show "$netdev" |grep -q UP 22 if [ $? -eq 0 ];then 23 echo "SKIP: $netdev: interface 24 return $ksft_skip 25 fi 26 27 ip link set "$netdev" up 28 if [ $? -ne 0 ];then 29 echo "FAIL: $netdev: Fail to u 30 return 1 31 else 32 echo "PASS: $netdev: set inter 33 NETDEV_STARTED=1 34 fi 35 return 0 36 } 37 38 # this function will try to setup an IP and MA 39 # Doing nothing if the interface was already u 40 # arg1: network interface name 41 kci_net_setup() 42 { 43 netdev=$1 44 45 # do nothing if the interface was alre 46 if [ $NETDEV_STARTED -eq 0 ];then 47 return 0 48 fi 49 50 MACADDR='02:03:04:05:06:07' 51 ip link set dev $netdev address "$MACA 52 if [ $? -ne 0 ];then 53 echo "FAIL: $netdev: Cannot se 54 else 55 ip link show $netdev |grep -q 56 if [ $? -eq 0 ];then 57 echo "PASS: $netdev: s 58 else 59 echo "FAIL: $netdev: C 60 fi 61 fi 62 63 #check that the interface did not alre 64 ip address show "$netdev" |grep '^[[:s 65 if [ $? -eq 0 ];then 66 echo "SKIP: $netdev: already h 67 return $ksft_skip 68 fi 69 70 # TODO what ipaddr to set ? DHCP ? 71 echo "SKIP: $netdev: set IP address" 72 return $ksft_skip 73 } 74 75 # test an ethtool command 76 # arg1: return code for not supported (see eth 77 # arg2: summary of the command 78 # arg3: command to execute 79 kci_netdev_ethtool_test() 80 { 81 if [ $# -le 2 ];then 82 echo "SKIP: $netdev: ethtool: 83 return 1 84 fi 85 $3 >/dev/null 86 ret=$? 87 if [ $ret -ne 0 ];then 88 if [ $ret -eq "$1" ];then 89 echo "SKIP: $netdev: e 90 return $ksft_skip 91 else 92 echo "FAIL: $netdev: e 93 return 1 94 fi 95 else 96 echo "PASS: $netdev: ethtool $ 97 fi 98 return 0 99 } 100 101 # test ethtool commands 102 # arg1: network interface name 103 kci_netdev_ethtool() 104 { 105 netdev=$1 106 107 #check presence of ethtool 108 ethtool --version 2>/dev/null >/dev/nu 109 if [ $? -ne 0 ];then 110 echo "SKIP: ethtool not presen 111 return $ksft_skip 112 fi 113 114 TMP_ETHTOOL_FEATURES="$(mktemp)" 115 if [ ! -e "$TMP_ETHTOOL_FEATURES" ];th 116 echo "SKIP: Cannot create a tm 117 return 1 118 fi 119 120 ethtool -k "$netdev" > "$TMP_ETHTOOL_F 121 if [ $? -ne 0 ];then 122 echo "FAIL: $netdev: ethtool l 123 rm "$TMP_ETHTOOL_FEATURES" 124 return 1 125 fi 126 echo "PASS: $netdev: ethtool list feat 127 #TODO for each non fixed features, try 128 rm "$TMP_ETHTOOL_FEATURES" 129 130 kci_netdev_ethtool_test 74 'dump' "eth 131 kci_netdev_ethtool_test 94 'stats' "et 132 return 0 133 } 134 135 # stop a netdev 136 # arg1: network interface name 137 kci_netdev_stop() 138 { 139 netdev=$1 140 141 if [ $NETDEV_STARTED -eq 0 ];then 142 echo "SKIP: $netdev: interface 143 return 0 144 fi 145 146 ip link set "$netdev" down 147 if [ $? -ne 0 ];then 148 echo "FAIL: $netdev: stop inte 149 return 1 150 fi 151 echo "PASS: $netdev: stop interface" 152 return 0 153 } 154 155 # run all test on a netdev 156 # arg1: network interface name 157 kci_test_netdev() 158 { 159 NETDEV_STARTED=0 160 IFACE_TO_UPDOWN="$1" 161 IFACE_TO_TEST="$1" 162 #check for VLAN interface 163 MASTER_IFACE="$(echo $1 | cut -d@ -f2) 164 if [ ! -z "$MASTER_IFACE" ];then 165 IFACE_TO_UPDOWN="$MASTER_IFACE 166 IFACE_TO_TEST="$(echo $1 | cut 167 fi 168 169 NETDEV_STARTED=0 170 kci_net_start "$IFACE_TO_UPDOWN" 171 172 kci_net_setup "$IFACE_TO_TEST" 173 174 kci_netdev_ethtool "$IFACE_TO_TEST" 175 176 kci_netdev_stop "$IFACE_TO_UPDOWN" 177 return 0 178 } 179 180 #check for needed privileges 181 if [ "$(id -u)" -ne 0 ];then 182 echo "SKIP: Need root privileges" 183 exit $ksft_skip 184 fi 185 186 ip link show 2>/dev/null >/dev/null 187 if [ $? -ne 0 ];then 188 echo "SKIP: Could not run test without 189 exit $ksft_skip 190 fi 191 192 TMP_LIST_NETDEV="$(mktemp)" 193 if [ ! -e "$TMP_LIST_NETDEV" ];then 194 echo "FAIL: Cannot create a tmp file" 195 exit 1 196 fi 197 198 ip link show |grep '^[0-9]' | grep -oE '[[:spa 199 while read netdev 200 do 201 kci_test_netdev "$netdev" 202 done < "$TMP_LIST_NETDEV" 203 204 rm "$TMP_LIST_NETDEV" 205 exit 0
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.