1 /*
2 * ng_base.c
3 */
4
5 /*-
6 * Copyright (c) 1996-1999 Whistle Communications, Inc.
7 * All rights reserved.
8 *
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.
19 *
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
36 * OF SUCH DAMAGE.
37 *
38 * Authors: Julian Elischer <julian@freebsd.org>
39 * Archie Cobbs <archie@freebsd.org>
40 *
41 * $FreeBSD$
42 * $Whistle: ng_base.c,v 1.39 1999/01/28 23:54:53 julian Exp $
43 */
44
45 /*
46 * This file implements the base netgraph code.
47 */
48
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/ctype.h>
52 #include <sys/errno.h>
53 #include <sys/kdb.h>
54 #include <sys/kernel.h>
55 #include <sys/ktr.h>
56 #include <sys/limits.h>
57 #include <sys/malloc.h>
58 #include <sys/mbuf.h>
59 #include <sys/queue.h>
60 #include <sys/sysctl.h>
61 #include <sys/syslog.h>
62 #include <sys/refcount.h>
63 #include <sys/proc.h>
64
65 #include <net/netisr.h>
66
67 #include <netgraph/ng_message.h>
68 #include <netgraph/netgraph.h>
69 #include <netgraph/ng_parse.h>
70
71 MODULE_VERSION(netgraph, NG_ABI_VERSION);
72
73 /* Mutex to protect topology events. */
74 static struct mtx ng_topo_mtx;
75
76 #ifdef NETGRAPH_DEBUG
77 static struct mtx ng_nodelist_mtx; /* protects global node/hook lists */
78 static struct mtx ngq_mtx; /* protects the queue item list */
79
80 static SLIST_HEAD(, ng_node) ng_allnodes;
81 static LIST_HEAD(, ng_node) ng_freenodes; /* in debug, we never free() them */
82 static SLIST_HEAD(, ng_hook) ng_allhooks;
83 static LIST_HEAD(, ng_hook) ng_freehooks; /* in debug, we never free() them */
84
85 static void ng_dumpitems(void);
86 static void ng_dumpnodes(void);
87 static void ng_dumphooks(void);
88
89 #endif /* NETGRAPH_DEBUG */
90 /*
91 * DEAD versions of the structures.
92 * In order to avoid races, it is sometimes neccesary to point
93 * at SOMETHING even though theoretically, the current entity is
94 * INVALID. Use these to avoid these races.
95 */
96 struct ng_type ng_deadtype = {
97 NG_ABI_VERSION,
98 "dead",
99 NULL, /* modevent */
100 NULL, /* constructor */
101 NULL, /* rcvmsg */
102 NULL, /* shutdown */
103 NULL, /* newhook */
104 NULL, /* findhook */
105 NULL, /* connect */
106 NULL, /* rcvdata */
107 NULL, /* disconnect */
108 NULL, /* cmdlist */
109 };
110
111 struct ng_node ng_deadnode = {
112 "dead",
113 &ng_deadtype,
114 NGF_INVALID,
115 1, /* refs */
116 0, /* numhooks */
117 NULL, /* private */
118 0, /* ID */
119 LIST_HEAD_INITIALIZER(ng_deadnode.hooks),
120 {}, /* all_nodes list entry */
121 {}, /* id hashtable list entry */
122 {}, /* workqueue entry */
123 { 0,
124 {}, /* should never use! (should hang) */
125 NULL,
126 &ng_deadnode.nd_input_queue.queue,
127 &ng_deadnode
128 },
129 #ifdef NETGRAPH_DEBUG
130 ND_MAGIC,
131 __FILE__,
132 __LINE__,
133 {NULL}
134 #endif /* NETGRAPH_DEBUG */
135 };
136
137 struct ng_hook ng_deadhook = {
138 "dead",
139 NULL, /* private */
140 HK_INVALID | HK_DEAD,
141 1, /* refs always >= 1 */
142 0, /* undefined data link type */
143 &ng_deadhook, /* Peer is self */
144 &ng_deadnode, /* attached to deadnode */
145 {}, /* hooks list */
146 NULL, /* override rcvmsg() */
147 NULL, /* override rcvdata() */
148 #ifdef NETGRAPH_DEBUG
149 HK_MAGIC,
150 __FILE__,
151 __LINE__,
152 {NULL}
153 #endif /* NETGRAPH_DEBUG */
154 };
155
156 /*
157 * END DEAD STRUCTURES
158 */
159 /* List nodes with unallocated work */
160 static TAILQ_HEAD(, ng_node) ng_worklist = TAILQ_HEAD_INITIALIZER(ng_worklist);
161 static struct mtx ng_worklist_mtx; /* MUST LOCK NODE FIRST */
162
163 /* List of installed types */
164 static LIST_HEAD(, ng_type) ng_typelist;
165 static struct mtx ng_typelist_mtx;
166
167 /* Hash related definitions */
168 /* XXX Don't need to initialise them because it's a LIST */
169 #define NG_ID_HASH_SIZE 128 /* most systems wont need even this many */
170 static LIST_HEAD(, ng_node) ng_ID_hash[NG_ID_HASH_SIZE];
171 static struct mtx ng_idhash_mtx;
172 /* Method to find a node.. used twice so do it here */
173 #define NG_IDHASH_FN(ID) ((ID) % (NG_ID_HASH_SIZE))
174 #define NG_IDHASH_FIND(ID, node) \
175 do { \
176 mtx_assert(&ng_idhash_mtx, MA_OWNED); \
177 LIST_FOREACH(node, &ng_ID_hash[NG_IDHASH_FN(ID)], \
178 nd_idnodes) { \
179 if (NG_NODE_IS_VALID(node) \
180 && (NG_NODE_ID(node) == ID)) { \
181 break; \
182 } \
183 } \
184 } while (0)
185
186 #define NG_NAME_HASH_SIZE 128 /* most systems wont need even this many */
187 static LIST_HEAD(, ng_node) ng_name_hash[NG_NAME_HASH_SIZE];
188 static struct mtx ng_namehash_mtx;
189 #define NG_NAMEHASH(NAME, HASH) \
190 do { \
191 u_char h = 0; \
192 const u_char *c; \
193 for (c = (const u_char*)(NAME); *c; c++)\
194 h += *c; \
195 (HASH) = h % (NG_NAME_HASH_SIZE); \
196 } while (0)
197
198
199 /* Internal functions */
200 static int ng_add_hook(node_p node, const char *name, hook_p * hookp);
201 static int ng_generic_msg(node_p here, item_p item, hook_p lasthook);
202 static ng_ID_t ng_decodeidname(const char *name);
203 static int ngb_mod_event(module_t mod, int event, void *data);
204 static void ng_worklist_remove(node_p node);
205 static void ngintr(void);
206 static int ng_apply_item(node_p node, item_p item, int rw);
207 static void ng_flush_input_queue(struct ng_queue * ngq);
208 static void ng_setisr(node_p node);
209 static node_p ng_ID2noderef(ng_ID_t ID);
210 static int ng_con_nodes(item_p item, node_p node, const char *name,
211 node_p node2, const char *name2);
212 static int ng_con_part2(node_p node, item_p item, hook_p hook);
213 static int ng_con_part3(node_p node, item_p item, hook_p hook);
214 static int ng_mkpeer(node_p node, const char *name,
215 const char *name2, char *type);
216
217 /* Imported, these used to be externally visible, some may go back. */
218 void ng_destroy_hook(hook_p hook);
219 node_p ng_name2noderef(node_p node, const char *name);
220 int ng_path2noderef(node_p here, const char *path,
221 node_p *dest, hook_p *lasthook);
222 int ng_make_node(const char *type, node_p *nodepp);
223 int ng_path_parse(char *addr, char **node, char **path, char **hook);
224 void ng_rmnode(node_p node, hook_p dummy1, void *dummy2, int dummy3);
225 void ng_unname(node_p node);
226
227
228 /* Our own netgraph malloc type */
229 MALLOC_DEFINE(M_NETGRAPH, "netgraph", "netgraph structures and ctrl messages");
230 MALLOC_DEFINE(M_NETGRAPH_HOOK, "netgraph_hook", "netgraph hook structures");
231 MALLOC_DEFINE(M_NETGRAPH_NODE, "netgraph_node", "netgraph node structures");
232 MALLOC_DEFINE(M_NETGRAPH_ITEM, "netgraph_item", "netgraph item structures");
233 MALLOC_DEFINE(M_NETGRAPH_MSG, "netgraph_msg", "netgraph name storage");
234
235 /* Should not be visible outside this file */
236
237 #define _NG_ALLOC_HOOK(hook) \
238 MALLOC(hook, hook_p, sizeof(*hook), M_NETGRAPH_HOOK, M_NOWAIT | M_ZERO)
239 #define _NG_ALLOC_NODE(node) \
240 MALLOC(node, node_p, sizeof(*node), M_NETGRAPH_NODE, M_NOWAIT | M_ZERO)
241
242 #define NG_QUEUE_LOCK_INIT(n) \
243 mtx_init(&(n)->q_mtx, "ng_node", NULL, MTX_DEF)
244 #define NG_QUEUE_LOCK(n) \
245 mtx_lock(&(n)->q_mtx)
246 #define NG_QUEUE_UNLOCK(n) \
247 mtx_unlock(&(n)->q_mtx)
248 #define NG_WORKLIST_LOCK_INIT() \
249 mtx_init(&ng_worklist_mtx, "ng_worklist", NULL, MTX_DEF)
250 #define NG_WORKLIST_LOCK() \
251 mtx_lock(&ng_worklist_mtx)
252 #define NG_WORKLIST_UNLOCK() \
253 mtx_unlock(&ng_worklist_mtx)
254
255 #ifdef NETGRAPH_DEBUG /*----------------------------------------------*/
256 /*
257 * In debug mode:
258 * In an attempt to help track reference count screwups
259 * we do not free objects back to the malloc system, but keep them
260 * in a local cache where we can examine them and keep information safely
261 * after they have been freed.
262 * We use this scheme for nodes and hooks, and to some extent for items.
263 */
264 static __inline hook_p
265 ng_alloc_hook(void)
266 {
267 hook_p hook;
268 SLIST_ENTRY(ng_hook) temp;
269 mtx_lock(&ng_nodelist_mtx);
270 hook = LIST_FIRST(&ng_freehooks);
271 if (hook) {
272 LIST_REMOVE(hook, hk_hooks);
273 bcopy(&hook->hk_all, &temp, sizeof(temp));
274 bzero(hook, sizeof(struct ng_hook));
275 bcopy(&temp, &hook->hk_all, sizeof(temp));
276 mtx_unlock(&ng_nodelist_mtx);
277 hook->hk_magic = HK_MAGIC;
278 } else {
279 mtx_unlock(&ng_nodelist_mtx);
280 _NG_ALLOC_HOOK(hook);
281 if (hook) {
282 hook->hk_magic = HK_MAGIC;
283 mtx_lock(&ng_nodelist_mtx);
284 SLIST_INSERT_HEAD(&ng_allhooks, hook, hk_all);
285 mtx_unlock(&ng_nodelist_mtx);
286 }
287 }
288 return (hook);
289 }
290
291 static __inline node_p
292 ng_alloc_node(void)
293 {
294 node_p node;
295 SLIST_ENTRY(ng_node) temp;
296 mtx_lock(&ng_nodelist_mtx);
297 node = LIST_FIRST(&ng_freenodes);
298 if (node) {
299 LIST_REMOVE(node, nd_nodes);
300 bcopy(&node->nd_all, &temp, sizeof(temp));
301 bzero(node, sizeof(struct ng_node));
302 bcopy(&temp, &node->nd_all, sizeof(temp));
303 mtx_unlock(&ng_nodelist_mtx);
304 node->nd_magic = ND_MAGIC;
305 } else {
306 mtx_unlock(&ng_nodelist_mtx);
307 _NG_ALLOC_NODE(node);
308 if (node) {
309 node->nd_magic = ND_MAGIC;
310 mtx_lock(&ng_nodelist_mtx);
311 SLIST_INSERT_HEAD(&ng_allnodes, node, nd_all);
312 mtx_unlock(&ng_nodelist_mtx);
313 }
314 }
315 return (node);
316 }
317
318 #define NG_ALLOC_HOOK(hook) do { (hook) = ng_alloc_hook(); } while (0)
319 #define NG_ALLOC_NODE(node) do { (node) = ng_alloc_node(); } while (0)
320
321
322 #define NG_FREE_HOOK(hook) \
323 do { \
324 mtx_lock(&ng_nodelist_mtx); \
325 LIST_INSERT_HEAD(&ng_freehooks, hook, hk_hooks); \
326 hook->hk_magic = 0; \
327 mtx_unlock(&ng_nodelist_mtx); \
328 } while (0)
329
330 #define NG_FREE_NODE(node) \
331 do { \
332 mtx_lock(&ng_nodelist_mtx); \
333 LIST_INSERT_HEAD(&ng_freenodes, node, nd_nodes); \
334 node->nd_magic = 0; \
335 mtx_unlock(&ng_nodelist_mtx); \
336 } while (0)
337
338 #else /* NETGRAPH_DEBUG */ /*----------------------------------------------*/
339
340 #define NG_ALLOC_HOOK(hook) _NG_ALLOC_HOOK(hook)
341 #define NG_ALLOC_NODE(node) _NG_ALLOC_NODE(node)
342
343 #define NG_FREE_HOOK(hook) do { FREE((hook), M_NETGRAPH_HOOK); } while (0)
344 #define NG_FREE_NODE(node) do { FREE((node), M_NETGRAPH_NODE); } while (0)
345
346 #endif /* NETGRAPH_DEBUG */ /*----------------------------------------------*/
347
348 /* Set this to kdb_enter("X") to catch all errors as they occur */
349 #ifndef TRAP_ERROR
350 #define TRAP_ERROR()
351 #endif
352
353 static ng_ID_t nextID = 1;
354
355 #ifdef INVARIANTS
356 #define CHECK_DATA_MBUF(m) do { \
357 struct mbuf *n; \
358 int total; \
359 \
360 M_ASSERTPKTHDR(m); \
361 for (total = 0, n = (m); n != NULL; n = n->m_next) { \
362 total += n->m_len; \
363 if (n->m_nextpkt != NULL) \
364 panic("%s: m_nextpkt", __func__); \
365 } \
366 \
367 if ((m)->m_pkthdr.len != total) { \
368 panic("%s: %d != %d", \
369 __func__, (m)->m_pkthdr.len, total); \
370 } \
371 } while (0)
372 #else
373 #define CHECK_DATA_MBUF(m)
374 #endif
375
376 #define ERROUT(x) do { error = (x); goto done; } while (0)
377
378 /************************************************************************
379 Parse type definitions for generic messages
380 ************************************************************************/
381
382 /* Handy structure parse type defining macro */
383 #define DEFINE_PARSE_STRUCT_TYPE(lo, up, args) \
384 static const struct ng_parse_struct_field \
385 ng_ ## lo ## _type_fields[] = NG_GENERIC_ ## up ## _INFO args; \
386 static const struct ng_parse_type ng_generic_ ## lo ## _type = { \
387 &ng_parse_struct_type, \
388 &ng_ ## lo ## _type_fields \
389 }
390
391 DEFINE_PARSE_STRUCT_TYPE(mkpeer, MKPEER, ());
392 DEFINE_PARSE_STRUCT_TYPE(connect, CONNECT, ());
393 DEFINE_PARSE_STRUCT_TYPE(name, NAME, ());
394 DEFINE_PARSE_STRUCT_TYPE(rmhook, RMHOOK, ());
395 DEFINE_PARSE_STRUCT_TYPE(nodeinfo, NODEINFO, ());
396 DEFINE_PARSE_STRUCT_TYPE(typeinfo, TYPEINFO, ());
397 DEFINE_PARSE_STRUCT_TYPE(linkinfo, LINKINFO, (&ng_generic_nodeinfo_type));
398
399 /* Get length of an array when the length is stored as a 32 bit
400 value immediately preceding the array -- as with struct namelist
401 and struct typelist. */
402 static int
403 ng_generic_list_getLength(const struct ng_parse_type *type,
404 const u_char *start, const u_char *buf)
405 {
406 return *((const u_int32_t *)(buf - 4));
407 }
408
409 /* Get length of the array of struct linkinfo inside a struct hooklist */
410 static int
411 ng_generic_linkinfo_getLength(const struct ng_parse_type *type,
412 const u_char *start, const u_char *buf)
413 {
414 const struct hooklist *hl = (const struct hooklist *)start;
415
416 return hl->nodeinfo.hooks;
417 }
418
419 /* Array type for a variable length array of struct namelist */
420 static const struct ng_parse_array_info ng_nodeinfoarray_type_info = {
421 &ng_generic_nodeinfo_type,
422 &ng_generic_list_getLength
423 };
424 static const struct ng_parse_type ng_generic_nodeinfoarray_type = {
425 &ng_parse_array_type,
426 &ng_nodeinfoarray_type_info
427 };
428
429 /* Array type for a variable length array of struct typelist */
430 static const struct ng_parse_array_info ng_typeinfoarray_type_info = {
431 &ng_generic_typeinfo_type,
432 &ng_generic_list_getLength
433 };
434 static const struct ng_parse_type ng_generic_typeinfoarray_type = {
435 &ng_parse_array_type,
436 &ng_typeinfoarray_type_info
437 };
438
439 /* Array type for array of struct linkinfo in struct hooklist */
440 static const struct ng_parse_array_info ng_generic_linkinfo_array_type_info = {
441 &ng_generic_linkinfo_type,
442 &ng_generic_linkinfo_getLength
443 };
444 static const struct ng_parse_type ng_generic_linkinfo_array_type = {
445 &ng_parse_array_type,
446 &ng_generic_linkinfo_array_type_info
447 };
448
449 DEFINE_PARSE_STRUCT_TYPE(typelist, TYPELIST, (&ng_generic_nodeinfoarray_type));
450 DEFINE_PARSE_STRUCT_TYPE(hooklist, HOOKLIST,
451 (&ng_generic_nodeinfo_type, &ng_generic_linkinfo_array_type));
452 DEFINE_PARSE_STRUCT_TYPE(listnodes, LISTNODES,
453 (&ng_generic_nodeinfoarray_type));
454
455 /* List of commands and how to convert arguments to/from ASCII */
456 static const struct ng_cmdlist ng_generic_cmds[] = {
457 {
458 NGM_GENERIC_COOKIE,
459 NGM_SHUTDOWN,
460 "shutdown",
461 NULL,
462 NULL
463 },
464 {
465 NGM_GENERIC_COOKIE,
466 NGM_MKPEER,
467 "mkpeer",
468 &ng_generic_mkpeer_type,
469 NULL
470 },
471 {
472 NGM_GENERIC_COOKIE,
473 NGM_CONNECT,
474 "connect",
475 &ng_generic_connect_type,
476 NULL
477 },
478 {
479 NGM_GENERIC_COOKIE,
480 NGM_NAME,
481 "name",
482 &ng_generic_name_type,
483 NULL
484 },
485 {
486 NGM_GENERIC_COOKIE,
487 NGM_RMHOOK,
488 "rmhook",
489 &ng_generic_rmhook_type,
490 NULL
491 },
492 {
493 NGM_GENERIC_COOKIE,
494 NGM_NODEINFO,
495 "nodeinfo",
496 NULL,
497 &ng_generic_nodeinfo_type
498 },
499 {
500 NGM_GENERIC_COOKIE,
501 NGM_LISTHOOKS,
502 "listhooks",
503 NULL,
504 &ng_generic_hooklist_type
505 },
506 {
507 NGM_GENERIC_COOKIE,
508 NGM_LISTNAMES,
509 "listnames",
510 NULL,
511 &ng_generic_listnodes_type /* same as NGM_LISTNODES */
512 },
513 {
514 NGM_GENERIC_COOKIE,
515 NGM_LISTNODES,
516 "listnodes",
517 NULL,
518 &ng_generic_listnodes_type
519 },
520 {
521 NGM_GENERIC_COOKIE,
522 NGM_LISTTYPES,
523 "listtypes",
524 NULL,
525 &ng_generic_typeinfo_type
526 },
527 {
528 NGM_GENERIC_COOKIE,
529 NGM_TEXT_CONFIG,
530 "textconfig",
531 NULL,
532 &ng_parse_string_type
533 },
534 {
535 NGM_GENERIC_COOKIE,
536 NGM_TEXT_STATUS,
537 "textstatus",
538 NULL,
539 &ng_parse_string_type
540 },
541 {
542 NGM_GENERIC_COOKIE,
543 NGM_ASCII2BINARY,
544 "ascii2binary",
545 &ng_parse_ng_mesg_type,
546 &ng_parse_ng_mesg_type
547 },
548 {
549 NGM_GENERIC_COOKIE,
550 NGM_BINARY2ASCII,
551 "binary2ascii",
552 &ng_parse_ng_mesg_type,
553 &ng_parse_ng_mesg_type
554 },
555 { 0 }
556 };
557
558 /************************************************************************
559 Node routines
560 ************************************************************************/
561
562 /*
563 * Instantiate a node of the requested type
564 */
565 int
566 ng_make_node(const char *typename, node_p *nodepp)
567 {
568 struct ng_type *type;
569 int error;
570
571 /* Check that the type makes sense */
572 if (typename == NULL) {
573 TRAP_ERROR();
574 return (EINVAL);
575 }
576
577 /* Locate the node type. If we fail we return. Do not try to load
578 * module.
579 */
580 if ((type = ng_findtype(typename)) == NULL)
581 return (ENXIO);
582
583 /*
584 * If we have a constructor, then make the node and
585 * call the constructor to do type specific initialisation.
586 */
587 if (type->constructor != NULL) {
588 if ((error = ng_make_node_common(type, nodepp)) == 0) {
589 if ((error = ((*type->constructor)(*nodepp)) != 0)) {
590 NG_NODE_UNREF(*nodepp);
591 }
592 }
593 } else {
594 /*
595 * Node has no constructor. We cannot ask for one
596 * to be made. It must be brought into existence by
597 * some external agency. The external agency should
598 * call ng_make_node_common() directly to get the
599 * netgraph part initialised.
600 */
601 TRAP_ERROR();
602 error = EINVAL;
603 }
604 return (error);
605 }
606
607 /*
608 * Generic node creation. Called by node initialisation for externally
609 * instantiated nodes (e.g. hardware, sockets, etc ).
610 * The returned node has a reference count of 1.
611 */
612 int
613 ng_make_node_common(struct ng_type *type, node_p *nodepp)
614 {
615 node_p node;
616
617 /* Require the node type to have been already installed */
618 if (ng_findtype(type->name) == NULL) {
619 TRAP_ERROR();
620 return (EINVAL);
621 }
622
623 /* Make a node and try attach it to the type */
624 NG_ALLOC_NODE(node);
625 if (node == NULL) {
626 TRAP_ERROR();
627 return (ENOMEM);
628 }
629 node->nd_type = type;
630 NG_NODE_REF(node); /* note reference */
631 type->refs++;
632
633 NG_QUEUE_LOCK_INIT(&node->nd_input_queue);
634 node->nd_input_queue.queue = NULL;
635 node->nd_input_queue.last = &node->nd_input_queue.queue;
636 node->nd_input_queue.q_flags = 0;
637 node->nd_input_queue.q_node = node;
638
639 /* Initialize hook list for new node */
640 LIST_INIT(&node->nd_hooks);
641
642 /* Link us into the name hash. */
643 mtx_lock(&ng_namehash_mtx);
644 LIST_INSERT_HEAD(&ng_name_hash[0], node, nd_nodes);
645 mtx_unlock(&ng_namehash_mtx);
646
647 /* get an ID and put us in the hash chain */
648 mtx_lock(&ng_idhash_mtx);
649 for (;;) { /* wrap protection, even if silly */
650 node_p node2 = NULL;
651 node->nd_ID = nextID++; /* 137/second for 1 year before wrap */
652
653 /* Is there a problem with the new number? */
654 NG_IDHASH_FIND(node->nd_ID, node2); /* already taken? */
655 if ((node->nd_ID != 0) && (node2 == NULL)) {
656 break;
657 }
658 }
659 LIST_INSERT_HEAD(&ng_ID_hash[NG_IDHASH_FN(node->nd_ID)],
660 node, nd_idnodes);
661 mtx_unlock(&ng_idhash_mtx);
662
663 /* Done */
664 *nodepp = node;
665 return (0);
666 }
667
668 /*
669 * Forceably start the shutdown process on a node. Either call
670 * its shutdown method, or do the default shutdown if there is
671 * no type-specific method.
672 *
673 * We can only be called from a shutdown message, so we know we have
674 * a writer lock, and therefore exclusive access. It also means
675 * that we should not be on the work queue, but we check anyhow.
676 *
677 * Persistent node types must have a type-specific method which
678 * allocates a new node in which case, this one is irretrievably going away,
679 * or cleans up anything it needs, and just makes the node valid again,
680 * in which case we allow the node to survive.
681 *
682 * XXX We need to think of how to tell a persistent node that we
683 * REALLY need to go away because the hardware has gone or we
684 * are rebooting.... etc.
685 */
686 void
687 ng_rmnode(node_p node, hook_p dummy1, void *dummy2, int dummy3)
688 {
689 hook_p hook;
690
691 /* Check if it's already shutting down */
692 if ((node->nd_flags & NGF_CLOSING) != 0)
693 return;
694
695 if (node == &ng_deadnode) {
696 printf ("shutdown called on deadnode\n");
697 return;
698 }
699
700 /* Add an extra reference so it doesn't go away during this */
701 NG_NODE_REF(node);
702
703 /*
704 * Mark it invalid so any newcomers know not to try use it
705 * Also add our own mark so we can't recurse
706 * note that NGF_INVALID does not do this as it's also set during
707 * creation
708 */
709 node->nd_flags |= NGF_INVALID|NGF_CLOSING;
710
711 /* If node has its pre-shutdown method, then call it first*/
712 if (node->nd_type && node->nd_type->close)
713 (*node->nd_type->close)(node);
714
715 /* Notify all remaining connected nodes to disconnect */
716 while ((hook = LIST_FIRST(&node->nd_hooks)) != NULL)
717 ng_destroy_hook(hook);
718
719 /*
720 * Drain the input queue forceably.
721 * it has no hooks so what's it going to do, bleed on someone?
722 * Theoretically we came here from a queue entry that was added
723 * Just before the queue was closed, so it should be empty anyway.
724 * Also removes us from worklist if needed.
725 */
726 ng_flush_input_queue(&node->nd_input_queue);
727
728 /* Ask the type if it has anything to do in this case */
729 if (node->nd_type && node->nd_type->shutdown) {
730 (*node->nd_type->shutdown)(node);
731 if (NG_NODE_IS_VALID(node)) {
732 /*
733 * Well, blow me down if the node code hasn't declared
734 * that it doesn't want to die.
735 * Presumably it is a persistant node.
736 * If we REALLY want it to go away,
737 * e.g. hardware going away,
738 * Our caller should set NGF_REALLY_DIE in nd_flags.
739 */
740 node->nd_flags &= ~(NGF_INVALID|NGF_CLOSING);
741 NG_NODE_UNREF(node); /* Assume they still have theirs */
742 return;
743 }
744 } else { /* do the default thing */
745 NG_NODE_UNREF(node);
746 }
747
748 ng_unname(node); /* basically a NOP these days */
749
750 /*
751 * Remove extra reference, possibly the last
752 * Possible other holders of references may include
753 * timeout callouts, but theoretically the node's supposed to
754 * have cancelled them. Possibly hardware dependencies may
755 * force a driver to 'linger' with a reference.
756 */
757 NG_NODE_UNREF(node);
758 }
759
760 /*
761 * Remove a reference to the node, possibly the last.
762 * deadnode always acts as it it were the last.
763 */
764 int
765 ng_unref_node(node_p node)
766 {
767 int v;
768
769 if (node == &ng_deadnode) {
770 return (0);
771 }
772
773 v = atomic_fetchadd_int(&node->nd_refs, -1);
774
775 if (v == 1) { /* we were the last */
776
777 mtx_lock(&ng_namehash_mtx);
778 node->nd_type->refs--; /* XXX maybe should get types lock? */
779 LIST_REMOVE(node, nd_nodes);
780 mtx_unlock(&ng_namehash_mtx);
781
782 mtx_lock(&ng_idhash_mtx);
783 LIST_REMOVE(node, nd_idnodes);
784 mtx_unlock(&ng_idhash_mtx);
785
786 mtx_destroy(&node->nd_input_queue.q_mtx);
787 NG_FREE_NODE(node);
788 }
789 return (v - 1);
790 }
791
792 /************************************************************************
793 Node ID handling
794 ************************************************************************/
795 static node_p
796 ng_ID2noderef(ng_ID_t ID)
797 {
798 node_p node;
799 mtx_lock(&ng_idhash_mtx);
800 NG_IDHASH_FIND(ID, node);
801 if(node)
802 NG_NODE_REF(node);
803 mtx_unlock(&ng_idhash_mtx);
804 return(node);
805 }
806
807 ng_ID_t
808 ng_node2ID(node_p node)
809 {
810 return (node ? NG_NODE_ID(node) : 0);
811 }
812
813 /************************************************************************
814 Node name handling
815 ************************************************************************/
816
817 /*
818 * Assign a node a name. Once assigned, the name cannot be changed.
819 */
820 int
821 ng_name_node(node_p node, const char *name)
822 {
823 int i, hash;
824 node_p node2;
825
826 /* Check the name is valid */
827 for (i = 0; i < NG_NODESIZ; i++) {
828 if (name[i] == '\0' || name[i] == '.' || name[i] == ':')
829 break;
830 }
831 if (i == 0 || name[i] != '\0') {
832 TRAP_ERROR();
833 return (EINVAL);
834 }
835 if (ng_decodeidname(name) != 0) { /* valid IDs not allowed here */
836 TRAP_ERROR();
837 return (EINVAL);
838 }
839
840 /* Check the name isn't already being used */
841 if ((node2 = ng_name2noderef(node, name)) != NULL) {
842 NG_NODE_UNREF(node2);
843 TRAP_ERROR();
844 return (EADDRINUSE);
845 }
846
847 /* copy it */
848 strlcpy(NG_NODE_NAME(node), name, NG_NODESIZ);
849
850 /* Update name hash. */
851 NG_NAMEHASH(name, hash);
852 mtx_lock(&ng_namehash_mtx);
853 LIST_REMOVE(node, nd_nodes);
854 LIST_INSERT_HEAD(&ng_name_hash[hash], node, nd_nodes);
855 mtx_unlock(&ng_namehash_mtx);
856
857 return (0);
858 }
859
860 /*
861 * Find a node by absolute name. The name should NOT end with ':'
862 * The name "." means "this node" and "[xxx]" means "the node
863 * with ID (ie, at address) xxx".
864 *
865 * Returns the node if found, else NULL.
866 * Eventually should add something faster than a sequential search.
867 * Note it acquires a reference on the node so you can be sure it's still
868 * there.
869 */
870 node_p
871 ng_name2noderef(node_p here, const char *name)
872 {
873 node_p node;
874 ng_ID_t temp;
875 int hash;
876
877 /* "." means "this node" */
878 if (strcmp(name, ".") == 0) {
879 NG_NODE_REF(here);
880 return(here);
881 }
882
883 /* Check for name-by-ID */
884 if ((temp = ng_decodeidname(name)) != 0) {
885 return (ng_ID2noderef(temp));
886 }
887
888 /* Find node by name */
889 NG_NAMEHASH(name, hash);
890 mtx_lock(&ng_namehash_mtx);
891 LIST_FOREACH(node, &ng_name_hash[hash], nd_nodes) {
892 if (NG_NODE_IS_VALID(node) &&
893 (strcmp(NG_NODE_NAME(node), name) == 0)) {
894 break;
895 }
896 }
897 if (node)
898 NG_NODE_REF(node);
899 mtx_unlock(&ng_namehash_mtx);
900 return (node);
901 }
902
903 /*
904 * Decode an ID name, eg. "[f03034de]". Returns 0 if the
905 * string is not valid, otherwise returns the value.
906 */
907 static ng_ID_t
908 ng_decodeidname(const char *name)
909 {
910 const int len = strlen(name);
911 char *eptr;
912 u_long val;
913
914 /* Check for proper length, brackets, no leading junk */
915 if ((len < 3)
916 || (name[0] != '[')
917 || (name[len - 1] != ']')
918 || (!isxdigit(name[1]))) {
919 return ((ng_ID_t)0);
920 }
921
922 /* Decode number */
923 val = strtoul(name + 1, &eptr, 16);
924 if ((eptr - name != len - 1)
925 || (val == ULONG_MAX)
926 || (val == 0)) {
927 return ((ng_ID_t)0);
928 }
929 return (ng_ID_t)val;
930 }
931
932 /*
933 * Remove a name from a node. This should only be called
934 * when shutting down and removing the node.
935 * IF we allow name changing this may be more resurrected.
936 */
937 void
938 ng_unname(node_p node)
939 {
940 }
941
942 /************************************************************************
943 Hook routines
944 Names are not optional. Hooks are always connected, except for a
945 brief moment within these routines. On invalidation or during creation
946 they are connected to the 'dead' hook.
947 ************************************************************************/
948
949 /*
950 * Remove a hook reference
951 */
952 void
953 ng_unref_hook(hook_p hook)
954 {
955 int v;
956
957 if (hook == &ng_deadhook) {
958 return;
959 }
960
961 v = atomic_fetchadd_int(&hook->hk_refs, -1);
962
963 if (v == 1) { /* we were the last */
964 if (_NG_HOOK_NODE(hook)) /* it'll probably be ng_deadnode */
965 _NG_NODE_UNREF((_NG_HOOK_NODE(hook)));
966 NG_FREE_HOOK(hook);
967 }
968 }
969
970 /*
971 * Add an unconnected hook to a node. Only used internally.
972 * Assumes node is locked. (XXX not yet true )
973 */
974 static int
975 ng_add_hook(node_p node, const char *name, hook_p *hookp)
976 {
977 hook_p hook;
978 int error = 0;
979
980 /* Check that the given name is good */
981 if (name == NULL) {
982 TRAP_ERROR();
983 return (EINVAL);
984 }
985 if (ng_findhook(node, name) != NULL) {
986 TRAP_ERROR();
987 return (EEXIST);
988 }
989
990 /* Allocate the hook and link it up */
991 NG_ALLOC_HOOK(hook);
992 if (hook == NULL) {
993 TRAP_ERROR();
994 return (ENOMEM);
995 }
996 hook->hk_refs = 1; /* add a reference for us to return */
997 hook->hk_flags = HK_INVALID;
998 hook->hk_peer = &ng_deadhook; /* start off this way */
999 hook->hk_node = node;
1000 NG_NODE_REF(node); /* each hook counts as a reference */
1001
1002 /* Set hook name */
1003 strlcpy(NG_HOOK_NAME(hook), name, NG_HOOKSIZ);
1004
1005 /*
1006 * Check if the node type code has something to say about it
1007 * If it fails, the unref of the hook will also unref the node.
1008 */
1009 if (node->nd_type->newhook != NULL) {
1010 if ((error = (*node->nd_type->newhook)(node, hook, name))) {
1011 NG_HOOK_UNREF(hook); /* this frees the hook */
1012 return (error);
1013 }
1014 }
1015 /*
1016 * The 'type' agrees so far, so go ahead and link it in.
1017 * We'll ask again later when we actually connect the hooks.
1018 */
1019 LIST_INSERT_HEAD(&node->nd_hooks, hook, hk_hooks);
1020 node->nd_numhooks++;
1021 NG_HOOK_REF(hook); /* one for the node */
1022
1023 if (hookp)
1024 *hookp = hook;
1025 return (0);
1026 }
1027
1028 /*
1029 * Find a hook
1030 *
1031 * Node types may supply their own optimized routines for finding
1032 * hooks. If none is supplied, we just do a linear search.
1033 * XXX Possibly we should add a reference to the hook?
1034 */
1035 hook_p
1036 ng_findhook(node_p node, const char *name)
1037 {
1038 hook_p hook;
1039
1040 if (node->nd_type->findhook != NULL)
1041 return (*node->nd_type->findhook)(node, name);
1042 LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) {
1043 if (NG_HOOK_IS_VALID(hook)
1044 && (strcmp(NG_HOOK_NAME(hook), name) == 0))
1045 return (hook);
1046 }
1047 return (NULL);
1048 }
1049
1050 /*
1051 * Destroy a hook
1052 *
1053 * As hooks are always attached, this really destroys two hooks.
1054 * The one given, and the one attached to it. Disconnect the hooks
1055 * from each other first. We reconnect the peer hook to the 'dead'
1056 * hook so that it can still exist after we depart. We then
1057 * send the peer its own destroy message. This ensures that we only
1058 * interact with the peer's structures when it is locked processing that
1059 * message. We hold a reference to the peer hook so we are guaranteed that
1060 * the peer hook and node are still going to exist until
1061 * we are finished there as the hook holds a ref on the node.
1062 * We run this same code again on the peer hook, but that time it is already
1063 * attached to the 'dead' hook.
1064 *
1065 * This routine is called at all stages of hook creation
1066 * on error detection and must be able to handle any such stage.
1067 */
1068 void
1069 ng_destroy_hook(hook_p hook)
1070 {
1071 hook_p peer;
1072 node_p node;
1073
1074 if (hook == &ng_deadhook) { /* better safe than sorry */
1075 printf("ng_destroy_hook called on deadhook\n");
1076 return;
1077 }
1078
1079 /*
1080 * Protect divorce process with mutex, to avoid races on
1081 * simultaneous disconnect.
1082 */
1083 mtx_lock(&ng_topo_mtx);
1084
1085 hook->hk_flags |= HK_INVALID;
1086
1087 peer = NG_HOOK_PEER(hook);
1088 node = NG_HOOK_NODE(hook);
1089
1090 if (peer && (peer != &ng_deadhook)) {
1091 /*
1092 * Set the peer to point to ng_deadhook
1093 * from this moment on we are effectively independent it.
1094 * send it an rmhook message of it's own.
1095 */
1096 peer->hk_peer = &ng_deadhook; /* They no longer know us */
1097 hook->hk_peer = &ng_deadhook; /* Nor us, them */
1098 if (NG_HOOK_NODE(peer) == &ng_deadnode) {
1099 /*
1100 * If it's already divorced from a node,
1101 * just free it.
1102 */
1103 mtx_unlock(&ng_topo_mtx);
1104 } else {
1105 mtx_unlock(&ng_topo_mtx);
1106 ng_rmhook_self(peer); /* Send it a surprise */
1107 }
1108 NG_HOOK_UNREF(peer); /* account for peer link */
1109 NG_HOOK_UNREF(hook); /* account for peer link */
1110 } else
1111 mtx_unlock(&ng_topo_mtx);
1112
1113 mtx_assert(&ng_topo_mtx, MA_NOTOWNED);
1114
1115 /*
1116 * Remove the hook from the node's list to avoid possible recursion
1117 * in case the disconnection results in node shutdown.
1118 */
1119 if (node == &ng_deadnode) { /* happens if called from ng_con_nodes() */
1120 return;
1121 }
1122 LIST_REMOVE(hook, hk_hooks);
1123 node->nd_numhooks--;
1124 if (node->nd_type->disconnect) {
1125 /*
1126 * The type handler may elect to destroy the node so don't
1127 * trust its existence after this point. (except
1128 * that we still hold a reference on it. (which we
1129 * inherrited from the hook we are destroying)
1130 */
1131 (*node->nd_type->disconnect) (hook);
1132 }
1133
1134 /*
1135 * Note that because we will point to ng_deadnode, the original node
1136 * is not decremented automatically so we do that manually.
1137 */
1138 _NG_HOOK_NODE(hook) = &ng_deadnode;
1139 NG_NODE_UNREF(node); /* We no longer point to it so adjust count */
1140 NG_HOOK_UNREF(hook); /* Account for linkage (in list) to node */
1141 }
1142
1143 /*
1144 * Take two hooks on a node and merge the connection so that the given node
1145 * is effectively bypassed.
1146 */
1147 int
1148 ng_bypass(hook_p hook1, hook_p hook2)
1149 {
1150 if (hook1->hk_node != hook2->hk_node) {
1151 TRAP_ERROR();
1152 return (EINVAL);
1153 }
1154 hook1->hk_peer->hk_peer = hook2->hk_peer;
1155 hook2->hk_peer->hk_peer = hook1->hk_peer;
1156
1157 hook1->hk_peer = &ng_deadhook;
1158 hook2->hk_peer = &ng_deadhook;
1159
1160 NG_HOOK_UNREF(hook1);
1161 NG_HOOK_UNREF(hook2);
1162
1163 /* XXX If we ever cache methods on hooks update them as well */
1164 ng_destroy_hook(hook1);
1165 ng_destroy_hook(hook2);
1166 return (0);
1167 }
1168
1169 /*
1170 * Install a new netgraph type
1171 */
1172 int
1173 ng_newtype(struct ng_type *tp)
1174 {
1175 const size_t namelen = strlen(tp->name);
1176
1177 /* Check version and type name fields */
1178 if ((tp->version != NG_ABI_VERSION)
1179 || (namelen == 0)
1180 || (namelen >= NG_TYPESIZ)) {
1181 TRAP_ERROR();
1182 if (tp->version != NG_ABI_VERSION) {
1183 printf("Netgraph: Node type rejected. ABI mismatch. Suggest recompile\n");
1184 }
1185 return (EINVAL);
1186 }
1187
1188 /* Check for name collision */
1189 if (ng_findtype(tp->name) != NULL) {
1190 TRAP_ERROR();
1191 return (EEXIST);
1192 }
1193
1194
1195 /* Link in new type */
1196 mtx_lock(&ng_typelist_mtx);
1197 LIST_INSERT_HEAD(&ng_typelist, tp, types);
1198 tp->refs = 1; /* first ref is linked list */
1199 mtx_unlock(&ng_typelist_mtx);
1200 return (0);
1201 }
1202
1203 /*
1204 * unlink a netgraph type
1205 * If no examples exist
1206 */
1207 int
1208 ng_rmtype(struct ng_type *tp)
1209 {
1210 /* Check for name collision */
1211 if (tp->refs != 1) {
1212 TRAP_ERROR();
1213 return (EBUSY);
1214 }
1215
1216 /* Unlink type */
1217 mtx_lock(&ng_typelist_mtx);
1218 LIST_REMOVE(tp, types);
1219 mtx_unlock(&ng_typelist_mtx);
1220 return (0);
1221 }
1222
1223 /*
1224 * Look for a type of the name given
1225 */
1226 struct ng_type *
1227 ng_findtype(const char *typename)
1228 {
1229 struct ng_type *type;
1230
1231 mtx_lock(&ng_typelist_mtx);
1232 LIST_FOREACH(type, &ng_typelist, types) {
1233 if (strcmp(type->name, typename) == 0)
1234 break;
1235 }
1236 mtx_unlock(&ng_typelist_mtx);
1237 return (type);
1238 }
1239
1240 /************************************************************************
1241 Composite routines
1242 ************************************************************************/
1243 /*
1244 * Connect two nodes using the specified hooks, using queued functions.
1245 */
1246 static int
1247 ng_con_part3(node_p node, item_p item, hook_p hook)
1248 {
1249 int error = 0;
1250
1251 /*
1252 * When we run, we know that the node 'node' is locked for us.
1253 * Our caller has a reference on the hook.
1254 * Our caller has a reference on the node.
1255 * (In this case our caller is ng_apply_item() ).
1256 * The peer hook has a reference on the hook.
1257 * We are all set up except for the final call to the node, and
1258 * the clearing of the INVALID flag.
1259 */
1260 if (NG_HOOK_NODE(hook) == &ng_deadnode) {
1261 /*
1262 * The node must have been freed again since we last visited
1263 * here. ng_destry_hook() has this effect but nothing else does.
1264 * We should just release our references and
1265 * free anything we can think of.
1266 * Since we know it's been destroyed, and it's our caller
1267 * that holds the references, just return.
1268 */
1269 ERROUT(ENOENT);
1270 }
1271 if (hook->hk_node->nd_type->connect) {
1272 if ((error = (*hook->hk_node->nd_type->connect) (hook))) {
1273 ng_destroy_hook(hook); /* also zaps peer */
1274 printf("failed in ng_con_part3()\n");
1275 ERROUT(error);
1276 }
1277 }
1278 /*
1279 * XXX this is wrong for SMP. Possibly we need
1280 * to separate out 'create' and 'invalid' flags.
1281 * should only set flags on hooks we have locked under our node.
1282 */
1283 hook->hk_flags &= ~HK_INVALID;
1284 done:
1285 NG_FREE_ITEM(item);
1286 return (error);
1287 }
1288
1289 static int
1290 ng_con_part2(node_p node, item_p item, hook_p hook)
1291 {
1292 hook_p peer;
1293 int error = 0;
1294
1295 /*
1296 * When we run, we know that the node 'node' is locked for us.
1297 * Our caller has a reference on the hook.
1298 * Our caller has a reference on the node.
1299 * (In this case our caller is ng_apply_item() ).
1300 * The peer hook has a reference on the hook.
1301 * our node pointer points to the 'dead' node.
1302 * First check the hook name is unique.
1303 * Should not happen because we checked before queueing this.
1304 */
1305 if (ng_findhook(node, NG_HOOK_NAME(hook)) != NULL) {
1306 TRAP_ERROR();
1307 ng_destroy_hook(hook); /* should destroy peer too */
1308 printf("failed in ng_con_part2()\n");
1309 ERROUT(EEXIST);
1310 }
1311 /*
1312 * Check if the node type code has something to say about it
1313 * If it fails, the unref of the hook will also unref the attached node,
1314 * however since that node is 'ng_deadnode' this will do nothing.
1315 * The peer hook will also be destroyed.
1316 */
1317 if (node->nd_type->newhook != NULL) {
1318 if ((error = (*node->nd_type->newhook)(node, hook,
1319 hook->hk_name))) {
1320 ng_destroy_hook(hook); /* should destroy peer too */
1321 printf("failed in ng_con_part2()\n");
1322 ERROUT(error);
1323 }
1324 }
1325
1326 /*
1327 * The 'type' agrees so far, so go ahead and link it in.
1328 * We'll ask again later when we actually connect the hooks.
1329 */
1330 hook->hk_node = node; /* just overwrite ng_deadnode */
1331 NG_NODE_REF(node); /* each hook counts as a reference */
1332 LIST_INSERT_HEAD(&node->nd_hooks, hook, hk_hooks);
1333 node->nd_numhooks++;
1334 NG_HOOK_REF(hook); /* one for the node */
1335
1336 /*
1337 * We now have a symmetrical situation, where both hooks have been
1338 * linked to their nodes, the newhook methods have been called
1339 * And the references are all correct. The hooks are still marked
1340 * as invalid, as we have not called the 'connect' methods
1341 * yet.
1342 * We can call the local one immediately as we have the
1343 * node locked, but we need to queue the remote one.
1344 */
1345 if (hook->hk_node->nd_type->connect) {
1346 if ((error = (*hook->hk_node->nd_type->connect) (hook))) {
1347 ng_destroy_hook(hook); /* also zaps peer */
1348 printf("failed in ng_con_part2(A)\n");
1349 ERROUT(error);
1350 }
1351 }
1352
1353 /*
1354 * Acquire topo mutex to avoid race with ng_destroy_hook().
1355 */
1356 mtx_lock(&ng_topo_mtx);
1357 peer = hook->hk_peer;
1358 if (peer == &ng_deadhook) {
1359 mtx_unlock(&ng_topo_mtx);
1360 printf("failed in ng_con_part2(B)\n");
1361 ng_destroy_hook(hook);
1362 ERROUT(ENOENT);
1363 }
1364 mtx_unlock(&ng_topo_mtx);
1365
1366 if ((error = ng_send_fn2(peer->hk_node, peer, item, &ng_con_part3,
1367 NULL, 0, NG_REUSE_ITEM))) {
1368 printf("failed in ng_con_part2(C)\n");
1369 ng_destroy_hook(hook); /* also zaps peer */
1370 return (error); /* item was consumed. */
1371 }
1372 hook->hk_flags &= ~HK_INVALID; /* need both to be able to work */
1373 return (0); /* item was consumed. */
1374 done:
1375 NG_FREE_ITEM(item);
1376 return (error);
1377 }
1378
1379 /*
1380 * Connect this node with another node. We assume that this node is
1381 * currently locked, as we are only called from an NGM_CONNECT message.
1382 */
1383 static int
1384 ng_con_nodes(item_p item, node_p node, const char *name,
1385 node_p node2, const char *name2)
1386 {
1387 int error;
1388 hook_p hook;
1389 hook_p hook2;
1390
1391 if (ng_findhook(node2, name2) != NULL) {
1392 return(EEXIST);
1393 }
1394 if ((error = ng_add_hook(node, name, &hook))) /* gives us a ref */
1395 return (error);
1396 /* Allocate the other hook and link it up */
1397 NG_ALLOC_HOOK(hook2);
1398 if (hook2 == NULL) {
1399 TRAP_ERROR();
1400 ng_destroy_hook(hook); /* XXX check ref counts so far */
1401 NG_HOOK_UNREF(hook); /* including our ref */
1402 return (ENOMEM);
1403 }
1404 hook2->hk_refs = 1; /* start with a reference for us. */
1405 hook2->hk_flags = HK_INVALID;
1406 hook2->hk_peer = hook; /* Link the two together */
1407 hook->hk_peer = hook2;
1408 NG_HOOK_REF(hook); /* Add a ref for the peer to each*/
1409 NG_HOOK_REF(hook2);
1410 hook2->hk_node = &ng_deadnode;
1411 strlcpy(NG_HOOK_NAME(hook2), name2, NG_HOOKSIZ);
1412
1413 /*
1414 * Queue the function above.
1415 * Procesing continues in that function in the lock context of
1416 * the other node.
1417 */
1418 if ((error = ng_send_fn2(node2, hook2, item, &ng_con_part2, NULL, 0,
1419 NG_NOFLAGS))) {
1420 printf("failed in ng_con_nodes(): %d\n", error);
1421 ng_destroy_hook(hook); /* also zaps peer */
1422 }
1423
1424 NG_HOOK_UNREF(hook); /* Let each hook go if it wants to */
1425 NG_HOOK_UNREF(hook2);
1426 return (error);
1427 }
1428
1429 /*
1430 * Make a peer and connect.
1431 * We assume that the local node is locked.
1432 * The new node probably doesn't need a lock until
1433 * it has a hook, because it cannot really have any work until then,
1434 * but we should think about it a bit more.
1435 *
1436 * The problem may come if the other node also fires up
1437 * some hardware or a timer or some other source of activation,
1438 * also it may already get a command msg via it's ID.
1439 *
1440 * We could use the same method as ng_con_nodes() but we'd have
1441 * to add ability to remove the node when failing. (Not hard, just
1442 * make arg1 point to the node to remove).
1443 * Unless of course we just ignore failure to connect and leave
1444 * an unconnected node?
1445 */
1446 static int
1447 ng_mkpeer(node_p node, const char *name, const char *name2, char *type)
1448 {
1449 node_p node2;
1450 hook_p hook1, hook2;
1451 int error;
1452
1453 if ((error = ng_make_node(type, &node2))) {
1454 return (error);
1455 }
1456
1457 if ((error = ng_add_hook(node, name, &hook1))) { /* gives us a ref */
1458 ng_rmnode(node2, NULL, NULL, 0);
1459 return (error);
1460 }
1461
1462 if ((error = ng_add_hook(node2, name2, &hook2))) {
1463 ng_rmnode(node2, NULL, NULL, 0);
1464 ng_destroy_hook(hook1);
1465 NG_HOOK_UNREF(hook1);
1466 return (error);
1467 }
1468
1469 /*
1470 * Actually link the two hooks together.
1471 */
1472 hook1->hk_peer = hook2;
1473 hook2->hk_peer = hook1;
1474
1475 /* Each hook is referenced by the other */
1476 NG_HOOK_REF(hook1);
1477 NG_HOOK_REF(hook2);
1478
1479 /* Give each node the opportunity to veto the pending connection */
1480 if (hook1->hk_node->nd_type->connect) {
1481 error = (*hook1->hk_node->nd_type->connect) (hook1);
1482 }
1483
1484 if ((error == 0) && hook2->hk_node->nd_type->connect) {
1485 error = (*hook2->hk_node->nd_type->connect) (hook2);
1486
1487 }
1488
1489 /*
1490 * drop the references we were holding on the two hooks.
1491 */
1492 if (error) {
1493 ng_destroy_hook(hook2); /* also zaps hook1 */
1494 ng_rmnode(node2, NULL, NULL, 0);
1495 } else {
1496 /* As a last act, allow the hooks to be used */
1497 hook1->hk_flags &= ~HK_INVALID;
1498 hook2->hk_flags &= ~HK_INVALID;
1499 }
1500 NG_HOOK_UNREF(hook1);
1501 NG_HOOK_UNREF(hook2);
1502 return (error);
1503 }
1504
1505 /************************************************************************
1506 Utility routines to send self messages
1507 ************************************************************************/
1508
1509 /* Shut this node down as soon as everyone is clear of it */
1510 /* Should add arg "immediately" to jump the queue */
1511 int
1512 ng_rmnode_self(node_p node)
1513 {
1514 int error;
1515
1516 if (node == &ng_deadnode)
1517 return (0);
1518 node->nd_flags |= NGF_INVALID;
1519 if (node->nd_flags & NGF_CLOSING)
1520 return (0);
1521
1522 error = ng_send_fn(node, NULL, &ng_rmnode, NULL, 0);
1523 return (error);
1524 }
1525
1526 static void
1527 ng_rmhook_part2(node_p node, hook_p hook, void *arg1, int arg2)
1528 {
1529 ng_destroy_hook(hook);
1530 return ;
1531 }
1532
1533 int
1534 ng_rmhook_self(hook_p hook)
1535 {
1536 int error;
1537 node_p node = NG_HOOK_NODE(hook);
1538
1539 if (node == &ng_deadnode)
1540 return (0);
1541
1542 error = ng_send_fn(node, hook, &ng_rmhook_part2, NULL, 0);
1543 return (error);
1544 }
1545
1546 /***********************************************************************
1547 * Parse and verify a string of the form: <NODE:><PATH>
1548 *
1549 * Such a string can refer to a specific node or a specific hook
1550 * on a specific node, depending on how you look at it. In the
1551 * latter case, the PATH component must not end in a dot.
1552 *
1553 * Both <NODE:> and <PATH> are optional. The <PATH> is a string
1554 * of hook names separated by dots. This breaks out the original
1555 * string, setting *nodep to "NODE" (or NULL if none) and *pathp
1556 * to "PATH" (or NULL if degenerate). Also, *hookp will point to
1557 * the final hook component of <PATH>, if any, otherwise NULL.
1558 *
1559 * This returns -1 if the path is malformed. The char ** are optional.
1560 ***********************************************************************/
1561 int
1562 ng_path_parse(char *addr, char **nodep, char **pathp, char **hookp)
1563 {
1564 char *node, *path, *hook;
1565 int k;
1566
1567 /*
1568 * Extract absolute NODE, if any
1569 */
1570 for (path = addr; *path && *path != ':'; path++);
1571 if (*path) {
1572 node = addr; /* Here's the NODE */
1573 *path++ = '\0'; /* Here's the PATH */
1574
1575 /* Node name must not be empty */
1576 if (!*node)
1577 return -1;
1578
1579 /* A name of "." is OK; otherwise '.' not allowed */
1580 if (strcmp(node, ".") != 0) {
1581 for (k = 0; node[k]; k++)
1582 if (node[k] == '.')
1583 return -1;
1584 }
1585 } else {
1586 node = NULL; /* No absolute NODE */
1587 path = addr; /* Here's the PATH */
1588 }
1589
1590 /* Snoop for illegal characters in PATH */
1591 for (k = 0; path[k]; k++)
1592 if (path[k] == ':')
1593 return -1;
1594
1595 /* Check for no repeated dots in PATH */
1596 for (k = 0; path[k]; k++)
1597 if (path[k] == '.' && path[k + 1] == '.')
1598 return -1;
1599
1600 /* Remove extra (degenerate) dots from beginning or end of PATH */
1601 if (path[0] == '.')
1602 path++;
1603 if (*path && path[strlen(path) - 1] == '.')
1604 path[strlen(path) - 1] = 0;
1605
1606 /* If PATH has a dot, then we're not talking about a hook */
1607 if (*path) {
1608 for (hook = path, k = 0; path[k]; k++)
1609 if (path[k] == '.') {
1610 hook = NULL;
1611 break;
1612 }
1613 } else
1614 path = hook = NULL;
1615
1616 /* Done */
1617 if (nodep)
1618 *nodep = node;
1619 if (pathp)
1620 *pathp = path;
1621 if (hookp)
1622 *hookp = hook;
1623 return (0);
1624 }
1625
1626 /*
1627 * Given a path, which may be absolute or relative, and a starting node,
1628 * return the destination node.
1629 */
1630 int
1631 ng_path2noderef(node_p here, const char *address,
1632 node_p *destp, hook_p *lasthook)
1633 {
1634 char fullpath[NG_PATHSIZ];
1635 char *nodename, *path, pbuf[2];
1636 node_p node, oldnode;
1637 char *cp;
1638 hook_p hook = NULL;
1639
1640 /* Initialize */
1641 if (destp == NULL) {
1642 TRAP_ERROR();
1643 return EINVAL;
1644 }
1645 *destp = NULL;
1646
1647 /* Make a writable copy of address for ng_path_parse() */
1648 strncpy(fullpath, address, sizeof(fullpath) - 1);
1649 fullpath[sizeof(fullpath) - 1] = '\0';
1650
1651 /* Parse out node and sequence of hooks */
1652 if (ng_path_parse(fullpath, &nodename, &path, NULL) < 0) {
1653 TRAP_ERROR();
1654 return EINVAL;
1655 }
1656 if (path == NULL) {
1657 pbuf[0] = '.'; /* Needs to be writable */
1658 pbuf[1] = '\0';
1659 path = pbuf;
1660 }
1661
1662 /*
1663 * For an absolute address, jump to the starting node.
1664 * Note that this holds a reference on the node for us.
1665 * Don't forget to drop the reference if we don't need it.
1666 */
1667 if (nodename) {
1668 node = ng_name2noderef(here, nodename);
1669 if (node == NULL) {
1670 TRAP_ERROR();
1671 return (ENOENT);
1672 }
1673 } else {
1674 if (here == NULL) {
1675 TRAP_ERROR();
1676 return (EINVAL);
1677 }
1678 node = here;
1679 NG_NODE_REF(node);
1680 }
1681
1682 /*
1683 * Now follow the sequence of hooks
1684 * XXX
1685 * We actually cannot guarantee that the sequence
1686 * is not being demolished as we crawl along it
1687 * without extra-ordinary locking etc.
1688 * So this is a bit dodgy to say the least.
1689 * We can probably hold up some things by holding
1690 * the nodelist mutex for the time of this
1691 * crawl if we wanted.. At least that way we wouldn't have to
1692 * worry about the nodes disappearing, but the hooks would still
1693 * be a problem.
1694 */
1695 for (cp = path; node != NULL && *cp != '\0'; ) {
1696 char *segment;
1697
1698 /*
1699 * Break out the next path segment. Replace the dot we just
1700 * found with a NUL; "cp" points to the next segment (or the
1701 * NUL at the end).
1702 */
1703 for (segment = cp; *cp != '\0'; cp++) {
1704 if (*cp == '.') {
1705 *cp++ = '\0';
1706 break;
1707 }
1708 }
1709
1710 /* Empty segment */
1711 if (*segment == '\0')
1712 continue;
1713
1714 /* We have a segment, so look for a hook by that name */
1715 hook = ng_findhook(node, segment);
1716
1717 /* Can't get there from here... */
1718 if (hook == NULL
1719 || NG_HOOK_PEER(hook) == NULL
1720 || NG_HOOK_NOT_VALID(hook)
1721 || NG_HOOK_NOT_VALID(NG_HOOK_PEER(hook))) {
1722 TRAP_ERROR();
1723 NG_NODE_UNREF(node);
1724 #if 0
1725 printf("hooknotvalid %s %s %d %d %d %d ",
1726 path,
1727 segment,
1728 hook == NULL,
1729 NG_HOOK_PEER(hook) == NULL,
1730 NG_HOOK_NOT_VALID(hook),
1731 NG_HOOK_NOT_VALID(NG_HOOK_PEER(hook)));
1732 #endif
1733 return (ENOENT);
1734 }
1735
1736 /*
1737 * Hop on over to the next node
1738 * XXX
1739 * Big race conditions here as hooks and nodes go away
1740 * *** Idea.. store an ng_ID_t in each hook and use that
1741 * instead of the direct hook in this crawl?
1742 */
1743 oldnode = node;
1744 if ((node = NG_PEER_NODE(hook)))
1745 NG_NODE_REF(node); /* XXX RACE */
1746 NG_NODE_UNREF(oldnode); /* XXX another race */
1747 if (NG_NODE_NOT_VALID(node)) {
1748 NG_NODE_UNREF(node); /* XXX more races */
1749 node = NULL;
1750 }
1751 }
1752
1753 /* If node somehow missing, fail here (probably this is not needed) */
1754 if (node == NULL) {
1755 TRAP_ERROR();
1756 return (ENXIO);
1757 }
1758
1759 /* Done */
1760 *destp = node;
1761 if (lasthook != NULL)
1762 *lasthook = (hook ? NG_HOOK_PEER(hook) : NULL);
1763 return (0);
1764 }
1765
1766 /***************************************************************\
1767 * Input queue handling.
1768 * All activities are submitted to the node via the input queue
1769 * which implements a multiple-reader/single-writer gate.
1770 * Items which cannot be handled immediately are queued.
1771 *
1772 * read-write queue locking inline functions *
1773 \***************************************************************/
1774
1775 static __inline item_p ng_dequeue(struct ng_queue * ngq, int *rw);
1776 static __inline item_p ng_acquire_read(struct ng_queue * ngq,
1777 item_p item);
1778 static __inline item_p ng_acquire_write(struct ng_queue * ngq,
1779 item_p item);
1780 static __inline void ng_leave_read(struct ng_queue * ngq);
1781 static __inline void ng_leave_write(struct ng_queue * ngq);
1782 static __inline void ng_queue_rw(struct ng_queue * ngq,
1783 item_p item, int rw);
1784
1785 /*
1786 * Definition of the bits fields in the ng_queue flag word.
1787 * Defined here rather than in netgraph.h because no-one should fiddle
1788 * with them.
1789 *
1790 * The ordering here may be important! don't shuffle these.
1791 */
1792 /*-
1793 Safety Barrier--------+ (adjustable to suit taste) (not used yet)
1794 |
1795 V
1796 +-------+-------+-------+-------+-------+-------+-------+-------+
1797 | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
1798 | |A|c|t|i|v|e| |R|e|a|d|e|r| |C|o|u|n|t| | | | | | | | | |P|A|
1799 | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |O|W|
1800 +-------+-------+-------+-------+-------+-------+-------+-------+
1801 \___________________________ ____________________________/ | |
1802 V | |
1803 [active reader count] | |
1804 | |
1805 Operation Pending -------------------------------+ |
1806 |
1807 Active Writer ---------------------------------------+
1808
1809
1810 */
1811 #define WRITER_ACTIVE 0x00000001
1812 #define OP_PENDING 0x00000002
1813 #define READER_INCREMENT 0x00000004
1814 #define READER_MASK 0xfffffffc /* Not valid if WRITER_ACTIVE is set */
1815 #define SAFETY_BARRIER 0x00100000 /* 128K items queued should be enough */
1816
1817 /* Defines of more elaborate states on the queue */
1818 /* Mask of bits a new read cares about */
1819 #define NGQ_RMASK (WRITER_ACTIVE|OP_PENDING)
1820
1821 /* Mask of bits a new write cares about */
1822 #define NGQ_WMASK (NGQ_RMASK|READER_MASK)
1823
1824 /* Test to decide if there is something on the queue. */
1825 #define QUEUE_ACTIVE(QP) ((QP)->q_flags & OP_PENDING)
1826
1827 /* How to decide what the next queued item is. */
1828 #define HEAD_IS_READER(QP) NGI_QUEUED_READER((QP)->queue)
1829 #define HEAD_IS_WRITER(QP) NGI_QUEUED_WRITER((QP)->queue) /* notused */
1830
1831 /* Read the status to decide if the next item on the queue can now run. */
1832 #define QUEUED_READER_CAN_PROCEED(QP) \
1833 (((QP)->q_flags & (NGQ_RMASK & ~OP_PENDING)) == 0)
1834 #define QUEUED_WRITER_CAN_PROCEED(QP) \
1835 (((QP)->q_flags & (NGQ_WMASK & ~OP_PENDING)) == 0)
1836
1837 /* Is there a chance of getting ANY work off the queue? */
1838 #define NEXT_QUEUED_ITEM_CAN_PROCEED(QP) \
1839 (QUEUE_ACTIVE(QP) && \
1840 ((HEAD_IS_READER(QP)) ? QUEUED_READER_CAN_PROCEED(QP) : \
1841 QUEUED_WRITER_CAN_PROCEED(QP)))
1842
1843
1844 #define NGQRW_R 0
1845 #define NGQRW_W 1
1846
1847 /*
1848 * Taking into account the current state of the queue and node, possibly take
1849 * the next entry off the queue and return it. Return NULL if there was
1850 * nothing we could return, either because there really was nothing there, or
1851 * because the node was in a state where it cannot yet process the next item
1852 * on the queue.
1853 *
1854 * This MUST MUST MUST be called with the mutex held.
1855 */
1856 static __inline item_p
1857 ng_dequeue(struct ng_queue *ngq, int *rw)
1858 {
1859 item_p item;
1860 u_int add_arg;
1861
1862 mtx_assert(&ngq->q_mtx, MA_OWNED);
1863 /*
1864 * If there is nothing queued, then just return.
1865 * No point in continuing.
1866 * XXXGL: assert this?
1867 */
1868 if (!QUEUE_ACTIVE(ngq)) {
1869 CTR4(KTR_NET, "%20s: node [%x] (%p) queue empty; "
1870 "queue flags 0x%lx", __func__,
1871 ngq->q_node->nd_ID, ngq->q_node, ngq->q_flags);
1872 return (NULL);
1873 }
1874
1875 /*
1876 * From here, we can assume there is a head item.
1877 * We need to find out what it is and if it can be dequeued, given
1878 * the current state of the node.
1879 */
1880 if (HEAD_IS_READER(ngq)) {
1881 if (!QUEUED_READER_CAN_PROCEED(ngq)) {
1882 /*
1883 * It's a reader but we can't use it.
1884 * We are stalled so make sure we don't
1885 * get called again until something changes.
1886 */
1887 ng_worklist_remove(ngq->q_node);
1888 CTR4(KTR_NET, "%20s: node [%x] (%p) queued reader "
1889 "can't proceed; queue flags 0x%lx", __func__,
1890 ngq->q_node->nd_ID, ngq->q_node, ngq->q_flags);
1891 return (NULL);
1892 }
1893 /*
1894 * Head of queue is a reader and we have no write active.
1895 * We don't care how many readers are already active.
1896 * Add the correct increment for the reader count.
1897 */
1898 add_arg = READER_INCREMENT;
1899 *rw = NGQRW_R;
1900 } else if (QUEUED_WRITER_CAN_PROCEED(ngq)) {
1901 /*
1902 * There is a pending write, no readers and no active writer.
1903 * This means we can go ahead with the pending writer. Note
1904 * the fact that we now have a writer, ready for when we take
1905 * it off the queue.
1906 *
1907 * We don't need to worry about a possible collision with the
1908 * fasttrack reader.
1909 *
1910 * The fasttrack thread may take a long time to discover that we
1911 * are running so we would have an inconsistent state in the
1912 * flags for a while. Since we ignore the reader count
1913 * entirely when the WRITER_ACTIVE flag is set, this should
1914 * not matter (in fact it is defined that way). If it tests
1915 * the flag before this operation, the OP_PENDING flag
1916 * will make it fail, and if it tests it later, the
1917 * WRITER_ACTIVE flag will do the same. If it is SO slow that
1918 * we have actually completed the operation, and neither flag
1919 * is set by the time that it tests the flags, then it is
1920 * actually ok for it to continue. If it completes and we've
1921 * finished and the read pending is set it still fails.
1922 *
1923 * So we can just ignore it, as long as we can ensure that the
1924 * transition from WRITE_PENDING state to the WRITER_ACTIVE
1925 * state is atomic.
1926 *
1927 * After failing, first it will be held back by the mutex, then
1928 * when it can proceed, it will queue its request, then it
1929 * would arrive at this function. Usually it will have to
1930 * leave empty handed because the ACTIVE WRITER bit will be
1931 * set.
1932 *
1933 * Adjust the flags for the new active writer.
1934 */
1935 add_arg = WRITER_ACTIVE;
1936 *rw = NGQRW_W;
1937 /*
1938 * We want to write "active writer, no readers " Now go make
1939 * it true. In fact there may be a number in the readers
1940 * count but we know it is not true and will be fixed soon.
1941 * We will fix the flags for the next pending entry in a
1942 * moment.
1943 */
1944 } else {
1945 /*
1946 * We can't dequeue anything.. return and say so. Probably we
1947 * have a write pending and the readers count is non zero. If
1948 * we got here because a reader hit us just at the wrong
1949 * moment with the fasttrack code, and put us in a strange
1950 * state, then it will be coming through in just a moment,
1951 * (just as soon as we release the mutex) and keep things
1952 * moving.
1953 * Make sure we remove ourselves from the work queue. It
1954 * would be a waste of effort to do all this again.
1955 */
1956 ng_worklist_remove(ngq->q_node);
1957 CTR4(KTR_NET, "%20s: node [%x] (%p) can't dequeue anything; "
1958 "queue flags 0x%lx", __func__,
1959 ngq->q_node->nd_ID, ngq->q_node, ngq->q_flags);
1960 return (NULL);
1961 }
1962
1963 /*
1964 * Now we dequeue the request (whatever it may be) and correct the
1965 * pending flags and the next and last pointers.
1966 */
1967 item = ngq->queue;
1968 ngq->queue = item->el_next;
1969 CTR6(KTR_NET, "%20s: node [%x] (%p) dequeued item %p with flags 0x%lx; "
1970 "queue flags 0x%lx", __func__,
1971 ngq->q_node->nd_ID,ngq->q_node, item, item->el_flags, ngq->q_flags);
1972 if (ngq->last == &(item->el_next)) {
1973 /*
1974 * that was the last entry in the queue so set the 'last
1975 * pointer up correctly and make sure the pending flag is
1976 * clear.
1977 */
1978 add_arg += -OP_PENDING;
1979 ngq->last = &(ngq->queue);
1980 /*
1981 * Whatever flag was set will be cleared and
1982 * the new acive field will be set by the add as well,
1983 * so we don't need to change add_arg.
1984 * But we know we don't need to be on the work list.
1985 */
1986 atomic_add_long(&ngq->q_flags, add_arg);
1987 ng_worklist_remove(ngq->q_node);
1988 } else {
1989 /*
1990 * Since there is still something on the queue
1991 * we don't need to change the PENDING flag.
1992 */
1993 atomic_add_long(&ngq->q_flags, add_arg);
1994 }
1995 CTR6(KTR_NET, "%20s: node [%x] (%p) returning item %p as %s; "
1996 "queue flags 0x%lx", __func__,
1997 ngq->q_node->nd_ID, ngq->q_node, item, *rw ? "WRITER" : "READER" ,
1998 ngq->q_flags);
1999 return (item);
2000 }
2001
2002 /*
2003 * Queue a packet to be picked up by someone else.
2004 * We really don't care who, but we can't or don't want to hang around
2005 * to process it ourselves. We are probably an interrupt routine..
2006 * If the queue could be run, flag the netisr handler to start.
2007 */
2008 static __inline void
2009 ng_queue_rw(struct ng_queue * ngq, item_p item, int rw)
2010 {
2011 mtx_assert(&ngq->q_mtx, MA_OWNED);
2012
2013 if (rw == NGQRW_W)
2014 NGI_SET_WRITER(item);
2015 else
2016 NGI_SET_READER(item);
2017 item->el_next = NULL; /* maybe not needed */
2018 *ngq->last = item;
2019 CTR5(KTR_NET, "%20s: node [%x] (%p) queued item %p as %s", __func__,
2020 ngq->q_node->nd_ID, ngq->q_node, item, rw ? "WRITER" : "READER" );
2021 /*
2022 * If it was the first item in the queue then we need to
2023 * set the last pointer and the type flags.
2024 */
2025 if (ngq->last == &(ngq->queue)) {
2026 atomic_add_long(&ngq->q_flags, OP_PENDING);
2027 CTR3(KTR_NET, "%20s: node [%x] (%p) set OP_PENDING", __func__,
2028 ngq->q_node->nd_ID, ngq->q_node);
2029 }
2030
2031 ngq->last = &(item->el_next);
2032 /*
2033 * We can take the worklist lock with the node locked
2034 * BUT NOT THE REVERSE!
2035 */
2036 if (NEXT_QUEUED_ITEM_CAN_PROCEED(ngq))
2037 ng_setisr(ngq->q_node);
2038 }
2039
2040
2041 /*
2042 * This function 'cheats' in that it first tries to 'grab' the use of the
2043 * node, without going through the mutex. We can do this becasue of the
2044 * semantics of the lock. The semantics include a clause that says that the
2045 * value of the readers count is invalid if the WRITER_ACTIVE flag is set. It
2046 * also says that the WRITER_ACTIVE flag cannot be set if the readers count
2047 * is not zero. Note that this talks about what is valid to SET the
2048 * WRITER_ACTIVE flag, because from the moment it is set, the value if the
2049 * reader count is immaterial, and not valid. The two 'pending' flags have a
2050 * similar effect, in that If they are orthogonal to the two active fields in
2051 * how they are set, but if either is set, the attempted 'grab' need to be
2052 * backed out because there is earlier work, and we maintain ordering in the
2053 * queue. The result of this is that the reader request can try obtain use of
2054 * the node with only a single atomic addition, and without any of the mutex
2055 * overhead. If this fails the operation degenerates to the same as for other
2056 * cases.
2057 *
2058 */
2059 static __inline item_p
2060 ng_acquire_read(struct ng_queue *ngq, item_p item)
2061 {
2062 KASSERT(ngq != &ng_deadnode.nd_input_queue,
2063 ("%s: working on deadnode", __func__));
2064
2065 /* ######### Hack alert ######### */
2066 atomic_add_long(&ngq->q_flags, READER_INCREMENT);
2067 if ((ngq->q_flags & NGQ_RMASK) == 0) {
2068 /* Successfully grabbed node */
2069 CTR4(KTR_NET, "%20s: node [%x] (%p) fast acquired item %p",
2070 __func__, ngq->q_node->nd_ID, ngq->q_node, item);
2071 return (item);
2072 }
2073 /* undo the damage if we didn't succeed */
2074 atomic_subtract_long(&ngq->q_flags, READER_INCREMENT);
2075
2076 /* ######### End Hack alert ######### */
2077 NG_QUEUE_LOCK(ngq);
2078 /*
2079 * Try again. Another processor (or interrupt for that matter) may
2080 * have removed the last queued item that was stopping us from
2081 * running, between the previous test, and the moment that we took
2082 * the mutex. (Or maybe a writer completed.)
2083 * Even if another fast-track reader hits during this period
2084 * we don't care as multiple readers is OK.
2085 */
2086 if ((ngq->q_flags & NGQ_RMASK) == 0) {
2087 atomic_add_long(&ngq->q_flags, READER_INCREMENT);
2088 NG_QUEUE_UNLOCK(ngq);
2089 CTR4(KTR_NET, "%20s: node [%x] (%p) slow acquired item %p",
2090 __func__, ngq->q_node->nd_ID, ngq->q_node, item);
2091 return (item);
2092 }
2093
2094 /*
2095 * and queue the request for later.
2096 */
2097 ng_queue_rw(ngq, item, NGQRW_R);
2098 NG_QUEUE_UNLOCK(ngq);
2099
2100 return (NULL);
2101 }
2102
2103 static __inline item_p
2104 ng_acquire_write(struct ng_queue *ngq, item_p item)
2105 {
2106 KASSERT(ngq != &ng_deadnode.nd_input_queue,
2107 ("%s: working on deadnode", __func__));
2108
2109 restart:
2110 NG_QUEUE_LOCK(ngq);
2111 /*
2112 * If there are no readers, no writer, and no pending packets, then
2113 * we can just go ahead. In all other situations we need to queue the
2114 * request
2115 */
2116 if ((ngq->q_flags & NGQ_WMASK) == 0) {
2117 /* collision could happen *HERE* */
2118 atomic_add_long(&ngq->q_flags, WRITER_ACTIVE);
2119 NG_QUEUE_UNLOCK(ngq);
2120 if (ngq->q_flags & READER_MASK) {
2121 /* Collision with fast-track reader */
2122 atomic_subtract_long(&ngq->q_flags, WRITER_ACTIVE);
2123 goto restart;
2124 }
2125 CTR4(KTR_NET, "%20s: node [%x] (%p) acquired item %p",
2126 __func__, ngq->q_node->nd_ID, ngq->q_node, item);
2127 return (item);
2128 }
2129
2130 /*
2131 * and queue the request for later.
2132 */
2133 ng_queue_rw(ngq, item, NGQRW_W);
2134 NG_QUEUE_UNLOCK(ngq);
2135
2136 return (NULL);
2137 }
2138
2139 static __inline void
2140 ng_leave_read(struct ng_queue *ngq)
2141 {
2142 atomic_subtract_long(&ngq->q_flags, READER_INCREMENT);
2143 }
2144
2145 static __inline void
2146 ng_leave_write(struct ng_queue *ngq)
2147 {
2148 atomic_subtract_long(&ngq->q_flags, WRITER_ACTIVE);
2149 }
2150
2151 static void
2152 ng_flush_input_queue(struct ng_queue * ngq)
2153 {
2154 item_p item;
2155
2156 NG_QUEUE_LOCK(ngq);
2157 while (ngq->queue) {
2158 item = ngq->queue;
2159 ngq->queue = item->el_next;
2160 if (ngq->last == &(item->el_next)) {
2161 ngq->last = &(ngq->queue);
2162 atomic_add_long(&ngq->q_flags, -OP_PENDING);
2163 }
2164 NG_QUEUE_UNLOCK(ngq);
2165
2166 /* If the item is supplying a callback, call it with an error */
2167 if (item->apply != NULL) {
2168 if (item->depth == 1)
2169 item->apply->error = ENOENT;
2170 if (refcount_release(&item->apply->refs)) {
2171 (*item->apply->apply)(item->apply->context,
2172 item->apply->error);
2173 }
2174 }
2175 NG_FREE_ITEM(item);
2176 NG_QUEUE_LOCK(ngq);
2177 }
2178 /*
2179 * Take us off the work queue if we are there.
2180 * We definately have no work to be done.
2181 */
2182 ng_worklist_remove(ngq->q_node);
2183 NG_QUEUE_UNLOCK(ngq);
2184 }
2185
2186 /***********************************************************************
2187 * Externally visible method for sending or queueing messages or data.
2188 ***********************************************************************/
2189
2190 /*
2191 * The module code should have filled out the item correctly by this stage:
2192 * Common:
2193 * reference to destination node.
2194 * Reference to destination rcv hook if relevant.
2195 * apply pointer must be or NULL or reference valid struct ng_apply_info.
2196 * Data:
2197 * pointer to mbuf
2198 * Control_Message:
2199 * pointer to msg.
2200 * ID of original sender node. (return address)
2201 * Function:
2202 * Function pointer
2203 * void * argument
2204 * integer argument
2205 *
2206 * The nodes have several routines and macros to help with this task:
2207 */
2208
2209 int
2210 ng_snd_item(item_p item, int flags)
2211 {
2212 hook_p hook;
2213 node_p node;
2214 int queue, rw;
2215 struct ng_queue *ngq;
2216 int error = 0;
2217
2218 /* We are sending item, so it must be present! */
2219 KASSERT(item != NULL, ("ng_snd_item: item is NULL"));
2220
2221 #ifdef NETGRAPH_DEBUG
2222 _ngi_check(item, __FILE__, __LINE__);
2223 #endif
2224
2225 /* Item was sent once more, postpone apply() call. */
2226 if (item->apply)
2227 refcount_acquire(&item->apply->refs);
2228
2229 node = NGI_NODE(item);
2230 /* Node is never optional. */
2231 KASSERT(node != NULL, ("ng_snd_item: node is NULL"));
2232
2233 hook = NGI_HOOK(item);
2234 /* Valid hook and mbuf are mandatory for data. */
2235 if ((item->el_flags & NGQF_TYPE) == NGQF_DATA) {
2236 KASSERT(hook != NULL, ("ng_snd_item: hook for data is NULL"));
2237 if (NGI_M(item) == NULL)
2238 ERROUT(EINVAL);
2239 CHECK_DATA_MBUF(NGI_M(item));
2240 }
2241
2242 /*
2243 * If the item or the node specifies single threading, force
2244 * writer semantics. Similarly, the node may say one hook always
2245 * produces writers. These are overrides.
2246 */
2247 if (((item->el_flags & NGQF_RW) == NGQF_WRITER) ||
2248 (node->nd_flags & NGF_FORCE_WRITER) ||
2249 (hook && (hook->hk_flags & HK_FORCE_WRITER))) {
2250 rw = NGQRW_W;
2251 } else {
2252 rw = NGQRW_R;
2253 }
2254
2255 /*
2256 * If sender or receiver requests queued delivery or stack usage
2257 * level is dangerous - enqueue message.
2258 */
2259 if ((flags & NG_QUEUE) || (hook && (hook->hk_flags & HK_QUEUE))) {
2260 queue = 1;
2261 } else {
2262 queue = 0;
2263 #ifdef GET_STACK_USAGE
2264 /*
2265 * Most of netgraph nodes have small stack consumption and
2266 * for them 25% of free stack space is more than enough.
2267 * Nodes/hooks with higher stack usage should be marked as
2268 * HI_STACK. For them 50% of stack will be guaranteed then.
2269 * XXX: Values 25% and 50% are completely empirical.
2270 */
2271 size_t st, su, sl;
2272 GET_STACK_USAGE(st, su);
2273 sl = st - su;
2274 if ((sl * 4 < st) ||
2275 ((sl * 2 < st) && ((node->nd_flags & NGF_HI_STACK) ||
2276 (hook && (hook->hk_flags & HK_HI_STACK))))) {
2277 queue = 1;
2278 }
2279 #endif
2280 }
2281
2282 ngq = &node->nd_input_queue;
2283 if (queue) {
2284 /* Put it on the queue for that node*/
2285 #ifdef NETGRAPH_DEBUG
2286 _ngi_check(item, __FILE__, __LINE__);
2287 #endif
2288 item->depth = 1;
2289 NG_QUEUE_LOCK(ngq);
2290 ng_queue_rw(ngq, item, rw);
2291 NG_QUEUE_UNLOCK(ngq);
2292
2293 return ((flags & NG_PROGRESS) ? EINPROGRESS : 0);
2294 }
2295
2296 /*
2297 * We already decided how we will be queueud or treated.
2298 * Try get the appropriate operating permission.
2299 */
2300 if (rw == NGQRW_R)
2301 item = ng_acquire_read(ngq, item);
2302 else
2303 item = ng_acquire_write(ngq, item);
2304
2305
2306 /* Item was queued while trying to get permission. */
2307 if (item == NULL)
2308 return ((flags & NG_PROGRESS) ? EINPROGRESS : 0);
2309
2310 #ifdef NETGRAPH_DEBUG
2311 _ngi_check(item, __FILE__, __LINE__);
2312 #endif
2313
2314 NGI_GET_NODE(item, node); /* zaps stored node */
2315
2316 item->depth++;
2317 error = ng_apply_item(node, item, rw); /* drops r/w lock when done */
2318
2319 /*
2320 * If the node goes away when we remove the reference,
2321 * whatever we just did caused it.. whatever we do, DO NOT
2322 * access the node again!
2323 */
2324 if (NG_NODE_UNREF(node) == 0)
2325 return (error);
2326
2327 NG_QUEUE_LOCK(ngq);
2328 if (NEXT_QUEUED_ITEM_CAN_PROCEED(ngq))
2329 ng_setisr(ngq->q_node);
2330 NG_QUEUE_UNLOCK(ngq);
2331
2332 return (error);
2333
2334 done:
2335 /* If was not sent, apply callback here. */
2336 if (item->apply != NULL) {
2337 if (item->depth == 0 && error != 0)
2338 item->apply->error = error;
2339 if (refcount_release(&item->apply->refs)) {
2340 (*item->apply->apply)(item->apply->context,
2341 item->apply->error);
2342 }
2343 }
2344
2345 NG_FREE_ITEM(item);
2346 return (error);
2347 }
2348
2349 /*
2350 * We have an item that was possibly queued somewhere.
2351 * It should contain all the information needed
2352 * to run it on the appropriate node/hook.
2353 * If there is apply pointer and we own the last reference, call apply().
2354 */
2355 static int
2356 ng_apply_item(node_p node, item_p item, int rw)
2357 {
2358 hook_p hook;
2359 ng_rcvdata_t *rcvdata;
2360 ng_rcvmsg_t *rcvmsg;
2361 struct ng_apply_info *apply;
2362 int error = 0, depth;
2363
2364 /* Node and item are never optional. */
2365 KASSERT(node != NULL, ("ng_apply_item: node is NULL"));
2366 KASSERT(item != NULL, ("ng_apply_item: item is NULL"));
2367
2368 NGI_GET_HOOK(item, hook); /* clears stored hook */
2369 #ifdef NETGRAPH_DEBUG
2370 _ngi_check(item, __FILE__, __LINE__);
2371 #endif
2372
2373 apply = item->apply;
2374 depth = item->depth;
2375
2376 switch (item->el_flags & NGQF_TYPE) {
2377 case NGQF_DATA:
2378 /*
2379 * Check things are still ok as when we were queued.
2380 */
2381 KASSERT(hook != NULL, ("ng_apply_item: hook for data is NULL"));
2382 if (NG_HOOK_NOT_VALID(hook) ||
2383 NG_NODE_NOT_VALID(node)) {
2384 error = EIO;
2385 NG_FREE_ITEM(item);
2386 break;
2387 }
2388 /*
2389 * If no receive method, just silently drop it.
2390 * Give preference to the hook over-ride method
2391 */
2392 if ((!(rcvdata = hook->hk_rcvdata))
2393 && (!(rcvdata = NG_HOOK_NODE(hook)->nd_type->rcvdata))) {
2394 error = 0;
2395 NG_FREE_ITEM(item);
2396 break;
2397 }
2398 error = (*rcvdata)(hook, item);
2399 break;
2400 case NGQF_MESG:
2401 if (hook && NG_HOOK_NOT_VALID(hook)) {
2402 /*
2403 * The hook has been zapped then we can't use it.
2404 * Immediately drop its reference.
2405 * The message may not need it.
2406 */
2407 NG_HOOK_UNREF(hook);
2408 hook = NULL;
2409 }
2410 /*
2411 * Similarly, if the node is a zombie there is
2412 * nothing we can do with it, drop everything.
2413 */
2414 if (NG_NODE_NOT_VALID(node)) {
2415 TRAP_ERROR();
2416 error = EINVAL;
2417 NG_FREE_ITEM(item);
2418 break;
2419 }
2420 /*
2421 * Call the appropriate message handler for the object.
2422 * It is up to the message handler to free the message.
2423 * If it's a generic message, handle it generically,
2424 * otherwise call the type's message handler (if it exists).
2425 * XXX (race). Remember that a queued message may
2426 * reference a node or hook that has just been
2427 * invalidated. It will exist as the queue code
2428 * is holding a reference, but..
2429 */
2430 if ((NGI_MSG(item)->header.typecookie == NGM_GENERIC_COOKIE) &&
2431 ((NGI_MSG(item)->header.flags & NGF_RESP) == 0)) {
2432 error = ng_generic_msg(node, item, hook);
2433 break;
2434 }
2435 if (((!hook) || (!(rcvmsg = hook->hk_rcvmsg))) &&
2436 (!(rcvmsg = node->nd_type->rcvmsg))) {
2437 TRAP_ERROR();
2438 error = 0;
2439 NG_FREE_ITEM(item);
2440 break;
2441 }
2442 error = (*rcvmsg)(node, item, hook);
2443 break;
2444 case NGQF_FN:
2445 case NGQF_FN2:
2446 /*
2447 * We have to implicitly trust the hook,
2448 * as some of these are used for system purposes
2449 * where the hook is invalid. In the case of
2450 * the shutdown message we allow it to hit
2451 * even if the node is invalid.
2452 */
2453 if ((NG_NODE_NOT_VALID(node))
2454 && (NGI_FN(item) != &ng_rmnode)) {
2455 TRAP_ERROR();
2456 error = EINVAL;
2457 NG_FREE_ITEM(item);
2458 break;
2459 }
2460 if ((item->el_flags & NGQF_TYPE) == NGQF_FN) {
2461 (*NGI_FN(item))(node, hook, NGI_ARG1(item),
2462 NGI_ARG2(item));
2463 NG_FREE_ITEM(item);
2464 } else /* it is NGQF_FN2 */
2465 error = (*NGI_FN2(item))(node, item, hook);
2466 break;
2467 }
2468 /*
2469 * We held references on some of the resources
2470 * that we took from the item. Now that we have
2471 * finished doing everything, drop those references.
2472 */
2473 if (hook)
2474 NG_HOOK_UNREF(hook);
2475
2476 if (rw == NGQRW_R)
2477 ng_leave_read(&node->nd_input_queue);
2478 else
2479 ng_leave_write(&node->nd_input_queue);
2480
2481 /* Apply callback. */
2482 if (apply != NULL) {
2483 if (depth == 1 && error != 0)
2484 apply->error = error;
2485 if (refcount_release(&apply->refs))
2486 (*apply->apply)(apply->context, apply->error);
2487 }
2488
2489 return (error);
2490 }
2491
2492 /***********************************************************************
2493 * Implement the 'generic' control messages
2494 ***********************************************************************/
2495 static int
2496 ng_generic_msg(node_p here, item_p item, hook_p lasthook)
2497 {
2498 int error = 0;
2499 struct ng_mesg *msg;
2500 struct ng_mesg *resp = NULL;
2501
2502 NGI_GET_MSG(item, msg);
2503 if (msg->header.typecookie != NGM_GENERIC_COOKIE) {
2504 TRAP_ERROR();
2505 error = EINVAL;
2506 goto out;
2507 }
2508 switch (msg->header.cmd) {
2509 case NGM_SHUTDOWN:
2510 ng_rmnode(here, NULL, NULL, 0);
2511 break;
2512 case NGM_MKPEER:
2513 {
2514 struct ngm_mkpeer *const mkp = (struct ngm_mkpeer *) msg->data;
2515
2516 if (msg->header.arglen != sizeof(*mkp)) {
2517 TRAP_ERROR();
2518 error = EINVAL;
2519 break;
2520 }
2521 mkp->type[sizeof(mkp->type) - 1] = '\0';
2522 mkp->ourhook[sizeof(mkp->ourhook) - 1] = '\0';
2523 mkp->peerhook[sizeof(mkp->peerhook) - 1] = '\0';
2524 error = ng_mkpeer(here, mkp->ourhook, mkp->peerhook, mkp->type);
2525 break;
2526 }
2527 case NGM_CONNECT:
2528 {
2529 struct ngm_connect *const con =
2530 (struct ngm_connect *) msg->data;
2531 node_p node2;
2532
2533 if (msg->header.arglen != sizeof(*con)) {
2534 TRAP_ERROR();
2535 error = EINVAL;
2536 break;
2537 }
2538 con->path[sizeof(con->path) - 1] = '\0';
2539 con->ourhook[sizeof(con->ourhook) - 1] = '\0';
2540 con->peerhook[sizeof(con->peerhook) - 1] = '\0';
2541 /* Don't forget we get a reference.. */
2542 error = ng_path2noderef(here, con->path, &node2, NULL);
2543 if (error)
2544 break;
2545 error = ng_con_nodes(item, here, con->ourhook,
2546 node2, con->peerhook);
2547 NG_NODE_UNREF(node2);
2548 break;
2549 }
2550 case NGM_NAME:
2551 {
2552 struct ngm_name *const nam = (struct ngm_name *) msg->data;
2553
2554 if (msg->header.arglen != sizeof(*nam)) {
2555 TRAP_ERROR();
2556 error = EINVAL;
2557 break;
2558 }
2559 nam->name[sizeof(nam->name) - 1] = '\0';
2560 error = ng_name_node(here, nam->name);
2561 break;
2562 }
2563 case NGM_RMHOOK:
2564 {
2565 struct ngm_rmhook *const rmh = (struct ngm_rmhook *) msg->data;
2566 hook_p hook;
2567
2568 if (msg->header.arglen != sizeof(*rmh)) {
2569 TRAP_ERROR();
2570 error = EINVAL;
2571 break;
2572 }
2573 rmh->ourhook[sizeof(rmh->ourhook) - 1] = '\0';
2574 if ((hook = ng_findhook(here, rmh->ourhook)) != NULL)
2575 ng_destroy_hook(hook);
2576 break;
2577 }
2578 case NGM_NODEINFO:
2579 {
2580 struct nodeinfo *ni;
2581
2582 NG_MKRESPONSE(resp, msg, sizeof(*ni), M_NOWAIT);
2583 if (resp == NULL) {
2584 error = ENOMEM;
2585 break;
2586 }
2587
2588 /* Fill in node info */
2589 ni = (struct nodeinfo *) resp->data;
2590 if (NG_NODE_HAS_NAME(here))
2591 strcpy(ni->name, NG_NODE_NAME(here));
2592 strcpy(ni->type, here->nd_type->name);
2593 ni->id = ng_node2ID(here);
2594 ni->hooks = here->nd_numhooks;
2595 break;
2596 }
2597 case NGM_LISTHOOKS:
2598 {
2599 const int nhooks = here->nd_numhooks;
2600 struct hooklist *hl;
2601 struct nodeinfo *ni;
2602 hook_p hook;
2603
2604 /* Get response struct */
2605 NG_MKRESPONSE(resp, msg, sizeof(*hl)
2606 + (nhooks * sizeof(struct linkinfo)), M_NOWAIT);
2607 if (resp == NULL) {
2608 error = ENOMEM;
2609 break;
2610 }
2611 hl = (struct hooklist *) resp->data;
2612 ni = &hl->nodeinfo;
2613
2614 /* Fill in node info */
2615 if (NG_NODE_HAS_NAME(here))
2616 strcpy(ni->name, NG_NODE_NAME(here));
2617 strcpy(ni->type, here->nd_type->name);
2618 ni->id = ng_node2ID(here);
2619
2620 /* Cycle through the linked list of hooks */
2621 ni->hooks = 0;
2622 LIST_FOREACH(hook, &here->nd_hooks, hk_hooks) {
2623 struct linkinfo *const link = &hl->link[ni->hooks];
2624
2625 if (ni->hooks >= nhooks) {
2626 log(LOG_ERR, "%s: number of %s changed\n",
2627 __func__, "hooks");
2628 break;
2629 }
2630 if (NG_HOOK_NOT_VALID(hook))
2631 continue;
2632 strcpy(link->ourhook, NG_HOOK_NAME(hook));
2633 strcpy(link->peerhook, NG_PEER_HOOK_NAME(hook));
2634 if (NG_PEER_NODE_NAME(hook)[0] != '\0')
2635 strcpy(link->nodeinfo.name,
2636 NG_PEER_NODE_NAME(hook));
2637 strcpy(link->nodeinfo.type,
2638 NG_PEER_NODE(hook)->nd_type->name);
2639 link->nodeinfo.id = ng_node2ID(NG_PEER_NODE(hook));
2640 link->nodeinfo.hooks = NG_PEER_NODE(hook)->nd_numhooks;
2641 ni->hooks++;
2642 }
2643 break;
2644 }
2645
2646 case NGM_LISTNAMES:
2647 case NGM_LISTNODES:
2648 {
2649 const int unnamed = (msg->header.cmd == NGM_LISTNODES);
2650 struct namelist *nl;
2651 node_p node;
2652 int num = 0, i;
2653
2654 mtx_lock(&ng_namehash_mtx);
2655 /* Count number of nodes */
2656 for (i = 0; i < NG_NAME_HASH_SIZE; i++) {
2657 LIST_FOREACH(node, &ng_name_hash[i], nd_nodes) {
2658 if (NG_NODE_IS_VALID(node) &&
2659 (unnamed || NG_NODE_HAS_NAME(node))) {
2660 num++;
2661 }
2662 }
2663 }
2664 mtx_unlock(&ng_namehash_mtx);
2665
2666 /* Get response struct */
2667 NG_MKRESPONSE(resp, msg, sizeof(*nl)
2668 + (num * sizeof(struct nodeinfo)), M_NOWAIT);
2669 if (resp == NULL) {
2670 error = ENOMEM;
2671 break;
2672 }
2673 nl = (struct namelist *) resp->data;
2674
2675 /* Cycle through the linked list of nodes */
2676 nl->numnames = 0;
2677 mtx_lock(&ng_namehash_mtx);
2678 for (i = 0; i < NG_NAME_HASH_SIZE; i++) {
2679 LIST_FOREACH(node, &ng_name_hash[i], nd_nodes) {
2680 struct nodeinfo *const np =
2681 &nl->nodeinfo[nl->numnames];
2682
2683 if (NG_NODE_NOT_VALID(node))
2684 continue;
2685 if (!unnamed && (! NG_NODE_HAS_NAME(node)))
2686 continue;
2687 if (nl->numnames >= num) {
2688 log(LOG_ERR, "%s: number of nodes changed\n",
2689 __func__);
2690 break;
2691 }
2692 if (NG_NODE_HAS_NAME(node))
2693 strcpy(np->name, NG_NODE_NAME(node));
2694 strcpy(np->type, node->nd_type->name);
2695 np->id = ng_node2ID(node);
2696 np->hooks = node->nd_numhooks;
2697 nl->numnames++;
2698 }
2699 }
2700 mtx_unlock(&ng_namehash_mtx);
2701 break;
2702 }
2703
2704 case NGM_LISTTYPES:
2705 {
2706 struct typelist *tl;
2707 struct ng_type *type;
2708 int num = 0;
2709
2710 mtx_lock(&ng_typelist_mtx);
2711 /* Count number of types */
2712 LIST_FOREACH(type, &ng_typelist, types) {
2713 num++;
2714 }
2715 mtx_unlock(&ng_typelist_mtx);
2716
2717 /* Get response struct */
2718 NG_MKRESPONSE(resp, msg, sizeof(*tl)
2719 + (num * sizeof(struct typeinfo)), M_NOWAIT);
2720 if (resp == NULL) {
2721 error = ENOMEM;
2722 break;
2723 }
2724 tl = (struct typelist *) resp->data;
2725
2726 /* Cycle through the linked list of types */
2727 tl->numtypes = 0;
2728 mtx_lock(&ng_typelist_mtx);
2729 LIST_FOREACH(type, &ng_typelist, types) {
2730 struct typeinfo *const tp = &tl->typeinfo[tl->numtypes];
2731
2732 if (tl->numtypes >= num) {
2733 log(LOG_ERR, "%s: number of %s changed\n",
2734 __func__, "types");
2735 break;
2736 }
2737 strcpy(tp->type_name, type->name);
2738 tp->numnodes = type->refs - 1; /* don't count list */
2739 tl->numtypes++;
2740 }
2741 mtx_unlock(&ng_typelist_mtx);
2742 break;
2743 }
2744
2745 case NGM_BINARY2ASCII:
2746 {
2747 int bufSize = 20 * 1024; /* XXX hard coded constant */
2748 const struct ng_parse_type *argstype;
2749 const struct ng_cmdlist *c;
2750 struct ng_mesg *binary, *ascii;
2751
2752 /* Data area must contain a valid netgraph message */
2753 binary = (struct ng_mesg *)msg->data;
2754 if (msg->header.arglen < sizeof(struct ng_mesg) ||
2755 (msg->header.arglen - sizeof(struct ng_mesg) <
2756 binary->header.arglen)) {
2757 TRAP_ERROR();
2758 error = EINVAL;
2759 break;
2760 }
2761
2762 /* Get a response message with lots of room */
2763 NG_MKRESPONSE(resp, msg, sizeof(*ascii) + bufSize, M_NOWAIT);
2764 if (resp == NULL) {
2765 error = ENOMEM;
2766 break;
2767 }
2768 ascii = (struct ng_mesg *)resp->data;
2769
2770 /* Copy binary message header to response message payload */
2771 bcopy(binary, ascii, sizeof(*binary));
2772
2773 /* Find command by matching typecookie and command number */
2774 for (c = here->nd_type->cmdlist;
2775 c != NULL && c->name != NULL; c++) {
2776 if (binary->header.typecookie == c->cookie
2777 && binary->header.cmd == c->cmd)
2778 break;
2779 }
2780 if (c == NULL || c->name == NULL) {
2781 for (c = ng_generic_cmds; c->name != NULL; c++) {
2782 if (binary->header.typecookie == c->cookie
2783 && binary->header.cmd == c->cmd)
2784 break;
2785 }
2786 if (c->name == NULL) {
2787 NG_FREE_MSG(resp);
2788 error = ENOSYS;
2789 break;
2790 }
2791 }
2792
2793 /* Convert command name to ASCII */
2794 snprintf(ascii->header.cmdstr, sizeof(ascii->header.cmdstr),
2795 "%s", c->name);
2796
2797 /* Convert command arguments to ASCII */
2798 argstype = (binary->header.flags & NGF_RESP) ?
2799 c->respType : c->mesgType;
2800 if (argstype == NULL) {
2801 *ascii->data = '\0';
2802 } else {
2803 if ((error = ng_unparse(argstype,
2804 (u_char *)binary->data,
2805 ascii->data, bufSize)) != 0) {
2806 NG_FREE_MSG(resp);
2807 break;
2808 }
2809 }
2810
2811 /* Return the result as struct ng_mesg plus ASCII string */
2812 bufSize = strlen(ascii->data) + 1;
2813 ascii->header.arglen = bufSize;
2814 resp->header.arglen = sizeof(*ascii) + bufSize;
2815 break;
2816 }
2817
2818 case NGM_ASCII2BINARY:
2819 {
2820 int bufSize = 2000; /* XXX hard coded constant */
2821 const struct ng_cmdlist *c;
2822 const struct ng_parse_type *argstype;
2823 struct ng_mesg *ascii, *binary;
2824 int off = 0;
2825
2826 /* Data area must contain at least a struct ng_mesg + '\0' */
2827 ascii = (struct ng_mesg *)msg->data;
2828 if ((msg->header.arglen < sizeof(*ascii) + 1) ||
2829 (ascii->header.arglen < 1) ||
2830 (msg->header.arglen < sizeof(*ascii) +
2831 ascii->header.arglen)) {
2832 TRAP_ERROR();
2833 error = EINVAL;
2834 break;
2835 }
2836 ascii->data[ascii->header.arglen - 1] = '\0';
2837
2838 /* Get a response message with lots of room */
2839 NG_MKRESPONSE(resp, msg, sizeof(*binary) + bufSize, M_NOWAIT);
2840 if (resp == NULL) {
2841 error = ENOMEM;
2842 break;
2843 }
2844 binary = (struct ng_mesg *)resp->data;
2845
2846 /* Copy ASCII message header to response message payload */
2847 bcopy(ascii, binary, sizeof(*ascii));
2848
2849 /* Find command by matching ASCII command string */
2850 for (c = here->nd_type->cmdlist;
2851 c != NULL && c->name != NULL; c++) {
2852 if (strcmp(ascii->header.cmdstr, c->name) == 0)
2853 break;
2854 }
2855 if (c == NULL || c->name == NULL) {
2856 for (c = ng_generic_cmds; c->name != NULL; c++) {
2857 if (strcmp(ascii->header.cmdstr, c->name) == 0)
2858 break;
2859 }
2860 if (c->name == NULL) {
2861 NG_FREE_MSG(resp);
2862 error = ENOSYS;
2863 break;
2864 }
2865 }
2866
2867 /* Convert command name to binary */
2868 binary->header.cmd = c->cmd;
2869 binary->header.typecookie = c->cookie;
2870
2871 /* Convert command arguments to binary */
2872 argstype = (binary->header.flags & NGF_RESP) ?
2873 c->respType : c->mesgType;
2874 if (argstype == NULL) {
2875 bufSize = 0;
2876 } else {
2877 if ((error = ng_parse(argstype, ascii->data,
2878 &off, (u_char *)binary->data, &bufSize)) != 0) {
2879 NG_FREE_MSG(resp);
2880 break;
2881 }
2882 }
2883
2884 /* Return the result */
2885 binary->header.arglen = bufSize;
2886 resp->header.arglen = sizeof(*binary) + bufSize;
2887 break;
2888 }
2889
2890 case NGM_TEXT_CONFIG:
2891 case NGM_TEXT_STATUS:
2892 /*
2893 * This one is tricky as it passes the command down to the
2894 * actual node, even though it is a generic type command.
2895 * This means we must assume that the item/msg is already freed
2896 * when control passes back to us.
2897 */
2898 if (here->nd_type->rcvmsg != NULL) {
2899 NGI_MSG(item) = msg; /* put it back as we found it */
2900 return((*here->nd_type->rcvmsg)(here, item, lasthook));
2901 }
2902 /* Fall through if rcvmsg not supported */
2903 default:
2904 TRAP_ERROR();
2905 error = EINVAL;
2906 }
2907 /*
2908 * Sometimes a generic message may be statically allocated
2909 * to avoid problems with allocating when in tight memeory situations.
2910 * Don't free it if it is so.
2911 * I break them appart here, because erros may cause a free if the item
2912 * in which case we'd be doing it twice.
2913 * they are kept together above, to simplify freeing.
2914 */
2915 out:
2916 NG_RESPOND_MSG(error, here, item, resp);
2917 if (msg)
2918 NG_FREE_MSG(msg);
2919 return (error);
2920 }
2921
2922 /************************************************************************
2923 Queue element get/free routines
2924 ************************************************************************/
2925
2926 uma_zone_t ng_qzone;
2927 uma_zone_t ng_qdzone;
2928 static int maxalloc = 4096;/* limit the damage of a leak */
2929 static int maxdata = 512; /* limit the damage of a DoS */
2930
2931 TUNABLE_INT("net.graph.maxalloc", &maxalloc);
2932 SYSCTL_INT(_net_graph, OID_AUTO, maxalloc, CTLFLAG_RDTUN, &maxalloc,
2933 0, "Maximum number of non-data queue items to allocate");
2934 TUNABLE_INT("net.graph.maxdata", &maxdata);
2935 SYSCTL_INT(_net_graph, OID_AUTO, maxdata, CTLFLAG_RDTUN, &maxdata,
2936 0, "Maximum number of data queue items to allocate");
2937
2938 #ifdef NETGRAPH_DEBUG
2939 static TAILQ_HEAD(, ng_item) ng_itemlist = TAILQ_HEAD_INITIALIZER(ng_itemlist);
2940 static int allocated; /* number of items malloc'd */
2941 #endif
2942
2943 /*
2944 * Get a queue entry.
2945 * This is usually called when a packet first enters netgraph.
2946 * By definition, this is usually from an interrupt, or from a user.
2947 * Users are not so important, but try be quick for the times that it's
2948 * an interrupt.
2949 */
2950 static __inline item_p
2951 ng_alloc_item(int type, int flags)
2952 {
2953 item_p item;
2954
2955 KASSERT(((type & ~NGQF_TYPE) == 0),
2956 ("%s: incorrect item type: %d", __func__, type));
2957
2958 item = uma_zalloc((type == NGQF_DATA)?ng_qdzone:ng_qzone,
2959 ((flags & NG_WAITOK) ? M_WAITOK : M_NOWAIT) | M_ZERO);
2960
2961 if (item) {
2962 item->el_flags = type;
2963 #ifdef NETGRAPH_DEBUG
2964 mtx_lock(&ngq_mtx);
2965 TAILQ_INSERT_TAIL(&ng_itemlist, item, all);
2966 allocated++;
2967 mtx_unlock(&ngq_mtx);
2968 #endif
2969 }
2970
2971 return (item);
2972 }
2973
2974 /*
2975 * Release a queue entry
2976 */
2977 void
2978 ng_free_item(item_p item)
2979 {
2980 /*
2981 * The item may hold resources on it's own. We need to free
2982 * these before we can free the item. What they are depends upon
2983 * what kind of item it is. it is important that nodes zero
2984 * out pointers to resources that they remove from the item
2985 * or we release them again here.
2986 */
2987 switch (item->el_flags & NGQF_TYPE) {
2988 case NGQF_DATA:
2989 /* If we have an mbuf still attached.. */
2990 NG_FREE_M(_NGI_M(item));
2991 break;
2992 case NGQF_MESG:
2993 _NGI_RETADDR(item) = 0;
2994 NG_FREE_MSG(_NGI_MSG(item));
2995 break;
2996 case NGQF_FN:
2997 case NGQF_FN2:
2998 /* nothing to free really, */
2999 _NGI_FN(item) = NULL;
3000 _NGI_ARG1(item) = NULL;
3001 _NGI_ARG2(item) = 0;
3002 break;
3003 }
3004 /* If we still have a node or hook referenced... */
3005 _NGI_CLR_NODE(item);
3006 _NGI_CLR_HOOK(item);
3007
3008 #ifdef NETGRAPH_DEBUG
3009 mtx_lock(&ngq_mtx);
3010 TAILQ_REMOVE(&ng_itemlist, item, all);
3011 allocated--;
3012 mtx_unlock(&ngq_mtx);
3013 #endif
3014 uma_zfree(((item->el_flags & NGQF_TYPE) == NGQF_DATA)?
3015 ng_qdzone:ng_qzone, item);
3016 }
3017
3018 /*
3019 * Change type of the queue entry.
3020 * Possibly reallocates it from another UMA zone.
3021 */
3022 static __inline item_p
3023 ng_realloc_item(item_p pitem, int type, int flags)
3024 {
3025 item_p item;
3026 int from, to;
3027
3028 KASSERT((pitem != NULL), ("%s: can't reallocate NULL", __func__));
3029 KASSERT(((type & ~NGQF_TYPE) == 0),
3030 ("%s: incorrect item type: %d", __func__, type));
3031
3032 from = ((pitem->el_flags & NGQF_TYPE) == NGQF_DATA);
3033 to = (type == NGQF_DATA);
3034 if (from != to) {
3035 /* If reallocation is required do it and copy item. */
3036 if ((item = ng_alloc_item(type, flags)) == NULL) {
3037 ng_free_item(pitem);
3038 return (NULL);
3039 }
3040 *item = *pitem;
3041 ng_free_item(pitem);
3042 } else
3043 item = pitem;
3044 item->el_flags = (item->el_flags & ~NGQF_TYPE) | type;
3045
3046 return (item);
3047 }
3048
3049 /************************************************************************
3050 Module routines
3051 ************************************************************************/
3052
3053 /*
3054 * Handle the loading/unloading of a netgraph node type module
3055 */
3056 int
3057 ng_mod_event(module_t mod, int event, void *data)
3058 {
3059 struct ng_type *const type = data;
3060 int s, error = 0;
3061
3062 switch (event) {
3063 case MOD_LOAD:
3064
3065 /* Register new netgraph node type */
3066 s = splnet();
3067 if ((error = ng_newtype(type)) != 0) {
3068 splx(s);
3069 break;
3070 }
3071
3072 /* Call type specific code */
3073 if (type->mod_event != NULL)
3074 if ((error = (*type->mod_event)(mod, event, data))) {
3075 mtx_lock(&ng_typelist_mtx);
3076 type->refs--; /* undo it */
3077 LIST_REMOVE(type, types);
3078 mtx_unlock(&ng_typelist_mtx);
3079 }
3080 splx(s);
3081 break;
3082
3083 case MOD_UNLOAD:
3084 s = splnet();
3085 if (type->refs > 1) { /* make sure no nodes exist! */
3086 error = EBUSY;
3087 } else {
3088 if (type->refs == 0) {
3089 /* failed load, nothing to undo */
3090 splx(s);
3091 break;
3092 }
3093 if (type->mod_event != NULL) { /* check with type */
3094 error = (*type->mod_event)(mod, event, data);
3095 if (error != 0) { /* type refuses.. */
3096 splx(s);
3097 break;
3098 }
3099 }
3100 mtx_lock(&ng_typelist_mtx);
3101 LIST_REMOVE(type, types);
3102 mtx_unlock(&ng_typelist_mtx);
3103 }
3104 splx(s);
3105 break;
3106
3107 default:
3108 if (type->mod_event != NULL)
3109 error = (*type->mod_event)(mod, event, data);
3110 else
3111 error = EOPNOTSUPP; /* XXX ? */
3112 break;
3113 }
3114 return (error);
3115 }
3116
3117 /*
3118 * Handle loading and unloading for this code.
3119 * The only thing we need to link into is the NETISR strucure.
3120 */
3121 static int
3122 ngb_mod_event(module_t mod, int event, void *data)
3123 {
3124 int error = 0;
3125
3126 switch (event) {
3127 case MOD_LOAD:
3128 /* Initialize everything. */
3129 NG_WORKLIST_LOCK_INIT();
3130 mtx_init(&ng_typelist_mtx, "netgraph types mutex", NULL,
3131 MTX_DEF);
3132 mtx_init(&ng_idhash_mtx, "netgraph idhash mutex", NULL,
3133 MTX_DEF);
3134 mtx_init(&ng_namehash_mtx, "netgraph namehash mutex", NULL,
3135 MTX_DEF);
3136 mtx_init(&ng_topo_mtx, "netgraph topology mutex", NULL,
3137 MTX_DEF);
3138 #ifdef NETGRAPH_DEBUG
3139 mtx_init(&ng_nodelist_mtx, "netgraph nodelist mutex", NULL,
3140 MTX_DEF);
3141 mtx_init(&ngq_mtx, "netgraph item list mutex", NULL,
3142 MTX_DEF);
3143 #endif
3144 ng_qzone = uma_zcreate("NetGraph items", sizeof(struct ng_item),
3145 NULL, NULL, NULL, NULL, UMA_ALIGN_CACHE, 0);
3146 uma_zone_set_max(ng_qzone, maxalloc);
3147 ng_qdzone = uma_zcreate("NetGraph data items", sizeof(struct ng_item),
3148 NULL, NULL, NULL, NULL, UMA_ALIGN_CACHE, 0);
3149 uma_zone_set_max(ng_qdzone, maxdata);
3150 netisr_register(NETISR_NETGRAPH, (netisr_t *)ngintr, NULL,
3151 NETISR_MPSAFE);
3152 break;
3153 case MOD_UNLOAD:
3154 /* You can't unload it because an interface may be using it. */
3155 error = EBUSY;
3156 break;
3157 default:
3158 error = EOPNOTSUPP;
3159 break;
3160 }
3161 return (error);
3162 }
3163
3164 static moduledata_t netgraph_mod = {
3165 "netgraph",
3166 ngb_mod_event,
3167 (NULL)
3168 };
3169 DECLARE_MODULE(netgraph, netgraph_mod, SI_SUB_NETGRAPH, SI_ORDER_MIDDLE);
3170 SYSCTL_NODE(_net, OID_AUTO, graph, CTLFLAG_RW, 0, "netgraph Family");
3171 SYSCTL_INT(_net_graph, OID_AUTO, abi_version, CTLFLAG_RD, 0, NG_ABI_VERSION,"");
3172 SYSCTL_INT(_net_graph, OID_AUTO, msg_version, CTLFLAG_RD, 0, NG_VERSION, "");
3173
3174 #ifdef NETGRAPH_DEBUG
3175 void
3176 dumphook (hook_p hook, char *file, int line)
3177 {
3178 printf("hook: name %s, %d refs, Last touched:\n",
3179 _NG_HOOK_NAME(hook), hook->hk_refs);
3180 printf(" Last active @ %s, line %d\n",
3181 hook->lastfile, hook->lastline);
3182 if (line) {
3183 printf(" problem discovered at file %s, line %d\n", file, line);
3184 }
3185 }
3186
3187 void
3188 dumpnode(node_p node, char *file, int line)
3189 {
3190 printf("node: ID [%x]: type '%s', %d hooks, flags 0x%x, %d refs, %s:\n",
3191 _NG_NODE_ID(node), node->nd_type->name,
3192 node->nd_numhooks, node->nd_flags,
3193 node->nd_refs, node->nd_name);
3194 printf(" Last active @ %s, line %d\n",
3195 node->lastfile, node->lastline);
3196 if (line) {
3197 printf(" problem discovered at file %s, line %d\n", file, line);
3198 }
3199 }
3200
3201 void
3202 dumpitem(item_p item, char *file, int line)
3203 {
3204 printf(" ACTIVE item, last used at %s, line %d",
3205 item->lastfile, item->lastline);
3206 switch(item->el_flags & NGQF_TYPE) {
3207 case NGQF_DATA:
3208 printf(" - [data]\n");
3209 break;
3210 case NGQF_MESG:
3211 printf(" - retaddr[%d]:\n", _NGI_RETADDR(item));
3212 break;
3213 case NGQF_FN:
3214 printf(" - fn@%p (%p, %p, %p, %d (%x))\n",
3215 _NGI_FN(item),
3216 _NGI_NODE(item),
3217 _NGI_HOOK(item),
3218 item->body.fn.fn_arg1,
3219 item->body.fn.fn_arg2,
3220 item->body.fn.fn_arg2);
3221 break;
3222 case NGQF_FN2:
3223 printf(" - fn2@%p (%p, %p, %p, %d (%x))\n",
3224 _NGI_FN2(item),
3225 _NGI_NODE(item),
3226 _NGI_HOOK(item),
3227 item->body.fn.fn_arg1,
3228 item->body.fn.fn_arg2,
3229 item->body.fn.fn_arg2);
3230 break;
3231 }
3232 if (line) {
3233 printf(" problem discovered at file %s, line %d\n", file, line);
3234 if (_NGI_NODE(item)) {
3235 printf("node %p ([%x])\n",
3236 _NGI_NODE(item), ng_node2ID(_NGI_NODE(item)));
3237 }
3238 }
3239 }
3240
3241 static void
3242 ng_dumpitems(void)
3243 {
3244 item_p item;
3245 int i = 1;
3246 TAILQ_FOREACH(item, &ng_itemlist, all) {
3247 printf("[%d] ", i++);
3248 dumpitem(item, NULL, 0);
3249 }
3250 }
3251
3252 static void
3253 ng_dumpnodes(void)
3254 {
3255 node_p node;
3256 int i = 1;
3257 mtx_lock(&ng_nodelist_mtx);
3258 SLIST_FOREACH(node, &ng_allnodes, nd_all) {
3259 printf("[%d] ", i++);
3260 dumpnode(node, NULL, 0);
3261 }
3262 mtx_unlock(&ng_nodelist_mtx);
3263 }
3264
3265 static void
3266 ng_dumphooks(void)
3267 {
3268 hook_p hook;
3269 int i = 1;
3270 mtx_lock(&ng_nodelist_mtx);
3271 SLIST_FOREACH(hook, &ng_allhooks, hk_all) {
3272 printf("[%d] ", i++);
3273 dumphook(hook, NULL, 0);
3274 }
3275 mtx_unlock(&ng_nodelist_mtx);
3276 }
3277
3278 static int
3279 sysctl_debug_ng_dump_items(SYSCTL_HANDLER_ARGS)
3280 {
3281 int error;
3282 int val;
3283 int i;
3284
3285 val = allocated;
3286 i = 1;
3287 error = sysctl_handle_int(oidp, &val, 0, req);
3288 if (error != 0 || req->newptr == NULL)
3289 return (error);
3290 if (val == 42) {
3291 ng_dumpitems();
3292 ng_dumpnodes();
3293 ng_dumphooks();
3294 }
3295 return (0);
3296 }
3297
3298 SYSCTL_PROC(_debug, OID_AUTO, ng_dump_items, CTLTYPE_INT | CTLFLAG_RW,
3299 0, sizeof(int), sysctl_debug_ng_dump_items, "I", "Number of allocated items");
3300 #endif /* NETGRAPH_DEBUG */
3301
3302
3303 /***********************************************************************
3304 * Worklist routines
3305 **********************************************************************/
3306 /* NETISR thread enters here */
3307 /*
3308 * Pick a node off the list of nodes with work,
3309 * try get an item to process off it.
3310 * If there are no more, remove the node from the list.
3311 */
3312 static void
3313 ngintr(void)
3314 {
3315 item_p item;
3316 node_p node = NULL;
3317
3318 for (;;) {
3319 NG_WORKLIST_LOCK();
3320 node = TAILQ_FIRST(&ng_worklist);
3321 if (!node) {
3322 NG_WORKLIST_UNLOCK();
3323 break;
3324 }
3325 node->nd_flags &= ~NGF_WORKQ;
3326 TAILQ_REMOVE(&ng_worklist, node, nd_work);
3327 NG_WORKLIST_UNLOCK();
3328 CTR3(KTR_NET, "%20s: node [%x] (%p) taken off worklist",
3329 __func__, node->nd_ID, node);
3330 /*
3331 * We have the node. We also take over the reference
3332 * that the list had on it.
3333 * Now process as much as you can, until it won't
3334 * let you have another item off the queue.
3335 * All this time, keep the reference
3336 * that lets us be sure that the node still exists.
3337 * Let the reference go at the last minute.
3338 */
3339 for (;;) {
3340 int rw;
3341
3342 NG_QUEUE_LOCK(&node->nd_input_queue);
3343 item = ng_dequeue(&node->nd_input_queue, &rw);
3344 if (item == NULL) {
3345 NG_QUEUE_UNLOCK(&node->nd_input_queue);
3346 break; /* go look for another node */
3347 } else {
3348 NG_QUEUE_UNLOCK(&node->nd_input_queue);
3349 NGI_GET_NODE(item, node); /* zaps stored node */
3350 ng_apply_item(node, item, rw);
3351 NG_NODE_UNREF(node);
3352 }
3353 }
3354 NG_NODE_UNREF(node);
3355 }
3356 }
3357
3358 static void
3359 ng_worklist_remove(node_p node)
3360 {
3361 mtx_assert(&node->nd_input_queue.q_mtx, MA_OWNED);
3362
3363 NG_WORKLIST_LOCK();
3364 if (node->nd_flags & NGF_WORKQ) {
3365 node->nd_flags &= ~NGF_WORKQ;
3366 TAILQ_REMOVE(&ng_worklist, node, nd_work);
3367 NG_WORKLIST_UNLOCK();
3368 NG_NODE_UNREF(node);
3369 CTR3(KTR_NET, "%20s: node [%x] (%p) removed from worklist",
3370 __func__, node->nd_ID, node);
3371 } else {
3372 NG_WORKLIST_UNLOCK();
3373 }
3374 }
3375
3376 /*
3377 * XXX
3378 * It's posible that a debugging NG_NODE_REF may need
3379 * to be outside the mutex zone
3380 */
3381 static void
3382 ng_setisr(node_p node)
3383 {
3384
3385 mtx_assert(&node->nd_input_queue.q_mtx, MA_OWNED);
3386
3387 if ((node->nd_flags & NGF_WORKQ) == 0) {
3388 /*
3389 * If we are not already on the work queue,
3390 * then put us on.
3391 */
3392 node->nd_flags |= NGF_WORKQ;
3393 NG_WORKLIST_LOCK();
3394 TAILQ_INSERT_TAIL(&ng_worklist, node, nd_work);
3395 NG_WORKLIST_UNLOCK();
3396 NG_NODE_REF(node); /* XXX fafe in mutex? */
3397 CTR3(KTR_NET, "%20s: node [%x] (%p) put on worklist", __func__,
3398 node->nd_ID, node);
3399 } else
3400 CTR3(KTR_NET, "%20s: node [%x] (%p) already on worklist",
3401 __func__, node->nd_ID, node);
3402 schednetisr(NETISR_NETGRAPH);
3403 }
3404
3405
3406 /***********************************************************************
3407 * Externally useable functions to set up a queue item ready for sending
3408 ***********************************************************************/
3409
3410 #ifdef NETGRAPH_DEBUG
3411 #define ITEM_DEBUG_CHECKS \
3412 do { \
3413 if (NGI_NODE(item) ) { \
3414 printf("item already has node"); \
3415 kdb_enter("has node"); \
3416 NGI_CLR_NODE(item); \
3417 } \
3418 if (NGI_HOOK(item) ) { \
3419 printf("item already has hook"); \
3420 kdb_enter("has hook"); \
3421 NGI_CLR_HOOK(item); \
3422 } \
3423 } while (0)
3424 #else
3425 #define ITEM_DEBUG_CHECKS
3426 #endif
3427
3428 /*
3429 * Put mbuf into the item.
3430 * Hook and node references will be removed when the item is dequeued.
3431 * (or equivalent)
3432 * (XXX) Unsafe because no reference held by peer on remote node.
3433 * remote node might go away in this timescale.
3434 * We know the hooks can't go away because that would require getting
3435 * a writer item on both nodes and we must have at least a reader
3436 * here to be able to do this.
3437 * Note that the hook loaded is the REMOTE hook.
3438 *
3439 * This is possibly in the critical path for new data.
3440 */
3441 item_p
3442 ng_package_data(struct mbuf *m, int flags)
3443 {
3444 item_p item;
3445
3446 if ((item = ng_alloc_item(NGQF_DATA, flags)) == NULL) {
3447 NG_FREE_M(m);
3448 return (NULL);
3449 }
3450 ITEM_DEBUG_CHECKS;
3451 item->el_flags |= NGQF_READER;
3452 NGI_M(item) = m;
3453 return (item);
3454 }
3455
3456 /*
3457 * Allocate a queue item and put items into it..
3458 * Evaluate the address as this will be needed to queue it and
3459 * to work out what some of the fields should be.
3460 * Hook and node references will be removed when the item is dequeued.
3461 * (or equivalent)
3462 */
3463 item_p
3464 ng_package_msg(struct ng_mesg *msg, int flags)
3465 {
3466 item_p item;
3467
3468 if ((item = ng_alloc_item(NGQF_MESG, flags)) == NULL) {
3469 NG_FREE_MSG(msg);
3470 return (NULL);
3471 }
3472 ITEM_DEBUG_CHECKS;
3473 /* Messages items count as writers unless explicitly exempted. */
3474 if (msg->header.cmd & NGM_READONLY)
3475 item->el_flags |= NGQF_READER;
3476 else
3477 item->el_flags |= NGQF_WRITER;
3478 /*
3479 * Set the current lasthook into the queue item
3480 */
3481 NGI_MSG(item) = msg;
3482 NGI_RETADDR(item) = 0;
3483 return (item);
3484 }
3485
3486
3487
3488 #define SET_RETADDR(item, here, retaddr) \
3489 do { /* Data or fn items don't have retaddrs */ \
3490 if ((item->el_flags & NGQF_TYPE) == NGQF_MESG) { \
3491 if (retaddr) { \
3492 NGI_RETADDR(item) = retaddr; \
3493 } else { \
3494 /* \
3495 * The old return address should be ok. \
3496 * If there isn't one, use the address \
3497 * here. \
3498 */ \
3499 if (NGI_RETADDR(item) == 0) { \
3500 NGI_RETADDR(item) \
3501 = ng_node2ID(here); \
3502 } \
3503 } \
3504 } \
3505 } while (0)
3506
3507 int
3508 ng_address_hook(node_p here, item_p item, hook_p hook, ng_ID_t retaddr)
3509 {
3510 hook_p peer;
3511 node_p peernode;
3512 ITEM_DEBUG_CHECKS;
3513 /*
3514 * Quick sanity check..
3515 * Since a hook holds a reference on it's node, once we know
3516 * that the peer is still connected (even if invalid,) we know
3517 * that the peer node is present, though maybe invalid.
3518 */
3519 if ((hook == NULL) ||
3520 NG_HOOK_NOT_VALID(hook) ||
3521 NG_HOOK_NOT_VALID(peer = NG_HOOK_PEER(hook)) ||
3522 NG_NODE_NOT_VALID(peernode = NG_PEER_NODE(hook))) {
3523 NG_FREE_ITEM(item);
3524 TRAP_ERROR();
3525 return (ENETDOWN);
3526 }
3527
3528 /*
3529 * Transfer our interest to the other (peer) end.
3530 */
3531 NG_HOOK_REF(peer);
3532 NG_NODE_REF(peernode);
3533 NGI_SET_HOOK(item, peer);
3534 NGI_SET_NODE(item, peernode);
3535 SET_RETADDR(item, here, retaddr);
3536 return (0);
3537 }
3538
3539 int
3540 ng_address_path(node_p here, item_p item, char *address, ng_ID_t retaddr)
3541 {
3542 node_p dest = NULL;
3543 hook_p hook = NULL;
3544 int error;
3545
3546 ITEM_DEBUG_CHECKS;
3547 /*
3548 * Note that ng_path2noderef increments the reference count
3549 * on the node for us if it finds one. So we don't have to.
3550 */
3551 error = ng_path2noderef(here, address, &dest, &hook);
3552 if (error) {
3553 NG_FREE_ITEM(item);
3554 return (error);
3555 }
3556 NGI_SET_NODE(item, dest);
3557 if ( hook) {
3558 NG_HOOK_REF(hook); /* don't let it go while on the queue */
3559 NGI_SET_HOOK(item, hook);
3560 }
3561 SET_RETADDR(item, here, retaddr);
3562 return (0);
3563 }
3564
3565 int
3566 ng_address_ID(node_p here, item_p item, ng_ID_t ID, ng_ID_t retaddr)
3567 {
3568 node_p dest;
3569
3570 ITEM_DEBUG_CHECKS;
3571 /*
3572 * Find the target node.
3573 */
3574 dest = ng_ID2noderef(ID); /* GETS REFERENCE! */
3575 if (dest == NULL) {
3576 NG_FREE_ITEM(item);
3577 TRAP_ERROR();
3578 return(EINVAL);
3579 }
3580 /* Fill out the contents */
3581 NGI_SET_NODE(item, dest);
3582 NGI_CLR_HOOK(item);
3583 SET_RETADDR(item, here, retaddr);
3584 return (0);
3585 }
3586
3587 /*
3588 * special case to send a message to self (e.g. destroy node)
3589 * Possibly indicate an arrival hook too.
3590 * Useful for removing that hook :-)
3591 */
3592 item_p
3593 ng_package_msg_self(node_p here, hook_p hook, struct ng_mesg *msg)
3594 {
3595 item_p item;
3596
3597 /*
3598 * Find the target node.
3599 * If there is a HOOK argument, then use that in preference
3600 * to the address.
3601 */
3602 if ((item = ng_alloc_item(NGQF_MESG, NG_NOFLAGS)) == NULL) {
3603 NG_FREE_MSG(msg);
3604 return (NULL);
3605 }
3606
3607 /* Fill out the contents */
3608 item->el_flags |= NGQF_WRITER;
3609 NG_NODE_REF(here);
3610 NGI_SET_NODE(item, here);
3611 if (hook) {
3612 NG_HOOK_REF(hook);
3613 NGI_SET_HOOK(item, hook);
3614 }
3615 NGI_MSG(item) = msg;
3616 NGI_RETADDR(item) = ng_node2ID(here);
3617 return (item);
3618 }
3619
3620 /*
3621 * Send ng_item_fn function call to the specified node.
3622 */
3623
3624 int
3625 ng_send_fn(node_p node, hook_p hook, ng_item_fn *fn, void * arg1, int arg2)
3626 {
3627
3628 return ng_send_fn1(node, hook, fn, arg1, arg2, NG_NOFLAGS);
3629 }
3630
3631 int
3632 ng_send_fn1(node_p node, hook_p hook, ng_item_fn *fn, void * arg1, int arg2,
3633 int flags)
3634 {
3635 item_p item;
3636
3637 if ((item = ng_alloc_item(NGQF_FN, flags)) == NULL) {
3638 return (ENOMEM);
3639 }
3640 item->el_flags |= NGQF_WRITER;
3641 NG_NODE_REF(node); /* and one for the item */
3642 NGI_SET_NODE(item, node);
3643 if (hook) {
3644 NG_HOOK_REF(hook);
3645 NGI_SET_HOOK(item, hook);
3646 }
3647 NGI_FN(item) = fn;
3648 NGI_ARG1(item) = arg1;
3649 NGI_ARG2(item) = arg2;
3650 return(ng_snd_item(item, flags));
3651 }
3652
3653 /*
3654 * Send ng_item_fn2 function call to the specified node.
3655 *
3656 * If an optional pitem parameter is supplied, its apply
3657 * callback will be copied to the new item. If also NG_REUSE_ITEM
3658 * flag is set, no new item will be allocated, but pitem will
3659 * be used.
3660 */
3661 int
3662 ng_send_fn2(node_p node, hook_p hook, item_p pitem, ng_item_fn2 *fn, void *arg1,
3663 int arg2, int flags)
3664 {
3665 item_p item;
3666
3667 KASSERT((pitem != NULL || (flags & NG_REUSE_ITEM) == 0),
3668 ("%s: NG_REUSE_ITEM but no pitem", __func__));
3669
3670 /*
3671 * Allocate a new item if no supplied or
3672 * if we can't use supplied one.
3673 */
3674 if (pitem == NULL || (flags & NG_REUSE_ITEM) == 0) {
3675 if ((item = ng_alloc_item(NGQF_FN2, flags)) == NULL)
3676 return (ENOMEM);
3677 if (pitem != NULL)
3678 item->apply = pitem->apply;
3679 } else {
3680 if ((item = ng_realloc_item(pitem, NGQF_FN2, flags)) == NULL)
3681 return (ENOMEM);
3682 }
3683
3684 item->el_flags = (item->el_flags & ~NGQF_RW) | NGQF_WRITER;
3685 NG_NODE_REF(node); /* and one for the item */
3686 NGI_SET_NODE(item, node);
3687 if (hook) {
3688 NG_HOOK_REF(hook);
3689 NGI_SET_HOOK(item, hook);
3690 }
3691 NGI_FN2(item) = fn;
3692 NGI_ARG1(item) = arg1;
3693 NGI_ARG2(item) = arg2;
3694 return(ng_snd_item(item, flags));
3695 }
3696
3697 /*
3698 * Official timeout routines for Netgraph nodes.
3699 */
3700 static void
3701 ng_callout_trampoline(void *arg)
3702 {
3703 item_p item = arg;
3704
3705 ng_snd_item(item, 0);
3706 }
3707
3708
3709 int
3710 ng_callout(struct callout *c, node_p node, hook_p hook, int ticks,
3711 ng_item_fn *fn, void * arg1, int arg2)
3712 {
3713 item_p item, oitem;
3714
3715 if ((item = ng_alloc_item(NGQF_FN, NG_NOFLAGS)) == NULL)
3716 return (ENOMEM);
3717
3718 item->el_flags |= NGQF_WRITER;
3719 NG_NODE_REF(node); /* and one for the item */
3720 NGI_SET_NODE(item, node);
3721 if (hook) {
3722 NG_HOOK_REF(hook);
3723 NGI_SET_HOOK(item, hook);
3724 }
3725 NGI_FN(item) = fn;
3726 NGI_ARG1(item) = arg1;
3727 NGI_ARG2(item) = arg2;
3728 oitem = c->c_arg;
3729 if (callout_reset(c, ticks, &ng_callout_trampoline, item) == 1 &&
3730 oitem != NULL)
3731 NG_FREE_ITEM(oitem);
3732 return (0);
3733 }
3734
3735 /* A special modified version of untimeout() */
3736 int
3737 ng_uncallout(struct callout *c, node_p node)
3738 {
3739 item_p item;
3740 int rval;
3741
3742 KASSERT(c != NULL, ("ng_uncallout: NULL callout"));
3743 KASSERT(node != NULL, ("ng_uncallout: NULL node"));
3744
3745 rval = callout_stop(c);
3746 item = c->c_arg;
3747 /* Do an extra check */
3748 if ((rval > 0) && (c->c_func == &ng_callout_trampoline) &&
3749 (NGI_NODE(item) == node)) {
3750 /*
3751 * We successfully removed it from the queue before it ran
3752 * So now we need to unreference everything that was
3753 * given extra references. (NG_FREE_ITEM does this).
3754 */
3755 NG_FREE_ITEM(item);
3756 }
3757 c->c_arg = NULL;
3758
3759 return (rval);
3760 }
3761
3762 /*
3763 * Set the address, if none given, give the node here.
3764 */
3765 void
3766 ng_replace_retaddr(node_p here, item_p item, ng_ID_t retaddr)
3767 {
3768 if (retaddr) {
3769 NGI_RETADDR(item) = retaddr;
3770 } else {
3771 /*
3772 * The old return address should be ok.
3773 * If there isn't one, use the address here.
3774 */
3775 NGI_RETADDR(item) = ng_node2ID(here);
3776 }
3777 }
3778
3779 #define TESTING
3780 #ifdef TESTING
3781 /* just test all the macros */
3782 void
3783 ng_macro_test(item_p item);
3784 void
3785 ng_macro_test(item_p item)
3786 {
3787 node_p node = NULL;
3788 hook_p hook = NULL;
3789 struct mbuf *m;
3790 struct ng_mesg *msg;
3791 ng_ID_t retaddr;
3792 int error;
3793
3794 NGI_GET_M(item, m);
3795 NGI_GET_MSG(item, msg);
3796 retaddr = NGI_RETADDR(item);
3797 NG_SEND_DATA(error, hook, m, NULL);
3798 NG_SEND_DATA_ONLY(error, hook, m);
3799 NG_FWD_NEW_DATA(error, item, hook, m);
3800 NG_FWD_ITEM_HOOK(error, item, hook);
3801 NG_SEND_MSG_HOOK(error, node, msg, hook, retaddr);
3802 NG_SEND_MSG_ID(error, node, msg, retaddr, retaddr);
3803 NG_SEND_MSG_PATH(error, node, msg, ".:", retaddr);
3804 NG_FWD_MSG_HOOK(error, node, item, hook, retaddr);
3805 }
3806 #endif /* TESTING */
3807
Cache object: bca3489d47c07158bc3c5fd6b24b0b9d
|