test - Fix build warnings
[dragonfly.git] / sys / netgraph7 / ng_sample.c
blob8beb302e7c85551e05fbc9d19a0ec461c2aef5cf
1 /*
2 * ng_sample.c
3 */
5 /*-
6 * Copyright (c) 1996-1999 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.
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.
38 * Author: Julian Elischer <julian@freebsd.org>
40 * $FreeBSD: src/sys/netgraph/ng_sample.c,v 1.30 2005/02/06 19:24:59 glebius Exp $
41 * $DragonFly: src/sys/netgraph7/ng_sample.c,v 1.2 2008/06/26 23:05:35 dillon Exp $
42 * $Whistle: ng_sample.c,v 1.13 1999/11/01 09:24:52 julian Exp $
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/mbuf.h>
49 #include <sys/malloc.h>
50 #include <sys/ctype.h>
51 #include <sys/errno.h>
52 #include <sys/syslog.h>
54 #include "ng_message.h"
55 #include "ng_parse.h"
56 #include "ng_sample.h"
57 #include "netgraph.h"
59 /* If you do complicated mallocs you may want to do this */
60 /* and use it for your mallocs */
61 #ifdef NG_SEPARATE_MALLOC
62 MALLOC_DEFINE(M_NETGRAPH_XXX, "netgraph_xxx", "netgraph xxx node ");
63 #else
64 #define M_NETGRAPH_XXX M_NETGRAPH
65 #endif
68 * This section contains the netgraph method declarations for the
69 * sample node. These methods define the netgraph 'type'.
72 static ng_constructor_t ng_xxx_constructor;
73 static ng_rcvmsg_t ng_xxx_rcvmsg;
74 static ng_shutdown_t ng_xxx_shutdown;
75 static ng_newhook_t ng_xxx_newhook;
76 static ng_connect_t ng_xxx_connect;
77 static ng_rcvdata_t ng_xxx_rcvdata;
78 static ng_disconnect_t ng_xxx_disconnect;
80 /* Parse type for struct ngxxxstat */
81 static const struct ng_parse_struct_field ng_xxx_stat_type_fields[]
82 = NG_XXX_STATS_TYPE_INFO;
83 static const struct ng_parse_type ng_xxx_stat_type = {
84 &ng_parse_struct_type,
85 &ng_xxx_stat_type_fields
88 /* List of commands and how to convert arguments to/from ASCII */
89 static const struct ng_cmdlist ng_xxx_cmdlist[] = {
91 NGM_XXX_COOKIE,
92 NGM_XXX_GET_STATUS,
93 "getstatus",
94 NULL,
95 &ng_xxx_stat_type,
98 NGM_XXX_COOKIE,
99 NGM_XXX_SET_FLAG,
100 "setflag",
101 &ng_parse_int32_type,
102 NULL
104 { 0 }
107 /* Netgraph node type descriptor */
108 static struct ng_type typestruct = {
109 .version = NG_ABI_VERSION,
110 .name = NG_XXX_NODE_TYPE,
111 .constructor = ng_xxx_constructor,
112 .rcvmsg = ng_xxx_rcvmsg,
113 .shutdown = ng_xxx_shutdown,
114 .newhook = ng_xxx_newhook,
115 /* .findhook = ng_xxx_findhook, */
116 .connect = ng_xxx_connect,
117 .rcvdata = ng_xxx_rcvdata,
118 .disconnect = ng_xxx_disconnect,
119 .cmdlist = ng_xxx_cmdlist,
121 NETGRAPH_INIT(xxx, &typestruct);
123 /* Information we store for each hook on each node */
124 struct XXX_hookinfo {
125 int dlci; /* The DLCI it represents, -1 == downstream */
126 int channel; /* The channel representing this DLCI */
127 hook_p hook;
130 /* Information we store for each node */
131 struct XXX {
132 struct XXX_hookinfo channel[XXX_NUM_DLCIS];
133 struct XXX_hookinfo downstream_hook;
134 node_p node; /* back pointer to node */
135 hook_p debughook;
136 u_int packets_in; /* packets in from downstream */
137 u_int packets_out; /* packets out towards downstream */
138 u_int32_t flags;
140 typedef struct XXX *xxx_p;
143 * Allocate the private data structure. The generic node has already
144 * been created. Link them together. We arrive with a reference to the node
145 * i.e. the reference count is incremented for us already.
147 * If this were a device node than this work would be done in the attach()
148 * routine and the constructor would return EINVAL as you should not be able
149 * to creatednodes that depend on hardware (unless you can add the hardware :)
151 static int
152 ng_xxx_constructor(node_p node)
154 xxx_p privdata;
155 int i;
157 /* Initialize private descriptor */
158 privdata = kmalloc(sizeof(*privdata), M_NETGRAPH,
159 M_WAITOK | M_NULLOK | M_ZERO);
160 if (privdata == NULL)
161 return (ENOMEM);
162 for (i = 0; i < XXX_NUM_DLCIS; i++) {
163 privdata->channel[i].dlci = -2;
164 privdata->channel[i].channel = i;
167 /* Link structs together; this counts as our one reference to *nodep */
168 NG_NODE_SET_PRIVATE(node, privdata);
169 privdata->node = node;
170 return (0);
174 * Give our ok for a hook to be added...
175 * If we are not running this might kick a device into life.
176 * Possibly decode information out of the hook name.
177 * Add the hook's private info to the hook structure.
178 * (if we had some). In this example, we assume that there is a
179 * an array of structs, called 'channel' in the private info,
180 * one for each active channel. The private
181 * pointer of each hook points to the appropriate XXX_hookinfo struct
182 * so that the source of an input packet is easily identified.
183 * (a dlci is a frame relay channel)
185 static int
186 ng_xxx_newhook(node_p node, hook_p hook, const char *name)
188 const xxx_p xxxp = NG_NODE_PRIVATE(node);
189 const char *cp;
190 int dlci = 0;
191 int chan;
193 #if 0
194 /* Possibly start up the device if it's not already going */
195 if ((xxxp->flags & SCF_RUNNING) == 0) {
196 ng_xxx_start_hardware(xxxp);
198 #endif
200 /* Example of how one might use hooks with embedded numbers: All
201 * hooks start with 'dlci' and have a decimal trailing channel
202 * number up to 4 digits Use the leadin defined int he associated .h
203 * file. */
204 if (strncmp(name,
205 NG_XXX_HOOK_DLCI_LEADIN, strlen(NG_XXX_HOOK_DLCI_LEADIN)) == 0) {
206 char *eptr;
208 cp = name + strlen(NG_XXX_HOOK_DLCI_LEADIN);
209 if (!isdigit(*cp) || (cp[0] == '0' && cp[1] != '\0'))
210 return (EINVAL);
211 dlci = (int)strtoul(cp, &eptr, 10);
212 if (*eptr != '\0' || dlci < 0 || dlci > 1023)
213 return (EINVAL);
215 /* We have a dlci, now either find it, or allocate it */
216 for (chan = 0; chan < XXX_NUM_DLCIS; chan++)
217 if (xxxp->channel[chan].dlci == dlci)
218 break;
219 if (chan == XXX_NUM_DLCIS) {
220 for (chan = 0; chan < XXX_NUM_DLCIS; chan++)
221 if (xxxp->channel[chan].dlci == -2)
222 break;
223 if (chan == XXX_NUM_DLCIS)
224 return (ENOBUFS);
225 xxxp->channel[chan].dlci = dlci;
227 if (xxxp->channel[chan].hook != NULL)
228 return (EADDRINUSE);
229 NG_HOOK_SET_PRIVATE(hook, xxxp->channel + chan);
230 xxxp->channel[chan].hook = hook;
231 return (0);
232 } else if (strcmp(name, NG_XXX_HOOK_DOWNSTREAM) == 0) {
233 /* Example of simple predefined hooks. */
234 /* do something specific to the downstream connection */
235 xxxp->downstream_hook.hook = hook;
236 NG_HOOK_SET_PRIVATE(hook, &xxxp->downstream_hook);
237 } else if (strcmp(name, NG_XXX_HOOK_DEBUG) == 0) {
238 /* do something specific to a debug connection */
239 xxxp->debughook = hook;
240 NG_HOOK_SET_PRIVATE(hook, NULL);
241 } else
242 return (EINVAL); /* not a hook we know about */
243 return(0);
247 * Get a netgraph control message.
248 * We actually recieve a queue item that has a pointer to the message.
249 * If we free the item, the message will be freed too, unless we remove
250 * it from the item using NGI_GET_MSG();
251 * The return address is also stored in the item, as an ng_ID_t,
252 * accessible as NGI_RETADDR(item);
253 * Check it is one we understand. If needed, send a response.
254 * We could save the address for an async action later, but don't here.
255 * Always free the message.
256 * The response should be in a malloc'd region that the caller can 'free'.
257 * A response is not required.
258 * Theoretically you could respond defferently to old message types if
259 * the cookie in the header didn't match what we consider to be current
260 * (so that old userland programs could continue to work).
262 static int
263 ng_xxx_rcvmsg(node_p node, item_p item, hook_p lasthook)
265 const xxx_p xxxp = NG_NODE_PRIVATE(node);
266 struct ng_mesg *resp = NULL;
267 int error = 0;
268 struct ng_mesg *msg;
270 NGI_GET_MSG(item, msg);
271 /* Deal with message according to cookie and command */
272 switch (msg->header.typecookie) {
273 case NGM_XXX_COOKIE:
274 switch (msg->header.cmd) {
275 case NGM_XXX_GET_STATUS:
277 struct ngxxxstat *stats;
279 NG_MKRESPONSE(resp, msg, sizeof(*stats), M_WAITOK | M_NULLOK);
280 if (!resp) {
281 error = ENOMEM;
282 break;
284 stats = (struct ngxxxstat *) resp->data;
285 stats->packets_in = xxxp->packets_in;
286 stats->packets_out = xxxp->packets_out;
287 break;
289 case NGM_XXX_SET_FLAG:
290 if (msg->header.arglen != sizeof(u_int32_t)) {
291 error = EINVAL;
292 break;
294 xxxp->flags = *((u_int32_t *) msg->data);
295 break;
296 default:
297 error = EINVAL; /* unknown command */
298 break;
300 break;
301 default:
302 error = EINVAL; /* unknown cookie type */
303 break;
306 /* Take care of synchronous response, if any */
307 NG_RESPOND_MSG(error, node, item, resp);
308 /* Free the message and return */
309 NG_FREE_MSG(msg);
310 return(error);
314 * Receive data, and do something with it.
315 * Actually we receive a queue item which holds the data.
316 * If we free the item it will also free the data unless we have
317 * previously disassociated it using the NGI_GET_M() macro.
318 * Possibly send it out on another link after processing.
319 * Possibly do something different if it comes from different
320 * hooks. The caller will never free m, so if we use up this data or
321 * abort we must free it.
323 * If we want, we may decide to force this data to be queued and reprocessed
324 * at the netgraph NETISR time.
325 * We would do that by setting the HK_QUEUE flag on our hook. We would do that
326 * in the connect() method.
328 static int
329 ng_xxx_rcvdata(hook_p hook, item_p item )
331 const xxx_p xxxp = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
332 int chan = -2;
333 int dlci = -2;
334 int error;
335 struct mbuf *m;
337 NGI_GET_M(item, m);
338 if (NG_HOOK_PRIVATE(hook)) {
339 dlci = ((struct XXX_hookinfo *) NG_HOOK_PRIVATE(hook))->dlci;
340 chan = ((struct XXX_hookinfo *) NG_HOOK_PRIVATE(hook))->channel;
341 if (dlci != -1) {
342 /* If received on a DLCI hook process for this
343 * channel and pass it to the downstream module.
344 * Normally one would add a multiplexing header at
345 * the front here */
346 /* M_PREPEND(....) ; */
347 /* mtod(m, xxxxxx)->dlci = dlci; */
348 NG_FWD_NEW_DATA(error, item,
349 xxxp->downstream_hook.hook, m);
350 xxxp->packets_out++;
351 } else {
352 /* data came from the multiplexed link */
353 dlci = 1; /* get dlci from header */
354 /* madjust(....) *//* chop off header */
355 for (chan = 0; chan < XXX_NUM_DLCIS; chan++)
356 if (xxxp->channel[chan].dlci == dlci)
357 break;
358 if (chan == XXX_NUM_DLCIS) {
359 NG_FREE_ITEM(item);
360 NG_FREE_M(m);
361 return (ENETUNREACH);
363 /* If we were called at splnet, use the following:
364 * NG_SEND_DATA_ONLY(error, otherhook, m); if this
365 * node is running at some SPL other than SPLNET
366 * then you should use instead: error =
367 * ng_queueit(otherhook, m, NULL); m = NULL;
368 * This queues the data using the standard NETISR
369 * system and schedules the data to be picked
370 * up again once the system has moved to SPLNET and
371 * the processing of the data can continue. After
372 * these are run 'm' should be considered
373 * as invalid and NG_SEND_DATA actually zaps them. */
374 NG_FWD_NEW_DATA(error, item,
375 xxxp->channel[chan].hook, m);
376 xxxp->packets_in++;
378 } else {
379 /* It's the debug hook, throw it away.. */
380 if (hook == xxxp->downstream_hook.hook) {
381 NG_FREE_ITEM(item);
382 NG_FREE_M(m);
385 return 0;
388 #if 0
390 * If this were a device node, the data may have been received in response
391 * to some interrupt.
392 * in which case it would probably look as follows:
394 devintr()
396 int error;
398 /* get packet from device and send on */
399 m = MGET(blah blah)
401 NG_SEND_DATA_ONLY(error, xxxp->upstream_hook.hook, m);
402 /* see note above in xxx_rcvdata() */
403 /* and ng_xxx_connect() */
406 #endif /* 0 */
409 * Do local shutdown processing..
410 * All our links and the name have already been removed.
411 * If we are a persistant device, we might refuse to go away.
412 * In the case of a persistant node we signal the framework that we
413 * are still in business by clearing the NGF_INVALID bit. However
414 * If we find the NGF_REALLY_DIE bit set, this means that
415 * we REALLY need to die (e.g. hardware removed).
416 * This would have been set using the NG_NODE_REALLY_DIE(node)
417 * macro in some device dependent function (not shown here) before
418 * calling ng_rmnode_self().
420 static int
421 ng_xxx_shutdown(node_p node)
423 const xxx_p privdata = NG_NODE_PRIVATE(node);
425 #ifndef PERSISTANT_NODE
426 NG_NODE_SET_PRIVATE(node, NULL);
427 NG_NODE_UNREF(node);
428 kfree(privdata, M_NETGRAPH);
429 #else
430 if (node->nd_flags & NGF_REALLY_DIE) {
432 * WE came here because the widget card is being unloaded,
433 * so stop being persistant.
434 * Actually undo all the things we did on creation.
436 NG_NODE_SET_PRIVATE(node, NULL);
437 NG_NODE_UNREF(privdata->node);
438 kfree(privdata, M_NETGRAPH);
439 return (0);
441 NG_NODE_REVIVE(node); /* tell ng_rmnode() we will persist */
442 #endif /* PERSISTANT_NODE */
443 return (0);
447 * This is called once we've already connected a new hook to the other node.
448 * It gives us a chance to balk at the last minute.
450 static int
451 ng_xxx_connect(hook_p hook)
453 #if 0
455 * If we were a driver running at other than splnet then
456 * we should set the QUEUE bit on the edge so that we
457 * will deliver by queing.
459 if /*it is the upstream hook */
460 NG_HOOK_FORCE_QUEUE(NG_HOOK_PEER(hook));
461 #endif
462 #if 0
464 * If for some reason we want incoming date to be queued
465 * by the NETISR system and delivered later we can set the same bit on
466 * OUR hook. (maybe to allow unwinding of the stack)
469 if (NG_HOOK_PRIVATE(hook)) {
470 int dlci;
472 * If it's dlci 1023, requeue it so that it's handled
473 * at a lower priority. This is how a node decides to
474 * defer a data message.
476 dlci = ((struct XXX_hookinfo *) NG_HOOK_PRIVATE(hook))->dlci;
477 if (dlci == 1023) {
478 NG_HOOK_FORCE_QUEUE(hook);
480 #endif
481 /* otherwise be really amiable and just say "YUP that's OK by me! " */
482 return (0);
486 * Hook disconnection
488 * For this type, removal of the last link destroys the node
490 static int
491 ng_xxx_disconnect(hook_p hook)
493 if (NG_HOOK_PRIVATE(hook))
494 ((struct XXX_hookinfo *) (NG_HOOK_PRIVATE(hook)))->hook = NULL;
495 if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
496 && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook)))) /* already shutting down? */
497 ng_rmnode_self(NG_HOOK_NODE(hook));
498 return (0);