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>
42 * ng_one2many(4) netgraph node type
44 * Packets received on the "one" hook are sent out each of the
45 * "many" hooks according to an algorithm. Packets received on any
46 * "many" hook are always delivered to the "one" hook.
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/kernel.h>
52 #include <sys/malloc.h>
53 #include <sys/ctype.h>
55 #include <sys/errno.h>
57 #include <netgraph/ng_message.h>
58 #include <netgraph/netgraph.h>
59 #include <netgraph/ng_parse.h>
60 #include <netgraph/ng_one2many.h>
62 /* Per-link private data */
63 struct ng_one2many_link {
64 hook_p hook; /* netgraph hook */
65 struct ng_one2many_link_stats stats; /* link stats */
68 /* Per-node private data */
69 struct ng_one2many_private {
70 node_p node; /* link to node */
71 struct ng_one2many_config conf; /* node configuration */
72 struct ng_one2many_link one; /* "one" hook */
73 struct ng_one2many_link many[NG_ONE2MANY_MAX_LINKS];
74 u_int16_t nextMany; /* next round-robin */
75 u_int16_t numActiveMany; /* # active "many" */
76 u_int16_t activeMany[NG_ONE2MANY_MAX_LINKS];
78 typedef struct ng_one2many_private *priv_p;
80 /* Netgraph node methods */
81 static ng_constructor_t ng_one2many_constructor;
82 static ng_rcvmsg_t ng_one2many_rcvmsg;
83 static ng_shutdown_t ng_one2many_shutdown;
84 static ng_newhook_t ng_one2many_newhook;
85 static ng_rcvdata_t ng_one2many_rcvdata;
86 static ng_disconnect_t ng_one2many_disconnect;
89 static void ng_one2many_update_many(priv_p priv);
90 static void ng_one2many_notify(priv_p priv, uint32_t cmd);
92 /******************************************************************
94 ******************************************************************/
96 /* Parse type for struct ng_one2many_config */
97 static const struct ng_parse_fixedarray_info
98 ng_one2many_enableLinks_array_type_info = {
100 NG_ONE2MANY_MAX_LINKS
102 static const struct ng_parse_type ng_one2many_enableLinks_array_type = {
103 &ng_parse_fixedarray_type,
104 &ng_one2many_enableLinks_array_type_info,
106 static const struct ng_parse_struct_field ng_one2many_config_type_fields[]
107 = NG_ONE2MANY_CONFIG_TYPE_INFO(&ng_one2many_enableLinks_array_type);
108 static const struct ng_parse_type ng_one2many_config_type = {
109 &ng_parse_struct_type,
110 &ng_one2many_config_type_fields
113 /* Parse type for struct ng_one2many_link_stats */
114 static const struct ng_parse_struct_field ng_one2many_link_stats_type_fields[]
115 = NG_ONE2MANY_LINK_STATS_TYPE_INFO;
116 static const struct ng_parse_type ng_one2many_link_stats_type = {
117 &ng_parse_struct_type,
118 &ng_one2many_link_stats_type_fields
121 /* List of commands and how to convert arguments to/from ASCII */
122 static const struct ng_cmdlist ng_one2many_cmdlist[] = {
125 NGM_ONE2MANY_SET_CONFIG,
127 &ng_one2many_config_type,
132 NGM_ONE2MANY_GET_CONFIG,
135 &ng_one2many_config_type
139 NGM_ONE2MANY_GET_STATS,
141 &ng_parse_int32_type,
142 &ng_one2many_link_stats_type
146 NGM_ONE2MANY_CLR_STATS,
148 &ng_parse_int32_type,
153 NGM_ONE2MANY_GETCLR_STATS,
155 &ng_parse_int32_type,
156 &ng_one2many_link_stats_type
161 /* Node type descriptor */
162 static struct ng_type ng_one2many_typestruct = {
163 .version = NG_ABI_VERSION,
164 .name = NG_ONE2MANY_NODE_TYPE,
165 .constructor = ng_one2many_constructor,
166 .rcvmsg = ng_one2many_rcvmsg,
167 .shutdown = ng_one2many_shutdown,
168 .newhook = ng_one2many_newhook,
169 .rcvdata = ng_one2many_rcvdata,
170 .disconnect = ng_one2many_disconnect,
171 .cmdlist = ng_one2many_cmdlist,
173 NETGRAPH_INIT(one2many, &ng_one2many_typestruct);
175 /******************************************************************
176 NETGRAPH NODE METHODS
177 ******************************************************************/
183 ng_one2many_constructor(node_p node)
187 /* Allocate and initialize private info */
188 priv = malloc(sizeof(*priv), M_NETGRAPH, M_WAITOK | M_ZERO);
189 priv->conf.xmitAlg = NG_ONE2MANY_XMIT_ROUNDROBIN;
190 priv->conf.failAlg = NG_ONE2MANY_FAIL_MANUAL;
192 /* cross reference */
193 NG_NODE_SET_PRIVATE(node, priv);
201 * Method for attaching a new hook
204 ng_one2many_newhook(node_p node, hook_p hook, const char *name)
206 const priv_p priv = NG_NODE_PRIVATE(node);
207 struct ng_one2many_link *link;
212 if (strncmp(name, NG_ONE2MANY_HOOK_MANY_PREFIX,
213 strlen(NG_ONE2MANY_HOOK_MANY_PREFIX)) == 0) {
217 cp = name + strlen(NG_ONE2MANY_HOOK_MANY_PREFIX);
218 if (!isdigit(*cp) || (cp[0] == '0' && cp[1] != '\0'))
220 i = strtoul(cp, &eptr, 10);
221 if (*eptr != '\0' || i >= NG_ONE2MANY_MAX_LINKS)
224 link = &priv->many[linkNum];
225 } else if (strcmp(name, NG_ONE2MANY_HOOK_ONE) == 0) {
226 linkNum = NG_ONE2MANY_ONE_LINKNUM;
231 /* Is hook already connected? (should never happen) */
232 if (link->hook != NULL)
235 /* Setup private info for this link */
236 NG_HOOK_SET_PRIVATE(hook, (void *)(intptr_t)linkNum);
238 bzero(&link->stats, sizeof(link->stats));
239 if (linkNum != NG_ONE2MANY_ONE_LINKNUM) {
240 priv->conf.enabledLinks[linkNum] = 1; /* auto-enable link */
241 ng_one2many_update_many(priv);
249 * Receive a control message
252 ng_one2many_rcvmsg(node_p node, item_p item, hook_p lasthook)
254 const priv_p priv = NG_NODE_PRIVATE(node);
255 struct ng_mesg *resp = NULL;
259 NGI_GET_MSG(item, msg);
260 switch (msg->header.typecookie) {
261 case NGM_ONE2MANY_COOKIE:
262 switch (msg->header.cmd) {
263 case NGM_ONE2MANY_SET_CONFIG:
265 struct ng_one2many_config *conf;
268 /* Check that new configuration is valid */
269 if (msg->header.arglen != sizeof(*conf)) {
273 conf = (struct ng_one2many_config *)msg->data;
274 switch (conf->xmitAlg) {
275 case NG_ONE2MANY_XMIT_ROUNDROBIN:
276 case NG_ONE2MANY_XMIT_ALL:
277 case NG_ONE2MANY_XMIT_FAILOVER:
283 switch (conf->failAlg) {
284 case NG_ONE2MANY_FAIL_MANUAL:
285 case NG_ONE2MANY_FAIL_NOTIFY:
294 /* Normalized many link enabled bits */
295 for (i = 0; i < NG_ONE2MANY_MAX_LINKS; i++)
296 conf->enabledLinks[i] = !!conf->enabledLinks[i];
298 /* Copy config and reset */
299 bcopy(conf, &priv->conf, sizeof(*conf));
300 ng_one2many_update_many(priv);
303 case NGM_ONE2MANY_GET_CONFIG:
305 struct ng_one2many_config *conf;
307 NG_MKRESPONSE(resp, msg, sizeof(*conf), M_NOWAIT);
312 conf = (struct ng_one2many_config *)resp->data;
313 bcopy(&priv->conf, conf, sizeof(priv->conf));
316 case NGM_ONE2MANY_GET_STATS:
317 case NGM_ONE2MANY_CLR_STATS:
318 case NGM_ONE2MANY_GETCLR_STATS:
320 struct ng_one2many_link *link;
324 if (msg->header.arglen != sizeof(int32_t)) {
328 linkNum = *((int32_t *)msg->data);
329 if (linkNum == NG_ONE2MANY_ONE_LINKNUM)
331 else if (linkNum >= 0
332 && linkNum < NG_ONE2MANY_MAX_LINKS) {
333 link = &priv->many[linkNum];
339 /* Get/clear stats */
340 if (msg->header.cmd != NGM_ONE2MANY_CLR_STATS) {
341 NG_MKRESPONSE(resp, msg,
342 sizeof(link->stats), M_NOWAIT);
348 resp->data, sizeof(link->stats));
350 if (msg->header.cmd != NGM_ONE2MANY_GET_STATS)
351 bzero(&link->stats, sizeof(link->stats));
360 * One of our downstreams notifies us of link change. If we are
361 * configured to listen to these message, then we remove/add
362 * this hook from array of active hooks.
364 case NGM_FLOW_COOKIE:
368 if (priv->conf.failAlg != NG_ONE2MANY_FAIL_NOTIFY)
371 if (lasthook == NULL)
374 linkNum = (intptr_t)NG_HOOK_PRIVATE(lasthook);
375 if (linkNum == NG_ONE2MANY_ONE_LINKNUM)
378 KASSERT((linkNum >= 0 && linkNum < NG_ONE2MANY_MAX_LINKS),
379 ("%s: linkNum=%d", __func__, linkNum));
381 switch (msg->header.cmd) {
383 priv->conf.enabledLinks[linkNum] = 1;
384 ng_one2many_update_many(priv);
386 case NGM_LINK_IS_DOWN:
387 priv->conf.enabledLinks[linkNum] = 0;
388 ng_one2many_update_many(priv);
401 NG_RESPOND_MSG(error, node, item, resp);
407 * Receive data on a hook
410 ng_one2many_rcvdata(hook_p hook, item_p item)
412 const node_p node = NG_HOOK_NODE(hook);
413 const priv_p priv = NG_NODE_PRIVATE(node);
414 struct ng_one2many_link *src;
415 struct ng_one2many_link *dst = NULL;
421 m = NGI_M(item); /* just peaking, mbuf still owned by item */
422 /* Get link number */
423 linkNum = (intptr_t)NG_HOOK_PRIVATE(hook);
424 KASSERT(linkNum == NG_ONE2MANY_ONE_LINKNUM
425 || (linkNum >= 0 && linkNum < NG_ONE2MANY_MAX_LINKS),
426 ("%s: linkNum=%d", __func__, linkNum));
428 /* Figure out source link */
429 src = (linkNum == NG_ONE2MANY_ONE_LINKNUM) ?
430 &priv->one : &priv->many[linkNum];
431 KASSERT(src->hook != NULL, ("%s: no src%d", __func__, linkNum));
433 /* Update receive stats */
434 src->stats.recvPackets++;
435 src->stats.recvOctets += m->m_pkthdr.len;
437 /* Figure out destination link */
438 if (linkNum == NG_ONE2MANY_ONE_LINKNUM) {
439 if (priv->numActiveMany == 0) {
443 switch(priv->conf.xmitAlg) {
444 case NG_ONE2MANY_XMIT_ROUNDROBIN:
445 dst = &priv->many[priv->activeMany[priv->nextMany]];
446 priv->nextMany = (priv->nextMany + 1) % priv->numActiveMany;
448 case NG_ONE2MANY_XMIT_ALL:
449 /* no need to copy data for the 1st one */
450 dst = &priv->many[priv->activeMany[0]];
452 /* make modifiable copies of data and send for all
453 * links except the first one, which we'll do last
455 for (i = 1; i < priv->numActiveMany; i++) {
457 struct ng_one2many_link *mdst;
459 mdst = &priv->many[priv->activeMany[i]];
460 m2 = m_dup(m, M_NOWAIT);
462 mdst->stats.memoryFailures++;
467 /* Update transmit stats */
468 mdst->stats.xmitPackets++;
469 mdst->stats.xmitOctets += m->m_pkthdr.len;
470 NG_SEND_DATA_ONLY(error, mdst->hook, m2);
473 case NG_ONE2MANY_XMIT_FAILOVER:
474 dst = &priv->many[priv->activeMany[0]];
478 panic("%s: invalid xmitAlg", __func__);
485 /* Update transmit stats */
486 dst->stats.xmitPackets++;
487 dst->stats.xmitOctets += m->m_pkthdr.len;
490 NG_FWD_ITEM_HOOK(error, item, dst->hook);
498 ng_one2many_shutdown(node_p node)
500 const priv_p priv = NG_NODE_PRIVATE(node);
502 KASSERT(priv->numActiveMany == 0,
503 ("%s: numActiveMany=%d", __func__, priv->numActiveMany));
504 free(priv, M_NETGRAPH);
505 NG_NODE_SET_PRIVATE(node, NULL);
511 * Hook disconnection.
514 ng_one2many_disconnect(hook_p hook)
516 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
519 /* Get link number */
520 linkNum = (intptr_t)NG_HOOK_PRIVATE(hook);
521 KASSERT(linkNum == NG_ONE2MANY_ONE_LINKNUM
522 || (linkNum >= 0 && linkNum < NG_ONE2MANY_MAX_LINKS),
523 ("%s: linkNum=%d", __func__, linkNum));
526 if (linkNum == NG_ONE2MANY_ONE_LINKNUM)
527 priv->one.hook = NULL;
529 priv->many[linkNum].hook = NULL;
530 priv->conf.enabledLinks[linkNum] = 0;
531 ng_one2many_update_many(priv);
534 /* If no hooks left, go away */
535 if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
536 && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook))))
537 ng_rmnode_self(NG_HOOK_NODE(hook));
541 /******************************************************************
543 ******************************************************************/
546 * Update internal state after the addition or removal of a "many" link
549 ng_one2many_update_many(priv_p priv)
551 uint16_t saveActive = priv->numActiveMany;
554 /* Update list of which "many" links are up */
555 priv->numActiveMany = 0;
556 for (linkNum = 0; linkNum < NG_ONE2MANY_MAX_LINKS; linkNum++) {
557 switch (priv->conf.failAlg) {
558 case NG_ONE2MANY_FAIL_MANUAL:
559 case NG_ONE2MANY_FAIL_NOTIFY:
560 if (priv->many[linkNum].hook != NULL
561 && priv->conf.enabledLinks[linkNum]) {
562 priv->activeMany[priv->numActiveMany] = linkNum;
563 priv->numActiveMany++;
568 panic("%s: invalid failAlg", __func__);
573 if (priv->numActiveMany == 0 && saveActive > 0)
574 ng_one2many_notify(priv, NGM_LINK_IS_DOWN);
576 if (saveActive == 0 && priv->numActiveMany > 0)
577 ng_one2many_notify(priv, NGM_LINK_IS_UP);
579 /* Update transmit algorithm state */
580 switch (priv->conf.xmitAlg) {
581 case NG_ONE2MANY_XMIT_ROUNDROBIN:
582 if (priv->numActiveMany > 0)
583 priv->nextMany %= priv->numActiveMany;
585 case NG_ONE2MANY_XMIT_ALL:
586 case NG_ONE2MANY_XMIT_FAILOVER:
590 panic("%s: invalid xmitAlg", __func__);
596 * Notify upstream if we are out of links, or we have at least one link.
599 ng_one2many_notify(priv_p priv, uint32_t cmd)
604 if (priv->one.hook == NULL)
607 NG_MKMESSAGE(msg, NGM_FLOW_COOKIE, cmd, 0, M_NOWAIT);
609 NG_SEND_MSG_HOOK(dummy_error, priv->node, msg, priv->one.hook, 0);