4 * Copyright (c) 1996-1999 Whistle Communications, Inc.
7 * Subject to the following obligations and disclaimer of warranty, use and
8 * redistribution of this software, in source or object code forms, with or
9 * without modifications are expressly permitted by Whistle Communications;
10 * provided, however, that:
11 * 1. Any and all reproductions of the source or object code must include the
12 * copyright notice above and the following disclaimer of warranties; and
13 * 2. No rights are granted, in any manner or form, to use Whistle
14 * Communications, Inc. trademarks, including the mark "WHISTLE
15 * COMMUNICATIONS" on advertising, endorsements, or otherwise except as
16 * such appears in the above copyright notice or in the software.
18 * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
19 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
20 * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
21 * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
23 * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
24 * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
25 * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
26 * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
27 * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
28 * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
29 * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
30 * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
36 * Authors: Julian Elischer <julian@freebsd.org>
37 * Archie Cobbs <archie@freebsd.org>
40 * $Whistle: ng_base.c,v 1.39 1999/01/28 23:54:53 julian Exp $
44 * This file implements the base netgraph code.
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/errno.h>
50 #include <sys/kernel.h>
51 #include <sys/malloc.h>
52 #include <sys/syslog.h>
53 #include <sys/sysctl.h>
54 #include <sys/linker.h>
55 #include <sys/queue.h>
57 #include <sys/ctype.h>
58 #include <sys/sysctl.h>
59 #include <machine/limits.h>
61 #include <net/netisr.h>
63 #include <netgraph/ng_message.h>
64 #include <netgraph/netgraph.h>
65 #include <netgraph/ng_parse.h>
67 MODULE_VERSION(netgraph, NG_ABI_VERSION);
69 /* List of all active nodes */
70 static LIST_HEAD(, ng_node) ng_nodelist;
71 static struct mtx ng_nodelist_mtx;
75 static SLIST_HEAD(, ng_node) ng_allnodes;
76 static LIST_HEAD(, ng_node) ng_freenodes; /* in debug, we never free() them */
77 static SLIST_HEAD(, ng_hook) ng_allhooks;
78 static LIST_HEAD(, ng_hook) ng_freehooks; /* in debug, we never free() them */
80 static void ng_dumpitems(void);
81 static void ng_dumpnodes(void);
82 static void ng_dumphooks(void);
84 #endif /* NETGRAPH_DEBUG */
86 * DEAD versions of the structures.
87 * In order to avoid races, it is sometimes neccesary to point
88 * at SOMETHING even though theoretically, the current entity is
89 * INVALID. Use these to avoid these races.
91 struct ng_type ng_deadtype = {
95 NULL, /* constructor */
102 NULL, /* disconnect */
106 struct ng_node ng_deadnode = {
114 LIST_HEAD_INITIALIZER(ng_deadnode.hooks),
115 {}, /* all_nodes list entry */
116 {}, /* id hashtable list entry */
117 {}, /* workqueue entry */
119 {}, /* should never use! (should hang) */
121 &ng_deadnode.nd_input_queue.queue,
124 #ifdef NETGRAPH_DEBUG
129 #endif /* NETGRAPH_DEBUG */
132 struct ng_hook ng_deadhook = {
135 HK_INVALID | HK_DEAD,
136 1, /* refs always >= 1 */
137 &ng_deadhook, /* Peer is self */
138 &ng_deadnode, /* attached to deadnode */
140 NULL, /* override rcvmsg() */
141 NULL, /* override rcvdata() */
142 #ifdef NETGRAPH_DEBUG
147 #endif /* NETGRAPH_DEBUG */
151 * END DEAD STRUCTURES
153 /* List nodes with unallocated work */
154 static TAILQ_HEAD(, ng_node) ng_worklist = TAILQ_HEAD_INITIALIZER(ng_worklist);
155 static struct mtx ng_worklist_mtx; /* MUST LOCK NODE FIRST */
157 /* List of installed types */
158 static LIST_HEAD(, ng_type) ng_typelist;
159 static struct mtx ng_typelist_mtx;
161 /* Hash related definitions */
162 /* XXX Don't need to initialise them because it's a LIST */
163 #define NG_ID_HASH_SIZE 32 /* most systems wont need even this many */
164 static LIST_HEAD(, ng_node) ng_ID_hash[NG_ID_HASH_SIZE];
165 static struct mtx ng_idhash_mtx;
166 /* Method to find a node.. used twice so do it here */
167 #define NG_IDHASH_FN(ID) ((ID) % (NG_ID_HASH_SIZE))
168 #define NG_IDHASH_FIND(ID, node) \
170 LIST_FOREACH(node, &ng_ID_hash[NG_IDHASH_FN(ID)], \
172 if (NG_NODE_IS_VALID(node) \
173 && (NG_NODE_ID(node) == ID)) { \
179 /* Mutex that protects the free queue item list */
180 static volatile item_p ngqfree; /* free ones */
181 static struct mtx ngq_mtx;
183 /* Internal functions */
184 static int ng_add_hook(node_p node, const char *name, hook_p * hookp);
185 static int ng_generic_msg(node_p here, item_p item, hook_p lasthook);
186 static ng_ID_t ng_decodeidname(const char *name);
187 static int ngb_mod_event(module_t mod, int event, void *data);
188 static void ng_worklist_remove(node_p node);
189 static void ngintr(void);
190 static int ng_apply_item(node_p node, item_p item);
191 static void ng_flush_input_queue(struct ng_queue * ngq);
192 static void ng_setisr(node_p node);
193 static node_p ng_ID2noderef(ng_ID_t ID);
194 static int ng_con_nodes(node_p node, const char *name, node_p node2,
196 static void ng_con_part2(node_p node, hook_p hook, void *arg1, int arg2);
197 static void ng_con_part3(node_p node, hook_p hook, void *arg1, int arg2);
198 static int ng_mkpeer(node_p node, const char *name,
199 const char *name2, char *type);
201 /* imported , these used to be externally visible, some may go back */
202 int ng_bypass(hook_p hook1, hook_p hook2);
203 void ng_destroy_hook(hook_p hook);
204 node_p ng_name2noderef(node_p node, const char *name);
205 int ng_path2noderef(node_p here, const char *path,
206 node_p *dest, hook_p *lasthook);
207 struct ng_type *ng_findtype(const char *type);
208 int ng_make_node(const char *type, node_p *nodepp);
209 int ng_path_parse(char *addr, char **node, char **path, char **hook);
210 void ng_rmnode(node_p node, hook_p dummy1, void *dummy2, int dummy3);
211 void ng_unname(node_p node);
214 /* Our own netgraph malloc type */
215 MALLOC_DEFINE(M_NETGRAPH, "netgraph", "netgraph structures and ctrl messages");
216 MALLOC_DEFINE(M_NETGRAPH_HOOK, "netgraph_hook", "netgraph hook structures");
217 MALLOC_DEFINE(M_NETGRAPH_NODE, "netgraph_node", "netgraph node structures");
218 MALLOC_DEFINE(M_NETGRAPH_ITEM, "netgraph_item", "netgraph item structures");
219 MALLOC_DEFINE(M_NETGRAPH_META, "netgraph_meta", "netgraph name storage");
220 MALLOC_DEFINE(M_NETGRAPH_MSG, "netgraph_msg", "netgraph name storage");
222 /* Should not be visible outside this file */
224 #define _NG_ALLOC_HOOK(hook) \
225 MALLOC(hook, hook_p, sizeof(*hook), M_NETGRAPH_HOOK, M_NOWAIT | M_ZERO)
226 #define _NG_ALLOC_NODE(node) \
227 MALLOC(node, node_p, sizeof(*node), M_NETGRAPH_NODE, M_NOWAIT | M_ZERO)
229 #ifdef NETGRAPH_DEBUG /*----------------------------------------------*/
232 * In an attempt to help track reference count screwups
233 * we do not free objects back to the malloc system, but keep them
234 * in a local cache where we can examine them and keep information safely
235 * after they have been freed.
236 * We use this scheme for nodes and hooks, and to some extent for items.
238 static __inline hook_p
242 SLIST_ENTRY(ng_hook) temp;
243 mtx_lock(&ng_nodelist_mtx);
244 hook = LIST_FIRST(&ng_freehooks);
246 LIST_REMOVE(hook, hk_hooks);
247 bcopy(&hook->hk_all, &temp, sizeof(temp));
248 bzero(hook, sizeof(struct ng_hook));
249 bcopy(&temp, &hook->hk_all, sizeof(temp));
250 mtx_unlock(&ng_nodelist_mtx);
251 hook->hk_magic = HK_MAGIC;
253 mtx_unlock(&ng_nodelist_mtx);
254 _NG_ALLOC_HOOK(hook);
256 hook->hk_magic = HK_MAGIC;
257 mtx_lock(&ng_nodelist_mtx);
258 SLIST_INSERT_HEAD(&ng_allhooks, hook, hk_all);
259 mtx_unlock(&ng_nodelist_mtx);
265 static __inline node_p
269 SLIST_ENTRY(ng_node) temp;
270 mtx_lock(&ng_nodelist_mtx);
271 node = LIST_FIRST(&ng_freenodes);
273 LIST_REMOVE(node, nd_nodes);
274 bcopy(&node->nd_all, &temp, sizeof(temp));
275 bzero(node, sizeof(struct ng_node));
276 bcopy(&temp, &node->nd_all, sizeof(temp));
277 mtx_unlock(&ng_nodelist_mtx);
278 node->nd_magic = ND_MAGIC;
280 mtx_unlock(&ng_nodelist_mtx);
281 _NG_ALLOC_NODE(node);
283 node->nd_magic = ND_MAGIC;
284 mtx_lock(&ng_nodelist_mtx);
285 SLIST_INSERT_HEAD(&ng_allnodes, node, nd_all);
286 mtx_unlock(&ng_nodelist_mtx);
292 #define NG_ALLOC_HOOK(hook) do { (hook) = ng_alloc_hook(); } while (0)
293 #define NG_ALLOC_NODE(node) do { (node) = ng_alloc_node(); } while (0)
296 #define NG_FREE_HOOK(hook) \
298 mtx_lock(&ng_nodelist_mtx); \
299 LIST_INSERT_HEAD(&ng_freehooks, hook, hk_hooks); \
300 hook->hk_magic = 0; \
301 mtx_unlock(&ng_nodelist_mtx); \
304 #define NG_FREE_NODE(node) \
306 mtx_lock(&ng_nodelist_mtx); \
307 LIST_INSERT_HEAD(&ng_freenodes, node, nd_nodes); \
308 node->nd_magic = 0; \
309 mtx_unlock(&ng_nodelist_mtx); \
312 #else /* NETGRAPH_DEBUG */ /*----------------------------------------------*/
314 #define NG_ALLOC_HOOK(hook) _NG_ALLOC_HOOK(hook)
315 #define NG_ALLOC_NODE(node) _NG_ALLOC_NODE(node)
317 #define NG_FREE_HOOK(hook) do { FREE((hook), M_NETGRAPH_HOOK); } while (0)
318 #define NG_FREE_NODE(node) do { FREE((node), M_NETGRAPH_NODE); } while (0)
320 #endif /* NETGRAPH_DEBUG */ /*----------------------------------------------*/
322 /* Warning: Generally use NG_FREE_ITEM() instead */
323 #define NG_FREE_ITEM_REAL(item) do { FREE((item), M_NETGRAPH_ITEM); } while (0)
326 /* Set this to Debugger("X") to catch all errors as they occur */
331 static ng_ID_t nextID = 1;
334 #define CHECK_DATA_MBUF(m) do { \
338 if (((m)->m_flags & M_PKTHDR) == 0) \
339 panic("%s: !PKTHDR", __func__); \
340 for (total = 0, n = (m); n != NULL; n = n->m_next) \
342 if ((m)->m_pkthdr.len != total) { \
343 panic("%s: %d != %d", \
344 __func__, (m)->m_pkthdr.len, total); \
348 #define CHECK_DATA_MBUF(m)
352 /************************************************************************
353 Parse type definitions for generic messages
354 ************************************************************************/
356 /* Handy structure parse type defining macro */
357 #define DEFINE_PARSE_STRUCT_TYPE(lo, up, args) \
358 static const struct ng_parse_struct_info \
359 ng_ ## lo ## _type_info = NG_GENERIC_ ## up ## _INFO args; \
360 static const struct ng_parse_type ng_generic_ ## lo ## _type = { \
361 &ng_parse_struct_type, \
362 &ng_ ## lo ## _type_info \
365 DEFINE_PARSE_STRUCT_TYPE(mkpeer, MKPEER, ());
366 DEFINE_PARSE_STRUCT_TYPE(connect, CONNECT, ());
367 DEFINE_PARSE_STRUCT_TYPE(name, NAME, ());
368 DEFINE_PARSE_STRUCT_TYPE(rmhook, RMHOOK, ());
369 DEFINE_PARSE_STRUCT_TYPE(nodeinfo, NODEINFO, ());
370 DEFINE_PARSE_STRUCT_TYPE(typeinfo, TYPEINFO, ());
371 DEFINE_PARSE_STRUCT_TYPE(linkinfo, LINKINFO, (&ng_generic_nodeinfo_type));
373 /* Get length of an array when the length is stored as a 32 bit
374 value immediately preceding the array -- as with struct namelist
375 and struct typelist. */
377 ng_generic_list_getLength(const struct ng_parse_type *type,
378 const u_char *start, const u_char *buf)
380 return *((const u_int32_t *)(buf - 4));
383 /* Get length of the array of struct linkinfo inside a struct hooklist */
385 ng_generic_linkinfo_getLength(const struct ng_parse_type *type,
386 const u_char *start, const u_char *buf)
388 const struct hooklist *hl = (const struct hooklist *)start;
390 return hl->nodeinfo.hooks;
393 /* Array type for a variable length array of struct namelist */
394 static const struct ng_parse_array_info ng_nodeinfoarray_type_info = {
395 &ng_generic_nodeinfo_type,
396 &ng_generic_list_getLength
398 static const struct ng_parse_type ng_generic_nodeinfoarray_type = {
399 &ng_parse_array_type,
400 &ng_nodeinfoarray_type_info
403 /* Array type for a variable length array of struct typelist */
404 static const struct ng_parse_array_info ng_typeinfoarray_type_info = {
405 &ng_generic_typeinfo_type,
406 &ng_generic_list_getLength
408 static const struct ng_parse_type ng_generic_typeinfoarray_type = {
409 &ng_parse_array_type,
410 &ng_typeinfoarray_type_info
413 /* Array type for array of struct linkinfo in struct hooklist */
414 static const struct ng_parse_array_info ng_generic_linkinfo_array_type_info = {
415 &ng_generic_linkinfo_type,
416 &ng_generic_linkinfo_getLength
418 static const struct ng_parse_type ng_generic_linkinfo_array_type = {
419 &ng_parse_array_type,
420 &ng_generic_linkinfo_array_type_info
423 DEFINE_PARSE_STRUCT_TYPE(typelist, TYPELIST, (&ng_generic_nodeinfoarray_type));
424 DEFINE_PARSE_STRUCT_TYPE(hooklist, HOOKLIST,
425 (&ng_generic_nodeinfo_type, &ng_generic_linkinfo_array_type));
426 DEFINE_PARSE_STRUCT_TYPE(listnodes, LISTNODES,
427 (&ng_generic_nodeinfoarray_type));
429 /* List of commands and how to convert arguments to/from ASCII */
430 static const struct ng_cmdlist ng_generic_cmds[] = {
442 &ng_generic_mkpeer_type,
449 &ng_generic_connect_type,
456 &ng_generic_name_type,
463 &ng_generic_rmhook_type,
471 &ng_generic_nodeinfo_type
478 &ng_generic_hooklist_type
485 &ng_generic_listnodes_type /* same as NGM_LISTNODES */
492 &ng_generic_listnodes_type
499 &ng_generic_typeinfo_type
506 &ng_parse_string_type
513 &ng_parse_string_type
519 &ng_parse_ng_mesg_type,
520 &ng_parse_ng_mesg_type
526 &ng_parse_ng_mesg_type,
527 &ng_parse_ng_mesg_type
532 /************************************************************************
534 ************************************************************************/
537 * Instantiate a node of the requested type
540 ng_make_node(const char *typename, node_p *nodepp)
542 struct ng_type *type;
545 /* Check that the type makes sense */
546 if (typename == NULL) {
551 /* Locate the node type */
552 if ((type = ng_findtype(typename)) == NULL) {
553 char filename[NG_TYPELEN + 4];
557 /* Not found, try to load it as a loadable module */
558 snprintf(filename, sizeof(filename), "ng_%s", typename);
559 error = linker_load_file(filename, &lf);
562 lf->userrefs++; /* pretend loaded by the syscall */
564 /* Try again, as now the type should have linked itself in */
565 if ((type = ng_findtype(typename)) == NULL)
570 * If we have a constructor, then make the node and
571 * call the constructor to do type specific initialisation.
573 if (type->constructor != NULL) {
574 if ((error = ng_make_node_common(type, nodepp)) == 0) {
575 if ((error = ((*type->constructor)(*nodepp)) != 0)) {
576 NG_NODE_UNREF(*nodepp);
581 * Node has no constructor. We cannot ask for one
582 * to be made. It must be brought into existance by
583 * some external agency. The external agency should
584 * call ng_make_node_common() directly to get the
585 * netgraph part initialised.
594 * Generic node creation. Called by node initialisation for externally
595 * instantiated nodes (e.g. hardware, sockets, etc ).
596 * The returned node has a reference count of 1.
599 ng_make_node_common(struct ng_type *type, node_p *nodepp)
603 /* Require the node type to have been already installed */
604 if (ng_findtype(type->name) == NULL) {
609 /* Make a node and try attach it to the type */
615 node->nd_type = type;
616 NG_NODE_REF(node); /* note reference */
619 mtx_init(&node->nd_input_queue.q_mtx, "ng_node", MTX_SPIN);
620 node->nd_input_queue.queue = NULL;
621 node->nd_input_queue.last = &node->nd_input_queue.queue;
622 node->nd_input_queue.q_flags = 0;
623 node->nd_input_queue.q_node = node;
625 /* Initialize hook list for new node */
626 LIST_INIT(&node->nd_hooks);
628 /* Link us into the node linked list */
629 mtx_lock(&ng_nodelist_mtx);
630 LIST_INSERT_HEAD(&ng_nodelist, node, nd_nodes);
631 mtx_unlock(&ng_nodelist_mtx);
634 /* get an ID and put us in the hash chain */
635 mtx_lock(&ng_idhash_mtx);
636 for (;;) { /* wrap protection, even if silly */
638 node->nd_ID = nextID++; /* 137/second for 1 year before wrap */
640 /* Is there a problem with the new number? */
641 NG_IDHASH_FIND(node->nd_ID, node2); /* already taken? */
642 if ((node->nd_ID != 0) && (node2 == NULL)) {
646 LIST_INSERT_HEAD(&ng_ID_hash[NG_IDHASH_FN(node->nd_ID)],
648 mtx_unlock(&ng_idhash_mtx);
656 * Forceably start the shutdown process on a node. Either call
657 * it's shutdown method, or do the default shutdown if there is
658 * no type-specific method.
660 * We can only be called form a shutdown message, so we know we have
661 * a writer lock, and therefore exclusive access. It also means
662 * that we should not be on the work queue, but we check anyhow.
664 * Persistent node types must have a type-specific method which
665 * Allocates a new node in which case, this one is irretrievably going away,
666 * or cleans up anything it needs, and just makes the node valid again,
667 * in which case we allow the node to survive.
669 * XXX We need to think of how to tell a persistant node that we
670 * REALLY need to go away because the hardware has gone or we
671 * are rebooting.... etc.
674 ng_rmnode(node_p node, hook_p dummy1, void *dummy2, int dummy3)
678 /* Check if it's already shutting down */
679 if ((node->nd_flags & NG_CLOSING) != 0)
682 if (node == &ng_deadnode) {
683 printf ("shutdown called on deadnode\n");
687 /* Add an extra reference so it doesn't go away during this */
691 * Mark it invalid so any newcomers know not to try use it
692 * Also add our own mark so we can't recurse
693 * note that NG_INVALID does not do this as it's also set during
696 node->nd_flags |= NG_INVALID|NG_CLOSING;
698 /* Notify all remaining connected nodes to disconnect */
699 while ((hook = LIST_FIRST(&node->nd_hooks)) != NULL)
700 ng_destroy_hook(hook);
703 * Drain the input queue forceably.
704 * it has no hooks so what's it going to do, bleed on someone?
705 * Theoretically we came here from a queue entry that was added
706 * Just before the queue was closed, so it should be empty anyway.
707 * Also removes us from worklist if needed.
709 ng_flush_input_queue(&node->nd_input_queue);
711 /* Ask the type if it has anything to do in this case */
712 if (node->nd_type && node->nd_type->shutdown) {
713 (*node->nd_type->shutdown)(node);
714 if (NG_NODE_IS_VALID(node)) {
716 * Well, blow me down if the node code hasn't declared
717 * that it doesn't want to die.
718 * Presumably it is a persistant node.
719 * If we REALLY want it to go away,
720 * e.g. hardware going away,
721 * Our caller should set NG_REALLY_DIE in nd_flags.
723 node->nd_flags &= ~(NG_INVALID|NG_CLOSING);
724 NG_NODE_UNREF(node); /* Assume they still have theirs */
727 } else { /* do the default thing */
731 ng_unname(node); /* basically a NOP these days */
734 * Remove extra reference, possibly the last
735 * Possible other holders of references may include
736 * timeout callouts, but theoretically the node's supposed to
737 * have cancelled them. Possibly hardware dependencies may
738 * force a driver to 'linger' with a reference.
743 #ifdef NETGRAPH_DEBUG
745 ng_ref_node(node_p node)
752 * Remove a reference to the node, possibly the last.
753 * deadnode always acts as it it were the last.
756 ng_unref_node(node_p node)
760 if (node == &ng_deadnode) {
765 v = node->nd_refs - 1;
766 } while (! atomic_cmpset_int(&node->nd_refs, v + 1, v));
768 if (v == 0) { /* we were the last */
770 mtx_lock(&ng_nodelist_mtx);
771 node->nd_type->refs--; /* XXX maybe should get types lock? */
772 LIST_REMOVE(node, nd_nodes);
773 mtx_unlock(&ng_nodelist_mtx);
775 mtx_lock(&ng_idhash_mtx);
776 LIST_REMOVE(node, nd_idnodes);
777 mtx_unlock(&ng_idhash_mtx);
779 mtx_destroy(&node->nd_input_queue.q_mtx);
785 /************************************************************************
787 ************************************************************************/
789 ng_ID2noderef(ng_ID_t ID)
792 mtx_lock(&ng_idhash_mtx);
793 NG_IDHASH_FIND(ID, node);
796 mtx_unlock(&ng_idhash_mtx);
801 ng_node2ID(node_p node)
803 return (node ? NG_NODE_ID(node) : 0);
806 /************************************************************************
808 ************************************************************************/
811 * Assign a node a name. Once assigned, the name cannot be changed.
814 ng_name_node(node_p node, const char *name)
819 /* Check the name is valid */
820 for (i = 0; i < NG_NODELEN + 1; i++) {
821 if (name[i] == '\0' || name[i] == '.' || name[i] == ':')
824 if (i == 0 || name[i] != '\0') {
828 if (ng_decodeidname(name) != 0) { /* valid IDs not allowed here */
833 /* Check the name isn't already being used */
834 if ((node2 = ng_name2noderef(node, name)) != NULL) {
835 NG_NODE_UNREF(node2);
841 strncpy(NG_NODE_NAME(node), name, NG_NODELEN);
847 * Find a node by absolute name. The name should NOT end with ':'
848 * The name "." means "this node" and "[xxx]" means "the node
849 * with ID (ie, at address) xxx".
851 * Returns the node if found, else NULL.
852 * Eventually should add something faster than a sequential search.
853 * Note it aquires a reference on the node so you can be sure it's still there.
856 ng_name2noderef(node_p here, const char *name)
861 /* "." means "this node" */
862 if (strcmp(name, ".") == 0) {
867 /* Check for name-by-ID */
868 if ((temp = ng_decodeidname(name)) != 0) {
869 return (ng_ID2noderef(temp));
872 /* Find node by name */
873 mtx_lock(&ng_nodelist_mtx);
874 LIST_FOREACH(node, &ng_nodelist, nd_nodes) {
875 if (NG_NODE_IS_VALID(node)
876 && NG_NODE_HAS_NAME(node)
877 && (strcmp(NG_NODE_NAME(node), name) == 0)) {
883 mtx_unlock(&ng_nodelist_mtx);
888 * Decode a ID name, eg. "[f03034de]". Returns 0 if the
889 * string is not valid, otherwise returns the value.
892 ng_decodeidname(const char *name)
894 const int len = strlen(name);
898 /* Check for proper length, brackets, no leading junk */
901 || (name[len - 1] != ']')
902 || (!isxdigit(name[1]))) {
907 val = strtoul(name + 1, &eptr, 16);
908 if ((eptr - name != len - 1)
909 || (val == ULONG_MAX)
917 * Remove a name from a node. This should only be called
918 * when shutting down and removing the node.
919 * IF we allow name changing this may be more resurected.
922 ng_unname(node_p node)
926 /************************************************************************
928 Names are not optional. Hooks are always connected, except for a
929 brief moment within these routines. On invalidation or during creation
930 they are connected to the 'dead' hook.
931 ************************************************************************/
934 * Remove a hook reference
937 ng_unref_hook(hook_p hook)
941 if (hook == &ng_deadhook) {
946 } while (! atomic_cmpset_int(&hook->hk_refs, v, v - 1));
948 if (v == 1) { /* we were the last */
949 if (_NG_HOOK_NODE(hook)) { /* it'll probably be ng_deadnode */
950 _NG_NODE_UNREF((_NG_HOOK_NODE(hook)));
951 hook->hk_node = NULL;
958 * Add an unconnected hook to a node. Only used internally.
959 * Assumes node is locked. (XXX not yet true )
962 ng_add_hook(node_p node, const char *name, hook_p *hookp)
967 /* Check that the given name is good */
972 if (ng_findhook(node, name) != NULL) {
977 /* Allocate the hook and link it up */
983 hook->hk_refs = 1; /* add a reference for us to return */
984 hook->hk_flags = HK_INVALID;
985 hook->hk_peer = &ng_deadhook; /* start off this way */
986 hook->hk_node = node;
987 NG_NODE_REF(node); /* each hook counts as a reference */
990 strncpy(NG_HOOK_NAME(hook), name, NG_HOOKLEN);
993 * Check if the node type code has something to say about it
994 * If it fails, the unref of the hook will also unref the node.
996 if (node->nd_type->newhook != NULL) {
997 if ((error = (*node->nd_type->newhook)(node, hook, name))) {
998 NG_HOOK_UNREF(hook); /* this frees the hook */
1003 * The 'type' agrees so far, so go ahead and link it in.
1004 * We'll ask again later when we actually connect the hooks.
1006 LIST_INSERT_HEAD(&node->nd_hooks, hook, hk_hooks);
1007 node->nd_numhooks++;
1008 NG_HOOK_REF(hook); /* one for the node */
1018 * Node types may supply their own optimized routines for finding
1019 * hooks. If none is supplied, we just do a linear search.
1020 * XXX Possibly we should add a reference to the hook?
1023 ng_findhook(node_p node, const char *name)
1027 if (node->nd_type->findhook != NULL)
1028 return (*node->nd_type->findhook)(node, name);
1029 LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) {
1030 if (NG_HOOK_IS_VALID(hook)
1031 && (strcmp(NG_HOOK_NAME(hook), name) == 0))
1040 * As hooks are always attached, this really destroys two hooks.
1041 * The one given, and the one attached to it. Disconnect the hooks
1042 * from each other first. We reconnect the peer hook to the 'dead'
1043 * hook so that it can still exist after we depart. We then
1044 * send the peer its own destroy message. This ensures that we only
1045 * interact with the peer's structures when it is locked processing that
1046 * message. We hold a reference to the peer hook so we are guaranteed that
1047 * the peer hook and node are still going to exist until
1048 * we are finished there as the hook holds a ref on the node.
1049 * We run this same code again on the peer hook, but that time it is already
1050 * attached to the 'dead' hook.
1052 * This routine is called at all stages of hook creation
1053 * on error detection and must be able to handle any such stage.
1056 ng_destroy_hook(hook_p hook)
1058 hook_p peer = NG_HOOK_PEER(hook);
1059 node_p node = NG_HOOK_NODE(hook);
1061 if (hook == &ng_deadhook) { /* better safe than sorry */
1062 printf("ng_destroy_hook called on deadhook\n");
1065 hook->hk_flags |= HK_INVALID; /* as soon as possible */
1066 if (peer && (peer != &ng_deadhook)) {
1068 * Set the peer to point to ng_deadhook
1069 * from this moment on we are effectively independent it.
1070 * send it an rmhook message of it's own.
1072 peer->hk_peer = &ng_deadhook; /* They no longer know us */
1073 hook->hk_peer = &ng_deadhook; /* Nor us, them */
1074 if (NG_HOOK_NODE(peer) == &ng_deadnode) {
1076 * If it's already divorced from a node,
1081 ng_rmhook_self(peer); /* Send it a surprise */
1083 NG_HOOK_UNREF(peer); /* account for peer link */
1084 NG_HOOK_UNREF(hook); /* account for peer link */
1088 * Remove the hook from the node's list to avoid possible recursion
1089 * in case the disconnection results in node shutdown.
1091 if (node == &ng_deadnode) { /* happens if called from ng_con_nodes() */
1094 LIST_REMOVE(hook, hk_hooks);
1095 node->nd_numhooks--;
1096 if (node->nd_type->disconnect) {
1098 * The type handler may elect to destroy the node so don't
1099 * trust its existance after this point. (except
1100 * that we still hold a reference on it. (which we
1101 * inherrited from the hook we are destroying)
1103 (*node->nd_type->disconnect) (hook);
1107 * Note that because we will point to ng_deadnode, the original node
1108 * is not decremented automatically so we do that manually.
1110 _NG_HOOK_NODE(hook) = &ng_deadnode;
1111 NG_NODE_UNREF(node); /* We no longer point to it so adjust count */
1112 NG_HOOK_UNREF(hook); /* Account for linkage (in list) to node */
1116 * Take two hooks on a node and merge the connection so that the given node
1117 * is effectively bypassed.
1120 ng_bypass(hook_p hook1, hook_p hook2)
1122 if (hook1->hk_node != hook2->hk_node) {
1126 hook1->hk_peer->hk_peer = hook2->hk_peer;
1127 hook2->hk_peer->hk_peer = hook1->hk_peer;
1129 hook1->hk_peer = &ng_deadhook;
1130 hook2->hk_peer = &ng_deadhook;
1132 /* XXX If we ever cache methods on hooks update them as well */
1133 ng_destroy_hook(hook1);
1134 ng_destroy_hook(hook2);
1139 * Install a new netgraph type
1142 ng_newtype(struct ng_type *tp)
1144 const size_t namelen = strlen(tp->name);
1146 /* Check version and type name fields */
1147 if ((tp->version != NG_ABI_VERSION)
1149 || (namelen > NG_TYPELEN)) {
1154 /* Check for name collision */
1155 if (ng_findtype(tp->name) != NULL) {
1161 /* Link in new type */
1162 mtx_lock(&ng_typelist_mtx);
1163 LIST_INSERT_HEAD(&ng_typelist, tp, types);
1164 tp->refs = 1; /* first ref is linked list */
1165 mtx_unlock(&ng_typelist_mtx);
1170 * unlink a netgraph type
1171 * If no examples exist
1174 ng_rmtype(struct ng_type *tp)
1176 /* Check for name collision */
1177 if (tp->refs != 1) {
1183 mtx_lock(&ng_typelist_mtx);
1184 LIST_REMOVE(tp, types);
1185 mtx_unlock(&ng_typelist_mtx);
1190 * Look for a type of the name given
1193 ng_findtype(const char *typename)
1195 struct ng_type *type;
1197 mtx_lock(&ng_typelist_mtx);
1198 LIST_FOREACH(type, &ng_typelist, types) {
1199 if (strcmp(type->name, typename) == 0)
1202 mtx_unlock(&ng_typelist_mtx);
1206 /************************************************************************
1208 ************************************************************************/
1210 * Connect two nodes using the specified hooks, using queued functions.
1213 ng_con_part3(node_p node, hook_p hook, void *arg1, int arg2)
1217 * When we run, we know that the node 'node' is locked for us.
1218 * Our caller has a reference on the hook.
1219 * Our caller has a reference on the node.
1220 * (In this case our caller is ng_apply_item() ).
1221 * The peer hook has a reference on the hook.
1222 * We are all set up except for the final call to the node, and
1223 * the clearing of the INVALID flag.
1225 if (NG_HOOK_NODE(hook) == &ng_deadnode) {
1227 * The node must have been freed again since we last visited
1228 * here. ng_destry_hook() has this effect but nothing else does.
1229 * We should just release our references and
1230 * free anything we can think of.
1231 * Since we know it's been destroyed, and it's our caller
1232 * that holds the references, just return.
1236 if (hook->hk_node->nd_type->connect) {
1237 if ((*hook->hk_node->nd_type->connect) (hook)) {
1238 ng_destroy_hook(hook); /* also zaps peer */
1239 printf("failed in ng_con_part3()\n");
1244 * XXX this is wrong for SMP. Possibly we need
1245 * to separate out 'create' and 'invalid' flags.
1246 * should only set flags on hooks we have locked under our node.
1248 hook->hk_flags &= ~HK_INVALID;
1253 ng_con_part2(node_p node, hook_p hook, void *arg1, int arg2)
1257 * When we run, we know that the node 'node' is locked for us.
1258 * Our caller has a reference on the hook.
1259 * Our caller has a reference on the node.
1260 * (In this case our caller is ng_apply_item() ).
1261 * The peer hook has a reference on the hook.
1262 * our node pointer points to the 'dead' node.
1263 * First check the hook name is unique.
1264 * Should not happen because we checked before queueing this.
1266 if (ng_findhook(node, NG_HOOK_NAME(hook)) != NULL) {
1268 ng_destroy_hook(hook); /* should destroy peer too */
1269 printf("failed in ng_con_part2()\n");
1273 * Check if the node type code has something to say about it
1274 * If it fails, the unref of the hook will also unref the attached node,
1275 * however since that node is 'ng_deadnode' this will do nothing.
1276 * The peer hook will also be destroyed.
1278 if (node->nd_type->newhook != NULL) {
1279 if ((*node->nd_type->newhook)(node, hook, hook->hk_name)) {
1280 ng_destroy_hook(hook); /* should destroy peer too */
1281 printf("failed in ng_con_part2()\n");
1287 * The 'type' agrees so far, so go ahead and link it in.
1288 * We'll ask again later when we actually connect the hooks.
1290 hook->hk_node = node; /* just overwrite ng_deadnode */
1291 NG_NODE_REF(node); /* each hook counts as a reference */
1292 LIST_INSERT_HEAD(&node->nd_hooks, hook, hk_hooks);
1293 node->nd_numhooks++;
1294 NG_HOOK_REF(hook); /* one for the node */
1297 * We now have a symetrical situation, where both hooks have been
1298 * linked to their nodes, the newhook methods have been called
1299 * And the references are all correct. The hooks are still marked
1300 * as invalid, as we have not called the 'connect' methods
1302 * We can call the local one immediatly as we have the
1303 * node locked, but we need to queue the remote one.
1305 if (hook->hk_node->nd_type->connect) {
1306 if ((*hook->hk_node->nd_type->connect) (hook)) {
1307 ng_destroy_hook(hook); /* also zaps peer */
1308 printf("failed in ng_con_part2(A)\n");
1312 if (ng_send_fn(hook->hk_peer->hk_node, hook->hk_peer,
1313 &ng_con_part3, arg1, arg2)) {
1314 printf("failed in ng_con_part2(B)");
1315 ng_destroy_hook(hook); /* also zaps peer */
1318 hook->hk_flags &= ~HK_INVALID; /* need both to be able to work */
1323 * Connect this node with another node. We assume that this node is
1324 * currently locked, as we are only called from an NGM_CONNECT message.
1327 ng_con_nodes(node_p node, const char *name, node_p node2, const char *name2)
1333 if (ng_findhook(node2, name2) != NULL) {
1336 if ((error = ng_add_hook(node, name, &hook))) /* gives us a ref */
1338 /* Allocate the other hook and link it up */
1339 NG_ALLOC_HOOK(hook2);
1342 ng_destroy_hook(hook); /* XXX check ref counts so far */
1343 NG_HOOK_UNREF(hook); /* including our ref */
1346 hook2->hk_refs = 1; /* start with a reference for us. */
1347 hook2->hk_flags = HK_INVALID;
1348 hook2->hk_peer = hook; /* Link the two together */
1349 hook->hk_peer = hook2;
1350 NG_HOOK_REF(hook); /* Add a ref for the peer to each*/
1352 hook2->hk_node = &ng_deadnode;
1353 strncpy(NG_HOOK_NAME(hook2), name2, NG_HOOKLEN);
1356 * Queue the function above.
1357 * Procesing continues in that function in the lock context of
1360 ng_send_fn(node2, hook2, &ng_con_part2, NULL, 0);
1362 NG_HOOK_UNREF(hook); /* Let each hook go if it wants to */
1363 NG_HOOK_UNREF(hook2);
1368 * Make a peer and connect.
1369 * We assume that the local node is locked.
1370 * The new node probably doesn't need a lock until
1371 * it has a hook, because it cannot really have any work until then,
1372 * but we should think about it a bit more.
1374 * The problem may come if the other node also fires up
1375 * some hardware or a timer or some other source of activation,
1376 * also it may already get a command msg via it's ID.
1378 * We could use the same method as ng_con_nodes() but we'd have
1379 * to add ability to remove the node when failing. (Not hard, just
1380 * make arg1 point to the node to remove).
1381 * Unless of course we just ignore failure to connect and leave
1382 * an unconnected node?
1385 ng_mkpeer(node_p node, const char *name, const char *name2, char *type)
1392 if ((error = ng_make_node(type, &node2))) {
1396 if ((error = ng_add_hook(node, name, &hook1))) { /* gives us a ref */
1397 ng_rmnode(node2, NULL, NULL, 0);
1401 if ((error = ng_add_hook(node2, name2, &hook2))) {
1402 ng_rmnode(node2, NULL, NULL, 0);
1403 ng_destroy_hook(hook1);
1404 NG_HOOK_UNREF(hook1);
1409 * Actually link the two hooks together.
1411 hook1->hk_peer = hook2;
1412 hook2->hk_peer = hook1;
1414 /* Each hook is referenced by the other */
1418 /* Give each node the opportunity to veto the pending connection */
1419 if (hook1->hk_node->nd_type->connect) {
1420 error = (*hook1->hk_node->nd_type->connect) (hook1);
1423 if ((error == 0) && hook2->hk_node->nd_type->connect) {
1424 error = (*hook2->hk_node->nd_type->connect) (hook2);
1429 * drop the references we were holding on the two hooks.
1432 ng_destroy_hook(hook2); /* also zaps hook1 */
1433 ng_rmnode(node2, NULL, NULL, 0);
1435 /* As a last act, allow the hooks to be used */
1436 hook1->hk_flags &= ~HK_INVALID;
1437 hook2->hk_flags &= ~HK_INVALID;
1439 NG_HOOK_UNREF(hook1);
1440 NG_HOOK_UNREF(hook2);
1444 /************************************************************************
1445 Utility routines to send self messages
1446 ************************************************************************/
1448 /* Shut this node down as soon as everyone is clear of it */
1449 /* Should add arg "immediatly" to jump the queue */
1451 ng_rmnode_self(node_p node)
1455 if (node == &ng_deadnode)
1457 node->nd_flags |= NG_INVALID;
1458 if (node->nd_flags & NG_CLOSING)
1461 error = ng_send_fn(node, NULL, &ng_rmnode, NULL, 0);
1466 ng_rmhook_part2(node_p node, hook_p hook, void *arg1, int arg2)
1468 ng_destroy_hook(hook);
1473 ng_rmhook_self(hook_p hook)
1476 node_p node = NG_HOOK_NODE(hook);
1478 if (node == &ng_deadnode)
1481 error = ng_send_fn(node, hook, &ng_rmhook_part2, NULL, 0);
1485 /***********************************************************************
1486 * Parse and verify a string of the form: <NODE:><PATH>
1488 * Such a string can refer to a specific node or a specific hook
1489 * on a specific node, depending on how you look at it. In the
1490 * latter case, the PATH component must not end in a dot.
1492 * Both <NODE:> and <PATH> are optional. The <PATH> is a string
1493 * of hook names separated by dots. This breaks out the original
1494 * string, setting *nodep to "NODE" (or NULL if none) and *pathp
1495 * to "PATH" (or NULL if degenerate). Also, *hookp will point to
1496 * the final hook component of <PATH>, if any, otherwise NULL.
1498 * This returns -1 if the path is malformed. The char ** are optional.
1499 ***********************************************************************/
1501 ng_path_parse(char *addr, char **nodep, char **pathp, char **hookp)
1503 char *node, *path, *hook;
1507 * Extract absolute NODE, if any
1509 for (path = addr; *path && *path != ':'; path++);
1511 node = addr; /* Here's the NODE */
1512 *path++ = '\0'; /* Here's the PATH */
1514 /* Node name must not be empty */
1518 /* A name of "." is OK; otherwise '.' not allowed */
1519 if (strcmp(node, ".") != 0) {
1520 for (k = 0; node[k]; k++)
1525 node = NULL; /* No absolute NODE */
1526 path = addr; /* Here's the PATH */
1529 /* Snoop for illegal characters in PATH */
1530 for (k = 0; path[k]; k++)
1534 /* Check for no repeated dots in PATH */
1535 for (k = 0; path[k]; k++)
1536 if (path[k] == '.' && path[k + 1] == '.')
1539 /* Remove extra (degenerate) dots from beginning or end of PATH */
1542 if (*path && path[strlen(path) - 1] == '.')
1543 path[strlen(path) - 1] = 0;
1545 /* If PATH has a dot, then we're not talking about a hook */
1547 for (hook = path, k = 0; path[k]; k++)
1548 if (path[k] == '.') {
1566 * Given a path, which may be absolute or relative, and a starting node,
1567 * return the destination node.
1570 ng_path2noderef(node_p here, const char *address,
1571 node_p *destp, hook_p *lasthook)
1573 char fullpath[NG_PATHLEN + 1];
1574 char *nodename, *path, pbuf[2];
1575 node_p node, oldnode;
1580 if (destp == NULL) {
1586 /* Make a writable copy of address for ng_path_parse() */
1587 strncpy(fullpath, address, sizeof(fullpath) - 1);
1588 fullpath[sizeof(fullpath) - 1] = '\0';
1590 /* Parse out node and sequence of hooks */
1591 if (ng_path_parse(fullpath, &nodename, &path, NULL) < 0) {
1596 pbuf[0] = '.'; /* Needs to be writable */
1602 * For an absolute address, jump to the starting node.
1603 * Note that this holds a reference on the node for us.
1604 * Don't forget to drop the reference if we don't need it.
1607 node = ng_name2noderef(here, nodename);
1622 * Now follow the sequence of hooks
1624 * We actually cannot guarantee that the sequence
1625 * is not being demolished as we crawl along it
1626 * without extra-ordinary locking etc.
1627 * So this is a bit dodgy to say the least.
1628 * We can probably hold up some things by holding
1629 * the nodelist mutex for the time of this
1630 * crawl if we wanted.. At least that way we wouldn't have to
1631 * worry about the nodes dissappearing, but the hooks would still
1634 for (cp = path; node != NULL && *cp != '\0'; ) {
1638 * Break out the next path segment. Replace the dot we just
1639 * found with a NUL; "cp" points to the next segment (or the
1642 for (segment = cp; *cp != '\0'; cp++) {
1650 if (*segment == '\0')
1653 /* We have a segment, so look for a hook by that name */
1654 hook = ng_findhook(node, segment);
1656 /* Can't get there from here... */
1658 || NG_HOOK_PEER(hook) == NULL
1659 || NG_HOOK_NOT_VALID(hook)
1660 || NG_HOOK_NOT_VALID(NG_HOOK_PEER(hook))) {
1662 NG_NODE_UNREF(node);
1664 printf("hooknotvalid %s %s %d %d %d %d ",
1668 NG_HOOK_PEER(hook) == NULL,
1669 NG_HOOK_NOT_VALID(hook),
1670 NG_HOOK_NOT_VALID(NG_HOOK_PEER(hook)));
1676 * Hop on over to the next node
1678 * Big race conditions here as hooks and nodes go away
1679 * *** Idea.. store an ng_ID_t in each hook and use that
1680 * instead of the direct hook in this crawl?
1683 if ((node = NG_PEER_NODE(hook)))
1684 NG_NODE_REF(node); /* XXX RACE */
1685 NG_NODE_UNREF(oldnode); /* XXX another race */
1686 if (NG_NODE_NOT_VALID(node)) {
1687 NG_NODE_UNREF(node); /* XXX more races */
1692 /* If node somehow missing, fail here (probably this is not needed) */
1700 if (lasthook != NULL)
1701 *lasthook = (hook ? NG_HOOK_PEER(hook) : NULL);
1705 /***************************************************************\
1706 * Input queue handling.
1707 * All activities are submitted to the node via the input queue
1708 * which implements a multiple-reader/single-writer gate.
1709 * Items which cannot be handled immeditly are queued.
1711 * read-write queue locking inline functions *
1712 \***************************************************************/
1714 static __inline item_p ng_dequeue(struct ng_queue * ngq);
1715 static __inline item_p ng_acquire_read(struct ng_queue * ngq,
1717 static __inline item_p ng_acquire_write(struct ng_queue * ngq,
1719 static __inline void ng_leave_read(struct ng_queue * ngq);
1720 static __inline void ng_leave_write(struct ng_queue * ngq);
1721 static __inline void ng_queue_rw(struct ng_queue * ngq,
1722 item_p item, int rw);
1725 * Definition of the bits fields in the ng_queue flag word.
1726 * Defined here rather than in netgraph.h because no-one should fiddle
1729 * The ordering here may be important! don't shuffle these.
1732 Safety Barrier--------+ (adjustable to suit taste) (not used yet)
1735 +-------+-------+-------+-------+-------+-------+-------+-------+
1736 | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
1737 | |A|c|t|i|v|e| |R|e|a|d|e|r| |C|o|u|n|t| | | | | | | | | |R|A|W|
1738 | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |P|W|P|
1739 +-------+-------+-------+-------+-------+-------+-------+-------+
1740 \___________________________ ____________________________/ | | |
1742 [active reader count] | | |
1744 Read Pending ------------------------------------+ | |
1746 Active Writer -------------------------------------+ |
1748 Write Pending ---------------------------------------+
1752 #define WRITE_PENDING 0x00000001
1753 #define WRITER_ACTIVE 0x00000002
1754 #define READ_PENDING 0x00000004
1755 #define READER_INCREMENT 0x00000008
1756 #define READER_MASK 0xfffffff0 /* Not valid if WRITER_ACTIVE is set */
1757 #define SAFETY_BARRIER 0x00100000 /* 64K items queued should be enough */
1759 /* Defines of more elaborate states on the queue */
1760 /* Mask of bits a read cares about */
1761 #define NGQ_RMASK (WRITE_PENDING|WRITER_ACTIVE|READ_PENDING)
1763 /* Mask of bits a write cares about */
1764 #define NGQ_WMASK (NGQ_RMASK|READER_MASK)
1766 /* tests to decide if we could get a read or write off the queue */
1767 #define CAN_GET_READ(flag) ((flag & NGQ_RMASK) == READ_PENDING)
1768 #define CAN_GET_WRITE(flag) ((flag & NGQ_WMASK) == WRITE_PENDING)
1770 /* Is there a chance of getting ANY work off the queue? */
1771 #define CAN_GET_WORK(flag) (CAN_GET_READ(flag) || CAN_GET_WRITE(flag))
1774 * Taking into account the current state of the queue and node, possibly take
1775 * the next entry off the queue and return it. Return NULL if there was
1776 * nothing we could return, either because there really was nothing there, or
1777 * because the node was in a state where it cannot yet process the next item
1780 * This MUST MUST MUST be called with the mutex held.
1782 static __inline item_p
1783 ng_dequeue(struct ng_queue *ngq)
1788 if (CAN_GET_READ(ngq->q_flags)) {
1790 * Head of queue is a reader and we have no write active.
1791 * We don't care how many readers are already active.
1792 * Adjust the flags for the item we are about to dequeue.
1793 * Add the correct increment for the reader count as well.
1795 add_arg = (READER_INCREMENT - READ_PENDING);
1796 } else if (CAN_GET_WRITE(ngq->q_flags)) {
1798 * There is a pending write, no readers and no active writer.
1799 * This means we can go ahead with the pending writer. Note
1800 * the fact that we now have a writer, ready for when we take
1803 * We don't need to worry about a possible collision with the
1806 * The fasttrack thread may take a long time to discover that we
1807 * are running so we would have an inconsistent state in the
1808 * flags for a while. Since we ignore the reader count
1809 * entirely when the WRITER_ACTIVE flag is set, this should
1810 * not matter (in fact it is defined that way). If it tests
1811 * the flag before this operation, the WRITE_PENDING flag
1812 * will make it fail, and if it tests it later, the
1813 * WRITER_ACTIVE flag will do the same. If it is SO slow that
1814 * we have actually completed the operation, and neither flag
1815 * is set (nor the READ_PENDING) by the time that it tests
1816 * the flags, then it is actually ok for it to continue. If
1817 * it completes and we've finished and the read pending is
1818 * set it still fails.
1820 * So we can just ignore it, as long as we can ensure that the
1821 * transition from WRITE_PENDING state to the WRITER_ACTIVE
1824 * After failing, first it will be held back by the mutex, then
1825 * when it can proceed, it will queue its request, then it
1826 * would arrive at this function. Usually it will have to
1827 * leave empty handed because the ACTIVE WRITER bit will be
1830 * Adjust the flags for the item we are about to dequeue
1831 * and for the new active writer.
1833 add_arg = (WRITER_ACTIVE - WRITE_PENDING);
1835 * We want to write "active writer, no readers " Now go make
1836 * it true. In fact there may be a number in the readers
1837 * count but we know it is not true and will be fixed soon.
1838 * We will fix the flags for the next pending entry in a
1843 * We can't dequeue anything.. return and say so. Probably we
1844 * have a write pending and the readers count is non zero. If
1845 * we got here because a reader hit us just at the wrong
1846 * moment with the fasttrack code, and put us in a strange
1847 * state, then it will be through in just a moment, (as soon
1848 * as we release the mutex) and keep things moving.
1849 * Make sure we remove ourselves from the work queue.
1851 ng_worklist_remove(ngq->q_node);
1856 * Now we dequeue the request (whatever it may be) and correct the
1857 * pending flags and the next and last pointers.
1860 ngq->queue = item->el_next;
1861 if (ngq->last == &(item->el_next)) {
1863 * that was the last entry in the queue so set the 'last
1864 * pointer up correctly and make sure the pending flags are
1867 ngq->last = &(ngq->queue);
1869 * Whatever flag was set will be cleared and
1870 * the new acive field will be set by the add as well,
1871 * so we don't need to change add_arg.
1872 * But we know we don't need to be on the work list.
1874 atomic_add_long(&ngq->q_flags, add_arg);
1875 ng_worklist_remove(ngq->q_node);
1878 * Since there is something on the queue, note what it is
1879 * in the flags word.
1881 if ((ngq->queue->el_flags & NGQF_RW) == NGQF_READER) {
1882 add_arg += READ_PENDING;
1884 add_arg += WRITE_PENDING;
1886 atomic_add_long(&ngq->q_flags, add_arg);
1888 * If we see more doable work, make sure we are
1889 * on the work queue.
1891 if (CAN_GET_WORK(ngq->q_flags)) {
1892 ng_setisr(ngq->q_node);
1896 * We have successfully cleared the old pending flag, set the new one
1897 * if it is needed, and incremented the appropriate active field.
1898 * (all in one atomic addition.. )
1904 * Queue a packet to be picked up by someone else.
1905 * We really don't care who, but we can't or don't want to hang around
1906 * to process it ourselves. We are probably an interrupt routine..
1907 * 1 = writer, 0 = reader
1911 static __inline void
1912 ng_queue_rw(struct ng_queue * ngq, item_p item, int rw)
1914 item->el_next = NULL; /* maybe not needed */
1917 * If it was the first item in the queue then we need to
1918 * set the last pointer and the type flags.
1920 if (ngq->last == &(ngq->queue)) {
1922 * When called with constants for rw, the optimiser will
1923 * remove the unneeded branch below.
1925 if (rw == NGQRW_W) {
1926 atomic_add_long(&ngq->q_flags, WRITE_PENDING);
1928 atomic_add_long(&ngq->q_flags, READ_PENDING);
1931 ngq->last = &(item->el_next);
1936 * This function 'cheats' in that it first tries to 'grab' the use of the
1937 * node, without going through the mutex. We can do this becasue of the
1938 * semantics of the lock. The semantics include a clause that says that the
1939 * value of the readers count is invalid if the WRITER_ACTIVE flag is set. It
1940 * also says that the WRITER_ACTIVE flag cannot be set if the readers count
1941 * is not zero. Note that this talks about what is valid to SET the
1942 * WRITER_ACTIVE flag, because from the moment it is set, the value if the
1943 * reader count is immaterial, and not valid. The two 'pending' flags have a
1944 * similar effect, in that If they are orthogonal to the two active fields in
1945 * how they are set, but if either is set, the attempted 'grab' need to be
1946 * backed out because there is earlier work, and we maintain ordering in the
1947 * queue. The result of this is that the reader request can try obtain use of
1948 * the node with only a single atomic addition, and without any of the mutex
1949 * overhead. If this fails the operation degenerates to the same as for other
1953 static __inline item_p
1954 ng_acquire_read(struct ng_queue *ngq, item_p item)
1957 /* ######### Hack alert ######### */
1958 atomic_add_long(&ngq->q_flags, READER_INCREMENT);
1959 if ((ngq->q_flags & NGQ_RMASK) == 0) {
1960 /* Successfully grabbed node */
1963 /* undo the damage if we didn't succeed */
1964 atomic_subtract_long(&ngq->q_flags, READER_INCREMENT);
1966 /* ######### End Hack alert ######### */
1967 mtx_lock_spin((&ngq->q_mtx));
1969 * Try again. Another processor (or interrupt for that matter) may
1970 * have removed the last queued item that was stopping us from
1971 * running, between the previous test, and the moment that we took
1972 * the mutex. (Or maybe a writer completed.)
1974 if ((ngq->q_flags & NGQ_RMASK) == 0) {
1975 atomic_add_long(&ngq->q_flags, READER_INCREMENT);
1976 mtx_unlock_spin((&ngq->q_mtx));
1981 * and queue the request for later.
1983 item->el_flags |= NGQF_READER;
1984 ng_queue_rw(ngq, item, NGQRW_R);
1987 * Ok, so that's the item successfully queued for later. So now we
1988 * see if we can dequeue something to run instead.
1990 item = ng_dequeue(ngq);
1991 mtx_unlock_spin(&(ngq->q_mtx));
1995 static __inline item_p
1996 ng_acquire_write(struct ng_queue *ngq, item_p item)
1999 mtx_lock_spin(&(ngq->q_mtx));
2001 * If there are no readers, no writer, and no pending packets, then
2002 * we can just go ahead. In all other situations we need to queue the
2005 if ((ngq->q_flags & NGQ_WMASK) == 0) {
2006 atomic_add_long(&ngq->q_flags, WRITER_ACTIVE);
2007 mtx_unlock_spin((&ngq->q_mtx));
2008 if (ngq->q_flags & READER_MASK) {
2009 /* Collision with fast-track reader */
2010 atomic_subtract_long(&ngq->q_flags, WRITER_ACTIVE);
2017 * and queue the request for later.
2019 item->el_flags &= ~NGQF_RW;
2020 ng_queue_rw(ngq, item, NGQRW_W);
2023 * Ok, so that's the item successfully queued for later. So now we
2024 * see if we can dequeue something to run instead.
2026 item = ng_dequeue(ngq);
2027 mtx_unlock_spin(&(ngq->q_mtx));
2031 static __inline void
2032 ng_leave_read(struct ng_queue *ngq)
2034 atomic_subtract_long(&ngq->q_flags, READER_INCREMENT);
2037 static __inline void
2038 ng_leave_write(struct ng_queue *ngq)
2040 atomic_subtract_long(&ngq->q_flags, WRITER_ACTIVE);
2044 ng_flush_input_queue(struct ng_queue * ngq)
2048 mtx_lock_spin(&ngq->q_mtx);
2050 /* Now take a look at what's on the queue */
2051 if (ngq->q_flags & READ_PENDING) {
2052 add_arg = -READ_PENDING;
2053 } else if (ngq->q_flags & WRITE_PENDING) {
2054 add_arg = -WRITE_PENDING;
2060 ngq->queue = item->el_next;
2061 if (ngq->last == &(item->el_next)) {
2062 ngq->last = &(ngq->queue);
2064 if ((ngq->queue->el_flags & NGQF_RW) == NGQF_READER) {
2065 add_arg += READ_PENDING;
2067 add_arg += WRITE_PENDING;
2070 atomic_add_long(&ngq->q_flags, add_arg);
2072 mtx_lock_spin(&ngq->q_mtx);
2074 mtx_unlock_spin(&ngq->q_mtx);
2077 * Take us off the work queue if we are there.
2078 * We definatly have no work to be done.
2080 ng_worklist_remove(ngq->q_node);
2081 mtx_unlock_spin(&ngq->q_mtx);
2084 /***********************************************************************
2085 * Externally visible method for sending or queueing messages or data.
2086 ***********************************************************************/
2089 * The module code should have filled out the item correctly by this stage:
2091 * reference to destination node.
2092 * Reference to destination rcv hook if relevant.
2095 * pointer to metadata
2098 * ID of original sender node. (return address)
2104 * The nodes have several routines and macros to help with this task:
2108 ng_snd_item(item_p item, int queue)
2110 hook_p hook = NGI_HOOK(item);
2111 node_p node = NGI_NODE(item);
2113 int error = 0, ierror;
2115 struct ng_queue * ngq = &node->nd_input_queue;
2117 #ifdef NETGRAPH_DEBUG
2118 _ngi_check(item, __FILE__, __LINE__);
2123 return (EINVAL); /* failed to get queue element */
2128 return (EINVAL); /* No address */
2130 switch(item->el_flags & NGQF_TYPE) {
2134 * Delivered to a node via a non-optional hook.
2135 * Both should be present in the item even though
2136 * the node is derivable from the hook.
2137 * References are held on both by the item.
2139 CHECK_DATA_MBUF(NGI_M(item));
2145 if ((NG_HOOK_NOT_VALID(hook))
2146 || (NG_NODE_NOT_VALID(NG_HOOK_NODE(hook)))) {
2150 if ((hook->hk_flags & HK_QUEUE)) {
2153 /* By default data is a reader in the locking scheme */
2154 item->el_flags |= NGQF_READER;
2160 * Delivered to a node.
2162 * References are held by the item on the node and
2163 * the hook if it is present.
2165 if (hook && (hook->hk_flags & HK_QUEUE)) {
2168 /* Data messages count as writers unles explicitly exempted */
2169 if (NGI_MSG(item)->header.cmd & NGM_READONLY) {
2170 item->el_flags |= NGQF_READER;
2173 item->el_flags &= ~NGQF_RW;
2178 item->el_flags &= ~NGQF_RW;
2187 * If the node specifies single threading, force writer semantics
2188 * Similarly the node may say one hook always produces writers.
2189 * These are over-rides.
2191 if ((node->nd_flags & NG_FORCE_WRITER)
2192 || (hook && (hook->hk_flags & HK_FORCE_WRITER))) {
2194 item->el_flags &= ~NGQF_READER;
2197 /* Put it on the queue for that node*/
2198 #ifdef NETGRAPH_DEBUG
2199 _ngi_check(item, __FILE__, __LINE__);
2201 mtx_lock_spin(&(ngq->q_mtx));
2202 ng_queue_rw(ngq, item, rw);
2204 * If there are active elements then we can rely on
2205 * them. if not we should not rely on another packet
2206 * coming here by another path,
2207 * so it is best to put us in the netisr list.
2208 * We can take the worklist lock with the node locked
2209 * BUT NOT THE REVERSE!
2211 if (CAN_GET_WORK(ngq->q_flags)) {
2214 mtx_unlock_spin(&(ngq->q_mtx));
2218 * Take a queue item and a node and see if we can apply the item to
2219 * the node. We may end up getting a different item to apply instead.
2220 * Will allow for a piggyback reply only in the case where
2221 * there is no queueing.
2226 * We already decided how we will be queueud or treated.
2227 * Try get the appropriate operating permission.
2229 if (rw == NGQRW_R) {
2230 item = ng_acquire_read(ngq, item);
2232 item = ng_acquire_write(ngq, item);
2236 * May have come back with a different item.
2237 * or maybe none at all. The one we started with will
2238 * have been queued in thises cases.
2244 #ifdef NETGRAPH_DEBUG
2245 _ngi_check(item, __FILE__, __LINE__);
2248 * Take over the reference frm the item.
2249 * Hold it until the called function returns.
2251 NGI_GET_NODE(item, node); /* zaps stored node */
2253 ierror = ng_apply_item(node, item); /* drops r/w lock when done */
2255 /* only return an error if it was our initial item.. (compat hack) */
2256 if (oitem == item) {
2261 * If the node goes away when we remove the reference,
2262 * whatever we just did caused it.. whatever we do, DO NOT
2263 * access the node again!
2265 if (NG_NODE_UNREF(node) == 0) {
2270 * Now we've handled the packet we brought, (or a friend of it) let's
2271 * look for any other packets that may have been queued up. We hold
2272 * no locks, so if someone puts something in the queue after
2273 * we check that it is empty, it is their problem
2274 * to ensure it is processed. If we have the netisr thread cme in here
2275 * while we still say we have stuff to do, we may get a boost
2276 * in SMP systems. :-)
2280 * dequeue acquires and adjusts the input_queue as it dequeues
2281 * packets. It acquires the rw lock as needed.
2283 mtx_lock_spin(&ngq->q_mtx);
2284 item = ng_dequeue(ngq); /* fixes worklist too*/
2286 mtx_unlock_spin(&ngq->q_mtx);
2289 mtx_unlock_spin(&ngq->q_mtx);
2292 * Take over the reference frm the item.
2293 * Hold it until the called function returns.
2296 NGI_GET_NODE(item, node); /* zaps stored node */
2299 * We have the appropriate lock, so run the item.
2300 * When finished it will drop the lock accordingly
2302 ierror = ng_apply_item(node, item);
2305 * only return an error if it was our initial
2306 * item.. (compat hack)
2308 if (oitem == item) {
2313 * If the node goes away when we remove the reference,
2314 * whatever we just did caused it.. whatever we do, DO NOT
2315 * access the node again!
2317 if (NG_NODE_UNREF(node) == 0) {
2325 * We have an item that was possibly queued somewhere.
2326 * It should contain all the information needed
2327 * to run it on the appropriate node/hook.
2330 ng_apply_item(node_p node, item_p item)
2333 int was_reader = ((item->el_flags & NGQF_RW));
2335 ng_rcvdata_t *rcvdata;
2336 ng_rcvmsg_t *rcvmsg;
2338 NGI_GET_HOOK(item, hook); /* clears stored hook */
2339 #ifdef NETGRAPH_DEBUG
2340 _ngi_check(item, __FILE__, __LINE__);
2342 switch (item->el_flags & NGQF_TYPE) {
2345 * Check things are still ok as when we were queued.
2348 || NG_HOOK_NOT_VALID(hook)
2349 || NG_NODE_NOT_VALID(node) ) {
2355 * If no receive method, just silently drop it.
2356 * Give preference to the hook over-ride method
2358 if ((!(rcvdata = hook->hk_rcvdata))
2359 && (!(rcvdata = NG_HOOK_NODE(hook)->nd_type->rcvdata))) {
2364 error = (*rcvdata)(hook, item);
2368 if (NG_HOOK_NOT_VALID(hook)) {
2370 * The hook has been zapped then we can't
2371 * use it. Immediatly drop its reference.
2372 * The message may not need it.
2374 NG_HOOK_UNREF(hook);
2379 * Similarly, if the node is a zombie there is
2380 * nothing we can do with it, drop everything.
2382 if (NG_NODE_NOT_VALID(node)) {
2388 * Call the appropriate message handler for the object.
2389 * It is up to the message handler to free the message.
2390 * If it's a generic message, handle it generically,
2391 * otherwise call the type's message handler
2393 * XXX (race). Remember that a queued message may
2394 * reference a node or hook that has just been
2395 * invalidated. It will exist as the queue code
2396 * is holding a reference, but..
2399 struct ng_mesg *msg = NGI_MSG(item);
2402 * check if the generic handler owns it.
2404 if ((msg->header.typecookie == NGM_GENERIC_COOKIE)
2405 && ((msg->header.flags & NGF_RESP) == 0)) {
2406 error = ng_generic_msg(node, item, hook);
2410 * Now see if there is a handler (hook or node specific)
2411 * in the target node. If none, silently discard.
2413 if (((!hook) || (!(rcvmsg = hook->hk_rcvmsg)))
2414 && (!(rcvmsg = node->nd_type->rcvmsg))) {
2420 error = (*rcvmsg)(node, item, hook);
2425 * We have to implicitly trust the hook,
2426 * as some of these are used for system purposes
2427 * where the hook is invalid. In the case of
2428 * the shutdown message we allow it to hit
2429 * even if the node is invalid.
2431 if ((NG_NODE_NOT_VALID(node))
2432 && (NGI_FN(item) != &ng_rmnode)) {
2437 (*NGI_FN(item))(node, hook, NGI_ARG1(item), NGI_ARG2(item));
2443 * We held references on some of the resources
2444 * that we took from the item. Now that we have
2445 * finished doing everything, drop those references.
2448 NG_HOOK_UNREF(hook);
2452 ng_leave_read(&node->nd_input_queue);
2454 ng_leave_write(&node->nd_input_queue);
2459 /***********************************************************************
2460 * Implement the 'generic' control messages
2461 ***********************************************************************/
2463 ng_generic_msg(node_p here, item_p item, hook_p lasthook)
2466 struct ng_mesg *msg;
2467 struct ng_mesg *resp = NULL;
2469 NGI_GET_MSG(item, msg);
2470 if (msg->header.typecookie != NGM_GENERIC_COOKIE) {
2475 switch (msg->header.cmd) {
2477 ng_rmnode(here, NULL, NULL, 0);
2481 struct ngm_mkpeer *const mkp = (struct ngm_mkpeer *) msg->data;
2483 if (msg->header.arglen != sizeof(*mkp)) {
2488 mkp->type[sizeof(mkp->type) - 1] = '\0';
2489 mkp->ourhook[sizeof(mkp->ourhook) - 1] = '\0';
2490 mkp->peerhook[sizeof(mkp->peerhook) - 1] = '\0';
2491 error = ng_mkpeer(here, mkp->ourhook, mkp->peerhook, mkp->type);
2496 struct ngm_connect *const con =
2497 (struct ngm_connect *) msg->data;
2500 if (msg->header.arglen != sizeof(*con)) {
2505 con->path[sizeof(con->path) - 1] = '\0';
2506 con->ourhook[sizeof(con->ourhook) - 1] = '\0';
2507 con->peerhook[sizeof(con->peerhook) - 1] = '\0';
2508 /* Don't forget we get a reference.. */
2509 error = ng_path2noderef(here, con->path, &node2, NULL);
2512 error = ng_con_nodes(here, con->ourhook, node2, con->peerhook);
2513 NG_NODE_UNREF(node2);
2518 struct ngm_name *const nam = (struct ngm_name *) msg->data;
2520 if (msg->header.arglen != sizeof(*nam)) {
2525 nam->name[sizeof(nam->name) - 1] = '\0';
2526 error = ng_name_node(here, nam->name);
2531 struct ngm_rmhook *const rmh = (struct ngm_rmhook *) msg->data;
2534 if (msg->header.arglen != sizeof(*rmh)) {
2539 rmh->ourhook[sizeof(rmh->ourhook) - 1] = '\0';
2540 if ((hook = ng_findhook(here, rmh->ourhook)) != NULL)
2541 ng_destroy_hook(hook);
2546 struct nodeinfo *ni;
2548 NG_MKRESPONSE(resp, msg, sizeof(*ni), M_NOWAIT);
2554 /* Fill in node info */
2555 ni = (struct nodeinfo *) resp->data;
2556 if (NG_NODE_HAS_NAME(here))
2557 strncpy(ni->name, NG_NODE_NAME(here), NG_NODELEN);
2558 strncpy(ni->type, here->nd_type->name, NG_TYPELEN);
2559 ni->id = ng_node2ID(here);
2560 ni->hooks = here->nd_numhooks;
2565 const int nhooks = here->nd_numhooks;
2566 struct hooklist *hl;
2567 struct nodeinfo *ni;
2570 /* Get response struct */
2571 NG_MKRESPONSE(resp, msg, sizeof(*hl)
2572 + (nhooks * sizeof(struct linkinfo)), M_NOWAIT);
2577 hl = (struct hooklist *) resp->data;
2580 /* Fill in node info */
2581 if (NG_NODE_HAS_NAME(here))
2582 strncpy(ni->name, NG_NODE_NAME(here), NG_NODELEN);
2583 strncpy(ni->type, here->nd_type->name, NG_TYPELEN);
2584 ni->id = ng_node2ID(here);
2586 /* Cycle through the linked list of hooks */
2588 LIST_FOREACH(hook, &here->nd_hooks, hk_hooks) {
2589 struct linkinfo *const link = &hl->link[ni->hooks];
2591 if (ni->hooks >= nhooks) {
2592 log(LOG_ERR, "%s: number of %s changed\n",
2596 if (NG_HOOK_NOT_VALID(hook))
2598 strncpy(link->ourhook, NG_HOOK_NAME(hook), NG_HOOKLEN);
2599 strncpy(link->peerhook,
2600 NG_PEER_HOOK_NAME(hook), NG_HOOKLEN);
2601 if (NG_PEER_NODE_NAME(hook)[0] != '\0')
2602 strncpy(link->nodeinfo.name,
2603 NG_PEER_NODE_NAME(hook), NG_NODELEN);
2604 strncpy(link->nodeinfo.type,
2605 NG_PEER_NODE(hook)->nd_type->name, NG_TYPELEN);
2606 link->nodeinfo.id = ng_node2ID(NG_PEER_NODE(hook));
2607 link->nodeinfo.hooks = NG_PEER_NODE(hook)->nd_numhooks;
2616 const int unnamed = (msg->header.cmd == NGM_LISTNODES);
2617 struct namelist *nl;
2621 mtx_lock(&ng_nodelist_mtx);
2622 /* Count number of nodes */
2623 LIST_FOREACH(node, &ng_nodelist, nd_nodes) {
2624 if (NG_NODE_IS_VALID(node)
2625 && (unnamed || NG_NODE_HAS_NAME(node))) {
2629 mtx_unlock(&ng_nodelist_mtx);
2631 /* Get response struct */
2632 NG_MKRESPONSE(resp, msg, sizeof(*nl)
2633 + (num * sizeof(struct nodeinfo)), M_NOWAIT);
2638 nl = (struct namelist *) resp->data;
2640 /* Cycle through the linked list of nodes */
2642 mtx_lock(&ng_nodelist_mtx);
2643 LIST_FOREACH(node, &ng_nodelist, nd_nodes) {
2644 struct nodeinfo *const np = &nl->nodeinfo[nl->numnames];
2646 if (nl->numnames >= num) {
2647 log(LOG_ERR, "%s: number of %s changed\n",
2651 if (NG_NODE_NOT_VALID(node))
2653 if (!unnamed && (! NG_NODE_HAS_NAME(node)))
2655 if (NG_NODE_HAS_NAME(node))
2656 strncpy(np->name, NG_NODE_NAME(node), NG_NODELEN);
2657 strncpy(np->type, node->nd_type->name, NG_TYPELEN);
2658 np->id = ng_node2ID(node);
2659 np->hooks = node->nd_numhooks;
2662 mtx_unlock(&ng_nodelist_mtx);
2668 struct typelist *tl;
2669 struct ng_type *type;
2672 mtx_lock(&ng_typelist_mtx);
2673 /* Count number of types */
2674 LIST_FOREACH(type, &ng_typelist, types) {
2677 mtx_unlock(&ng_typelist_mtx);
2679 /* Get response struct */
2680 NG_MKRESPONSE(resp, msg, sizeof(*tl)
2681 + (num * sizeof(struct typeinfo)), M_NOWAIT);
2686 tl = (struct typelist *) resp->data;
2688 /* Cycle through the linked list of types */
2690 mtx_lock(&ng_typelist_mtx);
2691 LIST_FOREACH(type, &ng_typelist, types) {
2692 struct typeinfo *const tp = &tl->typeinfo[tl->numtypes];
2694 if (tl->numtypes >= num) {
2695 log(LOG_ERR, "%s: number of %s changed\n",
2699 strncpy(tp->type_name, type->name, NG_TYPELEN);
2700 tp->numnodes = type->refs - 1; /* don't count list */
2703 mtx_unlock(&ng_typelist_mtx);
2707 case NGM_BINARY2ASCII:
2709 int bufSize = 20 * 1024; /* XXX hard coded constant */
2710 const struct ng_parse_type *argstype;
2711 const struct ng_cmdlist *c;
2712 struct ng_mesg *binary, *ascii;
2714 /* Data area must contain a valid netgraph message */
2715 binary = (struct ng_mesg *)msg->data;
2716 if (msg->header.arglen < sizeof(struct ng_mesg)
2717 || (msg->header.arglen - sizeof(struct ng_mesg)
2718 < binary->header.arglen)) {
2724 /* Get a response message with lots of room */
2725 NG_MKRESPONSE(resp, msg, sizeof(*ascii) + bufSize, M_NOWAIT);
2730 ascii = (struct ng_mesg *)resp->data;
2732 /* Copy binary message header to response message payload */
2733 bcopy(binary, ascii, sizeof(*binary));
2735 /* Find command by matching typecookie and command number */
2736 for (c = here->nd_type->cmdlist;
2737 c != NULL && c->name != NULL; c++) {
2738 if (binary->header.typecookie == c->cookie
2739 && binary->header.cmd == c->cmd)
2742 if (c == NULL || c->name == NULL) {
2743 for (c = ng_generic_cmds; c->name != NULL; c++) {
2744 if (binary->header.typecookie == c->cookie
2745 && binary->header.cmd == c->cmd)
2748 if (c->name == NULL) {
2755 /* Convert command name to ASCII */
2756 snprintf(ascii->header.cmdstr, sizeof(ascii->header.cmdstr),
2759 /* Convert command arguments to ASCII */
2760 argstype = (binary->header.flags & NGF_RESP) ?
2761 c->respType : c->mesgType;
2762 if (argstype == NULL) {
2763 *ascii->data = '\0';
2765 if ((error = ng_unparse(argstype,
2766 (u_char *)binary->data,
2767 ascii->data, bufSize)) != 0) {
2773 /* Return the result as struct ng_mesg plus ASCII string */
2774 bufSize = strlen(ascii->data) + 1;
2775 ascii->header.arglen = bufSize;
2776 resp->header.arglen = sizeof(*ascii) + bufSize;
2780 case NGM_ASCII2BINARY:
2782 int bufSize = 2000; /* XXX hard coded constant */
2783 const struct ng_cmdlist *c;
2784 const struct ng_parse_type *argstype;
2785 struct ng_mesg *ascii, *binary;
2788 /* Data area must contain at least a struct ng_mesg + '\0' */
2789 ascii = (struct ng_mesg *)msg->data;
2790 if ((msg->header.arglen < sizeof(*ascii) + 1)
2791 || (ascii->header.arglen < 1)
2792 || (msg->header.arglen
2793 < sizeof(*ascii) + ascii->header.arglen)) {
2798 ascii->data[ascii->header.arglen - 1] = '\0';
2800 /* Get a response message with lots of room */
2801 NG_MKRESPONSE(resp, msg, sizeof(*binary) + bufSize, M_NOWAIT);
2806 binary = (struct ng_mesg *)resp->data;
2808 /* Copy ASCII message header to response message payload */
2809 bcopy(ascii, binary, sizeof(*ascii));
2811 /* Find command by matching ASCII command string */
2812 for (c = here->nd_type->cmdlist;
2813 c != NULL && c->name != NULL; c++) {
2814 if (strcmp(ascii->header.cmdstr, c->name) == 0)
2817 if (c == NULL || c->name == NULL) {
2818 for (c = ng_generic_cmds; c->name != NULL; c++) {
2819 if (strcmp(ascii->header.cmdstr, c->name) == 0)
2822 if (c->name == NULL) {
2829 /* Convert command name to binary */
2830 binary->header.cmd = c->cmd;
2831 binary->header.typecookie = c->cookie;
2833 /* Convert command arguments to binary */
2834 argstype = (binary->header.flags & NGF_RESP) ?
2835 c->respType : c->mesgType;
2836 if (argstype == NULL) {
2839 if ((error = ng_parse(argstype, ascii->data,
2840 &off, (u_char *)binary->data, &bufSize)) != 0) {
2846 /* Return the result */
2847 binary->header.arglen = bufSize;
2848 resp->header.arglen = sizeof(*binary) + bufSize;
2852 case NGM_TEXT_CONFIG:
2853 case NGM_TEXT_STATUS:
2855 * This one is tricky as it passes the command down to the
2856 * actual node, even though it is a generic type command.
2857 * This means we must assume that the item/msg is already freed
2858 * when control passes back to us.
2860 if (here->nd_type->rcvmsg != NULL) {
2861 NGI_MSG(item) = msg; /* put it back as we found it */
2862 return((*here->nd_type->rcvmsg)(here, item, lasthook));
2864 /* Fall through if rcvmsg not supported */
2870 * Sometimes a generic message may be statically allocated
2871 * to avoid problems with allocating when in tight memeory situations.
2872 * Don't free it if it is so.
2873 * I break them appart here, because erros may cause a free if the item
2874 * in which case we'd be doing it twice.
2875 * they are kept together above, to simplify freeing.
2878 NG_RESPOND_MSG(error, here, item, resp);
2887 * Returns new meta, or NULL if original meta is NULL or ENOMEM.
2890 ng_copy_meta(meta_p meta)
2896 MALLOC(meta2, meta_p, meta->used_len, M_NETGRAPH_META, M_NOWAIT);
2899 meta2->allocated_len = meta->used_len;
2900 bcopy(meta, meta2, meta->used_len);
2904 /************************************************************************
2906 ************************************************************************/
2909 * Handle the loading/unloading of a netgraph node type module
2912 ng_mod_event(module_t mod, int event, void *data)
2914 struct ng_type *const type = data;
2920 /* Register new netgraph node type */
2922 if ((error = ng_newtype(type)) != 0) {
2927 /* Call type specific code */
2928 if (type->mod_event != NULL)
2929 if ((error = (*type->mod_event)(mod, event, data))) {
2930 mtx_lock(&ng_typelist_mtx);
2931 type->refs--; /* undo it */
2932 LIST_REMOVE(type, types);
2933 mtx_unlock(&ng_typelist_mtx);
2940 if (type->refs > 1) { /* make sure no nodes exist! */
2943 if (type->refs == 0) {
2944 /* failed load, nothing to undo */
2948 if (type->mod_event != NULL) { /* check with type */
2949 error = (*type->mod_event)(mod, event, data);
2950 if (error != 0) { /* type refuses.. */
2955 mtx_lock(&ng_typelist_mtx);
2956 LIST_REMOVE(type, types);
2957 mtx_unlock(&ng_typelist_mtx);
2963 if (type->mod_event != NULL)
2964 error = (*type->mod_event)(mod, event, data);
2966 error = 0; /* XXX ? */
2973 * Handle loading and unloading for this code.
2974 * The only thing we need to link into is the NETISR strucure.
2977 ngb_mod_event(module_t mod, int event, void *data)
2983 /* Register line discipline */
2984 mtx_init(&ng_worklist_mtx, "ng_worklist", MTX_SPIN);
2985 mtx_init(&ng_typelist_mtx, "netgraph types mutex", 0);
2986 mtx_init(&ng_nodelist_mtx, "netgraph nodelist mutex", 0);
2987 mtx_init(&ng_idhash_mtx, "netgraph idhash mutex", 0);
2988 mtx_init(&ngq_mtx, "netgraph netisr mutex", 0);
2990 error = register_netisr(NETISR_NETGRAPH, ngintr);
2994 /* You cant unload it because an interface may be using it. */
3004 static moduledata_t netgraph_mod = {
3009 DECLARE_MODULE(netgraph, netgraph_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
3010 SYSCTL_NODE(_net, OID_AUTO, graph, CTLFLAG_RW, 0, "netgraph Family");
3011 SYSCTL_INT(_net_graph, OID_AUTO, abi_version, CTLFLAG_RD, 0, NG_ABI_VERSION,"");
3012 SYSCTL_INT(_net_graph, OID_AUTO, msg_version, CTLFLAG_RD, 0, NG_VERSION, "");
3014 /************************************************************************
3015 Queue element get/free routines
3016 ************************************************************************/
3019 static int allocated; /* number of items malloc'd */
3020 static int maxalloc = 128; /* limit the damage of a leak */
3021 static const int ngqfreemax = 64;/* cache at most this many */
3022 static const int ngqfreelow = 4; /* try malloc if free < this */
3023 static volatile int ngqfreesize; /* number of cached entries */
3024 #ifdef NETGRAPH_DEBUG
3025 static TAILQ_HEAD(, ng_item) ng_itemlist = TAILQ_HEAD_INITIALIZER(ng_itemlist);
3029 * This is usually called when a packet first enters netgraph.
3030 * By definition, this is usually from an interrupt, or from a user.
3031 * Users are not so important, but try be quick for the times that it's
3032 * an interrupt. Use atomic operations to cope with collisions
3033 * with interrupts and other processors. Assumes MALLOC is SMP safe.
3034 * XXX If reserve is low, we should try to get 2 from malloc as this
3035 * would indicate it often fails.
3043 * Try get a cached queue block, or else allocate a new one
3044 * If we are less than our reserve, try malloc. If malloc
3045 * fails, then that's what the reserve is for...
3046 * Don't completely trust ngqfreesize, as it is subject
3047 * to races.. (it'll eventually catch up but may be out by one or two
3048 * for brief moments(under SMP or interrupts).
3049 * ngqfree is the final arbiter. We have our little reserve
3050 * because we use M_NOWAIT for malloc. This just helps us
3051 * avoid dropping packets while not increasing the time
3052 * we take to service the interrupt (on average) (I hope).
3055 if ((ngqfreesize < ngqfreelow) || (ngqfree == NULL)) {
3056 if (allocated < maxalloc) { /* don't leak forever */
3057 MALLOC(item, item_p ,
3058 sizeof(*item), M_NETGRAPH_ITEM,
3059 (M_NOWAIT | M_ZERO));
3061 #ifdef NETGRAPH_DEBUG
3062 TAILQ_INSERT_TAIL(&ng_itemlist,
3064 #endif /* NETGRAPH_DEBUG */
3065 atomic_add_int(&allocated, 1);
3072 * We didn't or couldn't malloc.
3073 * try get one from our cache.
3074 * item must be NULL to get here.
3076 if ((item = ngqfree) != NULL) {
3078 * Atomically try grab the first item
3079 * and put it's successor in its place.
3080 * If we fail, just try again.. someone else
3081 * beat us to this one or freed one.
3082 * Don't worry about races with ngqfreesize.
3083 * Close enough is good enough..
3085 if (atomic_cmpset_ptr(&ngqfree, item, item->el_next)) {
3086 atomic_subtract_int(&ngqfreesize, 1);
3087 item->el_flags &= ~NGQF_FREE;
3091 * something got there before we did.. try again
3092 * (go around the loop again)
3096 /* We really ran out */
3104 * Release a queue entry
3107 ng_free_item(item_p item)
3111 * The item may hold resources on it's own. We need to free
3112 * these before we can free the item. What they are depends upon
3113 * what kind of item it is. it is important that nodes zero
3114 * out pointers to resources that they remove from the item
3115 * or we release them again here.
3117 if (item->el_flags & NGQF_FREE) {
3118 panic(" Freeing free queue item");
3120 switch (item->el_flags & NGQF_TYPE) {
3122 /* If we have an mbuf and metadata still attached.. */
3123 NG_FREE_M(_NGI_M(item));
3124 NG_FREE_META(_NGI_META(item));
3127 _NGI_RETADDR(item) = NULL;
3128 NG_FREE_MSG(_NGI_MSG(item));
3131 /* nothing to free really, */
3132 _NGI_FN(item) = NULL;
3133 _NGI_ARG1(item) = NULL;
3134 _NGI_ARG2(item) = 0;
3137 /* If we still have a node or hook referenced... */
3138 _NGI_CLR_NODE(item);
3139 _NGI_CLR_HOOK(item);
3140 item->el_flags |= NGQF_FREE;
3143 * We have freed any resources held by the item.
3144 * now we can free the item itself.
3146 if (ngqfreesize < ngqfreemax) { /* don't worry about races */
3148 item->el_next = ngqfree;
3149 if (atomic_cmpset_ptr(&ngqfree, item->el_next, item)) {
3153 atomic_add_int(&ngqfreesize, 1);
3155 /* This is the only place that should use this Macro */
3156 #ifdef NETGRAPH_DEBUG
3157 TAILQ_REMOVE(&ng_itemlist, item, all);
3158 #endif /* NETGRAPH_DEBUG */
3159 NG_FREE_ITEM_REAL(item);
3160 atomic_subtract_int(&allocated, 1);
3164 #ifdef NETGRAPH_DEBUG
3166 dumphook (hook_p hook, char *file, int line)
3168 printf("hook: name %s, %d refs, Last touched:\n",
3169 _NG_HOOK_NAME(hook), hook->hk_refs);
3170 printf(" Last active @ %s, line %d\n",
3171 hook->lastfile, hook->lastline);
3173 printf(" problem discovered at file %s, line %d\n", file, line);
3178 dumpnode(node_p node, char *file, int line)
3180 printf("node: ID [%x]: type '%s', %d hooks, flags 0x%x, %d refs, %s:\n",
3181 _NG_NODE_ID(node), node->nd_type->name,
3182 node->nd_numhooks, node->nd_flags,
3183 node->nd_refs, node->nd_name);
3184 printf(" Last active @ %s, line %d\n",
3185 node->lastfile, node->lastline);
3187 printf(" problem discovered at file %s, line %d\n", file, line);
3192 dumpitem(item_p item, char *file, int line)
3194 if (item->el_flags & NGQF_FREE) {
3195 printf(" Free item, freed at %s, line %d\n",
3196 item->lastfile, item->lastline);
3198 printf(" ACTIVE item, last used at %s, line %d",
3199 item->lastfile, item->lastline);
3200 switch(item->el_flags & NGQF_TYPE) {
3202 printf(" - [data]\n");
3205 printf(" - retaddr[%d]:\n", _NGI_RETADDR(item));
3208 printf(" - fn@%p (%p, %p, %p, %d (%x))\n",
3209 item->body.fn.fn_fn,
3212 item->body.fn.fn_arg1,
3213 item->body.fn.fn_arg2,
3214 item->body.fn.fn_arg2);
3217 printf(" - UNDEFINED!\n");
3221 printf(" problem discovered at file %s, line %d\n", file, line);
3222 if (NGI_NODE(item)) {
3223 printf("node %p ([%x])\n",
3224 NGI_NODE(item), ng_node2ID(NGI_NODE(item)));
3234 TAILQ_FOREACH(item, &ng_itemlist, all) {
3235 printf("[%d] ", i++);
3236 dumpitem(item, NULL, 0);
3245 SLIST_FOREACH(node, &ng_allnodes, nd_all) {
3246 printf("[%d] ", i++);
3247 dumpnode(node, NULL, 0);
3256 SLIST_FOREACH(hook, &ng_allhooks, hk_all) {
3257 printf("[%d] ", i++);
3258 dumphook(hook, NULL, 0);
3263 sysctl_debug_ng_dump_items(SYSCTL_HANDLER_ARGS)
3271 error = sysctl_handle_int(oidp, &val, sizeof(int), req);
3272 if (error != 0 || req->newptr == NULL)
3282 SYSCTL_PROC(_debug, OID_AUTO, ng_dump_items, CTLTYPE_INT | CTLFLAG_RW,
3283 0, sizeof(int), sysctl_debug_ng_dump_items, "I", "Number of allocated items");
3284 #endif /* NETGRAPH_DEBUG */
3287 /***********************************************************************
3289 **********************************************************************/
3290 /* NETISR thread enters here */
3292 * Pick a node off the list of nodes with work,
3293 * try get an item to process off it.
3294 * If there are no more, remove the node from the list.
3303 mtx_lock_spin(&ng_worklist_mtx);
3304 node = TAILQ_FIRST(&ng_worklist);
3306 mtx_unlock_spin(&ng_worklist_mtx);
3309 node->nd_flags &= ~NG_WORKQ;
3310 TAILQ_REMOVE(&ng_worklist, node, nd_work);
3311 mtx_unlock_spin(&ng_worklist_mtx);
3313 * We have the node. We also take over the reference
3314 * that the list had on it.
3315 * Now process as much as you can, until it won't
3316 * let you have another item off the queue.
3317 * All this time, keep the reference
3318 * that lets us be sure that the node still exists.
3319 * Let the reference go at the last minute.
3320 * ng_dequeue will put us back on the worklist
3321 * if there is more too do. This may be of use if there
3322 * are Multiple Processors and multiple Net threads in the
3326 mtx_lock_spin(&node->nd_input_queue.q_mtx);
3327 item = ng_dequeue(&node->nd_input_queue);
3329 mtx_unlock_spin(&node->nd_input_queue.q_mtx);
3330 break; /* go look for another node */
3332 mtx_unlock_spin(&node->nd_input_queue.q_mtx);
3333 NGI_GET_NODE(item, node); /* zaps stored node */
3334 ng_apply_item(node, item);
3335 NG_NODE_UNREF(node);
3338 NG_NODE_UNREF(node);
3343 ng_worklist_remove(node_p node)
3345 mtx_lock_spin(&ng_worklist_mtx);
3346 if (node->nd_flags & NG_WORKQ) {
3347 node->nd_flags &= ~NG_WORKQ;
3348 TAILQ_REMOVE(&ng_worklist, node, nd_work);
3349 mtx_unlock_spin(&ng_worklist_mtx);
3350 NG_NODE_UNREF(node);
3352 mtx_unlock_spin(&ng_worklist_mtx);
3358 * It's posible that a debugging NG_NODE_REF may need
3359 * to be outside the mutex zone
3362 ng_setisr(node_p node)
3364 mtx_lock_spin(&ng_worklist_mtx);
3365 if ((node->nd_flags & NG_WORKQ) == 0) {
3367 * If we are not already on the work queue,
3370 node->nd_flags |= NG_WORKQ;
3371 TAILQ_INSERT_TAIL(&ng_worklist, node, nd_work);
3372 NG_NODE_REF(node); /* XXX fafe in mutex? */
3374 mtx_unlock_spin(&ng_worklist_mtx);
3375 schednetisr(NETISR_NETGRAPH);
3379 /***********************************************************************
3380 * Externally useable functions to set up a queue item ready for sending
3381 ***********************************************************************/
3383 #ifdef NETGRAPH_DEBUG
3384 #define ITEM_DEBUG_CHECKS \
3386 if (NGI_NODE(item) ) { \
3387 printf("item already has node"); \
3388 Debugger("has node"); \
3389 NGI_CLR_NODE(item); \
3391 if (NGI_HOOK(item) ) { \
3392 printf("item already has hook"); \
3393 Debugger("has hook"); \
3394 NGI_CLR_HOOK(item); \
3398 #define ITEM_DEBUG_CHECKS
3402 * Put elements into the item.
3403 * Hook and node references will be removed when the item is dequeued.
3405 * (XXX) Unsafe because no reference held by peer on remote node.
3406 * remote node might go away in this timescale.
3407 * We know the hooks can't go away because that would require getting
3408 * a writer item on both nodes and we must have at least a reader
3409 * here to eb able to do this.
3410 * Note that the hook loaded is the REMOTE hook.
3412 * This is possibly in the critical path for new data.
3415 ng_package_data(struct mbuf *m, meta_p meta)
3419 if ((item = ng_getqblk()) == NULL) {
3425 item->el_flags = NGQF_DATA;
3426 item->el_next = NULL;
3428 NGI_META(item) = meta;
3433 * Allocate a queue item and put items into it..
3434 * Evaluate the address as this will be needed to queue it and
3435 * to work out what some of the fields should be.
3436 * Hook and node references will be removed when the item is dequeued.
3440 ng_package_msg(struct ng_mesg *msg)
3444 if ((item = ng_getqblk()) == NULL) {
3449 item->el_flags = NGQF_MESG;
3450 item->el_next = NULL;
3452 * Set the current lasthook into the queue item
3454 NGI_MSG(item) = msg;
3455 NGI_RETADDR(item) = NULL;
3461 #define SET_RETADDR(item, here, retaddr) \
3462 do { /* Data or fn items don't have retaddrs */ \
3463 if ((item->el_flags & NGQF_TYPE) == NGQF_MESG) { \
3465 NGI_RETADDR(item) = retaddr; \
3468 * The old return address should be ok. \
3469 * If there isn't one, use the address \
3472 if (NGI_RETADDR(item) == 0) { \
3474 = ng_node2ID(here); \
3481 ng_address_hook(node_p here, item_p item, hook_p hook, ng_ID_t retaddr)
3487 * Quick sanity check..
3488 * Since a hook holds a reference on it's node, once we know
3489 * that the peer is still connected (even if invalid,) we know
3490 * that the peer node is present, though maybe invalid.
3493 || NG_HOOK_NOT_VALID(hook)
3494 || (NG_HOOK_PEER(hook) == NULL)
3495 || NG_HOOK_NOT_VALID(NG_HOOK_PEER(hook))
3496 || NG_NODE_NOT_VALID(NG_PEER_NODE(hook))) {
3503 * Transfer our interest to the other (peer) end.
3505 peer = NG_HOOK_PEER(hook);
3507 NGI_SET_HOOK(item, peer);
3508 peernode = NG_PEER_NODE(hook);
3509 NG_NODE_REF(peernode);
3510 NGI_SET_NODE(item, peernode);
3511 SET_RETADDR(item, here, retaddr);
3516 ng_address_path(node_p here, item_p item, char *address, ng_ID_t retaddr)
3524 * Note that ng_path2noderef increments the reference count
3525 * on the node for us if it finds one. So we don't have to.
3527 error = ng_path2noderef(here, address, &dest, &hook);
3532 NGI_SET_NODE(item, dest);
3534 NG_HOOK_REF(hook); /* don't let it go while on the queue */
3535 NGI_SET_HOOK(item, hook);
3537 SET_RETADDR(item, here, retaddr);
3542 ng_address_ID(node_p here, item_p item, ng_ID_t ID, ng_ID_t retaddr)
3548 * Find the target node.
3550 dest = ng_ID2noderef(ID); /* GETS REFERENCE! */
3556 /* Fill out the contents */
3557 item->el_flags = NGQF_MESG;
3558 item->el_next = NULL;
3559 NGI_SET_NODE(item, dest);
3561 SET_RETADDR(item, here, retaddr);
3566 * special case to send a message to self (e.g. destroy node)
3567 * Possibly indicate an arrival hook too.
3568 * Useful for removing that hook :-)
3571 ng_package_msg_self(node_p here, hook_p hook, struct ng_mesg *msg)
3576 * Find the target node.
3577 * If there is a HOOK argument, then use that in preference
3580 if ((item = ng_getqblk()) == NULL) {
3585 /* Fill out the contents */
3586 item->el_flags = NGQF_MESG;
3587 item->el_next = NULL;
3589 NGI_SET_NODE(item, here);
3592 NGI_SET_HOOK(item, hook);
3594 NGI_MSG(item) = msg;
3595 NGI_RETADDR(item) = ng_node2ID(here);
3600 ng_send_fn(node_p node, hook_p hook, ng_item_fn *fn, void * arg1, int arg2)
3604 if ((item = ng_getqblk()) == NULL) {
3607 item->el_flags = NGQF_FN | NGQF_WRITER;
3608 NG_NODE_REF(node); /* and one for the item */
3609 NGI_SET_NODE(item, node);
3612 NGI_SET_HOOK(item, hook);
3615 NGI_ARG1(item) = arg1;
3616 NGI_ARG2(item) = arg2;
3617 return(ng_snd_item(item, 0));
3621 * Official timeout routines for Netgraph nodes.
3624 ng_timeout_trapoline(void *arg)
3628 ng_snd_item(item, 0);
3632 struct callout_handle
3633 ng_timeout(node_p node, hook_p hook, int ticks,
3634 ng_item_fn *fn, void * arg1, int arg2)
3638 if ((item = ng_getqblk()) == NULL) {
3639 struct callout_handle handle;
3640 handle.callout = NULL;
3643 item->el_flags = NGQF_FN | NGQF_WRITER;
3644 NG_NODE_REF(node); /* and one for the item */
3645 NGI_SET_NODE(item, node);
3648 NGI_SET_HOOK(item, hook);
3651 NGI_ARG1(item) = arg1;
3652 NGI_ARG2(item) = arg2;
3653 return (timeout(&ng_timeout_trapoline, item, ticks));
3656 /* A special modified version of untimeout() */
3658 ng_untimeout(struct callout_handle handle, node_p node)
3662 if (handle.callout == NULL)
3664 mtx_lock_spin(&callout_lock);
3665 item = handle.callout->c_arg; /* should be an official way to do this */
3666 if ((handle.callout->c_func == &ng_timeout_trapoline) &&
3667 (NGI_NODE(item) == node) &&
3668 (callout_stop(handle.callout))) {
3670 * We successfully removed it from the queue before it ran
3671 * So now we need to unreference everything that was
3672 * given extra references. (NG_FREE_ITEM does this).
3674 mtx_unlock_spin(&callout_lock);
3678 mtx_unlock_spin(&callout_lock);
3683 * Set the address, if none given, give the node here.
3686 ng_replace_retaddr(node_p here, item_p item, ng_ID_t retaddr)
3689 NGI_RETADDR(item) = retaddr;
3692 * The old return address should be ok.
3693 * If there isn't one, use the address here.
3695 NGI_RETADDR(item) = ng_node2ID(here);
3701 /* just test all the macros */
3703 ng_macro_test(item_p item);
3705 ng_macro_test(item_p item)
3711 struct ng_mesg *msg;
3716 NGI_GET_META(item, meta);
3717 NGI_GET_MSG(item, msg);
3718 retaddr = NGI_RETADDR(item);
3719 NG_SEND_DATA(error, hook, m, meta);
3720 NG_SEND_DATA_ONLY(error, hook, m);
3721 NG_FWD_NEW_DATA(error, item, hook, m);
3722 NG_FWD_ITEM_HOOK(error, item, hook);
3723 NG_SEND_MSG_HOOK(error, node, msg, hook, retaddr);
3724 NG_SEND_MSG_ID(error, node, msg, retaddr, retaddr);
3725 NG_SEND_MSG_PATH(error, node, msg, ".:", retaddr);
3726 NG_FWD_MSG_HOOK(error, node, item, hook, retaddr);
3728 #endif /* TESTING */