Welcome ng_deflate.
[dragonfly.git] / sys / netgraph7 / ng_one2many.c
1 /*
2  * ng_one2many.c
3  */
4
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.
19  * 
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.
37  *
38  * Author: Archie Cobbs <archie@freebsd.org>
39  *
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 $
42  */
43
44 /*
45  * ng_one2many(4) netgraph node type
46  *
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.
50  */
51
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>
59
60 #include "ng_message.h"
61 #include "netgraph.h"
62 #include "ng_parse.h"
63 #include "ng_one2many.h"
64
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 */
69 };
70
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];
80 };
81 typedef struct ng_one2many_private *priv_p;
82
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;
90
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);
94
95 /******************************************************************
96                     NETGRAPH PARSE TYPES
97 ******************************************************************/
98
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
104 };
105 static const struct ng_parse_type ng_one2many_enableLinks_array_type = {
106         &ng_parse_fixedarray_type,
107         &ng_one2many_enableLinks_array_type_info,
108 };
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
114 };
115
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
122 };
123
124 /* List of commands and how to convert arguments to/from ASCII */
125 static const struct ng_cmdlist ng_one2many_cmdlist[] = {
126         {
127           NGM_ONE2MANY_COOKIE,
128           NGM_ONE2MANY_SET_CONFIG,
129           "setconfig",
130           &ng_one2many_config_type,
131           NULL
132         },
133         {
134           NGM_ONE2MANY_COOKIE,
135           NGM_ONE2MANY_GET_CONFIG,
136           "getconfig",
137           NULL,
138           &ng_one2many_config_type
139         },
140         {
141           NGM_ONE2MANY_COOKIE,
142           NGM_ONE2MANY_GET_STATS,
143           "getstats",
144           &ng_parse_int32_type,
145           &ng_one2many_link_stats_type
146         },
147         {
148           NGM_ONE2MANY_COOKIE,
149           NGM_ONE2MANY_CLR_STATS,
150           "clrstats",
151           &ng_parse_int32_type,
152           NULL,
153         },
154         {
155           NGM_ONE2MANY_COOKIE,
156           NGM_ONE2MANY_GETCLR_STATS,
157           "getclrstats",
158           &ng_parse_int32_type,
159           &ng_one2many_link_stats_type
160         },
161         { 0 }
162 };
163
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,
175 };
176 NETGRAPH_INIT(one2many, &ng_one2many_typestruct);
177
178 /******************************************************************
179                     NETGRAPH NODE METHODS
180 ******************************************************************/
181
182 /*
183  * Node constructor
184  */
185 static int
186 ng_one2many_constructor(node_p node)
187 {
188         priv_p priv;
189
190         /* Allocate and initialize private info */
191         priv = kmalloc(sizeof(*priv), M_NETGRAPH,
192                        M_WAITOK | M_NULLOK | M_ZERO);
193         if (priv == NULL)
194                 return (ENOMEM);
195         priv->conf.xmitAlg = NG_ONE2MANY_XMIT_ROUNDROBIN;
196         priv->conf.failAlg = NG_ONE2MANY_FAIL_MANUAL;
197
198         /* cross reference */
199         NG_NODE_SET_PRIVATE(node, priv);
200         priv->node = node;
201
202         /* Done */
203         return (0);
204 }
205
206 /*
207  * Method for attaching a new hook
208  */
209 static  int
210 ng_one2many_newhook(node_p node, hook_p hook, const char *name)
211 {
212         const priv_p priv = NG_NODE_PRIVATE(node);
213         struct ng_one2many_link *link;
214         int linkNum;
215         u_long i;
216
217         /* Which hook? */
218         if (strncmp(name, NG_ONE2MANY_HOOK_MANY_PREFIX,
219             strlen(NG_ONE2MANY_HOOK_MANY_PREFIX)) == 0) {
220                 const char *cp;
221                 char *eptr;
222
223                 cp = name + strlen(NG_ONE2MANY_HOOK_MANY_PREFIX);
224                 if (!isdigit(*cp) || (cp[0] == '0' && cp[1] != '\0'))
225                         return (EINVAL);
226                 i = strtoul(cp, &eptr, 10);
227                 if (*eptr != '\0' || i < 0 || i >= NG_ONE2MANY_MAX_LINKS)
228                         return (EINVAL);
229                 linkNum = (int)i;
230                 link = &priv->many[linkNum];
231         } else if (strcmp(name, NG_ONE2MANY_HOOK_ONE) == 0) {
232                 linkNum = NG_ONE2MANY_ONE_LINKNUM;
233                 link = &priv->one;
234         } else
235                 return (EINVAL);
236
237         /* Is hook already connected? (should never happen) */
238         if (link->hook != NULL)
239                 return (EISCONN);
240
241         /* Setup private info for this link */
242         NG_HOOK_SET_PRIVATE(hook, (void *)(intptr_t)linkNum);
243         link->hook = hook;
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);
248         }
249
250         /* Done */
251         return (0);
252 }
253
254 /*
255  * Receive a control message
256  */
257 static int
258 ng_one2many_rcvmsg(node_p node, item_p item, hook_p lasthook)
259 {
260         const priv_p priv = NG_NODE_PRIVATE(node);
261         struct ng_mesg *resp = NULL;
262         int error = 0;
263         struct ng_mesg *msg;
264
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:
270                     {
271                         struct ng_one2many_config *conf;
272                         int i;
273
274                         /* Check that new configuration is valid */
275                         if (msg->header.arglen != sizeof(*conf)) {
276                                 error = EINVAL;
277                                 break;
278                         }
279                         conf = (struct ng_one2many_config *)msg->data;
280                         switch (conf->xmitAlg) {
281                         case NG_ONE2MANY_XMIT_ROUNDROBIN:
282                         case NG_ONE2MANY_XMIT_ALL:
283                                 break;
284                         default:
285                                 error = EINVAL;
286                                 break;
287                         }
288                         switch (conf->failAlg) {
289                         case NG_ONE2MANY_FAIL_MANUAL:
290                         case NG_ONE2MANY_FAIL_NOTIFY:
291                                 break;
292                         default:
293                                 error = EINVAL;
294                                 break;
295                         }
296                         if (error != 0)
297                                 break;
298
299                         /* Normalized many link enabled bits */ 
300                         for (i = 0; i < NG_ONE2MANY_MAX_LINKS; i++)
301                                 conf->enabledLinks[i] = !!conf->enabledLinks[i];
302
303                         /* Copy config and reset */
304                         bcopy(conf, &priv->conf, sizeof(*conf));
305                         ng_one2many_update_many(priv);
306                         break;
307                     }
308                 case NGM_ONE2MANY_GET_CONFIG:
309                     {
310                         struct ng_one2many_config *conf;
311
312                         NG_MKRESPONSE(resp, msg, sizeof(*conf), M_WAITOK | M_NULLOK);
313                         if (resp == NULL) {
314                                 error = ENOMEM;
315                                 break;
316                         }
317                         conf = (struct ng_one2many_config *)resp->data;
318                         bcopy(&priv->conf, conf, sizeof(priv->conf));
319                         break;
320                     }
321                 case NGM_ONE2MANY_GET_STATS:
322                 case NGM_ONE2MANY_CLR_STATS:
323                 case NGM_ONE2MANY_GETCLR_STATS:
324                     {
325                         struct ng_one2many_link *link;
326                         int linkNum;
327
328                         /* Get link */
329                         if (msg->header.arglen != sizeof(int32_t)) {
330                                 error = EINVAL;
331                                 break;
332                         }
333                         linkNum = *((int32_t *)msg->data);
334                         if (linkNum == NG_ONE2MANY_ONE_LINKNUM)
335                                 link = &priv->one;
336                         else if (linkNum >= 0
337                             && linkNum < NG_ONE2MANY_MAX_LINKS) {
338                                 link = &priv->many[linkNum];
339                         } else {
340                                 error = EINVAL;
341                                 break;
342                         }
343
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);
348                                 if (resp == NULL) {
349                                         error = ENOMEM;
350                                         break;
351                                 }
352                                 bcopy(&link->stats,
353                                     resp->data, sizeof(link->stats));
354                         }
355                         if (msg->header.cmd != NGM_ONE2MANY_GET_STATS)
356                                 bzero(&link->stats, sizeof(link->stats));
357                         break;
358                     }
359                 default:
360                         error = EINVAL;
361                         break;
362                 }
363                 break;
364         /*
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.
368          */
369         case NGM_FLOW_COOKIE:
370             {
371                 int linkNum;
372
373                 if (priv->conf.failAlg != NG_ONE2MANY_FAIL_NOTIFY)
374                         break;
375
376                 if (lasthook == NULL)
377                         break;
378
379                 linkNum = (intptr_t)NG_HOOK_PRIVATE(lasthook);
380                 if (linkNum == NG_ONE2MANY_ONE_LINKNUM)
381                         break;
382
383                 KASSERT((linkNum >= 0 && linkNum < NG_ONE2MANY_MAX_LINKS),
384                     ("%s: linkNum=%d", __func__, linkNum));
385
386                 switch (msg->header.cmd) {
387                 case NGM_LINK_IS_UP:
388                         priv->conf.enabledLinks[linkNum] = 1;
389                         ng_one2many_update_many(priv);
390                         break;
391                 case NGM_LINK_IS_DOWN:
392                         priv->conf.enabledLinks[linkNum] = 0;
393                         ng_one2many_update_many(priv);
394                         break;
395                 default:
396                         break;
397                 }
398                 break;
399             }
400         default:
401                 error = EINVAL;
402                 break;
403         }
404
405         /* Done */
406         NG_RESPOND_MSG(error, node, item, resp);
407         NG_FREE_MSG(msg);
408         return (error);
409 }
410
411 /*
412  * Receive data on a hook
413  */
414 static int
415 ng_one2many_rcvdata(hook_p hook, item_p item)
416 {
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;
421         int error = 0;
422         int linkNum;
423         int i;
424         struct mbuf *m;
425
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));
432
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));
437
438         /* Update receive stats */
439         src->stats.recvPackets++;
440         src->stats.recvOctets += m->m_pkthdr.len;
441
442         /* Figure out destination link */
443         if (linkNum == NG_ONE2MANY_ONE_LINKNUM) {
444                 if (priv->numActiveMany == 0) {
445                         NG_FREE_ITEM(item);
446                         return (ENOTCONN);
447                 }
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;
452                         break;
453                 case NG_ONE2MANY_XMIT_ALL:
454                         /* no need to copy data for the 1st one */
455                         dst = &priv->many[priv->activeMany[0]];
456
457                         /* make copies of data and send for all links
458                          * except the first one, which we'll do last 
459                          */
460                         for (i = 1; i < priv->numActiveMany; i++) {
461                                 struct mbuf *m2;
462                                 struct ng_one2many_link *mdst;
463
464                                 mdst = &priv->many[priv->activeMany[i]];
465                                 m2 = m_dup(m, MB_DONTWAIT);        /* XXX m_copypacket() */
466                                 if (m2 == NULL) {
467                                         mdst->stats.memoryFailures++;
468                                         NG_FREE_ITEM(item);
469                                         NG_FREE_M(m);
470                                         return (ENOBUFS);
471                                 }
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);
476                         }
477                         break;
478 #ifdef INVARIANTS
479                 default:
480                         panic("%s: invalid xmitAlg", __func__);
481 #endif
482                 }
483         } else {
484                 dst = &priv->one;
485         }
486
487         /* Update transmit stats */
488         dst->stats.xmitPackets++;
489         dst->stats.xmitOctets += m->m_pkthdr.len;
490
491         /* Deliver packet */
492         NG_FWD_ITEM_HOOK(error, item, dst->hook);
493         return (error);
494 }
495
496 /*
497  * Shutdown node
498  */
499 static int
500 ng_one2many_shutdown(node_p node)
501 {
502         const priv_p priv = NG_NODE_PRIVATE(node);
503
504         KASSERT(priv->numActiveMany == 0,
505             ("%s: numActiveMany=%d", __func__, priv->numActiveMany));
506         kfree(priv, M_NETGRAPH);
507         NG_NODE_SET_PRIVATE(node, NULL);
508         NG_NODE_UNREF(node);
509         return (0);
510 }
511
512 /*
513  * Hook disconnection.
514  */
515 static int
516 ng_one2many_disconnect(hook_p hook)
517 {
518         const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
519         int linkNum;
520
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));
526
527         /* Nuke the link */
528         if (linkNum == NG_ONE2MANY_ONE_LINKNUM)
529                 priv->one.hook = NULL;
530         else {
531                 priv->many[linkNum].hook = NULL;
532                 priv->conf.enabledLinks[linkNum] = 0;
533                 ng_one2many_update_many(priv);
534         }
535
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));
540         return (0);
541 }
542
543 /******************************************************************
544                         OTHER FUNCTIONS
545 ******************************************************************/
546
547 /*
548  * Update internal state after the addition or removal of a "many" link
549  */
550 static void
551 ng_one2many_update_many(priv_p priv)
552 {
553         uint16_t saveActive = priv->numActiveMany;
554         int linkNum;
555
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++;
566                         }
567                         break;
568 #ifdef INVARIANTS
569                 default:
570                         panic("%s: invalid failAlg", __func__);
571 #endif
572                 }
573         }
574
575         if (priv->numActiveMany == 0 && saveActive > 0)
576                 ng_one2many_notify(priv, NGM_LINK_IS_DOWN);
577
578         if (saveActive == 0 && priv->numActiveMany > 0)
579                 ng_one2many_notify(priv, NGM_LINK_IS_UP);
580
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;
586                 break;
587         case NG_ONE2MANY_XMIT_ALL:
588                 break;
589 #ifdef INVARIANTS
590         default:
591                 panic("%s: invalid xmitAlg", __func__);
592 #endif
593         }
594 }
595
596 /*
597  * Notify upstream if we are out of links, or we have at least one link.
598  */
599 static void
600 ng_one2many_notify(priv_p priv, uint32_t cmd)
601 {
602         struct ng_mesg *msg;
603         int dummy_error = 0;
604
605         if (priv->one.hook == NULL)
606                 return;
607
608         NG_MKMESSAGE(msg, NGM_FLOW_COOKIE, cmd, 0, M_WAITOK | M_NULLOK);
609         if (msg != NULL)
610                 NG_SEND_MSG_HOOK(dummy_error, priv->node, msg, priv->one.hook, 0);
611 }