10147868eSNuno Antunes /*-
20147868eSNuno Antunes * Copyright (c) 1996-1999 Whistle Communications, Inc.
30147868eSNuno Antunes * All rights reserved.
40147868eSNuno Antunes *
50147868eSNuno Antunes * Subject to the following obligations and disclaimer of warranty, use and
60147868eSNuno Antunes * redistribution of this software, in source or object code forms, with or
70147868eSNuno Antunes * without modifications are expressly permitted by Whistle Communications;
80147868eSNuno Antunes * provided, however, that:
90147868eSNuno Antunes * 1. Any and all reproductions of the source or object code must include the
100147868eSNuno Antunes * copyright notice above and the following disclaimer of warranties; and
110147868eSNuno Antunes * 2. No rights are granted, in any manner or form, to use Whistle
120147868eSNuno Antunes * Communications, Inc. trademarks, including the mark "WHISTLE
130147868eSNuno Antunes * COMMUNICATIONS" on advertising, endorsements, or otherwise except as
140147868eSNuno Antunes * such appears in the above copyright notice or in the software.
150147868eSNuno Antunes *
160147868eSNuno Antunes * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
170147868eSNuno Antunes * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
180147868eSNuno Antunes * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
190147868eSNuno Antunes * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
200147868eSNuno Antunes * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
210147868eSNuno Antunes * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
220147868eSNuno Antunes * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
230147868eSNuno Antunes * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
240147868eSNuno Antunes * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
250147868eSNuno Antunes * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
260147868eSNuno Antunes * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
270147868eSNuno Antunes * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
280147868eSNuno Antunes * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
290147868eSNuno Antunes * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
300147868eSNuno Antunes * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
310147868eSNuno Antunes * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
320147868eSNuno Antunes * OF SUCH DAMAGE.
330147868eSNuno Antunes *
340147868eSNuno Antunes * Authors: Julian Elischer <julian@freebsd.org>
350147868eSNuno Antunes * Archie Cobbs <archie@freebsd.org>
360147868eSNuno Antunes *
370147868eSNuno Antunes * $FreeBSD: src/sys/netgraph/ng_base.c,v 1.159 2008/04/19 05:30:49 mav Exp $
380147868eSNuno Antunes * $Whistle: ng_base.c,v 1.39 1999/01/28 23:54:53 julian Exp $
390147868eSNuno Antunes */
400147868eSNuno Antunes
410147868eSNuno Antunes /*
420147868eSNuno Antunes * This file implements the base netgraph code.
430147868eSNuno Antunes */
440147868eSNuno Antunes
450147868eSNuno Antunes #include <sys/param.h>
460147868eSNuno Antunes #include <sys/systm.h>
470147868eSNuno Antunes #include <sys/ctype.h>
480147868eSNuno Antunes #include <sys/errno.h>
490560a357SNuno Antunes #include <sys/hash.h>
500147868eSNuno Antunes /*#include <sys/kdb.h>*/
510147868eSNuno Antunes #include <sys/kernel.h>
520147868eSNuno Antunes #include <sys/limits.h>
530147868eSNuno Antunes #include <sys/malloc.h>
540147868eSNuno Antunes #include <sys/mbuf.h>
550147868eSNuno Antunes #include <sys/msgport2.h>
56a6c72860SNuno Antunes #include <sys/mutex2.h>
570560a357SNuno Antunes #include <sys/objcache.h>
580560a357SNuno Antunes #include <sys/proc.h>
590147868eSNuno Antunes #include <sys/queue.h>
600560a357SNuno Antunes #include <sys/refcount.h>
610147868eSNuno Antunes #include <sys/sysctl.h>
620147868eSNuno Antunes #include <sys/syslog.h>
630147868eSNuno Antunes #include <machine/cpu.h>
640147868eSNuno Antunes
650147868eSNuno Antunes #include <netgraph7/netgraph.h>
66ad9efc7bSNuno Antunes #include <netgraph7/netgraph2.h>
670147868eSNuno Antunes #include <netgraph7/ng_parse.h>
680147868eSNuno Antunes
690147868eSNuno Antunes MODULE_VERSION(netgraph, NG_ABI_VERSION);
700147868eSNuno Antunes
71dce7062eSNuno Antunes /* Token to protect topology events. */
72dce7062eSNuno Antunes static struct lwkt_token ng_topo_token;
73dce7062eSNuno Antunes #define TOPOLOGY_RLOCK() lwkt_gettoken_shared(&ng_topo_token)
74dce7062eSNuno Antunes #define TOPOLOGY_RUNLOCK() lwkt_reltoken(&ng_topo_token)
75dce7062eSNuno Antunes #define TOPOLOGY_WLOCK() lwkt_gettoken(&ng_topo_token)
76dce7062eSNuno Antunes #define TOPOLOGY_WUNLOCK() lwkt_reltoken(&ng_topo_token)
77dce7062eSNuno Antunes #define TOPOLOGY_NOTOWNED() KKASSERT(!LWKT_TOKEN_HELD_ANY(&ng_topo_token))
780147868eSNuno Antunes
790147868eSNuno Antunes #ifdef NETGRAPH_DEBUG
800147868eSNuno Antunes static struct mtx ng_nodelist_mtx; /* protects global node/hook lists */
810147868eSNuno Antunes static struct mtx ngq_mtx; /* protects the queue item list */
820147868eSNuno Antunes
830147868eSNuno Antunes static SLIST_HEAD(, ng_node) ng_allnodes;
840147868eSNuno Antunes static LIST_HEAD(, ng_node) ng_freenodes; /* in debug, we never free() them */
850147868eSNuno Antunes static SLIST_HEAD(, ng_hook) ng_allhooks;
860147868eSNuno Antunes static LIST_HEAD(, ng_hook) ng_freehooks; /* in debug, we never free() them */
870147868eSNuno Antunes
880147868eSNuno Antunes static void ng_dumpitems(void);
890147868eSNuno Antunes static void ng_dumpnodes(void);
900147868eSNuno Antunes static void ng_dumphooks(void);
910147868eSNuno Antunes
920147868eSNuno Antunes #endif /* NETGRAPH_DEBUG */
930147868eSNuno Antunes /*
940147868eSNuno Antunes * DEAD versions of the structures.
95dce7062eSNuno Antunes * In order to avoid races, it is sometimes necessary to point
960147868eSNuno Antunes * at SOMETHING even though theoretically, the current entity is
970147868eSNuno Antunes * INVALID. Use these to avoid these races.
980147868eSNuno Antunes */
990147868eSNuno Antunes struct ng_type ng_deadtype = {
1000147868eSNuno Antunes NG_ABI_VERSION,
1010147868eSNuno Antunes "dead",
1020147868eSNuno Antunes NULL, /* modevent */
1030147868eSNuno Antunes NULL, /* constructor */
1040147868eSNuno Antunes NULL, /* rcvmsg */
1050147868eSNuno Antunes NULL, /* shutdown */
1060147868eSNuno Antunes NULL, /* newhook */
1070147868eSNuno Antunes NULL, /* findhook */
1080147868eSNuno Antunes NULL, /* connect */
1090147868eSNuno Antunes NULL, /* rcvdata */
1100147868eSNuno Antunes NULL, /* disconnect */
1110147868eSNuno Antunes NULL, /* cmdlist */
1120147868eSNuno Antunes };
1130147868eSNuno Antunes
1140147868eSNuno Antunes struct ng_node ng_deadnode = {
1150147868eSNuno Antunes "dead",
1160147868eSNuno Antunes &ng_deadtype,
1170147868eSNuno Antunes NGF_INVALID,
1180147868eSNuno Antunes 0, /* numhooks */
1190147868eSNuno Antunes NULL, /* private */
1200147868eSNuno Antunes 0, /* ID */
1210147868eSNuno Antunes LIST_HEAD_INITIALIZER(ng_deadnode.hooks),
1220147868eSNuno Antunes {}, /* all_nodes list entry */
1230147868eSNuno Antunes {}, /* id hashtable list entry */
124ad9efc7bSNuno Antunes {
1250147868eSNuno Antunes 0,
126ad9efc7bSNuno Antunes NULL,
127ad9efc7bSNuno Antunes 0,
128ad9efc7bSNuno Antunes NULL,
129ad9efc7bSNuno Antunes }, /* token */
1300147868eSNuno Antunes 1, /* refs */
1310147868eSNuno Antunes #ifdef NETGRAPH_DEBUG
1320147868eSNuno Antunes ND_MAGIC,
1330147868eSNuno Antunes __FILE__,
1340147868eSNuno Antunes __LINE__,
1350147868eSNuno Antunes {NULL}
1360147868eSNuno Antunes #endif /* NETGRAPH_DEBUG */
1370147868eSNuno Antunes };
1380147868eSNuno Antunes
1390147868eSNuno Antunes struct ng_hook ng_deadhook = {
1400147868eSNuno Antunes "dead",
1410147868eSNuno Antunes NULL, /* private */
1420147868eSNuno Antunes HK_INVALID | HK_DEAD,
1430147868eSNuno Antunes 0, /* undefined data link type */
1440147868eSNuno Antunes &ng_deadhook, /* Peer is self */
1450147868eSNuno Antunes &ng_deadnode, /* attached to deadnode */
1460147868eSNuno Antunes {}, /* hooks list */
1470147868eSNuno Antunes NULL, /* override rcvmsg() */
1480147868eSNuno Antunes NULL, /* override rcvdata() */
1490147868eSNuno Antunes 1, /* refs always >= 1 */
1500147868eSNuno Antunes #ifdef NETGRAPH_DEBUG
1510147868eSNuno Antunes HK_MAGIC,
1520147868eSNuno Antunes __FILE__,
1530147868eSNuno Antunes __LINE__,
1540147868eSNuno Antunes {NULL}
1550147868eSNuno Antunes #endif /* NETGRAPH_DEBUG */
1560147868eSNuno Antunes };
1570147868eSNuno Antunes
1580147868eSNuno Antunes /*
1590147868eSNuno Antunes * END DEAD STRUCTURES
1600147868eSNuno Antunes */
161ad9efc7bSNuno Antunes
162ad9efc7bSNuno Antunes /* We don't want our messages to be replied. This is our panic reply port. */
163ad9efc7bSNuno Antunes static struct lwkt_port ng_panic_reply_port;
164ad9efc7bSNuno Antunes
165ad9efc7bSNuno Antunes /* Array of per-CPU target ports */
166ad9efc7bSNuno Antunes struct lwkt_port *ng_msgport[MAXCPU];
1670147868eSNuno Antunes
1680147868eSNuno Antunes /* List of installed types */
1690147868eSNuno Antunes static LIST_HEAD(, ng_type) ng_typelist;
1707e5f7144SNuno Antunes static struct lwkt_token ng_typelist_token;
1717e5f7144SNuno Antunes #define TYPELIST_RLOCK() lwkt_gettoken_shared(&ng_typelist_token)
1727e5f7144SNuno Antunes #define TYPELIST_RUNLOCK() lwkt_reltoken(&ng_typelist_token)
1737e5f7144SNuno Antunes #define TYPELIST_WLOCK() lwkt_gettoken(&ng_typelist_token)
1747e5f7144SNuno Antunes #define TYPELIST_WUNLOCK() lwkt_reltoken(&ng_typelist_token)
1750147868eSNuno Antunes
1760147868eSNuno Antunes /* Hash related definitions */
1770560a357SNuno Antunes LIST_HEAD(nodehash, ng_node);
1780560a357SNuno Antunes static struct nodehash *ng_ID_hash;
1790560a357SNuno Antunes static u_long ng_ID_hmask;
1800560a357SNuno Antunes static u_long ng_nodes;
1810560a357SNuno Antunes static struct nodehash *ng_name_hash;
1820560a357SNuno Antunes static u_long ng_name_hmask;
1830560a357SNuno Antunes static u_long ng_named_nodes;
1840560a357SNuno Antunes static struct lwkt_token ng_idhash_token;
1850560a357SNuno Antunes #define IDHASH_RLOCK() lwkt_gettoken_shared(&ng_idhash_token)
1860560a357SNuno Antunes #define IDHASH_RUNLOCK() lwkt_reltoken(&ng_idhash_token)
1870560a357SNuno Antunes #define IDHASH_WLOCK() lwkt_gettoken(&ng_idhash_token)
1880560a357SNuno Antunes #define IDHASH_WUNLOCK() lwkt_reltoken(&ng_idhash_token)
1890560a357SNuno Antunes #define IDHASH_ASSERT_LOCKED() KKASSERT(LWKT_TOKEN_HELD_ANY(&ng_idhash_token));
1900147868eSNuno Antunes /* Method to find a node.. used twice so do it here */
1910560a357SNuno Antunes #define NG_IDHASH_FN(ID) ((ID) % (ng_ID_hmask + 1))
1920147868eSNuno Antunes #define NG_IDHASH_FIND(ID, node) \
1930147868eSNuno Antunes do { \
1940560a357SNuno Antunes IDHASH_ASSERT_LOCKED(); \
1950147868eSNuno Antunes LIST_FOREACH(node, &ng_ID_hash[NG_IDHASH_FN(ID)], \
1960147868eSNuno Antunes nd_idnodes) { \
1970147868eSNuno Antunes if (NG_NODE_IS_VALID(node) \
1980147868eSNuno Antunes && (NG_NODE_ID(node) == ID)) { \
1990147868eSNuno Antunes break; \
2000147868eSNuno Antunes } \
2010147868eSNuno Antunes } \
2020147868eSNuno Antunes } while (0)
2030147868eSNuno Antunes
2040560a357SNuno Antunes static struct lwkt_token ng_namehash_token;
2050560a357SNuno Antunes #define NAMEHASH_RLOCK() lwkt_gettoken_shared(&ng_namehash_token)
2060560a357SNuno Antunes #define NAMEHASH_RUNLOCK() lwkt_reltoken(&ng_namehash_token)
2070560a357SNuno Antunes #define NAMEHASH_WLOCK() lwkt_gettoken(&ng_namehash_token)
2080560a357SNuno Antunes #define NAMEHASH_WUNLOCK() lwkt_reltoken(&ng_namehash_token)
2090560a357SNuno Antunes #define NAMEHASH_ASSERT_LOCKED() KKASSERT(LWKT_TOKEN_HELD_ANY(&ng_namehash_token));
2100147868eSNuno Antunes
2110147868eSNuno Antunes /* Internal functions */
2120147868eSNuno Antunes static int ng_add_hook(node_p node, const char *name, hook_p * hookp);
2130147868eSNuno Antunes static int ng_generic_msg(node_p here, item_p item, hook_p lasthook);
2140147868eSNuno Antunes static ng_ID_t ng_decodeidname(const char *name);
2150147868eSNuno Antunes static int ngb_mod_event(module_t mod, int event, void *data);
216ad9efc7bSNuno Antunes static void ngthread(void *dummy);
217ad9efc7bSNuno Antunes static int ng_apply_item(item_p item);
2180147868eSNuno Antunes static node_p ng_ID2noderef(ng_ID_t ID);
2190147868eSNuno Antunes static int ng_con_nodes(item_p item, node_p node, const char *name,
2200147868eSNuno Antunes node_p node2, const char *name2);
2210147868eSNuno Antunes static int ng_con_part2(node_p node, item_p item, hook_p hook);
2220147868eSNuno Antunes static int ng_con_part3(node_p node, item_p item, hook_p hook);
2230560a357SNuno Antunes static int ng_mkpeer(node_p node, const char *name, const char *name2,
2240560a357SNuno Antunes char *type);
2250560a357SNuno Antunes static void ng_name_rehash(void);
2260560a357SNuno Antunes static void ng_ID_rehash(void);
2270147868eSNuno Antunes static boolean_t bzero_ctor(void *obj, void *private, int ocflags);
2280147868eSNuno Antunes
2290147868eSNuno Antunes /* Imported, these used to be externally visible, some may go back. */
2300147868eSNuno Antunes void ng_destroy_hook(hook_p hook);
2310147868eSNuno Antunes node_p ng_name2noderef(node_p node, const char *name);
2320147868eSNuno Antunes int ng_path2noderef(node_p here, const char *path,
2330147868eSNuno Antunes node_p *dest, hook_p *lasthook);
2340147868eSNuno Antunes int ng_make_node(const char *type, node_p *nodepp);
2350147868eSNuno Antunes int ng_path_parse(char *addr, char **node, char **path, char **hook);
2360147868eSNuno Antunes void ng_rmnode(node_p node, hook_p dummy1, void *dummy2, int dummy3);
2370147868eSNuno Antunes void ng_unname(node_p node);
2380147868eSNuno Antunes
2390147868eSNuno Antunes
2400147868eSNuno Antunes /* Our own netgraph malloc type */
2410147868eSNuno Antunes MALLOC_DEFINE(M_NETGRAPH, "netgraph", "netgraph structures and ctrl messages");
2420147868eSNuno Antunes MALLOC_DEFINE(M_NETGRAPH_HOOK, "netgraph_hook", "netgraph hook structures");
2430147868eSNuno Antunes MALLOC_DEFINE(M_NETGRAPH_NODE, "netgraph_node", "netgraph node structures");
2440147868eSNuno Antunes MALLOC_DEFINE(M_NETGRAPH_MSG, "netgraph_msg", "netgraph name storage");
245ad9efc7bSNuno Antunes MALLOC_DEFINE(M_NETGRAPH_APPLY, "netgraph_apply", "netgraph apply_info structures");
2460147868eSNuno Antunes
2470147868eSNuno Antunes /* Should not be visible outside this file */
2480147868eSNuno Antunes
2490147868eSNuno Antunes #define _NG_ALLOC_HOOK(hook) \
2500147868eSNuno Antunes hook = kmalloc(sizeof(*hook), M_NETGRAPH_HOOK, \
2510147868eSNuno Antunes M_WAITOK | M_NULLOK | M_ZERO)
2520147868eSNuno Antunes #define _NG_ALLOC_NODE(node) \
2530147868eSNuno Antunes node = kmalloc(sizeof(*node), M_NETGRAPH_NODE, \
2540147868eSNuno Antunes M_WAITOK | M_NULLOK | M_ZERO)
2550147868eSNuno Antunes
2560147868eSNuno Antunes #ifdef NETGRAPH_DEBUG /*----------------------------------------------*/
2570147868eSNuno Antunes /*
2580147868eSNuno Antunes * In debug mode:
2590147868eSNuno Antunes * In an attempt to help track reference count screwups
2600147868eSNuno Antunes * we do not free objects back to the malloc system, but keep them
2610147868eSNuno Antunes * in a local cache where we can examine them and keep information safely
2620147868eSNuno Antunes * after they have been freed.
2630147868eSNuno Antunes * We use this scheme for nodes and hooks, and to some extent for items.
2640147868eSNuno Antunes */
2650147868eSNuno Antunes static __inline hook_p
ng_alloc_hook(void)2660147868eSNuno Antunes ng_alloc_hook(void)
2670147868eSNuno Antunes {
2680147868eSNuno Antunes hook_p hook;
2690147868eSNuno Antunes SLIST_ENTRY(ng_hook) temp;
2700147868eSNuno Antunes mtx_lock(&ng_nodelist_mtx);
2710147868eSNuno Antunes hook = LIST_FIRST(&ng_freehooks);
2720147868eSNuno Antunes if (hook) {
2730147868eSNuno Antunes LIST_REMOVE(hook, hk_hooks);
2740147868eSNuno Antunes bcopy(&hook->hk_all, &temp, sizeof(temp));
2750147868eSNuno Antunes bzero(hook, sizeof(struct ng_hook));
2760147868eSNuno Antunes bcopy(&temp, &hook->hk_all, sizeof(temp));
2770147868eSNuno Antunes mtx_unlock(&ng_nodelist_mtx);
2780147868eSNuno Antunes hook->hk_magic = HK_MAGIC;
2790147868eSNuno Antunes } else {
2800147868eSNuno Antunes mtx_unlock(&ng_nodelist_mtx);
2810147868eSNuno Antunes _NG_ALLOC_HOOK(hook);
2820147868eSNuno Antunes if (hook) {
2830147868eSNuno Antunes hook->hk_magic = HK_MAGIC;
2840147868eSNuno Antunes mtx_lock(&ng_nodelist_mtx);
2850147868eSNuno Antunes SLIST_INSERT_HEAD(&ng_allhooks, hook, hk_all);
2860147868eSNuno Antunes mtx_unlock(&ng_nodelist_mtx);
2870147868eSNuno Antunes }
2880147868eSNuno Antunes }
2890147868eSNuno Antunes return (hook);
2900147868eSNuno Antunes }
2910147868eSNuno Antunes
2920147868eSNuno Antunes static __inline node_p
ng_alloc_node(void)2930147868eSNuno Antunes ng_alloc_node(void)
2940147868eSNuno Antunes {
2950147868eSNuno Antunes node_p node;
2960147868eSNuno Antunes SLIST_ENTRY(ng_node) temp;
2970147868eSNuno Antunes mtx_lock(&ng_nodelist_mtx);
2980147868eSNuno Antunes node = LIST_FIRST(&ng_freenodes);
2990147868eSNuno Antunes if (node) {
3000147868eSNuno Antunes LIST_REMOVE(node, nd_nodes);
3010147868eSNuno Antunes bcopy(&node->nd_all, &temp, sizeof(temp));
3020147868eSNuno Antunes bzero(node, sizeof(struct ng_node));
3030147868eSNuno Antunes bcopy(&temp, &node->nd_all, sizeof(temp));
3040147868eSNuno Antunes mtx_unlock(&ng_nodelist_mtx);
3050147868eSNuno Antunes node->nd_magic = ND_MAGIC;
3060147868eSNuno Antunes } else {
3070147868eSNuno Antunes mtx_unlock(&ng_nodelist_mtx);
3080147868eSNuno Antunes _NG_ALLOC_NODE(node);
3090147868eSNuno Antunes if (node) {
3100147868eSNuno Antunes node->nd_magic = ND_MAGIC;
3110147868eSNuno Antunes mtx_lock(&ng_nodelist_mtx);
3120147868eSNuno Antunes SLIST_INSERT_HEAD(&ng_allnodes, node, nd_all);
3130147868eSNuno Antunes mtx_unlock(&ng_nodelist_mtx);
3140147868eSNuno Antunes }
3150147868eSNuno Antunes }
3160147868eSNuno Antunes return (node);
3170147868eSNuno Antunes }
3180147868eSNuno Antunes
3190147868eSNuno Antunes #define NG_ALLOC_HOOK(hook) do { (hook) = ng_alloc_hook(); } while (0)
3200147868eSNuno Antunes #define NG_ALLOC_NODE(node) do { (node) = ng_alloc_node(); } while (0)
3210147868eSNuno Antunes
3220147868eSNuno Antunes
3230147868eSNuno Antunes #define NG_FREE_HOOK(hook) \
3240147868eSNuno Antunes do { \
3250147868eSNuno Antunes mtx_lock(&ng_nodelist_mtx); \
3260147868eSNuno Antunes LIST_INSERT_HEAD(&ng_freehooks, hook, hk_hooks); \
3270147868eSNuno Antunes hook->hk_magic = 0; \
3280147868eSNuno Antunes mtx_unlock(&ng_nodelist_mtx); \
3290147868eSNuno Antunes } while (0)
3300147868eSNuno Antunes
3310147868eSNuno Antunes #define NG_FREE_NODE(node) \
3320147868eSNuno Antunes do { \
3330147868eSNuno Antunes mtx_lock(&ng_nodelist_mtx); \
3340147868eSNuno Antunes LIST_INSERT_HEAD(&ng_freenodes, node, nd_nodes); \
3350147868eSNuno Antunes node->nd_magic = 0; \
3360147868eSNuno Antunes mtx_unlock(&ng_nodelist_mtx); \
3370147868eSNuno Antunes } while (0)
3380147868eSNuno Antunes
3390147868eSNuno Antunes #else /* NETGRAPH_DEBUG */ /*----------------------------------------------*/
3400147868eSNuno Antunes
3410147868eSNuno Antunes #define NG_ALLOC_HOOK(hook) _NG_ALLOC_HOOK(hook)
3420147868eSNuno Antunes #define NG_ALLOC_NODE(node) _NG_ALLOC_NODE(node)
3430147868eSNuno Antunes
3440147868eSNuno Antunes #define NG_FREE_HOOK(hook) do { kfree((hook), M_NETGRAPH_HOOK); } while (0)
3450147868eSNuno Antunes #define NG_FREE_NODE(node) do { kfree((node), M_NETGRAPH_NODE); } while (0)
3460147868eSNuno Antunes
3470147868eSNuno Antunes #endif /* NETGRAPH_DEBUG */ /*----------------------------------------------*/
3480147868eSNuno Antunes
3490147868eSNuno Antunes /* Set this to kdb_enter("X") to catch all errors as they occur */
3500147868eSNuno Antunes #ifndef TRAP_ERROR
3510147868eSNuno Antunes #define TRAP_ERROR()
3520147868eSNuno Antunes #endif
3530147868eSNuno Antunes
3540147868eSNuno Antunes static ng_ID_t nextID = 1;
3550147868eSNuno Antunes
3560147868eSNuno Antunes #ifdef INVARIANTS
3570147868eSNuno Antunes #define CHECK_DATA_MBUF(m) do { \
3580147868eSNuno Antunes struct mbuf *n; \
3590147868eSNuno Antunes int total; \
3600147868eSNuno Antunes \
3610147868eSNuno Antunes M_ASSERTPKTHDR(m); \
3620147868eSNuno Antunes for (total = 0, n = (m); n != NULL; n = n->m_next) { \
3630147868eSNuno Antunes total += n->m_len; \
3640147868eSNuno Antunes if (n->m_nextpkt != NULL) \
3650147868eSNuno Antunes panic("%s: m_nextpkt", __func__); \
3660147868eSNuno Antunes } \
3670147868eSNuno Antunes \
3680147868eSNuno Antunes if ((m)->m_pkthdr.len != total) { \
3690147868eSNuno Antunes panic("%s: %d != %d", \
3700147868eSNuno Antunes __func__, (m)->m_pkthdr.len, total); \
3710147868eSNuno Antunes } \
3720147868eSNuno Antunes } while (0)
3730147868eSNuno Antunes #else
3740147868eSNuno Antunes #define CHECK_DATA_MBUF(m)
3750147868eSNuno Antunes #endif
3760147868eSNuno Antunes
3770147868eSNuno Antunes #define ERROUT(x) do { error = (x); goto done; } while (0)
3780147868eSNuno Antunes
3790147868eSNuno Antunes /************************************************************************
3800147868eSNuno Antunes Parse type definitions for generic messages
3810147868eSNuno Antunes ************************************************************************/
3820147868eSNuno Antunes
3830147868eSNuno Antunes /* Handy structure parse type defining macro */
3840147868eSNuno Antunes #define DEFINE_PARSE_STRUCT_TYPE(lo, up, args) \
3850147868eSNuno Antunes static const struct ng_parse_struct_field \
3860147868eSNuno Antunes ng_ ## lo ## _type_fields[] = NG_GENERIC_ ## up ## _INFO args; \
3870147868eSNuno Antunes static const struct ng_parse_type ng_generic_ ## lo ## _type = { \
3880147868eSNuno Antunes &ng_parse_struct_type, \
3890147868eSNuno Antunes &ng_ ## lo ## _type_fields \
3900147868eSNuno Antunes }
3910147868eSNuno Antunes
3920147868eSNuno Antunes DEFINE_PARSE_STRUCT_TYPE(mkpeer, MKPEER, ());
3930147868eSNuno Antunes DEFINE_PARSE_STRUCT_TYPE(connect, CONNECT, ());
3940147868eSNuno Antunes DEFINE_PARSE_STRUCT_TYPE(name, NAME, ());
3950147868eSNuno Antunes DEFINE_PARSE_STRUCT_TYPE(rmhook, RMHOOK, ());
3960147868eSNuno Antunes DEFINE_PARSE_STRUCT_TYPE(nodeinfo, NODEINFO, ());
3970147868eSNuno Antunes DEFINE_PARSE_STRUCT_TYPE(typeinfo, TYPEINFO, ());
3980147868eSNuno Antunes DEFINE_PARSE_STRUCT_TYPE(linkinfo, LINKINFO, (&ng_generic_nodeinfo_type));
3990147868eSNuno Antunes
4000147868eSNuno Antunes /* Get length of an array when the length is stored as a 32 bit
4010147868eSNuno Antunes value immediately preceding the array -- as with struct namelist
4020147868eSNuno Antunes and struct typelist. */
4030147868eSNuno Antunes static int
ng_generic_list_getLength(const struct ng_parse_type * type,const u_char * start,const u_char * buf)4040147868eSNuno Antunes ng_generic_list_getLength(const struct ng_parse_type *type,
4050147868eSNuno Antunes const u_char *start, const u_char *buf)
4060147868eSNuno Antunes {
4070147868eSNuno Antunes return *((const u_int32_t *)(buf - 4));
4080147868eSNuno Antunes }
4090147868eSNuno Antunes
4100147868eSNuno Antunes /* Get length of the array of struct linkinfo inside a struct hooklist */
4110147868eSNuno Antunes static int
ng_generic_linkinfo_getLength(const struct ng_parse_type * type,const u_char * start,const u_char * buf)4120147868eSNuno Antunes ng_generic_linkinfo_getLength(const struct ng_parse_type *type,
4130147868eSNuno Antunes const u_char *start, const u_char *buf)
4140147868eSNuno Antunes {
4150147868eSNuno Antunes const struct hooklist *hl = (const struct hooklist *)start;
4160147868eSNuno Antunes
4170147868eSNuno Antunes return hl->nodeinfo.hooks;
4180147868eSNuno Antunes }
4190147868eSNuno Antunes
4200147868eSNuno Antunes /* Array type for a variable length array of struct namelist */
4210147868eSNuno Antunes static const struct ng_parse_array_info ng_nodeinfoarray_type_info = {
4220147868eSNuno Antunes &ng_generic_nodeinfo_type,
4230147868eSNuno Antunes &ng_generic_list_getLength
4240147868eSNuno Antunes };
4250147868eSNuno Antunes static const struct ng_parse_type ng_generic_nodeinfoarray_type = {
4260147868eSNuno Antunes &ng_parse_array_type,
4270147868eSNuno Antunes &ng_nodeinfoarray_type_info
4280147868eSNuno Antunes };
4290147868eSNuno Antunes
430e535ebfaSzrj #if 0 /* unused */
4310147868eSNuno Antunes /* Array type for a variable length array of struct typelist */
4320147868eSNuno Antunes static const struct ng_parse_array_info ng_typeinfoarray_type_info = {
4330147868eSNuno Antunes &ng_generic_typeinfo_type,
4340147868eSNuno Antunes &ng_generic_list_getLength
4350147868eSNuno Antunes };
4360147868eSNuno Antunes static const struct ng_parse_type ng_generic_typeinfoarray_type = {
4370147868eSNuno Antunes &ng_parse_array_type,
4380147868eSNuno Antunes &ng_typeinfoarray_type_info
4390147868eSNuno Antunes };
440e535ebfaSzrj #endif
4410147868eSNuno Antunes
4420147868eSNuno Antunes /* Array type for array of struct linkinfo in struct hooklist */
4430147868eSNuno Antunes static const struct ng_parse_array_info ng_generic_linkinfo_array_type_info = {
4440147868eSNuno Antunes &ng_generic_linkinfo_type,
4450147868eSNuno Antunes &ng_generic_linkinfo_getLength
4460147868eSNuno Antunes };
4470147868eSNuno Antunes static const struct ng_parse_type ng_generic_linkinfo_array_type = {
4480147868eSNuno Antunes &ng_parse_array_type,
4490147868eSNuno Antunes &ng_generic_linkinfo_array_type_info
4500147868eSNuno Antunes };
4510147868eSNuno Antunes
452e535ebfaSzrj #if 0 /* unused */
4537e5f7144SNuno Antunes DEFINE_PARSE_STRUCT_TYPE(typelist, TYPELIST, (&ng_generic_typeinfoarray_type));
454e535ebfaSzrj #endif
4550147868eSNuno Antunes DEFINE_PARSE_STRUCT_TYPE(hooklist, HOOKLIST,
4560147868eSNuno Antunes (&ng_generic_nodeinfo_type, &ng_generic_linkinfo_array_type));
4570147868eSNuno Antunes DEFINE_PARSE_STRUCT_TYPE(listnodes, LISTNODES,
4580147868eSNuno Antunes (&ng_generic_nodeinfoarray_type));
4590147868eSNuno Antunes
4600147868eSNuno Antunes /* List of commands and how to convert arguments to/from ASCII */
4610147868eSNuno Antunes static const struct ng_cmdlist ng_generic_cmds[] = {
4620147868eSNuno Antunes {
4630147868eSNuno Antunes NGM_GENERIC_COOKIE,
4640147868eSNuno Antunes NGM_SHUTDOWN,
4650147868eSNuno Antunes "shutdown",
4660147868eSNuno Antunes NULL,
4670147868eSNuno Antunes NULL
4680147868eSNuno Antunes },
4690147868eSNuno Antunes {
4700147868eSNuno Antunes NGM_GENERIC_COOKIE,
4710147868eSNuno Antunes NGM_MKPEER,
4720147868eSNuno Antunes "mkpeer",
4730147868eSNuno Antunes &ng_generic_mkpeer_type,
4740147868eSNuno Antunes NULL
4750147868eSNuno Antunes },
4760147868eSNuno Antunes {
4770147868eSNuno Antunes NGM_GENERIC_COOKIE,
4780147868eSNuno Antunes NGM_CONNECT,
4790147868eSNuno Antunes "connect",
4800147868eSNuno Antunes &ng_generic_connect_type,
4810147868eSNuno Antunes NULL
4820147868eSNuno Antunes },
4830147868eSNuno Antunes {
4840147868eSNuno Antunes NGM_GENERIC_COOKIE,
4850147868eSNuno Antunes NGM_NAME,
4860147868eSNuno Antunes "name",
4870147868eSNuno Antunes &ng_generic_name_type,
4880147868eSNuno Antunes NULL
4890147868eSNuno Antunes },
4900147868eSNuno Antunes {
4910147868eSNuno Antunes NGM_GENERIC_COOKIE,
4920147868eSNuno Antunes NGM_RMHOOK,
4930147868eSNuno Antunes "rmhook",
4940147868eSNuno Antunes &ng_generic_rmhook_type,
4950147868eSNuno Antunes NULL
4960147868eSNuno Antunes },
4970147868eSNuno Antunes {
4980147868eSNuno Antunes NGM_GENERIC_COOKIE,
4990147868eSNuno Antunes NGM_NODEINFO,
5000147868eSNuno Antunes "nodeinfo",
5010147868eSNuno Antunes NULL,
5020147868eSNuno Antunes &ng_generic_nodeinfo_type
5030147868eSNuno Antunes },
5040147868eSNuno Antunes {
5050147868eSNuno Antunes NGM_GENERIC_COOKIE,
5060147868eSNuno Antunes NGM_LISTHOOKS,
5070147868eSNuno Antunes "listhooks",
5080147868eSNuno Antunes NULL,
5090147868eSNuno Antunes &ng_generic_hooklist_type
5100147868eSNuno Antunes },
5110147868eSNuno Antunes {
5120147868eSNuno Antunes NGM_GENERIC_COOKIE,
5130147868eSNuno Antunes NGM_LISTNAMES,
5140147868eSNuno Antunes "listnames",
5150147868eSNuno Antunes NULL,
5160147868eSNuno Antunes &ng_generic_listnodes_type /* same as NGM_LISTNODES */
5170147868eSNuno Antunes },
5180147868eSNuno Antunes {
5190147868eSNuno Antunes NGM_GENERIC_COOKIE,
5200147868eSNuno Antunes NGM_LISTNODES,
5210147868eSNuno Antunes "listnodes",
5220147868eSNuno Antunes NULL,
5230147868eSNuno Antunes &ng_generic_listnodes_type
5240147868eSNuno Antunes },
5250147868eSNuno Antunes {
5260147868eSNuno Antunes NGM_GENERIC_COOKIE,
5270147868eSNuno Antunes NGM_LISTTYPES,
5280147868eSNuno Antunes "listtypes",
5290147868eSNuno Antunes NULL,
5300147868eSNuno Antunes &ng_generic_typeinfo_type
5310147868eSNuno Antunes },
5320147868eSNuno Antunes {
5330147868eSNuno Antunes NGM_GENERIC_COOKIE,
5340147868eSNuno Antunes NGM_TEXT_CONFIG,
5350147868eSNuno Antunes "textconfig",
5360147868eSNuno Antunes NULL,
5370147868eSNuno Antunes &ng_parse_string_type
5380147868eSNuno Antunes },
5390147868eSNuno Antunes {
5400147868eSNuno Antunes NGM_GENERIC_COOKIE,
5410147868eSNuno Antunes NGM_TEXT_STATUS,
5420147868eSNuno Antunes "textstatus",
5430147868eSNuno Antunes NULL,
5440147868eSNuno Antunes &ng_parse_string_type
5450147868eSNuno Antunes },
5460147868eSNuno Antunes {
5470147868eSNuno Antunes NGM_GENERIC_COOKIE,
5480147868eSNuno Antunes NGM_ASCII2BINARY,
5490147868eSNuno Antunes "ascii2binary",
5500147868eSNuno Antunes &ng_parse_ng_mesg_type,
5510147868eSNuno Antunes &ng_parse_ng_mesg_type
5520147868eSNuno Antunes },
5530147868eSNuno Antunes {
5540147868eSNuno Antunes NGM_GENERIC_COOKIE,
5550147868eSNuno Antunes NGM_BINARY2ASCII,
5560147868eSNuno Antunes "binary2ascii",
5570147868eSNuno Antunes &ng_parse_ng_mesg_type,
5580147868eSNuno Antunes &ng_parse_ng_mesg_type
5590147868eSNuno Antunes },
5600147868eSNuno Antunes { 0 }
5610147868eSNuno Antunes };
5620147868eSNuno Antunes
5630147868eSNuno Antunes /************************************************************************
5640147868eSNuno Antunes Node routines
5650147868eSNuno Antunes ************************************************************************/
5660147868eSNuno Antunes
5670147868eSNuno Antunes /*
5680147868eSNuno Antunes * Instantiate a node of the requested type
5690147868eSNuno Antunes */
5700147868eSNuno Antunes int
ng_make_node(const char * typename,node_p * nodepp)5710147868eSNuno Antunes ng_make_node(const char *typename, node_p *nodepp)
5720147868eSNuno Antunes {
5730147868eSNuno Antunes struct ng_type *type;
5740147868eSNuno Antunes int error;
5750147868eSNuno Antunes
5760147868eSNuno Antunes /* Check that the type makes sense */
5770147868eSNuno Antunes if (typename == NULL) {
5780147868eSNuno Antunes TRAP_ERROR();
5790147868eSNuno Antunes return (EINVAL);
5800147868eSNuno Antunes }
5810147868eSNuno Antunes
5820147868eSNuno Antunes /* Locate the node type. If we fail we return. Do not try to load
5830147868eSNuno Antunes * module.
5840147868eSNuno Antunes */
5850147868eSNuno Antunes if ((type = ng_findtype(typename)) == NULL)
5860147868eSNuno Antunes return (ENXIO);
5870147868eSNuno Antunes
5880147868eSNuno Antunes /*
5890147868eSNuno Antunes * If we have a constructor, then make the node and
5900147868eSNuno Antunes * call the constructor to do type specific initialisation.
5910147868eSNuno Antunes */
5920147868eSNuno Antunes if (type->constructor != NULL) {
5930147868eSNuno Antunes if ((error = ng_make_node_common(type, nodepp)) == 0) {
594ca34a086SMatthew Dillon if ((error = ((*type->constructor)(*nodepp))) != 0) {
5950147868eSNuno Antunes NG_NODE_UNREF(*nodepp);
5960147868eSNuno Antunes }
5970147868eSNuno Antunes }
5980147868eSNuno Antunes } else {
5990147868eSNuno Antunes /*
6000147868eSNuno Antunes * Node has no constructor. We cannot ask for one
6010147868eSNuno Antunes * to be made. It must be brought into existence by
6020147868eSNuno Antunes * some external agency. The external agency should
6030147868eSNuno Antunes * call ng_make_node_common() directly to get the
6040147868eSNuno Antunes * netgraph part initialised.
6050147868eSNuno Antunes */
6060147868eSNuno Antunes TRAP_ERROR();
6070147868eSNuno Antunes error = EINVAL;
6080147868eSNuno Antunes }
6090147868eSNuno Antunes return (error);
6100147868eSNuno Antunes }
6110147868eSNuno Antunes
6120147868eSNuno Antunes /*
6130147868eSNuno Antunes * Generic node creation. Called by node initialisation for externally
6140147868eSNuno Antunes * instantiated nodes (e.g. hardware, sockets, etc ).
6150147868eSNuno Antunes * The returned node has a reference count of 1.
6160147868eSNuno Antunes */
6170147868eSNuno Antunes int
ng_make_node_common(struct ng_type * type,node_p * nodepp)6180147868eSNuno Antunes ng_make_node_common(struct ng_type *type, node_p *nodepp)
6190147868eSNuno Antunes {
6200147868eSNuno Antunes node_p node;
6210147868eSNuno Antunes
6220147868eSNuno Antunes /* Require the node type to have been already installed */
6230147868eSNuno Antunes if (ng_findtype(type->name) == NULL) {
6240147868eSNuno Antunes TRAP_ERROR();
6250147868eSNuno Antunes return (EINVAL);
6260147868eSNuno Antunes }
6270147868eSNuno Antunes
6280147868eSNuno Antunes /* Make a node and try attach it to the type */
6290147868eSNuno Antunes NG_ALLOC_NODE(node);
6300147868eSNuno Antunes if (node == NULL) {
6310147868eSNuno Antunes TRAP_ERROR();
6320147868eSNuno Antunes return (ENOMEM);
6330147868eSNuno Antunes }
6340147868eSNuno Antunes node->nd_type = type;
6350147868eSNuno Antunes NG_NODE_REF(node); /* note reference */
6360147868eSNuno Antunes type->refs++;
6370147868eSNuno Antunes
638ad9efc7bSNuno Antunes /* Initialize the reader/writer shared token */
639ad9efc7bSNuno Antunes lwkt_token_init(&node->nd_token, type->name);
6400147868eSNuno Antunes
6410147868eSNuno Antunes /* Initialize hook list for new node */
6420147868eSNuno Antunes LIST_INIT(&node->nd_hooks);
6430147868eSNuno Antunes
6440560a357SNuno Antunes /* Get an ID and put us in the hash chain. */
6450560a357SNuno Antunes IDHASH_WLOCK();
6460147868eSNuno Antunes for (;;) { /* wrap protection, even if silly */
6470147868eSNuno Antunes node_p node2 = NULL;
6480147868eSNuno Antunes node->nd_ID = nextID++; /* 137/second for 1 year before wrap */
6490147868eSNuno Antunes
6500147868eSNuno Antunes /* Is there a problem with the new number? */
6510147868eSNuno Antunes NG_IDHASH_FIND(node->nd_ID, node2); /* already taken? */
6520147868eSNuno Antunes if ((node->nd_ID != 0) && (node2 == NULL)) {
6530147868eSNuno Antunes break;
6540147868eSNuno Antunes }
6550147868eSNuno Antunes }
6560560a357SNuno Antunes ng_nodes++;
6570560a357SNuno Antunes if (ng_nodes * 2 > ng_ID_hmask)
6580560a357SNuno Antunes ng_ID_rehash();
6590560a357SNuno Antunes LIST_INSERT_HEAD(&ng_ID_hash[NG_IDHASH_FN(node->nd_ID)], node,
6600560a357SNuno Antunes nd_idnodes);
6610560a357SNuno Antunes IDHASH_WUNLOCK();
6620147868eSNuno Antunes
6630147868eSNuno Antunes /* Done */
6640147868eSNuno Antunes *nodepp = node;
6650147868eSNuno Antunes return (0);
6660147868eSNuno Antunes }
6670147868eSNuno Antunes
6680147868eSNuno Antunes /*
6690147868eSNuno Antunes * Forceably start the shutdown process on a node. Either call
6700147868eSNuno Antunes * its shutdown method, or do the default shutdown if there is
6710147868eSNuno Antunes * no type-specific method.
6720147868eSNuno Antunes *
6730147868eSNuno Antunes * We can only be called from a shutdown message, so we know we have
674ad9efc7bSNuno Antunes * a writer lock, and therefore exclusive access.
6750147868eSNuno Antunes *
6760147868eSNuno Antunes * Persistent node types must have a type-specific method which
6770147868eSNuno Antunes * allocates a new node in which case, this one is irretrievably going away,
6780147868eSNuno Antunes * or cleans up anything it needs, and just makes the node valid again,
6790147868eSNuno Antunes * in which case we allow the node to survive.
6800147868eSNuno Antunes *
6810147868eSNuno Antunes * XXX We need to think of how to tell a persistent node that we
6820147868eSNuno Antunes * REALLY need to go away because the hardware has gone or we
6830147868eSNuno Antunes * are rebooting.... etc.
6840147868eSNuno Antunes */
6850147868eSNuno Antunes void
ng_rmnode(node_p node,hook_p dummy1,void * dummy2,int dummy3)6860147868eSNuno Antunes ng_rmnode(node_p node, hook_p dummy1, void *dummy2, int dummy3)
6870147868eSNuno Antunes {
6880147868eSNuno Antunes hook_p hook;
6890147868eSNuno Antunes
6900147868eSNuno Antunes /* Check if it's already shutting down */
6910147868eSNuno Antunes if ((node->nd_flags & NGF_CLOSING) != 0)
6920147868eSNuno Antunes return;
6930147868eSNuno Antunes
6940147868eSNuno Antunes if (node == &ng_deadnode) {
695a62226e4SSascha Wildner kprintf ("shutdown called on deadnode\n");
6960147868eSNuno Antunes return;
6970147868eSNuno Antunes }
6980147868eSNuno Antunes
6990147868eSNuno Antunes /* Add an extra reference so it doesn't go away during this */
7000147868eSNuno Antunes NG_NODE_REF(node);
7010147868eSNuno Antunes
7020147868eSNuno Antunes /*
7030147868eSNuno Antunes * Mark it invalid so any newcomers know not to try use it
7040147868eSNuno Antunes * Also add our own mark so we can't recurse
7050147868eSNuno Antunes * note that NGF_INVALID does not do this as it's also set during
7060147868eSNuno Antunes * creation
7070147868eSNuno Antunes */
7080147868eSNuno Antunes node->nd_flags |= NGF_INVALID|NGF_CLOSING;
7090147868eSNuno Antunes
7100147868eSNuno Antunes /* If node has its pre-shutdown method, then call it first*/
7110147868eSNuno Antunes if (node->nd_type && node->nd_type->close)
7120147868eSNuno Antunes (*node->nd_type->close)(node);
7130147868eSNuno Antunes
7140147868eSNuno Antunes /* Notify all remaining connected nodes to disconnect */
7150147868eSNuno Antunes while ((hook = LIST_FIRST(&node->nd_hooks)) != NULL)
7160147868eSNuno Antunes ng_destroy_hook(hook);
7170147868eSNuno Antunes
7180147868eSNuno Antunes /* Ask the type if it has anything to do in this case */
7190147868eSNuno Antunes if (node->nd_type && node->nd_type->shutdown) {
7200147868eSNuno Antunes (*node->nd_type->shutdown)(node);
7210147868eSNuno Antunes if (NG_NODE_IS_VALID(node)) {
7220147868eSNuno Antunes /*
7230147868eSNuno Antunes * Well, blow me down if the node code hasn't declared
7240147868eSNuno Antunes * that it doesn't want to die.
7250147868eSNuno Antunes * Presumably it is a persistant node.
7260147868eSNuno Antunes * If we REALLY want it to go away,
7270147868eSNuno Antunes * e.g. hardware going away,
7280147868eSNuno Antunes * Our caller should set NGF_REALLY_DIE in nd_flags.
7290147868eSNuno Antunes */
7300147868eSNuno Antunes node->nd_flags &= ~(NGF_INVALID|NGF_CLOSING);
7310147868eSNuno Antunes NG_NODE_UNREF(node); /* Assume they still have theirs */
7320147868eSNuno Antunes return;
7330147868eSNuno Antunes }
7340147868eSNuno Antunes } else { /* do the default thing */
7350147868eSNuno Antunes NG_NODE_UNREF(node);
7360147868eSNuno Antunes }
7370147868eSNuno Antunes
7380147868eSNuno Antunes ng_unname(node); /* basically a NOP these days */
7390147868eSNuno Antunes
7400147868eSNuno Antunes /*
7410147868eSNuno Antunes * Remove extra reference, possibly the last
7420147868eSNuno Antunes * Possible other holders of references may include
7430147868eSNuno Antunes * timeout callouts, but theoretically the node's supposed to
7440147868eSNuno Antunes * have cancelled them. Possibly hardware dependencies may
7450147868eSNuno Antunes * force a driver to 'linger' with a reference.
7460147868eSNuno Antunes */
7470147868eSNuno Antunes NG_NODE_UNREF(node);
7480147868eSNuno Antunes }
7490147868eSNuno Antunes
7500147868eSNuno Antunes /*
7510147868eSNuno Antunes * Remove a reference to the node, possibly the last.
7520147868eSNuno Antunes * deadnode always acts as it it were the last.
7530147868eSNuno Antunes */
7540560a357SNuno Antunes void
ng_unref_node(node_p node)7550147868eSNuno Antunes ng_unref_node(node_p node)
7560147868eSNuno Antunes {
7570147868eSNuno Antunes
7580560a357SNuno Antunes if (node == &ng_deadnode)
7590560a357SNuno Antunes return;
7600147868eSNuno Antunes
7610560a357SNuno Antunes if (refcount_release(&node->nd_refs)) { /* we were the last */
7620147868eSNuno Antunes
7630147868eSNuno Antunes node->nd_type->refs--; /* XXX maybe should get types lock? */
7640560a357SNuno Antunes NAMEHASH_WLOCK();
7650560a357SNuno Antunes if (NG_NODE_HAS_NAME(node)) {
7660560a357SNuno Antunes ng_named_nodes--;
7670147868eSNuno Antunes LIST_REMOVE(node, nd_nodes);
7680560a357SNuno Antunes }
7690560a357SNuno Antunes NAMEHASH_WUNLOCK();
7700147868eSNuno Antunes
7710560a357SNuno Antunes IDHASH_WLOCK();
7720560a357SNuno Antunes ng_nodes--;
7730147868eSNuno Antunes LIST_REMOVE(node, nd_idnodes);
7740560a357SNuno Antunes IDHASH_WUNLOCK();
7750147868eSNuno Antunes
7760147868eSNuno Antunes NG_FREE_NODE(node);
7770147868eSNuno Antunes }
7780147868eSNuno Antunes }
7790147868eSNuno Antunes
7800147868eSNuno Antunes /************************************************************************
7810147868eSNuno Antunes Node ID handling
7820147868eSNuno Antunes ************************************************************************/
7830147868eSNuno Antunes static node_p
ng_ID2noderef(ng_ID_t ID)7840147868eSNuno Antunes ng_ID2noderef(ng_ID_t ID)
7850147868eSNuno Antunes {
7860147868eSNuno Antunes node_p node;
7870560a357SNuno Antunes
7880560a357SNuno Antunes IDHASH_RLOCK();
7890147868eSNuno Antunes NG_IDHASH_FIND(ID, node);
7900147868eSNuno Antunes if (node)
7910147868eSNuno Antunes NG_NODE_REF(node);
7920560a357SNuno Antunes IDHASH_RUNLOCK();
7930147868eSNuno Antunes return(node);
7940147868eSNuno Antunes }
7950147868eSNuno Antunes
7960147868eSNuno Antunes ng_ID_t
ng_node2ID(node_p node)7970147868eSNuno Antunes ng_node2ID(node_p node)
7980147868eSNuno Antunes {
7990147868eSNuno Antunes return (node ? NG_NODE_ID(node) : 0);
8000147868eSNuno Antunes }
8010147868eSNuno Antunes
8020147868eSNuno Antunes /************************************************************************
8030147868eSNuno Antunes Node name handling
8040147868eSNuno Antunes ************************************************************************/
8050147868eSNuno Antunes
8060147868eSNuno Antunes /*
8070560a357SNuno Antunes * Assign a node a name.
8080147868eSNuno Antunes */
8090147868eSNuno Antunes int
ng_name_node(node_p node,const char * name)8100147868eSNuno Antunes ng_name_node(node_p node, const char *name)
8110147868eSNuno Antunes {
8120560a357SNuno Antunes uint32_t hash;
8130147868eSNuno Antunes node_p node2;
8140560a357SNuno Antunes int i;
8150147868eSNuno Antunes
8160147868eSNuno Antunes /* Check the name is valid */
8170147868eSNuno Antunes for (i = 0; i < NG_NODESIZ; i++) {
8180147868eSNuno Antunes if (name[i] == '\0' || name[i] == '.' || name[i] == ':')
8190147868eSNuno Antunes break;
8200147868eSNuno Antunes }
8210147868eSNuno Antunes if (i == 0 || name[i] != '\0') {
8220147868eSNuno Antunes TRAP_ERROR();
8230147868eSNuno Antunes return (EINVAL);
8240147868eSNuno Antunes }
8250147868eSNuno Antunes if (ng_decodeidname(name) != 0) { /* valid IDs not allowed here */
8260147868eSNuno Antunes TRAP_ERROR();
8270147868eSNuno Antunes return (EINVAL);
8280147868eSNuno Antunes }
8290147868eSNuno Antunes
8300560a357SNuno Antunes NAMEHASH_WLOCK();
8310560a357SNuno Antunes if (ng_named_nodes * 2 > ng_name_hmask)
8320560a357SNuno Antunes ng_name_rehash();
8330560a357SNuno Antunes
8340560a357SNuno Antunes hash = hash32_str(name, HASHINIT) & ng_name_hmask;
8350560a357SNuno Antunes /* Check the name isn't already being used. */
8360560a357SNuno Antunes LIST_FOREACH(node2, &ng_name_hash[hash], nd_nodes)
8370560a357SNuno Antunes if (NG_NODE_IS_VALID(node2) &&
8380560a357SNuno Antunes (strcmp(NG_NODE_NAME(node2), name) == 0)) {
8390560a357SNuno Antunes NAMEHASH_WUNLOCK();
8400147868eSNuno Antunes return (EADDRINUSE);
8410147868eSNuno Antunes }
8420147868eSNuno Antunes
8430560a357SNuno Antunes if (NG_NODE_HAS_NAME(node))
8440560a357SNuno Antunes LIST_REMOVE(node, nd_nodes);
8450560a357SNuno Antunes else
8460560a357SNuno Antunes ng_named_nodes++;
8470560a357SNuno Antunes /* Copy it. */
8480147868eSNuno Antunes strlcpy(NG_NODE_NAME(node), name, NG_NODESIZ);
8490147868eSNuno Antunes
8500147868eSNuno Antunes /* Update name hash. */
8510147868eSNuno Antunes LIST_INSERT_HEAD(&ng_name_hash[hash], node, nd_nodes);
8520560a357SNuno Antunes NAMEHASH_WUNLOCK();
8530147868eSNuno Antunes
8540147868eSNuno Antunes return (0);
8550147868eSNuno Antunes }
8560147868eSNuno Antunes
8570147868eSNuno Antunes /*
8580147868eSNuno Antunes * Find a node by absolute name. The name should NOT end with ':'
8590147868eSNuno Antunes * The name "." means "this node" and "[xxx]" means "the node
8600147868eSNuno Antunes * with ID (ie, at address) xxx".
8610147868eSNuno Antunes *
8620147868eSNuno Antunes * Returns the node if found, else NULL.
8630147868eSNuno Antunes * Eventually should add something faster than a sequential search.
8640147868eSNuno Antunes * Note it acquires a reference on the node so you can be sure it's still
8650147868eSNuno Antunes * there.
8660147868eSNuno Antunes */
8670147868eSNuno Antunes node_p
ng_name2noderef(node_p here,const char * name)8680147868eSNuno Antunes ng_name2noderef(node_p here, const char *name)
8690147868eSNuno Antunes {
8700147868eSNuno Antunes node_p node;
8710147868eSNuno Antunes ng_ID_t temp;
8720147868eSNuno Antunes int hash;
8730147868eSNuno Antunes
8740147868eSNuno Antunes /* "." means "this node" */
8750147868eSNuno Antunes if (strcmp(name, ".") == 0) {
8760147868eSNuno Antunes NG_NODE_REF(here);
8770147868eSNuno Antunes return(here);
8780147868eSNuno Antunes }
8790147868eSNuno Antunes
8800147868eSNuno Antunes /* Check for name-by-ID */
8810147868eSNuno Antunes if ((temp = ng_decodeidname(name)) != 0) {
8820147868eSNuno Antunes return (ng_ID2noderef(temp));
8830147868eSNuno Antunes }
8840147868eSNuno Antunes
8850560a357SNuno Antunes /* Find node by name. */
8860560a357SNuno Antunes hash = hash32_str(name, HASHINIT) & ng_name_hmask;
8870560a357SNuno Antunes NAMEHASH_RLOCK();
8880560a357SNuno Antunes LIST_FOREACH(node, &ng_name_hash[hash], nd_nodes)
8890147868eSNuno Antunes if (NG_NODE_IS_VALID(node) &&
8900147868eSNuno Antunes (strcmp(NG_NODE_NAME(node), name) == 0)) {
8910560a357SNuno Antunes NG_NODE_REF(node);
8920147868eSNuno Antunes break;
8930147868eSNuno Antunes }
8940560a357SNuno Antunes NAMEHASH_RUNLOCK();
8950560a357SNuno Antunes
8960147868eSNuno Antunes return (node);
8970147868eSNuno Antunes }
8980147868eSNuno Antunes
8990147868eSNuno Antunes /*
9000147868eSNuno Antunes * Decode an ID name, eg. "[f03034de]". Returns 0 if the
9010147868eSNuno Antunes * string is not valid, otherwise returns the value.
9020147868eSNuno Antunes */
9030147868eSNuno Antunes static ng_ID_t
ng_decodeidname(const char * name)9040147868eSNuno Antunes ng_decodeidname(const char *name)
9050147868eSNuno Antunes {
9060147868eSNuno Antunes const int len = strlen(name);
9070147868eSNuno Antunes char *eptr;
9080147868eSNuno Antunes u_long val;
9090147868eSNuno Antunes
9100147868eSNuno Antunes /* Check for proper length, brackets, no leading junk */
9110147868eSNuno Antunes if ((len < 3)
9120147868eSNuno Antunes || (name[0] != '[')
9130147868eSNuno Antunes || (name[len - 1] != ']')
9140147868eSNuno Antunes || (!isxdigit(name[1]))) {
9150147868eSNuno Antunes return ((ng_ID_t)0);
9160147868eSNuno Antunes }
9170147868eSNuno Antunes
9180147868eSNuno Antunes /* Decode number */
9190147868eSNuno Antunes val = strtoul(name + 1, &eptr, 16);
9200147868eSNuno Antunes if ((eptr - name != len - 1)
9210147868eSNuno Antunes || (val == ULONG_MAX)
9220147868eSNuno Antunes || (val == 0)) {
9230147868eSNuno Antunes return ((ng_ID_t)0);
9240147868eSNuno Antunes }
9250147868eSNuno Antunes return (ng_ID_t)val;
9260147868eSNuno Antunes }
9270147868eSNuno Antunes
9280147868eSNuno Antunes /*
9290147868eSNuno Antunes * Remove a name from a node. This should only be called
9300147868eSNuno Antunes * when shutting down and removing the node.
9310147868eSNuno Antunes */
9320147868eSNuno Antunes void
ng_unname(node_p node)9330147868eSNuno Antunes ng_unname(node_p node)
9340147868eSNuno Antunes {
9350147868eSNuno Antunes }
9360147868eSNuno Antunes
9370560a357SNuno Antunes /*
9380560a357SNuno Antunes * Allocate a bigger name hash.
9390560a357SNuno Antunes */
9400560a357SNuno Antunes static void
ng_name_rehash(void)9410560a357SNuno Antunes ng_name_rehash(void)
9420560a357SNuno Antunes {
9430560a357SNuno Antunes struct nodehash *new;
9440560a357SNuno Antunes uint32_t hash;
9450560a357SNuno Antunes u_long hmask;
9460560a357SNuno Antunes node_p node, node2;
9470560a357SNuno Antunes int i;
9480560a357SNuno Antunes
9490560a357SNuno Antunes NAMEHASH_ASSERT_LOCKED();
9500560a357SNuno Antunes new = hashinit((ng_name_hmask + 1) * 2, M_NETGRAPH_NODE, &hmask);
9510560a357SNuno Antunes if (new == NULL)
9520560a357SNuno Antunes return;
9530560a357SNuno Antunes
9540560a357SNuno Antunes for (i = 0; i <= ng_name_hmask; i++) {
9550560a357SNuno Antunes LIST_FOREACH_MUTABLE(node, &ng_name_hash[i], nd_nodes, node2) {
9560560a357SNuno Antunes #ifdef INVARIANTS
9570560a357SNuno Antunes LIST_REMOVE(node, nd_nodes);
9580560a357SNuno Antunes #endif
9590560a357SNuno Antunes hash = hash32_str(NG_NODE_NAME(node), HASHINIT) & hmask;
9600560a357SNuno Antunes LIST_INSERT_HEAD(&new[hash], node, nd_nodes);
9610560a357SNuno Antunes }
9620560a357SNuno Antunes }
9630560a357SNuno Antunes
9640560a357SNuno Antunes hashdestroy(ng_name_hash, M_NETGRAPH_NODE, ng_name_hmask);
9650560a357SNuno Antunes ng_name_hash = new;
9660560a357SNuno Antunes ng_name_hmask = hmask;
9670560a357SNuno Antunes }
9680560a357SNuno Antunes
9690560a357SNuno Antunes /*
9700560a357SNuno Antunes * Allocate a bigger ID hash.
9710560a357SNuno Antunes */
9720560a357SNuno Antunes static void
ng_ID_rehash(void)9730560a357SNuno Antunes ng_ID_rehash(void)
9740560a357SNuno Antunes {
9750560a357SNuno Antunes struct nodehash *new;
9760560a357SNuno Antunes uint32_t hash;
9770560a357SNuno Antunes u_long hmask;
9780560a357SNuno Antunes node_p node, node2;
9790560a357SNuno Antunes int i;
9800560a357SNuno Antunes
9810560a357SNuno Antunes IDHASH_ASSERT_LOCKED();
9820560a357SNuno Antunes new = hashinit((ng_ID_hmask + 1) * 2, M_NETGRAPH_NODE, &hmask);
9830560a357SNuno Antunes if (new == NULL)
9840560a357SNuno Antunes return;
9850560a357SNuno Antunes
9860560a357SNuno Antunes for (i = 0; i <= ng_ID_hmask; i++) {
9870560a357SNuno Antunes LIST_FOREACH_MUTABLE(node, &ng_ID_hash[i], nd_idnodes, node2) {
9880560a357SNuno Antunes #ifdef INVARIANTS
9890560a357SNuno Antunes LIST_REMOVE(node, nd_idnodes);
9900560a357SNuno Antunes #endif
9910560a357SNuno Antunes hash = (node->nd_ID % (hmask + 1));
9920560a357SNuno Antunes LIST_INSERT_HEAD(&new[hash], node, nd_idnodes);
9930560a357SNuno Antunes }
9940560a357SNuno Antunes }
9950560a357SNuno Antunes
9960560a357SNuno Antunes hashdestroy(ng_ID_hash, M_NETGRAPH_NODE, ng_name_hmask);
9970560a357SNuno Antunes ng_ID_hash = new;
9980560a357SNuno Antunes ng_ID_hmask = hmask;
9990560a357SNuno Antunes }
10000560a357SNuno Antunes
10010147868eSNuno Antunes /************************************************************************
10020147868eSNuno Antunes Hook routines
10030147868eSNuno Antunes Names are not optional. Hooks are always connected, except for a
10040147868eSNuno Antunes brief moment within these routines. On invalidation or during creation
10050147868eSNuno Antunes they are connected to the 'dead' hook.
10060147868eSNuno Antunes ************************************************************************/
10070147868eSNuno Antunes
10080147868eSNuno Antunes /*
10090147868eSNuno Antunes * Remove a hook reference
10100147868eSNuno Antunes */
10110147868eSNuno Antunes void
ng_unref_hook(hook_p hook)10120147868eSNuno Antunes ng_unref_hook(hook_p hook)
10130147868eSNuno Antunes {
10140147868eSNuno Antunes int v;
10150147868eSNuno Antunes
10160147868eSNuno Antunes if (hook == &ng_deadhook) {
10170147868eSNuno Antunes return;
10180147868eSNuno Antunes }
10190147868eSNuno Antunes
10200147868eSNuno Antunes v = atomic_fetchadd_int(&hook->hk_refs, -1);
10210147868eSNuno Antunes
10220147868eSNuno Antunes if (v == 1) { /* we were the last */
10230147868eSNuno Antunes if (_NG_HOOK_NODE(hook)) /* it'll probably be ng_deadnode */
10240147868eSNuno Antunes _NG_NODE_UNREF((_NG_HOOK_NODE(hook)));
10250147868eSNuno Antunes NG_FREE_HOOK(hook);
10260147868eSNuno Antunes }
10270147868eSNuno Antunes }
10280147868eSNuno Antunes
10290147868eSNuno Antunes /*
10300147868eSNuno Antunes * Add an unconnected hook to a node. Only used internally.
10310147868eSNuno Antunes * Assumes node is locked. (XXX not yet true )
10320147868eSNuno Antunes */
10330147868eSNuno Antunes static int
ng_add_hook(node_p node,const char * name,hook_p * hookp)10340147868eSNuno Antunes ng_add_hook(node_p node, const char *name, hook_p *hookp)
10350147868eSNuno Antunes {
10360147868eSNuno Antunes hook_p hook;
10370147868eSNuno Antunes int error = 0;
10380147868eSNuno Antunes
10390147868eSNuno Antunes /* Check that the given name is good */
10400147868eSNuno Antunes if (name == NULL) {
10410147868eSNuno Antunes TRAP_ERROR();
10420147868eSNuno Antunes return (EINVAL);
10430147868eSNuno Antunes }
10440147868eSNuno Antunes if (ng_findhook(node, name) != NULL) {
10450147868eSNuno Antunes TRAP_ERROR();
10460147868eSNuno Antunes return (EEXIST);
10470147868eSNuno Antunes }
10480147868eSNuno Antunes
10490147868eSNuno Antunes /* Allocate the hook and link it up */
10500147868eSNuno Antunes NG_ALLOC_HOOK(hook);
10510147868eSNuno Antunes if (hook == NULL) {
10520147868eSNuno Antunes TRAP_ERROR();
10530147868eSNuno Antunes return (ENOMEM);
10540147868eSNuno Antunes }
10550147868eSNuno Antunes hook->hk_refs = 1; /* add a reference for us to return */
10560147868eSNuno Antunes hook->hk_flags = HK_INVALID;
10570147868eSNuno Antunes hook->hk_peer = &ng_deadhook; /* start off this way */
10580147868eSNuno Antunes hook->hk_node = node;
10590147868eSNuno Antunes NG_NODE_REF(node); /* each hook counts as a reference */
10600147868eSNuno Antunes
10610147868eSNuno Antunes /* Set hook name */
10620147868eSNuno Antunes strlcpy(NG_HOOK_NAME(hook), name, NG_HOOKSIZ);
10630147868eSNuno Antunes
10640147868eSNuno Antunes /*
10650147868eSNuno Antunes * Check if the node type code has something to say about it
10660147868eSNuno Antunes * If it fails, the unref of the hook will also unref the node.
10670147868eSNuno Antunes */
10680147868eSNuno Antunes if (node->nd_type->newhook != NULL) {
10690147868eSNuno Antunes if ((error = (*node->nd_type->newhook)(node, hook, name))) {
10700147868eSNuno Antunes NG_HOOK_UNREF(hook); /* this frees the hook */
10710147868eSNuno Antunes return (error);
10720147868eSNuno Antunes }
10730147868eSNuno Antunes }
10740147868eSNuno Antunes /*
10750147868eSNuno Antunes * The 'type' agrees so far, so go ahead and link it in.
10760147868eSNuno Antunes * We'll ask again later when we actually connect the hooks.
10770147868eSNuno Antunes */
10780147868eSNuno Antunes LIST_INSERT_HEAD(&node->nd_hooks, hook, hk_hooks);
10790147868eSNuno Antunes node->nd_numhooks++;
10800147868eSNuno Antunes NG_HOOK_REF(hook); /* one for the node */
10810147868eSNuno Antunes
10820147868eSNuno Antunes if (hookp)
10830147868eSNuno Antunes *hookp = hook;
10840147868eSNuno Antunes return (0);
10850147868eSNuno Antunes }
10860147868eSNuno Antunes
10870147868eSNuno Antunes /*
10880147868eSNuno Antunes * Find a hook
10890147868eSNuno Antunes *
10900147868eSNuno Antunes * Node types may supply their own optimized routines for finding
10910147868eSNuno Antunes * hooks. If none is supplied, we just do a linear search.
10920147868eSNuno Antunes * XXX Possibly we should add a reference to the hook?
10930147868eSNuno Antunes */
10940147868eSNuno Antunes hook_p
ng_findhook(node_p node,const char * name)10950147868eSNuno Antunes ng_findhook(node_p node, const char *name)
10960147868eSNuno Antunes {
10970147868eSNuno Antunes hook_p hook;
10980147868eSNuno Antunes
10990147868eSNuno Antunes if (node->nd_type->findhook != NULL)
11000147868eSNuno Antunes return (*node->nd_type->findhook)(node, name);
11010147868eSNuno Antunes LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) {
11020147868eSNuno Antunes if (NG_HOOK_IS_VALID(hook)
11030147868eSNuno Antunes && (strcmp(NG_HOOK_NAME(hook), name) == 0))
11040147868eSNuno Antunes return (hook);
11050147868eSNuno Antunes }
11060147868eSNuno Antunes return (NULL);
11070147868eSNuno Antunes }
11080147868eSNuno Antunes
11090147868eSNuno Antunes /*
11100147868eSNuno Antunes * Destroy a hook
11110147868eSNuno Antunes *
11120147868eSNuno Antunes * As hooks are always attached, this really destroys two hooks.
11130147868eSNuno Antunes * The one given, and the one attached to it. Disconnect the hooks
11140147868eSNuno Antunes * from each other first. We reconnect the peer hook to the 'dead'
11150147868eSNuno Antunes * hook so that it can still exist after we depart. We then
11160147868eSNuno Antunes * send the peer its own destroy message. This ensures that we only
11170147868eSNuno Antunes * interact with the peer's structures when it is locked processing that
11180147868eSNuno Antunes * message. We hold a reference to the peer hook so we are guaranteed that
11190147868eSNuno Antunes * the peer hook and node are still going to exist until
11200147868eSNuno Antunes * we are finished there as the hook holds a ref on the node.
11210147868eSNuno Antunes * We run this same code again on the peer hook, but that time it is already
11220147868eSNuno Antunes * attached to the 'dead' hook.
11230147868eSNuno Antunes *
11240147868eSNuno Antunes * This routine is called at all stages of hook creation
11250147868eSNuno Antunes * on error detection and must be able to handle any such stage.
11260147868eSNuno Antunes */
11270147868eSNuno Antunes void
ng_destroy_hook(hook_p hook)11280147868eSNuno Antunes ng_destroy_hook(hook_p hook)
11290147868eSNuno Antunes {
11300147868eSNuno Antunes hook_p peer;
11310147868eSNuno Antunes node_p node;
11320147868eSNuno Antunes
11330147868eSNuno Antunes if (hook == &ng_deadhook) { /* better safe than sorry */
1134a62226e4SSascha Wildner kprintf("ng_destroy_hook called on deadhook\n");
11350147868eSNuno Antunes return;
11360147868eSNuno Antunes }
11370147868eSNuno Antunes
11380147868eSNuno Antunes /*
11390147868eSNuno Antunes * Protect divorce process with mutex, to avoid races on
11400147868eSNuno Antunes * simultaneous disconnect.
11410147868eSNuno Antunes */
1142dce7062eSNuno Antunes TOPOLOGY_WLOCK();
11430147868eSNuno Antunes
11440147868eSNuno Antunes hook->hk_flags |= HK_INVALID;
11450147868eSNuno Antunes
11460147868eSNuno Antunes peer = NG_HOOK_PEER(hook);
11470147868eSNuno Antunes node = NG_HOOK_NODE(hook);
11480147868eSNuno Antunes
11490147868eSNuno Antunes if (peer && (peer != &ng_deadhook)) {
11500147868eSNuno Antunes /*
11510147868eSNuno Antunes * Set the peer to point to ng_deadhook
11520147868eSNuno Antunes * from this moment on we are effectively independent it.
11530147868eSNuno Antunes * send it an rmhook message of it's own.
11540147868eSNuno Antunes */
11550147868eSNuno Antunes peer->hk_peer = &ng_deadhook; /* They no longer know us */
11560147868eSNuno Antunes hook->hk_peer = &ng_deadhook; /* Nor us, them */
11570147868eSNuno Antunes if (NG_HOOK_NODE(peer) == &ng_deadnode) {
11580147868eSNuno Antunes /*
11590147868eSNuno Antunes * If it's already divorced from a node,
11600147868eSNuno Antunes * just free it.
11610147868eSNuno Antunes */
1162dce7062eSNuno Antunes TOPOLOGY_WUNLOCK();
11630147868eSNuno Antunes } else {
1164dce7062eSNuno Antunes TOPOLOGY_WUNLOCK();
11650147868eSNuno Antunes ng_rmhook_self(peer); /* Send it a surprise */
11660147868eSNuno Antunes }
11670147868eSNuno Antunes NG_HOOK_UNREF(peer); /* account for peer link */
11680147868eSNuno Antunes NG_HOOK_UNREF(hook); /* account for peer link */
11690147868eSNuno Antunes } else
1170dce7062eSNuno Antunes TOPOLOGY_WUNLOCK();
11710147868eSNuno Antunes
1172dce7062eSNuno Antunes TOPOLOGY_NOTOWNED();
11730147868eSNuno Antunes
11740147868eSNuno Antunes /*
11750147868eSNuno Antunes * Remove the hook from the node's list to avoid possible recursion
11760147868eSNuno Antunes * in case the disconnection results in node shutdown.
11770147868eSNuno Antunes */
11780147868eSNuno Antunes if (node == &ng_deadnode) { /* happens if called from ng_con_nodes() */
11790147868eSNuno Antunes return;
11800147868eSNuno Antunes }
11810147868eSNuno Antunes LIST_REMOVE(hook, hk_hooks);
11820147868eSNuno Antunes node->nd_numhooks--;
11830147868eSNuno Antunes if (node->nd_type->disconnect) {
11840147868eSNuno Antunes /*
11850147868eSNuno Antunes * The type handler may elect to destroy the node so don't
11860147868eSNuno Antunes * trust its existence after this point. (except
11870147868eSNuno Antunes * that we still hold a reference on it. (which we
11880147868eSNuno Antunes * inherrited from the hook we are destroying)
11890147868eSNuno Antunes */
11900147868eSNuno Antunes (*node->nd_type->disconnect) (hook);
11910147868eSNuno Antunes }
11920147868eSNuno Antunes
11930147868eSNuno Antunes /*
11940147868eSNuno Antunes * Note that because we will point to ng_deadnode, the original node
11950147868eSNuno Antunes * is not decremented automatically so we do that manually.
11960147868eSNuno Antunes */
11970147868eSNuno Antunes _NG_HOOK_NODE(hook) = &ng_deadnode;
11980147868eSNuno Antunes NG_NODE_UNREF(node); /* We no longer point to it so adjust count */
11990147868eSNuno Antunes NG_HOOK_UNREF(hook); /* Account for linkage (in list) to node */
12000147868eSNuno Antunes }
12010147868eSNuno Antunes
12020147868eSNuno Antunes /*
12030147868eSNuno Antunes * Take two hooks on a node and merge the connection so that the given node
12040147868eSNuno Antunes * is effectively bypassed.
12050147868eSNuno Antunes */
12060147868eSNuno Antunes int
ng_bypass(hook_p hook1,hook_p hook2)12070147868eSNuno Antunes ng_bypass(hook_p hook1, hook_p hook2)
12080147868eSNuno Antunes {
12090147868eSNuno Antunes if (hook1->hk_node != hook2->hk_node) {
12100147868eSNuno Antunes TRAP_ERROR();
12110147868eSNuno Antunes return (EINVAL);
12120147868eSNuno Antunes }
12130147868eSNuno Antunes hook1->hk_peer->hk_peer = hook2->hk_peer;
12140147868eSNuno Antunes hook2->hk_peer->hk_peer = hook1->hk_peer;
12150147868eSNuno Antunes
12160147868eSNuno Antunes hook1->hk_peer = &ng_deadhook;
12170147868eSNuno Antunes hook2->hk_peer = &ng_deadhook;
12180147868eSNuno Antunes
12190147868eSNuno Antunes NG_HOOK_UNREF(hook1);
12200147868eSNuno Antunes NG_HOOK_UNREF(hook2);
12210147868eSNuno Antunes
12220147868eSNuno Antunes /* XXX If we ever cache methods on hooks update them as well */
12230147868eSNuno Antunes ng_destroy_hook(hook1);
12240147868eSNuno Antunes ng_destroy_hook(hook2);
12250147868eSNuno Antunes return (0);
12260147868eSNuno Antunes }
12270147868eSNuno Antunes
12280147868eSNuno Antunes /*
12290147868eSNuno Antunes * Install a new netgraph type
12300147868eSNuno Antunes */
12310147868eSNuno Antunes int
ng_newtype(struct ng_type * tp)12320147868eSNuno Antunes ng_newtype(struct ng_type *tp)
12330147868eSNuno Antunes {
12340147868eSNuno Antunes const size_t namelen = strlen(tp->name);
12350147868eSNuno Antunes
12360147868eSNuno Antunes /* Check version and type name fields */
12370147868eSNuno Antunes if ((tp->version != NG_ABI_VERSION)
12380147868eSNuno Antunes || (namelen == 0)
12390147868eSNuno Antunes || (namelen >= NG_TYPESIZ)) {
12400147868eSNuno Antunes TRAP_ERROR();
12410147868eSNuno Antunes if (tp->version != NG_ABI_VERSION) {
1242a62226e4SSascha Wildner kprintf("Netgraph: Node type rejected. ABI mismatch. Suggest recompile\n");
12430147868eSNuno Antunes }
12440147868eSNuno Antunes return (EINVAL);
12450147868eSNuno Antunes }
12460147868eSNuno Antunes
12470147868eSNuno Antunes /* Check for name collision */
12480147868eSNuno Antunes if (ng_findtype(tp->name) != NULL) {
12490147868eSNuno Antunes TRAP_ERROR();
12500147868eSNuno Antunes return (EEXIST);
12510147868eSNuno Antunes }
12520147868eSNuno Antunes
12530147868eSNuno Antunes /* Link in new type */
12547e5f7144SNuno Antunes TYPELIST_WLOCK();
12550147868eSNuno Antunes LIST_INSERT_HEAD(&ng_typelist, tp, types);
12560147868eSNuno Antunes tp->refs = 1; /* first ref is linked list */
12577e5f7144SNuno Antunes TYPELIST_WUNLOCK();
12580147868eSNuno Antunes return (0);
12590147868eSNuno Antunes }
12600147868eSNuno Antunes
12610147868eSNuno Antunes /*
12620147868eSNuno Antunes * unlink a netgraph type
12630147868eSNuno Antunes * If no examples exist
12640147868eSNuno Antunes */
12650147868eSNuno Antunes int
ng_rmtype(struct ng_type * tp)12660147868eSNuno Antunes ng_rmtype(struct ng_type *tp)
12670147868eSNuno Antunes {
12680147868eSNuno Antunes /* Check for name collision */
12690147868eSNuno Antunes if (tp->refs != 1) {
12700147868eSNuno Antunes TRAP_ERROR();
12710147868eSNuno Antunes return (EBUSY);
12720147868eSNuno Antunes }
12730147868eSNuno Antunes
12740147868eSNuno Antunes /* Unlink type */
12757e5f7144SNuno Antunes TYPELIST_WLOCK();
12760147868eSNuno Antunes LIST_REMOVE(tp, types);
12777e5f7144SNuno Antunes TYPELIST_WUNLOCK();
12780147868eSNuno Antunes return (0);
12790147868eSNuno Antunes }
12800147868eSNuno Antunes
12810147868eSNuno Antunes /*
12820147868eSNuno Antunes * Look for a type of the name given
12830147868eSNuno Antunes */
12840147868eSNuno Antunes struct ng_type *
ng_findtype(const char * typename)12850147868eSNuno Antunes ng_findtype(const char *typename)
12860147868eSNuno Antunes {
12870147868eSNuno Antunes struct ng_type *type;
12880147868eSNuno Antunes
12897e5f7144SNuno Antunes TYPELIST_RLOCK();
12900147868eSNuno Antunes LIST_FOREACH(type, &ng_typelist, types) {
12910147868eSNuno Antunes if (strcmp(type->name, typename) == 0)
12920147868eSNuno Antunes break;
12930147868eSNuno Antunes }
12947e5f7144SNuno Antunes TYPELIST_RUNLOCK();
12950147868eSNuno Antunes return (type);
12960147868eSNuno Antunes }
12970147868eSNuno Antunes
12980147868eSNuno Antunes /************************************************************************
12990147868eSNuno Antunes Composite routines
13000147868eSNuno Antunes ************************************************************************/
13010147868eSNuno Antunes /*
13020147868eSNuno Antunes * Connect two nodes using the specified hooks, using queued functions.
13030147868eSNuno Antunes */
13040147868eSNuno Antunes static int
ng_con_part3(node_p node,item_p item,hook_p hook)13050147868eSNuno Antunes ng_con_part3(node_p node, item_p item, hook_p hook)
13060147868eSNuno Antunes {
13070147868eSNuno Antunes int error = 0;
13080147868eSNuno Antunes
13090147868eSNuno Antunes /*
13100147868eSNuno Antunes * When we run, we know that the node 'node' is locked for us.
13110147868eSNuno Antunes * Our caller has a reference on the hook.
13120147868eSNuno Antunes * Our caller has a reference on the node.
13130147868eSNuno Antunes * (In this case our caller is ng_apply_item() ).
13140147868eSNuno Antunes * The peer hook has a reference on the hook.
13150147868eSNuno Antunes * We are all set up except for the final call to the node, and
13160147868eSNuno Antunes * the clearing of the INVALID flag.
13170147868eSNuno Antunes */
13180147868eSNuno Antunes if (NG_HOOK_NODE(hook) == &ng_deadnode) {
13190147868eSNuno Antunes /*
13200147868eSNuno Antunes * The node must have been freed again since we last visited
13210147868eSNuno Antunes * here. ng_destry_hook() has this effect but nothing else does.
13220147868eSNuno Antunes * We should just release our references and
13230147868eSNuno Antunes * free anything we can think of.
13240147868eSNuno Antunes * Since we know it's been destroyed, and it's our caller
13250147868eSNuno Antunes * that holds the references, just return.
13260147868eSNuno Antunes */
13270147868eSNuno Antunes ERROUT(ENOENT);
13280147868eSNuno Antunes }
13290147868eSNuno Antunes if (hook->hk_node->nd_type->connect) {
13300147868eSNuno Antunes if ((error = (*hook->hk_node->nd_type->connect) (hook))) {
13310147868eSNuno Antunes ng_destroy_hook(hook); /* also zaps peer */
1332a62226e4SSascha Wildner kprintf("failed in ng_con_part3()\n");
13330147868eSNuno Antunes ERROUT(error);
13340147868eSNuno Antunes }
13350147868eSNuno Antunes }
13360147868eSNuno Antunes /*
13370147868eSNuno Antunes * XXX this is wrong for SMP. Possibly we need
13380147868eSNuno Antunes * to separate out 'create' and 'invalid' flags.
13390147868eSNuno Antunes * should only set flags on hooks we have locked under our node.
13400147868eSNuno Antunes */
13410147868eSNuno Antunes hook->hk_flags &= ~HK_INVALID;
13420147868eSNuno Antunes done:
13430147868eSNuno Antunes NG_FREE_ITEM(item);
13440147868eSNuno Antunes return (error);
13450147868eSNuno Antunes }
13460147868eSNuno Antunes
13470147868eSNuno Antunes static int
ng_con_part2(node_p node,item_p item,hook_p hook)13480147868eSNuno Antunes ng_con_part2(node_p node, item_p item, hook_p hook)
13490147868eSNuno Antunes {
13500147868eSNuno Antunes hook_p peer;
13510147868eSNuno Antunes int error = 0;
13520147868eSNuno Antunes
13530147868eSNuno Antunes /*
13540147868eSNuno Antunes * When we run, we know that the node 'node' is locked for us.
13550147868eSNuno Antunes * Our caller has a reference on the hook.
13560147868eSNuno Antunes * Our caller has a reference on the node.
13570147868eSNuno Antunes * (In this case our caller is ng_apply_item() ).
13580147868eSNuno Antunes * The peer hook has a reference on the hook.
13590147868eSNuno Antunes * our node pointer points to the 'dead' node.
13600147868eSNuno Antunes * First check the hook name is unique.
13610147868eSNuno Antunes * Should not happen because we checked before queueing this.
13620147868eSNuno Antunes */
13630147868eSNuno Antunes if (ng_findhook(node, NG_HOOK_NAME(hook)) != NULL) {
13640147868eSNuno Antunes TRAP_ERROR();
13650147868eSNuno Antunes ng_destroy_hook(hook); /* should destroy peer too */
1366a62226e4SSascha Wildner kprintf("failed in ng_con_part2()\n");
13670147868eSNuno Antunes ERROUT(EEXIST);
13680147868eSNuno Antunes }
13690147868eSNuno Antunes /*
13700147868eSNuno Antunes * Check if the node type code has something to say about it
13710147868eSNuno Antunes * If it fails, the unref of the hook will also unref the attached node,
13720147868eSNuno Antunes * however since that node is 'ng_deadnode' this will do nothing.
13730147868eSNuno Antunes * The peer hook will also be destroyed.
13740147868eSNuno Antunes */
13750147868eSNuno Antunes if (node->nd_type->newhook != NULL) {
13760147868eSNuno Antunes if ((error = (*node->nd_type->newhook)(node, hook,
13770147868eSNuno Antunes hook->hk_name))) {
13780147868eSNuno Antunes ng_destroy_hook(hook); /* should destroy peer too */
1379a62226e4SSascha Wildner kprintf("failed in ng_con_part2()\n");
13800147868eSNuno Antunes ERROUT(error);
13810147868eSNuno Antunes }
13820147868eSNuno Antunes }
13830147868eSNuno Antunes
13840147868eSNuno Antunes /*
13850147868eSNuno Antunes * The 'type' agrees so far, so go ahead and link it in.
13860147868eSNuno Antunes * We'll ask again later when we actually connect the hooks.
13870147868eSNuno Antunes */
13880147868eSNuno Antunes hook->hk_node = node; /* just overwrite ng_deadnode */
13890147868eSNuno Antunes NG_NODE_REF(node); /* each hook counts as a reference */
13900147868eSNuno Antunes LIST_INSERT_HEAD(&node->nd_hooks, hook, hk_hooks);
13910147868eSNuno Antunes node->nd_numhooks++;
13920147868eSNuno Antunes NG_HOOK_REF(hook); /* one for the node */
13930147868eSNuno Antunes
13940147868eSNuno Antunes /*
13950147868eSNuno Antunes * We now have a symmetrical situation, where both hooks have been
13960147868eSNuno Antunes * linked to their nodes, the newhook methods have been called
13970147868eSNuno Antunes * And the references are all correct. The hooks are still marked
13980147868eSNuno Antunes * as invalid, as we have not called the 'connect' methods
13990147868eSNuno Antunes * yet.
14000147868eSNuno Antunes * We can call the local one immediately as we have the
14010147868eSNuno Antunes * node locked, but we need to queue the remote one.
14020147868eSNuno Antunes */
14030147868eSNuno Antunes if (hook->hk_node->nd_type->connect) {
14040147868eSNuno Antunes if ((error = (*hook->hk_node->nd_type->connect) (hook))) {
14050147868eSNuno Antunes ng_destroy_hook(hook); /* also zaps peer */
1406a62226e4SSascha Wildner kprintf("failed in ng_con_part2(A)\n");
14070147868eSNuno Antunes ERROUT(error);
14080147868eSNuno Antunes }
14090147868eSNuno Antunes }
14100147868eSNuno Antunes
14110147868eSNuno Antunes /*
1412dce7062eSNuno Antunes * Acquire topo token to avoid race with ng_destroy_hook().
14130147868eSNuno Antunes */
1414dce7062eSNuno Antunes TOPOLOGY_RLOCK();
14150147868eSNuno Antunes peer = hook->hk_peer;
14160147868eSNuno Antunes if (peer == &ng_deadhook) {
1417dce7062eSNuno Antunes TOPOLOGY_RUNLOCK();
1418a62226e4SSascha Wildner kprintf("failed in ng_con_part2(B)\n");
14190147868eSNuno Antunes ng_destroy_hook(hook);
14200147868eSNuno Antunes ERROUT(ENOENT);
14210147868eSNuno Antunes }
1422dce7062eSNuno Antunes TOPOLOGY_RUNLOCK();
14230147868eSNuno Antunes
14240147868eSNuno Antunes if ((error = ng_send_fn2(peer->hk_node, peer, item, &ng_con_part3,
14250147868eSNuno Antunes NULL, 0, NG_REUSE_ITEM))) {
1426a62226e4SSascha Wildner kprintf("failed in ng_con_part2(C)\n");
14270147868eSNuno Antunes ng_destroy_hook(hook); /* also zaps peer */
14280147868eSNuno Antunes return (error); /* item was consumed. */
14290147868eSNuno Antunes }
14300147868eSNuno Antunes hook->hk_flags &= ~HK_INVALID; /* need both to be able to work */
14310147868eSNuno Antunes return (0); /* item was consumed. */
14320147868eSNuno Antunes done:
14330147868eSNuno Antunes NG_FREE_ITEM(item);
14340147868eSNuno Antunes return (error);
14350147868eSNuno Antunes }
14360147868eSNuno Antunes
14370147868eSNuno Antunes /*
14380147868eSNuno Antunes * Connect this node with another node. We assume that this node is
14390147868eSNuno Antunes * currently locked, as we are only called from an NGM_CONNECT message.
14400147868eSNuno Antunes */
14410147868eSNuno Antunes static int
ng_con_nodes(item_p item,node_p node,const char * name,node_p node2,const char * name2)14420147868eSNuno Antunes ng_con_nodes(item_p item, node_p node, const char *name,
14430147868eSNuno Antunes node_p node2, const char *name2)
14440147868eSNuno Antunes {
14450147868eSNuno Antunes int error;
14460147868eSNuno Antunes hook_p hook;
14470147868eSNuno Antunes hook_p hook2;
14480147868eSNuno Antunes
14490147868eSNuno Antunes if (ng_findhook(node2, name2) != NULL) {
14500147868eSNuno Antunes return(EEXIST);
14510147868eSNuno Antunes }
14520147868eSNuno Antunes if ((error = ng_add_hook(node, name, &hook))) /* gives us a ref */
14530147868eSNuno Antunes return (error);
14540147868eSNuno Antunes /* Allocate the other hook and link it up */
14550147868eSNuno Antunes NG_ALLOC_HOOK(hook2);
14560147868eSNuno Antunes if (hook2 == NULL) {
14570147868eSNuno Antunes TRAP_ERROR();
14580147868eSNuno Antunes ng_destroy_hook(hook); /* XXX check ref counts so far */
14590147868eSNuno Antunes NG_HOOK_UNREF(hook); /* including our ref */
14600147868eSNuno Antunes return (ENOMEM);
14610147868eSNuno Antunes }
14620147868eSNuno Antunes hook2->hk_refs = 1; /* start with a reference for us. */
14630147868eSNuno Antunes hook2->hk_flags = HK_INVALID;
14640147868eSNuno Antunes hook2->hk_peer = hook; /* Link the two together */
14650147868eSNuno Antunes hook->hk_peer = hook2;
14660147868eSNuno Antunes NG_HOOK_REF(hook); /* Add a ref for the peer to each*/
14670147868eSNuno Antunes NG_HOOK_REF(hook2);
14680147868eSNuno Antunes hook2->hk_node = &ng_deadnode;
14690147868eSNuno Antunes strlcpy(NG_HOOK_NAME(hook2), name2, NG_HOOKSIZ);
14700147868eSNuno Antunes
14710147868eSNuno Antunes /*
14720147868eSNuno Antunes * Queue the function above.
14730147868eSNuno Antunes * Procesing continues in that function in the lock context of
14740147868eSNuno Antunes * the other node.
14750147868eSNuno Antunes */
14760147868eSNuno Antunes if ((error = ng_send_fn2(node2, hook2, item, &ng_con_part2, NULL, 0,
14770147868eSNuno Antunes NG_NOFLAGS))) {
1478a62226e4SSascha Wildner kprintf("failed in ng_con_nodes(): %d\n", error);
14790147868eSNuno Antunes ng_destroy_hook(hook); /* also zaps peer */
14800147868eSNuno Antunes }
14810147868eSNuno Antunes
14820147868eSNuno Antunes NG_HOOK_UNREF(hook); /* Let each hook go if it wants to */
14830147868eSNuno Antunes NG_HOOK_UNREF(hook2);
14840147868eSNuno Antunes return (error);
14850147868eSNuno Antunes }
14860147868eSNuno Antunes
14870147868eSNuno Antunes /*
14880147868eSNuno Antunes * Make a peer and connect.
14890147868eSNuno Antunes * We assume that the local node is locked.
14900147868eSNuno Antunes * The new node probably doesn't need a lock until
14910147868eSNuno Antunes * it has a hook, because it cannot really have any work until then,
14920147868eSNuno Antunes * but we should think about it a bit more.
14930147868eSNuno Antunes *
14940147868eSNuno Antunes * The problem may come if the other node also fires up
14950147868eSNuno Antunes * some hardware or a timer or some other source of activation,
14960147868eSNuno Antunes * also it may already get a command msg via it's ID.
14970147868eSNuno Antunes *
14980147868eSNuno Antunes * We could use the same method as ng_con_nodes() but we'd have
14990147868eSNuno Antunes * to add ability to remove the node when failing. (Not hard, just
15000147868eSNuno Antunes * make arg1 point to the node to remove).
15010147868eSNuno Antunes * Unless of course we just ignore failure to connect and leave
15020147868eSNuno Antunes * an unconnected node?
15030147868eSNuno Antunes */
15040147868eSNuno Antunes static int
ng_mkpeer(node_p node,const char * name,const char * name2,char * type)15050147868eSNuno Antunes ng_mkpeer(node_p node, const char *name, const char *name2, char *type)
15060147868eSNuno Antunes {
15070147868eSNuno Antunes node_p node2;
15080147868eSNuno Antunes hook_p hook1, hook2;
15090147868eSNuno Antunes int error;
15100147868eSNuno Antunes
15110147868eSNuno Antunes if ((error = ng_make_node(type, &node2))) {
15120147868eSNuno Antunes return (error);
15130147868eSNuno Antunes }
15140147868eSNuno Antunes
15150147868eSNuno Antunes if ((error = ng_add_hook(node, name, &hook1))) { /* gives us a ref */
15160147868eSNuno Antunes ng_rmnode(node2, NULL, NULL, 0);
15170147868eSNuno Antunes return (error);
15180147868eSNuno Antunes }
15190147868eSNuno Antunes
15200147868eSNuno Antunes if ((error = ng_add_hook(node2, name2, &hook2))) {
15210147868eSNuno Antunes ng_rmnode(node2, NULL, NULL, 0);
15220147868eSNuno Antunes ng_destroy_hook(hook1);
15230147868eSNuno Antunes NG_HOOK_UNREF(hook1);
15240147868eSNuno Antunes return (error);
15250147868eSNuno Antunes }
15260147868eSNuno Antunes
15270147868eSNuno Antunes /*
15280147868eSNuno Antunes * Actually link the two hooks together.
15290147868eSNuno Antunes */
15300147868eSNuno Antunes hook1->hk_peer = hook2;
15310147868eSNuno Antunes hook2->hk_peer = hook1;
15320147868eSNuno Antunes
15330147868eSNuno Antunes /* Each hook is referenced by the other */
15340147868eSNuno Antunes NG_HOOK_REF(hook1);
15350147868eSNuno Antunes NG_HOOK_REF(hook2);
15360147868eSNuno Antunes
15370147868eSNuno Antunes /* Give each node the opportunity to veto the pending connection */
15380147868eSNuno Antunes if (hook1->hk_node->nd_type->connect) {
15390147868eSNuno Antunes error = (*hook1->hk_node->nd_type->connect) (hook1);
15400147868eSNuno Antunes }
15410147868eSNuno Antunes
15420147868eSNuno Antunes if ((error == 0) && hook2->hk_node->nd_type->connect) {
15430147868eSNuno Antunes error = (*hook2->hk_node->nd_type->connect) (hook2);
15440147868eSNuno Antunes
15450147868eSNuno Antunes }
15460147868eSNuno Antunes
15470147868eSNuno Antunes /*
15480147868eSNuno Antunes * drop the references we were holding on the two hooks.
15490147868eSNuno Antunes */
15500147868eSNuno Antunes if (error) {
15510147868eSNuno Antunes ng_destroy_hook(hook2); /* also zaps hook1 */
15520147868eSNuno Antunes ng_rmnode(node2, NULL, NULL, 0);
15530147868eSNuno Antunes } else {
15540147868eSNuno Antunes /* As a last act, allow the hooks to be used */
15550147868eSNuno Antunes hook1->hk_flags &= ~HK_INVALID;
15560147868eSNuno Antunes hook2->hk_flags &= ~HK_INVALID;
15570147868eSNuno Antunes }
15580147868eSNuno Antunes NG_HOOK_UNREF(hook1);
15590147868eSNuno Antunes NG_HOOK_UNREF(hook2);
15600147868eSNuno Antunes return (error);
15610147868eSNuno Antunes }
15620147868eSNuno Antunes
15630147868eSNuno Antunes /************************************************************************
15640147868eSNuno Antunes Utility routines to send self messages
15650147868eSNuno Antunes ************************************************************************/
15660147868eSNuno Antunes
15670147868eSNuno Antunes /* Shut this node down as soon as everyone is clear of it */
15680147868eSNuno Antunes /* Should add arg "immediately" to jump the queue */
15690147868eSNuno Antunes int
ng_rmnode_self(node_p node)15700147868eSNuno Antunes ng_rmnode_self(node_p node)
15710147868eSNuno Antunes {
15720147868eSNuno Antunes int error;
15730147868eSNuno Antunes
15740147868eSNuno Antunes if (node == &ng_deadnode)
15750147868eSNuno Antunes return (0);
15760147868eSNuno Antunes node->nd_flags |= NGF_INVALID;
15770147868eSNuno Antunes if (node->nd_flags & NGF_CLOSING)
15780147868eSNuno Antunes return (0);
15790147868eSNuno Antunes
15800147868eSNuno Antunes error = ng_send_fn(node, NULL, &ng_rmnode, NULL, 0);
15810147868eSNuno Antunes return (error);
15820147868eSNuno Antunes }
15830147868eSNuno Antunes
15840147868eSNuno Antunes static void
ng_rmhook_part2(node_p node,hook_p hook,void * arg1,int arg2)15850147868eSNuno Antunes ng_rmhook_part2(node_p node, hook_p hook, void *arg1, int arg2)
15860147868eSNuno Antunes {
15870147868eSNuno Antunes ng_destroy_hook(hook);
15880147868eSNuno Antunes return ;
15890147868eSNuno Antunes }
15900147868eSNuno Antunes
15910147868eSNuno Antunes int
ng_rmhook_self(hook_p hook)15920147868eSNuno Antunes ng_rmhook_self(hook_p hook)
15930147868eSNuno Antunes {
15940147868eSNuno Antunes int error;
15950147868eSNuno Antunes node_p node = NG_HOOK_NODE(hook);
15960147868eSNuno Antunes
15970147868eSNuno Antunes if (node == &ng_deadnode)
15980147868eSNuno Antunes return (0);
15990147868eSNuno Antunes
16000147868eSNuno Antunes error = ng_send_fn(node, hook, &ng_rmhook_part2, NULL, 0);
16010147868eSNuno Antunes return (error);
16020147868eSNuno Antunes }
16030147868eSNuno Antunes
16040147868eSNuno Antunes /***********************************************************************
16050147868eSNuno Antunes * Parse and verify a string of the form: <NODE:><PATH>
16060147868eSNuno Antunes *
16070147868eSNuno Antunes * Such a string can refer to a specific node or a specific hook
16080147868eSNuno Antunes * on a specific node, depending on how you look at it. In the
16090147868eSNuno Antunes * latter case, the PATH component must not end in a dot.
16100147868eSNuno Antunes *
16110147868eSNuno Antunes * Both <NODE:> and <PATH> are optional. The <PATH> is a string
16120147868eSNuno Antunes * of hook names separated by dots. This breaks out the original
16130147868eSNuno Antunes * string, setting *nodep to "NODE" (or NULL if none) and *pathp
16140147868eSNuno Antunes * to "PATH" (or NULL if degenerate). Also, *hookp will point to
16150147868eSNuno Antunes * the final hook component of <PATH>, if any, otherwise NULL.
16160147868eSNuno Antunes *
16170147868eSNuno Antunes * This returns -1 if the path is malformed. The char ** are optional.
16180147868eSNuno Antunes ***********************************************************************/
16190147868eSNuno Antunes int
ng_path_parse(char * addr,char ** nodep,char ** pathp,char ** hookp)16200147868eSNuno Antunes ng_path_parse(char *addr, char **nodep, char **pathp, char **hookp)
16210147868eSNuno Antunes {
16220147868eSNuno Antunes char *node, *path, *hook;
16230147868eSNuno Antunes int k;
16240147868eSNuno Antunes
16250147868eSNuno Antunes /*
16260147868eSNuno Antunes * Extract absolute NODE, if any
16270147868eSNuno Antunes */
16280147868eSNuno Antunes for (path = addr; *path && *path != ':'; path++);
16290147868eSNuno Antunes if (*path) {
16300147868eSNuno Antunes node = addr; /* Here's the NODE */
16310147868eSNuno Antunes *path++ = '\0'; /* Here's the PATH */
16320147868eSNuno Antunes
16330147868eSNuno Antunes /* Node name must not be empty */
16340147868eSNuno Antunes if (!*node)
16350147868eSNuno Antunes return -1;
16360147868eSNuno Antunes
16370147868eSNuno Antunes /* A name of "." is OK; otherwise '.' not allowed */
16380147868eSNuno Antunes if (strcmp(node, ".") != 0) {
16390147868eSNuno Antunes for (k = 0; node[k]; k++)
16400147868eSNuno Antunes if (node[k] == '.')
16410147868eSNuno Antunes return -1;
16420147868eSNuno Antunes }
16430147868eSNuno Antunes } else {
16440147868eSNuno Antunes node = NULL; /* No absolute NODE */
16450147868eSNuno Antunes path = addr; /* Here's the PATH */
16460147868eSNuno Antunes }
16470147868eSNuno Antunes
16480147868eSNuno Antunes /* Snoop for illegal characters in PATH */
16490147868eSNuno Antunes for (k = 0; path[k]; k++)
16500147868eSNuno Antunes if (path[k] == ':')
16510147868eSNuno Antunes return -1;
16520147868eSNuno Antunes
16530147868eSNuno Antunes /* Check for no repeated dots in PATH */
16540147868eSNuno Antunes for (k = 0; path[k]; k++)
16550147868eSNuno Antunes if (path[k] == '.' && path[k + 1] == '.')
16560147868eSNuno Antunes return -1;
16570147868eSNuno Antunes
16580147868eSNuno Antunes /* Remove extra (degenerate) dots from beginning or end of PATH */
16590147868eSNuno Antunes if (path[0] == '.')
16600147868eSNuno Antunes path++;
16610147868eSNuno Antunes if (*path && path[strlen(path) - 1] == '.')
16620147868eSNuno Antunes path[strlen(path) - 1] = 0;
16630147868eSNuno Antunes
16640147868eSNuno Antunes /* If PATH has a dot, then we're not talking about a hook */
16650147868eSNuno Antunes if (*path) {
16660147868eSNuno Antunes for (hook = path, k = 0; path[k]; k++)
16670147868eSNuno Antunes if (path[k] == '.') {
16680147868eSNuno Antunes hook = NULL;
16690147868eSNuno Antunes break;
16700147868eSNuno Antunes }
16710147868eSNuno Antunes } else
16720147868eSNuno Antunes path = hook = NULL;
16730147868eSNuno Antunes
16740147868eSNuno Antunes /* Done */
16750147868eSNuno Antunes if (nodep)
16760147868eSNuno Antunes *nodep = node;
16770147868eSNuno Antunes if (pathp)
16780147868eSNuno Antunes *pathp = path;
16790147868eSNuno Antunes if (hookp)
16800147868eSNuno Antunes *hookp = hook;
16810147868eSNuno Antunes return (0);
16820147868eSNuno Antunes }
16830147868eSNuno Antunes
16840147868eSNuno Antunes /*
16850147868eSNuno Antunes * Given a path, which may be absolute or relative, and a starting node,
16860147868eSNuno Antunes * return the destination node.
16870147868eSNuno Antunes */
16880147868eSNuno Antunes int
ng_path2noderef(node_p here,const char * address,node_p * destp,hook_p * lasthook)16890147868eSNuno Antunes ng_path2noderef(node_p here, const char *address,
16900147868eSNuno Antunes node_p *destp, hook_p *lasthook)
16910147868eSNuno Antunes {
16920147868eSNuno Antunes char fullpath[NG_PATHSIZ];
16930147868eSNuno Antunes char *nodename, *path, pbuf[2];
16940147868eSNuno Antunes node_p node, oldnode;
16950147868eSNuno Antunes char *cp;
16960147868eSNuno Antunes hook_p hook = NULL;
16970147868eSNuno Antunes
16980147868eSNuno Antunes /* Initialize */
16990147868eSNuno Antunes if (destp == NULL) {
17000147868eSNuno Antunes TRAP_ERROR();
17010147868eSNuno Antunes return EINVAL;
17020147868eSNuno Antunes }
17030147868eSNuno Antunes *destp = NULL;
17040147868eSNuno Antunes
17050147868eSNuno Antunes /* Make a writable copy of address for ng_path_parse() */
17060147868eSNuno Antunes strncpy(fullpath, address, sizeof(fullpath) - 1);
17070147868eSNuno Antunes fullpath[sizeof(fullpath) - 1] = '\0';
17080147868eSNuno Antunes
17090147868eSNuno Antunes /* Parse out node and sequence of hooks */
17100147868eSNuno Antunes if (ng_path_parse(fullpath, &nodename, &path, NULL) < 0) {
17110147868eSNuno Antunes TRAP_ERROR();
17120147868eSNuno Antunes return EINVAL;
17130147868eSNuno Antunes }
17140147868eSNuno Antunes if (path == NULL) {
17150147868eSNuno Antunes pbuf[0] = '.'; /* Needs to be writable */
17160147868eSNuno Antunes pbuf[1] = '\0';
17170147868eSNuno Antunes path = pbuf;
17180147868eSNuno Antunes }
17190147868eSNuno Antunes
17200147868eSNuno Antunes /*
17210147868eSNuno Antunes * For an absolute address, jump to the starting node.
17220147868eSNuno Antunes * Note that this holds a reference on the node for us.
17230147868eSNuno Antunes * Don't forget to drop the reference if we don't need it.
17240147868eSNuno Antunes */
17250147868eSNuno Antunes if (nodename) {
17260147868eSNuno Antunes node = ng_name2noderef(here, nodename);
17270147868eSNuno Antunes if (node == NULL) {
17280147868eSNuno Antunes TRAP_ERROR();
17290147868eSNuno Antunes return (ENOENT);
17300147868eSNuno Antunes }
17310147868eSNuno Antunes } else {
17320147868eSNuno Antunes if (here == NULL) {
17330147868eSNuno Antunes TRAP_ERROR();
17340147868eSNuno Antunes return (EINVAL);
17350147868eSNuno Antunes }
17360147868eSNuno Antunes node = here;
17370147868eSNuno Antunes NG_NODE_REF(node);
17380147868eSNuno Antunes }
17390147868eSNuno Antunes
17400147868eSNuno Antunes /*
17410147868eSNuno Antunes * Now follow the sequence of hooks
17420147868eSNuno Antunes * XXX
17430147868eSNuno Antunes * We actually cannot guarantee that the sequence
17440147868eSNuno Antunes * is not being demolished as we crawl along it
17450147868eSNuno Antunes * without extra-ordinary locking etc.
17460147868eSNuno Antunes * So this is a bit dodgy to say the least.
17470147868eSNuno Antunes * We can probably hold up some things by holding
17480147868eSNuno Antunes * the nodelist mutex for the time of this
17490147868eSNuno Antunes * crawl if we wanted.. At least that way we wouldn't have to
17500147868eSNuno Antunes * worry about the nodes disappearing, but the hooks would still
17510147868eSNuno Antunes * be a problem.
17520147868eSNuno Antunes */
17530147868eSNuno Antunes for (cp = path; node != NULL && *cp != '\0'; ) {
17540147868eSNuno Antunes char *segment;
17550147868eSNuno Antunes
17560147868eSNuno Antunes /*
17570147868eSNuno Antunes * Break out the next path segment. Replace the dot we just
17580147868eSNuno Antunes * found with a NUL; "cp" points to the next segment (or the
17590147868eSNuno Antunes * NUL at the end).
17600147868eSNuno Antunes */
17610147868eSNuno Antunes for (segment = cp; *cp != '\0'; cp++) {
17620147868eSNuno Antunes if (*cp == '.') {
17630147868eSNuno Antunes *cp++ = '\0';
17640147868eSNuno Antunes break;
17650147868eSNuno Antunes }
17660147868eSNuno Antunes }
17670147868eSNuno Antunes
17680147868eSNuno Antunes /* Empty segment */
17690147868eSNuno Antunes if (*segment == '\0')
17700147868eSNuno Antunes continue;
17710147868eSNuno Antunes
17720147868eSNuno Antunes /* We have a segment, so look for a hook by that name */
17730147868eSNuno Antunes hook = ng_findhook(node, segment);
17740147868eSNuno Antunes
17750147868eSNuno Antunes /* Can't get there from here... */
17760147868eSNuno Antunes if (hook == NULL
17770147868eSNuno Antunes || NG_HOOK_PEER(hook) == NULL
17780147868eSNuno Antunes || NG_HOOK_NOT_VALID(hook)
17790147868eSNuno Antunes || NG_HOOK_NOT_VALID(NG_HOOK_PEER(hook))) {
17800147868eSNuno Antunes TRAP_ERROR();
17810147868eSNuno Antunes NG_NODE_UNREF(node);
17820147868eSNuno Antunes #if 0
1783a62226e4SSascha Wildner kprintf("hooknotvalid %s %s %d %d %d %d ",
17840147868eSNuno Antunes path,
17850147868eSNuno Antunes segment,
17860147868eSNuno Antunes hook == NULL,
17870147868eSNuno Antunes NG_HOOK_PEER(hook) == NULL,
17880147868eSNuno Antunes NG_HOOK_NOT_VALID(hook),
17890147868eSNuno Antunes NG_HOOK_NOT_VALID(NG_HOOK_PEER(hook)));
17900147868eSNuno Antunes #endif
17910147868eSNuno Antunes return (ENOENT);
17920147868eSNuno Antunes }
17930147868eSNuno Antunes
17940147868eSNuno Antunes /*
17950147868eSNuno Antunes * Hop on over to the next node
17960147868eSNuno Antunes * XXX
17970147868eSNuno Antunes * Big race conditions here as hooks and nodes go away
17980147868eSNuno Antunes * *** Idea.. store an ng_ID_t in each hook and use that
17990147868eSNuno Antunes * instead of the direct hook in this crawl?
18000147868eSNuno Antunes */
18010147868eSNuno Antunes oldnode = node;
18020147868eSNuno Antunes if ((node = NG_PEER_NODE(hook)))
18030147868eSNuno Antunes NG_NODE_REF(node); /* XXX RACE */
18040147868eSNuno Antunes NG_NODE_UNREF(oldnode); /* XXX another race */
18050147868eSNuno Antunes if (NG_NODE_NOT_VALID(node)) {
18060147868eSNuno Antunes NG_NODE_UNREF(node); /* XXX more races */
18070147868eSNuno Antunes node = NULL;
18080147868eSNuno Antunes }
18090147868eSNuno Antunes }
18100147868eSNuno Antunes
18110147868eSNuno Antunes /* If node somehow missing, fail here (probably this is not needed) */
18120147868eSNuno Antunes if (node == NULL) {
18130147868eSNuno Antunes TRAP_ERROR();
18140147868eSNuno Antunes return (ENXIO);
18150147868eSNuno Antunes }
18160147868eSNuno Antunes
18170147868eSNuno Antunes /* Done */
18180147868eSNuno Antunes *destp = node;
18190147868eSNuno Antunes if (lasthook != NULL)
18200147868eSNuno Antunes *lasthook = (hook ? NG_HOOK_PEER(hook) : NULL);
18210147868eSNuno Antunes return (0);
18220147868eSNuno Antunes }
18230147868eSNuno Antunes
1824ad9efc7bSNuno Antunes /*********************************************************************\
1825ad9efc7bSNuno Antunes * Inter-CPU node synchronization
18260147868eSNuno Antunes *
1827ad9efc7bSNuno Antunes * All activities are submitted to one of the netgraph per-CPU threads.
1828ad9efc7bSNuno Antunes * There is one item input queue per CPU, not one per node as in
1829ad9efc7bSNuno Antunes * FreeBSD. If the item is entering netgraph for the first time, it is
1830ad9efc7bSNuno Antunes * queued to the thread's msgport. Otherwise it is applied directly.
1831ad9efc7bSNuno Antunes * From start to finish, the item is processed on the same CPU. Items
1832ad9efc7bSNuno Antunes * are distributed based on the ingress node, to keep item ordering.
18330147868eSNuno Antunes \***************************************************************/
18340147868eSNuno Antunes
1835ad9efc7bSNuno Antunes static __inline void ng_acquire_read(node_p node);
1836ad9efc7bSNuno Antunes static __inline void ng_acquire_write(node_p node);
1837ad9efc7bSNuno Antunes static __inline void ng_leave_readwrite(node_p node);
18380147868eSNuno Antunes
1839ad9efc7bSNuno Antunes static __inline void
ng_acquire_read(node_p node)1840ad9efc7bSNuno Antunes ng_acquire_read(node_p node)
1841ad9efc7bSNuno Antunes {
1842ad9efc7bSNuno Antunes KASSERT(node != &ng_deadnode,
1843ad9efc7bSNuno Antunes ("%s: working on deadnode", __func__));
1844ad9efc7bSNuno Antunes
1845ad9efc7bSNuno Antunes lwkt_gettoken_shared(&node->nd_token);
1846ad9efc7bSNuno Antunes }
1847ad9efc7bSNuno Antunes
1848ad9efc7bSNuno Antunes /* Acquire writer lock on node. If node is busy, sleep. */
1849ad9efc7bSNuno Antunes static __inline void
ng_acquire_write(node_p node)1850ad9efc7bSNuno Antunes ng_acquire_write(node_p node)
1851ad9efc7bSNuno Antunes {
1852ad9efc7bSNuno Antunes KASSERT(node != &ng_deadnode,
1853ad9efc7bSNuno Antunes ("%s: working on deadnode", __func__));
1854ad9efc7bSNuno Antunes
1855ad9efc7bSNuno Antunes lwkt_gettoken(&node->nd_token);
1856ad9efc7bSNuno Antunes }
1857ad9efc7bSNuno Antunes
1858ad9efc7bSNuno Antunes /* Release reader or writer lock. */
1859ad9efc7bSNuno Antunes static __inline void
ng_leave_readwrite(node_p node)1860ad9efc7bSNuno Antunes ng_leave_readwrite(node_p node)
1861ad9efc7bSNuno Antunes {
1862ad9efc7bSNuno Antunes lwkt_reltoken(&node->nd_token);
1863ad9efc7bSNuno Antunes }
1864ad9efc7bSNuno Antunes
1865ad9efc7bSNuno Antunes /***********************************************************************
1866ad9efc7bSNuno Antunes * Worklist routines
1867ad9efc7bSNuno Antunes **********************************************************************/
1868ad9efc7bSNuno Antunes /* NETGRAPH thread routine
18690147868eSNuno Antunes *
1870ad9efc7bSNuno Antunes * Pick an item from our thread's queue and apply it.
18710147868eSNuno Antunes */
18720147868eSNuno Antunes static void
ngthread(void * dummy __unused)1873ad9efc7bSNuno Antunes ngthread(void *dummy __unused)
18740147868eSNuno Antunes {
1875ad9efc7bSNuno Antunes lwkt_msg_t msg;
18760147868eSNuno Antunes
1877ad9efc7bSNuno Antunes while ((msg = lwkt_waitport(&curthread->td_msgport, 0)) != NULL) {
1878ad9efc7bSNuno Antunes item_p item = (void *)msg;
18790147868eSNuno Antunes
1880ad9efc7bSNuno Antunes ng_apply_item(item);
1881ad9efc7bSNuno Antunes /* Do not reply to the message */
18820147868eSNuno Antunes }
18830147868eSNuno Antunes }
18840147868eSNuno Antunes
18850147868eSNuno Antunes /***********************************************************************
18860147868eSNuno Antunes * Externally visible method for sending or queueing messages or data.
18870147868eSNuno Antunes ***********************************************************************/
18880147868eSNuno Antunes
18890147868eSNuno Antunes /*
18900147868eSNuno Antunes * The module code should have filled out the item correctly by this stage:
18910147868eSNuno Antunes * Common:
18920147868eSNuno Antunes * reference to destination node.
18930147868eSNuno Antunes * Reference to destination rcv hook if relevant.
18940147868eSNuno Antunes * apply pointer must be or NULL or reference valid struct ng_apply_info.
18950147868eSNuno Antunes * Data:
18960147868eSNuno Antunes * pointer to mbuf
18970147868eSNuno Antunes * Control_Message:
18980147868eSNuno Antunes * pointer to msg.
18990147868eSNuno Antunes * ID of original sender node. (return address)
19000147868eSNuno Antunes * Function:
19010147868eSNuno Antunes * Function pointer
19020147868eSNuno Antunes * void * argument
19030147868eSNuno Antunes * integer argument
19040147868eSNuno Antunes *
19050147868eSNuno Antunes * The nodes have several routines and macros to help with this task:
19060147868eSNuno Antunes */
19070147868eSNuno Antunes
19080147868eSNuno Antunes int
ng_snd_item(item_p item,int flags)19090147868eSNuno Antunes ng_snd_item(item_p item, int flags)
19100147868eSNuno Antunes {
19110147868eSNuno Antunes hook_p hook;
19120147868eSNuno Antunes node_p node;
19130147868eSNuno Antunes int error = 0;
19140147868eSNuno Antunes
19150147868eSNuno Antunes /* We are sending item, so it must be present! */
19160147868eSNuno Antunes KASSERT(item != NULL, ("ng_snd_item: item is NULL"));
19170147868eSNuno Antunes
19180147868eSNuno Antunes #ifdef NETGRAPH_DEBUG
19190147868eSNuno Antunes _ngi_check(item, __FILE__, __LINE__);
19200147868eSNuno Antunes #endif
19210147868eSNuno Antunes
1922ad9efc7bSNuno Antunes /*
1923ad9efc7bSNuno Antunes * Every time an item is sent or forwarded we hold a reference on it
1924ad9efc7bSNuno Antunes * to postone the callback (if there is one) and item freedom.
1925ad9efc7bSNuno Antunes */
1926cfb360d8SNuno Antunes ng_ref_item(item);
19270147868eSNuno Antunes
1928ad9efc7bSNuno Antunes /*
1929ad9efc7bSNuno Antunes * Node is never optional.
1930ad9efc7bSNuno Antunes */
19310147868eSNuno Antunes node = NGI_NODE(item);
19320147868eSNuno Antunes KASSERT(node != NULL, ("ng_snd_item: node is NULL"));
19330147868eSNuno Antunes
1934ad9efc7bSNuno Antunes /*
1935ad9efc7bSNuno Antunes * Valid hook and mbuf are mandatory for data.
1936ad9efc7bSNuno Antunes */
19370147868eSNuno Antunes hook = NGI_HOOK(item);
19380147868eSNuno Antunes if ((item->el_flags & NGQF_TYPE) == NGQF_DATA) {
19390147868eSNuno Antunes KASSERT(hook != NULL, ("ng_snd_item: hook for data is NULL"));
19400147868eSNuno Antunes if (NGI_M(item) == NULL)
19410147868eSNuno Antunes ERROUT(EINVAL);
19420147868eSNuno Antunes CHECK_DATA_MBUF(NGI_M(item));
19430147868eSNuno Antunes }
19440147868eSNuno Antunes
19450147868eSNuno Antunes /*
1946ad9efc7bSNuno Antunes * Always queue items entering netgraph for the first time.
19470147868eSNuno Antunes */
1948cfb360d8SNuno Antunes if (item->refs == 1) {
1949ad9efc7bSNuno Antunes struct lwkt_msg *msg = &item->el_lmsg;
19500147868eSNuno Antunes
1951ad9efc7bSNuno Antunes lwkt_initmsg(msg, &ng_panic_reply_port, 0);
1952ad9efc7bSNuno Antunes /* Always send to cpu0 for now */
1953ad9efc7bSNuno Antunes lwkt_sendmsg(ng_cpuport(0), msg);
19540147868eSNuno Antunes
19550147868eSNuno Antunes return ((flags & NG_PROGRESS) ? EINPROGRESS : 0);
19560147868eSNuno Antunes }
19570147868eSNuno Antunes
19580147868eSNuno Antunes /*
1959ad9efc7bSNuno Antunes * The item wasn't queued. Process it synchronously.
19600147868eSNuno Antunes */
1961ad9efc7bSNuno Antunes error = ng_apply_item(item);
19620147868eSNuno Antunes
19630147868eSNuno Antunes done:
19640147868eSNuno Antunes return (error);
19650147868eSNuno Antunes }
19660147868eSNuno Antunes
19670147868eSNuno Antunes /*
19680147868eSNuno Antunes * We have an item that was possibly queued somewhere.
19690147868eSNuno Antunes * It should contain all the information needed
19700147868eSNuno Antunes * to run it on the appropriate node/hook.
19710147868eSNuno Antunes * If there is apply pointer and we own the last reference, call apply().
19720147868eSNuno Antunes */
19730147868eSNuno Antunes static int
ng_apply_item(item_p item)1974ad9efc7bSNuno Antunes ng_apply_item(item_p item)
19750147868eSNuno Antunes {
19760147868eSNuno Antunes hook_p hook;
1977ad9efc7bSNuno Antunes node_p node;
19780147868eSNuno Antunes ng_rcvdata_t *rcvdata;
19790147868eSNuno Antunes ng_rcvmsg_t *rcvmsg;
1980ad9efc7bSNuno Antunes int error = 0;
19810147868eSNuno Antunes
19820147868eSNuno Antunes /* Node and item are never optional. */
19830147868eSNuno Antunes KASSERT(item != NULL, ("ng_apply_item: item is NULL"));
1984ad9efc7bSNuno Antunes NGI_GET_NODE(item, node); /* zaps stored node */
1985ad9efc7bSNuno Antunes KASSERT(node != NULL, ("ng_apply_item: node is NULL"));
19860147868eSNuno Antunes NGI_GET_HOOK(item, hook); /* clears stored hook */
1987ad9efc7bSNuno Antunes
1988ad9efc7bSNuno Antunes /*
1989ad9efc7bSNuno Antunes * If the item or the node specifies single threading, force
1990ad9efc7bSNuno Antunes * writer semantics. Similarly, the node may say one hook always
1991ad9efc7bSNuno Antunes * produces writers. These are overrides.
1992ad9efc7bSNuno Antunes */
1993ad9efc7bSNuno Antunes if (((item->el_flags & NGQF_RW) == NGQF_WRITER) ||
1994ad9efc7bSNuno Antunes (node->nd_flags & NGF_FORCE_WRITER) ||
1995ad9efc7bSNuno Antunes (hook && (hook->hk_flags & HK_FORCE_WRITER))) {
1996ad9efc7bSNuno Antunes ng_acquire_write(node);
1997ad9efc7bSNuno Antunes } else {
1998ad9efc7bSNuno Antunes ng_acquire_read(node);
1999ad9efc7bSNuno Antunes }
2000ad9efc7bSNuno Antunes
20010147868eSNuno Antunes #ifdef NETGRAPH_DEBUG
20020147868eSNuno Antunes _ngi_check(item, __FILE__, __LINE__);
20030147868eSNuno Antunes #endif
20040147868eSNuno Antunes
20050147868eSNuno Antunes switch (item->el_flags & NGQF_TYPE) {
20060147868eSNuno Antunes case NGQF_DATA:
20070147868eSNuno Antunes /*
20080147868eSNuno Antunes * Check things are still ok as when we were queued.
20090147868eSNuno Antunes */
20100147868eSNuno Antunes KASSERT(hook != NULL, ("ng_apply_item: hook for data is NULL"));
20110147868eSNuno Antunes if (NG_HOOK_NOT_VALID(hook) ||
20120147868eSNuno Antunes NG_NODE_NOT_VALID(node)) {
20130147868eSNuno Antunes error = EIO;
20140147868eSNuno Antunes NG_FREE_ITEM(item);
20150147868eSNuno Antunes break;
20160147868eSNuno Antunes }
20170147868eSNuno Antunes /*
20180147868eSNuno Antunes * If no receive method, just silently drop it.
20190147868eSNuno Antunes * Give preference to the hook over-ride method
20200147868eSNuno Antunes */
20210147868eSNuno Antunes if ((!(rcvdata = hook->hk_rcvdata))
20220147868eSNuno Antunes && (!(rcvdata = NG_HOOK_NODE(hook)->nd_type->rcvdata))) {
20230147868eSNuno Antunes error = 0;
20240147868eSNuno Antunes NG_FREE_ITEM(item);
20250147868eSNuno Antunes break;
20260147868eSNuno Antunes }
20270147868eSNuno Antunes error = (*rcvdata)(hook, item);
20280147868eSNuno Antunes break;
20290147868eSNuno Antunes case NGQF_MESG:
20300147868eSNuno Antunes if (hook && NG_HOOK_NOT_VALID(hook)) {
20310147868eSNuno Antunes /*
20320147868eSNuno Antunes * The hook has been zapped then we can't use it.
20330147868eSNuno Antunes * Immediately drop its reference.
20340147868eSNuno Antunes * The message may not need it.
20350147868eSNuno Antunes */
20360147868eSNuno Antunes NG_HOOK_UNREF(hook);
20370147868eSNuno Antunes hook = NULL;
20380147868eSNuno Antunes }
20390147868eSNuno Antunes /*
20400147868eSNuno Antunes * Similarly, if the node is a zombie there is
20410147868eSNuno Antunes * nothing we can do with it, drop everything.
20420147868eSNuno Antunes */
20430147868eSNuno Antunes if (NG_NODE_NOT_VALID(node)) {
20440147868eSNuno Antunes TRAP_ERROR();
20450147868eSNuno Antunes error = EINVAL;
20460147868eSNuno Antunes NG_FREE_ITEM(item);
20470147868eSNuno Antunes break;
20480147868eSNuno Antunes }
20490147868eSNuno Antunes /*
20500147868eSNuno Antunes * Call the appropriate message handler for the object.
20510147868eSNuno Antunes * It is up to the message handler to free the message.
20520147868eSNuno Antunes * If it's a generic message, handle it generically,
20530147868eSNuno Antunes * otherwise call the type's message handler (if it exists).
20540147868eSNuno Antunes * XXX (race). Remember that a queued message may
20550147868eSNuno Antunes * reference a node or hook that has just been
20560147868eSNuno Antunes * invalidated. It will exist as the queue code
20570147868eSNuno Antunes * is holding a reference, but..
20580147868eSNuno Antunes */
20590147868eSNuno Antunes if ((NGI_MSG(item)->header.typecookie == NGM_GENERIC_COOKIE) &&
20600147868eSNuno Antunes ((NGI_MSG(item)->header.flags & NGF_RESP) == 0)) {
20610147868eSNuno Antunes error = ng_generic_msg(node, item, hook);
20620147868eSNuno Antunes break;
20630147868eSNuno Antunes }
20640147868eSNuno Antunes if (((!hook) || (!(rcvmsg = hook->hk_rcvmsg))) &&
20650147868eSNuno Antunes (!(rcvmsg = node->nd_type->rcvmsg))) {
20660147868eSNuno Antunes TRAP_ERROR();
20670147868eSNuno Antunes error = 0;
20680147868eSNuno Antunes NG_FREE_ITEM(item);
20690147868eSNuno Antunes break;
20700147868eSNuno Antunes }
20710147868eSNuno Antunes error = (*rcvmsg)(node, item, hook);
20720147868eSNuno Antunes break;
20730147868eSNuno Antunes case NGQF_FN:
20740147868eSNuno Antunes case NGQF_FN2:
20750147868eSNuno Antunes /*
20760147868eSNuno Antunes * We have to implicitly trust the hook,
20770147868eSNuno Antunes * as some of these are used for system purposes
20780147868eSNuno Antunes * where the hook is invalid. In the case of
20790147868eSNuno Antunes * the shutdown message we allow it to hit
20800147868eSNuno Antunes * even if the node is invalid.
20810147868eSNuno Antunes */
20820147868eSNuno Antunes if ((NG_NODE_NOT_VALID(node))
20830147868eSNuno Antunes && (NGI_FN(item) != &ng_rmnode)) {
20840147868eSNuno Antunes TRAP_ERROR();
20850147868eSNuno Antunes error = EINVAL;
20860147868eSNuno Antunes NG_FREE_ITEM(item);
20870147868eSNuno Antunes break;
20880147868eSNuno Antunes }
20890147868eSNuno Antunes if ((item->el_flags & NGQF_TYPE) == NGQF_FN) {
20900147868eSNuno Antunes (*NGI_FN(item))(node, hook, NGI_ARG1(item),
20910147868eSNuno Antunes NGI_ARG2(item));
20920147868eSNuno Antunes NG_FREE_ITEM(item);
20930147868eSNuno Antunes } else /* it is NGQF_FN2 */
20940147868eSNuno Antunes error = (*NGI_FN2(item))(node, item, hook);
20950147868eSNuno Antunes break;
20960147868eSNuno Antunes }
20970147868eSNuno Antunes /*
20980147868eSNuno Antunes * We held references on some of the resources
20990147868eSNuno Antunes * that we took from the item. Now that we have
21000147868eSNuno Antunes * finished doing everything, drop those references.
21010147868eSNuno Antunes */
21020147868eSNuno Antunes if (hook)
21030147868eSNuno Antunes NG_HOOK_UNREF(hook);
21040147868eSNuno Antunes
2105ad9efc7bSNuno Antunes /* Release our node's token */
2106ad9efc7bSNuno Antunes ng_leave_readwrite(node);
21070147868eSNuno Antunes
2108ad9efc7bSNuno Antunes /* Free the item if we own the last reference to it. */
2109cfb360d8SNuno Antunes ng_unref_item(item, error);
2110cfb360d8SNuno Antunes
2111ad9efc7bSNuno Antunes NG_NODE_UNREF(node);
21120147868eSNuno Antunes
21130147868eSNuno Antunes return (error);
21140147868eSNuno Antunes }
21150147868eSNuno Antunes
21160147868eSNuno Antunes /***********************************************************************
21170147868eSNuno Antunes * Implement the 'generic' control messages
21180147868eSNuno Antunes ***********************************************************************/
21190147868eSNuno Antunes static int
ng_generic_msg(node_p here,item_p item,hook_p lasthook)21200147868eSNuno Antunes ng_generic_msg(node_p here, item_p item, hook_p lasthook)
21210147868eSNuno Antunes {
21220147868eSNuno Antunes int error = 0;
21230147868eSNuno Antunes struct ng_mesg *msg;
21240147868eSNuno Antunes struct ng_mesg *resp = NULL;
21250147868eSNuno Antunes
21260147868eSNuno Antunes NGI_GET_MSG(item, msg);
21270147868eSNuno Antunes if (msg->header.typecookie != NGM_GENERIC_COOKIE) {
21280147868eSNuno Antunes TRAP_ERROR();
21290147868eSNuno Antunes error = EINVAL;
21300147868eSNuno Antunes goto out;
21310147868eSNuno Antunes }
21320147868eSNuno Antunes switch (msg->header.cmd) {
21330147868eSNuno Antunes case NGM_SHUTDOWN:
21340147868eSNuno Antunes ng_rmnode(here, NULL, NULL, 0);
21350147868eSNuno Antunes break;
21360147868eSNuno Antunes case NGM_MKPEER:
21370147868eSNuno Antunes {
21380147868eSNuno Antunes struct ngm_mkpeer *const mkp = (struct ngm_mkpeer *) msg->data;
21390147868eSNuno Antunes
21400147868eSNuno Antunes if (msg->header.arglen != sizeof(*mkp)) {
21410147868eSNuno Antunes TRAP_ERROR();
21420147868eSNuno Antunes error = EINVAL;
21430147868eSNuno Antunes break;
21440147868eSNuno Antunes }
21450147868eSNuno Antunes mkp->type[sizeof(mkp->type) - 1] = '\0';
21460147868eSNuno Antunes mkp->ourhook[sizeof(mkp->ourhook) - 1] = '\0';
21470147868eSNuno Antunes mkp->peerhook[sizeof(mkp->peerhook) - 1] = '\0';
21480147868eSNuno Antunes error = ng_mkpeer(here, mkp->ourhook, mkp->peerhook, mkp->type);
21490147868eSNuno Antunes break;
21500147868eSNuno Antunes }
21510147868eSNuno Antunes case NGM_CONNECT:
21520147868eSNuno Antunes {
21530147868eSNuno Antunes struct ngm_connect *const con =
21540147868eSNuno Antunes (struct ngm_connect *) msg->data;
21550147868eSNuno Antunes node_p node2;
21560147868eSNuno Antunes
21570147868eSNuno Antunes if (msg->header.arglen != sizeof(*con)) {
21580147868eSNuno Antunes TRAP_ERROR();
21590147868eSNuno Antunes error = EINVAL;
21600147868eSNuno Antunes break;
21610147868eSNuno Antunes }
21620147868eSNuno Antunes con->path[sizeof(con->path) - 1] = '\0';
21630147868eSNuno Antunes con->ourhook[sizeof(con->ourhook) - 1] = '\0';
21640147868eSNuno Antunes con->peerhook[sizeof(con->peerhook) - 1] = '\0';
21650147868eSNuno Antunes /* Don't forget we get a reference.. */
21660147868eSNuno Antunes error = ng_path2noderef(here, con->path, &node2, NULL);
21670147868eSNuno Antunes if (error)
21680147868eSNuno Antunes break;
21690147868eSNuno Antunes error = ng_con_nodes(item, here, con->ourhook,
21700147868eSNuno Antunes node2, con->peerhook);
21710147868eSNuno Antunes NG_NODE_UNREF(node2);
21720147868eSNuno Antunes break;
21730147868eSNuno Antunes }
21740147868eSNuno Antunes case NGM_NAME:
21750147868eSNuno Antunes {
21760147868eSNuno Antunes struct ngm_name *const nam = (struct ngm_name *) msg->data;
21770147868eSNuno Antunes
21780147868eSNuno Antunes if (msg->header.arglen != sizeof(*nam)) {
21790147868eSNuno Antunes TRAP_ERROR();
21800147868eSNuno Antunes error = EINVAL;
21810147868eSNuno Antunes break;
21820147868eSNuno Antunes }
21830147868eSNuno Antunes nam->name[sizeof(nam->name) - 1] = '\0';
21840147868eSNuno Antunes error = ng_name_node(here, nam->name);
21850147868eSNuno Antunes break;
21860147868eSNuno Antunes }
21870147868eSNuno Antunes case NGM_RMHOOK:
21880147868eSNuno Antunes {
21890147868eSNuno Antunes struct ngm_rmhook *const rmh = (struct ngm_rmhook *) msg->data;
21900147868eSNuno Antunes hook_p hook;
21910147868eSNuno Antunes
21920147868eSNuno Antunes if (msg->header.arglen != sizeof(*rmh)) {
21930147868eSNuno Antunes TRAP_ERROR();
21940147868eSNuno Antunes error = EINVAL;
21950147868eSNuno Antunes break;
21960147868eSNuno Antunes }
21970147868eSNuno Antunes rmh->ourhook[sizeof(rmh->ourhook) - 1] = '\0';
21980147868eSNuno Antunes if ((hook = ng_findhook(here, rmh->ourhook)) != NULL)
21990147868eSNuno Antunes ng_destroy_hook(hook);
22000147868eSNuno Antunes break;
22010147868eSNuno Antunes }
22020147868eSNuno Antunes case NGM_NODEINFO:
22030147868eSNuno Antunes {
22040147868eSNuno Antunes struct nodeinfo *ni;
22050147868eSNuno Antunes
22060147868eSNuno Antunes NG_MKRESPONSE(resp, msg, sizeof(*ni), M_WAITOK | M_NULLOK);
22070147868eSNuno Antunes if (resp == NULL) {
22080147868eSNuno Antunes error = ENOMEM;
22090147868eSNuno Antunes break;
22100147868eSNuno Antunes }
22110147868eSNuno Antunes
22120147868eSNuno Antunes /* Fill in node info */
22130147868eSNuno Antunes ni = (struct nodeinfo *) resp->data;
22140147868eSNuno Antunes if (NG_NODE_HAS_NAME(here))
22150147868eSNuno Antunes strcpy(ni->name, NG_NODE_NAME(here));
22160147868eSNuno Antunes strcpy(ni->type, here->nd_type->name);
22170147868eSNuno Antunes ni->id = ng_node2ID(here);
22180147868eSNuno Antunes ni->hooks = here->nd_numhooks;
22190147868eSNuno Antunes break;
22200147868eSNuno Antunes }
22210147868eSNuno Antunes case NGM_LISTHOOKS:
22220147868eSNuno Antunes {
22230147868eSNuno Antunes const int nhooks = here->nd_numhooks;
22240147868eSNuno Antunes struct hooklist *hl;
22250147868eSNuno Antunes struct nodeinfo *ni;
22260147868eSNuno Antunes hook_p hook;
22270147868eSNuno Antunes
22280147868eSNuno Antunes /* Get response struct */
22290147868eSNuno Antunes NG_MKRESPONSE(resp, msg, sizeof(*hl)
22300147868eSNuno Antunes + (nhooks * sizeof(struct linkinfo)), M_WAITOK | M_NULLOK);
22310147868eSNuno Antunes if (resp == NULL) {
22320147868eSNuno Antunes error = ENOMEM;
22330147868eSNuno Antunes break;
22340147868eSNuno Antunes }
22350147868eSNuno Antunes hl = (struct hooklist *) resp->data;
22360147868eSNuno Antunes ni = &hl->nodeinfo;
22370147868eSNuno Antunes
22380147868eSNuno Antunes /* Fill in node info */
22390147868eSNuno Antunes if (NG_NODE_HAS_NAME(here))
22400147868eSNuno Antunes strcpy(ni->name, NG_NODE_NAME(here));
22410147868eSNuno Antunes strcpy(ni->type, here->nd_type->name);
22420147868eSNuno Antunes ni->id = ng_node2ID(here);
22430147868eSNuno Antunes
22440147868eSNuno Antunes /* Cycle through the linked list of hooks */
22450147868eSNuno Antunes ni->hooks = 0;
22460147868eSNuno Antunes LIST_FOREACH(hook, &here->nd_hooks, hk_hooks) {
22470147868eSNuno Antunes struct linkinfo *const link = &hl->link[ni->hooks];
22480147868eSNuno Antunes
22490147868eSNuno Antunes if (ni->hooks >= nhooks) {
22500147868eSNuno Antunes log(LOG_ERR, "%s: number of %s changed\n",
22510147868eSNuno Antunes __func__, "hooks");
22520147868eSNuno Antunes break;
22530147868eSNuno Antunes }
22540147868eSNuno Antunes if (NG_HOOK_NOT_VALID(hook))
22550147868eSNuno Antunes continue;
22560147868eSNuno Antunes strcpy(link->ourhook, NG_HOOK_NAME(hook));
22570147868eSNuno Antunes strcpy(link->peerhook, NG_PEER_HOOK_NAME(hook));
22580147868eSNuno Antunes if (NG_PEER_NODE_NAME(hook)[0] != '\0')
22590147868eSNuno Antunes strcpy(link->nodeinfo.name,
22600147868eSNuno Antunes NG_PEER_NODE_NAME(hook));
22610147868eSNuno Antunes strcpy(link->nodeinfo.type,
22620147868eSNuno Antunes NG_PEER_NODE(hook)->nd_type->name);
22630147868eSNuno Antunes link->nodeinfo.id = ng_node2ID(NG_PEER_NODE(hook));
22640147868eSNuno Antunes link->nodeinfo.hooks = NG_PEER_NODE(hook)->nd_numhooks;
22650147868eSNuno Antunes ni->hooks++;
22660147868eSNuno Antunes }
22670147868eSNuno Antunes break;
22680147868eSNuno Antunes }
22690147868eSNuno Antunes
22700147868eSNuno Antunes case NGM_LISTNODES:
22710147868eSNuno Antunes {
22720147868eSNuno Antunes struct namelist *nl;
22730147868eSNuno Antunes node_p node;
22740560a357SNuno Antunes int i;
22750147868eSNuno Antunes
22760560a357SNuno Antunes IDHASH_RLOCK();
22770560a357SNuno Antunes /* Get response struct. */
22780560a357SNuno Antunes NG_MKRESPONSE(resp, msg, sizeof(*nl) +
22790560a357SNuno Antunes (ng_nodes * sizeof(struct nodeinfo)), M_NOWAIT | M_ZERO);
22800147868eSNuno Antunes if (resp == NULL) {
22810560a357SNuno Antunes IDHASH_RUNLOCK();
22820147868eSNuno Antunes error = ENOMEM;
22830147868eSNuno Antunes break;
22840147868eSNuno Antunes }
22850147868eSNuno Antunes nl = (struct namelist *) resp->data;
22860147868eSNuno Antunes
22870560a357SNuno Antunes /* Cycle through the lists of nodes. */
22880147868eSNuno Antunes nl->numnames = 0;
22890560a357SNuno Antunes for (i = 0; i <= ng_ID_hmask; i++) {
22900560a357SNuno Antunes LIST_FOREACH(node, &ng_ID_hash[i], nd_idnodes) {
22910560a357SNuno Antunes struct nodeinfo *const np =
22920560a357SNuno Antunes &nl->nodeinfo[nl->numnames];
22930560a357SNuno Antunes
22940560a357SNuno Antunes if (NG_NODE_NOT_VALID(node))
22950560a357SNuno Antunes continue;
22960560a357SNuno Antunes if (NG_NODE_HAS_NAME(node))
22970560a357SNuno Antunes strcpy(np->name, NG_NODE_NAME(node));
22980560a357SNuno Antunes strcpy(np->type, node->nd_type->name);
22990560a357SNuno Antunes np->id = ng_node2ID(node);
23000560a357SNuno Antunes np->hooks = node->nd_numhooks;
23010560a357SNuno Antunes KASSERT(nl->numnames < ng_nodes,
23020560a357SNuno Antunes ("%s: no space", __func__));
23030560a357SNuno Antunes nl->numnames++;
23040560a357SNuno Antunes }
23050560a357SNuno Antunes }
23060560a357SNuno Antunes IDHASH_RUNLOCK();
23070560a357SNuno Antunes break;
23080560a357SNuno Antunes }
23090560a357SNuno Antunes case NGM_LISTNAMES:
23100560a357SNuno Antunes {
23110560a357SNuno Antunes struct namelist *nl;
23120560a357SNuno Antunes node_p node;
23130560a357SNuno Antunes int i;
23140560a357SNuno Antunes
23150560a357SNuno Antunes NAMEHASH_RLOCK();
23160560a357SNuno Antunes /* Get response struct. */
23170560a357SNuno Antunes NG_MKRESPONSE(resp, msg, sizeof(*nl) +
23180560a357SNuno Antunes (ng_named_nodes * sizeof(struct nodeinfo)), M_NOWAIT);
23190560a357SNuno Antunes if (resp == NULL) {
23200560a357SNuno Antunes NAMEHASH_RUNLOCK();
23210560a357SNuno Antunes error = ENOMEM;
23220560a357SNuno Antunes break;
23230560a357SNuno Antunes }
23240560a357SNuno Antunes nl = (struct namelist *) resp->data;
23250560a357SNuno Antunes
23260560a357SNuno Antunes /* Cycle through the lists of nodes. */
23270560a357SNuno Antunes nl->numnames = 0;
23280560a357SNuno Antunes for (i = 0; i <= ng_name_hmask; i++) {
23290147868eSNuno Antunes LIST_FOREACH(node, &ng_name_hash[i], nd_nodes) {
23300147868eSNuno Antunes struct nodeinfo *const np =
23310147868eSNuno Antunes &nl->nodeinfo[nl->numnames];
23320147868eSNuno Antunes
23330147868eSNuno Antunes if (NG_NODE_NOT_VALID(node))
23340147868eSNuno Antunes continue;
23350147868eSNuno Antunes strcpy(np->name, NG_NODE_NAME(node));
23360147868eSNuno Antunes strcpy(np->type, node->nd_type->name);
23370147868eSNuno Antunes np->id = ng_node2ID(node);
23380147868eSNuno Antunes np->hooks = node->nd_numhooks;
23390560a357SNuno Antunes KASSERT(nl->numnames < ng_named_nodes,
23400560a357SNuno Antunes ("%s: no space", __func__));
23410147868eSNuno Antunes nl->numnames++;
23420147868eSNuno Antunes }
23430147868eSNuno Antunes }
23440560a357SNuno Antunes NAMEHASH_RUNLOCK();
23450147868eSNuno Antunes break;
23460147868eSNuno Antunes }
23470147868eSNuno Antunes
23480147868eSNuno Antunes case NGM_LISTTYPES:
23490147868eSNuno Antunes {
23500147868eSNuno Antunes struct typelist *tl;
23510147868eSNuno Antunes struct ng_type *type;
23520147868eSNuno Antunes int num = 0;
23530147868eSNuno Antunes
23547e5f7144SNuno Antunes TYPELIST_RLOCK();
23550147868eSNuno Antunes /* Count number of types */
23567e5f7144SNuno Antunes LIST_FOREACH(type, &ng_typelist, types)
23570147868eSNuno Antunes num++;
23580147868eSNuno Antunes
23590147868eSNuno Antunes /* Get response struct */
23600147868eSNuno Antunes NG_MKRESPONSE(resp, msg, sizeof(*tl)
23610147868eSNuno Antunes + (num * sizeof(struct typeinfo)), M_WAITOK | M_NULLOK);
23620147868eSNuno Antunes if (resp == NULL) {
23637e5f7144SNuno Antunes TYPELIST_RUNLOCK();
23640147868eSNuno Antunes error = ENOMEM;
23650147868eSNuno Antunes break;
23660147868eSNuno Antunes }
23670147868eSNuno Antunes tl = (struct typelist *) resp->data;
23680147868eSNuno Antunes
23690147868eSNuno Antunes /* Cycle through the linked list of types */
23700147868eSNuno Antunes tl->numtypes = 0;
23710147868eSNuno Antunes LIST_FOREACH(type, &ng_typelist, types) {
23720147868eSNuno Antunes struct typeinfo *const tp = &tl->typeinfo[tl->numtypes];
23730147868eSNuno Antunes
23740147868eSNuno Antunes strcpy(tp->type_name, type->name);
23750147868eSNuno Antunes tp->numnodes = type->refs - 1; /* don't count list */
23767e5f7144SNuno Antunes KASSERT(tl->numtypes < num, ("%s: no space", __func__));
23770147868eSNuno Antunes tl->numtypes++;
23780147868eSNuno Antunes }
23797e5f7144SNuno Antunes TYPELIST_RUNLOCK();
23800147868eSNuno Antunes break;
23810147868eSNuno Antunes }
23820147868eSNuno Antunes
23830147868eSNuno Antunes case NGM_BINARY2ASCII:
23840147868eSNuno Antunes {
23850147868eSNuno Antunes int bufSize = 20 * 1024; /* XXX hard coded constant */
23860147868eSNuno Antunes const struct ng_parse_type *argstype;
23870147868eSNuno Antunes const struct ng_cmdlist *c;
23880147868eSNuno Antunes struct ng_mesg *binary, *ascii;
23890147868eSNuno Antunes
23900147868eSNuno Antunes /* Data area must contain a valid netgraph message */
23910147868eSNuno Antunes binary = (struct ng_mesg *)msg->data;
23920147868eSNuno Antunes if (msg->header.arglen < sizeof(struct ng_mesg) ||
23930147868eSNuno Antunes (msg->header.arglen - sizeof(struct ng_mesg) <
23940147868eSNuno Antunes binary->header.arglen)) {
23950147868eSNuno Antunes TRAP_ERROR();
23960147868eSNuno Antunes error = EINVAL;
23970147868eSNuno Antunes break;
23980147868eSNuno Antunes }
23990147868eSNuno Antunes
24000147868eSNuno Antunes /* Get a response message with lots of room */
24010147868eSNuno Antunes NG_MKRESPONSE(resp, msg, sizeof(*ascii) + bufSize, M_WAITOK | M_NULLOK);
24020147868eSNuno Antunes if (resp == NULL) {
24030147868eSNuno Antunes error = ENOMEM;
24040147868eSNuno Antunes break;
24050147868eSNuno Antunes }
24060147868eSNuno Antunes ascii = (struct ng_mesg *)resp->data;
24070147868eSNuno Antunes
24080147868eSNuno Antunes /* Copy binary message header to response message payload */
24090147868eSNuno Antunes bcopy(binary, ascii, sizeof(*binary));
24100147868eSNuno Antunes
24110147868eSNuno Antunes /* Find command by matching typecookie and command number */
24120147868eSNuno Antunes for (c = here->nd_type->cmdlist;
24130147868eSNuno Antunes c != NULL && c->name != NULL; c++) {
24140147868eSNuno Antunes if (binary->header.typecookie == c->cookie
24150147868eSNuno Antunes && binary->header.cmd == c->cmd)
24160147868eSNuno Antunes break;
24170147868eSNuno Antunes }
24180147868eSNuno Antunes if (c == NULL || c->name == NULL) {
24190147868eSNuno Antunes for (c = ng_generic_cmds; c->name != NULL; c++) {
24200147868eSNuno Antunes if (binary->header.typecookie == c->cookie
24210147868eSNuno Antunes && binary->header.cmd == c->cmd)
24220147868eSNuno Antunes break;
24230147868eSNuno Antunes }
24240147868eSNuno Antunes if (c->name == NULL) {
24250147868eSNuno Antunes NG_FREE_MSG(resp);
24260147868eSNuno Antunes error = ENOSYS;
24270147868eSNuno Antunes break;
24280147868eSNuno Antunes }
24290147868eSNuno Antunes }
24300147868eSNuno Antunes
24310147868eSNuno Antunes /* Convert command name to ASCII */
2432a62226e4SSascha Wildner ksnprintf(ascii->header.cmdstr, sizeof(ascii->header.cmdstr),
24330147868eSNuno Antunes "%s", c->name);
24340147868eSNuno Antunes
24350147868eSNuno Antunes /* Convert command arguments to ASCII */
24360147868eSNuno Antunes argstype = (binary->header.flags & NGF_RESP) ?
24370147868eSNuno Antunes c->respType : c->mesgType;
24380147868eSNuno Antunes if (argstype == NULL) {
24390147868eSNuno Antunes *ascii->data = '\0';
24400147868eSNuno Antunes } else {
24410147868eSNuno Antunes if ((error = ng_unparse(argstype,
24420147868eSNuno Antunes (u_char *)binary->data,
24430147868eSNuno Antunes ascii->data, bufSize)) != 0) {
24440147868eSNuno Antunes NG_FREE_MSG(resp);
24450147868eSNuno Antunes break;
24460147868eSNuno Antunes }
24470147868eSNuno Antunes }
24480147868eSNuno Antunes
24490147868eSNuno Antunes /* Return the result as struct ng_mesg plus ASCII string */
24500147868eSNuno Antunes bufSize = strlen(ascii->data) + 1;
24510147868eSNuno Antunes ascii->header.arglen = bufSize;
24520147868eSNuno Antunes resp->header.arglen = sizeof(*ascii) + bufSize;
24530147868eSNuno Antunes break;
24540147868eSNuno Antunes }
24550147868eSNuno Antunes
24560147868eSNuno Antunes case NGM_ASCII2BINARY:
24570147868eSNuno Antunes {
24580147868eSNuno Antunes int bufSize = 2000; /* XXX hard coded constant */
24590147868eSNuno Antunes const struct ng_cmdlist *c;
24600147868eSNuno Antunes const struct ng_parse_type *argstype;
24610147868eSNuno Antunes struct ng_mesg *ascii, *binary;
24620147868eSNuno Antunes int off = 0;
24630147868eSNuno Antunes
24640147868eSNuno Antunes /* Data area must contain at least a struct ng_mesg + '\0' */
24650147868eSNuno Antunes ascii = (struct ng_mesg *)msg->data;
24660147868eSNuno Antunes if ((msg->header.arglen < sizeof(*ascii) + 1) ||
24670147868eSNuno Antunes (ascii->header.arglen < 1) ||
24680147868eSNuno Antunes (msg->header.arglen < sizeof(*ascii) +
24690147868eSNuno Antunes ascii->header.arglen)) {
24700147868eSNuno Antunes TRAP_ERROR();
24710147868eSNuno Antunes error = EINVAL;
24720147868eSNuno Antunes break;
24730147868eSNuno Antunes }
24740147868eSNuno Antunes ascii->data[ascii->header.arglen - 1] = '\0';
24750147868eSNuno Antunes
24760147868eSNuno Antunes /* Get a response message with lots of room */
24770147868eSNuno Antunes NG_MKRESPONSE(resp, msg, sizeof(*binary) + bufSize, M_WAITOK | M_NULLOK);
24780147868eSNuno Antunes if (resp == NULL) {
24790147868eSNuno Antunes error = ENOMEM;
24800147868eSNuno Antunes break;
24810147868eSNuno Antunes }
24820147868eSNuno Antunes binary = (struct ng_mesg *)resp->data;
24830147868eSNuno Antunes
24840147868eSNuno Antunes /* Copy ASCII message header to response message payload */
24850147868eSNuno Antunes bcopy(ascii, binary, sizeof(*ascii));
24860147868eSNuno Antunes
24870147868eSNuno Antunes /* Find command by matching ASCII command string */
24880147868eSNuno Antunes for (c = here->nd_type->cmdlist;
24890147868eSNuno Antunes c != NULL && c->name != NULL; c++) {
24900147868eSNuno Antunes if (strcmp(ascii->header.cmdstr, c->name) == 0)
24910147868eSNuno Antunes break;
24920147868eSNuno Antunes }
24930147868eSNuno Antunes if (c == NULL || c->name == NULL) {
24940147868eSNuno Antunes for (c = ng_generic_cmds; c->name != NULL; c++) {
24950147868eSNuno Antunes if (strcmp(ascii->header.cmdstr, c->name) == 0)
24960147868eSNuno Antunes break;
24970147868eSNuno Antunes }
24980147868eSNuno Antunes if (c->name == NULL) {
24990147868eSNuno Antunes NG_FREE_MSG(resp);
25000147868eSNuno Antunes error = ENOSYS;
25010147868eSNuno Antunes break;
25020147868eSNuno Antunes }
25030147868eSNuno Antunes }
25040147868eSNuno Antunes
25050147868eSNuno Antunes /* Convert command name to binary */
25060147868eSNuno Antunes binary->header.cmd = c->cmd;
25070147868eSNuno Antunes binary->header.typecookie = c->cookie;
25080147868eSNuno Antunes
25090147868eSNuno Antunes /* Convert command arguments to binary */
25100147868eSNuno Antunes argstype = (binary->header.flags & NGF_RESP) ?
25110147868eSNuno Antunes c->respType : c->mesgType;
25120147868eSNuno Antunes if (argstype == NULL) {
25130147868eSNuno Antunes bufSize = 0;
25140147868eSNuno Antunes } else {
25150147868eSNuno Antunes if ((error = ng_parse(argstype, ascii->data,
25160147868eSNuno Antunes &off, (u_char *)binary->data, &bufSize)) != 0) {
25170147868eSNuno Antunes NG_FREE_MSG(resp);
25180147868eSNuno Antunes break;
25190147868eSNuno Antunes }
25200147868eSNuno Antunes }
25210147868eSNuno Antunes
25220147868eSNuno Antunes /* Return the result */
25230147868eSNuno Antunes binary->header.arglen = bufSize;
25240147868eSNuno Antunes resp->header.arglen = sizeof(*binary) + bufSize;
25250147868eSNuno Antunes break;
25260147868eSNuno Antunes }
25270147868eSNuno Antunes
25280147868eSNuno Antunes case NGM_TEXT_CONFIG:
25290147868eSNuno Antunes case NGM_TEXT_STATUS:
25300147868eSNuno Antunes /*
25310147868eSNuno Antunes * This one is tricky as it passes the command down to the
25320147868eSNuno Antunes * actual node, even though it is a generic type command.
25330147868eSNuno Antunes * This means we must assume that the item/msg is already freed
25340147868eSNuno Antunes * when control passes back to us.
25350147868eSNuno Antunes */
25360147868eSNuno Antunes if (here->nd_type->rcvmsg != NULL) {
25370147868eSNuno Antunes NGI_MSG(item) = msg; /* put it back as we found it */
25380147868eSNuno Antunes return((*here->nd_type->rcvmsg)(here, item, lasthook));
25390147868eSNuno Antunes }
25400147868eSNuno Antunes /* Fall through if rcvmsg not supported */
25410147868eSNuno Antunes default:
25420147868eSNuno Antunes TRAP_ERROR();
25430147868eSNuno Antunes error = EINVAL;
25440147868eSNuno Antunes }
25450147868eSNuno Antunes /*
25460147868eSNuno Antunes * Sometimes a generic message may be statically allocated
25470147868eSNuno Antunes * to avoid problems with allocating when in tight memeory situations.
25480147868eSNuno Antunes * Don't free it if it is so.
25490147868eSNuno Antunes * I break them appart here, because erros may cause a free if the item
25500147868eSNuno Antunes * in which case we'd be doing it twice.
25510147868eSNuno Antunes * they are kept together above, to simplify freeing.
25520147868eSNuno Antunes */
25530147868eSNuno Antunes out:
25540147868eSNuno Antunes NG_RESPOND_MSG(error, here, item, resp);
25550147868eSNuno Antunes if (msg)
25560147868eSNuno Antunes NG_FREE_MSG(msg);
25570147868eSNuno Antunes return (error);
25580147868eSNuno Antunes }
25590147868eSNuno Antunes
25600147868eSNuno Antunes /************************************************************************
25610147868eSNuno Antunes Queue element get/free routines
25620147868eSNuno Antunes ************************************************************************/
25630147868eSNuno Antunes
25642ffe124dSNuno Antunes static struct objcache *ng_oc;
25652ffe124dSNuno Antunes static struct objcache *ng_apply_oc;
25660147868eSNuno Antunes static int maxalloc = 4096; /* limit the damage of a leak */
25670147868eSNuno Antunes
25680147868eSNuno Antunes TUNABLE_INT("net.graph.maxalloc", &maxalloc);
256999334a07SSascha Wildner SYSCTL_INT(_net_graph, OID_AUTO, maxalloc, CTLFLAG_RD, &maxalloc,
25702ffe124dSNuno Antunes 0, "Maximum number of queue items to allocate");
25710147868eSNuno Antunes
25720147868eSNuno Antunes #ifdef NETGRAPH_DEBUG
25730147868eSNuno Antunes static TAILQ_HEAD(, ng_item) ng_itemlist = TAILQ_HEAD_INITIALIZER(ng_itemlist);
25740147868eSNuno Antunes static int allocated; /* number of items malloc'd */
25750147868eSNuno Antunes #endif
25760147868eSNuno Antunes
25770147868eSNuno Antunes /*
25780147868eSNuno Antunes * Get a queue entry.
25790147868eSNuno Antunes * This is usually called when a packet first enters netgraph.
25800147868eSNuno Antunes * By definition, this is usually from an interrupt, or from a user.
25810147868eSNuno Antunes * Users are not so important, but try be quick for the times that it's
25820147868eSNuno Antunes * an interrupt.
25830147868eSNuno Antunes */
25840147868eSNuno Antunes static __inline item_p
ng_alloc_item(int type,int flags)25850147868eSNuno Antunes ng_alloc_item(int type, int flags)
25860147868eSNuno Antunes {
25870147868eSNuno Antunes item_p item;
25880147868eSNuno Antunes
25890147868eSNuno Antunes KASSERT(((type & ~NGQF_TYPE) == 0),
25900147868eSNuno Antunes ("%s: incorrect item type: %d", __func__, type));
25910147868eSNuno Antunes
25922ffe124dSNuno Antunes item = objcache_get(ng_oc,
25930147868eSNuno Antunes (flags & NG_WAITOK) ? M_WAITOK : M_NOWAIT );
25940147868eSNuno Antunes
25950147868eSNuno Antunes if (item) {
25960147868eSNuno Antunes item->el_flags = type;
25970147868eSNuno Antunes #ifdef NETGRAPH_DEBUG
25980147868eSNuno Antunes mtx_lock(&ngq_mtx);
25990147868eSNuno Antunes TAILQ_INSERT_TAIL(&ng_itemlist, item, all);
26000147868eSNuno Antunes allocated++;
26010147868eSNuno Antunes mtx_unlock(&ngq_mtx);
26020147868eSNuno Antunes #endif
26030147868eSNuno Antunes }
26040147868eSNuno Antunes
26050147868eSNuno Antunes return (item);
26060147868eSNuno Antunes }
26070147868eSNuno Antunes
26080147868eSNuno Antunes /*
26090147868eSNuno Antunes * Release a queue entry
26100147868eSNuno Antunes */
26110147868eSNuno Antunes void
ng_free_item(item_p item)26120147868eSNuno Antunes ng_free_item(item_p item)
26130147868eSNuno Antunes {
26140147868eSNuno Antunes /*
2615ad9efc7bSNuno Antunes * If the item still has an apply callback registered, it is
2616ad9efc7bSNuno Antunes * being freed too early.
2617ad9efc7bSNuno Antunes */
2618ad9efc7bSNuno Antunes KASSERT(item->apply == NULL, ("freeing item with registered callback"));
2619ad9efc7bSNuno Antunes
2620ad9efc7bSNuno Antunes /*
2621384d4156SNuno Antunes * Make sure the reference count has reached zero.
2622384d4156SNuno Antunes */
2623384d4156SNuno Antunes KASSERT(item->refs == 0, ("freeing item with non-zero refcount"));
2624384d4156SNuno Antunes
2625384d4156SNuno Antunes /*
26260147868eSNuno Antunes * The item may hold resources on it's own. We need to free
26270147868eSNuno Antunes * these before we can free the item. What they are depends upon
26280147868eSNuno Antunes * what kind of item it is. it is important that nodes zero
26290147868eSNuno Antunes * out pointers to resources that they remove from the item
26300147868eSNuno Antunes * or we release them again here.
26310147868eSNuno Antunes */
26320147868eSNuno Antunes switch (item->el_flags & NGQF_TYPE) {
26330147868eSNuno Antunes case NGQF_DATA:
26340147868eSNuno Antunes /* If we have an mbuf still attached.. */
26350147868eSNuno Antunes NG_FREE_M(_NGI_M(item));
26360147868eSNuno Antunes break;
26370147868eSNuno Antunes case NGQF_MESG:
26380147868eSNuno Antunes _NGI_RETADDR(item) = 0;
26390147868eSNuno Antunes NG_FREE_MSG(_NGI_MSG(item));
26400147868eSNuno Antunes break;
26410147868eSNuno Antunes case NGQF_FN:
26420147868eSNuno Antunes case NGQF_FN2:
26430147868eSNuno Antunes /* nothing to free really, */
26440147868eSNuno Antunes _NGI_FN(item) = NULL;
26450147868eSNuno Antunes _NGI_ARG1(item) = NULL;
26460147868eSNuno Antunes _NGI_ARG2(item) = 0;
26470147868eSNuno Antunes break;
26480147868eSNuno Antunes }
26490147868eSNuno Antunes /* If we still have a node or hook referenced... */
26500147868eSNuno Antunes _NGI_CLR_NODE(item);
26510147868eSNuno Antunes _NGI_CLR_HOOK(item);
26520147868eSNuno Antunes
26530147868eSNuno Antunes #ifdef NETGRAPH_DEBUG
26540147868eSNuno Antunes mtx_lock(&ngq_mtx);
26550147868eSNuno Antunes TAILQ_REMOVE(&ng_itemlist, item, all);
26560147868eSNuno Antunes allocated--;
26570147868eSNuno Antunes mtx_unlock(&ngq_mtx);
26580147868eSNuno Antunes #endif
2659ad9efc7bSNuno Antunes /* Object must be initialized before returning to objcache */
2660ad9efc7bSNuno Antunes bzero(item, sizeof(struct ng_item));
26612ffe124dSNuno Antunes objcache_put(ng_oc, item);
26620147868eSNuno Antunes }
26630147868eSNuno Antunes
26640147868eSNuno Antunes /*
26650147868eSNuno Antunes * Change type of the queue entry.
26660147868eSNuno Antunes */
26670147868eSNuno Antunes static __inline item_p
ng_realloc_item(item_p item,int type,int flags)2668ad9efc7bSNuno Antunes ng_realloc_item(item_p item, int type, int flags)
26690147868eSNuno Antunes {
26700147868eSNuno Antunes
2671ad9efc7bSNuno Antunes KASSERT((item != NULL), ("%s: can't reallocate NULL", __func__));
26720147868eSNuno Antunes KASSERT(((type & ~NGQF_TYPE) == 0),
26730147868eSNuno Antunes ("%s: incorrect item type: %d", __func__, type));
26740147868eSNuno Antunes
26750147868eSNuno Antunes item->el_flags = (item->el_flags & ~NGQF_TYPE) | type;
26760147868eSNuno Antunes
26770147868eSNuno Antunes return (item);
26780147868eSNuno Antunes }
26790147868eSNuno Antunes
2680ad9efc7bSNuno Antunes __inline apply_p
ng_alloc_apply(void)2681ad9efc7bSNuno Antunes ng_alloc_apply(void)
2682ad9efc7bSNuno Antunes {
2683ad9efc7bSNuno Antunes return (objcache_get(ng_apply_oc, M_WAITOK));
2684ad9efc7bSNuno Antunes }
2685ad9efc7bSNuno Antunes
2686ad9efc7bSNuno Antunes __inline void
ng_free_apply(apply_p apply)2687ad9efc7bSNuno Antunes ng_free_apply(apply_p apply)
2688ad9efc7bSNuno Antunes {
2689ad9efc7bSNuno Antunes objcache_put(ng_apply_oc, apply);
2690ad9efc7bSNuno Antunes }
2691ad9efc7bSNuno Antunes
26920147868eSNuno Antunes /************************************************************************
26930147868eSNuno Antunes Module routines
26940147868eSNuno Antunes ************************************************************************/
26950147868eSNuno Antunes
26960147868eSNuno Antunes /*
26970147868eSNuno Antunes * Handle the loading/unloading of a netgraph node type module
26980147868eSNuno Antunes */
26990147868eSNuno Antunes int
ng_mod_event(module_t mod,int event,void * data)27000147868eSNuno Antunes ng_mod_event(module_t mod, int event, void *data)
27010147868eSNuno Antunes {
27020147868eSNuno Antunes struct ng_type *const type = data;
2703d721fb72SNuno Antunes int error = 0;
27040147868eSNuno Antunes
27050147868eSNuno Antunes switch (event) {
27060147868eSNuno Antunes case MOD_LOAD:
27070147868eSNuno Antunes
27080147868eSNuno Antunes /* Register new netgraph node type */
27090147868eSNuno Antunes if ((error = ng_newtype(type)) != 0) {
27100147868eSNuno Antunes break;
27110147868eSNuno Antunes }
27120147868eSNuno Antunes
27130147868eSNuno Antunes /* Call type specific code */
27140147868eSNuno Antunes if (type->mod_event != NULL)
27150147868eSNuno Antunes if ((error = (*type->mod_event)(mod, event, data))) {
27167e5f7144SNuno Antunes TYPELIST_WLOCK();
27170147868eSNuno Antunes type->refs--; /* undo it */
27180147868eSNuno Antunes LIST_REMOVE(type, types);
27197e5f7144SNuno Antunes TYPELIST_WUNLOCK();
27200147868eSNuno Antunes }
27210147868eSNuno Antunes break;
27220147868eSNuno Antunes
27230147868eSNuno Antunes case MOD_UNLOAD:
27240147868eSNuno Antunes if (type->refs > 1) { /* make sure no nodes exist! */
27250147868eSNuno Antunes error = EBUSY;
27260147868eSNuno Antunes } else {
27270147868eSNuno Antunes if (type->refs == 0) {
27280147868eSNuno Antunes /* failed load, nothing to undo */
27290147868eSNuno Antunes break;
27300147868eSNuno Antunes }
27310147868eSNuno Antunes if (type->mod_event != NULL) { /* check with type */
27320147868eSNuno Antunes error = (*type->mod_event)(mod, event, data);
27330147868eSNuno Antunes if (error != 0) { /* type refuses.. */
27340147868eSNuno Antunes break;
27350147868eSNuno Antunes }
27360147868eSNuno Antunes }
27377e5f7144SNuno Antunes TYPELIST_WLOCK();
27380147868eSNuno Antunes LIST_REMOVE(type, types);
27397e5f7144SNuno Antunes TYPELIST_WUNLOCK();
27400147868eSNuno Antunes }
27410147868eSNuno Antunes break;
27420147868eSNuno Antunes
27430147868eSNuno Antunes default:
27440147868eSNuno Antunes if (type->mod_event != NULL)
27450147868eSNuno Antunes error = (*type->mod_event)(mod, event, data);
27460147868eSNuno Antunes else
27470147868eSNuno Antunes error = EOPNOTSUPP; /* XXX ? */
27480147868eSNuno Antunes break;
27490147868eSNuno Antunes }
27500147868eSNuno Antunes return (error);
27510147868eSNuno Antunes }
27520147868eSNuno Antunes
27530147868eSNuno Antunes /*
27540147868eSNuno Antunes * Handle loading and unloading for this code.
27550147868eSNuno Antunes */
27560147868eSNuno Antunes static int
ngb_mod_event(module_t mod,int event,void * data)27570147868eSNuno Antunes ngb_mod_event(module_t mod, int event, void *data)
27580147868eSNuno Antunes {
2759ad9efc7bSNuno Antunes int i, error = 0;
27600147868eSNuno Antunes
27610147868eSNuno Antunes switch (event) {
27620147868eSNuno Antunes case MOD_LOAD:
27630147868eSNuno Antunes /* Initialize everything. */
27647e5f7144SNuno Antunes lwkt_token_init(&ng_typelist_token, "ng typelist");
27650560a357SNuno Antunes lwkt_token_init(&ng_idhash_token, "ng idhash");
27660560a357SNuno Antunes lwkt_token_init(&ng_namehash_token, "ng namehash");
2767dce7062eSNuno Antunes lwkt_token_init(&ng_topo_token, "ng topology");
27680147868eSNuno Antunes #ifdef NETGRAPH_DEBUG
2769c0095626SSascha Wildner mtx_init(&ng_nodelist_mtx, "ng nodelist");
2770c0095626SSascha Wildner mtx_init(&ngq_mtx, "ng queue");
27710147868eSNuno Antunes #endif
27722ffe124dSNuno Antunes ng_oc = objcache_create_mbacked(M_NETGRAPH,
27732ffe124dSNuno Antunes sizeof(struct ng_item), maxalloc, 0, bzero_ctor,
27742ffe124dSNuno Antunes NULL, NULL);
2775ad9efc7bSNuno Antunes ng_apply_oc = objcache_create_mbacked(M_NETGRAPH_APPLY,
2776aba65d57SNuno Antunes sizeof(struct ng_apply_info), 0, 0, bzero_ctor,
2777aba65d57SNuno Antunes NULL, NULL);
27780560a357SNuno Antunes
27790560a357SNuno Antunes /* We start with small hashes, but they can grow. */
27800560a357SNuno Antunes ng_ID_hash = hashinit(16, M_NETGRAPH_NODE, &ng_ID_hmask);
27810560a357SNuno Antunes ng_name_hash = hashinit(16, M_NETGRAPH_NODE, &ng_name_hmask);
27820560a357SNuno Antunes
27830560a357SNuno Antunes lwkt_initport_panic(&ng_panic_reply_port);
27840560a357SNuno Antunes for (i = 0; i < ncpus; ++i) {
27850560a357SNuno Antunes thread_t td;
27860560a357SNuno Antunes
27870560a357SNuno Antunes lwkt_create(ngthread, NULL, &td,
27880560a357SNuno Antunes NULL, 0, i, "netgraph %d", i);
27890560a357SNuno Antunes ng_msgport[i] = &td->td_msgport;
27900560a357SNuno Antunes }
27910147868eSNuno Antunes break;
27920147868eSNuno Antunes case MOD_UNLOAD:
2793ad9efc7bSNuno Antunes #if 0
27940560a357SNuno Antunes hashdestroy(V_ng_name_hash, M_NETGRAPH_NODE, V_ng_name_hmask);
27950560a357SNuno Antunes hashdestroy(V_ng_ID_hash, M_NETGRAPH_NODE, V_ng_ID_hmask);
27960560a357SNuno Antunes
2797ad9efc7bSNuno Antunes /* Destroy the lwkt threads too */
27980560a357SNuno Antunes
2799ad9efc7bSNuno Antunes objcache_destroy(ng_apply_oc);
2800aba65d57SNuno Antunes objcache_destroy(ng_oc);
2801ad9efc7bSNuno Antunes #endif
28020147868eSNuno Antunes /* You can't unload it because an interface may be using it. */
28030147868eSNuno Antunes error = EBUSY;
28040147868eSNuno Antunes break;
28050147868eSNuno Antunes default:
28060147868eSNuno Antunes error = EOPNOTSUPP;
28070147868eSNuno Antunes break;
28080147868eSNuno Antunes }
28090147868eSNuno Antunes return (error);
28100147868eSNuno Antunes }
28110147868eSNuno Antunes
28120147868eSNuno Antunes static moduledata_t netgraph_mod = {
28130147868eSNuno Antunes "netgraph",
28140147868eSNuno Antunes ngb_mod_event,
28150147868eSNuno Antunes (NULL)
28160147868eSNuno Antunes };
28170147868eSNuno Antunes DECLARE_MODULE(netgraph, netgraph_mod, SI_SUB_NETGRAPH, SI_ORDER_MIDDLE);
28180147868eSNuno Antunes SYSCTL_NODE(_net, OID_AUTO, graph, CTLFLAG_RW, 0, "netgraph Family");
28190147868eSNuno Antunes SYSCTL_INT(_net_graph, OID_AUTO, abi_version, CTLFLAG_RD, 0, NG_ABI_VERSION,"");
28200147868eSNuno Antunes SYSCTL_INT(_net_graph, OID_AUTO, msg_version, CTLFLAG_RD, 0, NG_VERSION, "");
28210147868eSNuno Antunes
28220147868eSNuno Antunes #ifdef NETGRAPH_DEBUG
28230147868eSNuno Antunes void
dumphook(hook_p hook,char * file,int line)28240147868eSNuno Antunes dumphook (hook_p hook, char *file, int line)
28250147868eSNuno Antunes {
2826a62226e4SSascha Wildner kprintf("hook: name %s, %d refs, Last touched:\n",
28270147868eSNuno Antunes _NG_HOOK_NAME(hook), hook->hk_refs);
2828a62226e4SSascha Wildner kprintf(" Last active @ %s, line %d\n",
28290147868eSNuno Antunes hook->lastfile, hook->lastline);
28300147868eSNuno Antunes if (line) {
2831a62226e4SSascha Wildner kprintf(" problem discovered at file %s, line %d\n", file, line);
28320147868eSNuno Antunes }
28330147868eSNuno Antunes }
28340147868eSNuno Antunes
28350147868eSNuno Antunes void
dumpnode(node_p node,char * file,int line)28360147868eSNuno Antunes dumpnode(node_p node, char *file, int line)
28370147868eSNuno Antunes {
2838a62226e4SSascha Wildner kprintf("node: ID [%x]: type '%s', %d hooks, flags 0x%x, %d refs, %s:\n",
28390147868eSNuno Antunes _NG_NODE_ID(node), node->nd_type->name,
28400147868eSNuno Antunes node->nd_numhooks, node->nd_flags,
28410147868eSNuno Antunes node->nd_refs, node->nd_name);
2842a62226e4SSascha Wildner kprintf(" Last active @ %s, line %d\n",
28430147868eSNuno Antunes node->lastfile, node->lastline);
28440147868eSNuno Antunes if (line) {
2845a62226e4SSascha Wildner kprintf(" problem discovered at file %s, line %d\n", file, line);
28460147868eSNuno Antunes }
28470147868eSNuno Antunes }
28480147868eSNuno Antunes
28490147868eSNuno Antunes void
dumpitem(item_p item,char * file,int line)28500147868eSNuno Antunes dumpitem(item_p item, char *file, int line)
28510147868eSNuno Antunes {
2852a62226e4SSascha Wildner kprintf(" ACTIVE item, last used at %s, line %d",
28530147868eSNuno Antunes item->lastfile, item->lastline);
28540147868eSNuno Antunes switch(item->el_flags & NGQF_TYPE) {
28550147868eSNuno Antunes case NGQF_DATA:
2856a62226e4SSascha Wildner kprintf(" - [data]\n");
28570147868eSNuno Antunes break;
28580147868eSNuno Antunes case NGQF_MESG:
2859a62226e4SSascha Wildner kprintf(" - retaddr[%d]:\n", _NGI_RETADDR(item));
28600147868eSNuno Antunes break;
28610147868eSNuno Antunes case NGQF_FN:
2862a62226e4SSascha Wildner kprintf(" - fn@%p (%p, %p, %p, %d (%x))\n",
28630147868eSNuno Antunes _NGI_FN(item),
28640147868eSNuno Antunes _NGI_NODE(item),
28650147868eSNuno Antunes _NGI_HOOK(item),
28660147868eSNuno Antunes item->body.fn.fn_arg1,
28670147868eSNuno Antunes item->body.fn.fn_arg2,
28680147868eSNuno Antunes item->body.fn.fn_arg2);
28690147868eSNuno Antunes break;
28700147868eSNuno Antunes case NGQF_FN2:
2871a62226e4SSascha Wildner kprintf(" - fn2@%p (%p, %p, %p, %d (%x))\n",
28720147868eSNuno Antunes _NGI_FN2(item),
28730147868eSNuno Antunes _NGI_NODE(item),
28740147868eSNuno Antunes _NGI_HOOK(item),
28750147868eSNuno Antunes item->body.fn.fn_arg1,
28760147868eSNuno Antunes item->body.fn.fn_arg2,
28770147868eSNuno Antunes item->body.fn.fn_arg2);
28780147868eSNuno Antunes break;
28790147868eSNuno Antunes }
28800147868eSNuno Antunes if (line) {
2881a62226e4SSascha Wildner kprintf(" problem discovered at file %s, line %d\n", file, line);
28820147868eSNuno Antunes if (_NGI_NODE(item)) {
2883a62226e4SSascha Wildner kprintf("node %p ([%x])\n",
28840147868eSNuno Antunes _NGI_NODE(item), ng_node2ID(_NGI_NODE(item)));
28850147868eSNuno Antunes }
28860147868eSNuno Antunes }
28870147868eSNuno Antunes }
28880147868eSNuno Antunes
28890147868eSNuno Antunes static void
ng_dumpitems(void)28900147868eSNuno Antunes ng_dumpitems(void)
28910147868eSNuno Antunes {
28920147868eSNuno Antunes item_p item;
28930147868eSNuno Antunes int i = 1;
28940147868eSNuno Antunes TAILQ_FOREACH(item, &ng_itemlist, all) {
2895a62226e4SSascha Wildner kprintf("[%d] ", i++);
28960147868eSNuno Antunes dumpitem(item, NULL, 0);
28970147868eSNuno Antunes }
28980147868eSNuno Antunes }
28990147868eSNuno Antunes
29000147868eSNuno Antunes static void
ng_dumpnodes(void)29010147868eSNuno Antunes ng_dumpnodes(void)
29020147868eSNuno Antunes {
29030147868eSNuno Antunes node_p node;
29040147868eSNuno Antunes int i = 1;
29050147868eSNuno Antunes mtx_lock(&ng_nodelist_mtx);
29060147868eSNuno Antunes SLIST_FOREACH(node, &ng_allnodes, nd_all) {
2907a62226e4SSascha Wildner kprintf("[%d] ", i++);
29080147868eSNuno Antunes dumpnode(node, NULL, 0);
29090147868eSNuno Antunes }
29100147868eSNuno Antunes mtx_unlock(&ng_nodelist_mtx);
29110147868eSNuno Antunes }
29120147868eSNuno Antunes
29130147868eSNuno Antunes static void
ng_dumphooks(void)29140147868eSNuno Antunes ng_dumphooks(void)
29150147868eSNuno Antunes {
29160147868eSNuno Antunes hook_p hook;
29170147868eSNuno Antunes int i = 1;
29180147868eSNuno Antunes mtx_lock(&ng_nodelist_mtx);
29190147868eSNuno Antunes SLIST_FOREACH(hook, &ng_allhooks, hk_all) {
2920a62226e4SSascha Wildner kprintf("[%d] ", i++);
29210147868eSNuno Antunes dumphook(hook, NULL, 0);
29220147868eSNuno Antunes }
29230147868eSNuno Antunes mtx_unlock(&ng_nodelist_mtx);
29240147868eSNuno Antunes }
29250147868eSNuno Antunes
29260147868eSNuno Antunes static int
sysctl_debug_ng_dump_items(SYSCTL_HANDLER_ARGS)29270147868eSNuno Antunes sysctl_debug_ng_dump_items(SYSCTL_HANDLER_ARGS)
29280147868eSNuno Antunes {
29290147868eSNuno Antunes int error;
29300147868eSNuno Antunes int val;
29310147868eSNuno Antunes
29320147868eSNuno Antunes val = allocated;
29330147868eSNuno Antunes error = sysctl_handle_int(oidp, &val, 0, req);
29340147868eSNuno Antunes if (error != 0 || req->newptr == NULL)
29350147868eSNuno Antunes return (error);
29360147868eSNuno Antunes if (val == 42) {
29370147868eSNuno Antunes ng_dumpitems();
29380147868eSNuno Antunes ng_dumpnodes();
29390147868eSNuno Antunes ng_dumphooks();
29400147868eSNuno Antunes }
29410147868eSNuno Antunes return (0);
29420147868eSNuno Antunes }
29430147868eSNuno Antunes
29440147868eSNuno Antunes SYSCTL_PROC(_debug, OID_AUTO, ng_dump_items, CTLTYPE_INT | CTLFLAG_RW,
29450147868eSNuno Antunes 0, sizeof(int), sysctl_debug_ng_dump_items, "I", "Number of allocated items");
29460147868eSNuno Antunes #endif /* NETGRAPH_DEBUG */
29470147868eSNuno Antunes
29480147868eSNuno Antunes
29490147868eSNuno Antunes /***********************************************************************
29500147868eSNuno Antunes * Externally useable functions to set up a queue item ready for sending
29510147868eSNuno Antunes ***********************************************************************/
29520147868eSNuno Antunes
29530147868eSNuno Antunes #ifdef NETGRAPH_DEBUG
29540147868eSNuno Antunes #define ITEM_DEBUG_CHECKS \
29550147868eSNuno Antunes do { \
29560147868eSNuno Antunes if (NGI_NODE(item) ) { \
2957a62226e4SSascha Wildner kprintf("item already has node"); \
29580147868eSNuno Antunes kdb_enter(KDB_WHY_NETGRAPH, "has node"); \
29590147868eSNuno Antunes NGI_CLR_NODE(item); \
29600147868eSNuno Antunes } \
29610147868eSNuno Antunes if (NGI_HOOK(item) ) { \
2962a62226e4SSascha Wildner kprintf("item already has hook"); \
29630147868eSNuno Antunes kdb_enter(KDB_WHY_NETGRAPH, "has hook"); \
29640147868eSNuno Antunes NGI_CLR_HOOK(item); \
29650147868eSNuno Antunes } \
29660147868eSNuno Antunes } while (0)
29670147868eSNuno Antunes #else
29680147868eSNuno Antunes #define ITEM_DEBUG_CHECKS
29690147868eSNuno Antunes #endif
29700147868eSNuno Antunes
29710147868eSNuno Antunes /*
29720147868eSNuno Antunes * Put mbuf into the item.
29730147868eSNuno Antunes * Hook and node references will be removed when the item is dequeued.
29740147868eSNuno Antunes * (or equivalent)
29750147868eSNuno Antunes * (XXX) Unsafe because no reference held by peer on remote node.
29760147868eSNuno Antunes * remote node might go away in this timescale.
29770147868eSNuno Antunes * We know the hooks can't go away because that would require getting
29780147868eSNuno Antunes * a writer item on both nodes and we must have at least a reader
29790147868eSNuno Antunes * here to be able to do this.
29800147868eSNuno Antunes * Note that the hook loaded is the REMOTE hook.
29810147868eSNuno Antunes *
29820147868eSNuno Antunes * This is possibly in the critical path for new data.
29830147868eSNuno Antunes */
29840147868eSNuno Antunes item_p
ng_package_data(struct mbuf * m,int flags)29850147868eSNuno Antunes ng_package_data(struct mbuf *m, int flags)
29860147868eSNuno Antunes {
29870147868eSNuno Antunes item_p item;
29880147868eSNuno Antunes
29890147868eSNuno Antunes if ((item = ng_alloc_item(NGQF_DATA, flags)) == NULL) {
29900147868eSNuno Antunes NG_FREE_M(m);
29910147868eSNuno Antunes return (NULL);
29920147868eSNuno Antunes }
29930147868eSNuno Antunes ITEM_DEBUG_CHECKS;
29940147868eSNuno Antunes item->el_flags |= NGQF_READER;
29950147868eSNuno Antunes NGI_M(item) = m;
29960147868eSNuno Antunes return (item);
29970147868eSNuno Antunes }
29980147868eSNuno Antunes
29990147868eSNuno Antunes /*
30000147868eSNuno Antunes * Allocate a queue item and put items into it..
30010147868eSNuno Antunes * Evaluate the address as this will be needed to queue it and
30020147868eSNuno Antunes * to work out what some of the fields should be.
30030147868eSNuno Antunes * Hook and node references will be removed when the item is dequeued.
30040147868eSNuno Antunes * (or equivalent)
30050147868eSNuno Antunes */
30060147868eSNuno Antunes item_p
ng_package_msg(struct ng_mesg * msg,int flags)30070147868eSNuno Antunes ng_package_msg(struct ng_mesg *msg, int flags)
30080147868eSNuno Antunes {
30090147868eSNuno Antunes item_p item;
30100147868eSNuno Antunes
30110147868eSNuno Antunes if ((item = ng_alloc_item(NGQF_MESG, flags)) == NULL) {
30120147868eSNuno Antunes NG_FREE_MSG(msg);
30130147868eSNuno Antunes return (NULL);
30140147868eSNuno Antunes }
30150147868eSNuno Antunes ITEM_DEBUG_CHECKS;
30160147868eSNuno Antunes /* Messages items count as writers unless explicitly exempted. */
30170147868eSNuno Antunes if (msg->header.cmd & NGM_READONLY)
30180147868eSNuno Antunes item->el_flags |= NGQF_READER;
30190147868eSNuno Antunes else
30200147868eSNuno Antunes item->el_flags |= NGQF_WRITER;
30210147868eSNuno Antunes /*
30220147868eSNuno Antunes * Set the current lasthook into the queue item
30230147868eSNuno Antunes */
30240147868eSNuno Antunes NGI_MSG(item) = msg;
30250147868eSNuno Antunes NGI_RETADDR(item) = 0;
30260147868eSNuno Antunes return (item);
30270147868eSNuno Antunes }
30280147868eSNuno Antunes
30290147868eSNuno Antunes
30300147868eSNuno Antunes
30310147868eSNuno Antunes #define SET_RETADDR(item, here, retaddr) \
30320147868eSNuno Antunes do { /* Data or fn items don't have retaddrs */ \
30330147868eSNuno Antunes if ((item->el_flags & NGQF_TYPE) == NGQF_MESG) { \
30340147868eSNuno Antunes if (retaddr) { \
30350147868eSNuno Antunes NGI_RETADDR(item) = retaddr; \
30360147868eSNuno Antunes } else { \
30370147868eSNuno Antunes /* \
30380147868eSNuno Antunes * The old return address should be ok. \
30390147868eSNuno Antunes * If there isn't one, use the address \
30400147868eSNuno Antunes * here. \
30410147868eSNuno Antunes */ \
30420147868eSNuno Antunes if (NGI_RETADDR(item) == 0) { \
30430147868eSNuno Antunes NGI_RETADDR(item) \
30440147868eSNuno Antunes = ng_node2ID(here); \
30450147868eSNuno Antunes } \
30460147868eSNuno Antunes } \
30470147868eSNuno Antunes } \
30480147868eSNuno Antunes } while (0)
30490147868eSNuno Antunes
30500147868eSNuno Antunes int
ng_address_hook(node_p here,item_p item,hook_p hook,ng_ID_t retaddr)30510147868eSNuno Antunes ng_address_hook(node_p here, item_p item, hook_p hook, ng_ID_t retaddr)
30520147868eSNuno Antunes {
30530147868eSNuno Antunes hook_p peer;
30540147868eSNuno Antunes node_p peernode;
30550147868eSNuno Antunes ITEM_DEBUG_CHECKS;
30560147868eSNuno Antunes /*
30570147868eSNuno Antunes * Quick sanity check..
30580147868eSNuno Antunes * Since a hook holds a reference on it's node, once we know
30590147868eSNuno Antunes * that the peer is still connected (even if invalid,) we know
30600147868eSNuno Antunes * that the peer node is present, though maybe invalid.
30610147868eSNuno Antunes */
30620147868eSNuno Antunes if ((hook == NULL) ||
30630147868eSNuno Antunes NG_HOOK_NOT_VALID(hook) ||
30640147868eSNuno Antunes NG_HOOK_NOT_VALID(peer = NG_HOOK_PEER(hook)) ||
30650147868eSNuno Antunes NG_NODE_NOT_VALID(peernode = NG_PEER_NODE(hook))) {
30660147868eSNuno Antunes NG_FREE_ITEM(item);
30670147868eSNuno Antunes TRAP_ERROR();
30680147868eSNuno Antunes return (ENETDOWN);
30690147868eSNuno Antunes }
30700147868eSNuno Antunes
30710147868eSNuno Antunes /*
30720147868eSNuno Antunes * Transfer our interest to the other (peer) end.
30730147868eSNuno Antunes */
30740147868eSNuno Antunes NG_HOOK_REF(peer);
30750147868eSNuno Antunes NG_NODE_REF(peernode);
30760147868eSNuno Antunes NGI_SET_HOOK(item, peer);
30770147868eSNuno Antunes NGI_SET_NODE(item, peernode);
30780147868eSNuno Antunes SET_RETADDR(item, here, retaddr);
30790147868eSNuno Antunes return (0);
30800147868eSNuno Antunes }
30810147868eSNuno Antunes
30820147868eSNuno Antunes int
ng_address_path(node_p here,item_p item,char * address,ng_ID_t retaddr)30830147868eSNuno Antunes ng_address_path(node_p here, item_p item, char *address, ng_ID_t retaddr)
30840147868eSNuno Antunes {
30850147868eSNuno Antunes node_p dest = NULL;
30860147868eSNuno Antunes hook_p hook = NULL;
30870147868eSNuno Antunes int error;
30880147868eSNuno Antunes
30890147868eSNuno Antunes ITEM_DEBUG_CHECKS;
30900147868eSNuno Antunes /*
30910147868eSNuno Antunes * Note that ng_path2noderef increments the reference count
30920147868eSNuno Antunes * on the node for us if it finds one. So we don't have to.
30930147868eSNuno Antunes */
30940147868eSNuno Antunes error = ng_path2noderef(here, address, &dest, &hook);
30950147868eSNuno Antunes if (error) {
30960147868eSNuno Antunes NG_FREE_ITEM(item);
30970147868eSNuno Antunes return (error);
30980147868eSNuno Antunes }
30990147868eSNuno Antunes NGI_SET_NODE(item, dest);
31000147868eSNuno Antunes if ( hook) {
31010147868eSNuno Antunes NG_HOOK_REF(hook); /* don't let it go while on the queue */
31020147868eSNuno Antunes NGI_SET_HOOK(item, hook);
31030147868eSNuno Antunes }
31040147868eSNuno Antunes SET_RETADDR(item, here, retaddr);
31050147868eSNuno Antunes return (0);
31060147868eSNuno Antunes }
31070147868eSNuno Antunes
31080147868eSNuno Antunes int
ng_address_ID(node_p here,item_p item,ng_ID_t ID,ng_ID_t retaddr)31090147868eSNuno Antunes ng_address_ID(node_p here, item_p item, ng_ID_t ID, ng_ID_t retaddr)
31100147868eSNuno Antunes {
31110147868eSNuno Antunes node_p dest;
31120147868eSNuno Antunes
31130147868eSNuno Antunes ITEM_DEBUG_CHECKS;
31140147868eSNuno Antunes /*
31150147868eSNuno Antunes * Find the target node.
31160147868eSNuno Antunes */
31170147868eSNuno Antunes dest = ng_ID2noderef(ID); /* GETS REFERENCE! */
31180147868eSNuno Antunes if (dest == NULL) {
31190147868eSNuno Antunes NG_FREE_ITEM(item);
31200147868eSNuno Antunes TRAP_ERROR();
31210147868eSNuno Antunes return(EINVAL);
31220147868eSNuno Antunes }
31230147868eSNuno Antunes /* Fill out the contents */
31240147868eSNuno Antunes NGI_SET_NODE(item, dest);
31250147868eSNuno Antunes NGI_CLR_HOOK(item);
31260147868eSNuno Antunes SET_RETADDR(item, here, retaddr);
31270147868eSNuno Antunes return (0);
31280147868eSNuno Antunes }
31290147868eSNuno Antunes
31300147868eSNuno Antunes /*
31310147868eSNuno Antunes * special case to send a message to self (e.g. destroy node)
31320147868eSNuno Antunes * Possibly indicate an arrival hook too.
31330147868eSNuno Antunes * Useful for removing that hook :-)
31340147868eSNuno Antunes */
31350147868eSNuno Antunes item_p
ng_package_msg_self(node_p here,hook_p hook,struct ng_mesg * msg)31360147868eSNuno Antunes ng_package_msg_self(node_p here, hook_p hook, struct ng_mesg *msg)
31370147868eSNuno Antunes {
31380147868eSNuno Antunes item_p item;
31390147868eSNuno Antunes
31400147868eSNuno Antunes /*
31410147868eSNuno Antunes * Find the target node.
31420147868eSNuno Antunes * If there is a HOOK argument, then use that in preference
31430147868eSNuno Antunes * to the address.
31440147868eSNuno Antunes */
31450147868eSNuno Antunes if ((item = ng_alloc_item(NGQF_MESG, NG_NOFLAGS)) == NULL) {
31460147868eSNuno Antunes NG_FREE_MSG(msg);
31470147868eSNuno Antunes return (NULL);
31480147868eSNuno Antunes }
31490147868eSNuno Antunes
31500147868eSNuno Antunes /* Fill out the contents */
31510147868eSNuno Antunes item->el_flags |= NGQF_WRITER;
31520147868eSNuno Antunes NG_NODE_REF(here);
31530147868eSNuno Antunes NGI_SET_NODE(item, here);
31540147868eSNuno Antunes if (hook) {
31550147868eSNuno Antunes NG_HOOK_REF(hook);
31560147868eSNuno Antunes NGI_SET_HOOK(item, hook);
31570147868eSNuno Antunes }
31580147868eSNuno Antunes NGI_MSG(item) = msg;
31590147868eSNuno Antunes NGI_RETADDR(item) = ng_node2ID(here);
31600147868eSNuno Antunes return (item);
31610147868eSNuno Antunes }
31620147868eSNuno Antunes
31630147868eSNuno Antunes /*
31640147868eSNuno Antunes * Send ng_item_fn function call to the specified node.
31650147868eSNuno Antunes */
31660147868eSNuno Antunes
31670147868eSNuno Antunes int
ng_send_fn(node_p node,hook_p hook,ng_item_fn * fn,void * arg1,int arg2)31680147868eSNuno Antunes ng_send_fn(node_p node, hook_p hook, ng_item_fn *fn, void * arg1, int arg2)
31690147868eSNuno Antunes {
31700147868eSNuno Antunes return ng_send_fn1(node, hook, fn, arg1, arg2, NG_NOFLAGS);
31710147868eSNuno Antunes }
31720147868eSNuno Antunes
31730147868eSNuno Antunes int
ng_send_fn1(node_p node,hook_p hook,ng_item_fn * fn,void * arg1,int arg2,int flags)31740147868eSNuno Antunes ng_send_fn1(node_p node, hook_p hook, ng_item_fn *fn, void * arg1, int arg2,
31750147868eSNuno Antunes int flags)
31760147868eSNuno Antunes {
31770147868eSNuno Antunes item_p item;
31780147868eSNuno Antunes
31790147868eSNuno Antunes if ((item = ng_alloc_item(NGQF_FN, flags)) == NULL) {
31800147868eSNuno Antunes return (ENOMEM);
31810147868eSNuno Antunes }
31820147868eSNuno Antunes item->el_flags |= NGQF_WRITER;
31830147868eSNuno Antunes NG_NODE_REF(node); /* and one for the item */
31840147868eSNuno Antunes NGI_SET_NODE(item, node);
31850147868eSNuno Antunes if (hook) {
31860147868eSNuno Antunes NG_HOOK_REF(hook);
31870147868eSNuno Antunes NGI_SET_HOOK(item, hook);
31880147868eSNuno Antunes }
31890147868eSNuno Antunes NGI_FN(item) = fn;
31900147868eSNuno Antunes NGI_ARG1(item) = arg1;
31910147868eSNuno Antunes NGI_ARG2(item) = arg2;
31920147868eSNuno Antunes return(ng_snd_item(item, flags));
31930147868eSNuno Antunes }
31940147868eSNuno Antunes
31950147868eSNuno Antunes /*
31960147868eSNuno Antunes * Send ng_item_fn2 function call to the specified node.
31970147868eSNuno Antunes *
3198ad9efc7bSNuno Antunes * If NG_REUSE_ITEM flag is set, no new item will be allocated,
3199ad9efc7bSNuno Antunes * pitem will be used instead.
32000147868eSNuno Antunes */
32010147868eSNuno Antunes int
ng_send_fn2(node_p node,hook_p hook,item_p pitem,ng_item_fn2 * fn,void * arg1,int arg2,int flags)32020147868eSNuno Antunes ng_send_fn2(node_p node, hook_p hook, item_p pitem, ng_item_fn2 *fn, void *arg1,
32030147868eSNuno Antunes int arg2, int flags)
32040147868eSNuno Antunes {
32050147868eSNuno Antunes item_p item;
32060147868eSNuno Antunes
32070147868eSNuno Antunes KASSERT((pitem != NULL || (flags & NG_REUSE_ITEM) == 0),
32080147868eSNuno Antunes ("%s: NG_REUSE_ITEM but no pitem", __func__));
32090147868eSNuno Antunes
32100147868eSNuno Antunes /*
32110147868eSNuno Antunes * Allocate a new item if no supplied or
32120147868eSNuno Antunes * if we can't use supplied one.
32130147868eSNuno Antunes */
32140147868eSNuno Antunes if (pitem == NULL || (flags & NG_REUSE_ITEM) == 0) {
32150147868eSNuno Antunes if ((item = ng_alloc_item(NGQF_FN2, flags)) == NULL)
32160147868eSNuno Antunes return (ENOMEM);
32170147868eSNuno Antunes } else {
32180147868eSNuno Antunes if ((item = ng_realloc_item(pitem, NGQF_FN2, flags)) == NULL)
32190147868eSNuno Antunes return (ENOMEM);
32200147868eSNuno Antunes }
32210147868eSNuno Antunes
32220147868eSNuno Antunes item->el_flags = (item->el_flags & ~NGQF_RW) | NGQF_WRITER;
32230147868eSNuno Antunes NG_NODE_REF(node); /* and one for the item */
32240147868eSNuno Antunes NGI_SET_NODE(item, node);
32250147868eSNuno Antunes if (hook) {
32260147868eSNuno Antunes NG_HOOK_REF(hook);
32270147868eSNuno Antunes NGI_SET_HOOK(item, hook);
32280147868eSNuno Antunes }
32290147868eSNuno Antunes NGI_FN2(item) = fn;
32300147868eSNuno Antunes NGI_ARG1(item) = arg1;
32310147868eSNuno Antunes NGI_ARG2(item) = arg2;
32320147868eSNuno Antunes return(ng_snd_item(item, flags));
32330147868eSNuno Antunes }
32340147868eSNuno Antunes
32350147868eSNuno Antunes /*
32360147868eSNuno Antunes * Official timeout routines for Netgraph nodes.
32370147868eSNuno Antunes */
32380147868eSNuno Antunes static void
ng_callout_trampoline(void * arg)32390147868eSNuno Antunes ng_callout_trampoline(void *arg)
32400147868eSNuno Antunes {
32410147868eSNuno Antunes item_p item = arg;
32420147868eSNuno Antunes
32430147868eSNuno Antunes ng_snd_item(item, 0);
32440147868eSNuno Antunes }
32450147868eSNuno Antunes
32460147868eSNuno Antunes
32470147868eSNuno Antunes int
ng_callout(struct callout * c,node_p node,hook_p hook,int ticks,ng_item_fn * fn,void * arg1,int arg2)32480147868eSNuno Antunes ng_callout(struct callout *c, node_p node, hook_p hook, int ticks,
32490147868eSNuno Antunes ng_item_fn *fn, void * arg1, int arg2)
32500147868eSNuno Antunes {
32510147868eSNuno Antunes item_p item, oitem;
32520147868eSNuno Antunes
32530147868eSNuno Antunes if ((item = ng_alloc_item(NGQF_FN, NG_NOFLAGS)) == NULL)
32540147868eSNuno Antunes return (ENOMEM);
32550147868eSNuno Antunes
32560147868eSNuno Antunes item->el_flags |= NGQF_WRITER;
32570147868eSNuno Antunes NG_NODE_REF(node); /* and one for the item */
32580147868eSNuno Antunes NGI_SET_NODE(item, node);
32590147868eSNuno Antunes if (hook) {
32600147868eSNuno Antunes NG_HOOK_REF(hook);
32610147868eSNuno Antunes NGI_SET_HOOK(item, hook);
32620147868eSNuno Antunes }
32630147868eSNuno Antunes NGI_FN(item) = fn;
32640147868eSNuno Antunes NGI_ARG1(item) = arg1;
32650147868eSNuno Antunes NGI_ARG2(item) = arg2;
3266*79dee9e9SSascha Wildner oitem = callout_arg(c);
32670147868eSNuno Antunes callout_reset(c, ticks, &ng_callout_trampoline, item);
32680147868eSNuno Antunes return (0);
32690147868eSNuno Antunes }
32700147868eSNuno Antunes
32710147868eSNuno Antunes /* A special modified version of untimeout() */
32720147868eSNuno Antunes int
ng_uncallout(struct callout * c,node_p node)32730147868eSNuno Antunes ng_uncallout(struct callout *c, node_p node)
32740147868eSNuno Antunes {
32750147868eSNuno Antunes item_p item;
32760147868eSNuno Antunes int rval;
32770147868eSNuno Antunes
32780147868eSNuno Antunes KASSERT(c != NULL, ("ng_uncallout: NULL callout"));
32790147868eSNuno Antunes KASSERT(node != NULL, ("ng_uncallout: NULL node"));
32800147868eSNuno Antunes
32810147868eSNuno Antunes rval = callout_stop(c);
3282*79dee9e9SSascha Wildner item = callout_arg(c);
32830147868eSNuno Antunes /* Do an extra check */
3284*79dee9e9SSascha Wildner if ((rval > 0) && (callout_func(c) == &ng_callout_trampoline) &&
32850147868eSNuno Antunes (NGI_NODE(item) == node)) {
32860147868eSNuno Antunes /*
32870147868eSNuno Antunes * We successfully removed it from the queue before it ran
32880147868eSNuno Antunes * So now we need to unreference everything that was
32890147868eSNuno Antunes * given extra references. (NG_FREE_ITEM does this).
32900147868eSNuno Antunes */
32910147868eSNuno Antunes NG_FREE_ITEM(item);
32920147868eSNuno Antunes }
3293*79dee9e9SSascha Wildner callout_set_arg(c, NULL);
32940147868eSNuno Antunes
32950147868eSNuno Antunes return (rval);
32960147868eSNuno Antunes }
32970147868eSNuno Antunes
32980147868eSNuno Antunes /*
32990147868eSNuno Antunes * Set the address, if none given, give the node here.
33000147868eSNuno Antunes */
33010147868eSNuno Antunes void
ng_replace_retaddr(node_p here,item_p item,ng_ID_t retaddr)33020147868eSNuno Antunes ng_replace_retaddr(node_p here, item_p item, ng_ID_t retaddr)
33030147868eSNuno Antunes {
33040147868eSNuno Antunes if (retaddr) {
33050147868eSNuno Antunes NGI_RETADDR(item) = retaddr;
33060147868eSNuno Antunes } else {
33070147868eSNuno Antunes /*
33080147868eSNuno Antunes * The old return address should be ok.
33090147868eSNuno Antunes * If there isn't one, use the address here.
33100147868eSNuno Antunes */
33110147868eSNuno Antunes NGI_RETADDR(item) = ng_node2ID(here);
33120147868eSNuno Antunes }
33130147868eSNuno Antunes }
33140147868eSNuno Antunes
33150147868eSNuno Antunes static boolean_t
bzero_ctor(void * obj,void * private,int ocflags)33160147868eSNuno Antunes bzero_ctor(void *obj, void *private, int ocflags)
33170147868eSNuno Antunes {
33180147868eSNuno Antunes struct ng_item *i = obj;
33190147868eSNuno Antunes
33200147868eSNuno Antunes bzero(i, sizeof(struct ng_item));
33210147868eSNuno Antunes return(TRUE);
33220147868eSNuno Antunes }
33230147868eSNuno Antunes
33240147868eSNuno Antunes #define TESTING
33250147868eSNuno Antunes #ifdef TESTING
33260147868eSNuno Antunes /* just test all the macros */
33270147868eSNuno Antunes void
33280147868eSNuno Antunes ng_macro_test(item_p item);
33290147868eSNuno Antunes void
ng_macro_test(item_p item)33300147868eSNuno Antunes ng_macro_test(item_p item)
33310147868eSNuno Antunes {
33320147868eSNuno Antunes node_p node = NULL;
33330147868eSNuno Antunes hook_p hook = NULL;
33340147868eSNuno Antunes struct mbuf *m;
33350147868eSNuno Antunes struct ng_mesg *msg;
33360147868eSNuno Antunes ng_ID_t retaddr;
33370147868eSNuno Antunes int error;
33380147868eSNuno Antunes
33390147868eSNuno Antunes NGI_GET_M(item, m);
33400147868eSNuno Antunes NGI_GET_MSG(item, msg);
33410147868eSNuno Antunes retaddr = NGI_RETADDR(item);
33420147868eSNuno Antunes NG_SEND_DATA(error, hook, m, NULL);
33430147868eSNuno Antunes NG_SEND_DATA_ONLY(error, hook, m);
33440147868eSNuno Antunes NG_FWD_NEW_DATA(error, item, hook, m);
33450147868eSNuno Antunes NG_FWD_ITEM_HOOK(error, item, hook);
33460147868eSNuno Antunes NG_SEND_MSG_HOOK(error, node, msg, hook, retaddr);
33470147868eSNuno Antunes NG_SEND_MSG_ID(error, node, msg, retaddr, retaddr);
33480147868eSNuno Antunes NG_SEND_MSG_PATH(error, node, msg, ".:", retaddr);
33490147868eSNuno Antunes NG_FWD_MSG_HOOK(error, node, item, hook, retaddr);
33500147868eSNuno Antunes }
33510147868eSNuno Antunes #endif /* TESTING */
33520147868eSNuno Antunes
3353