1b08fbf24SPaolo Abeni#!/bin/bash
2b08fbf24SPaolo Abeni# SPDX-License-Identifier: GPL-2.0
3b08fbf24SPaolo Abeni
4d8d08302SMatthieu Baerts# Double quotes to prevent globbing and word splitting is recommended in new
5d8d08302SMatthieu Baerts# code but we accept it, especially because there were too many before having
6d8d08302SMatthieu Baerts# address all other issues detected by shellcheck.
7d8d08302SMatthieu Baerts#shellcheck disable=SC2086
8d8d08302SMatthieu Baerts
90fcd72dfSMatthieu Baerts# ShellCheck incorrectly believes that most of the code here is unreachable
100fcd72dfSMatthieu Baerts# because it's invoked by variable name, see how the "tests" array is used
110fcd72dfSMatthieu Baerts#shellcheck disable=SC2317
120fcd72dfSMatthieu Baerts
13715c78a8SMatthieu Baerts. "$(dirname "${0}")/mptcp_lib.sh"
14715c78a8SMatthieu Baerts
15b08fbf24SPaolo Abeniret=0
16b08fbf24SPaolo Abenisin=""
177d1e6f16SPaolo Abenisinfail=""
18b08fbf24SPaolo Abenisout=""
19b08fbf24SPaolo Abenicin=""
207d1e6f16SPaolo Abenicinfail=""
218b819a84SFlorian Westphalcinsent=""
22d328fe87SMatthieu Baertstmpfile=""
23b08fbf24SPaolo Abenicout=""
241e777bd8SMatthieu Baertscapout=""
251e777bd8SMatthieu Baertsns1=""
261e777bd8SMatthieu Baertsns2=""
27b08fbf24SPaolo Abeniksft_skip=4
28*0c4cd3f8SMatthieu Baertsiptables="iptables"
29*0c4cd3f8SMatthieu Baertsip6tables="ip6tables"
305888a61cSMatthieu Baertstimeout_poll=30
315888a61cSMatthieu Baertstimeout_test=$((timeout_poll * 2 + 1))
32b08fbf24SPaolo Abenicapture=0
33af66d3e1SGeliang Tangchecksum=0
3434aa6e3bSGeliang Tangip_mptcp=0
358117dac3SGeliang Tangcheck_invert=0
363c082695SGeliang Tangvalidate_checksum=0
3793827ad5SMatthieu Baertsinit=0
38a3735625SGeliang Tangevts_ns1=""
39a3735625SGeliang Tangevts_ns2=""
40a3735625SGeliang Tangevts_ns1_pid=0
41a3735625SGeliang Tangevts_ns2_pid=0
42b08fbf24SPaolo Abeni
433afd0280SMatthieu Baertsdeclare -A all_tests
44c7d49c03SMatthieu Baertsdeclare -a only_tests_ids
45c7d49c03SMatthieu Baertsdeclare -a only_tests_names
4639aab882SMatthieu Baertsdeclare -A failed_tests
47b08fbf24SPaolo AbeniTEST_COUNT=0
48c7d49c03SMatthieu BaertsTEST_NAME=""
499a0a9367SGeliang Tangnr_blank=40
50b08fbf24SPaolo Abeni
51e59300ceSMatthieu Baertsexport FAILING_LINKS=""
52e59300ceSMatthieu Baerts
538d014eaaSGeliang Tang# generated using "nfbpf_compile '(ip && (ip[54] & 0xf0) == 0x30) ||
548d014eaaSGeliang Tang#				  (ip6 && (ip6[74] & 0xf0) == 0x30)'"
558d014eaaSGeliang TangCBPF_MPTCP_SUBOPTION_ADD_ADDR="14,
568d014eaaSGeliang Tang			       48 0 0 0,
578d014eaaSGeliang Tang			       84 0 0 240,
588d014eaaSGeliang Tang			       21 0 3 64,
598d014eaaSGeliang Tang			       48 0 0 54,
608d014eaaSGeliang Tang			       84 0 0 240,
618d014eaaSGeliang Tang			       21 6 7 48,
628d014eaaSGeliang Tang			       48 0 0 0,
638d014eaaSGeliang Tang			       84 0 0 240,
648d014eaaSGeliang Tang			       21 0 4 96,
658d014eaaSGeliang Tang			       48 0 0 74,
668d014eaaSGeliang Tang			       84 0 0 240,
678d014eaaSGeliang Tang			       21 0 1 48,
688d014eaaSGeliang Tang			       6 0 0 65535,
698d014eaaSGeliang Tang			       6 0 0 0"
708d014eaaSGeliang Tang
7193827ad5SMatthieu Baertsinit_partial()
72b08fbf24SPaolo Abeni{
73b08fbf24SPaolo Abeni	capout=$(mktemp)
74b08fbf24SPaolo Abeni
75787eb1e4SMatthieu Baerts	local sec rndh
76787eb1e4SMatthieu Baerts	sec=$(date +%s)
77787eb1e4SMatthieu Baerts	rndh=$(printf %x $sec)-$(mktemp -u XXXXXX)
78b08fbf24SPaolo Abeni
79b08fbf24SPaolo Abeni	ns1="ns1-$rndh"
80b08fbf24SPaolo Abeni	ns2="ns2-$rndh"
81b08fbf24SPaolo Abeni
821e777bd8SMatthieu Baerts	local netns
83b08fbf24SPaolo Abeni	for netns in "$ns1" "$ns2"; do
84b08fbf24SPaolo Abeni		ip netns add $netns || exit $ksft_skip
85b08fbf24SPaolo Abeni		ip -net $netns link set lo up
86b08fbf24SPaolo Abeni		ip netns exec $netns sysctl -q net.mptcp.enabled=1
875ac1d2d6SMat Martineau		ip netns exec $netns sysctl -q net.mptcp.pm_type=0
88b08fbf24SPaolo Abeni		ip netns exec $netns sysctl -q net.ipv4.conf.all.rp_filter=0
89b08fbf24SPaolo Abeni		ip netns exec $netns sysctl -q net.ipv4.conf.default.rp_filter=0
90af66d3e1SGeliang Tang		if [ $checksum -eq 1 ]; then
91af66d3e1SGeliang Tang			ip netns exec $netns sysctl -q net.mptcp.checksum_enabled=1
92af66d3e1SGeliang Tang		fi
93b08fbf24SPaolo Abeni	done
94b08fbf24SPaolo Abeni
958117dac3SGeliang Tang	check_invert=0
963c082695SGeliang Tang	validate_checksum=$checksum
97e59300ceSMatthieu Baerts	FAILING_LINKS=""
988117dac3SGeliang Tang
99b08fbf24SPaolo Abeni	#  ns1         ns2
100b08fbf24SPaolo Abeni	# ns1eth1    ns2eth1
101b08fbf24SPaolo Abeni	# ns1eth2    ns2eth2
102b08fbf24SPaolo Abeni	# ns1eth3    ns2eth3
103b08fbf24SPaolo Abeni	# ns1eth4    ns2eth4
104b08fbf24SPaolo Abeni
1051e777bd8SMatthieu Baerts	local i
1064bfadd71SMatthieu Baerts	for i in $(seq 1 4); do
107b08fbf24SPaolo Abeni		ip link add ns1eth$i netns "$ns1" type veth peer name ns2eth$i netns "$ns2"
108b08fbf24SPaolo Abeni		ip -net "$ns1" addr add 10.0.$i.1/24 dev ns1eth$i
109b08fbf24SPaolo Abeni		ip -net "$ns1" addr add dead:beef:$i::1/64 dev ns1eth$i nodad
110b08fbf24SPaolo Abeni		ip -net "$ns1" link set ns1eth$i up
111b08fbf24SPaolo Abeni
112b08fbf24SPaolo Abeni		ip -net "$ns2" addr add 10.0.$i.2/24 dev ns2eth$i
113b08fbf24SPaolo Abeni		ip -net "$ns2" addr add dead:beef:$i::2/64 dev ns2eth$i nodad
114b08fbf24SPaolo Abeni		ip -net "$ns2" link set ns2eth$i up
115b08fbf24SPaolo Abeni
116b08fbf24SPaolo Abeni		# let $ns2 reach any $ns1 address from any interface
117b08fbf24SPaolo Abeni		ip -net "$ns2" route add default via 10.0.$i.1 dev ns2eth$i metric 10$i
1189846921dSPaolo Abeni		ip -net "$ns2" route add default via dead:beef:$i::1 dev ns2eth$i metric 10$i
119b08fbf24SPaolo Abeni	done
120b08fbf24SPaolo Abeni}
121b08fbf24SPaolo Abeni
1227d1e6f16SPaolo Abeniinit_shapers()
1237d1e6f16SPaolo Abeni{
1241e777bd8SMatthieu Baerts	local i
1254bfadd71SMatthieu Baerts	for i in $(seq 1 4); do
1267d1e6f16SPaolo Abeni		tc -n $ns1 qdisc add dev ns1eth$i root netem rate 20mbit delay 1
1277d1e6f16SPaolo Abeni		tc -n $ns2 qdisc add dev ns2eth$i root netem rate 20mbit delay 1
1287d1e6f16SPaolo Abeni	done
1297d1e6f16SPaolo Abeni}
1307d1e6f16SPaolo Abeni
131b08fbf24SPaolo Abenicleanup_partial()
132b08fbf24SPaolo Abeni{
133b08fbf24SPaolo Abeni	rm -f "$capout"
134b08fbf24SPaolo Abeni
1351e777bd8SMatthieu Baerts	local netns
136b08fbf24SPaolo Abeni	for netns in "$ns1" "$ns2"; do
137b08fbf24SPaolo Abeni		ip netns del $netns
138c2a55e8fSMatthieu Baerts		rm -f /tmp/$netns.{nstat,out}
139b08fbf24SPaolo Abeni	done
140b08fbf24SPaolo Abeni}
141b08fbf24SPaolo Abeni
14287154755SMatthieu Baertscheck_tools()
14387154755SMatthieu Baerts{
144715c78a8SMatthieu Baerts	mptcp_lib_check_mptcp
145715c78a8SMatthieu Baerts
14687154755SMatthieu Baerts	if ! ip -Version &> /dev/null; then
14787154755SMatthieu Baerts		echo "SKIP: Could not run test without ip tool"
14887154755SMatthieu Baerts		exit $ksft_skip
14987154755SMatthieu Baerts	fi
15087154755SMatthieu Baerts
151*0c4cd3f8SMatthieu Baerts	# Use the legacy version if available to support old kernel versions
152*0c4cd3f8SMatthieu Baerts	if iptables-legacy -V &> /dev/null; then
153*0c4cd3f8SMatthieu Baerts		iptables="iptables-legacy"
154*0c4cd3f8SMatthieu Baerts		ip6tables="ip6tables-legacy"
155*0c4cd3f8SMatthieu Baerts	elif ! iptables -V &> /dev/null; then
15687154755SMatthieu Baerts		echo "SKIP: Could not run all tests without iptables tool"
15787154755SMatthieu Baerts		exit $ksft_skip
15887154755SMatthieu Baerts	fi
15987154755SMatthieu Baerts
16087154755SMatthieu Baerts	if ! ip6tables -V &> /dev/null; then
16187154755SMatthieu Baerts		echo "SKIP: Could not run all tests without ip6tables tool"
16287154755SMatthieu Baerts		exit $ksft_skip
16387154755SMatthieu Baerts	fi
16487154755SMatthieu Baerts}
16587154755SMatthieu Baerts
16693827ad5SMatthieu Baertsinit() {
16793827ad5SMatthieu Baerts	init=1
16893827ad5SMatthieu Baerts
16987154755SMatthieu Baerts	check_tools
17087154755SMatthieu Baerts
17193827ad5SMatthieu Baerts	sin=$(mktemp)
17293827ad5SMatthieu Baerts	sout=$(mktemp)
17393827ad5SMatthieu Baerts	cin=$(mktemp)
17493827ad5SMatthieu Baerts	cinsent=$(mktemp)
17593827ad5SMatthieu Baerts	cout=$(mktemp)
176a3735625SGeliang Tang	evts_ns1=$(mktemp)
177a3735625SGeliang Tang	evts_ns2=$(mktemp)
17893827ad5SMatthieu Baerts
17993827ad5SMatthieu Baerts	trap cleanup EXIT
18093827ad5SMatthieu Baerts
18193827ad5SMatthieu Baerts	make_file "$cin" "client" 1
18293827ad5SMatthieu Baerts	make_file "$sin" "server" 1
18393827ad5SMatthieu Baerts}
18493827ad5SMatthieu Baerts
185b08fbf24SPaolo Abenicleanup()
186b08fbf24SPaolo Abeni{
1877d1e6f16SPaolo Abeni	rm -f "$cin" "$cout" "$sinfail"
1887d1e6f16SPaolo Abeni	rm -f "$sin" "$sout" "$cinsent" "$cinfail"
189d328fe87SMatthieu Baerts	rm -f "$tmpfile"
190a3735625SGeliang Tang	rm -rf $evts_ns1 $evts_ns2
191b08fbf24SPaolo Abeni	cleanup_partial
192b08fbf24SPaolo Abeni}
193b08fbf24SPaolo Abeni
194ae7bd9ccSMatthieu Baertsskip_test()
195ae7bd9ccSMatthieu Baerts{
196c7d49c03SMatthieu Baerts	if [ "${#only_tests_ids[@]}" -eq 0 ] && [ "${#only_tests_names[@]}" -eq 0 ]; then
197ae7bd9ccSMatthieu Baerts		return 1
198ae7bd9ccSMatthieu Baerts	fi
199ae7bd9ccSMatthieu Baerts
200ae7bd9ccSMatthieu Baerts	local i
201c7d49c03SMatthieu Baerts	for i in "${only_tests_ids[@]}"; do
202ae7bd9ccSMatthieu Baerts		if [ "${TEST_COUNT}" -eq "${i}" ]; then
203ae7bd9ccSMatthieu Baerts			return 1
204ae7bd9ccSMatthieu Baerts		fi
205ae7bd9ccSMatthieu Baerts	done
206c7d49c03SMatthieu Baerts	for i in "${only_tests_names[@]}"; do
207c7d49c03SMatthieu Baerts		if [ "${TEST_NAME}" = "${i}" ]; then
208c7d49c03SMatthieu Baerts			return 1
209c7d49c03SMatthieu Baerts		fi
210c7d49c03SMatthieu Baerts	done
211ae7bd9ccSMatthieu Baerts
212ae7bd9ccSMatthieu Baerts	return 0
213ae7bd9ccSMatthieu Baerts}
214ae7bd9ccSMatthieu Baerts
215c7d49c03SMatthieu Baerts# $1: test name
216b08fbf24SPaolo Abenireset()
217b08fbf24SPaolo Abeni{
218c7d49c03SMatthieu Baerts	TEST_NAME="${1}"
219c7d49c03SMatthieu Baerts
220ae7bd9ccSMatthieu Baerts	TEST_COUNT=$((TEST_COUNT+1))
221ae7bd9ccSMatthieu Baerts
222ae7bd9ccSMatthieu Baerts	if skip_test; then
223ae7bd9ccSMatthieu Baerts		return 1
224ae7bd9ccSMatthieu Baerts	fi
225ae7bd9ccSMatthieu Baerts
22693827ad5SMatthieu Baerts	if [ "${init}" != "1" ]; then
227b08fbf24SPaolo Abeni		init
22893827ad5SMatthieu Baerts	else
22993827ad5SMatthieu Baerts		cleanup_partial
23093827ad5SMatthieu Baerts	fi
23193827ad5SMatthieu Baerts
23293827ad5SMatthieu Baerts	init_partial
233ae7bd9ccSMatthieu Baerts
234ae7bd9ccSMatthieu Baerts	return 0
235b08fbf24SPaolo Abeni}
236b08fbf24SPaolo Abeni
237c7d49c03SMatthieu Baerts# $1: test name
23800587187SFlorian Westphalreset_with_cookies()
23900587187SFlorian Westphal{
240c7d49c03SMatthieu Baerts	reset "${1}" || return 1
24100587187SFlorian Westphal
2421e777bd8SMatthieu Baerts	local netns
24300587187SFlorian Westphal	for netns in "$ns1" "$ns2"; do
24400587187SFlorian Westphal		ip netns exec $netns sysctl -q net.ipv4.tcp_syncookies=2
24500587187SFlorian Westphal	done
24600587187SFlorian Westphal}
24700587187SFlorian Westphal
248c7d49c03SMatthieu Baerts# $1: test name
2498d014eaaSGeliang Tangreset_with_add_addr_timeout()
2508d014eaaSGeliang Tang{
251c7d49c03SMatthieu Baerts	local ip="${2:-4}"
2528d014eaaSGeliang Tang	local tables
2538d014eaaSGeliang Tang
254c7d49c03SMatthieu Baerts	reset "${1}" || return 1
255c7d49c03SMatthieu Baerts
256*0c4cd3f8SMatthieu Baerts	tables="${iptables}"
2578d014eaaSGeliang Tang	if [ $ip -eq 6 ]; then
258*0c4cd3f8SMatthieu Baerts		tables="${ip6tables}"
2598d014eaaSGeliang Tang	fi
2608d014eaaSGeliang Tang
2618d014eaaSGeliang Tang	ip netns exec $ns1 sysctl -q net.mptcp.add_addr_timeout=1
2628d014eaaSGeliang Tang	ip netns exec $ns2 $tables -A OUTPUT -p tcp \
2638d014eaaSGeliang Tang		-m tcp --tcp-option 30 \
2648d014eaaSGeliang Tang		-m bpf --bytecode \
2658d014eaaSGeliang Tang		"$CBPF_MPTCP_SUBOPTION_ADD_ADDR" \
2668d014eaaSGeliang Tang		-j DROP
2678d014eaaSGeliang Tang}
2688d014eaaSGeliang Tang
269c7d49c03SMatthieu Baerts# $1: test name
270af66d3e1SGeliang Tangreset_with_checksum()
271af66d3e1SGeliang Tang{
272af66d3e1SGeliang Tang	local ns1_enable=$1
273af66d3e1SGeliang Tang	local ns2_enable=$2
274af66d3e1SGeliang Tang
275c7d49c03SMatthieu Baerts	reset "checksum test ${1} ${2}" || return 1
276af66d3e1SGeliang Tang
277af66d3e1SGeliang Tang	ip netns exec $ns1 sysctl -q net.mptcp.checksum_enabled=$ns1_enable
278af66d3e1SGeliang Tang	ip netns exec $ns2 sysctl -q net.mptcp.checksum_enabled=$ns2_enable
2793c082695SGeliang Tang
2803c082695SGeliang Tang	validate_checksum=1
281af66d3e1SGeliang Tang}
282af66d3e1SGeliang Tang
2830cddb4a6SGeliang Tangreset_with_allow_join_id0()
2840cddb4a6SGeliang Tang{
285c7d49c03SMatthieu Baerts	local ns1_enable=$2
286c7d49c03SMatthieu Baerts	local ns2_enable=$3
2870cddb4a6SGeliang Tang
288c7d49c03SMatthieu Baerts	reset "${1}" || return 1
2890cddb4a6SGeliang Tang
2900cddb4a6SGeliang Tang	ip netns exec $ns1 sysctl -q net.mptcp.allow_join_initial_addr_port=$ns1_enable
2910cddb4a6SGeliang Tang	ip netns exec $ns2 sysctl -q net.mptcp.allow_join_initial_addr_port=$ns2_enable
2920cddb4a6SGeliang Tang}
2930cddb4a6SGeliang Tang
294b6e074e1SGeliang Tang# Modify TCP payload without corrupting the TCP packet
295b6e074e1SGeliang Tang#
296b6e074e1SGeliang Tang# This rule inverts a 8-bit word at byte offset 148 for the 2nd TCP ACK packets
297b6e074e1SGeliang Tang# carrying enough data.
298b6e074e1SGeliang Tang# Once it is done, the TCP Checksum field is updated so the packet is still
299b6e074e1SGeliang Tang# considered as valid at the TCP level.
300b6e074e1SGeliang Tang# Because the MPTCP checksum, covering the TCP options and data, has not been
301b6e074e1SGeliang Tang# updated, the modification will be detected and an MP_FAIL will be emitted:
302b6e074e1SGeliang Tang# what we want to validate here without corrupting "random" MPTCP options.
303b6e074e1SGeliang Tang#
304b6e074e1SGeliang Tang# To avoid having tc producing this pr_info() message for each TCP ACK packets
305b6e074e1SGeliang Tang# not carrying enough data:
306b6e074e1SGeliang Tang#
307b6e074e1SGeliang Tang#     tc action pedit offset 162 out of bounds
308b6e074e1SGeliang Tang#
309b6e074e1SGeliang Tang# Netfilter is used to mark packets with enough data.
310b6e074e1SGeliang Tangreset_with_fail()
311b6e074e1SGeliang Tang{
312b6e074e1SGeliang Tang	reset "${1}" || return 1
313b6e074e1SGeliang Tang
314b6e074e1SGeliang Tang	ip netns exec $ns1 sysctl -q net.mptcp.checksum_enabled=1
315b6e074e1SGeliang Tang	ip netns exec $ns2 sysctl -q net.mptcp.checksum_enabled=1
316b6e074e1SGeliang Tang
317b6e074e1SGeliang Tang	check_invert=1
318b6e074e1SGeliang Tang	validate_checksum=1
319b6e074e1SGeliang Tang	local i="$2"
320b6e074e1SGeliang Tang	local ip="${3:-4}"
321b6e074e1SGeliang Tang	local tables
322b6e074e1SGeliang Tang
323*0c4cd3f8SMatthieu Baerts	tables="${iptables}"
324b6e074e1SGeliang Tang	if [ $ip -eq 6 ]; then
325*0c4cd3f8SMatthieu Baerts		tables="${ip6tables}"
326b6e074e1SGeliang Tang	fi
327b6e074e1SGeliang Tang
328b6e074e1SGeliang Tang	ip netns exec $ns2 $tables \
329b6e074e1SGeliang Tang		-t mangle \
330b6e074e1SGeliang Tang		-A OUTPUT \
331b6e074e1SGeliang Tang		-o ns2eth$i \
332b6e074e1SGeliang Tang		-p tcp \
333b6e074e1SGeliang Tang		-m length --length 150:9999 \
334b6e074e1SGeliang Tang		-m statistic --mode nth --packet 1 --every 99999 \
335b6e074e1SGeliang Tang		-j MARK --set-mark 42 || exit 1
336b6e074e1SGeliang Tang
337b6e074e1SGeliang Tang	tc -n $ns2 qdisc add dev ns2eth$i clsact || exit 1
338b6e074e1SGeliang Tang	tc -n $ns2 filter add dev ns2eth$i egress \
339b6e074e1SGeliang Tang		protocol ip prio 1000 \
340b6e074e1SGeliang Tang		handle 42 fw \
341b6e074e1SGeliang Tang		action pedit munge offset 148 u8 invert \
342b6e074e1SGeliang Tang		pipe csum tcp \
343b6e074e1SGeliang Tang		index 100 || exit 1
344b6e074e1SGeliang Tang}
345b6e074e1SGeliang Tang
346a3735625SGeliang Tangreset_with_events()
347a3735625SGeliang Tang{
348a3735625SGeliang Tang	reset "${1}" || return 1
349a3735625SGeliang Tang
350a3735625SGeliang Tang	:> "$evts_ns1"
351a3735625SGeliang Tang	:> "$evts_ns2"
352a3735625SGeliang Tang	ip netns exec $ns1 ./pm_nl_ctl events >> "$evts_ns1" 2>&1 &
353a3735625SGeliang Tang	evts_ns1_pid=$!
354a3735625SGeliang Tang	ip netns exec $ns2 ./pm_nl_ctl events >> "$evts_ns2" 2>&1 &
355a3735625SGeliang Tang	evts_ns2_pid=$!
356a3735625SGeliang Tang}
357a3735625SGeliang Tang
35839aab882SMatthieu Baertsfail_test()
35939aab882SMatthieu Baerts{
36039aab882SMatthieu Baerts	ret=1
36139aab882SMatthieu Baerts	failed_tests[${TEST_COUNT}]="${TEST_NAME}"
36239aab882SMatthieu Baerts}
36339aab882SMatthieu Baerts
36439aab882SMatthieu Baertsget_failed_tests_ids()
36539aab882SMatthieu Baerts{
36639aab882SMatthieu Baerts	# sorted
36739aab882SMatthieu Baerts	local i
36839aab882SMatthieu Baerts	for i in "${!failed_tests[@]}"; do
36939aab882SMatthieu Baerts		echo "${i}"
37039aab882SMatthieu Baerts	done | sort -n
37139aab882SMatthieu Baerts}
37239aab882SMatthieu Baerts
3738b819a84SFlorian Westphalprint_file_err()
3748b819a84SFlorian Westphal{
3758b819a84SFlorian Westphal	ls -l "$1" 1>&2
3768b819a84SFlorian Westphal	echo "Trailing bytes are: "
3778b819a84SFlorian Westphal	tail -c 27 "$1"
3788b819a84SFlorian Westphal}
3798b819a84SFlorian Westphal
380b08fbf24SPaolo Abenicheck_transfer()
381b08fbf24SPaolo Abeni{
3821e777bd8SMatthieu Baerts	local in=$1
3831e777bd8SMatthieu Baerts	local out=$2
3841e777bd8SMatthieu Baerts	local what=$3
3856bf41020SPaolo Abeni	local bytes=$4
386d8d08302SMatthieu Baerts	local i a b
387b08fbf24SPaolo Abeni
3881e777bd8SMatthieu Baerts	local line
3896bf41020SPaolo Abeni	if [ -n "$bytes" ]; then
3900fcd72dfSMatthieu Baerts		local out_size
3916bf41020SPaolo Abeni		# when truncating we must check the size explicitly
3920fcd72dfSMatthieu Baerts		out_size=$(wc -c $out | awk '{print $1}')
3936bf41020SPaolo Abeni		if [ $out_size -ne $bytes ]; then
3946bf41020SPaolo Abeni			echo "[ FAIL ] $what output file has wrong size ($out_size, $bytes)"
3956bf41020SPaolo Abeni			fail_test
3966bf41020SPaolo Abeni			return 1
3976bf41020SPaolo Abeni		fi
398d328fe87SMatthieu Baerts
399d328fe87SMatthieu Baerts		# note: BusyBox's "cmp" command doesn't support --bytes
400d328fe87SMatthieu Baerts		tmpfile=$(mktemp)
401d328fe87SMatthieu Baerts		head --bytes="$bytes" "$in" > "$tmpfile"
402d328fe87SMatthieu Baerts		mv "$tmpfile" "$in"
403d328fe87SMatthieu Baerts		head --bytes="$bytes" "$out" > "$tmpfile"
404d328fe87SMatthieu Baerts		mv "$tmpfile" "$out"
405d328fe87SMatthieu Baerts		tmpfile=""
4066bf41020SPaolo Abeni	fi
407d328fe87SMatthieu Baerts	cmp -l "$in" "$out" | while read -r i a b; do
408d8d08302SMatthieu Baerts		local sum=$((0${a} + 0${b}))
4098117dac3SGeliang Tang		if [ $check_invert -eq 0 ] || [ $sum -ne $((0xff)) ]; then
410b08fbf24SPaolo Abeni			echo "[ FAIL ] $what does not match (in, out):"
411b08fbf24SPaolo Abeni			print_file_err "$in"
412b08fbf24SPaolo Abeni			print_file_err "$out"
41339aab882SMatthieu Baerts			fail_test
414b08fbf24SPaolo Abeni
415b08fbf24SPaolo Abeni			return 1
4168117dac3SGeliang Tang		else
417d8d08302SMatthieu Baerts			echo "$what has inverted byte at ${i}"
418b08fbf24SPaolo Abeni		fi
4198117dac3SGeliang Tang	done
420b08fbf24SPaolo Abeni
421b08fbf24SPaolo Abeni	return 0
422b08fbf24SPaolo Abeni}
423b08fbf24SPaolo Abeni
424b08fbf24SPaolo Abenido_ping()
425b08fbf24SPaolo Abeni{
4261e777bd8SMatthieu Baerts	local listener_ns="$1"
4271e777bd8SMatthieu Baerts	local connector_ns="$2"
4281e777bd8SMatthieu Baerts	local connect_addr="$3"
429b08fbf24SPaolo Abeni
430d8d08302SMatthieu Baerts	if ! ip netns exec ${connector_ns} ping -q -c 1 $connect_addr >/dev/null; then
431b08fbf24SPaolo Abeni		echo "$listener_ns -> $connect_addr connectivity [ FAIL ]" 1>&2
43239aab882SMatthieu Baerts		fail_test
433b08fbf24SPaolo Abeni	fi
434b08fbf24SPaolo Abeni}
435b08fbf24SPaolo Abeni
4368b819a84SFlorian Westphallink_failure()
4378b819a84SFlorian Westphal{
4381e777bd8SMatthieu Baerts	local ns="$1"
4398b819a84SFlorian Westphal
4407d1e6f16SPaolo Abeni	if [ -z "$FAILING_LINKS" ]; then
4418b819a84SFlorian Westphal		l=$((RANDOM%4))
4427d1e6f16SPaolo Abeni		FAILING_LINKS=$((l+1))
4437d1e6f16SPaolo Abeni	fi
4448b819a84SFlorian Westphal
4451e777bd8SMatthieu Baerts	local l
4467d1e6f16SPaolo Abeni	for l in $FAILING_LINKS; do
4471e777bd8SMatthieu Baerts		local veth="ns1eth$l"
4488b819a84SFlorian Westphal		ip -net "$ns" link set "$veth" down
4497d1e6f16SPaolo Abeni	done
4508b819a84SFlorian Westphal}
4518b819a84SFlorian Westphal
452523514edSGeliang Tang# $1: IP address
453523514edSGeliang Tangis_v6()
454523514edSGeliang Tang{
455523514edSGeliang Tang	[ -z "${1##*:*}" ]
456523514edSGeliang Tang}
457523514edSGeliang Tang
458327b9a94SPaolo Abeni# $1: ns, $2: port
459327b9a94SPaolo Abeniwait_local_port_listen()
460327b9a94SPaolo Abeni{
461327b9a94SPaolo Abeni	local listener_ns="${1}"
462327b9a94SPaolo Abeni	local port="${2}"
463327b9a94SPaolo Abeni
4641e777bd8SMatthieu Baerts	local port_hex
465327b9a94SPaolo Abeni	port_hex="$(printf "%04X" "${port}")"
4661e777bd8SMatthieu Baerts
4671e777bd8SMatthieu Baerts	local i
468327b9a94SPaolo Abeni	for i in $(seq 10); do
469327b9a94SPaolo Abeni		ip netns exec "${listener_ns}" cat /proc/net/tcp* | \
470327b9a94SPaolo Abeni			awk "BEGIN {rc=1} {if (\$2 ~ /:${port_hex}\$/ && \$4 ~ /0A/) {rc=0; exit}} END {exit rc}" &&
471327b9a94SPaolo Abeni			break
472327b9a94SPaolo Abeni		sleep 0.1
473327b9a94SPaolo Abeni	done
474327b9a94SPaolo Abeni}
475327b9a94SPaolo Abeni
476327b9a94SPaolo Abenirm_addr_count()
477327b9a94SPaolo Abeni{
4781e777bd8SMatthieu Baerts	local ns=${1}
479327b9a94SPaolo Abeni
480327b9a94SPaolo Abeni	ip netns exec ${ns} nstat -as | grep MPTcpExtRmAddr | awk '{print $2}'
481327b9a94SPaolo Abeni}
482327b9a94SPaolo Abeni
483327b9a94SPaolo Abeni# $1: ns, $2: old rm_addr counter in $ns
484327b9a94SPaolo Abeniwait_rm_addr()
485327b9a94SPaolo Abeni{
486327b9a94SPaolo Abeni	local ns="${1}"
487327b9a94SPaolo Abeni	local old_cnt="${2}"
488327b9a94SPaolo Abeni	local cnt
489327b9a94SPaolo Abeni
4901e777bd8SMatthieu Baerts	local i
491327b9a94SPaolo Abeni	for i in $(seq 10); do
492327b9a94SPaolo Abeni		cnt=$(rm_addr_count ${ns})
493327b9a94SPaolo Abeni		[ "$cnt" = "${old_cnt}" ] || break
494327b9a94SPaolo Abeni		sleep 0.1
495327b9a94SPaolo Abeni	done
496327b9a94SPaolo Abeni}
497327b9a94SPaolo Abeni
49869c6ce7bSPaolo Abeniwait_mpj()
49969c6ce7bSPaolo Abeni{
50069c6ce7bSPaolo Abeni	local ns="${1}"
50169c6ce7bSPaolo Abeni	local cnt old_cnt
50269c6ce7bSPaolo Abeni
50369c6ce7bSPaolo Abeni	old_cnt=$(ip netns exec ${ns} nstat -as | grep MPJoinAckRx | awk '{print $2}')
50469c6ce7bSPaolo Abeni
50569c6ce7bSPaolo Abeni	local i
50669c6ce7bSPaolo Abeni	for i in $(seq 10); do
50769c6ce7bSPaolo Abeni		cnt=$(ip netns exec ${ns} nstat -as | grep MPJoinAckRx | awk '{print $2}')
50869c6ce7bSPaolo Abeni		[ "$cnt" = "${old_cnt}" ] || break
50969c6ce7bSPaolo Abeni		sleep 0.1
51069c6ce7bSPaolo Abeni	done
51169c6ce7bSPaolo Abeni}
51269c6ce7bSPaolo Abeni
51397040cf9SGeliang Tangkill_wait()
51497040cf9SGeliang Tang{
51597040cf9SGeliang Tang	kill $1 > /dev/null 2>&1
51697040cf9SGeliang Tang	wait $1 2>/dev/null
51797040cf9SGeliang Tang}
51897040cf9SGeliang Tang
519a3735625SGeliang Tangkill_events_pids()
520a3735625SGeliang Tang{
521a3735625SGeliang Tang	kill_wait $evts_ns1_pid
522a3735625SGeliang Tang	kill_wait $evts_ns2_pid
523a3735625SGeliang Tang}
524a3735625SGeliang Tang
525070d6dafSMatthieu Baertskill_tests_wait()
526070d6dafSMatthieu Baerts{
5270fcd72dfSMatthieu Baerts	#shellcheck disable=SC2046
528070d6dafSMatthieu Baerts	kill -SIGUSR1 $(ip netns pids $ns2) $(ip netns pids $ns1)
529070d6dafSMatthieu Baerts	wait
530070d6dafSMatthieu Baerts}
531070d6dafSMatthieu Baerts
53234aa6e3bSGeliang Tangpm_nl_set_limits()
53334aa6e3bSGeliang Tang{
53434aa6e3bSGeliang Tang	local ns=$1
53534aa6e3bSGeliang Tang	local addrs=$2
53634aa6e3bSGeliang Tang	local subflows=$3
53734aa6e3bSGeliang Tang
53834aa6e3bSGeliang Tang	if [ $ip_mptcp -eq 1 ]; then
53934aa6e3bSGeliang Tang		ip -n $ns mptcp limits set add_addr_accepted $addrs subflows $subflows
54034aa6e3bSGeliang Tang	else
54134aa6e3bSGeliang Tang		ip netns exec $ns ./pm_nl_ctl limits $addrs $subflows
54234aa6e3bSGeliang Tang	fi
54334aa6e3bSGeliang Tang}
54434aa6e3bSGeliang Tang
54534aa6e3bSGeliang Tangpm_nl_add_endpoint()
54634aa6e3bSGeliang Tang{
54734aa6e3bSGeliang Tang	local ns=$1
54834aa6e3bSGeliang Tang	local addr=$2
5491e777bd8SMatthieu Baerts	local flags _flags
5501e777bd8SMatthieu Baerts	local port _port
5511e777bd8SMatthieu Baerts	local dev _dev
5521e777bd8SMatthieu Baerts	local id _id
55334aa6e3bSGeliang Tang	local nr=2
55434aa6e3bSGeliang Tang
5551e777bd8SMatthieu Baerts	local p
556d8d08302SMatthieu Baerts	for p in "${@}"
55734aa6e3bSGeliang Tang	do
55834aa6e3bSGeliang Tang		if [ $p = "flags" ]; then
55934aa6e3bSGeliang Tang			eval _flags=\$"$nr"
560d8d08302SMatthieu Baerts			[ -n "$_flags" ]; flags="flags $_flags"
56134aa6e3bSGeliang Tang		fi
56234aa6e3bSGeliang Tang		if [ $p = "dev" ]; then
56334aa6e3bSGeliang Tang			eval _dev=\$"$nr"
564d8d08302SMatthieu Baerts			[ -n "$_dev" ]; dev="dev $_dev"
56534aa6e3bSGeliang Tang		fi
56634aa6e3bSGeliang Tang		if [ $p = "id" ]; then
56734aa6e3bSGeliang Tang			eval _id=\$"$nr"
568d8d08302SMatthieu Baerts			[ -n "$_id" ]; id="id $_id"
56934aa6e3bSGeliang Tang		fi
57034aa6e3bSGeliang Tang		if [ $p = "port" ]; then
57134aa6e3bSGeliang Tang			eval _port=\$"$nr"
572d8d08302SMatthieu Baerts			[ -n "$_port" ]; port="port $_port"
57334aa6e3bSGeliang Tang		fi
57434aa6e3bSGeliang Tang
575d8d08302SMatthieu Baerts		nr=$((nr + 1))
57634aa6e3bSGeliang Tang	done
57734aa6e3bSGeliang Tang
57834aa6e3bSGeliang Tang	if [ $ip_mptcp -eq 1 ]; then
57934aa6e3bSGeliang Tang		ip -n $ns mptcp endpoint add $addr ${_flags//","/" "} $dev $id $port
58034aa6e3bSGeliang Tang	else
58134aa6e3bSGeliang Tang		ip netns exec $ns ./pm_nl_ctl add $addr $flags $dev $id $port
58234aa6e3bSGeliang Tang	fi
58334aa6e3bSGeliang Tang}
58434aa6e3bSGeliang Tang
58534aa6e3bSGeliang Tangpm_nl_del_endpoint()
58634aa6e3bSGeliang Tang{
58734aa6e3bSGeliang Tang	local ns=$1
58834aa6e3bSGeliang Tang	local id=$2
58934aa6e3bSGeliang Tang	local addr=$3
59034aa6e3bSGeliang Tang
59134aa6e3bSGeliang Tang	if [ $ip_mptcp -eq 1 ]; then
59234aa6e3bSGeliang Tang		ip -n $ns mptcp endpoint delete id $id $addr
59334aa6e3bSGeliang Tang	else
59434aa6e3bSGeliang Tang		ip netns exec $ns ./pm_nl_ctl del $id $addr
59534aa6e3bSGeliang Tang	fi
59634aa6e3bSGeliang Tang}
59734aa6e3bSGeliang Tang
59834aa6e3bSGeliang Tangpm_nl_flush_endpoint()
59934aa6e3bSGeliang Tang{
60034aa6e3bSGeliang Tang	local ns=$1
60134aa6e3bSGeliang Tang
60234aa6e3bSGeliang Tang	if [ $ip_mptcp -eq 1 ]; then
60334aa6e3bSGeliang Tang		ip -n $ns mptcp endpoint flush
60434aa6e3bSGeliang Tang	else
60534aa6e3bSGeliang Tang		ip netns exec $ns ./pm_nl_ctl flush
60634aa6e3bSGeliang Tang	fi
60734aa6e3bSGeliang Tang}
60834aa6e3bSGeliang Tang
609dda61b3dSGeliang Tangpm_nl_show_endpoints()
610dda61b3dSGeliang Tang{
611dda61b3dSGeliang Tang	local ns=$1
612dda61b3dSGeliang Tang
613dda61b3dSGeliang Tang	if [ $ip_mptcp -eq 1 ]; then
614dda61b3dSGeliang Tang		ip -n $ns mptcp endpoint show
615dda61b3dSGeliang Tang	else
616dda61b3dSGeliang Tang		ip netns exec $ns ./pm_nl_ctl dump
617dda61b3dSGeliang Tang	fi
618dda61b3dSGeliang Tang}
619dda61b3dSGeliang Tang
620f0140386SGeliang Tangpm_nl_change_endpoint()
621f0140386SGeliang Tang{
622f0140386SGeliang Tang	local ns=$1
623bccefb76SGeliang Tang	local id=$2
624bccefb76SGeliang Tang	local flags=$3
625f0140386SGeliang Tang
626f0140386SGeliang Tang	if [ $ip_mptcp -eq 1 ]; then
627f0140386SGeliang Tang		ip -n $ns mptcp endpoint change id $id ${flags//","/" "}
628f0140386SGeliang Tang	else
629bccefb76SGeliang Tang		ip netns exec $ns ./pm_nl_ctl set id $id flags $flags
630f0140386SGeliang Tang	fi
631f0140386SGeliang Tang}
632f0140386SGeliang Tang
63369c6ce7bSPaolo Abenipm_nl_check_endpoint()
63469c6ce7bSPaolo Abeni{
63569c6ce7bSPaolo Abeni	local line expected_line
636c7d49c03SMatthieu Baerts	local need_title=$1
63769c6ce7bSPaolo Abeni	local msg="$2"
63869c6ce7bSPaolo Abeni	local ns=$3
63969c6ce7bSPaolo Abeni	local addr=$4
64069c6ce7bSPaolo Abeni	local _flags=""
64169c6ce7bSPaolo Abeni	local flags
64269c6ce7bSPaolo Abeni	local _port
64369c6ce7bSPaolo Abeni	local port
64469c6ce7bSPaolo Abeni	local dev
64569c6ce7bSPaolo Abeni	local _id
64669c6ce7bSPaolo Abeni	local id
64769c6ce7bSPaolo Abeni
648c7d49c03SMatthieu Baerts	if [ "${need_title}" = 1 ]; then
649c7d49c03SMatthieu Baerts		printf "%03u %-36s %s" "${TEST_COUNT}" "${TEST_NAME}" "${msg}"
65069c6ce7bSPaolo Abeni	else
65169c6ce7bSPaolo Abeni		printf "%-${nr_blank}s %s" " " "${msg}"
65269c6ce7bSPaolo Abeni	fi
65369c6ce7bSPaolo Abeni
65469c6ce7bSPaolo Abeni	shift 4
65569c6ce7bSPaolo Abeni	while [ -n "$1" ]; do
65669c6ce7bSPaolo Abeni		if [ $1 = "flags" ]; then
65769c6ce7bSPaolo Abeni			_flags=$2
658d8d08302SMatthieu Baerts			[ -n "$_flags" ]; flags="flags $_flags"
65969c6ce7bSPaolo Abeni			shift
66069c6ce7bSPaolo Abeni		elif [ $1 = "dev" ]; then
661d8d08302SMatthieu Baerts			[ -n "$2" ]; dev="dev $1"
66269c6ce7bSPaolo Abeni			shift
66369c6ce7bSPaolo Abeni		elif [ $1 = "id" ]; then
66469c6ce7bSPaolo Abeni			_id=$2
665d8d08302SMatthieu Baerts			[ -n "$_id" ]; id="id $_id"
66669c6ce7bSPaolo Abeni			shift
66769c6ce7bSPaolo Abeni		elif [ $1 = "port" ]; then
66869c6ce7bSPaolo Abeni			_port=$2
669d8d08302SMatthieu Baerts			[ -n "$_port" ]; port=" port $_port"
67069c6ce7bSPaolo Abeni			shift
67169c6ce7bSPaolo Abeni		fi
67269c6ce7bSPaolo Abeni
67369c6ce7bSPaolo Abeni		shift
67469c6ce7bSPaolo Abeni	done
67569c6ce7bSPaolo Abeni
67669c6ce7bSPaolo Abeni	if [ -z "$id" ]; then
67769c6ce7bSPaolo Abeni		echo "[skip] bad test - missing endpoint id"
67869c6ce7bSPaolo Abeni		return
67969c6ce7bSPaolo Abeni	fi
68069c6ce7bSPaolo Abeni
68169c6ce7bSPaolo Abeni	if [ $ip_mptcp -eq 1 ]; then
68269c6ce7bSPaolo Abeni		line=$(ip -n $ns mptcp endpoint show $id)
68369c6ce7bSPaolo Abeni		# the dump order is: address id flags port dev
68469c6ce7bSPaolo Abeni		expected_line="$addr"
68569c6ce7bSPaolo Abeni		[ -n "$addr" ] && expected_line="$expected_line $addr"
68669c6ce7bSPaolo Abeni		expected_line="$expected_line $id"
68769c6ce7bSPaolo Abeni		[ -n "$_flags" ] && expected_line="$expected_line ${_flags//","/" "}"
68869c6ce7bSPaolo Abeni		[ -n "$dev" ] && expected_line="$expected_line $dev"
68969c6ce7bSPaolo Abeni		[ -n "$port" ] && expected_line="$expected_line $port"
69069c6ce7bSPaolo Abeni	else
69169c6ce7bSPaolo Abeni		line=$(ip netns exec $ns ./pm_nl_ctl get $_id)
69269c6ce7bSPaolo Abeni		# the dump order is: id flags dev address port
69369c6ce7bSPaolo Abeni		expected_line="$id"
69469c6ce7bSPaolo Abeni		[ -n "$flags" ] && expected_line="$expected_line $flags"
69569c6ce7bSPaolo Abeni		[ -n "$dev" ] && expected_line="$expected_line $dev"
69669c6ce7bSPaolo Abeni		[ -n "$addr" ] && expected_line="$expected_line $addr"
69769c6ce7bSPaolo Abeni		[ -n "$_port" ] && expected_line="$expected_line $_port"
69869c6ce7bSPaolo Abeni	fi
69969c6ce7bSPaolo Abeni	if [ "$line" = "$expected_line" ]; then
70069c6ce7bSPaolo Abeni		echo "[ ok ]"
70169c6ce7bSPaolo Abeni	else
70269c6ce7bSPaolo Abeni		echo "[fail] expected '$expected_line' found '$line'"
70339aab882SMatthieu Baerts		fail_test
70469c6ce7bSPaolo Abeni	fi
70569c6ce7bSPaolo Abeni}
70669c6ce7bSPaolo Abeni
7073469d72fSMatthieu Baertsfilter_tcp_from()
7083469d72fSMatthieu Baerts{
7093469d72fSMatthieu Baerts	local ns="${1}"
7103469d72fSMatthieu Baerts	local src="${2}"
7113469d72fSMatthieu Baerts	local target="${3}"
7123469d72fSMatthieu Baerts
713*0c4cd3f8SMatthieu Baerts	ip netns exec "${ns}" ${iptables} -A INPUT -s "${src}" -p tcp -j "${target}"
7143469d72fSMatthieu Baerts}
7153469d72fSMatthieu Baerts
716b08fbf24SPaolo Abenido_transfer()
717b08fbf24SPaolo Abeni{
7181e777bd8SMatthieu Baerts	local listener_ns="$1"
7191e777bd8SMatthieu Baerts	local connector_ns="$2"
7201e777bd8SMatthieu Baerts	local cl_proto="$3"
7211e777bd8SMatthieu Baerts	local srv_proto="$4"
7221e777bd8SMatthieu Baerts	local connect_addr="$5"
7231e777bd8SMatthieu Baerts	local test_link_fail="$6"
7241e777bd8SMatthieu Baerts	local addr_nr_ns1="$7"
7251e777bd8SMatthieu Baerts	local addr_nr_ns2="$8"
7261e777bd8SMatthieu Baerts	local speed="$9"
7271e777bd8SMatthieu Baerts	local sflags="${10}"
728b08fbf24SPaolo Abeni
7291e777bd8SMatthieu Baerts	local port=$((10000 + TEST_COUNT - 1))
7301e777bd8SMatthieu Baerts	local cappid
73197040cf9SGeliang Tang	local userspace_pm=0
732b08fbf24SPaolo Abeni
733b08fbf24SPaolo Abeni	:> "$cout"
734b08fbf24SPaolo Abeni	:> "$sout"
735b08fbf24SPaolo Abeni	:> "$capout"
736b08fbf24SPaolo Abeni
737b08fbf24SPaolo Abeni	if [ $capture -eq 1 ]; then
7381e777bd8SMatthieu Baerts		local capuser
739b08fbf24SPaolo Abeni		if [ -z $SUDO_USER ] ; then
740b08fbf24SPaolo Abeni			capuser=""
741b08fbf24SPaolo Abeni		else
742b08fbf24SPaolo Abeni			capuser="-Z $SUDO_USER"
743b08fbf24SPaolo Abeni		fi
744b08fbf24SPaolo Abeni
74500587187SFlorian Westphal		capfile=$(printf "mp_join-%02u-%s.pcap" "$TEST_COUNT" "${listener_ns}")
746b08fbf24SPaolo Abeni
747b08fbf24SPaolo Abeni		echo "Capturing traffic for test $TEST_COUNT into $capfile"
748b08fbf24SPaolo Abeni		ip netns exec ${listener_ns} tcpdump -i any -s 65535 -B 32768 $capuser -w $capfile > "$capout" 2>&1 &
749b08fbf24SPaolo Abeni		cappid=$!
750b08fbf24SPaolo Abeni
751b08fbf24SPaolo Abeni		sleep 1
752b08fbf24SPaolo Abeni	fi
753b08fbf24SPaolo Abeni
754c2a55e8fSMatthieu Baerts	NSTAT_HISTORY=/tmp/${listener_ns}.nstat ip netns exec ${listener_ns} \
755c2a55e8fSMatthieu Baerts		nstat -n
756c2a55e8fSMatthieu Baerts	NSTAT_HISTORY=/tmp/${connector_ns}.nstat ip netns exec ${connector_ns} \
757c2a55e8fSMatthieu Baerts		nstat -n
758c2a55e8fSMatthieu Baerts
759cbfafac4SGeliang Tang	local extra_args
7608d014eaaSGeliang Tang	if [ $speed = "fast" ]; then
761cbfafac4SGeliang Tang		extra_args="-j"
7628da6229bSGeliang Tang	elif [ $speed = "slow" ]; then
763cbfafac4SGeliang Tang		extra_args="-r 50"
764cbfafac4SGeliang Tang	elif [[ $speed = "speed_"* ]]; then
765cbfafac4SGeliang Tang		extra_args="-r ${speed:6}"
766dd72b0feSGeliang Tang	fi
767dd72b0feSGeliang Tang
76897040cf9SGeliang Tang	if [[ "${addr_nr_ns1}" = "userspace_"* ]]; then
76997040cf9SGeliang Tang		userspace_pm=1
77097040cf9SGeliang Tang		addr_nr_ns1=${addr_nr_ns1:10}
77197040cf9SGeliang Tang	fi
77297040cf9SGeliang Tang
773a1c3bdd9SGeliang Tang	local flags="subflow"
7746bf41020SPaolo Abeni	local extra_cl_args=""
7756bf41020SPaolo Abeni	local extra_srv_args=""
7766bf41020SPaolo Abeni	local trunc_size=""
77701542c9bSGeliang Tang	if [[ "${addr_nr_ns2}" = "fastclose_"* ]]; then
7786bf41020SPaolo Abeni		if [ ${test_link_fail} -le 1 ]; then
7796bf41020SPaolo Abeni			echo "fastclose tests need test_link_fail argument"
7806bf41020SPaolo Abeni			fail_test
7816bf41020SPaolo Abeni			return 1
7826bf41020SPaolo Abeni		fi
7836bf41020SPaolo Abeni
78401542c9bSGeliang Tang		# disconnect
7856bf41020SPaolo Abeni		trunc_size=${test_link_fail}
7866bf41020SPaolo Abeni		local side=${addr_nr_ns2:10}
7876bf41020SPaolo Abeni
7886bf41020SPaolo Abeni		if [ ${side} = "client" ]; then
7896bf41020SPaolo Abeni			extra_cl_args="-f ${test_link_fail}"
7906bf41020SPaolo Abeni			extra_srv_args="-f -1"
7916bf41020SPaolo Abeni		elif [ ${side} = "server" ]; then
7926bf41020SPaolo Abeni			extra_srv_args="-f ${test_link_fail}"
7936bf41020SPaolo Abeni			extra_cl_args="-f -1"
7946bf41020SPaolo Abeni		else
7956bf41020SPaolo Abeni			echo "wrong/unknown fastclose spec ${side}"
7966bf41020SPaolo Abeni			fail_test
7976bf41020SPaolo Abeni			return 1
7986bf41020SPaolo Abeni		fi
79901542c9bSGeliang Tang		addr_nr_ns2=0
8005e986ec4SGeliang Tang	elif [[ "${addr_nr_ns2}" = "userspace_"* ]]; then
8015e986ec4SGeliang Tang		userspace_pm=1
8025e986ec4SGeliang Tang		addr_nr_ns2=${addr_nr_ns2:10}
803a1c3bdd9SGeliang Tang	elif [[ "${addr_nr_ns2}" = "fullmesh_"* ]]; then
804a1c3bdd9SGeliang Tang		flags="${flags},fullmesh"
805a1c3bdd9SGeliang Tang		addr_nr_ns2=${addr_nr_ns2:9}
80601542c9bSGeliang Tang	fi
80701542c9bSGeliang Tang
8086bf41020SPaolo Abeni	extra_srv_args="$extra_args $extra_srv_args"
80934b572b7SGeliang Tang	if [ "$test_link_fail" -gt 1 ];then
8107d1e6f16SPaolo Abeni		timeout ${timeout_test} \
8117d1e6f16SPaolo Abeni			ip netns exec ${listener_ns} \
812cbfafac4SGeliang Tang				./mptcp_connect -t ${timeout_poll} -l -p $port -s ${srv_proto} \
813ad349374SPaolo Abeni					$extra_srv_args "::" < "$sinfail" > "$sout" &
8147d1e6f16SPaolo Abeni	else
8155888a61cSMatthieu Baerts		timeout ${timeout_test} \
8165888a61cSMatthieu Baerts			ip netns exec ${listener_ns} \
817cbfafac4SGeliang Tang				./mptcp_connect -t ${timeout_poll} -l -p $port -s ${srv_proto} \
818ad349374SPaolo Abeni					$extra_srv_args "::" < "$sin" > "$sout" &
8197d1e6f16SPaolo Abeni	fi
8201e777bd8SMatthieu Baerts	local spid=$!
821b08fbf24SPaolo Abeni
822327b9a94SPaolo Abeni	wait_local_port_listen "${listener_ns}" "${port}"
823b08fbf24SPaolo Abeni
8246bf41020SPaolo Abeni	extra_cl_args="$extra_args $extra_cl_args"
8258b819a84SFlorian Westphal	if [ "$test_link_fail" -eq 0 ];then
8265888a61cSMatthieu Baerts		timeout ${timeout_test} \
8275888a61cSMatthieu Baerts			ip netns exec ${connector_ns} \
828cbfafac4SGeliang Tang				./mptcp_connect -t ${timeout_poll} -p $port -s ${cl_proto} \
8296bf41020SPaolo Abeni					$extra_cl_args $connect_addr < "$cin" > "$cout" &
83034b572b7SGeliang Tang	elif [ "$test_link_fail" -eq 1 ] || [ "$test_link_fail" -eq 2 ];then
8317d1e6f16SPaolo Abeni		( cat "$cinfail" ; sleep 2; link_failure $listener_ns ; cat "$cinfail" ) | \
8325888a61cSMatthieu Baerts			tee "$cinsent" | \
8335888a61cSMatthieu Baerts			timeout ${timeout_test} \
8345888a61cSMatthieu Baerts				ip netns exec ${connector_ns} \
835cbfafac4SGeliang Tang					./mptcp_connect -t ${timeout_poll} -p $port -s ${cl_proto} \
8366bf41020SPaolo Abeni						$extra_cl_args $connect_addr > "$cout" &
83734b572b7SGeliang Tang	else
838d8d08302SMatthieu Baerts		tee "$cinsent" < "$cinfail" | \
83934b572b7SGeliang Tang			timeout ${timeout_test} \
84034b572b7SGeliang Tang				ip netns exec ${connector_ns} \
84134b572b7SGeliang Tang					./mptcp_connect -t ${timeout_poll} -p $port -s ${cl_proto} \
8426bf41020SPaolo Abeni						$extra_cl_args $connect_addr > "$cout" &
8438b819a84SFlorian Westphal	fi
8441e777bd8SMatthieu Baerts	local cpid=$!
845b08fbf24SPaolo Abeni
846327b9a94SPaolo Abeni	# let the mptcp subflow be established in background before
847327b9a94SPaolo Abeni	# do endpoint manipulation
848d8d08302SMatthieu Baerts	if [ $addr_nr_ns1 != "0" ] || [ $addr_nr_ns2 != "0" ]; then
849d8d08302SMatthieu Baerts		sleep 1
850d8d08302SMatthieu Baerts	fi
851327b9a94SPaolo Abeni
8526208fd82SGeliang Tang	if [ $addr_nr_ns1 -gt 0 ]; then
8531e777bd8SMatthieu Baerts		local counter=2
854d8d08302SMatthieu Baerts		local add_nr_ns1=${addr_nr_ns1}
85597040cf9SGeliang Tang		local id=10
85697040cf9SGeliang Tang		local tk
8576208fd82SGeliang Tang		while [ $add_nr_ns1 -gt 0 ]; do
8586208fd82SGeliang Tang			local addr
8596208fd82SGeliang Tang			if is_v6 "${connect_addr}"; then
8606208fd82SGeliang Tang				addr="dead:beef:$counter::1"
8616208fd82SGeliang Tang			else
8626208fd82SGeliang Tang				addr="10.0.$counter.1"
8636208fd82SGeliang Tang			fi
86497040cf9SGeliang Tang			if [ $userspace_pm -eq 0 ]; then
86534aa6e3bSGeliang Tang				pm_nl_add_endpoint $ns1 $addr flags signal
86697040cf9SGeliang Tang			else
8677dff74f5SGeliang Tang				tk=$(grep "type:1," "$evts_ns1" |
8687dff74f5SGeliang Tang				     sed -n 's/.*\(token:\)\([[:digit:]]*\).*$/\2/p;q')
86997040cf9SGeliang Tang				ip netns exec ${listener_ns} ./pm_nl_ctl ann $addr token $tk id $id
87097040cf9SGeliang Tang				sleep 1
87148d73f60SGeliang Tang				sp=$(grep "type:10" "$evts_ns1" |
87248d73f60SGeliang Tang				     sed -n 's/.*\(sport:\)\([[:digit:]]*\).*$/\2/p;q')
87348d73f60SGeliang Tang				da=$(grep "type:10" "$evts_ns1" |
87448d73f60SGeliang Tang				     sed -n 's/.*\(daddr6:\)\([0-9a-f:.]*\).*$/\2/p;q')
87548d73f60SGeliang Tang				dp=$(grep "type:10" "$evts_ns1" |
87648d73f60SGeliang Tang				     sed -n 's/.*\(dport:\)\([[:digit:]]*\).*$/\2/p;q')
87797040cf9SGeliang Tang				ip netns exec ${listener_ns} ./pm_nl_ctl rem token $tk id $id
87848d73f60SGeliang Tang				ip netns exec ${listener_ns} ./pm_nl_ctl dsf lip "::ffff:$addr" \
87948d73f60SGeliang Tang							lport $sp rip $da rport $dp token $tk
88097040cf9SGeliang Tang			fi
88197040cf9SGeliang Tang
882d8d08302SMatthieu Baerts			counter=$((counter + 1))
883d8d08302SMatthieu Baerts			add_nr_ns1=$((add_nr_ns1 - 1))
88497040cf9SGeliang Tang			id=$((id + 1))
8856208fd82SGeliang Tang		done
8866208fd82SGeliang Tang	elif [ $addr_nr_ns1 -lt 0 ]; then
887d8d08302SMatthieu Baerts		local rm_nr_ns1=$((-addr_nr_ns1))
8886fe4ccdcSGeliang Tang		if [ $rm_nr_ns1 -lt 8 ]; then
8891e777bd8SMatthieu Baerts			local counter=0
8901e777bd8SMatthieu Baerts			local line
891d8d08302SMatthieu Baerts			pm_nl_show_endpoints ${listener_ns} | while read -r line; do
892d8d08302SMatthieu Baerts				# shellcheck disable=SC2206 # we do want to split per word
893dda61b3dSGeliang Tang				local arr=($line)
894dda61b3dSGeliang Tang				local nr=0
895dda61b3dSGeliang Tang
8961e777bd8SMatthieu Baerts				local i
897d8d08302SMatthieu Baerts				for i in "${arr[@]}"; do
898dda61b3dSGeliang Tang					if [ $i = "id" ]; then
899dda61b3dSGeliang Tang						if [ $counter -eq $rm_nr_ns1 ]; then
900dda61b3dSGeliang Tang							break
901dda61b3dSGeliang Tang						fi
902dda61b3dSGeliang Tang						id=${arr[$nr+1]}
903327b9a94SPaolo Abeni						rm_addr=$(rm_addr_count ${connector_ns})
90434aa6e3bSGeliang Tang						pm_nl_del_endpoint ${listener_ns} $id
905327b9a94SPaolo Abeni						wait_rm_addr ${connector_ns} ${rm_addr}
906d8d08302SMatthieu Baerts						counter=$((counter + 1))
907f87744adSGeliang Tang					fi
908d8d08302SMatthieu Baerts					nr=$((nr + 1))
909dda61b3dSGeliang Tang				done
910dda61b3dSGeliang Tang			done
9115e287fe7SGeliang Tang		elif [ $rm_nr_ns1 -eq 8 ]; then
91234aa6e3bSGeliang Tang			pm_nl_flush_endpoint ${listener_ns}
9135e287fe7SGeliang Tang		elif [ $rm_nr_ns1 -eq 9 ]; then
91434aa6e3bSGeliang Tang			pm_nl_del_endpoint ${listener_ns} 0 ${connect_addr}
9156fe4ccdcSGeliang Tang		fi
916dd72b0feSGeliang Tang	fi
917dd72b0feSGeliang Tang
918327b9a94SPaolo Abeni	# if newly added endpoints must be deleted, give the background msk
919327b9a94SPaolo Abeni	# some time to created them
920d8d08302SMatthieu Baerts	[ $addr_nr_ns1 -gt 0 ] && [ $addr_nr_ns2 -lt 0 ] && sleep 1
921327b9a94SPaolo Abeni
9226208fd82SGeliang Tang	if [ $addr_nr_ns2 -gt 0 ]; then
923d8d08302SMatthieu Baerts		local add_nr_ns2=${addr_nr_ns2}
9241e777bd8SMatthieu Baerts		local counter=3
9255e986ec4SGeliang Tang		local id=20
9265e986ec4SGeliang Tang		local tk da dp sp
9276208fd82SGeliang Tang		while [ $add_nr_ns2 -gt 0 ]; do
9286208fd82SGeliang Tang			local addr
9296208fd82SGeliang Tang			if is_v6 "${connect_addr}"; then
9306208fd82SGeliang Tang				addr="dead:beef:$counter::2"
9316208fd82SGeliang Tang			else
9326208fd82SGeliang Tang				addr="10.0.$counter.2"
9336208fd82SGeliang Tang			fi
9345e986ec4SGeliang Tang			if [ $userspace_pm -eq 0 ]; then
93534aa6e3bSGeliang Tang				pm_nl_add_endpoint $ns2 $addr flags $flags
9365e986ec4SGeliang Tang			else
9375e986ec4SGeliang Tang				tk=$(sed -n 's/.*\(token:\)\([[:digit:]]*\).*$/\2/p;q' "$evts_ns2")
9385e986ec4SGeliang Tang				da=$(sed -n 's/.*\(daddr4:\)\([0-9.]*\).*$/\2/p;q' "$evts_ns2")
9395e986ec4SGeliang Tang				dp=$(sed -n 's/.*\(dport:\)\([[:digit:]]*\).*$/\2/p;q' "$evts_ns2")
9405e986ec4SGeliang Tang				ip netns exec ${connector_ns} ./pm_nl_ctl csf lip $addr lid $id \
9415e986ec4SGeliang Tang									rip $da rport $dp token $tk
9425e986ec4SGeliang Tang				sleep 1
9435e986ec4SGeliang Tang				sp=$(grep "type:10" "$evts_ns2" |
9445e986ec4SGeliang Tang				     sed -n 's/.*\(sport:\)\([[:digit:]]*\).*$/\2/p;q')
9456c160b63SGeliang Tang				ip netns exec ${connector_ns} ./pm_nl_ctl rem token $tk id $id
9465e986ec4SGeliang Tang				ip netns exec ${connector_ns} ./pm_nl_ctl dsf lip $addr lport $sp \
9475e986ec4SGeliang Tang									rip $da rport $dp token $tk
9485e986ec4SGeliang Tang			fi
949d8d08302SMatthieu Baerts			counter=$((counter + 1))
950d8d08302SMatthieu Baerts			add_nr_ns2=$((add_nr_ns2 - 1))
9515e986ec4SGeliang Tang			id=$((id + 1))
9526208fd82SGeliang Tang		done
9536208fd82SGeliang Tang	elif [ $addr_nr_ns2 -lt 0 ]; then
954d8d08302SMatthieu Baerts		local rm_nr_ns2=$((-addr_nr_ns2))
9556fe4ccdcSGeliang Tang		if [ $rm_nr_ns2 -lt 8 ]; then
9561e777bd8SMatthieu Baerts			local counter=0
9571e777bd8SMatthieu Baerts			local line
958d8d08302SMatthieu Baerts			pm_nl_show_endpoints ${connector_ns} | while read -r line; do
959d8d08302SMatthieu Baerts				# shellcheck disable=SC2206 # we do want to split per word
960dda61b3dSGeliang Tang				local arr=($line)
961dda61b3dSGeliang Tang				local nr=0
962dda61b3dSGeliang Tang
9631e777bd8SMatthieu Baerts				local i
964d8d08302SMatthieu Baerts				for i in "${arr[@]}"; do
965dda61b3dSGeliang Tang					if [ $i = "id" ]; then
966dda61b3dSGeliang Tang						if [ $counter -eq $rm_nr_ns2 ]; then
967dda61b3dSGeliang Tang							break
968dda61b3dSGeliang Tang						fi
9691e777bd8SMatthieu Baerts						local id rm_addr
970dda61b3dSGeliang Tang						# rm_addr are serialized, allow the previous one to
971dda61b3dSGeliang Tang						# complete
972dda61b3dSGeliang Tang						id=${arr[$nr+1]}
973327b9a94SPaolo Abeni						rm_addr=$(rm_addr_count ${listener_ns})
97434aa6e3bSGeliang Tang						pm_nl_del_endpoint ${connector_ns} $id
975327b9a94SPaolo Abeni						wait_rm_addr ${listener_ns} ${rm_addr}
976d8d08302SMatthieu Baerts						counter=$((counter + 1))
977f87744adSGeliang Tang					fi
978d8d08302SMatthieu Baerts					nr=$((nr + 1))
979dda61b3dSGeliang Tang				done
980dda61b3dSGeliang Tang			done
9815e287fe7SGeliang Tang		elif [ $rm_nr_ns2 -eq 8 ]; then
98234aa6e3bSGeliang Tang			pm_nl_flush_endpoint ${connector_ns}
9835e287fe7SGeliang Tang		elif [ $rm_nr_ns2 -eq 9 ]; then
9845e287fe7SGeliang Tang			local addr
9855e287fe7SGeliang Tang			if is_v6 "${connect_addr}"; then
9865e287fe7SGeliang Tang				addr="dead:beef:1::2"
9875e287fe7SGeliang Tang			else
9885e287fe7SGeliang Tang				addr="10.0.1.2"
9895e287fe7SGeliang Tang			fi
99034aa6e3bSGeliang Tang			pm_nl_del_endpoint ${connector_ns} 0 $addr
9916fe4ccdcSGeliang Tang		fi
992dd72b0feSGeliang Tang	fi
993dd72b0feSGeliang Tang
994d8d08302SMatthieu Baerts	if [ -n "${sflags}" ]; then
995718eb44eSGeliang Tang		sleep 1
9961e777bd8SMatthieu Baerts
9971e777bd8SMatthieu Baerts		local netns
998718eb44eSGeliang Tang		for netns in "$ns1" "$ns2"; do
9991e777bd8SMatthieu Baerts			local line
1000d8d08302SMatthieu Baerts			pm_nl_show_endpoints $netns | while read -r line; do
1001d8d08302SMatthieu Baerts				# shellcheck disable=SC2206 # we do want to split per word
100233397b83SGeliang Tang				local arr=($line)
1003bccefb76SGeliang Tang				local nr=0
1004f0140386SGeliang Tang				local id
100533397b83SGeliang Tang
10061e777bd8SMatthieu Baerts				local i
1007d8d08302SMatthieu Baerts				for i in "${arr[@]}"; do
1008bccefb76SGeliang Tang					if [ $i = "id" ]; then
1009bccefb76SGeliang Tang						id=${arr[$nr+1]}
1010718eb44eSGeliang Tang					fi
1011d8d08302SMatthieu Baerts					nr=$((nr + 1))
101233397b83SGeliang Tang				done
1013bccefb76SGeliang Tang				pm_nl_change_endpoint $netns $id $sflags
101433397b83SGeliang Tang			done
1015718eb44eSGeliang Tang		done
1016718eb44eSGeliang Tang	fi
1017718eb44eSGeliang Tang
1018b08fbf24SPaolo Abeni	wait $cpid
10191e777bd8SMatthieu Baerts	local retc=$?
1020b08fbf24SPaolo Abeni	wait $spid
10211e777bd8SMatthieu Baerts	local rets=$?
1022b08fbf24SPaolo Abeni
1023b08fbf24SPaolo Abeni	if [ $capture -eq 1 ]; then
1024b08fbf24SPaolo Abeni	    sleep 1
1025b08fbf24SPaolo Abeni	    kill $cappid
1026b08fbf24SPaolo Abeni	fi
1027b08fbf24SPaolo Abeni
1028c2a55e8fSMatthieu Baerts	NSTAT_HISTORY=/tmp/${listener_ns}.nstat ip netns exec ${listener_ns} \
1029c2a55e8fSMatthieu Baerts		nstat | grep Tcp > /tmp/${listener_ns}.out
1030c2a55e8fSMatthieu Baerts	NSTAT_HISTORY=/tmp/${connector_ns}.nstat ip netns exec ${connector_ns} \
1031c2a55e8fSMatthieu Baerts		nstat | grep Tcp > /tmp/${connector_ns}.out
1032c2a55e8fSMatthieu Baerts
1033b08fbf24SPaolo Abeni	if [ ${rets} -ne 0 ] || [ ${retc} -ne 0 ]; then
1034b08fbf24SPaolo Abeni		echo " client exit code $retc, server $rets" 1>&2
10358b974778SMatthieu Baerts		echo -e "\nnetns ${listener_ns} socket stat for ${port}:" 1>&2
1036c2a55e8fSMatthieu Baerts		ip netns exec ${listener_ns} ss -Menita 1>&2 -o "sport = :$port"
1037c2a55e8fSMatthieu Baerts		cat /tmp/${listener_ns}.out
10388b974778SMatthieu Baerts		echo -e "\nnetns ${connector_ns} socket stat for ${port}:" 1>&2
1039c2a55e8fSMatthieu Baerts		ip netns exec ${connector_ns} ss -Menita 1>&2 -o "dport = :$port"
1040c2a55e8fSMatthieu Baerts		cat /tmp/${connector_ns}.out
1041b08fbf24SPaolo Abeni
1042b08fbf24SPaolo Abeni		cat "$capout"
104339aab882SMatthieu Baerts		fail_test
1044b08fbf24SPaolo Abeni		return 1
1045b08fbf24SPaolo Abeni	fi
1046b08fbf24SPaolo Abeni
104734b572b7SGeliang Tang	if [ "$test_link_fail" -gt 1 ];then
10486bf41020SPaolo Abeni		check_transfer $sinfail $cout "file received by client" $trunc_size
10497d1e6f16SPaolo Abeni	else
10506bf41020SPaolo Abeni		check_transfer $sin $cout "file received by client" $trunc_size
10517d1e6f16SPaolo Abeni	fi
1052b08fbf24SPaolo Abeni	retc=$?
10538b819a84SFlorian Westphal	if [ "$test_link_fail" -eq 0 ];then
10546bf41020SPaolo Abeni		check_transfer $cin $sout "file received by server" $trunc_size
10558b819a84SFlorian Westphal	else
10566bf41020SPaolo Abeni		check_transfer $cinsent $sout "file received by server" $trunc_size
10578b819a84SFlorian Westphal	fi
1058b08fbf24SPaolo Abeni	rets=$?
1059b08fbf24SPaolo Abeni
1060b08fbf24SPaolo Abeni	if [ $retc -eq 0 ] && [ $rets -eq 0 ];then
1061b08fbf24SPaolo Abeni		cat "$capout"
1062b08fbf24SPaolo Abeni		return 0
1063b08fbf24SPaolo Abeni	fi
1064b08fbf24SPaolo Abeni
1065b08fbf24SPaolo Abeni	cat "$capout"
1066b08fbf24SPaolo Abeni	return 1
1067b08fbf24SPaolo Abeni}
1068b08fbf24SPaolo Abeni
1069b08fbf24SPaolo Abenimake_file()
1070b08fbf24SPaolo Abeni{
10711e777bd8SMatthieu Baerts	local name=$1
10721e777bd8SMatthieu Baerts	local who=$2
10731e777bd8SMatthieu Baerts	local size=$3
1074b08fbf24SPaolo Abeni
10758b819a84SFlorian Westphal	dd if=/dev/urandom of="$name" bs=1024 count=$size 2> /dev/null
1076b08fbf24SPaolo Abeni	echo -e "\nMPTCP_TEST_FILE_END_MARKER" >> "$name"
1077b08fbf24SPaolo Abeni
10788b819a84SFlorian Westphal	echo "Created $name (size $size KB) containing data sent by $who"
1079b08fbf24SPaolo Abeni}
1080b08fbf24SPaolo Abeni
1081b08fbf24SPaolo Abenirun_tests()
1082b08fbf24SPaolo Abeni{
10831e777bd8SMatthieu Baerts	local listener_ns="$1"
10841e777bd8SMatthieu Baerts	local connector_ns="$2"
10851e777bd8SMatthieu Baerts	local connect_addr="$3"
10861e777bd8SMatthieu Baerts	local test_linkfail="${4:-0}"
10871e777bd8SMatthieu Baerts	local addr_nr_ns1="${5:-0}"
10881e777bd8SMatthieu Baerts	local addr_nr_ns2="${6:-0}"
10891e777bd8SMatthieu Baerts	local speed="${7:-fast}"
10901e777bd8SMatthieu Baerts	local sflags="${8:-""}"
10911e777bd8SMatthieu Baerts
10921e777bd8SMatthieu Baerts	local size
10938b819a84SFlorian Westphal
109434b572b7SGeliang Tang	# The values above 2 are reused to make test files
109534b572b7SGeliang Tang	# with the given sizes (KB)
109634b572b7SGeliang Tang	if [ "$test_linkfail" -gt 2 ]; then
109734b572b7SGeliang Tang		size=$test_linkfail
109834b572b7SGeliang Tang
109934b572b7SGeliang Tang		if [ -z "$cinfail" ]; then
110034b572b7SGeliang Tang			cinfail=$(mktemp)
110134b572b7SGeliang Tang		fi
110234b572b7SGeliang Tang		make_file "$cinfail" "client" $size
11037d1e6f16SPaolo Abeni	# create the input file for the failure test when
11047d1e6f16SPaolo Abeni	# the first failure test run
1105d8d08302SMatthieu Baerts	elif [ "$test_linkfail" -ne 0 ] && [ -z "$cinfail" ]; then
11067d1e6f16SPaolo Abeni		# the client file must be considerably larger
11077d1e6f16SPaolo Abeni		# of the maximum expected cwin value, or the
11087d1e6f16SPaolo Abeni		# link utilization will be not predicable
11097d1e6f16SPaolo Abeni		size=$((RANDOM%2))
11108b819a84SFlorian Westphal		size=$((size+1))
11117d1e6f16SPaolo Abeni		size=$((size*8192))
1112d8d08302SMatthieu Baerts		size=$((size + ( RANDOM % 8192) ))
11138b819a84SFlorian Westphal
11147d1e6f16SPaolo Abeni		cinfail=$(mktemp)
11157d1e6f16SPaolo Abeni		make_file "$cinfail" "client" $size
11167d1e6f16SPaolo Abeni	fi
11177d1e6f16SPaolo Abeni
111834b572b7SGeliang Tang	if [ "$test_linkfail" -gt 2 ]; then
111934b572b7SGeliang Tang		size=$test_linkfail
112034b572b7SGeliang Tang
112134b572b7SGeliang Tang		if [ -z "$sinfail" ]; then
112234b572b7SGeliang Tang			sinfail=$(mktemp)
112334b572b7SGeliang Tang		fi
112434b572b7SGeliang Tang		make_file "$sinfail" "server" $size
1125d8d08302SMatthieu Baerts	elif [ "$test_linkfail" -eq 2 ] && [ -z "$sinfail" ]; then
11267d1e6f16SPaolo Abeni		size=$((RANDOM%16))
11277d1e6f16SPaolo Abeni		size=$((size+1))
11287d1e6f16SPaolo Abeni		size=$((size*2048))
11297d1e6f16SPaolo Abeni
11307d1e6f16SPaolo Abeni		sinfail=$(mktemp)
11317d1e6f16SPaolo Abeni		make_file "$sinfail" "server" $size
11328b819a84SFlorian Westphal	fi
1133b08fbf24SPaolo Abeni
11348d014eaaSGeliang Tang	do_transfer ${listener_ns} ${connector_ns} MPTCP MPTCP ${connect_addr} \
11356a0653b9SGeliang Tang		${test_linkfail} ${addr_nr_ns1} ${addr_nr_ns2} ${speed} ${sflags}
1136b08fbf24SPaolo Abeni}
1137b08fbf24SPaolo Abeni
1138327b9a94SPaolo Abenidump_stats()
1139327b9a94SPaolo Abeni{
1140327b9a94SPaolo Abeni	echo Server ns stats
1141327b9a94SPaolo Abeni	ip netns exec $ns1 nstat -as | grep Tcp
1142327b9a94SPaolo Abeni	echo Client ns stats
1143327b9a94SPaolo Abeni	ip netns exec $ns2 nstat -as | grep Tcp
1144327b9a94SPaolo Abeni}
1145327b9a94SPaolo Abeni
1146af66d3e1SGeliang Tangchk_csum_nr()
1147af66d3e1SGeliang Tang{
11483c082695SGeliang Tang	local csum_ns1=${1:-0}
11493c082695SGeliang Tang	local csum_ns2=${2:-0}
1150af66d3e1SGeliang Tang	local count
1151af66d3e1SGeliang Tang	local dump_stats
115253f368bfSGeliang Tang	local extra_msg=""
115326516e10SGeliang Tang	local allow_multi_errors_ns1=0
115426516e10SGeliang Tang	local allow_multi_errors_ns2=0
115526516e10SGeliang Tang
115626516e10SGeliang Tang	if [[ "${csum_ns1}" = "+"* ]]; then
115726516e10SGeliang Tang		allow_multi_errors_ns1=1
115826516e10SGeliang Tang		csum_ns1=${csum_ns1:1}
115926516e10SGeliang Tang	fi
116026516e10SGeliang Tang	if [[ "${csum_ns2}" = "+"* ]]; then
116126516e10SGeliang Tang		allow_multi_errors_ns2=1
116226516e10SGeliang Tang		csum_ns2=${csum_ns2:1}
116326516e10SGeliang Tang	fi
1164af66d3e1SGeliang Tang
11653c082695SGeliang Tang	printf "%-${nr_blank}s %s" " " "sum"
11664bfadd71SMatthieu Baerts	count=$(ip netns exec $ns1 nstat -as | grep MPTcpExtDataCsumErr | awk '{print $2}')
1167af66d3e1SGeliang Tang	[ -z "$count" ] && count=0
116853f368bfSGeliang Tang	if [ "$count" != "$csum_ns1" ]; then
116953f368bfSGeliang Tang		extra_msg="$extra_msg ns1=$count"
117053f368bfSGeliang Tang	fi
1171d8d08302SMatthieu Baerts	if { [ "$count" != $csum_ns1 ] && [ $allow_multi_errors_ns1 -eq 0 ]; } ||
1172d8d08302SMatthieu Baerts	   { [ "$count" -lt $csum_ns1 ] && [ $allow_multi_errors_ns1 -eq 1 ]; }; then
117326516e10SGeliang Tang		echo "[fail] got $count data checksum error[s] expected $csum_ns1"
117439aab882SMatthieu Baerts		fail_test
1175af66d3e1SGeliang Tang		dump_stats=1
1176af66d3e1SGeliang Tang	else
1177af66d3e1SGeliang Tang		echo -n "[ ok ]"
1178af66d3e1SGeliang Tang	fi
1179af66d3e1SGeliang Tang	echo -n " - csum  "
11804bfadd71SMatthieu Baerts	count=$(ip netns exec $ns2 nstat -as | grep MPTcpExtDataCsumErr | awk '{print $2}')
1181af66d3e1SGeliang Tang	[ -z "$count" ] && count=0
118253f368bfSGeliang Tang	if [ "$count" != "$csum_ns2" ]; then
118353f368bfSGeliang Tang		extra_msg="$extra_msg ns2=$count"
118453f368bfSGeliang Tang	fi
1185d8d08302SMatthieu Baerts	if { [ "$count" != $csum_ns2 ] && [ $allow_multi_errors_ns2 -eq 0 ]; } ||
1186d8d08302SMatthieu Baerts	   { [ "$count" -lt $csum_ns2 ] && [ $allow_multi_errors_ns2 -eq 1 ]; }; then
118726516e10SGeliang Tang		echo "[fail] got $count data checksum error[s] expected $csum_ns2"
118839aab882SMatthieu Baerts		fail_test
1189af66d3e1SGeliang Tang		dump_stats=1
1190af66d3e1SGeliang Tang	else
119153f368bfSGeliang Tang		echo -n "[ ok ]"
1192af66d3e1SGeliang Tang	fi
1193327b9a94SPaolo Abeni	[ "${dump_stats}" = 1 ] && dump_stats
119453f368bfSGeliang Tang
119553f368bfSGeliang Tang	echo "$extra_msg"
1196af66d3e1SGeliang Tang}
1197af66d3e1SGeliang Tang
11986bb3ab49SGeliang Tangchk_fail_nr()
11996bb3ab49SGeliang Tang{
120026516e10SGeliang Tang	local fail_tx=$1
120126516e10SGeliang Tang	local fail_rx=$2
12021f7d325fSGeliang Tang	local ns_invert=${3:-""}
12036bb3ab49SGeliang Tang	local count
12046bb3ab49SGeliang Tang	local dump_stats
12051f7d325fSGeliang Tang	local ns_tx=$ns1
12061f7d325fSGeliang Tang	local ns_rx=$ns2
12071f7d325fSGeliang Tang	local extra_msg=""
12081f7d325fSGeliang Tang	local allow_tx_lost=0
12091f7d325fSGeliang Tang	local allow_rx_lost=0
12101f7d325fSGeliang Tang
12111f7d325fSGeliang Tang	if [[ $ns_invert = "invert" ]]; then
12121f7d325fSGeliang Tang		ns_tx=$ns2
12131f7d325fSGeliang Tang		ns_rx=$ns1
12141f7d325fSGeliang Tang		extra_msg=" invert"
12151f7d325fSGeliang Tang	fi
12161f7d325fSGeliang Tang
12171f7d325fSGeliang Tang	if [[ "${fail_tx}" = "-"* ]]; then
12181f7d325fSGeliang Tang		allow_tx_lost=1
12191f7d325fSGeliang Tang		fail_tx=${fail_tx:1}
12201f7d325fSGeliang Tang	fi
12211f7d325fSGeliang Tang	if [[ "${fail_rx}" = "-"* ]]; then
12221f7d325fSGeliang Tang		allow_rx_lost=1
12231f7d325fSGeliang Tang		fail_rx=${fail_rx:1}
12241f7d325fSGeliang Tang	fi
12256bb3ab49SGeliang Tang
12269a0a9367SGeliang Tang	printf "%-${nr_blank}s %s" " " "ftx"
12271f7d325fSGeliang Tang	count=$(ip netns exec $ns_tx nstat -as | grep MPTcpExtMPFailTx | awk '{print $2}')
12286bb3ab49SGeliang Tang	[ -z "$count" ] && count=0
122926516e10SGeliang Tang	if [ "$count" != "$fail_tx" ]; then
12301f7d325fSGeliang Tang		extra_msg="$extra_msg,tx=$count"
12311f7d325fSGeliang Tang	fi
12321f7d325fSGeliang Tang	if { [ "$count" != "$fail_tx" ] && [ $allow_tx_lost -eq 0 ]; } ||
12331f7d325fSGeliang Tang	   { [ "$count" -gt "$fail_tx" ] && [ $allow_tx_lost -eq 1 ]; }; then
123426516e10SGeliang Tang		echo "[fail] got $count MP_FAIL[s] TX expected $fail_tx"
123539aab882SMatthieu Baerts		fail_test
12366bb3ab49SGeliang Tang		dump_stats=1
12376bb3ab49SGeliang Tang	else
12386bb3ab49SGeliang Tang		echo -n "[ ok ]"
12396bb3ab49SGeliang Tang	fi
12406bb3ab49SGeliang Tang
124126516e10SGeliang Tang	echo -n " - failrx"
12421f7d325fSGeliang Tang	count=$(ip netns exec $ns_rx nstat -as | grep MPTcpExtMPFailRx | awk '{print $2}')
12436bb3ab49SGeliang Tang	[ -z "$count" ] && count=0
124426516e10SGeliang Tang	if [ "$count" != "$fail_rx" ]; then
12451f7d325fSGeliang Tang		extra_msg="$extra_msg,rx=$count"
12461f7d325fSGeliang Tang	fi
12471f7d325fSGeliang Tang	if { [ "$count" != "$fail_rx" ] && [ $allow_rx_lost -eq 0 ]; } ||
12481f7d325fSGeliang Tang	   { [ "$count" -gt "$fail_rx" ] && [ $allow_rx_lost -eq 1 ]; }; then
124926516e10SGeliang Tang		echo "[fail] got $count MP_FAIL[s] RX expected $fail_rx"
125039aab882SMatthieu Baerts		fail_test
12516bb3ab49SGeliang Tang		dump_stats=1
12526bb3ab49SGeliang Tang	else
12531f7d325fSGeliang Tang		echo -n "[ ok ]"
12546bb3ab49SGeliang Tang	fi
12556bb3ab49SGeliang Tang
1256327b9a94SPaolo Abeni	[ "${dump_stats}" = 1 ] && dump_stats
12571f7d325fSGeliang Tang
12581f7d325fSGeliang Tang	echo "$extra_msg"
12596bb3ab49SGeliang Tang}
12606bb3ab49SGeliang Tang
1261e8e947efSGeliang Tangchk_fclose_nr()
1262e8e947efSGeliang Tang{
1263e8e947efSGeliang Tang	local fclose_tx=$1
1264e8e947efSGeliang Tang	local fclose_rx=$2
12656bf41020SPaolo Abeni	local ns_invert=$3
1266e8e947efSGeliang Tang	local count
1267e8e947efSGeliang Tang	local dump_stats
12686bf41020SPaolo Abeni	local ns_tx=$ns2
12696bf41020SPaolo Abeni	local ns_rx=$ns1
12706bf41020SPaolo Abeni	local extra_msg="   "
12716bf41020SPaolo Abeni
12726bf41020SPaolo Abeni	if [[ $ns_invert = "invert" ]]; then
12736bf41020SPaolo Abeni		ns_tx=$ns1
12746bf41020SPaolo Abeni		ns_rx=$ns2
12756bf41020SPaolo Abeni		extra_msg=${extra_msg}"invert"
12766bf41020SPaolo Abeni	fi
1277e8e947efSGeliang Tang
1278e8e947efSGeliang Tang	printf "%-${nr_blank}s %s" " " "ctx"
12796bf41020SPaolo Abeni	count=$(ip netns exec $ns_tx nstat -as | grep MPTcpExtMPFastcloseTx | awk '{print $2}')
1280e8e947efSGeliang Tang	[ -z "$count" ] && count=0
12816bf41020SPaolo Abeni	[ "$count" != "$fclose_tx" ] && extra_msg="$extra_msg,tx=$count"
1282e8e947efSGeliang Tang	if [ "$count" != "$fclose_tx" ]; then
1283e8e947efSGeliang Tang		echo "[fail] got $count MP_FASTCLOSE[s] TX expected $fclose_tx"
128439aab882SMatthieu Baerts		fail_test
1285e8e947efSGeliang Tang		dump_stats=1
1286e8e947efSGeliang Tang	else
1287e8e947efSGeliang Tang		echo -n "[ ok ]"
1288e8e947efSGeliang Tang	fi
1289e8e947efSGeliang Tang
1290e8e947efSGeliang Tang	echo -n " - fclzrx"
12916bf41020SPaolo Abeni	count=$(ip netns exec $ns_rx nstat -as | grep MPTcpExtMPFastcloseRx | awk '{print $2}')
1292e8e947efSGeliang Tang	[ -z "$count" ] && count=0
12936bf41020SPaolo Abeni	[ "$count" != "$fclose_rx" ] && extra_msg="$extra_msg,rx=$count"
1294e8e947efSGeliang Tang	if [ "$count" != "$fclose_rx" ]; then
1295e8e947efSGeliang Tang		echo "[fail] got $count MP_FASTCLOSE[s] RX expected $fclose_rx"
129639aab882SMatthieu Baerts		fail_test
1297e8e947efSGeliang Tang		dump_stats=1
1298e8e947efSGeliang Tang	else
12996bf41020SPaolo Abeni		echo -n "[ ok ]"
1300e8e947efSGeliang Tang	fi
1301e8e947efSGeliang Tang
1302e8e947efSGeliang Tang	[ "${dump_stats}" = 1 ] && dump_stats
13036bf41020SPaolo Abeni
13046bf41020SPaolo Abeni	echo "$extra_msg"
1305e8e947efSGeliang Tang}
1306e8e947efSGeliang Tang
1307922fd2b3SGeliang Tangchk_rst_nr()
1308922fd2b3SGeliang Tang{
1309922fd2b3SGeliang Tang	local rst_tx=$1
1310922fd2b3SGeliang Tang	local rst_rx=$2
1311922fd2b3SGeliang Tang	local ns_invert=${3:-""}
1312922fd2b3SGeliang Tang	local count
1313922fd2b3SGeliang Tang	local dump_stats
1314922fd2b3SGeliang Tang	local ns_tx=$ns1
1315922fd2b3SGeliang Tang	local ns_rx=$ns2
1316922fd2b3SGeliang Tang	local extra_msg=""
1317922fd2b3SGeliang Tang
1318922fd2b3SGeliang Tang	if [[ $ns_invert = "invert" ]]; then
1319922fd2b3SGeliang Tang		ns_tx=$ns2
1320922fd2b3SGeliang Tang		ns_rx=$ns1
1321922fd2b3SGeliang Tang		extra_msg="   invert"
1322922fd2b3SGeliang Tang	fi
1323922fd2b3SGeliang Tang
1324922fd2b3SGeliang Tang	printf "%-${nr_blank}s %s" " " "rtx"
1325922fd2b3SGeliang Tang	count=$(ip netns exec $ns_tx nstat -as | grep MPTcpExtMPRstTx | awk '{print $2}')
1326922fd2b3SGeliang Tang	[ -z "$count" ] && count=0
13276bf41020SPaolo Abeni	if [ $count -lt $rst_tx ]; then
1328922fd2b3SGeliang Tang		echo "[fail] got $count MP_RST[s] TX expected $rst_tx"
132939aab882SMatthieu Baerts		fail_test
1330922fd2b3SGeliang Tang		dump_stats=1
1331922fd2b3SGeliang Tang	else
1332922fd2b3SGeliang Tang		echo -n "[ ok ]"
1333922fd2b3SGeliang Tang	fi
1334922fd2b3SGeliang Tang
1335922fd2b3SGeliang Tang	echo -n " - rstrx "
1336922fd2b3SGeliang Tang	count=$(ip netns exec $ns_rx nstat -as | grep MPTcpExtMPRstRx | awk '{print $2}')
1337922fd2b3SGeliang Tang	[ -z "$count" ] && count=0
13386bf41020SPaolo Abeni	if [ "$count" -lt "$rst_rx" ]; then
1339922fd2b3SGeliang Tang		echo "[fail] got $count MP_RST[s] RX expected $rst_rx"
134039aab882SMatthieu Baerts		fail_test
1341922fd2b3SGeliang Tang		dump_stats=1
1342922fd2b3SGeliang Tang	else
1343922fd2b3SGeliang Tang		echo -n "[ ok ]"
1344922fd2b3SGeliang Tang	fi
1345922fd2b3SGeliang Tang
1346922fd2b3SGeliang Tang	[ "${dump_stats}" = 1 ] && dump_stats
1347922fd2b3SGeliang Tang
1348922fd2b3SGeliang Tang	echo "$extra_msg"
1349922fd2b3SGeliang Tang}
1350922fd2b3SGeliang Tang
13518bd03be3SGeliang Tangchk_infi_nr()
13528bd03be3SGeliang Tang{
13538bd03be3SGeliang Tang	local infi_tx=$1
13548bd03be3SGeliang Tang	local infi_rx=$2
13558bd03be3SGeliang Tang	local count
13568bd03be3SGeliang Tang	local dump_stats
13578bd03be3SGeliang Tang
13588bd03be3SGeliang Tang	printf "%-${nr_blank}s %s" " " "itx"
13598bd03be3SGeliang Tang	count=$(ip netns exec $ns2 nstat -as | grep InfiniteMapTx | awk '{print $2}')
13608bd03be3SGeliang Tang	[ -z "$count" ] && count=0
13618bd03be3SGeliang Tang	if [ "$count" != "$infi_tx" ]; then
13628bd03be3SGeliang Tang		echo "[fail] got $count infinite map[s] TX expected $infi_tx"
13638bd03be3SGeliang Tang		fail_test
13648bd03be3SGeliang Tang		dump_stats=1
13658bd03be3SGeliang Tang	else
13668bd03be3SGeliang Tang		echo -n "[ ok ]"
13678bd03be3SGeliang Tang	fi
13688bd03be3SGeliang Tang
13698bd03be3SGeliang Tang	echo -n " - infirx"
13708bd03be3SGeliang Tang	count=$(ip netns exec $ns1 nstat -as | grep InfiniteMapRx | awk '{print $2}')
13718bd03be3SGeliang Tang	[ -z "$count" ] && count=0
13728bd03be3SGeliang Tang	if [ "$count" != "$infi_rx" ]; then
13738bd03be3SGeliang Tang		echo "[fail] got $count infinite map[s] RX expected $infi_rx"
13748bd03be3SGeliang Tang		fail_test
13758bd03be3SGeliang Tang		dump_stats=1
13768bd03be3SGeliang Tang	else
13778bd03be3SGeliang Tang		echo "[ ok ]"
13788bd03be3SGeliang Tang	fi
13798bd03be3SGeliang Tang
13808bd03be3SGeliang Tang	[ "${dump_stats}" = 1 ] && dump_stats
13818bd03be3SGeliang Tang}
13828bd03be3SGeliang Tang
1383b08fbf24SPaolo Abenichk_join_nr()
1384b08fbf24SPaolo Abeni{
1385c7d49c03SMatthieu Baerts	local syn_nr=$1
1386c7d49c03SMatthieu Baerts	local syn_ack_nr=$2
1387c7d49c03SMatthieu Baerts	local ack_nr=$3
1388c7d49c03SMatthieu Baerts	local csum_ns1=${4:-0}
1389c7d49c03SMatthieu Baerts	local csum_ns2=${5:-0}
1390c7d49c03SMatthieu Baerts	local fail_nr=${6:-0}
1391c7d49c03SMatthieu Baerts	local rst_nr=${7:-0}
13928bd03be3SGeliang Tang	local infi_nr=${8:-0}
13938bd03be3SGeliang Tang	local corrupted_pkts=${9:-0}
1394b08fbf24SPaolo Abeni	local count
1395b08fbf24SPaolo Abeni	local dump_stats
1396e35f885bSPaolo Abeni	local with_cookie
1397c7d49c03SMatthieu Baerts	local title="${TEST_NAME}"
1398b08fbf24SPaolo Abeni
1399c7d49c03SMatthieu Baerts	if [ "${corrupted_pkts}" -gt 0 ]; then
1400c7d49c03SMatthieu Baerts		title+=": ${corrupted_pkts} corrupted pkts"
1401c7d49c03SMatthieu Baerts	fi
1402c7d49c03SMatthieu Baerts
1403c7d49c03SMatthieu Baerts	printf "%03u %-36s %s" "${TEST_COUNT}" "${title}" "syn"
14044bfadd71SMatthieu Baerts	count=$(ip netns exec $ns1 nstat -as | grep MPTcpExtMPJoinSynRx | awk '{print $2}')
1405b08fbf24SPaolo Abeni	[ -z "$count" ] && count=0
1406b08fbf24SPaolo Abeni	if [ "$count" != "$syn_nr" ]; then
1407b08fbf24SPaolo Abeni		echo "[fail] got $count JOIN[s] syn expected $syn_nr"
140839aab882SMatthieu Baerts		fail_test
1409b08fbf24SPaolo Abeni		dump_stats=1
1410b08fbf24SPaolo Abeni	else
1411b08fbf24SPaolo Abeni		echo -n "[ ok ]"
1412b08fbf24SPaolo Abeni	fi
1413b08fbf24SPaolo Abeni
1414b08fbf24SPaolo Abeni	echo -n " - synack"
14154bfadd71SMatthieu Baerts	with_cookie=$(ip netns exec $ns2 sysctl -n net.ipv4.tcp_syncookies)
14164bfadd71SMatthieu Baerts	count=$(ip netns exec $ns2 nstat -as | grep MPTcpExtMPJoinSynAckRx | awk '{print $2}')
1417b08fbf24SPaolo Abeni	[ -z "$count" ] && count=0
1418b08fbf24SPaolo Abeni	if [ "$count" != "$syn_ack_nr" ]; then
1419e35f885bSPaolo Abeni		# simult connections exceeding the limit with cookie enabled could go up to
1420e35f885bSPaolo Abeni		# synack validation as the conn limit can be enforced reliably only after
1421e35f885bSPaolo Abeni		# the subflow creation
1422e35f885bSPaolo Abeni		if [ "$with_cookie" = 2 ] && [ "$count" -gt "$syn_ack_nr" ] && [ "$count" -le "$syn_nr" ]; then
1423e35f885bSPaolo Abeni			echo -n "[ ok ]"
1424e35f885bSPaolo Abeni		else
1425b08fbf24SPaolo Abeni			echo "[fail] got $count JOIN[s] synack expected $syn_ack_nr"
142639aab882SMatthieu Baerts			fail_test
1427b08fbf24SPaolo Abeni			dump_stats=1
1428e35f885bSPaolo Abeni		fi
1429b08fbf24SPaolo Abeni	else
1430b08fbf24SPaolo Abeni		echo -n "[ ok ]"
1431b08fbf24SPaolo Abeni	fi
1432b08fbf24SPaolo Abeni
1433b08fbf24SPaolo Abeni	echo -n " - ack"
14344bfadd71SMatthieu Baerts	count=$(ip netns exec $ns1 nstat -as | grep MPTcpExtMPJoinAckRx | awk '{print $2}')
1435b08fbf24SPaolo Abeni	[ -z "$count" ] && count=0
1436b08fbf24SPaolo Abeni	if [ "$count" != "$ack_nr" ]; then
1437b08fbf24SPaolo Abeni		echo "[fail] got $count JOIN[s] ack expected $ack_nr"
143839aab882SMatthieu Baerts		fail_test
1439b08fbf24SPaolo Abeni		dump_stats=1
1440b08fbf24SPaolo Abeni	else
1441b08fbf24SPaolo Abeni		echo "[ ok ]"
1442b08fbf24SPaolo Abeni	fi
1443327b9a94SPaolo Abeni	[ "${dump_stats}" = 1 ] && dump_stats
1444b6e074e1SGeliang Tang	if [ $validate_checksum -eq 1 ]; then
14453c082695SGeliang Tang		chk_csum_nr $csum_ns1 $csum_ns2
144626516e10SGeliang Tang		chk_fail_nr $fail_nr $fail_nr
144726516e10SGeliang Tang		chk_rst_nr $rst_nr $rst_nr
14488bd03be3SGeliang Tang		chk_infi_nr $infi_nr $infi_nr
1449af66d3e1SGeliang Tang	fi
1450b08fbf24SPaolo Abeni}
1451b08fbf24SPaolo Abeni
14527d1e6f16SPaolo Abeni# a negative value for 'stale_max' means no upper bound:
14537d1e6f16SPaolo Abeni# for bidirectional transfer, if one peer sleep for a while
14547d1e6f16SPaolo Abeni# - as these tests do - we can have a quite high number of
14557d1e6f16SPaolo Abeni# stale/recover conversions, proportional to
14567d1e6f16SPaolo Abeni# sleep duration/ MPTCP-level RTX interval.
14577d1e6f16SPaolo Abenichk_stale_nr()
14587d1e6f16SPaolo Abeni{
14597d1e6f16SPaolo Abeni	local ns=$1
14607d1e6f16SPaolo Abeni	local stale_min=$2
14617d1e6f16SPaolo Abeni	local stale_max=$3
14627d1e6f16SPaolo Abeni	local stale_delta=$4
14637d1e6f16SPaolo Abeni	local dump_stats
14647d1e6f16SPaolo Abeni	local stale_nr
14657d1e6f16SPaolo Abeni	local recover_nr
14667d1e6f16SPaolo Abeni
14679a0a9367SGeliang Tang	printf "%-${nr_blank}s %-18s" " " "stale"
14684bfadd71SMatthieu Baerts	stale_nr=$(ip netns exec $ns nstat -as | grep MPTcpExtSubflowStale | awk '{print $2}')
14697d1e6f16SPaolo Abeni	[ -z "$stale_nr" ] && stale_nr=0
14704bfadd71SMatthieu Baerts	recover_nr=$(ip netns exec $ns nstat -as | grep MPTcpExtSubflowRecover | awk '{print $2}')
14717d1e6f16SPaolo Abeni	[ -z "$recover_nr" ] && recover_nr=0
14727d1e6f16SPaolo Abeni
14737d1e6f16SPaolo Abeni	if [ $stale_nr -lt $stale_min ] ||
1474d8d08302SMatthieu Baerts	   { [ $stale_max -gt 0 ] && [ $stale_nr -gt $stale_max ]; } ||
1475d8d08302SMatthieu Baerts	   [ $((stale_nr - recover_nr)) -ne $stale_delta ]; then
14767d1e6f16SPaolo Abeni		echo "[fail] got $stale_nr stale[s] $recover_nr recover[s], " \
14777d1e6f16SPaolo Abeni		     " expected stale in range [$stale_min..$stale_max]," \
14787d1e6f16SPaolo Abeni		     " stale-recover delta $stale_delta "
147939aab882SMatthieu Baerts		fail_test
14807d1e6f16SPaolo Abeni		dump_stats=1
14817d1e6f16SPaolo Abeni	else
14827d1e6f16SPaolo Abeni		echo "[ ok ]"
14837d1e6f16SPaolo Abeni	fi
14847d1e6f16SPaolo Abeni
14857d1e6f16SPaolo Abeni	if [ "${dump_stats}" = 1 ]; then
14867d1e6f16SPaolo Abeni		echo $ns stats
14877d1e6f16SPaolo Abeni		ip netns exec $ns ip -s link show
14887d1e6f16SPaolo Abeni		ip netns exec $ns nstat -as | grep MPTcp
14897d1e6f16SPaolo Abeni	fi
14907d1e6f16SPaolo Abeni}
14917d1e6f16SPaolo Abeni
1492be613160SGeliang Tangchk_add_nr()
1493be613160SGeliang Tang{
1494be613160SGeliang Tang	local add_nr=$1
1495be613160SGeliang Tang	local echo_nr=$2
14968a127bf6SGeliang Tang	local port_nr=${3:-0}
14978a127bf6SGeliang Tang	local syn_nr=${4:-$port_nr}
14988a127bf6SGeliang Tang	local syn_ack_nr=${5:-$port_nr}
14998a127bf6SGeliang Tang	local ack_nr=${6:-$port_nr}
15008a127bf6SGeliang Tang	local mis_syn_nr=${7:-0}
15018a127bf6SGeliang Tang	local mis_ack_nr=${8:-0}
1502be613160SGeliang Tang	local count
1503be613160SGeliang Tang	local dump_stats
15046ef84b15SPaolo Abeni	local timeout
15056ef84b15SPaolo Abeni
15064bfadd71SMatthieu Baerts	timeout=$(ip netns exec $ns1 sysctl -n net.mptcp.add_addr_timeout)
1507be613160SGeliang Tang
15089a0a9367SGeliang Tang	printf "%-${nr_blank}s %s" " " "add"
15094bfadd71SMatthieu Baerts	count=$(ip netns exec $ns2 nstat -as MPTcpExtAddAddr | grep MPTcpExtAddAddr | awk '{print $2}')
1510be613160SGeliang Tang	[ -z "$count" ] && count=0
15116ef84b15SPaolo Abeni
15126ef84b15SPaolo Abeni	# if the test configured a short timeout tolerate greater then expected
15136ef84b15SPaolo Abeni	# add addrs options, due to retransmissions
1514d8d08302SMatthieu Baerts	if [ "$count" != "$add_nr" ] && { [ "$timeout" -gt 1 ] || [ "$count" -lt "$add_nr" ]; }; then
1515be613160SGeliang Tang		echo "[fail] got $count ADD_ADDR[s] expected $add_nr"
151639aab882SMatthieu Baerts		fail_test
1517be613160SGeliang Tang		dump_stats=1
1518be613160SGeliang Tang	else
1519be613160SGeliang Tang		echo -n "[ ok ]"
1520be613160SGeliang Tang	fi
1521be613160SGeliang Tang
1522be613160SGeliang Tang	echo -n " - echo  "
15234bfadd71SMatthieu Baerts	count=$(ip netns exec $ns1 nstat -as | grep MPTcpExtEchoAdd | awk '{print $2}')
1524be613160SGeliang Tang	[ -z "$count" ] && count=0
1525be613160SGeliang Tang	if [ "$count" != "$echo_nr" ]; then
1526be613160SGeliang Tang		echo "[fail] got $count ADD_ADDR echo[s] expected $echo_nr"
152739aab882SMatthieu Baerts		fail_test
1528be613160SGeliang Tang		dump_stats=1
1529be613160SGeliang Tang	else
15308a127bf6SGeliang Tang		echo -n "[ ok ]"
15318a127bf6SGeliang Tang	fi
15328a127bf6SGeliang Tang
15338a127bf6SGeliang Tang	if [ $port_nr -gt 0 ]; then
15348a127bf6SGeliang Tang		echo -n " - pt "
15354bfadd71SMatthieu Baerts		count=$(ip netns exec $ns2 nstat -as | grep MPTcpExtPortAdd | awk '{print $2}')
15368a127bf6SGeliang Tang		[ -z "$count" ] && count=0
15378a127bf6SGeliang Tang		if [ "$count" != "$port_nr" ]; then
15388a127bf6SGeliang Tang			echo "[fail] got $count ADD_ADDR[s] with a port-number expected $port_nr"
153939aab882SMatthieu Baerts			fail_test
15408a127bf6SGeliang Tang			dump_stats=1
15418a127bf6SGeliang Tang		else
1542be613160SGeliang Tang			echo "[ ok ]"
1543be613160SGeliang Tang		fi
1544be613160SGeliang Tang
15459a0a9367SGeliang Tang		printf "%-${nr_blank}s %s" " " "syn"
15464bfadd71SMatthieu Baerts		count=$(ip netns exec $ns1 nstat -as | grep MPTcpExtMPJoinPortSynRx |
15474bfadd71SMatthieu Baerts			awk '{print $2}')
15488a127bf6SGeliang Tang		[ -z "$count" ] && count=0
15498a127bf6SGeliang Tang		if [ "$count" != "$syn_nr" ]; then
15508a127bf6SGeliang Tang			echo "[fail] got $count JOIN[s] syn with a different \
15518a127bf6SGeliang Tang				port-number expected $syn_nr"
155239aab882SMatthieu Baerts			fail_test
15538a127bf6SGeliang Tang			dump_stats=1
15548a127bf6SGeliang Tang		else
15558a127bf6SGeliang Tang			echo -n "[ ok ]"
15568a127bf6SGeliang Tang		fi
15578a127bf6SGeliang Tang
15588a127bf6SGeliang Tang		echo -n " - synack"
15594bfadd71SMatthieu Baerts		count=$(ip netns exec $ns2 nstat -as | grep MPTcpExtMPJoinPortSynAckRx |
15604bfadd71SMatthieu Baerts			awk '{print $2}')
15618a127bf6SGeliang Tang		[ -z "$count" ] && count=0
15628a127bf6SGeliang Tang		if [ "$count" != "$syn_ack_nr" ]; then
15638a127bf6SGeliang Tang			echo "[fail] got $count JOIN[s] synack with a different \
15648a127bf6SGeliang Tang				port-number expected $syn_ack_nr"
156539aab882SMatthieu Baerts			fail_test
15668a127bf6SGeliang Tang			dump_stats=1
15678a127bf6SGeliang Tang		else
15688a127bf6SGeliang Tang			echo -n "[ ok ]"
15698a127bf6SGeliang Tang		fi
15708a127bf6SGeliang Tang
15718a127bf6SGeliang Tang		echo -n " - ack"
15724bfadd71SMatthieu Baerts		count=$(ip netns exec $ns1 nstat -as | grep MPTcpExtMPJoinPortAckRx |
15734bfadd71SMatthieu Baerts			awk '{print $2}')
15748a127bf6SGeliang Tang		[ -z "$count" ] && count=0
15758a127bf6SGeliang Tang		if [ "$count" != "$ack_nr" ]; then
15768a127bf6SGeliang Tang			echo "[fail] got $count JOIN[s] ack with a different \
15778a127bf6SGeliang Tang				port-number expected $ack_nr"
157839aab882SMatthieu Baerts			fail_test
15798a127bf6SGeliang Tang			dump_stats=1
15808a127bf6SGeliang Tang		else
15818a127bf6SGeliang Tang			echo "[ ok ]"
15828a127bf6SGeliang Tang		fi
15838a127bf6SGeliang Tang
15849a0a9367SGeliang Tang		printf "%-${nr_blank}s %s" " " "syn"
15854bfadd71SMatthieu Baerts		count=$(ip netns exec $ns1 nstat -as | grep MPTcpExtMismatchPortSynRx |
15864bfadd71SMatthieu Baerts			awk '{print $2}')
15878a127bf6SGeliang Tang		[ -z "$count" ] && count=0
15888a127bf6SGeliang Tang		if [ "$count" != "$mis_syn_nr" ]; then
15898a127bf6SGeliang Tang			echo "[fail] got $count JOIN[s] syn with a mismatched \
15908a127bf6SGeliang Tang				port-number expected $mis_syn_nr"
159139aab882SMatthieu Baerts			fail_test
15928a127bf6SGeliang Tang			dump_stats=1
15938a127bf6SGeliang Tang		else
15948a127bf6SGeliang Tang			echo -n "[ ok ]"
15958a127bf6SGeliang Tang		fi
15968a127bf6SGeliang Tang
15978a127bf6SGeliang Tang		echo -n " - ack   "
15984bfadd71SMatthieu Baerts		count=$(ip netns exec $ns1 nstat -as | grep MPTcpExtMismatchPortAckRx |
15994bfadd71SMatthieu Baerts			awk '{print $2}')
16008a127bf6SGeliang Tang		[ -z "$count" ] && count=0
16018a127bf6SGeliang Tang		if [ "$count" != "$mis_ack_nr" ]; then
16028a127bf6SGeliang Tang			echo "[fail] got $count JOIN[s] ack with a mismatched \
16038a127bf6SGeliang Tang				port-number expected $mis_ack_nr"
160439aab882SMatthieu Baerts			fail_test
16058a127bf6SGeliang Tang			dump_stats=1
16068a127bf6SGeliang Tang		else
16078a127bf6SGeliang Tang			echo "[ ok ]"
16088a127bf6SGeliang Tang		fi
16098a127bf6SGeliang Tang	else
16108a127bf6SGeliang Tang		echo ""
16118a127bf6SGeliang Tang	fi
16128a127bf6SGeliang Tang
1613327b9a94SPaolo Abeni	[ "${dump_stats}" = 1 ] && dump_stats
1614be613160SGeliang Tang}
1615be613160SGeliang Tang
1616dd72b0feSGeliang Tangchk_rm_nr()
1617dd72b0feSGeliang Tang{
1618dd72b0feSGeliang Tang	local rm_addr_nr=$1
1619dd72b0feSGeliang Tang	local rm_subflow_nr=$2
16206fa0174aSPaolo Abeni	local invert
16216fa0174aSPaolo Abeni	local simult
1622dd72b0feSGeliang Tang	local count
1623dd72b0feSGeliang Tang	local dump_stats
16247d9bf018SGeliang Tang	local addr_ns=$ns1
16257d9bf018SGeliang Tang	local subflow_ns=$ns2
16267d9bf018SGeliang Tang	local extra_msg=""
16277028ba8aSGeliang Tang
16286fa0174aSPaolo Abeni	shift 2
16296fa0174aSPaolo Abeni	while [ -n "$1" ]; do
16306fa0174aSPaolo Abeni		[ "$1" = "invert" ] && invert=true
16316fa0174aSPaolo Abeni		[ "$1" = "simult" ] && simult=true
16326fa0174aSPaolo Abeni		shift
16336fa0174aSPaolo Abeni	done
16346fa0174aSPaolo Abeni
16356fa0174aSPaolo Abeni	if [ -z $invert ]; then
16366fa0174aSPaolo Abeni		addr_ns=$ns1
16376fa0174aSPaolo Abeni		subflow_ns=$ns2
16386fa0174aSPaolo Abeni	elif [ $invert = "true" ]; then
16397028ba8aSGeliang Tang		addr_ns=$ns2
16407028ba8aSGeliang Tang		subflow_ns=$ns1
16417d9bf018SGeliang Tang		extra_msg="   invert"
16427028ba8aSGeliang Tang	fi
1643dd72b0feSGeliang Tang
16449a0a9367SGeliang Tang	printf "%-${nr_blank}s %s" " " "rm "
16454bfadd71SMatthieu Baerts	count=$(ip netns exec $addr_ns nstat -as | grep MPTcpExtRmAddr | awk '{print $2}')
1646dd72b0feSGeliang Tang	[ -z "$count" ] && count=0
1647dd72b0feSGeliang Tang	if [ "$count" != "$rm_addr_nr" ]; then
1648dd72b0feSGeliang Tang		echo "[fail] got $count RM_ADDR[s] expected $rm_addr_nr"
164939aab882SMatthieu Baerts		fail_test
1650dd72b0feSGeliang Tang		dump_stats=1
1651dd72b0feSGeliang Tang	else
1652dd72b0feSGeliang Tang		echo -n "[ ok ]"
1653dd72b0feSGeliang Tang	fi
1654dd72b0feSGeliang Tang
16557d9bf018SGeliang Tang	echo -n " - rmsf  "
16564bfadd71SMatthieu Baerts	count=$(ip netns exec $subflow_ns nstat -as | grep MPTcpExtRmSubflow | awk '{print $2}')
1657dd72b0feSGeliang Tang	[ -z "$count" ] && count=0
16586fa0174aSPaolo Abeni	if [ -n "$simult" ]; then
1659d8d08302SMatthieu Baerts		local cnt suffix
1660d8d08302SMatthieu Baerts
1661d8d08302SMatthieu Baerts		cnt=$(ip netns exec $addr_ns nstat -as | grep MPTcpExtRmSubflow | awk '{print $2}')
16626fa0174aSPaolo Abeni
16636fa0174aSPaolo Abeni		# in case of simult flush, the subflow removal count on each side is
16646fa0174aSPaolo Abeni		# unreliable
16656fa0174aSPaolo Abeni		[ -z "$cnt" ] && cnt=0
16666fa0174aSPaolo Abeni		count=$((count + cnt))
16676fa0174aSPaolo Abeni		[ "$count" != "$rm_subflow_nr" ] && suffix="$count in [$rm_subflow_nr:$((rm_subflow_nr*2))]"
16686fa0174aSPaolo Abeni		if [ $count -ge "$rm_subflow_nr" ] && \
16696fa0174aSPaolo Abeni		   [ "$count" -le "$((rm_subflow_nr *2 ))" ]; then
16706fa0174aSPaolo Abeni			echo "[ ok ] $suffix"
16716fa0174aSPaolo Abeni		else
16726fa0174aSPaolo Abeni			echo "[fail] got $count RM_SUBFLOW[s] expected in range [$rm_subflow_nr:$((rm_subflow_nr*2))]"
167339aab882SMatthieu Baerts			fail_test
16746fa0174aSPaolo Abeni			dump_stats=1
16756fa0174aSPaolo Abeni		fi
16766fa0174aSPaolo Abeni		return
16776fa0174aSPaolo Abeni	fi
1678dd72b0feSGeliang Tang	if [ "$count" != "$rm_subflow_nr" ]; then
1679dd72b0feSGeliang Tang		echo "[fail] got $count RM_SUBFLOW[s] expected $rm_subflow_nr"
168039aab882SMatthieu Baerts		fail_test
1681dd72b0feSGeliang Tang		dump_stats=1
1682dd72b0feSGeliang Tang	else
16837d9bf018SGeliang Tang		echo -n "[ ok ]"
1684dd72b0feSGeliang Tang	fi
1685dd72b0feSGeliang Tang
1686327b9a94SPaolo Abeni	[ "${dump_stats}" = 1 ] && dump_stats
16877d9bf018SGeliang Tang
16887d9bf018SGeliang Tang	echo "$extra_msg"
1689dd72b0feSGeliang Tang}
1690dd72b0feSGeliang Tang
1691718eb44eSGeliang Tangchk_prio_nr()
1692718eb44eSGeliang Tang{
1693718eb44eSGeliang Tang	local mp_prio_nr_tx=$1
1694718eb44eSGeliang Tang	local mp_prio_nr_rx=$2
1695718eb44eSGeliang Tang	local count
1696718eb44eSGeliang Tang	local dump_stats
1697718eb44eSGeliang Tang
16989a0a9367SGeliang Tang	printf "%-${nr_blank}s %s" " " "ptx"
16994bfadd71SMatthieu Baerts	count=$(ip netns exec $ns1 nstat -as | grep MPTcpExtMPPrioTx | awk '{print $2}')
1700718eb44eSGeliang Tang	[ -z "$count" ] && count=0
1701718eb44eSGeliang Tang	if [ "$count" != "$mp_prio_nr_tx" ]; then
1702718eb44eSGeliang Tang		echo "[fail] got $count MP_PRIO[s] TX expected $mp_prio_nr_tx"
170339aab882SMatthieu Baerts		fail_test
1704718eb44eSGeliang Tang		dump_stats=1
1705718eb44eSGeliang Tang	else
1706718eb44eSGeliang Tang		echo -n "[ ok ]"
1707718eb44eSGeliang Tang	fi
1708718eb44eSGeliang Tang
1709718eb44eSGeliang Tang	echo -n " - prx   "
17104bfadd71SMatthieu Baerts	count=$(ip netns exec $ns1 nstat -as | grep MPTcpExtMPPrioRx | awk '{print $2}')
1711718eb44eSGeliang Tang	[ -z "$count" ] && count=0
1712718eb44eSGeliang Tang	if [ "$count" != "$mp_prio_nr_rx" ]; then
1713718eb44eSGeliang Tang		echo "[fail] got $count MP_PRIO[s] RX expected $mp_prio_nr_rx"
171439aab882SMatthieu Baerts		fail_test
1715718eb44eSGeliang Tang		dump_stats=1
1716718eb44eSGeliang Tang	else
1717718eb44eSGeliang Tang		echo "[ ok ]"
1718718eb44eSGeliang Tang	fi
1719718eb44eSGeliang Tang
1720327b9a94SPaolo Abeni	[ "${dump_stats}" = 1 ] && dump_stats
1721718eb44eSGeliang Tang}
1722718eb44eSGeliang Tang
1723e274f715SPaolo Abenichk_subflow_nr()
1724e274f715SPaolo Abeni{
1725e274f715SPaolo Abeni	local need_title="$1"
1726e274f715SPaolo Abeni	local msg="$2"
1727e274f715SPaolo Abeni	local subflow_nr=$3
1728e274f715SPaolo Abeni	local cnt1
1729e274f715SPaolo Abeni	local cnt2
1730a635a8c3SPaolo Abeni	local dump_stats
1731e274f715SPaolo Abeni
1732e274f715SPaolo Abeni	if [ -n "${need_title}" ]; then
1733e274f715SPaolo Abeni		printf "%03u %-36s %s" "${TEST_COUNT}" "${TEST_NAME}" "${msg}"
1734e274f715SPaolo Abeni	else
1735e274f715SPaolo Abeni		printf "%-${nr_blank}s %s" " " "${msg}"
1736e274f715SPaolo Abeni	fi
1737e274f715SPaolo Abeni
1738e274f715SPaolo Abeni	cnt1=$(ss -N $ns1 -tOni | grep -c token)
1739e274f715SPaolo Abeni	cnt2=$(ss -N $ns2 -tOni | grep -c token)
17400fcd72dfSMatthieu Baerts	if [ "$cnt1" != "$subflow_nr" ] || [ "$cnt2" != "$subflow_nr" ]; then
1741e274f715SPaolo Abeni		echo "[fail] got $cnt1:$cnt2 subflows expected $subflow_nr"
1742e274f715SPaolo Abeni		fail_test
1743e274f715SPaolo Abeni		dump_stats=1
1744e274f715SPaolo Abeni	else
1745e274f715SPaolo Abeni		echo "[ ok ]"
1746e274f715SPaolo Abeni	fi
1747e274f715SPaolo Abeni
1748a635a8c3SPaolo Abeni	if [ "${dump_stats}" = 1 ]; then
1749a635a8c3SPaolo Abeni		ss -N $ns1 -tOni
1750a635a8c3SPaolo Abeni		ss -N $ns1 -tOni | grep token
1751a635a8c3SPaolo Abeni		ip -n $ns1 mptcp endpoint
1752a635a8c3SPaolo Abeni		dump_stats
1753a635a8c3SPaolo Abeni	fi
1754e274f715SPaolo Abeni}
1755e274f715SPaolo Abeni
17569095ce97SGeliang Tangchk_mptcp_info()
17579095ce97SGeliang Tang{
17589095ce97SGeliang Tang	local nr_info=$1
17599095ce97SGeliang Tang	local info
17609095ce97SGeliang Tang	local cnt1
17619095ce97SGeliang Tang	local cnt2
17629095ce97SGeliang Tang	local dump_stats
17639095ce97SGeliang Tang
17649095ce97SGeliang Tang	if [[ $nr_info = "subflows_"* ]]; then
17659095ce97SGeliang Tang		info="subflows"
17669095ce97SGeliang Tang		nr_info=${nr_info:9}
17679095ce97SGeliang Tang	else
17689095ce97SGeliang Tang		echo "[fail] unsupported argument: $nr_info"
17699095ce97SGeliang Tang		fail_test
17709095ce97SGeliang Tang		return 1
17719095ce97SGeliang Tang	fi
17729095ce97SGeliang Tang
17739095ce97SGeliang Tang	printf "%-${nr_blank}s %-30s" " " "mptcp_info $info=$nr_info"
17749095ce97SGeliang Tang
17759095ce97SGeliang Tang	cnt1=$(ss -N $ns1 -inmHM | grep "$info:" |
17769095ce97SGeliang Tang		sed -n 's/.*\('"$info"':\)\([[:digit:]]*\).*$/\2/p;q')
17779095ce97SGeliang Tang	[ -z "$cnt1" ] && cnt1=0
17789095ce97SGeliang Tang	cnt2=$(ss -N $ns2 -inmHM | grep "$info:" |
17799095ce97SGeliang Tang		sed -n 's/.*\('"$info"':\)\([[:digit:]]*\).*$/\2/p;q')
17809095ce97SGeliang Tang	[ -z "$cnt2" ] && cnt2=0
17819095ce97SGeliang Tang	if [ "$cnt1" != "$nr_info" ] || [ "$cnt2" != "$nr_info" ]; then
17829095ce97SGeliang Tang		echo "[fail] got $cnt1:$cnt2 $info expected $nr_info"
17839095ce97SGeliang Tang		fail_test
17849095ce97SGeliang Tang		dump_stats=1
17859095ce97SGeliang Tang	else
17869095ce97SGeliang Tang		echo "[ ok ]"
17879095ce97SGeliang Tang	fi
17889095ce97SGeliang Tang
17899095ce97SGeliang Tang	if [ "$dump_stats" = 1 ]; then
17909095ce97SGeliang Tang		ss -N $ns1 -inmHM
17919095ce97SGeliang Tang		ss -N $ns2 -inmHM
17929095ce97SGeliang Tang		dump_stats
17939095ce97SGeliang Tang	fi
17949095ce97SGeliang Tang}
17959095ce97SGeliang Tang
17967d1e6f16SPaolo Abenichk_link_usage()
17977d1e6f16SPaolo Abeni{
17987d1e6f16SPaolo Abeni	local ns=$1
17997d1e6f16SPaolo Abeni	local link=$2
18007d1e6f16SPaolo Abeni	local out=$3
18017d1e6f16SPaolo Abeni	local expected_rate=$4
18024bfadd71SMatthieu Baerts
18034bfadd71SMatthieu Baerts	local tx_link tx_total
18044bfadd71SMatthieu Baerts	tx_link=$(ip netns exec $ns cat /sys/class/net/$link/statistics/tx_bytes)
1805d8d08302SMatthieu Baerts	tx_total=$(stat --format=%s $out)
1806d8d08302SMatthieu Baerts	local tx_rate=$((tx_link * 100 / tx_total))
18077d1e6f16SPaolo Abeni	local tolerance=5
18087d1e6f16SPaolo Abeni
18099a0a9367SGeliang Tang	printf "%-${nr_blank}s %-18s" " " "link usage"
1810d8d08302SMatthieu Baerts	if [ $tx_rate -lt $((expected_rate - tolerance)) ] || \
1811d8d08302SMatthieu Baerts	   [ $tx_rate -gt $((expected_rate + tolerance)) ]; then
18127d1e6f16SPaolo Abeni		echo "[fail] got $tx_rate% usage, expected $expected_rate%"
181339aab882SMatthieu Baerts		fail_test
18147d1e6f16SPaolo Abeni	else
18157d1e6f16SPaolo Abeni		echo "[ ok ]"
18167d1e6f16SPaolo Abeni	fi
18177d1e6f16SPaolo Abeni}
18187d1e6f16SPaolo Abeni
1819f98c2bcaSMat Martineauwait_attempt_fail()
182046e967d1SPaolo Abeni{
182146e967d1SPaolo Abeni	local timeout_ms=$((timeout_poll * 1000))
182246e967d1SPaolo Abeni	local time=0
182346e967d1SPaolo Abeni	local ns=$1
182446e967d1SPaolo Abeni
182546e967d1SPaolo Abeni	while [ $time -lt $timeout_ms ]; do
18261e777bd8SMatthieu Baerts		local cnt
18271e777bd8SMatthieu Baerts
18281e777bd8SMatthieu Baerts		cnt=$(ip netns exec $ns nstat -as TcpAttemptFails | grep TcpAttemptFails | awk '{print $2}')
182946e967d1SPaolo Abeni
183046e967d1SPaolo Abeni		[ "$cnt" = 1 ] && return 1
183146e967d1SPaolo Abeni		time=$((time + 100))
183246e967d1SPaolo Abeni		sleep 0.1
183346e967d1SPaolo Abeni	done
183446e967d1SPaolo Abeni	return 1
183546e967d1SPaolo Abeni}
183646e967d1SPaolo Abeni
18375ac1d2d6SMat Martineauset_userspace_pm()
18385ac1d2d6SMat Martineau{
18395ac1d2d6SMat Martineau	local ns=$1
18405ac1d2d6SMat Martineau
18415ac1d2d6SMat Martineau	ip netns exec $ns sysctl -q net.mptcp.pm_type=1
18425ac1d2d6SMat Martineau}
18435ac1d2d6SMat Martineau
18441002b89fSGeliang Tangsubflows_tests()
18451002b89fSGeliang Tang{
1846c7d49c03SMatthieu Baerts	if reset "no JOIN"; then
1847b08fbf24SPaolo Abeni		run_tests $ns1 $ns2 10.0.1.1
1848c7d49c03SMatthieu Baerts		chk_join_nr 0 0 0
1849ae7bd9ccSMatthieu Baerts	fi
1850b08fbf24SPaolo Abeni
18511002b89fSGeliang Tang	# subflow limited by client
1852c7d49c03SMatthieu Baerts	if reset "single subflow, limited by client"; then
185334aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 0
185434aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 0 0
185534aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
1856b08fbf24SPaolo Abeni		run_tests $ns1 $ns2 10.0.1.1
1857c7d49c03SMatthieu Baerts		chk_join_nr 0 0 0
1858ae7bd9ccSMatthieu Baerts	fi
1859b08fbf24SPaolo Abeni
18601002b89fSGeliang Tang	# subflow limited by server
1861c7d49c03SMatthieu Baerts	if reset "single subflow, limited by server"; then
186234aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 0
186334aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 0 1
186434aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
1865b08fbf24SPaolo Abeni		run_tests $ns1 $ns2 10.0.1.1
1866c7d49c03SMatthieu Baerts		chk_join_nr 1 1 0
1867ae7bd9ccSMatthieu Baerts	fi
1868b08fbf24SPaolo Abeni
1869b08fbf24SPaolo Abeni	# subflow
1870c7d49c03SMatthieu Baerts	if reset "single subflow"; then
187134aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 1
187234aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 0 1
187334aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
1874b08fbf24SPaolo Abeni		run_tests $ns1 $ns2 10.0.1.1
1875c7d49c03SMatthieu Baerts		chk_join_nr 1 1 1
1876ae7bd9ccSMatthieu Baerts	fi
1877b08fbf24SPaolo Abeni
1878b08fbf24SPaolo Abeni	# multiple subflows
1879c7d49c03SMatthieu Baerts	if reset "multiple subflows"; then
188034aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 2
188134aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 0 2
188234aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
188334aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.2.2 flags subflow
1884b08fbf24SPaolo Abeni		run_tests $ns1 $ns2 10.0.1.1
1885c7d49c03SMatthieu Baerts		chk_join_nr 2 2 2
1886ae7bd9ccSMatthieu Baerts	fi
1887b08fbf24SPaolo Abeni
188872bcbc46SPaolo Abeni	# multiple subflows limited by server
1889c7d49c03SMatthieu Baerts	if reset "multiple subflows, limited by server"; then
189034aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 1
189134aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 0 2
189234aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
189334aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.2.2 flags subflow
1894b08fbf24SPaolo Abeni		run_tests $ns1 $ns2 10.0.1.1
1895c7d49c03SMatthieu Baerts		chk_join_nr 2 2 1
1896ae7bd9ccSMatthieu Baerts	fi
1897c3eaa5f6SGeliang Tang
1898c3eaa5f6SGeliang Tang	# single subflow, dev
1899c7d49c03SMatthieu Baerts	if reset "single subflow, dev"; then
190034aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 1
190134aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 0 1
190234aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow dev ns2eth3
1903c3eaa5f6SGeliang Tang		run_tests $ns1 $ns2 10.0.1.1
1904c7d49c03SMatthieu Baerts		chk_join_nr 1 1 1
1905ae7bd9ccSMatthieu Baerts	fi
19061002b89fSGeliang Tang}
1907b08fbf24SPaolo Abeni
190846e967d1SPaolo Abenisubflows_error_tests()
190946e967d1SPaolo Abeni{
191046e967d1SPaolo Abeni	# If a single subflow is configured, and matches the MPC src
191146e967d1SPaolo Abeni	# address, no additional subflow should be created
1912c7d49c03SMatthieu Baerts	if reset "no MPC reuse with single endpoint"; then
191334aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 1
191434aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 0 1
191534aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.1.2 flags subflow
191646e967d1SPaolo Abeni		run_tests $ns1 $ns2 10.0.1.1 0 0 0 slow
1917c7d49c03SMatthieu Baerts		chk_join_nr 0 0 0
1918ae7bd9ccSMatthieu Baerts	fi
191946e967d1SPaolo Abeni
192046e967d1SPaolo Abeni	# multiple subflows, with subflow creation error
1921c7d49c03SMatthieu Baerts	if reset "multi subflows, with failing subflow"; then
192234aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 2
192334aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 0 2
192434aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
192534aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.2.2 flags subflow
19263469d72fSMatthieu Baerts		filter_tcp_from $ns1 10.0.3.2 REJECT
192746e967d1SPaolo Abeni		run_tests $ns1 $ns2 10.0.1.1 0 0 0 slow
1928c7d49c03SMatthieu Baerts		chk_join_nr 1 1 1
1929ae7bd9ccSMatthieu Baerts	fi
193046e967d1SPaolo Abeni
193146e967d1SPaolo Abeni	# multiple subflows, with subflow timeout on MPJ
1932c7d49c03SMatthieu Baerts	if reset "multi subflows, with subflow timeout"; then
193334aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 2
193434aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 0 2
193534aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
193634aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.2.2 flags subflow
19373469d72fSMatthieu Baerts		filter_tcp_from $ns1 10.0.3.2 DROP
193846e967d1SPaolo Abeni		run_tests $ns1 $ns2 10.0.1.1 0 0 0 slow
1939c7d49c03SMatthieu Baerts		chk_join_nr 1 1 1
1940ae7bd9ccSMatthieu Baerts	fi
194146e967d1SPaolo Abeni
194246e967d1SPaolo Abeni	# multiple subflows, check that the endpoint corresponding to
194346e967d1SPaolo Abeni	# closed subflow (due to reset) is not reused if additional
194446e967d1SPaolo Abeni	# subflows are added later
1945c7d49c03SMatthieu Baerts	if reset "multi subflows, fair usage on close"; then
194634aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 1
194734aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 0 1
194834aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
19493469d72fSMatthieu Baerts		filter_tcp_from $ns1 10.0.3.2 REJECT
195046e967d1SPaolo Abeni		run_tests $ns1 $ns2 10.0.1.1 0 0 0 slow &
195146e967d1SPaolo Abeni
195246e967d1SPaolo Abeni		# mpj subflow will be in TW after the reset
1953f98c2bcaSMat Martineau		wait_attempt_fail $ns2
195434aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.2.2 flags subflow
195546e967d1SPaolo Abeni		wait
195646e967d1SPaolo Abeni
195746e967d1SPaolo Abeni		# additional subflow could be created only if the PM select
195846e967d1SPaolo Abeni		# the later endpoint, skipping the already used one
1959c7d49c03SMatthieu Baerts		chk_join_nr 1 1 1
1960ae7bd9ccSMatthieu Baerts	fi
196146e967d1SPaolo Abeni}
196246e967d1SPaolo Abeni
19631002b89fSGeliang Tangsignal_address_tests()
19641002b89fSGeliang Tang{
1965b08fbf24SPaolo Abeni	# add_address, unused
1966c7d49c03SMatthieu Baerts	if reset "unused signal address"; then
196734aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
1968b08fbf24SPaolo Abeni		run_tests $ns1 $ns2 10.0.1.1
1969c7d49c03SMatthieu Baerts		chk_join_nr 0 0 0
1970be613160SGeliang Tang		chk_add_nr 1 1
1971ae7bd9ccSMatthieu Baerts	fi
1972b08fbf24SPaolo Abeni
1973b08fbf24SPaolo Abeni	# accept and use add_addr
1974c7d49c03SMatthieu Baerts	if reset "signal address"; then
197534aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 1
197634aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 1 1
197734aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
1978b08fbf24SPaolo Abeni		run_tests $ns1 $ns2 10.0.1.1
1979c7d49c03SMatthieu Baerts		chk_join_nr 1 1 1
1980be613160SGeliang Tang		chk_add_nr 1 1
1981ae7bd9ccSMatthieu Baerts	fi
1982b08fbf24SPaolo Abeni
1983b08fbf24SPaolo Abeni	# accept and use add_addr with an additional subflow
1984b08fbf24SPaolo Abeni	# note: signal address in server ns and local addresses in client ns must
1985b08fbf24SPaolo Abeni	# belong to different subnets or one of the listed local address could be
1986b08fbf24SPaolo Abeni	# used for 'add_addr' subflow
1987c7d49c03SMatthieu Baerts	if reset "subflow and signal"; then
198834aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
198934aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 2
199034aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 1 2
199134aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
1992b08fbf24SPaolo Abeni		run_tests $ns1 $ns2 10.0.1.1
1993c7d49c03SMatthieu Baerts		chk_join_nr 2 2 2
1994be613160SGeliang Tang		chk_add_nr 1 1
1995ae7bd9ccSMatthieu Baerts	fi
1996b08fbf24SPaolo Abeni
1997b08fbf24SPaolo Abeni	# accept and use add_addr with additional subflows
1998c7d49c03SMatthieu Baerts	if reset "multiple subflows and signal"; then
199934aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 3
200034aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
200134aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 1 3
200234aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
200334aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.4.2 flags subflow
2004b08fbf24SPaolo Abeni		run_tests $ns1 $ns2 10.0.1.1
2005c7d49c03SMatthieu Baerts		chk_join_nr 3 3 3
2006be613160SGeliang Tang		chk_add_nr 1 1
2007ae7bd9ccSMatthieu Baerts	fi
2008ef360019SGeliang Tang
2009ef360019SGeliang Tang	# signal addresses
2010c7d49c03SMatthieu Baerts	if reset "signal addresses"; then
201134aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 3 3
201234aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
201334aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.3.1 flags signal
201434aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.4.1 flags signal
201534aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 3 3
2016ef360019SGeliang Tang		run_tests $ns1 $ns2 10.0.1.1
2017c7d49c03SMatthieu Baerts		chk_join_nr 3 3 3
2018ef360019SGeliang Tang		chk_add_nr 3 3
2019ae7bd9ccSMatthieu Baerts	fi
2020ef360019SGeliang Tang
2021ef360019SGeliang Tang	# signal invalid addresses
2022c7d49c03SMatthieu Baerts	if reset "signal invalid addresses"; then
202334aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 3 3
202434aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.12.1 flags signal
202534aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.3.1 flags signal
202634aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.14.1 flags signal
202734aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 3 3
2028ef360019SGeliang Tang		run_tests $ns1 $ns2 10.0.1.1
2029c7d49c03SMatthieu Baerts		chk_join_nr 1 1 1
2030ef360019SGeliang Tang		chk_add_nr 3 3
2031ae7bd9ccSMatthieu Baerts	fi
203233c563adSYonglong Li
203333c563adSYonglong Li	# signal addresses race test
2034c7d49c03SMatthieu Baerts	if reset "signal addresses race test"; then
203534aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 4 4
203634aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 4 4
203734aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.1.1 flags signal
203834aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
203934aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.3.1 flags signal
204034aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.4.1 flags signal
204134aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.1.2 flags signal
204234aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.2.2 flags signal
204334aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.3.2 flags signal
204434aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.4.2 flags signal
20456ef84b15SPaolo Abeni
20466ef84b15SPaolo Abeni		# the peer could possibly miss some addr notification, allow retransmission
20476ef84b15SPaolo Abeni		ip netns exec $ns1 sysctl -q net.mptcp.add_addr_timeout=1
20486ef84b15SPaolo Abeni		run_tests $ns1 $ns2 10.0.1.1 0 0 0 slow
2049c7d49c03SMatthieu Baerts		chk_join_nr 3 3 3
205086e39e04SPaolo Abeni
205186e39e04SPaolo Abeni		# the server will not signal the address terminating
205286e39e04SPaolo Abeni		# the MPC subflow
205386e39e04SPaolo Abeni		chk_add_nr 3 3
2054ae7bd9ccSMatthieu Baerts	fi
20551002b89fSGeliang Tang}
2056b08fbf24SPaolo Abeni
20571002b89fSGeliang Tanglink_failure_tests()
20581002b89fSGeliang Tang{
20598b819a84SFlorian Westphal	# accept and use add_addr with additional subflows and link loss
2060c7d49c03SMatthieu Baerts	if reset "multiple flows, signal, link failure"; then
20617d1e6f16SPaolo Abeni		# without any b/w limit each veth could spool the packets and get
20627d1e6f16SPaolo Abeni		# them acked at xmit time, so that the corresponding subflow will
20637d1e6f16SPaolo Abeni		# have almost always no outstanding pkts, the scheduler will pick
20647d1e6f16SPaolo Abeni		# always the first subflow and we will have hard time testing
20657d1e6f16SPaolo Abeni		# active backup and link switch-over.
20667d1e6f16SPaolo Abeni		# Let's set some arbitrary (low) virtual link limits.
20677d1e6f16SPaolo Abeni		init_shapers
206834aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 3
206934aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.2.1 dev ns1eth2 flags signal
207034aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 1 3
207134aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.3.2 dev ns2eth3 flags subflow
207234aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.4.2 dev ns2eth4 flags subflow
20738b819a84SFlorian Westphal		run_tests $ns1 $ns2 10.0.1.1 1
2074c7d49c03SMatthieu Baerts		chk_join_nr 3 3 3
20758b819a84SFlorian Westphal		chk_add_nr 1 1
20767d1e6f16SPaolo Abeni		chk_stale_nr $ns2 1 5 1
2077ae7bd9ccSMatthieu Baerts	fi
20787d1e6f16SPaolo Abeni
20797d1e6f16SPaolo Abeni	# accept and use add_addr with additional subflows and link loss
20807d1e6f16SPaolo Abeni	# for bidirectional transfer
2081c7d49c03SMatthieu Baerts	if reset "multi flows, signal, bidi, link fail"; then
20827d1e6f16SPaolo Abeni		init_shapers
208334aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 3
208434aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.2.1 dev ns1eth2 flags signal
208534aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 1 3
208634aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.3.2 dev ns2eth3 flags subflow
208734aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.4.2 dev ns2eth4 flags subflow
20887d1e6f16SPaolo Abeni		run_tests $ns1 $ns2 10.0.1.1 2
2089c7d49c03SMatthieu Baerts		chk_join_nr 3 3 3
20907d1e6f16SPaolo Abeni		chk_add_nr 1 1
20917d1e6f16SPaolo Abeni		chk_stale_nr $ns2 1 -1 1
2092ae7bd9ccSMatthieu Baerts	fi
20937d1e6f16SPaolo Abeni
20947d1e6f16SPaolo Abeni	# 2 subflows plus 1 backup subflow with a lossy link, backup
20957d1e6f16SPaolo Abeni	# will never be used
2096c7d49c03SMatthieu Baerts	if reset "backup subflow unused, link failure"; then
20977d1e6f16SPaolo Abeni		init_shapers
209834aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 2
209934aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.2.1 dev ns1eth2 flags signal
210034aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 1 2
2101e59300ceSMatthieu Baerts		FAILING_LINKS="1"
210234aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.3.2 dev ns2eth3 flags subflow,backup
21037d1e6f16SPaolo Abeni		run_tests $ns1 $ns2 10.0.1.1 1
2104c7d49c03SMatthieu Baerts		chk_join_nr 2 2 2
21057d1e6f16SPaolo Abeni		chk_add_nr 1 1
21067d1e6f16SPaolo Abeni		chk_link_usage $ns2 ns2eth3 $cinsent 0
2107ae7bd9ccSMatthieu Baerts	fi
21087d1e6f16SPaolo Abeni
21097d1e6f16SPaolo Abeni	# 2 lossy links after half transfer, backup will get half of
21107d1e6f16SPaolo Abeni	# the traffic
2111c7d49c03SMatthieu Baerts	if reset "backup flow used, multi links fail"; then
21127d1e6f16SPaolo Abeni		init_shapers
211334aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 2
211434aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.2.1 dev ns1eth2 flags signal
211534aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 1 2
211634aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.3.2 dev ns2eth3 flags subflow,backup
2117e59300ceSMatthieu Baerts		FAILING_LINKS="1 2"
21187d1e6f16SPaolo Abeni		run_tests $ns1 $ns2 10.0.1.1 1
2119c7d49c03SMatthieu Baerts		chk_join_nr 2 2 2
21207d1e6f16SPaolo Abeni		chk_add_nr 1 1
21217d1e6f16SPaolo Abeni		chk_stale_nr $ns2 2 4 2
21227d1e6f16SPaolo Abeni		chk_link_usage $ns2 ns2eth3 $cinsent 50
2123ae7bd9ccSMatthieu Baerts	fi
21247d1e6f16SPaolo Abeni
21257d1e6f16SPaolo Abeni	# use a backup subflow with the first subflow on a lossy link
21267d1e6f16SPaolo Abeni	# for bidirectional transfer
2127c7d49c03SMatthieu Baerts	if reset "backup flow used, bidi, link failure"; then
21287d1e6f16SPaolo Abeni		init_shapers
212934aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 2
213034aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.2.1 dev ns1eth2 flags signal
213134aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 1 3
213234aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.3.2 dev ns2eth3 flags subflow,backup
2133e59300ceSMatthieu Baerts		FAILING_LINKS="1 2"
21347d1e6f16SPaolo Abeni		run_tests $ns1 $ns2 10.0.1.1 2
2135c7d49c03SMatthieu Baerts		chk_join_nr 2 2 2
21367d1e6f16SPaolo Abeni		chk_add_nr 1 1
21377d1e6f16SPaolo Abeni		chk_stale_nr $ns2 1 -1 2
21387d1e6f16SPaolo Abeni		chk_link_usage $ns2 ns2eth3 $cinsent 50
2139ae7bd9ccSMatthieu Baerts	fi
21401002b89fSGeliang Tang}
21418b819a84SFlorian Westphal
21421002b89fSGeliang Tangadd_addr_timeout_tests()
21431002b89fSGeliang Tang{
21448d014eaaSGeliang Tang	# add_addr timeout
2145c7d49c03SMatthieu Baerts	if reset_with_add_addr_timeout "signal address, ADD_ADDR timeout"; then
214634aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 1
214734aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 1 1
214834aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
21498b819a84SFlorian Westphal		run_tests $ns1 $ns2 10.0.1.1 0 0 0 slow
2150c7d49c03SMatthieu Baerts		chk_join_nr 1 1 1
21518d014eaaSGeliang Tang		chk_add_nr 4 0
2152ae7bd9ccSMatthieu Baerts	fi
21538d014eaaSGeliang Tang
21541002b89fSGeliang Tang	# add_addr timeout IPv6
2155c7d49c03SMatthieu Baerts	if reset_with_add_addr_timeout "signal address, ADD_ADDR6 timeout" 6; then
215634aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 1
215734aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 1 1
215834aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 dead:beef:2::1 flags signal
21591002b89fSGeliang Tang		run_tests $ns1 $ns2 dead:beef:1::1 0 0 0 slow
2160c7d49c03SMatthieu Baerts		chk_join_nr 1 1 1
21611002b89fSGeliang Tang		chk_add_nr 4 0
2162ae7bd9ccSMatthieu Baerts	fi
21638da6229bSGeliang Tang
21648da6229bSGeliang Tang	# signal addresses timeout
2165c7d49c03SMatthieu Baerts	if reset_with_add_addr_timeout "signal addresses, ADD_ADDR timeout"; then
216634aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 2 2
216734aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
216834aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.3.1 flags signal
216934aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 2 2
2170cbfafac4SGeliang Tang		run_tests $ns1 $ns2 10.0.1.1 0 0 0 speed_10
2171c7d49c03SMatthieu Baerts		chk_join_nr 2 2 2
21728da6229bSGeliang Tang		chk_add_nr 8 0
2173ae7bd9ccSMatthieu Baerts	fi
21748da6229bSGeliang Tang
21758da6229bSGeliang Tang	# signal invalid addresses timeout
2176c7d49c03SMatthieu Baerts	if reset_with_add_addr_timeout "invalid address, ADD_ADDR timeout"; then
217734aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 2 2
217834aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.12.1 flags signal
217934aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.3.1 flags signal
218034aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 2 2
2181cbfafac4SGeliang Tang		run_tests $ns1 $ns2 10.0.1.1 0 0 0 speed_10
2182c7d49c03SMatthieu Baerts		chk_join_nr 1 1 1
21838da6229bSGeliang Tang		chk_add_nr 8 0
2184ae7bd9ccSMatthieu Baerts	fi
21851002b89fSGeliang Tang}
21861002b89fSGeliang Tang
21871002b89fSGeliang Tangremove_tests()
21881002b89fSGeliang Tang{
2189dd72b0feSGeliang Tang	# single subflow, remove
2190c7d49c03SMatthieu Baerts	if reset "remove single subflow"; then
219134aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 1
219234aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 0 1
219334aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
21942e8cbf45SGeliang Tang		run_tests $ns1 $ns2 10.0.1.1 0 0 -1 slow
2195c7d49c03SMatthieu Baerts		chk_join_nr 1 1 1
2196dd72b0feSGeliang Tang		chk_rm_nr 1 1
2197ae7bd9ccSMatthieu Baerts	fi
2198dd72b0feSGeliang Tang
2199dd72b0feSGeliang Tang	# multiple subflows, remove
2200c7d49c03SMatthieu Baerts	if reset "remove multiple subflows"; then
220134aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 2
220234aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 0 2
220334aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.2.2 flags subflow
220434aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
22052e8cbf45SGeliang Tang		run_tests $ns1 $ns2 10.0.1.1 0 0 -2 slow
2206c7d49c03SMatthieu Baerts		chk_join_nr 2 2 2
2207dd72b0feSGeliang Tang		chk_rm_nr 2 2
2208ae7bd9ccSMatthieu Baerts	fi
2209dd72b0feSGeliang Tang
2210dd72b0feSGeliang Tang	# single address, remove
2211c7d49c03SMatthieu Baerts	if reset "remove single address"; then
221234aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 1
221334aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
221434aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 1 1
22152e8cbf45SGeliang Tang		run_tests $ns1 $ns2 10.0.1.1 0 -1 0 slow
2216c7d49c03SMatthieu Baerts		chk_join_nr 1 1 1
2217dd72b0feSGeliang Tang		chk_add_nr 1 1
22187028ba8aSGeliang Tang		chk_rm_nr 1 1 invert
2219ae7bd9ccSMatthieu Baerts	fi
2220dd72b0feSGeliang Tang
2221dd72b0feSGeliang Tang	# subflow and signal, remove
2222c7d49c03SMatthieu Baerts	if reset "remove subflow and signal"; then
222334aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 2
222434aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
222534aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 1 2
222634aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
22272e8cbf45SGeliang Tang		run_tests $ns1 $ns2 10.0.1.1 0 -1 -1 slow
2228c7d49c03SMatthieu Baerts		chk_join_nr 2 2 2
2229dd72b0feSGeliang Tang		chk_add_nr 1 1
2230dd72b0feSGeliang Tang		chk_rm_nr 1 1
2231ae7bd9ccSMatthieu Baerts	fi
2232dd72b0feSGeliang Tang
2233dd72b0feSGeliang Tang	# subflows and signal, remove
2234c7d49c03SMatthieu Baerts	if reset "remove subflows and signal"; then
223534aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 3
223634aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
223734aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 1 3
223834aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
223934aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.4.2 flags subflow
224022b29557SPaolo Abeni		run_tests $ns1 $ns2 10.0.1.1 0 -1 -2 speed_10
2241c7d49c03SMatthieu Baerts		chk_join_nr 3 3 3
2242dd72b0feSGeliang Tang		chk_add_nr 1 1
2243dd72b0feSGeliang Tang		chk_rm_nr 2 2
2244ae7bd9ccSMatthieu Baerts	fi
2245dd72b0feSGeliang Tang
2246ef360019SGeliang Tang	# addresses remove
2247c7d49c03SMatthieu Baerts	if reset "remove addresses"; then
224834aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 3 3
224934aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal id 250
225034aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.3.1 flags signal
225134aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.4.1 flags signal
225234aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 3 3
225322b29557SPaolo Abeni		run_tests $ns1 $ns2 10.0.1.1 0 -3 0 speed_10
2254c7d49c03SMatthieu Baerts		chk_join_nr 3 3 3
2255ef360019SGeliang Tang		chk_add_nr 3 3
2256ef360019SGeliang Tang		chk_rm_nr 3 3 invert
2257ae7bd9ccSMatthieu Baerts	fi
2258ef360019SGeliang Tang
2259ef360019SGeliang Tang	# invalid addresses remove
2260c7d49c03SMatthieu Baerts	if reset "remove invalid addresses"; then
226134aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 3 3
226234aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.12.1 flags signal
226334aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.3.1 flags signal
226434aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.14.1 flags signal
226534aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 3 3
226622b29557SPaolo Abeni		run_tests $ns1 $ns2 10.0.1.1 0 -3 0 speed_10
2267c7d49c03SMatthieu Baerts		chk_join_nr 1 1 1
2268ef360019SGeliang Tang		chk_add_nr 3 3
2269ef360019SGeliang Tang		chk_rm_nr 3 1 invert
2270ae7bd9ccSMatthieu Baerts	fi
2271ef360019SGeliang Tang
22726fe4ccdcSGeliang Tang	# subflows and signal, flush
2273c7d49c03SMatthieu Baerts	if reset "flush subflows and signal"; then
227434aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 3
227534aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
227634aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 1 3
227734aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
227834aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.4.2 flags subflow
22792e8cbf45SGeliang Tang		run_tests $ns1 $ns2 10.0.1.1 0 -8 -8 slow
2280c7d49c03SMatthieu Baerts		chk_join_nr 3 3 3
22816fe4ccdcSGeliang Tang		chk_add_nr 1 1
22826fa0174aSPaolo Abeni		chk_rm_nr 1 3 invert simult
2283ae7bd9ccSMatthieu Baerts	fi
2284d2c4333aSGeliang Tang
2285d2c4333aSGeliang Tang	# subflows flush
2286c7d49c03SMatthieu Baerts	if reset "flush subflows"; then
228734aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 3 3
228834aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 3 3
228934aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.2.2 flags subflow id 150
229034aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
229134aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.4.2 flags subflow
2292d2c4333aSGeliang Tang		run_tests $ns1 $ns2 10.0.1.1 0 -8 -8 slow
2293c7d49c03SMatthieu Baerts		chk_join_nr 3 3 3
22946fa0174aSPaolo Abeni		chk_rm_nr 0 3 simult
2295ae7bd9ccSMatthieu Baerts	fi
2296d2c4333aSGeliang Tang
2297d2c4333aSGeliang Tang	# addresses flush
2298c7d49c03SMatthieu Baerts	if reset "flush addresses"; then
229934aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 3 3
230034aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal id 250
230134aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.3.1 flags signal
230234aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.4.1 flags signal
230334aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 3 3
2304d2c4333aSGeliang Tang		run_tests $ns1 $ns2 10.0.1.1 0 -8 -8 slow
2305c7d49c03SMatthieu Baerts		chk_join_nr 3 3 3
2306d2c4333aSGeliang Tang		chk_add_nr 3 3
23076fa0174aSPaolo Abeni		chk_rm_nr 3 3 invert simult
2308ae7bd9ccSMatthieu Baerts	fi
2309ef360019SGeliang Tang
2310ef360019SGeliang Tang	# invalid addresses flush
2311c7d49c03SMatthieu Baerts	if reset "flush invalid addresses"; then
231234aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 3 3
231334aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.12.1 flags signal
231434aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.3.1 flags signal
231534aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.14.1 flags signal
231634aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 3 3
2317ef360019SGeliang Tang		run_tests $ns1 $ns2 10.0.1.1 0 -8 0 slow
2318c7d49c03SMatthieu Baerts		chk_join_nr 1 1 1
2319ef360019SGeliang Tang		chk_add_nr 3 3
2320ef360019SGeliang Tang		chk_rm_nr 3 1 invert
2321ae7bd9ccSMatthieu Baerts	fi
23225e287fe7SGeliang Tang
23235e287fe7SGeliang Tang	# remove id 0 subflow
2324c7d49c03SMatthieu Baerts	if reset "remove id 0 subflow"; then
232534aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 1
232634aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 0 1
232734aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
23285e287fe7SGeliang Tang		run_tests $ns1 $ns2 10.0.1.1 0 0 -9 slow
2329c7d49c03SMatthieu Baerts		chk_join_nr 1 1 1
23305e287fe7SGeliang Tang		chk_rm_nr 1 1
2331ae7bd9ccSMatthieu Baerts	fi
23325e287fe7SGeliang Tang
23335e287fe7SGeliang Tang	# remove id 0 address
2334c7d49c03SMatthieu Baerts	if reset "remove id 0 address"; then
233534aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 1
233634aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
233734aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 1 1
23385e287fe7SGeliang Tang		run_tests $ns1 $ns2 10.0.1.1 0 -9 0 slow
2339c7d49c03SMatthieu Baerts		chk_join_nr 1 1 1
23405e287fe7SGeliang Tang		chk_add_nr 1 1
23415e287fe7SGeliang Tang		chk_rm_nr 1 1 invert
2342ae7bd9ccSMatthieu Baerts	fi
23431002b89fSGeliang Tang}
23446fe4ccdcSGeliang Tang
23451002b89fSGeliang Tangadd_tests()
23461002b89fSGeliang Tang{
23476208fd82SGeliang Tang	# add single subflow
2348c7d49c03SMatthieu Baerts	if reset "add single subflow"; then
234934aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 1
235034aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 0 1
23516208fd82SGeliang Tang		run_tests $ns1 $ns2 10.0.1.1 0 0 1 slow
2352c7d49c03SMatthieu Baerts		chk_join_nr 1 1 1
2353ae7bd9ccSMatthieu Baerts	fi
23546208fd82SGeliang Tang
23556208fd82SGeliang Tang	# add signal address
2356c7d49c03SMatthieu Baerts	if reset "add signal address"; then
235734aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 1
235834aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 1 1
23596208fd82SGeliang Tang		run_tests $ns1 $ns2 10.0.1.1 0 1 0 slow
2360c7d49c03SMatthieu Baerts		chk_join_nr 1 1 1
23616208fd82SGeliang Tang		chk_add_nr 1 1
2362ae7bd9ccSMatthieu Baerts	fi
23636208fd82SGeliang Tang
23646208fd82SGeliang Tang	# add multiple subflows
2365c7d49c03SMatthieu Baerts	if reset "add multiple subflows"; then
236634aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 2
236734aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 0 2
23686208fd82SGeliang Tang		run_tests $ns1 $ns2 10.0.1.1 0 0 2 slow
2369c7d49c03SMatthieu Baerts		chk_join_nr 2 2 2
2370ae7bd9ccSMatthieu Baerts	fi
23716208fd82SGeliang Tang
23726208fd82SGeliang Tang	# add multiple subflows IPv6
2373c7d49c03SMatthieu Baerts	if reset "add multiple subflows IPv6"; then
237434aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 2
237534aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 0 2
23766208fd82SGeliang Tang		run_tests $ns1 $ns2 dead:beef:1::1 0 0 2 slow
2377c7d49c03SMatthieu Baerts		chk_join_nr 2 2 2
2378ae7bd9ccSMatthieu Baerts	fi
23796208fd82SGeliang Tang
23806208fd82SGeliang Tang	# add multiple addresses IPv6
2381c7d49c03SMatthieu Baerts	if reset "add multiple addresses IPv6"; then
238234aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 2
238334aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 2 2
23846208fd82SGeliang Tang		run_tests $ns1 $ns2 dead:beef:1::1 0 2 0 slow
2385c7d49c03SMatthieu Baerts		chk_join_nr 2 2 2
23866208fd82SGeliang Tang		chk_add_nr 2 2
2387ae7bd9ccSMatthieu Baerts	fi
23881002b89fSGeliang Tang}
23896208fd82SGeliang Tang
23901002b89fSGeliang Tangipv6_tests()
23911002b89fSGeliang Tang{
2392523514edSGeliang Tang	# subflow IPv6
2393c7d49c03SMatthieu Baerts	if reset "single subflow IPv6"; then
239434aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 1
239534aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 0 1
239634aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 dead:beef:3::2 dev ns2eth3 flags subflow
2397523514edSGeliang Tang		run_tests $ns1 $ns2 dead:beef:1::1 0 0 0 slow
2398c7d49c03SMatthieu Baerts		chk_join_nr 1 1 1
2399ae7bd9ccSMatthieu Baerts	fi
2400523514edSGeliang Tang
2401523514edSGeliang Tang	# add_address, unused IPv6
2402c7d49c03SMatthieu Baerts	if reset "unused signal address IPv6"; then
240334aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 dead:beef:2::1 flags signal
2404523514edSGeliang Tang		run_tests $ns1 $ns2 dead:beef:1::1 0 0 0 slow
2405c7d49c03SMatthieu Baerts		chk_join_nr 0 0 0
2406523514edSGeliang Tang		chk_add_nr 1 1
2407ae7bd9ccSMatthieu Baerts	fi
2408523514edSGeliang Tang
2409523514edSGeliang Tang	# signal address IPv6
2410c7d49c03SMatthieu Baerts	if reset "single address IPv6"; then
241134aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 1
241234aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 dead:beef:2::1 flags signal
241334aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 1 1
2414523514edSGeliang Tang		run_tests $ns1 $ns2 dead:beef:1::1 0 0 0 slow
2415c7d49c03SMatthieu Baerts		chk_join_nr 1 1 1
2416523514edSGeliang Tang		chk_add_nr 1 1
2417ae7bd9ccSMatthieu Baerts	fi
2418523514edSGeliang Tang
2419523514edSGeliang Tang	# single address IPv6, remove
2420c7d49c03SMatthieu Baerts	if reset "remove single address IPv6"; then
242134aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 1
242234aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 dead:beef:2::1 flags signal
242334aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 1 1
24242e8cbf45SGeliang Tang		run_tests $ns1 $ns2 dead:beef:1::1 0 -1 0 slow
2425c7d49c03SMatthieu Baerts		chk_join_nr 1 1 1
2426523514edSGeliang Tang		chk_add_nr 1 1
24277028ba8aSGeliang Tang		chk_rm_nr 1 1 invert
2428ae7bd9ccSMatthieu Baerts	fi
2429523514edSGeliang Tang
2430523514edSGeliang Tang	# subflow and signal IPv6, remove
2431c7d49c03SMatthieu Baerts	if reset "remove subflow and signal IPv6"; then
243234aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 2
243334aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 dead:beef:2::1 flags signal
243434aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 1 2
243534aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 dead:beef:3::2 dev ns2eth3 flags subflow
24362e8cbf45SGeliang Tang		run_tests $ns1 $ns2 dead:beef:1::1 0 -1 -1 slow
2437c7d49c03SMatthieu Baerts		chk_join_nr 2 2 2
2438523514edSGeliang Tang		chk_add_nr 1 1
2439523514edSGeliang Tang		chk_rm_nr 1 1
2440ae7bd9ccSMatthieu Baerts	fi
24411002b89fSGeliang Tang}
2442523514edSGeliang Tang
24431002b89fSGeliang Tangv4mapped_tests()
24441002b89fSGeliang Tang{
2445a6094788SGeliang Tang	# subflow IPv4-mapped to IPv4-mapped
2446c7d49c03SMatthieu Baerts	if reset "single subflow IPv4-mapped"; then
244734aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 1
244834aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 0 1
244934aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 "::ffff:10.0.3.2" flags subflow
2450a6094788SGeliang Tang		run_tests $ns1 $ns2 "::ffff:10.0.1.1"
2451c7d49c03SMatthieu Baerts		chk_join_nr 1 1 1
2452ae7bd9ccSMatthieu Baerts	fi
2453a6094788SGeliang Tang
2454a6094788SGeliang Tang	# signal address IPv4-mapped with IPv4-mapped sk
2455c7d49c03SMatthieu Baerts	if reset "signal address IPv4-mapped"; then
245634aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 1
245734aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 1 1
245834aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 "::ffff:10.0.2.1" flags signal
2459a6094788SGeliang Tang		run_tests $ns1 $ns2 "::ffff:10.0.1.1"
2460c7d49c03SMatthieu Baerts		chk_join_nr 1 1 1
2461a6094788SGeliang Tang		chk_add_nr 1 1
2462ae7bd9ccSMatthieu Baerts	fi
2463a6094788SGeliang Tang
2464a6094788SGeliang Tang	# subflow v4-map-v6
2465c7d49c03SMatthieu Baerts	if reset "single subflow v4-map-v6"; then
246634aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 1
246734aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 0 1
246834aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
2469a6094788SGeliang Tang		run_tests $ns1 $ns2 "::ffff:10.0.1.1"
2470c7d49c03SMatthieu Baerts		chk_join_nr 1 1 1
2471ae7bd9ccSMatthieu Baerts	fi
2472a6094788SGeliang Tang
2473a6094788SGeliang Tang	# signal address v4-map-v6
2474c7d49c03SMatthieu Baerts	if reset "signal address v4-map-v6"; then
247534aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 1
247634aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 1 1
247734aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
2478a6094788SGeliang Tang		run_tests $ns1 $ns2 "::ffff:10.0.1.1"
2479c7d49c03SMatthieu Baerts		chk_join_nr 1 1 1
2480a6094788SGeliang Tang		chk_add_nr 1 1
2481ae7bd9ccSMatthieu Baerts	fi
2482a6094788SGeliang Tang
2483a6094788SGeliang Tang	# subflow v6-map-v4
2484c7d49c03SMatthieu Baerts	if reset "single subflow v6-map-v4"; then
248534aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 1
248634aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 0 1
248734aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 "::ffff:10.0.3.2" flags subflow
2488a6094788SGeliang Tang		run_tests $ns1 $ns2 10.0.1.1
2489c7d49c03SMatthieu Baerts		chk_join_nr 1 1 1
2490ae7bd9ccSMatthieu Baerts	fi
2491a6094788SGeliang Tang
2492a6094788SGeliang Tang	# signal address v6-map-v4
2493c7d49c03SMatthieu Baerts	if reset "signal address v6-map-v4"; then
249434aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 1
249534aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 1 1
249634aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 "::ffff:10.0.2.1" flags signal
2497a6094788SGeliang Tang		run_tests $ns1 $ns2 10.0.1.1
2498c7d49c03SMatthieu Baerts		chk_join_nr 1 1 1
2499a6094788SGeliang Tang		chk_add_nr 1 1
2500ae7bd9ccSMatthieu Baerts	fi
2501a6094788SGeliang Tang
2502a6094788SGeliang Tang	# no subflow IPv6 to v4 address
2503c7d49c03SMatthieu Baerts	if reset "no JOIN with diff families v4-v6"; then
250434aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 1
250534aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 0 1
250634aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 dead:beef:2::2 flags subflow
2507a6094788SGeliang Tang		run_tests $ns1 $ns2 10.0.1.1
2508c7d49c03SMatthieu Baerts		chk_join_nr 0 0 0
2509ae7bd9ccSMatthieu Baerts	fi
2510a6094788SGeliang Tang
2511a6094788SGeliang Tang	# no subflow IPv6 to v4 address even if v6 has a valid v4 at the end
2512c7d49c03SMatthieu Baerts	if reset "no JOIN with diff families v4-v6-2"; then
251334aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 1
251434aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 0 1
251534aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 dead:beef:2::10.0.3.2 flags subflow
2516a6094788SGeliang Tang		run_tests $ns1 $ns2 10.0.1.1
2517c7d49c03SMatthieu Baerts		chk_join_nr 0 0 0
2518ae7bd9ccSMatthieu Baerts	fi
2519a6094788SGeliang Tang
2520a6094788SGeliang Tang	# no subflow IPv4 to v6 address, no need to slow down too then
2521c7d49c03SMatthieu Baerts	if reset "no JOIN with diff families v6-v4"; then
252234aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 1
252334aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 0 1
252434aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
2525a6094788SGeliang Tang		run_tests $ns1 $ns2 dead:beef:1::1
2526c7d49c03SMatthieu Baerts		chk_join_nr 0 0 0
2527ae7bd9ccSMatthieu Baerts	fi
25281002b89fSGeliang Tang}
2529a6094788SGeliang Tang
2530ad349374SPaolo Abenimixed_tests()
2531ad349374SPaolo Abeni{
2532ad349374SPaolo Abeni	if reset "IPv4 sockets do not use IPv6 addresses"; then
2533ad349374SPaolo Abeni		pm_nl_set_limits $ns1 0 1
2534ad349374SPaolo Abeni		pm_nl_set_limits $ns2 1 1
2535ad349374SPaolo Abeni		pm_nl_add_endpoint $ns1 dead:beef:2::1 flags signal
2536ad349374SPaolo Abeni		run_tests $ns1 $ns2 10.0.1.1 0 0 0 slow
2537ad349374SPaolo Abeni		chk_join_nr 0 0 0
2538ad349374SPaolo Abeni	fi
2539ad349374SPaolo Abeni
2540ad349374SPaolo Abeni	# Need an IPv6 mptcp socket to allow subflows of both families
2541ad349374SPaolo Abeni	if reset "simult IPv4 and IPv6 subflows"; then
2542ad349374SPaolo Abeni		pm_nl_set_limits $ns1 0 1
2543ad349374SPaolo Abeni		pm_nl_set_limits $ns2 1 1
2544ad349374SPaolo Abeni		pm_nl_add_endpoint $ns1 10.0.1.1 flags signal
2545ad349374SPaolo Abeni		run_tests $ns1 $ns2 dead:beef:2::1 0 0 0 slow
2546ad349374SPaolo Abeni		chk_join_nr 1 1 1
2547ad349374SPaolo Abeni	fi
2548ad349374SPaolo Abeni
2549ad349374SPaolo Abeni	# cross families subflows will not be created even in fullmesh mode
2550ad349374SPaolo Abeni	if reset "simult IPv4 and IPv6 subflows, fullmesh 1x1"; then
2551ad349374SPaolo Abeni		pm_nl_set_limits $ns1 0 4
2552ad349374SPaolo Abeni		pm_nl_set_limits $ns2 1 4
2553ad349374SPaolo Abeni		pm_nl_add_endpoint $ns2 dead:beef:2::2 flags subflow,fullmesh
2554ad349374SPaolo Abeni		pm_nl_add_endpoint $ns1 10.0.1.1 flags signal
2555ad349374SPaolo Abeni		run_tests $ns1 $ns2 dead:beef:2::1 0 0 0 slow
2556ad349374SPaolo Abeni		chk_join_nr 1 1 1
2557ad349374SPaolo Abeni	fi
2558ad349374SPaolo Abeni
2559ad349374SPaolo Abeni	# fullmesh still tries to create all the possibly subflows with
2560ad349374SPaolo Abeni	# matching family
2561ad349374SPaolo Abeni	if reset "simult IPv4 and IPv6 subflows, fullmesh 2x2"; then
2562ad349374SPaolo Abeni		pm_nl_set_limits $ns1 0 4
2563ad349374SPaolo Abeni		pm_nl_set_limits $ns2 2 4
2564ad349374SPaolo Abeni		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
2565ad349374SPaolo Abeni		pm_nl_add_endpoint $ns1 dead:beef:2::1 flags signal
2566ad349374SPaolo Abeni		run_tests $ns1 $ns2 dead:beef:1::1 0 0 fullmesh_1 slow
2567ad349374SPaolo Abeni		chk_join_nr 4 4 4
2568ad349374SPaolo Abeni	fi
2569ad349374SPaolo Abeni}
2570ad349374SPaolo Abeni
25711002b89fSGeliang Tangbackup_tests()
25721002b89fSGeliang Tang{
2573718eb44eSGeliang Tang	# single subflow, backup
2574c7d49c03SMatthieu Baerts	if reset "single subflow, backup"; then
257534aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 1
257634aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 0 1
257734aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow,backup
2578718eb44eSGeliang Tang		run_tests $ns1 $ns2 10.0.1.1 0 0 0 slow nobackup
2579c7d49c03SMatthieu Baerts		chk_join_nr 1 1 1
2580718eb44eSGeliang Tang		chk_prio_nr 0 1
2581ae7bd9ccSMatthieu Baerts	fi
2582718eb44eSGeliang Tang
2583718eb44eSGeliang Tang	# single address, backup
2584c7d49c03SMatthieu Baerts	if reset "single address, backup"; then
258534aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 1
258634aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
258734aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 1 1
2588718eb44eSGeliang Tang		run_tests $ns1 $ns2 10.0.1.1 0 0 0 slow backup
2589c7d49c03SMatthieu Baerts		chk_join_nr 1 1 1
2590718eb44eSGeliang Tang		chk_add_nr 1 1
2591d045b9ebSPaolo Abeni		chk_prio_nr 1 1
2592ae7bd9ccSMatthieu Baerts	fi
259333397b83SGeliang Tang
259433397b83SGeliang Tang	# single address with port, backup
2595c7d49c03SMatthieu Baerts	if reset "single address with port, backup"; then
259634aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 1
259734aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal port 10100
259834aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 1 1
259933397b83SGeliang Tang		run_tests $ns1 $ns2 10.0.1.1 0 0 0 slow backup
2600c7d49c03SMatthieu Baerts		chk_join_nr 1 1 1
260133397b83SGeliang Tang		chk_add_nr 1 1
2602d045b9ebSPaolo Abeni		chk_prio_nr 1 1
2603ae7bd9ccSMatthieu Baerts	fi
2604914f6a59SPaolo Abeni
2605914f6a59SPaolo Abeni	if reset "mpc backup"; then
2606914f6a59SPaolo Abeni		pm_nl_add_endpoint $ns2 10.0.1.2 flags subflow,backup
2607914f6a59SPaolo Abeni		run_tests $ns1 $ns2 10.0.1.1 0 0 0 slow
2608914f6a59SPaolo Abeni		chk_join_nr 0 0 0
2609914f6a59SPaolo Abeni		chk_prio_nr 0 1
2610914f6a59SPaolo Abeni	fi
2611914f6a59SPaolo Abeni
2612914f6a59SPaolo Abeni	if reset "mpc backup both sides"; then
2613914f6a59SPaolo Abeni		pm_nl_add_endpoint $ns1 10.0.1.1 flags subflow,backup
2614914f6a59SPaolo Abeni		pm_nl_add_endpoint $ns2 10.0.1.2 flags subflow,backup
2615914f6a59SPaolo Abeni		run_tests $ns1 $ns2 10.0.1.1 0 0 0 slow
2616914f6a59SPaolo Abeni		chk_join_nr 0 0 0
2617914f6a59SPaolo Abeni		chk_prio_nr 1 1
2618914f6a59SPaolo Abeni	fi
2619914f6a59SPaolo Abeni
2620914f6a59SPaolo Abeni	if reset "mpc switch to backup"; then
2621914f6a59SPaolo Abeni		pm_nl_add_endpoint $ns2 10.0.1.2 flags subflow
2622914f6a59SPaolo Abeni		run_tests $ns1 $ns2 10.0.1.1 0 0 0 slow backup
2623914f6a59SPaolo Abeni		chk_join_nr 0 0 0
2624914f6a59SPaolo Abeni		chk_prio_nr 0 1
2625914f6a59SPaolo Abeni	fi
2626914f6a59SPaolo Abeni
2627914f6a59SPaolo Abeni	if reset "mpc switch to backup both sides"; then
2628914f6a59SPaolo Abeni		pm_nl_add_endpoint $ns1 10.0.1.1 flags subflow
2629914f6a59SPaolo Abeni		pm_nl_add_endpoint $ns2 10.0.1.2 flags subflow
2630914f6a59SPaolo Abeni		run_tests $ns1 $ns2 10.0.1.1 0 0 0 slow backup
2631914f6a59SPaolo Abeni		chk_join_nr 0 0 0
2632914f6a59SPaolo Abeni		chk_prio_nr 1 1
2633914f6a59SPaolo Abeni	fi
26341002b89fSGeliang Tang}
2635718eb44eSGeliang Tang
2636178d0232SGeliang TangLISTENER_CREATED=15 #MPTCP_EVENT_LISTENER_CREATED
2637178d0232SGeliang TangLISTENER_CLOSED=16  #MPTCP_EVENT_LISTENER_CLOSED
2638178d0232SGeliang Tang
2639178d0232SGeliang TangAF_INET=2
2640178d0232SGeliang TangAF_INET6=10
2641178d0232SGeliang Tang
2642178d0232SGeliang Tangverify_listener_events()
2643178d0232SGeliang Tang{
2644178d0232SGeliang Tang	local evt=$1
2645178d0232SGeliang Tang	local e_type=$2
2646178d0232SGeliang Tang	local e_family=$3
2647178d0232SGeliang Tang	local e_saddr=$4
2648178d0232SGeliang Tang	local e_sport=$5
2649178d0232SGeliang Tang	local type
2650178d0232SGeliang Tang	local family
2651178d0232SGeliang Tang	local saddr
2652178d0232SGeliang Tang	local sport
2653178d0232SGeliang Tang
2654178d0232SGeliang Tang	if [ $e_type = $LISTENER_CREATED ]; then
2655178d0232SGeliang Tang		stdbuf -o0 -e0 printf "\t\t\t\t\t CREATE_LISTENER %s:%s"\
2656178d0232SGeliang Tang			$e_saddr $e_sport
2657178d0232SGeliang Tang	elif [ $e_type = $LISTENER_CLOSED ]; then
2658178d0232SGeliang Tang		stdbuf -o0 -e0 printf "\t\t\t\t\t CLOSE_LISTENER %s:%s "\
2659178d0232SGeliang Tang			$e_saddr $e_sport
2660178d0232SGeliang Tang	fi
2661178d0232SGeliang Tang
2662178d0232SGeliang Tang	type=$(grep "type:$e_type," $evt |
2663178d0232SGeliang Tang	       sed --unbuffered -n 's/.*\(type:\)\([[:digit:]]*\).*$/\2/p;q')
2664178d0232SGeliang Tang	family=$(grep "type:$e_type," $evt |
2665178d0232SGeliang Tang		 sed --unbuffered -n 's/.*\(family:\)\([[:digit:]]*\).*$/\2/p;q')
2666178d0232SGeliang Tang	sport=$(grep "type:$e_type," $evt |
2667178d0232SGeliang Tang		sed --unbuffered -n 's/.*\(sport:\)\([[:digit:]]*\).*$/\2/p;q')
2668178d0232SGeliang Tang	if [ $family ] && [ $family = $AF_INET6 ]; then
2669178d0232SGeliang Tang		saddr=$(grep "type:$e_type," $evt |
2670178d0232SGeliang Tang			sed --unbuffered -n 's/.*\(saddr6:\)\([0-9a-f:.]*\).*$/\2/p;q')
2671178d0232SGeliang Tang	else
2672178d0232SGeliang Tang		saddr=$(grep "type:$e_type," $evt |
2673178d0232SGeliang Tang			sed --unbuffered -n 's/.*\(saddr4:\)\([0-9.]*\).*$/\2/p;q')
2674178d0232SGeliang Tang	fi
2675178d0232SGeliang Tang
2676178d0232SGeliang Tang	if [ $type ] && [ $type = $e_type ] &&
2677178d0232SGeliang Tang	   [ $family ] && [ $family = $e_family ] &&
2678178d0232SGeliang Tang	   [ $saddr ] && [ $saddr = $e_saddr ] &&
2679178d0232SGeliang Tang	   [ $sport ] && [ $sport = $e_sport ]; then
2680178d0232SGeliang Tang		stdbuf -o0 -e0 printf "[ ok ]\n"
2681178d0232SGeliang Tang		return 0
2682178d0232SGeliang Tang	fi
2683178d0232SGeliang Tang	fail_test
2684178d0232SGeliang Tang	stdbuf -o0 -e0 printf "[fail]\n"
2685178d0232SGeliang Tang}
2686178d0232SGeliang Tang
26871002b89fSGeliang Tangadd_addr_ports_tests()
26881002b89fSGeliang Tang{
26898a127bf6SGeliang Tang	# signal address with port
2690c7d49c03SMatthieu Baerts	if reset "signal address with port"; then
269134aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 1
269234aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 1 1
269334aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal port 10100
26948a127bf6SGeliang Tang		run_tests $ns1 $ns2 10.0.1.1
2695c7d49c03SMatthieu Baerts		chk_join_nr 1 1 1
26968a127bf6SGeliang Tang		chk_add_nr 1 1 1
2697ae7bd9ccSMatthieu Baerts	fi
26988a127bf6SGeliang Tang
26998a127bf6SGeliang Tang	# subflow and signal with port
2700c7d49c03SMatthieu Baerts	if reset "subflow and signal with port"; then
270134aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal port 10100
270234aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 2
270334aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 1 2
270434aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
27058a127bf6SGeliang Tang		run_tests $ns1 $ns2 10.0.1.1
2706c7d49c03SMatthieu Baerts		chk_join_nr 2 2 2
27078a127bf6SGeliang Tang		chk_add_nr 1 1 1
2708ae7bd9ccSMatthieu Baerts	fi
27098a127bf6SGeliang Tang
27108a127bf6SGeliang Tang	# single address with port, remove
2711178d0232SGeliang Tang	# pm listener events
2712178d0232SGeliang Tang	if reset_with_events "remove single address with port"; then
271334aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 1
271434aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal port 10100
271534aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 1 1
27168a127bf6SGeliang Tang		run_tests $ns1 $ns2 10.0.1.1 0 -1 0 slow
2717c7d49c03SMatthieu Baerts		chk_join_nr 1 1 1
27188a127bf6SGeliang Tang		chk_add_nr 1 1 1
27197028ba8aSGeliang Tang		chk_rm_nr 1 1 invert
2720178d0232SGeliang Tang
2721178d0232SGeliang Tang		verify_listener_events $evts_ns1 $LISTENER_CREATED $AF_INET 10.0.2.1 10100
2722178d0232SGeliang Tang		verify_listener_events $evts_ns1 $LISTENER_CLOSED $AF_INET 10.0.2.1 10100
2723178d0232SGeliang Tang		kill_events_pids
2724ae7bd9ccSMatthieu Baerts	fi
27258a127bf6SGeliang Tang
27268a127bf6SGeliang Tang	# subflow and signal with port, remove
2727c7d49c03SMatthieu Baerts	if reset "remove subflow and signal with port"; then
272834aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 2
272934aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal port 10100
273034aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 1 2
273134aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
27328a127bf6SGeliang Tang		run_tests $ns1 $ns2 10.0.1.1 0 -1 -1 slow
2733c7d49c03SMatthieu Baerts		chk_join_nr 2 2 2
27348a127bf6SGeliang Tang		chk_add_nr 1 1 1
27358a127bf6SGeliang Tang		chk_rm_nr 1 1
2736ae7bd9ccSMatthieu Baerts	fi
27378a127bf6SGeliang Tang
27388a127bf6SGeliang Tang	# subflows and signal with port, flush
2739c7d49c03SMatthieu Baerts	if reset "flush subflows and signal with port"; then
274034aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 3
274134aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal port 10100
274234aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 1 3
274334aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
274434aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.4.2 flags subflow
2745327b9a94SPaolo Abeni		run_tests $ns1 $ns2 10.0.1.1 0 -8 -2 slow
2746c7d49c03SMatthieu Baerts		chk_join_nr 3 3 3
27478a127bf6SGeliang Tang		chk_add_nr 1 1
27486fa0174aSPaolo Abeni		chk_rm_nr 1 3 invert simult
2749ae7bd9ccSMatthieu Baerts	fi
27508a127bf6SGeliang Tang
27518a127bf6SGeliang Tang	# multiple addresses with port
2752c7d49c03SMatthieu Baerts	if reset "multiple addresses with port"; then
275334aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 2 2
275434aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal port 10100
275534aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.3.1 flags signal port 10100
275634aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 2 2
27578a127bf6SGeliang Tang		run_tests $ns1 $ns2 10.0.1.1
2758c7d49c03SMatthieu Baerts		chk_join_nr 2 2 2
27598a127bf6SGeliang Tang		chk_add_nr 2 2 2
2760ae7bd9ccSMatthieu Baerts	fi
27618a127bf6SGeliang Tang
27628a127bf6SGeliang Tang	# multiple addresses with ports
2763c7d49c03SMatthieu Baerts	if reset "multiple addresses with ports"; then
276434aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 2 2
276534aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal port 10100
276634aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.3.1 flags signal port 10101
276734aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 2 2
27688a127bf6SGeliang Tang		run_tests $ns1 $ns2 10.0.1.1
2769c7d49c03SMatthieu Baerts		chk_join_nr 2 2 2
27708a127bf6SGeliang Tang		chk_add_nr 2 2 2
2771ae7bd9ccSMatthieu Baerts	fi
27721002b89fSGeliang Tang}
27738a127bf6SGeliang Tang
27741002b89fSGeliang Tangsyncookies_tests()
27751002b89fSGeliang Tang{
277600587187SFlorian Westphal	# single subflow, syncookies
2777c7d49c03SMatthieu Baerts	if reset_with_cookies "single subflow with syn cookies"; then
277834aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 1
277934aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 0 1
278034aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
278100587187SFlorian Westphal		run_tests $ns1 $ns2 10.0.1.1
2782c7d49c03SMatthieu Baerts		chk_join_nr 1 1 1
2783ae7bd9ccSMatthieu Baerts	fi
278400587187SFlorian Westphal
278500587187SFlorian Westphal	# multiple subflows with syn cookies
2786c7d49c03SMatthieu Baerts	if reset_with_cookies "multiple subflows with syn cookies"; then
278734aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 2
278834aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 0 2
278934aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
279034aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.2.2 flags subflow
279100587187SFlorian Westphal		run_tests $ns1 $ns2 10.0.1.1
2792c7d49c03SMatthieu Baerts		chk_join_nr 2 2 2
2793ae7bd9ccSMatthieu Baerts	fi
279400587187SFlorian Westphal
279500587187SFlorian Westphal	# multiple subflows limited by server
2796c7d49c03SMatthieu Baerts	if reset_with_cookies "subflows limited by server w cookies"; then
279734aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 1
279834aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 0 2
279934aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
280034aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.2.2 flags subflow
280100587187SFlorian Westphal		run_tests $ns1 $ns2 10.0.1.1
2802c7d49c03SMatthieu Baerts		chk_join_nr 2 1 1
2803ae7bd9ccSMatthieu Baerts	fi
280400587187SFlorian Westphal
280500587187SFlorian Westphal	# test signal address with cookies
2806c7d49c03SMatthieu Baerts	if reset_with_cookies "signal address with syn cookies"; then
280734aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 1
280834aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 1 1
280934aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
281000587187SFlorian Westphal		run_tests $ns1 $ns2 10.0.1.1
2811c7d49c03SMatthieu Baerts		chk_join_nr 1 1 1
2812be613160SGeliang Tang		chk_add_nr 1 1
2813ae7bd9ccSMatthieu Baerts	fi
281400587187SFlorian Westphal
281500587187SFlorian Westphal	# test cookie with subflow and signal
2816c7d49c03SMatthieu Baerts	if reset_with_cookies "subflow and signal w cookies"; then
281734aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
281834aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 2
281934aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 1 2
282034aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
282100587187SFlorian Westphal		run_tests $ns1 $ns2 10.0.1.1
2822c7d49c03SMatthieu Baerts		chk_join_nr 2 2 2
2823be613160SGeliang Tang		chk_add_nr 1 1
2824ae7bd9ccSMatthieu Baerts	fi
282500587187SFlorian Westphal
282600587187SFlorian Westphal	# accept and use add_addr with additional subflows
2827c7d49c03SMatthieu Baerts	if reset_with_cookies "subflows and signal w. cookies"; then
282834aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 3
282934aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
283034aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 1 3
283134aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
283234aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.4.2 flags subflow
283300587187SFlorian Westphal		run_tests $ns1 $ns2 10.0.1.1
2834c7d49c03SMatthieu Baerts		chk_join_nr 3 3 3
2835be613160SGeliang Tang		chk_add_nr 1 1
2836ae7bd9ccSMatthieu Baerts	fi
28371002b89fSGeliang Tang}
28381002b89fSGeliang Tang
2839af66d3e1SGeliang Tangchecksum_tests()
2840af66d3e1SGeliang Tang{
2841af66d3e1SGeliang Tang	# checksum test 0 0
2842ae7bd9ccSMatthieu Baerts	if reset_with_checksum 0 0; then
284334aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 1
284434aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 0 1
2845af66d3e1SGeliang Tang		run_tests $ns1 $ns2 10.0.1.1
2846c7d49c03SMatthieu Baerts		chk_join_nr 0 0 0
2847ae7bd9ccSMatthieu Baerts	fi
2848af66d3e1SGeliang Tang
2849af66d3e1SGeliang Tang	# checksum test 1 1
2850ae7bd9ccSMatthieu Baerts	if reset_with_checksum 1 1; then
285134aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 1
285234aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 0 1
2853af66d3e1SGeliang Tang		run_tests $ns1 $ns2 10.0.1.1
2854c7d49c03SMatthieu Baerts		chk_join_nr 0 0 0
2855ae7bd9ccSMatthieu Baerts	fi
2856af66d3e1SGeliang Tang
2857af66d3e1SGeliang Tang	# checksum test 0 1
2858ae7bd9ccSMatthieu Baerts	if reset_with_checksum 0 1; then
285934aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 1
286034aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 0 1
2861af66d3e1SGeliang Tang		run_tests $ns1 $ns2 10.0.1.1
2862c7d49c03SMatthieu Baerts		chk_join_nr 0 0 0
2863ae7bd9ccSMatthieu Baerts	fi
2864af66d3e1SGeliang Tang
2865af66d3e1SGeliang Tang	# checksum test 1 0
2866ae7bd9ccSMatthieu Baerts	if reset_with_checksum 1 0; then
286734aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 1
286834aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 0 1
2869af66d3e1SGeliang Tang		run_tests $ns1 $ns2 10.0.1.1
2870c7d49c03SMatthieu Baerts		chk_join_nr 0 0 0
2871ae7bd9ccSMatthieu Baerts	fi
2872af66d3e1SGeliang Tang}
2873af66d3e1SGeliang Tang
28740cddb4a6SGeliang Tangdeny_join_id0_tests()
28750cddb4a6SGeliang Tang{
28760cddb4a6SGeliang Tang	# subflow allow join id0 ns1
2877c7d49c03SMatthieu Baerts	if reset_with_allow_join_id0 "single subflow allow join id0 ns1" 1 0; then
287834aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 1 1
287934aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 1 1
288034aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
28810cddb4a6SGeliang Tang		run_tests $ns1 $ns2 10.0.1.1
2882c7d49c03SMatthieu Baerts		chk_join_nr 1 1 1
2883ae7bd9ccSMatthieu Baerts	fi
28840cddb4a6SGeliang Tang
28850cddb4a6SGeliang Tang	# subflow allow join id0 ns2
2886c7d49c03SMatthieu Baerts	if reset_with_allow_join_id0 "single subflow allow join id0 ns2" 0 1; then
288734aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 1 1
288834aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 1 1
288934aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
28900cddb4a6SGeliang Tang		run_tests $ns1 $ns2 10.0.1.1
2891c7d49c03SMatthieu Baerts		chk_join_nr 0 0 0
2892ae7bd9ccSMatthieu Baerts	fi
28930cddb4a6SGeliang Tang
28940cddb4a6SGeliang Tang	# signal address allow join id0 ns1
28950cddb4a6SGeliang Tang	# ADD_ADDRs are not affected by allow_join_id0 value.
2896c7d49c03SMatthieu Baerts	if reset_with_allow_join_id0 "signal address allow join id0 ns1" 1 0; then
289734aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 1 1
289834aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 1 1
289934aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
29000cddb4a6SGeliang Tang		run_tests $ns1 $ns2 10.0.1.1
2901c7d49c03SMatthieu Baerts		chk_join_nr 1 1 1
29020cddb4a6SGeliang Tang		chk_add_nr 1 1
2903ae7bd9ccSMatthieu Baerts	fi
29040cddb4a6SGeliang Tang
29050cddb4a6SGeliang Tang	# signal address allow join id0 ns2
29060cddb4a6SGeliang Tang	# ADD_ADDRs are not affected by allow_join_id0 value.
2907c7d49c03SMatthieu Baerts	if reset_with_allow_join_id0 "signal address allow join id0 ns2" 0 1; then
290834aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 1 1
290934aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 1 1
291034aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
29110cddb4a6SGeliang Tang		run_tests $ns1 $ns2 10.0.1.1
2912c7d49c03SMatthieu Baerts		chk_join_nr 1 1 1
29130cddb4a6SGeliang Tang		chk_add_nr 1 1
2914ae7bd9ccSMatthieu Baerts	fi
29150cddb4a6SGeliang Tang
29160cddb4a6SGeliang Tang	# subflow and address allow join id0 ns1
2917c7d49c03SMatthieu Baerts	if reset_with_allow_join_id0 "subflow and address allow join id0 1" 1 0; then
291834aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 2 2
291934aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 2 2
292034aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
292134aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
29220cddb4a6SGeliang Tang		run_tests $ns1 $ns2 10.0.1.1
2923c7d49c03SMatthieu Baerts		chk_join_nr 2 2 2
2924ae7bd9ccSMatthieu Baerts	fi
29250cddb4a6SGeliang Tang
29260cddb4a6SGeliang Tang	# subflow and address allow join id0 ns2
2927c7d49c03SMatthieu Baerts	if reset_with_allow_join_id0 "subflow and address allow join id0 2" 0 1; then
292834aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 2 2
292934aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 2 2
293034aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
293134aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
29320cddb4a6SGeliang Tang		run_tests $ns1 $ns2 10.0.1.1
2933c7d49c03SMatthieu Baerts		chk_join_nr 1 1 1
2934ae7bd9ccSMatthieu Baerts	fi
29350cddb4a6SGeliang Tang}
29360cddb4a6SGeliang Tang
29374f49d633SGeliang Tangfullmesh_tests()
29384f49d633SGeliang Tang{
29394f49d633SGeliang Tang	# fullmesh 1
29404f49d633SGeliang Tang	# 2 fullmesh addrs in ns2, added before the connection,
29414f49d633SGeliang Tang	# 1 non-fullmesh addr in ns1, added during the connection.
2942c7d49c03SMatthieu Baerts	if reset "fullmesh test 2x1"; then
294334aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 4
294434aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 1 4
294534aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.2.2 flags subflow,fullmesh
294634aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow,fullmesh
29474f49d633SGeliang Tang		run_tests $ns1 $ns2 10.0.1.1 0 1 0 slow
2948c7d49c03SMatthieu Baerts		chk_join_nr 4 4 4
29494f49d633SGeliang Tang		chk_add_nr 1 1
2950ae7bd9ccSMatthieu Baerts	fi
29514f49d633SGeliang Tang
29524f49d633SGeliang Tang	# fullmesh 2
29534f49d633SGeliang Tang	# 1 non-fullmesh addr in ns1, added before the connection,
29544f49d633SGeliang Tang	# 1 fullmesh addr in ns2, added during the connection.
2955c7d49c03SMatthieu Baerts	if reset "fullmesh test 1x1"; then
295634aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 1 3
295734aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 1 3
295834aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
29594f49d633SGeliang Tang		run_tests $ns1 $ns2 10.0.1.1 0 0 fullmesh_1 slow
2960c7d49c03SMatthieu Baerts		chk_join_nr 3 3 3
29614f49d633SGeliang Tang		chk_add_nr 1 1
2962ae7bd9ccSMatthieu Baerts	fi
29634f49d633SGeliang Tang
29644f49d633SGeliang Tang	# fullmesh 3
29654f49d633SGeliang Tang	# 1 non-fullmesh addr in ns1, added before the connection,
29664f49d633SGeliang Tang	# 2 fullmesh addrs in ns2, added during the connection.
2967c7d49c03SMatthieu Baerts	if reset "fullmesh test 1x2"; then
296834aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 2 5
296934aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 1 5
297034aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
29714f49d633SGeliang Tang		run_tests $ns1 $ns2 10.0.1.1 0 0 fullmesh_2 slow
2972c7d49c03SMatthieu Baerts		chk_join_nr 5 5 5
29734f49d633SGeliang Tang		chk_add_nr 1 1
2974ae7bd9ccSMatthieu Baerts	fi
29754f49d633SGeliang Tang
29764f49d633SGeliang Tang	# fullmesh 4
29774f49d633SGeliang Tang	# 1 non-fullmesh addr in ns1, added before the connection,
29784f49d633SGeliang Tang	# 2 fullmesh addrs in ns2, added during the connection,
29794f49d633SGeliang Tang	# limit max_subflows to 4.
2980c7d49c03SMatthieu Baerts	if reset "fullmesh test 1x2, limited"; then
298134aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 2 4
298234aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 1 4
298334aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
29844f49d633SGeliang Tang		run_tests $ns1 $ns2 10.0.1.1 0 0 fullmesh_2 slow
2985c7d49c03SMatthieu Baerts		chk_join_nr 4 4 4
29864f49d633SGeliang Tang		chk_add_nr 1 1
2987ae7bd9ccSMatthieu Baerts	fi
29886a0653b9SGeliang Tang
29896a0653b9SGeliang Tang	# set fullmesh flag
2990c7d49c03SMatthieu Baerts	if reset "set fullmesh flag test"; then
299134aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 4 4
299234aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.2.1 flags subflow
299334aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 4 4
29946a0653b9SGeliang Tang		run_tests $ns1 $ns2 10.0.1.1 0 0 1 slow fullmesh
2995c7d49c03SMatthieu Baerts		chk_join_nr 2 2 2
29966a0653b9SGeliang Tang		chk_rm_nr 0 1
2997ae7bd9ccSMatthieu Baerts	fi
29986a0653b9SGeliang Tang
29996a0653b9SGeliang Tang	# set nofullmesh flag
3000c7d49c03SMatthieu Baerts	if reset "set nofullmesh flag test"; then
300134aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 4 4
300234aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.2.1 flags subflow,fullmesh
300334aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 4 4
30046a0653b9SGeliang Tang		run_tests $ns1 $ns2 10.0.1.1 0 0 fullmesh_1 slow nofullmesh
3005c7d49c03SMatthieu Baerts		chk_join_nr 2 2 2
30066a0653b9SGeliang Tang		chk_rm_nr 0 1
3007ae7bd9ccSMatthieu Baerts	fi
30086a0653b9SGeliang Tang
30096a0653b9SGeliang Tang	# set backup,fullmesh flags
3010c7d49c03SMatthieu Baerts	if reset "set backup,fullmesh flags test"; then
301134aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 4 4
301234aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.2.1 flags subflow
301334aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 4 4
30146a0653b9SGeliang Tang		run_tests $ns1 $ns2 10.0.1.1 0 0 1 slow backup,fullmesh
3015c7d49c03SMatthieu Baerts		chk_join_nr 2 2 2
30166a0653b9SGeliang Tang		chk_prio_nr 0 1
30176a0653b9SGeliang Tang		chk_rm_nr 0 1
3018ae7bd9ccSMatthieu Baerts	fi
30196a0653b9SGeliang Tang
30206a0653b9SGeliang Tang	# set nobackup,nofullmesh flags
3021c7d49c03SMatthieu Baerts	if reset "set nobackup,nofullmesh flags test"; then
302234aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 4 4
302334aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 4 4
302434aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.2.2 flags subflow,backup,fullmesh
30256a0653b9SGeliang Tang		run_tests $ns1 $ns2 10.0.1.1 0 0 0 slow nobackup,nofullmesh
3026c7d49c03SMatthieu Baerts		chk_join_nr 2 2 2
30276a0653b9SGeliang Tang		chk_prio_nr 0 1
30286a0653b9SGeliang Tang		chk_rm_nr 0 1
3029ae7bd9ccSMatthieu Baerts	fi
30304f49d633SGeliang Tang}
30314f49d633SGeliang Tang
303201542c9bSGeliang Tangfastclose_tests()
303301542c9bSGeliang Tang{
3034c7d49c03SMatthieu Baerts	if reset "fastclose test"; then
30356bf41020SPaolo Abeni		run_tests $ns1 $ns2 10.0.1.1 1024 0 fastclose_client
3036c7d49c03SMatthieu Baerts		chk_join_nr 0 0 0
303701542c9bSGeliang Tang		chk_fclose_nr 1 1
303801542c9bSGeliang Tang		chk_rst_nr 1 1 invert
3039ae7bd9ccSMatthieu Baerts	fi
30406bf41020SPaolo Abeni
30416bf41020SPaolo Abeni	if reset "fastclose server test"; then
30426bf41020SPaolo Abeni		run_tests $ns1 $ns2 10.0.1.1 1024 0 fastclose_server
30436bf41020SPaolo Abeni		chk_join_nr 0 0 0
30446bf41020SPaolo Abeni		chk_fclose_nr 1 1 invert
30456bf41020SPaolo Abeni		chk_rst_nr 1 1
30466bf41020SPaolo Abeni	fi
304701542c9bSGeliang Tang}
304801542c9bSGeliang Tang
3049b6e074e1SGeliang Tangpedit_action_pkts()
3050b6e074e1SGeliang Tang{
3051b6e074e1SGeliang Tang	tc -n $ns2 -j -s action show action pedit index 100 | \
3052c43ce398SGeliang Tang		grep "packets" | \
3053b6e074e1SGeliang Tang		sed 's/.*"packets":\([0-9]\+\),.*/\1/'
3054b6e074e1SGeliang Tang}
3055b6e074e1SGeliang Tang
3056b6e074e1SGeliang Tangfail_tests()
3057b6e074e1SGeliang Tang{
3058b6e074e1SGeliang Tang	# single subflow
3059b6e074e1SGeliang Tang	if reset_with_fail "Infinite map" 1; then
3060b6e074e1SGeliang Tang		run_tests $ns1 $ns2 10.0.1.1 128
3061b6e074e1SGeliang Tang		chk_join_nr 0 0 0 +1 +0 1 0 1 "$(pedit_action_pkts)"
30621f7d325fSGeliang Tang		chk_fail_nr 1 -1 invert
3063b6e074e1SGeliang Tang	fi
30642ba18161SGeliang Tang
30652ba18161SGeliang Tang	# multiple subflows
30662ba18161SGeliang Tang	if reset_with_fail "MP_FAIL MP_RST" 2; then
30672ba18161SGeliang Tang		tc -n $ns2 qdisc add dev ns2eth1 root netem rate 1mbit delay 5
30682ba18161SGeliang Tang		pm_nl_set_limits $ns1 0 1
30692ba18161SGeliang Tang		pm_nl_set_limits $ns2 0 1
30702ba18161SGeliang Tang		pm_nl_add_endpoint $ns2 10.0.2.2 dev ns2eth2 flags subflow
30712ba18161SGeliang Tang		run_tests $ns1 $ns2 10.0.1.1 1024
30722ba18161SGeliang Tang		chk_join_nr 1 1 1 1 0 1 1 0 "$(pedit_action_pkts)"
30732ba18161SGeliang Tang	fi
3074b6e074e1SGeliang Tang}
3075b6e074e1SGeliang Tang
30765ac1d2d6SMat Martineauuserspace_tests()
30775ac1d2d6SMat Martineau{
30785ac1d2d6SMat Martineau	# userspace pm type prevents add_addr
30795ac1d2d6SMat Martineau	if reset "userspace pm type prevents add_addr"; then
30805ac1d2d6SMat Martineau		set_userspace_pm $ns1
30815ac1d2d6SMat Martineau		pm_nl_set_limits $ns1 0 2
30825ac1d2d6SMat Martineau		pm_nl_set_limits $ns2 0 2
30835ac1d2d6SMat Martineau		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
30845ac1d2d6SMat Martineau		run_tests $ns1 $ns2 10.0.1.1
30855ac1d2d6SMat Martineau		chk_join_nr 0 0 0
30865ac1d2d6SMat Martineau		chk_add_nr 0 0
30875ac1d2d6SMat Martineau	fi
30885ac1d2d6SMat Martineau
3089b3b71bf9SMat Martineau	# userspace pm type does not echo add_addr without daemon
3090b3b71bf9SMat Martineau	if reset "userspace pm no echo w/o daemon"; then
3091b3b71bf9SMat Martineau		set_userspace_pm $ns2
3092b3b71bf9SMat Martineau		pm_nl_set_limits $ns1 0 2
3093b3b71bf9SMat Martineau		pm_nl_set_limits $ns2 0 2
3094b3b71bf9SMat Martineau		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
3095b3b71bf9SMat Martineau		run_tests $ns1 $ns2 10.0.1.1
3096b3b71bf9SMat Martineau		chk_join_nr 0 0 0
3097b3b71bf9SMat Martineau		chk_add_nr 1 0
3098b3b71bf9SMat Martineau	fi
3099b3b71bf9SMat Martineau
31005ac1d2d6SMat Martineau	# userspace pm type rejects join
31015ac1d2d6SMat Martineau	if reset "userspace pm type rejects join"; then
31025ac1d2d6SMat Martineau		set_userspace_pm $ns1
31035ac1d2d6SMat Martineau		pm_nl_set_limits $ns1 1 1
31045ac1d2d6SMat Martineau		pm_nl_set_limits $ns2 1 1
31055ac1d2d6SMat Martineau		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
31065ac1d2d6SMat Martineau		run_tests $ns1 $ns2 10.0.1.1
31075ac1d2d6SMat Martineau		chk_join_nr 1 1 0
31085ac1d2d6SMat Martineau	fi
31095ac1d2d6SMat Martineau
31105ac1d2d6SMat Martineau	# userspace pm type does not send join
31115ac1d2d6SMat Martineau	if reset "userspace pm type does not send join"; then
31125ac1d2d6SMat Martineau		set_userspace_pm $ns2
31135ac1d2d6SMat Martineau		pm_nl_set_limits $ns1 1 1
31145ac1d2d6SMat Martineau		pm_nl_set_limits $ns2 1 1
31155ac1d2d6SMat Martineau		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
31165ac1d2d6SMat Martineau		run_tests $ns1 $ns2 10.0.1.1
31175ac1d2d6SMat Martineau		chk_join_nr 0 0 0
31185ac1d2d6SMat Martineau	fi
31195ac1d2d6SMat Martineau
31205ac1d2d6SMat Martineau	# userspace pm type prevents mp_prio
31215ac1d2d6SMat Martineau	if reset "userspace pm type prevents mp_prio"; then
31225ac1d2d6SMat Martineau		set_userspace_pm $ns1
31235ac1d2d6SMat Martineau		pm_nl_set_limits $ns1 1 1
31245ac1d2d6SMat Martineau		pm_nl_set_limits $ns2 1 1
31255ac1d2d6SMat Martineau		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
31265ac1d2d6SMat Martineau		run_tests $ns1 $ns2 10.0.1.1 0 0 0 slow backup
31275ac1d2d6SMat Martineau		chk_join_nr 1 1 0
31285ac1d2d6SMat Martineau		chk_prio_nr 0 0
31295ac1d2d6SMat Martineau	fi
31305ac1d2d6SMat Martineau
31315ac1d2d6SMat Martineau	# userspace pm type prevents rm_addr
31325ac1d2d6SMat Martineau	if reset "userspace pm type prevents rm_addr"; then
31335ac1d2d6SMat Martineau		set_userspace_pm $ns1
31345ac1d2d6SMat Martineau		set_userspace_pm $ns2
31355ac1d2d6SMat Martineau		pm_nl_set_limits $ns1 0 1
31365ac1d2d6SMat Martineau		pm_nl_set_limits $ns2 0 1
31375ac1d2d6SMat Martineau		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
31385ac1d2d6SMat Martineau		run_tests $ns1 $ns2 10.0.1.1 0 0 -1 slow
31395ac1d2d6SMat Martineau		chk_join_nr 0 0 0
31405ac1d2d6SMat Martineau		chk_rm_nr 0 0
31415ac1d2d6SMat Martineau	fi
314297040cf9SGeliang Tang
314397040cf9SGeliang Tang	# userspace pm add & remove address
3144a3735625SGeliang Tang	if reset_with_events "userspace pm add & remove address"; then
314597040cf9SGeliang Tang		set_userspace_pm $ns1
314697040cf9SGeliang Tang		pm_nl_set_limits $ns2 1 1
314797040cf9SGeliang Tang		run_tests $ns1 $ns2 10.0.1.1 0 userspace_1 0 slow
314897040cf9SGeliang Tang		chk_join_nr 1 1 1
314997040cf9SGeliang Tang		chk_add_nr 1 1
315097040cf9SGeliang Tang		chk_rm_nr 1 1 invert
3151a3735625SGeliang Tang		kill_events_pids
315297040cf9SGeliang Tang	fi
31535e986ec4SGeliang Tang
31545e986ec4SGeliang Tang	# userspace pm create destroy subflow
3155a3735625SGeliang Tang	if reset_with_events "userspace pm create destroy subflow"; then
31565e986ec4SGeliang Tang		set_userspace_pm $ns2
31575e986ec4SGeliang Tang		pm_nl_set_limits $ns1 0 1
31585e986ec4SGeliang Tang		run_tests $ns1 $ns2 10.0.1.1 0 0 userspace_1 slow
31595e986ec4SGeliang Tang		chk_join_nr 1 1 1
31606c160b63SGeliang Tang		chk_rm_nr 1 1
3161a3735625SGeliang Tang		kill_events_pids
31625e986ec4SGeliang Tang	fi
31635ac1d2d6SMat Martineau}
31645ac1d2d6SMat Martineau
3165e274f715SPaolo Abeniendpoint_tests()
316669c6ce7bSPaolo Abeni{
316769c6ce7bSPaolo Abeni	# userspace pm type prevents add_addr
3168c7d49c03SMatthieu Baerts	if reset "implicit EP"; then
316969c6ce7bSPaolo Abeni		pm_nl_set_limits $ns1 2 2
317069c6ce7bSPaolo Abeni		pm_nl_set_limits $ns2 2 2
317169c6ce7bSPaolo Abeni		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
3172070d6dafSMatthieu Baerts		run_tests $ns1 $ns2 10.0.1.1 0 0 0 slow 2>/dev/null &
317369c6ce7bSPaolo Abeni
317469c6ce7bSPaolo Abeni		wait_mpj $ns1
3175c7d49c03SMatthieu Baerts		pm_nl_check_endpoint 1 "creation" \
317669c6ce7bSPaolo Abeni			$ns2 10.0.2.2 id 1 flags implicit
317769c6ce7bSPaolo Abeni
317869c6ce7bSPaolo Abeni		pm_nl_add_endpoint $ns2 10.0.2.2 id 33
3179c7d49c03SMatthieu Baerts		pm_nl_check_endpoint 0 "ID change is prevented" \
318069c6ce7bSPaolo Abeni			$ns2 10.0.2.2 id 1 flags implicit
318169c6ce7bSPaolo Abeni
318269c6ce7bSPaolo Abeni		pm_nl_add_endpoint $ns2 10.0.2.2 flags signal
3183c7d49c03SMatthieu Baerts		pm_nl_check_endpoint 0 "modif is allowed" \
318469c6ce7bSPaolo Abeni			$ns2 10.0.2.2 id 1 flags signal
3185070d6dafSMatthieu Baerts		kill_tests_wait
3186ae7bd9ccSMatthieu Baerts	fi
3187e274f715SPaolo Abeni
3188e274f715SPaolo Abeni	if reset "delete and re-add"; then
3189e274f715SPaolo Abeni		pm_nl_set_limits $ns1 1 1
3190e274f715SPaolo Abeni		pm_nl_set_limits $ns2 1 1
3191e274f715SPaolo Abeni		pm_nl_add_endpoint $ns2 10.0.2.2 id 2 dev ns2eth2 flags subflow
3192070d6dafSMatthieu Baerts		run_tests $ns1 $ns2 10.0.1.1 4 0 0 speed_20 2>/dev/null &
3193e274f715SPaolo Abeni
3194e274f715SPaolo Abeni		wait_mpj $ns2
31959095ce97SGeliang Tang		chk_subflow_nr needtitle "before delete" 2
31969095ce97SGeliang Tang		chk_mptcp_info subflows_1
31979095ce97SGeliang Tang
3198e274f715SPaolo Abeni		pm_nl_del_endpoint $ns2 2 10.0.2.2
3199e274f715SPaolo Abeni		sleep 0.5
32009095ce97SGeliang Tang		chk_subflow_nr "" "after delete" 1
32019095ce97SGeliang Tang		chk_mptcp_info subflows_0
3202e274f715SPaolo Abeni
3203e274f715SPaolo Abeni		pm_nl_add_endpoint $ns2 10.0.2.2 dev ns2eth2 flags subflow
3204e274f715SPaolo Abeni		wait_mpj $ns2
3205e274f715SPaolo Abeni		chk_subflow_nr "" "after re-add" 2
32069095ce97SGeliang Tang		chk_mptcp_info subflows_1
3207070d6dafSMatthieu Baerts		kill_tests_wait
3208e274f715SPaolo Abeni	fi
320969c6ce7bSPaolo Abeni}
321069c6ce7bSPaolo Abeni
321122514d52SMatthieu Baerts# [$1: error message]
32121002b89fSGeliang Tangusage()
32131002b89fSGeliang Tang{
321422514d52SMatthieu Baerts	if [ -n "${1}" ]; then
321522514d52SMatthieu Baerts		echo "${1}"
321622514d52SMatthieu Baerts		ret=1
321722514d52SMatthieu Baerts	fi
321822514d52SMatthieu Baerts
32191002b89fSGeliang Tang	echo "mptcp_join usage:"
32203afd0280SMatthieu Baerts
32213afd0280SMatthieu Baerts	local key
32223afd0280SMatthieu Baerts	for key in "${!all_tests[@]}"; do
32233afd0280SMatthieu Baerts		echo "  -${key} ${all_tests[${key}]}"
32243afd0280SMatthieu Baerts	done
32253afd0280SMatthieu Baerts
3226a673321aSMat Martineau	echo "  -c capture pcap files"
3227af66d3e1SGeliang Tang	echo "  -C enable data checksum"
3228621bd393SGeliang Tang	echo "  -i use ip mptcp"
32291002b89fSGeliang Tang	echo "  -h help"
323022514d52SMatthieu Baerts
3231c7d49c03SMatthieu Baerts	echo "[test ids|names]"
3232ae7bd9ccSMatthieu Baerts
323322514d52SMatthieu Baerts	exit ${ret}
32341002b89fSGeliang Tang}
32351002b89fSGeliang Tang
3236a673321aSMat Martineau
32373afd0280SMatthieu Baerts# Use a "simple" array to force an specific order we cannot have with an associative one
32383afd0280SMatthieu Baertsall_tests_sorted=(
32393afd0280SMatthieu Baerts	f@subflows_tests
32403afd0280SMatthieu Baerts	e@subflows_error_tests
32413afd0280SMatthieu Baerts	s@signal_address_tests
32423afd0280SMatthieu Baerts	l@link_failure_tests
32433afd0280SMatthieu Baerts	t@add_addr_timeout_tests
32443afd0280SMatthieu Baerts	r@remove_tests
32453afd0280SMatthieu Baerts	a@add_tests
32463afd0280SMatthieu Baerts	6@ipv6_tests
32473afd0280SMatthieu Baerts	4@v4mapped_tests
3248ad349374SPaolo Abeni	M@mixed_tests
32493afd0280SMatthieu Baerts	b@backup_tests
32503afd0280SMatthieu Baerts	p@add_addr_ports_tests
32513afd0280SMatthieu Baerts	k@syncookies_tests
32523afd0280SMatthieu Baerts	S@checksum_tests
32533afd0280SMatthieu Baerts	d@deny_join_id0_tests
32543afd0280SMatthieu Baerts	m@fullmesh_tests
32553afd0280SMatthieu Baerts	z@fastclose_tests
3256b6e074e1SGeliang Tang	F@fail_tests
32575ac1d2d6SMat Martineau	u@userspace_tests
3258e274f715SPaolo Abeni	I@endpoint_tests
32593afd0280SMatthieu Baerts)
32603afd0280SMatthieu Baerts
32613afd0280SMatthieu Baertsall_tests_args=""
32623afd0280SMatthieu Baertsall_tests_names=()
32633afd0280SMatthieu Baertsfor subtests in "${all_tests_sorted[@]}"; do
32643afd0280SMatthieu Baerts	key="${subtests%@*}"
32653afd0280SMatthieu Baerts	value="${subtests#*@}"
32663afd0280SMatthieu Baerts
32673afd0280SMatthieu Baerts	all_tests_args+="${key}"
32683afd0280SMatthieu Baerts	all_tests_names+=("${value}")
32693afd0280SMatthieu Baerts	all_tests[${key}]="${value}"
32703afd0280SMatthieu Baertsdone
32713afd0280SMatthieu Baerts
3272826d7bdcSMatthieu Baertstests=()
32733afd0280SMatthieu Baertswhile getopts "${all_tests_args}cCih" opt; do
32741002b89fSGeliang Tang	case $opt in
32753afd0280SMatthieu Baerts		["${all_tests_args}"])
32763afd0280SMatthieu Baerts			tests+=("${all_tests[${opt}]}")
327769c6ce7bSPaolo Abeni			;;
3278a673321aSMat Martineau		c)
3279826d7bdcSMatthieu Baerts			capture=1
3280a673321aSMat Martineau			;;
3281af66d3e1SGeliang Tang		C)
3282826d7bdcSMatthieu Baerts			checksum=1
3283af66d3e1SGeliang Tang			;;
3284621bd393SGeliang Tang		i)
3285826d7bdcSMatthieu Baerts			ip_mptcp=1
3286621bd393SGeliang Tang			;;
328722514d52SMatthieu Baerts		h)
32881002b89fSGeliang Tang			usage
32891002b89fSGeliang Tang			;;
329022514d52SMatthieu Baerts		*)
329122514d52SMatthieu Baerts			usage "Unknown option: -${opt}"
329222514d52SMatthieu Baerts			;;
32931002b89fSGeliang Tang	esac
32941002b89fSGeliang Tangdone
329500587187SFlorian Westphal
3296ae7bd9ccSMatthieu Baertsshift $((OPTIND - 1))
3297ae7bd9ccSMatthieu Baerts
3298ae7bd9ccSMatthieu Baertsfor arg in "${@}"; do
3299ae7bd9ccSMatthieu Baerts	if [[ "${arg}" =~ ^[0-9]+$ ]]; then
3300c7d49c03SMatthieu Baerts		only_tests_ids+=("${arg}")
3301ae7bd9ccSMatthieu Baerts	else
3302c7d49c03SMatthieu Baerts		only_tests_names+=("${arg}")
3303ae7bd9ccSMatthieu Baerts	fi
3304ae7bd9ccSMatthieu Baertsdone
3305ae7bd9ccSMatthieu Baerts
3306826d7bdcSMatthieu Baertsif [ ${#tests[@]} -eq 0 ]; then
33073afd0280SMatthieu Baerts	tests=("${all_tests_names[@]}")
33083afd0280SMatthieu Baertsfi
33093afd0280SMatthieu Baerts
3310826d7bdcSMatthieu Baertsfor subtests in "${tests[@]}"; do
3311826d7bdcSMatthieu Baerts	"${subtests}"
3312826d7bdcSMatthieu Baertsdone
3313826d7bdcSMatthieu Baerts
331439aab882SMatthieu Baertsif [ ${ret} -ne 0 ]; then
331539aab882SMatthieu Baerts	echo
331639aab882SMatthieu Baerts	echo "${#failed_tests[@]} failure(s) has(ve) been detected:"
331739aab882SMatthieu Baerts	for i in $(get_failed_tests_ids); do
331839aab882SMatthieu Baerts		echo -e "\t- ${i}: ${failed_tests[${i}]}"
331939aab882SMatthieu Baerts	done
332039aab882SMatthieu Baerts	echo
332139aab882SMatthieu Baertsfi
332239aab882SMatthieu Baerts
3323b08fbf24SPaolo Abeniexit $ret
3324