258 lines · bash
1#!/bin/sh2# SPDX-License-Identifier: GPL-2.03#4# This test is for checking network interface5# For the moment it tests only ethernet interface (but wifi could be easily added)6#7# We assume that all network driver are loaded8# if not they probably have failed earlier in the boot process and their logged error will be catched by another test9#10 11# Kselftest framework requirement - SKIP code is 4.12ksft_skip=413 14# this function will try to up the interface15# if already up, nothing done16# arg1: network interface name17kci_net_start()18{19 netdev=$120 21 ip link show "$netdev" |grep -q UP22 if [ $? -eq 0 ];then23 echo "SKIP: $netdev: interface already up"24 return $ksft_skip25 fi26 27 ip link set "$netdev" up28 if [ $? -ne 0 ];then29 echo "FAIL: $netdev: Fail to up interface"30 return 131 else32 echo "PASS: $netdev: set interface up"33 NETDEV_STARTED=134 fi35 return 036}37 38# this function will try to setup an IP and MAC address on a network interface39# Doing nothing if the interface was already up40# arg1: network interface name41kci_net_setup()42{43 netdev=$144 45 # do nothing if the interface was already up46 if [ $NETDEV_STARTED -eq 0 ];then47 return 048 fi49 50 MACADDR='02:03:04:05:06:07'51 ip link set dev $netdev address "$MACADDR"52 if [ $? -ne 0 ];then53 echo "FAIL: $netdev: Cannot set MAC address"54 else55 ip link show $netdev |grep -q "$MACADDR"56 if [ $? -eq 0 ];then57 echo "PASS: $netdev: set MAC address"58 else59 echo "FAIL: $netdev: Cannot set MAC address"60 fi61 fi62 63 #check that the interface did not already have an IP64 ip address show "$netdev" |grep '^[[:space:]]*inet'65 if [ $? -eq 0 ];then66 echo "SKIP: $netdev: already have an IP"67 return $ksft_skip68 fi69 70 if [ "$veth_created" ]; then71 echo "XFAIL: $netdev: set IP address unsupported for veth*"72 else73 # TODO what ipaddr to set ? DHCP ?74 echo "SKIP: $netdev: set IP address"75 fi76 return $ksft_skip77}78 79# test an ethtool command80# arg1: return code for not supported (see ethtool code source)81# arg2: summary of the command82# arg3: command to execute83kci_netdev_ethtool_test()84{85 if [ $# -le 2 ];then86 echo "SKIP: $netdev: ethtool: invalid number of arguments"87 return 188 fi89 $3 >/dev/null90 ret=$?91 if [ $ret -ne 0 ];then92 if [ $ret -eq "$1" ];then93 echo "XFAIL: $netdev: ethtool $2 not supported"94 return $ksft_skip95 else96 echo "FAIL: $netdev: ethtool $2"97 return 198 fi99 else100 echo "PASS: $netdev: ethtool $2"101 fi102 return 0103}104 105# test ethtool commands106# arg1: network interface name107kci_netdev_ethtool()108{109 netdev=$1110 111 #check presence of ethtool112 ethtool --version 2>/dev/null >/dev/null113 if [ $? -ne 0 ];then114 echo "SKIP: ethtool not present"115 return $ksft_skip116 fi117 118 TMP_ETHTOOL_FEATURES="$(mktemp)"119 if [ ! -e "$TMP_ETHTOOL_FEATURES" ];then120 echo "SKIP: Cannot create a tmp file"121 return 1122 fi123 124 ethtool -k "$netdev" > "$TMP_ETHTOOL_FEATURES"125 if [ $? -ne 0 ];then126 echo "FAIL: $netdev: ethtool list features"127 rm "$TMP_ETHTOOL_FEATURES"128 return 1129 fi130 echo "PASS: $netdev: ethtool list features"131 132 while read -r FEATURE VALUE FIXED; do133 [ "$FEATURE" != "Features" ] || continue # Skip "Features"134 [ "$FIXED" != "[fixed]" ] || continue # Skip fixed features135 feature="${FEATURE%:*}"136 137 ethtool --offload "$netdev" "$feature" off138 if [ $? -eq 0 ]; then139 echo "PASS: $netdev: Turned off feature: $feature"140 else141 echo "FAIL: $netdev: Failed to turn off feature:" \142 "$feature"143 fi144 145 ethtool --offload "$netdev" "$feature" on146 if [ $? -eq 0 ]; then147 echo "PASS: $netdev: Turned on feature: $feature"148 else149 echo "FAIL: $netdev: Failed to turn on feature:" \150 "$feature"151 fi152 153 #restore the feature to its initial state154 ethtool --offload "$netdev" "$feature" "$VALUE"155 if [ $? -eq 0 ]; then156 echo "PASS: $netdev: Restore feature $feature" \157 "to initial state $VALUE"158 else159 echo "FAIL: $netdev: Failed to restore feature" \160 "$feature to initial state $VALUE"161 fi162 163 done < "$TMP_ETHTOOL_FEATURES"164 165 rm "$TMP_ETHTOOL_FEATURES"166 167 kci_netdev_ethtool_test 74 'dump' "ethtool -d $netdev"168 kci_netdev_ethtool_test 94 'stats' "ethtool -S $netdev"169 170 return 0171}172 173# stop a netdev174# arg1: network interface name175kci_netdev_stop()176{177 netdev=$1178 179 if [ $NETDEV_STARTED -eq 0 ];then180 echo "SKIP: $netdev: interface kept up"181 return 0182 fi183 184 ip link set "$netdev" down185 if [ $? -ne 0 ];then186 echo "FAIL: $netdev: stop interface"187 return 1188 fi189 echo "PASS: $netdev: stop interface"190 return 0191}192 193# run all test on a netdev194# arg1: network interface name195kci_test_netdev()196{197 NETDEV_STARTED=0198 IFACE_TO_UPDOWN="$1"199 IFACE_TO_TEST="$1"200 #check for VLAN interface201 MASTER_IFACE="$(echo $1 | cut -d@ -f2)"202 if [ ! -z "$MASTER_IFACE" ];then203 IFACE_TO_UPDOWN="$MASTER_IFACE"204 IFACE_TO_TEST="$(echo $1 | cut -d@ -f1)"205 fi206 207 NETDEV_STARTED=0208 kci_net_start "$IFACE_TO_UPDOWN"209 210 kci_net_setup "$IFACE_TO_TEST"211 212 kci_netdev_ethtool "$IFACE_TO_TEST"213 214 kci_netdev_stop "$IFACE_TO_UPDOWN"215 return 0216}217 218#check for needed privileges219if [ "$(id -u)" -ne 0 ];then220 echo "SKIP: Need root privileges"221 exit $ksft_skip222fi223 224ip link show 2>/dev/null >/dev/null225if [ $? -ne 0 ];then226 echo "SKIP: Could not run test without the ip tool"227 exit $ksft_skip228fi229 230TMP_LIST_NETDEV="$(mktemp)"231if [ ! -e "$TMP_LIST_NETDEV" ];then232 echo "FAIL: Cannot create a tmp file"233 exit 1234fi235 236ip 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"237 238if [ ! -s "$TMP_LIST_NETDEV" ]; then239 echo "No valid network device found, creating veth pair"240 ip link add veth0 type veth peer name veth1241 echo "veth0" > "$TMP_LIST_NETDEV"242 veth_created=1243fi244 245while read netdev246do247 kci_test_netdev "$netdev"248done < "$TMP_LIST_NETDEV"249 250#clean up veth interface pair if it was created251if [ "$veth_created" ]; then252 ip link delete veth0253 echo "Removed veth pair"254fi255 256rm "$TMP_LIST_NETDEV"257exit 0258