]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netgraph/ng_one2many.c
Disable the EC GPE in the shutdown path. This is correct but is not known
[FreeBSD/FreeBSD.git] / sys / netgraph / ng_one2many.c
1
2 /*
3  * ng_one2many.c
4  *
5  * Copyright (c) 2000 Whistle Communications, Inc.
6  * All rights reserved.
7  * 
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.
18  * 
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
35  * OF SUCH DAMAGE.
36  *
37  * Author: Archie Cobbs <archie@freebsd.org>
38  *
39  * $FreeBSD$
40  */
41
42 /*
43  * ng_one2many(4) netgraph node type
44  *
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.
48  */
49
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>
55 #include <sys/mbuf.h>
56 #include <sys/errno.h>
57
58 #include <netgraph/ng_message.h>
59 #include <netgraph/netgraph.h>
60 #include <netgraph/ng_parse.h>
61 #include <netgraph/ng_one2many.h>
62
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 */
67 };
68
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];
77 };
78 typedef struct ng_one2many_private *priv_p;
79
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;
87
88 /* Other functions */
89 static void             ng_one2many_update_many(priv_p priv);
90
91 /* Store each hook's link number in the private field */
92 #define LINK_NUM(hook)          (*(int16_t *)(&(hook)->private))
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         MALLOC(priv, priv_p, 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         NG_NODE_SET_PRIVATE(node, priv);
197
198         /* Done */
199         return (0);
200 }
201
202 /*
203  * Method for attaching a new hook
204  */
205 static  int
206 ng_one2many_newhook(node_p node, hook_p hook, const char *name)
207 {
208         const priv_p priv = NG_NODE_PRIVATE(node);
209         struct ng_one2many_link *link;
210         int linkNum;
211         u_long i;
212
213         /* Which hook? */
214         if (strncmp(name, NG_ONE2MANY_HOOK_MANY_PREFIX,
215             strlen(NG_ONE2MANY_HOOK_MANY_PREFIX)) == 0) {
216                 const char *cp;
217                 char *eptr;
218
219                 cp = name + strlen(NG_ONE2MANY_HOOK_MANY_PREFIX);
220                 if (!isdigit(*cp) || (cp[0] == '0' && cp[1] != '\0'))
221                         return (EINVAL);
222                 i = strtoul(cp, &eptr, 10);
223                 if (*eptr != '\0' || i < 0 || i >= NG_ONE2MANY_MAX_LINKS)
224                         return (EINVAL);
225                 linkNum = (int)i;
226                 link = &priv->many[linkNum];
227         } else if (strcmp(name, NG_ONE2MANY_HOOK_ONE) == 0) {
228                 linkNum = NG_ONE2MANY_ONE_LINKNUM;
229                 link = &priv->one;
230         } else
231                 return (EINVAL);
232
233         /* Is hook already connected? (should never happen) */
234         if (link->hook != NULL)
235                 return (EISCONN);
236
237         /* Setup private info for this link */
238         NG_HOOK_SET_PRIVATE(hook, (void *)(intptr_t)linkNum);
239         link->hook = hook;
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);
244         }
245
246         /* Done */
247         return (0);
248 }
249
250 /*
251  * Receive a control message
252  */
253 static int
254 ng_one2many_rcvmsg(node_p node, item_p item, hook_p lasthook)
255 {
256         const priv_p priv = NG_NODE_PRIVATE(node);
257         struct ng_mesg *resp = NULL;
258         int error = 0;
259         struct ng_mesg *msg;
260
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:
266                     {
267                         struct ng_one2many_config *conf;
268                         int i;
269
270                         /* Check that new configuration is valid */
271                         if (msg->header.arglen != sizeof(*conf)) {
272                                 error = EINVAL;
273                                 break;
274                         }
275                         conf = (struct ng_one2many_config *)msg->data;
276                         switch (conf->xmitAlg) {
277                         case NG_ONE2MANY_XMIT_ROUNDROBIN:
278                         case NG_ONE2MANY_XMIT_ALL:
279                                 break;
280                         default:
281                                 error = EINVAL;
282                                 break;
283                         }
284                         switch (conf->failAlg) {
285                         case NG_ONE2MANY_FAIL_MANUAL:
286                                 break;
287                         default:
288                                 error = EINVAL;
289                                 break;
290                         }
291                         if (error != 0)
292                                 break;
293
294                         /* Normalized many link enabled bits */ 
295                         for (i = 0; i < NG_ONE2MANY_MAX_LINKS; i++)
296                                 conf->enabledLinks[i] = !!conf->enabledLinks[i];
297
298                         /* Copy config and reset */
299                         bcopy(conf, &priv->conf, sizeof(*conf));
300                         ng_one2many_update_many(priv);
301                         break;
302                     }
303                 case NGM_ONE2MANY_GET_CONFIG:
304                     {
305                         struct ng_one2many_config *conf;
306
307                         NG_MKRESPONSE(resp, msg, sizeof(*conf), M_NOWAIT);
308                         if (resp == NULL) {
309                                 error = ENOMEM;
310                                 break;
311                         }
312                         conf = (struct ng_one2many_config *)resp->data;
313                         bcopy(&priv->conf, conf, sizeof(priv->conf));
314                         break;
315                     }
316                 case NGM_ONE2MANY_GET_STATS:
317                 case NGM_ONE2MANY_CLR_STATS:
318                 case NGM_ONE2MANY_GETCLR_STATS:
319                     {
320                         struct ng_one2many_link *link;
321                         int linkNum;
322
323                         /* Get link */
324                         if (msg->header.arglen != sizeof(int32_t)) {
325                                 error = EINVAL;
326                                 break;
327                         }
328                         linkNum = *((int32_t *)msg->data);
329                         if (linkNum == NG_ONE2MANY_ONE_LINKNUM)
330                                 link = &priv->one;
331                         else if (linkNum == 0
332                             && linkNum < NG_ONE2MANY_MAX_LINKS) {
333                                 link = &priv->many[linkNum];
334                         } else {
335                                 error = EINVAL;
336                                 break;
337                         }
338
339                         /* Get/clear stats */
340                         if (msg->header.cmd != NGM_ONE2MANY_CLR_STATS) {
341                                 NG_MKRESPONSE(resp, msg,
342                                     sizeof(link->stats), M_NOWAIT);
343                                 if (resp == NULL) {
344                                         error = ENOMEM;
345                                         break;
346                                 }
347                                 bcopy(&link->stats,
348                                     resp->data, sizeof(link->stats));
349                         }
350                         if (msg->header.cmd != NGM_ONE2MANY_GET_STATS)
351                                 bzero(&link->stats, sizeof(link->stats));
352                         break;
353                     }
354                 default:
355                         error = EINVAL;
356                         break;
357                 }
358                 break;
359         default:
360                 error = EINVAL;
361                 break;
362         }
363
364         /* Done */
365         NG_RESPOND_MSG(error, node, item, resp);
366         NG_FREE_MSG(msg);
367         return (error);
368 }
369
370 /*
371  * Receive data on a hook
372  */
373 static int
374 ng_one2many_rcvdata(hook_p hook, item_p item)
375 {
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;
380         int error = 0;
381         int linkNum;
382         int i;
383         struct mbuf *m;
384
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));
391
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));
396
397         /* Update receive stats */
398         src->stats.recvPackets++;
399         src->stats.recvOctets += m->m_pkthdr.len;
400
401         /* Figure out destination link */
402         if (linkNum == NG_ONE2MANY_ONE_LINKNUM) {
403                 if (priv->numActiveMany == 0) {
404                         NG_FREE_ITEM(item);
405                         return (ENOTCONN);
406                 }
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;
411                         break;
412                 case NG_ONE2MANY_XMIT_ALL:
413                         /* no need to copy data for the 1st one */
414                         dst = &priv->many[priv->activeMany[0]];
415
416                         /* make copies of data and send for all links
417                          * except the first one, which we'll do last 
418                          */
419                         for (i = 1; i < priv->numActiveMany; i++) {
420                                 struct mbuf *m2;
421                                 struct ng_one2many_link *mdst;
422
423                                 mdst = &priv->many[priv->activeMany[i]];
424                                 m2 = m_dup(m, M_DONTWAIT);        /* XXX m_copypacket() */
425                                 if (m2 == NULL) {
426                                         mdst->stats.memoryFailures++;
427                                         NG_FREE_ITEM(item);
428                                         NG_FREE_M(m);
429                                         return (ENOBUFS);
430                                 }
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);
435                         }
436                         break;
437 #ifdef INVARIANTS
438                 default:
439                         panic("%s: invalid xmitAlg", __func__);
440 #endif
441                 }
442         } else {
443                 dst = &priv->one;
444         }
445
446         /* Update transmit stats */
447         dst->stats.xmitPackets++;
448         dst->stats.xmitOctets += m->m_pkthdr.len;
449
450         /* Deliver packet */
451         NG_FWD_ITEM_HOOK(error, item, dst->hook);
452         return (error);
453 }
454
455 /*
456  * Shutdown node
457  */
458 static int
459 ng_one2many_shutdown(node_p node)
460 {
461         const priv_p priv = NG_NODE_PRIVATE(node);
462
463         KASSERT(priv->numActiveMany == 0,
464             ("%s: numActiveMany=%d", __func__, priv->numActiveMany));
465         FREE(priv, M_NETGRAPH);
466         NG_NODE_SET_PRIVATE(node, NULL);
467         NG_NODE_UNREF(node);
468         return (0);
469 }
470
471 /*
472  * Hook disconnection.
473  */
474 static int
475 ng_one2many_disconnect(hook_p hook)
476 {
477         const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
478         int linkNum;
479
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));
485
486         /* Nuke the link */
487         if (linkNum == NG_ONE2MANY_ONE_LINKNUM)
488                 priv->one.hook = NULL;
489         else {
490                 priv->many[linkNum].hook = NULL;
491                 priv->conf.enabledLinks[linkNum] = 0;
492                 ng_one2many_update_many(priv);
493         }
494
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));
499         return (0);
500 }
501
502 /******************************************************************
503                         OTHER FUNCTIONS
504 ******************************************************************/
505
506 /*
507  * Update internal state after the addition or removal of a "many" link
508  */
509 static void
510 ng_one2many_update_many(priv_p priv)
511 {
512         int linkNum;
513
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++;
523                         }
524                         break;
525 #ifdef INVARIANTS
526                 default:
527                         panic("%s: invalid failAlg", __func__);
528 #endif
529                 }
530         }
531
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;
537                 break;
538         case NG_ONE2MANY_XMIT_ALL:
539                 break;
540 #ifdef INVARIANTS
541         default:
542                 panic("%s: invalid xmitAlg", __func__);
543 #endif
544         }
545 }
546
547