Path: blob/master/tools/testing/selftests/drivers/net/hw/ethtool.sh
26295 views
#!/bin/bash1# SPDX-License-Identifier: GPL-2.023ALL_TESTS="4same_speeds_autoneg_off5different_speeds_autoneg_off6combination_of_neg_on_and_off7advertise_subset_of_speeds8check_highest_speed_is_chosen9different_speeds_autoneg_on10"11NUM_NETIFS=212lib_dir=$(dirname "$0")13source "$lib_dir"/../../../net/forwarding/lib.sh14source ethtool_lib.sh1516h1_create()17{18simple_if_init $h1 192.0.2.1/2419}2021h1_destroy()22{23simple_if_fini $h1 192.0.2.1/2424}2526h2_create()27{28simple_if_init $h2 192.0.2.2/2429}3031h2_destroy()32{33simple_if_fini $h2 192.0.2.2/2434}3536setup_prepare()37{38h1=${NETIFS[p1]}39h2=${NETIFS[p2]}4041h1_create42h2_create43}4445cleanup()46{47pre_cleanup4849h2_destroy50h1_destroy51}5253same_speeds_autoneg_off()54{55# Check that when each of the reported speeds is forced, the links come56# up and are operational.57local -a speeds_arr=($(common_speeds_get $h1 $h2 0 0))5859for speed in "${speeds_arr[@]}"; do60RET=061ethtool_set $h1 speed $speed autoneg off62ethtool_set $h2 speed $speed autoneg off6364setup_wait_dev_with_timeout $h165setup_wait_dev_with_timeout $h266ping_do $h1 192.0.2.267check_err $? "ping with speed $speed autoneg off"68log_test "force speed $speed on both ends"69done7071ethtool -s $h2 autoneg on72ethtool -s $h1 autoneg on73}7475different_speeds_autoneg_off()76{77# Test that when we force different speeds, links are not up and ping78# fails.79RET=08081local -a speeds_arr=($(different_speeds_get $h1 $h2 0 0))82local speed1=${speeds_arr[0]}83local speed2=${speeds_arr[1]}8485ethtool_set $h1 speed $speed1 autoneg off86ethtool_set $h2 speed $speed2 autoneg off8788setup_wait_dev_with_timeout $h189setup_wait_dev_with_timeout $h290ping_do $h1 192.0.2.291check_fail $? "ping with different speeds"9293log_test "force of different speeds autoneg off"9495ethtool -s $h2 autoneg on96ethtool -s $h1 autoneg on97}9899combination_of_neg_on_and_off()100{101# Test that when one device is forced to a speed supported by both102# endpoints and the other device is configured to autoneg on, the links103# are up and ping passes.104local -a speeds_arr=($(common_speeds_get $h1 $h2 0 1))105106for speed in "${speeds_arr[@]}"; do107RET=0108ethtool_set $h1 speed $speed autoneg off109110setup_wait_dev_with_timeout $h1111setup_wait_dev_with_timeout $h2112ping_do $h1 192.0.2.2113check_err $? "ping with h1-speed=$speed autoneg off, h2 autoneg on"114log_test "force speed $speed vs. autoneg"115done116117ethtool -s $h1 autoneg on118}119120hex_speed_value_get()121{122local speed=$1; shift123124local shift_size=${speed_values[$speed]}125speed=$((0x1 << $"shift_size"))126printf "%#x" "$speed"127}128129subset_of_common_speeds_get()130{131local dev1=$1; shift132local dev2=$1; shift133local adver=$1; shift134135local -a speeds_arr=($(common_speeds_get $dev1 $dev2 0 $adver))136local speed_to_advertise=0137local speed_to_remove=${speeds_arr[0]}138speed_to_remove+='base'139140local -a speeds_mode_arr=($(common_speeds_get $dev1 $dev2 1 $adver))141142for speed in ${speeds_mode_arr[@]}; do143if [[ $speed != $speed_to_remove* ]]; then144speed=$(hex_speed_value_get $speed)145speed_to_advertise=$(($speed_to_advertise | \146$speed))147fi148149done150151# Convert to hex.152printf "%#x" "$speed_to_advertise"153}154155speed_to_advertise_get()156{157# The function returns the hex number that is composed by OR-ing all158# the modes corresponding to the provided speed.159local speed_without_mode=$1; shift160local supported_speeds=("$@"); shift161local speed_to_advertise=0162163speed_without_mode+='base'164165for speed in ${supported_speeds[@]}; do166if [[ $speed == $speed_without_mode* ]]; then167speed=$(hex_speed_value_get $speed)168speed_to_advertise=$(($speed_to_advertise | \169$speed))170fi171172done173174# Convert to hex.175printf "%#x" "$speed_to_advertise"176}177178advertise_subset_of_speeds()179{180# Test that when one device advertises a subset of speeds and another181# advertises a specific speed (but all modes of this speed), the links182# are up and ping passes.183RET=0184185local speed_1_to_advertise=$(subset_of_common_speeds_get $h1 $h2 1)186ethtool_set $h1 advertise $speed_1_to_advertise187188if [ $RET != 0 ]; then189log_test "advertise subset of speeds"190return191fi192193local -a speeds_arr_without_mode=($(common_speeds_get $h1 $h2 0 1))194# Check only speeds that h1 advertised. Remove the first speed.195unset speeds_arr_without_mode[0]196local -a speeds_arr_with_mode=($(common_speeds_get $h1 $h2 1 1))197198for speed_value in ${speeds_arr_without_mode[@]}; do199RET=0200local speed_2_to_advertise=$(speed_to_advertise_get $speed_value \201"${speeds_arr_with_mode[@]}")202ethtool_set $h2 advertise $speed_2_to_advertise203204setup_wait_dev_with_timeout $h1205setup_wait_dev_with_timeout $h2206ping_do $h1 192.0.2.2207check_err $? "ping with h1=$speed_1_to_advertise, h2=$speed_2_to_advertise ($speed_value)"208209log_test "advertise $speed_1_to_advertise vs. $speed_2_to_advertise"210done211212ethtool -s $h2 autoneg on213ethtool -s $h1 autoneg on214}215216check_highest_speed_is_chosen()217{218# Test that when one device advertises a subset of speeds, the other219# chooses the highest speed. This test checks configuration without220# traffic.221RET=0222223local max_speed224local chosen_speed225local speed_to_advertise=$(subset_of_common_speeds_get $h1 $h2 1)226227ethtool_set $h1 advertise $speed_to_advertise228229if [ $RET != 0 ]; then230log_test "check highest speed"231return232fi233234local -a speeds_arr=($(common_speeds_get $h1 $h2 0 1))235236max_speed=${speeds_arr[0]}237for current in ${speeds_arr[@]}; do238if [[ $current -gt $max_speed ]]; then239max_speed=$current240fi241done242243setup_wait_dev_with_timeout $h1244setup_wait_dev_with_timeout $h2245chosen_speed=$(ethtool $h1 | grep 'Speed:')246chosen_speed=${chosen_speed%"Mb/s"*}247chosen_speed=${chosen_speed#*"Speed: "}248((chosen_speed == max_speed))249check_err $? "h1 advertise $speed_to_advertise, h2 sync to speed $chosen_speed"250251log_test "check highest speed"252253ethtool -s $h2 autoneg on254ethtool -s $h1 autoneg on255}256257different_speeds_autoneg_on()258{259# Test that when we configure links to advertise different speeds,260# links are not up and ping fails.261RET=0262263local -a speeds=($(different_speeds_get $h1 $h2 1 1))264local speed1=${speeds[0]}265local speed2=${speeds[1]}266267speed1=$(hex_speed_value_get $speed1)268speed2=$(hex_speed_value_get $speed2)269270ethtool_set $h1 advertise $speed1271ethtool_set $h2 advertise $speed2272273if (($RET)); then274setup_wait_dev_with_timeout $h1275setup_wait_dev_with_timeout $h2276ping_do $h1 192.0.2.2277check_fail $? "ping with different speeds autoneg on"278fi279280log_test "advertise different speeds autoneg on"281282ethtool -s $h2 autoneg on283ethtool -s $h1 autoneg on284}285286trap cleanup EXIT287288setup_prepare289setup_wait290291declare -gA speed_values292eval "speed_values=($(speeds_arr_get))"293294tests_run295296exit $EXIT_STATUS297298299