netdevice.sh 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. # Kselftest framework requirement - SKIP code is 4.
  11. ksft_skip=4
  12. # this function will try to up the interface
  13. # if already up, nothing done
  14. # arg1: network interface name
  15. kci_net_start()
  16. {
  17. netdev=$1
  18. ip link show "$netdev" |grep -q UP
  19. if [ $? -eq 0 ];then
  20. echo "SKIP: $netdev: interface already up"
  21. return $ksft_skip
  22. fi
  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. # this function will try to setup an IP and MAC address on a network interface
  34. # Doing nothing if the interface was already up
  35. # arg1: network interface name
  36. kci_net_setup()
  37. {
  38. netdev=$1
  39. # do nothing if the interface was already up
  40. if [ $NETDEV_STARTED -eq 0 ];then
  41. return 0
  42. fi
  43. MACADDR='02:03:04:05:06:07'
  44. ip link set dev $netdev address "$MACADDR"
  45. if [ $? -ne 0 ];then
  46. echo "FAIL: $netdev: Cannot set MAC address"
  47. else
  48. ip link show $netdev |grep -q "$MACADDR"
  49. if [ $? -eq 0 ];then
  50. echo "PASS: $netdev: set MAC address"
  51. else
  52. echo "FAIL: $netdev: Cannot set MAC address"
  53. fi
  54. fi
  55. #check that the interface did not already have an IP
  56. ip address show "$netdev" |grep '^[[:space:]]*inet'
  57. if [ $? -eq 0 ];then
  58. echo "SKIP: $netdev: already have an IP"
  59. return $ksft_skip
  60. fi
  61. if [ "$veth_created" ]; then
  62. echo "XFAIL: $netdev: set IP address unsupported for veth*"
  63. else
  64. # TODO what ipaddr to set ? DHCP ?
  65. echo "SKIP: $netdev: set IP address"
  66. fi
  67. return $ksft_skip
  68. }
  69. # test an ethtool command
  70. # arg1: return code for not supported (see ethtool code source)
  71. # arg2: summary of the command
  72. # arg3: command to execute
  73. kci_netdev_ethtool_test()
  74. {
  75. if [ $# -le 2 ];then
  76. echo "SKIP: $netdev: ethtool: invalid number of arguments"
  77. return 1
  78. fi
  79. $3 >/dev/null
  80. ret=$?
  81. if [ $ret -ne 0 ];then
  82. if [ $ret -eq "$1" ];then
  83. echo "XFAIL: $netdev: ethtool $2 not supported"
  84. return $ksft_skip
  85. else
  86. echo "FAIL: $netdev: ethtool $2"
  87. return 1
  88. fi
  89. else
  90. echo "PASS: $netdev: ethtool $2"
  91. fi
  92. return 0
  93. }
  94. # test ethtool commands
  95. # arg1: network interface name
  96. kci_netdev_ethtool()
  97. {
  98. netdev=$1
  99. #check presence of ethtool
  100. ethtool --version 2>/dev/null >/dev/null
  101. if [ $? -ne 0 ];then
  102. echo "SKIP: ethtool not present"
  103. return $ksft_skip
  104. fi
  105. TMP_ETHTOOL_FEATURES="$(mktemp)"
  106. if [ ! -e "$TMP_ETHTOOL_FEATURES" ];then
  107. echo "SKIP: Cannot create a tmp file"
  108. return 1
  109. fi
  110. ethtool -k "$netdev" > "$TMP_ETHTOOL_FEATURES"
  111. if [ $? -ne 0 ];then
  112. echo "FAIL: $netdev: ethtool list features"
  113. rm "$TMP_ETHTOOL_FEATURES"
  114. return 1
  115. fi
  116. echo "PASS: $netdev: ethtool list features"
  117. while read -r FEATURE VALUE FIXED; do
  118. [ "$FEATURE" != "Features" ] || continue # Skip "Features"
  119. [ "$FIXED" != "[fixed]" ] || continue # Skip fixed features
  120. feature="${FEATURE%:*}"
  121. ethtool --offload "$netdev" "$feature" off
  122. if [ $? -eq 0 ]; then
  123. echo "PASS: $netdev: Turned off feature: $feature"
  124. else
  125. echo "FAIL: $netdev: Failed to turn off feature:" \
  126. "$feature"
  127. fi
  128. ethtool --offload "$netdev" "$feature" on
  129. if [ $? -eq 0 ]; then
  130. echo "PASS: $netdev: Turned on feature: $feature"
  131. else
  132. echo "FAIL: $netdev: Failed to turn on feature:" \
  133. "$feature"
  134. fi
  135. #restore the feature to its initial state
  136. ethtool --offload "$netdev" "$feature" "$VALUE"
  137. if [ $? -eq 0 ]; then
  138. echo "PASS: $netdev: Restore feature $feature" \
  139. "to initial state $VALUE"
  140. else
  141. echo "FAIL: $netdev: Failed to restore feature" \
  142. "$feature to initial state $VALUE"
  143. fi
  144. done < "$TMP_ETHTOOL_FEATURES"
  145. rm "$TMP_ETHTOOL_FEATURES"
  146. kci_netdev_ethtool_test 74 'dump' "ethtool -d $netdev"
  147. kci_netdev_ethtool_test 94 'stats' "ethtool -S $netdev"
  148. return 0
  149. }
  150. # stop a netdev
  151. # arg1: network interface name
  152. kci_netdev_stop()
  153. {
  154. netdev=$1
  155. if [ $NETDEV_STARTED -eq 0 ];then
  156. echo "SKIP: $netdev: interface kept up"
  157. return 0
  158. fi
  159. ip link set "$netdev" down
  160. if [ $? -ne 0 ];then
  161. echo "FAIL: $netdev: stop interface"
  162. return 1
  163. fi
  164. echo "PASS: $netdev: stop interface"
  165. return 0
  166. }
  167. # run all test on a netdev
  168. # arg1: network interface name
  169. kci_test_netdev()
  170. {
  171. NETDEV_STARTED=0
  172. IFACE_TO_UPDOWN="$1"
  173. IFACE_TO_TEST="$1"
  174. #check for VLAN interface
  175. MASTER_IFACE="$(echo $1 | cut -d@ -f2)"
  176. if [ ! -z "$MASTER_IFACE" ];then
  177. IFACE_TO_UPDOWN="$MASTER_IFACE"
  178. IFACE_TO_TEST="$(echo $1 | cut -d@ -f1)"
  179. fi
  180. NETDEV_STARTED=0
  181. kci_net_start "$IFACE_TO_UPDOWN"
  182. kci_net_setup "$IFACE_TO_TEST"
  183. kci_netdev_ethtool "$IFACE_TO_TEST"
  184. kci_netdev_stop "$IFACE_TO_UPDOWN"
  185. return 0
  186. }
  187. #check for needed privileges
  188. if [ "$(id -u)" -ne 0 ];then
  189. echo "SKIP: Need root privileges"
  190. exit $ksft_skip
  191. fi
  192. ip link show 2>/dev/null >/dev/null
  193. if [ $? -ne 0 ];then
  194. echo "SKIP: Could not run test without the ip tool"
  195. exit $ksft_skip
  196. fi
  197. TMP_LIST_NETDEV="$(mktemp)"
  198. if [ ! -e "$TMP_LIST_NETDEV" ];then
  199. echo "FAIL: Cannot create a tmp file"
  200. exit 1
  201. fi
  202. 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"
  203. if [ ! -s "$TMP_LIST_NETDEV" ]; then
  204. echo "No valid network device found, creating veth pair"
  205. ip link add veth0 type veth peer name veth1
  206. echo "veth0" > "$TMP_LIST_NETDEV"
  207. veth_created=1
  208. fi
  209. while read netdev
  210. do
  211. kci_test_netdev "$netdev"
  212. done < "$TMP_LIST_NETDEV"
  213. #clean up veth interface pair if it was created
  214. if [ "$veth_created" ]; then
  215. ip link delete veth0
  216. echo "Removed veth pair"
  217. fi
  218. rm "$TMP_LIST_NETDEV"
  219. exit 0