xref: /illumos-gate/usr/src/test/util-tests/tests/dladm/show-class-confusion.ksh (revision 3fe455549728ac525df3be56130ad8e075d645d7)
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 2024 Oxide Computer Company
15#
16
17#
18# This is a regression test for illumos#16777. VNICs, VLANs, and Etherstubs are
19# similar devices under the hood, but different classes. In this case, one could
20# end up incidentally seeing information about one device when using the
21# show-XXX interface of the other class. Other times, this would end up just
22# outputting nothing.
23#
24# This test goes through and creates a device of each type that can done safely
25# without interfering with actual system networking state and doesn't rely on
26# existing hardware. We create everything as a temporary device other than the
27# bridge because it does not currently support -t.
28#
29
30unalias -a
31set -o pipefail
32export LANG=C.UTF-8
33
34dl_exit=0
35dl_arg0=$(basename $0)
36dl_stub="UTILtest_stub$$"
37dl_vnic="UTILtest_vnic$$"
38dl_vlan="UTILtest_vlan$$"
39dl_sim="UTILtest_sim$$"
40dl_iptun="UTILtest_iptun$$"
41#
42# Bridges can't end with a digit.
43#
44dl_bridge="UTILtest_bridge$$_"
45dl_secobj="UTILtest_secobj$$"
46dl_secobj_file="/tmp/UTILtest_secobj.pass"
47
48typeset -A dl_classes
49dl_classes["etherstub"]="show-etherstub"
50dl_classes["vnic"]="show-vnic"
51dl_classes["vlan"]="show-vlan"
52dl_classes["simnet"]="show-simnet"
53dl_classes["iptun"]="show-iptun"
54dl_classes["bridge"]="show-bridge"
55dl_classes["secobj"]="show-secobj"
56
57fatal()
58{
59	typeset msg="$*"
60	echo "TEST FAILED: $msg" >&2
61	exit 1
62}
63
64warn()
65{
66	typeset msg="$*"
67	echo "TEST FAILED: $msg" >&2
68	dl_exit=1
69}
70
71cleanup()
72{
73	rm -f $dl_secobj_file
74	dladm delete-secobj "$dl_secobj" 2>/dev/null
75	dladm delete-bridge "$dl_bridge" 2>/dev/null
76	dladm delete-iptun $dl_iptun 2>/dev/null
77	dladm delete-vlan $dl_vlan 2>/dev/null
78	dladm delete-simnet $dl_sim 2>/dev/null
79	dladm delete-vnic $dl_vnic 2>/dev/null
80	dladm delete-etherstub $dl_stub 2>/dev/null
81}
82
83setup()
84{
85	dladm create-etherstub -t $dl_stub || fatal "failed to create $dl_stub"
86	dladm create-vnic -t -l $dl_stub $dl_vnic || fatal \
87	    "failed to create $dl_vnic"
88	dladm create-simnet -t -m Ethernet $dl_sim || fatal \
89	    "failed to create $dl_sim"
90	dladm create-vlan -t -v 23 -l $dl_sim $dl_vlan || fatal \
91	    "failed to create $dl_vlan"
92	dladm create-iptun -T ipv4 -t $dl_iptun || fatal \
93	    "failed to create $dl_iptun"
94	dladm create-bridge $dl_bridge || fatal "failed to create $dl_bridge"
95	printf "A wonderful password" > $dl_secobj_file
96	dladm create-secobj -c wpa -f $dl_secobj_file -t $dl_secobj || fatal \
97	    "failed to create $dl_secobj"
98}
99
100#
101# Given a specific device class and device, run through all of the different
102# variants and make sure they pass and fail appropriately.
103#
104run_one()
105{
106	typeset dev="$1"
107	typeset class="$2"
108	typeset ex=
109	typeset out=
110	typeset ret=
111
112	for c in "${!dl_classes[@]}"; do
113		if [[ $class == $c ]]; then
114			ex=0
115		else
116			ex=1
117		fi
118
119		out=$(dladm ${dl_classes[$c]} $dev 2>/dev/null)
120		ret=$?
121		if (( ret != ex )); then
122			warn "dladm ${dl_classes[$c]} $dev returned $ret," \
123			    "but expected $ex"
124			continue
125		fi
126
127		printf "TEST PASSED: dladm %s %s correctly returned %d\n" \
128		    "${dl_classes[$c]}" "$dev" $ex
129
130		if (( ex != 0 )); then
131			continue
132		fi
133
134		[[ -z "$out" ]] && warn "$dladm ${dl_classes[$c]} $dev output" \
135		    "was empty"
136	done
137}
138
139trap cleanup EXIT
140
141setup
142run_one "$dl_stub" "etherstub"
143run_one "$dl_vnic" "vnic"
144run_one "$dl_vlan" "vlan"
145run_one "$dl_sim" "simnet"
146run_one "$dl_iptun" "iptun"
147run_one "$dl_bridge" "bridge"
148run_one "$dl_secobj" "secobj"
149cleanup
150
151if (( dl_exit == 0 )); then
152	printf "All tests passed successfully\n"
153fi
154exit $dl_exit
155