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