1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3
4rndh=$(printf %x $sec)-$(mktemp -u XXXXXX)
5ns="ns1-$rndh"
6ksft_skip=4
7test_cnt=1
8timeout_poll=100
9timeout_test=$((timeout_poll * 2 + 1))
10ret=0
11
12flush_pids()
13{
14	# mptcp_connect in join mode will sleep a bit before completing,
15	# give it some time
16	sleep 1.1
17
18	ip netns pids "${ns}" | xargs --no-run-if-empty kill -SIGUSR1 &>/dev/null
19}
20
21cleanup()
22{
23	ip netns pids "${ns}" | xargs --no-run-if-empty kill -SIGKILL &>/dev/null
24
25	ip netns del $ns
26}
27
28ip -Version > /dev/null 2>&1
29if [ $? -ne 0 ];then
30	echo "SKIP: Could not run test without ip tool"
31	exit $ksft_skip
32fi
33ss -h | grep -q MPTCP
34if [ $? -ne 0 ];then
35	echo "SKIP: ss tool does not support MPTCP"
36	exit $ksft_skip
37fi
38
39__chk_nr()
40{
41	local condition="$1"
42	local expected=$2
43	local msg nr
44
45	shift 2
46	msg=$*
47	nr=$(ss -inmHMN $ns | $condition)
48
49	printf "%-50s" "$msg"
50	if [ $nr != $expected ]; then
51		echo "[ fail ] expected $expected found $nr"
52		ret=$test_cnt
53	else
54		echo "[  ok  ]"
55	fi
56	test_cnt=$((test_cnt+1))
57}
58
59chk_msk_nr()
60{
61	__chk_nr "grep -c token:" $*
62}
63
64chk_msk_fallback_nr()
65{
66		__chk_nr "grep -c fallback" $*
67}
68
69chk_msk_remote_key_nr()
70{
71		__chk_nr "grep -c remote_key" $*
72}
73
74# $1: ns, $2: port
75wait_local_port_listen()
76{
77	local listener_ns="${1}"
78	local port="${2}"
79
80	local port_hex i
81
82	port_hex="$(printf "%04X" "${port}")"
83	for i in $(seq 10); do
84		ip netns exec "${listener_ns}" cat /proc/net/tcp | \
85			awk "BEGIN {rc=1} {if (\$2 ~ /:${port_hex}\$/ && \$4 ~ /0A/) {rc=0; exit}} END {exit rc}" &&
86			break
87		sleep 0.1
88	done
89}
90
91wait_connected()
92{
93	local listener_ns="${1}"
94	local port="${2}"
95
96	local port_hex i
97
98	port_hex="$(printf "%04X" "${port}")"
99	for i in $(seq 10); do
100		ip netns exec ${listener_ns} grep -q " 0100007F:${port_hex} " /proc/net/tcp && break
101		sleep 0.1
102	done
103}
104
105trap cleanup EXIT
106ip netns add $ns
107ip -n $ns link set dev lo up
108
109echo "a" | \
110	timeout ${timeout_test} \
111		ip netns exec $ns \
112			./mptcp_connect -p 10000 -l -t ${timeout_poll} \
113				0.0.0.0 >/dev/null &
114wait_local_port_listen $ns 10000
115chk_msk_nr 0 "no msk on netns creation"
116
117echo "b" | \
118	timeout ${timeout_test} \
119		ip netns exec $ns \
120			./mptcp_connect -p 10000 -r 0 -t ${timeout_poll} \
121				127.0.0.1 >/dev/null &
122wait_connected $ns 10000
123chk_msk_nr 2 "after MPC handshake "
124chk_msk_remote_key_nr 2 "....chk remote_key"
125chk_msk_fallback_nr 0 "....chk no fallback"
126flush_pids
127
128
129echo "a" | \
130	timeout ${timeout_test} \
131		ip netns exec $ns \
132			./mptcp_connect -p 10001 -l -s TCP -t ${timeout_poll} \
133				0.0.0.0 >/dev/null &
134wait_local_port_listen $ns 10001
135echo "b" | \
136	timeout ${timeout_test} \
137		ip netns exec $ns \
138			./mptcp_connect -p 10001 -r 0 -t ${timeout_poll} \
139				127.0.0.1 >/dev/null &
140wait_connected $ns 10001
141chk_msk_fallback_nr 1 "check fallback"
142flush_pids
143
144NR_CLIENTS=100
145for I in `seq 1 $NR_CLIENTS`; do
146	echo "a" | \
147		timeout ${timeout_test} \
148			ip netns exec $ns \
149				./mptcp_connect -p $((I+10001)) -l -w 10 \
150					-t ${timeout_poll} 0.0.0.0 >/dev/null &
151done
152wait_local_port_listen $ns $((NR_CLIENTS + 10001))
153
154for I in `seq 1 $NR_CLIENTS`; do
155	echo "b" | \
156		timeout ${timeout_test} \
157			ip netns exec $ns \
158				./mptcp_connect -p $((I+10001)) -w 10 \
159					-t ${timeout_poll} 127.0.0.1 >/dev/null &
160done
161sleep 1.5
162
163chk_msk_nr $((NR_CLIENTS*2)) "many msk socket present"
164flush_pids
165
166exit $ret
167