1#!/bin/sh 2# SPDX-License-Identifier: GPL-2.0-or-later 3# Copyright (c) 2017-2021 Petr Vorel <pvorel@suse.cz> 4# Copyright (c) 2015-2017 Oracle and/or its affiliates. All Rights Reserved. 5# Copyright (c) International Business Machines Corp., 2005 6# Author: Mitsuru Chinen <mitch@jp.ibm.com> 7 8IF_CMD='ifconfig' 9TST_SETUP="do_setup" 10TST_CLEANUP="do_cleanup" 11. if-lib.sh 12 13# CHANGE_INTERVAL: The interval of the mtu change 14TST_TIMEOUT=1 15if tst_net_use_netns; then 16 CHANGE_INTERVAL=${CHANGE_INTERVAL:-100ms} 17else 18 CHANGE_INTERVAL=${CHANGE_INTERVAL:-5} 19fi 20tst_is_int $CHANGE_INTERVAL && TST_TIMEOUT=$CHANGE_INTERVAL 21TST_TIMEOUT=$(((TST_TIMEOUT + 30) * MTU_CHANGE_TIMES)) 22 23# The array of the value which MTU is changed into sequentially 24# 552 - net.ipv4.route.min_pmtu 25CHANGE_VALUES="784 1142 552 1500 552 1500 552 748 552 1142 1500" 26CHANGE6_VALUES="1280 1445 1335 1390 1500 1280 1500 1280 1335 1500" 27saved_mtu= 28 29MAX_PACKET_SIZE=65507 30 31set_mtu() 32{ 33 local mtu="$1" 34 local cmd="$2" 35 local ret=0 36 local iface=$(tst_iface) 37 local iface_rmt=$(tst_iface rhost) 38 39 case $cmd in 40 ifconfig) ifconfig $iface mtu $mtu || ret=1 41 tst_rhost_run -c "ifconfig $iface_rmt mtu $mtu" || ret=1 42 ;; 43 ip) ip link set $iface mtu $mtu || ret=1 44 tst_rhost_run -c "ip link set $iface_rmt mtu $mtu" || ret=1 45 ;; 46 *) tst_brk TBROK "unknown cmd '$cmd'" 47 ;; 48 esac 49 50 return $ret 51} 52 53find_ipv4_max_packet_size() 54{ 55 local min_mtu=552 56 local size=$MAX_PACKET_SIZE 57 58 set_mtu $min_mtu $CMD || tst_brk TBROK "failed to set MTU to $mtu" 59 tst_res TINFO "checking max MTU" 60 while [ $size -gt 0 ]; do 61 if ping -I $(tst_iface) -c1 -w1 -s $size $(tst_ipaddr rhost) >/dev/null; then 62 tst_res TINFO "use max MTU $size" 63 MAX_PACKET_SIZE=$size 64 return 65 fi 66 size=$((size - 500)) 67 done 68 tst_brk TBROK "failed to find max MTU" 69} 70 71do_setup() 72{ 73 74 [ "$TST_IPV6" ] && CHANGE_VALUES=$CHANGE6_VALUES 75 if_setup 76 saved_mtu="$(cat /sys/class/net/$(tst_iface)/mtu)" 77 [ "$TST_IPV6" ] || find_ipv4_max_packet_size 78} 79 80do_cleanup() 81{ 82 if_cleanup_restore 83 if [ "$saved_mtu" ]; then 84 ip link set $(tst_iface) mtu $saved_mtu 85 tst_rhost_run -c "ip link set $(tst_iface rhost) mtu $saved_mtu" 86 fi 87} 88 89test_body() 90{ 91 local cmd="$CMD" 92 local msg="'$cmd' changes MTU $MTU_CHANGE_TIMES times every $CHANGE_INTERVAL" 93 94 tst_is_int $CHANGE_INTERVAL && msg="$msg seconds" 95 tst_res TINFO "$msg" 96 97 mtu_array_len=$(echo $CHANGE_VALUES | wc -w) 98 local cnt=0 99 while [ $cnt -lt $MTU_CHANGE_TIMES ]; do 100 local nth=$(($cnt % $mtu_array_len)) 101 field=$(($nth + 1)) 102 cnt=$(($cnt + 1)) 103 mtu=$(echo $CHANGE_VALUES | cut -d ' ' -f $field) 104 [ $cnt -eq $MTU_CHANGE_TIMES ] && mtu="$saved_mtu" 105 106 make_background_tcp_traffic 107 108 tst_res TINFO "set MTU to $mtu $cnt/$MTU_CHANGE_TIMES" 109 if ! set_mtu $mtu $cmd; then 110 tst_res TFAIL "failed to change MTU to $mtu at $cnt time" 111 return 112 fi 113 114 tst_sleep $CHANGE_INTERVAL 115 116 tst_ping -s "1 $((mtu / 2)) $mtu $MAX_PACKET_SIZE" 117 done 118} 119 120tst_run 121