Skip to content

Commit 4d0dac4

Browse files
wdebruijPaolo Abeni
authored andcommitted
selftests/net: test tcp connection load balancing
Verify that TCP connections use both routes when connecting multiple times to a remote service over a two nexthop multipath route. Use socat to create the connections. Use tc prio + tc filter to count routes taken, counting SYN packets across the two egress devices. Also verify that the saddr matches that of the device. To avoid flaky tests when testing inherently randomized behavior, set a low bar and pass if even a single SYN is observed on each device. Signed-off-by: Willem de Bruijn <[email protected]> Reviewed-by: Ido Schimmel <[email protected]> Tested-by: Ido Schimmel <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Paolo Abeni <[email protected]>
1 parent 65e9024 commit 4d0dac4

File tree

2 files changed

+143
-1
lines changed

2 files changed

+143
-1
lines changed

tools/testing/selftests/net/fib_tests.sh

Lines changed: 119 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ TESTS="unregister down carrier nexthop suppress ipv6_notify ipv4_notify \
1111
ipv6_rt ipv4_rt ipv6_addr_metric ipv4_addr_metric ipv6_route_metrics \
1212
ipv4_route_metrics ipv4_route_v6_gw rp_filter ipv4_del_addr \
1313
ipv6_del_addr ipv4_mangle ipv6_mangle ipv4_bcast_neigh fib6_gc_test \
14-
ipv4_mpath_list ipv6_mpath_list"
14+
ipv4_mpath_list ipv6_mpath_list ipv4_mpath_balance ipv6_mpath_balance"
1515

1616
VERBOSE=0
1717
PAUSE_ON_FAIL=no
@@ -1085,6 +1085,35 @@ route_setup()
10851085
set +e
10861086
}
10871087

1088+
forwarding_cleanup()
1089+
{
1090+
cleanup_ns $ns3
1091+
1092+
route_cleanup
1093+
}
1094+
1095+
# extend route_setup with an ns3 reachable through ns2 over both devices
1096+
forwarding_setup()
1097+
{
1098+
forwarding_cleanup
1099+
1100+
route_setup
1101+
1102+
setup_ns ns3
1103+
1104+
ip link add veth5 netns $ns3 type veth peer name veth6 netns $ns2
1105+
ip -netns $ns3 link set veth5 up
1106+
ip -netns $ns2 link set veth6 up
1107+
1108+
ip -netns $ns3 -4 addr add dev veth5 172.16.105.1/24
1109+
ip -netns $ns2 -4 addr add dev veth6 172.16.105.2/24
1110+
ip -netns $ns3 -4 route add 172.16.100.0/22 via 172.16.105.2
1111+
1112+
ip -netns $ns3 -6 addr add dev veth5 2001:db8:105::1/64 nodad
1113+
ip -netns $ns2 -6 addr add dev veth6 2001:db8:105::2/64 nodad
1114+
ip -netns $ns3 -6 route add 2001:db8:101::/33 via 2001:db8:105::2
1115+
}
1116+
10881117
# assumption is that basic add of a single path route works
10891118
# otherwise just adding an address on an interface is broken
10901119
ipv6_rt_add()
@@ -2600,6 +2629,93 @@ ipv6_mpath_list_test()
26002629
route_cleanup
26012630
}
26022631

