2 # SPDX-License-Identifier: GPL-2.0
6 optstring="S:R:d:e:l:r:h4cm:f:t"
15 timeout_test=$((timeout_poll * 2 + 1))
17 ethtool_random_on=true
18 tc_delay="$((RANDOM%50))"
19 tc_loss=$((RANDOM%101))
27 if [ $tc_loss -eq 100 ];then
29 elif [ $tc_loss -ge 10 ]; then
31 elif [ $tc_loss -ge 1 ]; then
38 echo "Usage: $0 [ -a ]"
39 echo -e "\t-d: tc/netem delay in milliseconds, e.g. \"-d 10\" (default random)"
40 echo -e "\t-l: tc/netem loss percentage, e.g. \"-l 0.02\" (default random)"
41 echo -e "\t-r: tc/netem reorder mode, e.g. \"-r 25% 50% gap 5\", use "-r 0" to disable reordering (default random)"
42 echo -e "\t-e: ethtool features to disable, e.g.: \"-e tso -e gso\" (default: randomly disable any of tso/gso/gro)"
43 echo -e "\t-4: IPv4 only: disable IPv6 tests (default: test both IPv4 and IPv6)"
44 echo -e "\t-c: capture packets for each test using tcpdump (default: no capture)"
45 echo -e "\t-f: size of file to transfer in bytes (default random)"
46 echo -e "\t-S: set sndbuf value (default: use kernel default)"
47 echo -e "\t-R: set rcvbuf value (default: use kernel default)"
48 echo -e "\t-m: test mode (poll, sendfile; default: poll)"
49 echo -e "\t-t: also run tests with TCP (use twice to non-fallback tcp)"
52 while getopts "$optstring" option;do
59 if [ $OPTARG -ge 0 ];then
62 echo "-d requires numeric argument, got \"$OPTARG\"" 1>&2
67 ethtool_args="$ethtool_args $OPTARG off"
68 ethtool_random_on=false
83 if [ $OPTARG -ge 0 ];then
86 echo "-S requires numeric argument, got \"$OPTARG\"" 1>&2
91 if [ $OPTARG -ge 0 ];then
94 echo "-R requires numeric argument, got \"$OPTARG\"" 1>&2
115 rndh=$(printf %x $sec)-$(mktemp -u XXXXXX)
130 for netns in "$ns1" "$ns2" "$ns3" "$ns4";do
132 rm -f /tmp/$netns.{nstat,out}
136 ip -Version > /dev/null 2>&1
138 echo "SKIP: Could not run test without ip tool"
149 for i in "$ns1" "$ns2" "$ns3" "$ns4";do
150 ip netns add $i || exit $ksft_skip
151 ip -net $i link set lo up
155 # ns1eth2 ns2eth1 ns2eth3 ns3eth2 ns3eth4 ns4eth3
156 # - drop 1% -> reorder 25%
159 ip link add ns1eth2 netns "$ns1" type veth peer name ns2eth1 netns "$ns2"
160 ip link add ns2eth3 netns "$ns2" type veth peer name ns3eth2 netns "$ns3"
161 ip link add ns3eth4 netns "$ns3" type veth peer name ns4eth3 netns "$ns4"
163 ip -net "$ns1" addr add 10.0.1.1/24 dev ns1eth2
164 ip -net "$ns1" addr add dead:beef:1::1/64 dev ns1eth2 nodad
166 ip -net "$ns1" link set ns1eth2 up
167 ip -net "$ns1" route add default via 10.0.1.2
168 ip -net "$ns1" route add default via dead:beef:1::2
170 ip -net "$ns2" addr add 10.0.1.2/24 dev ns2eth1
171 ip -net "$ns2" addr add dead:beef:1::2/64 dev ns2eth1 nodad
172 ip -net "$ns2" link set ns2eth1 up
174 ip -net "$ns2" addr add 10.0.2.1/24 dev ns2eth3
175 ip -net "$ns2" addr add dead:beef:2::1/64 dev ns2eth3 nodad
176 ip -net "$ns2" link set ns2eth3 up
177 ip -net "$ns2" route add default via 10.0.2.2
178 ip -net "$ns2" route add default via dead:beef:2::2
179 ip netns exec "$ns2" sysctl -q net.ipv4.ip_forward=1
180 ip netns exec "$ns2" sysctl -q net.ipv6.conf.all.forwarding=1
182 ip -net "$ns3" addr add 10.0.2.2/24 dev ns3eth2
183 ip -net "$ns3" addr add dead:beef:2::2/64 dev ns3eth2 nodad
184 ip -net "$ns3" link set ns3eth2 up
186 ip -net "$ns3" addr add 10.0.3.2/24 dev ns3eth4
187 ip -net "$ns3" addr add dead:beef:3::2/64 dev ns3eth4 nodad
188 ip -net "$ns3" link set ns3eth4 up
189 ip -net "$ns3" route add default via 10.0.2.1
190 ip -net "$ns3" route add default via dead:beef:2::1
191 ip netns exec "$ns3" sysctl -q net.ipv4.ip_forward=1
192 ip netns exec "$ns3" sysctl -q net.ipv6.conf.all.forwarding=1
194 ip -net "$ns4" addr add 10.0.3.1/24 dev ns4eth3
195 ip -net "$ns4" addr add dead:beef:3::1/64 dev ns4eth3 nodad
196 ip -net "$ns4" link set ns4eth3 up
197 ip -net "$ns4" route add default via 10.0.3.2
198 ip -net "$ns4" route add default via dead:beef:3::2
200 # use TCP syn cookies, even if no flooding was detected.
201 ip netns exec "$ns2" sysctl -q net.ipv4.tcp_syncookies=2
203 set_ethtool_flags() {
208 ip netns exec $ns ethtool -K $dev $flags 2>/dev/null
209 [ $? -eq 0 ] && echo "INFO: set $ns dev $dev: ethtool -K $flags"
212 set_random_ethtool_flags() {
216 local pick1=$((r & 1))
217 local pick2=$((r & 2))
218 local pick3=$((r & 4))
220 [ $pick1 -ne 0 ] && flags="tso off"
221 [ $pick2 -ne 0 ] && flags="$flags gso off"
222 [ $pick3 -ne 0 ] && flags="$flags gro off"
224 [ -z "$flags" ] && return
226 set_ethtool_flags "$1" "$2" "$flags"
229 if $ethtool_random_on;then
230 set_random_ethtool_flags "$ns3" ns3eth2
231 set_random_ethtool_flags "$ns4" ns4eth3
233 set_ethtool_flags "$ns3" ns3eth2 "$ethtool_args"
234 set_ethtool_flags "$ns4" ns4eth3 "$ethtool_args"
240 echo "Trailing bytes are: "
250 cmp "$in" "$out" > /dev/null 2>&1
251 if [ $? -ne 0 ] ;then
252 echo "[ FAIL ] $what does not match (in, out):"
254 print_file_err "$out"
262 check_mptcp_disabled()
265 disabled_ns="ns_disabled-$sech-$(mktemp -u XXXXXX)"
266 ip netns add ${disabled_ns} || exit $ksft_skip
268 # net.mptcp.enabled should be enabled by default
269 if [ "$(ip netns exec ${disabled_ns} sysctl net.mptcp.enabled | awk '{ print $3 }')" -ne 1 ]; then
270 echo -e "net.mptcp.enabled sysctl is not 1 by default\t\t[ FAIL ]"
274 ip netns exec ${disabled_ns} sysctl -q net.mptcp.enabled=0
277 LANG=C ip netns exec ${disabled_ns} ./mptcp_connect -p 10000 -s MPTCP 127.0.0.1 < "$cin" 2>&1 | \
278 grep -q "^socket: Protocol not available$" && err=1
279 ip netns delete ${disabled_ns}
281 if [ ${err} -eq 0 ]; then
282 echo -e "New MPTCP socket cannot be blocked via sysctl\t\t[ FAIL ]"
287 echo -e "New MPTCP socket can be blocked via sysctl\t\t[ OK ]"
291 check_mptcp_ulp_setsockopt()
294 t="ns_ulp-$sech-$(mktemp -u XXXXXX)"
296 ip netns add ${t} || exit $ksft_skip
297 if ! ip netns exec ${t} ./mptcp_connect -u -p 10000 -s TCP 127.0.0.1 2>&1; then
298 printf "setsockopt(..., TCP_ULP, \"mptcp\", ...) allowed\t[ FAIL ]\n"
302 printf "setsockopt(..., TCP_ULP, \"mptcp\", ...) blocked\t[ OK ]\n"
317 local listener_ns="$1"
318 local connector_ns="$2"
319 local connect_addr="$3"
320 local ping_args="-q -c 1"
322 if is_v6 "${connect_addr}"; then
324 ping_args="${ping_args} -6"
327 ip netns exec ${connector_ns} ping ${ping_args} $connect_addr >/dev/null
328 if [ $? -ne 0 ] ; then
329 echo "$listener_ns -> $connect_addr connectivity [ FAIL ]" 1>&2
338 # $1: ns, $2: MIB counter
341 local listener_ns="${1}"
345 ip netns exec "${listener_ns}" \
346 nstat -z -a "${mib}" | \
348 while read a count c rest; do
354 wait_local_port_listen()
356 local listener_ns="${1}"
361 port_hex="$(printf "%04X" "${port}")"
362 for i in $(seq 10); do
363 ip netns exec "${listener_ns}" cat /proc/net/tcp* | \
364 awk "BEGIN {rc=1} {if (\$2 ~ /:${port_hex}\$/ && \$4 ~ /0A/) {rc=0; exit}} END {exit rc}" &&
372 local listener_ns="$1"
373 local connector_ns="$2"
376 local connect_addr="$5"
377 local local_addr="$6"
378 local extra_args="$7"
381 port=$((10000+$TEST_COUNT))
382 TEST_COUNT=$((TEST_COUNT+1))
384 if [ "$rcvbuf" -gt 0 ]; then
385 extra_args="$extra_args -R $rcvbuf"
388 if [ "$sndbuf" -gt 0 ]; then
389 extra_args="$extra_args -S $sndbuf"
392 if [ -n "$testmode" ]; then
393 extra_args="$extra_args -m $testmode"
396 if [ -n "$extra_args" ] && $options_log; then
397 echo "INFO: extra options: $extra_args"
406 addr_port=$(printf "%s:%d" ${connect_addr} ${port})
407 printf "%.3s %-5s -> %.3s (%-20s) %-5s\t" ${connector_ns} ${cl_proto} ${listener_ns} ${addr_port} ${srv_proto}
411 if [ -z $SUDO_USER ] ; then
414 capuser="-Z $SUDO_USER"
417 local capfile="${rndh}-${connector_ns:0:3}-${listener_ns:0:3}-${cl_proto}-${srv_proto}-${connect_addr}-${port}"
418 local capopt="-i any -s 65535 -B 32768 ${capuser}"
420 ip netns exec ${listener_ns} tcpdump ${capopt} -w "${capfile}-listener.pcap" >> "${capout}" 2>&1 &
421 local cappid_listener=$!
423 ip netns exec ${connector_ns} tcpdump ${capopt} -w "${capfile}-connector.pcap" >> "${capout}" 2>&1 &
424 local cappid_connector=$!
429 NSTAT_HISTORY=/tmp/${listener_ns}.nstat ip netns exec ${listener_ns} \
431 if [ ${listener_ns} != ${connector_ns} ]; then
432 NSTAT_HISTORY=/tmp/${connector_ns}.nstat ip netns exec ${connector_ns} \
436 local stat_synrx_last_l=$(get_mib_counter "${listener_ns}" "MPTcpExtMPCapableSYNRX")
437 local stat_ackrx_last_l=$(get_mib_counter "${listener_ns}" "MPTcpExtMPCapableACKRX")
438 local stat_cookietx_last=$(get_mib_counter "${listener_ns}" "TcpExtSyncookiesSent")
439 local stat_cookierx_last=$(get_mib_counter "${listener_ns}" "TcpExtSyncookiesRecv")
441 timeout ${timeout_test} \
442 ip netns exec ${listener_ns} \
443 ./mptcp_connect -t ${timeout_poll} -l -p $port -s ${srv_proto} \
444 $extra_args $local_addr < "$sin" > "$sout" &
447 wait_local_port_listen "${listener_ns}" "${port}"
451 timeout ${timeout_test} \
452 ip netns exec ${connector_ns} \
453 ./mptcp_connect -t ${timeout_poll} -p $port -s ${cl_proto} \
454 $extra_args $connect_addr < "$cin" > "$cout" &
467 kill ${cappid_listener}
468 kill ${cappid_connector}
471 NSTAT_HISTORY=/tmp/${listener_ns}.nstat ip netns exec ${listener_ns} \
472 nstat | grep Tcp > /tmp/${listener_ns}.out
473 if [ ${listener_ns} != ${connector_ns} ]; then
474 NSTAT_HISTORY=/tmp/${connector_ns}.nstat ip netns exec ${connector_ns} \
475 nstat | grep Tcp > /tmp/${connector_ns}.out
479 duration=$((stop-start))
480 printf "(duration %05sms) " "${duration}"
481 if [ ${rets} -ne 0 ] || [ ${retc} -ne 0 ]; then
482 echo "[ FAIL ] client exit code $retc, server $rets" 1>&2
483 echo -e "\nnetns ${listener_ns} socket stat for ${port}:" 1>&2
484 ip netns exec ${listener_ns} ss -Menita 1>&2 -o "sport = :$port"
485 cat /tmp/${listener_ns}.out
486 echo -e "\nnetns ${connector_ns} socket stat for ${port}:" 1>&2
487 ip netns exec ${connector_ns} ss -Menita 1>&2 -o "dport = :$port"
488 [ ${listener_ns} != ${connector_ns} ] && cat /tmp/${connector_ns}.out
495 check_transfer $sin $cout "file received by client"
497 check_transfer $cin $sout "file received by server"
500 local stat_synrx_now_l=$(get_mib_counter "${listener_ns}" "MPTcpExtMPCapableSYNRX")
501 local stat_ackrx_now_l=$(get_mib_counter "${listener_ns}" "MPTcpExtMPCapableACKRX")
502 local stat_cookietx_now=$(get_mib_counter "${listener_ns}" "TcpExtSyncookiesSent")
503 local stat_cookierx_now=$(get_mib_counter "${listener_ns}" "TcpExtSyncookiesRecv")
505 expect_synrx=$((stat_synrx_last_l))
506 expect_ackrx=$((stat_ackrx_last_l))
508 cookies=$(ip netns exec ${listener_ns} sysctl net.ipv4.tcp_syncookies)
509 cookies=${cookies##*=}
511 if [ ${cl_proto} = "MPTCP" ] && [ ${srv_proto} = "MPTCP" ]; then
512 expect_synrx=$((stat_synrx_last_l+1))
513 expect_ackrx=$((stat_ackrx_last_l+1))
516 if [ ${stat_synrx_now_l} -lt ${expect_synrx} ]; then
517 printf "[ FAIL ] lower MPC SYN rx (%d) than expected (%d)\n" \
518 "${stat_synrx_now_l}" "${expect_synrx}" 1>&2
521 if [ ${stat_ackrx_now_l} -lt ${expect_ackrx} ]; then
522 printf "[ FAIL ] lower MPC ACK rx (%d) than expected (%d)\n" \
523 "${stat_ackrx_now_l}" "${expect_ackrx}" 1>&2
527 if [ $retc -eq 0 ] && [ $rets -eq 0 ]; then
531 if [ $cookies -eq 2 ];then
532 if [ $stat_cookietx_last -ge $stat_cookietx_now ] ;then
533 printf " WARN: CookieSent: did not advance"
535 if [ $stat_cookierx_last -ge $stat_cookierx_now ] ;then
536 printf " WARN: CookieRecv: did not advance"
539 if [ $stat_cookietx_last -ne $stat_cookietx_now ] ;then
540 printf " WARN: CookieSent: changed"
542 if [ $stat_cookierx_last -ne $stat_cookierx_now ] ;then
543 printf " WARN: CookieRecv: changed"
547 if [ ${stat_synrx_now_l} -gt ${expect_synrx} ]; then
548 printf " WARN: SYNRX: expect %d, got %d (probably retransmissions)" \
549 "${expect_synrx}" "${stat_synrx_now_l}"
551 if [ ${stat_ackrx_now_l} -gt ${expect_ackrx} ]; then
552 printf " WARN: ACKRX: expect %d, got %d (probably retransmissions)" \
553 "${expect_ackrx}" "${stat_ackrx_now_l}"
558 [ $retc -eq 0 ] && [ $rets -eq 0 ]
569 if [ $SIZE -eq 0 ]; then
570 local MAXSIZE=$((1024 * 1024 * 8))
571 local MINSIZE=$((1024 * 256))
573 SIZE=$(((RANDOM * RANDOM + MINSIZE) % MAXSIZE))
576 ksize=$((SIZE / 1024))
577 rem=$((SIZE - (ksize * 1024)))
579 dd if=/dev/urandom of="$name" bs=1024 count=$ksize 2> /dev/null
580 dd if=/dev/urandom conv=notrunc of="$name" bs=1 count=$rem 2> /dev/null
581 echo -e "\nMPTCP_TEST_FILE_END_MARKER" >> "$name"
583 echo "Created $name (size $(du -b "$name")) containing data sent by $who"
588 local listener_ns="$1"
589 local connector_ns="$2"
590 local connect_addr="$3"
592 local extra_args="$5"
595 # skip if test programs are running inside same netns for subsequent runs.
596 if [ $loopback -eq 0 ] && [ ${listener_ns} = ${connector_ns} ]; then
600 # skip if we don't want v6
601 if ! $ipv6 && is_v6 "${connect_addr}"; then
606 if is_v6 "${connect_addr}"; then
612 do_transfer ${listener_ns} ${connector_ns} MPTCP MPTCP \
613 ${connect_addr} ${local_addr} "${extra_args}"
615 if [ $lret -ne 0 ]; then
620 if [ $do_tcp -eq 0 ]; then
621 # don't bother testing fallback tcp except for loopback case.
622 if [ ${listener_ns} != ${connector_ns} ]; then
627 do_transfer ${listener_ns} ${connector_ns} MPTCP TCP \
628 ${connect_addr} ${local_addr} "${extra_args}"
630 if [ $lret -ne 0 ]; then
635 do_transfer ${listener_ns} ${connector_ns} TCP MPTCP \
636 ${connect_addr} ${local_addr} "${extra_args}"
638 if [ $lret -ne 0 ]; then
643 if [ $do_tcp -gt 1 ] ;then
644 do_transfer ${listener_ns} ${connector_ns} TCP TCP \
645 ${connect_addr} ${local_addr} "${extra_args}"
647 if [ $lret -ne 0 ]; then
658 run_tests_lo $1 $2 $3 0
665 echo "INFO: with peek mode: ${peekmode}"
666 run_tests_lo "$ns1" "$ns1" 10.0.1.1 1 "-P ${peekmode}"
667 run_tests_lo "$ns1" "$ns1" dead:beef:1::1 1 "-P ${peekmode}"
670 make_file "$cin" "client"
671 make_file "$sin" "server"
675 check_mptcp_ulp_setsockopt
677 echo "INFO: validating network environment with pings"
678 for sender in "$ns1" "$ns2" "$ns3" "$ns4";do
679 do_ping "$ns1" $sender 10.0.1.1
680 do_ping "$ns1" $sender dead:beef:1::1
682 do_ping "$ns2" $sender 10.0.1.2
683 do_ping "$ns2" $sender dead:beef:1::2
684 do_ping "$ns2" $sender 10.0.2.1
685 do_ping "$ns2" $sender dead:beef:2::1
687 do_ping "$ns3" $sender 10.0.2.2
688 do_ping "$ns3" $sender dead:beef:2::2
689 do_ping "$ns3" $sender 10.0.3.2
690 do_ping "$ns3" $sender dead:beef:3::2
692 do_ping "$ns4" $sender 10.0.3.1
693 do_ping "$ns4" $sender dead:beef:3::1
696 [ -n "$tc_loss" ] && tc -net "$ns2" qdisc add dev ns2eth3 root netem loss random $tc_loss delay ${tc_delay}ms
697 echo -n "INFO: Using loss of $tc_loss "
698 test "$tc_delay" -gt 0 && echo -n "delay $tc_delay ms "
700 reorder_delay=$(($tc_delay / 4))
702 if [ -z "${tc_reorder}" ]; then
703 reorder1=$((RANDOM%10))
704 reorder1=$((100 - reorder1))
705 reorder2=$((RANDOM%100))
707 if [ $reorder_delay -gt 0 ] && [ $reorder1 -lt 100 ] && [ $reorder2 -gt 0 ]; then
708 tc_reorder="reorder ${reorder1}% ${reorder2}%"
709 echo -n "$tc_reorder with delay ${reorder_delay}ms "
711 elif [ "$tc_reorder" = "0" ];then
713 elif [ "$reorder_delay" -gt 0 ];then
714 # reordering requires some delay
715 tc_reorder="reorder $tc_reorder"
716 echo -n "$tc_reorder with delay ${reorder_delay}ms "
721 tc -net "$ns3" qdisc add dev ns3eth4 root netem delay ${reorder_delay}ms $tc_reorder
723 for sender in $ns1 $ns2 $ns3 $ns4;do
724 run_tests_lo "$ns1" "$sender" 10.0.1.1 1
725 if [ $ret -ne 0 ] ;then
726 echo "FAIL: Could not even run loopback test" 1>&2
729 run_tests_lo "$ns1" $sender dead:beef:1::1 1
730 if [ $ret -ne 0 ] ;then
731 echo "FAIL: Could not even run loopback v6 test" 2>&1
735 run_tests "$ns2" $sender 10.0.1.2
736 run_tests "$ns2" $sender dead:beef:1::2
737 run_tests "$ns2" $sender 10.0.2.1
738 run_tests "$ns2" $sender dead:beef:2::1
740 run_tests "$ns3" $sender 10.0.2.2
741 run_tests "$ns3" $sender dead:beef:2::2
742 run_tests "$ns3" $sender 10.0.3.2
743 run_tests "$ns3" $sender dead:beef:3::2
745 run_tests "$ns4" $sender 10.0.3.1
746 run_tests "$ns4" $sender dead:beef:3::1
749 run_tests_peekmode "saveWithPeek"
750 run_tests_peekmode "saveAfterPeek"
753 time_run=$((time_end-time_start))
755 echo "Time: ${time_run} seconds"