5 * Copyright (c) 2000 Whistle Communications, Inc.
8 * Subject to the following obligations and disclaimer of warranty, use and
9 * redistribution of this software, in source or object code forms, with or
10 * without modifications are expressly permitted by Whistle Communications;
11 * provided, however, that:
12 * 1. Any and all reproductions of the source or object code must include the
13 * copyright notice above and the following disclaimer of warranties; and
14 * 2. No rights are granted, in any manner or form, to use Whistle
15 * Communications, Inc. trademarks, including the mark "WHISTLE
16 * COMMUNICATIONS" on advertising, endorsements, or otherwise except as
17 * such appears in the above copyright notice or in the software.
19 * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
20 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
21 * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
22 * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
23 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
24 * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
25 * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
26 * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
27 * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
28 * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
29 * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
30 * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
31 * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34 * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
37 * Author: Archie Cobbs <archie@freebsd.org>
43 * ng_one2many(4) netgraph node type
45 * Packets received on the "one" hook are sent out each of the
46 * "many" hooks accoring to an algorithm. Packets received on any
47 * "many" hook are always delivered to the "one" hook.
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/kernel.h>
53 #include <sys/malloc.h>
54 #include <sys/ctype.h>
56 #include <sys/errno.h>
58 #include <netgraph/ng_message.h>
59 #include <netgraph/netgraph.h>
60 #include <netgraph/ng_parse.h>
61 #include <netgraph/ng_one2many.h>
63 /* Per-link private data */
64 struct ng_one2many_link {
65 hook_p hook; /* netgraph hook */
66 struct ng_one2many_link_stats stats; /* link stats */
69 /* Per-node private data */
70 struct ng_one2many_private {
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);
91 /* Store each hook's link number in the private field */
92 #define LINK_NUM(hook) (*(int16_t *)(&(hook)->private))
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 MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO);
193 priv->conf.xmitAlg = NG_ONE2MANY_XMIT_ROUNDROBIN;
194 priv->conf.failAlg = NG_ONE2MANY_FAIL_MANUAL;
196 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:
284 switch (conf->failAlg) {
285 case NG_ONE2MANY_FAIL_MANUAL:
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));
365 NG_RESPOND_MSG(error, node, item, resp);
371 * Receive data on a hook
374 ng_one2many_rcvdata(hook_p hook, item_p item)
376 const node_p node = NG_HOOK_NODE(hook);
377 const priv_p priv = NG_NODE_PRIVATE(node);
378 struct ng_one2many_link *src;
379 struct ng_one2many_link *dst = NULL;
385 m = NGI_M(item); /* just peaking, mbuf still owned by item */
386 /* Get link number */
387 linkNum = (intptr_t)NG_HOOK_PRIVATE(hook);
388 KASSERT(linkNum == NG_ONE2MANY_ONE_LINKNUM
389 || (linkNum >= 0 && linkNum < NG_ONE2MANY_MAX_LINKS),
390 ("%s: linkNum=%d", __func__, linkNum));
392 /* Figure out source link */
393 src = (linkNum == NG_ONE2MANY_ONE_LINKNUM) ?
394 &priv->one : &priv->many[linkNum];
395 KASSERT(src->hook != NULL, ("%s: no src%d", __func__, linkNum));
397 /* Update receive stats */
398 src->stats.recvPackets++;
399 src->stats.recvOctets += m->m_pkthdr.len;
401 /* Figure out destination link */
402 if (linkNum == NG_ONE2MANY_ONE_LINKNUM) {
403 if (priv->numActiveMany == 0) {
407 switch(priv->conf.xmitAlg) {
408 case NG_ONE2MANY_XMIT_ROUNDROBIN:
409 dst = &priv->many[priv->activeMany[priv->nextMany]];
410 priv->nextMany = (priv->nextMany + 1) % priv->numActiveMany;
412 case NG_ONE2MANY_XMIT_ALL:
413 /* no need to copy data for the 1st one */
414 dst = &priv->many[priv->activeMany[0]];
416 /* make copies of data and send for all links
417 * except the first one, which we'll do last
419 for (i = 1; i < priv->numActiveMany; i++) {
421 struct ng_one2many_link *mdst;
423 mdst = &priv->many[priv->activeMany[i]];
424 m2 = m_dup(m, M_DONTWAIT); /* XXX m_copypacket() */
426 mdst->stats.memoryFailures++;
431 /* Update transmit stats */
432 mdst->stats.xmitPackets++;
433 mdst->stats.xmitOctets += m->m_pkthdr.len;
434 NG_SEND_DATA_ONLY(error, mdst->hook, m2);
439 panic("%s: invalid xmitAlg", __func__);
446 /* Update transmit stats */
447 dst->stats.xmitPackets++;
448 dst->stats.xmitOctets += m->m_pkthdr.len;
451 NG_FWD_ITEM_HOOK(error, item, dst->hook);
459 ng_one2many_shutdown(node_p node)
461 const priv_p priv = NG_NODE_PRIVATE(node);
463 KASSERT(priv->numActiveMany == 0,
464 ("%s: numActiveMany=%d", __func__, priv->numActiveMany));
465 FREE(priv, M_NETGRAPH);
466 NG_NODE_SET_PRIVATE(node, NULL);
472 * Hook disconnection.
475 ng_one2many_disconnect(hook_p hook)
477 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
480 /* Get link number */
481 linkNum = (intptr_t)NG_HOOK_PRIVATE(hook);
482 KASSERT(linkNum == NG_ONE2MANY_ONE_LINKNUM
483 || (linkNum >= 0 && linkNum < NG_ONE2MANY_MAX_LINKS),
484 ("%s: linkNum=%d", __func__, linkNum));
487 if (linkNum == NG_ONE2MANY_ONE_LINKNUM)
488 priv->one.hook = NULL;
490 priv->many[linkNum].hook = NULL;
491 priv->conf.enabledLinks[linkNum] = 0;
492 ng_one2many_update_many(priv);
495 /* If no hooks left, go away */
496 if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
497 && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook))))
498 ng_rmnode_self(NG_HOOK_NODE(hook));
502 /******************************************************************
504 ******************************************************************/
507 * Update internal state after the addition or removal of a "many" link
510 ng_one2many_update_many(priv_p priv)
514 /* Update list of which "many" links are up */
515 priv->numActiveMany = 0;
516 for (linkNum = 0; linkNum < NG_ONE2MANY_MAX_LINKS; linkNum++) {
517 switch (priv->conf.failAlg) {
518 case NG_ONE2MANY_FAIL_MANUAL:
519 if (priv->many[linkNum].hook != NULL
520 && priv->conf.enabledLinks[linkNum]) {
521 priv->activeMany[priv->numActiveMany] = linkNum;
522 priv->numActiveMany++;
527 panic("%s: invalid failAlg", __func__);
532 /* Update transmit algorithm state */
533 switch (priv->conf.xmitAlg) {
534 case NG_ONE2MANY_XMIT_ROUNDROBIN:
535 if (priv->numActiveMany > 0)
536 priv->nextMany %= priv->numActiveMany;
538 case NG_ONE2MANY_XMIT_ALL:
542 panic("%s: invalid xmitAlg", __func__);