xref: /titanic_50/usr/src/uts/common/io/dls/dls_link.c (revision 711890bc9379ceea66272dc8d4981812224ea86e)
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 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  * Data-Link Services Module
30  */
31 
32 #include	<sys/types.h>
33 #include	<sys/stream.h>
34 #include	<sys/strsun.h>
35 #include	<sys/strsubr.h>
36 #include	<sys/sysmacros.h>
37 #include	<sys/atomic.h>
38 #include	<sys/modhash.h>
39 #include	<sys/dlpi.h>
40 #include	<sys/ethernet.h>
41 #include	<sys/byteorder.h>
42 #include	<sys/vlan.h>
43 #include	<sys/mac.h>
44 #include	<sys/sdt.h>
45 
46 #include	<sys/dls.h>
47 #include	<sys/dld_impl.h>
48 #include	<sys/dls_impl.h>
49 
50 static kmem_cache_t	*i_dls_link_cachep;
51 static mod_hash_t	*i_dls_link_hash;
52 static uint_t		i_dls_link_count;
53 static krwlock_t	i_dls_link_lock;
54 
55 #define		LINK_HASHSZ	67	/* prime */
56 #define		IMPL_HASHSZ	67	/* prime */
57 
58 /*
59  * Construct a hash key encompassing both DLSAP value and VLAN idenitifier.
60  */
61 #define	MAKE_KEY(_sap, _vid)						\
62 	((mod_hash_key_t)(uintptr_t)					\
63 	(((_sap) << VLAN_ID_SIZE) | (_vid) & VLAN_ID_MASK))
64 
65 /*
66  * Extract the DLSAP value from the hash key.
67  */
68 #define	KEY_SAP(_key)							\
69 	(((uint32_t)(uintptr_t)(_key)) >> VLAN_ID_SIZE)
70 
71 #define	DLS_STRIP_PADDING(pktsize, p) {			\
72 	if (pktsize != 0) {				\
73 		ssize_t delta = pktsize - msgdsize(p);	\
74 							\
75 		if (delta < 0)				\
76 			(void) adjmsg(p, delta);	\
77 	}						\
78 }
79 
80 /*
81  * Private functions.
82  */
83 
84 /*ARGSUSED*/
85 static int
86 i_dls_link_constructor(void *buf, void *arg, int kmflag)
87 {
88 	dls_link_t	*dlp = buf;
89 	char		name[MAXNAMELEN];
90 
91 	bzero(buf, sizeof (dls_link_t));
92 
93 	(void) sprintf(name, "dls_link_t_%p_hash", buf);
94 	dlp->dl_impl_hash = mod_hash_create_idhash(name, IMPL_HASHSZ,
95 	    mod_hash_null_valdtor);
96 
97 	mutex_init(&dlp->dl_lock, NULL, MUTEX_DEFAULT, NULL);
98 	mutex_init(&dlp->dl_promisc_lock, NULL, MUTEX_DEFAULT, NULL);
99 	rw_init(&dlp->dl_impl_lock, NULL, RW_DEFAULT, NULL);
100 	return (0);
101 }
102 
103 /*ARGSUSED*/
104 static void
105 i_dls_link_destructor(void *buf, void *arg)
106 {
107 	dls_link_t	*dlp = buf;
108 
109 	ASSERT(dlp->dl_ref == 0);
110 	ASSERT(dlp->dl_mh == NULL);
111 	ASSERT(dlp->dl_unknowns == 0);
112 
113 	mod_hash_destroy_idhash(dlp->dl_impl_hash);
114 	dlp->dl_impl_hash = NULL;
115 
116 	mutex_destroy(&dlp->dl_lock);
117 	mutex_destroy(&dlp->dl_promisc_lock);
118 	rw_destroy(&dlp->dl_impl_lock);
119 }
120 
121 /*
122  * - Parse the mac header information of the given packet.
123  * - Strip the padding and skip over the header. Note that because some
124  *   DLS consumers only check the db_ref count of the first mblk, we
125  *   pullup the message into a single mblk. Because the original message
126  *   is freed as the result of message pulling up, dls_link_header_info()
127  *   is called again to update the mhi_saddr and mhi_daddr pointers in the
128  *   mhip. Further, the dls_link_header_info() function ensures that the
129  *   size of the pulled message is greater than the MAC header size,
130  *   therefore we can directly advance b_rptr to point at the payload.
131  *
132  * We choose to use a macro for performance reasons.
133  */
134 #define	DLS_PREPARE_PKT(dlp, mp, mhip, err) {				\
135 	mblk_t *nextp = (mp)->b_next;					\
136 	if (((err) = dls_link_header_info((dlp), (mp), (mhip))) == 0) {	\
137 		DLS_STRIP_PADDING((mhip)->mhi_pktsize, (mp));		\
138 		if (MBLKL((mp)) < (mhip)->mhi_hdrsize) {		\
139 			mblk_t *newmp;					\
140 			if ((newmp = msgpullup((mp), -1)) == NULL) {	\
141 				(err) = EINVAL;				\
142 			} else {					\
143 				(mp)->b_next = NULL;			\
144 				freemsg((mp));				\
145 				(mp) = newmp;				\
146 				VERIFY(dls_link_header_info((dlp),	\
147 				    (mp), (mhip)) == 0);		\
148 				(mp)->b_next = nextp;			\
149 				(mp)->b_rptr += (mhip)->mhi_hdrsize;	\
150 			}						\
151 		} else {						\
152 			(mp)->b_rptr += (mhip)->mhi_hdrsize;		\
153 		}							\
154 	}								\
155 }
156 
157 /*
158  * Truncate the chain starting at mp such that all packets in the chain
159  * have identical source and destination addresses, saps, and tag types
160  * (see below).  It returns a pointer to the mblk following the chain,
161  * NULL if there is no further packet following the processed chain.
162  * The countp argument is set to the number of valid packets in the chain.
163  * Note that the whole MAC header (including the VLAN tag if any) in each
164  * packet will be stripped.
165  */
166 static mblk_t *
167 i_dls_link_subchain(dls_link_t *dlp, mblk_t *mp, const mac_header_info_t *mhip,
168     uint_t *countp)
169 {
170 	mblk_t		*prevp;
171 	uint_t		npacket = 1;
172 	size_t		addr_size = dlp->dl_mip->mi_addr_length;
173 	uint16_t	vid = VLAN_ID(mhip->mhi_tci);
174 	uint16_t	pri = VLAN_PRI(mhip->mhi_tci);
175 
176 	/*
177 	 * Compare with subsequent headers until we find one that has
178 	 * differing header information. After checking each packet
179 	 * strip padding and skip over the header.
180 	 */
181 	for (prevp = mp; (mp = mp->b_next) != NULL; prevp = mp) {
182 		mac_header_info_t cmhi;
183 		uint16_t cvid, cpri;
184 		int err;
185 
186 		DLS_PREPARE_PKT(dlp, mp, &cmhi, err);
187 		if (err != 0)
188 			break;
189 
190 		prevp->b_next = mp;
191 
192 		/*
193 		 * The source, destination, sap, and vlan id must all match
194 		 * in a given subchain.
195 		 */
196 		if (memcmp(mhip->mhi_daddr, cmhi.mhi_daddr, addr_size) != 0 ||
197 		    memcmp(mhip->mhi_saddr, cmhi.mhi_saddr, addr_size) != 0 ||
198 		    mhip->mhi_bindsap != cmhi.mhi_bindsap) {
199 			/*
200 			 * Note that we don't need to restore the padding.
201 			 */
202 			mp->b_rptr -= cmhi.mhi_hdrsize;
203 			break;
204 		}
205 
206 		cvid = VLAN_ID(cmhi.mhi_tci);
207 		cpri = VLAN_PRI(cmhi.mhi_tci);
208 
209 		/*
210 		 * There are several types of packets. Packets don't match
211 		 * if they are classified to different type or if they are
212 		 * VLAN packets but belong to different VLANs:
213 		 *
214 		 * packet type		tagged		vid		pri
215 		 * ---------------------------------------------------------
216 		 * untagged		No		zero		zero
217 		 * VLAN packets		Yes		non-zero	-
218 		 * priority tagged	Yes		zero		non-zero
219 		 * 0 tagged		Yes		zero		zero
220 		 */
221 		if ((mhip->mhi_istagged != cmhi.mhi_istagged) ||
222 		    (vid != cvid) || ((vid == VLAN_ID_NONE) &&
223 		    (((pri == 0) && (cpri != 0)) ||
224 		    ((pri != 0) && (cpri == 0))))) {
225 			mp->b_rptr -= cmhi.mhi_hdrsize;
226 			break;
227 		}
228 
229 		npacket++;
230 	}
231 
232 	/*
233 	 * Break the chain at this point and return a pointer to the next
234 	 * sub-chain.
235 	 */
236 	prevp->b_next = NULL;
237 	*countp = npacket;
238 	return (mp);
239 }
240 
241 static void
242 i_dls_head_hold(dls_head_t *dhp)
243 {
244 	atomic_inc_32(&dhp->dh_ref);
245 }
246 
247 static void
248 i_dls_head_rele(dls_head_t *dhp)
249 {
250 	atomic_dec_32(&dhp->dh_ref);
251 }
252 
253 static dls_head_t *
254 i_dls_head_alloc(mod_hash_key_t key)
255 {
256 	dls_head_t	*dhp;
257 
258 	dhp = kmem_zalloc(sizeof (dls_head_t), KM_SLEEP);
259 	dhp->dh_key = key;
260 	return (dhp);
261 }
262 
263 static void
264 i_dls_head_free(dls_head_t *dhp)
265 {
266 	ASSERT(dhp->dh_ref == 0);
267 	kmem_free(dhp, sizeof (dls_head_t));
268 }
269 
270 /*
271  * Try to send mp up to the streams of the given sap and vid. Return B_TRUE
272  * if this message is sent to any streams.
273  * Note that this function will copy the message chain and the original
274  * mp will remain valid after this function
275  */
276 static uint_t
277 i_dls_link_rx_func(dls_link_t *dlp, mac_resource_handle_t mrh,
278     mac_header_info_t *mhip, mblk_t *mp, uint32_t sap, uint16_t vid,
279     boolean_t (*acceptfunc)())
280 {
281 	mod_hash_t	*hash = dlp->dl_impl_hash;
282 	mod_hash_key_t	key;
283 	dls_head_t	*dhp;
284 	dls_impl_t	*dip;
285 	mblk_t		*nmp;
286 	dls_rx_t	di_rx;
287 	void		*di_rx_arg;
288 	uint_t		naccepted = 0;
289 
290 	/*
291 	 * Construct a hash key from the VLAN identifier and the
292 	 * DLSAP that represents dls_impl_t in promiscuous mode.
293 	 */
294 	key = MAKE_KEY(sap, vid);
295 
296 	/*
297 	 * Search the hash table for dls_impl_t eligible to receive
298 	 * a packet chain for this DLSAP/VLAN combination.
299 	 */
300 	rw_enter(&dlp->dl_impl_lock, RW_READER);
301 	if (mod_hash_find(hash, key, (mod_hash_val_t *)&dhp) != 0) {
302 		rw_exit(&dlp->dl_impl_lock);
303 		return (B_FALSE);
304 	}
305 	i_dls_head_hold(dhp);
306 	rw_exit(&dlp->dl_impl_lock);
307 
308 	/*
309 	 * Find dls_impl_t that will accept the sub-chain.
310 	 */
311 	for (dip = dhp->dh_list; dip != NULL; dip = dip->di_nextp) {
312 		if (!acceptfunc(dip, mhip, &di_rx, &di_rx_arg))
313 			continue;
314 
315 		/*
316 		 * We have at least one acceptor.
317 		 */
318 		naccepted ++;
319 
320 		/*
321 		 * There will normally be at least more dls_impl_t
322 		 * (since we've yet to check for non-promiscuous
323 		 * dls_impl_t) so dup the sub-chain.
324 		 */
325 		if ((nmp = copymsgchain(mp)) != NULL)
326 			di_rx(di_rx_arg, mrh, nmp, mhip);
327 	}
328 
329 	/*
330 	 * Release the hold on the dls_impl_t chain now that we have
331 	 * finished walking it.
332 	 */
333 	i_dls_head_rele(dhp);
334 	return (naccepted);
335 }
336 
337 static void
338 i_dls_link_rx(void *arg, mac_resource_handle_t mrh, mblk_t *mp)
339 {
340 	dls_link_t			*dlp = arg;
341 	mod_hash_t			*hash = dlp->dl_impl_hash;
342 	mblk_t				*nextp;
343 	mac_header_info_t		mhi;
344 	dls_head_t			*dhp;
345 	dls_impl_t			*dip;
346 	dls_impl_t			*ndip;
347 	mblk_t				*nmp;
348 	mod_hash_key_t			key;
349 	uint_t				npacket;
350 	boolean_t			accepted;
351 	dls_rx_t			di_rx, ndi_rx;
352 	void				*di_rx_arg, *ndi_rx_arg;
353 	uint16_t			vid;
354 	int				err;
355 
356 	/*
357 	 * Walk the packet chain.
358 	 */
359 	for (; mp != NULL; mp = nextp) {
360 		/*
361 		 * Wipe the accepted state.
362 		 */
363 		accepted = B_FALSE;
364 
365 		DLS_PREPARE_PKT(dlp, mp, &mhi, err);
366 		if (err != 0) {
367 			atomic_add_32(&(dlp->dl_unknowns), 1);
368 			nextp = mp->b_next;
369 			mp->b_next = NULL;
370 			freemsg(mp);
371 			continue;
372 		}
373 
374 		/*
375 		 * Grab the longest sub-chain we can process as a single
376 		 * unit.
377 		 */
378 		nextp = i_dls_link_subchain(dlp, mp, &mhi, &npacket);
379 		ASSERT(npacket != 0);
380 
381 		vid = VLAN_ID(mhi.mhi_tci);
382 
383 		if (mhi.mhi_istagged) {
384 			/*
385 			 * If it is tagged traffic, send it upstream to
386 			 * all dls_impl_t which are attached to the physical
387 			 * link and bound to SAP 0x8100.
388 			 */
389 			if (i_dls_link_rx_func(dlp, mrh, &mhi, mp,
390 			    ETHERTYPE_VLAN, VLAN_ID_NONE, dls_accept) > 0) {
391 				accepted = B_TRUE;
392 			}
393 
394 			/*
395 			 * Don't pass the packets up if they are tagged
396 			 * packets and:
397 			 *  - their VID and priority are both zero (invalid
398 			 *    packets).
399 			 *  - their sap is ETHERTYPE_VLAN and their VID is
400 			 *    zero as they have already been sent upstreams.
401 			 */
402 			if ((vid == VLAN_ID_NONE &&
403 			    VLAN_PRI(mhi.mhi_tci) == 0) ||
404 			    (mhi.mhi_bindsap == ETHERTYPE_VLAN &&
405 			    vid == VLAN_ID_NONE)) {
406 				freemsgchain(mp);
407 				goto loop;
408 			}
409 		}
410 
411 		/*
412 		 * Construct a hash key from the VLAN identifier and the
413 		 * DLSAP.
414 		 */
415 		key = MAKE_KEY(mhi.mhi_bindsap, vid);
416 
417 		/*
418 		 * Search the has table for dls_impl_t eligible to receive
419 		 * a packet chain for this DLSAP/VLAN combination.
420 		 */
421 		rw_enter(&dlp->dl_impl_lock, RW_READER);
422 		if (mod_hash_find(hash, key, (mod_hash_val_t *)&dhp) != 0) {
423 			rw_exit(&dlp->dl_impl_lock);
424 			freemsgchain(mp);
425 			goto loop;
426 		}
427 		i_dls_head_hold(dhp);
428 		rw_exit(&dlp->dl_impl_lock);
429 
430 		/*
431 		 * Find the first dls_impl_t that will accept the sub-chain.
432 		 */
433 		for (dip = dhp->dh_list; dip != NULL; dip = dip->di_nextp)
434 			if (dls_accept(dip, &mhi, &di_rx, &di_rx_arg))
435 				break;
436 
437 		/*
438 		 * If we did not find any dls_impl_t willing to accept the
439 		 * sub-chain then throw it away.
440 		 */
441 		if (dip == NULL) {
442 			i_dls_head_rele(dhp);
443 			freemsgchain(mp);
444 			goto loop;
445 		}
446 
447 		/*
448 		 * We have at least one acceptor.
449 		 */
450 		accepted = B_TRUE;
451 		for (;;) {
452 			/*
453 			 * Find the next dls_impl_t that will accept the
454 			 * sub-chain.
455 			 */
456 			for (ndip = dip->di_nextp; ndip != NULL;
457 			    ndip = ndip->di_nextp)
458 				if (dls_accept(ndip, &mhi, &ndi_rx,
459 				    &ndi_rx_arg))
460 					break;
461 
462 			/*
463 			 * If there are no more dls_impl_t that are willing
464 			 * to accept the sub-chain then we don't need to dup
465 			 * it before handing it to the current one.
466 			 */
467 			if (ndip == NULL) {
468 				di_rx(di_rx_arg, mrh, mp, &mhi);
469 
470 				/*
471 				 * Since there are no more dls_impl_t, we're
472 				 * done.
473 				 */
474 				break;
475 			}
476 
477 			/*
478 			 * There are more dls_impl_t so dup the sub-chain.
479 			 */
480 			if ((nmp = copymsgchain(mp)) != NULL)
481 				di_rx(di_rx_arg, mrh, nmp, &mhi);
482 
483 			dip = ndip;
484 			di_rx = ndi_rx;
485 			di_rx_arg = ndi_rx_arg;
486 		}
487 
488 		/*
489 		 * Release the hold on the dls_impl_t chain now that we have
490 		 * finished walking it.
491 		 */
492 		i_dls_head_rele(dhp);
493 
494 loop:
495 		/*
496 		 * If there were no acceptors then add the packet count to the
497 		 * 'unknown' count.
498 		 */
499 		if (!accepted)
500 			atomic_add_32(&(dlp->dl_unknowns), npacket);
501 	}
502 }
503 
504 /*
505  * Try to send mp up to the DLS_SAP_PROMISC listeners. Return B_TRUE if this
506  * message is sent to any streams.
507  */
508 static uint_t
509 i_dls_link_rx_common_promisc(dls_link_t *dlp, mac_resource_handle_t mrh,
510     mac_header_info_t *mhip, mblk_t *mp, uint16_t vid,
511     boolean_t (*acceptfunc)())
512 {
513 	uint_t naccepted;
514 
515 	naccepted = i_dls_link_rx_func(dlp, mrh, mhip, mp, DLS_SAP_PROMISC,
516 	    vid, acceptfunc);
517 
518 	if (vid != VLAN_ID_NONE) {
519 		naccepted += i_dls_link_rx_func(dlp, mrh, mhip, mp,
520 		    DLS_SAP_PROMISC, VLAN_ID_NONE, acceptfunc);
521 	}
522 	return (naccepted);
523 }
524 
525 static void
526 i_dls_link_rx_common(void *arg, mac_resource_handle_t mrh, mblk_t *mp,
527     boolean_t (*acceptfunc)())
528 {
529 	dls_link_t			*dlp = arg;
530 	mod_hash_t			*hash = dlp->dl_impl_hash;
531 	mblk_t				*nextp;
532 	mac_header_info_t		mhi;
533 	uint16_t			vid, vidkey, pri;
534 	dls_head_t			*dhp;
535 	dls_impl_t			*dip;
536 	mblk_t				*nmp;
537 	mod_hash_key_t			key;
538 	uint_t				npacket;
539 	uint32_t			sap;
540 	boolean_t			accepted;
541 	dls_rx_t			di_rx, fdi_rx;
542 	void				*di_rx_arg, *fdi_rx_arg;
543 	boolean_t			pass2;
544 	int				err;
545 
546 	/*
547 	 * Walk the packet chain.
548 	 */
549 	for (; mp != NULL; mp = nextp) {
550 		/*
551 		 * Wipe the accepted state and the receive information of
552 		 * the first eligible dls_impl_t.
553 		 */
554 		accepted = B_FALSE;
555 		pass2 = B_FALSE;
556 		fdi_rx = NULL;
557 		fdi_rx_arg = NULL;
558 
559 		DLS_PREPARE_PKT(dlp, mp, &mhi, err);
560 		if (err != 0) {
561 			if (acceptfunc == dls_accept)
562 				atomic_add_32(&(dlp->dl_unknowns), 1);
563 			nextp = mp->b_next;
564 			mp->b_next = NULL;
565 			freemsg(mp);
566 			continue;
567 		}
568 
569 		/*
570 		 * Grab the longest sub-chain we can process as a single
571 		 * unit.
572 		 */
573 		nextp = i_dls_link_subchain(dlp, mp, &mhi, &npacket);
574 		ASSERT(npacket != 0);
575 
576 		vid = VLAN_ID(mhi.mhi_tci);
577 		pri = VLAN_PRI(mhi.mhi_tci);
578 
579 		vidkey = vid;
580 
581 		/*
582 		 * Note that we need to first send to the dls_impl_t
583 		 * in promiscuous mode in order to avoid the packet reordering
584 		 * when snooping.
585 		 */
586 		if (i_dls_link_rx_common_promisc(dlp, mrh, &mhi, mp, vidkey,
587 		    acceptfunc) > 0) {
588 			accepted = B_TRUE;
589 		}
590 
591 		/*
592 		 * Non promisc case. Two passes:
593 		 *   1. send tagged packets to ETHERTYPE_VLAN listeners
594 		 *   2. send packets to listeners bound to the specific SAP.
595 		 */
596 		if (mhi.mhi_istagged) {
597 			vidkey = VLAN_ID_NONE;
598 			sap = ETHERTYPE_VLAN;
599 		} else {
600 			goto non_promisc_loop;
601 		}
602 non_promisc:
603 		/*
604 		 * Construct a hash key from the VLAN identifier and the
605 		 * DLSAP.
606 		 */
607 		key = MAKE_KEY(sap, vidkey);
608 
609 		/*
610 		 * Search the has table for dls_impl_t eligible to receive
611 		 * a packet chain for this DLSAP/VLAN combination.
612 		 */
613 		rw_enter(&dlp->dl_impl_lock, RW_READER);
614 		if (mod_hash_find(hash, key, (mod_hash_val_t *)&dhp) != 0) {
615 			rw_exit(&dlp->dl_impl_lock);
616 			goto non_promisc_loop;
617 		}
618 		i_dls_head_hold(dhp);
619 		rw_exit(&dlp->dl_impl_lock);
620 
621 		/*
622 		 * Find the first dls_impl_t that will accept the sub-chain.
623 		 */
624 		for (dip = dhp->dh_list; dip != NULL; dip = dip->di_nextp) {
625 			if (!acceptfunc(dip, &mhi, &di_rx, &di_rx_arg))
626 				continue;
627 
628 			accepted = B_TRUE;
629 
630 			/*
631 			 * To avoid the extra copymsgchain(), if this
632 			 * is the first eligible dls_impl_t, remember required
633 			 * information and send up the message afterwards.
634 			 */
635 			if (fdi_rx == NULL) {
636 				fdi_rx = di_rx;
637 				fdi_rx_arg = di_rx_arg;
638 				continue;
639 			}
640 
641 			if ((nmp = copymsgchain(mp)) != NULL)
642 				di_rx(di_rx_arg, mrh, nmp, &mhi);
643 		}
644 
645 		/*
646 		 * Release the hold on the dls_impl_t chain now that we have
647 		 * finished walking it.
648 		 */
649 		i_dls_head_rele(dhp);
650 
651 non_promisc_loop:
652 		/*
653 		 * Don't pass the packets up again if:
654 		 * - First pass is done and the packets are tagged and their:
655 		 *	- VID and priority are both zero (invalid packets).
656 		 *	- their sap is ETHERTYPE_VLAN and their VID is zero
657 		 *	  (they have already been sent upstreams).
658 		 *  - Second pass is done:
659 		 */
660 		if (pass2 || (mhi.mhi_istagged &&
661 		    ((vid == VLAN_ID_NONE && pri == 0) ||
662 		    (mhi.mhi_bindsap == ETHERTYPE_VLAN &&
663 		    vid == VLAN_ID_NONE)))) {
664 			/*
665 			 * Send the message up to the first eligible dls_impl_t.
666 			 */
667 			if (fdi_rx != NULL)
668 				fdi_rx(fdi_rx_arg, mrh, mp, &mhi);
669 			else
670 				freemsgchain(mp);
671 		} else {
672 			vidkey = vid;
673 			sap = mhi.mhi_bindsap;
674 			pass2 = B_TRUE;
675 			goto non_promisc;
676 		}
677 
678 		/*
679 		 * If there were no acceptors then add the packet count to the
680 		 * 'unknown' count.
681 		 */
682 		if (!accepted && (acceptfunc == dls_accept))
683 			atomic_add_32(&(dlp->dl_unknowns), npacket);
684 	}
685 }
686 
687 static void
688 i_dls_link_rx_promisc(void *arg, mac_resource_handle_t mrh, mblk_t *mp)
689 {
690 	i_dls_link_rx_common(arg, mrh, mp, dls_accept);
691 }
692 
693 static void
694 i_dls_link_txloop(void *arg, mblk_t *mp)
695 {
696 	i_dls_link_rx_common(arg, NULL, mp, dls_accept_loopback);
697 }
698 
699 /*ARGSUSED*/
700 static uint_t
701 i_dls_link_walk(mod_hash_key_t key, mod_hash_val_t *val, void *arg)
702 {
703 	boolean_t	*promiscp = arg;
704 	uint32_t	sap = KEY_SAP(key);
705 
706 	if (sap == DLS_SAP_PROMISC) {
707 		*promiscp = B_TRUE;
708 		return (MH_WALK_TERMINATE);
709 	}
710 
711 	return (MH_WALK_CONTINUE);
712 }
713 
714 static int
715 i_dls_link_create(const char *name, uint_t ddi_instance, dls_link_t **dlpp)
716 {
717 	dls_link_t		*dlp;
718 
719 	/*
720 	 * Allocate a new dls_link_t structure.
721 	 */
722 	dlp = kmem_cache_alloc(i_dls_link_cachep, KM_SLEEP);
723 
724 	/*
725 	 * Name the dls_link_t after the MAC interface it represents.
726 	 */
727 	(void) strlcpy(dlp->dl_name, name, sizeof (dlp->dl_name));
728 	dlp->dl_ddi_instance = ddi_instance;
729 
730 	/*
731 	 * Set the packet loopback function for use when the MAC is in
732 	 * promiscuous mode, and initialize promiscuous bookeeping fields.
733 	 */
734 	dlp->dl_txloop = i_dls_link_txloop;
735 	dlp->dl_npromisc = 0;
736 	dlp->dl_mth = NULL;
737 
738 	*dlpp = dlp;
739 	return (0);
740 }
741 
742 static void
743 i_dls_link_destroy(dls_link_t *dlp)
744 {
745 	ASSERT(dlp->dl_npromisc == 0);
746 	ASSERT(dlp->dl_nactive == 0);
747 	ASSERT(dlp->dl_mth == NULL);
748 	ASSERT(dlp->dl_macref == 0);
749 	ASSERT(dlp->dl_mh == NULL);
750 	ASSERT(dlp->dl_mip == NULL);
751 	ASSERT(dlp->dl_impl_count == 0);
752 	ASSERT(dlp->dl_mrh == NULL);
753 
754 	/*
755 	 * Free the structure back to the cache.
756 	 */
757 	dlp->dl_unknowns = 0;
758 	kmem_cache_free(i_dls_link_cachep, dlp);
759 }
760 
761 /*
762  * Module initialization functions.
763  */
764 
765 void
766 dls_link_init(void)
767 {
768 	/*
769 	 * Create a kmem_cache of dls_link_t structures.
770 	 */
771 	i_dls_link_cachep = kmem_cache_create("dls_link_cache",
772 	    sizeof (dls_link_t), 0, i_dls_link_constructor,
773 	    i_dls_link_destructor, NULL, NULL, NULL, 0);
774 	ASSERT(i_dls_link_cachep != NULL);
775 
776 	/*
777 	 * Create a dls_link_t hash table and associated lock.
778 	 */
779 	i_dls_link_hash = mod_hash_create_extended("dls_link_hash",
780 	    IMPL_HASHSZ, mod_hash_null_keydtor, mod_hash_null_valdtor,
781 	    mod_hash_bystr, NULL, mod_hash_strkey_cmp, KM_SLEEP);
782 	rw_init(&i_dls_link_lock, NULL, RW_DEFAULT, NULL);
783 	i_dls_link_count = 0;
784 }
785 
786 int
787 dls_link_fini(void)
788 {
789 	if (i_dls_link_count > 0)
790 		return (EBUSY);
791 
792 	/*
793 	 * Destroy the kmem_cache.
794 	 */
795 	kmem_cache_destroy(i_dls_link_cachep);
796 
797 	/*
798 	 * Destroy the hash table and associated lock.
799 	 */
800 	mod_hash_destroy_hash(i_dls_link_hash);
801 	rw_destroy(&i_dls_link_lock);
802 	return (0);
803 }
804 
805 /*
806  * Exported functions.
807  */
808 
809 int
810 dls_link_hold(const char *name, uint_t ddi_instance, dls_link_t **dlpp)
811 {
812 	dls_link_t		*dlp;
813 	int			err;
814 
815 	/*
816 	 * Look up a dls_link_t corresponding to the given mac_handle_t
817 	 * in the global hash table. We need to hold i_dls_link_lock in
818 	 * order to atomically find and insert a dls_link_t into the
819 	 * hash table.
820 	 */
821 	rw_enter(&i_dls_link_lock, RW_WRITER);
822 	if ((err = mod_hash_find(i_dls_link_hash, (mod_hash_key_t)name,
823 	    (mod_hash_val_t *)&dlp)) == 0)
824 		goto done;
825 
826 	ASSERT(err == MH_ERR_NOTFOUND);
827 
828 	/*
829 	 * We didn't find anything so we need to create one.
830 	 */
831 	if ((err = i_dls_link_create(name, ddi_instance, &dlp)) != 0) {
832 		rw_exit(&i_dls_link_lock);
833 		return (err);
834 	}
835 
836 	/*
837 	 * Insert the dls_link_t.
838 	 */
839 	err = mod_hash_insert(i_dls_link_hash, (mod_hash_key_t)name,
840 	    (mod_hash_val_t)dlp);
841 	ASSERT(err == 0);
842 
843 	i_dls_link_count++;
844 	ASSERT(i_dls_link_count != 0);
845 
846 done:
847 	/*
848 	 * Bump the reference count and hand back the reference.
849 	 */
850 	dlp->dl_ref++;
851 	*dlpp = dlp;
852 	rw_exit(&i_dls_link_lock);
853 	return (0);
854 }
855 
856 void
857 dls_link_rele(dls_link_t *dlp)
858 {
859 	mod_hash_val_t	val;
860 
861 	rw_enter(&i_dls_link_lock, RW_WRITER);
862 
863 	/*
864 	 * Check if there are any more references.
865 	 */
866 	if (--dlp->dl_ref != 0) {
867 		/*
868 		 * There are more references so there's nothing more to do.
869 		 */
870 		goto done;
871 	}
872 
873 	(void) mod_hash_remove(i_dls_link_hash,
874 	    (mod_hash_key_t)dlp->dl_name, &val);
875 	ASSERT(dlp == (dls_link_t *)val);
876 
877 	/*
878 	 * Destroy the dls_link_t.
879 	 */
880 	i_dls_link_destroy(dlp);
881 	ASSERT(i_dls_link_count > 0);
882 	i_dls_link_count--;
883 done:
884 	rw_exit(&i_dls_link_lock);
885 }
886 
887 int
888 dls_mac_hold(dls_link_t *dlp)
889 {
890 	int err = 0;
891 
892 	mutex_enter(&dlp->dl_lock);
893 
894 	ASSERT(IMPLY(dlp->dl_macref != 0, dlp->dl_mh != NULL));
895 	ASSERT(IMPLY(dlp->dl_macref == 0, dlp->dl_mh == NULL));
896 
897 	if (dlp->dl_macref == 0) {
898 		/*
899 		 * First reference; hold open the MAC interface.
900 		 */
901 		err = mac_open(dlp->dl_name, dlp->dl_ddi_instance, &dlp->dl_mh);
902 		if (err != 0)
903 			goto done;
904 
905 		dlp->dl_mip = mac_info(dlp->dl_mh);
906 	}
907 
908 	dlp->dl_macref++;
909 done:
910 	mutex_exit(&dlp->dl_lock);
911 	return (err);
912 }
913 
914 void
915 dls_mac_rele(dls_link_t *dlp)
916 {
917 	mutex_enter(&dlp->dl_lock);
918 	ASSERT(dlp->dl_mh != NULL);
919 
920 	if (--dlp->dl_macref == 0) {
921 		mac_close(dlp->dl_mh);
922 		dlp->dl_mh = NULL;
923 		dlp->dl_mip = NULL;
924 	}
925 	mutex_exit(&dlp->dl_lock);
926 }
927 
928 void
929 dls_link_add(dls_link_t *dlp, uint32_t sap, dls_impl_t *dip)
930 {
931 	dls_vlan_t	*dvp = dip->di_dvp;
932 	mod_hash_t	*hash = dlp->dl_impl_hash;
933 	mod_hash_key_t	key;
934 	dls_head_t	*dhp;
935 	dls_impl_t	*p;
936 	mac_rx_t	rx;
937 	int		err;
938 	boolean_t	promisc = B_FALSE;
939 
940 	/*
941 	 * Generate a hash key based on the sap and the VLAN id.
942 	 */
943 	key = MAKE_KEY(sap, dvp->dv_id);
944 
945 	/*
946 	 * We need dl_lock here because we want to be able to walk
947 	 * the hash table *and* set the mac rx func atomically. if
948 	 * these two operations are separate, someone else could
949 	 * insert/remove dls_impl_t from the hash table after we
950 	 * drop the hash lock and this could cause our chosen rx
951 	 * func to be incorrect. note that we cannot call mac_rx_add
952 	 * when holding the hash lock because this can cause deadlock.
953 	 */
954 	mutex_enter(&dlp->dl_lock);
955 
956 	/*
957 	 * Search the table for a list head with this key.
958 	 */
959 	rw_enter(&dlp->dl_impl_lock, RW_WRITER);
960 
961 	if ((err = mod_hash_find(hash, key, (mod_hash_val_t *)&dhp)) != 0) {
962 		ASSERT(err == MH_ERR_NOTFOUND);
963 
964 		dhp = i_dls_head_alloc(key);
965 		err = mod_hash_insert(hash, key, (mod_hash_val_t)dhp);
966 		ASSERT(err == 0);
967 	}
968 
969 	/*
970 	 * Add the dls_impl_t to the head of the list.
971 	 */
972 	ASSERT(dip->di_nextp == NULL);
973 	p = dhp->dh_list;
974 	dip->di_nextp = p;
975 	dhp->dh_list = dip;
976 
977 	/*
978 	 * Save a pointer to the list head.
979 	 */
980 	dip->di_headp = dhp;
981 	dlp->dl_impl_count++;
982 
983 	/*
984 	 * Walk the bound dls_impl_t to see if there are any
985 	 * in promiscuous 'all sap' mode.
986 	 */
987 	mod_hash_walk(hash, i_dls_link_walk, (void *)&promisc);
988 	rw_exit(&dlp->dl_impl_lock);
989 
990 	/*
991 	 * If there are then we need to use a receive routine
992 	 * which will route packets to those dls_impl_t as well
993 	 * as ones bound to the  DLSAP of the packet.
994 	 */
995 	if (promisc)
996 		rx = i_dls_link_rx_promisc;
997 	else
998 		rx = i_dls_link_rx;
999 
1000 	/* Replace the existing receive function if there is one. */
1001 	if (dlp->dl_mrh != NULL)
1002 		mac_rx_remove(dlp->dl_mh, dlp->dl_mrh);
1003 	dlp->dl_mrh = mac_rx_add(dlp->dl_mh, rx, (void *)dlp);
1004 	mutex_exit(&dlp->dl_lock);
1005 }
1006 
1007 void
1008 dls_link_remove(dls_link_t *dlp, dls_impl_t *dip)
1009 {
1010 	mod_hash_t	*hash = dlp->dl_impl_hash;
1011 	dls_impl_t	**pp;
1012 	dls_impl_t	*p;
1013 	dls_head_t	*dhp;
1014 	mac_rx_t	rx;
1015 
1016 	/*
1017 	 * We need dl_lock here because we want to be able to walk
1018 	 * the hash table *and* set the mac rx func atomically. if
1019 	 * these two operations are separate, someone else could
1020 	 * insert/remove dls_impl_t from the hash table after we
1021 	 * drop the hash lock and this could cause our chosen rx
1022 	 * func to be incorrect. note that we cannot call mac_rx_add
1023 	 * when holding the hash lock because this can cause deadlock.
1024 	 */
1025 	mutex_enter(&dlp->dl_lock);
1026 	rw_enter(&dlp->dl_impl_lock, RW_WRITER);
1027 
1028 	/*
1029 	 * Poll the hash table entry until all references have been dropped.
1030 	 * We need to drop all locks before sleeping because we don't want
1031 	 * the interrupt handler to block. We set di_removing here to
1032 	 * tell the receive callbacks not to pass up packets anymore.
1033 	 * This is only a hint to quicken the decrease of the refcnt so
1034 	 * the assignment need not be protected by any lock.
1035 	 */
1036 	dhp = dip->di_headp;
1037 	dip->di_removing = B_TRUE;
1038 	while (dhp->dh_ref != 0) {
1039 		rw_exit(&dlp->dl_impl_lock);
1040 		mutex_exit(&dlp->dl_lock);
1041 		delay(drv_usectohz(1000));	/* 1ms delay */
1042 		mutex_enter(&dlp->dl_lock);
1043 		rw_enter(&dlp->dl_impl_lock, RW_WRITER);
1044 	}
1045 
1046 	/*
1047 	 * Walk the list and remove the dls_impl_t.
1048 	 */
1049 	for (pp = &dhp->dh_list; (p = *pp) != NULL; pp = &(p->di_nextp)) {
1050 		if (p == dip)
1051 			break;
1052 	}
1053 	ASSERT(p != NULL);
1054 	*pp = p->di_nextp;
1055 	p->di_nextp = NULL;
1056 
1057 	ASSERT(dlp->dl_impl_count > 0);
1058 	dlp->dl_impl_count--;
1059 
1060 	if (dhp->dh_list == NULL) {
1061 		mod_hash_val_t	val = NULL;
1062 
1063 		/*
1064 		 * The list is empty so remove the hash table entry.
1065 		 */
1066 		(void) mod_hash_remove(hash, dhp->dh_key, &val);
1067 		ASSERT(dhp == (dls_head_t *)val);
1068 		i_dls_head_free(dhp);
1069 	}
1070 	dip->di_removing = B_FALSE;
1071 
1072 	/*
1073 	 * If there are no dls_impl_t then there's no need to register a
1074 	 * receive function with the mac.
1075 	 */
1076 	if (dlp->dl_impl_count == 0) {
1077 		rw_exit(&dlp->dl_impl_lock);
1078 		mac_rx_remove(dlp->dl_mh, dlp->dl_mrh);
1079 		dlp->dl_mrh = NULL;
1080 	} else {
1081 		boolean_t promisc = B_FALSE;
1082 
1083 		/*
1084 		 * Walk the bound dls_impl_t to see if there are any
1085 		 * in promiscuous 'all sap' mode.
1086 		 */
1087 		mod_hash_walk(hash, i_dls_link_walk, (void *)&promisc);
1088 		rw_exit(&dlp->dl_impl_lock);
1089 
1090 		/*
1091 		 * If there are then we need to use a receive routine
1092 		 * which will route packets to those dls_impl_t as well
1093 		 * as ones bound to the  DLSAP of the packet.
1094 		 */
1095 		if (promisc)
1096 			rx = i_dls_link_rx_promisc;
1097 		else
1098 			rx = i_dls_link_rx;
1099 
1100 		mac_rx_remove(dlp->dl_mh, dlp->dl_mrh);
1101 		dlp->dl_mrh = mac_rx_add(dlp->dl_mh, rx, (void *)dlp);
1102 	}
1103 	mutex_exit(&dlp->dl_lock);
1104 }
1105 
1106 int
1107 dls_link_header_info(dls_link_t *dlp, mblk_t *mp, mac_header_info_t *mhip)
1108 {
1109 	boolean_t	is_ethernet = (dlp->dl_mip->mi_media == DL_ETHER);
1110 	int		err = 0;
1111 
1112 	/*
1113 	 * Packets should always be at least 16 bit aligned.
1114 	 */
1115 	ASSERT(IS_P2ALIGNED(mp->b_rptr, sizeof (uint16_t)));
1116 
1117 	if ((err = mac_header_info(dlp->dl_mh, mp, mhip)) != 0)
1118 		return (err);
1119 
1120 	/*
1121 	 * If this is a VLAN-tagged Ethernet packet, then the SAP in the
1122 	 * mac_header_info_t as returned by mac_header_info() is
1123 	 * ETHERTYPE_VLAN. We need to grab the ethertype from the VLAN header.
1124 	 */
1125 	if (is_ethernet && (mhip->mhi_bindsap == ETHERTYPE_VLAN)) {
1126 		struct ether_vlan_header *evhp;
1127 		uint16_t sap;
1128 		mblk_t *tmp = NULL;
1129 		size_t size;
1130 
1131 		size = sizeof (struct ether_vlan_header);
1132 		if (MBLKL(mp) < size) {
1133 			/*
1134 			 * Pullup the message in order to get the MAC header
1135 			 * infomation. Note that this is a read-only function,
1136 			 * we keep the input packet intact.
1137 			 */
1138 			if ((tmp = msgpullup(mp, size)) == NULL)
1139 				return (EINVAL);
1140 
1141 			mp = tmp;
1142 		}
1143 		evhp = (struct ether_vlan_header *)mp->b_rptr;
1144 		sap = ntohs(evhp->ether_type);
1145 		(void) mac_sap_verify(dlp->dl_mh, sap, &mhip->mhi_bindsap);
1146 		mhip->mhi_hdrsize = sizeof (struct ether_vlan_header);
1147 		mhip->mhi_tci = ntohs(evhp->ether_tci);
1148 		mhip->mhi_istagged = B_TRUE;
1149 		freemsg(tmp);
1150 
1151 		if (VLAN_CFI(mhip->mhi_tci) != ETHER_CFI)
1152 			return (EINVAL);
1153 	} else {
1154 		mhip->mhi_istagged = B_FALSE;
1155 		mhip->mhi_tci = 0;
1156 	}
1157 	return (0);
1158 }
1159