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>
39 * $Whistle: ng_sample.c,v 1.13 1999/11/01 09:24:52 julian Exp $
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
46 #include <sys/malloc.h>
47 #include <sys/ctype.h>
48 #include <sys/errno.h>
49 #include <sys/syslog.h>
51 #include <netgraph/ng_message.h>
52 #include <netgraph/ng_parse.h>
53 #include <netgraph/ng_sample.h>
54 #include <netgraph/netgraph.h>
56 /* If you do complicated mallocs you may want to do this */
57 /* and use it for your mallocs */
58 #ifdef NG_SEPARATE_MALLOC
59 static MALLOC_DEFINE(M_NETGRAPH_XXX, "netgraph_xxx", "netgraph xxx node");
61 #define M_NETGRAPH_XXX M_NETGRAPH
65 * This section contains the netgraph method declarations for the
66 * sample node. These methods define the netgraph 'type'.
69 static ng_constructor_t ng_xxx_constructor;
70 static ng_rcvmsg_t ng_xxx_rcvmsg;
71 static ng_shutdown_t ng_xxx_shutdown;
72 static ng_newhook_t ng_xxx_newhook;
73 static ng_connect_t ng_xxx_connect;
74 static ng_rcvdata_t ng_xxx_rcvdata;
75 static ng_disconnect_t ng_xxx_disconnect;
77 /* Parse type for struct ngxxxstat */
78 static const struct ng_parse_struct_field ng_xxx_stat_type_fields[]
79 = NG_XXX_STATS_TYPE_INFO;
80 static const struct ng_parse_type ng_xxx_stat_type = {
81 &ng_parse_struct_type,
82 &ng_xxx_stat_type_fields
85 /* List of commands and how to convert arguments to/from ASCII */
86 static const struct ng_cmdlist ng_xxx_cmdlist[] = {
104 /* Netgraph node type descriptor */
105 static struct ng_type typestruct = {
106 .version = NG_ABI_VERSION,
107 .name = NG_XXX_NODE_TYPE,
108 .constructor = ng_xxx_constructor,
109 .rcvmsg = ng_xxx_rcvmsg,
110 .shutdown = ng_xxx_shutdown,
111 .newhook = ng_xxx_newhook,
112 /* .findhook = ng_xxx_findhook, */
113 .connect = ng_xxx_connect,
114 .rcvdata = ng_xxx_rcvdata,
115 .disconnect = ng_xxx_disconnect,
116 .cmdlist = ng_xxx_cmdlist,
118 NETGRAPH_INIT(xxx, &typestruct);
120 /* Information we store for each hook on each node */
121 struct XXX_hookinfo {
122 int dlci; /* The DLCI it represents, -1 == downstream */
123 int channel; /* The channel representing this DLCI */
127 /* Information we store for each node */
129 struct XXX_hookinfo channel[XXX_NUM_DLCIS];
130 struct XXX_hookinfo downstream_hook;
131 node_p node; /* back pointer to node */
133 u_int packets_in; /* packets in from downstream */
134 u_int packets_out; /* packets out towards downstream */
137 typedef struct XXX *xxx_p;
140 * Allocate the private data structure. The generic node has already
141 * been created. Link them together. We arrive with a reference to the node
142 * i.e. the reference count is incremented for us already.
144 * If this were a device node than this work would be done in the attach()
145 * routine and the constructor would return EINVAL as you should not be able
146 * to creatednodes that depend on hardware (unless you can add the hardware :)
149 ng_xxx_constructor(node_p node)
154 /* Initialize private descriptor */
155 privdata = malloc(sizeof(*privdata), M_NETGRAPH, M_WAITOK | M_ZERO);
156 for (i = 0; i < XXX_NUM_DLCIS; i++) {
157 privdata->channel[i].dlci = -2;
158 privdata->channel[i].channel = i;
161 /* Link structs together; this counts as our one reference to *nodep */
162 NG_NODE_SET_PRIVATE(node, privdata);
163 privdata->node = node;
168 * Give our ok for a hook to be added...
169 * If we are not running this might kick a device into life.
170 * Possibly decode information out of the hook name.
171 * Add the hook's private info to the hook structure.
172 * (if we had some). In this example, we assume that there is a
173 * an array of structs, called 'channel' in the private info,
174 * one for each active channel. The private
175 * pointer of each hook points to the appropriate XXX_hookinfo struct
176 * so that the source of an input packet is easily identified.
177 * (a dlci is a frame relay channel)
180 ng_xxx_newhook(node_p node, hook_p hook, const char *name)
182 const xxx_p xxxp = NG_NODE_PRIVATE(node);
188 /* Possibly start up the device if it's not already going */
189 if ((xxxp->flags & SCF_RUNNING) == 0) {
190 ng_xxx_start_hardware(xxxp);
194 /* Example of how one might use hooks with embedded numbers: All
195 * hooks start with 'dlci' and have a decimal trailing channel
196 * number up to 4 digits Use the leadin defined int he associated .h
199 NG_XXX_HOOK_DLCI_LEADIN, strlen(NG_XXX_HOOK_DLCI_LEADIN)) == 0) {
202 cp = name + strlen(NG_XXX_HOOK_DLCI_LEADIN);
203 if (!isdigit(*cp) || (cp[0] == '0' && cp[1] != '\0'))
205 dlci = (int)strtoul(cp, &eptr, 10);
206 if (*eptr != '\0' || dlci < 0 || dlci > 1023)
209 /* We have a dlci, now either find it, or allocate it */
210 for (chan = 0; chan < XXX_NUM_DLCIS; chan++)
211 if (xxxp->channel[chan].dlci == dlci)
213 if (chan == XXX_NUM_DLCIS) {
214 for (chan = 0; chan < XXX_NUM_DLCIS; chan++)
215 if (xxxp->channel[chan].dlci == -2)
217 if (chan == XXX_NUM_DLCIS)
219 xxxp->channel[chan].dlci = dlci;
221 if (xxxp->channel[chan].hook != NULL)
223 NG_HOOK_SET_PRIVATE(hook, xxxp->channel + chan);
224 xxxp->channel[chan].hook = hook;
226 } else if (strcmp(name, NG_XXX_HOOK_DOWNSTREAM) == 0) {
227 /* Example of simple predefined hooks. */
228 /* do something specific to the downstream connection */
229 xxxp->downstream_hook.hook = hook;
230 NG_HOOK_SET_PRIVATE(hook, &xxxp->downstream_hook);
231 } else if (strcmp(name, NG_XXX_HOOK_DEBUG) == 0) {
232 /* do something specific to a debug connection */
233 xxxp->debughook = hook;
234 NG_HOOK_SET_PRIVATE(hook, NULL);
236 return (EINVAL); /* not a hook we know about */
241 * Get a netgraph control message.
242 * We actually receive a queue item that has a pointer to the message.
243 * If we free the item, the message will be freed too, unless we remove
244 * it from the item using NGI_GET_MSG();
245 * The return address is also stored in the item, as an ng_ID_t,
246 * accessible as NGI_RETADDR(item);
247 * Check it is one we understand. If needed, send a response.
248 * We could save the address for an async action later, but don't here.
249 * Always free the message.
250 * The response should be in a malloc'd region that the caller can 'free'.
251 * A response is not required.
252 * Theoretically you could respond defferently to old message types if
253 * the cookie in the header didn't match what we consider to be current
254 * (so that old userland programs could continue to work).
257 ng_xxx_rcvmsg(node_p node, item_p item, hook_p lasthook)
259 const xxx_p xxxp = NG_NODE_PRIVATE(node);
260 struct ng_mesg *resp = NULL;
264 NGI_GET_MSG(item, msg);
265 /* Deal with message according to cookie and command */
266 switch (msg->header.typecookie) {
268 switch (msg->header.cmd) {
269 case NGM_XXX_GET_STATUS:
271 struct ngxxxstat *stats;
273 NG_MKRESPONSE(resp, msg, sizeof(*stats), M_NOWAIT);
278 stats = (struct ngxxxstat *) resp->data;
279 stats->packets_in = xxxp->packets_in;
280 stats->packets_out = xxxp->packets_out;
283 case NGM_XXX_SET_FLAG:
284 if (msg->header.arglen != sizeof(u_int32_t)) {
288 xxxp->flags = *((u_int32_t *) msg->data);
291 error = EINVAL; /* unknown command */
296 error = EINVAL; /* unknown cookie type */
300 /* Take care of synchronous response, if any */
301 NG_RESPOND_MSG(error, node, item, resp);
302 /* Free the message and return */
308 * Receive data, and do something with it.
309 * Actually we receive a queue item which holds the data.
310 * If we free the item it will also free the data unless we have
311 * previously disassociated it using the NGI_GET_M() macro.
312 * Possibly send it out on another link after processing.
313 * Possibly do something different if it comes from different
314 * hooks. The caller will never free m, so if we use up this data or
315 * abort we must free it.
317 * If we want, we may decide to force this data to be queued and reprocessed
318 * at the netgraph NETISR time.
319 * We would do that by setting the HK_QUEUE flag on our hook. We would do that
320 * in the connect() method.
323 ng_xxx_rcvdata(hook_p hook, item_p item )
325 const xxx_p xxxp = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
332 if (NG_HOOK_PRIVATE(hook)) {
333 dlci = ((struct XXX_hookinfo *) NG_HOOK_PRIVATE(hook))->dlci;
334 chan = ((struct XXX_hookinfo *) NG_HOOK_PRIVATE(hook))->channel;
336 /* If received on a DLCI hook process for this
337 * channel and pass it to the downstream module.
338 * Normally one would add a multiplexing header at
340 /* M_PREPEND(....) ; */
341 /* mtod(m, xxxxxx)->dlci = dlci; */
342 NG_FWD_NEW_DATA(error, item,
343 xxxp->downstream_hook.hook, m);
346 /* data came from the multiplexed link */
347 dlci = 1; /* get dlci from header */
348 /* madjust(....) *//* chop off header */
349 for (chan = 0; chan < XXX_NUM_DLCIS; chan++)
350 if (xxxp->channel[chan].dlci == dlci)
352 if (chan == XXX_NUM_DLCIS) {
355 return (ENETUNREACH);
357 /* After this are run 'm' should be considered
359 NG_FWD_NEW_DATA(error, item,
360 xxxp->channel[chan].hook, m);
364 /* It's the debug hook, throw it away.. */
365 if (hook == xxxp->downstream_hook.hook) {
375 * If this were a device node, the data may have been received in response
377 * in which case it would probably look as follows:
383 /* get packet from device and send on */
386 NG_SEND_DATA_ONLY(error, xxxp->upstream_hook.hook, m);
387 /* see note above in xxx_rcvdata() */
388 /* and ng_xxx_connect() */
394 * Do local shutdown processing..
395 * All our links and the name have already been removed.
396 * If we are a persistent device, we might refuse to go away.
397 * In the case of a persistent node we signal the framework that we
398 * are still in business by clearing the NGF_INVALID bit. However
399 * If we find the NGF_REALLY_DIE bit set, this means that
400 * we REALLY need to die (e.g. hardware removed).
401 * This would have been set using the NG_NODE_REALLY_DIE(node)
402 * macro in some device dependent function (not shown here) before
403 * calling ng_rmnode_self().
406 ng_xxx_shutdown(node_p node)
408 const xxx_p privdata = NG_NODE_PRIVATE(node);
410 #ifndef PERSISTANT_NODE
411 NG_NODE_SET_PRIVATE(node, NULL);
413 free(privdata, M_NETGRAPH);
415 if (node->nd_flags & NGF_REALLY_DIE) {
417 * WE came here because the widget card is being unloaded,
418 * so stop being persistent.
419 * Actually undo all the things we did on creation.
421 NG_NODE_SET_PRIVATE(node, NULL);
422 NG_NODE_UNREF(privdata->node);
423 free(privdata, M_NETGRAPH);
426 NG_NODE_REVIVE(node); /* tell ng_rmnode() we will persist */
427 #endif /* PERSISTANT_NODE */
432 * This is called once we've already connected a new hook to the other node.
433 * It gives us a chance to balk at the last minute.
436 ng_xxx_connect(hook_p hook)
440 * If we were a driver running at other than splnet then
441 * we should set the QUEUE bit on the edge so that we
442 * will deliver by queing.
444 if /*it is the upstream hook */
445 NG_HOOK_FORCE_QUEUE(NG_HOOK_PEER(hook));
449 * If for some reason we want incoming date to be queued
450 * by the NETISR system and delivered later we can set the same bit on
451 * OUR hook. (maybe to allow unwinding of the stack)
454 if (NG_HOOK_PRIVATE(hook)) {
457 * If it's dlci 1023, requeue it so that it's handled
458 * at a lower priority. This is how a node decides to
459 * defer a data message.
461 dlci = ((struct XXX_hookinfo *) NG_HOOK_PRIVATE(hook))->dlci;
463 NG_HOOK_FORCE_QUEUE(hook);
466 /* otherwise be really amiable and just say "YUP that's OK by me! " */
473 * For this type, removal of the last link destroys the node
476 ng_xxx_disconnect(hook_p hook)
478 if (NG_HOOK_PRIVATE(hook))
479 ((struct XXX_hookinfo *) (NG_HOOK_PRIVATE(hook)))->hook = NULL;
480 if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
481 && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook)))) /* already shutting down? */
482 ng_rmnode_self(NG_HOOK_NODE(hook));