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 "$cin" "$cout"
26	rm -f "$sin" "$sout"
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=$((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	done
64
65	ip link add ns1eth1 netns "$ns1" type veth peer name ns2eth1 netns "$ns2"
66	ip link add ns1eth2 netns "$ns1" type veth peer name ns2eth2 netns "$ns2"
67	ip link add ns2eth3 netns "$ns2" type veth peer name ns3eth1 netns "$ns3"
68
69	ip -net "$ns1" addr add 10.0.1.1/24 dev ns1eth1
70	ip -net "$ns1" addr add dead:beef:1::1/64 dev ns1eth1 nodad
71	ip -net "$ns1" link set ns1eth1 up mtu 1500
72	ip -net "$ns1" route add default via 10.0.1.2
73	ip -net "$ns1" route add default via dead:beef:1::2
74
75	ip -net "$ns1" addr add 10.0.2.1/24 dev ns1eth2
76	ip -net "$ns1" addr add dead:beef:2::1/64 dev ns1eth2 nodad
77	ip -net "$ns1" link set ns1eth2 up mtu 1500
78	ip -net "$ns1" route add default via 10.0.2.2 metric 101
79	ip -net "$ns1" route add default via dead:beef:2::2 metric 101
80
81	ip netns exec "$ns1" ./pm_nl_ctl limits 1 1
82	ip netns exec "$ns1" ./pm_nl_ctl add 10.0.2.1 dev ns1eth2 flags subflow
83	ip netns exec "$ns1" sysctl -q net.ipv4.conf.all.rp_filter=0
84
85	ip -net "$ns2" addr add 10.0.1.2/24 dev ns2eth1
86	ip -net "$ns2" addr add dead:beef:1::2/64 dev ns2eth1 nodad
87	ip -net "$ns2" link set ns2eth1 up mtu 1500
88
89	ip -net "$ns2" addr add 10.0.2.2/24 dev ns2eth2
90	ip -net "$ns2" addr add dead:beef:2::2/64 dev ns2eth2 nodad
91	ip -net "$ns2" link set ns2eth2 up mtu 1500
92
93	ip -net "$ns2" addr add 10.0.3.2/24 dev ns2eth3
94	ip -net "$ns2" addr add dead:beef:3::2/64 dev ns2eth3 nodad
95	ip -net "$ns2" link set ns2eth3 up mtu 1500
96	ip netns exec "$ns2" sysctl -q net.ipv4.ip_forward=1
97	ip netns exec "$ns2" sysctl -q net.ipv6.conf.all.forwarding=1
98
99	ip -net "$ns3" addr add 10.0.3.3/24 dev ns3eth1
100	ip -net "$ns3" addr add dead:beef:3::3/64 dev ns3eth1 nodad
101	ip -net "$ns3" link set ns3eth1 up mtu 1500
102	ip -net "$ns3" route add default via 10.0.3.2
103	ip -net "$ns3" route add default via dead:beef:3::2
104
105	ip netns exec "$ns3" ./pm_nl_ctl limits 1 1
106}
107
108# $1: ns, $2: port
109wait_local_port_listen()
110{
111	local listener_ns="${1}"
112	local port="${2}"
113
114	local port_hex i
115
116	port_hex="$(printf "%04X" "${port}")"
117	for i in $(seq 10); do
118		ip netns exec "${listener_ns}" cat /proc/net/tcp* | \
119			awk "BEGIN {rc=1} {if (\$2 ~ /:${port_hex}\$/ && \$4 ~ /0A/) {rc=0; exit}} END {exit rc}" &&
120			break
121		sleep 0.1
122	done
123}
124
125do_transfer()
126{
127	local cin=$1
128	local sin=$2
129	local max_time=$3
130	local port
131	port=$((10000+$test_cnt))
132	test_cnt=$((test_cnt+1))
133
134	:> "$cout"
135	:> "$sout"
136	:> "$capout"
137
138	local addr_port
139	addr_port=$(printf "%s:%d" ${connect_addr} ${port})
140
141	if $capture; then
142		local capuser
143		if [ -z $SUDO_USER ] ; then
144			capuser=""
145		else
146			capuser="-Z $SUDO_USER"
147		fi
148
149		local capfile="${rndh}-${port}"
150		local capopt="-i any -s 65535 -B 32768 ${capuser}"
151
152		ip netns exec ${ns3}  tcpdump ${capopt} -w "${capfile}-listener.pcap"  >> "${capout}" 2>&1 &
153		local cappid_listener=$!
154
155		ip netns exec ${ns1} tcpdump ${capopt} -w "${capfile}-connector.pcap" >> "${capout}" 2>&1 &
156		local cappid_connector=$!
157
158		sleep 1
159	fi
160
161	timeout ${timeout_test} \
162		ip netns exec ${ns3} \
163			./mptcp_connect -jt ${timeout_poll} -l -p $port \
164				0.0.0.0 < "$sin" > "$sout" &
165	local spid=$!
166
167	wait_local_port_listen "${ns3}" "${port}"
168
169	local start
170	start=$(date +%s%3N)
171	timeout ${timeout_test} \
172		ip netns exec ${ns1} \
173			./mptcp_connect -jt ${timeout_poll} -p $port \
174				10.0.3.3 < "$cin" > "$cout" &
175	local cpid=$!
176
177	wait $cpid
178	local retc=$?
179	wait $spid
180	local rets=$?
181
182	local stop
183	stop=$(date +%s%3N)
184
185	if $capture; then
186		sleep 1
187		kill ${cappid_listener}
188		kill ${cappid_connector}
189	fi
190
191	local duration
192	duration=$((stop-start))
193
194	cmp $sin $cout > /dev/null 2>&1
195	local cmps=$?
196	cmp $cin $sout > /dev/null 2>&1
197	local cmpc=$?
198
199	printf "%16s" "$duration max $max_time "
200	if [ $retc -eq 0 ] && [ $rets -eq 0 ] && \
201	   [ $cmpc -eq 0 ] && [ $cmps -eq 0 ] && \
202	   [ $duration -lt $max_time ]; then
203		echo "[ OK ]"
204		cat "$capout"
205		return 0
206	fi
207
208	echo " [ fail ]"
209	echo "client exit code $retc, server $rets" 1>&2
210	echo -e "\nnetns ${ns3} socket stat for $port:" 1>&2
211	ip netns exec ${ns3} ss -nita 1>&2 -o "sport = :$port"
212	echo -e "\nnetns ${ns1} socket stat for $port:" 1>&2
213	ip netns exec ${ns1} ss -nita 1>&2 -o "dport = :$port"
214	ls -l $sin $cout
215	ls -l $cin $sout
216
217	cat "$capout"
218	return 1
219}
220
221run_test()
222{
223	local rate1=$1
224	local rate2=$2
225	local delay1=$3
226	local delay2=$4
227	local lret
228	local dev
229	shift 4
230	local msg=$*
231
232	[ $delay1 -gt 0 ] && delay1="delay $delay1" || delay1=""
233	[ $delay2 -gt 0 ] && delay2="delay $delay2" || delay2=""
234
235	for dev in ns1eth1 ns1eth2; do
236		tc -n $ns1 qdisc del dev $dev root >/dev/null 2>&1
237	done
238	for dev in ns2eth1 ns2eth2; do
239		tc -n $ns2 qdisc del dev $dev root >/dev/null 2>&1
240	done
241	tc -n $ns1 qdisc add dev ns1eth1 root netem rate ${rate1}mbit $delay1
242	tc -n $ns1 qdisc add dev ns1eth2 root netem rate ${rate2}mbit $delay2
243	tc -n $ns2 qdisc add dev ns2eth1 root netem rate ${rate1}mbit $delay1
244	tc -n $ns2 qdisc add dev ns2eth2 root netem rate ${rate2}mbit $delay2
245
246	# time is measure in ms
247	local time=$((size * 8 * 1000 / (( $rate1 + $rate2) * 1024 *1024) ))
248
249	# mptcp_connect will do some sleeps to allow the mp_join handshake
250	# completion
251	time=$((time + 1350))
252
253	printf "%-50s" "$msg"
254	do_transfer $small $large $((time * 11 / 10))
255	lret=$?
256	if [ $lret -ne 0 ]; then
257		ret=$lret
258		[ $bail -eq 0 ] || exit $ret
259	fi
260
261	printf "%-50s" "$msg - reverse direction"
262	do_transfer $large $small $((time * 11 / 10))
263	lret=$?
264	if [ $lret -ne 0 ]; then
265		ret=$lret
266		[ $bail -eq 0 ] || exit $ret
267	fi
268}
269
270while getopts "bcdh" option;do
271	case "$option" in
272	"h")
273		usage $0
274		exit 0
275		;;
276	"b")
277		bail=1
278		;;
279	"c")
280		capture=true
281		;;
282	"d")
283		set -x
284		;;
285	"?")
286		usage $0
287		exit 1
288		;;
289	esac
290done
291
292setup
293run_test 10 10 0 0 "balanced bwidth"
294run_test 10 10 1 50 "balanced bwidth with unbalanced delay"
295
296# we still need some additional infrastructure to pass the following test-cases
297run_test 30 10 0 0 "unbalanced bwidth"
298run_test 30 10 1 50 "unbalanced bwidth with unbalanced delay"
299run_test 30 10 50 1 "unbalanced bwidth with opposed, unbalanced delay"
300exit $ret
301