1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3
4# Double quotes to prevent globbing and word splitting is recommended in new
5# code but we accept it, especially because there were too many before having
6# address all other issues detected by shellcheck.
7#shellcheck disable=SC2086
8
9ret=0
10sin=""
11sinfail=""
12sout=""
13cin=""
14cinfail=""
15cinsent=""
16cout=""
17capout=""
18ns1=""
19ns2=""
20ksft_skip=4
21timeout_poll=30
22timeout_test=$((timeout_poll * 2 + 1))
23capture=0
24checksum=0
25ip_mptcp=0
26check_invert=0
27validate_checksum=0
28init=0
29
30declare -A all_tests
31declare -a only_tests_ids
32declare -a only_tests_names
33declare -A failed_tests
34TEST_COUNT=0
35TEST_NAME=""
36nr_blank=40
37
38export FAILING_LINKS=""
39
40# generated using "nfbpf_compile '(ip && (ip[54] & 0xf0) == 0x30) ||
41#				  (ip6 && (ip6[74] & 0xf0) == 0x30)'"
42CBPF_MPTCP_SUBOPTION_ADD_ADDR="14,
43			       48 0 0 0,
44			       84 0 0 240,
45			       21 0 3 64,
46			       48 0 0 54,
47			       84 0 0 240,
48			       21 6 7 48,
49			       48 0 0 0,
50			       84 0 0 240,
51			       21 0 4 96,
52			       48 0 0 74,
53			       84 0 0 240,
54			       21 0 1 48,
55			       6 0 0 65535,
56			       6 0 0 0"
57
58init_partial()
59{
60	capout=$(mktemp)
61
62	local rndh
63	rndh=$(mktemp -u XXXXXX)
64
65	ns1="ns1-$rndh"
66	ns2="ns2-$rndh"
67
68	local netns
69	for netns in "$ns1" "$ns2"; do
70		ip netns add $netns || exit $ksft_skip
71		ip -net $netns link set lo up
72		ip netns exec $netns sysctl -q net.mptcp.enabled=1
73		ip netns exec $netns sysctl -q net.mptcp.pm_type=0
74		ip netns exec $netns sysctl -q net.ipv4.conf.all.rp_filter=0
75		ip netns exec $netns sysctl -q net.ipv4.conf.default.rp_filter=0
76		if [ $checksum -eq 1 ]; then
77			ip netns exec $netns sysctl -q net.mptcp.checksum_enabled=1
78		fi
79	done
80
81	check_invert=0
82	validate_checksum=$checksum
83	FAILING_LINKS=""
84
85	#  ns1         ns2
86	# ns1eth1    ns2eth1
87	# ns1eth2    ns2eth2
88	# ns1eth3    ns2eth3
89	# ns1eth4    ns2eth4
90
91	local i
92	for i in $(seq 1 4); do
93		ip link add ns1eth$i netns "$ns1" type veth peer name ns2eth$i netns "$ns2"
94		ip -net "$ns1" addr add 10.0.$i.1/24 dev ns1eth$i
95		ip -net "$ns1" addr add dead:beef:$i::1/64 dev ns1eth$i nodad
96		ip -net "$ns1" link set ns1eth$i up
97
98		ip -net "$ns2" addr add 10.0.$i.2/24 dev ns2eth$i
99		ip -net "$ns2" addr add dead:beef:$i::2/64 dev ns2eth$i nodad
100		ip -net "$ns2" link set ns2eth$i up
101
102		# let $ns2 reach any $ns1 address from any interface
103		ip -net "$ns2" route add default via 10.0.$i.1 dev ns2eth$i metric 10$i
104		ip -net "$ns2" route add default via dead:beef:$i::1 dev ns2eth$i metric 10$i
105	done
106}
107
108init_shapers()
109{
110	local i
111	for i in $(seq 1 4); do
112		tc -n $ns1 qdisc add dev ns1eth$i root netem rate 20mbit delay 1
113		tc -n $ns2 qdisc add dev ns2eth$i root netem rate 20mbit delay 1
114	done
115}
116
117cleanup_partial()
118{
119	rm -f "$capout"
120
121	local netns
122	for netns in "$ns1" "$ns2"; do
123		ip netns del $netns
124		rm -f /tmp/$netns.{nstat,out}
125	done
126}
127
128check_tools()
129{
130	if ! ip -Version &> /dev/null; then
131		echo "SKIP: Could not run test without ip tool"
132		exit $ksft_skip
133	fi
134
135	if ! iptables -V &> /dev/null; then
136		echo "SKIP: Could not run all tests without iptables tool"
137		exit $ksft_skip
138	fi
139
140	if ! ip6tables -V &> /dev/null; then
141		echo "SKIP: Could not run all tests without ip6tables tool"
142		exit $ksft_skip
143	fi
144}
145
146init() {
147	init=1
148
149	check_tools
150
151	sin=$(mktemp)
152	sout=$(mktemp)
153	cin=$(mktemp)
154	cinsent=$(mktemp)
155	cout=$(mktemp)
156
157	trap cleanup EXIT
158
159	make_file "$cin" "client" 1
160	make_file "$sin" "server" 1
161}
162
163cleanup()
164{
165	rm -f "$cin" "$cout" "$sinfail"
166	rm -f "$sin" "$sout" "$cinsent" "$cinfail"
167	cleanup_partial
168}
169
170skip_test()
171{
172	if [ "${#only_tests_ids[@]}" -eq 0 ] && [ "${#only_tests_names[@]}" -eq 0 ]; then
173		return 1
174	fi
175
176	local i
177	for i in "${only_tests_ids[@]}"; do
178		if [ "${TEST_COUNT}" -eq "${i}" ]; then
179			return 1
180		fi
181	done
182	for i in "${only_tests_names[@]}"; do
183		if [ "${TEST_NAME}" = "${i}" ]; then
184			return 1
185		fi
186	done
187
188	return 0
189}
190
191# $1: test name
192reset()
193{
194	TEST_NAME="${1}"
195
196	TEST_COUNT=$((TEST_COUNT+1))
197
198	if skip_test; then
199		return 1
200	fi
201
202	if [ "${init}" != "1" ]; then
203		init
204	else
205		cleanup_partial
206	fi
207
208	init_partial
209
210	return 0
211}
212
213# $1: test name
214reset_with_cookies()
215{
216	reset "${1}" || return 1
217
218	local netns
219	for netns in "$ns1" "$ns2"; do
220		ip netns exec $netns sysctl -q net.ipv4.tcp_syncookies=2
221	done
222}
223
224# $1: test name
225reset_with_add_addr_timeout()
226{
227	local ip="${2:-4}"
228	local tables
229
230	reset "${1}" || return 1
231
232	tables="iptables"
233	if [ $ip -eq 6 ]; then
234		tables="ip6tables"
235	fi
236
237	ip netns exec $ns1 sysctl -q net.mptcp.add_addr_timeout=1
238	ip netns exec $ns2 $tables -A OUTPUT -p tcp \
239		-m tcp --tcp-option 30 \
240		-m bpf --bytecode \
241		"$CBPF_MPTCP_SUBOPTION_ADD_ADDR" \
242		-j DROP
243}
244
245# $1: test name
246reset_with_checksum()
247{
248	local ns1_enable=$1
249	local ns2_enable=$2
250
251	reset "checksum test ${1} ${2}" || return 1
252
253	ip netns exec $ns1 sysctl -q net.mptcp.checksum_enabled=$ns1_enable
254	ip netns exec $ns2 sysctl -q net.mptcp.checksum_enabled=$ns2_enable
255
256	validate_checksum=1
257}
258
259reset_with_allow_join_id0()
260{
261	local ns1_enable=$2
262	local ns2_enable=$3
263
264	reset "${1}" || return 1
265
266	ip netns exec $ns1 sysctl -q net.mptcp.allow_join_initial_addr_port=$ns1_enable
267	ip netns exec $ns2 sysctl -q net.mptcp.allow_join_initial_addr_port=$ns2_enable
268}
269
270# Modify TCP payload without corrupting the TCP packet
271#
272# This rule inverts a 8-bit word at byte offset 148 for the 2nd TCP ACK packets
273# carrying enough data.
274# Once it is done, the TCP Checksum field is updated so the packet is still
275# considered as valid at the TCP level.
276# Because the MPTCP checksum, covering the TCP options and data, has not been
277# updated, the modification will be detected and an MP_FAIL will be emitted:
278# what we want to validate here without corrupting "random" MPTCP options.
279#
280# To avoid having tc producing this pr_info() message for each TCP ACK packets
281# not carrying enough data:
282#
283#     tc action pedit offset 162 out of bounds
284#
285# Netfilter is used to mark packets with enough data.
286reset_with_fail()
287{
288	reset "${1}" || return 1
289
290	ip netns exec $ns1 sysctl -q net.mptcp.checksum_enabled=1
291	ip netns exec $ns2 sysctl -q net.mptcp.checksum_enabled=1
292
293	check_invert=1
294	validate_checksum=1
295	local i="$2"
296	local ip="${3:-4}"
297	local tables
298
299	tables="iptables"
300	if [ $ip -eq 6 ]; then
301		tables="ip6tables"
302	fi
303
304	ip netns exec $ns2 $tables \
305		-t mangle \
306		-A OUTPUT \
307		-o ns2eth$i \
308		-p tcp \
309		-m length --length 150:9999 \
310		-m statistic --mode nth --packet 1 --every 99999 \
311		-j MARK --set-mark 42 || exit 1
312
313	tc -n $ns2 qdisc add dev ns2eth$i clsact || exit 1
314	tc -n $ns2 filter add dev ns2eth$i egress \
315		protocol ip prio 1000 \
316		handle 42 fw \
317		action pedit munge offset 148 u8 invert \
318		pipe csum tcp \
319		index 100 || exit 1
320}
321
322fail_test()
323{
324	ret=1
325	failed_tests[${TEST_COUNT}]="${TEST_NAME}"
326}
327
328get_failed_tests_ids()
329{
330	# sorted
331	local i
332	for i in "${!failed_tests[@]}"; do
333		echo "${i}"
334	done | sort -n
335}
336
337print_file_err()
338{
339	ls -l "$1" 1>&2
340	echo "Trailing bytes are: "
341	tail -c 27 "$1"
342}
343
344check_transfer()
345{
346	local in=$1
347	local out=$2
348	local what=$3
349	local bytes=$4
350	local i a b
351
352	local line
353	if [ -n "$bytes" ]; then
354		# when truncating we must check the size explicitly
355		local out_size=$(wc -c $out | awk '{print $1}')
356		if [ $out_size -ne $bytes ]; then
357			echo "[ FAIL ] $what output file has wrong size ($out_size, $bytes)"
358			fail_test
359			return 1
360		fi
361		bytes="--bytes=${bytes}"
362	fi
363	cmp -l "$in" "$out" ${bytes} | while read -r i a b; do
364		local sum=$((0${a} + 0${b}))
365		if [ $check_invert -eq 0 ] || [ $sum -ne $((0xff)) ]; then
366			echo "[ FAIL ] $what does not match (in, out):"
367			print_file_err "$in"
368			print_file_err "$out"
369			fail_test
370
371			return 1
372		else
373			echo "$what has inverted byte at ${i}"
374		fi
375	done
376
377	return 0
378}
379
380do_ping()
381{
382	local listener_ns="$1"
383	local connector_ns="$2"
384	local connect_addr="$3"
385
386	if ! ip netns exec ${connector_ns} ping -q -c 1 $connect_addr >/dev/null; then
387		echo "$listener_ns -> $connect_addr connectivity [ FAIL ]" 1>&2
388		fail_test
389	fi
390}
391
392link_failure()
393{
394	local ns="$1"
395
396	if [ -z "$FAILING_LINKS" ]; then
397		l=$((RANDOM%4))
398		FAILING_LINKS=$((l+1))
399	fi
400
401	local l
402	for l in $FAILING_LINKS; do
403		local veth="ns1eth$l"
404		ip -net "$ns" link set "$veth" down
405	done
406}
407
408# $1: IP address
409is_v6()
410{
411	[ -z "${1##*:*}" ]
412}
413
414# $1: ns, $2: port
415wait_local_port_listen()
416{
417	local listener_ns="${1}"
418	local port="${2}"
419
420	local port_hex
421	port_hex="$(printf "%04X" "${port}")"
422
423	local i
424	for i in $(seq 10); do
425		ip netns exec "${listener_ns}" cat /proc/net/tcp* | \
426			awk "BEGIN {rc=1} {if (\$2 ~ /:${port_hex}\$/ && \$4 ~ /0A/) {rc=0; exit}} END {exit rc}" &&
427			break
428		sleep 0.1
429	done
430}
431
432rm_addr_count()
433{
434	local ns=${1}
435
436	ip netns exec ${ns} nstat -as | grep MPTcpExtRmAddr | awk '{print $2}'
437}
438
439# $1: ns, $2: old rm_addr counter in $ns
440wait_rm_addr()
441{
442	local ns="${1}"
443	local old_cnt="${2}"
444	local cnt
445
446	local i
447	for i in $(seq 10); do
448		cnt=$(rm_addr_count ${ns})
449		[ "$cnt" = "${old_cnt}" ] || break
450		sleep 0.1
451	done
452}
453
454wait_mpj()
455{
456	local ns="${1}"
457	local cnt old_cnt
458
459	old_cnt=$(ip netns exec ${ns} nstat -as | grep MPJoinAckRx | awk '{print $2}')
460
461	local i
462	for i in $(seq 10); do
463		cnt=$(ip netns exec ${ns} nstat -as | grep MPJoinAckRx | awk '{print $2}')
464		[ "$cnt" = "${old_cnt}" ] || break
465		sleep 0.1
466	done
467}
468
469kill_wait()
470{
471	kill $1 > /dev/null 2>&1
472	wait $1 2>/dev/null
473}
474
475pm_nl_set_limits()
476{
477	local ns=$1
478	local addrs=$2
479	local subflows=$3
480
481	if [ $ip_mptcp -eq 1 ]; then
482		ip -n $ns mptcp limits set add_addr_accepted $addrs subflows $subflows
483	else
484		ip netns exec $ns ./pm_nl_ctl limits $addrs $subflows
485	fi
486}
487
488pm_nl_add_endpoint()
489{
490	local ns=$1
491	local addr=$2
492	local flags _flags
493	local port _port
494	local dev _dev
495	local id _id
496	local nr=2
497
498	local p
499	for p in "${@}"
500	do
501		if [ $p = "flags" ]; then
502			eval _flags=\$"$nr"
503			[ -n "$_flags" ]; flags="flags $_flags"
504		fi
505		if [ $p = "dev" ]; then
506			eval _dev=\$"$nr"
507			[ -n "$_dev" ]; dev="dev $_dev"
508		fi
509		if [ $p = "id" ]; then
510			eval _id=\$"$nr"
511			[ -n "$_id" ]; id="id $_id"
512		fi
513		if [ $p = "port" ]; then
514			eval _port=\$"$nr"
515			[ -n "$_port" ]; port="port $_port"
516		fi
517
518		nr=$((nr + 1))
519	done
520
521	if [ $ip_mptcp -eq 1 ]; then
522		ip -n $ns mptcp endpoint add $addr ${_flags//","/" "} $dev $id $port
523	else
524		ip netns exec $ns ./pm_nl_ctl add $addr $flags $dev $id $port
525	fi
526}
527
528pm_nl_del_endpoint()
529{
530	local ns=$1
531	local id=$2
532	local addr=$3
533
534	if [ $ip_mptcp -eq 1 ]; then
535		ip -n $ns mptcp endpoint delete id $id $addr
536	else
537		ip netns exec $ns ./pm_nl_ctl del $id $addr
538	fi
539}
540
541pm_nl_flush_endpoint()
542{
543	local ns=$1
544
545	if [ $ip_mptcp -eq 1 ]; then
546		ip -n $ns mptcp endpoint flush
547	else
548		ip netns exec $ns ./pm_nl_ctl flush
549	fi
550}
551
552pm_nl_show_endpoints()
553{
554	local ns=$1
555
556	if [ $ip_mptcp -eq 1 ]; then
557		ip -n $ns mptcp endpoint show
558	else
559		ip netns exec $ns ./pm_nl_ctl dump
560	fi
561}
562
563pm_nl_change_endpoint()
564{
565	local ns=$1
566	local id=$2
567	local flags=$3
568
569	if [ $ip_mptcp -eq 1 ]; then
570		ip -n $ns mptcp endpoint change id $id ${flags//","/" "}
571	else
572		ip netns exec $ns ./pm_nl_ctl set id $id flags $flags
573	fi
574}
575
576pm_nl_check_endpoint()
577{
578	local line expected_line
579	local need_title=$1
580	local msg="$2"
581	local ns=$3
582	local addr=$4
583	local _flags=""
584	local flags
585	local _port
586	local port
587	local dev
588	local _id
589	local id
590
591	if [ "${need_title}" = 1 ]; then
592		printf "%03u %-36s %s" "${TEST_COUNT}" "${TEST_NAME}" "${msg}"
593	else
594		printf "%-${nr_blank}s %s" " " "${msg}"
595	fi
596
597	shift 4
598	while [ -n "$1" ]; do
599		if [ $1 = "flags" ]; then
600			_flags=$2
601			[ -n "$_flags" ]; flags="flags $_flags"
602			shift
603		elif [ $1 = "dev" ]; then
604			[ -n "$2" ]; dev="dev $1"
605			shift
606		elif [ $1 = "id" ]; then
607			_id=$2
608			[ -n "$_id" ]; id="id $_id"
609			shift
610		elif [ $1 = "port" ]; then
611			_port=$2
612			[ -n "$_port" ]; port=" port $_port"
613			shift
614		fi
615
616		shift
617	done
618
619	if [ -z "$id" ]; then
620		echo "[skip] bad test - missing endpoint id"
621		return
622	fi
623
624	if [ $ip_mptcp -eq 1 ]; then
625		line=$(ip -n $ns mptcp endpoint show $id)
626		# the dump order is: address id flags port dev
627		expected_line="$addr"
628		[ -n "$addr" ] && expected_line="$expected_line $addr"
629		expected_line="$expected_line $id"
630		[ -n "$_flags" ] && expected_line="$expected_line ${_flags//","/" "}"
631		[ -n "$dev" ] && expected_line="$expected_line $dev"
632		[ -n "$port" ] && expected_line="$expected_line $port"
633	else
634		line=$(ip netns exec $ns ./pm_nl_ctl get $_id)
635		# the dump order is: id flags dev address port
636		expected_line="$id"
637		[ -n "$flags" ] && expected_line="$expected_line $flags"
638		[ -n "$dev" ] && expected_line="$expected_line $dev"
639		[ -n "$addr" ] && expected_line="$expected_line $addr"
640		[ -n "$_port" ] && expected_line="$expected_line $_port"
641	fi
642	if [ "$line" = "$expected_line" ]; then
643		echo "[ ok ]"
644	else
645		echo "[fail] expected '$expected_line' found '$line'"
646		fail_test
647	fi
648}
649
650filter_tcp_from()
651{
652	local ns="${1}"
653	local src="${2}"
654	local target="${3}"
655
656	ip netns exec "${ns}" iptables -A INPUT -s "${src}" -p tcp -j "${target}"
657}
658
659do_transfer()
660{
661	local listener_ns="$1"
662	local connector_ns="$2"
663	local cl_proto="$3"
664	local srv_proto="$4"
665	local connect_addr="$5"
666	local test_link_fail="$6"
667	local addr_nr_ns1="$7"
668	local addr_nr_ns2="$8"
669	local speed="$9"
670	local sflags="${10}"
671
672	local port=$((10000 + TEST_COUNT - 1))
673	local cappid
674	local userspace_pm=0
675	local evts_ns1
676	local evts_ns1_pid
677	local evts_ns2
678	local evts_ns2_pid
679
680	:> "$cout"
681	:> "$sout"
682	:> "$capout"
683
684	if [ $capture -eq 1 ]; then
685		local capuser
686		if [ -z $SUDO_USER ] ; then
687			capuser=""
688		else
689			capuser="-Z $SUDO_USER"
690		fi
691
692		capfile=$(printf "mp_join-%02u-%s.pcap" "$TEST_COUNT" "${listener_ns}")
693
694		echo "Capturing traffic for test $TEST_COUNT into $capfile"
695		ip netns exec ${listener_ns} tcpdump -i any -s 65535 -B 32768 $capuser -w $capfile > "$capout" 2>&1 &
696		cappid=$!
697
698		sleep 1
699	fi
700
701	NSTAT_HISTORY=/tmp/${listener_ns}.nstat ip netns exec ${listener_ns} \
702		nstat -n
703	NSTAT_HISTORY=/tmp/${connector_ns}.nstat ip netns exec ${connector_ns} \
704		nstat -n
705
706	local extra_args
707	if [ $speed = "fast" ]; then
708		extra_args="-j"
709	elif [ $speed = "slow" ]; then
710		extra_args="-r 50"
711	elif [[ $speed = "speed_"* ]]; then
712		extra_args="-r ${speed:6}"
713	fi
714
715	if [[ "${addr_nr_ns1}" = "userspace_"* ]]; then
716		userspace_pm=1
717		addr_nr_ns1=${addr_nr_ns1:10}
718	fi
719
720	local flags="subflow"
721	local extra_cl_args=""
722	local extra_srv_args=""
723	local trunc_size=""
724	if [[ "${addr_nr_ns2}" = "fastclose_"* ]]; then
725		if [ ${test_link_fail} -le 1 ]; then
726			echo "fastclose tests need test_link_fail argument"
727			fail_test
728			return 1
729		fi
730
731		# disconnect
732		trunc_size=${test_link_fail}
733		local side=${addr_nr_ns2:10}
734
735		if [ ${side} = "client" ]; then
736			extra_cl_args="-f ${test_link_fail}"
737			extra_srv_args="-f -1"
738		elif [ ${side} = "server" ]; then
739			extra_srv_args="-f ${test_link_fail}"
740			extra_cl_args="-f -1"
741		else
742			echo "wrong/unknown fastclose spec ${side}"
743			fail_test
744			return 1
745		fi
746		addr_nr_ns2=0
747	elif [[ "${addr_nr_ns2}" = "userspace_"* ]]; then
748		userspace_pm=1
749		addr_nr_ns2=${addr_nr_ns2:10}
750	elif [[ "${addr_nr_ns2}" = "fullmesh_"* ]]; then
751		flags="${flags},fullmesh"
752		addr_nr_ns2=${addr_nr_ns2:9}
753	fi
754
755	if [ $userspace_pm -eq 1 ]; then
756		evts_ns1=$(mktemp)
757		evts_ns2=$(mktemp)
758		:> "$evts_ns1"
759		:> "$evts_ns2"
760		ip netns exec ${listener_ns} ./pm_nl_ctl events >> "$evts_ns1" 2>&1 &
761		evts_ns1_pid=$!
762		ip netns exec ${connector_ns} ./pm_nl_ctl events >> "$evts_ns2" 2>&1 &
763		evts_ns2_pid=$!
764	fi
765
766	local local_addr
767	if is_v6 "${connect_addr}"; then
768		local_addr="::"
769	else
770		local_addr="0.0.0.0"
771	fi
772
773	extra_srv_args="$extra_args $extra_srv_args"
774	if [ "$test_link_fail" -gt 1 ];then
775		timeout ${timeout_test} \
776			ip netns exec ${listener_ns} \
777				./mptcp_connect -t ${timeout_poll} -l -p $port -s ${srv_proto} \
778					$extra_srv_args ${local_addr} < "$sinfail" > "$sout" &
779	else
780		timeout ${timeout_test} \
781			ip netns exec ${listener_ns} \
782				./mptcp_connect -t ${timeout_poll} -l -p $port -s ${srv_proto} \
783					$extra_srv_args ${local_addr} < "$sin" > "$sout" &
784	fi
785	local spid=$!
786
787	wait_local_port_listen "${listener_ns}" "${port}"
788
789	extra_cl_args="$extra_args $extra_cl_args"
790	if [ "$test_link_fail" -eq 0 ];then
791		timeout ${timeout_test} \
792			ip netns exec ${connector_ns} \
793				./mptcp_connect -t ${timeout_poll} -p $port -s ${cl_proto} \
794					$extra_cl_args $connect_addr < "$cin" > "$cout" &
795	elif [ "$test_link_fail" -eq 1 ] || [ "$test_link_fail" -eq 2 ];then
796		( cat "$cinfail" ; sleep 2; link_failure $listener_ns ; cat "$cinfail" ) | \
797			tee "$cinsent" | \
798			timeout ${timeout_test} \
799				ip netns exec ${connector_ns} \
800					./mptcp_connect -t ${timeout_poll} -p $port -s ${cl_proto} \
801						$extra_cl_args $connect_addr > "$cout" &
802	else
803		tee "$cinsent" < "$cinfail" | \
804			timeout ${timeout_test} \
805				ip netns exec ${connector_ns} \
806					./mptcp_connect -t ${timeout_poll} -p $port -s ${cl_proto} \
807						$extra_cl_args $connect_addr > "$cout" &
808	fi
809	local cpid=$!
810
811	# let the mptcp subflow be established in background before
812	# do endpoint manipulation
813	if [ $addr_nr_ns1 != "0" ] || [ $addr_nr_ns2 != "0" ]; then
814		sleep 1
815	fi
816
817	if [ $addr_nr_ns1 -gt 0 ]; then
818		local counter=2
819		local add_nr_ns1=${addr_nr_ns1}
820		local id=10
821		local tk
822		while [ $add_nr_ns1 -gt 0 ]; do
823			local addr
824			if is_v6 "${connect_addr}"; then
825				addr="dead:beef:$counter::1"
826			else
827				addr="10.0.$counter.1"
828			fi
829			if [ $userspace_pm -eq 0 ]; then
830				pm_nl_add_endpoint $ns1 $addr flags signal
831			else
832				tk=$(sed -n 's/.*\(token:\)\([[:digit:]]*\).*$/\2/p;q' "$evts_ns1")
833				ip netns exec ${listener_ns} ./pm_nl_ctl ann $addr token $tk id $id
834				sleep 1
835				ip netns exec ${listener_ns} ./pm_nl_ctl rem token $tk id $id
836			fi
837
838			counter=$((counter + 1))
839			add_nr_ns1=$((add_nr_ns1 - 1))
840			id=$((id + 1))
841		done
842	elif [ $addr_nr_ns1 -lt 0 ]; then
843		local rm_nr_ns1=$((-addr_nr_ns1))
844		if [ $rm_nr_ns1 -lt 8 ]; then
845			local counter=0
846			local line
847			pm_nl_show_endpoints ${listener_ns} | while read -r line; do
848				# shellcheck disable=SC2206 # we do want to split per word
849				local arr=($line)
850				local nr=0
851
852				local i
853				for i in "${arr[@]}"; do
854					if [ $i = "id" ]; then
855						if [ $counter -eq $rm_nr_ns1 ]; then
856							break
857						fi
858						id=${arr[$nr+1]}
859						rm_addr=$(rm_addr_count ${connector_ns})
860						pm_nl_del_endpoint ${listener_ns} $id
861						wait_rm_addr ${connector_ns} ${rm_addr}
862						counter=$((counter + 1))
863					fi
864					nr=$((nr + 1))
865				done
866			done
867		elif [ $rm_nr_ns1 -eq 8 ]; then
868			pm_nl_flush_endpoint ${listener_ns}
869		elif [ $rm_nr_ns1 -eq 9 ]; then
870			pm_nl_del_endpoint ${listener_ns} 0 ${connect_addr}
871		fi
872	fi
873
874	# if newly added endpoints must be deleted, give the background msk
875	# some time to created them
876	[ $addr_nr_ns1 -gt 0 ] && [ $addr_nr_ns2 -lt 0 ] && sleep 1
877
878	if [ $addr_nr_ns2 -gt 0 ]; then
879		local add_nr_ns2=${addr_nr_ns2}
880		local counter=3
881		local id=20
882		local tk da dp sp
883		while [ $add_nr_ns2 -gt 0 ]; do
884			local addr
885			if is_v6 "${connect_addr}"; then
886				addr="dead:beef:$counter::2"
887			else
888				addr="10.0.$counter.2"
889			fi
890			if [ $userspace_pm -eq 0 ]; then
891				pm_nl_add_endpoint $ns2 $addr flags $flags
892			else
893				tk=$(sed -n 's/.*\(token:\)\([[:digit:]]*\).*$/\2/p;q' "$evts_ns2")
894				da=$(sed -n 's/.*\(daddr4:\)\([0-9.]*\).*$/\2/p;q' "$evts_ns2")
895				dp=$(sed -n 's/.*\(dport:\)\([[:digit:]]*\).*$/\2/p;q' "$evts_ns2")
896				ip netns exec ${connector_ns} ./pm_nl_ctl csf lip $addr lid $id \
897									rip $da rport $dp token $tk
898				sleep 1
899				sp=$(grep "type:10" "$evts_ns2" |
900				     sed -n 's/.*\(sport:\)\([[:digit:]]*\).*$/\2/p;q')
901				ip netns exec ${connector_ns} ./pm_nl_ctl dsf lip $addr lport $sp \
902									rip $da rport $dp token $tk
903			fi
904			counter=$((counter + 1))
905			add_nr_ns2=$((add_nr_ns2 - 1))
906			id=$((id + 1))
907		done
908	elif [ $addr_nr_ns2 -lt 0 ]; then
909		local rm_nr_ns2=$((-addr_nr_ns2))
910		if [ $rm_nr_ns2 -lt 8 ]; then
911			local counter=0
912			local line
913			pm_nl_show_endpoints ${connector_ns} | while read -r line; do
914				# shellcheck disable=SC2206 # we do want to split per word
915				local arr=($line)
916				local nr=0
917
918				local i
919				for i in "${arr[@]}"; do
920					if [ $i = "id" ]; then
921						if [ $counter -eq $rm_nr_ns2 ]; then
922							break
923						fi
924						local id rm_addr
925						# rm_addr are serialized, allow the previous one to
926						# complete
927						id=${arr[$nr+1]}
928						rm_addr=$(rm_addr_count ${listener_ns})
929						pm_nl_del_endpoint ${connector_ns} $id
930						wait_rm_addr ${listener_ns} ${rm_addr}
931						counter=$((counter + 1))
932					fi
933					nr=$((nr + 1))
934				done
935			done
936		elif [ $rm_nr_ns2 -eq 8 ]; then
937			pm_nl_flush_endpoint ${connector_ns}
938		elif [ $rm_nr_ns2 -eq 9 ]; then
939			local addr
940			if is_v6 "${connect_addr}"; then
941				addr="dead:beef:1::2"
942			else
943				addr="10.0.1.2"
944			fi
945			pm_nl_del_endpoint ${connector_ns} 0 $addr
946		fi
947	fi
948
949	if [ -n "${sflags}" ]; then
950		sleep 1
951
952		local netns
953		for netns in "$ns1" "$ns2"; do
954			local line
955			pm_nl_show_endpoints $netns | while read -r line; do
956				# shellcheck disable=SC2206 # we do want to split per word
957				local arr=($line)
958				local nr=0
959				local id
960
961				local i
962				for i in "${arr[@]}"; do
963					if [ $i = "id" ]; then
964						id=${arr[$nr+1]}
965					fi
966					nr=$((nr + 1))
967				done
968				pm_nl_change_endpoint $netns $id $sflags
969			done
970		done
971	fi
972
973	wait $cpid
974	local retc=$?
975	wait $spid
976	local rets=$?
977
978	if [ $capture -eq 1 ]; then
979	    sleep 1
980	    kill $cappid
981	fi
982
983	if [ $userspace_pm -eq 1 ]; then
984		kill_wait $evts_ns1_pid
985		kill_wait $evts_ns2_pid
986		rm -rf $evts_ns1 $evts_ns2
987	fi
988
989	NSTAT_HISTORY=/tmp/${listener_ns}.nstat ip netns exec ${listener_ns} \
990		nstat | grep Tcp > /tmp/${listener_ns}.out
991	NSTAT_HISTORY=/tmp/${connector_ns}.nstat ip netns exec ${connector_ns} \
992		nstat | grep Tcp > /tmp/${connector_ns}.out
993
994	if [ ${rets} -ne 0 ] || [ ${retc} -ne 0 ]; then
995		echo " client exit code $retc, server $rets" 1>&2
996		echo -e "\nnetns ${listener_ns} socket stat for ${port}:" 1>&2
997		ip netns exec ${listener_ns} ss -Menita 1>&2 -o "sport = :$port"
998		cat /tmp/${listener_ns}.out
999		echo -e "\nnetns ${connector_ns} socket stat for ${port}:" 1>&2
1000		ip netns exec ${connector_ns} ss -Menita 1>&2 -o "dport = :$port"
1001		cat /tmp/${connector_ns}.out
1002
1003		cat "$capout"
1004		fail_test
1005		return 1
1006	fi
1007
1008	if [ "$test_link_fail" -gt 1 ];then
1009		check_transfer $sinfail $cout "file received by client" $trunc_size
1010	else
1011		check_transfer $sin $cout "file received by client" $trunc_size
1012	fi
1013	retc=$?
1014	if [ "$test_link_fail" -eq 0 ];then
1015		check_transfer $cin $sout "file received by server" $trunc_size
1016	else
1017		check_transfer $cinsent $sout "file received by server" $trunc_size
1018	fi
1019	rets=$?
1020
1021	if [ $retc -eq 0 ] && [ $rets -eq 0 ];then
1022		cat "$capout"
1023		return 0
1024	fi
1025
1026	cat "$capout"
1027	return 1
1028}
1029
1030make_file()
1031{
1032	local name=$1
1033	local who=$2
1034	local size=$3
1035
1036	dd if=/dev/urandom of="$name" bs=1024 count=$size 2> /dev/null
1037	echo -e "\nMPTCP_TEST_FILE_END_MARKER" >> "$name"
1038
1039	echo "Created $name (size $size KB) containing data sent by $who"
1040}
1041
1042run_tests()
1043{
1044	local listener_ns="$1"
1045	local connector_ns="$2"
1046	local connect_addr="$3"
1047	local test_linkfail="${4:-0}"
1048	local addr_nr_ns1="${5:-0}"
1049	local addr_nr_ns2="${6:-0}"
1050	local speed="${7:-fast}"
1051	local sflags="${8:-""}"
1052
1053	local size
1054
1055	# The values above 2 are reused to make test files
1056	# with the given sizes (KB)
1057	if [ "$test_linkfail" -gt 2 ]; then
1058		size=$test_linkfail
1059
1060		if [ -z "$cinfail" ]; then
1061			cinfail=$(mktemp)
1062		fi
1063		make_file "$cinfail" "client" $size
1064	# create the input file for the failure test when
1065	# the first failure test run
1066	elif [ "$test_linkfail" -ne 0 ] && [ -z "$cinfail" ]; then
1067		# the client file must be considerably larger
1068		# of the maximum expected cwin value, or the
1069		# link utilization will be not predicable
1070		size=$((RANDOM%2))
1071		size=$((size+1))
1072		size=$((size*8192))
1073		size=$((size + ( RANDOM % 8192) ))
1074
1075		cinfail=$(mktemp)
1076		make_file "$cinfail" "client" $size
1077	fi
1078
1079	if [ "$test_linkfail" -gt 2 ]; then
1080		size=$test_linkfail
1081
1082		if [ -z "$sinfail" ]; then
1083			sinfail=$(mktemp)
1084		fi
1085		make_file "$sinfail" "server" $size
1086	elif [ "$test_linkfail" -eq 2 ] && [ -z "$sinfail" ]; then
1087		size=$((RANDOM%16))
1088		size=$((size+1))
1089		size=$((size*2048))
1090
1091		sinfail=$(mktemp)
1092		make_file "$sinfail" "server" $size
1093	fi
1094
1095	do_transfer ${listener_ns} ${connector_ns} MPTCP MPTCP ${connect_addr} \
1096		${test_linkfail} ${addr_nr_ns1} ${addr_nr_ns2} ${speed} ${sflags}
1097}
1098
1099dump_stats()
1100{
1101	echo Server ns stats
1102	ip netns exec $ns1 nstat -as | grep Tcp
1103	echo Client ns stats
1104	ip netns exec $ns2 nstat -as | grep Tcp
1105}
1106
1107chk_csum_nr()
1108{
1109	local csum_ns1=${1:-0}
1110	local csum_ns2=${2:-0}
1111	local count
1112	local dump_stats
1113	local extra_msg=""
1114	local allow_multi_errors_ns1=0
1115	local allow_multi_errors_ns2=0
1116
1117	if [[ "${csum_ns1}" = "+"* ]]; then
1118		allow_multi_errors_ns1=1
1119		csum_ns1=${csum_ns1:1}
1120	fi
1121	if [[ "${csum_ns2}" = "+"* ]]; then
1122		allow_multi_errors_ns2=1
1123		csum_ns2=${csum_ns2:1}
1124	fi
1125
1126	printf "%-${nr_blank}s %s" " " "sum"
1127	count=$(ip netns exec $ns1 nstat -as | grep MPTcpExtDataCsumErr | awk '{print $2}')
1128	[ -z "$count" ] && count=0
1129	if [ "$count" != "$csum_ns1" ]; then
1130		extra_msg="$extra_msg ns1=$count"
1131	fi
1132	if { [ "$count" != $csum_ns1 ] && [ $allow_multi_errors_ns1 -eq 0 ]; } ||
1133	   { [ "$count" -lt $csum_ns1 ] && [ $allow_multi_errors_ns1 -eq 1 ]; }; then
1134		echo "[fail] got $count data checksum error[s] expected $csum_ns1"
1135		fail_test
1136		dump_stats=1
1137	else
1138		echo -n "[ ok ]"
1139	fi
1140	echo -n " - csum  "
1141	count=$(ip netns exec $ns2 nstat -as | grep MPTcpExtDataCsumErr | awk '{print $2}')
1142	[ -z "$count" ] && count=0
1143	if [ "$count" != "$csum_ns2" ]; then
1144		extra_msg="$extra_msg ns2=$count"
1145	fi
1146	if { [ "$count" != $csum_ns2 ] && [ $allow_multi_errors_ns2 -eq 0 ]; } ||
1147	   { [ "$count" -lt $csum_ns2 ] && [ $allow_multi_errors_ns2 -eq 1 ]; }; then
1148		echo "[fail] got $count data checksum error[s] expected $csum_ns2"
1149		fail_test
1150		dump_stats=1
1151	else
1152		echo -n "[ ok ]"
1153	fi
1154	[ "${dump_stats}" = 1 ] && dump_stats
1155
1156	echo "$extra_msg"
1157}
1158
1159chk_fail_nr()
1160{
1161	local fail_tx=$1
1162	local fail_rx=$2
1163	local ns_invert=${3:-""}
1164	local count
1165	local dump_stats
1166	local ns_tx=$ns1
1167	local ns_rx=$ns2
1168	local extra_msg=""
1169	local allow_tx_lost=0
1170	local allow_rx_lost=0
1171
1172	if [[ $ns_invert = "invert" ]]; then
1173		ns_tx=$ns2
1174		ns_rx=$ns1
1175		extra_msg=" invert"
1176	fi
1177
1178	if [[ "${fail_tx}" = "-"* ]]; then
1179		allow_tx_lost=1
1180		fail_tx=${fail_tx:1}
1181	fi
1182	if [[ "${fail_rx}" = "-"* ]]; then
1183		allow_rx_lost=1
1184		fail_rx=${fail_rx:1}
1185	fi
1186
1187	printf "%-${nr_blank}s %s" " " "ftx"
1188	count=$(ip netns exec $ns_tx nstat -as | grep MPTcpExtMPFailTx | awk '{print $2}')
1189	[ -z "$count" ] && count=0
1190	if [ "$count" != "$fail_tx" ]; then
1191		extra_msg="$extra_msg,tx=$count"
1192	fi
1193	if { [ "$count" != "$fail_tx" ] && [ $allow_tx_lost -eq 0 ]; } ||
1194	   { [ "$count" -gt "$fail_tx" ] && [ $allow_tx_lost -eq 1 ]; }; then
1195		echo "[fail] got $count MP_FAIL[s] TX expected $fail_tx"
1196		fail_test
1197		dump_stats=1
1198	else
1199		echo -n "[ ok ]"
1200	fi
1201
1202	echo -n " - failrx"
1203	count=$(ip netns exec $ns_rx nstat -as | grep MPTcpExtMPFailRx | awk '{print $2}')
1204	[ -z "$count" ] && count=0
1205	if [ "$count" != "$fail_rx" ]; then
1206		extra_msg="$extra_msg,rx=$count"
1207	fi
1208	if { [ "$count" != "$fail_rx" ] && [ $allow_rx_lost -eq 0 ]; } ||
1209	   { [ "$count" -gt "$fail_rx" ] && [ $allow_rx_lost -eq 1 ]; }; then
1210		echo "[fail] got $count MP_FAIL[s] RX expected $fail_rx"
1211		fail_test
1212		dump_stats=1
1213	else
1214		echo -n "[ ok ]"
1215	fi
1216
1217	[ "${dump_stats}" = 1 ] && dump_stats
1218
1219	echo "$extra_msg"
1220}
1221
1222chk_fclose_nr()
1223{
1224	local fclose_tx=$1
1225	local fclose_rx=$2
1226	local ns_invert=$3
1227	local count
1228	local dump_stats
1229	local ns_tx=$ns2
1230	local ns_rx=$ns1
1231	local extra_msg="   "
1232
1233	if [[ $ns_invert = "invert" ]]; then
1234		ns_tx=$ns1
1235		ns_rx=$ns2
1236		extra_msg=${extra_msg}"invert"
1237	fi
1238
1239	printf "%-${nr_blank}s %s" " " "ctx"
1240	count=$(ip netns exec $ns_tx nstat -as | grep MPTcpExtMPFastcloseTx | awk '{print $2}')
1241	[ -z "$count" ] && count=0
1242	[ "$count" != "$fclose_tx" ] && extra_msg="$extra_msg,tx=$count"
1243	if [ "$count" != "$fclose_tx" ]; then
1244		echo "[fail] got $count MP_FASTCLOSE[s] TX expected $fclose_tx"
1245		fail_test
1246		dump_stats=1
1247	else
1248		echo -n "[ ok ]"
1249	fi
1250
1251	echo -n " - fclzrx"
1252	count=$(ip netns exec $ns_rx nstat -as | grep MPTcpExtMPFastcloseRx | awk '{print $2}')
1253	[ -z "$count" ] && count=0
1254	[ "$count" != "$fclose_rx" ] && extra_msg="$extra_msg,rx=$count"
1255	if [ "$count" != "$fclose_rx" ]; then
1256		echo "[fail] got $count MP_FASTCLOSE[s] RX expected $fclose_rx"
1257		fail_test
1258		dump_stats=1
1259	else
1260		echo -n "[ ok ]"
1261	fi
1262
1263	[ "${dump_stats}" = 1 ] && dump_stats
1264
1265	echo "$extra_msg"
1266}
1267
1268chk_rst_nr()
1269{
1270	local rst_tx=$1
1271	local rst_rx=$2
1272	local ns_invert=${3:-""}
1273	local count
1274	local dump_stats
1275	local ns_tx=$ns1
1276	local ns_rx=$ns2
1277	local extra_msg=""
1278
1279	if [[ $ns_invert = "invert" ]]; then
1280		ns_tx=$ns2
1281		ns_rx=$ns1
1282		extra_msg="   invert"
1283	fi
1284
1285	printf "%-${nr_blank}s %s" " " "rtx"
1286	count=$(ip netns exec $ns_tx nstat -as | grep MPTcpExtMPRstTx | awk '{print $2}')
1287	[ -z "$count" ] && count=0
1288	if [ $count -lt $rst_tx ]; then
1289		echo "[fail] got $count MP_RST[s] TX expected $rst_tx"
1290		fail_test
1291		dump_stats=1
1292	else
1293		echo -n "[ ok ]"
1294	fi
1295
1296	echo -n " - rstrx "
1297	count=$(ip netns exec $ns_rx nstat -as | grep MPTcpExtMPRstRx | awk '{print $2}')
1298	[ -z "$count" ] && count=0
1299	if [ "$count" -lt "$rst_rx" ]; then
1300		echo "[fail] got $count MP_RST[s] RX expected $rst_rx"
1301		fail_test
1302		dump_stats=1
1303	else
1304		echo -n "[ ok ]"
1305	fi
1306
1307	[ "${dump_stats}" = 1 ] && dump_stats
1308
1309	echo "$extra_msg"
1310}
1311
1312chk_infi_nr()
1313{
1314	local infi_tx=$1
1315	local infi_rx=$2
1316	local count
1317	local dump_stats
1318
1319	printf "%-${nr_blank}s %s" " " "itx"
1320	count=$(ip netns exec $ns2 nstat -as | grep InfiniteMapTx | awk '{print $2}')
1321	[ -z "$count" ] && count=0
1322	if [ "$count" != "$infi_tx" ]; then
1323		echo "[fail] got $count infinite map[s] TX expected $infi_tx"
1324		fail_test
1325		dump_stats=1
1326	else
1327		echo -n "[ ok ]"
1328	fi
1329
1330	echo -n " - infirx"
1331	count=$(ip netns exec $ns1 nstat -as | grep InfiniteMapRx | awk '{print $2}')
1332	[ -z "$count" ] && count=0
1333	if [ "$count" != "$infi_rx" ]; then
1334		echo "[fail] got $count infinite map[s] RX expected $infi_rx"
1335		fail_test
1336		dump_stats=1
1337	else
1338		echo "[ ok ]"
1339	fi
1340
1341	[ "${dump_stats}" = 1 ] && dump_stats
1342}
1343
1344chk_join_nr()
1345{
1346	local syn_nr=$1
1347	local syn_ack_nr=$2
1348	local ack_nr=$3
1349	local csum_ns1=${4:-0}
1350	local csum_ns2=${5:-0}
1351	local fail_nr=${6:-0}
1352	local rst_nr=${7:-0}
1353	local infi_nr=${8:-0}
1354	local corrupted_pkts=${9:-0}
1355	local count
1356	local dump_stats
1357	local with_cookie
1358	local title="${TEST_NAME}"
1359
1360	if [ "${corrupted_pkts}" -gt 0 ]; then
1361		title+=": ${corrupted_pkts} corrupted pkts"
1362	fi
1363
1364	printf "%03u %-36s %s" "${TEST_COUNT}" "${title}" "syn"
1365	count=$(ip netns exec $ns1 nstat -as | grep MPTcpExtMPJoinSynRx | awk '{print $2}')
1366	[ -z "$count" ] && count=0
1367	if [ "$count" != "$syn_nr" ]; then
1368		echo "[fail] got $count JOIN[s] syn expected $syn_nr"
1369		fail_test
1370		dump_stats=1
1371	else
1372		echo -n "[ ok ]"
1373	fi
1374
1375	echo -n " - synack"
1376	with_cookie=$(ip netns exec $ns2 sysctl -n net.ipv4.tcp_syncookies)
1377	count=$(ip netns exec $ns2 nstat -as | grep MPTcpExtMPJoinSynAckRx | awk '{print $2}')
1378	[ -z "$count" ] && count=0
1379	if [ "$count" != "$syn_ack_nr" ]; then
1380		# simult connections exceeding the limit with cookie enabled could go up to
1381		# synack validation as the conn limit can be enforced reliably only after
1382		# the subflow creation
1383		if [ "$with_cookie" = 2 ] && [ "$count" -gt "$syn_ack_nr" ] && [ "$count" -le "$syn_nr" ]; then
1384			echo -n "[ ok ]"
1385		else
1386			echo "[fail] got $count JOIN[s] synack expected $syn_ack_nr"
1387			fail_test
1388			dump_stats=1
1389		fi
1390	else
1391		echo -n "[ ok ]"
1392	fi
1393
1394	echo -n " - ack"
1395	count=$(ip netns exec $ns1 nstat -as | grep MPTcpExtMPJoinAckRx | awk '{print $2}')
1396	[ -z "$count" ] && count=0
1397	if [ "$count" != "$ack_nr" ]; then
1398		echo "[fail] got $count JOIN[s] ack expected $ack_nr"
1399		fail_test
1400		dump_stats=1
1401	else
1402		echo "[ ok ]"
1403	fi
1404	[ "${dump_stats}" = 1 ] && dump_stats
1405	if [ $validate_checksum -eq 1 ]; then
1406		chk_csum_nr $csum_ns1 $csum_ns2
1407		chk_fail_nr $fail_nr $fail_nr
1408		chk_rst_nr $rst_nr $rst_nr
1409		chk_infi_nr $infi_nr $infi_nr
1410	fi
1411}
1412
1413# a negative value for 'stale_max' means no upper bound:
1414# for bidirectional transfer, if one peer sleep for a while
1415# - as these tests do - we can have a quite high number of
1416# stale/recover conversions, proportional to
1417# sleep duration/ MPTCP-level RTX interval.
1418chk_stale_nr()
1419{
1420	local ns=$1
1421	local stale_min=$2
1422	local stale_max=$3
1423	local stale_delta=$4
1424	local dump_stats
1425	local stale_nr
1426	local recover_nr
1427
1428	printf "%-${nr_blank}s %-18s" " " "stale"
1429	stale_nr=$(ip netns exec $ns nstat -as | grep MPTcpExtSubflowStale | awk '{print $2}')
1430	[ -z "$stale_nr" ] && stale_nr=0
1431	recover_nr=$(ip netns exec $ns nstat -as | grep MPTcpExtSubflowRecover | awk '{print $2}')
1432	[ -z "$recover_nr" ] && recover_nr=0
1433
1434	if [ $stale_nr -lt $stale_min ] ||
1435	   { [ $stale_max -gt 0 ] && [ $stale_nr -gt $stale_max ]; } ||
1436	   [ $((stale_nr - recover_nr)) -ne $stale_delta ]; then
1437		echo "[fail] got $stale_nr stale[s] $recover_nr recover[s], " \
1438		     " expected stale in range [$stale_min..$stale_max]," \
1439		     " stale-recover delta $stale_delta "
1440		fail_test
1441		dump_stats=1
1442	else
1443		echo "[ ok ]"
1444	fi
1445
1446	if [ "${dump_stats}" = 1 ]; then
1447		echo $ns stats
1448		ip netns exec $ns ip -s link show
1449		ip netns exec $ns nstat -as | grep MPTcp
1450	fi
1451}
1452
1453chk_add_nr()
1454{
1455	local add_nr=$1
1456	local echo_nr=$2
1457	local port_nr=${3:-0}
1458	local syn_nr=${4:-$port_nr}
1459	local syn_ack_nr=${5:-$port_nr}
1460	local ack_nr=${6:-$port_nr}
1461	local mis_syn_nr=${7:-0}
1462	local mis_ack_nr=${8:-0}
1463	local count
1464	local dump_stats
1465	local timeout
1466
1467	timeout=$(ip netns exec $ns1 sysctl -n net.mptcp.add_addr_timeout)
1468
1469	printf "%-${nr_blank}s %s" " " "add"
1470	count=$(ip netns exec $ns2 nstat -as MPTcpExtAddAddr | grep MPTcpExtAddAddr | awk '{print $2}')
1471	[ -z "$count" ] && count=0
1472
1473	# if the test configured a short timeout tolerate greater then expected
1474	# add addrs options, due to retransmissions
1475	if [ "$count" != "$add_nr" ] && { [ "$timeout" -gt 1 ] || [ "$count" -lt "$add_nr" ]; }; then
1476		echo "[fail] got $count ADD_ADDR[s] expected $add_nr"
1477		fail_test
1478		dump_stats=1
1479	else
1480		echo -n "[ ok ]"
1481	fi
1482
1483	echo -n " - echo  "
1484	count=$(ip netns exec $ns1 nstat -as | grep MPTcpExtEchoAdd | awk '{print $2}')
1485	[ -z "$count" ] && count=0
1486	if [ "$count" != "$echo_nr" ]; then
1487		echo "[fail] got $count ADD_ADDR echo[s] expected $echo_nr"
1488		fail_test
1489		dump_stats=1
1490	else
1491		echo -n "[ ok ]"
1492	fi
1493
1494	if [ $port_nr -gt 0 ]; then
1495		echo -n " - pt "
1496		count=$(ip netns exec $ns2 nstat -as | grep MPTcpExtPortAdd | awk '{print $2}')
1497		[ -z "$count" ] && count=0
1498		if [ "$count" != "$port_nr" ]; then
1499			echo "[fail] got $count ADD_ADDR[s] with a port-number expected $port_nr"
1500			fail_test
1501			dump_stats=1
1502		else
1503			echo "[ ok ]"
1504		fi
1505
1506		printf "%-${nr_blank}s %s" " " "syn"
1507		count=$(ip netns exec $ns1 nstat -as | grep MPTcpExtMPJoinPortSynRx |
1508			awk '{print $2}')
1509		[ -z "$count" ] && count=0
1510		if [ "$count" != "$syn_nr" ]; then
1511			echo "[fail] got $count JOIN[s] syn with a different \
1512				port-number expected $syn_nr"
1513			fail_test
1514			dump_stats=1
1515		else
1516			echo -n "[ ok ]"
1517		fi
1518
1519		echo -n " - synack"
1520		count=$(ip netns exec $ns2 nstat -as | grep MPTcpExtMPJoinPortSynAckRx |
1521			awk '{print $2}')
1522		[ -z "$count" ] && count=0
1523		if [ "$count" != "$syn_ack_nr" ]; then
1524			echo "[fail] got $count JOIN[s] synack with a different \
1525				port-number expected $syn_ack_nr"
1526			fail_test
1527			dump_stats=1
1528		else
1529			echo -n "[ ok ]"
1530		fi
1531
1532		echo -n " - ack"
1533		count=$(ip netns exec $ns1 nstat -as | grep MPTcpExtMPJoinPortAckRx |
1534			awk '{print $2}')
1535		[ -z "$count" ] && count=0
1536		if [ "$count" != "$ack_nr" ]; then
1537			echo "[fail] got $count JOIN[s] ack with a different \
1538				port-number expected $ack_nr"
1539			fail_test
1540			dump_stats=1
1541		else
1542			echo "[ ok ]"
1543		fi
1544
1545		printf "%-${nr_blank}s %s" " " "syn"
1546		count=$(ip netns exec $ns1 nstat -as | grep MPTcpExtMismatchPortSynRx |
1547			awk '{print $2}')
1548		[ -z "$count" ] && count=0
1549		if [ "$count" != "$mis_syn_nr" ]; then
1550			echo "[fail] got $count JOIN[s] syn with a mismatched \
1551				port-number expected $mis_syn_nr"
1552			fail_test
1553			dump_stats=1
1554		else
1555			echo -n "[ ok ]"
1556		fi
1557
1558		echo -n " - ack   "
1559		count=$(ip netns exec $ns1 nstat -as | grep MPTcpExtMismatchPortAckRx |
1560			awk '{print $2}')
1561		[ -z "$count" ] && count=0
1562		if [ "$count" != "$mis_ack_nr" ]; then
1563			echo "[fail] got $count JOIN[s] ack with a mismatched \
1564				port-number expected $mis_ack_nr"
1565			fail_test
1566			dump_stats=1
1567		else
1568			echo "[ ok ]"
1569		fi
1570	else
1571		echo ""
1572	fi
1573
1574	[ "${dump_stats}" = 1 ] && dump_stats
1575}
1576
1577chk_rm_nr()
1578{
1579	local rm_addr_nr=$1
1580	local rm_subflow_nr=$2
1581	local invert
1582	local simult
1583	local count
1584	local dump_stats
1585	local addr_ns=$ns1
1586	local subflow_ns=$ns2
1587	local extra_msg=""
1588
1589	shift 2
1590	while [ -n "$1" ]; do
1591		[ "$1" = "invert" ] && invert=true
1592		[ "$1" = "simult" ] && simult=true
1593		shift
1594	done
1595
1596	if [ -z $invert ]; then
1597		addr_ns=$ns1
1598		subflow_ns=$ns2
1599	elif [ $invert = "true" ]; then
1600		addr_ns=$ns2
1601		subflow_ns=$ns1
1602		extra_msg="   invert"
1603	fi
1604
1605	printf "%-${nr_blank}s %s" " " "rm "
1606	count=$(ip netns exec $addr_ns nstat -as | grep MPTcpExtRmAddr | awk '{print $2}')
1607	[ -z "$count" ] && count=0
1608	if [ "$count" != "$rm_addr_nr" ]; then
1609		echo "[fail] got $count RM_ADDR[s] expected $rm_addr_nr"
1610		fail_test
1611		dump_stats=1
1612	else
1613		echo -n "[ ok ]"
1614	fi
1615
1616	echo -n " - rmsf  "
1617	count=$(ip netns exec $subflow_ns nstat -as | grep MPTcpExtRmSubflow | awk '{print $2}')
1618	[ -z "$count" ] && count=0
1619	if [ -n "$simult" ]; then
1620		local cnt suffix
1621
1622		cnt=$(ip netns exec $addr_ns nstat -as | grep MPTcpExtRmSubflow | awk '{print $2}')
1623
1624		# in case of simult flush, the subflow removal count on each side is
1625		# unreliable
1626		[ -z "$cnt" ] && cnt=0
1627		count=$((count + cnt))
1628		[ "$count" != "$rm_subflow_nr" ] && suffix="$count in [$rm_subflow_nr:$((rm_subflow_nr*2))]"
1629		if [ $count -ge "$rm_subflow_nr" ] && \
1630		   [ "$count" -le "$((rm_subflow_nr *2 ))" ]; then
1631			echo "[ ok ] $suffix"
1632		else
1633			echo "[fail] got $count RM_SUBFLOW[s] expected in range [$rm_subflow_nr:$((rm_subflow_nr*2))]"
1634			fail_test
1635			dump_stats=1
1636		fi
1637		return
1638	fi
1639	if [ "$count" != "$rm_subflow_nr" ]; then
1640		echo "[fail] got $count RM_SUBFLOW[s] expected $rm_subflow_nr"
1641		fail_test
1642		dump_stats=1
1643	else
1644		echo -n "[ ok ]"
1645	fi
1646
1647	[ "${dump_stats}" = 1 ] && dump_stats
1648
1649	echo "$extra_msg"
1650}
1651
1652chk_prio_nr()
1653{
1654	local mp_prio_nr_tx=$1
1655	local mp_prio_nr_rx=$2
1656	local count
1657	local dump_stats
1658
1659	printf "%-${nr_blank}s %s" " " "ptx"
1660	count=$(ip netns exec $ns1 nstat -as | grep MPTcpExtMPPrioTx | awk '{print $2}')
1661	[ -z "$count" ] && count=0
1662	if [ "$count" != "$mp_prio_nr_tx" ]; then
1663		echo "[fail] got $count MP_PRIO[s] TX expected $mp_prio_nr_tx"
1664		fail_test
1665		dump_stats=1
1666	else
1667		echo -n "[ ok ]"
1668	fi
1669
1670	echo -n " - prx   "
1671	count=$(ip netns exec $ns1 nstat -as | grep MPTcpExtMPPrioRx | awk '{print $2}')
1672	[ -z "$count" ] && count=0
1673	if [ "$count" != "$mp_prio_nr_rx" ]; then
1674		echo "[fail] got $count MP_PRIO[s] RX expected $mp_prio_nr_rx"
1675		fail_test
1676		dump_stats=1
1677	else
1678		echo "[ ok ]"
1679	fi
1680
1681	[ "${dump_stats}" = 1 ] && dump_stats
1682}
1683
1684chk_subflow_nr()
1685{
1686	local need_title="$1"
1687	local msg="$2"
1688	local subflow_nr=$3
1689	local cnt1
1690	local cnt2
1691
1692	if [ -n "${need_title}" ]; then
1693		printf "%03u %-36s %s" "${TEST_COUNT}" "${TEST_NAME}" "${msg}"
1694	else
1695		printf "%-${nr_blank}s %s" " " "${msg}"
1696	fi
1697
1698	cnt1=$(ss -N $ns1 -tOni | grep -c token)
1699	cnt2=$(ss -N $ns2 -tOni | grep -c token)
1700	if [ "$cnt1" != "$subflow_nr" -o "$cnt2" != "$subflow_nr" ]; then
1701		echo "[fail] got $cnt1:$cnt2 subflows expected $subflow_nr"
1702		fail_test
1703		dump_stats=1
1704	else
1705		echo "[ ok ]"
1706	fi
1707
1708	[ "${dump_stats}" = 1 ] && ( ss -N $ns1 -tOni ; ss -N $ns1 -tOni | grep token; ip -n $ns1 mptcp endpoint )
1709}
1710
1711chk_link_usage()
1712{
1713	local ns=$1
1714	local link=$2
1715	local out=$3
1716	local expected_rate=$4
1717
1718	local tx_link tx_total
1719	tx_link=$(ip netns exec $ns cat /sys/class/net/$link/statistics/tx_bytes)
1720	tx_total=$(stat --format=%s $out)
1721	local tx_rate=$((tx_link * 100 / tx_total))
1722	local tolerance=5
1723
1724	printf "%-${nr_blank}s %-18s" " " "link usage"
1725	if [ $tx_rate -lt $((expected_rate - tolerance)) ] || \
1726	   [ $tx_rate -gt $((expected_rate + tolerance)) ]; then
1727		echo "[fail] got $tx_rate% usage, expected $expected_rate%"
1728		fail_test
1729	else
1730		echo "[ ok ]"
1731	fi
1732}
1733
1734wait_attempt_fail()
1735{
1736	local timeout_ms=$((timeout_poll * 1000))
1737	local time=0
1738	local ns=$1
1739
1740	while [ $time -lt $timeout_ms ]; do
1741		local cnt
1742
1743		cnt=$(ip netns exec $ns nstat -as TcpAttemptFails | grep TcpAttemptFails | awk '{print $2}')
1744
1745		[ "$cnt" = 1 ] && return 1
1746		time=$((time + 100))
1747		sleep 0.1
1748	done
1749	return 1
1750}
1751
1752set_userspace_pm()
1753{
1754	local ns=$1
1755
1756	ip netns exec $ns sysctl -q net.mptcp.pm_type=1
1757}
1758
1759subflows_tests()
1760{
1761	if reset "no JOIN"; then
1762		run_tests $ns1 $ns2 10.0.1.1
1763		chk_join_nr 0 0 0
1764	fi
1765
1766	# subflow limited by client
1767	if reset "single subflow, limited by client"; then
1768		pm_nl_set_limits $ns1 0 0
1769		pm_nl_set_limits $ns2 0 0
1770		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
1771		run_tests $ns1 $ns2 10.0.1.1
1772		chk_join_nr 0 0 0
1773	fi
1774
1775	# subflow limited by server
1776	if reset "single subflow, limited by server"; then
1777		pm_nl_set_limits $ns1 0 0
1778		pm_nl_set_limits $ns2 0 1
1779		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
1780		run_tests $ns1 $ns2 10.0.1.1
1781		chk_join_nr 1 1 0
1782	fi
1783
1784	# subflow
1785	if reset "single subflow"; then
1786		pm_nl_set_limits $ns1 0 1
1787		pm_nl_set_limits $ns2 0 1
1788		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
1789		run_tests $ns1 $ns2 10.0.1.1
1790		chk_join_nr 1 1 1
1791	fi
1792
1793	# multiple subflows
1794	if reset "multiple subflows"; then
1795		pm_nl_set_limits $ns1 0 2
1796		pm_nl_set_limits $ns2 0 2
1797		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
1798		pm_nl_add_endpoint $ns2 10.0.2.2 flags subflow
1799		run_tests $ns1 $ns2 10.0.1.1
1800		chk_join_nr 2 2 2
1801	fi
1802
1803	# multiple subflows limited by server
1804	if reset "multiple subflows, limited by server"; then
1805		pm_nl_set_limits $ns1 0 1
1806		pm_nl_set_limits $ns2 0 2
1807		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
1808		pm_nl_add_endpoint $ns2 10.0.2.2 flags subflow
1809		run_tests $ns1 $ns2 10.0.1.1
1810		chk_join_nr 2 2 1
1811	fi
1812
1813	# single subflow, dev
1814	if reset "single subflow, dev"; then
1815		pm_nl_set_limits $ns1 0 1
1816		pm_nl_set_limits $ns2 0 1
1817		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow dev ns2eth3
1818		run_tests $ns1 $ns2 10.0.1.1
1819		chk_join_nr 1 1 1
1820	fi
1821}
1822
1823subflows_error_tests()
1824{
1825	# If a single subflow is configured, and matches the MPC src
1826	# address, no additional subflow should be created
1827	if reset "no MPC reuse with single endpoint"; then
1828		pm_nl_set_limits $ns1 0 1
1829		pm_nl_set_limits $ns2 0 1
1830		pm_nl_add_endpoint $ns2 10.0.1.2 flags subflow
1831		run_tests $ns1 $ns2 10.0.1.1 0 0 0 slow
1832		chk_join_nr 0 0 0
1833	fi
1834
1835	# multiple subflows, with subflow creation error
1836	if reset "multi subflows, with failing subflow"; then
1837		pm_nl_set_limits $ns1 0 2
1838		pm_nl_set_limits $ns2 0 2
1839		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
1840		pm_nl_add_endpoint $ns2 10.0.2.2 flags subflow
1841		filter_tcp_from $ns1 10.0.3.2 REJECT
1842		run_tests $ns1 $ns2 10.0.1.1 0 0 0 slow
1843		chk_join_nr 1 1 1
1844	fi
1845
1846	# multiple subflows, with subflow timeout on MPJ
1847	if reset "multi subflows, with subflow timeout"; then
1848		pm_nl_set_limits $ns1 0 2
1849		pm_nl_set_limits $ns2 0 2
1850		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
1851		pm_nl_add_endpoint $ns2 10.0.2.2 flags subflow
1852		filter_tcp_from $ns1 10.0.3.2 DROP
1853		run_tests $ns1 $ns2 10.0.1.1 0 0 0 slow
1854		chk_join_nr 1 1 1
1855	fi
1856
1857	# multiple subflows, check that the endpoint corresponding to
1858	# closed subflow (due to reset) is not reused if additional
1859	# subflows are added later
1860	if reset "multi subflows, fair usage on close"; then
1861		pm_nl_set_limits $ns1 0 1
1862		pm_nl_set_limits $ns2 0 1
1863		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
1864		filter_tcp_from $ns1 10.0.3.2 REJECT
1865		run_tests $ns1 $ns2 10.0.1.1 0 0 0 slow &
1866
1867		# mpj subflow will be in TW after the reset
1868		wait_attempt_fail $ns2
1869		pm_nl_add_endpoint $ns2 10.0.2.2 flags subflow
1870		wait
1871
1872		# additional subflow could be created only if the PM select
1873		# the later endpoint, skipping the already used one
1874		chk_join_nr 1 1 1
1875	fi
1876}
1877
1878signal_address_tests()
1879{
1880	# add_address, unused
1881	if reset "unused signal address"; then
1882		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
1883		run_tests $ns1 $ns2 10.0.1.1
1884		chk_join_nr 0 0 0
1885		chk_add_nr 1 1
1886	fi
1887
1888	# accept and use add_addr
1889	if reset "signal address"; then
1890		pm_nl_set_limits $ns1 0 1
1891		pm_nl_set_limits $ns2 1 1
1892		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
1893		run_tests $ns1 $ns2 10.0.1.1
1894		chk_join_nr 1 1 1
1895		chk_add_nr 1 1
1896	fi
1897
1898	# accept and use add_addr with an additional subflow
1899	# note: signal address in server ns and local addresses in client ns must
1900	# belong to different subnets or one of the listed local address could be
1901	# used for 'add_addr' subflow
1902	if reset "subflow and signal"; then
1903		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
1904		pm_nl_set_limits $ns1 0 2
1905		pm_nl_set_limits $ns2 1 2
1906		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
1907		run_tests $ns1 $ns2 10.0.1.1
1908		chk_join_nr 2 2 2
1909		chk_add_nr 1 1
1910	fi
1911
1912	# accept and use add_addr with additional subflows
1913	if reset "multiple subflows and signal"; then
1914		pm_nl_set_limits $ns1 0 3
1915		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
1916		pm_nl_set_limits $ns2 1 3
1917		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
1918		pm_nl_add_endpoint $ns2 10.0.4.2 flags subflow
1919		run_tests $ns1 $ns2 10.0.1.1
1920		chk_join_nr 3 3 3
1921		chk_add_nr 1 1
1922	fi
1923
1924	# signal addresses
1925	if reset "signal addresses"; then
1926		pm_nl_set_limits $ns1 3 3
1927		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
1928		pm_nl_add_endpoint $ns1 10.0.3.1 flags signal
1929		pm_nl_add_endpoint $ns1 10.0.4.1 flags signal
1930		pm_nl_set_limits $ns2 3 3
1931		run_tests $ns1 $ns2 10.0.1.1
1932		chk_join_nr 3 3 3
1933		chk_add_nr 3 3
1934	fi
1935
1936	# signal invalid addresses
1937	if reset "signal invalid addresses"; then
1938		pm_nl_set_limits $ns1 3 3
1939		pm_nl_add_endpoint $ns1 10.0.12.1 flags signal
1940		pm_nl_add_endpoint $ns1 10.0.3.1 flags signal
1941		pm_nl_add_endpoint $ns1 10.0.14.1 flags signal
1942		pm_nl_set_limits $ns2 3 3
1943		run_tests $ns1 $ns2 10.0.1.1
1944		chk_join_nr 1 1 1
1945		chk_add_nr 3 3
1946	fi
1947
1948	# signal addresses race test
1949	if reset "signal addresses race test"; then
1950		pm_nl_set_limits $ns1 4 4
1951		pm_nl_set_limits $ns2 4 4
1952		pm_nl_add_endpoint $ns1 10.0.1.1 flags signal
1953		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
1954		pm_nl_add_endpoint $ns1 10.0.3.1 flags signal
1955		pm_nl_add_endpoint $ns1 10.0.4.1 flags signal
1956		pm_nl_add_endpoint $ns2 10.0.1.2 flags signal
1957		pm_nl_add_endpoint $ns2 10.0.2.2 flags signal
1958		pm_nl_add_endpoint $ns2 10.0.3.2 flags signal
1959		pm_nl_add_endpoint $ns2 10.0.4.2 flags signal
1960
1961		# the peer could possibly miss some addr notification, allow retransmission
1962		ip netns exec $ns1 sysctl -q net.mptcp.add_addr_timeout=1
1963		run_tests $ns1 $ns2 10.0.1.1 0 0 0 slow
1964		chk_join_nr 3 3 3
1965
1966		# the server will not signal the address terminating
1967		# the MPC subflow
1968		chk_add_nr 3 3
1969	fi
1970}
1971
1972link_failure_tests()
1973{
1974	# accept and use add_addr with additional subflows and link loss
1975	if reset "multiple flows, signal, link failure"; then
1976		# without any b/w limit each veth could spool the packets and get
1977		# them acked at xmit time, so that the corresponding subflow will
1978		# have almost always no outstanding pkts, the scheduler will pick
1979		# always the first subflow and we will have hard time testing
1980		# active backup and link switch-over.
1981		# Let's set some arbitrary (low) virtual link limits.
1982		init_shapers
1983		pm_nl_set_limits $ns1 0 3
1984		pm_nl_add_endpoint $ns1 10.0.2.1 dev ns1eth2 flags signal
1985		pm_nl_set_limits $ns2 1 3
1986		pm_nl_add_endpoint $ns2 10.0.3.2 dev ns2eth3 flags subflow
1987		pm_nl_add_endpoint $ns2 10.0.4.2 dev ns2eth4 flags subflow
1988		run_tests $ns1 $ns2 10.0.1.1 1
1989		chk_join_nr 3 3 3
1990		chk_add_nr 1 1
1991		chk_stale_nr $ns2 1 5 1
1992	fi
1993
1994	# accept and use add_addr with additional subflows and link loss
1995	# for bidirectional transfer
1996	if reset "multi flows, signal, bidi, link fail"; then
1997		init_shapers
1998		pm_nl_set_limits $ns1 0 3
1999		pm_nl_add_endpoint $ns1 10.0.2.1 dev ns1eth2 flags signal
2000		pm_nl_set_limits $ns2 1 3
2001		pm_nl_add_endpoint $ns2 10.0.3.2 dev ns2eth3 flags subflow
2002		pm_nl_add_endpoint $ns2 10.0.4.2 dev ns2eth4 flags subflow
2003		run_tests $ns1 $ns2 10.0.1.1 2
2004		chk_join_nr 3 3 3
2005		chk_add_nr 1 1
2006		chk_stale_nr $ns2 1 -1 1
2007	fi
2008
2009	# 2 subflows plus 1 backup subflow with a lossy link, backup
2010	# will never be used
2011	if reset "backup subflow unused, link failure"; then
2012		init_shapers
2013		pm_nl_set_limits $ns1 0 2
2014		pm_nl_add_endpoint $ns1 10.0.2.1 dev ns1eth2 flags signal
2015		pm_nl_set_limits $ns2 1 2
2016		FAILING_LINKS="1"
2017		pm_nl_add_endpoint $ns2 10.0.3.2 dev ns2eth3 flags subflow,backup
2018		run_tests $ns1 $ns2 10.0.1.1 1
2019		chk_join_nr 2 2 2
2020		chk_add_nr 1 1
2021		chk_link_usage $ns2 ns2eth3 $cinsent 0
2022	fi
2023
2024	# 2 lossy links after half transfer, backup will get half of
2025	# the traffic
2026	if reset "backup flow used, multi links fail"; then
2027		init_shapers
2028		pm_nl_set_limits $ns1 0 2
2029		pm_nl_add_endpoint $ns1 10.0.2.1 dev ns1eth2 flags signal
2030		pm_nl_set_limits $ns2 1 2
2031		pm_nl_add_endpoint $ns2 10.0.3.2 dev ns2eth3 flags subflow,backup
2032		FAILING_LINKS="1 2"
2033		run_tests $ns1 $ns2 10.0.1.1 1
2034		chk_join_nr 2 2 2
2035		chk_add_nr 1 1
2036		chk_stale_nr $ns2 2 4 2
2037		chk_link_usage $ns2 ns2eth3 $cinsent 50
2038	fi
2039
2040	# use a backup subflow with the first subflow on a lossy link
2041	# for bidirectional transfer
2042	if reset "backup flow used, bidi, link failure"; then
2043		init_shapers
2044		pm_nl_set_limits $ns1 0 2
2045		pm_nl_add_endpoint $ns1 10.0.2.1 dev ns1eth2 flags signal
2046		pm_nl_set_limits $ns2 1 3
2047		pm_nl_add_endpoint $ns2 10.0.3.2 dev ns2eth3 flags subflow,backup
2048		FAILING_LINKS="1 2"
2049		run_tests $ns1 $ns2 10.0.1.1 2
2050		chk_join_nr 2 2 2
2051		chk_add_nr 1 1
2052		chk_stale_nr $ns2 1 -1 2
2053		chk_link_usage $ns2 ns2eth3 $cinsent 50
2054	fi
2055}
2056
2057add_addr_timeout_tests()
2058{
2059	# add_addr timeout
2060	if reset_with_add_addr_timeout "signal address, ADD_ADDR timeout"; then
2061		pm_nl_set_limits $ns1 0 1
2062		pm_nl_set_limits $ns2 1 1
2063		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
2064		run_tests $ns1 $ns2 10.0.1.1 0 0 0 slow
2065		chk_join_nr 1 1 1
2066		chk_add_nr 4 0
2067	fi
2068
2069	# add_addr timeout IPv6
2070	if reset_with_add_addr_timeout "signal address, ADD_ADDR6 timeout" 6; then
2071		pm_nl_set_limits $ns1 0 1
2072		pm_nl_set_limits $ns2 1 1
2073		pm_nl_add_endpoint $ns1 dead:beef:2::1 flags signal
2074		run_tests $ns1 $ns2 dead:beef:1::1 0 0 0 slow
2075		chk_join_nr 1 1 1
2076		chk_add_nr 4 0
2077	fi
2078
2079	# signal addresses timeout
2080	if reset_with_add_addr_timeout "signal addresses, ADD_ADDR timeout"; then
2081		pm_nl_set_limits $ns1 2 2
2082		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
2083		pm_nl_add_endpoint $ns1 10.0.3.1 flags signal
2084		pm_nl_set_limits $ns2 2 2
2085		run_tests $ns1 $ns2 10.0.1.1 0 0 0 speed_10
2086		chk_join_nr 2 2 2
2087		chk_add_nr 8 0
2088	fi
2089
2090	# signal invalid addresses timeout
2091	if reset_with_add_addr_timeout "invalid address, ADD_ADDR timeout"; then
2092		pm_nl_set_limits $ns1 2 2
2093		pm_nl_add_endpoint $ns1 10.0.12.1 flags signal
2094		pm_nl_add_endpoint $ns1 10.0.3.1 flags signal
2095		pm_nl_set_limits $ns2 2 2
2096		run_tests $ns1 $ns2 10.0.1.1 0 0 0 speed_10
2097		chk_join_nr 1 1 1
2098		chk_add_nr 8 0
2099	fi
2100}
2101
2102remove_tests()
2103{
2104	# single subflow, remove
2105	if reset "remove single subflow"; then
2106		pm_nl_set_limits $ns1 0 1
2107		pm_nl_set_limits $ns2 0 1
2108		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
2109		run_tests $ns1 $ns2 10.0.1.1 0 0 -1 slow
2110		chk_join_nr 1 1 1
2111		chk_rm_nr 1 1
2112	fi
2113
2114	# multiple subflows, remove
2115	if reset "remove multiple subflows"; then
2116		pm_nl_set_limits $ns1 0 2
2117		pm_nl_set_limits $ns2 0 2
2118		pm_nl_add_endpoint $ns2 10.0.2.2 flags subflow
2119		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
2120		run_tests $ns1 $ns2 10.0.1.1 0 0 -2 slow
2121		chk_join_nr 2 2 2
2122		chk_rm_nr 2 2
2123	fi
2124
2125	# single address, remove
2126	if reset "remove single address"; then
2127		pm_nl_set_limits $ns1 0 1
2128		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
2129		pm_nl_set_limits $ns2 1 1
2130		run_tests $ns1 $ns2 10.0.1.1 0 -1 0 slow
2131		chk_join_nr 1 1 1
2132		chk_add_nr 1 1
2133		chk_rm_nr 1 1 invert
2134	fi
2135
2136	# subflow and signal, remove
2137	if reset "remove subflow and signal"; then
2138		pm_nl_set_limits $ns1 0 2
2139		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
2140		pm_nl_set_limits $ns2 1 2
2141		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
2142		run_tests $ns1 $ns2 10.0.1.1 0 -1 -1 slow
2143		chk_join_nr 2 2 2
2144		chk_add_nr 1 1
2145		chk_rm_nr 1 1
2146	fi
2147
2148	# subflows and signal, remove
2149	if reset "remove subflows and signal"; then
2150		pm_nl_set_limits $ns1 0 3
2151		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
2152		pm_nl_set_limits $ns2 1 3
2153		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
2154		pm_nl_add_endpoint $ns2 10.0.4.2 flags subflow
2155		run_tests $ns1 $ns2 10.0.1.1 0 -1 -2 slow
2156		chk_join_nr 3 3 3
2157		chk_add_nr 1 1
2158		chk_rm_nr 2 2
2159	fi
2160
2161	# addresses remove
2162	if reset "remove addresses"; then
2163		pm_nl_set_limits $ns1 3 3
2164		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal id 250
2165		pm_nl_add_endpoint $ns1 10.0.3.1 flags signal
2166		pm_nl_add_endpoint $ns1 10.0.4.1 flags signal
2167		pm_nl_set_limits $ns2 3 3
2168		run_tests $ns1 $ns2 10.0.1.1 0 -3 0 slow
2169		chk_join_nr 3 3 3
2170		chk_add_nr 3 3
2171		chk_rm_nr 3 3 invert
2172	fi
2173
2174	# invalid addresses remove
2175	if reset "remove invalid addresses"; then
2176		pm_nl_set_limits $ns1 3 3
2177		pm_nl_add_endpoint $ns1 10.0.12.1 flags signal
2178		pm_nl_add_endpoint $ns1 10.0.3.1 flags signal
2179		pm_nl_add_endpoint $ns1 10.0.14.1 flags signal
2180		pm_nl_set_limits $ns2 3 3
2181		run_tests $ns1 $ns2 10.0.1.1 0 -3 0 slow
2182		chk_join_nr 1 1 1
2183		chk_add_nr 3 3
2184		chk_rm_nr 3 1 invert
2185	fi
2186
2187	# subflows and signal, flush
2188	if reset "flush subflows and signal"; then
2189		pm_nl_set_limits $ns1 0 3
2190		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
2191		pm_nl_set_limits $ns2 1 3
2192		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
2193		pm_nl_add_endpoint $ns2 10.0.4.2 flags subflow
2194		run_tests $ns1 $ns2 10.0.1.1 0 -8 -8 slow
2195		chk_join_nr 3 3 3
2196		chk_add_nr 1 1
2197		chk_rm_nr 1 3 invert simult
2198	fi
2199
2200	# subflows flush
2201	if reset "flush subflows"; then
2202		pm_nl_set_limits $ns1 3 3
2203		pm_nl_set_limits $ns2 3 3
2204		pm_nl_add_endpoint $ns2 10.0.2.2 flags subflow id 150
2205		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
2206		pm_nl_add_endpoint $ns2 10.0.4.2 flags subflow
2207		run_tests $ns1 $ns2 10.0.1.1 0 -8 -8 slow
2208		chk_join_nr 3 3 3
2209		chk_rm_nr 0 3 simult
2210	fi
2211
2212	# addresses flush
2213	if reset "flush addresses"; then
2214		pm_nl_set_limits $ns1 3 3
2215		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal id 250
2216		pm_nl_add_endpoint $ns1 10.0.3.1 flags signal
2217		pm_nl_add_endpoint $ns1 10.0.4.1 flags signal
2218		pm_nl_set_limits $ns2 3 3
2219		run_tests $ns1 $ns2 10.0.1.1 0 -8 -8 slow
2220		chk_join_nr 3 3 3
2221		chk_add_nr 3 3
2222		chk_rm_nr 3 3 invert simult
2223	fi
2224
2225	# invalid addresses flush
2226	if reset "flush invalid addresses"; then
2227		pm_nl_set_limits $ns1 3 3
2228		pm_nl_add_endpoint $ns1 10.0.12.1 flags signal
2229		pm_nl_add_endpoint $ns1 10.0.3.1 flags signal
2230		pm_nl_add_endpoint $ns1 10.0.14.1 flags signal
2231		pm_nl_set_limits $ns2 3 3
2232		run_tests $ns1 $ns2 10.0.1.1 0 -8 0 slow
2233		chk_join_nr 1 1 1
2234		chk_add_nr 3 3
2235		chk_rm_nr 3 1 invert
2236	fi
2237
2238	# remove id 0 subflow
2239	if reset "remove id 0 subflow"; then
2240		pm_nl_set_limits $ns1 0 1
2241		pm_nl_set_limits $ns2 0 1
2242		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
2243		run_tests $ns1 $ns2 10.0.1.1 0 0 -9 slow
2244		chk_join_nr 1 1 1
2245		chk_rm_nr 1 1
2246	fi
2247
2248	# remove id 0 address
2249	if reset "remove id 0 address"; then
2250		pm_nl_set_limits $ns1 0 1
2251		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
2252		pm_nl_set_limits $ns2 1 1
2253		run_tests $ns1 $ns2 10.0.1.1 0 -9 0 slow
2254		chk_join_nr 1 1 1
2255		chk_add_nr 1 1
2256		chk_rm_nr 1 1 invert
2257	fi
2258}
2259
2260add_tests()
2261{
2262	# add single subflow
2263	if reset "add single subflow"; then
2264		pm_nl_set_limits $ns1 0 1
2265		pm_nl_set_limits $ns2 0 1
2266		run_tests $ns1 $ns2 10.0.1.1 0 0 1 slow
2267		chk_join_nr 1 1 1
2268	fi
2269
2270	# add signal address
2271	if reset "add signal address"; then
2272		pm_nl_set_limits $ns1 0 1
2273		pm_nl_set_limits $ns2 1 1
2274		run_tests $ns1 $ns2 10.0.1.1 0 1 0 slow
2275		chk_join_nr 1 1 1
2276		chk_add_nr 1 1
2277	fi
2278
2279	# add multiple subflows
2280	if reset "add multiple subflows"; then
2281		pm_nl_set_limits $ns1 0 2
2282		pm_nl_set_limits $ns2 0 2
2283		run_tests $ns1 $ns2 10.0.1.1 0 0 2 slow
2284		chk_join_nr 2 2 2
2285	fi
2286
2287	# add multiple subflows IPv6
2288	if reset "add multiple subflows IPv6"; then
2289		pm_nl_set_limits $ns1 0 2
2290		pm_nl_set_limits $ns2 0 2
2291		run_tests $ns1 $ns2 dead:beef:1::1 0 0 2 slow
2292		chk_join_nr 2 2 2
2293	fi
2294
2295	# add multiple addresses IPv6
2296	if reset "add multiple addresses IPv6"; then
2297		pm_nl_set_limits $ns1 0 2
2298		pm_nl_set_limits $ns2 2 2
2299		run_tests $ns1 $ns2 dead:beef:1::1 0 2 0 slow
2300		chk_join_nr 2 2 2
2301		chk_add_nr 2 2
2302	fi
2303}
2304
2305ipv6_tests()
2306{
2307	# subflow IPv6
2308	if reset "single subflow IPv6"; then
2309		pm_nl_set_limits $ns1 0 1
2310		pm_nl_set_limits $ns2 0 1
2311		pm_nl_add_endpoint $ns2 dead:beef:3::2 dev ns2eth3 flags subflow
2312		run_tests $ns1 $ns2 dead:beef:1::1 0 0 0 slow
2313		chk_join_nr 1 1 1
2314	fi
2315
2316	# add_address, unused IPv6
2317	if reset "unused signal address IPv6"; then
2318		pm_nl_add_endpoint $ns1 dead:beef:2::1 flags signal
2319		run_tests $ns1 $ns2 dead:beef:1::1 0 0 0 slow
2320		chk_join_nr 0 0 0
2321		chk_add_nr 1 1
2322	fi
2323
2324	# signal address IPv6
2325	if reset "single address IPv6"; then
2326		pm_nl_set_limits $ns1 0 1
2327		pm_nl_add_endpoint $ns1 dead:beef:2::1 flags signal
2328		pm_nl_set_limits $ns2 1 1
2329		run_tests $ns1 $ns2 dead:beef:1::1 0 0 0 slow
2330		chk_join_nr 1 1 1
2331		chk_add_nr 1 1
2332	fi
2333
2334	# single address IPv6, remove
2335	if reset "remove single address IPv6"; then
2336		pm_nl_set_limits $ns1 0 1
2337		pm_nl_add_endpoint $ns1 dead:beef:2::1 flags signal
2338		pm_nl_set_limits $ns2 1 1
2339		run_tests $ns1 $ns2 dead:beef:1::1 0 -1 0 slow
2340		chk_join_nr 1 1 1
2341		chk_add_nr 1 1
2342		chk_rm_nr 1 1 invert
2343	fi
2344
2345	# subflow and signal IPv6, remove
2346	if reset "remove subflow and signal IPv6"; then
2347		pm_nl_set_limits $ns1 0 2
2348		pm_nl_add_endpoint $ns1 dead:beef:2::1 flags signal
2349		pm_nl_set_limits $ns2 1 2
2350		pm_nl_add_endpoint $ns2 dead:beef:3::2 dev ns2eth3 flags subflow
2351		run_tests $ns1 $ns2 dead:beef:1::1 0 -1 -1 slow
2352		chk_join_nr 2 2 2
2353		chk_add_nr 1 1
2354		chk_rm_nr 1 1
2355	fi
2356}
2357
2358v4mapped_tests()
2359{
2360	# subflow IPv4-mapped to IPv4-mapped
2361	if reset "single subflow IPv4-mapped"; then
2362		pm_nl_set_limits $ns1 0 1
2363		pm_nl_set_limits $ns2 0 1
2364		pm_nl_add_endpoint $ns2 "::ffff:10.0.3.2" flags subflow
2365		run_tests $ns1 $ns2 "::ffff:10.0.1.1"
2366		chk_join_nr 1 1 1
2367	fi
2368
2369	# signal address IPv4-mapped with IPv4-mapped sk
2370	if reset "signal address IPv4-mapped"; then
2371		pm_nl_set_limits $ns1 0 1
2372		pm_nl_set_limits $ns2 1 1
2373		pm_nl_add_endpoint $ns1 "::ffff:10.0.2.1" flags signal
2374		run_tests $ns1 $ns2 "::ffff:10.0.1.1"
2375		chk_join_nr 1 1 1
2376		chk_add_nr 1 1
2377	fi
2378
2379	# subflow v4-map-v6
2380	if reset "single subflow v4-map-v6"; then
2381		pm_nl_set_limits $ns1 0 1
2382		pm_nl_set_limits $ns2 0 1
2383		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
2384		run_tests $ns1 $ns2 "::ffff:10.0.1.1"
2385		chk_join_nr 1 1 1
2386	fi
2387
2388	# signal address v4-map-v6
2389	if reset "signal address v4-map-v6"; then
2390		pm_nl_set_limits $ns1 0 1
2391		pm_nl_set_limits $ns2 1 1
2392		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
2393		run_tests $ns1 $ns2 "::ffff:10.0.1.1"
2394		chk_join_nr 1 1 1
2395		chk_add_nr 1 1
2396	fi
2397
2398	# subflow v6-map-v4
2399	if reset "single subflow v6-map-v4"; then
2400		pm_nl_set_limits $ns1 0 1
2401		pm_nl_set_limits $ns2 0 1
2402		pm_nl_add_endpoint $ns2 "::ffff:10.0.3.2" flags subflow
2403		run_tests $ns1 $ns2 10.0.1.1
2404		chk_join_nr 1 1 1
2405	fi
2406
2407	# signal address v6-map-v4
2408	if reset "signal address v6-map-v4"; then
2409		pm_nl_set_limits $ns1 0 1
2410		pm_nl_set_limits $ns2 1 1
2411		pm_nl_add_endpoint $ns1 "::ffff:10.0.2.1" flags signal
2412		run_tests $ns1 $ns2 10.0.1.1
2413		chk_join_nr 1 1 1
2414		chk_add_nr 1 1
2415	fi
2416
2417	# no subflow IPv6 to v4 address
2418	if reset "no JOIN with diff families v4-v6"; then
2419		pm_nl_set_limits $ns1 0 1
2420		pm_nl_set_limits $ns2 0 1
2421		pm_nl_add_endpoint $ns2 dead:beef:2::2 flags subflow
2422		run_tests $ns1 $ns2 10.0.1.1
2423		chk_join_nr 0 0 0
2424	fi
2425
2426	# no subflow IPv6 to v4 address even if v6 has a valid v4 at the end
2427	if reset "no JOIN with diff families v4-v6-2"; then
2428		pm_nl_set_limits $ns1 0 1
2429		pm_nl_set_limits $ns2 0 1
2430		pm_nl_add_endpoint $ns2 dead:beef:2::10.0.3.2 flags subflow
2431		run_tests $ns1 $ns2 10.0.1.1
2432		chk_join_nr 0 0 0
2433	fi
2434
2435	# no subflow IPv4 to v6 address, no need to slow down too then
2436	if reset "no JOIN with diff families v6-v4"; then
2437		pm_nl_set_limits $ns1 0 1
2438		pm_nl_set_limits $ns2 0 1
2439		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
2440		run_tests $ns1 $ns2 dead:beef:1::1
2441		chk_join_nr 0 0 0
2442	fi
2443}
2444
2445backup_tests()
2446{
2447	# single subflow, backup
2448	if reset "single subflow, backup"; then
2449		pm_nl_set_limits $ns1 0 1
2450		pm_nl_set_limits $ns2 0 1
2451		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow,backup
2452		run_tests $ns1 $ns2 10.0.1.1 0 0 0 slow nobackup
2453		chk_join_nr 1 1 1
2454		chk_prio_nr 0 1
2455	fi
2456
2457	# single address, backup
2458	if reset "single address, backup"; then
2459		pm_nl_set_limits $ns1 0 1
2460		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
2461		pm_nl_set_limits $ns2 1 1
2462		run_tests $ns1 $ns2 10.0.1.1 0 0 0 slow backup
2463		chk_join_nr 1 1 1
2464		chk_add_nr 1 1
2465		chk_prio_nr 1 1
2466	fi
2467
2468	# single address with port, backup
2469	if reset "single address with port, backup"; then
2470		pm_nl_set_limits $ns1 0 1
2471		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal port 10100
2472		pm_nl_set_limits $ns2 1 1
2473		run_tests $ns1 $ns2 10.0.1.1 0 0 0 slow backup
2474		chk_join_nr 1 1 1
2475		chk_add_nr 1 1
2476		chk_prio_nr 1 1
2477	fi
2478
2479	if reset "mpc backup"; then
2480		pm_nl_add_endpoint $ns2 10.0.1.2 flags subflow,backup
2481		run_tests $ns1 $ns2 10.0.1.1 0 0 0 slow
2482		chk_join_nr 0 0 0
2483		chk_prio_nr 0 1
2484	fi
2485
2486	if reset "mpc backup both sides"; then
2487		pm_nl_add_endpoint $ns1 10.0.1.1 flags subflow,backup
2488		pm_nl_add_endpoint $ns2 10.0.1.2 flags subflow,backup
2489		run_tests $ns1 $ns2 10.0.1.1 0 0 0 slow
2490		chk_join_nr 0 0 0
2491		chk_prio_nr 1 1
2492	fi
2493
2494	if reset "mpc switch to backup"; then
2495		pm_nl_add_endpoint $ns2 10.0.1.2 flags subflow
2496		run_tests $ns1 $ns2 10.0.1.1 0 0 0 slow backup
2497		chk_join_nr 0 0 0
2498		chk_prio_nr 0 1
2499	fi
2500
2501	if reset "mpc switch to backup both sides"; then
2502		pm_nl_add_endpoint $ns1 10.0.1.1 flags subflow
2503		pm_nl_add_endpoint $ns2 10.0.1.2 flags subflow
2504		run_tests $ns1 $ns2 10.0.1.1 0 0 0 slow backup
2505		chk_join_nr 0 0 0
2506		chk_prio_nr 1 1
2507	fi
2508}
2509
2510add_addr_ports_tests()
2511{
2512	# signal address with port
2513	if reset "signal address with port"; then
2514		pm_nl_set_limits $ns1 0 1
2515		pm_nl_set_limits $ns2 1 1
2516		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal port 10100
2517		run_tests $ns1 $ns2 10.0.1.1
2518		chk_join_nr 1 1 1
2519		chk_add_nr 1 1 1
2520	fi
2521
2522	# subflow and signal with port
2523	if reset "subflow and signal with port"; then
2524		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal port 10100
2525		pm_nl_set_limits $ns1 0 2
2526		pm_nl_set_limits $ns2 1 2
2527		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
2528		run_tests $ns1 $ns2 10.0.1.1
2529		chk_join_nr 2 2 2
2530		chk_add_nr 1 1 1
2531	fi
2532
2533	# single address with port, remove
2534	if reset "remove single address with port"; then
2535		pm_nl_set_limits $ns1 0 1
2536		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal port 10100
2537		pm_nl_set_limits $ns2 1 1
2538		run_tests $ns1 $ns2 10.0.1.1 0 -1 0 slow
2539		chk_join_nr 1 1 1
2540		chk_add_nr 1 1 1
2541		chk_rm_nr 1 1 invert
2542	fi
2543
2544	# subflow and signal with port, remove
2545	if reset "remove subflow and signal with port"; then
2546		pm_nl_set_limits $ns1 0 2
2547		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal port 10100
2548		pm_nl_set_limits $ns2 1 2
2549		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
2550		run_tests $ns1 $ns2 10.0.1.1 0 -1 -1 slow
2551		chk_join_nr 2 2 2
2552		chk_add_nr 1 1 1
2553		chk_rm_nr 1 1
2554	fi
2555
2556	# subflows and signal with port, flush
2557	if reset "flush subflows and signal with port"; then
2558		pm_nl_set_limits $ns1 0 3
2559		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal port 10100
2560		pm_nl_set_limits $ns2 1 3
2561		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
2562		pm_nl_add_endpoint $ns2 10.0.4.2 flags subflow
2563		run_tests $ns1 $ns2 10.0.1.1 0 -8 -2 slow
2564		chk_join_nr 3 3 3
2565		chk_add_nr 1 1
2566		chk_rm_nr 1 3 invert simult
2567	fi
2568
2569	# multiple addresses with port
2570	if reset "multiple addresses with port"; then
2571		pm_nl_set_limits $ns1 2 2
2572		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal port 10100
2573		pm_nl_add_endpoint $ns1 10.0.3.1 flags signal port 10100
2574		pm_nl_set_limits $ns2 2 2
2575		run_tests $ns1 $ns2 10.0.1.1
2576		chk_join_nr 2 2 2
2577		chk_add_nr 2 2 2
2578	fi
2579
2580	# multiple addresses with ports
2581	if reset "multiple addresses with ports"; then
2582		pm_nl_set_limits $ns1 2 2
2583		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal port 10100
2584		pm_nl_add_endpoint $ns1 10.0.3.1 flags signal port 10101
2585		pm_nl_set_limits $ns2 2 2
2586		run_tests $ns1 $ns2 10.0.1.1
2587		chk_join_nr 2 2 2
2588		chk_add_nr 2 2 2
2589	fi
2590}
2591
2592syncookies_tests()
2593{
2594	# single subflow, syncookies
2595	if reset_with_cookies "single subflow with syn cookies"; then
2596		pm_nl_set_limits $ns1 0 1
2597		pm_nl_set_limits $ns2 0 1
2598		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
2599		run_tests $ns1 $ns2 10.0.1.1
2600		chk_join_nr 1 1 1
2601	fi
2602
2603	# multiple subflows with syn cookies
2604	if reset_with_cookies "multiple subflows with syn cookies"; then
2605		pm_nl_set_limits $ns1 0 2
2606		pm_nl_set_limits $ns2 0 2
2607		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
2608		pm_nl_add_endpoint $ns2 10.0.2.2 flags subflow
2609		run_tests $ns1 $ns2 10.0.1.1
2610		chk_join_nr 2 2 2
2611	fi
2612
2613	# multiple subflows limited by server
2614	if reset_with_cookies "subflows limited by server w cookies"; then
2615		pm_nl_set_limits $ns1 0 1
2616		pm_nl_set_limits $ns2 0 2
2617		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
2618		pm_nl_add_endpoint $ns2 10.0.2.2 flags subflow
2619		run_tests $ns1 $ns2 10.0.1.1
2620		chk_join_nr 2 1 1
2621	fi
2622
2623	# test signal address with cookies
2624	if reset_with_cookies "signal address with syn cookies"; then
2625		pm_nl_set_limits $ns1 0 1
2626		pm_nl_set_limits $ns2 1 1
2627		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
2628		run_tests $ns1 $ns2 10.0.1.1
2629		chk_join_nr 1 1 1
2630		chk_add_nr 1 1
2631	fi
2632
2633	# test cookie with subflow and signal
2634	if reset_with_cookies "subflow and signal w cookies"; then
2635		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
2636		pm_nl_set_limits $ns1 0 2
2637		pm_nl_set_limits $ns2 1 2
2638		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
2639		run_tests $ns1 $ns2 10.0.1.1
2640		chk_join_nr 2 2 2
2641		chk_add_nr 1 1
2642	fi
2643
2644	# accept and use add_addr with additional subflows
2645	if reset_with_cookies "subflows and signal w. cookies"; then
2646		pm_nl_set_limits $ns1 0 3
2647		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
2648		pm_nl_set_limits $ns2 1 3
2649		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
2650		pm_nl_add_endpoint $ns2 10.0.4.2 flags subflow
2651		run_tests $ns1 $ns2 10.0.1.1
2652		chk_join_nr 3 3 3
2653		chk_add_nr 1 1
2654	fi
2655}
2656
2657checksum_tests()
2658{
2659	# checksum test 0 0
2660	if reset_with_checksum 0 0; then
2661		pm_nl_set_limits $ns1 0 1
2662		pm_nl_set_limits $ns2 0 1
2663		run_tests $ns1 $ns2 10.0.1.1
2664		chk_join_nr 0 0 0
2665	fi
2666
2667	# checksum test 1 1
2668	if reset_with_checksum 1 1; then
2669		pm_nl_set_limits $ns1 0 1
2670		pm_nl_set_limits $ns2 0 1
2671		run_tests $ns1 $ns2 10.0.1.1
2672		chk_join_nr 0 0 0
2673	fi
2674
2675	# checksum test 0 1
2676	if reset_with_checksum 0 1; then
2677		pm_nl_set_limits $ns1 0 1
2678		pm_nl_set_limits $ns2 0 1
2679		run_tests $ns1 $ns2 10.0.1.1
2680		chk_join_nr 0 0 0
2681	fi
2682
2683	# checksum test 1 0
2684	if reset_with_checksum 1 0; then
2685		pm_nl_set_limits $ns1 0 1
2686		pm_nl_set_limits $ns2 0 1
2687		run_tests $ns1 $ns2 10.0.1.1
2688		chk_join_nr 0 0 0
2689	fi
2690}
2691
2692deny_join_id0_tests()
2693{
2694	# subflow allow join id0 ns1
2695	if reset_with_allow_join_id0 "single subflow allow join id0 ns1" 1 0; then
2696		pm_nl_set_limits $ns1 1 1
2697		pm_nl_set_limits $ns2 1 1
2698		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
2699		run_tests $ns1 $ns2 10.0.1.1
2700		chk_join_nr 1 1 1
2701	fi
2702
2703	# subflow allow join id0 ns2
2704	if reset_with_allow_join_id0 "single subflow allow join id0 ns2" 0 1; then
2705		pm_nl_set_limits $ns1 1 1
2706		pm_nl_set_limits $ns2 1 1
2707		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
2708		run_tests $ns1 $ns2 10.0.1.1
2709		chk_join_nr 0 0 0
2710	fi
2711
2712	# signal address allow join id0 ns1
2713	# ADD_ADDRs are not affected by allow_join_id0 value.
2714	if reset_with_allow_join_id0 "signal address allow join id0 ns1" 1 0; then
2715		pm_nl_set_limits $ns1 1 1
2716		pm_nl_set_limits $ns2 1 1
2717		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
2718		run_tests $ns1 $ns2 10.0.1.1
2719		chk_join_nr 1 1 1
2720		chk_add_nr 1 1
2721	fi
2722
2723	# signal address allow join id0 ns2
2724	# ADD_ADDRs are not affected by allow_join_id0 value.
2725	if reset_with_allow_join_id0 "signal address allow join id0 ns2" 0 1; then
2726		pm_nl_set_limits $ns1 1 1
2727		pm_nl_set_limits $ns2 1 1
2728		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
2729		run_tests $ns1 $ns2 10.0.1.1
2730		chk_join_nr 1 1 1
2731		chk_add_nr 1 1
2732	fi
2733
2734	# subflow and address allow join id0 ns1
2735	if reset_with_allow_join_id0 "subflow and address allow join id0 1" 1 0; then
2736		pm_nl_set_limits $ns1 2 2
2737		pm_nl_set_limits $ns2 2 2
2738		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
2739		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
2740		run_tests $ns1 $ns2 10.0.1.1
2741		chk_join_nr 2 2 2
2742	fi
2743
2744	# subflow and address allow join id0 ns2
2745	if reset_with_allow_join_id0 "subflow and address allow join id0 2" 0 1; then
2746		pm_nl_set_limits $ns1 2 2
2747		pm_nl_set_limits $ns2 2 2
2748		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
2749		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
2750		run_tests $ns1 $ns2 10.0.1.1
2751		chk_join_nr 1 1 1
2752	fi
2753}
2754
2755fullmesh_tests()
2756{
2757	# fullmesh 1
2758	# 2 fullmesh addrs in ns2, added before the connection,
2759	# 1 non-fullmesh addr in ns1, added during the connection.
2760	if reset "fullmesh test 2x1"; then
2761		pm_nl_set_limits $ns1 0 4
2762		pm_nl_set_limits $ns2 1 4
2763		pm_nl_add_endpoint $ns2 10.0.2.2 flags subflow,fullmesh
2764		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow,fullmesh
2765		run_tests $ns1 $ns2 10.0.1.1 0 1 0 slow
2766		chk_join_nr 4 4 4
2767		chk_add_nr 1 1
2768	fi
2769
2770	# fullmesh 2
2771	# 1 non-fullmesh addr in ns1, added before the connection,
2772	# 1 fullmesh addr in ns2, added during the connection.
2773	if reset "fullmesh test 1x1"; then
2774		pm_nl_set_limits $ns1 1 3
2775		pm_nl_set_limits $ns2 1 3
2776		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
2777		run_tests $ns1 $ns2 10.0.1.1 0 0 fullmesh_1 slow
2778		chk_join_nr 3 3 3
2779		chk_add_nr 1 1
2780	fi
2781
2782	# fullmesh 3
2783	# 1 non-fullmesh addr in ns1, added before the connection,
2784	# 2 fullmesh addrs in ns2, added during the connection.
2785	if reset "fullmesh test 1x2"; then
2786		pm_nl_set_limits $ns1 2 5
2787		pm_nl_set_limits $ns2 1 5
2788		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
2789		run_tests $ns1 $ns2 10.0.1.1 0 0 fullmesh_2 slow
2790		chk_join_nr 5 5 5
2791		chk_add_nr 1 1
2792	fi
2793
2794	# fullmesh 4
2795	# 1 non-fullmesh addr in ns1, added before the connection,
2796	# 2 fullmesh addrs in ns2, added during the connection,
2797	# limit max_subflows to 4.
2798	if reset "fullmesh test 1x2, limited"; then
2799		pm_nl_set_limits $ns1 2 4
2800		pm_nl_set_limits $ns2 1 4
2801		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
2802		run_tests $ns1 $ns2 10.0.1.1 0 0 fullmesh_2 slow
2803		chk_join_nr 4 4 4
2804		chk_add_nr 1 1
2805	fi
2806
2807	# set fullmesh flag
2808	if reset "set fullmesh flag test"; then
2809		pm_nl_set_limits $ns1 4 4
2810		pm_nl_add_endpoint $ns1 10.0.2.1 flags subflow
2811		pm_nl_set_limits $ns2 4 4
2812		run_tests $ns1 $ns2 10.0.1.1 0 0 1 slow fullmesh
2813		chk_join_nr 2 2 2
2814		chk_rm_nr 0 1
2815	fi
2816
2817	# set nofullmesh flag
2818	if reset "set nofullmesh flag test"; then
2819		pm_nl_set_limits $ns1 4 4
2820		pm_nl_add_endpoint $ns1 10.0.2.1 flags subflow,fullmesh
2821		pm_nl_set_limits $ns2 4 4
2822		run_tests $ns1 $ns2 10.0.1.1 0 0 fullmesh_1 slow nofullmesh
2823		chk_join_nr 2 2 2
2824		chk_rm_nr 0 1
2825	fi
2826
2827	# set backup,fullmesh flags
2828	if reset "set backup,fullmesh flags test"; then
2829		pm_nl_set_limits $ns1 4 4
2830		pm_nl_add_endpoint $ns1 10.0.2.1 flags subflow
2831		pm_nl_set_limits $ns2 4 4
2832		run_tests $ns1 $ns2 10.0.1.1 0 0 1 slow backup,fullmesh
2833		chk_join_nr 2 2 2
2834		chk_prio_nr 0 1
2835		chk_rm_nr 0 1
2836	fi
2837
2838	# set nobackup,nofullmesh flags
2839	if reset "set nobackup,nofullmesh flags test"; then
2840		pm_nl_set_limits $ns1 4 4
2841		pm_nl_set_limits $ns2 4 4
2842		pm_nl_add_endpoint $ns2 10.0.2.2 flags subflow,backup,fullmesh
2843		run_tests $ns1 $ns2 10.0.1.1 0 0 0 slow nobackup,nofullmesh
2844		chk_join_nr 2 2 2
2845		chk_prio_nr 0 1
2846		chk_rm_nr 0 1
2847	fi
2848}
2849
2850fastclose_tests()
2851{
2852	if reset "fastclose test"; then
2853		run_tests $ns1 $ns2 10.0.1.1 1024 0 fastclose_client
2854		chk_join_nr 0 0 0
2855		chk_fclose_nr 1 1
2856		chk_rst_nr 1 1 invert
2857	fi
2858
2859	if reset "fastclose server test"; then
2860		run_tests $ns1 $ns2 10.0.1.1 1024 0 fastclose_server
2861		chk_join_nr 0 0 0
2862		chk_fclose_nr 1 1 invert
2863		chk_rst_nr 1 1
2864	fi
2865}
2866
2867pedit_action_pkts()
2868{
2869	tc -n $ns2 -j -s action show action pedit index 100 | \
2870		grep "packets" | \
2871		sed 's/.*"packets":\([0-9]\+\),.*/\1/'
2872}
2873
2874fail_tests()
2875{
2876	# single subflow
2877	if reset_with_fail "Infinite map" 1; then
2878		run_tests $ns1 $ns2 10.0.1.1 128
2879		chk_join_nr 0 0 0 +1 +0 1 0 1 "$(pedit_action_pkts)"
2880		chk_fail_nr 1 -1 invert
2881	fi
2882
2883	# multiple subflows
2884	if reset_with_fail "MP_FAIL MP_RST" 2; then
2885		tc -n $ns2 qdisc add dev ns2eth1 root netem rate 1mbit delay 5
2886		pm_nl_set_limits $ns1 0 1
2887		pm_nl_set_limits $ns2 0 1
2888		pm_nl_add_endpoint $ns2 10.0.2.2 dev ns2eth2 flags subflow
2889		run_tests $ns1 $ns2 10.0.1.1 1024
2890		chk_join_nr 1 1 1 1 0 1 1 0 "$(pedit_action_pkts)"
2891	fi
2892}
2893
2894userspace_tests()
2895{
2896	# userspace pm type prevents add_addr
2897	if reset "userspace pm type prevents add_addr"; then
2898		set_userspace_pm $ns1
2899		pm_nl_set_limits $ns1 0 2
2900		pm_nl_set_limits $ns2 0 2
2901		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
2902		run_tests $ns1 $ns2 10.0.1.1
2903		chk_join_nr 0 0 0
2904		chk_add_nr 0 0
2905	fi
2906
2907	# userspace pm type does not echo add_addr without daemon
2908	if reset "userspace pm no echo w/o daemon"; then
2909		set_userspace_pm $ns2
2910		pm_nl_set_limits $ns1 0 2
2911		pm_nl_set_limits $ns2 0 2
2912		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
2913		run_tests $ns1 $ns2 10.0.1.1
2914		chk_join_nr 0 0 0
2915		chk_add_nr 1 0
2916	fi
2917
2918	# userspace pm type rejects join
2919	if reset "userspace pm type rejects join"; then
2920		set_userspace_pm $ns1
2921		pm_nl_set_limits $ns1 1 1
2922		pm_nl_set_limits $ns2 1 1
2923		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
2924		run_tests $ns1 $ns2 10.0.1.1
2925		chk_join_nr 1 1 0
2926	fi
2927
2928	# userspace pm type does not send join
2929	if reset "userspace pm type does not send join"; then
2930		set_userspace_pm $ns2
2931		pm_nl_set_limits $ns1 1 1
2932		pm_nl_set_limits $ns2 1 1
2933		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
2934		run_tests $ns1 $ns2 10.0.1.1
2935		chk_join_nr 0 0 0
2936	fi
2937
2938	# userspace pm type prevents mp_prio
2939	if reset "userspace pm type prevents mp_prio"; then
2940		set_userspace_pm $ns1
2941		pm_nl_set_limits $ns1 1 1
2942		pm_nl_set_limits $ns2 1 1
2943		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
2944		run_tests $ns1 $ns2 10.0.1.1 0 0 0 slow backup
2945		chk_join_nr 1 1 0
2946		chk_prio_nr 0 0
2947	fi
2948
2949	# userspace pm type prevents rm_addr
2950	if reset "userspace pm type prevents rm_addr"; then
2951		set_userspace_pm $ns1
2952		set_userspace_pm $ns2
2953		pm_nl_set_limits $ns1 0 1
2954		pm_nl_set_limits $ns2 0 1
2955		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
2956		run_tests $ns1 $ns2 10.0.1.1 0 0 -1 slow
2957		chk_join_nr 0 0 0
2958		chk_rm_nr 0 0
2959	fi
2960
2961	# userspace pm add & remove address
2962	if reset "userspace pm add & remove address"; then
2963		set_userspace_pm $ns1
2964		pm_nl_set_limits $ns2 1 1
2965		run_tests $ns1 $ns2 10.0.1.1 0 userspace_1 0 slow
2966		chk_join_nr 1 1 1
2967		chk_add_nr 1 1
2968		chk_rm_nr 1 1 invert
2969	fi
2970
2971	# userspace pm create destroy subflow
2972	if reset "userspace pm create destroy subflow"; then
2973		set_userspace_pm $ns2
2974		pm_nl_set_limits $ns1 0 1
2975		run_tests $ns1 $ns2 10.0.1.1 0 0 userspace_1 slow
2976		chk_join_nr 1 1 1
2977		chk_rm_nr 0 1
2978	fi
2979}
2980
2981endpoint_tests()
2982{
2983	# userspace pm type prevents add_addr
2984	if reset "implicit EP"; then
2985		pm_nl_set_limits $ns1 2 2
2986		pm_nl_set_limits $ns2 2 2
2987		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
2988		run_tests $ns1 $ns2 10.0.1.1 0 0 0 slow &
2989
2990		wait_mpj $ns1
2991		pm_nl_check_endpoint 1 "creation" \
2992			$ns2 10.0.2.2 id 1 flags implicit
2993
2994		pm_nl_add_endpoint $ns2 10.0.2.2 id 33
2995		pm_nl_check_endpoint 0 "ID change is prevented" \
2996			$ns2 10.0.2.2 id 1 flags implicit
2997
2998		pm_nl_add_endpoint $ns2 10.0.2.2 flags signal
2999		pm_nl_check_endpoint 0 "modif is allowed" \
3000			$ns2 10.0.2.2 id 1 flags signal
3001		wait
3002	fi
3003
3004	if reset "delete and re-add"; then
3005		pm_nl_set_limits $ns1 1 1
3006		pm_nl_set_limits $ns2 1 1
3007		pm_nl_add_endpoint $ns2 10.0.2.2 id 2 dev ns2eth2 flags subflow
3008		run_tests $ns1 $ns2 10.0.1.1 4 0 0 slow &
3009
3010		wait_mpj $ns2
3011		pm_nl_del_endpoint $ns2 2 10.0.2.2
3012		sleep 0.5
3013		chk_subflow_nr needtitle "after delete" 1
3014
3015		pm_nl_add_endpoint $ns2 10.0.2.2 dev ns2eth2 flags subflow
3016		wait_mpj $ns2
3017		chk_subflow_nr "" "after re-add" 2
3018		wait
3019	fi
3020}
3021
3022# [$1: error message]
3023usage()
3024{
3025	if [ -n "${1}" ]; then
3026		echo "${1}"
3027		ret=1
3028	fi
3029
3030	echo "mptcp_join usage:"
3031
3032	local key
3033	for key in "${!all_tests[@]}"; do
3034		echo "  -${key} ${all_tests[${key}]}"
3035	done
3036
3037	echo "  -c capture pcap files"
3038	echo "  -C enable data checksum"
3039	echo "  -i use ip mptcp"
3040	echo "  -h help"
3041
3042	echo "[test ids|names]"
3043
3044	exit ${ret}
3045}
3046
3047
3048# Use a "simple" array to force an specific order we cannot have with an associative one
3049all_tests_sorted=(
3050	f@subflows_tests
3051	e@subflows_error_tests
3052	s@signal_address_tests
3053	l@link_failure_tests
3054	t@add_addr_timeout_tests
3055	r@remove_tests
3056	a@add_tests
3057	6@ipv6_tests
3058	4@v4mapped_tests
3059	b@backup_tests
3060	p@add_addr_ports_tests
3061	k@syncookies_tests
3062	S@checksum_tests
3063	d@deny_join_id0_tests
3064	m@fullmesh_tests
3065	z@fastclose_tests
3066	F@fail_tests
3067	u@userspace_tests
3068	I@endpoint_tests
3069)
3070
3071all_tests_args=""
3072all_tests_names=()
3073for subtests in "${all_tests_sorted[@]}"; do
3074	key="${subtests%@*}"
3075	value="${subtests#*@}"
3076
3077	all_tests_args+="${key}"
3078	all_tests_names+=("${value}")
3079	all_tests[${key}]="${value}"
3080done
3081
3082tests=()
3083while getopts "${all_tests_args}cCih" opt; do
3084	case $opt in
3085		["${all_tests_args}"])
3086			tests+=("${all_tests[${opt}]}")
3087			;;
3088		c)
3089			capture=1
3090			;;
3091		C)
3092			checksum=1
3093			;;
3094		i)
3095			ip_mptcp=1
3096			;;
3097		h)
3098			usage
3099			;;
3100		*)
3101			usage "Unknown option: -${opt}"
3102			;;
3103	esac
3104done
3105
3106shift $((OPTIND - 1))
3107
3108for arg in "${@}"; do
3109	if [[ "${arg}" =~ ^[0-9]+$ ]]; then
3110		only_tests_ids+=("${arg}")
3111	else
3112		only_tests_names+=("${arg}")
3113	fi
3114done
3115
3116if [ ${#tests[@]} -eq 0 ]; then
3117	tests=("${all_tests_names[@]}")
3118fi
3119
3120for subtests in "${tests[@]}"; do
3121	"${subtests}"
3122done
3123
3124if [ ${ret} -ne 0 ]; then
3125	echo
3126	echo "${#failed_tests[@]} failure(s) has(ve) been detected:"
3127	for i in $(get_failed_tests_ids); do
3128		echo -e "\t- ${i}: ${failed_tests[${i}]}"
3129	done
3130	echo
3131fi
3132
3133exit $ret
3134