netdevice.sh 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 interface (but wifi could be easily added)
  6. #
  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
  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. ip link show "$netdev" |grep -q UP
  17. if [ $? -eq 0 ];then
  18. echo "SKIP: $netdev: interface already up"
  19. return 0
  20. fi
  21. ip link set "$netdev" up
  22. if [ $? -ne 0 ];then
  23. echo "FAIL: $netdev: Fail to up interface"
  24. return 1
  25. else
  26. echo "PASS: $netdev: set interface up"
  27. NETDEV_STARTED=1
  28. fi
  29. return 0
  30. }
  31. # this function will try to setup an IP and MAC address on a network interface
  32. # Doing nothing if the interface was already up
  33. # arg1: network interface name
  34. kci_net_setup()
  35. {
  36. netdev=$1
  37. # do nothing if the interface was already up
  38. if [ $NETDEV_STARTED -eq 0 ];then
  39. return 0
  40. fi
  41. MACADDR='02:03:04:05:06:07'
  42. ip link set dev $netdev address "$MACADDR"
  43. if [ $? -ne 0 ];then
  44. echo "FAIL: $netdev: Cannot set MAC address"
  45. else
  46. ip link show $netdev |grep -q "$MACADDR"
  47. if [ $? -eq 0 ];then
  48. echo "PASS: $netdev: set MAC address"
  49. else
  50. echo "FAIL: $netdev: Cannot set MAC address"
  51. fi
  52. fi
  53. #check that the interface did not already have an IP
  54. ip address show "$netdev" |grep '^[[:space:]]*inet'
  55. if [ $? -eq 0 ];then
  56. echo "SKIP: $netdev: already have an IP"
  57. return 0
  58. fi
  59. # TODO what ipaddr to set ? DHCP ?
  60. echo "SKIP: $netdev: set IP address"
  61. return 0
  62. }
  63. # test an ethtool command
  64. # arg1: return code for not supported (see ethtool code source)
  65. # arg2: summary of the command
  66. # arg3: command to execute
  67. kci_netdev_ethtool_test()
  68. {
  69. if [ $# -le 2 ];then
  70. echo "SKIP: $netdev: ethtool: invalid number of arguments"
  71. return 1
  72. fi
  73. $3 >/dev/null
  74. ret=$?
  75. if [ $ret -ne 0 ];then
  76. if [ $ret -eq "$1" ];then
  77. echo "SKIP: $netdev: ethtool $2 not supported"
  78. else
  79. echo "FAIL: $netdev: ethtool $2"
  80. return 1
  81. fi
  82. else
  83. echo "PASS: $netdev: ethtool $2"
  84. fi
  85. return 0
  86. }
  87. # test ethtool commands
  88. # arg1: network interface name
  89. kci_netdev_ethtool()
  90. {
  91. netdev=$1
  92. #check presence of ethtool
  93. ethtool --version 2>/dev/null >/dev/null
  94. if [ $? -ne 0 ];then
  95. echo "SKIP: ethtool not present"
  96. return 1
  97. fi
  98. TMP_ETHTOOL_FEATURES="$(mktemp)"
  99. if [ ! -e "$TMP_ETHTOOL_FEATURES" ];then
  100. echo "SKIP: Cannot create a tmp file"
  101. return 1
  102. fi
  103. ethtool -k "$netdev" > "$TMP_ETHTOOL_FEATURES"
  104. if [ $? -ne 0 ];then
  105. echo "FAIL: $netdev: ethtool list features"
  106. rm "$TMP_ETHTOOL_FEATURES"
  107. return 1
  108. fi
  109. echo "PASS: $netdev: ethtool list features"
  110. #TODO for each non fixed features, try to turn them on/off
  111. rm "$TMP_ETHTOOL_FEATURES"
  112. kci_netdev_ethtool_test 74 'dump' "ethtool -d $netdev"
  113. kci_netdev_ethtool_test 94 'stats' "ethtool -S $netdev"
  114. return 0
  115. }
  116. # stop a netdev
  117. # arg1: network interface name
  118. kci_netdev_stop()
  119. {
  120. netdev=$1
  121. if [ $NETDEV_STARTED -eq 0 ];then
  122. echo "SKIP: $netdev: interface kept up"
  123. return 0
  124. fi
  125. ip link set "$netdev" down
  126. if [ $? -ne 0 ];then
  127. echo "FAIL: $netdev: stop interface"
  128. return 1
  129. fi
  130. echo "PASS: $netdev: stop interface"
  131. return 0
  132. }
  133. # run all test on a netdev
  134. # arg1: network interface name
  135. kci_test_netdev()
  136. {
  137. NETDEV_STARTED=0
  138. IFACE_TO_UPDOWN="$1"
  139. IFACE_TO_TEST="$1"
  140. #check for VLAN interface
  141. MASTER_IFACE="$(echo $1 | cut -d@ -f2)"
  142. if [ ! -z "$MASTER_IFACE" ];then
  143. IFACE_TO_UPDOWN="$MASTER_IFACE"
  144. IFACE_TO_TEST="$(echo $1 | cut -d@ -f1)"
  145. fi
  146. NETDEV_STARTED=0
  147. kci_net_start "$IFACE_TO_UPDOWN"
  148. kci_net_setup "$IFACE_TO_TEST"
  149. kci_netdev_ethtool "$IFACE_TO_TEST"
  150. kci_netdev_stop "$IFACE_TO_UPDOWN"
  151. return 0
  152. }
  153. #check for needed privileges
  154. if [ "$(id -u)" -ne 0 ];then
  155. echo "SKIP: Need root privileges"
  156. exit 0
  157. fi
  158. ip link show 2>/dev/null >/dev/null
  159. if [ $? -ne 0 ];then
  160. echo "SKIP: Could not run test without the ip tool"
  161. exit 0
  162. fi
  163. TMP_LIST_NETDEV="$(mktemp)"
  164. if [ ! -e "$TMP_LIST_NETDEV" ];then
  165. echo "FAIL: Cannot create a tmp file"
  166. exit 1
  167. fi
  168. 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"
  169. while read netdev
  170. do
  171. kci_test_netdev "$netdev"
  172. done < "$TMP_LIST_NETDEV"
  173. rm "$TMP_LIST_NETDEV"
  174. exit 0