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>
40 * $FreeBSD: src/sys/netgraph/ng_one2many.c,v 1.21 2005/03/11 10:29:38 glebius Exp $
41 * $DragonFly: src/sys/netgraph7/ng_one2many.c,v 1.2 2008/06/26 23:05:35 dillon Exp $
45 * ng_one2many(4) netgraph node type
47 * Packets received on the "one" hook are sent out each of the
48 * "many" hooks accoring to an algorithm. Packets received on any
49 * "many" hook are always delivered to the "one" hook.
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/kernel.h>
55 #include <sys/malloc.h>
56 #include <sys/ctype.h>
58 #include <sys/errno.h>
60 #include <netgraph7/ng_message.h>
61 #include <netgraph7/netgraph.h>
62 #include <netgraph7/ng_parse.h>
63 #include "ng_one2many.h"
65 /* Per-link private data */
66 struct ng_one2many_link {
67 hook_p hook; /* netgraph hook */
68 struct ng_one2many_link_stats stats; /* link stats */
71 /* Per-node private data */
72 struct ng_one2many_private {
73 node_p node; /* link to node */
74 struct ng_one2many_config conf; /* node configuration */
75 struct ng_one2many_link one; /* "one" hook */
76 struct ng_one2many_link many[NG_ONE2MANY_MAX_LINKS];
77 u_int16_t nextMany; /* next round-robin */
78 u_int16_t numActiveMany; /* # active "many" */
79 u_int16_t activeMany[NG_ONE2MANY_MAX_LINKS];
81 typedef struct ng_one2many_private *priv_p;
83 /* Netgraph node methods */
84 static ng_constructor_t ng_one2many_constructor;
85 static ng_rcvmsg_t ng_one2many_rcvmsg;
86 static ng_shutdown_t ng_one2many_shutdown;
87 static ng_newhook_t ng_one2many_newhook;
88 static ng_rcvdata_t ng_one2many_rcvdata;
89 static ng_disconnect_t ng_one2many_disconnect;
92 static void ng_one2many_update_many(priv_p priv);
93 static void ng_one2many_notify(priv_p priv, uint32_t cmd);
95 /******************************************************************
97 ******************************************************************/
99 /* Parse type for struct ng_one2many_config */
100 static const struct ng_parse_fixedarray_info
101 ng_one2many_enableLinks_array_type_info = {
102 &ng_parse_uint8_type,
103 NG_ONE2MANY_MAX_LINKS
105 static const struct ng_parse_type ng_one2many_enableLinks_array_type = {
106 &ng_parse_fixedarray_type,
107 &ng_one2many_enableLinks_array_type_info,
109 static const struct ng_parse_struct_field ng_one2many_config_type_fields[]
110 = NG_ONE2MANY_CONFIG_TYPE_INFO(&ng_one2many_enableLinks_array_type);
111 static const struct ng_parse_type ng_one2many_config_type = {
112 &ng_parse_struct_type,
113 &ng_one2many_config_type_fields
116 /* Parse type for struct ng_one2many_link_stats */
117 static const struct ng_parse_struct_field ng_one2many_link_stats_type_fields[]
118 = NG_ONE2MANY_LINK_STATS_TYPE_INFO;
119 static const struct ng_parse_type ng_one2many_link_stats_type = {
120 &ng_parse_struct_type,
121 &ng_one2many_link_stats_type_fields
124 /* List of commands and how to convert arguments to/from ASCII */
125 static const struct ng_cmdlist ng_one2many_cmdlist[] = {
128 NGM_ONE2MANY_SET_CONFIG,
130 &ng_one2many_config_type,
135 NGM_ONE2MANY_GET_CONFIG,
138 &ng_one2many_config_type
142 NGM_ONE2MANY_GET_STATS,
144 &ng_parse_int32_type,
145 &ng_one2many_link_stats_type
149 NGM_ONE2MANY_CLR_STATS,
151 &ng_parse_int32_type,
156 NGM_ONE2MANY_GETCLR_STATS,
158 &ng_parse_int32_type,
159 &ng_one2many_link_stats_type
164 /* Node type descriptor */
165 static struct ng_type ng_one2many_typestruct = {
166 .version = NG_ABI_VERSION,
167 .name = NG_ONE2MANY_NODE_TYPE,
168 .constructor = ng_one2many_constructor,
169 .rcvmsg = ng_one2many_rcvmsg,
170 .shutdown = ng_one2many_shutdown,
171 .newhook = ng_one2many_newhook,
172 .rcvdata = ng_one2many_rcvdata,
173 .disconnect = ng_one2many_disconnect,
174 .cmdlist = ng_one2many_cmdlist,
176 NETGRAPH_INIT(one2many, &ng_one2many_typestruct);
178 /******************************************************************
179 NETGRAPH NODE METHODS
180 ******************************************************************/
186 ng_one2many_constructor(node_p node)
190 /* Allocate and initialize private info */
191 priv = kmalloc(sizeof(*priv), M_NETGRAPH,
192 M_WAITOK | M_NULLOK | M_ZERO);
195 priv->conf.xmitAlg = NG_ONE2MANY_XMIT_ROUNDROBIN;
196 priv->conf.failAlg = NG_ONE2MANY_FAIL_MANUAL;
198 /* cross reference */
199 NG_NODE_SET_PRIVATE(node, priv);
207 * Method for attaching a new hook
210 ng_one2many_newhook(node_p node, hook_p hook, const char *name)
212 const priv_p priv = NG_NODE_PRIVATE(node);
213 struct ng_one2many_link *link;
218 if (strncmp(name, NG_ONE2MANY_HOOK_MANY_PREFIX,
219 strlen(NG_ONE2MANY_HOOK_MANY_PREFIX)) == 0) {
223 cp = name + strlen(NG_ONE2MANY_HOOK_MANY_PREFIX);
224 if (!isdigit(*cp) || (cp[0] == '0' && cp[1] != '\0'))
226 i = strtoul(cp, &eptr, 10);
227 if (*eptr != '\0' || i < 0 || i >= NG_ONE2MANY_MAX_LINKS)
230 link = &priv->many[linkNum];
231 } else if (strcmp(name, NG_ONE2MANY_HOOK_ONE) == 0) {
232 linkNum = NG_ONE2MANY_ONE_LINKNUM;
237 /* Is hook already connected? (should never happen) */
238 if (link->hook != NULL)
241 /* Setup private info for this link */
242 NG_HOOK_SET_PRIVATE(hook, (void *)(intptr_t)linkNum);
244 bzero(&link->stats, sizeof(link->stats));
245 if (linkNum != NG_ONE2MANY_ONE_LINKNUM) {
246 priv->conf.enabledLinks[linkNum] = 1; /* auto-enable link */
247 ng_one2many_update_many(priv);
255 * Receive a control message
258 ng_one2many_rcvmsg(node_p node, item_p item, hook_p lasthook)
260 const priv_p priv = NG_NODE_PRIVATE(node);
261 struct ng_mesg *resp = NULL;
265 NGI_GET_MSG(item, msg);
266 switch (msg->header.typecookie) {
267 case NGM_ONE2MANY_COOKIE:
268 switch (msg->header.cmd) {
269 case NGM_ONE2MANY_SET_CONFIG:
271 struct ng_one2many_config *conf;
274 /* Check that new configuration is valid */
275 if (msg->header.arglen != sizeof(*conf)) {
279 conf = (struct ng_one2many_config *)msg->data;
280 switch (conf->xmitAlg) {
281 case NG_ONE2MANY_XMIT_ROUNDROBIN:
282 case NG_ONE2MANY_XMIT_ALL:
288 switch (conf->failAlg) {
289 case NG_ONE2MANY_FAIL_MANUAL:
290 case NG_ONE2MANY_FAIL_NOTIFY:
299 /* Normalized many link enabled bits */
300 for (i = 0; i < NG_ONE2MANY_MAX_LINKS; i++)
301 conf->enabledLinks[i] = !!conf->enabledLinks[i];
303 /* Copy config and reset */
304 bcopy(conf, &priv->conf, sizeof(*conf));
305 ng_one2many_update_many(priv);
308 case NGM_ONE2MANY_GET_CONFIG:
310 struct ng_one2many_config *conf;
312 NG_MKRESPONSE(resp, msg, sizeof(*conf), M_WAITOK | M_NULLOK);
317 conf = (struct ng_one2many_config *)resp->data;
318 bcopy(&priv->conf, conf, sizeof(priv->conf));
321 case NGM_ONE2MANY_GET_STATS:
322 case NGM_ONE2MANY_CLR_STATS:
323 case NGM_ONE2MANY_GETCLR_STATS:
325 struct ng_one2many_link *link;
329 if (msg->header.arglen != sizeof(int32_t)) {
333 linkNum = *((int32_t *)msg->data);
334 if (linkNum == NG_ONE2MANY_ONE_LINKNUM)
336 else if (linkNum >= 0
337 && linkNum < NG_ONE2MANY_MAX_LINKS) {
338 link = &priv->many[linkNum];
344 /* Get/clear stats */
345 if (msg->header.cmd != NGM_ONE2MANY_CLR_STATS) {
346 NG_MKRESPONSE(resp, msg,
347 sizeof(link->stats), M_WAITOK | M_NULLOK);
353 resp->data, sizeof(link->stats));
355 if (msg->header.cmd != NGM_ONE2MANY_GET_STATS)
356 bzero(&link->stats, sizeof(link->stats));
365 * One of our downstreams notifies us of link change. If we are
366 * configured to listen to these message, then we remove/add
367 * this hook from array of active hooks.
369 case NGM_FLOW_COOKIE:
373 if (priv->conf.failAlg != NG_ONE2MANY_FAIL_NOTIFY)
376 if (lasthook == NULL)
379 linkNum = (intptr_t)NG_HOOK_PRIVATE(lasthook);
380 if (linkNum == NG_ONE2MANY_ONE_LINKNUM)
383 KASSERT((linkNum >= 0 && linkNum < NG_ONE2MANY_MAX_LINKS),
384 ("%s: linkNum=%d", __func__, linkNum));
386 switch (msg->header.cmd) {
388 priv->conf.enabledLinks[linkNum] = 1;
389 ng_one2many_update_many(priv);
391 case NGM_LINK_IS_DOWN:
392 priv->conf.enabledLinks[linkNum] = 0;
393 ng_one2many_update_many(priv);
406 NG_RESPOND_MSG(error, node, item, resp);
412 * Receive data on a hook
415 ng_one2many_rcvdata(hook_p hook, item_p item)
417 const node_p node = NG_HOOK_NODE(hook);
418 const priv_p priv = NG_NODE_PRIVATE(node);
419 struct ng_one2many_link *src;
420 struct ng_one2many_link *dst = NULL;
426 m = NGI_M(item); /* just peaking, mbuf still owned by item */
427 /* Get link number */
428 linkNum = (intptr_t)NG_HOOK_PRIVATE(hook);
429 KASSERT(linkNum == NG_ONE2MANY_ONE_LINKNUM
430 || (linkNum >= 0 && linkNum < NG_ONE2MANY_MAX_LINKS),
431 ("%s: linkNum=%d", __func__, linkNum));
433 /* Figure out source link */
434 src = (linkNum == NG_ONE2MANY_ONE_LINKNUM) ?
435 &priv->one : &priv->many[linkNum];
436 KASSERT(src->hook != NULL, ("%s: no src%d", __func__, linkNum));
438 /* Update receive stats */
439 src->stats.recvPackets++;
440 src->stats.recvOctets += m->m_pkthdr.len;
442 /* Figure out destination link */
443 if (linkNum == NG_ONE2MANY_ONE_LINKNUM) {
444 if (priv->numActiveMany == 0) {
448 switch(priv->conf.xmitAlg) {
449 case NG_ONE2MANY_XMIT_ROUNDROBIN:
450 dst = &priv->many[priv->activeMany[priv->nextMany]];
451 priv->nextMany = (priv->nextMany + 1) % priv->numActiveMany;
453 case NG_ONE2MANY_XMIT_ALL:
454 /* no need to copy data for the 1st one */
455 dst = &priv->many[priv->activeMany[0]];
457 /* make copies of data and send for all links
458 * except the first one, which we'll do last
460 for (i = 1; i < priv->numActiveMany; i++) {
462 struct ng_one2many_link *mdst;
464 mdst = &priv->many[priv->activeMany[i]];
465 m2 = m_dup(m, MB_DONTWAIT); /* XXX m_copypacket() */
467 mdst->stats.memoryFailures++;
472 /* Update transmit stats */
473 mdst->stats.xmitPackets++;
474 mdst->stats.xmitOctets += m->m_pkthdr.len;
475 NG_SEND_DATA_ONLY(error, mdst->hook, m2);
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 kfree(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:
591 panic("%s: invalid xmitAlg", __func__);
597 * Notify upstream if we are out of links, or we have at least one link.
600 ng_one2many_notify(priv_p priv, uint32_t cmd)
605 if (priv->one.hook == NULL)
608 NG_MKMESSAGE(msg, NGM_FLOW_COOKIE, cmd, 0, M_WAITOK | M_NULLOK);
610 NG_SEND_MSG_HOOK(dummy_error, priv->node, msg, priv->one.hook, 0);