xref: /freebsd/sys/netgraph/ng_base.c (revision 45d75e3ac3fb5bf8230ca28dc09b48c6e5ed7a4f)
1c398230bSWarner Losh /*-
24cf49a43SJulian Elischer  * Copyright (c) 1996-1999 Whistle Communications, Inc.
34cf49a43SJulian Elischer  * All rights reserved.
44cf49a43SJulian Elischer  *
54cf49a43SJulian Elischer  * Subject to the following obligations and disclaimer of warranty, use and
64cf49a43SJulian Elischer  * redistribution of this software, in source or object code forms, with or
74cf49a43SJulian Elischer  * without modifications are expressly permitted by Whistle Communications;
84cf49a43SJulian Elischer  * provided, however, that:
94cf49a43SJulian Elischer  * 1. Any and all reproductions of the source or object code must include the
104cf49a43SJulian Elischer  *    copyright notice above and the following disclaimer of warranties; and
114cf49a43SJulian Elischer  * 2. No rights are granted, in any manner or form, to use Whistle
124cf49a43SJulian Elischer  *    Communications, Inc. trademarks, including the mark "WHISTLE
134cf49a43SJulian Elischer  *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
144cf49a43SJulian Elischer  *    such appears in the above copyright notice or in the software.
154cf49a43SJulian Elischer  *
164cf49a43SJulian Elischer  * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
174cf49a43SJulian Elischer  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
184cf49a43SJulian Elischer  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
194cf49a43SJulian Elischer  * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
204cf49a43SJulian Elischer  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
214cf49a43SJulian Elischer  * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
224cf49a43SJulian Elischer  * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
234cf49a43SJulian Elischer  * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
244cf49a43SJulian Elischer  * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
254cf49a43SJulian Elischer  * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
264cf49a43SJulian Elischer  * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
274cf49a43SJulian Elischer  * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
284cf49a43SJulian Elischer  * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
294cf49a43SJulian Elischer  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
304cf49a43SJulian Elischer  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
314cf49a43SJulian Elischer  * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
324cf49a43SJulian Elischer  * OF SUCH DAMAGE.
334cf49a43SJulian Elischer  *
34cc3bbd68SJulian Elischer  * Authors: Julian Elischer <julian@freebsd.org>
35cc3bbd68SJulian Elischer  *          Archie Cobbs <archie@freebsd.org>
364cf49a43SJulian Elischer  *
374cf49a43SJulian Elischer  * $FreeBSD$
384cf49a43SJulian Elischer  * $Whistle: ng_base.c,v 1.39 1999/01/28 23:54:53 julian Exp $
394cf49a43SJulian Elischer  */
404cf49a43SJulian Elischer 
414cf49a43SJulian Elischer /*
424cf49a43SJulian Elischer  * This file implements the base netgraph code.
434cf49a43SJulian Elischer  */
444cf49a43SJulian Elischer 
454cf49a43SJulian Elischer #include <sys/param.h>
46b99a7379SGleb Smirnoff #include <sys/systm.h>
4777a58296SGleb Smirnoff #include <sys/ctype.h>
48687adb70SGleb Smirnoff #include <sys/hash.h>
49f33ca0c9SMarcel Moolenaar #include <sys/kdb.h>
504cf49a43SJulian Elischer #include <sys/kernel.h>
5119afcd98SGleb Smirnoff #include <sys/kthread.h>
523b33fbe7SGleb Smirnoff #include <sys/ktr.h>
53104a9b7eSAlexander Kabaev #include <sys/limits.h>
54c4282b74SGleb Smirnoff #include <sys/lock.h>
554cf49a43SJulian Elischer #include <sys/malloc.h>
564cf49a43SJulian Elischer #include <sys/mbuf.h>
5719afcd98SGleb Smirnoff #include <sys/proc.h>
5835e67a79SGleb Smirnoff #include <sys/epoch.h>
5977a58296SGleb Smirnoff #include <sys/queue.h>
6019afcd98SGleb Smirnoff #include <sys/refcount.h>
6119afcd98SGleb Smirnoff #include <sys/rwlock.h>
6219afcd98SGleb Smirnoff #include <sys/smp.h>
63bfa7e882SJulian Elischer #include <sys/sysctl.h>
6477a58296SGleb Smirnoff #include <sys/syslog.h>
65f2fbb838SAlexander Motin #include <sys/unistd.h>
66394cb30aSAlexander Motin #include <machine/cpu.h>
678ec07310SGleb Smirnoff #include <vm/uma.h>
684cf49a43SJulian Elischer 
694cf49a43SJulian Elischer #include <net/netisr.h>
70eddfbb76SRobert Watson #include <net/vnet.h>
714cf49a43SJulian Elischer 
724cf49a43SJulian Elischer #include <netgraph/ng_message.h>
734cf49a43SJulian Elischer #include <netgraph/netgraph.h>
74f8307e12SArchie Cobbs #include <netgraph/ng_parse.h>
754cf49a43SJulian Elischer 
769d72a7a3SJulian Elischer MODULE_VERSION(netgraph, NG_ABI_VERSION);
7799ff8176SPeter Wemm 
78ac5dd141SGleb Smirnoff /* Mutex to protect topology events. */
79d2fd0788SAlexander V. Chernikov static struct rwlock	ng_topo_lock;
80d2fd0788SAlexander V. Chernikov #define	TOPOLOGY_RLOCK()	rw_rlock(&ng_topo_lock)
81d2fd0788SAlexander V. Chernikov #define	TOPOLOGY_RUNLOCK()	rw_runlock(&ng_topo_lock)
82d2fd0788SAlexander V. Chernikov #define	TOPOLOGY_WLOCK()	rw_wlock(&ng_topo_lock)
83d2fd0788SAlexander V. Chernikov #define	TOPOLOGY_WUNLOCK()	rw_wunlock(&ng_topo_lock)
84d2fd0788SAlexander V. Chernikov #define	TOPOLOGY_NOTOWNED()	rw_assert(&ng_topo_lock, RA_UNLOCKED)
85ac5dd141SGleb Smirnoff 
8630400f03SJulian Elischer #ifdef	NETGRAPH_DEBUG
87cfea3f85SAlexander Motin static struct mtx	ng_nodelist_mtx; /* protects global node/hook lists */
881489164fSGleb Smirnoff static struct mtx	ngq_mtx;	/* protects the queue item list */
8930400f03SJulian Elischer 
9030400f03SJulian Elischer static SLIST_HEAD(, ng_node) ng_allnodes;
9130400f03SJulian Elischer static LIST_HEAD(, ng_node) ng_freenodes; /* in debug, we never free() them */
9230400f03SJulian Elischer static SLIST_HEAD(, ng_hook) ng_allhooks;
9330400f03SJulian Elischer static LIST_HEAD(, ng_hook) ng_freehooks; /* in debug, we never free() them */
9430400f03SJulian Elischer 
9530400f03SJulian Elischer static void ng_dumpitems(void);
9630400f03SJulian Elischer static void ng_dumpnodes(void);
9730400f03SJulian Elischer static void ng_dumphooks(void);
9830400f03SJulian Elischer 
9930400f03SJulian Elischer #endif	/* NETGRAPH_DEBUG */
100954c4772SJulian Elischer /*
101954c4772SJulian Elischer  * DEAD versions of the structures.
102a2098feaSGabor Kovesdan  * In order to avoid races, it is sometimes necessary to point
103954c4772SJulian Elischer  * at SOMETHING even though theoretically, the current entity is
104954c4772SJulian Elischer  * INVALID. Use these to avoid these races.
105954c4772SJulian Elischer  */
106954c4772SJulian Elischer struct ng_type ng_deadtype = {
107954c4772SJulian Elischer 	NG_ABI_VERSION,
108954c4772SJulian Elischer 	"dead",
109954c4772SJulian Elischer 	NULL,	/* modevent */
110954c4772SJulian Elischer 	NULL,	/* constructor */
111954c4772SJulian Elischer 	NULL,	/* rcvmsg */
112954c4772SJulian Elischer 	NULL,	/* shutdown */
113954c4772SJulian Elischer 	NULL,	/* newhook */
114954c4772SJulian Elischer 	NULL,	/* findhook */
115954c4772SJulian Elischer 	NULL,	/* connect */
116954c4772SJulian Elischer 	NULL,	/* rcvdata */
117954c4772SJulian Elischer 	NULL,	/* disconnect */
118954c4772SJulian Elischer 	NULL, 	/* cmdlist */
119954c4772SJulian Elischer };
12030400f03SJulian Elischer 
121954c4772SJulian Elischer struct ng_node ng_deadnode = {
122954c4772SJulian Elischer 	"dead",
123954c4772SJulian Elischer 	&ng_deadtype,
124be4252b3SJulian Elischer 	NGF_INVALID,
125954c4772SJulian Elischer 	0,	/* numhooks */
126954c4772SJulian Elischer 	NULL,	/* private */
127954c4772SJulian Elischer 	0,	/* ID */
12813e403fdSAntoine Brodin 	LIST_HEAD_INITIALIZER(ng_deadnode.nd_hooks),
129954c4772SJulian Elischer 	{},	/* all_nodes list entry */
130954c4772SJulian Elischer 	{},	/* id hashtable list entry */
131954c4772SJulian Elischer 	{	0,
1329852972bSAlexander Motin 		0,
133954c4772SJulian Elischer 		{}, /* should never use! (should hang) */
1349852972bSAlexander Motin 		{}, /* workqueue entry */
1359852972bSAlexander Motin 		STAILQ_HEAD_INITIALIZER(ng_deadnode.nd_input_queue.queue),
136954c4772SJulian Elischer 	},
1379852972bSAlexander Motin 	1,	/* refs */
138a40b7874SMarko Zec 	NULL,	/* vnet */
139954c4772SJulian Elischer #ifdef	NETGRAPH_DEBUG
140954c4772SJulian Elischer 	ND_MAGIC,
141954c4772SJulian Elischer 	__FILE__,
142954c4772SJulian Elischer 	__LINE__,
143954c4772SJulian Elischer 	{NULL}
144954c4772SJulian Elischer #endif	/* NETGRAPH_DEBUG */
145954c4772SJulian Elischer };
146954c4772SJulian Elischer 
147954c4772SJulian Elischer struct ng_hook ng_deadhook = {
148954c4772SJulian Elischer 	"dead",
149954c4772SJulian Elischer 	NULL,		/* private */
150954c4772SJulian Elischer 	HK_INVALID | HK_DEAD,
151e58d779dSGleb Smirnoff 	0,		/* undefined data link type */
152954c4772SJulian Elischer 	&ng_deadhook,	/* Peer is self */
153954c4772SJulian Elischer 	&ng_deadnode,	/* attached to deadnode */
154954c4772SJulian Elischer 	{},		/* hooks list */
155c4b5eea4SJulian Elischer 	NULL,		/* override rcvmsg() */
156c4b5eea4SJulian Elischer 	NULL,		/* override rcvdata() */
1579852972bSAlexander Motin 	1,		/* refs always >= 1 */
158954c4772SJulian Elischer #ifdef	NETGRAPH_DEBUG
159954c4772SJulian Elischer 	HK_MAGIC,
160954c4772SJulian Elischer 	__FILE__,
161954c4772SJulian Elischer 	__LINE__,
162954c4772SJulian Elischer 	{NULL}
163954c4772SJulian Elischer #endif	/* NETGRAPH_DEBUG */
164954c4772SJulian Elischer };
165954c4772SJulian Elischer 
166954c4772SJulian Elischer /*
167954c4772SJulian Elischer  * END DEAD STRUCTURES
168954c4772SJulian Elischer  */
169069154d5SJulian Elischer /* List nodes with unallocated work */
1709852972bSAlexander Motin static STAILQ_HEAD(, ng_node) ng_worklist = STAILQ_HEAD_INITIALIZER(ng_worklist);
171b57a7965SJulian Elischer static struct mtx	ng_worklist_mtx;   /* MUST LOCK NODE FIRST */
1724cf49a43SJulian Elischer 
1734cf49a43SJulian Elischer /* List of installed types */
174069154d5SJulian Elischer static LIST_HEAD(, ng_type) ng_typelist;
175c4282b74SGleb Smirnoff static struct rwlock	ng_typelist_lock;
176c4282b74SGleb Smirnoff #define	TYPELIST_RLOCK()	rw_rlock(&ng_typelist_lock)
177c4282b74SGleb Smirnoff #define	TYPELIST_RUNLOCK()	rw_runlock(&ng_typelist_lock)
178c4282b74SGleb Smirnoff #define	TYPELIST_WLOCK()	rw_wlock(&ng_typelist_lock)
179c4282b74SGleb Smirnoff #define	TYPELIST_WUNLOCK()	rw_wunlock(&ng_typelist_lock)
1804cf49a43SJulian Elischer 
181687adb70SGleb Smirnoff /* Hash related definitions. */
182687adb70SGleb Smirnoff LIST_HEAD(nodehash, ng_node);
1835f901c92SAndrew Turner VNET_DEFINE_STATIC(struct nodehash *, ng_ID_hash);
1845f901c92SAndrew Turner VNET_DEFINE_STATIC(u_long, ng_ID_hmask);
1855f901c92SAndrew Turner VNET_DEFINE_STATIC(u_long, ng_nodes);
1865f901c92SAndrew Turner VNET_DEFINE_STATIC(struct nodehash *, ng_name_hash);
1875f901c92SAndrew Turner VNET_DEFINE_STATIC(u_long, ng_name_hmask);
1885f901c92SAndrew Turner VNET_DEFINE_STATIC(u_long, ng_named_nodes);
1891e77c105SRobert Watson #define	V_ng_ID_hash		VNET(ng_ID_hash)
190687adb70SGleb Smirnoff #define	V_ng_ID_hmask		VNET(ng_ID_hmask)
191687adb70SGleb Smirnoff #define	V_ng_nodes		VNET(ng_nodes)
192687adb70SGleb Smirnoff #define	V_ng_name_hash		VNET(ng_name_hash)
193687adb70SGleb Smirnoff #define	V_ng_name_hmask		VNET(ng_name_hmask)
194687adb70SGleb Smirnoff #define	V_ng_named_nodes	VNET(ng_named_nodes)
195eddfbb76SRobert Watson 
196c4282b74SGleb Smirnoff static struct rwlock	ng_idhash_lock;
197c4282b74SGleb Smirnoff #define	IDHASH_RLOCK()		rw_rlock(&ng_idhash_lock)
198c4282b74SGleb Smirnoff #define	IDHASH_RUNLOCK()	rw_runlock(&ng_idhash_lock)
199c4282b74SGleb Smirnoff #define	IDHASH_WLOCK()		rw_wlock(&ng_idhash_lock)
200c4282b74SGleb Smirnoff #define	IDHASH_WUNLOCK()	rw_wunlock(&ng_idhash_lock)
201c4282b74SGleb Smirnoff 
2020f150d04SJulian Elischer /* Method to find a node.. used twice so do it here */
203687adb70SGleb Smirnoff #define NG_IDHASH_FN(ID) ((ID) % (V_ng_ID_hmask + 1))
2040f150d04SJulian Elischer #define NG_IDHASH_FIND(ID, node)					\
2050f150d04SJulian Elischer 	do { 								\
206c4282b74SGleb Smirnoff 		rw_assert(&ng_idhash_lock, RA_LOCKED);			\
207603724d3SBjoern A. Zeeb 		LIST_FOREACH(node, &V_ng_ID_hash[NG_IDHASH_FN(ID)],	\
2080f150d04SJulian Elischer 						nd_idnodes) {		\
2090f150d04SJulian Elischer 			if (NG_NODE_IS_VALID(node)			\
2100f150d04SJulian Elischer 			&& (NG_NODE_ID(node) == ID)) {			\
2110f150d04SJulian Elischer 				break;					\
2120f150d04SJulian Elischer 			}						\
2130f150d04SJulian Elischer 		}							\
2140f150d04SJulian Elischer 	} while (0)
215069154d5SJulian Elischer 
216c4282b74SGleb Smirnoff static struct rwlock	ng_namehash_lock;
217c4282b74SGleb Smirnoff #define	NAMEHASH_RLOCK()	rw_rlock(&ng_namehash_lock)
218c4282b74SGleb Smirnoff #define	NAMEHASH_RUNLOCK()	rw_runlock(&ng_namehash_lock)
219c4282b74SGleb Smirnoff #define	NAMEHASH_WLOCK()	rw_wlock(&ng_namehash_lock)
220c4282b74SGleb Smirnoff #define	NAMEHASH_WUNLOCK()	rw_wunlock(&ng_namehash_lock)
221dc90cad9SJulian Elischer 
2224cf49a43SJulian Elischer /* Internal functions */
2234cf49a43SJulian Elischer static int	ng_add_hook(node_p node, const char *name, hook_p * hookp);
224069154d5SJulian Elischer static int	ng_generic_msg(node_p here, item_p item, hook_p lasthook);
225dc90cad9SJulian Elischer static ng_ID_t	ng_decodeidname(const char *name);
2264cf49a43SJulian Elischer static int	ngb_mod_event(module_t mod, int event, void *data);
227394cb30aSAlexander Motin static void	ng_worklist_add(node_p node);
228f2fbb838SAlexander Motin static void	ngthread(void *);
22927757487SGleb Smirnoff static int	ng_apply_item(node_p node, item_p item, int rw);
2309852972bSAlexander Motin static void	ng_flush_input_queue(node_p node);
231069154d5SJulian Elischer static node_p	ng_ID2noderef(ng_ID_t ID);
232e088dd4cSAlexander Motin static int	ng_con_nodes(item_p item, node_p node, const char *name,
233e088dd4cSAlexander Motin 		    node_p node2, const char *name2);
234e088dd4cSAlexander Motin static int	ng_con_part2(node_p node, item_p item, hook_p hook);
235e088dd4cSAlexander Motin static int	ng_con_part3(node_p node, item_p item, hook_p hook);
236687adb70SGleb Smirnoff static int	ng_mkpeer(node_p node, const char *name, const char *name2,
237687adb70SGleb Smirnoff 		    char *type);
238687adb70SGleb Smirnoff static void	ng_name_rehash(void);
239687adb70SGleb Smirnoff static void	ng_ID_rehash(void);
240069154d5SJulian Elischer 
2414c9b5910SGleb Smirnoff /* Imported, these used to be externally visible, some may go back. */
242069154d5SJulian Elischer void	ng_destroy_hook(hook_p hook);
243069154d5SJulian Elischer int	ng_path2noderef(node_p here, const char *path,
244069154d5SJulian Elischer 	node_p *dest, hook_p *lasthook);
245069154d5SJulian Elischer int	ng_make_node(const char *type, node_p *nodepp);
246069154d5SJulian Elischer int	ng_path_parse(char *addr, char **node, char **path, char **hook);
2471acb27c6SJulian Elischer void	ng_rmnode(node_p node, hook_p dummy1, void *dummy2, int dummy3);
24830400f03SJulian Elischer void	ng_unname(node_p node);
249069154d5SJulian Elischer 
2504cf49a43SJulian Elischer /* Our own netgraph malloc type */
2514cf49a43SJulian Elischer MALLOC_DEFINE(M_NETGRAPH, "netgraph", "netgraph structures and ctrl messages");
252069154d5SJulian Elischer MALLOC_DEFINE(M_NETGRAPH_MSG, "netgraph_msg", "netgraph name storage");
253d745c852SEd Schouten static MALLOC_DEFINE(M_NETGRAPH_HOOK, "netgraph_hook",
254d745c852SEd Schouten     "netgraph hook structures");
255d745c852SEd Schouten static MALLOC_DEFINE(M_NETGRAPH_NODE, "netgraph_node",
256d745c852SEd Schouten     "netgraph node structures");
257d745c852SEd Schouten static MALLOC_DEFINE(M_NETGRAPH_ITEM, "netgraph_item",
258d745c852SEd Schouten     "netgraph item structures");
259069154d5SJulian Elischer 
260069154d5SJulian Elischer /* Should not be visible outside this file */
26130400f03SJulian Elischer 
26230400f03SJulian Elischer #define _NG_ALLOC_HOOK(hook) \
2631ede983cSDag-Erling Smørgrav 	hook = malloc(sizeof(*hook), M_NETGRAPH_HOOK, M_NOWAIT | M_ZERO)
26430400f03SJulian Elischer #define _NG_ALLOC_NODE(node) \
2651ede983cSDag-Erling Smørgrav 	node = malloc(sizeof(*node), M_NETGRAPH_NODE, M_NOWAIT | M_ZERO)
26630400f03SJulian Elischer 
2672c8dda8dSWojciech A. Koszek #define	NG_QUEUE_LOCK_INIT(n)			\
2684abab3d5SWojciech A. Koszek 	mtx_init(&(n)->q_mtx, "ng_node", NULL, MTX_DEF)
2692c8dda8dSWojciech A. Koszek #define	NG_QUEUE_LOCK(n)			\
2704abab3d5SWojciech A. Koszek 	mtx_lock(&(n)->q_mtx)
2712c8dda8dSWojciech A. Koszek #define	NG_QUEUE_UNLOCK(n)			\
2724abab3d5SWojciech A. Koszek 	mtx_unlock(&(n)->q_mtx)
2732c8dda8dSWojciech A. Koszek #define	NG_WORKLIST_LOCK_INIT()			\
2744abab3d5SWojciech A. Koszek 	mtx_init(&ng_worklist_mtx, "ng_worklist", NULL, MTX_DEF)
2752c8dda8dSWojciech A. Koszek #define	NG_WORKLIST_LOCK()			\
2764abab3d5SWojciech A. Koszek 	mtx_lock(&ng_worklist_mtx)
2772c8dda8dSWojciech A. Koszek #define	NG_WORKLIST_UNLOCK()			\
2784abab3d5SWojciech A. Koszek 	mtx_unlock(&ng_worklist_mtx)
279f2fbb838SAlexander Motin #define	NG_WORKLIST_SLEEP()			\
280f2fbb838SAlexander Motin 	mtx_sleep(&ng_worklist, &ng_worklist_mtx, PI_NET, "sleep", 0)
281f2fbb838SAlexander Motin #define	NG_WORKLIST_WAKEUP()			\
282f2fbb838SAlexander Motin 	wakeup_one(&ng_worklist)
2832c8dda8dSWojciech A. Koszek 
28430400f03SJulian Elischer #ifdef NETGRAPH_DEBUG /*----------------------------------------------*/
28530400f03SJulian Elischer /*
28630400f03SJulian Elischer  * In debug mode:
28730400f03SJulian Elischer  * In an attempt to help track reference count screwups
28830400f03SJulian Elischer  * we do not free objects back to the malloc system, but keep them
28930400f03SJulian Elischer  * in a local cache where we can examine them and keep information safely
29030400f03SJulian Elischer  * after they have been freed.
29130400f03SJulian Elischer  * We use this scheme for nodes and hooks, and to some extent for items.
29230400f03SJulian Elischer  */
29330400f03SJulian Elischer static __inline hook_p
29430400f03SJulian Elischer ng_alloc_hook(void)
29530400f03SJulian Elischer {
29630400f03SJulian Elischer 	hook_p hook;
29730400f03SJulian Elischer 	SLIST_ENTRY(ng_hook) temp;
2989ed346baSBosko Milekic 	mtx_lock(&ng_nodelist_mtx);
29930400f03SJulian Elischer 	hook = LIST_FIRST(&ng_freehooks);
30030400f03SJulian Elischer 	if (hook) {
30130400f03SJulian Elischer 		LIST_REMOVE(hook, hk_hooks);
30230400f03SJulian Elischer 		bcopy(&hook->hk_all, &temp, sizeof(temp));
30330400f03SJulian Elischer 		bzero(hook, sizeof(struct ng_hook));
30430400f03SJulian Elischer 		bcopy(&temp, &hook->hk_all, sizeof(temp));
3059ed346baSBosko Milekic 		mtx_unlock(&ng_nodelist_mtx);
30630400f03SJulian Elischer 		hook->hk_magic = HK_MAGIC;
30730400f03SJulian Elischer 	} else {
3089ed346baSBosko Milekic 		mtx_unlock(&ng_nodelist_mtx);
30930400f03SJulian Elischer 		_NG_ALLOC_HOOK(hook);
31030400f03SJulian Elischer 		if (hook) {
31130400f03SJulian Elischer 			hook->hk_magic = HK_MAGIC;
3129ed346baSBosko Milekic 			mtx_lock(&ng_nodelist_mtx);
31330400f03SJulian Elischer 			SLIST_INSERT_HEAD(&ng_allhooks, hook, hk_all);
3149ed346baSBosko Milekic 			mtx_unlock(&ng_nodelist_mtx);
31530400f03SJulian Elischer 		}
31630400f03SJulian Elischer 	}
31730400f03SJulian Elischer 	return (hook);
31830400f03SJulian Elischer }
31930400f03SJulian Elischer 
32030400f03SJulian Elischer static __inline node_p
32130400f03SJulian Elischer ng_alloc_node(void)
32230400f03SJulian Elischer {
32330400f03SJulian Elischer 	node_p node;
32430400f03SJulian Elischer 	SLIST_ENTRY(ng_node) temp;
3259ed346baSBosko Milekic 	mtx_lock(&ng_nodelist_mtx);
32630400f03SJulian Elischer 	node = LIST_FIRST(&ng_freenodes);
32730400f03SJulian Elischer 	if (node) {
32830400f03SJulian Elischer 		LIST_REMOVE(node, nd_nodes);
32930400f03SJulian Elischer 		bcopy(&node->nd_all, &temp, sizeof(temp));
33030400f03SJulian Elischer 		bzero(node, sizeof(struct ng_node));
33130400f03SJulian Elischer 		bcopy(&temp, &node->nd_all, sizeof(temp));
3329ed346baSBosko Milekic 		mtx_unlock(&ng_nodelist_mtx);
33330400f03SJulian Elischer 		node->nd_magic = ND_MAGIC;
33430400f03SJulian Elischer 	} else {
3359ed346baSBosko Milekic 		mtx_unlock(&ng_nodelist_mtx);
33630400f03SJulian Elischer 		_NG_ALLOC_NODE(node);
33730400f03SJulian Elischer 		if (node) {
33830400f03SJulian Elischer 			node->nd_magic = ND_MAGIC;
3399ed346baSBosko Milekic 			mtx_lock(&ng_nodelist_mtx);
34030400f03SJulian Elischer 			SLIST_INSERT_HEAD(&ng_allnodes, node, nd_all);
3419ed346baSBosko Milekic 			mtx_unlock(&ng_nodelist_mtx);
34230400f03SJulian Elischer 		}
34330400f03SJulian Elischer 	}
34430400f03SJulian Elischer 	return (node);
34530400f03SJulian Elischer }
34630400f03SJulian Elischer 
34730400f03SJulian Elischer #define NG_ALLOC_HOOK(hook) do { (hook) = ng_alloc_hook(); } while (0)
34830400f03SJulian Elischer #define NG_ALLOC_NODE(node) do { (node) = ng_alloc_node(); } while (0)
34930400f03SJulian Elischer 
35030400f03SJulian Elischer #define NG_FREE_HOOK(hook)						\
35130400f03SJulian Elischer 	do {								\
3529ed346baSBosko Milekic 		mtx_lock(&ng_nodelist_mtx);				\
35330400f03SJulian Elischer 		LIST_INSERT_HEAD(&ng_freehooks, hook, hk_hooks);	\
35430400f03SJulian Elischer 		hook->hk_magic = 0;					\
3559ed346baSBosko Milekic 		mtx_unlock(&ng_nodelist_mtx);				\
35630400f03SJulian Elischer 	} while (0)
35730400f03SJulian Elischer 
35830400f03SJulian Elischer #define NG_FREE_NODE(node)						\
35930400f03SJulian Elischer 	do {								\
3609ed346baSBosko Milekic 		mtx_lock(&ng_nodelist_mtx);				\
36130400f03SJulian Elischer 		LIST_INSERT_HEAD(&ng_freenodes, node, nd_nodes);	\
36230400f03SJulian Elischer 		node->nd_magic = 0;					\
3639ed346baSBosko Milekic 		mtx_unlock(&ng_nodelist_mtx);				\
36430400f03SJulian Elischer 	} while (0)
36530400f03SJulian Elischer 
36630400f03SJulian Elischer #else /* NETGRAPH_DEBUG */ /*----------------------------------------------*/
36730400f03SJulian Elischer 
36830400f03SJulian Elischer #define NG_ALLOC_HOOK(hook) _NG_ALLOC_HOOK(hook)
36930400f03SJulian Elischer #define NG_ALLOC_NODE(node) _NG_ALLOC_NODE(node)
37030400f03SJulian Elischer 
3711ede983cSDag-Erling Smørgrav #define NG_FREE_HOOK(hook) do { free((hook), M_NETGRAPH_HOOK); } while (0)
3721ede983cSDag-Erling Smørgrav #define NG_FREE_NODE(node) do { free((node), M_NETGRAPH_NODE); } while (0)
37330400f03SJulian Elischer 
37430400f03SJulian Elischer #endif /* NETGRAPH_DEBUG */ /*----------------------------------------------*/
37530400f03SJulian Elischer 
376f33ca0c9SMarcel Moolenaar /* Set this to kdb_enter("X") to catch all errors as they occur */
3774cf49a43SJulian Elischer #ifndef TRAP_ERROR
3786b795970SJulian Elischer #define TRAP_ERROR()
3794cf49a43SJulian Elischer #endif
3804cf49a43SJulian Elischer 
3815f901c92SAndrew Turner VNET_DEFINE_STATIC(ng_ID_t, nextID) = 1;
3821e77c105SRobert Watson #define	V_nextID			VNET(nextID)
383dc90cad9SJulian Elischer 
384b2da83c2SArchie Cobbs #ifdef INVARIANTS
385b2da83c2SArchie Cobbs #define CHECK_DATA_MBUF(m)	do {					\
386b2da83c2SArchie Cobbs 		struct mbuf *n;						\
387b2da83c2SArchie Cobbs 		int total;						\
388b2da83c2SArchie Cobbs 									\
389fe584538SDag-Erling Smørgrav 		M_ASSERTPKTHDR(m);					\
390b32cfb32SGleb Smirnoff 		for (total = 0, n = (m); n != NULL; n = n->m_next) {	\
391b2da83c2SArchie Cobbs 			total += n->m_len;				\
392b32cfb32SGleb Smirnoff 			if (n->m_nextpkt != NULL)			\
393b32cfb32SGleb Smirnoff 				panic("%s: m_nextpkt", __func__);	\
394b32cfb32SGleb Smirnoff 		}							\
395ba5b359aSGleb Smirnoff 									\
396b2da83c2SArchie Cobbs 		if ((m)->m_pkthdr.len != total) {			\
397b2da83c2SArchie Cobbs 			panic("%s: %d != %d",				\
3986e551fb6SDavid E. O'Brien 			    __func__, (m)->m_pkthdr.len, total);	\
399b2da83c2SArchie Cobbs 		}							\
400b2da83c2SArchie Cobbs 	} while (0)
401b2da83c2SArchie Cobbs #else
402b2da83c2SArchie Cobbs #define CHECK_DATA_MBUF(m)
403b2da83c2SArchie Cobbs #endif
404b2da83c2SArchie Cobbs 
405e088dd4cSAlexander Motin #define ERROUT(x)	do { error = (x); goto done; } while (0)
406dc90cad9SJulian Elischer 
4074cf49a43SJulian Elischer /************************************************************************
408f8307e12SArchie Cobbs 	Parse type definitions for generic messages
409f8307e12SArchie Cobbs ************************************************************************/
410f8307e12SArchie Cobbs 
411f8307e12SArchie Cobbs /* Handy structure parse type defining macro */
412f8307e12SArchie Cobbs #define DEFINE_PARSE_STRUCT_TYPE(lo, up, args)				\
413f0184ff8SArchie Cobbs static const struct ng_parse_struct_field				\
414f0184ff8SArchie Cobbs 	ng_ ## lo ## _type_fields[] = NG_GENERIC_ ## up ## _INFO args;	\
415f8307e12SArchie Cobbs static const struct ng_parse_type ng_generic_ ## lo ## _type = {	\
416f8307e12SArchie Cobbs 	&ng_parse_struct_type,						\
417f0184ff8SArchie Cobbs 	&ng_ ## lo ## _type_fields					\
418f8307e12SArchie Cobbs }
419f8307e12SArchie Cobbs 
420f8307e12SArchie Cobbs DEFINE_PARSE_STRUCT_TYPE(mkpeer, MKPEER, ());
421f8307e12SArchie Cobbs DEFINE_PARSE_STRUCT_TYPE(connect, CONNECT, ());
422f8307e12SArchie Cobbs DEFINE_PARSE_STRUCT_TYPE(name, NAME, ());
423f8307e12SArchie Cobbs DEFINE_PARSE_STRUCT_TYPE(rmhook, RMHOOK, ());
424f8307e12SArchie Cobbs DEFINE_PARSE_STRUCT_TYPE(nodeinfo, NODEINFO, ());
425f8307e12SArchie Cobbs DEFINE_PARSE_STRUCT_TYPE(typeinfo, TYPEINFO, ());
426f8307e12SArchie Cobbs DEFINE_PARSE_STRUCT_TYPE(linkinfo, LINKINFO, (&ng_generic_nodeinfo_type));
427f8307e12SArchie Cobbs 
428f8307e12SArchie Cobbs /* Get length of an array when the length is stored as a 32 bit
429d7d97eb0SJeroen Ruigrok van der Werven    value immediately preceding the array -- as with struct namelist
430f8307e12SArchie Cobbs    and struct typelist. */
431f8307e12SArchie Cobbs static int
432f8307e12SArchie Cobbs ng_generic_list_getLength(const struct ng_parse_type *type,
433f8307e12SArchie Cobbs 	const u_char *start, const u_char *buf)
434f8307e12SArchie Cobbs {
435f8307e12SArchie Cobbs 	return *((const u_int32_t *)(buf - 4));
436f8307e12SArchie Cobbs }
437f8307e12SArchie Cobbs 
438f8307e12SArchie Cobbs /* Get length of the array of struct linkinfo inside a struct hooklist */
439f8307e12SArchie Cobbs static int
440f8307e12SArchie Cobbs ng_generic_linkinfo_getLength(const struct ng_parse_type *type,
441f8307e12SArchie Cobbs 	const u_char *start, const u_char *buf)
442f8307e12SArchie Cobbs {
443f8307e12SArchie Cobbs 	const struct hooklist *hl = (const struct hooklist *)start;
444f8307e12SArchie Cobbs 
445f8307e12SArchie Cobbs 	return hl->nodeinfo.hooks;
446f8307e12SArchie Cobbs }
447f8307e12SArchie Cobbs 
448f8307e12SArchie Cobbs /* Array type for a variable length array of struct namelist */
449f8307e12SArchie Cobbs static const struct ng_parse_array_info ng_nodeinfoarray_type_info = {
450f8307e12SArchie Cobbs 	&ng_generic_nodeinfo_type,
451f8307e12SArchie Cobbs 	&ng_generic_list_getLength
452f8307e12SArchie Cobbs };
453f8307e12SArchie Cobbs static const struct ng_parse_type ng_generic_nodeinfoarray_type = {
454f8307e12SArchie Cobbs 	&ng_parse_array_type,
455f8307e12SArchie Cobbs 	&ng_nodeinfoarray_type_info
456f8307e12SArchie Cobbs };
457f8307e12SArchie Cobbs 
458f8307e12SArchie Cobbs /* Array type for a variable length array of struct typelist */
459f8307e12SArchie Cobbs static const struct ng_parse_array_info ng_typeinfoarray_type_info = {
460f8307e12SArchie Cobbs 	&ng_generic_typeinfo_type,
461f8307e12SArchie Cobbs 	&ng_generic_list_getLength
462f8307e12SArchie Cobbs };
463f8307e12SArchie Cobbs static const struct ng_parse_type ng_generic_typeinfoarray_type = {
464f8307e12SArchie Cobbs 	&ng_parse_array_type,
465f8307e12SArchie Cobbs 	&ng_typeinfoarray_type_info
466f8307e12SArchie Cobbs };
467f8307e12SArchie Cobbs 
468f8307e12SArchie Cobbs /* Array type for array of struct linkinfo in struct hooklist */
469f8307e12SArchie Cobbs static const struct ng_parse_array_info ng_generic_linkinfo_array_type_info = {
470f8307e12SArchie Cobbs 	&ng_generic_linkinfo_type,
471f8307e12SArchie Cobbs 	&ng_generic_linkinfo_getLength
472f8307e12SArchie Cobbs };
473f8307e12SArchie Cobbs static const struct ng_parse_type ng_generic_linkinfo_array_type = {
474f8307e12SArchie Cobbs 	&ng_parse_array_type,
475f8307e12SArchie Cobbs 	&ng_generic_linkinfo_array_type_info
476f8307e12SArchie Cobbs };
477f8307e12SArchie Cobbs 
4785caf0d56SGleb Smirnoff DEFINE_PARSE_STRUCT_TYPE(typelist, TYPELIST, (&ng_generic_typeinfoarray_type));
479f8307e12SArchie Cobbs DEFINE_PARSE_STRUCT_TYPE(hooklist, HOOKLIST,
480f8307e12SArchie Cobbs 	(&ng_generic_nodeinfo_type, &ng_generic_linkinfo_array_type));
481f8307e12SArchie Cobbs DEFINE_PARSE_STRUCT_TYPE(listnodes, LISTNODES,
482f8307e12SArchie Cobbs 	(&ng_generic_nodeinfoarray_type));
483f8307e12SArchie Cobbs 
484f8307e12SArchie Cobbs /* List of commands and how to convert arguments to/from ASCII */
485f8307e12SArchie Cobbs static const struct ng_cmdlist ng_generic_cmds[] = {
486f8307e12SArchie Cobbs 	{
487f8307e12SArchie Cobbs 	  NGM_GENERIC_COOKIE,
488f8307e12SArchie Cobbs 	  NGM_SHUTDOWN,
489f8307e12SArchie Cobbs 	  "shutdown",
490f8307e12SArchie Cobbs 	  NULL,
491f8307e12SArchie Cobbs 	  NULL
492f8307e12SArchie Cobbs 	},
493f8307e12SArchie Cobbs 	{
494f8307e12SArchie Cobbs 	  NGM_GENERIC_COOKIE,
495f8307e12SArchie Cobbs 	  NGM_MKPEER,
496f8307e12SArchie Cobbs 	  "mkpeer",
497f8307e12SArchie Cobbs 	  &ng_generic_mkpeer_type,
498f8307e12SArchie Cobbs 	  NULL
499f8307e12SArchie Cobbs 	},
500f8307e12SArchie Cobbs 	{
501f8307e12SArchie Cobbs 	  NGM_GENERIC_COOKIE,
502f8307e12SArchie Cobbs 	  NGM_CONNECT,
503f8307e12SArchie Cobbs 	  "connect",
504f8307e12SArchie Cobbs 	  &ng_generic_connect_type,
505f8307e12SArchie Cobbs 	  NULL
506f8307e12SArchie Cobbs 	},
507f8307e12SArchie Cobbs 	{
508f8307e12SArchie Cobbs 	  NGM_GENERIC_COOKIE,
509f8307e12SArchie Cobbs 	  NGM_NAME,
510f8307e12SArchie Cobbs 	  "name",
511f8307e12SArchie Cobbs 	  &ng_generic_name_type,
512f8307e12SArchie Cobbs 	  NULL
513f8307e12SArchie Cobbs 	},
514f8307e12SArchie Cobbs 	{
515f8307e12SArchie Cobbs 	  NGM_GENERIC_COOKIE,
516f8307e12SArchie Cobbs 	  NGM_RMHOOK,
517f8307e12SArchie Cobbs 	  "rmhook",
518f8307e12SArchie Cobbs 	  &ng_generic_rmhook_type,
519f8307e12SArchie Cobbs 	  NULL
520f8307e12SArchie Cobbs 	},
521f8307e12SArchie Cobbs 	{
522f8307e12SArchie Cobbs 	  NGM_GENERIC_COOKIE,
523f8307e12SArchie Cobbs 	  NGM_NODEINFO,
524f8307e12SArchie Cobbs 	  "nodeinfo",
525f8307e12SArchie Cobbs 	  NULL,
526f8307e12SArchie Cobbs 	  &ng_generic_nodeinfo_type
527f8307e12SArchie Cobbs 	},
528f8307e12SArchie Cobbs 	{
529f8307e12SArchie Cobbs 	  NGM_GENERIC_COOKIE,
530f8307e12SArchie Cobbs 	  NGM_LISTHOOKS,
531f8307e12SArchie Cobbs 	  "listhooks",
532f8307e12SArchie Cobbs 	  NULL,
533f8307e12SArchie Cobbs 	  &ng_generic_hooklist_type
534f8307e12SArchie Cobbs 	},
535f8307e12SArchie Cobbs 	{
536f8307e12SArchie Cobbs 	  NGM_GENERIC_COOKIE,
537f8307e12SArchie Cobbs 	  NGM_LISTNAMES,
538f8307e12SArchie Cobbs 	  "listnames",
539f8307e12SArchie Cobbs 	  NULL,
540f8307e12SArchie Cobbs 	  &ng_generic_listnodes_type	/* same as NGM_LISTNODES */
541f8307e12SArchie Cobbs 	},
542f8307e12SArchie Cobbs 	{
543f8307e12SArchie Cobbs 	  NGM_GENERIC_COOKIE,
544f8307e12SArchie Cobbs 	  NGM_LISTNODES,
545f8307e12SArchie Cobbs 	  "listnodes",
546f8307e12SArchie Cobbs 	  NULL,
547f8307e12SArchie Cobbs 	  &ng_generic_listnodes_type
548f8307e12SArchie Cobbs 	},
549f8307e12SArchie Cobbs 	{
550f8307e12SArchie Cobbs 	  NGM_GENERIC_COOKIE,
551f8307e12SArchie Cobbs 	  NGM_LISTTYPES,
552f8307e12SArchie Cobbs 	  "listtypes",
553f8307e12SArchie Cobbs 	  NULL,
5545caf0d56SGleb Smirnoff 	  &ng_generic_typelist_type
555f8307e12SArchie Cobbs 	},
556f8307e12SArchie Cobbs 	{
557f8307e12SArchie Cobbs 	  NGM_GENERIC_COOKIE,
5587095e097SPoul-Henning Kamp 	  NGM_TEXT_CONFIG,
5597095e097SPoul-Henning Kamp 	  "textconfig",
5607095e097SPoul-Henning Kamp 	  NULL,
5617095e097SPoul-Henning Kamp 	  &ng_parse_string_type
5627095e097SPoul-Henning Kamp 	},
5637095e097SPoul-Henning Kamp 	{
5647095e097SPoul-Henning Kamp 	  NGM_GENERIC_COOKIE,
565f8307e12SArchie Cobbs 	  NGM_TEXT_STATUS,
566f8307e12SArchie Cobbs 	  "textstatus",
567f8307e12SArchie Cobbs 	  NULL,
568f8307e12SArchie Cobbs 	  &ng_parse_string_type
569f8307e12SArchie Cobbs 	},
570f8307e12SArchie Cobbs 	{
571f8307e12SArchie Cobbs 	  NGM_GENERIC_COOKIE,
572f8307e12SArchie Cobbs 	  NGM_ASCII2BINARY,
573f8307e12SArchie Cobbs 	  "ascii2binary",
574f8307e12SArchie Cobbs 	  &ng_parse_ng_mesg_type,
575f8307e12SArchie Cobbs 	  &ng_parse_ng_mesg_type
576f8307e12SArchie Cobbs 	},
577f8307e12SArchie Cobbs 	{
578f8307e12SArchie Cobbs 	  NGM_GENERIC_COOKIE,
579f8307e12SArchie Cobbs 	  NGM_BINARY2ASCII,
580f8307e12SArchie Cobbs 	  "binary2ascii",
581f8307e12SArchie Cobbs 	  &ng_parse_ng_mesg_type,
582f8307e12SArchie Cobbs 	  &ng_parse_ng_mesg_type
583f8307e12SArchie Cobbs 	},
584f8307e12SArchie Cobbs 	{ 0 }
585f8307e12SArchie Cobbs };
586f8307e12SArchie Cobbs 
587f8307e12SArchie Cobbs /************************************************************************
5884cf49a43SJulian Elischer 			Node routines
5894cf49a43SJulian Elischer ************************************************************************/
5904cf49a43SJulian Elischer 
5914cf49a43SJulian Elischer /*
5924cf49a43SJulian Elischer  * Instantiate a node of the requested type
5934cf49a43SJulian Elischer  */
5944cf49a43SJulian Elischer int
5954cf49a43SJulian Elischer ng_make_node(const char *typename, node_p *nodepp)
5964cf49a43SJulian Elischer {
5974cf49a43SJulian Elischer 	struct ng_type *type;
598069154d5SJulian Elischer 	int	error;
5994cf49a43SJulian Elischer 
6004cf49a43SJulian Elischer 	/* Check that the type makes sense */
6014cf49a43SJulian Elischer 	if (typename == NULL) {
6026b795970SJulian Elischer 		TRAP_ERROR();
6034cf49a43SJulian Elischer 		return (EINVAL);
6044cf49a43SJulian Elischer 	}
6054cf49a43SJulian Elischer 
6067610f574SGleb Smirnoff 	/* Locate the node type. If we fail we return. Do not try to load
6077610f574SGleb Smirnoff 	 * module.
6087610f574SGleb Smirnoff 	 */
6094cf49a43SJulian Elischer 	if ((type = ng_findtype(typename)) == NULL)
6104cf49a43SJulian Elischer 		return (ENXIO);
6114cf49a43SJulian Elischer 
612069154d5SJulian Elischer 	/*
613069154d5SJulian Elischer 	 * If we have a constructor, then make the node and
614069154d5SJulian Elischer 	 * call the constructor to do type specific initialisation.
615069154d5SJulian Elischer 	 */
616069154d5SJulian Elischer 	if (type->constructor != NULL) {
617069154d5SJulian Elischer 		if ((error = ng_make_node_common(type, nodepp)) == 0) {
6185633ca71SGleb Smirnoff 			if ((error = ((*type->constructor)(*nodepp))) != 0) {
61930400f03SJulian Elischer 				NG_NODE_UNREF(*nodepp);
620069154d5SJulian Elischer 			}
621069154d5SJulian Elischer 		}
622069154d5SJulian Elischer 	} else {
623069154d5SJulian Elischer 		/*
624069154d5SJulian Elischer 		 * Node has no constructor. We cannot ask for one
62564efc707SRobert Watson 		 * to be made. It must be brought into existence by
626954c4772SJulian Elischer 		 * some external agency. The external agency should
627069154d5SJulian Elischer 		 * call ng_make_node_common() directly to get the
628069154d5SJulian Elischer 		 * netgraph part initialised.
629069154d5SJulian Elischer 		 */
6306b795970SJulian Elischer 		TRAP_ERROR();
631069154d5SJulian Elischer 		error = EINVAL;
632069154d5SJulian Elischer 	}
633069154d5SJulian Elischer 	return (error);
6344cf49a43SJulian Elischer }
6354cf49a43SJulian Elischer 
6364cf49a43SJulian Elischer /*
637069154d5SJulian Elischer  * Generic node creation. Called by node initialisation for externally
638069154d5SJulian Elischer  * instantiated nodes (e.g. hardware, sockets, etc ).
6394cf49a43SJulian Elischer  * The returned node has a reference count of 1.
6404cf49a43SJulian Elischer  */
6414cf49a43SJulian Elischer int
6424cf49a43SJulian Elischer ng_make_node_common(struct ng_type *type, node_p *nodepp)
6434cf49a43SJulian Elischer {
6444cf49a43SJulian Elischer 	node_p node;
6454cf49a43SJulian Elischer 
6464cf49a43SJulian Elischer 	/* Require the node type to have been already installed */
6474cf49a43SJulian Elischer 	if (ng_findtype(type->name) == NULL) {
6486b795970SJulian Elischer 		TRAP_ERROR();
6494cf49a43SJulian Elischer 		return (EINVAL);
6504cf49a43SJulian Elischer 	}
6514cf49a43SJulian Elischer 
6524cf49a43SJulian Elischer 	/* Make a node and try attach it to the type */
65330400f03SJulian Elischer 	NG_ALLOC_NODE(node);
6544cf49a43SJulian Elischer 	if (node == NULL) {
6556b795970SJulian Elischer 		TRAP_ERROR();
6564cf49a43SJulian Elischer 		return (ENOMEM);
6574cf49a43SJulian Elischer 	}
65830400f03SJulian Elischer 	node->nd_type = type;
659bc29160dSMarko Zec #ifdef VIMAGE
660bc29160dSMarko Zec 	node->nd_vnet = curvnet;
661bc29160dSMarko Zec #endif
66230400f03SJulian Elischer 	NG_NODE_REF(node);				/* note reference */
6634cf49a43SJulian Elischer 	type->refs++;
6644cf49a43SJulian Elischer 
6652c8dda8dSWojciech A. Koszek 	NG_QUEUE_LOCK_INIT(&node->nd_input_queue);
6669852972bSAlexander Motin 	STAILQ_INIT(&node->nd_input_queue.queue);
66730400f03SJulian Elischer 	node->nd_input_queue.q_flags = 0;
6684cf49a43SJulian Elischer 
6694cf49a43SJulian Elischer 	/* Initialize hook list for new node */
67030400f03SJulian Elischer 	LIST_INIT(&node->nd_hooks);
6714cf49a43SJulian Elischer 
672687adb70SGleb Smirnoff 	/* Get an ID and put us in the hash chain. */
673c4282b74SGleb Smirnoff 	IDHASH_WLOCK();
67430400f03SJulian Elischer 	for (;;) { /* wrap protection, even if silly */
675069154d5SJulian Elischer 		node_p node2 = NULL;
676ac957cd2SJulian Elischer 		node->nd_ID = V_nextID++; /* 137/sec for 1 year before wrap */
6770f150d04SJulian Elischer 
67830400f03SJulian Elischer 		/* Is there a problem with the new number? */
6790f150d04SJulian Elischer 		NG_IDHASH_FIND(node->nd_ID, node2); /* already taken? */
6800f150d04SJulian Elischer 		if ((node->nd_ID != 0) && (node2 == NULL)) {
68130400f03SJulian Elischer 			break;
682069154d5SJulian Elischer 		}
68330400f03SJulian Elischer 	}
684687adb70SGleb Smirnoff 	V_ng_nodes++;
685687adb70SGleb Smirnoff 	if (V_ng_nodes * 2 > V_ng_ID_hmask)
686687adb70SGleb Smirnoff 		ng_ID_rehash();
6874bd1b557SGleb Smirnoff 	LIST_INSERT_HEAD(&V_ng_ID_hash[NG_IDHASH_FN(node->nd_ID)], node,
6884bd1b557SGleb Smirnoff 	    nd_idnodes);
689c4282b74SGleb Smirnoff 	IDHASH_WUNLOCK();
690dc90cad9SJulian Elischer 
6914cf49a43SJulian Elischer 	/* Done */
6924cf49a43SJulian Elischer 	*nodepp = node;
6934cf49a43SJulian Elischer 	return (0);
6944cf49a43SJulian Elischer }
6954cf49a43SJulian Elischer 
6964cf49a43SJulian Elischer /*
6974cf49a43SJulian Elischer  * Forceably start the shutdown process on a node. Either call
69864efc707SRobert Watson  * its shutdown method, or do the default shutdown if there is
6994cf49a43SJulian Elischer  * no type-specific method.
7004cf49a43SJulian Elischer  *
70164efc707SRobert Watson  * We can only be called from a shutdown message, so we know we have
7023e4084c8SJulian Elischer  * a writer lock, and therefore exclusive access. It also means
7033e4084c8SJulian Elischer  * that we should not be on the work queue, but we check anyhow.
704069154d5SJulian Elischer  *
705069154d5SJulian Elischer  * Persistent node types must have a type-specific method which
70664efc707SRobert Watson  * allocates a new node in which case, this one is irretrievably going away,
7073e4084c8SJulian Elischer  * or cleans up anything it needs, and just makes the node valid again,
7083e4084c8SJulian Elischer  * in which case we allow the node to survive.
7093e4084c8SJulian Elischer  *
71064efc707SRobert Watson  * XXX We need to think of how to tell a persistent node that we
7113e4084c8SJulian Elischer  * REALLY need to go away because the hardware has gone or we
7123e4084c8SJulian Elischer  * are rebooting.... etc.
7134cf49a43SJulian Elischer  */
7144cf49a43SJulian Elischer void
7151acb27c6SJulian Elischer ng_rmnode(node_p node, hook_p dummy1, void *dummy2, int dummy3)
7164cf49a43SJulian Elischer {
7173e4084c8SJulian Elischer 	hook_p hook;
7183e4084c8SJulian Elischer 
7194cf49a43SJulian Elischer 	/* Check if it's already shutting down */
720be4252b3SJulian Elischer 	if ((node->nd_flags & NGF_CLOSING) != 0)
7214cf49a43SJulian Elischer 		return;
7224cf49a43SJulian Elischer 
7231acb27c6SJulian Elischer 	if (node == &ng_deadnode) {
7241acb27c6SJulian Elischer 		printf ("shutdown called on deadnode\n");
7251acb27c6SJulian Elischer 		return;
7261acb27c6SJulian Elischer 	}
7271acb27c6SJulian Elischer 
7284cf49a43SJulian Elischer 	/* Add an extra reference so it doesn't go away during this */
72930400f03SJulian Elischer 	NG_NODE_REF(node);
7304cf49a43SJulian Elischer 
73130400f03SJulian Elischer 	/*
73230400f03SJulian Elischer 	 * Mark it invalid so any newcomers know not to try use it
73330400f03SJulian Elischer 	 * Also add our own mark so we can't recurse
734be4252b3SJulian Elischer 	 * note that NGF_INVALID does not do this as it's also set during
73530400f03SJulian Elischer 	 * creation
73630400f03SJulian Elischer 	 */
737be4252b3SJulian Elischer 	node->nd_flags |= NGF_INVALID|NGF_CLOSING;
7384cf49a43SJulian Elischer 
739991fc65aSJulian Elischer 	/* If node has its pre-shutdown method, then call it first*/
740991fc65aSJulian Elischer 	if (node->nd_type && node->nd_type->close)
741991fc65aSJulian Elischer 		(*node->nd_type->close)(node);
742991fc65aSJulian Elischer 
7433e4084c8SJulian Elischer 	/* Notify all remaining connected nodes to disconnect */
7443e4084c8SJulian Elischer 	while ((hook = LIST_FIRST(&node->nd_hooks)) != NULL)
7453e4084c8SJulian Elischer 		ng_destroy_hook(hook);
74630400f03SJulian Elischer 
747069154d5SJulian Elischer 	/*
748069154d5SJulian Elischer 	 * Drain the input queue forceably.
74930400f03SJulian Elischer 	 * it has no hooks so what's it going to do, bleed on someone?
75030400f03SJulian Elischer 	 * Theoretically we came here from a queue entry that was added
75130400f03SJulian Elischer 	 * Just before the queue was closed, so it should be empty anyway.
752b57a7965SJulian Elischer 	 * Also removes us from worklist if needed.
753069154d5SJulian Elischer 	 */
7549852972bSAlexander Motin 	ng_flush_input_queue(node);
755069154d5SJulian Elischer 
756069154d5SJulian Elischer 	/* Ask the type if it has anything to do in this case */
75730400f03SJulian Elischer 	if (node->nd_type && node->nd_type->shutdown) {
75830400f03SJulian Elischer 		(*node->nd_type->shutdown)(node);
75930400f03SJulian Elischer 		if (NG_NODE_IS_VALID(node)) {
76030400f03SJulian Elischer 			/*
76130400f03SJulian Elischer 			 * Well, blow me down if the node code hasn't declared
76230400f03SJulian Elischer 			 * that it doesn't want to die.
763053359b7SPedro F. Giffuni 			 * Presumably it is a persistent node.
7641acb27c6SJulian Elischer 			 * If we REALLY want it to go away,
7651acb27c6SJulian Elischer 			 *  e.g. hardware going away,
766be4252b3SJulian Elischer 			 * Our caller should set NGF_REALLY_DIE in nd_flags.
76730400f03SJulian Elischer 			 */
768be4252b3SJulian Elischer 			node->nd_flags &= ~(NGF_INVALID|NGF_CLOSING);
7691acb27c6SJulian Elischer 			NG_NODE_UNREF(node); /* Assume they still have theirs */
77030400f03SJulian Elischer 			return;
7714cf49a43SJulian Elischer 		}
7721acb27c6SJulian Elischer 	} else {				/* do the default thing */
7731acb27c6SJulian Elischer 		NG_NODE_UNREF(node);
7741acb27c6SJulian Elischer 	}
7754cf49a43SJulian Elischer 
77630400f03SJulian Elischer 	ng_unname(node); /* basically a NOP these days */
77730400f03SJulian Elischer 
77830400f03SJulian Elischer 	/*
77930400f03SJulian Elischer 	 * Remove extra reference, possibly the last
78030400f03SJulian Elischer 	 * Possible other holders of references may include
78130400f03SJulian Elischer 	 * timeout callouts, but theoretically the node's supposed to
78230400f03SJulian Elischer 	 * have cancelled them. Possibly hardware dependencies may
78330400f03SJulian Elischer 	 * force a driver to 'linger' with a reference.
78430400f03SJulian Elischer 	 */
78530400f03SJulian Elischer 	NG_NODE_UNREF(node);
7864cf49a43SJulian Elischer }
7874cf49a43SJulian Elischer 
7885951069aSJulian Elischer /*
7895951069aSJulian Elischer  * Remove a reference to the node, possibly the last.
7905951069aSJulian Elischer  * deadnode always acts as it it were the last.
7915951069aSJulian Elischer  */
7923fbdf774SGleb Smirnoff void
79330400f03SJulian Elischer ng_unref_node(node_p node)
7944cf49a43SJulian Elischer {
7956b795970SJulian Elischer 
7963fbdf774SGleb Smirnoff 	if (node == &ng_deadnode)
7973fbdf774SGleb Smirnoff 		return;
7986b795970SJulian Elischer 
799719fb725SCraig Rodrigues 	CURVNET_SET(node->nd_vnet);
800719fb725SCraig Rodrigues 
8013fbdf774SGleb Smirnoff 	if (refcount_release(&node->nd_refs)) { /* we were the last */
802069154d5SJulian Elischer 
80330400f03SJulian Elischer 		node->nd_type->refs--; /* XXX maybe should get types lock? */
804c4282b74SGleb Smirnoff 		NAMEHASH_WLOCK();
805687adb70SGleb Smirnoff 		if (NG_NODE_HAS_NAME(node)) {
806687adb70SGleb Smirnoff 			V_ng_named_nodes--;
80730400f03SJulian Elischer 			LIST_REMOVE(node, nd_nodes);
808687adb70SGleb Smirnoff 		}
809c4282b74SGleb Smirnoff 		NAMEHASH_WUNLOCK();
810069154d5SJulian Elischer 
811c4282b74SGleb Smirnoff 		IDHASH_WLOCK();
812687adb70SGleb Smirnoff 		V_ng_nodes--;
81330400f03SJulian Elischer 		LIST_REMOVE(node, nd_idnodes);
814c4282b74SGleb Smirnoff 		IDHASH_WUNLOCK();
815069154d5SJulian Elischer 
81612574a02SJulian Elischer 		mtx_destroy(&node->nd_input_queue.q_mtx);
817069154d5SJulian Elischer 		NG_FREE_NODE(node);
8184cf49a43SJulian Elischer 	}
819719fb725SCraig Rodrigues 	CURVNET_RESTORE();
8204cf49a43SJulian Elischer }
8214cf49a43SJulian Elischer 
8224cf49a43SJulian Elischer /************************************************************************
823dc90cad9SJulian Elischer 			Node ID handling
824dc90cad9SJulian Elischer ************************************************************************/
825dc90cad9SJulian Elischer static node_p
826069154d5SJulian Elischer ng_ID2noderef(ng_ID_t ID)
827dc90cad9SJulian Elischer {
82830400f03SJulian Elischer 	node_p node;
829687adb70SGleb Smirnoff 
830c4282b74SGleb Smirnoff 	IDHASH_RLOCK();
8310f150d04SJulian Elischer 	NG_IDHASH_FIND(ID, node);
83230400f03SJulian Elischer 	if (node)
83330400f03SJulian Elischer 		NG_NODE_REF(node);
834c4282b74SGleb Smirnoff 	IDHASH_RUNLOCK();
83530400f03SJulian Elischer 	return(node);
836dc90cad9SJulian Elischer }
837dc90cad9SJulian Elischer 
838dc90cad9SJulian Elischer ng_ID_t
8396117aa58SLutz Donnerhacke ng_node2ID(node_cp node)
840dc90cad9SJulian Elischer {
84170de87f2SJulian Elischer 	return (node ? NG_NODE_ID(node) : 0);
842dc90cad9SJulian Elischer }
843dc90cad9SJulian Elischer 
844dc90cad9SJulian Elischer /************************************************************************
8454cf49a43SJulian Elischer 			Node name handling
8464cf49a43SJulian Elischer ************************************************************************/
8474cf49a43SJulian Elischer 
8484cf49a43SJulian Elischer /*
8494bd1b557SGleb Smirnoff  * Assign a node a name.
8504cf49a43SJulian Elischer  */
8514cf49a43SJulian Elischer int
8524cf49a43SJulian Elischer ng_name_node(node_p node, const char *name)
8534cf49a43SJulian Elischer {
854687adb70SGleb Smirnoff 	uint32_t hash;
855069154d5SJulian Elischer 	node_p node2;
856687adb70SGleb Smirnoff 	int i;
8574cf49a43SJulian Elischer 
8584cf49a43SJulian Elischer 	/* Check the name is valid */
85987e2c66aSHartmut Brandt 	for (i = 0; i < NG_NODESIZ; i++) {
8604cf49a43SJulian Elischer 		if (name[i] == '\0' || name[i] == '.' || name[i] == ':')
8614cf49a43SJulian Elischer 			break;
8624cf49a43SJulian Elischer 	}
8634cf49a43SJulian Elischer 	if (i == 0 || name[i] != '\0') {
8646b795970SJulian Elischer 		TRAP_ERROR();
8654cf49a43SJulian Elischer 		return (EINVAL);
8664cf49a43SJulian Elischer 	}
867dc90cad9SJulian Elischer 	if (ng_decodeidname(name) != 0) { /* valid IDs not allowed here */
8686b795970SJulian Elischer 		TRAP_ERROR();
8694cf49a43SJulian Elischer 		return (EINVAL);
8704cf49a43SJulian Elischer 	}
8714cf49a43SJulian Elischer 
872687adb70SGleb Smirnoff 	NAMEHASH_WLOCK();
873687adb70SGleb Smirnoff 	if (V_ng_named_nodes * 2 > V_ng_name_hmask)
874687adb70SGleb Smirnoff 		ng_name_rehash();
875687adb70SGleb Smirnoff 
876687adb70SGleb Smirnoff 	hash = hash32_str(name, HASHINIT) & V_ng_name_hmask;
877687adb70SGleb Smirnoff 	/* Check the name isn't already being used. */
878687adb70SGleb Smirnoff 	LIST_FOREACH(node2, &V_ng_name_hash[hash], nd_nodes)
879687adb70SGleb Smirnoff 		if (NG_NODE_IS_VALID(node2) &&
880687adb70SGleb Smirnoff 		    (strcmp(NG_NODE_NAME(node2), name) == 0)) {
881687adb70SGleb Smirnoff 			NAMEHASH_WUNLOCK();
8824cf49a43SJulian Elischer 			return (EADDRINUSE);
8834cf49a43SJulian Elischer 		}
8844cf49a43SJulian Elischer 
885687adb70SGleb Smirnoff 	if (NG_NODE_HAS_NAME(node))
886cfea3f85SAlexander Motin 		LIST_REMOVE(node, nd_nodes);
887687adb70SGleb Smirnoff 	else
888687adb70SGleb Smirnoff 		V_ng_named_nodes++;
889687adb70SGleb Smirnoff 	/* Copy it. */
890687adb70SGleb Smirnoff 	strlcpy(NG_NODE_NAME(node), name, NG_NODESIZ);
891687adb70SGleb Smirnoff 	/* Update name hash. */
892603724d3SBjoern A. Zeeb 	LIST_INSERT_HEAD(&V_ng_name_hash[hash], node, nd_nodes);
893c4282b74SGleb Smirnoff 	NAMEHASH_WUNLOCK();
894cfea3f85SAlexander Motin 
8954cf49a43SJulian Elischer 	return (0);
8964cf49a43SJulian Elischer }
8974cf49a43SJulian Elischer 
8984cf49a43SJulian Elischer /*
8994cf49a43SJulian Elischer  * Find a node by absolute name. The name should NOT end with ':'
9004cf49a43SJulian Elischer  * The name "." means "this node" and "[xxx]" means "the node
9014cf49a43SJulian Elischer  * with ID (ie, at address) xxx".
9024cf49a43SJulian Elischer  *
9034cf49a43SJulian Elischer  * Returns the node if found, else NULL.
904069154d5SJulian Elischer  * Eventually should add something faster than a sequential search.
905e1e8f51bSRobert Watson  * Note it acquires a reference on the node so you can be sure it's still
906e1e8f51bSRobert Watson  * there.
9074cf49a43SJulian Elischer  */
9084cf49a43SJulian Elischer node_p
909069154d5SJulian Elischer ng_name2noderef(node_p here, const char *name)
9104cf49a43SJulian Elischer {
911dc90cad9SJulian Elischer 	node_p node;
912dc90cad9SJulian Elischer 	ng_ID_t temp;
913cfea3f85SAlexander Motin 	int	hash;
9144cf49a43SJulian Elischer 
9154cf49a43SJulian Elischer 	/* "." means "this node" */
916069154d5SJulian Elischer 	if (strcmp(name, ".") == 0) {
91730400f03SJulian Elischer 		NG_NODE_REF(here);
918069154d5SJulian Elischer 		return(here);
919069154d5SJulian Elischer 	}
9204cf49a43SJulian Elischer 
9214cf49a43SJulian Elischer 	/* Check for name-by-ID */
922dc90cad9SJulian Elischer 	if ((temp = ng_decodeidname(name)) != 0) {
923069154d5SJulian Elischer 		return (ng_ID2noderef(temp));
9244cf49a43SJulian Elischer 	}
9254cf49a43SJulian Elischer 
926687adb70SGleb Smirnoff 	/* Find node by name. */
927687adb70SGleb Smirnoff 	hash = hash32_str(name, HASHINIT) & V_ng_name_hmask;
928c4282b74SGleb Smirnoff 	NAMEHASH_RLOCK();
929c4282b74SGleb Smirnoff 	LIST_FOREACH(node, &V_ng_name_hash[hash], nd_nodes)
930cfea3f85SAlexander Motin 		if (NG_NODE_IS_VALID(node) &&
931cfea3f85SAlexander Motin 		    (strcmp(NG_NODE_NAME(node), name) == 0)) {
932c4282b74SGleb Smirnoff 			NG_NODE_REF(node);
9334cf49a43SJulian Elischer 			break;
9344cf49a43SJulian Elischer 		}
935c4282b74SGleb Smirnoff 	NAMEHASH_RUNLOCK();
936c4282b74SGleb Smirnoff 
9374cf49a43SJulian Elischer 	return (node);
9384cf49a43SJulian Elischer }
9394cf49a43SJulian Elischer 
9404cf49a43SJulian Elischer /*
9419d5abbddSJens Schweikhardt  * Decode an ID name, eg. "[f03034de]". Returns 0 if the
942dc90cad9SJulian Elischer  * string is not valid, otherwise returns the value.
9434cf49a43SJulian Elischer  */
944dc90cad9SJulian Elischer static ng_ID_t
9454cf49a43SJulian Elischer ng_decodeidname(const char *name)
9464cf49a43SJulian Elischer {
9472b70adcbSArchie Cobbs 	const int len = strlen(name);
94825792ef3SArchie Cobbs 	char *eptr;
9492b70adcbSArchie Cobbs 	u_long val;
9504cf49a43SJulian Elischer 
9512b70adcbSArchie Cobbs 	/* Check for proper length, brackets, no leading junk */
9524bd1b557SGleb Smirnoff 	if ((len < 3) || (name[0] != '[') || (name[len - 1] != ']') ||
9534bd1b557SGleb Smirnoff 	    (!isxdigit(name[1])))
95470de87f2SJulian Elischer 		return ((ng_ID_t)0);
9554cf49a43SJulian Elischer 
9562b70adcbSArchie Cobbs 	/* Decode number */
9572b70adcbSArchie Cobbs 	val = strtoul(name + 1, &eptr, 16);
9584bd1b557SGleb Smirnoff 	if ((eptr - name != len - 1) || (val == ULONG_MAX) || (val == 0))
95912f035e0SJulian Elischer 		return ((ng_ID_t)0);
9604bd1b557SGleb Smirnoff 
9614bd1b557SGleb Smirnoff 	return ((ng_ID_t)val);
9624cf49a43SJulian Elischer }
9634cf49a43SJulian Elischer 
9644cf49a43SJulian Elischer /*
9654cf49a43SJulian Elischer  * Remove a name from a node. This should only be called
9664cf49a43SJulian Elischer  * when shutting down and removing the node.
9674cf49a43SJulian Elischer  */
9684cf49a43SJulian Elischer void
9694cf49a43SJulian Elischer ng_unname(node_p node)
9704cf49a43SJulian Elischer {
9714cf49a43SJulian Elischer }
9724cf49a43SJulian Elischer 
973687adb70SGleb Smirnoff /*
974687adb70SGleb Smirnoff  * Allocate a bigger name hash.
975687adb70SGleb Smirnoff  */
976687adb70SGleb Smirnoff static void
977687adb70SGleb Smirnoff ng_name_rehash()
978687adb70SGleb Smirnoff {
979687adb70SGleb Smirnoff 	struct nodehash *new;
980687adb70SGleb Smirnoff 	uint32_t hash;
981687adb70SGleb Smirnoff 	u_long hmask;
982687adb70SGleb Smirnoff 	node_p node, node2;
983687adb70SGleb Smirnoff 	int i;
984687adb70SGleb Smirnoff 
985687adb70SGleb Smirnoff 	new = hashinit_flags((V_ng_name_hmask + 1) * 2, M_NETGRAPH_NODE, &hmask,
986687adb70SGleb Smirnoff 	    HASH_NOWAIT);
987687adb70SGleb Smirnoff 	if (new == NULL)
988687adb70SGleb Smirnoff 		return;
989687adb70SGleb Smirnoff 
990687adb70SGleb Smirnoff 	for (i = 0; i <= V_ng_name_hmask; i++)
991687adb70SGleb Smirnoff 		LIST_FOREACH_SAFE(node, &V_ng_name_hash[i], nd_nodes, node2) {
992687adb70SGleb Smirnoff #ifdef INVARIANTS
993687adb70SGleb Smirnoff 			LIST_REMOVE(node, nd_nodes);
994687adb70SGleb Smirnoff #endif
995687adb70SGleb Smirnoff 			hash = hash32_str(NG_NODE_NAME(node), HASHINIT) & hmask;
996687adb70SGleb Smirnoff 			LIST_INSERT_HEAD(&new[hash], node, nd_nodes);
997687adb70SGleb Smirnoff 		}
998687adb70SGleb Smirnoff 
999687adb70SGleb Smirnoff 	hashdestroy(V_ng_name_hash, M_NETGRAPH_NODE, V_ng_name_hmask);
1000687adb70SGleb Smirnoff 	V_ng_name_hash = new;
1001687adb70SGleb Smirnoff 	V_ng_name_hmask = hmask;
1002687adb70SGleb Smirnoff }
1003687adb70SGleb Smirnoff 
1004687adb70SGleb Smirnoff /*
1005687adb70SGleb Smirnoff  * Allocate a bigger ID hash.
1006687adb70SGleb Smirnoff  */
1007687adb70SGleb Smirnoff static void
1008687adb70SGleb Smirnoff ng_ID_rehash()
1009687adb70SGleb Smirnoff {
1010687adb70SGleb Smirnoff 	struct nodehash *new;
1011687adb70SGleb Smirnoff 	uint32_t hash;
1012687adb70SGleb Smirnoff 	u_long hmask;
1013687adb70SGleb Smirnoff 	node_p node, node2;
1014687adb70SGleb Smirnoff 	int i;
1015687adb70SGleb Smirnoff 
1016687adb70SGleb Smirnoff 	new = hashinit_flags((V_ng_ID_hmask + 1) * 2, M_NETGRAPH_NODE, &hmask,
1017687adb70SGleb Smirnoff 	    HASH_NOWAIT);
1018687adb70SGleb Smirnoff 	if (new == NULL)
1019687adb70SGleb Smirnoff 		return;
1020687adb70SGleb Smirnoff 
1021687adb70SGleb Smirnoff 	for (i = 0; i <= V_ng_ID_hmask; i++)
1022687adb70SGleb Smirnoff 		LIST_FOREACH_SAFE(node, &V_ng_ID_hash[i], nd_idnodes, node2) {
1023687adb70SGleb Smirnoff #ifdef INVARIANTS
1024687adb70SGleb Smirnoff 			LIST_REMOVE(node, nd_idnodes);
1025687adb70SGleb Smirnoff #endif
1026687adb70SGleb Smirnoff 			hash = (node->nd_ID % (hmask + 1));
1027687adb70SGleb Smirnoff 			LIST_INSERT_HEAD(&new[hash], node, nd_idnodes);
1028687adb70SGleb Smirnoff 		}
1029687adb70SGleb Smirnoff 
1030687adb70SGleb Smirnoff 	hashdestroy(V_ng_ID_hash, M_NETGRAPH_NODE, V_ng_name_hmask);
1031687adb70SGleb Smirnoff 	V_ng_ID_hash = new;
1032687adb70SGleb Smirnoff 	V_ng_ID_hmask = hmask;
1033687adb70SGleb Smirnoff }
1034687adb70SGleb Smirnoff 
10354cf49a43SJulian Elischer /************************************************************************
10364cf49a43SJulian Elischer 			Hook routines
10374cf49a43SJulian Elischer  Names are not optional. Hooks are always connected, except for a
10383e4084c8SJulian Elischer  brief moment within these routines. On invalidation or during creation
10393e4084c8SJulian Elischer  they are connected to the 'dead' hook.
10404cf49a43SJulian Elischer ************************************************************************/
10414cf49a43SJulian Elischer 
10424cf49a43SJulian Elischer /*
10434cf49a43SJulian Elischer  * Remove a hook reference
10444cf49a43SJulian Elischer  */
104530400f03SJulian Elischer void
10464cf49a43SJulian Elischer ng_unref_hook(hook_p hook)
10474cf49a43SJulian Elischer {
10486b795970SJulian Elischer 
10493fbdf774SGleb Smirnoff 	if (hook == &ng_deadhook)
10506b795970SJulian Elischer 		return;
1051018fe3d1SAlexander Motin 
10523fbdf774SGleb Smirnoff 	if (refcount_release(&hook->hk_refs)) { /* we were the last */
1053f573da1aSAlexander Motin 		if (_NG_HOOK_NODE(hook)) /* it'll probably be ng_deadnode */
10546b795970SJulian Elischer 			_NG_NODE_UNREF((_NG_HOOK_NODE(hook)));
1055069154d5SJulian Elischer 		NG_FREE_HOOK(hook);
1056069154d5SJulian Elischer 	}
10574cf49a43SJulian Elischer }
10584cf49a43SJulian Elischer 
10594cf49a43SJulian Elischer /*
10604cf49a43SJulian Elischer  * Add an unconnected hook to a node. Only used internally.
10613e4084c8SJulian Elischer  * Assumes node is locked. (XXX not yet true )
10624cf49a43SJulian Elischer  */
10634cf49a43SJulian Elischer static int
10644cf49a43SJulian Elischer ng_add_hook(node_p node, const char *name, hook_p *hookp)
10654cf49a43SJulian Elischer {
10664cf49a43SJulian Elischer 	hook_p hook;
10674cf49a43SJulian Elischer 	int error = 0;
10684cf49a43SJulian Elischer 
10694cf49a43SJulian Elischer 	/* Check that the given name is good */
10704cf49a43SJulian Elischer 	if (name == NULL) {
10716b795970SJulian Elischer 		TRAP_ERROR();
10724cf49a43SJulian Elischer 		return (EINVAL);
10734cf49a43SJulian Elischer 	}
1074899e9c4eSArchie Cobbs 	if (ng_findhook(node, name) != NULL) {
10756b795970SJulian Elischer 		TRAP_ERROR();
10764cf49a43SJulian Elischer 		return (EEXIST);
10774cf49a43SJulian Elischer 	}
10784cf49a43SJulian Elischer 
10794cf49a43SJulian Elischer 	/* Allocate the hook and link it up */
108030400f03SJulian Elischer 	NG_ALLOC_HOOK(hook);
10814cf49a43SJulian Elischer 	if (hook == NULL) {
10826b795970SJulian Elischer 		TRAP_ERROR();
10834cf49a43SJulian Elischer 		return (ENOMEM);
10844cf49a43SJulian Elischer 	}
10853e4084c8SJulian Elischer 	hook->hk_refs = 1;		/* add a reference for us to return */
108630400f03SJulian Elischer 	hook->hk_flags = HK_INVALID;
10873e4084c8SJulian Elischer 	hook->hk_peer = &ng_deadhook;	/* start off this way */
108830400f03SJulian Elischer 	hook->hk_node = node;
108930400f03SJulian Elischer 	NG_NODE_REF(node);		/* each hook counts as a reference */
10904cf49a43SJulian Elischer 
10913e4084c8SJulian Elischer 	/* Set hook name */
109287e2c66aSHartmut Brandt 	strlcpy(NG_HOOK_NAME(hook), name, NG_HOOKSIZ);
10933e4084c8SJulian Elischer 
10943e4084c8SJulian Elischer 	/*
10953e4084c8SJulian Elischer 	 * Check if the node type code has something to say about it
10963e4084c8SJulian Elischer 	 * If it fails, the unref of the hook will also unref the node.
10973e4084c8SJulian Elischer 	 */
1098954c4772SJulian Elischer 	if (node->nd_type->newhook != NULL) {
1099954c4772SJulian Elischer 		if ((error = (*node->nd_type->newhook)(node, hook, name))) {
110030400f03SJulian Elischer 			NG_HOOK_UNREF(hook);	/* this frees the hook */
1101069154d5SJulian Elischer 			return (error);
1102069154d5SJulian Elischer 		}
1103954c4772SJulian Elischer 	}
11044cf49a43SJulian Elischer 	/*
11054cf49a43SJulian Elischer 	 * The 'type' agrees so far, so go ahead and link it in.
11064cf49a43SJulian Elischer 	 * We'll ask again later when we actually connect the hooks.
11074cf49a43SJulian Elischer 	 */
110830400f03SJulian Elischer 	LIST_INSERT_HEAD(&node->nd_hooks, hook, hk_hooks);
110930400f03SJulian Elischer 	node->nd_numhooks++;
11103e4084c8SJulian Elischer 	NG_HOOK_REF(hook);	/* one for the node */
11114cf49a43SJulian Elischer 
11124cf49a43SJulian Elischer 	if (hookp)
11134cf49a43SJulian Elischer 		*hookp = hook;
11143e4084c8SJulian Elischer 	return (0);
11154cf49a43SJulian Elischer }
11164cf49a43SJulian Elischer 
11174cf49a43SJulian Elischer /*
1118899e9c4eSArchie Cobbs  * Find a hook
1119899e9c4eSArchie Cobbs  *
1120899e9c4eSArchie Cobbs  * Node types may supply their own optimized routines for finding
1121899e9c4eSArchie Cobbs  * hooks.  If none is supplied, we just do a linear search.
11223e4084c8SJulian Elischer  * XXX Possibly we should add a reference to the hook?
1123899e9c4eSArchie Cobbs  */
1124899e9c4eSArchie Cobbs hook_p
1125899e9c4eSArchie Cobbs ng_findhook(node_p node, const char *name)
1126899e9c4eSArchie Cobbs {
1127899e9c4eSArchie Cobbs 	hook_p hook;
1128899e9c4eSArchie Cobbs 
112930400f03SJulian Elischer 	if (node->nd_type->findhook != NULL)
113030400f03SJulian Elischer 		return (*node->nd_type->findhook)(node, name);
113130400f03SJulian Elischer 	LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) {
11324bd1b557SGleb Smirnoff 		if (NG_HOOK_IS_VALID(hook) &&
11334bd1b557SGleb Smirnoff 		    (strcmp(NG_HOOK_NAME(hook), name) == 0))
1134899e9c4eSArchie Cobbs 			return (hook);
1135899e9c4eSArchie Cobbs 	}
1136899e9c4eSArchie Cobbs 	return (NULL);
1137899e9c4eSArchie Cobbs }
1138899e9c4eSArchie Cobbs 
1139899e9c4eSArchie Cobbs /*
11404cf49a43SJulian Elischer  * Destroy a hook
11414cf49a43SJulian Elischer  *
11424cf49a43SJulian Elischer  * As hooks are always attached, this really destroys two hooks.
11434cf49a43SJulian Elischer  * The one given, and the one attached to it. Disconnect the hooks
11443e4084c8SJulian Elischer  * from each other first. We reconnect the peer hook to the 'dead'
11453e4084c8SJulian Elischer  * hook so that it can still exist after we depart. We then
11463e4084c8SJulian Elischer  * send the peer its own destroy message. This ensures that we only
11473e4084c8SJulian Elischer  * interact with the peer's structures when it is locked processing that
11483e4084c8SJulian Elischer  * message. We hold a reference to the peer hook so we are guaranteed that
11493e4084c8SJulian Elischer  * the peer hook and node are still going to exist until
11503e4084c8SJulian Elischer  * we are finished there as the hook holds a ref on the node.
11513e4084c8SJulian Elischer  * We run this same code again on the peer hook, but that time it is already
11523e4084c8SJulian Elischer  * attached to the 'dead' hook.
11536b795970SJulian Elischer  *
11546b795970SJulian Elischer  * This routine is called at all stages of hook creation
11556b795970SJulian Elischer  * on error detection and must be able to handle any such stage.
11564cf49a43SJulian Elischer  */
11574cf49a43SJulian Elischer void
11584cf49a43SJulian Elischer ng_destroy_hook(hook_p hook)
11594cf49a43SJulian Elischer {
1160ac5dd141SGleb Smirnoff 	hook_p peer;
1161ac5dd141SGleb Smirnoff 	node_p node;
11624cf49a43SJulian Elischer 
11636b795970SJulian Elischer 	if (hook == &ng_deadhook) {	/* better safe than sorry */
11646b795970SJulian Elischer 		printf("ng_destroy_hook called on deadhook\n");
11656b795970SJulian Elischer 		return;
11666b795970SJulian Elischer 	}
1167ac5dd141SGleb Smirnoff 
1168ac5dd141SGleb Smirnoff 	/*
1169ac5dd141SGleb Smirnoff 	 * Protect divorce process with mutex, to avoid races on
1170ac5dd141SGleb Smirnoff 	 * simultaneous disconnect.
1171ac5dd141SGleb Smirnoff 	 */
1172d2fd0788SAlexander V. Chernikov 	TOPOLOGY_WLOCK();
1173ac5dd141SGleb Smirnoff 
1174ac5dd141SGleb Smirnoff 	hook->hk_flags |= HK_INVALID;
1175ac5dd141SGleb Smirnoff 
1176ac5dd141SGleb Smirnoff 	peer = NG_HOOK_PEER(hook);
1177ac5dd141SGleb Smirnoff 	node = NG_HOOK_NODE(hook);
1178ac5dd141SGleb Smirnoff 
11793e4084c8SJulian Elischer 	if (peer && (peer != &ng_deadhook)) {
11803e4084c8SJulian Elischer 		/*
11813e4084c8SJulian Elischer 		 * Set the peer to point to ng_deadhook
11823e4084c8SJulian Elischer 		 * from this moment on we are effectively independent it.
118328323addSBryan Drewery 		 * send it an rmhook message of its own.
11843e4084c8SJulian Elischer 		 */
11853e4084c8SJulian Elischer 		peer->hk_peer = &ng_deadhook;	/* They no longer know us */
11863e4084c8SJulian Elischer 		hook->hk_peer = &ng_deadhook;	/* Nor us, them */
11876b795970SJulian Elischer 		if (NG_HOOK_NODE(peer) == &ng_deadnode) {
11886b795970SJulian Elischer 			/*
11896b795970SJulian Elischer 			 * If it's already divorced from a node,
11906b795970SJulian Elischer 			 * just free it.
11916b795970SJulian Elischer 			 */
1192d2fd0788SAlexander V. Chernikov 			TOPOLOGY_WUNLOCK();
11936b795970SJulian Elischer 		} else {
1194d2fd0788SAlexander V. Chernikov 			TOPOLOGY_WUNLOCK();
11956b795970SJulian Elischer 			ng_rmhook_self(peer); 	/* Send it a surprise */
11966b795970SJulian Elischer 		}
119752fa3556SJulian Elischer 		NG_HOOK_UNREF(peer);		/* account for peer link */
119852fa3556SJulian Elischer 		NG_HOOK_UNREF(hook);		/* account for peer link */
1199ac5dd141SGleb Smirnoff 	} else
1200d2fd0788SAlexander V. Chernikov 		TOPOLOGY_WUNLOCK();
1201ac5dd141SGleb Smirnoff 
1202d2fd0788SAlexander V. Chernikov 	TOPOLOGY_NOTOWNED();
12034cf49a43SJulian Elischer 
12044cf49a43SJulian Elischer 	/*
12054cf49a43SJulian Elischer 	 * Remove the hook from the node's list to avoid possible recursion
12064cf49a43SJulian Elischer 	 * in case the disconnection results in node shutdown.
12074cf49a43SJulian Elischer 	 */
12086b795970SJulian Elischer 	if (node == &ng_deadnode) { /* happens if called from ng_con_nodes() */
12096b795970SJulian Elischer 		return;
12106b795970SJulian Elischer 	}
121130400f03SJulian Elischer 	LIST_REMOVE(hook, hk_hooks);
121230400f03SJulian Elischer 	node->nd_numhooks--;
121330400f03SJulian Elischer 	if (node->nd_type->disconnect) {
12144cf49a43SJulian Elischer 		/*
12156b795970SJulian Elischer 		 * The type handler may elect to destroy the node so don't
121664efc707SRobert Watson 		 * trust its existence after this point. (except
12176b795970SJulian Elischer 		 * that we still hold a reference on it. (which we
12186b795970SJulian Elischer 		 * inherrited from the hook we are destroying)
12194cf49a43SJulian Elischer 		 */
122030400f03SJulian Elischer 		(*node->nd_type->disconnect) (hook);
12214cf49a43SJulian Elischer 	}
12226b795970SJulian Elischer 
12236b795970SJulian Elischer 	/*
12246b795970SJulian Elischer 	 * Note that because we will point to ng_deadnode, the original node
12256b795970SJulian Elischer 	 * is not decremented automatically so we do that manually.
12266b795970SJulian Elischer 	 */
12276b795970SJulian Elischer 	_NG_HOOK_NODE(hook) = &ng_deadnode;
12286b795970SJulian Elischer 	NG_NODE_UNREF(node);	/* We no longer point to it so adjust count */
12296b795970SJulian Elischer 	NG_HOOK_UNREF(hook);	/* Account for linkage (in list) to node */
12304cf49a43SJulian Elischer }
12314cf49a43SJulian Elischer 
12324cf49a43SJulian Elischer /*
12334cf49a43SJulian Elischer  * Take two hooks on a node and merge the connection so that the given node
12344cf49a43SJulian Elischer  * is effectively bypassed.
12354cf49a43SJulian Elischer  */
12364cf49a43SJulian Elischer int
12374cf49a43SJulian Elischer ng_bypass(hook_p hook1, hook_p hook2)
12384cf49a43SJulian Elischer {
123930400f03SJulian Elischer 	if (hook1->hk_node != hook2->hk_node) {
12406b795970SJulian Elischer 		TRAP_ERROR();
12414cf49a43SJulian Elischer 		return (EINVAL);
124230400f03SJulian Elischer 	}
1243d2fd0788SAlexander V. Chernikov 	TOPOLOGY_WLOCK();
1244c3189b3fSGleb Smirnoff 	if (NG_HOOK_NOT_VALID(hook1) || NG_HOOK_NOT_VALID(hook2)) {
1245d2fd0788SAlexander V. Chernikov 		TOPOLOGY_WUNLOCK();
1246c3189b3fSGleb Smirnoff 		return (EINVAL);
1247c3189b3fSGleb Smirnoff 	}
124830400f03SJulian Elischer 	hook1->hk_peer->hk_peer = hook2->hk_peer;
124930400f03SJulian Elischer 	hook2->hk_peer->hk_peer = hook1->hk_peer;
12504cf49a43SJulian Elischer 
12513e4084c8SJulian Elischer 	hook1->hk_peer = &ng_deadhook;
12523e4084c8SJulian Elischer 	hook2->hk_peer = &ng_deadhook;
1253d2fd0788SAlexander V. Chernikov 	TOPOLOGY_WUNLOCK();
12543e4084c8SJulian Elischer 
1255cf3254aaSGleb Smirnoff 	NG_HOOK_UNREF(hook1);
1256cf3254aaSGleb Smirnoff 	NG_HOOK_UNREF(hook2);
1257cf3254aaSGleb Smirnoff 
12584cf49a43SJulian Elischer 	/* XXX If we ever cache methods on hooks update them as well */
12594cf49a43SJulian Elischer 	ng_destroy_hook(hook1);
12604cf49a43SJulian Elischer 	ng_destroy_hook(hook2);
12614cf49a43SJulian Elischer 	return (0);
12624cf49a43SJulian Elischer }
12634cf49a43SJulian Elischer 
12644cf49a43SJulian Elischer /*
12654cf49a43SJulian Elischer  * Install a new netgraph type
12664cf49a43SJulian Elischer  */
12674cf49a43SJulian Elischer int
12684cf49a43SJulian Elischer ng_newtype(struct ng_type *tp)
12694cf49a43SJulian Elischer {
12704cf49a43SJulian Elischer 	const size_t namelen = strlen(tp->name);
12714cf49a43SJulian Elischer 
12724cf49a43SJulian Elischer 	/* Check version and type name fields */
12734bd1b557SGleb Smirnoff 	if ((tp->version != NG_ABI_VERSION) || (namelen == 0) ||
12744bd1b557SGleb Smirnoff 	    (namelen >= NG_TYPESIZ)) {
12756b795970SJulian Elischer 		TRAP_ERROR();
12768ed370fdSJulian Elischer 		if (tp->version != NG_ABI_VERSION) {
12774bd1b557SGleb Smirnoff 			printf("Netgraph: Node type rejected. ABI mismatch. "
12784bd1b557SGleb Smirnoff 			    "Suggest recompile\n");
12798ed370fdSJulian Elischer 		}
12804cf49a43SJulian Elischer 		return (EINVAL);
12814cf49a43SJulian Elischer 	}
12824cf49a43SJulian Elischer 
12834cf49a43SJulian Elischer 	/* Check for name collision */
12844cf49a43SJulian Elischer 	if (ng_findtype(tp->name) != NULL) {
12856b795970SJulian Elischer 		TRAP_ERROR();
12864cf49a43SJulian Elischer 		return (EEXIST);
12874cf49a43SJulian Elischer 	}
12884cf49a43SJulian Elischer 
1289069154d5SJulian Elischer 	/* Link in new type */
1290c4282b74SGleb Smirnoff 	TYPELIST_WLOCK();
1291069154d5SJulian Elischer 	LIST_INSERT_HEAD(&ng_typelist, tp, types);
1292c73b94a2SJulian Elischer 	tp->refs = 1;	/* first ref is linked list */
1293c4282b74SGleb Smirnoff 	TYPELIST_WUNLOCK();
12944cf49a43SJulian Elischer 	return (0);
12954cf49a43SJulian Elischer }
12964cf49a43SJulian Elischer 
12974cf49a43SJulian Elischer /*
1298c31b4a53SJulian Elischer  * unlink a netgraph type
1299c31b4a53SJulian Elischer  * If no examples exist
1300c31b4a53SJulian Elischer  */
1301c31b4a53SJulian Elischer int
1302c31b4a53SJulian Elischer ng_rmtype(struct ng_type *tp)
1303c31b4a53SJulian Elischer {
1304c31b4a53SJulian Elischer 	/* Check for name collision */
1305c31b4a53SJulian Elischer 	if (tp->refs != 1) {
1306c31b4a53SJulian Elischer 		TRAP_ERROR();
1307c31b4a53SJulian Elischer 		return (EBUSY);
1308c31b4a53SJulian Elischer 	}
1309c31b4a53SJulian Elischer 
1310c31b4a53SJulian Elischer 	/* Unlink type */
1311c4282b74SGleb Smirnoff 	TYPELIST_WLOCK();
1312c31b4a53SJulian Elischer 	LIST_REMOVE(tp, types);
1313c4282b74SGleb Smirnoff 	TYPELIST_WUNLOCK();
1314c31b4a53SJulian Elischer 	return (0);
1315c31b4a53SJulian Elischer }
1316c31b4a53SJulian Elischer 
1317c31b4a53SJulian Elischer /*
13184cf49a43SJulian Elischer  * Look for a type of the name given
13194cf49a43SJulian Elischer  */
13204cf49a43SJulian Elischer struct ng_type *
13214cf49a43SJulian Elischer ng_findtype(const char *typename)
13224cf49a43SJulian Elischer {
13234cf49a43SJulian Elischer 	struct ng_type *type;
13244cf49a43SJulian Elischer 
1325c4282b74SGleb Smirnoff 	TYPELIST_RLOCK();
1326069154d5SJulian Elischer 	LIST_FOREACH(type, &ng_typelist, types) {
13274cf49a43SJulian Elischer 		if (strcmp(type->name, typename) == 0)
13284cf49a43SJulian Elischer 			break;
13294cf49a43SJulian Elischer 	}
1330c4282b74SGleb Smirnoff 	TYPELIST_RUNLOCK();
13314cf49a43SJulian Elischer 	return (type);
13324cf49a43SJulian Elischer }
13334cf49a43SJulian Elischer 
13344cf49a43SJulian Elischer /************************************************************************
13354cf49a43SJulian Elischer 			Composite routines
13364cf49a43SJulian Elischer ************************************************************************/
13374cf49a43SJulian Elischer /*
13386b795970SJulian Elischer  * Connect two nodes using the specified hooks, using queued functions.
13394cf49a43SJulian Elischer  */
1340e088dd4cSAlexander Motin static int
1341e088dd4cSAlexander Motin ng_con_part3(node_p node, item_p item, hook_p hook)
13424cf49a43SJulian Elischer {
1343e088dd4cSAlexander Motin 	int	error = 0;
13444cf49a43SJulian Elischer 
13456b795970SJulian Elischer 	/*
13466b795970SJulian Elischer 	 * When we run, we know that the node 'node' is locked for us.
13476b795970SJulian Elischer 	 * Our caller has a reference on the hook.
13486b795970SJulian Elischer 	 * Our caller has a reference on the node.
13496b795970SJulian Elischer 	 * (In this case our caller is ng_apply_item() ).
13506b795970SJulian Elischer 	 * The peer hook has a reference on the hook.
13511acb27c6SJulian Elischer 	 * We are all set up except for the final call to the node, and
13521acb27c6SJulian Elischer 	 * the clearing of the INVALID flag.
13536b795970SJulian Elischer 	 */
13546b795970SJulian Elischer 	if (NG_HOOK_NODE(hook) == &ng_deadnode) {
13556b795970SJulian Elischer 		/*
13566b795970SJulian Elischer 		 * The node must have been freed again since we last visited
13576b795970SJulian Elischer 		 * here. ng_destry_hook() has this effect but nothing else does.
13586b795970SJulian Elischer 		 * We should just release our references and
13596b795970SJulian Elischer 		 * free anything we can think of.
13606b795970SJulian Elischer 		 * Since we know it's been destroyed, and it's our caller
13616b795970SJulian Elischer 		 * that holds the references, just return.
13626b795970SJulian Elischer 		 */
1363e088dd4cSAlexander Motin 		ERROUT(ENOENT);
13646b795970SJulian Elischer 	}
13656b795970SJulian Elischer 	if (hook->hk_node->nd_type->connect) {
1366e088dd4cSAlexander Motin 		if ((error = (*hook->hk_node->nd_type->connect) (hook))) {
13676b795970SJulian Elischer 			ng_destroy_hook(hook);	/* also zaps peer */
13681acb27c6SJulian Elischer 			printf("failed in ng_con_part3()\n");
1369e088dd4cSAlexander Motin 			ERROUT(error);
13704cf49a43SJulian Elischer 		}
13716b795970SJulian Elischer 	}
13726b795970SJulian Elischer 	/*
13736b795970SJulian Elischer 	 *  XXX this is wrong for SMP. Possibly we need
13746b795970SJulian Elischer 	 * to separate out 'create' and 'invalid' flags.
13756b795970SJulian Elischer 	 * should only set flags on hooks we have locked under our node.
13766b795970SJulian Elischer 	 */
13776b795970SJulian Elischer 	hook->hk_flags &= ~HK_INVALID;
1378e088dd4cSAlexander Motin done:
1379e088dd4cSAlexander Motin 	NG_FREE_ITEM(item);
1380e088dd4cSAlexander Motin 	return (error);
13816b795970SJulian Elischer }
13826b795970SJulian Elischer 
1383e088dd4cSAlexander Motin static int
1384e088dd4cSAlexander Motin ng_con_part2(node_p node, item_p item, hook_p hook)
13856b795970SJulian Elischer {
1386ac5dd141SGleb Smirnoff 	hook_p	peer;
1387e088dd4cSAlexander Motin 	int	error = 0;
13886b795970SJulian Elischer 
13896b795970SJulian Elischer 	/*
13906b795970SJulian Elischer 	 * When we run, we know that the node 'node' is locked for us.
13916b795970SJulian Elischer 	 * Our caller has a reference on the hook.
13926b795970SJulian Elischer 	 * Our caller has a reference on the node.
13936b795970SJulian Elischer 	 * (In this case our caller is ng_apply_item() ).
13946b795970SJulian Elischer 	 * The peer hook has a reference on the hook.
13956b795970SJulian Elischer 	 * our node pointer points to the 'dead' node.
13966b795970SJulian Elischer 	 * First check the hook name is unique.
13971acb27c6SJulian Elischer 	 * Should not happen because we checked before queueing this.
13986b795970SJulian Elischer 	 */
13996b795970SJulian Elischer 	if (ng_findhook(node, NG_HOOK_NAME(hook)) != NULL) {
14006b795970SJulian Elischer 		TRAP_ERROR();
14016b795970SJulian Elischer 		ng_destroy_hook(hook); /* should destroy peer too */
14021acb27c6SJulian Elischer 		printf("failed in ng_con_part2()\n");
1403e088dd4cSAlexander Motin 		ERROUT(EEXIST);
14046b795970SJulian Elischer 	}
14056b795970SJulian Elischer 	/*
14066b795970SJulian Elischer 	 * Check if the node type code has something to say about it
14076b795970SJulian Elischer 	 * If it fails, the unref of the hook will also unref the attached node,
14086b795970SJulian Elischer 	 * however since that node is 'ng_deadnode' this will do nothing.
14096b795970SJulian Elischer 	 * The peer hook will also be destroyed.
14106b795970SJulian Elischer 	 */
14116b795970SJulian Elischer 	if (node->nd_type->newhook != NULL) {
1412e088dd4cSAlexander Motin 		if ((error = (*node->nd_type->newhook)(node, hook,
1413e088dd4cSAlexander Motin 		    hook->hk_name))) {
14146b795970SJulian Elischer 			ng_destroy_hook(hook); /* should destroy peer too */
14151acb27c6SJulian Elischer 			printf("failed in ng_con_part2()\n");
1416e088dd4cSAlexander Motin 			ERROUT(error);
14176b795970SJulian Elischer 		}
14186b795970SJulian Elischer 	}
14196b795970SJulian Elischer 
14206b795970SJulian Elischer 	/*
14216b795970SJulian Elischer 	 * The 'type' agrees so far, so go ahead and link it in.
14226b795970SJulian Elischer 	 * We'll ask again later when we actually connect the hooks.
14236b795970SJulian Elischer 	 */
14246b795970SJulian Elischer 	hook->hk_node = node;		/* just overwrite ng_deadnode */
14256b795970SJulian Elischer 	NG_NODE_REF(node);		/* each hook counts as a reference */
14266b795970SJulian Elischer 	LIST_INSERT_HEAD(&node->nd_hooks, hook, hk_hooks);
14276b795970SJulian Elischer 	node->nd_numhooks++;
14286b795970SJulian Elischer 	NG_HOOK_REF(hook);	/* one for the node */
14296b795970SJulian Elischer 
14306b795970SJulian Elischer 	/*
143164efc707SRobert Watson 	 * We now have a symmetrical situation, where both hooks have been
14325951069aSJulian Elischer 	 * linked to their nodes, the newhook methods have been called
14336b795970SJulian Elischer 	 * And the references are all correct. The hooks are still marked
14346b795970SJulian Elischer 	 * as invalid, as we have not called the 'connect' methods
14356b795970SJulian Elischer 	 * yet.
143664efc707SRobert Watson 	 * We can call the local one immediately as we have the
14376b795970SJulian Elischer 	 * node locked, but we need to queue the remote one.
14386b795970SJulian Elischer 	 */
14396b795970SJulian Elischer 	if (hook->hk_node->nd_type->connect) {
1440e088dd4cSAlexander Motin 		if ((error = (*hook->hk_node->nd_type->connect) (hook))) {
14416b795970SJulian Elischer 			ng_destroy_hook(hook);	/* also zaps peer */
14421acb27c6SJulian Elischer 			printf("failed in ng_con_part2(A)\n");
1443e088dd4cSAlexander Motin 			ERROUT(error);
14446b795970SJulian Elischer 		}
14456b795970SJulian Elischer 	}
1446ac5dd141SGleb Smirnoff 
1447ac5dd141SGleb Smirnoff 	/*
1448ac5dd141SGleb Smirnoff 	 * Acquire topo mutex to avoid race with ng_destroy_hook().
1449ac5dd141SGleb Smirnoff 	 */
1450d2fd0788SAlexander V. Chernikov 	TOPOLOGY_RLOCK();
1451ac5dd141SGleb Smirnoff 	peer = hook->hk_peer;
1452ac5dd141SGleb Smirnoff 	if (peer == &ng_deadhook) {
1453d2fd0788SAlexander V. Chernikov 		TOPOLOGY_RUNLOCK();
1454ac5dd141SGleb Smirnoff 		printf("failed in ng_con_part2(B)\n");
1455ac5dd141SGleb Smirnoff 		ng_destroy_hook(hook);
1456e088dd4cSAlexander Motin 		ERROUT(ENOENT);
1457ac5dd141SGleb Smirnoff 	}
1458d2fd0788SAlexander V. Chernikov 	TOPOLOGY_RUNLOCK();
1459ac5dd141SGleb Smirnoff 
1460b332b91fSGleb Smirnoff 	if ((error = ng_send_fn2(peer->hk_node, peer, item, &ng_con_part3,
1461b332b91fSGleb Smirnoff 	    NULL, 0, NG_REUSE_ITEM))) {
1462ac5dd141SGleb Smirnoff 		printf("failed in ng_con_part2(C)\n");
14631acb27c6SJulian Elischer 		ng_destroy_hook(hook);	/* also zaps peer */
1464e088dd4cSAlexander Motin 		return (error);		/* item was consumed. */
14651acb27c6SJulian Elischer 	}
14666b795970SJulian Elischer 	hook->hk_flags &= ~HK_INVALID; /* need both to be able to work */
1467e088dd4cSAlexander Motin 	return (0);			/* item was consumed. */
1468e088dd4cSAlexander Motin done:
1469e088dd4cSAlexander Motin 	NG_FREE_ITEM(item);
1470e088dd4cSAlexander Motin 	return (error);
14714cf49a43SJulian Elischer }
14724cf49a43SJulian Elischer 
14734cf49a43SJulian Elischer /*
14746b795970SJulian Elischer  * Connect this node with another node. We assume that this node is
14756b795970SJulian Elischer  * currently locked, as we are only called from an NGM_CONNECT message.
14764cf49a43SJulian Elischer  */
14776b795970SJulian Elischer static int
1478e088dd4cSAlexander Motin ng_con_nodes(item_p item, node_p node, const char *name,
1479e088dd4cSAlexander Motin     node_p node2, const char *name2)
14804cf49a43SJulian Elischer {
14814cf49a43SJulian Elischer 	int	error;
14824cf49a43SJulian Elischer 	hook_p	hook;
14834cf49a43SJulian Elischer 	hook_p	hook2;
14844cf49a43SJulian Elischer 
14851acb27c6SJulian Elischer 	if (ng_findhook(node2, name2) != NULL) {
14861acb27c6SJulian Elischer 		return(EEXIST);
14871acb27c6SJulian Elischer 	}
14883e4084c8SJulian Elischer 	if ((error = ng_add_hook(node, name, &hook)))  /* gives us a ref */
14894cf49a43SJulian Elischer 		return (error);
14906b795970SJulian Elischer 	/* Allocate the other hook and link it up */
14916b795970SJulian Elischer 	NG_ALLOC_HOOK(hook2);
149219724144SGleb Smirnoff 	if (hook2 == NULL) {
14936b795970SJulian Elischer 		TRAP_ERROR();
14946b795970SJulian Elischer 		ng_destroy_hook(hook);	/* XXX check ref counts so far */
14956b795970SJulian Elischer 		NG_HOOK_UNREF(hook);	/* including our ref */
14966b795970SJulian Elischer 		return (ENOMEM);
14976b795970SJulian Elischer 	}
14986b795970SJulian Elischer 	hook2->hk_refs = 1;		/* start with a reference for us. */
14996b795970SJulian Elischer 	hook2->hk_flags = HK_INVALID;
15006b795970SJulian Elischer 	hook2->hk_peer = hook;		/* Link the two together */
15016b795970SJulian Elischer 	hook->hk_peer = hook2;
15026b795970SJulian Elischer 	NG_HOOK_REF(hook);		/* Add a ref for the peer to each*/
15036b795970SJulian Elischer 	NG_HOOK_REF(hook2);
15046b795970SJulian Elischer 	hook2->hk_node = &ng_deadnode;
150587e2c66aSHartmut Brandt 	strlcpy(NG_HOOK_NAME(hook2), name2, NG_HOOKSIZ);
15066b795970SJulian Elischer 
15076b795970SJulian Elischer 	/*
15086b795970SJulian Elischer 	 * Queue the function above.
15096b795970SJulian Elischer 	 * Procesing continues in that function in the lock context of
15106b795970SJulian Elischer 	 * the other node.
15116b795970SJulian Elischer 	 */
1512b332b91fSGleb Smirnoff 	if ((error = ng_send_fn2(node2, hook2, item, &ng_con_part2, NULL, 0,
1513b332b91fSGleb Smirnoff 	    NG_NOFLAGS))) {
15143fb87c24SAlexander Motin 		printf("failed in ng_con_nodes(): %d\n", error);
15153fb87c24SAlexander Motin 		ng_destroy_hook(hook);	/* also zaps peer */
15163fb87c24SAlexander Motin 	}
15176b795970SJulian Elischer 
15186b795970SJulian Elischer 	NG_HOOK_UNREF(hook);		/* Let each hook go if it wants to */
15196b795970SJulian Elischer 	NG_HOOK_UNREF(hook2);
15203fb87c24SAlexander Motin 	return (error);
15214cf49a43SJulian Elischer }
15226b795970SJulian Elischer 
15236b795970SJulian Elischer /*
15246b795970SJulian Elischer  * Make a peer and connect.
15256b795970SJulian Elischer  * We assume that the local node is locked.
15266b795970SJulian Elischer  * The new node probably doesn't need a lock until
15276b795970SJulian Elischer  * it has a hook, because it cannot really have any work until then,
15286b795970SJulian Elischer  * but we should think about it a bit more.
15296b795970SJulian Elischer  *
15306b795970SJulian Elischer  * The problem may come if the other node also fires up
15316b795970SJulian Elischer  * some hardware or a timer or some other source of activation,
15326b795970SJulian Elischer  * also it may already get a command msg via it's ID.
15336b795970SJulian Elischer  *
15346b795970SJulian Elischer  * We could use the same method as ng_con_nodes() but we'd have
15356b795970SJulian Elischer  * to add ability to remove the node when failing. (Not hard, just
15366b795970SJulian Elischer  * make arg1 point to the node to remove).
15376b795970SJulian Elischer  * Unless of course we just ignore failure to connect and leave
15386b795970SJulian Elischer  * an unconnected node?
15396b795970SJulian Elischer  */
15406b795970SJulian Elischer static int
15416b795970SJulian Elischer ng_mkpeer(node_p node, const char *name, const char *name2, char *type)
15426b795970SJulian Elischer {
15436b795970SJulian Elischer 	node_p	node2;
15444c9b5910SGleb Smirnoff 	hook_p	hook1, hook2;
15456b795970SJulian Elischer 	int	error;
15466b795970SJulian Elischer 
15476b795970SJulian Elischer 	if ((error = ng_make_node(type, &node2))) {
15486b795970SJulian Elischer 		return (error);
15496b795970SJulian Elischer 	}
15506b795970SJulian Elischer 
15516b795970SJulian Elischer 	if ((error = ng_add_hook(node, name, &hook1))) { /* gives us a ref */
15521acb27c6SJulian Elischer 		ng_rmnode(node2, NULL, NULL, 0);
15536b795970SJulian Elischer 		return (error);
15546b795970SJulian Elischer 	}
15556b795970SJulian Elischer 
15566b795970SJulian Elischer 	if ((error = ng_add_hook(node2, name2, &hook2))) {
15571acb27c6SJulian Elischer 		ng_rmnode(node2, NULL, NULL, 0);
15586b795970SJulian Elischer 		ng_destroy_hook(hook1);
15596b795970SJulian Elischer 		NG_HOOK_UNREF(hook1);
15606b795970SJulian Elischer 		return (error);
15616b795970SJulian Elischer 	}
15626b795970SJulian Elischer 
15636b795970SJulian Elischer 	/*
15646b795970SJulian Elischer 	 * Actually link the two hooks together.
15656b795970SJulian Elischer 	 */
15666b795970SJulian Elischer 	hook1->hk_peer = hook2;
15676b795970SJulian Elischer 	hook2->hk_peer = hook1;
15686b795970SJulian Elischer 
15696b795970SJulian Elischer 	/* Each hook is referenced by the other */
15706b795970SJulian Elischer 	NG_HOOK_REF(hook1);
15716b795970SJulian Elischer 	NG_HOOK_REF(hook2);
15726b795970SJulian Elischer 
15736b795970SJulian Elischer 	/* Give each node the opportunity to veto the pending connection */
15746b795970SJulian Elischer 	if (hook1->hk_node->nd_type->connect) {
15756b795970SJulian Elischer 		error = (*hook1->hk_node->nd_type->connect) (hook1);
15766b795970SJulian Elischer 	}
15776b795970SJulian Elischer 
15786b795970SJulian Elischer 	if ((error == 0) && hook2->hk_node->nd_type->connect) {
15796b795970SJulian Elischer 		error = (*hook2->hk_node->nd_type->connect) (hook2);
15806b795970SJulian Elischer 	}
15813e4084c8SJulian Elischer 
15823e4084c8SJulian Elischer 	/*
15833e4084c8SJulian Elischer 	 * drop the references we were holding on the two hooks.
15843e4084c8SJulian Elischer 	 */
15856b795970SJulian Elischer 	if (error) {
15866b795970SJulian Elischer 		ng_destroy_hook(hook2);	/* also zaps hook1 */
15871acb27c6SJulian Elischer 		ng_rmnode(node2, NULL, NULL, 0);
15886b795970SJulian Elischer 	} else {
15896b795970SJulian Elischer 		/* As a last act, allow the hooks to be used */
15906b795970SJulian Elischer 		hook1->hk_flags &= ~HK_INVALID;
15916b795970SJulian Elischer 		hook2->hk_flags &= ~HK_INVALID;
15926b795970SJulian Elischer 	}
15936b795970SJulian Elischer 	NG_HOOK_UNREF(hook1);
15943e4084c8SJulian Elischer 	NG_HOOK_UNREF(hook2);
15953e4084c8SJulian Elischer 	return (error);
15964cf49a43SJulian Elischer }
15976b795970SJulian Elischer 
1598069154d5SJulian Elischer /************************************************************************
1599069154d5SJulian Elischer 		Utility routines to send self messages
1600069154d5SJulian Elischer ************************************************************************/
1601069154d5SJulian Elischer 
16021acb27c6SJulian Elischer /* Shut this node down as soon as everyone is clear of it */
160364efc707SRobert Watson /* Should add arg "immediately" to jump the queue */
1604069154d5SJulian Elischer int
160515cea89fSAlexander Motin ng_rmnode_self(node_p node)
1606069154d5SJulian Elischer {
16071acb27c6SJulian Elischer 	int		error;
16084cf49a43SJulian Elischer 
16091acb27c6SJulian Elischer 	if (node == &ng_deadnode)
16101acb27c6SJulian Elischer 		return (0);
1611be4252b3SJulian Elischer 	node->nd_flags |= NGF_INVALID;
1612be4252b3SJulian Elischer 	if (node->nd_flags & NGF_CLOSING)
16131acb27c6SJulian Elischer 		return (0);
1614069154d5SJulian Elischer 
161515cea89fSAlexander Motin 	error = ng_send_fn(node, NULL, &ng_rmnode, NULL, 0);
16161acb27c6SJulian Elischer 	return (error);
1617069154d5SJulian Elischer }
1618069154d5SJulian Elischer 
16191acb27c6SJulian Elischer static void
16206b795970SJulian Elischer ng_rmhook_part2(node_p node, hook_p hook, void *arg1, int arg2)
16216b795970SJulian Elischer {
16226b795970SJulian Elischer 	ng_destroy_hook(hook);
16231acb27c6SJulian Elischer 	return ;
16246b795970SJulian Elischer }
16256b795970SJulian Elischer 
1626954c4772SJulian Elischer int
1627954c4772SJulian Elischer ng_rmhook_self(hook_p hook)
1628954c4772SJulian Elischer {
16296b795970SJulian Elischer 	int		error;
1630954c4772SJulian Elischer 	node_p node = NG_HOOK_NODE(hook);
1631954c4772SJulian Elischer 
16326b795970SJulian Elischer 	if (node == &ng_deadnode)
16336b795970SJulian Elischer 		return (0);
16346b795970SJulian Elischer 
16356b795970SJulian Elischer 	error = ng_send_fn(node, hook, &ng_rmhook_part2, NULL, 0);
16366b795970SJulian Elischer 	return (error);
1637954c4772SJulian Elischer }
1638954c4772SJulian Elischer 
1639069154d5SJulian Elischer /***********************************************************************
16404cf49a43SJulian Elischer  * Parse and verify a string of the form:  <NODE:><PATH>
16414cf49a43SJulian Elischer  *
16424cf49a43SJulian Elischer  * Such a string can refer to a specific node or a specific hook
16434cf49a43SJulian Elischer  * on a specific node, depending on how you look at it. In the
16444cf49a43SJulian Elischer  * latter case, the PATH component must not end in a dot.
16454cf49a43SJulian Elischer  *
16464cf49a43SJulian Elischer  * Both <NODE:> and <PATH> are optional. The <PATH> is a string
16474cf49a43SJulian Elischer  * of hook names separated by dots. This breaks out the original
16484cf49a43SJulian Elischer  * string, setting *nodep to "NODE" (or NULL if none) and *pathp
16494cf49a43SJulian Elischer  * to "PATH" (or NULL if degenerate). Also, *hookp will point to
16504cf49a43SJulian Elischer  * the final hook component of <PATH>, if any, otherwise NULL.
16514cf49a43SJulian Elischer  *
16524cf49a43SJulian Elischer  * This returns -1 if the path is malformed. The char ** are optional.
1653069154d5SJulian Elischer  ***********************************************************************/
16544cf49a43SJulian Elischer int
16554cf49a43SJulian Elischer ng_path_parse(char *addr, char **nodep, char **pathp, char **hookp)
16564cf49a43SJulian Elischer {
16574cf49a43SJulian Elischer 	char	*node, *path, *hook;
16584cf49a43SJulian Elischer 	int	k;
16594cf49a43SJulian Elischer 
16604cf49a43SJulian Elischer 	/*
16614cf49a43SJulian Elischer 	 * Extract absolute NODE, if any
16624cf49a43SJulian Elischer 	 */
16634cf49a43SJulian Elischer 	for (path = addr; *path && *path != ':'; path++);
16644cf49a43SJulian Elischer 	if (*path) {
16654cf49a43SJulian Elischer 		node = addr;	/* Here's the NODE */
16664cf49a43SJulian Elischer 		*path++ = '\0';	/* Here's the PATH */
16674cf49a43SJulian Elischer 
16684cf49a43SJulian Elischer 		/* Node name must not be empty */
16694cf49a43SJulian Elischer 		if (!*node)
16704cf49a43SJulian Elischer 			return -1;
16714cf49a43SJulian Elischer 
16724cf49a43SJulian Elischer 		/* A name of "." is OK; otherwise '.' not allowed */
16734cf49a43SJulian Elischer 		if (strcmp(node, ".") != 0) {
16744cf49a43SJulian Elischer 			for (k = 0; node[k]; k++)
16754cf49a43SJulian Elischer 				if (node[k] == '.')
16764cf49a43SJulian Elischer 					return -1;
16774cf49a43SJulian Elischer 		}
16784cf49a43SJulian Elischer 	} else {
16794cf49a43SJulian Elischer 		node = NULL;	/* No absolute NODE */
16804cf49a43SJulian Elischer 		path = addr;	/* Here's the PATH */
16814cf49a43SJulian Elischer 	}
16824cf49a43SJulian Elischer 
16834cf49a43SJulian Elischer 	/* Snoop for illegal characters in PATH */
16844cf49a43SJulian Elischer 	for (k = 0; path[k]; k++)
16854cf49a43SJulian Elischer 		if (path[k] == ':')
16864cf49a43SJulian Elischer 			return -1;
16874cf49a43SJulian Elischer 
16884cf49a43SJulian Elischer 	/* Check for no repeated dots in PATH */
16894cf49a43SJulian Elischer 	for (k = 0; path[k]; k++)
16904cf49a43SJulian Elischer 		if (path[k] == '.' && path[k + 1] == '.')
16914cf49a43SJulian Elischer 			return -1;
16924cf49a43SJulian Elischer 
16934cf49a43SJulian Elischer 	/* Remove extra (degenerate) dots from beginning or end of PATH */
16944cf49a43SJulian Elischer 	if (path[0] == '.')
16954cf49a43SJulian Elischer 		path++;
16964cf49a43SJulian Elischer 	if (*path && path[strlen(path) - 1] == '.')
16974cf49a43SJulian Elischer 		path[strlen(path) - 1] = 0;
16984cf49a43SJulian Elischer 
16994cf49a43SJulian Elischer 	/* If PATH has a dot, then we're not talking about a hook */
17004cf49a43SJulian Elischer 	if (*path) {
17014cf49a43SJulian Elischer 		for (hook = path, k = 0; path[k]; k++)
17024cf49a43SJulian Elischer 			if (path[k] == '.') {
17034cf49a43SJulian Elischer 				hook = NULL;
17044cf49a43SJulian Elischer 				break;
17054cf49a43SJulian Elischer 			}
17064cf49a43SJulian Elischer 	} else
17074cf49a43SJulian Elischer 		path = hook = NULL;
17084cf49a43SJulian Elischer 
17094cf49a43SJulian Elischer 	/* Done */
17104cf49a43SJulian Elischer 	if (nodep)
17114cf49a43SJulian Elischer 		*nodep = node;
17124cf49a43SJulian Elischer 	if (pathp)
17134cf49a43SJulian Elischer 		*pathp = path;
17144cf49a43SJulian Elischer 	if (hookp)
17154cf49a43SJulian Elischer 		*hookp = hook;
17164cf49a43SJulian Elischer 	return (0);
17174cf49a43SJulian Elischer }
17184cf49a43SJulian Elischer 
17194cf49a43SJulian Elischer /*
17204cf49a43SJulian Elischer  * Given a path, which may be absolute or relative, and a starting node,
1721069154d5SJulian Elischer  * return the destination node.
17224cf49a43SJulian Elischer  */
17234cf49a43SJulian Elischer int
17244bd1b557SGleb Smirnoff ng_path2noderef(node_p here, const char *address, node_p *destp,
17254bd1b557SGleb Smirnoff     hook_p *lasthook)
17264cf49a43SJulian Elischer {
172787e2c66aSHartmut Brandt 	char    fullpath[NG_PATHSIZ];
1728a7da736aSGleb Smirnoff 	char   *nodename, *path;
1729069154d5SJulian Elischer 	node_p  node, oldnode;
17304cf49a43SJulian Elischer 
17314cf49a43SJulian Elischer 	/* Initialize */
173230400f03SJulian Elischer 	if (destp == NULL) {
17336b795970SJulian Elischer 		TRAP_ERROR();
17344cf49a43SJulian Elischer 		return EINVAL;
173530400f03SJulian Elischer 	}
17364cf49a43SJulian Elischer 	*destp = NULL;
17374cf49a43SJulian Elischer 
17384cf49a43SJulian Elischer 	/* Make a writable copy of address for ng_path_parse() */
17394cf49a43SJulian Elischer 	strncpy(fullpath, address, sizeof(fullpath) - 1);
17404cf49a43SJulian Elischer 	fullpath[sizeof(fullpath) - 1] = '\0';
17414cf49a43SJulian Elischer 
17424cf49a43SJulian Elischer 	/* Parse out node and sequence of hooks */
17434cf49a43SJulian Elischer 	if (ng_path_parse(fullpath, &nodename, &path, NULL) < 0) {
17446b795970SJulian Elischer 		TRAP_ERROR();
17454cf49a43SJulian Elischer 		return EINVAL;
17464cf49a43SJulian Elischer 	}
17474cf49a43SJulian Elischer 
1748069154d5SJulian Elischer 	/*
1749069154d5SJulian Elischer 	 * For an absolute address, jump to the starting node.
1750069154d5SJulian Elischer 	 * Note that this holds a reference on the node for us.
1751069154d5SJulian Elischer 	 * Don't forget to drop the reference if we don't need it.
1752069154d5SJulian Elischer 	 */
17534cf49a43SJulian Elischer 	if (nodename) {
1754069154d5SJulian Elischer 		node = ng_name2noderef(here, nodename);
17554cf49a43SJulian Elischer 		if (node == NULL) {
17566b795970SJulian Elischer 			TRAP_ERROR();
17574cf49a43SJulian Elischer 			return (ENOENT);
17584cf49a43SJulian Elischer 		}
1759069154d5SJulian Elischer 	} else {
1760069154d5SJulian Elischer 		if (here == NULL) {
17616b795970SJulian Elischer 			TRAP_ERROR();
1762069154d5SJulian Elischer 			return (EINVAL);
1763069154d5SJulian Elischer 		}
17644cf49a43SJulian Elischer 		node = here;
176530400f03SJulian Elischer 		NG_NODE_REF(node);
1766069154d5SJulian Elischer 	}
17674cf49a43SJulian Elischer 
1768a7da736aSGleb Smirnoff 	if (path == NULL) {
1769a7da736aSGleb Smirnoff 		if (lasthook != NULL)
1770a7da736aSGleb Smirnoff 			*lasthook = NULL;
1771a7da736aSGleb Smirnoff 		*destp = node;
1772a7da736aSGleb Smirnoff 		return (0);
1773a7da736aSGleb Smirnoff 	}
1774a7da736aSGleb Smirnoff 
1775069154d5SJulian Elischer 	/*
1776069154d5SJulian Elischer 	 * Now follow the sequence of hooks
1777a7da736aSGleb Smirnoff 	 *
1778a7da736aSGleb Smirnoff 	 * XXXGL: The path may demolish as we go the sequence, but if
1779a7da736aSGleb Smirnoff 	 * we hold the topology mutex at critical places, then, I hope,
1780a7da736aSGleb Smirnoff 	 * we would always have valid pointers in hand, although the
1781a7da736aSGleb Smirnoff 	 * path behind us may no longer exist.
1782069154d5SJulian Elischer 	 */
1783a7da736aSGleb Smirnoff 	for (;;) {
1784a7da736aSGleb Smirnoff 		hook_p hook;
17854cf49a43SJulian Elischer 		char *segment;
17864cf49a43SJulian Elischer 
17874cf49a43SJulian Elischer 		/*
17884cf49a43SJulian Elischer 		 * Break out the next path segment. Replace the dot we just
1789a7da736aSGleb Smirnoff 		 * found with a NUL; "path" points to the next segment (or the
17904cf49a43SJulian Elischer 		 * NUL at the end).
17914cf49a43SJulian Elischer 		 */
1792a7da736aSGleb Smirnoff 		for (segment = path; *path != '\0'; path++) {
1793a7da736aSGleb Smirnoff 			if (*path == '.') {
1794a7da736aSGleb Smirnoff 				*path++ = '\0';
17954cf49a43SJulian Elischer 				break;
17964cf49a43SJulian Elischer 			}
17974cf49a43SJulian Elischer 		}
17984cf49a43SJulian Elischer 
17994cf49a43SJulian Elischer 		/* We have a segment, so look for a hook by that name */
1800899e9c4eSArchie Cobbs 		hook = ng_findhook(node, segment);
18014cf49a43SJulian Elischer 
1802d2fd0788SAlexander V. Chernikov 		TOPOLOGY_WLOCK();
18034cf49a43SJulian Elischer 		/* Can't get there from here... */
18044bd1b557SGleb Smirnoff 		if (hook == NULL || NG_HOOK_PEER(hook) == NULL ||
18054bd1b557SGleb Smirnoff 		    NG_HOOK_NOT_VALID(hook) ||
18064bd1b557SGleb Smirnoff 		    NG_HOOK_NOT_VALID(NG_HOOK_PEER(hook))) {
18076b795970SJulian Elischer 			TRAP_ERROR();
180830400f03SJulian Elischer 			NG_NODE_UNREF(node);
1809d2fd0788SAlexander V. Chernikov 			TOPOLOGY_WUNLOCK();
18104cf49a43SJulian Elischer 			return (ENOENT);
18114cf49a43SJulian Elischer 		}
18124cf49a43SJulian Elischer 
1813069154d5SJulian Elischer 		/*
1814069154d5SJulian Elischer 		 * Hop on over to the next node
1815069154d5SJulian Elischer 		 * XXX
1816069154d5SJulian Elischer 		 * Big race conditions here as hooks and nodes go away
1817069154d5SJulian Elischer 		 * *** Idea.. store an ng_ID_t in each hook and use that
1818069154d5SJulian Elischer 		 * instead of the direct hook in this crawl?
1819069154d5SJulian Elischer 		 */
1820069154d5SJulian Elischer 		oldnode = node;
182130400f03SJulian Elischer 		if ((node = NG_PEER_NODE(hook)))
182230400f03SJulian Elischer 			NG_NODE_REF(node);	/* XXX RACE */
182330400f03SJulian Elischer 		NG_NODE_UNREF(oldnode);	/* XXX another race */
182430400f03SJulian Elischer 		if (NG_NODE_NOT_VALID(node)) {
182530400f03SJulian Elischer 			NG_NODE_UNREF(node);	/* XXX more races */
1826d2fd0788SAlexander V. Chernikov 			TOPOLOGY_WUNLOCK();
18276b795970SJulian Elischer 			TRAP_ERROR();
18284cf49a43SJulian Elischer 			return (ENXIO);
18294cf49a43SJulian Elischer 		}
18304cf49a43SJulian Elischer 
1831a7da736aSGleb Smirnoff 		if (*path == '\0') {
1832a7da736aSGleb Smirnoff 			if (lasthook != NULL) {
1833a7da736aSGleb Smirnoff 				if (hook != NULL) {
1834a7da736aSGleb Smirnoff 					*lasthook = NG_HOOK_PEER(hook);
1835a7da736aSGleb Smirnoff 					NG_HOOK_REF(*lasthook);
1836a7da736aSGleb Smirnoff 				} else
1837a7da736aSGleb Smirnoff 					*lasthook = NULL;
1838a7da736aSGleb Smirnoff 			}
1839d2fd0788SAlexander V. Chernikov 			TOPOLOGY_WUNLOCK();
18404cf49a43SJulian Elischer 			*destp = node;
1841069154d5SJulian Elischer 			return (0);
1842069154d5SJulian Elischer 		}
1843d2fd0788SAlexander V. Chernikov 		TOPOLOGY_WUNLOCK();
1844a7da736aSGleb Smirnoff 	}
1845a7da736aSGleb Smirnoff }
1846069154d5SJulian Elischer 
1847069154d5SJulian Elischer /***************************************************************\
1848069154d5SJulian Elischer * Input queue handling.
1849069154d5SJulian Elischer * All activities are submitted to the node via the input queue
1850069154d5SJulian Elischer * which implements a multiple-reader/single-writer gate.
185164efc707SRobert Watson * Items which cannot be handled immediately are queued.
1852069154d5SJulian Elischer *
1853069154d5SJulian Elischer * read-write queue locking inline functions			*
1854069154d5SJulian Elischer \***************************************************************/
1855069154d5SJulian Elischer 
18569852972bSAlexander Motin static __inline void	ng_queue_rw(node_p node, item_p  item, int rw);
18579852972bSAlexander Motin static __inline item_p	ng_dequeue(node_p node, int *rw);
18589852972bSAlexander Motin static __inline item_p	ng_acquire_read(node_p node, item_p  item);
18599852972bSAlexander Motin static __inline item_p	ng_acquire_write(node_p node, item_p  item);
18609852972bSAlexander Motin static __inline void	ng_leave_read(node_p node);
18619852972bSAlexander Motin static __inline void	ng_leave_write(node_p node);
1862069154d5SJulian Elischer 
1863069154d5SJulian Elischer /*
1864069154d5SJulian Elischer  * Definition of the bits fields in the ng_queue flag word.
1865069154d5SJulian Elischer  * Defined here rather than in netgraph.h because no-one should fiddle
1866069154d5SJulian Elischer  * with them.
1867069154d5SJulian Elischer  *
1868b57a7965SJulian Elischer  * The ordering here may be important! don't shuffle these.
1869069154d5SJulian Elischer  */
1870069154d5SJulian Elischer /*-
1871069154d5SJulian Elischer  Safety Barrier--------+ (adjustable to suit taste) (not used yet)
1872069154d5SJulian Elischer                        |
1873069154d5SJulian Elischer                        V
1874069154d5SJulian Elischer +-------+-------+-------+-------+-------+-------+-------+-------+
18754be59335SGleb Smirnoff   | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
18764be59335SGleb Smirnoff   | |A|c|t|i|v|e| |R|e|a|d|e|r| |C|o|u|n|t| | | | | | | | | |P|A|
18774be59335SGleb Smirnoff   | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |O|W|
1878069154d5SJulian Elischer +-------+-------+-------+-------+-------+-------+-------+-------+
18794be59335SGleb Smirnoff   \___________________________ ____________________________/ | |
18804be59335SGleb Smirnoff                             V                                | |
18814be59335SGleb Smirnoff                   [active reader count]                      | |
1882069154d5SJulian Elischer                                                              | |
18834be59335SGleb Smirnoff             Operation Pending -------------------------------+ |
1884069154d5SJulian Elischer                                                                |
18854be59335SGleb Smirnoff           Active Writer ---------------------------------------+
1886b57a7965SJulian Elischer 
18878f9ac44aSAlexander Motin Node queue has such semantics:
18888f9ac44aSAlexander Motin - All flags modifications are atomic.
18898f9ac44aSAlexander Motin - Reader count can be incremented only if there is no writer or pending flags.
18908f9ac44aSAlexander Motin   As soon as this can't be done with single operation, it is implemented with
18918f9ac44aSAlexander Motin   spin loop and atomic_cmpset().
18928f9ac44aSAlexander Motin - Writer flag can be set only if there is no any bits set.
18938f9ac44aSAlexander Motin   It is implemented with atomic_cmpset().
18948f9ac44aSAlexander Motin - Pending flag can be set any time, but to avoid collision on queue processing
18958f9ac44aSAlexander Motin   all queue fields are protected by the mutex.
18968f9ac44aSAlexander Motin - Queue processing thread reads queue holding the mutex, but releases it while
18978f9ac44aSAlexander Motin   processing. When queue is empty pending flag is removed.
1898069154d5SJulian Elischer */
18998f9ac44aSAlexander Motin 
19004be59335SGleb Smirnoff #define WRITER_ACTIVE	0x00000001
19014be59335SGleb Smirnoff #define OP_PENDING	0x00000002
19024be59335SGleb Smirnoff #define READER_INCREMENT 0x00000004
19034be59335SGleb Smirnoff #define READER_MASK	0xfffffffc	/* Not valid if WRITER_ACTIVE is set */
19044be59335SGleb Smirnoff #define SAFETY_BARRIER	0x00100000	/* 128K items queued should be enough */
1905b57a7965SJulian Elischer 
1906b57a7965SJulian Elischer /* Defines of more elaborate states on the queue */
19074be59335SGleb Smirnoff /* Mask of bits a new read cares about */
19084be59335SGleb Smirnoff #define NGQ_RMASK	(WRITER_ACTIVE|OP_PENDING)
1909b57a7965SJulian Elischer 
19104be59335SGleb Smirnoff /* Mask of bits a new write cares about */
1911b57a7965SJulian Elischer #define NGQ_WMASK	(NGQ_RMASK|READER_MASK)
1912b57a7965SJulian Elischer 
19134be59335SGleb Smirnoff /* Test to decide if there is something on the queue. */
19144be59335SGleb Smirnoff #define QUEUE_ACTIVE(QP) ((QP)->q_flags & OP_PENDING)
19154be59335SGleb Smirnoff 
19164be59335SGleb Smirnoff /* How to decide what the next queued item is. */
19179852972bSAlexander Motin #define HEAD_IS_READER(QP)  NGI_QUEUED_READER(STAILQ_FIRST(&(QP)->queue))
19189852972bSAlexander Motin #define HEAD_IS_WRITER(QP)  NGI_QUEUED_WRITER(STAILQ_FIRST(&(QP)->queue)) /* notused */
19194be59335SGleb Smirnoff 
19204be59335SGleb Smirnoff /* Read the status to decide if the next item on the queue can now run. */
19214be59335SGleb Smirnoff #define QUEUED_READER_CAN_PROCEED(QP)			\
19224be59335SGleb Smirnoff 		(((QP)->q_flags & (NGQ_RMASK & ~OP_PENDING)) == 0)
19234be59335SGleb Smirnoff #define QUEUED_WRITER_CAN_PROCEED(QP)			\
19244be59335SGleb Smirnoff 		(((QP)->q_flags & (NGQ_WMASK & ~OP_PENDING)) == 0)
1925b57a7965SJulian Elischer 
1926b57a7965SJulian Elischer /* Is there a chance of getting ANY work off the queue? */
19274be59335SGleb Smirnoff #define NEXT_QUEUED_ITEM_CAN_PROCEED(QP)				\
19284be59335SGleb Smirnoff 	((HEAD_IS_READER(QP)) ? QUEUED_READER_CAN_PROCEED(QP) :		\
1929394cb30aSAlexander Motin 				QUEUED_WRITER_CAN_PROCEED(QP))
19304be59335SGleb Smirnoff 
1931714fb865SGleb Smirnoff #define NGQRW_R 0
1932714fb865SGleb Smirnoff #define NGQRW_W 1
1933714fb865SGleb Smirnoff 
19349852972bSAlexander Motin #define NGQ2_WORKQ	0x00000001
19359852972bSAlexander Motin 
1936069154d5SJulian Elischer /*
1937069154d5SJulian Elischer  * Taking into account the current state of the queue and node, possibly take
1938069154d5SJulian Elischer  * the next entry off the queue and return it. Return NULL if there was
1939069154d5SJulian Elischer  * nothing we could return, either because there really was nothing there, or
1940069154d5SJulian Elischer  * because the node was in a state where it cannot yet process the next item
1941069154d5SJulian Elischer  * on the queue.
1942069154d5SJulian Elischer  */
1943069154d5SJulian Elischer static __inline item_p
19449852972bSAlexander Motin ng_dequeue(node_p node, int *rw)
1945069154d5SJulian Elischer {
1946069154d5SJulian Elischer 	item_p item;
19479852972bSAlexander Motin 	struct ng_queue *ngq = &node->nd_input_queue;
1948b57a7965SJulian Elischer 
19498f9ac44aSAlexander Motin 	/* This MUST be called with the mutex held. */
1950d58e678fSGleb Smirnoff 	mtx_assert(&ngq->q_mtx, MA_OWNED);
19518f9ac44aSAlexander Motin 
19528f9ac44aSAlexander Motin 	/* If there is nothing queued, then just return. */
19534be59335SGleb Smirnoff 	if (!QUEUE_ACTIVE(ngq)) {
19542955ee18SGleb Smirnoff 		CTR4(KTR_NET, "%20s: node [%x] (%p) queue empty; "
19552955ee18SGleb Smirnoff 		    "queue flags 0x%lx", __func__,
19569852972bSAlexander Motin 		    node->nd_ID, node, ngq->q_flags);
19574be59335SGleb Smirnoff 		return (NULL);
19584be59335SGleb Smirnoff 	}
1959d58e678fSGleb Smirnoff 
19604be59335SGleb Smirnoff 	/*
19614be59335SGleb Smirnoff 	 * From here, we can assume there is a head item.
19624be59335SGleb Smirnoff 	 * We need to find out what it is and if it can be dequeued, given
19634be59335SGleb Smirnoff 	 * the current state of the node.
19644be59335SGleb Smirnoff 	 */
19654be59335SGleb Smirnoff 	if (HEAD_IS_READER(ngq)) {
1966394cb30aSAlexander Motin 		while (1) {
1967394cb30aSAlexander Motin 			long t = ngq->q_flags;
1968394cb30aSAlexander Motin 			if (t & WRITER_ACTIVE) {
19698f9ac44aSAlexander Motin 				/* There is writer, reader can't proceed. */
19704bd1b557SGleb Smirnoff 				CTR4(KTR_NET, "%20s: node [%x] (%p) queued "
19714bd1b557SGleb Smirnoff 				    "reader can't proceed; queue flags 0x%lx",
19724bd1b557SGleb Smirnoff 				    __func__, node->nd_ID, node, t);
19734be59335SGleb Smirnoff 				return (NULL);
19744be59335SGleb Smirnoff 			}
19759852972bSAlexander Motin 			if (atomic_cmpset_acq_int(&ngq->q_flags, t,
1976394cb30aSAlexander Motin 			    t + READER_INCREMENT))
1977394cb30aSAlexander Motin 				break;
1978394cb30aSAlexander Motin 			cpu_spinwait();
1979394cb30aSAlexander Motin 		}
19808f9ac44aSAlexander Motin 		/* We have got reader lock for the node. */
1981714fb865SGleb Smirnoff 		*rw = NGQRW_R;
19829852972bSAlexander Motin 	} else if (atomic_cmpset_acq_int(&ngq->q_flags, OP_PENDING,
1983394cb30aSAlexander Motin 	    OP_PENDING + WRITER_ACTIVE)) {
19848f9ac44aSAlexander Motin 		/* We have got writer lock for the node. */
1985714fb865SGleb Smirnoff 		*rw = NGQRW_W;
1986069154d5SJulian Elischer 	} else {
19878f9ac44aSAlexander Motin 		/* There is somebody other, writer can't proceed. */
19884bd1b557SGleb Smirnoff 		CTR4(KTR_NET, "%20s: node [%x] (%p) queued writer can't "
19894bd1b557SGleb Smirnoff 		    "proceed; queue flags 0x%lx", __func__, node->nd_ID, node,
19904bd1b557SGleb Smirnoff 		    ngq->q_flags);
19914be59335SGleb Smirnoff 		return (NULL);
19924cf49a43SJulian Elischer 	}
19934cf49a43SJulian Elischer 
19944cf49a43SJulian Elischer 	/*
1995069154d5SJulian Elischer 	 * Now we dequeue the request (whatever it may be) and correct the
1996069154d5SJulian Elischer 	 * pending flags and the next and last pointers.
19974cf49a43SJulian Elischer 	 */
19989852972bSAlexander Motin 	item = STAILQ_FIRST(&ngq->queue);
19999852972bSAlexander Motin 	STAILQ_REMOVE_HEAD(&ngq->queue, el_next);
20009852972bSAlexander Motin 	if (STAILQ_EMPTY(&ngq->queue))
20019852972bSAlexander Motin 		atomic_clear_int(&ngq->q_flags, OP_PENDING);
20024bd1b557SGleb Smirnoff 	CTR6(KTR_NET, "%20s: node [%x] (%p) returning item %p as %s; queue "
20034bd1b557SGleb Smirnoff 	    "flags 0x%lx", __func__, node->nd_ID, node, item, *rw ? "WRITER" :
20044bd1b557SGleb Smirnoff 	    "READER", ngq->q_flags);
2005069154d5SJulian Elischer 	return (item);
2006069154d5SJulian Elischer }
2007859a4d16SJulian Elischer 
2008859a4d16SJulian Elischer /*
20098f9ac44aSAlexander Motin  * Queue a packet to be picked up later by someone else.
20108f9ac44aSAlexander Motin  * If the queue could be run now, add node to the queue handler's worklist.
2011859a4d16SJulian Elischer  */
2012069154d5SJulian Elischer static __inline void
20139852972bSAlexander Motin ng_queue_rw(node_p node, item_p  item, int rw)
2014069154d5SJulian Elischer {
20159852972bSAlexander Motin 	struct ng_queue *ngq = &node->nd_input_queue;
20164be59335SGleb Smirnoff 	if (rw == NGQRW_W)
20174be59335SGleb Smirnoff 		NGI_SET_WRITER(item);
20184be59335SGleb Smirnoff 	else
20194be59335SGleb Smirnoff 		NGI_SET_READER(item);
20203fabe28bSRyan Stone 	item->depth = 1;
2021394cb30aSAlexander Motin 
2022394cb30aSAlexander Motin 	NG_QUEUE_LOCK(ngq);
2023394cb30aSAlexander Motin 	/* Set OP_PENDING flag and enqueue the item. */
20249852972bSAlexander Motin 	atomic_set_int(&ngq->q_flags, OP_PENDING);
20259852972bSAlexander Motin 	STAILQ_INSERT_TAIL(&ngq->queue, item, el_next);
2026394cb30aSAlexander Motin 
20272955ee18SGleb Smirnoff 	CTR5(KTR_NET, "%20s: node [%x] (%p) queued item %p as %s", __func__,
20289852972bSAlexander Motin 	    node->nd_ID, node, item, rw ? "WRITER" : "READER" );
20294be59335SGleb Smirnoff 
20304be59335SGleb Smirnoff 	/*
20314be59335SGleb Smirnoff 	 * We can take the worklist lock with the node locked
20324be59335SGleb Smirnoff 	 * BUT NOT THE REVERSE!
20334be59335SGleb Smirnoff 	 */
20344be59335SGleb Smirnoff 	if (NEXT_QUEUED_ITEM_CAN_PROCEED(ngq))
20359852972bSAlexander Motin 		ng_worklist_add(node);
2036394cb30aSAlexander Motin 	NG_QUEUE_UNLOCK(ngq);
2037069154d5SJulian Elischer }
2038069154d5SJulian Elischer 
20398f9ac44aSAlexander Motin /* Acquire reader lock on node. If node is busy, queue the packet. */
2040069154d5SJulian Elischer static __inline item_p
20419852972bSAlexander Motin ng_acquire_read(node_p node, item_p item)
2042069154d5SJulian Elischer {
20439852972bSAlexander Motin 	KASSERT(node != &ng_deadnode,
2044ac5dd141SGleb Smirnoff 	    ("%s: working on deadnode", __func__));
2045069154d5SJulian Elischer 
2046394cb30aSAlexander Motin 	/* Reader needs node without writer and pending items. */
20474bd1b557SGleb Smirnoff 	for (;;) {
20489852972bSAlexander Motin 		long t = node->nd_input_queue.q_flags;
2049394cb30aSAlexander Motin 		if (t & NGQ_RMASK)
2050394cb30aSAlexander Motin 			break; /* Node is not ready for reader. */
20514bd1b557SGleb Smirnoff 		if (atomic_cmpset_acq_int(&node->nd_input_queue.q_flags, t,
20524bd1b557SGleb Smirnoff 		    t + READER_INCREMENT)) {
2053069154d5SJulian Elischer 	    		/* Successfully grabbed node */
2054394cb30aSAlexander Motin 			CTR4(KTR_NET, "%20s: node [%x] (%p) acquired item %p",
20559852972bSAlexander Motin 			    __func__, node->nd_ID, node, item);
2056069154d5SJulian Elischer 			return (item);
2057069154d5SJulian Elischer 		}
2058394cb30aSAlexander Motin 		cpu_spinwait();
205974b8d63dSPedro F. Giffuni 	}
2060069154d5SJulian Elischer 
2061394cb30aSAlexander Motin 	/* Queue the request for later. */
20629852972bSAlexander Motin 	ng_queue_rw(node, item, NGQRW_R);
2063f912c0f7SGleb Smirnoff 
2064f912c0f7SGleb Smirnoff 	return (NULL);
2065069154d5SJulian Elischer }
2066069154d5SJulian Elischer 
20678f9ac44aSAlexander Motin /* Acquire writer lock on node. If node is busy, queue the packet. */
2068069154d5SJulian Elischer static __inline item_p
20699852972bSAlexander Motin ng_acquire_write(node_p node, item_p item)
2070069154d5SJulian Elischer {
20719852972bSAlexander Motin 	KASSERT(node != &ng_deadnode,
2072ac5dd141SGleb Smirnoff 	    ("%s: working on deadnode", __func__));
2073ac5dd141SGleb Smirnoff 
2074394cb30aSAlexander Motin 	/* Writer needs completely idle node. */
20754bd1b557SGleb Smirnoff 	if (atomic_cmpset_acq_int(&node->nd_input_queue.q_flags, 0,
20764bd1b557SGleb Smirnoff 	    WRITER_ACTIVE)) {
2077394cb30aSAlexander Motin 	    	/* Successfully grabbed node */
20782955ee18SGleb Smirnoff 		CTR4(KTR_NET, "%20s: node [%x] (%p) acquired item %p",
20799852972bSAlexander Motin 		    __func__, node->nd_ID, node, item);
2080069154d5SJulian Elischer 		return (item);
2081069154d5SJulian Elischer 	}
2082069154d5SJulian Elischer 
2083394cb30aSAlexander Motin 	/* Queue the request for later. */
20849852972bSAlexander Motin 	ng_queue_rw(node, item, NGQRW_W);
2085f912c0f7SGleb Smirnoff 
2086f912c0f7SGleb Smirnoff 	return (NULL);
2087069154d5SJulian Elischer }
2088069154d5SJulian Elischer 
2089262dfd7fSJulian Elischer #if 0
2090262dfd7fSJulian Elischer static __inline item_p
20919852972bSAlexander Motin ng_upgrade_write(node_p node, item_p item)
2092262dfd7fSJulian Elischer {
20939852972bSAlexander Motin 	struct ng_queue *ngq = &node->nd_input_queue;
20949852972bSAlexander Motin 	KASSERT(node != &ng_deadnode,
2095262dfd7fSJulian Elischer 	    ("%s: working on deadnode", __func__));
2096262dfd7fSJulian Elischer 
2097262dfd7fSJulian Elischer 	NGI_SET_WRITER(item);
2098262dfd7fSJulian Elischer 
20999852972bSAlexander Motin 	NG_QUEUE_LOCK(ngq);
2100262dfd7fSJulian Elischer 
2101262dfd7fSJulian Elischer 	/*
2102262dfd7fSJulian Elischer 	 * There will never be no readers as we are there ourselves.
2103262dfd7fSJulian Elischer 	 * Set the WRITER_ACTIVE flags ASAP to block out fast track readers.
2104262dfd7fSJulian Elischer 	 * The caller we are running from will call ng_leave_read()
2105262dfd7fSJulian Elischer 	 * soon, so we must account for that. We must leave again with the
2106262dfd7fSJulian Elischer 	 * READER lock. If we find other readers, then
2107262dfd7fSJulian Elischer 	 * queue the request for later. However "later" may be rignt now
2108262dfd7fSJulian Elischer 	 * if there are no readers. We don't really care if there are queued
2109262dfd7fSJulian Elischer 	 * items as we will bypass them anyhow.
2110262dfd7fSJulian Elischer 	 */
21119852972bSAlexander Motin 	atomic_add_int(&ngq->q_flags, WRITER_ACTIVE - READER_INCREMENT);
21129852972bSAlexander Motin 	if ((ngq->q_flags & (NGQ_WMASK & ~OP_PENDING)) == WRITER_ACTIVE) {
21139852972bSAlexander Motin 		NG_QUEUE_UNLOCK(ngq);
2114262dfd7fSJulian Elischer 
2115262dfd7fSJulian Elischer 		/* It's just us, act on the item. */
2116262dfd7fSJulian Elischer 		/* will NOT drop writer lock when done */
2117262dfd7fSJulian Elischer 		ng_apply_item(node, item, 0);
2118262dfd7fSJulian Elischer 
2119262dfd7fSJulian Elischer 		/*
2120262dfd7fSJulian Elischer 		 * Having acted on the item, atomically
21214bd1b557SGleb Smirnoff 		 * downgrade back to READER and finish up.
2122262dfd7fSJulian Elischer 	 	 */
21234bd1b557SGleb Smirnoff 		atomic_add_int(&ngq->q_flags, READER_INCREMENT - WRITER_ACTIVE);
2124262dfd7fSJulian Elischer 
2125262dfd7fSJulian Elischer 		/* Our caller will call ng_leave_read() */
2126262dfd7fSJulian Elischer 		return;
2127262dfd7fSJulian Elischer 	}
2128262dfd7fSJulian Elischer 	/*
2129262dfd7fSJulian Elischer 	 * It's not just us active, so queue us AT THE HEAD.
2130262dfd7fSJulian Elischer 	 * "Why?" I hear you ask.
2131262dfd7fSJulian Elischer 	 * Put us at the head of the queue as we've already been
2132262dfd7fSJulian Elischer 	 * through it once. If there is nothing else waiting,
2133262dfd7fSJulian Elischer 	 * set the correct flags.
2134262dfd7fSJulian Elischer 	 */
21359852972bSAlexander Motin 	if (STAILQ_EMPTY(&ngq->queue)) {
2136262dfd7fSJulian Elischer 		/* We've gone from, 0 to 1 item in the queue */
21379852972bSAlexander Motin 		atomic_set_int(&ngq->q_flags, OP_PENDING);
2138262dfd7fSJulian Elischer 
2139262dfd7fSJulian Elischer 		CTR3(KTR_NET, "%20s: node [%x] (%p) set OP_PENDING", __func__,
21409852972bSAlexander Motin 		    node->nd_ID, node);
2141262dfd7fSJulian Elischer 	};
21429852972bSAlexander Motin 	STAILQ_INSERT_HEAD(&ngq->queue, item, el_next);
21439852972bSAlexander Motin 	CTR4(KTR_NET, "%20s: node [%x] (%p) requeued item %p as WRITER",
21449852972bSAlexander Motin 	    __func__, node->nd_ID, node, item );
2145262dfd7fSJulian Elischer 
2146262dfd7fSJulian Elischer 	/* Reverse what we did above. That downgrades us back to reader */
21479852972bSAlexander Motin 	atomic_add_int(&ngq->q_flags, READER_INCREMENT - WRITER_ACTIVE);
2148394cb30aSAlexander Motin 	if (QUEUE_ACTIVE(ngq) && NEXT_QUEUED_ITEM_CAN_PROCEED(ngq))
21499852972bSAlexander Motin 		ng_worklist_add(node);
21509852972bSAlexander Motin 	NG_QUEUE_UNLOCK(ngq);
2151262dfd7fSJulian Elischer 
2152262dfd7fSJulian Elischer 	return;
2153262dfd7fSJulian Elischer }
2154262dfd7fSJulian Elischer #endif
2155262dfd7fSJulian Elischer 
21568f9ac44aSAlexander Motin /* Release reader lock. */
2157069154d5SJulian Elischer static __inline void
21589852972bSAlexander Motin ng_leave_read(node_p node)
2159069154d5SJulian Elischer {
21609852972bSAlexander Motin 	atomic_subtract_rel_int(&node->nd_input_queue.q_flags, READER_INCREMENT);
2161069154d5SJulian Elischer }
2162069154d5SJulian Elischer 
21638f9ac44aSAlexander Motin /* Release writer lock. */
2164069154d5SJulian Elischer static __inline void
21659852972bSAlexander Motin ng_leave_write(node_p node)
2166069154d5SJulian Elischer {
21679852972bSAlexander Motin 	atomic_clear_rel_int(&node->nd_input_queue.q_flags, WRITER_ACTIVE);
2168069154d5SJulian Elischer }
2169069154d5SJulian Elischer 
21708f9ac44aSAlexander Motin /* Purge node queue. Called on node shutdown. */
2171069154d5SJulian Elischer static void
21729852972bSAlexander Motin ng_flush_input_queue(node_p node)
2173069154d5SJulian Elischer {
21749852972bSAlexander Motin 	struct ng_queue *ngq = &node->nd_input_queue;
2175069154d5SJulian Elischer 	item_p item;
2176069154d5SJulian Elischer 
21772c8dda8dSWojciech A. Koszek 	NG_QUEUE_LOCK(ngq);
21789852972bSAlexander Motin 	while ((item = STAILQ_FIRST(&ngq->queue)) != NULL) {
21799852972bSAlexander Motin 		STAILQ_REMOVE_HEAD(&ngq->queue, el_next);
21809852972bSAlexander Motin 		if (STAILQ_EMPTY(&ngq->queue))
21819852972bSAlexander Motin 			atomic_clear_int(&ngq->q_flags, OP_PENDING);
21822c8dda8dSWojciech A. Koszek 		NG_QUEUE_UNLOCK(ngq);
21834be59335SGleb Smirnoff 
21844be59335SGleb Smirnoff 		/* If the item is supplying a callback, call it with an error */
218510e87318SAlexander Motin 		if (item->apply != NULL) {
218610e87318SAlexander Motin 			if (item->depth == 1)
218710e87318SAlexander Motin 				item->apply->error = ENOENT;
218810e87318SAlexander Motin 			if (refcount_release(&item->apply->refs)) {
218910e87318SAlexander Motin 				(*item->apply->apply)(item->apply->context,
219010e87318SAlexander Motin 				    item->apply->error);
219110e87318SAlexander Motin 			}
2192eb2405ddSGleb Smirnoff 		}
2193505fad52SJulian Elischer 		NG_FREE_ITEM(item);
21942c8dda8dSWojciech A. Koszek 		NG_QUEUE_LOCK(ngq);
2195069154d5SJulian Elischer 	}
21962c8dda8dSWojciech A. Koszek 	NG_QUEUE_UNLOCK(ngq);
2197069154d5SJulian Elischer }
2198069154d5SJulian Elischer 
2199069154d5SJulian Elischer /***********************************************************************
2200069154d5SJulian Elischer * Externally visible method for sending or queueing messages or data.
2201069154d5SJulian Elischer ***********************************************************************/
2202069154d5SJulian Elischer 
2203069154d5SJulian Elischer /*
22041acb27c6SJulian Elischer  * The module code should have filled out the item correctly by this stage:
2205069154d5SJulian Elischer  * Common:
2206069154d5SJulian Elischer  *    reference to destination node.
2207069154d5SJulian Elischer  *    Reference to destination rcv hook if relevant.
2208e088dd4cSAlexander Motin  *    apply pointer must be or NULL or reference valid struct ng_apply_info.
2209069154d5SJulian Elischer  * Data:
2210069154d5SJulian Elischer  *    pointer to mbuf
2211069154d5SJulian Elischer  * Control_Message:
2212069154d5SJulian Elischer  *    pointer to msg.
2213069154d5SJulian Elischer  *    ID of original sender node. (return address)
22141acb27c6SJulian Elischer  * Function:
22151acb27c6SJulian Elischer  *    Function pointer
22161acb27c6SJulian Elischer  *    void * argument
22171acb27c6SJulian Elischer  *    integer argument
2218069154d5SJulian Elischer  *
2219069154d5SJulian Elischer  * The nodes have several routines and macros to help with this task:
2220069154d5SJulian Elischer  */
2221069154d5SJulian Elischer 
2222069154d5SJulian Elischer int
222342282202SGleb Smirnoff ng_snd_item(item_p item, int flags)
2224069154d5SJulian Elischer {
2225e088dd4cSAlexander Motin 	hook_p hook;
2226e088dd4cSAlexander Motin 	node_p node;
222742282202SGleb Smirnoff 	int queue, rw;
2228e088dd4cSAlexander Motin 	struct ng_queue *ngq;
222932b33288SGleb Smirnoff 	int error = 0;
2230069154d5SJulian Elischer 
2231f50597f5SAlexander Motin 	/* We are sending item, so it must be present! */
2232f50597f5SAlexander Motin 	KASSERT(item != NULL, ("ng_snd_item: item is NULL"));
2233e088dd4cSAlexander Motin 
2234e088dd4cSAlexander Motin #ifdef	NETGRAPH_DEBUG
2235e088dd4cSAlexander Motin 	_ngi_check(item, __FILE__, __LINE__);
2236e088dd4cSAlexander Motin #endif
2237e088dd4cSAlexander Motin 
2238f50597f5SAlexander Motin 	/* Item was sent once more, postpone apply() call. */
2239e088dd4cSAlexander Motin 	if (item->apply)
2240e088dd4cSAlexander Motin 		refcount_acquire(&item->apply->refs);
2241e088dd4cSAlexander Motin 
2242e088dd4cSAlexander Motin 	node = NGI_NODE(item);
2243f50597f5SAlexander Motin 	/* Node is never optional. */
2244f50597f5SAlexander Motin 	KASSERT(node != NULL, ("ng_snd_item: node is NULL"));
2245e088dd4cSAlexander Motin 
2246e72a98f4SAlexander Motin 	hook = NGI_HOOK(item);
2247f50597f5SAlexander Motin 	/* Valid hook and mbuf are mandatory for data. */
2248f50597f5SAlexander Motin 	if ((item->el_flags & NGQF_TYPE) == NGQF_DATA) {
2249f50597f5SAlexander Motin 		KASSERT(hook != NULL, ("ng_snd_item: hook for data is NULL"));
225010204449SJulian Elischer 		if (NGI_M(item) == NULL)
2251e088dd4cSAlexander Motin 			ERROUT(EINVAL);
2252069154d5SJulian Elischer 		CHECK_DATA_MBUF(NGI_M(item));
22536f683eeeSGleb Smirnoff 	}
22546f683eeeSGleb Smirnoff 
2255069154d5SJulian Elischer 	/*
2256f50597f5SAlexander Motin 	 * If the item or the node specifies single threading, force
2257f50597f5SAlexander Motin 	 * writer semantics. Similarly, the node may say one hook always
2258f50597f5SAlexander Motin 	 * produces writers. These are overrides.
2259069154d5SJulian Elischer 	 */
2260db3408aeSAlexander Motin 	if (((item->el_flags & NGQF_RW) == NGQF_WRITER) ||
2261f50597f5SAlexander Motin 	    (node->nd_flags & NGF_FORCE_WRITER) ||
2262f50597f5SAlexander Motin 	    (hook && (hook->hk_flags & HK_FORCE_WRITER))) {
2263069154d5SJulian Elischer 		rw = NGQRW_W;
2264f50597f5SAlexander Motin 	} else {
2265f50597f5SAlexander Motin 		rw = NGQRW_R;
2266f50597f5SAlexander Motin 	}
22676f683eeeSGleb Smirnoff 
226881a253a4SAlexander Motin 	/*
2269f089869fSMarko Zec 	 * If sender or receiver requests queued delivery, or call graph
2270f089869fSMarko Zec 	 * loops back from outbound to inbound path, or stack usage
227181a253a4SAlexander Motin 	 * level is dangerous - enqueue message.
227281a253a4SAlexander Motin 	 */
227381a253a4SAlexander Motin 	if ((flags & NG_QUEUE) || (hook && (hook->hk_flags & HK_QUEUE))) {
227481a253a4SAlexander Motin 		queue = 1;
2275f089869fSMarko Zec 	} else if (hook && (hook->hk_flags & HK_TO_INBOUND) &&
2276f089869fSMarko Zec 	    curthread->td_ng_outbound) {
2277f089869fSMarko Zec 		queue = 1;
2278f50597f5SAlexander Motin 	} else {
2279f50597f5SAlexander Motin 		queue = 0;
228081a253a4SAlexander Motin #ifdef GET_STACK_USAGE
2281d4529f98SAlexander Motin 		/*
2282942fe01fSDmitry Morozovsky 		 * Most of netgraph nodes have small stack consumption and
2283f50597f5SAlexander Motin 		 * for them 25% of free stack space is more than enough.
2284d4529f98SAlexander Motin 		 * Nodes/hooks with higher stack usage should be marked as
2285f9773372SDmitry Morozovsky 		 * HI_STACK. For them 50% of stack will be guaranteed then.
2286f50597f5SAlexander Motin 		 * XXX: Values 25% and 50% are completely empirical.
2287d4529f98SAlexander Motin 		 */
2288f50597f5SAlexander Motin 		size_t	st, su, sl;
228981a253a4SAlexander Motin 		GET_STACK_USAGE(st, su);
2290f50597f5SAlexander Motin 		sl = st - su;
22914bd1b557SGleb Smirnoff 		if ((sl * 4 < st) || ((sl * 2 < st) &&
22924bd1b557SGleb Smirnoff 		    ((node->nd_flags & NGF_HI_STACK) || (hook &&
22934bd1b557SGleb Smirnoff 		    (hook->hk_flags & HK_HI_STACK)))))
229481a253a4SAlexander Motin 			queue = 1;
229581a253a4SAlexander Motin #endif
2296f50597f5SAlexander Motin 	}
229781a253a4SAlexander Motin 
2298069154d5SJulian Elischer 	if (queue) {
2299394cb30aSAlexander Motin 		/* Put it on the queue for that node*/
23009852972bSAlexander Motin 		ng_queue_rw(node, item, rw);
2301f50597f5SAlexander Motin 		return ((flags & NG_PROGRESS) ? EINPROGRESS : 0);
2302069154d5SJulian Elischer 	}
2303069154d5SJulian Elischer 
2304069154d5SJulian Elischer 	/*
2305069154d5SJulian Elischer 	 * We already decided how we will be queueud or treated.
2306069154d5SJulian Elischer 	 * Try get the appropriate operating permission.
2307069154d5SJulian Elischer 	 */
230832b33288SGleb Smirnoff  	if (rw == NGQRW_R)
23099852972bSAlexander Motin 		item = ng_acquire_read(node, item);
231032b33288SGleb Smirnoff 	else
23119852972bSAlexander Motin 		item = ng_acquire_write(node, item);
23124cf49a43SJulian Elischer 
2313f50597f5SAlexander Motin 	/* Item was queued while trying to get permission. */
2314f50597f5SAlexander Motin 	if (item == NULL)
2315f50597f5SAlexander Motin 		return ((flags & NG_PROGRESS) ? EINPROGRESS : 0);
2316069154d5SJulian Elischer 
23175951069aSJulian Elischer 	NGI_GET_NODE(item, node); /* zaps stored node */
23185951069aSJulian Elischer 
231910e87318SAlexander Motin 	item->depth++;
232027757487SGleb Smirnoff 	error = ng_apply_item(node, item, rw); /* drops r/w lock when done */
2321069154d5SJulian Elischer 
2322394cb30aSAlexander Motin 	/* If something is waiting on queue and ready, schedule it. */
23239852972bSAlexander Motin 	ngq = &node->nd_input_queue;
2324394cb30aSAlexander Motin 	if (QUEUE_ACTIVE(ngq)) {
23252c8dda8dSWojciech A. Koszek 		NG_QUEUE_LOCK(ngq);
2326394cb30aSAlexander Motin 		if (QUEUE_ACTIVE(ngq) && NEXT_QUEUED_ITEM_CAN_PROCEED(ngq))
23279852972bSAlexander Motin 			ng_worklist_add(node);
23282c8dda8dSWojciech A. Koszek 		NG_QUEUE_UNLOCK(ngq);
2329394cb30aSAlexander Motin 	}
2330394cb30aSAlexander Motin 
2331394cb30aSAlexander Motin 	/*
2332394cb30aSAlexander Motin 	 * Node may go away as soon as we remove the reference.
2333394cb30aSAlexander Motin 	 * Whatever we do, DO NOT access the node again!
2334394cb30aSAlexander Motin 	 */
2335394cb30aSAlexander Motin 	NG_NODE_UNREF(node);
2336069154d5SJulian Elischer 
23371acb27c6SJulian Elischer 	return (error);
2338e088dd4cSAlexander Motin 
2339e088dd4cSAlexander Motin done:
2340f50597f5SAlexander Motin 	/* If was not sent, apply callback here. */
234110e87318SAlexander Motin 	if (item->apply != NULL) {
234210e87318SAlexander Motin 		if (item->depth == 0 && error != 0)
234310e87318SAlexander Motin 			item->apply->error = error;
234410e87318SAlexander Motin 		if (refcount_release(&item->apply->refs)) {
234510e87318SAlexander Motin 			(*item->apply->apply)(item->apply->context,
234610e87318SAlexander Motin 			    item->apply->error);
234710e87318SAlexander Motin 		}
234810e87318SAlexander Motin 	}
2349e72a98f4SAlexander Motin 
2350e088dd4cSAlexander Motin 	NG_FREE_ITEM(item);
2351e088dd4cSAlexander Motin 	return (error);
2352069154d5SJulian Elischer }
2353069154d5SJulian Elischer 
2354069154d5SJulian Elischer /*
2355069154d5SJulian Elischer  * We have an item that was possibly queued somewhere.
2356069154d5SJulian Elischer  * It should contain all the information needed
2357069154d5SJulian Elischer  * to run it on the appropriate node/hook.
2358e088dd4cSAlexander Motin  * If there is apply pointer and we own the last reference, call apply().
23594cf49a43SJulian Elischer  */
236027757487SGleb Smirnoff static int
2361714fb865SGleb Smirnoff ng_apply_item(node_p node, item_p item, int rw)
2362069154d5SJulian Elischer {
2363069154d5SJulian Elischer 	hook_p  hook;
2364069154d5SJulian Elischer 	ng_rcvdata_t *rcvdata;
2365c4b5eea4SJulian Elischer 	ng_rcvmsg_t *rcvmsg;
2366e088dd4cSAlexander Motin 	struct ng_apply_info *apply;
236710e87318SAlexander Motin 	int	error = 0, depth;
2368069154d5SJulian Elischer 
2369f50597f5SAlexander Motin 	/* Node and item are never optional. */
2370f50597f5SAlexander Motin 	KASSERT(node != NULL, ("ng_apply_item: node is NULL"));
2371f50597f5SAlexander Motin 	KASSERT(item != NULL, ("ng_apply_item: item is NULL"));
2372f50597f5SAlexander Motin 
23731acb27c6SJulian Elischer 	NGI_GET_HOOK(item, hook); /* clears stored hook */
237430400f03SJulian Elischer #ifdef	NETGRAPH_DEBUG
2375069154d5SJulian Elischer 	_ngi_check(item, __FILE__, __LINE__);
2376069154d5SJulian Elischer #endif
23778afe16d5SGleb Smirnoff 
23788afe16d5SGleb Smirnoff 	apply = item->apply;
237910e87318SAlexander Motin 	depth = item->depth;
23808afe16d5SGleb Smirnoff 
23816b795970SJulian Elischer 	switch (item->el_flags & NGQF_TYPE) {
2382069154d5SJulian Elischer 	case NGQF_DATA:
2383069154d5SJulian Elischer 		/*
2384069154d5SJulian Elischer 		 * Check things are still ok as when we were queued.
2385069154d5SJulian Elischer 		 */
2386f50597f5SAlexander Motin 		KASSERT(hook != NULL, ("ng_apply_item: hook for data is NULL"));
2387f50597f5SAlexander Motin 		if (NG_HOOK_NOT_VALID(hook) ||
2388f50597f5SAlexander Motin 		    NG_NODE_NOT_VALID(node)) {
2389a54a69d7SJulian Elischer 			error = EIO;
2390069154d5SJulian Elischer 			NG_FREE_ITEM(item);
2391c4b5eea4SJulian Elischer 			break;
2392069154d5SJulian Elischer 		}
2393c4b5eea4SJulian Elischer 		/*
2394c4b5eea4SJulian Elischer 		 * If no receive method, just silently drop it.
23954bd1b557SGleb Smirnoff 		 * Give preference to the hook over-ride method.
2396c4b5eea4SJulian Elischer 		 */
23974bd1b557SGleb Smirnoff 		if ((!(rcvdata = hook->hk_rcvdata)) &&
23984bd1b557SGleb Smirnoff 		    (!(rcvdata = NG_HOOK_NODE(hook)->nd_type->rcvdata))) {
2399c4b5eea4SJulian Elischer 			error = 0;
2400c4b5eea4SJulian Elischer 			NG_FREE_ITEM(item);
2401c4b5eea4SJulian Elischer 			break;
2402c4b5eea4SJulian Elischer 		}
2403a54a69d7SJulian Elischer 		error = (*rcvdata)(hook, item);
2404069154d5SJulian Elischer 		break;
2405069154d5SJulian Elischer 	case NGQF_MESG:
2406e72a98f4SAlexander Motin 		if (hook && NG_HOOK_NOT_VALID(hook)) {
2407069154d5SJulian Elischer 			/*
2408e72a98f4SAlexander Motin 			 * The hook has been zapped then we can't use it.
2409e72a98f4SAlexander Motin 			 * Immediately drop its reference.
2410069154d5SJulian Elischer 			 * The message may not need it.
2411069154d5SJulian Elischer 			 */
241230400f03SJulian Elischer 			NG_HOOK_UNREF(hook);
2413069154d5SJulian Elischer 			hook = NULL;
2414069154d5SJulian Elischer 		}
2415069154d5SJulian Elischer 		/*
2416069154d5SJulian Elischer 		 * Similarly, if the node is a zombie there is
2417069154d5SJulian Elischer 		 * nothing we can do with it, drop everything.
2418069154d5SJulian Elischer 		 */
241930400f03SJulian Elischer 		if (NG_NODE_NOT_VALID(node)) {
24206b795970SJulian Elischer 			TRAP_ERROR();
2421a54a69d7SJulian Elischer 			error = EINVAL;
2422069154d5SJulian Elischer 			NG_FREE_ITEM(item);
2423e72a98f4SAlexander Motin 			break;
2424e72a98f4SAlexander Motin 		}
2425069154d5SJulian Elischer 		/*
2426069154d5SJulian Elischer 		 * Call the appropriate message handler for the object.
2427069154d5SJulian Elischer 		 * It is up to the message handler to free the message.
2428069154d5SJulian Elischer 		 * If it's a generic message, handle it generically,
2429e72a98f4SAlexander Motin 		 * otherwise call the type's message handler (if it exists).
2430069154d5SJulian Elischer 		 * XXX (race). Remember that a queued message may
2431069154d5SJulian Elischer 		 * reference a node or hook that has just been
2432069154d5SJulian Elischer 		 * invalidated. It will exist as the queue code
2433069154d5SJulian Elischer 		 * is holding a reference, but..
2434069154d5SJulian Elischer 		 */
2435e72a98f4SAlexander Motin 		if ((NGI_MSG(item)->header.typecookie == NGM_GENERIC_COOKIE) &&
2436e72a98f4SAlexander Motin 		    ((NGI_MSG(item)->header.flags & NGF_RESP) == 0)) {
2437a54a69d7SJulian Elischer 			error = ng_generic_msg(node, item, hook);
2438c4b5eea4SJulian Elischer 			break;
2439c4b5eea4SJulian Elischer 		}
2440e72a98f4SAlexander Motin 		if (((!hook) || (!(rcvmsg = hook->hk_rcvmsg))) &&
2441e72a98f4SAlexander Motin 		    (!(rcvmsg = node->nd_type->rcvmsg))) {
24426b795970SJulian Elischer 			TRAP_ERROR();
2443a54a69d7SJulian Elischer 			error = 0;
2444069154d5SJulian Elischer 			NG_FREE_ITEM(item);
2445c4b5eea4SJulian Elischer 			break;
2446069154d5SJulian Elischer 		}
2447a54a69d7SJulian Elischer 		error = (*rcvmsg)(node, item, hook);
2448069154d5SJulian Elischer 		break;
24496b795970SJulian Elischer 	case NGQF_FN:
2450e088dd4cSAlexander Motin 	case NGQF_FN2:
2451e088dd4cSAlexander Motin 		/*
245274c9119dSAlexander Motin 		 * In the case of the shutdown message we allow it to hit
2453e088dd4cSAlexander Motin 		 * even if the node is invalid.
2454e088dd4cSAlexander Motin 		 */
245574c9119dSAlexander Motin 		if (NG_NODE_NOT_VALID(node) &&
245674c9119dSAlexander Motin 		    NGI_FN(item) != &ng_rmnode) {
2457e088dd4cSAlexander Motin 			TRAP_ERROR();
2458e088dd4cSAlexander Motin 			error = EINVAL;
2459e088dd4cSAlexander Motin 			NG_FREE_ITEM(item);
2460e088dd4cSAlexander Motin 			break;
2461e088dd4cSAlexander Motin 		}
246274c9119dSAlexander Motin 		/* Same is about some internal functions and invalid hook. */
246374c9119dSAlexander Motin 		if (hook && NG_HOOK_NOT_VALID(hook) &&
246474c9119dSAlexander Motin 		    NGI_FN2(item) != &ng_con_part2 &&
246574c9119dSAlexander Motin 		    NGI_FN2(item) != &ng_con_part3 &&
246674c9119dSAlexander Motin 		    NGI_FN(item) != &ng_rmhook_part2) {
246774c9119dSAlexander Motin 			TRAP_ERROR();
246874c9119dSAlexander Motin 			error = EINVAL;
246974c9119dSAlexander Motin 			NG_FREE_ITEM(item);
247074c9119dSAlexander Motin 			break;
247174c9119dSAlexander Motin 		}
247274c9119dSAlexander Motin 
2473b332b91fSGleb Smirnoff 		if ((item->el_flags & NGQF_TYPE) == NGQF_FN) {
2474b332b91fSGleb Smirnoff 			(*NGI_FN(item))(node, hook, NGI_ARG1(item),
2475b332b91fSGleb Smirnoff 			    NGI_ARG2(item));
2476b332b91fSGleb Smirnoff 			NG_FREE_ITEM(item);
2477b332b91fSGleb Smirnoff 		} else	/* it is NGQF_FN2 */
2478e088dd4cSAlexander Motin 			error = (*NGI_FN2(item))(node, item, hook);
2479e088dd4cSAlexander Motin 		break;
2480069154d5SJulian Elischer 	}
2481069154d5SJulian Elischer 	/*
2482069154d5SJulian Elischer 	 * We held references on some of the resources
2483069154d5SJulian Elischer 	 * that we took from the item. Now that we have
2484069154d5SJulian Elischer 	 * finished doing everything, drop those references.
2485069154d5SJulian Elischer 	 */
2486e72a98f4SAlexander Motin 	if (hook)
248730400f03SJulian Elischer 		NG_HOOK_UNREF(hook);
2488069154d5SJulian Elischer 
2489f50597f5SAlexander Motin  	if (rw == NGQRW_R)
24909852972bSAlexander Motin 		ng_leave_read(node);
2491f50597f5SAlexander Motin 	else
24929852972bSAlexander Motin 		ng_leave_write(node);
24938afe16d5SGleb Smirnoff 
24948afe16d5SGleb Smirnoff 	/* Apply callback. */
249510e87318SAlexander Motin 	if (apply != NULL) {
249610e87318SAlexander Motin 		if (depth == 1 && error != 0)
249710e87318SAlexander Motin 			apply->error = error;
249810e87318SAlexander Motin 		if (refcount_release(&apply->refs))
249910e87318SAlexander Motin 			(*apply->apply)(apply->context, apply->error);
250010e87318SAlexander Motin 	}
25018afe16d5SGleb Smirnoff 
250227757487SGleb Smirnoff 	return (error);
2503069154d5SJulian Elischer }
2504069154d5SJulian Elischer 
2505069154d5SJulian Elischer /***********************************************************************
2506069154d5SJulian Elischer  * Implement the 'generic' control messages
2507069154d5SJulian Elischer  ***********************************************************************/
2508069154d5SJulian Elischer static int
2509069154d5SJulian Elischer ng_generic_msg(node_p here, item_p item, hook_p lasthook)
25104cf49a43SJulian Elischer {
25114cf49a43SJulian Elischer 	int error = 0;
2512069154d5SJulian Elischer 	struct ng_mesg *msg;
2513069154d5SJulian Elischer 	struct ng_mesg *resp = NULL;
25144cf49a43SJulian Elischer 
2515069154d5SJulian Elischer 	NGI_GET_MSG(item, msg);
25164cf49a43SJulian Elischer 	if (msg->header.typecookie != NGM_GENERIC_COOKIE) {
25176b795970SJulian Elischer 		TRAP_ERROR();
2518069154d5SJulian Elischer 		error = EINVAL;
2519069154d5SJulian Elischer 		goto out;
25204cf49a43SJulian Elischer 	}
25214cf49a43SJulian Elischer 	switch (msg->header.cmd) {
25224cf49a43SJulian Elischer 	case NGM_SHUTDOWN:
25231acb27c6SJulian Elischer 		ng_rmnode(here, NULL, NULL, 0);
25244cf49a43SJulian Elischer 		break;
25254cf49a43SJulian Elischer 	case NGM_MKPEER:
25264cf49a43SJulian Elischer 	    {
25274cf49a43SJulian Elischer 		struct ngm_mkpeer *const mkp = (struct ngm_mkpeer *) msg->data;
25284cf49a43SJulian Elischer 
25294cf49a43SJulian Elischer 		if (msg->header.arglen != sizeof(*mkp)) {
25306b795970SJulian Elischer 			TRAP_ERROR();
2531069154d5SJulian Elischer 			error = EINVAL;
2532069154d5SJulian Elischer 			break;
25334cf49a43SJulian Elischer 		}
25344cf49a43SJulian Elischer 		mkp->type[sizeof(mkp->type) - 1] = '\0';
25354cf49a43SJulian Elischer 		mkp->ourhook[sizeof(mkp->ourhook) - 1] = '\0';
25364cf49a43SJulian Elischer 		mkp->peerhook[sizeof(mkp->peerhook) - 1] = '\0';
25374cf49a43SJulian Elischer 		error = ng_mkpeer(here, mkp->ourhook, mkp->peerhook, mkp->type);
25384cf49a43SJulian Elischer 		break;
25394cf49a43SJulian Elischer 	    }
25404cf49a43SJulian Elischer 	case NGM_CONNECT:
25414cf49a43SJulian Elischer 	    {
25424cf49a43SJulian Elischer 		struct ngm_connect *const con =
25434cf49a43SJulian Elischer 			(struct ngm_connect *) msg->data;
25444cf49a43SJulian Elischer 		node_p node2;
25454cf49a43SJulian Elischer 
25464cf49a43SJulian Elischer 		if (msg->header.arglen != sizeof(*con)) {
25476b795970SJulian Elischer 			TRAP_ERROR();
2548069154d5SJulian Elischer 			error = EINVAL;
2549069154d5SJulian Elischer 			break;
25504cf49a43SJulian Elischer 		}
25514cf49a43SJulian Elischer 		con->path[sizeof(con->path) - 1] = '\0';
25524cf49a43SJulian Elischer 		con->ourhook[sizeof(con->ourhook) - 1] = '\0';
25534cf49a43SJulian Elischer 		con->peerhook[sizeof(con->peerhook) - 1] = '\0';
2554069154d5SJulian Elischer 		/* Don't forget we get a reference.. */
2555069154d5SJulian Elischer 		error = ng_path2noderef(here, con->path, &node2, NULL);
25564cf49a43SJulian Elischer 		if (error)
25574cf49a43SJulian Elischer 			break;
2558e088dd4cSAlexander Motin 		error = ng_con_nodes(item, here, con->ourhook,
2559e088dd4cSAlexander Motin 		    node2, con->peerhook);
256030400f03SJulian Elischer 		NG_NODE_UNREF(node2);
25614cf49a43SJulian Elischer 		break;
25624cf49a43SJulian Elischer 	    }
25634cf49a43SJulian Elischer 	case NGM_NAME:
25644cf49a43SJulian Elischer 	    {
25654cf49a43SJulian Elischer 		struct ngm_name *const nam = (struct ngm_name *) msg->data;
25664cf49a43SJulian Elischer 
25674cf49a43SJulian Elischer 		if (msg->header.arglen != sizeof(*nam)) {
25686b795970SJulian Elischer 			TRAP_ERROR();
2569069154d5SJulian Elischer 			error = EINVAL;
2570069154d5SJulian Elischer 			break;
25714cf49a43SJulian Elischer 		}
25724cf49a43SJulian Elischer 		nam->name[sizeof(nam->name) - 1] = '\0';
25734cf49a43SJulian Elischer 		error = ng_name_node(here, nam->name);
25744cf49a43SJulian Elischer 		break;
25754cf49a43SJulian Elischer 	    }
25764cf49a43SJulian Elischer 	case NGM_RMHOOK:
25774cf49a43SJulian Elischer 	    {
25784cf49a43SJulian Elischer 		struct ngm_rmhook *const rmh = (struct ngm_rmhook *) msg->data;
25794cf49a43SJulian Elischer 		hook_p hook;
25804cf49a43SJulian Elischer 
25814cf49a43SJulian Elischer 		if (msg->header.arglen != sizeof(*rmh)) {
25826b795970SJulian Elischer 			TRAP_ERROR();
2583069154d5SJulian Elischer 			error = EINVAL;
2584069154d5SJulian Elischer 			break;
25854cf49a43SJulian Elischer 		}
25864cf49a43SJulian Elischer 		rmh->ourhook[sizeof(rmh->ourhook) - 1] = '\0';
2587899e9c4eSArchie Cobbs 		if ((hook = ng_findhook(here, rmh->ourhook)) != NULL)
25884cf49a43SJulian Elischer 			ng_destroy_hook(hook);
25894cf49a43SJulian Elischer 		break;
25904cf49a43SJulian Elischer 	    }
25914cf49a43SJulian Elischer 	case NGM_NODEINFO:
25924cf49a43SJulian Elischer 	    {
25934cf49a43SJulian Elischer 		struct nodeinfo *ni;
25944cf49a43SJulian Elischer 
2595069154d5SJulian Elischer 		NG_MKRESPONSE(resp, msg, sizeof(*ni), M_NOWAIT);
25964cf49a43SJulian Elischer 		if (resp == NULL) {
25974cf49a43SJulian Elischer 			error = ENOMEM;
25984cf49a43SJulian Elischer 			break;
25994cf49a43SJulian Elischer 		}
26004cf49a43SJulian Elischer 
26014cf49a43SJulian Elischer 		/* Fill in node info */
2602069154d5SJulian Elischer 		ni = (struct nodeinfo *) resp->data;
260330400f03SJulian Elischer 		if (NG_NODE_HAS_NAME(here))
260487e2c66aSHartmut Brandt 			strcpy(ni->name, NG_NODE_NAME(here));
260587e2c66aSHartmut Brandt 		strcpy(ni->type, here->nd_type->name);
2606dc90cad9SJulian Elischer 		ni->id = ng_node2ID(here);
260730400f03SJulian Elischer 		ni->hooks = here->nd_numhooks;
26084cf49a43SJulian Elischer 		break;
26094cf49a43SJulian Elischer 	    }
26104cf49a43SJulian Elischer 	case NGM_LISTHOOKS:
26114cf49a43SJulian Elischer 	    {
261230400f03SJulian Elischer 		const int nhooks = here->nd_numhooks;
26134cf49a43SJulian Elischer 		struct hooklist *hl;
26144cf49a43SJulian Elischer 		struct nodeinfo *ni;
26154cf49a43SJulian Elischer 		hook_p hook;
26164cf49a43SJulian Elischer 
26174cf49a43SJulian Elischer 		/* Get response struct */
26184bd1b557SGleb Smirnoff 		NG_MKRESPONSE(resp, msg, sizeof(*hl) +
26194bd1b557SGleb Smirnoff 		    (nhooks * sizeof(struct linkinfo)), M_NOWAIT);
2620069154d5SJulian Elischer 		if (resp == NULL) {
26214cf49a43SJulian Elischer 			error = ENOMEM;
26224cf49a43SJulian Elischer 			break;
26234cf49a43SJulian Elischer 		}
2624069154d5SJulian Elischer 		hl = (struct hooklist *) resp->data;
26254cf49a43SJulian Elischer 		ni = &hl->nodeinfo;
26264cf49a43SJulian Elischer 
26274cf49a43SJulian Elischer 		/* Fill in node info */
262830400f03SJulian Elischer 		if (NG_NODE_HAS_NAME(here))
262987e2c66aSHartmut Brandt 			strcpy(ni->name, NG_NODE_NAME(here));
263087e2c66aSHartmut Brandt 		strcpy(ni->type, here->nd_type->name);
2631dc90cad9SJulian Elischer 		ni->id = ng_node2ID(here);
26324cf49a43SJulian Elischer 
26334cf49a43SJulian Elischer 		/* Cycle through the linked list of hooks */
26344cf49a43SJulian Elischer 		ni->hooks = 0;
263530400f03SJulian Elischer 		LIST_FOREACH(hook, &here->nd_hooks, hk_hooks) {
26364cf49a43SJulian Elischer 			struct linkinfo *const link = &hl->link[ni->hooks];
26374cf49a43SJulian Elischer 
26384cf49a43SJulian Elischer 			if (ni->hooks >= nhooks) {
26394cf49a43SJulian Elischer 				log(LOG_ERR, "%s: number of %s changed\n",
26406e551fb6SDavid E. O'Brien 				    __func__, "hooks");
26414cf49a43SJulian Elischer 				break;
26424cf49a43SJulian Elischer 			}
264330400f03SJulian Elischer 			if (NG_HOOK_NOT_VALID(hook))
26444cf49a43SJulian Elischer 				continue;
264587e2c66aSHartmut Brandt 			strcpy(link->ourhook, NG_HOOK_NAME(hook));
264687e2c66aSHartmut Brandt 			strcpy(link->peerhook, NG_PEER_HOOK_NAME(hook));
264730400f03SJulian Elischer 			if (NG_PEER_NODE_NAME(hook)[0] != '\0')
264887e2c66aSHartmut Brandt 				strcpy(link->nodeinfo.name,
264987e2c66aSHartmut Brandt 				    NG_PEER_NODE_NAME(hook));
265087e2c66aSHartmut Brandt 			strcpy(link->nodeinfo.type,
265187e2c66aSHartmut Brandt 			   NG_PEER_NODE(hook)->nd_type->name);
265230400f03SJulian Elischer 			link->nodeinfo.id = ng_node2ID(NG_PEER_NODE(hook));
265330400f03SJulian Elischer 			link->nodeinfo.hooks = NG_PEER_NODE(hook)->nd_numhooks;
26544cf49a43SJulian Elischer 			ni->hooks++;
26554cf49a43SJulian Elischer 		}
26564cf49a43SJulian Elischer 		break;
26574cf49a43SJulian Elischer 	    }
26584cf49a43SJulian Elischer 
26594cf49a43SJulian Elischer 	case NGM_LISTNODES:
26604cf49a43SJulian Elischer 	    {
26614cf49a43SJulian Elischer 		struct namelist *nl;
26624cf49a43SJulian Elischer 		node_p node;
2663687adb70SGleb Smirnoff 		int i;
2664687adb70SGleb Smirnoff 
2665687adb70SGleb Smirnoff 		IDHASH_RLOCK();
2666687adb70SGleb Smirnoff 		/* Get response struct. */
2667687adb70SGleb Smirnoff 		NG_MKRESPONSE(resp, msg, sizeof(*nl) +
26688805f3d7SAlexander Motin 		    (V_ng_nodes * sizeof(struct nodeinfo)), M_NOWAIT);
2669687adb70SGleb Smirnoff 		if (resp == NULL) {
2670687adb70SGleb Smirnoff 			IDHASH_RUNLOCK();
2671687adb70SGleb Smirnoff 			error = ENOMEM;
2672687adb70SGleb Smirnoff 			break;
2673687adb70SGleb Smirnoff 		}
2674687adb70SGleb Smirnoff 		nl = (struct namelist *) resp->data;
2675687adb70SGleb Smirnoff 
2676687adb70SGleb Smirnoff 		/* Cycle through the lists of nodes. */
2677687adb70SGleb Smirnoff 		nl->numnames = 0;
2678687adb70SGleb Smirnoff 		for (i = 0; i <= V_ng_ID_hmask; i++) {
2679687adb70SGleb Smirnoff 			LIST_FOREACH(node, &V_ng_ID_hash[i], nd_idnodes) {
2680687adb70SGleb Smirnoff 				struct nodeinfo *const np =
2681687adb70SGleb Smirnoff 				    &nl->nodeinfo[nl->numnames];
2682687adb70SGleb Smirnoff 
2683687adb70SGleb Smirnoff 				if (NG_NODE_NOT_VALID(node))
2684687adb70SGleb Smirnoff 					continue;
2685687adb70SGleb Smirnoff 				if (NG_NODE_HAS_NAME(node))
2686687adb70SGleb Smirnoff 					strcpy(np->name, NG_NODE_NAME(node));
2687687adb70SGleb Smirnoff 				strcpy(np->type, node->nd_type->name);
2688687adb70SGleb Smirnoff 				np->id = ng_node2ID(node);
2689687adb70SGleb Smirnoff 				np->hooks = node->nd_numhooks;
2690687adb70SGleb Smirnoff 				KASSERT(nl->numnames < V_ng_nodes,
2691687adb70SGleb Smirnoff 				    ("%s: no space", __func__));
2692687adb70SGleb Smirnoff 				nl->numnames++;
2693687adb70SGleb Smirnoff 			}
2694687adb70SGleb Smirnoff 		}
2695687adb70SGleb Smirnoff 		IDHASH_RUNLOCK();
2696687adb70SGleb Smirnoff 		break;
2697687adb70SGleb Smirnoff 	    }
2698687adb70SGleb Smirnoff 	case NGM_LISTNAMES:
2699687adb70SGleb Smirnoff 	    {
2700687adb70SGleb Smirnoff 		struct namelist *nl;
2701687adb70SGleb Smirnoff 		node_p node;
2702687adb70SGleb Smirnoff 		int i;
27034cf49a43SJulian Elischer 
2704c4282b74SGleb Smirnoff 		NAMEHASH_RLOCK();
2705687adb70SGleb Smirnoff 		/* Get response struct. */
27064bd1b557SGleb Smirnoff 		NG_MKRESPONSE(resp, msg, sizeof(*nl) +
2707687adb70SGleb Smirnoff 		    (V_ng_named_nodes * sizeof(struct nodeinfo)), M_NOWAIT);
2708069154d5SJulian Elischer 		if (resp == NULL) {
2709c4282b74SGleb Smirnoff 			NAMEHASH_RUNLOCK();
27104cf49a43SJulian Elischer 			error = ENOMEM;
27114cf49a43SJulian Elischer 			break;
27124cf49a43SJulian Elischer 		}
2713069154d5SJulian Elischer 		nl = (struct namelist *) resp->data;
27144cf49a43SJulian Elischer 
2715687adb70SGleb Smirnoff 		/* Cycle through the lists of nodes. */
27164cf49a43SJulian Elischer 		nl->numnames = 0;
2717687adb70SGleb Smirnoff 		for (i = 0; i <= V_ng_name_hmask; i++) {
2718603724d3SBjoern A. Zeeb 			LIST_FOREACH(node, &V_ng_name_hash[i], nd_nodes) {
2719cfea3f85SAlexander Motin 				struct nodeinfo *const np =
2720cfea3f85SAlexander Motin 				    &nl->nodeinfo[nl->numnames];
27214cf49a43SJulian Elischer 
2722b96baf0aSGleb Smirnoff 				if (NG_NODE_NOT_VALID(node))
2723b96baf0aSGleb Smirnoff 					continue;
272487e2c66aSHartmut Brandt 				strcpy(np->name, NG_NODE_NAME(node));
272587e2c66aSHartmut Brandt 				strcpy(np->type, node->nd_type->name);
2726dc90cad9SJulian Elischer 				np->id = ng_node2ID(node);
272730400f03SJulian Elischer 				np->hooks = node->nd_numhooks;
2728687adb70SGleb Smirnoff 				KASSERT(nl->numnames < V_ng_named_nodes,
2729687adb70SGleb Smirnoff 				    ("%s: no space", __func__));
27304cf49a43SJulian Elischer 				nl->numnames++;
27314cf49a43SJulian Elischer 			}
2732cfea3f85SAlexander Motin 		}
2733c4282b74SGleb Smirnoff 		NAMEHASH_RUNLOCK();
27344cf49a43SJulian Elischer 		break;
27354cf49a43SJulian Elischer 	    }
27364cf49a43SJulian Elischer 
27374cf49a43SJulian Elischer 	case NGM_LISTTYPES:
27384cf49a43SJulian Elischer 	    {
27394cf49a43SJulian Elischer 		struct typelist *tl;
27404cf49a43SJulian Elischer 		struct ng_type *type;
27414cf49a43SJulian Elischer 		int num = 0;
27424cf49a43SJulian Elischer 
2743c4282b74SGleb Smirnoff 		TYPELIST_RLOCK();
27444cf49a43SJulian Elischer 		/* Count number of types */
2745c4282b74SGleb Smirnoff 		LIST_FOREACH(type, &ng_typelist, types)
27464cf49a43SJulian Elischer 			num++;
27474cf49a43SJulian Elischer 
27484cf49a43SJulian Elischer 		/* Get response struct */
27494bd1b557SGleb Smirnoff 		NG_MKRESPONSE(resp, msg, sizeof(*tl) +
27504bd1b557SGleb Smirnoff 		    (num * sizeof(struct typeinfo)), M_NOWAIT);
2751069154d5SJulian Elischer 		if (resp == NULL) {
2752c4282b74SGleb Smirnoff 			TYPELIST_RUNLOCK();
27534cf49a43SJulian Elischer 			error = ENOMEM;
27544cf49a43SJulian Elischer 			break;
27554cf49a43SJulian Elischer 		}
2756069154d5SJulian Elischer 		tl = (struct typelist *) resp->data;
27574cf49a43SJulian Elischer 
27584cf49a43SJulian Elischer 		/* Cycle through the linked list of types */
27594cf49a43SJulian Elischer 		tl->numtypes = 0;
2760069154d5SJulian Elischer 		LIST_FOREACH(type, &ng_typelist, types) {
27614cf49a43SJulian Elischer 			struct typeinfo *const tp = &tl->typeinfo[tl->numtypes];
27624cf49a43SJulian Elischer 
276387e2c66aSHartmut Brandt 			strcpy(tp->type_name, type->name);
2764c73b94a2SJulian Elischer 			tp->numnodes = type->refs - 1; /* don't count list */
2765c4282b74SGleb Smirnoff 			KASSERT(tl->numtypes < num, ("%s: no space", __func__));
27664cf49a43SJulian Elischer 			tl->numtypes++;
27674cf49a43SJulian Elischer 		}
2768c4282b74SGleb Smirnoff 		TYPELIST_RUNLOCK();
27694cf49a43SJulian Elischer 		break;
27704cf49a43SJulian Elischer 	    }
27714cf49a43SJulian Elischer 
2772f8307e12SArchie Cobbs 	case NGM_BINARY2ASCII:
2773f8307e12SArchie Cobbs 	    {
2774*45d75e3aSLutz Donnerhacke 		int bufSize = 1024;
2775f8307e12SArchie Cobbs 		const struct ng_parse_type *argstype;
2776f8307e12SArchie Cobbs 		const struct ng_cmdlist *c;
2777069154d5SJulian Elischer 		struct ng_mesg *binary, *ascii;
2778f8307e12SArchie Cobbs 
2779f8307e12SArchie Cobbs 		/* Data area must contain a valid netgraph message */
2780f8307e12SArchie Cobbs 		binary = (struct ng_mesg *)msg->data;
27814c9b5910SGleb Smirnoff 		if (msg->header.arglen < sizeof(struct ng_mesg) ||
27824c9b5910SGleb Smirnoff 		    (msg->header.arglen - sizeof(struct ng_mesg) <
27834c9b5910SGleb Smirnoff 		    binary->header.arglen)) {
27846b795970SJulian Elischer 			TRAP_ERROR();
2785f8307e12SArchie Cobbs 			error = EINVAL;
2786f8307e12SArchie Cobbs 			break;
2787f8307e12SArchie Cobbs 		}
2788*45d75e3aSLutz Donnerhacke retry_b2a:
2789f8307e12SArchie Cobbs 		/* Get a response message with lots of room */
2790069154d5SJulian Elischer 		NG_MKRESPONSE(resp, msg, sizeof(*ascii) + bufSize, M_NOWAIT);
2791069154d5SJulian Elischer 		if (resp == NULL) {
2792f8307e12SArchie Cobbs 			error = ENOMEM;
2793f8307e12SArchie Cobbs 			break;
2794f8307e12SArchie Cobbs 		}
2795069154d5SJulian Elischer 		ascii = (struct ng_mesg *)resp->data;
2796f8307e12SArchie Cobbs 
2797f8307e12SArchie Cobbs 		/* Copy binary message header to response message payload */
2798f8307e12SArchie Cobbs 		bcopy(binary, ascii, sizeof(*binary));
2799f8307e12SArchie Cobbs 
2800f8307e12SArchie Cobbs 		/* Find command by matching typecookie and command number */
28014bd1b557SGleb Smirnoff 		for (c = here->nd_type->cmdlist; c != NULL && c->name != NULL;
28024bd1b557SGleb Smirnoff 		    c++) {
28034bd1b557SGleb Smirnoff 			if (binary->header.typecookie == c->cookie &&
28044bd1b557SGleb Smirnoff 			    binary->header.cmd == c->cmd)
2805f8307e12SArchie Cobbs 				break;
2806f8307e12SArchie Cobbs 		}
2807f8307e12SArchie Cobbs 		if (c == NULL || c->name == NULL) {
2808f8307e12SArchie Cobbs 			for (c = ng_generic_cmds; c->name != NULL; c++) {
28094bd1b557SGleb Smirnoff 				if (binary->header.typecookie == c->cookie &&
28104bd1b557SGleb Smirnoff 				    binary->header.cmd == c->cmd)
2811f8307e12SArchie Cobbs 					break;
2812f8307e12SArchie Cobbs 			}
2813f8307e12SArchie Cobbs 			if (c->name == NULL) {
2814069154d5SJulian Elischer 				NG_FREE_MSG(resp);
2815f8307e12SArchie Cobbs 				error = ENOSYS;
2816f8307e12SArchie Cobbs 				break;
2817f8307e12SArchie Cobbs 			}
2818f8307e12SArchie Cobbs 		}
2819f8307e12SArchie Cobbs 
2820f8307e12SArchie Cobbs 		/* Convert command name to ASCII */
2821f8307e12SArchie Cobbs 		snprintf(ascii->header.cmdstr, sizeof(ascii->header.cmdstr),
2822f8307e12SArchie Cobbs 		    "%s", c->name);
2823f8307e12SArchie Cobbs 
2824f8307e12SArchie Cobbs 		/* Convert command arguments to ASCII */
2825f8307e12SArchie Cobbs 		argstype = (binary->header.flags & NGF_RESP) ?
2826f8307e12SArchie Cobbs 		    c->respType : c->mesgType;
282770de87f2SJulian Elischer 		if (argstype == NULL) {
2828f8307e12SArchie Cobbs 			*ascii->data = '\0';
282970de87f2SJulian Elischer 		} else {
2830*45d75e3aSLutz Donnerhacke 			error = ng_unparse(argstype, (u_char *)binary->data,
2831*45d75e3aSLutz Donnerhacke 			    ascii->data, bufSize);
2832*45d75e3aSLutz Donnerhacke 			if (error == ERANGE) {
2833*45d75e3aSLutz Donnerhacke 				NG_FREE_MSG(resp);
2834*45d75e3aSLutz Donnerhacke 				bufSize *= 2;
2835*45d75e3aSLutz Donnerhacke 				goto retry_b2a;
2836*45d75e3aSLutz Donnerhacke 			} else if (error) {
2837069154d5SJulian Elischer 				NG_FREE_MSG(resp);
2838f8307e12SArchie Cobbs 				break;
2839f8307e12SArchie Cobbs 			}
2840f8307e12SArchie Cobbs 		}
2841f8307e12SArchie Cobbs 
2842f8307e12SArchie Cobbs 		/* Return the result as struct ng_mesg plus ASCII string */
2843f8307e12SArchie Cobbs 		bufSize = strlen(ascii->data) + 1;
2844f8307e12SArchie Cobbs 		ascii->header.arglen = bufSize;
2845069154d5SJulian Elischer 		resp->header.arglen = sizeof(*ascii) + bufSize;
2846f8307e12SArchie Cobbs 		break;
2847f8307e12SArchie Cobbs 	    }
2848f8307e12SArchie Cobbs 
2849f8307e12SArchie Cobbs 	case NGM_ASCII2BINARY:
2850f8307e12SArchie Cobbs 	    {
285198a5a343SMarko Zec 		int bufSize = 20 * 1024;	/* XXX hard coded constant */
2852f8307e12SArchie Cobbs 		const struct ng_cmdlist *c;
2853f8307e12SArchie Cobbs 		const struct ng_parse_type *argstype;
2854069154d5SJulian Elischer 		struct ng_mesg *ascii, *binary;
285552ec4a03SArchie Cobbs 		int off = 0;
2856f8307e12SArchie Cobbs 
2857f8307e12SArchie Cobbs 		/* Data area must contain at least a struct ng_mesg + '\0' */
2858f8307e12SArchie Cobbs 		ascii = (struct ng_mesg *)msg->data;
28594c9b5910SGleb Smirnoff 		if ((msg->header.arglen < sizeof(*ascii) + 1) ||
28604c9b5910SGleb Smirnoff 		    (ascii->header.arglen < 1) ||
28614c9b5910SGleb Smirnoff 		    (msg->header.arglen < sizeof(*ascii) +
28624c9b5910SGleb Smirnoff 		    ascii->header.arglen)) {
28636b795970SJulian Elischer 			TRAP_ERROR();
2864f8307e12SArchie Cobbs 			error = EINVAL;
2865f8307e12SArchie Cobbs 			break;
2866f8307e12SArchie Cobbs 		}
2867f8307e12SArchie Cobbs 		ascii->data[ascii->header.arglen - 1] = '\0';
2868f8307e12SArchie Cobbs 
2869f8307e12SArchie Cobbs 		/* Get a response message with lots of room */
2870069154d5SJulian Elischer 		NG_MKRESPONSE(resp, msg, sizeof(*binary) + bufSize, M_NOWAIT);
2871069154d5SJulian Elischer 		if (resp == NULL) {
2872f8307e12SArchie Cobbs 			error = ENOMEM;
2873f8307e12SArchie Cobbs 			break;
2874f8307e12SArchie Cobbs 		}
2875069154d5SJulian Elischer 		binary = (struct ng_mesg *)resp->data;
2876f8307e12SArchie Cobbs 
2877f8307e12SArchie Cobbs 		/* Copy ASCII message header to response message payload */
2878f8307e12SArchie Cobbs 		bcopy(ascii, binary, sizeof(*ascii));
2879f8307e12SArchie Cobbs 
2880f8307e12SArchie Cobbs 		/* Find command by matching ASCII command string */
288130400f03SJulian Elischer 		for (c = here->nd_type->cmdlist;
2882f8307e12SArchie Cobbs 		    c != NULL && c->name != NULL; c++) {
2883f8307e12SArchie Cobbs 			if (strcmp(ascii->header.cmdstr, c->name) == 0)
2884f8307e12SArchie Cobbs 				break;
2885f8307e12SArchie Cobbs 		}
2886f8307e12SArchie Cobbs 		if (c == NULL || c->name == NULL) {
2887f8307e12SArchie Cobbs 			for (c = ng_generic_cmds; c->name != NULL; c++) {
2888f8307e12SArchie Cobbs 				if (strcmp(ascii->header.cmdstr, c->name) == 0)
2889f8307e12SArchie Cobbs 					break;
2890f8307e12SArchie Cobbs 			}
2891f8307e12SArchie Cobbs 			if (c->name == NULL) {
2892069154d5SJulian Elischer 				NG_FREE_MSG(resp);
2893f8307e12SArchie Cobbs 				error = ENOSYS;
2894f8307e12SArchie Cobbs 				break;
2895f8307e12SArchie Cobbs 			}
2896f8307e12SArchie Cobbs 		}
2897f8307e12SArchie Cobbs 
2898f8307e12SArchie Cobbs 		/* Convert command name to binary */
2899f8307e12SArchie Cobbs 		binary->header.cmd = c->cmd;
2900f8307e12SArchie Cobbs 		binary->header.typecookie = c->cookie;
2901f8307e12SArchie Cobbs 
2902f8307e12SArchie Cobbs 		/* Convert command arguments to binary */
2903f8307e12SArchie Cobbs 		argstype = (binary->header.flags & NGF_RESP) ?
2904f8307e12SArchie Cobbs 		    c->respType : c->mesgType;
290570de87f2SJulian Elischer 		if (argstype == NULL) {
2906f8307e12SArchie Cobbs 			bufSize = 0;
290770de87f2SJulian Elischer 		} else {
29084bd1b557SGleb Smirnoff 			if ((error = ng_parse(argstype, ascii->data, &off,
29094bd1b557SGleb Smirnoff 			    (u_char *)binary->data, &bufSize)) != 0) {
2910069154d5SJulian Elischer 				NG_FREE_MSG(resp);
2911f8307e12SArchie Cobbs 				break;
2912f8307e12SArchie Cobbs 			}
2913f8307e12SArchie Cobbs 		}
2914f8307e12SArchie Cobbs 
2915f8307e12SArchie Cobbs 		/* Return the result */
2916f8307e12SArchie Cobbs 		binary->header.arglen = bufSize;
2917069154d5SJulian Elischer 		resp->header.arglen = sizeof(*binary) + bufSize;
2918f8307e12SArchie Cobbs 		break;
2919f8307e12SArchie Cobbs 	    }
2920f8307e12SArchie Cobbs 
29217095e097SPoul-Henning Kamp 	case NGM_TEXT_CONFIG:
29224cf49a43SJulian Elischer 	case NGM_TEXT_STATUS:
29234cf49a43SJulian Elischer 		/*
29244cf49a43SJulian Elischer 		 * This one is tricky as it passes the command down to the
29254cf49a43SJulian Elischer 		 * actual node, even though it is a generic type command.
2926069154d5SJulian Elischer 		 * This means we must assume that the item/msg is already freed
29274cf49a43SJulian Elischer 		 * when control passes back to us.
29284cf49a43SJulian Elischer 		 */
292930400f03SJulian Elischer 		if (here->nd_type->rcvmsg != NULL) {
2930069154d5SJulian Elischer 			NGI_MSG(item) = msg; /* put it back as we found it */
293130400f03SJulian Elischer 			return((*here->nd_type->rcvmsg)(here, item, lasthook));
29324cf49a43SJulian Elischer 		}
29334cf49a43SJulian Elischer 		/* Fall through if rcvmsg not supported */
29344cf49a43SJulian Elischer 	default:
29356b795970SJulian Elischer 		TRAP_ERROR();
29364cf49a43SJulian Elischer 		error = EINVAL;
29374cf49a43SJulian Elischer 	}
2938069154d5SJulian Elischer 	/*
2939069154d5SJulian Elischer 	 * Sometimes a generic message may be statically allocated
29404bd1b557SGleb Smirnoff 	 * to avoid problems with allocating when in tight memory situations.
2941069154d5SJulian Elischer 	 * Don't free it if it is so.
2942053359b7SPedro F. Giffuni 	 * I break them apart here, because erros may cause a free if the item
2943069154d5SJulian Elischer 	 * in which case we'd be doing it twice.
2944069154d5SJulian Elischer 	 * they are kept together above, to simplify freeing.
2945069154d5SJulian Elischer 	 */
2946069154d5SJulian Elischer out:
2947069154d5SJulian Elischer 	NG_RESPOND_MSG(error, here, item, resp);
2948069154d5SJulian Elischer 	NG_FREE_MSG(msg);
29494cf49a43SJulian Elischer 	return (error);
29504cf49a43SJulian Elischer }
29514cf49a43SJulian Elischer 
29524cf49a43SJulian Elischer /************************************************************************
29538253c060SGleb Smirnoff 			Queue element get/free routines
29548253c060SGleb Smirnoff ************************************************************************/
29558253c060SGleb Smirnoff 
29568253c060SGleb Smirnoff uma_zone_t			ng_qzone;
29576aa6d011SAlexander Motin uma_zone_t			ng_qdzone;
2958f2fbb838SAlexander Motin static int			numthreads = 0; /* number of queue threads */
2959ed75521fSAlexander Motin static int			maxalloc = 4096;/* limit the damage of a leak */
29608d8e595eSGleb Smirnoff static int			maxdata = 4096;	/* limit the damage of a DoS */
29618253c060SGleb Smirnoff 
2962f2fbb838SAlexander Motin SYSCTL_INT(_net_graph, OID_AUTO, threads, CTLFLAG_RDTUN, &numthreads,
2963f2fbb838SAlexander Motin     0, "Number of queue processing threads");
29648253c060SGleb Smirnoff SYSCTL_INT(_net_graph, OID_AUTO, maxalloc, CTLFLAG_RDTUN, &maxalloc,
29656aa6d011SAlexander Motin     0, "Maximum number of non-data queue items to allocate");
29666aa6d011SAlexander Motin SYSCTL_INT(_net_graph, OID_AUTO, maxdata, CTLFLAG_RDTUN, &maxdata,
29676aa6d011SAlexander Motin     0, "Maximum number of data queue items to allocate");
29688253c060SGleb Smirnoff 
29698253c060SGleb Smirnoff #ifdef	NETGRAPH_DEBUG
29708253c060SGleb Smirnoff static TAILQ_HEAD(, ng_item) ng_itemlist = TAILQ_HEAD_INITIALIZER(ng_itemlist);
29718253c060SGleb Smirnoff static int allocated;	/* number of items malloc'd */
29728253c060SGleb Smirnoff #endif
29738253c060SGleb Smirnoff 
29748253c060SGleb Smirnoff /*
29758253c060SGleb Smirnoff  * Get a queue entry.
29768253c060SGleb Smirnoff  * This is usually called when a packet first enters netgraph.
29778253c060SGleb Smirnoff  * By definition, this is usually from an interrupt, or from a user.
29788253c060SGleb Smirnoff  * Users are not so important, but try be quick for the times that it's
29798253c060SGleb Smirnoff  * an interrupt.
29808253c060SGleb Smirnoff  */
29818253c060SGleb Smirnoff static __inline item_p
29826aa6d011SAlexander Motin ng_alloc_item(int type, int flags)
29838253c060SGleb Smirnoff {
29846aa6d011SAlexander Motin 	item_p item;
29858253c060SGleb Smirnoff 
29866aa6d011SAlexander Motin 	KASSERT(((type & ~NGQF_TYPE) == 0),
29876aa6d011SAlexander Motin 	    ("%s: incorrect item type: %d", __func__, type));
298842282202SGleb Smirnoff 
29896aa6d011SAlexander Motin 	item = uma_zalloc((type == NGQF_DATA) ? ng_qdzone : ng_qzone,
29906aa6d011SAlexander Motin 	    ((flags & NG_WAITOK) ? M_WAITOK : M_NOWAIT) | M_ZERO);
29918253c060SGleb Smirnoff 
29928253c060SGleb Smirnoff 	if (item) {
29936aa6d011SAlexander Motin 		item->el_flags = type;
29946aa6d011SAlexander Motin #ifdef	NETGRAPH_DEBUG
29958253c060SGleb Smirnoff 		mtx_lock(&ngq_mtx);
29968253c060SGleb Smirnoff 		TAILQ_INSERT_TAIL(&ng_itemlist, item, all);
29978253c060SGleb Smirnoff 		allocated++;
29988253c060SGleb Smirnoff 		mtx_unlock(&ngq_mtx);
29998253c060SGleb Smirnoff #endif
30006aa6d011SAlexander Motin 	}
30018253c060SGleb Smirnoff 
30028253c060SGleb Smirnoff 	return (item);
30038253c060SGleb Smirnoff }
30048253c060SGleb Smirnoff 
30058253c060SGleb Smirnoff /*
30068253c060SGleb Smirnoff  * Release a queue entry
30078253c060SGleb Smirnoff  */
30088253c060SGleb Smirnoff void
30098253c060SGleb Smirnoff ng_free_item(item_p item)
30108253c060SGleb Smirnoff {
30118253c060SGleb Smirnoff 	/*
301228323addSBryan Drewery 	 * The item may hold resources on its own. We need to free
30138253c060SGleb Smirnoff 	 * these before we can free the item. What they are depends upon
30148253c060SGleb Smirnoff 	 * what kind of item it is. it is important that nodes zero
30158253c060SGleb Smirnoff 	 * out pointers to resources that they remove from the item
30168253c060SGleb Smirnoff 	 * or we release them again here.
30178253c060SGleb Smirnoff 	 */
30188253c060SGleb Smirnoff 	switch (item->el_flags & NGQF_TYPE) {
30198253c060SGleb Smirnoff 	case NGQF_DATA:
30208253c060SGleb Smirnoff 		/* If we have an mbuf still attached.. */
30218253c060SGleb Smirnoff 		NG_FREE_M(_NGI_M(item));
30228253c060SGleb Smirnoff 		break;
30238253c060SGleb Smirnoff 	case NGQF_MESG:
30248253c060SGleb Smirnoff 		_NGI_RETADDR(item) = 0;
30258253c060SGleb Smirnoff 		NG_FREE_MSG(_NGI_MSG(item));
30268253c060SGleb Smirnoff 		break;
30278253c060SGleb Smirnoff 	case NGQF_FN:
3028e088dd4cSAlexander Motin 	case NGQF_FN2:
30298253c060SGleb Smirnoff 		/* nothing to free really, */
30308253c060SGleb Smirnoff 		_NGI_FN(item) = NULL;
30318253c060SGleb Smirnoff 		_NGI_ARG1(item) = NULL;
30328253c060SGleb Smirnoff 		_NGI_ARG2(item) = 0;
30338253c060SGleb Smirnoff 		break;
30348253c060SGleb Smirnoff 	}
30358253c060SGleb Smirnoff 	/* If we still have a node or hook referenced... */
30368253c060SGleb Smirnoff 	_NGI_CLR_NODE(item);
30378253c060SGleb Smirnoff 	_NGI_CLR_HOOK(item);
30388253c060SGleb Smirnoff 
30398253c060SGleb Smirnoff #ifdef	NETGRAPH_DEBUG
30408253c060SGleb Smirnoff 	mtx_lock(&ngq_mtx);
30418253c060SGleb Smirnoff 	TAILQ_REMOVE(&ng_itemlist, item, all);
30428253c060SGleb Smirnoff 	allocated--;
30438253c060SGleb Smirnoff 	mtx_unlock(&ngq_mtx);
30448253c060SGleb Smirnoff #endif
30456aa6d011SAlexander Motin 	uma_zfree(((item->el_flags & NGQF_TYPE) == NGQF_DATA) ?
30466aa6d011SAlexander Motin 	    ng_qdzone : ng_qzone, item);
30476aa6d011SAlexander Motin }
30486aa6d011SAlexander Motin 
30496aa6d011SAlexander Motin /*
30506aa6d011SAlexander Motin  * Change type of the queue entry.
30516aa6d011SAlexander Motin  * Possibly reallocates it from another UMA zone.
30526aa6d011SAlexander Motin  */
30536aa6d011SAlexander Motin static __inline item_p
30546aa6d011SAlexander Motin ng_realloc_item(item_p pitem, int type, int flags)
30556aa6d011SAlexander Motin {
30566aa6d011SAlexander Motin 	item_p item;
30576aa6d011SAlexander Motin 	int from, to;
30586aa6d011SAlexander Motin 
30596aa6d011SAlexander Motin 	KASSERT((pitem != NULL), ("%s: can't reallocate NULL", __func__));
30606aa6d011SAlexander Motin 	KASSERT(((type & ~NGQF_TYPE) == 0),
30616aa6d011SAlexander Motin 	    ("%s: incorrect item type: %d", __func__, type));
30626aa6d011SAlexander Motin 
30636aa6d011SAlexander Motin 	from = ((pitem->el_flags & NGQF_TYPE) == NGQF_DATA);
30646aa6d011SAlexander Motin 	to = (type == NGQF_DATA);
30656aa6d011SAlexander Motin 	if (from != to) {
30666aa6d011SAlexander Motin 		/* If reallocation is required do it and copy item. */
30676aa6d011SAlexander Motin 		if ((item = ng_alloc_item(type, flags)) == NULL) {
30686aa6d011SAlexander Motin 			ng_free_item(pitem);
30696aa6d011SAlexander Motin 			return (NULL);
30706aa6d011SAlexander Motin 		}
30716aa6d011SAlexander Motin 		*item = *pitem;
30726aa6d011SAlexander Motin 		ng_free_item(pitem);
30736aa6d011SAlexander Motin 	} else
30746aa6d011SAlexander Motin 		item = pitem;
30756aa6d011SAlexander Motin 	item->el_flags = (item->el_flags & ~NGQF_TYPE) | type;
30766aa6d011SAlexander Motin 
30776aa6d011SAlexander Motin 	return (item);
30788253c060SGleb Smirnoff }
30798253c060SGleb Smirnoff 
30808253c060SGleb Smirnoff /************************************************************************
30814cf49a43SJulian Elischer 			Module routines
30824cf49a43SJulian Elischer ************************************************************************/
30834cf49a43SJulian Elischer 
30844cf49a43SJulian Elischer /*
30854cf49a43SJulian Elischer  * Handle the loading/unloading of a netgraph node type module
30864cf49a43SJulian Elischer  */
30874cf49a43SJulian Elischer int
30884cf49a43SJulian Elischer ng_mod_event(module_t mod, int event, void *data)
30894cf49a43SJulian Elischer {
30904cf49a43SJulian Elischer 	struct ng_type *const type = data;
30914bd1b557SGleb Smirnoff 	int error = 0;
30924cf49a43SJulian Elischer 
30934cf49a43SJulian Elischer 	switch (event) {
30944cf49a43SJulian Elischer 	case MOD_LOAD:
30954cf49a43SJulian Elischer 
30964cf49a43SJulian Elischer 		/* Register new netgraph node type */
30974bd1b557SGleb Smirnoff 		if ((error = ng_newtype(type)) != 0)
30984cf49a43SJulian Elischer 			break;
30994cf49a43SJulian Elischer 
31004cf49a43SJulian Elischer 		/* Call type specific code */
31014cf49a43SJulian Elischer 		if (type->mod_event != NULL)
3102069154d5SJulian Elischer 			if ((error = (*type->mod_event)(mod, event, data))) {
3103c4282b74SGleb Smirnoff 				TYPELIST_WLOCK();
3104c73b94a2SJulian Elischer 				type->refs--;	/* undo it */
31054cf49a43SJulian Elischer 				LIST_REMOVE(type, types);
3106c4282b74SGleb Smirnoff 				TYPELIST_WUNLOCK();
3107069154d5SJulian Elischer 			}
31084cf49a43SJulian Elischer 		break;
31094cf49a43SJulian Elischer 
31104cf49a43SJulian Elischer 	case MOD_UNLOAD:
3111c73b94a2SJulian Elischer 		if (type->refs > 1) {		/* make sure no nodes exist! */
31124cf49a43SJulian Elischer 			error = EBUSY;
3113c73b94a2SJulian Elischer 		} else {
31144bd1b557SGleb Smirnoff 			if (type->refs == 0) /* failed load, nothing to undo */
3115c73b94a2SJulian Elischer 				break;
31164cf49a43SJulian Elischer 			if (type->mod_event != NULL) {	/* check with type */
31174cf49a43SJulian Elischer 				error = (*type->mod_event)(mod, event, data);
31184bd1b557SGleb Smirnoff 				if (error != 0)	/* type refuses.. */
31194cf49a43SJulian Elischer 					break;
31204cf49a43SJulian Elischer 			}
3121c4282b74SGleb Smirnoff 			TYPELIST_WLOCK();
31224cf49a43SJulian Elischer 			LIST_REMOVE(type, types);
3123c4282b74SGleb Smirnoff 			TYPELIST_WUNLOCK();
31244cf49a43SJulian Elischer 		}
31254cf49a43SJulian Elischer 		break;
31264cf49a43SJulian Elischer 
31274cf49a43SJulian Elischer 	default:
31284cf49a43SJulian Elischer 		if (type->mod_event != NULL)
31294cf49a43SJulian Elischer 			error = (*type->mod_event)(mod, event, data);
31304cf49a43SJulian Elischer 		else
31313e019deaSPoul-Henning Kamp 			error = EOPNOTSUPP;		/* XXX ? */
31324cf49a43SJulian Elischer 		break;
31334cf49a43SJulian Elischer 	}
31344cf49a43SJulian Elischer 	return (error);
31354cf49a43SJulian Elischer }
31364cf49a43SJulian Elischer 
3137687adb70SGleb Smirnoff static void
3138687adb70SGleb Smirnoff vnet_netgraph_init(const void *unused __unused)
3139687adb70SGleb Smirnoff {
3140687adb70SGleb Smirnoff 
3141687adb70SGleb Smirnoff 	/* We start with small hashes, but they can grow. */
3142687adb70SGleb Smirnoff 	V_ng_ID_hash = hashinit(16, M_NETGRAPH_NODE, &V_ng_ID_hmask);
3143687adb70SGleb Smirnoff 	V_ng_name_hash = hashinit(16, M_NETGRAPH_NODE, &V_ng_name_hmask);
3144687adb70SGleb Smirnoff }
3145687adb70SGleb Smirnoff VNET_SYSINIT(vnet_netgraph_init, SI_SUB_NETGRAPH, SI_ORDER_FIRST,
3146687adb70SGleb Smirnoff     vnet_netgraph_init, NULL);
3147687adb70SGleb Smirnoff 
3148eddfbb76SRobert Watson #ifdef VIMAGE
3149d0728d71SRobert Watson static void
3150d0728d71SRobert Watson vnet_netgraph_uninit(const void *unused __unused)
3151bc29160dSMarko Zec {
3152a3f93b72SMarko Zec 	node_p node = NULL, last_killed = NULL;
3153a3f93b72SMarko Zec 	int i;
3154bc29160dSMarko Zec 
3155a3f93b72SMarko Zec 	do {
3156a3f93b72SMarko Zec 		/* Find a node to kill */
3157687adb70SGleb Smirnoff 		IDHASH_RLOCK();
3158687adb70SGleb Smirnoff 		for (i = 0; i <= V_ng_ID_hmask; i++) {
3159687adb70SGleb Smirnoff 			LIST_FOREACH(node, &V_ng_ID_hash[i], nd_idnodes) {
3160a3f93b72SMarko Zec 				if (node != &ng_deadnode) {
3161a3f93b72SMarko Zec 					NG_NODE_REF(node);
3162a3f93b72SMarko Zec 					break;
3163a3f93b72SMarko Zec 				}
3164a3f93b72SMarko Zec 			}
3165a3f93b72SMarko Zec 			if (node != NULL)
3166a3f93b72SMarko Zec 				break;
3167a3f93b72SMarko Zec 		}
3168687adb70SGleb Smirnoff 		IDHASH_RUNLOCK();
3169a3f93b72SMarko Zec 
3170a3f93b72SMarko Zec 		/* Attempt to kill it only if it is a regular node */
3171a3f93b72SMarko Zec 		if (node != NULL) {
3172bc29160dSMarko Zec 			if (node == last_killed) {
3173a3f93b72SMarko Zec 				if (node->nd_flags & NGF_REALLY_DIE)
3174a3f93b72SMarko Zec 					panic("ng node %s won't die",
3175a3f93b72SMarko Zec 					    node->nd_name);
3176cd698c51SMark Johnston 				/* The node persisted itself.  Try again. */
3177bc29160dSMarko Zec 				node->nd_flags |= NGF_REALLY_DIE;
3178bc29160dSMarko Zec 			}
3179bc29160dSMarko Zec 			ng_rmnode(node, NULL, NULL, 0);
3180a3f93b72SMarko Zec 			NG_NODE_UNREF(node);
3181bc29160dSMarko Zec 			last_killed = node;
3182bc29160dSMarko Zec 		}
3183a3f93b72SMarko Zec 	} while (node != NULL);
3184687adb70SGleb Smirnoff 
3185687adb70SGleb Smirnoff 	hashdestroy(V_ng_name_hash, M_NETGRAPH_NODE, V_ng_name_hmask);
3186687adb70SGleb Smirnoff 	hashdestroy(V_ng_ID_hash, M_NETGRAPH_NODE, V_ng_ID_hmask);
3187bc29160dSMarko Zec }
3188320d00eeSGleb Smirnoff VNET_SYSUNINIT(vnet_netgraph_uninit, SI_SUB_NETGRAPH, SI_ORDER_FIRST,
3189d0728d71SRobert Watson     vnet_netgraph_uninit, NULL);
3190bc29160dSMarko Zec #endif /* VIMAGE */
3191bc29160dSMarko Zec 
31924cf49a43SJulian Elischer /*
31934cf49a43SJulian Elischer  * Handle loading and unloading for this code.
31944cf49a43SJulian Elischer  * The only thing we need to link into is the NETISR strucure.
31954cf49a43SJulian Elischer  */
31964cf49a43SJulian Elischer static int
31974cf49a43SJulian Elischer ngb_mod_event(module_t mod, int event, void *data)
31984cf49a43SJulian Elischer {
3199f2fbb838SAlexander Motin 	struct proc *p;
3200f2fbb838SAlexander Motin 	struct thread *td;
3201f2fbb838SAlexander Motin 	int i, error = 0;
32024cf49a43SJulian Elischer 
32034cf49a43SJulian Elischer 	switch (event) {
32044cf49a43SJulian Elischer 	case MOD_LOAD:
32051489164fSGleb Smirnoff 		/* Initialize everything. */
32062c8dda8dSWojciech A. Koszek 		NG_WORKLIST_LOCK_INIT();
3207c4282b74SGleb Smirnoff 		rw_init(&ng_typelist_lock, "netgraph types");
3208c4282b74SGleb Smirnoff 		rw_init(&ng_idhash_lock, "netgraph idhash");
3209c4282b74SGleb Smirnoff 		rw_init(&ng_namehash_lock, "netgraph namehash");
3210d2fd0788SAlexander V. Chernikov 		rw_init(&ng_topo_lock, "netgraph topology mutex");
32111489164fSGleb Smirnoff #ifdef	NETGRAPH_DEBUG
3212cfea3f85SAlexander Motin 		mtx_init(&ng_nodelist_mtx, "netgraph nodelist mutex", NULL,
3213cfea3f85SAlexander Motin 		    MTX_DEF);
32141489164fSGleb Smirnoff 		mtx_init(&ngq_mtx, "netgraph item list mutex", NULL,
3215efd8e7c9SDon Lewis 		    MTX_DEF);
32161489164fSGleb Smirnoff #endif
32171489164fSGleb Smirnoff 		ng_qzone = uma_zcreate("NetGraph items", sizeof(struct ng_item),
321877a117caSGleb Smirnoff 		    NULL, NULL, NULL, NULL, UMA_ALIGN_CACHE, 0);
32191489164fSGleb Smirnoff 		uma_zone_set_max(ng_qzone, maxalloc);
32204bd1b557SGleb Smirnoff 		ng_qdzone = uma_zcreate("NetGraph data items",
32214bd1b557SGleb Smirnoff 		    sizeof(struct ng_item), NULL, NULL, NULL, NULL,
322277a117caSGleb Smirnoff 		    UMA_ALIGN_CACHE, 0);
32236aa6d011SAlexander Motin 		uma_zone_set_max(ng_qdzone, maxdata);
3224f2fbb838SAlexander Motin 		/* Autoconfigure number of threads. */
3225f2fbb838SAlexander Motin 		if (numthreads <= 0)
3226f2fbb838SAlexander Motin 			numthreads = mp_ncpus;
3227f2fbb838SAlexander Motin 		/* Create threads. */
3228f2fbb838SAlexander Motin     		p = NULL; /* start with no process */
3229f2fbb838SAlexander Motin 		for (i = 0; i < numthreads; i++) {
3230f2fbb838SAlexander Motin 			if (kproc_kthread_add(ngthread, NULL, &p, &td,
3231f2fbb838SAlexander Motin 			    RFHIGHPID, 0, "ng_queue", "ng_queue%d", i)) {
3232f2fbb838SAlexander Motin 				numthreads = i;
3233f2fbb838SAlexander Motin 				break;
3234f2fbb838SAlexander Motin 			}
3235f2fbb838SAlexander Motin 		}
32364cf49a43SJulian Elischer 		break;
32374cf49a43SJulian Elischer 	case MOD_UNLOAD:
323864efc707SRobert Watson 		/* You can't unload it because an interface may be using it. */
32394cf49a43SJulian Elischer 		error = EBUSY;
32404cf49a43SJulian Elischer 		break;
32414cf49a43SJulian Elischer 	default:
32424cf49a43SJulian Elischer 		error = EOPNOTSUPP;
32434cf49a43SJulian Elischer 		break;
32444cf49a43SJulian Elischer 	}
32454cf49a43SJulian Elischer 	return (error);
32464cf49a43SJulian Elischer }
32474cf49a43SJulian Elischer 
32484cf49a43SJulian Elischer static moduledata_t netgraph_mod = {
32494cf49a43SJulian Elischer 	"netgraph",
32504cf49a43SJulian Elischer 	ngb_mod_event,
32514cf49a43SJulian Elischer 	(NULL)
32524cf49a43SJulian Elischer };
3253320d00eeSGleb Smirnoff DECLARE_MODULE(netgraph, netgraph_mod, SI_SUB_NETGRAPH, SI_ORDER_FIRST);
32547029da5cSPawel Biernacki SYSCTL_NODE(_net, OID_AUTO, graph, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
32557029da5cSPawel Biernacki     "netgraph Family");
3256f0188618SHans Petter Selasky SYSCTL_INT(_net_graph, OID_AUTO, abi_version, CTLFLAG_RD, SYSCTL_NULL_INT_PTR, NG_ABI_VERSION,"");
3257f0188618SHans Petter Selasky SYSCTL_INT(_net_graph, OID_AUTO, msg_version, CTLFLAG_RD, SYSCTL_NULL_INT_PTR, NG_VERSION, "");
32584cf49a43SJulian Elischer 
325930400f03SJulian Elischer #ifdef	NETGRAPH_DEBUG
326030400f03SJulian Elischer void
326130400f03SJulian Elischer dumphook (hook_p hook, char *file, int line)
326230400f03SJulian Elischer {
326330400f03SJulian Elischer 	printf("hook: name %s, %d refs, Last touched:\n",
326430400f03SJulian Elischer 		_NG_HOOK_NAME(hook), hook->hk_refs);
326530400f03SJulian Elischer 	printf("	Last active @ %s, line %d\n",
326630400f03SJulian Elischer 		hook->lastfile, hook->lastline);
326730400f03SJulian Elischer 	if (line) {
326830400f03SJulian Elischer 		printf(" problem discovered at file %s, line %d\n", file, line);
3269e5fe87b3SGleb Smirnoff #ifdef KDB
3270e5fe87b3SGleb Smirnoff 		kdb_backtrace();
3271e5fe87b3SGleb Smirnoff #endif
327230400f03SJulian Elischer 	}
327330400f03SJulian Elischer }
327430400f03SJulian Elischer 
327530400f03SJulian Elischer void
327630400f03SJulian Elischer dumpnode(node_p node, char *file, int line)
327730400f03SJulian Elischer {
327830400f03SJulian Elischer 	printf("node: ID [%x]: type '%s', %d hooks, flags 0x%x, %d refs, %s:\n",
32796b795970SJulian Elischer 		_NG_NODE_ID(node), node->nd_type->name,
328030400f03SJulian Elischer 		node->nd_numhooks, node->nd_flags,
328130400f03SJulian Elischer 		node->nd_refs, node->nd_name);
328230400f03SJulian Elischer 	printf("	Last active @ %s, line %d\n",
328330400f03SJulian Elischer 		node->lastfile, node->lastline);
328430400f03SJulian Elischer 	if (line) {
328530400f03SJulian Elischer 		printf(" problem discovered at file %s, line %d\n", file, line);
3286e5fe87b3SGleb Smirnoff #ifdef KDB
3287e5fe87b3SGleb Smirnoff 		kdb_backtrace();
3288e5fe87b3SGleb Smirnoff #endif
328930400f03SJulian Elischer 	}
329030400f03SJulian Elischer }
329130400f03SJulian Elischer 
3292069154d5SJulian Elischer void
3293069154d5SJulian Elischer dumpitem(item_p item, char *file, int line)
3294069154d5SJulian Elischer {
3295069154d5SJulian Elischer 	printf(" ACTIVE item, last used at %s, line %d",
3296069154d5SJulian Elischer 		item->lastfile, item->lastline);
32976b795970SJulian Elischer 	switch(item->el_flags & NGQF_TYPE) {
32986b795970SJulian Elischer 	case NGQF_DATA:
3299069154d5SJulian Elischer 		printf(" - [data]\n");
33006b795970SJulian Elischer 		break;
33016b795970SJulian Elischer 	case NGQF_MESG:
33026b795970SJulian Elischer 		printf(" - retaddr[%d]:\n", _NGI_RETADDR(item));
33036b795970SJulian Elischer 		break;
33046b795970SJulian Elischer 	case NGQF_FN:
3305857304e6SRuslan Ermilov 		printf(" - fn@%p (%p, %p, %p, %d (%x))\n",
3306857304e6SRuslan Ermilov 			_NGI_FN(item),
3307857304e6SRuslan Ermilov 			_NGI_NODE(item),
3308857304e6SRuslan Ermilov 			_NGI_HOOK(item),
3309857304e6SRuslan Ermilov 			item->body.fn.fn_arg1,
3310857304e6SRuslan Ermilov 			item->body.fn.fn_arg2,
3311857304e6SRuslan Ermilov 			item->body.fn.fn_arg2);
3312857304e6SRuslan Ermilov 		break;
3313e088dd4cSAlexander Motin 	case NGQF_FN2:
3314eb4687d2SAlexander Motin 		printf(" - fn2@%p (%p, %p, %p, %d (%x))\n",
3315857304e6SRuslan Ermilov 			_NGI_FN2(item),
33166064e568SGleb Smirnoff 			_NGI_NODE(item),
33176064e568SGleb Smirnoff 			_NGI_HOOK(item),
33186b795970SJulian Elischer 			item->body.fn.fn_arg1,
33196b795970SJulian Elischer 			item->body.fn.fn_arg2,
33206b795970SJulian Elischer 			item->body.fn.fn_arg2);
33216b795970SJulian Elischer 		break;
3322069154d5SJulian Elischer 	}
332330400f03SJulian Elischer 	if (line) {
3324069154d5SJulian Elischer 		printf(" problem discovered at file %s, line %d\n", file, line);
33256064e568SGleb Smirnoff 		if (_NGI_NODE(item)) {
332630400f03SJulian Elischer 			printf("node %p ([%x])\n",
33276064e568SGleb Smirnoff 				_NGI_NODE(item), ng_node2ID(_NGI_NODE(item)));
3328069154d5SJulian Elischer 		}
332930400f03SJulian Elischer 	}
333030400f03SJulian Elischer }
333130400f03SJulian Elischer 
333230400f03SJulian Elischer static void
333330400f03SJulian Elischer ng_dumpitems(void)
333430400f03SJulian Elischer {
333530400f03SJulian Elischer 	item_p item;
333630400f03SJulian Elischer 	int i = 1;
333730400f03SJulian Elischer 	TAILQ_FOREACH(item, &ng_itemlist, all) {
333830400f03SJulian Elischer 		printf("[%d] ", i++);
333930400f03SJulian Elischer 		dumpitem(item, NULL, 0);
334030400f03SJulian Elischer 	}
334130400f03SJulian Elischer }
334230400f03SJulian Elischer 
334330400f03SJulian Elischer static void
334430400f03SJulian Elischer ng_dumpnodes(void)
334530400f03SJulian Elischer {
334630400f03SJulian Elischer 	node_p node;
334730400f03SJulian Elischer 	int i = 1;
334853f9c5e9SRobert Watson 	mtx_lock(&ng_nodelist_mtx);
334930400f03SJulian Elischer 	SLIST_FOREACH(node, &ng_allnodes, nd_all) {
335030400f03SJulian Elischer 		printf("[%d] ", i++);
335130400f03SJulian Elischer 		dumpnode(node, NULL, 0);
335230400f03SJulian Elischer 	}
335353f9c5e9SRobert Watson 	mtx_unlock(&ng_nodelist_mtx);
335430400f03SJulian Elischer }
335530400f03SJulian Elischer 
335630400f03SJulian Elischer static void
335730400f03SJulian Elischer ng_dumphooks(void)
335830400f03SJulian Elischer {
335930400f03SJulian Elischer 	hook_p hook;
336030400f03SJulian Elischer 	int i = 1;
336153f9c5e9SRobert Watson 	mtx_lock(&ng_nodelist_mtx);
336230400f03SJulian Elischer 	SLIST_FOREACH(hook, &ng_allhooks, hk_all) {
336330400f03SJulian Elischer 		printf("[%d] ", i++);
336430400f03SJulian Elischer 		dumphook(hook, NULL, 0);
336530400f03SJulian Elischer 	}
336653f9c5e9SRobert Watson 	mtx_unlock(&ng_nodelist_mtx);
336730400f03SJulian Elischer }
3368069154d5SJulian Elischer 
3369069154d5SJulian Elischer static int
3370069154d5SJulian Elischer sysctl_debug_ng_dump_items(SYSCTL_HANDLER_ARGS)
3371069154d5SJulian Elischer {
3372069154d5SJulian Elischer 	int error;
3373069154d5SJulian Elischer 	int val;
3374069154d5SJulian Elischer 	int i;
3375069154d5SJulian Elischer 
3376069154d5SJulian Elischer 	val = allocated;
3377069154d5SJulian Elischer 	i = 1;
3378041b706bSDavid Malone 	error = sysctl_handle_int(oidp, &val, 0, req);
33796b795970SJulian Elischer 	if (error != 0 || req->newptr == NULL)
33806b795970SJulian Elischer 		return (error);
33816b795970SJulian Elischer 	if (val == 42) {
338230400f03SJulian Elischer 		ng_dumpitems();
338330400f03SJulian Elischer 		ng_dumpnodes();
338430400f03SJulian Elischer 		ng_dumphooks();
3385069154d5SJulian Elischer 	}
33866b795970SJulian Elischer 	return (0);
3387069154d5SJulian Elischer }
3388069154d5SJulian Elischer 
33897029da5cSPawel Biernacki SYSCTL_PROC(_debug, OID_AUTO, ng_dump_items,
33907029da5cSPawel Biernacki     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 0, sizeof(int),
33917029da5cSPawel Biernacki     sysctl_debug_ng_dump_items, "I",
33927029da5cSPawel Biernacki     "Number of allocated items");
339330400f03SJulian Elischer #endif	/* NETGRAPH_DEBUG */
3394069154d5SJulian Elischer 
3395069154d5SJulian Elischer /***********************************************************************
3396069154d5SJulian Elischer * Worklist routines
3397069154d5SJulian Elischer **********************************************************************/
3398069154d5SJulian Elischer /*
3399069154d5SJulian Elischer  * Pick a node off the list of nodes with work,
3400f2fbb838SAlexander Motin  * try get an item to process off it. Remove the node from the list.
3401069154d5SJulian Elischer  */
3402069154d5SJulian Elischer static void
3403f2fbb838SAlexander Motin ngthread(void *arg)
3404069154d5SJulian Elischer {
3405069154d5SJulian Elischer 	for (;;) {
340635e67a79SGleb Smirnoff 		struct epoch_tracker et;
3407394cb30aSAlexander Motin 		node_p  node;
3408394cb30aSAlexander Motin 
3409394cb30aSAlexander Motin 		/* Get node from the worklist. */
34102c8dda8dSWojciech A. Koszek 		NG_WORKLIST_LOCK();
3411f2fbb838SAlexander Motin 		while ((node = STAILQ_FIRST(&ng_worklist)) == NULL)
3412f2fbb838SAlexander Motin 			NG_WORKLIST_SLEEP();
34139852972bSAlexander Motin 		STAILQ_REMOVE_HEAD(&ng_worklist, nd_input_queue.q_work);
34142c8dda8dSWojciech A. Koszek 		NG_WORKLIST_UNLOCK();
3415bc29160dSMarko Zec 		CURVNET_SET(node->nd_vnet);
34162955ee18SGleb Smirnoff 		CTR3(KTR_NET, "%20s: node [%x] (%p) taken off worklist",
34172955ee18SGleb Smirnoff 		    __func__, node->nd_ID, node);
3418069154d5SJulian Elischer 		/*
3419069154d5SJulian Elischer 		 * We have the node. We also take over the reference
3420069154d5SJulian Elischer 		 * that the list had on it.
3421069154d5SJulian Elischer 		 * Now process as much as you can, until it won't
3422069154d5SJulian Elischer 		 * let you have another item off the queue.
3423069154d5SJulian Elischer 		 * All this time, keep the reference
3424069154d5SJulian Elischer 		 * that lets us be sure that the node still exists.
3425069154d5SJulian Elischer 		 * Let the reference go at the last minute.
3426069154d5SJulian Elischer 		 */
342735e67a79SGleb Smirnoff 		NET_EPOCH_ENTER(et);
3428069154d5SJulian Elischer 		for (;;) {
3429394cb30aSAlexander Motin 			item_p item;
3430714fb865SGleb Smirnoff 			int rw;
3431714fb865SGleb Smirnoff 
34322c8dda8dSWojciech A. Koszek 			NG_QUEUE_LOCK(&node->nd_input_queue);
34339852972bSAlexander Motin 			item = ng_dequeue(node, &rw);
3434069154d5SJulian Elischer 			if (item == NULL) {
34359852972bSAlexander Motin 				node->nd_input_queue.q_flags2 &= ~NGQ2_WORKQ;
34362c8dda8dSWojciech A. Koszek 				NG_QUEUE_UNLOCK(&node->nd_input_queue);
3437069154d5SJulian Elischer 				break; /* go look for another node */
3438069154d5SJulian Elischer 			} else {
34392c8dda8dSWojciech A. Koszek 				NG_QUEUE_UNLOCK(&node->nd_input_queue);
34405951069aSJulian Elischer 				NGI_GET_NODE(item, node); /* zaps stored node */
3441714fb865SGleb Smirnoff 				ng_apply_item(node, item, rw);
34425951069aSJulian Elischer 				NG_NODE_UNREF(node);
3443069154d5SJulian Elischer 			}
3444069154d5SJulian Elischer 		}
344535e67a79SGleb Smirnoff 		NET_EPOCH_EXIT(et);
3446a96dcd84SJulian Elischer 		NG_NODE_UNREF(node);
3447bc29160dSMarko Zec 		CURVNET_RESTORE();
3448069154d5SJulian Elischer 	}
3449069154d5SJulian Elischer }
3450069154d5SJulian Elischer 
345133338e73SJulian Elischer /*
345233338e73SJulian Elischer  * XXX
345333338e73SJulian Elischer  * It's posible that a debugging NG_NODE_REF may need
345433338e73SJulian Elischer  * to be outside the mutex zone
345533338e73SJulian Elischer  */
3456069154d5SJulian Elischer static void
3457394cb30aSAlexander Motin ng_worklist_add(node_p node)
3458069154d5SJulian Elischer {
3459f912c0f7SGleb Smirnoff 
34605bc15201SGleb Smirnoff 	mtx_assert(&node->nd_input_queue.q_mtx, MA_OWNED);
3461f912c0f7SGleb Smirnoff 
34629852972bSAlexander Motin 	if ((node->nd_input_queue.q_flags2 & NGQ2_WORKQ) == 0) {
3463069154d5SJulian Elischer 		/*
3464069154d5SJulian Elischer 		 * If we are not already on the work queue,
3465069154d5SJulian Elischer 		 * then put us on.
3466069154d5SJulian Elischer 		 */
34679852972bSAlexander Motin 		node->nd_input_queue.q_flags2 |= NGQ2_WORKQ;
34684bd1b557SGleb Smirnoff 		NG_NODE_REF(node); /* XXX safe in mutex? */
34692c8dda8dSWojciech A. Koszek 		NG_WORKLIST_LOCK();
34709852972bSAlexander Motin 		STAILQ_INSERT_TAIL(&ng_worklist, node, nd_input_queue.q_work);
34712c8dda8dSWojciech A. Koszek 		NG_WORKLIST_UNLOCK();
34722955ee18SGleb Smirnoff 		CTR3(KTR_NET, "%20s: node [%x] (%p) put on worklist", __func__,
34732955ee18SGleb Smirnoff 		    node->nd_ID, node);
3474f2fbb838SAlexander Motin 		NG_WORKLIST_WAKEUP();
3475394cb30aSAlexander Motin 	} else {
34762955ee18SGleb Smirnoff 		CTR3(KTR_NET, "%20s: node [%x] (%p) already on worklist",
34772955ee18SGleb Smirnoff 		    __func__, node->nd_ID, node);
3478394cb30aSAlexander Motin 	}
3479069154d5SJulian Elischer }
3480069154d5SJulian Elischer 
3481069154d5SJulian Elischer /***********************************************************************
3482069154d5SJulian Elischer * Externally useable functions to set up a queue item ready for sending
3483069154d5SJulian Elischer ***********************************************************************/
3484069154d5SJulian Elischer 
348530400f03SJulian Elischer #ifdef	NETGRAPH_DEBUG
348630400f03SJulian Elischer #define	ITEM_DEBUG_CHECKS						\
34874cf49a43SJulian Elischer 	do {								\
34881acb27c6SJulian Elischer 		if (NGI_NODE(item) ) {					\
3489069154d5SJulian Elischer 			printf("item already has node");		\
34903de213ccSRobert Watson 			kdb_enter(KDB_WHY_NETGRAPH, "has node");	\
34911acb27c6SJulian Elischer 			NGI_CLR_NODE(item);				\
3492069154d5SJulian Elischer 		}							\
34931acb27c6SJulian Elischer 		if (NGI_HOOK(item) ) {					\
3494069154d5SJulian Elischer 			printf("item already has hook");		\
34953de213ccSRobert Watson 			kdb_enter(KDB_WHY_NETGRAPH, "has hook");	\
34961acb27c6SJulian Elischer 			NGI_CLR_HOOK(item);				\
3497069154d5SJulian Elischer 		}							\
3498069154d5SJulian Elischer 	} while (0)
3499069154d5SJulian Elischer #else
350030400f03SJulian Elischer #define ITEM_DEBUG_CHECKS
3501069154d5SJulian Elischer #endif
3502069154d5SJulian Elischer 
3503069154d5SJulian Elischer /*
35048ed370fdSJulian Elischer  * Put mbuf into the item.
3505069154d5SJulian Elischer  * Hook and node references will be removed when the item is dequeued.
3506069154d5SJulian Elischer  * (or equivalent)
3507069154d5SJulian Elischer  * (XXX) Unsafe because no reference held by peer on remote node.
3508069154d5SJulian Elischer  * remote node might go away in this timescale.
3509069154d5SJulian Elischer  * We know the hooks can't go away because that would require getting
3510069154d5SJulian Elischer  * a writer item on both nodes and we must have at least a  reader
35114be59335SGleb Smirnoff  * here to be able to do this.
3512069154d5SJulian Elischer  * Note that the hook loaded is the REMOTE hook.
3513069154d5SJulian Elischer  *
3514069154d5SJulian Elischer  * This is possibly in the critical path for new data.
3515069154d5SJulian Elischer  */
3516069154d5SJulian Elischer item_p
351742282202SGleb Smirnoff ng_package_data(struct mbuf *m, int flags)
3518069154d5SJulian Elischer {
3519069154d5SJulian Elischer 	item_p item;
3520069154d5SJulian Elischer 
35216aa6d011SAlexander Motin 	if ((item = ng_alloc_item(NGQF_DATA, flags)) == NULL) {
3522069154d5SJulian Elischer 		NG_FREE_M(m);
3523069154d5SJulian Elischer 		return (NULL);
3524069154d5SJulian Elischer 	}
352530400f03SJulian Elischer 	ITEM_DEBUG_CHECKS;
35266aa6d011SAlexander Motin 	item->el_flags |= NGQF_READER;
3527069154d5SJulian Elischer 	NGI_M(item) = m;
3528069154d5SJulian Elischer 	return (item);
3529069154d5SJulian Elischer }
3530069154d5SJulian Elischer 
3531069154d5SJulian Elischer /*
3532069154d5SJulian Elischer  * Allocate a queue item and put items into it..
3533069154d5SJulian Elischer  * Evaluate the address as this will be needed to queue it and
3534069154d5SJulian Elischer  * to work out what some of the fields should be.
3535069154d5SJulian Elischer  * Hook and node references will be removed when the item is dequeued.
3536069154d5SJulian Elischer  * (or equivalent)
3537069154d5SJulian Elischer  */
3538069154d5SJulian Elischer item_p
353942282202SGleb Smirnoff ng_package_msg(struct ng_mesg *msg, int flags)
3540069154d5SJulian Elischer {
3541069154d5SJulian Elischer 	item_p item;
3542069154d5SJulian Elischer 
35436aa6d011SAlexander Motin 	if ((item = ng_alloc_item(NGQF_MESG, flags)) == NULL) {
3544069154d5SJulian Elischer 		NG_FREE_MSG(msg);
3545069154d5SJulian Elischer 		return (NULL);
3546069154d5SJulian Elischer 	}
354730400f03SJulian Elischer 	ITEM_DEBUG_CHECKS;
35486f683eeeSGleb Smirnoff 	/* Messages items count as writers unless explicitly exempted. */
35496f683eeeSGleb Smirnoff 	if (msg->header.cmd & NGM_READONLY)
35506aa6d011SAlexander Motin 		item->el_flags |= NGQF_READER;
35516f683eeeSGleb Smirnoff 	else
35526aa6d011SAlexander Motin 		item->el_flags |= NGQF_WRITER;
3553069154d5SJulian Elischer 	/*
3554069154d5SJulian Elischer 	 * Set the current lasthook into the queue item
3555069154d5SJulian Elischer 	 */
3556069154d5SJulian Elischer 	NGI_MSG(item) = msg;
3557facfd889SArchie Cobbs 	NGI_RETADDR(item) = 0;
3558069154d5SJulian Elischer 	return (item);
3559069154d5SJulian Elischer }
3560069154d5SJulian Elischer 
35611acb27c6SJulian Elischer #define SET_RETADDR(item, here, retaddr)				\
35626b795970SJulian Elischer 	do {	/* Data or fn items don't have retaddrs */		\
35636b795970SJulian Elischer 		if ((item->el_flags & NGQF_TYPE) == NGQF_MESG) {	\
3564069154d5SJulian Elischer 			if (retaddr) {					\
3565069154d5SJulian Elischer 				NGI_RETADDR(item) = retaddr;		\
35664cf49a43SJulian Elischer 			} else {					\
3567069154d5SJulian Elischer 				/*					\
3568069154d5SJulian Elischer 				 * The old return address should be ok.	\
3569069154d5SJulian Elischer 				 * If there isn't one, use the address	\
3570069154d5SJulian Elischer 				 * here.				\
3571069154d5SJulian Elischer 				 */					\
3572069154d5SJulian Elischer 				if (NGI_RETADDR(item) == 0) {		\
3573069154d5SJulian Elischer 					NGI_RETADDR(item)		\
3574069154d5SJulian Elischer 						= ng_node2ID(here);	\
3575069154d5SJulian Elischer 				}					\
3576069154d5SJulian Elischer 			}						\
35774cf49a43SJulian Elischer 		}							\
35784cf49a43SJulian Elischer 	} while (0)
35794cf49a43SJulian Elischer 
35804cf49a43SJulian Elischer int
3581069154d5SJulian Elischer ng_address_hook(node_p here, item_p item, hook_p hook, ng_ID_t retaddr)
35824cf49a43SJulian Elischer {
35831acb27c6SJulian Elischer 	hook_p peer;
35841acb27c6SJulian Elischer 	node_p peernode;
358530400f03SJulian Elischer 	ITEM_DEBUG_CHECKS;
3586069154d5SJulian Elischer 	/*
3587069154d5SJulian Elischer 	 * Quick sanity check..
358828323addSBryan Drewery 	 * Since a hook holds a reference on its node, once we know
358930400f03SJulian Elischer 	 * that the peer is still connected (even if invalid,) we know
359030400f03SJulian Elischer 	 * that the peer node is present, though maybe invalid.
3591069154d5SJulian Elischer 	 */
3592d2fd0788SAlexander V. Chernikov 	TOPOLOGY_RLOCK();
35934bd1b557SGleb Smirnoff 	if ((hook == NULL) || NG_HOOK_NOT_VALID(hook) ||
3594a04e9846SAlexander Motin 	    NG_HOOK_NOT_VALID(peer = NG_HOOK_PEER(hook)) ||
3595a04e9846SAlexander Motin 	    NG_NODE_NOT_VALID(peernode = NG_PEER_NODE(hook))) {
3596069154d5SJulian Elischer 		NG_FREE_ITEM(item);
35976b795970SJulian Elischer 		TRAP_ERROR();
3598d2fd0788SAlexander V. Chernikov 		TOPOLOGY_RUNLOCK();
3599e08d3e3cSJulian Elischer 		return (ENETDOWN);
36004cf49a43SJulian Elischer 	}
36014cf49a43SJulian Elischer 
36024cf49a43SJulian Elischer 	/*
3603069154d5SJulian Elischer 	 * Transfer our interest to the other (peer) end.
36044cf49a43SJulian Elischer 	 */
36051acb27c6SJulian Elischer 	NG_HOOK_REF(peer);
36061acb27c6SJulian Elischer 	NG_NODE_REF(peernode);
3607a04e9846SAlexander Motin 	NGI_SET_HOOK(item, peer);
36081acb27c6SJulian Elischer 	NGI_SET_NODE(item, peernode);
36098b68f82fSJulian Elischer 	SET_RETADDR(item, here, retaddr);
3610a7da736aSGleb Smirnoff 
3611d2fd0788SAlexander V. Chernikov 	TOPOLOGY_RUNLOCK();
3612a7da736aSGleb Smirnoff 
3613069154d5SJulian Elischer 	return (0);
3614069154d5SJulian Elischer }
3615069154d5SJulian Elischer 
36164cf49a43SJulian Elischer int
3617707d2058SMax Khon ng_address_path(node_p here, item_p item, const char *address, ng_ID_t retaddr)
36184cf49a43SJulian Elischer {
36194cf49a43SJulian Elischer 	node_p	dest = NULL;
3620069154d5SJulian Elischer 	hook_p	hook = NULL;
36214cf49a43SJulian Elischer 	int	error;
3622069154d5SJulian Elischer 
362330400f03SJulian Elischer 	ITEM_DEBUG_CHECKS;
3624069154d5SJulian Elischer 	/*
3625069154d5SJulian Elischer 	 * Note that ng_path2noderef increments the reference count
3626069154d5SJulian Elischer 	 * on the node for us if it finds one. So we don't have to.
3627069154d5SJulian Elischer 	 */
3628069154d5SJulian Elischer 	error = ng_path2noderef(here, address, &dest, &hook);
3629069154d5SJulian Elischer 	if (error) {
3630069154d5SJulian Elischer 		NG_FREE_ITEM(item);
363130400f03SJulian Elischer 		return (error);
3632069154d5SJulian Elischer 	}
36331acb27c6SJulian Elischer 	NGI_SET_NODE(item, dest);
3634a7da736aSGleb Smirnoff 	if (hook)
36351acb27c6SJulian Elischer 		NGI_SET_HOOK(item, hook);
3636a7da736aSGleb Smirnoff 
36371acb27c6SJulian Elischer 	SET_RETADDR(item, here, retaddr);
3638069154d5SJulian Elischer 	return (0);
3639069154d5SJulian Elischer }
3640069154d5SJulian Elischer 
3641069154d5SJulian Elischer int
3642069154d5SJulian Elischer ng_address_ID(node_p here, item_p item, ng_ID_t ID, ng_ID_t retaddr)
3643069154d5SJulian Elischer {
3644069154d5SJulian Elischer 	node_p dest;
3645069154d5SJulian Elischer 
364630400f03SJulian Elischer 	ITEM_DEBUG_CHECKS;
3647069154d5SJulian Elischer 	/*
3648069154d5SJulian Elischer 	 * Find the target node.
3649069154d5SJulian Elischer 	 */
3650069154d5SJulian Elischer 	dest = ng_ID2noderef(ID); /* GETS REFERENCE! */
3651069154d5SJulian Elischer 	if (dest == NULL) {
3652069154d5SJulian Elischer 		NG_FREE_ITEM(item);
36536b795970SJulian Elischer 		TRAP_ERROR();
3654069154d5SJulian Elischer 		return(EINVAL);
3655069154d5SJulian Elischer 	}
3656069154d5SJulian Elischer 	/* Fill out the contents */
36571acb27c6SJulian Elischer 	NGI_SET_NODE(item, dest);
36581acb27c6SJulian Elischer 	NGI_CLR_HOOK(item);
36591acb27c6SJulian Elischer 	SET_RETADDR(item, here, retaddr);
3660069154d5SJulian Elischer 	return (0);
3661069154d5SJulian Elischer }
3662069154d5SJulian Elischer 
3663069154d5SJulian Elischer /*
3664069154d5SJulian Elischer  * special case to send a message to self (e.g. destroy node)
3665069154d5SJulian Elischer  * Possibly indicate an arrival hook too.
3666069154d5SJulian Elischer  * Useful for removing that hook :-)
3667069154d5SJulian Elischer  */
3668069154d5SJulian Elischer item_p
3669069154d5SJulian Elischer ng_package_msg_self(node_p here, hook_p hook, struct ng_mesg *msg)
3670069154d5SJulian Elischer {
3671069154d5SJulian Elischer 	item_p item;
36724cf49a43SJulian Elischer 
3673859a4d16SJulian Elischer 	/*
3674859a4d16SJulian Elischer 	 * Find the target node.
3675859a4d16SJulian Elischer 	 * If there is a HOOK argument, then use that in preference
3676859a4d16SJulian Elischer 	 * to the address.
3677859a4d16SJulian Elischer 	 */
36786aa6d011SAlexander Motin 	if ((item = ng_alloc_item(NGQF_MESG, NG_NOFLAGS)) == NULL) {
3679069154d5SJulian Elischer 		NG_FREE_MSG(msg);
3680069154d5SJulian Elischer 		return (NULL);
36814cf49a43SJulian Elischer 	}
36824cf49a43SJulian Elischer 
36834cf49a43SJulian Elischer 	/* Fill out the contents */
36846aa6d011SAlexander Motin 	item->el_flags |= NGQF_WRITER;
368530400f03SJulian Elischer 	NG_NODE_REF(here);
36861acb27c6SJulian Elischer 	NGI_SET_NODE(item, here);
36871acb27c6SJulian Elischer 	if (hook) {
368830400f03SJulian Elischer 		NG_HOOK_REF(hook);
36891acb27c6SJulian Elischer 		NGI_SET_HOOK(item, hook);
36901acb27c6SJulian Elischer 	}
3691069154d5SJulian Elischer 	NGI_MSG(item) = msg;
3692069154d5SJulian Elischer 	NGI_RETADDR(item) = ng_node2ID(here);
3693069154d5SJulian Elischer 	return (item);
36944cf49a43SJulian Elischer }
36954cf49a43SJulian Elischer 
3696e088dd4cSAlexander Motin /*
3697e088dd4cSAlexander Motin  * Send ng_item_fn function call to the specified node.
3698e088dd4cSAlexander Motin  */
3699e088dd4cSAlexander Motin 
370042282202SGleb Smirnoff int
3701b332b91fSGleb Smirnoff ng_send_fn(node_p node, hook_p hook, ng_item_fn *fn, void * arg1, int arg2)
3702b332b91fSGleb Smirnoff {
3703b332b91fSGleb Smirnoff 
3704b332b91fSGleb Smirnoff 	return ng_send_fn1(node, hook, fn, arg1, arg2, NG_NOFLAGS);
3705b332b91fSGleb Smirnoff }
3706b332b91fSGleb Smirnoff 
3707b332b91fSGleb Smirnoff int
3708aacdb114SGleb Smirnoff ng_send_fn1(node_p node, hook_p hook, ng_item_fn *fn, void * arg1, int arg2,
370942282202SGleb Smirnoff 	int flags)
37106b795970SJulian Elischer {
37116b795970SJulian Elischer 	item_p item;
37126b795970SJulian Elischer 
37136aa6d011SAlexander Motin 	if ((item = ng_alloc_item(NGQF_FN, flags)) == NULL) {
37146b795970SJulian Elischer 		return (ENOMEM);
37156b795970SJulian Elischer 	}
37166aa6d011SAlexander Motin 	item->el_flags |= NGQF_WRITER;
3717a96dcd84SJulian Elischer 	NG_NODE_REF(node); /* and one for the item */
37181acb27c6SJulian Elischer 	NGI_SET_NODE(item, node);
37191acb27c6SJulian Elischer 	if (hook) {
37206b795970SJulian Elischer 		NG_HOOK_REF(hook);
37211acb27c6SJulian Elischer 		NGI_SET_HOOK(item, hook);
37226b795970SJulian Elischer 	}
37236b795970SJulian Elischer 	NGI_FN(item) = fn;
37246b795970SJulian Elischer 	NGI_ARG1(item) = arg1;
37256b795970SJulian Elischer 	NGI_ARG2(item) = arg2;
372642282202SGleb Smirnoff 	return(ng_snd_item(item, flags));
37276b795970SJulian Elischer }
37286b795970SJulian Elischer 
37294cf49a43SJulian Elischer /*
3730b332b91fSGleb Smirnoff  * Send ng_item_fn2 function call to the specified node.
3731b332b91fSGleb Smirnoff  *
3732b332b91fSGleb Smirnoff  * If an optional pitem parameter is supplied, its apply
3733b332b91fSGleb Smirnoff  * callback will be copied to the new item. If also NG_REUSE_ITEM
3734b332b91fSGleb Smirnoff  * flag is set, no new item will be allocated, but pitem will
3735b332b91fSGleb Smirnoff  * be used.
3736e088dd4cSAlexander Motin  */
3737e088dd4cSAlexander Motin int
3738b332b91fSGleb Smirnoff ng_send_fn2(node_p node, hook_p hook, item_p pitem, ng_item_fn2 *fn, void *arg1,
3739b332b91fSGleb Smirnoff 	int arg2, int flags)
3740e088dd4cSAlexander Motin {
3741e088dd4cSAlexander Motin 	item_p item;
3742e088dd4cSAlexander Motin 
3743b332b91fSGleb Smirnoff 	KASSERT((pitem != NULL || (flags & NG_REUSE_ITEM) == 0),
3744b332b91fSGleb Smirnoff 	    ("%s: NG_REUSE_ITEM but no pitem", __func__));
3745e088dd4cSAlexander Motin 
3746e088dd4cSAlexander Motin 	/*
3747b332b91fSGleb Smirnoff 	 * Allocate a new item if no supplied or
3748b332b91fSGleb Smirnoff 	 * if we can't use supplied one.
3749e088dd4cSAlexander Motin 	 */
3750b332b91fSGleb Smirnoff 	if (pitem == NULL || (flags & NG_REUSE_ITEM) == 0) {
37516aa6d011SAlexander Motin 		if ((item = ng_alloc_item(NGQF_FN2, flags)) == NULL)
3752e088dd4cSAlexander Motin 			return (ENOMEM);
37536aa6d011SAlexander Motin 		if (pitem != NULL)
37546aa6d011SAlexander Motin 			item->apply = pitem->apply;
3755ed75521fSAlexander Motin 	} else {
37566aa6d011SAlexander Motin 		if ((item = ng_realloc_item(pitem, NGQF_FN2, flags)) == NULL)
37576aa6d011SAlexander Motin 			return (ENOMEM);
3758ed75521fSAlexander Motin 	}
3759b332b91fSGleb Smirnoff 
37606aa6d011SAlexander Motin 	item->el_flags = (item->el_flags & ~NGQF_RW) | NGQF_WRITER;
3761e088dd4cSAlexander Motin 	NG_NODE_REF(node); /* and one for the item */
3762e088dd4cSAlexander Motin 	NGI_SET_NODE(item, node);
3763e088dd4cSAlexander Motin 	if (hook) {
3764e088dd4cSAlexander Motin 		NG_HOOK_REF(hook);
3765e088dd4cSAlexander Motin 		NGI_SET_HOOK(item, hook);
3766e088dd4cSAlexander Motin 	}
3767e088dd4cSAlexander Motin 	NGI_FN2(item) = fn;
3768e088dd4cSAlexander Motin 	NGI_ARG1(item) = arg1;
3769e088dd4cSAlexander Motin 	NGI_ARG2(item) = arg2;
3770e088dd4cSAlexander Motin 	return(ng_snd_item(item, flags));
3771e088dd4cSAlexander Motin }
3772e088dd4cSAlexander Motin 
3773e088dd4cSAlexander Motin /*
3774d2ca21a9SJulian Elischer  * Official timeout routines for Netgraph nodes.
3775d2ca21a9SJulian Elischer  */
3776d2ca21a9SJulian Elischer static void
37771fbb36ffSGleb Smirnoff ng_callout_trampoline(void *arg)
3778d2ca21a9SJulian Elischer {
37794c02c20cSGleb Smirnoff 	struct epoch_tracker et;
3780d2ca21a9SJulian Elischer 	item_p item = arg;
3781d2ca21a9SJulian Elischer 
37824c02c20cSGleb Smirnoff 	NET_EPOCH_ENTER(et);
3783bc29160dSMarko Zec 	CURVNET_SET(NGI_NODE(item)->nd_vnet);
3784d2ca21a9SJulian Elischer 	ng_snd_item(item, 0);
3785bc29160dSMarko Zec 	CURVNET_RESTORE();
37864c02c20cSGleb Smirnoff 	NET_EPOCH_EXIT(et);
3787d2ca21a9SJulian Elischer }
3788d2ca21a9SJulian Elischer 
378930bef41bSGleb Smirnoff int
3790f9d9e1b4SGleb Smirnoff ng_callout(struct callout *c, node_p node, hook_p hook, int ticks,
3791d2ca21a9SJulian Elischer     ng_item_fn *fn, void * arg1, int arg2)
3792d2ca21a9SJulian Elischer {
37931bf8e0faSGleb Smirnoff 	item_p item, oitem;
3794d2ca21a9SJulian Elischer 
37956aa6d011SAlexander Motin 	if ((item = ng_alloc_item(NGQF_FN, NG_NOFLAGS)) == NULL)
379630bef41bSGleb Smirnoff 		return (ENOMEM);
379730bef41bSGleb Smirnoff 
37986aa6d011SAlexander Motin 	item->el_flags |= NGQF_WRITER;
3799d2ca21a9SJulian Elischer 	NG_NODE_REF(node);		/* and one for the item */
3800d2ca21a9SJulian Elischer 	NGI_SET_NODE(item, node);
3801d2ca21a9SJulian Elischer 	if (hook) {
3802d2ca21a9SJulian Elischer 		NG_HOOK_REF(hook);
3803d2ca21a9SJulian Elischer 		NGI_SET_HOOK(item, hook);
3804d2ca21a9SJulian Elischer 	}
3805d2ca21a9SJulian Elischer 	NGI_FN(item) = fn;
3806d2ca21a9SJulian Elischer 	NGI_ARG1(item) = arg1;
3807d2ca21a9SJulian Elischer 	NGI_ARG2(item) = arg2;
38081bf8e0faSGleb Smirnoff 	oitem = c->c_arg;
38091bf8e0faSGleb Smirnoff 	if (callout_reset(c, ticks, &ng_callout_trampoline, item) == 1 &&
38101bf8e0faSGleb Smirnoff 	    oitem != NULL)
38111bf8e0faSGleb Smirnoff 		NG_FREE_ITEM(oitem);
381230bef41bSGleb Smirnoff 	return (0);
3813d2ca21a9SJulian Elischer }
3814d2ca21a9SJulian Elischer 
38154b28d96eSJohn Baldwin /* A special modified version of callout_stop() */
3816d2ca21a9SJulian Elischer int
3817f9d9e1b4SGleb Smirnoff ng_uncallout(struct callout *c, node_p node)
3818d2ca21a9SJulian Elischer {
3819d2ca21a9SJulian Elischer 	item_p item;
382030bef41bSGleb Smirnoff 	int rval;
3821d2ca21a9SJulian Elischer 
382203b25f5dSGleb Smirnoff 	KASSERT(c != NULL, ("ng_uncallout: NULL callout"));
382303b25f5dSGleb Smirnoff 	KASSERT(node != NULL, ("ng_uncallout: NULL node"));
382403b25f5dSGleb Smirnoff 
38253eadb26dSGleb Smirnoff 	rval = callout_stop(c);
382630bef41bSGleb Smirnoff 	item = c->c_arg;
382730bef41bSGleb Smirnoff 	/* Do an extra check */
38281fbb36ffSGleb Smirnoff 	if ((rval > 0) && (c->c_func == &ng_callout_trampoline) &&
38292f632dbbSSean Bruno 	    (item != NULL) && (NGI_NODE(item) == node)) {
3830d2ca21a9SJulian Elischer 		/*
3831d2ca21a9SJulian Elischer 		 * We successfully removed it from the queue before it ran
3832d2ca21a9SJulian Elischer 		 * So now we need to unreference everything that was
3833d2ca21a9SJulian Elischer 		 * given extra references. (NG_FREE_ITEM does this).
3834d2ca21a9SJulian Elischer 		 */
3835d2ca21a9SJulian Elischer 		NG_FREE_ITEM(item);
3836d2ca21a9SJulian Elischer 	}
38371bf8e0faSGleb Smirnoff 	c->c_arg = NULL;
383830bef41bSGleb Smirnoff 
38390e493ed3SHans Petter Selasky 	/*
38400e493ed3SHans Petter Selasky 	 * Callers only want to know if the callout was cancelled and
38410e493ed3SHans Petter Selasky 	 * not draining or stopped.
38420e493ed3SHans Petter Selasky 	 */
38430e493ed3SHans Petter Selasky 	return (rval > 0);
3844d2ca21a9SJulian Elischer }
3845d2ca21a9SJulian Elischer 
3846d2ca21a9SJulian Elischer /*
3847069154d5SJulian Elischer  * Set the address, if none given, give the node here.
38484cf49a43SJulian Elischer  */
3849069154d5SJulian Elischer void
3850069154d5SJulian Elischer ng_replace_retaddr(node_p here, item_p item, ng_ID_t retaddr)
38514cf49a43SJulian Elischer {
3852069154d5SJulian Elischer 	if (retaddr) {
3853069154d5SJulian Elischer 		NGI_RETADDR(item) = retaddr;
3854069154d5SJulian Elischer 	} else {
3855069154d5SJulian Elischer 		/*
3856069154d5SJulian Elischer 		 * The old return address should be ok.
3857069154d5SJulian Elischer 		 * If there isn't one, use the address here.
3858069154d5SJulian Elischer 		 */
3859069154d5SJulian Elischer 		NGI_RETADDR(item) = ng_node2ID(here);
3860069154d5SJulian Elischer 	}
3861069154d5SJulian Elischer }
3862