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