1#!/bin/bash 2# SPDX-License-Identifier: GPL-2.0 3 4# Double quotes to prevent globbing and word splitting is recommended in new 5# code but we accept it, especially because there were too many before having 6# address all other issues detected by shellcheck. 7#shellcheck disable=SC2086 8 9. "$(dirname "${0}")/mptcp_lib.sh" 10 11ns="" 12timeout_poll=30 13timeout_test=$((timeout_poll * 2 + 1)) 14ret=0 15 16flush_pids() 17{ 18 # mptcp_connect in join mode will sleep a bit before completing, 19 # give it some time 20 sleep 1.1 21 22 ip netns pids "${ns}" | xargs --no-run-if-empty kill -SIGUSR1 &>/dev/null 23 24 for _ in $(seq $((timeout_poll * 10))); do 25 [ -z "$(ip netns pids "${ns}")" ] && break 26 sleep 0.1 27 done 28} 29 30# This function is used in the cleanup trap 31#shellcheck disable=SC2317 32cleanup() 33{ 34 ip netns pids "${ns}" | xargs --no-run-if-empty kill -SIGKILL &>/dev/null 35 36 mptcp_lib_ns_exit "${ns}" 37} 38 39mptcp_lib_check_mptcp 40mptcp_lib_check_tools ip ss 41 42get_msk_inuse() 43{ 44 ip netns exec $ns cat /proc/net/protocols | awk '$1~/^MPTCP$/{print $3}' 45} 46 47__chk_nr() 48{ 49 local command="$1" 50 local expected=$2 51 local msg="$3" 52 local skip="${4-SKIP}" 53 local nr 54 55 nr=$(eval $command) 56 57 mptcp_lib_print_title "$msg" 58 if [ "$nr" != "$expected" ]; then 59 if [ "$nr" = "$skip" ] && ! mptcp_lib_expect_all_features; then 60 mptcp_lib_pr_skip "Feature probably not supported" 61 mptcp_lib_result_skip "${msg}" 62 else 63 mptcp_lib_pr_fail "expected $expected found $nr" 64 mptcp_lib_result_fail "${msg}" 65 ret=${KSFT_FAIL} 66 fi 67 else 68 mptcp_lib_pr_ok 69 mptcp_lib_result_pass "${msg}" 70 fi 71} 72 73__chk_msk_nr() 74{ 75 local condition=$1 76 shift 1 77 78 __chk_nr "ss -inmHMN $ns | $condition" "$@" 79} 80 81chk_msk_nr() 82{ 83 __chk_msk_nr "grep -c token:" "$@" 84} 85 86chk_listener_nr() 87{ 88 local expected=$1 89 local msg="$2" 90 91 __chk_nr "ss -nlHMON $ns | wc -l" "$expected" "$msg - mptcp" 0 92 __chk_nr "ss -nlHtON $ns | wc -l" "$expected" "$msg - subflows" 93} 94 95wait_msk_nr() 96{ 97 local condition="grep -c token:" 98 local expected=$1 99 local timeout=20 100 local msg nr 101 local max=0 102 local i=0 103 104 shift 1 105 msg=$* 106 107 while [ $i -lt $timeout ]; do 108 nr=$(ss -inmHMN $ns | $condition) 109 [ $nr == $expected ] && break; 110 [ $nr -gt $max ] && max=$nr 111 i=$((i + 1)) 112 sleep 1 113 done 114 115 mptcp_lib_print_title "$msg" 116 if [ $i -ge $timeout ]; then 117 mptcp_lib_pr_fail "timeout while expecting $expected max $max last $nr" 118 mptcp_lib_result_fail "${msg} # timeout" 119 ret=${KSFT_FAIL} 120 elif [ $nr != $expected ]; then 121 mptcp_lib_pr_fail "expected $expected found $nr" 122 mptcp_lib_result_fail "${msg} # unexpected result" 123 ret=${KSFT_FAIL} 124 else 125 mptcp_lib_pr_ok 126 mptcp_lib_result_pass "${msg}" 127 fi 128} 129 130chk_msk_fallback_nr() 131{ 132 __chk_msk_nr "grep -c fallback" "$@" 133} 134 135chk_msk_remote_key_nr() 136{ 137 __chk_msk_nr "grep -c remote_key" "$@" 138} 139 140__chk_listen() 141{ 142 local filter="$1" 143 local expected=$2 144 local msg="$3" 145 146 __chk_nr "ss -N $ns -Ml '$filter' | grep -c LISTEN" "$expected" "$msg" 0 147} 148 149chk_msk_listen() 150{ 151 lport=$1 152 153 # destination port search should always return empty list 154 __chk_listen "dport $lport" 0 "listen match for dport $lport" 155 156 # should return 'our' mptcp listen socket 157 __chk_listen "sport $lport" 1 "listen match for sport $lport" 158 159 __chk_listen "src inet:0.0.0.0:$lport" 1 "listen match for saddr and sport" 160 161 __chk_listen "" 1 "all listen sockets" 162 163 nr=$(ss -Ml $filter | wc -l) 164} 165 166chk_msk_inuse() 167{ 168 local expected=$1 169 local msg="....chk ${2:-${expected}} msk in use" 170 local listen_nr 171 172 if [ "${expected}" -eq 0 ]; then 173 msg+=" after flush" 174 fi 175 176 listen_nr=$(ss -N "${ns}" -Ml | grep -c LISTEN) 177 expected=$((expected + listen_nr)) 178 179 for _ in $(seq 10); do 180 if [ "$(get_msk_inuse)" -eq $expected ]; then 181 break 182 fi 183 sleep 0.1 184 done 185 186 __chk_nr get_msk_inuse $expected "${msg}" 0 187} 188 189# $1: cestab nr 190chk_msk_cestab() 191{ 192 local expected=$1 193 local msg="....chk ${2:-${expected}} cestab" 194 195 if [ "${expected}" -eq 0 ]; then 196 msg+=" after flush" 197 fi 198 199 __chk_nr "mptcp_lib_get_counter ${ns} MPTcpExtMPCurrEstab" \ 200 "${expected}" "${msg}" "" 201} 202 203wait_connected() 204{ 205 local listener_ns="${1}" 206 local port="${2}" 207 208 local port_hex i 209 210 port_hex="$(printf "%04X" "${port}")" 211 for i in $(seq 10); do 212 ip netns exec ${listener_ns} grep -q " 0100007F:${port_hex} " /proc/net/tcp && break 213 sleep 0.1 214 done 215} 216 217trap cleanup EXIT 218mptcp_lib_ns_init ns 219 220echo "a" | \ 221 timeout ${timeout_test} \ 222 ip netns exec $ns \ 223 ./mptcp_connect -p 10000 -l -t ${timeout_poll} -w 20 \ 224 0.0.0.0 >/dev/null & 225mptcp_lib_wait_local_port_listen $ns 10000 226chk_msk_nr 0 "no msk on netns creation" 227chk_msk_listen 10000 228 229echo "b" | \ 230 timeout ${timeout_test} \ 231 ip netns exec $ns \ 232 ./mptcp_connect -p 10000 -r 0 -t ${timeout_poll} -w 20 \ 233 127.0.0.1 >/dev/null & 234wait_connected $ns 10000 235chk_msk_nr 2 "after MPC handshake " 236chk_msk_remote_key_nr 2 "....chk remote_key" 237chk_msk_fallback_nr 0 "....chk no fallback" 238chk_msk_inuse 2 239chk_msk_cestab 2 240flush_pids 241 242chk_msk_inuse 0 "2->0" 243chk_msk_cestab 0 "2->0" 244 245echo "a" | \ 246 timeout ${timeout_test} \ 247 ip netns exec $ns \ 248 ./mptcp_connect -p 10001 -l -s TCP -t ${timeout_poll} -w 20 \ 249 0.0.0.0 >/dev/null & 250mptcp_lib_wait_local_port_listen $ns 10001 251echo "b" | \ 252 timeout ${timeout_test} \ 253 ip netns exec $ns \ 254 ./mptcp_connect -p 10001 -r 0 -t ${timeout_poll} -w 20 \ 255 127.0.0.1 >/dev/null & 256wait_connected $ns 10001 257chk_msk_fallback_nr 1 "check fallback" 258chk_msk_inuse 1 259chk_msk_cestab 1 260flush_pids 261 262chk_msk_inuse 0 "1->0" 263chk_msk_cestab 0 "1->0" 264 265NR_CLIENTS=100 266for I in $(seq 1 $NR_CLIENTS); do 267 echo "a" | \ 268 timeout ${timeout_test} \ 269 ip netns exec $ns \ 270 ./mptcp_connect -p $((I+10001)) -l -w 20 \ 271 -t ${timeout_poll} 0.0.0.0 >/dev/null & 272done 273mptcp_lib_wait_local_port_listen $ns $((NR_CLIENTS + 10001)) 274 275for I in $(seq 1 $NR_CLIENTS); do 276 echo "b" | \ 277 timeout ${timeout_test} \ 278 ip netns exec $ns \ 279 ./mptcp_connect -p $((I+10001)) -w 20 \ 280 -t ${timeout_poll} 127.0.0.1 >/dev/null & 281done 282 283wait_msk_nr $((NR_CLIENTS*2)) "many msk socket present" 284chk_msk_inuse $((NR_CLIENTS*2)) "many" 285chk_msk_cestab $((NR_CLIENTS*2)) "many" 286flush_pids 287 288chk_msk_inuse 0 "many->0" 289chk_msk_cestab 0 "many->0" 290 291chk_listener_nr 0 "no listener sockets" 292NR_SERVERS=100 293for I in $(seq 1 $NR_SERVERS); do 294 ip netns exec $ns ./mptcp_connect -p $((I + 20001)) \ 295 -t ${timeout_poll} -l 0.0.0.0 >/dev/null 2>&1 & 296done 297mptcp_lib_wait_local_port_listen $ns $((NR_SERVERS + 20001)) 298 299chk_listener_nr $NR_SERVERS "many listener sockets" 300 301# graceful termination 302for I in $(seq 1 $NR_SERVERS); do 303 echo a | ip netns exec $ns ./mptcp_connect -p $((I + 20001)) 127.0.0.1 >/dev/null 2>&1 & 304done 305flush_pids 306 307mptcp_lib_result_print_all_tap 308exit $ret 309