xref: /titanic_51/usr/src/uts/common/io/vnic/vnic_dev.c (revision 63251bc7f1ca38259078c48e316fee4ed66d4e93)
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 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <sys/types.h>
27 #include <sys/sysmacros.h>
28 #include <sys/conf.h>
29 #include <sys/cmn_err.h>
30 #include <sys/list.h>
31 #include <sys/ksynch.h>
32 #include <sys/kmem.h>
33 #include <sys/stream.h>
34 #include <sys/modctl.h>
35 #include <sys/ddi.h>
36 #include <sys/sunddi.h>
37 #include <sys/atomic.h>
38 #include <sys/stat.h>
39 #include <sys/modhash.h>
40 #include <sys/strsubr.h>
41 #include <sys/strsun.h>
42 #include <sys/dlpi.h>
43 #include <sys/mac.h>
44 #include <sys/mac_provider.h>
45 #include <sys/mac_client.h>
46 #include <sys/mac_client_priv.h>
47 #include <sys/mac_ether.h>
48 #include <sys/dls.h>
49 #include <sys/pattr.h>
50 #include <sys/time.h>
51 #include <sys/vlan.h>
52 #include <sys/vnic.h>
53 #include <sys/vnic_impl.h>
54 #include <sys/mac_flow_impl.h>
55 #include <inet/ip_impl.h>
56 
57 /*
58  * Note that for best performance, the VNIC is a passthrough design.
59  * For each VNIC corresponds a MAC client of the underlying MAC (lower MAC).
60  * This MAC client is opened by the VNIC driver at VNIC creation,
61  * and closed when the VNIC is deleted.
62  * When a MAC client of the VNIC itself opens a VNIC, the MAC layer
63  * (upper MAC) detects that the MAC being opened is a VNIC. Instead
64  * of allocating a new MAC client, it asks the VNIC driver to return
65  * the lower MAC client handle associated with the VNIC, and that handle
66  * is returned to the upper MAC client directly. This allows access
67  * by upper MAC clients of the VNIC to have direct access to the lower
68  * MAC client for the control path and data path.
69  *
70  * Due to this passthrough, some of the entry points exported by the
71  * VNIC driver are never directly invoked. These entry points include
72  * vnic_m_start, vnic_m_stop, vnic_m_promisc, vnic_m_multicst, etc.
73  */
74 
75 static int vnic_m_start(void *);
76 static void vnic_m_stop(void *);
77 static int vnic_m_promisc(void *, boolean_t);
78 static int vnic_m_multicst(void *, boolean_t, const uint8_t *);
79 static int vnic_m_unicst(void *, const uint8_t *);
80 static int vnic_m_stat(void *, uint_t, uint64_t *);
81 static void vnic_m_ioctl(void *, queue_t *, mblk_t *);
82 static int vnic_m_setprop(void *, const char *, mac_prop_id_t, uint_t,
83     const void *);
84 static int vnic_m_getprop(void *, const char *, mac_prop_id_t, uint_t,
85     uint_t, void *, uint_t *);
86 static mblk_t *vnic_m_tx(void *, mblk_t *);
87 static boolean_t vnic_m_capab_get(void *, mac_capab_t, void *);
88 static void vnic_notify_cb(void *, mac_notify_type_t);
89 
90 static kmem_cache_t	*vnic_cache;
91 static krwlock_t	vnic_lock;
92 static uint_t		vnic_count;
93 
94 /* hash of VNICs (vnic_t's), keyed by VNIC id */
95 static mod_hash_t	*vnic_hash;
96 #define	VNIC_HASHSZ	64
97 #define	VNIC_HASH_KEY(vnic_id)	((mod_hash_key_t)(uintptr_t)vnic_id)
98 
99 #define	VNIC_M_CALLBACK_FLAGS	\
100 	(MC_IOCTL | MC_GETCAPAB | MC_SETPROP | MC_GETPROP)
101 
102 static mac_callbacks_t vnic_m_callbacks = {
103 	VNIC_M_CALLBACK_FLAGS,
104 	vnic_m_stat,
105 	vnic_m_start,
106 	vnic_m_stop,
107 	vnic_m_promisc,
108 	vnic_m_multicst,
109 	vnic_m_unicst,
110 	vnic_m_tx,
111 	vnic_m_ioctl,
112 	vnic_m_capab_get,
113 	NULL,
114 	NULL,
115 	vnic_m_setprop,
116 	vnic_m_getprop
117 };
118 
119 void
120 vnic_dev_init(void)
121 {
122 	vnic_cache = kmem_cache_create("vnic_cache",
123 	    sizeof (vnic_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
124 
125 	vnic_hash = mod_hash_create_idhash("vnic_hash",
126 	    VNIC_HASHSZ, mod_hash_null_valdtor);
127 
128 	rw_init(&vnic_lock, NULL, RW_DEFAULT, NULL);
129 
130 	vnic_count = 0;
131 }
132 
133 void
134 vnic_dev_fini(void)
135 {
136 	ASSERT(vnic_count == 0);
137 
138 	rw_destroy(&vnic_lock);
139 	mod_hash_destroy_idhash(vnic_hash);
140 	kmem_cache_destroy(vnic_cache);
141 }
142 
143 uint_t
144 vnic_dev_count(void)
145 {
146 	return (vnic_count);
147 }
148 
149 static vnic_ioc_diag_t
150 vnic_mac2vnic_diag(mac_diag_t diag)
151 {
152 	switch (diag) {
153 	case MAC_DIAG_MACADDR_NIC:
154 		return (VNIC_IOC_DIAG_MACADDR_NIC);
155 	case MAC_DIAG_MACADDR_INUSE:
156 		return (VNIC_IOC_DIAG_MACADDR_INUSE);
157 	case MAC_DIAG_MACADDR_INVALID:
158 		return (VNIC_IOC_DIAG_MACADDR_INVALID);
159 	case MAC_DIAG_MACADDRLEN_INVALID:
160 		return (VNIC_IOC_DIAG_MACADDRLEN_INVALID);
161 	case MAC_DIAG_MACFACTORYSLOTINVALID:
162 		return (VNIC_IOC_DIAG_MACFACTORYSLOTINVALID);
163 	case MAC_DIAG_MACFACTORYSLOTUSED:
164 		return (VNIC_IOC_DIAG_MACFACTORYSLOTUSED);
165 	case MAC_DIAG_MACFACTORYSLOTALLUSED:
166 		return (VNIC_IOC_DIAG_MACFACTORYSLOTALLUSED);
167 	case MAC_DIAG_MACFACTORYNOTSUP:
168 		return (VNIC_IOC_DIAG_MACFACTORYNOTSUP);
169 	case MAC_DIAG_MACPREFIX_INVALID:
170 		return (VNIC_IOC_DIAG_MACPREFIX_INVALID);
171 	case MAC_DIAG_MACPREFIXLEN_INVALID:
172 		return (VNIC_IOC_DIAG_MACPREFIXLEN_INVALID);
173 	case MAC_DIAG_MACNO_HWRINGS:
174 		return (VNIC_IOC_DIAG_NO_HWRINGS);
175 	default:
176 		return (VNIC_IOC_DIAG_NONE);
177 	}
178 }
179 
180 static int
181 vnic_unicast_add(vnic_t *vnic, vnic_mac_addr_type_t vnic_addr_type,
182     int *addr_slot, uint_t prefix_len, int *addr_len_ptr_arg,
183     uint8_t *mac_addr_arg, uint16_t flags, vnic_ioc_diag_t *diag,
184     uint16_t vid)
185 {
186 	mac_diag_t mac_diag;
187 	uint16_t mac_flags = 0;
188 	int err;
189 	uint_t addr_len;
190 
191 	if (flags & VNIC_IOC_CREATE_NODUPCHECK)
192 		mac_flags |= MAC_UNICAST_NODUPCHECK;
193 
194 	switch (vnic_addr_type) {
195 	case VNIC_MAC_ADDR_TYPE_FIXED:
196 		/*
197 		 * The MAC address value to assign to the VNIC
198 		 * is already provided in mac_addr_arg. addr_len_ptr_arg
199 		 * already contains the MAC address length.
200 		 */
201 		break;
202 
203 	case VNIC_MAC_ADDR_TYPE_RANDOM:
204 		/*
205 		 * Random MAC address. There are two sub-cases:
206 		 *
207 		 * 1 - If mac_len == 0, a new MAC address is generated.
208 		 *	The length of the MAC address to generated depends
209 		 *	on the type of MAC used. The prefix to use for the MAC
210 		 *	address is stored in the most significant bytes
211 		 *	of the mac_addr argument, and its length is specified
212 		 *	by the mac_prefix_len argument. This prefix can
213 		 *	correspond to a IEEE OUI in the case of Ethernet,
214 		 *	for example.
215 		 *
216 		 * 2 - If mac_len > 0, the address was already picked
217 		 *	randomly, and is now passed back during VNIC
218 		 *	re-creation. The mac_addr argument contains the MAC
219 		 *	address that was generated. We distinguish this
220 		 *	case from the fixed MAC address case, since we
221 		 *	want the user consumers to know, when they query
222 		 *	the list of VNICs, that a VNIC was assigned a
223 		 *	random MAC address vs assigned a fixed address
224 		 *	specified by the user.
225 		 */
226 
227 		/*
228 		 * If it's a pre-generated address, we're done. mac_addr_arg
229 		 * and addr_len_ptr_arg already contain the MAC address
230 		 * value and length.
231 		 */
232 		if (*addr_len_ptr_arg > 0)
233 			break;
234 
235 		/* generate a new random MAC address */
236 		if ((err = mac_addr_random(vnic->vn_mch,
237 		    prefix_len, mac_addr_arg, &mac_diag)) != 0) {
238 			*diag = vnic_mac2vnic_diag(mac_diag);
239 			return (err);
240 		}
241 		*addr_len_ptr_arg = mac_addr_len(vnic->vn_lower_mh);
242 		break;
243 
244 	case VNIC_MAC_ADDR_TYPE_FACTORY:
245 		err = mac_addr_factory_reserve(vnic->vn_mch, addr_slot);
246 		if (err != 0) {
247 			if (err == EINVAL)
248 				*diag = VNIC_IOC_DIAG_MACFACTORYSLOTINVALID;
249 			if (err == EBUSY)
250 				*diag = VNIC_IOC_DIAG_MACFACTORYSLOTUSED;
251 			if (err == ENOSPC)
252 				*diag = VNIC_IOC_DIAG_MACFACTORYSLOTALLUSED;
253 			return (err);
254 		}
255 
256 		mac_addr_factory_value(vnic->vn_lower_mh, *addr_slot,
257 		    mac_addr_arg, &addr_len, NULL, NULL);
258 		*addr_len_ptr_arg = addr_len;
259 		break;
260 
261 	case VNIC_MAC_ADDR_TYPE_AUTO:
262 		/* first try to allocate a factory MAC address */
263 		err = mac_addr_factory_reserve(vnic->vn_mch, addr_slot);
264 		if (err == 0) {
265 			mac_addr_factory_value(vnic->vn_lower_mh, *addr_slot,
266 			    mac_addr_arg, &addr_len, NULL, NULL);
267 			vnic_addr_type = VNIC_MAC_ADDR_TYPE_FACTORY;
268 			*addr_len_ptr_arg = addr_len;
269 			break;
270 		}
271 
272 		/*
273 		 * Allocating a factory MAC address failed, generate a
274 		 * random MAC address instead.
275 		 */
276 		if ((err = mac_addr_random(vnic->vn_mch,
277 		    prefix_len, mac_addr_arg, &mac_diag)) != 0) {
278 			*diag = vnic_mac2vnic_diag(mac_diag);
279 			return (err);
280 		}
281 		*addr_len_ptr_arg = mac_addr_len(vnic->vn_lower_mh);
282 		vnic_addr_type = VNIC_MAC_ADDR_TYPE_RANDOM;
283 		break;
284 	case VNIC_MAC_ADDR_TYPE_PRIMARY:
285 		/*
286 		 * We get the address here since we copy it in the
287 		 * vnic's vn_addr.
288 		 */
289 		mac_unicast_primary_get(vnic->vn_lower_mh, mac_addr_arg);
290 		*addr_len_ptr_arg = mac_addr_len(vnic->vn_lower_mh);
291 		mac_flags |= MAC_UNICAST_VNIC_PRIMARY;
292 		break;
293 	}
294 
295 	vnic->vn_addr_type = vnic_addr_type;
296 
297 	err = mac_unicast_add(vnic->vn_mch, mac_addr_arg, mac_flags,
298 	    &vnic->vn_muh, vid, &mac_diag);
299 	if (err != 0) {
300 		if (vnic_addr_type == VNIC_MAC_ADDR_TYPE_FACTORY) {
301 			/* release factory MAC address */
302 			mac_addr_factory_release(vnic->vn_mch, *addr_slot);
303 		}
304 		*diag = vnic_mac2vnic_diag(mac_diag);
305 	}
306 
307 	return (err);
308 }
309 
310 /*
311  * Create a new VNIC upon request from administrator.
312  * Returns 0 on success, an errno on failure.
313  */
314 /* ARGSUSED */
315 int
316 vnic_dev_create(datalink_id_t vnic_id, datalink_id_t linkid,
317     vnic_mac_addr_type_t *vnic_addr_type, int *mac_len, uchar_t *mac_addr,
318     int *mac_slot, uint_t mac_prefix_len, uint16_t vid,
319     mac_resource_props_t *mrp, uint32_t flags, vnic_ioc_diag_t *diag)
320 {
321 	vnic_t *vnic;
322 	mac_register_t *mac;
323 	int err;
324 	boolean_t is_anchor = ((flags & VNIC_IOC_CREATE_ANCHOR) != 0);
325 	char vnic_name[MAXNAMELEN];
326 	const mac_info_t *minfop;
327 	uint32_t req_hwgrp_flag = ((flags & VNIC_IOC_CREATE_REQ_HWRINGS) != 0) ?
328 	    MAC_OPEN_FLAGS_REQ_HWRINGS : 0;
329 
330 	*diag = VNIC_IOC_DIAG_NONE;
331 
332 	rw_enter(&vnic_lock, RW_WRITER);
333 
334 	/* does a VNIC with the same id already exist? */
335 	err = mod_hash_find(vnic_hash, VNIC_HASH_KEY(vnic_id),
336 	    (mod_hash_val_t *)&vnic);
337 	if (err == 0) {
338 		rw_exit(&vnic_lock);
339 		return (EEXIST);
340 	}
341 
342 	vnic = kmem_cache_alloc(vnic_cache, KM_NOSLEEP);
343 	if (vnic == NULL) {
344 		rw_exit(&vnic_lock);
345 		return (ENOMEM);
346 	}
347 
348 	bzero(vnic, sizeof (*vnic));
349 
350 	vnic->vn_id = vnic_id;
351 	vnic->vn_link_id = linkid;
352 	vnic->vn_started = B_FALSE;
353 
354 	if (!is_anchor) {
355 		if (linkid == DATALINK_INVALID_LINKID) {
356 			err = EINVAL;
357 			goto bail;
358 		}
359 
360 		/*
361 		 * Open the lower MAC and assign its initial bandwidth and
362 		 * MAC address. We do this here during VNIC creation and
363 		 * do not wait until the upper MAC client open so that we
364 		 * can validate the VNIC creation parameters (bandwidth,
365 		 * MAC address, etc) and reserve a factory MAC address if
366 		 * one was requested.
367 		 */
368 		err = mac_open_by_linkid(linkid, &vnic->vn_lower_mh);
369 		if (err != 0)
370 			goto bail;
371 
372 		/*
373 		 * VNIC(vlan) over VNICs(vlans) is not supported.
374 		 */
375 		if (mac_is_vnic(vnic->vn_lower_mh)) {
376 			err = EINVAL;
377 			goto bail;
378 		}
379 
380 		/* only ethernet support for now */
381 		minfop = mac_info(vnic->vn_lower_mh);
382 		if (minfop->mi_nativemedia != DL_ETHER) {
383 			err = ENOTSUP;
384 			goto bail;
385 		}
386 
387 		(void) dls_mgmt_get_linkinfo(vnic_id, vnic_name, NULL, NULL,
388 		    NULL);
389 		err = mac_client_open(vnic->vn_lower_mh, &vnic->vn_mch,
390 		    vnic_name, MAC_OPEN_FLAGS_IS_VNIC | req_hwgrp_flag);
391 		if (err != 0)
392 			goto bail;
393 
394 		if (mrp != NULL) {
395 			err = mac_client_set_resources(vnic->vn_mch, mrp);
396 			if (err != 0)
397 				goto bail;
398 		}
399 		/* assign a MAC address to the VNIC */
400 
401 		err = vnic_unicast_add(vnic, *vnic_addr_type, mac_slot,
402 		    mac_prefix_len, mac_len, mac_addr, flags, diag, vid);
403 		if (err != 0) {
404 			vnic->vn_muh = NULL;
405 			if (diag != NULL && req_hwgrp_flag != 0)
406 				*diag = VNIC_IOC_DIAG_NO_HWRINGS;
407 			goto bail;
408 		}
409 
410 		/* register to receive notification from underlying MAC */
411 		vnic->vn_mnh = mac_notify_add(vnic->vn_lower_mh, vnic_notify_cb,
412 		    vnic);
413 
414 		*vnic_addr_type = vnic->vn_addr_type;
415 		vnic->vn_addr_len = *mac_len;
416 		vnic->vn_vid = vid;
417 
418 		bcopy(mac_addr, vnic->vn_addr, vnic->vn_addr_len);
419 
420 		if (vnic->vn_addr_type == VNIC_MAC_ADDR_TYPE_FACTORY)
421 			vnic->vn_slot_id = *mac_slot;
422 
423 		/* set the initial VNIC capabilities */
424 		if (!mac_capab_get(vnic->vn_lower_mh, MAC_CAPAB_HCKSUM,
425 		    &vnic->vn_hcksum_txflags))
426 			vnic->vn_hcksum_txflags = 0;
427 	}
428 
429 	/* register with the MAC module */
430 	if ((mac = mac_alloc(MAC_VERSION)) == NULL)
431 		goto bail;
432 
433 	mac->m_type_ident = MAC_PLUGIN_IDENT_ETHER;
434 	mac->m_driver = vnic;
435 	mac->m_dip = vnic_get_dip();
436 	mac->m_instance = (uint_t)-1;
437 	mac->m_src_addr = vnic->vn_addr;
438 	mac->m_callbacks = &vnic_m_callbacks;
439 
440 	if (!is_anchor) {
441 		/*
442 		 * If this is a VNIC based VLAN, then we check for the
443 		 * margin unless it has been created with the force
444 		 * flag. If we are configuring a VLAN over an etherstub,
445 		 * we don't check the margin even if force is not set.
446 		 */
447 		if (vid == 0 || (flags & VNIC_IOC_CREATE_FORCE) != 0) {
448 			if (vid != VLAN_ID_NONE)
449 				vnic->vn_force = B_TRUE;
450 			/*
451 			 * As the current margin size of the underlying mac is
452 			 * used to determine the margin size of the VNIC
453 			 * itself, request the underlying mac not to change
454 			 * to a smaller margin size.
455 			 */
456 			err = mac_margin_add(vnic->vn_lower_mh,
457 			    &vnic->vn_margin, B_TRUE);
458 			ASSERT(err == 0);
459 		} else {
460 			vnic->vn_margin = VLAN_TAGSZ;
461 			err = mac_margin_add(vnic->vn_lower_mh,
462 			    &vnic->vn_margin, B_FALSE);
463 			if (err != 0) {
464 				mac_free(mac);
465 				if (diag != NULL)
466 					*diag = VNIC_IOC_DIAG_MACMARGIN_INVALID;
467 				goto bail;
468 			}
469 		}
470 
471 		mac_sdu_get(vnic->vn_lower_mh, &mac->m_min_sdu,
472 		    &mac->m_max_sdu);
473 	} else {
474 		vnic->vn_margin = VLAN_TAGSZ;
475 		mac->m_min_sdu = 0;
476 		mac->m_max_sdu = 9000;
477 	}
478 
479 	mac->m_margin = vnic->vn_margin;
480 
481 	err = mac_register(mac, &vnic->vn_mh);
482 	mac_free(mac);
483 	if (err != 0) {
484 		VERIFY(is_anchor || mac_margin_remove(vnic->vn_lower_mh,
485 		    vnic->vn_margin) == 0);
486 		goto bail;
487 	}
488 
489 	/* Set the VNIC's MAC in the client */
490 	if (!is_anchor)
491 		mac_set_upper_mac(vnic->vn_mch, vnic->vn_mh);
492 
493 	if ((err = dls_devnet_create(vnic->vn_mh, vnic->vn_id)) != 0) {
494 		VERIFY(is_anchor || mac_margin_remove(vnic->vn_lower_mh,
495 		    vnic->vn_margin) == 0);
496 		(void) mac_unregister(vnic->vn_mh);
497 		goto bail;
498 	}
499 
500 	/* add new VNIC to hash table */
501 	err = mod_hash_insert(vnic_hash, VNIC_HASH_KEY(vnic_id),
502 	    (mod_hash_val_t)vnic);
503 	ASSERT(err == 0);
504 	vnic_count++;
505 
506 	rw_exit(&vnic_lock);
507 
508 	return (0);
509 
510 bail:
511 	rw_exit(&vnic_lock);
512 	if (!is_anchor) {
513 		if (vnic->vn_mnh != NULL)
514 			(void) mac_notify_remove(vnic->vn_mnh, B_TRUE);
515 		if (vnic->vn_muh != NULL)
516 			(void) mac_unicast_remove(vnic->vn_mch, vnic->vn_muh);
517 		if (vnic->vn_mch != NULL)
518 			mac_client_close(vnic->vn_mch, MAC_CLOSE_FLAGS_IS_VNIC);
519 		if (vnic->vn_lower_mh != NULL)
520 			mac_close(vnic->vn_lower_mh);
521 	}
522 
523 	kmem_cache_free(vnic_cache, vnic);
524 	return (err);
525 }
526 
527 /*
528  * Modify the properties of an existing VNIC.
529  */
530 /* ARGSUSED */
531 int
532 vnic_dev_modify(datalink_id_t vnic_id, uint_t modify_mask,
533     vnic_mac_addr_type_t mac_addr_type, uint_t mac_len, uchar_t *mac_addr,
534     uint_t mac_slot, mac_resource_props_t *mrp)
535 {
536 	vnic_t *vnic = NULL;
537 
538 	rw_enter(&vnic_lock, RW_WRITER);
539 
540 	if (mod_hash_find(vnic_hash, VNIC_HASH_KEY(vnic_id),
541 	    (mod_hash_val_t *)&vnic) != 0) {
542 		rw_exit(&vnic_lock);
543 		return (ENOENT);
544 	}
545 
546 	rw_exit(&vnic_lock);
547 
548 	return (0);
549 }
550 
551 /* ARGSUSED */
552 int
553 vnic_dev_delete(datalink_id_t vnic_id, uint32_t flags)
554 {
555 	vnic_t *vnic = NULL;
556 	mod_hash_val_t val;
557 	datalink_id_t tmpid;
558 	int rc;
559 
560 	rw_enter(&vnic_lock, RW_WRITER);
561 
562 	if (mod_hash_find(vnic_hash, VNIC_HASH_KEY(vnic_id),
563 	    (mod_hash_val_t *)&vnic) != 0) {
564 		rw_exit(&vnic_lock);
565 		return (ENOENT);
566 	}
567 
568 	if ((rc = dls_devnet_destroy(vnic->vn_mh, &tmpid, B_TRUE)) != 0) {
569 		rw_exit(&vnic_lock);
570 		return (rc);
571 	}
572 
573 	ASSERT(vnic_id == tmpid);
574 
575 	/*
576 	 * We cannot unregister the MAC yet. Unregistering would
577 	 * free up mac_impl_t which should not happen at this time.
578 	 * So disable mac_impl_t by calling mac_disable(). This will prevent
579 	 * any new claims on mac_impl_t.
580 	 */
581 	if ((rc = mac_disable(vnic->vn_mh)) != 0) {
582 		(void) dls_devnet_create(vnic->vn_mh, vnic_id);
583 		rw_exit(&vnic_lock);
584 		return (rc);
585 	}
586 
587 	(void) mod_hash_remove(vnic_hash, VNIC_HASH_KEY(vnic_id), &val);
588 	ASSERT(vnic == (vnic_t *)val);
589 	vnic_count--;
590 	rw_exit(&vnic_lock);
591 
592 	/*
593 	 * XXX-nicolas shouldn't have a void cast here, if it's
594 	 * expected that the function will never fail, then we should
595 	 * have an ASSERT().
596 	 */
597 	(void) mac_unregister(vnic->vn_mh);
598 
599 	if (vnic->vn_lower_mh != NULL) {
600 		/*
601 		 * Check if MAC address for the vnic was obtained from the
602 		 * factory MAC addresses. If yes, release it.
603 		 */
604 		if (vnic->vn_addr_type == VNIC_MAC_ADDR_TYPE_FACTORY) {
605 			(void) mac_addr_factory_release(vnic->vn_mch,
606 			    vnic->vn_slot_id);
607 		}
608 		(void) mac_margin_remove(vnic->vn_lower_mh, vnic->vn_margin);
609 		(void) mac_notify_remove(vnic->vn_mnh, B_TRUE);
610 		(void) mac_unicast_remove(vnic->vn_mch, vnic->vn_muh);
611 		mac_client_close(vnic->vn_mch, MAC_CLOSE_FLAGS_IS_VNIC);
612 		mac_close(vnic->vn_lower_mh);
613 	}
614 
615 	kmem_cache_free(vnic_cache, vnic);
616 	return (0);
617 }
618 
619 /* ARGSUSED */
620 mblk_t *
621 vnic_m_tx(void *arg, mblk_t *mp_chain)
622 {
623 	/*
624 	 * This function could be invoked for an anchor VNIC when sending
625 	 * broadcast and multicast packets, and unicast packets which did
626 	 * not match any local known destination.
627 	 */
628 	freemsgchain(mp_chain);
629 	return (NULL);
630 }
631 
632 /*ARGSUSED*/
633 static void
634 vnic_m_ioctl(void *arg, queue_t *q, mblk_t *mp)
635 {
636 	miocnak(q, mp, 0, ENOTSUP);
637 }
638 
639 /*
640  * This entry point cannot be passed-through, since it is invoked
641  * for the per-VNIC kstats which must be exported independently
642  * of the existence of VNIC MAC clients.
643  */
644 static int
645 vnic_m_stat(void *arg, uint_t stat, uint64_t *val)
646 {
647 	vnic_t *vnic = arg;
648 	int rval = 0;
649 
650 	if (vnic->vn_lower_mh == NULL) {
651 		/*
652 		 * It's an anchor VNIC, which does not have any
653 		 * statistics in itself.
654 		 */
655 		return (ENOTSUP);
656 	}
657 
658 	/*
659 	 * ENOTSUP must be reported for unsupported stats, the VNIC
660 	 * driver reports a subset of the stats that would
661 	 * be returned by a real piece of hardware.
662 	 */
663 
664 	switch (stat) {
665 	case MAC_STAT_LINK_STATE:
666 	case MAC_STAT_LINK_UP:
667 	case MAC_STAT_PROMISC:
668 	case MAC_STAT_IFSPEED:
669 	case MAC_STAT_MULTIRCV:
670 	case MAC_STAT_MULTIXMT:
671 	case MAC_STAT_BRDCSTRCV:
672 	case MAC_STAT_BRDCSTXMT:
673 	case MAC_STAT_OPACKETS:
674 	case MAC_STAT_OBYTES:
675 	case MAC_STAT_IERRORS:
676 	case MAC_STAT_OERRORS:
677 	case MAC_STAT_RBYTES:
678 	case MAC_STAT_IPACKETS:
679 		*val = mac_client_stat_get(vnic->vn_mch, stat);
680 		break;
681 	default:
682 		rval = ENOTSUP;
683 	}
684 
685 	return (rval);
686 }
687 
688 /*
689  * Invoked by the upper MAC to retrieve the lower MAC client handle
690  * corresponding to a VNIC. A pointer to this function is obtained
691  * by the upper MAC via capability query.
692  *
693  * XXX-nicolas Note: this currently causes all VNIC MAC clients to
694  * receive the same MAC client handle for the same VNIC. This is ok
695  * as long as we have only one VNIC MAC client which sends and
696  * receives data, but we don't currently enforce this at the MAC layer.
697  */
698 static void *
699 vnic_mac_client_handle(void *vnic_arg)
700 {
701 	vnic_t *vnic = vnic_arg;
702 
703 	return (vnic->vn_mch);
704 }
705 
706 
707 /*
708  * Return information about the specified capability.
709  */
710 /* ARGSUSED */
711 static boolean_t
712 vnic_m_capab_get(void *arg, mac_capab_t cap, void *cap_data)
713 {
714 	vnic_t *vnic = arg;
715 
716 	switch (cap) {
717 	case MAC_CAPAB_HCKSUM: {
718 		uint32_t *hcksum_txflags = cap_data;
719 
720 		*hcksum_txflags = vnic->vn_hcksum_txflags &
721 		    (HCKSUM_INET_FULL_V4 | HCKSUM_IPHDRCKSUM |
722 		    HCKSUM_INET_PARTIAL);
723 		break;
724 	}
725 	case MAC_CAPAB_VNIC: {
726 		mac_capab_vnic_t *vnic_capab = cap_data;
727 
728 		if (vnic->vn_lower_mh == NULL) {
729 			/*
730 			 * It's an anchor VNIC, we don't have an underlying
731 			 * NIC and MAC client handle.
732 			 */
733 			return (B_FALSE);
734 		}
735 
736 		if (vnic_capab != NULL) {
737 			vnic_capab->mcv_arg = vnic;
738 			vnic_capab->mcv_mac_client_handle =
739 			    vnic_mac_client_handle;
740 		}
741 		break;
742 	}
743 	case MAC_CAPAB_ANCHOR_VNIC: {
744 		/* since it's an anchor VNIC we don't have lower mac handle */
745 		if (vnic->vn_lower_mh == NULL) {
746 			ASSERT(vnic->vn_link_id == 0);
747 			return (B_TRUE);
748 		}
749 		return (B_FALSE);
750 	}
751 	case MAC_CAPAB_NO_NATIVEVLAN:
752 	case MAC_CAPAB_NO_ZCOPY:
753 		return (B_TRUE);
754 	default:
755 		return (B_FALSE);
756 	}
757 	return (B_TRUE);
758 }
759 
760 /* ARGSUSED */
761 static int
762 vnic_m_start(void *arg)
763 {
764 	return (0);
765 }
766 
767 /* ARGSUSED */
768 static void
769 vnic_m_stop(void *arg)
770 {
771 }
772 
773 /* ARGSUSED */
774 static int
775 vnic_m_promisc(void *arg, boolean_t on)
776 {
777 	return (0);
778 }
779 
780 /* ARGSUSED */
781 static int
782 vnic_m_multicst(void *arg, boolean_t add, const uint8_t *addrp)
783 {
784 	return (0);
785 }
786 
787 static int
788 vnic_m_unicst(void *arg, const uint8_t *macaddr)
789 {
790 	vnic_t *vnic = arg;
791 
792 	return (mac_vnic_unicast_set(vnic->vn_mch, macaddr));
793 }
794 
795 /*
796  * Callback functions for set/get of properties
797  */
798 /*ARGSUSED*/
799 static int
800 vnic_m_setprop(void *m_driver, const char *pr_name, mac_prop_id_t pr_num,
801     uint_t pr_valsize, const void *pr_val)
802 {
803 	int 		err = ENOTSUP;
804 	vnic_t		*vn = m_driver;
805 
806 	/* allow setting MTU only on an etherstub */
807 	if (vn->vn_link_id != DATALINK_INVALID_LINKID)
808 		return (err);
809 
810 	switch (pr_num) {
811 	case MAC_PROP_MTU: {
812 		uint32_t	mtu;
813 
814 		if (pr_valsize < sizeof (mtu)) {
815 			err = EINVAL;
816 			break;
817 		}
818 		bcopy(pr_val, &mtu, sizeof (mtu));
819 		err = mac_maxsdu_update(vn->vn_mh, mtu);
820 		break;
821 	}
822 	default:
823 		break;
824 	}
825 	return (err);
826 }
827 
828 /*ARGSUSED*/
829 static int
830 vnic_m_getprop(void *m_driver, const char *pr_name, mac_prop_id_t pr_num,
831     uint_t pr_flags, uint_t pr_valsize, void *pr_val, uint_t *perm)
832 {
833 	return (ENOTSUP);
834 }
835 
836 
837 int
838 vnic_info(vnic_info_t *info)
839 {
840 	vnic_t		*vnic;
841 	int		err;
842 
843 	rw_enter(&vnic_lock, RW_WRITER);
844 
845 	err = mod_hash_find(vnic_hash, VNIC_HASH_KEY(info->vn_vnic_id),
846 	    (mod_hash_val_t *)&vnic);
847 	if (err != 0) {
848 		rw_exit(&vnic_lock);
849 		return (ENOENT);
850 	}
851 
852 	info->vn_link_id = vnic->vn_link_id;
853 	info->vn_mac_addr_type = vnic->vn_addr_type;
854 	info->vn_mac_len = vnic->vn_addr_len;
855 	bcopy(vnic->vn_addr, info->vn_mac_addr, MAXMACADDRLEN);
856 	info->vn_mac_slot = vnic->vn_slot_id;
857 	info->vn_mac_prefix_len = 0;
858 	info->vn_vid = vnic->vn_vid;
859 	info->vn_force = vnic->vn_force;
860 
861 	bzero(&info->vn_resource_props, sizeof (mac_resource_props_t));
862 	if (vnic->vn_mch != NULL)
863 		mac_resource_ctl_get(vnic->vn_mch, &info->vn_resource_props);
864 
865 	rw_exit(&vnic_lock);
866 	return (0);
867 }
868 
869 static void
870 vnic_notify_cb(void *arg, mac_notify_type_t type)
871 {
872 	vnic_t *vnic = arg;
873 
874 	/*
875 	 * Only the VLAN VNIC needs to be notified with primary MAC
876 	 * address change.
877 	 */
878 	if (vnic->vn_addr_type != VNIC_MAC_ADDR_TYPE_PRIMARY)
879 		return;
880 
881 	switch (type) {
882 	case MAC_NOTE_UNICST:
883 		/*  the unicast MAC address value */
884 		mac_unicast_primary_get(vnic->vn_lower_mh, vnic->vn_addr);
885 
886 		/* notify its upper layer MAC about MAC address change */
887 		mac_unicst_update(vnic->vn_mh, (const uint8_t *)vnic->vn_addr);
888 		break;
889 	default:
890 		break;
891 	}
892 }
893