linprocfs - Introduce /proc/mounts
[dragonfly.git] / sys / netgraph7 / ng_one2many.c
blobf4923e65a19635a4fde5d92e5ca00bf2a65a590a
1 /*
2 * ng_one2many.c
3 */
5 /*-
6 * Copyright (c) 2000 Whistle Communications, Inc.
7 * All rights reserved.
8 *
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
36 * OF SUCH DAMAGE.
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>
57 #include <sys/mbuf.h>
58 #include <sys/errno.h>
60 #include "ng_message.h"
61 #include "netgraph.h"
62 #include "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;
91 /* Other functions */
92 static void ng_one2many_update_many(priv_p priv);
93 static void ng_one2many_notify(priv_p priv, uint32_t cmd);
95 /******************************************************************
96 NETGRAPH PARSE TYPES
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[] = {
127 NGM_ONE2MANY_COOKIE,
128 NGM_ONE2MANY_SET_CONFIG,
129 "setconfig",
130 &ng_one2many_config_type,
131 NULL
134 NGM_ONE2MANY_COOKIE,
135 NGM_ONE2MANY_GET_CONFIG,
136 "getconfig",
137 NULL,
138 &ng_one2many_config_type
141 NGM_ONE2MANY_COOKIE,
142 NGM_ONE2MANY_GET_STATS,
143 "getstats",
144 &ng_parse_int32_type,
145 &ng_one2many_link_stats_type
148 NGM_ONE2MANY_COOKIE,
149 NGM_ONE2MANY_CLR_STATS,
150 "clrstats",
151 &ng_parse_int32_type,
152 NULL,
155 NGM_ONE2MANY_COOKIE,
156 NGM_ONE2MANY_GETCLR_STATS,
157 "getclrstats",
158 &ng_parse_int32_type,
159 &ng_one2many_link_stats_type
161 { 0 }
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 ******************************************************************/
183 * Node constructor
185 static int
186 ng_one2many_constructor(node_p node)
188 priv_p priv;
190 /* Allocate and initialize private info */
191 MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_WAITOK | M_NULLOK | M_ZERO);
192 if (priv == NULL)
193 return (ENOMEM);
194 priv->conf.xmitAlg = NG_ONE2MANY_XMIT_ROUNDROBIN;
195 priv->conf.failAlg = NG_ONE2MANY_FAIL_MANUAL;
197 /* cross reference */
198 NG_NODE_SET_PRIVATE(node, priv);
199 priv->node = node;
201 /* Done */
202 return (0);
206 * Method for attaching a new hook
208 static int
209 ng_one2many_newhook(node_p node, hook_p hook, const char *name)
211 const priv_p priv = NG_NODE_PRIVATE(node);
212 struct ng_one2many_link *link;
213 int linkNum;
214 u_long i;
216 /* Which hook? */
217 if (strncmp(name, NG_ONE2MANY_HOOK_MANY_PREFIX,
218 strlen(NG_ONE2MANY_HOOK_MANY_PREFIX)) == 0) {
219 const char *cp;
220 char *eptr;
222 cp = name + strlen(NG_ONE2MANY_HOOK_MANY_PREFIX);
223 if (!isdigit(*cp) || (cp[0] == '0' && cp[1] != '\0'))
224 return (EINVAL);
225 i = strtoul(cp, &eptr, 10);
226 if (*eptr != '\0' || i < 0 || i >= NG_ONE2MANY_MAX_LINKS)
227 return (EINVAL);
228 linkNum = (int)i;
229 link = &priv->many[linkNum];
230 } else if (strcmp(name, NG_ONE2MANY_HOOK_ONE) == 0) {
231 linkNum = NG_ONE2MANY_ONE_LINKNUM;
232 link = &priv->one;
233 } else
234 return (EINVAL);
236 /* Is hook already connected? (should never happen) */
237 if (link->hook != NULL)
238 return (EISCONN);
240 /* Setup private info for this link */
241 NG_HOOK_SET_PRIVATE(hook, (void *)(intptr_t)linkNum);
242 link->hook = hook;
243 bzero(&link->stats, sizeof(link->stats));
244 if (linkNum != NG_ONE2MANY_ONE_LINKNUM) {
245 priv->conf.enabledLinks[linkNum] = 1; /* auto-enable link */
246 ng_one2many_update_many(priv);
249 /* Done */
250 return (0);
254 * Receive a control message
256 static int
257 ng_one2many_rcvmsg(node_p node, item_p item, hook_p lasthook)
259 const priv_p priv = NG_NODE_PRIVATE(node);
260 struct ng_mesg *resp = NULL;
261 int error = 0;
262 struct ng_mesg *msg;
264 NGI_GET_MSG(item, msg);
265 switch (msg->header.typecookie) {
266 case NGM_ONE2MANY_COOKIE:
267 switch (msg->header.cmd) {
268 case NGM_ONE2MANY_SET_CONFIG:
270 struct ng_one2many_config *conf;
271 int i;
273 /* Check that new configuration is valid */
274 if (msg->header.arglen != sizeof(*conf)) {
275 error = EINVAL;
276 break;
278 conf = (struct ng_one2many_config *)msg->data;
279 switch (conf->xmitAlg) {
280 case NG_ONE2MANY_XMIT_ROUNDROBIN:
281 case NG_ONE2MANY_XMIT_ALL:
282 break;
283 default:
284 error = EINVAL;
285 break;
287 switch (conf->failAlg) {
288 case NG_ONE2MANY_FAIL_MANUAL:
289 case NG_ONE2MANY_FAIL_NOTIFY:
290 break;
291 default:
292 error = EINVAL;
293 break;
295 if (error != 0)
296 break;
298 /* Normalized many link enabled bits */
299 for (i = 0; i < NG_ONE2MANY_MAX_LINKS; i++)
300 conf->enabledLinks[i] = !!conf->enabledLinks[i];
302 /* Copy config and reset */
303 bcopy(conf, &priv->conf, sizeof(*conf));
304 ng_one2many_update_many(priv);
305 break;
307 case NGM_ONE2MANY_GET_CONFIG:
309 struct ng_one2many_config *conf;
311 NG_MKRESPONSE(resp, msg, sizeof(*conf), M_WAITOK | M_NULLOK);
312 if (resp == NULL) {
313 error = ENOMEM;
314 break;
316 conf = (struct ng_one2many_config *)resp->data;
317 bcopy(&priv->conf, conf, sizeof(priv->conf));
318 break;
320 case NGM_ONE2MANY_GET_STATS:
321 case NGM_ONE2MANY_CLR_STATS:
322 case NGM_ONE2MANY_GETCLR_STATS:
324 struct ng_one2many_link *link;
325 int linkNum;
327 /* Get link */
328 if (msg->header.arglen != sizeof(int32_t)) {
329 error = EINVAL;
330 break;
332 linkNum = *((int32_t *)msg->data);
333 if (linkNum == NG_ONE2MANY_ONE_LINKNUM)
334 link = &priv->one;
335 else if (linkNum >= 0
336 && linkNum < NG_ONE2MANY_MAX_LINKS) {
337 link = &priv->many[linkNum];
338 } else {
339 error = EINVAL;
340 break;
343 /* Get/clear stats */
344 if (msg->header.cmd != NGM_ONE2MANY_CLR_STATS) {
345 NG_MKRESPONSE(resp, msg,
346 sizeof(link->stats), M_WAITOK | M_NULLOK);
347 if (resp == NULL) {
348 error = ENOMEM;
349 break;
351 bcopy(&link->stats,
352 resp->data, sizeof(link->stats));
354 if (msg->header.cmd != NGM_ONE2MANY_GET_STATS)
355 bzero(&link->stats, sizeof(link->stats));
356 break;
358 default:
359 error = EINVAL;
360 break;
362 break;
364 * One of our downstreams notifies us of link change. If we are
365 * configured to listen to these message, then we remove/add
366 * this hook from array of active hooks.
368 case NGM_FLOW_COOKIE:
370 int linkNum;
372 if (priv->conf.failAlg != NG_ONE2MANY_FAIL_NOTIFY)
373 break;
375 if (lasthook == NULL)
376 break;
378 linkNum = (intptr_t)NG_HOOK_PRIVATE(lasthook);
379 if (linkNum == NG_ONE2MANY_ONE_LINKNUM)
380 break;
382 KASSERT((linkNum >= 0 && linkNum < NG_ONE2MANY_MAX_LINKS),
383 ("%s: linkNum=%d", __func__, linkNum));
385 switch (msg->header.cmd) {
386 case NGM_LINK_IS_UP:
387 priv->conf.enabledLinks[linkNum] = 1;
388 ng_one2many_update_many(priv);
389 break;
390 case NGM_LINK_IS_DOWN:
391 priv->conf.enabledLinks[linkNum] = 0;
392 ng_one2many_update_many(priv);
393 break;
394 default:
395 break;
397 break;
399 default:
400 error = EINVAL;
401 break;
404 /* Done */
405 NG_RESPOND_MSG(error, node, item, resp);
406 NG_FREE_MSG(msg);
407 return (error);
411 * Receive data on a hook
413 static int
414 ng_one2many_rcvdata(hook_p hook, item_p item)
416 const node_p node = NG_HOOK_NODE(hook);
417 const priv_p priv = NG_NODE_PRIVATE(node);
418 struct ng_one2many_link *src;
419 struct ng_one2many_link *dst = NULL;
420 int error = 0;
421 int linkNum;
422 int i;
423 struct mbuf *m;
425 m = NGI_M(item); /* just peaking, mbuf still owned by item */
426 /* Get link number */
427 linkNum = (intptr_t)NG_HOOK_PRIVATE(hook);
428 KASSERT(linkNum == NG_ONE2MANY_ONE_LINKNUM
429 || (linkNum >= 0 && linkNum < NG_ONE2MANY_MAX_LINKS),
430 ("%s: linkNum=%d", __func__, linkNum));
432 /* Figure out source link */
433 src = (linkNum == NG_ONE2MANY_ONE_LINKNUM) ?
434 &priv->one : &priv->many[linkNum];
435 KASSERT(src->hook != NULL, ("%s: no src%d", __func__, linkNum));
437 /* Update receive stats */
438 src->stats.recvPackets++;
439 src->stats.recvOctets += m->m_pkthdr.len;
441 /* Figure out destination link */
442 if (linkNum == NG_ONE2MANY_ONE_LINKNUM) {
443 if (priv->numActiveMany == 0) {
444 NG_FREE_ITEM(item);
445 return (ENOTCONN);
447 switch(priv->conf.xmitAlg) {
448 case NG_ONE2MANY_XMIT_ROUNDROBIN:
449 dst = &priv->many[priv->activeMany[priv->nextMany]];
450 priv->nextMany = (priv->nextMany + 1) % priv->numActiveMany;
451 break;
452 case NG_ONE2MANY_XMIT_ALL:
453 /* no need to copy data for the 1st one */
454 dst = &priv->many[priv->activeMany[0]];
456 /* make copies of data and send for all links
457 * except the first one, which we'll do last
459 for (i = 1; i < priv->numActiveMany; i++) {
460 struct mbuf *m2;
461 struct ng_one2many_link *mdst;
463 mdst = &priv->many[priv->activeMany[i]];
464 m2 = m_dup(m, MB_DONTWAIT); /* XXX m_copypacket() */
465 if (m2 == NULL) {
466 mdst->stats.memoryFailures++;
467 NG_FREE_ITEM(item);
468 NG_FREE_M(m);
469 return (ENOBUFS);
471 /* Update transmit stats */
472 mdst->stats.xmitPackets++;
473 mdst->stats.xmitOctets += m->m_pkthdr.len;
474 NG_SEND_DATA_ONLY(error, mdst->hook, m2);
476 break;
477 #ifdef INVARIANTS
478 default:
479 panic("%s: invalid xmitAlg", __func__);
480 #endif
482 } else {
483 dst = &priv->one;
486 /* Update transmit stats */
487 dst->stats.xmitPackets++;
488 dst->stats.xmitOctets += m->m_pkthdr.len;
490 /* Deliver packet */
491 NG_FWD_ITEM_HOOK(error, item, dst->hook);
492 return (error);
496 * Shutdown node
498 static int
499 ng_one2many_shutdown(node_p node)
501 const priv_p priv = NG_NODE_PRIVATE(node);
503 KASSERT(priv->numActiveMany == 0,
504 ("%s: numActiveMany=%d", __func__, priv->numActiveMany));
505 FREE(priv, M_NETGRAPH);
506 NG_NODE_SET_PRIVATE(node, NULL);
507 NG_NODE_UNREF(node);
508 return (0);
512 * Hook disconnection.
514 static int
515 ng_one2many_disconnect(hook_p hook)
517 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
518 int linkNum;
520 /* Get link number */
521 linkNum = (intptr_t)NG_HOOK_PRIVATE(hook);
522 KASSERT(linkNum == NG_ONE2MANY_ONE_LINKNUM
523 || (linkNum >= 0 && linkNum < NG_ONE2MANY_MAX_LINKS),
524 ("%s: linkNum=%d", __func__, linkNum));
526 /* Nuke the link */
527 if (linkNum == NG_ONE2MANY_ONE_LINKNUM)
528 priv->one.hook = NULL;
529 else {
530 priv->many[linkNum].hook = NULL;
531 priv->conf.enabledLinks[linkNum] = 0;
532 ng_one2many_update_many(priv);
535 /* If no hooks left, go away */
536 if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
537 && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook))))
538 ng_rmnode_self(NG_HOOK_NODE(hook));
539 return (0);
542 /******************************************************************
543 OTHER FUNCTIONS
544 ******************************************************************/
547 * Update internal state after the addition or removal of a "many" link
549 static void
550 ng_one2many_update_many(priv_p priv)
552 uint16_t saveActive = priv->numActiveMany;
553 int linkNum;
555 /* Update list of which "many" links are up */
556 priv->numActiveMany = 0;
557 for (linkNum = 0; linkNum < NG_ONE2MANY_MAX_LINKS; linkNum++) {
558 switch (priv->conf.failAlg) {
559 case NG_ONE2MANY_FAIL_MANUAL:
560 case NG_ONE2MANY_FAIL_NOTIFY:
561 if (priv->many[linkNum].hook != NULL
562 && priv->conf.enabledLinks[linkNum]) {
563 priv->activeMany[priv->numActiveMany] = linkNum;
564 priv->numActiveMany++;
566 break;
567 #ifdef INVARIANTS
568 default:
569 panic("%s: invalid failAlg", __func__);
570 #endif
574 if (priv->numActiveMany == 0 && saveActive > 0)
575 ng_one2many_notify(priv, NGM_LINK_IS_DOWN);
577 if (saveActive == 0 && priv->numActiveMany > 0)
578 ng_one2many_notify(priv, NGM_LINK_IS_UP);
580 /* Update transmit algorithm state */
581 switch (priv->conf.xmitAlg) {
582 case NG_ONE2MANY_XMIT_ROUNDROBIN:
583 if (priv->numActiveMany > 0)
584 priv->nextMany %= priv->numActiveMany;
585 break;
586 case NG_ONE2MANY_XMIT_ALL:
587 break;
588 #ifdef INVARIANTS
589 default:
590 panic("%s: invalid xmitAlg", __func__);
591 #endif
596 * Notify upstream if we are out of links, or we have at least one link.
598 static void
599 ng_one2many_notify(priv_p priv, uint32_t cmd)
601 struct ng_mesg *msg;
602 int dummy_error = 0;
604 if (priv->one.hook == NULL)
605 return;
607 NG_MKMESSAGE(msg, NGM_FLOW_COOKIE, cmd, 0, M_WAITOK | M_NULLOK);
608 if (msg != NULL)
609 NG_SEND_MSG_HOOK(dummy_error, priv->node, msg, priv->one.hook, 0);