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