2 * Copyright (c) 2000 Whistle Communications, Inc.
5 * Subject to the following obligations and disclaimer of warranty, use and
6 * redistribution of this software, in source or object code forms, with or
7 * without modifications are expressly permitted by Whistle Communications;
8 * provided, however, that:
9 * 1. Any and all reproductions of the source or object code must include the
10 * copyright notice above and the following disclaimer of warranties; and
11 * 2. No rights are granted, in any manner or form, to use Whistle
12 * Communications, Inc. trademarks, including the mark "WHISTLE
13 * COMMUNICATIONS" on advertising, endorsements, or otherwise except as
14 * such appears in the above copyright notice or in the software.
16 * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
17 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
18 * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
19 * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
21 * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
22 * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
23 * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
24 * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
25 * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
26 * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
27 * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
28 * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
34 * Author: Archie Cobbs <archie@freebsd.org>
38 * ng_bridge(4) netgraph node type
40 * The node performs standard intelligent Ethernet bridging over
41 * each of its connected hooks, or links. A simple loop detection
42 * algorithm is included which disables a link for priv->conf.loopTimeout
43 * seconds when a host is seen to have jumped from one link to
44 * another within priv->conf.minStableAge seconds.
46 * We keep a hashtable that maps Ethernet addresses to host info,
47 * which is contained in struct ng_bridge_host's. These structures
48 * tell us on which link the host may be found. A host's entry will
49 * expire after priv->conf.maxStaleness seconds.
51 * This node is optimzed for stable networks, where machines jump
52 * from one port to the other only rarely.
55 #include <sys/param.h>
56 #include <sys/systm.h>
57 #include <sys/kernel.h>
59 #include <sys/malloc.h>
61 #include <sys/errno.h>
62 #include <sys/rwlock.h>
63 #include <sys/syslog.h>
64 #include <sys/socket.h>
65 #include <sys/ctype.h>
66 #include <sys/types.h>
67 #include <sys/counter.h>
70 #include <net/if_var.h>
71 #include <net/ethernet.h>
74 #include <netinet/in.h>
75 #include <netgraph/ng_message.h>
76 #include <netgraph/netgraph.h>
77 #include <netgraph/ng_parse.h>
78 #include <netgraph/ng_bridge.h>
80 #ifdef NG_SEPARATE_MALLOC
81 static MALLOC_DEFINE(M_NETGRAPH_BRIDGE, "netgraph_bridge",
82 "netgraph bridge node");
84 #define M_NETGRAPH_BRIDGE M_NETGRAPH
87 /* Counter based stats */
88 struct ng_bridge_link_kernel_stats {
89 counter_u64_t recvOctets; /* total octets rec'd on link */
90 counter_u64_t recvPackets; /* total pkts rec'd on link */
91 counter_u64_t recvMulticasts; /* multicast pkts rec'd on link */
92 counter_u64_t recvBroadcasts; /* broadcast pkts rec'd on link */
93 counter_u64_t recvUnknown; /* pkts rec'd with unknown dest addr */
94 counter_u64_t recvRunts; /* pkts rec'd less than 14 bytes */
95 counter_u64_t recvInvalid; /* pkts rec'd with bogus source addr */
96 counter_u64_t xmitOctets; /* total octets xmit'd on link */
97 counter_u64_t xmitPackets; /* total pkts xmit'd on link */
98 counter_u64_t xmitMulticasts; /* multicast pkts xmit'd on link */
99 counter_u64_t xmitBroadcasts; /* broadcast pkts xmit'd on link */
100 counter_u64_t loopDrops; /* pkts dropped due to loopback */
101 u_int64_t loopDetects; /* number of loop detections */
102 counter_u64_t memoryFailures; /* times couldn't get mem or mbuf */
105 /* Per-link private data */
106 struct ng_bridge_link {
107 hook_p hook; /* netgraph hook */
108 u_int16_t loopCount; /* loop ignore timer */
109 unsigned int learnMac : 1, /* autolearn macs */
110 sendUnknown : 1;/* send unknown macs out */
111 struct ng_bridge_link_kernel_stats stats; /* link stats */
113 typedef struct ng_bridge_link const *link_cp; /* read only access */
115 /* Per-node private data */
116 struct ng_bridge_private {
117 struct ng_bridge_bucket *tab; /* hash table bucket array */
118 struct ng_bridge_config conf; /* node configuration */
119 node_p node; /* netgraph node */
120 u_int numHosts; /* num entries in table */
121 u_int numBuckets; /* num buckets in table */
122 u_int hashMask; /* numBuckets - 1 */
123 int numLinks; /* num connected links */
124 unsigned int persistent : 1, /* can exist w/o hooks */
125 sendUnknown : 1;/* links receive unknowns by default */
126 struct callout timer; /* one second periodic timer */
128 typedef struct ng_bridge_private *priv_p;
129 typedef struct ng_bridge_private const *priv_cp; /* read only access */
131 /* Information about a host, stored in a hash table entry */
132 struct ng_bridge_host {
133 u_char addr[6]; /* ethernet address */
134 link_p link; /* link where addr can be found */
135 u_int16_t age; /* seconds ago entry was created */
136 u_int16_t staleness; /* seconds ago host last heard from */
137 SLIST_ENTRY(ng_bridge_host) next; /* next entry in bucket */
140 /* Hash table bucket declaration */
141 SLIST_HEAD(ng_bridge_bucket, ng_bridge_host);
143 /* Netgraph node methods */
144 static ng_constructor_t ng_bridge_constructor;
145 static ng_rcvmsg_t ng_bridge_rcvmsg;
146 static ng_shutdown_t ng_bridge_shutdown;
147 static ng_newhook_t ng_bridge_newhook;
148 static ng_rcvdata_t ng_bridge_rcvdata;
149 static ng_disconnect_t ng_bridge_disconnect;
151 /* Other internal functions */
152 static void ng_bridge_free_link(link_p link);
153 static struct ng_bridge_host *ng_bridge_get(priv_cp priv, const u_char *addr);
154 static int ng_bridge_put(priv_p priv, const u_char *addr, link_p link);
155 static void ng_bridge_rehash(priv_p priv);
156 static void ng_bridge_remove_hosts(priv_p priv, link_p link);
157 static void ng_bridge_timeout(node_p node, hook_p hook, void *arg1, int arg2);
158 static const char *ng_bridge_nodename(node_cp node);
160 /* Ethernet broadcast */
161 static const u_char ng_bridge_bcast_addr[ETHER_ADDR_LEN] =
162 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
164 /* Compare Ethernet addresses using 32 and 16 bit words instead of bytewise */
165 #define ETHER_EQUAL(a,b) (((const u_int32_t *)(a))[0] \
166 == ((const u_int32_t *)(b))[0] \
167 && ((const u_int16_t *)(a))[2] \
168 == ((const u_int16_t *)(b))[2])
170 /* Minimum and maximum number of hash buckets. Must be a power of two. */
171 #define MIN_BUCKETS (1 << 5) /* 32 */
172 #define MAX_BUCKETS (1 << 14) /* 16384 */
174 /* Configuration default values */
175 #define DEFAULT_LOOP_TIMEOUT 60
176 #define DEFAULT_MAX_STALENESS (15 * 60) /* same as ARP timeout */
177 #define DEFAULT_MIN_STABLE_AGE 1
179 /******************************************************************
181 ******************************************************************/
184 * How to determine the length of the table returned by NGM_BRIDGE_GET_TABLE
187 ng_bridge_getTableLength(const struct ng_parse_type *type,
188 const u_char *start, const u_char *buf)
190 const struct ng_bridge_host_ary *const hary
191 = (const struct ng_bridge_host_ary *)(buf - sizeof(u_int32_t));
193 return hary->numHosts;
196 /* Parse type for struct ng_bridge_host_ary */
197 static const struct ng_parse_struct_field ng_bridge_host_type_fields[]
198 = NG_BRIDGE_HOST_TYPE_INFO(&ng_parse_enaddr_type);
199 static const struct ng_parse_type ng_bridge_host_type = {
200 &ng_parse_struct_type,
201 &ng_bridge_host_type_fields
203 static const struct ng_parse_array_info ng_bridge_hary_type_info = {
204 &ng_bridge_host_type,
205 ng_bridge_getTableLength
207 static const struct ng_parse_type ng_bridge_hary_type = {
208 &ng_parse_array_type,
209 &ng_bridge_hary_type_info
211 static const struct ng_parse_struct_field ng_bridge_host_ary_type_fields[]
212 = NG_BRIDGE_HOST_ARY_TYPE_INFO(&ng_bridge_hary_type);
213 static const struct ng_parse_type ng_bridge_host_ary_type = {
214 &ng_parse_struct_type,
215 &ng_bridge_host_ary_type_fields
218 /* Parse type for struct ng_bridge_config */
219 static const struct ng_parse_struct_field ng_bridge_config_type_fields[]
220 = NG_BRIDGE_CONFIG_TYPE_INFO;
221 static const struct ng_parse_type ng_bridge_config_type = {
222 &ng_parse_struct_type,
223 &ng_bridge_config_type_fields
226 /* Parse type for struct ng_bridge_link_stat */
227 static const struct ng_parse_struct_field ng_bridge_stats_type_fields[]
228 = NG_BRIDGE_STATS_TYPE_INFO;
229 static const struct ng_parse_type ng_bridge_stats_type = {
230 &ng_parse_struct_type,
231 &ng_bridge_stats_type_fields
233 /* Parse type for struct ng_bridge_move_host */
234 static const struct ng_parse_struct_field ng_bridge_move_host_type_fields[]
235 = NG_BRIDGE_MOVE_HOST_TYPE_INFO(&ng_parse_enaddr_type);
236 static const struct ng_parse_type ng_bridge_move_host_type = {
237 &ng_parse_struct_type,
238 &ng_bridge_move_host_type_fields
241 /* List of commands and how to convert arguments to/from ASCII */
242 static const struct ng_cmdlist ng_bridge_cmdlist[] = {
245 NGM_BRIDGE_SET_CONFIG,
247 &ng_bridge_config_type,
252 NGM_BRIDGE_GET_CONFIG,
255 &ng_bridge_config_type
266 NGM_BRIDGE_GET_STATS,
268 &ng_parse_uint32_type,
269 &ng_bridge_stats_type
273 NGM_BRIDGE_CLR_STATS,
275 &ng_parse_uint32_type,
280 NGM_BRIDGE_GETCLR_STATS,
282 &ng_parse_uint32_type,
283 &ng_bridge_stats_type
287 NGM_BRIDGE_GET_TABLE,
290 &ng_bridge_host_ary_type
294 NGM_BRIDGE_SET_PERSISTENT,
301 NGM_BRIDGE_MOVE_HOST,
303 &ng_bridge_move_host_type,
309 /* Node type descriptor */
310 static struct ng_type ng_bridge_typestruct = {
311 .version = NG_ABI_VERSION,
312 .name = NG_BRIDGE_NODE_TYPE,
313 .constructor = ng_bridge_constructor,
314 .rcvmsg = ng_bridge_rcvmsg,
315 .shutdown = ng_bridge_shutdown,
316 .newhook = ng_bridge_newhook,
317 .rcvdata = ng_bridge_rcvdata,
318 .disconnect = ng_bridge_disconnect,
319 .cmdlist = ng_bridge_cmdlist,
321 NETGRAPH_INIT(bridge, &ng_bridge_typestruct);
323 /******************************************************************
324 NETGRAPH NODE METHODS
325 ******************************************************************/
331 ng_bridge_constructor(node_p node)
335 /* Allocate and initialize private info */
336 priv = malloc(sizeof(*priv), M_NETGRAPH_BRIDGE, M_WAITOK | M_ZERO);
337 ng_callout_init(&priv->timer);
339 /* Allocate and initialize hash table, etc. */
340 priv->tab = malloc(MIN_BUCKETS * sizeof(*priv->tab),
341 M_NETGRAPH_BRIDGE, M_WAITOK | M_ZERO);
342 priv->numBuckets = MIN_BUCKETS;
343 priv->hashMask = MIN_BUCKETS - 1;
344 priv->conf.debugLevel = 1;
345 priv->conf.loopTimeout = DEFAULT_LOOP_TIMEOUT;
346 priv->conf.maxStaleness = DEFAULT_MAX_STALENESS;
347 priv->conf.minStableAge = DEFAULT_MIN_STABLE_AGE;
348 priv->sendUnknown = 1; /* classic bridge */
350 NG_NODE_SET_PRIVATE(node, priv);
353 /* Start timer; timer is always running while node is alive */
354 ng_callout(&priv->timer, node, NULL, hz, ng_bridge_timeout, NULL, 0);
361 * Method for attaching a new hook
364 ng_bridge_newhook(node_p node, hook_p hook, const char *name)
366 const priv_p priv = NG_NODE_PRIVATE(node);
367 char linkName[NG_HOOKSIZ];
370 const char *prefix = NG_BRIDGE_HOOK_LINK_PREFIX;
373 /* Check for a link hook */
374 if (strlen(name) <= strlen(prefix))
375 return (EINVAL); /* Unknown hook name */
377 isUplink = (name[0] == 'u');
379 prefix = NG_BRIDGE_HOOK_UPLINK_PREFIX;
381 /* primitive parsing */
382 linkNum = strtoul(name + strlen(prefix), NULL, 10);
383 /* validation by comparing against the reconstucted name */
384 snprintf(linkName, sizeof(linkName), "%s%u", prefix, linkNum);
385 if (strcmp(linkName, name) != 0)
388 if (linkNum == 0 && isUplink)
391 if(NG_PEER_NODE(hook) == node)
394 link = malloc(sizeof(*link), M_NETGRAPH_BRIDGE, M_NOWAIT | M_ZERO);
398 #define NG_BRIDGE_COUNTER_ALLOC(f) do { \
399 link->stats.f = counter_u64_alloc(M_NOWAIT); \
400 if (link->stats.f == NULL) \
403 NG_BRIDGE_COUNTER_ALLOC(recvOctets);
404 NG_BRIDGE_COUNTER_ALLOC(recvPackets);
405 NG_BRIDGE_COUNTER_ALLOC(recvMulticasts);
406 NG_BRIDGE_COUNTER_ALLOC(recvBroadcasts);
407 NG_BRIDGE_COUNTER_ALLOC(recvUnknown);
408 NG_BRIDGE_COUNTER_ALLOC(recvRunts);
409 NG_BRIDGE_COUNTER_ALLOC(recvInvalid);
410 NG_BRIDGE_COUNTER_ALLOC(xmitOctets);
411 NG_BRIDGE_COUNTER_ALLOC(xmitPackets);
412 NG_BRIDGE_COUNTER_ALLOC(xmitMulticasts);
413 NG_BRIDGE_COUNTER_ALLOC(xmitBroadcasts);
414 NG_BRIDGE_COUNTER_ALLOC(loopDrops);
415 NG_BRIDGE_COUNTER_ALLOC(memoryFailures);
416 #undef NG_BRIDGE_COUNTER_ALLOC
421 link->sendUnknown = 1;
422 if (priv->numLinks == 0) /* if the first link is an uplink */
423 priv->sendUnknown = 0; /* switch to restrictive mode */
426 link->sendUnknown = priv->sendUnknown;
429 NG_HOOK_SET_PRIVATE(hook, link);
434 ng_bridge_free_link(link);
439 * Receive a control message
442 ng_bridge_clear_link_stats(struct ng_bridge_link_kernel_stats *p)
444 counter_u64_zero(p->recvOctets);
445 counter_u64_zero(p->recvPackets);
446 counter_u64_zero(p->recvMulticasts);
447 counter_u64_zero(p->recvBroadcasts);
448 counter_u64_zero(p->recvUnknown);
449 counter_u64_zero(p->recvRunts);
450 counter_u64_zero(p->recvInvalid);
451 counter_u64_zero(p->xmitOctets);
452 counter_u64_zero(p->xmitPackets);
453 counter_u64_zero(p->xmitMulticasts);
454 counter_u64_zero(p->xmitBroadcasts);
455 counter_u64_zero(p->loopDrops);
457 counter_u64_zero(p->memoryFailures);
461 ng_bridge_free_link(link_p link)
463 counter_u64_free(link->stats.recvOctets);
464 counter_u64_free(link->stats.recvPackets);
465 counter_u64_free(link->stats.recvMulticasts);
466 counter_u64_free(link->stats.recvBroadcasts);
467 counter_u64_free(link->stats.recvUnknown);
468 counter_u64_free(link->stats.recvRunts);
469 counter_u64_free(link->stats.recvInvalid);
470 counter_u64_free(link->stats.xmitOctets);
471 counter_u64_free(link->stats.xmitPackets);
472 counter_u64_free(link->stats.xmitMulticasts);
473 counter_u64_free(link->stats.xmitBroadcasts);
474 counter_u64_free(link->stats.loopDrops);
475 counter_u64_free(link->stats.memoryFailures);
476 free(link, M_NETGRAPH_BRIDGE);
480 ng_bridge_reset_link(hook_p hook, void *arg __unused)
482 link_p priv = NG_HOOK_PRIVATE(hook);
485 ng_bridge_clear_link_stats(&priv->stats);
490 ng_bridge_rcvmsg(node_p node, item_p item, hook_p lasthook)
492 const priv_p priv = NG_NODE_PRIVATE(node);
493 struct ng_mesg *resp = NULL;
497 NGI_GET_MSG(item, msg);
498 switch (msg->header.typecookie) {
499 case NGM_BRIDGE_COOKIE:
500 switch (msg->header.cmd) {
501 case NGM_BRIDGE_GET_CONFIG:
503 struct ng_bridge_config *conf;
505 NG_MKRESPONSE(resp, msg,
506 sizeof(struct ng_bridge_config), M_NOWAIT);
511 conf = (struct ng_bridge_config *)resp->data;
512 *conf = priv->conf; /* no sanity checking needed */
515 case NGM_BRIDGE_SET_CONFIG:
517 struct ng_bridge_config *conf;
519 if (msg->header.arglen
520 != sizeof(struct ng_bridge_config)) {
524 conf = (struct ng_bridge_config *)msg->data;
528 case NGM_BRIDGE_RESET:
530 /* Flush all entries in the hash table */
531 ng_bridge_remove_hosts(priv, NULL);
533 /* Reset all loop detection counters and stats */
534 NG_NODE_FOREACH_HOOK(node, ng_bridge_reset_link, NULL);
537 case NGM_BRIDGE_GET_STATS:
538 case NGM_BRIDGE_CLR_STATS:
539 case NGM_BRIDGE_GETCLR_STATS:
543 char linkName[NG_HOOKSIZ];
546 /* Get link number */
547 if (msg->header.arglen != sizeof(u_int32_t)) {
551 linkNum = *((int32_t *)msg->data);
553 snprintf(linkName, sizeof(linkName),
554 "%s%u", NG_BRIDGE_HOOK_UPLINK_PREFIX, -linkNum);
556 snprintf(linkName, sizeof(linkName),
557 "%s%u", NG_BRIDGE_HOOK_LINK_PREFIX, linkNum);
559 if ((hook = ng_findhook(node, linkName)) == NULL) {
563 link = NG_HOOK_PRIVATE(hook);
565 /* Get/clear stats */
566 if (msg->header.cmd != NGM_BRIDGE_CLR_STATS) {
567 struct ng_bridge_link_stats *rs;
569 NG_MKRESPONSE(resp, msg,
570 sizeof(link->stats), M_NOWAIT);
575 rs = (struct ng_bridge_link_stats *)resp->data;
576 #define FETCH(x) rs->x = counter_u64_fetch(link->stats.x)
579 FETCH(recvMulticasts);
580 FETCH(recvBroadcasts);
586 FETCH(xmitMulticasts);
587 FETCH(xmitBroadcasts);
589 rs->loopDetects = link->stats.loopDetects;
590 FETCH(memoryFailures);
593 if (msg->header.cmd != NGM_BRIDGE_GET_STATS)
594 ng_bridge_clear_link_stats(&link->stats);
597 case NGM_BRIDGE_GET_TABLE:
599 struct ng_bridge_host_ary *ary;
600 struct ng_bridge_host *host;
603 NG_MKRESPONSE(resp, msg, sizeof(*ary)
604 + (priv->numHosts * sizeof(*ary->hosts)), M_NOWAIT);
609 ary = (struct ng_bridge_host_ary *)resp->data;
610 ary->numHosts = priv->numHosts;
611 for (bucket = 0; bucket < priv->numBuckets; bucket++) {
612 SLIST_FOREACH(host, &priv->tab[bucket], next) {
613 memcpy(ary->hosts[i].addr,
615 sizeof(ary->hosts[i].addr));
616 ary->hosts[i].age = host->age;
617 ary->hosts[i].staleness = host->staleness;
618 strncpy(ary->hosts[i].hook,
619 NG_HOOK_NAME(host->link->hook),
620 sizeof(ary->hosts[i].hook));
626 case NGM_BRIDGE_SET_PERSISTENT:
628 priv->persistent = 1;
631 case NGM_BRIDGE_MOVE_HOST:
633 struct ng_bridge_move_host *mh;
636 if (msg->header.arglen < sizeof(*mh)) {
640 mh = (struct ng_bridge_move_host *)msg->data;
641 hook = (mh->hook[0] == 0)
643 : ng_findhook(node, mh->hook);
648 error = ng_bridge_put(priv, mh->addr, NG_HOOK_PRIVATE(hook));
662 NG_RESPOND_MSG(error, node, item, resp);
668 * Receive data on a hook
670 struct ng_bridge_send_ctx {
671 link_p foundFirst, incoming;
677 * Update stats and send out
680 ng_bridge_send_data(link_cp dst, int manycast, struct mbuf *m, item_p item) {
682 size_t len = m->m_pkthdr.len;
685 NG_FWD_NEW_DATA(error, item, dst->hook, m);
687 NG_SEND_DATA_ONLY(error, dst->hook, m);
691 counter_u64_add(dst->stats.memoryFailures, 1);
692 /* The packet is still ours */
700 counter_u64_add(dst->stats.xmitPackets, 1);
701 counter_u64_add(dst->stats.xmitOctets, len);
703 default: /* unknown unicast */
705 case 1: /* multicast */
706 counter_u64_add(dst->stats.xmitMulticasts, 1);
708 case 2: /* broadcast */
709 counter_u64_add(dst->stats.xmitBroadcasts, 1);
716 * Loop body for sending to multiple destinations
717 * return 0 to stop looping
720 ng_bridge_send_ctx(hook_p dst, void *arg)
722 struct ng_bridge_send_ctx *ctx = arg;
723 link_p destLink = NG_HOOK_PRIVATE(dst);
724 struct mbuf *m2 = NULL;
727 /* Skip incoming link */
728 if (destLink == ctx->incoming) {
732 /* Skip sending unknowns to undesired links */
733 if (!ctx->manycast && !destLink->sendUnknown)
736 if (ctx->foundFirst == NULL) {
738 * This is the first usable link we have found.
739 * Reserve it for the originals.
740 * If we never find another we save a copy.
742 ctx->foundFirst = destLink;
747 * It's usable link but not the reserved (first) one.
748 * Copy mbuf info for sending.
750 m2 = m_dup(ctx->m, M_NOWAIT);
752 counter_u64_add(ctx->incoming->stats.memoryFailures, 1);
753 ctx->error = ENOBUFS;
754 return (0); /* abort loop, do not try again and again */
758 error = ng_bridge_send_data(destLink, ctx->manycast, m2, NULL);
765 ng_bridge_rcvdata(hook_p hook, item_p item)
767 const node_p node = NG_HOOK_NODE(hook);
768 const priv_p priv = NG_NODE_PRIVATE(node);
769 struct ng_bridge_host *host;
770 struct ether_header *eh;
771 struct ng_bridge_send_ctx ctx = { 0 };
773 NGI_GET_M(item, ctx.m);
775 ctx.incoming = NG_HOOK_PRIVATE(hook);
776 /* Sanity check packet and pull up header */
777 if (ctx.m->m_pkthdr.len < ETHER_HDR_LEN) {
778 counter_u64_add(ctx.incoming->stats.recvRunts, 1);
783 if (ctx.m->m_len < ETHER_HDR_LEN && !(ctx.m = m_pullup(ctx.m, ETHER_HDR_LEN))) {
784 counter_u64_add(ctx.incoming->stats.memoryFailures, 1);
788 eh = mtod(ctx.m, struct ether_header *);
789 if ((eh->ether_shost[0] & 1) != 0) {
790 counter_u64_add(ctx.incoming->stats.recvInvalid, 1);
796 /* Is link disabled due to a loopback condition? */
797 if (ctx.incoming->loopCount != 0) {
798 counter_u64_add(ctx.incoming->stats.loopDrops, 1);
805 counter_u64_add(ctx.incoming->stats.recvPackets, 1);
806 counter_u64_add(ctx.incoming->stats.recvOctets, ctx.m->m_pkthdr.len);
807 if ((ctx.manycast = (eh->ether_dhost[0] & 1)) != 0) {
808 if (ETHER_EQUAL(eh->ether_dhost, ng_bridge_bcast_addr)) {
809 counter_u64_add(ctx.incoming->stats.recvBroadcasts, 1);
812 counter_u64_add(ctx.incoming->stats.recvMulticasts, 1);
815 /* Look up packet's source Ethernet address in hashtable */
816 if ((host = ng_bridge_get(priv, eh->ether_shost)) != NULL)
817 /* Update time since last heard from this host.
818 * This is safe without locking, because it's
819 * the only operation during shared access.
821 if (__predict_false(host->staleness > 0))
824 if ((host == NULL && ctx.incoming->learnMac) ||
825 (host != NULL && host->link != ctx.incoming)) {
827 struct ng_bridge_move_host *mh;
830 NG_MKMESSAGE(msg, NGM_BRIDGE_COOKIE, NGM_BRIDGE_MOVE_HOST,
831 sizeof(*mh), M_NOWAIT);
833 counter_u64_add(ctx.incoming->stats.memoryFailures, 1);
838 mh = (struct ng_bridge_move_host *)msg->data;
839 strncpy(mh->hook, NG_HOOK_NAME(ctx.incoming->hook),
841 memcpy(mh->addr, eh->ether_shost, sizeof(mh->addr));
842 NG_SEND_MSG_ID(error, node, msg, NG_NODE_ID(node),
845 counter_u64_add(ctx.incoming->stats.memoryFailures, 1);
848 if (host != NULL && host->link != ctx.incoming) {
849 if (host->age < priv->conf.minStableAge) {
850 /* Drop packet on instable links */
851 counter_u64_add(ctx.incoming->stats.loopDrops, 1);
859 * If unicast and destination host known, deliver to host's link,
860 * unless it is the same link as the packet came in on.
863 /* Determine packet destination link */
864 if ((host = ng_bridge_get(priv, eh->ether_dhost)) != NULL) {
865 link_p destLink = host->link;
867 /* If destination same as incoming link, do nothing */
868 if (destLink == ctx.incoming) {
874 /* Deliver packet out the destination link */
875 return (ng_bridge_send_data(destLink, ctx.manycast, ctx.m, item));
878 /* Destination host is not known */
879 counter_u64_add(ctx.incoming->stats.recvUnknown, 1);
882 /* Distribute unknown, multicast, broadcast pkts to all other links */
883 NG_NODE_FOREACH_HOOK(node, ng_bridge_send_ctx, &ctx);
885 /* Finally send out on the first link found */
886 if (ctx.foundFirst != NULL) {
887 int error = ng_bridge_send_data(ctx.foundFirst, ctx.manycast, ctx.m, item);
890 } else { /* nothing to send at all */
902 ng_bridge_shutdown(node_p node)
904 const priv_p priv = NG_NODE_PRIVATE(node);
907 * Shut down everything including the timer. Even if the
908 * callout has already been dequeued and is about to be
909 * run, ng_bridge_timeout() won't be fired as the node
910 * is already marked NGF_INVALID, so we're safe to free
913 KASSERT(priv->numLinks == 0 && priv->numHosts == 0,
914 ("%s: numLinks=%d numHosts=%d",
915 __func__, priv->numLinks, priv->numHosts));
916 ng_uncallout(&priv->timer, node);
917 NG_NODE_SET_PRIVATE(node, NULL);
919 free(priv->tab, M_NETGRAPH_BRIDGE);
920 free(priv, M_NETGRAPH_BRIDGE);
925 * Hook disconnection.
928 ng_bridge_disconnect(hook_p hook)
930 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
931 link_p link = NG_HOOK_PRIVATE(hook);
933 /* Remove all hosts associated with this link */
934 ng_bridge_remove_hosts(priv, link);
936 /* Free associated link information */
937 ng_bridge_free_link(link);
940 /* If no more hooks, go away */
941 if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
942 && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook)))
943 && !priv->persistent) {
944 ng_rmnode_self(NG_HOOK_NODE(hook));
949 /******************************************************************
951 ******************************************************************/
956 #define HASH(addr,mask) ( (((const u_int16_t *)(addr))[0] \
957 ^ ((const u_int16_t *)(addr))[1] \
958 ^ ((const u_int16_t *)(addr))[2]) & (mask) )
961 * Find a host entry in the table.
963 static struct ng_bridge_host *
964 ng_bridge_get(priv_cp priv, const u_char *addr)
966 const int bucket = HASH(addr, priv->hashMask);
967 struct ng_bridge_host *host;
969 SLIST_FOREACH(host, &priv->tab[bucket], next) {
970 if (ETHER_EQUAL(host->addr, addr))
977 * Add a host entry to the table. If it already exists, move it
978 * to the new link. Returns 0 on success.
981 ng_bridge_put(priv_p priv, const u_char *addr, link_p link)
983 const int bucket = HASH(addr, priv->hashMask);
984 struct ng_bridge_host *host;
986 if ((host = ng_bridge_get(priv, addr)) != NULL) {
987 /* Host already on the correct link? */
988 if (host->link == link)
991 /* Move old host over to new link */
992 if (host->age >= priv->conf.minStableAge) {
998 * If the host was recently moved to the old link and
999 * it's now jumping to a new link, declare a loopback
1002 if (priv->conf.debugLevel >= 2)
1003 log(LOG_WARNING, "ng_bridge: %s:"
1004 " loopback detected on %s\n",
1005 ng_bridge_nodename(priv->node),
1006 NG_HOOK_NAME(link->hook));
1008 /* Mark link as linka non grata */
1009 link->loopCount = priv->conf.loopTimeout;
1010 link->stats.loopDetects++;
1012 /* Forget all hosts on this link */
1013 ng_bridge_remove_hosts(priv, link);
1017 /* Allocate and initialize new hashtable entry */
1018 host = malloc(sizeof(*host), M_NETGRAPH_BRIDGE, M_NOWAIT);
1021 bcopy(addr, host->addr, ETHER_ADDR_LEN);
1023 host->staleness = 0;
1026 /* Add new element to hash bucket */
1027 SLIST_INSERT_HEAD(&priv->tab[bucket], host, next);
1030 /* Resize table if necessary */
1031 ng_bridge_rehash(priv);
1036 * Resize the hash table. We try to maintain the number of buckets
1037 * such that the load factor is in the range 0.25 to 1.0.
1039 * If we can't get the new memory then we silently fail. This is OK
1040 * because things will still work and we'll try again soon anyway.
1043 ng_bridge_rehash(priv_p priv)
1045 struct ng_bridge_bucket *newTab;
1046 int oldBucket, newBucket;
1050 /* Is table too full or too empty? */
1051 if (priv->numHosts > priv->numBuckets
1052 && (priv->numBuckets << 1) <= MAX_BUCKETS)
1053 newNumBuckets = priv->numBuckets << 1;
1054 else if (priv->numHosts < (priv->numBuckets >> 2)
1055 && (priv->numBuckets >> 2) >= MIN_BUCKETS)
1056 newNumBuckets = priv->numBuckets >> 2;
1059 newMask = newNumBuckets - 1;
1061 /* Allocate and initialize new table */
1062 newTab = malloc(newNumBuckets * sizeof(*newTab),
1063 M_NETGRAPH_BRIDGE, M_NOWAIT | M_ZERO);
1067 /* Move all entries from old table to new table */
1068 for (oldBucket = 0; oldBucket < priv->numBuckets; oldBucket++) {
1069 struct ng_bridge_bucket *const oldList = &priv->tab[oldBucket];
1071 while (!SLIST_EMPTY(oldList)) {
1072 struct ng_bridge_host *const host
1073 = SLIST_FIRST(oldList);
1075 SLIST_REMOVE_HEAD(oldList, next);
1076 newBucket = HASH(host->addr, newMask);
1077 SLIST_INSERT_HEAD(&newTab[newBucket], host, next);
1081 /* Replace old table with new one */
1082 if (priv->conf.debugLevel >= 3) {
1083 log(LOG_INFO, "ng_bridge: %s: table size %d -> %d\n",
1084 ng_bridge_nodename(priv->node),
1085 priv->numBuckets, newNumBuckets);
1087 free(priv->tab, M_NETGRAPH_BRIDGE);
1088 priv->numBuckets = newNumBuckets;
1089 priv->hashMask = newMask;
1094 /******************************************************************
1096 ******************************************************************/
1099 * Remove all hosts associated with a specific link from the hashtable.
1100 * If linkNum == -1, then remove all hosts in the table.
1103 ng_bridge_remove_hosts(priv_p priv, link_p link)
1107 for (bucket = 0; bucket < priv->numBuckets; bucket++) {
1108 struct ng_bridge_host **hptr = &SLIST_FIRST(&priv->tab[bucket]);
1110 while (*hptr != NULL) {
1111 struct ng_bridge_host *const host = *hptr;
1113 if (link == NULL || host->link == link) {
1114 *hptr = SLIST_NEXT(host, next);
1115 free(host, M_NETGRAPH_BRIDGE);
1118 hptr = &SLIST_NEXT(host, next);
1124 * Handle our once-per-second timeout event. We do two things:
1125 * we decrement link->loopCount for those links being muted due to
1126 * a detected loopback condition, and we remove any hosts from
1127 * the hashtable whom we haven't heard from in a long while.
1130 ng_bridge_unmute(hook_p hook, void *arg)
1132 link_p link = NG_HOOK_PRIVATE(hook);
1133 node_p node = NG_HOOK_NODE(hook);
1134 priv_p priv = NG_NODE_PRIVATE(node);
1137 if (link->loopCount != 0) {
1139 if (link->loopCount == 0 && priv->conf.debugLevel >= 2) {
1140 log(LOG_INFO, "ng_bridge: %s:"
1141 " restoring looped back %s\n",
1142 ng_bridge_nodename(node), NG_HOOK_NAME(hook));
1150 ng_bridge_timeout(node_p node, hook_p hook, void *arg1, int arg2)
1152 const priv_p priv = NG_NODE_PRIVATE(node);
1156 /* Update host time counters and remove stale entries */
1157 for (bucket = 0; bucket < priv->numBuckets; bucket++) {
1158 struct ng_bridge_host **hptr = &SLIST_FIRST(&priv->tab[bucket]);
1160 while (*hptr != NULL) {
1161 struct ng_bridge_host *const host = *hptr;
1163 /* Remove hosts we haven't heard from in a while */
1164 if (++host->staleness >= priv->conf.maxStaleness) {
1165 *hptr = SLIST_NEXT(host, next);
1166 free(host, M_NETGRAPH_BRIDGE);
1169 if (host->age < 0xffff)
1171 hptr = &SLIST_NEXT(host, next);
1176 KASSERT(priv->numHosts == counter,
1177 ("%s: hosts: %d != %d", __func__, priv->numHosts, counter));
1179 /* Decrease table size if necessary */
1180 ng_bridge_rehash(priv);
1182 /* Decrease loop counter on muted looped back links */
1184 NG_NODE_FOREACH_HOOK(node, ng_bridge_unmute, &counter);
1185 KASSERT(priv->numLinks == counter,
1186 ("%s: links: %d != %d", __func__, priv->numLinks, counter));
1188 /* Register a new timeout, keeping the existing node reference */
1189 ng_callout(&priv->timer, node, NULL, hz, ng_bridge_timeout, NULL, 0);
1193 * Return node's "name", even if it doesn't have one.
1196 ng_bridge_nodename(node_cp node)
1198 static char name[NG_NODESIZ];
1200 if (NG_NODE_HAS_NAME(node))
1201 snprintf(name, sizeof(name), "%s", NG_NODE_NAME(node));
1203 snprintf(name, sizeof(name), "[%x]", ng_node2ID(node));