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