1#!/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 2017 Joyent, Inc.
15#
16
17#
18# The purpose of this test is to verify that set-linkprop performs as
19# it should -- both on persistent and temporary links.
20#
21
22vm_arg0="$(basename $0)"
23vm_stub="teststub$$"
24vm_pvnic="test_pvnic$$"
25vm_tvnic="test_tvnic$$"
26
27DL_FILE=/etc/dladm/datalink.conf
28
29fatal()
30{
31	typeset msg="$*"
32	[[ -z "$msg" ]] && msg="failed"
33	echo "TEST_FAIL: $vm_arg0: $msg" >&2
34
35	# Try to clean up just in case
36	dladm delete-vnic $vm_pvnic 2>/dev/null
37	dladm delete-vnic $vm_tvnic 2>/dev/null
38	dladm delete-etherstub $vm_stub 2>/dev/null
39	exit 1
40}
41
42delete_stub()
43{
44	dladm delete-etherstub $vm_stub || fatal \
45	    "failed to delete stub $vm_stub"
46}
47
48create_stub()
49{
50	dladm create-etherstub $vm_stub || fatal \
51	    "failed to create stub"
52}
53
54create_vnic()
55{
56	typeset dev=$1
57	typeset flags=$2
58
59	dladm create-vnic $flags -l $vm_stub $dev 2>/dev/null || fatal \
60	    "failed to create vnic: $dev"
61}
62
63delete_vnic()
64{
65	typeset dev=$1
66
67	dladm delete-vnic $dev || fatal "failed to delete vnic: $dev"
68}
69
70#
71# Validate the property is reported by dladm.
72#
73validate_prop()
74{
75	typeset dev=$1
76	typeset prop=$2
77	typeset val=$3
78	typeset oval
79
80	[[ -z "$dev" ]] && fatal "missing required device"
81	[[ -z "$prop" ]] && fatal "missing required prop"
82	[[ -z "$val" ]] && fatal "missing required val"
83	oval=$(dladm show-linkprop -c -o value -p $prop $dev | tr -d ' ')
84	[[ $? -eq 0 ]] || fatal "failed to get $prop for $dev"
85	[[ "$val" == "$oval" ]] || fatal \
86	    "$prop mismatch on $dev: expected $val, got $oval"
87}
88
89#
90# Validate the property is persistent.
91#
92validate_pprop()
93{
94	typeset dev=$1
95	typeset prop=$2
96	typeset val=$3
97	typeset oval
98
99	[[ -z "$dev" ]] && fatal "missing required device"
100	[[ -z "$prop" ]] && fatal "missing required prop"
101	[[ -z "$val" ]] && fatal "missing required val"
102
103	oval=$(awk "/^$dev/ { print \$2 }" $DL_FILE | \
104	    awk -F, "BEGIN { RS=\";\"; } /^$prop/ { print \$2; }")
105
106	[[ $? -eq 0 ]] || fatal "failed to get persistent $prop for $dev"
107	[[ "$val" == "$oval" ]] || fatal \
108	    "persistent $prop mismatch on $dev: expected $val, got $oval"
109}
110
111#
112# Validate the the property is not persistent.
113#
114validate_not_pprop()
115{
116	typeset dev=$1
117	typeset prop=$2
118
119	[[ -z "$dev" ]] && fatal "missing required device"
120	[[ -z "$prop" ]] && fatal "missing required prop"
121
122	oval=$(awk "/^$dev/ { print \$2 }" $DL_FILE | \
123	    awk -F, "BEGIN { RS=\";\"; } /^$prop/ { print \$2; }")
124
125	[[ $? -eq 0 ]] || fatal "failed to search $DL_FILE"
126
127	[[ -z "$oval" ]] || fatal \
128	    "found persistent $prop for $dev but didn't expect to"
129
130}
131
132set_prop_pass()
133{
134	typeset dev=$1
135	typeset flags=$2
136	typeset prop=$3
137	typeset val=$4
138	typeset msg="failed to set prop $prop on $dev"
139
140	[[ "$#" -ne 4 ]] && fatal "set_prop_pass() requires 4 args"
141	[[ -z "$dev" ]] && fatal "missing required device"
142	[[ -z "$prop" ]] && fatal "missing required prop"
143	[[ -z "$val" ]] && fatal "missing required val"
144
145	if [ -n "$flags" ]; then
146		typeset msg="failed to set temp prop $prop on $dev"
147	fi
148
149	dladm set-linkprop $flags -p $prop=$val $dev || fatal $msg
150}
151
152test_pass()
153{
154	[[ -f $DL_FILE ]] || fatal "datalink file does not exist: $DL_FILE"
155
156	create_stub
157
158	#
159	# Test setting persistent and temp properties on a persistent
160	# link.
161	#
162	create_vnic $vm_pvnic
163
164	set_prop_pass $vm_pvnic "-t" maxbw 89
165	validate_prop $vm_pvnic maxbw 89
166	validate_not_pprop $vm_pvnic maxbw 89
167	set_prop_pass $vm_pvnic "-t" priority medium
168	validate_prop $vm_pvnic priority medium
169	validate_not_pprop $vm_pvnic priority medium
170
171	set_prop_pass $vm_pvnic "" maxbw 99
172	validate_prop $vm_pvnic maxbw 99
173	validate_pprop $vm_pvnic maxbw 99
174	set_prop_pass $vm_pvnic "" priority low
175	validate_prop $vm_pvnic priority low
176	validate_pprop $vm_pvnic priority low
177
178	delete_vnic $vm_pvnic
179
180	#
181	# Test setting persistent and temp properties on a temp link.
182	# A "persistent" property on a temp link is really just a temp
183	# property. But setting a property on a temp link, without
184	# passing -t, should still work and report success to the
185	# user.
186	#
187	create_vnic $vm_tvnic "-t"
188
189	set_prop_pass $vm_tvnic "-t" maxbw 89
190	validate_prop $vm_tvnic maxbw 89
191	validate_not_pprop $vm_tvnic maxbw 89
192	set_prop_pass $vm_tvnic "-t" priority medium
193	validate_prop $vm_tvnic priority medium
194	validate_not_pprop $vm_tvnic priority medium
195
196	set_prop_pass $vm_tvnic "" maxbw 99
197	validate_prop $vm_tvnic maxbw 99
198	validate_not_pprop $vm_tvnic maxbw 99
199	set_prop_pass $vm_tvnic "" priority low
200	validate_prop $vm_tvnic priority low
201	validate_not_pprop $vm_tvnic priority low
202
203	delete_vnic $vm_tvnic
204
205	delete_stub
206}
207
208test_pass
209