2 * Copyright (c) 1996-1999 Whistle Communications, Inc.
5 * Subject to the following obligations and disclaimer of warranty, use and
6 * redistribution of this software, in source or object code forms, with or
7 * without modifications are expressly permitted by Whistle Communications;
8 * provided, however, that:
9 * 1. Any and all reproductions of the source or object code must include the
10 * copyright notice above and the following disclaimer of warranties; and
11 * 2. No rights are granted, in any manner or form, to use Whistle
12 * Communications, Inc. trademarks, including the mark "WHISTLE
13 * COMMUNICATIONS" on advertising, endorsements, or otherwise except as
14 * such appears in the above copyright notice or in the software.
16 * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
17 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
18 * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
19 * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
21 * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
22 * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
23 * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
24 * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
25 * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
26 * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
27 * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
28 * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
34 * Authors: Julian Elischer <julian@freebsd.org>
35 * Archie Cobbs <archie@freebsd.org>
38 * $Whistle: ng_base.c,v 1.39 1999/01/28 23:54:53 julian Exp $
42 * This file implements the base netgraph code.
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/ctype.h>
50 #include <sys/kernel.h>
51 #include <sys/kthread.h>
53 #include <sys/limits.h>
55 #include <sys/malloc.h>
58 #include <sys/epoch.h>
59 #include <sys/queue.h>
60 #include <sys/refcount.h>
61 #include <sys/rwlock.h>
63 #include <sys/sysctl.h>
64 #include <sys/syslog.h>
65 #include <sys/unistd.h>
66 #include <machine/cpu.h>
69 #include <net/netisr.h>
72 #include <netgraph/ng_message.h>
73 #include <netgraph/netgraph.h>
74 #include <netgraph/ng_parse.h>
76 MODULE_VERSION(netgraph, NG_ABI_VERSION);
78 /* Mutex to protect topology events. */
79 static struct rwlock ng_topo_lock;
80 #define TOPOLOGY_RLOCK() rw_rlock(&ng_topo_lock)
81 #define TOPOLOGY_RUNLOCK() rw_runlock(&ng_topo_lock)
82 #define TOPOLOGY_WLOCK() rw_wlock(&ng_topo_lock)
83 #define TOPOLOGY_WUNLOCK() rw_wunlock(&ng_topo_lock)
84 #define TOPOLOGY_NOTOWNED() rw_assert(&ng_topo_lock, RA_UNLOCKED)
87 static struct mtx ng_nodelist_mtx; /* protects global node/hook lists */
88 static struct mtx ngq_mtx; /* protects the queue item list */
90 static SLIST_HEAD(, ng_node) ng_allnodes;
91 static LIST_HEAD(, ng_node) ng_freenodes; /* in debug, we never free() them */
92 static SLIST_HEAD(, ng_hook) ng_allhooks;
93 static LIST_HEAD(, ng_hook) ng_freehooks; /* in debug, we never free() them */
95 static void ng_dumpitems(void);
96 static void ng_dumpnodes(void);
97 static void ng_dumphooks(void);
99 #endif /* NETGRAPH_DEBUG */
101 * DEAD versions of the structures.
102 * In order to avoid races, it is sometimes necessary to point
103 * at SOMETHING even though theoretically, the current entity is
104 * INVALID. Use these to avoid these races.
106 struct ng_type ng_deadtype = {
110 NULL, /* constructor */
117 NULL, /* disconnect */
121 struct ng_node ng_deadnode = {
128 LIST_HEAD_INITIALIZER(ng_deadnode.nd_hooks),
129 {}, /* all_nodes list entry */
130 {}, /* id hashtable list entry */
133 {}, /* should never use! (should hang) */
134 {}, /* workqueue entry */
135 STAILQ_HEAD_INITIALIZER(ng_deadnode.nd_input_queue.queue),
139 #ifdef NETGRAPH_DEBUG
144 #endif /* NETGRAPH_DEBUG */
147 struct ng_hook ng_deadhook = {
150 HK_INVALID | HK_DEAD,
151 0, /* undefined data link type */
152 &ng_deadhook, /* Peer is self */
153 &ng_deadnode, /* attached to deadnode */
155 NULL, /* override rcvmsg() */
156 NULL, /* override rcvdata() */
157 1, /* refs always >= 1 */
158 #ifdef NETGRAPH_DEBUG
163 #endif /* NETGRAPH_DEBUG */
167 * END DEAD STRUCTURES
169 /* List nodes with unallocated work */
170 static STAILQ_HEAD(, ng_node) ng_worklist = STAILQ_HEAD_INITIALIZER(ng_worklist);
171 static struct mtx ng_worklist_mtx; /* MUST LOCK NODE FIRST */
173 /* List of installed types */
174 static LIST_HEAD(, ng_type) ng_typelist;
175 static struct rwlock ng_typelist_lock;
176 #define TYPELIST_RLOCK() rw_rlock(&ng_typelist_lock)
177 #define TYPELIST_RUNLOCK() rw_runlock(&ng_typelist_lock)
178 #define TYPELIST_WLOCK() rw_wlock(&ng_typelist_lock)
179 #define TYPELIST_WUNLOCK() rw_wunlock(&ng_typelist_lock)
181 /* Hash related definitions. */
182 LIST_HEAD(nodehash, ng_node);
183 VNET_DEFINE_STATIC(struct nodehash *, ng_ID_hash);
184 VNET_DEFINE_STATIC(u_long, ng_ID_hmask);
185 VNET_DEFINE_STATIC(u_long, ng_nodes);
186 VNET_DEFINE_STATIC(struct nodehash *, ng_name_hash);
187 VNET_DEFINE_STATIC(u_long, ng_name_hmask);
188 VNET_DEFINE_STATIC(u_long, ng_named_nodes);
189 #define V_ng_ID_hash VNET(ng_ID_hash)
190 #define V_ng_ID_hmask VNET(ng_ID_hmask)
191 #define V_ng_nodes VNET(ng_nodes)
192 #define V_ng_name_hash VNET(ng_name_hash)
193 #define V_ng_name_hmask VNET(ng_name_hmask)
194 #define V_ng_named_nodes VNET(ng_named_nodes)
196 static struct rwlock ng_idhash_lock;
197 #define IDHASH_RLOCK() rw_rlock(&ng_idhash_lock)
198 #define IDHASH_RUNLOCK() rw_runlock(&ng_idhash_lock)
199 #define IDHASH_WLOCK() rw_wlock(&ng_idhash_lock)
200 #define IDHASH_WUNLOCK() rw_wunlock(&ng_idhash_lock)
202 /* Method to find a node.. used twice so do it here */
203 #define NG_IDHASH_FN(ID) ((ID) % (V_ng_ID_hmask + 1))
204 #define NG_IDHASH_FIND(ID, node) \
206 rw_assert(&ng_idhash_lock, RA_LOCKED); \
207 LIST_FOREACH(node, &V_ng_ID_hash[NG_IDHASH_FN(ID)], \
209 if (NG_NODE_IS_VALID(node) \
210 && (NG_NODE_ID(node) == ID)) { \
216 static struct rwlock ng_namehash_lock;
217 #define NAMEHASH_RLOCK() rw_rlock(&ng_namehash_lock)
218 #define NAMEHASH_RUNLOCK() rw_runlock(&ng_namehash_lock)
219 #define NAMEHASH_WLOCK() rw_wlock(&ng_namehash_lock)
220 #define NAMEHASH_WUNLOCK() rw_wunlock(&ng_namehash_lock)
222 /* Internal functions */
223 static int ng_add_hook(node_p node, const char *name, hook_p * hookp);
224 static int ng_generic_msg(node_p here, item_p item, hook_p lasthook);
225 static ng_ID_t ng_decodeidname(const char *name);
226 static int ngb_mod_event(module_t mod, int event, void *data);
227 static void ng_worklist_add(node_p node);
228 static void ngthread(void *);
229 static int ng_apply_item(node_p node, item_p item, int rw);
230 static void ng_flush_input_queue(node_p node);
231 static node_p ng_ID2noderef(ng_ID_t ID);
232 static int ng_con_nodes(item_p item, node_p node, const char *name,
233 node_p node2, const char *name2);
234 static int ng_con_part2(node_p node, item_p item, hook_p hook);
235 static int ng_con_part3(node_p node, item_p item, hook_p hook);
236 static int ng_mkpeer(node_p node, const char *name, const char *name2,
238 static void ng_name_rehash(void);
239 static void ng_ID_rehash(void);
241 /* Imported, these used to be externally visible, some may go back. */
242 void ng_destroy_hook(hook_p hook);
243 int ng_path2noderef(node_p here, const char *path,
244 node_p *dest, hook_p *lasthook);
245 int ng_make_node(const char *type, node_p *nodepp);
246 int ng_path_parse(char *addr, char **node, char **path, char **hook);
247 void ng_rmnode(node_p node, hook_p dummy1, void *dummy2, int dummy3);
248 void ng_unname(node_p node);
250 /* Our own netgraph malloc type */
251 MALLOC_DEFINE(M_NETGRAPH, "netgraph", "netgraph structures and ctrl messages");
252 MALLOC_DEFINE(M_NETGRAPH_MSG, "netgraph_msg", "netgraph name storage");
253 static MALLOC_DEFINE(M_NETGRAPH_HOOK, "netgraph_hook",
254 "netgraph hook structures");
255 static MALLOC_DEFINE(M_NETGRAPH_NODE, "netgraph_node",
256 "netgraph node structures");
257 static MALLOC_DEFINE(M_NETGRAPH_ITEM, "netgraph_item",
258 "netgraph item structures");
260 /* Should not be visible outside this file */
262 #define _NG_ALLOC_HOOK(hook) \
263 hook = malloc(sizeof(*hook), M_NETGRAPH_HOOK, M_NOWAIT | M_ZERO)
264 #define _NG_ALLOC_NODE(node) \
265 node = malloc(sizeof(*node), M_NETGRAPH_NODE, M_NOWAIT | M_ZERO)
267 #define NG_QUEUE_LOCK_INIT(n) \
268 mtx_init(&(n)->q_mtx, "ng_node", NULL, MTX_DEF)
269 #define NG_QUEUE_LOCK(n) \
270 mtx_lock(&(n)->q_mtx)
271 #define NG_QUEUE_UNLOCK(n) \
272 mtx_unlock(&(n)->q_mtx)
273 #define NG_WORKLIST_LOCK_INIT() \
274 mtx_init(&ng_worklist_mtx, "ng_worklist", NULL, MTX_DEF)
275 #define NG_WORKLIST_LOCK() \
276 mtx_lock(&ng_worklist_mtx)
277 #define NG_WORKLIST_UNLOCK() \
278 mtx_unlock(&ng_worklist_mtx)
279 #define NG_WORKLIST_SLEEP() \
280 mtx_sleep(&ng_worklist, &ng_worklist_mtx, PI_NET, "sleep", 0)
281 #define NG_WORKLIST_WAKEUP() \
282 wakeup_one(&ng_worklist)
284 #ifdef NETGRAPH_DEBUG /*----------------------------------------------*/
287 * In an attempt to help track reference count screwups
288 * we do not free objects back to the malloc system, but keep them
289 * in a local cache where we can examine them and keep information safely
290 * after they have been freed.
291 * We use this scheme for nodes and hooks, and to some extent for items.
293 static __inline hook_p
297 SLIST_ENTRY(ng_hook) temp;
298 mtx_lock(&ng_nodelist_mtx);
299 hook = LIST_FIRST(&ng_freehooks);
301 LIST_REMOVE(hook, hk_hooks);
302 bcopy(&hook->hk_all, &temp, sizeof(temp));
303 bzero(hook, sizeof(struct ng_hook));
304 bcopy(&temp, &hook->hk_all, sizeof(temp));
305 mtx_unlock(&ng_nodelist_mtx);
306 hook->hk_magic = HK_MAGIC;
308 mtx_unlock(&ng_nodelist_mtx);
309 _NG_ALLOC_HOOK(hook);
311 hook->hk_magic = HK_MAGIC;
312 mtx_lock(&ng_nodelist_mtx);
313 SLIST_INSERT_HEAD(&ng_allhooks, hook, hk_all);
314 mtx_unlock(&ng_nodelist_mtx);
320 static __inline node_p
324 SLIST_ENTRY(ng_node) temp;
325 mtx_lock(&ng_nodelist_mtx);
326 node = LIST_FIRST(&ng_freenodes);
328 LIST_REMOVE(node, nd_nodes);
329 bcopy(&node->nd_all, &temp, sizeof(temp));
330 bzero(node, sizeof(struct ng_node));
331 bcopy(&temp, &node->nd_all, sizeof(temp));
332 mtx_unlock(&ng_nodelist_mtx);
333 node->nd_magic = ND_MAGIC;
335 mtx_unlock(&ng_nodelist_mtx);
336 _NG_ALLOC_NODE(node);
338 node->nd_magic = ND_MAGIC;
339 mtx_lock(&ng_nodelist_mtx);
340 SLIST_INSERT_HEAD(&ng_allnodes, node, nd_all);
341 mtx_unlock(&ng_nodelist_mtx);
347 #define NG_ALLOC_HOOK(hook) do { (hook) = ng_alloc_hook(); } while (0)
348 #define NG_ALLOC_NODE(node) do { (node) = ng_alloc_node(); } while (0)
350 #define NG_FREE_HOOK(hook) \
352 mtx_lock(&ng_nodelist_mtx); \
353 LIST_INSERT_HEAD(&ng_freehooks, hook, hk_hooks); \
354 hook->hk_magic = 0; \
355 mtx_unlock(&ng_nodelist_mtx); \
358 #define NG_FREE_NODE(node) \
360 mtx_lock(&ng_nodelist_mtx); \
361 LIST_INSERT_HEAD(&ng_freenodes, node, nd_nodes); \
362 node->nd_magic = 0; \
363 mtx_unlock(&ng_nodelist_mtx); \
366 #else /* NETGRAPH_DEBUG */ /*----------------------------------------------*/
368 #define NG_ALLOC_HOOK(hook) _NG_ALLOC_HOOK(hook)
369 #define NG_ALLOC_NODE(node) _NG_ALLOC_NODE(node)
371 #define NG_FREE_HOOK(hook) do { free((hook), M_NETGRAPH_HOOK); } while (0)
372 #define NG_FREE_NODE(node) do { free((node), M_NETGRAPH_NODE); } while (0)
374 #endif /* NETGRAPH_DEBUG */ /*----------------------------------------------*/
376 /* Set this to kdb_enter("X") to catch all errors as they occur */
381 VNET_DEFINE_STATIC(ng_ID_t, nextID) = 1;
382 #define V_nextID VNET(nextID)
385 #define CHECK_DATA_MBUF(m) do { \
390 for (total = 0, n = (m); n != NULL; n = n->m_next) { \
392 if (n->m_nextpkt != NULL) \
393 panic("%s: m_nextpkt", __func__); \
396 if ((m)->m_pkthdr.len != total) { \
397 panic("%s: %d != %d", \
398 __func__, (m)->m_pkthdr.len, total); \
402 #define CHECK_DATA_MBUF(m)
405 #define ERROUT(x) do { error = (x); goto done; } while (0)
407 /************************************************************************
408 Parse type definitions for generic messages
409 ************************************************************************/
411 /* Handy structure parse type defining macro */
412 #define DEFINE_PARSE_STRUCT_TYPE(lo, up, args) \
413 static const struct ng_parse_struct_field \
414 ng_ ## lo ## _type_fields[] = NG_GENERIC_ ## up ## _INFO args; \
415 static const struct ng_parse_type ng_generic_ ## lo ## _type = { \
416 &ng_parse_struct_type, \
417 &ng_ ## lo ## _type_fields \
420 DEFINE_PARSE_STRUCT_TYPE(mkpeer, MKPEER, ());
421 DEFINE_PARSE_STRUCT_TYPE(connect, CONNECT, ());
422 DEFINE_PARSE_STRUCT_TYPE(name, NAME, ());
423 DEFINE_PARSE_STRUCT_TYPE(rmhook, RMHOOK, ());
424 DEFINE_PARSE_STRUCT_TYPE(nodeinfo, NODEINFO, ());
425 DEFINE_PARSE_STRUCT_TYPE(typeinfo, TYPEINFO, ());
426 DEFINE_PARSE_STRUCT_TYPE(linkinfo, LINKINFO, (&ng_generic_nodeinfo_type));
428 /* Get length of an array when the length is stored as a 32 bit
429 value immediately preceding the array -- as with struct namelist
430 and struct typelist. */
432 ng_generic_list_getLength(const struct ng_parse_type *type,
433 const u_char *start, const u_char *buf)
435 return *((const u_int32_t *)(buf - 4));
438 /* Get length of the array of struct linkinfo inside a struct hooklist */
440 ng_generic_linkinfo_getLength(const struct ng_parse_type *type,
441 const u_char *start, const u_char *buf)
443 const struct hooklist *hl = (const struct hooklist *)start;
445 return hl->nodeinfo.hooks;
448 /* Array type for a variable length array of struct namelist */
449 static const struct ng_parse_array_info ng_nodeinfoarray_type_info = {
450 &ng_generic_nodeinfo_type,
451 &ng_generic_list_getLength
453 static const struct ng_parse_type ng_generic_nodeinfoarray_type = {
454 &ng_parse_array_type,
455 &ng_nodeinfoarray_type_info
458 /* Array type for a variable length array of struct typelist */
459 static const struct ng_parse_array_info ng_typeinfoarray_type_info = {
460 &ng_generic_typeinfo_type,
461 &ng_generic_list_getLength
463 static const struct ng_parse_type ng_generic_typeinfoarray_type = {
464 &ng_parse_array_type,
465 &ng_typeinfoarray_type_info
468 /* Array type for array of struct linkinfo in struct hooklist */
469 static const struct ng_parse_array_info ng_generic_linkinfo_array_type_info = {
470 &ng_generic_linkinfo_type,
471 &ng_generic_linkinfo_getLength
473 static const struct ng_parse_type ng_generic_linkinfo_array_type = {
474 &ng_parse_array_type,
475 &ng_generic_linkinfo_array_type_info
478 DEFINE_PARSE_STRUCT_TYPE(typelist, TYPELIST, (&ng_generic_typeinfoarray_type));
479 DEFINE_PARSE_STRUCT_TYPE(hooklist, HOOKLIST,
480 (&ng_generic_nodeinfo_type, &ng_generic_linkinfo_array_type));
481 DEFINE_PARSE_STRUCT_TYPE(listnodes, LISTNODES,
482 (&ng_generic_nodeinfoarray_type));
484 /* List of commands and how to convert arguments to/from ASCII */
485 static const struct ng_cmdlist ng_generic_cmds[] = {
497 &ng_generic_mkpeer_type,
504 &ng_generic_connect_type,
511 &ng_generic_name_type,
518 &ng_generic_rmhook_type,
526 &ng_generic_nodeinfo_type
533 &ng_generic_hooklist_type
540 &ng_generic_listnodes_type /* same as NGM_LISTNODES */
547 &ng_generic_listnodes_type
554 &ng_generic_typelist_type
561 &ng_parse_string_type
568 &ng_parse_string_type
574 &ng_parse_ng_mesg_type,
575 &ng_parse_ng_mesg_type
581 &ng_parse_ng_mesg_type,
582 &ng_parse_ng_mesg_type
587 /************************************************************************
589 ************************************************************************/
592 * Instantiate a node of the requested type
595 ng_make_node(const char *typename, node_p *nodepp)
597 struct ng_type *type;
600 /* Check that the type makes sense */
601 if (typename == NULL) {
606 /* Locate the node type. If we fail we return. Do not try to load
609 if ((type = ng_findtype(typename)) == NULL)
613 * If we have a constructor, then make the node and
614 * call the constructor to do type specific initialisation.
616 if (type->constructor != NULL) {
617 if ((error = ng_make_node_common(type, nodepp)) == 0) {
618 if ((error = ((*type->constructor)(*nodepp))) != 0) {
619 NG_NODE_UNREF(*nodepp);
624 * Node has no constructor. We cannot ask for one
625 * to be made. It must be brought into existence by
626 * some external agency. The external agency should
627 * call ng_make_node_common() directly to get the
628 * netgraph part initialised.
637 * Generic node creation. Called by node initialisation for externally
638 * instantiated nodes (e.g. hardware, sockets, etc ).
639 * The returned node has a reference count of 1.
642 ng_make_node_common(struct ng_type *type, node_p *nodepp)
646 /* Require the node type to have been already installed */
647 if (ng_findtype(type->name) == NULL) {
652 /* Make a node and try attach it to the type */
658 node->nd_type = type;
660 node->nd_vnet = curvnet;
662 NG_NODE_REF(node); /* note reference */
665 NG_QUEUE_LOCK_INIT(&node->nd_input_queue);
666 STAILQ_INIT(&node->nd_input_queue.queue);
667 node->nd_input_queue.q_flags = 0;
669 /* Initialize hook list for new node */
670 LIST_INIT(&node->nd_hooks);
672 /* Get an ID and put us in the hash chain. */
674 for (;;) { /* wrap protection, even if silly */
676 node->nd_ID = V_nextID++; /* 137/sec for 1 year before wrap */
678 /* Is there a problem with the new number? */
679 NG_IDHASH_FIND(node->nd_ID, node2); /* already taken? */
680 if ((node->nd_ID != 0) && (node2 == NULL)) {
685 if (V_ng_nodes * 2 > V_ng_ID_hmask)
687 LIST_INSERT_HEAD(&V_ng_ID_hash[NG_IDHASH_FN(node->nd_ID)], node,
697 * Forceably start the shutdown process on a node. Either call
698 * its shutdown method, or do the default shutdown if there is
699 * no type-specific method.
701 * We can only be called from a shutdown message, so we know we have
702 * a writer lock, and therefore exclusive access. It also means
703 * that we should not be on the work queue, but we check anyhow.
705 * Persistent node types must have a type-specific method which
706 * allocates a new node in which case, this one is irretrievably going away,
707 * or cleans up anything it needs, and just makes the node valid again,
708 * in which case we allow the node to survive.
710 * XXX We need to think of how to tell a persistent node that we
711 * REALLY need to go away because the hardware has gone or we
712 * are rebooting.... etc.
715 ng_rmnode(node_p node, hook_p dummy1, void *dummy2, int dummy3)
719 /* Check if it's already shutting down */
720 if ((node->nd_flags & NGF_CLOSING) != 0)
723 if (node == &ng_deadnode) {
724 printf ("shutdown called on deadnode\n");
728 /* Add an extra reference so it doesn't go away during this */
732 * Mark it invalid so any newcomers know not to try use it
733 * Also add our own mark so we can't recurse
734 * note that NGF_INVALID does not do this as it's also set during
737 node->nd_flags |= NGF_INVALID|NGF_CLOSING;
739 /* If node has its pre-shutdown method, then call it first*/
740 if (node->nd_type && node->nd_type->close)
741 (*node->nd_type->close)(node);
743 /* Notify all remaining connected nodes to disconnect */
744 while ((hook = LIST_FIRST(&node->nd_hooks)) != NULL)
745 ng_destroy_hook(hook);
748 * Drain the input queue forceably.
749 * it has no hooks so what's it going to do, bleed on someone?
750 * Theoretically we came here from a queue entry that was added
751 * Just before the queue was closed, so it should be empty anyway.
752 * Also removes us from worklist if needed.
754 ng_flush_input_queue(node);
756 /* Ask the type if it has anything to do in this case */
757 if (node->nd_type && node->nd_type->shutdown) {
758 (*node->nd_type->shutdown)(node);
759 if (NG_NODE_IS_VALID(node)) {
761 * Well, blow me down if the node code hasn't declared
762 * that it doesn't want to die.
763 * Presumably it is a persistent node.
764 * If we REALLY want it to go away,
765 * e.g. hardware going away,
766 * Our caller should set NGF_REALLY_DIE in nd_flags.
768 node->nd_flags &= ~(NGF_INVALID|NGF_CLOSING);
769 NG_NODE_UNREF(node); /* Assume they still have theirs */
772 } else { /* do the default thing */
776 ng_unname(node); /* basically a NOP these days */
779 * Remove extra reference, possibly the last
780 * Possible other holders of references may include
781 * timeout callouts, but theoretically the node's supposed to
782 * have cancelled them. Possibly hardware dependencies may
783 * force a driver to 'linger' with a reference.
789 * Remove a reference to the node, possibly the last.
790 * deadnode always acts as it it were the last.
793 ng_unref_node(node_p node)
796 if (node == &ng_deadnode)
799 CURVNET_SET(node->nd_vnet);
801 if (refcount_release(&node->nd_refs)) { /* we were the last */
803 node->nd_type->refs--; /* XXX maybe should get types lock? */
805 if (NG_NODE_HAS_NAME(node)) {
807 LIST_REMOVE(node, nd_nodes);
813 LIST_REMOVE(node, nd_idnodes);
816 mtx_destroy(&node->nd_input_queue.q_mtx);
822 /************************************************************************
824 ************************************************************************/
826 ng_ID2noderef(ng_ID_t ID)
831 NG_IDHASH_FIND(ID, node);
839 ng_node2ID(node_p node)
841 return (node ? NG_NODE_ID(node) : 0);
844 /************************************************************************
846 ************************************************************************/
849 * Assign a node a name.
852 ng_name_node(node_p node, const char *name)
858 /* Check the name is valid */
859 for (i = 0; i < NG_NODESIZ; i++) {
860 if (name[i] == '\0' || name[i] == '.' || name[i] == ':')
863 if (i == 0 || name[i] != '\0') {
867 if (ng_decodeidname(name) != 0) { /* valid IDs not allowed here */
873 if (V_ng_named_nodes * 2 > V_ng_name_hmask)
876 hash = hash32_str(name, HASHINIT) & V_ng_name_hmask;
877 /* Check the name isn't already being used. */
878 LIST_FOREACH(node2, &V_ng_name_hash[hash], nd_nodes)
879 if (NG_NODE_IS_VALID(node2) &&
880 (strcmp(NG_NODE_NAME(node2), name) == 0)) {
885 if (NG_NODE_HAS_NAME(node))
886 LIST_REMOVE(node, nd_nodes);
890 strlcpy(NG_NODE_NAME(node), name, NG_NODESIZ);
891 /* Update name hash. */
892 LIST_INSERT_HEAD(&V_ng_name_hash[hash], node, nd_nodes);
899 * Find a node by absolute name. The name should NOT end with ':'
900 * The name "." means "this node" and "[xxx]" means "the node
901 * with ID (ie, at address) xxx".
903 * Returns the node if found, else NULL.
904 * Eventually should add something faster than a sequential search.
905 * Note it acquires a reference on the node so you can be sure it's still
909 ng_name2noderef(node_p here, const char *name)
915 /* "." means "this node" */
916 if (strcmp(name, ".") == 0) {
921 /* Check for name-by-ID */
922 if ((temp = ng_decodeidname(name)) != 0) {
923 return (ng_ID2noderef(temp));
926 /* Find node by name. */
927 hash = hash32_str(name, HASHINIT) & V_ng_name_hmask;
929 LIST_FOREACH(node, &V_ng_name_hash[hash], nd_nodes)
930 if (NG_NODE_IS_VALID(node) &&
931 (strcmp(NG_NODE_NAME(node), name) == 0)) {
941 * Decode an ID name, eg. "[f03034de]". Returns 0 if the
942 * string is not valid, otherwise returns the value.
945 ng_decodeidname(const char *name)
947 const int len = strlen(name);
951 /* Check for proper length, brackets, no leading junk */
952 if ((len < 3) || (name[0] != '[') || (name[len - 1] != ']') ||
953 (!isxdigit(name[1])))
957 val = strtoul(name + 1, &eptr, 16);
958 if ((eptr - name != len - 1) || (val == ULONG_MAX) || (val == 0))
961 return ((ng_ID_t)val);
965 * Remove a name from a node. This should only be called
966 * when shutting down and removing the node.
969 ng_unname(node_p node)
974 * Allocate a bigger name hash.
979 struct nodehash *new;
985 new = hashinit_flags((V_ng_name_hmask + 1) * 2, M_NETGRAPH_NODE, &hmask,
990 for (i = 0; i <= V_ng_name_hmask; i++)
991 LIST_FOREACH_SAFE(node, &V_ng_name_hash[i], nd_nodes, node2) {
993 LIST_REMOVE(node, nd_nodes);
995 hash = hash32_str(NG_NODE_NAME(node), HASHINIT) & hmask;
996 LIST_INSERT_HEAD(&new[hash], node, nd_nodes);
999 hashdestroy(V_ng_name_hash, M_NETGRAPH_NODE, V_ng_name_hmask);
1000 V_ng_name_hash = new;
1001 V_ng_name_hmask = hmask;
1005 * Allocate a bigger ID hash.
1010 struct nodehash *new;
1016 new = hashinit_flags((V_ng_ID_hmask + 1) * 2, M_NETGRAPH_NODE, &hmask,
1021 for (i = 0; i <= V_ng_ID_hmask; i++)
1022 LIST_FOREACH_SAFE(node, &V_ng_ID_hash[i], nd_idnodes, node2) {
1024 LIST_REMOVE(node, nd_idnodes);
1026 hash = (node->nd_ID % (hmask + 1));
1027 LIST_INSERT_HEAD(&new[hash], node, nd_idnodes);
1030 hashdestroy(V_ng_ID_hash, M_NETGRAPH_NODE, V_ng_name_hmask);
1032 V_ng_ID_hmask = hmask;
1035 /************************************************************************
1037 Names are not optional. Hooks are always connected, except for a
1038 brief moment within these routines. On invalidation or during creation
1039 they are connected to the 'dead' hook.
1040 ************************************************************************/
1043 * Remove a hook reference
1046 ng_unref_hook(hook_p hook)
1049 if (hook == &ng_deadhook)
1052 if (refcount_release(&hook->hk_refs)) { /* we were the last */
1053 if (_NG_HOOK_NODE(hook)) /* it'll probably be ng_deadnode */
1054 _NG_NODE_UNREF((_NG_HOOK_NODE(hook)));
1060 * Add an unconnected hook to a node. Only used internally.
1061 * Assumes node is locked. (XXX not yet true )
1064 ng_add_hook(node_p node, const char *name, hook_p *hookp)
1069 /* Check that the given name is good */
1074 if (ng_findhook(node, name) != NULL) {
1079 /* Allocate the hook and link it up */
1080 NG_ALLOC_HOOK(hook);
1085 hook->hk_refs = 1; /* add a reference for us to return */
1086 hook->hk_flags = HK_INVALID;
1087 hook->hk_peer = &ng_deadhook; /* start off this way */
1088 hook->hk_node = node;
1089 NG_NODE_REF(node); /* each hook counts as a reference */
1092 strlcpy(NG_HOOK_NAME(hook), name, NG_HOOKSIZ);
1095 * Check if the node type code has something to say about it
1096 * If it fails, the unref of the hook will also unref the node.
1098 if (node->nd_type->newhook != NULL) {
1099 if ((error = (*node->nd_type->newhook)(node, hook, name))) {
1100 NG_HOOK_UNREF(hook); /* this frees the hook */
1105 * The 'type' agrees so far, so go ahead and link it in.
1106 * We'll ask again later when we actually connect the hooks.
1108 LIST_INSERT_HEAD(&node->nd_hooks, hook, hk_hooks);
1109 node->nd_numhooks++;
1110 NG_HOOK_REF(hook); /* one for the node */
1120 * Node types may supply their own optimized routines for finding
1121 * hooks. If none is supplied, we just do a linear search.
1122 * XXX Possibly we should add a reference to the hook?
1125 ng_findhook(node_p node, const char *name)
1129 if (node->nd_type->findhook != NULL)
1130 return (*node->nd_type->findhook)(node, name);
1131 LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) {
1132 if (NG_HOOK_IS_VALID(hook) &&
1133 (strcmp(NG_HOOK_NAME(hook), name) == 0))
1142 * As hooks are always attached, this really destroys two hooks.
1143 * The one given, and the one attached to it. Disconnect the hooks
1144 * from each other first. We reconnect the peer hook to the 'dead'
1145 * hook so that it can still exist after we depart. We then
1146 * send the peer its own destroy message. This ensures that we only
1147 * interact with the peer's structures when it is locked processing that
1148 * message. We hold a reference to the peer hook so we are guaranteed that
1149 * the peer hook and node are still going to exist until
1150 * we are finished there as the hook holds a ref on the node.
1151 * We run this same code again on the peer hook, but that time it is already
1152 * attached to the 'dead' hook.
1154 * This routine is called at all stages of hook creation
1155 * on error detection and must be able to handle any such stage.
1158 ng_destroy_hook(hook_p hook)
1163 if (hook == &ng_deadhook) { /* better safe than sorry */
1164 printf("ng_destroy_hook called on deadhook\n");
1169 * Protect divorce process with mutex, to avoid races on
1170 * simultaneous disconnect.
1174 hook->hk_flags |= HK_INVALID;
1176 peer = NG_HOOK_PEER(hook);
1177 node = NG_HOOK_NODE(hook);
1179 if (peer && (peer != &ng_deadhook)) {
1181 * Set the peer to point to ng_deadhook
1182 * from this moment on we are effectively independent it.
1183 * send it an rmhook message of its own.
1185 peer->hk_peer = &ng_deadhook; /* They no longer know us */
1186 hook->hk_peer = &ng_deadhook; /* Nor us, them */
1187 if (NG_HOOK_NODE(peer) == &ng_deadnode) {
1189 * If it's already divorced from a node,
1195 ng_rmhook_self(peer); /* Send it a surprise */
1197 NG_HOOK_UNREF(peer); /* account for peer link */
1198 NG_HOOK_UNREF(hook); /* account for peer link */
1202 TOPOLOGY_NOTOWNED();
1205 * Remove the hook from the node's list to avoid possible recursion
1206 * in case the disconnection results in node shutdown.
1208 if (node == &ng_deadnode) { /* happens if called from ng_con_nodes() */
1211 LIST_REMOVE(hook, hk_hooks);
1212 node->nd_numhooks--;
1213 if (node->nd_type->disconnect) {
1215 * The type handler may elect to destroy the node so don't
1216 * trust its existence after this point. (except
1217 * that we still hold a reference on it. (which we
1218 * inherrited from the hook we are destroying)
1220 (*node->nd_type->disconnect) (hook);
1224 * Note that because we will point to ng_deadnode, the original node
1225 * is not decremented automatically so we do that manually.
1227 _NG_HOOK_NODE(hook) = &ng_deadnode;
1228 NG_NODE_UNREF(node); /* We no longer point to it so adjust count */
1229 NG_HOOK_UNREF(hook); /* Account for linkage (in list) to node */
1233 * Take two hooks on a node and merge the connection so that the given node
1234 * is effectively bypassed.
1237 ng_bypass(hook_p hook1, hook_p hook2)
1239 if (hook1->hk_node != hook2->hk_node) {
1244 if (NG_HOOK_NOT_VALID(hook1) || NG_HOOK_NOT_VALID(hook2)) {
1248 hook1->hk_peer->hk_peer = hook2->hk_peer;
1249 hook2->hk_peer->hk_peer = hook1->hk_peer;
1251 hook1->hk_peer = &ng_deadhook;
1252 hook2->hk_peer = &ng_deadhook;
1255 NG_HOOK_UNREF(hook1);
1256 NG_HOOK_UNREF(hook2);
1258 /* XXX If we ever cache methods on hooks update them as well */
1259 ng_destroy_hook(hook1);
1260 ng_destroy_hook(hook2);
1265 * Install a new netgraph type
1268 ng_newtype(struct ng_type *tp)
1270 const size_t namelen = strlen(tp->name);
1272 /* Check version and type name fields */
1273 if ((tp->version != NG_ABI_VERSION) || (namelen == 0) ||
1274 (namelen >= NG_TYPESIZ)) {
1276 if (tp->version != NG_ABI_VERSION) {
1277 printf("Netgraph: Node type rejected. ABI mismatch. "
1278 "Suggest recompile\n");
1283 /* Check for name collision */
1284 if (ng_findtype(tp->name) != NULL) {
1289 /* Link in new type */
1291 LIST_INSERT_HEAD(&ng_typelist, tp, types);
1292 tp->refs = 1; /* first ref is linked list */
1298 * unlink a netgraph type
1299 * If no examples exist
1302 ng_rmtype(struct ng_type *tp)
1304 /* Check for name collision */
1305 if (tp->refs != 1) {
1312 LIST_REMOVE(tp, types);
1318 * Look for a type of the name given
1321 ng_findtype(const char *typename)
1323 struct ng_type *type;
1326 LIST_FOREACH(type, &ng_typelist, types) {
1327 if (strcmp(type->name, typename) == 0)
1334 /************************************************************************
1336 ************************************************************************/
1338 * Connect two nodes using the specified hooks, using queued functions.
1341 ng_con_part3(node_p node, item_p item, hook_p hook)
1346 * When we run, we know that the node 'node' is locked for us.
1347 * Our caller has a reference on the hook.
1348 * Our caller has a reference on the node.
1349 * (In this case our caller is ng_apply_item() ).
1350 * The peer hook has a reference on the hook.
1351 * We are all set up except for the final call to the node, and
1352 * the clearing of the INVALID flag.
1354 if (NG_HOOK_NODE(hook) == &ng_deadnode) {
1356 * The node must have been freed again since we last visited
1357 * here. ng_destry_hook() has this effect but nothing else does.
1358 * We should just release our references and
1359 * free anything we can think of.
1360 * Since we know it's been destroyed, and it's our caller
1361 * that holds the references, just return.
1365 if (hook->hk_node->nd_type->connect) {
1366 if ((error = (*hook->hk_node->nd_type->connect) (hook))) {
1367 ng_destroy_hook(hook); /* also zaps peer */
1368 printf("failed in ng_con_part3()\n");
1373 * XXX this is wrong for SMP. Possibly we need
1374 * to separate out 'create' and 'invalid' flags.
1375 * should only set flags on hooks we have locked under our node.
1377 hook->hk_flags &= ~HK_INVALID;
1384 ng_con_part2(node_p node, item_p item, hook_p hook)
1390 * When we run, we know that the node 'node' is locked for us.
1391 * Our caller has a reference on the hook.
1392 * Our caller has a reference on the node.
1393 * (In this case our caller is ng_apply_item() ).
1394 * The peer hook has a reference on the hook.
1395 * our node pointer points to the 'dead' node.
1396 * First check the hook name is unique.
1397 * Should not happen because we checked before queueing this.
1399 if (ng_findhook(node, NG_HOOK_NAME(hook)) != NULL) {
1401 ng_destroy_hook(hook); /* should destroy peer too */
1402 printf("failed in ng_con_part2()\n");
1406 * Check if the node type code has something to say about it
1407 * If it fails, the unref of the hook will also unref the attached node,
1408 * however since that node is 'ng_deadnode' this will do nothing.
1409 * The peer hook will also be destroyed.
1411 if (node->nd_type->newhook != NULL) {
1412 if ((error = (*node->nd_type->newhook)(node, hook,
1414 ng_destroy_hook(hook); /* should destroy peer too */
1415 printf("failed in ng_con_part2()\n");
1421 * The 'type' agrees so far, so go ahead and link it in.
1422 * We'll ask again later when we actually connect the hooks.
1424 hook->hk_node = node; /* just overwrite ng_deadnode */
1425 NG_NODE_REF(node); /* each hook counts as a reference */
1426 LIST_INSERT_HEAD(&node->nd_hooks, hook, hk_hooks);
1427 node->nd_numhooks++;
1428 NG_HOOK_REF(hook); /* one for the node */
1431 * We now have a symmetrical situation, where both hooks have been
1432 * linked to their nodes, the newhook methods have been called
1433 * And the references are all correct. The hooks are still marked
1434 * as invalid, as we have not called the 'connect' methods
1436 * We can call the local one immediately as we have the
1437 * node locked, but we need to queue the remote one.
1439 if (hook->hk_node->nd_type->connect) {
1440 if ((error = (*hook->hk_node->nd_type->connect) (hook))) {
1441 ng_destroy_hook(hook); /* also zaps peer */
1442 printf("failed in ng_con_part2(A)\n");
1448 * Acquire topo mutex to avoid race with ng_destroy_hook().
1451 peer = hook->hk_peer;
1452 if (peer == &ng_deadhook) {
1454 printf("failed in ng_con_part2(B)\n");
1455 ng_destroy_hook(hook);
1460 if ((error = ng_send_fn2(peer->hk_node, peer, item, &ng_con_part3,
1461 NULL, 0, NG_REUSE_ITEM))) {
1462 printf("failed in ng_con_part2(C)\n");
1463 ng_destroy_hook(hook); /* also zaps peer */
1464 return (error); /* item was consumed. */
1466 hook->hk_flags &= ~HK_INVALID; /* need both to be able to work */
1467 return (0); /* item was consumed. */
1474 * Connect this node with another node. We assume that this node is
1475 * currently locked, as we are only called from an NGM_CONNECT message.
1478 ng_con_nodes(item_p item, node_p node, const char *name,
1479 node_p node2, const char *name2)
1485 if (ng_findhook(node2, name2) != NULL) {
1488 if ((error = ng_add_hook(node, name, &hook))) /* gives us a ref */
1490 /* Allocate the other hook and link it up */
1491 NG_ALLOC_HOOK(hook2);
1492 if (hook2 == NULL) {
1494 ng_destroy_hook(hook); /* XXX check ref counts so far */
1495 NG_HOOK_UNREF(hook); /* including our ref */
1498 hook2->hk_refs = 1; /* start with a reference for us. */
1499 hook2->hk_flags = HK_INVALID;
1500 hook2->hk_peer = hook; /* Link the two together */
1501 hook->hk_peer = hook2;
1502 NG_HOOK_REF(hook); /* Add a ref for the peer to each*/
1504 hook2->hk_node = &ng_deadnode;
1505 strlcpy(NG_HOOK_NAME(hook2), name2, NG_HOOKSIZ);
1508 * Queue the function above.
1509 * Procesing continues in that function in the lock context of
1512 if ((error = ng_send_fn2(node2, hook2, item, &ng_con_part2, NULL, 0,
1514 printf("failed in ng_con_nodes(): %d\n", error);
1515 ng_destroy_hook(hook); /* also zaps peer */
1518 NG_HOOK_UNREF(hook); /* Let each hook go if it wants to */
1519 NG_HOOK_UNREF(hook2);
1524 * Make a peer and connect.
1525 * We assume that the local node is locked.
1526 * The new node probably doesn't need a lock until
1527 * it has a hook, because it cannot really have any work until then,
1528 * but we should think about it a bit more.
1530 * The problem may come if the other node also fires up
1531 * some hardware or a timer or some other source of activation,
1532 * also it may already get a command msg via it's ID.
1534 * We could use the same method as ng_con_nodes() but we'd have
1535 * to add ability to remove the node when failing. (Not hard, just
1536 * make arg1 point to the node to remove).
1537 * Unless of course we just ignore failure to connect and leave
1538 * an unconnected node?
1541 ng_mkpeer(node_p node, const char *name, const char *name2, char *type)
1544 hook_p hook1, hook2;
1547 if ((error = ng_make_node(type, &node2))) {
1551 if ((error = ng_add_hook(node, name, &hook1))) { /* gives us a ref */
1552 ng_rmnode(node2, NULL, NULL, 0);
1556 if ((error = ng_add_hook(node2, name2, &hook2))) {
1557 ng_rmnode(node2, NULL, NULL, 0);
1558 ng_destroy_hook(hook1);
1559 NG_HOOK_UNREF(hook1);
1564 * Actually link the two hooks together.
1566 hook1->hk_peer = hook2;
1567 hook2->hk_peer = hook1;
1569 /* Each hook is referenced by the other */
1573 /* Give each node the opportunity to veto the pending connection */
1574 if (hook1->hk_node->nd_type->connect) {
1575 error = (*hook1->hk_node->nd_type->connect) (hook1);
1578 if ((error == 0) && hook2->hk_node->nd_type->connect) {
1579 error = (*hook2->hk_node->nd_type->connect) (hook2);
1584 * drop the references we were holding on the two hooks.
1587 ng_destroy_hook(hook2); /* also zaps hook1 */
1588 ng_rmnode(node2, NULL, NULL, 0);
1590 /* As a last act, allow the hooks to be used */
1591 hook1->hk_flags &= ~HK_INVALID;
1592 hook2->hk_flags &= ~HK_INVALID;
1594 NG_HOOK_UNREF(hook1);
1595 NG_HOOK_UNREF(hook2);
1599 /************************************************************************
1600 Utility routines to send self messages
1601 ************************************************************************/
1603 /* Shut this node down as soon as everyone is clear of it */
1604 /* Should add arg "immediately" to jump the queue */
1606 ng_rmnode_self(node_p node)
1610 if (node == &ng_deadnode)
1612 node->nd_flags |= NGF_INVALID;
1613 if (node->nd_flags & NGF_CLOSING)
1616 error = ng_send_fn(node, NULL, &ng_rmnode, NULL, 0);
1621 ng_rmhook_part2(node_p node, hook_p hook, void *arg1, int arg2)
1623 ng_destroy_hook(hook);
1628 ng_rmhook_self(hook_p hook)
1631 node_p node = NG_HOOK_NODE(hook);
1633 if (node == &ng_deadnode)
1636 error = ng_send_fn(node, hook, &ng_rmhook_part2, NULL, 0);
1640 /***********************************************************************
1641 * Parse and verify a string of the form: <NODE:><PATH>
1643 * Such a string can refer to a specific node or a specific hook
1644 * on a specific node, depending on how you look at it. In the
1645 * latter case, the PATH component must not end in a dot.
1647 * Both <NODE:> and <PATH> are optional. The <PATH> is a string
1648 * of hook names separated by dots. This breaks out the original
1649 * string, setting *nodep to "NODE" (or NULL if none) and *pathp
1650 * to "PATH" (or NULL if degenerate). Also, *hookp will point to
1651 * the final hook component of <PATH>, if any, otherwise NULL.
1653 * This returns -1 if the path is malformed. The char ** are optional.
1654 ***********************************************************************/
1656 ng_path_parse(char *addr, char **nodep, char **pathp, char **hookp)
1658 char *node, *path, *hook;
1662 * Extract absolute NODE, if any
1664 for (path = addr; *path && *path != ':'; path++);
1666 node = addr; /* Here's the NODE */
1667 *path++ = '\0'; /* Here's the PATH */
1669 /* Node name must not be empty */
1673 /* A name of "." is OK; otherwise '.' not allowed */
1674 if (strcmp(node, ".") != 0) {
1675 for (k = 0; node[k]; k++)
1680 node = NULL; /* No absolute NODE */
1681 path = addr; /* Here's the PATH */
1684 /* Snoop for illegal characters in PATH */
1685 for (k = 0; path[k]; k++)
1689 /* Check for no repeated dots in PATH */
1690 for (k = 0; path[k]; k++)
1691 if (path[k] == '.' && path[k + 1] == '.')
1694 /* Remove extra (degenerate) dots from beginning or end of PATH */
1697 if (*path && path[strlen(path) - 1] == '.')
1698 path[strlen(path) - 1] = 0;
1700 /* If PATH has a dot, then we're not talking about a hook */
1702 for (hook = path, k = 0; path[k]; k++)
1703 if (path[k] == '.') {
1721 * Given a path, which may be absolute or relative, and a starting node,
1722 * return the destination node.
1725 ng_path2noderef(node_p here, const char *address, node_p *destp,
1728 char fullpath[NG_PATHSIZ];
1729 char *nodename, *path;
1730 node_p node, oldnode;
1733 if (destp == NULL) {
1739 /* Make a writable copy of address for ng_path_parse() */
1740 strncpy(fullpath, address, sizeof(fullpath) - 1);
1741 fullpath[sizeof(fullpath) - 1] = '\0';
1743 /* Parse out node and sequence of hooks */
1744 if (ng_path_parse(fullpath, &nodename, &path, NULL) < 0) {
1750 * For an absolute address, jump to the starting node.
1751 * Note that this holds a reference on the node for us.
1752 * Don't forget to drop the reference if we don't need it.
1755 node = ng_name2noderef(here, nodename);
1770 if (lasthook != NULL)
1777 * Now follow the sequence of hooks
1779 * XXXGL: The path may demolish as we go the sequence, but if
1780 * we hold the topology mutex at critical places, then, I hope,
1781 * we would always have valid pointers in hand, although the
1782 * path behind us may no longer exist.
1789 * Break out the next path segment. Replace the dot we just
1790 * found with a NUL; "path" points to the next segment (or the
1793 for (segment = path; *path != '\0'; path++) {
1800 /* We have a segment, so look for a hook by that name */
1801 hook = ng_findhook(node, segment);
1804 /* Can't get there from here... */
1805 if (hook == NULL || NG_HOOK_PEER(hook) == NULL ||
1806 NG_HOOK_NOT_VALID(hook) ||
1807 NG_HOOK_NOT_VALID(NG_HOOK_PEER(hook))) {
1809 NG_NODE_UNREF(node);
1815 * Hop on over to the next node
1817 * Big race conditions here as hooks and nodes go away
1818 * *** Idea.. store an ng_ID_t in each hook and use that
1819 * instead of the direct hook in this crawl?
1822 if ((node = NG_PEER_NODE(hook)))
1823 NG_NODE_REF(node); /* XXX RACE */
1824 NG_NODE_UNREF(oldnode); /* XXX another race */
1825 if (NG_NODE_NOT_VALID(node)) {
1826 NG_NODE_UNREF(node); /* XXX more races */
1832 if (*path == '\0') {
1833 if (lasthook != NULL) {
1835 *lasthook = NG_HOOK_PEER(hook);
1836 NG_HOOK_REF(*lasthook);
1848 /***************************************************************\
1849 * Input queue handling.
1850 * All activities are submitted to the node via the input queue
1851 * which implements a multiple-reader/single-writer gate.
1852 * Items which cannot be handled immediately are queued.
1854 * read-write queue locking inline functions *
1855 \***************************************************************/
1857 static __inline void ng_queue_rw(node_p node, item_p item, int rw);
1858 static __inline item_p ng_dequeue(node_p node, int *rw);
1859 static __inline item_p ng_acquire_read(node_p node, item_p item);
1860 static __inline item_p ng_acquire_write(node_p node, item_p item);
1861 static __inline void ng_leave_read(node_p node);
1862 static __inline void ng_leave_write(node_p node);
1865 * Definition of the bits fields in the ng_queue flag word.
1866 * Defined here rather than in netgraph.h because no-one should fiddle
1869 * The ordering here may be important! don't shuffle these.
1872 Safety Barrier--------+ (adjustable to suit taste) (not used yet)
1875 +-------+-------+-------+-------+-------+-------+-------+-------+
1876 | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
1877 | |A|c|t|i|v|e| |R|e|a|d|e|r| |C|o|u|n|t| | | | | | | | | |P|A|
1878 | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |O|W|
1879 +-------+-------+-------+-------+-------+-------+-------+-------+
1880 \___________________________ ____________________________/ | |
1882 [active reader count] | |
1884 Operation Pending -------------------------------+ |
1886 Active Writer ---------------------------------------+
1888 Node queue has such semantics:
1889 - All flags modifications are atomic.
1890 - Reader count can be incremented only if there is no writer or pending flags.
1891 As soon as this can't be done with single operation, it is implemented with
1892 spin loop and atomic_cmpset().
1893 - Writer flag can be set only if there is no any bits set.
1894 It is implemented with atomic_cmpset().
1895 - Pending flag can be set any time, but to avoid collision on queue processing
1896 all queue fields are protected by the mutex.
1897 - Queue processing thread reads queue holding the mutex, but releases it while
1898 processing. When queue is empty pending flag is removed.
1901 #define WRITER_ACTIVE 0x00000001
1902 #define OP_PENDING 0x00000002
1903 #define READER_INCREMENT 0x00000004
1904 #define READER_MASK 0xfffffffc /* Not valid if WRITER_ACTIVE is set */
1905 #define SAFETY_BARRIER 0x00100000 /* 128K items queued should be enough */
1907 /* Defines of more elaborate states on the queue */
1908 /* Mask of bits a new read cares about */
1909 #define NGQ_RMASK (WRITER_ACTIVE|OP_PENDING)
1911 /* Mask of bits a new write cares about */
1912 #define NGQ_WMASK (NGQ_RMASK|READER_MASK)
1914 /* Test to decide if there is something on the queue. */
1915 #define QUEUE_ACTIVE(QP) ((QP)->q_flags & OP_PENDING)
1917 /* How to decide what the next queued item is. */
1918 #define HEAD_IS_READER(QP) NGI_QUEUED_READER(STAILQ_FIRST(&(QP)->queue))
1919 #define HEAD_IS_WRITER(QP) NGI_QUEUED_WRITER(STAILQ_FIRST(&(QP)->queue)) /* notused */
1921 /* Read the status to decide if the next item on the queue can now run. */
1922 #define QUEUED_READER_CAN_PROCEED(QP) \
1923 (((QP)->q_flags & (NGQ_RMASK & ~OP_PENDING)) == 0)
1924 #define QUEUED_WRITER_CAN_PROCEED(QP) \
1925 (((QP)->q_flags & (NGQ_WMASK & ~OP_PENDING)) == 0)
1927 /* Is there a chance of getting ANY work off the queue? */
1928 #define NEXT_QUEUED_ITEM_CAN_PROCEED(QP) \
1929 ((HEAD_IS_READER(QP)) ? QUEUED_READER_CAN_PROCEED(QP) : \
1930 QUEUED_WRITER_CAN_PROCEED(QP))
1935 #define NGQ2_WORKQ 0x00000001
1938 * Taking into account the current state of the queue and node, possibly take
1939 * the next entry off the queue and return it. Return NULL if there was
1940 * nothing we could return, either because there really was nothing there, or
1941 * because the node was in a state where it cannot yet process the next item
1944 static __inline item_p
1945 ng_dequeue(node_p node, int *rw)
1948 struct ng_queue *ngq = &node->nd_input_queue;
1950 /* This MUST be called with the mutex held. */
1951 mtx_assert(&ngq->q_mtx, MA_OWNED);
1953 /* If there is nothing queued, then just return. */
1954 if (!QUEUE_ACTIVE(ngq)) {
1955 CTR4(KTR_NET, "%20s: node [%x] (%p) queue empty; "
1956 "queue flags 0x%lx", __func__,
1957 node->nd_ID, node, ngq->q_flags);
1962 * From here, we can assume there is a head item.
1963 * We need to find out what it is and if it can be dequeued, given
1964 * the current state of the node.
1966 if (HEAD_IS_READER(ngq)) {
1968 long t = ngq->q_flags;
1969 if (t & WRITER_ACTIVE) {
1970 /* There is writer, reader can't proceed. */
1971 CTR4(KTR_NET, "%20s: node [%x] (%p) queued "
1972 "reader can't proceed; queue flags 0x%lx",
1973 __func__, node->nd_ID, node, t);
1976 if (atomic_cmpset_acq_int(&ngq->q_flags, t,
1977 t + READER_INCREMENT))
1981 /* We have got reader lock for the node. */
1983 } else if (atomic_cmpset_acq_int(&ngq->q_flags, OP_PENDING,
1984 OP_PENDING + WRITER_ACTIVE)) {
1985 /* We have got writer lock for the node. */
1988 /* There is somebody other, writer can't proceed. */
1989 CTR4(KTR_NET, "%20s: node [%x] (%p) queued writer can't "
1990 "proceed; queue flags 0x%lx", __func__, node->nd_ID, node,
1996 * Now we dequeue the request (whatever it may be) and correct the
1997 * pending flags and the next and last pointers.
1999 item = STAILQ_FIRST(&ngq->queue);
2000 STAILQ_REMOVE_HEAD(&ngq->queue, el_next);
2001 if (STAILQ_EMPTY(&ngq->queue))
2002 atomic_clear_int(&ngq->q_flags, OP_PENDING);
2003 CTR6(KTR_NET, "%20s: node [%x] (%p) returning item %p as %s; queue "
2004 "flags 0x%lx", __func__, node->nd_ID, node, item, *rw ? "WRITER" :
2005 "READER", ngq->q_flags);
2010 * Queue a packet to be picked up later by someone else.
2011 * If the queue could be run now, add node to the queue handler's worklist.
2013 static __inline void
2014 ng_queue_rw(node_p node, item_p item, int rw)
2016 struct ng_queue *ngq = &node->nd_input_queue;
2018 NGI_SET_WRITER(item);
2020 NGI_SET_READER(item);
2024 /* Set OP_PENDING flag and enqueue the item. */
2025 atomic_set_int(&ngq->q_flags, OP_PENDING);
2026 STAILQ_INSERT_TAIL(&ngq->queue, item, el_next);
2028 CTR5(KTR_NET, "%20s: node [%x] (%p) queued item %p as %s", __func__,
2029 node->nd_ID, node, item, rw ? "WRITER" : "READER" );
2032 * We can take the worklist lock with the node locked
2033 * BUT NOT THE REVERSE!
2035 if (NEXT_QUEUED_ITEM_CAN_PROCEED(ngq))
2036 ng_worklist_add(node);
2037 NG_QUEUE_UNLOCK(ngq);
2040 /* Acquire reader lock on node. If node is busy, queue the packet. */
2041 static __inline item_p
2042 ng_acquire_read(node_p node, item_p item)
2044 KASSERT(node != &ng_deadnode,
2045 ("%s: working on deadnode", __func__));
2047 /* Reader needs node without writer and pending items. */
2049 long t = node->nd_input_queue.q_flags;
2051 break; /* Node is not ready for reader. */
2052 if (atomic_cmpset_acq_int(&node->nd_input_queue.q_flags, t,
2053 t + READER_INCREMENT)) {
2054 /* Successfully grabbed node */
2055 CTR4(KTR_NET, "%20s: node [%x] (%p) acquired item %p",
2056 __func__, node->nd_ID, node, item);
2062 /* Queue the request for later. */
2063 ng_queue_rw(node, item, NGQRW_R);
2068 /* Acquire writer lock on node. If node is busy, queue the packet. */
2069 static __inline item_p
2070 ng_acquire_write(node_p node, item_p item)
2072 KASSERT(node != &ng_deadnode,
2073 ("%s: working on deadnode", __func__));
2075 /* Writer needs completely idle node. */
2076 if (atomic_cmpset_acq_int(&node->nd_input_queue.q_flags, 0,
2078 /* Successfully grabbed node */
2079 CTR4(KTR_NET, "%20s: node [%x] (%p) acquired item %p",
2080 __func__, node->nd_ID, node, item);
2084 /* Queue the request for later. */
2085 ng_queue_rw(node, item, NGQRW_W);
2091 static __inline item_p
2092 ng_upgrade_write(node_p node, item_p item)
2094 struct ng_queue *ngq = &node->nd_input_queue;
2095 KASSERT(node != &ng_deadnode,
2096 ("%s: working on deadnode", __func__));
2098 NGI_SET_WRITER(item);
2103 * There will never be no readers as we are there ourselves.
2104 * Set the WRITER_ACTIVE flags ASAP to block out fast track readers.
2105 * The caller we are running from will call ng_leave_read()
2106 * soon, so we must account for that. We must leave again with the
2107 * READER lock. If we find other readers, then
2108 * queue the request for later. However "later" may be rignt now
2109 * if there are no readers. We don't really care if there are queued
2110 * items as we will bypass them anyhow.
2112 atomic_add_int(&ngq->q_flags, WRITER_ACTIVE - READER_INCREMENT);
2113 if ((ngq->q_flags & (NGQ_WMASK & ~OP_PENDING)) == WRITER_ACTIVE) {
2114 NG_QUEUE_UNLOCK(ngq);
2116 /* It's just us, act on the item. */
2117 /* will NOT drop writer lock when done */
2118 ng_apply_item(node, item, 0);
2121 * Having acted on the item, atomically
2122 * downgrade back to READER and finish up.
2124 atomic_add_int(&ngq->q_flags, READER_INCREMENT - WRITER_ACTIVE);
2126 /* Our caller will call ng_leave_read() */
2130 * It's not just us active, so queue us AT THE HEAD.
2131 * "Why?" I hear you ask.
2132 * Put us at the head of the queue as we've already been
2133 * through it once. If there is nothing else waiting,
2134 * set the correct flags.
2136 if (STAILQ_EMPTY(&ngq->queue)) {
2137 /* We've gone from, 0 to 1 item in the queue */
2138 atomic_set_int(&ngq->q_flags, OP_PENDING);
2140 CTR3(KTR_NET, "%20s: node [%x] (%p) set OP_PENDING", __func__,
2143 STAILQ_INSERT_HEAD(&ngq->queue, item, el_next);
2144 CTR4(KTR_NET, "%20s: node [%x] (%p) requeued item %p as WRITER",
2145 __func__, node->nd_ID, node, item );
2147 /* Reverse what we did above. That downgrades us back to reader */
2148 atomic_add_int(&ngq->q_flags, READER_INCREMENT - WRITER_ACTIVE);
2149 if (QUEUE_ACTIVE(ngq) && NEXT_QUEUED_ITEM_CAN_PROCEED(ngq))
2150 ng_worklist_add(node);
2151 NG_QUEUE_UNLOCK(ngq);
2157 /* Release reader lock. */
2158 static __inline void
2159 ng_leave_read(node_p node)
2161 atomic_subtract_rel_int(&node->nd_input_queue.q_flags, READER_INCREMENT);
2164 /* Release writer lock. */
2165 static __inline void
2166 ng_leave_write(node_p node)
2168 atomic_clear_rel_int(&node->nd_input_queue.q_flags, WRITER_ACTIVE);
2171 /* Purge node queue. Called on node shutdown. */
2173 ng_flush_input_queue(node_p node)
2175 struct ng_queue *ngq = &node->nd_input_queue;
2179 while ((item = STAILQ_FIRST(&ngq->queue)) != NULL) {
2180 STAILQ_REMOVE_HEAD(&ngq->queue, el_next);
2181 if (STAILQ_EMPTY(&ngq->queue))
2182 atomic_clear_int(&ngq->q_flags, OP_PENDING);
2183 NG_QUEUE_UNLOCK(ngq);
2185 /* If the item is supplying a callback, call it with an error */
2186 if (item->apply != NULL) {
2187 if (item->depth == 1)
2188 item->apply->error = ENOENT;
2189 if (refcount_release(&item->apply->refs)) {
2190 (*item->apply->apply)(item->apply->context,
2191 item->apply->error);
2197 NG_QUEUE_UNLOCK(ngq);
2200 /***********************************************************************
2201 * Externally visible method for sending or queueing messages or data.
2202 ***********************************************************************/
2205 * The module code should have filled out the item correctly by this stage:
2207 * reference to destination node.
2208 * Reference to destination rcv hook if relevant.
2209 * apply pointer must be or NULL or reference valid struct ng_apply_info.
2214 * ID of original sender node. (return address)
2220 * The nodes have several routines and macros to help with this task:
2224 ng_snd_item(item_p item, int flags)
2229 struct ng_queue *ngq;
2232 /* We are sending item, so it must be present! */
2233 KASSERT(item != NULL, ("ng_snd_item: item is NULL"));
2235 #ifdef NETGRAPH_DEBUG
2236 _ngi_check(item, __FILE__, __LINE__);
2239 /* Item was sent once more, postpone apply() call. */
2241 refcount_acquire(&item->apply->refs);
2243 node = NGI_NODE(item);
2244 /* Node is never optional. */
2245 KASSERT(node != NULL, ("ng_snd_item: node is NULL"));
2247 hook = NGI_HOOK(item);
2248 /* Valid hook and mbuf are mandatory for data. */
2249 if ((item->el_flags & NGQF_TYPE) == NGQF_DATA) {
2250 KASSERT(hook != NULL, ("ng_snd_item: hook for data is NULL"));
2251 if (NGI_M(item) == NULL)
2253 CHECK_DATA_MBUF(NGI_M(item));
2257 * If the item or the node specifies single threading, force
2258 * writer semantics. Similarly, the node may say one hook always
2259 * produces writers. These are overrides.
2261 if (((item->el_flags & NGQF_RW) == NGQF_WRITER) ||
2262 (node->nd_flags & NGF_FORCE_WRITER) ||
2263 (hook && (hook->hk_flags & HK_FORCE_WRITER))) {
2270 * If sender or receiver requests queued delivery, or call graph
2271 * loops back from outbound to inbound path, or stack usage
2272 * level is dangerous - enqueue message.
2274 if ((flags & NG_QUEUE) || (hook && (hook->hk_flags & HK_QUEUE))) {
2276 } else if (hook && (hook->hk_flags & HK_TO_INBOUND) &&
2277 curthread->td_ng_outbound) {
2281 #ifdef GET_STACK_USAGE
2283 * Most of netgraph nodes have small stack consumption and
2284 * for them 25% of free stack space is more than enough.
2285 * Nodes/hooks with higher stack usage should be marked as
2286 * HI_STACK. For them 50% of stack will be guaranteed then.
2287 * XXX: Values 25% and 50% are completely empirical.
2290 GET_STACK_USAGE(st, su);
2292 if ((sl * 4 < st) || ((sl * 2 < st) &&
2293 ((node->nd_flags & NGF_HI_STACK) || (hook &&
2294 (hook->hk_flags & HK_HI_STACK)))))
2300 /* Put it on the queue for that node*/
2301 ng_queue_rw(node, item, rw);
2302 return ((flags & NG_PROGRESS) ? EINPROGRESS : 0);
2306 * We already decided how we will be queueud or treated.
2307 * Try get the appropriate operating permission.
2310 item = ng_acquire_read(node, item);
2312 item = ng_acquire_write(node, item);
2314 /* Item was queued while trying to get permission. */
2316 return ((flags & NG_PROGRESS) ? EINPROGRESS : 0);
2318 NGI_GET_NODE(item, node); /* zaps stored node */
2321 error = ng_apply_item(node, item, rw); /* drops r/w lock when done */
2323 /* If something is waiting on queue and ready, schedule it. */
2324 ngq = &node->nd_input_queue;
2325 if (QUEUE_ACTIVE(ngq)) {
2327 if (QUEUE_ACTIVE(ngq) && NEXT_QUEUED_ITEM_CAN_PROCEED(ngq))
2328 ng_worklist_add(node);
2329 NG_QUEUE_UNLOCK(ngq);
2333 * Node may go away as soon as we remove the reference.
2334 * Whatever we do, DO NOT access the node again!
2336 NG_NODE_UNREF(node);
2341 /* If was not sent, apply callback here. */
2342 if (item->apply != NULL) {
2343 if (item->depth == 0 && error != 0)
2344 item->apply->error = error;
2345 if (refcount_release(&item->apply->refs)) {
2346 (*item->apply->apply)(item->apply->context,
2347 item->apply->error);
2356 * We have an item that was possibly queued somewhere.
2357 * It should contain all the information needed
2358 * to run it on the appropriate node/hook.
2359 * If there is apply pointer and we own the last reference, call apply().
2362 ng_apply_item(node_p node, item_p item, int rw)
2365 ng_rcvdata_t *rcvdata;
2366 ng_rcvmsg_t *rcvmsg;
2367 struct ng_apply_info *apply;
2368 int error = 0, depth;
2370 /* Node and item are never optional. */
2371 KASSERT(node != NULL, ("ng_apply_item: node is NULL"));
2372 KASSERT(item != NULL, ("ng_apply_item: item is NULL"));
2374 NGI_GET_HOOK(item, hook); /* clears stored hook */
2375 #ifdef NETGRAPH_DEBUG
2376 _ngi_check(item, __FILE__, __LINE__);
2379 apply = item->apply;
2380 depth = item->depth;
2382 switch (item->el_flags & NGQF_TYPE) {
2385 * Check things are still ok as when we were queued.
2387 KASSERT(hook != NULL, ("ng_apply_item: hook for data is NULL"));
2388 if (NG_HOOK_NOT_VALID(hook) ||
2389 NG_NODE_NOT_VALID(node)) {
2395 * If no receive method, just silently drop it.
2396 * Give preference to the hook over-ride method.
2398 if ((!(rcvdata = hook->hk_rcvdata)) &&
2399 (!(rcvdata = NG_HOOK_NODE(hook)->nd_type->rcvdata))) {
2404 error = (*rcvdata)(hook, item);
2407 if (hook && NG_HOOK_NOT_VALID(hook)) {
2409 * The hook has been zapped then we can't use it.
2410 * Immediately drop its reference.
2411 * The message may not need it.
2413 NG_HOOK_UNREF(hook);
2417 * Similarly, if the node is a zombie there is
2418 * nothing we can do with it, drop everything.
2420 if (NG_NODE_NOT_VALID(node)) {
2427 * Call the appropriate message handler for the object.
2428 * It is up to the message handler to free the message.
2429 * If it's a generic message, handle it generically,
2430 * otherwise call the type's message handler (if it exists).
2431 * XXX (race). Remember that a queued message may
2432 * reference a node or hook that has just been
2433 * invalidated. It will exist as the queue code
2434 * is holding a reference, but..
2436 if ((NGI_MSG(item)->header.typecookie == NGM_GENERIC_COOKIE) &&
2437 ((NGI_MSG(item)->header.flags & NGF_RESP) == 0)) {
2438 error = ng_generic_msg(node, item, hook);
2441 if (((!hook) || (!(rcvmsg = hook->hk_rcvmsg))) &&
2442 (!(rcvmsg = node->nd_type->rcvmsg))) {
2448 error = (*rcvmsg)(node, item, hook);
2453 * In the case of the shutdown message we allow it to hit
2454 * even if the node is invalid.
2456 if (NG_NODE_NOT_VALID(node) &&
2457 NGI_FN(item) != &ng_rmnode) {
2463 /* Same is about some internal functions and invalid hook. */
2464 if (hook && NG_HOOK_NOT_VALID(hook) &&
2465 NGI_FN2(item) != &ng_con_part2 &&
2466 NGI_FN2(item) != &ng_con_part3 &&
2467 NGI_FN(item) != &ng_rmhook_part2) {
2474 if ((item->el_flags & NGQF_TYPE) == NGQF_FN) {
2475 (*NGI_FN(item))(node, hook, NGI_ARG1(item),
2478 } else /* it is NGQF_FN2 */
2479 error = (*NGI_FN2(item))(node, item, hook);
2483 * We held references on some of the resources
2484 * that we took from the item. Now that we have
2485 * finished doing everything, drop those references.
2488 NG_HOOK_UNREF(hook);
2491 ng_leave_read(node);
2493 ng_leave_write(node);
2495 /* Apply callback. */
2496 if (apply != NULL) {
2497 if (depth == 1 && error != 0)
2498 apply->error = error;
2499 if (refcount_release(&apply->refs))
2500 (*apply->apply)(apply->context, apply->error);
2506 /***********************************************************************
2507 * Implement the 'generic' control messages
2508 ***********************************************************************/
2510 ng_generic_msg(node_p here, item_p item, hook_p lasthook)
2513 struct ng_mesg *msg;
2514 struct ng_mesg *resp = NULL;
2516 NGI_GET_MSG(item, msg);
2517 if (msg->header.typecookie != NGM_GENERIC_COOKIE) {
2522 switch (msg->header.cmd) {
2524 ng_rmnode(here, NULL, NULL, 0);
2528 struct ngm_mkpeer *const mkp = (struct ngm_mkpeer *) msg->data;
2530 if (msg->header.arglen != sizeof(*mkp)) {
2535 mkp->type[sizeof(mkp->type) - 1] = '\0';
2536 mkp->ourhook[sizeof(mkp->ourhook) - 1] = '\0';
2537 mkp->peerhook[sizeof(mkp->peerhook) - 1] = '\0';
2538 error = ng_mkpeer(here, mkp->ourhook, mkp->peerhook, mkp->type);
2543 struct ngm_connect *const con =
2544 (struct ngm_connect *) msg->data;
2547 if (msg->header.arglen != sizeof(*con)) {
2552 con->path[sizeof(con->path) - 1] = '\0';
2553 con->ourhook[sizeof(con->ourhook) - 1] = '\0';
2554 con->peerhook[sizeof(con->peerhook) - 1] = '\0';
2555 /* Don't forget we get a reference.. */
2556 error = ng_path2noderef(here, con->path, &node2, NULL);
2559 error = ng_con_nodes(item, here, con->ourhook,
2560 node2, con->peerhook);
2561 NG_NODE_UNREF(node2);
2566 struct ngm_name *const nam = (struct ngm_name *) msg->data;
2568 if (msg->header.arglen != sizeof(*nam)) {
2573 nam->name[sizeof(nam->name) - 1] = '\0';
2574 error = ng_name_node(here, nam->name);
2579 struct ngm_rmhook *const rmh = (struct ngm_rmhook *) msg->data;
2582 if (msg->header.arglen != sizeof(*rmh)) {
2587 rmh->ourhook[sizeof(rmh->ourhook) - 1] = '\0';
2588 if ((hook = ng_findhook(here, rmh->ourhook)) != NULL)
2589 ng_destroy_hook(hook);
2594 struct nodeinfo *ni;
2596 NG_MKRESPONSE(resp, msg, sizeof(*ni), M_NOWAIT);
2602 /* Fill in node info */
2603 ni = (struct nodeinfo *) resp->data;
2604 if (NG_NODE_HAS_NAME(here))
2605 strcpy(ni->name, NG_NODE_NAME(here));
2606 strcpy(ni->type, here->nd_type->name);
2607 ni->id = ng_node2ID(here);
2608 ni->hooks = here->nd_numhooks;
2613 const int nhooks = here->nd_numhooks;
2614 struct hooklist *hl;
2615 struct nodeinfo *ni;
2618 /* Get response struct */
2619 NG_MKRESPONSE(resp, msg, sizeof(*hl) +
2620 (nhooks * sizeof(struct linkinfo)), M_NOWAIT);
2625 hl = (struct hooklist *) resp->data;
2628 /* Fill in node info */
2629 if (NG_NODE_HAS_NAME(here))
2630 strcpy(ni->name, NG_NODE_NAME(here));
2631 strcpy(ni->type, here->nd_type->name);
2632 ni->id = ng_node2ID(here);
2634 /* Cycle through the linked list of hooks */
2636 LIST_FOREACH(hook, &here->nd_hooks, hk_hooks) {
2637 struct linkinfo *const link = &hl->link[ni->hooks];
2639 if (ni->hooks >= nhooks) {
2640 log(LOG_ERR, "%s: number of %s changed\n",
2644 if (NG_HOOK_NOT_VALID(hook))
2646 strcpy(link->ourhook, NG_HOOK_NAME(hook));
2647 strcpy(link->peerhook, NG_PEER_HOOK_NAME(hook));
2648 if (NG_PEER_NODE_NAME(hook)[0] != '\0')
2649 strcpy(link->nodeinfo.name,
2650 NG_PEER_NODE_NAME(hook));
2651 strcpy(link->nodeinfo.type,
2652 NG_PEER_NODE(hook)->nd_type->name);
2653 link->nodeinfo.id = ng_node2ID(NG_PEER_NODE(hook));
2654 link->nodeinfo.hooks = NG_PEER_NODE(hook)->nd_numhooks;
2662 struct namelist *nl;
2667 /* Get response struct. */
2668 NG_MKRESPONSE(resp, msg, sizeof(*nl) +
2669 (V_ng_nodes * sizeof(struct nodeinfo)), M_NOWAIT);
2675 nl = (struct namelist *) resp->data;
2677 /* Cycle through the lists of nodes. */
2679 for (i = 0; i <= V_ng_ID_hmask; i++) {
2680 LIST_FOREACH(node, &V_ng_ID_hash[i], nd_idnodes) {
2681 struct nodeinfo *const np =
2682 &nl->nodeinfo[nl->numnames];
2684 if (NG_NODE_NOT_VALID(node))
2686 if (NG_NODE_HAS_NAME(node))
2687 strcpy(np->name, NG_NODE_NAME(node));
2688 strcpy(np->type, node->nd_type->name);
2689 np->id = ng_node2ID(node);
2690 np->hooks = node->nd_numhooks;
2691 KASSERT(nl->numnames < V_ng_nodes,
2692 ("%s: no space", __func__));
2701 struct namelist *nl;
2706 /* Get response struct. */
2707 NG_MKRESPONSE(resp, msg, sizeof(*nl) +
2708 (V_ng_named_nodes * sizeof(struct nodeinfo)), M_NOWAIT);
2714 nl = (struct namelist *) resp->data;
2716 /* Cycle through the lists of nodes. */
2718 for (i = 0; i <= V_ng_name_hmask; i++) {
2719 LIST_FOREACH(node, &V_ng_name_hash[i], nd_nodes) {
2720 struct nodeinfo *const np =
2721 &nl->nodeinfo[nl->numnames];
2723 if (NG_NODE_NOT_VALID(node))
2725 strcpy(np->name, NG_NODE_NAME(node));
2726 strcpy(np->type, node->nd_type->name);
2727 np->id = ng_node2ID(node);
2728 np->hooks = node->nd_numhooks;
2729 KASSERT(nl->numnames < V_ng_named_nodes,
2730 ("%s: no space", __func__));
2740 struct typelist *tl;
2741 struct ng_type *type;
2745 /* Count number of types */
2746 LIST_FOREACH(type, &ng_typelist, types)
2749 /* Get response struct */
2750 NG_MKRESPONSE(resp, msg, sizeof(*tl) +
2751 (num * sizeof(struct typeinfo)), M_NOWAIT);
2757 tl = (struct typelist *) resp->data;
2759 /* Cycle through the linked list of types */
2761 LIST_FOREACH(type, &ng_typelist, types) {
2762 struct typeinfo *const tp = &tl->typeinfo[tl->numtypes];
2764 strcpy(tp->type_name, type->name);
2765 tp->numnodes = type->refs - 1; /* don't count list */
2766 KASSERT(tl->numtypes < num, ("%s: no space", __func__));
2773 case NGM_BINARY2ASCII:
2775 int bufSize = 20 * 1024; /* XXX hard coded constant */
2776 const struct ng_parse_type *argstype;
2777 const struct ng_cmdlist *c;
2778 struct ng_mesg *binary, *ascii;
2780 /* Data area must contain a valid netgraph message */
2781 binary = (struct ng_mesg *)msg->data;
2782 if (msg->header.arglen < sizeof(struct ng_mesg) ||
2783 (msg->header.arglen - sizeof(struct ng_mesg) <
2784 binary->header.arglen)) {
2790 /* Get a response message with lots of room */
2791 NG_MKRESPONSE(resp, msg, sizeof(*ascii) + bufSize, M_NOWAIT);
2796 ascii = (struct ng_mesg *)resp->data;
2798 /* Copy binary message header to response message payload */
2799 bcopy(binary, ascii, sizeof(*binary));
2801 /* Find command by matching typecookie and command number */
2802 for (c = here->nd_type->cmdlist; c != NULL && c->name != NULL;
2804 if (binary->header.typecookie == c->cookie &&
2805 binary->header.cmd == c->cmd)
2808 if (c == NULL || c->name == NULL) {
2809 for (c = ng_generic_cmds; c->name != NULL; c++) {
2810 if (binary->header.typecookie == c->cookie &&
2811 binary->header.cmd == c->cmd)
2814 if (c->name == NULL) {
2821 /* Convert command name to ASCII */
2822 snprintf(ascii->header.cmdstr, sizeof(ascii->header.cmdstr),
2825 /* Convert command arguments to ASCII */
2826 argstype = (binary->header.flags & NGF_RESP) ?
2827 c->respType : c->mesgType;
2828 if (argstype == NULL) {
2829 *ascii->data = '\0';
2831 if ((error = ng_unparse(argstype,
2832 (u_char *)binary->data,
2833 ascii->data, bufSize)) != 0) {
2839 /* Return the result as struct ng_mesg plus ASCII string */
2840 bufSize = strlen(ascii->data) + 1;
2841 ascii->header.arglen = bufSize;
2842 resp->header.arglen = sizeof(*ascii) + bufSize;
2846 case NGM_ASCII2BINARY:
2848 int bufSize = 20 * 1024; /* XXX hard coded constant */
2849 const struct ng_cmdlist *c;
2850 const struct ng_parse_type *argstype;
2851 struct ng_mesg *ascii, *binary;
2854 /* Data area must contain at least a struct ng_mesg + '\0' */
2855 ascii = (struct ng_mesg *)msg->data;
2856 if ((msg->header.arglen < sizeof(*ascii) + 1) ||
2857 (ascii->header.arglen < 1) ||
2858 (msg->header.arglen < sizeof(*ascii) +
2859 ascii->header.arglen)) {
2864 ascii->data[ascii->header.arglen - 1] = '\0';
2866 /* Get a response message with lots of room */
2867 NG_MKRESPONSE(resp, msg, sizeof(*binary) + bufSize, M_NOWAIT);
2872 binary = (struct ng_mesg *)resp->data;
2874 /* Copy ASCII message header to response message payload */
2875 bcopy(ascii, binary, sizeof(*ascii));
2877 /* Find command by matching ASCII command string */
2878 for (c = here->nd_type->cmdlist;
2879 c != NULL && c->name != NULL; c++) {
2880 if (strcmp(ascii->header.cmdstr, c->name) == 0)
2883 if (c == NULL || c->name == NULL) {
2884 for (c = ng_generic_cmds; c->name != NULL; c++) {
2885 if (strcmp(ascii->header.cmdstr, c->name) == 0)
2888 if (c->name == NULL) {
2895 /* Convert command name to binary */
2896 binary->header.cmd = c->cmd;
2897 binary->header.typecookie = c->cookie;
2899 /* Convert command arguments to binary */
2900 argstype = (binary->header.flags & NGF_RESP) ?
2901 c->respType : c->mesgType;
2902 if (argstype == NULL) {
2905 if ((error = ng_parse(argstype, ascii->data, &off,
2906 (u_char *)binary->data, &bufSize)) != 0) {
2912 /* Return the result */
2913 binary->header.arglen = bufSize;
2914 resp->header.arglen = sizeof(*binary) + bufSize;
2918 case NGM_TEXT_CONFIG:
2919 case NGM_TEXT_STATUS:
2921 * This one is tricky as it passes the command down to the
2922 * actual node, even though it is a generic type command.
2923 * This means we must assume that the item/msg is already freed
2924 * when control passes back to us.
2926 if (here->nd_type->rcvmsg != NULL) {
2927 NGI_MSG(item) = msg; /* put it back as we found it */
2928 return((*here->nd_type->rcvmsg)(here, item, lasthook));
2930 /* Fall through if rcvmsg not supported */
2936 * Sometimes a generic message may be statically allocated
2937 * to avoid problems with allocating when in tight memory situations.
2938 * Don't free it if it is so.
2939 * I break them apart here, because erros may cause a free if the item
2940 * in which case we'd be doing it twice.
2941 * they are kept together above, to simplify freeing.
2944 NG_RESPOND_MSG(error, here, item, resp);
2949 /************************************************************************
2950 Queue element get/free routines
2951 ************************************************************************/
2953 uma_zone_t ng_qzone;
2954 uma_zone_t ng_qdzone;
2955 static int numthreads = 0; /* number of queue threads */
2956 static int maxalloc = 4096;/* limit the damage of a leak */
2957 static int maxdata = 4096; /* limit the damage of a DoS */
2959 SYSCTL_INT(_net_graph, OID_AUTO, threads, CTLFLAG_RDTUN, &numthreads,
2960 0, "Number of queue processing threads");
2961 SYSCTL_INT(_net_graph, OID_AUTO, maxalloc, CTLFLAG_RDTUN, &maxalloc,
2962 0, "Maximum number of non-data queue items to allocate");
2963 SYSCTL_INT(_net_graph, OID_AUTO, maxdata, CTLFLAG_RDTUN, &maxdata,
2964 0, "Maximum number of data queue items to allocate");
2966 #ifdef NETGRAPH_DEBUG
2967 static TAILQ_HEAD(, ng_item) ng_itemlist = TAILQ_HEAD_INITIALIZER(ng_itemlist);
2968 static int allocated; /* number of items malloc'd */
2972 * Get a queue entry.
2973 * This is usually called when a packet first enters netgraph.
2974 * By definition, this is usually from an interrupt, or from a user.
2975 * Users are not so important, but try be quick for the times that it's
2978 static __inline item_p
2979 ng_alloc_item(int type, int flags)
2983 KASSERT(((type & ~NGQF_TYPE) == 0),
2984 ("%s: incorrect item type: %d", __func__, type));
2986 item = uma_zalloc((type == NGQF_DATA) ? ng_qdzone : ng_qzone,
2987 ((flags & NG_WAITOK) ? M_WAITOK : M_NOWAIT) | M_ZERO);
2990 item->el_flags = type;
2991 #ifdef NETGRAPH_DEBUG
2993 TAILQ_INSERT_TAIL(&ng_itemlist, item, all);
2995 mtx_unlock(&ngq_mtx);
3003 * Release a queue entry
3006 ng_free_item(item_p item)
3009 * The item may hold resources on its own. We need to free
3010 * these before we can free the item. What they are depends upon
3011 * what kind of item it is. it is important that nodes zero
3012 * out pointers to resources that they remove from the item
3013 * or we release them again here.
3015 switch (item->el_flags & NGQF_TYPE) {
3017 /* If we have an mbuf still attached.. */
3018 NG_FREE_M(_NGI_M(item));
3021 _NGI_RETADDR(item) = 0;
3022 NG_FREE_MSG(_NGI_MSG(item));
3026 /* nothing to free really, */
3027 _NGI_FN(item) = NULL;
3028 _NGI_ARG1(item) = NULL;
3029 _NGI_ARG2(item) = 0;
3032 /* If we still have a node or hook referenced... */
3033 _NGI_CLR_NODE(item);
3034 _NGI_CLR_HOOK(item);
3036 #ifdef NETGRAPH_DEBUG
3038 TAILQ_REMOVE(&ng_itemlist, item, all);
3040 mtx_unlock(&ngq_mtx);
3042 uma_zfree(((item->el_flags & NGQF_TYPE) == NGQF_DATA) ?
3043 ng_qdzone : ng_qzone, item);
3047 * Change type of the queue entry.
3048 * Possibly reallocates it from another UMA zone.
3050 static __inline item_p
3051 ng_realloc_item(item_p pitem, int type, int flags)
3056 KASSERT((pitem != NULL), ("%s: can't reallocate NULL", __func__));
3057 KASSERT(((type & ~NGQF_TYPE) == 0),
3058 ("%s: incorrect item type: %d", __func__, type));
3060 from = ((pitem->el_flags & NGQF_TYPE) == NGQF_DATA);
3061 to = (type == NGQF_DATA);
3063 /* If reallocation is required do it and copy item. */
3064 if ((item = ng_alloc_item(type, flags)) == NULL) {
3065 ng_free_item(pitem);
3069 ng_free_item(pitem);
3072 item->el_flags = (item->el_flags & ~NGQF_TYPE) | type;
3077 /************************************************************************
3079 ************************************************************************/
3082 * Handle the loading/unloading of a netgraph node type module
3085 ng_mod_event(module_t mod, int event, void *data)
3087 struct ng_type *const type = data;
3093 /* Register new netgraph node type */
3094 if ((error = ng_newtype(type)) != 0)
3097 /* Call type specific code */
3098 if (type->mod_event != NULL)
3099 if ((error = (*type->mod_event)(mod, event, data))) {
3101 type->refs--; /* undo it */
3102 LIST_REMOVE(type, types);
3108 if (type->refs > 1) { /* make sure no nodes exist! */
3111 if (type->refs == 0) /* failed load, nothing to undo */
3113 if (type->mod_event != NULL) { /* check with type */
3114 error = (*type->mod_event)(mod, event, data);
3115 if (error != 0) /* type refuses.. */
3119 LIST_REMOVE(type, types);
3125 if (type->mod_event != NULL)
3126 error = (*type->mod_event)(mod, event, data);
3128 error = EOPNOTSUPP; /* XXX ? */
3135 vnet_netgraph_init(const void *unused __unused)
3138 /* We start with small hashes, but they can grow. */
3139 V_ng_ID_hash = hashinit(16, M_NETGRAPH_NODE, &V_ng_ID_hmask);
3140 V_ng_name_hash = hashinit(16, M_NETGRAPH_NODE, &V_ng_name_hmask);
3142 VNET_SYSINIT(vnet_netgraph_init, SI_SUB_NETGRAPH, SI_ORDER_FIRST,
3143 vnet_netgraph_init, NULL);
3147 vnet_netgraph_uninit(const void *unused __unused)
3149 node_p node = NULL, last_killed = NULL;
3153 /* Find a node to kill */
3155 for (i = 0; i <= V_ng_ID_hmask; i++) {
3156 LIST_FOREACH(node, &V_ng_ID_hash[i], nd_idnodes) {
3157 if (node != &ng_deadnode) {
3167 /* Attempt to kill it only if it is a regular node */
3169 if (node == last_killed) {
3170 /* This should never happen */
3171 printf("ng node %s needs NGF_REALLY_DIE\n",
3173 if (node->nd_flags & NGF_REALLY_DIE)
3174 panic("ng node %s won't die",
3176 node->nd_flags |= NGF_REALLY_DIE;
3178 ng_rmnode(node, NULL, NULL, 0);
3179 NG_NODE_UNREF(node);
3182 } while (node != NULL);
3184 hashdestroy(V_ng_name_hash, M_NETGRAPH_NODE, V_ng_name_hmask);
3185 hashdestroy(V_ng_ID_hash, M_NETGRAPH_NODE, V_ng_ID_hmask);
3187 VNET_SYSUNINIT(vnet_netgraph_uninit, SI_SUB_NETGRAPH, SI_ORDER_FIRST,
3188 vnet_netgraph_uninit, NULL);
3192 * Handle loading and unloading for this code.
3193 * The only thing we need to link into is the NETISR strucure.
3196 ngb_mod_event(module_t mod, int event, void *data)
3204 /* Initialize everything. */
3205 NG_WORKLIST_LOCK_INIT();
3206 rw_init(&ng_typelist_lock, "netgraph types");
3207 rw_init(&ng_idhash_lock, "netgraph idhash");
3208 rw_init(&ng_namehash_lock, "netgraph namehash");
3209 rw_init(&ng_topo_lock, "netgraph topology mutex");
3210 #ifdef NETGRAPH_DEBUG
3211 mtx_init(&ng_nodelist_mtx, "netgraph nodelist mutex", NULL,
3213 mtx_init(&ngq_mtx, "netgraph item list mutex", NULL,
3216 ng_qzone = uma_zcreate("NetGraph items", sizeof(struct ng_item),
3217 NULL, NULL, NULL, NULL, UMA_ALIGN_CACHE, 0);
3218 uma_zone_set_max(ng_qzone, maxalloc);
3219 ng_qdzone = uma_zcreate("NetGraph data items",
3220 sizeof(struct ng_item), NULL, NULL, NULL, NULL,
3221 UMA_ALIGN_CACHE, 0);
3222 uma_zone_set_max(ng_qdzone, maxdata);
3223 /* Autoconfigure number of threads. */
3224 if (numthreads <= 0)
3225 numthreads = mp_ncpus;
3226 /* Create threads. */
3227 p = NULL; /* start with no process */
3228 for (i = 0; i < numthreads; i++) {
3229 if (kproc_kthread_add(ngthread, NULL, &p, &td,
3230 RFHIGHPID, 0, "ng_queue", "ng_queue%d", i)) {
3237 /* You can't unload it because an interface may be using it. */
3247 static moduledata_t netgraph_mod = {
3252 DECLARE_MODULE(netgraph, netgraph_mod, SI_SUB_NETGRAPH, SI_ORDER_FIRST);
3253 SYSCTL_NODE(_net, OID_AUTO, graph, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
3255 SYSCTL_INT(_net_graph, OID_AUTO, abi_version, CTLFLAG_RD, SYSCTL_NULL_INT_PTR, NG_ABI_VERSION,"");
3256 SYSCTL_INT(_net_graph, OID_AUTO, msg_version, CTLFLAG_RD, SYSCTL_NULL_INT_PTR, NG_VERSION, "");
3258 #ifdef NETGRAPH_DEBUG
3260 dumphook (hook_p hook, char *file, int line)
3262 printf("hook: name %s, %d refs, Last touched:\n",
3263 _NG_HOOK_NAME(hook), hook->hk_refs);
3264 printf(" Last active @ %s, line %d\n",
3265 hook->lastfile, hook->lastline);
3267 printf(" problem discovered at file %s, line %d\n", file, line);
3275 dumpnode(node_p node, char *file, int line)
3277 printf("node: ID [%x]: type '%s', %d hooks, flags 0x%x, %d refs, %s:\n",
3278 _NG_NODE_ID(node), node->nd_type->name,
3279 node->nd_numhooks, node->nd_flags,
3280 node->nd_refs, node->nd_name);
3281 printf(" Last active @ %s, line %d\n",
3282 node->lastfile, node->lastline);
3284 printf(" problem discovered at file %s, line %d\n", file, line);
3292 dumpitem(item_p item, char *file, int line)
3294 printf(" ACTIVE item, last used at %s, line %d",
3295 item->lastfile, item->lastline);
3296 switch(item->el_flags & NGQF_TYPE) {
3298 printf(" - [data]\n");
3301 printf(" - retaddr[%d]:\n", _NGI_RETADDR(item));
3304 printf(" - fn@%p (%p, %p, %p, %d (%x))\n",
3308 item->body.fn.fn_arg1,
3309 item->body.fn.fn_arg2,
3310 item->body.fn.fn_arg2);
3313 printf(" - fn2@%p (%p, %p, %p, %d (%x))\n",
3317 item->body.fn.fn_arg1,
3318 item->body.fn.fn_arg2,
3319 item->body.fn.fn_arg2);
3323 printf(" problem discovered at file %s, line %d\n", file, line);
3324 if (_NGI_NODE(item)) {
3325 printf("node %p ([%x])\n",
3326 _NGI_NODE(item), ng_node2ID(_NGI_NODE(item)));
3336 TAILQ_FOREACH(item, &ng_itemlist, all) {
3337 printf("[%d] ", i++);
3338 dumpitem(item, NULL, 0);
3347 mtx_lock(&ng_nodelist_mtx);
3348 SLIST_FOREACH(node, &ng_allnodes, nd_all) {
3349 printf("[%d] ", i++);
3350 dumpnode(node, NULL, 0);
3352 mtx_unlock(&ng_nodelist_mtx);
3360 mtx_lock(&ng_nodelist_mtx);
3361 SLIST_FOREACH(hook, &ng_allhooks, hk_all) {
3362 printf("[%d] ", i++);
3363 dumphook(hook, NULL, 0);
3365 mtx_unlock(&ng_nodelist_mtx);
3369 sysctl_debug_ng_dump_items(SYSCTL_HANDLER_ARGS)
3377 error = sysctl_handle_int(oidp, &val, 0, req);
3378 if (error != 0 || req->newptr == NULL)
3388 SYSCTL_PROC(_debug, OID_AUTO, ng_dump_items,
3389 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 0, sizeof(int),
3390 sysctl_debug_ng_dump_items, "I",
3391 "Number of allocated items");
3392 #endif /* NETGRAPH_DEBUG */
3394 /***********************************************************************
3396 **********************************************************************/
3398 * Pick a node off the list of nodes with work,
3399 * try get an item to process off it. Remove the node from the list.
3405 struct epoch_tracker et;
3408 /* Get node from the worklist. */
3410 while ((node = STAILQ_FIRST(&ng_worklist)) == NULL)
3411 NG_WORKLIST_SLEEP();
3412 STAILQ_REMOVE_HEAD(&ng_worklist, nd_input_queue.q_work);
3413 NG_WORKLIST_UNLOCK();
3414 CURVNET_SET(node->nd_vnet);
3415 CTR3(KTR_NET, "%20s: node [%x] (%p) taken off worklist",
3416 __func__, node->nd_ID, node);
3418 * We have the node. We also take over the reference
3419 * that the list had on it.
3420 * Now process as much as you can, until it won't
3421 * let you have another item off the queue.
3422 * All this time, keep the reference
3423 * that lets us be sure that the node still exists.
3424 * Let the reference go at the last minute.
3426 NET_EPOCH_ENTER(et);
3431 NG_QUEUE_LOCK(&node->nd_input_queue);
3432 item = ng_dequeue(node, &rw);
3434 node->nd_input_queue.q_flags2 &= ~NGQ2_WORKQ;
3435 NG_QUEUE_UNLOCK(&node->nd_input_queue);
3436 break; /* go look for another node */
3438 NG_QUEUE_UNLOCK(&node->nd_input_queue);
3439 NGI_GET_NODE(item, node); /* zaps stored node */
3440 ng_apply_item(node, item, rw);
3441 NG_NODE_UNREF(node);
3445 NG_NODE_UNREF(node);
3452 * It's posible that a debugging NG_NODE_REF may need
3453 * to be outside the mutex zone
3456 ng_worklist_add(node_p node)
3459 mtx_assert(&node->nd_input_queue.q_mtx, MA_OWNED);
3461 if ((node->nd_input_queue.q_flags2 & NGQ2_WORKQ) == 0) {
3463 * If we are not already on the work queue,
3466 node->nd_input_queue.q_flags2 |= NGQ2_WORKQ;
3467 NG_NODE_REF(node); /* XXX safe in mutex? */
3469 STAILQ_INSERT_TAIL(&ng_worklist, node, nd_input_queue.q_work);
3470 NG_WORKLIST_UNLOCK();
3471 CTR3(KTR_NET, "%20s: node [%x] (%p) put on worklist", __func__,
3473 NG_WORKLIST_WAKEUP();
3475 CTR3(KTR_NET, "%20s: node [%x] (%p) already on worklist",
3476 __func__, node->nd_ID, node);
3480 /***********************************************************************
3481 * Externally useable functions to set up a queue item ready for sending
3482 ***********************************************************************/
3484 #ifdef NETGRAPH_DEBUG
3485 #define ITEM_DEBUG_CHECKS \
3487 if (NGI_NODE(item) ) { \
3488 printf("item already has node"); \
3489 kdb_enter(KDB_WHY_NETGRAPH, "has node"); \
3490 NGI_CLR_NODE(item); \
3492 if (NGI_HOOK(item) ) { \
3493 printf("item already has hook"); \
3494 kdb_enter(KDB_WHY_NETGRAPH, "has hook"); \
3495 NGI_CLR_HOOK(item); \
3499 #define ITEM_DEBUG_CHECKS
3503 * Put mbuf into the item.
3504 * Hook and node references will be removed when the item is dequeued.
3506 * (XXX) Unsafe because no reference held by peer on remote node.
3507 * remote node might go away in this timescale.
3508 * We know the hooks can't go away because that would require getting
3509 * a writer item on both nodes and we must have at least a reader
3510 * here to be able to do this.
3511 * Note that the hook loaded is the REMOTE hook.
3513 * This is possibly in the critical path for new data.
3516 ng_package_data(struct mbuf *m, int flags)
3520 if ((item = ng_alloc_item(NGQF_DATA, flags)) == NULL) {
3525 item->el_flags |= NGQF_READER;
3531 * Allocate a queue item and put items into it..
3532 * Evaluate the address as this will be needed to queue it and
3533 * to work out what some of the fields should be.
3534 * Hook and node references will be removed when the item is dequeued.
3538 ng_package_msg(struct ng_mesg *msg, int flags)
3542 if ((item = ng_alloc_item(NGQF_MESG, flags)) == NULL) {
3547 /* Messages items count as writers unless explicitly exempted. */
3548 if (msg->header.cmd & NGM_READONLY)
3549 item->el_flags |= NGQF_READER;
3551 item->el_flags |= NGQF_WRITER;
3553 * Set the current lasthook into the queue item
3555 NGI_MSG(item) = msg;
3556 NGI_RETADDR(item) = 0;
3560 #define SET_RETADDR(item, here, retaddr) \
3561 do { /* Data or fn items don't have retaddrs */ \
3562 if ((item->el_flags & NGQF_TYPE) == NGQF_MESG) { \
3564 NGI_RETADDR(item) = retaddr; \
3567 * The old return address should be ok. \
3568 * If there isn't one, use the address \
3571 if (NGI_RETADDR(item) == 0) { \
3573 = ng_node2ID(here); \
3580 ng_address_hook(node_p here, item_p item, hook_p hook, ng_ID_t retaddr)
3586 * Quick sanity check..
3587 * Since a hook holds a reference on its node, once we know
3588 * that the peer is still connected (even if invalid,) we know
3589 * that the peer node is present, though maybe invalid.
3592 if ((hook == NULL) || NG_HOOK_NOT_VALID(hook) ||
3593 NG_HOOK_NOT_VALID(peer = NG_HOOK_PEER(hook)) ||
3594 NG_NODE_NOT_VALID(peernode = NG_PEER_NODE(hook))) {
3602 * Transfer our interest to the other (peer) end.
3605 NG_NODE_REF(peernode);
3606 NGI_SET_HOOK(item, peer);
3607 NGI_SET_NODE(item, peernode);
3608 SET_RETADDR(item, here, retaddr);
3616 ng_address_path(node_p here, item_p item, const char *address, ng_ID_t retaddr)
3624 * Note that ng_path2noderef increments the reference count
3625 * on the node for us if it finds one. So we don't have to.
3627 error = ng_path2noderef(here, address, &dest, &hook);
3632 NGI_SET_NODE(item, dest);
3634 NGI_SET_HOOK(item, hook);
3636 SET_RETADDR(item, here, retaddr);
3641 ng_address_ID(node_p here, item_p item, ng_ID_t ID, ng_ID_t retaddr)
3647 * Find the target node.
3649 dest = ng_ID2noderef(ID); /* GETS REFERENCE! */
3655 /* Fill out the contents */
3656 NGI_SET_NODE(item, dest);
3658 SET_RETADDR(item, here, retaddr);
3663 * special case to send a message to self (e.g. destroy node)
3664 * Possibly indicate an arrival hook too.
3665 * Useful for removing that hook :-)
3668 ng_package_msg_self(node_p here, hook_p hook, struct ng_mesg *msg)
3673 * Find the target node.
3674 * If there is a HOOK argument, then use that in preference
3677 if ((item = ng_alloc_item(NGQF_MESG, NG_NOFLAGS)) == NULL) {
3682 /* Fill out the contents */
3683 item->el_flags |= NGQF_WRITER;
3685 NGI_SET_NODE(item, here);
3688 NGI_SET_HOOK(item, hook);
3690 NGI_MSG(item) = msg;
3691 NGI_RETADDR(item) = ng_node2ID(here);
3696 * Send ng_item_fn function call to the specified node.
3700 ng_send_fn(node_p node, hook_p hook, ng_item_fn *fn, void * arg1, int arg2)
3703 return ng_send_fn1(node, hook, fn, arg1, arg2, NG_NOFLAGS);
3707 ng_send_fn1(node_p node, hook_p hook, ng_item_fn *fn, void * arg1, int arg2,
3712 if ((item = ng_alloc_item(NGQF_FN, flags)) == NULL) {
3715 item->el_flags |= NGQF_WRITER;
3716 NG_NODE_REF(node); /* and one for the item */
3717 NGI_SET_NODE(item, node);
3720 NGI_SET_HOOK(item, hook);
3723 NGI_ARG1(item) = arg1;
3724 NGI_ARG2(item) = arg2;
3725 return(ng_snd_item(item, flags));
3729 * Send ng_item_fn2 function call to the specified node.
3731 * If an optional pitem parameter is supplied, its apply
3732 * callback will be copied to the new item. If also NG_REUSE_ITEM
3733 * flag is set, no new item will be allocated, but pitem will
3737 ng_send_fn2(node_p node, hook_p hook, item_p pitem, ng_item_fn2 *fn, void *arg1,
3738 int arg2, int flags)
3742 KASSERT((pitem != NULL || (flags & NG_REUSE_ITEM) == 0),
3743 ("%s: NG_REUSE_ITEM but no pitem", __func__));
3746 * Allocate a new item if no supplied or
3747 * if we can't use supplied one.
3749 if (pitem == NULL || (flags & NG_REUSE_ITEM) == 0) {
3750 if ((item = ng_alloc_item(NGQF_FN2, flags)) == NULL)
3753 item->apply = pitem->apply;
3755 if ((item = ng_realloc_item(pitem, NGQF_FN2, flags)) == NULL)
3759 item->el_flags = (item->el_flags & ~NGQF_RW) | NGQF_WRITER;
3760 NG_NODE_REF(node); /* and one for the item */
3761 NGI_SET_NODE(item, node);
3764 NGI_SET_HOOK(item, hook);
3767 NGI_ARG1(item) = arg1;
3768 NGI_ARG2(item) = arg2;
3769 return(ng_snd_item(item, flags));
3773 * Official timeout routines for Netgraph nodes.
3776 ng_callout_trampoline(void *arg)
3778 struct epoch_tracker et;
3781 NET_EPOCH_ENTER(et);
3782 CURVNET_SET(NGI_NODE(item)->nd_vnet);
3783 ng_snd_item(item, 0);
3789 ng_callout(struct callout *c, node_p node, hook_p hook, int ticks,
3790 ng_item_fn *fn, void * arg1, int arg2)
3794 if ((item = ng_alloc_item(NGQF_FN, NG_NOFLAGS)) == NULL)
3797 item->el_flags |= NGQF_WRITER;
3798 NG_NODE_REF(node); /* and one for the item */
3799 NGI_SET_NODE(item, node);
3802 NGI_SET_HOOK(item, hook);
3805 NGI_ARG1(item) = arg1;
3806 NGI_ARG2(item) = arg2;
3808 if (callout_reset(c, ticks, &ng_callout_trampoline, item) == 1 &&
3810 NG_FREE_ITEM(oitem);
3814 /* A special modified version of callout_stop() */
3816 ng_uncallout(struct callout *c, node_p node)
3821 KASSERT(c != NULL, ("ng_uncallout: NULL callout"));
3822 KASSERT(node != NULL, ("ng_uncallout: NULL node"));
3824 rval = callout_stop(c);
3826 /* Do an extra check */
3827 if ((rval > 0) && (c->c_func == &ng_callout_trampoline) &&
3828 (item != NULL) && (NGI_NODE(item) == node)) {
3830 * We successfully removed it from the queue before it ran
3831 * So now we need to unreference everything that was
3832 * given extra references. (NG_FREE_ITEM does this).
3839 * Callers only want to know if the callout was cancelled and
3840 * not draining or stopped.
3846 * Set the address, if none given, give the node here.
3849 ng_replace_retaddr(node_p here, item_p item, ng_ID_t retaddr)
3852 NGI_RETADDR(item) = retaddr;
3855 * The old return address should be ok.
3856 * If there isn't one, use the address here.
3858 NGI_RETADDR(item) = ng_node2ID(here);