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_one2many(4) netgraph node type
46 * Packets received on the "one" hook are sent out each of the
47 * "many" hooks accoring to an algorithm. Packets received on any
48 * "many" hook are always delivered to the "one" hook.
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/kernel.h>
54 #include <sys/malloc.h>
55 #include <sys/ctype.h>
57 #include <sys/errno.h>
59 #include <netgraph/ng_message.h>
60 #include <netgraph/netgraph.h>
61 #include <netgraph/ng_parse.h>
62 #include <netgraph/ng_one2many.h>
64 /* Per-link private data */
65 struct ng_one2many_link {
66 hook_p hook; /* netgraph hook */
67 struct ng_one2many_link_stats stats; /* link stats */
70 /* Per-node private data */
71 struct ng_one2many_private {
72 node_p node; /* link to node */
73 struct ng_one2many_config conf; /* node configuration */
74 struct ng_one2many_link one; /* "one" hook */
75 struct ng_one2many_link many[NG_ONE2MANY_MAX_LINKS];
76 u_int16_t nextMany; /* next round-robin */
77 u_int16_t numActiveMany; /* # active "many" */
78 u_int16_t activeMany[NG_ONE2MANY_MAX_LINKS];
80 typedef struct ng_one2many_private *priv_p;
82 /* Netgraph node methods */
83 static ng_constructor_t ng_one2many_constructor;
84 static ng_rcvmsg_t ng_one2many_rcvmsg;
85 static ng_shutdown_t ng_one2many_shutdown;
86 static ng_newhook_t ng_one2many_newhook;
87 static ng_rcvdata_t ng_one2many_rcvdata;
88 static ng_disconnect_t ng_one2many_disconnect;
91 static void ng_one2many_update_many(priv_p priv);
92 static void ng_one2many_notify(priv_p priv, uint32_t cmd);
94 /******************************************************************
96 ******************************************************************/
98 /* Parse type for struct ng_one2many_config */
99 static const struct ng_parse_fixedarray_info
100 ng_one2many_enableLinks_array_type_info = {
101 &ng_parse_uint8_type,
102 NG_ONE2MANY_MAX_LINKS
104 static const struct ng_parse_type ng_one2many_enableLinks_array_type = {
105 &ng_parse_fixedarray_type,
106 &ng_one2many_enableLinks_array_type_info,
108 static const struct ng_parse_struct_field ng_one2many_config_type_fields[]
109 = NG_ONE2MANY_CONFIG_TYPE_INFO(&ng_one2many_enableLinks_array_type);
110 static const struct ng_parse_type ng_one2many_config_type = {
111 &ng_parse_struct_type,
112 &ng_one2many_config_type_fields
115 /* Parse type for struct ng_one2many_link_stats */
116 static const struct ng_parse_struct_field ng_one2many_link_stats_type_fields[]
117 = NG_ONE2MANY_LINK_STATS_TYPE_INFO;
118 static const struct ng_parse_type ng_one2many_link_stats_type = {
119 &ng_parse_struct_type,
120 &ng_one2many_link_stats_type_fields
123 /* List of commands and how to convert arguments to/from ASCII */
124 static const struct ng_cmdlist ng_one2many_cmdlist[] = {
127 NGM_ONE2MANY_SET_CONFIG,
129 &ng_one2many_config_type,
134 NGM_ONE2MANY_GET_CONFIG,
137 &ng_one2many_config_type
141 NGM_ONE2MANY_GET_STATS,
143 &ng_parse_int32_type,
144 &ng_one2many_link_stats_type
148 NGM_ONE2MANY_CLR_STATS,
150 &ng_parse_int32_type,
155 NGM_ONE2MANY_GETCLR_STATS,
157 &ng_parse_int32_type,
158 &ng_one2many_link_stats_type
163 /* Node type descriptor */
164 static struct ng_type ng_one2many_typestruct = {
165 .version = NG_ABI_VERSION,
166 .name = NG_ONE2MANY_NODE_TYPE,
167 .constructor = ng_one2many_constructor,
168 .rcvmsg = ng_one2many_rcvmsg,
169 .shutdown = ng_one2many_shutdown,
170 .newhook = ng_one2many_newhook,
171 .rcvdata = ng_one2many_rcvdata,
172 .disconnect = ng_one2many_disconnect,
173 .cmdlist = ng_one2many_cmdlist,
175 NETGRAPH_INIT(one2many, &ng_one2many_typestruct);
177 /******************************************************************
178 NETGRAPH NODE METHODS
179 ******************************************************************/
185 ng_one2many_constructor(node_p node)
189 /* Allocate and initialize private info */
190 priv = malloc(sizeof(*priv), M_NETGRAPH, M_WAITOK | M_ZERO);
191 priv->conf.xmitAlg = NG_ONE2MANY_XMIT_ROUNDROBIN;
192 priv->conf.failAlg = NG_ONE2MANY_FAIL_MANUAL;
194 /* cross reference */
195 NG_NODE_SET_PRIVATE(node, priv);
203 * Method for attaching a new hook
206 ng_one2many_newhook(node_p node, hook_p hook, const char *name)
208 const priv_p priv = NG_NODE_PRIVATE(node);
209 struct ng_one2many_link *link;
214 if (strncmp(name, NG_ONE2MANY_HOOK_MANY_PREFIX,
215 strlen(NG_ONE2MANY_HOOK_MANY_PREFIX)) == 0) {
219 cp = name + strlen(NG_ONE2MANY_HOOK_MANY_PREFIX);
220 if (!isdigit(*cp) || (cp[0] == '0' && cp[1] != '\0'))
222 i = strtoul(cp, &eptr, 10);
223 if (*eptr != '\0' || i < 0 || i >= NG_ONE2MANY_MAX_LINKS)
226 link = &priv->many[linkNum];
227 } else if (strcmp(name, NG_ONE2MANY_HOOK_ONE) == 0) {
228 linkNum = NG_ONE2MANY_ONE_LINKNUM;
233 /* Is hook already connected? (should never happen) */
234 if (link->hook != NULL)
237 /* Setup private info for this link */
238 NG_HOOK_SET_PRIVATE(hook, (void *)(intptr_t)linkNum);
240 bzero(&link->stats, sizeof(link->stats));
241 if (linkNum != NG_ONE2MANY_ONE_LINKNUM) {
242 priv->conf.enabledLinks[linkNum] = 1; /* auto-enable link */
243 ng_one2many_update_many(priv);
251 * Receive a control message
254 ng_one2many_rcvmsg(node_p node, item_p item, hook_p lasthook)
256 const priv_p priv = NG_NODE_PRIVATE(node);
257 struct ng_mesg *resp = NULL;
261 NGI_GET_MSG(item, msg);
262 switch (msg->header.typecookie) {
263 case NGM_ONE2MANY_COOKIE:
264 switch (msg->header.cmd) {
265 case NGM_ONE2MANY_SET_CONFIG:
267 struct ng_one2many_config *conf;
270 /* Check that new configuration is valid */
271 if (msg->header.arglen != sizeof(*conf)) {
275 conf = (struct ng_one2many_config *)msg->data;
276 switch (conf->xmitAlg) {
277 case NG_ONE2MANY_XMIT_ROUNDROBIN:
278 case NG_ONE2MANY_XMIT_ALL:
279 case NG_ONE2MANY_XMIT_FAILOVER:
285 switch (conf->failAlg) {
286 case NG_ONE2MANY_FAIL_MANUAL:
287 case NG_ONE2MANY_FAIL_NOTIFY:
296 /* Normalized many link enabled bits */
297 for (i = 0; i < NG_ONE2MANY_MAX_LINKS; i++)
298 conf->enabledLinks[i] = !!conf->enabledLinks[i];
300 /* Copy config and reset */
301 bcopy(conf, &priv->conf, sizeof(*conf));
302 ng_one2many_update_many(priv);
305 case NGM_ONE2MANY_GET_CONFIG:
307 struct ng_one2many_config *conf;
309 NG_MKRESPONSE(resp, msg, sizeof(*conf), M_NOWAIT);
314 conf = (struct ng_one2many_config *)resp->data;
315 bcopy(&priv->conf, conf, sizeof(priv->conf));
318 case NGM_ONE2MANY_GET_STATS:
319 case NGM_ONE2MANY_CLR_STATS:
320 case NGM_ONE2MANY_GETCLR_STATS:
322 struct ng_one2many_link *link;
326 if (msg->header.arglen != sizeof(int32_t)) {
330 linkNum = *((int32_t *)msg->data);
331 if (linkNum == NG_ONE2MANY_ONE_LINKNUM)
333 else if (linkNum >= 0
334 && linkNum < NG_ONE2MANY_MAX_LINKS) {
335 link = &priv->many[linkNum];
341 /* Get/clear stats */
342 if (msg->header.cmd != NGM_ONE2MANY_CLR_STATS) {
343 NG_MKRESPONSE(resp, msg,
344 sizeof(link->stats), M_NOWAIT);
350 resp->data, sizeof(link->stats));
352 if (msg->header.cmd != NGM_ONE2MANY_GET_STATS)
353 bzero(&link->stats, sizeof(link->stats));
362 * One of our downstreams notifies us of link change. If we are
363 * configured to listen to these message, then we remove/add
364 * this hook from array of active hooks.
366 case NGM_FLOW_COOKIE:
370 if (priv->conf.failAlg != NG_ONE2MANY_FAIL_NOTIFY)
373 if (lasthook == NULL)
376 linkNum = (intptr_t)NG_HOOK_PRIVATE(lasthook);
377 if (linkNum == NG_ONE2MANY_ONE_LINKNUM)
380 KASSERT((linkNum >= 0 && linkNum < NG_ONE2MANY_MAX_LINKS),
381 ("%s: linkNum=%d", __func__, linkNum));
383 switch (msg->header.cmd) {
385 priv->conf.enabledLinks[linkNum] = 1;
386 ng_one2many_update_many(priv);
388 case NGM_LINK_IS_DOWN:
389 priv->conf.enabledLinks[linkNum] = 0;
390 ng_one2many_update_many(priv);
403 NG_RESPOND_MSG(error, node, item, resp);
409 * Receive data on a hook
412 ng_one2many_rcvdata(hook_p hook, item_p item)
414 const node_p node = NG_HOOK_NODE(hook);
415 const priv_p priv = NG_NODE_PRIVATE(node);
416 struct ng_one2many_link *src;
417 struct ng_one2many_link *dst = NULL;
423 m = NGI_M(item); /* just peaking, mbuf still owned by item */
424 /* Get link number */
425 linkNum = (intptr_t)NG_HOOK_PRIVATE(hook);
426 KASSERT(linkNum == NG_ONE2MANY_ONE_LINKNUM
427 || (linkNum >= 0 && linkNum < NG_ONE2MANY_MAX_LINKS),
428 ("%s: linkNum=%d", __func__, linkNum));
430 /* Figure out source link */
431 src = (linkNum == NG_ONE2MANY_ONE_LINKNUM) ?
432 &priv->one : &priv->many[linkNum];
433 KASSERT(src->hook != NULL, ("%s: no src%d", __func__, linkNum));
435 /* Update receive stats */
436 src->stats.recvPackets++;
437 src->stats.recvOctets += m->m_pkthdr.len;
439 /* Figure out destination link */
440 if (linkNum == NG_ONE2MANY_ONE_LINKNUM) {
441 if (priv->numActiveMany == 0) {
445 switch(priv->conf.xmitAlg) {
446 case NG_ONE2MANY_XMIT_ROUNDROBIN:
447 dst = &priv->many[priv->activeMany[priv->nextMany]];
448 priv->nextMany = (priv->nextMany + 1) % priv->numActiveMany;
450 case NG_ONE2MANY_XMIT_ALL:
451 /* no need to copy data for the 1st one */
452 dst = &priv->many[priv->activeMany[0]];
454 /* make copies of data and send for all links
455 * except the first one, which we'll do last
457 for (i = 1; i < priv->numActiveMany; i++) {
459 struct ng_one2many_link *mdst;
461 mdst = &priv->many[priv->activeMany[i]];
462 m2 = m_dup(m, M_DONTWAIT); /* XXX m_copypacket() */
464 mdst->stats.memoryFailures++;
469 /* Update transmit stats */
470 mdst->stats.xmitPackets++;
471 mdst->stats.xmitOctets += m->m_pkthdr.len;
472 NG_SEND_DATA_ONLY(error, mdst->hook, m2);
475 case NG_ONE2MANY_XMIT_FAILOVER:
476 dst = &priv->many[priv->activeMany[0]];
480 panic("%s: invalid xmitAlg", __func__);
487 /* Update transmit stats */
488 dst->stats.xmitPackets++;
489 dst->stats.xmitOctets += m->m_pkthdr.len;
492 NG_FWD_ITEM_HOOK(error, item, dst->hook);
500 ng_one2many_shutdown(node_p node)
502 const priv_p priv = NG_NODE_PRIVATE(node);
504 KASSERT(priv->numActiveMany == 0,
505 ("%s: numActiveMany=%d", __func__, priv->numActiveMany));
506 free(priv, M_NETGRAPH);
507 NG_NODE_SET_PRIVATE(node, NULL);
513 * Hook disconnection.
516 ng_one2many_disconnect(hook_p hook)
518 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
521 /* Get link number */
522 linkNum = (intptr_t)NG_HOOK_PRIVATE(hook);
523 KASSERT(linkNum == NG_ONE2MANY_ONE_LINKNUM
524 || (linkNum >= 0 && linkNum < NG_ONE2MANY_MAX_LINKS),
525 ("%s: linkNum=%d", __func__, linkNum));
528 if (linkNum == NG_ONE2MANY_ONE_LINKNUM)
529 priv->one.hook = NULL;
531 priv->many[linkNum].hook = NULL;
532 priv->conf.enabledLinks[linkNum] = 0;
533 ng_one2many_update_many(priv);
536 /* If no hooks left, go away */
537 if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
538 && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook))))
539 ng_rmnode_self(NG_HOOK_NODE(hook));
543 /******************************************************************
545 ******************************************************************/
548 * Update internal state after the addition or removal of a "many" link
551 ng_one2many_update_many(priv_p priv)
553 uint16_t saveActive = priv->numActiveMany;
556 /* Update list of which "many" links are up */
557 priv->numActiveMany = 0;
558 for (linkNum = 0; linkNum < NG_ONE2MANY_MAX_LINKS; linkNum++) {
559 switch (priv->conf.failAlg) {
560 case NG_ONE2MANY_FAIL_MANUAL:
561 case NG_ONE2MANY_FAIL_NOTIFY:
562 if (priv->many[linkNum].hook != NULL
563 && priv->conf.enabledLinks[linkNum]) {
564 priv->activeMany[priv->numActiveMany] = linkNum;
565 priv->numActiveMany++;
570 panic("%s: invalid failAlg", __func__);
575 if (priv->numActiveMany == 0 && saveActive > 0)
576 ng_one2many_notify(priv, NGM_LINK_IS_DOWN);
578 if (saveActive == 0 && priv->numActiveMany > 0)
579 ng_one2many_notify(priv, NGM_LINK_IS_UP);
581 /* Update transmit algorithm state */
582 switch (priv->conf.xmitAlg) {
583 case NG_ONE2MANY_XMIT_ROUNDROBIN:
584 if (priv->numActiveMany > 0)
585 priv->nextMany %= priv->numActiveMany;
587 case NG_ONE2MANY_XMIT_ALL:
588 case NG_ONE2MANY_XMIT_FAILOVER:
592 panic("%s: invalid xmitAlg", __func__);
598 * Notify upstream if we are out of links, or we have at least one link.
601 ng_one2many_notify(priv_p priv, uint32_t cmd)
606 if (priv->one.hook == NULL)
609 NG_MKMESSAGE(msg, NGM_FLOW_COOKIE, cmd, 0, M_NOWAIT);
611 NG_SEND_MSG_HOOK(dummy_error, priv->node, msg, priv->one.hook, 0);