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