ng_base.c revision 72946
152419Sjulian/* 252419Sjulian * ng_base.c 352419Sjulian * 452419Sjulian * Copyright (c) 1996-1999 Whistle Communications, Inc. 552419Sjulian * All rights reserved. 670700Sjulian * 752419Sjulian * Subject to the following obligations and disclaimer of warranty, use and 852419Sjulian * redistribution of this software, in source or object code forms, with or 952419Sjulian * without modifications are expressly permitted by Whistle Communications; 1052419Sjulian * provided, however, that: 1152419Sjulian * 1. Any and all reproductions of the source or object code must include the 1252419Sjulian * copyright notice above and the following disclaimer of warranties; and 1352419Sjulian * 2. No rights are granted, in any manner or form, to use Whistle 1452419Sjulian * Communications, Inc. trademarks, including the mark "WHISTLE 1552419Sjulian * COMMUNICATIONS" on advertising, endorsements, or otherwise except as 1652419Sjulian * such appears in the above copyright notice or in the software. 1770700Sjulian * 1852419Sjulian * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND 1952419Sjulian * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO 2052419Sjulian * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE, 2152419Sjulian * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF 2252419Sjulian * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. 2352419Sjulian * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY 2452419Sjulian * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS 2552419Sjulian * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE. 2652419Sjulian * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES 2752419Sjulian * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING 2852419Sjulian * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 2952419Sjulian * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR 3052419Sjulian * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY 3152419Sjulian * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 3252419Sjulian * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 3352419Sjulian * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY 3452419Sjulian * OF SUCH DAMAGE. 3552419Sjulian * 3667506Sjulian * Authors: Julian Elischer <julian@freebsd.org> 3767506Sjulian * Archie Cobbs <archie@freebsd.org> 3852419Sjulian * 3952419Sjulian * $FreeBSD: head/sys/netgraph/ng_base.c 72946 2001-02-23 16:34:22Z julian $ 4052419Sjulian * $Whistle: ng_base.c,v 1.39 1999/01/28 23:54:53 julian Exp $ 4152419Sjulian */ 4252419Sjulian 4352419Sjulian/* 4452419Sjulian * This file implements the base netgraph code. 4552419Sjulian */ 4652419Sjulian 4752419Sjulian#include <sys/param.h> 4852419Sjulian#include <sys/systm.h> 4952419Sjulian#include <sys/errno.h> 5052419Sjulian#include <sys/kernel.h> 5152419Sjulian#include <sys/malloc.h> 5252419Sjulian#include <sys/syslog.h> 5370700Sjulian#include <sys/sysctl.h> 5452419Sjulian#include <sys/linker.h> 5552419Sjulian#include <sys/queue.h> 5652419Sjulian#include <sys/mbuf.h> 5752843Sphk#include <sys/ctype.h> 5872946Sjulian#include <sys/sysctl.h> 5952816Sarchie#include <machine/limits.h> 6052419Sjulian 6152419Sjulian#include <net/netisr.h> 6252419Sjulian 6352419Sjulian#include <netgraph/ng_message.h> 6452419Sjulian#include <netgraph/netgraph.h> 6553913Sarchie#include <netgraph/ng_parse.h> 6652419Sjulian 6772053SjulianMODULE_VERSION(netgraph, NG_ABI_VERSION); 6859756Speter 6970784Sjulian/* List of all active nodes */ 7070700Sjulianstatic LIST_HEAD(, ng_node) ng_nodelist; 7170700Sjulianstatic struct mtx ng_nodelist_mtx; 7252419Sjulian 7370784Sjulian#ifdef NETGRAPH_DEBUG 7470784Sjulian 7570784Sjulianstatic SLIST_HEAD(, ng_node) ng_allnodes; 7670784Sjulianstatic LIST_HEAD(, ng_node) ng_freenodes; /* in debug, we never free() them */ 7770784Sjulianstatic SLIST_HEAD(, ng_hook) ng_allhooks; 7870784Sjulianstatic LIST_HEAD(, ng_hook) ng_freehooks; /* in debug, we never free() them */ 7970784Sjulian 8070784Sjulianstatic void ng_dumpitems(void); 8170784Sjulianstatic void ng_dumpnodes(void); 8270784Sjulianstatic void ng_dumphooks(void); 8370784Sjulian 8470784Sjulian#endif /* NETGRAPH_DEBUG */ 8570935Sjulian/* 8670935Sjulian * DEAD versions of the structures. 8770935Sjulian * In order to avoid races, it is sometimes neccesary to point 8870935Sjulian * at SOMETHING even though theoretically, the current entity is 8970935Sjulian * INVALID. Use these to avoid these races. 9070935Sjulian */ 9170935Sjulianstruct ng_type ng_deadtype = { 9270935Sjulian NG_ABI_VERSION, 9370935Sjulian "dead", 9470935Sjulian NULL, /* modevent */ 9570935Sjulian NULL, /* constructor */ 9670935Sjulian NULL, /* rcvmsg */ 9770935Sjulian NULL, /* shutdown */ 9870935Sjulian NULL, /* newhook */ 9970935Sjulian NULL, /* findhook */ 10070935Sjulian NULL, /* connect */ 10170935Sjulian NULL, /* rcvdata */ 10270935Sjulian NULL, /* disconnect */ 10370935Sjulian NULL, /* cmdlist */ 10470935Sjulian}; 10570784Sjulian 10670935Sjulianstruct ng_node ng_deadnode = { 10770935Sjulian "dead", 10870935Sjulian &ng_deadtype, 10970935Sjulian NG_INVALID, 11070935Sjulian 1, /* refs */ 11170935Sjulian 0, /* numhooks */ 11270935Sjulian NULL, /* private */ 11370935Sjulian 0, /* ID */ 11470935Sjulian LIST_HEAD_INITIALIZER(ng_deadnode.hooks), 11570935Sjulian {}, /* all_nodes list entry */ 11670935Sjulian {}, /* id hashtable list entry */ 11770935Sjulian {}, /* workqueue entry */ 11870935Sjulian { 0, 11970935Sjulian {}, /* should never use! (should hang) */ 12070935Sjulian NULL, 12170935Sjulian &ng_deadnode.nd_input_queue.queue, 12270935Sjulian &ng_deadnode 12370935Sjulian }, 12470935Sjulian#ifdef NETGRAPH_DEBUG 12570935Sjulian ND_MAGIC, 12670935Sjulian __FILE__, 12770935Sjulian __LINE__, 12870935Sjulian {NULL} 12970935Sjulian#endif /* NETGRAPH_DEBUG */ 13070935Sjulian}; 13170935Sjulian 13270935Sjulianstruct ng_hook ng_deadhook = { 13370935Sjulian "dead", 13470935Sjulian NULL, /* private */ 13570935Sjulian HK_INVALID | HK_DEAD, 13670935Sjulian 1, /* refs always >= 1 */ 13770935Sjulian &ng_deadhook, /* Peer is self */ 13870935Sjulian &ng_deadnode, /* attached to deadnode */ 13970935Sjulian {}, /* hooks list */ 14071885Sjulian NULL, /* override rcvmsg() */ 14171885Sjulian NULL, /* override rcvdata() */ 14270935Sjulian#ifdef NETGRAPH_DEBUG 14370935Sjulian HK_MAGIC, 14470935Sjulian __FILE__, 14570935Sjulian __LINE__, 14670935Sjulian {NULL} 14770935Sjulian#endif /* NETGRAPH_DEBUG */ 14870935Sjulian}; 14970935Sjulian 15070935Sjulian/* 15170935Sjulian * END DEAD STRUCTURES 15270935Sjulian */ 15370700Sjulian/* List nodes with unallocated work */ 15470700Sjulianstatic TAILQ_HEAD(, ng_node) ng_worklist = TAILQ_HEAD_INITIALIZER(ng_worklist); 15571902Sjulianstatic struct mtx ng_worklist_mtx; /* MUST LOCK NODE FIRST */ 15670700Sjulian 15752419Sjulian/* List of installed types */ 15870700Sjulianstatic LIST_HEAD(, ng_type) ng_typelist; 15970700Sjulianstatic struct mtx ng_typelist_mtx; 16052419Sjulian 16170700Sjulian/* Hash related definitions */ 16271354Sjulian/* XXX Don't need to initialise them because it's a LIST */ 16371354Sjulian#define NG_ID_HASH_SIZE 32 /* most systems wont need even this many */ 16471354Sjulianstatic LIST_HEAD(, ng_node) ng_ID_hash[NG_ID_HASH_SIZE]; 16570700Sjulianstatic struct mtx ng_idhash_mtx; 16671354Sjulian/* Method to find a node.. used twice so do it here */ 16771354Sjulian#define NG_IDHASH_FN(ID) ((ID) % (NG_ID_HASH_SIZE)) 16871354Sjulian#define NG_IDHASH_FIND(ID, node) \ 16971354Sjulian do { \ 17071354Sjulian LIST_FOREACH(node, &ng_ID_hash[NG_IDHASH_FN(ID)], \ 17171354Sjulian nd_idnodes) { \ 17271354Sjulian if (NG_NODE_IS_VALID(node) \ 17371354Sjulian && (NG_NODE_ID(node) == ID)) { \ 17471354Sjulian break; \ 17571354Sjulian } \ 17671354Sjulian } \ 17771354Sjulian } while (0) 17852722Sjulian 17970700Sjulian/* Mutex that protects the free queue item list */ 18070700Sjulianstatic volatile item_p ngqfree; /* free ones */ 18170700Sjulianstatic struct mtx ngq_mtx; 18270700Sjulian 18352419Sjulian/* Internal functions */ 18452419Sjulianstatic int ng_add_hook(node_p node, const char *name, hook_p * hookp); 18570700Sjulianstatic int ng_generic_msg(node_p here, item_p item, hook_p lasthook); 18652722Sjulianstatic ng_ID_t ng_decodeidname(const char *name); 18752419Sjulianstatic int ngb_mod_event(module_t mod, int event, void *data); 18870700Sjulianstatic void ng_worklist_remove(node_p node); 18952419Sjulianstatic void ngintr(void); 19071849Sjulianstatic int ng_apply_item(item_p item); 19170700Sjulianstatic void ng_flush_input_queue(struct ng_queue * ngq); 19270700Sjulianstatic void ng_setisr(node_p node); 19370700Sjulianstatic node_p ng_ID2noderef(ng_ID_t ID); 19471047Sjulianstatic int ng_con_nodes(node_p node, const char *name, node_p node2, 19571047Sjulian const char *name2); 19671849Sjulianstatic void ng_con_part2(node_p node, hook_p hook, void *arg1, int arg2); 19771849Sjulianstatic void ng_con_part3(node_p node, hook_p hook, void *arg1, int arg2); 19871047Sjulianstatic int ng_mkpeer(node_p node, const char *name, 19971047Sjulian const char *name2, char *type); 20052419Sjulian 20170784Sjulian/* imported , these used to be externally visible, some may go back */ 20270700Sjulianint ng_bypass(hook_p hook1, hook_p hook2); 20370700Sjulianvoid ng_destroy_hook(hook_p hook); 20470700Sjuliannode_p ng_name2noderef(node_p node, const char *name); 20570700Sjulianint ng_path2noderef(node_p here, const char *path, 20670700Sjulian node_p *dest, hook_p *lasthook); 20770700Sjulianstruct ng_type *ng_findtype(const char *type); 20870700Sjulianint ng_make_node(const char *type, node_p *nodepp); 20970700Sjulianint ng_path_parse(char *addr, char **node, char **path, char **hook); 21071849Sjulianvoid ng_rmnode(node_p node, hook_p dummy1, void *dummy2, int dummy3); 21170784Sjulianvoid ng_unname(node_p node); 21270700Sjulian 21370700Sjulian 21452419Sjulian/* Our own netgraph malloc type */ 21552419SjulianMALLOC_DEFINE(M_NETGRAPH, "netgraph", "netgraph structures and ctrl messages"); 21670700SjulianMALLOC_DEFINE(M_NETGRAPH_HOOK, "netgraph_hook", "netgraph hook structures"); 21770700SjulianMALLOC_DEFINE(M_NETGRAPH_NODE, "netgraph_node", "netgraph node structures"); 21870700SjulianMALLOC_DEFINE(M_NETGRAPH_ITEM, "netgraph_item", "netgraph item structures"); 21970700SjulianMALLOC_DEFINE(M_NETGRAPH_META, "netgraph_meta", "netgraph name storage"); 22070700SjulianMALLOC_DEFINE(M_NETGRAPH_MSG, "netgraph_msg", "netgraph name storage"); 22152419Sjulian 22270700Sjulian/* Should not be visible outside this file */ 22370784Sjulian 22470784Sjulian#define _NG_ALLOC_HOOK(hook) \ 22570784Sjulian MALLOC(hook, hook_p, sizeof(*hook), M_NETGRAPH_HOOK, M_NOWAIT | M_ZERO) 22670784Sjulian#define _NG_ALLOC_NODE(node) \ 22770784Sjulian MALLOC(node, node_p, sizeof(*node), M_NETGRAPH_NODE, M_NOWAIT | M_ZERO) 22870784Sjulian 22970784Sjulian#ifdef NETGRAPH_DEBUG /*----------------------------------------------*/ 23070784Sjulian/* 23170784Sjulian * In debug mode: 23270784Sjulian * In an attempt to help track reference count screwups 23370784Sjulian * we do not free objects back to the malloc system, but keep them 23470784Sjulian * in a local cache where we can examine them and keep information safely 23570784Sjulian * after they have been freed. 23670784Sjulian * We use this scheme for nodes and hooks, and to some extent for items. 23770784Sjulian */ 23870784Sjulianstatic __inline hook_p 23970784Sjulianng_alloc_hook(void) 24070784Sjulian{ 24170784Sjulian hook_p hook; 24270784Sjulian SLIST_ENTRY(ng_hook) temp; 24372200Sbmilekic mtx_lock(&ng_nodelist_mtx); 24470784Sjulian hook = LIST_FIRST(&ng_freehooks); 24570784Sjulian if (hook) { 24670784Sjulian LIST_REMOVE(hook, hk_hooks); 24770784Sjulian bcopy(&hook->hk_all, &temp, sizeof(temp)); 24870784Sjulian bzero(hook, sizeof(struct ng_hook)); 24970784Sjulian bcopy(&temp, &hook->hk_all, sizeof(temp)); 25072200Sbmilekic mtx_unlock(&ng_nodelist_mtx); 25170784Sjulian hook->hk_magic = HK_MAGIC; 25270784Sjulian } else { 25372200Sbmilekic mtx_unlock(&ng_nodelist_mtx); 25470784Sjulian _NG_ALLOC_HOOK(hook); 25570784Sjulian if (hook) { 25670784Sjulian hook->hk_magic = HK_MAGIC; 25772200Sbmilekic mtx_lock(&ng_nodelist_mtx); 25870784Sjulian SLIST_INSERT_HEAD(&ng_allhooks, hook, hk_all); 25972200Sbmilekic mtx_unlock(&ng_nodelist_mtx); 26070784Sjulian } 26170784Sjulian } 26270784Sjulian return (hook); 26370784Sjulian} 26470784Sjulian 26570784Sjulianstatic __inline node_p 26670784Sjulianng_alloc_node(void) 26770784Sjulian{ 26870784Sjulian node_p node; 26970784Sjulian SLIST_ENTRY(ng_node) temp; 27072200Sbmilekic mtx_lock(&ng_nodelist_mtx); 27170784Sjulian node = LIST_FIRST(&ng_freenodes); 27270784Sjulian if (node) { 27370784Sjulian LIST_REMOVE(node, nd_nodes); 27470784Sjulian bcopy(&node->nd_all, &temp, sizeof(temp)); 27570784Sjulian bzero(node, sizeof(struct ng_node)); 27670784Sjulian bcopy(&temp, &node->nd_all, sizeof(temp)); 27772200Sbmilekic mtx_unlock(&ng_nodelist_mtx); 27870784Sjulian node->nd_magic = ND_MAGIC; 27970784Sjulian } else { 28072200Sbmilekic mtx_unlock(&ng_nodelist_mtx); 28170784Sjulian _NG_ALLOC_NODE(node); 28270784Sjulian if (node) { 28370784Sjulian node->nd_magic = ND_MAGIC; 28472200Sbmilekic mtx_lock(&ng_nodelist_mtx); 28570784Sjulian SLIST_INSERT_HEAD(&ng_allnodes, node, nd_all); 28672200Sbmilekic mtx_unlock(&ng_nodelist_mtx); 28770784Sjulian } 28870784Sjulian } 28970784Sjulian return (node); 29070784Sjulian} 29170784Sjulian 29270784Sjulian#define NG_ALLOC_HOOK(hook) do { (hook) = ng_alloc_hook(); } while (0) 29370784Sjulian#define NG_ALLOC_NODE(node) do { (node) = ng_alloc_node(); } while (0) 29470784Sjulian 29570784Sjulian 29670784Sjulian#define NG_FREE_HOOK(hook) \ 29770784Sjulian do { \ 29872200Sbmilekic mtx_lock(&ng_nodelist_mtx); \ 29970784Sjulian LIST_INSERT_HEAD(&ng_freehooks, hook, hk_hooks); \ 30070784Sjulian hook->hk_magic = 0; \ 30172200Sbmilekic mtx_unlock(&ng_nodelist_mtx); \ 30270784Sjulian } while (0) 30370784Sjulian 30470784Sjulian#define NG_FREE_NODE(node) \ 30570784Sjulian do { \ 30672200Sbmilekic mtx_lock(&ng_nodelist_mtx); \ 30770784Sjulian LIST_INSERT_HEAD(&ng_freenodes, node, nd_nodes); \ 30870784Sjulian node->nd_magic = 0; \ 30972200Sbmilekic mtx_unlock(&ng_nodelist_mtx); \ 31070784Sjulian } while (0) 31170784Sjulian 31270784Sjulian#else /* NETGRAPH_DEBUG */ /*----------------------------------------------*/ 31370784Sjulian 31470784Sjulian#define NG_ALLOC_HOOK(hook) _NG_ALLOC_HOOK(hook) 31570784Sjulian#define NG_ALLOC_NODE(node) _NG_ALLOC_NODE(node) 31670784Sjulian 31770700Sjulian#define NG_FREE_HOOK(hook) do { FREE((hook), M_NETGRAPH_HOOK); } while (0) 31870700Sjulian#define NG_FREE_NODE(node) do { FREE((node), M_NETGRAPH_NODE); } while (0) 31970784Sjulian 32070784Sjulian#endif /* NETGRAPH_DEBUG */ /*----------------------------------------------*/ 32170784Sjulian 32270700Sjulian/* Warning: Generally use NG_FREE_ITEM() instead */ 32370700Sjulian#define NG_FREE_ITEM_REAL(item) do { FREE((item), M_NETGRAPH_ITEM); } while (0) 32470700Sjulian 32570700Sjulian 32652419Sjulian/* Set this to Debugger("X") to catch all errors as they occur */ 32752419Sjulian#ifndef TRAP_ERROR 32871047Sjulian#define TRAP_ERROR() 32952419Sjulian#endif 33052419Sjulian 33152722Sjulianstatic ng_ID_t nextID = 1; 33252722Sjulian 33353403Sarchie#ifdef INVARIANTS 33453403Sarchie#define CHECK_DATA_MBUF(m) do { \ 33553403Sarchie struct mbuf *n; \ 33653403Sarchie int total; \ 33753403Sarchie \ 33853403Sarchie if (((m)->m_flags & M_PKTHDR) == 0) \ 33953403Sarchie panic("%s: !PKTHDR", __FUNCTION__); \ 34053403Sarchie for (total = 0, n = (m); n != NULL; n = n->m_next) \ 34153403Sarchie total += n->m_len; \ 34253403Sarchie if ((m)->m_pkthdr.len != total) { \ 34353403Sarchie panic("%s: %d != %d", \ 34453403Sarchie __FUNCTION__, (m)->m_pkthdr.len, total); \ 34553403Sarchie } \ 34653403Sarchie } while (0) 34753403Sarchie#else 34853403Sarchie#define CHECK_DATA_MBUF(m) 34953403Sarchie#endif 35052722Sjulian 35153403Sarchie 35252419Sjulian/************************************************************************ 35353913Sarchie Parse type definitions for generic messages 35453913Sarchie************************************************************************/ 35553913Sarchie 35653913Sarchie/* Handy structure parse type defining macro */ 35753913Sarchie#define DEFINE_PARSE_STRUCT_TYPE(lo, up, args) \ 35853913Sarchiestatic const struct ng_parse_struct_info \ 35953913Sarchie ng_ ## lo ## _type_info = NG_GENERIC_ ## up ## _INFO args; \ 36053913Sarchiestatic const struct ng_parse_type ng_generic_ ## lo ## _type = { \ 36153913Sarchie &ng_parse_struct_type, \ 36253913Sarchie &ng_ ## lo ## _type_info \ 36353913Sarchie} 36453913Sarchie 36553913SarchieDEFINE_PARSE_STRUCT_TYPE(mkpeer, MKPEER, ()); 36653913SarchieDEFINE_PARSE_STRUCT_TYPE(connect, CONNECT, ()); 36753913SarchieDEFINE_PARSE_STRUCT_TYPE(name, NAME, ()); 36853913SarchieDEFINE_PARSE_STRUCT_TYPE(rmhook, RMHOOK, ()); 36953913SarchieDEFINE_PARSE_STRUCT_TYPE(nodeinfo, NODEINFO, ()); 37053913SarchieDEFINE_PARSE_STRUCT_TYPE(typeinfo, TYPEINFO, ()); 37153913SarchieDEFINE_PARSE_STRUCT_TYPE(linkinfo, LINKINFO, (&ng_generic_nodeinfo_type)); 37253913Sarchie 37353913Sarchie/* Get length of an array when the length is stored as a 32 bit 37472645Sasmodai value immediately preceding the array -- as with struct namelist 37553913Sarchie and struct typelist. */ 37653913Sarchiestatic int 37753913Sarchieng_generic_list_getLength(const struct ng_parse_type *type, 37853913Sarchie const u_char *start, const u_char *buf) 37953913Sarchie{ 38053913Sarchie return *((const u_int32_t *)(buf - 4)); 38153913Sarchie} 38253913Sarchie 38353913Sarchie/* Get length of the array of struct linkinfo inside a struct hooklist */ 38453913Sarchiestatic int 38553913Sarchieng_generic_linkinfo_getLength(const struct ng_parse_type *type, 38653913Sarchie const u_char *start, const u_char *buf) 38753913Sarchie{ 38853913Sarchie const struct hooklist *hl = (const struct hooklist *)start; 38953913Sarchie 39053913Sarchie return hl->nodeinfo.hooks; 39153913Sarchie} 39253913Sarchie 39353913Sarchie/* Array type for a variable length array of struct namelist */ 39453913Sarchiestatic const struct ng_parse_array_info ng_nodeinfoarray_type_info = { 39553913Sarchie &ng_generic_nodeinfo_type, 39653913Sarchie &ng_generic_list_getLength 39753913Sarchie}; 39853913Sarchiestatic const struct ng_parse_type ng_generic_nodeinfoarray_type = { 39953913Sarchie &ng_parse_array_type, 40053913Sarchie &ng_nodeinfoarray_type_info 40153913Sarchie}; 40253913Sarchie 40353913Sarchie/* Array type for a variable length array of struct typelist */ 40453913Sarchiestatic const struct ng_parse_array_info ng_typeinfoarray_type_info = { 40553913Sarchie &ng_generic_typeinfo_type, 40653913Sarchie &ng_generic_list_getLength 40753913Sarchie}; 40853913Sarchiestatic const struct ng_parse_type ng_generic_typeinfoarray_type = { 40953913Sarchie &ng_parse_array_type, 41053913Sarchie &ng_typeinfoarray_type_info 41153913Sarchie}; 41253913Sarchie 41353913Sarchie/* Array type for array of struct linkinfo in struct hooklist */ 41453913Sarchiestatic const struct ng_parse_array_info ng_generic_linkinfo_array_type_info = { 41553913Sarchie &ng_generic_linkinfo_type, 41653913Sarchie &ng_generic_linkinfo_getLength 41753913Sarchie}; 41853913Sarchiestatic const struct ng_parse_type ng_generic_linkinfo_array_type = { 41953913Sarchie &ng_parse_array_type, 42053913Sarchie &ng_generic_linkinfo_array_type_info 42153913Sarchie}; 42253913Sarchie 42353913SarchieDEFINE_PARSE_STRUCT_TYPE(typelist, TYPELIST, (&ng_generic_nodeinfoarray_type)); 42453913SarchieDEFINE_PARSE_STRUCT_TYPE(hooklist, HOOKLIST, 42553913Sarchie (&ng_generic_nodeinfo_type, &ng_generic_linkinfo_array_type)); 42653913SarchieDEFINE_PARSE_STRUCT_TYPE(listnodes, LISTNODES, 42753913Sarchie (&ng_generic_nodeinfoarray_type)); 42853913Sarchie 42953913Sarchie/* List of commands and how to convert arguments to/from ASCII */ 43053913Sarchiestatic const struct ng_cmdlist ng_generic_cmds[] = { 43153913Sarchie { 43253913Sarchie NGM_GENERIC_COOKIE, 43353913Sarchie NGM_SHUTDOWN, 43453913Sarchie "shutdown", 43553913Sarchie NULL, 43653913Sarchie NULL 43753913Sarchie }, 43853913Sarchie { 43953913Sarchie NGM_GENERIC_COOKIE, 44053913Sarchie NGM_MKPEER, 44153913Sarchie "mkpeer", 44253913Sarchie &ng_generic_mkpeer_type, 44353913Sarchie NULL 44453913Sarchie }, 44553913Sarchie { 44653913Sarchie NGM_GENERIC_COOKIE, 44753913Sarchie NGM_CONNECT, 44853913Sarchie "connect", 44953913Sarchie &ng_generic_connect_type, 45053913Sarchie NULL 45153913Sarchie }, 45253913Sarchie { 45353913Sarchie NGM_GENERIC_COOKIE, 45453913Sarchie NGM_NAME, 45553913Sarchie "name", 45653913Sarchie &ng_generic_name_type, 45753913Sarchie NULL 45853913Sarchie }, 45953913Sarchie { 46053913Sarchie NGM_GENERIC_COOKIE, 46153913Sarchie NGM_RMHOOK, 46253913Sarchie "rmhook", 46353913Sarchie &ng_generic_rmhook_type, 46453913Sarchie NULL 46553913Sarchie }, 46653913Sarchie { 46753913Sarchie NGM_GENERIC_COOKIE, 46853913Sarchie NGM_NODEINFO, 46953913Sarchie "nodeinfo", 47053913Sarchie NULL, 47153913Sarchie &ng_generic_nodeinfo_type 47253913Sarchie }, 47353913Sarchie { 47453913Sarchie NGM_GENERIC_COOKIE, 47553913Sarchie NGM_LISTHOOKS, 47653913Sarchie "listhooks", 47753913Sarchie NULL, 47853913Sarchie &ng_generic_hooklist_type 47953913Sarchie }, 48053913Sarchie { 48153913Sarchie NGM_GENERIC_COOKIE, 48253913Sarchie NGM_LISTNAMES, 48353913Sarchie "listnames", 48453913Sarchie NULL, 48553913Sarchie &ng_generic_listnodes_type /* same as NGM_LISTNODES */ 48653913Sarchie }, 48753913Sarchie { 48853913Sarchie NGM_GENERIC_COOKIE, 48953913Sarchie NGM_LISTNODES, 49053913Sarchie "listnodes", 49153913Sarchie NULL, 49253913Sarchie &ng_generic_listnodes_type 49353913Sarchie }, 49453913Sarchie { 49553913Sarchie NGM_GENERIC_COOKIE, 49653913Sarchie NGM_LISTTYPES, 49753913Sarchie "listtypes", 49853913Sarchie NULL, 49953913Sarchie &ng_generic_typeinfo_type 50053913Sarchie }, 50153913Sarchie { 50253913Sarchie NGM_GENERIC_COOKIE, 50362471Sphk NGM_TEXT_CONFIG, 50462471Sphk "textconfig", 50562471Sphk NULL, 50662471Sphk &ng_parse_string_type 50762471Sphk }, 50862471Sphk { 50962471Sphk NGM_GENERIC_COOKIE, 51053913Sarchie NGM_TEXT_STATUS, 51153913Sarchie "textstatus", 51253913Sarchie NULL, 51353913Sarchie &ng_parse_string_type 51453913Sarchie }, 51553913Sarchie { 51653913Sarchie NGM_GENERIC_COOKIE, 51753913Sarchie NGM_ASCII2BINARY, 51853913Sarchie "ascii2binary", 51953913Sarchie &ng_parse_ng_mesg_type, 52053913Sarchie &ng_parse_ng_mesg_type 52153913Sarchie }, 52253913Sarchie { 52353913Sarchie NGM_GENERIC_COOKIE, 52453913Sarchie NGM_BINARY2ASCII, 52553913Sarchie "binary2ascii", 52653913Sarchie &ng_parse_ng_mesg_type, 52753913Sarchie &ng_parse_ng_mesg_type 52853913Sarchie }, 52953913Sarchie { 0 } 53053913Sarchie}; 53153913Sarchie 53253913Sarchie/************************************************************************ 53352419Sjulian Node routines 53452419Sjulian************************************************************************/ 53552419Sjulian 53652419Sjulian/* 53752419Sjulian * Instantiate a node of the requested type 53852419Sjulian */ 53952419Sjulianint 54052419Sjulianng_make_node(const char *typename, node_p *nodepp) 54152419Sjulian{ 54252419Sjulian struct ng_type *type; 54370700Sjulian int error; 54452419Sjulian 54552419Sjulian /* Check that the type makes sense */ 54652419Sjulian if (typename == NULL) { 54771047Sjulian TRAP_ERROR(); 54852419Sjulian return (EINVAL); 54952419Sjulian } 55052419Sjulian 55152419Sjulian /* Locate the node type */ 55252419Sjulian if ((type = ng_findtype(typename)) == NULL) { 55359875Speter char filename[NG_TYPELEN + 4]; 55452419Sjulian linker_file_t lf; 55552419Sjulian int error; 55652419Sjulian 55752419Sjulian /* Not found, try to load it as a loadable module */ 55869923Sjulian snprintf(filename, sizeof(filename), "ng_%s", typename); 55959875Speter error = linker_load_file(filename, &lf); 56052419Sjulian if (error != 0) 56152419Sjulian return (error); 56252419Sjulian lf->userrefs++; /* pretend loaded by the syscall */ 56352419Sjulian 56452419Sjulian /* Try again, as now the type should have linked itself in */ 56552419Sjulian if ((type = ng_findtype(typename)) == NULL) 56652419Sjulian return (ENXIO); 56752419Sjulian } 56852419Sjulian 56970700Sjulian /* 57070700Sjulian * If we have a constructor, then make the node and 57170700Sjulian * call the constructor to do type specific initialisation. 57270700Sjulian */ 57370700Sjulian if (type->constructor != NULL) { 57470700Sjulian if ((error = ng_make_node_common(type, nodepp)) == 0) { 57570700Sjulian if ((error = ((*type->constructor)(*nodepp)) != 0)) { 57670784Sjulian NG_NODE_UNREF(*nodepp); 57770700Sjulian } 57870700Sjulian } 57970700Sjulian } else { 58070700Sjulian /* 58170700Sjulian * Node has no constructor. We cannot ask for one 58270700Sjulian * to be made. It must be brought into existance by 58370935Sjulian * some external agency. The external agency should 58470700Sjulian * call ng_make_node_common() directly to get the 58570700Sjulian * netgraph part initialised. 58670700Sjulian */ 58771047Sjulian TRAP_ERROR(); 58870700Sjulian error = EINVAL; 58970700Sjulian } 59070700Sjulian return (error); 59152419Sjulian} 59252419Sjulian 59352419Sjulian/* 59470700Sjulian * Generic node creation. Called by node initialisation for externally 59570700Sjulian * instantiated nodes (e.g. hardware, sockets, etc ). 59652419Sjulian * The returned node has a reference count of 1. 59752419Sjulian */ 59852419Sjulianint 59952419Sjulianng_make_node_common(struct ng_type *type, node_p *nodepp) 60052419Sjulian{ 60152419Sjulian node_p node; 60252419Sjulian 60352419Sjulian /* Require the node type to have been already installed */ 60452419Sjulian if (ng_findtype(type->name) == NULL) { 60571047Sjulian TRAP_ERROR(); 60652419Sjulian return (EINVAL); 60752419Sjulian } 60852419Sjulian 60952419Sjulian /* Make a node and try attach it to the type */ 61070784Sjulian NG_ALLOC_NODE(node); 61152419Sjulian if (node == NULL) { 61271047Sjulian TRAP_ERROR(); 61352419Sjulian return (ENOMEM); 61452419Sjulian } 61570784Sjulian node->nd_type = type; 61670784Sjulian NG_NODE_REF(node); /* note reference */ 61752419Sjulian type->refs++; 61852419Sjulian 61971380Sjulian mtx_init(&node->nd_input_queue.q_mtx, "netgraph node mutex", MTX_SPIN); 62070784Sjulian node->nd_input_queue.queue = NULL; 62170784Sjulian node->nd_input_queue.last = &node->nd_input_queue.queue; 62270784Sjulian node->nd_input_queue.q_flags = 0; 62370784Sjulian node->nd_input_queue.q_node = node; 62452419Sjulian 62552419Sjulian /* Initialize hook list for new node */ 62670784Sjulian LIST_INIT(&node->nd_hooks); 62752419Sjulian 62870700Sjulian /* Link us into the node linked list */ 62972200Sbmilekic mtx_lock(&ng_nodelist_mtx); 63070784Sjulian LIST_INSERT_HEAD(&ng_nodelist, node, nd_nodes); 63172200Sbmilekic mtx_unlock(&ng_nodelist_mtx); 63270700Sjulian 63370700Sjulian 63452722Sjulian /* get an ID and put us in the hash chain */ 63572200Sbmilekic mtx_lock(&ng_idhash_mtx); 63670784Sjulian for (;;) { /* wrap protection, even if silly */ 63770700Sjulian node_p node2 = NULL; 63870784Sjulian node->nd_ID = nextID++; /* 137/second for 1 year before wrap */ 63971354Sjulian 64070784Sjulian /* Is there a problem with the new number? */ 64171354Sjulian NG_IDHASH_FIND(node->nd_ID, node2); /* already taken? */ 64271354Sjulian if ((node->nd_ID != 0) && (node2 == NULL)) { 64370784Sjulian break; 64470700Sjulian } 64570784Sjulian } 64671354Sjulian LIST_INSERT_HEAD(&ng_ID_hash[NG_IDHASH_FN(node->nd_ID)], 64770784Sjulian node, nd_idnodes); 64872200Sbmilekic mtx_unlock(&ng_idhash_mtx); 64952722Sjulian 65052419Sjulian /* Done */ 65152419Sjulian *nodepp = node; 65252419Sjulian return (0); 65352419Sjulian} 65452419Sjulian 65552419Sjulian/* 65652419Sjulian * Forceably start the shutdown process on a node. Either call 65752419Sjulian * it's shutdown method, or do the default shutdown if there is 65852419Sjulian * no type-specific method. 65952419Sjulian * 66070700Sjulian * We can only be called form a shutdown message, so we know we have 66170939Sjulian * a writer lock, and therefore exclusive access. It also means 66270939Sjulian * that we should not be on the work queue, but we check anyhow. 66370700Sjulian * 66470700Sjulian * Persistent node types must have a type-specific method which 66570939Sjulian * Allocates a new node in which case, this one is irretrievably going away, 66670939Sjulian * or cleans up anything it needs, and just makes the node valid again, 66770939Sjulian * in which case we allow the node to survive. 66870939Sjulian * 66970939Sjulian * XXX We need to think of how to tell a persistant node that we 67070939Sjulian * REALLY need to go away because the hardware has gone or we 67170939Sjulian * are rebooting.... etc. 67252419Sjulian */ 67352419Sjulianvoid 67471849Sjulianng_rmnode(node_p node, hook_p dummy1, void *dummy2, int dummy3) 67552419Sjulian{ 67670939Sjulian hook_p hook; 67770939Sjulian 67852419Sjulian /* Check if it's already shutting down */ 67970784Sjulian if ((node->nd_flags & NG_CLOSING) != 0) 68052419Sjulian return; 68152419Sjulian 68271849Sjulian if (node == &ng_deadnode) { 68371849Sjulian printf ("shutdown called on deadnode\n"); 68471849Sjulian return; 68571849Sjulian } 68671849Sjulian 68752419Sjulian /* Add an extra reference so it doesn't go away during this */ 68870784Sjulian NG_NODE_REF(node); 68952419Sjulian 69070784Sjulian /* 69170784Sjulian * Mark it invalid so any newcomers know not to try use it 69270784Sjulian * Also add our own mark so we can't recurse 69370784Sjulian * note that NG_INVALID does not do this as it's also set during 69470784Sjulian * creation 69570784Sjulian */ 69670784Sjulian node->nd_flags |= NG_INVALID|NG_CLOSING; 69752419Sjulian 69870939Sjulian /* Notify all remaining connected nodes to disconnect */ 69970939Sjulian while ((hook = LIST_FIRST(&node->nd_hooks)) != NULL) 70070939Sjulian ng_destroy_hook(hook); 70170784Sjulian 70270700Sjulian /* 70370700Sjulian * Drain the input queue forceably. 70470784Sjulian * it has no hooks so what's it going to do, bleed on someone? 70570784Sjulian * Theoretically we came here from a queue entry that was added 70670784Sjulian * Just before the queue was closed, so it should be empty anyway. 70771902Sjulian * Also removes us from worklist if needed. 70870700Sjulian */ 70970784Sjulian ng_flush_input_queue(&node->nd_input_queue); 71070700Sjulian 71152419Sjulian /* Ask the type if it has anything to do in this case */ 71270784Sjulian if (node->nd_type && node->nd_type->shutdown) { 71370784Sjulian (*node->nd_type->shutdown)(node); 71471849Sjulian if (NG_NODE_IS_VALID(node)) { 71571849Sjulian /* 71671849Sjulian * Well, blow me down if the node code hasn't declared 71771849Sjulian * that it doesn't want to die. 71871849Sjulian * Presumably it is a persistant node. 71971849Sjulian * If we REALLY want it to go away, 72071849Sjulian * e.g. hardware going away, 72171849Sjulian * Our caller should set NG_REALLY_DIE in nd_flags. 72271849Sjulian */ 72371849Sjulian node->nd_flags &= ~(NG_INVALID|NG_CLOSING); 72471849Sjulian NG_NODE_UNREF(node); /* Assume they still have theirs */ 72571849Sjulian return; 72671849Sjulian } 72770700Sjulian } else { /* do the default thing */ 72870784Sjulian NG_NODE_UNREF(node); 72952419Sjulian } 73052419Sjulian 73170784Sjulian ng_unname(node); /* basically a NOP these days */ 73270784Sjulian 73370784Sjulian /* 73470784Sjulian * Remove extra reference, possibly the last 73570784Sjulian * Possible other holders of references may include 73670784Sjulian * timeout callouts, but theoretically the node's supposed to 73770784Sjulian * have cancelled them. Possibly hardware dependencies may 73870784Sjulian * force a driver to 'linger' with a reference. 73970784Sjulian */ 74070784Sjulian NG_NODE_UNREF(node); 74152419Sjulian} 74252419Sjulian 74352419Sjulian/* 74452419Sjulian * Remove a reference to the node, possibly the last 74552419Sjulian */ 74652419Sjulianvoid 74770784Sjulianng_unref_node(node_p node) 74852419Sjulian{ 74970784Sjulian int v; 75071047Sjulian 75171047Sjulian if (node == &ng_deadnode) { 75271047Sjulian return; 75371047Sjulian } 75471047Sjulian 75570784Sjulian do { 75670784Sjulian v = node->nd_refs; 75770784Sjulian } while (! atomic_cmpset_int(&node->nd_refs, v, v - 1)); 75869519Sjulian 75970784Sjulian if (v == 1) { /* we were the last */ 76070700Sjulian 76172200Sbmilekic mtx_lock(&ng_nodelist_mtx); 76270784Sjulian node->nd_type->refs--; /* XXX maybe should get types lock? */ 76370784Sjulian LIST_REMOVE(node, nd_nodes); 76472200Sbmilekic mtx_unlock(&ng_nodelist_mtx); 76570700Sjulian 76672200Sbmilekic mtx_lock(&ng_idhash_mtx); 76770784Sjulian LIST_REMOVE(node, nd_idnodes); 76872200Sbmilekic mtx_unlock(&ng_idhash_mtx); 76952419Sjulian 77070791Sjulian mtx_destroy(&node->nd_input_queue.q_mtx); 77170700Sjulian NG_FREE_NODE(node); 77252419Sjulian } 77352419Sjulian} 77452419Sjulian 77552419Sjulian/************************************************************************ 77652722Sjulian Node ID handling 77752722Sjulian************************************************************************/ 77852722Sjulianstatic node_p 77970700Sjulianng_ID2noderef(ng_ID_t ID) 78052722Sjulian{ 78170784Sjulian node_p node; 78272200Sbmilekic mtx_lock(&ng_idhash_mtx); 78371354Sjulian NG_IDHASH_FIND(ID, node); 78470784Sjulian if(node) 78570784Sjulian NG_NODE_REF(node); 78672200Sbmilekic mtx_unlock(&ng_idhash_mtx); 78770784Sjulian return(node); 78852722Sjulian} 78952722Sjulian 79052722Sjulianng_ID_t 79152722Sjulianng_node2ID(node_p node) 79252722Sjulian{ 79370912Sjulian return (node ? NG_NODE_ID(node) : 0); 79452722Sjulian} 79552722Sjulian 79652722Sjulian/************************************************************************ 79752419Sjulian Node name handling 79852419Sjulian************************************************************************/ 79952419Sjulian 80052419Sjulian/* 80152419Sjulian * Assign a node a name. Once assigned, the name cannot be changed. 80252419Sjulian */ 80352419Sjulianint 80452419Sjulianng_name_node(node_p node, const char *name) 80552419Sjulian{ 80652419Sjulian int i; 80770700Sjulian node_p node2; 80852419Sjulian 80952419Sjulian /* Check the name is valid */ 81052419Sjulian for (i = 0; i < NG_NODELEN + 1; i++) { 81152419Sjulian if (name[i] == '\0' || name[i] == '.' || name[i] == ':') 81252419Sjulian break; 81352419Sjulian } 81452419Sjulian if (i == 0 || name[i] != '\0') { 81571047Sjulian TRAP_ERROR(); 81652419Sjulian return (EINVAL); 81752419Sjulian } 81852722Sjulian if (ng_decodeidname(name) != 0) { /* valid IDs not allowed here */ 81971047Sjulian TRAP_ERROR(); 82052419Sjulian return (EINVAL); 82152419Sjulian } 82252419Sjulian 82352419Sjulian /* Check the name isn't already being used */ 82470700Sjulian if ((node2 = ng_name2noderef(node, name)) != NULL) { 82570784Sjulian NG_NODE_UNREF(node2); 82671047Sjulian TRAP_ERROR(); 82752419Sjulian return (EADDRINUSE); 82852419Sjulian } 82952419Sjulian 83070700Sjulian /* copy it */ 83170784Sjulian strncpy(NG_NODE_NAME(node), name, NG_NODELEN); 83252419Sjulian 83352419Sjulian return (0); 83452419Sjulian} 83552419Sjulian 83652419Sjulian/* 83752419Sjulian * Find a node by absolute name. The name should NOT end with ':' 83852419Sjulian * The name "." means "this node" and "[xxx]" means "the node 83952419Sjulian * with ID (ie, at address) xxx". 84052419Sjulian * 84152419Sjulian * Returns the node if found, else NULL. 84270700Sjulian * Eventually should add something faster than a sequential search. 84370784Sjulian * Note it aquires a reference on the node so you can be sure it's still there. 84452419Sjulian */ 84552419Sjuliannode_p 84670700Sjulianng_name2noderef(node_p here, const char *name) 84752419Sjulian{ 84852722Sjulian node_p node; 84952722Sjulian ng_ID_t temp; 85052419Sjulian 85152419Sjulian /* "." means "this node" */ 85270700Sjulian if (strcmp(name, ".") == 0) { 85370784Sjulian NG_NODE_REF(here); 85470700Sjulian return(here); 85570700Sjulian } 85652419Sjulian 85752419Sjulian /* Check for name-by-ID */ 85852722Sjulian if ((temp = ng_decodeidname(name)) != 0) { 85970700Sjulian return (ng_ID2noderef(temp)); 86052419Sjulian } 86152419Sjulian 86252419Sjulian /* Find node by name */ 86372200Sbmilekic mtx_lock(&ng_nodelist_mtx); 86470784Sjulian LIST_FOREACH(node, &ng_nodelist, nd_nodes) { 86570912Sjulian if (NG_NODE_IS_VALID(node) 86670912Sjulian && NG_NODE_HAS_NAME(node) 86770912Sjulian && (strcmp(NG_NODE_NAME(node), name) == 0)) { 86852419Sjulian break; 86970912Sjulian } 87052419Sjulian } 87170700Sjulian if (node) 87270784Sjulian NG_NODE_REF(node); 87372200Sbmilekic mtx_unlock(&ng_nodelist_mtx); 87452419Sjulian return (node); 87552419Sjulian} 87652419Sjulian 87752419Sjulian/* 87852722Sjulian * Decode a ID name, eg. "[f03034de]". Returns 0 if the 87952722Sjulian * string is not valid, otherwise returns the value. 88052419Sjulian */ 88152722Sjulianstatic ng_ID_t 88252419Sjulianng_decodeidname(const char *name) 88352419Sjulian{ 88452816Sarchie const int len = strlen(name); 88553648Sarchie char *eptr; 88652816Sarchie u_long val; 88752419Sjulian 88852816Sarchie /* Check for proper length, brackets, no leading junk */ 88970912Sjulian if ((len < 3) 89070912Sjulian || (name[0] != '[') 89170912Sjulian || (name[len - 1] != ']') 89270912Sjulian || (!isxdigit(name[1]))) { 89370912Sjulian return ((ng_ID_t)0); 89470912Sjulian } 89552419Sjulian 89652816Sarchie /* Decode number */ 89752816Sarchie val = strtoul(name + 1, &eptr, 16); 89870912Sjulian if ((eptr - name != len - 1) 89970912Sjulian || (val == ULONG_MAX) 90070912Sjulian || (val == 0)) { 90153042Sjulian return ((ng_ID_t)0); 90270912Sjulian } 90352816Sarchie return (ng_ID_t)val; 90452419Sjulian} 90552419Sjulian 90652419Sjulian/* 90752419Sjulian * Remove a name from a node. This should only be called 90852419Sjulian * when shutting down and removing the node. 90971849Sjulian * IF we allow name changing this may be more resurected. 91052419Sjulian */ 91152419Sjulianvoid 91252419Sjulianng_unname(node_p node) 91352419Sjulian{ 91452419Sjulian} 91552419Sjulian 91652419Sjulian/************************************************************************ 91752419Sjulian Hook routines 91852419Sjulian Names are not optional. Hooks are always connected, except for a 91970939Sjulian brief moment within these routines. On invalidation or during creation 92070939Sjulian they are connected to the 'dead' hook. 92152419Sjulian************************************************************************/ 92252419Sjulian 92352419Sjulian/* 92452419Sjulian * Remove a hook reference 92552419Sjulian */ 92670784Sjulianvoid 92752419Sjulianng_unref_hook(hook_p hook) 92852419Sjulian{ 92970784Sjulian int v; 93071047Sjulian 93171047Sjulian if (hook == &ng_deadhook) { 93271047Sjulian return; 93371047Sjulian } 93470784Sjulian do { 93570784Sjulian v = hook->hk_refs; 93670784Sjulian } while (! atomic_cmpset_int(&hook->hk_refs, v, v - 1)); 93769519Sjulian 93870784Sjulian if (v == 1) { /* we were the last */ 93971047Sjulian if (_NG_HOOK_NODE(hook)) { /* it'll probably be ng_deadnode */ 94071047Sjulian _NG_NODE_UNREF((_NG_HOOK_NODE(hook))); 94170784Sjulian hook->hk_node = NULL; 94270700Sjulian } 94370700Sjulian NG_FREE_HOOK(hook); 94470700Sjulian } 94552419Sjulian} 94652419Sjulian 94752419Sjulian/* 94852419Sjulian * Add an unconnected hook to a node. Only used internally. 94970939Sjulian * Assumes node is locked. (XXX not yet true ) 95052419Sjulian */ 95152419Sjulianstatic int 95252419Sjulianng_add_hook(node_p node, const char *name, hook_p *hookp) 95352419Sjulian{ 95452419Sjulian hook_p hook; 95552419Sjulian int error = 0; 95652419Sjulian 95752419Sjulian /* Check that the given name is good */ 95852419Sjulian if (name == NULL) { 95971047Sjulian TRAP_ERROR(); 96052419Sjulian return (EINVAL); 96152419Sjulian } 96254096Sarchie if (ng_findhook(node, name) != NULL) { 96371047Sjulian TRAP_ERROR(); 96454096Sarchie return (EEXIST); 96552419Sjulian } 96652419Sjulian 96752419Sjulian /* Allocate the hook and link it up */ 96870784Sjulian NG_ALLOC_HOOK(hook); 96952419Sjulian if (hook == NULL) { 97071047Sjulian TRAP_ERROR(); 97152419Sjulian return (ENOMEM); 97252419Sjulian } 97370939Sjulian hook->hk_refs = 1; /* add a reference for us to return */ 97470784Sjulian hook->hk_flags = HK_INVALID; 97570939Sjulian hook->hk_peer = &ng_deadhook; /* start off this way */ 97670784Sjulian hook->hk_node = node; 97770784Sjulian NG_NODE_REF(node); /* each hook counts as a reference */ 97852419Sjulian 97970939Sjulian /* Set hook name */ 98070939Sjulian strncpy(NG_HOOK_NAME(hook), name, NG_HOOKLEN); 98170939Sjulian 98270939Sjulian /* 98370939Sjulian * Check if the node type code has something to say about it 98470939Sjulian * If it fails, the unref of the hook will also unref the node. 98570939Sjulian */ 98670935Sjulian if (node->nd_type->newhook != NULL) { 98770935Sjulian if ((error = (*node->nd_type->newhook)(node, hook, name))) { 98870784Sjulian NG_HOOK_UNREF(hook); /* this frees the hook */ 98970700Sjulian return (error); 99070700Sjulian } 99170935Sjulian } 99252419Sjulian /* 99352419Sjulian * The 'type' agrees so far, so go ahead and link it in. 99452419Sjulian * We'll ask again later when we actually connect the hooks. 99552419Sjulian */ 99670784Sjulian LIST_INSERT_HEAD(&node->nd_hooks, hook, hk_hooks); 99770784Sjulian node->nd_numhooks++; 99870939Sjulian NG_HOOK_REF(hook); /* one for the node */ 99952419Sjulian 100052419Sjulian if (hookp) 100152419Sjulian *hookp = hook; 100270939Sjulian return (0); 100352419Sjulian} 100452419Sjulian 100552419Sjulian/* 100654096Sarchie * Find a hook 100754096Sarchie * 100854096Sarchie * Node types may supply their own optimized routines for finding 100954096Sarchie * hooks. If none is supplied, we just do a linear search. 101070939Sjulian * XXX Possibly we should add a reference to the hook? 101154096Sarchie */ 101254096Sarchiehook_p 101354096Sarchieng_findhook(node_p node, const char *name) 101454096Sarchie{ 101554096Sarchie hook_p hook; 101654096Sarchie 101770784Sjulian if (node->nd_type->findhook != NULL) 101870784Sjulian return (*node->nd_type->findhook)(node, name); 101970784Sjulian LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) { 102070912Sjulian if (NG_HOOK_IS_VALID(hook) 102170917Sarchie && (strcmp(NG_HOOK_NAME(hook), name) == 0)) 102254096Sarchie return (hook); 102354096Sarchie } 102454096Sarchie return (NULL); 102554096Sarchie} 102654096Sarchie 102754096Sarchie/* 102852419Sjulian * Destroy a hook 102952419Sjulian * 103052419Sjulian * As hooks are always attached, this really destroys two hooks. 103152419Sjulian * The one given, and the one attached to it. Disconnect the hooks 103270939Sjulian * from each other first. We reconnect the peer hook to the 'dead' 103370939Sjulian * hook so that it can still exist after we depart. We then 103470939Sjulian * send the peer its own destroy message. This ensures that we only 103570939Sjulian * interact with the peer's structures when it is locked processing that 103670939Sjulian * message. We hold a reference to the peer hook so we are guaranteed that 103770939Sjulian * the peer hook and node are still going to exist until 103870939Sjulian * we are finished there as the hook holds a ref on the node. 103970939Sjulian * We run this same code again on the peer hook, but that time it is already 104070939Sjulian * attached to the 'dead' hook. 104171047Sjulian * 104271047Sjulian * This routine is called at all stages of hook creation 104371047Sjulian * on error detection and must be able to handle any such stage. 104452419Sjulian */ 104552419Sjulianvoid 104652419Sjulianng_destroy_hook(hook_p hook) 104752419Sjulian{ 104870784Sjulian hook_p peer = NG_HOOK_PEER(hook); 104971047Sjulian node_p node = NG_HOOK_NODE(hook); 105052419Sjulian 105171047Sjulian if (hook == &ng_deadhook) { /* better safe than sorry */ 105271047Sjulian printf("ng_destroy_hook called on deadhook\n"); 105371047Sjulian return; 105471047Sjulian } 105570784Sjulian hook->hk_flags |= HK_INVALID; /* as soon as possible */ 105670939Sjulian if (peer && (peer != &ng_deadhook)) { 105770939Sjulian /* 105870939Sjulian * Set the peer to point to ng_deadhook 105970939Sjulian * from this moment on we are effectively independent it. 106070939Sjulian * send it an rmhook message of it's own. 106170939Sjulian */ 106270939Sjulian peer->hk_peer = &ng_deadhook; /* They no longer know us */ 106370939Sjulian hook->hk_peer = &ng_deadhook; /* Nor us, them */ 106471047Sjulian if (NG_HOOK_NODE(peer) == &ng_deadnode) { 106571047Sjulian /* 106671047Sjulian * If it's already divorced from a node, 106771047Sjulian * just free it. 106871047Sjulian */ 106971047Sjulian /* nothing */ 107071047Sjulian } else { 107171047Sjulian ng_rmhook_self(peer); /* Send it a surprise */ 107271047Sjulian } 107370942Sjulian NG_HOOK_UNREF(peer); /* account for peer link */ 107470942Sjulian NG_HOOK_UNREF(hook); /* account for peer link */ 107552419Sjulian } 107652419Sjulian 107752419Sjulian /* 107852419Sjulian * Remove the hook from the node's list to avoid possible recursion 107952419Sjulian * in case the disconnection results in node shutdown. 108052419Sjulian */ 108171047Sjulian if (node == &ng_deadnode) { /* happens if called from ng_con_nodes() */ 108271047Sjulian return; 108371047Sjulian } 108470784Sjulian LIST_REMOVE(hook, hk_hooks); 108570784Sjulian node->nd_numhooks--; 108670784Sjulian if (node->nd_type->disconnect) { 108752419Sjulian /* 108871047Sjulian * The type handler may elect to destroy the node so don't 108971047Sjulian * trust its existance after this point. (except 109071047Sjulian * that we still hold a reference on it. (which we 109171047Sjulian * inherrited from the hook we are destroying) 109252419Sjulian */ 109370784Sjulian (*node->nd_type->disconnect) (hook); 109452419Sjulian } 109571047Sjulian 109671047Sjulian /* 109771047Sjulian * Note that because we will point to ng_deadnode, the original node 109871047Sjulian * is not decremented automatically so we do that manually. 109971047Sjulian */ 110071047Sjulian _NG_HOOK_NODE(hook) = &ng_deadnode; 110171047Sjulian NG_NODE_UNREF(node); /* We no longer point to it so adjust count */ 110271047Sjulian NG_HOOK_UNREF(hook); /* Account for linkage (in list) to node */ 110352419Sjulian} 110452419Sjulian 110552419Sjulian/* 110652419Sjulian * Take two hooks on a node and merge the connection so that the given node 110752419Sjulian * is effectively bypassed. 110852419Sjulian */ 110952419Sjulianint 111052419Sjulianng_bypass(hook_p hook1, hook_p hook2) 111152419Sjulian{ 111270784Sjulian if (hook1->hk_node != hook2->hk_node) { 111371047Sjulian TRAP_ERROR(); 111452419Sjulian return (EINVAL); 111570784Sjulian } 111670784Sjulian hook1->hk_peer->hk_peer = hook2->hk_peer; 111770784Sjulian hook2->hk_peer->hk_peer = hook1->hk_peer; 111852419Sjulian 111970939Sjulian hook1->hk_peer = &ng_deadhook; 112070939Sjulian hook2->hk_peer = &ng_deadhook; 112170939Sjulian 112252419Sjulian /* XXX If we ever cache methods on hooks update them as well */ 112352419Sjulian ng_destroy_hook(hook1); 112452419Sjulian ng_destroy_hook(hook2); 112552419Sjulian return (0); 112652419Sjulian} 112752419Sjulian 112852419Sjulian/* 112952419Sjulian * Install a new netgraph type 113052419Sjulian */ 113152419Sjulianint 113252419Sjulianng_newtype(struct ng_type *tp) 113352419Sjulian{ 113452419Sjulian const size_t namelen = strlen(tp->name); 113552419Sjulian 113652419Sjulian /* Check version and type name fields */ 113770159Sjulian if ((tp->version != NG_ABI_VERSION) 113870159Sjulian || (namelen == 0) 113970159Sjulian || (namelen > NG_TYPELEN)) { 114071047Sjulian TRAP_ERROR(); 114152419Sjulian return (EINVAL); 114252419Sjulian } 114352419Sjulian 114452419Sjulian /* Check for name collision */ 114552419Sjulian if (ng_findtype(tp->name) != NULL) { 114671047Sjulian TRAP_ERROR(); 114752419Sjulian return (EEXIST); 114852419Sjulian } 114952419Sjulian 115070700Sjulian 115152419Sjulian /* Link in new type */ 115272200Sbmilekic mtx_lock(&ng_typelist_mtx); 115370700Sjulian LIST_INSERT_HEAD(&ng_typelist, tp, types); 115471603Sjulian tp->refs = 1; /* first ref is linked list */ 115572200Sbmilekic mtx_unlock(&ng_typelist_mtx); 115652419Sjulian return (0); 115752419Sjulian} 115852419Sjulian 115952419Sjulian/* 116052419Sjulian * Look for a type of the name given 116152419Sjulian */ 116252419Sjulianstruct ng_type * 116352419Sjulianng_findtype(const char *typename) 116452419Sjulian{ 116552419Sjulian struct ng_type *type; 116652419Sjulian 116772200Sbmilekic mtx_lock(&ng_typelist_mtx); 116870700Sjulian LIST_FOREACH(type, &ng_typelist, types) { 116952419Sjulian if (strcmp(type->name, typename) == 0) 117052419Sjulian break; 117152419Sjulian } 117272200Sbmilekic mtx_unlock(&ng_typelist_mtx); 117352419Sjulian return (type); 117452419Sjulian} 117552419Sjulian 117652419Sjulian/************************************************************************ 117752419Sjulian Composite routines 117852419Sjulian************************************************************************/ 117952419Sjulian/* 118071047Sjulian * Connect two nodes using the specified hooks, using queued functions. 118152419Sjulian */ 118271849Sjulianstatic void 118371047Sjulianng_con_part3(node_p node, hook_p hook, void *arg1, int arg2) 118452419Sjulian{ 118552419Sjulian 118671047Sjulian /* 118771047Sjulian * When we run, we know that the node 'node' is locked for us. 118871047Sjulian * Our caller has a reference on the hook. 118971047Sjulian * Our caller has a reference on the node. 119071047Sjulian * (In this case our caller is ng_apply_item() ). 119171047Sjulian * The peer hook has a reference on the hook. 119271849Sjulian * We are all set up except for the final call to the node, and 119371849Sjulian * the clearing of the INVALID flag. 119471047Sjulian */ 119571047Sjulian if (NG_HOOK_NODE(hook) == &ng_deadnode) { 119671047Sjulian /* 119771047Sjulian * The node must have been freed again since we last visited 119871047Sjulian * here. ng_destry_hook() has this effect but nothing else does. 119971047Sjulian * We should just release our references and 120071047Sjulian * free anything we can think of. 120171047Sjulian * Since we know it's been destroyed, and it's our caller 120271047Sjulian * that holds the references, just return. 120371047Sjulian */ 120471849Sjulian return ; 120552419Sjulian } 120671047Sjulian if (hook->hk_node->nd_type->connect) { 120771849Sjulian if ((*hook->hk_node->nd_type->connect) (hook)) { 120871047Sjulian ng_destroy_hook(hook); /* also zaps peer */ 120971849Sjulian printf("failed in ng_con_part3()\n"); 121071849Sjulian return ; 121171047Sjulian } 121252419Sjulian } 121371047Sjulian /* 121471047Sjulian * XXX this is wrong for SMP. Possibly we need 121571047Sjulian * to separate out 'create' and 'invalid' flags. 121671047Sjulian * should only set flags on hooks we have locked under our node. 121771047Sjulian */ 121871047Sjulian hook->hk_flags &= ~HK_INVALID; 121971849Sjulian return ; 122071047Sjulian} 122152419Sjulian 122271849Sjulianstatic void 122371047Sjulianng_con_part2(node_p node, hook_p hook, void *arg1, int arg2) 122471047Sjulian{ 122571047Sjulian 122652419Sjulian /* 122771047Sjulian * When we run, we know that the node 'node' is locked for us. 122871047Sjulian * Our caller has a reference on the hook. 122971047Sjulian * Our caller has a reference on the node. 123071047Sjulian * (In this case our caller is ng_apply_item() ). 123171047Sjulian * The peer hook has a reference on the hook. 123271047Sjulian * our node pointer points to the 'dead' node. 123371047Sjulian * First check the hook name is unique. 123471849Sjulian * Should not happen because we checked before queueing this. 123552419Sjulian */ 123671047Sjulian if (ng_findhook(node, NG_HOOK_NAME(hook)) != NULL) { 123771047Sjulian TRAP_ERROR(); 123871047Sjulian ng_destroy_hook(hook); /* should destroy peer too */ 123971849Sjulian printf("failed in ng_con_part2()\n"); 124071849Sjulian return ; 124171047Sjulian } 124270939Sjulian /* 124371047Sjulian * Check if the node type code has something to say about it 124471047Sjulian * If it fails, the unref of the hook will also unref the attached node, 124571047Sjulian * however since that node is 'ng_deadnode' this will do nothing. 124671047Sjulian * The peer hook will also be destroyed. 124770939Sjulian */ 124871047Sjulian if (node->nd_type->newhook != NULL) { 124971849Sjulian if ((*node->nd_type->newhook)(node, hook, hook->hk_name)) { 125071047Sjulian ng_destroy_hook(hook); /* should destroy peer too */ 125171849Sjulian printf("failed in ng_con_part2()\n"); 125271849Sjulian return ; 125371047Sjulian } 125471047Sjulian } 125571047Sjulian 125671047Sjulian /* 125771047Sjulian * The 'type' agrees so far, so go ahead and link it in. 125871047Sjulian * We'll ask again later when we actually connect the hooks. 125971047Sjulian */ 126071047Sjulian hook->hk_node = node; /* just overwrite ng_deadnode */ 126171047Sjulian NG_NODE_REF(node); /* each hook counts as a reference */ 126271047Sjulian LIST_INSERT_HEAD(&node->nd_hooks, hook, hk_hooks); 126371047Sjulian node->nd_numhooks++; 126471047Sjulian NG_HOOK_REF(hook); /* one for the node */ 126571047Sjulian 126671047Sjulian /* 126771047Sjulian * We now have a symetrical situation, where both hooks have been 126871047Sjulian * linked to theur nodes, the newhook methods have been called 126971047Sjulian * And the references are all correct. The hooks are still marked 127071047Sjulian * as invalid, as we have not called the 'connect' methods 127171047Sjulian * yet. 127271047Sjulian * We can call the local one immediatly as we have the 127371047Sjulian * node locked, but we need to queue the remote one. 127471047Sjulian */ 127571047Sjulian if (hook->hk_node->nd_type->connect) { 127671849Sjulian if ((*hook->hk_node->nd_type->connect) (hook)) { 127771047Sjulian ng_destroy_hook(hook); /* also zaps peer */ 127871849Sjulian printf("failed in ng_con_part2(A)\n"); 127971849Sjulian return ; 128071047Sjulian } 128171047Sjulian } 128271849Sjulian if (ng_send_fn(hook->hk_peer->hk_node, hook->hk_peer, 128371849Sjulian &ng_con_part3, arg1, arg2)) { 128471849Sjulian printf("failed in ng_con_part2(B)"); 128571849Sjulian ng_destroy_hook(hook); /* also zaps peer */ 128671849Sjulian return ; 128771849Sjulian } 128871047Sjulian hook->hk_flags &= ~HK_INVALID; /* need both to be able to work */ 128971849Sjulian return ; 129052419Sjulian} 129152419Sjulian 129252419Sjulian/* 129371047Sjulian * Connect this node with another node. We assume that this node is 129471047Sjulian * currently locked, as we are only called from an NGM_CONNECT message. 129552419Sjulian */ 129671047Sjulianstatic int 129752419Sjulianng_con_nodes(node_p node, const char *name, node_p node2, const char *name2) 129852419Sjulian{ 129952419Sjulian int error; 130052419Sjulian hook_p hook; 130152419Sjulian hook_p hook2; 130252419Sjulian 130371849Sjulian if (ng_findhook(node2, name2) != NULL) { 130471849Sjulian return(EEXIST); 130571849Sjulian } 130670939Sjulian if ((error = ng_add_hook(node, name, &hook))) /* gives us a ref */ 130752419Sjulian return (error); 130871047Sjulian /* Allocate the other hook and link it up */ 130971047Sjulian NG_ALLOC_HOOK(hook2); 131071047Sjulian if (hook == NULL) { 131171047Sjulian TRAP_ERROR(); 131271047Sjulian ng_destroy_hook(hook); /* XXX check ref counts so far */ 131371047Sjulian NG_HOOK_UNREF(hook); /* including our ref */ 131471047Sjulian return (ENOMEM); 131571047Sjulian } 131671047Sjulian hook2->hk_refs = 1; /* start with a reference for us. */ 131771047Sjulian hook2->hk_flags = HK_INVALID; 131871047Sjulian hook2->hk_peer = hook; /* Link the two together */ 131971047Sjulian hook->hk_peer = hook2; 132071047Sjulian NG_HOOK_REF(hook); /* Add a ref for the peer to each*/ 132171047Sjulian NG_HOOK_REF(hook2); 132271047Sjulian hook2->hk_node = &ng_deadnode; 132371047Sjulian strncpy(NG_HOOK_NAME(hook2), name2, NG_HOOKLEN); 132471047Sjulian 132571047Sjulian /* 132671047Sjulian * Queue the function above. 132771047Sjulian * Procesing continues in that function in the lock context of 132871047Sjulian * the other node. 132971047Sjulian */ 133071849Sjulian ng_send_fn(node2, hook2, &ng_con_part2, NULL, 0); 133171047Sjulian 133271047Sjulian NG_HOOK_UNREF(hook); /* Let each hook go if it wants to */ 133371047Sjulian NG_HOOK_UNREF(hook2); 133471849Sjulian return (0); 133571047Sjulian} 133671047Sjulian 133771047Sjulian/* 133871047Sjulian * Make a peer and connect. 133971047Sjulian * We assume that the local node is locked. 134071047Sjulian * The new node probably doesn't need a lock until 134171047Sjulian * it has a hook, because it cannot really have any work until then, 134271047Sjulian * but we should think about it a bit more. 134371047Sjulian * 134471047Sjulian * The problem may come if the other node also fires up 134571047Sjulian * some hardware or a timer or some other source of activation, 134671047Sjulian * also it may already get a command msg via it's ID. 134771047Sjulian * 134871047Sjulian * We could use the same method as ng_con_nodes() but we'd have 134971047Sjulian * to add ability to remove the node when failing. (Not hard, just 135071047Sjulian * make arg1 point to the node to remove). 135171047Sjulian * Unless of course we just ignore failure to connect and leave 135271047Sjulian * an unconnected node? 135371047Sjulian */ 135471047Sjulianstatic int 135571047Sjulianng_mkpeer(node_p node, const char *name, const char *name2, char *type) 135671047Sjulian{ 135771047Sjulian node_p node2; 135871047Sjulian hook_p hook1; 135971047Sjulian hook_p hook2; 136071047Sjulian int error; 136171047Sjulian 136271047Sjulian if ((error = ng_make_node(type, &node2))) { 136352419Sjulian return (error); 136452419Sjulian } 136570939Sjulian 136671047Sjulian if ((error = ng_add_hook(node, name, &hook1))) { /* gives us a ref */ 136771849Sjulian ng_rmnode(node2, NULL, NULL, 0); 136871047Sjulian return (error); 136971047Sjulian } 137071047Sjulian 137171047Sjulian if ((error = ng_add_hook(node2, name2, &hook2))) { 137271849Sjulian ng_rmnode(node2, NULL, NULL, 0); 137371047Sjulian ng_destroy_hook(hook1); 137471047Sjulian NG_HOOK_UNREF(hook1); 137571047Sjulian return (error); 137671047Sjulian } 137771047Sjulian 137870939Sjulian /* 137971047Sjulian * Actually link the two hooks together. 138071047Sjulian */ 138171047Sjulian hook1->hk_peer = hook2; 138271047Sjulian hook2->hk_peer = hook1; 138371047Sjulian 138471047Sjulian /* Each hook is referenced by the other */ 138571047Sjulian NG_HOOK_REF(hook1); 138671047Sjulian NG_HOOK_REF(hook2); 138771047Sjulian 138871047Sjulian /* Give each node the opportunity to veto the pending connection */ 138971047Sjulian if (hook1->hk_node->nd_type->connect) { 139071047Sjulian error = (*hook1->hk_node->nd_type->connect) (hook1); 139171047Sjulian } 139271047Sjulian 139371047Sjulian if ((error == 0) && hook2->hk_node->nd_type->connect) { 139471047Sjulian error = (*hook2->hk_node->nd_type->connect) (hook2); 139571047Sjulian 139671047Sjulian } 139771047Sjulian 139871047Sjulian /* 139970939Sjulian * drop the references we were holding on the two hooks. 140070939Sjulian */ 140171047Sjulian if (error) { 140271047Sjulian ng_destroy_hook(hook2); /* also zaps hook1 */ 140371849Sjulian ng_rmnode(node2, NULL, NULL, 0); 140471047Sjulian } else { 140571047Sjulian /* As a last act, allow the hooks to be used */ 140671047Sjulian hook1->hk_flags &= ~HK_INVALID; 140771047Sjulian hook2->hk_flags &= ~HK_INVALID; 140871047Sjulian } 140971047Sjulian NG_HOOK_UNREF(hook1); 141070939Sjulian NG_HOOK_UNREF(hook2); 141170939Sjulian return (error); 141252419Sjulian} 141371047Sjulian 141470700Sjulian/************************************************************************ 141570700Sjulian Utility routines to send self messages 141670700Sjulian************************************************************************/ 141770700Sjulian 141871849Sjulian/* Shut this node down as soon as everyone is clear of it */ 141971849Sjulian/* Should add arg "immediatly" to jump the queue */ 142070700Sjulianint 142171849Sjulianng_rmnode_self(node_p node) 142270700Sjulian{ 142371849Sjulian int error; 142452419Sjulian 142571849Sjulian if (node == &ng_deadnode) 142671849Sjulian return (0); 142772053Sjulian node->nd_flags |= NG_INVALID; 142871849Sjulian if (node->nd_flags & NG_CLOSING) 142971849Sjulian return (0); 143070700Sjulian 143171849Sjulian error = ng_send_fn(node, NULL, &ng_rmnode, NULL, 0); 143271849Sjulian return (error); 143370700Sjulian} 143470700Sjulian 143571849Sjulianstatic void 143671047Sjulianng_rmhook_part2(node_p node, hook_p hook, void *arg1, int arg2) 143771047Sjulian{ 143871047Sjulian ng_destroy_hook(hook); 143971849Sjulian return ; 144071047Sjulian} 144171047Sjulian 144270935Sjulianint 144370935Sjulianng_rmhook_self(hook_p hook) 144470935Sjulian{ 144571047Sjulian int error; 144670935Sjulian node_p node = NG_HOOK_NODE(hook); 144770935Sjulian 144871047Sjulian if (node == &ng_deadnode) 144971047Sjulian return (0); 145071047Sjulian 145171047Sjulian error = ng_send_fn(node, hook, &ng_rmhook_part2, NULL, 0); 145271047Sjulian return (error); 145370935Sjulian} 145470935Sjulian 145570700Sjulian/*********************************************************************** 145652419Sjulian * Parse and verify a string of the form: <NODE:><PATH> 145752419Sjulian * 145852419Sjulian * Such a string can refer to a specific node or a specific hook 145952419Sjulian * on a specific node, depending on how you look at it. In the 146052419Sjulian * latter case, the PATH component must not end in a dot. 146152419Sjulian * 146252419Sjulian * Both <NODE:> and <PATH> are optional. The <PATH> is a string 146352419Sjulian * of hook names separated by dots. This breaks out the original 146452419Sjulian * string, setting *nodep to "NODE" (or NULL if none) and *pathp 146552419Sjulian * to "PATH" (or NULL if degenerate). Also, *hookp will point to 146652419Sjulian * the final hook component of <PATH>, if any, otherwise NULL. 146752419Sjulian * 146852419Sjulian * This returns -1 if the path is malformed. The char ** are optional. 146970700Sjulian ***********************************************************************/ 147052419Sjulianint 147152419Sjulianng_path_parse(char *addr, char **nodep, char **pathp, char **hookp) 147252419Sjulian{ 147352419Sjulian char *node, *path, *hook; 147452419Sjulian int k; 147552419Sjulian 147652419Sjulian /* 147752419Sjulian * Extract absolute NODE, if any 147852419Sjulian */ 147952419Sjulian for (path = addr; *path && *path != ':'; path++); 148052419Sjulian if (*path) { 148152419Sjulian node = addr; /* Here's the NODE */ 148252419Sjulian *path++ = '\0'; /* Here's the PATH */ 148352419Sjulian 148452419Sjulian /* Node name must not be empty */ 148552419Sjulian if (!*node) 148652419Sjulian return -1; 148752419Sjulian 148852419Sjulian /* A name of "." is OK; otherwise '.' not allowed */ 148952419Sjulian if (strcmp(node, ".") != 0) { 149052419Sjulian for (k = 0; node[k]; k++) 149152419Sjulian if (node[k] == '.') 149252419Sjulian return -1; 149352419Sjulian } 149452419Sjulian } else { 149552419Sjulian node = NULL; /* No absolute NODE */ 149652419Sjulian path = addr; /* Here's the PATH */ 149752419Sjulian } 149852419Sjulian 149952419Sjulian /* Snoop for illegal characters in PATH */ 150052419Sjulian for (k = 0; path[k]; k++) 150152419Sjulian if (path[k] == ':') 150252419Sjulian return -1; 150352419Sjulian 150452419Sjulian /* Check for no repeated dots in PATH */ 150552419Sjulian for (k = 0; path[k]; k++) 150652419Sjulian if (path[k] == '.' && path[k + 1] == '.') 150752419Sjulian return -1; 150852419Sjulian 150952419Sjulian /* Remove extra (degenerate) dots from beginning or end of PATH */ 151052419Sjulian if (path[0] == '.') 151152419Sjulian path++; 151252419Sjulian if (*path && path[strlen(path) - 1] == '.') 151352419Sjulian path[strlen(path) - 1] = 0; 151452419Sjulian 151552419Sjulian /* If PATH has a dot, then we're not talking about a hook */ 151652419Sjulian if (*path) { 151752419Sjulian for (hook = path, k = 0; path[k]; k++) 151852419Sjulian if (path[k] == '.') { 151952419Sjulian hook = NULL; 152052419Sjulian break; 152152419Sjulian } 152252419Sjulian } else 152352419Sjulian path = hook = NULL; 152452419Sjulian 152552419Sjulian /* Done */ 152652419Sjulian if (nodep) 152752419Sjulian *nodep = node; 152852419Sjulian if (pathp) 152952419Sjulian *pathp = path; 153052419Sjulian if (hookp) 153152419Sjulian *hookp = hook; 153252419Sjulian return (0); 153352419Sjulian} 153452419Sjulian 153552419Sjulian/* 153652419Sjulian * Given a path, which may be absolute or relative, and a starting node, 153770700Sjulian * return the destination node. 153852419Sjulian */ 153952419Sjulianint 154070700Sjulianng_path2noderef(node_p here, const char *address, 154170700Sjulian node_p *destp, hook_p *lasthook) 154252419Sjulian{ 154352419Sjulian char fullpath[NG_PATHLEN + 1]; 154452419Sjulian char *nodename, *path, pbuf[2]; 154570700Sjulian node_p node, oldnode; 154652419Sjulian char *cp; 154759728Sjulian hook_p hook = NULL; 154852419Sjulian 154952419Sjulian /* Initialize */ 155070784Sjulian if (destp == NULL) { 155171047Sjulian TRAP_ERROR(); 155252419Sjulian return EINVAL; 155370784Sjulian } 155452419Sjulian *destp = NULL; 155552419Sjulian 155652419Sjulian /* Make a writable copy of address for ng_path_parse() */ 155752419Sjulian strncpy(fullpath, address, sizeof(fullpath) - 1); 155852419Sjulian fullpath[sizeof(fullpath) - 1] = '\0'; 155952419Sjulian 156052419Sjulian /* Parse out node and sequence of hooks */ 156152419Sjulian if (ng_path_parse(fullpath, &nodename, &path, NULL) < 0) { 156271047Sjulian TRAP_ERROR(); 156352419Sjulian return EINVAL; 156452419Sjulian } 156552419Sjulian if (path == NULL) { 156652419Sjulian pbuf[0] = '.'; /* Needs to be writable */ 156752419Sjulian pbuf[1] = '\0'; 156852419Sjulian path = pbuf; 156952419Sjulian } 157052419Sjulian 157170700Sjulian /* 157270700Sjulian * For an absolute address, jump to the starting node. 157370700Sjulian * Note that this holds a reference on the node for us. 157470700Sjulian * Don't forget to drop the reference if we don't need it. 157570700Sjulian */ 157652419Sjulian if (nodename) { 157770700Sjulian node = ng_name2noderef(here, nodename); 157852419Sjulian if (node == NULL) { 157971047Sjulian TRAP_ERROR(); 158052419Sjulian return (ENOENT); 158152419Sjulian } 158270700Sjulian } else { 158370700Sjulian if (here == NULL) { 158471047Sjulian TRAP_ERROR(); 158570700Sjulian return (EINVAL); 158670700Sjulian } 158752419Sjulian node = here; 158870784Sjulian NG_NODE_REF(node); 158970700Sjulian } 159052419Sjulian 159170700Sjulian /* 159270700Sjulian * Now follow the sequence of hooks 159370700Sjulian * XXX 159470700Sjulian * We actually cannot guarantee that the sequence 159570700Sjulian * is not being demolished as we crawl along it 159670700Sjulian * without extra-ordinary locking etc. 159770700Sjulian * So this is a bit dodgy to say the least. 159870700Sjulian * We can probably hold up some things by holding 159970700Sjulian * the nodelist mutex for the time of this 160070700Sjulian * crawl if we wanted.. At least that way we wouldn't have to 160170700Sjulian * worry about the nodes dissappearing, but the hooks would still 160270700Sjulian * be a problem. 160370700Sjulian */ 160452419Sjulian for (cp = path; node != NULL && *cp != '\0'; ) { 160552419Sjulian char *segment; 160652419Sjulian 160752419Sjulian /* 160852419Sjulian * Break out the next path segment. Replace the dot we just 160952419Sjulian * found with a NUL; "cp" points to the next segment (or the 161052419Sjulian * NUL at the end). 161152419Sjulian */ 161252419Sjulian for (segment = cp; *cp != '\0'; cp++) { 161352419Sjulian if (*cp == '.') { 161452419Sjulian *cp++ = '\0'; 161552419Sjulian break; 161652419Sjulian } 161752419Sjulian } 161852419Sjulian 161952419Sjulian /* Empty segment */ 162052419Sjulian if (*segment == '\0') 162152419Sjulian continue; 162252419Sjulian 162352419Sjulian /* We have a segment, so look for a hook by that name */ 162454096Sarchie hook = ng_findhook(node, segment); 162552419Sjulian 162652419Sjulian /* Can't get there from here... */ 162752419Sjulian if (hook == NULL 162870784Sjulian || NG_HOOK_PEER(hook) == NULL 162970784Sjulian || NG_HOOK_NOT_VALID(hook) 163070784Sjulian || NG_HOOK_NOT_VALID(NG_HOOK_PEER(hook))) { 163171047Sjulian TRAP_ERROR(); 163270784Sjulian NG_NODE_UNREF(node); 163370784Sjulian#if 0 163470784Sjulian printf("hooknotvalid %s %s %d %d %d %d ", 163570784Sjulian path, 163670784Sjulian segment, 163770784Sjulian hook == NULL, 163870784Sjulian NG_HOOK_PEER(hook) == NULL, 163970784Sjulian NG_HOOK_NOT_VALID(hook), 164070784Sjulian NG_HOOK_NOT_VALID(NG_HOOK_PEER(hook))); 164170784Sjulian#endif 164252419Sjulian return (ENOENT); 164352419Sjulian } 164452419Sjulian 164570700Sjulian /* 164670700Sjulian * Hop on over to the next node 164770700Sjulian * XXX 164870700Sjulian * Big race conditions here as hooks and nodes go away 164970700Sjulian * *** Idea.. store an ng_ID_t in each hook and use that 165070700Sjulian * instead of the direct hook in this crawl? 165170700Sjulian */ 165270700Sjulian oldnode = node; 165370784Sjulian if ((node = NG_PEER_NODE(hook))) 165470784Sjulian NG_NODE_REF(node); /* XXX RACE */ 165570784Sjulian NG_NODE_UNREF(oldnode); /* XXX another race */ 165670784Sjulian if (NG_NODE_NOT_VALID(node)) { 165770784Sjulian NG_NODE_UNREF(node); /* XXX more races */ 165870700Sjulian node = NULL; 165970700Sjulian } 166052419Sjulian } 166152419Sjulian 166252419Sjulian /* If node somehow missing, fail here (probably this is not needed) */ 166352419Sjulian if (node == NULL) { 166471047Sjulian TRAP_ERROR(); 166552419Sjulian return (ENXIO); 166652419Sjulian } 166752419Sjulian 166852419Sjulian /* Done */ 166952419Sjulian *destp = node; 167059900Sarchie if (lasthook != NULL) 167170784Sjulian *lasthook = (hook ? NG_HOOK_PEER(hook) : NULL); 167252419Sjulian return (0); 167352419Sjulian} 167452419Sjulian 167570700Sjulian/***************************************************************\ 167670700Sjulian* Input queue handling. 167770700Sjulian* All activities are submitted to the node via the input queue 167870700Sjulian* which implements a multiple-reader/single-writer gate. 167970700Sjulian* Items which cannot be handled immeditly are queued. 168070700Sjulian* 168170700Sjulian* read-write queue locking inline functions * 168270700Sjulian\***************************************************************/ 168370700Sjulian 168470700Sjulianstatic __inline item_p ng_dequeue(struct ng_queue * ngq); 168570700Sjulianstatic __inline item_p ng_acquire_read(struct ng_queue * ngq, 168670700Sjulian item_p item); 168770700Sjulianstatic __inline item_p ng_acquire_write(struct ng_queue * ngq, 168870700Sjulian item_p item); 168970700Sjulianstatic __inline void ng_leave_read(struct ng_queue * ngq); 169070700Sjulianstatic __inline void ng_leave_write(struct ng_queue * ngq); 169170700Sjulianstatic __inline void ng_queue_rw(struct ng_queue * ngq, 169270700Sjulian item_p item, int rw); 169370700Sjulian 169452419Sjulian/* 169570700Sjulian * Definition of the bits fields in the ng_queue flag word. 169670700Sjulian * Defined here rather than in netgraph.h because no-one should fiddle 169770700Sjulian * with them. 169870700Sjulian * 169971902Sjulian * The ordering here may be important! don't shuffle these. 170052419Sjulian */ 170170700Sjulian/*- 170270700Sjulian Safety Barrier--------+ (adjustable to suit taste) (not used yet) 170370700Sjulian | 170470700Sjulian V 170570700Sjulian+-------+-------+-------+-------+-------+-------+-------+-------+ 170670700Sjulian| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 170771902Sjulian| |A|c|t|i|v|e| |R|e|a|d|e|r| |C|o|u|n|t| | | | | | | | | |R|A|W| 170871902Sjulian| | | | | | | | | | | | | | | | | | | | | | | | | | | | | |P|W|P| 170970700Sjulian+-------+-------+-------+-------+-------+-------+-------+-------+ 171071902Sjulian\___________________________ ____________________________/ | | | 171171902Sjulian V | | | 171271902Sjulian [active reader count] | | | 171370700Sjulian | | | 171471902Sjulian Read Pending ------------------------------------+ | | 171570700Sjulian | | 171671902Sjulian Active Writer -------------------------------------+ | 171770700Sjulian | 171871902Sjulian Write Pending ---------------------------------------+ 171971902Sjulian 172071902Sjulian 172170700Sjulian*/ 172271902Sjulian#define WRITE_PENDING 0x00000001 172371902Sjulian#define WRITER_ACTIVE 0x00000002 172471902Sjulian#define READ_PENDING 0x00000004 172571902Sjulian#define READER_INCREMENT 0x00000008 172671902Sjulian#define READER_MASK 0xfffffff0 /* Not valid if WRITER_ACTIVE is set */ 172770700Sjulian#define SAFETY_BARRIER 0x00100000 /* 64K items queued should be enough */ 172871902Sjulian 172971902Sjulian/* Defines of more elaborate states on the queue */ 173071902Sjulian/* Mask of bits a read cares about */ 173171902Sjulian#define NGQ_RMASK (WRITE_PENDING|WRITER_ACTIVE|READ_PENDING) 173271902Sjulian 173371902Sjulian/* Mask of bits a write cares about */ 173471902Sjulian#define NGQ_WMASK (NGQ_RMASK|READER_MASK) 173571902Sjulian 173671902Sjulian/* tests to decide if we could get a read or write off the queue */ 173771902Sjulian#define CAN_GET_READ(flag) ((flag & NGQ_RMASK) == READ_PENDING) 173871902Sjulian#define CAN_GET_WRITE(flag) ((flag & NGQ_WMASK) == WRITE_PENDING) 173971902Sjulian 174071902Sjulian/* Is there a chance of getting ANY work off the queue? */ 174171902Sjulian#define CAN_GET_WORK(flag) (CAN_GET_READ(flag) || CAN_GET_WRITE(flag)) 174271902Sjulian 174370700Sjulian/* 174470700Sjulian * Taking into account the current state of the queue and node, possibly take 174570700Sjulian * the next entry off the queue and return it. Return NULL if there was 174670700Sjulian * nothing we could return, either because there really was nothing there, or 174770700Sjulian * because the node was in a state where it cannot yet process the next item 174870700Sjulian * on the queue. 174970700Sjulian * 175070700Sjulian * This MUST MUST MUST be called with the mutex held. 175170700Sjulian */ 175270700Sjulianstatic __inline item_p 175370700Sjulianng_dequeue(struct ng_queue *ngq) 175470700Sjulian{ 175570700Sjulian item_p item; 175670700Sjulian u_int add_arg; 175771902Sjulian 175871902Sjulian if (CAN_GET_READ(ngq->q_flags)) { 175970700Sjulian /* 176071902Sjulian * Head of queue is a reader and we have no write active. 176171902Sjulian * We don't care how many readers are already active. 176271902Sjulian * Adjust the flags for the item we are about to dequeue. 176371902Sjulian * Add the correct increment for the reader count as well. 176470700Sjulian */ 176571902Sjulian add_arg = (READER_INCREMENT - READ_PENDING); 176671902Sjulian } else if (CAN_GET_WRITE(ngq->q_flags)) { 176770700Sjulian /* 176870700Sjulian * There is a pending write, no readers and no active writer. 176970700Sjulian * This means we can go ahead with the pending writer. Note 177070700Sjulian * the fact that we now have a writer, ready for when we take 177170700Sjulian * it off the queue. 177270700Sjulian * 177370700Sjulian * We don't need to worry about a possible collision with the 177470700Sjulian * fasttrack reader. 177570700Sjulian * 177670700Sjulian * The fasttrack thread may take a long time to discover that we 177770700Sjulian * are running so we would have an inconsistent state in the 177870700Sjulian * flags for a while. Since we ignore the reader count 177970700Sjulian * entirely when the WRITER_ACTIVE flag is set, this should 178070700Sjulian * not matter (in fact it is defined that way). If it tests 178170700Sjulian * the flag before this operation, the WRITE_PENDING flag 178270700Sjulian * will make it fail, and if it tests it later, the 178371902Sjulian * WRITER_ACTIVE flag will do the same. If it is SO slow that 178470700Sjulian * we have actually completed the operation, and neither flag 178570700Sjulian * is set (nor the READ_PENDING) by the time that it tests 178670700Sjulian * the flags, then it is actually ok for it to continue. If 178770700Sjulian * it completes and we've finished and the read pending is 178870700Sjulian * set it still fails. 178970700Sjulian * 179070700Sjulian * So we can just ignore it, as long as we can ensure that the 179170700Sjulian * transition from WRITE_PENDING state to the WRITER_ACTIVE 179270700Sjulian * state is atomic. 179370700Sjulian * 179470700Sjulian * After failing, first it will be held back by the mutex, then 179570700Sjulian * when it can proceed, it will queue its request, then it 179670700Sjulian * would arrive at this function. Usually it will have to 179771902Sjulian * leave empty handed because the ACTIVE WRITER bit will be 179870700Sjulian * set. 179971902Sjulian * 180071902Sjulian * Adjust the flags for the item we are about to dequeue 180171902Sjulian * and for the new active writer. 180270700Sjulian */ 180371902Sjulian add_arg = (WRITER_ACTIVE - WRITE_PENDING); 180470700Sjulian /* 180570700Sjulian * We want to write "active writer, no readers " Now go make 180670700Sjulian * it true. In fact there may be a number in the readers 180770700Sjulian * count but we know it is not true and will be fixed soon. 180870700Sjulian * We will fix the flags for the next pending entry in a 180970700Sjulian * moment. 181070700Sjulian */ 181170700Sjulian } else { 181270700Sjulian /* 181370700Sjulian * We can't dequeue anything.. return and say so. Probably we 181470700Sjulian * have a write pending and the readers count is non zero. If 181570700Sjulian * we got here because a reader hit us just at the wrong 181670700Sjulian * moment with the fasttrack code, and put us in a strange 181770700Sjulian * state, then it will be through in just a moment, (as soon 181870700Sjulian * as we release the mutex) and keep things moving. 181971902Sjulian * Make sure we remove ourselves from the work queue. 182070700Sjulian */ 182171902Sjulian ng_worklist_remove(ngq->q_node); 182270700Sjulian return (0); 182370700Sjulian } 182452419Sjulian 182570700Sjulian /* 182670700Sjulian * Now we dequeue the request (whatever it may be) and correct the 182770700Sjulian * pending flags and the next and last pointers. 182870700Sjulian */ 182970700Sjulian item = ngq->queue; 183070700Sjulian ngq->queue = item->el_next; 183170700Sjulian if (ngq->last == &(item->el_next)) { 183270700Sjulian /* 183370700Sjulian * that was the last entry in the queue so set the 'last 183470700Sjulian * pointer up correctly and make sure the pending flags are 183570700Sjulian * clear. 183670700Sjulian */ 183770700Sjulian ngq->last = &(ngq->queue); 183870700Sjulian /* 183971902Sjulian * Whatever flag was set will be cleared and 184071902Sjulian * the new acive field will be set by the add as well, 184171902Sjulian * so we don't need to change add_arg. 184271902Sjulian * But we know we don't need to be on the work list. 184370700Sjulian */ 184471902Sjulian atomic_add_long(&ngq->q_flags, add_arg); 184571902Sjulian ng_worklist_remove(ngq->q_node); 184670700Sjulian } else { 184771902Sjulian /* 184871902Sjulian * Since there is something on the queue, note what it is 184971902Sjulian * in the flags word. 185071902Sjulian */ 185171047Sjulian if ((ngq->queue->el_flags & NGQF_RW) == NGQF_READER) { 185270700Sjulian add_arg += READ_PENDING; 185370700Sjulian } else { 185470700Sjulian add_arg += WRITE_PENDING; 185570700Sjulian } 185671902Sjulian atomic_add_long(&ngq->q_flags, add_arg); 185771902Sjulian /* 185871902Sjulian * If we see more doable work, make sure we are 185971902Sjulian * on the work queue. 186071902Sjulian */ 186171902Sjulian if (CAN_GET_WORK(ngq->q_flags)) { 186271902Sjulian ng_setisr(ngq->q_node); 186371902Sjulian } 186470700Sjulian } 186570700Sjulian /* 186670700Sjulian * We have successfully cleared the old pending flag, set the new one 186770700Sjulian * if it is needed, and incremented the appropriate active field. 186871902Sjulian * (all in one atomic addition.. ) 186970700Sjulian */ 187070700Sjulian return (item); 187170700Sjulian} 187252419Sjulian 187370700Sjulian/* 187470700Sjulian * Queue a packet to be picked up by someone else. 187570700Sjulian * We really don't care who, but we can't or don't want to hang around 187670700Sjulian * to process it ourselves. We are probably an interrupt routine.. 187770700Sjulian * 1 = writer, 0 = reader 187870700Sjulian */ 187970700Sjulian#define NGQRW_R 0 188070700Sjulian#define NGQRW_W 1 188170700Sjulianstatic __inline void 188270700Sjulianng_queue_rw(struct ng_queue * ngq, item_p item, int rw) 188370700Sjulian{ 188470700Sjulian item->el_next = NULL; /* maybe not needed */ 188570700Sjulian *ngq->last = item; 188670700Sjulian /* 188770700Sjulian * If it was the first item in the queue then we need to 188870700Sjulian * set the last pointer and the type flags. 188970700Sjulian */ 189070700Sjulian if (ngq->last == &(ngq->queue)) { 189170700Sjulian /* 189270700Sjulian * When called with constants for rw, the optimiser will 189370700Sjulian * remove the unneeded branch below. 189470700Sjulian */ 189570700Sjulian if (rw == NGQRW_W) { 189670700Sjulian atomic_add_long(&ngq->q_flags, WRITE_PENDING); 189770700Sjulian } else { 189870700Sjulian atomic_add_long(&ngq->q_flags, READ_PENDING); 189970700Sjulian } 190070700Sjulian } 190170700Sjulian ngq->last = &(item->el_next); 190270700Sjulian} 190352419Sjulian 190470700Sjulian 190552419Sjulian/* 190670700Sjulian * This function 'cheats' in that it first tries to 'grab' the use of the 190770700Sjulian * node, without going through the mutex. We can do this becasue of the 190870700Sjulian * semantics of the lock. The semantics include a clause that says that the 190970700Sjulian * value of the readers count is invalid if the WRITER_ACTIVE flag is set. It 191070700Sjulian * also says that the WRITER_ACTIVE flag cannot be set if the readers count 191170700Sjulian * is not zero. Note that this talks about what is valid to SET the 191270700Sjulian * WRITER_ACTIVE flag, because from the moment it is set, the value if the 191370700Sjulian * reader count is immaterial, and not valid. The two 'pending' flags have a 191470700Sjulian * similar effect, in that If they are orthogonal to the two active fields in 191570700Sjulian * how they are set, but if either is set, the attempted 'grab' need to be 191670700Sjulian * backed out because there is earlier work, and we maintain ordering in the 191770700Sjulian * queue. The result of this is that the reader request can try obtain use of 191870700Sjulian * the node with only a single atomic addition, and without any of the mutex 191970700Sjulian * overhead. If this fails the operation degenerates to the same as for other 192070700Sjulian * cases. 192170700Sjulian * 192252419Sjulian */ 192370700Sjulianstatic __inline item_p 192470700Sjulianng_acquire_read(struct ng_queue *ngq, item_p item) 192552419Sjulian{ 192652419Sjulian 192770700Sjulian /* ######### Hack alert ######### */ 192870700Sjulian atomic_add_long(&ngq->q_flags, READER_INCREMENT); 192971902Sjulian if ((ngq->q_flags & NGQ_RMASK) == 0) { 193070700Sjulian /* Successfully grabbed node */ 193170700Sjulian return (item); 193270700Sjulian } 193370700Sjulian /* undo the damage if we didn't succeed */ 193470700Sjulian atomic_subtract_long(&ngq->q_flags, READER_INCREMENT); 193570700Sjulian 193670700Sjulian /* ######### End Hack alert ######### */ 193772200Sbmilekic mtx_lock_spin((&ngq->q_mtx)); 193869922Sjulian /* 193970700Sjulian * Try again. Another processor (or interrupt for that matter) may 194070700Sjulian * have removed the last queued item that was stopping us from 194170700Sjulian * running, between the previous test, and the moment that we took 194270700Sjulian * the mutex. (Or maybe a writer completed.) 194369922Sjulian */ 194471902Sjulian if ((ngq->q_flags & NGQ_RMASK) == 0) { 194570700Sjulian atomic_add_long(&ngq->q_flags, READER_INCREMENT); 194672200Sbmilekic mtx_unlock_spin((&ngq->q_mtx)); 194770700Sjulian return (item); 194870700Sjulian } 194970700Sjulian 195070700Sjulian /* 195170700Sjulian * and queue the request for later. 195270700Sjulian */ 195371047Sjulian item->el_flags |= NGQF_READER; 195470700Sjulian ng_queue_rw(ngq, item, NGQRW_R); 195570700Sjulian 195670700Sjulian /* 195770700Sjulian * Ok, so that's the item successfully queued for later. So now we 195870700Sjulian * see if we can dequeue something to run instead. 195970700Sjulian */ 196070700Sjulian item = ng_dequeue(ngq); 196172200Sbmilekic mtx_unlock_spin(&(ngq->q_mtx)); 196270700Sjulian return (item); 196370700Sjulian} 196470700Sjulian 196570700Sjulianstatic __inline item_p 196670700Sjulianng_acquire_write(struct ng_queue *ngq, item_p item) 196770700Sjulian{ 196870700Sjulianrestart: 196972200Sbmilekic mtx_lock_spin(&(ngq->q_mtx)); 197070700Sjulian /* 197170700Sjulian * If there are no readers, no writer, and no pending packets, then 197270700Sjulian * we can just go ahead. In all other situations we need to queue the 197370700Sjulian * request 197470700Sjulian */ 197571902Sjulian if ((ngq->q_flags & NGQ_WMASK) == 0) { 197670700Sjulian atomic_add_long(&ngq->q_flags, WRITER_ACTIVE); 197772200Sbmilekic mtx_unlock_spin((&ngq->q_mtx)); 197870700Sjulian if (ngq->q_flags & READER_MASK) { 197970700Sjulian /* Collision with fast-track reader */ 198071902Sjulian atomic_subtract_long(&ngq->q_flags, WRITER_ACTIVE); 198170700Sjulian goto restart; 198269922Sjulian } 198370700Sjulian return (item); 198452419Sjulian } 198552419Sjulian 198670700Sjulian /* 198770700Sjulian * and queue the request for later. 198870700Sjulian */ 198971047Sjulian item->el_flags &= ~NGQF_RW; 199070700Sjulian ng_queue_rw(ngq, item, NGQRW_W); 199170700Sjulian 199270700Sjulian /* 199370700Sjulian * Ok, so that's the item successfully queued for later. So now we 199470700Sjulian * see if we can dequeue something to run instead. 199570700Sjulian */ 199670700Sjulian item = ng_dequeue(ngq); 199772200Sbmilekic mtx_unlock_spin(&(ngq->q_mtx)); 199870700Sjulian return (item); 199970700Sjulian} 200070700Sjulian 200170700Sjulianstatic __inline void 200270700Sjulianng_leave_read(struct ng_queue *ngq) 200370700Sjulian{ 200470700Sjulian atomic_subtract_long(&ngq->q_flags, READER_INCREMENT); 200570700Sjulian} 200670700Sjulian 200770700Sjulianstatic __inline void 200870700Sjulianng_leave_write(struct ng_queue *ngq) 200970700Sjulian{ 201070700Sjulian atomic_subtract_long(&ngq->q_flags, WRITER_ACTIVE); 201170700Sjulian} 201270700Sjulian 201370700Sjulianstatic void 201470700Sjulianng_flush_input_queue(struct ng_queue * ngq) 201570700Sjulian{ 201670700Sjulian item_p item; 201770700Sjulian u_int add_arg; 201872200Sbmilekic mtx_lock_spin(&ngq->q_mtx); 201970700Sjulian for (;;) { 202070700Sjulian /* Now take a look at what's on the queue */ 202170700Sjulian if (ngq->q_flags & READ_PENDING) { 202270700Sjulian add_arg = -READ_PENDING; 202370700Sjulian } else if (ngq->q_flags & WRITE_PENDING) { 202470700Sjulian add_arg = -WRITE_PENDING; 202570700Sjulian } else { 202670700Sjulian break; 202770700Sjulian } 202870700Sjulian 202970700Sjulian item = ngq->queue; 203070700Sjulian ngq->queue = item->el_next; 203170700Sjulian if (ngq->last == &(item->el_next)) { 203270700Sjulian ngq->last = &(ngq->queue); 203370700Sjulian } else { 203471047Sjulian if ((ngq->queue->el_flags & NGQF_RW) == NGQF_READER) { 203570700Sjulian add_arg += READ_PENDING; 203670700Sjulian } else { 203770700Sjulian add_arg += WRITE_PENDING; 203870700Sjulian } 203970700Sjulian } 204070700Sjulian atomic_add_long(&ngq->q_flags, add_arg); 204170700Sjulian 204272200Sbmilekic mtx_lock_spin(&ngq->q_mtx); 204370700Sjulian NG_FREE_ITEM(item); 204472200Sbmilekic mtx_unlock_spin(&ngq->q_mtx); 204570700Sjulian } 204671902Sjulian /* 204771902Sjulian * Take us off the work queue if we are there. 204871902Sjulian * We definatly have no work to be done. 204971902Sjulian */ 205071902Sjulian ng_worklist_remove(ngq->q_node); 205172200Sbmilekic mtx_unlock_spin(&ngq->q_mtx); 205270700Sjulian} 205370700Sjulian 205470700Sjulian/*********************************************************************** 205570700Sjulian* Externally visible method for sending or queueing messages or data. 205670700Sjulian***********************************************************************/ 205770700Sjulian 205870700Sjulian/* 205971849Sjulian * The module code should have filled out the item correctly by this stage: 206070700Sjulian * Common: 206170700Sjulian * reference to destination node. 206270700Sjulian * Reference to destination rcv hook if relevant. 206370700Sjulian * Data: 206470700Sjulian * pointer to mbuf 206570700Sjulian * pointer to metadata 206670700Sjulian * Control_Message: 206770700Sjulian * pointer to msg. 206870700Sjulian * ID of original sender node. (return address) 206971849Sjulian * Function: 207071849Sjulian * Function pointer 207171849Sjulian * void * argument 207271849Sjulian * integer argument 207370700Sjulian * 207470700Sjulian * The nodes have several routines and macros to help with this task: 207570700Sjulian */ 207670700Sjulian 207770700Sjulianint 207870700Sjulianng_snd_item(item_p item, int queue) 207970700Sjulian{ 208071849Sjulian hook_p hook = NGI_HOOK(item); 208171902Sjulian node_p node = NGI_NODE(item); 208270700Sjulian int rw; 208370700Sjulian int error = 0, ierror; 208470700Sjulian item_p oitem; 208571902Sjulian struct ng_queue * ngq = &node->nd_input_queue; 208670700Sjulian 208770784Sjulian#ifdef NETGRAPH_DEBUG 208870700Sjulian _ngi_check(item, __FILE__, __LINE__); 208970700Sjulian#endif 209070700Sjulian 209170700Sjulian if (item == NULL) { 209271047Sjulian TRAP_ERROR(); 209370700Sjulian return (EINVAL); /* failed to get queue element */ 209470700Sjulian } 209571902Sjulian if (node == NULL) { 209670700Sjulian NG_FREE_ITEM(item); 209771047Sjulian TRAP_ERROR(); 209870700Sjulian return (EINVAL); /* No address */ 209970700Sjulian } 210071047Sjulian switch(item->el_flags & NGQF_TYPE) { 210171047Sjulian case NGQF_DATA: 210269922Sjulian /* 210370700Sjulian * DATA MESSAGE 210470700Sjulian * Delivered to a node via a non-optional hook. 210570700Sjulian * Both should be present in the item even though 210670700Sjulian * the node is derivable from the hook. 210770700Sjulian * References are held on both by the item. 210869922Sjulian */ 210970700Sjulian CHECK_DATA_MBUF(NGI_M(item)); 211070700Sjulian if (hook == NULL) { 211170700Sjulian NG_FREE_ITEM(item); 211271047Sjulian TRAP_ERROR(); 211370700Sjulian return(EINVAL); 211470700Sjulian } 211570784Sjulian if ((NG_HOOK_NOT_VALID(hook)) 211670784Sjulian || (NG_NODE_NOT_VALID(NG_HOOK_NODE(hook)))) { 211770700Sjulian NG_FREE_ITEM(item); 211870700Sjulian return (ENOTCONN); 211969922Sjulian } 212070784Sjulian if ((hook->hk_flags & HK_QUEUE)) { 212170700Sjulian queue = 1; 212270700Sjulian } 212370700Sjulian /* By default data is a reader in the locking scheme */ 212470700Sjulian item->el_flags |= NGQF_READER; 212570700Sjulian rw = NGQRW_R; 212671047Sjulian break; 212771047Sjulian case NGQF_MESG: 212870700Sjulian /* 212970700Sjulian * CONTROL MESSAGE 213070700Sjulian * Delivered to a node. 213170700Sjulian * Hook is optional. 213270700Sjulian * References are held by the item on the node and 213370700Sjulian * the hook if it is present. 213470700Sjulian */ 213570784Sjulian if (hook && (hook->hk_flags & HK_QUEUE)) { 213670700Sjulian queue = 1; 213770700Sjulian } 213870700Sjulian /* Data messages count as writers unles explicitly exempted */ 213970700Sjulian if (NGI_MSG(item)->header.cmd & NGM_READONLY) { 214070700Sjulian item->el_flags |= NGQF_READER; 214170700Sjulian rw = NGQRW_R; 214270700Sjulian } else { 214371047Sjulian item->el_flags &= ~NGQF_RW; 214470700Sjulian rw = NGQRW_W; 214570700Sjulian } 214671047Sjulian break; 214771047Sjulian case NGQF_FN: 214871047Sjulian item->el_flags &= ~NGQF_RW; 214971047Sjulian rw = NGQRW_W; 215071047Sjulian break; 215171047Sjulian default: 215271047Sjulian NG_FREE_ITEM(item); 215371047Sjulian TRAP_ERROR(); 215471047Sjulian return (EINVAL); 215569922Sjulian } 215670700Sjulian /* 215770700Sjulian * If the node specifies single threading, force writer semantics 215870700Sjulian * Similarly the node may say one hook always produces writers. 215970700Sjulian * These are over-rides. 216070700Sjulian */ 216171902Sjulian if ((node->nd_flags & NG_FORCE_WRITER) 216270784Sjulian || (hook && (hook->hk_flags & HK_FORCE_WRITER))) { 216370700Sjulian rw = NGQRW_W; 216471047Sjulian item->el_flags &= ~NGQF_READER; 216570700Sjulian } 216670700Sjulian if (queue) { 216770700Sjulian /* Put it on the queue for that node*/ 216870784Sjulian#ifdef NETGRAPH_DEBUG 216970700Sjulian _ngi_check(item, __FILE__, __LINE__); 217070700Sjulian#endif 217172200Sbmilekic mtx_lock_spin(&(ngq->q_mtx)); 217270700Sjulian ng_queue_rw(ngq, item, rw); 217370700Sjulian /* 217470700Sjulian * If there are active elements then we can rely on 217570700Sjulian * them. if not we should not rely on another packet 217670700Sjulian * coming here by another path, 217770700Sjulian * so it is best to put us in the netisr list. 217871902Sjulian * We can take the worklist lock with the node locked 217971902Sjulian * BUT NOT THE REVERSE! 218070700Sjulian */ 218171902Sjulian if (CAN_GET_WORK(ngq->q_flags)) { 218271902Sjulian ng_setisr(node); 218370700Sjulian } 218472200Sbmilekic mtx_unlock_spin(&(ngq->q_mtx)); 218570700Sjulian return (0); 218670700Sjulian } 218770700Sjulian /* 218870700Sjulian * Take a queue item and a node and see if we can apply the item to 218970700Sjulian * the node. We may end up getting a different item to apply instead. 219070700Sjulian * Will allow for a piggyback reply only in the case where 219170700Sjulian * there is no queueing. 219270700Sjulian */ 219369922Sjulian 219470700Sjulian oitem = item; 219570700Sjulian /* 219670700Sjulian * We already decided how we will be queueud or treated. 219770700Sjulian * Try get the appropriate operating permission. 219870700Sjulian */ 219970700Sjulian if (rw == NGQRW_R) { 220070700Sjulian item = ng_acquire_read(ngq, item); 220170700Sjulian } else { 220270700Sjulian item = ng_acquire_write(ngq, item); 220370700Sjulian } 220452419Sjulian 220570700Sjulian /* 220670700Sjulian * May have come back with a different item. 220770700Sjulian * or maybe none at all. The one we started with will 220870700Sjulian * have been queued in thises cases. 220970700Sjulian */ 221070700Sjulian if (item == NULL) { 221170700Sjulian return (0); 221270700Sjulian } 221352419Sjulian 221470784Sjulian#ifdef NETGRAPH_DEBUG 221570700Sjulian _ngi_check(item, __FILE__, __LINE__); 221670700Sjulian#endif 221771849Sjulian ierror = ng_apply_item(item); /* drops r/w lock when done */ 221852419Sjulian 221970700Sjulian /* only return an error if it was our initial item.. (compat hack) */ 222070700Sjulian if (oitem == item) { 222170700Sjulian error = ierror; 222270700Sjulian } 222370700Sjulian 222452419Sjulian /* 222570700Sjulian * Now we've handled the packet we brought, (or a friend of it) let's 222670700Sjulian * look for any other packets that may have been queued up. We hold 222770700Sjulian * no locks, so if someone puts something in the queue after 222870700Sjulian * we check that it is empty, it is their problem 222970700Sjulian * to ensure it is processed. If we have the netisr thread cme in here 223070700Sjulian * while we still say we have stuff to do, we may get a boost 223170700Sjulian * in SMP systems. :-) 223252419Sjulian */ 223370700Sjulian for (;;) { 223470700Sjulian /* 223570700Sjulian * dequeue acquires and adjusts the input_queue as it dequeues 223670700Sjulian * packets. It acquires the rw lock as needed. 223770700Sjulian */ 223872200Sbmilekic mtx_lock_spin(&ngq->q_mtx); 223971902Sjulian item = ng_dequeue(ngq); /* fixes worklist too*/ 224070700Sjulian if (!item) { 224172200Sbmilekic mtx_unlock_spin(&ngq->q_mtx); 224271849Sjulian return (error); 224370700Sjulian } 224472200Sbmilekic mtx_unlock_spin(&ngq->q_mtx); 224570700Sjulian 224670700Sjulian /* 224770700Sjulian * We have the appropriate lock, so run the item. 224870700Sjulian * When finished it will drop the lock accordingly 224970700Sjulian */ 225071849Sjulian ierror = ng_apply_item(item); 225170700Sjulian 225270700Sjulian /* 225370700Sjulian * only return an error if it was our initial 225470700Sjulian * item.. (compat hack) 225570700Sjulian */ 225670700Sjulian if (oitem == item) { 225770700Sjulian error = ierror; 225870700Sjulian } 225970700Sjulian } 226071849Sjulian return (error); 226152419Sjulian} 226252419Sjulian 226352419Sjulian/* 226470700Sjulian * We have an item that was possibly queued somewhere. 226570700Sjulian * It should contain all the information needed 226670700Sjulian * to run it on the appropriate node/hook. 226752419Sjulian */ 226852419Sjulianstatic int 226971849Sjulianng_apply_item(item_p item) 227052419Sjulian{ 227171849Sjulian node_p node; 227270700Sjulian hook_p hook; 227371849Sjulian int was_reader = ((item->el_flags & NGQF_RW)); 227471849Sjulian int error = 0; 227570700Sjulian ng_rcvdata_t *rcvdata; 227671885Sjulian ng_rcvmsg_t *rcvmsg; 227752419Sjulian 227871849Sjulian NGI_GET_HOOK(item, hook); /* clears stored hook */ 227971849Sjulian NGI_GET_NODE(item, node); /* clears stored node */ 228070784Sjulian#ifdef NETGRAPH_DEBUG 228170700Sjulian _ngi_check(item, __FILE__, __LINE__); 228270700Sjulian#endif 228371047Sjulian switch (item->el_flags & NGQF_TYPE) { 228470700Sjulian case NGQF_DATA: 228570700Sjulian /* 228670700Sjulian * Check things are still ok as when we were queued. 228770700Sjulian */ 228870700Sjulian if ((hook == NULL) 228970784Sjulian || NG_HOOK_NOT_VALID(hook) 229071885Sjulian || NG_NODE_NOT_VALID(node) ) { 229170700Sjulian error = EIO; 229270700Sjulian NG_FREE_ITEM(item); 229371885Sjulian break; 229470700Sjulian } 229571885Sjulian /* 229671885Sjulian * If no receive method, just silently drop it. 229771885Sjulian * Give preference to the hook over-ride method 229871885Sjulian */ 229971885Sjulian if ((!(rcvdata = hook->hk_rcvdata)) 230071885Sjulian && (!(rcvdata = NG_HOOK_NODE(hook)->nd_type->rcvdata))) { 230171885Sjulian error = 0; 230271885Sjulian NG_FREE_ITEM(item); 230371885Sjulian break; 230471885Sjulian } 230571885Sjulian error = (*rcvdata)(hook, item); 230670700Sjulian break; 230770700Sjulian case NGQF_MESG: 230870700Sjulian if (hook) { 230970784Sjulian if (NG_HOOK_NOT_VALID(hook)) { 231071849Sjulian /* 231171849Sjulian * The hook has been zapped then we can't 231271849Sjulian * use it. Immediatly drop its reference. 231371849Sjulian * The message may not need it. 231471849Sjulian */ 231570784Sjulian NG_HOOK_UNREF(hook); 231670700Sjulian hook = NULL; 231770700Sjulian } 231870700Sjulian } 231970700Sjulian /* 232070700Sjulian * Similarly, if the node is a zombie there is 232170700Sjulian * nothing we can do with it, drop everything. 232270700Sjulian */ 232370784Sjulian if (NG_NODE_NOT_VALID(node)) { 232471047Sjulian TRAP_ERROR(); 232570700Sjulian error = EINVAL; 232670700Sjulian NG_FREE_ITEM(item); 232770700Sjulian } else { 232870700Sjulian /* 232970700Sjulian * Call the appropriate message handler for the object. 233070700Sjulian * It is up to the message handler to free the message. 233170700Sjulian * If it's a generic message, handle it generically, 233270700Sjulian * otherwise call the type's message handler 233370700Sjulian * (if it exists) 233470700Sjulian * XXX (race). Remember that a queued message may 233570700Sjulian * reference a node or hook that has just been 233670700Sjulian * invalidated. It will exist as the queue code 233770700Sjulian * is holding a reference, but.. 233870700Sjulian */ 233970700Sjulian 234070700Sjulian struct ng_mesg *msg = NGI_MSG(item); 234170700Sjulian 234271885Sjulian /* 234371885Sjulian * check if the generic handler owns it. 234471885Sjulian */ 234570700Sjulian if ((msg->header.typecookie == NGM_GENERIC_COOKIE) 234670700Sjulian && ((msg->header.flags & NGF_RESP) == 0)) { 234770700Sjulian error = ng_generic_msg(node, item, hook); 234871885Sjulian break; 234970700Sjulian } 235071885Sjulian /* 235171885Sjulian * Now see if there is a handler (hook or node specific) 235271885Sjulian * in the target node. If none, silently discard. 235371885Sjulian */ 235471885Sjulian if (((!hook) || (!(rcvmsg = hook->hk_rcvmsg))) 235571885Sjulian && (!(rcvmsg = node->nd_type->rcvmsg))) { 235671885Sjulian TRAP_ERROR(); 235771885Sjulian error = 0; 235871885Sjulian NG_FREE_ITEM(item); 235971885Sjulian break; 236071885Sjulian } 236171885Sjulian error = (*rcvmsg)(node, item, hook); 236270700Sjulian } 236370700Sjulian break; 236471047Sjulian case NGQF_FN: 236571047Sjulian /* 236671047Sjulian * We have to implicitly trust the hook, 236771047Sjulian * as some of these are used for system purposes 236871849Sjulian * where the hook is invalid. In the case of 236971849Sjulian * the shutdown message we allow it to hit 237071849Sjulian * even if the node is invalid. 237171047Sjulian */ 237271849Sjulian if ((NG_NODE_NOT_VALID(node)) 237371849Sjulian && (NGI_FN(item) != &ng_rmnode)) { 237471047Sjulian TRAP_ERROR(); 237571047Sjulian error = EINVAL; 237671047Sjulian break; 237771047Sjulian } 237871849Sjulian (*NGI_FN(item))(node, hook, NGI_ARG1(item), NGI_ARG2(item)); 237971047Sjulian NG_FREE_ITEM(item); 238071047Sjulian break; 238171047Sjulian 238270700Sjulian } 238370700Sjulian /* 238470700Sjulian * We held references on some of the resources 238570700Sjulian * that we took from the item. Now that we have 238670700Sjulian * finished doing everything, drop those references. 238770700Sjulian */ 238870700Sjulian if (hook) { 238970784Sjulian NG_HOOK_UNREF(hook); 239070700Sjulian } 239170700Sjulian 239270700Sjulian if (was_reader) { 239370784Sjulian ng_leave_read(&node->nd_input_queue); 239470700Sjulian } else { 239570784Sjulian ng_leave_write(&node->nd_input_queue); 239670700Sjulian } 239770784Sjulian NG_NODE_UNREF(node); 239870700Sjulian return (error); 239970700Sjulian} 240070700Sjulian 240170700Sjulian/*********************************************************************** 240270700Sjulian * Implement the 'generic' control messages 240370700Sjulian ***********************************************************************/ 240470700Sjulianstatic int 240570700Sjulianng_generic_msg(node_p here, item_p item, hook_p lasthook) 240670700Sjulian{ 240770700Sjulian int error = 0; 240870700Sjulian struct ng_mesg *msg; 240970700Sjulian struct ng_mesg *resp = NULL; 241070700Sjulian 241170700Sjulian NGI_GET_MSG(item, msg); 241252419Sjulian if (msg->header.typecookie != NGM_GENERIC_COOKIE) { 241371047Sjulian TRAP_ERROR(); 241470700Sjulian error = EINVAL; 241570700Sjulian goto out; 241652419Sjulian } 241752419Sjulian switch (msg->header.cmd) { 241852419Sjulian case NGM_SHUTDOWN: 241971849Sjulian ng_rmnode(here, NULL, NULL, 0); 242052419Sjulian break; 242152419Sjulian case NGM_MKPEER: 242252419Sjulian { 242352419Sjulian struct ngm_mkpeer *const mkp = (struct ngm_mkpeer *) msg->data; 242452419Sjulian 242552419Sjulian if (msg->header.arglen != sizeof(*mkp)) { 242671047Sjulian TRAP_ERROR(); 242770700Sjulian error = EINVAL; 242870700Sjulian break; 242952419Sjulian } 243052419Sjulian mkp->type[sizeof(mkp->type) - 1] = '\0'; 243152419Sjulian mkp->ourhook[sizeof(mkp->ourhook) - 1] = '\0'; 243252419Sjulian mkp->peerhook[sizeof(mkp->peerhook) - 1] = '\0'; 243352419Sjulian error = ng_mkpeer(here, mkp->ourhook, mkp->peerhook, mkp->type); 243452419Sjulian break; 243552419Sjulian } 243652419Sjulian case NGM_CONNECT: 243752419Sjulian { 243852419Sjulian struct ngm_connect *const con = 243952419Sjulian (struct ngm_connect *) msg->data; 244052419Sjulian node_p node2; 244152419Sjulian 244252419Sjulian if (msg->header.arglen != sizeof(*con)) { 244371047Sjulian TRAP_ERROR(); 244470700Sjulian error = EINVAL; 244570700Sjulian break; 244652419Sjulian } 244752419Sjulian con->path[sizeof(con->path) - 1] = '\0'; 244852419Sjulian con->ourhook[sizeof(con->ourhook) - 1] = '\0'; 244952419Sjulian con->peerhook[sizeof(con->peerhook) - 1] = '\0'; 245070700Sjulian /* Don't forget we get a reference.. */ 245170700Sjulian error = ng_path2noderef(here, con->path, &node2, NULL); 245252419Sjulian if (error) 245352419Sjulian break; 245452419Sjulian error = ng_con_nodes(here, con->ourhook, node2, con->peerhook); 245570784Sjulian NG_NODE_UNREF(node2); 245652419Sjulian break; 245752419Sjulian } 245852419Sjulian case NGM_NAME: 245952419Sjulian { 246052419Sjulian struct ngm_name *const nam = (struct ngm_name *) msg->data; 246152419Sjulian 246252419Sjulian if (msg->header.arglen != sizeof(*nam)) { 246371047Sjulian TRAP_ERROR(); 246470700Sjulian error = EINVAL; 246570700Sjulian break; 246652419Sjulian } 246752419Sjulian nam->name[sizeof(nam->name) - 1] = '\0'; 246852419Sjulian error = ng_name_node(here, nam->name); 246952419Sjulian break; 247052419Sjulian } 247152419Sjulian case NGM_RMHOOK: 247252419Sjulian { 247352419Sjulian struct ngm_rmhook *const rmh = (struct ngm_rmhook *) msg->data; 247452419Sjulian hook_p hook; 247552419Sjulian 247652419Sjulian if (msg->header.arglen != sizeof(*rmh)) { 247771047Sjulian TRAP_ERROR(); 247870700Sjulian error = EINVAL; 247970700Sjulian break; 248052419Sjulian } 248152419Sjulian rmh->ourhook[sizeof(rmh->ourhook) - 1] = '\0'; 248254096Sarchie if ((hook = ng_findhook(here, rmh->ourhook)) != NULL) 248352419Sjulian ng_destroy_hook(hook); 248452419Sjulian break; 248552419Sjulian } 248652419Sjulian case NGM_NODEINFO: 248752419Sjulian { 248852419Sjulian struct nodeinfo *ni; 248952419Sjulian 249070700Sjulian NG_MKRESPONSE(resp, msg, sizeof(*ni), M_NOWAIT); 249152419Sjulian if (resp == NULL) { 249252419Sjulian error = ENOMEM; 249352419Sjulian break; 249452419Sjulian } 249552419Sjulian 249652419Sjulian /* Fill in node info */ 249770700Sjulian ni = (struct nodeinfo *) resp->data; 249870784Sjulian if (NG_NODE_HAS_NAME(here)) 249970784Sjulian strncpy(ni->name, NG_NODE_NAME(here), NG_NODELEN); 250070784Sjulian strncpy(ni->type, here->nd_type->name, NG_TYPELEN); 250152722Sjulian ni->id = ng_node2ID(here); 250270784Sjulian ni->hooks = here->nd_numhooks; 250352419Sjulian break; 250452419Sjulian } 250552419Sjulian case NGM_LISTHOOKS: 250652419Sjulian { 250770784Sjulian const int nhooks = here->nd_numhooks; 250852419Sjulian struct hooklist *hl; 250952419Sjulian struct nodeinfo *ni; 251052419Sjulian hook_p hook; 251152419Sjulian 251252419Sjulian /* Get response struct */ 251370700Sjulian NG_MKRESPONSE(resp, msg, sizeof(*hl) 251470700Sjulian + (nhooks * sizeof(struct linkinfo)), M_NOWAIT); 251552419Sjulian if (resp == NULL) { 251652419Sjulian error = ENOMEM; 251752419Sjulian break; 251852419Sjulian } 251970700Sjulian hl = (struct hooklist *) resp->data; 252052419Sjulian ni = &hl->nodeinfo; 252152419Sjulian 252252419Sjulian /* Fill in node info */ 252370784Sjulian if (NG_NODE_HAS_NAME(here)) 252470784Sjulian strncpy(ni->name, NG_NODE_NAME(here), NG_NODELEN); 252570784Sjulian strncpy(ni->type, here->nd_type->name, NG_TYPELEN); 252652722Sjulian ni->id = ng_node2ID(here); 252752419Sjulian 252852419Sjulian /* Cycle through the linked list of hooks */ 252952419Sjulian ni->hooks = 0; 253070784Sjulian LIST_FOREACH(hook, &here->nd_hooks, hk_hooks) { 253152419Sjulian struct linkinfo *const link = &hl->link[ni->hooks]; 253252419Sjulian 253352419Sjulian if (ni->hooks >= nhooks) { 253452419Sjulian log(LOG_ERR, "%s: number of %s changed\n", 253552419Sjulian __FUNCTION__, "hooks"); 253652419Sjulian break; 253752419Sjulian } 253870784Sjulian if (NG_HOOK_NOT_VALID(hook)) 253952419Sjulian continue; 254070784Sjulian strncpy(link->ourhook, NG_HOOK_NAME(hook), NG_HOOKLEN); 254170784Sjulian strncpy(link->peerhook, 254270784Sjulian NG_PEER_HOOK_NAME(hook), NG_HOOKLEN); 254370784Sjulian if (NG_PEER_NODE_NAME(hook)[0] != '\0') 254452419Sjulian strncpy(link->nodeinfo.name, 254570784Sjulian NG_PEER_NODE_NAME(hook), NG_NODELEN); 254652419Sjulian strncpy(link->nodeinfo.type, 254770784Sjulian NG_PEER_NODE(hook)->nd_type->name, NG_TYPELEN); 254870784Sjulian link->nodeinfo.id = ng_node2ID(NG_PEER_NODE(hook)); 254970784Sjulian link->nodeinfo.hooks = NG_PEER_NODE(hook)->nd_numhooks; 255052419Sjulian ni->hooks++; 255152419Sjulian } 255252419Sjulian break; 255352419Sjulian } 255452419Sjulian 255552419Sjulian case NGM_LISTNAMES: 255652419Sjulian case NGM_LISTNODES: 255752419Sjulian { 255852419Sjulian const int unnamed = (msg->header.cmd == NGM_LISTNODES); 255952419Sjulian struct namelist *nl; 256052419Sjulian node_p node; 256152419Sjulian int num = 0; 256252419Sjulian 256372200Sbmilekic mtx_lock(&ng_nodelist_mtx); 256452419Sjulian /* Count number of nodes */ 256570784Sjulian LIST_FOREACH(node, &ng_nodelist, nd_nodes) { 256670912Sjulian if (NG_NODE_IS_VALID(node) 256770912Sjulian && (unnamed || NG_NODE_HAS_NAME(node))) { 256852419Sjulian num++; 256970912Sjulian } 257052419Sjulian } 257172200Sbmilekic mtx_unlock(&ng_nodelist_mtx); 257252419Sjulian 257352419Sjulian /* Get response struct */ 257470700Sjulian NG_MKRESPONSE(resp, msg, sizeof(*nl) 257570700Sjulian + (num * sizeof(struct nodeinfo)), M_NOWAIT); 257652419Sjulian if (resp == NULL) { 257752419Sjulian error = ENOMEM; 257852419Sjulian break; 257952419Sjulian } 258070700Sjulian nl = (struct namelist *) resp->data; 258152419Sjulian 258252419Sjulian /* Cycle through the linked list of nodes */ 258352419Sjulian nl->numnames = 0; 258472200Sbmilekic mtx_lock(&ng_nodelist_mtx); 258570784Sjulian LIST_FOREACH(node, &ng_nodelist, nd_nodes) { 258652419Sjulian struct nodeinfo *const np = &nl->nodeinfo[nl->numnames]; 258752419Sjulian 258852419Sjulian if (nl->numnames >= num) { 258952419Sjulian log(LOG_ERR, "%s: number of %s changed\n", 259052419Sjulian __FUNCTION__, "nodes"); 259152419Sjulian break; 259252419Sjulian } 259370784Sjulian if (NG_NODE_NOT_VALID(node)) 259452419Sjulian continue; 259570784Sjulian if (!unnamed && (! NG_NODE_HAS_NAME(node))) 259652419Sjulian continue; 259770784Sjulian if (NG_NODE_HAS_NAME(node)) 259870784Sjulian strncpy(np->name, NG_NODE_NAME(node), NG_NODELEN); 259970784Sjulian strncpy(np->type, node->nd_type->name, NG_TYPELEN); 260052722Sjulian np->id = ng_node2ID(node); 260170784Sjulian np->hooks = node->nd_numhooks; 260252419Sjulian nl->numnames++; 260352419Sjulian } 260472200Sbmilekic mtx_unlock(&ng_nodelist_mtx); 260552419Sjulian break; 260652419Sjulian } 260752419Sjulian 260852419Sjulian case NGM_LISTTYPES: 260952419Sjulian { 261052419Sjulian struct typelist *tl; 261152419Sjulian struct ng_type *type; 261252419Sjulian int num = 0; 261352419Sjulian 261472200Sbmilekic mtx_lock(&ng_typelist_mtx); 261552419Sjulian /* Count number of types */ 261670912Sjulian LIST_FOREACH(type, &ng_typelist, types) { 261752419Sjulian num++; 261870912Sjulian } 261972200Sbmilekic mtx_unlock(&ng_typelist_mtx); 262052419Sjulian 262152419Sjulian /* Get response struct */ 262270700Sjulian NG_MKRESPONSE(resp, msg, sizeof(*tl) 262370700Sjulian + (num * sizeof(struct typeinfo)), M_NOWAIT); 262452419Sjulian if (resp == NULL) { 262552419Sjulian error = ENOMEM; 262652419Sjulian break; 262752419Sjulian } 262870700Sjulian tl = (struct typelist *) resp->data; 262952419Sjulian 263052419Sjulian /* Cycle through the linked list of types */ 263152419Sjulian tl->numtypes = 0; 263272200Sbmilekic mtx_lock(&ng_typelist_mtx); 263370700Sjulian LIST_FOREACH(type, &ng_typelist, types) { 263452419Sjulian struct typeinfo *const tp = &tl->typeinfo[tl->numtypes]; 263552419Sjulian 263652419Sjulian if (tl->numtypes >= num) { 263752419Sjulian log(LOG_ERR, "%s: number of %s changed\n", 263852419Sjulian __FUNCTION__, "types"); 263952419Sjulian break; 264052419Sjulian } 264159879Sarchie strncpy(tp->type_name, type->name, NG_TYPELEN); 264271603Sjulian tp->numnodes = type->refs - 1; /* don't count list */ 264352419Sjulian tl->numtypes++; 264452419Sjulian } 264572200Sbmilekic mtx_unlock(&ng_typelist_mtx); 264652419Sjulian break; 264752419Sjulian } 264852419Sjulian 264953913Sarchie case NGM_BINARY2ASCII: 265053913Sarchie { 265164510Sarchie int bufSize = 20 * 1024; /* XXX hard coded constant */ 265253913Sarchie const struct ng_parse_type *argstype; 265353913Sarchie const struct ng_cmdlist *c; 265470700Sjulian struct ng_mesg *binary, *ascii; 265553913Sarchie 265653913Sarchie /* Data area must contain a valid netgraph message */ 265753913Sarchie binary = (struct ng_mesg *)msg->data; 265853913Sarchie if (msg->header.arglen < sizeof(struct ng_mesg) 265970912Sjulian || (msg->header.arglen - sizeof(struct ng_mesg) 266070912Sjulian < binary->header.arglen)) { 266171047Sjulian TRAP_ERROR(); 266253913Sarchie error = EINVAL; 266353913Sarchie break; 266453913Sarchie } 266553913Sarchie 266670700Sjulian /* Get a response message with lots of room */ 266770700Sjulian NG_MKRESPONSE(resp, msg, sizeof(*ascii) + bufSize, M_NOWAIT); 266870159Sjulian if (resp == NULL) { 266953913Sarchie error = ENOMEM; 267053913Sarchie break; 267153913Sarchie } 267270700Sjulian ascii = (struct ng_mesg *)resp->data; 267353913Sarchie 267453913Sarchie /* Copy binary message header to response message payload */ 267553913Sarchie bcopy(binary, ascii, sizeof(*binary)); 267653913Sarchie 267753913Sarchie /* Find command by matching typecookie and command number */ 267870784Sjulian for (c = here->nd_type->cmdlist; 267953913Sarchie c != NULL && c->name != NULL; c++) { 268053913Sarchie if (binary->header.typecookie == c->cookie 268153913Sarchie && binary->header.cmd == c->cmd) 268253913Sarchie break; 268353913Sarchie } 268453913Sarchie if (c == NULL || c->name == NULL) { 268553913Sarchie for (c = ng_generic_cmds; c->name != NULL; c++) { 268653913Sarchie if (binary->header.typecookie == c->cookie 268753913Sarchie && binary->header.cmd == c->cmd) 268853913Sarchie break; 268953913Sarchie } 269053913Sarchie if (c->name == NULL) { 269170700Sjulian NG_FREE_MSG(resp); 269253913Sarchie error = ENOSYS; 269353913Sarchie break; 269453913Sarchie } 269553913Sarchie } 269653913Sarchie 269753913Sarchie /* Convert command name to ASCII */ 269853913Sarchie snprintf(ascii->header.cmdstr, sizeof(ascii->header.cmdstr), 269953913Sarchie "%s", c->name); 270053913Sarchie 270153913Sarchie /* Convert command arguments to ASCII */ 270253913Sarchie argstype = (binary->header.flags & NGF_RESP) ? 270353913Sarchie c->respType : c->mesgType; 270470912Sjulian if (argstype == NULL) { 270553913Sarchie *ascii->data = '\0'; 270670912Sjulian } else { 270753913Sarchie if ((error = ng_unparse(argstype, 270853913Sarchie (u_char *)binary->data, 270953913Sarchie ascii->data, bufSize)) != 0) { 271070700Sjulian NG_FREE_MSG(resp); 271153913Sarchie break; 271253913Sarchie } 271353913Sarchie } 271453913Sarchie 271553913Sarchie /* Return the result as struct ng_mesg plus ASCII string */ 271653913Sarchie bufSize = strlen(ascii->data) + 1; 271753913Sarchie ascii->header.arglen = bufSize; 271870700Sjulian resp->header.arglen = sizeof(*ascii) + bufSize; 271953913Sarchie break; 272053913Sarchie } 272153913Sarchie 272253913Sarchie case NGM_ASCII2BINARY: 272353913Sarchie { 272453913Sarchie int bufSize = 2000; /* XXX hard coded constant */ 272553913Sarchie const struct ng_cmdlist *c; 272653913Sarchie const struct ng_parse_type *argstype; 272770700Sjulian struct ng_mesg *ascii, *binary; 272859178Sarchie int off = 0; 272953913Sarchie 273053913Sarchie /* Data area must contain at least a struct ng_mesg + '\0' */ 273153913Sarchie ascii = (struct ng_mesg *)msg->data; 273270912Sjulian if ((msg->header.arglen < sizeof(*ascii) + 1) 273370912Sjulian || (ascii->header.arglen < 1) 273470912Sjulian || (msg->header.arglen 273570912Sjulian < sizeof(*ascii) + ascii->header.arglen)) { 273671047Sjulian TRAP_ERROR(); 273753913Sarchie error = EINVAL; 273853913Sarchie break; 273953913Sarchie } 274053913Sarchie ascii->data[ascii->header.arglen - 1] = '\0'; 274153913Sarchie 274270700Sjulian /* Get a response message with lots of room */ 274370700Sjulian NG_MKRESPONSE(resp, msg, sizeof(*binary) + bufSize, M_NOWAIT); 274470159Sjulian if (resp == NULL) { 274553913Sarchie error = ENOMEM; 274653913Sarchie break; 274753913Sarchie } 274870700Sjulian binary = (struct ng_mesg *)resp->data; 274953913Sarchie 275053913Sarchie /* Copy ASCII message header to response message payload */ 275153913Sarchie bcopy(ascii, binary, sizeof(*ascii)); 275253913Sarchie 275353913Sarchie /* Find command by matching ASCII command string */ 275470784Sjulian for (c = here->nd_type->cmdlist; 275553913Sarchie c != NULL && c->name != NULL; c++) { 275653913Sarchie if (strcmp(ascii->header.cmdstr, c->name) == 0) 275753913Sarchie break; 275853913Sarchie } 275953913Sarchie if (c == NULL || c->name == NULL) { 276053913Sarchie for (c = ng_generic_cmds; c->name != NULL; c++) { 276153913Sarchie if (strcmp(ascii->header.cmdstr, c->name) == 0) 276253913Sarchie break; 276353913Sarchie } 276453913Sarchie if (c->name == NULL) { 276570700Sjulian NG_FREE_MSG(resp); 276653913Sarchie error = ENOSYS; 276753913Sarchie break; 276853913Sarchie } 276953913Sarchie } 277053913Sarchie 277153913Sarchie /* Convert command name to binary */ 277253913Sarchie binary->header.cmd = c->cmd; 277353913Sarchie binary->header.typecookie = c->cookie; 277453913Sarchie 277553913Sarchie /* Convert command arguments to binary */ 277653913Sarchie argstype = (binary->header.flags & NGF_RESP) ? 277753913Sarchie c->respType : c->mesgType; 277870912Sjulian if (argstype == NULL) { 277953913Sarchie bufSize = 0; 278070912Sjulian } else { 278153913Sarchie if ((error = ng_parse(argstype, ascii->data, 278253913Sarchie &off, (u_char *)binary->data, &bufSize)) != 0) { 278370700Sjulian NG_FREE_MSG(resp); 278453913Sarchie break; 278553913Sarchie } 278653913Sarchie } 278753913Sarchie 278853913Sarchie /* Return the result */ 278953913Sarchie binary->header.arglen = bufSize; 279070700Sjulian resp->header.arglen = sizeof(*binary) + bufSize; 279153913Sarchie break; 279253913Sarchie } 279353913Sarchie 279462471Sphk case NGM_TEXT_CONFIG: 279552419Sjulian case NGM_TEXT_STATUS: 279652419Sjulian /* 279752419Sjulian * This one is tricky as it passes the command down to the 279852419Sjulian * actual node, even though it is a generic type command. 279970700Sjulian * This means we must assume that the item/msg is already freed 280052419Sjulian * when control passes back to us. 280152419Sjulian */ 280270784Sjulian if (here->nd_type->rcvmsg != NULL) { 280370700Sjulian NGI_MSG(item) = msg; /* put it back as we found it */ 280470784Sjulian return((*here->nd_type->rcvmsg)(here, item, lasthook)); 280552419Sjulian } 280652419Sjulian /* Fall through if rcvmsg not supported */ 280752419Sjulian default: 280871047Sjulian TRAP_ERROR(); 280952419Sjulian error = EINVAL; 281052419Sjulian } 281170700Sjulian /* 281270700Sjulian * Sometimes a generic message may be statically allocated 281370700Sjulian * to avoid problems with allocating when in tight memeory situations. 281470700Sjulian * Don't free it if it is so. 281570700Sjulian * I break them appart here, because erros may cause a free if the item 281670700Sjulian * in which case we'd be doing it twice. 281770700Sjulian * they are kept together above, to simplify freeing. 281870700Sjulian */ 281970700Sjulianout: 282070700Sjulian NG_RESPOND_MSG(error, here, item, resp); 282171849Sjulian if (msg) 282270700Sjulian NG_FREE_MSG(msg); 282352419Sjulian return (error); 282452419Sjulian} 282552419Sjulian 282652419Sjulian/* 282759879Sarchie * Copy a 'meta'. 282859879Sarchie * 282959879Sarchie * Returns new meta, or NULL if original meta is NULL or ENOMEM. 283059879Sarchie */ 283159879Sarchiemeta_p 283259879Sarchieng_copy_meta(meta_p meta) 283359879Sarchie{ 283459879Sarchie meta_p meta2; 283559879Sarchie 283659879Sarchie if (meta == NULL) 283759879Sarchie return (NULL); 283870700Sjulian MALLOC(meta2, meta_p, meta->used_len, M_NETGRAPH_META, M_NOWAIT); 283959879Sarchie if (meta2 == NULL) 284059879Sarchie return (NULL); 284159879Sarchie meta2->allocated_len = meta->used_len; 284259879Sarchie bcopy(meta, meta2, meta->used_len); 284359879Sarchie return (meta2); 284459879Sarchie} 284559879Sarchie 284652419Sjulian/************************************************************************ 284752419Sjulian Module routines 284852419Sjulian************************************************************************/ 284952419Sjulian 285052419Sjulian/* 285152419Sjulian * Handle the loading/unloading of a netgraph node type module 285252419Sjulian */ 285352419Sjulianint 285452419Sjulianng_mod_event(module_t mod, int event, void *data) 285552419Sjulian{ 285652419Sjulian struct ng_type *const type = data; 285752419Sjulian int s, error = 0; 285852419Sjulian 285952419Sjulian switch (event) { 286052419Sjulian case MOD_LOAD: 286152419Sjulian 286252419Sjulian /* Register new netgraph node type */ 286352419Sjulian s = splnet(); 286452419Sjulian if ((error = ng_newtype(type)) != 0) { 286552419Sjulian splx(s); 286652419Sjulian break; 286752419Sjulian } 286852419Sjulian 286952419Sjulian /* Call type specific code */ 287052419Sjulian if (type->mod_event != NULL) 287170700Sjulian if ((error = (*type->mod_event)(mod, event, data))) { 287272200Sbmilekic mtx_lock(&ng_typelist_mtx); 287371603Sjulian type->refs--; /* undo it */ 287452419Sjulian LIST_REMOVE(type, types); 287572200Sbmilekic mtx_unlock(&ng_typelist_mtx); 287670700Sjulian } 287752419Sjulian splx(s); 287852419Sjulian break; 287952419Sjulian 288052419Sjulian case MOD_UNLOAD: 288152419Sjulian s = splnet(); 288271603Sjulian if (type->refs > 1) { /* make sure no nodes exist! */ 288352419Sjulian error = EBUSY; 288471603Sjulian } else { 288571603Sjulian if (type->refs == 0) { 288671603Sjulian /* failed load, nothing to undo */ 288771603Sjulian splx(s); 288871603Sjulian break; 288971603Sjulian } 289052419Sjulian if (type->mod_event != NULL) { /* check with type */ 289152419Sjulian error = (*type->mod_event)(mod, event, data); 289252419Sjulian if (error != 0) { /* type refuses.. */ 289352419Sjulian splx(s); 289452419Sjulian break; 289552419Sjulian } 289652419Sjulian } 289772200Sbmilekic mtx_lock(&ng_typelist_mtx); 289852419Sjulian LIST_REMOVE(type, types); 289972200Sbmilekic mtx_unlock(&ng_typelist_mtx); 290052419Sjulian } 290152419Sjulian splx(s); 290252419Sjulian break; 290352419Sjulian 290452419Sjulian default: 290552419Sjulian if (type->mod_event != NULL) 290652419Sjulian error = (*type->mod_event)(mod, event, data); 290752419Sjulian else 290852419Sjulian error = 0; /* XXX ? */ 290952419Sjulian break; 291052419Sjulian } 291152419Sjulian return (error); 291252419Sjulian} 291352419Sjulian 291452419Sjulian/* 291552419Sjulian * Handle loading and unloading for this code. 291652419Sjulian * The only thing we need to link into is the NETISR strucure. 291752419Sjulian */ 291852419Sjulianstatic int 291952419Sjulianngb_mod_event(module_t mod, int event, void *data) 292052419Sjulian{ 292152419Sjulian int s, error = 0; 292252419Sjulian 292352419Sjulian switch (event) { 292452419Sjulian case MOD_LOAD: 292552419Sjulian /* Register line discipline */ 292671902Sjulian mtx_init(&ng_worklist_mtx, "netgraph worklist mutex", MTX_SPIN); 292770700Sjulian mtx_init(&ng_typelist_mtx, "netgraph types mutex", 0); 292870700Sjulian mtx_init(&ng_nodelist_mtx, "netgraph nodelist mutex", 0); 292970700Sjulian mtx_init(&ng_idhash_mtx, "netgraph idhash mutex", 0); 293070700Sjulian mtx_init(&ngq_mtx, "netgraph netisr mutex", 0); 293152419Sjulian s = splimp(); 293252419Sjulian error = register_netisr(NETISR_NETGRAPH, ngintr); 293352419Sjulian splx(s); 293452419Sjulian break; 293552419Sjulian case MOD_UNLOAD: 293652419Sjulian /* You cant unload it because an interface may be using it. */ 293752419Sjulian error = EBUSY; 293852419Sjulian break; 293952419Sjulian default: 294052419Sjulian error = EOPNOTSUPP; 294152419Sjulian break; 294252419Sjulian } 294352419Sjulian return (error); 294452419Sjulian} 294552419Sjulian 294652419Sjulianstatic moduledata_t netgraph_mod = { 294752419Sjulian "netgraph", 294852419Sjulian ngb_mod_event, 294952419Sjulian (NULL) 295052419Sjulian}; 295152419SjulianDECLARE_MODULE(netgraph, netgraph_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE); 295272946SjulianSYSCTL_NODE(_net, OID_AUTO, graph, CTLFLAG_RW, 0, "netgraph Family"); 295372946SjulianSYSCTL_INT(_net_graph, OID_AUTO, abi_version, CTLFLAG_RD, 0, NG_ABI_VERSION,""); 295472946SjulianSYSCTL_INT(_net_graph, OID_AUTO, msg_version, CTLFLAG_RD, 0, NG_VERSION, ""); 295552419Sjulian 295652419Sjulian/************************************************************************ 295770700Sjulian Queue element get/free routines 295852419Sjulian************************************************************************/ 295952419Sjulian 296052419Sjulian 296170700Sjulianstatic int allocated; /* number of items malloc'd */ 296270700Sjulianstatic int maxalloc = 128; /* limit the damage of a leak */ 296370700Sjulianstatic const int ngqfreemax = 64;/* cache at most this many */ 296470700Sjulianstatic const int ngqfreelow = 4; /* try malloc if free < this */ 296570700Sjulianstatic volatile int ngqfreesize; /* number of cached entries */ 296670784Sjulian#ifdef NETGRAPH_DEBUG 296770700Sjulianstatic TAILQ_HEAD(, ng_item) ng_itemlist = TAILQ_HEAD_INITIALIZER(ng_itemlist); 296870700Sjulian#endif 296952419Sjulian/* 297052419Sjulian * Get a queue entry 297170700Sjulian * This is usually called when a packet first enters netgraph. 297270700Sjulian * By definition, this is usually from an interrupt, or from a user. 297370700Sjulian * Users are not so important, but try be quick for the times that it's 297470700Sjulian * an interrupt. Use atomic operations to cope with collisions 297570700Sjulian * with interrupts and other processors. Assumes MALLOC is SMP safe. 297670700Sjulian * XXX If reserve is low, we should try to get 2 from malloc as this 297770700Sjulian * would indicate it often fails. 297852419Sjulian */ 297970700Sjulianstatic item_p 298052419Sjulianng_getqblk(void) 298152419Sjulian{ 298270700Sjulian item_p item = NULL; 298352419Sjulian 298470700Sjulian /* 298570700Sjulian * Try get a cached queue block, or else allocate a new one 298670700Sjulian * If we are less than our reserve, try malloc. If malloc 298770700Sjulian * fails, then that's what the reserve is for... 298870700Sjulian * Don't completely trust ngqfreesize, as it is subject 298970700Sjulian * to races.. (it'll eventually catch up but may be out by one or two 299070700Sjulian * for brief moments(under SMP or interrupts). 299170700Sjulian * ngqfree is the final arbiter. We have our little reserve 299270700Sjulian * because we use M_NOWAIT for malloc. This just helps us 299370700Sjulian * avoid dropping packets while not increasing the time 299471650Sjulian * we take to service the interrupt (on average) (I hope). 299570700Sjulian */ 299670700Sjulian for (;;) { 299770700Sjulian if ((ngqfreesize < ngqfreelow) || (ngqfree == NULL)) { 299870700Sjulian if (allocated < maxalloc) { /* don't leak forever */ 299970700Sjulian MALLOC(item, item_p , 300070700Sjulian sizeof(*item), M_NETGRAPH_ITEM, 300170700Sjulian (M_NOWAIT | M_ZERO)); 300270700Sjulian if (item) { 300370784Sjulian#ifdef NETGRAPH_DEBUG 300470700Sjulian TAILQ_INSERT_TAIL(&ng_itemlist, 300570700Sjulian item, all); 300670784Sjulian#endif /* NETGRAPH_DEBUG */ 300770700Sjulian atomic_add_int(&allocated, 1); 300870700Sjulian break; 300970700Sjulian } 301070700Sjulian } 301170700Sjulian } 301252419Sjulian 301370700Sjulian /* 301470700Sjulian * We didn't or couldn't malloc. 301570700Sjulian * try get one from our cache. 301670700Sjulian * item must be NULL to get here. 301770700Sjulian */ 301870700Sjulian if ((item = ngqfree) != NULL) { 301970700Sjulian /* 302070700Sjulian * Atomically try grab the first item 302170700Sjulian * and put it's successor in its place. 302270700Sjulian * If we fail, just try again.. someone else 302370700Sjulian * beat us to this one or freed one. 302470700Sjulian * Don't worry about races with ngqfreesize. 302570700Sjulian * Close enough is good enough.. 302670700Sjulian */ 302770700Sjulian if (atomic_cmpset_ptr(&ngqfree, item, item->el_next)) { 302870700Sjulian atomic_subtract_int(&ngqfreesize, 1); 302971650Sjulian item->el_flags &= ~NGQF_FREE; 303070700Sjulian break; 303170700Sjulian } 303271650Sjulian /* 303371650Sjulian * something got there before we did.. try again 303471650Sjulian * (go around the loop again) 303571650Sjulian */ 303670700Sjulian item = NULL; 303770700Sjulian } else { 303870700Sjulian /* We really ran out */ 303970700Sjulian break; 304052419Sjulian } 304152419Sjulian } 304270700Sjulian return (item); 304352419Sjulian} 304452419Sjulian 304552419Sjulian/* 304652419Sjulian * Release a queue entry 304752419Sjulian */ 304870700Sjulianvoid 304970700Sjulianng_free_item(item_p item) 305052419Sjulian{ 305152419Sjulian 305270700Sjulian /* 305370700Sjulian * The item may hold resources on it's own. We need to free 305470700Sjulian * these before we can free the item. What they are depends upon 305570700Sjulian * what kind of item it is. it is important that nodes zero 305670700Sjulian * out pointers to resources that they remove from the item 305770700Sjulian * or we release them again here. 305870700Sjulian */ 305970700Sjulian if (item->el_flags & NGQF_FREE) { 306070700Sjulian panic(" Freeing free queue item"); 306152419Sjulian } 306271047Sjulian switch (item->el_flags & NGQF_TYPE) { 306370700Sjulian case NGQF_DATA: 306470700Sjulian /* If we have an mbuf and metadata still attached.. */ 306570700Sjulian NG_FREE_M(_NGI_M(item)); 306670700Sjulian NG_FREE_META(_NGI_META(item)); 306770700Sjulian break; 306870700Sjulian case NGQF_MESG: 306970700Sjulian _NGI_RETADDR(item) = NULL; 307070700Sjulian NG_FREE_MSG(_NGI_MSG(item)); 307170700Sjulian break; 307271047Sjulian case NGQF_FN: 307371047Sjulian /* nothing to free really, */ 307471047Sjulian _NGI_FN(item) = NULL; 307571047Sjulian _NGI_ARG1(item) = NULL; 307671047Sjulian _NGI_ARG2(item) = 0; 307771047Sjulian case NGQF_UNDEF: 307852419Sjulian } 307971849Sjulian /* If we still have a node or hook referenced... */ 308071849Sjulian _NGI_CLR_NODE(item); 308171849Sjulian _NGI_CLR_HOOK(item); 308270700Sjulian item->el_flags |= NGQF_FREE; 308352419Sjulian 308470700Sjulian /* 308570700Sjulian * We have freed any resources held by the item. 308670700Sjulian * now we can free the item itself. 308770700Sjulian */ 308870700Sjulian if (ngqfreesize < ngqfreemax) { /* don't worry about races */ 308970700Sjulian for (;;) { 309070700Sjulian item->el_next = ngqfree; 309170700Sjulian if (atomic_cmpset_ptr(&ngqfree, item->el_next, item)) { 309270700Sjulian break; 309370700Sjulian } 309470700Sjulian } 309570700Sjulian atomic_add_int(&ngqfreesize, 1); 309670700Sjulian } else { 309770700Sjulian /* This is the only place that should use this Macro */ 309870784Sjulian#ifdef NETGRAPH_DEBUG 309970700Sjulian TAILQ_REMOVE(&ng_itemlist, item, all); 310070784Sjulian#endif /* NETGRAPH_DEBUG */ 310170700Sjulian NG_FREE_ITEM_REAL(item); 310270700Sjulian atomic_subtract_int(&allocated, 1); 310370700Sjulian } 310470700Sjulian} 310552419Sjulian 310670784Sjulian#ifdef NETGRAPH_DEBUG 310770700Sjulianvoid 310870784Sjuliandumphook (hook_p hook, char *file, int line) 310970784Sjulian{ 311070784Sjulian printf("hook: name %s, %d refs, Last touched:\n", 311170784Sjulian _NG_HOOK_NAME(hook), hook->hk_refs); 311270784Sjulian printf(" Last active @ %s, line %d\n", 311370784Sjulian hook->lastfile, hook->lastline); 311470784Sjulian if (line) { 311570784Sjulian printf(" problem discovered at file %s, line %d\n", file, line); 311670784Sjulian } 311770784Sjulian} 311870784Sjulian 311970784Sjulianvoid 312070784Sjuliandumpnode(node_p node, char *file, int line) 312170784Sjulian{ 312270784Sjulian printf("node: ID [%x]: type '%s', %d hooks, flags 0x%x, %d refs, %s:\n", 312371047Sjulian _NG_NODE_ID(node), node->nd_type->name, 312470784Sjulian node->nd_numhooks, node->nd_flags, 312570784Sjulian node->nd_refs, node->nd_name); 312670784Sjulian printf(" Last active @ %s, line %d\n", 312770784Sjulian node->lastfile, node->lastline); 312870784Sjulian if (line) { 312970784Sjulian printf(" problem discovered at file %s, line %d\n", file, line); 313070784Sjulian } 313170784Sjulian} 313270784Sjulian 313370784Sjulianvoid 313470700Sjuliandumpitem(item_p item, char *file, int line) 313570700Sjulian{ 313670700Sjulian if (item->el_flags & NGQF_FREE) { 313770700Sjulian printf(" Free item, freed at %s, line %d\n", 313870700Sjulian item->lastfile, item->lastline); 313952419Sjulian } else { 314070700Sjulian printf(" ACTIVE item, last used at %s, line %d", 314170700Sjulian item->lastfile, item->lastline); 314271047Sjulian switch(item->el_flags & NGQF_TYPE) { 314371047Sjulian case NGQF_DATA: 314471047Sjulian printf(" - [data]\n"); 314571047Sjulian break; 314671047Sjulian case NGQF_MESG: 314770700Sjulian printf(" - retaddr[%d]:\n", _NGI_RETADDR(item)); 314871047Sjulian break; 314971047Sjulian case NGQF_FN: 315071047Sjulian printf(" - fn@%p (%p, %p, %p, %d (%x))\n", 315171047Sjulian item->body.fn.fn_fn, 315271849Sjulian NGI_NODE(item), 315371849Sjulian NGI_HOOK(item), 315471047Sjulian item->body.fn.fn_arg1, 315571047Sjulian item->body.fn.fn_arg2, 315671047Sjulian item->body.fn.fn_arg2); 315771047Sjulian break; 315871047Sjulian case NGQF_UNDEF: 315971047Sjulian printf(" - UNDEFINED!\n"); 316070700Sjulian } 316152419Sjulian } 316270784Sjulian if (line) { 316370784Sjulian printf(" problem discovered at file %s, line %d\n", file, line); 316471849Sjulian if (NGI_NODE(item)) { 316570784Sjulian printf("node %p ([%x])\n", 316671849Sjulian NGI_NODE(item), ng_node2ID(NGI_NODE(item))); 316770784Sjulian } 316870784Sjulian } 316970700Sjulian} 317052419Sjulian 317170784Sjulianstatic void 317270784Sjulianng_dumpitems(void) 317370784Sjulian{ 317470784Sjulian item_p item; 317570784Sjulian int i = 1; 317670784Sjulian TAILQ_FOREACH(item, &ng_itemlist, all) { 317770784Sjulian printf("[%d] ", i++); 317870784Sjulian dumpitem(item, NULL, 0); 317970784Sjulian } 318070784Sjulian} 318170784Sjulian 318270784Sjulianstatic void 318370784Sjulianng_dumpnodes(void) 318470784Sjulian{ 318570784Sjulian node_p node; 318670784Sjulian int i = 1; 318770784Sjulian SLIST_FOREACH(node, &ng_allnodes, nd_all) { 318870784Sjulian printf("[%d] ", i++); 318970784Sjulian dumpnode(node, NULL, 0); 319070784Sjulian } 319170784Sjulian} 319270784Sjulian 319370784Sjulianstatic void 319470784Sjulianng_dumphooks(void) 319570784Sjulian{ 319670784Sjulian hook_p hook; 319770784Sjulian int i = 1; 319870784Sjulian SLIST_FOREACH(hook, &ng_allhooks, hk_all) { 319970784Sjulian printf("[%d] ", i++); 320070784Sjulian dumphook(hook, NULL, 0); 320170784Sjulian } 320270784Sjulian} 320370784Sjulian 320470700Sjulianstatic int 320570700Sjuliansysctl_debug_ng_dump_items(SYSCTL_HANDLER_ARGS) 320670700Sjulian{ 320770700Sjulian int error; 320870700Sjulian int val; 320970700Sjulian int i; 321070700Sjulian 321170700Sjulian val = allocated; 321270700Sjulian i = 1; 321370700Sjulian error = sysctl_handle_int(oidp, &val, sizeof(int), req); 321471047Sjulian if (error != 0 || req->newptr == NULL) 321571047Sjulian return (error); 321671047Sjulian if (val == 42) { 321770784Sjulian ng_dumpitems(); 321870784Sjulian ng_dumpnodes(); 321970784Sjulian ng_dumphooks(); 322070700Sjulian } 322171047Sjulian return (0); 322252419Sjulian} 322352419Sjulian 322471047SjulianSYSCTL_PROC(_debug, OID_AUTO, ng_dump_items, CTLTYPE_INT | CTLFLAG_RW, 322571047Sjulian 0, sizeof(int), sysctl_debug_ng_dump_items, "I", "Number of allocated items"); 322670784Sjulian#endif /* NETGRAPH_DEBUG */ 322770700Sjulian 322870700Sjulian 322970700Sjulian/*********************************************************************** 323070700Sjulian* Worklist routines 323170700Sjulian**********************************************************************/ 323270700Sjulian/* NETISR thread enters here */ 323352419Sjulian/* 323470700Sjulian * Pick a node off the list of nodes with work, 323570700Sjulian * try get an item to process off it. 323670700Sjulian * If there are no more, remove the node from the list. 323752419Sjulian */ 323870700Sjulianstatic void 323970700Sjulianngintr(void) 324052419Sjulian{ 324170700Sjulian item_p item; 324270700Sjulian node_p node = NULL; 324352419Sjulian 324470700Sjulian for (;;) { 324572200Sbmilekic mtx_lock_spin(&ng_worklist_mtx); 324670700Sjulian node = TAILQ_FIRST(&ng_worklist); 324770700Sjulian if (!node) { 324872200Sbmilekic mtx_unlock_spin(&ng_worklist_mtx); 324970700Sjulian break; 325069922Sjulian } 325171902Sjulian node->nd_flags &= ~NG_WORKQ; 325270784Sjulian TAILQ_REMOVE(&ng_worklist, node, nd_work); 325372200Sbmilekic mtx_unlock_spin(&ng_worklist_mtx); 325470700Sjulian /* 325570700Sjulian * We have the node. We also take over the reference 325670700Sjulian * that the list had on it. 325770700Sjulian * Now process as much as you can, until it won't 325870700Sjulian * let you have another item off the queue. 325970700Sjulian * All this time, keep the reference 326070700Sjulian * that lets us be sure that the node still exists. 326170700Sjulian * Let the reference go at the last minute. 326271902Sjulian * ng_dequeue will put us back on the worklist 326371902Sjulian * if there is more too do. This may be of use if there 326471902Sjulian * are Multiple Processors and multiple Net threads in the 326571902Sjulian * future. 326670700Sjulian */ 326770700Sjulian for (;;) { 326872200Sbmilekic mtx_lock_spin(&node->nd_input_queue.q_mtx); 326970784Sjulian item = ng_dequeue(&node->nd_input_queue); 327070700Sjulian if (item == NULL) { 327172200Sbmilekic mtx_unlock_spin(&node->nd_input_queue.q_mtx); 327270784Sjulian NG_NODE_UNREF(node); 327370700Sjulian break; /* go look for another node */ 327470700Sjulian } else { 327572200Sbmilekic mtx_unlock_spin(&node->nd_input_queue.q_mtx); 327671849Sjulian ng_apply_item(item); 327770700Sjulian } 327870700Sjulian } 327952419Sjulian } 328070700Sjulian} 328169922Sjulian 328270700Sjulianstatic void 328370700Sjulianng_worklist_remove(node_p node) 328470700Sjulian{ 328572200Sbmilekic mtx_lock_spin(&ng_worklist_mtx); 328670784Sjulian if (node->nd_flags & NG_WORKQ) { 328770784Sjulian TAILQ_REMOVE(&ng_worklist, node, nd_work); 328870784Sjulian NG_NODE_UNREF(node); 328970700Sjulian } 329070784Sjulian node->nd_flags &= ~NG_WORKQ; 329172200Sbmilekic mtx_unlock_spin(&ng_worklist_mtx); 329270700Sjulian} 329370700Sjulian 329470700Sjulianstatic void 329570700Sjulianng_setisr(node_p node) 329670700Sjulian{ 329772200Sbmilekic mtx_lock_spin(&ng_worklist_mtx); 329870784Sjulian if ((node->nd_flags & NG_WORKQ) == 0) { 329969922Sjulian /* 330070700Sjulian * If we are not already on the work queue, 330170700Sjulian * then put us on. 330269922Sjulian */ 330370784Sjulian node->nd_flags |= NG_WORKQ; 330470784Sjulian TAILQ_INSERT_TAIL(&ng_worklist, node, nd_work); 330570784Sjulian NG_NODE_REF(node); 330670700Sjulian } 330772200Sbmilekic mtx_unlock_spin(&ng_worklist_mtx); 330870700Sjulian schednetisr(NETISR_NETGRAPH); 330970700Sjulian} 331070700Sjulian 331170700Sjulian 331270700Sjulian/*********************************************************************** 331370700Sjulian* Externally useable functions to set up a queue item ready for sending 331470700Sjulian***********************************************************************/ 331570700Sjulian 331670784Sjulian#ifdef NETGRAPH_DEBUG 331770784Sjulian#define ITEM_DEBUG_CHECKS \ 331870700Sjulian do { \ 331971849Sjulian if (NGI_NODE(item) ) { \ 332070700Sjulian printf("item already has node"); \ 332170700Sjulian Debugger("has node"); \ 332271849Sjulian NGI_CLR_NODE(item); \ 332370700Sjulian } \ 332471849Sjulian if (NGI_HOOK(item) ) { \ 332570700Sjulian printf("item already has hook"); \ 332670700Sjulian Debugger("has hook"); \ 332771849Sjulian NGI_CLR_HOOK(item); \ 332870700Sjulian } \ 332970700Sjulian } while (0) 333070700Sjulian#else 333170784Sjulian#define ITEM_DEBUG_CHECKS 333270700Sjulian#endif 333370700Sjulian 333470700Sjulian/* 333570700Sjulian * Put elements into the item. 333670700Sjulian * Hook and node references will be removed when the item is dequeued. 333770700Sjulian * (or equivalent) 333870700Sjulian * (XXX) Unsafe because no reference held by peer on remote node. 333970700Sjulian * remote node might go away in this timescale. 334070700Sjulian * We know the hooks can't go away because that would require getting 334170700Sjulian * a writer item on both nodes and we must have at least a reader 334270700Sjulian * here to eb able to do this. 334370700Sjulian * Note that the hook loaded is the REMOTE hook. 334470700Sjulian * 334570700Sjulian * This is possibly in the critical path for new data. 334670700Sjulian */ 334770700Sjulianitem_p 334870700Sjulianng_package_data(struct mbuf *m, meta_p meta) 334970700Sjulian{ 335070700Sjulian item_p item; 335170700Sjulian 335270700Sjulian if ((item = ng_getqblk()) == NULL) { 335370700Sjulian NG_FREE_M(m); 335470700Sjulian NG_FREE_META(meta); 335570700Sjulian return (NULL); 335670700Sjulian } 335770784Sjulian ITEM_DEBUG_CHECKS; 335870700Sjulian item->el_flags = NGQF_DATA; 335970700Sjulian item->el_next = NULL; 336070700Sjulian NGI_M(item) = m; 336170700Sjulian NGI_META(item) = meta; 336270700Sjulian return (item); 336370700Sjulian} 336470700Sjulian 336570700Sjulian/* 336670700Sjulian * Allocate a queue item and put items into it.. 336770700Sjulian * Evaluate the address as this will be needed to queue it and 336870700Sjulian * to work out what some of the fields should be. 336970700Sjulian * Hook and node references will be removed when the item is dequeued. 337070700Sjulian * (or equivalent) 337170700Sjulian */ 337270700Sjulianitem_p 337370700Sjulianng_package_msg(struct ng_mesg *msg) 337470700Sjulian{ 337570700Sjulian item_p item; 337670700Sjulian 337770700Sjulian if ((item = ng_getqblk()) == NULL) { 337871849Sjulian NG_FREE_MSG(msg); 337970700Sjulian return (NULL); 338069922Sjulian } 338170784Sjulian ITEM_DEBUG_CHECKS; 338270700Sjulian item->el_flags = NGQF_MESG; 338370700Sjulian item->el_next = NULL; 338470700Sjulian /* 338570700Sjulian * Set the current lasthook into the queue item 338670700Sjulian */ 338770700Sjulian NGI_MSG(item) = msg; 338870700Sjulian NGI_RETADDR(item) = NULL; 338970700Sjulian return (item); 339070700Sjulian} 339169922Sjulian 339270700Sjulian 339370700Sjulian 339471849Sjulian#define SET_RETADDR(item, here, retaddr) \ 339571047Sjulian do { /* Data or fn items don't have retaddrs */ \ 339671047Sjulian if ((item->el_flags & NGQF_TYPE) == NGQF_MESG) { \ 339770700Sjulian if (retaddr) { \ 339870700Sjulian NGI_RETADDR(item) = retaddr; \ 339970700Sjulian } else { \ 340070700Sjulian /* \ 340170700Sjulian * The old return address should be ok. \ 340270700Sjulian * If there isn't one, use the address \ 340370700Sjulian * here. \ 340470700Sjulian */ \ 340570700Sjulian if (NGI_RETADDR(item) == 0) { \ 340670700Sjulian NGI_RETADDR(item) \ 340770700Sjulian = ng_node2ID(here); \ 340870700Sjulian } \ 340970700Sjulian } \ 341070700Sjulian } \ 341170700Sjulian } while (0) 341270700Sjulian 341370700Sjulianint 341470700Sjulianng_address_hook(node_p here, item_p item, hook_p hook, ng_ID_t retaddr) 341570700Sjulian{ 341671849Sjulian hook_p peer; 341771849Sjulian node_p peernode; 341870784Sjulian ITEM_DEBUG_CHECKS; 341970700Sjulian /* 342070700Sjulian * Quick sanity check.. 342170784Sjulian * Since a hook holds a reference on it's node, once we know 342270784Sjulian * that the peer is still connected (even if invalid,) we know 342370784Sjulian * that the peer node is present, though maybe invalid. 342470700Sjulian */ 342570700Sjulian if ((hook == NULL) 342670784Sjulian || NG_HOOK_NOT_VALID(hook) 342770784Sjulian || (NG_HOOK_PEER(hook) == NULL) 342870784Sjulian || NG_HOOK_NOT_VALID(NG_HOOK_PEER(hook)) 342970784Sjulian || NG_NODE_NOT_VALID(NG_PEER_NODE(hook))) { 343070700Sjulian NG_FREE_ITEM(item); 343171047Sjulian TRAP_ERROR(); 343270700Sjulian return (EINVAL); 343352419Sjulian } 343452419Sjulian 343570700Sjulian /* 343670700Sjulian * Transfer our interest to the other (peer) end. 343770700Sjulian */ 343871849Sjulian peer = NG_HOOK_PEER(hook); 343971849Sjulian NG_HOOK_REF(peer); 344071849Sjulian NGI_SET_HOOK(item, peer); 344171849Sjulian peernode = NG_PEER_NODE(hook); 344271849Sjulian NG_NODE_REF(peernode); 344371849Sjulian NGI_SET_NODE(item, peernode); 344470700Sjulian return (0); 344570700Sjulian} 344652419Sjulian 344770700Sjulianint 344870700Sjulianng_address_path(node_p here, item_p item, char *address, ng_ID_t retaddr) 344970700Sjulian{ 345070700Sjulian node_p dest = NULL; 345170700Sjulian hook_p hook = NULL; 345270700Sjulian int error; 345370700Sjulian 345470784Sjulian ITEM_DEBUG_CHECKS; 345570700Sjulian /* 345670700Sjulian * Note that ng_path2noderef increments the reference count 345770700Sjulian * on the node for us if it finds one. So we don't have to. 345870700Sjulian */ 345970700Sjulian error = ng_path2noderef(here, address, &dest, &hook); 346070700Sjulian if (error) { 346170700Sjulian NG_FREE_ITEM(item); 346270784Sjulian return (error); 346352419Sjulian } 346471849Sjulian NGI_SET_NODE(item, dest); 346571849Sjulian if ( hook) { 346670784Sjulian NG_HOOK_REF(hook); /* don't let it go while on the queue */ 346771849Sjulian NGI_SET_HOOK(item, hook); 346871849Sjulian } 346971849Sjulian SET_RETADDR(item, here, retaddr); 347070700Sjulian return (0); 347170700Sjulian} 347252419Sjulian 347370700Sjulianint 347470700Sjulianng_address_ID(node_p here, item_p item, ng_ID_t ID, ng_ID_t retaddr) 347570700Sjulian{ 347670700Sjulian node_p dest; 347770700Sjulian 347870784Sjulian ITEM_DEBUG_CHECKS; 347970700Sjulian /* 348070700Sjulian * Find the target node. 348170700Sjulian */ 348270700Sjulian dest = ng_ID2noderef(ID); /* GETS REFERENCE! */ 348370700Sjulian if (dest == NULL) { 348470700Sjulian NG_FREE_ITEM(item); 348571047Sjulian TRAP_ERROR(); 348670700Sjulian return(EINVAL); 348770700Sjulian } 348870700Sjulian /* Fill out the contents */ 348970700Sjulian item->el_flags = NGQF_MESG; 349070700Sjulian item->el_next = NULL; 349171849Sjulian NGI_SET_NODE(item, dest); 349271849Sjulian NGI_CLR_HOOK(item); 349371849Sjulian SET_RETADDR(item, here, retaddr); 349452419Sjulian return (0); 349552419Sjulian} 349652419Sjulian 349752419Sjulian/* 349870700Sjulian * special case to send a message to self (e.g. destroy node) 349970700Sjulian * Possibly indicate an arrival hook too. 350070700Sjulian * Useful for removing that hook :-) 350152419Sjulian */ 350270700Sjulianitem_p 350370700Sjulianng_package_msg_self(node_p here, hook_p hook, struct ng_mesg *msg) 350452419Sjulian{ 350570700Sjulian item_p item; 350652419Sjulian 350770700Sjulian /* 350870700Sjulian * Find the target node. 350970700Sjulian * If there is a HOOK argument, then use that in preference 351070700Sjulian * to the address. 351170700Sjulian */ 351270700Sjulian if ((item = ng_getqblk()) == NULL) { 351371849Sjulian NG_FREE_MSG(msg); 351470700Sjulian return (NULL); 351552419Sjulian } 351670700Sjulian 351770700Sjulian /* Fill out the contents */ 351870700Sjulian item->el_flags = NGQF_MESG; 351970700Sjulian item->el_next = NULL; 352070784Sjulian NG_NODE_REF(here); 352171849Sjulian NGI_SET_NODE(item, here); 352271849Sjulian if (hook) { 352370784Sjulian NG_HOOK_REF(hook); 352471849Sjulian NGI_SET_HOOK(item, hook); 352571849Sjulian } 352670700Sjulian NGI_MSG(item) = msg; 352770700Sjulian NGI_RETADDR(item) = ng_node2ID(here); 352870700Sjulian return (item); 352952419Sjulian} 353052419Sjulian 353171047Sjulianint 353271047Sjulianng_send_fn(node_p node, hook_p hook, ng_item_fn *fn, void * arg1, int arg2) 353371047Sjulian{ 353471047Sjulian item_p item; 353571047Sjulian 353671047Sjulian if ((item = ng_getqblk()) == NULL) { 353771047Sjulian return (ENOMEM); 353871047Sjulian } 353971047Sjulian item->el_flags = NGQF_FN | NGQF_WRITER; 354071047Sjulian NG_NODE_REF(node); 354171849Sjulian NGI_SET_NODE(item, node); 354271849Sjulian if (hook) { 354371047Sjulian NG_HOOK_REF(hook); 354471849Sjulian NGI_SET_HOOK(item, hook); 354571047Sjulian } 354671047Sjulian NGI_FN(item) = fn; 354771047Sjulian NGI_ARG1(item) = arg1; 354871047Sjulian NGI_ARG2(item) = arg2; 354971047Sjulian return (ng_snd_item(item, 0)); 355071047Sjulian} 355171047Sjulian 355270700Sjulian/* 355370700Sjulian * Set the address, if none given, give the node here. 355470700Sjulian */ 355570700Sjulianvoid 355670700Sjulianng_replace_retaddr(node_p here, item_p item, ng_ID_t retaddr) 355770700Sjulian{ 355870700Sjulian if (retaddr) { 355970700Sjulian NGI_RETADDR(item) = retaddr; 356070700Sjulian } else { 356170700Sjulian /* 356270700Sjulian * The old return address should be ok. 356370700Sjulian * If there isn't one, use the address here. 356470700Sjulian */ 356570700Sjulian NGI_RETADDR(item) = ng_node2ID(here); 356670700Sjulian } 356770700Sjulian} 356852419Sjulian 356970700Sjulian#define TESTING 357070700Sjulian#ifdef TESTING 357170700Sjulian/* just test all the macros */ 357270700Sjulianvoid 357370700Sjulianng_macro_test(item_p item); 357470700Sjulianvoid 357570700Sjulianng_macro_test(item_p item) 357670700Sjulian{ 357770700Sjulian node_p node = NULL; 357870700Sjulian hook_p hook = NULL; 357970700Sjulian struct mbuf *m; 358070700Sjulian meta_p meta; 358170700Sjulian struct ng_mesg *msg; 358270700Sjulian ng_ID_t retaddr; 358370700Sjulian int error; 358470700Sjulian 358570700Sjulian NGI_GET_M(item, m); 358670700Sjulian NGI_GET_META(item, meta); 358770700Sjulian NGI_GET_MSG(item, msg); 358870700Sjulian retaddr = NGI_RETADDR(item); 358970700Sjulian NG_SEND_DATA(error, hook, m, meta); 359070700Sjulian NG_SEND_DATA_ONLY(error, hook, m); 359170700Sjulian NG_FWD_NEW_DATA(error, item, hook, m); 359270784Sjulian NG_FWD_ITEM_HOOK(error, item, hook); 359370700Sjulian NG_SEND_MSG_HOOK(error, node, msg, hook, retaddr); 359470700Sjulian NG_SEND_MSG_ID(error, node, msg, retaddr, retaddr); 359570700Sjulian NG_SEND_MSG_PATH(error, node, msg, ".:", retaddr); 359670700Sjulian NG_QUEUE_MSG(error, node, msg, ".:", retaddr); 359770700Sjulian NG_FWD_MSG_HOOK(error, node, item, hook, retaddr); 359870700Sjulian} 359970700Sjulian#endif /* TESTING */ 360070700Sjulian 3601