For kmalloc(), MALLOC() and contigmalloc(), use M_ZERO instead of
[dragonfly.git] / sys / netgraph / ng_sample.c
1
2 /*
3  * ng_sample.c
4  *
5  * Copyright (c) 1996-1999 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: Julian Elischer <julian@freebsd.org>
38  *
39  * $FreeBSD: src/sys/netgraph/ng_sample.c,v 1.7.2.3 2002/07/02 23:44:03 archie Exp $
40  * $DragonFly: src/sys/netgraph/ng_sample.c,v 1.4 2008/01/05 14:02:38 swildner Exp $
41  * $Whistle: ng_sample.c,v 1.13 1999/11/01 09:24:52 julian Exp $
42  */
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/mbuf.h>
48 #include <sys/malloc.h>
49 #include <sys/ctype.h>
50 #include <sys/errno.h>
51 #include <sys/syslog.h>
52
53 #include <netgraph/ng_message.h>
54 #include <netgraph/ng_parse.h>
55 #include <netgraph/ng_sample.h>
56 #include <netgraph/netgraph.h>
57
58 /*
59  * This section contains the netgraph method declarations for the
60  * sample node. These methods define the netgraph 'type'.
61  */
62
63 static ng_constructor_t ng_xxx_constructor;
64 static ng_rcvmsg_t      ng_xxx_rcvmsg;
65 static ng_shutdown_t    ng_xxx_rmnode;
66 static ng_newhook_t     ng_xxx_newhook;
67 static ng_connect_t     ng_xxx_connect;
68 static ng_rcvdata_t     ng_xxx_rcvdata;  /* note these are both ng_rcvdata_t */
69 static ng_rcvdata_t     ng_xxx_rcvdataq; /* note these are both ng_rcvdata_t */
70 static ng_disconnect_t  ng_xxx_disconnect;
71
72 /* Parse type for struct ngxxxstat */
73 static const struct ng_parse_struct_field ng_xxx_stat_type_fields[]
74         = NG_XXX_STATS_TYPE_INFO;
75 static const struct ng_parse_type ng_xxx_stat_type = {
76         &ng_parse_struct_type,
77         &ng_xxx_stat_type_fields
78 };
79
80 /* List of commands and how to convert arguments to/from ASCII */
81 static const struct ng_cmdlist ng_xxx_cmdlist[] = {
82         {
83           NGM_XXX_COOKIE,
84           NGM_XXX_GET_STATUS,
85           "getstatus",
86           NULL,
87           &ng_xxx_stat_type,
88         },
89         {
90           NGM_XXX_COOKIE,
91           NGM_XXX_SET_FLAG,
92           "setflag",
93           &ng_parse_int32_type,
94           NULL
95         },
96         { 0 }
97 };
98
99 /* Netgraph node type descriptor */
100 static struct ng_type typestruct = {
101         NG_VERSION,
102         NG_XXX_NODE_TYPE,
103         NULL,
104         ng_xxx_constructor,
105         ng_xxx_rcvmsg,
106         ng_xxx_rmnode,
107         ng_xxx_newhook,
108         NULL,
109         ng_xxx_connect,
110         ng_xxx_rcvdata,
111         ng_xxx_rcvdataq,
112         ng_xxx_disconnect,
113         ng_xxx_cmdlist
114 };
115 NETGRAPH_INIT(xxx, &typestruct);
116
117 /* Information we store for each hook on each node */
118 struct XXX_hookinfo {
119         int     dlci;           /* The DLCI it represents, -1 == downstream */
120         int     channel;        /* The channel representing this DLCI */
121         hook_p  hook;
122 };
123
124 /* Information we store for each node */
125 struct XXX {
126         struct XXX_hookinfo channel[XXX_NUM_DLCIS];
127         struct XXX_hookinfo downstream_hook;
128         node_p          node;           /* back pointer to node */
129         hook_p          debughook;
130         u_int           packets_in;     /* packets in from downstream */
131         u_int           packets_out;    /* packets out towards downstream */
132         u_int32_t       flags;
133 };
134 typedef struct XXX *xxx_p;
135
136 /*
137  * Allocate the private data structure and the generic node
138  * and link them together.
139  *
140  * ng_make_node_common() returns with a generic node struct
141  * with a single reference for us.. we transfer it to the
142  * private structure.. when we free the private struct we must
143  * unref the node so it gets freed too.
144  *
145  * If this were a device node than this work would be done in the attach()
146  * routine and the constructor would return EINVAL as you should not be able
147  * to creatednodes that depend on hardware (unless you can add the hardware :)
148  */
149 static int
150 ng_xxx_constructor(node_p *nodep)
151 {
152         xxx_p privdata;
153         int i, error;
154
155         /* Initialize private descriptor */
156         MALLOC(privdata, xxx_p, sizeof(*privdata), M_NETGRAPH, M_NOWAIT | M_ZERO);
157         if (privdata == NULL)
158                 return (ENOMEM);
159         for (i = 0; i < XXX_NUM_DLCIS; i++) {
160                 privdata->channel[i].dlci = -2;
161                 privdata->channel[i].channel = i;
162         }
163
164         /* Call the 'generic' (ie, superclass) node constructor */
165         if ((error = ng_make_node_common(&typestruct, nodep))) {
166                 FREE(privdata, M_NETGRAPH);
167                 return (error);
168         }
169
170         /* Link structs together; this counts as our one reference to *nodep */
171         (*nodep)->private = privdata;
172         privdata->node = *nodep;
173         return (0);
174 }
175
176 /*
177  * Give our ok for a hook to be added...
178  * If we are not running this might kick a device into life.
179  * Possibly decode information out of the hook name.
180  * Add the hook's private info to the hook structure.
181  * (if we had some). In this example, we assume that there is a
182  * an array of structs, called 'channel' in the private info,
183  * one for each active channel. The private
184  * pointer of each hook points to the appropriate XXX_hookinfo struct
185  * so that the source of an input packet is easily identified.
186  * (a dlci is a frame relay channel)
187  */
188 static int
189 ng_xxx_newhook(node_p node, hook_p hook, const char *name)
190 {
191         const xxx_p xxxp = node->private;
192         const char *cp;
193         int dlci = 0;
194         int chan;
195
196 #if 0
197         /* Possibly start up the device if it's not already going */
198         if ((xxxp->flags & SCF_RUNNING) == 0) {
199                 ng_xxx_start_hardware(xxxp);
200         }
201 #endif
202
203         /* Example of how one might use hooks with embedded numbers: All
204          * hooks start with 'dlci' and have a decimal trailing channel
205          * number up to 4 digits Use the leadin defined int he associated .h
206          * file. */
207         if (strncmp(name,
208             NG_XXX_HOOK_DLCI_LEADIN, strlen(NG_XXX_HOOK_DLCI_LEADIN)) == 0) {
209                 char *eptr;
210
211                 cp = name + sizeof(NG_XXX_HOOK_DLCI_LEADIN);
212                 if (!isdigit(*cp) || (cp[0] == '0' && cp[1] != '\0'))
213                         return (EINVAL);
214                 dlci = (int)strtoul(cp, &eptr, 10);
215                 if (*eptr != '\0' || dlci < 0 || dlci > 1023)
216                         return (EINVAL);
217
218                 /* We have a dlci, now either find it, or allocate it */
219                 for (chan = 0; chan < XXX_NUM_DLCIS; chan++)
220                         if (xxxp->channel[chan].dlci == dlci)
221                                 break;
222                 if (chan == XXX_NUM_DLCIS) {
223                         for (chan = 0; chan < XXX_NUM_DLCIS; chan++)
224                                 if (xxxp->channel[chan].dlci != -2)
225                                         continue;
226                         if (chan == XXX_NUM_DLCIS)
227                                 return (ENOBUFS);
228                 }
229                 if (xxxp->channel[chan].hook != NULL)
230                         return (EADDRINUSE);
231                 hook->private = xxxp->channel + chan;
232                 xxxp->channel[chan].hook = hook;
233                 return (0);
234         } else if (strcmp(name, NG_XXX_HOOK_DOWNSTREAM) == 0) {
235                 /* Example of simple predefined hooks. */
236                 /* do something specific to the downstream connection */
237                 xxxp->downstream_hook.hook = hook;
238                 hook->private = &xxxp->downstream_hook;
239         } else if (strcmp(name, NG_XXX_HOOK_DEBUG) == 0) {
240                 /* do something specific to a debug connection */
241                 xxxp->debughook = hook;
242                 hook->private = NULL;
243         } else
244                 return (EINVAL);        /* not a hook we know about */
245         return(0);
246 }
247
248 /*
249  * Get a netgraph control message.
250  * Check it is one we understand. If needed, send a response.
251  * We could save the address for an async action later, but don't here.
252  * Always free the message.
253  * The response should be in a malloc'd region that the caller can 'free'.
254  * A response is not required.
255  * Theoretically you could respond defferently to old message types if
256  * the cookie in the header didn't match what we consider to be current
257  * (so that old userland programs could continue to work).
258  */
259 static int
260 ng_xxx_rcvmsg(node_p node,
261            struct ng_mesg *msg, const char *retaddr, struct ng_mesg **rptr)
262 {
263         const xxx_p xxxp = node->private;
264         struct ng_mesg *resp = NULL;
265         int error = 0;
266
267         /* Deal with message according to cookie and command */
268         switch (msg->header.typecookie) {
269         case NGM_XXX_COOKIE: 
270                 switch (msg->header.cmd) {
271                 case NGM_XXX_GET_STATUS:
272                     {
273                         struct ngxxxstat *stats;
274
275                         NG_MKRESPONSE(resp, msg, sizeof(*stats), M_NOWAIT);
276                         if (!resp) {
277                                 error = ENOMEM;
278                                 break;
279                         }
280                         stats = (struct ngxxxstat *) resp->data;
281                         stats->packets_in = xxxp->packets_in;
282                         stats->packets_out = xxxp->packets_out;
283                         break;
284                     }
285                 case NGM_XXX_SET_FLAG:
286                         if (msg->header.arglen != sizeof(u_int32_t)) {
287                                 error = EINVAL;
288                                 break;
289                         }
290                         xxxp->flags = *((u_int32_t *) msg->data);
291                         break;
292                 default:
293                         error = EINVAL;         /* unknown command */
294                         break;
295                 }
296                 break;
297         default:
298                 error = EINVAL;                 /* unknown cookie type */
299                 break;
300         }
301
302         /* Take care of synchronous response, if any */
303         if (rptr)
304                 *rptr = resp;
305         else if (resp)
306                 FREE(resp, M_NETGRAPH);
307
308         /* Free the message and return */
309         FREE(msg, M_NETGRAPH);
310         return(error);
311 }
312
313 /*
314  * Receive data, and do something with it.
315  * Possibly send it out on another link after processing.
316  * Possibly do something different if it comes from different
317  * hooks. the caller will never free m or meta, so
318  * if we use up this data or abort we must free BOTH of these.
319  *
320  * If we want, we may decide to force this data to be queued and reprocessed
321  * at the netgraph NETISR time. (at which time it will be entered using ng_xxx_rcvdataq().
322  */
323 static int
324 ng_xxx_rcvdata(hook_p hook, struct mbuf *m, meta_p meta)
325 {
326         int dlci = -2;
327
328         if (hook->private) {
329                 /* 
330                  * If it's dlci 1023, requeue it so that it's handled
331                  * at a lower priority. This is how a node decides to
332                  * defer a data message.
333                  */
334                 dlci = ((struct XXX_hookinfo *) hook->private)->dlci;
335                 if (dlci == 1023) {
336                         return(ng_queue_data(hook->peer, m, meta));
337                 }
338         }
339         return(ng_xxx_rcvdataq(hook, m, meta));
340 }
341
342 /*
343  * Always accept the data. This version of rcvdata is called from the dequeueing routine.
344  */
345 static int
346 ng_xxx_rcvdataq(hook_p hook, struct mbuf *m, meta_p meta)
347 {
348         const xxx_p xxxp = hook->node->private;
349         int chan = -2;
350         int dlci = -2;
351         int error;
352
353         if (hook->private) {
354                 dlci = ((struct XXX_hookinfo *) hook->private)->dlci;
355                 chan = ((struct XXX_hookinfo *) hook->private)->channel;
356                 if (dlci != -1) {
357                         /* If received on a DLCI hook process for this
358                          * channel and pass it to the downstream module.
359                          * Normally one would add a multiplexing header at
360                          * the front here */
361                         /* M_PREPEND(....)      ; */
362                         /* mtod(m, xxxxxx)->dlci = dlci; */
363                         error = ng_send_data(xxxp->downstream_hook.hook,
364                             m, meta);
365                         xxxp->packets_out++;
366                 } else {
367                         /* data came from the multiplexed link */
368                         dlci = 1;       /* get dlci from header */
369                         /* madjust(....) *//* chop off header */
370                         for (chan = 0; chan < XXX_NUM_DLCIS; chan++)
371                                 if (xxxp->channel[chan].dlci == dlci)
372                                         break;
373                         if (chan == XXX_NUM_DLCIS) {
374                                 NG_FREE_DATA(m, meta);
375                                 return (ENETUNREACH);
376                         }
377                         /* If we were called at splnet, use the following:
378                          * NG_SEND_DATA(error, otherhook, m, meta); if this
379                          * node is running at some SPL other than SPLNET
380                          * then you should use instead: error =
381                          * ng_queueit(otherhook, m, meta); m = NULL: meta =
382                          * NULL; this queues the data using the standard
383                          * NETISR system and schedules the data to be picked
384                          * up again once the system has moved to SPLNET and
385                          * the processing of the data can continue. after
386                          * these are run 'm' and 'meta' should be considered
387                          * as invalid and NG_SEND_DATA actually zaps them. */
388                         NG_SEND_DATA(error, xxxp->channel[chan].hook, m, meta);
389                         xxxp->packets_in++;
390                 }
391         } else {
392                 /* It's the debug hook, throw it away.. */
393                 if (hook == xxxp->downstream_hook.hook)
394                         NG_FREE_DATA(m, meta);
395         }
396         return 0;
397 }
398
399 #if 0
400 /*
401  * If this were a device node, the data may have been received in response
402  * to some interrupt.
403  * in which case it would probably look as follows:
404  */
405 void
406 devintr(void)
407 {
408         meta_p  meta = NULL;    /* whatever metadata we might imagine goes
409                                  * here */
410
411         /* get packet from device and send on */
412         m = MGET(blah blah)
413             error = ng_queueit(upstream, m, meta);      /* see note above in
414                                                          * xxx_rcvdata() */
415 }
416
417 #endif                          /* 0 */
418
419 /*
420  * Do local shutdown processing..
421  * If we are a persistant device, we might refuse to go away, and
422  * we'd only remove our links and reset ourself.
423  */
424 static int
425 ng_xxx_rmnode(node_p node)
426 {
427         const xxx_p privdata = node->private;
428
429         node->flags |= NG_INVALID;
430         ng_cutlinks(node);
431 #ifndef PERSISTANT_NODE
432         ng_unname(node);
433         node->private = NULL;
434         ng_unref(privdata->node);
435         FREE(privdata, M_NETGRAPH);
436 #else
437         privdata->packets_in = 0;               /* reset stats */
438         privdata->packets_out = 0;
439         node->flags &= ~NG_INVALID;             /* reset invalid flag */
440 #endif /* PERSISTANT_NODE */
441         return (0);
442 }
443
444 /*
445  * This is called once we've already connected a new hook to the other node.
446  * It gives us a chance to balk at the last minute.
447  */
448 static int
449 ng_xxx_connect(hook_p hook)
450 {
451         /* be really amiable and just say "YUP that's OK by me! " */
452         return (0);
453 }
454
455 /*
456  * Dook disconnection
457  *
458  * For this type, removal of the last link destroys the node
459  */
460 static int
461 ng_xxx_disconnect(hook_p hook)
462 {
463         if (hook->private)
464                 ((struct XXX_hookinfo *) (hook->private))->hook = NULL;
465         if (hook->node->numhooks == 0)
466                 ng_rmnode(hook->node);
467         return (0);
468 }
469