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
280c4cd3f8SMatthieu Baertsiptables="iptables"
290c4cd3f8SMatthieu 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
427f117cd3SMatthieu Baertslast_test_failed=0
437f117cd3SMatthieu Baertslast_test_skipped=0
447f117cd3SMatthieu Baertslast_test_ignored=1
45b08fbf24SPaolo Abeni
463afd0280SMatthieu Baertsdeclare -A all_tests
47c7d49c03SMatthieu Baertsdeclare -a only_tests_ids
48c7d49c03SMatthieu Baertsdeclare -a only_tests_names
4939aab882SMatthieu Baertsdeclare -A failed_tests
50b08fbf24SPaolo AbeniTEST_COUNT=0
51c7d49c03SMatthieu BaertsTEST_NAME=""
5203668c65SMatthieu Baertsnr_blank=6
53b08fbf24SPaolo Abeni
54662aa22dSGeliang Tang# These var are used only in some tests, make sure they are not already set
55662aa22dSGeliang Tangunset FAILING_LINKS
56662aa22dSGeliang Tangunset test_linkfail
57662aa22dSGeliang Tangunset addr_nr_ns1
58662aa22dSGeliang Tangunset addr_nr_ns2
59662aa22dSGeliang Tangunset sflags
60080b7f57SGeliang Tangunset fastclose
614aadde08SGeliang Tangunset fullmesh
62e571fb09SGeliang Tangunset speed
63e59300ceSMatthieu Baerts
648d014eaaSGeliang Tang# generated using "nfbpf_compile '(ip && (ip[54] & 0xf0) == 0x30) ||
658d014eaaSGeliang Tang#				  (ip6 && (ip6[74] & 0xf0) == 0x30)'"
668d014eaaSGeliang TangCBPF_MPTCP_SUBOPTION_ADD_ADDR="14,
678d014eaaSGeliang Tang			       48 0 0 0,
688d014eaaSGeliang Tang			       84 0 0 240,
698d014eaaSGeliang Tang			       21 0 3 64,
708d014eaaSGeliang Tang			       48 0 0 54,
718d014eaaSGeliang Tang			       84 0 0 240,
728d014eaaSGeliang Tang			       21 6 7 48,
738d014eaaSGeliang Tang			       48 0 0 0,
748d014eaaSGeliang Tang			       84 0 0 240,
758d014eaaSGeliang Tang			       21 0 4 96,
768d014eaaSGeliang Tang			       48 0 0 74,
778d014eaaSGeliang Tang			       84 0 0 240,
788d014eaaSGeliang Tang			       21 0 1 48,
798d014eaaSGeliang Tang			       6 0 0 65535,
808d014eaaSGeliang Tang			       6 0 0 0"
818d014eaaSGeliang Tang
8293827ad5SMatthieu Baertsinit_partial()
83b08fbf24SPaolo Abeni{
84b08fbf24SPaolo Abeni	capout=$(mktemp)
85b08fbf24SPaolo Abeni
86787eb1e4SMatthieu Baerts	local sec rndh
87787eb1e4SMatthieu Baerts	sec=$(date +%s)
88787eb1e4SMatthieu Baerts	rndh=$(printf %x $sec)-$(mktemp -u XXXXXX)
89b08fbf24SPaolo Abeni
90b08fbf24SPaolo Abeni	ns1="ns1-$rndh"
91b08fbf24SPaolo Abeni	ns2="ns2-$rndh"
92b08fbf24SPaolo Abeni
931e777bd8SMatthieu Baerts	local netns
94b08fbf24SPaolo Abeni	for netns in "$ns1" "$ns2"; do
95b08fbf24SPaolo Abeni		ip netns add $netns || exit $ksft_skip
96b08fbf24SPaolo Abeni		ip -net $netns link set lo up
97b08fbf24SPaolo Abeni		ip netns exec $netns sysctl -q net.mptcp.enabled=1
98f2b492b0SMatthieu Baerts		ip netns exec $netns sysctl -q net.mptcp.pm_type=0 2>/dev/null || true
99b08fbf24SPaolo Abeni		ip netns exec $netns sysctl -q net.ipv4.conf.all.rp_filter=0
100b08fbf24SPaolo Abeni		ip netns exec $netns sysctl -q net.ipv4.conf.default.rp_filter=0
101af66d3e1SGeliang Tang		if [ $checksum -eq 1 ]; then
102af66d3e1SGeliang Tang			ip netns exec $netns sysctl -q net.mptcp.checksum_enabled=1
103af66d3e1SGeliang Tang		fi
104b08fbf24SPaolo Abeni	done
105b08fbf24SPaolo Abeni
1068117dac3SGeliang Tang	check_invert=0
1073c082695SGeliang Tang	validate_checksum=$checksum
1088117dac3SGeliang Tang
109b08fbf24SPaolo Abeni	#  ns1         ns2
110b08fbf24SPaolo Abeni	# ns1eth1    ns2eth1
111b08fbf24SPaolo Abeni	# ns1eth2    ns2eth2
112b08fbf24SPaolo Abeni	# ns1eth3    ns2eth3
113b08fbf24SPaolo Abeni	# ns1eth4    ns2eth4
114b08fbf24SPaolo Abeni
1151e777bd8SMatthieu Baerts	local i
1164bfadd71SMatthieu Baerts	for i in $(seq 1 4); do
117b08fbf24SPaolo Abeni		ip link add ns1eth$i netns "$ns1" type veth peer name ns2eth$i netns "$ns2"
118b08fbf24SPaolo Abeni		ip -net "$ns1" addr add 10.0.$i.1/24 dev ns1eth$i
119b08fbf24SPaolo Abeni		ip -net "$ns1" addr add dead:beef:$i::1/64 dev ns1eth$i nodad
120b08fbf24SPaolo Abeni		ip -net "$ns1" link set ns1eth$i up
121b08fbf24SPaolo Abeni
122b08fbf24SPaolo Abeni		ip -net "$ns2" addr add 10.0.$i.2/24 dev ns2eth$i
123b08fbf24SPaolo Abeni		ip -net "$ns2" addr add dead:beef:$i::2/64 dev ns2eth$i nodad
124b08fbf24SPaolo Abeni		ip -net "$ns2" link set ns2eth$i up
125b08fbf24SPaolo Abeni
126b08fbf24SPaolo Abeni		# let $ns2 reach any $ns1 address from any interface
127b08fbf24SPaolo Abeni		ip -net "$ns2" route add default via 10.0.$i.1 dev ns2eth$i metric 10$i
1289846921dSPaolo Abeni		ip -net "$ns2" route add default via dead:beef:$i::1 dev ns2eth$i metric 10$i
129b08fbf24SPaolo Abeni	done
130b08fbf24SPaolo Abeni}
131b08fbf24SPaolo Abeni
1327d1e6f16SPaolo Abeniinit_shapers()
1337d1e6f16SPaolo Abeni{
1341e777bd8SMatthieu Baerts	local i
1354bfadd71SMatthieu Baerts	for i in $(seq 1 4); do
1367d1e6f16SPaolo Abeni		tc -n $ns1 qdisc add dev ns1eth$i root netem rate 20mbit delay 1
1377d1e6f16SPaolo Abeni		tc -n $ns2 qdisc add dev ns2eth$i root netem rate 20mbit delay 1
1387d1e6f16SPaolo Abeni	done
1397d1e6f16SPaolo Abeni}
1407d1e6f16SPaolo Abeni
141b08fbf24SPaolo Abenicleanup_partial()
142b08fbf24SPaolo Abeni{
143b08fbf24SPaolo Abeni	rm -f "$capout"
144b08fbf24SPaolo Abeni
1451e777bd8SMatthieu Baerts	local netns
146b08fbf24SPaolo Abeni	for netns in "$ns1" "$ns2"; do
147b08fbf24SPaolo Abeni		ip netns del $netns
148c2a55e8fSMatthieu Baerts		rm -f /tmp/$netns.{nstat,out}
149b08fbf24SPaolo Abeni	done
150b08fbf24SPaolo Abeni}
151b08fbf24SPaolo Abeni
15287154755SMatthieu Baertscheck_tools()
15387154755SMatthieu Baerts{
154715c78a8SMatthieu Baerts	mptcp_lib_check_mptcp
155cdb50525SMatthieu Baerts	mptcp_lib_check_kallsyms
156715c78a8SMatthieu Baerts
15787154755SMatthieu Baerts	if ! ip -Version &> /dev/null; then
15887154755SMatthieu Baerts		echo "SKIP: Could not run test without ip tool"
15987154755SMatthieu Baerts		exit $ksft_skip
16087154755SMatthieu Baerts	fi
16187154755SMatthieu Baerts
1620c4cd3f8SMatthieu Baerts	# Use the legacy version if available to support old kernel versions
1630c4cd3f8SMatthieu Baerts	if iptables-legacy -V &> /dev/null; then
1640c4cd3f8SMatthieu Baerts		iptables="iptables-legacy"
1650c4cd3f8SMatthieu Baerts		ip6tables="ip6tables-legacy"
1660c4cd3f8SMatthieu Baerts	elif ! iptables -V &> /dev/null; then
16787154755SMatthieu Baerts		echo "SKIP: Could not run all tests without iptables tool"
16887154755SMatthieu Baerts		exit $ksft_skip
169016e7ba4SMatthieu Baerts	elif ! ip6tables -V &> /dev/null; then
17087154755SMatthieu Baerts		echo "SKIP: Could not run all tests without ip6tables tool"
17187154755SMatthieu Baerts		exit $ksft_skip
17287154755SMatthieu Baerts	fi
17387154755SMatthieu Baerts}
17487154755SMatthieu Baerts
17593827ad5SMatthieu Baertsinit() {
17693827ad5SMatthieu Baerts	init=1
17793827ad5SMatthieu Baerts
17887154755SMatthieu Baerts	check_tools
17987154755SMatthieu Baerts
18093827ad5SMatthieu Baerts	sin=$(mktemp)
18193827ad5SMatthieu Baerts	sout=$(mktemp)
18293827ad5SMatthieu Baerts	cin=$(mktemp)
18393827ad5SMatthieu Baerts	cinsent=$(mktemp)
18493827ad5SMatthieu Baerts	cout=$(mktemp)
185a3735625SGeliang Tang	evts_ns1=$(mktemp)
186a3735625SGeliang Tang	evts_ns2=$(mktemp)
18793827ad5SMatthieu Baerts
18893827ad5SMatthieu Baerts	trap cleanup EXIT
18993827ad5SMatthieu Baerts
19003668c65SMatthieu Baerts	make_file "$cin" "client" 1 >/dev/null
19103668c65SMatthieu Baerts	make_file "$sin" "server" 1 >/dev/null
19293827ad5SMatthieu Baerts}
19393827ad5SMatthieu Baerts
194b08fbf24SPaolo Abenicleanup()
195b08fbf24SPaolo Abeni{
1967d1e6f16SPaolo Abeni	rm -f "$cin" "$cout" "$sinfail"
1977d1e6f16SPaolo Abeni	rm -f "$sin" "$sout" "$cinsent" "$cinfail"
198d328fe87SMatthieu Baerts	rm -f "$tmpfile"
199a3735625SGeliang Tang	rm -rf $evts_ns1 $evts_ns2
200b08fbf24SPaolo Abeni	cleanup_partial
201b08fbf24SPaolo Abeni}
202b08fbf24SPaolo Abeni
203cdb50525SMatthieu Baertsprint_title()
204cdb50525SMatthieu Baerts{
20503668c65SMatthieu Baerts	printf "%03u %s\n" "${TEST_COUNT}" "${TEST_NAME}"
20603668c65SMatthieu Baerts}
20703668c65SMatthieu Baerts
20803668c65SMatthieu Baertsprint_check()
20903668c65SMatthieu Baerts{
21003668c65SMatthieu Baerts	printf "%-${nr_blank}s%-36s" " " "${*}"
21103668c65SMatthieu Baerts}
21203668c65SMatthieu Baerts
21303668c65SMatthieu Baertsprint_info()
21403668c65SMatthieu Baerts{
21503668c65SMatthieu Baerts	# It can be empty, no need to print anything then
21603668c65SMatthieu Baerts	[ -z "${1}" ] && return
21703668c65SMatthieu Baerts
2189466df1aSMatthieu Baerts	mptcp_lib_print_info "      Info: ${*}"
21903668c65SMatthieu Baerts}
22003668c65SMatthieu Baerts
22103668c65SMatthieu Baertsprint_ok()
22203668c65SMatthieu Baerts{
2239466df1aSMatthieu Baerts	mptcp_lib_print_ok "[ ok ]${1:+ ${*}}"
22403668c65SMatthieu Baerts}
22503668c65SMatthieu Baerts
22603668c65SMatthieu Baertsprint_fail()
22703668c65SMatthieu Baerts{
2289466df1aSMatthieu Baerts	mptcp_lib_print_err "[fail]${1:+ ${*}}"
22903668c65SMatthieu Baerts}
23003668c65SMatthieu Baerts
23103668c65SMatthieu Baertsprint_skip()
23203668c65SMatthieu Baerts{
2339466df1aSMatthieu Baerts	mptcp_lib_print_warn "[skip]${1:+ ${*}}"
234cdb50525SMatthieu Baerts}
235cdb50525SMatthieu Baerts
236cdb50525SMatthieu Baerts# [ $1: fail msg ]
237cdb50525SMatthieu Baertsmark_as_skipped()
238cdb50525SMatthieu Baerts{
239cdb50525SMatthieu Baerts	local msg="${1:-"Feature not supported"}"
240cdb50525SMatthieu Baerts
241cdb50525SMatthieu Baerts	mptcp_lib_fail_if_expected_feature "${msg}"
242cdb50525SMatthieu Baerts
24303668c65SMatthieu Baerts	print_check "${msg}"
24403668c65SMatthieu Baerts	print_skip
2457f117cd3SMatthieu Baerts
2467f117cd3SMatthieu Baerts	last_test_skipped=1
247cdb50525SMatthieu Baerts}
248cdb50525SMatthieu Baerts
249cdb50525SMatthieu Baerts# $@: condition
250cdb50525SMatthieu Baertscontinue_if()
251cdb50525SMatthieu Baerts{
252cdb50525SMatthieu Baerts	if ! "${@}"; then
253cdb50525SMatthieu Baerts		mark_as_skipped
254cdb50525SMatthieu Baerts		return 1
255cdb50525SMatthieu Baerts	fi
256cdb50525SMatthieu Baerts}
257cdb50525SMatthieu Baerts
258ae7bd9ccSMatthieu Baertsskip_test()
259ae7bd9ccSMatthieu Baerts{
260c7d49c03SMatthieu Baerts	if [ "${#only_tests_ids[@]}" -eq 0 ] && [ "${#only_tests_names[@]}" -eq 0 ]; then
261ae7bd9ccSMatthieu Baerts		return 1
262ae7bd9ccSMatthieu Baerts	fi
263ae7bd9ccSMatthieu Baerts
264ae7bd9ccSMatthieu Baerts	local i
265c7d49c03SMatthieu Baerts	for i in "${only_tests_ids[@]}"; do
266ae7bd9ccSMatthieu Baerts		if [ "${TEST_COUNT}" -eq "${i}" ]; then
267ae7bd9ccSMatthieu Baerts			return 1
268ae7bd9ccSMatthieu Baerts		fi
269ae7bd9ccSMatthieu Baerts	done
270c7d49c03SMatthieu Baerts	for i in "${only_tests_names[@]}"; do
271c7d49c03SMatthieu Baerts		if [ "${TEST_NAME}" = "${i}" ]; then
272c7d49c03SMatthieu Baerts			return 1
273c7d49c03SMatthieu Baerts		fi
274c7d49c03SMatthieu Baerts	done
275ae7bd9ccSMatthieu Baerts
276ae7bd9ccSMatthieu Baerts	return 0
277ae7bd9ccSMatthieu Baerts}
278ae7bd9ccSMatthieu Baerts
2797f117cd3SMatthieu Baertsappend_prev_results()
2807f117cd3SMatthieu Baerts{
2817f117cd3SMatthieu Baerts	if [ ${last_test_failed} -eq 1 ]; then
2827f117cd3SMatthieu Baerts		mptcp_lib_result_fail "${TEST_NAME}"
2837f117cd3SMatthieu Baerts	elif [ ${last_test_skipped} -eq 1 ]; then
2847f117cd3SMatthieu Baerts		mptcp_lib_result_skip "${TEST_NAME}"
2857f117cd3SMatthieu Baerts	elif [ ${last_test_ignored} -ne 1 ]; then
2867f117cd3SMatthieu Baerts		mptcp_lib_result_pass "${TEST_NAME}"
2877f117cd3SMatthieu Baerts	fi
2887f117cd3SMatthieu Baerts
2897f117cd3SMatthieu Baerts	last_test_failed=0
2907f117cd3SMatthieu Baerts	last_test_skipped=0
2917f117cd3SMatthieu Baerts	last_test_ignored=0
2927f117cd3SMatthieu Baerts}
2937f117cd3SMatthieu Baerts
294c7d49c03SMatthieu Baerts# $1: test name
295b08fbf24SPaolo Abenireset()
296b08fbf24SPaolo Abeni{
2977f117cd3SMatthieu Baerts	append_prev_results
2987f117cd3SMatthieu Baerts
299c7d49c03SMatthieu Baerts	TEST_NAME="${1}"
300c7d49c03SMatthieu Baerts
301ae7bd9ccSMatthieu Baerts	TEST_COUNT=$((TEST_COUNT+1))
302ae7bd9ccSMatthieu Baerts
303ae7bd9ccSMatthieu Baerts	if skip_test; then
3047f117cd3SMatthieu Baerts		last_test_ignored=1
305ae7bd9ccSMatthieu Baerts		return 1
306ae7bd9ccSMatthieu Baerts	fi
307ae7bd9ccSMatthieu Baerts
30803668c65SMatthieu Baerts	print_title
30903668c65SMatthieu Baerts
31093827ad5SMatthieu Baerts	if [ "${init}" != "1" ]; then
311b08fbf24SPaolo Abeni		init
31293827ad5SMatthieu Baerts	else
31393827ad5SMatthieu Baerts		cleanup_partial
31493827ad5SMatthieu Baerts	fi
31593827ad5SMatthieu Baerts
31693827ad5SMatthieu Baerts	init_partial
317ae7bd9ccSMatthieu Baerts
318ae7bd9ccSMatthieu Baerts	return 0
319b08fbf24SPaolo Abeni}
320b08fbf24SPaolo Abeni
321ae947bb2SMatthieu Baerts# $1: test name ; $2: counter to check
322ae947bb2SMatthieu Baertsreset_check_counter()
323ae947bb2SMatthieu Baerts{
324ae947bb2SMatthieu Baerts	reset "${1}" || return 1
325ae947bb2SMatthieu Baerts
326ae947bb2SMatthieu Baerts	local counter="${2}"
327ae947bb2SMatthieu Baerts
328ae947bb2SMatthieu Baerts	if ! nstat -asz "${counter}" | grep -wq "${counter}"; then
329ae947bb2SMatthieu Baerts		mark_as_skipped "counter '${counter}' is not available"
330ae947bb2SMatthieu Baerts		return 1
331ae947bb2SMatthieu Baerts	fi
332ae947bb2SMatthieu Baerts}
333ae947bb2SMatthieu Baerts
334c7d49c03SMatthieu Baerts# $1: test name
33500587187SFlorian Westphalreset_with_cookies()
33600587187SFlorian Westphal{
337c7d49c03SMatthieu Baerts	reset "${1}" || return 1
33800587187SFlorian Westphal
3391e777bd8SMatthieu Baerts	local netns
34000587187SFlorian Westphal	for netns in "$ns1" "$ns2"; do
34100587187SFlorian Westphal		ip netns exec $netns sysctl -q net.ipv4.tcp_syncookies=2
34200587187SFlorian Westphal	done
34300587187SFlorian Westphal}
34400587187SFlorian Westphal
345c7d49c03SMatthieu Baerts# $1: test name
3468d014eaaSGeliang Tangreset_with_add_addr_timeout()
3478d014eaaSGeliang Tang{
348c7d49c03SMatthieu Baerts	local ip="${2:-4}"
3498d014eaaSGeliang Tang	local tables
3508d014eaaSGeliang Tang
351c7d49c03SMatthieu Baerts	reset "${1}" || return 1
352c7d49c03SMatthieu Baerts
3530c4cd3f8SMatthieu Baerts	tables="${iptables}"
3548d014eaaSGeliang Tang	if [ $ip -eq 6 ]; then
3550c4cd3f8SMatthieu Baerts		tables="${ip6tables}"
3568d014eaaSGeliang Tang	fi
3578d014eaaSGeliang Tang
3588d014eaaSGeliang Tang	ip netns exec $ns1 sysctl -q net.mptcp.add_addr_timeout=1
3594a0b866aSMatthieu Baerts
3604a0b866aSMatthieu Baerts	if ! ip netns exec $ns2 $tables -A OUTPUT -p tcp \
3618d014eaaSGeliang Tang			-m tcp --tcp-option 30 \
3628d014eaaSGeliang Tang			-m bpf --bytecode \
3638d014eaaSGeliang Tang			"$CBPF_MPTCP_SUBOPTION_ADD_ADDR" \
3644a0b866aSMatthieu Baerts			-j DROP; then
3654a0b866aSMatthieu Baerts		mark_as_skipped "unable to set the 'add addr' rule"
3664a0b866aSMatthieu Baerts		return 1
3674a0b866aSMatthieu Baerts	fi
3688d014eaaSGeliang Tang}
3698d014eaaSGeliang Tang
370c7d49c03SMatthieu Baerts# $1: test name
371af66d3e1SGeliang Tangreset_with_checksum()
372af66d3e1SGeliang Tang{
373af66d3e1SGeliang Tang	local ns1_enable=$1
374af66d3e1SGeliang Tang	local ns2_enable=$2
375af66d3e1SGeliang Tang
376c7d49c03SMatthieu Baerts	reset "checksum test ${1} ${2}" || return 1
377af66d3e1SGeliang Tang
378af66d3e1SGeliang Tang	ip netns exec $ns1 sysctl -q net.mptcp.checksum_enabled=$ns1_enable
379af66d3e1SGeliang Tang	ip netns exec $ns2 sysctl -q net.mptcp.checksum_enabled=$ns2_enable
3803c082695SGeliang Tang
3813c082695SGeliang Tang	validate_checksum=1
382af66d3e1SGeliang Tang}
383af66d3e1SGeliang Tang
3840cddb4a6SGeliang Tangreset_with_allow_join_id0()
3850cddb4a6SGeliang Tang{
386c7d49c03SMatthieu Baerts	local ns1_enable=$2
387c7d49c03SMatthieu Baerts	local ns2_enable=$3
3880cddb4a6SGeliang Tang
389c7d49c03SMatthieu Baerts	reset "${1}" || return 1
3900cddb4a6SGeliang Tang
3910cddb4a6SGeliang Tang	ip netns exec $ns1 sysctl -q net.mptcp.allow_join_initial_addr_port=$ns1_enable
3920cddb4a6SGeliang Tang	ip netns exec $ns2 sysctl -q net.mptcp.allow_join_initial_addr_port=$ns2_enable
3930cddb4a6SGeliang Tang}
3940cddb4a6SGeliang Tang
395b6e074e1SGeliang Tang# Modify TCP payload without corrupting the TCP packet
396b6e074e1SGeliang Tang#
397b6e074e1SGeliang Tang# This rule inverts a 8-bit word at byte offset 148 for the 2nd TCP ACK packets
398b6e074e1SGeliang Tang# carrying enough data.
399b6e074e1SGeliang Tang# Once it is done, the TCP Checksum field is updated so the packet is still
400b6e074e1SGeliang Tang# considered as valid at the TCP level.
401b6e074e1SGeliang Tang# Because the MPTCP checksum, covering the TCP options and data, has not been
402b6e074e1SGeliang Tang# updated, the modification will be detected and an MP_FAIL will be emitted:
403b6e074e1SGeliang Tang# what we want to validate here without corrupting "random" MPTCP options.
404b6e074e1SGeliang Tang#
405b6e074e1SGeliang Tang# To avoid having tc producing this pr_info() message for each TCP ACK packets
406b6e074e1SGeliang Tang# not carrying enough data:
407b6e074e1SGeliang Tang#
408b6e074e1SGeliang Tang#     tc action pedit offset 162 out of bounds
409b6e074e1SGeliang Tang#
410b6e074e1SGeliang Tang# Netfilter is used to mark packets with enough data.
4114a0b866aSMatthieu Baertssetup_fail_rules()
412b6e074e1SGeliang Tang{
413b6e074e1SGeliang Tang	check_invert=1
414b6e074e1SGeliang Tang	validate_checksum=1
4154a0b866aSMatthieu Baerts	local i="$1"
4164a0b866aSMatthieu Baerts	local ip="${2:-4}"
417b6e074e1SGeliang Tang	local tables
418b6e074e1SGeliang Tang
4190c4cd3f8SMatthieu Baerts	tables="${iptables}"
420b6e074e1SGeliang Tang	if [ $ip -eq 6 ]; then
4210c4cd3f8SMatthieu Baerts		tables="${ip6tables}"
422b6e074e1SGeliang Tang	fi
423b6e074e1SGeliang Tang
424b6e074e1SGeliang Tang	ip netns exec $ns2 $tables \
425b6e074e1SGeliang Tang		-t mangle \
426b6e074e1SGeliang Tang		-A OUTPUT \
427b6e074e1SGeliang Tang		-o ns2eth$i \
428b6e074e1SGeliang Tang		-p tcp \
429b6e074e1SGeliang Tang		-m length --length 150:9999 \
430b6e074e1SGeliang Tang		-m statistic --mode nth --packet 1 --every 99999 \
4314a0b866aSMatthieu Baerts		-j MARK --set-mark 42 || return ${ksft_skip}
432b6e074e1SGeliang Tang
4334a0b866aSMatthieu Baerts	tc -n $ns2 qdisc add dev ns2eth$i clsact || return ${ksft_skip}
434b6e074e1SGeliang Tang	tc -n $ns2 filter add dev ns2eth$i egress \
435b6e074e1SGeliang Tang		protocol ip prio 1000 \
436b6e074e1SGeliang Tang		handle 42 fw \
437b6e074e1SGeliang Tang		action pedit munge offset 148 u8 invert \
438b6e074e1SGeliang Tang		pipe csum tcp \
4394a0b866aSMatthieu Baerts		index 100 || return ${ksft_skip}
4404a0b866aSMatthieu Baerts}
4414a0b866aSMatthieu Baerts
4424a0b866aSMatthieu Baertsreset_with_fail()
4434a0b866aSMatthieu Baerts{
444ff8897b5SMatthieu Baerts	reset_check_counter "${1}" "MPTcpExtInfiniteMapTx" || return 1
4454a0b866aSMatthieu Baerts	shift
4464a0b866aSMatthieu Baerts
4474a0b866aSMatthieu Baerts	ip netns exec $ns1 sysctl -q net.mptcp.checksum_enabled=1
4484a0b866aSMatthieu Baerts	ip netns exec $ns2 sysctl -q net.mptcp.checksum_enabled=1
4494a0b866aSMatthieu Baerts
4504a0b866aSMatthieu Baerts	local rc=0
4514a0b866aSMatthieu Baerts	setup_fail_rules "${@}" || rc=$?
4524a0b866aSMatthieu Baerts
4534a0b866aSMatthieu Baerts	if [ ${rc} -eq ${ksft_skip} ]; then
4544a0b866aSMatthieu Baerts		mark_as_skipped "unable to set the 'fail' rules"
4554a0b866aSMatthieu Baerts		return 1
4564a0b866aSMatthieu Baerts	fi
457b6e074e1SGeliang Tang}
458b6e074e1SGeliang Tang
459a3735625SGeliang Tangreset_with_events()
460a3735625SGeliang Tang{
461a3735625SGeliang Tang	reset "${1}" || return 1
462a3735625SGeliang Tang
463a3735625SGeliang Tang	:> "$evts_ns1"
464a3735625SGeliang Tang	:> "$evts_ns2"
465a3735625SGeliang Tang	ip netns exec $ns1 ./pm_nl_ctl events >> "$evts_ns1" 2>&1 &
466a3735625SGeliang Tang	evts_ns1_pid=$!
467a3735625SGeliang Tang	ip netns exec $ns2 ./pm_nl_ctl events >> "$evts_ns2" 2>&1 &
468a3735625SGeliang Tang	evts_ns2_pid=$!
469a3735625SGeliang Tang}
470a3735625SGeliang Tang
4714a0b866aSMatthieu Baertsreset_with_tcp_filter()
4724a0b866aSMatthieu Baerts{
4734a0b866aSMatthieu Baerts	reset "${1}" || return 1
4744a0b866aSMatthieu Baerts	shift
4754a0b866aSMatthieu Baerts
4764a0b866aSMatthieu Baerts	local ns="${!1}"
4774a0b866aSMatthieu Baerts	local src="${2}"
4784a0b866aSMatthieu Baerts	local target="${3}"
4794a0b866aSMatthieu Baerts
4804a0b866aSMatthieu Baerts	if ! ip netns exec "${ns}" ${iptables} \
4814a0b866aSMatthieu Baerts			-A INPUT \
4824a0b866aSMatthieu Baerts			-s "${src}" \
4834a0b866aSMatthieu Baerts			-p tcp \
4844a0b866aSMatthieu Baerts			-j "${target}"; then
4854a0b866aSMatthieu Baerts		mark_as_skipped "unable to set the filter rules"
4864a0b866aSMatthieu Baerts		return 1
4874a0b866aSMatthieu Baerts	fi
4884a0b866aSMatthieu Baerts}
4894a0b866aSMatthieu Baerts
49003668c65SMatthieu Baerts# $1: err msg
49139aab882SMatthieu Baertsfail_test()
49239aab882SMatthieu Baerts{
49339aab882SMatthieu Baerts	ret=1
494985de459SPaolo Abeni
49503668c65SMatthieu Baerts	print_fail "${@}"
49603668c65SMatthieu Baerts
4977f117cd3SMatthieu Baerts	# just in case a test is marked twice as failed
4987f117cd3SMatthieu Baerts	if [ ${last_test_failed} -eq 0 ]; then
4997f117cd3SMatthieu Baerts		failed_tests[${TEST_COUNT}]="${TEST_NAME}"
5007f117cd3SMatthieu Baerts		dump_stats
5017f117cd3SMatthieu Baerts		last_test_failed=1
5027f117cd3SMatthieu Baerts	fi
50339aab882SMatthieu Baerts}
50439aab882SMatthieu Baerts
50539aab882SMatthieu Baertsget_failed_tests_ids()
50639aab882SMatthieu Baerts{
50739aab882SMatthieu Baerts	# sorted
50839aab882SMatthieu Baerts	local i
50939aab882SMatthieu Baerts	for i in "${!failed_tests[@]}"; do
51039aab882SMatthieu Baerts		echo "${i}"
51139aab882SMatthieu Baerts	done | sort -n
51239aab882SMatthieu Baerts}
51339aab882SMatthieu Baerts
5148b819a84SFlorian Westphalprint_file_err()
5158b819a84SFlorian Westphal{
5168b819a84SFlorian Westphal	ls -l "$1" 1>&2
51703668c65SMatthieu Baerts	echo -n "Trailing bytes are: "
5188b819a84SFlorian Westphal	tail -c 27 "$1"
5198b819a84SFlorian Westphal}
5208b819a84SFlorian Westphal
521b08fbf24SPaolo Abenicheck_transfer()
522b08fbf24SPaolo Abeni{
5231e777bd8SMatthieu Baerts	local in=$1
5241e777bd8SMatthieu Baerts	local out=$2
5251e777bd8SMatthieu Baerts	local what=$3
5266bf41020SPaolo Abeni	local bytes=$4
527d8d08302SMatthieu Baerts	local i a b
528b08fbf24SPaolo Abeni
5291e777bd8SMatthieu Baerts	local line
5306bf41020SPaolo Abeni	if [ -n "$bytes" ]; then
5310fcd72dfSMatthieu Baerts		local out_size
5326bf41020SPaolo Abeni		# when truncating we must check the size explicitly
5330fcd72dfSMatthieu Baerts		out_size=$(wc -c $out | awk '{print $1}')
5346bf41020SPaolo Abeni		if [ $out_size -ne $bytes ]; then
53503668c65SMatthieu Baerts			fail_test "$what output file has wrong size ($out_size, $bytes)"
5366bf41020SPaolo Abeni			return 1
5376bf41020SPaolo Abeni		fi
538d328fe87SMatthieu Baerts
539d328fe87SMatthieu Baerts		# note: BusyBox's "cmp" command doesn't support --bytes
540d328fe87SMatthieu Baerts		tmpfile=$(mktemp)
541d328fe87SMatthieu Baerts		head --bytes="$bytes" "$in" > "$tmpfile"
542d328fe87SMatthieu Baerts		mv "$tmpfile" "$in"
543d328fe87SMatthieu Baerts		head --bytes="$bytes" "$out" > "$tmpfile"
544d328fe87SMatthieu Baerts		mv "$tmpfile" "$out"
545d328fe87SMatthieu Baerts		tmpfile=""
5466bf41020SPaolo Abeni	fi
547d328fe87SMatthieu Baerts	cmp -l "$in" "$out" | while read -r i a b; do
548d8d08302SMatthieu Baerts		local sum=$((0${a} + 0${b}))
5498117dac3SGeliang Tang		if [ $check_invert -eq 0 ] || [ $sum -ne $((0xff)) ]; then
55003668c65SMatthieu Baerts			fail_test "$what does not match (in, out):"
551b08fbf24SPaolo Abeni			print_file_err "$in"
552b08fbf24SPaolo Abeni			print_file_err "$out"
553b08fbf24SPaolo Abeni
554b08fbf24SPaolo Abeni			return 1
5558117dac3SGeliang Tang		else
55603668c65SMatthieu Baerts			print_info "$what has inverted byte at ${i}"
557b08fbf24SPaolo Abeni		fi
5588117dac3SGeliang Tang	done
559b08fbf24SPaolo Abeni
560b08fbf24SPaolo Abeni	return 0
561b08fbf24SPaolo Abeni}
562b08fbf24SPaolo Abeni
563b08fbf24SPaolo Abenido_ping()
564b08fbf24SPaolo Abeni{
5651e777bd8SMatthieu Baerts	local listener_ns="$1"
5661e777bd8SMatthieu Baerts	local connector_ns="$2"
5671e777bd8SMatthieu Baerts	local connect_addr="$3"
568b08fbf24SPaolo Abeni
569d8d08302SMatthieu Baerts	if ! ip netns exec ${connector_ns} ping -q -c 1 $connect_addr >/dev/null; then
57003668c65SMatthieu Baerts		fail_test "$listener_ns -> $connect_addr connectivity"
571b08fbf24SPaolo Abeni	fi
572b08fbf24SPaolo Abeni}
573b08fbf24SPaolo Abeni
5748b819a84SFlorian Westphallink_failure()
5758b819a84SFlorian Westphal{
5761e777bd8SMatthieu Baerts	local ns="$1"
5778b819a84SFlorian Westphal
5787d1e6f16SPaolo Abeni	if [ -z "$FAILING_LINKS" ]; then
5798b819a84SFlorian Westphal		l=$((RANDOM%4))
5807d1e6f16SPaolo Abeni		FAILING_LINKS=$((l+1))
5817d1e6f16SPaolo Abeni	fi
5828b819a84SFlorian Westphal
5831e777bd8SMatthieu Baerts	local l
5847d1e6f16SPaolo Abeni	for l in $FAILING_LINKS; do
5851e777bd8SMatthieu Baerts		local veth="ns1eth$l"
5868b819a84SFlorian Westphal		ip -net "$ns" link set "$veth" down
5877d1e6f16SPaolo Abeni	done
5888b819a84SFlorian Westphal}
5898b819a84SFlorian Westphal
590523514edSGeliang Tang# $1: IP address
591523514edSGeliang Tangis_v6()
592523514edSGeliang Tang{
593523514edSGeliang Tang	[ -z "${1##*:*}" ]
594523514edSGeliang Tang}
595523514edSGeliang Tang
596327b9a94SPaolo Abeni# $1: ns, $2: port
597327b9a94SPaolo Abeniwait_local_port_listen()
598327b9a94SPaolo Abeni{
599327b9a94SPaolo Abeni	local listener_ns="${1}"
600327b9a94SPaolo Abeni	local port="${2}"
601327b9a94SPaolo Abeni
6021e777bd8SMatthieu Baerts	local port_hex
603327b9a94SPaolo Abeni	port_hex="$(printf "%04X" "${port}")"
6041e777bd8SMatthieu Baerts
6051e777bd8SMatthieu Baerts	local i
606327b9a94SPaolo Abeni	for i in $(seq 10); do
607327b9a94SPaolo Abeni		ip netns exec "${listener_ns}" cat /proc/net/tcp* | \
608327b9a94SPaolo Abeni			awk "BEGIN {rc=1} {if (\$2 ~ /:${port_hex}\$/ && \$4 ~ /0A/) {rc=0; exit}} END {exit rc}" &&
609327b9a94SPaolo Abeni			break
610327b9a94SPaolo Abeni		sleep 0.1
611327b9a94SPaolo Abeni	done
612327b9a94SPaolo Abeni}
613327b9a94SPaolo Abeni
61447867f0aSMatthieu Baerts# $1: ns ; $2: counter
61547867f0aSMatthieu Baertsget_counter()
61647867f0aSMatthieu Baerts{
61747867f0aSMatthieu Baerts	local ns="${1}"
61847867f0aSMatthieu Baerts	local counter="${2}"
61947867f0aSMatthieu Baerts	local count
62047867f0aSMatthieu Baerts
62147867f0aSMatthieu Baerts	count=$(ip netns exec ${ns} nstat -asz "${counter}" | awk 'NR==1 {next} {print $2}')
62247867f0aSMatthieu Baerts	if [ -z "${count}" ]; then
62347867f0aSMatthieu Baerts		mptcp_lib_fail_if_expected_feature "${counter} counter"
62447867f0aSMatthieu Baerts		return 1
62547867f0aSMatthieu Baerts	fi
62647867f0aSMatthieu Baerts
62747867f0aSMatthieu Baerts	echo "${count}"
62847867f0aSMatthieu Baerts}
62947867f0aSMatthieu Baerts
630327b9a94SPaolo Abenirm_addr_count()
631327b9a94SPaolo Abeni{
63247867f0aSMatthieu Baerts	get_counter "${1}" "MPTcpExtRmAddr"
633327b9a94SPaolo Abeni}
634327b9a94SPaolo Abeni
635327b9a94SPaolo Abeni# $1: ns, $2: old rm_addr counter in $ns
636327b9a94SPaolo Abeniwait_rm_addr()
637327b9a94SPaolo Abeni{
638327b9a94SPaolo Abeni	local ns="${1}"
639327b9a94SPaolo Abeni	local old_cnt="${2}"
640327b9a94SPaolo Abeni	local cnt
641327b9a94SPaolo Abeni
6421e777bd8SMatthieu Baerts	local i
643327b9a94SPaolo Abeni	for i in $(seq 10); do
644327b9a94SPaolo Abeni		cnt=$(rm_addr_count ${ns})
645327b9a94SPaolo Abeni		[ "$cnt" = "${old_cnt}" ] || break
646327b9a94SPaolo Abeni		sleep 0.1
647327b9a94SPaolo Abeni	done
648327b9a94SPaolo Abeni}
649327b9a94SPaolo Abeni
6504369c198SGeliang Tangrm_sf_count()
6514369c198SGeliang Tang{
6524369c198SGeliang Tang	get_counter "${1}" "MPTcpExtRmSubflow"
6534369c198SGeliang Tang}
6544369c198SGeliang Tang
6554369c198SGeliang Tang# $1: ns, $2: old rm_sf counter in $ns
6564369c198SGeliang Tangwait_rm_sf()
6574369c198SGeliang Tang{
6584369c198SGeliang Tang	local ns="${1}"
6594369c198SGeliang Tang	local old_cnt="${2}"
6604369c198SGeliang Tang	local cnt
6614369c198SGeliang Tang
6624369c198SGeliang Tang	local i
6634369c198SGeliang Tang	for i in $(seq 10); do
6644369c198SGeliang Tang		cnt=$(rm_sf_count ${ns})
6654369c198SGeliang Tang		[ "$cnt" = "${old_cnt}" ] || break
6664369c198SGeliang Tang		sleep 0.1
6674369c198SGeliang Tang	done
6684369c198SGeliang Tang}
6694369c198SGeliang Tang
67069c6ce7bSPaolo Abeniwait_mpj()
67169c6ce7bSPaolo Abeni{
67269c6ce7bSPaolo Abeni	local ns="${1}"
67369c6ce7bSPaolo Abeni	local cnt old_cnt
67469c6ce7bSPaolo Abeni
67547867f0aSMatthieu Baerts	old_cnt=$(get_counter ${ns} "MPTcpExtMPJoinAckRx")
67669c6ce7bSPaolo Abeni
67769c6ce7bSPaolo Abeni	local i
67869c6ce7bSPaolo Abeni	for i in $(seq 10); do
67947867f0aSMatthieu Baerts		cnt=$(get_counter ${ns} "MPTcpExtMPJoinAckRx")
68069c6ce7bSPaolo Abeni		[ "$cnt" = "${old_cnt}" ] || break
68169c6ce7bSPaolo Abeni		sleep 0.1
68269c6ce7bSPaolo Abeni	done
68369c6ce7bSPaolo Abeni}
68469c6ce7bSPaolo Abeni
68597040cf9SGeliang Tangkill_wait()
68697040cf9SGeliang Tang{
68797040cf9SGeliang Tang	kill $1 > /dev/null 2>&1
68897040cf9SGeliang Tang	wait $1 2>/dev/null
68997040cf9SGeliang Tang}
69097040cf9SGeliang Tang
691a3735625SGeliang Tangkill_events_pids()
692a3735625SGeliang Tang{
693a3735625SGeliang Tang	kill_wait $evts_ns1_pid
694a3735625SGeliang Tang	kill_wait $evts_ns2_pid
695a3735625SGeliang Tang}
696a3735625SGeliang Tang
697070d6dafSMatthieu Baertskill_tests_wait()
698070d6dafSMatthieu Baerts{
6990fcd72dfSMatthieu Baerts	#shellcheck disable=SC2046
700070d6dafSMatthieu Baerts	kill -SIGUSR1 $(ip netns pids $ns2) $(ip netns pids $ns1)
701070d6dafSMatthieu Baerts	wait
702070d6dafSMatthieu Baerts}
703070d6dafSMatthieu Baerts
70434aa6e3bSGeliang Tangpm_nl_set_limits()
70534aa6e3bSGeliang Tang{
70634aa6e3bSGeliang Tang	local ns=$1
70734aa6e3bSGeliang Tang	local addrs=$2
70834aa6e3bSGeliang Tang	local subflows=$3
70934aa6e3bSGeliang Tang
71034aa6e3bSGeliang Tang	if [ $ip_mptcp -eq 1 ]; then
71134aa6e3bSGeliang Tang		ip -n $ns mptcp limits set add_addr_accepted $addrs subflows $subflows
71234aa6e3bSGeliang Tang	else
71334aa6e3bSGeliang Tang		ip netns exec $ns ./pm_nl_ctl limits $addrs $subflows
71434aa6e3bSGeliang Tang	fi
71534aa6e3bSGeliang Tang}
71634aa6e3bSGeliang Tang
71734aa6e3bSGeliang Tangpm_nl_add_endpoint()
71834aa6e3bSGeliang Tang{
71934aa6e3bSGeliang Tang	local ns=$1
72034aa6e3bSGeliang Tang	local addr=$2
7211e777bd8SMatthieu Baerts	local flags _flags
7221e777bd8SMatthieu Baerts	local port _port
7231e777bd8SMatthieu Baerts	local dev _dev
7241e777bd8SMatthieu Baerts	local id _id
72534aa6e3bSGeliang Tang	local nr=2
72634aa6e3bSGeliang Tang
7271e777bd8SMatthieu Baerts	local p
728d8d08302SMatthieu Baerts	for p in "${@}"
72934aa6e3bSGeliang Tang	do
73034aa6e3bSGeliang Tang		if [ $p = "flags" ]; then
73134aa6e3bSGeliang Tang			eval _flags=\$"$nr"
732d8d08302SMatthieu Baerts			[ -n "$_flags" ]; flags="flags $_flags"
73334aa6e3bSGeliang Tang		fi
73434aa6e3bSGeliang Tang		if [ $p = "dev" ]; then
73534aa6e3bSGeliang Tang			eval _dev=\$"$nr"
736d8d08302SMatthieu Baerts			[ -n "$_dev" ]; dev="dev $_dev"
73734aa6e3bSGeliang Tang		fi
73834aa6e3bSGeliang Tang		if [ $p = "id" ]; then
73934aa6e3bSGeliang Tang			eval _id=\$"$nr"
740d8d08302SMatthieu Baerts			[ -n "$_id" ]; id="id $_id"
74134aa6e3bSGeliang Tang		fi
74234aa6e3bSGeliang Tang		if [ $p = "port" ]; then
74334aa6e3bSGeliang Tang			eval _port=\$"$nr"
744d8d08302SMatthieu Baerts			[ -n "$_port" ]; port="port $_port"
74534aa6e3bSGeliang Tang		fi
74634aa6e3bSGeliang Tang
747d8d08302SMatthieu Baerts		nr=$((nr + 1))
74834aa6e3bSGeliang Tang	done
74934aa6e3bSGeliang Tang
75034aa6e3bSGeliang Tang	if [ $ip_mptcp -eq 1 ]; then
75134aa6e3bSGeliang Tang		ip -n $ns mptcp endpoint add $addr ${_flags//","/" "} $dev $id $port
75234aa6e3bSGeliang Tang	else
75334aa6e3bSGeliang Tang		ip netns exec $ns ./pm_nl_ctl add $addr $flags $dev $id $port
75434aa6e3bSGeliang Tang	fi
75534aa6e3bSGeliang Tang}
75634aa6e3bSGeliang Tang
75734aa6e3bSGeliang Tangpm_nl_del_endpoint()
75834aa6e3bSGeliang Tang{
75934aa6e3bSGeliang Tang	local ns=$1
76034aa6e3bSGeliang Tang	local id=$2
76134aa6e3bSGeliang Tang	local addr=$3
76234aa6e3bSGeliang Tang
76334aa6e3bSGeliang Tang	if [ $ip_mptcp -eq 1 ]; then
764aaf2123aSAndrea Claudi		[ $id -ne 0 ] && addr=''
76534aa6e3bSGeliang Tang		ip -n $ns mptcp endpoint delete id $id $addr
76634aa6e3bSGeliang Tang	else
76734aa6e3bSGeliang Tang		ip netns exec $ns ./pm_nl_ctl del $id $addr
76834aa6e3bSGeliang Tang	fi
76934aa6e3bSGeliang Tang}
77034aa6e3bSGeliang Tang
77134aa6e3bSGeliang Tangpm_nl_flush_endpoint()
77234aa6e3bSGeliang Tang{
77334aa6e3bSGeliang Tang	local ns=$1
77434aa6e3bSGeliang Tang
77534aa6e3bSGeliang Tang	if [ $ip_mptcp -eq 1 ]; then
77634aa6e3bSGeliang Tang		ip -n $ns mptcp endpoint flush
77734aa6e3bSGeliang Tang	else
77834aa6e3bSGeliang Tang		ip netns exec $ns ./pm_nl_ctl flush
77934aa6e3bSGeliang Tang	fi
78034aa6e3bSGeliang Tang}
78134aa6e3bSGeliang Tang
782dda61b3dSGeliang Tangpm_nl_show_endpoints()
783dda61b3dSGeliang Tang{
784dda61b3dSGeliang Tang	local ns=$1
785dda61b3dSGeliang Tang
786dda61b3dSGeliang Tang	if [ $ip_mptcp -eq 1 ]; then
787dda61b3dSGeliang Tang		ip -n $ns mptcp endpoint show
788dda61b3dSGeliang Tang	else
789dda61b3dSGeliang Tang		ip netns exec $ns ./pm_nl_ctl dump
790dda61b3dSGeliang Tang	fi
791dda61b3dSGeliang Tang}
792dda61b3dSGeliang Tang
793f0140386SGeliang Tangpm_nl_change_endpoint()
794f0140386SGeliang Tang{
795f0140386SGeliang Tang	local ns=$1
796bccefb76SGeliang Tang	local id=$2
797bccefb76SGeliang Tang	local flags=$3
798f0140386SGeliang Tang
799f0140386SGeliang Tang	if [ $ip_mptcp -eq 1 ]; then
800f0140386SGeliang Tang		ip -n $ns mptcp endpoint change id $id ${flags//","/" "}
801f0140386SGeliang Tang	else
802bccefb76SGeliang Tang		ip netns exec $ns ./pm_nl_ctl set id $id flags $flags
803f0140386SGeliang Tang	fi
804f0140386SGeliang Tang}
805f0140386SGeliang Tang
80669c6ce7bSPaolo Abenipm_nl_check_endpoint()
80769c6ce7bSPaolo Abeni{
80869c6ce7bSPaolo Abeni	local line expected_line
80903668c65SMatthieu Baerts	local msg="$1"
81003668c65SMatthieu Baerts	local ns=$2
81103668c65SMatthieu Baerts	local addr=$3
81269c6ce7bSPaolo Abeni	local _flags=""
81369c6ce7bSPaolo Abeni	local flags
81469c6ce7bSPaolo Abeni	local _port
81569c6ce7bSPaolo Abeni	local port
81669c6ce7bSPaolo Abeni	local dev
81769c6ce7bSPaolo Abeni	local _id
81869c6ce7bSPaolo Abeni	local id
81969c6ce7bSPaolo Abeni
82003668c65SMatthieu Baerts	print_check "${msg}"
82169c6ce7bSPaolo Abeni
82203668c65SMatthieu Baerts	shift 3
82369c6ce7bSPaolo Abeni	while [ -n "$1" ]; do
82469c6ce7bSPaolo Abeni		if [ $1 = "flags" ]; then
82569c6ce7bSPaolo Abeni			_flags=$2
826d8d08302SMatthieu Baerts			[ -n "$_flags" ]; flags="flags $_flags"
82769c6ce7bSPaolo Abeni			shift
82869c6ce7bSPaolo Abeni		elif [ $1 = "dev" ]; then
829d8d08302SMatthieu Baerts			[ -n "$2" ]; dev="dev $1"
83069c6ce7bSPaolo Abeni			shift
83169c6ce7bSPaolo Abeni		elif [ $1 = "id" ]; then
83269c6ce7bSPaolo Abeni			_id=$2
833d8d08302SMatthieu Baerts			[ -n "$_id" ]; id="id $_id"
83469c6ce7bSPaolo Abeni			shift
83569c6ce7bSPaolo Abeni		elif [ $1 = "port" ]; then
83669c6ce7bSPaolo Abeni			_port=$2
837d8d08302SMatthieu Baerts			[ -n "$_port" ]; port=" port $_port"
83869c6ce7bSPaolo Abeni			shift
83969c6ce7bSPaolo Abeni		fi
84069c6ce7bSPaolo Abeni
84169c6ce7bSPaolo Abeni		shift
84269c6ce7bSPaolo Abeni	done
84369c6ce7bSPaolo Abeni
84469c6ce7bSPaolo Abeni	if [ -z "$id" ]; then
84503668c65SMatthieu Baerts		test_fail "bad test - missing endpoint id"
84669c6ce7bSPaolo Abeni		return
84769c6ce7bSPaolo Abeni	fi
84869c6ce7bSPaolo Abeni
84969c6ce7bSPaolo Abeni	if [ $ip_mptcp -eq 1 ]; then
850c8c101aeSAndrea Claudi		# get line and trim trailing whitespace
85169c6ce7bSPaolo Abeni		line=$(ip -n $ns mptcp endpoint show $id)
852c8c101aeSAndrea Claudi		line="${line% }"
85369c6ce7bSPaolo Abeni		# the dump order is: address id flags port dev
854c8c101aeSAndrea Claudi		[ -n "$addr" ] && expected_line="$addr"
85569c6ce7bSPaolo Abeni		expected_line="$expected_line $id"
85669c6ce7bSPaolo Abeni		[ -n "$_flags" ] && expected_line="$expected_line ${_flags//","/" "}"
85769c6ce7bSPaolo Abeni		[ -n "$dev" ] && expected_line="$expected_line $dev"
85869c6ce7bSPaolo Abeni		[ -n "$port" ] && expected_line="$expected_line $port"
85969c6ce7bSPaolo Abeni	else
86069c6ce7bSPaolo Abeni		line=$(ip netns exec $ns ./pm_nl_ctl get $_id)
86169c6ce7bSPaolo Abeni		# the dump order is: id flags dev address port
86269c6ce7bSPaolo Abeni		expected_line="$id"
86369c6ce7bSPaolo Abeni		[ -n "$flags" ] && expected_line="$expected_line $flags"
86469c6ce7bSPaolo Abeni		[ -n "$dev" ] && expected_line="$expected_line $dev"
86569c6ce7bSPaolo Abeni		[ -n "$addr" ] && expected_line="$expected_line $addr"
86669c6ce7bSPaolo Abeni		[ -n "$_port" ] && expected_line="$expected_line $_port"
86769c6ce7bSPaolo Abeni	fi
86869c6ce7bSPaolo Abeni	if [ "$line" = "$expected_line" ]; then
86903668c65SMatthieu Baerts		print_ok
87069c6ce7bSPaolo Abeni	else
87103668c65SMatthieu Baerts		fail_test "expected '$expected_line' found '$line'"
87269c6ce7bSPaolo Abeni	fi
87369c6ce7bSPaolo Abeni}
87469c6ce7bSPaolo Abeni
8759e9d176dSGeliang Tangpm_nl_set_endpoint()
876b08fbf24SPaolo Abeni{
8771e777bd8SMatthieu Baerts	local listener_ns="$1"
8781e777bd8SMatthieu Baerts	local connector_ns="$2"
8799e9d176dSGeliang Tang	local connect_addr="$3"
880b08fbf24SPaolo Abeni
881662aa22dSGeliang Tang	local addr_nr_ns1=${addr_nr_ns1:-0}
882662aa22dSGeliang Tang	local addr_nr_ns2=${addr_nr_ns2:-0}
883662aa22dSGeliang Tang	local sflags=${sflags:-""}
8844aadde08SGeliang Tang	local fullmesh=${fullmesh:-""}
885662aa22dSGeliang Tang
886080b7f57SGeliang Tang	local flags="subflow"
8874aadde08SGeliang Tang	if [ -n "${fullmesh}" ]; then
888080b7f57SGeliang Tang		flags="${flags},fullmesh"
8894aadde08SGeliang Tang		addr_nr_ns2=${fullmesh}
890080b7f57SGeliang Tang	fi
891080b7f57SGeliang Tang
892327b9a94SPaolo Abeni	# let the mptcp subflow be established in background before
893327b9a94SPaolo Abeni	# do endpoint manipulation
894d8d08302SMatthieu Baerts	if [ $addr_nr_ns1 != "0" ] || [ $addr_nr_ns2 != "0" ]; then
895d8d08302SMatthieu Baerts		sleep 1
896d8d08302SMatthieu Baerts	fi
897327b9a94SPaolo Abeni
8986208fd82SGeliang Tang	if [ $addr_nr_ns1 -gt 0 ]; then
8991e777bd8SMatthieu Baerts		local counter=2
900d8d08302SMatthieu Baerts		local add_nr_ns1=${addr_nr_ns1}
90197040cf9SGeliang Tang		local id=10
9026208fd82SGeliang Tang		while [ $add_nr_ns1 -gt 0 ]; do
9036208fd82SGeliang Tang			local addr
9046208fd82SGeliang Tang			if is_v6 "${connect_addr}"; then
9056208fd82SGeliang Tang				addr="dead:beef:$counter::1"
9066208fd82SGeliang Tang			else
9076208fd82SGeliang Tang				addr="10.0.$counter.1"
9086208fd82SGeliang Tang			fi
90934aa6e3bSGeliang Tang			pm_nl_add_endpoint $ns1 $addr flags signal
910d8d08302SMatthieu Baerts			counter=$((counter + 1))
911d8d08302SMatthieu Baerts			add_nr_ns1=$((add_nr_ns1 - 1))
91297040cf9SGeliang Tang			id=$((id + 1))
9136208fd82SGeliang Tang		done
9146208fd82SGeliang Tang	elif [ $addr_nr_ns1 -lt 0 ]; then
915d8d08302SMatthieu Baerts		local rm_nr_ns1=$((-addr_nr_ns1))
9166fe4ccdcSGeliang Tang		if [ $rm_nr_ns1 -lt 8 ]; then
9171e777bd8SMatthieu Baerts			local counter=0
9181e777bd8SMatthieu Baerts			local line
919d8d08302SMatthieu Baerts			pm_nl_show_endpoints ${listener_ns} | while read -r line; do
920d8d08302SMatthieu Baerts				# shellcheck disable=SC2206 # we do want to split per word
921dda61b3dSGeliang Tang				local arr=($line)
922dda61b3dSGeliang Tang				local nr=0
923dda61b3dSGeliang Tang
9241e777bd8SMatthieu Baerts				local i
925d8d08302SMatthieu Baerts				for i in "${arr[@]}"; do
926dda61b3dSGeliang Tang					if [ $i = "id" ]; then
927dda61b3dSGeliang Tang						if [ $counter -eq $rm_nr_ns1 ]; then
928dda61b3dSGeliang Tang							break
929dda61b3dSGeliang Tang						fi
930dda61b3dSGeliang Tang						id=${arr[$nr+1]}
931327b9a94SPaolo Abeni						rm_addr=$(rm_addr_count ${connector_ns})
93234aa6e3bSGeliang Tang						pm_nl_del_endpoint ${listener_ns} $id
933327b9a94SPaolo Abeni						wait_rm_addr ${connector_ns} ${rm_addr}
934d8d08302SMatthieu Baerts						counter=$((counter + 1))
935f87744adSGeliang Tang					fi
936d8d08302SMatthieu Baerts					nr=$((nr + 1))
937dda61b3dSGeliang Tang				done
938dda61b3dSGeliang Tang			done
9395e287fe7SGeliang Tang		elif [ $rm_nr_ns1 -eq 8 ]; then
94034aa6e3bSGeliang Tang			pm_nl_flush_endpoint ${listener_ns}
9415e287fe7SGeliang Tang		elif [ $rm_nr_ns1 -eq 9 ]; then
94234aa6e3bSGeliang Tang			pm_nl_del_endpoint ${listener_ns} 0 ${connect_addr}
9436fe4ccdcSGeliang Tang		fi
944dd72b0feSGeliang Tang	fi
945dd72b0feSGeliang Tang
946327b9a94SPaolo Abeni	# if newly added endpoints must be deleted, give the background msk
947327b9a94SPaolo Abeni	# some time to created them
948d8d08302SMatthieu Baerts	[ $addr_nr_ns1 -gt 0 ] && [ $addr_nr_ns2 -lt 0 ] && sleep 1
949327b9a94SPaolo Abeni
9506208fd82SGeliang Tang	if [ $addr_nr_ns2 -gt 0 ]; then
951d8d08302SMatthieu Baerts		local add_nr_ns2=${addr_nr_ns2}
9521e777bd8SMatthieu Baerts		local counter=3
9535e986ec4SGeliang Tang		local id=20
9546208fd82SGeliang Tang		while [ $add_nr_ns2 -gt 0 ]; do
9556208fd82SGeliang Tang			local addr
9566208fd82SGeliang Tang			if is_v6 "${connect_addr}"; then
9576208fd82SGeliang Tang				addr="dead:beef:$counter::2"
9586208fd82SGeliang Tang			else
9596208fd82SGeliang Tang				addr="10.0.$counter.2"
9606208fd82SGeliang Tang			fi
96134aa6e3bSGeliang Tang			pm_nl_add_endpoint $ns2 $addr flags $flags
962d8d08302SMatthieu Baerts			counter=$((counter + 1))
963d8d08302SMatthieu Baerts			add_nr_ns2=$((add_nr_ns2 - 1))
9645e986ec4SGeliang Tang			id=$((id + 1))
9656208fd82SGeliang Tang		done
9666208fd82SGeliang Tang	elif [ $addr_nr_ns2 -lt 0 ]; then
967d8d08302SMatthieu Baerts		local rm_nr_ns2=$((-addr_nr_ns2))
9686fe4ccdcSGeliang Tang		if [ $rm_nr_ns2 -lt 8 ]; then
9691e777bd8SMatthieu Baerts			local counter=0
9701e777bd8SMatthieu Baerts			local line
971d8d08302SMatthieu Baerts			pm_nl_show_endpoints ${connector_ns} | while read -r line; do
972d8d08302SMatthieu Baerts				# shellcheck disable=SC2206 # we do want to split per word
973dda61b3dSGeliang Tang				local arr=($line)
974dda61b3dSGeliang Tang				local nr=0
975dda61b3dSGeliang Tang
9761e777bd8SMatthieu Baerts				local i
977d8d08302SMatthieu Baerts				for i in "${arr[@]}"; do
978dda61b3dSGeliang Tang					if [ $i = "id" ]; then
979dda61b3dSGeliang Tang						if [ $counter -eq $rm_nr_ns2 ]; then
980dda61b3dSGeliang Tang							break
981dda61b3dSGeliang Tang						fi
9821e777bd8SMatthieu Baerts						local id rm_addr
983dda61b3dSGeliang Tang						# rm_addr are serialized, allow the previous one to
984dda61b3dSGeliang Tang						# complete
985dda61b3dSGeliang Tang						id=${arr[$nr+1]}
986327b9a94SPaolo Abeni						rm_addr=$(rm_addr_count ${listener_ns})
98734aa6e3bSGeliang Tang						pm_nl_del_endpoint ${connector_ns} $id
988327b9a94SPaolo Abeni						wait_rm_addr ${listener_ns} ${rm_addr}
989d8d08302SMatthieu Baerts						counter=$((counter + 1))
990f87744adSGeliang Tang					fi
991d8d08302SMatthieu Baerts					nr=$((nr + 1))
992dda61b3dSGeliang Tang				done
993dda61b3dSGeliang Tang			done
9945e287fe7SGeliang Tang		elif [ $rm_nr_ns2 -eq 8 ]; then
99534aa6e3bSGeliang Tang			pm_nl_flush_endpoint ${connector_ns}
9965e287fe7SGeliang Tang		elif [ $rm_nr_ns2 -eq 9 ]; then
9975e287fe7SGeliang Tang			local addr
9985e287fe7SGeliang Tang			if is_v6 "${connect_addr}"; then
9995e287fe7SGeliang Tang				addr="dead:beef:1::2"
10005e287fe7SGeliang Tang			else
10015e287fe7SGeliang Tang				addr="10.0.1.2"
10025e287fe7SGeliang Tang			fi
100334aa6e3bSGeliang Tang			pm_nl_del_endpoint ${connector_ns} 0 $addr
10046fe4ccdcSGeliang Tang		fi
1005dd72b0feSGeliang Tang	fi
1006dd72b0feSGeliang Tang
1007d8d08302SMatthieu Baerts	if [ -n "${sflags}" ]; then
1008718eb44eSGeliang Tang		sleep 1
10091e777bd8SMatthieu Baerts
10101e777bd8SMatthieu Baerts		local netns
1011718eb44eSGeliang Tang		for netns in "$ns1" "$ns2"; do
10121e777bd8SMatthieu Baerts			local line
1013d8d08302SMatthieu Baerts			pm_nl_show_endpoints $netns | while read -r line; do
1014d8d08302SMatthieu Baerts				# shellcheck disable=SC2206 # we do want to split per word
101533397b83SGeliang Tang				local arr=($line)
1016bccefb76SGeliang Tang				local nr=0
1017f0140386SGeliang Tang				local id
101833397b83SGeliang Tang
10191e777bd8SMatthieu Baerts				local i
1020d8d08302SMatthieu Baerts				for i in "${arr[@]}"; do
1021bccefb76SGeliang Tang					if [ $i = "id" ]; then
1022bccefb76SGeliang Tang						id=${arr[$nr+1]}
1023718eb44eSGeliang Tang					fi
1024d8d08302SMatthieu Baerts					nr=$((nr + 1))
102533397b83SGeliang Tang				done
1026bccefb76SGeliang Tang				pm_nl_change_endpoint $netns $id $sflags
102733397b83SGeliang Tang			done
1028718eb44eSGeliang Tang		done
1029718eb44eSGeliang Tang	fi
10309e9d176dSGeliang Tang}
10319e9d176dSGeliang Tang
10329e9d176dSGeliang Tangdo_transfer()
10339e9d176dSGeliang Tang{
10349e9d176dSGeliang Tang	local listener_ns="$1"
10359e9d176dSGeliang Tang	local connector_ns="$2"
10369e9d176dSGeliang Tang	local cl_proto="$3"
10379e9d176dSGeliang Tang	local srv_proto="$4"
10389e9d176dSGeliang Tang	local connect_addr="$5"
10399e9d176dSGeliang Tang
10409e9d176dSGeliang Tang	local port=$((10000 + TEST_COUNT - 1))
10419e9d176dSGeliang Tang	local cappid
1042662aa22dSGeliang Tang	local FAILING_LINKS=${FAILING_LINKS:-""}
1043080b7f57SGeliang Tang	local fastclose=${fastclose:-""}
1044e571fb09SGeliang Tang	local speed=${speed:-"fast"}
10459e9d176dSGeliang Tang
10469e9d176dSGeliang Tang	:> "$cout"
10479e9d176dSGeliang Tang	:> "$sout"
10489e9d176dSGeliang Tang	:> "$capout"
10499e9d176dSGeliang Tang
10509e9d176dSGeliang Tang	if [ $capture -eq 1 ]; then
10519e9d176dSGeliang Tang		local capuser
10529e9d176dSGeliang Tang		if [ -z $SUDO_USER ] ; then
10539e9d176dSGeliang Tang			capuser=""
10549e9d176dSGeliang Tang		else
10559e9d176dSGeliang Tang			capuser="-Z $SUDO_USER"
10569e9d176dSGeliang Tang		fi
10579e9d176dSGeliang Tang
10589e9d176dSGeliang Tang		capfile=$(printf "mp_join-%02u-%s.pcap" "$TEST_COUNT" "${listener_ns}")
10599e9d176dSGeliang Tang
10609e9d176dSGeliang Tang		echo "Capturing traffic for test $TEST_COUNT into $capfile"
10619e9d176dSGeliang Tang		ip netns exec ${listener_ns} tcpdump -i any -s 65535 -B 32768 $capuser -w $capfile > "$capout" 2>&1 &
10629e9d176dSGeliang Tang		cappid=$!
10639e9d176dSGeliang Tang
10649e9d176dSGeliang Tang		sleep 1
10659e9d176dSGeliang Tang	fi
10669e9d176dSGeliang Tang
10679e9d176dSGeliang Tang	NSTAT_HISTORY=/tmp/${listener_ns}.nstat ip netns exec ${listener_ns} \
10689e9d176dSGeliang Tang		nstat -n
10699e9d176dSGeliang Tang	NSTAT_HISTORY=/tmp/${connector_ns}.nstat ip netns exec ${connector_ns} \
10709e9d176dSGeliang Tang		nstat -n
10719e9d176dSGeliang Tang
10729e9d176dSGeliang Tang	local extra_args
10739e9d176dSGeliang Tang	if [ $speed = "fast" ]; then
10749e9d176dSGeliang Tang		extra_args="-j"
10759e9d176dSGeliang Tang	elif [ $speed = "slow" ]; then
10769e9d176dSGeliang Tang		extra_args="-r 50"
1077e571fb09SGeliang Tang	elif [ $speed -gt 0 ]; then
1078e571fb09SGeliang Tang		extra_args="-r ${speed}"
10799e9d176dSGeliang Tang	fi
10809e9d176dSGeliang Tang
10819e9d176dSGeliang Tang	local extra_cl_args=""
10829e9d176dSGeliang Tang	local extra_srv_args=""
10839e9d176dSGeliang Tang	local trunc_size=""
1084080b7f57SGeliang Tang	if [ -n "${fastclose}" ]; then
10859e9d176dSGeliang Tang		if [ ${test_linkfail} -le 1 ]; then
108603668c65SMatthieu Baerts			fail_test "fastclose tests need test_linkfail argument"
10879e9d176dSGeliang Tang			return 1
10889e9d176dSGeliang Tang		fi
10899e9d176dSGeliang Tang
10909e9d176dSGeliang Tang		# disconnect
10919e9d176dSGeliang Tang		trunc_size=${test_linkfail}
1092080b7f57SGeliang Tang		local side=${fastclose}
10939e9d176dSGeliang Tang
10949e9d176dSGeliang Tang		if [ ${side} = "client" ]; then
10959e9d176dSGeliang Tang			extra_cl_args="-f ${test_linkfail}"
10969e9d176dSGeliang Tang			extra_srv_args="-f -1"
10979e9d176dSGeliang Tang		elif [ ${side} = "server" ]; then
10989e9d176dSGeliang Tang			extra_srv_args="-f ${test_linkfail}"
10999e9d176dSGeliang Tang			extra_cl_args="-f -1"
11009e9d176dSGeliang Tang		else
110103668c65SMatthieu Baerts			fail_test "wrong/unknown fastclose spec ${side}"
11029e9d176dSGeliang Tang			return 1
11039e9d176dSGeliang Tang		fi
11049e9d176dSGeliang Tang	fi
11059e9d176dSGeliang Tang
11069e9d176dSGeliang Tang	extra_srv_args="$extra_args $extra_srv_args"
11079e9d176dSGeliang Tang	if [ "$test_linkfail" -gt 1 ];then
11089e9d176dSGeliang Tang		timeout ${timeout_test} \
11099e9d176dSGeliang Tang			ip netns exec ${listener_ns} \
11109e9d176dSGeliang Tang				./mptcp_connect -t ${timeout_poll} -l -p $port -s ${srv_proto} \
11119e9d176dSGeliang Tang					$extra_srv_args "::" < "$sinfail" > "$sout" &
11129e9d176dSGeliang Tang	else
11139e9d176dSGeliang Tang		timeout ${timeout_test} \
11149e9d176dSGeliang Tang			ip netns exec ${listener_ns} \
11159e9d176dSGeliang Tang				./mptcp_connect -t ${timeout_poll} -l -p $port -s ${srv_proto} \
11169e9d176dSGeliang Tang					$extra_srv_args "::" < "$sin" > "$sout" &
11179e9d176dSGeliang Tang	fi
11189e9d176dSGeliang Tang	local spid=$!
11199e9d176dSGeliang Tang
11209e9d176dSGeliang Tang	wait_local_port_listen "${listener_ns}" "${port}"
11219e9d176dSGeliang Tang
11229e9d176dSGeliang Tang	extra_cl_args="$extra_args $extra_cl_args"
11239e9d176dSGeliang Tang	if [ "$test_linkfail" -eq 0 ];then
11249e9d176dSGeliang Tang		timeout ${timeout_test} \
11259e9d176dSGeliang Tang			ip netns exec ${connector_ns} \
11269e9d176dSGeliang Tang				./mptcp_connect -t ${timeout_poll} -p $port -s ${cl_proto} \
11279e9d176dSGeliang Tang					$extra_cl_args $connect_addr < "$cin" > "$cout" &
11289e9d176dSGeliang Tang	elif [ "$test_linkfail" -eq 1 ] || [ "$test_linkfail" -eq 2 ];then
11299e9d176dSGeliang Tang		( cat "$cinfail" ; sleep 2; link_failure $listener_ns ; cat "$cinfail" ) | \
11309e9d176dSGeliang Tang			tee "$cinsent" | \
11319e9d176dSGeliang Tang			timeout ${timeout_test} \
11329e9d176dSGeliang Tang				ip netns exec ${connector_ns} \
11339e9d176dSGeliang Tang					./mptcp_connect -t ${timeout_poll} -p $port -s ${cl_proto} \
11349e9d176dSGeliang Tang						$extra_cl_args $connect_addr > "$cout" &
11359e9d176dSGeliang Tang	else
11369e9d176dSGeliang Tang		tee "$cinsent" < "$cinfail" | \
11379e9d176dSGeliang Tang			timeout ${timeout_test} \
11389e9d176dSGeliang Tang				ip netns exec ${connector_ns} \
11399e9d176dSGeliang Tang					./mptcp_connect -t ${timeout_poll} -p $port -s ${cl_proto} \
11409e9d176dSGeliang Tang						$extra_cl_args $connect_addr > "$cout" &
11419e9d176dSGeliang Tang	fi
11429e9d176dSGeliang Tang	local cpid=$!
11439e9d176dSGeliang Tang
11449e9d176dSGeliang Tang	pm_nl_set_endpoint $listener_ns $connector_ns $connect_addr
1145718eb44eSGeliang Tang
1146b08fbf24SPaolo Abeni	wait $cpid
11471e777bd8SMatthieu Baerts	local retc=$?
1148b08fbf24SPaolo Abeni	wait $spid
11491e777bd8SMatthieu Baerts	local rets=$?
1150b08fbf24SPaolo Abeni
1151b08fbf24SPaolo Abeni	if [ $capture -eq 1 ]; then
1152b08fbf24SPaolo Abeni	    sleep 1
1153b08fbf24SPaolo Abeni	    kill $cappid
1154b08fbf24SPaolo Abeni	fi
1155b08fbf24SPaolo Abeni
1156c2a55e8fSMatthieu Baerts	NSTAT_HISTORY=/tmp/${listener_ns}.nstat ip netns exec ${listener_ns} \
1157c2a55e8fSMatthieu Baerts		nstat | grep Tcp > /tmp/${listener_ns}.out
1158c2a55e8fSMatthieu Baerts	NSTAT_HISTORY=/tmp/${connector_ns}.nstat ip netns exec ${connector_ns} \
1159c2a55e8fSMatthieu Baerts		nstat | grep Tcp > /tmp/${connector_ns}.out
1160c2a55e8fSMatthieu Baerts
1161b08fbf24SPaolo Abeni	if [ ${rets} -ne 0 ] || [ ${retc} -ne 0 ]; then
116203668c65SMatthieu Baerts		fail_test "client exit code $retc, server $rets"
11638b974778SMatthieu Baerts		echo -e "\nnetns ${listener_ns} socket stat for ${port}:" 1>&2
1164c2a55e8fSMatthieu Baerts		ip netns exec ${listener_ns} ss -Menita 1>&2 -o "sport = :$port"
1165c2a55e8fSMatthieu Baerts		cat /tmp/${listener_ns}.out
11668b974778SMatthieu Baerts		echo -e "\nnetns ${connector_ns} socket stat for ${port}:" 1>&2
1167c2a55e8fSMatthieu Baerts		ip netns exec ${connector_ns} ss -Menita 1>&2 -o "dport = :$port"
1168c2a55e8fSMatthieu Baerts		cat /tmp/${connector_ns}.out
1169b08fbf24SPaolo Abeni
1170b08fbf24SPaolo Abeni		cat "$capout"
1171b08fbf24SPaolo Abeni		return 1
1172b08fbf24SPaolo Abeni	fi
1173b08fbf24SPaolo Abeni
11740c93af1fSGeliang Tang	if [ "$test_linkfail" -gt 1 ];then
11756bf41020SPaolo Abeni		check_transfer $sinfail $cout "file received by client" $trunc_size
11767d1e6f16SPaolo Abeni	else
11776bf41020SPaolo Abeni		check_transfer $sin $cout "file received by client" $trunc_size
11787d1e6f16SPaolo Abeni	fi
1179b08fbf24SPaolo Abeni	retc=$?
11800c93af1fSGeliang Tang	if [ "$test_linkfail" -eq 0 ];then
11816bf41020SPaolo Abeni		check_transfer $cin $sout "file received by server" $trunc_size
11828b819a84SFlorian Westphal	else
11836bf41020SPaolo Abeni		check_transfer $cinsent $sout "file received by server" $trunc_size
11848b819a84SFlorian Westphal	fi
1185b08fbf24SPaolo Abeni	rets=$?
1186b08fbf24SPaolo Abeni
1187b08fbf24SPaolo Abeni	if [ $retc -eq 0 ] && [ $rets -eq 0 ];then
1188b08fbf24SPaolo Abeni		cat "$capout"
1189b08fbf24SPaolo Abeni		return 0
1190b08fbf24SPaolo Abeni	fi
1191b08fbf24SPaolo Abeni
1192b08fbf24SPaolo Abeni	cat "$capout"
1193b08fbf24SPaolo Abeni	return 1
1194b08fbf24SPaolo Abeni}
1195b08fbf24SPaolo Abeni
1196b08fbf24SPaolo Abenimake_file()
1197b08fbf24SPaolo Abeni{
11981e777bd8SMatthieu Baerts	local name=$1
11991e777bd8SMatthieu Baerts	local who=$2
12001e777bd8SMatthieu Baerts	local size=$3
1201b08fbf24SPaolo Abeni
12028b819a84SFlorian Westphal	dd if=/dev/urandom of="$name" bs=1024 count=$size 2> /dev/null
1203b08fbf24SPaolo Abeni	echo -e "\nMPTCP_TEST_FILE_END_MARKER" >> "$name"
1204b08fbf24SPaolo Abeni
120503668c65SMatthieu Baerts	print_info "Test file (size $size KB) for $who"
1206b08fbf24SPaolo Abeni}
1207b08fbf24SPaolo Abeni
1208b08fbf24SPaolo Abenirun_tests()
1209b08fbf24SPaolo Abeni{
12101e777bd8SMatthieu Baerts	local listener_ns="$1"
12111e777bd8SMatthieu Baerts	local connector_ns="$2"
12121e777bd8SMatthieu Baerts	local connect_addr="$3"
12131e777bd8SMatthieu Baerts
12141e777bd8SMatthieu Baerts	local size
1215662aa22dSGeliang Tang	local test_linkfail=${test_linkfail:-0}
12168b819a84SFlorian Westphal
121734b572b7SGeliang Tang	# The values above 2 are reused to make test files
121834b572b7SGeliang Tang	# with the given sizes (KB)
121934b572b7SGeliang Tang	if [ "$test_linkfail" -gt 2 ]; then
122034b572b7SGeliang Tang		size=$test_linkfail
122134b572b7SGeliang Tang
122234b572b7SGeliang Tang		if [ -z "$cinfail" ]; then
122334b572b7SGeliang Tang			cinfail=$(mktemp)
122434b572b7SGeliang Tang		fi
122534b572b7SGeliang Tang		make_file "$cinfail" "client" $size
12267d1e6f16SPaolo Abeni	# create the input file for the failure test when
12277d1e6f16SPaolo Abeni	# the first failure test run
1228d8d08302SMatthieu Baerts	elif [ "$test_linkfail" -ne 0 ] && [ -z "$cinfail" ]; then
12297d1e6f16SPaolo Abeni		# the client file must be considerably larger
12307d1e6f16SPaolo Abeni		# of the maximum expected cwin value, or the
12317d1e6f16SPaolo Abeni		# link utilization will be not predicable
12327d1e6f16SPaolo Abeni		size=$((RANDOM%2))
12338b819a84SFlorian Westphal		size=$((size+1))
12347d1e6f16SPaolo Abeni		size=$((size*8192))
1235d8d08302SMatthieu Baerts		size=$((size + ( RANDOM % 8192) ))
12368b819a84SFlorian Westphal
12377d1e6f16SPaolo Abeni		cinfail=$(mktemp)
12387d1e6f16SPaolo Abeni		make_file "$cinfail" "client" $size
12397d1e6f16SPaolo Abeni	fi
12407d1e6f16SPaolo Abeni
124134b572b7SGeliang Tang	if [ "$test_linkfail" -gt 2 ]; then
124234b572b7SGeliang Tang		size=$test_linkfail
124334b572b7SGeliang Tang
124434b572b7SGeliang Tang		if [ -z "$sinfail" ]; then
124534b572b7SGeliang Tang			sinfail=$(mktemp)
124634b572b7SGeliang Tang		fi
124734b572b7SGeliang Tang		make_file "$sinfail" "server" $size
1248d8d08302SMatthieu Baerts	elif [ "$test_linkfail" -eq 2 ] && [ -z "$sinfail" ]; then
12497d1e6f16SPaolo Abeni		size=$((RANDOM%16))
12507d1e6f16SPaolo Abeni		size=$((size+1))
12517d1e6f16SPaolo Abeni		size=$((size*2048))
12527d1e6f16SPaolo Abeni
12537d1e6f16SPaolo Abeni		sinfail=$(mktemp)
12547d1e6f16SPaolo Abeni		make_file "$sinfail" "server" $size
12558b819a84SFlorian Westphal	fi
1256b08fbf24SPaolo Abeni
1257e571fb09SGeliang Tang	do_transfer ${listener_ns} ${connector_ns} MPTCP MPTCP ${connect_addr}
1258b08fbf24SPaolo Abeni}
1259b08fbf24SPaolo Abeni
1260327b9a94SPaolo Abenidump_stats()
1261327b9a94SPaolo Abeni{
1262327b9a94SPaolo Abeni	echo Server ns stats
1263327b9a94SPaolo Abeni	ip netns exec $ns1 nstat -as | grep Tcp
1264327b9a94SPaolo Abeni	echo Client ns stats
1265327b9a94SPaolo Abeni	ip netns exec $ns2 nstat -as | grep Tcp
1266327b9a94SPaolo Abeni}
1267327b9a94SPaolo Abeni
1268af66d3e1SGeliang Tangchk_csum_nr()
1269af66d3e1SGeliang Tang{
12703c082695SGeliang Tang	local csum_ns1=${1:-0}
12713c082695SGeliang Tang	local csum_ns2=${2:-0}
1272af66d3e1SGeliang Tang	local count
127353f368bfSGeliang Tang	local extra_msg=""
127426516e10SGeliang Tang	local allow_multi_errors_ns1=0
127526516e10SGeliang Tang	local allow_multi_errors_ns2=0
127626516e10SGeliang Tang
127726516e10SGeliang Tang	if [[ "${csum_ns1}" = "+"* ]]; then
127826516e10SGeliang Tang		allow_multi_errors_ns1=1
127926516e10SGeliang Tang		csum_ns1=${csum_ns1:1}
128026516e10SGeliang Tang	fi
128126516e10SGeliang Tang	if [[ "${csum_ns2}" = "+"* ]]; then
128226516e10SGeliang Tang		allow_multi_errors_ns2=1
128326516e10SGeliang Tang		csum_ns2=${csum_ns2:1}
128426516e10SGeliang Tang	fi
1285af66d3e1SGeliang Tang
128603668c65SMatthieu Baerts	print_check "sum"
128747867f0aSMatthieu Baerts	count=$(get_counter ${ns1} "MPTcpExtDataCsumErr")
128853f368bfSGeliang Tang	if [ "$count" != "$csum_ns1" ]; then
128953f368bfSGeliang Tang		extra_msg="$extra_msg ns1=$count"
129053f368bfSGeliang Tang	fi
129147867f0aSMatthieu Baerts	if [ -z "$count" ]; then
129203668c65SMatthieu Baerts		print_skip
129347867f0aSMatthieu Baerts	elif { [ "$count" != $csum_ns1 ] && [ $allow_multi_errors_ns1 -eq 0 ]; } ||
1294d8d08302SMatthieu Baerts	   { [ "$count" -lt $csum_ns1 ] && [ $allow_multi_errors_ns1 -eq 1 ]; }; then
129503668c65SMatthieu Baerts		fail_test "got $count data checksum error[s] expected $csum_ns1"
1296af66d3e1SGeliang Tang	else
129703668c65SMatthieu Baerts		print_ok
1298af66d3e1SGeliang Tang	fi
129903668c65SMatthieu Baerts	print_check "csum"
130047867f0aSMatthieu Baerts	count=$(get_counter ${ns2} "MPTcpExtDataCsumErr")
130153f368bfSGeliang Tang	if [ "$count" != "$csum_ns2" ]; then
130253f368bfSGeliang Tang		extra_msg="$extra_msg ns2=$count"
130353f368bfSGeliang Tang	fi
130447867f0aSMatthieu Baerts	if [ -z "$count" ]; then
130503668c65SMatthieu Baerts		print_skip
130647867f0aSMatthieu Baerts	elif { [ "$count" != $csum_ns2 ] && [ $allow_multi_errors_ns2 -eq 0 ]; } ||
1307d8d08302SMatthieu Baerts	   { [ "$count" -lt $csum_ns2 ] && [ $allow_multi_errors_ns2 -eq 1 ]; }; then
130803668c65SMatthieu Baerts		fail_test "got $count data checksum error[s] expected $csum_ns2"
1309af66d3e1SGeliang Tang	else
131003668c65SMatthieu Baerts		print_ok
1311af66d3e1SGeliang Tang	fi
131253f368bfSGeliang Tang
131303668c65SMatthieu Baerts	print_info "$extra_msg"
1314af66d3e1SGeliang Tang}
1315af66d3e1SGeliang Tang
13166bb3ab49SGeliang Tangchk_fail_nr()
13176bb3ab49SGeliang Tang{
131826516e10SGeliang Tang	local fail_tx=$1
131926516e10SGeliang Tang	local fail_rx=$2
13201f7d325fSGeliang Tang	local ns_invert=${3:-""}
13216bb3ab49SGeliang Tang	local count
13221f7d325fSGeliang Tang	local ns_tx=$ns1
13231f7d325fSGeliang Tang	local ns_rx=$ns2
13241f7d325fSGeliang Tang	local extra_msg=""
13251f7d325fSGeliang Tang	local allow_tx_lost=0
13261f7d325fSGeliang Tang	local allow_rx_lost=0
13271f7d325fSGeliang Tang
13281f7d325fSGeliang Tang	if [[ $ns_invert = "invert" ]]; then
13291f7d325fSGeliang Tang		ns_tx=$ns2
13301f7d325fSGeliang Tang		ns_rx=$ns1
13311f7d325fSGeliang Tang		extra_msg="invert"
13321f7d325fSGeliang Tang	fi
13331f7d325fSGeliang Tang
13341f7d325fSGeliang Tang	if [[ "${fail_tx}" = "-"* ]]; then
13351f7d325fSGeliang Tang		allow_tx_lost=1
13361f7d325fSGeliang Tang		fail_tx=${fail_tx:1}
13371f7d325fSGeliang Tang	fi
13381f7d325fSGeliang Tang	if [[ "${fail_rx}" = "-"* ]]; then
13391f7d325fSGeliang Tang		allow_rx_lost=1
13401f7d325fSGeliang Tang		fail_rx=${fail_rx:1}
13411f7d325fSGeliang Tang	fi
13426bb3ab49SGeliang Tang
134303668c65SMatthieu Baerts	print_check "ftx"
134447867f0aSMatthieu Baerts	count=$(get_counter ${ns_tx} "MPTcpExtMPFailTx")
134526516e10SGeliang Tang	if [ "$count" != "$fail_tx" ]; then
13461f7d325fSGeliang Tang		extra_msg="$extra_msg,tx=$count"
13471f7d325fSGeliang Tang	fi
134847867f0aSMatthieu Baerts	if [ -z "$count" ]; then
134903668c65SMatthieu Baerts		print_skip
135047867f0aSMatthieu Baerts	elif { [ "$count" != "$fail_tx" ] && [ $allow_tx_lost -eq 0 ]; } ||
13511f7d325fSGeliang Tang	   { [ "$count" -gt "$fail_tx" ] && [ $allow_tx_lost -eq 1 ]; }; then
135203668c65SMatthieu Baerts		fail_test "got $count MP_FAIL[s] TX expected $fail_tx"
13536bb3ab49SGeliang Tang	else
135403668c65SMatthieu Baerts		print_ok
13556bb3ab49SGeliang Tang	fi
13566bb3ab49SGeliang Tang
135703668c65SMatthieu Baerts	print_check "failrx"
135847867f0aSMatthieu Baerts	count=$(get_counter ${ns_rx} "MPTcpExtMPFailRx")
135926516e10SGeliang Tang	if [ "$count" != "$fail_rx" ]; then
13601f7d325fSGeliang Tang		extra_msg="$extra_msg,rx=$count"
13611f7d325fSGeliang Tang	fi
136247867f0aSMatthieu Baerts	if [ -z "$count" ]; then
136303668c65SMatthieu Baerts		print_skip
136447867f0aSMatthieu Baerts	elif { [ "$count" != "$fail_rx" ] && [ $allow_rx_lost -eq 0 ]; } ||
13651f7d325fSGeliang Tang	   { [ "$count" -gt "$fail_rx" ] && [ $allow_rx_lost -eq 1 ]; }; then
136603668c65SMatthieu Baerts		fail_test "got $count MP_FAIL[s] RX expected $fail_rx"
13676bb3ab49SGeliang Tang	else
136803668c65SMatthieu Baerts		print_ok
13696bb3ab49SGeliang Tang	fi
13706bb3ab49SGeliang Tang
137103668c65SMatthieu Baerts	print_info "$extra_msg"
13726bb3ab49SGeliang Tang}
13736bb3ab49SGeliang Tang
1374e8e947efSGeliang Tangchk_fclose_nr()
1375e8e947efSGeliang Tang{
1376e8e947efSGeliang Tang	local fclose_tx=$1
1377e8e947efSGeliang Tang	local fclose_rx=$2
13786bf41020SPaolo Abeni	local ns_invert=$3
1379e8e947efSGeliang Tang	local count
13806bf41020SPaolo Abeni	local ns_tx=$ns2
13816bf41020SPaolo Abeni	local ns_rx=$ns1
13826bf41020SPaolo Abeni	local extra_msg=""
13836bf41020SPaolo Abeni
13846bf41020SPaolo Abeni	if [[ $ns_invert = "invert" ]]; then
13856bf41020SPaolo Abeni		ns_tx=$ns1
13866bf41020SPaolo Abeni		ns_rx=$ns2
138703668c65SMatthieu Baerts		extra_msg="invert"
13886bf41020SPaolo Abeni	fi
1389e8e947efSGeliang Tang
139003668c65SMatthieu Baerts	print_check "ctx"
139147867f0aSMatthieu Baerts	count=$(get_counter ${ns_tx} "MPTcpExtMPFastcloseTx")
139247867f0aSMatthieu Baerts	if [ -z "$count" ]; then
139303668c65SMatthieu Baerts		print_skip
139447867f0aSMatthieu Baerts	elif [ "$count" != "$fclose_tx" ]; then
139547867f0aSMatthieu Baerts		extra_msg="$extra_msg,tx=$count"
139603668c65SMatthieu Baerts		fail_test "got $count MP_FASTCLOSE[s] TX expected $fclose_tx"
1397e8e947efSGeliang Tang	else
139803668c65SMatthieu Baerts		print_ok
1399e8e947efSGeliang Tang	fi
1400e8e947efSGeliang Tang
140103668c65SMatthieu Baerts	print_check "fclzrx"
140247867f0aSMatthieu Baerts	count=$(get_counter ${ns_rx} "MPTcpExtMPFastcloseRx")
140347867f0aSMatthieu Baerts	if [ -z "$count" ]; then
140403668c65SMatthieu Baerts		print_skip
140547867f0aSMatthieu Baerts	elif [ "$count" != "$fclose_rx" ]; then
140647867f0aSMatthieu Baerts		extra_msg="$extra_msg,rx=$count"
140703668c65SMatthieu Baerts		fail_test "got $count MP_FASTCLOSE[s] RX expected $fclose_rx"
1408e8e947efSGeliang Tang	else
140903668c65SMatthieu Baerts		print_ok
1410e8e947efSGeliang Tang	fi
1411e8e947efSGeliang Tang
141203668c65SMatthieu Baerts	print_info "$extra_msg"
1413e8e947efSGeliang Tang}
1414e8e947efSGeliang Tang
1415922fd2b3SGeliang Tangchk_rst_nr()
1416922fd2b3SGeliang Tang{
1417922fd2b3SGeliang Tang	local rst_tx=$1
1418922fd2b3SGeliang Tang	local rst_rx=$2
1419922fd2b3SGeliang Tang	local ns_invert=${3:-""}
1420922fd2b3SGeliang Tang	local count
1421922fd2b3SGeliang Tang	local ns_tx=$ns1
1422922fd2b3SGeliang Tang	local ns_rx=$ns2
1423922fd2b3SGeliang Tang	local extra_msg=""
1424922fd2b3SGeliang Tang
1425922fd2b3SGeliang Tang	if [[ $ns_invert = "invert" ]]; then
1426922fd2b3SGeliang Tang		ns_tx=$ns2
1427922fd2b3SGeliang Tang		ns_rx=$ns1
1428922fd2b3SGeliang Tang		extra_msg="invert"
1429922fd2b3SGeliang Tang	fi
1430922fd2b3SGeliang Tang
143103668c65SMatthieu Baerts	print_check "rtx"
143247867f0aSMatthieu Baerts	count=$(get_counter ${ns_tx} "MPTcpExtMPRstTx")
143347867f0aSMatthieu Baerts	if [ -z "$count" ]; then
143403668c65SMatthieu Baerts		print_skip
1435b134a580SMatthieu Baerts	# accept more rst than expected except if we don't expect any
1436b134a580SMatthieu Baerts	elif { [ $rst_tx -ne 0 ] && [ $count -lt $rst_tx ]; } ||
1437b134a580SMatthieu Baerts	     { [ $rst_tx -eq 0 ] && [ $count -ne 0 ]; }; then
143803668c65SMatthieu Baerts		fail_test "got $count MP_RST[s] TX expected $rst_tx"
1439922fd2b3SGeliang Tang	else
144003668c65SMatthieu Baerts		print_ok
1441922fd2b3SGeliang Tang	fi
1442922fd2b3SGeliang Tang
144303668c65SMatthieu Baerts	print_check "rstrx"
144447867f0aSMatthieu Baerts	count=$(get_counter ${ns_rx} "MPTcpExtMPRstRx")
144547867f0aSMatthieu Baerts	if [ -z "$count" ]; then
144603668c65SMatthieu Baerts		print_skip
1447b134a580SMatthieu Baerts	# accept more rst than expected except if we don't expect any
1448b134a580SMatthieu Baerts	elif { [ $rst_rx -ne 0 ] && [ $count -lt $rst_rx ]; } ||
1449b134a580SMatthieu Baerts	     { [ $rst_rx -eq 0 ] && [ $count -ne 0 ]; }; then
145003668c65SMatthieu Baerts		fail_test "got $count MP_RST[s] RX expected $rst_rx"
1451922fd2b3SGeliang Tang	else
145203668c65SMatthieu Baerts		print_ok
1453922fd2b3SGeliang Tang	fi
1454922fd2b3SGeliang Tang
145503668c65SMatthieu Baerts	print_info "$extra_msg"
1456922fd2b3SGeliang Tang}
1457922fd2b3SGeliang Tang
14588bd03be3SGeliang Tangchk_infi_nr()
14598bd03be3SGeliang Tang{
14608bd03be3SGeliang Tang	local infi_tx=$1
14618bd03be3SGeliang Tang	local infi_rx=$2
14628bd03be3SGeliang Tang	local count
14638bd03be3SGeliang Tang
146403668c65SMatthieu Baerts	print_check "itx"
146547867f0aSMatthieu Baerts	count=$(get_counter ${ns2} "MPTcpExtInfiniteMapTx")
146647867f0aSMatthieu Baerts	if [ -z "$count" ]; then
146703668c65SMatthieu Baerts		print_skip
146847867f0aSMatthieu Baerts	elif [ "$count" != "$infi_tx" ]; then
146903668c65SMatthieu Baerts		fail_test "got $count infinite map[s] TX expected $infi_tx"
14708bd03be3SGeliang Tang	else
147103668c65SMatthieu Baerts		print_ok
14728bd03be3SGeliang Tang	fi
14738bd03be3SGeliang Tang
147403668c65SMatthieu Baerts	print_check "infirx"
147547867f0aSMatthieu Baerts	count=$(get_counter ${ns1} "MPTcpExtInfiniteMapRx")
147647867f0aSMatthieu Baerts	if [ -z "$count" ]; then
147703668c65SMatthieu Baerts		print_skip
147847867f0aSMatthieu Baerts	elif [ "$count" != "$infi_rx" ]; then
147903668c65SMatthieu Baerts		fail_test "got $count infinite map[s] RX expected $infi_rx"
14808bd03be3SGeliang Tang	else
148103668c65SMatthieu Baerts		print_ok
14828bd03be3SGeliang Tang	fi
14838bd03be3SGeliang Tang}
14848bd03be3SGeliang Tang
1485b08fbf24SPaolo Abenichk_join_nr()
1486b08fbf24SPaolo Abeni{
1487c7d49c03SMatthieu Baerts	local syn_nr=$1
1488c7d49c03SMatthieu Baerts	local syn_ack_nr=$2
1489c7d49c03SMatthieu Baerts	local ack_nr=$3
1490c7d49c03SMatthieu Baerts	local csum_ns1=${4:-0}
1491c7d49c03SMatthieu Baerts	local csum_ns2=${5:-0}
1492c7d49c03SMatthieu Baerts	local fail_nr=${6:-0}
1493c7d49c03SMatthieu Baerts	local rst_nr=${7:-0}
14948bd03be3SGeliang Tang	local infi_nr=${8:-0}
14958bd03be3SGeliang Tang	local corrupted_pkts=${9:-0}
1496b08fbf24SPaolo Abeni	local count
1497e35f885bSPaolo Abeni	local with_cookie
1498b08fbf24SPaolo Abeni
1499c7d49c03SMatthieu Baerts	if [ "${corrupted_pkts}" -gt 0 ]; then
150003668c65SMatthieu Baerts		print_info "${corrupted_pkts} corrupted pkts"
1501c7d49c03SMatthieu Baerts	fi
1502c7d49c03SMatthieu Baerts
150303668c65SMatthieu Baerts	print_check "syn"
150447867f0aSMatthieu Baerts	count=$(get_counter ${ns1} "MPTcpExtMPJoinSynRx")
150547867f0aSMatthieu Baerts	if [ -z "$count" ]; then
150603668c65SMatthieu Baerts		print_skip
150747867f0aSMatthieu Baerts	elif [ "$count" != "$syn_nr" ]; then
150803668c65SMatthieu Baerts		fail_test "got $count JOIN[s] syn expected $syn_nr"
1509b08fbf24SPaolo Abeni	else
151003668c65SMatthieu Baerts		print_ok
1511b08fbf24SPaolo Abeni	fi
1512b08fbf24SPaolo Abeni
151303668c65SMatthieu Baerts	print_check "synack"
15144bfadd71SMatthieu Baerts	with_cookie=$(ip netns exec $ns2 sysctl -n net.ipv4.tcp_syncookies)
151547867f0aSMatthieu Baerts	count=$(get_counter ${ns2} "MPTcpExtMPJoinSynAckRx")
151647867f0aSMatthieu Baerts	if [ -z "$count" ]; then
151703668c65SMatthieu Baerts		print_skip
151847867f0aSMatthieu Baerts	elif [ "$count" != "$syn_ack_nr" ]; then
1519e35f885bSPaolo Abeni		# simult connections exceeding the limit with cookie enabled could go up to
1520e35f885bSPaolo Abeni		# synack validation as the conn limit can be enforced reliably only after
1521e35f885bSPaolo Abeni		# the subflow creation
1522e35f885bSPaolo Abeni		if [ "$with_cookie" = 2 ] && [ "$count" -gt "$syn_ack_nr" ] && [ "$count" -le "$syn_nr" ]; then
152303668c65SMatthieu Baerts			print_ok
1524e35f885bSPaolo Abeni		else
152503668c65SMatthieu Baerts			fail_test "got $count JOIN[s] synack expected $syn_ack_nr"
1526e35f885bSPaolo Abeni		fi
1527b08fbf24SPaolo Abeni	else
152803668c65SMatthieu Baerts		print_ok
1529b08fbf24SPaolo Abeni	fi
1530b08fbf24SPaolo Abeni
153103668c65SMatthieu Baerts	print_check "ack"
153247867f0aSMatthieu Baerts	count=$(get_counter ${ns1} "MPTcpExtMPJoinAckRx")
153347867f0aSMatthieu Baerts	if [ -z "$count" ]; then
153403668c65SMatthieu Baerts		print_skip
153547867f0aSMatthieu Baerts	elif [ "$count" != "$ack_nr" ]; then
153603668c65SMatthieu Baerts		fail_test "got $count JOIN[s] ack expected $ack_nr"
1537b08fbf24SPaolo Abeni	else
153803668c65SMatthieu Baerts		print_ok
1539b08fbf24SPaolo Abeni	fi
1540b6e074e1SGeliang Tang	if [ $validate_checksum -eq 1 ]; then
15413c082695SGeliang Tang		chk_csum_nr $csum_ns1 $csum_ns2
154226516e10SGeliang Tang		chk_fail_nr $fail_nr $fail_nr
154326516e10SGeliang Tang		chk_rst_nr $rst_nr $rst_nr
15448bd03be3SGeliang Tang		chk_infi_nr $infi_nr $infi_nr
1545af66d3e1SGeliang Tang	fi
1546b08fbf24SPaolo Abeni}
1547b08fbf24SPaolo Abeni
15487d1e6f16SPaolo Abeni# a negative value for 'stale_max' means no upper bound:
15497d1e6f16SPaolo Abeni# for bidirectional transfer, if one peer sleep for a while
15507d1e6f16SPaolo Abeni# - as these tests do - we can have a quite high number of
15517d1e6f16SPaolo Abeni# stale/recover conversions, proportional to
15527d1e6f16SPaolo Abeni# sleep duration/ MPTCP-level RTX interval.
15537d1e6f16SPaolo Abenichk_stale_nr()
15547d1e6f16SPaolo Abeni{
15557d1e6f16SPaolo Abeni	local ns=$1
15567d1e6f16SPaolo Abeni	local stale_min=$2
15577d1e6f16SPaolo Abeni	local stale_max=$3
15587d1e6f16SPaolo Abeni	local stale_delta=$4
15597d1e6f16SPaolo Abeni	local dump_stats
15607d1e6f16SPaolo Abeni	local stale_nr
15617d1e6f16SPaolo Abeni	local recover_nr
15627d1e6f16SPaolo Abeni
156303668c65SMatthieu Baerts	print_check "stale"
15647d1e6f16SPaolo Abeni
156547867f0aSMatthieu Baerts	stale_nr=$(get_counter ${ns} "MPTcpExtSubflowStale")
156647867f0aSMatthieu Baerts	recover_nr=$(get_counter ${ns} "MPTcpExtSubflowRecover")
156747867f0aSMatthieu Baerts	if [ -z "$stale_nr" ] || [ -z "$recover_nr" ]; then
156803668c65SMatthieu Baerts		print_skip
156947867f0aSMatthieu Baerts	elif [ $stale_nr -lt $stale_min ] ||
1570d8d08302SMatthieu Baerts	   { [ $stale_max -gt 0 ] && [ $stale_nr -gt $stale_max ]; } ||
1571d8d08302SMatthieu Baerts	   [ $((stale_nr - recover_nr)) -ne $stale_delta ]; then
157203668c65SMatthieu Baerts		fail_test "got $stale_nr stale[s] $recover_nr recover[s], " \
15737d1e6f16SPaolo Abeni		     " expected stale in range [$stale_min..$stale_max]," \
15747d1e6f16SPaolo Abeni		     " stale-recover delta $stale_delta"
15757d1e6f16SPaolo Abeni		dump_stats=1
15767d1e6f16SPaolo Abeni	else
157703668c65SMatthieu Baerts		print_ok
15787d1e6f16SPaolo Abeni	fi
15797d1e6f16SPaolo Abeni
15807d1e6f16SPaolo Abeni	if [ "${dump_stats}" = 1 ]; then
15817d1e6f16SPaolo Abeni		echo $ns stats
15827d1e6f16SPaolo Abeni		ip netns exec $ns ip -s link show
15837d1e6f16SPaolo Abeni		ip netns exec $ns nstat -as | grep MPTcp
15847d1e6f16SPaolo Abeni	fi
15857d1e6f16SPaolo Abeni}
15867d1e6f16SPaolo Abeni
1587be613160SGeliang Tangchk_add_nr()
1588be613160SGeliang Tang{
1589be613160SGeliang Tang	local add_nr=$1
1590be613160SGeliang Tang	local echo_nr=$2
15918a127bf6SGeliang Tang	local port_nr=${3:-0}
15928a127bf6SGeliang Tang	local syn_nr=${4:-$port_nr}
15938a127bf6SGeliang Tang	local syn_ack_nr=${5:-$port_nr}
15948a127bf6SGeliang Tang	local ack_nr=${6:-$port_nr}
15958a127bf6SGeliang Tang	local mis_syn_nr=${7:-0}
15968a127bf6SGeliang Tang	local mis_ack_nr=${8:-0}
1597be613160SGeliang Tang	local count
15986ef84b15SPaolo Abeni	local timeout
15996ef84b15SPaolo Abeni
16004bfadd71SMatthieu Baerts	timeout=$(ip netns exec $ns1 sysctl -n net.mptcp.add_addr_timeout)
1601be613160SGeliang Tang
160203668c65SMatthieu Baerts	print_check "add"
160347867f0aSMatthieu Baerts	count=$(get_counter ${ns2} "MPTcpExtAddAddr")
160447867f0aSMatthieu Baerts	if [ -z "$count" ]; then
160503668c65SMatthieu Baerts		print_skip
16066ef84b15SPaolo Abeni	# if the test configured a short timeout tolerate greater then expected
16076ef84b15SPaolo Abeni	# add addrs options, due to retransmissions
160847867f0aSMatthieu Baerts	elif [ "$count" != "$add_nr" ] && { [ "$timeout" -gt 1 ] || [ "$count" -lt "$add_nr" ]; }; then
160903668c65SMatthieu Baerts		fail_test "got $count ADD_ADDR[s] expected $add_nr"
1610be613160SGeliang Tang	else
161103668c65SMatthieu Baerts		print_ok
1612be613160SGeliang Tang	fi
1613be613160SGeliang Tang
161403668c65SMatthieu Baerts	print_check "echo"
161547867f0aSMatthieu Baerts	count=$(get_counter ${ns1} "MPTcpExtEchoAdd")
161647867f0aSMatthieu Baerts	if [ -z "$count" ]; then
161703668c65SMatthieu Baerts		print_skip
161847867f0aSMatthieu Baerts	elif [ "$count" != "$echo_nr" ]; then
161903668c65SMatthieu Baerts		fail_test "got $count ADD_ADDR echo[s] expected $echo_nr"
1620be613160SGeliang Tang	else
162103668c65SMatthieu Baerts		print_ok
16228a127bf6SGeliang Tang	fi
16238a127bf6SGeliang Tang
16248a127bf6SGeliang Tang	if [ $port_nr -gt 0 ]; then
162503668c65SMatthieu Baerts		print_check "pt"
162647867f0aSMatthieu Baerts		count=$(get_counter ${ns2} "MPTcpExtPortAdd")
162747867f0aSMatthieu Baerts		if [ -z "$count" ]; then
162803668c65SMatthieu Baerts			print_skip
162947867f0aSMatthieu Baerts		elif [ "$count" != "$port_nr" ]; then
163003668c65SMatthieu Baerts			fail_test "got $count ADD_ADDR[s] with a port-number expected $port_nr"
16318a127bf6SGeliang Tang		else
163203668c65SMatthieu Baerts			print_ok
1633be613160SGeliang Tang		fi
1634be613160SGeliang Tang
163503668c65SMatthieu Baerts		print_check "syn"
163647867f0aSMatthieu Baerts		count=$(get_counter ${ns1} "MPTcpExtMPJoinPortSynRx")
163747867f0aSMatthieu Baerts		if [ -z "$count" ]; then
163803668c65SMatthieu Baerts			print_skip
163947867f0aSMatthieu Baerts		elif [ "$count" != "$syn_nr" ]; then
164003668c65SMatthieu Baerts			fail_test "got $count JOIN[s] syn with a different \
16418a127bf6SGeliang Tang				   port-number expected $syn_nr"
16428a127bf6SGeliang Tang		else
164303668c65SMatthieu Baerts			print_ok
16448a127bf6SGeliang Tang		fi
16458a127bf6SGeliang Tang
164603668c65SMatthieu Baerts		print_check "synack"
164747867f0aSMatthieu Baerts		count=$(get_counter ${ns2} "MPTcpExtMPJoinPortSynAckRx")
164847867f0aSMatthieu Baerts		if [ -z "$count" ]; then
164903668c65SMatthieu Baerts			print_skip
165047867f0aSMatthieu Baerts		elif [ "$count" != "$syn_ack_nr" ]; then
165103668c65SMatthieu Baerts			fail_test "got $count JOIN[s] synack with a different \
16528a127bf6SGeliang Tang				   port-number expected $syn_ack_nr"
16538a127bf6SGeliang Tang		else
165403668c65SMatthieu Baerts			print_ok
16558a127bf6SGeliang Tang		fi
16568a127bf6SGeliang Tang
165703668c65SMatthieu Baerts		print_check "ack"
165847867f0aSMatthieu Baerts		count=$(get_counter ${ns1} "MPTcpExtMPJoinPortAckRx")
165947867f0aSMatthieu Baerts		if [ -z "$count" ]; then
166003668c65SMatthieu Baerts			print_skip
166147867f0aSMatthieu Baerts		elif [ "$count" != "$ack_nr" ]; then
166203668c65SMatthieu Baerts			fail_test "got $count JOIN[s] ack with a different \
16638a127bf6SGeliang Tang				   port-number expected $ack_nr"
16648a127bf6SGeliang Tang		else
166503668c65SMatthieu Baerts			print_ok
16668a127bf6SGeliang Tang		fi
16678a127bf6SGeliang Tang
166803668c65SMatthieu Baerts		print_check "syn"
166947867f0aSMatthieu Baerts		count=$(get_counter ${ns1} "MPTcpExtMismatchPortSynRx")
167047867f0aSMatthieu Baerts		if [ -z "$count" ]; then
167103668c65SMatthieu Baerts			print_skip
167247867f0aSMatthieu Baerts		elif [ "$count" != "$mis_syn_nr" ]; then
167303668c65SMatthieu Baerts			fail_test "got $count JOIN[s] syn with a mismatched \
16748a127bf6SGeliang Tang				   port-number expected $mis_syn_nr"
16758a127bf6SGeliang Tang		else
167603668c65SMatthieu Baerts			print_ok
16778a127bf6SGeliang Tang		fi
16788a127bf6SGeliang Tang
167903668c65SMatthieu Baerts		print_check "ack"
168047867f0aSMatthieu Baerts		count=$(get_counter ${ns1} "MPTcpExtMismatchPortAckRx")
168147867f0aSMatthieu Baerts		if [ -z "$count" ]; then
168203668c65SMatthieu Baerts			print_skip
168347867f0aSMatthieu Baerts		elif [ "$count" != "$mis_ack_nr" ]; then
168403668c65SMatthieu Baerts			fail_test "got $count JOIN[s] ack with a mismatched \
16858a127bf6SGeliang Tang				   port-number expected $mis_ack_nr"
16868a127bf6SGeliang Tang		else
168703668c65SMatthieu Baerts			print_ok
16888a127bf6SGeliang Tang		fi
16898a127bf6SGeliang Tang	fi
1690be613160SGeliang Tang}
1691be613160SGeliang Tang
16920639fa23SPaolo Abenichk_add_tx_nr()
16930639fa23SPaolo Abeni{
16940639fa23SPaolo Abeni	local add_tx_nr=$1
16950639fa23SPaolo Abeni	local echo_tx_nr=$2
16960639fa23SPaolo Abeni	local timeout
16970639fa23SPaolo Abeni	local count
16980639fa23SPaolo Abeni
16990639fa23SPaolo Abeni	timeout=$(ip netns exec $ns1 sysctl -n net.mptcp.add_addr_timeout)
17000639fa23SPaolo Abeni
170103668c65SMatthieu Baerts	print_check "add TX"
170200079f18SMatthieu Baerts	count=$(get_counter ${ns1} "MPTcpExtAddAddrTx")
170300079f18SMatthieu Baerts	if [ -z "$count" ]; then
170403668c65SMatthieu Baerts		print_skip
17050639fa23SPaolo Abeni	# if the test configured a short timeout tolerate greater then expected
17060639fa23SPaolo Abeni	# add addrs options, due to retransmissions
170700079f18SMatthieu Baerts	elif [ "$count" != "$add_tx_nr" ] && { [ "$timeout" -gt 1 ] || [ "$count" -lt "$add_tx_nr" ]; }; then
170803668c65SMatthieu Baerts		fail_test "got $count ADD_ADDR[s] TX, expected $add_tx_nr"
17090639fa23SPaolo Abeni	else
171003668c65SMatthieu Baerts		print_ok
17110639fa23SPaolo Abeni	fi
17120639fa23SPaolo Abeni
171303668c65SMatthieu Baerts	print_check "echo TX"
171400079f18SMatthieu Baerts	count=$(get_counter ${ns2} "MPTcpExtEchoAddTx")
171500079f18SMatthieu Baerts	if [ -z "$count" ]; then
171603668c65SMatthieu Baerts		print_skip
171700079f18SMatthieu Baerts	elif [ "$count" != "$echo_tx_nr" ]; then
171803668c65SMatthieu Baerts		fail_test "got $count ADD_ADDR echo[s] TX, expected $echo_tx_nr"
17190639fa23SPaolo Abeni	else
172003668c65SMatthieu Baerts		print_ok
17210639fa23SPaolo Abeni	fi
17220639fa23SPaolo Abeni}
17230639fa23SPaolo Abeni
1724dd72b0feSGeliang Tangchk_rm_nr()
1725dd72b0feSGeliang Tang{
1726dd72b0feSGeliang Tang	local rm_addr_nr=$1
1727dd72b0feSGeliang Tang	local rm_subflow_nr=$2
17286fa0174aSPaolo Abeni	local invert
17296fa0174aSPaolo Abeni	local simult
1730dd72b0feSGeliang Tang	local count
17317d9bf018SGeliang Tang	local addr_ns=$ns1
17327d9bf018SGeliang Tang	local subflow_ns=$ns2
17337d9bf018SGeliang Tang	local extra_msg=""
17347028ba8aSGeliang Tang
17356fa0174aSPaolo Abeni	shift 2
17366fa0174aSPaolo Abeni	while [ -n "$1" ]; do
17376fa0174aSPaolo Abeni		[ "$1" = "invert" ] && invert=true
17386fa0174aSPaolo Abeni		[ "$1" = "simult" ] && simult=true
17396fa0174aSPaolo Abeni		shift
17406fa0174aSPaolo Abeni	done
17416fa0174aSPaolo Abeni
17426fa0174aSPaolo Abeni	if [ -z $invert ]; then
17436fa0174aSPaolo Abeni		addr_ns=$ns1
17446fa0174aSPaolo Abeni		subflow_ns=$ns2
17456fa0174aSPaolo Abeni	elif [ $invert = "true" ]; then
17467028ba8aSGeliang Tang		addr_ns=$ns2
17477028ba8aSGeliang Tang		subflow_ns=$ns1
17487d9bf018SGeliang Tang		extra_msg="invert"
17497028ba8aSGeliang Tang	fi
1750dd72b0feSGeliang Tang
175103668c65SMatthieu Baerts	print_check "rm"
175200079f18SMatthieu Baerts	count=$(get_counter ${addr_ns} "MPTcpExtRmAddr")
175300079f18SMatthieu Baerts	if [ -z "$count" ]; then
175403668c65SMatthieu Baerts		print_skip
175500079f18SMatthieu Baerts	elif [ "$count" != "$rm_addr_nr" ]; then
175603668c65SMatthieu Baerts		fail_test "got $count RM_ADDR[s] expected $rm_addr_nr"
1757dd72b0feSGeliang Tang	else
175803668c65SMatthieu Baerts		print_ok
1759dd72b0feSGeliang Tang	fi
1760dd72b0feSGeliang Tang
176103668c65SMatthieu Baerts	print_check "rmsf"
176247867f0aSMatthieu Baerts	count=$(get_counter ${subflow_ns} "MPTcpExtRmSubflow")
176347867f0aSMatthieu Baerts	if [ -z "$count" ]; then
176403668c65SMatthieu Baerts		print_skip
176547867f0aSMatthieu Baerts	elif [ -n "$simult" ]; then
1766d8d08302SMatthieu Baerts		local cnt suffix
1767d8d08302SMatthieu Baerts
176847867f0aSMatthieu Baerts		cnt=$(get_counter ${addr_ns} "MPTcpExtRmSubflow")
17696fa0174aSPaolo Abeni
17706fa0174aSPaolo Abeni		# in case of simult flush, the subflow removal count on each side is
17716fa0174aSPaolo Abeni		# unreliable
17726fa0174aSPaolo Abeni		count=$((count + cnt))
17736fa0174aSPaolo Abeni		[ "$count" != "$rm_subflow_nr" ] && suffix="$count in [$rm_subflow_nr:$((rm_subflow_nr*2))]"
17746fa0174aSPaolo Abeni		if [ $count -ge "$rm_subflow_nr" ] && \
17756fa0174aSPaolo Abeni		   [ "$count" -le "$((rm_subflow_nr *2 ))" ]; then
177603668c65SMatthieu Baerts			print_ok "$suffix"
17776fa0174aSPaolo Abeni		else
177803668c65SMatthieu Baerts			fail_test "got $count RM_SUBFLOW[s] expected in range [$rm_subflow_nr:$((rm_subflow_nr*2))]"
17796fa0174aSPaolo Abeni		fi
178047867f0aSMatthieu Baerts	elif [ "$count" != "$rm_subflow_nr" ]; then
178103668c65SMatthieu Baerts		fail_test "got $count RM_SUBFLOW[s] expected $rm_subflow_nr"
1782dd72b0feSGeliang Tang	else
178303668c65SMatthieu Baerts		print_ok
1784dd72b0feSGeliang Tang	fi
1785dd72b0feSGeliang Tang
178603668c65SMatthieu Baerts	print_info "$extra_msg"
1787dd72b0feSGeliang Tang}
1788dd72b0feSGeliang Tang
17890639fa23SPaolo Abenichk_rm_tx_nr()
17900639fa23SPaolo Abeni{
17910639fa23SPaolo Abeni	local rm_addr_tx_nr=$1
17920639fa23SPaolo Abeni
179303668c65SMatthieu Baerts	print_check "rm TX"
179400079f18SMatthieu Baerts	count=$(get_counter ${ns2} "MPTcpExtRmAddrTx")
179500079f18SMatthieu Baerts	if [ -z "$count" ]; then
179603668c65SMatthieu Baerts		print_skip
179700079f18SMatthieu Baerts	elif [ "$count" != "$rm_addr_tx_nr" ]; then
179803668c65SMatthieu Baerts		fail_test "got $count RM_ADDR[s] expected $rm_addr_tx_nr"
17990639fa23SPaolo Abeni	else
180003668c65SMatthieu Baerts		print_ok
18010639fa23SPaolo Abeni	fi
18020639fa23SPaolo Abeni}
18030639fa23SPaolo Abeni
1804718eb44eSGeliang Tangchk_prio_nr()
1805718eb44eSGeliang Tang{
1806718eb44eSGeliang Tang	local mp_prio_nr_tx=$1
1807718eb44eSGeliang Tang	local mp_prio_nr_rx=$2
1808718eb44eSGeliang Tang	local count
1809718eb44eSGeliang Tang
181003668c65SMatthieu Baerts	print_check "ptx"
181147867f0aSMatthieu Baerts	count=$(get_counter ${ns1} "MPTcpExtMPPrioTx")
181247867f0aSMatthieu Baerts	if [ -z "$count" ]; then
181303668c65SMatthieu Baerts		print_skip
181447867f0aSMatthieu Baerts	elif [ "$count" != "$mp_prio_nr_tx" ]; then
181503668c65SMatthieu Baerts		fail_test "got $count MP_PRIO[s] TX expected $mp_prio_nr_tx"
1816718eb44eSGeliang Tang	else
181703668c65SMatthieu Baerts		print_ok
1818718eb44eSGeliang Tang	fi
1819718eb44eSGeliang Tang
182003668c65SMatthieu Baerts	print_check "prx"
182147867f0aSMatthieu Baerts	count=$(get_counter ${ns1} "MPTcpExtMPPrioRx")
182247867f0aSMatthieu Baerts	if [ -z "$count" ]; then
182303668c65SMatthieu Baerts		print_skip
182447867f0aSMatthieu Baerts	elif [ "$count" != "$mp_prio_nr_rx" ]; then
182503668c65SMatthieu Baerts		fail_test "got $count MP_PRIO[s] RX expected $mp_prio_nr_rx"
1826718eb44eSGeliang Tang	else
182703668c65SMatthieu Baerts		print_ok
1828718eb44eSGeliang Tang	fi
1829718eb44eSGeliang Tang}
1830718eb44eSGeliang Tang
1831e274f715SPaolo Abenichk_subflow_nr()
1832e274f715SPaolo Abeni{
183303668c65SMatthieu Baerts	local msg="$1"
183403668c65SMatthieu Baerts	local subflow_nr=$2
1835e274f715SPaolo Abeni	local cnt1
1836e274f715SPaolo Abeni	local cnt2
1837a635a8c3SPaolo Abeni	local dump_stats
1838e274f715SPaolo Abeni
183903668c65SMatthieu Baerts	print_check "${msg}"
1840e274f715SPaolo Abeni
1841e274f715SPaolo Abeni	cnt1=$(ss -N $ns1 -tOni | grep -c token)
1842e274f715SPaolo Abeni	cnt2=$(ss -N $ns2 -tOni | grep -c token)
18430fcd72dfSMatthieu Baerts	if [ "$cnt1" != "$subflow_nr" ] || [ "$cnt2" != "$subflow_nr" ]; then
184403668c65SMatthieu Baerts		fail_test "got $cnt1:$cnt2 subflows expected $subflow_nr"
1845e274f715SPaolo Abeni		dump_stats=1
1846e274f715SPaolo Abeni	else
184703668c65SMatthieu Baerts		print_ok
1848e274f715SPaolo Abeni	fi
1849e274f715SPaolo Abeni
1850a635a8c3SPaolo Abeni	if [ "${dump_stats}" = 1 ]; then
1851a635a8c3SPaolo Abeni		ss -N $ns1 -tOni
1852a635a8c3SPaolo Abeni		ss -N $ns1 -tOni | grep token
1853a635a8c3SPaolo Abeni		ip -n $ns1 mptcp endpoint
1854a635a8c3SPaolo Abeni	fi
1855e274f715SPaolo Abeni}
1856e274f715SPaolo Abeni
18579095ce97SGeliang Tangchk_mptcp_info()
18589095ce97SGeliang Tang{
1859d7ced753SGeliang Tang	local info1=$1
1860d7ced753SGeliang Tang	local exp1=$2
1861d7ced753SGeliang Tang	local info2=$3
1862d7ced753SGeliang Tang	local exp2=$4
18639095ce97SGeliang Tang	local cnt1
18649095ce97SGeliang Tang	local cnt2
18659095ce97SGeliang Tang	local dump_stats
18669095ce97SGeliang Tang
186703668c65SMatthieu Baerts	print_check "mptcp_info ${info1:0:8}=$exp1:$exp2"
18689095ce97SGeliang Tang
1869d7ced753SGeliang Tang	cnt1=$(ss -N $ns1 -inmHM | grep "$info1:" |
1870d7ced753SGeliang Tang	       sed -n 's/.*\('"$info1"':\)\([[:digit:]]*\).*$/\2/p;q')
1871d7ced753SGeliang Tang	cnt2=$(ss -N $ns2 -inmHM | grep "$info2:" |
1872d7ced753SGeliang Tang	       sed -n 's/.*\('"$info2"':\)\([[:digit:]]*\).*$/\2/p;q')
1873d7ced753SGeliang Tang	# 'ss' only display active connections and counters that are not 0.
18749095ce97SGeliang Tang	[ -z "$cnt1" ] && cnt1=0
18759095ce97SGeliang Tang	[ -z "$cnt2" ] && cnt2=0
1876d7ced753SGeliang Tang
1877d7ced753SGeliang Tang	if [ "$cnt1" != "$exp1" ] || [ "$cnt2" != "$exp2" ]; then
187803668c65SMatthieu Baerts		fail_test "got $cnt1:$cnt2 $info1:$info2 expected $exp1:$exp2"
18799095ce97SGeliang Tang		dump_stats=1
18809095ce97SGeliang Tang	else
188103668c65SMatthieu Baerts		print_ok
18829095ce97SGeliang Tang	fi
18839095ce97SGeliang Tang
18849095ce97SGeliang Tang	if [ "$dump_stats" = 1 ]; then
18859095ce97SGeliang Tang		ss -N $ns1 -inmHM
18869095ce97SGeliang Tang		ss -N $ns2 -inmHM
18879095ce97SGeliang Tang	fi
18889095ce97SGeliang Tang}
18899095ce97SGeliang Tang
18907d1e6f16SPaolo Abenichk_link_usage()
18917d1e6f16SPaolo Abeni{
18927d1e6f16SPaolo Abeni	local ns=$1
18937d1e6f16SPaolo Abeni	local link=$2
18947d1e6f16SPaolo Abeni	local out=$3
18957d1e6f16SPaolo Abeni	local expected_rate=$4
18964bfadd71SMatthieu Baerts
18974bfadd71SMatthieu Baerts	local tx_link tx_total
18984bfadd71SMatthieu Baerts	tx_link=$(ip netns exec $ns cat /sys/class/net/$link/statistics/tx_bytes)
1899d8d08302SMatthieu Baerts	tx_total=$(stat --format=%s $out)
1900d8d08302SMatthieu Baerts	local tx_rate=$((tx_link * 100 / tx_total))
19017d1e6f16SPaolo Abeni	local tolerance=5
19027d1e6f16SPaolo Abeni
190303668c65SMatthieu Baerts	print_check "link usage"
1904d8d08302SMatthieu Baerts	if [ $tx_rate -lt $((expected_rate - tolerance)) ] || \
1905d8d08302SMatthieu Baerts	   [ $tx_rate -gt $((expected_rate + tolerance)) ]; then
190603668c65SMatthieu Baerts		fail_test "got $tx_rate% usage, expected $expected_rate%"
19077d1e6f16SPaolo Abeni	else
190803668c65SMatthieu Baerts		print_ok
19097d1e6f16SPaolo Abeni	fi
19107d1e6f16SPaolo Abeni}
19117d1e6f16SPaolo Abeni
1912f98c2bcaSMat Martineauwait_attempt_fail()
191346e967d1SPaolo Abeni{
191446e967d1SPaolo Abeni	local timeout_ms=$((timeout_poll * 1000))
191546e967d1SPaolo Abeni	local time=0
191646e967d1SPaolo Abeni	local ns=$1
191746e967d1SPaolo Abeni
191846e967d1SPaolo Abeni	while [ $time -lt $timeout_ms ]; do
19191e777bd8SMatthieu Baerts		local cnt
19201e777bd8SMatthieu Baerts
192147867f0aSMatthieu Baerts		cnt=$(get_counter ${ns} "TcpAttemptFails")
192246e967d1SPaolo Abeni
192346e967d1SPaolo Abeni		[ "$cnt" = 1 ] && return 1
192446e967d1SPaolo Abeni		time=$((time + 100))
192546e967d1SPaolo Abeni		sleep 0.1
192646e967d1SPaolo Abeni	done
192746e967d1SPaolo Abeni	return 1
192846e967d1SPaolo Abeni}
192946e967d1SPaolo Abeni
19305ac1d2d6SMat Martineauset_userspace_pm()
19315ac1d2d6SMat Martineau{
19325ac1d2d6SMat Martineau	local ns=$1
19335ac1d2d6SMat Martineau
19345ac1d2d6SMat Martineau	ip netns exec $ns sysctl -q net.mptcp.pm_type=1
19355ac1d2d6SMat Martineau}
19365ac1d2d6SMat Martineau
19371002b89fSGeliang Tangsubflows_tests()
19381002b89fSGeliang Tang{
1939c7d49c03SMatthieu Baerts	if reset "no JOIN"; then
1940b08fbf24SPaolo Abeni		run_tests $ns1 $ns2 10.0.1.1
1941c7d49c03SMatthieu Baerts		chk_join_nr 0 0 0
1942ae7bd9ccSMatthieu Baerts	fi
1943b08fbf24SPaolo Abeni
19441002b89fSGeliang Tang	# subflow limited by client
1945c7d49c03SMatthieu Baerts	if reset "single subflow, limited by client"; then
194634aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 0
194734aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 0 0
194834aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
1949b08fbf24SPaolo Abeni		run_tests $ns1 $ns2 10.0.1.1
1950c7d49c03SMatthieu Baerts		chk_join_nr 0 0 0
1951ae7bd9ccSMatthieu Baerts	fi
1952b08fbf24SPaolo Abeni
19531002b89fSGeliang Tang	# subflow limited by server
1954c7d49c03SMatthieu Baerts	if reset "single subflow, limited by server"; then
195534aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 0
195634aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 0 1
195734aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
1958b08fbf24SPaolo Abeni		run_tests $ns1 $ns2 10.0.1.1
1959c7d49c03SMatthieu Baerts		chk_join_nr 1 1 0
1960ae7bd9ccSMatthieu Baerts	fi
1961b08fbf24SPaolo Abeni
1962b08fbf24SPaolo Abeni	# subflow
1963c7d49c03SMatthieu Baerts	if reset "single subflow"; then
196434aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 1
196534aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 0 1
196634aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
1967b08fbf24SPaolo Abeni		run_tests $ns1 $ns2 10.0.1.1
1968c7d49c03SMatthieu Baerts		chk_join_nr 1 1 1
1969ae7bd9ccSMatthieu Baerts	fi
1970b08fbf24SPaolo Abeni
1971b08fbf24SPaolo Abeni	# multiple subflows
1972c7d49c03SMatthieu Baerts	if reset "multiple subflows"; then
197334aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 2
197434aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 0 2
197534aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
197634aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.2.2 flags subflow
1977b08fbf24SPaolo Abeni		run_tests $ns1 $ns2 10.0.1.1
1978c7d49c03SMatthieu Baerts		chk_join_nr 2 2 2
1979ae7bd9ccSMatthieu Baerts	fi
1980b08fbf24SPaolo Abeni
198172bcbc46SPaolo Abeni	# multiple subflows limited by server
1982c7d49c03SMatthieu Baerts	if reset "multiple subflows, limited by server"; then
198334aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 1
198434aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 0 2
198534aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
198634aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.2.2 flags subflow
1987b08fbf24SPaolo Abeni		run_tests $ns1 $ns2 10.0.1.1
1988c7d49c03SMatthieu Baerts		chk_join_nr 2 2 1
1989ae7bd9ccSMatthieu Baerts	fi
1990c3eaa5f6SGeliang Tang
1991c3eaa5f6SGeliang Tang	# single subflow, dev
1992c7d49c03SMatthieu Baerts	if reset "single subflow, dev"; then
199334aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 1
199434aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 0 1
199534aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow dev ns2eth3
1996c3eaa5f6SGeliang Tang		run_tests $ns1 $ns2 10.0.1.1
1997c7d49c03SMatthieu Baerts		chk_join_nr 1 1 1
1998ae7bd9ccSMatthieu Baerts	fi
19991002b89fSGeliang Tang}
2000b08fbf24SPaolo Abeni
200146e967d1SPaolo Abenisubflows_error_tests()
200246e967d1SPaolo Abeni{
200346e967d1SPaolo Abeni	# If a single subflow is configured, and matches the MPC src
200446e967d1SPaolo Abeni	# address, no additional subflow should be created
2005c7d49c03SMatthieu Baerts	if reset "no MPC reuse with single endpoint"; then
200634aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 1
200734aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 0 1
200834aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.1.2 flags subflow
2009e571fb09SGeliang Tang		speed=slow \
2010e571fb09SGeliang Tang			run_tests $ns1 $ns2 10.0.1.1
2011c7d49c03SMatthieu Baerts		chk_join_nr 0 0 0
2012ae7bd9ccSMatthieu Baerts	fi
201346e967d1SPaolo Abeni
201446e967d1SPaolo Abeni	# multiple subflows, with subflow creation error
20154a0b866aSMatthieu Baerts	if reset_with_tcp_filter "multi subflows, with failing subflow" ns1 10.0.3.2 REJECT &&
20164a0b866aSMatthieu Baerts	   continue_if mptcp_lib_kallsyms_has "mptcp_pm_subflow_check_next$"; then
201734aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 2
201834aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 0 2
201934aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
202034aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.2.2 flags subflow
2021e571fb09SGeliang Tang		speed=slow \
2022e571fb09SGeliang Tang			run_tests $ns1 $ns2 10.0.1.1
2023c7d49c03SMatthieu Baerts		chk_join_nr 1 1 1
2024ae7bd9ccSMatthieu Baerts	fi
202546e967d1SPaolo Abeni
202646e967d1SPaolo Abeni	# multiple subflows, with subflow timeout on MPJ
20274a0b866aSMatthieu Baerts	if reset_with_tcp_filter "multi subflows, with subflow timeout" ns1 10.0.3.2 DROP &&
20284a0b866aSMatthieu Baerts	   continue_if mptcp_lib_kallsyms_has "mptcp_pm_subflow_check_next$"; then
202934aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 2
203034aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 0 2
203134aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
203234aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.2.2 flags subflow
2033e571fb09SGeliang Tang		speed=slow \
2034e571fb09SGeliang Tang			run_tests $ns1 $ns2 10.0.1.1
2035c7d49c03SMatthieu Baerts		chk_join_nr 1 1 1
2036ae7bd9ccSMatthieu Baerts	fi
203746e967d1SPaolo Abeni
203846e967d1SPaolo Abeni	# multiple subflows, check that the endpoint corresponding to
203946e967d1SPaolo Abeni	# closed subflow (due to reset) is not reused if additional
204046e967d1SPaolo Abeni	# subflows are added later
20414a0b866aSMatthieu Baerts	if reset_with_tcp_filter "multi subflows, fair usage on close" ns1 10.0.3.2 REJECT &&
20424a0b866aSMatthieu Baerts	   continue_if mptcp_lib_kallsyms_has "mptcp_pm_subflow_check_next$"; then
204334aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 1
204434aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 0 1
204534aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
2046e571fb09SGeliang Tang		speed=slow \
2047e571fb09SGeliang Tang			run_tests $ns1 $ns2 10.0.1.1 &
204846e967d1SPaolo Abeni
204946e967d1SPaolo Abeni		# mpj subflow will be in TW after the reset
2050f98c2bcaSMat Martineau		wait_attempt_fail $ns2
205134aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.2.2 flags subflow
205246e967d1SPaolo Abeni		wait
205346e967d1SPaolo Abeni
205446e967d1SPaolo Abeni		# additional subflow could be created only if the PM select
205546e967d1SPaolo Abeni		# the later endpoint, skipping the already used one
2056c7d49c03SMatthieu Baerts		chk_join_nr 1 1 1
2057ae7bd9ccSMatthieu Baerts	fi
205846e967d1SPaolo Abeni}
205946e967d1SPaolo Abeni
20601002b89fSGeliang Tangsignal_address_tests()
20611002b89fSGeliang Tang{
2062b08fbf24SPaolo Abeni	# add_address, unused
2063c7d49c03SMatthieu Baerts	if reset "unused signal address"; then
206434aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
2065b08fbf24SPaolo Abeni		run_tests $ns1 $ns2 10.0.1.1
2066c7d49c03SMatthieu Baerts		chk_join_nr 0 0 0
20670639fa23SPaolo Abeni		chk_add_tx_nr 1 1
2068be613160SGeliang Tang		chk_add_nr 1 1
2069ae7bd9ccSMatthieu Baerts	fi
2070b08fbf24SPaolo Abeni
2071b08fbf24SPaolo Abeni	# accept and use add_addr
2072c7d49c03SMatthieu Baerts	if reset "signal address"; then
207334aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 1
207434aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 1 1
207534aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
2076b08fbf24SPaolo Abeni		run_tests $ns1 $ns2 10.0.1.1
2077c7d49c03SMatthieu Baerts		chk_join_nr 1 1 1
2078be613160SGeliang Tang		chk_add_nr 1 1
2079ae7bd9ccSMatthieu Baerts	fi
2080b08fbf24SPaolo Abeni
2081b08fbf24SPaolo Abeni	# accept and use add_addr with an additional subflow
2082b08fbf24SPaolo Abeni	# note: signal address in server ns and local addresses in client ns must
2083b08fbf24SPaolo Abeni	# belong to different subnets or one of the listed local address could be
2084b08fbf24SPaolo Abeni	# used for 'add_addr' subflow
2085c7d49c03SMatthieu Baerts	if reset "subflow and signal"; then
208634aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
208734aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 2
208834aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 1 2
208934aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
2090b08fbf24SPaolo Abeni		run_tests $ns1 $ns2 10.0.1.1
2091c7d49c03SMatthieu Baerts		chk_join_nr 2 2 2
2092be613160SGeliang Tang		chk_add_nr 1 1
2093ae7bd9ccSMatthieu Baerts	fi
2094b08fbf24SPaolo Abeni
2095b08fbf24SPaolo Abeni	# accept and use add_addr with additional subflows
2096c7d49c03SMatthieu Baerts	if reset "multiple subflows and signal"; then
209734aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 3
209834aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
209934aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 1 3
210034aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
210134aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.4.2 flags subflow
2102b08fbf24SPaolo Abeni		run_tests $ns1 $ns2 10.0.1.1
2103c7d49c03SMatthieu Baerts		chk_join_nr 3 3 3
2104be613160SGeliang Tang		chk_add_nr 1 1
2105ae7bd9ccSMatthieu Baerts	fi
2106ef360019SGeliang Tang
2107ef360019SGeliang Tang	# signal addresses
2108c7d49c03SMatthieu Baerts	if reset "signal addresses"; then
210934aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 3 3
211034aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
211134aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.3.1 flags signal
211234aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.4.1 flags signal
211334aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 3 3
2114ef360019SGeliang Tang		run_tests $ns1 $ns2 10.0.1.1
2115c7d49c03SMatthieu Baerts		chk_join_nr 3 3 3
2116ef360019SGeliang Tang		chk_add_nr 3 3
2117ae7bd9ccSMatthieu Baerts	fi
2118ef360019SGeliang Tang
2119ef360019SGeliang Tang	# signal invalid addresses
2120c7d49c03SMatthieu Baerts	if reset "signal invalid addresses"; then
212134aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 3 3
212234aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.12.1 flags signal
212334aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.3.1 flags signal
212434aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.14.1 flags signal
212534aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 3 3
2126ef360019SGeliang Tang		run_tests $ns1 $ns2 10.0.1.1
2127c7d49c03SMatthieu Baerts		chk_join_nr 1 1 1
2128ef360019SGeliang Tang		chk_add_nr 3 3
2129ae7bd9ccSMatthieu Baerts	fi
213033c563adSYonglong Li
213133c563adSYonglong Li	# signal addresses race test
2132c7d49c03SMatthieu Baerts	if reset "signal addresses race test"; then
213334aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 4 4
213434aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 4 4
213534aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.1.1 flags signal
213634aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
213734aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.3.1 flags signal
213834aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.4.1 flags signal
213934aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.1.2 flags signal
214034aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.2.2 flags signal
214134aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.3.2 flags signal
214234aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.4.2 flags signal
21436ef84b15SPaolo Abeni
21446ef84b15SPaolo Abeni		# the peer could possibly miss some addr notification, allow retransmission
21456ef84b15SPaolo Abeni		ip netns exec $ns1 sysctl -q net.mptcp.add_addr_timeout=1
2146e571fb09SGeliang Tang		speed=slow \
2147e571fb09SGeliang Tang			run_tests $ns1 $ns2 10.0.1.1
214886e39e04SPaolo Abeni
2149d4c81bbbSMatthieu Baerts		# It is not directly linked to the commit introducing this
2150d4c81bbbSMatthieu Baerts		# symbol but for the parent one which is linked anyway.
2151d4c81bbbSMatthieu Baerts		if ! mptcp_lib_kallsyms_has "mptcp_pm_subflow_check_next$"; then
2152d4c81bbbSMatthieu Baerts			chk_join_nr 3 3 2
2153d4c81bbbSMatthieu Baerts			chk_add_nr 4 4
2154d4c81bbbSMatthieu Baerts		else
2155d4c81bbbSMatthieu Baerts			chk_join_nr 3 3 3
215686e39e04SPaolo Abeni			# the server will not signal the address terminating
215786e39e04SPaolo Abeni			# the MPC subflow
215886e39e04SPaolo Abeni			chk_add_nr 3 3
2159ae7bd9ccSMatthieu Baerts		fi
2160d4c81bbbSMatthieu Baerts	fi
21611002b89fSGeliang Tang}
2162b08fbf24SPaolo Abeni
21631002b89fSGeliang Tanglink_failure_tests()
21641002b89fSGeliang Tang{
21658b819a84SFlorian Westphal	# accept and use add_addr with additional subflows and link loss
2166c7d49c03SMatthieu Baerts	if reset "multiple flows, signal, link failure"; then
21677d1e6f16SPaolo Abeni		# without any b/w limit each veth could spool the packets and get
21687d1e6f16SPaolo Abeni		# them acked at xmit time, so that the corresponding subflow will
21697d1e6f16SPaolo Abeni		# have almost always no outstanding pkts, the scheduler will pick
21707d1e6f16SPaolo Abeni		# always the first subflow and we will have hard time testing
21717d1e6f16SPaolo Abeni		# active backup and link switch-over.
21727d1e6f16SPaolo Abeni		# Let's set some arbitrary (low) virtual link limits.
21737d1e6f16SPaolo Abeni		init_shapers
217434aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 3
217534aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.2.1 dev ns1eth2 flags signal
217634aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 1 3
217734aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.3.2 dev ns2eth3 flags subflow
217834aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.4.2 dev ns2eth4 flags subflow
21790c93af1fSGeliang Tang		test_linkfail=1 \
21800c93af1fSGeliang Tang			run_tests $ns1 $ns2 10.0.1.1
2181c7d49c03SMatthieu Baerts		chk_join_nr 3 3 3
21828b819a84SFlorian Westphal		chk_add_nr 1 1
21837d1e6f16SPaolo Abeni		chk_stale_nr $ns2 1 5 1
2184ae7bd9ccSMatthieu Baerts	fi
21857d1e6f16SPaolo Abeni
21867d1e6f16SPaolo Abeni	# accept and use add_addr with additional subflows and link loss
21877d1e6f16SPaolo Abeni	# for bidirectional transfer
2188c7d49c03SMatthieu Baerts	if reset "multi flows, signal, bidi, link fail"; then
21897d1e6f16SPaolo Abeni		init_shapers
219034aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 3
219134aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.2.1 dev ns1eth2 flags signal
219234aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 1 3
219334aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.3.2 dev ns2eth3 flags subflow
219434aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.4.2 dev ns2eth4 flags subflow
21950c93af1fSGeliang Tang		test_linkfail=2 \
21960c93af1fSGeliang Tang			run_tests $ns1 $ns2 10.0.1.1
2197c7d49c03SMatthieu Baerts		chk_join_nr 3 3 3
21987d1e6f16SPaolo Abeni		chk_add_nr 1 1
21997d1e6f16SPaolo Abeni		chk_stale_nr $ns2 1 -1 1
2200ae7bd9ccSMatthieu Baerts	fi
22017d1e6f16SPaolo Abeni
22027d1e6f16SPaolo Abeni	# 2 subflows plus 1 backup subflow with a lossy link, backup
22037d1e6f16SPaolo Abeni	# will never be used
2204c7d49c03SMatthieu Baerts	if reset "backup subflow unused, link failure"; then
22057d1e6f16SPaolo Abeni		init_shapers
220634aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 2
220734aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.2.1 dev ns1eth2 flags signal
220834aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 1 2
220934aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.3.2 dev ns2eth3 flags subflow,backup
22100c93af1fSGeliang Tang		FAILING_LINKS="1" test_linkfail=1 \
22110c93af1fSGeliang Tang			run_tests $ns1 $ns2 10.0.1.1
2212c7d49c03SMatthieu Baerts		chk_join_nr 2 2 2
22137d1e6f16SPaolo Abeni		chk_add_nr 1 1
22147d1e6f16SPaolo Abeni		chk_link_usage $ns2 ns2eth3 $cinsent 0
2215ae7bd9ccSMatthieu Baerts	fi
22167d1e6f16SPaolo Abeni
22177d1e6f16SPaolo Abeni	# 2 lossy links after half transfer, backup will get half of
22187d1e6f16SPaolo Abeni	# the traffic
2219c7d49c03SMatthieu Baerts	if reset "backup flow used, multi links fail"; then
22207d1e6f16SPaolo Abeni		init_shapers
222134aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 2
222234aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.2.1 dev ns1eth2 flags signal
222334aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 1 2
222434aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.3.2 dev ns2eth3 flags subflow,backup
22250c93af1fSGeliang Tang		FAILING_LINKS="1 2" test_linkfail=1 \
22260c93af1fSGeliang Tang			run_tests $ns1 $ns2 10.0.1.1
2227c7d49c03SMatthieu Baerts		chk_join_nr 2 2 2
22287d1e6f16SPaolo Abeni		chk_add_nr 1 1
22297d1e6f16SPaolo Abeni		chk_stale_nr $ns2 2 4 2
22307d1e6f16SPaolo Abeni		chk_link_usage $ns2 ns2eth3 $cinsent 50
2231ae7bd9ccSMatthieu Baerts	fi
22327d1e6f16SPaolo Abeni
22337d1e6f16SPaolo Abeni	# use a backup subflow with the first subflow on a lossy link
22347d1e6f16SPaolo Abeni	# for bidirectional transfer
2235c7d49c03SMatthieu Baerts	if reset "backup flow used, bidi, link failure"; then
22367d1e6f16SPaolo Abeni		init_shapers
223734aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 2
223834aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.2.1 dev ns1eth2 flags signal
223934aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 1 3
224034aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.3.2 dev ns2eth3 flags subflow,backup
22410c93af1fSGeliang Tang		FAILING_LINKS="1 2" test_linkfail=2 \
22420c93af1fSGeliang Tang			run_tests $ns1 $ns2 10.0.1.1
2243c7d49c03SMatthieu Baerts		chk_join_nr 2 2 2
22447d1e6f16SPaolo Abeni		chk_add_nr 1 1
22457d1e6f16SPaolo Abeni		chk_stale_nr $ns2 1 -1 2
22467d1e6f16SPaolo Abeni		chk_link_usage $ns2 ns2eth3 $cinsent 50
2247ae7bd9ccSMatthieu Baerts	fi
22481002b89fSGeliang Tang}
22498b819a84SFlorian Westphal
22501002b89fSGeliang Tangadd_addr_timeout_tests()
22511002b89fSGeliang Tang{
22528d014eaaSGeliang Tang	# add_addr timeout
2253c7d49c03SMatthieu Baerts	if reset_with_add_addr_timeout "signal address, ADD_ADDR timeout"; then
225434aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 1
225534aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 1 1
225634aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
2257e571fb09SGeliang Tang		speed=slow \
2258e571fb09SGeliang Tang			run_tests $ns1 $ns2 10.0.1.1
2259c7d49c03SMatthieu Baerts		chk_join_nr 1 1 1
22600639fa23SPaolo Abeni		chk_add_tx_nr 4 4
22618d014eaaSGeliang Tang		chk_add_nr 4 0
2262ae7bd9ccSMatthieu Baerts	fi
22638d014eaaSGeliang Tang
22641002b89fSGeliang Tang	# add_addr timeout IPv6
2265c7d49c03SMatthieu Baerts	if reset_with_add_addr_timeout "signal address, ADD_ADDR6 timeout" 6; then
226634aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 1
226734aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 1 1
226834aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 dead:beef:2::1 flags signal
2269e571fb09SGeliang Tang		speed=slow \
2270e571fb09SGeliang Tang			run_tests $ns1 $ns2 dead:beef:1::1
2271c7d49c03SMatthieu Baerts		chk_join_nr 1 1 1
22721002b89fSGeliang Tang		chk_add_nr 4 0
2273ae7bd9ccSMatthieu Baerts	fi
22748da6229bSGeliang Tang
22758da6229bSGeliang Tang	# signal addresses timeout
2276c7d49c03SMatthieu Baerts	if reset_with_add_addr_timeout "signal addresses, ADD_ADDR timeout"; then
227734aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 2 2
227834aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
227934aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.3.1 flags signal
228034aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 2 2
2281e571fb09SGeliang Tang		speed=10 \
2282e571fb09SGeliang Tang			run_tests $ns1 $ns2 10.0.1.1
2283c7d49c03SMatthieu Baerts		chk_join_nr 2 2 2
22848da6229bSGeliang Tang		chk_add_nr 8 0
2285ae7bd9ccSMatthieu Baerts	fi
22868da6229bSGeliang Tang
22878da6229bSGeliang Tang	# signal invalid addresses timeout
2288c7d49c03SMatthieu Baerts	if reset_with_add_addr_timeout "invalid address, ADD_ADDR timeout"; then
228934aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 2 2
229034aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.12.1 flags signal
229134aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.3.1 flags signal
229234aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 2 2
2293e571fb09SGeliang Tang		speed=10 \
2294e571fb09SGeliang Tang			run_tests $ns1 $ns2 10.0.1.1
2295c7d49c03SMatthieu Baerts		chk_join_nr 1 1 1
22968da6229bSGeliang Tang		chk_add_nr 8 0
2297ae7bd9ccSMatthieu Baerts	fi
22981002b89fSGeliang Tang}
22991002b89fSGeliang Tang
23001002b89fSGeliang Tangremove_tests()
23011002b89fSGeliang Tang{
2302dd72b0feSGeliang Tang	# single subflow, remove
2303c7d49c03SMatthieu Baerts	if reset "remove single subflow"; then
230434aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 1
230534aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 0 1
230634aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
2307e571fb09SGeliang Tang		addr_nr_ns2=-1 speed=slow \
2308e571fb09SGeliang Tang			run_tests $ns1 $ns2 10.0.1.1
2309c7d49c03SMatthieu Baerts		chk_join_nr 1 1 1
23100639fa23SPaolo Abeni		chk_rm_tx_nr 1
2311dd72b0feSGeliang Tang		chk_rm_nr 1 1
23122cfaa8b3SMatthieu Baerts		chk_rst_nr 0 0
2313ae7bd9ccSMatthieu Baerts	fi
2314dd72b0feSGeliang Tang
2315dd72b0feSGeliang Tang	# multiple subflows, remove
2316c7d49c03SMatthieu Baerts	if reset "remove multiple subflows"; then
231734aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 2
231834aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 0 2
231934aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.2.2 flags subflow
232034aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
2321e571fb09SGeliang Tang		addr_nr_ns2=-2 speed=slow \
2322e571fb09SGeliang Tang			run_tests $ns1 $ns2 10.0.1.1
2323c7d49c03SMatthieu Baerts		chk_join_nr 2 2 2
2324dd72b0feSGeliang Tang		chk_rm_nr 2 2
23252cfaa8b3SMatthieu Baerts		chk_rst_nr 0 0
2326ae7bd9ccSMatthieu Baerts	fi
2327dd72b0feSGeliang Tang
2328dd72b0feSGeliang Tang	# single address, remove
2329c7d49c03SMatthieu Baerts	if reset "remove single address"; then
233034aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 1
233134aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
233234aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 1 1
2333e571fb09SGeliang Tang		addr_nr_ns1=-1 speed=slow \
2334e571fb09SGeliang Tang			run_tests $ns1 $ns2 10.0.1.1
2335c7d49c03SMatthieu Baerts		chk_join_nr 1 1 1
2336dd72b0feSGeliang Tang		chk_add_nr 1 1
23377028ba8aSGeliang Tang		chk_rm_nr 1 1 invert
23382cfaa8b3SMatthieu Baerts		chk_rst_nr 0 0
2339ae7bd9ccSMatthieu Baerts	fi
2340dd72b0feSGeliang Tang
2341dd72b0feSGeliang Tang	# subflow and signal, remove
2342c7d49c03SMatthieu Baerts	if reset "remove subflow and signal"; then
234334aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 2
234434aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
234534aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 1 2
234634aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
2347e571fb09SGeliang Tang		addr_nr_ns1=-1 addr_nr_ns2=-1 speed=slow \
2348e571fb09SGeliang Tang			run_tests $ns1 $ns2 10.0.1.1
2349c7d49c03SMatthieu Baerts		chk_join_nr 2 2 2
2350dd72b0feSGeliang Tang		chk_add_nr 1 1
2351dd72b0feSGeliang Tang		chk_rm_nr 1 1
23522cfaa8b3SMatthieu Baerts		chk_rst_nr 0 0
2353ae7bd9ccSMatthieu Baerts	fi
2354dd72b0feSGeliang Tang
2355dd72b0feSGeliang Tang	# subflows and signal, remove
2356c7d49c03SMatthieu Baerts	if reset "remove subflows and signal"; then
235734aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 3
235834aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
235934aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 1 3
236034aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
236134aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.4.2 flags subflow
2362e571fb09SGeliang Tang		addr_nr_ns1=-1 addr_nr_ns2=-2 speed=10 \
2363e571fb09SGeliang Tang			run_tests $ns1 $ns2 10.0.1.1
2364c7d49c03SMatthieu Baerts		chk_join_nr 3 3 3
2365dd72b0feSGeliang Tang		chk_add_nr 1 1
2366dd72b0feSGeliang Tang		chk_rm_nr 2 2
23672cfaa8b3SMatthieu Baerts		chk_rst_nr 0 0
2368ae7bd9ccSMatthieu Baerts	fi
2369dd72b0feSGeliang Tang
2370ef360019SGeliang Tang	# addresses remove
2371c7d49c03SMatthieu Baerts	if reset "remove addresses"; then
237234aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 3 3
237334aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal id 250
237434aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.3.1 flags signal
237534aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.4.1 flags signal
237634aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 3 3
2377e571fb09SGeliang Tang		addr_nr_ns1=-3 speed=10 \
2378e571fb09SGeliang Tang			run_tests $ns1 $ns2 10.0.1.1
2379c7d49c03SMatthieu Baerts		chk_join_nr 3 3 3
2380ef360019SGeliang Tang		chk_add_nr 3 3
2381ef360019SGeliang Tang		chk_rm_nr 3 3 invert
23822cfaa8b3SMatthieu Baerts		chk_rst_nr 0 0
2383ae7bd9ccSMatthieu Baerts	fi
2384ef360019SGeliang Tang
2385ef360019SGeliang Tang	# invalid addresses remove
2386c7d49c03SMatthieu Baerts	if reset "remove invalid addresses"; then
238734aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 3 3
238834aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.12.1 flags signal
238934aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.3.1 flags signal
239034aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.14.1 flags signal
239134aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 3 3
2392e571fb09SGeliang Tang		addr_nr_ns1=-3 speed=10 \
2393e571fb09SGeliang Tang			run_tests $ns1 $ns2 10.0.1.1
2394c7d49c03SMatthieu Baerts		chk_join_nr 1 1 1
2395ef360019SGeliang Tang		chk_add_nr 3 3
2396ef360019SGeliang Tang		chk_rm_nr 3 1 invert
23972cfaa8b3SMatthieu Baerts		chk_rst_nr 0 0
2398ae7bd9ccSMatthieu Baerts	fi
2399ef360019SGeliang Tang
24006fe4ccdcSGeliang Tang	# subflows and signal, flush
2401c7d49c03SMatthieu Baerts	if reset "flush subflows and signal"; then
240234aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 3
240334aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
240434aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 1 3
240534aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
240634aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.4.2 flags subflow
2407e571fb09SGeliang Tang		addr_nr_ns1=-8 addr_nr_ns2=-8 speed=slow \
2408e571fb09SGeliang Tang			run_tests $ns1 $ns2 10.0.1.1
2409c7d49c03SMatthieu Baerts		chk_join_nr 3 3 3
24106fe4ccdcSGeliang Tang		chk_add_nr 1 1
24116fa0174aSPaolo Abeni		chk_rm_nr 1 3 invert simult
24122cfaa8b3SMatthieu Baerts		chk_rst_nr 0 0
2413ae7bd9ccSMatthieu Baerts	fi
2414d2c4333aSGeliang Tang
2415d2c4333aSGeliang Tang	# subflows flush
2416c7d49c03SMatthieu Baerts	if reset "flush subflows"; then
241734aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 3 3
241834aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 3 3
241934aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.2.2 flags subflow id 150
242034aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
242134aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.4.2 flags subflow
2422e571fb09SGeliang Tang		addr_nr_ns1=-8 addr_nr_ns2=-8 speed=slow \
2423e571fb09SGeliang Tang			run_tests $ns1 $ns2 10.0.1.1
2424c7d49c03SMatthieu Baerts		chk_join_nr 3 3 3
2425425ba803SMatthieu Baerts
2426425ba803SMatthieu Baerts		if mptcp_lib_kversion_ge 5.18; then
24270639fa23SPaolo Abeni			chk_rm_tx_nr 0
24286fa0174aSPaolo Abeni			chk_rm_nr 0 3 simult
2429425ba803SMatthieu Baerts		else
2430425ba803SMatthieu Baerts			chk_rm_nr 3 3
2431425ba803SMatthieu Baerts		fi
24322cfaa8b3SMatthieu Baerts		chk_rst_nr 0 0
2433ae7bd9ccSMatthieu Baerts	fi
2434d2c4333aSGeliang Tang
2435d2c4333aSGeliang Tang	# addresses flush
2436c7d49c03SMatthieu Baerts	if reset "flush addresses"; then
243734aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 3 3
243834aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal id 250
243934aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.3.1 flags signal
244034aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.4.1 flags signal
244134aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 3 3
2442e571fb09SGeliang Tang		addr_nr_ns1=-8 addr_nr_ns2=-8 speed=slow \
2443e571fb09SGeliang Tang			run_tests $ns1 $ns2 10.0.1.1
2444c7d49c03SMatthieu Baerts		chk_join_nr 3 3 3
2445d2c4333aSGeliang Tang		chk_add_nr 3 3
24466fa0174aSPaolo Abeni		chk_rm_nr 3 3 invert simult
24472cfaa8b3SMatthieu Baerts		chk_rst_nr 0 0
2448ae7bd9ccSMatthieu Baerts	fi
2449ef360019SGeliang Tang
2450ef360019SGeliang Tang	# invalid addresses flush
2451c7d49c03SMatthieu Baerts	if reset "flush invalid addresses"; then
245234aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 3 3
245334aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.12.1 flags signal
245434aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.3.1 flags signal
245534aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.14.1 flags signal
245634aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 3 3
2457e571fb09SGeliang Tang		addr_nr_ns1=-8 speed=slow \
2458e571fb09SGeliang Tang			run_tests $ns1 $ns2 10.0.1.1
2459c7d49c03SMatthieu Baerts		chk_join_nr 1 1 1
2460ef360019SGeliang Tang		chk_add_nr 3 3
2461ef360019SGeliang Tang		chk_rm_nr 3 1 invert
24622cfaa8b3SMatthieu Baerts		chk_rst_nr 0 0
2463ae7bd9ccSMatthieu Baerts	fi
24645e287fe7SGeliang Tang
24655e287fe7SGeliang Tang	# remove id 0 subflow
2466c7d49c03SMatthieu Baerts	if reset "remove id 0 subflow"; then
246734aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 1
246834aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 0 1
246934aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
2470e571fb09SGeliang Tang		addr_nr_ns2=-9 speed=slow \
2471e571fb09SGeliang Tang			run_tests $ns1 $ns2 10.0.1.1
2472c7d49c03SMatthieu Baerts		chk_join_nr 1 1 1
24735e287fe7SGeliang Tang		chk_rm_nr 1 1
24742cfaa8b3SMatthieu Baerts		chk_rst_nr 0 0
2475ae7bd9ccSMatthieu Baerts	fi
24765e287fe7SGeliang Tang
24775e287fe7SGeliang Tang	# remove id 0 address
2478c7d49c03SMatthieu Baerts	if reset "remove id 0 address"; then
247934aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 1
248034aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
248134aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 1 1
2482e571fb09SGeliang Tang		addr_nr_ns1=-9 speed=slow \
2483e571fb09SGeliang Tang			run_tests $ns1 $ns2 10.0.1.1
2484c7d49c03SMatthieu Baerts		chk_join_nr 1 1 1
24855e287fe7SGeliang Tang		chk_add_nr 1 1
24865e287fe7SGeliang Tang		chk_rm_nr 1 1 invert
24872cfaa8b3SMatthieu Baerts		chk_rst_nr 0 0 invert
2488ae7bd9ccSMatthieu Baerts	fi
24891002b89fSGeliang Tang}
24906fe4ccdcSGeliang Tang
24911002b89fSGeliang Tangadd_tests()
24921002b89fSGeliang Tang{
24936208fd82SGeliang Tang	# add single subflow
2494c7d49c03SMatthieu Baerts	if reset "add single subflow"; then
249534aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 1
249634aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 0 1
2497e571fb09SGeliang Tang		addr_nr_ns2=1 speed=slow \
2498e571fb09SGeliang Tang			run_tests $ns1 $ns2 10.0.1.1
2499c7d49c03SMatthieu Baerts		chk_join_nr 1 1 1
2500ae7bd9ccSMatthieu Baerts	fi
25016208fd82SGeliang Tang
25026208fd82SGeliang Tang	# add signal address
2503c7d49c03SMatthieu Baerts	if reset "add signal address"; then
250434aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 1
250534aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 1 1
2506e571fb09SGeliang Tang		addr_nr_ns1=1 speed=slow \
2507e571fb09SGeliang Tang			run_tests $ns1 $ns2 10.0.1.1
2508c7d49c03SMatthieu Baerts		chk_join_nr 1 1 1
25096208fd82SGeliang Tang		chk_add_nr 1 1
2510ae7bd9ccSMatthieu Baerts	fi
25116208fd82SGeliang Tang
25126208fd82SGeliang Tang	# add multiple subflows
2513c7d49c03SMatthieu Baerts	if reset "add multiple subflows"; then
251434aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 2
251534aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 0 2
2516e571fb09SGeliang Tang		addr_nr_ns2=2 speed=slow \
2517e571fb09SGeliang Tang			run_tests $ns1 $ns2 10.0.1.1
2518c7d49c03SMatthieu Baerts		chk_join_nr 2 2 2
2519ae7bd9ccSMatthieu Baerts	fi
25206208fd82SGeliang Tang
25216208fd82SGeliang Tang	# add multiple subflows IPv6
2522c7d49c03SMatthieu Baerts	if reset "add multiple subflows IPv6"; then
252334aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 2
252434aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 0 2
2525e571fb09SGeliang Tang		addr_nr_ns2=2 speed=slow \
2526e571fb09SGeliang Tang			run_tests $ns1 $ns2 dead:beef:1::1
2527c7d49c03SMatthieu Baerts		chk_join_nr 2 2 2
2528ae7bd9ccSMatthieu Baerts	fi
25296208fd82SGeliang Tang
25306208fd82SGeliang Tang	# add multiple addresses IPv6
2531c7d49c03SMatthieu Baerts	if reset "add multiple addresses IPv6"; then
253234aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 2
253334aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 2 2
2534e571fb09SGeliang Tang		addr_nr_ns1=2 speed=slow \
2535e571fb09SGeliang Tang			run_tests $ns1 $ns2 dead:beef:1::1
2536c7d49c03SMatthieu Baerts		chk_join_nr 2 2 2
25376208fd82SGeliang Tang		chk_add_nr 2 2
2538ae7bd9ccSMatthieu Baerts	fi
25391002b89fSGeliang Tang}
25406208fd82SGeliang Tang
25411002b89fSGeliang Tangipv6_tests()
25421002b89fSGeliang Tang{
2543523514edSGeliang Tang	# subflow IPv6
2544c7d49c03SMatthieu Baerts	if reset "single subflow IPv6"; then
254534aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 1
254634aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 0 1
254734aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 dead:beef:3::2 dev ns2eth3 flags subflow
2548e571fb09SGeliang Tang		speed=slow \
2549e571fb09SGeliang Tang			run_tests $ns1 $ns2 dead:beef:1::1
2550c7d49c03SMatthieu Baerts		chk_join_nr 1 1 1
2551ae7bd9ccSMatthieu Baerts	fi
2552523514edSGeliang Tang
2553523514edSGeliang Tang	# add_address, unused IPv6
2554c7d49c03SMatthieu Baerts	if reset "unused signal address IPv6"; then
255534aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 dead:beef:2::1 flags signal
2556e571fb09SGeliang Tang		speed=slow \
2557e571fb09SGeliang Tang			run_tests $ns1 $ns2 dead:beef:1::1
2558c7d49c03SMatthieu Baerts		chk_join_nr 0 0 0
2559523514edSGeliang Tang		chk_add_nr 1 1
2560ae7bd9ccSMatthieu Baerts	fi
2561523514edSGeliang Tang
2562523514edSGeliang Tang	# signal address IPv6
2563c7d49c03SMatthieu Baerts	if reset "single address IPv6"; then
256434aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 1
256534aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 dead:beef:2::1 flags signal
256634aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 1 1
2567e571fb09SGeliang Tang		speed=slow \
2568e571fb09SGeliang Tang			run_tests $ns1 $ns2 dead:beef:1::1
2569c7d49c03SMatthieu Baerts		chk_join_nr 1 1 1
2570523514edSGeliang Tang		chk_add_nr 1 1
2571ae7bd9ccSMatthieu Baerts	fi
2572523514edSGeliang Tang
2573523514edSGeliang Tang	# single address IPv6, remove
2574c7d49c03SMatthieu Baerts	if reset "remove single address IPv6"; then
257534aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 1
257634aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 dead:beef:2::1 flags signal
257734aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 1 1
2578e571fb09SGeliang Tang		addr_nr_ns1=-1 speed=slow \
2579e571fb09SGeliang Tang			run_tests $ns1 $ns2 dead:beef:1::1
2580c7d49c03SMatthieu Baerts		chk_join_nr 1 1 1
2581523514edSGeliang Tang		chk_add_nr 1 1
25827028ba8aSGeliang Tang		chk_rm_nr 1 1 invert
2583ae7bd9ccSMatthieu Baerts	fi
2584523514edSGeliang Tang
2585523514edSGeliang Tang	# subflow and signal IPv6, remove
2586c7d49c03SMatthieu Baerts	if reset "remove subflow and signal IPv6"; then
258734aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 2
258834aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 dead:beef:2::1 flags signal
258934aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 1 2
259034aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 dead:beef:3::2 dev ns2eth3 flags subflow
2591e571fb09SGeliang Tang		addr_nr_ns1=-1 addr_nr_ns2=-1 speed=slow \
2592e571fb09SGeliang Tang			run_tests $ns1 $ns2 dead:beef:1::1
2593c7d49c03SMatthieu Baerts		chk_join_nr 2 2 2
2594523514edSGeliang Tang		chk_add_nr 1 1
2595523514edSGeliang Tang		chk_rm_nr 1 1
2596ae7bd9ccSMatthieu Baerts	fi
25971002b89fSGeliang Tang}
2598523514edSGeliang Tang
25991002b89fSGeliang Tangv4mapped_tests()
26001002b89fSGeliang Tang{
2601a6094788SGeliang Tang	# subflow IPv4-mapped to IPv4-mapped
2602c7d49c03SMatthieu Baerts	if reset "single subflow IPv4-mapped"; then
260334aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 1
260434aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 0 1
260534aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 "::ffff:10.0.3.2" flags subflow
2606a6094788SGeliang Tang		run_tests $ns1 $ns2 "::ffff:10.0.1.1"
2607c7d49c03SMatthieu Baerts		chk_join_nr 1 1 1
2608ae7bd9ccSMatthieu Baerts	fi
2609a6094788SGeliang Tang
2610a6094788SGeliang Tang	# signal address IPv4-mapped with IPv4-mapped sk
2611c7d49c03SMatthieu Baerts	if reset "signal address IPv4-mapped"; then
261234aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 1
261334aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 1 1
261434aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 "::ffff:10.0.2.1" flags signal
2615a6094788SGeliang Tang		run_tests $ns1 $ns2 "::ffff:10.0.1.1"
2616c7d49c03SMatthieu Baerts		chk_join_nr 1 1 1
2617a6094788SGeliang Tang		chk_add_nr 1 1
2618ae7bd9ccSMatthieu Baerts	fi
2619a6094788SGeliang Tang
2620a6094788SGeliang Tang	# subflow v4-map-v6
2621c7d49c03SMatthieu Baerts	if reset "single subflow v4-map-v6"; then
262234aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 1
262334aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 0 1
262434aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
2625a6094788SGeliang Tang		run_tests $ns1 $ns2 "::ffff:10.0.1.1"
2626c7d49c03SMatthieu Baerts		chk_join_nr 1 1 1
2627ae7bd9ccSMatthieu Baerts	fi
2628a6094788SGeliang Tang
2629a6094788SGeliang Tang	# signal address v4-map-v6
2630c7d49c03SMatthieu Baerts	if reset "signal address v4-map-v6"; then
263134aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 1
263234aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 1 1
263334aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
2634a6094788SGeliang Tang		run_tests $ns1 $ns2 "::ffff:10.0.1.1"
2635c7d49c03SMatthieu Baerts		chk_join_nr 1 1 1
2636a6094788SGeliang Tang		chk_add_nr 1 1
2637ae7bd9ccSMatthieu Baerts	fi
2638a6094788SGeliang Tang
2639a6094788SGeliang Tang	# subflow v6-map-v4
2640c7d49c03SMatthieu Baerts	if reset "single subflow v6-map-v4"; then
264134aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 1
264234aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 0 1
264334aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 "::ffff:10.0.3.2" flags subflow
2644a6094788SGeliang Tang		run_tests $ns1 $ns2 10.0.1.1
2645c7d49c03SMatthieu Baerts		chk_join_nr 1 1 1
2646ae7bd9ccSMatthieu Baerts	fi
2647a6094788SGeliang Tang
2648a6094788SGeliang Tang	# signal address v6-map-v4
2649c7d49c03SMatthieu Baerts	if reset "signal address v6-map-v4"; then
265034aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 1
265134aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 1 1
265234aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 "::ffff:10.0.2.1" flags signal
2653a6094788SGeliang Tang		run_tests $ns1 $ns2 10.0.1.1
2654c7d49c03SMatthieu Baerts		chk_join_nr 1 1 1
2655a6094788SGeliang Tang		chk_add_nr 1 1
2656ae7bd9ccSMatthieu Baerts	fi
2657a6094788SGeliang Tang
2658a6094788SGeliang Tang	# no subflow IPv6 to v4 address
2659c7d49c03SMatthieu Baerts	if reset "no JOIN with diff families v4-v6"; then
266034aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 1
266134aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 0 1
266234aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 dead:beef:2::2 flags subflow
2663a6094788SGeliang Tang		run_tests $ns1 $ns2 10.0.1.1
2664c7d49c03SMatthieu Baerts		chk_join_nr 0 0 0
2665ae7bd9ccSMatthieu Baerts	fi
2666a6094788SGeliang Tang
2667a6094788SGeliang Tang	# no subflow IPv6 to v4 address even if v6 has a valid v4 at the end
2668c7d49c03SMatthieu Baerts	if reset "no JOIN with diff families v4-v6-2"; then
266934aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 1
267034aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 0 1
267134aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 dead:beef:2::10.0.3.2 flags subflow
2672a6094788SGeliang Tang		run_tests $ns1 $ns2 10.0.1.1
2673c7d49c03SMatthieu Baerts		chk_join_nr 0 0 0
2674ae7bd9ccSMatthieu Baerts	fi
2675a6094788SGeliang Tang
2676a6094788SGeliang Tang	# no subflow IPv4 to v6 address, no need to slow down too then
2677c7d49c03SMatthieu Baerts	if reset "no JOIN with diff families v6-v4"; then
267834aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 1
267934aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 0 1
268034aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
2681a6094788SGeliang Tang		run_tests $ns1 $ns2 dead:beef:1::1
2682c7d49c03SMatthieu Baerts		chk_join_nr 0 0 0
2683ae7bd9ccSMatthieu Baerts	fi
26841002b89fSGeliang Tang}
2685a6094788SGeliang Tang
2686ad349374SPaolo Abenimixed_tests()
2687ad349374SPaolo Abeni{
26886673851bSMatthieu Baerts	if reset "IPv4 sockets do not use IPv6 addresses" &&
26896673851bSMatthieu Baerts	   continue_if mptcp_lib_kversion_ge 6.3; then
2690ad349374SPaolo Abeni		pm_nl_set_limits $ns1 0 1
2691ad349374SPaolo Abeni		pm_nl_set_limits $ns2 1 1
2692ad349374SPaolo Abeni		pm_nl_add_endpoint $ns1 dead:beef:2::1 flags signal
2693e571fb09SGeliang Tang		speed=slow \
2694e571fb09SGeliang Tang			run_tests $ns1 $ns2 10.0.1.1
2695ad349374SPaolo Abeni		chk_join_nr 0 0 0
2696ad349374SPaolo Abeni	fi
2697ad349374SPaolo Abeni
2698ad349374SPaolo Abeni	# Need an IPv6 mptcp socket to allow subflows of both families
26996673851bSMatthieu Baerts	if reset "simult IPv4 and IPv6 subflows" &&
27006673851bSMatthieu Baerts	   continue_if mptcp_lib_kversion_ge 6.3; then
2701ad349374SPaolo Abeni		pm_nl_set_limits $ns1 0 1
2702ad349374SPaolo Abeni		pm_nl_set_limits $ns2 1 1
2703ad349374SPaolo Abeni		pm_nl_add_endpoint $ns1 10.0.1.1 flags signal
2704e571fb09SGeliang Tang		speed=slow \
2705e571fb09SGeliang Tang			run_tests $ns1 $ns2 dead:beef:2::1
2706ad349374SPaolo Abeni		chk_join_nr 1 1 1
2707ad349374SPaolo Abeni	fi
2708ad349374SPaolo Abeni
2709ad349374SPaolo Abeni	# cross families subflows will not be created even in fullmesh mode
27106673851bSMatthieu Baerts	if reset "simult IPv4 and IPv6 subflows, fullmesh 1x1" &&
27116673851bSMatthieu Baerts	   continue_if mptcp_lib_kversion_ge 6.3; then
2712ad349374SPaolo Abeni		pm_nl_set_limits $ns1 0 4
2713ad349374SPaolo Abeni		pm_nl_set_limits $ns2 1 4
2714ad349374SPaolo Abeni		pm_nl_add_endpoint $ns2 dead:beef:2::2 flags subflow,fullmesh
2715ad349374SPaolo Abeni		pm_nl_add_endpoint $ns1 10.0.1.1 flags signal
2716e571fb09SGeliang Tang		speed=slow \
2717e571fb09SGeliang Tang			run_tests $ns1 $ns2 dead:beef:2::1
2718ad349374SPaolo Abeni		chk_join_nr 1 1 1
2719ad349374SPaolo Abeni	fi
2720ad349374SPaolo Abeni
2721ad349374SPaolo Abeni	# fullmesh still tries to create all the possibly subflows with
2722ad349374SPaolo Abeni	# matching family
27236673851bSMatthieu Baerts	if reset "simult IPv4 and IPv6 subflows, fullmesh 2x2" &&
27246673851bSMatthieu Baerts	   continue_if mptcp_lib_kversion_ge 6.3; then
2725ad349374SPaolo Abeni		pm_nl_set_limits $ns1 0 4
2726ad349374SPaolo Abeni		pm_nl_set_limits $ns2 2 4
2727ad349374SPaolo Abeni		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
2728ad349374SPaolo Abeni		pm_nl_add_endpoint $ns1 dead:beef:2::1 flags signal
2729e571fb09SGeliang Tang		fullmesh=1 speed=slow \
2730e571fb09SGeliang Tang			run_tests $ns1 $ns2 dead:beef:1::1
2731ad349374SPaolo Abeni		chk_join_nr 4 4 4
2732ad349374SPaolo Abeni	fi
2733ad349374SPaolo Abeni}
2734ad349374SPaolo Abeni
27351002b89fSGeliang Tangbackup_tests()
27361002b89fSGeliang Tang{
2737718eb44eSGeliang Tang	# single subflow, backup
273807216a3cSMatthieu Baerts	if reset "single subflow, backup" &&
273907216a3cSMatthieu Baerts	   continue_if mptcp_lib_kallsyms_has "subflow_rebuild_header$"; then
274034aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 1
274134aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 0 1
274234aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow,backup
2743e571fb09SGeliang Tang		sflags=nobackup speed=slow \
2744e571fb09SGeliang Tang			run_tests $ns1 $ns2 10.0.1.1
2745c7d49c03SMatthieu Baerts		chk_join_nr 1 1 1
2746718eb44eSGeliang Tang		chk_prio_nr 0 1
2747ae7bd9ccSMatthieu Baerts	fi
2748718eb44eSGeliang Tang
2749718eb44eSGeliang Tang	# single address, backup
275007216a3cSMatthieu Baerts	if reset "single address, backup" &&
275107216a3cSMatthieu Baerts	   continue_if mptcp_lib_kallsyms_has "subflow_rebuild_header$"; then
275234aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 1
275334aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
275434aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 1 1
2755e571fb09SGeliang Tang		sflags=backup speed=slow \
2756e571fb09SGeliang Tang			run_tests $ns1 $ns2 10.0.1.1
2757c7d49c03SMatthieu Baerts		chk_join_nr 1 1 1
2758718eb44eSGeliang Tang		chk_add_nr 1 1
2759d045b9ebSPaolo Abeni		chk_prio_nr 1 1
2760ae7bd9ccSMatthieu Baerts	fi
276133397b83SGeliang Tang
276233397b83SGeliang Tang	# single address with port, backup
276307216a3cSMatthieu Baerts	if reset "single address with port, backup" &&
276407216a3cSMatthieu Baerts	   continue_if mptcp_lib_kallsyms_has "subflow_rebuild_header$"; then
276534aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 1
276634aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal port 10100
276734aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 1 1
2768e571fb09SGeliang Tang		sflags=backup speed=slow \
2769e571fb09SGeliang Tang			run_tests $ns1 $ns2 10.0.1.1
2770c7d49c03SMatthieu Baerts		chk_join_nr 1 1 1
277133397b83SGeliang Tang		chk_add_nr 1 1
2772d045b9ebSPaolo Abeni		chk_prio_nr 1 1
2773ae7bd9ccSMatthieu Baerts	fi
2774914f6a59SPaolo Abeni
2775632978f0SMatthieu Baerts	if reset "mpc backup" &&
2776632978f0SMatthieu Baerts	   continue_if mptcp_lib_kallsyms_doesnt_have "mptcp_subflow_send_ack$"; then
2777914f6a59SPaolo Abeni		pm_nl_add_endpoint $ns2 10.0.1.2 flags subflow,backup
2778e571fb09SGeliang Tang		speed=slow \
2779e571fb09SGeliang Tang			run_tests $ns1 $ns2 10.0.1.1
2780914f6a59SPaolo Abeni		chk_join_nr 0 0 0
2781914f6a59SPaolo Abeni		chk_prio_nr 0 1
2782914f6a59SPaolo Abeni	fi
2783914f6a59SPaolo Abeni
2784632978f0SMatthieu Baerts	if reset "mpc backup both sides" &&
2785632978f0SMatthieu Baerts	   continue_if mptcp_lib_kallsyms_doesnt_have "mptcp_subflow_send_ack$"; then
2786914f6a59SPaolo Abeni		pm_nl_add_endpoint $ns1 10.0.1.1 flags subflow,backup
2787914f6a59SPaolo Abeni		pm_nl_add_endpoint $ns2 10.0.1.2 flags subflow,backup
2788e571fb09SGeliang Tang		speed=slow \
2789e571fb09SGeliang Tang			run_tests $ns1 $ns2 10.0.1.1
2790914f6a59SPaolo Abeni		chk_join_nr 0 0 0
2791914f6a59SPaolo Abeni		chk_prio_nr 1 1
2792914f6a59SPaolo Abeni	fi
2793914f6a59SPaolo Abeni
2794632978f0SMatthieu Baerts	if reset "mpc switch to backup" &&
2795632978f0SMatthieu Baerts	   continue_if mptcp_lib_kallsyms_doesnt_have "mptcp_subflow_send_ack$"; then
2796914f6a59SPaolo Abeni		pm_nl_add_endpoint $ns2 10.0.1.2 flags subflow
2797e571fb09SGeliang Tang		sflags=backup speed=slow \
2798e571fb09SGeliang Tang			run_tests $ns1 $ns2 10.0.1.1
2799914f6a59SPaolo Abeni		chk_join_nr 0 0 0
2800914f6a59SPaolo Abeni		chk_prio_nr 0 1
2801914f6a59SPaolo Abeni	fi
2802914f6a59SPaolo Abeni
2803632978f0SMatthieu Baerts	if reset "mpc switch to backup both sides" &&
2804632978f0SMatthieu Baerts	   continue_if mptcp_lib_kallsyms_doesnt_have "mptcp_subflow_send_ack$"; then
2805914f6a59SPaolo Abeni		pm_nl_add_endpoint $ns1 10.0.1.1 flags subflow
2806914f6a59SPaolo Abeni		pm_nl_add_endpoint $ns2 10.0.1.2 flags subflow
2807e571fb09SGeliang Tang		sflags=backup speed=slow \
2808e571fb09SGeliang Tang			run_tests $ns1 $ns2 10.0.1.1
2809914f6a59SPaolo Abeni		chk_join_nr 0 0 0
2810914f6a59SPaolo Abeni		chk_prio_nr 1 1
2811914f6a59SPaolo Abeni	fi
28121002b89fSGeliang Tang}
2813718eb44eSGeliang Tang
2814178d0232SGeliang TangLISTENER_CREATED=15 #MPTCP_EVENT_LISTENER_CREATED
2815178d0232SGeliang TangLISTENER_CLOSED=16  #MPTCP_EVENT_LISTENER_CLOSED
2816178d0232SGeliang Tang
2817178d0232SGeliang TangAF_INET=2
2818178d0232SGeliang TangAF_INET6=10
2819178d0232SGeliang Tang
2820178d0232SGeliang Tangverify_listener_events()
2821178d0232SGeliang Tang{
2822178d0232SGeliang Tang	local evt=$1
2823178d0232SGeliang Tang	local e_type=$2
2824178d0232SGeliang Tang	local e_family=$3
2825178d0232SGeliang Tang	local e_saddr=$4
2826178d0232SGeliang Tang	local e_sport=$5
2827178d0232SGeliang Tang	local type
2828178d0232SGeliang Tang	local family
2829178d0232SGeliang Tang	local saddr
2830178d0232SGeliang Tang	local sport
283196b84195SMatthieu Baerts	local name
2832178d0232SGeliang Tang
2833178d0232SGeliang Tang	if [ $e_type = $LISTENER_CREATED ]; then
283496b84195SMatthieu Baerts		name="LISTENER_CREATED"
2835178d0232SGeliang Tang	elif [ $e_type = $LISTENER_CLOSED ]; then
283696b84195SMatthieu Baerts		name="LISTENER_CLOSED "
283796b84195SMatthieu Baerts	else
283896b84195SMatthieu Baerts		name="$e_type"
2839178d0232SGeliang Tang	fi
2840178d0232SGeliang Tang
284103668c65SMatthieu Baerts	print_check "$name $e_saddr:$e_sport"
284296b84195SMatthieu Baerts
28430471bb47SMatthieu Baerts	if ! mptcp_lib_kallsyms_has "mptcp_event_pm_listener$"; then
284403668c65SMatthieu Baerts		print_skip "event not supported"
28450471bb47SMatthieu Baerts		return
28460471bb47SMatthieu Baerts	fi
28470471bb47SMatthieu Baerts
284896b84195SMatthieu Baerts	type=$(grep "type:$e_type," $evt | sed -n 's/.*\(type:\)\([[:digit:]]*\).*$/\2/p;q')
284996b84195SMatthieu Baerts	family=$(grep "type:$e_type," $evt | sed -n 's/.*\(family:\)\([[:digit:]]*\).*$/\2/p;q')
285096b84195SMatthieu Baerts	sport=$(grep "type:$e_type," $evt | sed -n 's/.*\(sport:\)\([[:digit:]]*\).*$/\2/p;q')
2851178d0232SGeliang Tang	if [ $family ] && [ $family = $AF_INET6 ]; then
285296b84195SMatthieu Baerts		saddr=$(grep "type:$e_type," $evt | sed -n 's/.*\(saddr6:\)\([0-9a-f:.]*\).*$/\2/p;q')
2853178d0232SGeliang Tang	else
285496b84195SMatthieu Baerts		saddr=$(grep "type:$e_type," $evt | sed -n 's/.*\(saddr4:\)\([0-9.]*\).*$/\2/p;q')
2855178d0232SGeliang Tang	fi
2856178d0232SGeliang Tang
2857178d0232SGeliang Tang	if [ $type ] && [ $type = $e_type ] &&
2858178d0232SGeliang Tang	   [ $family ] && [ $family = $e_family ] &&
2859178d0232SGeliang Tang	   [ $saddr ] && [ $saddr = $e_saddr ] &&
2860178d0232SGeliang Tang	   [ $sport ] && [ $sport = $e_sport ]; then
286103668c65SMatthieu Baerts		print_ok
2862178d0232SGeliang Tang		return 0
2863178d0232SGeliang Tang	fi
286403668c65SMatthieu Baerts	fail_test "$e_type:$type $e_family:$family $e_saddr:$saddr $e_sport:$sport"
2865178d0232SGeliang Tang}
2866178d0232SGeliang Tang
28671002b89fSGeliang Tangadd_addr_ports_tests()
28681002b89fSGeliang Tang{
28698a127bf6SGeliang Tang	# signal address with port
2870c7d49c03SMatthieu Baerts	if reset "signal address with port"; then
287134aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 1
287234aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 1 1
287334aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal port 10100
28748a127bf6SGeliang Tang		run_tests $ns1 $ns2 10.0.1.1
2875c7d49c03SMatthieu Baerts		chk_join_nr 1 1 1
28768a127bf6SGeliang Tang		chk_add_nr 1 1 1
2877ae7bd9ccSMatthieu Baerts	fi
28788a127bf6SGeliang Tang
28798a127bf6SGeliang Tang	# subflow and signal with port
2880c7d49c03SMatthieu Baerts	if reset "subflow and signal with port"; then
288134aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal port 10100
288234aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 2
288334aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 1 2
288434aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
28858a127bf6SGeliang Tang		run_tests $ns1 $ns2 10.0.1.1
2886c7d49c03SMatthieu Baerts		chk_join_nr 2 2 2
28878a127bf6SGeliang Tang		chk_add_nr 1 1 1
2888ae7bd9ccSMatthieu Baerts	fi
28898a127bf6SGeliang Tang
28908a127bf6SGeliang Tang	# single address with port, remove
2891178d0232SGeliang Tang	# pm listener events
2892178d0232SGeliang Tang	if reset_with_events "remove single address with port"; then
289334aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 1
289434aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal port 10100
289534aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 1 1
2896e571fb09SGeliang Tang		addr_nr_ns1=-1 speed=slow \
2897e571fb09SGeliang Tang			run_tests $ns1 $ns2 10.0.1.1
2898c7d49c03SMatthieu Baerts		chk_join_nr 1 1 1
28998a127bf6SGeliang Tang		chk_add_nr 1 1 1
29007028ba8aSGeliang Tang		chk_rm_nr 1 1 invert
2901178d0232SGeliang Tang
2902178d0232SGeliang Tang		verify_listener_events $evts_ns1 $LISTENER_CREATED $AF_INET 10.0.2.1 10100
2903178d0232SGeliang Tang		verify_listener_events $evts_ns1 $LISTENER_CLOSED $AF_INET 10.0.2.1 10100
2904178d0232SGeliang Tang		kill_events_pids
2905ae7bd9ccSMatthieu Baerts	fi
29068a127bf6SGeliang Tang
29078a127bf6SGeliang Tang	# subflow and signal with port, remove
2908c7d49c03SMatthieu Baerts	if reset "remove subflow and signal with port"; then
290934aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 2
291034aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal port 10100
291134aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 1 2
291234aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
2913e571fb09SGeliang Tang		addr_nr_ns1=-1 addr_nr_ns2=-1 speed=slow \
2914e571fb09SGeliang Tang			run_tests $ns1 $ns2 10.0.1.1
2915c7d49c03SMatthieu Baerts		chk_join_nr 2 2 2
29168a127bf6SGeliang Tang		chk_add_nr 1 1 1
29178a127bf6SGeliang Tang		chk_rm_nr 1 1
2918ae7bd9ccSMatthieu Baerts	fi
29198a127bf6SGeliang Tang
29208a127bf6SGeliang Tang	# subflows and signal with port, flush
2921c7d49c03SMatthieu Baerts	if reset "flush subflows and signal with port"; then
292234aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 3
292334aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal port 10100
292434aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 1 3
292534aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
292634aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.4.2 flags subflow
2927e571fb09SGeliang Tang		addr_nr_ns1=-8 addr_nr_ns2=-2 speed=slow \
2928e571fb09SGeliang Tang			run_tests $ns1 $ns2 10.0.1.1
2929c7d49c03SMatthieu Baerts		chk_join_nr 3 3 3
29308a127bf6SGeliang Tang		chk_add_nr 1 1
29316fa0174aSPaolo Abeni		chk_rm_nr 1 3 invert simult
2932ae7bd9ccSMatthieu Baerts	fi
29338a127bf6SGeliang Tang
29348a127bf6SGeliang Tang	# multiple addresses with port
2935c7d49c03SMatthieu Baerts	if reset "multiple addresses with port"; then
293634aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 2 2
293734aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal port 10100
293834aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.3.1 flags signal port 10100
293934aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 2 2
29408a127bf6SGeliang Tang		run_tests $ns1 $ns2 10.0.1.1
2941c7d49c03SMatthieu Baerts		chk_join_nr 2 2 2
29428a127bf6SGeliang Tang		chk_add_nr 2 2 2
2943ae7bd9ccSMatthieu Baerts	fi
29448a127bf6SGeliang Tang
29458a127bf6SGeliang Tang	# multiple addresses with ports
2946c7d49c03SMatthieu Baerts	if reset "multiple addresses with ports"; then
294734aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 2 2
294834aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal port 10100
294934aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.3.1 flags signal port 10101
295034aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 2 2
29518a127bf6SGeliang Tang		run_tests $ns1 $ns2 10.0.1.1
2952c7d49c03SMatthieu Baerts		chk_join_nr 2 2 2
29538a127bf6SGeliang Tang		chk_add_nr 2 2 2
2954ae7bd9ccSMatthieu Baerts	fi
29551002b89fSGeliang Tang}
29568a127bf6SGeliang Tang
29571002b89fSGeliang Tangsyncookies_tests()
29581002b89fSGeliang Tang{
295900587187SFlorian Westphal	# single subflow, syncookies
2960c7d49c03SMatthieu Baerts	if reset_with_cookies "single subflow with syn cookies"; then
296134aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 1
296234aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 0 1
296334aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
296400587187SFlorian Westphal		run_tests $ns1 $ns2 10.0.1.1
2965c7d49c03SMatthieu Baerts		chk_join_nr 1 1 1
2966ae7bd9ccSMatthieu Baerts	fi
296700587187SFlorian Westphal
296800587187SFlorian Westphal	# multiple subflows with syn cookies
2969c7d49c03SMatthieu Baerts	if reset_with_cookies "multiple subflows with syn cookies"; then
297034aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 2
297134aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 0 2
297234aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
297334aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.2.2 flags subflow
297400587187SFlorian Westphal		run_tests $ns1 $ns2 10.0.1.1
2975c7d49c03SMatthieu Baerts		chk_join_nr 2 2 2
2976ae7bd9ccSMatthieu Baerts	fi
297700587187SFlorian Westphal
297800587187SFlorian Westphal	# multiple subflows limited by server
2979c7d49c03SMatthieu Baerts	if reset_with_cookies "subflows limited by server w cookies"; then
298034aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 1
298134aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 0 2
298234aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
298334aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.2.2 flags subflow
298400587187SFlorian Westphal		run_tests $ns1 $ns2 10.0.1.1
2985c7d49c03SMatthieu Baerts		chk_join_nr 2 1 1
2986ae7bd9ccSMatthieu Baerts	fi
298700587187SFlorian Westphal
298800587187SFlorian Westphal	# test signal address with cookies
2989c7d49c03SMatthieu Baerts	if reset_with_cookies "signal address with syn cookies"; then
299034aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 1
299134aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 1 1
299234aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
299300587187SFlorian Westphal		run_tests $ns1 $ns2 10.0.1.1
2994c7d49c03SMatthieu Baerts		chk_join_nr 1 1 1
2995be613160SGeliang Tang		chk_add_nr 1 1
2996ae7bd9ccSMatthieu Baerts	fi
299700587187SFlorian Westphal
299800587187SFlorian Westphal	# test cookie with subflow and signal
2999c7d49c03SMatthieu Baerts	if reset_with_cookies "subflow and signal w cookies"; then
300034aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
300134aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 2
300234aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 1 2
300334aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
300400587187SFlorian Westphal		run_tests $ns1 $ns2 10.0.1.1
3005c7d49c03SMatthieu Baerts		chk_join_nr 2 2 2
3006be613160SGeliang Tang		chk_add_nr 1 1
3007ae7bd9ccSMatthieu Baerts	fi
300800587187SFlorian Westphal
300900587187SFlorian Westphal	# accept and use add_addr with additional subflows
3010c7d49c03SMatthieu Baerts	if reset_with_cookies "subflows and signal w. cookies"; then
301134aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 3
301234aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
301334aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 1 3
301434aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
301534aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.4.2 flags subflow
301600587187SFlorian Westphal		run_tests $ns1 $ns2 10.0.1.1
3017c7d49c03SMatthieu Baerts		chk_join_nr 3 3 3
3018be613160SGeliang Tang		chk_add_nr 1 1
3019ae7bd9ccSMatthieu Baerts	fi
30201002b89fSGeliang Tang}
30211002b89fSGeliang Tang
3022af66d3e1SGeliang Tangchecksum_tests()
3023af66d3e1SGeliang Tang{
3024af66d3e1SGeliang Tang	# checksum test 0 0
3025ae7bd9ccSMatthieu Baerts	if reset_with_checksum 0 0; then
302634aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 1
302734aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 0 1
3028af66d3e1SGeliang Tang		run_tests $ns1 $ns2 10.0.1.1
3029c7d49c03SMatthieu Baerts		chk_join_nr 0 0 0
3030ae7bd9ccSMatthieu Baerts	fi
3031af66d3e1SGeliang Tang
3032af66d3e1SGeliang Tang	# checksum test 1 1
3033ae7bd9ccSMatthieu Baerts	if reset_with_checksum 1 1; then
303434aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 1
303534aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 0 1
3036af66d3e1SGeliang Tang		run_tests $ns1 $ns2 10.0.1.1
3037c7d49c03SMatthieu Baerts		chk_join_nr 0 0 0
3038ae7bd9ccSMatthieu Baerts	fi
3039af66d3e1SGeliang Tang
3040af66d3e1SGeliang Tang	# checksum test 0 1
3041ae7bd9ccSMatthieu Baerts	if reset_with_checksum 0 1; then
304234aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 1
304334aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 0 1
3044af66d3e1SGeliang Tang		run_tests $ns1 $ns2 10.0.1.1
3045c7d49c03SMatthieu Baerts		chk_join_nr 0 0 0
3046ae7bd9ccSMatthieu Baerts	fi
3047af66d3e1SGeliang Tang
3048af66d3e1SGeliang Tang	# checksum test 1 0
3049ae7bd9ccSMatthieu Baerts	if reset_with_checksum 1 0; then
305034aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 1
305134aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 0 1
3052af66d3e1SGeliang Tang		run_tests $ns1 $ns2 10.0.1.1
3053c7d49c03SMatthieu Baerts		chk_join_nr 0 0 0
3054ae7bd9ccSMatthieu Baerts	fi
3055af66d3e1SGeliang Tang}
3056af66d3e1SGeliang Tang
30570cddb4a6SGeliang Tangdeny_join_id0_tests()
30580cddb4a6SGeliang Tang{
30590cddb4a6SGeliang Tang	# subflow allow join id0 ns1
3060c7d49c03SMatthieu Baerts	if reset_with_allow_join_id0 "single subflow allow join id0 ns1" 1 0; then
306134aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 1 1
306234aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 1 1
306334aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
30640cddb4a6SGeliang Tang		run_tests $ns1 $ns2 10.0.1.1
3065c7d49c03SMatthieu Baerts		chk_join_nr 1 1 1
3066ae7bd9ccSMatthieu Baerts	fi
30670cddb4a6SGeliang Tang
30680cddb4a6SGeliang Tang	# subflow allow join id0 ns2
3069c7d49c03SMatthieu Baerts	if reset_with_allow_join_id0 "single subflow allow join id0 ns2" 0 1; then
307034aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 1 1
307134aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 1 1
307234aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
30730cddb4a6SGeliang Tang		run_tests $ns1 $ns2 10.0.1.1
3074c7d49c03SMatthieu Baerts		chk_join_nr 0 0 0
3075ae7bd9ccSMatthieu Baerts	fi
30760cddb4a6SGeliang Tang
30770cddb4a6SGeliang Tang	# signal address allow join id0 ns1
30780cddb4a6SGeliang Tang	# ADD_ADDRs are not affected by allow_join_id0 value.
3079c7d49c03SMatthieu Baerts	if reset_with_allow_join_id0 "signal address allow join id0 ns1" 1 0; then
308034aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 1 1
308134aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 1 1
308234aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
30830cddb4a6SGeliang Tang		run_tests $ns1 $ns2 10.0.1.1
3084c7d49c03SMatthieu Baerts		chk_join_nr 1 1 1
30850cddb4a6SGeliang Tang		chk_add_nr 1 1
3086ae7bd9ccSMatthieu Baerts	fi
30870cddb4a6SGeliang Tang
30880cddb4a6SGeliang Tang	# signal address allow join id0 ns2
30890cddb4a6SGeliang Tang	# ADD_ADDRs are not affected by allow_join_id0 value.
3090c7d49c03SMatthieu Baerts	if reset_with_allow_join_id0 "signal address allow join id0 ns2" 0 1; then
309134aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 1 1
309234aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 1 1
309334aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
30940cddb4a6SGeliang Tang		run_tests $ns1 $ns2 10.0.1.1
3095c7d49c03SMatthieu Baerts		chk_join_nr 1 1 1
30960cddb4a6SGeliang Tang		chk_add_nr 1 1
3097ae7bd9ccSMatthieu Baerts	fi
30980cddb4a6SGeliang Tang
30990cddb4a6SGeliang Tang	# subflow and address allow join id0 ns1
3100c7d49c03SMatthieu Baerts	if reset_with_allow_join_id0 "subflow and address allow join id0 1" 1 0; then
310134aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 2 2
310234aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 2 2
310334aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
310434aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
31050cddb4a6SGeliang Tang		run_tests $ns1 $ns2 10.0.1.1
3106c7d49c03SMatthieu Baerts		chk_join_nr 2 2 2
3107ae7bd9ccSMatthieu Baerts	fi
31080cddb4a6SGeliang Tang
31090cddb4a6SGeliang Tang	# subflow and address allow join id0 ns2
3110c7d49c03SMatthieu Baerts	if reset_with_allow_join_id0 "subflow and address allow join id0 2" 0 1; then
311134aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 2 2
311234aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 2 2
311334aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
311434aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
31150cddb4a6SGeliang Tang		run_tests $ns1 $ns2 10.0.1.1
3116c7d49c03SMatthieu Baerts		chk_join_nr 1 1 1
3117ae7bd9ccSMatthieu Baerts	fi
31180cddb4a6SGeliang Tang}
31190cddb4a6SGeliang Tang
31204f49d633SGeliang Tangfullmesh_tests()
31214f49d633SGeliang Tang{
31224f49d633SGeliang Tang	# fullmesh 1
31234f49d633SGeliang Tang	# 2 fullmesh addrs in ns2, added before the connection,
31244f49d633SGeliang Tang	# 1 non-fullmesh addr in ns1, added during the connection.
3125c7d49c03SMatthieu Baerts	if reset "fullmesh test 2x1"; then
312634aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 0 4
312734aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 1 4
312834aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.2.2 flags subflow,fullmesh
312934aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow,fullmesh
3130e571fb09SGeliang Tang		addr_nr_ns1=1 speed=slow \
3131e571fb09SGeliang Tang			run_tests $ns1 $ns2 10.0.1.1
3132c7d49c03SMatthieu Baerts		chk_join_nr 4 4 4
31334f49d633SGeliang Tang		chk_add_nr 1 1
3134ae7bd9ccSMatthieu Baerts	fi
31354f49d633SGeliang Tang
31364f49d633SGeliang Tang	# fullmesh 2
31374f49d633SGeliang Tang	# 1 non-fullmesh addr in ns1, added before the connection,
31384f49d633SGeliang Tang	# 1 fullmesh addr in ns2, added during the connection.
3139c7d49c03SMatthieu Baerts	if reset "fullmesh test 1x1"; then
314034aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 1 3
314134aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 1 3
314234aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
3143e571fb09SGeliang Tang		fullmesh=1 speed=slow \
3144e571fb09SGeliang Tang			run_tests $ns1 $ns2 10.0.1.1
3145c7d49c03SMatthieu Baerts		chk_join_nr 3 3 3
31464f49d633SGeliang Tang		chk_add_nr 1 1
3147ae7bd9ccSMatthieu Baerts	fi
31484f49d633SGeliang Tang
31494f49d633SGeliang Tang	# fullmesh 3
31504f49d633SGeliang Tang	# 1 non-fullmesh addr in ns1, added before the connection,
31514f49d633SGeliang Tang	# 2 fullmesh addrs in ns2, added during the connection.
3152c7d49c03SMatthieu Baerts	if reset "fullmesh test 1x2"; then
315334aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 2 5
315434aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 1 5
315534aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
3156e571fb09SGeliang Tang		fullmesh=2 speed=slow \
3157e571fb09SGeliang Tang			run_tests $ns1 $ns2 10.0.1.1
3158c7d49c03SMatthieu Baerts		chk_join_nr 5 5 5
31594f49d633SGeliang Tang		chk_add_nr 1 1
3160ae7bd9ccSMatthieu Baerts	fi
31614f49d633SGeliang Tang
31624f49d633SGeliang Tang	# fullmesh 4
31634f49d633SGeliang Tang	# 1 non-fullmesh addr in ns1, added before the connection,
31644f49d633SGeliang Tang	# 2 fullmesh addrs in ns2, added during the connection,
31654f49d633SGeliang Tang	# limit max_subflows to 4.
3166c7d49c03SMatthieu Baerts	if reset "fullmesh test 1x2, limited"; then
316734aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 2 4
316834aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 1 4
316934aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
3170e571fb09SGeliang Tang		fullmesh=2 speed=slow \
3171e571fb09SGeliang Tang			run_tests $ns1 $ns2 10.0.1.1
3172c7d49c03SMatthieu Baerts		chk_join_nr 4 4 4
31734f49d633SGeliang Tang		chk_add_nr 1 1
3174ae7bd9ccSMatthieu Baerts	fi
31756a0653b9SGeliang Tang
31766a0653b9SGeliang Tang	# set fullmesh flag
31779db34c42SMatthieu Baerts	if reset "set fullmesh flag test" &&
31789db34c42SMatthieu Baerts	   continue_if mptcp_lib_kversion_ge 5.18; then
317934aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 4 4
318034aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.2.1 flags subflow
318134aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 4 4
3182e571fb09SGeliang Tang		addr_nr_ns2=1 sflags=fullmesh speed=slow \
3183e571fb09SGeliang Tang			run_tests $ns1 $ns2 10.0.1.1
3184c7d49c03SMatthieu Baerts		chk_join_nr 2 2 2
31856a0653b9SGeliang Tang		chk_rm_nr 0 1
3186ae7bd9ccSMatthieu Baerts	fi
31876a0653b9SGeliang Tang
31886a0653b9SGeliang Tang	# set nofullmesh flag
31899db34c42SMatthieu Baerts	if reset "set nofullmesh flag test" &&
31909db34c42SMatthieu Baerts	   continue_if mptcp_lib_kversion_ge 5.18; then
319134aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 4 4
319234aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.2.1 flags subflow,fullmesh
319334aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 4 4
3194e571fb09SGeliang Tang		fullmesh=1 sflags=nofullmesh speed=slow \
3195e571fb09SGeliang Tang			run_tests $ns1 $ns2 10.0.1.1
3196c7d49c03SMatthieu Baerts		chk_join_nr 2 2 2
31976a0653b9SGeliang Tang		chk_rm_nr 0 1
3198ae7bd9ccSMatthieu Baerts	fi
31996a0653b9SGeliang Tang
32006a0653b9SGeliang Tang	# set backup,fullmesh flags
32019db34c42SMatthieu Baerts	if reset "set backup,fullmesh flags test" &&
32029db34c42SMatthieu Baerts	   continue_if mptcp_lib_kversion_ge 5.18; then
320334aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 4 4
320434aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns1 10.0.2.1 flags subflow
320534aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 4 4
3206e571fb09SGeliang Tang		addr_nr_ns2=1 sflags=backup,fullmesh speed=slow \
3207e571fb09SGeliang Tang			run_tests $ns1 $ns2 10.0.1.1
3208c7d49c03SMatthieu Baerts		chk_join_nr 2 2 2
32096a0653b9SGeliang Tang		chk_prio_nr 0 1
32106a0653b9SGeliang Tang		chk_rm_nr 0 1
3211ae7bd9ccSMatthieu Baerts	fi
32126a0653b9SGeliang Tang
32136a0653b9SGeliang Tang	# set nobackup,nofullmesh flags
32149db34c42SMatthieu Baerts	if reset "set nobackup,nofullmesh flags test" &&
32159db34c42SMatthieu Baerts	   continue_if mptcp_lib_kversion_ge 5.18; then
321634aa6e3bSGeliang Tang		pm_nl_set_limits $ns1 4 4
321734aa6e3bSGeliang Tang		pm_nl_set_limits $ns2 4 4
321834aa6e3bSGeliang Tang		pm_nl_add_endpoint $ns2 10.0.2.2 flags subflow,backup,fullmesh
3219e571fb09SGeliang Tang		sflags=nobackup,nofullmesh speed=slow \
3220e571fb09SGeliang Tang			run_tests $ns1 $ns2 10.0.1.1
3221c7d49c03SMatthieu Baerts		chk_join_nr 2 2 2
32226a0653b9SGeliang Tang		chk_prio_nr 0 1
32236a0653b9SGeliang Tang		chk_rm_nr 0 1
3224ae7bd9ccSMatthieu Baerts	fi
32254f49d633SGeliang Tang}
32264f49d633SGeliang Tang
322701542c9bSGeliang Tangfastclose_tests()
322801542c9bSGeliang Tang{
3229ae947bb2SMatthieu Baerts	if reset_check_counter "fastclose test" "MPTcpExtMPFastcloseTx"; then
3230080b7f57SGeliang Tang		test_linkfail=1024 fastclose=client \
3231595ef566SGeliang Tang			run_tests $ns1 $ns2 10.0.1.1
3232c7d49c03SMatthieu Baerts		chk_join_nr 0 0 0
323301542c9bSGeliang Tang		chk_fclose_nr 1 1
323401542c9bSGeliang Tang		chk_rst_nr 1 1 invert
3235ae7bd9ccSMatthieu Baerts	fi
32366bf41020SPaolo Abeni
3237ae947bb2SMatthieu Baerts	if reset_check_counter "fastclose server test" "MPTcpExtMPFastcloseRx"; then
3238080b7f57SGeliang Tang		test_linkfail=1024 fastclose=server \
3239595ef566SGeliang Tang			run_tests $ns1 $ns2 10.0.1.1
32406bf41020SPaolo Abeni		chk_join_nr 0 0 0
32416bf41020SPaolo Abeni		chk_fclose_nr 1 1 invert
32426bf41020SPaolo Abeni		chk_rst_nr 1 1
32436bf41020SPaolo Abeni	fi
324401542c9bSGeliang Tang}
324501542c9bSGeliang Tang
3246b6e074e1SGeliang Tangpedit_action_pkts()
3247b6e074e1SGeliang Tang{
3248b6e074e1SGeliang Tang	tc -n $ns2 -j -s action show action pedit index 100 | \
3249c43ce398SGeliang Tang		grep "packets" | \
3250b6e074e1SGeliang Tang		sed 's/.*"packets":\([0-9]\+\),.*/\1/'
3251b6e074e1SGeliang Tang}
3252b6e074e1SGeliang Tang
3253b6e074e1SGeliang Tangfail_tests()
3254b6e074e1SGeliang Tang{
3255b6e074e1SGeliang Tang	# single subflow
3256b6e074e1SGeliang Tang	if reset_with_fail "Infinite map" 1; then
32570c93af1fSGeliang Tang		test_linkfail=128 \
32580c93af1fSGeliang Tang			run_tests $ns1 $ns2 10.0.1.1
3259b6e074e1SGeliang Tang		chk_join_nr 0 0 0 +1 +0 1 0 1 "$(pedit_action_pkts)"
32601f7d325fSGeliang Tang		chk_fail_nr 1 -1 invert
3261b6e074e1SGeliang Tang	fi
32622ba18161SGeliang Tang
32632ba18161SGeliang Tang	# multiple subflows
32642ba18161SGeliang Tang	if reset_with_fail "MP_FAIL MP_RST" 2; then
32652ba18161SGeliang Tang		tc -n $ns2 qdisc add dev ns2eth1 root netem rate 1mbit delay 5
32662ba18161SGeliang Tang		pm_nl_set_limits $ns1 0 1
32672ba18161SGeliang Tang		pm_nl_set_limits $ns2 0 1
32682ba18161SGeliang Tang		pm_nl_add_endpoint $ns2 10.0.2.2 dev ns2eth2 flags subflow
32690c93af1fSGeliang Tang		test_linkfail=1024 \
32700c93af1fSGeliang Tang			run_tests $ns1 $ns2 10.0.1.1
32712ba18161SGeliang Tang		chk_join_nr 1 1 1 1 0 1 1 0 "$(pedit_action_pkts)"
32722ba18161SGeliang Tang	fi
3273b6e074e1SGeliang Tang}
3274b6e074e1SGeliang Tang
32754369c198SGeliang Tanguserspace_pm_add_addr()
32764369c198SGeliang Tang{
32774369c198SGeliang Tang	local addr=$1
32784369c198SGeliang Tang	local id=$2
32794369c198SGeliang Tang	local tk
32804369c198SGeliang Tang
32814369c198SGeliang Tang	tk=$(grep "type:1," "$evts_ns1" |
32824369c198SGeliang Tang	     sed -n 's/.*\(token:\)\([[:digit:]]*\).*$/\2/p;q')
32834369c198SGeliang Tang	ip netns exec $ns1 ./pm_nl_ctl ann $addr token $tk id $id
32844369c198SGeliang Tang	sleep 1
32854369c198SGeliang Tang}
32864369c198SGeliang Tang
32874369c198SGeliang Tanguserspace_pm_rm_sf_addr_ns1()
32884369c198SGeliang Tang{
32894369c198SGeliang Tang	local addr=$1
32904369c198SGeliang Tang	local id=$2
32914369c198SGeliang Tang	local tk sp da dp
3292*e8b45937SGeliang Tang	local cnt_addr cnt_sf
32934369c198SGeliang Tang
32944369c198SGeliang Tang	tk=$(grep "type:1," "$evts_ns1" |
32954369c198SGeliang Tang	     sed -n 's/.*\(token:\)\([[:digit:]]*\).*$/\2/p;q')
32964369c198SGeliang Tang	sp=$(grep "type:10" "$evts_ns1" |
32974369c198SGeliang Tang	     sed -n 's/.*\(sport:\)\([[:digit:]]*\).*$/\2/p;q')
32984369c198SGeliang Tang	da=$(grep "type:10" "$evts_ns1" |
32994369c198SGeliang Tang	     sed -n 's/.*\(daddr6:\)\([0-9a-f:.]*\).*$/\2/p;q')
33004369c198SGeliang Tang	dp=$(grep "type:10" "$evts_ns1" |
33014369c198SGeliang Tang	     sed -n 's/.*\(dport:\)\([[:digit:]]*\).*$/\2/p;q')
3302*e8b45937SGeliang Tang	cnt_addr=$(rm_addr_count ${ns1})
3303*e8b45937SGeliang Tang	cnt_sf=$(rm_sf_count ${ns1})
33044369c198SGeliang Tang	ip netns exec $ns1 ./pm_nl_ctl rem token $tk id $id
33054369c198SGeliang Tang	ip netns exec $ns1 ./pm_nl_ctl dsf lip "::ffff:$addr" \
33064369c198SGeliang Tang				lport $sp rip $da rport $dp token $tk
3307*e8b45937SGeliang Tang	wait_rm_addr $ns1 "${cnt_addr}"
3308*e8b45937SGeliang Tang	wait_rm_sf $ns1 "${cnt_sf}"
33094369c198SGeliang Tang}
33104369c198SGeliang Tang
33114369c198SGeliang Tanguserspace_pm_add_sf()
33124369c198SGeliang Tang{
33134369c198SGeliang Tang	local addr=$1
33144369c198SGeliang Tang	local id=$2
33154369c198SGeliang Tang	local tk da dp
33164369c198SGeliang Tang
33174369c198SGeliang Tang	tk=$(sed -n 's/.*\(token:\)\([[:digit:]]*\).*$/\2/p;q' "$evts_ns2")
33184369c198SGeliang Tang	da=$(sed -n 's/.*\(daddr4:\)\([0-9.]*\).*$/\2/p;q' "$evts_ns2")
33194369c198SGeliang Tang	dp=$(sed -n 's/.*\(dport:\)\([[:digit:]]*\).*$/\2/p;q' "$evts_ns2")
33204369c198SGeliang Tang	ip netns exec $ns2 ./pm_nl_ctl csf lip $addr lid $id \
33214369c198SGeliang Tang				rip $da rport $dp token $tk
33224369c198SGeliang Tang	sleep 1
33234369c198SGeliang Tang}
33244369c198SGeliang Tang
33254369c198SGeliang Tanguserspace_pm_rm_sf_addr_ns2()
33264369c198SGeliang Tang{
33274369c198SGeliang Tang	local addr=$1
33284369c198SGeliang Tang	local id=$2
33294369c198SGeliang Tang	local tk da dp sp
3330*e8b45937SGeliang Tang	local cnt_addr cnt_sf
33314369c198SGeliang Tang
33324369c198SGeliang Tang	tk=$(sed -n 's/.*\(token:\)\([[:digit:]]*\).*$/\2/p;q' "$evts_ns2")
33334369c198SGeliang Tang	da=$(sed -n 's/.*\(daddr4:\)\([0-9.]*\).*$/\2/p;q' "$evts_ns2")
33344369c198SGeliang Tang	dp=$(sed -n 's/.*\(dport:\)\([[:digit:]]*\).*$/\2/p;q' "$evts_ns2")
33354369c198SGeliang Tang	sp=$(grep "type:10" "$evts_ns2" |
33364369c198SGeliang Tang	     sed -n 's/.*\(sport:\)\([[:digit:]]*\).*$/\2/p;q')
3337*e8b45937SGeliang Tang	cnt_addr=$(rm_addr_count ${ns2})
3338*e8b45937SGeliang Tang	cnt_sf=$(rm_sf_count ${ns2})
33394369c198SGeliang Tang	ip netns exec $ns2 ./pm_nl_ctl rem token $tk id $id
33404369c198SGeliang Tang	ip netns exec $ns2 ./pm_nl_ctl dsf lip $addr lport $sp \
33414369c198SGeliang Tang				rip $da rport $dp token $tk
3342*e8b45937SGeliang Tang	wait_rm_addr $ns2 "${cnt_addr}"
3343*e8b45937SGeliang Tang	wait_rm_sf $ns2 "${cnt_sf}"
33444369c198SGeliang Tang}
33454369c198SGeliang Tang
33465ac1d2d6SMat Martineauuserspace_tests()
33475ac1d2d6SMat Martineau{
33485ac1d2d6SMat Martineau	# userspace pm type prevents add_addr
3349f2b492b0SMatthieu Baerts	if reset "userspace pm type prevents add_addr" &&
3350f2b492b0SMatthieu Baerts	   continue_if mptcp_lib_has_file '/proc/sys/net/mptcp/pm_type'; then
33515ac1d2d6SMat Martineau		set_userspace_pm $ns1
33525ac1d2d6SMat Martineau		pm_nl_set_limits $ns1 0 2
33535ac1d2d6SMat Martineau		pm_nl_set_limits $ns2 0 2
33545ac1d2d6SMat Martineau		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
33555ac1d2d6SMat Martineau		run_tests $ns1 $ns2 10.0.1.1
33565ac1d2d6SMat Martineau		chk_join_nr 0 0 0
33575ac1d2d6SMat Martineau		chk_add_nr 0 0
33585ac1d2d6SMat Martineau	fi
33595ac1d2d6SMat Martineau
3360b3b71bf9SMat Martineau	# userspace pm type does not echo add_addr without daemon
3361f2b492b0SMatthieu Baerts	if reset "userspace pm no echo w/o daemon" &&
3362f2b492b0SMatthieu Baerts	   continue_if mptcp_lib_has_file '/proc/sys/net/mptcp/pm_type'; then
3363b3b71bf9SMat Martineau		set_userspace_pm $ns2
3364b3b71bf9SMat Martineau		pm_nl_set_limits $ns1 0 2
3365b3b71bf9SMat Martineau		pm_nl_set_limits $ns2 0 2
3366b3b71bf9SMat Martineau		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
3367b3b71bf9SMat Martineau		run_tests $ns1 $ns2 10.0.1.1
3368b3b71bf9SMat Martineau		chk_join_nr 0 0 0
3369b3b71bf9SMat Martineau		chk_add_nr 1 0
3370b3b71bf9SMat Martineau	fi
3371b3b71bf9SMat Martineau
33725ac1d2d6SMat Martineau	# userspace pm type rejects join
3373f2b492b0SMatthieu Baerts	if reset "userspace pm type rejects join" &&
3374f2b492b0SMatthieu Baerts	   continue_if mptcp_lib_has_file '/proc/sys/net/mptcp/pm_type'; then
33755ac1d2d6SMat Martineau		set_userspace_pm $ns1
33765ac1d2d6SMat Martineau		pm_nl_set_limits $ns1 1 1
33775ac1d2d6SMat Martineau		pm_nl_set_limits $ns2 1 1
33785ac1d2d6SMat Martineau		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
33795ac1d2d6SMat Martineau		run_tests $ns1 $ns2 10.0.1.1
33805ac1d2d6SMat Martineau		chk_join_nr 1 1 0
33815ac1d2d6SMat Martineau	fi
33825ac1d2d6SMat Martineau
33835ac1d2d6SMat Martineau	# userspace pm type does not send join
3384f2b492b0SMatthieu Baerts	if reset "userspace pm type does not send join" &&
3385f2b492b0SMatthieu Baerts	   continue_if mptcp_lib_has_file '/proc/sys/net/mptcp/pm_type'; then
33865ac1d2d6SMat Martineau		set_userspace_pm $ns2
33875ac1d2d6SMat Martineau		pm_nl_set_limits $ns1 1 1
33885ac1d2d6SMat Martineau		pm_nl_set_limits $ns2 1 1
33895ac1d2d6SMat Martineau		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
33905ac1d2d6SMat Martineau		run_tests $ns1 $ns2 10.0.1.1
33915ac1d2d6SMat Martineau		chk_join_nr 0 0 0
33925ac1d2d6SMat Martineau	fi
33935ac1d2d6SMat Martineau
33945ac1d2d6SMat Martineau	# userspace pm type prevents mp_prio
3395f2b492b0SMatthieu Baerts	if reset "userspace pm type prevents mp_prio" &&
3396f2b492b0SMatthieu Baerts	   continue_if mptcp_lib_has_file '/proc/sys/net/mptcp/pm_type'; then
33975ac1d2d6SMat Martineau		set_userspace_pm $ns1
33985ac1d2d6SMat Martineau		pm_nl_set_limits $ns1 1 1
33995ac1d2d6SMat Martineau		pm_nl_set_limits $ns2 1 1
34005ac1d2d6SMat Martineau		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
3401e571fb09SGeliang Tang		sflags=backup speed=slow \
3402e571fb09SGeliang Tang			run_tests $ns1 $ns2 10.0.1.1
34035ac1d2d6SMat Martineau		chk_join_nr 1 1 0
34045ac1d2d6SMat Martineau		chk_prio_nr 0 0
34055ac1d2d6SMat Martineau	fi
34065ac1d2d6SMat Martineau
34075ac1d2d6SMat Martineau	# userspace pm type prevents rm_addr
3408f2b492b0SMatthieu Baerts	if reset "userspace pm type prevents rm_addr" &&
3409f2b492b0SMatthieu Baerts	   continue_if mptcp_lib_has_file '/proc/sys/net/mptcp/pm_type'; then
34105ac1d2d6SMat Martineau		set_userspace_pm $ns1
34115ac1d2d6SMat Martineau		set_userspace_pm $ns2
34125ac1d2d6SMat Martineau		pm_nl_set_limits $ns1 0 1
34135ac1d2d6SMat Martineau		pm_nl_set_limits $ns2 0 1
34145ac1d2d6SMat Martineau		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
3415e571fb09SGeliang Tang		addr_nr_ns2=-1 speed=slow \
3416e571fb09SGeliang Tang			run_tests $ns1 $ns2 10.0.1.1
34175ac1d2d6SMat Martineau		chk_join_nr 0 0 0
34185ac1d2d6SMat Martineau		chk_rm_nr 0 0
34195ac1d2d6SMat Martineau	fi
342097040cf9SGeliang Tang
342197040cf9SGeliang Tang	# userspace pm add & remove address
3422f2b492b0SMatthieu Baerts	if reset_with_events "userspace pm add & remove address" &&
3423f2b492b0SMatthieu Baerts	   continue_if mptcp_lib_has_file '/proc/sys/net/mptcp/pm_type'; then
342497040cf9SGeliang Tang		set_userspace_pm $ns1
342597040cf9SGeliang Tang		pm_nl_set_limits $ns2 1 1
34261c6d07ddSGeliang Tang		speed=5 \
3427e571fb09SGeliang Tang			run_tests $ns1 $ns2 10.0.1.1 &
34284369c198SGeliang Tang		local tests_pid=$!
34294369c198SGeliang Tang		wait_mpj $ns1
34304369c198SGeliang Tang		userspace_pm_add_addr 10.0.2.1 10
343197040cf9SGeliang Tang		chk_join_nr 1 1 1
343297040cf9SGeliang Tang		chk_add_nr 1 1
3433d7ced753SGeliang Tang		chk_mptcp_info subflows 1 subflows 1
3434d7ced753SGeliang Tang		chk_mptcp_info add_addr_signal 1 add_addr_accepted 1
34354369c198SGeliang Tang		userspace_pm_rm_sf_addr_ns1 10.0.2.1 10
343697040cf9SGeliang Tang		chk_rm_nr 1 1 invert
3437d7ced753SGeliang Tang		chk_mptcp_info subflows 0 subflows 0
3438a3735625SGeliang Tang		kill_events_pids
34394369c198SGeliang Tang		wait $tests_pid
344097040cf9SGeliang Tang	fi
34415e986ec4SGeliang Tang
34425e986ec4SGeliang Tang	# userspace pm create destroy subflow
3443f2b492b0SMatthieu Baerts	if reset_with_events "userspace pm create destroy subflow" &&
3444f2b492b0SMatthieu Baerts	   continue_if mptcp_lib_has_file '/proc/sys/net/mptcp/pm_type'; then
34455e986ec4SGeliang Tang		set_userspace_pm $ns2
34465e986ec4SGeliang Tang		pm_nl_set_limits $ns1 0 1
34471c6d07ddSGeliang Tang		speed=5 \
3448e571fb09SGeliang Tang			run_tests $ns1 $ns2 10.0.1.1 &
34494369c198SGeliang Tang		local tests_pid=$!
34504369c198SGeliang Tang		wait_mpj $ns2
34514369c198SGeliang Tang		userspace_pm_add_sf 10.0.3.2 20
34525e986ec4SGeliang Tang		chk_join_nr 1 1 1
3453d7ced753SGeliang Tang		chk_mptcp_info subflows 1 subflows 1
34544369c198SGeliang Tang		userspace_pm_rm_sf_addr_ns2 10.0.3.2 20
34556c160b63SGeliang Tang		chk_rm_nr 1 1
3456d7ced753SGeliang Tang		chk_mptcp_info subflows 0 subflows 0
3457a3735625SGeliang Tang		kill_events_pids
34584369c198SGeliang Tang		wait $tests_pid
34595e986ec4SGeliang Tang	fi
34605ac1d2d6SMat Martineau}
34615ac1d2d6SMat Martineau
3462e274f715SPaolo Abeniendpoint_tests()
346369c6ce7bSPaolo Abeni{
346436c4127aSMatthieu Baerts	# subflow_rebuild_header is needed to support the implicit flag
346569c6ce7bSPaolo Abeni	# userspace pm type prevents add_addr
346636c4127aSMatthieu Baerts	if reset "implicit EP" &&
346736c4127aSMatthieu Baerts	   mptcp_lib_kallsyms_has "subflow_rebuild_header$"; then
346869c6ce7bSPaolo Abeni		pm_nl_set_limits $ns1 2 2
346969c6ce7bSPaolo Abeni		pm_nl_set_limits $ns2 2 2
347069c6ce7bSPaolo Abeni		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
3471e571fb09SGeliang Tang		speed=slow \
3472e571fb09SGeliang Tang			run_tests $ns1 $ns2 10.0.1.1 2>/dev/null &
347369c6ce7bSPaolo Abeni
347469c6ce7bSPaolo Abeni		wait_mpj $ns1
347503668c65SMatthieu Baerts		pm_nl_check_endpoint "creation" \
347669c6ce7bSPaolo Abeni			$ns2 10.0.2.2 id 1 flags implicit
3477d7ced753SGeliang Tang		chk_mptcp_info subflows 1 subflows 1
3478d7ced753SGeliang Tang		chk_mptcp_info add_addr_signal 1 add_addr_accepted 1
347969c6ce7bSPaolo Abeni
34801dc88d24SMatthieu Baerts		pm_nl_add_endpoint $ns2 10.0.2.2 id 33 2>/dev/null
348103668c65SMatthieu Baerts		pm_nl_check_endpoint "ID change is prevented" \
348269c6ce7bSPaolo Abeni			$ns2 10.0.2.2 id 1 flags implicit
348369c6ce7bSPaolo Abeni
348469c6ce7bSPaolo Abeni		pm_nl_add_endpoint $ns2 10.0.2.2 flags signal
348503668c65SMatthieu Baerts		pm_nl_check_endpoint "modif is allowed" \
348669c6ce7bSPaolo Abeni			$ns2 10.0.2.2 id 1 flags signal
3487070d6dafSMatthieu Baerts		kill_tests_wait
3488ae7bd9ccSMatthieu Baerts	fi
3489e274f715SPaolo Abeni
349036c4127aSMatthieu Baerts	if reset "delete and re-add" &&
349136c4127aSMatthieu Baerts	   mptcp_lib_kallsyms_has "subflow_rebuild_header$"; then
3492e274f715SPaolo Abeni		pm_nl_set_limits $ns1 1 1
3493e274f715SPaolo Abeni		pm_nl_set_limits $ns2 1 1
3494e274f715SPaolo Abeni		pm_nl_add_endpoint $ns2 10.0.2.2 id 2 dev ns2eth2 flags subflow
3495e571fb09SGeliang Tang		test_linkfail=4 speed=20 \
3496e571fb09SGeliang Tang			run_tests $ns1 $ns2 10.0.1.1 2>/dev/null &
3497e274f715SPaolo Abeni
3498e274f715SPaolo Abeni		wait_mpj $ns2
349903668c65SMatthieu Baerts		chk_subflow_nr "before delete" 2
3500d7ced753SGeliang Tang		chk_mptcp_info subflows 1 subflows 1
35019095ce97SGeliang Tang
3502e274f715SPaolo Abeni		pm_nl_del_endpoint $ns2 2 10.0.2.2
3503e274f715SPaolo Abeni		sleep 0.5
350403668c65SMatthieu Baerts		chk_subflow_nr "after delete" 1
3505d7ced753SGeliang Tang		chk_mptcp_info subflows 0 subflows 0
3506e274f715SPaolo Abeni
3507e274f715SPaolo Abeni		pm_nl_add_endpoint $ns2 10.0.2.2 dev ns2eth2 flags subflow
3508e274f715SPaolo Abeni		wait_mpj $ns2
350903668c65SMatthieu Baerts		chk_subflow_nr "after re-add" 2
3510d7ced753SGeliang Tang		chk_mptcp_info subflows 1 subflows 1
3511070d6dafSMatthieu Baerts		kill_tests_wait
3512e274f715SPaolo Abeni	fi
351369c6ce7bSPaolo Abeni}
351469c6ce7bSPaolo Abeni
351522514d52SMatthieu Baerts# [$1: error message]
35161002b89fSGeliang Tangusage()
35171002b89fSGeliang Tang{
351822514d52SMatthieu Baerts	if [ -n "${1}" ]; then
351922514d52SMatthieu Baerts		echo "${1}"
352022514d52SMatthieu Baerts		ret=1
352122514d52SMatthieu Baerts	fi
352222514d52SMatthieu Baerts
35231002b89fSGeliang Tang	echo "mptcp_join usage:"
35243afd0280SMatthieu Baerts
35253afd0280SMatthieu Baerts	local key
35263afd0280SMatthieu Baerts	for key in "${!all_tests[@]}"; do
35273afd0280SMatthieu Baerts		echo "  -${key} ${all_tests[${key}]}"
35283afd0280SMatthieu Baerts	done
35293afd0280SMatthieu Baerts
3530a673321aSMat Martineau	echo "  -c capture pcap files"
3531af66d3e1SGeliang Tang	echo "  -C enable data checksum"
3532621bd393SGeliang Tang	echo "  -i use ip mptcp"
35331002b89fSGeliang Tang	echo "  -h help"
353422514d52SMatthieu Baerts
3535c7d49c03SMatthieu Baerts	echo "[test ids|names]"
3536ae7bd9ccSMatthieu Baerts
353722514d52SMatthieu Baerts	exit ${ret}
35381002b89fSGeliang Tang}
35391002b89fSGeliang Tang
3540a673321aSMat Martineau
35413afd0280SMatthieu Baerts# Use a "simple" array to force an specific order we cannot have with an associative one
35423afd0280SMatthieu Baertsall_tests_sorted=(
35433afd0280SMatthieu Baerts	f@subflows_tests
35443afd0280SMatthieu Baerts	e@subflows_error_tests
35453afd0280SMatthieu Baerts	s@signal_address_tests
35463afd0280SMatthieu Baerts	l@link_failure_tests
35473afd0280SMatthieu Baerts	t@add_addr_timeout_tests
35483afd0280SMatthieu Baerts	r@remove_tests
35493afd0280SMatthieu Baerts	a@add_tests
35503afd0280SMatthieu Baerts	6@ipv6_tests
35513afd0280SMatthieu Baerts	4@v4mapped_tests
3552ad349374SPaolo Abeni	M@mixed_tests
35533afd0280SMatthieu Baerts	b@backup_tests
35543afd0280SMatthieu Baerts	p@add_addr_ports_tests
35553afd0280SMatthieu Baerts	k@syncookies_tests
35563afd0280SMatthieu Baerts	S@checksum_tests
35573afd0280SMatthieu Baerts	d@deny_join_id0_tests
35583afd0280SMatthieu Baerts	m@fullmesh_tests
35593afd0280SMatthieu Baerts	z@fastclose_tests
3560b6e074e1SGeliang Tang	F@fail_tests
35615ac1d2d6SMat Martineau	u@userspace_tests
3562e274f715SPaolo Abeni	I@endpoint_tests
35633afd0280SMatthieu Baerts)
35643afd0280SMatthieu Baerts
35653afd0280SMatthieu Baertsall_tests_args=""
35663afd0280SMatthieu Baertsall_tests_names=()
35673afd0280SMatthieu Baertsfor subtests in "${all_tests_sorted[@]}"; do
35683afd0280SMatthieu Baerts	key="${subtests%@*}"
35693afd0280SMatthieu Baerts	value="${subtests#*@}"
35703afd0280SMatthieu Baerts
35713afd0280SMatthieu Baerts	all_tests_args+="${key}"
35723afd0280SMatthieu Baerts	all_tests_names+=("${value}")
35733afd0280SMatthieu Baerts	all_tests[${key}]="${value}"
35743afd0280SMatthieu Baertsdone
35753afd0280SMatthieu Baerts
3576826d7bdcSMatthieu Baertstests=()
35773afd0280SMatthieu Baertswhile getopts "${all_tests_args}cCih" opt; do
35781002b89fSGeliang Tang	case $opt in
35793afd0280SMatthieu Baerts		["${all_tests_args}"])
35803afd0280SMatthieu Baerts			tests+=("${all_tests[${opt}]}")
358169c6ce7bSPaolo Abeni			;;
3582a673321aSMat Martineau		c)
3583826d7bdcSMatthieu Baerts			capture=1
3584a673321aSMat Martineau			;;
3585af66d3e1SGeliang Tang		C)
3586826d7bdcSMatthieu Baerts			checksum=1
3587af66d3e1SGeliang Tang			;;
3588621bd393SGeliang Tang		i)
3589826d7bdcSMatthieu Baerts			ip_mptcp=1
3590621bd393SGeliang Tang			;;
359122514d52SMatthieu Baerts		h)
35921002b89fSGeliang Tang			usage
35931002b89fSGeliang Tang			;;
359422514d52SMatthieu Baerts		*)
359522514d52SMatthieu Baerts			usage "Unknown option: -${opt}"
359622514d52SMatthieu Baerts			;;
35971002b89fSGeliang Tang	esac
35981002b89fSGeliang Tangdone
359900587187SFlorian Westphal
3600ae7bd9ccSMatthieu Baertsshift $((OPTIND - 1))
3601ae7bd9ccSMatthieu Baerts
3602ae7bd9ccSMatthieu Baertsfor arg in "${@}"; do
3603ae7bd9ccSMatthieu Baerts	if [[ "${arg}" =~ ^[0-9]+$ ]]; then
3604c7d49c03SMatthieu Baerts		only_tests_ids+=("${arg}")
3605ae7bd9ccSMatthieu Baerts	else
3606c7d49c03SMatthieu Baerts		only_tests_names+=("${arg}")
3607ae7bd9ccSMatthieu Baerts	fi
3608ae7bd9ccSMatthieu Baertsdone
3609ae7bd9ccSMatthieu Baerts
3610826d7bdcSMatthieu Baertsif [ ${#tests[@]} -eq 0 ]; then
36113afd0280SMatthieu Baerts	tests=("${all_tests_names[@]}")
36123afd0280SMatthieu Baertsfi
36133afd0280SMatthieu Baerts
3614826d7bdcSMatthieu Baertsfor subtests in "${tests[@]}"; do
3615826d7bdcSMatthieu Baerts	"${subtests}"
3616826d7bdcSMatthieu Baertsdone
3617826d7bdcSMatthieu Baerts
361839aab882SMatthieu Baertsif [ ${ret} -ne 0 ]; then
361939aab882SMatthieu Baerts	echo
362039aab882SMatthieu Baerts	echo "${#failed_tests[@]} failure(s) has(ve) been detected:"
362139aab882SMatthieu Baerts	for i in $(get_failed_tests_ids); do
362239aab882SMatthieu Baerts		echo -e "\t- ${i}: ${failed_tests[${i}]}"
362339aab882SMatthieu Baerts	done
362439aab882SMatthieu Baerts	echo
362539aab882SMatthieu Baertsfi
362639aab882SMatthieu Baerts
36277f117cd3SMatthieu Baertsappend_prev_results
36287f117cd3SMatthieu Baertsmptcp_lib_result_print_all_tap
36297f117cd3SMatthieu Baerts
3630b08fbf24SPaolo Abeniexit $ret
3631