Deleted Added
sdiff udiff text old ( 132828 ) new ( 139238 )
full compact
1
2/*
3 * ng_sample.c
4 *
5 * Copyright (c) 1996-1999 Whistle Communications, Inc.
6 * All rights reserved.
7 *
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.
18 *
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
35 * OF SUCH DAMAGE.
36 *
37 * Author: Julian Elischer <julian@freebsd.org>
38 *
39 * $FreeBSD: head/sys/netgraph/ng_sample.c 139238 2004-12-23 18:39:13Z rik $
40 * $Whistle: ng_sample.c,v 1.13 1999/11/01 09:24:52 julian Exp $
41 */
42
43#include <sys/param.h>
44#include <sys/systm.h>
45#include <sys/kernel.h>
46#include <sys/mbuf.h>
47#include <sys/malloc.h>
48#include <sys/ctype.h>
49#include <sys/errno.h>
50#include <sys/syslog.h>
51
52#include <netgraph/ng_message.h>
53#include <netgraph/ng_parse.h>
54#include <netgraph/ng_sample.h>
55#include <netgraph/netgraph.h>
56
57/* If you do complicated mallocs you may want to do this */
58/* and use it for your mallocs */
59#ifdef NG_SEPARATE_MALLOC
60MALLOC_DEFINE(M_NETGRAPH_XXX, "netgraph_xxx", "netgraph xxx node ");
61#else
62#define M_NETGRAPH_XXX M_NETGRAPH
63#endif
64
65/*
66 * This section contains the netgraph method declarations for the
67 * sample node. These methods define the netgraph 'type'.
68 */
69
70static ng_constructor_t ng_xxx_constructor;
71static ng_rcvmsg_t ng_xxx_rcvmsg;
72static ng_shutdown_t ng_xxx_shutdown;
73static ng_newhook_t ng_xxx_newhook;
74static ng_connect_t ng_xxx_connect;
75static ng_rcvdata_t ng_xxx_rcvdata; /* note these are both ng_rcvdata_t */
76static ng_disconnect_t ng_xxx_disconnect;
77
78/* Parse type for struct ngxxxstat */
79static const struct ng_parse_struct_field ng_xxx_stat_type_fields[]
80 = NG_XXX_STATS_TYPE_INFO;
81static const struct ng_parse_type ng_xxx_stat_type = {
82 &ng_parse_struct_type,
83 &ng_xxx_stat_type_fields
84};
85
86/* List of commands and how to convert arguments to/from ASCII */
87static const struct ng_cmdlist ng_xxx_cmdlist[] = {
88 {
89 NGM_XXX_COOKIE,
90 NGM_XXX_GET_STATUS,
91 "getstatus",
92 NULL,
93 &ng_xxx_stat_type,
94 },
95 {
96 NGM_XXX_COOKIE,
97 NGM_XXX_SET_FLAG,
98 "setflag",
99 &ng_parse_int32_type,
100 NULL
101 },
102 { 0 }
103};
104
105/* Netgraph node type descriptor */
106static 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,
118};
119NETGRAPH_INIT(xxx, &typestruct);
120
121/* Information we store for each hook on each node */
122struct XXX_hookinfo {
123 int dlci; /* The DLCI it represents, -1 == downstream */
124 int channel; /* The channel representing this DLCI */
125 hook_p hook;
126};
127
128/* Information we store for each node */
129struct XXX {
130 struct XXX_hookinfo channel[XXX_NUM_DLCIS];
131 struct XXX_hookinfo downstream_hook;
132 node_p node; /* back pointer to node */
133 hook_p debughook;
134 u_int packets_in; /* packets in from downstream */
135 u_int packets_out; /* packets out towards downstream */
136 u_int32_t flags;
137};
138typedef struct XXX *xxx_p;
139
140/*
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.
144 *
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 :)
148 */
149static int
150ng_xxx_constructor(node_p node)
151{
152 xxx_p privdata;
153 int i;
154
155 /* Initialize private descriptor */
156 MALLOC(privdata, xxx_p, sizeof(*privdata), M_NETGRAPH,
157 M_NOWAIT | M_ZERO);
158 if (privdata == NULL)
159 return (ENOMEM);
160 for (i = 0; i < XXX_NUM_DLCIS; i++) {
161 privdata->channel[i].dlci = -2;
162 privdata->channel[i].channel = i;
163 }
164
165 /* Link structs together; this counts as our one reference to *nodep */
166 NG_NODE_SET_PRIVATE(node, privdata);
167 privdata->node = node;
168 return (0);
169}
170
171/*
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)
182 */
183static int
184ng_xxx_newhook(node_p node, hook_p hook, const char *name)
185{
186 const xxx_p xxxp = NG_NODE_PRIVATE(node);
187 const char *cp;
188 int dlci = 0;
189 int chan;
190
191#if 0
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);
195 }
196#endif
197
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
201 * file. */
202 if (strncmp(name,
203 NG_XXX_HOOK_DLCI_LEADIN, strlen(NG_XXX_HOOK_DLCI_LEADIN)) == 0) {
204 char *eptr;
205
206 cp = name + strlen(NG_XXX_HOOK_DLCI_LEADIN);
207 if (!isdigit(*cp) || (cp[0] == '0' && cp[1] != '\0'))
208 return (EINVAL);
209 dlci = (int)strtoul(cp, &eptr, 10);
210 if (*eptr != '\0' || dlci < 0 || dlci > 1023)
211 return (EINVAL);
212
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)
216 break;
217 if (chan == XXX_NUM_DLCIS) {
218 for (chan = 0; chan < XXX_NUM_DLCIS; chan++)
219 if (xxxp->channel[chan].dlci != -2)
220 continue;
221 if (chan == XXX_NUM_DLCIS)
222 return (ENOBUFS);
223 }
224 if (xxxp->channel[chan].hook != NULL)
225 return (EADDRINUSE);
226 NG_HOOK_SET_PRIVATE(hook, xxxp->channel + chan);
227 xxxp->channel[chan].hook = hook;
228 return (0);
229 } else if (strcmp(name, NG_XXX_HOOK_DOWNSTREAM) == 0) {
230 /* Example of simple predefined hooks. */
231 /* do something specific to the downstream connection */
232 xxxp->downstream_hook.hook = hook;
233 NG_HOOK_SET_PRIVATE(hook, &xxxp->downstream_hook);
234 } else if (strcmp(name, NG_XXX_HOOK_DEBUG) == 0) {
235 /* do something specific to a debug connection */
236 xxxp->debughook = hook;
237 NG_HOOK_SET_PRIVATE(hook, NULL);
238 } else
239 return (EINVAL); /* not a hook we know about */
240 return(0);
241}
242
243/*
244 * Get a netgraph control message.
245 * We actually recieve a queue item that has a pointer to the message.
246 * If we free the item, the message will be freed too, unless we remove
247 * it from the item using NGI_GET_MSG();
248 * The return address is also stored in the item, as an ng_ID_t,
249 * accessible as NGI_RETADDR(item);
250 * Check it is one we understand. If needed, send a response.
251 * We could save the address for an async action later, but don't here.
252 * Always free the message.
253 * The response should be in a malloc'd region that the caller can 'free'.
254 * A response is not required.
255 * Theoretically you could respond defferently to old message types if
256 * the cookie in the header didn't match what we consider to be current
257 * (so that old userland programs could continue to work).
258 */
259static int
260ng_xxx_rcvmsg(node_p node, item_p item, hook_p lasthook)
261{
262 const xxx_p xxxp = NG_NODE_PRIVATE(node);
263 struct ng_mesg *resp = NULL;
264 int error = 0;
265 struct ng_mesg *msg;
266
267 NGI_GET_MSG(item, msg);
268 /* Deal with message according to cookie and command */
269 switch (msg->header.typecookie) {
270 case NGM_XXX_COOKIE:
271 switch (msg->header.cmd) {
272 case NGM_XXX_GET_STATUS:
273 {
274 struct ngxxxstat *stats;
275
276 NG_MKRESPONSE(resp, msg, sizeof(*stats), M_NOWAIT);
277 if (!resp) {
278 error = ENOMEM;
279 break;
280 }
281 stats = (struct ngxxxstat *) resp->data;
282 stats->packets_in = xxxp->packets_in;
283 stats->packets_out = xxxp->packets_out;
284 break;
285 }
286 case NGM_XXX_SET_FLAG:
287 if (msg->header.arglen != sizeof(u_int32_t)) {
288 error = EINVAL;
289 break;
290 }
291 xxxp->flags = *((u_int32_t *) msg->data);
292 break;
293 default:
294 error = EINVAL; /* unknown command */
295 break;
296 }
297 break;
298 default:
299 error = EINVAL; /* unknown cookie type */
300 break;
301 }
302
303 /* Take care of synchronous response, if any */
304 NG_RESPOND_MSG(error, node, item, resp);
305 /* Free the message and return */
306 NG_FREE_MSG(msg);
307 return(error);
308}
309
310/*
311 * Receive data, and do something with it.
312 * Actually we receive a queue item which holds the data.
313 * If we free the item it will also free the data unless we have
314 * previously disassociated it using the NGI_GET_M() macro.
315 * Possibly send it out on another link after processing.
316 * Possibly do something different if it comes from different
317 * hooks. The caller will never free m, so if we use up this data or
318 * abort we must free it.
319 *
320 * If we want, we may decide to force this data to be queued and reprocessed
321 * at the netgraph NETISR time.
322 * We would do that by setting the HK_QUEUE flag on our hook. We would do that
323 * in the connect() method.
324 */
325static int
326ng_xxx_rcvdata(hook_p hook, item_p item )
327{
328 const xxx_p xxxp = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
329 int chan = -2;
330 int dlci = -2;
331 int error;
332 struct mbuf *m;
333
334 NGI_GET_M(item, m);
335 if (NG_HOOK_PRIVATE(hook)) {
336 dlci = ((struct XXX_hookinfo *) NG_HOOK_PRIVATE(hook))->dlci;
337 chan = ((struct XXX_hookinfo *) NG_HOOK_PRIVATE(hook))->channel;
338 if (dlci != -1) {
339 /* If received on a DLCI hook process for this
340 * channel and pass it to the downstream module.
341 * Normally one would add a multiplexing header at
342 * the front here */
343 /* M_PREPEND(....) ; */
344 /* mtod(m, xxxxxx)->dlci = dlci; */
345 NG_FWD_NEW_DATA(error, item,
346 xxxp->downstream_hook.hook, m);
347 xxxp->packets_out++;
348 } else {
349 /* data came from the multiplexed link */
350 dlci = 1; /* get dlci from header */
351 /* madjust(....) *//* chop off header */
352 for (chan = 0; chan < XXX_NUM_DLCIS; chan++)
353 if (xxxp->channel[chan].dlci == dlci)
354 break;
355 if (chan == XXX_NUM_DLCIS) {
356 NG_FREE_ITEM(item);
357 NG_FREE_M(m);
358 return (ENETUNREACH);
359 }
360 /* If we were called at splnet, use the following:
361 * NG_SEND_DATA_ONLY(error, otherhook, m); if this
362 * node is running at some SPL other than SPLNET
363 * then you should use instead: error =
364 * ng_queueit(otherhook, m, NULL); m = NULL;
365 * This queues the data using the standard NETISR
366 * system and schedules the data to be picked
367 * up again once the system has moved to SPLNET and
368 * the processing of the data can continue. After
369 * these are run 'm' should be considered
370 * as invalid and NG_SEND_DATA actually zaps them. */
371 NG_FWD_NEW_DATA(error, item,
372 xxxp->channel[chan].hook, m);
373 xxxp->packets_in++;
374 }
375 } else {
376 /* It's the debug hook, throw it away.. */
377 if (hook == xxxp->downstream_hook.hook) {
378 NG_FREE_ITEM(item);
379 NG_FREE_M(m);
380 }
381 }
382 return 0;
383}
384
385#if 0
386/*
387 * If this were a device node, the data may have been received in response
388 * to some interrupt.
389 * in which case it would probably look as follows:
390 */
391devintr()
392{
393 int error;
394 * here */
395
396 /* get packet from device and send on */
397 m = MGET(blah blah)
398
399 NG_SEND_DATA_ONLY(error, xxxp->upstream_hook.hook, m);
400 /* see note above in xxx_rcvdata() */
401 /* and ng_xxx_connect() */
402}
403
404#endif /* 0 */
405
406/*
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().
417 */
418static int
419ng_xxx_shutdown(node_p node)
420{
421 const xxx_p privdata = NG_NODE_PRIVATE(node);
422
423#ifndef PERSISTANT_NODE
424 NG_NODE_SET_PRIVATE(node, NULL);
425 NG_NODE_UNREF(node);
426 FREE(privdata, M_NETGRAPH);
427#else
428 if (node->nd_flags & NGF_REALLY_DIE) {
429 /*
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.
433 */
434 NG_NODE_SET_PRIVATE(node, NULL);
435 NG_NODE_UNREF(privdata->node);
436 FREE(privdata, M_NETGRAPH);
437 return (0);
438 }
439 NG_NODE_REVIVE(node); /* tell ng_rmnode() we will persist */
440#endif /* PERSISTANT_NODE */
441 return (0);
442}
443
444/*
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.
447 */
448static int
449ng_xxx_connect(hook_p hook)
450{
451#if 0
452 /*
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.
456 */
457 if /*it is the upstream hook */
458 NG_HOOK_FORCE_QUEUE(NG_HOOK_PEER(hook));
459#endif
460#if 0
461 /*
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)
465 */
466
467 if (NG_HOOK_PRIVATE(hook)) {
468 int dlci;
469 /*
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.
473 */
474 dlci = ((struct XXX_hookinfo *) NG_HOOK_PRIVATE(hook))->dlci;
475 if (dlci == 1023) {
476 NG_HOOK_FORCE_QUEUE(hook);
477 }
478#endif
479 /* otherwise be really amiable and just say "YUP that's OK by me! " */
480 return (0);
481}
482
483/*
484 * Hook disconnection
485 *
486 * For this type, removal of the last link destroys the node
487 */
488static int
489ng_xxx_disconnect(hook_p hook)
490{
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));
496 return (0);
497}
498