dls_link.c revision 6f45d2ae10396ef73a04ceaaa040cb4e15615be3
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* Data-Link Services Module
*/
#include <sys/sysmacros.h>
#include <sys/ethernet.h>
#include <sys/byteorder.h>
#include <sys/dld_impl.h>
#include <sys/dls_impl.h>
static kmem_cache_t *i_dls_link_cachep;
static mod_hash_t *i_dls_link_hash;
static uint_t i_dls_link_count;
static krwlock_t i_dls_link_lock;
/*
* Construct a hash key encompassing both DLSAP value and VLAN idenitifier.
*/
((mod_hash_key_t)(uintptr_t) \
/*
* Extract the DLSAP value from the hash key.
*/
#define DLS_STRIP_PADDING(pktsize, p) { \
if (pktsize != 0) { \
\
if (delta < 0) \
} \
}
/*
* Private functions.
*/
/*ARGSUSED*/
static int
{
char name[MAXNAMELEN];
return (0);
}
/*ARGSUSED*/
static void
{
}
/*
* - Parse the mac header information of the given packet.
* - Strip the padding and skip over the header. Note that because some
* DLS consumers only check the db_ref count of the first mblk, we
* pullup the message into a single mblk. Because the original message
* is freed as the result of message pulling up, dls_link_header_info()
* is called again to update the mhi_saddr and mhi_daddr pointers in the
* mhip. Further, the dls_link_header_info() function ensures that the
* size of the pulled message is greater than the MAC header size,
* therefore we can directly advance b_rptr to point at the payload.
*
* We choose to use a macro for performance reasons.
*/
} else { \
} \
} else { \
} \
} \
}
/*
* Truncate the chain starting at mp such that all packets in the chain
* have identical source and destination addresses, saps, and tag types
* (see below). It returns a pointer to the mblk following the chain,
* NULL if there is no further packet following the processed chain.
* The countp argument is set to the number of valid packets in the chain.
* Note that the whole MAC header (including the VLAN tag if any) in each
* packet will be stripped.
*/
static mblk_t *
{
/*
* Compare with subsequent headers until we find one that has
* differing header information. After checking each packet
* strip padding and skip over the header.
*/
int err;
if (err != 0)
break;
/*
* The source, destination, sap, and vlan id must all match
* in a given subchain.
*/
/*
* Note that we don't need to restore the padding.
*/
break;
}
/*
* There are several types of packets. Packets don't match
* if they are classified to different type or if they are
* VLAN packets but belong to different VLANs:
*
* packet type tagged vid pri
* ---------------------------------------------------------
* untagged No zero zero
* VLAN packets Yes non-zero -
* priority tagged Yes zero non-zero
* 0 tagged Yes zero zero
*/
break;
}
npacket++;
}
/*
* Break the chain at this point and return a pointer to the next
* sub-chain.
*/
return (mp);
}
static void
{
}
static void
{
}
static dls_head_t *
{
return (dhp);
}
static void
{
}
/*
* Try to send mp up to the streams of the given sap and vid. Return B_TRUE
* if this message is sent to any streams.
* Note that this function will copy the message chain and the original
* mp will remain valid after this function
*/
static uint_t
boolean_t (*acceptfunc)())
{
void *di_rx_arg;
/*
* Construct a hash key from the VLAN identifier and the
* DLSAP that represents dls_impl_t in promiscuous mode.
*/
/*
* Search the hash table for dls_impl_t eligible to receive
*/
return (B_FALSE);
}
/*
* Find dls_impl_t that will accept the sub-chain.
*/
continue;
/*
* We have at least one acceptor.
*/
naccepted ++;
/*
* There will normally be at least more dls_impl_t
* (since we've yet to check for non-promiscuous
* dls_impl_t) so dup the sub-chain.
*/
}
/*
* Release the hold on the dls_impl_t chain now that we have
* finished walking it.
*/
return (naccepted);
}
static void
{
void *di_rx_arg, *ndi_rx_arg;
int err;
/*
* Walk the packet chain.
*/
/*
* Wipe the accepted state.
*/
if (err != 0) {
continue;
}
/*
* Grab the longest sub-chain we can process as a single
* unit.
*/
if (mhi.mhi_istagged) {
/*
* If it is tagged traffic, send it upstream to
* all dls_impl_t which are attached to the physical
* link and bound to SAP 0x8100.
*/
}
/*
* Don't pass the packets up if they are tagged
* packets and:
* - their VID and priority are both zero (invalid
* packets).
* - their sap is ETHERTYPE_VLAN and their VID is
* zero as they have already been sent upstreams.
*/
if ((vid == VLAN_ID_NONE &&
vid == VLAN_ID_NONE)) {
goto loop;
}
}
/*
* Construct a hash key from the VLAN identifier and the
* DLSAP.
*/
/*
* Search the has table for dls_impl_t eligible to receive
*/
goto loop;
}
/*
* Find the first dls_impl_t that will accept the sub-chain.
*/
break;
/*
* If we did not find any dls_impl_t willing to accept the
* sub-chain then throw it away.
*/
goto loop;
}
/*
* We have at least one acceptor.
*/
for (;;) {
/*
* Find the next dls_impl_t that will accept the
* sub-chain.
*/
&ndi_rx_arg))
break;
/*
* If there are no more dls_impl_t that are willing
* to accept the sub-chain then we don't need to dup
* it before handing it to the current one.
*/
/*
* Since there are no more dls_impl_t, we're
* done.
*/
break;
}
/*
* There are more dls_impl_t so dup the sub-chain.
*/
}
/*
* Release the hold on the dls_impl_t chain now that we have
* finished walking it.
*/
loop:
/*
* If there were no acceptors then add the packet count to the
* 'unknown' count.
*/
if (!accepted)
}
}
/*
* Try to send mp up to the DLS_SAP_PROMISC listeners. Return B_TRUE if this
* message is sent to any streams.
*/
static uint_t
boolean_t (*acceptfunc)())
{
vid, acceptfunc);
if (vid != VLAN_ID_NONE) {
}
return (naccepted);
}
static void
boolean_t (*acceptfunc)())
{
void *di_rx_arg, *fdi_rx_arg;
int err;
/*
* Walk the packet chain.
*/
/*
* Wipe the accepted state and the receive information of
* the first eligible dls_impl_t.
*/
fdi_rx_arg = NULL;
if (err != 0) {
if (acceptfunc == dls_accept)
continue;
}
/*
* Grab the longest sub-chain we can process as a single
* unit.
*/
/*
* Note that we need to first send to the dls_impl_t
* in promiscuous mode in order to avoid the packet reordering
* when snooping.
*/
acceptfunc) > 0) {
}
/*
* Non promisc case. Two passes:
* 1. send tagged packets to ETHERTYPE_VLAN listeners
* 2. send packets to listeners bound to the specific SAP.
*/
if (mhi.mhi_istagged) {
} else {
goto non_promisc_loop;
}
/*
* Construct a hash key from the VLAN identifier and the
* DLSAP.
*/
/*
* Search the has table for dls_impl_t eligible to receive
*/
goto non_promisc_loop;
}
/*
* Find the first dls_impl_t that will accept the sub-chain.
*/
continue;
/*
* To avoid the extra copymsgchain(), if this
* is the first eligible dls_impl_t, remember required
* information and send up the message afterwards.
*/
continue;
}
}
/*
* Release the hold on the dls_impl_t chain now that we have
* finished walking it.
*/
/*
* Don't pass the packets up again if:
* - First pass is done and the packets are tagged and their:
* - VID and priority are both zero (invalid packets).
* - their sap is ETHERTYPE_VLAN and their VID is zero
* (they have already been sent upstreams).
* - Second pass is done:
*/
vid == VLAN_ID_NONE)))) {
/*
* Send the message up to the first eligible dls_impl_t.
*/
else
} else {
goto non_promisc;
}
/*
* If there were no acceptors then add the packet count to the
* 'unknown' count.
*/
}
}
static void
{
}
static void
{
}
/*ARGSUSED*/
static uint_t
{
if (sap == DLS_SAP_PROMISC) {
return (MH_WALK_TERMINATE);
}
return (MH_WALK_CONTINUE);
}
static int
{
/*
* Allocate a new dls_link_t structure.
*/
/*
* Name the dls_link_t after the MAC interface it represents.
*/
/*
* Set the packet loopback function for use when the MAC is in
* promiscuous mode, and initialize promiscuous bookeeping fields.
*/
dlp->dl_npromisc = 0;
return (0);
}
static void
{
/*
* Free the structure back to the cache.
*/
dlp->dl_unknowns = 0;
}
/*
* Module initialization functions.
*/
void
dls_link_init(void)
{
/*
* Create a kmem_cache of dls_link_t structures.
*/
sizeof (dls_link_t), 0, i_dls_link_constructor,
/*
* Create a dls_link_t hash table and associated lock.
*/
i_dls_link_count = 0;
}
int
dls_link_fini(void)
{
if (i_dls_link_count > 0)
return (EBUSY);
/*
* Destroy the kmem_cache.
*/
/*
* Destroy the hash table and associated lock.
*/
return (0);
}
/*
* Exported functions.
*/
int
{
int err;
/*
* Look up a dls_link_t corresponding to the given mac_handle_t
* in the global hash table. We need to hold i_dls_link_lock in
* order to atomically find and insert a dls_link_t into the
* hash table.
*/
(mod_hash_val_t *)&dlp)) == 0)
goto done;
/*
* We didn't find anything so we need to create one.
*/
return (err);
}
/*
* Insert the dls_link_t.
*/
ASSERT(i_dls_link_count != 0);
done:
/*
* Bump the reference count and hand back the reference.
*/
return (0);
}
void
{
/*
* Check if there are any more references.
*/
/*
* There are more references so there's nothing more to do.
*/
goto done;
}
(void) mod_hash_remove(i_dls_link_hash,
/*
* Destroy the dls_link_t.
*/
ASSERT(i_dls_link_count > 0);
done:
}
int
{
int err = 0;
/*
* First reference; hold open the MAC interface.
*/
if (err != 0)
goto done;
}
done:
return (err);
}
void
{
}
}
void
{
dls_impl_t *p;
int err;
/*
* Generate a hash key based on the sap and the VLAN id.
*/
/*
* We need dl_lock here because we want to be able to walk
* the hash table *and* set the mac rx func atomically. if
* these two operations are separate, someone else could
* drop the hash lock and this could cause our chosen rx
* func to be incorrect. note that we cannot call mac_rx_add
* when holding the hash lock because this can cause deadlock.
*/
/*
* Search the table for a list head with this key.
*/
}
/*
* Add the dls_impl_t to the head of the list.
*/
/*
* Save a pointer to the list head.
*/
dlp->dl_impl_count++;
/*
* Walk the bound dls_impl_t to see if there are any
* in promiscuous 'all sap' mode.
*/
/*
* If there are then we need to use a receive routine
* which will route packets to those dls_impl_t as well
* as ones bound to the DLSAP of the packet.
*/
if (promisc)
else
rx = i_dls_link_rx;
/* Replace the existing receive function if there is one. */
}
void
{
dls_impl_t **pp;
dls_impl_t *p;
/*
* We need dl_lock here because we want to be able to walk
* the hash table *and* set the mac rx func atomically. if
* these two operations are separate, someone else could
* drop the hash lock and this could cause our chosen rx
* func to be incorrect. note that we cannot call mac_rx_add
* when holding the hash lock because this can cause deadlock.
*/
/*
* Poll the hash table entry until all references have been dropped.
* We need to drop all locks before sleeping because we don't want
* the interrupt handler to block. We set di_removing here to
* tell the receive callbacks not to pass up packets anymore.
* This is only a hint to quicken the decrease of the refcnt so
* the assignment need not be protected by any lock.
*/
}
/*
* Walk the list and remove the dls_impl_t.
*/
if (p == dip)
break;
}
dlp->dl_impl_count--;
/*
* The list is empty so remove the hash table entry.
*/
}
/*
* If there are no dls_impl_t then there's no need to register a
* receive function with the mac.
*/
if (dlp->dl_impl_count == 0) {
} else {
/*
* Walk the bound dls_impl_t to see if there are any
* in promiscuous 'all sap' mode.
*/
/*
* If there are then we need to use a receive routine
* which will route packets to those dls_impl_t as well
* as ones bound to the DLSAP of the packet.
*/
if (promisc)
else
rx = i_dls_link_rx;
}
}
int
{
int err = 0;
/*
* Packets should always be at least 16 bit aligned.
*/
return (err);
/*
* If this is a VLAN-tagged Ethernet packet, then the SAP in the
* mac_header_info_t as returned by mac_header_info() is
* ETHERTYPE_VLAN. We need to grab the ethertype from the VLAN header.
*/
struct ether_vlan_header *evhp;
size = sizeof (struct ether_vlan_header);
/*
* Pullup the message in order to get the MAC header
* infomation. Note that this is a read-only function,
* we keep the input packet intact.
*/
return (EINVAL);
}
return (EINVAL);
} else {
}
return (0);
}