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