]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - sys/netgraph/ng_one2many.c
Add missed mergeinfo.
[FreeBSD/stable/8.git] / sys / netgraph / 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$
41  */
42
43 /*
44  * ng_one2many(4) netgraph node type
45  *
46  * Packets received on the "one" hook are sent out each of the
47  * "many" hooks accoring to an algorithm. Packets received on any
48  * "many" hook are always delivered to the "one" hook.
49  */
50
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/kernel.h>
54 #include <sys/malloc.h>
55 #include <sys/ctype.h>
56 #include <sys/mbuf.h>
57 #include <sys/errno.h>
58
59 #include <netgraph/ng_message.h>
60 #include <netgraph/netgraph.h>
61 #include <netgraph/ng_parse.h>
62 #include <netgraph/ng_one2many.h>
63
64 /* Per-link private data */
65 struct ng_one2many_link {
66         hook_p                          hook;   /* netgraph hook */
67         struct ng_one2many_link_stats   stats;  /* link stats */
68 };
69
70 /* Per-node private data */
71 struct ng_one2many_private {
72         node_p                          node;           /* link to node */
73         struct ng_one2many_config       conf;           /* node configuration */
74         struct ng_one2many_link         one;            /* "one" hook */
75         struct ng_one2many_link         many[NG_ONE2MANY_MAX_LINKS];
76         u_int16_t                       nextMany;       /* next round-robin */
77         u_int16_t                       numActiveMany;  /* # active "many" */
78         u_int16_t                       activeMany[NG_ONE2MANY_MAX_LINKS];
79 };
80 typedef struct ng_one2many_private *priv_p;
81
82 /* Netgraph node methods */
83 static ng_constructor_t ng_one2many_constructor;
84 static ng_rcvmsg_t      ng_one2many_rcvmsg;
85 static ng_shutdown_t    ng_one2many_shutdown;
86 static ng_newhook_t     ng_one2many_newhook;
87 static ng_rcvdata_t     ng_one2many_rcvdata;
88 static ng_disconnect_t  ng_one2many_disconnect;
89
90 /* Other functions */
91 static void             ng_one2many_update_many(priv_p priv);
92 static void             ng_one2many_notify(priv_p priv, uint32_t cmd);
93
94 /******************************************************************
95                     NETGRAPH PARSE TYPES
96 ******************************************************************/
97
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
103 };
104 static const struct ng_parse_type ng_one2many_enableLinks_array_type = {
105         &ng_parse_fixedarray_type,
106         &ng_one2many_enableLinks_array_type_info,
107 };
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
113 };
114
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
121 };
122
123 /* List of commands and how to convert arguments to/from ASCII */
124 static const struct ng_cmdlist ng_one2many_cmdlist[] = {
125         {
126           NGM_ONE2MANY_COOKIE,
127           NGM_ONE2MANY_SET_CONFIG,
128           "setconfig",
129           &ng_one2many_config_type,
130           NULL
131         },
132         {
133           NGM_ONE2MANY_COOKIE,
134           NGM_ONE2MANY_GET_CONFIG,
135           "getconfig",
136           NULL,
137           &ng_one2many_config_type
138         },
139         {
140           NGM_ONE2MANY_COOKIE,
141           NGM_ONE2MANY_GET_STATS,
142           "getstats",
143           &ng_parse_int32_type,
144           &ng_one2many_link_stats_type
145         },
146         {
147           NGM_ONE2MANY_COOKIE,
148           NGM_ONE2MANY_CLR_STATS,
149           "clrstats",
150           &ng_parse_int32_type,
151           NULL,
152         },
153         {
154           NGM_ONE2MANY_COOKIE,
155           NGM_ONE2MANY_GETCLR_STATS,
156           "getclrstats",
157           &ng_parse_int32_type,
158           &ng_one2many_link_stats_type
159         },
160         { 0 }
161 };
162
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,
174 };
175 NETGRAPH_INIT(one2many, &ng_one2many_typestruct);
176
177 /******************************************************************
178                     NETGRAPH NODE METHODS
179 ******************************************************************/
180
181 /*
182  * Node constructor
183  */
184 static int
185 ng_one2many_constructor(node_p node)
186 {
187         priv_p priv;
188
189         /* Allocate and initialize private info */
190         priv = malloc(sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO);
191         if (priv == NULL)
192                 return (ENOMEM);
193         priv->conf.xmitAlg = NG_ONE2MANY_XMIT_ROUNDROBIN;
194         priv->conf.failAlg = NG_ONE2MANY_FAIL_MANUAL;
195
196         /* cross reference */
197         NG_NODE_SET_PRIVATE(node, priv);
198         priv->node = node;
199
200         /* Done */
201         return (0);
202 }
203
204 /*
205  * Method for attaching a new hook
206  */
207 static  int
208 ng_one2many_newhook(node_p node, hook_p hook, const char *name)
209 {
210         const priv_p priv = NG_NODE_PRIVATE(node);
211         struct ng_one2many_link *link;
212         int linkNum;
213         u_long i;
214
215         /* Which hook? */
216         if (strncmp(name, NG_ONE2MANY_HOOK_MANY_PREFIX,
217             strlen(NG_ONE2MANY_HOOK_MANY_PREFIX)) == 0) {
218                 const char *cp;
219                 char *eptr;
220
221                 cp = name + strlen(NG_ONE2MANY_HOOK_MANY_PREFIX);
222                 if (!isdigit(*cp) || (cp[0] == '0' && cp[1] != '\0'))
223                         return (EINVAL);
224                 i = strtoul(cp, &eptr, 10);
225                 if (*eptr != '\0' || i < 0 || i >= NG_ONE2MANY_MAX_LINKS)
226                         return (EINVAL);
227                 linkNum = (int)i;
228                 link = &priv->many[linkNum];
229         } else if (strcmp(name, NG_ONE2MANY_HOOK_ONE) == 0) {
230                 linkNum = NG_ONE2MANY_ONE_LINKNUM;
231                 link = &priv->one;
232         } else
233                 return (EINVAL);
234
235         /* Is hook already connected? (should never happen) */
236         if (link->hook != NULL)
237                 return (EISCONN);
238
239         /* Setup private info for this link */
240         NG_HOOK_SET_PRIVATE(hook, (void *)(intptr_t)linkNum);
241         link->hook = hook;
242         bzero(&link->stats, sizeof(link->stats));
243         if (linkNum != NG_ONE2MANY_ONE_LINKNUM) {
244                 priv->conf.enabledLinks[linkNum] = 1;   /* auto-enable link */
245                 ng_one2many_update_many(priv);
246         }
247
248         /* Done */
249         return (0);
250 }
251
252 /*
253  * Receive a control message
254  */
255 static int
256 ng_one2many_rcvmsg(node_p node, item_p item, hook_p lasthook)
257 {
258         const priv_p priv = NG_NODE_PRIVATE(node);
259         struct ng_mesg *resp = NULL;
260         int error = 0;
261         struct ng_mesg *msg;
262
263         NGI_GET_MSG(item, msg);
264         switch (msg->header.typecookie) {
265         case NGM_ONE2MANY_COOKIE:
266                 switch (msg->header.cmd) {
267                 case NGM_ONE2MANY_SET_CONFIG:
268                     {
269                         struct ng_one2many_config *conf;
270                         int i;
271
272                         /* Check that new configuration is valid */
273                         if (msg->header.arglen != sizeof(*conf)) {
274                                 error = EINVAL;
275                                 break;
276                         }
277                         conf = (struct ng_one2many_config *)msg->data;
278                         switch (conf->xmitAlg) {
279                         case NG_ONE2MANY_XMIT_ROUNDROBIN:
280                         case NG_ONE2MANY_XMIT_ALL:
281                         case NG_ONE2MANY_XMIT_FAILOVER:
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_NOWAIT);
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_NOWAIT);
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, M_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                 case NG_ONE2MANY_XMIT_FAILOVER:
478                         dst = &priv->many[priv->activeMany[0]];
479                         break;
480 #ifdef INVARIANTS
481                 default:
482                         panic("%s: invalid xmitAlg", __func__);
483 #endif
484                 }
485         } else {
486                 dst = &priv->one;
487         }
488
489         /* Update transmit stats */
490         dst->stats.xmitPackets++;
491         dst->stats.xmitOctets += m->m_pkthdr.len;
492
493         /* Deliver packet */
494         NG_FWD_ITEM_HOOK(error, item, dst->hook);
495         return (error);
496 }
497
498 /*
499  * Shutdown node
500  */
501 static int
502 ng_one2many_shutdown(node_p node)
503 {
504         const priv_p priv = NG_NODE_PRIVATE(node);
505
506         KASSERT(priv->numActiveMany == 0,
507             ("%s: numActiveMany=%d", __func__, priv->numActiveMany));
508         free(priv, M_NETGRAPH);
509         NG_NODE_SET_PRIVATE(node, NULL);
510         NG_NODE_UNREF(node);
511         return (0);
512 }
513
514 /*
515  * Hook disconnection.
516  */
517 static int
518 ng_one2many_disconnect(hook_p hook)
519 {
520         const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
521         int linkNum;
522
523         /* Get link number */
524         linkNum = (intptr_t)NG_HOOK_PRIVATE(hook);
525         KASSERT(linkNum == NG_ONE2MANY_ONE_LINKNUM
526             || (linkNum >= 0 && linkNum < NG_ONE2MANY_MAX_LINKS),
527             ("%s: linkNum=%d", __func__, linkNum));
528
529         /* Nuke the link */
530         if (linkNum == NG_ONE2MANY_ONE_LINKNUM)
531                 priv->one.hook = NULL;
532         else {
533                 priv->many[linkNum].hook = NULL;
534                 priv->conf.enabledLinks[linkNum] = 0;
535                 ng_one2many_update_many(priv);
536         }
537
538         /* If no hooks left, go away */
539         if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
540         && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook))))
541                 ng_rmnode_self(NG_HOOK_NODE(hook));
542         return (0);
543 }
544
545 /******************************************************************
546                         OTHER FUNCTIONS
547 ******************************************************************/
548
549 /*
550  * Update internal state after the addition or removal of a "many" link
551  */
552 static void
553 ng_one2many_update_many(priv_p priv)
554 {
555         uint16_t saveActive = priv->numActiveMany;
556         int linkNum;
557
558         /* Update list of which "many" links are up */
559         priv->numActiveMany = 0;
560         for (linkNum = 0; linkNum < NG_ONE2MANY_MAX_LINKS; linkNum++) {
561                 switch (priv->conf.failAlg) {
562                 case NG_ONE2MANY_FAIL_MANUAL:
563                 case NG_ONE2MANY_FAIL_NOTIFY:
564                         if (priv->many[linkNum].hook != NULL
565                             && priv->conf.enabledLinks[linkNum]) {
566                                 priv->activeMany[priv->numActiveMany] = linkNum;
567                                 priv->numActiveMany++;
568                         }
569                         break;
570 #ifdef INVARIANTS
571                 default:
572                         panic("%s: invalid failAlg", __func__);
573 #endif
574                 }
575         }
576
577         if (priv->numActiveMany == 0 && saveActive > 0)
578                 ng_one2many_notify(priv, NGM_LINK_IS_DOWN);
579
580         if (saveActive == 0 && priv->numActiveMany > 0)
581                 ng_one2many_notify(priv, NGM_LINK_IS_UP);
582
583         /* Update transmit algorithm state */
584         switch (priv->conf.xmitAlg) {
585         case NG_ONE2MANY_XMIT_ROUNDROBIN:
586                 if (priv->numActiveMany > 0)
587                         priv->nextMany %= priv->numActiveMany;
588                 break;
589         case NG_ONE2MANY_XMIT_ALL:
590         case NG_ONE2MANY_XMIT_FAILOVER:
591                 break;
592 #ifdef INVARIANTS
593         default:
594                 panic("%s: invalid xmitAlg", __func__);
595 #endif
596         }
597 }
598
599 /*
600  * Notify upstream if we are out of links, or we have at least one link.
601  */
602 static void
603 ng_one2many_notify(priv_p priv, uint32_t cmd)
604 {
605         struct ng_mesg *msg;
606         int dummy_error = 0;
607
608         if (priv->one.hook == NULL)
609                 return;
610
611         NG_MKMESSAGE(msg, NGM_FLOW_COOKIE, cmd, 0, M_NOWAIT);
612         if (msg != NULL)
613                 NG_SEND_MSG_HOOK(dummy_error, priv->node, msg, priv->one.hook, 0);
614 }