5 * Copyright (c) 2000 Whistle Communications, Inc.
8 * Subject to the following obligations and disclaimer of warranty, use and
9 * redistribution of this software, in source or object code forms, with or
10 * without modifications are expressly permitted by Whistle Communications;
11 * provided, however, that:
12 * 1. Any and all reproductions of the source or object code must include the
13 * copyright notice above and the following disclaimer of warranties; and
14 * 2. No rights are granted, in any manner or form, to use Whistle
15 * Communications, Inc. trademarks, including the mark "WHISTLE
16 * COMMUNICATIONS" on advertising, endorsements, or otherwise except as
17 * such appears in the above copyright notice or in the software.
19 * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
20 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
21 * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
22 * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
23 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
24 * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
25 * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
26 * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
27 * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
28 * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
29 * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
30 * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
31 * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34 * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
37 * Author: Archie Cobbs <archie@freebsd.org>
43 * ng_bridge(4) netgraph node type
45 * The node performs standard intelligent Ethernet bridging over
46 * each of its connected hooks, or links. A simple loop detection
47 * algorithm is included which disables a link for priv->conf.loopTimeout
48 * seconds when a host is seen to have jumped from one link to
49 * another within priv->conf.minStableAge seconds.
51 * We keep a hashtable that maps Ethernet addresses to host info,
52 * which is contained in struct ng_bridge_host's. These structures
53 * tell us on which link the host may be found. A host's entry will
54 * expire after priv->conf.maxStaleness seconds.
56 * This node is optimzed for stable networks, where machines jump
57 * from one port to the other only rarely.
60 #include <sys/param.h>
61 #include <sys/systm.h>
62 #include <sys/kernel.h>
63 #include <sys/malloc.h>
65 #include <sys/errno.h>
66 #include <sys/syslog.h>
67 #include <sys/socket.h>
68 #include <sys/ctype.h>
71 #include <net/ethernet.h>
73 #include <netinet/in.h>
74 #include <netinet/ip_fw.h>
76 #include <netgraph/ng_message.h>
77 #include <netgraph/netgraph.h>
78 #include <netgraph/ng_parse.h>
79 #include <netgraph/ng_bridge.h>
80 #include <netgraph/ng_ether.h>
82 #ifdef NG_SEPARATE_MALLOC
83 MALLOC_DEFINE(M_NETGRAPH_BRIDGE, "netgraph_bridge", "netgraph bridge node ");
85 #define M_NETGRAPH_BRIDGE M_NETGRAPH
88 /* Per-link private data */
89 struct ng_bridge_link {
90 hook_p hook; /* netgraph hook */
91 u_int16_t loopCount; /* loop ignore timer */
92 struct ng_bridge_link_stats stats; /* link stats */
95 /* Per-node private data */
96 struct ng_bridge_private {
97 struct ng_bridge_bucket *tab; /* hash table bucket array */
98 struct ng_bridge_link *links[NG_BRIDGE_MAX_LINKS];
99 struct ng_bridge_config conf; /* node configuration */
100 node_p node; /* netgraph node */
101 u_int numHosts; /* num entries in table */
102 u_int numBuckets; /* num buckets in table */
103 u_int hashMask; /* numBuckets - 1 */
104 int numLinks; /* num connected links */
105 struct callout timer; /* one second periodic timer */
107 typedef struct ng_bridge_private *priv_p;
109 /* Information about a host, stored in a hash table entry */
110 struct ng_bridge_hent {
111 struct ng_bridge_host host; /* actual host info */
112 SLIST_ENTRY(ng_bridge_hent) next; /* next entry in bucket */
115 /* Hash table bucket declaration */
116 SLIST_HEAD(ng_bridge_bucket, ng_bridge_hent);
118 /* Netgraph node methods */
119 static ng_constructor_t ng_bridge_constructor;
120 static ng_rcvmsg_t ng_bridge_rcvmsg;
121 static ng_shutdown_t ng_bridge_shutdown;
122 static ng_newhook_t ng_bridge_newhook;
123 static ng_rcvdata_t ng_bridge_rcvdata;
124 static ng_disconnect_t ng_bridge_disconnect;
126 /* Other internal functions */
127 static struct ng_bridge_host *ng_bridge_get(priv_p priv, const u_char *addr);
128 static int ng_bridge_put(priv_p priv, const u_char *addr, int linkNum);
129 static void ng_bridge_rehash(priv_p priv);
130 static void ng_bridge_remove_hosts(priv_p priv, int linkNum);
131 static void ng_bridge_timeout(void *arg);
132 static const char *ng_bridge_nodename(node_p node);
134 /* Ethernet broadcast */
135 static const u_char ng_bridge_bcast_addr[ETHER_ADDR_LEN] =
136 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
138 /* Store each hook's link number in the private field */
139 #define LINK_NUM(hook) (*(u_int16_t *)(&(hook)->private))
141 /* Compare Ethernet addresses using 32 and 16 bit words instead of bytewise */
142 #define ETHER_EQUAL(a,b) (((const u_int32_t *)(a))[0] \
143 == ((const u_int32_t *)(b))[0] \
144 && ((const u_int16_t *)(a))[2] \
145 == ((const u_int16_t *)(b))[2])
147 /* Minimum and maximum number of hash buckets. Must be a power of two. */
148 #define MIN_BUCKETS (1 << 5) /* 32 */
149 #define MAX_BUCKETS (1 << 14) /* 16384 */
151 /* Configuration default values */
152 #define DEFAULT_LOOP_TIMEOUT 60
153 #define DEFAULT_MAX_STALENESS (15 * 60) /* same as ARP timeout */
154 #define DEFAULT_MIN_STABLE_AGE 1
156 /******************************************************************
158 ******************************************************************/
161 * How to determine the length of the table returned by NGM_BRIDGE_GET_TABLE
164 ng_bridge_getTableLength(const struct ng_parse_type *type,
165 const u_char *start, const u_char *buf)
167 const struct ng_bridge_host_ary *const hary
168 = (const struct ng_bridge_host_ary *)(buf - sizeof(u_int32_t));
170 return hary->numHosts;
173 /* Parse type for struct ng_bridge_host_ary */
174 static const struct ng_parse_struct_info ng_bridge_host_type_info
175 = NG_BRIDGE_HOST_TYPE_INFO(&ng_ether_enaddr_type);
176 static const struct ng_parse_type ng_bridge_host_type = {
177 &ng_parse_struct_type,
178 &ng_bridge_host_type_info
180 static const struct ng_parse_array_info ng_bridge_hary_type_info = {
181 &ng_bridge_host_type,
182 ng_bridge_getTableLength
184 static const struct ng_parse_type ng_bridge_hary_type = {
185 &ng_parse_array_type,
186 &ng_bridge_hary_type_info
188 static const struct ng_parse_struct_info ng_bridge_host_ary_type_info
189 = NG_BRIDGE_HOST_ARY_TYPE_INFO(&ng_bridge_hary_type);
190 static const struct ng_parse_type ng_bridge_host_ary_type = {
191 &ng_parse_struct_type,
192 &ng_bridge_host_ary_type_info
195 /* Parse type for struct ng_bridge_config */
196 static const struct ng_parse_fixedarray_info ng_bridge_ipfwary_type_info = {
197 &ng_parse_uint8_type,
200 static const struct ng_parse_type ng_bridge_ipfwary_type = {
201 &ng_parse_fixedarray_type,
202 &ng_bridge_ipfwary_type_info
204 static const struct ng_parse_struct_info ng_bridge_config_type_info
205 = NG_BRIDGE_CONFIG_TYPE_INFO(&ng_bridge_ipfwary_type);
206 static const struct ng_parse_type ng_bridge_config_type = {
207 &ng_parse_struct_type,
208 &ng_bridge_config_type_info
211 /* Parse type for struct ng_bridge_link_stat */
212 static const struct ng_parse_struct_info
213 ng_bridge_stats_type_info = NG_BRIDGE_STATS_TYPE_INFO;
214 static const struct ng_parse_type ng_bridge_stats_type = {
215 &ng_parse_struct_type,
216 &ng_bridge_stats_type_info
219 /* List of commands and how to convert arguments to/from ASCII */
220 static const struct ng_cmdlist ng_bridge_cmdlist[] = {
223 NGM_BRIDGE_SET_CONFIG,
225 &ng_bridge_config_type,
230 NGM_BRIDGE_GET_CONFIG,
233 &ng_bridge_config_type
244 NGM_BRIDGE_GET_STATS,
246 &ng_parse_uint32_type,
247 &ng_bridge_stats_type
251 NGM_BRIDGE_CLR_STATS,
253 &ng_parse_uint32_type,
258 NGM_BRIDGE_GETCLR_STATS,
260 &ng_parse_uint32_type,
261 &ng_bridge_stats_type
265 NGM_BRIDGE_GET_TABLE,
268 &ng_bridge_host_ary_type
273 /* Node type descriptor */
274 static struct ng_type ng_bridge_typestruct = {
278 ng_bridge_constructor,
285 ng_bridge_disconnect,
288 NETGRAPH_INIT(bridge, &ng_bridge_typestruct);
290 /* Depend on ng_ether so we can use the Ethernet parse type */
291 MODULE_DEPEND(ng_bridge, ng_ether, 1, 1, 1);
293 /******************************************************************
294 NETGRAPH NODE METHODS
295 ******************************************************************/
301 ng_bridge_constructor(node_p node)
305 /* Allocate and initialize private info */
306 MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH_BRIDGE, M_NOWAIT | M_ZERO);
309 callout_init(&priv->timer, 0);
311 /* Allocate and initialize hash table, etc. */
312 MALLOC(priv->tab, struct ng_bridge_bucket *,
313 MIN_BUCKETS * sizeof(*priv->tab), M_NETGRAPH_BRIDGE, M_NOWAIT | M_ZERO);
314 if (priv->tab == NULL) {
315 FREE(priv, M_NETGRAPH_BRIDGE);
318 priv->numBuckets = MIN_BUCKETS;
319 priv->hashMask = MIN_BUCKETS - 1;
320 priv->conf.debugLevel = 1;
321 priv->conf.loopTimeout = DEFAULT_LOOP_TIMEOUT;
322 priv->conf.maxStaleness = DEFAULT_MAX_STALENESS;
323 priv->conf.minStableAge = DEFAULT_MIN_STABLE_AGE;
326 * This node has all kinds of stuff that could be screwed by SMP.
327 * Until it gets it's own internal protection, we go through in
328 * single file. This could hurt a machine bridging beteen two
329 * GB ethernets so it should be fixed.
330 * When it's fixed the process SHOULD NOT SLEEP, spinlocks please!
333 NG_NODE_FORCE_WRITER(node);
334 NG_NODE_SET_PRIVATE(node, priv);
337 /* Start timer; timer is always running while node is alive */
338 callout_reset(&priv->timer, hz, ng_bridge_timeout, priv->node);
345 * Method for attaching a new hook
348 ng_bridge_newhook(node_p node, hook_p hook, const char *name)
350 const priv_p priv = NG_NODE_PRIVATE(node);
352 /* Check for a link hook */
353 if (strncmp(name, NG_BRIDGE_HOOK_LINK_PREFIX,
354 strlen(NG_BRIDGE_HOOK_LINK_PREFIX)) == 0) {
359 cp = name + strlen(NG_BRIDGE_HOOK_LINK_PREFIX);
360 if (!isdigit(*cp) || (cp[0] == '0' && cp[1] != '\0'))
362 linkNum = strtoul(cp, &eptr, 10);
363 if (*eptr != '\0' || linkNum >= NG_BRIDGE_MAX_LINKS)
365 if (priv->links[linkNum] != NULL)
367 MALLOC(priv->links[linkNum], struct ng_bridge_link *,
368 sizeof(*priv->links[linkNum]), M_NETGRAPH_BRIDGE, M_NOWAIT|M_ZERO);
369 if (priv->links[linkNum] == NULL)
371 priv->links[linkNum]->hook = hook;
372 NG_HOOK_SET_PRIVATE(hook, (void *)linkNum);
377 /* Unknown hook name */
382 * Receive a control message
385 ng_bridge_rcvmsg(node_p node, item_p item, hook_p lasthook)
387 const priv_p priv = NG_NODE_PRIVATE(node);
388 struct ng_mesg *resp = NULL;
392 NGI_GET_MSG(item, msg);
393 switch (msg->header.typecookie) {
394 case NGM_BRIDGE_COOKIE:
395 switch (msg->header.cmd) {
396 case NGM_BRIDGE_GET_CONFIG:
398 struct ng_bridge_config *conf;
400 NG_MKRESPONSE(resp, msg,
401 sizeof(struct ng_bridge_config), M_NOWAIT);
406 conf = (struct ng_bridge_config *)resp->data;
407 *conf = priv->conf; /* no sanity checking needed */
410 case NGM_BRIDGE_SET_CONFIG:
412 struct ng_bridge_config *conf;
415 if (msg->header.arglen
416 != sizeof(struct ng_bridge_config)) {
420 conf = (struct ng_bridge_config *)msg->data;
422 for (i = 0; i < NG_BRIDGE_MAX_LINKS; i++)
423 priv->conf.ipfw[i] = !!priv->conf.ipfw[i];
426 case NGM_BRIDGE_RESET:
430 /* Flush all entries in the hash table */
431 ng_bridge_remove_hosts(priv, -1);
433 /* Reset all loop detection counters and stats */
434 for (i = 0; i < NG_BRIDGE_MAX_LINKS; i++) {
435 if (priv->links[i] == NULL)
437 priv->links[i]->loopCount = 0;
438 bzero(&priv->links[i]->stats,
439 sizeof(priv->links[i]->stats));
443 case NGM_BRIDGE_GET_STATS:
444 case NGM_BRIDGE_CLR_STATS:
445 case NGM_BRIDGE_GETCLR_STATS:
447 struct ng_bridge_link *link;
450 /* Get link number */
451 if (msg->header.arglen != sizeof(u_int32_t)) {
455 linkNum = *((u_int32_t *)msg->data);
456 if (linkNum < 0 || linkNum >= NG_BRIDGE_MAX_LINKS) {
460 if ((link = priv->links[linkNum]) == NULL) {
465 /* Get/clear stats */
466 if (msg->header.cmd != NGM_BRIDGE_CLR_STATS) {
467 NG_MKRESPONSE(resp, msg,
468 sizeof(link->stats), M_NOWAIT);
474 resp->data, sizeof(link->stats));
476 if (msg->header.cmd != NGM_BRIDGE_GET_STATS)
477 bzero(&link->stats, sizeof(link->stats));
480 case NGM_BRIDGE_GET_TABLE:
482 struct ng_bridge_host_ary *ary;
483 struct ng_bridge_hent *hent;
486 NG_MKRESPONSE(resp, msg, sizeof(*ary)
487 + (priv->numHosts * sizeof(*ary->hosts)), M_NOWAIT);
492 ary = (struct ng_bridge_host_ary *)resp->data;
493 ary->numHosts = priv->numHosts;
494 for (bucket = 0; bucket < priv->numBuckets; bucket++) {
495 SLIST_FOREACH(hent, &priv->tab[bucket], next)
496 ary->hosts[i++] = hent->host;
511 NG_RESPOND_MSG(error, node, item, resp);
517 * Receive data on a hook
520 ng_bridge_rcvdata(hook_p hook, item_p item)
522 const node_p node = NG_HOOK_NODE(hook);
523 const priv_p priv = NG_NODE_PRIVATE(node);
524 struct ng_bridge_host *host;
525 struct ng_bridge_link *link;
526 struct ether_header *eh;
527 int error = 0, linkNum;
531 struct ng_bridge_link *firstLink;
534 /* Get link number */
535 linkNum = (int)NG_HOOK_PRIVATE(hook);
536 KASSERT(linkNum >= 0 && linkNum < NG_BRIDGE_MAX_LINKS,
537 ("%s: linkNum=%u", __func__, linkNum));
538 link = priv->links[linkNum];
539 KASSERT(link != NULL, ("%s: link%d null", __func__, linkNum));
541 /* Sanity check packet and pull up header */
542 if (m->m_pkthdr.len < ETHER_HDR_LEN) {
543 link->stats.recvRunts++;
548 if (m->m_len < ETHER_HDR_LEN && !(m = m_pullup(m, ETHER_HDR_LEN))) {
549 link->stats.memoryFailures++;
553 eh = mtod(m, struct ether_header *);
554 if ((eh->ether_shost[0] & 1) != 0) {
555 link->stats.recvInvalid++;
561 /* Is link disabled due to a loopback condition? */
562 if (link->loopCount != 0) {
563 link->stats.loopDrops++;
566 return (ELOOP); /* XXX is this an appropriate error? */
570 link->stats.recvPackets++;
571 link->stats.recvOctets += m->m_pkthdr.len;
572 if ((manycast = (eh->ether_dhost[0] & 1)) != 0) {
573 if (ETHER_EQUAL(eh->ether_dhost, ng_bridge_bcast_addr)) {
574 link->stats.recvBroadcasts++;
577 link->stats.recvMulticasts++;
580 /* Look up packet's source Ethernet address in hashtable */
581 if ((host = ng_bridge_get(priv, eh->ether_shost)) != NULL) {
583 /* Update time since last heard from this host */
586 /* Did host jump to a different link? */
587 if (host->linkNum != linkNum) {
590 * If the host's old link was recently established
591 * on the old link and it's already jumped to a new
592 * link, declare a loopback condition.
594 if (host->age < priv->conf.minStableAge) {
596 /* Log the problem */
597 if (priv->conf.debugLevel >= 2) {
598 struct ifnet *ifp = m->m_pkthdr.rcvif;
602 snprintf(suffix, sizeof(suffix),
603 " (%s%d)", ifp->if_name,
607 log(LOG_WARNING, "ng_bridge: %s:"
608 " loopback detected on %s%s\n",
609 ng_bridge_nodename(node),
610 NG_HOOK_NAME(hook), suffix);
613 /* Mark link as linka non grata */
614 link->loopCount = priv->conf.loopTimeout;
615 link->stats.loopDetects++;
617 /* Forget all hosts on this link */
618 ng_bridge_remove_hosts(priv, linkNum);
621 link->stats.loopDrops++;
624 return (ELOOP); /* XXX appropriate? */
627 /* Move host over to new link */
628 host->linkNum = linkNum;
632 if (!ng_bridge_put(priv, eh->ether_shost, linkNum)) {
633 link->stats.memoryFailures++;
640 /* Run packet through ipfw processing, if enabled */
641 if (priv->conf.ipfw[linkNum] && fw_enable && ip_fw_chk_ptr != NULL) {
642 /* XXX not implemented yet */
646 * If unicast and destination host known, deliver to host's link,
647 * unless it is the same link as the packet came in on.
651 /* Determine packet destination link */
652 if ((host = ng_bridge_get(priv, eh->ether_dhost)) != NULL) {
653 struct ng_bridge_link *const destLink
654 = priv->links[host->linkNum];
656 /* If destination same as incoming link, do nothing */
657 KASSERT(destLink != NULL,
658 ("%s: link%d null", __func__, host->linkNum));
659 if (destLink == link) {
665 /* Deliver packet out the destination link */
666 destLink->stats.xmitPackets++;
667 destLink->stats.xmitOctets += m->m_pkthdr.len;
668 NG_FWD_NEW_DATA(error, item, destLink->hook, m);
672 /* Destination host is not known */
673 link->stats.recvUnknown++;
676 /* Distribute unknown, multicast, broadcast pkts to all other links */
677 meta = NGI_META(item); /* peek.. */
679 for (linkNum = 0; linkNum <= priv->numLinks; linkNum++) {
680 struct ng_bridge_link *destLink;
682 struct mbuf *m2 = NULL;
685 * If we have checked all the links then now
686 * send the original on its reserved link
688 if (linkNum == priv->numLinks) {
689 /* If we never saw a good link, leave. */
690 if (firstLink == NULL) {
695 destLink = firstLink;
697 destLink = priv->links[linkNum];
698 /* Skip incoming link and disconnected links */
699 if (destLink == NULL || destLink == link) {
702 if (firstLink == NULL) {
704 * This is the first usable link we have found.
705 * Reserve it for the originals.
706 * If we never find another we save a copy.
708 firstLink = destLink;
713 * It's usable link but not the reserved (first) one.
714 * Copy mbuf and meta info for sending.
716 m2 = m_dup(m, M_NOWAIT); /* XXX m_copypacket() */
718 link->stats.memoryFailures++;
724 && (meta2 = ng_copy_meta(meta)) == NULL) {
725 link->stats.memoryFailures++;
734 destLink->stats.xmitPackets++;
735 destLink->stats.xmitOctets += m->m_pkthdr.len;
737 case 0: /* unicast */
739 case 1: /* multicast */
740 destLink->stats.xmitMulticasts++;
742 case 2: /* broadcast */
743 destLink->stats.xmitBroadcasts++;
748 if (destLink == firstLink) {
750 * If we've sent all the others, send the original
751 * on the first link we found.
753 NG_FWD_NEW_DATA(error, item, destLink->hook, m);
754 break; /* always done last - not really needed. */
756 NG_SEND_DATA(error, destLink->hook, m2, meta2);
766 ng_bridge_shutdown(node_p node)
768 const priv_p priv = NG_NODE_PRIVATE(node);
771 * Shut down everything except the timer. There's no way to
772 * avoid another possible timeout event (it may have already
773 * been dequeued), so we can't free the node yet.
775 KASSERT(priv->numLinks == 0 && priv->numHosts == 0,
776 ("%s: numLinks=%d numHosts=%d",
777 __func__, priv->numLinks, priv->numHosts));
778 FREE(priv->tab, M_NETGRAPH_BRIDGE);
780 /* NG_INVALID flag is now set so node will be freed at next timeout */
785 * Hook disconnection.
788 ng_bridge_disconnect(hook_p hook)
790 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
793 /* Get link number */
794 linkNum = (int)NG_HOOK_PRIVATE(hook);
795 KASSERT(linkNum >= 0 && linkNum < NG_BRIDGE_MAX_LINKS,
796 ("%s: linkNum=%u", __func__, linkNum));
798 /* Remove all hosts associated with this link */
799 ng_bridge_remove_hosts(priv, linkNum);
801 /* Free associated link information */
802 KASSERT(priv->links[linkNum] != NULL, ("%s: no link", __func__));
803 FREE(priv->links[linkNum], M_NETGRAPH_BRIDGE);
804 priv->links[linkNum] = NULL;
807 /* If no more hooks, go away */
808 if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
809 && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook)))) {
810 ng_rmnode_self(NG_HOOK_NODE(hook));
815 /******************************************************************
817 ******************************************************************/
822 * Only hashing bytes 3-6 of the Ethernet address is sufficient and fast.
824 #define HASH(addr,mask) ( (((const u_int16_t *)(addr))[0] \
825 ^ ((const u_int16_t *)(addr))[1] \
826 ^ ((const u_int16_t *)(addr))[2]) & (mask) )
829 * Find a host entry in the table.
831 static struct ng_bridge_host *
832 ng_bridge_get(priv_p priv, const u_char *addr)
834 const int bucket = HASH(addr, priv->hashMask);
835 struct ng_bridge_hent *hent;
837 SLIST_FOREACH(hent, &priv->tab[bucket], next) {
838 if (ETHER_EQUAL(hent->host.addr, addr))
839 return (&hent->host);
845 * Add a new host entry to the table. This assumes the host doesn't
846 * already exist in the table. Returns 1 on success, 0 if there
847 * was a memory allocation failure.
850 ng_bridge_put(priv_p priv, const u_char *addr, int linkNum)
852 const int bucket = HASH(addr, priv->hashMask);
853 struct ng_bridge_hent *hent;
856 /* Assert that entry does not already exist in hashtable */
857 SLIST_FOREACH(hent, &priv->tab[bucket], next) {
858 KASSERT(!ETHER_EQUAL(hent->host.addr, addr),
859 ("%s: entry %6D exists in table", __func__, addr, ":"));
863 /* Allocate and initialize new hashtable entry */
864 MALLOC(hent, struct ng_bridge_hent *,
865 sizeof(*hent), M_NETGRAPH_BRIDGE, M_NOWAIT);
868 bcopy(addr, hent->host.addr, ETHER_ADDR_LEN);
869 hent->host.linkNum = linkNum;
870 hent->host.staleness = 0;
873 /* Add new element to hash bucket */
874 SLIST_INSERT_HEAD(&priv->tab[bucket], hent, next);
877 /* Resize table if necessary */
878 ng_bridge_rehash(priv);
883 * Resize the hash table. We try to maintain the number of buckets
884 * such that the load factor is in the range 0.25 to 1.0.
886 * If we can't get the new memory then we silently fail. This is OK
887 * because things will still work and we'll try again soon anyway.
890 ng_bridge_rehash(priv_p priv)
892 struct ng_bridge_bucket *newTab;
893 int oldBucket, newBucket;
897 /* Is table too full or too empty? */
898 if (priv->numHosts > priv->numBuckets
899 && (priv->numBuckets << 1) <= MAX_BUCKETS)
900 newNumBuckets = priv->numBuckets << 1;
901 else if (priv->numHosts < (priv->numBuckets >> 2)
902 && (priv->numBuckets >> 2) >= MIN_BUCKETS)
903 newNumBuckets = priv->numBuckets >> 2;
906 newMask = newNumBuckets - 1;
908 /* Allocate and initialize new table */
909 MALLOC(newTab, struct ng_bridge_bucket *,
910 newNumBuckets * sizeof(*newTab), M_NETGRAPH_BRIDGE, M_NOWAIT | M_ZERO);
914 /* Move all entries from old table to new table */
915 for (oldBucket = 0; oldBucket < priv->numBuckets; oldBucket++) {
916 struct ng_bridge_bucket *const oldList = &priv->tab[oldBucket];
918 while (!SLIST_EMPTY(oldList)) {
919 struct ng_bridge_hent *const hent
920 = SLIST_FIRST(oldList);
922 SLIST_REMOVE_HEAD(oldList, next);
923 newBucket = HASH(hent->host.addr, newMask);
924 SLIST_INSERT_HEAD(&newTab[newBucket], hent, next);
928 /* Replace old table with new one */
929 if (priv->conf.debugLevel >= 3) {
930 log(LOG_INFO, "ng_bridge: %s: table size %d -> %d\n",
931 ng_bridge_nodename(priv->node),
932 priv->numBuckets, newNumBuckets);
934 FREE(priv->tab, M_NETGRAPH_BRIDGE);
935 priv->numBuckets = newNumBuckets;
936 priv->hashMask = newMask;
941 /******************************************************************
943 ******************************************************************/
946 * Remove all hosts associated with a specific link from the hashtable.
947 * If linkNum == -1, then remove all hosts in the table.
950 ng_bridge_remove_hosts(priv_p priv, int linkNum)
954 for (bucket = 0; bucket < priv->numBuckets; bucket++) {
955 struct ng_bridge_hent **hptr = &SLIST_FIRST(&priv->tab[bucket]);
957 while (*hptr != NULL) {
958 struct ng_bridge_hent *const hent = *hptr;
960 if (linkNum == -1 || hent->host.linkNum == linkNum) {
961 *hptr = SLIST_NEXT(hent, next);
962 FREE(hent, M_NETGRAPH_BRIDGE);
965 hptr = &SLIST_NEXT(hent, next);
971 * Handle our once-per-second timeout event. We do two things:
972 * we decrement link->loopCount for those links being muted due to
973 * a detected loopback condition, and we remove any hosts from
974 * the hashtable whom we haven't heard from in a long while.
976 * If the node has the NG_INVALID flag set, our job is to kill it.
979 ng_bridge_timeout(void *arg)
981 const node_p node = arg;
982 const priv_p priv = NG_NODE_PRIVATE(node);
987 /* If node was shut down, this is the final lingering timeout */
989 if (NG_NODE_NOT_VALID(node)) {
990 FREE(priv, M_NETGRAPH);
991 NG_NODE_SET_PRIVATE(node, NULL);
997 /* Register a new timeout, keeping the existing node reference */
998 callout_reset(&priv->timer, hz, ng_bridge_timeout, node);
1000 /* Update host time counters and remove stale entries */
1001 for (bucket = 0; bucket < priv->numBuckets; bucket++) {
1002 struct ng_bridge_hent **hptr = &SLIST_FIRST(&priv->tab[bucket]);
1004 while (*hptr != NULL) {
1005 struct ng_bridge_hent *const hent = *hptr;
1007 /* Make sure host's link really exists */
1008 KASSERT(priv->links[hent->host.linkNum] != NULL,
1009 ("%s: host %6D on nonexistent link %d\n",
1010 __func__, hent->host.addr, ":",
1011 hent->host.linkNum));
1013 /* Remove hosts we haven't heard from in a while */
1014 if (++hent->host.staleness >= priv->conf.maxStaleness) {
1015 *hptr = SLIST_NEXT(hent, next);
1016 FREE(hent, M_NETGRAPH_BRIDGE);
1019 if (hent->host.age < 0xffff)
1021 hptr = &SLIST_NEXT(hent, next);
1026 KASSERT(priv->numHosts == counter,
1027 ("%s: hosts: %d != %d", __func__, priv->numHosts, counter));
1029 /* Decrease table size if necessary */
1030 ng_bridge_rehash(priv);
1032 /* Decrease loop counter on muted looped back links */
1033 for (counter = linkNum = 0; linkNum < NG_BRIDGE_MAX_LINKS; linkNum++) {
1034 struct ng_bridge_link *const link = priv->links[linkNum];
1037 if (link->loopCount != 0) {
1039 if (link->loopCount == 0
1040 && priv->conf.debugLevel >= 2) {
1041 log(LOG_INFO, "ng_bridge: %s:"
1042 " restoring looped back link%d\n",
1043 ng_bridge_nodename(node), linkNum);
1049 KASSERT(priv->numLinks == counter,
1050 ("%s: links: %d != %d", __func__, priv->numLinks, counter));
1057 * Return node's "name", even if it doesn't have one.
1060 ng_bridge_nodename(node_p node)
1062 static char name[NG_NODELEN+1];
1064 if (NG_NODE_NAME(node) != NULL)
1065 snprintf(name, sizeof(name), "%s", NG_NODE_NAME(node));
1067 snprintf(name, sizeof(name), "[%x]", ng_node2ID(node));