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