xref: /titanic_51/usr/src/uts/common/io/dls/dls_link.c (revision f899e5733f35e45012ad40c8325b2622dcc2b673)
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 /*
27  * Data-Link Services Module
28  */
29 
30 #include	<sys/sysmacros.h>
31 #include	<sys/strsubr.h>
32 #include	<sys/strsun.h>
33 #include	<sys/vlan.h>
34 #include	<sys/dld_impl.h>
35 #include	<sys/sdt.h>
36 #include	<sys/atomic.h>
37 
38 static void		dls_bpf_newzone(dls_link_t *dlp, zoneid_t zid);
39 
40 static kmem_cache_t	*i_dls_link_cachep;
41 mod_hash_t		*i_dls_link_hash;
42 static uint_t		i_dls_link_count;
43 
44 #define		LINK_HASHSZ	67	/* prime */
45 #define		IMPL_HASHSZ	67	/* prime */
46 
47 /*
48  * Construct a hash key encompassing both DLSAP value and VLAN idenitifier.
49  */
50 #define	MAKE_KEY(_sap)						\
51 	((mod_hash_key_t)(uintptr_t)((_sap) << VLAN_ID_SIZE))
52 
53 #define	DLS_STRIP_PADDING(pktsize, p) {			\
54 	if (pktsize != 0) {				\
55 		ssize_t delta = pktsize - msgdsize(p);	\
56 							\
57 		if (delta < 0)				\
58 			(void) adjmsg(p, delta);	\
59 	}						\
60 }
61 
62 /*
63  * Private functions.
64  */
65 
66 /*ARGSUSED*/
67 static int
68 i_dls_link_constructor(void *buf, void *arg, int kmflag)
69 {
70 	dls_link_t	*dlp = buf;
71 	char		name[MAXNAMELEN];
72 
73 	bzero(buf, sizeof (dls_link_t));
74 
75 	(void) snprintf(name, MAXNAMELEN, "dls_link_t_%p_hash", buf);
76 	dlp->dl_str_hash = mod_hash_create_idhash(name, IMPL_HASHSZ,
77 	    mod_hash_null_valdtor);
78 
79 	return (0);
80 }
81 
82 /*ARGSUSED*/
83 static void
84 i_dls_link_destructor(void *buf, void *arg)
85 {
86 	dls_link_t	*dlp = buf;
87 
88 	ASSERT(dlp->dl_ref == 0);
89 	ASSERT(dlp->dl_mh == NULL);
90 	ASSERT(dlp->dl_mah == NULL);
91 	ASSERT(dlp->dl_unknowns == 0);
92 
93 	mod_hash_destroy_idhash(dlp->dl_str_hash);
94 	dlp->dl_str_hash = NULL;
95 
96 }
97 
98 /*
99  * - Parse the mac header information of the given packet.
100  * - Strip the padding and skip over the header. Note that because some
101  *   DLS consumers only check the db_ref count of the first mblk, we
102  *   pullup the message into a single mblk. Because the original message
103  *   is freed as the result of message pulling up, dls_link_header_info()
104  *   is called again to update the mhi_saddr and mhi_daddr pointers in the
105  *   mhip. Further, the dls_link_header_info() function ensures that the
106  *   size of the pulled message is greater than the MAC header size,
107  *   therefore we can directly advance b_rptr to point at the payload.
108  *
109  * We choose to use a macro for performance reasons.
110  */
111 #define	DLS_PREPARE_PKT(dlp, mp, mhip, err) {				\
112 	mblk_t *nextp = (mp)->b_next;					\
113 	if (((err) = dls_link_header_info((dlp), (mp), (mhip))) == 0) {	\
114 		DLS_STRIP_PADDING((mhip)->mhi_pktsize, (mp));		\
115 		if (MBLKL((mp)) < (mhip)->mhi_hdrsize) {		\
116 			mblk_t *newmp;					\
117 			if ((newmp = msgpullup((mp), -1)) == NULL) {	\
118 				(err) = EINVAL;				\
119 			} else {					\
120 				(mp)->b_next = NULL;			\
121 				freemsg((mp));				\
122 				(mp) = newmp;				\
123 				VERIFY(dls_link_header_info((dlp),	\
124 				    (mp), (mhip)) == 0);		\
125 				(mp)->b_next = nextp;			\
126 				(mp)->b_rptr += (mhip)->mhi_hdrsize;	\
127 			}						\
128 		} else {						\
129 			(mp)->b_rptr += (mhip)->mhi_hdrsize;		\
130 		}							\
131 	}								\
132 }
133 
134 /*
135  * Truncate the chain starting at mp such that all packets in the chain
136  * have identical source and destination addresses, saps, and tag types
137  * (see below).  It returns a pointer to the mblk following the chain,
138  * NULL if there is no further packet following the processed chain.
139  * The countp argument is set to the number of valid packets in the chain.
140  * Note that the whole MAC header (including the VLAN tag if any) in each
141  * packet will be stripped.
142  */
143 static mblk_t *
144 i_dls_link_subchain(dls_link_t *dlp, mblk_t *mp, const mac_header_info_t *mhip,
145     uint_t *countp)
146 {
147 	mblk_t		*prevp;
148 	uint_t		npacket = 1;
149 	size_t		addr_size = dlp->dl_mip->mi_addr_length;
150 	uint16_t	vid = VLAN_ID(mhip->mhi_tci);
151 	uint16_t	pri = VLAN_PRI(mhip->mhi_tci);
152 
153 	/*
154 	 * Compare with subsequent headers until we find one that has
155 	 * differing header information. After checking each packet
156 	 * strip padding and skip over the header.
157 	 */
158 	for (prevp = mp; (mp = mp->b_next) != NULL; prevp = mp) {
159 		mac_header_info_t cmhi;
160 		uint16_t cvid, cpri;
161 		int err;
162 
163 		DLS_PREPARE_PKT(dlp, mp, &cmhi, err);
164 		if (err != 0)
165 			break;
166 
167 		prevp->b_next = mp;
168 
169 		/*
170 		 * The source, destination, sap, vlan id and the MSGNOLOOP
171 		 * flag must all match in a given subchain.
172 		 */
173 		if (memcmp(mhip->mhi_daddr, cmhi.mhi_daddr, addr_size) != 0 ||
174 		    memcmp(mhip->mhi_saddr, cmhi.mhi_saddr, addr_size) != 0 ||
175 		    mhip->mhi_bindsap != cmhi.mhi_bindsap) {
176 			/*
177 			 * Note that we don't need to restore the padding.
178 			 */
179 			mp->b_rptr -= cmhi.mhi_hdrsize;
180 			break;
181 		}
182 
183 		cvid = VLAN_ID(cmhi.mhi_tci);
184 		cpri = VLAN_PRI(cmhi.mhi_tci);
185 
186 		/*
187 		 * There are several types of packets. Packets don't match
188 		 * if they are classified to different type or if they are
189 		 * VLAN packets but belong to different VLANs:
190 		 *
191 		 * packet type		tagged		vid		pri
192 		 * ---------------------------------------------------------
193 		 * untagged		No		zero		zero
194 		 * VLAN packets		Yes		non-zero	-
195 		 * priority tagged	Yes		zero		non-zero
196 		 * 0 tagged		Yes		zero		zero
197 		 */
198 		if ((mhip->mhi_istagged != cmhi.mhi_istagged) ||
199 		    (vid != cvid) || ((vid == VLAN_ID_NONE) &&
200 		    (((pri == 0) && (cpri != 0)) ||
201 		    ((pri != 0) && (cpri == 0))))) {
202 			mp->b_rptr -= cmhi.mhi_hdrsize;
203 			break;
204 		}
205 
206 		npacket++;
207 	}
208 
209 	/*
210 	 * Break the chain at this point and return a pointer to the next
211 	 * sub-chain.
212 	 */
213 	prevp->b_next = NULL;
214 	*countp = npacket;
215 	return (mp);
216 }
217 
218 /* ARGSUSED */
219 static int
220 i_dls_head_hold(mod_hash_key_t key, mod_hash_val_t val)
221 {
222 	dls_head_t *dhp = (dls_head_t *)val;
223 
224 	/*
225 	 * The lock order is  mod_hash's internal lock -> dh_lock as in the
226 	 * call to i_dls_link_rx -> mod_hash_find_cb_rval -> i_dls_head_hold
227 	 */
228 	mutex_enter(&dhp->dh_lock);
229 	if (dhp->dh_removing) {
230 		mutex_exit(&dhp->dh_lock);
231 		return (-1);
232 	}
233 	dhp->dh_ref++;
234 	mutex_exit(&dhp->dh_lock);
235 	return (0);
236 }
237 
238 void
239 i_dls_head_rele(dls_head_t *dhp)
240 {
241 	mutex_enter(&dhp->dh_lock);
242 	dhp->dh_ref--;
243 	if (dhp->dh_ref == 0 && dhp->dh_removing != 0)
244 		cv_broadcast(&dhp->dh_cv);
245 	mutex_exit(&dhp->dh_lock);
246 }
247 
248 static dls_head_t *
249 i_dls_head_alloc(mod_hash_key_t key)
250 {
251 	dls_head_t	*dhp;
252 
253 	dhp = kmem_zalloc(sizeof (dls_head_t), KM_SLEEP);
254 	dhp->dh_key = key;
255 	return (dhp);
256 }
257 
258 static void
259 i_dls_head_free(dls_head_t *dhp)
260 {
261 	ASSERT(dhp->dh_ref == 0);
262 	kmem_free(dhp, sizeof (dls_head_t));
263 }
264 
265 /*
266  * Try to send mp up to the streams of the given sap and vid. Return B_TRUE
267  * if this message is sent to any streams.
268  * Note that this function will copy the message chain and the original
269  * mp will remain valid after this function
270  */
271 static uint_t
272 i_dls_link_rx_func(dls_link_t *dlp, mac_resource_handle_t mrh,
273     mac_header_info_t *mhip, mblk_t *mp, uint32_t sap,
274     boolean_t (*acceptfunc)())
275 {
276 	mod_hash_t	*hash = dlp->dl_str_hash;
277 	mod_hash_key_t	key;
278 	dls_head_t	*dhp;
279 	dld_str_t	*dsp;
280 	mblk_t		*nmp;
281 	dls_rx_t	ds_rx;
282 	void		*ds_rx_arg;
283 	uint_t		naccepted = 0;
284 	int		rval;
285 
286 	/*
287 	 * Construct a hash key from the VLAN identifier and the
288 	 * DLSAP that represents dld_str_t in promiscuous mode.
289 	 */
290 	key = MAKE_KEY(sap);
291 
292 	/*
293 	 * Search the hash table for dld_str_t eligible to receive
294 	 * a packet chain for this DLSAP/VLAN combination. The mod hash's
295 	 * internal lock serializes find/insert/remove from the mod hash list.
296 	 * Incrementing the dh_ref (while holding the mod hash lock) ensures
297 	 * dls_link_remove will wait for the upcall to finish.
298 	 */
299 	if (mod_hash_find_cb_rval(hash, key, (mod_hash_val_t *)&dhp,
300 	    i_dls_head_hold, &rval) != 0 || (rval != 0)) {
301 		return (B_FALSE);
302 	}
303 
304 	/*
305 	 * Find dld_str_t that will accept the sub-chain.
306 	 */
307 	for (dsp = dhp->dh_list; dsp != NULL; dsp = dsp->ds_next) {
308 		if (!acceptfunc(dsp, mhip, &ds_rx, &ds_rx_arg))
309 			continue;
310 
311 		/*
312 		 * We have at least one acceptor.
313 		 */
314 		naccepted++;
315 
316 		/*
317 		 * There will normally be at least more dld_str_t
318 		 * (since we've yet to check for non-promiscuous
319 		 * dld_str_t) so dup the sub-chain.
320 		 */
321 		if ((nmp = copymsgchain(mp)) != NULL)
322 			ds_rx(ds_rx_arg, mrh, nmp, mhip);
323 	}
324 
325 	/*
326 	 * Release the hold on the dld_str_t chain now that we have
327 	 * finished walking it.
328 	 */
329 	i_dls_head_rele(dhp);
330 	return (naccepted);
331 }
332 
333 /* ARGSUSED */
334 void
335 i_dls_link_rx(void *arg, mac_resource_handle_t mrh, mblk_t *mp,
336     boolean_t loopback)
337 {
338 	dls_link_t			*dlp = arg;
339 	mod_hash_t			*hash = dlp->dl_str_hash;
340 	mblk_t				*nextp;
341 	mac_header_info_t		mhi;
342 	dls_head_t			*dhp;
343 	dld_str_t			*dsp;
344 	dld_str_t			*ndsp;
345 	mblk_t				*nmp;
346 	mod_hash_key_t			key;
347 	uint_t				npacket;
348 	boolean_t			accepted;
349 	dls_rx_t			ds_rx, nds_rx;
350 	void				*ds_rx_arg, *nds_rx_arg;
351 	uint16_t			vid;
352 	int				err, rval;
353 
354 	/*
355 	 * Walk the packet chain.
356 	 */
357 	for (; mp != NULL; mp = nextp) {
358 		/*
359 		 * Wipe the accepted state.
360 		 */
361 		accepted = B_FALSE;
362 
363 		DLS_PREPARE_PKT(dlp, mp, &mhi, err);
364 		if (err != 0) {
365 			atomic_add_32(&(dlp->dl_unknowns), 1);
366 			nextp = mp->b_next;
367 			mp->b_next = NULL;
368 			freemsg(mp);
369 			continue;
370 		}
371 
372 		/*
373 		 * Grab the longest sub-chain we can process as a single
374 		 * unit.
375 		 */
376 		nextp = i_dls_link_subchain(dlp, mp, &mhi, &npacket);
377 		ASSERT(npacket != 0);
378 
379 		vid = VLAN_ID(mhi.mhi_tci);
380 
381 		if (mhi.mhi_istagged) {
382 			/*
383 			 * If it is tagged traffic, send it upstream to
384 			 * all dld_str_t which are attached to the physical
385 			 * link and bound to SAP 0x8100.
386 			 */
387 			if (i_dls_link_rx_func(dlp, mrh, &mhi, mp,
388 			    ETHERTYPE_VLAN, dls_accept) > 0) {
389 				accepted = B_TRUE;
390 			}
391 
392 			/*
393 			 * Don't pass the packets up if they are tagged
394 			 * packets and:
395 			 *  - their VID and priority are both zero and the
396 			 *    original packet isn't using the PVID (invalid
397 			 *    packets).
398 			 *  - their sap is ETHERTYPE_VLAN and their VID is
399 			 *    zero as they have already been sent upstreams.
400 			 */
401 			if ((vid == VLAN_ID_NONE && !mhi.mhi_ispvid &&
402 			    VLAN_PRI(mhi.mhi_tci) == 0) ||
403 			    (mhi.mhi_bindsap == ETHERTYPE_VLAN &&
404 			    vid == VLAN_ID_NONE)) {
405 				freemsgchain(mp);
406 				goto loop;
407 			}
408 		}
409 
410 		/*
411 		 * Construct a hash key from the VLAN identifier and the
412 		 * DLSAP.
413 		 */
414 		key = MAKE_KEY(mhi.mhi_bindsap);
415 
416 		/*
417 		 * Search the has table for dld_str_t eligible to receive
418 		 * a packet chain for this DLSAP/VLAN combination.
419 		 */
420 		if (mod_hash_find_cb_rval(hash, key, (mod_hash_val_t *)&dhp,
421 		    i_dls_head_hold, &rval) != 0 || (rval != 0)) {
422 			freemsgchain(mp);
423 			goto loop;
424 		}
425 
426 		/*
427 		 * Find the first dld_str_t that will accept the sub-chain.
428 		 */
429 		for (dsp = dhp->dh_list; dsp != NULL; dsp = dsp->ds_next)
430 			if (dls_accept(dsp, &mhi, &ds_rx, &ds_rx_arg))
431 				break;
432 
433 		/*
434 		 * If we did not find any dld_str_t willing to accept the
435 		 * sub-chain then throw it away.
436 		 */
437 		if (dsp == NULL) {
438 			i_dls_head_rele(dhp);
439 			freemsgchain(mp);
440 			goto loop;
441 		}
442 
443 		/*
444 		 * We have at least one acceptor.
445 		 */
446 		accepted = B_TRUE;
447 		for (;;) {
448 			/*
449 			 * Find the next dld_str_t that will accept the
450 			 * sub-chain.
451 			 */
452 			for (ndsp = dsp->ds_next; ndsp != NULL;
453 			    ndsp = ndsp->ds_next)
454 				if (dls_accept(ndsp, &mhi, &nds_rx,
455 				    &nds_rx_arg))
456 					break;
457 
458 			/*
459 			 * If there are no more dld_str_t that are willing
460 			 * to accept the sub-chain then we don't need to dup
461 			 * it before handing it to the current one.
462 			 */
463 			if (ndsp == NULL) {
464 				ds_rx(ds_rx_arg, mrh, mp, &mhi);
465 
466 				/*
467 				 * Since there are no more dld_str_t, we're
468 				 * done.
469 				 */
470 				break;
471 			}
472 
473 			/*
474 			 * There are more dld_str_t so dup the sub-chain.
475 			 */
476 			if ((nmp = copymsgchain(mp)) != NULL)
477 				ds_rx(ds_rx_arg, mrh, nmp, &mhi);
478 
479 			dsp = ndsp;
480 			ds_rx = nds_rx;
481 			ds_rx_arg = nds_rx_arg;
482 		}
483 
484 		/*
485 		 * Release the hold on the dld_str_t chain now that we have
486 		 * finished walking it.
487 		 */
488 		i_dls_head_rele(dhp);
489 
490 loop:
491 		/*
492 		 * If there were no acceptors then add the packet count to the
493 		 * 'unknown' count.
494 		 */
495 		if (!accepted)
496 			atomic_add_32(&(dlp->dl_unknowns), npacket);
497 	}
498 }
499 
500 /* ARGSUSED */
501 void
502 dls_rx_vlan_promisc(void *arg, mac_resource_handle_t mrh, mblk_t *mp,
503     boolean_t loopback)
504 {
505 	dld_str_t			*dsp = arg;
506 	dls_link_t			*dlp = dsp->ds_dlp;
507 	mac_header_info_t		mhi;
508 	dls_rx_t			ds_rx;
509 	void				*ds_rx_arg;
510 	int				err;
511 
512 	DLS_PREPARE_PKT(dlp, mp, &mhi, err);
513 	if (err != 0)
514 		goto drop;
515 
516 	/*
517 	 * If there is promiscuous handle for vlan, we filter out the untagged
518 	 * pkts and pkts that are not for the primary unicast address.
519 	 */
520 	if (dsp->ds_vlan_mph != NULL) {
521 		uint8_t prim_addr[MAXMACADDRLEN];
522 		size_t	addr_length = dsp->ds_mip->mi_addr_length;
523 
524 		if (!(mhi.mhi_istagged))
525 			goto drop;
526 		ASSERT(dsp->ds_mh != NULL);
527 		mac_unicast_primary_get(dsp->ds_mh, (uint8_t *)prim_addr);
528 		if (memcmp(mhi.mhi_daddr, prim_addr, addr_length) != 0)
529 			goto drop;
530 
531 		if (!dls_accept(dsp, &mhi, &ds_rx, &ds_rx_arg))
532 			goto drop;
533 
534 		ds_rx(ds_rx_arg, NULL, mp, &mhi);
535 		return;
536 	}
537 
538 drop:
539 	atomic_add_32(&dlp->dl_unknowns, 1);
540 	freemsg(mp);
541 }
542 
543 /* ARGSUSED */
544 void
545 dls_rx_promisc(void *arg, mac_resource_handle_t mrh, mblk_t *mp,
546     boolean_t loopback)
547 {
548 	dld_str_t			*dsp = arg;
549 	dls_link_t			*dlp = dsp->ds_dlp;
550 	mac_header_info_t		mhi;
551 	dls_rx_t			ds_rx;
552 	void				*ds_rx_arg;
553 	int				err;
554 	dls_head_t			*dhp;
555 	mod_hash_key_t			key;
556 
557 	DLS_PREPARE_PKT(dlp, mp, &mhi, err);
558 	if (err != 0)
559 		goto drop;
560 
561 	/*
562 	 * In order to filter out sap pkt that no dls channel listens, search
563 	 * the hash table trying to find a dld_str_t eligible to receive the pkt
564 	 */
565 	if ((dsp->ds_promisc & DLS_PROMISC_SAP) == 0) {
566 		key = MAKE_KEY(mhi.mhi_bindsap);
567 		if (mod_hash_find(dsp->ds_dlp->dl_str_hash, key,
568 		    (mod_hash_val_t *)&dhp) != 0)
569 			goto drop;
570 	}
571 
572 	if (!dls_accept_promisc(dsp, &mhi, &ds_rx, &ds_rx_arg, loopback))
573 		goto drop;
574 
575 	ds_rx(ds_rx_arg, NULL, mp, &mhi);
576 	return;
577 
578 drop:
579 	atomic_add_32(&dlp->dl_unknowns, 1);
580 	freemsg(mp);
581 }
582 
583 static void
584 i_dls_link_destroy(dls_link_t *dlp)
585 {
586 	ASSERT(dlp->dl_nactive == 0);
587 	ASSERT(dlp->dl_impl_count == 0);
588 	ASSERT(dlp->dl_zone_ref == 0);
589 
590 	/*
591 	 * Free the structure back to the cache.
592 	 */
593 	if (dlp->dl_mch != NULL)
594 		mac_client_close(dlp->dl_mch, 0);
595 
596 	if (dlp->dl_mh != NULL) {
597 		ASSERT(MAC_PERIM_HELD(dlp->dl_mh));
598 		mac_close(dlp->dl_mh);
599 	}
600 
601 	dlp->dl_mh = NULL;
602 	dlp->dl_mch = NULL;
603 	dlp->dl_mip = NULL;
604 	dlp->dl_unknowns = 0;
605 	kmem_cache_free(i_dls_link_cachep, dlp);
606 }
607 
608 static int
609 i_dls_link_create(const char *name, dls_link_t **dlpp)
610 {
611 	dls_link_t		*dlp;
612 	int			err;
613 
614 	/*
615 	 * Allocate a new dls_link_t structure.
616 	 */
617 	dlp = kmem_cache_alloc(i_dls_link_cachep, KM_SLEEP);
618 
619 	/*
620 	 * Name the dls_link_t after the MAC interface it represents.
621 	 */
622 	(void) strlcpy(dlp->dl_name, name, sizeof (dlp->dl_name));
623 
624 	/*
625 	 * First reference; hold open the MAC interface.
626 	 */
627 	ASSERT(dlp->dl_mh == NULL);
628 	err = mac_open(dlp->dl_name, &dlp->dl_mh);
629 	if (err != 0)
630 		goto bail;
631 
632 	ASSERT(MAC_PERIM_HELD(dlp->dl_mh));
633 	dlp->dl_mip = mac_info(dlp->dl_mh);
634 
635 	/* DLS is the "primary" MAC client */
636 	ASSERT(dlp->dl_mch == NULL);
637 
638 	err = mac_client_open(dlp->dl_mh, &dlp->dl_mch, NULL,
639 	    MAC_OPEN_FLAGS_USE_DATALINK_NAME);
640 	if (err != 0)
641 		goto bail;
642 
643 	DTRACE_PROBE2(dls__primary__client, char *, dlp->dl_name, void *,
644 	    dlp->dl_mch);
645 
646 	*dlpp = dlp;
647 	return (0);
648 
649 bail:
650 	i_dls_link_destroy(dlp);
651 	return (err);
652 }
653 
654 /*
655  * Module initialization functions.
656  */
657 
658 void
659 dls_link_init(void)
660 {
661 	/*
662 	 * Create a kmem_cache of dls_link_t structures.
663 	 */
664 	i_dls_link_cachep = kmem_cache_create("dls_link_cache",
665 	    sizeof (dls_link_t), 0, i_dls_link_constructor,
666 	    i_dls_link_destructor, NULL, NULL, NULL, 0);
667 	ASSERT(i_dls_link_cachep != NULL);
668 
669 	/*
670 	 * Create a dls_link_t hash table and associated lock.
671 	 */
672 	i_dls_link_hash = mod_hash_create_extended("dls_link_hash",
673 	    IMPL_HASHSZ, mod_hash_null_keydtor, mod_hash_null_valdtor,
674 	    mod_hash_bystr, NULL, mod_hash_strkey_cmp, KM_SLEEP);
675 	i_dls_link_count = 0;
676 }
677 
678 int
679 dls_link_fini(void)
680 {
681 	if (i_dls_link_count > 0)
682 		return (EBUSY);
683 
684 	/*
685 	 * Destroy the kmem_cache.
686 	 */
687 	kmem_cache_destroy(i_dls_link_cachep);
688 
689 	/*
690 	 * Destroy the hash table and associated lock.
691 	 */
692 	mod_hash_destroy_hash(i_dls_link_hash);
693 	return (0);
694 }
695 
696 /*
697  * Exported functions.
698  */
699 
700 static int
701 dls_link_hold_common(const char *name, dls_link_t **dlpp, boolean_t create)
702 {
703 	dls_link_t		*dlp;
704 	int			err;
705 
706 	/*
707 	 * Look up a dls_link_t corresponding to the given macname in the
708 	 * global hash table. The i_dls_link_hash itself is protected by the
709 	 * mod_hash package's internal lock which synchronizes
710 	 * find/insert/remove into the global mod_hash list. Assumes that
711 	 * inserts and removes are single threaded on a per mac end point
712 	 * by the mac perimeter.
713 	 */
714 	if ((err = mod_hash_find(i_dls_link_hash, (mod_hash_key_t)name,
715 	    (mod_hash_val_t *)&dlp)) == 0)
716 		goto done;
717 
718 	ASSERT(err == MH_ERR_NOTFOUND);
719 	if (!create)
720 		return (ENOENT);
721 
722 	/*
723 	 * We didn't find anything so we need to create one.
724 	 */
725 	if ((err = i_dls_link_create(name, &dlp)) != 0)
726 		return (err);
727 
728 	/*
729 	 * Insert the dls_link_t.
730 	 */
731 	err = mod_hash_insert(i_dls_link_hash, (mod_hash_key_t)dlp->dl_name,
732 	    (mod_hash_val_t)dlp);
733 	ASSERT(err == 0);
734 
735 	atomic_add_32(&i_dls_link_count, 1);
736 	ASSERT(i_dls_link_count != 0);
737 
738 done:
739 	ASSERT(MAC_PERIM_HELD(dlp->dl_mh));
740 	/*
741 	 * Bump the reference count and hand back the reference.
742 	 */
743 	dlp->dl_ref++;
744 	*dlpp = dlp;
745 	return (0);
746 }
747 
748 int
749 dls_link_hold_create(const char *name, dls_link_t **dlpp)
750 {
751 	return (dls_link_hold_common(name, dlpp, B_TRUE));
752 }
753 
754 int
755 dls_link_hold(const char *name, dls_link_t **dlpp)
756 {
757 	return (dls_link_hold_common(name, dlpp, B_FALSE));
758 }
759 
760 dev_info_t *
761 dls_link_devinfo(dev_t dev)
762 {
763 	dls_link_t	*dlp;
764 	dev_info_t	*dip;
765 	char	macname[MAXNAMELEN];
766 	char	*drv;
767 	mac_perim_handle_t	mph;
768 
769 	if ((drv = ddi_major_to_name(getmajor(dev))) == NULL)
770 		return (NULL);
771 	(void) snprintf(macname, MAXNAMELEN, "%s%d", drv, getminor(dev) - 1);
772 
773 	/*
774 	 * The code below assumes that the name constructed above is the
775 	 * macname. This is not the case for legacy devices. Currently this
776 	 * is ok because this function is only called in the getinfo(9e) path,
777 	 * which for a legacy device would directly end up in the driver's
778 	 * getinfo, rather than here
779 	 */
780 	if (mac_perim_enter_by_macname(macname, &mph) != 0)
781 		return (NULL);
782 
783 	if (dls_link_hold(macname, &dlp) != 0) {
784 		mac_perim_exit(mph);
785 		return (NULL);
786 	}
787 
788 	dip = mac_devinfo_get(dlp->dl_mh);
789 	dls_link_rele(dlp);
790 	mac_perim_exit(mph);
791 
792 	return (dip);
793 }
794 
795 dev_t
796 dls_link_dev(dls_link_t *dlp)
797 {
798 	return (makedevice(ddi_driver_major(mac_devinfo_get(dlp->dl_mh)),
799 	    mac_minor(dlp->dl_mh)));
800 }
801 
802 void
803 dls_link_rele(dls_link_t *dlp)
804 {
805 	mod_hash_val_t	val;
806 
807 	ASSERT(MAC_PERIM_HELD(dlp->dl_mh));
808 	/*
809 	 * Check if there are any more references.
810 	 */
811 	if (--dlp->dl_ref == 0) {
812 		(void) mod_hash_remove(i_dls_link_hash,
813 		    (mod_hash_key_t)dlp->dl_name, &val);
814 		ASSERT(dlp == (dls_link_t *)val);
815 
816 		/*
817 		 * Destroy the dls_link_t.
818 		 */
819 		i_dls_link_destroy(dlp);
820 		ASSERT(i_dls_link_count > 0);
821 		atomic_add_32(&i_dls_link_count, -1);
822 	}
823 }
824 
825 int
826 dls_link_rele_by_name(const char *name)
827 {
828 	dls_link_t		*dlp;
829 
830 	if (mod_hash_find(i_dls_link_hash, (mod_hash_key_t)name,
831 	    (mod_hash_val_t *)&dlp) != 0)
832 		return (ENOENT);
833 
834 	ASSERT(MAC_PERIM_HELD(dlp->dl_mh));
835 
836 	/*
837 	 * Must fail detach if mac client is busy.
838 	 */
839 	ASSERT(dlp->dl_ref > 0 && dlp->dl_mch != NULL);
840 	if (mac_link_has_flows(dlp->dl_mch))
841 		return (ENOTEMPTY);
842 
843 	dls_link_rele(dlp);
844 	return (0);
845 }
846 
847 int
848 dls_link_setzid(const char *name, zoneid_t zid)
849 {
850 	dls_link_t	*dlp;
851 	int		err = 0;
852 	zoneid_t	old_zid;
853 
854 	if ((err = dls_link_hold_create(name, &dlp)) != 0)
855 		return (err);
856 
857 	ASSERT(MAC_PERIM_HELD(dlp->dl_mh));
858 
859 	if ((old_zid = dlp->dl_zid) == zid)
860 		goto done;
861 
862 	/*
863 	 * Check whether this dlp is used by its own zone.  If yes, we cannot
864 	 * change its zoneid.
865 	 */
866 	if (dlp->dl_zone_ref != 0) {
867 		err = EBUSY;
868 		goto done;
869 	}
870 
871 	dls_bpf_newzone(dlp, zid);
872 	dlp->dl_zid = zid;
873 
874 	if (zid == GLOBAL_ZONEID) {
875 		/*
876 		 * The link is moving from a non-global zone to the global
877 		 * zone, so we need to release the reference that was held
878 		 * when the link was originally assigned to the non-global
879 		 * zone.
880 		 */
881 		dls_link_rele(dlp);
882 	}
883 
884 done:
885 	/*
886 	 * We only keep the reference to this link open if the link has
887 	 * successfully moved from the global zone to a non-global zone.
888 	 */
889 	if (err != 0 || old_zid != GLOBAL_ZONEID)
890 		dls_link_rele(dlp);
891 	return (err);
892 }
893 
894 
895 /*
896  * When a NIC changes zone, that change needs to be communicated to BPF
897  * so that it can correctly enforce access rights on it via BPF. In the
898  * absence of a function from BPF to just change the zoneid, this is
899  * done with a detach followed by an attach.
900  */
901 static void
902 dls_bpf_newzone(dls_link_t *dlp, zoneid_t zid)
903 {
904 	if (dls_bpfdetach_fn != NULL)
905 		dls_bpfdetach_fn((uintptr_t)dlp->dl_mh);
906 
907 	if (dls_bpfattach_fn != NULL)
908 		dls_bpfattach_fn((uintptr_t)dlp->dl_mh, mac_type(dlp->dl_mh),
909 		    zid, BPR_MAC);
910 }
911 
912 int
913 dls_link_getzid(const char *name, zoneid_t *zidp)
914 {
915 	dls_link_t	*dlp;
916 	int		err = 0;
917 
918 	if ((err = dls_link_hold(name, &dlp)) != 0)
919 		return (err);
920 
921 	ASSERT(MAC_PERIM_HELD(dlp->dl_mh));
922 
923 	*zidp = dlp->dl_zid;
924 
925 	dls_link_rele(dlp);
926 	return (0);
927 }
928 
929 void
930 dls_link_add(dls_link_t *dlp, uint32_t sap, dld_str_t *dsp)
931 {
932 	mod_hash_t	*hash = dlp->dl_str_hash;
933 	mod_hash_key_t	key;
934 	dls_head_t	*dhp;
935 	dld_str_t	*p;
936 	int		err;
937 
938 	ASSERT(MAC_PERIM_HELD(dlp->dl_mh));
939 
940 	/*
941 	 * Generate a hash key based on the sap.
942 	 */
943 	key = MAKE_KEY(sap);
944 
945 	/*
946 	 * Search the table for a list head with this key.
947 	 */
948 	if ((err = mod_hash_find(hash, key, (mod_hash_val_t *)&dhp)) != 0) {
949 		ASSERT(err == MH_ERR_NOTFOUND);
950 
951 		dhp = i_dls_head_alloc(key);
952 		err = mod_hash_insert(hash, key, (mod_hash_val_t)dhp);
953 		ASSERT(err == 0);
954 	}
955 
956 	/*
957 	 * Add the dld_str_t to the head of the list. List walkers in
958 	 * i_dls_link_rx_* bump up dh_ref to ensure the list does not change
959 	 * while they walk the list. The membar below ensures that list walkers
960 	 * see exactly the old list or the new list.
961 	 */
962 	ASSERT(dsp->ds_next == NULL);
963 	p = dhp->dh_list;
964 	dsp->ds_next = p;
965 
966 	membar_producer();
967 
968 	dhp->dh_list = dsp;
969 
970 	/*
971 	 * Save a pointer to the list head.
972 	 */
973 	dsp->ds_head = dhp;
974 	dlp->dl_impl_count++;
975 }
976 
977 void
978 dls_link_remove(dls_link_t *dlp, dld_str_t *dsp)
979 {
980 	mod_hash_t	*hash = dlp->dl_str_hash;
981 	dld_str_t	**pp;
982 	dld_str_t	*p;
983 	dls_head_t	*dhp;
984 
985 	ASSERT(MAC_PERIM_HELD(dlp->dl_mh));
986 
987 	/*
988 	 * We set dh_removing here to tell the receive callbacks not to pass
989 	 * up packets anymore. Then wait till the current callbacks are done.
990 	 * This happens either in the close path or in processing the
991 	 * DL_UNBIND_REQ via a taskq thread, and it is ok to cv_wait in either.
992 	 * The dh_ref ensures there aren't and there won't be any upcalls
993 	 * walking or using the dh_list. The mod hash internal lock ensures
994 	 * that the insert/remove of the dls_head_t itself synchronizes with
995 	 * any i_dls_link_rx trying to locate it. The perimeter ensures that
996 	 * there isn't another simultaneous dls_link_add/remove.
997 	 */
998 	dhp = dsp->ds_head;
999 
1000 	mutex_enter(&dhp->dh_lock);
1001 	dhp->dh_removing = B_TRUE;
1002 	while (dhp->dh_ref != 0)
1003 		cv_wait(&dhp->dh_cv, &dhp->dh_lock);
1004 	mutex_exit(&dhp->dh_lock);
1005 
1006 	/*
1007 	 * Walk the list and remove the dld_str_t.
1008 	 */
1009 	for (pp = &dhp->dh_list; (p = *pp) != NULL; pp = &(p->ds_next)) {
1010 		if (p == dsp)
1011 			break;
1012 	}
1013 	ASSERT(p != NULL);
1014 	*pp = p->ds_next;
1015 	p->ds_next = NULL;
1016 	p->ds_head = NULL;
1017 
1018 	ASSERT(dlp->dl_impl_count != 0);
1019 	dlp->dl_impl_count--;
1020 
1021 	if (dhp->dh_list == NULL) {
1022 		mod_hash_val_t	val = NULL;
1023 
1024 		/*
1025 		 * The list is empty so remove the hash table entry.
1026 		 */
1027 		(void) mod_hash_remove(hash, dhp->dh_key, &val);
1028 		ASSERT(dhp == (dls_head_t *)val);
1029 		i_dls_head_free(dhp);
1030 	} else {
1031 		mutex_enter(&dhp->dh_lock);
1032 		dhp->dh_removing = B_FALSE;
1033 		mutex_exit(&dhp->dh_lock);
1034 	}
1035 }
1036 
1037 int
1038 dls_link_header_info(dls_link_t *dlp, mblk_t *mp, mac_header_info_t *mhip)
1039 {
1040 	boolean_t	is_ethernet = (dlp->dl_mip->mi_media == DL_ETHER);
1041 	uint16_t	pvid = mac_get_pvid(dlp->dl_mh);
1042 	int		err = 0;
1043 
1044 	/*
1045 	 * Packets should always be at least 16 bit aligned.
1046 	 */
1047 	ASSERT(IS_P2ALIGNED(mp->b_rptr, sizeof (uint16_t)));
1048 
1049 	if ((err = mac_header_info(dlp->dl_mh, mp, mhip)) != 0)
1050 		return (err);
1051 
1052 	/*
1053 	 * If this is a VLAN-tagged Ethernet packet, then the SAP in the
1054 	 * mac_header_info_t as returned by mac_header_info() is
1055 	 * ETHERTYPE_VLAN. We need to grab the ethertype from the VLAN header.
1056 	 */
1057 	mhip->mhi_ispvid = B_FALSE;
1058 	if (is_ethernet && (mhip->mhi_bindsap == ETHERTYPE_VLAN)) {
1059 		struct ether_vlan_header *evhp;
1060 		uint16_t sap;
1061 		mblk_t *tmp = NULL;
1062 		size_t size;
1063 
1064 		size = sizeof (struct ether_vlan_header);
1065 		if (MBLKL(mp) < size) {
1066 			/*
1067 			 * Pullup the message in order to get the MAC header
1068 			 * infomation. Note that this is a read-only function,
1069 			 * we keep the input packet intact.
1070 			 */
1071 			if ((tmp = msgpullup(mp, size)) == NULL)
1072 				return (EINVAL);
1073 
1074 			mp = tmp;
1075 		}
1076 		evhp = (struct ether_vlan_header *)mp->b_rptr;
1077 		sap = ntohs(evhp->ether_type);
1078 		(void) mac_sap_verify(dlp->dl_mh, sap, &mhip->mhi_bindsap);
1079 		mhip->mhi_hdrsize = sizeof (struct ether_vlan_header);
1080 		mhip->mhi_tci = ntohs(evhp->ether_tci);
1081 		mhip->mhi_istagged = B_TRUE;
1082 		freemsg(tmp);
1083 
1084 		/*
1085 		 * If this port has a non-zero PVID, then we have to lie to the
1086 		 * caller about the VLAN ID.  It's always zero on receive for
1087 		 * that VLAN.
1088 		 */
1089 		if (pvid != VLAN_ID_NONE && VLAN_ID(mhip->mhi_tci) == pvid) {
1090 			mhip->mhi_tci &= ~(VLAN_ID_MASK << VLAN_ID_SHIFT);
1091 			mhip->mhi_ispvid = B_TRUE;
1092 		}
1093 
1094 		if (VLAN_CFI(mhip->mhi_tci) != ETHER_CFI)
1095 			return (EINVAL);
1096 	} else {
1097 		mhip->mhi_istagged = B_FALSE;
1098 		mhip->mhi_tci = 0;
1099 	}
1100 
1101 	return (0);
1102 }
1103