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
75trap cleanup EXIT
76ip netns add $ns
77ip -n $ns link set dev lo up
78
79echo "a" | \
80	timeout ${timeout_test} \
81		ip netns exec $ns \
82			./mptcp_connect -p 10000 -l -t ${timeout_poll} \
83				0.0.0.0 >/dev/null &
84sleep 0.1
85chk_msk_nr 0 "no msk on netns creation"
86
87echo "b" | \
88	timeout ${timeout_test} \
89		ip netns exec $ns \
90			./mptcp_connect -p 10000 -j -t ${timeout_poll} \
91				127.0.0.1 >/dev/null &
92sleep 0.1
93chk_msk_nr 2 "after MPC handshake "
94chk_msk_remote_key_nr 2 "....chk remote_key"
95chk_msk_fallback_nr 0 "....chk no fallback"
96flush_pids
97
98
99echo "a" | \
100	timeout ${timeout_test} \
101		ip netns exec $ns \
102			./mptcp_connect -p 10001 -l -s TCP -t ${timeout_poll} \
103				0.0.0.0 >/dev/null &
104sleep 0.1
105echo "b" | \
106	timeout ${timeout_test} \
107		ip netns exec $ns \
108			./mptcp_connect -p 10001 -j -t ${timeout_poll} \
109				127.0.0.1 >/dev/null &
110sleep 0.1
111chk_msk_fallback_nr 1 "check fallback"
112flush_pids
113
114NR_CLIENTS=100
115for I in `seq 1 $NR_CLIENTS`; do
116	echo "a" | \
117		timeout ${timeout_test} \
118			ip netns exec $ns \
119				./mptcp_connect -p $((I+10001)) -l -w 10 \
120					-t ${timeout_poll} 0.0.0.0 >/dev/null &
121done
122sleep 0.1
123
124for I in `seq 1 $NR_CLIENTS`; do
125	echo "b" | \
126		timeout ${timeout_test} \
127			ip netns exec $ns \
128				./mptcp_connect -p $((I+10001)) -w 10 \
129					-t ${timeout_poll} 127.0.0.1 >/dev/null &
130done
131sleep 1.5
132
133chk_msk_nr $((NR_CLIENTS*2)) "many msk socket present"
134flush_pids
135
136exit $ret
137