5 * Copyright (c) 1996-1999 Whistle Communications, Inc.
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.
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
37 * Author: Julian Elischer <julian@freebsd.org>
40 * $Whistle: ng_sample.c,v 1.13 1999/11/01 09:24:52 julian Exp $
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
47 #include <sys/malloc.h>
48 #include <sys/ctype.h>
49 #include <sys/errno.h>
50 #include <sys/syslog.h>
52 #include <netgraph/ng_message.h>
53 #include <netgraph/ng_parse.h>
54 #include <netgraph/ng_sample.h>
55 #include <netgraph/netgraph.h>
57 /* If you do complicated mallocs you may want to do this */
58 /* and use it for your mallocs */
59 #ifdef NG_SEPARATE_MALLOC
60 MALLOC_DEFINE(M_NETGRAPH_XXX, "netgraph_xxx", "netgraph xxx node ");
62 #define M_NETGRAPH_XXX M_NETGRAPH
66 * This section contains the netgraph method declarations for the
67 * sample node. These methods define the netgraph 'type'.
70 static ng_constructor_t ng_xxx_constructor;
71 static ng_rcvmsg_t ng_xxx_rcvmsg;
72 static ng_shutdown_t ng_xxx_shutdown;
73 static ng_newhook_t ng_xxx_newhook;
74 static ng_connect_t ng_xxx_connect;
75 static ng_rcvdata_t ng_xxx_rcvdata; /* note these are both ng_rcvdata_t */
76 static ng_disconnect_t ng_xxx_disconnect;
78 /* Parse type for struct ngxxxstat */
79 static const struct ng_parse_struct_field ng_xxx_stat_type_fields[]
80 = NG_XXX_STATS_TYPE_INFO;
81 static const struct ng_parse_type ng_xxx_stat_type = {
82 &ng_parse_struct_type,
83 &ng_xxx_stat_type_fields
86 /* List of commands and how to convert arguments to/from ASCII */
87 static const struct ng_cmdlist ng_xxx_cmdlist[] = {
105 /* Netgraph node type descriptor */
106 static struct ng_type typestruct = {
107 .version = NG_ABI_VERSION,
108 .name = NG_XXX_NODE_TYPE,
109 .constructor = ng_xxx_constructor,
110 .rcvmsg = ng_xxx_rcvmsg,
111 .shutdown = ng_xxx_shutdown,
112 .newhook = ng_xxx_newhook,
113 /* .findhook = ng_xxx_findhook, */
114 .connect = ng_xxx_connect,
115 .rcvdata = ng_xxx_rcvdata,
116 .disconnect = ng_xxx_disconnect,
117 .cmdlist = ng_xxx_cmdlist,
119 NETGRAPH_INIT(xxx, &typestruct);
121 /* Information we store for each hook on each node */
122 struct XXX_hookinfo {
123 int dlci; /* The DLCI it represents, -1 == downstream */
124 int channel; /* The channel representing this DLCI */
128 /* Information we store for each node */
130 struct XXX_hookinfo channel[XXX_NUM_DLCIS];
131 struct XXX_hookinfo downstream_hook;
132 node_p node; /* back pointer to node */
134 u_int packets_in; /* packets in from downstream */
135 u_int packets_out; /* packets out towards downstream */
138 typedef struct XXX *xxx_p;
141 * Allocate the private data structure. The generic node has already
142 * been created. Link them together. We arrive with a reference to the node
143 * i.e. the reference count is incremented for us already.
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 :)
150 ng_xxx_constructor(node_p node)
155 /* Initialize private descriptor */
156 MALLOC(privdata, xxx_p, sizeof(*privdata), M_NETGRAPH,
158 if (privdata == NULL)
160 for (i = 0; i < XXX_NUM_DLCIS; i++) {
161 privdata->channel[i].dlci = -2;
162 privdata->channel[i].channel = i;
165 /* Link structs together; this counts as our one reference to *nodep */
166 NG_NODE_SET_PRIVATE(node, privdata);
167 privdata->node = node;
172 * Give our ok for a hook to be added...
173 * If we are not running this might kick a device into life.
174 * Possibly decode information out of the hook name.
175 * Add the hook's private info to the hook structure.
176 * (if we had some). In this example, we assume that there is a
177 * an array of structs, called 'channel' in the private info,
178 * one for each active channel. The private
179 * pointer of each hook points to the appropriate XXX_hookinfo struct
180 * so that the source of an input packet is easily identified.
181 * (a dlci is a frame relay channel)
184 ng_xxx_newhook(node_p node, hook_p hook, const char *name)
186 const xxx_p xxxp = NG_NODE_PRIVATE(node);
192 /* Possibly start up the device if it's not already going */
193 if ((xxxp->flags & SCF_RUNNING) == 0) {
194 ng_xxx_start_hardware(xxxp);
198 /* Example of how one might use hooks with embedded numbers: All
199 * hooks start with 'dlci' and have a decimal trailing channel
200 * number up to 4 digits Use the leadin defined int he associated .h
203 NG_XXX_HOOK_DLCI_LEADIN, strlen(NG_XXX_HOOK_DLCI_LEADIN)) == 0) {
206 cp = name + strlen(NG_XXX_HOOK_DLCI_LEADIN);
207 if (!isdigit(*cp) || (cp[0] == '0' && cp[1] != '\0'))
209 dlci = (int)strtoul(cp, &eptr, 10);
210 if (*eptr != '\0' || dlci < 0 || dlci > 1023)
213 /* We have a dlci, now either find it, or allocate it */
214 for (chan = 0; chan < XXX_NUM_DLCIS; chan++)
215 if (xxxp->channel[chan].dlci == dlci)
217 if (chan == XXX_NUM_DLCIS) {
218 for (chan = 0; chan < XXX_NUM_DLCIS; chan++)
219 if (xxxp->channel[chan].dlci == -2)
221 if (chan == XXX_NUM_DLCIS)
223 xxxp->channel[chan].dlci = dlci;
225 if (xxxp->channel[chan].hook != NULL)
227 NG_HOOK_SET_PRIVATE(hook, xxxp->channel + chan);
228 xxxp->channel[chan].hook = hook;
230 } else if (strcmp(name, NG_XXX_HOOK_DOWNSTREAM) == 0) {
231 /* Example of simple predefined hooks. */
232 /* do something specific to the downstream connection */
233 xxxp->downstream_hook.hook = hook;
234 NG_HOOK_SET_PRIVATE(hook, &xxxp->downstream_hook);
235 } else if (strcmp(name, NG_XXX_HOOK_DEBUG) == 0) {
236 /* do something specific to a debug connection */
237 xxxp->debughook = hook;
238 NG_HOOK_SET_PRIVATE(hook, NULL);
240 return (EINVAL); /* not a hook we know about */
245 * Get a netgraph control message.
246 * We actually recieve a queue item that has a pointer to the message.
247 * If we free the item, the message will be freed too, unless we remove
248 * it from the item using NGI_GET_MSG();
249 * The return address is also stored in the item, as an ng_ID_t,
250 * accessible as NGI_RETADDR(item);
251 * Check it is one we understand. If needed, send a response.
252 * We could save the address for an async action later, but don't here.
253 * Always free the message.
254 * The response should be in a malloc'd region that the caller can 'free'.
255 * A response is not required.
256 * Theoretically you could respond defferently to old message types if
257 * the cookie in the header didn't match what we consider to be current
258 * (so that old userland programs could continue to work).
261 ng_xxx_rcvmsg(node_p node, item_p item, hook_p lasthook)
263 const xxx_p xxxp = NG_NODE_PRIVATE(node);
264 struct ng_mesg *resp = NULL;
268 NGI_GET_MSG(item, msg);
269 /* Deal with message according to cookie and command */
270 switch (msg->header.typecookie) {
272 switch (msg->header.cmd) {
273 case NGM_XXX_GET_STATUS:
275 struct ngxxxstat *stats;
277 NG_MKRESPONSE(resp, msg, sizeof(*stats), M_NOWAIT);
282 stats = (struct ngxxxstat *) resp->data;
283 stats->packets_in = xxxp->packets_in;
284 stats->packets_out = xxxp->packets_out;
287 case NGM_XXX_SET_FLAG:
288 if (msg->header.arglen != sizeof(u_int32_t)) {
292 xxxp->flags = *((u_int32_t *) msg->data);
295 error = EINVAL; /* unknown command */
300 error = EINVAL; /* unknown cookie type */
304 /* Take care of synchronous response, if any */
305 NG_RESPOND_MSG(error, node, item, resp);
306 /* Free the message and return */
312 * Receive data, and do something with it.
313 * Actually we receive a queue item which holds the data.
314 * If we free the item it will also free the data unless we have
315 * previously disassociated it using the NGI_GET_M() macro.
316 * Possibly send it out on another link after processing.
317 * Possibly do something different if it comes from different
318 * hooks. The caller will never free m, so if we use up this data or
319 * abort we must free it.
321 * If we want, we may decide to force this data to be queued and reprocessed
322 * at the netgraph NETISR time.
323 * We would do that by setting the HK_QUEUE flag on our hook. We would do that
324 * in the connect() method.
327 ng_xxx_rcvdata(hook_p hook, item_p item )
329 const xxx_p xxxp = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
336 if (NG_HOOK_PRIVATE(hook)) {
337 dlci = ((struct XXX_hookinfo *) NG_HOOK_PRIVATE(hook))->dlci;
338 chan = ((struct XXX_hookinfo *) NG_HOOK_PRIVATE(hook))->channel;
340 /* If received on a DLCI hook process for this
341 * channel and pass it to the downstream module.
342 * Normally one would add a multiplexing header at
344 /* M_PREPEND(....) ; */
345 /* mtod(m, xxxxxx)->dlci = dlci; */
346 NG_FWD_NEW_DATA(error, item,
347 xxxp->downstream_hook.hook, m);
350 /* data came from the multiplexed link */
351 dlci = 1; /* get dlci from header */
352 /* madjust(....) *//* chop off header */
353 for (chan = 0; chan < XXX_NUM_DLCIS; chan++)
354 if (xxxp->channel[chan].dlci == dlci)
356 if (chan == XXX_NUM_DLCIS) {
359 return (ENETUNREACH);
361 /* If we were called at splnet, use the following:
362 * NG_SEND_DATA_ONLY(error, otherhook, m); if this
363 * node is running at some SPL other than SPLNET
364 * then you should use instead: error =
365 * ng_queueit(otherhook, m, NULL); m = NULL;
366 * This queues the data using the standard NETISR
367 * system and schedules the data to be picked
368 * up again once the system has moved to SPLNET and
369 * the processing of the data can continue. After
370 * these are run 'm' should be considered
371 * as invalid and NG_SEND_DATA actually zaps them. */
372 NG_FWD_NEW_DATA(error, item,
373 xxxp->channel[chan].hook, m);
377 /* It's the debug hook, throw it away.. */
378 if (hook == xxxp->downstream_hook.hook) {
388 * If this were a device node, the data may have been received in response
390 * in which case it would probably look as follows:
396 /* get packet from device and send on */
399 NG_SEND_DATA_ONLY(error, xxxp->upstream_hook.hook, m);
400 /* see note above in xxx_rcvdata() */
401 /* and ng_xxx_connect() */
407 * Do local shutdown processing..
408 * All our links and the name have already been removed.
409 * If we are a persistant device, we might refuse to go away.
410 * In the case of a persistant node we signal the framework that we
411 * are still in business by clearing the NGF_INVALID bit. However
412 * If we find the NGF_REALLY_DIE bit set, this means that
413 * we REALLY need to die (e.g. hardware removed).
414 * This would have been set using the NG_NODE_REALLY_DIE(node)
415 * macro in some device dependent function (not shown here) before
416 * calling ng_rmnode_self().
419 ng_xxx_shutdown(node_p node)
421 const xxx_p privdata = NG_NODE_PRIVATE(node);
423 #ifndef PERSISTANT_NODE
424 NG_NODE_SET_PRIVATE(node, NULL);
426 FREE(privdata, M_NETGRAPH);
428 if (node->nd_flags & NGF_REALLY_DIE) {
430 * WE came here because the widget card is being unloaded,
431 * so stop being persistant.
432 * Actually undo all the things we did on creation.
434 NG_NODE_SET_PRIVATE(node, NULL);
435 NG_NODE_UNREF(privdata->node);
436 FREE(privdata, M_NETGRAPH);
439 NG_NODE_REVIVE(node); /* tell ng_rmnode() we will persist */
440 #endif /* PERSISTANT_NODE */
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.
449 ng_xxx_connect(hook_p hook)
453 * If we were a driver running at other than splnet then
454 * we should set the QUEUE bit on the edge so that we
455 * will deliver by queing.
457 if /*it is the upstream hook */
458 NG_HOOK_FORCE_QUEUE(NG_HOOK_PEER(hook));
462 * If for some reason we want incoming date to be queued
463 * by the NETISR system and delivered later we can set the same bit on
464 * OUR hook. (maybe to allow unwinding of the stack)
467 if (NG_HOOK_PRIVATE(hook)) {
470 * If it's dlci 1023, requeue it so that it's handled
471 * at a lower priority. This is how a node decides to
472 * defer a data message.
474 dlci = ((struct XXX_hookinfo *) NG_HOOK_PRIVATE(hook))->dlci;
476 NG_HOOK_FORCE_QUEUE(hook);
479 /* otherwise be really amiable and just say "YUP that's OK by me! " */
486 * For this type, removal of the last link destroys the node
489 ng_xxx_disconnect(hook_p hook)
491 if (NG_HOOK_PRIVATE(hook))
492 ((struct XXX_hookinfo *) (NG_HOOK_PRIVATE(hook)))->hook = NULL;
493 if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
494 && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook)))) /* already shutting down? */
495 ng_rmnode_self(NG_HOOK_NODE(hook));