1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3
4rndh=$(printf %x $sec)-$(mktemp -u XXXXXX)
5ns1="ns1-$rndh"
6ns2="ns2-$rndh"
7ns3="ns3-$rndh"
8capture=false
9ksft_skip=4
10timeout_poll=30
11timeout_test=$((timeout_poll * 2 + 1))
12test_cnt=1
13ret=0
14bail=0
15
16usage() {
17	echo "Usage: $0 [ -b ] [ -c ] [ -d ]"
18	echo -e "\t-b: bail out after first error, otherwise runs al testcases"
19	echo -e "\t-c: capture packets for each test using tcpdump (default: no capture)"
20	echo -e "\t-d: debug this script"
21}
22
23cleanup()
24{
25	rm -f "$cout" "$sout"
26	rm -f "$large" "$small"
27	rm -f "$capout"
28
29	local netns
30	for netns in "$ns1" "$ns2" "$ns3";do
31		ip netns del $netns
32	done
33}
34
35ip -Version > /dev/null 2>&1
36if [ $? -ne 0 ];then
37	echo "SKIP: Could not run test without ip tool"
38	exit $ksft_skip
39fi
40
41#  "$ns1"              ns2                    ns3
42#     ns1eth1    ns2eth1   ns2eth3      ns3eth1
43#            netem
44#     ns1eth2    ns2eth2
45#            netem
46
47setup()
48{
49	large=$(mktemp)
50	small=$(mktemp)
51	sout=$(mktemp)
52	cout=$(mktemp)
53	capout=$(mktemp)
54	size=$((2 * 2048 * 4096))
55	dd if=/dev/zero of=$small bs=4096 count=20 >/dev/null 2>&1
56	dd if=/dev/zero of=$large bs=4096 count=$((size / 4096)) >/dev/null 2>&1
57
58	trap cleanup EXIT
59
60	for i in "$ns1" "$ns2" "$ns3";do
61		ip netns add $i || exit $ksft_skip
62		ip -net $i link set lo up
63		ip netns exec $i sysctl -q net.ipv4.conf.all.rp_filter=0
64		ip netns exec $i sysctl -q net.ipv4.conf.default.rp_filter=0
65	done
66
67	ip link add ns1eth1 netns "$ns1" type veth peer name ns2eth1 netns "$ns2"
68	ip link add ns1eth2 netns "$ns1" type veth peer name ns2eth2 netns "$ns2"
69	ip link add ns2eth3 netns "$ns2" type veth peer name ns3eth1 netns "$ns3"
70
71	ip -net "$ns1" addr add 10.0.1.1/24 dev ns1eth1
72	ip -net "$ns1" addr add dead:beef:1::1/64 dev ns1eth1 nodad
73	ip -net "$ns1" link set ns1eth1 up mtu 1500
74	ip -net "$ns1" route add default via 10.0.1.2
75	ip -net "$ns1" route add default via dead:beef:1::2
76
77	ip -net "$ns1" addr add 10.0.2.1/24 dev ns1eth2
78	ip -net "$ns1" addr add dead:beef:2::1/64 dev ns1eth2 nodad
79	ip -net "$ns1" link set ns1eth2 up mtu 1500
80	ip -net "$ns1" route add default via 10.0.2.2 metric 101
81	ip -net "$ns1" route add default via dead:beef:2::2 metric 101
82
83	ip netns exec "$ns1" ./pm_nl_ctl limits 1 1
84	ip netns exec "$ns1" ./pm_nl_ctl add 10.0.2.1 dev ns1eth2 flags subflow
85
86	ip -net "$ns2" addr add 10.0.1.2/24 dev ns2eth1
87	ip -net "$ns2" addr add dead:beef:1::2/64 dev ns2eth1 nodad
88	ip -net "$ns2" link set ns2eth1 up mtu 1500
89
90	ip -net "$ns2" addr add 10.0.2.2/24 dev ns2eth2
91	ip -net "$ns2" addr add dead:beef:2::2/64 dev ns2eth2 nodad
92	ip -net "$ns2" link set ns2eth2 up mtu 1500
93
94	ip -net "$ns2" addr add 10.0.3.2/24 dev ns2eth3
95	ip -net "$ns2" addr add dead:beef:3::2/64 dev ns2eth3 nodad
96	ip -net "$ns2" link set ns2eth3 up mtu 1500
97	ip netns exec "$ns2" sysctl -q net.ipv4.ip_forward=1
98	ip netns exec "$ns2" sysctl -q net.ipv6.conf.all.forwarding=1
99
100	ip -net "$ns3" addr add 10.0.3.3/24 dev ns3eth1
101	ip -net "$ns3" addr add dead:beef:3::3/64 dev ns3eth1 nodad
102	ip -net "$ns3" link set ns3eth1 up mtu 1500
103	ip -net "$ns3" route add default via 10.0.3.2
104	ip -net "$ns3" route add default via dead:beef:3::2
105
106	ip netns exec "$ns3" ./pm_nl_ctl limits 1 1
107}
108
109# $1: ns, $2: port
110wait_local_port_listen()
111{
112	local listener_ns="${1}"
113	local port="${2}"
114
115	local port_hex i
116
117	port_hex="$(printf "%04X" "${port}")"
118	for i in $(seq 10); do
119		ip netns exec "${listener_ns}" cat /proc/net/tcp* | \
120			awk "BEGIN {rc=1} {if (\$2 ~ /:${port_hex}\$/ && \$4 ~ /0A/) {rc=0; exit}} END {exit rc}" &&
121			break
122		sleep 0.1
123	done
124}
125
126do_transfer()
127{
128	local cin=$1
129	local sin=$2
130	local max_time=$3
131	local port
132	port=$((10000+$test_cnt))
133	test_cnt=$((test_cnt+1))
134
135	:> "$cout"
136	:> "$sout"
137	:> "$capout"
138
139	local addr_port
140	addr_port=$(printf "%s:%d" ${connect_addr} ${port})
141
142	if $capture; then
143		local capuser
144		if [ -z $SUDO_USER ] ; then
145			capuser=""
146		else
147			capuser="-Z $SUDO_USER"
148		fi
149
150		local capfile="${rndh}-${port}"
151		local capopt="-i any -s 65535 -B 32768 ${capuser}"
152
153		ip netns exec ${ns3}  tcpdump ${capopt} -w "${capfile}-listener.pcap"  >> "${capout}" 2>&1 &
154		local cappid_listener=$!
155
156		ip netns exec ${ns1} tcpdump ${capopt} -w "${capfile}-connector.pcap" >> "${capout}" 2>&1 &
157		local cappid_connector=$!
158
159		sleep 1
160	fi
161
162	timeout ${timeout_test} \
163		ip netns exec ${ns3} \
164			./mptcp_connect -jt ${timeout_poll} -l -p $port -T $time \
165				0.0.0.0 < "$sin" > "$sout" &
166	local spid=$!
167
168	wait_local_port_listen "${ns3}" "${port}"
169
170	timeout ${timeout_test} \
171		ip netns exec ${ns1} \
172			./mptcp_connect -jt ${timeout_poll} -p $port -T $time \
173				10.0.3.3 < "$cin" > "$cout" &
174	local cpid=$!
175
176	wait $cpid
177	local retc=$?
178	wait $spid
179	local rets=$?
180
181	if $capture; then
182		sleep 1
183		kill ${cappid_listener}
184		kill ${cappid_connector}
185	fi
186
187	cmp $sin $cout > /dev/null 2>&1
188	local cmps=$?
189	cmp $cin $sout > /dev/null 2>&1
190	local cmpc=$?
191
192	printf "%-16s" " max $max_time "
193	if [ $retc -eq 0 ] && [ $rets -eq 0 ] && \
194	   [ $cmpc -eq 0 ] && [ $cmps -eq 0 ]; then
195		echo "[ OK ]"
196		cat "$capout"
197		return 0
198	fi
199
200	echo " [ fail ]"
201	echo "client exit code $retc, server $rets" 1>&2
202	echo -e "\nnetns ${ns3} socket stat for $port:" 1>&2
203	ip netns exec ${ns3} ss -nita 1>&2 -o "sport = :$port"
204	echo -e "\nnetns ${ns1} socket stat for $port:" 1>&2
205	ip netns exec ${ns1} ss -nita 1>&2 -o "dport = :$port"
206	ls -l $sin $cout
207	ls -l $cin $sout
208
209	cat "$capout"
210	return 1
211}
212
213run_test()
214{
215	local rate1=$1
216	local rate2=$2
217	local delay1=$3
218	local delay2=$4
219	local lret
220	local dev
221	shift 4
222	local msg=$*
223
224	[ $delay1 -gt 0 ] && delay1="delay $delay1" || delay1=""
225	[ $delay2 -gt 0 ] && delay2="delay $delay2" || delay2=""
226
227	for dev in ns1eth1 ns1eth2; do
228		tc -n $ns1 qdisc del dev $dev root >/dev/null 2>&1
229	done
230	for dev in ns2eth1 ns2eth2; do
231		tc -n $ns2 qdisc del dev $dev root >/dev/null 2>&1
232	done
233	tc -n $ns1 qdisc add dev ns1eth1 root netem rate ${rate1}mbit $delay1
234	tc -n $ns1 qdisc add dev ns1eth2 root netem rate ${rate2}mbit $delay2
235	tc -n $ns2 qdisc add dev ns2eth1 root netem rate ${rate1}mbit $delay1
236	tc -n $ns2 qdisc add dev ns2eth2 root netem rate ${rate2}mbit $delay2
237
238	# time is measured in ms, account for transfer size, affegated link speed
239	# and header overhead (10%)
240	local time=$((size * 8 * 1000 * 10 / (( $rate1 + $rate2) * 1024 *1024 * 9) ))
241
242	# mptcp_connect will do some sleeps to allow the mp_join handshake
243	# completion (see mptcp_connect): 200ms on each side, add some slack
244	time=$((time + 450))
245
246	printf "%-60s" "$msg"
247	do_transfer $small $large $time
248	lret=$?
249	if [ $lret -ne 0 ]; then
250		ret=$lret
251		[ $bail -eq 0 ] || exit $ret
252	fi
253
254	printf "%-60s" "$msg - reverse direction"
255	do_transfer $large $small $time
256	lret=$?
257	if [ $lret -ne 0 ]; then
258		ret=$lret
259		[ $bail -eq 0 ] || exit $ret
260	fi
261}
262
263while getopts "bcdh" option;do
264	case "$option" in
265	"h")
266		usage $0
267		exit 0
268		;;
269	"b")
270		bail=1
271		;;
272	"c")
273		capture=true
274		;;
275	"d")
276		set -x
277		;;
278	"?")
279		usage $0
280		exit 1
281		;;
282	esac
283done
284
285setup
286run_test 10 10 0 0 "balanced bwidth"
287run_test 10 10 1 50 "balanced bwidth with unbalanced delay"
288
289# we still need some additional infrastructure to pass the following test-cases
290run_test 30 10 0 0 "unbalanced bwidth"
291run_test 30 10 1 50 "unbalanced bwidth with unbalanced delay"
292run_test 30 10 50 1 "unbalanced bwidth with opposed, unbalanced delay"
293exit $ret
294