10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
51502Sericheng * Common Development and Distribution License (the "License").
61502Sericheng * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
228833SVenu.Iyer@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
230Sstevel@tonic-gate * Use is subject to license terms.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate /*
270Sstevel@tonic-gate * Data-Link Services Module
280Sstevel@tonic-gate */
290Sstevel@tonic-gate
308275SEric Cheng #include <sys/sysmacros.h>
318275SEric Cheng #include <sys/strsubr.h>
320Sstevel@tonic-gate #include <sys/strsun.h>
338275SEric Cheng #include <sys/vlan.h>
348275SEric Cheng #include <sys/dld_impl.h>
358275SEric Cheng #include <sys/sdt.h>
360Sstevel@tonic-gate #include <sys/atomic.h>
370Sstevel@tonic-gate
380Sstevel@tonic-gate static kmem_cache_t *i_dls_link_cachep;
398833SVenu.Iyer@Sun.COM mod_hash_t *i_dls_link_hash;
40269Sericheng static uint_t i_dls_link_count;
410Sstevel@tonic-gate
420Sstevel@tonic-gate #define LINK_HASHSZ 67 /* prime */
430Sstevel@tonic-gate #define IMPL_HASHSZ 67 /* prime */
440Sstevel@tonic-gate
450Sstevel@tonic-gate /*
460Sstevel@tonic-gate * Construct a hash key encompassing both DLSAP value and VLAN idenitifier.
470Sstevel@tonic-gate */
488275SEric Cheng #define MAKE_KEY(_sap) \
498275SEric Cheng ((mod_hash_key_t)(uintptr_t)((_sap) << VLAN_ID_SIZE))
500Sstevel@tonic-gate
512311Sseb #define DLS_STRIP_PADDING(pktsize, p) { \
522311Sseb if (pktsize != 0) { \
532311Sseb ssize_t delta = pktsize - msgdsize(p); \
542311Sseb \
552311Sseb if (delta < 0) \
562311Sseb (void) adjmsg(p, delta); \
572311Sseb } \
582311Sseb }
592311Sseb
600Sstevel@tonic-gate /*
610Sstevel@tonic-gate * Private functions.
620Sstevel@tonic-gate */
630Sstevel@tonic-gate
640Sstevel@tonic-gate /*ARGSUSED*/
650Sstevel@tonic-gate static int
i_dls_link_constructor(void * buf,void * arg,int kmflag)660Sstevel@tonic-gate i_dls_link_constructor(void *buf, void *arg, int kmflag)
670Sstevel@tonic-gate {
680Sstevel@tonic-gate dls_link_t *dlp = buf;
690Sstevel@tonic-gate char name[MAXNAMELEN];
700Sstevel@tonic-gate
710Sstevel@tonic-gate bzero(buf, sizeof (dls_link_t));
720Sstevel@tonic-gate
735895Syz147064 (void) snprintf(name, MAXNAMELEN, "dls_link_t_%p_hash", buf);
748275SEric Cheng dlp->dl_str_hash = mod_hash_create_idhash(name, IMPL_HASHSZ,
75269Sericheng mod_hash_null_valdtor);
760Sstevel@tonic-gate
770Sstevel@tonic-gate return (0);
780Sstevel@tonic-gate }
790Sstevel@tonic-gate
800Sstevel@tonic-gate /*ARGSUSED*/
810Sstevel@tonic-gate static void
i_dls_link_destructor(void * buf,void * arg)820Sstevel@tonic-gate i_dls_link_destructor(void *buf, void *arg)
830Sstevel@tonic-gate {
840Sstevel@tonic-gate dls_link_t *dlp = buf;
850Sstevel@tonic-gate
860Sstevel@tonic-gate ASSERT(dlp->dl_ref == 0);
870Sstevel@tonic-gate ASSERT(dlp->dl_mh == NULL);
888275SEric Cheng ASSERT(dlp->dl_mah == NULL);
890Sstevel@tonic-gate ASSERT(dlp->dl_unknowns == 0);
900Sstevel@tonic-gate
918275SEric Cheng mod_hash_destroy_idhash(dlp->dl_str_hash);
928275SEric Cheng dlp->dl_str_hash = NULL;
930Sstevel@tonic-gate
940Sstevel@tonic-gate }
950Sstevel@tonic-gate
962311Sseb /*
972760Sdg199075 * - Parse the mac header information of the given packet.
982760Sdg199075 * - Strip the padding and skip over the header. Note that because some
992760Sdg199075 * DLS consumers only check the db_ref count of the first mblk, we
1003037Syz147064 * pullup the message into a single mblk. Because the original message
10110734SEric Cheng * is freed as the result of message pulling up, mac_vlan_header_info()
1023037Syz147064 * is called again to update the mhi_saddr and mhi_daddr pointers in the
10310734SEric Cheng * mhip. Further, the mac_vlan_header_info() function ensures that the
1043037Syz147064 * size of the pulled message is greater than the MAC header size,
1053037Syz147064 * therefore we can directly advance b_rptr to point at the payload.
1062760Sdg199075 *
1072760Sdg199075 * We choose to use a macro for performance reasons.
1082760Sdg199075 */
10910734SEric Cheng #define DLS_PREPARE_PKT(mh, mp, mhip, err) { \
1102760Sdg199075 mblk_t *nextp = (mp)->b_next; \
11110734SEric Cheng if (((err) = mac_vlan_header_info((mh), (mp), (mhip))) == 0) { \
1122760Sdg199075 DLS_STRIP_PADDING((mhip)->mhi_pktsize, (mp)); \
1132760Sdg199075 if (MBLKL((mp)) < (mhip)->mhi_hdrsize) { \
1142760Sdg199075 mblk_t *newmp; \
1152760Sdg199075 if ((newmp = msgpullup((mp), -1)) == NULL) { \
1162760Sdg199075 (err) = EINVAL; \
1172760Sdg199075 } else { \
1183037Syz147064 (mp)->b_next = NULL; \
1192760Sdg199075 freemsg((mp)); \
1202760Sdg199075 (mp) = newmp; \
12110734SEric Cheng VERIFY(mac_vlan_header_info((mh), \
1223037Syz147064 (mp), (mhip)) == 0); \
1232760Sdg199075 (mp)->b_next = nextp; \
1242760Sdg199075 (mp)->b_rptr += (mhip)->mhi_hdrsize; \
1252760Sdg199075 } \
1262760Sdg199075 } else { \
1272760Sdg199075 (mp)->b_rptr += (mhip)->mhi_hdrsize; \
1282760Sdg199075 } \
1292760Sdg199075 } \
1302760Sdg199075 }
1312760Sdg199075
1322760Sdg199075 /*
1332311Sseb * Truncate the chain starting at mp such that all packets in the chain
1342760Sdg199075 * have identical source and destination addresses, saps, and tag types
1352760Sdg199075 * (see below). It returns a pointer to the mblk following the chain,
1362760Sdg199075 * NULL if there is no further packet following the processed chain.
1372760Sdg199075 * The countp argument is set to the number of valid packets in the chain.
1382760Sdg199075 * Note that the whole MAC header (including the VLAN tag if any) in each
1392760Sdg199075 * packet will be stripped.
1402311Sseb */
1410Sstevel@tonic-gate static mblk_t *
i_dls_link_subchain(dls_link_t * dlp,mblk_t * mp,const mac_header_info_t * mhip,uint_t * countp)1422760Sdg199075 i_dls_link_subchain(dls_link_t *dlp, mblk_t *mp, const mac_header_info_t *mhip,
1432760Sdg199075 uint_t *countp)
1440Sstevel@tonic-gate {
1452760Sdg199075 mblk_t *prevp;
1462760Sdg199075 uint_t npacket = 1;
1472311Sseb size_t addr_size = dlp->dl_mip->mi_addr_length;
1482760Sdg199075 uint16_t vid = VLAN_ID(mhip->mhi_tci);
1492760Sdg199075 uint16_t pri = VLAN_PRI(mhip->mhi_tci);
1500Sstevel@tonic-gate
1510Sstevel@tonic-gate /*
1520Sstevel@tonic-gate * Compare with subsequent headers until we find one that has
1531502Sericheng * differing header information. After checking each packet
1541502Sericheng * strip padding and skip over the header.
1550Sstevel@tonic-gate */
1562760Sdg199075 for (prevp = mp; (mp = mp->b_next) != NULL; prevp = mp) {
1572311Sseb mac_header_info_t cmhi;
1582760Sdg199075 uint16_t cvid, cpri;
1592760Sdg199075 int err;
1602311Sseb
16110734SEric Cheng DLS_PREPARE_PKT(dlp->dl_mh, mp, &cmhi, err);
1622760Sdg199075 if (err != 0)
1630Sstevel@tonic-gate break;
1642311Sseb
1652760Sdg199075 prevp->b_next = mp;
1662760Sdg199075
1672311Sseb /*
16810700SEric Cheng * The source, destination, sap, vlan tag must all match in
16910700SEric Cheng * a given subchain.
1702311Sseb */
17110700SEric Cheng if (mhip->mhi_saddr == NULL || cmhi.mhi_saddr == NULL ||
17210700SEric Cheng memcmp(mhip->mhi_daddr, cmhi.mhi_daddr, addr_size) != 0 ||
1732311Sseb memcmp(mhip->mhi_saddr, cmhi.mhi_saddr, addr_size) != 0 ||
1748275SEric Cheng mhip->mhi_bindsap != cmhi.mhi_bindsap) {
1752760Sdg199075 /*
1762760Sdg199075 * Note that we don't need to restore the padding.
1772760Sdg199075 */
1782760Sdg199075 mp->b_rptr -= cmhi.mhi_hdrsize;
1792311Sseb break;
1802311Sseb }
1812311Sseb
1822760Sdg199075 cvid = VLAN_ID(cmhi.mhi_tci);
1832760Sdg199075 cpri = VLAN_PRI(cmhi.mhi_tci);
1842311Sseb
1852760Sdg199075 /*
1862760Sdg199075 * There are several types of packets. Packets don't match
1872760Sdg199075 * if they are classified to different type or if they are
1882760Sdg199075 * VLAN packets but belong to different VLANs:
1892760Sdg199075 *
1902760Sdg199075 * packet type tagged vid pri
1912760Sdg199075 * ---------------------------------------------------------
1922760Sdg199075 * untagged No zero zero
1932760Sdg199075 * VLAN packets Yes non-zero -
1942760Sdg199075 * priority tagged Yes zero non-zero
1952760Sdg199075 * 0 tagged Yes zero zero
1962760Sdg199075 */
1972760Sdg199075 if ((mhip->mhi_istagged != cmhi.mhi_istagged) ||
1982760Sdg199075 (vid != cvid) || ((vid == VLAN_ID_NONE) &&
1992760Sdg199075 (((pri == 0) && (cpri != 0)) ||
2002760Sdg199075 ((pri != 0) && (cpri == 0))))) {
2012760Sdg199075 mp->b_rptr -= cmhi.mhi_hdrsize;
2022760Sdg199075 break;
2032760Sdg199075 }
2042760Sdg199075
2050Sstevel@tonic-gate npacket++;
2060Sstevel@tonic-gate }
2070Sstevel@tonic-gate
2080Sstevel@tonic-gate /*
2090Sstevel@tonic-gate * Break the chain at this point and return a pointer to the next
2100Sstevel@tonic-gate * sub-chain.
2110Sstevel@tonic-gate */
2122760Sdg199075 prevp->b_next = NULL;
2130Sstevel@tonic-gate *countp = npacket;
2142760Sdg199075 return (mp);
2150Sstevel@tonic-gate }
2160Sstevel@tonic-gate
2178275SEric Cheng /* ARGSUSED */
2188275SEric Cheng static int
i_dls_head_hold(mod_hash_key_t key,mod_hash_val_t val)2198275SEric Cheng i_dls_head_hold(mod_hash_key_t key, mod_hash_val_t val)
220269Sericheng {
2218275SEric Cheng dls_head_t *dhp = (dls_head_t *)val;
2228275SEric Cheng
2238275SEric Cheng /*
2248275SEric Cheng * The lock order is mod_hash's internal lock -> dh_lock as in the
2258275SEric Cheng * call to i_dls_link_rx -> mod_hash_find_cb_rval -> i_dls_head_hold
2268275SEric Cheng */
2278275SEric Cheng mutex_enter(&dhp->dh_lock);
2288275SEric Cheng if (dhp->dh_removing) {
2298275SEric Cheng mutex_exit(&dhp->dh_lock);
2308275SEric Cheng return (-1);
2318275SEric Cheng }
2328275SEric Cheng dhp->dh_ref++;
2338275SEric Cheng mutex_exit(&dhp->dh_lock);
2348275SEric Cheng return (0);
235269Sericheng }
236269Sericheng
2378275SEric Cheng void
i_dls_head_rele(dls_head_t * dhp)238269Sericheng i_dls_head_rele(dls_head_t *dhp)
239269Sericheng {
2408275SEric Cheng mutex_enter(&dhp->dh_lock);
2418275SEric Cheng dhp->dh_ref--;
2428275SEric Cheng if (dhp->dh_ref == 0 && dhp->dh_removing != 0)
2438275SEric Cheng cv_broadcast(&dhp->dh_cv);
2448275SEric Cheng mutex_exit(&dhp->dh_lock);
245269Sericheng }
246269Sericheng
247269Sericheng static dls_head_t *
i_dls_head_alloc(mod_hash_key_t key)248269Sericheng i_dls_head_alloc(mod_hash_key_t key)
249269Sericheng {
250269Sericheng dls_head_t *dhp;
251269Sericheng
252269Sericheng dhp = kmem_zalloc(sizeof (dls_head_t), KM_SLEEP);
253269Sericheng dhp->dh_key = key;
254269Sericheng return (dhp);
255269Sericheng }
256269Sericheng
257269Sericheng static void
i_dls_head_free(dls_head_t * dhp)258269Sericheng i_dls_head_free(dls_head_t *dhp)
259269Sericheng {
260269Sericheng ASSERT(dhp->dh_ref == 0);
261269Sericheng kmem_free(dhp, sizeof (dls_head_t));
262269Sericheng }
263269Sericheng
2642760Sdg199075 /*
2652760Sdg199075 * Try to send mp up to the streams of the given sap and vid. Return B_TRUE
2662760Sdg199075 * if this message is sent to any streams.
2672760Sdg199075 * Note that this function will copy the message chain and the original
2682760Sdg199075 * mp will remain valid after this function
2692760Sdg199075 */
2702760Sdg199075 static uint_t
i_dls_link_rx_func(dls_link_t * dlp,mac_resource_handle_t mrh,mac_header_info_t * mhip,mblk_t * mp,uint32_t sap,boolean_t (* acceptfunc)())2712760Sdg199075 i_dls_link_rx_func(dls_link_t *dlp, mac_resource_handle_t mrh,
2728275SEric Cheng mac_header_info_t *mhip, mblk_t *mp, uint32_t sap,
2732760Sdg199075 boolean_t (*acceptfunc)())
2742760Sdg199075 {
2758275SEric Cheng mod_hash_t *hash = dlp->dl_str_hash;
2762760Sdg199075 mod_hash_key_t key;
2772760Sdg199075 dls_head_t *dhp;
2788275SEric Cheng dld_str_t *dsp;
2792760Sdg199075 mblk_t *nmp;
2808275SEric Cheng dls_rx_t ds_rx;
2818275SEric Cheng void *ds_rx_arg;
2822760Sdg199075 uint_t naccepted = 0;
2838275SEric Cheng int rval;
2842760Sdg199075
2852760Sdg199075 /*
2862760Sdg199075 * Construct a hash key from the VLAN identifier and the
2878275SEric Cheng * DLSAP that represents dld_str_t in promiscuous mode.
2882760Sdg199075 */
2898275SEric Cheng key = MAKE_KEY(sap);
2902760Sdg199075
2912760Sdg199075 /*
2928275SEric Cheng * Search the hash table for dld_str_t eligible to receive
2938275SEric Cheng * a packet chain for this DLSAP/VLAN combination. The mod hash's
2948275SEric Cheng * internal lock serializes find/insert/remove from the mod hash list.
2958275SEric Cheng * Incrementing the dh_ref (while holding the mod hash lock) ensures
2968275SEric Cheng * dls_link_remove will wait for the upcall to finish.
2972760Sdg199075 */
2988275SEric Cheng if (mod_hash_find_cb_rval(hash, key, (mod_hash_val_t *)&dhp,
2998275SEric Cheng i_dls_head_hold, &rval) != 0 || (rval != 0)) {
3002760Sdg199075 return (B_FALSE);
3012760Sdg199075 }
3022760Sdg199075
3032760Sdg199075 /*
3048275SEric Cheng * Find dld_str_t that will accept the sub-chain.
3052760Sdg199075 */
3068275SEric Cheng for (dsp = dhp->dh_list; dsp != NULL; dsp = dsp->ds_next) {
3078275SEric Cheng if (!acceptfunc(dsp, mhip, &ds_rx, &ds_rx_arg))
3082760Sdg199075 continue;
3092760Sdg199075
3102760Sdg199075 /*
3112760Sdg199075 * We have at least one acceptor.
3122760Sdg199075 */
3138275SEric Cheng naccepted++;
3142760Sdg199075
3152760Sdg199075 /*
3168275SEric Cheng * There will normally be at least more dld_str_t
3172760Sdg199075 * (since we've yet to check for non-promiscuous
3188275SEric Cheng * dld_str_t) so dup the sub-chain.
3192760Sdg199075 */
3202760Sdg199075 if ((nmp = copymsgchain(mp)) != NULL)
3218275SEric Cheng ds_rx(ds_rx_arg, mrh, nmp, mhip);
3222760Sdg199075 }
3232760Sdg199075
3242760Sdg199075 /*
3258275SEric Cheng * Release the hold on the dld_str_t chain now that we have
3262760Sdg199075 * finished walking it.
3272760Sdg199075 */
3282760Sdg199075 i_dls_head_rele(dhp);
3292760Sdg199075 return (naccepted);
3302760Sdg199075 }
3312760Sdg199075
3328275SEric Cheng /* ARGSUSED */
3338275SEric Cheng void
i_dls_link_rx(void * arg,mac_resource_handle_t mrh,mblk_t * mp,boolean_t loopback)3348275SEric Cheng i_dls_link_rx(void *arg, mac_resource_handle_t mrh, mblk_t *mp,
3358275SEric Cheng boolean_t loopback)
3360Sstevel@tonic-gate {
3370Sstevel@tonic-gate dls_link_t *dlp = arg;
3388275SEric Cheng mod_hash_t *hash = dlp->dl_str_hash;
3390Sstevel@tonic-gate mblk_t *nextp;
3402311Sseb mac_header_info_t mhi;
341269Sericheng dls_head_t *dhp;
3428275SEric Cheng dld_str_t *dsp;
3438275SEric Cheng dld_str_t *ndsp;
3440Sstevel@tonic-gate mblk_t *nmp;
345269Sericheng mod_hash_key_t key;
3460Sstevel@tonic-gate uint_t npacket;
3470Sstevel@tonic-gate boolean_t accepted;
3488275SEric Cheng dls_rx_t ds_rx, nds_rx;
3498275SEric Cheng void *ds_rx_arg, *nds_rx_arg;
3502760Sdg199075 uint16_t vid;
3518275SEric Cheng int err, rval;
3520Sstevel@tonic-gate
3530Sstevel@tonic-gate /*
3540Sstevel@tonic-gate * Walk the packet chain.
3550Sstevel@tonic-gate */
3562760Sdg199075 for (; mp != NULL; mp = nextp) {
3570Sstevel@tonic-gate /*
3580Sstevel@tonic-gate * Wipe the accepted state.
3590Sstevel@tonic-gate */
3600Sstevel@tonic-gate accepted = B_FALSE;
3610Sstevel@tonic-gate
36210734SEric Cheng DLS_PREPARE_PKT(dlp->dl_mh, mp, &mhi, err);
3632760Sdg199075 if (err != 0) {
3642760Sdg199075 atomic_add_32(&(dlp->dl_unknowns), 1);
3652760Sdg199075 nextp = mp->b_next;
3663037Syz147064 mp->b_next = NULL;
3672760Sdg199075 freemsg(mp);
3682760Sdg199075 continue;
3692760Sdg199075 }
3702760Sdg199075
3710Sstevel@tonic-gate /*
3720Sstevel@tonic-gate * Grab the longest sub-chain we can process as a single
3730Sstevel@tonic-gate * unit.
3740Sstevel@tonic-gate */
3752760Sdg199075 nextp = i_dls_link_subchain(dlp, mp, &mhi, &npacket);
3762760Sdg199075 ASSERT(npacket != 0);
3770Sstevel@tonic-gate
3782760Sdg199075 vid = VLAN_ID(mhi.mhi_tci);
3792760Sdg199075
3802760Sdg199075 if (mhi.mhi_istagged) {
3812311Sseb /*
3822760Sdg199075 * If it is tagged traffic, send it upstream to
3838275SEric Cheng * all dld_str_t which are attached to the physical
3842760Sdg199075 * link and bound to SAP 0x8100.
3852311Sseb */
3862760Sdg199075 if (i_dls_link_rx_func(dlp, mrh, &mhi, mp,
3878275SEric Cheng ETHERTYPE_VLAN, dls_accept) > 0) {
3882760Sdg199075 accepted = B_TRUE;
3892760Sdg199075 }
3902760Sdg199075
3912760Sdg199075 /*
3922760Sdg199075 * Don't pass the packets up if they are tagged
3932760Sdg199075 * packets and:
39410491SRishi.Srivatsavai@Sun.COM * - their VID and priority are both zero and the
39510491SRishi.Srivatsavai@Sun.COM * original packet isn't using the PVID (invalid
3962760Sdg199075 * packets).
3972760Sdg199075 * - their sap is ETHERTYPE_VLAN and their VID is
3982760Sdg199075 * zero as they have already been sent upstreams.
3992760Sdg199075 */
40010491SRishi.Srivatsavai@Sun.COM if ((vid == VLAN_ID_NONE && !mhi.mhi_ispvid &&
4012760Sdg199075 VLAN_PRI(mhi.mhi_tci) == 0) ||
4022760Sdg199075 (mhi.mhi_bindsap == ETHERTYPE_VLAN &&
4032760Sdg199075 vid == VLAN_ID_NONE)) {
4042760Sdg199075 freemsgchain(mp);
4052760Sdg199075 goto loop;
4062760Sdg199075 }
4072311Sseb }
4080Sstevel@tonic-gate
4090Sstevel@tonic-gate /*
4100Sstevel@tonic-gate * Construct a hash key from the VLAN identifier and the
4110Sstevel@tonic-gate * DLSAP.
4120Sstevel@tonic-gate */
4138275SEric Cheng key = MAKE_KEY(mhi.mhi_bindsap);
4140Sstevel@tonic-gate
4150Sstevel@tonic-gate /*
4168275SEric Cheng * Search the has table for dld_str_t eligible to receive
4170Sstevel@tonic-gate * a packet chain for this DLSAP/VLAN combination.
4180Sstevel@tonic-gate */
4198275SEric Cheng if (mod_hash_find_cb_rval(hash, key, (mod_hash_val_t *)&dhp,
4208275SEric Cheng i_dls_head_hold, &rval) != 0 || (rval != 0)) {
4210Sstevel@tonic-gate freemsgchain(mp);
4220Sstevel@tonic-gate goto loop;
4230Sstevel@tonic-gate }
4240Sstevel@tonic-gate
4250Sstevel@tonic-gate /*
4268275SEric Cheng * Find the first dld_str_t that will accept the sub-chain.
4270Sstevel@tonic-gate */
4288275SEric Cheng for (dsp = dhp->dh_list; dsp != NULL; dsp = dsp->ds_next)
4298275SEric Cheng if (dls_accept(dsp, &mhi, &ds_rx, &ds_rx_arg))
4300Sstevel@tonic-gate break;
4310Sstevel@tonic-gate
4320Sstevel@tonic-gate /*
4338275SEric Cheng * If we did not find any dld_str_t willing to accept the
4340Sstevel@tonic-gate * sub-chain then throw it away.
4350Sstevel@tonic-gate */
4368275SEric Cheng if (dsp == NULL) {
437269Sericheng i_dls_head_rele(dhp);
4380Sstevel@tonic-gate freemsgchain(mp);
4390Sstevel@tonic-gate goto loop;
4400Sstevel@tonic-gate }
4410Sstevel@tonic-gate
4420Sstevel@tonic-gate /*
4430Sstevel@tonic-gate * We have at least one acceptor.
4440Sstevel@tonic-gate */
4450Sstevel@tonic-gate accepted = B_TRUE;
4460Sstevel@tonic-gate for (;;) {
4470Sstevel@tonic-gate /*
4488275SEric Cheng * Find the next dld_str_t that will accept the
4490Sstevel@tonic-gate * sub-chain.
4500Sstevel@tonic-gate */
4518275SEric Cheng for (ndsp = dsp->ds_next; ndsp != NULL;
4528275SEric Cheng ndsp = ndsp->ds_next)
4538275SEric Cheng if (dls_accept(ndsp, &mhi, &nds_rx,
4548275SEric Cheng &nds_rx_arg))
4550Sstevel@tonic-gate break;
4560Sstevel@tonic-gate
4570Sstevel@tonic-gate /*
4588275SEric Cheng * If there are no more dld_str_t that are willing
4590Sstevel@tonic-gate * to accept the sub-chain then we don't need to dup
4600Sstevel@tonic-gate * it before handing it to the current one.
4610Sstevel@tonic-gate */
4628275SEric Cheng if (ndsp == NULL) {
4638275SEric Cheng ds_rx(ds_rx_arg, mrh, mp, &mhi);
4640Sstevel@tonic-gate
4650Sstevel@tonic-gate /*
4668275SEric Cheng * Since there are no more dld_str_t, we're
4670Sstevel@tonic-gate * done.
4680Sstevel@tonic-gate */
4690Sstevel@tonic-gate break;
4700Sstevel@tonic-gate }
4710Sstevel@tonic-gate
4720Sstevel@tonic-gate /*
4738275SEric Cheng * There are more dld_str_t so dup the sub-chain.
4740Sstevel@tonic-gate */
4750Sstevel@tonic-gate if ((nmp = copymsgchain(mp)) != NULL)
4768275SEric Cheng ds_rx(ds_rx_arg, mrh, nmp, &mhi);
4770Sstevel@tonic-gate
4788275SEric Cheng dsp = ndsp;
4798275SEric Cheng ds_rx = nds_rx;
4808275SEric Cheng ds_rx_arg = nds_rx_arg;
4810Sstevel@tonic-gate }
4820Sstevel@tonic-gate
4830Sstevel@tonic-gate /*
4848275SEric Cheng * Release the hold on the dld_str_t chain now that we have
4850Sstevel@tonic-gate * finished walking it.
4860Sstevel@tonic-gate */
487269Sericheng i_dls_head_rele(dhp);
4880Sstevel@tonic-gate
4890Sstevel@tonic-gate loop:
4900Sstevel@tonic-gate /*
4910Sstevel@tonic-gate * If there were no acceptors then add the packet count to the
4920Sstevel@tonic-gate * 'unknown' count.
4930Sstevel@tonic-gate */
4940Sstevel@tonic-gate if (!accepted)
4950Sstevel@tonic-gate atomic_add_32(&(dlp->dl_unknowns), npacket);
4960Sstevel@tonic-gate }
4970Sstevel@tonic-gate }
4980Sstevel@tonic-gate
4998275SEric Cheng /* ARGSUSED */
5008275SEric Cheng void
dls_rx_vlan_promisc(void * arg,mac_resource_handle_t mrh,mblk_t * mp,boolean_t loopback)5018275SEric Cheng dls_rx_vlan_promisc(void *arg, mac_resource_handle_t mrh, mblk_t *mp,
5028275SEric Cheng boolean_t loopback)
5032760Sdg199075 {
5048275SEric Cheng dld_str_t *dsp = arg;
5058275SEric Cheng dls_link_t *dlp = dsp->ds_dlp;
5068275SEric Cheng mac_header_info_t mhi;
5078275SEric Cheng dls_rx_t ds_rx;
5088275SEric Cheng void *ds_rx_arg;
5098275SEric Cheng int err;
5108275SEric Cheng
51110734SEric Cheng DLS_PREPARE_PKT(dlp->dl_mh, mp, &mhi, err);
5128275SEric Cheng if (err != 0)
5138275SEric Cheng goto drop;
5148275SEric Cheng
5158275SEric Cheng /*
5168275SEric Cheng * If there is promiscuous handle for vlan, we filter out the untagged
5178275SEric Cheng * pkts and pkts that are not for the primary unicast address.
5188275SEric Cheng */
5198275SEric Cheng if (dsp->ds_vlan_mph != NULL) {
5208275SEric Cheng uint8_t prim_addr[MAXMACADDRLEN];
5218275SEric Cheng size_t addr_length = dsp->ds_mip->mi_addr_length;
5222760Sdg199075
5238275SEric Cheng if (!(mhi.mhi_istagged))
5248275SEric Cheng goto drop;
5258275SEric Cheng ASSERT(dsp->ds_mh != NULL);
5268275SEric Cheng mac_unicast_primary_get(dsp->ds_mh, (uint8_t *)prim_addr);
5278275SEric Cheng if (memcmp(mhi.mhi_daddr, prim_addr, addr_length) != 0)
5288275SEric Cheng goto drop;
5298275SEric Cheng
5308275SEric Cheng if (!dls_accept(dsp, &mhi, &ds_rx, &ds_rx_arg))
5318275SEric Cheng goto drop;
5328275SEric Cheng
5338275SEric Cheng ds_rx(ds_rx_arg, NULL, mp, &mhi);
5348275SEric Cheng return;
5358275SEric Cheng }
5368275SEric Cheng
5378275SEric Cheng drop:
5388275SEric Cheng atomic_add_32(&dlp->dl_unknowns, 1);
5398275SEric Cheng freemsg(mp);
5408275SEric Cheng }
5412760Sdg199075
5428275SEric Cheng /* ARGSUSED */
5438275SEric Cheng void
dls_rx_promisc(void * arg,mac_resource_handle_t mrh,mblk_t * mp,boolean_t loopback)5448275SEric Cheng dls_rx_promisc(void *arg, mac_resource_handle_t mrh, mblk_t *mp,
5458275SEric Cheng boolean_t loopback)
5468275SEric Cheng {
5478275SEric Cheng dld_str_t *dsp = arg;
5488275SEric Cheng dls_link_t *dlp = dsp->ds_dlp;
5498275SEric Cheng mac_header_info_t mhi;
5508275SEric Cheng dls_rx_t ds_rx;
5518275SEric Cheng void *ds_rx_arg;
5528275SEric Cheng int err;
5538275SEric Cheng dls_head_t *dhp;
5548275SEric Cheng mod_hash_key_t key;
5558275SEric Cheng
55610734SEric Cheng DLS_PREPARE_PKT(dlp->dl_mh, mp, &mhi, err);
5578275SEric Cheng if (err != 0)
5588275SEric Cheng goto drop;
5598275SEric Cheng
5608275SEric Cheng /*
5618275SEric Cheng * In order to filter out sap pkt that no dls channel listens, search
5628275SEric Cheng * the hash table trying to find a dld_str_t eligible to receive the pkt
5638275SEric Cheng */
5648275SEric Cheng if ((dsp->ds_promisc & DLS_PROMISC_SAP) == 0) {
5658275SEric Cheng key = MAKE_KEY(mhi.mhi_bindsap);
5668275SEric Cheng if (mod_hash_find(dsp->ds_dlp->dl_str_hash, key,
5678275SEric Cheng (mod_hash_val_t *)&dhp) != 0)
5688275SEric Cheng goto drop;
5692760Sdg199075 }
5708275SEric Cheng
5718275SEric Cheng if (!dls_accept_promisc(dsp, &mhi, &ds_rx, &ds_rx_arg, loopback))
5728275SEric Cheng goto drop;
5738275SEric Cheng
5748275SEric Cheng ds_rx(ds_rx_arg, NULL, mp, &mhi);
5758275SEric Cheng return;
5768275SEric Cheng
5778275SEric Cheng drop:
5788275SEric Cheng atomic_add_32(&dlp->dl_unknowns, 1);
5798275SEric Cheng freemsg(mp);
5802760Sdg199075 }
5812760Sdg199075
5820Sstevel@tonic-gate static void
i_dls_link_destroy(dls_link_t * dlp)5838275SEric Cheng i_dls_link_destroy(dls_link_t *dlp)
5840Sstevel@tonic-gate {
5858275SEric Cheng ASSERT(dlp->dl_nactive == 0);
5868275SEric Cheng ASSERT(dlp->dl_impl_count == 0);
5878275SEric Cheng ASSERT(dlp->dl_zone_ref == 0);
5880Sstevel@tonic-gate
5890Sstevel@tonic-gate /*
5908275SEric Cheng * Free the structure back to the cache.
5910Sstevel@tonic-gate */
5928275SEric Cheng if (dlp->dl_mch != NULL)
5938275SEric Cheng mac_client_close(dlp->dl_mch, 0);
5940Sstevel@tonic-gate
5958275SEric Cheng if (dlp->dl_mh != NULL) {
5968275SEric Cheng ASSERT(MAC_PERIM_HELD(dlp->dl_mh));
5978275SEric Cheng mac_close(dlp->dl_mh);
5980Sstevel@tonic-gate }
5990Sstevel@tonic-gate
6008275SEric Cheng dlp->dl_mh = NULL;
6018275SEric Cheng dlp->dl_mch = NULL;
6028275SEric Cheng dlp->dl_mip = NULL;
6038275SEric Cheng dlp->dl_unknowns = 0;
604*11021SEric.Cheng@Sun.COM dlp->dl_nonip_cnt = 0;
6058275SEric Cheng kmem_cache_free(i_dls_link_cachep, dlp);
6060Sstevel@tonic-gate }
6070Sstevel@tonic-gate
6080Sstevel@tonic-gate static int
i_dls_link_create(const char * name,dls_link_t ** dlpp)6095733Syz147064 i_dls_link_create(const char *name, dls_link_t **dlpp)
6100Sstevel@tonic-gate {
6110Sstevel@tonic-gate dls_link_t *dlp;
6128275SEric Cheng int err;
6130Sstevel@tonic-gate
6140Sstevel@tonic-gate /*
6150Sstevel@tonic-gate * Allocate a new dls_link_t structure.
6160Sstevel@tonic-gate */
6170Sstevel@tonic-gate dlp = kmem_cache_alloc(i_dls_link_cachep, KM_SLEEP);
6180Sstevel@tonic-gate
6190Sstevel@tonic-gate /*
6200Sstevel@tonic-gate * Name the dls_link_t after the MAC interface it represents.
6210Sstevel@tonic-gate */
6222311Sseb (void) strlcpy(dlp->dl_name, name, sizeof (dlp->dl_name));
6230Sstevel@tonic-gate
6240Sstevel@tonic-gate /*
6258275SEric Cheng * First reference; hold open the MAC interface.
6260Sstevel@tonic-gate */
6278275SEric Cheng ASSERT(dlp->dl_mh == NULL);
6288275SEric Cheng err = mac_open(dlp->dl_name, &dlp->dl_mh);
6298275SEric Cheng if (err != 0)
6308275SEric Cheng goto bail;
6318275SEric Cheng
6328275SEric Cheng ASSERT(MAC_PERIM_HELD(dlp->dl_mh));
6338275SEric Cheng dlp->dl_mip = mac_info(dlp->dl_mh);
6348275SEric Cheng
6358275SEric Cheng /* DLS is the "primary" MAC client */
6368275SEric Cheng ASSERT(dlp->dl_mch == NULL);
6378275SEric Cheng
6388275SEric Cheng err = mac_client_open(dlp->dl_mh, &dlp->dl_mch, NULL,
6398275SEric Cheng MAC_OPEN_FLAGS_USE_DATALINK_NAME);
6408275SEric Cheng if (err != 0)
6418275SEric Cheng goto bail;
6428275SEric Cheng
6438275SEric Cheng DTRACE_PROBE2(dls__primary__client, char *, dlp->dl_name, void *,
6448275SEric Cheng dlp->dl_mch);
6450Sstevel@tonic-gate
6460Sstevel@tonic-gate *dlpp = dlp;
6470Sstevel@tonic-gate return (0);
6480Sstevel@tonic-gate
6498275SEric Cheng bail:
6508275SEric Cheng i_dls_link_destroy(dlp);
6518275SEric Cheng return (err);
6520Sstevel@tonic-gate }
6530Sstevel@tonic-gate
6540Sstevel@tonic-gate /*
6550Sstevel@tonic-gate * Module initialization functions.
6560Sstevel@tonic-gate */
6570Sstevel@tonic-gate
6580Sstevel@tonic-gate void
dls_link_init(void)6590Sstevel@tonic-gate dls_link_init(void)
6600Sstevel@tonic-gate {
6610Sstevel@tonic-gate /*
6620Sstevel@tonic-gate * Create a kmem_cache of dls_link_t structures.
6630Sstevel@tonic-gate */
6640Sstevel@tonic-gate i_dls_link_cachep = kmem_cache_create("dls_link_cache",
6650Sstevel@tonic-gate sizeof (dls_link_t), 0, i_dls_link_constructor,
6660Sstevel@tonic-gate i_dls_link_destructor, NULL, NULL, NULL, 0);
6670Sstevel@tonic-gate ASSERT(i_dls_link_cachep != NULL);
6680Sstevel@tonic-gate
6690Sstevel@tonic-gate /*
670269Sericheng * Create a dls_link_t hash table and associated lock.
6710Sstevel@tonic-gate */
672269Sericheng i_dls_link_hash = mod_hash_create_extended("dls_link_hash",
673269Sericheng IMPL_HASHSZ, mod_hash_null_keydtor, mod_hash_null_valdtor,
674269Sericheng mod_hash_bystr, NULL, mod_hash_strkey_cmp, KM_SLEEP);
675269Sericheng i_dls_link_count = 0;
6760Sstevel@tonic-gate }
6770Sstevel@tonic-gate
6780Sstevel@tonic-gate int
dls_link_fini(void)6790Sstevel@tonic-gate dls_link_fini(void)
6800Sstevel@tonic-gate {
681269Sericheng if (i_dls_link_count > 0)
682269Sericheng return (EBUSY);
6830Sstevel@tonic-gate
6840Sstevel@tonic-gate /*
6850Sstevel@tonic-gate * Destroy the kmem_cache.
6860Sstevel@tonic-gate */
6870Sstevel@tonic-gate kmem_cache_destroy(i_dls_link_cachep);
688269Sericheng
689269Sericheng /*
690269Sericheng * Destroy the hash table and associated lock.
691269Sericheng */
692269Sericheng mod_hash_destroy_hash(i_dls_link_hash);
6930Sstevel@tonic-gate return (0);
6940Sstevel@tonic-gate }
6950Sstevel@tonic-gate
6960Sstevel@tonic-gate /*
6970Sstevel@tonic-gate * Exported functions.
6980Sstevel@tonic-gate */
6990Sstevel@tonic-gate
7008275SEric Cheng static int
dls_link_hold_common(const char * name,dls_link_t ** dlpp,boolean_t create)7018275SEric Cheng dls_link_hold_common(const char *name, dls_link_t **dlpp, boolean_t create)
7020Sstevel@tonic-gate {
7030Sstevel@tonic-gate dls_link_t *dlp;
7040Sstevel@tonic-gate int err;
7050Sstevel@tonic-gate
7060Sstevel@tonic-gate /*
7078275SEric Cheng * Look up a dls_link_t corresponding to the given macname in the
7088275SEric Cheng * global hash table. The i_dls_link_hash itself is protected by the
7098275SEric Cheng * mod_hash package's internal lock which synchronizes
7108275SEric Cheng * find/insert/remove into the global mod_hash list. Assumes that
7118275SEric Cheng * inserts and removes are single threaded on a per mac end point
7128275SEric Cheng * by the mac perimeter.
7130Sstevel@tonic-gate */
714269Sericheng if ((err = mod_hash_find(i_dls_link_hash, (mod_hash_key_t)name,
715269Sericheng (mod_hash_val_t *)&dlp)) == 0)
7160Sstevel@tonic-gate goto done;
717269Sericheng
718269Sericheng ASSERT(err == MH_ERR_NOTFOUND);
7198275SEric Cheng if (!create)
7208275SEric Cheng return (ENOENT);
7210Sstevel@tonic-gate
7220Sstevel@tonic-gate /*
7230Sstevel@tonic-gate * We didn't find anything so we need to create one.
7240Sstevel@tonic-gate */
7258275SEric Cheng if ((err = i_dls_link_create(name, &dlp)) != 0)
7260Sstevel@tonic-gate return (err);
7270Sstevel@tonic-gate
7280Sstevel@tonic-gate /*
729269Sericheng * Insert the dls_link_t.
7300Sstevel@tonic-gate */
7315895Syz147064 err = mod_hash_insert(i_dls_link_hash, (mod_hash_key_t)dlp->dl_name,
732269Sericheng (mod_hash_val_t)dlp);
7330Sstevel@tonic-gate ASSERT(err == 0);
7340Sstevel@tonic-gate
7358275SEric Cheng atomic_add_32(&i_dls_link_count, 1);
736269Sericheng ASSERT(i_dls_link_count != 0);
737269Sericheng
7380Sstevel@tonic-gate done:
7398275SEric Cheng ASSERT(MAC_PERIM_HELD(dlp->dl_mh));
7400Sstevel@tonic-gate /*
7410Sstevel@tonic-gate * Bump the reference count and hand back the reference.
7420Sstevel@tonic-gate */
7430Sstevel@tonic-gate dlp->dl_ref++;
7440Sstevel@tonic-gate *dlpp = dlp;
745269Sericheng return (0);
7460Sstevel@tonic-gate }
7470Sstevel@tonic-gate
7488275SEric Cheng int
dls_link_hold_create(const char * name,dls_link_t ** dlpp)7498275SEric Cheng dls_link_hold_create(const char *name, dls_link_t **dlpp)
7508275SEric Cheng {
7518275SEric Cheng return (dls_link_hold_common(name, dlpp, B_TRUE));
7528275SEric Cheng }
7538275SEric Cheng
7548275SEric Cheng int
dls_link_hold(const char * name,dls_link_t ** dlpp)7558275SEric Cheng dls_link_hold(const char *name, dls_link_t **dlpp)
7568275SEric Cheng {
7578275SEric Cheng return (dls_link_hold_common(name, dlpp, B_FALSE));
7588275SEric Cheng }
7598275SEric Cheng
7608275SEric Cheng dev_info_t *
dls_link_devinfo(dev_t dev)7618275SEric Cheng dls_link_devinfo(dev_t dev)
7628275SEric Cheng {
7638275SEric Cheng dls_link_t *dlp;
7648275SEric Cheng dev_info_t *dip;
7658275SEric Cheng char macname[MAXNAMELEN];
7668275SEric Cheng char *drv;
7678275SEric Cheng mac_perim_handle_t mph;
7688275SEric Cheng
7698275SEric Cheng if ((drv = ddi_major_to_name(getmajor(dev))) == NULL)
7708275SEric Cheng return (NULL);
77110654SGarrett.Damore@Sun.COM (void) snprintf(macname, MAXNAMELEN, "%s%d", drv,
77210654SGarrett.Damore@Sun.COM DLS_MINOR2INST(getminor(dev)));
7738275SEric Cheng
7748275SEric Cheng /*
7758275SEric Cheng * The code below assumes that the name constructed above is the
7768275SEric Cheng * macname. This is not the case for legacy devices. Currently this
7778275SEric Cheng * is ok because this function is only called in the getinfo(9e) path,
7788275SEric Cheng * which for a legacy device would directly end up in the driver's
7798275SEric Cheng * getinfo, rather than here
7808275SEric Cheng */
7818275SEric Cheng if (mac_perim_enter_by_macname(macname, &mph) != 0)
7828275SEric Cheng return (NULL);
7838275SEric Cheng
7848275SEric Cheng if (dls_link_hold(macname, &dlp) != 0) {
7858275SEric Cheng mac_perim_exit(mph);
7868275SEric Cheng return (NULL);
7878275SEric Cheng }
7888275SEric Cheng
7898275SEric Cheng dip = mac_devinfo_get(dlp->dl_mh);
7908275SEric Cheng dls_link_rele(dlp);
7918275SEric Cheng mac_perim_exit(mph);
7928275SEric Cheng
7938275SEric Cheng return (dip);
7948275SEric Cheng }
7958275SEric Cheng
7968275SEric Cheng dev_t
dls_link_dev(dls_link_t * dlp)7978275SEric Cheng dls_link_dev(dls_link_t *dlp)
7988275SEric Cheng {
7998275SEric Cheng return (makedevice(ddi_driver_major(mac_devinfo_get(dlp->dl_mh)),
8008275SEric Cheng mac_minor(dlp->dl_mh)));
8018275SEric Cheng }
8028275SEric Cheng
8030Sstevel@tonic-gate void
dls_link_rele(dls_link_t * dlp)8040Sstevel@tonic-gate dls_link_rele(dls_link_t *dlp)
8050Sstevel@tonic-gate {
806269Sericheng mod_hash_val_t val;
8070Sstevel@tonic-gate
8088275SEric Cheng ASSERT(MAC_PERIM_HELD(dlp->dl_mh));
8090Sstevel@tonic-gate /*
8100Sstevel@tonic-gate * Check if there are any more references.
8110Sstevel@tonic-gate */
8128275SEric Cheng if (--dlp->dl_ref == 0) {
8138275SEric Cheng (void) mod_hash_remove(i_dls_link_hash,
8148275SEric Cheng (mod_hash_key_t)dlp->dl_name, &val);
8158275SEric Cheng ASSERT(dlp == (dls_link_t *)val);
8168275SEric Cheng
8170Sstevel@tonic-gate /*
8188275SEric Cheng * Destroy the dls_link_t.
8190Sstevel@tonic-gate */
8208275SEric Cheng i_dls_link_destroy(dlp);
8218275SEric Cheng ASSERT(i_dls_link_count > 0);
8228275SEric Cheng atomic_add_32(&i_dls_link_count, -1);
8238275SEric Cheng }
8248275SEric Cheng }
8258275SEric Cheng
8268275SEric Cheng int
dls_link_rele_by_name(const char * name)8278275SEric Cheng dls_link_rele_by_name(const char *name)
8288275SEric Cheng {
8298275SEric Cheng dls_link_t *dlp;
8308275SEric Cheng
8318275SEric Cheng if (mod_hash_find(i_dls_link_hash, (mod_hash_key_t)name,
8328275SEric Cheng (mod_hash_val_t *)&dlp) != 0)
8338275SEric Cheng return (ENOENT);
8348275SEric Cheng
8358275SEric Cheng ASSERT(MAC_PERIM_HELD(dlp->dl_mh));
8368275SEric Cheng
8378275SEric Cheng /*
8388275SEric Cheng * Must fail detach if mac client is busy.
8398275SEric Cheng */
8408275SEric Cheng ASSERT(dlp->dl_ref > 0 && dlp->dl_mch != NULL);
8418275SEric Cheng if (mac_link_has_flows(dlp->dl_mch))
8428275SEric Cheng return (ENOTEMPTY);
8438275SEric Cheng
8448275SEric Cheng dls_link_rele(dlp);
8458275SEric Cheng return (0);
8468275SEric Cheng }
8478275SEric Cheng
8488275SEric Cheng int
dls_link_setzid(const char * name,zoneid_t zid)8498275SEric Cheng dls_link_setzid(const char *name, zoneid_t zid)
8508275SEric Cheng {
8518275SEric Cheng dls_link_t *dlp;
8528275SEric Cheng int err = 0;
8538275SEric Cheng zoneid_t old_zid;
8548275SEric Cheng
8558275SEric Cheng if ((err = dls_link_hold_create(name, &dlp)) != 0)
8568275SEric Cheng return (err);
8578275SEric Cheng
8588275SEric Cheng ASSERT(MAC_PERIM_HELD(dlp->dl_mh));
8598275SEric Cheng
8608275SEric Cheng if ((old_zid = dlp->dl_zid) == zid)
8618275SEric Cheng goto done;
8628275SEric Cheng
8638275SEric Cheng /*
86410616SSebastien.Roy@Sun.COM * Check whether this dlp is used by its own zone. If yes, we cannot
86510616SSebastien.Roy@Sun.COM * change its zoneid.
8668275SEric Cheng */
8678275SEric Cheng if (dlp->dl_zone_ref != 0) {
8688275SEric Cheng err = EBUSY;
8690Sstevel@tonic-gate goto done;
8700Sstevel@tonic-gate }
8710Sstevel@tonic-gate
87210616SSebastien.Roy@Sun.COM dlp->dl_zid = zid;
87310616SSebastien.Roy@Sun.COM
8748275SEric Cheng if (zid == GLOBAL_ZONEID) {
8758275SEric Cheng /*
87610616SSebastien.Roy@Sun.COM * The link is moving from a non-global zone to the global
87710616SSebastien.Roy@Sun.COM * zone, so we need to release the reference that was held
87810616SSebastien.Roy@Sun.COM * when the link was originally assigned to the non-global
87910616SSebastien.Roy@Sun.COM * zone.
8808275SEric Cheng */
8818275SEric Cheng dls_link_rele(dlp);
8820Sstevel@tonic-gate }
8830Sstevel@tonic-gate
8848275SEric Cheng done:
88510616SSebastien.Roy@Sun.COM /*
88610616SSebastien.Roy@Sun.COM * We only keep the reference to this link open if the link has
88710616SSebastien.Roy@Sun.COM * successfully moved from the global zone to a non-global zone.
88810616SSebastien.Roy@Sun.COM */
88910616SSebastien.Roy@Sun.COM if (err != 0 || old_zid != GLOBAL_ZONEID)
89010616SSebastien.Roy@Sun.COM dls_link_rele(dlp);
8910Sstevel@tonic-gate return (err);
8920Sstevel@tonic-gate }
8930Sstevel@tonic-gate
89410639SDarren.Reed@Sun.COM int
dls_link_getzid(const char * name,zoneid_t * zidp)89510639SDarren.Reed@Sun.COM dls_link_getzid(const char *name, zoneid_t *zidp)
89610639SDarren.Reed@Sun.COM {
89710639SDarren.Reed@Sun.COM dls_link_t *dlp;
89810639SDarren.Reed@Sun.COM int err = 0;
89910639SDarren.Reed@Sun.COM
90010639SDarren.Reed@Sun.COM if ((err = dls_link_hold(name, &dlp)) != 0)
90110639SDarren.Reed@Sun.COM return (err);
90210639SDarren.Reed@Sun.COM
90310639SDarren.Reed@Sun.COM ASSERT(MAC_PERIM_HELD(dlp->dl_mh));
90410639SDarren.Reed@Sun.COM
90510639SDarren.Reed@Sun.COM *zidp = dlp->dl_zid;
90610639SDarren.Reed@Sun.COM
90710639SDarren.Reed@Sun.COM dls_link_rele(dlp);
90810639SDarren.Reed@Sun.COM return (0);
90910639SDarren.Reed@Sun.COM }
91010639SDarren.Reed@Sun.COM
9110Sstevel@tonic-gate void
dls_link_add(dls_link_t * dlp,uint32_t sap,dld_str_t * dsp)9128275SEric Cheng dls_link_add(dls_link_t *dlp, uint32_t sap, dld_str_t *dsp)
9130Sstevel@tonic-gate {
9148275SEric Cheng mod_hash_t *hash = dlp->dl_str_hash;
915269Sericheng mod_hash_key_t key;
916269Sericheng dls_head_t *dhp;
9178275SEric Cheng dld_str_t *p;
9180Sstevel@tonic-gate int err;
9198275SEric Cheng
9208275SEric Cheng ASSERT(MAC_PERIM_HELD(dlp->dl_mh));
9210Sstevel@tonic-gate
9220Sstevel@tonic-gate /*
9238275SEric Cheng * Generate a hash key based on the sap.
9240Sstevel@tonic-gate */
9258275SEric Cheng key = MAKE_KEY(sap);
9260Sstevel@tonic-gate
9270Sstevel@tonic-gate /*
928269Sericheng * Search the table for a list head with this key.
9290Sstevel@tonic-gate */
930269Sericheng if ((err = mod_hash_find(hash, key, (mod_hash_val_t *)&dhp)) != 0) {
931269Sericheng ASSERT(err == MH_ERR_NOTFOUND);
9320Sstevel@tonic-gate
933269Sericheng dhp = i_dls_head_alloc(key);
934269Sericheng err = mod_hash_insert(hash, key, (mod_hash_val_t)dhp);
935269Sericheng ASSERT(err == 0);
9360Sstevel@tonic-gate }
9370Sstevel@tonic-gate
9380Sstevel@tonic-gate /*
9398275SEric Cheng * Add the dld_str_t to the head of the list. List walkers in
9408275SEric Cheng * i_dls_link_rx_* bump up dh_ref to ensure the list does not change
9418275SEric Cheng * while they walk the list. The membar below ensures that list walkers
9428275SEric Cheng * see exactly the old list or the new list.
943269Sericheng */
9448275SEric Cheng ASSERT(dsp->ds_next == NULL);
945269Sericheng p = dhp->dh_list;
9468275SEric Cheng dsp->ds_next = p;
9478275SEric Cheng
9488275SEric Cheng membar_producer();
9498275SEric Cheng
9508275SEric Cheng dhp->dh_list = dsp;
951269Sericheng
952269Sericheng /*
953269Sericheng * Save a pointer to the list head.
954269Sericheng */
9558275SEric Cheng dsp->ds_head = dhp;
956269Sericheng dlp->dl_impl_count++;
957269Sericheng }
958269Sericheng
959269Sericheng void
dls_link_remove(dls_link_t * dlp,dld_str_t * dsp)9608275SEric Cheng dls_link_remove(dls_link_t *dlp, dld_str_t *dsp)
961269Sericheng {
9628275SEric Cheng mod_hash_t *hash = dlp->dl_str_hash;
9638275SEric Cheng dld_str_t **pp;
9648275SEric Cheng dld_str_t *p;
965269Sericheng dls_head_t *dhp;
9660Sstevel@tonic-gate
9678275SEric Cheng ASSERT(MAC_PERIM_HELD(dlp->dl_mh));
9680Sstevel@tonic-gate
969269Sericheng /*
9708275SEric Cheng * We set dh_removing here to tell the receive callbacks not to pass
9718275SEric Cheng * up packets anymore. Then wait till the current callbacks are done.
9728275SEric Cheng * This happens either in the close path or in processing the
9738275SEric Cheng * DL_UNBIND_REQ via a taskq thread, and it is ok to cv_wait in either.
9748275SEric Cheng * The dh_ref ensures there aren't and there won't be any upcalls
9758275SEric Cheng * walking or using the dh_list. The mod hash internal lock ensures
9768275SEric Cheng * that the insert/remove of the dls_head_t itself synchronizes with
9778275SEric Cheng * any i_dls_link_rx trying to locate it. The perimeter ensures that
9788275SEric Cheng * there isn't another simultaneous dls_link_add/remove.
979269Sericheng */
9808275SEric Cheng dhp = dsp->ds_head;
9818275SEric Cheng
9828275SEric Cheng mutex_enter(&dhp->dh_lock);
9838275SEric Cheng dhp->dh_removing = B_TRUE;
9848275SEric Cheng while (dhp->dh_ref != 0)
9858275SEric Cheng cv_wait(&dhp->dh_cv, &dhp->dh_lock);
9868275SEric Cheng mutex_exit(&dhp->dh_lock);
9870Sstevel@tonic-gate
9880Sstevel@tonic-gate /*
9898275SEric Cheng * Walk the list and remove the dld_str_t.
9900Sstevel@tonic-gate */
9918275SEric Cheng for (pp = &dhp->dh_list; (p = *pp) != NULL; pp = &(p->ds_next)) {
9928275SEric Cheng if (p == dsp)
993269Sericheng break;
994269Sericheng }
995269Sericheng ASSERT(p != NULL);
9968275SEric Cheng *pp = p->ds_next;
9978275SEric Cheng p->ds_next = NULL;
9988275SEric Cheng p->ds_head = NULL;
999269Sericheng
10008275SEric Cheng ASSERT(dlp->dl_impl_count != 0);
1001269Sericheng dlp->dl_impl_count--;
10020Sstevel@tonic-gate
1003269Sericheng if (dhp->dh_list == NULL) {
1004269Sericheng mod_hash_val_t val = NULL;
1005269Sericheng
1006269Sericheng /*
1007269Sericheng * The list is empty so remove the hash table entry.
1008269Sericheng */
1009269Sericheng (void) mod_hash_remove(hash, dhp->dh_key, &val);
1010269Sericheng ASSERT(dhp == (dls_head_t *)val);
1011269Sericheng i_dls_head_free(dhp);
10128275SEric Cheng } else {
10138275SEric Cheng mutex_enter(&dhp->dh_lock);
10148275SEric Cheng dhp->dh_removing = B_FALSE;
10158275SEric Cheng mutex_exit(&dhp->dh_lock);
1016269Sericheng }
10170Sstevel@tonic-gate }
1018