1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3
4ret=0
5sin=""
6sinfail=""
7sout=""
8cin=""
9cinfail=""
10cinsent=""
11cout=""
12ksft_skip=4
13timeout_poll=30
14timeout_test=$((timeout_poll * 2 + 1))
15mptcp_connect=""
16capture=0
17checksum=0
18do_all_tests=1
19
20TEST_COUNT=0
21
22# generated using "nfbpf_compile '(ip && (ip[54] & 0xf0) == 0x30) ||
23#				  (ip6 && (ip6[74] & 0xf0) == 0x30)'"
24CBPF_MPTCP_SUBOPTION_ADD_ADDR="14,
25			       48 0 0 0,
26			       84 0 0 240,
27			       21 0 3 64,
28			       48 0 0 54,
29			       84 0 0 240,
30			       21 6 7 48,
31			       48 0 0 0,
32			       84 0 0 240,
33			       21 0 4 96,
34			       48 0 0 74,
35			       84 0 0 240,
36			       21 0 1 48,
37			       6 0 0 65535,
38			       6 0 0 0"
39
40init()
41{
42	capout=$(mktemp)
43
44	rndh=$(printf %x $sec)-$(mktemp -u XXXXXX)
45
46	ns1="ns1-$rndh"
47	ns2="ns2-$rndh"
48
49	for netns in "$ns1" "$ns2";do
50		ip netns add $netns || exit $ksft_skip
51		ip -net $netns link set lo up
52		ip netns exec $netns sysctl -q net.mptcp.enabled=1
53		ip netns exec $netns sysctl -q net.ipv4.conf.all.rp_filter=0
54		ip netns exec $netns sysctl -q net.ipv4.conf.default.rp_filter=0
55		if [ $checksum -eq 1 ]; then
56			ip netns exec $netns sysctl -q net.mptcp.checksum_enabled=1
57		fi
58	done
59
60	#  ns1              ns2
61	# ns1eth1    ns2eth1
62	# ns1eth2    ns2eth2
63	# ns1eth3    ns2eth3
64	# ns1eth4    ns2eth4
65
66	for i in `seq 1 4`; do
67		ip link add ns1eth$i netns "$ns1" type veth peer name ns2eth$i netns "$ns2"
68		ip -net "$ns1" addr add 10.0.$i.1/24 dev ns1eth$i
69		ip -net "$ns1" addr add dead:beef:$i::1/64 dev ns1eth$i nodad
70		ip -net "$ns1" link set ns1eth$i up
71
72		ip -net "$ns2" addr add 10.0.$i.2/24 dev ns2eth$i
73		ip -net "$ns2" addr add dead:beef:$i::2/64 dev ns2eth$i nodad
74		ip -net "$ns2" link set ns2eth$i up
75
76		# let $ns2 reach any $ns1 address from any interface
77		ip -net "$ns2" route add default via 10.0.$i.1 dev ns2eth$i metric 10$i
78		ip -net "$ns2" route add default via dead:beef:$i::1 dev ns2eth$i metric 10$i
79	done
80}
81
82init_shapers()
83{
84	for i in `seq 1 4`; do
85		tc -n $ns1 qdisc add dev ns1eth$i root netem rate 20mbit delay 1
86		tc -n $ns2 qdisc add dev ns2eth$i root netem rate 20mbit delay 1
87	done
88}
89
90cleanup_partial()
91{
92	rm -f "$capout"
93
94	for netns in "$ns1" "$ns2"; do
95		ip netns del $netns
96		rm -f /tmp/$netns.{nstat,out}
97	done
98}
99
100cleanup()
101{
102	rm -f "$cin" "$cout" "$sinfail"
103	rm -f "$sin" "$sout" "$cinsent" "$cinfail"
104	cleanup_partial
105}
106
107reset()
108{
109	cleanup_partial
110	init
111}
112
113reset_with_cookies()
114{
115	reset
116
117	for netns in "$ns1" "$ns2";do
118		ip netns exec $netns sysctl -q net.ipv4.tcp_syncookies=2
119	done
120}
121
122reset_with_add_addr_timeout()
123{
124	local ip="${1:-4}"
125	local tables
126
127	tables="iptables"
128	if [ $ip -eq 6 ]; then
129		tables="ip6tables"
130	fi
131
132	reset
133
134	ip netns exec $ns1 sysctl -q net.mptcp.add_addr_timeout=1
135	ip netns exec $ns2 $tables -A OUTPUT -p tcp \
136		-m tcp --tcp-option 30 \
137		-m bpf --bytecode \
138		"$CBPF_MPTCP_SUBOPTION_ADD_ADDR" \
139		-j DROP
140}
141
142reset_with_checksum()
143{
144	local ns1_enable=$1
145	local ns2_enable=$2
146
147	reset
148
149	ip netns exec $ns1 sysctl -q net.mptcp.checksum_enabled=$ns1_enable
150	ip netns exec $ns2 sysctl -q net.mptcp.checksum_enabled=$ns2_enable
151}
152
153reset_with_allow_join_id0()
154{
155	local ns1_enable=$1
156	local ns2_enable=$2
157
158	reset
159
160	ip netns exec $ns1 sysctl -q net.mptcp.allow_join_initial_addr_port=$ns1_enable
161	ip netns exec $ns2 sysctl -q net.mptcp.allow_join_initial_addr_port=$ns2_enable
162}
163
164ip -Version > /dev/null 2>&1
165if [ $? -ne 0 ];then
166	echo "SKIP: Could not run test without ip tool"
167	exit $ksft_skip
168fi
169
170iptables -V > /dev/null 2>&1
171if [ $? -ne 0 ];then
172	echo "SKIP: Could not run all tests without iptables tool"
173	exit $ksft_skip
174fi
175
176ip6tables -V > /dev/null 2>&1
177if [ $? -ne 0 ];then
178	echo "SKIP: Could not run all tests without ip6tables tool"
179	exit $ksft_skip
180fi
181
182print_file_err()
183{
184	ls -l "$1" 1>&2
185	echo "Trailing bytes are: "
186	tail -c 27 "$1"
187}
188
189check_transfer()
190{
191	in=$1
192	out=$2
193	what=$3
194
195	cmp "$in" "$out" > /dev/null 2>&1
196	if [ $? -ne 0 ] ;then
197		echo "[ FAIL ] $what does not match (in, out):"
198		print_file_err "$in"
199		print_file_err "$out"
200		ret=1
201
202		return 1
203	fi
204
205	return 0
206}
207
208do_ping()
209{
210	listener_ns="$1"
211	connector_ns="$2"
212	connect_addr="$3"
213
214	ip netns exec ${connector_ns} ping -q -c 1 $connect_addr >/dev/null
215	if [ $? -ne 0 ] ; then
216		echo "$listener_ns -> $connect_addr connectivity [ FAIL ]" 1>&2
217		ret=1
218	fi
219}
220
221link_failure()
222{
223	ns="$1"
224
225	if [ -z "$FAILING_LINKS" ]; then
226		l=$((RANDOM%4))
227		FAILING_LINKS=$((l+1))
228	fi
229
230	for l in $FAILING_LINKS; do
231		veth="ns1eth$l"
232		ip -net "$ns" link set "$veth" down
233	done
234}
235
236# $1: IP address
237is_v6()
238{
239	[ -z "${1##*:*}" ]
240}
241
242# $1: ns, $2: port
243wait_local_port_listen()
244{
245	local listener_ns="${1}"
246	local port="${2}"
247
248	local port_hex i
249
250	port_hex="$(printf "%04X" "${port}")"
251	for i in $(seq 10); do
252		ip netns exec "${listener_ns}" cat /proc/net/tcp* | \
253			awk "BEGIN {rc=1} {if (\$2 ~ /:${port_hex}\$/ && \$4 ~ /0A/) {rc=0; exit}} END {exit rc}" &&
254			break
255		sleep 0.1
256	done
257}
258
259rm_addr_count()
260{
261	ns=${1}
262
263	ip netns exec ${ns} nstat -as | grep MPTcpExtRmAddr | awk '{print $2}'
264}
265
266# $1: ns, $2: old rm_addr counter in $ns
267wait_rm_addr()
268{
269	local ns="${1}"
270	local old_cnt="${2}"
271	local cnt
272	local i
273
274	for i in $(seq 10); do
275		cnt=$(rm_addr_count ${ns})
276		[ "$cnt" = "${old_cnt}" ] || break
277		sleep 0.1
278	done
279}
280
281do_transfer()
282{
283	listener_ns="$1"
284	connector_ns="$2"
285	cl_proto="$3"
286	srv_proto="$4"
287	connect_addr="$5"
288	test_link_fail="$6"
289	addr_nr_ns1="$7"
290	addr_nr_ns2="$8"
291	speed="$9"
292	bkup="${10}"
293
294	port=$((10000+$TEST_COUNT))
295	TEST_COUNT=$((TEST_COUNT+1))
296
297	:> "$cout"
298	:> "$sout"
299	:> "$capout"
300
301	if [ $capture -eq 1 ]; then
302		if [ -z $SUDO_USER ] ; then
303			capuser=""
304		else
305			capuser="-Z $SUDO_USER"
306		fi
307
308		capfile=$(printf "mp_join-%02u-%s.pcap" "$TEST_COUNT" "${listener_ns}")
309
310		echo "Capturing traffic for test $TEST_COUNT into $capfile"
311		ip netns exec ${listener_ns} tcpdump -i any -s 65535 -B 32768 $capuser -w $capfile > "$capout" 2>&1 &
312		cappid=$!
313
314		sleep 1
315	fi
316
317	NSTAT_HISTORY=/tmp/${listener_ns}.nstat ip netns exec ${listener_ns} \
318		nstat -n
319	NSTAT_HISTORY=/tmp/${connector_ns}.nstat ip netns exec ${connector_ns} \
320		nstat -n
321
322	if [ $speed = "fast" ]; then
323		mptcp_connect="./mptcp_connect -j"
324	elif [ $speed = "slow" ]; then
325		mptcp_connect="./mptcp_connect -r 50"
326	elif [ $speed = "least" ]; then
327		mptcp_connect="./mptcp_connect -r 10"
328	fi
329
330	local local_addr
331	if is_v6 "${connect_addr}"; then
332		local_addr="::"
333	else
334		local_addr="0.0.0.0"
335	fi
336
337	if [ "$test_link_fail" -eq 2 ];then
338		timeout ${timeout_test} \
339			ip netns exec ${listener_ns} \
340				$mptcp_connect -t ${timeout_poll} -l -p $port -s ${srv_proto} \
341					${local_addr} < "$sinfail" > "$sout" &
342	else
343		timeout ${timeout_test} \
344			ip netns exec ${listener_ns} \
345				$mptcp_connect -t ${timeout_poll} -l -p $port -s ${srv_proto} \
346					${local_addr} < "$sin" > "$sout" &
347	fi
348	spid=$!
349
350	wait_local_port_listen "${listener_ns}" "${port}"
351
352	if [ "$test_link_fail" -eq 0 ];then
353		timeout ${timeout_test} \
354			ip netns exec ${connector_ns} \
355				$mptcp_connect -t ${timeout_poll} -p $port -s ${cl_proto} \
356					$connect_addr < "$cin" > "$cout" &
357	else
358		( cat "$cinfail" ; sleep 2; link_failure $listener_ns ; cat "$cinfail" ) | \
359			tee "$cinsent" | \
360			timeout ${timeout_test} \
361				ip netns exec ${connector_ns} \
362					$mptcp_connect -t ${timeout_poll} -p $port -s ${cl_proto} \
363						$connect_addr > "$cout" &
364	fi
365	cpid=$!
366
367	# let the mptcp subflow be established in background before
368	# do endpoint manipulation
369	[ $addr_nr_ns1 = "0" -a $addr_nr_ns2 = "0" ] || sleep 1
370
371	if [ $addr_nr_ns1 -gt 0 ]; then
372		let add_nr_ns1=addr_nr_ns1
373		counter=2
374		while [ $add_nr_ns1 -gt 0 ]; do
375			local addr
376			if is_v6 "${connect_addr}"; then
377				addr="dead:beef:$counter::1"
378			else
379				addr="10.0.$counter.1"
380			fi
381			ip netns exec $ns1 ./pm_nl_ctl add $addr flags signal
382			let counter+=1
383			let add_nr_ns1-=1
384		done
385	elif [ $addr_nr_ns1 -lt 0 ]; then
386		let rm_nr_ns1=-addr_nr_ns1
387		if [ $rm_nr_ns1 -lt 8 ]; then
388			counter=1
389			pos=1
390			dump=(`ip netns exec ${listener_ns} ./pm_nl_ctl dump`)
391			if [ ${#dump[@]} -gt 0 ]; then
392				while [ $counter -le $rm_nr_ns1 ]
393				do
394					id=${dump[$pos]}
395					rm_addr=$(rm_addr_count ${connector_ns})
396					ip netns exec ${listener_ns} ./pm_nl_ctl del $id
397					wait_rm_addr ${connector_ns} ${rm_addr}
398					let counter+=1
399					let pos+=5
400				done
401			fi
402		elif [ $rm_nr_ns1 -eq 8 ]; then
403			ip netns exec ${listener_ns} ./pm_nl_ctl flush
404		elif [ $rm_nr_ns1 -eq 9 ]; then
405			ip netns exec ${listener_ns} ./pm_nl_ctl del 0 ${connect_addr}
406		fi
407	fi
408
409	flags="subflow"
410	if [[ "${addr_nr_ns2}" = "fullmesh_"* ]]; then
411		flags="${flags},fullmesh"
412		addr_nr_ns2=${addr_nr_ns2:9}
413	fi
414
415	# if newly added endpoints must be deleted, give the background msk
416	# some time to created them
417	[ $addr_nr_ns1 -gt 0 -a $addr_nr_ns2 -lt 0 ] && sleep 1
418
419	if [ $addr_nr_ns2 -gt 0 ]; then
420		let add_nr_ns2=addr_nr_ns2
421		counter=3
422		while [ $add_nr_ns2 -gt 0 ]; do
423			local addr
424			if is_v6 "${connect_addr}"; then
425				addr="dead:beef:$counter::2"
426			else
427				addr="10.0.$counter.2"
428			fi
429			ip netns exec $ns2 ./pm_nl_ctl add $addr flags $flags
430			let counter+=1
431			let add_nr_ns2-=1
432		done
433	elif [ $addr_nr_ns2 -lt 0 ]; then
434		let rm_nr_ns2=-addr_nr_ns2
435		if [ $rm_nr_ns2 -lt 8 ]; then
436			counter=1
437			pos=1
438			dump=(`ip netns exec ${connector_ns} ./pm_nl_ctl dump`)
439			if [ ${#dump[@]} -gt 0 ]; then
440				while [ $counter -le $rm_nr_ns2 ]
441				do
442					# rm_addr are serialized, allow the previous one to complete
443					id=${dump[$pos]}
444					rm_addr=$(rm_addr_count ${listener_ns})
445					ip netns exec ${connector_ns} ./pm_nl_ctl del $id
446					wait_rm_addr ${listener_ns} ${rm_addr}
447					let counter+=1
448					let pos+=5
449				done
450			fi
451		elif [ $rm_nr_ns2 -eq 8 ]; then
452			ip netns exec ${connector_ns} ./pm_nl_ctl flush
453		elif [ $rm_nr_ns2 -eq 9 ]; then
454			local addr
455			if is_v6 "${connect_addr}"; then
456				addr="dead:beef:1::2"
457			else
458				addr="10.0.1.2"
459			fi
460			ip netns exec ${connector_ns} ./pm_nl_ctl del 0 $addr
461		fi
462	fi
463
464	if [ ! -z $bkup ]; then
465		sleep 1
466		for netns in "$ns1" "$ns2"; do
467			dump=(`ip netns exec $netns ./pm_nl_ctl dump`)
468			if [ ${#dump[@]} -gt 0 ]; then
469				addr=${dump[${#dump[@]} - 1]}
470				backup="ip netns exec $netns ./pm_nl_ctl set $addr flags $bkup"
471				$backup
472			fi
473		done
474	fi
475
476	wait $cpid
477	retc=$?
478	wait $spid
479	rets=$?
480
481	if [ $capture -eq 1 ]; then
482	    sleep 1
483	    kill $cappid
484	fi
485
486	NSTAT_HISTORY=/tmp/${listener_ns}.nstat ip netns exec ${listener_ns} \
487		nstat | grep Tcp > /tmp/${listener_ns}.out
488	NSTAT_HISTORY=/tmp/${connector_ns}.nstat ip netns exec ${connector_ns} \
489		nstat | grep Tcp > /tmp/${connector_ns}.out
490
491	if [ ${rets} -ne 0 ] || [ ${retc} -ne 0 ]; then
492		echo " client exit code $retc, server $rets" 1>&2
493		echo -e "\nnetns ${listener_ns} socket stat for ${port}:" 1>&2
494		ip netns exec ${listener_ns} ss -Menita 1>&2 -o "sport = :$port"
495		cat /tmp/${listener_ns}.out
496		echo -e "\nnetns ${connector_ns} socket stat for ${port}:" 1>&2
497		ip netns exec ${connector_ns} ss -Menita 1>&2 -o "dport = :$port"
498		cat /tmp/${connector_ns}.out
499
500		cat "$capout"
501		ret=1
502		return 1
503	fi
504
505	if [ "$test_link_fail" -eq 2 ];then
506		check_transfer $sinfail $cout "file received by client"
507	else
508		check_transfer $sin $cout "file received by client"
509	fi
510	retc=$?
511	if [ "$test_link_fail" -eq 0 ];then
512		check_transfer $cin $sout "file received by server"
513	else
514		check_transfer $cinsent $sout "file received by server"
515	fi
516	rets=$?
517
518	if [ $retc -eq 0 ] && [ $rets -eq 0 ];then
519		cat "$capout"
520		return 0
521	fi
522
523	cat "$capout"
524	return 1
525}
526
527make_file()
528{
529	name=$1
530	who=$2
531	size=$3
532
533	dd if=/dev/urandom of="$name" bs=1024 count=$size 2> /dev/null
534	echo -e "\nMPTCP_TEST_FILE_END_MARKER" >> "$name"
535
536	echo "Created $name (size $size KB) containing data sent by $who"
537}
538
539run_tests()
540{
541	listener_ns="$1"
542	connector_ns="$2"
543	connect_addr="$3"
544	test_linkfail="${4:-0}"
545	addr_nr_ns1="${5:-0}"
546	addr_nr_ns2="${6:-0}"
547	speed="${7:-fast}"
548	bkup="${8:-""}"
549	lret=0
550	oldin=""
551
552	# create the input file for the failure test when
553	# the first failure test run
554	if [ "$test_linkfail" -ne 0 -a -z "$cinfail" ]; then
555		# the client file must be considerably larger
556		# of the maximum expected cwin value, or the
557		# link utilization will be not predicable
558		size=$((RANDOM%2))
559		size=$((size+1))
560		size=$((size*8192))
561		size=$((size + ( $RANDOM % 8192) ))
562
563		cinfail=$(mktemp)
564		make_file "$cinfail" "client" $size
565	fi
566
567	if [ "$test_linkfail" -eq 2 -a -z "$sinfail" ]; then
568		size=$((RANDOM%16))
569		size=$((size+1))
570		size=$((size*2048))
571
572		sinfail=$(mktemp)
573		make_file "$sinfail" "server" $size
574	fi
575
576	do_transfer ${listener_ns} ${connector_ns} MPTCP MPTCP ${connect_addr} \
577		${test_linkfail} ${addr_nr_ns1} ${addr_nr_ns2} ${speed} ${bkup}
578	lret=$?
579}
580
581dump_stats()
582{
583	echo Server ns stats
584	ip netns exec $ns1 nstat -as | grep Tcp
585	echo Client ns stats
586	ip netns exec $ns2 nstat -as | grep Tcp
587}
588
589chk_csum_nr()
590{
591	local msg=${1:-""}
592	local count
593	local dump_stats
594
595	if [ ! -z "$msg" ]; then
596		printf "%02u" "$TEST_COUNT"
597	else
598		echo -n "  "
599	fi
600	printf " %-36s %s" "$msg" "sum"
601	count=`ip netns exec $ns1 nstat -as | grep MPTcpExtDataCsumErr | awk '{print $2}'`
602	[ -z "$count" ] && count=0
603	if [ "$count" != 0 ]; then
604		echo "[fail] got $count data checksum error[s] expected 0"
605		ret=1
606		dump_stats=1
607	else
608		echo -n "[ ok ]"
609	fi
610	echo -n " - csum  "
611	count=`ip netns exec $ns2 nstat -as | grep MPTcpExtDataCsumErr | awk '{print $2}'`
612	[ -z "$count" ] && count=0
613	if [ "$count" != 0 ]; then
614		echo "[fail] got $count data checksum error[s] expected 0"
615		ret=1
616		dump_stats=1
617	else
618		echo "[ ok ]"
619	fi
620	[ "${dump_stats}" = 1 ] && dump_stats
621}
622
623chk_fail_nr()
624{
625	local mp_fail_nr_tx=$1
626	local mp_fail_nr_rx=$2
627	local count
628	local dump_stats
629
630	printf "%-39s %s" " " "ftx"
631	count=`ip netns exec $ns1 nstat -as | grep MPTcpExtMPFailTx | awk '{print $2}'`
632	[ -z "$count" ] && count=0
633	if [ "$count" != "$mp_fail_nr_tx" ]; then
634		echo "[fail] got $count MP_FAIL[s] TX expected $mp_fail_nr_tx"
635		ret=1
636		dump_stats=1
637	else
638		echo -n "[ ok ]"
639	fi
640
641	echo -n " - frx   "
642	count=`ip netns exec $ns2 nstat -as | grep MPTcpExtMPFailRx | awk '{print $2}'`
643	[ -z "$count" ] && count=0
644	if [ "$count" != "$mp_fail_nr_rx" ]; then
645		echo "[fail] got $count MP_FAIL[s] RX expected $mp_fail_nr_rx"
646		ret=1
647		dump_stats=1
648	else
649		echo "[ ok ]"
650	fi
651
652	[ "${dump_stats}" = 1 ] && dump_stats
653}
654
655chk_join_nr()
656{
657	local msg="$1"
658	local syn_nr=$2
659	local syn_ack_nr=$3
660	local ack_nr=$4
661	local count
662	local dump_stats
663	local with_cookie
664
665	printf "%02u %-36s %s" "$TEST_COUNT" "$msg" "syn"
666	count=`ip netns exec $ns1 nstat -as | grep MPTcpExtMPJoinSynRx | awk '{print $2}'`
667	[ -z "$count" ] && count=0
668	if [ "$count" != "$syn_nr" ]; then
669		echo "[fail] got $count JOIN[s] syn expected $syn_nr"
670		ret=1
671		dump_stats=1
672	else
673		echo -n "[ ok ]"
674	fi
675
676	echo -n " - synack"
677	with_cookie=`ip netns exec $ns2 sysctl -n net.ipv4.tcp_syncookies`
678	count=`ip netns exec $ns2 nstat -as | grep MPTcpExtMPJoinSynAckRx | awk '{print $2}'`
679	[ -z "$count" ] && count=0
680	if [ "$count" != "$syn_ack_nr" ]; then
681		# simult connections exceeding the limit with cookie enabled could go up to
682		# synack validation as the conn limit can be enforced reliably only after
683		# the subflow creation
684		if [ "$with_cookie" = 2 ] && [ "$count" -gt "$syn_ack_nr" ] && [ "$count" -le "$syn_nr" ]; then
685			echo -n "[ ok ]"
686		else
687			echo "[fail] got $count JOIN[s] synack expected $syn_ack_nr"
688			ret=1
689			dump_stats=1
690		fi
691	else
692		echo -n "[ ok ]"
693	fi
694
695	echo -n " - ack"
696	count=`ip netns exec $ns1 nstat -as | grep MPTcpExtMPJoinAckRx | awk '{print $2}'`
697	[ -z "$count" ] && count=0
698	if [ "$count" != "$ack_nr" ]; then
699		echo "[fail] got $count JOIN[s] ack expected $ack_nr"
700		ret=1
701		dump_stats=1
702	else
703		echo "[ ok ]"
704	fi
705	[ "${dump_stats}" = 1 ] && dump_stats
706	if [ $checksum -eq 1 ]; then
707		chk_csum_nr
708		chk_fail_nr 0 0
709	fi
710}
711
712# a negative value for 'stale_max' means no upper bound:
713# for bidirectional transfer, if one peer sleep for a while
714# - as these tests do - we can have a quite high number of
715# stale/recover conversions, proportional to
716# sleep duration/ MPTCP-level RTX interval.
717chk_stale_nr()
718{
719	local ns=$1
720	local stale_min=$2
721	local stale_max=$3
722	local stale_delta=$4
723	local dump_stats
724	local stale_nr
725	local recover_nr
726
727	printf "%-39s %-18s" " " "stale"
728	stale_nr=`ip netns exec $ns nstat -as | grep MPTcpExtSubflowStale | awk '{print $2}'`
729	[ -z "$stale_nr" ] && stale_nr=0
730	recover_nr=`ip netns exec $ns nstat -as | grep MPTcpExtSubflowRecover | awk '{print $2}'`
731	[ -z "$recover_nr" ] && recover_nr=0
732
733	if [ $stale_nr -lt $stale_min ] ||
734	   [ $stale_max -gt 0 -a $stale_nr -gt $stale_max ] ||
735	   [ $((stale_nr - $recover_nr)) -ne $stale_delta ]; then
736		echo "[fail] got $stale_nr stale[s] $recover_nr recover[s], " \
737		     " expected stale in range [$stale_min..$stale_max]," \
738		     " stale-recover delta $stale_delta "
739		ret=1
740		dump_stats=1
741	else
742		echo "[ ok ]"
743	fi
744
745	if [ "${dump_stats}" = 1 ]; then
746		echo $ns stats
747		ip netns exec $ns ip -s link show
748		ip netns exec $ns nstat -as | grep MPTcp
749	fi
750}
751
752chk_add_nr()
753{
754	local add_nr=$1
755	local echo_nr=$2
756	local port_nr=${3:-0}
757	local syn_nr=${4:-$port_nr}
758	local syn_ack_nr=${5:-$port_nr}
759	local ack_nr=${6:-$port_nr}
760	local mis_syn_nr=${7:-0}
761	local mis_ack_nr=${8:-0}
762	local count
763	local dump_stats
764	local timeout
765
766	timeout=`ip netns exec $ns1 sysctl -n net.mptcp.add_addr_timeout`
767
768	printf "%-39s %s" " " "add"
769	count=`ip netns exec $ns2 nstat -as MPTcpExtAddAddr | grep MPTcpExtAddAddr | awk '{print $2}'`
770	[ -z "$count" ] && count=0
771
772	# if the test configured a short timeout tolerate greater then expected
773	# add addrs options, due to retransmissions
774	if [ "$count" != "$add_nr" ] && [ "$timeout" -gt 1 -o "$count" -lt "$add_nr" ]; then
775		echo "[fail] got $count ADD_ADDR[s] expected $add_nr"
776		ret=1
777		dump_stats=1
778	else
779		echo -n "[ ok ]"
780	fi
781
782	echo -n " - echo  "
783	count=`ip netns exec $ns1 nstat -as | grep MPTcpExtEchoAdd | awk '{print $2}'`
784	[ -z "$count" ] && count=0
785	if [ "$count" != "$echo_nr" ]; then
786		echo "[fail] got $count ADD_ADDR echo[s] expected $echo_nr"
787		ret=1
788		dump_stats=1
789	else
790		echo -n "[ ok ]"
791	fi
792
793	if [ $port_nr -gt 0 ]; then
794		echo -n " - pt "
795		count=`ip netns exec $ns2 nstat -as | grep MPTcpExtPortAdd | awk '{print $2}'`
796		[ -z "$count" ] && count=0
797		if [ "$count" != "$port_nr" ]; then
798			echo "[fail] got $count ADD_ADDR[s] with a port-number expected $port_nr"
799			ret=1
800			dump_stats=1
801		else
802			echo "[ ok ]"
803		fi
804
805		printf "%-39s %s" " " "syn"
806		count=`ip netns exec $ns1 nstat -as | grep MPTcpExtMPJoinPortSynRx |
807			awk '{print $2}'`
808		[ -z "$count" ] && count=0
809		if [ "$count" != "$syn_nr" ]; then
810			echo "[fail] got $count JOIN[s] syn with a different \
811				port-number expected $syn_nr"
812			ret=1
813			dump_stats=1
814		else
815			echo -n "[ ok ]"
816		fi
817
818		echo -n " - synack"
819		count=`ip netns exec $ns2 nstat -as | grep MPTcpExtMPJoinPortSynAckRx |
820			awk '{print $2}'`
821		[ -z "$count" ] && count=0
822		if [ "$count" != "$syn_ack_nr" ]; then
823			echo "[fail] got $count JOIN[s] synack with a different \
824				port-number expected $syn_ack_nr"
825			ret=1
826			dump_stats=1
827		else
828			echo -n "[ ok ]"
829		fi
830
831		echo -n " - ack"
832		count=`ip netns exec $ns1 nstat -as | grep MPTcpExtMPJoinPortAckRx |
833			awk '{print $2}'`
834		[ -z "$count" ] && count=0
835		if [ "$count" != "$ack_nr" ]; then
836			echo "[fail] got $count JOIN[s] ack with a different \
837				port-number expected $ack_nr"
838			ret=1
839			dump_stats=1
840		else
841			echo "[ ok ]"
842		fi
843
844		printf "%-39s %s" " " "syn"
845		count=`ip netns exec $ns1 nstat -as | grep MPTcpExtMismatchPortSynRx |
846			awk '{print $2}'`
847		[ -z "$count" ] && count=0
848		if [ "$count" != "$mis_syn_nr" ]; then
849			echo "[fail] got $count JOIN[s] syn with a mismatched \
850				port-number expected $mis_syn_nr"
851			ret=1
852			dump_stats=1
853		else
854			echo -n "[ ok ]"
855		fi
856
857		echo -n " - ack   "
858		count=`ip netns exec $ns1 nstat -as | grep MPTcpExtMismatchPortAckRx |
859			awk '{print $2}'`
860		[ -z "$count" ] && count=0
861		if [ "$count" != "$mis_ack_nr" ]; then
862			echo "[fail] got $count JOIN[s] ack with a mismatched \
863				port-number expected $mis_ack_nr"
864			ret=1
865			dump_stats=1
866		else
867			echo "[ ok ]"
868		fi
869	else
870		echo ""
871	fi
872
873	[ "${dump_stats}" = 1 ] && dump_stats
874}
875
876chk_rm_nr()
877{
878	local rm_addr_nr=$1
879	local rm_subflow_nr=$2
880	local invert=${3:-""}
881	local count
882	local dump_stats
883	local addr_ns
884	local subflow_ns
885
886	if [ -z $invert ]; then
887		addr_ns=$ns1
888		subflow_ns=$ns2
889	elif [ $invert = "invert" ]; then
890		addr_ns=$ns2
891		subflow_ns=$ns1
892	fi
893
894	printf "%-39s %s" " " "rm "
895	count=`ip netns exec $addr_ns nstat -as | grep MPTcpExtRmAddr | awk '{print $2}'`
896	[ -z "$count" ] && count=0
897	if [ "$count" != "$rm_addr_nr" ]; then
898		echo "[fail] got $count RM_ADDR[s] expected $rm_addr_nr"
899		ret=1
900		dump_stats=1
901	else
902		echo -n "[ ok ]"
903	fi
904
905	echo -n " - sf    "
906	count=`ip netns exec $subflow_ns nstat -as | grep MPTcpExtRmSubflow | awk '{print $2}'`
907	[ -z "$count" ] && count=0
908	if [ "$count" != "$rm_subflow_nr" ]; then
909		echo "[fail] got $count RM_SUBFLOW[s] expected $rm_subflow_nr"
910		ret=1
911		dump_stats=1
912	else
913		echo "[ ok ]"
914	fi
915
916	[ "${dump_stats}" = 1 ] && dump_stats
917}
918
919chk_prio_nr()
920{
921	local mp_prio_nr_tx=$1
922	local mp_prio_nr_rx=$2
923	local count
924	local dump_stats
925
926	printf "%-39s %s" " " "ptx"
927	count=`ip netns exec $ns1 nstat -as | grep MPTcpExtMPPrioTx | awk '{print $2}'`
928	[ -z "$count" ] && count=0
929	if [ "$count" != "$mp_prio_nr_tx" ]; then
930		echo "[fail] got $count MP_PRIO[s] TX expected $mp_prio_nr_tx"
931		ret=1
932		dump_stats=1
933	else
934		echo -n "[ ok ]"
935	fi
936
937	echo -n " - prx   "
938	count=`ip netns exec $ns1 nstat -as | grep MPTcpExtMPPrioRx | awk '{print $2}'`
939	[ -z "$count" ] && count=0
940	if [ "$count" != "$mp_prio_nr_rx" ]; then
941		echo "[fail] got $count MP_PRIO[s] RX expected $mp_prio_nr_rx"
942		ret=1
943		dump_stats=1
944	else
945		echo "[ ok ]"
946	fi
947
948	[ "${dump_stats}" = 1 ] && dump_stats
949}
950
951chk_link_usage()
952{
953	local ns=$1
954	local link=$2
955	local out=$3
956	local expected_rate=$4
957	local tx_link=`ip netns exec $ns cat /sys/class/net/$link/statistics/tx_bytes`
958	local tx_total=`ls -l $out | awk '{print $5}'`
959	local tx_rate=$((tx_link * 100 / $tx_total))
960	local tolerance=5
961
962	printf "%-39s %-18s" " " "link usage"
963	if [ $tx_rate -lt $((expected_rate - $tolerance)) -o \
964	     $tx_rate -gt $((expected_rate + $tolerance)) ]; then
965		echo "[fail] got $tx_rate% usage, expected $expected_rate%"
966		ret=1
967	else
968		echo "[ ok ]"
969	fi
970}
971
972wait_for_tw()
973{
974	local timeout_ms=$((timeout_poll * 1000))
975	local time=0
976	local ns=$1
977
978	while [ $time -lt $timeout_ms ]; do
979		local cnt=$(ip netns exec $ns nstat -as TcpAttemptFails | grep TcpAttemptFails | awk '{print $2}')
980
981		[ "$cnt" = 1 ] && return 1
982		time=$((time + 100))
983		sleep 0.1
984	done
985	return 1
986}
987
988subflows_tests()
989{
990	reset
991	run_tests $ns1 $ns2 10.0.1.1
992	chk_join_nr "no JOIN" "0" "0" "0"
993
994	# subflow limited by client
995	reset
996	ip netns exec $ns1 ./pm_nl_ctl limits 0 0
997	ip netns exec $ns2 ./pm_nl_ctl limits 0 0
998	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow
999	run_tests $ns1 $ns2 10.0.1.1
1000	chk_join_nr "single subflow, limited by client" 0 0 0
1001
1002	# subflow limited by server
1003	reset
1004	ip netns exec $ns1 ./pm_nl_ctl limits 0 0
1005	ip netns exec $ns2 ./pm_nl_ctl limits 0 1
1006	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow
1007	run_tests $ns1 $ns2 10.0.1.1
1008	chk_join_nr "single subflow, limited by server" 1 1 0
1009
1010	# subflow
1011	reset
1012	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1013	ip netns exec $ns2 ./pm_nl_ctl limits 0 1
1014	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow
1015	run_tests $ns1 $ns2 10.0.1.1
1016	chk_join_nr "single subflow" 1 1 1
1017
1018	# multiple subflows
1019	reset
1020	ip netns exec $ns1 ./pm_nl_ctl limits 0 2
1021	ip netns exec $ns2 ./pm_nl_ctl limits 0 2
1022	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow
1023	ip netns exec $ns2 ./pm_nl_ctl add 10.0.2.2 flags subflow
1024	run_tests $ns1 $ns2 10.0.1.1
1025	chk_join_nr "multiple subflows" 2 2 2
1026
1027	# multiple subflows limited by server
1028	reset
1029	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1030	ip netns exec $ns2 ./pm_nl_ctl limits 0 2
1031	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow
1032	ip netns exec $ns2 ./pm_nl_ctl add 10.0.2.2 flags subflow
1033	run_tests $ns1 $ns2 10.0.1.1
1034	chk_join_nr "multiple subflows, limited by server" 2 2 1
1035
1036	# single subflow, dev
1037	reset
1038	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1039	ip netns exec $ns2 ./pm_nl_ctl limits 0 1
1040	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow dev ns2eth3
1041	run_tests $ns1 $ns2 10.0.1.1
1042	chk_join_nr "single subflow, dev" 1 1 1
1043}
1044
1045subflows_error_tests()
1046{
1047	# If a single subflow is configured, and matches the MPC src
1048	# address, no additional subflow should be created
1049	reset
1050	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1051	ip netns exec $ns2 ./pm_nl_ctl limits 0 1
1052	ip netns exec $ns2 ./pm_nl_ctl add 10.0.1.2 flags subflow
1053	run_tests $ns1 $ns2 10.0.1.1 0 0 0 slow
1054	chk_join_nr "no MPC reuse with single endpoint" 0 0 0
1055
1056	# multiple subflows, with subflow creation error
1057	reset
1058	ip netns exec $ns1 ./pm_nl_ctl limits 0 2
1059	ip netns exec $ns2 ./pm_nl_ctl limits 0 2
1060	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow
1061	ip netns exec $ns2 ./pm_nl_ctl add 10.0.2.2 flags subflow
1062	ip netns exec $ns1 iptables -A INPUT -s 10.0.3.2 -p tcp -j REJECT
1063	run_tests $ns1 $ns2 10.0.1.1 0 0 0 slow
1064	chk_join_nr "multi subflows, with failing subflow" 1 1 1
1065
1066	# multiple subflows, with subflow timeout on MPJ
1067	reset
1068	ip netns exec $ns1 ./pm_nl_ctl limits 0 2
1069	ip netns exec $ns2 ./pm_nl_ctl limits 0 2
1070	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow
1071	ip netns exec $ns2 ./pm_nl_ctl add 10.0.2.2 flags subflow
1072	ip netns exec $ns1 iptables -A INPUT -s 10.0.3.2 -p tcp -j DROP
1073	run_tests $ns1 $ns2 10.0.1.1 0 0 0 slow
1074	chk_join_nr "multi subflows, with subflow timeout" 1 1 1
1075
1076	# multiple subflows, check that the endpoint corresponding to
1077	# closed subflow (due to reset) is not reused if additional
1078	# subflows are added later
1079	reset
1080	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1081	ip netns exec $ns2 ./pm_nl_ctl limits 0 1
1082	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow
1083	ip netns exec $ns1 iptables -A INPUT -s 10.0.3.2 -p tcp -j REJECT
1084	run_tests $ns1 $ns2 10.0.1.1 0 0 0 slow &
1085
1086	# updates in the child shell do not have any effect here, we
1087	# need to bump the test counter for the above case
1088	TEST_COUNT=$((TEST_COUNT+1))
1089
1090	# mpj subflow will be in TW after the reset
1091	wait_for_tw $ns2
1092	ip netns exec $ns2 ./pm_nl_ctl add 10.0.2.2 flags subflow
1093	wait
1094
1095	# additional subflow could be created only if the PM select
1096	# the later endpoint, skipping the already used one
1097	chk_join_nr "multi subflows, fair usage on close" 1 1 1
1098}
1099
1100signal_address_tests()
1101{
1102	# add_address, unused
1103	reset
1104	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal
1105	run_tests $ns1 $ns2 10.0.1.1
1106	chk_join_nr "unused signal address" 0 0 0
1107	chk_add_nr 1 1
1108
1109	# accept and use add_addr
1110	reset
1111	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1112	ip netns exec $ns2 ./pm_nl_ctl limits 1 1
1113	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal
1114	run_tests $ns1 $ns2 10.0.1.1
1115	chk_join_nr "signal address" 1 1 1
1116	chk_add_nr 1 1
1117
1118	# accept and use add_addr with an additional subflow
1119	# note: signal address in server ns and local addresses in client ns must
1120	# belong to different subnets or one of the listed local address could be
1121	# used for 'add_addr' subflow
1122	reset
1123	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal
1124	ip netns exec $ns1 ./pm_nl_ctl limits 0 2
1125	ip netns exec $ns2 ./pm_nl_ctl limits 1 2
1126	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow
1127	run_tests $ns1 $ns2 10.0.1.1
1128	chk_join_nr "subflow and signal" 2 2 2
1129	chk_add_nr 1 1
1130
1131	# accept and use add_addr with additional subflows
1132	reset
1133	ip netns exec $ns1 ./pm_nl_ctl limits 0 3
1134	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal
1135	ip netns exec $ns2 ./pm_nl_ctl limits 1 3
1136	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow
1137	ip netns exec $ns2 ./pm_nl_ctl add 10.0.4.2 flags subflow
1138	run_tests $ns1 $ns2 10.0.1.1
1139	chk_join_nr "multiple subflows and signal" 3 3 3
1140	chk_add_nr 1 1
1141
1142	# signal addresses
1143	reset
1144	ip netns exec $ns1 ./pm_nl_ctl limits 3 3
1145	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal
1146	ip netns exec $ns1 ./pm_nl_ctl add 10.0.3.1 flags signal
1147	ip netns exec $ns1 ./pm_nl_ctl add 10.0.4.1 flags signal
1148	ip netns exec $ns2 ./pm_nl_ctl limits 3 3
1149	run_tests $ns1 $ns2 10.0.1.1
1150	chk_join_nr "signal addresses" 3 3 3
1151	chk_add_nr 3 3
1152
1153	# signal invalid addresses
1154	reset
1155	ip netns exec $ns1 ./pm_nl_ctl limits 3 3
1156	ip netns exec $ns1 ./pm_nl_ctl add 10.0.12.1 flags signal
1157	ip netns exec $ns1 ./pm_nl_ctl add 10.0.3.1 flags signal
1158	ip netns exec $ns1 ./pm_nl_ctl add 10.0.14.1 flags signal
1159	ip netns exec $ns2 ./pm_nl_ctl limits 3 3
1160	run_tests $ns1 $ns2 10.0.1.1
1161	chk_join_nr "signal invalid addresses" 1 1 1
1162	chk_add_nr 3 3
1163
1164	# signal addresses race test
1165	reset
1166	ip netns exec $ns1 ./pm_nl_ctl limits 4 4
1167	ip netns exec $ns2 ./pm_nl_ctl limits 4 4
1168	ip netns exec $ns1 ./pm_nl_ctl add 10.0.1.1 flags signal
1169	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal
1170	ip netns exec $ns1 ./pm_nl_ctl add 10.0.3.1 flags signal
1171	ip netns exec $ns1 ./pm_nl_ctl add 10.0.4.1 flags signal
1172	ip netns exec $ns2 ./pm_nl_ctl add 10.0.1.2 flags signal
1173	ip netns exec $ns2 ./pm_nl_ctl add 10.0.2.2 flags signal
1174	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags signal
1175	ip netns exec $ns2 ./pm_nl_ctl add 10.0.4.2 flags signal
1176
1177	# the peer could possibly miss some addr notification, allow retransmission
1178	ip netns exec $ns1 sysctl -q net.mptcp.add_addr_timeout=1
1179	run_tests $ns1 $ns2 10.0.1.1 0 0 0 slow
1180	chk_join_nr "signal addresses race test" 3 3 3
1181
1182	# the server will not signal the address terminating
1183	# the MPC subflow
1184	chk_add_nr 3 3
1185}
1186
1187link_failure_tests()
1188{
1189	# accept and use add_addr with additional subflows and link loss
1190	reset
1191
1192	# without any b/w limit each veth could spool the packets and get
1193	# them acked at xmit time, so that the corresponding subflow will
1194	# have almost always no outstanding pkts, the scheduler will pick
1195	# always the first subflow and we will have hard time testing
1196	# active backup and link switch-over.
1197	# Let's set some arbitrary (low) virtual link limits.
1198	init_shapers
1199	ip netns exec $ns1 ./pm_nl_ctl limits 0 3
1200	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 dev ns1eth2 flags signal
1201	ip netns exec $ns2 ./pm_nl_ctl limits 1 3
1202	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 dev ns2eth3 flags subflow
1203	ip netns exec $ns2 ./pm_nl_ctl add 10.0.4.2 dev ns2eth4 flags subflow
1204	run_tests $ns1 $ns2 10.0.1.1 1
1205	chk_join_nr "multiple flows, signal, link failure" 3 3 3
1206	chk_add_nr 1 1
1207	chk_stale_nr $ns2 1 5 1
1208
1209	# accept and use add_addr with additional subflows and link loss
1210	# for bidirectional transfer
1211	reset
1212	init_shapers
1213	ip netns exec $ns1 ./pm_nl_ctl limits 0 3
1214	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 dev ns1eth2 flags signal
1215	ip netns exec $ns2 ./pm_nl_ctl limits 1 3
1216	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 dev ns2eth3 flags subflow
1217	ip netns exec $ns2 ./pm_nl_ctl add 10.0.4.2 dev ns2eth4 flags subflow
1218	run_tests $ns1 $ns2 10.0.1.1 2
1219	chk_join_nr "multi flows, signal, bidi, link fail" 3 3 3
1220	chk_add_nr 1 1
1221	chk_stale_nr $ns2 1 -1 1
1222
1223	# 2 subflows plus 1 backup subflow with a lossy link, backup
1224	# will never be used
1225	reset
1226	init_shapers
1227	ip netns exec $ns1 ./pm_nl_ctl limits 0 2
1228	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 dev ns1eth2 flags signal
1229	ip netns exec $ns2 ./pm_nl_ctl limits 1 2
1230	export FAILING_LINKS="1"
1231	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 dev ns2eth3 flags subflow,backup
1232	run_tests $ns1 $ns2 10.0.1.1 1
1233	chk_join_nr "backup subflow unused, link failure" 2 2 2
1234	chk_add_nr 1 1
1235	chk_link_usage $ns2 ns2eth3 $cinsent 0
1236
1237	# 2 lossy links after half transfer, backup will get half of
1238	# the traffic
1239	reset
1240	init_shapers
1241	ip netns exec $ns1 ./pm_nl_ctl limits 0 2
1242	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 dev ns1eth2 flags signal
1243	ip netns exec $ns2 ./pm_nl_ctl limits 1 2
1244	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 dev ns2eth3 flags subflow,backup
1245	export FAILING_LINKS="1 2"
1246	run_tests $ns1 $ns2 10.0.1.1 1
1247	chk_join_nr "backup flow used, multi links fail" 2 2 2
1248	chk_add_nr 1 1
1249	chk_stale_nr $ns2 2 4 2
1250	chk_link_usage $ns2 ns2eth3 $cinsent 50
1251
1252	# use a backup subflow with the first subflow on a lossy link
1253	# for bidirectional transfer
1254	reset
1255	init_shapers
1256	ip netns exec $ns1 ./pm_nl_ctl limits 0 2
1257	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 dev ns1eth2 flags signal
1258	ip netns exec $ns2 ./pm_nl_ctl limits 1 3
1259	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 dev ns2eth3 flags subflow,backup
1260	run_tests $ns1 $ns2 10.0.1.1 2
1261	chk_join_nr "backup flow used, bidi, link failure" 2 2 2
1262	chk_add_nr 1 1
1263	chk_stale_nr $ns2 1 -1 2
1264	chk_link_usage $ns2 ns2eth3 $cinsent 50
1265}
1266
1267add_addr_timeout_tests()
1268{
1269	# add_addr timeout
1270	reset_with_add_addr_timeout
1271	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1272	ip netns exec $ns2 ./pm_nl_ctl limits 1 1
1273	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal
1274	run_tests $ns1 $ns2 10.0.1.1 0 0 0 slow
1275	chk_join_nr "signal address, ADD_ADDR timeout" 1 1 1
1276	chk_add_nr 4 0
1277
1278	# add_addr timeout IPv6
1279	reset_with_add_addr_timeout 6
1280	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1281	ip netns exec $ns2 ./pm_nl_ctl limits 1 1
1282	ip netns exec $ns1 ./pm_nl_ctl add dead:beef:2::1 flags signal
1283	run_tests $ns1 $ns2 dead:beef:1::1 0 0 0 slow
1284	chk_join_nr "signal address, ADD_ADDR6 timeout" 1 1 1
1285	chk_add_nr 4 0
1286
1287	# signal addresses timeout
1288	reset_with_add_addr_timeout
1289	ip netns exec $ns1 ./pm_nl_ctl limits 2 2
1290	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal
1291	ip netns exec $ns1 ./pm_nl_ctl add 10.0.3.1 flags signal
1292	ip netns exec $ns2 ./pm_nl_ctl limits 2 2
1293	run_tests $ns1 $ns2 10.0.1.1 0 0 0 least
1294	chk_join_nr "signal addresses, ADD_ADDR timeout" 2 2 2
1295	chk_add_nr 8 0
1296
1297	# signal invalid addresses timeout
1298	reset_with_add_addr_timeout
1299	ip netns exec $ns1 ./pm_nl_ctl limits 2 2
1300	ip netns exec $ns1 ./pm_nl_ctl add 10.0.12.1 flags signal
1301	ip netns exec $ns1 ./pm_nl_ctl add 10.0.3.1 flags signal
1302	ip netns exec $ns2 ./pm_nl_ctl limits 2 2
1303	run_tests $ns1 $ns2 10.0.1.1 0 0 0 least
1304	chk_join_nr "invalid address, ADD_ADDR timeout" 1 1 1
1305	chk_add_nr 8 0
1306}
1307
1308remove_tests()
1309{
1310	# single subflow, remove
1311	reset
1312	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1313	ip netns exec $ns2 ./pm_nl_ctl limits 0 1
1314	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow
1315	run_tests $ns1 $ns2 10.0.1.1 0 0 -1 slow
1316	chk_join_nr "remove single subflow" 1 1 1
1317	chk_rm_nr 1 1
1318
1319	# multiple subflows, remove
1320	reset
1321	ip netns exec $ns1 ./pm_nl_ctl limits 0 2
1322	ip netns exec $ns2 ./pm_nl_ctl limits 0 2
1323	ip netns exec $ns2 ./pm_nl_ctl add 10.0.2.2 flags subflow
1324	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow
1325	run_tests $ns1 $ns2 10.0.1.1 0 0 -2 slow
1326	chk_join_nr "remove multiple subflows" 2 2 2
1327	chk_rm_nr 2 2
1328
1329	# single address, remove
1330	reset
1331	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1332	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal
1333	ip netns exec $ns2 ./pm_nl_ctl limits 1 1
1334	run_tests $ns1 $ns2 10.0.1.1 0 -1 0 slow
1335	chk_join_nr "remove single address" 1 1 1
1336	chk_add_nr 1 1
1337	chk_rm_nr 1 1 invert
1338
1339	# subflow and signal, remove
1340	reset
1341	ip netns exec $ns1 ./pm_nl_ctl limits 0 2
1342	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal
1343	ip netns exec $ns2 ./pm_nl_ctl limits 1 2
1344	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow
1345	run_tests $ns1 $ns2 10.0.1.1 0 -1 -1 slow
1346	chk_join_nr "remove subflow and signal" 2 2 2
1347	chk_add_nr 1 1
1348	chk_rm_nr 1 1
1349
1350	# subflows and signal, remove
1351	reset
1352	ip netns exec $ns1 ./pm_nl_ctl limits 0 3
1353	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal
1354	ip netns exec $ns2 ./pm_nl_ctl limits 1 3
1355	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow
1356	ip netns exec $ns2 ./pm_nl_ctl add 10.0.4.2 flags subflow
1357	run_tests $ns1 $ns2 10.0.1.1 0 -1 -2 slow
1358	chk_join_nr "remove subflows and signal" 3 3 3
1359	chk_add_nr 1 1
1360	chk_rm_nr 2 2
1361
1362	# addresses remove
1363	reset
1364	ip netns exec $ns1 ./pm_nl_ctl limits 3 3
1365	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal id 250
1366	ip netns exec $ns1 ./pm_nl_ctl add 10.0.3.1 flags signal
1367	ip netns exec $ns1 ./pm_nl_ctl add 10.0.4.1 flags signal
1368	ip netns exec $ns2 ./pm_nl_ctl limits 3 3
1369	run_tests $ns1 $ns2 10.0.1.1 0 -3 0 slow
1370	chk_join_nr "remove addresses" 3 3 3
1371	chk_add_nr 3 3
1372	chk_rm_nr 3 3 invert
1373
1374	# invalid addresses remove
1375	reset
1376	ip netns exec $ns1 ./pm_nl_ctl limits 3 3
1377	ip netns exec $ns1 ./pm_nl_ctl add 10.0.12.1 flags signal
1378	ip netns exec $ns1 ./pm_nl_ctl add 10.0.3.1 flags signal
1379	ip netns exec $ns1 ./pm_nl_ctl add 10.0.14.1 flags signal
1380	ip netns exec $ns2 ./pm_nl_ctl limits 3 3
1381	run_tests $ns1 $ns2 10.0.1.1 0 -3 0 slow
1382	chk_join_nr "remove invalid addresses" 1 1 1
1383	chk_add_nr 3 3
1384	chk_rm_nr 3 1 invert
1385
1386	# subflows and signal, flush
1387	reset
1388	ip netns exec $ns1 ./pm_nl_ctl limits 0 3
1389	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal
1390	ip netns exec $ns2 ./pm_nl_ctl limits 1 3
1391	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow
1392	ip netns exec $ns2 ./pm_nl_ctl add 10.0.4.2 flags subflow
1393	run_tests $ns1 $ns2 10.0.1.1 0 -8 -8 slow
1394	chk_join_nr "flush subflows and signal" 3 3 3
1395	chk_add_nr 1 1
1396	chk_rm_nr 2 2
1397
1398	# subflows flush
1399	reset
1400	ip netns exec $ns1 ./pm_nl_ctl limits 3 3
1401	ip netns exec $ns2 ./pm_nl_ctl limits 3 3
1402	ip netns exec $ns2 ./pm_nl_ctl add 10.0.2.2 flags subflow id 150
1403	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow
1404	ip netns exec $ns2 ./pm_nl_ctl add 10.0.4.2 flags subflow
1405	run_tests $ns1 $ns2 10.0.1.1 0 -8 -8 slow
1406	chk_join_nr "flush subflows" 3 3 3
1407	chk_rm_nr 3 3
1408
1409	# addresses flush
1410	reset
1411	ip netns exec $ns1 ./pm_nl_ctl limits 3 3
1412	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal id 250
1413	ip netns exec $ns1 ./pm_nl_ctl add 10.0.3.1 flags signal
1414	ip netns exec $ns1 ./pm_nl_ctl add 10.0.4.1 flags signal
1415	ip netns exec $ns2 ./pm_nl_ctl limits 3 3
1416	run_tests $ns1 $ns2 10.0.1.1 0 -8 -8 slow
1417	chk_join_nr "flush addresses" 3 3 3
1418	chk_add_nr 3 3
1419	chk_rm_nr 3 3 invert
1420
1421	# invalid addresses flush
1422	reset
1423	ip netns exec $ns1 ./pm_nl_ctl limits 3 3
1424	ip netns exec $ns1 ./pm_nl_ctl add 10.0.12.1 flags signal
1425	ip netns exec $ns1 ./pm_nl_ctl add 10.0.3.1 flags signal
1426	ip netns exec $ns1 ./pm_nl_ctl add 10.0.14.1 flags signal
1427	ip netns exec $ns2 ./pm_nl_ctl limits 3 3
1428	run_tests $ns1 $ns2 10.0.1.1 0 -8 0 slow
1429	chk_join_nr "flush invalid addresses" 1 1 1
1430	chk_add_nr 3 3
1431	chk_rm_nr 3 1 invert
1432
1433	# remove id 0 subflow
1434	reset
1435	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1436	ip netns exec $ns2 ./pm_nl_ctl limits 0 1
1437	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow
1438	run_tests $ns1 $ns2 10.0.1.1 0 0 -9 slow
1439	chk_join_nr "remove id 0 subflow" 1 1 1
1440	chk_rm_nr 1 1
1441
1442	# remove id 0 address
1443	reset
1444	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1445	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal
1446	ip netns exec $ns2 ./pm_nl_ctl limits 1 1
1447	run_tests $ns1 $ns2 10.0.1.1 0 -9 0 slow
1448	chk_join_nr "remove id 0 address" 1 1 1
1449	chk_add_nr 1 1
1450	chk_rm_nr 1 1 invert
1451}
1452
1453add_tests()
1454{
1455	# add single subflow
1456	reset
1457	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1458	ip netns exec $ns2 ./pm_nl_ctl limits 0 1
1459	run_tests $ns1 $ns2 10.0.1.1 0 0 1 slow
1460	chk_join_nr "add single subflow" 1 1 1
1461
1462	# add signal address
1463	reset
1464	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1465	ip netns exec $ns2 ./pm_nl_ctl limits 1 1
1466	run_tests $ns1 $ns2 10.0.1.1 0 1 0 slow
1467	chk_join_nr "add signal address" 1 1 1
1468	chk_add_nr 1 1
1469
1470	# add multiple subflows
1471	reset
1472	ip netns exec $ns1 ./pm_nl_ctl limits 0 2
1473	ip netns exec $ns2 ./pm_nl_ctl limits 0 2
1474	run_tests $ns1 $ns2 10.0.1.1 0 0 2 slow
1475	chk_join_nr "add multiple subflows" 2 2 2
1476
1477	# add multiple subflows IPv6
1478	reset
1479	ip netns exec $ns1 ./pm_nl_ctl limits 0 2
1480	ip netns exec $ns2 ./pm_nl_ctl limits 0 2
1481	run_tests $ns1 $ns2 dead:beef:1::1 0 0 2 slow
1482	chk_join_nr "add multiple subflows IPv6" 2 2 2
1483
1484	# add multiple addresses IPv6
1485	reset
1486	ip netns exec $ns1 ./pm_nl_ctl limits 0 2
1487	ip netns exec $ns2 ./pm_nl_ctl limits 2 2
1488	run_tests $ns1 $ns2 dead:beef:1::1 0 2 0 slow
1489	chk_join_nr "add multiple addresses IPv6" 2 2 2
1490	chk_add_nr 2 2
1491}
1492
1493ipv6_tests()
1494{
1495	# subflow IPv6
1496	reset
1497	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1498	ip netns exec $ns2 ./pm_nl_ctl limits 0 1
1499	ip netns exec $ns2 ./pm_nl_ctl add dead:beef:3::2 dev ns2eth3 flags subflow
1500	run_tests $ns1 $ns2 dead:beef:1::1 0 0 0 slow
1501	chk_join_nr "single subflow IPv6" 1 1 1
1502
1503	# add_address, unused IPv6
1504	reset
1505	ip netns exec $ns1 ./pm_nl_ctl add dead:beef:2::1 flags signal
1506	run_tests $ns1 $ns2 dead:beef:1::1 0 0 0 slow
1507	chk_join_nr "unused signal address IPv6" 0 0 0
1508	chk_add_nr 1 1
1509
1510	# signal address IPv6
1511	reset
1512	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1513	ip netns exec $ns1 ./pm_nl_ctl add dead:beef:2::1 flags signal
1514	ip netns exec $ns2 ./pm_nl_ctl limits 1 1
1515	run_tests $ns1 $ns2 dead:beef:1::1 0 0 0 slow
1516	chk_join_nr "single address IPv6" 1 1 1
1517	chk_add_nr 1 1
1518
1519	# single address IPv6, remove
1520	reset
1521	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1522	ip netns exec $ns1 ./pm_nl_ctl add dead:beef:2::1 flags signal
1523	ip netns exec $ns2 ./pm_nl_ctl limits 1 1
1524	run_tests $ns1 $ns2 dead:beef:1::1 0 -1 0 slow
1525	chk_join_nr "remove single address IPv6" 1 1 1
1526	chk_add_nr 1 1
1527	chk_rm_nr 1 1 invert
1528
1529	# subflow and signal IPv6, remove
1530	reset
1531	ip netns exec $ns1 ./pm_nl_ctl limits 0 2
1532	ip netns exec $ns1 ./pm_nl_ctl add dead:beef:2::1 flags signal
1533	ip netns exec $ns2 ./pm_nl_ctl limits 1 2
1534	ip netns exec $ns2 ./pm_nl_ctl add dead:beef:3::2 dev ns2eth3 flags subflow
1535	run_tests $ns1 $ns2 dead:beef:1::1 0 -1 -1 slow
1536	chk_join_nr "remove subflow and signal IPv6" 2 2 2
1537	chk_add_nr 1 1
1538	chk_rm_nr 1 1
1539}
1540
1541v4mapped_tests()
1542{
1543	# subflow IPv4-mapped to IPv4-mapped
1544	reset
1545	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1546	ip netns exec $ns2 ./pm_nl_ctl limits 0 1
1547	ip netns exec $ns2 ./pm_nl_ctl add "::ffff:10.0.3.2" flags subflow
1548	run_tests $ns1 $ns2 "::ffff:10.0.1.1"
1549	chk_join_nr "single subflow IPv4-mapped" 1 1 1
1550
1551	# signal address IPv4-mapped with IPv4-mapped sk
1552	reset
1553	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1554	ip netns exec $ns2 ./pm_nl_ctl limits 1 1
1555	ip netns exec $ns1 ./pm_nl_ctl add "::ffff:10.0.2.1" flags signal
1556	run_tests $ns1 $ns2 "::ffff:10.0.1.1"
1557	chk_join_nr "signal address IPv4-mapped" 1 1 1
1558	chk_add_nr 1 1
1559
1560	# subflow v4-map-v6
1561	reset
1562	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1563	ip netns exec $ns2 ./pm_nl_ctl limits 0 1
1564	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow
1565	run_tests $ns1 $ns2 "::ffff:10.0.1.1"
1566	chk_join_nr "single subflow v4-map-v6" 1 1 1
1567
1568	# signal address v4-map-v6
1569	reset
1570	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1571	ip netns exec $ns2 ./pm_nl_ctl limits 1 1
1572	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal
1573	run_tests $ns1 $ns2 "::ffff:10.0.1.1"
1574	chk_join_nr "signal address v4-map-v6" 1 1 1
1575	chk_add_nr 1 1
1576
1577	# subflow v6-map-v4
1578	reset
1579	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1580	ip netns exec $ns2 ./pm_nl_ctl limits 0 1
1581	ip netns exec $ns2 ./pm_nl_ctl add "::ffff:10.0.3.2" flags subflow
1582	run_tests $ns1 $ns2 10.0.1.1
1583	chk_join_nr "single subflow v6-map-v4" 1 1 1
1584
1585	# signal address v6-map-v4
1586	reset
1587	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1588	ip netns exec $ns2 ./pm_nl_ctl limits 1 1
1589	ip netns exec $ns1 ./pm_nl_ctl add "::ffff:10.0.2.1" flags signal
1590	run_tests $ns1 $ns2 10.0.1.1
1591	chk_join_nr "signal address v6-map-v4" 1 1 1
1592	chk_add_nr 1 1
1593
1594	# no subflow IPv6 to v4 address
1595	reset
1596	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1597	ip netns exec $ns2 ./pm_nl_ctl limits 0 1
1598	ip netns exec $ns2 ./pm_nl_ctl add dead:beef:2::2 flags subflow
1599	run_tests $ns1 $ns2 10.0.1.1
1600	chk_join_nr "no JOIN with diff families v4-v6" 0 0 0
1601
1602	# no subflow IPv6 to v4 address even if v6 has a valid v4 at the end
1603	reset
1604	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1605	ip netns exec $ns2 ./pm_nl_ctl limits 0 1
1606	ip netns exec $ns2 ./pm_nl_ctl add dead:beef:2::10.0.3.2 flags subflow
1607	run_tests $ns1 $ns2 10.0.1.1
1608	chk_join_nr "no JOIN with diff families v4-v6-2" 0 0 0
1609
1610	# no subflow IPv4 to v6 address, no need to slow down too then
1611	reset
1612	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1613	ip netns exec $ns2 ./pm_nl_ctl limits 0 1
1614	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow
1615	run_tests $ns1 $ns2 dead:beef:1::1
1616	chk_join_nr "no JOIN with diff families v6-v4" 0 0 0
1617}
1618
1619backup_tests()
1620{
1621	# single subflow, backup
1622	reset
1623	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1624	ip netns exec $ns2 ./pm_nl_ctl limits 0 1
1625	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow,backup
1626	run_tests $ns1 $ns2 10.0.1.1 0 0 0 slow nobackup
1627	chk_join_nr "single subflow, backup" 1 1 1
1628	chk_prio_nr 0 1
1629
1630	# single address, backup
1631	reset
1632	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1633	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal
1634	ip netns exec $ns2 ./pm_nl_ctl limits 1 1
1635	run_tests $ns1 $ns2 10.0.1.1 0 0 0 slow backup
1636	chk_join_nr "single address, backup" 1 1 1
1637	chk_add_nr 1 1
1638	chk_prio_nr 1 0
1639}
1640
1641add_addr_ports_tests()
1642{
1643	# signal address with port
1644	reset
1645	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1646	ip netns exec $ns2 ./pm_nl_ctl limits 1 1
1647	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal port 10100
1648	run_tests $ns1 $ns2 10.0.1.1
1649	chk_join_nr "signal address with port" 1 1 1
1650	chk_add_nr 1 1 1
1651
1652	# subflow and signal with port
1653	reset
1654	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal port 10100
1655	ip netns exec $ns1 ./pm_nl_ctl limits 0 2
1656	ip netns exec $ns2 ./pm_nl_ctl limits 1 2
1657	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow
1658	run_tests $ns1 $ns2 10.0.1.1
1659	chk_join_nr "subflow and signal with port" 2 2 2
1660	chk_add_nr 1 1 1
1661
1662	# single address with port, remove
1663	reset
1664	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1665	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal port 10100
1666	ip netns exec $ns2 ./pm_nl_ctl limits 1 1
1667	run_tests $ns1 $ns2 10.0.1.1 0 -1 0 slow
1668	chk_join_nr "remove single address with port" 1 1 1
1669	chk_add_nr 1 1 1
1670	chk_rm_nr 1 1 invert
1671
1672	# subflow and signal with port, remove
1673	reset
1674	ip netns exec $ns1 ./pm_nl_ctl limits 0 2
1675	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal port 10100
1676	ip netns exec $ns2 ./pm_nl_ctl limits 1 2
1677	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow
1678	run_tests $ns1 $ns2 10.0.1.1 0 -1 -1 slow
1679	chk_join_nr "remove subflow and signal with port" 2 2 2
1680	chk_add_nr 1 1 1
1681	chk_rm_nr 1 1
1682
1683	# subflows and signal with port, flush
1684	reset
1685	ip netns exec $ns1 ./pm_nl_ctl limits 0 3
1686	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal port 10100
1687	ip netns exec $ns2 ./pm_nl_ctl limits 1 3
1688	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow
1689	ip netns exec $ns2 ./pm_nl_ctl add 10.0.4.2 flags subflow
1690	run_tests $ns1 $ns2 10.0.1.1 0 -8 -2 slow
1691	chk_join_nr "flush subflows and signal with port" 3 3 3
1692	chk_add_nr 1 1
1693	chk_rm_nr 2 2
1694
1695	# multiple addresses with port
1696	reset
1697	ip netns exec $ns1 ./pm_nl_ctl limits 2 2
1698	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal port 10100
1699	ip netns exec $ns1 ./pm_nl_ctl add 10.0.3.1 flags signal port 10100
1700	ip netns exec $ns2 ./pm_nl_ctl limits 2 2
1701	run_tests $ns1 $ns2 10.0.1.1
1702	chk_join_nr "multiple addresses with port" 2 2 2
1703	chk_add_nr 2 2 2
1704
1705	# multiple addresses with ports
1706	reset
1707	ip netns exec $ns1 ./pm_nl_ctl limits 2 2
1708	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal port 10100
1709	ip netns exec $ns1 ./pm_nl_ctl add 10.0.3.1 flags signal port 10101
1710	ip netns exec $ns2 ./pm_nl_ctl limits 2 2
1711	run_tests $ns1 $ns2 10.0.1.1
1712	chk_join_nr "multiple addresses with ports" 2 2 2
1713	chk_add_nr 2 2 2
1714}
1715
1716syncookies_tests()
1717{
1718	# single subflow, syncookies
1719	reset_with_cookies
1720	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1721	ip netns exec $ns2 ./pm_nl_ctl limits 0 1
1722	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow
1723	run_tests $ns1 $ns2 10.0.1.1
1724	chk_join_nr "single subflow with syn cookies" 1 1 1
1725
1726	# multiple subflows with syn cookies
1727	reset_with_cookies
1728	ip netns exec $ns1 ./pm_nl_ctl limits 0 2
1729	ip netns exec $ns2 ./pm_nl_ctl limits 0 2
1730	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow
1731	ip netns exec $ns2 ./pm_nl_ctl add 10.0.2.2 flags subflow
1732	run_tests $ns1 $ns2 10.0.1.1
1733	chk_join_nr "multiple subflows with syn cookies" 2 2 2
1734
1735	# multiple subflows limited by server
1736	reset_with_cookies
1737	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1738	ip netns exec $ns2 ./pm_nl_ctl limits 0 2
1739	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow
1740	ip netns exec $ns2 ./pm_nl_ctl add 10.0.2.2 flags subflow
1741	run_tests $ns1 $ns2 10.0.1.1
1742	chk_join_nr "subflows limited by server w cookies" 2 1 1
1743
1744	# test signal address with cookies
1745	reset_with_cookies
1746	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1747	ip netns exec $ns2 ./pm_nl_ctl limits 1 1
1748	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal
1749	run_tests $ns1 $ns2 10.0.1.1
1750	chk_join_nr "signal address with syn cookies" 1 1 1
1751	chk_add_nr 1 1
1752
1753	# test cookie with subflow and signal
1754	reset_with_cookies
1755	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal
1756	ip netns exec $ns1 ./pm_nl_ctl limits 0 2
1757	ip netns exec $ns2 ./pm_nl_ctl limits 1 2
1758	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow
1759	run_tests $ns1 $ns2 10.0.1.1
1760	chk_join_nr "subflow and signal w cookies" 2 2 2
1761	chk_add_nr 1 1
1762
1763	# accept and use add_addr with additional subflows
1764	reset_with_cookies
1765	ip netns exec $ns1 ./pm_nl_ctl limits 0 3
1766	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal
1767	ip netns exec $ns2 ./pm_nl_ctl limits 1 3
1768	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow
1769	ip netns exec $ns2 ./pm_nl_ctl add 10.0.4.2 flags subflow
1770	run_tests $ns1 $ns2 10.0.1.1
1771	chk_join_nr "subflows and signal w. cookies" 3 3 3
1772	chk_add_nr 1 1
1773}
1774
1775checksum_tests()
1776{
1777	# checksum test 0 0
1778	reset_with_checksum 0 0
1779	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1780	ip netns exec $ns2 ./pm_nl_ctl limits 0 1
1781	run_tests $ns1 $ns2 10.0.1.1
1782	chk_csum_nr "checksum test 0 0"
1783
1784	# checksum test 1 1
1785	reset_with_checksum 1 1
1786	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1787	ip netns exec $ns2 ./pm_nl_ctl limits 0 1
1788	run_tests $ns1 $ns2 10.0.1.1
1789	chk_csum_nr "checksum test 1 1"
1790
1791	# checksum test 0 1
1792	reset_with_checksum 0 1
1793	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1794	ip netns exec $ns2 ./pm_nl_ctl limits 0 1
1795	run_tests $ns1 $ns2 10.0.1.1
1796	chk_csum_nr "checksum test 0 1"
1797
1798	# checksum test 1 0
1799	reset_with_checksum 1 0
1800	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1801	ip netns exec $ns2 ./pm_nl_ctl limits 0 1
1802	run_tests $ns1 $ns2 10.0.1.1
1803	chk_csum_nr "checksum test 1 0"
1804}
1805
1806deny_join_id0_tests()
1807{
1808	# subflow allow join id0 ns1
1809	reset_with_allow_join_id0 1 0
1810	ip netns exec $ns1 ./pm_nl_ctl limits 1 1
1811	ip netns exec $ns2 ./pm_nl_ctl limits 1 1
1812	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow
1813	run_tests $ns1 $ns2 10.0.1.1
1814	chk_join_nr "single subflow allow join id0 ns1" 1 1 1
1815
1816	# subflow allow join id0 ns2
1817	reset_with_allow_join_id0 0 1
1818	ip netns exec $ns1 ./pm_nl_ctl limits 1 1
1819	ip netns exec $ns2 ./pm_nl_ctl limits 1 1
1820	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow
1821	run_tests $ns1 $ns2 10.0.1.1
1822	chk_join_nr "single subflow allow join id0 ns2" 0 0 0
1823
1824	# signal address allow join id0 ns1
1825	# ADD_ADDRs are not affected by allow_join_id0 value.
1826	reset_with_allow_join_id0 1 0
1827	ip netns exec $ns1 ./pm_nl_ctl limits 1 1
1828	ip netns exec $ns2 ./pm_nl_ctl limits 1 1
1829	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal
1830	run_tests $ns1 $ns2 10.0.1.1
1831	chk_join_nr "signal address allow join id0 ns1" 1 1 1
1832	chk_add_nr 1 1
1833
1834	# signal address allow join id0 ns2
1835	# ADD_ADDRs are not affected by allow_join_id0 value.
1836	reset_with_allow_join_id0 0 1
1837	ip netns exec $ns1 ./pm_nl_ctl limits 1 1
1838	ip netns exec $ns2 ./pm_nl_ctl limits 1 1
1839	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal
1840	run_tests $ns1 $ns2 10.0.1.1
1841	chk_join_nr "signal address allow join id0 ns2" 1 1 1
1842	chk_add_nr 1 1
1843
1844	# subflow and address allow join id0 ns1
1845	reset_with_allow_join_id0 1 0
1846	ip netns exec $ns1 ./pm_nl_ctl limits 2 2
1847	ip netns exec $ns2 ./pm_nl_ctl limits 2 2
1848	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal
1849	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow
1850	run_tests $ns1 $ns2 10.0.1.1
1851	chk_join_nr "subflow and address allow join id0 1" 2 2 2
1852
1853	# subflow and address allow join id0 ns2
1854	reset_with_allow_join_id0 0 1
1855	ip netns exec $ns1 ./pm_nl_ctl limits 2 2
1856	ip netns exec $ns2 ./pm_nl_ctl limits 2 2
1857	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal
1858	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow
1859	run_tests $ns1 $ns2 10.0.1.1
1860	chk_join_nr "subflow and address allow join id0 2" 1 1 1
1861}
1862
1863fullmesh_tests()
1864{
1865	# fullmesh 1
1866	# 2 fullmesh addrs in ns2, added before the connection,
1867	# 1 non-fullmesh addr in ns1, added during the connection.
1868	reset
1869	ip netns exec $ns1 ./pm_nl_ctl limits 0 4
1870	ip netns exec $ns2 ./pm_nl_ctl limits 1 4
1871	ip netns exec $ns2 ./pm_nl_ctl add 10.0.2.2 flags subflow,fullmesh
1872	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow,fullmesh
1873	run_tests $ns1 $ns2 10.0.1.1 0 1 0 slow
1874	chk_join_nr "fullmesh test 2x1" 4 4 4
1875	chk_add_nr 1 1
1876
1877	# fullmesh 2
1878	# 1 non-fullmesh addr in ns1, added before the connection,
1879	# 1 fullmesh addr in ns2, added during the connection.
1880	reset
1881	ip netns exec $ns1 ./pm_nl_ctl limits 1 3
1882	ip netns exec $ns2 ./pm_nl_ctl limits 1 3
1883	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal
1884	run_tests $ns1 $ns2 10.0.1.1 0 0 fullmesh_1 slow
1885	chk_join_nr "fullmesh test 1x1" 3 3 3
1886	chk_add_nr 1 1
1887
1888	# fullmesh 3
1889	# 1 non-fullmesh addr in ns1, added before the connection,
1890	# 2 fullmesh addrs in ns2, added during the connection.
1891	reset
1892	ip netns exec $ns1 ./pm_nl_ctl limits 2 5
1893	ip netns exec $ns2 ./pm_nl_ctl limits 1 5
1894	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal
1895	run_tests $ns1 $ns2 10.0.1.1 0 0 fullmesh_2 slow
1896	chk_join_nr "fullmesh test 1x2" 5 5 5
1897	chk_add_nr 1 1
1898
1899	# fullmesh 4
1900	# 1 non-fullmesh addr in ns1, added before the connection,
1901	# 2 fullmesh addrs in ns2, added during the connection,
1902	# limit max_subflows to 4.
1903	reset
1904	ip netns exec $ns1 ./pm_nl_ctl limits 2 4
1905	ip netns exec $ns2 ./pm_nl_ctl limits 1 4
1906	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal
1907	run_tests $ns1 $ns2 10.0.1.1 0 0 fullmesh_2 slow
1908	chk_join_nr "fullmesh test 1x2, limited" 4 4 4
1909	chk_add_nr 1 1
1910}
1911
1912all_tests()
1913{
1914	subflows_tests
1915	subflows_error_tests
1916	signal_address_tests
1917	link_failure_tests
1918	add_addr_timeout_tests
1919	remove_tests
1920	add_tests
1921	ipv6_tests
1922	v4mapped_tests
1923	backup_tests
1924	add_addr_ports_tests
1925	syncookies_tests
1926	checksum_tests
1927	deny_join_id0_tests
1928	fullmesh_tests
1929}
1930
1931usage()
1932{
1933	echo "mptcp_join usage:"
1934	echo "  -f subflows_tests"
1935	echo "  -e subflows_error_tests"
1936	echo "  -s signal_address_tests"
1937	echo "  -l link_failure_tests"
1938	echo "  -t add_addr_timeout_tests"
1939	echo "  -r remove_tests"
1940	echo "  -a add_tests"
1941	echo "  -6 ipv6_tests"
1942	echo "  -4 v4mapped_tests"
1943	echo "  -b backup_tests"
1944	echo "  -p add_addr_ports_tests"
1945	echo "  -k syncookies_tests"
1946	echo "  -S checksum_tests"
1947	echo "  -d deny_join_id0_tests"
1948	echo "  -m fullmesh_tests"
1949	echo "  -c capture pcap files"
1950	echo "  -C enable data checksum"
1951	echo "  -h help"
1952}
1953
1954sin=$(mktemp)
1955sout=$(mktemp)
1956cin=$(mktemp)
1957cinsent=$(mktemp)
1958cout=$(mktemp)
1959init
1960make_file "$cin" "client" 1
1961make_file "$sin" "server" 1
1962trap cleanup EXIT
1963
1964for arg in "$@"; do
1965	# check for "capture/checksum" args before launching tests
1966	if [[ "${arg}" =~ ^"-"[0-9a-zA-Z]*"c"[0-9a-zA-Z]*$ ]]; then
1967		capture=1
1968	fi
1969	if [[ "${arg}" =~ ^"-"[0-9a-zA-Z]*"C"[0-9a-zA-Z]*$ ]]; then
1970		checksum=1
1971	fi
1972
1973	# exception for the capture/checksum options, the rest means: a part of the tests
1974	if [ "${arg}" != "-c" ] && [ "${arg}" != "-C" ]; then
1975		do_all_tests=0
1976	fi
1977done
1978
1979if [ $do_all_tests -eq 1 ]; then
1980	all_tests
1981	exit $ret
1982fi
1983
1984while getopts 'fesltra64bpkdmchCS' opt; do
1985	case $opt in
1986		f)
1987			subflows_tests
1988			;;
1989		e)
1990			subflows_error_tests
1991			;;
1992		s)
1993			signal_address_tests
1994			;;
1995		l)
1996			link_failure_tests
1997			;;
1998		t)
1999			add_addr_timeout_tests
2000			;;
2001		r)
2002			remove_tests
2003			;;
2004		a)
2005			add_tests
2006			;;
2007		6)
2008			ipv6_tests
2009			;;
2010		4)
2011			v4mapped_tests
2012			;;
2013		b)
2014			backup_tests
2015			;;
2016		p)
2017			add_addr_ports_tests
2018			;;
2019		k)
2020			syncookies_tests
2021			;;
2022		S)
2023			checksum_tests
2024			;;
2025		d)
2026			deny_join_id0_tests
2027			;;
2028		m)
2029			fullmesh_tests
2030			;;
2031		c)
2032			;;
2033		C)
2034			;;
2035		h | *)
2036			usage
2037			;;
2038	esac
2039done
2040
2041exit $ret
2042