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