xref: /freebsd/sys/netgraph/ng_base.c (revision 018fe3d10e4572f2324219085829b0cbffbaf7e6)
14cf49a43SJulian Elischer /*
24cf49a43SJulian Elischer  * ng_base.c
3c398230bSWarner Losh  */
4c398230bSWarner Losh 
5c398230bSWarner Losh /*-
64cf49a43SJulian Elischer  * Copyright (c) 1996-1999 Whistle Communications, Inc.
74cf49a43SJulian Elischer  * All rights reserved.
84cf49a43SJulian Elischer  *
94cf49a43SJulian Elischer  * Subject to the following obligations and disclaimer of warranty, use and
104cf49a43SJulian Elischer  * redistribution of this software, in source or object code forms, with or
114cf49a43SJulian Elischer  * without modifications are expressly permitted by Whistle Communications;
124cf49a43SJulian Elischer  * provided, however, that:
134cf49a43SJulian Elischer  * 1. Any and all reproductions of the source or object code must include the
144cf49a43SJulian Elischer  *    copyright notice above and the following disclaimer of warranties; and
154cf49a43SJulian Elischer  * 2. No rights are granted, in any manner or form, to use Whistle
164cf49a43SJulian Elischer  *    Communications, Inc. trademarks, including the mark "WHISTLE
174cf49a43SJulian Elischer  *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
184cf49a43SJulian Elischer  *    such appears in the above copyright notice or in the software.
194cf49a43SJulian Elischer  *
204cf49a43SJulian Elischer  * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
214cf49a43SJulian Elischer  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
224cf49a43SJulian Elischer  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
234cf49a43SJulian Elischer  * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
244cf49a43SJulian Elischer  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
254cf49a43SJulian Elischer  * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
264cf49a43SJulian Elischer  * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
274cf49a43SJulian Elischer  * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
284cf49a43SJulian Elischer  * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
294cf49a43SJulian Elischer  * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
304cf49a43SJulian Elischer  * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
314cf49a43SJulian Elischer  * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
324cf49a43SJulian Elischer  * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
334cf49a43SJulian Elischer  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
344cf49a43SJulian Elischer  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
354cf49a43SJulian Elischer  * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
364cf49a43SJulian Elischer  * OF SUCH DAMAGE.
374cf49a43SJulian Elischer  *
38cc3bbd68SJulian Elischer  * Authors: Julian Elischer <julian@freebsd.org>
39cc3bbd68SJulian Elischer  *          Archie Cobbs <archie@freebsd.org>
404cf49a43SJulian Elischer  *
414cf49a43SJulian Elischer  * $FreeBSD$
424cf49a43SJulian Elischer  * $Whistle: ng_base.c,v 1.39 1999/01/28 23:54:53 julian Exp $
434cf49a43SJulian Elischer  */
444cf49a43SJulian Elischer 
454cf49a43SJulian Elischer /*
464cf49a43SJulian Elischer  * This file implements the base netgraph code.
474cf49a43SJulian Elischer  */
484cf49a43SJulian Elischer 
494cf49a43SJulian Elischer #include <sys/param.h>
508e853a2cSGleb Smirnoff #include <sys/systm.h>
5177a58296SGleb Smirnoff #include <sys/ctype.h>
524cf49a43SJulian Elischer #include <sys/errno.h>
53f33ca0c9SMarcel Moolenaar #include <sys/kdb.h>
544cf49a43SJulian Elischer #include <sys/kernel.h>
553b33fbe7SGleb Smirnoff #include <sys/ktr.h>
56104a9b7eSAlexander Kabaev #include <sys/limits.h>
574cf49a43SJulian Elischer #include <sys/malloc.h>
584cf49a43SJulian Elischer #include <sys/mbuf.h>
5977a58296SGleb Smirnoff #include <sys/queue.h>
60bfa7e882SJulian Elischer #include <sys/sysctl.h>
6177a58296SGleb Smirnoff #include <sys/syslog.h>
62e088dd4cSAlexander Motin #include <sys/refcount.h>
6381a253a4SAlexander Motin #include <sys/proc.h>
644cf49a43SJulian Elischer 
654cf49a43SJulian Elischer #include <net/netisr.h>
664cf49a43SJulian Elischer 
674cf49a43SJulian Elischer #include <netgraph/ng_message.h>
684cf49a43SJulian Elischer #include <netgraph/netgraph.h>
69f8307e12SArchie Cobbs #include <netgraph/ng_parse.h>
704cf49a43SJulian Elischer 
719d72a7a3SJulian Elischer MODULE_VERSION(netgraph, NG_ABI_VERSION);
7299ff8176SPeter Wemm 
73ac5dd141SGleb Smirnoff /* Mutex to protect topology events. */
74ac5dd141SGleb Smirnoff static struct mtx	ng_topo_mtx;
75ac5dd141SGleb Smirnoff 
7630400f03SJulian Elischer #ifdef	NETGRAPH_DEBUG
77cfea3f85SAlexander Motin static struct mtx	ng_nodelist_mtx; /* protects global node/hook lists */
781489164fSGleb Smirnoff static struct mtx	ngq_mtx;	/* protects the queue item list */
7930400f03SJulian Elischer 
8030400f03SJulian Elischer static SLIST_HEAD(, ng_node) ng_allnodes;
8130400f03SJulian Elischer static LIST_HEAD(, ng_node) ng_freenodes; /* in debug, we never free() them */
8230400f03SJulian Elischer static SLIST_HEAD(, ng_hook) ng_allhooks;
8330400f03SJulian Elischer static LIST_HEAD(, ng_hook) ng_freehooks; /* in debug, we never free() them */
8430400f03SJulian Elischer 
8530400f03SJulian Elischer static void ng_dumpitems(void);
8630400f03SJulian Elischer static void ng_dumpnodes(void);
8730400f03SJulian Elischer static void ng_dumphooks(void);
8830400f03SJulian Elischer 
8930400f03SJulian Elischer #endif	/* NETGRAPH_DEBUG */
90954c4772SJulian Elischer /*
91954c4772SJulian Elischer  * DEAD versions of the structures.
92954c4772SJulian Elischer  * In order to avoid races, it is sometimes neccesary to point
93954c4772SJulian Elischer  * at SOMETHING even though theoretically, the current entity is
94954c4772SJulian Elischer  * INVALID. Use these to avoid these races.
95954c4772SJulian Elischer  */
96954c4772SJulian Elischer struct ng_type ng_deadtype = {
97954c4772SJulian Elischer 	NG_ABI_VERSION,
98954c4772SJulian Elischer 	"dead",
99954c4772SJulian Elischer 	NULL,	/* modevent */
100954c4772SJulian Elischer 	NULL,	/* constructor */
101954c4772SJulian Elischer 	NULL,	/* rcvmsg */
102954c4772SJulian Elischer 	NULL,	/* shutdown */
103954c4772SJulian Elischer 	NULL,	/* newhook */
104954c4772SJulian Elischer 	NULL,	/* findhook */
105954c4772SJulian Elischer 	NULL,	/* connect */
106954c4772SJulian Elischer 	NULL,	/* rcvdata */
107954c4772SJulian Elischer 	NULL,	/* disconnect */
108954c4772SJulian Elischer 	NULL, 	/* cmdlist */
109954c4772SJulian Elischer };
11030400f03SJulian Elischer 
111954c4772SJulian Elischer struct ng_node ng_deadnode = {
112954c4772SJulian Elischer 	"dead",
113954c4772SJulian Elischer 	&ng_deadtype,
114be4252b3SJulian Elischer 	NGF_INVALID,
115954c4772SJulian Elischer 	1,	/* refs */
116954c4772SJulian Elischer 	0,	/* numhooks */
117954c4772SJulian Elischer 	NULL,	/* private */
118954c4772SJulian Elischer 	0,	/* ID */
119954c4772SJulian Elischer 	LIST_HEAD_INITIALIZER(ng_deadnode.hooks),
120954c4772SJulian Elischer 	{},	/* all_nodes list entry */
121954c4772SJulian Elischer 	{},	/* id hashtable list entry */
122954c4772SJulian Elischer 	{},	/* workqueue entry */
123954c4772SJulian Elischer 	{	0,
124954c4772SJulian Elischer 		{}, /* should never use! (should hang) */
125954c4772SJulian Elischer 		NULL,
126954c4772SJulian Elischer 		&ng_deadnode.nd_input_queue.queue,
127954c4772SJulian Elischer 		&ng_deadnode
128954c4772SJulian Elischer 	},
129954c4772SJulian Elischer #ifdef	NETGRAPH_DEBUG
130954c4772SJulian Elischer 	ND_MAGIC,
131954c4772SJulian Elischer 	__FILE__,
132954c4772SJulian Elischer 	__LINE__,
133954c4772SJulian Elischer 	{NULL}
134954c4772SJulian Elischer #endif	/* NETGRAPH_DEBUG */
135954c4772SJulian Elischer };
136954c4772SJulian Elischer 
137954c4772SJulian Elischer struct ng_hook ng_deadhook = {
138954c4772SJulian Elischer 	"dead",
139954c4772SJulian Elischer 	NULL,		/* private */
140954c4772SJulian Elischer 	HK_INVALID | HK_DEAD,
141954c4772SJulian Elischer 	1,		/* refs always >= 1 */
142e58d779dSGleb Smirnoff 	0,		/* undefined data link type */
143954c4772SJulian Elischer 	&ng_deadhook,	/* Peer is self */
144954c4772SJulian Elischer 	&ng_deadnode,	/* attached to deadnode */
145954c4772SJulian Elischer 	{},		/* hooks list */
146c4b5eea4SJulian Elischer 	NULL,		/* override rcvmsg() */
147c4b5eea4SJulian Elischer 	NULL,		/* override rcvdata() */
148954c4772SJulian Elischer #ifdef	NETGRAPH_DEBUG
149954c4772SJulian Elischer 	HK_MAGIC,
150954c4772SJulian Elischer 	__FILE__,
151954c4772SJulian Elischer 	__LINE__,
152954c4772SJulian Elischer 	{NULL}
153954c4772SJulian Elischer #endif	/* NETGRAPH_DEBUG */
154954c4772SJulian Elischer };
155954c4772SJulian Elischer 
156954c4772SJulian Elischer /*
157954c4772SJulian Elischer  * END DEAD STRUCTURES
158954c4772SJulian Elischer  */
159069154d5SJulian Elischer /* List nodes with unallocated work */
160069154d5SJulian Elischer static TAILQ_HEAD(, ng_node) ng_worklist = TAILQ_HEAD_INITIALIZER(ng_worklist);
161b57a7965SJulian Elischer static struct mtx	ng_worklist_mtx;   /* MUST LOCK NODE FIRST */
1624cf49a43SJulian Elischer 
1634cf49a43SJulian Elischer /* List of installed types */
164069154d5SJulian Elischer static LIST_HEAD(, ng_type) ng_typelist;
165069154d5SJulian Elischer static struct mtx	ng_typelist_mtx;
1664cf49a43SJulian Elischer 
167069154d5SJulian Elischer /* Hash related definitions */
1680f150d04SJulian Elischer /* XXX Don't need to initialise them because it's a LIST */
169cfea3f85SAlexander Motin #define NG_ID_HASH_SIZE 128 /* most systems wont need even this many */
1700f150d04SJulian Elischer static LIST_HEAD(, ng_node) ng_ID_hash[NG_ID_HASH_SIZE];
171069154d5SJulian Elischer static struct mtx	ng_idhash_mtx;
1720f150d04SJulian Elischer /* Method to find a node.. used twice so do it here */
1730f150d04SJulian Elischer #define NG_IDHASH_FN(ID) ((ID) % (NG_ID_HASH_SIZE))
1740f150d04SJulian Elischer #define NG_IDHASH_FIND(ID, node)					\
1750f150d04SJulian Elischer 	do { 								\
17653f9c5e9SRobert Watson 		mtx_assert(&ng_idhash_mtx, MA_OWNED);			\
1770f150d04SJulian Elischer 		LIST_FOREACH(node, &ng_ID_hash[NG_IDHASH_FN(ID)],	\
1780f150d04SJulian Elischer 						nd_idnodes) {		\
1790f150d04SJulian Elischer 			if (NG_NODE_IS_VALID(node)			\
1800f150d04SJulian Elischer 			&& (NG_NODE_ID(node) == ID)) {			\
1810f150d04SJulian Elischer 				break;					\
1820f150d04SJulian Elischer 			}						\
1830f150d04SJulian Elischer 		}							\
1840f150d04SJulian Elischer 	} while (0)
185069154d5SJulian Elischer 
186cfea3f85SAlexander Motin #define NG_NAME_HASH_SIZE 128 /* most systems wont need even this many */
187cfea3f85SAlexander Motin static LIST_HEAD(, ng_node) ng_name_hash[NG_NAME_HASH_SIZE];
188cfea3f85SAlexander Motin static struct mtx	ng_namehash_mtx;
189cfea3f85SAlexander Motin #define NG_NAMEHASH(NAME, HASH)				\
190cfea3f85SAlexander Motin 	do {						\
191cfea3f85SAlexander Motin 		u_char	h = 0;				\
192cfea3f85SAlexander Motin 		const u_char	*c;			\
193cfea3f85SAlexander Motin 		for (c = (const u_char*)(NAME); *c; c++)\
194cfea3f85SAlexander Motin 			h += *c;			\
195cfea3f85SAlexander Motin 		(HASH) = h % (NG_NAME_HASH_SIZE);	\
196cfea3f85SAlexander Motin 	} while (0)
197cfea3f85SAlexander Motin 
198dc90cad9SJulian Elischer 
1994cf49a43SJulian Elischer /* Internal functions */
2004cf49a43SJulian Elischer static int	ng_add_hook(node_p node, const char *name, hook_p * hookp);
201069154d5SJulian Elischer static int	ng_generic_msg(node_p here, item_p item, hook_p lasthook);
202dc90cad9SJulian Elischer static ng_ID_t	ng_decodeidname(const char *name);
2034cf49a43SJulian Elischer static int	ngb_mod_event(module_t mod, int event, void *data);
204069154d5SJulian Elischer static void	ng_worklist_remove(node_p node);
2054cf49a43SJulian Elischer static void	ngintr(void);
20627757487SGleb Smirnoff static int	ng_apply_item(node_p node, item_p item, int rw);
207069154d5SJulian Elischer static void	ng_flush_input_queue(struct ng_queue * ngq);
208069154d5SJulian Elischer static void	ng_setisr(node_p node);
209069154d5SJulian Elischer static node_p	ng_ID2noderef(ng_ID_t ID);
210e088dd4cSAlexander Motin static int	ng_con_nodes(item_p item, node_p node, const char *name,
211e088dd4cSAlexander Motin 		    node_p node2, const char *name2);
212e088dd4cSAlexander Motin static int	ng_con_part2(node_p node, item_p item, hook_p hook);
213e088dd4cSAlexander Motin static int	ng_con_part3(node_p node, item_p item, hook_p hook);
2146b795970SJulian Elischer static int	ng_mkpeer(node_p node, const char *name,
2156b795970SJulian Elischer 						const char *name2, char *type);
216069154d5SJulian Elischer 
2174c9b5910SGleb Smirnoff /* Imported, these used to be externally visible, some may go back. */
218069154d5SJulian Elischer void	ng_destroy_hook(hook_p hook);
219069154d5SJulian Elischer node_p	ng_name2noderef(node_p node, const char *name);
220069154d5SJulian Elischer int	ng_path2noderef(node_p here, const char *path,
221069154d5SJulian Elischer 	node_p *dest, hook_p *lasthook);
222069154d5SJulian Elischer int	ng_make_node(const char *type, node_p *nodepp);
223069154d5SJulian Elischer int	ng_path_parse(char *addr, char **node, char **path, char **hook);
2241acb27c6SJulian Elischer void	ng_rmnode(node_p node, hook_p dummy1, void *dummy2, int dummy3);
22530400f03SJulian Elischer void	ng_unname(node_p node);
226069154d5SJulian Elischer 
2274cf49a43SJulian Elischer 
2284cf49a43SJulian Elischer /* Our own netgraph malloc type */
2294cf49a43SJulian Elischer MALLOC_DEFINE(M_NETGRAPH, "netgraph", "netgraph structures and ctrl messages");
230069154d5SJulian Elischer MALLOC_DEFINE(M_NETGRAPH_HOOK, "netgraph_hook", "netgraph hook structures");
231069154d5SJulian Elischer MALLOC_DEFINE(M_NETGRAPH_NODE, "netgraph_node", "netgraph node structures");
232069154d5SJulian Elischer MALLOC_DEFINE(M_NETGRAPH_ITEM, "netgraph_item", "netgraph item structures");
233069154d5SJulian Elischer MALLOC_DEFINE(M_NETGRAPH_MSG, "netgraph_msg", "netgraph name storage");
234069154d5SJulian Elischer 
235069154d5SJulian Elischer /* Should not be visible outside this file */
23630400f03SJulian Elischer 
23730400f03SJulian Elischer #define _NG_ALLOC_HOOK(hook) \
23830400f03SJulian Elischer 	MALLOC(hook, hook_p, sizeof(*hook), M_NETGRAPH_HOOK, M_NOWAIT | M_ZERO)
23930400f03SJulian Elischer #define _NG_ALLOC_NODE(node) \
24030400f03SJulian Elischer 	MALLOC(node, node_p, sizeof(*node), M_NETGRAPH_NODE, M_NOWAIT | M_ZERO)
24130400f03SJulian Elischer 
2422c8dda8dSWojciech A. Koszek #define	NG_QUEUE_LOCK_INIT(n)			\
2434abab3d5SWojciech A. Koszek 	mtx_init(&(n)->q_mtx, "ng_node", NULL, MTX_DEF)
2442c8dda8dSWojciech A. Koszek #define	NG_QUEUE_LOCK(n)			\
2454abab3d5SWojciech A. Koszek 	mtx_lock(&(n)->q_mtx)
2462c8dda8dSWojciech A. Koszek #define	NG_QUEUE_UNLOCK(n)			\
2474abab3d5SWojciech A. Koszek 	mtx_unlock(&(n)->q_mtx)
2482c8dda8dSWojciech A. Koszek #define	NG_WORKLIST_LOCK_INIT()			\
2494abab3d5SWojciech A. Koszek 	mtx_init(&ng_worklist_mtx, "ng_worklist", NULL, MTX_DEF)
2502c8dda8dSWojciech A. Koszek #define	NG_WORKLIST_LOCK()			\
2514abab3d5SWojciech A. Koszek 	mtx_lock(&ng_worklist_mtx)
2522c8dda8dSWojciech A. Koszek #define	NG_WORKLIST_UNLOCK()			\
2534abab3d5SWojciech A. Koszek 	mtx_unlock(&ng_worklist_mtx)
2542c8dda8dSWojciech A. Koszek 
25530400f03SJulian Elischer #ifdef NETGRAPH_DEBUG /*----------------------------------------------*/
25630400f03SJulian Elischer /*
25730400f03SJulian Elischer  * In debug mode:
25830400f03SJulian Elischer  * In an attempt to help track reference count screwups
25930400f03SJulian Elischer  * we do not free objects back to the malloc system, but keep them
26030400f03SJulian Elischer  * in a local cache where we can examine them and keep information safely
26130400f03SJulian Elischer  * after they have been freed.
26230400f03SJulian Elischer  * We use this scheme for nodes and hooks, and to some extent for items.
26330400f03SJulian Elischer  */
26430400f03SJulian Elischer static __inline hook_p
26530400f03SJulian Elischer ng_alloc_hook(void)
26630400f03SJulian Elischer {
26730400f03SJulian Elischer 	hook_p hook;
26830400f03SJulian Elischer 	SLIST_ENTRY(ng_hook) temp;
2699ed346baSBosko Milekic 	mtx_lock(&ng_nodelist_mtx);
27030400f03SJulian Elischer 	hook = LIST_FIRST(&ng_freehooks);
27130400f03SJulian Elischer 	if (hook) {
27230400f03SJulian Elischer 		LIST_REMOVE(hook, hk_hooks);
27330400f03SJulian Elischer 		bcopy(&hook->hk_all, &temp, sizeof(temp));
27430400f03SJulian Elischer 		bzero(hook, sizeof(struct ng_hook));
27530400f03SJulian Elischer 		bcopy(&temp, &hook->hk_all, sizeof(temp));
2769ed346baSBosko Milekic 		mtx_unlock(&ng_nodelist_mtx);
27730400f03SJulian Elischer 		hook->hk_magic = HK_MAGIC;
27830400f03SJulian Elischer 	} else {
2799ed346baSBosko Milekic 		mtx_unlock(&ng_nodelist_mtx);
28030400f03SJulian Elischer 		_NG_ALLOC_HOOK(hook);
28130400f03SJulian Elischer 		if (hook) {
28230400f03SJulian Elischer 			hook->hk_magic = HK_MAGIC;
2839ed346baSBosko Milekic 			mtx_lock(&ng_nodelist_mtx);
28430400f03SJulian Elischer 			SLIST_INSERT_HEAD(&ng_allhooks, hook, hk_all);
2859ed346baSBosko Milekic 			mtx_unlock(&ng_nodelist_mtx);
28630400f03SJulian Elischer 		}
28730400f03SJulian Elischer 	}
28830400f03SJulian Elischer 	return (hook);
28930400f03SJulian Elischer }
29030400f03SJulian Elischer 
29130400f03SJulian Elischer static __inline node_p
29230400f03SJulian Elischer ng_alloc_node(void)
29330400f03SJulian Elischer {
29430400f03SJulian Elischer 	node_p node;
29530400f03SJulian Elischer 	SLIST_ENTRY(ng_node) temp;
2969ed346baSBosko Milekic 	mtx_lock(&ng_nodelist_mtx);
29730400f03SJulian Elischer 	node = LIST_FIRST(&ng_freenodes);
29830400f03SJulian Elischer 	if (node) {
29930400f03SJulian Elischer 		LIST_REMOVE(node, nd_nodes);
30030400f03SJulian Elischer 		bcopy(&node->nd_all, &temp, sizeof(temp));
30130400f03SJulian Elischer 		bzero(node, sizeof(struct ng_node));
30230400f03SJulian Elischer 		bcopy(&temp, &node->nd_all, sizeof(temp));
3039ed346baSBosko Milekic 		mtx_unlock(&ng_nodelist_mtx);
30430400f03SJulian Elischer 		node->nd_magic = ND_MAGIC;
30530400f03SJulian Elischer 	} else {
3069ed346baSBosko Milekic 		mtx_unlock(&ng_nodelist_mtx);
30730400f03SJulian Elischer 		_NG_ALLOC_NODE(node);
30830400f03SJulian Elischer 		if (node) {
30930400f03SJulian Elischer 			node->nd_magic = ND_MAGIC;
3109ed346baSBosko Milekic 			mtx_lock(&ng_nodelist_mtx);
31130400f03SJulian Elischer 			SLIST_INSERT_HEAD(&ng_allnodes, node, nd_all);
3129ed346baSBosko Milekic 			mtx_unlock(&ng_nodelist_mtx);
31330400f03SJulian Elischer 		}
31430400f03SJulian Elischer 	}
31530400f03SJulian Elischer 	return (node);
31630400f03SJulian Elischer }
31730400f03SJulian Elischer 
31830400f03SJulian Elischer #define NG_ALLOC_HOOK(hook) do { (hook) = ng_alloc_hook(); } while (0)
31930400f03SJulian Elischer #define NG_ALLOC_NODE(node) do { (node) = ng_alloc_node(); } while (0)
32030400f03SJulian Elischer 
32130400f03SJulian Elischer 
32230400f03SJulian Elischer #define NG_FREE_HOOK(hook)						\
32330400f03SJulian Elischer 	do {								\
3249ed346baSBosko Milekic 		mtx_lock(&ng_nodelist_mtx);			\
32530400f03SJulian Elischer 		LIST_INSERT_HEAD(&ng_freehooks, hook, hk_hooks);	\
32630400f03SJulian Elischer 		hook->hk_magic = 0;					\
3279ed346baSBosko Milekic 		mtx_unlock(&ng_nodelist_mtx);			\
32830400f03SJulian Elischer 	} while (0)
32930400f03SJulian Elischer 
33030400f03SJulian Elischer #define NG_FREE_NODE(node)						\
33130400f03SJulian Elischer 	do {								\
3329ed346baSBosko Milekic 		mtx_lock(&ng_nodelist_mtx);			\
33330400f03SJulian Elischer 		LIST_INSERT_HEAD(&ng_freenodes, node, nd_nodes);	\
33430400f03SJulian Elischer 		node->nd_magic = 0;					\
3359ed346baSBosko Milekic 		mtx_unlock(&ng_nodelist_mtx);			\
33630400f03SJulian Elischer 	} while (0)
33730400f03SJulian Elischer 
33830400f03SJulian Elischer #else /* NETGRAPH_DEBUG */ /*----------------------------------------------*/
33930400f03SJulian Elischer 
34030400f03SJulian Elischer #define NG_ALLOC_HOOK(hook) _NG_ALLOC_HOOK(hook)
34130400f03SJulian Elischer #define NG_ALLOC_NODE(node) _NG_ALLOC_NODE(node)
34230400f03SJulian Elischer 
343069154d5SJulian Elischer #define NG_FREE_HOOK(hook) do { FREE((hook), M_NETGRAPH_HOOK); } while (0)
344069154d5SJulian Elischer #define NG_FREE_NODE(node) do { FREE((node), M_NETGRAPH_NODE); } while (0)
34530400f03SJulian Elischer 
34630400f03SJulian Elischer #endif /* NETGRAPH_DEBUG */ /*----------------------------------------------*/
34730400f03SJulian Elischer 
348f33ca0c9SMarcel Moolenaar /* Set this to kdb_enter("X") to catch all errors as they occur */
3494cf49a43SJulian Elischer #ifndef TRAP_ERROR
3506b795970SJulian Elischer #define TRAP_ERROR()
3514cf49a43SJulian Elischer #endif
3524cf49a43SJulian Elischer 
353dc90cad9SJulian Elischer static	ng_ID_t nextID = 1;
354dc90cad9SJulian Elischer 
355b2da83c2SArchie Cobbs #ifdef INVARIANTS
356b2da83c2SArchie Cobbs #define CHECK_DATA_MBUF(m)	do {					\
357b2da83c2SArchie Cobbs 		struct mbuf *n;						\
358b2da83c2SArchie Cobbs 		int total;						\
359b2da83c2SArchie Cobbs 									\
360fe584538SDag-Erling Smørgrav 		M_ASSERTPKTHDR(m);					\
361b32cfb32SGleb Smirnoff 		for (total = 0, n = (m); n != NULL; n = n->m_next) {	\
362b2da83c2SArchie Cobbs 			total += n->m_len;				\
363b32cfb32SGleb Smirnoff 			if (n->m_nextpkt != NULL)			\
364b32cfb32SGleb Smirnoff 				panic("%s: m_nextpkt", __func__);	\
365b32cfb32SGleb Smirnoff 		}							\
366ba5b359aSGleb Smirnoff 									\
367b2da83c2SArchie Cobbs 		if ((m)->m_pkthdr.len != total) {			\
368b2da83c2SArchie Cobbs 			panic("%s: %d != %d",				\
3696e551fb6SDavid E. O'Brien 			    __func__, (m)->m_pkthdr.len, total);	\
370b2da83c2SArchie Cobbs 		}							\
371b2da83c2SArchie Cobbs 	} while (0)
372b2da83c2SArchie Cobbs #else
373b2da83c2SArchie Cobbs #define CHECK_DATA_MBUF(m)
374b2da83c2SArchie Cobbs #endif
375b2da83c2SArchie Cobbs 
376e088dd4cSAlexander Motin #define ERROUT(x)	do { error = (x); goto done; } while (0)
377dc90cad9SJulian Elischer 
3784cf49a43SJulian Elischer /************************************************************************
379f8307e12SArchie Cobbs 	Parse type definitions for generic messages
380f8307e12SArchie Cobbs ************************************************************************/
381f8307e12SArchie Cobbs 
382f8307e12SArchie Cobbs /* Handy structure parse type defining macro */
383f8307e12SArchie Cobbs #define DEFINE_PARSE_STRUCT_TYPE(lo, up, args)				\
384f0184ff8SArchie Cobbs static const struct ng_parse_struct_field				\
385f0184ff8SArchie Cobbs 	ng_ ## lo ## _type_fields[] = NG_GENERIC_ ## up ## _INFO args;	\
386f8307e12SArchie Cobbs static const struct ng_parse_type ng_generic_ ## lo ## _type = {	\
387f8307e12SArchie Cobbs 	&ng_parse_struct_type,						\
388f0184ff8SArchie Cobbs 	&ng_ ## lo ## _type_fields					\
389f8307e12SArchie Cobbs }
390f8307e12SArchie Cobbs 
391f8307e12SArchie Cobbs DEFINE_PARSE_STRUCT_TYPE(mkpeer, MKPEER, ());
392f8307e12SArchie Cobbs DEFINE_PARSE_STRUCT_TYPE(connect, CONNECT, ());
393f8307e12SArchie Cobbs DEFINE_PARSE_STRUCT_TYPE(name, NAME, ());
394f8307e12SArchie Cobbs DEFINE_PARSE_STRUCT_TYPE(rmhook, RMHOOK, ());
395f8307e12SArchie Cobbs DEFINE_PARSE_STRUCT_TYPE(nodeinfo, NODEINFO, ());
396f8307e12SArchie Cobbs DEFINE_PARSE_STRUCT_TYPE(typeinfo, TYPEINFO, ());
397f8307e12SArchie Cobbs DEFINE_PARSE_STRUCT_TYPE(linkinfo, LINKINFO, (&ng_generic_nodeinfo_type));
398f8307e12SArchie Cobbs 
399f8307e12SArchie Cobbs /* Get length of an array when the length is stored as a 32 bit
400d7d97eb0SJeroen Ruigrok van der Werven    value immediately preceding the array -- as with struct namelist
401f8307e12SArchie Cobbs    and struct typelist. */
402f8307e12SArchie Cobbs static int
403f8307e12SArchie Cobbs ng_generic_list_getLength(const struct ng_parse_type *type,
404f8307e12SArchie Cobbs 	const u_char *start, const u_char *buf)
405f8307e12SArchie Cobbs {
406f8307e12SArchie Cobbs 	return *((const u_int32_t *)(buf - 4));
407f8307e12SArchie Cobbs }
408f8307e12SArchie Cobbs 
409f8307e12SArchie Cobbs /* Get length of the array of struct linkinfo inside a struct hooklist */
410f8307e12SArchie Cobbs static int
411f8307e12SArchie Cobbs ng_generic_linkinfo_getLength(const struct ng_parse_type *type,
412f8307e12SArchie Cobbs 	const u_char *start, const u_char *buf)
413f8307e12SArchie Cobbs {
414f8307e12SArchie Cobbs 	const struct hooklist *hl = (const struct hooklist *)start;
415f8307e12SArchie Cobbs 
416f8307e12SArchie Cobbs 	return hl->nodeinfo.hooks;
417f8307e12SArchie Cobbs }
418f8307e12SArchie Cobbs 
419f8307e12SArchie Cobbs /* Array type for a variable length array of struct namelist */
420f8307e12SArchie Cobbs static const struct ng_parse_array_info ng_nodeinfoarray_type_info = {
421f8307e12SArchie Cobbs 	&ng_generic_nodeinfo_type,
422f8307e12SArchie Cobbs 	&ng_generic_list_getLength
423f8307e12SArchie Cobbs };
424f8307e12SArchie Cobbs static const struct ng_parse_type ng_generic_nodeinfoarray_type = {
425f8307e12SArchie Cobbs 	&ng_parse_array_type,
426f8307e12SArchie Cobbs 	&ng_nodeinfoarray_type_info
427f8307e12SArchie Cobbs };
428f8307e12SArchie Cobbs 
429f8307e12SArchie Cobbs /* Array type for a variable length array of struct typelist */
430f8307e12SArchie Cobbs static const struct ng_parse_array_info ng_typeinfoarray_type_info = {
431f8307e12SArchie Cobbs 	&ng_generic_typeinfo_type,
432f8307e12SArchie Cobbs 	&ng_generic_list_getLength
433f8307e12SArchie Cobbs };
434f8307e12SArchie Cobbs static const struct ng_parse_type ng_generic_typeinfoarray_type = {
435f8307e12SArchie Cobbs 	&ng_parse_array_type,
436f8307e12SArchie Cobbs 	&ng_typeinfoarray_type_info
437f8307e12SArchie Cobbs };
438f8307e12SArchie Cobbs 
439f8307e12SArchie Cobbs /* Array type for array of struct linkinfo in struct hooklist */
440f8307e12SArchie Cobbs static const struct ng_parse_array_info ng_generic_linkinfo_array_type_info = {
441f8307e12SArchie Cobbs 	&ng_generic_linkinfo_type,
442f8307e12SArchie Cobbs 	&ng_generic_linkinfo_getLength
443f8307e12SArchie Cobbs };
444f8307e12SArchie Cobbs static const struct ng_parse_type ng_generic_linkinfo_array_type = {
445f8307e12SArchie Cobbs 	&ng_parse_array_type,
446f8307e12SArchie Cobbs 	&ng_generic_linkinfo_array_type_info
447f8307e12SArchie Cobbs };
448f8307e12SArchie Cobbs 
449f8307e12SArchie Cobbs DEFINE_PARSE_STRUCT_TYPE(typelist, TYPELIST, (&ng_generic_nodeinfoarray_type));
450f8307e12SArchie Cobbs DEFINE_PARSE_STRUCT_TYPE(hooklist, HOOKLIST,
451f8307e12SArchie Cobbs 	(&ng_generic_nodeinfo_type, &ng_generic_linkinfo_array_type));
452f8307e12SArchie Cobbs DEFINE_PARSE_STRUCT_TYPE(listnodes, LISTNODES,
453f8307e12SArchie Cobbs 	(&ng_generic_nodeinfoarray_type));
454f8307e12SArchie Cobbs 
455f8307e12SArchie Cobbs /* List of commands and how to convert arguments to/from ASCII */
456f8307e12SArchie Cobbs static const struct ng_cmdlist ng_generic_cmds[] = {
457f8307e12SArchie Cobbs 	{
458f8307e12SArchie Cobbs 	  NGM_GENERIC_COOKIE,
459f8307e12SArchie Cobbs 	  NGM_SHUTDOWN,
460f8307e12SArchie Cobbs 	  "shutdown",
461f8307e12SArchie Cobbs 	  NULL,
462f8307e12SArchie Cobbs 	  NULL
463f8307e12SArchie Cobbs 	},
464f8307e12SArchie Cobbs 	{
465f8307e12SArchie Cobbs 	  NGM_GENERIC_COOKIE,
466f8307e12SArchie Cobbs 	  NGM_MKPEER,
467f8307e12SArchie Cobbs 	  "mkpeer",
468f8307e12SArchie Cobbs 	  &ng_generic_mkpeer_type,
469f8307e12SArchie Cobbs 	  NULL
470f8307e12SArchie Cobbs 	},
471f8307e12SArchie Cobbs 	{
472f8307e12SArchie Cobbs 	  NGM_GENERIC_COOKIE,
473f8307e12SArchie Cobbs 	  NGM_CONNECT,
474f8307e12SArchie Cobbs 	  "connect",
475f8307e12SArchie Cobbs 	  &ng_generic_connect_type,
476f8307e12SArchie Cobbs 	  NULL
477f8307e12SArchie Cobbs 	},
478f8307e12SArchie Cobbs 	{
479f8307e12SArchie Cobbs 	  NGM_GENERIC_COOKIE,
480f8307e12SArchie Cobbs 	  NGM_NAME,
481f8307e12SArchie Cobbs 	  "name",
482f8307e12SArchie Cobbs 	  &ng_generic_name_type,
483f8307e12SArchie Cobbs 	  NULL
484f8307e12SArchie Cobbs 	},
485f8307e12SArchie Cobbs 	{
486f8307e12SArchie Cobbs 	  NGM_GENERIC_COOKIE,
487f8307e12SArchie Cobbs 	  NGM_RMHOOK,
488f8307e12SArchie Cobbs 	  "rmhook",
489f8307e12SArchie Cobbs 	  &ng_generic_rmhook_type,
490f8307e12SArchie Cobbs 	  NULL
491f8307e12SArchie Cobbs 	},
492f8307e12SArchie Cobbs 	{
493f8307e12SArchie Cobbs 	  NGM_GENERIC_COOKIE,
494f8307e12SArchie Cobbs 	  NGM_NODEINFO,
495f8307e12SArchie Cobbs 	  "nodeinfo",
496f8307e12SArchie Cobbs 	  NULL,
497f8307e12SArchie Cobbs 	  &ng_generic_nodeinfo_type
498f8307e12SArchie Cobbs 	},
499f8307e12SArchie Cobbs 	{
500f8307e12SArchie Cobbs 	  NGM_GENERIC_COOKIE,
501f8307e12SArchie Cobbs 	  NGM_LISTHOOKS,
502f8307e12SArchie Cobbs 	  "listhooks",
503f8307e12SArchie Cobbs 	  NULL,
504f8307e12SArchie Cobbs 	  &ng_generic_hooklist_type
505f8307e12SArchie Cobbs 	},
506f8307e12SArchie Cobbs 	{
507f8307e12SArchie Cobbs 	  NGM_GENERIC_COOKIE,
508f8307e12SArchie Cobbs 	  NGM_LISTNAMES,
509f8307e12SArchie Cobbs 	  "listnames",
510f8307e12SArchie Cobbs 	  NULL,
511f8307e12SArchie Cobbs 	  &ng_generic_listnodes_type	/* same as NGM_LISTNODES */
512f8307e12SArchie Cobbs 	},
513f8307e12SArchie Cobbs 	{
514f8307e12SArchie Cobbs 	  NGM_GENERIC_COOKIE,
515f8307e12SArchie Cobbs 	  NGM_LISTNODES,
516f8307e12SArchie Cobbs 	  "listnodes",
517f8307e12SArchie Cobbs 	  NULL,
518f8307e12SArchie Cobbs 	  &ng_generic_listnodes_type
519f8307e12SArchie Cobbs 	},
520f8307e12SArchie Cobbs 	{
521f8307e12SArchie Cobbs 	  NGM_GENERIC_COOKIE,
522f8307e12SArchie Cobbs 	  NGM_LISTTYPES,
523f8307e12SArchie Cobbs 	  "listtypes",
524f8307e12SArchie Cobbs 	  NULL,
525f8307e12SArchie Cobbs 	  &ng_generic_typeinfo_type
526f8307e12SArchie Cobbs 	},
527f8307e12SArchie Cobbs 	{
528f8307e12SArchie Cobbs 	  NGM_GENERIC_COOKIE,
5297095e097SPoul-Henning Kamp 	  NGM_TEXT_CONFIG,
5307095e097SPoul-Henning Kamp 	  "textconfig",
5317095e097SPoul-Henning Kamp 	  NULL,
5327095e097SPoul-Henning Kamp 	  &ng_parse_string_type
5337095e097SPoul-Henning Kamp 	},
5347095e097SPoul-Henning Kamp 	{
5357095e097SPoul-Henning Kamp 	  NGM_GENERIC_COOKIE,
536f8307e12SArchie Cobbs 	  NGM_TEXT_STATUS,
537f8307e12SArchie Cobbs 	  "textstatus",
538f8307e12SArchie Cobbs 	  NULL,
539f8307e12SArchie Cobbs 	  &ng_parse_string_type
540f8307e12SArchie Cobbs 	},
541f8307e12SArchie Cobbs 	{
542f8307e12SArchie Cobbs 	  NGM_GENERIC_COOKIE,
543f8307e12SArchie Cobbs 	  NGM_ASCII2BINARY,
544f8307e12SArchie Cobbs 	  "ascii2binary",
545f8307e12SArchie Cobbs 	  &ng_parse_ng_mesg_type,
546f8307e12SArchie Cobbs 	  &ng_parse_ng_mesg_type
547f8307e12SArchie Cobbs 	},
548f8307e12SArchie Cobbs 	{
549f8307e12SArchie Cobbs 	  NGM_GENERIC_COOKIE,
550f8307e12SArchie Cobbs 	  NGM_BINARY2ASCII,
551f8307e12SArchie Cobbs 	  "binary2ascii",
552f8307e12SArchie Cobbs 	  &ng_parse_ng_mesg_type,
553f8307e12SArchie Cobbs 	  &ng_parse_ng_mesg_type
554f8307e12SArchie Cobbs 	},
555f8307e12SArchie Cobbs 	{ 0 }
556f8307e12SArchie Cobbs };
557f8307e12SArchie Cobbs 
558f8307e12SArchie Cobbs /************************************************************************
5594cf49a43SJulian Elischer 			Node routines
5604cf49a43SJulian Elischer ************************************************************************/
5614cf49a43SJulian Elischer 
5624cf49a43SJulian Elischer /*
5634cf49a43SJulian Elischer  * Instantiate a node of the requested type
5644cf49a43SJulian Elischer  */
5654cf49a43SJulian Elischer int
5664cf49a43SJulian Elischer ng_make_node(const char *typename, node_p *nodepp)
5674cf49a43SJulian Elischer {
5684cf49a43SJulian Elischer 	struct ng_type *type;
569069154d5SJulian Elischer 	int	error;
5704cf49a43SJulian Elischer 
5714cf49a43SJulian Elischer 	/* Check that the type makes sense */
5724cf49a43SJulian Elischer 	if (typename == NULL) {
5736b795970SJulian Elischer 		TRAP_ERROR();
5744cf49a43SJulian Elischer 		return (EINVAL);
5754cf49a43SJulian Elischer 	}
5764cf49a43SJulian Elischer 
5777610f574SGleb Smirnoff 	/* Locate the node type. If we fail we return. Do not try to load
5787610f574SGleb Smirnoff 	 * module.
5797610f574SGleb Smirnoff 	 */
5804cf49a43SJulian Elischer 	if ((type = ng_findtype(typename)) == NULL)
5814cf49a43SJulian Elischer 		return (ENXIO);
5824cf49a43SJulian Elischer 
583069154d5SJulian Elischer 	/*
584069154d5SJulian Elischer 	 * If we have a constructor, then make the node and
585069154d5SJulian Elischer 	 * call the constructor to do type specific initialisation.
586069154d5SJulian Elischer 	 */
587069154d5SJulian Elischer 	if (type->constructor != NULL) {
588069154d5SJulian Elischer 		if ((error = ng_make_node_common(type, nodepp)) == 0) {
589069154d5SJulian Elischer 			if ((error = ((*type->constructor)(*nodepp)) != 0)) {
59030400f03SJulian Elischer 				NG_NODE_UNREF(*nodepp);
591069154d5SJulian Elischer 			}
592069154d5SJulian Elischer 		}
593069154d5SJulian Elischer 	} else {
594069154d5SJulian Elischer 		/*
595069154d5SJulian Elischer 		 * Node has no constructor. We cannot ask for one
59664efc707SRobert Watson 		 * to be made. It must be brought into existence by
597954c4772SJulian Elischer 		 * some external agency. The external agency should
598069154d5SJulian Elischer 		 * call ng_make_node_common() directly to get the
599069154d5SJulian Elischer 		 * netgraph part initialised.
600069154d5SJulian Elischer 		 */
6016b795970SJulian Elischer 		TRAP_ERROR();
602069154d5SJulian Elischer 		error = EINVAL;
603069154d5SJulian Elischer 	}
604069154d5SJulian Elischer 	return (error);
6054cf49a43SJulian Elischer }
6064cf49a43SJulian Elischer 
6074cf49a43SJulian Elischer /*
608069154d5SJulian Elischer  * Generic node creation. Called by node initialisation for externally
609069154d5SJulian Elischer  * instantiated nodes (e.g. hardware, sockets, etc ).
6104cf49a43SJulian Elischer  * The returned node has a reference count of 1.
6114cf49a43SJulian Elischer  */
6124cf49a43SJulian Elischer int
6134cf49a43SJulian Elischer ng_make_node_common(struct ng_type *type, node_p *nodepp)
6144cf49a43SJulian Elischer {
6154cf49a43SJulian Elischer 	node_p node;
6164cf49a43SJulian Elischer 
6174cf49a43SJulian Elischer 	/* Require the node type to have been already installed */
6184cf49a43SJulian Elischer 	if (ng_findtype(type->name) == NULL) {
6196b795970SJulian Elischer 		TRAP_ERROR();
6204cf49a43SJulian Elischer 		return (EINVAL);
6214cf49a43SJulian Elischer 	}
6224cf49a43SJulian Elischer 
6234cf49a43SJulian Elischer 	/* Make a node and try attach it to the type */
62430400f03SJulian Elischer 	NG_ALLOC_NODE(node);
6254cf49a43SJulian Elischer 	if (node == NULL) {
6266b795970SJulian Elischer 		TRAP_ERROR();
6274cf49a43SJulian Elischer 		return (ENOMEM);
6284cf49a43SJulian Elischer 	}
62930400f03SJulian Elischer 	node->nd_type = type;
63030400f03SJulian Elischer 	NG_NODE_REF(node);				/* note reference */
6314cf49a43SJulian Elischer 	type->refs++;
6324cf49a43SJulian Elischer 
6332c8dda8dSWojciech A. Koszek 	NG_QUEUE_LOCK_INIT(&node->nd_input_queue);
63430400f03SJulian Elischer 	node->nd_input_queue.queue = NULL;
63530400f03SJulian Elischer 	node->nd_input_queue.last = &node->nd_input_queue.queue;
63630400f03SJulian Elischer 	node->nd_input_queue.q_flags = 0;
63730400f03SJulian Elischer 	node->nd_input_queue.q_node = node;
6384cf49a43SJulian Elischer 
6394cf49a43SJulian Elischer 	/* Initialize hook list for new node */
64030400f03SJulian Elischer 	LIST_INIT(&node->nd_hooks);
6414cf49a43SJulian Elischer 
642cfea3f85SAlexander Motin 	/* Link us into the name hash. */
643cfea3f85SAlexander Motin 	mtx_lock(&ng_namehash_mtx);
644cfea3f85SAlexander Motin 	LIST_INSERT_HEAD(&ng_name_hash[0], node, nd_nodes);
645cfea3f85SAlexander Motin 	mtx_unlock(&ng_namehash_mtx);
646069154d5SJulian Elischer 
647dc90cad9SJulian Elischer 	/* get an ID and put us in the hash chain */
6489ed346baSBosko Milekic 	mtx_lock(&ng_idhash_mtx);
64930400f03SJulian Elischer 	for (;;) { /* wrap protection, even if silly */
650069154d5SJulian Elischer 		node_p node2 = NULL;
65130400f03SJulian Elischer 		node->nd_ID = nextID++; /* 137/second for 1 year before wrap */
6520f150d04SJulian Elischer 
65330400f03SJulian Elischer 		/* Is there a problem with the new number? */
6540f150d04SJulian Elischer 		NG_IDHASH_FIND(node->nd_ID, node2); /* already taken? */
6550f150d04SJulian Elischer 		if ((node->nd_ID != 0) && (node2 == NULL)) {
65630400f03SJulian Elischer 			break;
657069154d5SJulian Elischer 		}
65830400f03SJulian Elischer 	}
6590f150d04SJulian Elischer 	LIST_INSERT_HEAD(&ng_ID_hash[NG_IDHASH_FN(node->nd_ID)],
66030400f03SJulian Elischer 							node, nd_idnodes);
6619ed346baSBosko Milekic 	mtx_unlock(&ng_idhash_mtx);
662dc90cad9SJulian Elischer 
6634cf49a43SJulian Elischer 	/* Done */
6644cf49a43SJulian Elischer 	*nodepp = node;
6654cf49a43SJulian Elischer 	return (0);
6664cf49a43SJulian Elischer }
6674cf49a43SJulian Elischer 
6684cf49a43SJulian Elischer /*
6694cf49a43SJulian Elischer  * Forceably start the shutdown process on a node. Either call
67064efc707SRobert Watson  * its shutdown method, or do the default shutdown if there is
6714cf49a43SJulian Elischer  * no type-specific method.
6724cf49a43SJulian Elischer  *
67364efc707SRobert Watson  * We can only be called from a shutdown message, so we know we have
6743e4084c8SJulian Elischer  * a writer lock, and therefore exclusive access. It also means
6753e4084c8SJulian Elischer  * that we should not be on the work queue, but we check anyhow.
676069154d5SJulian Elischer  *
677069154d5SJulian Elischer  * Persistent node types must have a type-specific method which
67864efc707SRobert Watson  * allocates a new node in which case, this one is irretrievably going away,
6793e4084c8SJulian Elischer  * or cleans up anything it needs, and just makes the node valid again,
6803e4084c8SJulian Elischer  * in which case we allow the node to survive.
6813e4084c8SJulian Elischer  *
68264efc707SRobert Watson  * XXX We need to think of how to tell a persistent node that we
6833e4084c8SJulian Elischer  * REALLY need to go away because the hardware has gone or we
6843e4084c8SJulian Elischer  * are rebooting.... etc.
6854cf49a43SJulian Elischer  */
6864cf49a43SJulian Elischer void
6871acb27c6SJulian Elischer ng_rmnode(node_p node, hook_p dummy1, void *dummy2, int dummy3)
6884cf49a43SJulian Elischer {
6893e4084c8SJulian Elischer 	hook_p hook;
6903e4084c8SJulian Elischer 
6914cf49a43SJulian Elischer 	/* Check if it's already shutting down */
692be4252b3SJulian Elischer 	if ((node->nd_flags & NGF_CLOSING) != 0)
6934cf49a43SJulian Elischer 		return;
6944cf49a43SJulian Elischer 
6951acb27c6SJulian Elischer 	if (node == &ng_deadnode) {
6961acb27c6SJulian Elischer 		printf ("shutdown called on deadnode\n");
6971acb27c6SJulian Elischer 		return;
6981acb27c6SJulian Elischer 	}
6991acb27c6SJulian Elischer 
7004cf49a43SJulian Elischer 	/* Add an extra reference so it doesn't go away during this */
70130400f03SJulian Elischer 	NG_NODE_REF(node);
7024cf49a43SJulian Elischer 
70330400f03SJulian Elischer 	/*
70430400f03SJulian Elischer 	 * Mark it invalid so any newcomers know not to try use it
70530400f03SJulian Elischer 	 * Also add our own mark so we can't recurse
706be4252b3SJulian Elischer 	 * note that NGF_INVALID does not do this as it's also set during
70730400f03SJulian Elischer 	 * creation
70830400f03SJulian Elischer 	 */
709be4252b3SJulian Elischer 	node->nd_flags |= NGF_INVALID|NGF_CLOSING;
7104cf49a43SJulian Elischer 
711991fc65aSJulian Elischer 	/* If node has its pre-shutdown method, then call it first*/
712991fc65aSJulian Elischer 	if (node->nd_type && node->nd_type->close)
713991fc65aSJulian Elischer 		(*node->nd_type->close)(node);
714991fc65aSJulian Elischer 
7153e4084c8SJulian Elischer 	/* Notify all remaining connected nodes to disconnect */
7163e4084c8SJulian Elischer 	while ((hook = LIST_FIRST(&node->nd_hooks)) != NULL)
7173e4084c8SJulian Elischer 		ng_destroy_hook(hook);
71830400f03SJulian Elischer 
719069154d5SJulian Elischer 	/*
720069154d5SJulian Elischer 	 * Drain the input queue forceably.
72130400f03SJulian Elischer 	 * it has no hooks so what's it going to do, bleed on someone?
72230400f03SJulian Elischer 	 * Theoretically we came here from a queue entry that was added
72330400f03SJulian Elischer 	 * Just before the queue was closed, so it should be empty anyway.
724b57a7965SJulian Elischer 	 * Also removes us from worklist if needed.
725069154d5SJulian Elischer 	 */
72630400f03SJulian Elischer 	ng_flush_input_queue(&node->nd_input_queue);
727069154d5SJulian Elischer 
728069154d5SJulian Elischer 	/* Ask the type if it has anything to do in this case */
72930400f03SJulian Elischer 	if (node->nd_type && node->nd_type->shutdown) {
73030400f03SJulian Elischer 		(*node->nd_type->shutdown)(node);
73130400f03SJulian Elischer 		if (NG_NODE_IS_VALID(node)) {
73230400f03SJulian Elischer 			/*
73330400f03SJulian Elischer 			 * Well, blow me down if the node code hasn't declared
73430400f03SJulian Elischer 			 * that it doesn't want to die.
73530400f03SJulian Elischer 			 * Presumably it is a persistant node.
7361acb27c6SJulian Elischer 			 * If we REALLY want it to go away,
7371acb27c6SJulian Elischer 			 *  e.g. hardware going away,
738be4252b3SJulian Elischer 			 * Our caller should set NGF_REALLY_DIE in nd_flags.
73930400f03SJulian Elischer 			 */
740be4252b3SJulian Elischer 			node->nd_flags &= ~(NGF_INVALID|NGF_CLOSING);
7411acb27c6SJulian Elischer 			NG_NODE_UNREF(node); /* Assume they still have theirs */
74230400f03SJulian Elischer 			return;
7434cf49a43SJulian Elischer 		}
7441acb27c6SJulian Elischer 	} else {				/* do the default thing */
7451acb27c6SJulian Elischer 		NG_NODE_UNREF(node);
7461acb27c6SJulian Elischer 	}
7474cf49a43SJulian Elischer 
74830400f03SJulian Elischer 	ng_unname(node); /* basically a NOP these days */
74930400f03SJulian Elischer 
75030400f03SJulian Elischer 	/*
75130400f03SJulian Elischer 	 * Remove extra reference, possibly the last
75230400f03SJulian Elischer 	 * Possible other holders of references may include
75330400f03SJulian Elischer 	 * timeout callouts, but theoretically the node's supposed to
75430400f03SJulian Elischer 	 * have cancelled them. Possibly hardware dependencies may
75530400f03SJulian Elischer 	 * force a driver to 'linger' with a reference.
75630400f03SJulian Elischer 	 */
75730400f03SJulian Elischer 	NG_NODE_UNREF(node);
7584cf49a43SJulian Elischer }
7594cf49a43SJulian Elischer 
7605951069aSJulian Elischer /*
7615951069aSJulian Elischer  * Remove a reference to the node, possibly the last.
7625951069aSJulian Elischer  * deadnode always acts as it it were the last.
7635951069aSJulian Elischer  */
7645951069aSJulian Elischer int
76530400f03SJulian Elischer ng_unref_node(node_p node)
7664cf49a43SJulian Elischer {
76730400f03SJulian Elischer 	int v;
7686b795970SJulian Elischer 
7696b795970SJulian Elischer 	if (node == &ng_deadnode) {
7705951069aSJulian Elischer 		return (0);
7716b795970SJulian Elischer 	}
7726b795970SJulian Elischer 
773018fe3d1SAlexander Motin 	v = atomic_fetchadd_int(&node->nd_refs, -1);
774e8a49db2SJulian Elischer 
775018fe3d1SAlexander Motin 	if (v == 1) { /* we were the last */
776069154d5SJulian Elischer 
777cfea3f85SAlexander Motin 		mtx_lock(&ng_namehash_mtx);
77830400f03SJulian Elischer 		node->nd_type->refs--; /* XXX maybe should get types lock? */
77930400f03SJulian Elischer 		LIST_REMOVE(node, nd_nodes);
780cfea3f85SAlexander Motin 		mtx_unlock(&ng_namehash_mtx);
781069154d5SJulian Elischer 
7829ed346baSBosko Milekic 		mtx_lock(&ng_idhash_mtx);
78330400f03SJulian Elischer 		LIST_REMOVE(node, nd_idnodes);
7849ed346baSBosko Milekic 		mtx_unlock(&ng_idhash_mtx);
785069154d5SJulian Elischer 
78612574a02SJulian Elischer 		mtx_destroy(&node->nd_input_queue.q_mtx);
787069154d5SJulian Elischer 		NG_FREE_NODE(node);
7884cf49a43SJulian Elischer 	}
789018fe3d1SAlexander Motin 	return (v - 1);
7904cf49a43SJulian Elischer }
7914cf49a43SJulian Elischer 
7924cf49a43SJulian Elischer /************************************************************************
793dc90cad9SJulian Elischer 			Node ID handling
794dc90cad9SJulian Elischer ************************************************************************/
795dc90cad9SJulian Elischer static node_p
796069154d5SJulian Elischer ng_ID2noderef(ng_ID_t ID)
797dc90cad9SJulian Elischer {
79830400f03SJulian Elischer 	node_p node;
7999ed346baSBosko Milekic 	mtx_lock(&ng_idhash_mtx);
8000f150d04SJulian Elischer 	NG_IDHASH_FIND(ID, node);
80130400f03SJulian Elischer 	if(node)
80230400f03SJulian Elischer 		NG_NODE_REF(node);
8039ed346baSBosko Milekic 	mtx_unlock(&ng_idhash_mtx);
80430400f03SJulian Elischer 	return(node);
805dc90cad9SJulian Elischer }
806dc90cad9SJulian Elischer 
807dc90cad9SJulian Elischer ng_ID_t
808dc90cad9SJulian Elischer ng_node2ID(node_p node)
809dc90cad9SJulian Elischer {
81070de87f2SJulian Elischer 	return (node ? NG_NODE_ID(node) : 0);
811dc90cad9SJulian Elischer }
812dc90cad9SJulian Elischer 
813dc90cad9SJulian Elischer /************************************************************************
8144cf49a43SJulian Elischer 			Node name handling
8154cf49a43SJulian Elischer ************************************************************************/
8164cf49a43SJulian Elischer 
8174cf49a43SJulian Elischer /*
8184cf49a43SJulian Elischer  * Assign a node a name. Once assigned, the name cannot be changed.
8194cf49a43SJulian Elischer  */
8204cf49a43SJulian Elischer int
8214cf49a43SJulian Elischer ng_name_node(node_p node, const char *name)
8224cf49a43SJulian Elischer {
823cfea3f85SAlexander Motin 	int i, hash;
824069154d5SJulian Elischer 	node_p node2;
8254cf49a43SJulian Elischer 
8264cf49a43SJulian Elischer 	/* Check the name is valid */
82787e2c66aSHartmut Brandt 	for (i = 0; i < NG_NODESIZ; i++) {
8284cf49a43SJulian Elischer 		if (name[i] == '\0' || name[i] == '.' || name[i] == ':')
8294cf49a43SJulian Elischer 			break;
8304cf49a43SJulian Elischer 	}
8314cf49a43SJulian Elischer 	if (i == 0 || name[i] != '\0') {
8326b795970SJulian Elischer 		TRAP_ERROR();
8334cf49a43SJulian Elischer 		return (EINVAL);
8344cf49a43SJulian Elischer 	}
835dc90cad9SJulian Elischer 	if (ng_decodeidname(name) != 0) { /* valid IDs not allowed here */
8366b795970SJulian Elischer 		TRAP_ERROR();
8374cf49a43SJulian Elischer 		return (EINVAL);
8384cf49a43SJulian Elischer 	}
8394cf49a43SJulian Elischer 
8404cf49a43SJulian Elischer 	/* Check the name isn't already being used */
841069154d5SJulian Elischer 	if ((node2 = ng_name2noderef(node, name)) != NULL) {
84230400f03SJulian Elischer 		NG_NODE_UNREF(node2);
8436b795970SJulian Elischer 		TRAP_ERROR();
8444cf49a43SJulian Elischer 		return (EADDRINUSE);
8454cf49a43SJulian Elischer 	}
8464cf49a43SJulian Elischer 
847069154d5SJulian Elischer 	/* copy it */
84887e2c66aSHartmut Brandt 	strlcpy(NG_NODE_NAME(node), name, NG_NODESIZ);
8494cf49a43SJulian Elischer 
850cfea3f85SAlexander Motin 	/* Update name hash. */
851cfea3f85SAlexander Motin 	NG_NAMEHASH(name, hash);
852cfea3f85SAlexander Motin 	mtx_lock(&ng_namehash_mtx);
853cfea3f85SAlexander Motin 	LIST_REMOVE(node, nd_nodes);
854cfea3f85SAlexander Motin 	LIST_INSERT_HEAD(&ng_name_hash[hash], node, nd_nodes);
855cfea3f85SAlexander Motin 	mtx_unlock(&ng_namehash_mtx);
856cfea3f85SAlexander Motin 
8574cf49a43SJulian Elischer 	return (0);
8584cf49a43SJulian Elischer }
8594cf49a43SJulian Elischer 
8604cf49a43SJulian Elischer /*
8614cf49a43SJulian Elischer  * Find a node by absolute name. The name should NOT end with ':'
8624cf49a43SJulian Elischer  * The name "." means "this node" and "[xxx]" means "the node
8634cf49a43SJulian Elischer  * with ID (ie, at address) xxx".
8644cf49a43SJulian Elischer  *
8654cf49a43SJulian Elischer  * Returns the node if found, else NULL.
866069154d5SJulian Elischer  * Eventually should add something faster than a sequential search.
867e1e8f51bSRobert Watson  * Note it acquires a reference on the node so you can be sure it's still
868e1e8f51bSRobert Watson  * there.
8694cf49a43SJulian Elischer  */
8704cf49a43SJulian Elischer node_p
871069154d5SJulian Elischer ng_name2noderef(node_p here, const char *name)
8724cf49a43SJulian Elischer {
873dc90cad9SJulian Elischer 	node_p node;
874dc90cad9SJulian Elischer 	ng_ID_t temp;
875cfea3f85SAlexander Motin 	int	hash;
8764cf49a43SJulian Elischer 
8774cf49a43SJulian Elischer 	/* "." means "this node" */
878069154d5SJulian Elischer 	if (strcmp(name, ".") == 0) {
87930400f03SJulian Elischer 		NG_NODE_REF(here);
880069154d5SJulian Elischer 		return(here);
881069154d5SJulian Elischer 	}
8824cf49a43SJulian Elischer 
8834cf49a43SJulian Elischer 	/* Check for name-by-ID */
884dc90cad9SJulian Elischer 	if ((temp = ng_decodeidname(name)) != 0) {
885069154d5SJulian Elischer 		return (ng_ID2noderef(temp));
8864cf49a43SJulian Elischer 	}
8874cf49a43SJulian Elischer 
8884cf49a43SJulian Elischer 	/* Find node by name */
889cfea3f85SAlexander Motin 	NG_NAMEHASH(name, hash);
890cfea3f85SAlexander Motin 	mtx_lock(&ng_namehash_mtx);
891cfea3f85SAlexander Motin 	LIST_FOREACH(node, &ng_name_hash[hash], nd_nodes) {
892cfea3f85SAlexander Motin 		if (NG_NODE_IS_VALID(node) &&
893cfea3f85SAlexander Motin 		    (strcmp(NG_NODE_NAME(node), name) == 0)) {
8944cf49a43SJulian Elischer 			break;
8954cf49a43SJulian Elischer 		}
89670de87f2SJulian Elischer 	}
897069154d5SJulian Elischer 	if (node)
89830400f03SJulian Elischer 		NG_NODE_REF(node);
899cfea3f85SAlexander Motin 	mtx_unlock(&ng_namehash_mtx);
9004cf49a43SJulian Elischer 	return (node);
9014cf49a43SJulian Elischer }
9024cf49a43SJulian Elischer 
9034cf49a43SJulian Elischer /*
9049d5abbddSJens Schweikhardt  * Decode an ID name, eg. "[f03034de]". Returns 0 if the
905dc90cad9SJulian Elischer  * string is not valid, otherwise returns the value.
9064cf49a43SJulian Elischer  */
907dc90cad9SJulian Elischer static ng_ID_t
9084cf49a43SJulian Elischer ng_decodeidname(const char *name)
9094cf49a43SJulian Elischer {
9102b70adcbSArchie Cobbs 	const int len = strlen(name);
91125792ef3SArchie Cobbs 	char *eptr;
9122b70adcbSArchie Cobbs 	u_long val;
9134cf49a43SJulian Elischer 
9142b70adcbSArchie Cobbs 	/* Check for proper length, brackets, no leading junk */
91570de87f2SJulian Elischer 	if ((len < 3)
91670de87f2SJulian Elischer 	|| (name[0] != '[')
91770de87f2SJulian Elischer 	|| (name[len - 1] != ']')
91870de87f2SJulian Elischer 	|| (!isxdigit(name[1]))) {
91970de87f2SJulian Elischer 		return ((ng_ID_t)0);
92070de87f2SJulian Elischer 	}
9214cf49a43SJulian Elischer 
9222b70adcbSArchie Cobbs 	/* Decode number */
9232b70adcbSArchie Cobbs 	val = strtoul(name + 1, &eptr, 16);
92470de87f2SJulian Elischer 	if ((eptr - name != len - 1)
92570de87f2SJulian Elischer 	|| (val == ULONG_MAX)
92670de87f2SJulian Elischer 	|| (val == 0)) {
92712f035e0SJulian Elischer 		return ((ng_ID_t)0);
92870de87f2SJulian Elischer 	}
9292b70adcbSArchie Cobbs 	return (ng_ID_t)val;
9304cf49a43SJulian Elischer }
9314cf49a43SJulian Elischer 
9324cf49a43SJulian Elischer /*
9334cf49a43SJulian Elischer  * Remove a name from a node. This should only be called
9344cf49a43SJulian Elischer  * when shutting down and removing the node.
93564efc707SRobert Watson  * IF we allow name changing this may be more resurrected.
9364cf49a43SJulian Elischer  */
9374cf49a43SJulian Elischer void
9384cf49a43SJulian Elischer ng_unname(node_p node)
9394cf49a43SJulian Elischer {
9404cf49a43SJulian Elischer }
9414cf49a43SJulian Elischer 
9424cf49a43SJulian Elischer /************************************************************************
9434cf49a43SJulian Elischer 			Hook routines
9444cf49a43SJulian Elischer  Names are not optional. Hooks are always connected, except for a
9453e4084c8SJulian Elischer  brief moment within these routines. On invalidation or during creation
9463e4084c8SJulian Elischer  they are connected to the 'dead' hook.
9474cf49a43SJulian Elischer ************************************************************************/
9484cf49a43SJulian Elischer 
9494cf49a43SJulian Elischer /*
9504cf49a43SJulian Elischer  * Remove a hook reference
9514cf49a43SJulian Elischer  */
95230400f03SJulian Elischer void
9534cf49a43SJulian Elischer ng_unref_hook(hook_p hook)
9544cf49a43SJulian Elischer {
95530400f03SJulian Elischer 	int v;
9566b795970SJulian Elischer 
9576b795970SJulian Elischer 	if (hook == &ng_deadhook) {
9586b795970SJulian Elischer 		return;
9596b795970SJulian Elischer 	}
960018fe3d1SAlexander Motin 
961018fe3d1SAlexander Motin 	v = atomic_fetchadd_int(&hook->hk_refs, -1);
962e8a49db2SJulian Elischer 
96330400f03SJulian Elischer 	if (v == 1) { /* we were the last */
964f573da1aSAlexander Motin 		if (_NG_HOOK_NODE(hook)) /* it'll probably be ng_deadnode */
9656b795970SJulian Elischer 			_NG_NODE_UNREF((_NG_HOOK_NODE(hook)));
966069154d5SJulian Elischer 		NG_FREE_HOOK(hook);
967069154d5SJulian Elischer 	}
9684cf49a43SJulian Elischer }
9694cf49a43SJulian Elischer 
9704cf49a43SJulian Elischer /*
9714cf49a43SJulian Elischer  * Add an unconnected hook to a node. Only used internally.
9723e4084c8SJulian Elischer  * Assumes node is locked. (XXX not yet true )
9734cf49a43SJulian Elischer  */
9744cf49a43SJulian Elischer static int
9754cf49a43SJulian Elischer ng_add_hook(node_p node, const char *name, hook_p *hookp)
9764cf49a43SJulian Elischer {
9774cf49a43SJulian Elischer 	hook_p hook;
9784cf49a43SJulian Elischer 	int error = 0;
9794cf49a43SJulian Elischer 
9804cf49a43SJulian Elischer 	/* Check that the given name is good */
9814cf49a43SJulian Elischer 	if (name == NULL) {
9826b795970SJulian Elischer 		TRAP_ERROR();
9834cf49a43SJulian Elischer 		return (EINVAL);
9844cf49a43SJulian Elischer 	}
985899e9c4eSArchie Cobbs 	if (ng_findhook(node, name) != NULL) {
9866b795970SJulian Elischer 		TRAP_ERROR();
9874cf49a43SJulian Elischer 		return (EEXIST);
9884cf49a43SJulian Elischer 	}
9894cf49a43SJulian Elischer 
9904cf49a43SJulian Elischer 	/* Allocate the hook and link it up */
99130400f03SJulian Elischer 	NG_ALLOC_HOOK(hook);
9924cf49a43SJulian Elischer 	if (hook == NULL) {
9936b795970SJulian Elischer 		TRAP_ERROR();
9944cf49a43SJulian Elischer 		return (ENOMEM);
9954cf49a43SJulian Elischer 	}
9963e4084c8SJulian Elischer 	hook->hk_refs = 1;		/* add a reference for us to return */
99730400f03SJulian Elischer 	hook->hk_flags = HK_INVALID;
9983e4084c8SJulian Elischer 	hook->hk_peer = &ng_deadhook;	/* start off this way */
99930400f03SJulian Elischer 	hook->hk_node = node;
100030400f03SJulian Elischer 	NG_NODE_REF(node);		/* each hook counts as a reference */
10014cf49a43SJulian Elischer 
10023e4084c8SJulian Elischer 	/* Set hook name */
100387e2c66aSHartmut Brandt 	strlcpy(NG_HOOK_NAME(hook), name, NG_HOOKSIZ);
10043e4084c8SJulian Elischer 
10053e4084c8SJulian Elischer 	/*
10063e4084c8SJulian Elischer 	 * Check if the node type code has something to say about it
10073e4084c8SJulian Elischer 	 * If it fails, the unref of the hook will also unref the node.
10083e4084c8SJulian Elischer 	 */
1009954c4772SJulian Elischer 	if (node->nd_type->newhook != NULL) {
1010954c4772SJulian Elischer 		if ((error = (*node->nd_type->newhook)(node, hook, name))) {
101130400f03SJulian Elischer 			NG_HOOK_UNREF(hook);	/* this frees the hook */
1012069154d5SJulian Elischer 			return (error);
1013069154d5SJulian Elischer 		}
1014954c4772SJulian Elischer 	}
10154cf49a43SJulian Elischer 	/*
10164cf49a43SJulian Elischer 	 * The 'type' agrees so far, so go ahead and link it in.
10174cf49a43SJulian Elischer 	 * We'll ask again later when we actually connect the hooks.
10184cf49a43SJulian Elischer 	 */
101930400f03SJulian Elischer 	LIST_INSERT_HEAD(&node->nd_hooks, hook, hk_hooks);
102030400f03SJulian Elischer 	node->nd_numhooks++;
10213e4084c8SJulian Elischer 	NG_HOOK_REF(hook);	/* one for the node */
10224cf49a43SJulian Elischer 
10234cf49a43SJulian Elischer 	if (hookp)
10244cf49a43SJulian Elischer 		*hookp = hook;
10253e4084c8SJulian Elischer 	return (0);
10264cf49a43SJulian Elischer }
10274cf49a43SJulian Elischer 
10284cf49a43SJulian Elischer /*
1029899e9c4eSArchie Cobbs  * Find a hook
1030899e9c4eSArchie Cobbs  *
1031899e9c4eSArchie Cobbs  * Node types may supply their own optimized routines for finding
1032899e9c4eSArchie Cobbs  * hooks.  If none is supplied, we just do a linear search.
10333e4084c8SJulian Elischer  * XXX Possibly we should add a reference to the hook?
1034899e9c4eSArchie Cobbs  */
1035899e9c4eSArchie Cobbs hook_p
1036899e9c4eSArchie Cobbs ng_findhook(node_p node, const char *name)
1037899e9c4eSArchie Cobbs {
1038899e9c4eSArchie Cobbs 	hook_p hook;
1039899e9c4eSArchie Cobbs 
104030400f03SJulian Elischer 	if (node->nd_type->findhook != NULL)
104130400f03SJulian Elischer 		return (*node->nd_type->findhook)(node, name);
104230400f03SJulian Elischer 	LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) {
104370de87f2SJulian Elischer 		if (NG_HOOK_IS_VALID(hook)
1044ce5e5f99SArchie Cobbs 		&& (strcmp(NG_HOOK_NAME(hook), name) == 0))
1045899e9c4eSArchie Cobbs 			return (hook);
1046899e9c4eSArchie Cobbs 	}
1047899e9c4eSArchie Cobbs 	return (NULL);
1048899e9c4eSArchie Cobbs }
1049899e9c4eSArchie Cobbs 
1050899e9c4eSArchie Cobbs /*
10514cf49a43SJulian Elischer  * Destroy a hook
10524cf49a43SJulian Elischer  *
10534cf49a43SJulian Elischer  * As hooks are always attached, this really destroys two hooks.
10544cf49a43SJulian Elischer  * The one given, and the one attached to it. Disconnect the hooks
10553e4084c8SJulian Elischer  * from each other first. We reconnect the peer hook to the 'dead'
10563e4084c8SJulian Elischer  * hook so that it can still exist after we depart. We then
10573e4084c8SJulian Elischer  * send the peer its own destroy message. This ensures that we only
10583e4084c8SJulian Elischer  * interact with the peer's structures when it is locked processing that
10593e4084c8SJulian Elischer  * message. We hold a reference to the peer hook so we are guaranteed that
10603e4084c8SJulian Elischer  * the peer hook and node are still going to exist until
10613e4084c8SJulian Elischer  * we are finished there as the hook holds a ref on the node.
10623e4084c8SJulian Elischer  * We run this same code again on the peer hook, but that time it is already
10633e4084c8SJulian Elischer  * attached to the 'dead' hook.
10646b795970SJulian Elischer  *
10656b795970SJulian Elischer  * This routine is called at all stages of hook creation
10666b795970SJulian Elischer  * on error detection and must be able to handle any such stage.
10674cf49a43SJulian Elischer  */
10684cf49a43SJulian Elischer void
10694cf49a43SJulian Elischer ng_destroy_hook(hook_p hook)
10704cf49a43SJulian Elischer {
1071ac5dd141SGleb Smirnoff 	hook_p peer;
1072ac5dd141SGleb Smirnoff 	node_p node;
10734cf49a43SJulian Elischer 
10746b795970SJulian Elischer 	if (hook == &ng_deadhook) {	/* better safe than sorry */
10756b795970SJulian Elischer 		printf("ng_destroy_hook called on deadhook\n");
10766b795970SJulian Elischer 		return;
10776b795970SJulian Elischer 	}
1078ac5dd141SGleb Smirnoff 
1079ac5dd141SGleb Smirnoff 	/*
1080ac5dd141SGleb Smirnoff 	 * Protect divorce process with mutex, to avoid races on
1081ac5dd141SGleb Smirnoff 	 * simultaneous disconnect.
1082ac5dd141SGleb Smirnoff 	 */
1083ac5dd141SGleb Smirnoff 	mtx_lock(&ng_topo_mtx);
1084ac5dd141SGleb Smirnoff 
1085ac5dd141SGleb Smirnoff 	hook->hk_flags |= HK_INVALID;
1086ac5dd141SGleb Smirnoff 
1087ac5dd141SGleb Smirnoff 	peer = NG_HOOK_PEER(hook);
1088ac5dd141SGleb Smirnoff 	node = NG_HOOK_NODE(hook);
1089ac5dd141SGleb Smirnoff 
10903e4084c8SJulian Elischer 	if (peer && (peer != &ng_deadhook)) {
10913e4084c8SJulian Elischer 		/*
10923e4084c8SJulian Elischer 		 * Set the peer to point to ng_deadhook
10933e4084c8SJulian Elischer 		 * from this moment on we are effectively independent it.
10943e4084c8SJulian Elischer 		 * send it an rmhook message of it's own.
10953e4084c8SJulian Elischer 		 */
10963e4084c8SJulian Elischer 		peer->hk_peer = &ng_deadhook;	/* They no longer know us */
10973e4084c8SJulian Elischer 		hook->hk_peer = &ng_deadhook;	/* Nor us, them */
10986b795970SJulian Elischer 		if (NG_HOOK_NODE(peer) == &ng_deadnode) {
10996b795970SJulian Elischer 			/*
11006b795970SJulian Elischer 			 * If it's already divorced from a node,
11016b795970SJulian Elischer 			 * just free it.
11026b795970SJulian Elischer 			 */
1103ac5dd141SGleb Smirnoff 			mtx_unlock(&ng_topo_mtx);
11046b795970SJulian Elischer 		} else {
1105ac5dd141SGleb Smirnoff 			mtx_unlock(&ng_topo_mtx);
11066b795970SJulian Elischer 			ng_rmhook_self(peer); 	/* Send it a surprise */
11076b795970SJulian Elischer 		}
110852fa3556SJulian Elischer 		NG_HOOK_UNREF(peer);		/* account for peer link */
110952fa3556SJulian Elischer 		NG_HOOK_UNREF(hook);		/* account for peer link */
1110ac5dd141SGleb Smirnoff 	} else
1111ac5dd141SGleb Smirnoff 		mtx_unlock(&ng_topo_mtx);
1112ac5dd141SGleb Smirnoff 
1113ac5dd141SGleb Smirnoff 	mtx_assert(&ng_topo_mtx, MA_NOTOWNED);
11144cf49a43SJulian Elischer 
11154cf49a43SJulian Elischer 	/*
11164cf49a43SJulian Elischer 	 * Remove the hook from the node's list to avoid possible recursion
11174cf49a43SJulian Elischer 	 * in case the disconnection results in node shutdown.
11184cf49a43SJulian Elischer 	 */
11196b795970SJulian Elischer 	if (node == &ng_deadnode) { /* happens if called from ng_con_nodes() */
11206b795970SJulian Elischer 		return;
11216b795970SJulian Elischer 	}
112230400f03SJulian Elischer 	LIST_REMOVE(hook, hk_hooks);
112330400f03SJulian Elischer 	node->nd_numhooks--;
112430400f03SJulian Elischer 	if (node->nd_type->disconnect) {
11254cf49a43SJulian Elischer 		/*
11266b795970SJulian Elischer 		 * The type handler may elect to destroy the node so don't
112764efc707SRobert Watson 		 * trust its existence after this point. (except
11286b795970SJulian Elischer 		 * that we still hold a reference on it. (which we
11296b795970SJulian Elischer 		 * inherrited from the hook we are destroying)
11304cf49a43SJulian Elischer 		 */
113130400f03SJulian Elischer 		(*node->nd_type->disconnect) (hook);
11324cf49a43SJulian Elischer 	}
11336b795970SJulian Elischer 
11346b795970SJulian Elischer 	/*
11356b795970SJulian Elischer 	 * Note that because we will point to ng_deadnode, the original node
11366b795970SJulian Elischer 	 * is not decremented automatically so we do that manually.
11376b795970SJulian Elischer 	 */
11386b795970SJulian Elischer 	_NG_HOOK_NODE(hook) = &ng_deadnode;
11396b795970SJulian Elischer 	NG_NODE_UNREF(node);	/* We no longer point to it so adjust count */
11406b795970SJulian Elischer 	NG_HOOK_UNREF(hook);	/* Account for linkage (in list) to node */
11414cf49a43SJulian Elischer }
11424cf49a43SJulian Elischer 
11434cf49a43SJulian Elischer /*
11444cf49a43SJulian Elischer  * Take two hooks on a node and merge the connection so that the given node
11454cf49a43SJulian Elischer  * is effectively bypassed.
11464cf49a43SJulian Elischer  */
11474cf49a43SJulian Elischer int
11484cf49a43SJulian Elischer ng_bypass(hook_p hook1, hook_p hook2)
11494cf49a43SJulian Elischer {
115030400f03SJulian Elischer 	if (hook1->hk_node != hook2->hk_node) {
11516b795970SJulian Elischer 		TRAP_ERROR();
11524cf49a43SJulian Elischer 		return (EINVAL);
115330400f03SJulian Elischer 	}
115430400f03SJulian Elischer 	hook1->hk_peer->hk_peer = hook2->hk_peer;
115530400f03SJulian Elischer 	hook2->hk_peer->hk_peer = hook1->hk_peer;
11564cf49a43SJulian Elischer 
11573e4084c8SJulian Elischer 	hook1->hk_peer = &ng_deadhook;
11583e4084c8SJulian Elischer 	hook2->hk_peer = &ng_deadhook;
11593e4084c8SJulian Elischer 
1160cf3254aaSGleb Smirnoff 	NG_HOOK_UNREF(hook1);
1161cf3254aaSGleb Smirnoff 	NG_HOOK_UNREF(hook2);
1162cf3254aaSGleb Smirnoff 
11634cf49a43SJulian Elischer 	/* XXX If we ever cache methods on hooks update them as well */
11644cf49a43SJulian Elischer 	ng_destroy_hook(hook1);
11654cf49a43SJulian Elischer 	ng_destroy_hook(hook2);
11664cf49a43SJulian Elischer 	return (0);
11674cf49a43SJulian Elischer }
11684cf49a43SJulian Elischer 
11694cf49a43SJulian Elischer /*
11704cf49a43SJulian Elischer  * Install a new netgraph type
11714cf49a43SJulian Elischer  */
11724cf49a43SJulian Elischer int
11734cf49a43SJulian Elischer ng_newtype(struct ng_type *tp)
11744cf49a43SJulian Elischer {
11754cf49a43SJulian Elischer 	const size_t namelen = strlen(tp->name);
11764cf49a43SJulian Elischer 
11774cf49a43SJulian Elischer 	/* Check version and type name fields */
1178589f6ed8SJulian Elischer 	if ((tp->version != NG_ABI_VERSION)
1179589f6ed8SJulian Elischer 	|| (namelen == 0)
118087e2c66aSHartmut Brandt 	|| (namelen >= NG_TYPESIZ)) {
11816b795970SJulian Elischer 		TRAP_ERROR();
11828ed370fdSJulian Elischer 		if (tp->version != NG_ABI_VERSION) {
11838ed370fdSJulian Elischer 			printf("Netgraph: Node type rejected. ABI mismatch. Suggest recompile\n");
11848ed370fdSJulian Elischer 		}
11854cf49a43SJulian Elischer 		return (EINVAL);
11864cf49a43SJulian Elischer 	}
11874cf49a43SJulian Elischer 
11884cf49a43SJulian Elischer 	/* Check for name collision */
11894cf49a43SJulian Elischer 	if (ng_findtype(tp->name) != NULL) {
11906b795970SJulian Elischer 		TRAP_ERROR();
11914cf49a43SJulian Elischer 		return (EEXIST);
11924cf49a43SJulian Elischer 	}
11934cf49a43SJulian Elischer 
1194069154d5SJulian Elischer 
1195069154d5SJulian Elischer 	/* Link in new type */
11969ed346baSBosko Milekic 	mtx_lock(&ng_typelist_mtx);
1197069154d5SJulian Elischer 	LIST_INSERT_HEAD(&ng_typelist, tp, types);
1198c73b94a2SJulian Elischer 	tp->refs = 1;	/* first ref is linked list */
11999ed346baSBosko Milekic 	mtx_unlock(&ng_typelist_mtx);
12004cf49a43SJulian Elischer 	return (0);
12014cf49a43SJulian Elischer }
12024cf49a43SJulian Elischer 
12034cf49a43SJulian Elischer /*
1204c31b4a53SJulian Elischer  * unlink a netgraph type
1205c31b4a53SJulian Elischer  * If no examples exist
1206c31b4a53SJulian Elischer  */
1207c31b4a53SJulian Elischer int
1208c31b4a53SJulian Elischer ng_rmtype(struct ng_type *tp)
1209c31b4a53SJulian Elischer {
1210c31b4a53SJulian Elischer 	/* Check for name collision */
1211c31b4a53SJulian Elischer 	if (tp->refs != 1) {
1212c31b4a53SJulian Elischer 		TRAP_ERROR();
1213c31b4a53SJulian Elischer 		return (EBUSY);
1214c31b4a53SJulian Elischer 	}
1215c31b4a53SJulian Elischer 
1216c31b4a53SJulian Elischer 	/* Unlink type */
1217c31b4a53SJulian Elischer 	mtx_lock(&ng_typelist_mtx);
1218c31b4a53SJulian Elischer 	LIST_REMOVE(tp, types);
1219c31b4a53SJulian Elischer 	mtx_unlock(&ng_typelist_mtx);
1220c31b4a53SJulian Elischer 	return (0);
1221c31b4a53SJulian Elischer }
1222c31b4a53SJulian Elischer 
1223c31b4a53SJulian Elischer /*
12244cf49a43SJulian Elischer  * Look for a type of the name given
12254cf49a43SJulian Elischer  */
12264cf49a43SJulian Elischer struct ng_type *
12274cf49a43SJulian Elischer ng_findtype(const char *typename)
12284cf49a43SJulian Elischer {
12294cf49a43SJulian Elischer 	struct ng_type *type;
12304cf49a43SJulian Elischer 
12319ed346baSBosko Milekic 	mtx_lock(&ng_typelist_mtx);
1232069154d5SJulian Elischer 	LIST_FOREACH(type, &ng_typelist, types) {
12334cf49a43SJulian Elischer 		if (strcmp(type->name, typename) == 0)
12344cf49a43SJulian Elischer 			break;
12354cf49a43SJulian Elischer 	}
12369ed346baSBosko Milekic 	mtx_unlock(&ng_typelist_mtx);
12374cf49a43SJulian Elischer 	return (type);
12384cf49a43SJulian Elischer }
12394cf49a43SJulian Elischer 
12404cf49a43SJulian Elischer /************************************************************************
12414cf49a43SJulian Elischer 			Composite routines
12424cf49a43SJulian Elischer ************************************************************************/
12434cf49a43SJulian Elischer /*
12446b795970SJulian Elischer  * Connect two nodes using the specified hooks, using queued functions.
12454cf49a43SJulian Elischer  */
1246e088dd4cSAlexander Motin static int
1247e088dd4cSAlexander Motin ng_con_part3(node_p node, item_p item, hook_p hook)
12484cf49a43SJulian Elischer {
1249e088dd4cSAlexander Motin 	int	error = 0;
12504cf49a43SJulian Elischer 
12516b795970SJulian Elischer 	/*
12526b795970SJulian Elischer 	 * When we run, we know that the node 'node' is locked for us.
12536b795970SJulian Elischer 	 * Our caller has a reference on the hook.
12546b795970SJulian Elischer 	 * Our caller has a reference on the node.
12556b795970SJulian Elischer 	 * (In this case our caller is ng_apply_item() ).
12566b795970SJulian Elischer 	 * The peer hook has a reference on the hook.
12571acb27c6SJulian Elischer 	 * We are all set up except for the final call to the node, and
12581acb27c6SJulian Elischer 	 * the clearing of the INVALID flag.
12596b795970SJulian Elischer 	 */
12606b795970SJulian Elischer 	if (NG_HOOK_NODE(hook) == &ng_deadnode) {
12616b795970SJulian Elischer 		/*
12626b795970SJulian Elischer 		 * The node must have been freed again since we last visited
12636b795970SJulian Elischer 		 * here. ng_destry_hook() has this effect but nothing else does.
12646b795970SJulian Elischer 		 * We should just release our references and
12656b795970SJulian Elischer 		 * free anything we can think of.
12666b795970SJulian Elischer 		 * Since we know it's been destroyed, and it's our caller
12676b795970SJulian Elischer 		 * that holds the references, just return.
12686b795970SJulian Elischer 		 */
1269e088dd4cSAlexander Motin 		ERROUT(ENOENT);
12706b795970SJulian Elischer 	}
12716b795970SJulian Elischer 	if (hook->hk_node->nd_type->connect) {
1272e088dd4cSAlexander Motin 		if ((error = (*hook->hk_node->nd_type->connect) (hook))) {
12736b795970SJulian Elischer 			ng_destroy_hook(hook);	/* also zaps peer */
12741acb27c6SJulian Elischer 			printf("failed in ng_con_part3()\n");
1275e088dd4cSAlexander Motin 			ERROUT(error);
12764cf49a43SJulian Elischer 		}
12776b795970SJulian Elischer 	}
12786b795970SJulian Elischer 	/*
12796b795970SJulian Elischer 	 *  XXX this is wrong for SMP. Possibly we need
12806b795970SJulian Elischer 	 * to separate out 'create' and 'invalid' flags.
12816b795970SJulian Elischer 	 * should only set flags on hooks we have locked under our node.
12826b795970SJulian Elischer 	 */
12836b795970SJulian Elischer 	hook->hk_flags &= ~HK_INVALID;
1284e088dd4cSAlexander Motin done:
1285e088dd4cSAlexander Motin 	NG_FREE_ITEM(item);
1286e088dd4cSAlexander Motin 	return (error);
12876b795970SJulian Elischer }
12886b795970SJulian Elischer 
1289e088dd4cSAlexander Motin static int
1290e088dd4cSAlexander Motin ng_con_part2(node_p node, item_p item, hook_p hook)
12916b795970SJulian Elischer {
1292ac5dd141SGleb Smirnoff 	hook_p	peer;
1293e088dd4cSAlexander Motin 	int	error = 0;
12946b795970SJulian Elischer 
12956b795970SJulian Elischer 	/*
12966b795970SJulian Elischer 	 * When we run, we know that the node 'node' is locked for us.
12976b795970SJulian Elischer 	 * Our caller has a reference on the hook.
12986b795970SJulian Elischer 	 * Our caller has a reference on the node.
12996b795970SJulian Elischer 	 * (In this case our caller is ng_apply_item() ).
13006b795970SJulian Elischer 	 * The peer hook has a reference on the hook.
13016b795970SJulian Elischer 	 * our node pointer points to the 'dead' node.
13026b795970SJulian Elischer 	 * First check the hook name is unique.
13031acb27c6SJulian Elischer 	 * Should not happen because we checked before queueing this.
13046b795970SJulian Elischer 	 */
13056b795970SJulian Elischer 	if (ng_findhook(node, NG_HOOK_NAME(hook)) != NULL) {
13066b795970SJulian Elischer 		TRAP_ERROR();
13076b795970SJulian Elischer 		ng_destroy_hook(hook); /* should destroy peer too */
13081acb27c6SJulian Elischer 		printf("failed in ng_con_part2()\n");
1309e088dd4cSAlexander Motin 		ERROUT(EEXIST);
13106b795970SJulian Elischer 	}
13116b795970SJulian Elischer 	/*
13126b795970SJulian Elischer 	 * Check if the node type code has something to say about it
13136b795970SJulian Elischer 	 * If it fails, the unref of the hook will also unref the attached node,
13146b795970SJulian Elischer 	 * however since that node is 'ng_deadnode' this will do nothing.
13156b795970SJulian Elischer 	 * The peer hook will also be destroyed.
13166b795970SJulian Elischer 	 */
13176b795970SJulian Elischer 	if (node->nd_type->newhook != NULL) {
1318e088dd4cSAlexander Motin 		if ((error = (*node->nd_type->newhook)(node, hook,
1319e088dd4cSAlexander Motin 		    hook->hk_name))) {
13206b795970SJulian Elischer 			ng_destroy_hook(hook); /* should destroy peer too */
13211acb27c6SJulian Elischer 			printf("failed in ng_con_part2()\n");
1322e088dd4cSAlexander Motin 			ERROUT(error);
13236b795970SJulian Elischer 		}
13246b795970SJulian Elischer 	}
13256b795970SJulian Elischer 
13266b795970SJulian Elischer 	/*
13276b795970SJulian Elischer 	 * The 'type' agrees so far, so go ahead and link it in.
13286b795970SJulian Elischer 	 * We'll ask again later when we actually connect the hooks.
13296b795970SJulian Elischer 	 */
13306b795970SJulian Elischer 	hook->hk_node = node;		/* just overwrite ng_deadnode */
13316b795970SJulian Elischer 	NG_NODE_REF(node);		/* each hook counts as a reference */
13326b795970SJulian Elischer 	LIST_INSERT_HEAD(&node->nd_hooks, hook, hk_hooks);
13336b795970SJulian Elischer 	node->nd_numhooks++;
13346b795970SJulian Elischer 	NG_HOOK_REF(hook);	/* one for the node */
13356b795970SJulian Elischer 
13366b795970SJulian Elischer 	/*
133764efc707SRobert Watson 	 * We now have a symmetrical situation, where both hooks have been
13385951069aSJulian Elischer 	 * linked to their nodes, the newhook methods have been called
13396b795970SJulian Elischer 	 * And the references are all correct. The hooks are still marked
13406b795970SJulian Elischer 	 * as invalid, as we have not called the 'connect' methods
13416b795970SJulian Elischer 	 * yet.
134264efc707SRobert Watson 	 * We can call the local one immediately as we have the
13436b795970SJulian Elischer 	 * node locked, but we need to queue the remote one.
13446b795970SJulian Elischer 	 */
13456b795970SJulian Elischer 	if (hook->hk_node->nd_type->connect) {
1346e088dd4cSAlexander Motin 		if ((error = (*hook->hk_node->nd_type->connect) (hook))) {
13476b795970SJulian Elischer 			ng_destroy_hook(hook);	/* also zaps peer */
13481acb27c6SJulian Elischer 			printf("failed in ng_con_part2(A)\n");
1349e088dd4cSAlexander Motin 			ERROUT(error);
13506b795970SJulian Elischer 		}
13516b795970SJulian Elischer 	}
1352ac5dd141SGleb Smirnoff 
1353ac5dd141SGleb Smirnoff 	/*
1354ac5dd141SGleb Smirnoff 	 * Acquire topo mutex to avoid race with ng_destroy_hook().
1355ac5dd141SGleb Smirnoff 	 */
1356ac5dd141SGleb Smirnoff 	mtx_lock(&ng_topo_mtx);
1357ac5dd141SGleb Smirnoff 	peer = hook->hk_peer;
1358ac5dd141SGleb Smirnoff 	if (peer == &ng_deadhook) {
1359ac5dd141SGleb Smirnoff 		mtx_unlock(&ng_topo_mtx);
1360ac5dd141SGleb Smirnoff 		printf("failed in ng_con_part2(B)\n");
1361ac5dd141SGleb Smirnoff 		ng_destroy_hook(hook);
1362e088dd4cSAlexander Motin 		ERROUT(ENOENT);
1363ac5dd141SGleb Smirnoff 	}
1364ac5dd141SGleb Smirnoff 	mtx_unlock(&ng_topo_mtx);
1365ac5dd141SGleb Smirnoff 
1366b332b91fSGleb Smirnoff 	if ((error = ng_send_fn2(peer->hk_node, peer, item, &ng_con_part3,
1367b332b91fSGleb Smirnoff 	    NULL, 0, NG_REUSE_ITEM))) {
1368ac5dd141SGleb Smirnoff 		printf("failed in ng_con_part2(C)\n");
13691acb27c6SJulian Elischer 		ng_destroy_hook(hook);	/* also zaps peer */
1370e088dd4cSAlexander Motin 		return (error);		/* item was consumed. */
13711acb27c6SJulian Elischer 	}
13726b795970SJulian Elischer 	hook->hk_flags &= ~HK_INVALID; /* need both to be able to work */
1373e088dd4cSAlexander Motin 	return (0);			/* item was consumed. */
1374e088dd4cSAlexander Motin done:
1375e088dd4cSAlexander Motin 	NG_FREE_ITEM(item);
1376e088dd4cSAlexander Motin 	return (error);
13774cf49a43SJulian Elischer }
13784cf49a43SJulian Elischer 
13794cf49a43SJulian Elischer /*
13806b795970SJulian Elischer  * Connect this node with another node. We assume that this node is
13816b795970SJulian Elischer  * currently locked, as we are only called from an NGM_CONNECT message.
13824cf49a43SJulian Elischer  */
13836b795970SJulian Elischer static int
1384e088dd4cSAlexander Motin ng_con_nodes(item_p item, node_p node, const char *name,
1385e088dd4cSAlexander Motin     node_p node2, const char *name2)
13864cf49a43SJulian Elischer {
13874cf49a43SJulian Elischer 	int	error;
13884cf49a43SJulian Elischer 	hook_p	hook;
13894cf49a43SJulian Elischer 	hook_p	hook2;
13904cf49a43SJulian Elischer 
13911acb27c6SJulian Elischer 	if (ng_findhook(node2, name2) != NULL) {
13921acb27c6SJulian Elischer 		return(EEXIST);
13931acb27c6SJulian Elischer 	}
13943e4084c8SJulian Elischer 	if ((error = ng_add_hook(node, name, &hook)))  /* gives us a ref */
13954cf49a43SJulian Elischer 		return (error);
13966b795970SJulian Elischer 	/* Allocate the other hook and link it up */
13976b795970SJulian Elischer 	NG_ALLOC_HOOK(hook2);
139819724144SGleb Smirnoff 	if (hook2 == NULL) {
13996b795970SJulian Elischer 		TRAP_ERROR();
14006b795970SJulian Elischer 		ng_destroy_hook(hook);	/* XXX check ref counts so far */
14016b795970SJulian Elischer 		NG_HOOK_UNREF(hook);	/* including our ref */
14026b795970SJulian Elischer 		return (ENOMEM);
14036b795970SJulian Elischer 	}
14046b795970SJulian Elischer 	hook2->hk_refs = 1;		/* start with a reference for us. */
14056b795970SJulian Elischer 	hook2->hk_flags = HK_INVALID;
14066b795970SJulian Elischer 	hook2->hk_peer = hook;		/* Link the two together */
14076b795970SJulian Elischer 	hook->hk_peer = hook2;
14086b795970SJulian Elischer 	NG_HOOK_REF(hook);		/* Add a ref for the peer to each*/
14096b795970SJulian Elischer 	NG_HOOK_REF(hook2);
14106b795970SJulian Elischer 	hook2->hk_node = &ng_deadnode;
141187e2c66aSHartmut Brandt 	strlcpy(NG_HOOK_NAME(hook2), name2, NG_HOOKSIZ);
14126b795970SJulian Elischer 
14136b795970SJulian Elischer 	/*
14146b795970SJulian Elischer 	 * Queue the function above.
14156b795970SJulian Elischer 	 * Procesing continues in that function in the lock context of
14166b795970SJulian Elischer 	 * the other node.
14176b795970SJulian Elischer 	 */
1418b332b91fSGleb Smirnoff 	if ((error = ng_send_fn2(node2, hook2, item, &ng_con_part2, NULL, 0,
1419b332b91fSGleb Smirnoff 	    NG_NOFLAGS))) {
14203fb87c24SAlexander Motin 		printf("failed in ng_con_nodes(): %d\n", error);
14213fb87c24SAlexander Motin 		ng_destroy_hook(hook);	/* also zaps peer */
14223fb87c24SAlexander Motin 	}
14236b795970SJulian Elischer 
14246b795970SJulian Elischer 	NG_HOOK_UNREF(hook);		/* Let each hook go if it wants to */
14256b795970SJulian Elischer 	NG_HOOK_UNREF(hook2);
14263fb87c24SAlexander Motin 	return (error);
14274cf49a43SJulian Elischer }
14286b795970SJulian Elischer 
14296b795970SJulian Elischer /*
14306b795970SJulian Elischer  * Make a peer and connect.
14316b795970SJulian Elischer  * We assume that the local node is locked.
14326b795970SJulian Elischer  * The new node probably doesn't need a lock until
14336b795970SJulian Elischer  * it has a hook, because it cannot really have any work until then,
14346b795970SJulian Elischer  * but we should think about it a bit more.
14356b795970SJulian Elischer  *
14366b795970SJulian Elischer  * The problem may come if the other node also fires up
14376b795970SJulian Elischer  * some hardware or a timer or some other source of activation,
14386b795970SJulian Elischer  * also it may already get a command msg via it's ID.
14396b795970SJulian Elischer  *
14406b795970SJulian Elischer  * We could use the same method as ng_con_nodes() but we'd have
14416b795970SJulian Elischer  * to add ability to remove the node when failing. (Not hard, just
14426b795970SJulian Elischer  * make arg1 point to the node to remove).
14436b795970SJulian Elischer  * Unless of course we just ignore failure to connect and leave
14446b795970SJulian Elischer  * an unconnected node?
14456b795970SJulian Elischer  */
14466b795970SJulian Elischer static int
14476b795970SJulian Elischer ng_mkpeer(node_p node, const char *name, const char *name2, char *type)
14486b795970SJulian Elischer {
14496b795970SJulian Elischer 	node_p	node2;
14504c9b5910SGleb Smirnoff 	hook_p	hook1, hook2;
14516b795970SJulian Elischer 	int	error;
14526b795970SJulian Elischer 
14536b795970SJulian Elischer 	if ((error = ng_make_node(type, &node2))) {
14546b795970SJulian Elischer 		return (error);
14556b795970SJulian Elischer 	}
14566b795970SJulian Elischer 
14576b795970SJulian Elischer 	if ((error = ng_add_hook(node, name, &hook1))) { /* gives us a ref */
14581acb27c6SJulian Elischer 		ng_rmnode(node2, NULL, NULL, 0);
14596b795970SJulian Elischer 		return (error);
14606b795970SJulian Elischer 	}
14616b795970SJulian Elischer 
14626b795970SJulian Elischer 	if ((error = ng_add_hook(node2, name2, &hook2))) {
14631acb27c6SJulian Elischer 		ng_rmnode(node2, NULL, NULL, 0);
14646b795970SJulian Elischer 		ng_destroy_hook(hook1);
14656b795970SJulian Elischer 		NG_HOOK_UNREF(hook1);
14666b795970SJulian Elischer 		return (error);
14676b795970SJulian Elischer 	}
14686b795970SJulian Elischer 
14696b795970SJulian Elischer 	/*
14706b795970SJulian Elischer 	 * Actually link the two hooks together.
14716b795970SJulian Elischer 	 */
14726b795970SJulian Elischer 	hook1->hk_peer = hook2;
14736b795970SJulian Elischer 	hook2->hk_peer = hook1;
14746b795970SJulian Elischer 
14756b795970SJulian Elischer 	/* Each hook is referenced by the other */
14766b795970SJulian Elischer 	NG_HOOK_REF(hook1);
14776b795970SJulian Elischer 	NG_HOOK_REF(hook2);
14786b795970SJulian Elischer 
14796b795970SJulian Elischer 	/* Give each node the opportunity to veto the pending connection */
14806b795970SJulian Elischer 	if (hook1->hk_node->nd_type->connect) {
14816b795970SJulian Elischer 		error = (*hook1->hk_node->nd_type->connect) (hook1);
14826b795970SJulian Elischer 	}
14836b795970SJulian Elischer 
14846b795970SJulian Elischer 	if ((error == 0) && hook2->hk_node->nd_type->connect) {
14856b795970SJulian Elischer 		error = (*hook2->hk_node->nd_type->connect) (hook2);
14866b795970SJulian Elischer 
14876b795970SJulian Elischer 	}
14883e4084c8SJulian Elischer 
14893e4084c8SJulian Elischer 	/*
14903e4084c8SJulian Elischer 	 * drop the references we were holding on the two hooks.
14913e4084c8SJulian Elischer 	 */
14926b795970SJulian Elischer 	if (error) {
14936b795970SJulian Elischer 		ng_destroy_hook(hook2);	/* also zaps hook1 */
14941acb27c6SJulian Elischer 		ng_rmnode(node2, NULL, NULL, 0);
14956b795970SJulian Elischer 	} else {
14966b795970SJulian Elischer 		/* As a last act, allow the hooks to be used */
14976b795970SJulian Elischer 		hook1->hk_flags &= ~HK_INVALID;
14986b795970SJulian Elischer 		hook2->hk_flags &= ~HK_INVALID;
14996b795970SJulian Elischer 	}
15006b795970SJulian Elischer 	NG_HOOK_UNREF(hook1);
15013e4084c8SJulian Elischer 	NG_HOOK_UNREF(hook2);
15023e4084c8SJulian Elischer 	return (error);
15034cf49a43SJulian Elischer }
15046b795970SJulian Elischer 
1505069154d5SJulian Elischer /************************************************************************
1506069154d5SJulian Elischer 		Utility routines to send self messages
1507069154d5SJulian Elischer ************************************************************************/
1508069154d5SJulian Elischer 
15091acb27c6SJulian Elischer /* Shut this node down as soon as everyone is clear of it */
151064efc707SRobert Watson /* Should add arg "immediately" to jump the queue */
1511069154d5SJulian Elischer int
15121acb27c6SJulian Elischer ng_rmnode_self(node_p node)
1513069154d5SJulian Elischer {
15141acb27c6SJulian Elischer 	int		error;
15154cf49a43SJulian Elischer 
15161acb27c6SJulian Elischer 	if (node == &ng_deadnode)
15171acb27c6SJulian Elischer 		return (0);
1518be4252b3SJulian Elischer 	node->nd_flags |= NGF_INVALID;
1519be4252b3SJulian Elischer 	if (node->nd_flags & NGF_CLOSING)
15201acb27c6SJulian Elischer 		return (0);
1521069154d5SJulian Elischer 
15221acb27c6SJulian Elischer 	error = ng_send_fn(node, NULL, &ng_rmnode, NULL, 0);
15231acb27c6SJulian Elischer 	return (error);
1524069154d5SJulian Elischer }
1525069154d5SJulian Elischer 
15261acb27c6SJulian Elischer static void
15276b795970SJulian Elischer ng_rmhook_part2(node_p node, hook_p hook, void *arg1, int arg2)
15286b795970SJulian Elischer {
15296b795970SJulian Elischer 	ng_destroy_hook(hook);
15301acb27c6SJulian Elischer 	return ;
15316b795970SJulian Elischer }
15326b795970SJulian Elischer 
1533954c4772SJulian Elischer int
1534954c4772SJulian Elischer ng_rmhook_self(hook_p hook)
1535954c4772SJulian Elischer {
15366b795970SJulian Elischer 	int		error;
1537954c4772SJulian Elischer 	node_p node = NG_HOOK_NODE(hook);
1538954c4772SJulian Elischer 
15396b795970SJulian Elischer 	if (node == &ng_deadnode)
15406b795970SJulian Elischer 		return (0);
15416b795970SJulian Elischer 
15426b795970SJulian Elischer 	error = ng_send_fn(node, hook, &ng_rmhook_part2, NULL, 0);
15436b795970SJulian Elischer 	return (error);
1544954c4772SJulian Elischer }
1545954c4772SJulian Elischer 
1546069154d5SJulian Elischer /***********************************************************************
15474cf49a43SJulian Elischer  * Parse and verify a string of the form:  <NODE:><PATH>
15484cf49a43SJulian Elischer  *
15494cf49a43SJulian Elischer  * Such a string can refer to a specific node or a specific hook
15504cf49a43SJulian Elischer  * on a specific node, depending on how you look at it. In the
15514cf49a43SJulian Elischer  * latter case, the PATH component must not end in a dot.
15524cf49a43SJulian Elischer  *
15534cf49a43SJulian Elischer  * Both <NODE:> and <PATH> are optional. The <PATH> is a string
15544cf49a43SJulian Elischer  * of hook names separated by dots. This breaks out the original
15554cf49a43SJulian Elischer  * string, setting *nodep to "NODE" (or NULL if none) and *pathp
15564cf49a43SJulian Elischer  * to "PATH" (or NULL if degenerate). Also, *hookp will point to
15574cf49a43SJulian Elischer  * the final hook component of <PATH>, if any, otherwise NULL.
15584cf49a43SJulian Elischer  *
15594cf49a43SJulian Elischer  * This returns -1 if the path is malformed. The char ** are optional.
1560069154d5SJulian Elischer  ***********************************************************************/
15614cf49a43SJulian Elischer int
15624cf49a43SJulian Elischer ng_path_parse(char *addr, char **nodep, char **pathp, char **hookp)
15634cf49a43SJulian Elischer {
15644cf49a43SJulian Elischer 	char	*node, *path, *hook;
15654cf49a43SJulian Elischer 	int	k;
15664cf49a43SJulian Elischer 
15674cf49a43SJulian Elischer 	/*
15684cf49a43SJulian Elischer 	 * Extract absolute NODE, if any
15694cf49a43SJulian Elischer 	 */
15704cf49a43SJulian Elischer 	for (path = addr; *path && *path != ':'; path++);
15714cf49a43SJulian Elischer 	if (*path) {
15724cf49a43SJulian Elischer 		node = addr;	/* Here's the NODE */
15734cf49a43SJulian Elischer 		*path++ = '\0';	/* Here's the PATH */
15744cf49a43SJulian Elischer 
15754cf49a43SJulian Elischer 		/* Node name must not be empty */
15764cf49a43SJulian Elischer 		if (!*node)
15774cf49a43SJulian Elischer 			return -1;
15784cf49a43SJulian Elischer 
15794cf49a43SJulian Elischer 		/* A name of "." is OK; otherwise '.' not allowed */
15804cf49a43SJulian Elischer 		if (strcmp(node, ".") != 0) {
15814cf49a43SJulian Elischer 			for (k = 0; node[k]; k++)
15824cf49a43SJulian Elischer 				if (node[k] == '.')
15834cf49a43SJulian Elischer 					return -1;
15844cf49a43SJulian Elischer 		}
15854cf49a43SJulian Elischer 	} else {
15864cf49a43SJulian Elischer 		node = NULL;	/* No absolute NODE */
15874cf49a43SJulian Elischer 		path = addr;	/* Here's the PATH */
15884cf49a43SJulian Elischer 	}
15894cf49a43SJulian Elischer 
15904cf49a43SJulian Elischer 	/* Snoop for illegal characters in PATH */
15914cf49a43SJulian Elischer 	for (k = 0; path[k]; k++)
15924cf49a43SJulian Elischer 		if (path[k] == ':')
15934cf49a43SJulian Elischer 			return -1;
15944cf49a43SJulian Elischer 
15954cf49a43SJulian Elischer 	/* Check for no repeated dots in PATH */
15964cf49a43SJulian Elischer 	for (k = 0; path[k]; k++)
15974cf49a43SJulian Elischer 		if (path[k] == '.' && path[k + 1] == '.')
15984cf49a43SJulian Elischer 			return -1;
15994cf49a43SJulian Elischer 
16004cf49a43SJulian Elischer 	/* Remove extra (degenerate) dots from beginning or end of PATH */
16014cf49a43SJulian Elischer 	if (path[0] == '.')
16024cf49a43SJulian Elischer 		path++;
16034cf49a43SJulian Elischer 	if (*path && path[strlen(path) - 1] == '.')
16044cf49a43SJulian Elischer 		path[strlen(path) - 1] = 0;
16054cf49a43SJulian Elischer 
16064cf49a43SJulian Elischer 	/* If PATH has a dot, then we're not talking about a hook */
16074cf49a43SJulian Elischer 	if (*path) {
16084cf49a43SJulian Elischer 		for (hook = path, k = 0; path[k]; k++)
16094cf49a43SJulian Elischer 			if (path[k] == '.') {
16104cf49a43SJulian Elischer 				hook = NULL;
16114cf49a43SJulian Elischer 				break;
16124cf49a43SJulian Elischer 			}
16134cf49a43SJulian Elischer 	} else
16144cf49a43SJulian Elischer 		path = hook = NULL;
16154cf49a43SJulian Elischer 
16164cf49a43SJulian Elischer 	/* Done */
16174cf49a43SJulian Elischer 	if (nodep)
16184cf49a43SJulian Elischer 		*nodep = node;
16194cf49a43SJulian Elischer 	if (pathp)
16204cf49a43SJulian Elischer 		*pathp = path;
16214cf49a43SJulian Elischer 	if (hookp)
16224cf49a43SJulian Elischer 		*hookp = hook;
16234cf49a43SJulian Elischer 	return (0);
16244cf49a43SJulian Elischer }
16254cf49a43SJulian Elischer 
16264cf49a43SJulian Elischer /*
16274cf49a43SJulian Elischer  * Given a path, which may be absolute or relative, and a starting node,
1628069154d5SJulian Elischer  * return the destination node.
16294cf49a43SJulian Elischer  */
16304cf49a43SJulian Elischer int
1631069154d5SJulian Elischer ng_path2noderef(node_p here, const char *address,
1632069154d5SJulian Elischer 				node_p *destp, hook_p *lasthook)
16334cf49a43SJulian Elischer {
163487e2c66aSHartmut Brandt 	char    fullpath[NG_PATHSIZ];
16354cf49a43SJulian Elischer 	char   *nodename, *path, pbuf[2];
1636069154d5SJulian Elischer 	node_p  node, oldnode;
16374cf49a43SJulian Elischer 	char   *cp;
1638a4ec03cfSJulian Elischer 	hook_p hook = NULL;
16394cf49a43SJulian Elischer 
16404cf49a43SJulian Elischer 	/* Initialize */
164130400f03SJulian Elischer 	if (destp == NULL) {
16426b795970SJulian Elischer 		TRAP_ERROR();
16434cf49a43SJulian Elischer 		return EINVAL;
164430400f03SJulian Elischer 	}
16454cf49a43SJulian Elischer 	*destp = NULL;
16464cf49a43SJulian Elischer 
16474cf49a43SJulian Elischer 	/* Make a writable copy of address for ng_path_parse() */
16484cf49a43SJulian Elischer 	strncpy(fullpath, address, sizeof(fullpath) - 1);
16494cf49a43SJulian Elischer 	fullpath[sizeof(fullpath) - 1] = '\0';
16504cf49a43SJulian Elischer 
16514cf49a43SJulian Elischer 	/* Parse out node and sequence of hooks */
16524cf49a43SJulian Elischer 	if (ng_path_parse(fullpath, &nodename, &path, NULL) < 0) {
16536b795970SJulian Elischer 		TRAP_ERROR();
16544cf49a43SJulian Elischer 		return EINVAL;
16554cf49a43SJulian Elischer 	}
16564cf49a43SJulian Elischer 	if (path == NULL) {
16574cf49a43SJulian Elischer 		pbuf[0] = '.';	/* Needs to be writable */
16584cf49a43SJulian Elischer 		pbuf[1] = '\0';
16594cf49a43SJulian Elischer 		path = pbuf;
16604cf49a43SJulian Elischer 	}
16614cf49a43SJulian Elischer 
1662069154d5SJulian Elischer 	/*
1663069154d5SJulian Elischer 	 * For an absolute address, jump to the starting node.
1664069154d5SJulian Elischer 	 * Note that this holds a reference on the node for us.
1665069154d5SJulian Elischer 	 * Don't forget to drop the reference if we don't need it.
1666069154d5SJulian Elischer 	 */
16674cf49a43SJulian Elischer 	if (nodename) {
1668069154d5SJulian Elischer 		node = ng_name2noderef(here, nodename);
16694cf49a43SJulian Elischer 		if (node == NULL) {
16706b795970SJulian Elischer 			TRAP_ERROR();
16714cf49a43SJulian Elischer 			return (ENOENT);
16724cf49a43SJulian Elischer 		}
1673069154d5SJulian Elischer 	} else {
1674069154d5SJulian Elischer 		if (here == NULL) {
16756b795970SJulian Elischer 			TRAP_ERROR();
1676069154d5SJulian Elischer 			return (EINVAL);
1677069154d5SJulian Elischer 		}
16784cf49a43SJulian Elischer 		node = here;
167930400f03SJulian Elischer 		NG_NODE_REF(node);
1680069154d5SJulian Elischer 	}
16814cf49a43SJulian Elischer 
1682069154d5SJulian Elischer 	/*
1683069154d5SJulian Elischer 	 * Now follow the sequence of hooks
1684069154d5SJulian Elischer 	 * XXX
1685069154d5SJulian Elischer 	 * We actually cannot guarantee that the sequence
1686069154d5SJulian Elischer 	 * is not being demolished as we crawl along it
1687069154d5SJulian Elischer 	 * without extra-ordinary locking etc.
1688069154d5SJulian Elischer 	 * So this is a bit dodgy to say the least.
1689069154d5SJulian Elischer 	 * We can probably hold up some things by holding
1690069154d5SJulian Elischer 	 * the nodelist mutex for the time of this
1691069154d5SJulian Elischer 	 * crawl if we wanted.. At least that way we wouldn't have to
169264efc707SRobert Watson 	 * worry about the nodes disappearing, but the hooks would still
1693069154d5SJulian Elischer 	 * be a problem.
1694069154d5SJulian Elischer 	 */
16954cf49a43SJulian Elischer 	for (cp = path; node != NULL && *cp != '\0'; ) {
16964cf49a43SJulian Elischer 		char *segment;
16974cf49a43SJulian Elischer 
16984cf49a43SJulian Elischer 		/*
16994cf49a43SJulian Elischer 		 * Break out the next path segment. Replace the dot we just
17004cf49a43SJulian Elischer 		 * found with a NUL; "cp" points to the next segment (or the
17014cf49a43SJulian Elischer 		 * NUL at the end).
17024cf49a43SJulian Elischer 		 */
17034cf49a43SJulian Elischer 		for (segment = cp; *cp != '\0'; cp++) {
17044cf49a43SJulian Elischer 			if (*cp == '.') {
17054cf49a43SJulian Elischer 				*cp++ = '\0';
17064cf49a43SJulian Elischer 				break;
17074cf49a43SJulian Elischer 			}
17084cf49a43SJulian Elischer 		}
17094cf49a43SJulian Elischer 
17104cf49a43SJulian Elischer 		/* Empty segment */
17114cf49a43SJulian Elischer 		if (*segment == '\0')
17124cf49a43SJulian Elischer 			continue;
17134cf49a43SJulian Elischer 
17144cf49a43SJulian Elischer 		/* We have a segment, so look for a hook by that name */
1715899e9c4eSArchie Cobbs 		hook = ng_findhook(node, segment);
17164cf49a43SJulian Elischer 
17174cf49a43SJulian Elischer 		/* Can't get there from here... */
17184cf49a43SJulian Elischer 		if (hook == NULL
171930400f03SJulian Elischer 		    || NG_HOOK_PEER(hook) == NULL
172030400f03SJulian Elischer 		    || NG_HOOK_NOT_VALID(hook)
172130400f03SJulian Elischer 		    || NG_HOOK_NOT_VALID(NG_HOOK_PEER(hook))) {
17226b795970SJulian Elischer 			TRAP_ERROR();
172330400f03SJulian Elischer 			NG_NODE_UNREF(node);
172430400f03SJulian Elischer #if 0
172530400f03SJulian Elischer 			printf("hooknotvalid %s %s %d %d %d %d ",
172630400f03SJulian Elischer 					path,
172730400f03SJulian Elischer 					segment,
172830400f03SJulian Elischer 					hook == NULL,
172930400f03SJulian Elischer 					NG_HOOK_PEER(hook) == NULL,
173030400f03SJulian Elischer 					NG_HOOK_NOT_VALID(hook),
173130400f03SJulian Elischer 					NG_HOOK_NOT_VALID(NG_HOOK_PEER(hook)));
173230400f03SJulian Elischer #endif
17334cf49a43SJulian Elischer 			return (ENOENT);
17344cf49a43SJulian Elischer 		}
17354cf49a43SJulian Elischer 
1736069154d5SJulian Elischer 		/*
1737069154d5SJulian Elischer 		 * Hop on over to the next node
1738069154d5SJulian Elischer 		 * XXX
1739069154d5SJulian Elischer 		 * Big race conditions here as hooks and nodes go away
1740069154d5SJulian Elischer 		 * *** Idea.. store an ng_ID_t in each hook and use that
1741069154d5SJulian Elischer 		 * instead of the direct hook in this crawl?
1742069154d5SJulian Elischer 		 */
1743069154d5SJulian Elischer 		oldnode = node;
174430400f03SJulian Elischer 		if ((node = NG_PEER_NODE(hook)))
174530400f03SJulian Elischer 			NG_NODE_REF(node);	/* XXX RACE */
174630400f03SJulian Elischer 		NG_NODE_UNREF(oldnode);	/* XXX another race */
174730400f03SJulian Elischer 		if (NG_NODE_NOT_VALID(node)) {
174830400f03SJulian Elischer 			NG_NODE_UNREF(node);	/* XXX more races */
1749069154d5SJulian Elischer 			node = NULL;
1750069154d5SJulian Elischer 		}
17514cf49a43SJulian Elischer 	}
17524cf49a43SJulian Elischer 
17534cf49a43SJulian Elischer 	/* If node somehow missing, fail here (probably this is not needed) */
17544cf49a43SJulian Elischer 	if (node == NULL) {
17556b795970SJulian Elischer 		TRAP_ERROR();
17564cf49a43SJulian Elischer 		return (ENXIO);
17574cf49a43SJulian Elischer 	}
17584cf49a43SJulian Elischer 
17594cf49a43SJulian Elischer 	/* Done */
17604cf49a43SJulian Elischer 	*destp = node;
17611bdebe4dSArchie Cobbs 	if (lasthook != NULL)
176230400f03SJulian Elischer 		*lasthook = (hook ? NG_HOOK_PEER(hook) : NULL);
1763069154d5SJulian Elischer 	return (0);
1764069154d5SJulian Elischer }
1765069154d5SJulian Elischer 
1766069154d5SJulian Elischer /***************************************************************\
1767069154d5SJulian Elischer * Input queue handling.
1768069154d5SJulian Elischer * All activities are submitted to the node via the input queue
1769069154d5SJulian Elischer * which implements a multiple-reader/single-writer gate.
177064efc707SRobert Watson * Items which cannot be handled immediately are queued.
1771069154d5SJulian Elischer *
1772069154d5SJulian Elischer * read-write queue locking inline functions			*
1773069154d5SJulian Elischer \***************************************************************/
1774069154d5SJulian Elischer 
1775714fb865SGleb Smirnoff static __inline item_p ng_dequeue(struct ng_queue * ngq, int *rw);
1776069154d5SJulian Elischer static __inline item_p ng_acquire_read(struct ng_queue * ngq,
1777069154d5SJulian Elischer 					item_p  item);
1778069154d5SJulian Elischer static __inline item_p ng_acquire_write(struct ng_queue * ngq,
1779069154d5SJulian Elischer 					item_p  item);
1780069154d5SJulian Elischer static __inline void	ng_leave_read(struct ng_queue * ngq);
1781069154d5SJulian Elischer static __inline void	ng_leave_write(struct ng_queue * ngq);
1782069154d5SJulian Elischer static __inline void	ng_queue_rw(struct ng_queue * ngq,
1783069154d5SJulian Elischer 					item_p  item, int rw);
1784069154d5SJulian Elischer 
1785069154d5SJulian Elischer /*
1786069154d5SJulian Elischer  * Definition of the bits fields in the ng_queue flag word.
1787069154d5SJulian Elischer  * Defined here rather than in netgraph.h because no-one should fiddle
1788069154d5SJulian Elischer  * with them.
1789069154d5SJulian Elischer  *
1790b57a7965SJulian Elischer  * The ordering here may be important! don't shuffle these.
1791069154d5SJulian Elischer  */
1792069154d5SJulian Elischer /*-
1793069154d5SJulian Elischer  Safety Barrier--------+ (adjustable to suit taste) (not used yet)
1794069154d5SJulian Elischer                        |
1795069154d5SJulian Elischer                        V
1796069154d5SJulian Elischer +-------+-------+-------+-------+-------+-------+-------+-------+
17974be59335SGleb Smirnoff   | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
17984be59335SGleb Smirnoff   | |A|c|t|i|v|e| |R|e|a|d|e|r| |C|o|u|n|t| | | | | | | | | |P|A|
17994be59335SGleb Smirnoff   | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |O|W|
1800069154d5SJulian Elischer +-------+-------+-------+-------+-------+-------+-------+-------+
18014be59335SGleb Smirnoff   \___________________________ ____________________________/ | |
18024be59335SGleb Smirnoff                             V                                | |
18034be59335SGleb Smirnoff                   [active reader count]                      | |
1804069154d5SJulian Elischer                                                              | |
18054be59335SGleb Smirnoff             Operation Pending -------------------------------+ |
1806069154d5SJulian Elischer                                                                |
18074be59335SGleb Smirnoff           Active Writer ---------------------------------------+
1808b57a7965SJulian Elischer 
1809b57a7965SJulian Elischer 
1810069154d5SJulian Elischer */
18114be59335SGleb Smirnoff #define WRITER_ACTIVE	0x00000001
18124be59335SGleb Smirnoff #define OP_PENDING	0x00000002
18134be59335SGleb Smirnoff #define READER_INCREMENT 0x00000004
18144be59335SGleb Smirnoff #define READER_MASK	0xfffffffc	/* Not valid if WRITER_ACTIVE is set */
18154be59335SGleb Smirnoff #define SAFETY_BARRIER	0x00100000	/* 128K items queued should be enough */
1816b57a7965SJulian Elischer 
1817b57a7965SJulian Elischer /* Defines of more elaborate states on the queue */
18184be59335SGleb Smirnoff /* Mask of bits a new read cares about */
18194be59335SGleb Smirnoff #define NGQ_RMASK	(WRITER_ACTIVE|OP_PENDING)
1820b57a7965SJulian Elischer 
18214be59335SGleb Smirnoff /* Mask of bits a new write cares about */
1822b57a7965SJulian Elischer #define NGQ_WMASK	(NGQ_RMASK|READER_MASK)
1823b57a7965SJulian Elischer 
18244be59335SGleb Smirnoff /* Test to decide if there is something on the queue. */
18254be59335SGleb Smirnoff #define QUEUE_ACTIVE(QP) ((QP)->q_flags & OP_PENDING)
18264be59335SGleb Smirnoff 
18274be59335SGleb Smirnoff /* How to decide what the next queued item is. */
18284be59335SGleb Smirnoff #define HEAD_IS_READER(QP)  NGI_QUEUED_READER((QP)->queue)
18294be59335SGleb Smirnoff #define HEAD_IS_WRITER(QP)  NGI_QUEUED_WRITER((QP)->queue) /* notused */
18304be59335SGleb Smirnoff 
18314be59335SGleb Smirnoff /* Read the status to decide if the next item on the queue can now run. */
18324be59335SGleb Smirnoff #define QUEUED_READER_CAN_PROCEED(QP)			\
18334be59335SGleb Smirnoff 		(((QP)->q_flags & (NGQ_RMASK & ~OP_PENDING)) == 0)
18344be59335SGleb Smirnoff #define QUEUED_WRITER_CAN_PROCEED(QP)			\
18354be59335SGleb Smirnoff 		(((QP)->q_flags & (NGQ_WMASK & ~OP_PENDING)) == 0)
1836b57a7965SJulian Elischer 
1837b57a7965SJulian Elischer /* Is there a chance of getting ANY work off the queue? */
18384be59335SGleb Smirnoff #define NEXT_QUEUED_ITEM_CAN_PROCEED(QP)				\
18394be59335SGleb Smirnoff 	(QUEUE_ACTIVE(QP) && 						\
18404be59335SGleb Smirnoff 	((HEAD_IS_READER(QP)) ? QUEUED_READER_CAN_PROCEED(QP) :		\
18414be59335SGleb Smirnoff 				QUEUED_WRITER_CAN_PROCEED(QP)))
18424be59335SGleb Smirnoff 
1843b57a7965SJulian Elischer 
1844714fb865SGleb Smirnoff #define NGQRW_R 0
1845714fb865SGleb Smirnoff #define NGQRW_W 1
1846714fb865SGleb Smirnoff 
1847069154d5SJulian Elischer /*
1848069154d5SJulian Elischer  * Taking into account the current state of the queue and node, possibly take
1849069154d5SJulian Elischer  * the next entry off the queue and return it. Return NULL if there was
1850069154d5SJulian Elischer  * nothing we could return, either because there really was nothing there, or
1851069154d5SJulian Elischer  * because the node was in a state where it cannot yet process the next item
1852069154d5SJulian Elischer  * on the queue.
1853069154d5SJulian Elischer  *
1854069154d5SJulian Elischer  * This MUST MUST MUST be called with the mutex held.
1855069154d5SJulian Elischer  */
1856069154d5SJulian Elischer static __inline item_p
1857714fb865SGleb Smirnoff ng_dequeue(struct ng_queue *ngq, int *rw)
1858069154d5SJulian Elischer {
1859069154d5SJulian Elischer 	item_p item;
1860069154d5SJulian Elischer 	u_int		add_arg;
1861b57a7965SJulian Elischer 
1862d58e678fSGleb Smirnoff 	mtx_assert(&ngq->q_mtx, MA_OWNED);
18634be59335SGleb Smirnoff 	/*
18644be59335SGleb Smirnoff 	 * If there is nothing queued, then just return.
18654be59335SGleb Smirnoff 	 * No point in continuing.
18664be59335SGleb Smirnoff 	 * XXXGL: assert this?
18674be59335SGleb Smirnoff 	 */
18684be59335SGleb Smirnoff 	if (!QUEUE_ACTIVE(ngq)) {
18692955ee18SGleb Smirnoff 		CTR4(KTR_NET, "%20s: node [%x] (%p) queue empty; "
18702955ee18SGleb Smirnoff 		    "queue flags 0x%lx", __func__,
18712955ee18SGleb Smirnoff 		    ngq->q_node->nd_ID, ngq->q_node, ngq->q_flags);
18724be59335SGleb Smirnoff 		return (NULL);
18734be59335SGleb Smirnoff 	}
1874d58e678fSGleb Smirnoff 
18754be59335SGleb Smirnoff 	/*
18764be59335SGleb Smirnoff 	 * From here, we can assume there is a head item.
18774be59335SGleb Smirnoff 	 * We need to find out what it is and if it can be dequeued, given
18784be59335SGleb Smirnoff 	 * the current state of the node.
18794be59335SGleb Smirnoff 	 */
18804be59335SGleb Smirnoff 	if (HEAD_IS_READER(ngq)) {
18814be59335SGleb Smirnoff 		if (!QUEUED_READER_CAN_PROCEED(ngq)) {
18824be59335SGleb Smirnoff 			/*
18834be59335SGleb Smirnoff 			 * It's a reader but we can't use it.
18844be59335SGleb Smirnoff 			 * We are stalled so make sure we don't
18854be59335SGleb Smirnoff 			 * get called again until something changes.
18864be59335SGleb Smirnoff 			 */
18874be59335SGleb Smirnoff 			ng_worklist_remove(ngq->q_node);
18882955ee18SGleb Smirnoff 			CTR4(KTR_NET, "%20s: node [%x] (%p) queued reader "
18892955ee18SGleb Smirnoff 			    "can't proceed; queue flags 0x%lx", __func__,
18902955ee18SGleb Smirnoff 			    ngq->q_node->nd_ID, ngq->q_node, ngq->q_flags);
18914be59335SGleb Smirnoff 			return (NULL);
18924be59335SGleb Smirnoff 		}
1893069154d5SJulian Elischer 		/*
1894b57a7965SJulian Elischer 		 * Head of queue is a reader and we have no write active.
1895b57a7965SJulian Elischer 		 * We don't care how many readers are already active.
18964be59335SGleb Smirnoff 		 * Add the correct increment for the reader count.
1897069154d5SJulian Elischer 		 */
18984be59335SGleb Smirnoff 		add_arg = READER_INCREMENT;
1899714fb865SGleb Smirnoff 		*rw = NGQRW_R;
19004be59335SGleb Smirnoff 	} else if (QUEUED_WRITER_CAN_PROCEED(ngq)) {
1901069154d5SJulian Elischer 		/*
1902069154d5SJulian Elischer 		 * There is a pending write, no readers and no active writer.
1903069154d5SJulian Elischer 		 * This means we can go ahead with the pending writer. Note
1904069154d5SJulian Elischer 		 * the fact that we now have a writer, ready for when we take
1905069154d5SJulian Elischer 		 * it off the queue.
1906069154d5SJulian Elischer 		 *
1907069154d5SJulian Elischer 		 * We don't need to worry about a possible collision with the
1908069154d5SJulian Elischer 		 * fasttrack reader.
1909069154d5SJulian Elischer 		 *
1910069154d5SJulian Elischer 		 * The fasttrack thread may take a long time to discover that we
1911069154d5SJulian Elischer 		 * are running so we would have an inconsistent state in the
1912069154d5SJulian Elischer 		 * flags for a while. Since we ignore the reader count
1913069154d5SJulian Elischer 		 * entirely when the WRITER_ACTIVE flag is set, this should
1914069154d5SJulian Elischer 		 * not matter (in fact it is defined that way). If it tests
19154be59335SGleb Smirnoff 		 * the flag before this operation, the OP_PENDING flag
1916069154d5SJulian Elischer 		 * will make it fail, and if it tests it later, the
1917b57a7965SJulian Elischer 		 * WRITER_ACTIVE flag will do the same. If it is SO slow that
1918069154d5SJulian Elischer 		 * we have actually completed the operation, and neither flag
19194be59335SGleb Smirnoff 		 * is set by the time that it tests the flags, then it is
19204be59335SGleb Smirnoff 		 * actually ok for it to continue. If it completes and we've
19214be59335SGleb Smirnoff 		 * finished and the read pending is set it still fails.
1922069154d5SJulian Elischer 		 *
1923069154d5SJulian Elischer 		 * So we can just ignore it,  as long as we can ensure that the
1924069154d5SJulian Elischer 		 * transition from WRITE_PENDING state to the WRITER_ACTIVE
1925069154d5SJulian Elischer 		 * state is atomic.
1926069154d5SJulian Elischer 		 *
1927069154d5SJulian Elischer 		 * After failing, first it will be held back by the mutex, then
1928069154d5SJulian Elischer 		 * when it can proceed, it will queue its request, then it
1929069154d5SJulian Elischer 		 * would arrive at this function. Usually it will have to
1930b57a7965SJulian Elischer 		 * leave empty handed because the ACTIVE WRITER bit will be
1931069154d5SJulian Elischer 		 * set.
1932b57a7965SJulian Elischer 		 *
19334be59335SGleb Smirnoff 		 * Adjust the flags for the new active writer.
1934069154d5SJulian Elischer 		 */
19354be59335SGleb Smirnoff 		add_arg = WRITER_ACTIVE;
1936714fb865SGleb Smirnoff 		*rw = NGQRW_W;
1937069154d5SJulian Elischer 		/*
1938069154d5SJulian Elischer 		 * We want to write "active writer, no readers " Now go make
1939069154d5SJulian Elischer 		 * it true. In fact there may be a number in the readers
1940069154d5SJulian Elischer 		 * count but we know it is not true and will be fixed soon.
1941069154d5SJulian Elischer 		 * We will fix the flags for the next pending entry in a
1942069154d5SJulian Elischer 		 * moment.
1943069154d5SJulian Elischer 		 */
1944069154d5SJulian Elischer 	} else {
1945069154d5SJulian Elischer 		/*
1946069154d5SJulian Elischer 		 * We can't dequeue anything.. return and say so. Probably we
1947069154d5SJulian Elischer 		 * have a write pending and the readers count is non zero. If
1948069154d5SJulian Elischer 		 * we got here because a reader hit us just at the wrong
1949069154d5SJulian Elischer 		 * moment with the fasttrack code, and put us in a strange
19504be59335SGleb Smirnoff 		 * state, then it will be coming through in just a moment,
19514be59335SGleb Smirnoff 		 * (just as soon as we release the mutex) and keep things
19524be59335SGleb Smirnoff 		 * moving.
19534be59335SGleb Smirnoff 		 * Make sure we remove ourselves from the work queue. It
19544be59335SGleb Smirnoff 		 * would be a waste of effort to do all this again.
1955069154d5SJulian Elischer 		 */
1956b57a7965SJulian Elischer 		ng_worklist_remove(ngq->q_node);
19572955ee18SGleb Smirnoff 		CTR4(KTR_NET, "%20s: node [%x] (%p) can't dequeue anything; "
19582955ee18SGleb Smirnoff 		    "queue flags 0x%lx", __func__,
19592955ee18SGleb Smirnoff 		    ngq->q_node->nd_ID, ngq->q_node, ngq->q_flags);
19604be59335SGleb Smirnoff 		return (NULL);
19614cf49a43SJulian Elischer 	}
19624cf49a43SJulian Elischer 
19634cf49a43SJulian Elischer 	/*
1964069154d5SJulian Elischer 	 * Now we dequeue the request (whatever it may be) and correct the
1965069154d5SJulian Elischer 	 * pending flags and the next and last pointers.
19664cf49a43SJulian Elischer 	 */
1967069154d5SJulian Elischer 	item = ngq->queue;
1968069154d5SJulian Elischer 	ngq->queue = item->el_next;
19692955ee18SGleb Smirnoff 	CTR6(KTR_NET, "%20s: node [%x] (%p) dequeued item %p with flags 0x%lx; "
19702955ee18SGleb Smirnoff 	    "queue flags 0x%lx", __func__,
19712955ee18SGleb Smirnoff 	    ngq->q_node->nd_ID,ngq->q_node, item, item->el_flags, ngq->q_flags);
1972069154d5SJulian Elischer 	if (ngq->last == &(item->el_next)) {
19734cf49a43SJulian Elischer 		/*
1974069154d5SJulian Elischer 		 * that was the last entry in the queue so set the 'last
19754be59335SGleb Smirnoff 		 * pointer up correctly and make sure the pending flag is
1976069154d5SJulian Elischer 		 * clear.
19774cf49a43SJulian Elischer 		 */
19784be59335SGleb Smirnoff 		add_arg += -OP_PENDING;
1979069154d5SJulian Elischer 		ngq->last = &(ngq->queue);
1980859a4d16SJulian Elischer 		/*
1981b57a7965SJulian Elischer 		 * Whatever flag was set will be cleared and
1982b57a7965SJulian Elischer 		 * the new acive field will be set by the add as well,
1983b57a7965SJulian Elischer 		 * so we don't need to change add_arg.
1984b57a7965SJulian Elischer 		 * But we know we don't need to be on the work list.
1985859a4d16SJulian Elischer 		 */
1986b57a7965SJulian Elischer 		atomic_add_long(&ngq->q_flags, add_arg);
1987b57a7965SJulian Elischer 		ng_worklist_remove(ngq->q_node);
1988859a4d16SJulian Elischer 	} else {
1989069154d5SJulian Elischer 		/*
19904be59335SGleb Smirnoff 		 * Since there is still something on the queue
19914be59335SGleb Smirnoff 		 * we don't need to change the PENDING flag.
1992069154d5SJulian Elischer 		 */
1993069154d5SJulian Elischer 		atomic_add_long(&ngq->q_flags, add_arg);
1994b57a7965SJulian Elischer 	}
19952955ee18SGleb Smirnoff 	CTR6(KTR_NET, "%20s: node [%x] (%p) returning item %p as %s; "
19962955ee18SGleb Smirnoff 	    "queue flags 0x%lx", __func__,
19972955ee18SGleb Smirnoff 	    ngq->q_node->nd_ID, ngq->q_node, item, *rw ? "WRITER" : "READER" ,
19983b33fbe7SGleb Smirnoff 	    ngq->q_flags);
1999069154d5SJulian Elischer 	return (item);
2000069154d5SJulian Elischer }
2001859a4d16SJulian Elischer 
2002859a4d16SJulian Elischer /*
2003069154d5SJulian Elischer  * Queue a packet to be picked up by someone else.
2004069154d5SJulian Elischer  * We really don't care who, but we can't or don't want to hang around
2005069154d5SJulian Elischer  * to process it ourselves. We are probably an interrupt routine..
20064be59335SGleb Smirnoff  * If the queue could be run, flag the netisr handler to start.
2007859a4d16SJulian Elischer  */
2008069154d5SJulian Elischer static __inline void
2009069154d5SJulian Elischer ng_queue_rw(struct ng_queue * ngq, item_p  item, int rw)
2010069154d5SJulian Elischer {
2011d58e678fSGleb Smirnoff 	mtx_assert(&ngq->q_mtx, MA_OWNED);
2012d58e678fSGleb Smirnoff 
20134be59335SGleb Smirnoff 	if (rw == NGQRW_W)
20144be59335SGleb Smirnoff 		NGI_SET_WRITER(item);
20154be59335SGleb Smirnoff 	else
20164be59335SGleb Smirnoff 		NGI_SET_READER(item);
2017069154d5SJulian Elischer 	item->el_next = NULL;	/* maybe not needed */
2018069154d5SJulian Elischer 	*ngq->last = item;
20192955ee18SGleb Smirnoff 	CTR5(KTR_NET, "%20s: node [%x] (%p) queued item %p as %s", __func__,
20202955ee18SGleb Smirnoff 	    ngq->q_node->nd_ID, ngq->q_node, item, rw ? "WRITER" : "READER" );
2021069154d5SJulian Elischer 	/*
2022069154d5SJulian Elischer 	 * If it was the first item in the queue then we need to
2023069154d5SJulian Elischer 	 * set the last pointer and the type flags.
2024069154d5SJulian Elischer 	 */
20253b33fbe7SGleb Smirnoff 	if (ngq->last == &(ngq->queue)) {
20264be59335SGleb Smirnoff 		atomic_add_long(&ngq->q_flags, OP_PENDING);
20272955ee18SGleb Smirnoff 		CTR3(KTR_NET, "%20s: node [%x] (%p) set OP_PENDING", __func__,
20282955ee18SGleb Smirnoff 		    ngq->q_node->nd_ID, ngq->q_node);
20293b33fbe7SGleb Smirnoff 	}
20304be59335SGleb Smirnoff 
2031069154d5SJulian Elischer 	ngq->last = &(item->el_next);
20324be59335SGleb Smirnoff 	/*
20334be59335SGleb Smirnoff 	 * We can take the worklist lock with the node locked
20344be59335SGleb Smirnoff 	 * BUT NOT THE REVERSE!
20354be59335SGleb Smirnoff 	 */
20364be59335SGleb Smirnoff 	if (NEXT_QUEUED_ITEM_CAN_PROCEED(ngq))
20374be59335SGleb Smirnoff 		ng_setisr(ngq->q_node);
2038069154d5SJulian Elischer }
2039069154d5SJulian Elischer 
2040069154d5SJulian Elischer 
2041069154d5SJulian Elischer /*
2042069154d5SJulian Elischer  * This function 'cheats' in that it first tries to 'grab' the use of the
2043069154d5SJulian Elischer  * node, without going through the mutex. We can do this becasue of the
2044069154d5SJulian Elischer  * semantics of the lock. The semantics include a clause that says that the
2045069154d5SJulian Elischer  * value of the readers count is invalid if the WRITER_ACTIVE flag is set. It
2046069154d5SJulian Elischer  * also says that the WRITER_ACTIVE flag cannot be set if the readers count
2047069154d5SJulian Elischer  * is not zero. Note that this talks about what is valid to SET the
2048069154d5SJulian Elischer  * WRITER_ACTIVE flag, because from the moment it is set, the value if the
2049069154d5SJulian Elischer  * reader count is immaterial, and not valid. The two 'pending' flags have a
2050069154d5SJulian Elischer  * similar effect, in that If they are orthogonal to the two active fields in
2051069154d5SJulian Elischer  * how they are set, but if either is set, the attempted 'grab' need to be
2052069154d5SJulian Elischer  * backed out because there is earlier work, and we maintain ordering in the
2053069154d5SJulian Elischer  * queue. The result of this is that the reader request can try obtain use of
2054069154d5SJulian Elischer  * the node with only a single atomic addition, and without any of the mutex
2055069154d5SJulian Elischer  * overhead. If this fails the operation degenerates to the same as for other
2056069154d5SJulian Elischer  * cases.
2057069154d5SJulian Elischer  *
2058069154d5SJulian Elischer  */
2059069154d5SJulian Elischer static __inline item_p
2060069154d5SJulian Elischer ng_acquire_read(struct ng_queue *ngq, item_p item)
2061069154d5SJulian Elischer {
2062ac5dd141SGleb Smirnoff 	KASSERT(ngq != &ng_deadnode.nd_input_queue,
2063ac5dd141SGleb Smirnoff 	    ("%s: working on deadnode", __func__));
2064069154d5SJulian Elischer 
2065069154d5SJulian Elischer 	/* ######### Hack alert ######### */
2066069154d5SJulian Elischer 	atomic_add_long(&ngq->q_flags, READER_INCREMENT);
2067b57a7965SJulian Elischer 	if ((ngq->q_flags & NGQ_RMASK) == 0) {
2068069154d5SJulian Elischer 		/* Successfully grabbed node */
20692955ee18SGleb Smirnoff 		CTR4(KTR_NET, "%20s: node [%x] (%p) fast acquired item %p",
20702955ee18SGleb Smirnoff 		    __func__, ngq->q_node->nd_ID, ngq->q_node, item);
2071069154d5SJulian Elischer 		return (item);
2072069154d5SJulian Elischer 	}
2073069154d5SJulian Elischer 	/* undo the damage if we didn't succeed */
2074069154d5SJulian Elischer 	atomic_subtract_long(&ngq->q_flags, READER_INCREMENT);
2075069154d5SJulian Elischer 
2076069154d5SJulian Elischer 	/* ######### End Hack alert ######### */
20772c8dda8dSWojciech A. Koszek 	NG_QUEUE_LOCK(ngq);
2078069154d5SJulian Elischer 	/*
2079069154d5SJulian Elischer 	 * Try again. Another processor (or interrupt for that matter) may
2080069154d5SJulian Elischer 	 * have removed the last queued item that was stopping us from
2081069154d5SJulian Elischer 	 * running, between the previous test, and the moment that we took
2082069154d5SJulian Elischer 	 * the mutex. (Or maybe a writer completed.)
20834be59335SGleb Smirnoff 	 * Even if another fast-track reader hits during this period
20844be59335SGleb Smirnoff 	 * we don't care as multiple readers is OK.
2085069154d5SJulian Elischer 	 */
2086b57a7965SJulian Elischer 	if ((ngq->q_flags & NGQ_RMASK) == 0) {
2087069154d5SJulian Elischer 		atomic_add_long(&ngq->q_flags, READER_INCREMENT);
20882c8dda8dSWojciech A. Koszek 		NG_QUEUE_UNLOCK(ngq);
20892955ee18SGleb Smirnoff 		CTR4(KTR_NET, "%20s: node [%x] (%p) slow acquired item %p",
20902955ee18SGleb Smirnoff 		    __func__, ngq->q_node->nd_ID, ngq->q_node, item);
2091069154d5SJulian Elischer 		return (item);
2092069154d5SJulian Elischer 	}
2093069154d5SJulian Elischer 
2094069154d5SJulian Elischer 	/*
2095069154d5SJulian Elischer 	 * and queue the request for later.
2096069154d5SJulian Elischer 	 */
2097069154d5SJulian Elischer 	ng_queue_rw(ngq, item, NGQRW_R);
20982c8dda8dSWojciech A. Koszek 	NG_QUEUE_UNLOCK(ngq);
2099f912c0f7SGleb Smirnoff 
2100f912c0f7SGleb Smirnoff 	return (NULL);
2101069154d5SJulian Elischer }
2102069154d5SJulian Elischer 
2103069154d5SJulian Elischer static __inline item_p
2104069154d5SJulian Elischer ng_acquire_write(struct ng_queue *ngq, item_p item)
2105069154d5SJulian Elischer {
2106ac5dd141SGleb Smirnoff 	KASSERT(ngq != &ng_deadnode.nd_input_queue,
2107ac5dd141SGleb Smirnoff 	    ("%s: working on deadnode", __func__));
2108ac5dd141SGleb Smirnoff 
2109069154d5SJulian Elischer restart:
21102c8dda8dSWojciech A. Koszek 	NG_QUEUE_LOCK(ngq);
2111069154d5SJulian Elischer 	/*
2112069154d5SJulian Elischer 	 * If there are no readers, no writer, and no pending packets, then
2113069154d5SJulian Elischer 	 * we can just go ahead. In all other situations we need to queue the
2114069154d5SJulian Elischer 	 * request
2115069154d5SJulian Elischer 	 */
2116b57a7965SJulian Elischer 	if ((ngq->q_flags & NGQ_WMASK) == 0) {
21174be59335SGleb Smirnoff 		/* collision could happen *HERE* */
2118069154d5SJulian Elischer 		atomic_add_long(&ngq->q_flags, WRITER_ACTIVE);
21192c8dda8dSWojciech A. Koszek 		NG_QUEUE_UNLOCK(ngq);
2120069154d5SJulian Elischer 		if (ngq->q_flags & READER_MASK) {
2121069154d5SJulian Elischer 			/* Collision with fast-track reader */
2122b57a7965SJulian Elischer 			atomic_subtract_long(&ngq->q_flags, WRITER_ACTIVE);
2123069154d5SJulian Elischer 			goto restart;
2124069154d5SJulian Elischer 		}
21252955ee18SGleb Smirnoff 		CTR4(KTR_NET, "%20s: node [%x] (%p) acquired item %p",
21262955ee18SGleb Smirnoff 		    __func__, ngq->q_node->nd_ID, ngq->q_node, item);
2127069154d5SJulian Elischer 		return (item);
2128069154d5SJulian Elischer 	}
2129069154d5SJulian Elischer 
2130069154d5SJulian Elischer 	/*
2131069154d5SJulian Elischer 	 * and queue the request for later.
2132069154d5SJulian Elischer 	 */
2133069154d5SJulian Elischer 	ng_queue_rw(ngq, item, NGQRW_W);
21342c8dda8dSWojciech A. Koszek 	NG_QUEUE_UNLOCK(ngq);
2135f912c0f7SGleb Smirnoff 
2136f912c0f7SGleb Smirnoff 	return (NULL);
2137069154d5SJulian Elischer }
2138069154d5SJulian Elischer 
2139262dfd7fSJulian Elischer #if 0
2140262dfd7fSJulian Elischer static __inline item_p
2141262dfd7fSJulian Elischer ng_upgrade_write(struct ng_queue *ngq, item_p item)
2142262dfd7fSJulian Elischer {
2143262dfd7fSJulian Elischer 	KASSERT(ngq != &ng_deadnode.nd_input_queue,
2144262dfd7fSJulian Elischer 	    ("%s: working on deadnode", __func__));
2145262dfd7fSJulian Elischer 
2146262dfd7fSJulian Elischer 	NGI_SET_WRITER(item);
2147262dfd7fSJulian Elischer 
2148262dfd7fSJulian Elischer 	mtx_lock_spin(&(ngq->q_mtx));
2149262dfd7fSJulian Elischer 
2150262dfd7fSJulian Elischer 	/*
2151262dfd7fSJulian Elischer 	 * There will never be no readers as we are there ourselves.
2152262dfd7fSJulian Elischer 	 * Set the WRITER_ACTIVE flags ASAP to block out fast track readers.
2153262dfd7fSJulian Elischer 	 * The caller we are running from will call ng_leave_read()
2154262dfd7fSJulian Elischer 	 * soon, so we must account for that. We must leave again with the
2155262dfd7fSJulian Elischer 	 * READER lock. If we find other readers, then
2156262dfd7fSJulian Elischer 	 * queue the request for later. However "later" may be rignt now
2157262dfd7fSJulian Elischer 	 * if there are no readers. We don't really care if there are queued
2158262dfd7fSJulian Elischer 	 * items as we will bypass them anyhow.
2159262dfd7fSJulian Elischer 	 */
2160262dfd7fSJulian Elischer 	atomic_add_long(&ngq->q_flags, WRITER_ACTIVE - READER_INCREMENT);
2161262dfd7fSJulian Elischer 	if (ngq->q_flags & (NGQ_WMASK & ~OP_PENDING) == WRITER_ACTIVE) {
2162262dfd7fSJulian Elischer 		mtx_unlock_spin(&(ngq->q_mtx));
2163262dfd7fSJulian Elischer 
2164262dfd7fSJulian Elischer 		/* It's just us, act on the item. */
2165262dfd7fSJulian Elischer 		/* will NOT drop writer lock when done */
2166262dfd7fSJulian Elischer 		ng_apply_item(node, item, 0);
2167262dfd7fSJulian Elischer 
2168262dfd7fSJulian Elischer 		/*
2169262dfd7fSJulian Elischer 		 * Having acted on the item, atomically
2170262dfd7fSJulian Elischer 		 * down grade back to READER and finish up
2171262dfd7fSJulian Elischer 	 	 */
2172262dfd7fSJulian Elischer 		atomic_add_long(&ngq->q_flags,
2173262dfd7fSJulian Elischer 		    READER_INCREMENT - WRITER_ACTIVE);
2174262dfd7fSJulian Elischer 
2175262dfd7fSJulian Elischer 		/* Our caller will call ng_leave_read() */
2176262dfd7fSJulian Elischer 		return;
2177262dfd7fSJulian Elischer 	}
2178262dfd7fSJulian Elischer 	/*
2179262dfd7fSJulian Elischer 	 * It's not just us active, so queue us AT THE HEAD.
2180262dfd7fSJulian Elischer 	 * "Why?" I hear you ask.
2181262dfd7fSJulian Elischer 	 * Put us at the head of the queue as we've already been
2182262dfd7fSJulian Elischer 	 * through it once. If there is nothing else waiting,
2183262dfd7fSJulian Elischer 	 * set the correct flags.
2184262dfd7fSJulian Elischer 	 */
2185262dfd7fSJulian Elischer 	if ((item->el_next = ngq->queue) == NULL) {
2186262dfd7fSJulian Elischer 		/*
2187262dfd7fSJulian Elischer 		 * Set up the "last" pointer.
2188262dfd7fSJulian Elischer 		 * We are the only (and thus last) item
2189262dfd7fSJulian Elischer 		 */
2190262dfd7fSJulian Elischer 		ngq->last = &(item->el_next);
2191262dfd7fSJulian Elischer 
2192262dfd7fSJulian Elischer 		/* We've gone from, 0 to 1 item in the queue */
2193262dfd7fSJulian Elischer 		atomic_add_long(&ngq->q_flags, OP_PENDING);
2194262dfd7fSJulian Elischer 
2195262dfd7fSJulian Elischer 		CTR3(KTR_NET, "%20s: node [%x] (%p) set OP_PENDING", __func__,
2196262dfd7fSJulian Elischer 		    ngq->q_node->nd_ID, ngq->q_node);
2197262dfd7fSJulian Elischer 	};
2198262dfd7fSJulian Elischer 	ngq->queue = item;
2199262dfd7fSJulian Elischer 	CTR5(KTR_NET, "%20s: node [%x] (%p) requeued item %p as WRITER",
2200262dfd7fSJulian Elischer 	    __func__, ngq->q_node->nd_ID, ngq->q_node, item );
2201262dfd7fSJulian Elischer 
2202262dfd7fSJulian Elischer 	/* Reverse what we did above. That downgrades us back to reader */
2203262dfd7fSJulian Elischer 	atomic_add_long(&ngq->q_flags, READER_INCREMENT - WRITER_ACTIVE);
2204262dfd7fSJulian Elischer 	if (NEXT_QUEUED_ITEM_CAN_PROCEED(ngq))
2205262dfd7fSJulian Elischer 		ng_setisr(ngq->q_node);
2206262dfd7fSJulian Elischer 	mtx_unlock_spin(&(ngq->q_mtx));
2207262dfd7fSJulian Elischer 
2208262dfd7fSJulian Elischer 	return;
2209262dfd7fSJulian Elischer }
2210262dfd7fSJulian Elischer 
2211262dfd7fSJulian Elischer #endif
2212262dfd7fSJulian Elischer 
2213069154d5SJulian Elischer static __inline void
2214069154d5SJulian Elischer ng_leave_read(struct ng_queue *ngq)
2215069154d5SJulian Elischer {
2216069154d5SJulian Elischer 	atomic_subtract_long(&ngq->q_flags, READER_INCREMENT);
2217069154d5SJulian Elischer }
2218069154d5SJulian Elischer 
2219069154d5SJulian Elischer static __inline void
2220069154d5SJulian Elischer ng_leave_write(struct ng_queue *ngq)
2221069154d5SJulian Elischer {
2222069154d5SJulian Elischer 	atomic_subtract_long(&ngq->q_flags, WRITER_ACTIVE);
2223069154d5SJulian Elischer }
2224069154d5SJulian Elischer 
2225069154d5SJulian Elischer static void
2226069154d5SJulian Elischer ng_flush_input_queue(struct ng_queue * ngq)
2227069154d5SJulian Elischer {
2228069154d5SJulian Elischer 	item_p item;
2229069154d5SJulian Elischer 
22302c8dda8dSWojciech A. Koszek 	NG_QUEUE_LOCK(ngq);
22314be59335SGleb Smirnoff 	while (ngq->queue) {
2232069154d5SJulian Elischer 		item = ngq->queue;
2233069154d5SJulian Elischer 		ngq->queue = item->el_next;
2234069154d5SJulian Elischer 		if (ngq->last == &(item->el_next)) {
2235069154d5SJulian Elischer 			ngq->last = &(ngq->queue);
22364be59335SGleb Smirnoff 			atomic_add_long(&ngq->q_flags, -OP_PENDING);
2237069154d5SJulian Elischer 		}
22382c8dda8dSWojciech A. Koszek 		NG_QUEUE_UNLOCK(ngq);
22394be59335SGleb Smirnoff 
22404be59335SGleb Smirnoff 		/* If the item is supplying a callback, call it with an error */
224110e87318SAlexander Motin 		if (item->apply != NULL) {
224210e87318SAlexander Motin 			if (item->depth == 1)
224310e87318SAlexander Motin 				item->apply->error = ENOENT;
224410e87318SAlexander Motin 			if (refcount_release(&item->apply->refs)) {
224510e87318SAlexander Motin 				(*item->apply->apply)(item->apply->context,
224610e87318SAlexander Motin 				    item->apply->error);
224710e87318SAlexander Motin 			}
2248eb2405ddSGleb Smirnoff 		}
2249505fad52SJulian Elischer 		NG_FREE_ITEM(item);
22502c8dda8dSWojciech A. Koszek 		NG_QUEUE_LOCK(ngq);
2251069154d5SJulian Elischer 	}
2252b57a7965SJulian Elischer 	/*
2253b57a7965SJulian Elischer 	 * Take us off the work queue if we are there.
225464efc707SRobert Watson 	 * We definately have no work to be done.
2255b57a7965SJulian Elischer 	 */
2256b57a7965SJulian Elischer 	ng_worklist_remove(ngq->q_node);
22572c8dda8dSWojciech A. Koszek 	NG_QUEUE_UNLOCK(ngq);
2258069154d5SJulian Elischer }
2259069154d5SJulian Elischer 
2260069154d5SJulian Elischer /***********************************************************************
2261069154d5SJulian Elischer * Externally visible method for sending or queueing messages or data.
2262069154d5SJulian Elischer ***********************************************************************/
2263069154d5SJulian Elischer 
2264069154d5SJulian Elischer /*
22651acb27c6SJulian Elischer  * The module code should have filled out the item correctly by this stage:
2266069154d5SJulian Elischer  * Common:
2267069154d5SJulian Elischer  *    reference to destination node.
2268069154d5SJulian Elischer  *    Reference to destination rcv hook if relevant.
2269e088dd4cSAlexander Motin  *    apply pointer must be or NULL or reference valid struct ng_apply_info.
2270069154d5SJulian Elischer  * Data:
2271069154d5SJulian Elischer  *    pointer to mbuf
2272069154d5SJulian Elischer  * Control_Message:
2273069154d5SJulian Elischer  *    pointer to msg.
2274069154d5SJulian Elischer  *    ID of original sender node. (return address)
22751acb27c6SJulian Elischer  * Function:
22761acb27c6SJulian Elischer  *    Function pointer
22771acb27c6SJulian Elischer  *    void * argument
22781acb27c6SJulian Elischer  *    integer argument
2279069154d5SJulian Elischer  *
2280069154d5SJulian Elischer  * The nodes have several routines and macros to help with this task:
2281069154d5SJulian Elischer  */
2282069154d5SJulian Elischer 
2283069154d5SJulian Elischer int
228442282202SGleb Smirnoff ng_snd_item(item_p item, int flags)
2285069154d5SJulian Elischer {
2286e088dd4cSAlexander Motin 	hook_p hook;
2287e088dd4cSAlexander Motin 	node_p node;
228842282202SGleb Smirnoff 	int queue, rw;
2289e088dd4cSAlexander Motin 	struct ng_queue *ngq;
229032b33288SGleb Smirnoff 	int error = 0;
2291069154d5SJulian Elischer 
2292f50597f5SAlexander Motin 	/* We are sending item, so it must be present! */
2293f50597f5SAlexander Motin 	KASSERT(item != NULL, ("ng_snd_item: item is NULL"));
2294e088dd4cSAlexander Motin 
2295e088dd4cSAlexander Motin #ifdef	NETGRAPH_DEBUG
2296e088dd4cSAlexander Motin 	_ngi_check(item, __FILE__, __LINE__);
2297e088dd4cSAlexander Motin #endif
2298e088dd4cSAlexander Motin 
2299f50597f5SAlexander Motin 	/* Item was sent once more, postpone apply() call. */
2300e088dd4cSAlexander Motin 	if (item->apply)
2301e088dd4cSAlexander Motin 		refcount_acquire(&item->apply->refs);
2302e088dd4cSAlexander Motin 
2303e088dd4cSAlexander Motin 	node = NGI_NODE(item);
2304f50597f5SAlexander Motin 	/* Node is never optional. */
2305f50597f5SAlexander Motin 	KASSERT(node != NULL, ("ng_snd_item: node is NULL"));
2306e088dd4cSAlexander Motin 
2307e72a98f4SAlexander Motin 	hook = NGI_HOOK(item);
2308f50597f5SAlexander Motin 	/* Valid hook and mbuf are mandatory for data. */
2309f50597f5SAlexander Motin 	if ((item->el_flags & NGQF_TYPE) == NGQF_DATA) {
2310f50597f5SAlexander Motin 		KASSERT(hook != NULL, ("ng_snd_item: hook for data is NULL"));
231110204449SJulian Elischer 		if (NGI_M(item) == NULL)
2312e088dd4cSAlexander Motin 			ERROUT(EINVAL);
2313069154d5SJulian Elischer 		CHECK_DATA_MBUF(NGI_M(item));
23146f683eeeSGleb Smirnoff 	}
23156f683eeeSGleb Smirnoff 
2316069154d5SJulian Elischer 	/*
2317f50597f5SAlexander Motin 	 * If the item or the node specifies single threading, force
2318f50597f5SAlexander Motin 	 * writer semantics. Similarly, the node may say one hook always
2319f50597f5SAlexander Motin 	 * produces writers. These are overrides.
2320069154d5SJulian Elischer 	 */
2321db3408aeSAlexander Motin 	if (((item->el_flags & NGQF_RW) == NGQF_WRITER) ||
2322f50597f5SAlexander Motin 	    (node->nd_flags & NGF_FORCE_WRITER) ||
2323f50597f5SAlexander Motin 	    (hook && (hook->hk_flags & HK_FORCE_WRITER))) {
2324069154d5SJulian Elischer 		rw = NGQRW_W;
2325f50597f5SAlexander Motin 	} else {
2326f50597f5SAlexander Motin 		rw = NGQRW_R;
2327f50597f5SAlexander Motin 	}
23286f683eeeSGleb Smirnoff 
232981a253a4SAlexander Motin 	/*
233081a253a4SAlexander Motin 	 * If sender or receiver requests queued delivery or stack usage
233181a253a4SAlexander Motin 	 * level is dangerous - enqueue message.
233281a253a4SAlexander Motin 	 */
233381a253a4SAlexander Motin 	if ((flags & NG_QUEUE) || (hook && (hook->hk_flags & HK_QUEUE))) {
233481a253a4SAlexander Motin 		queue = 1;
2335f50597f5SAlexander Motin 	} else {
2336f50597f5SAlexander Motin 		queue = 0;
233781a253a4SAlexander Motin #ifdef GET_STACK_USAGE
2338d4529f98SAlexander Motin 		/*
2339942fe01fSDmitry Morozovsky 		 * Most of netgraph nodes have small stack consumption and
2340f50597f5SAlexander Motin 		 * for them 25% of free stack space is more than enough.
2341d4529f98SAlexander Motin 		 * Nodes/hooks with higher stack usage should be marked as
2342f9773372SDmitry Morozovsky 		 * HI_STACK. For them 50% of stack will be guaranteed then.
2343f50597f5SAlexander Motin 		 * XXX: Values 25% and 50% are completely empirical.
2344d4529f98SAlexander Motin 		 */
2345f50597f5SAlexander Motin 		size_t	st, su, sl;
234681a253a4SAlexander Motin 		GET_STACK_USAGE(st, su);
2347f50597f5SAlexander Motin 		sl = st - su;
2348f50597f5SAlexander Motin 		if ((sl * 4 < st) ||
2349f50597f5SAlexander Motin 		    ((sl * 2 < st) && ((node->nd_flags & NGF_HI_STACK) ||
235081a253a4SAlexander Motin 		      (hook && (hook->hk_flags & HK_HI_STACK))))) {
235181a253a4SAlexander Motin 			queue = 1;
235281a253a4SAlexander Motin 		}
235381a253a4SAlexander Motin #endif
2354f50597f5SAlexander Motin 	}
235581a253a4SAlexander Motin 
2356e72a98f4SAlexander Motin 	ngq = &node->nd_input_queue;
2357069154d5SJulian Elischer 	if (queue) {
2358069154d5SJulian Elischer 		/* Put it on the queue for that node*/
235930400f03SJulian Elischer #ifdef	NETGRAPH_DEBUG
2360069154d5SJulian Elischer 		_ngi_check(item, __FILE__, __LINE__);
2361069154d5SJulian Elischer #endif
236210e87318SAlexander Motin 		item->depth = 1;
23632c8dda8dSWojciech A. Koszek 		NG_QUEUE_LOCK(ngq);
2364069154d5SJulian Elischer 		ng_queue_rw(ngq, item, rw);
23652c8dda8dSWojciech A. Koszek 		NG_QUEUE_UNLOCK(ngq);
23668afe16d5SGleb Smirnoff 
2367f50597f5SAlexander Motin 		return ((flags & NG_PROGRESS) ? EINPROGRESS : 0);
2368069154d5SJulian Elischer 	}
2369069154d5SJulian Elischer 
2370069154d5SJulian Elischer 	/*
2371069154d5SJulian Elischer 	 * We already decided how we will be queueud or treated.
2372069154d5SJulian Elischer 	 * Try get the appropriate operating permission.
2373069154d5SJulian Elischer 	 */
237432b33288SGleb Smirnoff  	if (rw == NGQRW_R)
2375069154d5SJulian Elischer 		item = ng_acquire_read(ngq, item);
237632b33288SGleb Smirnoff 	else
2377069154d5SJulian Elischer 		item = ng_acquire_write(ngq, item);
23784cf49a43SJulian Elischer 
23794be59335SGleb Smirnoff 
2380f50597f5SAlexander Motin 	/* Item was queued while trying to get permission. */
2381f50597f5SAlexander Motin 	if (item == NULL)
2382f50597f5SAlexander Motin 		return ((flags & NG_PROGRESS) ? EINPROGRESS : 0);
2383069154d5SJulian Elischer 
238430400f03SJulian Elischer #ifdef	NETGRAPH_DEBUG
2385069154d5SJulian Elischer 	_ngi_check(item, __FILE__, __LINE__);
2386069154d5SJulian Elischer #endif
23874be59335SGleb Smirnoff 
23885951069aSJulian Elischer 	NGI_GET_NODE(item, node); /* zaps stored node */
23895951069aSJulian Elischer 
239010e87318SAlexander Motin 	item->depth++;
239127757487SGleb Smirnoff 	error = ng_apply_item(node, item, rw); /* drops r/w lock when done */
2392069154d5SJulian Elischer 
2393069154d5SJulian Elischer 	/*
23945951069aSJulian Elischer 	 * If the node goes away when we remove the reference,
2395376958b4SBrian Somers 	 * whatever we just did caused it.. whatever we do, DO NOT
23965951069aSJulian Elischer 	 * access the node again!
23975951069aSJulian Elischer 	 */
2398f50597f5SAlexander Motin 	if (NG_NODE_UNREF(node) == 0)
23995951069aSJulian Elischer 		return (error);
24005951069aSJulian Elischer 
24012c8dda8dSWojciech A. Koszek 	NG_QUEUE_LOCK(ngq);
24024be59335SGleb Smirnoff 	if (NEXT_QUEUED_ITEM_CAN_PROCEED(ngq))
2403f912c0f7SGleb Smirnoff 		ng_setisr(ngq->q_node);
24042c8dda8dSWojciech A. Koszek 	NG_QUEUE_UNLOCK(ngq);
2405069154d5SJulian Elischer 
24061acb27c6SJulian Elischer 	return (error);
2407e088dd4cSAlexander Motin 
2408e088dd4cSAlexander Motin done:
2409f50597f5SAlexander Motin 	/* If was not sent, apply callback here. */
241010e87318SAlexander Motin 	if (item->apply != NULL) {
241110e87318SAlexander Motin 		if (item->depth == 0 && error != 0)
241210e87318SAlexander Motin 			item->apply->error = error;
241310e87318SAlexander Motin 		if (refcount_release(&item->apply->refs)) {
241410e87318SAlexander Motin 			(*item->apply->apply)(item->apply->context,
241510e87318SAlexander Motin 			    item->apply->error);
241610e87318SAlexander Motin 		}
241710e87318SAlexander Motin 	}
2418e72a98f4SAlexander Motin 
2419e088dd4cSAlexander Motin 	NG_FREE_ITEM(item);
2420e088dd4cSAlexander Motin 	return (error);
2421069154d5SJulian Elischer }
2422069154d5SJulian Elischer 
2423069154d5SJulian Elischer /*
2424069154d5SJulian Elischer  * We have an item that was possibly queued somewhere.
2425069154d5SJulian Elischer  * It should contain all the information needed
2426069154d5SJulian Elischer  * to run it on the appropriate node/hook.
2427e088dd4cSAlexander Motin  * If there is apply pointer and we own the last reference, call apply().
24284cf49a43SJulian Elischer  */
242927757487SGleb Smirnoff static int
2430714fb865SGleb Smirnoff ng_apply_item(node_p node, item_p item, int rw)
2431069154d5SJulian Elischer {
2432069154d5SJulian Elischer 	hook_p  hook;
2433069154d5SJulian Elischer 	ng_rcvdata_t *rcvdata;
2434c4b5eea4SJulian Elischer 	ng_rcvmsg_t *rcvmsg;
2435e088dd4cSAlexander Motin 	struct ng_apply_info *apply;
243610e87318SAlexander Motin 	int	error = 0, depth;
2437069154d5SJulian Elischer 
2438f50597f5SAlexander Motin 	/* Node and item are never optional. */
2439f50597f5SAlexander Motin 	KASSERT(node != NULL, ("ng_apply_item: node is NULL"));
2440f50597f5SAlexander Motin 	KASSERT(item != NULL, ("ng_apply_item: item is NULL"));
2441f50597f5SAlexander Motin 
24421acb27c6SJulian Elischer 	NGI_GET_HOOK(item, hook); /* clears stored hook */
244330400f03SJulian Elischer #ifdef	NETGRAPH_DEBUG
2444069154d5SJulian Elischer 	_ngi_check(item, __FILE__, __LINE__);
2445069154d5SJulian Elischer #endif
24468afe16d5SGleb Smirnoff 
24478afe16d5SGleb Smirnoff 	apply = item->apply;
244810e87318SAlexander Motin 	depth = item->depth;
24498afe16d5SGleb Smirnoff 
24506b795970SJulian Elischer 	switch (item->el_flags & NGQF_TYPE) {
2451069154d5SJulian Elischer 	case NGQF_DATA:
2452069154d5SJulian Elischer 		/*
2453069154d5SJulian Elischer 		 * Check things are still ok as when we were queued.
2454069154d5SJulian Elischer 		 */
2455f50597f5SAlexander Motin 		KASSERT(hook != NULL, ("ng_apply_item: hook for data is NULL"));
2456f50597f5SAlexander Motin 		if (NG_HOOK_NOT_VALID(hook) ||
2457f50597f5SAlexander Motin 		    NG_NODE_NOT_VALID(node)) {
2458a54a69d7SJulian Elischer 			error = EIO;
2459069154d5SJulian Elischer 			NG_FREE_ITEM(item);
2460c4b5eea4SJulian Elischer 			break;
2461069154d5SJulian Elischer 		}
2462c4b5eea4SJulian Elischer 		/*
2463c4b5eea4SJulian Elischer 		 * If no receive method, just silently drop it.
2464c4b5eea4SJulian Elischer 		 * Give preference to the hook over-ride method
2465c4b5eea4SJulian Elischer 		 */
2466c4b5eea4SJulian Elischer 		if ((!(rcvdata = hook->hk_rcvdata))
2467c4b5eea4SJulian Elischer 		&& (!(rcvdata = NG_HOOK_NODE(hook)->nd_type->rcvdata))) {
2468c4b5eea4SJulian Elischer 			error = 0;
2469c4b5eea4SJulian Elischer 			NG_FREE_ITEM(item);
2470c4b5eea4SJulian Elischer 			break;
2471c4b5eea4SJulian Elischer 		}
2472a54a69d7SJulian Elischer 		error = (*rcvdata)(hook, item);
2473069154d5SJulian Elischer 		break;
2474069154d5SJulian Elischer 	case NGQF_MESG:
2475e72a98f4SAlexander Motin 		if (hook && NG_HOOK_NOT_VALID(hook)) {
2476069154d5SJulian Elischer 			/*
2477e72a98f4SAlexander Motin 			 * The hook has been zapped then we can't use it.
2478e72a98f4SAlexander Motin 			 * Immediately drop its reference.
2479069154d5SJulian Elischer 			 * The message may not need it.
2480069154d5SJulian Elischer 			 */
248130400f03SJulian Elischer 			NG_HOOK_UNREF(hook);
2482069154d5SJulian Elischer 			hook = NULL;
2483069154d5SJulian Elischer 		}
2484069154d5SJulian Elischer 		/*
2485069154d5SJulian Elischer 		 * Similarly, if the node is a zombie there is
2486069154d5SJulian Elischer 		 * nothing we can do with it, drop everything.
2487069154d5SJulian Elischer 		 */
248830400f03SJulian Elischer 		if (NG_NODE_NOT_VALID(node)) {
24896b795970SJulian Elischer 			TRAP_ERROR();
2490a54a69d7SJulian Elischer 			error = EINVAL;
2491069154d5SJulian Elischer 			NG_FREE_ITEM(item);
2492e72a98f4SAlexander Motin 			break;
2493e72a98f4SAlexander Motin 		}
2494069154d5SJulian Elischer 		/*
2495069154d5SJulian Elischer 		 * Call the appropriate message handler for the object.
2496069154d5SJulian Elischer 		 * It is up to the message handler to free the message.
2497069154d5SJulian Elischer 		 * If it's a generic message, handle it generically,
2498e72a98f4SAlexander Motin 		 * otherwise call the type's message handler (if it exists).
2499069154d5SJulian Elischer 		 * XXX (race). Remember that a queued message may
2500069154d5SJulian Elischer 		 * reference a node or hook that has just been
2501069154d5SJulian Elischer 		 * invalidated. It will exist as the queue code
2502069154d5SJulian Elischer 		 * is holding a reference, but..
2503069154d5SJulian Elischer 		 */
2504e72a98f4SAlexander Motin 		if ((NGI_MSG(item)->header.typecookie == NGM_GENERIC_COOKIE) &&
2505e72a98f4SAlexander Motin 		    ((NGI_MSG(item)->header.flags & NGF_RESP) == 0)) {
2506a54a69d7SJulian Elischer 			error = ng_generic_msg(node, item, hook);
2507c4b5eea4SJulian Elischer 			break;
2508c4b5eea4SJulian Elischer 		}
2509e72a98f4SAlexander Motin 		if (((!hook) || (!(rcvmsg = hook->hk_rcvmsg))) &&
2510e72a98f4SAlexander Motin 		    (!(rcvmsg = node->nd_type->rcvmsg))) {
25116b795970SJulian Elischer 			TRAP_ERROR();
2512a54a69d7SJulian Elischer 			error = 0;
2513069154d5SJulian Elischer 			NG_FREE_ITEM(item);
2514c4b5eea4SJulian Elischer 			break;
2515069154d5SJulian Elischer 		}
2516a54a69d7SJulian Elischer 		error = (*rcvmsg)(node, item, hook);
2517069154d5SJulian Elischer 		break;
25186b795970SJulian Elischer 	case NGQF_FN:
2519e088dd4cSAlexander Motin 	case NGQF_FN2:
2520e088dd4cSAlexander Motin 		/*
2521e088dd4cSAlexander Motin 		 *  We have to implicitly trust the hook,
2522e088dd4cSAlexander Motin 		 * as some of these are used for system purposes
2523e088dd4cSAlexander Motin 		 * where the hook is invalid. In the case of
2524e088dd4cSAlexander Motin 		 * the shutdown message we allow it to hit
2525e088dd4cSAlexander Motin 		 * even if the node is invalid.
2526e088dd4cSAlexander Motin 		 */
2527e088dd4cSAlexander Motin 		if ((NG_NODE_NOT_VALID(node))
2528e088dd4cSAlexander Motin 		&& (NGI_FN(item) != &ng_rmnode)) {
2529e088dd4cSAlexander Motin 			TRAP_ERROR();
2530e088dd4cSAlexander Motin 			error = EINVAL;
2531e088dd4cSAlexander Motin 			NG_FREE_ITEM(item);
2532e088dd4cSAlexander Motin 			break;
2533e088dd4cSAlexander Motin 		}
2534b332b91fSGleb Smirnoff 		if ((item->el_flags & NGQF_TYPE) == NGQF_FN) {
2535b332b91fSGleb Smirnoff 			(*NGI_FN(item))(node, hook, NGI_ARG1(item),
2536b332b91fSGleb Smirnoff 			    NGI_ARG2(item));
2537b332b91fSGleb Smirnoff 			NG_FREE_ITEM(item);
2538b332b91fSGleb Smirnoff 		} else	/* it is NGQF_FN2 */
2539e088dd4cSAlexander Motin 			error = (*NGI_FN2(item))(node, item, hook);
2540e088dd4cSAlexander Motin 		break;
2541069154d5SJulian Elischer 	}
2542069154d5SJulian Elischer 	/*
2543069154d5SJulian Elischer 	 * We held references on some of the resources
2544069154d5SJulian Elischer 	 * that we took from the item. Now that we have
2545069154d5SJulian Elischer 	 * finished doing everything, drop those references.
2546069154d5SJulian Elischer 	 */
2547e72a98f4SAlexander Motin 	if (hook)
254830400f03SJulian Elischer 		NG_HOOK_UNREF(hook);
2549069154d5SJulian Elischer 
2550f50597f5SAlexander Motin  	if (rw == NGQRW_R)
255130400f03SJulian Elischer 		ng_leave_read(&node->nd_input_queue);
2552f50597f5SAlexander Motin 	else
255330400f03SJulian Elischer 		ng_leave_write(&node->nd_input_queue);
25548afe16d5SGleb Smirnoff 
25558afe16d5SGleb Smirnoff 	/* Apply callback. */
255610e87318SAlexander Motin 	if (apply != NULL) {
255710e87318SAlexander Motin 		if (depth == 1 && error != 0)
255810e87318SAlexander Motin 			apply->error = error;
255910e87318SAlexander Motin 		if (refcount_release(&apply->refs))
256010e87318SAlexander Motin 			(*apply->apply)(apply->context, apply->error);
256110e87318SAlexander Motin 	}
25628afe16d5SGleb Smirnoff 
256327757487SGleb Smirnoff 	return (error);
2564069154d5SJulian Elischer }
2565069154d5SJulian Elischer 
2566069154d5SJulian Elischer /***********************************************************************
2567069154d5SJulian Elischer  * Implement the 'generic' control messages
2568069154d5SJulian Elischer  ***********************************************************************/
2569069154d5SJulian Elischer static int
2570069154d5SJulian Elischer ng_generic_msg(node_p here, item_p item, hook_p lasthook)
25714cf49a43SJulian Elischer {
25724cf49a43SJulian Elischer 	int error = 0;
2573069154d5SJulian Elischer 	struct ng_mesg *msg;
2574069154d5SJulian Elischer 	struct ng_mesg *resp = NULL;
25754cf49a43SJulian Elischer 
2576069154d5SJulian Elischer 	NGI_GET_MSG(item, msg);
25774cf49a43SJulian Elischer 	if (msg->header.typecookie != NGM_GENERIC_COOKIE) {
25786b795970SJulian Elischer 		TRAP_ERROR();
2579069154d5SJulian Elischer 		error = EINVAL;
2580069154d5SJulian Elischer 		goto out;
25814cf49a43SJulian Elischer 	}
25824cf49a43SJulian Elischer 	switch (msg->header.cmd) {
25834cf49a43SJulian Elischer 	case NGM_SHUTDOWN:
25841acb27c6SJulian Elischer 		ng_rmnode(here, NULL, NULL, 0);
25854cf49a43SJulian Elischer 		break;
25864cf49a43SJulian Elischer 	case NGM_MKPEER:
25874cf49a43SJulian Elischer 	    {
25884cf49a43SJulian Elischer 		struct ngm_mkpeer *const mkp = (struct ngm_mkpeer *) msg->data;
25894cf49a43SJulian Elischer 
25904cf49a43SJulian Elischer 		if (msg->header.arglen != sizeof(*mkp)) {
25916b795970SJulian Elischer 			TRAP_ERROR();
2592069154d5SJulian Elischer 			error = EINVAL;
2593069154d5SJulian Elischer 			break;
25944cf49a43SJulian Elischer 		}
25954cf49a43SJulian Elischer 		mkp->type[sizeof(mkp->type) - 1] = '\0';
25964cf49a43SJulian Elischer 		mkp->ourhook[sizeof(mkp->ourhook) - 1] = '\0';
25974cf49a43SJulian Elischer 		mkp->peerhook[sizeof(mkp->peerhook) - 1] = '\0';
25984cf49a43SJulian Elischer 		error = ng_mkpeer(here, mkp->ourhook, mkp->peerhook, mkp->type);
25994cf49a43SJulian Elischer 		break;
26004cf49a43SJulian Elischer 	    }
26014cf49a43SJulian Elischer 	case NGM_CONNECT:
26024cf49a43SJulian Elischer 	    {
26034cf49a43SJulian Elischer 		struct ngm_connect *const con =
26044cf49a43SJulian Elischer 			(struct ngm_connect *) msg->data;
26054cf49a43SJulian Elischer 		node_p node2;
26064cf49a43SJulian Elischer 
26074cf49a43SJulian Elischer 		if (msg->header.arglen != sizeof(*con)) {
26086b795970SJulian Elischer 			TRAP_ERROR();
2609069154d5SJulian Elischer 			error = EINVAL;
2610069154d5SJulian Elischer 			break;
26114cf49a43SJulian Elischer 		}
26124cf49a43SJulian Elischer 		con->path[sizeof(con->path) - 1] = '\0';
26134cf49a43SJulian Elischer 		con->ourhook[sizeof(con->ourhook) - 1] = '\0';
26144cf49a43SJulian Elischer 		con->peerhook[sizeof(con->peerhook) - 1] = '\0';
2615069154d5SJulian Elischer 		/* Don't forget we get a reference.. */
2616069154d5SJulian Elischer 		error = ng_path2noderef(here, con->path, &node2, NULL);
26174cf49a43SJulian Elischer 		if (error)
26184cf49a43SJulian Elischer 			break;
2619e088dd4cSAlexander Motin 		error = ng_con_nodes(item, here, con->ourhook,
2620e088dd4cSAlexander Motin 		    node2, con->peerhook);
262130400f03SJulian Elischer 		NG_NODE_UNREF(node2);
26224cf49a43SJulian Elischer 		break;
26234cf49a43SJulian Elischer 	    }
26244cf49a43SJulian Elischer 	case NGM_NAME:
26254cf49a43SJulian Elischer 	    {
26264cf49a43SJulian Elischer 		struct ngm_name *const nam = (struct ngm_name *) msg->data;
26274cf49a43SJulian Elischer 
26284cf49a43SJulian Elischer 		if (msg->header.arglen != sizeof(*nam)) {
26296b795970SJulian Elischer 			TRAP_ERROR();
2630069154d5SJulian Elischer 			error = EINVAL;
2631069154d5SJulian Elischer 			break;
26324cf49a43SJulian Elischer 		}
26334cf49a43SJulian Elischer 		nam->name[sizeof(nam->name) - 1] = '\0';
26344cf49a43SJulian Elischer 		error = ng_name_node(here, nam->name);
26354cf49a43SJulian Elischer 		break;
26364cf49a43SJulian Elischer 	    }
26374cf49a43SJulian Elischer 	case NGM_RMHOOK:
26384cf49a43SJulian Elischer 	    {
26394cf49a43SJulian Elischer 		struct ngm_rmhook *const rmh = (struct ngm_rmhook *) msg->data;
26404cf49a43SJulian Elischer 		hook_p hook;
26414cf49a43SJulian Elischer 
26424cf49a43SJulian Elischer 		if (msg->header.arglen != sizeof(*rmh)) {
26436b795970SJulian Elischer 			TRAP_ERROR();
2644069154d5SJulian Elischer 			error = EINVAL;
2645069154d5SJulian Elischer 			break;
26464cf49a43SJulian Elischer 		}
26474cf49a43SJulian Elischer 		rmh->ourhook[sizeof(rmh->ourhook) - 1] = '\0';
2648899e9c4eSArchie Cobbs 		if ((hook = ng_findhook(here, rmh->ourhook)) != NULL)
26494cf49a43SJulian Elischer 			ng_destroy_hook(hook);
26504cf49a43SJulian Elischer 		break;
26514cf49a43SJulian Elischer 	    }
26524cf49a43SJulian Elischer 	case NGM_NODEINFO:
26534cf49a43SJulian Elischer 	    {
26544cf49a43SJulian Elischer 		struct nodeinfo *ni;
26554cf49a43SJulian Elischer 
2656069154d5SJulian Elischer 		NG_MKRESPONSE(resp, msg, sizeof(*ni), M_NOWAIT);
26574cf49a43SJulian Elischer 		if (resp == NULL) {
26584cf49a43SJulian Elischer 			error = ENOMEM;
26594cf49a43SJulian Elischer 			break;
26604cf49a43SJulian Elischer 		}
26614cf49a43SJulian Elischer 
26624cf49a43SJulian Elischer 		/* Fill in node info */
2663069154d5SJulian Elischer 		ni = (struct nodeinfo *) resp->data;
266430400f03SJulian Elischer 		if (NG_NODE_HAS_NAME(here))
266587e2c66aSHartmut Brandt 			strcpy(ni->name, NG_NODE_NAME(here));
266687e2c66aSHartmut Brandt 		strcpy(ni->type, here->nd_type->name);
2667dc90cad9SJulian Elischer 		ni->id = ng_node2ID(here);
266830400f03SJulian Elischer 		ni->hooks = here->nd_numhooks;
26694cf49a43SJulian Elischer 		break;
26704cf49a43SJulian Elischer 	    }
26714cf49a43SJulian Elischer 	case NGM_LISTHOOKS:
26724cf49a43SJulian Elischer 	    {
267330400f03SJulian Elischer 		const int nhooks = here->nd_numhooks;
26744cf49a43SJulian Elischer 		struct hooklist *hl;
26754cf49a43SJulian Elischer 		struct nodeinfo *ni;
26764cf49a43SJulian Elischer 		hook_p hook;
26774cf49a43SJulian Elischer 
26784cf49a43SJulian Elischer 		/* Get response struct */
2679069154d5SJulian Elischer 		NG_MKRESPONSE(resp, msg, sizeof(*hl)
26804cf49a43SJulian Elischer 		    + (nhooks * sizeof(struct linkinfo)), M_NOWAIT);
2681069154d5SJulian Elischer 		if (resp == NULL) {
26824cf49a43SJulian Elischer 			error = ENOMEM;
26834cf49a43SJulian Elischer 			break;
26844cf49a43SJulian Elischer 		}
2685069154d5SJulian Elischer 		hl = (struct hooklist *) resp->data;
26864cf49a43SJulian Elischer 		ni = &hl->nodeinfo;
26874cf49a43SJulian Elischer 
26884cf49a43SJulian Elischer 		/* Fill in node info */
268930400f03SJulian Elischer 		if (NG_NODE_HAS_NAME(here))
269087e2c66aSHartmut Brandt 			strcpy(ni->name, NG_NODE_NAME(here));
269187e2c66aSHartmut Brandt 		strcpy(ni->type, here->nd_type->name);
2692dc90cad9SJulian Elischer 		ni->id = ng_node2ID(here);
26934cf49a43SJulian Elischer 
26944cf49a43SJulian Elischer 		/* Cycle through the linked list of hooks */
26954cf49a43SJulian Elischer 		ni->hooks = 0;
269630400f03SJulian Elischer 		LIST_FOREACH(hook, &here->nd_hooks, hk_hooks) {
26974cf49a43SJulian Elischer 			struct linkinfo *const link = &hl->link[ni->hooks];
26984cf49a43SJulian Elischer 
26994cf49a43SJulian Elischer 			if (ni->hooks >= nhooks) {
27004cf49a43SJulian Elischer 				log(LOG_ERR, "%s: number of %s changed\n",
27016e551fb6SDavid E. O'Brien 				    __func__, "hooks");
27024cf49a43SJulian Elischer 				break;
27034cf49a43SJulian Elischer 			}
270430400f03SJulian Elischer 			if (NG_HOOK_NOT_VALID(hook))
27054cf49a43SJulian Elischer 				continue;
270687e2c66aSHartmut Brandt 			strcpy(link->ourhook, NG_HOOK_NAME(hook));
270787e2c66aSHartmut Brandt 			strcpy(link->peerhook, NG_PEER_HOOK_NAME(hook));
270830400f03SJulian Elischer 			if (NG_PEER_NODE_NAME(hook)[0] != '\0')
270987e2c66aSHartmut Brandt 				strcpy(link->nodeinfo.name,
271087e2c66aSHartmut Brandt 				    NG_PEER_NODE_NAME(hook));
271187e2c66aSHartmut Brandt 			strcpy(link->nodeinfo.type,
271287e2c66aSHartmut Brandt 			   NG_PEER_NODE(hook)->nd_type->name);
271330400f03SJulian Elischer 			link->nodeinfo.id = ng_node2ID(NG_PEER_NODE(hook));
271430400f03SJulian Elischer 			link->nodeinfo.hooks = NG_PEER_NODE(hook)->nd_numhooks;
27154cf49a43SJulian Elischer 			ni->hooks++;
27164cf49a43SJulian Elischer 		}
27174cf49a43SJulian Elischer 		break;
27184cf49a43SJulian Elischer 	    }
27194cf49a43SJulian Elischer 
27204cf49a43SJulian Elischer 	case NGM_LISTNAMES:
27214cf49a43SJulian Elischer 	case NGM_LISTNODES:
27224cf49a43SJulian Elischer 	    {
27234cf49a43SJulian Elischer 		const int unnamed = (msg->header.cmd == NGM_LISTNODES);
27244cf49a43SJulian Elischer 		struct namelist *nl;
27254cf49a43SJulian Elischer 		node_p node;
2726cfea3f85SAlexander Motin 		int num = 0, i;
27274cf49a43SJulian Elischer 
2728cfea3f85SAlexander Motin 		mtx_lock(&ng_namehash_mtx);
27294cf49a43SJulian Elischer 		/* Count number of nodes */
2730cfea3f85SAlexander Motin 		for (i = 0; i < NG_NAME_HASH_SIZE; i++) {
2731cfea3f85SAlexander Motin 			LIST_FOREACH(node, &ng_name_hash[i], nd_nodes) {
2732cfea3f85SAlexander Motin 				if (NG_NODE_IS_VALID(node) &&
2733cfea3f85SAlexander Motin 				    (unnamed || NG_NODE_HAS_NAME(node))) {
27344cf49a43SJulian Elischer 					num++;
27354cf49a43SJulian Elischer 				}
273670de87f2SJulian Elischer 			}
2737cfea3f85SAlexander Motin 		}
2738cfea3f85SAlexander Motin 		mtx_unlock(&ng_namehash_mtx);
27394cf49a43SJulian Elischer 
27404cf49a43SJulian Elischer 		/* Get response struct */
2741069154d5SJulian Elischer 		NG_MKRESPONSE(resp, msg, sizeof(*nl)
27424cf49a43SJulian Elischer 		    + (num * sizeof(struct nodeinfo)), M_NOWAIT);
2743069154d5SJulian Elischer 		if (resp == NULL) {
27444cf49a43SJulian Elischer 			error = ENOMEM;
27454cf49a43SJulian Elischer 			break;
27464cf49a43SJulian Elischer 		}
2747069154d5SJulian Elischer 		nl = (struct namelist *) resp->data;
27484cf49a43SJulian Elischer 
27494cf49a43SJulian Elischer 		/* Cycle through the linked list of nodes */
27504cf49a43SJulian Elischer 		nl->numnames = 0;
2751cfea3f85SAlexander Motin 		mtx_lock(&ng_namehash_mtx);
2752cfea3f85SAlexander Motin 		for (i = 0; i < NG_NAME_HASH_SIZE; i++) {
2753cfea3f85SAlexander Motin 			LIST_FOREACH(node, &ng_name_hash[i], nd_nodes) {
2754cfea3f85SAlexander Motin 				struct nodeinfo *const np =
2755cfea3f85SAlexander Motin 				    &nl->nodeinfo[nl->numnames];
27564cf49a43SJulian Elischer 
2757b96baf0aSGleb Smirnoff 				if (NG_NODE_NOT_VALID(node))
2758b96baf0aSGleb Smirnoff 					continue;
2759b96baf0aSGleb Smirnoff 				if (!unnamed && (! NG_NODE_HAS_NAME(node)))
2760b96baf0aSGleb Smirnoff 					continue;
27614cf49a43SJulian Elischer 				if (nl->numnames >= num) {
2762cfea3f85SAlexander Motin 					log(LOG_ERR, "%s: number of nodes changed\n",
2763cfea3f85SAlexander Motin 					    __func__);
27644cf49a43SJulian Elischer 					break;
27654cf49a43SJulian Elischer 				}
276630400f03SJulian Elischer 				if (NG_NODE_HAS_NAME(node))
276787e2c66aSHartmut Brandt 					strcpy(np->name, NG_NODE_NAME(node));
276887e2c66aSHartmut Brandt 				strcpy(np->type, node->nd_type->name);
2769dc90cad9SJulian Elischer 				np->id = ng_node2ID(node);
277030400f03SJulian Elischer 				np->hooks = node->nd_numhooks;
27714cf49a43SJulian Elischer 				nl->numnames++;
27724cf49a43SJulian Elischer 			}
2773cfea3f85SAlexander Motin 		}
2774cfea3f85SAlexander Motin 		mtx_unlock(&ng_namehash_mtx);
27754cf49a43SJulian Elischer 		break;
27764cf49a43SJulian Elischer 	    }
27774cf49a43SJulian Elischer 
27784cf49a43SJulian Elischer 	case NGM_LISTTYPES:
27794cf49a43SJulian Elischer 	    {
27804cf49a43SJulian Elischer 		struct typelist *tl;
27814cf49a43SJulian Elischer 		struct ng_type *type;
27824cf49a43SJulian Elischer 		int num = 0;
27834cf49a43SJulian Elischer 
27849ed346baSBosko Milekic 		mtx_lock(&ng_typelist_mtx);
27854cf49a43SJulian Elischer 		/* Count number of types */
278670de87f2SJulian Elischer 		LIST_FOREACH(type, &ng_typelist, types) {
27874cf49a43SJulian Elischer 			num++;
278870de87f2SJulian Elischer 		}
27899ed346baSBosko Milekic 		mtx_unlock(&ng_typelist_mtx);
27904cf49a43SJulian Elischer 
27914cf49a43SJulian Elischer 		/* Get response struct */
2792069154d5SJulian Elischer 		NG_MKRESPONSE(resp, msg, sizeof(*tl)
27934cf49a43SJulian Elischer 		    + (num * sizeof(struct typeinfo)), M_NOWAIT);
2794069154d5SJulian Elischer 		if (resp == NULL) {
27954cf49a43SJulian Elischer 			error = ENOMEM;
27964cf49a43SJulian Elischer 			break;
27974cf49a43SJulian Elischer 		}
2798069154d5SJulian Elischer 		tl = (struct typelist *) resp->data;
27994cf49a43SJulian Elischer 
28004cf49a43SJulian Elischer 		/* Cycle through the linked list of types */
28014cf49a43SJulian Elischer 		tl->numtypes = 0;
28029ed346baSBosko Milekic 		mtx_lock(&ng_typelist_mtx);
2803069154d5SJulian Elischer 		LIST_FOREACH(type, &ng_typelist, types) {
28044cf49a43SJulian Elischer 			struct typeinfo *const tp = &tl->typeinfo[tl->numtypes];
28054cf49a43SJulian Elischer 
28064cf49a43SJulian Elischer 			if (tl->numtypes >= num) {
28074cf49a43SJulian Elischer 				log(LOG_ERR, "%s: number of %s changed\n",
28086e551fb6SDavid E. O'Brien 				    __func__, "types");
28094cf49a43SJulian Elischer 				break;
28104cf49a43SJulian Elischer 			}
281187e2c66aSHartmut Brandt 			strcpy(tp->type_name, type->name);
2812c73b94a2SJulian Elischer 			tp->numnodes = type->refs - 1; /* don't count list */
28134cf49a43SJulian Elischer 			tl->numtypes++;
28144cf49a43SJulian Elischer 		}
28159ed346baSBosko Milekic 		mtx_unlock(&ng_typelist_mtx);
28164cf49a43SJulian Elischer 		break;
28174cf49a43SJulian Elischer 	    }
28184cf49a43SJulian Elischer 
2819f8307e12SArchie Cobbs 	case NGM_BINARY2ASCII:
2820f8307e12SArchie Cobbs 	    {
28217133ac27SArchie Cobbs 		int bufSize = 20 * 1024;	/* XXX hard coded constant */
2822f8307e12SArchie Cobbs 		const struct ng_parse_type *argstype;
2823f8307e12SArchie Cobbs 		const struct ng_cmdlist *c;
2824069154d5SJulian Elischer 		struct ng_mesg *binary, *ascii;
2825f8307e12SArchie Cobbs 
2826f8307e12SArchie Cobbs 		/* Data area must contain a valid netgraph message */
2827f8307e12SArchie Cobbs 		binary = (struct ng_mesg *)msg->data;
28284c9b5910SGleb Smirnoff 		if (msg->header.arglen < sizeof(struct ng_mesg) ||
28294c9b5910SGleb Smirnoff 		    (msg->header.arglen - sizeof(struct ng_mesg) <
28304c9b5910SGleb Smirnoff 		    binary->header.arglen)) {
28316b795970SJulian Elischer 			TRAP_ERROR();
2832f8307e12SArchie Cobbs 			error = EINVAL;
2833f8307e12SArchie Cobbs 			break;
2834f8307e12SArchie Cobbs 		}
2835f8307e12SArchie Cobbs 
2836f8307e12SArchie Cobbs 		/* Get a response message with lots of room */
2837069154d5SJulian Elischer 		NG_MKRESPONSE(resp, msg, sizeof(*ascii) + bufSize, M_NOWAIT);
2838069154d5SJulian Elischer 		if (resp == NULL) {
2839f8307e12SArchie Cobbs 			error = ENOMEM;
2840f8307e12SArchie Cobbs 			break;
2841f8307e12SArchie Cobbs 		}
2842069154d5SJulian Elischer 		ascii = (struct ng_mesg *)resp->data;
2843f8307e12SArchie Cobbs 
2844f8307e12SArchie Cobbs 		/* Copy binary message header to response message payload */
2845f8307e12SArchie Cobbs 		bcopy(binary, ascii, sizeof(*binary));
2846f8307e12SArchie Cobbs 
2847f8307e12SArchie Cobbs 		/* Find command by matching typecookie and command number */
284830400f03SJulian Elischer 		for (c = here->nd_type->cmdlist;
2849f8307e12SArchie Cobbs 		    c != NULL && c->name != NULL; c++) {
2850f8307e12SArchie Cobbs 			if (binary->header.typecookie == c->cookie
2851f8307e12SArchie Cobbs 			    && binary->header.cmd == c->cmd)
2852f8307e12SArchie Cobbs 				break;
2853f8307e12SArchie Cobbs 		}
2854f8307e12SArchie Cobbs 		if (c == NULL || c->name == NULL) {
2855f8307e12SArchie Cobbs 			for (c = ng_generic_cmds; c->name != NULL; c++) {
2856f8307e12SArchie Cobbs 				if (binary->header.typecookie == c->cookie
2857f8307e12SArchie Cobbs 				    && binary->header.cmd == c->cmd)
2858f8307e12SArchie Cobbs 					break;
2859f8307e12SArchie Cobbs 			}
2860f8307e12SArchie Cobbs 			if (c->name == NULL) {
2861069154d5SJulian Elischer 				NG_FREE_MSG(resp);
2862f8307e12SArchie Cobbs 				error = ENOSYS;
2863f8307e12SArchie Cobbs 				break;
2864f8307e12SArchie Cobbs 			}
2865f8307e12SArchie Cobbs 		}
2866f8307e12SArchie Cobbs 
2867f8307e12SArchie Cobbs 		/* Convert command name to ASCII */
2868f8307e12SArchie Cobbs 		snprintf(ascii->header.cmdstr, sizeof(ascii->header.cmdstr),
2869f8307e12SArchie Cobbs 		    "%s", c->name);
2870f8307e12SArchie Cobbs 
2871f8307e12SArchie Cobbs 		/* Convert command arguments to ASCII */
2872f8307e12SArchie Cobbs 		argstype = (binary->header.flags & NGF_RESP) ?
2873f8307e12SArchie Cobbs 		    c->respType : c->mesgType;
287470de87f2SJulian Elischer 		if (argstype == NULL) {
2875f8307e12SArchie Cobbs 			*ascii->data = '\0';
287670de87f2SJulian Elischer 		} else {
2877f8307e12SArchie Cobbs 			if ((error = ng_unparse(argstype,
2878f8307e12SArchie Cobbs 			    (u_char *)binary->data,
2879f8307e12SArchie Cobbs 			    ascii->data, bufSize)) != 0) {
2880069154d5SJulian Elischer 				NG_FREE_MSG(resp);
2881f8307e12SArchie Cobbs 				break;
2882f8307e12SArchie Cobbs 			}
2883f8307e12SArchie Cobbs 		}
2884f8307e12SArchie Cobbs 
2885f8307e12SArchie Cobbs 		/* Return the result as struct ng_mesg plus ASCII string */
2886f8307e12SArchie Cobbs 		bufSize = strlen(ascii->data) + 1;
2887f8307e12SArchie Cobbs 		ascii->header.arglen = bufSize;
2888069154d5SJulian Elischer 		resp->header.arglen = sizeof(*ascii) + bufSize;
2889f8307e12SArchie Cobbs 		break;
2890f8307e12SArchie Cobbs 	    }
2891f8307e12SArchie Cobbs 
2892f8307e12SArchie Cobbs 	case NGM_ASCII2BINARY:
2893f8307e12SArchie Cobbs 	    {
2894f8307e12SArchie Cobbs 		int bufSize = 2000;	/* XXX hard coded constant */
2895f8307e12SArchie Cobbs 		const struct ng_cmdlist *c;
2896f8307e12SArchie Cobbs 		const struct ng_parse_type *argstype;
2897069154d5SJulian Elischer 		struct ng_mesg *ascii, *binary;
289852ec4a03SArchie Cobbs 		int off = 0;
2899f8307e12SArchie Cobbs 
2900f8307e12SArchie Cobbs 		/* Data area must contain at least a struct ng_mesg + '\0' */
2901f8307e12SArchie Cobbs 		ascii = (struct ng_mesg *)msg->data;
29024c9b5910SGleb Smirnoff 		if ((msg->header.arglen < sizeof(*ascii) + 1) ||
29034c9b5910SGleb Smirnoff 		    (ascii->header.arglen < 1) ||
29044c9b5910SGleb Smirnoff 		    (msg->header.arglen < sizeof(*ascii) +
29054c9b5910SGleb Smirnoff 		    ascii->header.arglen)) {
29066b795970SJulian Elischer 			TRAP_ERROR();
2907f8307e12SArchie Cobbs 			error = EINVAL;
2908f8307e12SArchie Cobbs 			break;
2909f8307e12SArchie Cobbs 		}
2910f8307e12SArchie Cobbs 		ascii->data[ascii->header.arglen - 1] = '\0';
2911f8307e12SArchie Cobbs 
2912f8307e12SArchie Cobbs 		/* Get a response message with lots of room */
2913069154d5SJulian Elischer 		NG_MKRESPONSE(resp, msg, sizeof(*binary) + bufSize, M_NOWAIT);
2914069154d5SJulian Elischer 		if (resp == NULL) {
2915f8307e12SArchie Cobbs 			error = ENOMEM;
2916f8307e12SArchie Cobbs 			break;
2917f8307e12SArchie Cobbs 		}
2918069154d5SJulian Elischer 		binary = (struct ng_mesg *)resp->data;
2919f8307e12SArchie Cobbs 
2920f8307e12SArchie Cobbs 		/* Copy ASCII message header to response message payload */
2921f8307e12SArchie Cobbs 		bcopy(ascii, binary, sizeof(*ascii));
2922f8307e12SArchie Cobbs 
2923f8307e12SArchie Cobbs 		/* Find command by matching ASCII command string */
292430400f03SJulian Elischer 		for (c = here->nd_type->cmdlist;
2925f8307e12SArchie Cobbs 		    c != NULL && c->name != NULL; c++) {
2926f8307e12SArchie Cobbs 			if (strcmp(ascii->header.cmdstr, c->name) == 0)
2927f8307e12SArchie Cobbs 				break;
2928f8307e12SArchie Cobbs 		}
2929f8307e12SArchie Cobbs 		if (c == NULL || c->name == NULL) {
2930f8307e12SArchie Cobbs 			for (c = ng_generic_cmds; c->name != NULL; c++) {
2931f8307e12SArchie Cobbs 				if (strcmp(ascii->header.cmdstr, c->name) == 0)
2932f8307e12SArchie Cobbs 					break;
2933f8307e12SArchie Cobbs 			}
2934f8307e12SArchie Cobbs 			if (c->name == NULL) {
2935069154d5SJulian Elischer 				NG_FREE_MSG(resp);
2936f8307e12SArchie Cobbs 				error = ENOSYS;
2937f8307e12SArchie Cobbs 				break;
2938f8307e12SArchie Cobbs 			}
2939f8307e12SArchie Cobbs 		}
2940f8307e12SArchie Cobbs 
2941f8307e12SArchie Cobbs 		/* Convert command name to binary */
2942f8307e12SArchie Cobbs 		binary->header.cmd = c->cmd;
2943f8307e12SArchie Cobbs 		binary->header.typecookie = c->cookie;
2944f8307e12SArchie Cobbs 
2945f8307e12SArchie Cobbs 		/* Convert command arguments to binary */
2946f8307e12SArchie Cobbs 		argstype = (binary->header.flags & NGF_RESP) ?
2947f8307e12SArchie Cobbs 		    c->respType : c->mesgType;
294870de87f2SJulian Elischer 		if (argstype == NULL) {
2949f8307e12SArchie Cobbs 			bufSize = 0;
295070de87f2SJulian Elischer 		} else {
2951f8307e12SArchie Cobbs 			if ((error = ng_parse(argstype, ascii->data,
2952f8307e12SArchie Cobbs 			    &off, (u_char *)binary->data, &bufSize)) != 0) {
2953069154d5SJulian Elischer 				NG_FREE_MSG(resp);
2954f8307e12SArchie Cobbs 				break;
2955f8307e12SArchie Cobbs 			}
2956f8307e12SArchie Cobbs 		}
2957f8307e12SArchie Cobbs 
2958f8307e12SArchie Cobbs 		/* Return the result */
2959f8307e12SArchie Cobbs 		binary->header.arglen = bufSize;
2960069154d5SJulian Elischer 		resp->header.arglen = sizeof(*binary) + bufSize;
2961f8307e12SArchie Cobbs 		break;
2962f8307e12SArchie Cobbs 	    }
2963f8307e12SArchie Cobbs 
29647095e097SPoul-Henning Kamp 	case NGM_TEXT_CONFIG:
29654cf49a43SJulian Elischer 	case NGM_TEXT_STATUS:
29664cf49a43SJulian Elischer 		/*
29674cf49a43SJulian Elischer 		 * This one is tricky as it passes the command down to the
29684cf49a43SJulian Elischer 		 * actual node, even though it is a generic type command.
2969069154d5SJulian Elischer 		 * This means we must assume that the item/msg is already freed
29704cf49a43SJulian Elischer 		 * when control passes back to us.
29714cf49a43SJulian Elischer 		 */
297230400f03SJulian Elischer 		if (here->nd_type->rcvmsg != NULL) {
2973069154d5SJulian Elischer 			NGI_MSG(item) = msg; /* put it back as we found it */
297430400f03SJulian Elischer 			return((*here->nd_type->rcvmsg)(here, item, lasthook));
29754cf49a43SJulian Elischer 		}
29764cf49a43SJulian Elischer 		/* Fall through if rcvmsg not supported */
29774cf49a43SJulian Elischer 	default:
29786b795970SJulian Elischer 		TRAP_ERROR();
29794cf49a43SJulian Elischer 		error = EINVAL;
29804cf49a43SJulian Elischer 	}
2981069154d5SJulian Elischer 	/*
2982069154d5SJulian Elischer 	 * Sometimes a generic message may be statically allocated
2983069154d5SJulian Elischer 	 * to avoid problems with allocating when in tight memeory situations.
2984069154d5SJulian Elischer 	 * Don't free it if it is so.
2985069154d5SJulian Elischer 	 * I break them appart here, because erros may cause a free if the item
2986069154d5SJulian Elischer 	 * in which case we'd be doing it twice.
2987069154d5SJulian Elischer 	 * they are kept together above, to simplify freeing.
2988069154d5SJulian Elischer 	 */
2989069154d5SJulian Elischer out:
2990069154d5SJulian Elischer 	NG_RESPOND_MSG(error, here, item, resp);
29911acb27c6SJulian Elischer 	if (msg)
2992069154d5SJulian Elischer 		NG_FREE_MSG(msg);
29934cf49a43SJulian Elischer 	return (error);
29944cf49a43SJulian Elischer }
29954cf49a43SJulian Elischer 
29964cf49a43SJulian Elischer /************************************************************************
29978253c060SGleb Smirnoff 			Queue element get/free routines
29988253c060SGleb Smirnoff ************************************************************************/
29998253c060SGleb Smirnoff 
30008253c060SGleb Smirnoff uma_zone_t			ng_qzone;
3001ed75521fSAlexander Motin static int			maxalloc = 4096;/* limit the damage of a leak */
3002ed75521fSAlexander Motin static int			maxdata = 512;	/* limit the damage of a DoS */
3003ed75521fSAlexander Motin static int			useddata = 0;
30048253c060SGleb Smirnoff 
30058253c060SGleb Smirnoff TUNABLE_INT("net.graph.maxalloc", &maxalloc);
30068253c060SGleb Smirnoff SYSCTL_INT(_net_graph, OID_AUTO, maxalloc, CTLFLAG_RDTUN, &maxalloc,
30078253c060SGleb Smirnoff     0, "Maximum number of queue items to allocate");
3008ed75521fSAlexander Motin TUNABLE_INT("net.graph.maxdata", &maxdata);
3009ed75521fSAlexander Motin SYSCTL_INT(_net_graph, OID_AUTO, maxdata, CTLFLAG_RW | CTLFLAG_TUN, &maxdata,
3010ed75521fSAlexander Motin     0, "Maximum number of queue data items to allocate");
30118253c060SGleb Smirnoff 
30128253c060SGleb Smirnoff #ifdef	NETGRAPH_DEBUG
30138253c060SGleb Smirnoff static TAILQ_HEAD(, ng_item) ng_itemlist = TAILQ_HEAD_INITIALIZER(ng_itemlist);
30148253c060SGleb Smirnoff static int			allocated;	/* number of items malloc'd */
30158253c060SGleb Smirnoff #endif
30168253c060SGleb Smirnoff 
30178253c060SGleb Smirnoff /*
30188253c060SGleb Smirnoff  * Get a queue entry.
30198253c060SGleb Smirnoff  * This is usually called when a packet first enters netgraph.
30208253c060SGleb Smirnoff  * By definition, this is usually from an interrupt, or from a user.
30218253c060SGleb Smirnoff  * Users are not so important, but try be quick for the times that it's
30228253c060SGleb Smirnoff  * an interrupt.
30238253c060SGleb Smirnoff  */
30248253c060SGleb Smirnoff static __inline item_p
302542282202SGleb Smirnoff ng_getqblk(int flags)
30268253c060SGleb Smirnoff {
30278253c060SGleb Smirnoff 	item_p item = NULL;
302842282202SGleb Smirnoff 	int wait;
30298253c060SGleb Smirnoff 
303042282202SGleb Smirnoff 	wait = (flags & NG_WAITOK) ? M_WAITOK : M_NOWAIT;
303142282202SGleb Smirnoff 
303242282202SGleb Smirnoff 	item = uma_zalloc(ng_qzone, wait | M_ZERO);
30338253c060SGleb Smirnoff 
30348253c060SGleb Smirnoff #ifdef	NETGRAPH_DEBUG
30358253c060SGleb Smirnoff 	if (item) {
30368253c060SGleb Smirnoff 			mtx_lock(&ngq_mtx);
30378253c060SGleb Smirnoff 			TAILQ_INSERT_TAIL(&ng_itemlist, item, all);
30388253c060SGleb Smirnoff 			allocated++;
30398253c060SGleb Smirnoff 			mtx_unlock(&ngq_mtx);
30408253c060SGleb Smirnoff 	}
30418253c060SGleb Smirnoff #endif
30428253c060SGleb Smirnoff 
30438253c060SGleb Smirnoff 	return (item);
30448253c060SGleb Smirnoff }
30458253c060SGleb Smirnoff 
30468253c060SGleb Smirnoff /*
30478253c060SGleb Smirnoff  * Release a queue entry
30488253c060SGleb Smirnoff  */
30498253c060SGleb Smirnoff void
30508253c060SGleb Smirnoff ng_free_item(item_p item)
30518253c060SGleb Smirnoff {
30528253c060SGleb Smirnoff 	/*
30538253c060SGleb Smirnoff 	 * The item may hold resources on it's own. We need to free
30548253c060SGleb Smirnoff 	 * these before we can free the item. What they are depends upon
30558253c060SGleb Smirnoff 	 * what kind of item it is. it is important that nodes zero
30568253c060SGleb Smirnoff 	 * out pointers to resources that they remove from the item
30578253c060SGleb Smirnoff 	 * or we release them again here.
30588253c060SGleb Smirnoff 	 */
30598253c060SGleb Smirnoff 	switch (item->el_flags & NGQF_TYPE) {
30608253c060SGleb Smirnoff 	case NGQF_DATA:
3061ed75521fSAlexander Motin 		atomic_subtract_int(&useddata, 1);
30628253c060SGleb Smirnoff 		/* If we have an mbuf still attached.. */
30638253c060SGleb Smirnoff 		NG_FREE_M(_NGI_M(item));
30648253c060SGleb Smirnoff 		break;
30658253c060SGleb Smirnoff 	case NGQF_MESG:
30668253c060SGleb Smirnoff 		_NGI_RETADDR(item) = 0;
30678253c060SGleb Smirnoff 		NG_FREE_MSG(_NGI_MSG(item));
30688253c060SGleb Smirnoff 		break;
30698253c060SGleb Smirnoff 	case NGQF_FN:
3070e088dd4cSAlexander Motin 	case NGQF_FN2:
30718253c060SGleb Smirnoff 		/* nothing to free really, */
30728253c060SGleb Smirnoff 		_NGI_FN(item) = NULL;
30738253c060SGleb Smirnoff 		_NGI_ARG1(item) = NULL;
30748253c060SGleb Smirnoff 		_NGI_ARG2(item) = 0;
30758253c060SGleb Smirnoff 		break;
30768253c060SGleb Smirnoff 	}
30778253c060SGleb Smirnoff 	/* If we still have a node or hook referenced... */
30788253c060SGleb Smirnoff 	_NGI_CLR_NODE(item);
30798253c060SGleb Smirnoff 	_NGI_CLR_HOOK(item);
30808253c060SGleb Smirnoff 
30818253c060SGleb Smirnoff #ifdef	NETGRAPH_DEBUG
30828253c060SGleb Smirnoff 	mtx_lock(&ngq_mtx);
30838253c060SGleb Smirnoff 	TAILQ_REMOVE(&ng_itemlist, item, all);
30848253c060SGleb Smirnoff 	allocated--;
30858253c060SGleb Smirnoff 	mtx_unlock(&ngq_mtx);
30868253c060SGleb Smirnoff #endif
30878253c060SGleb Smirnoff 	uma_zfree(ng_qzone, item);
30888253c060SGleb Smirnoff }
30898253c060SGleb Smirnoff 
30908253c060SGleb Smirnoff /************************************************************************
30914cf49a43SJulian Elischer 			Module routines
30924cf49a43SJulian Elischer ************************************************************************/
30934cf49a43SJulian Elischer 
30944cf49a43SJulian Elischer /*
30954cf49a43SJulian Elischer  * Handle the loading/unloading of a netgraph node type module
30964cf49a43SJulian Elischer  */
30974cf49a43SJulian Elischer int
30984cf49a43SJulian Elischer ng_mod_event(module_t mod, int event, void *data)
30994cf49a43SJulian Elischer {
31004cf49a43SJulian Elischer 	struct ng_type *const type = data;
31014cf49a43SJulian Elischer 	int s, error = 0;
31024cf49a43SJulian Elischer 
31034cf49a43SJulian Elischer 	switch (event) {
31044cf49a43SJulian Elischer 	case MOD_LOAD:
31054cf49a43SJulian Elischer 
31064cf49a43SJulian Elischer 		/* Register new netgraph node type */
31074cf49a43SJulian Elischer 		s = splnet();
31084cf49a43SJulian Elischer 		if ((error = ng_newtype(type)) != 0) {
31094cf49a43SJulian Elischer 			splx(s);
31104cf49a43SJulian Elischer 			break;
31114cf49a43SJulian Elischer 		}
31124cf49a43SJulian Elischer 
31134cf49a43SJulian Elischer 		/* Call type specific code */
31144cf49a43SJulian Elischer 		if (type->mod_event != NULL)
3115069154d5SJulian Elischer 			if ((error = (*type->mod_event)(mod, event, data))) {
31169ed346baSBosko Milekic 				mtx_lock(&ng_typelist_mtx);
3117c73b94a2SJulian Elischer 				type->refs--;	/* undo it */
31184cf49a43SJulian Elischer 				LIST_REMOVE(type, types);
31199ed346baSBosko Milekic 				mtx_unlock(&ng_typelist_mtx);
3120069154d5SJulian Elischer 			}
31214cf49a43SJulian Elischer 		splx(s);
31224cf49a43SJulian Elischer 		break;
31234cf49a43SJulian Elischer 
31244cf49a43SJulian Elischer 	case MOD_UNLOAD:
31254cf49a43SJulian Elischer 		s = splnet();
3126c73b94a2SJulian Elischer 		if (type->refs > 1) {		/* make sure no nodes exist! */
31274cf49a43SJulian Elischer 			error = EBUSY;
3128c73b94a2SJulian Elischer 		} else {
3129c73b94a2SJulian Elischer 			if (type->refs == 0) {
3130c73b94a2SJulian Elischer 				/* failed load, nothing to undo */
3131c73b94a2SJulian Elischer 				splx(s);
3132c73b94a2SJulian Elischer 				break;
3133c73b94a2SJulian Elischer 			}
31344cf49a43SJulian Elischer 			if (type->mod_event != NULL) {	/* check with type */
31354cf49a43SJulian Elischer 				error = (*type->mod_event)(mod, event, data);
31364cf49a43SJulian Elischer 				if (error != 0) {	/* type refuses.. */
31374cf49a43SJulian Elischer 					splx(s);
31384cf49a43SJulian Elischer 					break;
31394cf49a43SJulian Elischer 				}
31404cf49a43SJulian Elischer 			}
31419ed346baSBosko Milekic 			mtx_lock(&ng_typelist_mtx);
31424cf49a43SJulian Elischer 			LIST_REMOVE(type, types);
31439ed346baSBosko Milekic 			mtx_unlock(&ng_typelist_mtx);
31444cf49a43SJulian Elischer 		}
31454cf49a43SJulian Elischer 		splx(s);
31464cf49a43SJulian Elischer 		break;
31474cf49a43SJulian Elischer 
31484cf49a43SJulian Elischer 	default:
31494cf49a43SJulian Elischer 		if (type->mod_event != NULL)
31504cf49a43SJulian Elischer 			error = (*type->mod_event)(mod, event, data);
31514cf49a43SJulian Elischer 		else
31523e019deaSPoul-Henning Kamp 			error = EOPNOTSUPP;		/* XXX ? */
31534cf49a43SJulian Elischer 		break;
31544cf49a43SJulian Elischer 	}
31554cf49a43SJulian Elischer 	return (error);
31564cf49a43SJulian Elischer }
31574cf49a43SJulian Elischer 
31584cf49a43SJulian Elischer /*
31594cf49a43SJulian Elischer  * Handle loading and unloading for this code.
31604cf49a43SJulian Elischer  * The only thing we need to link into is the NETISR strucure.
31614cf49a43SJulian Elischer  */
31624cf49a43SJulian Elischer static int
31634cf49a43SJulian Elischer ngb_mod_event(module_t mod, int event, void *data)
31644cf49a43SJulian Elischer {
31651489164fSGleb Smirnoff 	int error = 0;
31664cf49a43SJulian Elischer 
31674cf49a43SJulian Elischer 	switch (event) {
31684cf49a43SJulian Elischer 	case MOD_LOAD:
31691489164fSGleb Smirnoff 		/* Initialize everything. */
31702c8dda8dSWojciech A. Koszek 		NG_WORKLIST_LOCK_INIT();
3171efd8e7c9SDon Lewis 		mtx_init(&ng_typelist_mtx, "netgraph types mutex", NULL,
3172efd8e7c9SDon Lewis 		    MTX_DEF);
3173efd8e7c9SDon Lewis 		mtx_init(&ng_idhash_mtx, "netgraph idhash mutex", NULL,
3174efd8e7c9SDon Lewis 		    MTX_DEF);
3175cfea3f85SAlexander Motin 		mtx_init(&ng_namehash_mtx, "netgraph namehash mutex", NULL,
3176cfea3f85SAlexander Motin 		    MTX_DEF);
3177ac5dd141SGleb Smirnoff 		mtx_init(&ng_topo_mtx, "netgraph topology mutex", NULL,
3178ac5dd141SGleb Smirnoff 		    MTX_DEF);
31791489164fSGleb Smirnoff #ifdef	NETGRAPH_DEBUG
3180cfea3f85SAlexander Motin 		mtx_init(&ng_nodelist_mtx, "netgraph nodelist mutex", NULL,
3181cfea3f85SAlexander Motin 		    MTX_DEF);
31821489164fSGleb Smirnoff 		mtx_init(&ngq_mtx, "netgraph item list mutex", NULL,
3183efd8e7c9SDon Lewis 		    MTX_DEF);
31841489164fSGleb Smirnoff #endif
31851489164fSGleb Smirnoff 		ng_qzone = uma_zcreate("NetGraph items", sizeof(struct ng_item),
31861489164fSGleb Smirnoff 		    NULL, NULL, NULL, NULL, UMA_ALIGN_CACHE, 0);
31871489164fSGleb Smirnoff 		uma_zone_set_max(ng_qzone, maxalloc);
318868780975SGleb Smirnoff 		netisr_register(NETISR_NETGRAPH, (netisr_t *)ngintr, NULL,
318968780975SGleb Smirnoff 		    NETISR_MPSAFE);
31904cf49a43SJulian Elischer 		break;
31914cf49a43SJulian Elischer 	case MOD_UNLOAD:
319264efc707SRobert Watson 		/* You can't unload it because an interface may be using it. */
31934cf49a43SJulian Elischer 		error = EBUSY;
31944cf49a43SJulian Elischer 		break;
31954cf49a43SJulian Elischer 	default:
31964cf49a43SJulian Elischer 		error = EOPNOTSUPP;
31974cf49a43SJulian Elischer 		break;
31984cf49a43SJulian Elischer 	}
31994cf49a43SJulian Elischer 	return (error);
32004cf49a43SJulian Elischer }
32014cf49a43SJulian Elischer 
32024cf49a43SJulian Elischer static moduledata_t netgraph_mod = {
32034cf49a43SJulian Elischer 	"netgraph",
32044cf49a43SJulian Elischer 	ngb_mod_event,
32054cf49a43SJulian Elischer 	(NULL)
32064cf49a43SJulian Elischer };
3207aa38f8f9SMaksim Yevmenkin DECLARE_MODULE(netgraph, netgraph_mod, SI_SUB_NETGRAPH, SI_ORDER_MIDDLE);
3208bfa7e882SJulian Elischer SYSCTL_NODE(_net, OID_AUTO, graph, CTLFLAG_RW, 0, "netgraph Family");
3209bfa7e882SJulian Elischer SYSCTL_INT(_net_graph, OID_AUTO, abi_version, CTLFLAG_RD, 0, NG_ABI_VERSION,"");
3210bfa7e882SJulian Elischer SYSCTL_INT(_net_graph, OID_AUTO, msg_version, CTLFLAG_RD, 0, NG_VERSION, "");
32114cf49a43SJulian Elischer 
321230400f03SJulian Elischer #ifdef	NETGRAPH_DEBUG
321330400f03SJulian Elischer void
321430400f03SJulian Elischer dumphook (hook_p hook, char *file, int line)
321530400f03SJulian Elischer {
321630400f03SJulian Elischer 	printf("hook: name %s, %d refs, Last touched:\n",
321730400f03SJulian Elischer 		_NG_HOOK_NAME(hook), hook->hk_refs);
321830400f03SJulian Elischer 	printf("	Last active @ %s, line %d\n",
321930400f03SJulian Elischer 		hook->lastfile, hook->lastline);
322030400f03SJulian Elischer 	if (line) {
322130400f03SJulian Elischer 		printf(" problem discovered at file %s, line %d\n", file, line);
322230400f03SJulian Elischer 	}
322330400f03SJulian Elischer }
322430400f03SJulian Elischer 
322530400f03SJulian Elischer void
322630400f03SJulian Elischer dumpnode(node_p node, char *file, int line)
322730400f03SJulian Elischer {
322830400f03SJulian Elischer 	printf("node: ID [%x]: type '%s', %d hooks, flags 0x%x, %d refs, %s:\n",
32296b795970SJulian Elischer 		_NG_NODE_ID(node), node->nd_type->name,
323030400f03SJulian Elischer 		node->nd_numhooks, node->nd_flags,
323130400f03SJulian Elischer 		node->nd_refs, node->nd_name);
323230400f03SJulian Elischer 	printf("	Last active @ %s, line %d\n",
323330400f03SJulian Elischer 		node->lastfile, node->lastline);
323430400f03SJulian Elischer 	if (line) {
323530400f03SJulian Elischer 		printf(" problem discovered at file %s, line %d\n", file, line);
323630400f03SJulian Elischer 	}
323730400f03SJulian Elischer }
323830400f03SJulian Elischer 
3239069154d5SJulian Elischer void
3240069154d5SJulian Elischer dumpitem(item_p item, char *file, int line)
3241069154d5SJulian Elischer {
3242069154d5SJulian Elischer 	printf(" ACTIVE item, last used at %s, line %d",
3243069154d5SJulian Elischer 		item->lastfile, item->lastline);
32446b795970SJulian Elischer 	switch(item->el_flags & NGQF_TYPE) {
32456b795970SJulian Elischer 	case NGQF_DATA:
3246069154d5SJulian Elischer 		printf(" - [data]\n");
32476b795970SJulian Elischer 		break;
32486b795970SJulian Elischer 	case NGQF_MESG:
32496b795970SJulian Elischer 		printf(" - retaddr[%d]:\n", _NGI_RETADDR(item));
32506b795970SJulian Elischer 		break;
32516b795970SJulian Elischer 	case NGQF_FN:
3252857304e6SRuslan Ermilov 		printf(" - fn@%p (%p, %p, %p, %d (%x))\n",
3253857304e6SRuslan Ermilov 			_NGI_FN(item),
3254857304e6SRuslan Ermilov 			_NGI_NODE(item),
3255857304e6SRuslan Ermilov 			_NGI_HOOK(item),
3256857304e6SRuslan Ermilov 			item->body.fn.fn_arg1,
3257857304e6SRuslan Ermilov 			item->body.fn.fn_arg2,
3258857304e6SRuslan Ermilov 			item->body.fn.fn_arg2);
3259857304e6SRuslan Ermilov 		break;
3260e088dd4cSAlexander Motin 	case NGQF_FN2:
3261eb4687d2SAlexander Motin 		printf(" - fn2@%p (%p, %p, %p, %d (%x))\n",
3262857304e6SRuslan Ermilov 			_NGI_FN2(item),
32636064e568SGleb Smirnoff 			_NGI_NODE(item),
32646064e568SGleb Smirnoff 			_NGI_HOOK(item),
32656b795970SJulian Elischer 			item->body.fn.fn_arg1,
32666b795970SJulian Elischer 			item->body.fn.fn_arg2,
32676b795970SJulian Elischer 			item->body.fn.fn_arg2);
32686b795970SJulian Elischer 		break;
3269069154d5SJulian Elischer 	}
327030400f03SJulian Elischer 	if (line) {
3271069154d5SJulian Elischer 		printf(" problem discovered at file %s, line %d\n", file, line);
32726064e568SGleb Smirnoff 		if (_NGI_NODE(item)) {
327330400f03SJulian Elischer 			printf("node %p ([%x])\n",
32746064e568SGleb Smirnoff 				_NGI_NODE(item), ng_node2ID(_NGI_NODE(item)));
3275069154d5SJulian Elischer 		}
327630400f03SJulian Elischer 	}
327730400f03SJulian Elischer }
327830400f03SJulian Elischer 
327930400f03SJulian Elischer static void
328030400f03SJulian Elischer ng_dumpitems(void)
328130400f03SJulian Elischer {
328230400f03SJulian Elischer 	item_p item;
328330400f03SJulian Elischer 	int i = 1;
328430400f03SJulian Elischer 	TAILQ_FOREACH(item, &ng_itemlist, all) {
328530400f03SJulian Elischer 		printf("[%d] ", i++);
328630400f03SJulian Elischer 		dumpitem(item, NULL, 0);
328730400f03SJulian Elischer 	}
328830400f03SJulian Elischer }
328930400f03SJulian Elischer 
329030400f03SJulian Elischer static void
329130400f03SJulian Elischer ng_dumpnodes(void)
329230400f03SJulian Elischer {
329330400f03SJulian Elischer 	node_p node;
329430400f03SJulian Elischer 	int i = 1;
329553f9c5e9SRobert Watson 	mtx_lock(&ng_nodelist_mtx);
329630400f03SJulian Elischer 	SLIST_FOREACH(node, &ng_allnodes, nd_all) {
329730400f03SJulian Elischer 		printf("[%d] ", i++);
329830400f03SJulian Elischer 		dumpnode(node, NULL, 0);
329930400f03SJulian Elischer 	}
330053f9c5e9SRobert Watson 	mtx_unlock(&ng_nodelist_mtx);
330130400f03SJulian Elischer }
330230400f03SJulian Elischer 
330330400f03SJulian Elischer static void
330430400f03SJulian Elischer ng_dumphooks(void)
330530400f03SJulian Elischer {
330630400f03SJulian Elischer 	hook_p hook;
330730400f03SJulian Elischer 	int i = 1;
330853f9c5e9SRobert Watson 	mtx_lock(&ng_nodelist_mtx);
330930400f03SJulian Elischer 	SLIST_FOREACH(hook, &ng_allhooks, hk_all) {
331030400f03SJulian Elischer 		printf("[%d] ", i++);
331130400f03SJulian Elischer 		dumphook(hook, NULL, 0);
331230400f03SJulian Elischer 	}
331353f9c5e9SRobert Watson 	mtx_unlock(&ng_nodelist_mtx);
331430400f03SJulian Elischer }
3315069154d5SJulian Elischer 
3316069154d5SJulian Elischer static int
3317069154d5SJulian Elischer sysctl_debug_ng_dump_items(SYSCTL_HANDLER_ARGS)
3318069154d5SJulian Elischer {
3319069154d5SJulian Elischer 	int error;
3320069154d5SJulian Elischer 	int val;
3321069154d5SJulian Elischer 	int i;
3322069154d5SJulian Elischer 
3323069154d5SJulian Elischer 	val = allocated;
3324069154d5SJulian Elischer 	i = 1;
3325041b706bSDavid Malone 	error = sysctl_handle_int(oidp, &val, 0, req);
33266b795970SJulian Elischer 	if (error != 0 || req->newptr == NULL)
33276b795970SJulian Elischer 		return (error);
33286b795970SJulian Elischer 	if (val == 42) {
332930400f03SJulian Elischer 		ng_dumpitems();
333030400f03SJulian Elischer 		ng_dumpnodes();
333130400f03SJulian Elischer 		ng_dumphooks();
3332069154d5SJulian Elischer 	}
33336b795970SJulian Elischer 	return (0);
3334069154d5SJulian Elischer }
3335069154d5SJulian Elischer 
33366b795970SJulian Elischer SYSCTL_PROC(_debug, OID_AUTO, ng_dump_items, CTLTYPE_INT | CTLFLAG_RW,
33376b795970SJulian Elischer     0, sizeof(int), sysctl_debug_ng_dump_items, "I", "Number of allocated items");
333830400f03SJulian Elischer #endif	/* NETGRAPH_DEBUG */
3339069154d5SJulian Elischer 
3340069154d5SJulian Elischer 
3341069154d5SJulian Elischer /***********************************************************************
3342069154d5SJulian Elischer * Worklist routines
3343069154d5SJulian Elischer **********************************************************************/
3344069154d5SJulian Elischer /* NETISR thread enters here */
3345069154d5SJulian Elischer /*
3346069154d5SJulian Elischer  * Pick a node off the list of nodes with work,
3347069154d5SJulian Elischer  * try get an item to process off it.
3348069154d5SJulian Elischer  * If there are no more, remove the node from the list.
3349069154d5SJulian Elischer  */
3350069154d5SJulian Elischer static void
3351069154d5SJulian Elischer ngintr(void)
3352069154d5SJulian Elischer {
3353069154d5SJulian Elischer 	item_p item;
3354069154d5SJulian Elischer 	node_p  node = NULL;
3355069154d5SJulian Elischer 
3356069154d5SJulian Elischer 	for (;;) {
33572c8dda8dSWojciech A. Koszek 		NG_WORKLIST_LOCK();
3358069154d5SJulian Elischer 		node = TAILQ_FIRST(&ng_worklist);
3359069154d5SJulian Elischer 		if (!node) {
33602c8dda8dSWojciech A. Koszek 			NG_WORKLIST_UNLOCK();
3361069154d5SJulian Elischer 			break;
3362069154d5SJulian Elischer 		}
3363be4252b3SJulian Elischer 		node->nd_flags &= ~NGF_WORKQ;
336430400f03SJulian Elischer 		TAILQ_REMOVE(&ng_worklist, node, nd_work);
33652c8dda8dSWojciech A. Koszek 		NG_WORKLIST_UNLOCK();
33662955ee18SGleb Smirnoff 		CTR3(KTR_NET, "%20s: node [%x] (%p) taken off worklist",
33672955ee18SGleb Smirnoff 		    __func__, node->nd_ID, node);
3368069154d5SJulian Elischer 		/*
3369069154d5SJulian Elischer 		 * We have the node. We also take over the reference
3370069154d5SJulian Elischer 		 * that the list had on it.
3371069154d5SJulian Elischer 		 * Now process as much as you can, until it won't
3372069154d5SJulian Elischer 		 * let you have another item off the queue.
3373069154d5SJulian Elischer 		 * All this time, keep the reference
3374069154d5SJulian Elischer 		 * that lets us be sure that the node still exists.
3375069154d5SJulian Elischer 		 * Let the reference go at the last minute.
3376069154d5SJulian Elischer 		 */
3377069154d5SJulian Elischer 		for (;;) {
3378714fb865SGleb Smirnoff 			int rw;
3379714fb865SGleb Smirnoff 
33802c8dda8dSWojciech A. Koszek 			NG_QUEUE_LOCK(&node->nd_input_queue);
3381714fb865SGleb Smirnoff 			item = ng_dequeue(&node->nd_input_queue, &rw);
3382069154d5SJulian Elischer 			if (item == NULL) {
33832c8dda8dSWojciech A. Koszek 				NG_QUEUE_UNLOCK(&node->nd_input_queue);
3384069154d5SJulian Elischer 				break; /* go look for another node */
3385069154d5SJulian Elischer 			} else {
33862c8dda8dSWojciech A. Koszek 				NG_QUEUE_UNLOCK(&node->nd_input_queue);
33875951069aSJulian Elischer 				NGI_GET_NODE(item, node); /* zaps stored node */
3388714fb865SGleb Smirnoff 				ng_apply_item(node, item, rw);
33895951069aSJulian Elischer 				NG_NODE_UNREF(node);
3390069154d5SJulian Elischer 			}
3391069154d5SJulian Elischer 		}
3392a96dcd84SJulian Elischer 		NG_NODE_UNREF(node);
3393069154d5SJulian Elischer 	}
3394069154d5SJulian Elischer }
3395069154d5SJulian Elischer 
3396069154d5SJulian Elischer static void
3397069154d5SJulian Elischer ng_worklist_remove(node_p node)
3398069154d5SJulian Elischer {
33993b33fbe7SGleb Smirnoff 	mtx_assert(&node->nd_input_queue.q_mtx, MA_OWNED);
34003b33fbe7SGleb Smirnoff 
34012c8dda8dSWojciech A. Koszek 	NG_WORKLIST_LOCK();
3402be4252b3SJulian Elischer 	if (node->nd_flags & NGF_WORKQ) {
3403be4252b3SJulian Elischer 		node->nd_flags &= ~NGF_WORKQ;
340433338e73SJulian Elischer 		TAILQ_REMOVE(&ng_worklist, node, nd_work);
34052c8dda8dSWojciech A. Koszek 		NG_WORKLIST_UNLOCK();
340633338e73SJulian Elischer 		NG_NODE_UNREF(node);
34072955ee18SGleb Smirnoff 		CTR3(KTR_NET, "%20s: node [%x] (%p) removed from worklist",
34082955ee18SGleb Smirnoff 		    __func__, node->nd_ID, node);
340933338e73SJulian Elischer 	} else {
34102c8dda8dSWojciech A. Koszek 		NG_WORKLIST_UNLOCK();
3411069154d5SJulian Elischer 	}
341233338e73SJulian Elischer }
3413069154d5SJulian Elischer 
341433338e73SJulian Elischer /*
341533338e73SJulian Elischer  * XXX
341633338e73SJulian Elischer  * It's posible that a debugging NG_NODE_REF may need
341733338e73SJulian Elischer  * to be outside the mutex zone
341833338e73SJulian Elischer  */
3419069154d5SJulian Elischer static void
3420069154d5SJulian Elischer ng_setisr(node_p node)
3421069154d5SJulian Elischer {
3422f912c0f7SGleb Smirnoff 
34235bc15201SGleb Smirnoff 	mtx_assert(&node->nd_input_queue.q_mtx, MA_OWNED);
3424f912c0f7SGleb Smirnoff 
3425be4252b3SJulian Elischer 	if ((node->nd_flags & NGF_WORKQ) == 0) {
3426069154d5SJulian Elischer 		/*
3427069154d5SJulian Elischer 		 * If we are not already on the work queue,
3428069154d5SJulian Elischer 		 * then put us on.
3429069154d5SJulian Elischer 		 */
3430be4252b3SJulian Elischer 		node->nd_flags |= NGF_WORKQ;
34312c8dda8dSWojciech A. Koszek 		NG_WORKLIST_LOCK();
343230400f03SJulian Elischer 		TAILQ_INSERT_TAIL(&ng_worklist, node, nd_work);
34332c8dda8dSWojciech A. Koszek 		NG_WORKLIST_UNLOCK();
343433338e73SJulian Elischer 		NG_NODE_REF(node); /* XXX fafe in mutex? */
34352955ee18SGleb Smirnoff 		CTR3(KTR_NET, "%20s: node [%x] (%p) put on worklist", __func__,
34362955ee18SGleb Smirnoff 		    node->nd_ID, node);
34373b33fbe7SGleb Smirnoff 	} else
34382955ee18SGleb Smirnoff 		CTR3(KTR_NET, "%20s: node [%x] (%p) already on worklist",
34392955ee18SGleb Smirnoff 		    __func__, node->nd_ID, node);
3440069154d5SJulian Elischer 	schednetisr(NETISR_NETGRAPH);
3441069154d5SJulian Elischer }
3442069154d5SJulian Elischer 
3443069154d5SJulian Elischer 
3444069154d5SJulian Elischer /***********************************************************************
3445069154d5SJulian Elischer * Externally useable functions to set up a queue item ready for sending
3446069154d5SJulian Elischer ***********************************************************************/
3447069154d5SJulian Elischer 
344830400f03SJulian Elischer #ifdef	NETGRAPH_DEBUG
344930400f03SJulian Elischer #define	ITEM_DEBUG_CHECKS						\
34504cf49a43SJulian Elischer 	do {								\
34511acb27c6SJulian Elischer 		if (NGI_NODE(item) ) {					\
3452069154d5SJulian Elischer 			printf("item already has node");		\
34533de213ccSRobert Watson 			kdb_enter(KDB_WHY_NETGRAPH, "has node");	\
34541acb27c6SJulian Elischer 			NGI_CLR_NODE(item);				\
3455069154d5SJulian Elischer 		}							\
34561acb27c6SJulian Elischer 		if (NGI_HOOK(item) ) {					\
3457069154d5SJulian Elischer 			printf("item already has hook");		\
34583de213ccSRobert Watson 			kdb_enter(KDB_WHY_NETGRAPH, "has hook");	\
34591acb27c6SJulian Elischer 			NGI_CLR_HOOK(item);				\
3460069154d5SJulian Elischer 		}							\
3461069154d5SJulian Elischer 	} while (0)
3462069154d5SJulian Elischer #else
346330400f03SJulian Elischer #define ITEM_DEBUG_CHECKS
3464069154d5SJulian Elischer #endif
3465069154d5SJulian Elischer 
3466069154d5SJulian Elischer /*
34678ed370fdSJulian Elischer  * Put mbuf into the item.
3468069154d5SJulian Elischer  * Hook and node references will be removed when the item is dequeued.
3469069154d5SJulian Elischer  * (or equivalent)
3470069154d5SJulian Elischer  * (XXX) Unsafe because no reference held by peer on remote node.
3471069154d5SJulian Elischer  * remote node might go away in this timescale.
3472069154d5SJulian Elischer  * We know the hooks can't go away because that would require getting
3473069154d5SJulian Elischer  * a writer item on both nodes and we must have at least a  reader
34744be59335SGleb Smirnoff  * here to be able to do this.
3475069154d5SJulian Elischer  * Note that the hook loaded is the REMOTE hook.
3476069154d5SJulian Elischer  *
3477069154d5SJulian Elischer  * This is possibly in the critical path for new data.
3478069154d5SJulian Elischer  */
3479069154d5SJulian Elischer item_p
348042282202SGleb Smirnoff ng_package_data(struct mbuf *m, int flags)
3481069154d5SJulian Elischer {
3482069154d5SJulian Elischer 	item_p item;
3483069154d5SJulian Elischer 
3484ed75521fSAlexander Motin 	if (atomic_fetchadd_int(&useddata, 1) >= maxdata) {
3485ed75521fSAlexander Motin 		atomic_subtract_int(&useddata, 1);
3486ed75521fSAlexander Motin 		NG_FREE_M(m);
3487ed75521fSAlexander Motin 		return (NULL);
3488ed75521fSAlexander Motin 	}
348942282202SGleb Smirnoff 	if ((item = ng_getqblk(flags)) == NULL) {
3490069154d5SJulian Elischer 		NG_FREE_M(m);
3491069154d5SJulian Elischer 		return (NULL);
3492069154d5SJulian Elischer 	}
349330400f03SJulian Elischer 	ITEM_DEBUG_CHECKS;
34946f683eeeSGleb Smirnoff 	item->el_flags = NGQF_DATA | NGQF_READER;
3495069154d5SJulian Elischer 	NGI_M(item) = m;
3496069154d5SJulian Elischer 	return (item);
3497069154d5SJulian Elischer }
3498069154d5SJulian Elischer 
3499069154d5SJulian Elischer /*
3500069154d5SJulian Elischer  * Allocate a queue item and put items into it..
3501069154d5SJulian Elischer  * Evaluate the address as this will be needed to queue it and
3502069154d5SJulian Elischer  * to work out what some of the fields should be.
3503069154d5SJulian Elischer  * Hook and node references will be removed when the item is dequeued.
3504069154d5SJulian Elischer  * (or equivalent)
3505069154d5SJulian Elischer  */
3506069154d5SJulian Elischer item_p
350742282202SGleb Smirnoff ng_package_msg(struct ng_mesg *msg, int flags)
3508069154d5SJulian Elischer {
3509069154d5SJulian Elischer 	item_p item;
3510069154d5SJulian Elischer 
351142282202SGleb Smirnoff 	if ((item = ng_getqblk(flags)) == NULL) {
3512069154d5SJulian Elischer 		NG_FREE_MSG(msg);
3513069154d5SJulian Elischer 		return (NULL);
3514069154d5SJulian Elischer 	}
351530400f03SJulian Elischer 	ITEM_DEBUG_CHECKS;
35166f683eeeSGleb Smirnoff 	/* Messages items count as writers unless explicitly exempted. */
35176f683eeeSGleb Smirnoff 	if (msg->header.cmd & NGM_READONLY)
35186f683eeeSGleb Smirnoff 		item->el_flags = NGQF_MESG | NGQF_READER;
35196f683eeeSGleb Smirnoff 	else
35206f683eeeSGleb Smirnoff 		item->el_flags = NGQF_MESG | NGQF_WRITER;
3521069154d5SJulian Elischer 	/*
3522069154d5SJulian Elischer 	 * Set the current lasthook into the queue item
3523069154d5SJulian Elischer 	 */
3524069154d5SJulian Elischer 	NGI_MSG(item) = msg;
3525facfd889SArchie Cobbs 	NGI_RETADDR(item) = 0;
3526069154d5SJulian Elischer 	return (item);
3527069154d5SJulian Elischer }
3528069154d5SJulian Elischer 
3529069154d5SJulian Elischer 
3530069154d5SJulian Elischer 
35311acb27c6SJulian Elischer #define SET_RETADDR(item, here, retaddr)				\
35326b795970SJulian Elischer 	do {	/* Data or fn items don't have retaddrs */		\
35336b795970SJulian Elischer 		if ((item->el_flags & NGQF_TYPE) == NGQF_MESG) {	\
3534069154d5SJulian Elischer 			if (retaddr) {					\
3535069154d5SJulian Elischer 				NGI_RETADDR(item) = retaddr;		\
35364cf49a43SJulian Elischer 			} else {					\
3537069154d5SJulian Elischer 				/*					\
3538069154d5SJulian Elischer 				 * The old return address should be ok.	\
3539069154d5SJulian Elischer 				 * If there isn't one, use the address	\
3540069154d5SJulian Elischer 				 * here.				\
3541069154d5SJulian Elischer 				 */					\
3542069154d5SJulian Elischer 				if (NGI_RETADDR(item) == 0) {		\
3543069154d5SJulian Elischer 					NGI_RETADDR(item)		\
3544069154d5SJulian Elischer 						= ng_node2ID(here);	\
3545069154d5SJulian Elischer 				}					\
3546069154d5SJulian Elischer 			}						\
35474cf49a43SJulian Elischer 		}							\
35484cf49a43SJulian Elischer 	} while (0)
35494cf49a43SJulian Elischer 
35504cf49a43SJulian Elischer int
3551069154d5SJulian Elischer ng_address_hook(node_p here, item_p item, hook_p hook, ng_ID_t retaddr)
35524cf49a43SJulian Elischer {
35531acb27c6SJulian Elischer 	hook_p peer;
35541acb27c6SJulian Elischer 	node_p peernode;
355530400f03SJulian Elischer 	ITEM_DEBUG_CHECKS;
3556069154d5SJulian Elischer 	/*
3557069154d5SJulian Elischer 	 * Quick sanity check..
355830400f03SJulian Elischer 	 * Since a hook holds a reference on it's node, once we know
355930400f03SJulian Elischer 	 * that the peer is still connected (even if invalid,) we know
356030400f03SJulian Elischer 	 * that the peer node is present, though maybe invalid.
3561069154d5SJulian Elischer 	 */
3562069154d5SJulian Elischer 	if ((hook == NULL)
356330400f03SJulian Elischer 	|| NG_HOOK_NOT_VALID(hook)
356430400f03SJulian Elischer 	|| NG_HOOK_NOT_VALID(NG_HOOK_PEER(hook))
356530400f03SJulian Elischer 	|| NG_NODE_NOT_VALID(NG_PEER_NODE(hook))) {
3566069154d5SJulian Elischer 		NG_FREE_ITEM(item);
35676b795970SJulian Elischer 		TRAP_ERROR();
3568e08d3e3cSJulian Elischer 		return (ENETDOWN);
35694cf49a43SJulian Elischer 	}
35704cf49a43SJulian Elischer 
35714cf49a43SJulian Elischer 	/*
3572069154d5SJulian Elischer 	 * Transfer our interest to the other (peer) end.
35734cf49a43SJulian Elischer 	 */
35741acb27c6SJulian Elischer 	peer = NG_HOOK_PEER(hook);
35751acb27c6SJulian Elischer 	NG_HOOK_REF(peer);
35761acb27c6SJulian Elischer 	NGI_SET_HOOK(item, peer);
35771acb27c6SJulian Elischer 	peernode = NG_PEER_NODE(hook);
35781acb27c6SJulian Elischer 	NG_NODE_REF(peernode);
35791acb27c6SJulian Elischer 	NGI_SET_NODE(item, peernode);
35808b68f82fSJulian Elischer 	SET_RETADDR(item, here, retaddr);
3581069154d5SJulian Elischer 	return (0);
3582069154d5SJulian Elischer }
3583069154d5SJulian Elischer 
35844cf49a43SJulian Elischer int
3585069154d5SJulian Elischer ng_address_path(node_p here, item_p item, char *address, ng_ID_t retaddr)
35864cf49a43SJulian Elischer {
35874cf49a43SJulian Elischer 	node_p	dest = NULL;
3588069154d5SJulian Elischer 	hook_p	hook = NULL;
35894cf49a43SJulian Elischer 	int	error;
3590069154d5SJulian Elischer 
359130400f03SJulian Elischer 	ITEM_DEBUG_CHECKS;
3592069154d5SJulian Elischer 	/*
3593069154d5SJulian Elischer 	 * Note that ng_path2noderef increments the reference count
3594069154d5SJulian Elischer 	 * on the node for us if it finds one. So we don't have to.
3595069154d5SJulian Elischer 	 */
3596069154d5SJulian Elischer 	error = ng_path2noderef(here, address, &dest, &hook);
3597069154d5SJulian Elischer 	if (error) {
3598069154d5SJulian Elischer 		NG_FREE_ITEM(item);
359930400f03SJulian Elischer 		return (error);
3600069154d5SJulian Elischer 	}
36011acb27c6SJulian Elischer 	NGI_SET_NODE(item, dest);
36021acb27c6SJulian Elischer 	if ( hook) {
360330400f03SJulian Elischer 		NG_HOOK_REF(hook);	/* don't let it go while on the queue */
36041acb27c6SJulian Elischer 		NGI_SET_HOOK(item, hook);
36051acb27c6SJulian Elischer 	}
36061acb27c6SJulian Elischer 	SET_RETADDR(item, here, retaddr);
3607069154d5SJulian Elischer 	return (0);
3608069154d5SJulian Elischer }
3609069154d5SJulian Elischer 
3610069154d5SJulian Elischer int
3611069154d5SJulian Elischer ng_address_ID(node_p here, item_p item, ng_ID_t ID, ng_ID_t retaddr)
3612069154d5SJulian Elischer {
3613069154d5SJulian Elischer 	node_p dest;
3614069154d5SJulian Elischer 
361530400f03SJulian Elischer 	ITEM_DEBUG_CHECKS;
3616069154d5SJulian Elischer 	/*
3617069154d5SJulian Elischer 	 * Find the target node.
3618069154d5SJulian Elischer 	 */
3619069154d5SJulian Elischer 	dest = ng_ID2noderef(ID); /* GETS REFERENCE! */
3620069154d5SJulian Elischer 	if (dest == NULL) {
3621069154d5SJulian Elischer 		NG_FREE_ITEM(item);
36226b795970SJulian Elischer 		TRAP_ERROR();
3623069154d5SJulian Elischer 		return(EINVAL);
3624069154d5SJulian Elischer 	}
3625069154d5SJulian Elischer 	/* Fill out the contents */
36261acb27c6SJulian Elischer 	NGI_SET_NODE(item, dest);
36271acb27c6SJulian Elischer 	NGI_CLR_HOOK(item);
36281acb27c6SJulian Elischer 	SET_RETADDR(item, here, retaddr);
3629069154d5SJulian Elischer 	return (0);
3630069154d5SJulian Elischer }
3631069154d5SJulian Elischer 
3632069154d5SJulian Elischer /*
3633069154d5SJulian Elischer  * special case to send a message to self (e.g. destroy node)
3634069154d5SJulian Elischer  * Possibly indicate an arrival hook too.
3635069154d5SJulian Elischer  * Useful for removing that hook :-)
3636069154d5SJulian Elischer  */
3637069154d5SJulian Elischer item_p
3638069154d5SJulian Elischer ng_package_msg_self(node_p here, hook_p hook, struct ng_mesg *msg)
3639069154d5SJulian Elischer {
3640069154d5SJulian Elischer 	item_p item;
36414cf49a43SJulian Elischer 
3642859a4d16SJulian Elischer 	/*
3643859a4d16SJulian Elischer 	 * Find the target node.
3644859a4d16SJulian Elischer 	 * If there is a HOOK argument, then use that in preference
3645859a4d16SJulian Elischer 	 * to the address.
3646859a4d16SJulian Elischer 	 */
364742282202SGleb Smirnoff 	if ((item = ng_getqblk(NG_NOFLAGS)) == NULL) {
3648069154d5SJulian Elischer 		NG_FREE_MSG(msg);
3649069154d5SJulian Elischer 		return (NULL);
36504cf49a43SJulian Elischer 	}
36514cf49a43SJulian Elischer 
36524cf49a43SJulian Elischer 	/* Fill out the contents */
36536f683eeeSGleb Smirnoff 	item->el_flags = NGQF_MESG | NGQF_WRITER;
365430400f03SJulian Elischer 	NG_NODE_REF(here);
36551acb27c6SJulian Elischer 	NGI_SET_NODE(item, here);
36561acb27c6SJulian Elischer 	if (hook) {
365730400f03SJulian Elischer 		NG_HOOK_REF(hook);
36581acb27c6SJulian Elischer 		NGI_SET_HOOK(item, hook);
36591acb27c6SJulian Elischer 	}
3660069154d5SJulian Elischer 	NGI_MSG(item) = msg;
3661069154d5SJulian Elischer 	NGI_RETADDR(item) = ng_node2ID(here);
3662069154d5SJulian Elischer 	return (item);
36634cf49a43SJulian Elischer }
36644cf49a43SJulian Elischer 
3665e088dd4cSAlexander Motin /*
3666e088dd4cSAlexander Motin  * Send ng_item_fn function call to the specified node.
3667e088dd4cSAlexander Motin  */
3668e088dd4cSAlexander Motin 
366942282202SGleb Smirnoff int
3670b332b91fSGleb Smirnoff ng_send_fn(node_p node, hook_p hook, ng_item_fn *fn, void * arg1, int arg2)
3671b332b91fSGleb Smirnoff {
3672b332b91fSGleb Smirnoff 
3673b332b91fSGleb Smirnoff 	return ng_send_fn1(node, hook, fn, arg1, arg2, NG_NOFLAGS);
3674b332b91fSGleb Smirnoff }
3675b332b91fSGleb Smirnoff 
3676b332b91fSGleb Smirnoff int
3677aacdb114SGleb Smirnoff ng_send_fn1(node_p node, hook_p hook, ng_item_fn *fn, void * arg1, int arg2,
367842282202SGleb Smirnoff 	int flags)
36796b795970SJulian Elischer {
36806b795970SJulian Elischer 	item_p item;
36816b795970SJulian Elischer 
368242282202SGleb Smirnoff 	if ((item = ng_getqblk(flags)) == NULL) {
36836b795970SJulian Elischer 		return (ENOMEM);
36846b795970SJulian Elischer 	}
36856b795970SJulian Elischer 	item->el_flags = NGQF_FN | NGQF_WRITER;
3686a96dcd84SJulian Elischer 	NG_NODE_REF(node); /* and one for the item */
36871acb27c6SJulian Elischer 	NGI_SET_NODE(item, node);
36881acb27c6SJulian Elischer 	if (hook) {
36896b795970SJulian Elischer 		NG_HOOK_REF(hook);
36901acb27c6SJulian Elischer 		NGI_SET_HOOK(item, hook);
36916b795970SJulian Elischer 	}
36926b795970SJulian Elischer 	NGI_FN(item) = fn;
36936b795970SJulian Elischer 	NGI_ARG1(item) = arg1;
36946b795970SJulian Elischer 	NGI_ARG2(item) = arg2;
369542282202SGleb Smirnoff 	return(ng_snd_item(item, flags));
36966b795970SJulian Elischer }
36976b795970SJulian Elischer 
36984cf49a43SJulian Elischer /*
3699b332b91fSGleb Smirnoff  * Send ng_item_fn2 function call to the specified node.
3700b332b91fSGleb Smirnoff  *
3701b332b91fSGleb Smirnoff  * If an optional pitem parameter is supplied, its apply
3702b332b91fSGleb Smirnoff  * callback will be copied to the new item. If also NG_REUSE_ITEM
3703b332b91fSGleb Smirnoff  * flag is set, no new item will be allocated, but pitem will
3704b332b91fSGleb Smirnoff  * be used.
3705e088dd4cSAlexander Motin  */
3706e088dd4cSAlexander Motin int
3707b332b91fSGleb Smirnoff ng_send_fn2(node_p node, hook_p hook, item_p pitem, ng_item_fn2 *fn, void *arg1,
3708b332b91fSGleb Smirnoff 	int arg2, int flags)
3709e088dd4cSAlexander Motin {
3710e088dd4cSAlexander Motin 	item_p item;
3711e088dd4cSAlexander Motin 
3712b332b91fSGleb Smirnoff 	KASSERT((pitem != NULL || (flags & NG_REUSE_ITEM) == 0),
3713b332b91fSGleb Smirnoff 	    ("%s: NG_REUSE_ITEM but no pitem", __func__));
3714e088dd4cSAlexander Motin 
3715e088dd4cSAlexander Motin 	/*
3716b332b91fSGleb Smirnoff 	 * Allocate a new item if no supplied or
3717b332b91fSGleb Smirnoff 	 * if we can't use supplied one.
3718e088dd4cSAlexander Motin 	 */
3719b332b91fSGleb Smirnoff 	if (pitem == NULL || (flags & NG_REUSE_ITEM) == 0) {
3720b332b91fSGleb Smirnoff 		if ((item = ng_getqblk(flags)) == NULL)
3721e088dd4cSAlexander Motin 			return (ENOMEM);
3722ed75521fSAlexander Motin 	} else {
3723ed75521fSAlexander Motin 		if ((pitem->el_flags & NGQF_TYPE) == NGQF_DATA)
3724ed75521fSAlexander Motin 			atomic_subtract_int(&useddata, 1);
3725b332b91fSGleb Smirnoff 		item = pitem;
3726ed75521fSAlexander Motin 	}
3727b332b91fSGleb Smirnoff 
3728e088dd4cSAlexander Motin 	item->el_flags = NGQF_FN2 | NGQF_WRITER;
3729e088dd4cSAlexander Motin 	NG_NODE_REF(node); /* and one for the item */
3730e088dd4cSAlexander Motin 	NGI_SET_NODE(item, node);
3731e088dd4cSAlexander Motin 	if (hook) {
3732e088dd4cSAlexander Motin 		NG_HOOK_REF(hook);
3733e088dd4cSAlexander Motin 		NGI_SET_HOOK(item, hook);
3734e088dd4cSAlexander Motin 	}
3735e088dd4cSAlexander Motin 	NGI_FN2(item) = fn;
3736e088dd4cSAlexander Motin 	NGI_ARG1(item) = arg1;
3737e088dd4cSAlexander Motin 	NGI_ARG2(item) = arg2;
3738b332b91fSGleb Smirnoff 	if (pitem != NULL && (flags & NG_REUSE_ITEM) == 0)
3739e088dd4cSAlexander Motin 		item->apply = pitem->apply;
3740e088dd4cSAlexander Motin 	return(ng_snd_item(item, flags));
3741e088dd4cSAlexander Motin }
3742e088dd4cSAlexander Motin 
3743e088dd4cSAlexander Motin /*
3744d2ca21a9SJulian Elischer  * Official timeout routines for Netgraph nodes.
3745d2ca21a9SJulian Elischer  */
3746d2ca21a9SJulian Elischer static void
37471fbb36ffSGleb Smirnoff ng_callout_trampoline(void *arg)
3748d2ca21a9SJulian Elischer {
3749d2ca21a9SJulian Elischer 	item_p item = arg;
3750d2ca21a9SJulian Elischer 
3751d2ca21a9SJulian Elischer 	ng_snd_item(item, 0);
3752d2ca21a9SJulian Elischer }
3753d2ca21a9SJulian Elischer 
3754d2ca21a9SJulian Elischer 
375530bef41bSGleb Smirnoff int
3756f9d9e1b4SGleb Smirnoff ng_callout(struct callout *c, node_p node, hook_p hook, int ticks,
3757d2ca21a9SJulian Elischer     ng_item_fn *fn, void * arg1, int arg2)
3758d2ca21a9SJulian Elischer {
37591bf8e0faSGleb Smirnoff 	item_p item, oitem;
3760d2ca21a9SJulian Elischer 
376142282202SGleb Smirnoff 	if ((item = ng_getqblk(NG_NOFLAGS)) == NULL)
376230bef41bSGleb Smirnoff 		return (ENOMEM);
376330bef41bSGleb Smirnoff 
3764d2ca21a9SJulian Elischer 	item->el_flags = NGQF_FN | NGQF_WRITER;
3765d2ca21a9SJulian Elischer 	NG_NODE_REF(node);		/* and one for the item */
3766d2ca21a9SJulian Elischer 	NGI_SET_NODE(item, node);
3767d2ca21a9SJulian Elischer 	if (hook) {
3768d2ca21a9SJulian Elischer 		NG_HOOK_REF(hook);
3769d2ca21a9SJulian Elischer 		NGI_SET_HOOK(item, hook);
3770d2ca21a9SJulian Elischer 	}
3771d2ca21a9SJulian Elischer 	NGI_FN(item) = fn;
3772d2ca21a9SJulian Elischer 	NGI_ARG1(item) = arg1;
3773d2ca21a9SJulian Elischer 	NGI_ARG2(item) = arg2;
37741bf8e0faSGleb Smirnoff 	oitem = c->c_arg;
37751bf8e0faSGleb Smirnoff 	if (callout_reset(c, ticks, &ng_callout_trampoline, item) == 1 &&
37761bf8e0faSGleb Smirnoff 	    oitem != NULL)
37771bf8e0faSGleb Smirnoff 		NG_FREE_ITEM(oitem);
377830bef41bSGleb Smirnoff 	return (0);
3779d2ca21a9SJulian Elischer }
3780d2ca21a9SJulian Elischer 
3781d2ca21a9SJulian Elischer /* A special modified version of untimeout() */
3782d2ca21a9SJulian Elischer int
3783f9d9e1b4SGleb Smirnoff ng_uncallout(struct callout *c, node_p node)
3784d2ca21a9SJulian Elischer {
3785d2ca21a9SJulian Elischer 	item_p item;
378630bef41bSGleb Smirnoff 	int rval;
3787d2ca21a9SJulian Elischer 
378803b25f5dSGleb Smirnoff 	KASSERT(c != NULL, ("ng_uncallout: NULL callout"));
378903b25f5dSGleb Smirnoff 	KASSERT(node != NULL, ("ng_uncallout: NULL node"));
379003b25f5dSGleb Smirnoff 
37913eadb26dSGleb Smirnoff 	rval = callout_stop(c);
379230bef41bSGleb Smirnoff 	item = c->c_arg;
379330bef41bSGleb Smirnoff 	/* Do an extra check */
37941fbb36ffSGleb Smirnoff 	if ((rval > 0) && (c->c_func == &ng_callout_trampoline) &&
379530bef41bSGleb Smirnoff 	    (NGI_NODE(item) == node)) {
3796d2ca21a9SJulian Elischer 		/*
3797d2ca21a9SJulian Elischer 		 * We successfully removed it from the queue before it ran
3798d2ca21a9SJulian Elischer 		 * So now we need to unreference everything that was
3799d2ca21a9SJulian Elischer 		 * given extra references. (NG_FREE_ITEM does this).
3800d2ca21a9SJulian Elischer 		 */
3801d2ca21a9SJulian Elischer 		NG_FREE_ITEM(item);
3802d2ca21a9SJulian Elischer 	}
38031bf8e0faSGleb Smirnoff 	c->c_arg = NULL;
380430bef41bSGleb Smirnoff 
380530bef41bSGleb Smirnoff 	return (rval);
3806d2ca21a9SJulian Elischer }
3807d2ca21a9SJulian Elischer 
3808d2ca21a9SJulian Elischer /*
3809069154d5SJulian Elischer  * Set the address, if none given, give the node here.
38104cf49a43SJulian Elischer  */
3811069154d5SJulian Elischer void
3812069154d5SJulian Elischer ng_replace_retaddr(node_p here, item_p item, ng_ID_t retaddr)
38134cf49a43SJulian Elischer {
3814069154d5SJulian Elischer 	if (retaddr) {
3815069154d5SJulian Elischer 		NGI_RETADDR(item) = retaddr;
3816069154d5SJulian Elischer 	} else {
3817069154d5SJulian Elischer 		/*
3818069154d5SJulian Elischer 		 * The old return address should be ok.
3819069154d5SJulian Elischer 		 * If there isn't one, use the address here.
3820069154d5SJulian Elischer 		 */
3821069154d5SJulian Elischer 		NGI_RETADDR(item) = ng_node2ID(here);
3822069154d5SJulian Elischer 	}
3823069154d5SJulian Elischer }
3824069154d5SJulian Elischer 
3825069154d5SJulian Elischer #define TESTING
3826069154d5SJulian Elischer #ifdef TESTING
3827069154d5SJulian Elischer /* just test all the macros */
3828069154d5SJulian Elischer void
3829069154d5SJulian Elischer ng_macro_test(item_p item);
3830069154d5SJulian Elischer void
3831069154d5SJulian Elischer ng_macro_test(item_p item)
3832069154d5SJulian Elischer {
3833069154d5SJulian Elischer 	node_p node = NULL;
3834069154d5SJulian Elischer 	hook_p hook = NULL;
38354cf49a43SJulian Elischer 	struct mbuf *m;
38364cf49a43SJulian Elischer 	struct ng_mesg *msg;
3837069154d5SJulian Elischer 	ng_ID_t retaddr;
3838069154d5SJulian Elischer 	int	error;
38394cf49a43SJulian Elischer 
3840069154d5SJulian Elischer 	NGI_GET_M(item, m);
3841069154d5SJulian Elischer 	NGI_GET_MSG(item, msg);
3842069154d5SJulian Elischer 	retaddr = NGI_RETADDR(item);
38438ed370fdSJulian Elischer 	NG_SEND_DATA(error, hook, m, NULL);
3844069154d5SJulian Elischer 	NG_SEND_DATA_ONLY(error, hook, m);
3845069154d5SJulian Elischer 	NG_FWD_NEW_DATA(error, item, hook, m);
384630400f03SJulian Elischer 	NG_FWD_ITEM_HOOK(error, item, hook);
3847069154d5SJulian Elischer 	NG_SEND_MSG_HOOK(error, node, msg, hook, retaddr);
3848069154d5SJulian Elischer 	NG_SEND_MSG_ID(error, node, msg, retaddr, retaddr);
3849069154d5SJulian Elischer 	NG_SEND_MSG_PATH(error, node, msg, ".:", retaddr);
3850069154d5SJulian Elischer 	NG_FWD_MSG_HOOK(error, node, item, hook, retaddr);
38514cf49a43SJulian Elischer }
3852069154d5SJulian Elischer #endif /* TESTING */
38534cf49a43SJulian Elischer 
3854