14cf49a43SJulian Elischer /* 24cf49a43SJulian Elischer * ng_base.c 34cf49a43SJulian Elischer * 44cf49a43SJulian Elischer * Copyright (c) 1996-1999 Whistle Communications, Inc. 54cf49a43SJulian Elischer * All rights reserved. 64cf49a43SJulian Elischer * 74cf49a43SJulian Elischer * Subject to the following obligations and disclaimer of warranty, use and 84cf49a43SJulian Elischer * redistribution of this software, in source or object code forms, with or 94cf49a43SJulian Elischer * without modifications are expressly permitted by Whistle Communications; 104cf49a43SJulian Elischer * provided, however, that: 114cf49a43SJulian Elischer * 1. Any and all reproductions of the source or object code must include the 124cf49a43SJulian Elischer * copyright notice above and the following disclaimer of warranties; and 134cf49a43SJulian Elischer * 2. No rights are granted, in any manner or form, to use Whistle 144cf49a43SJulian Elischer * Communications, Inc. trademarks, including the mark "WHISTLE 154cf49a43SJulian Elischer * COMMUNICATIONS" on advertising, endorsements, or otherwise except as 164cf49a43SJulian Elischer * such appears in the above copyright notice or in the software. 174cf49a43SJulian Elischer * 184cf49a43SJulian Elischer * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND 194cf49a43SJulian Elischer * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO 204cf49a43SJulian Elischer * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE, 214cf49a43SJulian Elischer * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF 224cf49a43SJulian Elischer * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. 234cf49a43SJulian Elischer * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY 244cf49a43SJulian Elischer * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS 254cf49a43SJulian Elischer * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE. 264cf49a43SJulian Elischer * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES 274cf49a43SJulian Elischer * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING 284cf49a43SJulian Elischer * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 294cf49a43SJulian Elischer * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR 304cf49a43SJulian Elischer * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY 314cf49a43SJulian Elischer * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 324cf49a43SJulian Elischer * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 334cf49a43SJulian Elischer * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY 344cf49a43SJulian Elischer * OF SUCH DAMAGE. 354cf49a43SJulian Elischer * 36cc3bbd68SJulian Elischer * Authors: Julian Elischer <julian@freebsd.org> 37cc3bbd68SJulian Elischer * Archie Cobbs <archie@freebsd.org> 384cf49a43SJulian Elischer * 394cf49a43SJulian Elischer * $FreeBSD$ 404cf49a43SJulian Elischer * $Whistle: ng_base.c,v 1.39 1999/01/28 23:54:53 julian Exp $ 414cf49a43SJulian Elischer */ 424cf49a43SJulian Elischer 434cf49a43SJulian Elischer /* 444cf49a43SJulian Elischer * This file implements the base netgraph code. 454cf49a43SJulian Elischer */ 464cf49a43SJulian Elischer 474cf49a43SJulian Elischer #include <sys/param.h> 484cf49a43SJulian Elischer #include <sys/systm.h> 494cf49a43SJulian Elischer #include <sys/errno.h> 504cf49a43SJulian Elischer #include <sys/kernel.h> 514cf49a43SJulian Elischer #include <sys/malloc.h> 524cf49a43SJulian Elischer #include <sys/syslog.h> 53069154d5SJulian Elischer #include <sys/sysctl.h> 544cf49a43SJulian Elischer #include <sys/linker.h> 554cf49a43SJulian Elischer #include <sys/queue.h> 564cf49a43SJulian Elischer #include <sys/mbuf.h> 575b664c7cSPoul-Henning Kamp #include <sys/ctype.h> 582b70adcbSArchie Cobbs #include <machine/limits.h> 594cf49a43SJulian Elischer 604cf49a43SJulian Elischer #include <net/netisr.h> 614cf49a43SJulian Elischer 624cf49a43SJulian Elischer #include <netgraph/ng_message.h> 634cf49a43SJulian Elischer #include <netgraph/netgraph.h> 64f8307e12SArchie Cobbs #include <netgraph/ng_parse.h> 654cf49a43SJulian Elischer 6699ff8176SPeter Wemm MODULE_VERSION(netgraph, 1); 6799ff8176SPeter Wemm 6830400f03SJulian Elischer /* List of all active nodes */ 69069154d5SJulian Elischer static LIST_HEAD(, ng_node) ng_nodelist; 70069154d5SJulian Elischer static struct mtx ng_nodelist_mtx; 71069154d5SJulian Elischer 7230400f03SJulian Elischer #ifdef NETGRAPH_DEBUG 7330400f03SJulian Elischer 7430400f03SJulian Elischer static SLIST_HEAD(, ng_node) ng_allnodes; 7530400f03SJulian Elischer static LIST_HEAD(, ng_node) ng_freenodes; /* in debug, we never free() them */ 7630400f03SJulian Elischer static SLIST_HEAD(, ng_hook) ng_allhooks; 7730400f03SJulian Elischer static LIST_HEAD(, ng_hook) ng_freehooks; /* in debug, we never free() them */ 7830400f03SJulian Elischer 7930400f03SJulian Elischer static void ng_dumpitems(void); 8030400f03SJulian Elischer static void ng_dumpnodes(void); 8130400f03SJulian Elischer static void ng_dumphooks(void); 8230400f03SJulian Elischer 8330400f03SJulian Elischer #endif /* NETGRAPH_DEBUG */ 84954c4772SJulian Elischer /* 85954c4772SJulian Elischer * DEAD versions of the structures. 86954c4772SJulian Elischer * In order to avoid races, it is sometimes neccesary to point 87954c4772SJulian Elischer * at SOMETHING even though theoretically, the current entity is 88954c4772SJulian Elischer * INVALID. Use these to avoid these races. 89954c4772SJulian Elischer */ 90954c4772SJulian Elischer struct ng_type ng_deadtype = { 91954c4772SJulian Elischer NG_ABI_VERSION, 92954c4772SJulian Elischer "dead", 93954c4772SJulian Elischer NULL, /* modevent */ 94954c4772SJulian Elischer NULL, /* constructor */ 95954c4772SJulian Elischer NULL, /* rcvmsg */ 96954c4772SJulian Elischer NULL, /* shutdown */ 97954c4772SJulian Elischer NULL, /* newhook */ 98954c4772SJulian Elischer NULL, /* findhook */ 99954c4772SJulian Elischer NULL, /* connect */ 100954c4772SJulian Elischer NULL, /* rcvdata */ 101954c4772SJulian Elischer NULL, /* disconnect */ 102954c4772SJulian Elischer NULL, /* cmdlist */ 103954c4772SJulian Elischer }; 10430400f03SJulian Elischer 105954c4772SJulian Elischer struct ng_node ng_deadnode = { 106954c4772SJulian Elischer "dead", 107954c4772SJulian Elischer &ng_deadtype, 108954c4772SJulian Elischer NG_INVALID, 109954c4772SJulian Elischer 1, /* refs */ 110954c4772SJulian Elischer 0, /* numhooks */ 111954c4772SJulian Elischer NULL, /* private */ 112954c4772SJulian Elischer 0, /* ID */ 113954c4772SJulian Elischer LIST_HEAD_INITIALIZER(ng_deadnode.hooks), 114954c4772SJulian Elischer {}, /* all_nodes list entry */ 115954c4772SJulian Elischer {}, /* id hashtable list entry */ 116954c4772SJulian Elischer {}, /* workqueue entry */ 117954c4772SJulian Elischer { 0, 118954c4772SJulian Elischer {}, /* should never use! (should hang) */ 119954c4772SJulian Elischer NULL, 120954c4772SJulian Elischer &ng_deadnode.nd_input_queue.queue, 121954c4772SJulian Elischer &ng_deadnode 122954c4772SJulian Elischer }, 123954c4772SJulian Elischer #ifdef NETGRAPH_DEBUG 124954c4772SJulian Elischer ND_MAGIC, 125954c4772SJulian Elischer __FILE__, 126954c4772SJulian Elischer __LINE__, 127954c4772SJulian Elischer {NULL} 128954c4772SJulian Elischer #endif /* NETGRAPH_DEBUG */ 129954c4772SJulian Elischer }; 130954c4772SJulian Elischer 131954c4772SJulian Elischer struct ng_hook ng_deadhook = { 132954c4772SJulian Elischer "dead", 133954c4772SJulian Elischer NULL, /* private */ 134954c4772SJulian Elischer HK_INVALID | HK_DEAD, 135954c4772SJulian Elischer 1, /* refs always >= 1 */ 136954c4772SJulian Elischer &ng_deadhook, /* Peer is self */ 137954c4772SJulian Elischer &ng_deadnode, /* attached to deadnode */ 138954c4772SJulian Elischer {}, /* hooks list */ 139954c4772SJulian Elischer #ifdef NETGRAPH_DEBUG 140954c4772SJulian Elischer HK_MAGIC, 141954c4772SJulian Elischer __FILE__, 142954c4772SJulian Elischer __LINE__, 143954c4772SJulian Elischer {NULL} 144954c4772SJulian Elischer #endif /* NETGRAPH_DEBUG */ 145954c4772SJulian Elischer }; 146954c4772SJulian Elischer 147954c4772SJulian Elischer /* 148954c4772SJulian Elischer * END DEAD STRUCTURES 149954c4772SJulian Elischer */ 150069154d5SJulian Elischer /* List nodes with unallocated work */ 151069154d5SJulian Elischer static TAILQ_HEAD(, ng_node) ng_worklist = TAILQ_HEAD_INITIALIZER(ng_worklist); 152069154d5SJulian Elischer static struct mtx ng_worklist_mtx; 1534cf49a43SJulian Elischer 1544cf49a43SJulian Elischer /* List of installed types */ 155069154d5SJulian Elischer static LIST_HEAD(, ng_type) ng_typelist; 156069154d5SJulian Elischer static struct mtx ng_typelist_mtx; 1574cf49a43SJulian Elischer 158069154d5SJulian Elischer /* Hash related definitions */ 159dc90cad9SJulian Elischer /* Don't nead to initialise them because it's a LIST */ 160069154d5SJulian Elischer #define ID_HASH_SIZE 32 /* most systems wont need even this many */ 161069154d5SJulian Elischer static LIST_HEAD(, ng_node) ng_ID_hash[ID_HASH_SIZE]; 162069154d5SJulian Elischer static struct mtx ng_idhash_mtx; 163069154d5SJulian Elischer 164069154d5SJulian Elischer /* Mutex that protects the free queue item list */ 165069154d5SJulian Elischer static volatile item_p ngqfree; /* free ones */ 166069154d5SJulian Elischer static struct mtx ngq_mtx; 167dc90cad9SJulian Elischer 1684cf49a43SJulian Elischer /* Internal functions */ 1694cf49a43SJulian Elischer static int ng_add_hook(node_p node, const char *name, hook_p * hookp); 1704cf49a43SJulian Elischer static int ng_connect(hook_p hook1, hook_p hook2); 1714cf49a43SJulian Elischer static void ng_disconnect_hook(hook_p hook); 172069154d5SJulian Elischer static int ng_generic_msg(node_p here, item_p item, hook_p lasthook); 173dc90cad9SJulian Elischer static ng_ID_t ng_decodeidname(const char *name); 1744cf49a43SJulian Elischer static int ngb_mod_event(module_t mod, int event, void *data); 175069154d5SJulian Elischer static void ng_worklist_remove(node_p node); 1764cf49a43SJulian Elischer static void ngintr(void); 177069154d5SJulian Elischer static int ng_apply_item(node_p node, item_p item); 178069154d5SJulian Elischer static void ng_flush_input_queue(struct ng_queue * ngq); 179069154d5SJulian Elischer static void ng_setisr(node_p node); 180069154d5SJulian Elischer static node_p ng_ID2noderef(ng_ID_t ID); 181069154d5SJulian Elischer 18230400f03SJulian Elischer /* imported , these used to be externally visible, some may go back */ 183069154d5SJulian Elischer int ng_bypass(hook_p hook1, hook_p hook2); 184069154d5SJulian Elischer void ng_cutlinks(node_p node); 185069154d5SJulian Elischer int ng_con_nodes(node_p node, const char *name, node_p node2, 186069154d5SJulian Elischer const char *name2); 187069154d5SJulian Elischer void ng_destroy_hook(hook_p hook); 188069154d5SJulian Elischer node_p ng_name2noderef(node_p node, const char *name); 189069154d5SJulian Elischer int ng_path2noderef(node_p here, const char *path, 190069154d5SJulian Elischer node_p *dest, hook_p *lasthook); 191069154d5SJulian Elischer struct ng_type *ng_findtype(const char *type); 192069154d5SJulian Elischer int ng_make_node(const char *type, node_p *nodepp); 193069154d5SJulian Elischer int ng_mkpeer(node_p node, const char *name, const char *name2, char *type); 194069154d5SJulian Elischer int ng_path_parse(char *addr, char **node, char **path, char **hook); 195069154d5SJulian Elischer void ng_rmnode(node_p node); 19630400f03SJulian Elischer void ng_unname(node_p node); 197069154d5SJulian Elischer 1984cf49a43SJulian Elischer 1994cf49a43SJulian Elischer /* Our own netgraph malloc type */ 2004cf49a43SJulian Elischer MALLOC_DEFINE(M_NETGRAPH, "netgraph", "netgraph structures and ctrl messages"); 201069154d5SJulian Elischer MALLOC_DEFINE(M_NETGRAPH_HOOK, "netgraph_hook", "netgraph hook structures"); 202069154d5SJulian Elischer MALLOC_DEFINE(M_NETGRAPH_NODE, "netgraph_node", "netgraph node structures"); 203069154d5SJulian Elischer MALLOC_DEFINE(M_NETGRAPH_ITEM, "netgraph_item", "netgraph item structures"); 204069154d5SJulian Elischer MALLOC_DEFINE(M_NETGRAPH_META, "netgraph_meta", "netgraph name storage"); 205069154d5SJulian Elischer MALLOC_DEFINE(M_NETGRAPH_MSG, "netgraph_msg", "netgraph name storage"); 206069154d5SJulian Elischer 207069154d5SJulian Elischer /* Should not be visible outside this file */ 20830400f03SJulian Elischer 20930400f03SJulian Elischer #define _NG_ALLOC_HOOK(hook) \ 21030400f03SJulian Elischer MALLOC(hook, hook_p, sizeof(*hook), M_NETGRAPH_HOOK, M_NOWAIT | M_ZERO) 21130400f03SJulian Elischer #define _NG_ALLOC_NODE(node) \ 21230400f03SJulian Elischer MALLOC(node, node_p, sizeof(*node), M_NETGRAPH_NODE, M_NOWAIT | M_ZERO) 21330400f03SJulian Elischer 21430400f03SJulian Elischer #ifdef NETGRAPH_DEBUG /*----------------------------------------------*/ 21530400f03SJulian Elischer /* 21630400f03SJulian Elischer * In debug mode: 21730400f03SJulian Elischer * In an attempt to help track reference count screwups 21830400f03SJulian Elischer * we do not free objects back to the malloc system, but keep them 21930400f03SJulian Elischer * in a local cache where we can examine them and keep information safely 22030400f03SJulian Elischer * after they have been freed. 22130400f03SJulian Elischer * We use this scheme for nodes and hooks, and to some extent for items. 22230400f03SJulian Elischer */ 22330400f03SJulian Elischer static __inline hook_p 22430400f03SJulian Elischer ng_alloc_hook(void) 22530400f03SJulian Elischer { 22630400f03SJulian Elischer hook_p hook; 22730400f03SJulian Elischer SLIST_ENTRY(ng_hook) temp; 22830400f03SJulian Elischer mtx_enter(&ng_nodelist_mtx, MTX_DEF); 22930400f03SJulian Elischer hook = LIST_FIRST(&ng_freehooks); 23030400f03SJulian Elischer if (hook) { 23130400f03SJulian Elischer LIST_REMOVE(hook, hk_hooks); 23230400f03SJulian Elischer bcopy(&hook->hk_all, &temp, sizeof(temp)); 23330400f03SJulian Elischer bzero(hook, sizeof(struct ng_hook)); 23430400f03SJulian Elischer bcopy(&temp, &hook->hk_all, sizeof(temp)); 23530400f03SJulian Elischer mtx_exit(&ng_nodelist_mtx, MTX_DEF); 23630400f03SJulian Elischer hook->hk_magic = HK_MAGIC; 23730400f03SJulian Elischer } else { 23830400f03SJulian Elischer mtx_exit(&ng_nodelist_mtx, MTX_DEF); 23930400f03SJulian Elischer _NG_ALLOC_HOOK(hook); 24030400f03SJulian Elischer if (hook) { 24130400f03SJulian Elischer hook->hk_magic = HK_MAGIC; 24230400f03SJulian Elischer mtx_enter(&ng_nodelist_mtx, MTX_DEF); 24330400f03SJulian Elischer SLIST_INSERT_HEAD(&ng_allhooks, hook, hk_all); 24430400f03SJulian Elischer mtx_exit(&ng_nodelist_mtx, MTX_DEF); 24530400f03SJulian Elischer } 24630400f03SJulian Elischer } 24730400f03SJulian Elischer return (hook); 24830400f03SJulian Elischer } 24930400f03SJulian Elischer 25030400f03SJulian Elischer static __inline node_p 25130400f03SJulian Elischer ng_alloc_node(void) 25230400f03SJulian Elischer { 25330400f03SJulian Elischer node_p node; 25430400f03SJulian Elischer SLIST_ENTRY(ng_node) temp; 25530400f03SJulian Elischer mtx_enter(&ng_nodelist_mtx, MTX_DEF); 25630400f03SJulian Elischer node = LIST_FIRST(&ng_freenodes); 25730400f03SJulian Elischer if (node) { 25830400f03SJulian Elischer LIST_REMOVE(node, nd_nodes); 25930400f03SJulian Elischer bcopy(&node->nd_all, &temp, sizeof(temp)); 26030400f03SJulian Elischer bzero(node, sizeof(struct ng_node)); 26130400f03SJulian Elischer bcopy(&temp, &node->nd_all, sizeof(temp)); 26230400f03SJulian Elischer mtx_exit(&ng_nodelist_mtx, MTX_DEF); 26330400f03SJulian Elischer node->nd_magic = ND_MAGIC; 26430400f03SJulian Elischer } else { 26530400f03SJulian Elischer mtx_exit(&ng_nodelist_mtx, MTX_DEF); 26630400f03SJulian Elischer _NG_ALLOC_NODE(node); 26730400f03SJulian Elischer if (node) { 26830400f03SJulian Elischer node->nd_magic = ND_MAGIC; 26930400f03SJulian Elischer mtx_enter(&ng_nodelist_mtx, MTX_DEF); 27030400f03SJulian Elischer SLIST_INSERT_HEAD(&ng_allnodes, node, nd_all); 27130400f03SJulian Elischer mtx_exit(&ng_nodelist_mtx, MTX_DEF); 27230400f03SJulian Elischer } 27330400f03SJulian Elischer } 27430400f03SJulian Elischer return (node); 27530400f03SJulian Elischer } 27630400f03SJulian Elischer 27730400f03SJulian Elischer #define NG_ALLOC_HOOK(hook) do { (hook) = ng_alloc_hook(); } while (0) 27830400f03SJulian Elischer #define NG_ALLOC_NODE(node) do { (node) = ng_alloc_node(); } while (0) 27930400f03SJulian Elischer 28030400f03SJulian Elischer 28130400f03SJulian Elischer #define NG_FREE_HOOK(hook) \ 28230400f03SJulian Elischer do { \ 28330400f03SJulian Elischer mtx_enter(&ng_nodelist_mtx, MTX_DEF); \ 28430400f03SJulian Elischer LIST_INSERT_HEAD(&ng_freehooks, hook, hk_hooks); \ 28530400f03SJulian Elischer hook->hk_magic = 0; \ 28630400f03SJulian Elischer mtx_exit(&ng_nodelist_mtx, MTX_DEF); \ 28730400f03SJulian Elischer } while (0) 28830400f03SJulian Elischer 28930400f03SJulian Elischer #define NG_FREE_NODE(node) \ 29030400f03SJulian Elischer do { \ 29130400f03SJulian Elischer mtx_enter(&ng_nodelist_mtx, MTX_DEF); \ 29230400f03SJulian Elischer LIST_INSERT_HEAD(&ng_freenodes, node, nd_nodes); \ 29330400f03SJulian Elischer node->nd_magic = 0; \ 29430400f03SJulian Elischer mtx_exit(&ng_nodelist_mtx, MTX_DEF); \ 29530400f03SJulian Elischer } while (0) 29630400f03SJulian Elischer 29730400f03SJulian Elischer #else /* NETGRAPH_DEBUG */ /*----------------------------------------------*/ 29830400f03SJulian Elischer 29930400f03SJulian Elischer #define NG_ALLOC_HOOK(hook) _NG_ALLOC_HOOK(hook) 30030400f03SJulian Elischer #define NG_ALLOC_NODE(node) _NG_ALLOC_NODE(node) 30130400f03SJulian Elischer 302069154d5SJulian Elischer #define NG_FREE_HOOK(hook) do { FREE((hook), M_NETGRAPH_HOOK); } while (0) 303069154d5SJulian Elischer #define NG_FREE_NODE(node) do { FREE((node), M_NETGRAPH_NODE); } while (0) 30430400f03SJulian Elischer 30530400f03SJulian Elischer #endif /* NETGRAPH_DEBUG */ /*----------------------------------------------*/ 30630400f03SJulian Elischer 307069154d5SJulian Elischer /* Warning: Generally use NG_FREE_ITEM() instead */ 308069154d5SJulian Elischer #define NG_FREE_ITEM_REAL(item) do { FREE((item), M_NETGRAPH_ITEM); } while (0) 309069154d5SJulian Elischer 3104cf49a43SJulian Elischer 3114cf49a43SJulian Elischer /* Set this to Debugger("X") to catch all errors as they occur */ 3124cf49a43SJulian Elischer #ifndef TRAP_ERROR 3134cf49a43SJulian Elischer #define TRAP_ERROR 3144cf49a43SJulian Elischer #endif 3154cf49a43SJulian Elischer 316dc90cad9SJulian Elischer static ng_ID_t nextID = 1; 317dc90cad9SJulian Elischer 318b2da83c2SArchie Cobbs #ifdef INVARIANTS 319b2da83c2SArchie Cobbs #define CHECK_DATA_MBUF(m) do { \ 320b2da83c2SArchie Cobbs struct mbuf *n; \ 321b2da83c2SArchie Cobbs int total; \ 322b2da83c2SArchie Cobbs \ 323b2da83c2SArchie Cobbs if (((m)->m_flags & M_PKTHDR) == 0) \ 324b2da83c2SArchie Cobbs panic("%s: !PKTHDR", __FUNCTION__); \ 325b2da83c2SArchie Cobbs for (total = 0, n = (m); n != NULL; n = n->m_next) \ 326b2da83c2SArchie Cobbs total += n->m_len; \ 327b2da83c2SArchie Cobbs if ((m)->m_pkthdr.len != total) { \ 328b2da83c2SArchie Cobbs panic("%s: %d != %d", \ 329b2da83c2SArchie Cobbs __FUNCTION__, (m)->m_pkthdr.len, total); \ 330b2da83c2SArchie Cobbs } \ 331b2da83c2SArchie Cobbs } while (0) 332b2da83c2SArchie Cobbs #else 333b2da83c2SArchie Cobbs #define CHECK_DATA_MBUF(m) 334b2da83c2SArchie Cobbs #endif 335b2da83c2SArchie Cobbs 336dc90cad9SJulian Elischer 3374cf49a43SJulian Elischer /************************************************************************ 338f8307e12SArchie Cobbs Parse type definitions for generic messages 339f8307e12SArchie Cobbs ************************************************************************/ 340f8307e12SArchie Cobbs 341f8307e12SArchie Cobbs /* Handy structure parse type defining macro */ 342f8307e12SArchie Cobbs #define DEFINE_PARSE_STRUCT_TYPE(lo, up, args) \ 343f8307e12SArchie Cobbs static const struct ng_parse_struct_info \ 344f8307e12SArchie Cobbs ng_ ## lo ## _type_info = NG_GENERIC_ ## up ## _INFO args; \ 345f8307e12SArchie Cobbs static const struct ng_parse_type ng_generic_ ## lo ## _type = { \ 346f8307e12SArchie Cobbs &ng_parse_struct_type, \ 347f8307e12SArchie Cobbs &ng_ ## lo ## _type_info \ 348f8307e12SArchie Cobbs } 349f8307e12SArchie Cobbs 350f8307e12SArchie Cobbs DEFINE_PARSE_STRUCT_TYPE(mkpeer, MKPEER, ()); 351f8307e12SArchie Cobbs DEFINE_PARSE_STRUCT_TYPE(connect, CONNECT, ()); 352f8307e12SArchie Cobbs DEFINE_PARSE_STRUCT_TYPE(name, NAME, ()); 353f8307e12SArchie Cobbs DEFINE_PARSE_STRUCT_TYPE(rmhook, RMHOOK, ()); 354f8307e12SArchie Cobbs DEFINE_PARSE_STRUCT_TYPE(nodeinfo, NODEINFO, ()); 355f8307e12SArchie Cobbs DEFINE_PARSE_STRUCT_TYPE(typeinfo, TYPEINFO, ()); 356f8307e12SArchie Cobbs DEFINE_PARSE_STRUCT_TYPE(linkinfo, LINKINFO, (&ng_generic_nodeinfo_type)); 357f8307e12SArchie Cobbs 358f8307e12SArchie Cobbs /* Get length of an array when the length is stored as a 32 bit 359f8307e12SArchie Cobbs value immediately preceeding the array -- as with struct namelist 360f8307e12SArchie Cobbs and struct typelist. */ 361f8307e12SArchie Cobbs static int 362f8307e12SArchie Cobbs ng_generic_list_getLength(const struct ng_parse_type *type, 363f8307e12SArchie Cobbs const u_char *start, const u_char *buf) 364f8307e12SArchie Cobbs { 365f8307e12SArchie Cobbs return *((const u_int32_t *)(buf - 4)); 366f8307e12SArchie Cobbs } 367f8307e12SArchie Cobbs 368f8307e12SArchie Cobbs /* Get length of the array of struct linkinfo inside a struct hooklist */ 369f8307e12SArchie Cobbs static int 370f8307e12SArchie Cobbs ng_generic_linkinfo_getLength(const struct ng_parse_type *type, 371f8307e12SArchie Cobbs const u_char *start, const u_char *buf) 372f8307e12SArchie Cobbs { 373f8307e12SArchie Cobbs const struct hooklist *hl = (const struct hooklist *)start; 374f8307e12SArchie Cobbs 375f8307e12SArchie Cobbs return hl->nodeinfo.hooks; 376f8307e12SArchie Cobbs } 377f8307e12SArchie Cobbs 378f8307e12SArchie Cobbs /* Array type for a variable length array of struct namelist */ 379f8307e12SArchie Cobbs static const struct ng_parse_array_info ng_nodeinfoarray_type_info = { 380f8307e12SArchie Cobbs &ng_generic_nodeinfo_type, 381f8307e12SArchie Cobbs &ng_generic_list_getLength 382f8307e12SArchie Cobbs }; 383f8307e12SArchie Cobbs static const struct ng_parse_type ng_generic_nodeinfoarray_type = { 384f8307e12SArchie Cobbs &ng_parse_array_type, 385f8307e12SArchie Cobbs &ng_nodeinfoarray_type_info 386f8307e12SArchie Cobbs }; 387f8307e12SArchie Cobbs 388f8307e12SArchie Cobbs /* Array type for a variable length array of struct typelist */ 389f8307e12SArchie Cobbs static const struct ng_parse_array_info ng_typeinfoarray_type_info = { 390f8307e12SArchie Cobbs &ng_generic_typeinfo_type, 391f8307e12SArchie Cobbs &ng_generic_list_getLength 392f8307e12SArchie Cobbs }; 393f8307e12SArchie Cobbs static const struct ng_parse_type ng_generic_typeinfoarray_type = { 394f8307e12SArchie Cobbs &ng_parse_array_type, 395f8307e12SArchie Cobbs &ng_typeinfoarray_type_info 396f8307e12SArchie Cobbs }; 397f8307e12SArchie Cobbs 398f8307e12SArchie Cobbs /* Array type for array of struct linkinfo in struct hooklist */ 399f8307e12SArchie Cobbs static const struct ng_parse_array_info ng_generic_linkinfo_array_type_info = { 400f8307e12SArchie Cobbs &ng_generic_linkinfo_type, 401f8307e12SArchie Cobbs &ng_generic_linkinfo_getLength 402f8307e12SArchie Cobbs }; 403f8307e12SArchie Cobbs static const struct ng_parse_type ng_generic_linkinfo_array_type = { 404f8307e12SArchie Cobbs &ng_parse_array_type, 405f8307e12SArchie Cobbs &ng_generic_linkinfo_array_type_info 406f8307e12SArchie Cobbs }; 407f8307e12SArchie Cobbs 408f8307e12SArchie Cobbs DEFINE_PARSE_STRUCT_TYPE(typelist, TYPELIST, (&ng_generic_nodeinfoarray_type)); 409f8307e12SArchie Cobbs DEFINE_PARSE_STRUCT_TYPE(hooklist, HOOKLIST, 410f8307e12SArchie Cobbs (&ng_generic_nodeinfo_type, &ng_generic_linkinfo_array_type)); 411f8307e12SArchie Cobbs DEFINE_PARSE_STRUCT_TYPE(listnodes, LISTNODES, 412f8307e12SArchie Cobbs (&ng_generic_nodeinfoarray_type)); 413f8307e12SArchie Cobbs 414f8307e12SArchie Cobbs /* List of commands and how to convert arguments to/from ASCII */ 415f8307e12SArchie Cobbs static const struct ng_cmdlist ng_generic_cmds[] = { 416f8307e12SArchie Cobbs { 417f8307e12SArchie Cobbs NGM_GENERIC_COOKIE, 418f8307e12SArchie Cobbs NGM_SHUTDOWN, 419f8307e12SArchie Cobbs "shutdown", 420f8307e12SArchie Cobbs NULL, 421f8307e12SArchie Cobbs NULL 422f8307e12SArchie Cobbs }, 423f8307e12SArchie Cobbs { 424f8307e12SArchie Cobbs NGM_GENERIC_COOKIE, 425f8307e12SArchie Cobbs NGM_MKPEER, 426f8307e12SArchie Cobbs "mkpeer", 427f8307e12SArchie Cobbs &ng_generic_mkpeer_type, 428f8307e12SArchie Cobbs NULL 429f8307e12SArchie Cobbs }, 430f8307e12SArchie Cobbs { 431f8307e12SArchie Cobbs NGM_GENERIC_COOKIE, 432f8307e12SArchie Cobbs NGM_CONNECT, 433f8307e12SArchie Cobbs "connect", 434f8307e12SArchie Cobbs &ng_generic_connect_type, 435f8307e12SArchie Cobbs NULL 436f8307e12SArchie Cobbs }, 437f8307e12SArchie Cobbs { 438f8307e12SArchie Cobbs NGM_GENERIC_COOKIE, 439f8307e12SArchie Cobbs NGM_NAME, 440f8307e12SArchie Cobbs "name", 441f8307e12SArchie Cobbs &ng_generic_name_type, 442f8307e12SArchie Cobbs NULL 443f8307e12SArchie Cobbs }, 444f8307e12SArchie Cobbs { 445f8307e12SArchie Cobbs NGM_GENERIC_COOKIE, 446f8307e12SArchie Cobbs NGM_RMHOOK, 447f8307e12SArchie Cobbs "rmhook", 448f8307e12SArchie Cobbs &ng_generic_rmhook_type, 449f8307e12SArchie Cobbs NULL 450f8307e12SArchie Cobbs }, 451f8307e12SArchie Cobbs { 452f8307e12SArchie Cobbs NGM_GENERIC_COOKIE, 453f8307e12SArchie Cobbs NGM_NODEINFO, 454f8307e12SArchie Cobbs "nodeinfo", 455f8307e12SArchie Cobbs NULL, 456f8307e12SArchie Cobbs &ng_generic_nodeinfo_type 457f8307e12SArchie Cobbs }, 458f8307e12SArchie Cobbs { 459f8307e12SArchie Cobbs NGM_GENERIC_COOKIE, 460f8307e12SArchie Cobbs NGM_LISTHOOKS, 461f8307e12SArchie Cobbs "listhooks", 462f8307e12SArchie Cobbs NULL, 463f8307e12SArchie Cobbs &ng_generic_hooklist_type 464f8307e12SArchie Cobbs }, 465f8307e12SArchie Cobbs { 466f8307e12SArchie Cobbs NGM_GENERIC_COOKIE, 467f8307e12SArchie Cobbs NGM_LISTNAMES, 468f8307e12SArchie Cobbs "listnames", 469f8307e12SArchie Cobbs NULL, 470f8307e12SArchie Cobbs &ng_generic_listnodes_type /* same as NGM_LISTNODES */ 471f8307e12SArchie Cobbs }, 472f8307e12SArchie Cobbs { 473f8307e12SArchie Cobbs NGM_GENERIC_COOKIE, 474f8307e12SArchie Cobbs NGM_LISTNODES, 475f8307e12SArchie Cobbs "listnodes", 476f8307e12SArchie Cobbs NULL, 477f8307e12SArchie Cobbs &ng_generic_listnodes_type 478f8307e12SArchie Cobbs }, 479f8307e12SArchie Cobbs { 480f8307e12SArchie Cobbs NGM_GENERIC_COOKIE, 481f8307e12SArchie Cobbs NGM_LISTTYPES, 482f8307e12SArchie Cobbs "listtypes", 483f8307e12SArchie Cobbs NULL, 484f8307e12SArchie Cobbs &ng_generic_typeinfo_type 485f8307e12SArchie Cobbs }, 486f8307e12SArchie Cobbs { 487f8307e12SArchie Cobbs NGM_GENERIC_COOKIE, 4887095e097SPoul-Henning Kamp NGM_TEXT_CONFIG, 4897095e097SPoul-Henning Kamp "textconfig", 4907095e097SPoul-Henning Kamp NULL, 4917095e097SPoul-Henning Kamp &ng_parse_string_type 4927095e097SPoul-Henning Kamp }, 4937095e097SPoul-Henning Kamp { 4947095e097SPoul-Henning Kamp NGM_GENERIC_COOKIE, 495f8307e12SArchie Cobbs NGM_TEXT_STATUS, 496f8307e12SArchie Cobbs "textstatus", 497f8307e12SArchie Cobbs NULL, 498f8307e12SArchie Cobbs &ng_parse_string_type 499f8307e12SArchie Cobbs }, 500f8307e12SArchie Cobbs { 501f8307e12SArchie Cobbs NGM_GENERIC_COOKIE, 502f8307e12SArchie Cobbs NGM_ASCII2BINARY, 503f8307e12SArchie Cobbs "ascii2binary", 504f8307e12SArchie Cobbs &ng_parse_ng_mesg_type, 505f8307e12SArchie Cobbs &ng_parse_ng_mesg_type 506f8307e12SArchie Cobbs }, 507f8307e12SArchie Cobbs { 508f8307e12SArchie Cobbs NGM_GENERIC_COOKIE, 509f8307e12SArchie Cobbs NGM_BINARY2ASCII, 510f8307e12SArchie Cobbs "binary2ascii", 511f8307e12SArchie Cobbs &ng_parse_ng_mesg_type, 512f8307e12SArchie Cobbs &ng_parse_ng_mesg_type 513f8307e12SArchie Cobbs }, 514f8307e12SArchie Cobbs { 0 } 515f8307e12SArchie Cobbs }; 516f8307e12SArchie Cobbs 517f8307e12SArchie Cobbs /************************************************************************ 5184cf49a43SJulian Elischer Node routines 5194cf49a43SJulian Elischer ************************************************************************/ 5204cf49a43SJulian Elischer 5214cf49a43SJulian Elischer /* 5224cf49a43SJulian Elischer * Instantiate a node of the requested type 5234cf49a43SJulian Elischer */ 5244cf49a43SJulian Elischer int 5254cf49a43SJulian Elischer ng_make_node(const char *typename, node_p *nodepp) 5264cf49a43SJulian Elischer { 5274cf49a43SJulian Elischer struct ng_type *type; 528069154d5SJulian Elischer int error; 5294cf49a43SJulian Elischer 5304cf49a43SJulian Elischer /* Check that the type makes sense */ 5314cf49a43SJulian Elischer if (typename == NULL) { 5324cf49a43SJulian Elischer TRAP_ERROR; 5334cf49a43SJulian Elischer return (EINVAL); 5344cf49a43SJulian Elischer } 5354cf49a43SJulian Elischer 5364cf49a43SJulian Elischer /* Locate the node type */ 5374cf49a43SJulian Elischer if ((type = ng_findtype(typename)) == NULL) { 538cc3daab5SPeter Wemm char filename[NG_TYPELEN + 4]; 5394cf49a43SJulian Elischer linker_file_t lf; 5404cf49a43SJulian Elischer int error; 5414cf49a43SJulian Elischer 5424cf49a43SJulian Elischer /* Not found, try to load it as a loadable module */ 543453b5565SJulian Elischer snprintf(filename, sizeof(filename), "ng_%s", typename); 544cc3daab5SPeter Wemm error = linker_load_file(filename, &lf); 5454cf49a43SJulian Elischer if (error != 0) 5464cf49a43SJulian Elischer return (error); 5474cf49a43SJulian Elischer lf->userrefs++; /* pretend loaded by the syscall */ 5484cf49a43SJulian Elischer 5494cf49a43SJulian Elischer /* Try again, as now the type should have linked itself in */ 5504cf49a43SJulian Elischer if ((type = ng_findtype(typename)) == NULL) 5514cf49a43SJulian Elischer return (ENXIO); 5524cf49a43SJulian Elischer } 5534cf49a43SJulian Elischer 554069154d5SJulian Elischer /* 555069154d5SJulian Elischer * If we have a constructor, then make the node and 556069154d5SJulian Elischer * call the constructor to do type specific initialisation. 557069154d5SJulian Elischer */ 558069154d5SJulian Elischer if (type->constructor != NULL) { 559069154d5SJulian Elischer if ((error = ng_make_node_common(type, nodepp)) == 0) { 560069154d5SJulian Elischer if ((error = ((*type->constructor)(*nodepp)) != 0)) { 56130400f03SJulian Elischer NG_NODE_UNREF(*nodepp); 562069154d5SJulian Elischer } 563069154d5SJulian Elischer } 564069154d5SJulian Elischer } else { 565069154d5SJulian Elischer /* 566069154d5SJulian Elischer * Node has no constructor. We cannot ask for one 567069154d5SJulian Elischer * to be made. It must be brought into existance by 568954c4772SJulian Elischer * some external agency. The external agency should 569069154d5SJulian Elischer * call ng_make_node_common() directly to get the 570069154d5SJulian Elischer * netgraph part initialised. 571069154d5SJulian Elischer */ 57230400f03SJulian Elischer TRAP_ERROR 573069154d5SJulian Elischer error = EINVAL; 574069154d5SJulian Elischer } 575069154d5SJulian Elischer return (error); 5764cf49a43SJulian Elischer } 5774cf49a43SJulian Elischer 5784cf49a43SJulian Elischer /* 579069154d5SJulian Elischer * Generic node creation. Called by node initialisation for externally 580069154d5SJulian Elischer * instantiated nodes (e.g. hardware, sockets, etc ). 5814cf49a43SJulian Elischer * The returned node has a reference count of 1. 5824cf49a43SJulian Elischer */ 5834cf49a43SJulian Elischer int 5844cf49a43SJulian Elischer ng_make_node_common(struct ng_type *type, node_p *nodepp) 5854cf49a43SJulian Elischer { 5864cf49a43SJulian Elischer node_p node; 5874cf49a43SJulian Elischer 5884cf49a43SJulian Elischer /* Require the node type to have been already installed */ 5894cf49a43SJulian Elischer if (ng_findtype(type->name) == NULL) { 5904cf49a43SJulian Elischer TRAP_ERROR; 5914cf49a43SJulian Elischer return (EINVAL); 5924cf49a43SJulian Elischer } 5934cf49a43SJulian Elischer 5944cf49a43SJulian Elischer /* Make a node and try attach it to the type */ 59530400f03SJulian Elischer NG_ALLOC_NODE(node); 5964cf49a43SJulian Elischer if (node == NULL) { 5974cf49a43SJulian Elischer TRAP_ERROR; 5984cf49a43SJulian Elischer return (ENOMEM); 5994cf49a43SJulian Elischer } 60030400f03SJulian Elischer node->nd_type = type; 60130400f03SJulian Elischer NG_NODE_REF(node); /* note reference */ 6024cf49a43SJulian Elischer type->refs++; 6034cf49a43SJulian Elischer 60430400f03SJulian Elischer mtx_init(&node->nd_input_queue.q_mtx, "netgraph node mutex", 0); 60530400f03SJulian Elischer node->nd_input_queue.queue = NULL; 60630400f03SJulian Elischer node->nd_input_queue.last = &node->nd_input_queue.queue; 60730400f03SJulian Elischer node->nd_input_queue.q_flags = 0; 60830400f03SJulian Elischer node->nd_input_queue.q_node = node; 6094cf49a43SJulian Elischer 6104cf49a43SJulian Elischer /* Initialize hook list for new node */ 61130400f03SJulian Elischer LIST_INIT(&node->nd_hooks); 6124cf49a43SJulian Elischer 613069154d5SJulian Elischer /* Link us into the node linked list */ 614069154d5SJulian Elischer mtx_enter(&ng_nodelist_mtx, MTX_DEF); 61530400f03SJulian Elischer LIST_INSERT_HEAD(&ng_nodelist, node, nd_nodes); 616069154d5SJulian Elischer mtx_exit(&ng_nodelist_mtx, MTX_DEF); 617069154d5SJulian Elischer 618069154d5SJulian Elischer 619dc90cad9SJulian Elischer /* get an ID and put us in the hash chain */ 620069154d5SJulian Elischer mtx_enter(&ng_idhash_mtx, MTX_DEF); 62130400f03SJulian Elischer for (;;) { /* wrap protection, even if silly */ 622069154d5SJulian Elischer node_p node2 = NULL; 62330400f03SJulian Elischer node->nd_ID = nextID++; /* 137/second for 1 year before wrap */ 62430400f03SJulian Elischer /* Is there a problem with the new number? */ 62530400f03SJulian Elischer if ((node->nd_ID == 0) 62630400f03SJulian Elischer || (node2 = ng_ID2noderef(node->nd_ID))) { 627069154d5SJulian Elischer if (node2) { 62830400f03SJulian Elischer NG_NODE_UNREF(node2); 629069154d5SJulian Elischer node2 = NULL; 630069154d5SJulian Elischer } 63130400f03SJulian Elischer } else { 63230400f03SJulian Elischer break; 633069154d5SJulian Elischer } 63430400f03SJulian Elischer } 63530400f03SJulian Elischer LIST_INSERT_HEAD(&ng_ID_hash[node->nd_ID % ID_HASH_SIZE], 63630400f03SJulian Elischer node, nd_idnodes); 637069154d5SJulian Elischer mtx_exit(&ng_idhash_mtx, MTX_DEF); 638dc90cad9SJulian Elischer 6394cf49a43SJulian Elischer /* Done */ 6404cf49a43SJulian Elischer *nodepp = node; 6414cf49a43SJulian Elischer return (0); 6424cf49a43SJulian Elischer } 6434cf49a43SJulian Elischer 6444cf49a43SJulian Elischer /* 6454cf49a43SJulian Elischer * Forceably start the shutdown process on a node. Either call 6464cf49a43SJulian Elischer * it's shutdown method, or do the default shutdown if there is 6474cf49a43SJulian Elischer * no type-specific method. 6484cf49a43SJulian Elischer * 649069154d5SJulian Elischer * We can only be called form a shutdown message, so we know we have 650069154d5SJulian Elischer * a writer lock, and therefore exclusive access. 651069154d5SJulian Elischer * 652069154d5SJulian Elischer * Persistent node types must have a type-specific method which 653069154d5SJulian Elischer * Allocates a new node. This one is irretrievably going away. 6544cf49a43SJulian Elischer */ 6554cf49a43SJulian Elischer void 6564cf49a43SJulian Elischer ng_rmnode(node_p node) 6574cf49a43SJulian Elischer { 6584cf49a43SJulian Elischer /* Check if it's already shutting down */ 65930400f03SJulian Elischer if ((node->nd_flags & NG_CLOSING) != 0) 6604cf49a43SJulian Elischer return; 6614cf49a43SJulian Elischer 6624cf49a43SJulian Elischer /* Add an extra reference so it doesn't go away during this */ 66330400f03SJulian Elischer NG_NODE_REF(node); 6644cf49a43SJulian Elischer 66530400f03SJulian Elischer /* 66630400f03SJulian Elischer * Mark it invalid so any newcomers know not to try use it 66730400f03SJulian Elischer * Also add our own mark so we can't recurse 66830400f03SJulian Elischer * note that NG_INVALID does not do this as it's also set during 66930400f03SJulian Elischer * creation 67030400f03SJulian Elischer */ 67130400f03SJulian Elischer node->nd_flags |= NG_INVALID|NG_CLOSING; 6724cf49a43SJulian Elischer 6734cf49a43SJulian Elischer ng_cutlinks(node); 67430400f03SJulian Elischer 675069154d5SJulian Elischer /* 676069154d5SJulian Elischer * Drain the input queue forceably. 67730400f03SJulian Elischer * it has no hooks so what's it going to do, bleed on someone? 67830400f03SJulian Elischer * Theoretically we came here from a queue entry that was added 67930400f03SJulian Elischer * Just before the queue was closed, so it should be empty anyway. 680069154d5SJulian Elischer */ 68130400f03SJulian Elischer ng_flush_input_queue(&node->nd_input_queue); 682069154d5SJulian Elischer 683069154d5SJulian Elischer /* 684069154d5SJulian Elischer * Take us off the work queue if we are there. 68530400f03SJulian Elischer * We definatly have no work to be done. 686069154d5SJulian Elischer */ 687069154d5SJulian Elischer ng_worklist_remove(node); 688069154d5SJulian Elischer 689069154d5SJulian Elischer /* Ask the type if it has anything to do in this case */ 69030400f03SJulian Elischer if (node->nd_type && node->nd_type->shutdown) { 69130400f03SJulian Elischer (*node->nd_type->shutdown)(node); 692069154d5SJulian Elischer } else { /* do the default thing */ 69330400f03SJulian Elischer NG_NODE_UNREF(node); 69430400f03SJulian Elischer } 69530400f03SJulian Elischer if (NG_NODE_IS_VALID(node)) { 69630400f03SJulian Elischer /* 69730400f03SJulian Elischer * Well, blow me down if the node code hasn't declared 69830400f03SJulian Elischer * that it doesn't want to die. 69930400f03SJulian Elischer * Presumably it is a persistant node. 70030400f03SJulian Elischer * XXX we need a way to tell the node 70130400f03SJulian Elischer * "No, really.. the hardware's going away.. REALLY die" 70230400f03SJulian Elischer * We need a way 70330400f03SJulian Elischer */ 70430400f03SJulian Elischer return; 7054cf49a43SJulian Elischer } 7064cf49a43SJulian Elischer 70730400f03SJulian Elischer ng_unname(node); /* basically a NOP these days */ 70830400f03SJulian Elischer 70930400f03SJulian Elischer /* 71030400f03SJulian Elischer * Remove extra reference, possibly the last 71130400f03SJulian Elischer * Possible other holders of references may include 71230400f03SJulian Elischer * timeout callouts, but theoretically the node's supposed to 71330400f03SJulian Elischer * have cancelled them. Possibly hardware dependencies may 71430400f03SJulian Elischer * force a driver to 'linger' with a reference. 71530400f03SJulian Elischer */ 71630400f03SJulian Elischer NG_NODE_UNREF(node); 7174cf49a43SJulian Elischer } 7184cf49a43SJulian Elischer 7194cf49a43SJulian Elischer /* 7204cf49a43SJulian Elischer * Called by the destructor to remove any STANDARD external references 72130400f03SJulian Elischer * May one day have it's own message to call it.. 7224cf49a43SJulian Elischer */ 7234cf49a43SJulian Elischer void 7244cf49a43SJulian Elischer ng_cutlinks(node_p node) 7254cf49a43SJulian Elischer { 7264cf49a43SJulian Elischer hook_p hook; 7274cf49a43SJulian Elischer 7284cf49a43SJulian Elischer /* Make sure that this is set to stop infinite loops */ 72930400f03SJulian Elischer node->nd_flags |= NG_INVALID; 7304cf49a43SJulian Elischer 731069154d5SJulian Elischer /* 732069154d5SJulian Elischer * Drain the input queue forceably. 733069154d5SJulian Elischer * We also do this in ng_rmnode 734069154d5SJulian Elischer * to make sure we get all code paths. 735069154d5SJulian Elischer */ 73630400f03SJulian Elischer ng_flush_input_queue(&node->nd_input_queue); 7374cf49a43SJulian Elischer 7384cf49a43SJulian Elischer /* Notify all remaining connected nodes to disconnect */ 73930400f03SJulian Elischer while ((hook = LIST_FIRST(&node->nd_hooks)) != NULL) 7404cf49a43SJulian Elischer ng_destroy_hook(hook); 7414cf49a43SJulian Elischer } 7424cf49a43SJulian Elischer 7434cf49a43SJulian Elischer /* 7444cf49a43SJulian Elischer * Remove a reference to the node, possibly the last 7454cf49a43SJulian Elischer */ 7464cf49a43SJulian Elischer void 74730400f03SJulian Elischer ng_unref_node(node_p node) 7484cf49a43SJulian Elischer { 74930400f03SJulian Elischer int v; 75030400f03SJulian Elischer do { 75130400f03SJulian Elischer v = node->nd_refs; 75230400f03SJulian Elischer } while (! atomic_cmpset_int(&node->nd_refs, v, v - 1)); 753e8a49db2SJulian Elischer 75430400f03SJulian Elischer if (v == 1) { /* we were the last */ 755069154d5SJulian Elischer 756069154d5SJulian Elischer mtx_enter(&ng_nodelist_mtx, MTX_DEF); 75730400f03SJulian Elischer node->nd_type->refs--; /* XXX maybe should get types lock? */ 75830400f03SJulian Elischer LIST_REMOVE(node, nd_nodes); 759069154d5SJulian Elischer mtx_exit(&ng_nodelist_mtx, MTX_DEF); 760069154d5SJulian Elischer 761069154d5SJulian Elischer mtx_enter(&ng_idhash_mtx, MTX_DEF); 76230400f03SJulian Elischer LIST_REMOVE(node, nd_idnodes); 763069154d5SJulian Elischer mtx_exit(&ng_idhash_mtx, MTX_DEF); 764069154d5SJulian Elischer 76512574a02SJulian Elischer mtx_destroy(&node->nd_input_queue.q_mtx); 766069154d5SJulian Elischer NG_FREE_NODE(node); 7674cf49a43SJulian Elischer } 7684cf49a43SJulian Elischer } 7694cf49a43SJulian Elischer 7704cf49a43SJulian Elischer /************************************************************************ 771dc90cad9SJulian Elischer Node ID handling 772dc90cad9SJulian Elischer ************************************************************************/ 773dc90cad9SJulian Elischer static node_p 774069154d5SJulian Elischer ng_ID2noderef(ng_ID_t ID) 775dc90cad9SJulian Elischer { 77630400f03SJulian Elischer node_p node; 777069154d5SJulian Elischer mtx_enter(&ng_idhash_mtx, MTX_DEF); 77830400f03SJulian Elischer LIST_FOREACH(node, &ng_ID_hash[ID % ID_HASH_SIZE], nd_idnodes) { 77970de87f2SJulian Elischer if (NG_NODE_IS_VALID(node) && (NG_NODE_ID(node) == ID)) { 780dc90cad9SJulian Elischer break; 781dc90cad9SJulian Elischer } 78270de87f2SJulian Elischer } 78330400f03SJulian Elischer if(node) 78430400f03SJulian Elischer NG_NODE_REF(node); 785069154d5SJulian Elischer mtx_exit(&ng_idhash_mtx, MTX_DEF); 78630400f03SJulian Elischer return(node); 787dc90cad9SJulian Elischer } 788dc90cad9SJulian Elischer 789dc90cad9SJulian Elischer ng_ID_t 790dc90cad9SJulian Elischer ng_node2ID(node_p node) 791dc90cad9SJulian Elischer { 79270de87f2SJulian Elischer return (node ? NG_NODE_ID(node) : 0); 793dc90cad9SJulian Elischer } 794dc90cad9SJulian Elischer 795dc90cad9SJulian Elischer /************************************************************************ 7964cf49a43SJulian Elischer Node name handling 7974cf49a43SJulian Elischer ************************************************************************/ 7984cf49a43SJulian Elischer 7994cf49a43SJulian Elischer /* 8004cf49a43SJulian Elischer * Assign a node a name. Once assigned, the name cannot be changed. 8014cf49a43SJulian Elischer */ 8024cf49a43SJulian Elischer int 8034cf49a43SJulian Elischer ng_name_node(node_p node, const char *name) 8044cf49a43SJulian Elischer { 8054cf49a43SJulian Elischer int i; 806069154d5SJulian Elischer node_p node2; 8074cf49a43SJulian Elischer 8084cf49a43SJulian Elischer /* Check the name is valid */ 8094cf49a43SJulian Elischer for (i = 0; i < NG_NODELEN + 1; i++) { 8104cf49a43SJulian Elischer if (name[i] == '\0' || name[i] == '.' || name[i] == ':') 8114cf49a43SJulian Elischer break; 8124cf49a43SJulian Elischer } 8134cf49a43SJulian Elischer if (i == 0 || name[i] != '\0') { 8144cf49a43SJulian Elischer TRAP_ERROR; 8154cf49a43SJulian Elischer return (EINVAL); 8164cf49a43SJulian Elischer } 817dc90cad9SJulian Elischer if (ng_decodeidname(name) != 0) { /* valid IDs not allowed here */ 8184cf49a43SJulian Elischer TRAP_ERROR; 8194cf49a43SJulian Elischer return (EINVAL); 8204cf49a43SJulian Elischer } 8214cf49a43SJulian Elischer 8224cf49a43SJulian Elischer /* Check the name isn't already being used */ 823069154d5SJulian Elischer if ((node2 = ng_name2noderef(node, name)) != NULL) { 82430400f03SJulian Elischer NG_NODE_UNREF(node2); 8254cf49a43SJulian Elischer TRAP_ERROR; 8264cf49a43SJulian Elischer return (EADDRINUSE); 8274cf49a43SJulian Elischer } 8284cf49a43SJulian Elischer 829069154d5SJulian Elischer /* copy it */ 83030400f03SJulian Elischer strncpy(NG_NODE_NAME(node), name, NG_NODELEN); 8314cf49a43SJulian Elischer 8324cf49a43SJulian Elischer return (0); 8334cf49a43SJulian Elischer } 8344cf49a43SJulian Elischer 8354cf49a43SJulian Elischer /* 8364cf49a43SJulian Elischer * Find a node by absolute name. The name should NOT end with ':' 8374cf49a43SJulian Elischer * The name "." means "this node" and "[xxx]" means "the node 8384cf49a43SJulian Elischer * with ID (ie, at address) xxx". 8394cf49a43SJulian Elischer * 8404cf49a43SJulian Elischer * Returns the node if found, else NULL. 841069154d5SJulian Elischer * Eventually should add something faster than a sequential search. 84230400f03SJulian Elischer * Note it aquires a reference on the node so you can be sure it's still there. 8434cf49a43SJulian Elischer */ 8444cf49a43SJulian Elischer node_p 845069154d5SJulian Elischer ng_name2noderef(node_p here, const char *name) 8464cf49a43SJulian Elischer { 847dc90cad9SJulian Elischer node_p node; 848dc90cad9SJulian Elischer ng_ID_t temp; 8494cf49a43SJulian Elischer 8504cf49a43SJulian Elischer /* "." means "this node" */ 851069154d5SJulian Elischer if (strcmp(name, ".") == 0) { 85230400f03SJulian Elischer NG_NODE_REF(here); 853069154d5SJulian Elischer return(here); 854069154d5SJulian Elischer } 8554cf49a43SJulian Elischer 8564cf49a43SJulian Elischer /* Check for name-by-ID */ 857dc90cad9SJulian Elischer if ((temp = ng_decodeidname(name)) != 0) { 858069154d5SJulian Elischer return (ng_ID2noderef(temp)); 8594cf49a43SJulian Elischer } 8604cf49a43SJulian Elischer 8614cf49a43SJulian Elischer /* Find node by name */ 862069154d5SJulian Elischer mtx_enter(&ng_nodelist_mtx, MTX_DEF); 86330400f03SJulian Elischer LIST_FOREACH(node, &ng_nodelist, nd_nodes) { 86470de87f2SJulian Elischer if (NG_NODE_IS_VALID(node) 86570de87f2SJulian Elischer && NG_NODE_HAS_NAME(node) 86670de87f2SJulian Elischer && (strcmp(NG_NODE_NAME(node), name) == 0)) { 8674cf49a43SJulian Elischer break; 8684cf49a43SJulian Elischer } 86970de87f2SJulian Elischer } 870069154d5SJulian Elischer if (node) 87130400f03SJulian Elischer NG_NODE_REF(node); 872069154d5SJulian Elischer mtx_exit(&ng_nodelist_mtx, MTX_DEF); 8734cf49a43SJulian Elischer return (node); 8744cf49a43SJulian Elischer } 8754cf49a43SJulian Elischer 8764cf49a43SJulian Elischer /* 877dc90cad9SJulian Elischer * Decode a ID name, eg. "[f03034de]". Returns 0 if the 878dc90cad9SJulian Elischer * string is not valid, otherwise returns the value. 8794cf49a43SJulian Elischer */ 880dc90cad9SJulian Elischer static ng_ID_t 8814cf49a43SJulian Elischer ng_decodeidname(const char *name) 8824cf49a43SJulian Elischer { 8832b70adcbSArchie Cobbs const int len = strlen(name); 88425792ef3SArchie Cobbs char *eptr; 8852b70adcbSArchie Cobbs u_long val; 8864cf49a43SJulian Elischer 8872b70adcbSArchie Cobbs /* Check for proper length, brackets, no leading junk */ 88870de87f2SJulian Elischer if ((len < 3) 88970de87f2SJulian Elischer || (name[0] != '[') 89070de87f2SJulian Elischer || (name[len - 1] != ']') 89170de87f2SJulian Elischer || (!isxdigit(name[1]))) { 89270de87f2SJulian Elischer return ((ng_ID_t)0); 89370de87f2SJulian Elischer } 8944cf49a43SJulian Elischer 8952b70adcbSArchie Cobbs /* Decode number */ 8962b70adcbSArchie Cobbs val = strtoul(name + 1, &eptr, 16); 89770de87f2SJulian Elischer if ((eptr - name != len - 1) 89870de87f2SJulian Elischer || (val == ULONG_MAX) 89970de87f2SJulian Elischer || (val == 0)) { 90012f035e0SJulian Elischer return ((ng_ID_t)0); 90170de87f2SJulian Elischer } 9022b70adcbSArchie Cobbs return (ng_ID_t)val; 9034cf49a43SJulian Elischer } 9044cf49a43SJulian Elischer 9054cf49a43SJulian Elischer /* 9064cf49a43SJulian Elischer * Remove a name from a node. This should only be called 9074cf49a43SJulian Elischer * when shutting down and removing the node. 9084cf49a43SJulian Elischer */ 9094cf49a43SJulian Elischer void 9104cf49a43SJulian Elischer ng_unname(node_p node) 9114cf49a43SJulian Elischer { 91230400f03SJulian Elischer bzero(NG_NODE_NAME(node), NG_NODELEN); 9134cf49a43SJulian Elischer } 9144cf49a43SJulian Elischer 9154cf49a43SJulian Elischer /************************************************************************ 9164cf49a43SJulian Elischer Hook routines 9174cf49a43SJulian Elischer Names are not optional. Hooks are always connected, except for a 9184cf49a43SJulian Elischer brief moment within these routines. 9194cf49a43SJulian Elischer ************************************************************************/ 9204cf49a43SJulian Elischer 9214cf49a43SJulian Elischer /* 9224cf49a43SJulian Elischer * Remove a hook reference 9234cf49a43SJulian Elischer */ 92430400f03SJulian Elischer void 9254cf49a43SJulian Elischer ng_unref_hook(hook_p hook) 9264cf49a43SJulian Elischer { 92730400f03SJulian Elischer int v; 92830400f03SJulian Elischer do { 92930400f03SJulian Elischer v = hook->hk_refs; 93030400f03SJulian Elischer } while (! atomic_cmpset_int(&hook->hk_refs, v, v - 1)); 931e8a49db2SJulian Elischer 93230400f03SJulian Elischer if (v == 1) { /* we were the last */ 93330400f03SJulian Elischer if (NG_HOOK_NODE(hook)) { 93430400f03SJulian Elischer NG_NODE_UNREF((NG_HOOK_NODE(hook))); 93530400f03SJulian Elischer hook->hk_node = NULL; 936069154d5SJulian Elischer } 937069154d5SJulian Elischer NG_FREE_HOOK(hook); 938069154d5SJulian Elischer } 9394cf49a43SJulian Elischer } 9404cf49a43SJulian Elischer 9414cf49a43SJulian Elischer /* 9424cf49a43SJulian Elischer * Add an unconnected hook to a node. Only used internally. 9434cf49a43SJulian Elischer */ 9444cf49a43SJulian Elischer static int 9454cf49a43SJulian Elischer ng_add_hook(node_p node, const char *name, hook_p *hookp) 9464cf49a43SJulian Elischer { 9474cf49a43SJulian Elischer hook_p hook; 9484cf49a43SJulian Elischer int error = 0; 9494cf49a43SJulian Elischer 9504cf49a43SJulian Elischer /* Check that the given name is good */ 9514cf49a43SJulian Elischer if (name == NULL) { 9524cf49a43SJulian Elischer TRAP_ERROR; 9534cf49a43SJulian Elischer return (EINVAL); 9544cf49a43SJulian Elischer } 955899e9c4eSArchie Cobbs if (ng_findhook(node, name) != NULL) { 9564cf49a43SJulian Elischer TRAP_ERROR; 9574cf49a43SJulian Elischer return (EEXIST); 9584cf49a43SJulian Elischer } 9594cf49a43SJulian Elischer 9604cf49a43SJulian Elischer /* Allocate the hook and link it up */ 96130400f03SJulian Elischer NG_ALLOC_HOOK(hook); 9624cf49a43SJulian Elischer if (hook == NULL) { 9634cf49a43SJulian Elischer TRAP_ERROR; 9644cf49a43SJulian Elischer return (ENOMEM); 9654cf49a43SJulian Elischer } 96630400f03SJulian Elischer hook->hk_refs = 1; 96730400f03SJulian Elischer hook->hk_flags = HK_INVALID; 96830400f03SJulian Elischer hook->hk_node = node; 96930400f03SJulian Elischer NG_NODE_REF(node); /* each hook counts as a reference */ 9704cf49a43SJulian Elischer 9714cf49a43SJulian Elischer /* Check if the node type code has something to say about it */ 972954c4772SJulian Elischer if (node->nd_type->newhook != NULL) { 973954c4772SJulian Elischer if ((error = (*node->nd_type->newhook)(node, hook, name))) { 97430400f03SJulian Elischer NG_HOOK_UNREF(hook); /* this frees the hook */ 975069154d5SJulian Elischer return (error); 976069154d5SJulian Elischer } 977954c4772SJulian Elischer } 9784cf49a43SJulian Elischer /* 9794cf49a43SJulian Elischer * The 'type' agrees so far, so go ahead and link it in. 9804cf49a43SJulian Elischer * We'll ask again later when we actually connect the hooks. 981069154d5SJulian Elischer * The reference we have is for this linkage. 9824cf49a43SJulian Elischer */ 98330400f03SJulian Elischer LIST_INSERT_HEAD(&node->nd_hooks, hook, hk_hooks); 98430400f03SJulian Elischer node->nd_numhooks++; 9854cf49a43SJulian Elischer 9864cf49a43SJulian Elischer /* Set hook name */ 98730400f03SJulian Elischer strncpy(NG_HOOK_NAME(hook), name, NG_HOOKLEN); 9884cf49a43SJulian Elischer if (hookp) 9894cf49a43SJulian Elischer *hookp = hook; 9904cf49a43SJulian Elischer return (error); 9914cf49a43SJulian Elischer } 9924cf49a43SJulian Elischer 9934cf49a43SJulian Elischer /* 9944cf49a43SJulian Elischer * Connect a pair of hooks. Only used internally. 9954cf49a43SJulian Elischer */ 9964cf49a43SJulian Elischer static int 9974cf49a43SJulian Elischer ng_connect(hook_p hook1, hook_p hook2) 9984cf49a43SJulian Elischer { 9994cf49a43SJulian Elischer int error; 10004cf49a43SJulian Elischer 100130400f03SJulian Elischer hook1->hk_peer = hook2; 100230400f03SJulian Elischer hook2->hk_peer = hook1; 10034cf49a43SJulian Elischer 10044cf49a43SJulian Elischer /* Give each node the opportunity to veto the impending connection */ 100530400f03SJulian Elischer if (hook1->hk_node->nd_type->connect) { 100630400f03SJulian Elischer if ((error = (*hook1->hk_node->nd_type->connect) (hook1))) { 10074cf49a43SJulian Elischer ng_destroy_hook(hook1); /* also zaps hook2 */ 10084cf49a43SJulian Elischer return (error); 10094cf49a43SJulian Elischer } 10104cf49a43SJulian Elischer } 101130400f03SJulian Elischer if (hook2->hk_node->nd_type->connect) { 101230400f03SJulian Elischer if ((error = (*hook2->hk_node->nd_type->connect) (hook2))) { 10134cf49a43SJulian Elischer ng_destroy_hook(hook2); /* also zaps hook1 */ 10144cf49a43SJulian Elischer return (error); 10154cf49a43SJulian Elischer } 10164cf49a43SJulian Elischer } 101730400f03SJulian Elischer hook1->hk_flags &= ~HK_INVALID; 101830400f03SJulian Elischer hook2->hk_flags &= ~HK_INVALID; 10194cf49a43SJulian Elischer return (0); 10204cf49a43SJulian Elischer } 10214cf49a43SJulian Elischer 10224cf49a43SJulian Elischer /* 1023899e9c4eSArchie Cobbs * Find a hook 1024899e9c4eSArchie Cobbs * 1025899e9c4eSArchie Cobbs * Node types may supply their own optimized routines for finding 1026899e9c4eSArchie Cobbs * hooks. If none is supplied, we just do a linear search. 1027899e9c4eSArchie Cobbs */ 1028899e9c4eSArchie Cobbs hook_p 1029899e9c4eSArchie Cobbs ng_findhook(node_p node, const char *name) 1030899e9c4eSArchie Cobbs { 1031899e9c4eSArchie Cobbs hook_p hook; 1032899e9c4eSArchie Cobbs 103330400f03SJulian Elischer if (node->nd_type->findhook != NULL) 103430400f03SJulian Elischer return (*node->nd_type->findhook)(node, name); 103530400f03SJulian Elischer LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) { 103670de87f2SJulian Elischer if (NG_HOOK_IS_VALID(hook) 1037ce5e5f99SArchie Cobbs && (strcmp(NG_HOOK_NAME(hook), name) == 0)) 1038899e9c4eSArchie Cobbs return (hook); 1039899e9c4eSArchie Cobbs } 1040899e9c4eSArchie Cobbs return (NULL); 1041899e9c4eSArchie Cobbs } 1042899e9c4eSArchie Cobbs 1043899e9c4eSArchie Cobbs /* 10444cf49a43SJulian Elischer * Destroy a hook 10454cf49a43SJulian Elischer * 10464cf49a43SJulian Elischer * As hooks are always attached, this really destroys two hooks. 10474cf49a43SJulian Elischer * The one given, and the one attached to it. Disconnect the hooks 10484cf49a43SJulian Elischer * from each other first. 10494cf49a43SJulian Elischer */ 10504cf49a43SJulian Elischer void 10514cf49a43SJulian Elischer ng_destroy_hook(hook_p hook) 10524cf49a43SJulian Elischer { 105330400f03SJulian Elischer hook_p peer = NG_HOOK_PEER(hook); 10544cf49a43SJulian Elischer 105530400f03SJulian Elischer hook->hk_flags |= HK_INVALID; /* as soon as possible */ 10564cf49a43SJulian Elischer if (peer) { 105730400f03SJulian Elischer peer->hk_flags |= HK_INVALID; /* as soon as possible */ 105830400f03SJulian Elischer hook->hk_peer = NULL; 105930400f03SJulian Elischer peer->hk_peer = NULL; 10604cf49a43SJulian Elischer ng_disconnect_hook(peer); 10614cf49a43SJulian Elischer } 10624cf49a43SJulian Elischer ng_disconnect_hook(hook); 10634cf49a43SJulian Elischer } 10644cf49a43SJulian Elischer 10654cf49a43SJulian Elischer /* 10664cf49a43SJulian Elischer * Notify the node of the hook's demise. This may result in more actions 10674cf49a43SJulian Elischer * (e.g. shutdown) but we don't do that ourselves and don't know what 10684cf49a43SJulian Elischer * happens there. If there is no appropriate handler, then just remove it 10694cf49a43SJulian Elischer * (and decrement the reference count of it's node which in turn might 10704cf49a43SJulian Elischer * make something happen). 10714cf49a43SJulian Elischer */ 10724cf49a43SJulian Elischer static void 10734cf49a43SJulian Elischer ng_disconnect_hook(hook_p hook) 10744cf49a43SJulian Elischer { 107530400f03SJulian Elischer node_p node = NG_HOOK_NODE(hook); 10764cf49a43SJulian Elischer 10774cf49a43SJulian Elischer /* 10784cf49a43SJulian Elischer * Remove the hook from the node's list to avoid possible recursion 10794cf49a43SJulian Elischer * in case the disconnection results in node shutdown. 10804cf49a43SJulian Elischer */ 108130400f03SJulian Elischer LIST_REMOVE(hook, hk_hooks); 108230400f03SJulian Elischer node->nd_numhooks--; 108330400f03SJulian Elischer if (node->nd_type->disconnect) { 10844cf49a43SJulian Elischer /* 10854cf49a43SJulian Elischer * The type handler may elect to destroy the peer so don't 10864cf49a43SJulian Elischer * trust its existance after this point. 10874cf49a43SJulian Elischer */ 108830400f03SJulian Elischer (*node->nd_type->disconnect) (hook); 10894cf49a43SJulian Elischer } 109030400f03SJulian Elischer NG_HOOK_UNREF(hook); 10914cf49a43SJulian Elischer } 10924cf49a43SJulian Elischer 10934cf49a43SJulian Elischer /* 10944cf49a43SJulian Elischer * Take two hooks on a node and merge the connection so that the given node 10954cf49a43SJulian Elischer * is effectively bypassed. 10964cf49a43SJulian Elischer */ 10974cf49a43SJulian Elischer int 10984cf49a43SJulian Elischer ng_bypass(hook_p hook1, hook_p hook2) 10994cf49a43SJulian Elischer { 110030400f03SJulian Elischer if (hook1->hk_node != hook2->hk_node) { 110130400f03SJulian Elischer TRAP_ERROR 11024cf49a43SJulian Elischer return (EINVAL); 110330400f03SJulian Elischer } 110430400f03SJulian Elischer hook1->hk_peer->hk_peer = hook2->hk_peer; 110530400f03SJulian Elischer hook2->hk_peer->hk_peer = hook1->hk_peer; 11064cf49a43SJulian Elischer 11074cf49a43SJulian Elischer /* XXX If we ever cache methods on hooks update them as well */ 110830400f03SJulian Elischer hook1->hk_peer = NULL; 110930400f03SJulian Elischer hook2->hk_peer = NULL; 11104cf49a43SJulian Elischer ng_destroy_hook(hook1); 11114cf49a43SJulian Elischer ng_destroy_hook(hook2); 11124cf49a43SJulian Elischer return (0); 11134cf49a43SJulian Elischer } 11144cf49a43SJulian Elischer 11154cf49a43SJulian Elischer /* 11164cf49a43SJulian Elischer * Install a new netgraph type 11174cf49a43SJulian Elischer */ 11184cf49a43SJulian Elischer int 11194cf49a43SJulian Elischer ng_newtype(struct ng_type *tp) 11204cf49a43SJulian Elischer { 11214cf49a43SJulian Elischer const size_t namelen = strlen(tp->name); 11224cf49a43SJulian Elischer 11234cf49a43SJulian Elischer /* Check version and type name fields */ 1124589f6ed8SJulian Elischer if ((tp->version != NG_ABI_VERSION) 1125589f6ed8SJulian Elischer || (namelen == 0) 1126589f6ed8SJulian Elischer || (namelen > NG_TYPELEN)) { 11274cf49a43SJulian Elischer TRAP_ERROR; 11284cf49a43SJulian Elischer return (EINVAL); 11294cf49a43SJulian Elischer } 11304cf49a43SJulian Elischer 11314cf49a43SJulian Elischer /* Check for name collision */ 11324cf49a43SJulian Elischer if (ng_findtype(tp->name) != NULL) { 11334cf49a43SJulian Elischer TRAP_ERROR; 11344cf49a43SJulian Elischer return (EEXIST); 11354cf49a43SJulian Elischer } 11364cf49a43SJulian Elischer 11374cf49a43SJulian Elischer tp->refs = 0; 1138069154d5SJulian Elischer 1139069154d5SJulian Elischer /* Link in new type */ 1140069154d5SJulian Elischer mtx_enter(&ng_typelist_mtx, MTX_DEF); 1141069154d5SJulian Elischer LIST_INSERT_HEAD(&ng_typelist, tp, types); 1142069154d5SJulian Elischer mtx_exit(&ng_typelist_mtx, MTX_DEF); 11434cf49a43SJulian Elischer return (0); 11444cf49a43SJulian Elischer } 11454cf49a43SJulian Elischer 11464cf49a43SJulian Elischer /* 11474cf49a43SJulian Elischer * Look for a type of the name given 11484cf49a43SJulian Elischer */ 11494cf49a43SJulian Elischer struct ng_type * 11504cf49a43SJulian Elischer ng_findtype(const char *typename) 11514cf49a43SJulian Elischer { 11524cf49a43SJulian Elischer struct ng_type *type; 11534cf49a43SJulian Elischer 1154069154d5SJulian Elischer mtx_enter(&ng_typelist_mtx, MTX_DEF); 1155069154d5SJulian Elischer LIST_FOREACH(type, &ng_typelist, types) { 11564cf49a43SJulian Elischer if (strcmp(type->name, typename) == 0) 11574cf49a43SJulian Elischer break; 11584cf49a43SJulian Elischer } 1159069154d5SJulian Elischer mtx_exit(&ng_typelist_mtx, MTX_DEF); 11604cf49a43SJulian Elischer return (type); 11614cf49a43SJulian Elischer } 11624cf49a43SJulian Elischer 11634cf49a43SJulian Elischer /************************************************************************ 11644cf49a43SJulian Elischer Composite routines 11654cf49a43SJulian Elischer ************************************************************************/ 11664cf49a43SJulian Elischer 11674cf49a43SJulian Elischer /* 11684cf49a43SJulian Elischer * Make a peer and connect. The order is arranged to minimise 11694cf49a43SJulian Elischer * the work needed to back out in case of error. 11704cf49a43SJulian Elischer */ 11714cf49a43SJulian Elischer int 11724cf49a43SJulian Elischer ng_mkpeer(node_p node, const char *name, const char *name2, char *type) 11734cf49a43SJulian Elischer { 11744cf49a43SJulian Elischer node_p node2; 11754cf49a43SJulian Elischer hook_p hook; 11764cf49a43SJulian Elischer hook_p hook2; 11774cf49a43SJulian Elischer int error; 11784cf49a43SJulian Elischer 11794cf49a43SJulian Elischer if ((error = ng_add_hook(node, name, &hook))) 11804cf49a43SJulian Elischer return (error); 11814cf49a43SJulian Elischer if ((error = ng_make_node(type, &node2))) { 11824cf49a43SJulian Elischer ng_destroy_hook(hook); 11834cf49a43SJulian Elischer return (error); 11844cf49a43SJulian Elischer } 11854cf49a43SJulian Elischer if ((error = ng_add_hook(node2, name2, &hook2))) { 11864cf49a43SJulian Elischer ng_rmnode(node2); 11874cf49a43SJulian Elischer ng_destroy_hook(hook); 11884cf49a43SJulian Elischer return (error); 11894cf49a43SJulian Elischer } 11904cf49a43SJulian Elischer 11914cf49a43SJulian Elischer /* 11924cf49a43SJulian Elischer * Actually link the two hooks together.. on failure they are 11934cf49a43SJulian Elischer * destroyed so we don't have to do that here. 11944cf49a43SJulian Elischer */ 11954cf49a43SJulian Elischer if ((error = ng_connect(hook, hook2))) 11964cf49a43SJulian Elischer ng_rmnode(node2); 11974cf49a43SJulian Elischer return (error); 11984cf49a43SJulian Elischer } 11994cf49a43SJulian Elischer 12004cf49a43SJulian Elischer /* 12014cf49a43SJulian Elischer * Connect two nodes using the specified hooks 12024cf49a43SJulian Elischer */ 12034cf49a43SJulian Elischer int 12044cf49a43SJulian Elischer ng_con_nodes(node_p node, const char *name, node_p node2, const char *name2) 12054cf49a43SJulian Elischer { 12064cf49a43SJulian Elischer int error; 12074cf49a43SJulian Elischer hook_p hook; 12084cf49a43SJulian Elischer hook_p hook2; 12094cf49a43SJulian Elischer 12104cf49a43SJulian Elischer if ((error = ng_add_hook(node, name, &hook))) 12114cf49a43SJulian Elischer return (error); 12124cf49a43SJulian Elischer if ((error = ng_add_hook(node2, name2, &hook2))) { 12134cf49a43SJulian Elischer ng_destroy_hook(hook); 12144cf49a43SJulian Elischer return (error); 12154cf49a43SJulian Elischer } 12164cf49a43SJulian Elischer return (ng_connect(hook, hook2)); 12174cf49a43SJulian Elischer } 1218069154d5SJulian Elischer /************************************************************************ 1219069154d5SJulian Elischer Utility routines to send self messages 1220069154d5SJulian Elischer ************************************************************************/ 1221069154d5SJulian Elischer /* 1222069154d5SJulian Elischer * Static version of shutdown message. we don't want to need resources 1223069154d5SJulian Elischer * to shut down (we may be doing it to release resources because we ran out. 1224069154d5SJulian Elischer */ 1225069154d5SJulian Elischer static struct ng_mesg ng_msg_shutdown = { 1226069154d5SJulian Elischer {NG_VERSION, /* u_char */ 1227069154d5SJulian Elischer 0, /* u_char spare */ 1228069154d5SJulian Elischer 0, /* u_int16_t arglen */ 1229069154d5SJulian Elischer NGF_STATIC, /* u_int32_t flags */ 1230069154d5SJulian Elischer 0, /* u_int32_t token */ 1231069154d5SJulian Elischer NGM_GENERIC_COOKIE, /* u_int32_t */ 1232069154d5SJulian Elischer NGM_SHUTDOWN, /* u_int32_t */ 1233069154d5SJulian Elischer "shutdown"} /* u_char[16] */ 1234069154d5SJulian Elischer }; 1235069154d5SJulian Elischer 1236069154d5SJulian Elischer int 1237069154d5SJulian Elischer ng_rmnode_self(node_p here) 1238069154d5SJulian Elischer { 1239069154d5SJulian Elischer item_p item; 1240069154d5SJulian Elischer struct ng_mesg *msg; 12414cf49a43SJulian Elischer 12424cf49a43SJulian Elischer /* 1243069154d5SJulian Elischer * Use the static version to avoid needing 1244069154d5SJulian Elischer * memory allocation to succeed. 1245069154d5SJulian Elischer * The message is never written to and always the same. 1246069154d5SJulian Elischer */ 1247069154d5SJulian Elischer msg = &ng_msg_shutdown; 1248069154d5SJulian Elischer 1249069154d5SJulian Elischer /* 1250069154d5SJulian Elischer * Try get a queue item to send it with. 1251069154d5SJulian Elischer * Hopefully since it has a reserve, we can get one. 1252069154d5SJulian Elischer * If we can't we are screwed anyhow. 1253069154d5SJulian Elischer * Increase the chances by flushing our queue first. 1254069154d5SJulian Elischer * We may free an item, (if we were the hog). 1255069154d5SJulian Elischer * Work in progress is allowed to complete. 1256069154d5SJulian Elischer * We also pretty much ensure that we come straight 1257069154d5SJulian Elischer * back in to do the shutdown. It may be a good idea 1258069154d5SJulian Elischer * to hold a reference actually to stop it from all 1259069154d5SJulian Elischer * going up in smoke. 1260069154d5SJulian Elischer */ 126130400f03SJulian Elischer /* ng_flush_input_queue(&here->nd_input_queue); will mask problem */ 1262069154d5SJulian Elischer item = ng_package_msg_self(here, NULL, msg); 1263069154d5SJulian Elischer if (item == NULL) { /* it would have freed the msg except static */ 1264069154d5SJulian Elischer /* try again after flushing our queue */ 126530400f03SJulian Elischer ng_flush_input_queue(&here->nd_input_queue); 1266069154d5SJulian Elischer item = ng_package_msg_self(here, NULL, msg); 1267069154d5SJulian Elischer if (item == NULL) { 1268069154d5SJulian Elischer printf("failed to free node 0x%x\n", ng_node2ID(here)); 1269069154d5SJulian Elischer return (ENOMEM); 1270069154d5SJulian Elischer } 1271069154d5SJulian Elischer } 1272069154d5SJulian Elischer return (ng_snd_item(item, 0)); 1273069154d5SJulian Elischer } 1274069154d5SJulian Elischer 1275954c4772SJulian Elischer #define NG_INTERNAL_RMHOOK 0x123456 1276954c4772SJulian Elischer int 1277954c4772SJulian Elischer ng_rmhook_self(hook_p hook) 1278954c4772SJulian Elischer { 1279954c4772SJulian Elischer item_p item; 1280954c4772SJulian Elischer struct ng_mesg *msg; 1281954c4772SJulian Elischer node_p node = NG_HOOK_NODE(hook); 1282954c4772SJulian Elischer 1283954c4772SJulian Elischer NG_MKMESSAGE(msg, NGM_GENERIC_COOKIE, NG_INTERNAL_RMHOOK, 0, M_NOWAIT); 1284954c4772SJulian Elischer /* 1285954c4772SJulian Elischer * Try get a queue item to send it with. 1286954c4772SJulian Elischer * Hopefully since it has a reserve, we can get one. 1287954c4772SJulian Elischer * If we can't we are screwed anyhow. 1288954c4772SJulian Elischer * Increase the chances by flushing our queue first. 1289954c4772SJulian Elischer * We may free an item, (if we were the hog). 1290954c4772SJulian Elischer * Work in progress is allowed to complete. 1291954c4772SJulian Elischer * We also pretty much ensure that we come straight 1292954c4772SJulian Elischer * back in to do the shutdown. It may be a good idea 1293954c4772SJulian Elischer * to hold a reference actually to stop it from all 1294954c4772SJulian Elischer * going up in smoke. 1295954c4772SJulian Elischer */ 1296954c4772SJulian Elischer item = ng_package_msg_self(node, hook, msg); 1297954c4772SJulian Elischer if (item == NULL) { /* couldn't allocate item. Freed msg */ 1298954c4772SJulian Elischer /* try again after flushing our queue */ 1299954c4772SJulian Elischer panic("Couldn't allocate item to remove hook"); 1300954c4772SJulian Elischer } 1301954c4772SJulian Elischer return (ng_snd_item(item, 0)); 1302954c4772SJulian Elischer } 1303954c4772SJulian Elischer 1304069154d5SJulian Elischer /*********************************************************************** 13054cf49a43SJulian Elischer * Parse and verify a string of the form: <NODE:><PATH> 13064cf49a43SJulian Elischer * 13074cf49a43SJulian Elischer * Such a string can refer to a specific node or a specific hook 13084cf49a43SJulian Elischer * on a specific node, depending on how you look at it. In the 13094cf49a43SJulian Elischer * latter case, the PATH component must not end in a dot. 13104cf49a43SJulian Elischer * 13114cf49a43SJulian Elischer * Both <NODE:> and <PATH> are optional. The <PATH> is a string 13124cf49a43SJulian Elischer * of hook names separated by dots. This breaks out the original 13134cf49a43SJulian Elischer * string, setting *nodep to "NODE" (or NULL if none) and *pathp 13144cf49a43SJulian Elischer * to "PATH" (or NULL if degenerate). Also, *hookp will point to 13154cf49a43SJulian Elischer * the final hook component of <PATH>, if any, otherwise NULL. 13164cf49a43SJulian Elischer * 13174cf49a43SJulian Elischer * This returns -1 if the path is malformed. The char ** are optional. 1318069154d5SJulian Elischer ***********************************************************************/ 13194cf49a43SJulian Elischer int 13204cf49a43SJulian Elischer ng_path_parse(char *addr, char **nodep, char **pathp, char **hookp) 13214cf49a43SJulian Elischer { 13224cf49a43SJulian Elischer char *node, *path, *hook; 13234cf49a43SJulian Elischer int k; 13244cf49a43SJulian Elischer 13254cf49a43SJulian Elischer /* 13264cf49a43SJulian Elischer * Extract absolute NODE, if any 13274cf49a43SJulian Elischer */ 13284cf49a43SJulian Elischer for (path = addr; *path && *path != ':'; path++); 13294cf49a43SJulian Elischer if (*path) { 13304cf49a43SJulian Elischer node = addr; /* Here's the NODE */ 13314cf49a43SJulian Elischer *path++ = '\0'; /* Here's the PATH */ 13324cf49a43SJulian Elischer 13334cf49a43SJulian Elischer /* Node name must not be empty */ 13344cf49a43SJulian Elischer if (!*node) 13354cf49a43SJulian Elischer return -1; 13364cf49a43SJulian Elischer 13374cf49a43SJulian Elischer /* A name of "." is OK; otherwise '.' not allowed */ 13384cf49a43SJulian Elischer if (strcmp(node, ".") != 0) { 13394cf49a43SJulian Elischer for (k = 0; node[k]; k++) 13404cf49a43SJulian Elischer if (node[k] == '.') 13414cf49a43SJulian Elischer return -1; 13424cf49a43SJulian Elischer } 13434cf49a43SJulian Elischer } else { 13444cf49a43SJulian Elischer node = NULL; /* No absolute NODE */ 13454cf49a43SJulian Elischer path = addr; /* Here's the PATH */ 13464cf49a43SJulian Elischer } 13474cf49a43SJulian Elischer 13484cf49a43SJulian Elischer /* Snoop for illegal characters in PATH */ 13494cf49a43SJulian Elischer for (k = 0; path[k]; k++) 13504cf49a43SJulian Elischer if (path[k] == ':') 13514cf49a43SJulian Elischer return -1; 13524cf49a43SJulian Elischer 13534cf49a43SJulian Elischer /* Check for no repeated dots in PATH */ 13544cf49a43SJulian Elischer for (k = 0; path[k]; k++) 13554cf49a43SJulian Elischer if (path[k] == '.' && path[k + 1] == '.') 13564cf49a43SJulian Elischer return -1; 13574cf49a43SJulian Elischer 13584cf49a43SJulian Elischer /* Remove extra (degenerate) dots from beginning or end of PATH */ 13594cf49a43SJulian Elischer if (path[0] == '.') 13604cf49a43SJulian Elischer path++; 13614cf49a43SJulian Elischer if (*path && path[strlen(path) - 1] == '.') 13624cf49a43SJulian Elischer path[strlen(path) - 1] = 0; 13634cf49a43SJulian Elischer 13644cf49a43SJulian Elischer /* If PATH has a dot, then we're not talking about a hook */ 13654cf49a43SJulian Elischer if (*path) { 13664cf49a43SJulian Elischer for (hook = path, k = 0; path[k]; k++) 13674cf49a43SJulian Elischer if (path[k] == '.') { 13684cf49a43SJulian Elischer hook = NULL; 13694cf49a43SJulian Elischer break; 13704cf49a43SJulian Elischer } 13714cf49a43SJulian Elischer } else 13724cf49a43SJulian Elischer path = hook = NULL; 13734cf49a43SJulian Elischer 13744cf49a43SJulian Elischer /* Done */ 13754cf49a43SJulian Elischer if (nodep) 13764cf49a43SJulian Elischer *nodep = node; 13774cf49a43SJulian Elischer if (pathp) 13784cf49a43SJulian Elischer *pathp = path; 13794cf49a43SJulian Elischer if (hookp) 13804cf49a43SJulian Elischer *hookp = hook; 13814cf49a43SJulian Elischer return (0); 13824cf49a43SJulian Elischer } 13834cf49a43SJulian Elischer 13844cf49a43SJulian Elischer /* 13854cf49a43SJulian Elischer * Given a path, which may be absolute or relative, and a starting node, 1386069154d5SJulian Elischer * return the destination node. 13874cf49a43SJulian Elischer */ 13884cf49a43SJulian Elischer int 1389069154d5SJulian Elischer ng_path2noderef(node_p here, const char *address, 1390069154d5SJulian Elischer node_p *destp, hook_p *lasthook) 13914cf49a43SJulian Elischer { 13924cf49a43SJulian Elischer char fullpath[NG_PATHLEN + 1]; 13934cf49a43SJulian Elischer char *nodename, *path, pbuf[2]; 1394069154d5SJulian Elischer node_p node, oldnode; 13954cf49a43SJulian Elischer char *cp; 1396a4ec03cfSJulian Elischer hook_p hook = NULL; 13974cf49a43SJulian Elischer 13984cf49a43SJulian Elischer /* Initialize */ 139930400f03SJulian Elischer if (destp == NULL) { 140030400f03SJulian Elischer TRAP_ERROR 14014cf49a43SJulian Elischer return EINVAL; 140230400f03SJulian Elischer } 14034cf49a43SJulian Elischer *destp = NULL; 14044cf49a43SJulian Elischer 14054cf49a43SJulian Elischer /* Make a writable copy of address for ng_path_parse() */ 14064cf49a43SJulian Elischer strncpy(fullpath, address, sizeof(fullpath) - 1); 14074cf49a43SJulian Elischer fullpath[sizeof(fullpath) - 1] = '\0'; 14084cf49a43SJulian Elischer 14094cf49a43SJulian Elischer /* Parse out node and sequence of hooks */ 14104cf49a43SJulian Elischer if (ng_path_parse(fullpath, &nodename, &path, NULL) < 0) { 14114cf49a43SJulian Elischer TRAP_ERROR; 14124cf49a43SJulian Elischer return EINVAL; 14134cf49a43SJulian Elischer } 14144cf49a43SJulian Elischer if (path == NULL) { 14154cf49a43SJulian Elischer pbuf[0] = '.'; /* Needs to be writable */ 14164cf49a43SJulian Elischer pbuf[1] = '\0'; 14174cf49a43SJulian Elischer path = pbuf; 14184cf49a43SJulian Elischer } 14194cf49a43SJulian Elischer 1420069154d5SJulian Elischer /* 1421069154d5SJulian Elischer * For an absolute address, jump to the starting node. 1422069154d5SJulian Elischer * Note that this holds a reference on the node for us. 1423069154d5SJulian Elischer * Don't forget to drop the reference if we don't need it. 1424069154d5SJulian Elischer */ 14254cf49a43SJulian Elischer if (nodename) { 1426069154d5SJulian Elischer node = ng_name2noderef(here, nodename); 14274cf49a43SJulian Elischer if (node == NULL) { 142830400f03SJulian Elischer TRAP_ERROR 14294cf49a43SJulian Elischer return (ENOENT); 14304cf49a43SJulian Elischer } 1431069154d5SJulian Elischer } else { 1432069154d5SJulian Elischer if (here == NULL) { 1433069154d5SJulian Elischer TRAP_ERROR 1434069154d5SJulian Elischer return (EINVAL); 1435069154d5SJulian Elischer } 14364cf49a43SJulian Elischer node = here; 143730400f03SJulian Elischer NG_NODE_REF(node); 1438069154d5SJulian Elischer } 14394cf49a43SJulian Elischer 1440069154d5SJulian Elischer /* 1441069154d5SJulian Elischer * Now follow the sequence of hooks 1442069154d5SJulian Elischer * XXX 1443069154d5SJulian Elischer * We actually cannot guarantee that the sequence 1444069154d5SJulian Elischer * is not being demolished as we crawl along it 1445069154d5SJulian Elischer * without extra-ordinary locking etc. 1446069154d5SJulian Elischer * So this is a bit dodgy to say the least. 1447069154d5SJulian Elischer * We can probably hold up some things by holding 1448069154d5SJulian Elischer * the nodelist mutex for the time of this 1449069154d5SJulian Elischer * crawl if we wanted.. At least that way we wouldn't have to 1450069154d5SJulian Elischer * worry about the nodes dissappearing, but the hooks would still 1451069154d5SJulian Elischer * be a problem. 1452069154d5SJulian Elischer */ 14534cf49a43SJulian Elischer for (cp = path; node != NULL && *cp != '\0'; ) { 14544cf49a43SJulian Elischer char *segment; 14554cf49a43SJulian Elischer 14564cf49a43SJulian Elischer /* 14574cf49a43SJulian Elischer * Break out the next path segment. Replace the dot we just 14584cf49a43SJulian Elischer * found with a NUL; "cp" points to the next segment (or the 14594cf49a43SJulian Elischer * NUL at the end). 14604cf49a43SJulian Elischer */ 14614cf49a43SJulian Elischer for (segment = cp; *cp != '\0'; cp++) { 14624cf49a43SJulian Elischer if (*cp == '.') { 14634cf49a43SJulian Elischer *cp++ = '\0'; 14644cf49a43SJulian Elischer break; 14654cf49a43SJulian Elischer } 14664cf49a43SJulian Elischer } 14674cf49a43SJulian Elischer 14684cf49a43SJulian Elischer /* Empty segment */ 14694cf49a43SJulian Elischer if (*segment == '\0') 14704cf49a43SJulian Elischer continue; 14714cf49a43SJulian Elischer 14724cf49a43SJulian Elischer /* We have a segment, so look for a hook by that name */ 1473899e9c4eSArchie Cobbs hook = ng_findhook(node, segment); 14744cf49a43SJulian Elischer 14754cf49a43SJulian Elischer /* Can't get there from here... */ 14764cf49a43SJulian Elischer if (hook == NULL 147730400f03SJulian Elischer || NG_HOOK_PEER(hook) == NULL 147830400f03SJulian Elischer || NG_HOOK_NOT_VALID(hook) 147930400f03SJulian Elischer || NG_HOOK_NOT_VALID(NG_HOOK_PEER(hook))) { 14804cf49a43SJulian Elischer TRAP_ERROR; 148130400f03SJulian Elischer NG_NODE_UNREF(node); 148230400f03SJulian Elischer #if 0 148330400f03SJulian Elischer printf("hooknotvalid %s %s %d %d %d %d ", 148430400f03SJulian Elischer path, 148530400f03SJulian Elischer segment, 148630400f03SJulian Elischer hook == NULL, 148730400f03SJulian Elischer NG_HOOK_PEER(hook) == NULL, 148830400f03SJulian Elischer NG_HOOK_NOT_VALID(hook), 148930400f03SJulian Elischer NG_HOOK_NOT_VALID(NG_HOOK_PEER(hook))); 149030400f03SJulian Elischer #endif 14914cf49a43SJulian Elischer return (ENOENT); 14924cf49a43SJulian Elischer } 14934cf49a43SJulian Elischer 1494069154d5SJulian Elischer /* 1495069154d5SJulian Elischer * Hop on over to the next node 1496069154d5SJulian Elischer * XXX 1497069154d5SJulian Elischer * Big race conditions here as hooks and nodes go away 1498069154d5SJulian Elischer * *** Idea.. store an ng_ID_t in each hook and use that 1499069154d5SJulian Elischer * instead of the direct hook in this crawl? 1500069154d5SJulian Elischer */ 1501069154d5SJulian Elischer oldnode = node; 150230400f03SJulian Elischer if ((node = NG_PEER_NODE(hook))) 150330400f03SJulian Elischer NG_NODE_REF(node); /* XXX RACE */ 150430400f03SJulian Elischer NG_NODE_UNREF(oldnode); /* XXX another race */ 150530400f03SJulian Elischer if (NG_NODE_NOT_VALID(node)) { 150630400f03SJulian Elischer NG_NODE_UNREF(node); /* XXX more races */ 1507069154d5SJulian Elischer node = NULL; 1508069154d5SJulian Elischer } 15094cf49a43SJulian Elischer } 15104cf49a43SJulian Elischer 15114cf49a43SJulian Elischer /* If node somehow missing, fail here (probably this is not needed) */ 15124cf49a43SJulian Elischer if (node == NULL) { 15134cf49a43SJulian Elischer TRAP_ERROR; 15144cf49a43SJulian Elischer return (ENXIO); 15154cf49a43SJulian Elischer } 15164cf49a43SJulian Elischer 15174cf49a43SJulian Elischer /* Done */ 15184cf49a43SJulian Elischer *destp = node; 15191bdebe4dSArchie Cobbs if (lasthook != NULL) 152030400f03SJulian Elischer *lasthook = (hook ? NG_HOOK_PEER(hook) : NULL); 1521069154d5SJulian Elischer return (0); 1522069154d5SJulian Elischer } 1523069154d5SJulian Elischer 1524069154d5SJulian Elischer /***************************************************************\ 1525069154d5SJulian Elischer * Input queue handling. 1526069154d5SJulian Elischer * All activities are submitted to the node via the input queue 1527069154d5SJulian Elischer * which implements a multiple-reader/single-writer gate. 1528069154d5SJulian Elischer * Items which cannot be handled immeditly are queued. 1529069154d5SJulian Elischer * 1530069154d5SJulian Elischer * read-write queue locking inline functions * 1531069154d5SJulian Elischer \***************************************************************/ 1532069154d5SJulian Elischer 1533069154d5SJulian Elischer static __inline item_p ng_dequeue(struct ng_queue * ngq); 1534069154d5SJulian Elischer static __inline item_p ng_acquire_read(struct ng_queue * ngq, 1535069154d5SJulian Elischer item_p item); 1536069154d5SJulian Elischer static __inline item_p ng_acquire_write(struct ng_queue * ngq, 1537069154d5SJulian Elischer item_p item); 1538069154d5SJulian Elischer static __inline void ng_leave_read(struct ng_queue * ngq); 1539069154d5SJulian Elischer static __inline void ng_leave_write(struct ng_queue * ngq); 1540069154d5SJulian Elischer static __inline void ng_queue_rw(struct ng_queue * ngq, 1541069154d5SJulian Elischer item_p item, int rw); 1542069154d5SJulian Elischer 1543069154d5SJulian Elischer /* 1544069154d5SJulian Elischer * Definition of the bits fields in the ng_queue flag word. 1545069154d5SJulian Elischer * Defined here rather than in netgraph.h because no-one should fiddle 1546069154d5SJulian Elischer * with them. 1547069154d5SJulian Elischer * 1548069154d5SJulian Elischer * The ordering here is important! don't shuffle these. If you add 1549069154d5SJulian Elischer * READ_PENDING to the word when it has READ_PENDING already set, you 1550069154d5SJulian Elischer * generate a carry into the reader count, this you atomically add a reader, 1551069154d5SJulian Elischer * and remove the pending reader count! Similarly for the pending writer 1552069154d5SJulian Elischer * flag, adding WRITE_PENDING generates a carry and sets the WRITER_ACTIVE 1553069154d5SJulian Elischer * flag, while clearing WRITE_PENDING. When 'SINGLE_THREAD_ONLY' is set, then 1554069154d5SJulian Elischer * it is only permitted to do WRITER operations. Reader operations will 1555069154d5SJulian Elischer * result in errors. 1556069154d5SJulian Elischer * But that "hack" is unnecessary: "cpp" can do the math for us! 1557069154d5SJulian Elischer */ 1558069154d5SJulian Elischer /*- 1559069154d5SJulian Elischer Safety Barrier--------+ (adjustable to suit taste) (not used yet) 1560069154d5SJulian Elischer | 1561069154d5SJulian Elischer V 1562069154d5SJulian Elischer +-------+-------+-------+-------+-------+-------+-------+-------+ 1563069154d5SJulian Elischer | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 1564069154d5SJulian Elischer |A|c|t|i|v|e| |R|e|a|d|e|r| |C|o|u|n|t| | | | | | | | | |R|A|W|S| 1565069154d5SJulian Elischer | | | | | | | | | | | | | | | | | | | | | | | | | | | | |P|W|P|T| 1566069154d5SJulian Elischer +-------+-------+-------+-------+-------+-------+-------+-------+ 1567069154d5SJulian Elischer \_________________________ ____________________________/ | | | | 1568069154d5SJulian Elischer V | | | | 1569069154d5SJulian Elischer [active reader count] | | | | 1570069154d5SJulian Elischer | | | | 1571069154d5SJulian Elischer Read Pending ------------------------------------+ | | | 1572069154d5SJulian Elischer | | | 1573069154d5SJulian Elischer Active Writer -------------------------------------+ | | 1574069154d5SJulian Elischer | | 1575069154d5SJulian Elischer Write Pending ---------------------------------------+ | 1576069154d5SJulian Elischer | 1577069154d5SJulian Elischer Single Threading Only ---------------------------------+ 1578069154d5SJulian Elischer */ 1579069154d5SJulian Elischer #define SINGLE_THREAD_ONLY 0x00000001 /* if set, even reads single thread */ 1580069154d5SJulian Elischer #define WRITE_PENDING 0x00000002 1581069154d5SJulian Elischer #define WRITER_ACTIVE 0x00000004 1582069154d5SJulian Elischer #define READ_PENDING 0x00000008 1583069154d5SJulian Elischer #define READER_INCREMENT 0x00000010 1584069154d5SJulian Elischer #define READER_MASK 0xfffffff0 /* Not valid if WRITER_ACTIVE is set */ 1585069154d5SJulian Elischer #define SAFETY_BARRIER 0x00100000 /* 64K items queued should be enough */ 1586069154d5SJulian Elischer /* 1587069154d5SJulian Elischer * Taking into account the current state of the queue and node, possibly take 1588069154d5SJulian Elischer * the next entry off the queue and return it. Return NULL if there was 1589069154d5SJulian Elischer * nothing we could return, either because there really was nothing there, or 1590069154d5SJulian Elischer * because the node was in a state where it cannot yet process the next item 1591069154d5SJulian Elischer * on the queue. 1592069154d5SJulian Elischer * 1593069154d5SJulian Elischer * This MUST MUST MUST be called with the mutex held. 1594069154d5SJulian Elischer */ 1595069154d5SJulian Elischer static __inline item_p 1596069154d5SJulian Elischer ng_dequeue(struct ng_queue *ngq) 1597069154d5SJulian Elischer { 1598069154d5SJulian Elischer item_p item; 1599069154d5SJulian Elischer u_int add_arg; 1600069154d5SJulian Elischer /* 1601069154d5SJulian Elischer * If there is a writer, then the answer is "no". Everything else 1602069154d5SJulian Elischer * stops when there is a WRITER. 1603069154d5SJulian Elischer */ 1604069154d5SJulian Elischer if (ngq->q_flags & WRITER_ACTIVE) { 1605069154d5SJulian Elischer return (NULL); 1606069154d5SJulian Elischer } 1607069154d5SJulian Elischer /* Now take a look at what's on the queue and what's running */ 1608069154d5SJulian Elischer if ((ngq->q_flags & ~(READER_MASK | SINGLE_THREAD_ONLY)) == READ_PENDING) { 1609069154d5SJulian Elischer /* 1610069154d5SJulian Elischer * It was a reader and we have no write active. We don't care 1611069154d5SJulian Elischer * how many readers are already active. Adjust the count for 1612069154d5SJulian Elischer * the item we are about to dequeue. Adding READ_PENDING to 1613069154d5SJulian Elischer * the exisiting READ_PENDING clears it and generates a carry 1614069154d5SJulian Elischer * into the reader count. 1615069154d5SJulian Elischer */ 1616069154d5SJulian Elischer add_arg = READ_PENDING; 1617069154d5SJulian Elischer } else if ((ngq->q_flags & ~SINGLE_THREAD_ONLY) == WRITE_PENDING) { 1618069154d5SJulian Elischer /* 1619069154d5SJulian Elischer * There is a pending write, no readers and no active writer. 1620069154d5SJulian Elischer * This means we can go ahead with the pending writer. Note 1621069154d5SJulian Elischer * the fact that we now have a writer, ready for when we take 1622069154d5SJulian Elischer * it off the queue. 1623069154d5SJulian Elischer * 1624069154d5SJulian Elischer * We don't need to worry about a possible collision with the 1625069154d5SJulian Elischer * fasttrack reader. 1626069154d5SJulian Elischer * 1627069154d5SJulian Elischer * The fasttrack thread may take a long time to discover that we 1628069154d5SJulian Elischer * are running so we would have an inconsistent state in the 1629069154d5SJulian Elischer * flags for a while. Since we ignore the reader count 1630069154d5SJulian Elischer * entirely when the WRITER_ACTIVE flag is set, this should 1631069154d5SJulian Elischer * not matter (in fact it is defined that way). If it tests 1632069154d5SJulian Elischer * the flag before this operation, the WRITE_PENDING flag 1633069154d5SJulian Elischer * will make it fail, and if it tests it later, the 1634069154d5SJulian Elischer * ACTIVE_WRITER flag will do the same. If it is SO slow that 1635069154d5SJulian Elischer * we have actually completed the operation, and neither flag 1636069154d5SJulian Elischer * is set (nor the READ_PENDING) by the time that it tests 1637069154d5SJulian Elischer * the flags, then it is actually ok for it to continue. If 1638069154d5SJulian Elischer * it completes and we've finished and the read pending is 1639069154d5SJulian Elischer * set it still fails. 1640069154d5SJulian Elischer * 1641069154d5SJulian Elischer * So we can just ignore it, as long as we can ensure that the 1642069154d5SJulian Elischer * transition from WRITE_PENDING state to the WRITER_ACTIVE 1643069154d5SJulian Elischer * state is atomic. 1644069154d5SJulian Elischer * 1645069154d5SJulian Elischer * After failing, first it will be held back by the mutex, then 1646069154d5SJulian Elischer * when it can proceed, it will queue its request, then it 1647069154d5SJulian Elischer * would arrive at this function. Usually it will have to 1648069154d5SJulian Elischer * leave empty handed because the ACTIVE WRITER bit wil be 1649069154d5SJulian Elischer * set. 1650069154d5SJulian Elischer */ 1651069154d5SJulian Elischer /* 1652069154d5SJulian Elischer * Adjust the flags for the item we are about to dequeue. 1653069154d5SJulian Elischer * Adding WRITE_PENDING to the exisiting WRITE_PENDING clears 1654069154d5SJulian Elischer * it and generates a carry into the WRITER_ACTIVE flag, all 1655069154d5SJulian Elischer * atomically. 1656069154d5SJulian Elischer */ 1657069154d5SJulian Elischer add_arg = WRITE_PENDING; 1658069154d5SJulian Elischer /* 1659069154d5SJulian Elischer * We want to write "active writer, no readers " Now go make 1660069154d5SJulian Elischer * it true. In fact there may be a number in the readers 1661069154d5SJulian Elischer * count but we know it is not true and will be fixed soon. 1662069154d5SJulian Elischer * We will fix the flags for the next pending entry in a 1663069154d5SJulian Elischer * moment. 1664069154d5SJulian Elischer */ 1665069154d5SJulian Elischer } else { 1666069154d5SJulian Elischer /* 1667069154d5SJulian Elischer * We can't dequeue anything.. return and say so. Probably we 1668069154d5SJulian Elischer * have a write pending and the readers count is non zero. If 1669069154d5SJulian Elischer * we got here because a reader hit us just at the wrong 1670069154d5SJulian Elischer * moment with the fasttrack code, and put us in a strange 1671069154d5SJulian Elischer * state, then it will be through in just a moment, (as soon 1672069154d5SJulian Elischer * as we release the mutex) and keep things moving. 1673069154d5SJulian Elischer */ 16744cf49a43SJulian Elischer return (0); 16754cf49a43SJulian Elischer } 16764cf49a43SJulian Elischer 16774cf49a43SJulian Elischer /* 1678069154d5SJulian Elischer * Now we dequeue the request (whatever it may be) and correct the 1679069154d5SJulian Elischer * pending flags and the next and last pointers. 16804cf49a43SJulian Elischer */ 1681069154d5SJulian Elischer item = ngq->queue; 1682069154d5SJulian Elischer ngq->queue = item->el_next; 1683069154d5SJulian Elischer if (ngq->last == &(item->el_next)) { 16844cf49a43SJulian Elischer /* 1685069154d5SJulian Elischer * that was the last entry in the queue so set the 'last 1686069154d5SJulian Elischer * pointer up correctly and make sure the pending flags are 1687069154d5SJulian Elischer * clear. 16884cf49a43SJulian Elischer */ 1689069154d5SJulian Elischer ngq->last = &(ngq->queue); 1690859a4d16SJulian Elischer /* 1691069154d5SJulian Elischer * Whatever flag was set is cleared and the carry sets the 1692069154d5SJulian Elischer * correct new active state/count. So we don't need to change 1693069154d5SJulian Elischer * add_arg. 1694859a4d16SJulian Elischer */ 1695859a4d16SJulian Elischer } else { 1696069154d5SJulian Elischer if ((ngq->queue->el_flags & NGQF_TYPE) == NGQF_READER) { 1697069154d5SJulian Elischer /* 1698069154d5SJulian Elischer * If we had a READ_PENDING and have another one, we 1699069154d5SJulian Elischer * just want to add READ_PENDING twice (the same as 1700069154d5SJulian Elischer * adding READER_INCREMENT). If we had WRITE_PENDING, 1701069154d5SJulian Elischer * we want to add READ_PENDING + WRITE_PENDING to 1702069154d5SJulian Elischer * clear the old WRITE_PENDING, set ACTIVE_WRITER, 1703069154d5SJulian Elischer * and set READ_PENDING. Either way we just add 1704069154d5SJulian Elischer * READ_PENDING to whatever we already had. 1705069154d5SJulian Elischer */ 1706069154d5SJulian Elischer add_arg += READ_PENDING; 1707069154d5SJulian Elischer } else { 1708069154d5SJulian Elischer /* 1709069154d5SJulian Elischer * If we had a WRITE_PENDING and have another one, we 1710069154d5SJulian Elischer * just want to add WRITE_PENDING twice (the same as 1711069154d5SJulian Elischer * adding ACTIVE_WRITER). If we had READ_PENDING, we 1712069154d5SJulian Elischer * want to add READ_PENDING + WRITE_PENDING to clear 1713069154d5SJulian Elischer * the old READ_PENDING, increment the readers, and 1714069154d5SJulian Elischer * set WRITE_PENDING. Either way we just add 1715069154d5SJulian Elischer * WRITE_PENDING to whatever we already had. 1716069154d5SJulian Elischer */ 1717069154d5SJulian Elischer add_arg += WRITE_PENDING; 1718859a4d16SJulian Elischer } 1719859a4d16SJulian Elischer } 1720069154d5SJulian Elischer atomic_add_long(&ngq->q_flags, add_arg); 1721069154d5SJulian Elischer /* 1722069154d5SJulian Elischer * We have successfully cleared the old pending flag, set the new one 1723069154d5SJulian Elischer * if it is needed, and incremented the appropriate active field. 1724069154d5SJulian Elischer * (all in one atomic addition.. wow) 1725069154d5SJulian Elischer */ 1726069154d5SJulian Elischer return (item); 1727069154d5SJulian Elischer } 1728859a4d16SJulian Elischer 1729859a4d16SJulian Elischer /* 1730069154d5SJulian Elischer * Queue a packet to be picked up by someone else. 1731069154d5SJulian Elischer * We really don't care who, but we can't or don't want to hang around 1732069154d5SJulian Elischer * to process it ourselves. We are probably an interrupt routine.. 1733069154d5SJulian Elischer * 1 = writer, 0 = reader 1734069154d5SJulian Elischer * We should set something to indicate NETISR requested 1735069154d5SJulian Elischer * If it's the first item queued. 1736859a4d16SJulian Elischer */ 1737069154d5SJulian Elischer #define NGQRW_R 0 1738069154d5SJulian Elischer #define NGQRW_W 1 1739069154d5SJulian Elischer static __inline void 1740069154d5SJulian Elischer ng_queue_rw(struct ng_queue * ngq, item_p item, int rw) 1741069154d5SJulian Elischer { 1742069154d5SJulian Elischer item->el_next = NULL; /* maybe not needed */ 1743069154d5SJulian Elischer *ngq->last = item; 1744069154d5SJulian Elischer /* 1745069154d5SJulian Elischer * If it was the first item in the queue then we need to 1746069154d5SJulian Elischer * set the last pointer and the type flags. 1747069154d5SJulian Elischer */ 1748069154d5SJulian Elischer if (ngq->last == &(ngq->queue)) { 1749069154d5SJulian Elischer /* 1750069154d5SJulian Elischer * When called with constants for rw, the optimiser will 1751069154d5SJulian Elischer * remove the unneeded branch below. 1752069154d5SJulian Elischer */ 1753069154d5SJulian Elischer if (rw == NGQRW_W) { 1754069154d5SJulian Elischer atomic_add_long(&ngq->q_flags, WRITE_PENDING); 1755069154d5SJulian Elischer } else { 1756069154d5SJulian Elischer atomic_add_long(&ngq->q_flags, READ_PENDING); 1757069154d5SJulian Elischer } 1758069154d5SJulian Elischer } 1759069154d5SJulian Elischer ngq->last = &(item->el_next); 1760069154d5SJulian Elischer } 1761069154d5SJulian Elischer 1762069154d5SJulian Elischer 1763069154d5SJulian Elischer /* 1764069154d5SJulian Elischer * This function 'cheats' in that it first tries to 'grab' the use of the 1765069154d5SJulian Elischer * node, without going through the mutex. We can do this becasue of the 1766069154d5SJulian Elischer * semantics of the lock. The semantics include a clause that says that the 1767069154d5SJulian Elischer * value of the readers count is invalid if the WRITER_ACTIVE flag is set. It 1768069154d5SJulian Elischer * also says that the WRITER_ACTIVE flag cannot be set if the readers count 1769069154d5SJulian Elischer * is not zero. Note that this talks about what is valid to SET the 1770069154d5SJulian Elischer * WRITER_ACTIVE flag, because from the moment it is set, the value if the 1771069154d5SJulian Elischer * reader count is immaterial, and not valid. The two 'pending' flags have a 1772069154d5SJulian Elischer * similar effect, in that If they are orthogonal to the two active fields in 1773069154d5SJulian Elischer * how they are set, but if either is set, the attempted 'grab' need to be 1774069154d5SJulian Elischer * backed out because there is earlier work, and we maintain ordering in the 1775069154d5SJulian Elischer * queue. The result of this is that the reader request can try obtain use of 1776069154d5SJulian Elischer * the node with only a single atomic addition, and without any of the mutex 1777069154d5SJulian Elischer * overhead. If this fails the operation degenerates to the same as for other 1778069154d5SJulian Elischer * cases. 1779069154d5SJulian Elischer * 1780069154d5SJulian Elischer */ 1781069154d5SJulian Elischer static __inline item_p 1782069154d5SJulian Elischer ng_acquire_read(struct ng_queue *ngq, item_p item) 1783069154d5SJulian Elischer { 1784069154d5SJulian Elischer 1785069154d5SJulian Elischer /* ######### Hack alert ######### */ 1786069154d5SJulian Elischer atomic_add_long(&ngq->q_flags, READER_INCREMENT); 1787069154d5SJulian Elischer if ((ngq->q_flags & (~READER_MASK)) == 0) { 1788069154d5SJulian Elischer /* Successfully grabbed node */ 1789069154d5SJulian Elischer return (item); 1790069154d5SJulian Elischer } 1791069154d5SJulian Elischer /* undo the damage if we didn't succeed */ 1792069154d5SJulian Elischer atomic_subtract_long(&ngq->q_flags, READER_INCREMENT); 1793069154d5SJulian Elischer 1794069154d5SJulian Elischer /* ######### End Hack alert ######### */ 1795069154d5SJulian Elischer mtx_enter((&ngq->q_mtx), MTX_SPIN); 1796069154d5SJulian Elischer /* 1797069154d5SJulian Elischer * Try again. Another processor (or interrupt for that matter) may 1798069154d5SJulian Elischer * have removed the last queued item that was stopping us from 1799069154d5SJulian Elischer * running, between the previous test, and the moment that we took 1800069154d5SJulian Elischer * the mutex. (Or maybe a writer completed.) 1801069154d5SJulian Elischer */ 1802069154d5SJulian Elischer if ((ngq->q_flags & (~READER_MASK)) == 0) { 1803069154d5SJulian Elischer atomic_add_long(&ngq->q_flags, READER_INCREMENT); 1804069154d5SJulian Elischer mtx_exit((&ngq->q_mtx), MTX_SPIN); 1805069154d5SJulian Elischer return (item); 1806069154d5SJulian Elischer } 1807069154d5SJulian Elischer 1808069154d5SJulian Elischer /* 1809069154d5SJulian Elischer * Quick check that we are doing things right. 1810069154d5SJulian Elischer */ 1811069154d5SJulian Elischer if (ngq->q_flags & SINGLE_THREAD_ONLY) { 1812069154d5SJulian Elischer panic("Calling single treaded queue incorrectly"); 1813069154d5SJulian Elischer } 1814069154d5SJulian Elischer 1815069154d5SJulian Elischer /* 1816069154d5SJulian Elischer * and queue the request for later. 1817069154d5SJulian Elischer */ 1818069154d5SJulian Elischer item->el_flags |= NGQF_TYPE; 1819069154d5SJulian Elischer ng_queue_rw(ngq, item, NGQRW_R); 1820069154d5SJulian Elischer 1821069154d5SJulian Elischer /* 1822069154d5SJulian Elischer * Ok, so that's the item successfully queued for later. So now we 1823069154d5SJulian Elischer * see if we can dequeue something to run instead. 1824069154d5SJulian Elischer */ 1825069154d5SJulian Elischer item = ng_dequeue(ngq); 1826069154d5SJulian Elischer mtx_exit(&(ngq->q_mtx), MTX_SPIN); 1827069154d5SJulian Elischer return (item); 1828069154d5SJulian Elischer } 1829069154d5SJulian Elischer 1830069154d5SJulian Elischer static __inline item_p 1831069154d5SJulian Elischer ng_acquire_write(struct ng_queue *ngq, item_p item) 1832069154d5SJulian Elischer { 1833069154d5SJulian Elischer restart: 1834069154d5SJulian Elischer mtx_enter(&(ngq->q_mtx), MTX_SPIN); 1835069154d5SJulian Elischer /* 1836069154d5SJulian Elischer * If there are no readers, no writer, and no pending packets, then 1837069154d5SJulian Elischer * we can just go ahead. In all other situations we need to queue the 1838069154d5SJulian Elischer * request 1839069154d5SJulian Elischer */ 1840069154d5SJulian Elischer if ((ngq->q_flags & (~SINGLE_THREAD_ONLY)) == 0) { 1841069154d5SJulian Elischer atomic_add_long(&ngq->q_flags, WRITER_ACTIVE); 1842069154d5SJulian Elischer mtx_exit((&ngq->q_mtx), MTX_SPIN); 1843069154d5SJulian Elischer if (ngq->q_flags & READER_MASK) { 1844069154d5SJulian Elischer /* Collision with fast-track reader */ 1845069154d5SJulian Elischer atomic_add_long(&ngq->q_flags, -WRITER_ACTIVE); 1846069154d5SJulian Elischer goto restart; 1847069154d5SJulian Elischer } 1848069154d5SJulian Elischer 1849069154d5SJulian Elischer return (item); 1850069154d5SJulian Elischer } 1851069154d5SJulian Elischer 1852069154d5SJulian Elischer /* 1853069154d5SJulian Elischer * and queue the request for later. 1854069154d5SJulian Elischer */ 1855069154d5SJulian Elischer item->el_flags &= ~NGQF_TYPE; 1856069154d5SJulian Elischer ng_queue_rw(ngq, item, NGQRW_W); 1857069154d5SJulian Elischer 1858069154d5SJulian Elischer /* 1859069154d5SJulian Elischer * Ok, so that's the item successfully queued for later. So now we 1860069154d5SJulian Elischer * see if we can dequeue something to run instead. 1861069154d5SJulian Elischer */ 1862069154d5SJulian Elischer item = ng_dequeue(ngq); 1863069154d5SJulian Elischer mtx_exit(&(ngq->q_mtx), MTX_SPIN); 1864069154d5SJulian Elischer return (item); 1865069154d5SJulian Elischer } 1866069154d5SJulian Elischer 1867069154d5SJulian Elischer static __inline void 1868069154d5SJulian Elischer ng_leave_read(struct ng_queue *ngq) 1869069154d5SJulian Elischer { 1870069154d5SJulian Elischer atomic_subtract_long(&ngq->q_flags, READER_INCREMENT); 1871069154d5SJulian Elischer } 1872069154d5SJulian Elischer 1873069154d5SJulian Elischer static __inline void 1874069154d5SJulian Elischer ng_leave_write(struct ng_queue *ngq) 1875069154d5SJulian Elischer { 1876069154d5SJulian Elischer atomic_subtract_long(&ngq->q_flags, WRITER_ACTIVE); 1877069154d5SJulian Elischer } 1878069154d5SJulian Elischer 1879069154d5SJulian Elischer static void 1880069154d5SJulian Elischer ng_flush_input_queue(struct ng_queue * ngq) 1881069154d5SJulian Elischer { 1882069154d5SJulian Elischer item_p item; 1883069154d5SJulian Elischer u_int add_arg; 1884069154d5SJulian Elischer mtx_enter(&ngq->q_mtx, MTX_SPIN); 1885069154d5SJulian Elischer for (;;) { 1886069154d5SJulian Elischer /* Now take a look at what's on the queue */ 1887069154d5SJulian Elischer if (ngq->q_flags & READ_PENDING) { 1888069154d5SJulian Elischer add_arg = -READ_PENDING; 1889069154d5SJulian Elischer } else if (ngq->q_flags & WRITE_PENDING) { 1890069154d5SJulian Elischer add_arg = -WRITE_PENDING; 1891069154d5SJulian Elischer } else { 1892069154d5SJulian Elischer break; 1893069154d5SJulian Elischer } 1894069154d5SJulian Elischer 1895069154d5SJulian Elischer item = ngq->queue; 1896069154d5SJulian Elischer ngq->queue = item->el_next; 1897069154d5SJulian Elischer if (ngq->last == &(item->el_next)) { 1898069154d5SJulian Elischer ngq->last = &(ngq->queue); 1899069154d5SJulian Elischer } else { 1900069154d5SJulian Elischer if ((ngq->queue->el_flags & NGQF_TYPE) == NGQF_READER) { 1901069154d5SJulian Elischer add_arg += READ_PENDING; 1902069154d5SJulian Elischer } else { 1903069154d5SJulian Elischer add_arg += WRITE_PENDING; 1904069154d5SJulian Elischer } 1905069154d5SJulian Elischer } 1906069154d5SJulian Elischer atomic_add_long(&ngq->q_flags, add_arg); 1907069154d5SJulian Elischer 1908069154d5SJulian Elischer mtx_exit(&ngq->q_mtx, MTX_SPIN); 1909069154d5SJulian Elischer NG_FREE_ITEM(item); 1910069154d5SJulian Elischer mtx_enter(&ngq->q_mtx, MTX_SPIN); 1911069154d5SJulian Elischer } 1912069154d5SJulian Elischer mtx_exit(&ngq->q_mtx, MTX_SPIN); 1913069154d5SJulian Elischer } 1914069154d5SJulian Elischer 1915069154d5SJulian Elischer /*********************************************************************** 1916069154d5SJulian Elischer * Externally visible method for sending or queueing messages or data. 1917069154d5SJulian Elischer ***********************************************************************/ 1918069154d5SJulian Elischer 1919069154d5SJulian Elischer /* 1920069154d5SJulian Elischer * MACRO WILL DO THE JOB OF CALLING ng_package_msg IN CALLER 1921069154d5SJulian Elischer * before we are called. The user code should have filled out the item 1922069154d5SJulian Elischer * correctly by this stage: 1923069154d5SJulian Elischer * Common: 1924069154d5SJulian Elischer * reference to destination node. 1925069154d5SJulian Elischer * Reference to destination rcv hook if relevant. 1926069154d5SJulian Elischer * Data: 1927069154d5SJulian Elischer * pointer to mbuf 1928069154d5SJulian Elischer * pointer to metadata 1929069154d5SJulian Elischer * Control_Message: 1930069154d5SJulian Elischer * pointer to msg. 1931069154d5SJulian Elischer * ID of original sender node. (return address) 1932069154d5SJulian Elischer * 1933069154d5SJulian Elischer * The nodes have several routines and macros to help with this task: 1934069154d5SJulian Elischer * ng_package_msg() 1935069154d5SJulian Elischer * ng_package_data() do much of the work. 1936069154d5SJulian Elischer * ng_retarget_msg 1937069154d5SJulian Elischer * ng_retarget_data 1938069154d5SJulian Elischer */ 1939069154d5SJulian Elischer 1940069154d5SJulian Elischer int 1941069154d5SJulian Elischer ng_snd_item(item_p item, int queue) 1942069154d5SJulian Elischer { 1943069154d5SJulian Elischer hook_p hook = item->el_hook; 1944069154d5SJulian Elischer node_p dest = item->el_dest; 1945069154d5SJulian Elischer int rw; 1946069154d5SJulian Elischer int error = 0, ierror; 1947069154d5SJulian Elischer item_p oitem; 194830400f03SJulian Elischer struct ng_queue * ngq = &dest->nd_input_queue; 1949069154d5SJulian Elischer 195030400f03SJulian Elischer #ifdef NETGRAPH_DEBUG 1951069154d5SJulian Elischer _ngi_check(item, __FILE__, __LINE__); 1952069154d5SJulian Elischer #endif 1953069154d5SJulian Elischer 1954069154d5SJulian Elischer if (item == NULL) { 195530400f03SJulian Elischer TRAP_ERROR; 1956069154d5SJulian Elischer return (EINVAL); /* failed to get queue element */ 1957069154d5SJulian Elischer } 1958069154d5SJulian Elischer if (dest == NULL) { 1959069154d5SJulian Elischer NG_FREE_ITEM(item); 196030400f03SJulian Elischer TRAP_ERROR; 1961069154d5SJulian Elischer return (EINVAL); /* No address */ 1962069154d5SJulian Elischer } 1963069154d5SJulian Elischer if ((item->el_flags & NGQF_D_M) == NGQF_DATA) { 1964069154d5SJulian Elischer /* 1965069154d5SJulian Elischer * DATA MESSAGE 1966069154d5SJulian Elischer * Delivered to a node via a non-optional hook. 1967069154d5SJulian Elischer * Both should be present in the item even though 1968069154d5SJulian Elischer * the node is derivable from the hook. 1969069154d5SJulian Elischer * References are held on both by the item. 1970069154d5SJulian Elischer */ 197130400f03SJulian Elischer #ifdef NETGRAPH_DEBUG 1972069154d5SJulian Elischer _ngi_check(item, __FILE__, __LINE__); 1973069154d5SJulian Elischer #endif 1974069154d5SJulian Elischer CHECK_DATA_MBUF(NGI_M(item)); 1975069154d5SJulian Elischer if (hook == NULL) { 1976069154d5SJulian Elischer NG_FREE_ITEM(item); 197730400f03SJulian Elischer TRAP_ERROR; 1978069154d5SJulian Elischer return(EINVAL); 1979069154d5SJulian Elischer } 198030400f03SJulian Elischer if ((NG_HOOK_NOT_VALID(hook)) 198130400f03SJulian Elischer || (NG_NODE_NOT_VALID(NG_HOOK_NODE(hook)))) { 1982069154d5SJulian Elischer NG_FREE_ITEM(item); 1983069154d5SJulian Elischer return (ENOTCONN); 1984859a4d16SJulian Elischer } 198530400f03SJulian Elischer if ((hook->hk_flags & HK_QUEUE)) { 1986069154d5SJulian Elischer queue = 1; 19874cf49a43SJulian Elischer } 1988069154d5SJulian Elischer /* By default data is a reader in the locking scheme */ 1989069154d5SJulian Elischer item->el_flags |= NGQF_READER; 1990069154d5SJulian Elischer rw = NGQRW_R; 1991069154d5SJulian Elischer } else { 19924cf49a43SJulian Elischer /* 1993069154d5SJulian Elischer * CONTROL MESSAGE 1994069154d5SJulian Elischer * Delivered to a node. 1995069154d5SJulian Elischer * Hook is optional. 1996069154d5SJulian Elischer * References are held by the item on the node and 1997069154d5SJulian Elischer * the hook if it is present. 19984cf49a43SJulian Elischer */ 199930400f03SJulian Elischer if (hook && (hook->hk_flags & HK_QUEUE)) { 2000069154d5SJulian Elischer queue = 1; 2001069154d5SJulian Elischer } 2002069154d5SJulian Elischer /* Data messages count as writers unles explicitly exempted */ 2003069154d5SJulian Elischer if (NGI_MSG(item)->header.cmd & NGM_READONLY) { 2004069154d5SJulian Elischer item->el_flags |= NGQF_READER; 2005069154d5SJulian Elischer rw = NGQRW_R; 2006069154d5SJulian Elischer } else { 2007069154d5SJulian Elischer item->el_flags &= ~NGQF_TYPE; 2008069154d5SJulian Elischer rw = NGQRW_W; 2009069154d5SJulian Elischer } 2010069154d5SJulian Elischer } 2011069154d5SJulian Elischer /* 2012069154d5SJulian Elischer * If the node specifies single threading, force writer semantics 2013069154d5SJulian Elischer * Similarly the node may say one hook always produces writers. 2014069154d5SJulian Elischer * These are over-rides. 2015069154d5SJulian Elischer */ 2016069154d5SJulian Elischer if ((ngq->q_flags & SINGLE_THREAD_ONLY) 201730400f03SJulian Elischer || (dest->nd_flags & NG_FORCE_WRITER) 201830400f03SJulian Elischer || (hook && (hook->hk_flags & HK_FORCE_WRITER))) { 2019069154d5SJulian Elischer rw = NGQRW_W; 2020069154d5SJulian Elischer item->el_flags &= ~NGQF_TYPE; 2021069154d5SJulian Elischer } 2022069154d5SJulian Elischer if (queue) { 2023069154d5SJulian Elischer /* Put it on the queue for that node*/ 202430400f03SJulian Elischer #ifdef NETGRAPH_DEBUG 2025069154d5SJulian Elischer _ngi_check(item, __FILE__, __LINE__); 2026069154d5SJulian Elischer #endif 2027069154d5SJulian Elischer mtx_enter(&(ngq->q_mtx), MTX_SPIN); 2028069154d5SJulian Elischer ng_queue_rw(ngq, item, rw); 2029069154d5SJulian Elischer mtx_exit(&(ngq->q_mtx), MTX_SPIN); 2030069154d5SJulian Elischer /* 2031069154d5SJulian Elischer * If there are active elements then we can rely on 2032069154d5SJulian Elischer * them. if not we should not rely on another packet 2033069154d5SJulian Elischer * coming here by another path, 2034069154d5SJulian Elischer * so it is best to put us in the netisr list. 2035069154d5SJulian Elischer */ 2036069154d5SJulian Elischer if ((ngq->q_flags & (READER_MASK|WRITER_ACTIVE)) == 0) { 2037069154d5SJulian Elischer ng_setisr(ngq->q_node); 2038069154d5SJulian Elischer } 2039069154d5SJulian Elischer return (0); 2040069154d5SJulian Elischer } 2041069154d5SJulian Elischer /* 2042069154d5SJulian Elischer * Take a queue item and a node and see if we can apply the item to 2043069154d5SJulian Elischer * the node. We may end up getting a different item to apply instead. 2044069154d5SJulian Elischer * Will allow for a piggyback reply only in the case where 2045069154d5SJulian Elischer * there is no queueing. 2046069154d5SJulian Elischer */ 2047069154d5SJulian Elischer 2048069154d5SJulian Elischer oitem = item; 2049069154d5SJulian Elischer /* 2050069154d5SJulian Elischer * We already decided how we will be queueud or treated. 2051069154d5SJulian Elischer * Try get the appropriate operating permission. 2052069154d5SJulian Elischer */ 2053069154d5SJulian Elischer if (rw == NGQRW_R) { 2054069154d5SJulian Elischer item = ng_acquire_read(ngq, item); 2055069154d5SJulian Elischer } else { 2056069154d5SJulian Elischer item = ng_acquire_write(ngq, item); 20574cf49a43SJulian Elischer } 20584cf49a43SJulian Elischer 20594cf49a43SJulian Elischer /* 2060069154d5SJulian Elischer * May have come back with a different item. 2061069154d5SJulian Elischer * or maybe none at all. The one we started with will 2062069154d5SJulian Elischer * have been queued in thises cases. 2063069154d5SJulian Elischer */ 2064069154d5SJulian Elischer if (item == NULL) { 2065069154d5SJulian Elischer return (0); 2066069154d5SJulian Elischer } 2067069154d5SJulian Elischer 206830400f03SJulian Elischer #ifdef NETGRAPH_DEBUG 2069069154d5SJulian Elischer _ngi_check(item, __FILE__, __LINE__); 2070069154d5SJulian Elischer #endif 2071069154d5SJulian Elischer ierror = ng_apply_item(dest, item); /* drops r/w lock when done */ 2072069154d5SJulian Elischer 2073069154d5SJulian Elischer /* only return an error if it was our initial item.. (compat hack) */ 2074069154d5SJulian Elischer if (oitem == item) { 2075069154d5SJulian Elischer error = ierror; 2076069154d5SJulian Elischer } 2077069154d5SJulian Elischer 2078069154d5SJulian Elischer /* 2079069154d5SJulian Elischer * Now we've handled the packet we brought, (or a friend of it) let's 2080069154d5SJulian Elischer * look for any other packets that may have been queued up. We hold 2081069154d5SJulian Elischer * no locks, so if someone puts something in the queue after 2082069154d5SJulian Elischer * we check that it is empty, it is their problem 2083069154d5SJulian Elischer * to ensure it is processed. If we have the netisr thread cme in here 2084069154d5SJulian Elischer * while we still say we have stuff to do, we may get a boost 2085069154d5SJulian Elischer * in SMP systems. :-) 2086069154d5SJulian Elischer */ 2087069154d5SJulian Elischer for (;;) { 2088069154d5SJulian Elischer /* quick hack to save all that mutex stuff */ 2089069154d5SJulian Elischer if ((ngq->q_flags & (WRITE_PENDING | READ_PENDING)) == 0) { 209030400f03SJulian Elischer if (dest->nd_flags & NG_WORKQ) 2091069154d5SJulian Elischer ng_worklist_remove(dest); 2092069154d5SJulian Elischer return (0); 2093069154d5SJulian Elischer } 2094069154d5SJulian Elischer /* 2095069154d5SJulian Elischer * dequeue acquires and adjusts the input_queue as it dequeues 2096069154d5SJulian Elischer * packets. It acquires the rw lock as needed. 2097069154d5SJulian Elischer */ 2098069154d5SJulian Elischer mtx_enter(&ngq->q_mtx, MTX_SPIN); 2099069154d5SJulian Elischer item = ng_dequeue(ngq); 2100069154d5SJulian Elischer mtx_exit(&ngq->q_mtx, MTX_SPIN); 2101069154d5SJulian Elischer if (!item) { 2102069154d5SJulian Elischer /* 2103069154d5SJulian Elischer * If we have no work to do 2104069154d5SJulian Elischer * then we certainly don't need to be 2105069154d5SJulian Elischer * on the worklist. 2106069154d5SJulian Elischer */ 210730400f03SJulian Elischer if (dest->nd_flags & NG_WORKQ) 2108069154d5SJulian Elischer ng_worklist_remove(dest); 2109069154d5SJulian Elischer return (0); 2110069154d5SJulian Elischer } 211130400f03SJulian Elischer #ifdef NETGRAPH_DEBUG 2112069154d5SJulian Elischer _ngi_check(item, __FILE__, __LINE__); 2113069154d5SJulian Elischer #endif 2114069154d5SJulian Elischer 2115069154d5SJulian Elischer /* 2116069154d5SJulian Elischer * We have the appropriate lock, so run the item. 2117069154d5SJulian Elischer * When finished it will drop the lock accordingly 2118069154d5SJulian Elischer */ 2119069154d5SJulian Elischer 2120069154d5SJulian Elischer ierror = ng_apply_item(dest, item); 2121069154d5SJulian Elischer /* 2122069154d5SJulian Elischer * only return an error if it was our initial 2123069154d5SJulian Elischer * item.. (compat hack) 2124069154d5SJulian Elischer */ 2125069154d5SJulian Elischer if (oitem == item) { 2126069154d5SJulian Elischer error = ierror; 2127069154d5SJulian Elischer } 2128069154d5SJulian Elischer } 2129069154d5SJulian Elischer return (0); 2130069154d5SJulian Elischer } 2131069154d5SJulian Elischer 2132069154d5SJulian Elischer /* 2133069154d5SJulian Elischer * We have an item that was possibly queued somewhere. 2134069154d5SJulian Elischer * It should contain all the information needed 2135069154d5SJulian Elischer * to run it on the appropriate node/hook. 21364cf49a43SJulian Elischer */ 21374cf49a43SJulian Elischer static int 2138069154d5SJulian Elischer ng_apply_item(node_p node, item_p item) 2139069154d5SJulian Elischer { 2140069154d5SJulian Elischer hook_p hook; 2141069154d5SJulian Elischer int was_reader = ((item->el_flags & NGQF_TYPE)); 2142069154d5SJulian Elischer int error = 0; 2143069154d5SJulian Elischer ng_rcvdata_t *rcvdata; 2144069154d5SJulian Elischer 2145069154d5SJulian Elischer hook = item->el_hook; 214630400f03SJulian Elischer item->el_hook = NULL; /* so NG_FREE_ITEM doesn't NG_HOOK_UNREF() */ 2147069154d5SJulian Elischer /* We already have the node.. assume responsibility */ 2148069154d5SJulian Elischer /* And the reference */ 2149069154d5SJulian Elischer /* node = item->el_dest; */ 2150069154d5SJulian Elischer item->el_dest = NULL; /* same as for the hook above */ 215130400f03SJulian Elischer #ifdef NETGRAPH_DEBUG 2152069154d5SJulian Elischer _ngi_check(item, __FILE__, __LINE__); 2153069154d5SJulian Elischer #endif 2154069154d5SJulian Elischer 2155069154d5SJulian Elischer switch (item->el_flags & NGQF_D_M) { 2156069154d5SJulian Elischer case NGQF_DATA: 2157069154d5SJulian Elischer /* 2158069154d5SJulian Elischer * Check things are still ok as when we were queued. 2159069154d5SJulian Elischer */ 2160069154d5SJulian Elischer 2161069154d5SJulian Elischer if ((hook == NULL) 216230400f03SJulian Elischer || NG_HOOK_NOT_VALID(hook) 216330400f03SJulian Elischer || NG_NODE_NOT_VALID(NG_HOOK_NODE(hook)) 216430400f03SJulian Elischer || ((rcvdata = NG_HOOK_NODE(hook)->nd_type->rcvdata) == NULL)) { 2165069154d5SJulian Elischer error = EIO; 2166069154d5SJulian Elischer NG_FREE_ITEM(item); 2167069154d5SJulian Elischer } else { 2168069154d5SJulian Elischer error = (*rcvdata)(hook, item); 2169069154d5SJulian Elischer } 2170069154d5SJulian Elischer break; 2171069154d5SJulian Elischer case NGQF_MESG: 2172069154d5SJulian Elischer 2173069154d5SJulian Elischer if (hook) { 2174069154d5SJulian Elischer item->el_hook = NULL; 217530400f03SJulian Elischer if (NG_HOOK_NOT_VALID(hook)) { 2176069154d5SJulian Elischer /* 2177069154d5SJulian Elischer * If the hook has been zapped then we can't use it. 2178069154d5SJulian Elischer * Immediatly drop its reference. 2179069154d5SJulian Elischer * The message may not need it. 2180069154d5SJulian Elischer */ 218130400f03SJulian Elischer NG_HOOK_UNREF(hook); 2182069154d5SJulian Elischer hook = NULL; 2183069154d5SJulian Elischer } 2184069154d5SJulian Elischer } 2185069154d5SJulian Elischer /* 2186069154d5SJulian Elischer * Similarly, if the node is a zombie there is 2187069154d5SJulian Elischer * nothing we can do with it, drop everything. 2188069154d5SJulian Elischer */ 218930400f03SJulian Elischer if (NG_NODE_NOT_VALID(node)) { 219030400f03SJulian Elischer TRAP_ERROR; 2191069154d5SJulian Elischer error = EINVAL; 2192069154d5SJulian Elischer NG_FREE_ITEM(item); 2193069154d5SJulian Elischer } else { 2194069154d5SJulian Elischer /* 2195069154d5SJulian Elischer * Call the appropriate message handler for the object. 2196069154d5SJulian Elischer * It is up to the message handler to free the message. 2197069154d5SJulian Elischer * If it's a generic message, handle it generically, 2198069154d5SJulian Elischer * otherwise call the type's message handler 2199069154d5SJulian Elischer * (if it exists) 2200069154d5SJulian Elischer * XXX (race). Remember that a queued message may 2201069154d5SJulian Elischer * reference a node or hook that has just been 2202069154d5SJulian Elischer * invalidated. It will exist as the queue code 2203069154d5SJulian Elischer * is holding a reference, but.. 2204069154d5SJulian Elischer */ 2205069154d5SJulian Elischer 2206069154d5SJulian Elischer struct ng_mesg *msg = NGI_MSG(item); 2207069154d5SJulian Elischer 2208069154d5SJulian Elischer if ((msg->header.typecookie == NGM_GENERIC_COOKIE) 2209069154d5SJulian Elischer && ((msg->header.flags & NGF_RESP) == 0)) { 2210069154d5SJulian Elischer error = ng_generic_msg(node, item, hook); 2211069154d5SJulian Elischer } else { 221230400f03SJulian Elischer if ((node)->nd_type->rcvmsg != NULL) { 221330400f03SJulian Elischer error = (*(node)->nd_type->rcvmsg)((node), 2214069154d5SJulian Elischer (item), (hook)); 2215069154d5SJulian Elischer } else { 2216069154d5SJulian Elischer TRAP_ERROR; 2217069154d5SJulian Elischer error = EINVAL; /* XXX */ 2218069154d5SJulian Elischer NG_FREE_ITEM(item); 2219069154d5SJulian Elischer } 2220069154d5SJulian Elischer } 2221069154d5SJulian Elischer /* item is now invalid */ 2222069154d5SJulian Elischer } 2223069154d5SJulian Elischer break; 2224069154d5SJulian Elischer } 2225069154d5SJulian Elischer /* 2226069154d5SJulian Elischer * We held references on some of the resources 2227069154d5SJulian Elischer * that we took from the item. Now that we have 2228069154d5SJulian Elischer * finished doing everything, drop those references. 2229069154d5SJulian Elischer */ 2230069154d5SJulian Elischer if (hook) { 223130400f03SJulian Elischer NG_HOOK_UNREF(hook); 2232069154d5SJulian Elischer } 2233069154d5SJulian Elischer 2234069154d5SJulian Elischer if (was_reader) { 223530400f03SJulian Elischer ng_leave_read(&node->nd_input_queue); 2236069154d5SJulian Elischer } else { 223730400f03SJulian Elischer ng_leave_write(&node->nd_input_queue); 2238069154d5SJulian Elischer } 223930400f03SJulian Elischer NG_NODE_UNREF(node); 2240069154d5SJulian Elischer return (error); 2241069154d5SJulian Elischer } 2242069154d5SJulian Elischer 2243069154d5SJulian Elischer /*********************************************************************** 2244069154d5SJulian Elischer * Implement the 'generic' control messages 2245069154d5SJulian Elischer ***********************************************************************/ 2246069154d5SJulian Elischer static int 2247069154d5SJulian Elischer ng_generic_msg(node_p here, item_p item, hook_p lasthook) 22484cf49a43SJulian Elischer { 22494cf49a43SJulian Elischer int error = 0; 2250069154d5SJulian Elischer struct ng_mesg *msg; 2251069154d5SJulian Elischer struct ng_mesg *resp = NULL; 22524cf49a43SJulian Elischer 2253069154d5SJulian Elischer NGI_GET_MSG(item, msg); 22544cf49a43SJulian Elischer if (msg->header.typecookie != NGM_GENERIC_COOKIE) { 225530400f03SJulian Elischer TRAP_ERROR 2256069154d5SJulian Elischer error = EINVAL; 2257069154d5SJulian Elischer goto out; 22584cf49a43SJulian Elischer } 22594cf49a43SJulian Elischer switch (msg->header.cmd) { 22604cf49a43SJulian Elischer case NGM_SHUTDOWN: 22614cf49a43SJulian Elischer ng_rmnode(here); 22624cf49a43SJulian Elischer break; 22634cf49a43SJulian Elischer case NGM_MKPEER: 22644cf49a43SJulian Elischer { 22654cf49a43SJulian Elischer struct ngm_mkpeer *const mkp = (struct ngm_mkpeer *) msg->data; 22664cf49a43SJulian Elischer 22674cf49a43SJulian Elischer if (msg->header.arglen != sizeof(*mkp)) { 226830400f03SJulian Elischer TRAP_ERROR 2269069154d5SJulian Elischer error = EINVAL; 2270069154d5SJulian Elischer break; 22714cf49a43SJulian Elischer } 22724cf49a43SJulian Elischer mkp->type[sizeof(mkp->type) - 1] = '\0'; 22734cf49a43SJulian Elischer mkp->ourhook[sizeof(mkp->ourhook) - 1] = '\0'; 22744cf49a43SJulian Elischer mkp->peerhook[sizeof(mkp->peerhook) - 1] = '\0'; 22754cf49a43SJulian Elischer error = ng_mkpeer(here, mkp->ourhook, mkp->peerhook, mkp->type); 22764cf49a43SJulian Elischer break; 22774cf49a43SJulian Elischer } 22784cf49a43SJulian Elischer case NGM_CONNECT: 22794cf49a43SJulian Elischer { 22804cf49a43SJulian Elischer struct ngm_connect *const con = 22814cf49a43SJulian Elischer (struct ngm_connect *) msg->data; 22824cf49a43SJulian Elischer node_p node2; 22834cf49a43SJulian Elischer 22844cf49a43SJulian Elischer if (msg->header.arglen != sizeof(*con)) { 228530400f03SJulian Elischer TRAP_ERROR 2286069154d5SJulian Elischer error = EINVAL; 2287069154d5SJulian Elischer break; 22884cf49a43SJulian Elischer } 22894cf49a43SJulian Elischer con->path[sizeof(con->path) - 1] = '\0'; 22904cf49a43SJulian Elischer con->ourhook[sizeof(con->ourhook) - 1] = '\0'; 22914cf49a43SJulian Elischer con->peerhook[sizeof(con->peerhook) - 1] = '\0'; 2292069154d5SJulian Elischer /* Don't forget we get a reference.. */ 2293069154d5SJulian Elischer error = ng_path2noderef(here, con->path, &node2, NULL); 22944cf49a43SJulian Elischer if (error) 22954cf49a43SJulian Elischer break; 22964cf49a43SJulian Elischer error = ng_con_nodes(here, con->ourhook, node2, con->peerhook); 229730400f03SJulian Elischer NG_NODE_UNREF(node2); 22984cf49a43SJulian Elischer break; 22994cf49a43SJulian Elischer } 23004cf49a43SJulian Elischer case NGM_NAME: 23014cf49a43SJulian Elischer { 23024cf49a43SJulian Elischer struct ngm_name *const nam = (struct ngm_name *) msg->data; 23034cf49a43SJulian Elischer 23044cf49a43SJulian Elischer if (msg->header.arglen != sizeof(*nam)) { 230530400f03SJulian Elischer TRAP_ERROR 2306069154d5SJulian Elischer error = EINVAL; 2307069154d5SJulian Elischer break; 23084cf49a43SJulian Elischer } 23094cf49a43SJulian Elischer nam->name[sizeof(nam->name) - 1] = '\0'; 23104cf49a43SJulian Elischer error = ng_name_node(here, nam->name); 23114cf49a43SJulian Elischer break; 23124cf49a43SJulian Elischer } 23134cf49a43SJulian Elischer case NGM_RMHOOK: 23144cf49a43SJulian Elischer { 23154cf49a43SJulian Elischer struct ngm_rmhook *const rmh = (struct ngm_rmhook *) msg->data; 23164cf49a43SJulian Elischer hook_p hook; 23174cf49a43SJulian Elischer 23184cf49a43SJulian Elischer if (msg->header.arglen != sizeof(*rmh)) { 231930400f03SJulian Elischer TRAP_ERROR 2320069154d5SJulian Elischer error = EINVAL; 2321069154d5SJulian Elischer break; 23224cf49a43SJulian Elischer } 23234cf49a43SJulian Elischer rmh->ourhook[sizeof(rmh->ourhook) - 1] = '\0'; 2324899e9c4eSArchie Cobbs if ((hook = ng_findhook(here, rmh->ourhook)) != NULL) 23254cf49a43SJulian Elischer ng_destroy_hook(hook); 23264cf49a43SJulian Elischer break; 23274cf49a43SJulian Elischer } 2328954c4772SJulian Elischer case NG_INTERNAL_RMHOOK: 2329954c4772SJulian Elischer ng_destroy_hook(lasthook); 2330954c4772SJulian Elischer break; 23314cf49a43SJulian Elischer case NGM_NODEINFO: 23324cf49a43SJulian Elischer { 23334cf49a43SJulian Elischer struct nodeinfo *ni; 23344cf49a43SJulian Elischer 2335069154d5SJulian Elischer NG_MKRESPONSE(resp, msg, sizeof(*ni), M_NOWAIT); 23364cf49a43SJulian Elischer if (resp == NULL) { 23374cf49a43SJulian Elischer error = ENOMEM; 23384cf49a43SJulian Elischer break; 23394cf49a43SJulian Elischer } 23404cf49a43SJulian Elischer 23414cf49a43SJulian Elischer /* Fill in node info */ 2342069154d5SJulian Elischer ni = (struct nodeinfo *) resp->data; 234330400f03SJulian Elischer if (NG_NODE_HAS_NAME(here)) 234430400f03SJulian Elischer strncpy(ni->name, NG_NODE_NAME(here), NG_NODELEN); 234530400f03SJulian Elischer strncpy(ni->type, here->nd_type->name, NG_TYPELEN); 2346dc90cad9SJulian Elischer ni->id = ng_node2ID(here); 234730400f03SJulian Elischer ni->hooks = here->nd_numhooks; 23484cf49a43SJulian Elischer break; 23494cf49a43SJulian Elischer } 23504cf49a43SJulian Elischer case NGM_LISTHOOKS: 23514cf49a43SJulian Elischer { 235230400f03SJulian Elischer const int nhooks = here->nd_numhooks; 23534cf49a43SJulian Elischer struct hooklist *hl; 23544cf49a43SJulian Elischer struct nodeinfo *ni; 23554cf49a43SJulian Elischer hook_p hook; 23564cf49a43SJulian Elischer 23574cf49a43SJulian Elischer /* Get response struct */ 2358069154d5SJulian Elischer NG_MKRESPONSE(resp, msg, sizeof(*hl) 23594cf49a43SJulian Elischer + (nhooks * sizeof(struct linkinfo)), M_NOWAIT); 2360069154d5SJulian Elischer if (resp == NULL) { 23614cf49a43SJulian Elischer error = ENOMEM; 23624cf49a43SJulian Elischer break; 23634cf49a43SJulian Elischer } 2364069154d5SJulian Elischer hl = (struct hooklist *) resp->data; 23654cf49a43SJulian Elischer ni = &hl->nodeinfo; 23664cf49a43SJulian Elischer 23674cf49a43SJulian Elischer /* Fill in node info */ 236830400f03SJulian Elischer if (NG_NODE_HAS_NAME(here)) 236930400f03SJulian Elischer strncpy(ni->name, NG_NODE_NAME(here), NG_NODELEN); 237030400f03SJulian Elischer strncpy(ni->type, here->nd_type->name, NG_TYPELEN); 2371dc90cad9SJulian Elischer ni->id = ng_node2ID(here); 23724cf49a43SJulian Elischer 23734cf49a43SJulian Elischer /* Cycle through the linked list of hooks */ 23744cf49a43SJulian Elischer ni->hooks = 0; 237530400f03SJulian Elischer LIST_FOREACH(hook, &here->nd_hooks, hk_hooks) { 23764cf49a43SJulian Elischer struct linkinfo *const link = &hl->link[ni->hooks]; 23774cf49a43SJulian Elischer 23784cf49a43SJulian Elischer if (ni->hooks >= nhooks) { 23794cf49a43SJulian Elischer log(LOG_ERR, "%s: number of %s changed\n", 23804cf49a43SJulian Elischer __FUNCTION__, "hooks"); 23814cf49a43SJulian Elischer break; 23824cf49a43SJulian Elischer } 238330400f03SJulian Elischer if (NG_HOOK_NOT_VALID(hook)) 23844cf49a43SJulian Elischer continue; 238530400f03SJulian Elischer strncpy(link->ourhook, NG_HOOK_NAME(hook), NG_HOOKLEN); 238630400f03SJulian Elischer strncpy(link->peerhook, 238730400f03SJulian Elischer NG_PEER_HOOK_NAME(hook), NG_HOOKLEN); 238830400f03SJulian Elischer if (NG_PEER_NODE_NAME(hook)[0] != '\0') 23894cf49a43SJulian Elischer strncpy(link->nodeinfo.name, 239030400f03SJulian Elischer NG_PEER_NODE_NAME(hook), NG_NODELEN); 23914cf49a43SJulian Elischer strncpy(link->nodeinfo.type, 239230400f03SJulian Elischer NG_PEER_NODE(hook)->nd_type->name, NG_TYPELEN); 239330400f03SJulian Elischer link->nodeinfo.id = ng_node2ID(NG_PEER_NODE(hook)); 239430400f03SJulian Elischer link->nodeinfo.hooks = NG_PEER_NODE(hook)->nd_numhooks; 23954cf49a43SJulian Elischer ni->hooks++; 23964cf49a43SJulian Elischer } 23974cf49a43SJulian Elischer break; 23984cf49a43SJulian Elischer } 23994cf49a43SJulian Elischer 24004cf49a43SJulian Elischer case NGM_LISTNAMES: 24014cf49a43SJulian Elischer case NGM_LISTNODES: 24024cf49a43SJulian Elischer { 24034cf49a43SJulian Elischer const int unnamed = (msg->header.cmd == NGM_LISTNODES); 24044cf49a43SJulian Elischer struct namelist *nl; 24054cf49a43SJulian Elischer node_p node; 24064cf49a43SJulian Elischer int num = 0; 24074cf49a43SJulian Elischer 2408069154d5SJulian Elischer mtx_enter(&ng_nodelist_mtx, MTX_DEF); 24094cf49a43SJulian Elischer /* Count number of nodes */ 241030400f03SJulian Elischer LIST_FOREACH(node, &ng_nodelist, nd_nodes) { 241170de87f2SJulian Elischer if (NG_NODE_IS_VALID(node) 241270de87f2SJulian Elischer && (unnamed || NG_NODE_HAS_NAME(node))) { 24134cf49a43SJulian Elischer num++; 24144cf49a43SJulian Elischer } 241570de87f2SJulian Elischer } 2416069154d5SJulian Elischer mtx_exit(&ng_nodelist_mtx, MTX_DEF); 24174cf49a43SJulian Elischer 24184cf49a43SJulian Elischer /* Get response struct */ 2419069154d5SJulian Elischer NG_MKRESPONSE(resp, msg, sizeof(*nl) 24204cf49a43SJulian Elischer + (num * sizeof(struct nodeinfo)), M_NOWAIT); 2421069154d5SJulian Elischer if (resp == NULL) { 24224cf49a43SJulian Elischer error = ENOMEM; 24234cf49a43SJulian Elischer break; 24244cf49a43SJulian Elischer } 2425069154d5SJulian Elischer nl = (struct namelist *) resp->data; 24264cf49a43SJulian Elischer 24274cf49a43SJulian Elischer /* Cycle through the linked list of nodes */ 24284cf49a43SJulian Elischer nl->numnames = 0; 2429069154d5SJulian Elischer mtx_enter(&ng_nodelist_mtx, MTX_DEF); 243030400f03SJulian Elischer LIST_FOREACH(node, &ng_nodelist, nd_nodes) { 24314cf49a43SJulian Elischer struct nodeinfo *const np = &nl->nodeinfo[nl->numnames]; 24324cf49a43SJulian Elischer 24334cf49a43SJulian Elischer if (nl->numnames >= num) { 24344cf49a43SJulian Elischer log(LOG_ERR, "%s: number of %s changed\n", 24354cf49a43SJulian Elischer __FUNCTION__, "nodes"); 24364cf49a43SJulian Elischer break; 24374cf49a43SJulian Elischer } 243830400f03SJulian Elischer if (NG_NODE_NOT_VALID(node)) 24394cf49a43SJulian Elischer continue; 244030400f03SJulian Elischer if (!unnamed && (! NG_NODE_HAS_NAME(node))) 24414cf49a43SJulian Elischer continue; 244230400f03SJulian Elischer if (NG_NODE_HAS_NAME(node)) 244330400f03SJulian Elischer strncpy(np->name, NG_NODE_NAME(node), NG_NODELEN); 244430400f03SJulian Elischer strncpy(np->type, node->nd_type->name, NG_TYPELEN); 2445dc90cad9SJulian Elischer np->id = ng_node2ID(node); 244630400f03SJulian Elischer np->hooks = node->nd_numhooks; 24474cf49a43SJulian Elischer nl->numnames++; 24484cf49a43SJulian Elischer } 2449069154d5SJulian Elischer mtx_exit(&ng_nodelist_mtx, MTX_DEF); 24504cf49a43SJulian Elischer break; 24514cf49a43SJulian Elischer } 24524cf49a43SJulian Elischer 24534cf49a43SJulian Elischer case NGM_LISTTYPES: 24544cf49a43SJulian Elischer { 24554cf49a43SJulian Elischer struct typelist *tl; 24564cf49a43SJulian Elischer struct ng_type *type; 24574cf49a43SJulian Elischer int num = 0; 24584cf49a43SJulian Elischer 2459069154d5SJulian Elischer mtx_enter(&ng_typelist_mtx, MTX_DEF); 24604cf49a43SJulian Elischer /* Count number of types */ 246170de87f2SJulian Elischer LIST_FOREACH(type, &ng_typelist, types) { 24624cf49a43SJulian Elischer num++; 246370de87f2SJulian Elischer } 2464069154d5SJulian Elischer mtx_exit(&ng_typelist_mtx, MTX_DEF); 24654cf49a43SJulian Elischer 24664cf49a43SJulian Elischer /* Get response struct */ 2467069154d5SJulian Elischer NG_MKRESPONSE(resp, msg, sizeof(*tl) 24684cf49a43SJulian Elischer + (num * sizeof(struct typeinfo)), M_NOWAIT); 2469069154d5SJulian Elischer if (resp == NULL) { 24704cf49a43SJulian Elischer error = ENOMEM; 24714cf49a43SJulian Elischer break; 24724cf49a43SJulian Elischer } 2473069154d5SJulian Elischer tl = (struct typelist *) resp->data; 24744cf49a43SJulian Elischer 24754cf49a43SJulian Elischer /* Cycle through the linked list of types */ 24764cf49a43SJulian Elischer tl->numtypes = 0; 2477069154d5SJulian Elischer mtx_enter(&ng_typelist_mtx, MTX_DEF); 2478069154d5SJulian Elischer LIST_FOREACH(type, &ng_typelist, types) { 24794cf49a43SJulian Elischer struct typeinfo *const tp = &tl->typeinfo[tl->numtypes]; 24804cf49a43SJulian Elischer 24814cf49a43SJulian Elischer if (tl->numtypes >= num) { 24824cf49a43SJulian Elischer log(LOG_ERR, "%s: number of %s changed\n", 24834cf49a43SJulian Elischer __FUNCTION__, "types"); 24844cf49a43SJulian Elischer break; 24854cf49a43SJulian Elischer } 2486a096e45aSArchie Cobbs strncpy(tp->type_name, type->name, NG_TYPELEN); 24874cf49a43SJulian Elischer tp->numnodes = type->refs; 24884cf49a43SJulian Elischer tl->numtypes++; 24894cf49a43SJulian Elischer } 2490069154d5SJulian Elischer mtx_exit(&ng_typelist_mtx, MTX_DEF); 24914cf49a43SJulian Elischer break; 24924cf49a43SJulian Elischer } 24934cf49a43SJulian Elischer 2494f8307e12SArchie Cobbs case NGM_BINARY2ASCII: 2495f8307e12SArchie Cobbs { 24967133ac27SArchie Cobbs int bufSize = 20 * 1024; /* XXX hard coded constant */ 2497f8307e12SArchie Cobbs const struct ng_parse_type *argstype; 2498f8307e12SArchie Cobbs const struct ng_cmdlist *c; 2499069154d5SJulian Elischer struct ng_mesg *binary, *ascii; 2500f8307e12SArchie Cobbs 2501f8307e12SArchie Cobbs /* Data area must contain a valid netgraph message */ 2502f8307e12SArchie Cobbs binary = (struct ng_mesg *)msg->data; 2503f8307e12SArchie Cobbs if (msg->header.arglen < sizeof(struct ng_mesg) 250470de87f2SJulian Elischer || (msg->header.arglen - sizeof(struct ng_mesg) 250570de87f2SJulian Elischer < binary->header.arglen)) { 250630400f03SJulian Elischer TRAP_ERROR 2507f8307e12SArchie Cobbs error = EINVAL; 2508f8307e12SArchie Cobbs break; 2509f8307e12SArchie Cobbs } 2510f8307e12SArchie Cobbs 2511f8307e12SArchie Cobbs /* Get a response message with lots of room */ 2512069154d5SJulian Elischer NG_MKRESPONSE(resp, msg, sizeof(*ascii) + bufSize, M_NOWAIT); 2513069154d5SJulian Elischer if (resp == NULL) { 2514f8307e12SArchie Cobbs error = ENOMEM; 2515f8307e12SArchie Cobbs break; 2516f8307e12SArchie Cobbs } 2517069154d5SJulian Elischer ascii = (struct ng_mesg *)resp->data; 2518f8307e12SArchie Cobbs 2519f8307e12SArchie Cobbs /* Copy binary message header to response message payload */ 2520f8307e12SArchie Cobbs bcopy(binary, ascii, sizeof(*binary)); 2521f8307e12SArchie Cobbs 2522f8307e12SArchie Cobbs /* Find command by matching typecookie and command number */ 252330400f03SJulian Elischer for (c = here->nd_type->cmdlist; 2524f8307e12SArchie Cobbs c != NULL && c->name != NULL; c++) { 2525f8307e12SArchie Cobbs if (binary->header.typecookie == c->cookie 2526f8307e12SArchie Cobbs && binary->header.cmd == c->cmd) 2527f8307e12SArchie Cobbs break; 2528f8307e12SArchie Cobbs } 2529f8307e12SArchie Cobbs if (c == NULL || c->name == NULL) { 2530f8307e12SArchie Cobbs for (c = ng_generic_cmds; c->name != NULL; c++) { 2531f8307e12SArchie Cobbs if (binary->header.typecookie == c->cookie 2532f8307e12SArchie Cobbs && binary->header.cmd == c->cmd) 2533f8307e12SArchie Cobbs break; 2534f8307e12SArchie Cobbs } 2535f8307e12SArchie Cobbs if (c->name == NULL) { 2536069154d5SJulian Elischer NG_FREE_MSG(resp); 2537f8307e12SArchie Cobbs error = ENOSYS; 2538f8307e12SArchie Cobbs break; 2539f8307e12SArchie Cobbs } 2540f8307e12SArchie Cobbs } 2541f8307e12SArchie Cobbs 2542f8307e12SArchie Cobbs /* Convert command name to ASCII */ 2543f8307e12SArchie Cobbs snprintf(ascii->header.cmdstr, sizeof(ascii->header.cmdstr), 2544f8307e12SArchie Cobbs "%s", c->name); 2545f8307e12SArchie Cobbs 2546f8307e12SArchie Cobbs /* Convert command arguments to ASCII */ 2547f8307e12SArchie Cobbs argstype = (binary->header.flags & NGF_RESP) ? 2548f8307e12SArchie Cobbs c->respType : c->mesgType; 254970de87f2SJulian Elischer if (argstype == NULL) { 2550f8307e12SArchie Cobbs *ascii->data = '\0'; 255170de87f2SJulian Elischer } else { 2552f8307e12SArchie Cobbs if ((error = ng_unparse(argstype, 2553f8307e12SArchie Cobbs (u_char *)binary->data, 2554f8307e12SArchie Cobbs ascii->data, bufSize)) != 0) { 2555069154d5SJulian Elischer NG_FREE_MSG(resp); 2556f8307e12SArchie Cobbs break; 2557f8307e12SArchie Cobbs } 2558f8307e12SArchie Cobbs } 2559f8307e12SArchie Cobbs 2560f8307e12SArchie Cobbs /* Return the result as struct ng_mesg plus ASCII string */ 2561f8307e12SArchie Cobbs bufSize = strlen(ascii->data) + 1; 2562f8307e12SArchie Cobbs ascii->header.arglen = bufSize; 2563069154d5SJulian Elischer resp->header.arglen = sizeof(*ascii) + bufSize; 2564f8307e12SArchie Cobbs break; 2565f8307e12SArchie Cobbs } 2566f8307e12SArchie Cobbs 2567f8307e12SArchie Cobbs case NGM_ASCII2BINARY: 2568f8307e12SArchie Cobbs { 2569f8307e12SArchie Cobbs int bufSize = 2000; /* XXX hard coded constant */ 2570f8307e12SArchie Cobbs const struct ng_cmdlist *c; 2571f8307e12SArchie Cobbs const struct ng_parse_type *argstype; 2572069154d5SJulian Elischer struct ng_mesg *ascii, *binary; 257352ec4a03SArchie Cobbs int off = 0; 2574f8307e12SArchie Cobbs 2575f8307e12SArchie Cobbs /* Data area must contain at least a struct ng_mesg + '\0' */ 2576f8307e12SArchie Cobbs ascii = (struct ng_mesg *)msg->data; 257770de87f2SJulian Elischer if ((msg->header.arglen < sizeof(*ascii) + 1) 257870de87f2SJulian Elischer || (ascii->header.arglen < 1) 257970de87f2SJulian Elischer || (msg->header.arglen 258070de87f2SJulian Elischer < sizeof(*ascii) + ascii->header.arglen)) { 258130400f03SJulian Elischer TRAP_ERROR 2582f8307e12SArchie Cobbs error = EINVAL; 2583f8307e12SArchie Cobbs break; 2584f8307e12SArchie Cobbs } 2585f8307e12SArchie Cobbs ascii->data[ascii->header.arglen - 1] = '\0'; 2586f8307e12SArchie Cobbs 2587f8307e12SArchie Cobbs /* Get a response message with lots of room */ 2588069154d5SJulian Elischer NG_MKRESPONSE(resp, msg, sizeof(*binary) + bufSize, M_NOWAIT); 2589069154d5SJulian Elischer if (resp == NULL) { 2590f8307e12SArchie Cobbs error = ENOMEM; 2591f8307e12SArchie Cobbs break; 2592f8307e12SArchie Cobbs } 2593069154d5SJulian Elischer binary = (struct ng_mesg *)resp->data; 2594f8307e12SArchie Cobbs 2595f8307e12SArchie Cobbs /* Copy ASCII message header to response message payload */ 2596f8307e12SArchie Cobbs bcopy(ascii, binary, sizeof(*ascii)); 2597f8307e12SArchie Cobbs 2598f8307e12SArchie Cobbs /* Find command by matching ASCII command string */ 259930400f03SJulian Elischer for (c = here->nd_type->cmdlist; 2600f8307e12SArchie Cobbs c != NULL && c->name != NULL; c++) { 2601f8307e12SArchie Cobbs if (strcmp(ascii->header.cmdstr, c->name) == 0) 2602f8307e12SArchie Cobbs break; 2603f8307e12SArchie Cobbs } 2604f8307e12SArchie Cobbs if (c == NULL || c->name == NULL) { 2605f8307e12SArchie Cobbs for (c = ng_generic_cmds; c->name != NULL; c++) { 2606f8307e12SArchie Cobbs if (strcmp(ascii->header.cmdstr, c->name) == 0) 2607f8307e12SArchie Cobbs break; 2608f8307e12SArchie Cobbs } 2609f8307e12SArchie Cobbs if (c->name == NULL) { 2610069154d5SJulian Elischer NG_FREE_MSG(resp); 2611f8307e12SArchie Cobbs error = ENOSYS; 2612f8307e12SArchie Cobbs break; 2613f8307e12SArchie Cobbs } 2614f8307e12SArchie Cobbs } 2615f8307e12SArchie Cobbs 2616f8307e12SArchie Cobbs /* Convert command name to binary */ 2617f8307e12SArchie Cobbs binary->header.cmd = c->cmd; 2618f8307e12SArchie Cobbs binary->header.typecookie = c->cookie; 2619f8307e12SArchie Cobbs 2620f8307e12SArchie Cobbs /* Convert command arguments to binary */ 2621f8307e12SArchie Cobbs argstype = (binary->header.flags & NGF_RESP) ? 2622f8307e12SArchie Cobbs c->respType : c->mesgType; 262370de87f2SJulian Elischer if (argstype == NULL) { 2624f8307e12SArchie Cobbs bufSize = 0; 262570de87f2SJulian Elischer } else { 2626f8307e12SArchie Cobbs if ((error = ng_parse(argstype, ascii->data, 2627f8307e12SArchie Cobbs &off, (u_char *)binary->data, &bufSize)) != 0) { 2628069154d5SJulian Elischer NG_FREE_MSG(resp); 2629f8307e12SArchie Cobbs break; 2630f8307e12SArchie Cobbs } 2631f8307e12SArchie Cobbs } 2632f8307e12SArchie Cobbs 2633f8307e12SArchie Cobbs /* Return the result */ 2634f8307e12SArchie Cobbs binary->header.arglen = bufSize; 2635069154d5SJulian Elischer resp->header.arglen = sizeof(*binary) + bufSize; 2636f8307e12SArchie Cobbs break; 2637f8307e12SArchie Cobbs } 2638f8307e12SArchie Cobbs 26397095e097SPoul-Henning Kamp case NGM_TEXT_CONFIG: 26404cf49a43SJulian Elischer case NGM_TEXT_STATUS: 26414cf49a43SJulian Elischer /* 26424cf49a43SJulian Elischer * This one is tricky as it passes the command down to the 26434cf49a43SJulian Elischer * actual node, even though it is a generic type command. 2644069154d5SJulian Elischer * This means we must assume that the item/msg is already freed 26454cf49a43SJulian Elischer * when control passes back to us. 26464cf49a43SJulian Elischer */ 264730400f03SJulian Elischer if (here->nd_type->rcvmsg != NULL) { 2648069154d5SJulian Elischer NGI_MSG(item) = msg; /* put it back as we found it */ 264930400f03SJulian Elischer return((*here->nd_type->rcvmsg)(here, item, lasthook)); 26504cf49a43SJulian Elischer } 26514cf49a43SJulian Elischer /* Fall through if rcvmsg not supported */ 26524cf49a43SJulian Elischer default: 26534cf49a43SJulian Elischer TRAP_ERROR; 26544cf49a43SJulian Elischer error = EINVAL; 26554cf49a43SJulian Elischer } 2656069154d5SJulian Elischer /* 2657069154d5SJulian Elischer * Sometimes a generic message may be statically allocated 2658069154d5SJulian Elischer * to avoid problems with allocating when in tight memeory situations. 2659069154d5SJulian Elischer * Don't free it if it is so. 2660069154d5SJulian Elischer * I break them appart here, because erros may cause a free if the item 2661069154d5SJulian Elischer * in which case we'd be doing it twice. 2662069154d5SJulian Elischer * they are kept together above, to simplify freeing. 2663069154d5SJulian Elischer */ 2664069154d5SJulian Elischer out: 2665069154d5SJulian Elischer NG_RESPOND_MSG(error, here, item, resp); 2666069154d5SJulian Elischer if ( msg && ((msg->header.flags & NGF_STATIC) == 0)) 2667069154d5SJulian Elischer NG_FREE_MSG(msg); 26684cf49a43SJulian Elischer return (error); 26694cf49a43SJulian Elischer } 26704cf49a43SJulian Elischer 26714cf49a43SJulian Elischer /* 2672a096e45aSArchie Cobbs * Copy a 'meta'. 2673a096e45aSArchie Cobbs * 2674a096e45aSArchie Cobbs * Returns new meta, or NULL if original meta is NULL or ENOMEM. 2675a096e45aSArchie Cobbs */ 2676a096e45aSArchie Cobbs meta_p 2677a096e45aSArchie Cobbs ng_copy_meta(meta_p meta) 2678a096e45aSArchie Cobbs { 2679a096e45aSArchie Cobbs meta_p meta2; 2680a096e45aSArchie Cobbs 2681a096e45aSArchie Cobbs if (meta == NULL) 2682a096e45aSArchie Cobbs return (NULL); 2683069154d5SJulian Elischer MALLOC(meta2, meta_p, meta->used_len, M_NETGRAPH_META, M_NOWAIT); 2684a096e45aSArchie Cobbs if (meta2 == NULL) 2685a096e45aSArchie Cobbs return (NULL); 2686a096e45aSArchie Cobbs meta2->allocated_len = meta->used_len; 2687a096e45aSArchie Cobbs bcopy(meta, meta2, meta->used_len); 2688a096e45aSArchie Cobbs return (meta2); 2689a096e45aSArchie Cobbs } 2690a096e45aSArchie Cobbs 26914cf49a43SJulian Elischer /************************************************************************ 26924cf49a43SJulian Elischer Module routines 26934cf49a43SJulian Elischer ************************************************************************/ 26944cf49a43SJulian Elischer 26954cf49a43SJulian Elischer /* 26964cf49a43SJulian Elischer * Handle the loading/unloading of a netgraph node type module 26974cf49a43SJulian Elischer */ 26984cf49a43SJulian Elischer int 26994cf49a43SJulian Elischer ng_mod_event(module_t mod, int event, void *data) 27004cf49a43SJulian Elischer { 27014cf49a43SJulian Elischer struct ng_type *const type = data; 27024cf49a43SJulian Elischer int s, error = 0; 27034cf49a43SJulian Elischer 27044cf49a43SJulian Elischer switch (event) { 27054cf49a43SJulian Elischer case MOD_LOAD: 27064cf49a43SJulian Elischer 27074cf49a43SJulian Elischer /* Register new netgraph node type */ 27084cf49a43SJulian Elischer s = splnet(); 27094cf49a43SJulian Elischer if ((error = ng_newtype(type)) != 0) { 27104cf49a43SJulian Elischer splx(s); 27114cf49a43SJulian Elischer break; 27124cf49a43SJulian Elischer } 27134cf49a43SJulian Elischer 27144cf49a43SJulian Elischer /* Call type specific code */ 27154cf49a43SJulian Elischer if (type->mod_event != NULL) 2716069154d5SJulian Elischer if ((error = (*type->mod_event)(mod, event, data))) { 2717069154d5SJulian Elischer mtx_enter(&ng_typelist_mtx, MTX_DEF); 27184cf49a43SJulian Elischer LIST_REMOVE(type, types); 2719069154d5SJulian Elischer mtx_exit(&ng_typelist_mtx, MTX_DEF); 2720069154d5SJulian Elischer } 27214cf49a43SJulian Elischer splx(s); 27224cf49a43SJulian Elischer break; 27234cf49a43SJulian Elischer 27244cf49a43SJulian Elischer case MOD_UNLOAD: 27254cf49a43SJulian Elischer s = splnet(); 27264cf49a43SJulian Elischer if (type->refs != 0) /* make sure no nodes exist! */ 27274cf49a43SJulian Elischer error = EBUSY; 27284cf49a43SJulian Elischer else { 27294cf49a43SJulian Elischer if (type->mod_event != NULL) { /* check with type */ 27304cf49a43SJulian Elischer error = (*type->mod_event)(mod, event, data); 27314cf49a43SJulian Elischer if (error != 0) { /* type refuses.. */ 27324cf49a43SJulian Elischer splx(s); 27334cf49a43SJulian Elischer break; 27344cf49a43SJulian Elischer } 27354cf49a43SJulian Elischer } 2736069154d5SJulian Elischer mtx_enter(&ng_typelist_mtx, MTX_DEF); 27374cf49a43SJulian Elischer LIST_REMOVE(type, types); 2738069154d5SJulian Elischer mtx_exit(&ng_typelist_mtx, MTX_DEF); 27394cf49a43SJulian Elischer } 27404cf49a43SJulian Elischer splx(s); 27414cf49a43SJulian Elischer break; 27424cf49a43SJulian Elischer 27434cf49a43SJulian Elischer default: 27444cf49a43SJulian Elischer if (type->mod_event != NULL) 27454cf49a43SJulian Elischer error = (*type->mod_event)(mod, event, data); 27464cf49a43SJulian Elischer else 27474cf49a43SJulian Elischer error = 0; /* XXX ? */ 27484cf49a43SJulian Elischer break; 27494cf49a43SJulian Elischer } 27504cf49a43SJulian Elischer return (error); 27514cf49a43SJulian Elischer } 27524cf49a43SJulian Elischer 27534cf49a43SJulian Elischer /* 27544cf49a43SJulian Elischer * Handle loading and unloading for this code. 27554cf49a43SJulian Elischer * The only thing we need to link into is the NETISR strucure. 27564cf49a43SJulian Elischer */ 27574cf49a43SJulian Elischer static int 27584cf49a43SJulian Elischer ngb_mod_event(module_t mod, int event, void *data) 27594cf49a43SJulian Elischer { 27604cf49a43SJulian Elischer int s, error = 0; 27614cf49a43SJulian Elischer 27624cf49a43SJulian Elischer switch (event) { 27634cf49a43SJulian Elischer case MOD_LOAD: 27644cf49a43SJulian Elischer /* Register line discipline */ 2765069154d5SJulian Elischer mtx_init(&ng_worklist_mtx, "netgraph worklist mutex", 0); 2766069154d5SJulian Elischer mtx_init(&ng_typelist_mtx, "netgraph types mutex", 0); 2767069154d5SJulian Elischer mtx_init(&ng_nodelist_mtx, "netgraph nodelist mutex", 0); 2768069154d5SJulian Elischer mtx_init(&ng_idhash_mtx, "netgraph idhash mutex", 0); 2769069154d5SJulian Elischer mtx_init(&ngq_mtx, "netgraph netisr mutex", 0); 27704cf49a43SJulian Elischer s = splimp(); 27714cf49a43SJulian Elischer error = register_netisr(NETISR_NETGRAPH, ngintr); 27724cf49a43SJulian Elischer splx(s); 27734cf49a43SJulian Elischer break; 27744cf49a43SJulian Elischer case MOD_UNLOAD: 27754cf49a43SJulian Elischer /* You cant unload it because an interface may be using it. */ 27764cf49a43SJulian Elischer error = EBUSY; 27774cf49a43SJulian Elischer break; 27784cf49a43SJulian Elischer default: 27794cf49a43SJulian Elischer error = EOPNOTSUPP; 27804cf49a43SJulian Elischer break; 27814cf49a43SJulian Elischer } 27824cf49a43SJulian Elischer return (error); 27834cf49a43SJulian Elischer } 27844cf49a43SJulian Elischer 27854cf49a43SJulian Elischer static moduledata_t netgraph_mod = { 27864cf49a43SJulian Elischer "netgraph", 27874cf49a43SJulian Elischer ngb_mod_event, 27884cf49a43SJulian Elischer (NULL) 27894cf49a43SJulian Elischer }; 27904cf49a43SJulian Elischer DECLARE_MODULE(netgraph, netgraph_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE); 27914cf49a43SJulian Elischer 27924cf49a43SJulian Elischer /************************************************************************ 2793069154d5SJulian Elischer Queue element get/free routines 27944cf49a43SJulian Elischer ************************************************************************/ 27954cf49a43SJulian Elischer 27964cf49a43SJulian Elischer 2797069154d5SJulian Elischer static int allocated; /* number of items malloc'd */ 2798069154d5SJulian Elischer static int maxalloc = 128; /* limit the damage of a leak */ 2799069154d5SJulian Elischer static const int ngqfreemax = 64;/* cache at most this many */ 2800069154d5SJulian Elischer static const int ngqfreelow = 4; /* try malloc if free < this */ 2801069154d5SJulian Elischer static volatile int ngqfreesize; /* number of cached entries */ 280230400f03SJulian Elischer #ifdef NETGRAPH_DEBUG 2803069154d5SJulian Elischer static TAILQ_HEAD(, ng_item) ng_itemlist = TAILQ_HEAD_INITIALIZER(ng_itemlist); 2804069154d5SJulian Elischer #endif 28054cf49a43SJulian Elischer /* 28064cf49a43SJulian Elischer * Get a queue entry 2807069154d5SJulian Elischer * This is usually called when a packet first enters netgraph. 2808069154d5SJulian Elischer * By definition, this is usually from an interrupt, or from a user. 2809069154d5SJulian Elischer * Users are not so important, but try be quick for the times that it's 2810069154d5SJulian Elischer * an interrupt. Use atomic operations to cope with collisions 2811069154d5SJulian Elischer * with interrupts and other processors. Assumes MALLOC is SMP safe. 2812069154d5SJulian Elischer * XXX If reserve is low, we should try to get 2 from malloc as this 2813069154d5SJulian Elischer * would indicate it often fails. 28144cf49a43SJulian Elischer */ 2815069154d5SJulian Elischer static item_p 28164cf49a43SJulian Elischer ng_getqblk(void) 28174cf49a43SJulian Elischer { 2818069154d5SJulian Elischer item_p item = NULL; 28194cf49a43SJulian Elischer 2820069154d5SJulian Elischer /* 2821069154d5SJulian Elischer * Try get a cached queue block, or else allocate a new one 2822069154d5SJulian Elischer * If we are less than our reserve, try malloc. If malloc 2823069154d5SJulian Elischer * fails, then that's what the reserve is for... 2824069154d5SJulian Elischer * Don't completely trust ngqfreesize, as it is subject 2825069154d5SJulian Elischer * to races.. (it'll eventually catch up but may be out by one or two 2826069154d5SJulian Elischer * for brief moments(under SMP or interrupts). 2827069154d5SJulian Elischer * ngqfree is the final arbiter. We have our little reserve 2828069154d5SJulian Elischer * because we use M_NOWAIT for malloc. This just helps us 2829069154d5SJulian Elischer * avoid dropping packets while not increasing the time 2830069154d5SJulian Elischer * we take to service the interrupt (on average) (we hope). 2831069154d5SJulian Elischer */ 2832069154d5SJulian Elischer for (;;) { 2833069154d5SJulian Elischer if ((ngqfreesize < ngqfreelow) || (ngqfree == NULL)) { 2834069154d5SJulian Elischer if (allocated < maxalloc) { /* don't leak forever */ 2835069154d5SJulian Elischer MALLOC(item, item_p , 2836069154d5SJulian Elischer sizeof(*item), M_NETGRAPH_ITEM, 2837069154d5SJulian Elischer (M_NOWAIT | M_ZERO)); 2838069154d5SJulian Elischer if (item) { 283930400f03SJulian Elischer #ifdef NETGRAPH_DEBUG 2840069154d5SJulian Elischer TAILQ_INSERT_TAIL(&ng_itemlist, 2841069154d5SJulian Elischer item, all); 284230400f03SJulian Elischer #endif /* NETGRAPH_DEBUG */ 2843069154d5SJulian Elischer atomic_add_int(&allocated, 1); 2844069154d5SJulian Elischer break; 28454cf49a43SJulian Elischer } 2846069154d5SJulian Elischer } 2847069154d5SJulian Elischer } 2848069154d5SJulian Elischer 2849069154d5SJulian Elischer /* 2850069154d5SJulian Elischer * We didn't or couldn't malloc. 2851069154d5SJulian Elischer * try get one from our cache. 2852069154d5SJulian Elischer * item must be NULL to get here. 2853069154d5SJulian Elischer */ 2854069154d5SJulian Elischer if ((item = ngqfree) != NULL) { 2855069154d5SJulian Elischer /* 2856069154d5SJulian Elischer * Atomically try grab the first item 2857069154d5SJulian Elischer * and put it's successor in its place. 2858069154d5SJulian Elischer * If we fail, just try again.. someone else 2859069154d5SJulian Elischer * beat us to this one or freed one. 2860069154d5SJulian Elischer * Don't worry about races with ngqfreesize. 2861069154d5SJulian Elischer * Close enough is good enough.. 2862069154d5SJulian Elischer */ 2863069154d5SJulian Elischer if (atomic_cmpset_ptr(&ngqfree, item, item->el_next)) { 2864069154d5SJulian Elischer atomic_subtract_int(&ngqfreesize, 1); 2865069154d5SJulian Elischer break; 2866069154d5SJulian Elischer } 2867069154d5SJulian Elischer item = NULL; 28684cf49a43SJulian Elischer } else { 2869069154d5SJulian Elischer /* We really ran out */ 2870069154d5SJulian Elischer break; 28714cf49a43SJulian Elischer } 2872069154d5SJulian Elischer } 2873069154d5SJulian Elischer item->el_flags &= ~NGQF_FREE; 2874069154d5SJulian Elischer return (item); 28754cf49a43SJulian Elischer } 28764cf49a43SJulian Elischer 28774cf49a43SJulian Elischer /* 28784cf49a43SJulian Elischer * Release a queue entry 28794cf49a43SJulian Elischer */ 2880069154d5SJulian Elischer void 2881069154d5SJulian Elischer ng_free_item(item_p item) 2882069154d5SJulian Elischer { 2883069154d5SJulian Elischer 2884069154d5SJulian Elischer /* 2885069154d5SJulian Elischer * The item may hold resources on it's own. We need to free 2886069154d5SJulian Elischer * these before we can free the item. What they are depends upon 2887069154d5SJulian Elischer * what kind of item it is. it is important that nodes zero 2888069154d5SJulian Elischer * out pointers to resources that they remove from the item 2889069154d5SJulian Elischer * or we release them again here. 2890069154d5SJulian Elischer */ 2891069154d5SJulian Elischer if (item->el_flags & NGQF_FREE) { 2892069154d5SJulian Elischer panic(" Freeing free queue item"); 2893069154d5SJulian Elischer } 2894069154d5SJulian Elischer switch (item->el_flags & NGQF_D_M) { 2895069154d5SJulian Elischer case NGQF_DATA: 2896069154d5SJulian Elischer /* If we have an mbuf and metadata still attached.. */ 2897069154d5SJulian Elischer NG_FREE_M(_NGI_M(item)); 2898069154d5SJulian Elischer NG_FREE_META(_NGI_META(item)); 2899069154d5SJulian Elischer break; 2900069154d5SJulian Elischer case NGQF_MESG: 2901069154d5SJulian Elischer _NGI_RETADDR(item) = NULL; 2902069154d5SJulian Elischer NG_FREE_MSG(_NGI_MSG(item)); 2903069154d5SJulian Elischer break; 2904069154d5SJulian Elischer } 2905069154d5SJulian Elischer /* If we still have a node or hook referenced... */ 2906069154d5SJulian Elischer if (item->el_dest) { 290730400f03SJulian Elischer NG_NODE_UNREF(item->el_dest); 2908069154d5SJulian Elischer item->el_dest = NULL; 2909069154d5SJulian Elischer } 2910069154d5SJulian Elischer if (item->el_hook) { 291130400f03SJulian Elischer NG_HOOK_UNREF(item->el_hook); 2912069154d5SJulian Elischer item->el_hook = NULL; 2913069154d5SJulian Elischer } 2914069154d5SJulian Elischer item->el_flags |= NGQF_FREE; 2915069154d5SJulian Elischer 2916069154d5SJulian Elischer /* 2917069154d5SJulian Elischer * We have freed any resources held by the item. 2918069154d5SJulian Elischer * now we can free the item itself. 2919069154d5SJulian Elischer */ 2920069154d5SJulian Elischer if (ngqfreesize < ngqfreemax) { /* don't worry about races */ 2921069154d5SJulian Elischer for (;;) { 2922069154d5SJulian Elischer item->el_next = ngqfree; 2923069154d5SJulian Elischer if (atomic_cmpset_ptr(&ngqfree, item->el_next, item)) { 2924069154d5SJulian Elischer break; 2925069154d5SJulian Elischer } 2926069154d5SJulian Elischer } 2927069154d5SJulian Elischer atomic_add_int(&ngqfreesize, 1); 2928069154d5SJulian Elischer } else { 2929069154d5SJulian Elischer /* This is the only place that should use this Macro */ 293030400f03SJulian Elischer #ifdef NETGRAPH_DEBUG 2931069154d5SJulian Elischer TAILQ_REMOVE(&ng_itemlist, item, all); 293230400f03SJulian Elischer #endif /* NETGRAPH_DEBUG */ 2933069154d5SJulian Elischer NG_FREE_ITEM_REAL(item); 2934069154d5SJulian Elischer atomic_subtract_int(&allocated, 1); 2935069154d5SJulian Elischer } 2936069154d5SJulian Elischer } 2937069154d5SJulian Elischer 293830400f03SJulian Elischer #ifdef NETGRAPH_DEBUG 293930400f03SJulian Elischer void 294030400f03SJulian Elischer dumphook (hook_p hook, char *file, int line) 294130400f03SJulian Elischer { 294230400f03SJulian Elischer printf("hook: name %s, %d refs, Last touched:\n", 294330400f03SJulian Elischer _NG_HOOK_NAME(hook), hook->hk_refs); 294430400f03SJulian Elischer printf(" Last active @ %s, line %d\n", 294530400f03SJulian Elischer hook->lastfile, hook->lastline); 294630400f03SJulian Elischer if (line) { 294730400f03SJulian Elischer printf(" problem discovered at file %s, line %d\n", file, line); 294830400f03SJulian Elischer } 294930400f03SJulian Elischer } 295030400f03SJulian Elischer 295130400f03SJulian Elischer void 295230400f03SJulian Elischer dumpnode(node_p node, char *file, int line) 295330400f03SJulian Elischer { 295430400f03SJulian Elischer printf("node: ID [%x]: type '%s', %d hooks, flags 0x%x, %d refs, %s:\n", 295530400f03SJulian Elischer ng_node2ID(node), node->nd_type->name, 295630400f03SJulian Elischer node->nd_numhooks, node->nd_flags, 295730400f03SJulian Elischer node->nd_refs, node->nd_name); 295830400f03SJulian Elischer printf(" Last active @ %s, line %d\n", 295930400f03SJulian Elischer node->lastfile, node->lastline); 296030400f03SJulian Elischer if (line) { 296130400f03SJulian Elischer printf(" problem discovered at file %s, line %d\n", file, line); 296230400f03SJulian Elischer } 296330400f03SJulian Elischer } 296430400f03SJulian Elischer 2965069154d5SJulian Elischer void 2966069154d5SJulian Elischer dumpitem(item_p item, char *file, int line) 2967069154d5SJulian Elischer { 2968069154d5SJulian Elischer if (item->el_flags & NGQF_FREE) { 2969069154d5SJulian Elischer printf(" Free item, freed at %s, line %d\n", 2970069154d5SJulian Elischer item->lastfile, item->lastline); 2971069154d5SJulian Elischer } else { 2972069154d5SJulian Elischer printf(" ACTIVE item, last used at %s, line %d", 2973069154d5SJulian Elischer item->lastfile, item->lastline); 2974069154d5SJulian Elischer if ((item->el_flags & NGQF_D_M) == NGQF_MESG) { 2975069154d5SJulian Elischer printf(" - retaddr[%d]:\n", _NGI_RETADDR(item)); 2976069154d5SJulian Elischer } else { 2977069154d5SJulian Elischer printf(" - [data]\n"); 2978069154d5SJulian Elischer } 2979069154d5SJulian Elischer } 298030400f03SJulian Elischer if (line) { 2981069154d5SJulian Elischer printf(" problem discovered at file %s, line %d\n", file, line); 298230400f03SJulian Elischer if (item->el_dest) { 298330400f03SJulian Elischer printf("node %p ([%x])\n", 2984069154d5SJulian Elischer item->el_dest, ng_node2ID(item->el_dest)); 2985069154d5SJulian Elischer } 298630400f03SJulian Elischer } 298730400f03SJulian Elischer } 298830400f03SJulian Elischer 298930400f03SJulian Elischer static void 299030400f03SJulian Elischer ng_dumpitems(void) 299130400f03SJulian Elischer { 299230400f03SJulian Elischer item_p item; 299330400f03SJulian Elischer int i = 1; 299430400f03SJulian Elischer TAILQ_FOREACH(item, &ng_itemlist, all) { 299530400f03SJulian Elischer printf("[%d] ", i++); 299630400f03SJulian Elischer dumpitem(item, NULL, 0); 299730400f03SJulian Elischer } 299830400f03SJulian Elischer } 299930400f03SJulian Elischer 300030400f03SJulian Elischer static void 300130400f03SJulian Elischer ng_dumpnodes(void) 300230400f03SJulian Elischer { 300330400f03SJulian Elischer node_p node; 300430400f03SJulian Elischer int i = 1; 300530400f03SJulian Elischer SLIST_FOREACH(node, &ng_allnodes, nd_all) { 300630400f03SJulian Elischer printf("[%d] ", i++); 300730400f03SJulian Elischer dumpnode(node, NULL, 0); 300830400f03SJulian Elischer } 300930400f03SJulian Elischer } 301030400f03SJulian Elischer 301130400f03SJulian Elischer static void 301230400f03SJulian Elischer ng_dumphooks(void) 301330400f03SJulian Elischer { 301430400f03SJulian Elischer hook_p hook; 301530400f03SJulian Elischer int i = 1; 301630400f03SJulian Elischer SLIST_FOREACH(hook, &ng_allhooks, hk_all) { 301730400f03SJulian Elischer printf("[%d] ", i++); 301830400f03SJulian Elischer dumphook(hook, NULL, 0); 301930400f03SJulian Elischer } 302030400f03SJulian Elischer } 3021069154d5SJulian Elischer 3022069154d5SJulian Elischer static int 3023069154d5SJulian Elischer sysctl_debug_ng_dump_items(SYSCTL_HANDLER_ARGS) 3024069154d5SJulian Elischer { 302530400f03SJulian Elischer static int count; 3026069154d5SJulian Elischer int error; 3027069154d5SJulian Elischer int val; 3028069154d5SJulian Elischer int i; 3029069154d5SJulian Elischer 3030069154d5SJulian Elischer val = allocated; 3031069154d5SJulian Elischer i = 1; 3032069154d5SJulian Elischer error = sysctl_handle_int(oidp, &val, sizeof(int), req); 303330400f03SJulian Elischer if(count++ & 1) { /* for some reason sysctl calls it twice */ 303430400f03SJulian Elischer ng_dumpitems(); 303530400f03SJulian Elischer ng_dumpnodes(); 303630400f03SJulian Elischer ng_dumphooks(); 3037069154d5SJulian Elischer } 3038069154d5SJulian Elischer return error; 3039069154d5SJulian Elischer } 3040069154d5SJulian Elischer 3041069154d5SJulian Elischer SYSCTL_PROC(_debug, OID_AUTO, ng_dump_items, CTLTYPE_INT | CTLFLAG_RD, 3042069154d5SJulian Elischer 0, 0, sysctl_debug_ng_dump_items, "I", "Number of allocated items"); 304330400f03SJulian Elischer #endif /* NETGRAPH_DEBUG */ 3044069154d5SJulian Elischer 3045069154d5SJulian Elischer 3046069154d5SJulian Elischer /*********************************************************************** 3047069154d5SJulian Elischer * Worklist routines 3048069154d5SJulian Elischer **********************************************************************/ 3049069154d5SJulian Elischer /* NETISR thread enters here */ 3050069154d5SJulian Elischer /* 3051069154d5SJulian Elischer * Pick a node off the list of nodes with work, 3052069154d5SJulian Elischer * try get an item to process off it. 3053069154d5SJulian Elischer * If there are no more, remove the node from the list. 3054069154d5SJulian Elischer */ 3055069154d5SJulian Elischer static void 3056069154d5SJulian Elischer ngintr(void) 3057069154d5SJulian Elischer { 3058069154d5SJulian Elischer item_p item; 3059069154d5SJulian Elischer node_p node = NULL; 3060069154d5SJulian Elischer 3061069154d5SJulian Elischer for (;;) { 3062069154d5SJulian Elischer mtx_enter(&ng_worklist_mtx, MTX_SPIN); 3063069154d5SJulian Elischer node = TAILQ_FIRST(&ng_worklist); 3064069154d5SJulian Elischer if (!node) { 3065069154d5SJulian Elischer mtx_exit(&ng_worklist_mtx, MTX_SPIN); 3066069154d5SJulian Elischer break; 3067069154d5SJulian Elischer } 306830400f03SJulian Elischer TAILQ_REMOVE(&ng_worklist, node, nd_work); 3069069154d5SJulian Elischer mtx_exit(&ng_worklist_mtx, MTX_SPIN); 3070069154d5SJulian Elischer /* 3071069154d5SJulian Elischer * We have the node. We also take over the reference 3072069154d5SJulian Elischer * that the list had on it. 3073069154d5SJulian Elischer * Now process as much as you can, until it won't 3074069154d5SJulian Elischer * let you have another item off the queue. 3075069154d5SJulian Elischer * All this time, keep the reference 3076069154d5SJulian Elischer * that lets us be sure that the node still exists. 3077069154d5SJulian Elischer * Let the reference go at the last minute. 3078069154d5SJulian Elischer */ 3079069154d5SJulian Elischer for (;;) { 308030400f03SJulian Elischer mtx_enter(&node->nd_input_queue.q_mtx, MTX_SPIN); 308130400f03SJulian Elischer item = ng_dequeue(&node->nd_input_queue); 3082069154d5SJulian Elischer if (item == NULL) { 3083069154d5SJulian Elischer /* 3084069154d5SJulian Elischer * Say we are on the queue as long as 3085069154d5SJulian Elischer * we are processing it here. 3086069154d5SJulian Elischer * it probably wouldn't come here while we 3087069154d5SJulian Elischer * are processing anyhow. 3088069154d5SJulian Elischer */ 308930400f03SJulian Elischer node->nd_flags &= ~NG_WORKQ; 309030400f03SJulian Elischer mtx_exit(&node->nd_input_queue.q_mtx, MTX_SPIN); 309130400f03SJulian Elischer NG_NODE_UNREF(node); 3092069154d5SJulian Elischer break; /* go look for another node */ 3093069154d5SJulian Elischer } else { 309430400f03SJulian Elischer mtx_exit(&node->nd_input_queue.q_mtx, MTX_SPIN); 309530400f03SJulian Elischer #ifdef NETGRAPH_DEBUG 3096069154d5SJulian Elischer _ngi_check(item, __FILE__, __LINE__); 3097069154d5SJulian Elischer #endif 3098069154d5SJulian Elischer ng_apply_item(node, item); 3099069154d5SJulian Elischer } 3100069154d5SJulian Elischer } 3101069154d5SJulian Elischer } 3102069154d5SJulian Elischer } 3103069154d5SJulian Elischer 3104069154d5SJulian Elischer static void 3105069154d5SJulian Elischer ng_worklist_remove(node_p node) 3106069154d5SJulian Elischer { 3107069154d5SJulian Elischer mtx_enter(&ng_worklist_mtx, MTX_SPIN); 310830400f03SJulian Elischer if (node->nd_flags & NG_WORKQ) { 310930400f03SJulian Elischer TAILQ_REMOVE(&ng_worklist, node, nd_work); 311030400f03SJulian Elischer NG_NODE_UNREF(node); 3111069154d5SJulian Elischer } 311230400f03SJulian Elischer node->nd_flags &= ~NG_WORKQ; 3113069154d5SJulian Elischer mtx_exit(&ng_worklist_mtx, MTX_SPIN); 3114069154d5SJulian Elischer } 3115069154d5SJulian Elischer 3116069154d5SJulian Elischer static void 3117069154d5SJulian Elischer ng_setisr(node_p node) 3118069154d5SJulian Elischer { 3119069154d5SJulian Elischer mtx_enter(&ng_worklist_mtx, MTX_SPIN); 312030400f03SJulian Elischer if ((node->nd_flags & NG_WORKQ) == 0) { 3121069154d5SJulian Elischer /* 3122069154d5SJulian Elischer * If we are not already on the work queue, 3123069154d5SJulian Elischer * then put us on. 3124069154d5SJulian Elischer */ 312530400f03SJulian Elischer node->nd_flags |= NG_WORKQ; 312630400f03SJulian Elischer TAILQ_INSERT_TAIL(&ng_worklist, node, nd_work); 312730400f03SJulian Elischer NG_NODE_REF(node); 3128069154d5SJulian Elischer } 3129069154d5SJulian Elischer mtx_exit(&ng_worklist_mtx, MTX_SPIN); 3130069154d5SJulian Elischer schednetisr(NETISR_NETGRAPH); 3131069154d5SJulian Elischer } 3132069154d5SJulian Elischer 3133069154d5SJulian Elischer 3134069154d5SJulian Elischer /*********************************************************************** 3135069154d5SJulian Elischer * Externally useable functions to set up a queue item ready for sending 3136069154d5SJulian Elischer ***********************************************************************/ 3137069154d5SJulian Elischer 313830400f03SJulian Elischer #ifdef NETGRAPH_DEBUG 313930400f03SJulian Elischer #define ITEM_DEBUG_CHECKS \ 31404cf49a43SJulian Elischer do { \ 3141069154d5SJulian Elischer if (item->el_dest ) { \ 3142069154d5SJulian Elischer printf("item already has node"); \ 3143069154d5SJulian Elischer Debugger("has node"); \ 314430400f03SJulian Elischer NG_NODE_UNREF(item->el_dest); \ 3145069154d5SJulian Elischer item->el_dest = NULL; \ 3146069154d5SJulian Elischer } \ 3147069154d5SJulian Elischer if (item->el_hook ) { \ 3148069154d5SJulian Elischer printf("item already has hook"); \ 3149069154d5SJulian Elischer Debugger("has hook"); \ 315030400f03SJulian Elischer NG_HOOK_UNREF(item->el_hook); \ 3151069154d5SJulian Elischer item->el_hook = NULL; \ 3152069154d5SJulian Elischer } \ 3153069154d5SJulian Elischer } while (0) 3154069154d5SJulian Elischer #else 315530400f03SJulian Elischer #define ITEM_DEBUG_CHECKS 3156069154d5SJulian Elischer #endif 3157069154d5SJulian Elischer 3158069154d5SJulian Elischer /* 3159069154d5SJulian Elischer * Put elements into the item. 3160069154d5SJulian Elischer * Hook and node references will be removed when the item is dequeued. 3161069154d5SJulian Elischer * (or equivalent) 3162069154d5SJulian Elischer * (XXX) Unsafe because no reference held by peer on remote node. 3163069154d5SJulian Elischer * remote node might go away in this timescale. 3164069154d5SJulian Elischer * We know the hooks can't go away because that would require getting 3165069154d5SJulian Elischer * a writer item on both nodes and we must have at least a reader 3166069154d5SJulian Elischer * here to eb able to do this. 3167069154d5SJulian Elischer * Note that the hook loaded is the REMOTE hook. 3168069154d5SJulian Elischer * 3169069154d5SJulian Elischer * This is possibly in the critical path for new data. 3170069154d5SJulian Elischer */ 3171069154d5SJulian Elischer item_p 3172069154d5SJulian Elischer ng_package_data(struct mbuf *m, meta_p meta) 3173069154d5SJulian Elischer { 3174069154d5SJulian Elischer item_p item; 3175069154d5SJulian Elischer 3176069154d5SJulian Elischer if ((item = ng_getqblk()) == NULL) { 3177069154d5SJulian Elischer NG_FREE_M(m); 3178069154d5SJulian Elischer NG_FREE_META(meta); 3179069154d5SJulian Elischer return (NULL); 3180069154d5SJulian Elischer } 318130400f03SJulian Elischer ITEM_DEBUG_CHECKS; 3182069154d5SJulian Elischer item->el_flags = NGQF_DATA; 3183069154d5SJulian Elischer item->el_next = NULL; 3184069154d5SJulian Elischer NGI_M(item) = m; 3185069154d5SJulian Elischer NGI_META(item) = meta; 3186069154d5SJulian Elischer return (item); 3187069154d5SJulian Elischer } 3188069154d5SJulian Elischer 3189069154d5SJulian Elischer /* 3190069154d5SJulian Elischer * Allocate a queue item and put items into it.. 3191069154d5SJulian Elischer * Evaluate the address as this will be needed to queue it and 3192069154d5SJulian Elischer * to work out what some of the fields should be. 3193069154d5SJulian Elischer * Hook and node references will be removed when the item is dequeued. 3194069154d5SJulian Elischer * (or equivalent) 3195069154d5SJulian Elischer */ 3196069154d5SJulian Elischer item_p 3197069154d5SJulian Elischer ng_package_msg(struct ng_mesg *msg) 3198069154d5SJulian Elischer { 3199069154d5SJulian Elischer item_p item; 3200069154d5SJulian Elischer 3201069154d5SJulian Elischer if ((item = ng_getqblk()) == NULL) { 3202069154d5SJulian Elischer if ((msg->header.flags & NGF_STATIC) == 0) { 3203069154d5SJulian Elischer NG_FREE_MSG(msg); 3204069154d5SJulian Elischer } 3205069154d5SJulian Elischer return (NULL); 3206069154d5SJulian Elischer } 320730400f03SJulian Elischer ITEM_DEBUG_CHECKS; 3208069154d5SJulian Elischer item->el_flags = NGQF_MESG; 3209069154d5SJulian Elischer item->el_next = NULL; 3210069154d5SJulian Elischer /* 3211069154d5SJulian Elischer * Set the current lasthook into the queue item 3212069154d5SJulian Elischer */ 3213069154d5SJulian Elischer NGI_MSG(item) = msg; 3214069154d5SJulian Elischer NGI_RETADDR(item) = NULL; 3215069154d5SJulian Elischer return (item); 3216069154d5SJulian Elischer } 3217069154d5SJulian Elischer 3218069154d5SJulian Elischer 3219069154d5SJulian Elischer 3220069154d5SJulian Elischer #define SET_RETADDR \ 3221069154d5SJulian Elischer do { /* Data items don't have retaddrs */ \ 3222069154d5SJulian Elischer if ((item->el_flags & NGQF_D_M) == NGQF_MESG) { \ 3223069154d5SJulian Elischer if (retaddr) { \ 3224069154d5SJulian Elischer NGI_RETADDR(item) = retaddr; \ 32254cf49a43SJulian Elischer } else { \ 3226069154d5SJulian Elischer /* \ 3227069154d5SJulian Elischer * The old return address should be ok. \ 3228069154d5SJulian Elischer * If there isn't one, use the address \ 3229069154d5SJulian Elischer * here. \ 3230069154d5SJulian Elischer */ \ 3231069154d5SJulian Elischer if (NGI_RETADDR(item) == 0) { \ 3232069154d5SJulian Elischer NGI_RETADDR(item) \ 3233069154d5SJulian Elischer = ng_node2ID(here); \ 3234069154d5SJulian Elischer } \ 3235069154d5SJulian Elischer } \ 32364cf49a43SJulian Elischer } \ 32374cf49a43SJulian Elischer } while (0) 32384cf49a43SJulian Elischer 32394cf49a43SJulian Elischer int 3240069154d5SJulian Elischer ng_address_hook(node_p here, item_p item, hook_p hook, ng_ID_t retaddr) 32414cf49a43SJulian Elischer { 324230400f03SJulian Elischer ITEM_DEBUG_CHECKS; 3243069154d5SJulian Elischer /* 3244069154d5SJulian Elischer * Quick sanity check.. 324530400f03SJulian Elischer * Since a hook holds a reference on it's node, once we know 324630400f03SJulian Elischer * that the peer is still connected (even if invalid,) we know 324730400f03SJulian Elischer * that the peer node is present, though maybe invalid. 3248069154d5SJulian Elischer */ 3249069154d5SJulian Elischer if ((hook == NULL) 325030400f03SJulian Elischer || NG_HOOK_NOT_VALID(hook) 325130400f03SJulian Elischer || (NG_HOOK_PEER(hook) == NULL) 325230400f03SJulian Elischer || NG_HOOK_NOT_VALID(NG_HOOK_PEER(hook)) 325330400f03SJulian Elischer || NG_NODE_NOT_VALID(NG_PEER_NODE(hook))) { 3254069154d5SJulian Elischer NG_FREE_ITEM(item); 325530400f03SJulian Elischer TRAP_ERROR 3256069154d5SJulian Elischer return (EINVAL); 32574cf49a43SJulian Elischer } 32584cf49a43SJulian Elischer 32594cf49a43SJulian Elischer /* 3260069154d5SJulian Elischer * Transfer our interest to the other (peer) end. 32614cf49a43SJulian Elischer */ 326230400f03SJulian Elischer item->el_hook = NG_HOOK_PEER(hook); 326330400f03SJulian Elischer NG_HOOK_REF(item->el_hook); /* Don't let it go while on the queue */ 326430400f03SJulian Elischer item->el_dest = NG_PEER_NODE(hook); 326530400f03SJulian Elischer NG_NODE_REF(item->el_dest); /* Nor this */ 3266069154d5SJulian Elischer SET_RETADDR; 3267069154d5SJulian Elischer return (0); 3268069154d5SJulian Elischer } 3269069154d5SJulian Elischer 32704cf49a43SJulian Elischer int 3271069154d5SJulian Elischer ng_address_path(node_p here, item_p item, char *address, ng_ID_t retaddr) 32724cf49a43SJulian Elischer { 32734cf49a43SJulian Elischer node_p dest = NULL; 3274069154d5SJulian Elischer hook_p hook = NULL; 32754cf49a43SJulian Elischer int error; 3276069154d5SJulian Elischer 327730400f03SJulian Elischer ITEM_DEBUG_CHECKS; 3278069154d5SJulian Elischer /* 3279069154d5SJulian Elischer * Note that ng_path2noderef increments the reference count 3280069154d5SJulian Elischer * on the node for us if it finds one. So we don't have to. 3281069154d5SJulian Elischer */ 3282069154d5SJulian Elischer error = ng_path2noderef(here, address, &dest, &hook); 3283069154d5SJulian Elischer if (error) { 3284069154d5SJulian Elischer NG_FREE_ITEM(item); 328530400f03SJulian Elischer return (error); 3286069154d5SJulian Elischer } 3287069154d5SJulian Elischer item->el_dest = dest; 3288069154d5SJulian Elischer if (( item->el_hook = hook)) 328930400f03SJulian Elischer NG_HOOK_REF(hook); /* don't let it go while on the queue */ 3290069154d5SJulian Elischer SET_RETADDR; 3291069154d5SJulian Elischer return (0); 3292069154d5SJulian Elischer } 3293069154d5SJulian Elischer 3294069154d5SJulian Elischer int 3295069154d5SJulian Elischer ng_address_ID(node_p here, item_p item, ng_ID_t ID, ng_ID_t retaddr) 3296069154d5SJulian Elischer { 3297069154d5SJulian Elischer node_p dest; 3298069154d5SJulian Elischer 329930400f03SJulian Elischer ITEM_DEBUG_CHECKS; 3300069154d5SJulian Elischer /* 3301069154d5SJulian Elischer * Find the target node. 3302069154d5SJulian Elischer */ 3303069154d5SJulian Elischer dest = ng_ID2noderef(ID); /* GETS REFERENCE! */ 3304069154d5SJulian Elischer if (dest == NULL) { 3305069154d5SJulian Elischer NG_FREE_ITEM(item); 330630400f03SJulian Elischer TRAP_ERROR 3307069154d5SJulian Elischer return(EINVAL); 3308069154d5SJulian Elischer } 3309069154d5SJulian Elischer /* Fill out the contents */ 3310069154d5SJulian Elischer item->el_flags = NGQF_MESG; 3311069154d5SJulian Elischer item->el_next = NULL; 3312069154d5SJulian Elischer item->el_dest = dest; 3313069154d5SJulian Elischer item->el_hook = NULL; 3314069154d5SJulian Elischer SET_RETADDR; 3315069154d5SJulian Elischer return (0); 3316069154d5SJulian Elischer } 3317069154d5SJulian Elischer 3318069154d5SJulian Elischer /* 3319069154d5SJulian Elischer * special case to send a message to self (e.g. destroy node) 3320069154d5SJulian Elischer * Possibly indicate an arrival hook too. 3321069154d5SJulian Elischer * Useful for removing that hook :-) 3322069154d5SJulian Elischer */ 3323069154d5SJulian Elischer item_p 3324069154d5SJulian Elischer ng_package_msg_self(node_p here, hook_p hook, struct ng_mesg *msg) 3325069154d5SJulian Elischer { 3326069154d5SJulian Elischer item_p item; 33274cf49a43SJulian Elischer 3328859a4d16SJulian Elischer /* 3329859a4d16SJulian Elischer * Find the target node. 3330859a4d16SJulian Elischer * If there is a HOOK argument, then use that in preference 3331859a4d16SJulian Elischer * to the address. 3332859a4d16SJulian Elischer */ 3333069154d5SJulian Elischer if ((item = ng_getqblk()) == NULL) { 3334069154d5SJulian Elischer if ((msg->header.flags & NGF_STATIC) == 0) { 3335069154d5SJulian Elischer NG_FREE_MSG(msg); 33364cf49a43SJulian Elischer } 3337069154d5SJulian Elischer return (NULL); 33384cf49a43SJulian Elischer } 33394cf49a43SJulian Elischer 33404cf49a43SJulian Elischer /* Fill out the contents */ 3341069154d5SJulian Elischer item->el_flags = NGQF_MESG; 3342069154d5SJulian Elischer item->el_next = NULL; 3343069154d5SJulian Elischer item->el_dest = here; 334430400f03SJulian Elischer NG_NODE_REF(here); 3345069154d5SJulian Elischer item->el_hook = hook; 3346069154d5SJulian Elischer if (hook) 334730400f03SJulian Elischer NG_HOOK_REF(hook); 3348069154d5SJulian Elischer NGI_MSG(item) = msg; 3349069154d5SJulian Elischer NGI_RETADDR(item) = ng_node2ID(here); 3350069154d5SJulian Elischer return (item); 33514cf49a43SJulian Elischer } 33524cf49a43SJulian Elischer 33534cf49a43SJulian Elischer /* 3354069154d5SJulian Elischer * Set the address, if none given, give the node here. 33554cf49a43SJulian Elischer */ 3356069154d5SJulian Elischer void 3357069154d5SJulian Elischer ng_replace_retaddr(node_p here, item_p item, ng_ID_t retaddr) 33584cf49a43SJulian Elischer { 3359069154d5SJulian Elischer if (retaddr) { 3360069154d5SJulian Elischer NGI_RETADDR(item) = retaddr; 3361069154d5SJulian Elischer } else { 3362069154d5SJulian Elischer /* 3363069154d5SJulian Elischer * The old return address should be ok. 3364069154d5SJulian Elischer * If there isn't one, use the address here. 3365069154d5SJulian Elischer */ 3366069154d5SJulian Elischer NGI_RETADDR(item) = ng_node2ID(here); 3367069154d5SJulian Elischer } 3368069154d5SJulian Elischer } 3369069154d5SJulian Elischer 3370069154d5SJulian Elischer #define TESTING 3371069154d5SJulian Elischer #ifdef TESTING 3372069154d5SJulian Elischer /* just test all the macros */ 3373069154d5SJulian Elischer void 3374069154d5SJulian Elischer ng_macro_test(item_p item); 3375069154d5SJulian Elischer void 3376069154d5SJulian Elischer ng_macro_test(item_p item) 3377069154d5SJulian Elischer { 3378069154d5SJulian Elischer node_p node = NULL; 3379069154d5SJulian Elischer hook_p hook = NULL; 33804cf49a43SJulian Elischer struct mbuf *m; 33814cf49a43SJulian Elischer meta_p meta; 33824cf49a43SJulian Elischer struct ng_mesg *msg; 3383069154d5SJulian Elischer ng_ID_t retaddr; 3384069154d5SJulian Elischer int error; 33854cf49a43SJulian Elischer 3386069154d5SJulian Elischer NGI_GET_M(item, m); 3387069154d5SJulian Elischer NGI_GET_META(item, meta); 3388069154d5SJulian Elischer NGI_GET_MSG(item, msg); 3389069154d5SJulian Elischer retaddr = NGI_RETADDR(item); 3390069154d5SJulian Elischer NG_SEND_DATA(error, hook, m, meta); 3391069154d5SJulian Elischer NG_SEND_DATA_ONLY(error, hook, m); 3392069154d5SJulian Elischer NG_FWD_NEW_DATA(error, item, hook, m); 339330400f03SJulian Elischer NG_FWD_ITEM_HOOK(error, item, hook); 3394069154d5SJulian Elischer NG_SEND_MSG_HOOK(error, node, msg, hook, retaddr); 3395069154d5SJulian Elischer NG_SEND_MSG_ID(error, node, msg, retaddr, retaddr); 3396069154d5SJulian Elischer NG_SEND_MSG_PATH(error, node, msg, ".:", retaddr); 3397069154d5SJulian Elischer NG_QUEUE_MSG(error, node, msg, ".:", retaddr); 3398069154d5SJulian Elischer NG_FWD_MSG_HOOK(error, node, item, hook, retaddr); 33994cf49a43SJulian Elischer } 3400069154d5SJulian Elischer #endif /* TESTING */ 34014cf49a43SJulian Elischer 3402