summaryrefslogtreecommitdiff
path: root/tools/testing/selftests/net/forwarding/min_max_mtu.sh
blob: 97bb8b221bed8b982eee7ee0163c70798d61fb29 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#!/bin/bash
# SPDX-License-Identifier: GPL-2.0

# +--------------------+
# | H1                 |
# |                    |
# |           $h1.10 + |
# |     192.0.2.2/24 | |
# | 2001:db8:1::2/64 | |
# |                  | |
# |              $h1 + |
# |                  | |
# +------------------|-+
#                    |
# +------------------|-+
# | SW               | |
# |            $swp1 + |
# |                  | |
# |         $swp1.10 + |
# |     192.0.2.1/24   |
# | 2001:db8:1::1/64   |
# |                    |
# +--------------------+

ALL_TESTS="
	ping_ipv4
	ping_ipv6
	max_mtu_config_test
	max_mtu_traffic_test
	min_mtu_config_test
	min_mtu_traffic_test
"

NUM_NETIFS=2
source lib.sh

h1_create()
{
	simple_if_init $h1
	vlan_create $h1 10 v$h1 192.0.2.2/24 2001:db8:1::2/64
}

h1_destroy()
{
	vlan_destroy $h1 10 192.0.2.2/24 2001:db8:1::2/64
	simple_if_fini $h1
}

switch_create()
{
	ip li set dev $swp1 up
	vlan_create $swp1 10 "" 192.0.2.1/24 2001:db8:1::1/64
}

switch_destroy()
{
	ip li set dev $swp1 down
	vlan_destroy $swp1 10
}

setup_prepare()
{
	h1=${NETIFS[p1]}
	swp1=${NETIFS[p2]}

	vrf_prepare

	h1_create

	switch_create

	forwarding_enable
}

cleanup()
{
	pre_cleanup

	forwarding_restore

	switch_destroy

	h1_destroy

	vrf_cleanup
}

ping_ipv4()
{
	ping_test $h1.10 192.0.2.1
}

ping_ipv6()
{
	ping6_test $h1.10 2001:db8:1::1
}

min_max_mtu_get_if()
{
	local dev=$1; shift
	local min_max=$1; shift

	ip -d -j link show $dev | jq ".[].$min_max"
}

ensure_compatible_min_max_mtu()
{
	local min_max=$1; shift

	local mtu=$(min_max_mtu_get_if ${NETIFS[p1]} $min_max)
	local i

	for ((i = 2; i <= NUM_NETIFS; ++i)); do
		local current_mtu=$(min_max_mtu_get_if ${NETIFS[p$i]} $min_max)

		if [ $current_mtu -ne $mtu ]; then
			return 1
		fi
	done
}

mtu_set_if()
{
	local dev=$1; shift
	local mtu=$1; shift
	local should_fail=${1:-0}; shift

	mtu_set $dev $mtu 2>/dev/null
	check_err_fail $should_fail $? "Set MTU $mtu for $dev"
}

mtu_set_all_if()
{
	local mtu=$1; shift
	local i

	for ((i = 1; i <= NUM_NETIFS; ++i)); do
		mtu_set_if ${NETIFS[p$i]} $mtu
		mtu_set_if ${NETIFS[p$i]}.10 $mtu
	done
}

mtu_restore_all_if()
{
	local i

	for ((i = 1; i <= NUM_NETIFS; ++i)); do
		mtu_restore ${NETIFS[p$i]}.10
		mtu_restore ${NETIFS[p$i]}
	done
}

mtu_test_ping4()
{
	local mtu=$1; shift
	local should_fail=$1; shift

	# Ping adds 8 bytes for ICMP header and 20 bytes for IP header
	local ping_headers_len=$((20 + 8))
	local pkt_size=$((mtu - ping_headers_len))

	ping_do $h1.10 192.0.2.1 "-s $pkt_size -M do"
	check_err_fail $should_fail $? "Ping, packet size: $pkt_size"
}

mtu_test_ping6()
{
	local mtu=$1; shift
	local should_fail=$1; shift

	# Ping adds 8 bytes for ICMP header and 40 bytes for IPv6 header
	local ping6_headers_len=$((40 + 8))
	local pkt_size=$((mtu - ping6_headers_len))

	ping6_do $h1.10 2001:db8:1::1 "-s $pkt_size -M do"
	check_err_fail $should_fail $? "Ping6, packet size: $pkt_size"
}

max_mtu_config_test()
{
	local i

	RET=0

	for ((i = 1; i <= NUM_NETIFS; ++i)); do
		local dev=${NETIFS[p$i]}
		local max_mtu=$(min_max_mtu_get_if $dev "max_mtu")
		local should_fail

		should_fail=0
		mtu_set_if $dev $max_mtu $should_fail
		mtu_restore $dev

		should_fail=1
		mtu_set_if $dev $((max_mtu + 1)) $should_fail
		mtu_restore $dev
	done

	log_test "Test maximum MTU configuration"
}

max_mtu_traffic_test()
{
	local should_fail
	local max_mtu

	RET=0

	if ! ensure_compatible_min_max_mtu "max_mtu"; then
		log_test_xfail "Topology has incompatible maximum MTU values"
		return
	fi

	max_mtu=$(min_max_mtu_get_if ${NETIFS[p1]} "max_mtu")

	should_fail=0
	mtu_set_all_if $max_mtu
	mtu_test_ping4 $max_mtu $should_fail
	mtu_test_ping6 $max_mtu $should_fail
	mtu_restore_all_if

	should_fail=1
	mtu_set_all_if $((max_mtu - 1))
	mtu_test_ping4 $max_mtu $should_fail
	mtu_test_ping6 $max_mtu $should_fail
	mtu_restore_all_if

	log_test "Test traffic, packet size is maximum MTU"
}

min_mtu_config_test()
{
	local i

	RET=0

	for ((i = 1; i <= NUM_NETIFS; ++i)); do
		local dev=${NETIFS[p$i]}
		local min_mtu=$(min_max_mtu_get_if $dev "min_mtu")
		local should_fail

		should_fail=0
		mtu_set_if $dev $min_mtu $should_fail
		mtu_restore $dev

		should_fail=1
		mtu_set_if $dev $((min_mtu - 1)) $should_fail
		mtu_restore $dev
	done

	log_test "Test minimum MTU configuration"
}

min_mtu_traffic_test()
{
	local should_fail=0
	local min_mtu

	RET=0

	if ! ensure_compatible_min_max_mtu "min_mtu"; then
		log_test_xfail "Topology has incompatible minimum MTU values"
		return
	fi

	min_mtu=$(min_max_mtu_get_if ${NETIFS[p1]} "min_mtu")
	mtu_set_all_if $min_mtu
	mtu_test_ping4 $min_mtu $should_fail
	# Do not test minimum MTU with IPv6, as IPv6 requires higher MTU.

	mtu_restore_all_if

	log_test "Test traffic, packet size is minimum MTU"
}

trap cleanup EXIT

setup_prepare
setup_wait

tests_run

exit $EXIT_STATUS