xref: /illumos-gate/usr/src/uts/common/io/vnic/vnic_dev.c (revision ea7201620ceafab72b37e65bea4ec461dd27089d)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
23  * Copyright 2018 Joyent, Inc.
24  * Copyright 2016 OmniTI Computer Consulting, Inc. All rights reserved.
25  * Copyright 2020 OmniOS Community Edition (OmniOSce) Association.
26  */
27 
28 #include <sys/types.h>
29 #include <sys/cred.h>
30 #include <sys/sysmacros.h>
31 #include <sys/conf.h>
32 #include <sys/cmn_err.h>
33 #include <sys/list.h>
34 #include <sys/ksynch.h>
35 #include <sys/kmem.h>
36 #include <sys/stream.h>
37 #include <sys/modctl.h>
38 #include <sys/ddi.h>
39 #include <sys/sunddi.h>
40 #include <sys/atomic.h>
41 #include <sys/stat.h>
42 #include <sys/modhash.h>
43 #include <sys/strsubr.h>
44 #include <sys/strsun.h>
45 #include <sys/dlpi.h>
46 #include <sys/mac.h>
47 #include <sys/mac_provider.h>
48 #include <sys/mac_client.h>
49 #include <sys/mac_client_priv.h>
50 #include <sys/mac_ether.h>
51 #include <sys/dls.h>
52 #include <sys/pattr.h>
53 #include <sys/time.h>
54 #include <sys/vlan.h>
55 #include <sys/vnic.h>
56 #include <sys/vnic_impl.h>
57 #include <sys/mac_impl.h>
58 #include <sys/mac_flow_impl.h>
59 #include <inet/ip_impl.h>
60 
61 /*
62  * Note that for best performance, the VNIC is a passthrough design.
63  * For each VNIC corresponds a MAC client of the underlying MAC (lower MAC).
64  * This MAC client is opened by the VNIC driver at VNIC creation,
65  * and closed when the VNIC is deleted.
66  * When a MAC client of the VNIC itself opens a VNIC, the MAC layer
67  * (upper MAC) detects that the MAC being opened is a VNIC. Instead
68  * of allocating a new MAC client, it asks the VNIC driver to return
69  * the lower MAC client handle associated with the VNIC, and that handle
70  * is returned to the upper MAC client directly. This allows access
71  * by upper MAC clients of the VNIC to have direct access to the lower
72  * MAC client for the control path and data path.
73  *
74  * Due to this passthrough, some of the entry points exported by the
75  * VNIC driver are never directly invoked. These entry points include
76  * vnic_m_start, vnic_m_stop, vnic_m_promisc, vnic_m_multicst, etc.
77  *
78  * VNICs support multiple upper mac clients to enable support for
79  * multiple MAC addresses on the VNIC. When the VNIC is created the
80  * initial mac client is the primary upper mac. Any additional mac
81  * clients are secondary macs.
82  */
83 
84 static int vnic_m_start(void *);
85 static void vnic_m_stop(void *);
86 static int vnic_m_promisc(void *, boolean_t);
87 static int vnic_m_multicst(void *, boolean_t, const uint8_t *);
88 static int vnic_m_unicst(void *, const uint8_t *);
89 static int vnic_m_stat(void *, uint_t, uint64_t *);
90 static void vnic_m_ioctl(void *, queue_t *, mblk_t *);
91 static int vnic_m_setprop(void *, const char *, mac_prop_id_t, uint_t,
92     const void *);
93 static int vnic_m_getprop(void *, const char *, mac_prop_id_t, uint_t, void *);
94 static void vnic_m_propinfo(void *, const char *, mac_prop_id_t,
95     mac_prop_info_handle_t);
96 static mblk_t *vnic_m_tx(void *, mblk_t *);
97 static boolean_t vnic_m_capab_get(void *, mac_capab_t, void *);
98 static void vnic_notify_cb(void *, mac_notify_type_t);
99 static void vnic_cleanup_secondary_macs(vnic_t *, int);
100 
101 static kmem_cache_t	*vnic_cache;
102 static krwlock_t	vnic_lock;
103 static uint_t		vnic_count;
104 
105 #define	ANCHOR_VNIC_MIN_MTU	576
106 #define	ANCHOR_VNIC_MAX_MTU	9000
107 
108 /* hash of VNICs (vnic_t's), keyed by VNIC id */
109 static mod_hash_t	*vnic_hash;
110 #define	VNIC_HASHSZ	64
111 #define	VNIC_HASH_KEY(vnic_id)	((mod_hash_key_t)(uintptr_t)vnic_id)
112 
113 #define	VNIC_M_CALLBACK_FLAGS	\
114 	(MC_IOCTL | MC_GETCAPAB | MC_SETPROP | MC_GETPROP | MC_PROPINFO)
115 
116 static mac_callbacks_t vnic_m_callbacks = {
117 	VNIC_M_CALLBACK_FLAGS,
118 	vnic_m_stat,
119 	vnic_m_start,
120 	vnic_m_stop,
121 	vnic_m_promisc,
122 	vnic_m_multicst,
123 	vnic_m_unicst,
124 	vnic_m_tx,
125 	NULL,
126 	vnic_m_ioctl,
127 	vnic_m_capab_get,
128 	NULL,
129 	NULL,
130 	vnic_m_setprop,
131 	vnic_m_getprop,
132 	vnic_m_propinfo
133 };
134 
135 void
136 vnic_dev_init(void)
137 {
138 	vnic_cache = kmem_cache_create("vnic_cache",
139 	    sizeof (vnic_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
140 
141 	vnic_hash = mod_hash_create_idhash("vnic_hash",
142 	    VNIC_HASHSZ, mod_hash_null_valdtor);
143 
144 	rw_init(&vnic_lock, NULL, RW_DEFAULT, NULL);
145 
146 	vnic_count = 0;
147 }
148 
149 void
150 vnic_dev_fini(void)
151 {
152 	ASSERT(vnic_count == 0);
153 
154 	rw_destroy(&vnic_lock);
155 	mod_hash_destroy_idhash(vnic_hash);
156 	kmem_cache_destroy(vnic_cache);
157 }
158 
159 uint_t
160 vnic_dev_count(void)
161 {
162 	return (vnic_count);
163 }
164 
165 static vnic_ioc_diag_t
166 vnic_mac2vnic_diag(mac_diag_t diag)
167 {
168 	switch (diag) {
169 	case MAC_DIAG_MACADDR_NIC:
170 		return (VNIC_IOC_DIAG_MACADDR_NIC);
171 	case MAC_DIAG_MACADDR_INUSE:
172 		return (VNIC_IOC_DIAG_MACADDR_INUSE);
173 	case MAC_DIAG_MACADDR_INVALID:
174 		return (VNIC_IOC_DIAG_MACADDR_INVALID);
175 	case MAC_DIAG_MACADDRLEN_INVALID:
176 		return (VNIC_IOC_DIAG_MACADDRLEN_INVALID);
177 	case MAC_DIAG_MACFACTORYSLOTINVALID:
178 		return (VNIC_IOC_DIAG_MACFACTORYSLOTINVALID);
179 	case MAC_DIAG_MACFACTORYSLOTUSED:
180 		return (VNIC_IOC_DIAG_MACFACTORYSLOTUSED);
181 	case MAC_DIAG_MACFACTORYSLOTALLUSED:
182 		return (VNIC_IOC_DIAG_MACFACTORYSLOTALLUSED);
183 	case MAC_DIAG_MACFACTORYNOTSUP:
184 		return (VNIC_IOC_DIAG_MACFACTORYNOTSUP);
185 	case MAC_DIAG_MACPREFIX_INVALID:
186 		return (VNIC_IOC_DIAG_MACPREFIX_INVALID);
187 	case MAC_DIAG_MACPREFIXLEN_INVALID:
188 		return (VNIC_IOC_DIAG_MACPREFIXLEN_INVALID);
189 	case MAC_DIAG_MACNO_HWRINGS:
190 		return (VNIC_IOC_DIAG_NO_HWRINGS);
191 	default:
192 		return (VNIC_IOC_DIAG_NONE);
193 	}
194 }
195 
196 static int
197 vnic_unicast_add(vnic_t *vnic, vnic_mac_addr_type_t vnic_addr_type,
198     int *addr_slot, uint_t prefix_len, int *addr_len_ptr_arg,
199     uint8_t *mac_addr_arg, uint16_t flags, vnic_ioc_diag_t *diag,
200     uint16_t vid, boolean_t req_hwgrp_flag)
201 {
202 	mac_diag_t mac_diag = MAC_DIAG_NONE;
203 	uint16_t mac_flags = 0;
204 	int err;
205 	uint_t addr_len;
206 
207 	if (flags & VNIC_IOC_CREATE_NODUPCHECK)
208 		mac_flags |= MAC_UNICAST_NODUPCHECK;
209 
210 	switch (vnic_addr_type) {
211 	case VNIC_MAC_ADDR_TYPE_FIXED:
212 	case VNIC_MAC_ADDR_TYPE_VRID:
213 		/*
214 		 * The MAC address value to assign to the VNIC
215 		 * is already provided in mac_addr_arg. addr_len_ptr_arg
216 		 * already contains the MAC address length.
217 		 */
218 		break;
219 
220 	case VNIC_MAC_ADDR_TYPE_RANDOM:
221 		/*
222 		 * Random MAC address. There are two sub-cases:
223 		 *
224 		 * 1 - If mac_len == 0, a new MAC address is generated.
225 		 *	The length of the MAC address to generated depends
226 		 *	on the type of MAC used. The prefix to use for the MAC
227 		 *	address is stored in the most significant bytes
228 		 *	of the mac_addr argument, and its length is specified
229 		 *	by the mac_prefix_len argument. This prefix can
230 		 *	correspond to a IEEE OUI in the case of Ethernet,
231 		 *	for example.
232 		 *
233 		 * 2 - If mac_len > 0, the address was already picked
234 		 *	randomly, and is now passed back during VNIC
235 		 *	re-creation. The mac_addr argument contains the MAC
236 		 *	address that was generated. We distinguish this
237 		 *	case from the fixed MAC address case, since we
238 		 *	want the user consumers to know, when they query
239 		 *	the list of VNICs, that a VNIC was assigned a
240 		 *	random MAC address vs assigned a fixed address
241 		 *	specified by the user.
242 		 */
243 
244 		/*
245 		 * If it's a pre-generated address, we're done. mac_addr_arg
246 		 * and addr_len_ptr_arg already contain the MAC address
247 		 * value and length.
248 		 */
249 		if (*addr_len_ptr_arg > 0)
250 			break;
251 
252 		/* generate a new random MAC address */
253 		if ((err = mac_addr_random(vnic->vn_mch,
254 		    prefix_len, mac_addr_arg, &mac_diag)) != 0) {
255 			*diag = vnic_mac2vnic_diag(mac_diag);
256 			return (err);
257 		}
258 		*addr_len_ptr_arg = mac_addr_len(vnic->vn_lower_mh);
259 		break;
260 
261 	case VNIC_MAC_ADDR_TYPE_FACTORY:
262 		err = mac_addr_factory_reserve(vnic->vn_mch, addr_slot);
263 		if (err != 0) {
264 			if (err == EINVAL)
265 				*diag = VNIC_IOC_DIAG_MACFACTORYSLOTINVALID;
266 			if (err == EBUSY)
267 				*diag = VNIC_IOC_DIAG_MACFACTORYSLOTUSED;
268 			if (err == ENOSPC)
269 				*diag = VNIC_IOC_DIAG_MACFACTORYSLOTALLUSED;
270 			return (err);
271 		}
272 
273 		mac_addr_factory_value(vnic->vn_lower_mh, *addr_slot,
274 		    mac_addr_arg, &addr_len, NULL, NULL);
275 		*addr_len_ptr_arg = addr_len;
276 		break;
277 
278 	case VNIC_MAC_ADDR_TYPE_AUTO:
279 		/* first try to allocate a factory MAC address */
280 		err = mac_addr_factory_reserve(vnic->vn_mch, addr_slot);
281 		if (err == 0) {
282 			mac_addr_factory_value(vnic->vn_lower_mh, *addr_slot,
283 			    mac_addr_arg, &addr_len, NULL, NULL);
284 			vnic_addr_type = VNIC_MAC_ADDR_TYPE_FACTORY;
285 			*addr_len_ptr_arg = addr_len;
286 			break;
287 		}
288 
289 		/*
290 		 * Allocating a factory MAC address failed, generate a
291 		 * random MAC address instead.
292 		 */
293 		if ((err = mac_addr_random(vnic->vn_mch,
294 		    prefix_len, mac_addr_arg, &mac_diag)) != 0) {
295 			*diag = vnic_mac2vnic_diag(mac_diag);
296 			return (err);
297 		}
298 		*addr_len_ptr_arg = mac_addr_len(vnic->vn_lower_mh);
299 		vnic_addr_type = VNIC_MAC_ADDR_TYPE_RANDOM;
300 		break;
301 	case VNIC_MAC_ADDR_TYPE_PRIMARY:
302 		/*
303 		 * We get the address here since we copy it in the
304 		 * vnic's vn_addr.
305 		 * We can't ask for hardware resources since we
306 		 * don't currently support hardware classification
307 		 * for these MAC clients.
308 		 */
309 		if (req_hwgrp_flag) {
310 			*diag = VNIC_IOC_DIAG_NO_HWRINGS;
311 			return (ENOTSUP);
312 		}
313 		mac_unicast_primary_get(vnic->vn_lower_mh, mac_addr_arg);
314 		*addr_len_ptr_arg = mac_addr_len(vnic->vn_lower_mh);
315 		mac_flags |= MAC_UNICAST_VNIC_PRIMARY;
316 		break;
317 	}
318 
319 	vnic->vn_addr_type = vnic_addr_type;
320 
321 	err = mac_unicast_add(vnic->vn_mch, mac_addr_arg, mac_flags,
322 	    &vnic->vn_muh, vid, &mac_diag);
323 	if (err != 0) {
324 		if (vnic_addr_type == VNIC_MAC_ADDR_TYPE_FACTORY) {
325 			/* release factory MAC address */
326 			mac_addr_factory_release(vnic->vn_mch, *addr_slot);
327 		}
328 		*diag = vnic_mac2vnic_diag(mac_diag);
329 	}
330 
331 	return (err);
332 }
333 
334 /*
335  * Create a new VNIC upon request from administrator.
336  * Returns 0 on success, an errno on failure.
337  */
338 /* ARGSUSED */
339 int
340 vnic_dev_create(datalink_id_t vnic_id, datalink_id_t linkid,
341     vnic_mac_addr_type_t *vnic_addr_type, int *mac_len, uchar_t *mac_addr,
342     int *mac_slot, uint_t mac_prefix_len, uint16_t vid, vrid_t vrid,
343     int af, mac_resource_props_t *mrp, uint32_t flags, vnic_ioc_diag_t *diag,
344     cred_t *credp)
345 {
346 	vnic_t *vnic;
347 	mac_register_t *mac;
348 	int err;
349 	boolean_t is_anchor = ((flags & VNIC_IOC_CREATE_ANCHOR) != 0);
350 	char vnic_name[MAXNAMELEN];
351 	const mac_info_t *minfop;
352 	uint32_t req_hwgrp_flag = B_FALSE;
353 
354 	*diag = VNIC_IOC_DIAG_NONE;
355 
356 	rw_enter(&vnic_lock, RW_WRITER);
357 
358 	/* Does a VNIC with the same id already exist? */
359 	err = mod_hash_find(vnic_hash, VNIC_HASH_KEY(vnic_id),
360 	    (mod_hash_val_t *)&vnic);
361 	if (err == 0) {
362 		rw_exit(&vnic_lock);
363 		return (EEXIST);
364 	}
365 
366 	vnic = kmem_cache_alloc(vnic_cache, KM_NOSLEEP);
367 	if (vnic == NULL) {
368 		rw_exit(&vnic_lock);
369 		return (ENOMEM);
370 	}
371 
372 	bzero(vnic, sizeof (*vnic));
373 
374 	vnic->vn_ls = LINK_STATE_UNKNOWN;
375 	vnic->vn_id = vnic_id;
376 	vnic->vn_link_id = linkid;
377 	vnic->vn_vrid = vrid;
378 	vnic->vn_af = af;
379 
380 	if (!is_anchor) {
381 		if (linkid == DATALINK_INVALID_LINKID) {
382 			err = EINVAL;
383 			goto bail;
384 		}
385 
386 		/*
387 		 * Open the lower MAC and assign its initial bandwidth and
388 		 * MAC address. We do this here during VNIC creation and
389 		 * do not wait until the upper MAC client open so that we
390 		 * can validate the VNIC creation parameters (bandwidth,
391 		 * MAC address, etc) and reserve a factory MAC address if
392 		 * one was requested.
393 		 */
394 		err = mac_open_by_linkid(linkid, &vnic->vn_lower_mh);
395 		if (err != 0)
396 			goto bail;
397 
398 		/*
399 		 * VNIC(vlan) over VNICs(vlans) is not supported.
400 		 */
401 		if (mac_is_vnic(vnic->vn_lower_mh)) {
402 			err = EINVAL;
403 			goto bail;
404 		}
405 
406 		/* only ethernet support for now */
407 		minfop = mac_info(vnic->vn_lower_mh);
408 		if (minfop->mi_nativemedia != DL_ETHER) {
409 			err = ENOTSUP;
410 			goto bail;
411 		}
412 
413 		(void) dls_mgmt_get_linkinfo(vnic_id, vnic_name, NULL, NULL,
414 		    NULL);
415 		err = mac_client_open(vnic->vn_lower_mh, &vnic->vn_mch,
416 		    vnic_name, MAC_OPEN_FLAGS_IS_VNIC);
417 		if (err != 0)
418 			goto bail;
419 
420 		/* assign a MAC address to the VNIC */
421 
422 		err = vnic_unicast_add(vnic, *vnic_addr_type, mac_slot,
423 		    mac_prefix_len, mac_len, mac_addr, flags, diag, vid,
424 		    req_hwgrp_flag);
425 		if (err != 0) {
426 			vnic->vn_muh = NULL;
427 			if (diag != NULL && req_hwgrp_flag)
428 				*diag = VNIC_IOC_DIAG_NO_HWRINGS;
429 			goto bail;
430 		}
431 
432 		/* register to receive notification from underlying MAC */
433 		vnic->vn_mnh = mac_notify_add(vnic->vn_lower_mh, vnic_notify_cb,
434 		    vnic);
435 
436 		*vnic_addr_type = vnic->vn_addr_type;
437 		vnic->vn_addr_len = *mac_len;
438 		vnic->vn_vid = vid;
439 
440 		bcopy(mac_addr, vnic->vn_addr, vnic->vn_addr_len);
441 
442 		if (vnic->vn_addr_type == VNIC_MAC_ADDR_TYPE_FACTORY)
443 			vnic->vn_slot_id = *mac_slot;
444 
445 		/*
446 		 * Set the initial VNIC capabilities. If the VNIC is created
447 		 * over MACs which does not support nactive vlan, disable
448 		 * VNIC's hardware checksum capability if its VID is not 0,
449 		 * since the underlying MAC would get the hardware checksum
450 		 * offset wrong in case of VLAN packets.
451 		 */
452 		if (vid == 0 || !mac_capab_get(vnic->vn_lower_mh,
453 		    MAC_CAPAB_NO_NATIVEVLAN, NULL)) {
454 			if (!mac_capab_get(vnic->vn_lower_mh, MAC_CAPAB_HCKSUM,
455 			    &vnic->vn_hcksum_txflags))
456 				vnic->vn_hcksum_txflags = 0;
457 		} else {
458 			vnic->vn_hcksum_txflags = 0;
459 		}
460 	}
461 
462 	/* register with the MAC module */
463 	if ((mac = mac_alloc(MAC_VERSION)) == NULL)
464 		goto bail;
465 
466 	mac->m_type_ident = MAC_PLUGIN_IDENT_ETHER;
467 	mac->m_driver = vnic;
468 	mac->m_dip = vnic_get_dip();
469 	mac->m_instance = (uint_t)-1;
470 	mac->m_src_addr = vnic->vn_addr;
471 	mac->m_callbacks = &vnic_m_callbacks;
472 
473 	if (!is_anchor) {
474 		/*
475 		 * If this is a VNIC based VLAN, then we check for the
476 		 * margin unless it has been created with the force
477 		 * flag. If we are configuring a VLAN over an etherstub,
478 		 * we don't check the margin even if force is not set.
479 		 */
480 		if (vid == 0 || (flags & VNIC_IOC_CREATE_FORCE) != 0) {
481 			if (vid != VLAN_ID_NONE)
482 				vnic->vn_force = B_TRUE;
483 			/*
484 			 * As the current margin size of the underlying mac is
485 			 * used to determine the margin size of the VNIC
486 			 * itself, request the underlying mac not to change
487 			 * to a smaller margin size.
488 			 */
489 			err = mac_margin_add(vnic->vn_lower_mh,
490 			    &vnic->vn_margin, B_TRUE);
491 			ASSERT(err == 0);
492 		} else {
493 			vnic->vn_margin = VLAN_TAGSZ;
494 			err = mac_margin_add(vnic->vn_lower_mh,
495 			    &vnic->vn_margin, B_FALSE);
496 			if (err != 0) {
497 				mac_free(mac);
498 				if (diag != NULL)
499 					*diag = VNIC_IOC_DIAG_MACMARGIN_INVALID;
500 				goto bail;
501 			}
502 		}
503 
504 		mac_sdu_get(vnic->vn_lower_mh, &mac->m_min_sdu,
505 		    &mac->m_max_sdu);
506 		err = mac_mtu_add(vnic->vn_lower_mh, &mac->m_max_sdu, B_FALSE);
507 		if (err != 0) {
508 			VERIFY(mac_margin_remove(vnic->vn_lower_mh,
509 			    vnic->vn_margin) == 0);
510 			mac_free(mac);
511 			if (diag != NULL)
512 				*diag = VNIC_IOC_DIAG_MACMTU_INVALID;
513 			goto bail;
514 		}
515 		vnic->vn_mtu = mac->m_max_sdu;
516 	} else {
517 		vnic->vn_margin = VLAN_TAGSZ;
518 		mac->m_min_sdu = 1;
519 		mac->m_max_sdu = ANCHOR_VNIC_MAX_MTU;
520 		vnic->vn_mtu = ANCHOR_VNIC_MAX_MTU;
521 	}
522 
523 	mac->m_margin = vnic->vn_margin;
524 
525 	err = mac_register(mac, &vnic->vn_mh);
526 	mac_free(mac);
527 	if (err != 0) {
528 		if (!is_anchor) {
529 			VERIFY(mac_mtu_remove(vnic->vn_lower_mh,
530 			    vnic->vn_mtu) == 0);
531 			VERIFY(mac_margin_remove(vnic->vn_lower_mh,
532 			    vnic->vn_margin) == 0);
533 		}
534 		goto bail;
535 	}
536 
537 	/* Set the VNIC's MAC in the client */
538 	if (!is_anchor) {
539 		mac_set_upper_mac(vnic->vn_mch, vnic->vn_mh, mrp);
540 
541 		if (mrp != NULL) {
542 			if ((mrp->mrp_mask & MRP_RX_RINGS) != 0 ||
543 			    (mrp->mrp_mask & MRP_TX_RINGS) != 0) {
544 				req_hwgrp_flag = B_TRUE;
545 			}
546 			err = mac_client_set_resources(vnic->vn_mch, mrp);
547 			if (err != 0) {
548 				VERIFY(mac_mtu_remove(vnic->vn_lower_mh,
549 				    vnic->vn_mtu) == 0);
550 				VERIFY(mac_margin_remove(vnic->vn_lower_mh,
551 				    vnic->vn_margin) == 0);
552 				(void) mac_unregister(vnic->vn_mh);
553 				goto bail;
554 			}
555 		}
556 	}
557 
558 	err = dls_devnet_create(vnic->vn_mh, vnic->vn_id, crgetzoneid(credp));
559 	if (err != 0) {
560 		VERIFY(is_anchor || mac_margin_remove(vnic->vn_lower_mh,
561 		    vnic->vn_margin) == 0);
562 		if (!is_anchor) {
563 			VERIFY(mac_mtu_remove(vnic->vn_lower_mh,
564 			    vnic->vn_mtu) == 0);
565 			VERIFY(mac_margin_remove(vnic->vn_lower_mh,
566 			    vnic->vn_margin) == 0);
567 		}
568 		(void) mac_unregister(vnic->vn_mh);
569 		goto bail;
570 	}
571 
572 	/* add new VNIC to hash table */
573 	err = mod_hash_insert(vnic_hash, VNIC_HASH_KEY(vnic_id),
574 	    (mod_hash_val_t)vnic);
575 	ASSERT(err == 0);
576 	vnic_count++;
577 
578 	/*
579 	 * Now that we've enabled this VNIC, we should go through and update the
580 	 * link state by setting it to our parents.
581 	 */
582 	vnic->vn_enabled = B_TRUE;
583 
584 	if (is_anchor) {
585 		vnic->vn_ls = LINK_STATE_UP;
586 	} else {
587 		vnic->vn_ls = mac_client_stat_get(vnic->vn_mch,
588 		    MAC_STAT_LINK_STATE);
589 	}
590 	mac_link_update(vnic->vn_mh, vnic->vn_ls);
591 
592 	rw_exit(&vnic_lock);
593 
594 	return (0);
595 
596 bail:
597 	rw_exit(&vnic_lock);
598 	if (!is_anchor) {
599 		if (vnic->vn_mnh != NULL)
600 			(void) mac_notify_remove(vnic->vn_mnh, B_TRUE);
601 		if (vnic->vn_muh != NULL)
602 			(void) mac_unicast_remove(vnic->vn_mch, vnic->vn_muh);
603 		if (vnic->vn_mch != NULL)
604 			mac_client_close(vnic->vn_mch, MAC_CLOSE_FLAGS_IS_VNIC);
605 		if (vnic->vn_lower_mh != NULL)
606 			mac_close(vnic->vn_lower_mh);
607 	}
608 
609 	kmem_cache_free(vnic_cache, vnic);
610 	return (err);
611 }
612 
613 /*
614  * Modify the properties of an existing VNIC.
615  */
616 /* ARGSUSED */
617 int
618 vnic_dev_modify(datalink_id_t vnic_id, uint_t modify_mask,
619     vnic_mac_addr_type_t mac_addr_type, uint_t mac_len, uchar_t *mac_addr,
620     uint_t mac_slot, mac_resource_props_t *mrp)
621 {
622 	vnic_t *vnic = NULL;
623 
624 	rw_enter(&vnic_lock, RW_WRITER);
625 
626 	if (mod_hash_find(vnic_hash, VNIC_HASH_KEY(vnic_id),
627 	    (mod_hash_val_t *)&vnic) != 0) {
628 		rw_exit(&vnic_lock);
629 		return (ENOENT);
630 	}
631 
632 	rw_exit(&vnic_lock);
633 
634 	return (0);
635 }
636 
637 /* ARGSUSED */
638 int
639 vnic_dev_delete(datalink_id_t vnic_id, uint32_t flags, cred_t *credp)
640 {
641 	vnic_t *vnic = NULL;
642 	mod_hash_val_t val;
643 	datalink_id_t tmpid;
644 	int rc;
645 
646 	rw_enter(&vnic_lock, RW_WRITER);
647 
648 	if (mod_hash_find(vnic_hash, VNIC_HASH_KEY(vnic_id),
649 	    (mod_hash_val_t *)&vnic) != 0) {
650 		rw_exit(&vnic_lock);
651 		return (ENOENT);
652 	}
653 
654 	if ((rc = dls_devnet_destroy(vnic->vn_mh, &tmpid, B_TRUE)) != 0) {
655 		rw_exit(&vnic_lock);
656 		return (rc);
657 	}
658 
659 	ASSERT(vnic_id == tmpid);
660 
661 	/*
662 	 * We cannot unregister the MAC yet. Unregistering would
663 	 * free up mac_impl_t which should not happen at this time.
664 	 * So disable mac_impl_t by calling mac_disable(). This will prevent
665 	 * any new claims on mac_impl_t.
666 	 */
667 	if ((rc = mac_disable(vnic->vn_mh)) != 0) {
668 		(void) dls_devnet_create(vnic->vn_mh, vnic_id,
669 		    crgetzoneid(credp));
670 		rw_exit(&vnic_lock);
671 		return (rc);
672 	}
673 
674 	vnic_cleanup_secondary_macs(vnic, vnic->vn_nhandles);
675 
676 	vnic->vn_enabled = B_FALSE;
677 	(void) mod_hash_remove(vnic_hash, VNIC_HASH_KEY(vnic_id), &val);
678 	ASSERT(vnic == (vnic_t *)val);
679 	vnic_count--;
680 	rw_exit(&vnic_lock);
681 
682 	/*
683 	 * XXX-nicolas shouldn't have a void cast here, if it's
684 	 * expected that the function will never fail, then we should
685 	 * have an ASSERT().
686 	 */
687 	(void) mac_unregister(vnic->vn_mh);
688 
689 	if (vnic->vn_lower_mh != NULL) {
690 		/*
691 		 * Check if MAC address for the vnic was obtained from the
692 		 * factory MAC addresses. If yes, release it.
693 		 */
694 		if (vnic->vn_addr_type == VNIC_MAC_ADDR_TYPE_FACTORY) {
695 			(void) mac_addr_factory_release(vnic->vn_mch,
696 			    vnic->vn_slot_id);
697 		}
698 		(void) mac_margin_remove(vnic->vn_lower_mh, vnic->vn_margin);
699 		(void) mac_mtu_remove(vnic->vn_lower_mh, vnic->vn_mtu);
700 		(void) mac_notify_remove(vnic->vn_mnh, B_TRUE);
701 		(void) mac_unicast_remove(vnic->vn_mch, vnic->vn_muh);
702 		mac_client_close(vnic->vn_mch, MAC_CLOSE_FLAGS_IS_VNIC);
703 		mac_close(vnic->vn_lower_mh);
704 	}
705 
706 	kmem_cache_free(vnic_cache, vnic);
707 	return (0);
708 }
709 
710 /* ARGSUSED */
711 mblk_t *
712 vnic_m_tx(void *arg, mblk_t *mp_chain)
713 {
714 	/*
715 	 * This function could be invoked for an anchor VNIC when sending
716 	 * broadcast and multicast packets, and unicast packets which did
717 	 * not match any local known destination.
718 	 */
719 	freemsgchain(mp_chain);
720 	return (NULL);
721 }
722 
723 /*ARGSUSED*/
724 static void
725 vnic_m_ioctl(void *arg, queue_t *q, mblk_t *mp)
726 {
727 	miocnak(q, mp, 0, ENOTSUP);
728 }
729 
730 /*
731  * This entry point cannot be passed-through, since it is invoked
732  * for the per-VNIC kstats which must be exported independently
733  * of the existence of VNIC MAC clients.
734  */
735 static int
736 vnic_m_stat(void *arg, uint_t stat, uint64_t *val)
737 {
738 	vnic_t *vnic = arg;
739 	int rval = 0;
740 
741 	if (vnic->vn_lower_mh == NULL) {
742 		/*
743 		 * It's an anchor VNIC, which does not have any
744 		 * statistics in itself.
745 		 */
746 		return (ENOTSUP);
747 	}
748 
749 	/*
750 	 * ENOTSUP must be reported for unsupported stats, the VNIC
751 	 * driver reports a subset of the stats that would
752 	 * be returned by a real piece of hardware.
753 	 */
754 
755 	switch (stat) {
756 	case MAC_STAT_LINK_STATE:
757 	case MAC_STAT_LINK_UP:
758 	case MAC_STAT_PROMISC:
759 	case MAC_STAT_IFSPEED:
760 	case MAC_STAT_MULTIRCV:
761 	case MAC_STAT_MULTIXMT:
762 	case MAC_STAT_BRDCSTRCV:
763 	case MAC_STAT_BRDCSTXMT:
764 	case MAC_STAT_OPACKETS:
765 	case MAC_STAT_OBYTES:
766 	case MAC_STAT_IERRORS:
767 	case MAC_STAT_OERRORS:
768 	case MAC_STAT_RBYTES:
769 	case MAC_STAT_IPACKETS:
770 		*val = mac_client_stat_get(vnic->vn_mch, stat);
771 		break;
772 	default:
773 		rval = ENOTSUP;
774 	}
775 
776 	return (rval);
777 }
778 
779 /*
780  * Invoked by the upper MAC to retrieve the lower MAC client handle
781  * corresponding to a VNIC. A pointer to this function is obtained
782  * by the upper MAC via capability query.
783  *
784  * XXX-nicolas Note: this currently causes all VNIC MAC clients to
785  * receive the same MAC client handle for the same VNIC. This is ok
786  * as long as we have only one VNIC MAC client which sends and
787  * receives data, but we don't currently enforce this at the MAC layer.
788  */
789 static void *
790 vnic_mac_client_handle(void *vnic_arg)
791 {
792 	vnic_t *vnic = vnic_arg;
793 
794 	return (vnic->vn_mch);
795 }
796 
797 /*
798  * Invoked when updating the primary MAC so that the secondary MACs are
799  * kept in sync.
800  */
801 static void
802 vnic_mac_secondary_update(void *vnic_arg)
803 {
804 	vnic_t *vn = vnic_arg;
805 	int i;
806 
807 	for (i = 1; i <= vn->vn_nhandles; i++) {
808 		mac_secondary_dup(vn->vn_mc_handles[0], vn->vn_mc_handles[i]);
809 	}
810 }
811 
812 /*
813  * Return information about the specified capability.
814  */
815 /* ARGSUSED */
816 static boolean_t
817 vnic_m_capab_get(void *arg, mac_capab_t cap, void *cap_data)
818 {
819 	vnic_t *vnic = arg;
820 
821 	switch (cap) {
822 	case MAC_CAPAB_HCKSUM: {
823 		uint32_t *hcksum_txflags = cap_data;
824 
825 		*hcksum_txflags = vnic->vn_hcksum_txflags &
826 		    (HCKSUM_INET_FULL_V4 | HCKSUM_IPHDRCKSUM |
827 		    HCKSUM_INET_PARTIAL);
828 		break;
829 	}
830 	case MAC_CAPAB_VNIC: {
831 		mac_capab_vnic_t *vnic_capab = cap_data;
832 
833 		if (vnic->vn_lower_mh == NULL) {
834 			/*
835 			 * It's an anchor VNIC, we don't have an underlying
836 			 * NIC and MAC client handle.
837 			 */
838 			return (B_FALSE);
839 		}
840 
841 		if (vnic_capab != NULL) {
842 			vnic_capab->mcv_arg = vnic;
843 			vnic_capab->mcv_mac_client_handle =
844 			    vnic_mac_client_handle;
845 			vnic_capab->mcv_mac_secondary_update =
846 			    vnic_mac_secondary_update;
847 		}
848 		break;
849 	}
850 	case MAC_CAPAB_ANCHOR_VNIC: {
851 		/* since it's an anchor VNIC we don't have lower mac handle */
852 		if (vnic->vn_lower_mh == NULL) {
853 			ASSERT(vnic->vn_link_id == 0);
854 			return (B_TRUE);
855 		}
856 		return (B_FALSE);
857 	}
858 	case MAC_CAPAB_NO_NATIVEVLAN:
859 		return (B_FALSE);
860 	case MAC_CAPAB_NO_ZCOPY:
861 		return (B_TRUE);
862 	case MAC_CAPAB_VRRP: {
863 		mac_capab_vrrp_t *vrrp_capab = cap_data;
864 
865 		if (vnic->vn_vrid != 0) {
866 			if (vrrp_capab != NULL)
867 				vrrp_capab->mcv_af = vnic->vn_af;
868 			return (B_TRUE);
869 		}
870 		return (B_FALSE);
871 	}
872 	default:
873 		return (B_FALSE);
874 	}
875 	return (B_TRUE);
876 }
877 
878 /* ARGSUSED */
879 static int
880 vnic_m_start(void *arg)
881 {
882 	return (0);
883 }
884 
885 /* ARGSUSED */
886 static void
887 vnic_m_stop(void *arg)
888 {
889 }
890 
891 /* ARGSUSED */
892 static int
893 vnic_m_promisc(void *arg, boolean_t on)
894 {
895 	return (0);
896 }
897 
898 /* ARGSUSED */
899 static int
900 vnic_m_multicst(void *arg, boolean_t add, const uint8_t *addrp)
901 {
902 	return (0);
903 }
904 
905 static int
906 vnic_m_unicst(void *arg, const uint8_t *macaddr)
907 {
908 	vnic_t *vnic = arg;
909 
910 	return (mac_vnic_unicast_set(vnic->vn_mch, macaddr));
911 }
912 
913 static void
914 vnic_cleanup_secondary_macs(vnic_t *vn, int cnt)
915 {
916 	int i;
917 
918 	/* Remove existing secondaries (primary is at 0) */
919 	for (i = 1; i <= cnt; i++) {
920 		mac_rx_clear(vn->vn_mc_handles[i]);
921 
922 		/* unicast handle might not have been set yet */
923 		if (vn->vn_mu_handles[i] != NULL)
924 			(void) mac_unicast_remove(vn->vn_mc_handles[i],
925 			    vn->vn_mu_handles[i]);
926 
927 		mac_secondary_cleanup(vn->vn_mc_handles[i]);
928 
929 		mac_client_close(vn->vn_mc_handles[i], MAC_CLOSE_FLAGS_IS_VNIC);
930 
931 		vn->vn_mu_handles[i] = NULL;
932 		vn->vn_mc_handles[i] = NULL;
933 	}
934 
935 	vn->vn_nhandles = 0;
936 }
937 
938 /*
939  * Setup secondary MAC addresses on the vnic. Due to limitations in the mac
940  * code, each mac address must be associated with a mac_client (and the
941  * flow that goes along with the client) so we need to create those clients
942  * here.
943  */
944 static int
945 vnic_set_secondary_macs(vnic_t *vn, mac_secondary_addr_t *msa)
946 {
947 	int i, err;
948 	char primary_name[MAXNAMELEN];
949 
950 	/* First, remove pre-existing secondaries */
951 	ASSERT(vn->vn_nhandles < MPT_MAXMACADDR);
952 	vnic_cleanup_secondary_macs(vn, vn->vn_nhandles);
953 
954 	if (msa->ms_addrcnt == (uint32_t)-1)
955 		msa->ms_addrcnt = 0;
956 
957 	vn->vn_nhandles = msa->ms_addrcnt;
958 
959 	(void) dls_mgmt_get_linkinfo(vn->vn_id, primary_name, NULL, NULL, NULL);
960 
961 	/*
962 	 * Now add the new secondary MACs
963 	 * Recall that the primary MAC address is the first element.
964 	 * The secondary clients are named after the primary with their
965 	 * index to distinguish them.
966 	 */
967 	for (i = 1; i <= vn->vn_nhandles; i++) {
968 		uint8_t *addr;
969 		mac_diag_t mac_diag;
970 		char secondary_name[MAXNAMELEN];
971 
972 		(void) snprintf(secondary_name, sizeof (secondary_name),
973 		    "%s%02d", primary_name, i);
974 
975 		err = mac_client_open(vn->vn_lower_mh, &vn->vn_mc_handles[i],
976 		    secondary_name, MAC_OPEN_FLAGS_IS_VNIC);
977 		if (err != 0) {
978 			/* Remove any that we successfully added */
979 			vnic_cleanup_secondary_macs(vn, --i);
980 			return (err);
981 		}
982 
983 		/*
984 		 * Assign a MAC address to the VNIC
985 		 *
986 		 * Normally this would be done with vnic_unicast_add but since
987 		 * we know these are fixed adddresses, and since we need to
988 		 * save this in the proper array slot, we bypass that function
989 		 * and go direct.
990 		 */
991 		addr = msa->ms_addrs[i - 1];
992 		err = mac_unicast_add(vn->vn_mc_handles[i], addr, 0,
993 		    &vn->vn_mu_handles[i], vn->vn_vid, &mac_diag);
994 		if (err != 0) {
995 			/* Remove any that we successfully added */
996 			vnic_cleanup_secondary_macs(vn, i);
997 			return (err);
998 		}
999 
1000 		/*
1001 		 * Setup the secondary the same way as the primary (i.e.
1002 		 * receiver function/argument (e.g. i_dls_link_rx, mac_pkt_drop,
1003 		 * etc.), the promisc list, and the resource controls).
1004 		 */
1005 		mac_secondary_dup(vn->vn_mc_handles[0], vn->vn_mc_handles[i]);
1006 	}
1007 
1008 	return (0);
1009 }
1010 
1011 static int
1012 vnic_get_secondary_macs(vnic_t *vn, uint_t pr_valsize, void *pr_val)
1013 {
1014 	int i;
1015 	mac_secondary_addr_t msa;
1016 
1017 	if (pr_valsize < sizeof (msa))
1018 		return (EINVAL);
1019 
1020 	/* Get existing addresses (primary is at 0) */
1021 	ASSERT(vn->vn_nhandles < MPT_MAXMACADDR);
1022 	for (i = 1; i <= vn->vn_nhandles; i++) {
1023 		ASSERT(vn->vn_mc_handles[i] != NULL);
1024 		mac_unicast_secondary_get(vn->vn_mc_handles[i],
1025 		    msa.ms_addrs[i - 1]);
1026 	}
1027 	msa.ms_addrcnt = vn->vn_nhandles;
1028 
1029 	bcopy(&msa, pr_val, sizeof (msa));
1030 	return (0);
1031 }
1032 
1033 /*
1034  * Callback functions for set/get of properties
1035  */
1036 /*ARGSUSED*/
1037 static int
1038 vnic_m_setprop(void *m_driver, const char *pr_name, mac_prop_id_t pr_num,
1039     uint_t pr_valsize, const void *pr_val)
1040 {
1041 	int		err = 0;
1042 	vnic_t		*vn = m_driver;
1043 
1044 	switch (pr_num) {
1045 	case MAC_PROP_MTU: {
1046 		uint32_t	mtu;
1047 
1048 		if (pr_valsize < sizeof (mtu)) {
1049 			err = EINVAL;
1050 			break;
1051 		}
1052 		bcopy(pr_val, &mtu, sizeof (mtu));
1053 
1054 		if (vn->vn_link_id == DATALINK_INVALID_LINKID) {
1055 			if (mtu < ANCHOR_VNIC_MIN_MTU ||
1056 			    mtu > ANCHOR_VNIC_MAX_MTU) {
1057 				err = EINVAL;
1058 				break;
1059 			}
1060 		} else {
1061 			err = mac_mtu_add(vn->vn_lower_mh, &mtu, B_FALSE);
1062 			/*
1063 			 * If it's not supported to set a value here, translate
1064 			 * that to EINVAL, so user land gets a better idea of
1065 			 * what went wrong. This realistically means that they
1066 			 * violated the output of prop info.
1067 			 */
1068 			if (err == ENOTSUP)
1069 				err = EINVAL;
1070 			if (err != 0)
1071 				break;
1072 			VERIFY(mac_mtu_remove(vn->vn_lower_mh,
1073 			    vn->vn_mtu) == 0);
1074 		}
1075 		vn->vn_mtu = mtu;
1076 		err = mac_maxsdu_update(vn->vn_mh, mtu);
1077 		break;
1078 	}
1079 	case MAC_PROP_VN_PROMISC_FILTERED: {
1080 		boolean_t filtered;
1081 
1082 		if (pr_valsize < sizeof (filtered)) {
1083 			err = EINVAL;
1084 			break;
1085 		}
1086 
1087 		bcopy(pr_val, &filtered, sizeof (filtered));
1088 		mac_set_promisc_filtered(vn->vn_mch, filtered);
1089 		break;
1090 	}
1091 	case MAC_PROP_SECONDARY_ADDRS: {
1092 		mac_secondary_addr_t msa;
1093 
1094 		bcopy(pr_val, &msa, sizeof (msa));
1095 		err = vnic_set_secondary_macs(vn, &msa);
1096 		break;
1097 	}
1098 	case MAC_PROP_PRIVATE: {
1099 		long val, i;
1100 		const char *v;
1101 
1102 		if (vn->vn_link_id != DATALINK_INVALID_LINKID ||
1103 		    strcmp(pr_name, "_linkstate") != 0) {
1104 			err = ENOTSUP;
1105 			break;
1106 		}
1107 
1108 		for (v = pr_val, i = 0; i < pr_valsize; i++, v++) {
1109 			if (*v == '\0')
1110 				break;
1111 		}
1112 		if (i == pr_valsize) {
1113 			err = EINVAL;
1114 			break;
1115 		}
1116 
1117 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &val);
1118 		if (val != LINK_STATE_UP && val != LINK_STATE_DOWN) {
1119 			err = EINVAL;
1120 			break;
1121 		}
1122 		vn->vn_ls = val;
1123 		mac_link_update(vn->vn_mh, vn->vn_ls);
1124 		break;
1125 	}
1126 	default:
1127 		err = ENOTSUP;
1128 		break;
1129 	}
1130 	return (err);
1131 }
1132 
1133 /* ARGSUSED */
1134 static int
1135 vnic_m_getprop(void *arg, const char *pr_name, mac_prop_id_t pr_num,
1136     uint_t pr_valsize, void *pr_val)
1137 {
1138 	vnic_t		*vn = arg;
1139 	int		ret = 0;
1140 	boolean_t	out;
1141 
1142 	switch (pr_num) {
1143 	case MAC_PROP_VN_PROMISC_FILTERED:
1144 		out = mac_get_promisc_filtered(vn->vn_mch);
1145 		ASSERT(pr_valsize >= sizeof (boolean_t));
1146 		bcopy(&out, pr_val, sizeof (boolean_t));
1147 		break;
1148 	case MAC_PROP_SECONDARY_ADDRS:
1149 		ret = vnic_get_secondary_macs(vn, pr_valsize, pr_val);
1150 		break;
1151 	case MAC_PROP_PRIVATE:
1152 		if (vn->vn_link_id != DATALINK_INVALID_LINKID) {
1153 			ret = EINVAL;
1154 			break;
1155 		}
1156 
1157 		if (strcmp(pr_name, "_linkstate") != 0) {
1158 			ret = EINVAL;
1159 			break;
1160 		}
1161 		(void) snprintf(pr_val, pr_valsize, "%d", vn->vn_ls);
1162 		break;
1163 	default:
1164 		ret = ENOTSUP;
1165 		break;
1166 	}
1167 
1168 	return (ret);
1169 }
1170 
1171 /* ARGSUSED */
1172 static void
1173 vnic_m_propinfo(void *m_driver, const char *pr_name,
1174     mac_prop_id_t pr_num, mac_prop_info_handle_t prh)
1175 {
1176 	vnic_t		*vn = m_driver;
1177 
1178 	switch (pr_num) {
1179 	case MAC_PROP_MTU:
1180 		if (vn->vn_link_id == DATALINK_INVALID_LINKID) {
1181 			mac_prop_info_set_range_uint32(prh,
1182 			    ANCHOR_VNIC_MIN_MTU, ANCHOR_VNIC_MAX_MTU);
1183 		} else {
1184 			uint32_t		max;
1185 			mac_perim_handle_t	mph;
1186 			mac_propval_range_t	range;
1187 
1188 			/*
1189 			 * The valid range for a VNIC's MTU is the minimum that
1190 			 * the device supports and the current value of the
1191 			 * device. A VNIC cannot increase the current MTU of the
1192 			 * device. Therefore we need to get the range from the
1193 			 * propinfo endpoint and current mtu from the
1194 			 * traditional property endpoint.
1195 			 */
1196 			mac_perim_enter_by_mh(vn->vn_lower_mh, &mph);
1197 			if (mac_get_prop(vn->vn_lower_mh, MAC_PROP_MTU, "mtu",
1198 			    &max, sizeof (uint32_t)) != 0) {
1199 				mac_perim_exit(mph);
1200 				return;
1201 			}
1202 
1203 			range.mpr_count = 1;
1204 			if (mac_prop_info(vn->vn_lower_mh, MAC_PROP_MTU, "mtu",
1205 			    NULL, 0, &range, NULL) != 0) {
1206 				mac_perim_exit(mph);
1207 				return;
1208 			}
1209 
1210 			mac_prop_info_set_default_uint32(prh, max);
1211 			mac_prop_info_set_range_uint32(prh,
1212 			    range.mpr_range_uint32[0].mpur_min, max);
1213 			mac_perim_exit(mph);
1214 		}
1215 		break;
1216 	case MAC_PROP_PRIVATE:
1217 		if (vn->vn_link_id != DATALINK_INVALID_LINKID)
1218 			break;
1219 
1220 		if (strcmp(pr_name, "_linkstate") == 0) {
1221 			char buf[16];
1222 
1223 			mac_prop_info_set_perm(prh, MAC_PROP_PERM_RW);
1224 			(void) snprintf(buf, sizeof (buf), "%d", vn->vn_ls);
1225 			mac_prop_info_set_default_str(prh, buf);
1226 		}
1227 		break;
1228 	}
1229 }
1230 
1231 
1232 int
1233 vnic_info(vnic_info_t *info, cred_t *credp)
1234 {
1235 	vnic_t		*vnic;
1236 	int		err;
1237 
1238 	/* Make sure that the VNIC link is visible from the caller's zone. */
1239 	if (!dls_devnet_islinkvisible(info->vn_vnic_id, crgetzoneid(credp)))
1240 		return (ENOENT);
1241 
1242 	rw_enter(&vnic_lock, RW_WRITER);
1243 
1244 	err = mod_hash_find(vnic_hash, VNIC_HASH_KEY(info->vn_vnic_id),
1245 	    (mod_hash_val_t *)&vnic);
1246 	if (err != 0) {
1247 		rw_exit(&vnic_lock);
1248 		return (ENOENT);
1249 	}
1250 
1251 	info->vn_link_id = vnic->vn_link_id;
1252 	info->vn_mac_addr_type = vnic->vn_addr_type;
1253 	info->vn_mac_len = vnic->vn_addr_len;
1254 	bcopy(vnic->vn_addr, info->vn_mac_addr, MAXMACADDRLEN);
1255 	info->vn_mac_slot = vnic->vn_slot_id;
1256 	info->vn_mac_prefix_len = 0;
1257 	info->vn_vid = vnic->vn_vid;
1258 	info->vn_force = vnic->vn_force;
1259 	info->vn_vrid = vnic->vn_vrid;
1260 	info->vn_af = vnic->vn_af;
1261 
1262 	bzero(&info->vn_resource_props, sizeof (mac_resource_props_t));
1263 	if (vnic->vn_mch != NULL)
1264 		mac_client_get_resources(vnic->vn_mch,
1265 		    &info->vn_resource_props);
1266 
1267 	rw_exit(&vnic_lock);
1268 	return (0);
1269 }
1270 
1271 static void
1272 vnic_notify_cb(void *arg, mac_notify_type_t type)
1273 {
1274 	vnic_t *vnic = arg;
1275 
1276 	/*
1277 	 * Do not deliver notifications if the vnic is not fully initialized
1278 	 * or is in process of being torn down.
1279 	 */
1280 	if (!vnic->vn_enabled)
1281 		return;
1282 
1283 	switch (type) {
1284 	case MAC_NOTE_UNICST:
1285 		/*
1286 		 * Only the VLAN VNIC needs to be notified with primary MAC
1287 		 * address change.
1288 		 */
1289 		if (vnic->vn_addr_type != VNIC_MAC_ADDR_TYPE_PRIMARY)
1290 			return;
1291 
1292 		/*  the unicast MAC address value */
1293 		mac_unicast_primary_get(vnic->vn_lower_mh, vnic->vn_addr);
1294 
1295 		/* notify its upper layer MAC about MAC address change */
1296 		mac_unicst_update(vnic->vn_mh, (const uint8_t *)vnic->vn_addr);
1297 		break;
1298 
1299 	case MAC_NOTE_LINK:
1300 		vnic->vn_ls = mac_client_stat_get(vnic->vn_mch,
1301 		    MAC_STAT_LINK_STATE);
1302 		mac_link_update(vnic->vn_mh, vnic->vn_ls);
1303 		break;
1304 
1305 	default:
1306 		break;
1307 	}
1308 }
1309