ng_sample.c revision 70159
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 70159 2000-12-18 20:03:32Z julian $
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/*
58 * This section contains the netgraph method declarations for the
59 * sample node. These methods define the netgraph 'type'.
60 */
61
62static ng_constructor_t	ng_xxx_constructor;
63static ng_rcvmsg_t	ng_xxx_rcvmsg;
64static ng_shutdown_t	ng_xxx_rmnode;
65static ng_newhook_t	ng_xxx_newhook;
66static ng_connect_t	ng_xxx_connect;
67static ng_rcvdata_t	ng_xxx_rcvdata;	 /* note these are both ng_rcvdata_t */
68static ng_disconnect_t	ng_xxx_disconnect;
69
70/* Parse type for struct ngxxxstat */
71static const struct ng_parse_struct_info
72	ng_xxx_stat_type_info = NG_XXX_STATS_TYPE_INFO;
73static const struct ng_parse_type ng_xxx_stat_type = {
74	&ng_parse_struct_type,
75	&ng_xxx_stat_type_info
76};
77
78/* List of commands and how to convert arguments to/from ASCII */
79static const struct ng_cmdlist ng_xxx_cmdlist[] = {
80	{
81	  NGM_XXX_COOKIE,
82	  NGM_XXX_GET_STATUS,
83	  "getstatus",
84	  NULL,
85	  &ng_xxx_stat_type,
86	},
87	{
88	  NGM_XXX_COOKIE,
89	  NGM_XXX_SET_FLAG,
90	  "setflag",
91	  &ng_parse_int32_type,
92	  NULL
93	},
94	{ 0 }
95};
96
97/* Netgraph node type descriptor */
98static struct ng_type typestruct = {
99	NG_ABI_VERSION,
100	NG_XXX_NODE_TYPE,
101	NULL,
102	ng_xxx_constructor,
103	ng_xxx_rcvmsg,
104	ng_xxx_rmnode,
105	ng_xxx_newhook,
106	NULL,
107	ng_xxx_connect,
108	ng_xxx_rcvdata,
109	ng_xxx_disconnect,
110	ng_xxx_cmdlist
111};
112NETGRAPH_INIT(xxx, &typestruct);
113
114/* Information we store for each hook on each node */
115struct XXX_hookinfo {
116	int     dlci;		/* The DLCI it represents, -1 == downstream */
117	int     channel;	/* The channel representing this DLCI */
118	hook_p  hook;
119};
120
121/* Information we store for each node */
122struct XXX {
123	struct XXX_hookinfo channel[XXX_NUM_DLCIS];
124	struct XXX_hookinfo downstream_hook;
125	node_p		node;		/* back pointer to node */
126	hook_p  	debughook;
127	u_int   	packets_in;	/* packets in from downstream */
128	u_int   	packets_out;	/* packets out towards downstream */
129	u_int32_t	flags;
130};
131typedef struct XXX *xxx_p;
132
133/*
134 * Allocate the private data structure and the generic node
135 * and link them together.
136 *
137 * ng_make_node_common() returns with a generic node struct
138 * with a single reference for us.. we transfer it to the
139 * private structure.. when we free the private struct we must
140 * unref the node so it gets freed too.
141 *
142 * If this were a device node than this work would be done in the attach()
143 * routine and the constructor would return EINVAL as you should not be able
144 * to creatednodes that depend on hardware (unless you can add the hardware :)
145 */
146static int
147ng_xxx_constructor(node_p *nodep)
148{
149	xxx_p privdata;
150	int i, error;
151
152	/* Initialize private descriptor */
153	MALLOC(privdata, xxx_p, sizeof(*privdata), M_NETGRAPH,
154		M_NOWAIT | M_ZERO);
155	if (privdata == NULL)
156		return (ENOMEM);
157	for (i = 0; i < XXX_NUM_DLCIS; i++) {
158		privdata->channel[i].dlci = -2;
159		privdata->channel[i].channel = i;
160	}
161
162	/* Call the 'generic' (ie, superclass) node constructor */
163	if ((error = ng_make_node_common(&typestruct, nodep))) {
164		FREE(privdata, M_NETGRAPH);
165		return (error);
166	}
167
168	/* Link structs together; this counts as our one reference to *nodep */
169	(*nodep)->private = privdata;
170	privdata->node = *nodep;
171	return (0);
172}
173
174/*
175 * Give our ok for a hook to be added...
176 * If we are not running this might kick a device into life.
177 * Possibly decode information out of the hook name.
178 * Add the hook's private info to the hook structure.
179 * (if we had some). In this example, we assume that there is a
180 * an array of structs, called 'channel' in the private info,
181 * one for each active channel. The private
182 * pointer of each hook points to the appropriate XXX_hookinfo struct
183 * so that the source of an input packet is easily identified.
184 * (a dlci is a frame relay channel)
185 */
186static int
187ng_xxx_newhook(node_p node, hook_p hook, const char *name)
188{
189	const xxx_p xxxp = node->private;
190	const char *cp;
191	int dlci = 0;
192	int chan;
193
194#if 0
195	/* Possibly start up the device if it's not already going */
196	if ((xxxp->flags & SCF_RUNNING) == 0) {
197		ng_xxx_start_hardware(xxxp);
198	}
199#endif
200
201	/* Example of how one might use hooks with embedded numbers: All
202	 * hooks start with 'dlci' and have a decimal trailing channel
203	 * number up to 4 digits Use the leadin defined int he associated .h
204	 * file. */
205	if (strncmp(name,
206	    NG_XXX_HOOK_DLCI_LEADIN, strlen(NG_XXX_HOOK_DLCI_LEADIN)) == 0) {
207		char *eptr;
208
209		cp = name + sizeof(NG_XXX_HOOK_DLCI_LEADIN);
210		if (!isdigit(*cp) || (cp[0] == '0' && cp[1] != '\0'))
211			return (EINVAL);
212		dlci = (int)strtoul(cp, &eptr, 10);
213		if (*eptr != '\0' || dlci < 0 || dlci > 1023)
214			return (EINVAL);
215
216		/* We have a dlci, now either find it, or allocate it */
217		for (chan = 0; chan < XXX_NUM_DLCIS; chan++)
218			if (xxxp->channel[chan].dlci == dlci)
219				break;
220		if (chan == XXX_NUM_DLCIS) {
221			for (chan = 0; chan < XXX_NUM_DLCIS; chan++)
222				if (xxxp->channel[chan].dlci != -2)
223					continue;
224			if (chan == XXX_NUM_DLCIS)
225				return (ENOBUFS);
226		}
227		if (xxxp->channel[chan].hook != NULL)
228			return (EADDRINUSE);
229		hook->private = xxxp->channel + chan;
230		xxxp->channel[chan].hook = hook;
231		return (0);
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		hook->private = &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		hook->private = NULL;
241	} else
242		return (EINVAL);	/* not a hook we know about */
243	return(0);
244}
245
246/*
247 * Get a netgraph control message.
248 * Check it is one we understand. If needed, send a response.
249 * We could save the address for an async action later, but don't here.
250 * Always free the message.
251 * The response should be in a malloc'd region that the caller can 'free'.
252 * A response is not required.
253 * Theoretically you could respond defferently to old message types if
254 * the cookie in the header didn't match what we consider to be current
255 * (so that old userland programs could continue to work).
256 */
257static int
258ng_xxx_rcvmsg(node_p node, struct ng_mesg *msg, const char *retaddr,
259		struct ng_mesg **rptr, hook_p lasthook)
260{
261	const xxx_p xxxp = node->private;
262	struct ng_mesg *resp = NULL;
263	int error = 0;
264
265	/* Deal with message according to cookie and command */
266	switch (msg->header.typecookie) {
267	case NGM_XXX_COOKIE:
268		switch (msg->header.cmd) {
269		case NGM_XXX_GET_STATUS:
270		    {
271			struct ngxxxstat *stats;
272
273			NG_MKRESPONSE(resp, msg, sizeof(*stats), M_NOWAIT);
274			if (!resp) {
275				error = ENOMEM;
276				break;
277			}
278			stats = (struct ngxxxstat *) resp->data;
279			stats->packets_in = xxxp->packets_in;
280			stats->packets_out = xxxp->packets_out;
281			break;
282		    }
283		case NGM_XXX_SET_FLAG:
284			if (msg->header.arglen != sizeof(u_int32_t)) {
285				error = EINVAL;
286				break;
287			}
288			xxxp->flags = *((u_int32_t *) msg->data);
289			break;
290		default:
291			error = EINVAL;		/* unknown command */
292			break;
293		}
294		break;
295	default:
296		error = EINVAL;			/* unknown cookie type */
297		break;
298	}
299
300	/* Take care of synchronous response, if any */
301	if (rptr)
302		*rptr = resp;
303	else if (resp)
304		FREE(resp, M_NETGRAPH);
305
306	/* Free the message and return */
307	FREE(msg, M_NETGRAPH);
308	return(error);
309}
310
311/*
312 * Receive data, and do something with it.
313 * Possibly send it out on another link after processing.
314 * Possibly do something different if it comes from different
315 * hooks. the caller will never free m or meta, so
316 * if we use up this data or abort we must free BOTH of these.
317 *
318 * If we want, we may decide to force this data to be queued and reprocessed
319 * at the netgraph NETISR time.
320 * We would do that by setting the HK_QUEUE flag on our hook. We would do that
321 * in the connect() method.
322 */
323static int
324ng_xxx_rcvdata(hook_p hook, struct mbuf *m, meta_p meta,
325		struct mbuf **ret_m, meta_p *ret_meta, struct ng_mesg **resp)
326{
327	const xxx_p xxxp = hook->node->private;
328	int chan = -2;
329	int dlci = -2;
330	int error;
331
332	if (hook->private) {
333		dlci = ((struct XXX_hookinfo *) hook->private)->dlci;
334		chan = ((struct XXX_hookinfo *) hook->private)->channel;
335		if (dlci != -1) {
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
339			 * the front here */
340			/* M_PREPEND(....)	; */
341			/* mtod(m, xxxxxx)->dlci = dlci; */
342			NG_SEND_DATA(error, xxxp->downstream_hook.hook,
343			    m, meta);
344			xxxp->packets_out++;
345		} else {
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)
351					break;
352			if (chan == XXX_NUM_DLCIS) {
353				NG_FREE_DATA(m, meta);
354				return (ENETUNREACH);
355			}
356			/* If we were called at splnet, use the following:
357			 * NG_SEND_DATA(error, otherhook, m, meta); if this
358			 * node is running at some SPL other than SPLNET
359			 * then you should use instead: error =
360			 * ng_queueit(otherhook, m, meta); m = NULL: meta =
361			 * NULL; this queues the data using the standard
362			 * NETISR system and schedules the data to be picked
363			 * up again once the system has moved to SPLNET and
364			 * the processing of the data can continue. after
365			 * these are run 'm' and 'meta' should be considered
366			 * as invalid and NG_SEND_DATA actually zaps them. */
367			NG_SEND_DATA(error, xxxp->channel[chan].hook, m, meta);
368			xxxp->packets_in++;
369		}
370	} else {
371		/* It's the debug hook, throw it away.. */
372		if (hook == xxxp->downstream_hook.hook)
373			NG_FREE_DATA(m, meta);
374	}
375	return 0;
376}
377
378#if 0
379/*
380 * If this were a device node, the data may have been received in response
381 * to some interrupt.
382 * in which case it would probably look as follows:
383 */
384devintr()
385{
386	int error;
387				 * here */
388
389	/* get packet from device and send on */
390	m = MGET(blah blah)
391
392	NG_SEND_DATA_ONLY(error, xxxp->upstream_hook.hook, m);
393				/* see note above in xxx_rcvdata() */
394				/* and ng_xxx_connect() */
395}
396
397#endif				/* 0 */
398
399/*
400 * Do local shutdown processing..
401 * If we are a persistant device, we might refuse to go away, and
402 * we'd only remove our links and reset ourself.
403 */
404static int
405ng_xxx_rmnode(node_p node)
406{
407	const xxx_p privdata = node->private;
408
409	node->flags |= NG_INVALID;
410	ng_cutlinks(node);
411#ifndef PERSISTANT_NODE
412	ng_unname(node);
413	node->private = NULL;
414	ng_unref(privdata->node);
415	FREE(privdata, M_NETGRAPH);
416#else
417	privdata->packets_in = 0;		/* reset stats */
418	privdata->packets_out = 0;
419	node->flags &= ~NG_INVALID;		/* reset invalid flag */
420#endif /* PERSISTANT_NODE */
421	return (0);
422}
423
424/*
425 * This is called once we've already connected a new hook to the other node.
426 * It gives us a chance to balk at the last minute.
427 */
428static int
429ng_xxx_connect(hook_p hook)
430{
431#if 0
432	/*
433	 * If we were a driver running at other than splnet then
434	 * we should set the QUEUE bit on the edge so that we
435	 * will deliver by queing.
436	 */
437	if /*it is the upstream hook */
438	hook->peer->flags |= HK_QUEUE;
439#endif
440#if 0
441	/*
442	 * If for some reason we want incoming date to be queued
443	 * by the NETISR system and delivered later we can set the same bit on
444	 * OUR hook. (maybe to allow unwinding of the stack)
445	 */
446
447	if (hook->private) {
448		int dlci;
449		/*
450		 * If it's dlci 1023, requeue it so that it's handled
451		 * at a lower priority. This is how a node decides to
452		 * defer a data message.
453		 */
454		dlci = ((struct XXX_hookinfo *) hook->private)->dlci;
455		if (dlci == 1023) {
456			hook->flags |= HK_QUEUE;
457		}
458#endif
459	/* otherwise be really amiable and just say "YUP that's OK by me! " */
460	return (0);
461}
462
463/*
464 * Dook disconnection
465 *
466 * For this type, removal of the last link destroys the node
467 */
468static int
469ng_xxx_disconnect(hook_p hook)
470{
471	if (hook->private)
472		((struct XXX_hookinfo *) (hook->private))->hook = NULL;
473	if (hook->node->numhooks == 0)
474		ng_rmnode(hook->node);
475	return (0);
476}
477
478