ethtool.sh 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. #!/bin/bash
  2. # SPDX-License-Identifier: GPL-2.0
  3. ALL_TESTS="
  4. same_speeds_autoneg_off
  5. different_speeds_autoneg_off
  6. combination_of_neg_on_and_off
  7. advertise_subset_of_speeds
  8. check_highest_speed_is_chosen
  9. different_speeds_autoneg_on
  10. "
  11. NUM_NETIFS=2
  12. lib_dir=$(dirname "$0")
  13. source "$lib_dir"/../../../net/forwarding/lib.sh
  14. source ethtool_lib.sh
  15. h1_create()
  16. {
  17. simple_if_init $h1 192.0.2.1/24
  18. }
  19. h1_destroy()
  20. {
  21. simple_if_fini $h1 192.0.2.1/24
  22. }
  23. h2_create()
  24. {
  25. simple_if_init $h2 192.0.2.2/24
  26. }
  27. h2_destroy()
  28. {
  29. simple_if_fini $h2 192.0.2.2/24
  30. }
  31. setup_prepare()
  32. {
  33. h1=${NETIFS[p1]}
  34. h2=${NETIFS[p2]}
  35. h1_create
  36. h2_create
  37. }
  38. cleanup()
  39. {
  40. pre_cleanup
  41. h2_destroy
  42. h1_destroy
  43. }
  44. same_speeds_autoneg_off()
  45. {
  46. # Check that when each of the reported speeds is forced, the links come
  47. # up and are operational.
  48. local -a speeds_arr=($(common_speeds_get $h1 $h2 0 0))
  49. for speed in "${speeds_arr[@]}"; do
  50. RET=0
  51. ethtool_set $h1 speed $speed autoneg off
  52. ethtool_set $h2 speed $speed autoneg off
  53. setup_wait_dev_with_timeout $h1
  54. setup_wait_dev_with_timeout $h2
  55. ping_do $h1 192.0.2.2
  56. check_err $? "ping with speed $speed autoneg off"
  57. log_test "force speed $speed on both ends"
  58. done
  59. ethtool -s $h2 autoneg on
  60. ethtool -s $h1 autoneg on
  61. }
  62. different_speeds_autoneg_off()
  63. {
  64. # Test that when we force different speeds, links are not up and ping
  65. # fails.
  66. RET=0
  67. local -a speeds_arr=($(different_speeds_get $h1 $h2 0 0))
  68. local speed1=${speeds_arr[0]}
  69. local speed2=${speeds_arr[1]}
  70. ethtool_set $h1 speed $speed1 autoneg off
  71. ethtool_set $h2 speed $speed2 autoneg off
  72. setup_wait_dev_with_timeout $h1
  73. setup_wait_dev_with_timeout $h2
  74. ping_do $h1 192.0.2.2
  75. check_fail $? "ping with different speeds"
  76. log_test "force of different speeds autoneg off"
  77. ethtool -s $h2 autoneg on
  78. ethtool -s $h1 autoneg on
  79. }
  80. combination_of_neg_on_and_off()
  81. {
  82. # Test that when one device is forced to a speed supported by both
  83. # endpoints and the other device is configured to autoneg on, the links
  84. # are up and ping passes.
  85. local -a speeds_arr=($(common_speeds_get $h1 $h2 0 1))
  86. for speed in "${speeds_arr[@]}"; do
  87. RET=0
  88. ethtool_set $h1 speed $speed autoneg off
  89. setup_wait_dev_with_timeout $h1
  90. setup_wait_dev_with_timeout $h2
  91. ping_do $h1 192.0.2.2
  92. check_err $? "ping with h1-speed=$speed autoneg off, h2 autoneg on"
  93. log_test "force speed $speed vs. autoneg"
  94. done
  95. ethtool -s $h1 autoneg on
  96. }
  97. hex_speed_value_get()
  98. {
  99. local speed=$1; shift
  100. local shift_size=${speed_values[$speed]}
  101. speed=$((0x1 << $"shift_size"))
  102. printf "%#x" "$speed"
  103. }
  104. subset_of_common_speeds_get()
  105. {
  106. local dev1=$1; shift
  107. local dev2=$1; shift
  108. local adver=$1; shift
  109. local -a speeds_arr=($(common_speeds_get $dev1 $dev2 0 $adver))
  110. local speed_to_advertise=0
  111. local speed_to_remove=${speeds_arr[0]}
  112. speed_to_remove+='base'
  113. local -a speeds_mode_arr=($(common_speeds_get $dev1 $dev2 1 $adver))
  114. for speed in ${speeds_mode_arr[@]}; do
  115. if [[ $speed != $speed_to_remove* ]]; then
  116. speed=$(hex_speed_value_get $speed)
  117. speed_to_advertise=$(($speed_to_advertise | \
  118. $speed))
  119. fi
  120. done
  121. # Convert to hex.
  122. printf "%#x" "$speed_to_advertise"
  123. }
  124. speed_to_advertise_get()
  125. {
  126. # The function returns the hex number that is composed by OR-ing all
  127. # the modes corresponding to the provided speed.
  128. local speed_without_mode=$1; shift
  129. local supported_speeds=("$@"); shift
  130. local speed_to_advertise=0
  131. speed_without_mode+='base'
  132. for speed in ${supported_speeds[@]}; do
  133. if [[ $speed == $speed_without_mode* ]]; then
  134. speed=$(hex_speed_value_get $speed)
  135. speed_to_advertise=$(($speed_to_advertise | \
  136. $speed))
  137. fi
  138. done
  139. # Convert to hex.
  140. printf "%#x" "$speed_to_advertise"
  141. }
  142. advertise_subset_of_speeds()
  143. {
  144. # Test that when one device advertises a subset of speeds and another
  145. # advertises a specific speed (but all modes of this speed), the links
  146. # are up and ping passes.
  147. RET=0
  148. local speed_1_to_advertise=$(subset_of_common_speeds_get $h1 $h2 1)
  149. ethtool_set $h1 advertise $speed_1_to_advertise
  150. if [ $RET != 0 ]; then
  151. log_test "advertise subset of speeds"
  152. return
  153. fi
  154. local -a speeds_arr_without_mode=($(common_speeds_get $h1 $h2 0 1))
  155. # Check only speeds that h1 advertised. Remove the first speed.
  156. unset speeds_arr_without_mode[0]
  157. local -a speeds_arr_with_mode=($(common_speeds_get $h1 $h2 1 1))
  158. for speed_value in ${speeds_arr_without_mode[@]}; do
  159. RET=0
  160. local speed_2_to_advertise=$(speed_to_advertise_get $speed_value \
  161. "${speeds_arr_with_mode[@]}")
  162. ethtool_set $h2 advertise $speed_2_to_advertise
  163. setup_wait_dev_with_timeout $h1
  164. setup_wait_dev_with_timeout $h2
  165. ping_do $h1 192.0.2.2
  166. check_err $? "ping with h1=$speed_1_to_advertise, h2=$speed_2_to_advertise ($speed_value)"
  167. log_test "advertise $speed_1_to_advertise vs. $speed_2_to_advertise"
  168. done
  169. ethtool -s $h2 autoneg on
  170. ethtool -s $h1 autoneg on
  171. }
  172. check_highest_speed_is_chosen()
  173. {
  174. # Test that when one device advertises a subset of speeds, the other
  175. # chooses the highest speed. This test checks configuration without
  176. # traffic.
  177. RET=0
  178. local max_speed
  179. local chosen_speed
  180. local speed_to_advertise=$(subset_of_common_speeds_get $h1 $h2 1)
  181. ethtool_set $h1 advertise $speed_to_advertise
  182. if [ $RET != 0 ]; then
  183. log_test "check highest speed"
  184. return
  185. fi
  186. local -a speeds_arr=($(common_speeds_get $h1 $h2 0 1))
  187. max_speed=${speeds_arr[0]}
  188. for current in ${speeds_arr[@]}; do
  189. if [[ $current -gt $max_speed ]]; then
  190. max_speed=$current
  191. fi
  192. done
  193. setup_wait_dev_with_timeout $h1
  194. setup_wait_dev_with_timeout $h2
  195. chosen_speed=$(ethtool $h1 | grep 'Speed:')
  196. chosen_speed=${chosen_speed%"Mb/s"*}
  197. chosen_speed=${chosen_speed#*"Speed: "}
  198. ((chosen_speed == max_speed))
  199. check_err $? "h1 advertise $speed_to_advertise, h2 sync to speed $chosen_speed"
  200. log_test "check highest speed"
  201. ethtool -s $h2 autoneg on
  202. ethtool -s $h1 autoneg on
  203. }
  204. different_speeds_autoneg_on()
  205. {
  206. # Test that when we configure links to advertise different speeds,
  207. # links are not up and ping fails.
  208. RET=0
  209. local -a speeds=($(different_speeds_get $h1 $h2 1 1))
  210. local speed1=${speeds[0]}
  211. local speed2=${speeds[1]}
  212. speed1=$(hex_speed_value_get $speed1)
  213. speed2=$(hex_speed_value_get $speed2)
  214. ethtool_set $h1 advertise $speed1
  215. ethtool_set $h2 advertise $speed2
  216. if (($RET)); then
  217. setup_wait_dev_with_timeout $h1
  218. setup_wait_dev_with_timeout $h2
  219. ping_do $h1 192.0.2.2
  220. check_fail $? "ping with different speeds autoneg on"
  221. fi
  222. log_test "advertise different speeds autoneg on"
  223. ethtool -s $h2 autoneg on
  224. ethtool -s $h1 autoneg on
  225. }
  226. trap cleanup EXIT
  227. setup_prepare
  228. setup_wait
  229. declare -gA speed_values
  230. eval "speed_values=($(speeds_arr_get))"
  231. tests_run
  232. exit $EXIT_STATUS