Path: blob/master/tools/testing/selftests/drivers/net/hw/ethtool_lib.sh
26295 views
#!/bin/bash1# SPDX-License-Identifier: GPL-2.023speeds_arr_get()4{5cmd='/ETHTOOL_LINK_MODE_[^[:space:]]*_BIT[[:space:]]+=[[:space:]]+/ \6{sub(/,$/, "") \7sub(/ETHTOOL_LINK_MODE_/,"") \8sub(/_BIT/,"") \9sub(/_Full/,"/Full") \10sub(/_Half/,"/Half");\11print "["$1"]="$3}'1213awk "${cmd}" /usr/include/linux/ethtool.h14}1516ethtool_set()17{18local cmd="$@"19local out=$(ethtool -s $cmd 2>&1 | wc -l)2021check_err $out "error in configuration. $cmd"22}2324dev_linkmodes_params_get()25{26local dev=$1; shift27local adver=$1; shift28local -a linkmodes_params29local param_count30local arr3132if (($adver)); then33mode="Advertised link modes"34else35mode="Supported link modes"36fi3738local -a dev_linkmodes=($(dev_speeds_get $dev 1 $adver))39for ((i=0; i<${#dev_linkmodes[@]}; i++)); do40linkmodes_params[$i]=$(echo -e "${dev_linkmodes[$i]}" | \41# Replaces all non numbers with spaces42sed -e 's/[^0-9]/ /g' | \43# Squeeze spaces in sequence to 1 space44tr -s ' ')45# Count how many numbers were found in the linkmode46param_count=$(echo "${linkmodes_params[$i]}" | wc -w)47if [[ $param_count -eq 1 ]]; then48linkmodes_params[$i]="${linkmodes_params[$i]} 1"49elif [[ $param_count -ge 3 ]]; then50arr=(${linkmodes_params[$i]})51# Take only first two params52linkmodes_params[$i]=$(echo "${arr[@]:0:2}")53fi54done55echo ${linkmodes_params[@]}56}5758dev_speeds_get()59{60local dev=$1; shift61local with_mode=$1; shift62local adver=$1; shift63local speeds_str6465if (($adver)); then66mode="Advertised link modes"67else68mode="Supported link modes"69fi7071speeds_str=$(ethtool "$dev" | \72# Snip everything before the link modes section.73sed -n '/'"$mode"':/,$p' | \74# Quit processing the rest at the start of the next section.75# When checking, skip the header of this section (hence the 2,).76sed -n '2,${/^[\t][^ \t]/q};p' | \77# Drop the section header of the current section.78cut -d':' -f2)7980local -a speeds_arr=($speeds_str)81if [[ $with_mode -eq 0 ]]; then82for ((i=0; i<${#speeds_arr[@]}; i++)); do83speeds_arr[$i]=${speeds_arr[$i]%base*}84done85fi86echo ${speeds_arr[@]}87}8889common_speeds_get()90{91dev1=$1; shift92dev2=$1; shift93with_mode=$1; shift94adver=$1; shift9596local -a dev1_speeds=($(dev_speeds_get $dev1 $with_mode $adver))97local -a dev2_speeds=($(dev_speeds_get $dev2 $with_mode $adver))9899comm -12 \100<(printf '%s\n' "${dev1_speeds[@]}" | sort -u) \101<(printf '%s\n' "${dev2_speeds[@]}" | sort -u)102}103104different_speeds_get()105{106local dev1=$1; shift107local dev2=$1; shift108local with_mode=$1; shift109local adver=$1; shift110111local -a speeds_arr112113speeds_arr=($(common_speeds_get $dev1 $dev2 $with_mode $adver))114if [[ ${#speeds_arr[@]} < 2 ]]; then115check_err 1 "cannot check different speeds. There are not enough speeds"116fi117118echo ${speeds_arr[0]} ${speeds_arr[1]}119}120121122