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