2632+
tc_set_flower_counter__saddr_syn() {
2633+
tc_set_flower_counter $1 $2 $3 "src_ip $4 ip_proto tcp tcp_flags 0x2"
2634+
}
2635+
2636+
ip_mpath_balance_dep_check()
2637+
{
2638+
if [ ! -x "$(command -v socat)" ]; then
2639+
echo "socat command not found. Skipping test"
2640+
return 1
2641+
fi
2642+
2643+
if [ ! -x "$(command -v jq)" ]; then
2644+
echo "jq command not found. Skipping test"
2645+
return 1
2646+
fi
2647+
}
2648+
2649+
ip_mpath_balance() {
2650+
local -r ipver=$1
2651+
local -r daddr=$2
2652+
local -r num_conn=20
2653+
2654+
for i in $(seq 1 $num_conn); do
2655+
ip netns exec $ns3 socat $ipver TCP-LISTEN:8000 STDIO >/dev/null &
2656+
sleep 0.02
2657+
echo -n a | ip netns exec $ns1 socat $ipver STDIO TCP:$daddr:8000
2658+
done
2659+
2660+
local -r syn0="$(tc_get_flower_counter $ns1 veth1)"
2661+
local -r syn1="$(tc_get_flower_counter $ns1 veth3)"
2662+
local -r syns=$((syn0+syn1))
2663+
2664+
[ "$VERBOSE" = "1" ] && echo "multipath: syns seen: ($syn0,$syn1)"
2665+
2666+
[[ $syns -ge $num_conn ]] && [[ $syn0 -gt 0 ]] && [[ $syn1 -gt 0 ]]
2667+
}
2668+
2669+
ipv4_mpath_balance_test()
2670+
{
2671+
echo
2672+
echo "IPv4 multipath load balance test"
2673+
2674+
ip_mpath_balance_dep_check || return 1
2675+
forwarding_setup
2676+
2677+
$IP route add 172.16.105.1 \
2678+
nexthop via 172.16.101.2 \
2679+
nexthop via 172.16.103.2
2680+
2681+
ip netns exec $ns1 \
2682+
sysctl -q -w net.ipv4.fib_multipath_hash_policy=1
2683+
2684+
tc_set_flower_counter__saddr_syn $ns1 4 veth1 172.16.101.1
2685+
tc_set_flower_counter__saddr_syn $ns1 4 veth3 172.16.103.1
2686+
2687+
ip_mpath_balance -4 172.16.105.1
2688+
2689+
log_test $? 0 "IPv4 multipath loadbalance"
2690+
2691+
forwarding_cleanup
2692+
}
2693+
2694+
ipv6_mpath_balance_test()
2695+
{
2696+
echo
2697+
echo "IPv6 multipath load balance test"
2698+
2699+
ip_mpath_balance_dep_check || return 1
2700+
forwarding_setup
2701+
2702+
$IP route add 2001:db8:105::1\
2703+
nexthop via 2001:db8:101::2 \
2704+
nexthop via 2001:db8:103::2
2705+
2706+
ip netns exec $ns1 \
2707+
sysctl -q -w net.ipv6.fib_multipath_hash_policy=1
2708+
2709+
tc_set_flower_counter__saddr_syn $ns1 6 veth1 2001:db8:101::1
2710+
tc_set_flower_counter__saddr_syn $ns1 6 veth3 2001:db8:103::1
2711+
2712+
ip_mpath_balance -6 "[2001:db8:105::1]"
2713+
2714+
log_test $? 0 "IPv6 multipath loadbalance"
2715+
2716+
forwarding_cleanup
2717+
}
2718+
26032719
################################################################################
26042720
# usage
26052721

@@ -2683,6 +2799,8 @@ do
26832799
fib6_gc_test|ipv6_gc) fib6_gc_test;;
26842800
ipv4_mpath_list) ipv4_mpath_list_test;;
26852801
ipv6_mpath_list) ipv6_mpath_list_test;;
2802+
ipv4_mpath_balance) ipv4_mpath_balance_test;;
2803+
ipv6_mpath_balance) ipv6_mpath_balance_test;;
26862804

26872805
help) echo "Test names: $TESTS"; exit 0;;
26882806
esac

tools/testing/selftests/net/lib.sh

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,30 @@ tc_rule_handle_stats_get()
270270
.options.actions[0].stats$selector"
271271
}
272272

273+
# attach a qdisc with two children match/no-match and a flower filter to match
274+
tc_set_flower_counter() {
275+
local -r ns=$1
276+
local -r ipver=$2
277+
local -r dev=$3
278+
local -r flower_expr=$4
279+
280+
tc -n $ns qdisc add dev $dev root handle 1: prio bands 2 \
281+
priomap 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
282+
283+
tc -n $ns qdisc add dev $dev parent 1:1 handle 11: pfifo
284+
tc -n $ns qdisc add dev $dev parent 1:2 handle 12: pfifo
285+
286+
tc -n $ns filter add dev $dev parent 1: protocol ipv$ipver \
287+
flower $flower_expr classid 1:2
288+
}
289+
290+
tc_get_flower_counter() {
291+
local -r ns=$1
292+
local -r dev=$2
293+
294+
tc -n $ns -j -s qdisc show dev $dev handle 12: | jq .[0].packets
295+
}
296+
273297
ret_set_ksft_status()
274298
{
275299
local ksft_status=$1; shift

0 commit comments

Comments
 (0)