amd64 port: mainly on the pmap headers, identify_cpu and initcpu
[dragonfly/port-amd64.git] / sys / netgraph / ng_sample.c
blob40d6fb21557446e86b8323779279b9b36744a46f
2 /*
3 * ng_sample.c
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.
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.
37 * Author: Julian Elischer <julian@freebsd.org>
39 * $FreeBSD: src/sys/netgraph/ng_sample.c,v 1.7.2.3 2002/07/02 23:44:03 archie Exp $
40 * $DragonFly: src/sys/netgraph/ng_sample.c,v 1.3 2006/01/14 11:10:47 swildner Exp $
41 * $Whistle: ng_sample.c,v 1.13 1999/11/01 09:24:52 julian Exp $
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/mbuf.h>
48 #include <sys/malloc.h>
49 #include <sys/ctype.h>
50 #include <sys/errno.h>
51 #include <sys/syslog.h>
53 #include <netgraph/ng_message.h>
54 #include <netgraph/ng_parse.h>
55 #include <netgraph/ng_sample.h>
56 #include <netgraph/netgraph.h>
59 * This section contains the netgraph method declarations for the
60 * sample node. These methods define the netgraph 'type'.
63 static ng_constructor_t ng_xxx_constructor;
64 static ng_rcvmsg_t ng_xxx_rcvmsg;
65 static ng_shutdown_t ng_xxx_rmnode;
66 static ng_newhook_t ng_xxx_newhook;
67 static ng_connect_t ng_xxx_connect;
68 static ng_rcvdata_t ng_xxx_rcvdata; /* note these are both ng_rcvdata_t */
69 static ng_rcvdata_t ng_xxx_rcvdataq; /* note these are both ng_rcvdata_t */
70 static ng_disconnect_t ng_xxx_disconnect;
72 /* Parse type for struct ngxxxstat */
73 static const struct ng_parse_struct_field ng_xxx_stat_type_fields[]
74 = NG_XXX_STATS_TYPE_INFO;
75 static const struct ng_parse_type ng_xxx_stat_type = {
76 &ng_parse_struct_type,
77 &ng_xxx_stat_type_fields
80 /* List of commands and how to convert arguments to/from ASCII */
81 static const struct ng_cmdlist ng_xxx_cmdlist[] = {
83 NGM_XXX_COOKIE,
84 NGM_XXX_GET_STATUS,
85 "getstatus",
86 NULL,
87 &ng_xxx_stat_type,
90 NGM_XXX_COOKIE,
91 NGM_XXX_SET_FLAG,
92 "setflag",
93 &ng_parse_int32_type,
94 NULL
96 { 0 }
99 /* Netgraph node type descriptor */
100 static struct ng_type typestruct = {
101 NG_VERSION,
102 NG_XXX_NODE_TYPE,
103 NULL,
104 ng_xxx_constructor,
105 ng_xxx_rcvmsg,
106 ng_xxx_rmnode,
107 ng_xxx_newhook,
108 NULL,
109 ng_xxx_connect,
110 ng_xxx_rcvdata,
111 ng_xxx_rcvdataq,
112 ng_xxx_disconnect,
113 ng_xxx_cmdlist
115 NETGRAPH_INIT(xxx, &typestruct);
117 /* Information we store for each hook on each node */
118 struct XXX_hookinfo {
119 int dlci; /* The DLCI it represents, -1 == downstream */
120 int channel; /* The channel representing this DLCI */
121 hook_p hook;
124 /* Information we store for each node */
125 struct XXX {
126 struct XXX_hookinfo channel[XXX_NUM_DLCIS];
127 struct XXX_hookinfo downstream_hook;
128 node_p node; /* back pointer to node */
129 hook_p debughook;
130 u_int packets_in; /* packets in from downstream */
131 u_int packets_out; /* packets out towards downstream */
132 u_int32_t flags;
134 typedef struct XXX *xxx_p;
137 * Allocate the private data structure and the generic node
138 * and link them together.
140 * ng_make_node_common() returns with a generic node struct
141 * with a single reference for us.. we transfer it to the
142 * private structure.. when we free the private struct we must
143 * unref the node so it gets freed too.
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 :)
149 static int
150 ng_xxx_constructor(node_p *nodep)
152 xxx_p privdata;
153 int i, error;
155 /* Initialize private descriptor */
156 MALLOC(privdata, xxx_p, sizeof(*privdata), M_NETGRAPH, M_NOWAIT);
157 if (privdata == NULL)
158 return (ENOMEM);
159 bzero(privdata, sizeof(struct XXX));
160 for (i = 0; i < XXX_NUM_DLCIS; i++) {
161 privdata->channel[i].dlci = -2;
162 privdata->channel[i].channel = i;
165 /* Call the 'generic' (ie, superclass) node constructor */
166 if ((error = ng_make_node_common(&typestruct, nodep))) {
167 FREE(privdata, M_NETGRAPH);
168 return (error);
171 /* Link structs together; this counts as our one reference to *nodep */
172 (*nodep)->private = privdata;
173 privdata->node = *nodep;
174 return (0);
178 * Give our ok for a hook to be added...
179 * If we are not running this might kick a device into life.
180 * Possibly decode information out of the hook name.
181 * Add the hook's private info to the hook structure.
182 * (if we had some). In this example, we assume that there is a
183 * an array of structs, called 'channel' in the private info,
184 * one for each active channel. The private
185 * pointer of each hook points to the appropriate XXX_hookinfo struct
186 * so that the source of an input packet is easily identified.
187 * (a dlci is a frame relay channel)
189 static int
190 ng_xxx_newhook(node_p node, hook_p hook, const char *name)
192 const xxx_p xxxp = node->private;
193 const char *cp;
194 int dlci = 0;
195 int chan;
197 #if 0
198 /* Possibly start up the device if it's not already going */
199 if ((xxxp->flags & SCF_RUNNING) == 0) {
200 ng_xxx_start_hardware(xxxp);
202 #endif
204 /* Example of how one might use hooks with embedded numbers: All
205 * hooks start with 'dlci' and have a decimal trailing channel
206 * number up to 4 digits Use the leadin defined int he associated .h
207 * file. */
208 if (strncmp(name,
209 NG_XXX_HOOK_DLCI_LEADIN, strlen(NG_XXX_HOOK_DLCI_LEADIN)) == 0) {
210 char *eptr;
212 cp = name + sizeof(NG_XXX_HOOK_DLCI_LEADIN);
213 if (!isdigit(*cp) || (cp[0] == '0' && cp[1] != '\0'))
214 return (EINVAL);
215 dlci = (int)strtoul(cp, &eptr, 10);
216 if (*eptr != '\0' || dlci < 0 || dlci > 1023)
217 return (EINVAL);
219 /* We have a dlci, now either find it, or allocate it */
220 for (chan = 0; chan < XXX_NUM_DLCIS; chan++)
221 if (xxxp->channel[chan].dlci == dlci)
222 break;
223 if (chan == XXX_NUM_DLCIS) {
224 for (chan = 0; chan < XXX_NUM_DLCIS; chan++)
225 if (xxxp->channel[chan].dlci != -2)
226 continue;
227 if (chan == XXX_NUM_DLCIS)
228 return (ENOBUFS);
230 if (xxxp->channel[chan].hook != NULL)
231 return (EADDRINUSE);
232 hook->private = xxxp->channel + chan;
233 xxxp->channel[chan].hook = hook;
234 return (0);
235 } else if (strcmp(name, NG_XXX_HOOK_DOWNSTREAM) == 0) {
236 /* Example of simple predefined hooks. */
237 /* do something specific to the downstream connection */
238 xxxp->downstream_hook.hook = hook;
239 hook->private = &xxxp->downstream_hook;
240 } else if (strcmp(name, NG_XXX_HOOK_DEBUG) == 0) {
241 /* do something specific to a debug connection */
242 xxxp->debughook = hook;
243 hook->private = NULL;
244 } else
245 return (EINVAL); /* not a hook we know about */
246 return(0);
250 * Get a netgraph control message.
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).
260 static int
261 ng_xxx_rcvmsg(node_p node,
262 struct ng_mesg *msg, const char *retaddr, struct ng_mesg **rptr)
264 const xxx_p xxxp = node->private;
265 struct ng_mesg *resp = NULL;
266 int error = 0;
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:
274 struct ngxxxstat *stats;
276 NG_MKRESPONSE(resp, msg, sizeof(*stats), M_NOWAIT);
277 if (!resp) {
278 error = ENOMEM;
279 break;
281 stats = (struct ngxxxstat *) resp->data;
282 stats->packets_in = xxxp->packets_in;
283 stats->packets_out = xxxp->packets_out;
284 break;
286 case NGM_XXX_SET_FLAG:
287 if (msg->header.arglen != sizeof(u_int32_t)) {
288 error = EINVAL;
289 break;
291 xxxp->flags = *((u_int32_t *) msg->data);
292 break;
293 default:
294 error = EINVAL; /* unknown command */
295 break;
297 break;
298 default:
299 error = EINVAL; /* unknown cookie type */
300 break;
303 /* Take care of synchronous response, if any */
304 if (rptr)
305 *rptr = resp;
306 else if (resp)
307 FREE(resp, M_NETGRAPH);
309 /* Free the message and return */
310 FREE(msg, M_NETGRAPH);
311 return(error);
315 * Receive data, and do something with it.
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 or meta, so
319 * if we use up this data or abort we must free BOTH of these.
321 * If we want, we may decide to force this data to be queued and reprocessed
322 * at the netgraph NETISR time. (at which time it will be entered using ng_xxx_rcvdataq().
324 static int
325 ng_xxx_rcvdata(hook_p hook, struct mbuf *m, meta_p meta)
327 int dlci = -2;
329 if (hook->private) {
331 * If it's dlci 1023, requeue it so that it's handled
332 * at a lower priority. This is how a node decides to
333 * defer a data message.
335 dlci = ((struct XXX_hookinfo *) hook->private)->dlci;
336 if (dlci == 1023) {
337 return(ng_queue_data(hook->peer, m, meta));
340 return(ng_xxx_rcvdataq(hook, m, meta));
344 * Always accept the data. This version of rcvdata is called from the dequeueing routine.
346 static int
347 ng_xxx_rcvdataq(hook_p hook, struct mbuf *m, meta_p meta)
349 const xxx_p xxxp = hook->node->private;
350 int chan = -2;
351 int dlci = -2;
352 int error;
354 if (hook->private) {
355 dlci = ((struct XXX_hookinfo *) hook->private)->dlci;
356 chan = ((struct XXX_hookinfo *) hook->private)->channel;
357 if (dlci != -1) {
358 /* If received on a DLCI hook process for this
359 * channel and pass it to the downstream module.
360 * Normally one would add a multiplexing header at
361 * the front here */
362 /* M_PREPEND(....) ; */
363 /* mtod(m, xxxxxx)->dlci = dlci; */
364 error = ng_send_data(xxxp->downstream_hook.hook,
365 m, meta);
366 xxxp->packets_out++;
367 } else {
368 /* data came from the multiplexed link */
369 dlci = 1; /* get dlci from header */
370 /* madjust(....) *//* chop off header */
371 for (chan = 0; chan < XXX_NUM_DLCIS; chan++)
372 if (xxxp->channel[chan].dlci == dlci)
373 break;
374 if (chan == XXX_NUM_DLCIS) {
375 NG_FREE_DATA(m, meta);
376 return (ENETUNREACH);
378 /* If we were called at splnet, use the following:
379 * NG_SEND_DATA(error, otherhook, m, meta); if this
380 * node is running at some SPL other than SPLNET
381 * then you should use instead: error =
382 * ng_queueit(otherhook, m, meta); m = NULL: meta =
383 * NULL; this queues the data using the standard
384 * NETISR system and schedules the data to be picked
385 * up again once the system has moved to SPLNET and
386 * the processing of the data can continue. after
387 * these are run 'm' and 'meta' should be considered
388 * as invalid and NG_SEND_DATA actually zaps them. */
389 NG_SEND_DATA(error, xxxp->channel[chan].hook, m, meta);
390 xxxp->packets_in++;
392 } else {
393 /* It's the debug hook, throw it away.. */
394 if (hook == xxxp->downstream_hook.hook)
395 NG_FREE_DATA(m, meta);
397 return 0;
400 #if 0
402 * If this were a device node, the data may have been received in response
403 * to some interrupt.
404 * in which case it would probably look as follows:
406 void
407 devintr(void)
409 meta_p meta = NULL; /* whatever metadata we might imagine goes
410 * here */
412 /* get packet from device and send on */
413 m = MGET(blah blah)
414 error = ng_queueit(upstream, m, meta); /* see note above in
415 * xxx_rcvdata() */
418 #endif /* 0 */
421 * Do local shutdown processing..
422 * If we are a persistant device, we might refuse to go away, and
423 * we'd only remove our links and reset ourself.
425 static int
426 ng_xxx_rmnode(node_p node)
428 const xxx_p privdata = node->private;
430 node->flags |= NG_INVALID;
431 ng_cutlinks(node);
432 #ifndef PERSISTANT_NODE
433 ng_unname(node);
434 node->private = NULL;
435 ng_unref(privdata->node);
436 FREE(privdata, M_NETGRAPH);
437 #else
438 privdata->packets_in = 0; /* reset stats */
439 privdata->packets_out = 0;
440 node->flags &= ~NG_INVALID; /* reset invalid flag */
441 #endif /* PERSISTANT_NODE */
442 return (0);
446 * This is called once we've already connected a new hook to the other node.
447 * It gives us a chance to balk at the last minute.
449 static int
450 ng_xxx_connect(hook_p hook)
452 /* be really amiable and just say "YUP that's OK by me! " */
453 return (0);
457 * Dook disconnection
459 * For this type, removal of the last link destroys the node
461 static int
462 ng_xxx_disconnect(hook_p hook)
464 if (hook->private)
465 ((struct XXX_hookinfo *) (hook->private))->hook = NULL;
466 if (hook->node->numhooks == 0)
467 ng_rmnode(hook->node);
468 return (0);