xref: /titanic_44/usr/src/uts/common/io/dls/dls_link.c (revision d0538f66491267879b7418b21ad78e3dcc2dcc83)
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 2007 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 void
694 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, 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 
729 	/*
730 	 * Initialize promiscuous bookkeeping fields.
731 	 */
732 	dlp->dl_npromisc = 0;
733 	dlp->dl_mth = NULL;
734 
735 	*dlpp = dlp;
736 	return (0);
737 }
738 
739 static void
740 i_dls_link_destroy(dls_link_t *dlp)
741 {
742 	ASSERT(dlp->dl_npromisc == 0);
743 	ASSERT(dlp->dl_nactive == 0);
744 	ASSERT(dlp->dl_mth == NULL);
745 	ASSERT(dlp->dl_macref == 0);
746 	ASSERT(dlp->dl_mh == NULL);
747 	ASSERT(dlp->dl_mip == NULL);
748 	ASSERT(dlp->dl_impl_count == 0);
749 	ASSERT(dlp->dl_mrh == NULL);
750 
751 	/*
752 	 * Free the structure back to the cache.
753 	 */
754 	dlp->dl_unknowns = 0;
755 	kmem_cache_free(i_dls_link_cachep, dlp);
756 }
757 
758 /*
759  * Module initialization functions.
760  */
761 
762 void
763 dls_link_init(void)
764 {
765 	/*
766 	 * Create a kmem_cache of dls_link_t structures.
767 	 */
768 	i_dls_link_cachep = kmem_cache_create("dls_link_cache",
769 	    sizeof (dls_link_t), 0, i_dls_link_constructor,
770 	    i_dls_link_destructor, NULL, NULL, NULL, 0);
771 	ASSERT(i_dls_link_cachep != NULL);
772 
773 	/*
774 	 * Create a dls_link_t hash table and associated lock.
775 	 */
776 	i_dls_link_hash = mod_hash_create_extended("dls_link_hash",
777 	    IMPL_HASHSZ, mod_hash_null_keydtor, mod_hash_null_valdtor,
778 	    mod_hash_bystr, NULL, mod_hash_strkey_cmp, KM_SLEEP);
779 	rw_init(&i_dls_link_lock, NULL, RW_DEFAULT, NULL);
780 	i_dls_link_count = 0;
781 }
782 
783 int
784 dls_link_fini(void)
785 {
786 	if (i_dls_link_count > 0)
787 		return (EBUSY);
788 
789 	/*
790 	 * Destroy the kmem_cache.
791 	 */
792 	kmem_cache_destroy(i_dls_link_cachep);
793 
794 	/*
795 	 * Destroy the hash table and associated lock.
796 	 */
797 	mod_hash_destroy_hash(i_dls_link_hash);
798 	rw_destroy(&i_dls_link_lock);
799 	return (0);
800 }
801 
802 /*
803  * Exported functions.
804  */
805 
806 int
807 dls_link_hold(const char *name, dls_link_t **dlpp)
808 {
809 	dls_link_t		*dlp;
810 	int			err;
811 
812 	/*
813 	 * Look up a dls_link_t corresponding to the given mac_handle_t
814 	 * in the global hash table. We need to hold i_dls_link_lock in
815 	 * order to atomically find and insert a dls_link_t into the
816 	 * hash table.
817 	 */
818 	rw_enter(&i_dls_link_lock, RW_WRITER);
819 	if ((err = mod_hash_find(i_dls_link_hash, (mod_hash_key_t)name,
820 	    (mod_hash_val_t *)&dlp)) == 0)
821 		goto done;
822 
823 	ASSERT(err == MH_ERR_NOTFOUND);
824 
825 	/*
826 	 * We didn't find anything so we need to create one.
827 	 */
828 	if ((err = i_dls_link_create(name, &dlp)) != 0) {
829 		rw_exit(&i_dls_link_lock);
830 		return (err);
831 	}
832 
833 	/*
834 	 * Insert the dls_link_t.
835 	 */
836 	err = mod_hash_insert(i_dls_link_hash, (mod_hash_key_t)name,
837 	    (mod_hash_val_t)dlp);
838 	ASSERT(err == 0);
839 
840 	i_dls_link_count++;
841 	ASSERT(i_dls_link_count != 0);
842 
843 done:
844 	/*
845 	 * Bump the reference count and hand back the reference.
846 	 */
847 	dlp->dl_ref++;
848 	*dlpp = dlp;
849 	rw_exit(&i_dls_link_lock);
850 	return (0);
851 }
852 
853 void
854 dls_link_rele(dls_link_t *dlp)
855 {
856 	mod_hash_val_t	val;
857 
858 	rw_enter(&i_dls_link_lock, RW_WRITER);
859 
860 	/*
861 	 * Check if there are any more references.
862 	 */
863 	if (--dlp->dl_ref != 0) {
864 		/*
865 		 * There are more references so there's nothing more to do.
866 		 */
867 		goto done;
868 	}
869 
870 	(void) mod_hash_remove(i_dls_link_hash,
871 	    (mod_hash_key_t)dlp->dl_name, &val);
872 	ASSERT(dlp == (dls_link_t *)val);
873 
874 	/*
875 	 * Destroy the dls_link_t.
876 	 */
877 	i_dls_link_destroy(dlp);
878 	ASSERT(i_dls_link_count > 0);
879 	i_dls_link_count--;
880 done:
881 	rw_exit(&i_dls_link_lock);
882 }
883 
884 int
885 dls_mac_hold(dls_link_t *dlp)
886 {
887 	int err = 0;
888 
889 	mutex_enter(&dlp->dl_lock);
890 
891 	ASSERT(IMPLY(dlp->dl_macref != 0, dlp->dl_mh != NULL));
892 	ASSERT(IMPLY(dlp->dl_macref == 0, dlp->dl_mh == NULL));
893 
894 	if (dlp->dl_macref == 0) {
895 		/*
896 		 * First reference; hold open the MAC interface.
897 		 */
898 		err = mac_open(dlp->dl_name, &dlp->dl_mh);
899 		if (err != 0)
900 			goto done;
901 
902 		dlp->dl_mip = mac_info(dlp->dl_mh);
903 	}
904 
905 	dlp->dl_macref++;
906 done:
907 	mutex_exit(&dlp->dl_lock);
908 	return (err);
909 }
910 
911 void
912 dls_mac_rele(dls_link_t *dlp)
913 {
914 	mutex_enter(&dlp->dl_lock);
915 	ASSERT(dlp->dl_mh != NULL);
916 
917 	if (--dlp->dl_macref == 0) {
918 		mac_rx_remove_wait(dlp->dl_mh);
919 		mac_close(dlp->dl_mh);
920 		dlp->dl_mh = NULL;
921 		dlp->dl_mip = NULL;
922 	}
923 	mutex_exit(&dlp->dl_lock);
924 }
925 
926 void
927 dls_link_add(dls_link_t *dlp, uint32_t sap, dls_impl_t *dip)
928 {
929 	dls_vlan_t	*dvp = dip->di_dvp;
930 	mod_hash_t	*hash = dlp->dl_impl_hash;
931 	mod_hash_key_t	key;
932 	dls_head_t	*dhp;
933 	dls_impl_t	*p;
934 	mac_rx_t	rx;
935 	int		err;
936 	boolean_t	promisc = B_FALSE;
937 
938 	/*
939 	 * Generate a hash key based on the sap and the VLAN id.
940 	 */
941 	key = MAKE_KEY(sap, dvp->dv_id);
942 
943 	/*
944 	 * We need dl_lock here because we want to be able to walk
945 	 * the hash table *and* set the mac rx func atomically. if
946 	 * these two operations are separate, someone else could
947 	 * insert/remove dls_impl_t from the hash table after we
948 	 * drop the hash lock and this could cause our chosen rx
949 	 * func to be incorrect. note that we cannot call mac_rx_add
950 	 * when holding the hash lock because this can cause deadlock.
951 	 */
952 	mutex_enter(&dlp->dl_lock);
953 
954 	/*
955 	 * Search the table for a list head with this key.
956 	 */
957 	rw_enter(&dlp->dl_impl_lock, RW_WRITER);
958 
959 	if ((err = mod_hash_find(hash, key, (mod_hash_val_t *)&dhp)) != 0) {
960 		ASSERT(err == MH_ERR_NOTFOUND);
961 
962 		dhp = i_dls_head_alloc(key);
963 		err = mod_hash_insert(hash, key, (mod_hash_val_t)dhp);
964 		ASSERT(err == 0);
965 	}
966 
967 	/*
968 	 * Add the dls_impl_t to the head of the list.
969 	 */
970 	ASSERT(dip->di_nextp == NULL);
971 	p = dhp->dh_list;
972 	dip->di_nextp = p;
973 	dhp->dh_list = dip;
974 
975 	/*
976 	 * Save a pointer to the list head.
977 	 */
978 	dip->di_headp = dhp;
979 	dlp->dl_impl_count++;
980 
981 	/*
982 	 * Walk the bound dls_impl_t to see if there are any
983 	 * in promiscuous 'all sap' mode.
984 	 */
985 	mod_hash_walk(hash, i_dls_link_walk, (void *)&promisc);
986 	rw_exit(&dlp->dl_impl_lock);
987 
988 	/*
989 	 * If there are then we need to use a receive routine
990 	 * which will route packets to those dls_impl_t as well
991 	 * as ones bound to the  DLSAP of the packet.
992 	 */
993 	if (promisc)
994 		rx = i_dls_link_rx_promisc;
995 	else
996 		rx = i_dls_link_rx;
997 
998 	/* Replace the existing receive function if there is one. */
999 	if (dlp->dl_mrh != NULL)
1000 		mac_rx_remove(dlp->dl_mh, dlp->dl_mrh, B_FALSE);
1001 	dlp->dl_mrh = mac_active_rx_add(dlp->dl_mh, rx, (void *)dlp);
1002 	mutex_exit(&dlp->dl_lock);
1003 }
1004 
1005 void
1006 dls_link_remove(dls_link_t *dlp, dls_impl_t *dip)
1007 {
1008 	mod_hash_t	*hash = dlp->dl_impl_hash;
1009 	dls_impl_t	**pp;
1010 	dls_impl_t	*p;
1011 	dls_head_t	*dhp;
1012 	mac_rx_t	rx;
1013 
1014 	/*
1015 	 * We need dl_lock here because we want to be able to walk
1016 	 * the hash table *and* set the mac rx func atomically. if
1017 	 * these two operations are separate, someone else could
1018 	 * insert/remove dls_impl_t from the hash table after we
1019 	 * drop the hash lock and this could cause our chosen rx
1020 	 * func to be incorrect. note that we cannot call mac_rx_add
1021 	 * when holding the hash lock because this can cause deadlock.
1022 	 */
1023 	mutex_enter(&dlp->dl_lock);
1024 	rw_enter(&dlp->dl_impl_lock, RW_WRITER);
1025 
1026 	/*
1027 	 * Poll the hash table entry until all references have been dropped.
1028 	 * We need to drop all locks before sleeping because we don't want
1029 	 * the interrupt handler to block. We set di_removing here to
1030 	 * tell the receive callbacks not to pass up packets anymore.
1031 	 * This is only a hint to quicken the decrease of the refcnt so
1032 	 * the assignment need not be protected by any lock.
1033 	 */
1034 	dhp = dip->di_headp;
1035 	dip->di_removing = B_TRUE;
1036 	while (dhp->dh_ref != 0) {
1037 		rw_exit(&dlp->dl_impl_lock);
1038 		mutex_exit(&dlp->dl_lock);
1039 		delay(drv_usectohz(1000));	/* 1ms delay */
1040 		mutex_enter(&dlp->dl_lock);
1041 		rw_enter(&dlp->dl_impl_lock, RW_WRITER);
1042 	}
1043 
1044 	/*
1045 	 * Walk the list and remove the dls_impl_t.
1046 	 */
1047 	for (pp = &dhp->dh_list; (p = *pp) != NULL; pp = &(p->di_nextp)) {
1048 		if (p == dip)
1049 			break;
1050 	}
1051 	ASSERT(p != NULL);
1052 	*pp = p->di_nextp;
1053 	p->di_nextp = NULL;
1054 
1055 	ASSERT(dlp->dl_impl_count > 0);
1056 	dlp->dl_impl_count--;
1057 
1058 	if (dhp->dh_list == NULL) {
1059 		mod_hash_val_t	val = NULL;
1060 
1061 		/*
1062 		 * The list is empty so remove the hash table entry.
1063 		 */
1064 		(void) mod_hash_remove(hash, dhp->dh_key, &val);
1065 		ASSERT(dhp == (dls_head_t *)val);
1066 		i_dls_head_free(dhp);
1067 	}
1068 	dip->di_removing = B_FALSE;
1069 
1070 	/*
1071 	 * If there are no dls_impl_t then there's no need to register a
1072 	 * receive function with the mac.
1073 	 */
1074 	if (dlp->dl_impl_count == 0) {
1075 		rw_exit(&dlp->dl_impl_lock);
1076 		mac_rx_remove(dlp->dl_mh, dlp->dl_mrh, B_FALSE);
1077 		dlp->dl_mrh = NULL;
1078 	} else {
1079 		boolean_t promisc = B_FALSE;
1080 
1081 		/*
1082 		 * Walk the bound dls_impl_t to see if there are any
1083 		 * in promiscuous 'all sap' mode.
1084 		 */
1085 		mod_hash_walk(hash, i_dls_link_walk, (void *)&promisc);
1086 		rw_exit(&dlp->dl_impl_lock);
1087 
1088 		/*
1089 		 * If there are then we need to use a receive routine
1090 		 * which will route packets to those dls_impl_t as well
1091 		 * as ones bound to the  DLSAP of the packet.
1092 		 */
1093 		if (promisc)
1094 			rx = i_dls_link_rx_promisc;
1095 		else
1096 			rx = i_dls_link_rx;
1097 
1098 		mac_rx_remove(dlp->dl_mh, dlp->dl_mrh, B_FALSE);
1099 		dlp->dl_mrh = mac_active_rx_add(dlp->dl_mh, rx, (void *)dlp);
1100 	}
1101 	mutex_exit(&dlp->dl_lock);
1102 }
1103 
1104 int
1105 dls_link_header_info(dls_link_t *dlp, mblk_t *mp, mac_header_info_t *mhip)
1106 {
1107 	boolean_t	is_ethernet = (dlp->dl_mip->mi_media == DL_ETHER);
1108 	int		err = 0;
1109 
1110 	/*
1111 	 * Packets should always be at least 16 bit aligned.
1112 	 */
1113 	ASSERT(IS_P2ALIGNED(mp->b_rptr, sizeof (uint16_t)));
1114 
1115 	if ((err = mac_header_info(dlp->dl_mh, mp, mhip)) != 0)
1116 		return (err);
1117 
1118 	/*
1119 	 * If this is a VLAN-tagged Ethernet packet, then the SAP in the
1120 	 * mac_header_info_t as returned by mac_header_info() is
1121 	 * ETHERTYPE_VLAN. We need to grab the ethertype from the VLAN header.
1122 	 */
1123 	if (is_ethernet && (mhip->mhi_bindsap == ETHERTYPE_VLAN)) {
1124 		struct ether_vlan_header *evhp;
1125 		uint16_t sap;
1126 		mblk_t *tmp = NULL;
1127 		size_t size;
1128 
1129 		size = sizeof (struct ether_vlan_header);
1130 		if (MBLKL(mp) < size) {
1131 			/*
1132 			 * Pullup the message in order to get the MAC header
1133 			 * infomation. Note that this is a read-only function,
1134 			 * we keep the input packet intact.
1135 			 */
1136 			if ((tmp = msgpullup(mp, size)) == NULL)
1137 				return (EINVAL);
1138 
1139 			mp = tmp;
1140 		}
1141 		evhp = (struct ether_vlan_header *)mp->b_rptr;
1142 		sap = ntohs(evhp->ether_type);
1143 		(void) mac_sap_verify(dlp->dl_mh, sap, &mhip->mhi_bindsap);
1144 		mhip->mhi_hdrsize = sizeof (struct ether_vlan_header);
1145 		mhip->mhi_tci = ntohs(evhp->ether_tci);
1146 		mhip->mhi_istagged = B_TRUE;
1147 		freemsg(tmp);
1148 
1149 		if (VLAN_CFI(mhip->mhi_tci) != ETHER_CFI)
1150 			return (EINVAL);
1151 	} else {
1152 		mhip->mhi_istagged = B_FALSE;
1153 		mhip->mhi_tci = 0;
1154 	}
1155 	return (0);
1156 }
1157