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