1#!/bin/sh
2# Copyright (c) 2017-2018 Petr Vorel <pvorel@suse.cz>
3# Copyright (c) 2015-2017 Oracle and/or its affiliates. All Rights Reserved.
4# Copyright (c) International Business Machines  Corp., 2005
5#
6# This program is free software; you can redistribute it and/or
7# modify it under the terms of the GNU General Public License as
8# published by the Free Software Foundation; either version 2 of
9# the License, or (at your option) any later version.
10#
11# This program is distributed in the hope that it would be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with this program. If not, see <http://www.gnu.org/licenses/>.
18#
19# Author: Mitsuru Chinen <mitch@jp.ibm.com>
20
21IF_CMD='ifconfig'
22TST_SETUP="do_setup"
23TST_CLEANUP="do_cleanup"
24. if-lib.sh
25
26# The interval of the mtu change [second]
27CHANGE_INTERVAL=${CHANGE_INTERVAL:-5}
28
29# The array of the value which MTU is changed into sequentially
30# 552 - net.ipv4.route.min_pmtu
31CHANGE_VALUES="784 1142 552 1500 552 1500 552 748 552 1142 1500"
32CHANGE6_VALUES="1280 1445 1335 1390 1500 1280 1500 1280 1335 1500"
33saved_mtu=
34
35do_setup()
36{
37	[ "$TST_IPV6" ] && CHANGE_VALUES=$CHANGE6_VALUES
38	if_setup
39	saved_mtu="$(cat /sys/class/net/$(tst_iface)/mtu)"
40}
41
42do_cleanup()
43{
44	if_cleanup_restore
45	if [ "$saved_mtu" ]; then
46		ip li set $(tst_iface) mtu $saved_mtu
47		tst_rhost_run -c "ip li set $(tst_iface rhost) mtu $saved_mtu"
48	fi
49}
50
51test_body()
52{
53	local cmd="$CMD"
54
55	local iface=$(tst_iface)
56	local iface_rmt=$(tst_iface rhost)
57	[ "$TST_IPV6" ] && local netmask=64 || local netmask=16
58
59	tst_res TINFO "'$cmd' changes MTU $MTU_CHANGE_TIMES times" \
60	               "every $CHANGE_INTERVAL seconds"
61
62	mtu_array_len=$(echo $CHANGE_VALUES | wc -w)
63	local cnt=0
64	while [ $cnt -lt $MTU_CHANGE_TIMES ]; do
65		local nth=$(($cnt % $mtu_array_len))
66		field=$(($nth + 1))
67		cnt=$(($cnt + 1))
68		mtu=$(echo $CHANGE_VALUES | cut -d ' ' -f $field)
69		[ $cnt -eq $MTU_CHANGE_TIMES ] && mtu="$saved_mtu"
70
71		make_background_tcp_traffic
72
73		tst_res TINFO "set MTU to $mtu $cnt/$MTU_CHANGE_TIMES"
74		local ret=0
75		case $cmd in
76		ifconfig) ifconfig $iface mtu $mtu || ret=1
77			tst_rhost_run -c "ifconfig $iface_rmt mtu $mtu"
78		;;
79		ip) ip link set $iface mtu $mtu || ret=1
80			tst_rhost_run -c "ip link set $iface_rmt mtu $mtu"
81		;;
82		esac
83
84		if [ $? -ne 0 -o $ret -ne 0 ]; then
85			tst_res TFAIL "Failed to change the mtu at $cnt time"
86			return
87		fi
88
89		tst_sleep $CHANGE_INTERVAL
90
91		tst_ping $(tst_ipaddr) $(tst_ipaddr rhost) "1 1000 65507"
92	done
93}
94
95tst_run
96