6 * Copyright (c) 2000 Whistle Communications, Inc.
9 * Subject to the following obligations and disclaimer of warranty, use and
10 * redistribution of this software, in source or object code forms, with or
11 * without modifications are expressly permitted by Whistle Communications;
12 * provided, however, that:
13 * 1. Any and all reproductions of the source or object code must include the
14 * copyright notice above and the following disclaimer of warranties; and
15 * 2. No rights are granted, in any manner or form, to use Whistle
16 * Communications, Inc. trademarks, including the mark "WHISTLE
17 * COMMUNICATIONS" on advertising, endorsements, or otherwise except as
18 * such appears in the above copyright notice or in the software.
20 * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
21 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
22 * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
23 * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
24 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
25 * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
26 * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
27 * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
28 * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
29 * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
30 * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
31 * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
32 * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35 * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
38 * Author: Archie Cobbs <archie@freebsd.org>
44 * ng_bridge(4) netgraph node type
46 * The node performs standard intelligent Ethernet bridging over
47 * each of its connected hooks, or links. A simple loop detection
48 * algorithm is included which disables a link for priv->conf.loopTimeout
49 * seconds when a host is seen to have jumped from one link to
50 * another within priv->conf.minStableAge seconds.
52 * We keep a hashtable that maps Ethernet addresses to host info,
53 * which is contained in struct ng_bridge_host's. These structures
54 * tell us on which link the host may be found. A host's entry will
55 * expire after priv->conf.maxStaleness seconds.
57 * This node is optimzed for stable networks, where machines jump
58 * from one port to the other only rarely.
61 #include <sys/param.h>
62 #include <sys/systm.h>
63 #include <sys/kernel.h>
65 #include <sys/malloc.h>
67 #include <sys/errno.h>
68 #include <sys/rwlock.h>
69 #include <sys/syslog.h>
70 #include <sys/socket.h>
71 #include <sys/ctype.h>
74 #include <net/ethernet.h>
77 #include <netinet/in.h>
78 #include <netinet/ip_fw.h>
80 #include <netgraph/ng_message.h>
81 #include <netgraph/netgraph.h>
82 #include <netgraph/ng_parse.h>
83 #include <netgraph/ng_bridge.h>
85 #ifdef NG_SEPARATE_MALLOC
86 MALLOC_DEFINE(M_NETGRAPH_BRIDGE, "netgraph_bridge", "netgraph bridge node");
88 #define M_NETGRAPH_BRIDGE M_NETGRAPH
91 /* Per-link private data */
92 struct ng_bridge_link {
93 hook_p hook; /* netgraph hook */
94 u_int16_t loopCount; /* loop ignore timer */
95 struct ng_bridge_link_stats stats; /* link stats */
98 /* Per-node private data */
99 struct ng_bridge_private {
100 struct ng_bridge_bucket *tab; /* hash table bucket array */
101 struct ng_bridge_link *links[NG_BRIDGE_MAX_LINKS];
102 struct ng_bridge_config conf; /* node configuration */
103 node_p node; /* netgraph node */
104 u_int numHosts; /* num entries in table */
105 u_int numBuckets; /* num buckets in table */
106 u_int hashMask; /* numBuckets - 1 */
107 int numLinks; /* num connected links */
108 int persistent; /* can exist w/o hooks */
109 struct callout timer; /* one second periodic timer */
111 typedef struct ng_bridge_private *priv_p;
113 /* Information about a host, stored in a hash table entry */
114 struct ng_bridge_hent {
115 struct ng_bridge_host host; /* actual host info */
116 SLIST_ENTRY(ng_bridge_hent) next; /* next entry in bucket */
119 /* Hash table bucket declaration */
120 SLIST_HEAD(ng_bridge_bucket, ng_bridge_hent);
122 /* Netgraph node methods */
123 static ng_constructor_t ng_bridge_constructor;
124 static ng_rcvmsg_t ng_bridge_rcvmsg;
125 static ng_shutdown_t ng_bridge_shutdown;
126 static ng_newhook_t ng_bridge_newhook;
127 static ng_rcvdata_t ng_bridge_rcvdata;
128 static ng_disconnect_t ng_bridge_disconnect;
130 /* Other internal functions */
131 static struct ng_bridge_host *ng_bridge_get(priv_p priv, const u_char *addr);
132 static int ng_bridge_put(priv_p priv, const u_char *addr, int linkNum);
133 static void ng_bridge_rehash(priv_p priv);
134 static void ng_bridge_remove_hosts(priv_p priv, int linkNum);
135 static void ng_bridge_timeout(node_p node, hook_p hook, void *arg1, int arg2);
136 static const char *ng_bridge_nodename(node_p node);
138 /* Ethernet broadcast */
139 static const u_char ng_bridge_bcast_addr[ETHER_ADDR_LEN] =
140 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
142 /* Store each hook's link number in the private field */
143 #define LINK_NUM(hook) (*(u_int16_t *)(&(hook)->private))
145 /* Compare Ethernet addresses using 32 and 16 bit words instead of bytewise */
146 #define ETHER_EQUAL(a,b) (((const u_int32_t *)(a))[0] \
147 == ((const u_int32_t *)(b))[0] \
148 && ((const u_int16_t *)(a))[2] \
149 == ((const u_int16_t *)(b))[2])
151 /* Minimum and maximum number of hash buckets. Must be a power of two. */
152 #define MIN_BUCKETS (1 << 5) /* 32 */
153 #define MAX_BUCKETS (1 << 14) /* 16384 */
155 /* Configuration default values */
156 #define DEFAULT_LOOP_TIMEOUT 60
157 #define DEFAULT_MAX_STALENESS (15 * 60) /* same as ARP timeout */
158 #define DEFAULT_MIN_STABLE_AGE 1
160 /******************************************************************
162 ******************************************************************/
165 * How to determine the length of the table returned by NGM_BRIDGE_GET_TABLE
168 ng_bridge_getTableLength(const struct ng_parse_type *type,
169 const u_char *start, const u_char *buf)
171 const struct ng_bridge_host_ary *const hary
172 = (const struct ng_bridge_host_ary *)(buf - sizeof(u_int32_t));
174 return hary->numHosts;
177 /* Parse type for struct ng_bridge_host_ary */
178 static const struct ng_parse_struct_field ng_bridge_host_type_fields[]
179 = NG_BRIDGE_HOST_TYPE_INFO(&ng_parse_enaddr_type);
180 static const struct ng_parse_type ng_bridge_host_type = {
181 &ng_parse_struct_type,
182 &ng_bridge_host_type_fields
184 static const struct ng_parse_array_info ng_bridge_hary_type_info = {
185 &ng_bridge_host_type,
186 ng_bridge_getTableLength
188 static const struct ng_parse_type ng_bridge_hary_type = {
189 &ng_parse_array_type,
190 &ng_bridge_hary_type_info
192 static const struct ng_parse_struct_field ng_bridge_host_ary_type_fields[]
193 = NG_BRIDGE_HOST_ARY_TYPE_INFO(&ng_bridge_hary_type);
194 static const struct ng_parse_type ng_bridge_host_ary_type = {
195 &ng_parse_struct_type,
196 &ng_bridge_host_ary_type_fields
199 /* Parse type for struct ng_bridge_config */
200 static const struct ng_parse_fixedarray_info ng_bridge_ipfwary_type_info = {
201 &ng_parse_uint8_type,
204 static const struct ng_parse_type ng_bridge_ipfwary_type = {
205 &ng_parse_fixedarray_type,
206 &ng_bridge_ipfwary_type_info
208 static const struct ng_parse_struct_field ng_bridge_config_type_fields[]
209 = NG_BRIDGE_CONFIG_TYPE_INFO(&ng_bridge_ipfwary_type);
210 static const struct ng_parse_type ng_bridge_config_type = {
211 &ng_parse_struct_type,
212 &ng_bridge_config_type_fields
215 /* Parse type for struct ng_bridge_link_stat */
216 static const struct ng_parse_struct_field ng_bridge_stats_type_fields[]
217 = NG_BRIDGE_STATS_TYPE_INFO;
218 static const struct ng_parse_type ng_bridge_stats_type = {
219 &ng_parse_struct_type,
220 &ng_bridge_stats_type_fields
223 /* List of commands and how to convert arguments to/from ASCII */
224 static const struct ng_cmdlist ng_bridge_cmdlist[] = {
227 NGM_BRIDGE_SET_CONFIG,
229 &ng_bridge_config_type,
234 NGM_BRIDGE_GET_CONFIG,
237 &ng_bridge_config_type
248 NGM_BRIDGE_GET_STATS,
250 &ng_parse_uint32_type,
251 &ng_bridge_stats_type
255 NGM_BRIDGE_CLR_STATS,
257 &ng_parse_uint32_type,
262 NGM_BRIDGE_GETCLR_STATS,
264 &ng_parse_uint32_type,
265 &ng_bridge_stats_type
269 NGM_BRIDGE_GET_TABLE,
272 &ng_bridge_host_ary_type
276 NGM_BRIDGE_SET_PERSISTENT,
284 /* Node type descriptor */
285 static struct ng_type ng_bridge_typestruct = {
286 .version = NG_ABI_VERSION,
287 .name = NG_BRIDGE_NODE_TYPE,
288 .constructor = ng_bridge_constructor,
289 .rcvmsg = ng_bridge_rcvmsg,
290 .shutdown = ng_bridge_shutdown,
291 .newhook = ng_bridge_newhook,
292 .rcvdata = ng_bridge_rcvdata,
293 .disconnect = ng_bridge_disconnect,
294 .cmdlist = ng_bridge_cmdlist,
296 NETGRAPH_INIT(bridge, &ng_bridge_typestruct);
298 /******************************************************************
299 NETGRAPH NODE METHODS
300 ******************************************************************/
306 ng_bridge_constructor(node_p node)
310 /* Allocate and initialize private info */
311 priv = malloc(sizeof(*priv), M_NETGRAPH_BRIDGE, M_NOWAIT | M_ZERO);
314 ng_callout_init(&priv->timer);
316 /* Allocate and initialize hash table, etc. */
317 priv->tab = malloc(MIN_BUCKETS * sizeof(*priv->tab),
318 M_NETGRAPH_BRIDGE, M_NOWAIT | M_ZERO);
319 if (priv->tab == NULL) {
320 free(priv, M_NETGRAPH_BRIDGE);
323 priv->numBuckets = MIN_BUCKETS;
324 priv->hashMask = MIN_BUCKETS - 1;
325 priv->conf.debugLevel = 1;
326 priv->conf.loopTimeout = DEFAULT_LOOP_TIMEOUT;
327 priv->conf.maxStaleness = DEFAULT_MAX_STALENESS;
328 priv->conf.minStableAge = DEFAULT_MIN_STABLE_AGE;
331 * This node has all kinds of stuff that could be screwed by SMP.
332 * Until it gets it's own internal protection, we go through in
333 * single file. This could hurt a machine bridging beteen two
334 * GB ethernets so it should be fixed.
335 * When it's fixed the process SHOULD NOT SLEEP, spinlocks please!
338 NG_NODE_FORCE_WRITER(node);
339 NG_NODE_SET_PRIVATE(node, priv);
342 /* Start timer; timer is always running while node is alive */
343 ng_callout(&priv->timer, node, NULL, hz, ng_bridge_timeout, NULL, 0);
350 * Method for attaching a new hook
353 ng_bridge_newhook(node_p node, hook_p hook, const char *name)
355 const priv_p priv = NG_NODE_PRIVATE(node);
357 /* Check for a link hook */
358 if (strncmp(name, NG_BRIDGE_HOOK_LINK_PREFIX,
359 strlen(NG_BRIDGE_HOOK_LINK_PREFIX)) == 0) {
364 cp = name + strlen(NG_BRIDGE_HOOK_LINK_PREFIX);
365 if (!isdigit(*cp) || (cp[0] == '0' && cp[1] != '\0'))
367 linkNum = strtoul(cp, &eptr, 10);
368 if (*eptr != '\0' || linkNum >= NG_BRIDGE_MAX_LINKS)
370 if (priv->links[linkNum] != NULL)
372 priv->links[linkNum] = malloc(sizeof(*priv->links[linkNum]),
373 M_NETGRAPH_BRIDGE, M_NOWAIT|M_ZERO);
374 if (priv->links[linkNum] == NULL)
376 priv->links[linkNum]->hook = hook;
377 NG_HOOK_SET_PRIVATE(hook, (void *)linkNum);
382 /* Unknown hook name */
387 * Receive a control message
390 ng_bridge_rcvmsg(node_p node, item_p item, hook_p lasthook)
392 const priv_p priv = NG_NODE_PRIVATE(node);
393 struct ng_mesg *resp = NULL;
397 NGI_GET_MSG(item, msg);
398 switch (msg->header.typecookie) {
399 case NGM_BRIDGE_COOKIE:
400 switch (msg->header.cmd) {
401 case NGM_BRIDGE_GET_CONFIG:
403 struct ng_bridge_config *conf;
405 NG_MKRESPONSE(resp, msg,
406 sizeof(struct ng_bridge_config), M_NOWAIT);
411 conf = (struct ng_bridge_config *)resp->data;
412 *conf = priv->conf; /* no sanity checking needed */
415 case NGM_BRIDGE_SET_CONFIG:
417 struct ng_bridge_config *conf;
420 if (msg->header.arglen
421 != sizeof(struct ng_bridge_config)) {
425 conf = (struct ng_bridge_config *)msg->data;
427 for (i = 0; i < NG_BRIDGE_MAX_LINKS; i++)
428 priv->conf.ipfw[i] = !!priv->conf.ipfw[i];
431 case NGM_BRIDGE_RESET:
435 /* Flush all entries in the hash table */
436 ng_bridge_remove_hosts(priv, -1);
438 /* Reset all loop detection counters and stats */
439 for (i = 0; i < NG_BRIDGE_MAX_LINKS; i++) {
440 if (priv->links[i] == NULL)
442 priv->links[i]->loopCount = 0;
443 bzero(&priv->links[i]->stats,
444 sizeof(priv->links[i]->stats));
448 case NGM_BRIDGE_GET_STATS:
449 case NGM_BRIDGE_CLR_STATS:
450 case NGM_BRIDGE_GETCLR_STATS:
452 struct ng_bridge_link *link;
455 /* Get link number */
456 if (msg->header.arglen != sizeof(u_int32_t)) {
460 linkNum = *((u_int32_t *)msg->data);
461 if (linkNum < 0 || linkNum >= NG_BRIDGE_MAX_LINKS) {
465 if ((link = priv->links[linkNum]) == NULL) {
470 /* Get/clear stats */
471 if (msg->header.cmd != NGM_BRIDGE_CLR_STATS) {
472 NG_MKRESPONSE(resp, msg,
473 sizeof(link->stats), M_NOWAIT);
479 resp->data, sizeof(link->stats));
481 if (msg->header.cmd != NGM_BRIDGE_GET_STATS)
482 bzero(&link->stats, sizeof(link->stats));
485 case NGM_BRIDGE_GET_TABLE:
487 struct ng_bridge_host_ary *ary;
488 struct ng_bridge_hent *hent;
491 NG_MKRESPONSE(resp, msg, sizeof(*ary)
492 + (priv->numHosts * sizeof(*ary->hosts)), M_NOWAIT);
497 ary = (struct ng_bridge_host_ary *)resp->data;
498 ary->numHosts = priv->numHosts;
499 for (bucket = 0; bucket < priv->numBuckets; bucket++) {
500 SLIST_FOREACH(hent, &priv->tab[bucket], next)
501 ary->hosts[i++] = hent->host;
505 case NGM_BRIDGE_SET_PERSISTENT:
507 priv->persistent = 1;
521 NG_RESPOND_MSG(error, node, item, resp);
527 * Receive data on a hook
530 ng_bridge_rcvdata(hook_p hook, item_p item)
532 const node_p node = NG_HOOK_NODE(hook);
533 const priv_p priv = NG_NODE_PRIVATE(node);
534 struct ng_bridge_host *host;
535 struct ng_bridge_link *link;
536 struct ether_header *eh;
537 int error = 0, linkNum, linksSeen;
540 struct ng_bridge_link *firstLink;
543 /* Get link number */
544 linkNum = (intptr_t)NG_HOOK_PRIVATE(hook);
545 KASSERT(linkNum >= 0 && linkNum < NG_BRIDGE_MAX_LINKS,
546 ("%s: linkNum=%u", __func__, linkNum));
547 link = priv->links[linkNum];
548 KASSERT(link != NULL, ("%s: link%d null", __func__, linkNum));
550 /* Sanity check packet and pull up header */
551 if (m->m_pkthdr.len < ETHER_HDR_LEN) {
552 link->stats.recvRunts++;
557 if (m->m_len < ETHER_HDR_LEN && !(m = m_pullup(m, ETHER_HDR_LEN))) {
558 link->stats.memoryFailures++;
562 eh = mtod(m, struct ether_header *);
563 if ((eh->ether_shost[0] & 1) != 0) {
564 link->stats.recvInvalid++;
570 /* Is link disabled due to a loopback condition? */
571 if (link->loopCount != 0) {
572 link->stats.loopDrops++;
575 return (ELOOP); /* XXX is this an appropriate error? */
579 link->stats.recvPackets++;
580 link->stats.recvOctets += m->m_pkthdr.len;
581 if ((manycast = (eh->ether_dhost[0] & 1)) != 0) {
582 if (ETHER_EQUAL(eh->ether_dhost, ng_bridge_bcast_addr)) {
583 link->stats.recvBroadcasts++;
586 link->stats.recvMulticasts++;
589 /* Look up packet's source Ethernet address in hashtable */
590 if ((host = ng_bridge_get(priv, eh->ether_shost)) != NULL) {
592 /* Update time since last heard from this host */
595 /* Did host jump to a different link? */
596 if (host->linkNum != linkNum) {
599 * If the host's old link was recently established
600 * on the old link and it's already jumped to a new
601 * link, declare a loopback condition.
603 if (host->age < priv->conf.minStableAge) {
605 /* Log the problem */
606 if (priv->conf.debugLevel >= 2) {
607 struct ifnet *ifp = m->m_pkthdr.rcvif;
611 snprintf(suffix, sizeof(suffix),
612 " (%s)", ifp->if_xname);
615 log(LOG_WARNING, "ng_bridge: %s:"
616 " loopback detected on %s%s\n",
617 ng_bridge_nodename(node),
618 NG_HOOK_NAME(hook), suffix);
621 /* Mark link as linka non grata */
622 link->loopCount = priv->conf.loopTimeout;
623 link->stats.loopDetects++;
625 /* Forget all hosts on this link */
626 ng_bridge_remove_hosts(priv, linkNum);
629 link->stats.loopDrops++;
632 return (ELOOP); /* XXX appropriate? */
635 /* Move host over to new link */
636 host->linkNum = linkNum;
640 if (!ng_bridge_put(priv, eh->ether_shost, linkNum)) {
641 link->stats.memoryFailures++;
648 /* Run packet through ipfw processing, if enabled */
650 if (priv->conf.ipfw[linkNum] && V_fw_enable && V_ip_fw_chk_ptr != NULL) {
651 /* XXX not implemented yet */
656 * If unicast and destination host known, deliver to host's link,
657 * unless it is the same link as the packet came in on.
661 /* Determine packet destination link */
662 if ((host = ng_bridge_get(priv, eh->ether_dhost)) != NULL) {
663 struct ng_bridge_link *const destLink
664 = priv->links[host->linkNum];
666 /* If destination same as incoming link, do nothing */
667 KASSERT(destLink != NULL,
668 ("%s: link%d null", __func__, host->linkNum));
669 if (destLink == link) {
675 /* Deliver packet out the destination link */
676 destLink->stats.xmitPackets++;
677 destLink->stats.xmitOctets += m->m_pkthdr.len;
678 NG_FWD_NEW_DATA(error, item, destLink->hook, m);
682 /* Destination host is not known */
683 link->stats.recvUnknown++;
686 /* Distribute unknown, multicast, broadcast pkts to all other links */
688 for (linkNum = linksSeen = 0; linksSeen <= priv->numLinks; linkNum++) {
689 struct ng_bridge_link *destLink;
690 struct mbuf *m2 = NULL;
693 * If we have checked all the links then now
694 * send the original on its reserved link
696 if (linksSeen == priv->numLinks) {
697 /* If we never saw a good link, leave. */
698 if (firstLink == NULL) {
703 destLink = firstLink;
705 destLink = priv->links[linkNum];
706 if (destLink != NULL)
708 /* Skip incoming link and disconnected links */
709 if (destLink == NULL || destLink == link) {
712 if (firstLink == NULL) {
714 * This is the first usable link we have found.
715 * Reserve it for the originals.
716 * If we never find another we save a copy.
718 firstLink = destLink;
723 * It's usable link but not the reserved (first) one.
724 * Copy mbuf info for sending.
726 m2 = m_dup(m, M_DONTWAIT); /* XXX m_copypacket() */
728 link->stats.memoryFailures++;
736 destLink->stats.xmitPackets++;
737 destLink->stats.xmitOctets += m->m_pkthdr.len;
739 case 0: /* unicast */
741 case 1: /* multicast */
742 destLink->stats.xmitMulticasts++;
744 case 2: /* broadcast */
745 destLink->stats.xmitBroadcasts++;
750 if (destLink == firstLink) {
752 * If we've sent all the others, send the original
753 * on the first link we found.
755 NG_FWD_NEW_DATA(error, item, destLink->hook, m);
756 break; /* always done last - not really needed. */
758 NG_SEND_DATA_ONLY(error, destLink->hook, m2);
768 ng_bridge_shutdown(node_p node)
770 const priv_p priv = NG_NODE_PRIVATE(node);
773 * Shut down everything including the timer. Even if the
774 * callout has already been dequeued and is about to be
775 * run, ng_bridge_timeout() won't be fired as the node
776 * is already marked NGF_INVALID, so we're safe to free
779 KASSERT(priv->numLinks == 0 && priv->numHosts == 0,
780 ("%s: numLinks=%d numHosts=%d",
781 __func__, priv->numLinks, priv->numHosts));
782 ng_uncallout(&priv->timer, node);
783 NG_NODE_SET_PRIVATE(node, NULL);
785 free(priv->tab, M_NETGRAPH_BRIDGE);
786 free(priv, M_NETGRAPH_BRIDGE);
791 * Hook disconnection.
794 ng_bridge_disconnect(hook_p hook)
796 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
799 /* Get link number */
800 linkNum = (intptr_t)NG_HOOK_PRIVATE(hook);
801 KASSERT(linkNum >= 0 && linkNum < NG_BRIDGE_MAX_LINKS,
802 ("%s: linkNum=%u", __func__, linkNum));
804 /* Remove all hosts associated with this link */
805 ng_bridge_remove_hosts(priv, linkNum);
807 /* Free associated link information */
808 KASSERT(priv->links[linkNum] != NULL, ("%s: no link", __func__));
809 free(priv->links[linkNum], M_NETGRAPH_BRIDGE);
810 priv->links[linkNum] = NULL;
813 /* If no more hooks, go away */
814 if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
815 && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook)))
816 && !priv->persistent) {
817 ng_rmnode_self(NG_HOOK_NODE(hook));
822 /******************************************************************
824 ******************************************************************/
829 #define HASH(addr,mask) ( (((const u_int16_t *)(addr))[0] \
830 ^ ((const u_int16_t *)(addr))[1] \
831 ^ ((const u_int16_t *)(addr))[2]) & (mask) )
834 * Find a host entry in the table.
836 static struct ng_bridge_host *
837 ng_bridge_get(priv_p priv, const u_char *addr)
839 const int bucket = HASH(addr, priv->hashMask);
840 struct ng_bridge_hent *hent;
842 SLIST_FOREACH(hent, &priv->tab[bucket], next) {
843 if (ETHER_EQUAL(hent->host.addr, addr))
844 return (&hent->host);
850 * Add a new host entry to the table. This assumes the host doesn't
851 * already exist in the table. Returns 1 on success, 0 if there
852 * was a memory allocation failure.
855 ng_bridge_put(priv_p priv, const u_char *addr, int linkNum)
857 const int bucket = HASH(addr, priv->hashMask);
858 struct ng_bridge_hent *hent;
861 /* Assert that entry does not already exist in hashtable */
862 SLIST_FOREACH(hent, &priv->tab[bucket], next) {
863 KASSERT(!ETHER_EQUAL(hent->host.addr, addr),
864 ("%s: entry %6D exists in table", __func__, addr, ":"));
868 /* Allocate and initialize new hashtable entry */
869 hent = malloc(sizeof(*hent), M_NETGRAPH_BRIDGE, M_NOWAIT);
872 bcopy(addr, hent->host.addr, ETHER_ADDR_LEN);
873 hent->host.linkNum = linkNum;
874 hent->host.staleness = 0;
877 /* Add new element to hash bucket */
878 SLIST_INSERT_HEAD(&priv->tab[bucket], hent, next);
881 /* Resize table if necessary */
882 ng_bridge_rehash(priv);
887 * Resize the hash table. We try to maintain the number of buckets
888 * such that the load factor is in the range 0.25 to 1.0.
890 * If we can't get the new memory then we silently fail. This is OK
891 * because things will still work and we'll try again soon anyway.
894 ng_bridge_rehash(priv_p priv)
896 struct ng_bridge_bucket *newTab;
897 int oldBucket, newBucket;
901 /* Is table too full or too empty? */
902 if (priv->numHosts > priv->numBuckets
903 && (priv->numBuckets << 1) <= MAX_BUCKETS)
904 newNumBuckets = priv->numBuckets << 1;
905 else if (priv->numHosts < (priv->numBuckets >> 2)
906 && (priv->numBuckets >> 2) >= MIN_BUCKETS)
907 newNumBuckets = priv->numBuckets >> 2;
910 newMask = newNumBuckets - 1;
912 /* Allocate and initialize new table */
913 newTab = malloc(newNumBuckets * sizeof(*newTab),
914 M_NETGRAPH_BRIDGE, M_NOWAIT | M_ZERO);
918 /* Move all entries from old table to new table */
919 for (oldBucket = 0; oldBucket < priv->numBuckets; oldBucket++) {
920 struct ng_bridge_bucket *const oldList = &priv->tab[oldBucket];
922 while (!SLIST_EMPTY(oldList)) {
923 struct ng_bridge_hent *const hent
924 = SLIST_FIRST(oldList);
926 SLIST_REMOVE_HEAD(oldList, next);
927 newBucket = HASH(hent->host.addr, newMask);
928 SLIST_INSERT_HEAD(&newTab[newBucket], hent, next);
932 /* Replace old table with new one */
933 if (priv->conf.debugLevel >= 3) {
934 log(LOG_INFO, "ng_bridge: %s: table size %d -> %d\n",
935 ng_bridge_nodename(priv->node),
936 priv->numBuckets, newNumBuckets);
938 free(priv->tab, M_NETGRAPH_BRIDGE);
939 priv->numBuckets = newNumBuckets;
940 priv->hashMask = newMask;
945 /******************************************************************
947 ******************************************************************/
950 * Remove all hosts associated with a specific link from the hashtable.
951 * If linkNum == -1, then remove all hosts in the table.
954 ng_bridge_remove_hosts(priv_p priv, int linkNum)
958 for (bucket = 0; bucket < priv->numBuckets; bucket++) {
959 struct ng_bridge_hent **hptr = &SLIST_FIRST(&priv->tab[bucket]);
961 while (*hptr != NULL) {
962 struct ng_bridge_hent *const hent = *hptr;
964 if (linkNum == -1 || hent->host.linkNum == linkNum) {
965 *hptr = SLIST_NEXT(hent, next);
966 free(hent, M_NETGRAPH_BRIDGE);
969 hptr = &SLIST_NEXT(hent, next);
975 * Handle our once-per-second timeout event. We do two things:
976 * we decrement link->loopCount for those links being muted due to
977 * a detected loopback condition, and we remove any hosts from
978 * the hashtable whom we haven't heard from in a long while.
981 ng_bridge_timeout(node_p node, hook_p hook, void *arg1, int arg2)
983 const priv_p priv = NG_NODE_PRIVATE(node);
988 /* Update host time counters and remove stale entries */
989 for (bucket = 0; bucket < priv->numBuckets; bucket++) {
990 struct ng_bridge_hent **hptr = &SLIST_FIRST(&priv->tab[bucket]);
992 while (*hptr != NULL) {
993 struct ng_bridge_hent *const hent = *hptr;
995 /* Make sure host's link really exists */
996 KASSERT(priv->links[hent->host.linkNum] != NULL,
997 ("%s: host %6D on nonexistent link %d\n",
998 __func__, hent->host.addr, ":",
999 hent->host.linkNum));
1001 /* Remove hosts we haven't heard from in a while */
1002 if (++hent->host.staleness >= priv->conf.maxStaleness) {
1003 *hptr = SLIST_NEXT(hent, next);
1004 free(hent, M_NETGRAPH_BRIDGE);
1007 if (hent->host.age < 0xffff)
1009 hptr = &SLIST_NEXT(hent, next);
1014 KASSERT(priv->numHosts == counter,
1015 ("%s: hosts: %d != %d", __func__, priv->numHosts, counter));
1017 /* Decrease table size if necessary */
1018 ng_bridge_rehash(priv);
1020 /* Decrease loop counter on muted looped back links */
1021 for (counter = linkNum = 0; linkNum < NG_BRIDGE_MAX_LINKS; linkNum++) {
1022 struct ng_bridge_link *const link = priv->links[linkNum];
1025 if (link->loopCount != 0) {
1027 if (link->loopCount == 0
1028 && priv->conf.debugLevel >= 2) {
1029 log(LOG_INFO, "ng_bridge: %s:"
1030 " restoring looped back link%d\n",
1031 ng_bridge_nodename(node), linkNum);
1037 KASSERT(priv->numLinks == counter,
1038 ("%s: links: %d != %d", __func__, priv->numLinks, counter));
1040 /* Register a new timeout, keeping the existing node reference */
1041 ng_callout(&priv->timer, node, NULL, hz, ng_bridge_timeout, NULL, 0);
1045 * Return node's "name", even if it doesn't have one.
1048 ng_bridge_nodename(node_p node)
1050 static char name[NG_NODESIZ];
1052 if (NG_NODE_NAME(node) != NULL)
1053 snprintf(name, sizeof(name), "%s", NG_NODE_NAME(node));
1055 snprintf(name, sizeof(name), "[%x]", ng_node2ID(node));