Merge branch 'vendor/OPENSSH'
[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         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;
196
197         /* cross reference */
198         NG_NODE_SET_PRIVATE(node, priv);
199         priv->node = node;
200
201         /* Done */
202         return (0);
203 }
204
205 /*
206  * Method for attaching a new hook
207  */
208 static  int
209 ng_one2many_newhook(node_p node, hook_p hook, const char *name)
210 {
211         const priv_p priv = NG_NODE_PRIVATE(node);
212         struct ng_one2many_link *link;
213         int linkNum;
214         u_long i;
215
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;
221
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);
235
236         /* Is hook already connected? (should never happen) */
237         if (link->hook != NULL)
238                 return (EISCONN);
239
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);
247         }
248
249         /* Done */
250         return (0);
251 }
252
253 /*
254  * Receive a control message
255  */
256 static int
257 ng_one2many_rcvmsg(node_p node, item_p item, hook_p lasthook)
258 {
259         const priv_p priv = NG_NODE_PRIVATE(node);
260         struct ng_mesg *resp = NULL;
261         int error = 0;
262         struct ng_mesg *msg;
263
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:
269                     {
270                         struct ng_one2many_config *conf;
271                         int i;
272
273                         /* Check that new configuration is valid */
274                         if (msg->header.arglen != sizeof(*conf)) {
275                                 error = EINVAL;
276                                 break;
277                         }
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;
286                         }
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;
294                         }
295                         if (error != 0)
296                                 break;
297
298                         /* Normalized many link enabled bits */ 
299                         for (i = 0; i < NG_ONE2MANY_MAX_LINKS; i++)
300                                 conf->enabledLinks[i] = !!conf->enabledLinks[i];
301
302                         /* Copy config and reset */
303                         bcopy(conf, &priv->conf, sizeof(*conf));
304                         ng_one2many_update_many(priv);
305                         break;
306                     }
307                 case NGM_ONE2MANY_GET_CONFIG:
308                     {
309                         struct ng_one2many_config *conf;
310
311                         NG_MKRESPONSE(resp, msg, sizeof(*conf), M_WAITOK | M_NULLOK);
312                         if (resp == NULL) {
313                                 error = ENOMEM;
314                                 break;
315                         }
316                         conf = (struct ng_one2many_config *)resp->data;
317                         bcopy(&priv->conf, conf, sizeof(priv->conf));
318                         break;
319                     }
320                 case NGM_ONE2MANY_GET_STATS:
321                 case NGM_ONE2MANY_CLR_STATS:
322                 case NGM_ONE2MANY_GETCLR_STATS:
323                     {
324                         struct ng_one2many_link *link;
325                         int linkNum;
326
327                         /* Get link */
328                         if (msg->header.arglen != sizeof(int32_t)) {
329                                 error = EINVAL;
330                                 break;
331                         }
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;
341                         }
342
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;
350                                 }
351                                 bcopy(&link->stats,
352                                     resp->data, sizeof(link->stats));
353                         }
354                         if (msg->header.cmd != NGM_ONE2MANY_GET_STATS)
355                                 bzero(&link->stats, sizeof(link->stats));
356                         break;
357                     }
358                 default:
359                         error = EINVAL;
360                         break;
361                 }
362                 break;
363         /*
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.
367          */
368         case NGM_FLOW_COOKIE:
369             {
370                 int linkNum;
371
372                 if (priv->conf.failAlg != NG_ONE2MANY_FAIL_NOTIFY)
373                         break;
374
375                 if (lasthook == NULL)
376                         break;
377
378                 linkNum = (intptr_t)NG_HOOK_PRIVATE(lasthook);
379                 if (linkNum == NG_ONE2MANY_ONE_LINKNUM)
380                         break;
381
382                 KASSERT((linkNum >= 0 && linkNum < NG_ONE2MANY_MAX_LINKS),
383                     ("%s: linkNum=%d", __func__, linkNum));
384
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;
396                 }
397                 break;
398             }
399         default:
400                 error = EINVAL;
401                 break;
402         }
403
404         /* Done */
405         NG_RESPOND_MSG(error, node, item, resp);
406         NG_FREE_MSG(msg);
407         return (error);
408 }
409
410 /*
411  * Receive data on a hook
412  */
413 static int
414 ng_one2many_rcvdata(hook_p hook, item_p item)
415 {
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;
424
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));
431
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));
436
437         /* Update receive stats */
438         src->stats.recvPackets++;
439         src->stats.recvOctets += m->m_pkthdr.len;
440
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);
446                 }
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]];
455
456                         /* make copies of data and send for all links
457                          * except the first one, which we'll do last 
458                          */
459                         for (i = 1; i < priv->numActiveMany; i++) {
460                                 struct mbuf *m2;
461                                 struct ng_one2many_link *mdst;
462
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);
470                                 }
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);
475                         }
476                         break;
477 #ifdef INVARIANTS
478                 default:
479                         panic("%s: invalid xmitAlg", __func__);
480 #endif
481                 }
482         } else {
483                 dst = &priv->one;
484         }
485
486         /* Update transmit stats */
487         dst->stats.xmitPackets++;
488         dst->stats.xmitOctets += m->m_pkthdr.len;
489
490         /* Deliver packet */
491         NG_FWD_ITEM_HOOK(error, item, dst->hook);
492         return (error);
493 }
494
495 /*
496  * Shutdown node
497  */
498 static int
499 ng_one2many_shutdown(node_p node)
500 {
501         const priv_p priv = NG_NODE_PRIVATE(node);
502
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);
509 }
510
511 /*
512  * Hook disconnection.
513  */
514 static int
515 ng_one2many_disconnect(hook_p hook)
516 {
517         const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
518         int linkNum;
519
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));
525
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);
533         }
534
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);
540 }
541
542 /******************************************************************
543                         OTHER FUNCTIONS
544 ******************************************************************/
545
546 /*
547  * Update internal state after the addition or removal of a "many" link
548  */
549 static void
550 ng_one2many_update_many(priv_p priv)
551 {
552         uint16_t saveActive = priv->numActiveMany;
553         int linkNum;
554
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++;
565                         }
566                         break;
567 #ifdef INVARIANTS
568                 default:
569                         panic("%s: invalid failAlg", __func__);
570 #endif
571                 }
572         }
573
574         if (priv->numActiveMany == 0 && saveActive > 0)
575                 ng_one2many_notify(priv, NGM_LINK_IS_DOWN);
576
577         if (saveActive == 0 && priv->numActiveMany > 0)
578                 ng_one2many_notify(priv, NGM_LINK_IS_UP);
579
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
592         }
593 }
594
595 /*
596  * Notify upstream if we are out of links, or we have at least one link.
597  */
598 static void
599 ng_one2many_notify(priv_p priv, uint32_t cmd)
600 {
601         struct ng_mesg *msg;
602         int dummy_error = 0;
603
604         if (priv->one.hook == NULL)
605                 return;
606
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);
610 }