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
8ret=0
9pids=()
10
11flush_pids()
12{
13	# mptcp_connect in join mode will sleep a bit before completing,
14	# give it some time
15	sleep 1.1
16
17	for pid in ${pids[@]}; do
18		[ -d /proc/$pid ] && kill -SIGUSR1 $pid >/dev/null 2>&1
19	done
20	pids=()
21}
22
23cleanup()
24{
25	ip netns del $ns
26	for pid in ${pids[@]}; do
27		[ -d /proc/$pid ] && kill -9 $pid >/dev/null 2>&1
28	done
29}
30
31ip -Version > /dev/null 2>&1
32if [ $? -ne 0 ];then
33	echo "SKIP: Could not run test without ip tool"
34	exit $ksft_skip
35fi
36ss -h | grep -q MPTCP
37if [ $? -ne 0 ];then
38	echo "SKIP: ss tool does not support MPTCP"
39	exit $ksft_skip
40fi
41
42__chk_nr()
43{
44	local condition="$1"
45	local expected=$2
46	local msg nr
47
48	shift 2
49	msg=$*
50	nr=$(ss -inmHMN $ns | $condition)
51
52	printf "%-50s" "$msg"
53	if [ $nr != $expected ]; then
54		echo "[ fail ] expected $expected found $nr"
55		ret=$test_cnt
56	else
57		echo "[  ok  ]"
58	fi
59	test_cnt=$((test_cnt+1))
60}
61
62chk_msk_nr()
63{
64	__chk_nr "grep -c token:" $*
65}
66
67chk_msk_fallback_nr()
68{
69		__chk_nr "grep -c fallback" $*
70}
71
72chk_msk_remote_key_nr()
73{
74		__chk_nr "grep -c remote_key" $*
75}
76
77
78trap cleanup EXIT
79ip netns add $ns
80ip -n $ns link set dev lo up
81
82echo "a" | ip netns exec $ns ./mptcp_connect -p 10000 -l 0.0.0.0 -t 100 >/dev/null &
83sleep 0.1
84pids[0]=$!
85chk_msk_nr 0 "no msk on netns creation"
86
87echo "b" | ip netns exec $ns ./mptcp_connect -p 10000 127.0.0.1 -j -t 100 >/dev/null &
88sleep 0.1
89pids[1]=$!
90chk_msk_nr 2 "after MPC handshake "
91chk_msk_remote_key_nr 2 "....chk remote_key"
92chk_msk_fallback_nr 0 "....chk no fallback"
93flush_pids
94
95
96echo "a" | ip netns exec $ns ./mptcp_connect -p 10001 -s TCP -l 0.0.0.0 -t 100 >/dev/null &
97pids[0]=$!
98sleep 0.1
99echo "b" | ip netns exec $ns ./mptcp_connect -p 10001 127.0.0.1 -j -t 100 >/dev/null &
100pids[1]=$!
101sleep 0.1
102chk_msk_fallback_nr 1 "check fallback"
103flush_pids
104
105NR_CLIENTS=100
106for I in `seq 1 $NR_CLIENTS`; do
107	echo "a" | ip netns exec $ns ./mptcp_connect -p $((I+10001)) -l 0.0.0.0 -t 100 -w 10 >/dev/null  &
108	pids[$((I*2))]=$!
109done
110sleep 0.1
111
112for I in `seq 1 $NR_CLIENTS`; do
113	echo "b" | ip netns exec $ns ./mptcp_connect -p $((I+10001)) 127.0.0.1 -t 100 -w 10 >/dev/null &
114	pids[$((I*2 + 1))]=$!
115done
116sleep 1.5
117
118chk_msk_nr $((NR_CLIENTS*2)) "many msk socket present"
119flush_pids
120
121exit $ret
122