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