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