xref: /illumos-gate/usr/src/test/net-tests/tests/simnet/mtu.ksh (revision 5400c85432c0a689ecefb8c19b8f9861809ca093)
1#!/usr/bin/ksh
2#
3# This file and its contents are supplied under the terms of the
4# Common Development and Distribution License ("CDDL"), version 1.0.
5# You may only use this file in accordance with the terms of version
6# 1.0 of the CDDL.
7#
8# A full copy of the text of the CDDL should have accompanied this
9# source.  A copy of the CDDL is also available via the Internet at
10# http://www.illumos.org/license/CDDL.
11#
12
13#
14# Copyright 2026 Michal Skalski
15#
16
17#
18# Exercise simnet's MTU bounds and property metadata on both temporary and
19# persistent links, including changing the MTU to the maximum supported
20# value from a lower value.
21# No IP interfaces or peers are needed. Only links created here are removed.
22#
23
24export PATH=/usr/sbin:/usr/bin
25export LC_ALL=C.UTF-8
26unalias -a
27typeset -a links=()
28typeset -i failures=0
29typeset -i nextlink=0
30
31function fail
32{
33	print -u2 -- "FAIL: $*"
34	((failures++))
35}
36
37function cleanup
38{
39	typeset link
40	for link in "${links[@]}"; do
41		if ! dladm delete-simnet "$link"; then
42			fail "delete $link"
43		fi
44	done
45	if ((failures != 0)); then
46		exit 1
47	fi
48}
49
50trap cleanup EXIT
51
52if (( $(id -u) != 0 )); then
53	print -u2 'This test must run as root.'
54	exit 1
55fi
56
57function check_prop
58{
59	typeset link=$1 field=$2 expected=$3 flags=$4 actual
60
61	if ! actual=$(dladm show-linkprop $flags -c -o "$field" -p mtu \
62	    "$link"); then
63		fail "$link: read $field"
64		return
65	fi
66	[[ "$actual" == "$expected" ]] ||
67	    fail "$link: $flags $field: expected $expected, got $actual"
68}
69
70function test_link
71{
72	typeset media=$1 flags=$2 link="smtu$$_$nextlink" mtu before
73	((nextlink++))
74
75	print -- "Testing $media simnet ($flags): $link"
76	if ! dladm create-simnet $flags -m "$media" "$link"; then
77		fail "$link: create"
78		return
79	fi
80	links+=("$link")
81	check_prop "$link" possible 60-9000
82
83	for mtu in 60 61 1499 1500 8999 9000 1500 9000; do
84		if ! dladm set-linkprop $flags -p mtu=$mtu "$link"; then
85			fail "$link: set MTU $mtu"
86			continue
87		fi
88		check_prop "$link" value "$mtu"
89		check_prop "$link" possible 60-9000
90		if [[ -z "$flags" ]]; then
91			check_prop "$link" value "$mtu" -P
92		fi
93	done
94
95	if ! before=$(dladm show-linkprop -c -o value -p mtu "$link"); then
96		fail "$link: read MTU before invalid requests"
97		return
98	fi
99	for mtu in 0 59 9001; do
100		if dladm set-linkprop $flags -p mtu=$mtu "$link" \
101		    >/dev/null 2>&1; then
102			fail "$link: accepted invalid MTU $mtu"
103		fi
104		check_prop "$link" value "$before"
105		check_prop "$link" possible 60-9000
106		if [[ -z "$flags" ]]; then
107			check_prop "$link" value "$before" -P
108		fi
109	done
110}
111
112for media in Ethernet wifi; do
113	test_link "$media" -t
114	test_link "$media" ''
115done
116
117if ((failures != 0)); then
118	print -u2 -- "$failures checks failed"
119	exit 1
120fi
121print 'All simnet MTU checks passed'
122