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