6 * Copyright (c) 1996-1999 Whistle Communications, Inc.
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
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>
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"
56 #include "ng_sample.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 ");
64 #define M_NETGRAPH_XXX M_NETGRAPH
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
[] = {
101 &ng_parse_int32_type
,
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 */
130 /* Information we store for each node */
132 struct XXX_hookinfo channel
[XXX_NUM_DLCIS
];
133 struct XXX_hookinfo downstream_hook
;
134 node_p node
; /* back pointer to node */
136 u_int packets_in
; /* packets in from downstream */
137 u_int packets_out
; /* packets out towards downstream */
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 :)
152 ng_xxx_constructor(node_p node
)
157 /* Initialize private descriptor */
158 privdata
= kmalloc(sizeof(*privdata
), M_NETGRAPH
,
159 M_WAITOK
| M_NULLOK
| M_ZERO
);
160 if (privdata
== NULL
)
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
;
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)
186 ng_xxx_newhook(node_p node
, hook_p hook
, const char *name
)
188 const xxx_p xxxp
= NG_NODE_PRIVATE(node
);
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
);
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
205 NG_XXX_HOOK_DLCI_LEADIN
, strlen(NG_XXX_HOOK_DLCI_LEADIN
)) == 0) {
208 cp
= name
+ strlen(NG_XXX_HOOK_DLCI_LEADIN
);
209 if (!isdigit(*cp
) || (cp
[0] == '0' && cp
[1] != '\0'))
211 dlci
= (int)strtoul(cp
, &eptr
, 10);
212 if (*eptr
!= '\0' || dlci
< 0 || dlci
> 1023)
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
)
219 if (chan
== XXX_NUM_DLCIS
) {
220 for (chan
= 0; chan
< XXX_NUM_DLCIS
; chan
++)
221 if (xxxp
->channel
[chan
].dlci
== -2)
223 if (chan
== XXX_NUM_DLCIS
)
225 xxxp
->channel
[chan
].dlci
= dlci
;
227 if (xxxp
->channel
[chan
].hook
!= NULL
)
229 NG_HOOK_SET_PRIVATE(hook
, xxxp
->channel
+ chan
);
230 xxxp
->channel
[chan
].hook
= hook
;
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
);
242 return (EINVAL
); /* not a hook we know about */
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).
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
;
270 NGI_GET_MSG(item
, msg
);
271 /* Deal with message according to cookie and command */
272 switch (msg
->header
.typecookie
) {
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
);
284 stats
= (struct ngxxxstat
*) resp
->data
;
285 stats
->packets_in
= xxxp
->packets_in
;
286 stats
->packets_out
= xxxp
->packets_out
;
289 case NGM_XXX_SET_FLAG
:
290 if (msg
->header
.arglen
!= sizeof(u_int32_t
)) {
294 xxxp
->flags
= *((u_int32_t
*) msg
->data
);
297 error
= EINVAL
; /* unknown command */
302 error
= EINVAL
; /* unknown cookie type */
306 /* Take care of synchronous response, if any */
307 NG_RESPOND_MSG(error
, node
, item
, resp
);
308 /* Free the message and return */
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.
329 ng_xxx_rcvdata(hook_p hook
, item_p item
)
331 const xxx_p xxxp
= NG_NODE_PRIVATE(NG_HOOK_NODE(hook
));
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
;
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
346 /* M_PREPEND(....) ; */
347 /* mtod(m, xxxxxx)->dlci = dlci; */
348 NG_FWD_NEW_DATA(error
, item
,
349 xxxp
->downstream_hook
.hook
, m
);
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
)
358 if (chan
== XXX_NUM_DLCIS
) {
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
);
379 /* It's the debug hook, throw it away.. */
380 if (hook
== xxxp
->downstream_hook
.hook
) {
390 * If this were a device node, the data may have been received in response
392 * in which case it would probably look as follows:
398 /* get packet from device and send on */
401 NG_SEND_DATA_ONLY(error
, xxxp
->upstream_hook
.hook
, m
);
402 /* see note above in xxx_rcvdata() */
403 /* and ng_xxx_connect() */
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().
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
);
428 kfree(privdata
, M_NETGRAPH
);
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
);
441 NG_NODE_REVIVE(node
); /* tell ng_rmnode() we will persist */
442 #endif /* PERSISTANT_NODE */
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.
451 ng_xxx_connect(hook_p hook
)
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
));
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
)) {
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
;
478 NG_HOOK_FORCE_QUEUE(hook
);
481 /* otherwise be really amiable and just say "YUP that's OK by me! " */
488 * For this type, removal of the last link destroys the node
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
));