Skip to content

Commit f72e2f4

Browse files
daniellertskuba-moo
authored andcommitted
net: selftests: Add lanes setting test
Test that setting lanes parameter is working. Set max speed and max lanes in the list of advertised link modes, and then try to set max speed with the lanes below max lanes if exists in the list. And then, test that setting number of lanes larger than max lanes fails. Do the above for both autoneg on and off. $ ./ethtool_lanes.sh TEST: 4 lanes is autonegotiated [ OK ] TEST: Lanes number larger than max width is not set [ OK ] TEST: Autoneg off, 4 lanes detected during force mode [ OK ] TEST: Lanes number larger than max width is not set [ OK ] Signed-off-by: Danielle Ratson <[email protected]> Signed-off-by: Jakub Kicinski <[email protected]>
1 parent 25a96f0 commit f72e2f4

File tree

3 files changed

+249
-0
lines changed

3 files changed

+249
-0
lines changed
Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
#!/bin/bash
2+
# SPDX-License-Identifier: GPL-2.0
3+
4+
lib_dir=$(dirname $0)/../../../net/forwarding
5+
6+
ALL_TESTS="
7+
autoneg
8+
autoneg_force_mode
9+
"
10+
11+
NUM_NETIFS=2
12+
: ${TIMEOUT:=30000} # ms
13+
source $lib_dir/lib.sh
14+
source $lib_dir/ethtool_lib.sh
15+
16+
setup_prepare()
17+
{
18+
swp1=${NETIFS[p1]}
19+
swp2=${NETIFS[p2]}
20+
21+
ip link set dev $swp1 up
22+
ip link set dev $swp2 up
23+
24+
busywait "$TIMEOUT" wait_for_port_up ethtool $swp2
25+
check_err $? "ports did not come up"
26+
27+
local lanes_exist=$(ethtool $swp1 | grep 'Lanes:')
28+
if [[ -z $lanes_exist ]]; then
29+
log_test "SKIP: driver does not support lanes setting"
30+
exit 1
31+
fi
32+
33+
ip link set dev $swp2 down
34+
ip link set dev $swp1 down
35+
}
36+
37+
check_lanes()
38+
{
39+
local dev=$1; shift
40+
local lanes=$1; shift
41+
local max_speed=$1; shift
42+
local chosen_lanes
43+
44+
chosen_lanes=$(ethtool $dev | grep 'Lanes:')
45+
chosen_lanes=${chosen_lanes#*"Lanes: "}
46+
47+
((chosen_lanes == lanes))
48+
check_err $? "swp1 advertise $max_speed and $lanes, devs sync to $chosen_lanes"
49+
}
50+
51+
check_unsupported_lanes()
52+
{
53+
local dev=$1; shift
54+
local max_speed=$1; shift
55+
local max_lanes=$1; shift
56+
local autoneg=$1; shift
57+
local autoneg_str=""
58+
59+
local unsupported_lanes=$((max_lanes *= 2))
60+
61+
if [[ $autoneg -eq 0 ]]; then
62+
autoneg_str="autoneg off"
63+
fi
64+
65+
ethtool -s $swp1 speed $max_speed lanes $unsupported_lanes $autoneg_str &> /dev/null
66+
check_fail $? "Unsuccessful $unsupported_lanes lanes setting was expected"
67+
}
68+
69+
max_speed_and_lanes_get()
70+
{
71+
local dev=$1; shift
72+
local arr=("$@")
73+
local max_lanes
74+
local max_speed
75+
local -a lanes_arr
76+
local -a speeds_arr
77+
local -a max_values
78+
79+
for ((i=0; i<${#arr[@]}; i+=2)); do
80+
speeds_arr+=("${arr[$i]}")
81+
lanes_arr+=("${arr[i+1]}")
82+
done
83+
84+
max_values+=($(get_max "${speeds_arr[@]}"))
85+
max_values+=($(get_max "${lanes_arr[@]}"))
86+
87+
echo ${max_values[@]}
88+
}
89+
90+
search_linkmode()
91+
{
92+
local speed=$1; shift
93+
local lanes=$1; shift
94+
local arr=("$@")
95+
96+
for ((i=0; i<${#arr[@]}; i+=2)); do
97+
if [[ $speed -eq ${arr[$i]} && $lanes -eq ${arr[i+1]} ]]; then
98+
return 1
99+
fi
100+
done
101+
return 0
102+
}
103+
104+
autoneg()
105+
{
106+
RET=0
107+
108+
local lanes
109+
local max_speed
110+
local max_lanes
111+
112+
local -a linkmodes_params=($(dev_linkmodes_params_get $swp1 1))
113+
local -a max_values=($(max_speed_and_lanes_get $swp1 "${linkmodes_params[@]}"))
114+
max_speed=${max_values[0]}
115+
max_lanes=${max_values[1]}
116+
117+
lanes=$max_lanes
118+
119+
while [[ $lanes -ge 1 ]]; do
120+
search_linkmode $max_speed $lanes "${linkmodes_params[@]}"
121+
if [[ $? -eq 1 ]]; then
122+
ethtool_set $swp1 speed $max_speed lanes $lanes
123+
ip link set dev $swp1 up
124+
ip link set dev $swp2 up
125+
busywait "$TIMEOUT" wait_for_port_up ethtool $swp2
126+
check_err $? "ports did not come up"
127+
128+
check_lanes $swp1 $lanes $max_speed
129+
log_test "$lanes lanes is autonegotiated"
130+
fi
131+
let $((lanes /= 2))
132+
done
133+
134+
check_unsupported_lanes $swp1 $max_speed $max_lanes 1
135+
log_test "Lanes number larger than max width is not set"
136+
137+
ip link set dev $swp2 down
138+
ip link set dev $swp1 down
139+
}
140+
141+
autoneg_force_mode()
142+
{
143+
RET=0
144+
145+
local lanes
146+
local max_speed
147+
local max_lanes
148+
149+
local -a linkmodes_params=($(dev_linkmodes_params_get $swp1 1))
150+
local -a max_values=($(max_speed_and_lanes_get $swp1 "${linkmodes_params[@]}"))
151+
max_speed=${max_values[0]}
152+
max_lanes=${max_values[1]}
153+
154+
lanes=$max_lanes
155+
156+
while [[ $lanes -ge 1 ]]; do
157+
search_linkmode $max_speed $lanes "${linkmodes_params[@]}"
158+
if [[ $? -eq 1 ]]; then
159+
ethtool_set $swp1 speed $max_speed lanes $lanes autoneg off
160+
ethtool_set $swp2 speed $max_speed lanes $lanes autoneg off
161+
ip link set dev $swp1 up
162+
ip link set dev $swp2 up
163+
busywait "$TIMEOUT" wait_for_port_up ethtool $swp2
164+
check_err $? "ports did not come up"
165+
166+
check_lanes $swp1 $lanes $max_speed
167+
log_test "Autoneg off, $lanes lanes detected during force mode"
168+
fi
169+
let $((lanes /= 2))
170+
done
171+
172+
check_unsupported_lanes $swp1 $max_speed $max_lanes 0
173+
log_test "Lanes number larger than max width is not set"
174+
175+
ip link set dev $swp2 down
176+
ip link set dev $swp1 down
177+
178+
ethtool -s $swp2 autoneg on
179+
ethtool -s $swp1 autoneg on
180+
}
181+
182+
check_ethtool_lanes_support
183+
setup_prepare
184+
185+
tests_run
186+
187+
exit $EXIT_STATUS

tools/testing/selftests/net/forwarding/ethtool_lib.sh

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,40 @@ ethtool_set()
2222
check_err $out "error in configuration. $cmd"
2323
}
2424

25+
dev_linkmodes_params_get()
26+
{
27+
local dev=$1; shift
28+
local adver=$1; shift
29+
local -a linkmodes_params
30+
local param_count
31+
local arr
32+
33+
if (($adver)); then
34+
mode="Advertised link modes"
35+
else
36+
mode="Supported link modes"
37+
fi
38+
39+
local -a dev_linkmodes=($(dev_speeds_get $dev 1 $adver))
40+
for ((i=0; i<${#dev_linkmodes[@]}; i++)); do
41+
linkmodes_params[$i]=$(echo -e "${dev_linkmodes[$i]}" | \
42+
# Replaces all non numbers with spaces
43+
sed -e 's/[^0-9]/ /g' | \
44+
# Squeeze spaces in sequence to 1 space
45+
tr -s ' ')
46+
# Count how many numbers were found in the linkmode
47+
param_count=$(echo "${linkmodes_params[$i]}" | wc -w)
48+
if [[ $param_count -eq 1 ]]; then
49+
linkmodes_params[$i]="${linkmodes_params[$i]} 1"
50+
elif [[ $param_count -ge 3 ]]; then
51+
arr=(${linkmodes_params[$i]})
52+
# Take only first two params
53+
linkmodes_params[$i]=$(echo "${arr[@]:0:2}")
54+
fi
55+
done
56+
echo ${linkmodes_params[@]}
57+
}
58+
2559
dev_speeds_get()
2660
{
2761
local dev=$1; shift

tools/testing/selftests/net/forwarding/lib.sh

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,15 @@ check_tc_action_hw_stats_support()
6969
fi
7070
}
7171

72+
check_ethtool_lanes_support()
73+
{
74+
ethtool --help 2>&1| grep lanes &> /dev/null
75+
if [[ $? -ne 0 ]]; then
76+
echo "SKIP: ethtool too old; it is missing lanes support"
77+
exit 1
78+
fi
79+
}
80+
7281
if [[ "$(id -u)" -ne 0 ]]; then
7382
echo "SKIP: need root privileges"
7483
exit 0
@@ -263,6 +272,20 @@ not()
263272
[[ $? != 0 ]]
264273
}
265274

275+
get_max()
276+
{
277+
local arr=("$@")
278+
279+
max=${arr[0]}
280+
for cur in ${arr[@]}; do
281+
if [[ $cur -gt $max ]]; then
282+
max=$cur
283+
fi
284+
done
285+
286+
echo $max
287+
}
288+
266289
grep_bridge_fdb()
267290
{
268291
local addr=$1; shift
@@ -279,6 +302,11 @@ grep_bridge_fdb()
279302
$@ | grep $addr | grep $flag "$word"
280303
}
281304

305+
wait_for_port_up()
306+
{
307+
"$@" | grep -q "Link detected: yes"
308+
}
309+
282310
wait_for_offload()
283311
{
284312
"$@" | grep -q offload

0 commit comments

Comments
 (0)