Skip to content

Commit 3d578d8

Browse files
idoschdavem330
authored andcommitted
selftests: forwarding: Test IPv4 weighted nexthops
Use different weights for the multipath route configured on the first router and check that the different flows generated by the first host are distributed according to the provided weights. Signed-off-by: Ido Schimmel <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 937eeb3 commit 3d578d8

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,13 @@ log_test()
113113
return 0
114114
}
115115

116+
log_info()
117+
{
118+
local msg=$1
119+
120+
echo "INFO: $msg"
121+
}
122+
116123
setup_wait()
117124
{
118125
for i in $(eval echo {1..$NUM_NETIFS}); do
@@ -256,6 +263,13 @@ master_name_get()
256263
ip -j link show dev $if_name | jq -r '.[]["master"]'
257264
}
258265

266+
link_stats_tx_packets_get()
267+
{
268+
local if_name=$1
269+
270+
ip -j -s link show dev $if_name | jq '.[]["stats64"]["tx"]["packets"]'
271+
}
272+
259273
bridge_ageing_time_get()
260274
{
261275
local bridge=$1

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

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,82 @@ router2_destroy()
158158
vrf_destroy "vrf-r2"
159159
}
160160

161+
multipath_eval()
162+
{
163+
local weight_rp12=$1
164+
local weight_rp13=$2
165+
local packets_rp12=$3
166+
local packets_rp13=$4
167+
local weights_ratio packets_ratio diff
168+
169+
RET=0
170+
171+
if [[ "$weight_rp12" -gt "$weight_rp13" ]]; then
172+
weights_ratio=$(echo "scale=2; $weight_rp12 / $weight_rp13" \
173+
| bc -l)
174+
packets_ratio=$(echo "scale=2; $packets_rp12 / $packets_rp13" \
175+
| bc -l)
176+
else
177+
weights_ratio=$(echo "scale=2; $weight_rp13 / $weight_rp12" | \
178+
bc -l)
179+
packets_ratio=$(echo "scale=2; $packets_rp13 / $packets_rp12" | \
180+
bc -l)
181+
fi
182+
183+
diff=$(echo $weights_ratio - $packets_ratio | bc -l)
184+
diff=${diff#-}
185+
186+
test "$(echo "$diff / $weights_ratio > 0.1" | bc -l)" -eq 0
187+
check_err $? "Too large discrepancy between expected and measured ratios"
188+
log_test "Multipath"
189+
log_info "Expected ratio $weights_ratio Measured ratio $packets_ratio"
190+
}
191+
192+
multipath4_test()
193+
{
194+
local weight_rp12=$1
195+
local weight_rp13=$2
196+
local t0_rp12 t0_rp13 t1_rp12 t1_rp13
197+
local packets_rp12 packets_rp13
198+
local hash_policy
199+
200+
# Transmit multiple flows from h1 to h2 and make sure they are
201+
# distributed between both multipath links (rp12 and rp13)
202+
# according to the configured weights.
203+
hash_policy=$(sysctl -n net.ipv4.fib_multipath_hash_policy)
204+
sysctl -q -w net.ipv4.fib_multipath_hash_policy=1
205+
ip route replace 198.51.100.0/24 vrf vrf-r1 \
206+
nexthop via 169.254.2.22 dev $rp12 weight $weight_rp12 \
207+
nexthop via 169.254.3.23 dev $rp13 weight $weight_rp13
208+
209+
t0_rp12=$(link_stats_tx_packets_get $rp12)
210+
t0_rp13=$(link_stats_tx_packets_get $rp13)
211+
212+
ip vrf exec vrf-h1 $MZ -q -p 64 -A 192.0.2.2 -B 198.51.100.2 \
213+
-d 1msec -t udp "sp=1024,dp=0-32768"
214+
215+
t1_rp12=$(link_stats_tx_packets_get $rp12)
216+
t1_rp13=$(link_stats_tx_packets_get $rp13)
217+
218+
let "packets_rp12 = $t1_rp12 - $t0_rp12"
219+
let "packets_rp13 = $t1_rp13 - $t0_rp13"
220+
multipath_eval $weight_rp12 $weight_rp13 $packets_rp12 $packets_rp13
221+
222+
# Restore settings.
223+
ip route replace 198.51.100.0/24 vrf vrf-r1 \
224+
nexthop via 169.254.2.22 dev $rp12 \
225+
nexthop via 169.254.3.23 dev $rp13
226+
sysctl -q -w net.ipv4.fib_multipath_hash_policy=$hash_policy
227+
}
228+
229+
multipath_test()
230+
{
231+
log_info "Running IPv4 multipath tests"
232+
multipath4_test 1 1
233+
multipath4_test 2 1
234+
multipath4_test 11 45
235+
}
236+
161237
setup_prepare()
162238
{
163239
h1=${NETIFS[p1]}
@@ -205,5 +281,6 @@ setup_wait
205281

206282
ping_test $h1 198.51.100.2
207283
ping6_test $h1 2001:db8:2::2
284+
multipath_test
208285

209286
exit $EXIT_STATUS

0 commit comments

Comments
 (0)