14cf49a43SJulian Elischer /* 24cf49a43SJulian Elischer * ng_base.c 3c398230bSWarner Losh */ 4c398230bSWarner Losh 5c398230bSWarner Losh /*- 64cf49a43SJulian Elischer * Copyright (c) 1996-1999 Whistle Communications, Inc. 74cf49a43SJulian Elischer * All rights reserved. 84cf49a43SJulian Elischer * 94cf49a43SJulian Elischer * Subject to the following obligations and disclaimer of warranty, use and 104cf49a43SJulian Elischer * redistribution of this software, in source or object code forms, with or 114cf49a43SJulian Elischer * without modifications are expressly permitted by Whistle Communications; 124cf49a43SJulian Elischer * provided, however, that: 134cf49a43SJulian Elischer * 1. Any and all reproductions of the source or object code must include the 144cf49a43SJulian Elischer * copyright notice above and the following disclaimer of warranties; and 154cf49a43SJulian Elischer * 2. No rights are granted, in any manner or form, to use Whistle 164cf49a43SJulian Elischer * Communications, Inc. trademarks, including the mark "WHISTLE 174cf49a43SJulian Elischer * COMMUNICATIONS" on advertising, endorsements, or otherwise except as 184cf49a43SJulian Elischer * such appears in the above copyright notice or in the software. 194cf49a43SJulian Elischer * 204cf49a43SJulian Elischer * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND 214cf49a43SJulian Elischer * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO 224cf49a43SJulian Elischer * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE, 234cf49a43SJulian Elischer * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF 244cf49a43SJulian Elischer * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. 254cf49a43SJulian Elischer * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY 264cf49a43SJulian Elischer * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS 274cf49a43SJulian Elischer * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE. 284cf49a43SJulian Elischer * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES 294cf49a43SJulian Elischer * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING 304cf49a43SJulian Elischer * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 314cf49a43SJulian Elischer * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR 324cf49a43SJulian Elischer * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY 334cf49a43SJulian Elischer * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 344cf49a43SJulian Elischer * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 354cf49a43SJulian Elischer * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY 364cf49a43SJulian Elischer * OF SUCH DAMAGE. 374cf49a43SJulian Elischer * 38cc3bbd68SJulian Elischer * Authors: Julian Elischer <julian@freebsd.org> 39cc3bbd68SJulian Elischer * Archie Cobbs <archie@freebsd.org> 404cf49a43SJulian Elischer * 414cf49a43SJulian Elischer * $FreeBSD$ 424cf49a43SJulian Elischer * $Whistle: ng_base.c,v 1.39 1999/01/28 23:54:53 julian Exp $ 434cf49a43SJulian Elischer */ 444cf49a43SJulian Elischer 454cf49a43SJulian Elischer /* 464cf49a43SJulian Elischer * This file implements the base netgraph code. 474cf49a43SJulian Elischer */ 484cf49a43SJulian Elischer 494cf49a43SJulian Elischer #include <sys/param.h> 508e853a2cSGleb Smirnoff #include <sys/systm.h> 5177a58296SGleb Smirnoff #include <sys/ctype.h> 524cf49a43SJulian Elischer #include <sys/errno.h> 53f33ca0c9SMarcel Moolenaar #include <sys/kdb.h> 544cf49a43SJulian Elischer #include <sys/kernel.h> 553b33fbe7SGleb Smirnoff #include <sys/ktr.h> 56104a9b7eSAlexander Kabaev #include <sys/limits.h> 574cf49a43SJulian Elischer #include <sys/malloc.h> 584cf49a43SJulian Elischer #include <sys/mbuf.h> 5977a58296SGleb Smirnoff #include <sys/queue.h> 60bfa7e882SJulian Elischer #include <sys/sysctl.h> 6177a58296SGleb Smirnoff #include <sys/syslog.h> 624cf49a43SJulian Elischer 634cf49a43SJulian Elischer #include <net/netisr.h> 644cf49a43SJulian Elischer 654cf49a43SJulian Elischer #include <netgraph/ng_message.h> 664cf49a43SJulian Elischer #include <netgraph/netgraph.h> 67f8307e12SArchie Cobbs #include <netgraph/ng_parse.h> 684cf49a43SJulian Elischer 699d72a7a3SJulian Elischer MODULE_VERSION(netgraph, NG_ABI_VERSION); 7099ff8176SPeter Wemm 7130400f03SJulian Elischer /* List of all active nodes */ 72069154d5SJulian Elischer static LIST_HEAD(, ng_node) ng_nodelist; 73069154d5SJulian Elischer static struct mtx ng_nodelist_mtx; 74069154d5SJulian Elischer 75ac5dd141SGleb Smirnoff /* Mutex to protect topology events. */ 76ac5dd141SGleb Smirnoff static struct mtx ng_topo_mtx; 77ac5dd141SGleb Smirnoff 7830400f03SJulian Elischer #ifdef NETGRAPH_DEBUG 791489164fSGleb Smirnoff static struct mtx ngq_mtx; /* protects the queue item list */ 8030400f03SJulian Elischer 8130400f03SJulian Elischer static SLIST_HEAD(, ng_node) ng_allnodes; 8230400f03SJulian Elischer static LIST_HEAD(, ng_node) ng_freenodes; /* in debug, we never free() them */ 8330400f03SJulian Elischer static SLIST_HEAD(, ng_hook) ng_allhooks; 8430400f03SJulian Elischer static LIST_HEAD(, ng_hook) ng_freehooks; /* in debug, we never free() them */ 8530400f03SJulian Elischer 8630400f03SJulian Elischer static void ng_dumpitems(void); 8730400f03SJulian Elischer static void ng_dumpnodes(void); 8830400f03SJulian Elischer static void ng_dumphooks(void); 8930400f03SJulian Elischer 9030400f03SJulian Elischer #endif /* NETGRAPH_DEBUG */ 91954c4772SJulian Elischer /* 92954c4772SJulian Elischer * DEAD versions of the structures. 93954c4772SJulian Elischer * In order to avoid races, it is sometimes neccesary to point 94954c4772SJulian Elischer * at SOMETHING even though theoretically, the current entity is 95954c4772SJulian Elischer * INVALID. Use these to avoid these races. 96954c4772SJulian Elischer */ 97954c4772SJulian Elischer struct ng_type ng_deadtype = { 98954c4772SJulian Elischer NG_ABI_VERSION, 99954c4772SJulian Elischer "dead", 100954c4772SJulian Elischer NULL, /* modevent */ 101954c4772SJulian Elischer NULL, /* constructor */ 102954c4772SJulian Elischer NULL, /* rcvmsg */ 103954c4772SJulian Elischer NULL, /* shutdown */ 104954c4772SJulian Elischer NULL, /* newhook */ 105954c4772SJulian Elischer NULL, /* findhook */ 106954c4772SJulian Elischer NULL, /* connect */ 107954c4772SJulian Elischer NULL, /* rcvdata */ 108954c4772SJulian Elischer NULL, /* disconnect */ 109954c4772SJulian Elischer NULL, /* cmdlist */ 110954c4772SJulian Elischer }; 11130400f03SJulian Elischer 112954c4772SJulian Elischer struct ng_node ng_deadnode = { 113954c4772SJulian Elischer "dead", 114954c4772SJulian Elischer &ng_deadtype, 115be4252b3SJulian Elischer NGF_INVALID, 116954c4772SJulian Elischer 1, /* refs */ 117954c4772SJulian Elischer 0, /* numhooks */ 118954c4772SJulian Elischer NULL, /* private */ 119954c4772SJulian Elischer 0, /* ID */ 120954c4772SJulian Elischer LIST_HEAD_INITIALIZER(ng_deadnode.hooks), 121954c4772SJulian Elischer {}, /* all_nodes list entry */ 122954c4772SJulian Elischer {}, /* id hashtable list entry */ 123954c4772SJulian Elischer {}, /* workqueue entry */ 124954c4772SJulian Elischer { 0, 125954c4772SJulian Elischer {}, /* should never use! (should hang) */ 126954c4772SJulian Elischer NULL, 127954c4772SJulian Elischer &ng_deadnode.nd_input_queue.queue, 128954c4772SJulian Elischer &ng_deadnode 129954c4772SJulian Elischer }, 130954c4772SJulian Elischer #ifdef NETGRAPH_DEBUG 131954c4772SJulian Elischer ND_MAGIC, 132954c4772SJulian Elischer __FILE__, 133954c4772SJulian Elischer __LINE__, 134954c4772SJulian Elischer {NULL} 135954c4772SJulian Elischer #endif /* NETGRAPH_DEBUG */ 136954c4772SJulian Elischer }; 137954c4772SJulian Elischer 138954c4772SJulian Elischer struct ng_hook ng_deadhook = { 139954c4772SJulian Elischer "dead", 140954c4772SJulian Elischer NULL, /* private */ 141954c4772SJulian Elischer HK_INVALID | HK_DEAD, 142954c4772SJulian Elischer 1, /* refs always >= 1 */ 143e58d779dSGleb Smirnoff 0, /* undefined data link type */ 144954c4772SJulian Elischer &ng_deadhook, /* Peer is self */ 145954c4772SJulian Elischer &ng_deadnode, /* attached to deadnode */ 146954c4772SJulian Elischer {}, /* hooks list */ 147c4b5eea4SJulian Elischer NULL, /* override rcvmsg() */ 148c4b5eea4SJulian Elischer NULL, /* override rcvdata() */ 149954c4772SJulian Elischer #ifdef NETGRAPH_DEBUG 150954c4772SJulian Elischer HK_MAGIC, 151954c4772SJulian Elischer __FILE__, 152954c4772SJulian Elischer __LINE__, 153954c4772SJulian Elischer {NULL} 154954c4772SJulian Elischer #endif /* NETGRAPH_DEBUG */ 155954c4772SJulian Elischer }; 156954c4772SJulian Elischer 157954c4772SJulian Elischer /* 158954c4772SJulian Elischer * END DEAD STRUCTURES 159954c4772SJulian Elischer */ 160069154d5SJulian Elischer /* List nodes with unallocated work */ 161069154d5SJulian Elischer static TAILQ_HEAD(, ng_node) ng_worklist = TAILQ_HEAD_INITIALIZER(ng_worklist); 162b57a7965SJulian Elischer static struct mtx ng_worklist_mtx; /* MUST LOCK NODE FIRST */ 1634cf49a43SJulian Elischer 1644cf49a43SJulian Elischer /* List of installed types */ 165069154d5SJulian Elischer static LIST_HEAD(, ng_type) ng_typelist; 166069154d5SJulian Elischer static struct mtx ng_typelist_mtx; 1674cf49a43SJulian Elischer 168069154d5SJulian Elischer /* Hash related definitions */ 1690f150d04SJulian Elischer /* XXX Don't need to initialise them because it's a LIST */ 1700f150d04SJulian Elischer #define NG_ID_HASH_SIZE 32 /* most systems wont need even this many */ 1710f150d04SJulian Elischer static LIST_HEAD(, ng_node) ng_ID_hash[NG_ID_HASH_SIZE]; 172069154d5SJulian Elischer static struct mtx ng_idhash_mtx; 1730f150d04SJulian Elischer /* Method to find a node.. used twice so do it here */ 1740f150d04SJulian Elischer #define NG_IDHASH_FN(ID) ((ID) % (NG_ID_HASH_SIZE)) 1750f150d04SJulian Elischer #define NG_IDHASH_FIND(ID, node) \ 1760f150d04SJulian Elischer do { \ 17753f9c5e9SRobert Watson mtx_assert(&ng_idhash_mtx, MA_OWNED); \ 1780f150d04SJulian Elischer LIST_FOREACH(node, &ng_ID_hash[NG_IDHASH_FN(ID)], \ 1790f150d04SJulian Elischer nd_idnodes) { \ 1800f150d04SJulian Elischer if (NG_NODE_IS_VALID(node) \ 1810f150d04SJulian Elischer && (NG_NODE_ID(node) == ID)) { \ 1820f150d04SJulian Elischer break; \ 1830f150d04SJulian Elischer } \ 1840f150d04SJulian Elischer } \ 1850f150d04SJulian Elischer } while (0) 186069154d5SJulian Elischer 187dc90cad9SJulian Elischer 1884cf49a43SJulian Elischer /* Internal functions */ 1894cf49a43SJulian Elischer static int ng_add_hook(node_p node, const char *name, hook_p * hookp); 190069154d5SJulian Elischer static int ng_generic_msg(node_p here, item_p item, hook_p lasthook); 191dc90cad9SJulian Elischer static ng_ID_t ng_decodeidname(const char *name); 1924cf49a43SJulian Elischer static int ngb_mod_event(module_t mod, int event, void *data); 193069154d5SJulian Elischer static void ng_worklist_remove(node_p node); 1944cf49a43SJulian Elischer static void ngintr(void); 195262dfd7fSJulian Elischer static void ng_apply_item(node_p node, item_p item, int rw); 196069154d5SJulian Elischer static void ng_flush_input_queue(struct ng_queue * ngq); 197069154d5SJulian Elischer static void ng_setisr(node_p node); 198069154d5SJulian Elischer static node_p ng_ID2noderef(ng_ID_t ID); 1996b795970SJulian Elischer static int ng_con_nodes(node_p node, const char *name, node_p node2, 2006b795970SJulian Elischer const char *name2); 2011acb27c6SJulian Elischer static void ng_con_part2(node_p node, hook_p hook, void *arg1, int arg2); 2021acb27c6SJulian Elischer static void ng_con_part3(node_p node, hook_p hook, void *arg1, int arg2); 2036b795970SJulian Elischer static int ng_mkpeer(node_p node, const char *name, 2046b795970SJulian Elischer const char *name2, char *type); 205069154d5SJulian Elischer 2064c9b5910SGleb Smirnoff /* Imported, these used to be externally visible, some may go back. */ 207069154d5SJulian Elischer void ng_destroy_hook(hook_p hook); 208069154d5SJulian Elischer node_p ng_name2noderef(node_p node, const char *name); 209069154d5SJulian Elischer int ng_path2noderef(node_p here, const char *path, 210069154d5SJulian Elischer node_p *dest, hook_p *lasthook); 211069154d5SJulian Elischer int ng_make_node(const char *type, node_p *nodepp); 212069154d5SJulian Elischer int ng_path_parse(char *addr, char **node, char **path, char **hook); 2131acb27c6SJulian Elischer void ng_rmnode(node_p node, hook_p dummy1, void *dummy2, int dummy3); 21430400f03SJulian Elischer void ng_unname(node_p node); 215069154d5SJulian Elischer 2164cf49a43SJulian Elischer 2174cf49a43SJulian Elischer /* Our own netgraph malloc type */ 2184cf49a43SJulian Elischer MALLOC_DEFINE(M_NETGRAPH, "netgraph", "netgraph structures and ctrl messages"); 219069154d5SJulian Elischer MALLOC_DEFINE(M_NETGRAPH_HOOK, "netgraph_hook", "netgraph hook structures"); 220069154d5SJulian Elischer MALLOC_DEFINE(M_NETGRAPH_NODE, "netgraph_node", "netgraph node structures"); 221069154d5SJulian Elischer MALLOC_DEFINE(M_NETGRAPH_ITEM, "netgraph_item", "netgraph item structures"); 222069154d5SJulian Elischer MALLOC_DEFINE(M_NETGRAPH_MSG, "netgraph_msg", "netgraph name storage"); 223069154d5SJulian Elischer 224069154d5SJulian Elischer /* Should not be visible outside this file */ 22530400f03SJulian Elischer 22630400f03SJulian Elischer #define _NG_ALLOC_HOOK(hook) \ 22730400f03SJulian Elischer MALLOC(hook, hook_p, sizeof(*hook), M_NETGRAPH_HOOK, M_NOWAIT | M_ZERO) 22830400f03SJulian Elischer #define _NG_ALLOC_NODE(node) \ 22930400f03SJulian Elischer MALLOC(node, node_p, sizeof(*node), M_NETGRAPH_NODE, M_NOWAIT | M_ZERO) 23030400f03SJulian Elischer 2312c8dda8dSWojciech A. Koszek #define NG_QUEUE_LOCK_INIT(n) \ 2322c8dda8dSWojciech A. Koszek mtx_init(&(n)->q_mtx, "ng_node", NULL, MTX_SPIN) 2332c8dda8dSWojciech A. Koszek #define NG_QUEUE_LOCK(n) \ 2342c8dda8dSWojciech A. Koszek mtx_lock_spin(&(n)->q_mtx) 2352c8dda8dSWojciech A. Koszek #define NG_QUEUE_UNLOCK(n) \ 2362c8dda8dSWojciech A. Koszek mtx_unlock_spin(&(n)->q_mtx) 2372c8dda8dSWojciech A. Koszek #define NG_WORKLIST_LOCK_INIT() \ 2382c8dda8dSWojciech A. Koszek mtx_init(&ng_worklist_mtx, "ng_worklist", NULL, MTX_SPIN) 2392c8dda8dSWojciech A. Koszek #define NG_WORKLIST_LOCK() \ 2402c8dda8dSWojciech A. Koszek mtx_lock_spin(&ng_worklist_mtx) 2412c8dda8dSWojciech A. Koszek #define NG_WORKLIST_UNLOCK() \ 2422c8dda8dSWojciech A. Koszek mtx_unlock_spin(&ng_worklist_mtx) 2432c8dda8dSWojciech A. Koszek 24430400f03SJulian Elischer #ifdef NETGRAPH_DEBUG /*----------------------------------------------*/ 24530400f03SJulian Elischer /* 24630400f03SJulian Elischer * In debug mode: 24730400f03SJulian Elischer * In an attempt to help track reference count screwups 24830400f03SJulian Elischer * we do not free objects back to the malloc system, but keep them 24930400f03SJulian Elischer * in a local cache where we can examine them and keep information safely 25030400f03SJulian Elischer * after they have been freed. 25130400f03SJulian Elischer * We use this scheme for nodes and hooks, and to some extent for items. 25230400f03SJulian Elischer */ 25330400f03SJulian Elischer static __inline hook_p 25430400f03SJulian Elischer ng_alloc_hook(void) 25530400f03SJulian Elischer { 25630400f03SJulian Elischer hook_p hook; 25730400f03SJulian Elischer SLIST_ENTRY(ng_hook) temp; 2589ed346baSBosko Milekic mtx_lock(&ng_nodelist_mtx); 25930400f03SJulian Elischer hook = LIST_FIRST(&ng_freehooks); 26030400f03SJulian Elischer if (hook) { 26130400f03SJulian Elischer LIST_REMOVE(hook, hk_hooks); 26230400f03SJulian Elischer bcopy(&hook->hk_all, &temp, sizeof(temp)); 26330400f03SJulian Elischer bzero(hook, sizeof(struct ng_hook)); 26430400f03SJulian Elischer bcopy(&temp, &hook->hk_all, sizeof(temp)); 2659ed346baSBosko Milekic mtx_unlock(&ng_nodelist_mtx); 26630400f03SJulian Elischer hook->hk_magic = HK_MAGIC; 26730400f03SJulian Elischer } else { 2689ed346baSBosko Milekic mtx_unlock(&ng_nodelist_mtx); 26930400f03SJulian Elischer _NG_ALLOC_HOOK(hook); 27030400f03SJulian Elischer if (hook) { 27130400f03SJulian Elischer hook->hk_magic = HK_MAGIC; 2729ed346baSBosko Milekic mtx_lock(&ng_nodelist_mtx); 27330400f03SJulian Elischer SLIST_INSERT_HEAD(&ng_allhooks, hook, hk_all); 2749ed346baSBosko Milekic mtx_unlock(&ng_nodelist_mtx); 27530400f03SJulian Elischer } 27630400f03SJulian Elischer } 27730400f03SJulian Elischer return (hook); 27830400f03SJulian Elischer } 27930400f03SJulian Elischer 28030400f03SJulian Elischer static __inline node_p 28130400f03SJulian Elischer ng_alloc_node(void) 28230400f03SJulian Elischer { 28330400f03SJulian Elischer node_p node; 28430400f03SJulian Elischer SLIST_ENTRY(ng_node) temp; 2859ed346baSBosko Milekic mtx_lock(&ng_nodelist_mtx); 28630400f03SJulian Elischer node = LIST_FIRST(&ng_freenodes); 28730400f03SJulian Elischer if (node) { 28830400f03SJulian Elischer LIST_REMOVE(node, nd_nodes); 28930400f03SJulian Elischer bcopy(&node->nd_all, &temp, sizeof(temp)); 29030400f03SJulian Elischer bzero(node, sizeof(struct ng_node)); 29130400f03SJulian Elischer bcopy(&temp, &node->nd_all, sizeof(temp)); 2929ed346baSBosko Milekic mtx_unlock(&ng_nodelist_mtx); 29330400f03SJulian Elischer node->nd_magic = ND_MAGIC; 29430400f03SJulian Elischer } else { 2959ed346baSBosko Milekic mtx_unlock(&ng_nodelist_mtx); 29630400f03SJulian Elischer _NG_ALLOC_NODE(node); 29730400f03SJulian Elischer if (node) { 29830400f03SJulian Elischer node->nd_magic = ND_MAGIC; 2999ed346baSBosko Milekic mtx_lock(&ng_nodelist_mtx); 30030400f03SJulian Elischer SLIST_INSERT_HEAD(&ng_allnodes, node, nd_all); 3019ed346baSBosko Milekic mtx_unlock(&ng_nodelist_mtx); 30230400f03SJulian Elischer } 30330400f03SJulian Elischer } 30430400f03SJulian Elischer return (node); 30530400f03SJulian Elischer } 30630400f03SJulian Elischer 30730400f03SJulian Elischer #define NG_ALLOC_HOOK(hook) do { (hook) = ng_alloc_hook(); } while (0) 30830400f03SJulian Elischer #define NG_ALLOC_NODE(node) do { (node) = ng_alloc_node(); } while (0) 30930400f03SJulian Elischer 31030400f03SJulian Elischer 31130400f03SJulian Elischer #define NG_FREE_HOOK(hook) \ 31230400f03SJulian Elischer do { \ 3139ed346baSBosko Milekic mtx_lock(&ng_nodelist_mtx); \ 31430400f03SJulian Elischer LIST_INSERT_HEAD(&ng_freehooks, hook, hk_hooks); \ 31530400f03SJulian Elischer hook->hk_magic = 0; \ 3169ed346baSBosko Milekic mtx_unlock(&ng_nodelist_mtx); \ 31730400f03SJulian Elischer } while (0) 31830400f03SJulian Elischer 31930400f03SJulian Elischer #define NG_FREE_NODE(node) \ 32030400f03SJulian Elischer do { \ 3219ed346baSBosko Milekic mtx_lock(&ng_nodelist_mtx); \ 32230400f03SJulian Elischer LIST_INSERT_HEAD(&ng_freenodes, node, nd_nodes); \ 32330400f03SJulian Elischer node->nd_magic = 0; \ 3249ed346baSBosko Milekic mtx_unlock(&ng_nodelist_mtx); \ 32530400f03SJulian Elischer } while (0) 32630400f03SJulian Elischer 32730400f03SJulian Elischer #else /* NETGRAPH_DEBUG */ /*----------------------------------------------*/ 32830400f03SJulian Elischer 32930400f03SJulian Elischer #define NG_ALLOC_HOOK(hook) _NG_ALLOC_HOOK(hook) 33030400f03SJulian Elischer #define NG_ALLOC_NODE(node) _NG_ALLOC_NODE(node) 33130400f03SJulian Elischer 332069154d5SJulian Elischer #define NG_FREE_HOOK(hook) do { FREE((hook), M_NETGRAPH_HOOK); } while (0) 333069154d5SJulian Elischer #define NG_FREE_NODE(node) do { FREE((node), M_NETGRAPH_NODE); } while (0) 33430400f03SJulian Elischer 33530400f03SJulian Elischer #endif /* NETGRAPH_DEBUG */ /*----------------------------------------------*/ 33630400f03SJulian Elischer 337f33ca0c9SMarcel Moolenaar /* Set this to kdb_enter("X") to catch all errors as they occur */ 3384cf49a43SJulian Elischer #ifndef TRAP_ERROR 3396b795970SJulian Elischer #define TRAP_ERROR() 3404cf49a43SJulian Elischer #endif 3414cf49a43SJulian Elischer 342dc90cad9SJulian Elischer static ng_ID_t nextID = 1; 343dc90cad9SJulian Elischer 344b2da83c2SArchie Cobbs #ifdef INVARIANTS 345b2da83c2SArchie Cobbs #define CHECK_DATA_MBUF(m) do { \ 346b2da83c2SArchie Cobbs struct mbuf *n; \ 347b2da83c2SArchie Cobbs int total; \ 348b2da83c2SArchie Cobbs \ 349fe584538SDag-Erling Smørgrav M_ASSERTPKTHDR(m); \ 350b32cfb32SGleb Smirnoff for (total = 0, n = (m); n != NULL; n = n->m_next) { \ 351b2da83c2SArchie Cobbs total += n->m_len; \ 352b32cfb32SGleb Smirnoff if (n->m_nextpkt != NULL) \ 353b32cfb32SGleb Smirnoff panic("%s: m_nextpkt", __func__); \ 354b32cfb32SGleb Smirnoff } \ 355ba5b359aSGleb Smirnoff \ 356b2da83c2SArchie Cobbs if ((m)->m_pkthdr.len != total) { \ 357b2da83c2SArchie Cobbs panic("%s: %d != %d", \ 3586e551fb6SDavid E. O'Brien __func__, (m)->m_pkthdr.len, total); \ 359b2da83c2SArchie Cobbs } \ 360b2da83c2SArchie Cobbs } while (0) 361b2da83c2SArchie Cobbs #else 362b2da83c2SArchie Cobbs #define CHECK_DATA_MBUF(m) 363b2da83c2SArchie Cobbs #endif 364b2da83c2SArchie Cobbs 365dc90cad9SJulian Elischer 3664cf49a43SJulian Elischer /************************************************************************ 367f8307e12SArchie Cobbs Parse type definitions for generic messages 368f8307e12SArchie Cobbs ************************************************************************/ 369f8307e12SArchie Cobbs 370f8307e12SArchie Cobbs /* Handy structure parse type defining macro */ 371f8307e12SArchie Cobbs #define DEFINE_PARSE_STRUCT_TYPE(lo, up, args) \ 372f0184ff8SArchie Cobbs static const struct ng_parse_struct_field \ 373f0184ff8SArchie Cobbs ng_ ## lo ## _type_fields[] = NG_GENERIC_ ## up ## _INFO args; \ 374f8307e12SArchie Cobbs static const struct ng_parse_type ng_generic_ ## lo ## _type = { \ 375f8307e12SArchie Cobbs &ng_parse_struct_type, \ 376f0184ff8SArchie Cobbs &ng_ ## lo ## _type_fields \ 377f8307e12SArchie Cobbs } 378f8307e12SArchie Cobbs 379f8307e12SArchie Cobbs DEFINE_PARSE_STRUCT_TYPE(mkpeer, MKPEER, ()); 380f8307e12SArchie Cobbs DEFINE_PARSE_STRUCT_TYPE(connect, CONNECT, ()); 381f8307e12SArchie Cobbs DEFINE_PARSE_STRUCT_TYPE(name, NAME, ()); 382f8307e12SArchie Cobbs DEFINE_PARSE_STRUCT_TYPE(rmhook, RMHOOK, ()); 383f8307e12SArchie Cobbs DEFINE_PARSE_STRUCT_TYPE(nodeinfo, NODEINFO, ()); 384f8307e12SArchie Cobbs DEFINE_PARSE_STRUCT_TYPE(typeinfo, TYPEINFO, ()); 385f8307e12SArchie Cobbs DEFINE_PARSE_STRUCT_TYPE(linkinfo, LINKINFO, (&ng_generic_nodeinfo_type)); 386f8307e12SArchie Cobbs 387f8307e12SArchie Cobbs /* Get length of an array when the length is stored as a 32 bit 388d7d97eb0SJeroen Ruigrok van der Werven value immediately preceding the array -- as with struct namelist 389f8307e12SArchie Cobbs and struct typelist. */ 390f8307e12SArchie Cobbs static int 391f8307e12SArchie Cobbs ng_generic_list_getLength(const struct ng_parse_type *type, 392f8307e12SArchie Cobbs const u_char *start, const u_char *buf) 393f8307e12SArchie Cobbs { 394f8307e12SArchie Cobbs return *((const u_int32_t *)(buf - 4)); 395f8307e12SArchie Cobbs } 396f8307e12SArchie Cobbs 397f8307e12SArchie Cobbs /* Get length of the array of struct linkinfo inside a struct hooklist */ 398f8307e12SArchie Cobbs static int 399f8307e12SArchie Cobbs ng_generic_linkinfo_getLength(const struct ng_parse_type *type, 400f8307e12SArchie Cobbs const u_char *start, const u_char *buf) 401f8307e12SArchie Cobbs { 402f8307e12SArchie Cobbs const struct hooklist *hl = (const struct hooklist *)start; 403f8307e12SArchie Cobbs 404f8307e12SArchie Cobbs return hl->nodeinfo.hooks; 405f8307e12SArchie Cobbs } 406f8307e12SArchie Cobbs 407f8307e12SArchie Cobbs /* Array type for a variable length array of struct namelist */ 408f8307e12SArchie Cobbs static const struct ng_parse_array_info ng_nodeinfoarray_type_info = { 409f8307e12SArchie Cobbs &ng_generic_nodeinfo_type, 410f8307e12SArchie Cobbs &ng_generic_list_getLength 411f8307e12SArchie Cobbs }; 412f8307e12SArchie Cobbs static const struct ng_parse_type ng_generic_nodeinfoarray_type = { 413f8307e12SArchie Cobbs &ng_parse_array_type, 414f8307e12SArchie Cobbs &ng_nodeinfoarray_type_info 415f8307e12SArchie Cobbs }; 416f8307e12SArchie Cobbs 417f8307e12SArchie Cobbs /* Array type for a variable length array of struct typelist */ 418f8307e12SArchie Cobbs static const struct ng_parse_array_info ng_typeinfoarray_type_info = { 419f8307e12SArchie Cobbs &ng_generic_typeinfo_type, 420f8307e12SArchie Cobbs &ng_generic_list_getLength 421f8307e12SArchie Cobbs }; 422f8307e12SArchie Cobbs static const struct ng_parse_type ng_generic_typeinfoarray_type = { 423f8307e12SArchie Cobbs &ng_parse_array_type, 424f8307e12SArchie Cobbs &ng_typeinfoarray_type_info 425f8307e12SArchie Cobbs }; 426f8307e12SArchie Cobbs 427f8307e12SArchie Cobbs /* Array type for array of struct linkinfo in struct hooklist */ 428f8307e12SArchie Cobbs static const struct ng_parse_array_info ng_generic_linkinfo_array_type_info = { 429f8307e12SArchie Cobbs &ng_generic_linkinfo_type, 430f8307e12SArchie Cobbs &ng_generic_linkinfo_getLength 431f8307e12SArchie Cobbs }; 432f8307e12SArchie Cobbs static const struct ng_parse_type ng_generic_linkinfo_array_type = { 433f8307e12SArchie Cobbs &ng_parse_array_type, 434f8307e12SArchie Cobbs &ng_generic_linkinfo_array_type_info 435f8307e12SArchie Cobbs }; 436f8307e12SArchie Cobbs 437f8307e12SArchie Cobbs DEFINE_PARSE_STRUCT_TYPE(typelist, TYPELIST, (&ng_generic_nodeinfoarray_type)); 438f8307e12SArchie Cobbs DEFINE_PARSE_STRUCT_TYPE(hooklist, HOOKLIST, 439f8307e12SArchie Cobbs (&ng_generic_nodeinfo_type, &ng_generic_linkinfo_array_type)); 440f8307e12SArchie Cobbs DEFINE_PARSE_STRUCT_TYPE(listnodes, LISTNODES, 441f8307e12SArchie Cobbs (&ng_generic_nodeinfoarray_type)); 442f8307e12SArchie Cobbs 443f8307e12SArchie Cobbs /* List of commands and how to convert arguments to/from ASCII */ 444f8307e12SArchie Cobbs static const struct ng_cmdlist ng_generic_cmds[] = { 445f8307e12SArchie Cobbs { 446f8307e12SArchie Cobbs NGM_GENERIC_COOKIE, 447f8307e12SArchie Cobbs NGM_SHUTDOWN, 448f8307e12SArchie Cobbs "shutdown", 449f8307e12SArchie Cobbs NULL, 450f8307e12SArchie Cobbs NULL 451f8307e12SArchie Cobbs }, 452f8307e12SArchie Cobbs { 453f8307e12SArchie Cobbs NGM_GENERIC_COOKIE, 454f8307e12SArchie Cobbs NGM_MKPEER, 455f8307e12SArchie Cobbs "mkpeer", 456f8307e12SArchie Cobbs &ng_generic_mkpeer_type, 457f8307e12SArchie Cobbs NULL 458f8307e12SArchie Cobbs }, 459f8307e12SArchie Cobbs { 460f8307e12SArchie Cobbs NGM_GENERIC_COOKIE, 461f8307e12SArchie Cobbs NGM_CONNECT, 462f8307e12SArchie Cobbs "connect", 463f8307e12SArchie Cobbs &ng_generic_connect_type, 464f8307e12SArchie Cobbs NULL 465f8307e12SArchie Cobbs }, 466f8307e12SArchie Cobbs { 467f8307e12SArchie Cobbs NGM_GENERIC_COOKIE, 468f8307e12SArchie Cobbs NGM_NAME, 469f8307e12SArchie Cobbs "name", 470f8307e12SArchie Cobbs &ng_generic_name_type, 471f8307e12SArchie Cobbs NULL 472f8307e12SArchie Cobbs }, 473f8307e12SArchie Cobbs { 474f8307e12SArchie Cobbs NGM_GENERIC_COOKIE, 475f8307e12SArchie Cobbs NGM_RMHOOK, 476f8307e12SArchie Cobbs "rmhook", 477f8307e12SArchie Cobbs &ng_generic_rmhook_type, 478f8307e12SArchie Cobbs NULL 479f8307e12SArchie Cobbs }, 480f8307e12SArchie Cobbs { 481f8307e12SArchie Cobbs NGM_GENERIC_COOKIE, 482f8307e12SArchie Cobbs NGM_NODEINFO, 483f8307e12SArchie Cobbs "nodeinfo", 484f8307e12SArchie Cobbs NULL, 485f8307e12SArchie Cobbs &ng_generic_nodeinfo_type 486f8307e12SArchie Cobbs }, 487f8307e12SArchie Cobbs { 488f8307e12SArchie Cobbs NGM_GENERIC_COOKIE, 489f8307e12SArchie Cobbs NGM_LISTHOOKS, 490f8307e12SArchie Cobbs "listhooks", 491f8307e12SArchie Cobbs NULL, 492f8307e12SArchie Cobbs &ng_generic_hooklist_type 493f8307e12SArchie Cobbs }, 494f8307e12SArchie Cobbs { 495f8307e12SArchie Cobbs NGM_GENERIC_COOKIE, 496f8307e12SArchie Cobbs NGM_LISTNAMES, 497f8307e12SArchie Cobbs "listnames", 498f8307e12SArchie Cobbs NULL, 499f8307e12SArchie Cobbs &ng_generic_listnodes_type /* same as NGM_LISTNODES */ 500f8307e12SArchie Cobbs }, 501f8307e12SArchie Cobbs { 502f8307e12SArchie Cobbs NGM_GENERIC_COOKIE, 503f8307e12SArchie Cobbs NGM_LISTNODES, 504f8307e12SArchie Cobbs "listnodes", 505f8307e12SArchie Cobbs NULL, 506f8307e12SArchie Cobbs &ng_generic_listnodes_type 507f8307e12SArchie Cobbs }, 508f8307e12SArchie Cobbs { 509f8307e12SArchie Cobbs NGM_GENERIC_COOKIE, 510f8307e12SArchie Cobbs NGM_LISTTYPES, 511f8307e12SArchie Cobbs "listtypes", 512f8307e12SArchie Cobbs NULL, 513f8307e12SArchie Cobbs &ng_generic_typeinfo_type 514f8307e12SArchie Cobbs }, 515f8307e12SArchie Cobbs { 516f8307e12SArchie Cobbs NGM_GENERIC_COOKIE, 5177095e097SPoul-Henning Kamp NGM_TEXT_CONFIG, 5187095e097SPoul-Henning Kamp "textconfig", 5197095e097SPoul-Henning Kamp NULL, 5207095e097SPoul-Henning Kamp &ng_parse_string_type 5217095e097SPoul-Henning Kamp }, 5227095e097SPoul-Henning Kamp { 5237095e097SPoul-Henning Kamp NGM_GENERIC_COOKIE, 524f8307e12SArchie Cobbs NGM_TEXT_STATUS, 525f8307e12SArchie Cobbs "textstatus", 526f8307e12SArchie Cobbs NULL, 527f8307e12SArchie Cobbs &ng_parse_string_type 528f8307e12SArchie Cobbs }, 529f8307e12SArchie Cobbs { 530f8307e12SArchie Cobbs NGM_GENERIC_COOKIE, 531f8307e12SArchie Cobbs NGM_ASCII2BINARY, 532f8307e12SArchie Cobbs "ascii2binary", 533f8307e12SArchie Cobbs &ng_parse_ng_mesg_type, 534f8307e12SArchie Cobbs &ng_parse_ng_mesg_type 535f8307e12SArchie Cobbs }, 536f8307e12SArchie Cobbs { 537f8307e12SArchie Cobbs NGM_GENERIC_COOKIE, 538f8307e12SArchie Cobbs NGM_BINARY2ASCII, 539f8307e12SArchie Cobbs "binary2ascii", 540f8307e12SArchie Cobbs &ng_parse_ng_mesg_type, 541f8307e12SArchie Cobbs &ng_parse_ng_mesg_type 542f8307e12SArchie Cobbs }, 543f8307e12SArchie Cobbs { 0 } 544f8307e12SArchie Cobbs }; 545f8307e12SArchie Cobbs 546f8307e12SArchie Cobbs /************************************************************************ 5474cf49a43SJulian Elischer Node routines 5484cf49a43SJulian Elischer ************************************************************************/ 5494cf49a43SJulian Elischer 5504cf49a43SJulian Elischer /* 5514cf49a43SJulian Elischer * Instantiate a node of the requested type 5524cf49a43SJulian Elischer */ 5534cf49a43SJulian Elischer int 5544cf49a43SJulian Elischer ng_make_node(const char *typename, node_p *nodepp) 5554cf49a43SJulian Elischer { 5564cf49a43SJulian Elischer struct ng_type *type; 557069154d5SJulian Elischer int error; 5584cf49a43SJulian Elischer 5594cf49a43SJulian Elischer /* Check that the type makes sense */ 5604cf49a43SJulian Elischer if (typename == NULL) { 5616b795970SJulian Elischer TRAP_ERROR(); 5624cf49a43SJulian Elischer return (EINVAL); 5634cf49a43SJulian Elischer } 5644cf49a43SJulian Elischer 5657610f574SGleb Smirnoff /* Locate the node type. If we fail we return. Do not try to load 5667610f574SGleb Smirnoff * module. 5677610f574SGleb Smirnoff */ 5684cf49a43SJulian Elischer if ((type = ng_findtype(typename)) == NULL) 5694cf49a43SJulian Elischer return (ENXIO); 5704cf49a43SJulian Elischer 571069154d5SJulian Elischer /* 572069154d5SJulian Elischer * If we have a constructor, then make the node and 573069154d5SJulian Elischer * call the constructor to do type specific initialisation. 574069154d5SJulian Elischer */ 575069154d5SJulian Elischer if (type->constructor != NULL) { 576069154d5SJulian Elischer if ((error = ng_make_node_common(type, nodepp)) == 0) { 577069154d5SJulian Elischer if ((error = ((*type->constructor)(*nodepp)) != 0)) { 57830400f03SJulian Elischer NG_NODE_UNREF(*nodepp); 579069154d5SJulian Elischer } 580069154d5SJulian Elischer } 581069154d5SJulian Elischer } else { 582069154d5SJulian Elischer /* 583069154d5SJulian Elischer * Node has no constructor. We cannot ask for one 58464efc707SRobert Watson * to be made. It must be brought into existence by 585954c4772SJulian Elischer * some external agency. The external agency should 586069154d5SJulian Elischer * call ng_make_node_common() directly to get the 587069154d5SJulian Elischer * netgraph part initialised. 588069154d5SJulian Elischer */ 5896b795970SJulian Elischer TRAP_ERROR(); 590069154d5SJulian Elischer error = EINVAL; 591069154d5SJulian Elischer } 592069154d5SJulian Elischer return (error); 5934cf49a43SJulian Elischer } 5944cf49a43SJulian Elischer 5954cf49a43SJulian Elischer /* 596069154d5SJulian Elischer * Generic node creation. Called by node initialisation for externally 597069154d5SJulian Elischer * instantiated nodes (e.g. hardware, sockets, etc ). 5984cf49a43SJulian Elischer * The returned node has a reference count of 1. 5994cf49a43SJulian Elischer */ 6004cf49a43SJulian Elischer int 6014cf49a43SJulian Elischer ng_make_node_common(struct ng_type *type, node_p *nodepp) 6024cf49a43SJulian Elischer { 6034cf49a43SJulian Elischer node_p node; 6044cf49a43SJulian Elischer 6054cf49a43SJulian Elischer /* Require the node type to have been already installed */ 6064cf49a43SJulian Elischer if (ng_findtype(type->name) == NULL) { 6076b795970SJulian Elischer TRAP_ERROR(); 6084cf49a43SJulian Elischer return (EINVAL); 6094cf49a43SJulian Elischer } 6104cf49a43SJulian Elischer 6114cf49a43SJulian Elischer /* Make a node and try attach it to the type */ 61230400f03SJulian Elischer NG_ALLOC_NODE(node); 6134cf49a43SJulian Elischer if (node == NULL) { 6146b795970SJulian Elischer TRAP_ERROR(); 6154cf49a43SJulian Elischer return (ENOMEM); 6164cf49a43SJulian Elischer } 61730400f03SJulian Elischer node->nd_type = type; 61830400f03SJulian Elischer NG_NODE_REF(node); /* note reference */ 6194cf49a43SJulian Elischer type->refs++; 6204cf49a43SJulian Elischer 6212c8dda8dSWojciech A. Koszek NG_QUEUE_LOCK_INIT(&node->nd_input_queue); 62230400f03SJulian Elischer node->nd_input_queue.queue = NULL; 62330400f03SJulian Elischer node->nd_input_queue.last = &node->nd_input_queue.queue; 62430400f03SJulian Elischer node->nd_input_queue.q_flags = 0; 62530400f03SJulian Elischer node->nd_input_queue.q_node = node; 6264cf49a43SJulian Elischer 6274cf49a43SJulian Elischer /* Initialize hook list for new node */ 62830400f03SJulian Elischer LIST_INIT(&node->nd_hooks); 6294cf49a43SJulian Elischer 630069154d5SJulian Elischer /* Link us into the node linked list */ 6319ed346baSBosko Milekic mtx_lock(&ng_nodelist_mtx); 63230400f03SJulian Elischer LIST_INSERT_HEAD(&ng_nodelist, node, nd_nodes); 6339ed346baSBosko Milekic mtx_unlock(&ng_nodelist_mtx); 634069154d5SJulian Elischer 635069154d5SJulian Elischer 636dc90cad9SJulian Elischer /* get an ID and put us in the hash chain */ 6379ed346baSBosko Milekic mtx_lock(&ng_idhash_mtx); 63830400f03SJulian Elischer for (;;) { /* wrap protection, even if silly */ 639069154d5SJulian Elischer node_p node2 = NULL; 64030400f03SJulian Elischer node->nd_ID = nextID++; /* 137/second for 1 year before wrap */ 6410f150d04SJulian Elischer 64230400f03SJulian Elischer /* Is there a problem with the new number? */ 6430f150d04SJulian Elischer NG_IDHASH_FIND(node->nd_ID, node2); /* already taken? */ 6440f150d04SJulian Elischer if ((node->nd_ID != 0) && (node2 == NULL)) { 64530400f03SJulian Elischer break; 646069154d5SJulian Elischer } 64730400f03SJulian Elischer } 6480f150d04SJulian Elischer LIST_INSERT_HEAD(&ng_ID_hash[NG_IDHASH_FN(node->nd_ID)], 64930400f03SJulian Elischer node, nd_idnodes); 6509ed346baSBosko Milekic mtx_unlock(&ng_idhash_mtx); 651dc90cad9SJulian Elischer 6524cf49a43SJulian Elischer /* Done */ 6534cf49a43SJulian Elischer *nodepp = node; 6544cf49a43SJulian Elischer return (0); 6554cf49a43SJulian Elischer } 6564cf49a43SJulian Elischer 6574cf49a43SJulian Elischer /* 6584cf49a43SJulian Elischer * Forceably start the shutdown process on a node. Either call 65964efc707SRobert Watson * its shutdown method, or do the default shutdown if there is 6604cf49a43SJulian Elischer * no type-specific method. 6614cf49a43SJulian Elischer * 66264efc707SRobert Watson * We can only be called from a shutdown message, so we know we have 6633e4084c8SJulian Elischer * a writer lock, and therefore exclusive access. It also means 6643e4084c8SJulian Elischer * that we should not be on the work queue, but we check anyhow. 665069154d5SJulian Elischer * 666069154d5SJulian Elischer * Persistent node types must have a type-specific method which 66764efc707SRobert Watson * allocates a new node in which case, this one is irretrievably going away, 6683e4084c8SJulian Elischer * or cleans up anything it needs, and just makes the node valid again, 6693e4084c8SJulian Elischer * in which case we allow the node to survive. 6703e4084c8SJulian Elischer * 67164efc707SRobert Watson * XXX We need to think of how to tell a persistent node that we 6723e4084c8SJulian Elischer * REALLY need to go away because the hardware has gone or we 6733e4084c8SJulian Elischer * are rebooting.... etc. 6744cf49a43SJulian Elischer */ 6754cf49a43SJulian Elischer void 6761acb27c6SJulian Elischer ng_rmnode(node_p node, hook_p dummy1, void *dummy2, int dummy3) 6774cf49a43SJulian Elischer { 6783e4084c8SJulian Elischer hook_p hook; 6793e4084c8SJulian Elischer 6804cf49a43SJulian Elischer /* Check if it's already shutting down */ 681be4252b3SJulian Elischer if ((node->nd_flags & NGF_CLOSING) != 0) 6824cf49a43SJulian Elischer return; 6834cf49a43SJulian Elischer 6841acb27c6SJulian Elischer if (node == &ng_deadnode) { 6851acb27c6SJulian Elischer printf ("shutdown called on deadnode\n"); 6861acb27c6SJulian Elischer return; 6871acb27c6SJulian Elischer } 6881acb27c6SJulian Elischer 6894cf49a43SJulian Elischer /* Add an extra reference so it doesn't go away during this */ 69030400f03SJulian Elischer NG_NODE_REF(node); 6914cf49a43SJulian Elischer 69230400f03SJulian Elischer /* 69330400f03SJulian Elischer * Mark it invalid so any newcomers know not to try use it 69430400f03SJulian Elischer * Also add our own mark so we can't recurse 695be4252b3SJulian Elischer * note that NGF_INVALID does not do this as it's also set during 69630400f03SJulian Elischer * creation 69730400f03SJulian Elischer */ 698be4252b3SJulian Elischer node->nd_flags |= NGF_INVALID|NGF_CLOSING; 6994cf49a43SJulian Elischer 700991fc65aSJulian Elischer /* If node has its pre-shutdown method, then call it first*/ 701991fc65aSJulian Elischer if (node->nd_type && node->nd_type->close) 702991fc65aSJulian Elischer (*node->nd_type->close)(node); 703991fc65aSJulian Elischer 7043e4084c8SJulian Elischer /* Notify all remaining connected nodes to disconnect */ 7053e4084c8SJulian Elischer while ((hook = LIST_FIRST(&node->nd_hooks)) != NULL) 7063e4084c8SJulian Elischer ng_destroy_hook(hook); 70730400f03SJulian Elischer 708069154d5SJulian Elischer /* 709069154d5SJulian Elischer * Drain the input queue forceably. 71030400f03SJulian Elischer * it has no hooks so what's it going to do, bleed on someone? 71130400f03SJulian Elischer * Theoretically we came here from a queue entry that was added 71230400f03SJulian Elischer * Just before the queue was closed, so it should be empty anyway. 713b57a7965SJulian Elischer * Also removes us from worklist if needed. 714069154d5SJulian Elischer */ 71530400f03SJulian Elischer ng_flush_input_queue(&node->nd_input_queue); 716069154d5SJulian Elischer 717069154d5SJulian Elischer /* Ask the type if it has anything to do in this case */ 71830400f03SJulian Elischer if (node->nd_type && node->nd_type->shutdown) { 71930400f03SJulian Elischer (*node->nd_type->shutdown)(node); 72030400f03SJulian Elischer if (NG_NODE_IS_VALID(node)) { 72130400f03SJulian Elischer /* 72230400f03SJulian Elischer * Well, blow me down if the node code hasn't declared 72330400f03SJulian Elischer * that it doesn't want to die. 72430400f03SJulian Elischer * Presumably it is a persistant node. 7251acb27c6SJulian Elischer * If we REALLY want it to go away, 7261acb27c6SJulian Elischer * e.g. hardware going away, 727be4252b3SJulian Elischer * Our caller should set NGF_REALLY_DIE in nd_flags. 72830400f03SJulian Elischer */ 729be4252b3SJulian Elischer node->nd_flags &= ~(NGF_INVALID|NGF_CLOSING); 7301acb27c6SJulian Elischer NG_NODE_UNREF(node); /* Assume they still have theirs */ 73130400f03SJulian Elischer return; 7324cf49a43SJulian Elischer } 7331acb27c6SJulian Elischer } else { /* do the default thing */ 7341acb27c6SJulian Elischer NG_NODE_UNREF(node); 7351acb27c6SJulian Elischer } 7364cf49a43SJulian Elischer 73730400f03SJulian Elischer ng_unname(node); /* basically a NOP these days */ 73830400f03SJulian Elischer 73930400f03SJulian Elischer /* 74030400f03SJulian Elischer * Remove extra reference, possibly the last 74130400f03SJulian Elischer * Possible other holders of references may include 74230400f03SJulian Elischer * timeout callouts, but theoretically the node's supposed to 74330400f03SJulian Elischer * have cancelled them. Possibly hardware dependencies may 74430400f03SJulian Elischer * force a driver to 'linger' with a reference. 74530400f03SJulian Elischer */ 74630400f03SJulian Elischer NG_NODE_UNREF(node); 7474cf49a43SJulian Elischer } 7484cf49a43SJulian Elischer 7495951069aSJulian Elischer /* 7505951069aSJulian Elischer * Remove a reference to the node, possibly the last. 7515951069aSJulian Elischer * deadnode always acts as it it were the last. 7525951069aSJulian Elischer */ 7535951069aSJulian Elischer int 75430400f03SJulian Elischer ng_unref_node(node_p node) 7554cf49a43SJulian Elischer { 75630400f03SJulian Elischer int v; 7576b795970SJulian Elischer 7586b795970SJulian Elischer if (node == &ng_deadnode) { 7595951069aSJulian Elischer return (0); 7606b795970SJulian Elischer } 7616b795970SJulian Elischer 76230400f03SJulian Elischer do { 7635951069aSJulian Elischer v = node->nd_refs - 1; 7645951069aSJulian Elischer } while (! atomic_cmpset_int(&node->nd_refs, v + 1, v)); 765e8a49db2SJulian Elischer 7665951069aSJulian Elischer if (v == 0) { /* we were the last */ 767069154d5SJulian Elischer 7689ed346baSBosko Milekic mtx_lock(&ng_nodelist_mtx); 76930400f03SJulian Elischer node->nd_type->refs--; /* XXX maybe should get types lock? */ 77030400f03SJulian Elischer LIST_REMOVE(node, nd_nodes); 7719ed346baSBosko Milekic mtx_unlock(&ng_nodelist_mtx); 772069154d5SJulian Elischer 7739ed346baSBosko Milekic mtx_lock(&ng_idhash_mtx); 77430400f03SJulian Elischer LIST_REMOVE(node, nd_idnodes); 7759ed346baSBosko Milekic mtx_unlock(&ng_idhash_mtx); 776069154d5SJulian Elischer 77712574a02SJulian Elischer mtx_destroy(&node->nd_input_queue.q_mtx); 778069154d5SJulian Elischer NG_FREE_NODE(node); 7794cf49a43SJulian Elischer } 7805951069aSJulian Elischer return (v); 7814cf49a43SJulian Elischer } 7824cf49a43SJulian Elischer 7834cf49a43SJulian Elischer /************************************************************************ 784dc90cad9SJulian Elischer Node ID handling 785dc90cad9SJulian Elischer ************************************************************************/ 786dc90cad9SJulian Elischer static node_p 787069154d5SJulian Elischer ng_ID2noderef(ng_ID_t ID) 788dc90cad9SJulian Elischer { 78930400f03SJulian Elischer node_p node; 7909ed346baSBosko Milekic mtx_lock(&ng_idhash_mtx); 7910f150d04SJulian Elischer NG_IDHASH_FIND(ID, node); 79230400f03SJulian Elischer if(node) 79330400f03SJulian Elischer NG_NODE_REF(node); 7949ed346baSBosko Milekic mtx_unlock(&ng_idhash_mtx); 79530400f03SJulian Elischer return(node); 796dc90cad9SJulian Elischer } 797dc90cad9SJulian Elischer 798dc90cad9SJulian Elischer ng_ID_t 799dc90cad9SJulian Elischer ng_node2ID(node_p node) 800dc90cad9SJulian Elischer { 80170de87f2SJulian Elischer return (node ? NG_NODE_ID(node) : 0); 802dc90cad9SJulian Elischer } 803dc90cad9SJulian Elischer 804dc90cad9SJulian Elischer /************************************************************************ 8054cf49a43SJulian Elischer Node name handling 8064cf49a43SJulian Elischer ************************************************************************/ 8074cf49a43SJulian Elischer 8084cf49a43SJulian Elischer /* 8094cf49a43SJulian Elischer * Assign a node a name. Once assigned, the name cannot be changed. 8104cf49a43SJulian Elischer */ 8114cf49a43SJulian Elischer int 8124cf49a43SJulian Elischer ng_name_node(node_p node, const char *name) 8134cf49a43SJulian Elischer { 8144cf49a43SJulian Elischer int i; 815069154d5SJulian Elischer node_p node2; 8164cf49a43SJulian Elischer 8174cf49a43SJulian Elischer /* Check the name is valid */ 81887e2c66aSHartmut Brandt for (i = 0; i < NG_NODESIZ; i++) { 8194cf49a43SJulian Elischer if (name[i] == '\0' || name[i] == '.' || name[i] == ':') 8204cf49a43SJulian Elischer break; 8214cf49a43SJulian Elischer } 8224cf49a43SJulian Elischer if (i == 0 || name[i] != '\0') { 8236b795970SJulian Elischer TRAP_ERROR(); 8244cf49a43SJulian Elischer return (EINVAL); 8254cf49a43SJulian Elischer } 826dc90cad9SJulian Elischer if (ng_decodeidname(name) != 0) { /* valid IDs not allowed here */ 8276b795970SJulian Elischer TRAP_ERROR(); 8284cf49a43SJulian Elischer return (EINVAL); 8294cf49a43SJulian Elischer } 8304cf49a43SJulian Elischer 8314cf49a43SJulian Elischer /* Check the name isn't already being used */ 832069154d5SJulian Elischer if ((node2 = ng_name2noderef(node, name)) != NULL) { 83330400f03SJulian Elischer NG_NODE_UNREF(node2); 8346b795970SJulian Elischer TRAP_ERROR(); 8354cf49a43SJulian Elischer return (EADDRINUSE); 8364cf49a43SJulian Elischer } 8374cf49a43SJulian Elischer 838069154d5SJulian Elischer /* copy it */ 83987e2c66aSHartmut Brandt strlcpy(NG_NODE_NAME(node), name, NG_NODESIZ); 8404cf49a43SJulian Elischer 8414cf49a43SJulian Elischer return (0); 8424cf49a43SJulian Elischer } 8434cf49a43SJulian Elischer 8444cf49a43SJulian Elischer /* 8454cf49a43SJulian Elischer * Find a node by absolute name. The name should NOT end with ':' 8464cf49a43SJulian Elischer * The name "." means "this node" and "[xxx]" means "the node 8474cf49a43SJulian Elischer * with ID (ie, at address) xxx". 8484cf49a43SJulian Elischer * 8494cf49a43SJulian Elischer * Returns the node if found, else NULL. 850069154d5SJulian Elischer * Eventually should add something faster than a sequential search. 85130400f03SJulian Elischer * Note it aquires a reference on the node so you can be sure it's still there. 8524cf49a43SJulian Elischer */ 8534cf49a43SJulian Elischer node_p 854069154d5SJulian Elischer ng_name2noderef(node_p here, const char *name) 8554cf49a43SJulian Elischer { 856dc90cad9SJulian Elischer node_p node; 857dc90cad9SJulian Elischer ng_ID_t temp; 8584cf49a43SJulian Elischer 8594cf49a43SJulian Elischer /* "." means "this node" */ 860069154d5SJulian Elischer if (strcmp(name, ".") == 0) { 86130400f03SJulian Elischer NG_NODE_REF(here); 862069154d5SJulian Elischer return(here); 863069154d5SJulian Elischer } 8644cf49a43SJulian Elischer 8654cf49a43SJulian Elischer /* Check for name-by-ID */ 866dc90cad9SJulian Elischer if ((temp = ng_decodeidname(name)) != 0) { 867069154d5SJulian Elischer return (ng_ID2noderef(temp)); 8684cf49a43SJulian Elischer } 8694cf49a43SJulian Elischer 8704cf49a43SJulian Elischer /* Find node by name */ 8719ed346baSBosko Milekic mtx_lock(&ng_nodelist_mtx); 87230400f03SJulian Elischer LIST_FOREACH(node, &ng_nodelist, nd_nodes) { 87370de87f2SJulian Elischer if (NG_NODE_IS_VALID(node) 87470de87f2SJulian Elischer && NG_NODE_HAS_NAME(node) 87570de87f2SJulian Elischer && (strcmp(NG_NODE_NAME(node), name) == 0)) { 8764cf49a43SJulian Elischer break; 8774cf49a43SJulian Elischer } 87870de87f2SJulian Elischer } 879069154d5SJulian Elischer if (node) 88030400f03SJulian Elischer NG_NODE_REF(node); 8819ed346baSBosko Milekic mtx_unlock(&ng_nodelist_mtx); 8824cf49a43SJulian Elischer return (node); 8834cf49a43SJulian Elischer } 8844cf49a43SJulian Elischer 8854cf49a43SJulian Elischer /* 8869d5abbddSJens Schweikhardt * Decode an ID name, eg. "[f03034de]". Returns 0 if the 887dc90cad9SJulian Elischer * string is not valid, otherwise returns the value. 8884cf49a43SJulian Elischer */ 889dc90cad9SJulian Elischer static ng_ID_t 8904cf49a43SJulian Elischer ng_decodeidname(const char *name) 8914cf49a43SJulian Elischer { 8922b70adcbSArchie Cobbs const int len = strlen(name); 89325792ef3SArchie Cobbs char *eptr; 8942b70adcbSArchie Cobbs u_long val; 8954cf49a43SJulian Elischer 8962b70adcbSArchie Cobbs /* Check for proper length, brackets, no leading junk */ 89770de87f2SJulian Elischer if ((len < 3) 89870de87f2SJulian Elischer || (name[0] != '[') 89970de87f2SJulian Elischer || (name[len - 1] != ']') 90070de87f2SJulian Elischer || (!isxdigit(name[1]))) { 90170de87f2SJulian Elischer return ((ng_ID_t)0); 90270de87f2SJulian Elischer } 9034cf49a43SJulian Elischer 9042b70adcbSArchie Cobbs /* Decode number */ 9052b70adcbSArchie Cobbs val = strtoul(name + 1, &eptr, 16); 90670de87f2SJulian Elischer if ((eptr - name != len - 1) 90770de87f2SJulian Elischer || (val == ULONG_MAX) 90870de87f2SJulian Elischer || (val == 0)) { 90912f035e0SJulian Elischer return ((ng_ID_t)0); 91070de87f2SJulian Elischer } 9112b70adcbSArchie Cobbs return (ng_ID_t)val; 9124cf49a43SJulian Elischer } 9134cf49a43SJulian Elischer 9144cf49a43SJulian Elischer /* 9154cf49a43SJulian Elischer * Remove a name from a node. This should only be called 9164cf49a43SJulian Elischer * when shutting down and removing the node. 91764efc707SRobert Watson * IF we allow name changing this may be more resurrected. 9184cf49a43SJulian Elischer */ 9194cf49a43SJulian Elischer void 9204cf49a43SJulian Elischer ng_unname(node_p node) 9214cf49a43SJulian Elischer { 9224cf49a43SJulian Elischer } 9234cf49a43SJulian Elischer 9244cf49a43SJulian Elischer /************************************************************************ 9254cf49a43SJulian Elischer Hook routines 9264cf49a43SJulian Elischer Names are not optional. Hooks are always connected, except for a 9273e4084c8SJulian Elischer brief moment within these routines. On invalidation or during creation 9283e4084c8SJulian Elischer they are connected to the 'dead' hook. 9294cf49a43SJulian Elischer ************************************************************************/ 9304cf49a43SJulian Elischer 9314cf49a43SJulian Elischer /* 9324cf49a43SJulian Elischer * Remove a hook reference 9334cf49a43SJulian Elischer */ 93430400f03SJulian Elischer void 9354cf49a43SJulian Elischer ng_unref_hook(hook_p hook) 9364cf49a43SJulian Elischer { 93730400f03SJulian Elischer int v; 9386b795970SJulian Elischer 9396b795970SJulian Elischer if (hook == &ng_deadhook) { 9406b795970SJulian Elischer return; 9416b795970SJulian Elischer } 94230400f03SJulian Elischer do { 94330400f03SJulian Elischer v = hook->hk_refs; 94430400f03SJulian Elischer } while (! atomic_cmpset_int(&hook->hk_refs, v, v - 1)); 945e8a49db2SJulian Elischer 94630400f03SJulian Elischer if (v == 1) { /* we were the last */ 9476b795970SJulian Elischer if (_NG_HOOK_NODE(hook)) { /* it'll probably be ng_deadnode */ 9486b795970SJulian Elischer _NG_NODE_UNREF((_NG_HOOK_NODE(hook))); 94930400f03SJulian Elischer hook->hk_node = NULL; 950069154d5SJulian Elischer } 951069154d5SJulian Elischer NG_FREE_HOOK(hook); 952069154d5SJulian Elischer } 9534cf49a43SJulian Elischer } 9544cf49a43SJulian Elischer 9554cf49a43SJulian Elischer /* 9564cf49a43SJulian Elischer * Add an unconnected hook to a node. Only used internally. 9573e4084c8SJulian Elischer * Assumes node is locked. (XXX not yet true ) 9584cf49a43SJulian Elischer */ 9594cf49a43SJulian Elischer static int 9604cf49a43SJulian Elischer ng_add_hook(node_p node, const char *name, hook_p *hookp) 9614cf49a43SJulian Elischer { 9624cf49a43SJulian Elischer hook_p hook; 9634cf49a43SJulian Elischer int error = 0; 9644cf49a43SJulian Elischer 9654cf49a43SJulian Elischer /* Check that the given name is good */ 9664cf49a43SJulian Elischer if (name == NULL) { 9676b795970SJulian Elischer TRAP_ERROR(); 9684cf49a43SJulian Elischer return (EINVAL); 9694cf49a43SJulian Elischer } 970899e9c4eSArchie Cobbs if (ng_findhook(node, name) != NULL) { 9716b795970SJulian Elischer TRAP_ERROR(); 9724cf49a43SJulian Elischer return (EEXIST); 9734cf49a43SJulian Elischer } 9744cf49a43SJulian Elischer 9754cf49a43SJulian Elischer /* Allocate the hook and link it up */ 97630400f03SJulian Elischer NG_ALLOC_HOOK(hook); 9774cf49a43SJulian Elischer if (hook == NULL) { 9786b795970SJulian Elischer TRAP_ERROR(); 9794cf49a43SJulian Elischer return (ENOMEM); 9804cf49a43SJulian Elischer } 9813e4084c8SJulian Elischer hook->hk_refs = 1; /* add a reference for us to return */ 98230400f03SJulian Elischer hook->hk_flags = HK_INVALID; 9833e4084c8SJulian Elischer hook->hk_peer = &ng_deadhook; /* start off this way */ 98430400f03SJulian Elischer hook->hk_node = node; 98530400f03SJulian Elischer NG_NODE_REF(node); /* each hook counts as a reference */ 9864cf49a43SJulian Elischer 9873e4084c8SJulian Elischer /* Set hook name */ 98887e2c66aSHartmut Brandt strlcpy(NG_HOOK_NAME(hook), name, NG_HOOKSIZ); 9893e4084c8SJulian Elischer 9903e4084c8SJulian Elischer /* 9913e4084c8SJulian Elischer * Check if the node type code has something to say about it 9923e4084c8SJulian Elischer * If it fails, the unref of the hook will also unref the node. 9933e4084c8SJulian Elischer */ 994954c4772SJulian Elischer if (node->nd_type->newhook != NULL) { 995954c4772SJulian Elischer if ((error = (*node->nd_type->newhook)(node, hook, name))) { 99630400f03SJulian Elischer NG_HOOK_UNREF(hook); /* this frees the hook */ 997069154d5SJulian Elischer return (error); 998069154d5SJulian Elischer } 999954c4772SJulian Elischer } 10004cf49a43SJulian Elischer /* 10014cf49a43SJulian Elischer * The 'type' agrees so far, so go ahead and link it in. 10024cf49a43SJulian Elischer * We'll ask again later when we actually connect the hooks. 10034cf49a43SJulian Elischer */ 100430400f03SJulian Elischer LIST_INSERT_HEAD(&node->nd_hooks, hook, hk_hooks); 100530400f03SJulian Elischer node->nd_numhooks++; 10063e4084c8SJulian Elischer NG_HOOK_REF(hook); /* one for the node */ 10074cf49a43SJulian Elischer 10084cf49a43SJulian Elischer if (hookp) 10094cf49a43SJulian Elischer *hookp = hook; 10103e4084c8SJulian Elischer return (0); 10114cf49a43SJulian Elischer } 10124cf49a43SJulian Elischer 10134cf49a43SJulian Elischer /* 1014899e9c4eSArchie Cobbs * Find a hook 1015899e9c4eSArchie Cobbs * 1016899e9c4eSArchie Cobbs * Node types may supply their own optimized routines for finding 1017899e9c4eSArchie Cobbs * hooks. If none is supplied, we just do a linear search. 10183e4084c8SJulian Elischer * XXX Possibly we should add a reference to the hook? 1019899e9c4eSArchie Cobbs */ 1020899e9c4eSArchie Cobbs hook_p 1021899e9c4eSArchie Cobbs ng_findhook(node_p node, const char *name) 1022899e9c4eSArchie Cobbs { 1023899e9c4eSArchie Cobbs hook_p hook; 1024899e9c4eSArchie Cobbs 102530400f03SJulian Elischer if (node->nd_type->findhook != NULL) 102630400f03SJulian Elischer return (*node->nd_type->findhook)(node, name); 102730400f03SJulian Elischer LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) { 102870de87f2SJulian Elischer if (NG_HOOK_IS_VALID(hook) 1029ce5e5f99SArchie Cobbs && (strcmp(NG_HOOK_NAME(hook), name) == 0)) 1030899e9c4eSArchie Cobbs return (hook); 1031899e9c4eSArchie Cobbs } 1032899e9c4eSArchie Cobbs return (NULL); 1033899e9c4eSArchie Cobbs } 1034899e9c4eSArchie Cobbs 1035899e9c4eSArchie Cobbs /* 10364cf49a43SJulian Elischer * Destroy a hook 10374cf49a43SJulian Elischer * 10384cf49a43SJulian Elischer * As hooks are always attached, this really destroys two hooks. 10394cf49a43SJulian Elischer * The one given, and the one attached to it. Disconnect the hooks 10403e4084c8SJulian Elischer * from each other first. We reconnect the peer hook to the 'dead' 10413e4084c8SJulian Elischer * hook so that it can still exist after we depart. We then 10423e4084c8SJulian Elischer * send the peer its own destroy message. This ensures that we only 10433e4084c8SJulian Elischer * interact with the peer's structures when it is locked processing that 10443e4084c8SJulian Elischer * message. We hold a reference to the peer hook so we are guaranteed that 10453e4084c8SJulian Elischer * the peer hook and node are still going to exist until 10463e4084c8SJulian Elischer * we are finished there as the hook holds a ref on the node. 10473e4084c8SJulian Elischer * We run this same code again on the peer hook, but that time it is already 10483e4084c8SJulian Elischer * attached to the 'dead' hook. 10496b795970SJulian Elischer * 10506b795970SJulian Elischer * This routine is called at all stages of hook creation 10516b795970SJulian Elischer * on error detection and must be able to handle any such stage. 10524cf49a43SJulian Elischer */ 10534cf49a43SJulian Elischer void 10544cf49a43SJulian Elischer ng_destroy_hook(hook_p hook) 10554cf49a43SJulian Elischer { 1056ac5dd141SGleb Smirnoff hook_p peer; 1057ac5dd141SGleb Smirnoff node_p node; 10584cf49a43SJulian Elischer 10596b795970SJulian Elischer if (hook == &ng_deadhook) { /* better safe than sorry */ 10606b795970SJulian Elischer printf("ng_destroy_hook called on deadhook\n"); 10616b795970SJulian Elischer return; 10626b795970SJulian Elischer } 1063ac5dd141SGleb Smirnoff 1064ac5dd141SGleb Smirnoff /* 1065ac5dd141SGleb Smirnoff * Protect divorce process with mutex, to avoid races on 1066ac5dd141SGleb Smirnoff * simultaneous disconnect. 1067ac5dd141SGleb Smirnoff */ 1068ac5dd141SGleb Smirnoff mtx_lock(&ng_topo_mtx); 1069ac5dd141SGleb Smirnoff 1070ac5dd141SGleb Smirnoff hook->hk_flags |= HK_INVALID; 1071ac5dd141SGleb Smirnoff 1072ac5dd141SGleb Smirnoff peer = NG_HOOK_PEER(hook); 1073ac5dd141SGleb Smirnoff node = NG_HOOK_NODE(hook); 1074ac5dd141SGleb Smirnoff 10753e4084c8SJulian Elischer if (peer && (peer != &ng_deadhook)) { 10763e4084c8SJulian Elischer /* 10773e4084c8SJulian Elischer * Set the peer to point to ng_deadhook 10783e4084c8SJulian Elischer * from this moment on we are effectively independent it. 10793e4084c8SJulian Elischer * send it an rmhook message of it's own. 10803e4084c8SJulian Elischer */ 10813e4084c8SJulian Elischer peer->hk_peer = &ng_deadhook; /* They no longer know us */ 10823e4084c8SJulian Elischer hook->hk_peer = &ng_deadhook; /* Nor us, them */ 10836b795970SJulian Elischer if (NG_HOOK_NODE(peer) == &ng_deadnode) { 10846b795970SJulian Elischer /* 10856b795970SJulian Elischer * If it's already divorced from a node, 10866b795970SJulian Elischer * just free it. 10876b795970SJulian Elischer */ 1088ac5dd141SGleb Smirnoff mtx_unlock(&ng_topo_mtx); 10896b795970SJulian Elischer } else { 1090ac5dd141SGleb Smirnoff mtx_unlock(&ng_topo_mtx); 10916b795970SJulian Elischer ng_rmhook_self(peer); /* Send it a surprise */ 10926b795970SJulian Elischer } 109352fa3556SJulian Elischer NG_HOOK_UNREF(peer); /* account for peer link */ 109452fa3556SJulian Elischer NG_HOOK_UNREF(hook); /* account for peer link */ 1095ac5dd141SGleb Smirnoff } else 1096ac5dd141SGleb Smirnoff mtx_unlock(&ng_topo_mtx); 1097ac5dd141SGleb Smirnoff 1098ac5dd141SGleb Smirnoff mtx_assert(&ng_topo_mtx, MA_NOTOWNED); 10994cf49a43SJulian Elischer 11004cf49a43SJulian Elischer /* 11014cf49a43SJulian Elischer * Remove the hook from the node's list to avoid possible recursion 11024cf49a43SJulian Elischer * in case the disconnection results in node shutdown. 11034cf49a43SJulian Elischer */ 11046b795970SJulian Elischer if (node == &ng_deadnode) { /* happens if called from ng_con_nodes() */ 11056b795970SJulian Elischer return; 11066b795970SJulian Elischer } 110730400f03SJulian Elischer LIST_REMOVE(hook, hk_hooks); 110830400f03SJulian Elischer node->nd_numhooks--; 110930400f03SJulian Elischer if (node->nd_type->disconnect) { 11104cf49a43SJulian Elischer /* 11116b795970SJulian Elischer * The type handler may elect to destroy the node so don't 111264efc707SRobert Watson * trust its existence after this point. (except 11136b795970SJulian Elischer * that we still hold a reference on it. (which we 11146b795970SJulian Elischer * inherrited from the hook we are destroying) 11154cf49a43SJulian Elischer */ 111630400f03SJulian Elischer (*node->nd_type->disconnect) (hook); 11174cf49a43SJulian Elischer } 11186b795970SJulian Elischer 11196b795970SJulian Elischer /* 11206b795970SJulian Elischer * Note that because we will point to ng_deadnode, the original node 11216b795970SJulian Elischer * is not decremented automatically so we do that manually. 11226b795970SJulian Elischer */ 11236b795970SJulian Elischer _NG_HOOK_NODE(hook) = &ng_deadnode; 11246b795970SJulian Elischer NG_NODE_UNREF(node); /* We no longer point to it so adjust count */ 11256b795970SJulian Elischer NG_HOOK_UNREF(hook); /* Account for linkage (in list) to node */ 11264cf49a43SJulian Elischer } 11274cf49a43SJulian Elischer 11284cf49a43SJulian Elischer /* 11294cf49a43SJulian Elischer * Take two hooks on a node and merge the connection so that the given node 11304cf49a43SJulian Elischer * is effectively bypassed. 11314cf49a43SJulian Elischer */ 11324cf49a43SJulian Elischer int 11334cf49a43SJulian Elischer ng_bypass(hook_p hook1, hook_p hook2) 11344cf49a43SJulian Elischer { 113530400f03SJulian Elischer if (hook1->hk_node != hook2->hk_node) { 11366b795970SJulian Elischer TRAP_ERROR(); 11374cf49a43SJulian Elischer return (EINVAL); 113830400f03SJulian Elischer } 113930400f03SJulian Elischer hook1->hk_peer->hk_peer = hook2->hk_peer; 114030400f03SJulian Elischer hook2->hk_peer->hk_peer = hook1->hk_peer; 11414cf49a43SJulian Elischer 11423e4084c8SJulian Elischer hook1->hk_peer = &ng_deadhook; 11433e4084c8SJulian Elischer hook2->hk_peer = &ng_deadhook; 11443e4084c8SJulian Elischer 1145cf3254aaSGleb Smirnoff NG_HOOK_UNREF(hook1); 1146cf3254aaSGleb Smirnoff NG_HOOK_UNREF(hook2); 1147cf3254aaSGleb Smirnoff 11484cf49a43SJulian Elischer /* XXX If we ever cache methods on hooks update them as well */ 11494cf49a43SJulian Elischer ng_destroy_hook(hook1); 11504cf49a43SJulian Elischer ng_destroy_hook(hook2); 11514cf49a43SJulian Elischer return (0); 11524cf49a43SJulian Elischer } 11534cf49a43SJulian Elischer 11544cf49a43SJulian Elischer /* 11554cf49a43SJulian Elischer * Install a new netgraph type 11564cf49a43SJulian Elischer */ 11574cf49a43SJulian Elischer int 11584cf49a43SJulian Elischer ng_newtype(struct ng_type *tp) 11594cf49a43SJulian Elischer { 11604cf49a43SJulian Elischer const size_t namelen = strlen(tp->name); 11614cf49a43SJulian Elischer 11624cf49a43SJulian Elischer /* Check version and type name fields */ 1163589f6ed8SJulian Elischer if ((tp->version != NG_ABI_VERSION) 1164589f6ed8SJulian Elischer || (namelen == 0) 116587e2c66aSHartmut Brandt || (namelen >= NG_TYPESIZ)) { 11666b795970SJulian Elischer TRAP_ERROR(); 11678ed370fdSJulian Elischer if (tp->version != NG_ABI_VERSION) { 11688ed370fdSJulian Elischer printf("Netgraph: Node type rejected. ABI mismatch. Suggest recompile\n"); 11698ed370fdSJulian Elischer } 11704cf49a43SJulian Elischer return (EINVAL); 11714cf49a43SJulian Elischer } 11724cf49a43SJulian Elischer 11734cf49a43SJulian Elischer /* Check for name collision */ 11744cf49a43SJulian Elischer if (ng_findtype(tp->name) != NULL) { 11756b795970SJulian Elischer TRAP_ERROR(); 11764cf49a43SJulian Elischer return (EEXIST); 11774cf49a43SJulian Elischer } 11784cf49a43SJulian Elischer 1179069154d5SJulian Elischer 1180069154d5SJulian Elischer /* Link in new type */ 11819ed346baSBosko Milekic mtx_lock(&ng_typelist_mtx); 1182069154d5SJulian Elischer LIST_INSERT_HEAD(&ng_typelist, tp, types); 1183c73b94a2SJulian Elischer tp->refs = 1; /* first ref is linked list */ 11849ed346baSBosko Milekic mtx_unlock(&ng_typelist_mtx); 11854cf49a43SJulian Elischer return (0); 11864cf49a43SJulian Elischer } 11874cf49a43SJulian Elischer 11884cf49a43SJulian Elischer /* 1189c31b4a53SJulian Elischer * unlink a netgraph type 1190c31b4a53SJulian Elischer * If no examples exist 1191c31b4a53SJulian Elischer */ 1192c31b4a53SJulian Elischer int 1193c31b4a53SJulian Elischer ng_rmtype(struct ng_type *tp) 1194c31b4a53SJulian Elischer { 1195c31b4a53SJulian Elischer /* Check for name collision */ 1196c31b4a53SJulian Elischer if (tp->refs != 1) { 1197c31b4a53SJulian Elischer TRAP_ERROR(); 1198c31b4a53SJulian Elischer return (EBUSY); 1199c31b4a53SJulian Elischer } 1200c31b4a53SJulian Elischer 1201c31b4a53SJulian Elischer /* Unlink type */ 1202c31b4a53SJulian Elischer mtx_lock(&ng_typelist_mtx); 1203c31b4a53SJulian Elischer LIST_REMOVE(tp, types); 1204c31b4a53SJulian Elischer mtx_unlock(&ng_typelist_mtx); 1205c31b4a53SJulian Elischer return (0); 1206c31b4a53SJulian Elischer } 1207c31b4a53SJulian Elischer 1208c31b4a53SJulian Elischer /* 12094cf49a43SJulian Elischer * Look for a type of the name given 12104cf49a43SJulian Elischer */ 12114cf49a43SJulian Elischer struct ng_type * 12124cf49a43SJulian Elischer ng_findtype(const char *typename) 12134cf49a43SJulian Elischer { 12144cf49a43SJulian Elischer struct ng_type *type; 12154cf49a43SJulian Elischer 12169ed346baSBosko Milekic mtx_lock(&ng_typelist_mtx); 1217069154d5SJulian Elischer LIST_FOREACH(type, &ng_typelist, types) { 12184cf49a43SJulian Elischer if (strcmp(type->name, typename) == 0) 12194cf49a43SJulian Elischer break; 12204cf49a43SJulian Elischer } 12219ed346baSBosko Milekic mtx_unlock(&ng_typelist_mtx); 12224cf49a43SJulian Elischer return (type); 12234cf49a43SJulian Elischer } 12244cf49a43SJulian Elischer 12254cf49a43SJulian Elischer /************************************************************************ 12264cf49a43SJulian Elischer Composite routines 12274cf49a43SJulian Elischer ************************************************************************/ 12284cf49a43SJulian Elischer /* 12296b795970SJulian Elischer * Connect two nodes using the specified hooks, using queued functions. 12304cf49a43SJulian Elischer */ 12311acb27c6SJulian Elischer static void 12326b795970SJulian Elischer ng_con_part3(node_p node, hook_p hook, void *arg1, int arg2) 12334cf49a43SJulian Elischer { 12344cf49a43SJulian Elischer 12356b795970SJulian Elischer /* 12366b795970SJulian Elischer * When we run, we know that the node 'node' is locked for us. 12376b795970SJulian Elischer * Our caller has a reference on the hook. 12386b795970SJulian Elischer * Our caller has a reference on the node. 12396b795970SJulian Elischer * (In this case our caller is ng_apply_item() ). 12406b795970SJulian Elischer * The peer hook has a reference on the hook. 12411acb27c6SJulian Elischer * We are all set up except for the final call to the node, and 12421acb27c6SJulian Elischer * the clearing of the INVALID flag. 12436b795970SJulian Elischer */ 12446b795970SJulian Elischer if (NG_HOOK_NODE(hook) == &ng_deadnode) { 12456b795970SJulian Elischer /* 12466b795970SJulian Elischer * The node must have been freed again since we last visited 12476b795970SJulian Elischer * here. ng_destry_hook() has this effect but nothing else does. 12486b795970SJulian Elischer * We should just release our references and 12496b795970SJulian Elischer * free anything we can think of. 12506b795970SJulian Elischer * Since we know it's been destroyed, and it's our caller 12516b795970SJulian Elischer * that holds the references, just return. 12526b795970SJulian Elischer */ 12531acb27c6SJulian Elischer return ; 12546b795970SJulian Elischer } 12556b795970SJulian Elischer if (hook->hk_node->nd_type->connect) { 12561acb27c6SJulian Elischer if ((*hook->hk_node->nd_type->connect) (hook)) { 12576b795970SJulian Elischer ng_destroy_hook(hook); /* also zaps peer */ 12581acb27c6SJulian Elischer printf("failed in ng_con_part3()\n"); 12591acb27c6SJulian Elischer return ; 12604cf49a43SJulian Elischer } 12616b795970SJulian Elischer } 12626b795970SJulian Elischer /* 12636b795970SJulian Elischer * XXX this is wrong for SMP. Possibly we need 12646b795970SJulian Elischer * to separate out 'create' and 'invalid' flags. 12656b795970SJulian Elischer * should only set flags on hooks we have locked under our node. 12666b795970SJulian Elischer */ 12676b795970SJulian Elischer hook->hk_flags &= ~HK_INVALID; 12681acb27c6SJulian Elischer return ; 12696b795970SJulian Elischer } 12706b795970SJulian Elischer 12711acb27c6SJulian Elischer static void 12726b795970SJulian Elischer ng_con_part2(node_p node, hook_p hook, void *arg1, int arg2) 12736b795970SJulian Elischer { 1274ac5dd141SGleb Smirnoff hook_p peer; 12756b795970SJulian Elischer 12766b795970SJulian Elischer /* 12776b795970SJulian Elischer * When we run, we know that the node 'node' is locked for us. 12786b795970SJulian Elischer * Our caller has a reference on the hook. 12796b795970SJulian Elischer * Our caller has a reference on the node. 12806b795970SJulian Elischer * (In this case our caller is ng_apply_item() ). 12816b795970SJulian Elischer * The peer hook has a reference on the hook. 12826b795970SJulian Elischer * our node pointer points to the 'dead' node. 12836b795970SJulian Elischer * First check the hook name is unique. 12841acb27c6SJulian Elischer * Should not happen because we checked before queueing this. 12856b795970SJulian Elischer */ 12866b795970SJulian Elischer if (ng_findhook(node, NG_HOOK_NAME(hook)) != NULL) { 12876b795970SJulian Elischer TRAP_ERROR(); 12886b795970SJulian Elischer ng_destroy_hook(hook); /* should destroy peer too */ 12891acb27c6SJulian Elischer printf("failed in ng_con_part2()\n"); 12901acb27c6SJulian Elischer return ; 12916b795970SJulian Elischer } 12926b795970SJulian Elischer /* 12936b795970SJulian Elischer * Check if the node type code has something to say about it 12946b795970SJulian Elischer * If it fails, the unref of the hook will also unref the attached node, 12956b795970SJulian Elischer * however since that node is 'ng_deadnode' this will do nothing. 12966b795970SJulian Elischer * The peer hook will also be destroyed. 12976b795970SJulian Elischer */ 12986b795970SJulian Elischer if (node->nd_type->newhook != NULL) { 12991acb27c6SJulian Elischer if ((*node->nd_type->newhook)(node, hook, hook->hk_name)) { 13006b795970SJulian Elischer ng_destroy_hook(hook); /* should destroy peer too */ 13011acb27c6SJulian Elischer printf("failed in ng_con_part2()\n"); 13021acb27c6SJulian Elischer return ; 13036b795970SJulian Elischer } 13046b795970SJulian Elischer } 13056b795970SJulian Elischer 13066b795970SJulian Elischer /* 13076b795970SJulian Elischer * The 'type' agrees so far, so go ahead and link it in. 13086b795970SJulian Elischer * We'll ask again later when we actually connect the hooks. 13096b795970SJulian Elischer */ 13106b795970SJulian Elischer hook->hk_node = node; /* just overwrite ng_deadnode */ 13116b795970SJulian Elischer NG_NODE_REF(node); /* each hook counts as a reference */ 13126b795970SJulian Elischer LIST_INSERT_HEAD(&node->nd_hooks, hook, hk_hooks); 13136b795970SJulian Elischer node->nd_numhooks++; 13146b795970SJulian Elischer NG_HOOK_REF(hook); /* one for the node */ 13156b795970SJulian Elischer 13166b795970SJulian Elischer /* 131764efc707SRobert Watson * We now have a symmetrical situation, where both hooks have been 13185951069aSJulian Elischer * linked to their nodes, the newhook methods have been called 13196b795970SJulian Elischer * And the references are all correct. The hooks are still marked 13206b795970SJulian Elischer * as invalid, as we have not called the 'connect' methods 13216b795970SJulian Elischer * yet. 132264efc707SRobert Watson * We can call the local one immediately as we have the 13236b795970SJulian Elischer * node locked, but we need to queue the remote one. 13246b795970SJulian Elischer */ 13256b795970SJulian Elischer if (hook->hk_node->nd_type->connect) { 13261acb27c6SJulian Elischer if ((*hook->hk_node->nd_type->connect) (hook)) { 13276b795970SJulian Elischer ng_destroy_hook(hook); /* also zaps peer */ 13281acb27c6SJulian Elischer printf("failed in ng_con_part2(A)\n"); 13291acb27c6SJulian Elischer return ; 13306b795970SJulian Elischer } 13316b795970SJulian Elischer } 1332ac5dd141SGleb Smirnoff 1333ac5dd141SGleb Smirnoff /* 1334ac5dd141SGleb Smirnoff * Acquire topo mutex to avoid race with ng_destroy_hook(). 1335ac5dd141SGleb Smirnoff */ 1336ac5dd141SGleb Smirnoff mtx_lock(&ng_topo_mtx); 1337ac5dd141SGleb Smirnoff peer = hook->hk_peer; 1338ac5dd141SGleb Smirnoff if (peer == &ng_deadhook) { 1339ac5dd141SGleb Smirnoff mtx_unlock(&ng_topo_mtx); 1340ac5dd141SGleb Smirnoff printf("failed in ng_con_part2(B)\n"); 1341ac5dd141SGleb Smirnoff ng_destroy_hook(hook); 1342ac5dd141SGleb Smirnoff return ; 1343ac5dd141SGleb Smirnoff } 1344ac5dd141SGleb Smirnoff mtx_unlock(&ng_topo_mtx); 1345ac5dd141SGleb Smirnoff 1346ac5dd141SGleb Smirnoff if (ng_send_fn(peer->hk_node, peer, &ng_con_part3, arg1, arg2)) { 1347ac5dd141SGleb Smirnoff printf("failed in ng_con_part2(C)\n"); 13481acb27c6SJulian Elischer ng_destroy_hook(hook); /* also zaps peer */ 13491acb27c6SJulian Elischer return ; 13501acb27c6SJulian Elischer } 13516b795970SJulian Elischer hook->hk_flags &= ~HK_INVALID; /* need both to be able to work */ 13521acb27c6SJulian Elischer return ; 13534cf49a43SJulian Elischer } 13544cf49a43SJulian Elischer 13554cf49a43SJulian Elischer /* 13566b795970SJulian Elischer * Connect this node with another node. We assume that this node is 13576b795970SJulian Elischer * currently locked, as we are only called from an NGM_CONNECT message. 13584cf49a43SJulian Elischer */ 13596b795970SJulian Elischer static int 13604cf49a43SJulian Elischer ng_con_nodes(node_p node, const char *name, node_p node2, const char *name2) 13614cf49a43SJulian Elischer { 13624cf49a43SJulian Elischer int error; 13634cf49a43SJulian Elischer hook_p hook; 13644cf49a43SJulian Elischer hook_p hook2; 13654cf49a43SJulian Elischer 13661acb27c6SJulian Elischer if (ng_findhook(node2, name2) != NULL) { 13671acb27c6SJulian Elischer return(EEXIST); 13681acb27c6SJulian Elischer } 13693e4084c8SJulian Elischer if ((error = ng_add_hook(node, name, &hook))) /* gives us a ref */ 13704cf49a43SJulian Elischer return (error); 13716b795970SJulian Elischer /* Allocate the other hook and link it up */ 13726b795970SJulian Elischer NG_ALLOC_HOOK(hook2); 137319724144SGleb Smirnoff if (hook2 == NULL) { 13746b795970SJulian Elischer TRAP_ERROR(); 13756b795970SJulian Elischer ng_destroy_hook(hook); /* XXX check ref counts so far */ 13766b795970SJulian Elischer NG_HOOK_UNREF(hook); /* including our ref */ 13776b795970SJulian Elischer return (ENOMEM); 13786b795970SJulian Elischer } 13796b795970SJulian Elischer hook2->hk_refs = 1; /* start with a reference for us. */ 13806b795970SJulian Elischer hook2->hk_flags = HK_INVALID; 13816b795970SJulian Elischer hook2->hk_peer = hook; /* Link the two together */ 13826b795970SJulian Elischer hook->hk_peer = hook2; 13836b795970SJulian Elischer NG_HOOK_REF(hook); /* Add a ref for the peer to each*/ 13846b795970SJulian Elischer NG_HOOK_REF(hook2); 13856b795970SJulian Elischer hook2->hk_node = &ng_deadnode; 138687e2c66aSHartmut Brandt strlcpy(NG_HOOK_NAME(hook2), name2, NG_HOOKSIZ); 13876b795970SJulian Elischer 13886b795970SJulian Elischer /* 13896b795970SJulian Elischer * Queue the function above. 13906b795970SJulian Elischer * Procesing continues in that function in the lock context of 13916b795970SJulian Elischer * the other node. 13926b795970SJulian Elischer */ 13931acb27c6SJulian Elischer ng_send_fn(node2, hook2, &ng_con_part2, NULL, 0); 13946b795970SJulian Elischer 13956b795970SJulian Elischer NG_HOOK_UNREF(hook); /* Let each hook go if it wants to */ 13966b795970SJulian Elischer NG_HOOK_UNREF(hook2); 13971acb27c6SJulian Elischer return (0); 13984cf49a43SJulian Elischer } 13996b795970SJulian Elischer 14006b795970SJulian Elischer /* 14016b795970SJulian Elischer * Make a peer and connect. 14026b795970SJulian Elischer * We assume that the local node is locked. 14036b795970SJulian Elischer * The new node probably doesn't need a lock until 14046b795970SJulian Elischer * it has a hook, because it cannot really have any work until then, 14056b795970SJulian Elischer * but we should think about it a bit more. 14066b795970SJulian Elischer * 14076b795970SJulian Elischer * The problem may come if the other node also fires up 14086b795970SJulian Elischer * some hardware or a timer or some other source of activation, 14096b795970SJulian Elischer * also it may already get a command msg via it's ID. 14106b795970SJulian Elischer * 14116b795970SJulian Elischer * We could use the same method as ng_con_nodes() but we'd have 14126b795970SJulian Elischer * to add ability to remove the node when failing. (Not hard, just 14136b795970SJulian Elischer * make arg1 point to the node to remove). 14146b795970SJulian Elischer * Unless of course we just ignore failure to connect and leave 14156b795970SJulian Elischer * an unconnected node? 14166b795970SJulian Elischer */ 14176b795970SJulian Elischer static int 14186b795970SJulian Elischer ng_mkpeer(node_p node, const char *name, const char *name2, char *type) 14196b795970SJulian Elischer { 14206b795970SJulian Elischer node_p node2; 14214c9b5910SGleb Smirnoff hook_p hook1, hook2; 14226b795970SJulian Elischer int error; 14236b795970SJulian Elischer 14246b795970SJulian Elischer if ((error = ng_make_node(type, &node2))) { 14256b795970SJulian Elischer return (error); 14266b795970SJulian Elischer } 14276b795970SJulian Elischer 14286b795970SJulian Elischer if ((error = ng_add_hook(node, name, &hook1))) { /* gives us a ref */ 14291acb27c6SJulian Elischer ng_rmnode(node2, NULL, NULL, 0); 14306b795970SJulian Elischer return (error); 14316b795970SJulian Elischer } 14326b795970SJulian Elischer 14336b795970SJulian Elischer if ((error = ng_add_hook(node2, name2, &hook2))) { 14341acb27c6SJulian Elischer ng_rmnode(node2, NULL, NULL, 0); 14356b795970SJulian Elischer ng_destroy_hook(hook1); 14366b795970SJulian Elischer NG_HOOK_UNREF(hook1); 14376b795970SJulian Elischer return (error); 14386b795970SJulian Elischer } 14396b795970SJulian Elischer 14406b795970SJulian Elischer /* 14416b795970SJulian Elischer * Actually link the two hooks together. 14426b795970SJulian Elischer */ 14436b795970SJulian Elischer hook1->hk_peer = hook2; 14446b795970SJulian Elischer hook2->hk_peer = hook1; 14456b795970SJulian Elischer 14466b795970SJulian Elischer /* Each hook is referenced by the other */ 14476b795970SJulian Elischer NG_HOOK_REF(hook1); 14486b795970SJulian Elischer NG_HOOK_REF(hook2); 14496b795970SJulian Elischer 14506b795970SJulian Elischer /* Give each node the opportunity to veto the pending connection */ 14516b795970SJulian Elischer if (hook1->hk_node->nd_type->connect) { 14526b795970SJulian Elischer error = (*hook1->hk_node->nd_type->connect) (hook1); 14536b795970SJulian Elischer } 14546b795970SJulian Elischer 14556b795970SJulian Elischer if ((error == 0) && hook2->hk_node->nd_type->connect) { 14566b795970SJulian Elischer error = (*hook2->hk_node->nd_type->connect) (hook2); 14576b795970SJulian Elischer 14586b795970SJulian Elischer } 14593e4084c8SJulian Elischer 14603e4084c8SJulian Elischer /* 14613e4084c8SJulian Elischer * drop the references we were holding on the two hooks. 14623e4084c8SJulian Elischer */ 14636b795970SJulian Elischer if (error) { 14646b795970SJulian Elischer ng_destroy_hook(hook2); /* also zaps hook1 */ 14651acb27c6SJulian Elischer ng_rmnode(node2, NULL, NULL, 0); 14666b795970SJulian Elischer } else { 14676b795970SJulian Elischer /* As a last act, allow the hooks to be used */ 14686b795970SJulian Elischer hook1->hk_flags &= ~HK_INVALID; 14696b795970SJulian Elischer hook2->hk_flags &= ~HK_INVALID; 14706b795970SJulian Elischer } 14716b795970SJulian Elischer NG_HOOK_UNREF(hook1); 14723e4084c8SJulian Elischer NG_HOOK_UNREF(hook2); 14733e4084c8SJulian Elischer return (error); 14744cf49a43SJulian Elischer } 14756b795970SJulian Elischer 1476069154d5SJulian Elischer /************************************************************************ 1477069154d5SJulian Elischer Utility routines to send self messages 1478069154d5SJulian Elischer ************************************************************************/ 1479069154d5SJulian Elischer 14801acb27c6SJulian Elischer /* Shut this node down as soon as everyone is clear of it */ 148164efc707SRobert Watson /* Should add arg "immediately" to jump the queue */ 1482069154d5SJulian Elischer int 14831acb27c6SJulian Elischer ng_rmnode_self(node_p node) 1484069154d5SJulian Elischer { 14851acb27c6SJulian Elischer int error; 14864cf49a43SJulian Elischer 14871acb27c6SJulian Elischer if (node == &ng_deadnode) 14881acb27c6SJulian Elischer return (0); 1489be4252b3SJulian Elischer node->nd_flags |= NGF_INVALID; 1490be4252b3SJulian Elischer if (node->nd_flags & NGF_CLOSING) 14911acb27c6SJulian Elischer return (0); 1492069154d5SJulian Elischer 14931acb27c6SJulian Elischer error = ng_send_fn(node, NULL, &ng_rmnode, NULL, 0); 14941acb27c6SJulian Elischer return (error); 1495069154d5SJulian Elischer } 1496069154d5SJulian Elischer 14971acb27c6SJulian Elischer static void 14986b795970SJulian Elischer ng_rmhook_part2(node_p node, hook_p hook, void *arg1, int arg2) 14996b795970SJulian Elischer { 15006b795970SJulian Elischer ng_destroy_hook(hook); 15011acb27c6SJulian Elischer return ; 15026b795970SJulian Elischer } 15036b795970SJulian Elischer 1504954c4772SJulian Elischer int 1505954c4772SJulian Elischer ng_rmhook_self(hook_p hook) 1506954c4772SJulian Elischer { 15076b795970SJulian Elischer int error; 1508954c4772SJulian Elischer node_p node = NG_HOOK_NODE(hook); 1509954c4772SJulian Elischer 15106b795970SJulian Elischer if (node == &ng_deadnode) 15116b795970SJulian Elischer return (0); 15126b795970SJulian Elischer 15136b795970SJulian Elischer error = ng_send_fn(node, hook, &ng_rmhook_part2, NULL, 0); 15146b795970SJulian Elischer return (error); 1515954c4772SJulian Elischer } 1516954c4772SJulian Elischer 1517069154d5SJulian Elischer /*********************************************************************** 15184cf49a43SJulian Elischer * Parse and verify a string of the form: <NODE:><PATH> 15194cf49a43SJulian Elischer * 15204cf49a43SJulian Elischer * Such a string can refer to a specific node or a specific hook 15214cf49a43SJulian Elischer * on a specific node, depending on how you look at it. In the 15224cf49a43SJulian Elischer * latter case, the PATH component must not end in a dot. 15234cf49a43SJulian Elischer * 15244cf49a43SJulian Elischer * Both <NODE:> and <PATH> are optional. The <PATH> is a string 15254cf49a43SJulian Elischer * of hook names separated by dots. This breaks out the original 15264cf49a43SJulian Elischer * string, setting *nodep to "NODE" (or NULL if none) and *pathp 15274cf49a43SJulian Elischer * to "PATH" (or NULL if degenerate). Also, *hookp will point to 15284cf49a43SJulian Elischer * the final hook component of <PATH>, if any, otherwise NULL. 15294cf49a43SJulian Elischer * 15304cf49a43SJulian Elischer * This returns -1 if the path is malformed. The char ** are optional. 1531069154d5SJulian Elischer ***********************************************************************/ 15324cf49a43SJulian Elischer int 15334cf49a43SJulian Elischer ng_path_parse(char *addr, char **nodep, char **pathp, char **hookp) 15344cf49a43SJulian Elischer { 15354cf49a43SJulian Elischer char *node, *path, *hook; 15364cf49a43SJulian Elischer int k; 15374cf49a43SJulian Elischer 15384cf49a43SJulian Elischer /* 15394cf49a43SJulian Elischer * Extract absolute NODE, if any 15404cf49a43SJulian Elischer */ 15414cf49a43SJulian Elischer for (path = addr; *path && *path != ':'; path++); 15424cf49a43SJulian Elischer if (*path) { 15434cf49a43SJulian Elischer node = addr; /* Here's the NODE */ 15444cf49a43SJulian Elischer *path++ = '\0'; /* Here's the PATH */ 15454cf49a43SJulian Elischer 15464cf49a43SJulian Elischer /* Node name must not be empty */ 15474cf49a43SJulian Elischer if (!*node) 15484cf49a43SJulian Elischer return -1; 15494cf49a43SJulian Elischer 15504cf49a43SJulian Elischer /* A name of "." is OK; otherwise '.' not allowed */ 15514cf49a43SJulian Elischer if (strcmp(node, ".") != 0) { 15524cf49a43SJulian Elischer for (k = 0; node[k]; k++) 15534cf49a43SJulian Elischer if (node[k] == '.') 15544cf49a43SJulian Elischer return -1; 15554cf49a43SJulian Elischer } 15564cf49a43SJulian Elischer } else { 15574cf49a43SJulian Elischer node = NULL; /* No absolute NODE */ 15584cf49a43SJulian Elischer path = addr; /* Here's the PATH */ 15594cf49a43SJulian Elischer } 15604cf49a43SJulian Elischer 15614cf49a43SJulian Elischer /* Snoop for illegal characters in PATH */ 15624cf49a43SJulian Elischer for (k = 0; path[k]; k++) 15634cf49a43SJulian Elischer if (path[k] == ':') 15644cf49a43SJulian Elischer return -1; 15654cf49a43SJulian Elischer 15664cf49a43SJulian Elischer /* Check for no repeated dots in PATH */ 15674cf49a43SJulian Elischer for (k = 0; path[k]; k++) 15684cf49a43SJulian Elischer if (path[k] == '.' && path[k + 1] == '.') 15694cf49a43SJulian Elischer return -1; 15704cf49a43SJulian Elischer 15714cf49a43SJulian Elischer /* Remove extra (degenerate) dots from beginning or end of PATH */ 15724cf49a43SJulian Elischer if (path[0] == '.') 15734cf49a43SJulian Elischer path++; 15744cf49a43SJulian Elischer if (*path && path[strlen(path) - 1] == '.') 15754cf49a43SJulian Elischer path[strlen(path) - 1] = 0; 15764cf49a43SJulian Elischer 15774cf49a43SJulian Elischer /* If PATH has a dot, then we're not talking about a hook */ 15784cf49a43SJulian Elischer if (*path) { 15794cf49a43SJulian Elischer for (hook = path, k = 0; path[k]; k++) 15804cf49a43SJulian Elischer if (path[k] == '.') { 15814cf49a43SJulian Elischer hook = NULL; 15824cf49a43SJulian Elischer break; 15834cf49a43SJulian Elischer } 15844cf49a43SJulian Elischer } else 15854cf49a43SJulian Elischer path = hook = NULL; 15864cf49a43SJulian Elischer 15874cf49a43SJulian Elischer /* Done */ 15884cf49a43SJulian Elischer if (nodep) 15894cf49a43SJulian Elischer *nodep = node; 15904cf49a43SJulian Elischer if (pathp) 15914cf49a43SJulian Elischer *pathp = path; 15924cf49a43SJulian Elischer if (hookp) 15934cf49a43SJulian Elischer *hookp = hook; 15944cf49a43SJulian Elischer return (0); 15954cf49a43SJulian Elischer } 15964cf49a43SJulian Elischer 15974cf49a43SJulian Elischer /* 15984cf49a43SJulian Elischer * Given a path, which may be absolute or relative, and a starting node, 1599069154d5SJulian Elischer * return the destination node. 16004cf49a43SJulian Elischer */ 16014cf49a43SJulian Elischer int 1602069154d5SJulian Elischer ng_path2noderef(node_p here, const char *address, 1603069154d5SJulian Elischer node_p *destp, hook_p *lasthook) 16044cf49a43SJulian Elischer { 160587e2c66aSHartmut Brandt char fullpath[NG_PATHSIZ]; 16064cf49a43SJulian Elischer char *nodename, *path, pbuf[2]; 1607069154d5SJulian Elischer node_p node, oldnode; 16084cf49a43SJulian Elischer char *cp; 1609a4ec03cfSJulian Elischer hook_p hook = NULL; 16104cf49a43SJulian Elischer 16114cf49a43SJulian Elischer /* Initialize */ 161230400f03SJulian Elischer if (destp == NULL) { 16136b795970SJulian Elischer TRAP_ERROR(); 16144cf49a43SJulian Elischer return EINVAL; 161530400f03SJulian Elischer } 16164cf49a43SJulian Elischer *destp = NULL; 16174cf49a43SJulian Elischer 16184cf49a43SJulian Elischer /* Make a writable copy of address for ng_path_parse() */ 16194cf49a43SJulian Elischer strncpy(fullpath, address, sizeof(fullpath) - 1); 16204cf49a43SJulian Elischer fullpath[sizeof(fullpath) - 1] = '\0'; 16214cf49a43SJulian Elischer 16224cf49a43SJulian Elischer /* Parse out node and sequence of hooks */ 16234cf49a43SJulian Elischer if (ng_path_parse(fullpath, &nodename, &path, NULL) < 0) { 16246b795970SJulian Elischer TRAP_ERROR(); 16254cf49a43SJulian Elischer return EINVAL; 16264cf49a43SJulian Elischer } 16274cf49a43SJulian Elischer if (path == NULL) { 16284cf49a43SJulian Elischer pbuf[0] = '.'; /* Needs to be writable */ 16294cf49a43SJulian Elischer pbuf[1] = '\0'; 16304cf49a43SJulian Elischer path = pbuf; 16314cf49a43SJulian Elischer } 16324cf49a43SJulian Elischer 1633069154d5SJulian Elischer /* 1634069154d5SJulian Elischer * For an absolute address, jump to the starting node. 1635069154d5SJulian Elischer * Note that this holds a reference on the node for us. 1636069154d5SJulian Elischer * Don't forget to drop the reference if we don't need it. 1637069154d5SJulian Elischer */ 16384cf49a43SJulian Elischer if (nodename) { 1639069154d5SJulian Elischer node = ng_name2noderef(here, nodename); 16404cf49a43SJulian Elischer if (node == NULL) { 16416b795970SJulian Elischer TRAP_ERROR(); 16424cf49a43SJulian Elischer return (ENOENT); 16434cf49a43SJulian Elischer } 1644069154d5SJulian Elischer } else { 1645069154d5SJulian Elischer if (here == NULL) { 16466b795970SJulian Elischer TRAP_ERROR(); 1647069154d5SJulian Elischer return (EINVAL); 1648069154d5SJulian Elischer } 16494cf49a43SJulian Elischer node = here; 165030400f03SJulian Elischer NG_NODE_REF(node); 1651069154d5SJulian Elischer } 16524cf49a43SJulian Elischer 1653069154d5SJulian Elischer /* 1654069154d5SJulian Elischer * Now follow the sequence of hooks 1655069154d5SJulian Elischer * XXX 1656069154d5SJulian Elischer * We actually cannot guarantee that the sequence 1657069154d5SJulian Elischer * is not being demolished as we crawl along it 1658069154d5SJulian Elischer * without extra-ordinary locking etc. 1659069154d5SJulian Elischer * So this is a bit dodgy to say the least. 1660069154d5SJulian Elischer * We can probably hold up some things by holding 1661069154d5SJulian Elischer * the nodelist mutex for the time of this 1662069154d5SJulian Elischer * crawl if we wanted.. At least that way we wouldn't have to 166364efc707SRobert Watson * worry about the nodes disappearing, but the hooks would still 1664069154d5SJulian Elischer * be a problem. 1665069154d5SJulian Elischer */ 16664cf49a43SJulian Elischer for (cp = path; node != NULL && *cp != '\0'; ) { 16674cf49a43SJulian Elischer char *segment; 16684cf49a43SJulian Elischer 16694cf49a43SJulian Elischer /* 16704cf49a43SJulian Elischer * Break out the next path segment. Replace the dot we just 16714cf49a43SJulian Elischer * found with a NUL; "cp" points to the next segment (or the 16724cf49a43SJulian Elischer * NUL at the end). 16734cf49a43SJulian Elischer */ 16744cf49a43SJulian Elischer for (segment = cp; *cp != '\0'; cp++) { 16754cf49a43SJulian Elischer if (*cp == '.') { 16764cf49a43SJulian Elischer *cp++ = '\0'; 16774cf49a43SJulian Elischer break; 16784cf49a43SJulian Elischer } 16794cf49a43SJulian Elischer } 16804cf49a43SJulian Elischer 16814cf49a43SJulian Elischer /* Empty segment */ 16824cf49a43SJulian Elischer if (*segment == '\0') 16834cf49a43SJulian Elischer continue; 16844cf49a43SJulian Elischer 16854cf49a43SJulian Elischer /* We have a segment, so look for a hook by that name */ 1686899e9c4eSArchie Cobbs hook = ng_findhook(node, segment); 16874cf49a43SJulian Elischer 16884cf49a43SJulian Elischer /* Can't get there from here... */ 16894cf49a43SJulian Elischer if (hook == NULL 169030400f03SJulian Elischer || NG_HOOK_PEER(hook) == NULL 169130400f03SJulian Elischer || NG_HOOK_NOT_VALID(hook) 169230400f03SJulian Elischer || NG_HOOK_NOT_VALID(NG_HOOK_PEER(hook))) { 16936b795970SJulian Elischer TRAP_ERROR(); 169430400f03SJulian Elischer NG_NODE_UNREF(node); 169530400f03SJulian Elischer #if 0 169630400f03SJulian Elischer printf("hooknotvalid %s %s %d %d %d %d ", 169730400f03SJulian Elischer path, 169830400f03SJulian Elischer segment, 169930400f03SJulian Elischer hook == NULL, 170030400f03SJulian Elischer NG_HOOK_PEER(hook) == NULL, 170130400f03SJulian Elischer NG_HOOK_NOT_VALID(hook), 170230400f03SJulian Elischer NG_HOOK_NOT_VALID(NG_HOOK_PEER(hook))); 170330400f03SJulian Elischer #endif 17044cf49a43SJulian Elischer return (ENOENT); 17054cf49a43SJulian Elischer } 17064cf49a43SJulian Elischer 1707069154d5SJulian Elischer /* 1708069154d5SJulian Elischer * Hop on over to the next node 1709069154d5SJulian Elischer * XXX 1710069154d5SJulian Elischer * Big race conditions here as hooks and nodes go away 1711069154d5SJulian Elischer * *** Idea.. store an ng_ID_t in each hook and use that 1712069154d5SJulian Elischer * instead of the direct hook in this crawl? 1713069154d5SJulian Elischer */ 1714069154d5SJulian Elischer oldnode = node; 171530400f03SJulian Elischer if ((node = NG_PEER_NODE(hook))) 171630400f03SJulian Elischer NG_NODE_REF(node); /* XXX RACE */ 171730400f03SJulian Elischer NG_NODE_UNREF(oldnode); /* XXX another race */ 171830400f03SJulian Elischer if (NG_NODE_NOT_VALID(node)) { 171930400f03SJulian Elischer NG_NODE_UNREF(node); /* XXX more races */ 1720069154d5SJulian Elischer node = NULL; 1721069154d5SJulian Elischer } 17224cf49a43SJulian Elischer } 17234cf49a43SJulian Elischer 17244cf49a43SJulian Elischer /* If node somehow missing, fail here (probably this is not needed) */ 17254cf49a43SJulian Elischer if (node == NULL) { 17266b795970SJulian Elischer TRAP_ERROR(); 17274cf49a43SJulian Elischer return (ENXIO); 17284cf49a43SJulian Elischer } 17294cf49a43SJulian Elischer 17304cf49a43SJulian Elischer /* Done */ 17314cf49a43SJulian Elischer *destp = node; 17321bdebe4dSArchie Cobbs if (lasthook != NULL) 173330400f03SJulian Elischer *lasthook = (hook ? NG_HOOK_PEER(hook) : NULL); 1734069154d5SJulian Elischer return (0); 1735069154d5SJulian Elischer } 1736069154d5SJulian Elischer 1737069154d5SJulian Elischer /***************************************************************\ 1738069154d5SJulian Elischer * Input queue handling. 1739069154d5SJulian Elischer * All activities are submitted to the node via the input queue 1740069154d5SJulian Elischer * which implements a multiple-reader/single-writer gate. 174164efc707SRobert Watson * Items which cannot be handled immediately are queued. 1742069154d5SJulian Elischer * 1743069154d5SJulian Elischer * read-write queue locking inline functions * 1744069154d5SJulian Elischer \***************************************************************/ 1745069154d5SJulian Elischer 1746714fb865SGleb Smirnoff static __inline item_p ng_dequeue(struct ng_queue * ngq, int *rw); 1747069154d5SJulian Elischer static __inline item_p ng_acquire_read(struct ng_queue * ngq, 1748069154d5SJulian Elischer item_p item); 1749069154d5SJulian Elischer static __inline item_p ng_acquire_write(struct ng_queue * ngq, 1750069154d5SJulian Elischer item_p item); 1751069154d5SJulian Elischer static __inline void ng_leave_read(struct ng_queue * ngq); 1752069154d5SJulian Elischer static __inline void ng_leave_write(struct ng_queue * ngq); 1753069154d5SJulian Elischer static __inline void ng_queue_rw(struct ng_queue * ngq, 1754069154d5SJulian Elischer item_p item, int rw); 1755069154d5SJulian Elischer 1756069154d5SJulian Elischer /* 1757069154d5SJulian Elischer * Definition of the bits fields in the ng_queue flag word. 1758069154d5SJulian Elischer * Defined here rather than in netgraph.h because no-one should fiddle 1759069154d5SJulian Elischer * with them. 1760069154d5SJulian Elischer * 1761b57a7965SJulian Elischer * The ordering here may be important! don't shuffle these. 1762069154d5SJulian Elischer */ 1763069154d5SJulian Elischer /*- 1764069154d5SJulian Elischer Safety Barrier--------+ (adjustable to suit taste) (not used yet) 1765069154d5SJulian Elischer | 1766069154d5SJulian Elischer V 1767069154d5SJulian Elischer +-------+-------+-------+-------+-------+-------+-------+-------+ 17684be59335SGleb Smirnoff | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 17694be59335SGleb Smirnoff | |A|c|t|i|v|e| |R|e|a|d|e|r| |C|o|u|n|t| | | | | | | | | |P|A| 17704be59335SGleb Smirnoff | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |O|W| 1771069154d5SJulian Elischer +-------+-------+-------+-------+-------+-------+-------+-------+ 17724be59335SGleb Smirnoff \___________________________ ____________________________/ | | 17734be59335SGleb Smirnoff V | | 17744be59335SGleb Smirnoff [active reader count] | | 1775069154d5SJulian Elischer | | 17764be59335SGleb Smirnoff Operation Pending -------------------------------+ | 1777069154d5SJulian Elischer | 17784be59335SGleb Smirnoff Active Writer ---------------------------------------+ 1779b57a7965SJulian Elischer 1780b57a7965SJulian Elischer 1781069154d5SJulian Elischer */ 17824be59335SGleb Smirnoff #define WRITER_ACTIVE 0x00000001 17834be59335SGleb Smirnoff #define OP_PENDING 0x00000002 17844be59335SGleb Smirnoff #define READER_INCREMENT 0x00000004 17854be59335SGleb Smirnoff #define READER_MASK 0xfffffffc /* Not valid if WRITER_ACTIVE is set */ 17864be59335SGleb Smirnoff #define SAFETY_BARRIER 0x00100000 /* 128K items queued should be enough */ 1787b57a7965SJulian Elischer 1788b57a7965SJulian Elischer /* Defines of more elaborate states on the queue */ 17894be59335SGleb Smirnoff /* Mask of bits a new read cares about */ 17904be59335SGleb Smirnoff #define NGQ_RMASK (WRITER_ACTIVE|OP_PENDING) 1791b57a7965SJulian Elischer 17924be59335SGleb Smirnoff /* Mask of bits a new write cares about */ 1793b57a7965SJulian Elischer #define NGQ_WMASK (NGQ_RMASK|READER_MASK) 1794b57a7965SJulian Elischer 17954be59335SGleb Smirnoff /* Test to decide if there is something on the queue. */ 17964be59335SGleb Smirnoff #define QUEUE_ACTIVE(QP) ((QP)->q_flags & OP_PENDING) 17974be59335SGleb Smirnoff 17984be59335SGleb Smirnoff /* How to decide what the next queued item is. */ 17994be59335SGleb Smirnoff #define HEAD_IS_READER(QP) NGI_QUEUED_READER((QP)->queue) 18004be59335SGleb Smirnoff #define HEAD_IS_WRITER(QP) NGI_QUEUED_WRITER((QP)->queue) /* notused */ 18014be59335SGleb Smirnoff 18024be59335SGleb Smirnoff /* Read the status to decide if the next item on the queue can now run. */ 18034be59335SGleb Smirnoff #define QUEUED_READER_CAN_PROCEED(QP) \ 18044be59335SGleb Smirnoff (((QP)->q_flags & (NGQ_RMASK & ~OP_PENDING)) == 0) 18054be59335SGleb Smirnoff #define QUEUED_WRITER_CAN_PROCEED(QP) \ 18064be59335SGleb Smirnoff (((QP)->q_flags & (NGQ_WMASK & ~OP_PENDING)) == 0) 1807b57a7965SJulian Elischer 1808b57a7965SJulian Elischer /* Is there a chance of getting ANY work off the queue? */ 18094be59335SGleb Smirnoff #define NEXT_QUEUED_ITEM_CAN_PROCEED(QP) \ 18104be59335SGleb Smirnoff (QUEUE_ACTIVE(QP) && \ 18114be59335SGleb Smirnoff ((HEAD_IS_READER(QP)) ? QUEUED_READER_CAN_PROCEED(QP) : \ 18124be59335SGleb Smirnoff QUEUED_WRITER_CAN_PROCEED(QP))) 18134be59335SGleb Smirnoff 1814b57a7965SJulian Elischer 1815714fb865SGleb Smirnoff #define NGQRW_R 0 1816714fb865SGleb Smirnoff #define NGQRW_W 1 1817714fb865SGleb Smirnoff 1818069154d5SJulian Elischer /* 1819069154d5SJulian Elischer * Taking into account the current state of the queue and node, possibly take 1820069154d5SJulian Elischer * the next entry off the queue and return it. Return NULL if there was 1821069154d5SJulian Elischer * nothing we could return, either because there really was nothing there, or 1822069154d5SJulian Elischer * because the node was in a state where it cannot yet process the next item 1823069154d5SJulian Elischer * on the queue. 1824069154d5SJulian Elischer * 1825069154d5SJulian Elischer * This MUST MUST MUST be called with the mutex held. 1826069154d5SJulian Elischer */ 1827069154d5SJulian Elischer static __inline item_p 1828714fb865SGleb Smirnoff ng_dequeue(struct ng_queue *ngq, int *rw) 1829069154d5SJulian Elischer { 1830069154d5SJulian Elischer item_p item; 1831069154d5SJulian Elischer u_int add_arg; 1832b57a7965SJulian Elischer 1833d58e678fSGleb Smirnoff mtx_assert(&ngq->q_mtx, MA_OWNED); 18344be59335SGleb Smirnoff /* 18354be59335SGleb Smirnoff * If there is nothing queued, then just return. 18364be59335SGleb Smirnoff * No point in continuing. 18374be59335SGleb Smirnoff * XXXGL: assert this? 18384be59335SGleb Smirnoff */ 18394be59335SGleb Smirnoff if (!QUEUE_ACTIVE(ngq)) { 18402955ee18SGleb Smirnoff CTR4(KTR_NET, "%20s: node [%x] (%p) queue empty; " 18412955ee18SGleb Smirnoff "queue flags 0x%lx", __func__, 18422955ee18SGleb Smirnoff ngq->q_node->nd_ID, ngq->q_node, ngq->q_flags); 18434be59335SGleb Smirnoff return (NULL); 18444be59335SGleb Smirnoff } 1845d58e678fSGleb Smirnoff 18464be59335SGleb Smirnoff /* 18474be59335SGleb Smirnoff * From here, we can assume there is a head item. 18484be59335SGleb Smirnoff * We need to find out what it is and if it can be dequeued, given 18494be59335SGleb Smirnoff * the current state of the node. 18504be59335SGleb Smirnoff */ 18514be59335SGleb Smirnoff if (HEAD_IS_READER(ngq)) { 18524be59335SGleb Smirnoff if (!QUEUED_READER_CAN_PROCEED(ngq)) { 18534be59335SGleb Smirnoff /* 18544be59335SGleb Smirnoff * It's a reader but we can't use it. 18554be59335SGleb Smirnoff * We are stalled so make sure we don't 18564be59335SGleb Smirnoff * get called again until something changes. 18574be59335SGleb Smirnoff */ 18584be59335SGleb Smirnoff ng_worklist_remove(ngq->q_node); 18592955ee18SGleb Smirnoff CTR4(KTR_NET, "%20s: node [%x] (%p) queued reader " 18602955ee18SGleb Smirnoff "can't proceed; queue flags 0x%lx", __func__, 18612955ee18SGleb Smirnoff ngq->q_node->nd_ID, ngq->q_node, ngq->q_flags); 18624be59335SGleb Smirnoff return (NULL); 18634be59335SGleb Smirnoff } 1864069154d5SJulian Elischer /* 1865b57a7965SJulian Elischer * Head of queue is a reader and we have no write active. 1866b57a7965SJulian Elischer * We don't care how many readers are already active. 18674be59335SGleb Smirnoff * Add the correct increment for the reader count. 1868069154d5SJulian Elischer */ 18694be59335SGleb Smirnoff add_arg = READER_INCREMENT; 1870714fb865SGleb Smirnoff *rw = NGQRW_R; 18714be59335SGleb Smirnoff } else if (QUEUED_WRITER_CAN_PROCEED(ngq)) { 1872069154d5SJulian Elischer /* 1873069154d5SJulian Elischer * There is a pending write, no readers and no active writer. 1874069154d5SJulian Elischer * This means we can go ahead with the pending writer. Note 1875069154d5SJulian Elischer * the fact that we now have a writer, ready for when we take 1876069154d5SJulian Elischer * it off the queue. 1877069154d5SJulian Elischer * 1878069154d5SJulian Elischer * We don't need to worry about a possible collision with the 1879069154d5SJulian Elischer * fasttrack reader. 1880069154d5SJulian Elischer * 1881069154d5SJulian Elischer * The fasttrack thread may take a long time to discover that we 1882069154d5SJulian Elischer * are running so we would have an inconsistent state in the 1883069154d5SJulian Elischer * flags for a while. Since we ignore the reader count 1884069154d5SJulian Elischer * entirely when the WRITER_ACTIVE flag is set, this should 1885069154d5SJulian Elischer * not matter (in fact it is defined that way). If it tests 18864be59335SGleb Smirnoff * the flag before this operation, the OP_PENDING flag 1887069154d5SJulian Elischer * will make it fail, and if it tests it later, the 1888b57a7965SJulian Elischer * WRITER_ACTIVE flag will do the same. If it is SO slow that 1889069154d5SJulian Elischer * we have actually completed the operation, and neither flag 18904be59335SGleb Smirnoff * is set by the time that it tests the flags, then it is 18914be59335SGleb Smirnoff * actually ok for it to continue. If it completes and we've 18924be59335SGleb Smirnoff * finished and the read pending is set it still fails. 1893069154d5SJulian Elischer * 1894069154d5SJulian Elischer * So we can just ignore it, as long as we can ensure that the 1895069154d5SJulian Elischer * transition from WRITE_PENDING state to the WRITER_ACTIVE 1896069154d5SJulian Elischer * state is atomic. 1897069154d5SJulian Elischer * 1898069154d5SJulian Elischer * After failing, first it will be held back by the mutex, then 1899069154d5SJulian Elischer * when it can proceed, it will queue its request, then it 1900069154d5SJulian Elischer * would arrive at this function. Usually it will have to 1901b57a7965SJulian Elischer * leave empty handed because the ACTIVE WRITER bit will be 1902069154d5SJulian Elischer * set. 1903b57a7965SJulian Elischer * 19044be59335SGleb Smirnoff * Adjust the flags for the new active writer. 1905069154d5SJulian Elischer */ 19064be59335SGleb Smirnoff add_arg = WRITER_ACTIVE; 1907714fb865SGleb Smirnoff *rw = NGQRW_W; 1908069154d5SJulian Elischer /* 1909069154d5SJulian Elischer * We want to write "active writer, no readers " Now go make 1910069154d5SJulian Elischer * it true. In fact there may be a number in the readers 1911069154d5SJulian Elischer * count but we know it is not true and will be fixed soon. 1912069154d5SJulian Elischer * We will fix the flags for the next pending entry in a 1913069154d5SJulian Elischer * moment. 1914069154d5SJulian Elischer */ 1915069154d5SJulian Elischer } else { 1916069154d5SJulian Elischer /* 1917069154d5SJulian Elischer * We can't dequeue anything.. return and say so. Probably we 1918069154d5SJulian Elischer * have a write pending and the readers count is non zero. If 1919069154d5SJulian Elischer * we got here because a reader hit us just at the wrong 1920069154d5SJulian Elischer * moment with the fasttrack code, and put us in a strange 19214be59335SGleb Smirnoff * state, then it will be coming through in just a moment, 19224be59335SGleb Smirnoff * (just as soon as we release the mutex) and keep things 19234be59335SGleb Smirnoff * moving. 19244be59335SGleb Smirnoff * Make sure we remove ourselves from the work queue. It 19254be59335SGleb Smirnoff * would be a waste of effort to do all this again. 1926069154d5SJulian Elischer */ 1927b57a7965SJulian Elischer ng_worklist_remove(ngq->q_node); 19282955ee18SGleb Smirnoff CTR4(KTR_NET, "%20s: node [%x] (%p) can't dequeue anything; " 19292955ee18SGleb Smirnoff "queue flags 0x%lx", __func__, 19302955ee18SGleb Smirnoff ngq->q_node->nd_ID, ngq->q_node, ngq->q_flags); 19314be59335SGleb Smirnoff return (NULL); 19324cf49a43SJulian Elischer } 19334cf49a43SJulian Elischer 19344cf49a43SJulian Elischer /* 1935069154d5SJulian Elischer * Now we dequeue the request (whatever it may be) and correct the 1936069154d5SJulian Elischer * pending flags and the next and last pointers. 19374cf49a43SJulian Elischer */ 1938069154d5SJulian Elischer item = ngq->queue; 1939069154d5SJulian Elischer ngq->queue = item->el_next; 19402955ee18SGleb Smirnoff CTR6(KTR_NET, "%20s: node [%x] (%p) dequeued item %p with flags 0x%lx; " 19412955ee18SGleb Smirnoff "queue flags 0x%lx", __func__, 19422955ee18SGleb Smirnoff ngq->q_node->nd_ID,ngq->q_node, item, item->el_flags, ngq->q_flags); 1943069154d5SJulian Elischer if (ngq->last == &(item->el_next)) { 19444cf49a43SJulian Elischer /* 1945069154d5SJulian Elischer * that was the last entry in the queue so set the 'last 19464be59335SGleb Smirnoff * pointer up correctly and make sure the pending flag is 1947069154d5SJulian Elischer * clear. 19484cf49a43SJulian Elischer */ 19494be59335SGleb Smirnoff add_arg += -OP_PENDING; 1950069154d5SJulian Elischer ngq->last = &(ngq->queue); 1951859a4d16SJulian Elischer /* 1952b57a7965SJulian Elischer * Whatever flag was set will be cleared and 1953b57a7965SJulian Elischer * the new acive field will be set by the add as well, 1954b57a7965SJulian Elischer * so we don't need to change add_arg. 1955b57a7965SJulian Elischer * But we know we don't need to be on the work list. 1956859a4d16SJulian Elischer */ 1957b57a7965SJulian Elischer atomic_add_long(&ngq->q_flags, add_arg); 1958b57a7965SJulian Elischer ng_worklist_remove(ngq->q_node); 1959859a4d16SJulian Elischer } else { 1960069154d5SJulian Elischer /* 19614be59335SGleb Smirnoff * Since there is still something on the queue 19624be59335SGleb Smirnoff * we don't need to change the PENDING flag. 1963069154d5SJulian Elischer */ 1964069154d5SJulian Elischer atomic_add_long(&ngq->q_flags, add_arg); 1965069154d5SJulian Elischer /* 1966b57a7965SJulian Elischer * If we see more doable work, make sure we are 1967b57a7965SJulian Elischer * on the work queue. 1968b57a7965SJulian Elischer */ 19694be59335SGleb Smirnoff if (NEXT_QUEUED_ITEM_CAN_PROCEED(ngq)) { 1970b57a7965SJulian Elischer ng_setisr(ngq->q_node); 1971b57a7965SJulian Elischer } 1972b57a7965SJulian Elischer } 19732955ee18SGleb Smirnoff CTR6(KTR_NET, "%20s: node [%x] (%p) returning item %p as %s; " 19742955ee18SGleb Smirnoff "queue flags 0x%lx", __func__, 19752955ee18SGleb Smirnoff ngq->q_node->nd_ID, ngq->q_node, item, *rw ? "WRITER" : "READER" , 19763b33fbe7SGleb Smirnoff ngq->q_flags); 1977069154d5SJulian Elischer return (item); 1978069154d5SJulian Elischer } 1979859a4d16SJulian Elischer 1980859a4d16SJulian Elischer /* 1981069154d5SJulian Elischer * Queue a packet to be picked up by someone else. 1982069154d5SJulian Elischer * We really don't care who, but we can't or don't want to hang around 1983069154d5SJulian Elischer * to process it ourselves. We are probably an interrupt routine.. 19844be59335SGleb Smirnoff * If the queue could be run, flag the netisr handler to start. 1985859a4d16SJulian Elischer */ 1986069154d5SJulian Elischer static __inline void 1987069154d5SJulian Elischer ng_queue_rw(struct ng_queue * ngq, item_p item, int rw) 1988069154d5SJulian Elischer { 1989d58e678fSGleb Smirnoff mtx_assert(&ngq->q_mtx, MA_OWNED); 1990d58e678fSGleb Smirnoff 19914be59335SGleb Smirnoff if (rw == NGQRW_W) 19924be59335SGleb Smirnoff NGI_SET_WRITER(item); 19934be59335SGleb Smirnoff else 19944be59335SGleb Smirnoff NGI_SET_READER(item); 1995069154d5SJulian Elischer item->el_next = NULL; /* maybe not needed */ 1996069154d5SJulian Elischer *ngq->last = item; 19972955ee18SGleb Smirnoff CTR5(KTR_NET, "%20s: node [%x] (%p) queued item %p as %s", __func__, 19982955ee18SGleb Smirnoff ngq->q_node->nd_ID, ngq->q_node, item, rw ? "WRITER" : "READER" ); 1999069154d5SJulian Elischer /* 2000069154d5SJulian Elischer * If it was the first item in the queue then we need to 2001069154d5SJulian Elischer * set the last pointer and the type flags. 2002069154d5SJulian Elischer */ 20033b33fbe7SGleb Smirnoff if (ngq->last == &(ngq->queue)) { 20044be59335SGleb Smirnoff atomic_add_long(&ngq->q_flags, OP_PENDING); 20052955ee18SGleb Smirnoff CTR3(KTR_NET, "%20s: node [%x] (%p) set OP_PENDING", __func__, 20062955ee18SGleb Smirnoff ngq->q_node->nd_ID, ngq->q_node); 20073b33fbe7SGleb Smirnoff } 20084be59335SGleb Smirnoff 2009069154d5SJulian Elischer ngq->last = &(item->el_next); 20104be59335SGleb Smirnoff /* 20114be59335SGleb Smirnoff * We can take the worklist lock with the node locked 20124be59335SGleb Smirnoff * BUT NOT THE REVERSE! 20134be59335SGleb Smirnoff */ 20144be59335SGleb Smirnoff if (NEXT_QUEUED_ITEM_CAN_PROCEED(ngq)) 20154be59335SGleb Smirnoff ng_setisr(ngq->q_node); 2016069154d5SJulian Elischer } 2017069154d5SJulian Elischer 2018069154d5SJulian Elischer 2019069154d5SJulian Elischer /* 2020069154d5SJulian Elischer * This function 'cheats' in that it first tries to 'grab' the use of the 2021069154d5SJulian Elischer * node, without going through the mutex. We can do this becasue of the 2022069154d5SJulian Elischer * semantics of the lock. The semantics include a clause that says that the 2023069154d5SJulian Elischer * value of the readers count is invalid if the WRITER_ACTIVE flag is set. It 2024069154d5SJulian Elischer * also says that the WRITER_ACTIVE flag cannot be set if the readers count 2025069154d5SJulian Elischer * is not zero. Note that this talks about what is valid to SET the 2026069154d5SJulian Elischer * WRITER_ACTIVE flag, because from the moment it is set, the value if the 2027069154d5SJulian Elischer * reader count is immaterial, and not valid. The two 'pending' flags have a 2028069154d5SJulian Elischer * similar effect, in that If they are orthogonal to the two active fields in 2029069154d5SJulian Elischer * how they are set, but if either is set, the attempted 'grab' need to be 2030069154d5SJulian Elischer * backed out because there is earlier work, and we maintain ordering in the 2031069154d5SJulian Elischer * queue. The result of this is that the reader request can try obtain use of 2032069154d5SJulian Elischer * the node with only a single atomic addition, and without any of the mutex 2033069154d5SJulian Elischer * overhead. If this fails the operation degenerates to the same as for other 2034069154d5SJulian Elischer * cases. 2035069154d5SJulian Elischer * 2036069154d5SJulian Elischer */ 2037069154d5SJulian Elischer static __inline item_p 2038069154d5SJulian Elischer ng_acquire_read(struct ng_queue *ngq, item_p item) 2039069154d5SJulian Elischer { 2040ac5dd141SGleb Smirnoff KASSERT(ngq != &ng_deadnode.nd_input_queue, 2041ac5dd141SGleb Smirnoff ("%s: working on deadnode", __func__)); 2042069154d5SJulian Elischer 2043069154d5SJulian Elischer /* ######### Hack alert ######### */ 2044069154d5SJulian Elischer atomic_add_long(&ngq->q_flags, READER_INCREMENT); 2045b57a7965SJulian Elischer if ((ngq->q_flags & NGQ_RMASK) == 0) { 2046069154d5SJulian Elischer /* Successfully grabbed node */ 20472955ee18SGleb Smirnoff CTR4(KTR_NET, "%20s: node [%x] (%p) fast acquired item %p", 20482955ee18SGleb Smirnoff __func__, ngq->q_node->nd_ID, ngq->q_node, item); 2049069154d5SJulian Elischer return (item); 2050069154d5SJulian Elischer } 2051069154d5SJulian Elischer /* undo the damage if we didn't succeed */ 2052069154d5SJulian Elischer atomic_subtract_long(&ngq->q_flags, READER_INCREMENT); 2053069154d5SJulian Elischer 2054069154d5SJulian Elischer /* ######### End Hack alert ######### */ 20552c8dda8dSWojciech A. Koszek NG_QUEUE_LOCK(ngq); 2056069154d5SJulian Elischer /* 2057069154d5SJulian Elischer * Try again. Another processor (or interrupt for that matter) may 2058069154d5SJulian Elischer * have removed the last queued item that was stopping us from 2059069154d5SJulian Elischer * running, between the previous test, and the moment that we took 2060069154d5SJulian Elischer * the mutex. (Or maybe a writer completed.) 20614be59335SGleb Smirnoff * Even if another fast-track reader hits during this period 20624be59335SGleb Smirnoff * we don't care as multiple readers is OK. 2063069154d5SJulian Elischer */ 2064b57a7965SJulian Elischer if ((ngq->q_flags & NGQ_RMASK) == 0) { 2065069154d5SJulian Elischer atomic_add_long(&ngq->q_flags, READER_INCREMENT); 20662c8dda8dSWojciech A. Koszek NG_QUEUE_UNLOCK(ngq); 20672955ee18SGleb Smirnoff CTR4(KTR_NET, "%20s: node [%x] (%p) slow acquired item %p", 20682955ee18SGleb Smirnoff __func__, ngq->q_node->nd_ID, ngq->q_node, item); 2069069154d5SJulian Elischer return (item); 2070069154d5SJulian Elischer } 2071069154d5SJulian Elischer 2072069154d5SJulian Elischer /* 2073069154d5SJulian Elischer * and queue the request for later. 2074069154d5SJulian Elischer */ 2075069154d5SJulian Elischer ng_queue_rw(ngq, item, NGQRW_R); 20762c8dda8dSWojciech A. Koszek NG_QUEUE_UNLOCK(ngq); 2077f912c0f7SGleb Smirnoff 2078f912c0f7SGleb Smirnoff return (NULL); 2079069154d5SJulian Elischer } 2080069154d5SJulian Elischer 2081069154d5SJulian Elischer static __inline item_p 2082069154d5SJulian Elischer ng_acquire_write(struct ng_queue *ngq, item_p item) 2083069154d5SJulian Elischer { 2084ac5dd141SGleb Smirnoff KASSERT(ngq != &ng_deadnode.nd_input_queue, 2085ac5dd141SGleb Smirnoff ("%s: working on deadnode", __func__)); 2086ac5dd141SGleb Smirnoff 2087069154d5SJulian Elischer restart: 20882c8dda8dSWojciech A. Koszek NG_QUEUE_LOCK(ngq); 2089069154d5SJulian Elischer /* 2090069154d5SJulian Elischer * If there are no readers, no writer, and no pending packets, then 2091069154d5SJulian Elischer * we can just go ahead. In all other situations we need to queue the 2092069154d5SJulian Elischer * request 2093069154d5SJulian Elischer */ 2094b57a7965SJulian Elischer if ((ngq->q_flags & NGQ_WMASK) == 0) { 20954be59335SGleb Smirnoff /* collision could happen *HERE* */ 2096069154d5SJulian Elischer atomic_add_long(&ngq->q_flags, WRITER_ACTIVE); 20972c8dda8dSWojciech A. Koszek NG_QUEUE_UNLOCK(ngq); 2098069154d5SJulian Elischer if (ngq->q_flags & READER_MASK) { 2099069154d5SJulian Elischer /* Collision with fast-track reader */ 2100b57a7965SJulian Elischer atomic_subtract_long(&ngq->q_flags, WRITER_ACTIVE); 2101069154d5SJulian Elischer goto restart; 2102069154d5SJulian Elischer } 21032955ee18SGleb Smirnoff CTR4(KTR_NET, "%20s: node [%x] (%p) acquired item %p", 21042955ee18SGleb Smirnoff __func__, ngq->q_node->nd_ID, ngq->q_node, item); 2105069154d5SJulian Elischer return (item); 2106069154d5SJulian Elischer } 2107069154d5SJulian Elischer 2108069154d5SJulian Elischer /* 2109069154d5SJulian Elischer * and queue the request for later. 2110069154d5SJulian Elischer */ 2111069154d5SJulian Elischer ng_queue_rw(ngq, item, NGQRW_W); 21122c8dda8dSWojciech A. Koszek NG_QUEUE_UNLOCK(ngq); 2113f912c0f7SGleb Smirnoff 2114f912c0f7SGleb Smirnoff return (NULL); 2115069154d5SJulian Elischer } 2116069154d5SJulian Elischer 2117262dfd7fSJulian Elischer #if 0 2118262dfd7fSJulian Elischer static __inline item_p 2119262dfd7fSJulian Elischer ng_upgrade_write(struct ng_queue *ngq, item_p item) 2120262dfd7fSJulian Elischer { 2121262dfd7fSJulian Elischer KASSERT(ngq != &ng_deadnode.nd_input_queue, 2122262dfd7fSJulian Elischer ("%s: working on deadnode", __func__)); 2123262dfd7fSJulian Elischer 2124262dfd7fSJulian Elischer NGI_SET_WRITER(item); 2125262dfd7fSJulian Elischer 2126262dfd7fSJulian Elischer mtx_lock_spin(&(ngq->q_mtx)); 2127262dfd7fSJulian Elischer 2128262dfd7fSJulian Elischer /* 2129262dfd7fSJulian Elischer * There will never be no readers as we are there ourselves. 2130262dfd7fSJulian Elischer * Set the WRITER_ACTIVE flags ASAP to block out fast track readers. 2131262dfd7fSJulian Elischer * The caller we are running from will call ng_leave_read() 2132262dfd7fSJulian Elischer * soon, so we must account for that. We must leave again with the 2133262dfd7fSJulian Elischer * READER lock. If we find other readers, then 2134262dfd7fSJulian Elischer * queue the request for later. However "later" may be rignt now 2135262dfd7fSJulian Elischer * if there are no readers. We don't really care if there are queued 2136262dfd7fSJulian Elischer * items as we will bypass them anyhow. 2137262dfd7fSJulian Elischer */ 2138262dfd7fSJulian Elischer atomic_add_long(&ngq->q_flags, WRITER_ACTIVE - READER_INCREMENT); 2139262dfd7fSJulian Elischer if (ngq->q_flags & (NGQ_WMASK & ~OP_PENDING) == WRITER_ACTIVE) { 2140262dfd7fSJulian Elischer mtx_unlock_spin(&(ngq->q_mtx)); 2141262dfd7fSJulian Elischer 2142262dfd7fSJulian Elischer /* It's just us, act on the item. */ 2143262dfd7fSJulian Elischer /* will NOT drop writer lock when done */ 2144262dfd7fSJulian Elischer ng_apply_item(node, item, 0); 2145262dfd7fSJulian Elischer 2146262dfd7fSJulian Elischer /* 2147262dfd7fSJulian Elischer * Having acted on the item, atomically 2148262dfd7fSJulian Elischer * down grade back to READER and finish up 2149262dfd7fSJulian Elischer */ 2150262dfd7fSJulian Elischer atomic_add_long(&ngq->q_flags, 2151262dfd7fSJulian Elischer READER_INCREMENT - WRITER_ACTIVE); 2152262dfd7fSJulian Elischer 2153262dfd7fSJulian Elischer /* Our caller will call ng_leave_read() */ 2154262dfd7fSJulian Elischer return; 2155262dfd7fSJulian Elischer } 2156262dfd7fSJulian Elischer /* 2157262dfd7fSJulian Elischer * It's not just us active, so queue us AT THE HEAD. 2158262dfd7fSJulian Elischer * "Why?" I hear you ask. 2159262dfd7fSJulian Elischer * Put us at the head of the queue as we've already been 2160262dfd7fSJulian Elischer * through it once. If there is nothing else waiting, 2161262dfd7fSJulian Elischer * set the correct flags. 2162262dfd7fSJulian Elischer */ 2163262dfd7fSJulian Elischer if ((item->el_next = ngq->queue) == NULL) { 2164262dfd7fSJulian Elischer /* 2165262dfd7fSJulian Elischer * Set up the "last" pointer. 2166262dfd7fSJulian Elischer * We are the only (and thus last) item 2167262dfd7fSJulian Elischer */ 2168262dfd7fSJulian Elischer ngq->last = &(item->el_next); 2169262dfd7fSJulian Elischer 2170262dfd7fSJulian Elischer /* We've gone from, 0 to 1 item in the queue */ 2171262dfd7fSJulian Elischer atomic_add_long(&ngq->q_flags, OP_PENDING); 2172262dfd7fSJulian Elischer 2173262dfd7fSJulian Elischer CTR3(KTR_NET, "%20s: node [%x] (%p) set OP_PENDING", __func__, 2174262dfd7fSJulian Elischer ngq->q_node->nd_ID, ngq->q_node); 2175262dfd7fSJulian Elischer }; 2176262dfd7fSJulian Elischer ngq->queue = item; 2177262dfd7fSJulian Elischer CTR5(KTR_NET, "%20s: node [%x] (%p) requeued item %p as WRITER", 2178262dfd7fSJulian Elischer __func__, ngq->q_node->nd_ID, ngq->q_node, item ); 2179262dfd7fSJulian Elischer 2180262dfd7fSJulian Elischer /* Reverse what we did above. That downgrades us back to reader */ 2181262dfd7fSJulian Elischer atomic_add_long(&ngq->q_flags, READER_INCREMENT - WRITER_ACTIVE); 2182262dfd7fSJulian Elischer if (NEXT_QUEUED_ITEM_CAN_PROCEED(ngq)) 2183262dfd7fSJulian Elischer ng_setisr(ngq->q_node); 2184262dfd7fSJulian Elischer mtx_unlock_spin(&(ngq->q_mtx)); 2185262dfd7fSJulian Elischer 2186262dfd7fSJulian Elischer return; 2187262dfd7fSJulian Elischer } 2188262dfd7fSJulian Elischer 2189262dfd7fSJulian Elischer #endif 2190262dfd7fSJulian Elischer 2191069154d5SJulian Elischer static __inline void 2192069154d5SJulian Elischer ng_leave_read(struct ng_queue *ngq) 2193069154d5SJulian Elischer { 2194069154d5SJulian Elischer atomic_subtract_long(&ngq->q_flags, READER_INCREMENT); 2195069154d5SJulian Elischer } 2196069154d5SJulian Elischer 2197069154d5SJulian Elischer static __inline void 2198069154d5SJulian Elischer ng_leave_write(struct ng_queue *ngq) 2199069154d5SJulian Elischer { 2200069154d5SJulian Elischer atomic_subtract_long(&ngq->q_flags, WRITER_ACTIVE); 2201069154d5SJulian Elischer } 2202069154d5SJulian Elischer 2203069154d5SJulian Elischer static void 2204069154d5SJulian Elischer ng_flush_input_queue(struct ng_queue * ngq) 2205069154d5SJulian Elischer { 2206069154d5SJulian Elischer item_p item; 2207069154d5SJulian Elischer 22082c8dda8dSWojciech A. Koszek NG_QUEUE_LOCK(ngq); 22094be59335SGleb Smirnoff while (ngq->queue) { 2210069154d5SJulian Elischer item = ngq->queue; 2211069154d5SJulian Elischer ngq->queue = item->el_next; 2212069154d5SJulian Elischer if (ngq->last == &(item->el_next)) { 2213069154d5SJulian Elischer ngq->last = &(ngq->queue); 22144be59335SGleb Smirnoff atomic_add_long(&ngq->q_flags, -OP_PENDING); 2215069154d5SJulian Elischer } 22162c8dda8dSWojciech A. Koszek NG_QUEUE_UNLOCK(ngq); 22174be59335SGleb Smirnoff 22184be59335SGleb Smirnoff /* If the item is supplying a callback, call it with an error */ 2219eb2405ddSGleb Smirnoff if (item->apply != NULL) { 2220eb2405ddSGleb Smirnoff (item->apply)(item->context, ENOENT); 2221eb2405ddSGleb Smirnoff item->apply = NULL; 2222eb2405ddSGleb Smirnoff } 2223505fad52SJulian Elischer NG_FREE_ITEM(item); 22242c8dda8dSWojciech A. Koszek NG_QUEUE_LOCK(ngq); 2225069154d5SJulian Elischer } 2226b57a7965SJulian Elischer /* 2227b57a7965SJulian Elischer * Take us off the work queue if we are there. 222864efc707SRobert Watson * We definately have no work to be done. 2229b57a7965SJulian Elischer */ 2230b57a7965SJulian Elischer ng_worklist_remove(ngq->q_node); 22312c8dda8dSWojciech A. Koszek NG_QUEUE_UNLOCK(ngq); 2232069154d5SJulian Elischer } 2233069154d5SJulian Elischer 2234069154d5SJulian Elischer /*********************************************************************** 2235069154d5SJulian Elischer * Externally visible method for sending or queueing messages or data. 2236069154d5SJulian Elischer ***********************************************************************/ 2237069154d5SJulian Elischer 2238069154d5SJulian Elischer /* 22391acb27c6SJulian Elischer * The module code should have filled out the item correctly by this stage: 2240069154d5SJulian Elischer * Common: 2241069154d5SJulian Elischer * reference to destination node. 2242069154d5SJulian Elischer * Reference to destination rcv hook if relevant. 2243069154d5SJulian Elischer * Data: 2244069154d5SJulian Elischer * pointer to mbuf 2245069154d5SJulian Elischer * Control_Message: 2246069154d5SJulian Elischer * pointer to msg. 2247069154d5SJulian Elischer * ID of original sender node. (return address) 22481acb27c6SJulian Elischer * Function: 22491acb27c6SJulian Elischer * Function pointer 22501acb27c6SJulian Elischer * void * argument 22511acb27c6SJulian Elischer * integer argument 2252069154d5SJulian Elischer * 2253069154d5SJulian Elischer * The nodes have several routines and macros to help with this task: 2254069154d5SJulian Elischer */ 2255069154d5SJulian Elischer 2256069154d5SJulian Elischer int 225742282202SGleb Smirnoff ng_snd_item(item_p item, int flags) 2258069154d5SJulian Elischer { 22591acb27c6SJulian Elischer hook_p hook = NGI_HOOK(item); 2260b57a7965SJulian Elischer node_p node = NGI_NODE(item); 226142282202SGleb Smirnoff int queue, rw; 2262b57a7965SJulian Elischer struct ng_queue * ngq = &node->nd_input_queue; 226332b33288SGleb Smirnoff int error = 0; 2264069154d5SJulian Elischer 226530400f03SJulian Elischer #ifdef NETGRAPH_DEBUG 2266069154d5SJulian Elischer _ngi_check(item, __FILE__, __LINE__); 2267069154d5SJulian Elischer #endif 2268069154d5SJulian Elischer 226942282202SGleb Smirnoff queue = (flags & NG_QUEUE) ? 1 : 0; 227042282202SGleb Smirnoff 2271069154d5SJulian Elischer if (item == NULL) { 22726b795970SJulian Elischer TRAP_ERROR(); 2273069154d5SJulian Elischer return (EINVAL); /* failed to get queue element */ 2274069154d5SJulian Elischer } 2275b57a7965SJulian Elischer if (node == NULL) { 2276069154d5SJulian Elischer NG_FREE_ITEM(item); 22776b795970SJulian Elischer TRAP_ERROR(); 2278069154d5SJulian Elischer return (EINVAL); /* No address */ 2279069154d5SJulian Elischer } 22806b795970SJulian Elischer switch(item->el_flags & NGQF_TYPE) { 22816b795970SJulian Elischer case NGQF_DATA: 2282069154d5SJulian Elischer /* 2283069154d5SJulian Elischer * DATA MESSAGE 2284069154d5SJulian Elischer * Delivered to a node via a non-optional hook. 2285069154d5SJulian Elischer * Both should be present in the item even though 2286069154d5SJulian Elischer * the node is derivable from the hook. 2287069154d5SJulian Elischer * References are held on both by the item. 2288069154d5SJulian Elischer */ 22891cf3fa79SJulian Elischer 22901cf3fa79SJulian Elischer /* Protect nodes from sending NULL pointers 22911cf3fa79SJulian Elischer * to each other 22921cf3fa79SJulian Elischer */ 229310204449SJulian Elischer if (NGI_M(item) == NULL) 22941cf3fa79SJulian Elischer return (EINVAL); 22951cf3fa79SJulian Elischer 2296069154d5SJulian Elischer CHECK_DATA_MBUF(NGI_M(item)); 2297069154d5SJulian Elischer if (hook == NULL) { 2298069154d5SJulian Elischer NG_FREE_ITEM(item); 22996b795970SJulian Elischer TRAP_ERROR(); 2300069154d5SJulian Elischer return(EINVAL); 2301069154d5SJulian Elischer } 230230400f03SJulian Elischer if ((NG_HOOK_NOT_VALID(hook)) 230330400f03SJulian Elischer || (NG_NODE_NOT_VALID(NG_HOOK_NODE(hook)))) { 2304069154d5SJulian Elischer NG_FREE_ITEM(item); 2305069154d5SJulian Elischer return (ENOTCONN); 2306859a4d16SJulian Elischer } 230730400f03SJulian Elischer if ((hook->hk_flags & HK_QUEUE)) { 2308069154d5SJulian Elischer queue = 1; 23094cf49a43SJulian Elischer } 23106b795970SJulian Elischer break; 23116b795970SJulian Elischer case NGQF_MESG: 23124cf49a43SJulian Elischer /* 2313069154d5SJulian Elischer * CONTROL MESSAGE 2314069154d5SJulian Elischer * Delivered to a node. 2315069154d5SJulian Elischer * Hook is optional. 2316069154d5SJulian Elischer * References are held by the item on the node and 2317069154d5SJulian Elischer * the hook if it is present. 23184cf49a43SJulian Elischer */ 231930400f03SJulian Elischer if (hook && (hook->hk_flags & HK_QUEUE)) { 2320069154d5SJulian Elischer queue = 1; 2321069154d5SJulian Elischer } 23226b795970SJulian Elischer break; 23236b795970SJulian Elischer case NGQF_FN: 23246b795970SJulian Elischer break; 23256b795970SJulian Elischer default: 23266b795970SJulian Elischer NG_FREE_ITEM(item); 23276b795970SJulian Elischer TRAP_ERROR(); 23286b795970SJulian Elischer return (EINVAL); 2329069154d5SJulian Elischer } 23306f683eeeSGleb Smirnoff switch(item->el_flags & NGQF_RW) { 23316f683eeeSGleb Smirnoff case NGQF_READER: 23326f683eeeSGleb Smirnoff rw = NGQRW_R; 23336f683eeeSGleb Smirnoff break; 23346f683eeeSGleb Smirnoff case NGQF_WRITER: 23356f683eeeSGleb Smirnoff rw = NGQRW_W; 23366f683eeeSGleb Smirnoff break; 23376f683eeeSGleb Smirnoff default: 23386f683eeeSGleb Smirnoff panic("%s: invalid item flags %lx", __func__, item->el_flags); 23396f683eeeSGleb Smirnoff } 23406f683eeeSGleb Smirnoff 2341069154d5SJulian Elischer /* 23426f683eeeSGleb Smirnoff * If the node specifies single threading, force writer semantics. 23436f683eeeSGleb Smirnoff * Similarly, the node may say one hook always produces writers. 2344714fb865SGleb Smirnoff * These are overrides. 2345069154d5SJulian Elischer */ 2346be4252b3SJulian Elischer if ((node->nd_flags & NGF_FORCE_WRITER) 2347714fb865SGleb Smirnoff || (hook && (hook->hk_flags & HK_FORCE_WRITER))) 2348069154d5SJulian Elischer rw = NGQRW_W; 23496f683eeeSGleb Smirnoff 2350069154d5SJulian Elischer if (queue) { 2351069154d5SJulian Elischer /* Put it on the queue for that node*/ 235230400f03SJulian Elischer #ifdef NETGRAPH_DEBUG 2353069154d5SJulian Elischer _ngi_check(item, __FILE__, __LINE__); 2354069154d5SJulian Elischer #endif 23552c8dda8dSWojciech A. Koszek NG_QUEUE_LOCK(ngq); 2356069154d5SJulian Elischer ng_queue_rw(ngq, item, rw); 23572c8dda8dSWojciech A. Koszek NG_QUEUE_UNLOCK(ngq); 23588afe16d5SGleb Smirnoff 23598afe16d5SGleb Smirnoff if (flags & NG_PROGRESS) 23608afe16d5SGleb Smirnoff return (EINPROGRESS); 23618afe16d5SGleb Smirnoff else 2362069154d5SJulian Elischer return (0); 2363069154d5SJulian Elischer } 2364069154d5SJulian Elischer 2365069154d5SJulian Elischer /* 2366069154d5SJulian Elischer * We already decided how we will be queueud or treated. 2367069154d5SJulian Elischer * Try get the appropriate operating permission. 2368069154d5SJulian Elischer */ 236932b33288SGleb Smirnoff if (rw == NGQRW_R) 2370069154d5SJulian Elischer item = ng_acquire_read(ngq, item); 237132b33288SGleb Smirnoff else 2372069154d5SJulian Elischer item = ng_acquire_write(ngq, item); 23734cf49a43SJulian Elischer 23744be59335SGleb Smirnoff 2375069154d5SJulian Elischer if (item == NULL) { 23768afe16d5SGleb Smirnoff if (flags & NG_PROGRESS) 23778afe16d5SGleb Smirnoff return (EINPROGRESS); 23788afe16d5SGleb Smirnoff else 2379069154d5SJulian Elischer return (0); 2380069154d5SJulian Elischer } 2381069154d5SJulian Elischer 238230400f03SJulian Elischer #ifdef NETGRAPH_DEBUG 2383069154d5SJulian Elischer _ngi_check(item, __FILE__, __LINE__); 2384069154d5SJulian Elischer #endif 23854be59335SGleb Smirnoff 23865951069aSJulian Elischer NGI_GET_NODE(item, node); /* zaps stored node */ 23875951069aSJulian Elischer 2388262dfd7fSJulian Elischer /* Don't report any errors. act as if it had been queued */ 2389262dfd7fSJulian Elischer ng_apply_item(node, item, rw); /* drops r/w lock when done */ 2390069154d5SJulian Elischer 2391069154d5SJulian Elischer /* 23925951069aSJulian Elischer * If the node goes away when we remove the reference, 2393376958b4SBrian Somers * whatever we just did caused it.. whatever we do, DO NOT 23945951069aSJulian Elischer * access the node again! 23955951069aSJulian Elischer */ 23965951069aSJulian Elischer if (NG_NODE_UNREF(node) == 0) { 23975951069aSJulian Elischer return (error); 23985951069aSJulian Elischer } 23995951069aSJulian Elischer 24002c8dda8dSWojciech A. Koszek NG_QUEUE_LOCK(ngq); 24014be59335SGleb Smirnoff if (NEXT_QUEUED_ITEM_CAN_PROCEED(ngq)) 2402f912c0f7SGleb Smirnoff ng_setisr(ngq->q_node); 24032c8dda8dSWojciech A. Koszek NG_QUEUE_UNLOCK(ngq); 2404069154d5SJulian Elischer 24051acb27c6SJulian Elischer return (error); 2406069154d5SJulian Elischer } 2407069154d5SJulian Elischer 2408069154d5SJulian Elischer /* 2409069154d5SJulian Elischer * We have an item that was possibly queued somewhere. 2410069154d5SJulian Elischer * It should contain all the information needed 2411069154d5SJulian Elischer * to run it on the appropriate node/hook. 24124cf49a43SJulian Elischer */ 2413262dfd7fSJulian Elischer static void 2414714fb865SGleb Smirnoff ng_apply_item(node_p node, item_p item, int rw) 2415069154d5SJulian Elischer { 2416069154d5SJulian Elischer hook_p hook; 2417a54a69d7SJulian Elischer int error = 0; 2418069154d5SJulian Elischer ng_rcvdata_t *rcvdata; 2419c4b5eea4SJulian Elischer ng_rcvmsg_t *rcvmsg; 24208afe16d5SGleb Smirnoff ng_apply_t *apply = NULL; 24218afe16d5SGleb Smirnoff void *context = NULL; 2422069154d5SJulian Elischer 24231acb27c6SJulian Elischer NGI_GET_HOOK(item, hook); /* clears stored hook */ 242430400f03SJulian Elischer #ifdef NETGRAPH_DEBUG 2425069154d5SJulian Elischer _ngi_check(item, __FILE__, __LINE__); 2426069154d5SJulian Elischer #endif 24278afe16d5SGleb Smirnoff 24288afe16d5SGleb Smirnoff /* 24294be59335SGleb Smirnoff * If the item has an "apply" callback, store it. 24304be59335SGleb Smirnoff * Clear item's callback immediately, to avoid an extra call if 24314be59335SGleb Smirnoff * the item is reused by the destination node. 24328afe16d5SGleb Smirnoff */ 24338afe16d5SGleb Smirnoff if (item->apply != NULL) { 24348afe16d5SGleb Smirnoff apply = item->apply; 24358afe16d5SGleb Smirnoff context = item->context; 24368afe16d5SGleb Smirnoff item->apply = NULL; 24378afe16d5SGleb Smirnoff } 24388afe16d5SGleb Smirnoff 24396b795970SJulian Elischer switch (item->el_flags & NGQF_TYPE) { 2440069154d5SJulian Elischer case NGQF_DATA: 2441069154d5SJulian Elischer /* 2442069154d5SJulian Elischer * Check things are still ok as when we were queued. 2443069154d5SJulian Elischer */ 2444069154d5SJulian Elischer if ((hook == NULL) 244530400f03SJulian Elischer || NG_HOOK_NOT_VALID(hook) 2446c4b5eea4SJulian Elischer || NG_NODE_NOT_VALID(node) ) { 2447a54a69d7SJulian Elischer error = EIO; 2448069154d5SJulian Elischer NG_FREE_ITEM(item); 2449c4b5eea4SJulian Elischer break; 2450069154d5SJulian Elischer } 2451c4b5eea4SJulian Elischer /* 2452c4b5eea4SJulian Elischer * If no receive method, just silently drop it. 2453c4b5eea4SJulian Elischer * Give preference to the hook over-ride method 2454c4b5eea4SJulian Elischer */ 2455c4b5eea4SJulian Elischer if ((!(rcvdata = hook->hk_rcvdata)) 2456c4b5eea4SJulian Elischer && (!(rcvdata = NG_HOOK_NODE(hook)->nd_type->rcvdata))) { 2457c4b5eea4SJulian Elischer error = 0; 2458c4b5eea4SJulian Elischer NG_FREE_ITEM(item); 2459c4b5eea4SJulian Elischer break; 2460c4b5eea4SJulian Elischer } 2461a54a69d7SJulian Elischer error = (*rcvdata)(hook, item); 2462069154d5SJulian Elischer break; 2463069154d5SJulian Elischer case NGQF_MESG: 2464069154d5SJulian Elischer if (hook) { 246530400f03SJulian Elischer if (NG_HOOK_NOT_VALID(hook)) { 2466069154d5SJulian Elischer /* 24671acb27c6SJulian Elischer * The hook has been zapped then we can't 246864efc707SRobert Watson * use it. Immediately drop its reference. 2469069154d5SJulian Elischer * The message may not need it. 2470069154d5SJulian Elischer */ 247130400f03SJulian Elischer NG_HOOK_UNREF(hook); 2472069154d5SJulian Elischer hook = NULL; 2473069154d5SJulian Elischer } 2474069154d5SJulian Elischer } 2475069154d5SJulian Elischer /* 2476069154d5SJulian Elischer * Similarly, if the node is a zombie there is 2477069154d5SJulian Elischer * nothing we can do with it, drop everything. 2478069154d5SJulian Elischer */ 247930400f03SJulian Elischer if (NG_NODE_NOT_VALID(node)) { 24806b795970SJulian Elischer TRAP_ERROR(); 2481a54a69d7SJulian Elischer error = EINVAL; 2482069154d5SJulian Elischer NG_FREE_ITEM(item); 2483069154d5SJulian Elischer } else { 2484069154d5SJulian Elischer /* 2485069154d5SJulian Elischer * Call the appropriate message handler for the object. 2486069154d5SJulian Elischer * It is up to the message handler to free the message. 2487069154d5SJulian Elischer * If it's a generic message, handle it generically, 2488069154d5SJulian Elischer * otherwise call the type's message handler 2489069154d5SJulian Elischer * (if it exists) 2490069154d5SJulian Elischer * XXX (race). Remember that a queued message may 2491069154d5SJulian Elischer * reference a node or hook that has just been 2492069154d5SJulian Elischer * invalidated. It will exist as the queue code 2493069154d5SJulian Elischer * is holding a reference, but.. 2494069154d5SJulian Elischer */ 2495069154d5SJulian Elischer 2496069154d5SJulian Elischer struct ng_mesg *msg = NGI_MSG(item); 2497069154d5SJulian Elischer 2498c4b5eea4SJulian Elischer /* 2499c4b5eea4SJulian Elischer * check if the generic handler owns it. 2500c4b5eea4SJulian Elischer */ 2501069154d5SJulian Elischer if ((msg->header.typecookie == NGM_GENERIC_COOKIE) 2502069154d5SJulian Elischer && ((msg->header.flags & NGF_RESP) == 0)) { 2503a54a69d7SJulian Elischer error = ng_generic_msg(node, item, hook); 2504c4b5eea4SJulian Elischer break; 2505c4b5eea4SJulian Elischer } 2506c4b5eea4SJulian Elischer /* 2507c4b5eea4SJulian Elischer * Now see if there is a handler (hook or node specific) 2508c4b5eea4SJulian Elischer * in the target node. If none, silently discard. 2509c4b5eea4SJulian Elischer */ 2510c4b5eea4SJulian Elischer if (((!hook) || (!(rcvmsg = hook->hk_rcvmsg))) 2511c4b5eea4SJulian Elischer && (!(rcvmsg = node->nd_type->rcvmsg))) { 25126b795970SJulian Elischer TRAP_ERROR(); 2513a54a69d7SJulian Elischer error = 0; 2514069154d5SJulian Elischer NG_FREE_ITEM(item); 2515c4b5eea4SJulian Elischer break; 2516069154d5SJulian Elischer } 2517a54a69d7SJulian Elischer error = (*rcvmsg)(node, item, hook); 2518069154d5SJulian Elischer } 2519069154d5SJulian Elischer break; 25206b795970SJulian Elischer case NGQF_FN: 25216b795970SJulian Elischer /* 25226b795970SJulian Elischer * We have to implicitly trust the hook, 25236b795970SJulian Elischer * as some of these are used for system purposes 25241acb27c6SJulian Elischer * where the hook is invalid. In the case of 25251acb27c6SJulian Elischer * the shutdown message we allow it to hit 25261acb27c6SJulian Elischer * even if the node is invalid. 25276b795970SJulian Elischer */ 25281acb27c6SJulian Elischer if ((NG_NODE_NOT_VALID(node)) 25291acb27c6SJulian Elischer && (NGI_FN(item) != &ng_rmnode)) { 25306b795970SJulian Elischer TRAP_ERROR(); 2531a54a69d7SJulian Elischer error = EINVAL; 25328bb55179SGleb Smirnoff NG_FREE_ITEM(item); 25336b795970SJulian Elischer break; 25346b795970SJulian Elischer } 25356b795970SJulian Elischer (*NGI_FN(item))(node, hook, NGI_ARG1(item), NGI_ARG2(item)); 25366b795970SJulian Elischer NG_FREE_ITEM(item); 25376b795970SJulian Elischer break; 25386b795970SJulian Elischer 2539069154d5SJulian Elischer } 2540069154d5SJulian Elischer /* 2541069154d5SJulian Elischer * We held references on some of the resources 2542069154d5SJulian Elischer * that we took from the item. Now that we have 2543069154d5SJulian Elischer * finished doing everything, drop those references. 2544069154d5SJulian Elischer */ 2545069154d5SJulian Elischer if (hook) { 254630400f03SJulian Elischer NG_HOOK_UNREF(hook); 2547069154d5SJulian Elischer } 2548069154d5SJulian Elischer 2549714fb865SGleb Smirnoff if (rw == NGQRW_R) { 255030400f03SJulian Elischer ng_leave_read(&node->nd_input_queue); 2551a54a69d7SJulian Elischer } else { 255230400f03SJulian Elischer ng_leave_write(&node->nd_input_queue); 2553a54a69d7SJulian Elischer } 25548afe16d5SGleb Smirnoff 25558afe16d5SGleb Smirnoff /* Apply callback. */ 25568afe16d5SGleb Smirnoff if (apply != NULL) 25578afe16d5SGleb Smirnoff (*apply)(context, error); 25588afe16d5SGleb Smirnoff 2559262dfd7fSJulian Elischer return; 2560069154d5SJulian Elischer } 2561069154d5SJulian Elischer 2562069154d5SJulian Elischer /*********************************************************************** 2563069154d5SJulian Elischer * Implement the 'generic' control messages 2564069154d5SJulian Elischer ***********************************************************************/ 2565069154d5SJulian Elischer static int 2566069154d5SJulian Elischer ng_generic_msg(node_p here, item_p item, hook_p lasthook) 25674cf49a43SJulian Elischer { 25684cf49a43SJulian Elischer int error = 0; 2569069154d5SJulian Elischer struct ng_mesg *msg; 2570069154d5SJulian Elischer struct ng_mesg *resp = NULL; 25714cf49a43SJulian Elischer 2572069154d5SJulian Elischer NGI_GET_MSG(item, msg); 25734cf49a43SJulian Elischer if (msg->header.typecookie != NGM_GENERIC_COOKIE) { 25746b795970SJulian Elischer TRAP_ERROR(); 2575069154d5SJulian Elischer error = EINVAL; 2576069154d5SJulian Elischer goto out; 25774cf49a43SJulian Elischer } 25784cf49a43SJulian Elischer switch (msg->header.cmd) { 25794cf49a43SJulian Elischer case NGM_SHUTDOWN: 25801acb27c6SJulian Elischer ng_rmnode(here, NULL, NULL, 0); 25814cf49a43SJulian Elischer break; 25824cf49a43SJulian Elischer case NGM_MKPEER: 25834cf49a43SJulian Elischer { 25844cf49a43SJulian Elischer struct ngm_mkpeer *const mkp = (struct ngm_mkpeer *) msg->data; 25854cf49a43SJulian Elischer 25864cf49a43SJulian Elischer if (msg->header.arglen != sizeof(*mkp)) { 25876b795970SJulian Elischer TRAP_ERROR(); 2588069154d5SJulian Elischer error = EINVAL; 2589069154d5SJulian Elischer break; 25904cf49a43SJulian Elischer } 25914cf49a43SJulian Elischer mkp->type[sizeof(mkp->type) - 1] = '\0'; 25924cf49a43SJulian Elischer mkp->ourhook[sizeof(mkp->ourhook) - 1] = '\0'; 25934cf49a43SJulian Elischer mkp->peerhook[sizeof(mkp->peerhook) - 1] = '\0'; 25944cf49a43SJulian Elischer error = ng_mkpeer(here, mkp->ourhook, mkp->peerhook, mkp->type); 25954cf49a43SJulian Elischer break; 25964cf49a43SJulian Elischer } 25974cf49a43SJulian Elischer case NGM_CONNECT: 25984cf49a43SJulian Elischer { 25994cf49a43SJulian Elischer struct ngm_connect *const con = 26004cf49a43SJulian Elischer (struct ngm_connect *) msg->data; 26014cf49a43SJulian Elischer node_p node2; 26024cf49a43SJulian Elischer 26034cf49a43SJulian Elischer if (msg->header.arglen != sizeof(*con)) { 26046b795970SJulian Elischer TRAP_ERROR(); 2605069154d5SJulian Elischer error = EINVAL; 2606069154d5SJulian Elischer break; 26074cf49a43SJulian Elischer } 26084cf49a43SJulian Elischer con->path[sizeof(con->path) - 1] = '\0'; 26094cf49a43SJulian Elischer con->ourhook[sizeof(con->ourhook) - 1] = '\0'; 26104cf49a43SJulian Elischer con->peerhook[sizeof(con->peerhook) - 1] = '\0'; 2611069154d5SJulian Elischer /* Don't forget we get a reference.. */ 2612069154d5SJulian Elischer error = ng_path2noderef(here, con->path, &node2, NULL); 26134cf49a43SJulian Elischer if (error) 26144cf49a43SJulian Elischer break; 26154cf49a43SJulian Elischer error = ng_con_nodes(here, con->ourhook, node2, con->peerhook); 261630400f03SJulian Elischer NG_NODE_UNREF(node2); 26174cf49a43SJulian Elischer break; 26184cf49a43SJulian Elischer } 26194cf49a43SJulian Elischer case NGM_NAME: 26204cf49a43SJulian Elischer { 26214cf49a43SJulian Elischer struct ngm_name *const nam = (struct ngm_name *) msg->data; 26224cf49a43SJulian Elischer 26234cf49a43SJulian Elischer if (msg->header.arglen != sizeof(*nam)) { 26246b795970SJulian Elischer TRAP_ERROR(); 2625069154d5SJulian Elischer error = EINVAL; 2626069154d5SJulian Elischer break; 26274cf49a43SJulian Elischer } 26284cf49a43SJulian Elischer nam->name[sizeof(nam->name) - 1] = '\0'; 26294cf49a43SJulian Elischer error = ng_name_node(here, nam->name); 26304cf49a43SJulian Elischer break; 26314cf49a43SJulian Elischer } 26324cf49a43SJulian Elischer case NGM_RMHOOK: 26334cf49a43SJulian Elischer { 26344cf49a43SJulian Elischer struct ngm_rmhook *const rmh = (struct ngm_rmhook *) msg->data; 26354cf49a43SJulian Elischer hook_p hook; 26364cf49a43SJulian Elischer 26374cf49a43SJulian Elischer if (msg->header.arglen != sizeof(*rmh)) { 26386b795970SJulian Elischer TRAP_ERROR(); 2639069154d5SJulian Elischer error = EINVAL; 2640069154d5SJulian Elischer break; 26414cf49a43SJulian Elischer } 26424cf49a43SJulian Elischer rmh->ourhook[sizeof(rmh->ourhook) - 1] = '\0'; 2643899e9c4eSArchie Cobbs if ((hook = ng_findhook(here, rmh->ourhook)) != NULL) 26444cf49a43SJulian Elischer ng_destroy_hook(hook); 26454cf49a43SJulian Elischer break; 26464cf49a43SJulian Elischer } 26474cf49a43SJulian Elischer case NGM_NODEINFO: 26484cf49a43SJulian Elischer { 26494cf49a43SJulian Elischer struct nodeinfo *ni; 26504cf49a43SJulian Elischer 2651069154d5SJulian Elischer NG_MKRESPONSE(resp, msg, sizeof(*ni), M_NOWAIT); 26524cf49a43SJulian Elischer if (resp == NULL) { 26534cf49a43SJulian Elischer error = ENOMEM; 26544cf49a43SJulian Elischer break; 26554cf49a43SJulian Elischer } 26564cf49a43SJulian Elischer 26574cf49a43SJulian Elischer /* Fill in node info */ 2658069154d5SJulian Elischer ni = (struct nodeinfo *) resp->data; 265930400f03SJulian Elischer if (NG_NODE_HAS_NAME(here)) 266087e2c66aSHartmut Brandt strcpy(ni->name, NG_NODE_NAME(here)); 266187e2c66aSHartmut Brandt strcpy(ni->type, here->nd_type->name); 2662dc90cad9SJulian Elischer ni->id = ng_node2ID(here); 266330400f03SJulian Elischer ni->hooks = here->nd_numhooks; 26644cf49a43SJulian Elischer break; 26654cf49a43SJulian Elischer } 26664cf49a43SJulian Elischer case NGM_LISTHOOKS: 26674cf49a43SJulian Elischer { 266830400f03SJulian Elischer const int nhooks = here->nd_numhooks; 26694cf49a43SJulian Elischer struct hooklist *hl; 26704cf49a43SJulian Elischer struct nodeinfo *ni; 26714cf49a43SJulian Elischer hook_p hook; 26724cf49a43SJulian Elischer 26734cf49a43SJulian Elischer /* Get response struct */ 2674069154d5SJulian Elischer NG_MKRESPONSE(resp, msg, sizeof(*hl) 26754cf49a43SJulian Elischer + (nhooks * sizeof(struct linkinfo)), M_NOWAIT); 2676069154d5SJulian Elischer if (resp == NULL) { 26774cf49a43SJulian Elischer error = ENOMEM; 26784cf49a43SJulian Elischer break; 26794cf49a43SJulian Elischer } 2680069154d5SJulian Elischer hl = (struct hooklist *) resp->data; 26814cf49a43SJulian Elischer ni = &hl->nodeinfo; 26824cf49a43SJulian Elischer 26834cf49a43SJulian Elischer /* Fill in node info */ 268430400f03SJulian Elischer if (NG_NODE_HAS_NAME(here)) 268587e2c66aSHartmut Brandt strcpy(ni->name, NG_NODE_NAME(here)); 268687e2c66aSHartmut Brandt strcpy(ni->type, here->nd_type->name); 2687dc90cad9SJulian Elischer ni->id = ng_node2ID(here); 26884cf49a43SJulian Elischer 26894cf49a43SJulian Elischer /* Cycle through the linked list of hooks */ 26904cf49a43SJulian Elischer ni->hooks = 0; 269130400f03SJulian Elischer LIST_FOREACH(hook, &here->nd_hooks, hk_hooks) { 26924cf49a43SJulian Elischer struct linkinfo *const link = &hl->link[ni->hooks]; 26934cf49a43SJulian Elischer 26944cf49a43SJulian Elischer if (ni->hooks >= nhooks) { 26954cf49a43SJulian Elischer log(LOG_ERR, "%s: number of %s changed\n", 26966e551fb6SDavid E. O'Brien __func__, "hooks"); 26974cf49a43SJulian Elischer break; 26984cf49a43SJulian Elischer } 269930400f03SJulian Elischer if (NG_HOOK_NOT_VALID(hook)) 27004cf49a43SJulian Elischer continue; 270187e2c66aSHartmut Brandt strcpy(link->ourhook, NG_HOOK_NAME(hook)); 270287e2c66aSHartmut Brandt strcpy(link->peerhook, NG_PEER_HOOK_NAME(hook)); 270330400f03SJulian Elischer if (NG_PEER_NODE_NAME(hook)[0] != '\0') 270487e2c66aSHartmut Brandt strcpy(link->nodeinfo.name, 270587e2c66aSHartmut Brandt NG_PEER_NODE_NAME(hook)); 270687e2c66aSHartmut Brandt strcpy(link->nodeinfo.type, 270787e2c66aSHartmut Brandt NG_PEER_NODE(hook)->nd_type->name); 270830400f03SJulian Elischer link->nodeinfo.id = ng_node2ID(NG_PEER_NODE(hook)); 270930400f03SJulian Elischer link->nodeinfo.hooks = NG_PEER_NODE(hook)->nd_numhooks; 27104cf49a43SJulian Elischer ni->hooks++; 27114cf49a43SJulian Elischer } 27124cf49a43SJulian Elischer break; 27134cf49a43SJulian Elischer } 27144cf49a43SJulian Elischer 27154cf49a43SJulian Elischer case NGM_LISTNAMES: 27164cf49a43SJulian Elischer case NGM_LISTNODES: 27174cf49a43SJulian Elischer { 27184cf49a43SJulian Elischer const int unnamed = (msg->header.cmd == NGM_LISTNODES); 27194cf49a43SJulian Elischer struct namelist *nl; 27204cf49a43SJulian Elischer node_p node; 27214cf49a43SJulian Elischer int num = 0; 27224cf49a43SJulian Elischer 27239ed346baSBosko Milekic mtx_lock(&ng_nodelist_mtx); 27244cf49a43SJulian Elischer /* Count number of nodes */ 272530400f03SJulian Elischer LIST_FOREACH(node, &ng_nodelist, nd_nodes) { 272670de87f2SJulian Elischer if (NG_NODE_IS_VALID(node) 272770de87f2SJulian Elischer && (unnamed || NG_NODE_HAS_NAME(node))) { 27284cf49a43SJulian Elischer num++; 27294cf49a43SJulian Elischer } 273070de87f2SJulian Elischer } 27319ed346baSBosko Milekic mtx_unlock(&ng_nodelist_mtx); 27324cf49a43SJulian Elischer 27334cf49a43SJulian Elischer /* Get response struct */ 2734069154d5SJulian Elischer NG_MKRESPONSE(resp, msg, sizeof(*nl) 27354cf49a43SJulian Elischer + (num * sizeof(struct nodeinfo)), M_NOWAIT); 2736069154d5SJulian Elischer if (resp == NULL) { 27374cf49a43SJulian Elischer error = ENOMEM; 27384cf49a43SJulian Elischer break; 27394cf49a43SJulian Elischer } 2740069154d5SJulian Elischer nl = (struct namelist *) resp->data; 27414cf49a43SJulian Elischer 27424cf49a43SJulian Elischer /* Cycle through the linked list of nodes */ 27434cf49a43SJulian Elischer nl->numnames = 0; 27449ed346baSBosko Milekic mtx_lock(&ng_nodelist_mtx); 274530400f03SJulian Elischer LIST_FOREACH(node, &ng_nodelist, nd_nodes) { 27464cf49a43SJulian Elischer struct nodeinfo *const np = &nl->nodeinfo[nl->numnames]; 27474cf49a43SJulian Elischer 2748b96baf0aSGleb Smirnoff if (NG_NODE_NOT_VALID(node)) 2749b96baf0aSGleb Smirnoff continue; 2750b96baf0aSGleb Smirnoff if (!unnamed && (! NG_NODE_HAS_NAME(node))) 2751b96baf0aSGleb Smirnoff continue; 27524cf49a43SJulian Elischer if (nl->numnames >= num) { 27534cf49a43SJulian Elischer log(LOG_ERR, "%s: number of %s changed\n", 27546e551fb6SDavid E. O'Brien __func__, "nodes"); 27554cf49a43SJulian Elischer break; 27564cf49a43SJulian Elischer } 275730400f03SJulian Elischer if (NG_NODE_HAS_NAME(node)) 275887e2c66aSHartmut Brandt strcpy(np->name, NG_NODE_NAME(node)); 275987e2c66aSHartmut Brandt strcpy(np->type, node->nd_type->name); 2760dc90cad9SJulian Elischer np->id = ng_node2ID(node); 276130400f03SJulian Elischer np->hooks = node->nd_numhooks; 27624cf49a43SJulian Elischer nl->numnames++; 27634cf49a43SJulian Elischer } 27649ed346baSBosko Milekic mtx_unlock(&ng_nodelist_mtx); 27654cf49a43SJulian Elischer break; 27664cf49a43SJulian Elischer } 27674cf49a43SJulian Elischer 27684cf49a43SJulian Elischer case NGM_LISTTYPES: 27694cf49a43SJulian Elischer { 27704cf49a43SJulian Elischer struct typelist *tl; 27714cf49a43SJulian Elischer struct ng_type *type; 27724cf49a43SJulian Elischer int num = 0; 27734cf49a43SJulian Elischer 27749ed346baSBosko Milekic mtx_lock(&ng_typelist_mtx); 27754cf49a43SJulian Elischer /* Count number of types */ 277670de87f2SJulian Elischer LIST_FOREACH(type, &ng_typelist, types) { 27774cf49a43SJulian Elischer num++; 277870de87f2SJulian Elischer } 27799ed346baSBosko Milekic mtx_unlock(&ng_typelist_mtx); 27804cf49a43SJulian Elischer 27814cf49a43SJulian Elischer /* Get response struct */ 2782069154d5SJulian Elischer NG_MKRESPONSE(resp, msg, sizeof(*tl) 27834cf49a43SJulian Elischer + (num * sizeof(struct typeinfo)), M_NOWAIT); 2784069154d5SJulian Elischer if (resp == NULL) { 27854cf49a43SJulian Elischer error = ENOMEM; 27864cf49a43SJulian Elischer break; 27874cf49a43SJulian Elischer } 2788069154d5SJulian Elischer tl = (struct typelist *) resp->data; 27894cf49a43SJulian Elischer 27904cf49a43SJulian Elischer /* Cycle through the linked list of types */ 27914cf49a43SJulian Elischer tl->numtypes = 0; 27929ed346baSBosko Milekic mtx_lock(&ng_typelist_mtx); 2793069154d5SJulian Elischer LIST_FOREACH(type, &ng_typelist, types) { 27944cf49a43SJulian Elischer struct typeinfo *const tp = &tl->typeinfo[tl->numtypes]; 27954cf49a43SJulian Elischer 27964cf49a43SJulian Elischer if (tl->numtypes >= num) { 27974cf49a43SJulian Elischer log(LOG_ERR, "%s: number of %s changed\n", 27986e551fb6SDavid E. O'Brien __func__, "types"); 27994cf49a43SJulian Elischer break; 28004cf49a43SJulian Elischer } 280187e2c66aSHartmut Brandt strcpy(tp->type_name, type->name); 2802c73b94a2SJulian Elischer tp->numnodes = type->refs - 1; /* don't count list */ 28034cf49a43SJulian Elischer tl->numtypes++; 28044cf49a43SJulian Elischer } 28059ed346baSBosko Milekic mtx_unlock(&ng_typelist_mtx); 28064cf49a43SJulian Elischer break; 28074cf49a43SJulian Elischer } 28084cf49a43SJulian Elischer 2809f8307e12SArchie Cobbs case NGM_BINARY2ASCII: 2810f8307e12SArchie Cobbs { 28117133ac27SArchie Cobbs int bufSize = 20 * 1024; /* XXX hard coded constant */ 2812f8307e12SArchie Cobbs const struct ng_parse_type *argstype; 2813f8307e12SArchie Cobbs const struct ng_cmdlist *c; 2814069154d5SJulian Elischer struct ng_mesg *binary, *ascii; 2815f8307e12SArchie Cobbs 2816f8307e12SArchie Cobbs /* Data area must contain a valid netgraph message */ 2817f8307e12SArchie Cobbs binary = (struct ng_mesg *)msg->data; 28184c9b5910SGleb Smirnoff if (msg->header.arglen < sizeof(struct ng_mesg) || 28194c9b5910SGleb Smirnoff (msg->header.arglen - sizeof(struct ng_mesg) < 28204c9b5910SGleb Smirnoff binary->header.arglen)) { 28216b795970SJulian Elischer TRAP_ERROR(); 2822f8307e12SArchie Cobbs error = EINVAL; 2823f8307e12SArchie Cobbs break; 2824f8307e12SArchie Cobbs } 2825f8307e12SArchie Cobbs 2826f8307e12SArchie Cobbs /* Get a response message with lots of room */ 2827069154d5SJulian Elischer NG_MKRESPONSE(resp, msg, sizeof(*ascii) + bufSize, M_NOWAIT); 2828069154d5SJulian Elischer if (resp == NULL) { 2829f8307e12SArchie Cobbs error = ENOMEM; 2830f8307e12SArchie Cobbs break; 2831f8307e12SArchie Cobbs } 2832069154d5SJulian Elischer ascii = (struct ng_mesg *)resp->data; 2833f8307e12SArchie Cobbs 2834f8307e12SArchie Cobbs /* Copy binary message header to response message payload */ 2835f8307e12SArchie Cobbs bcopy(binary, ascii, sizeof(*binary)); 2836f8307e12SArchie Cobbs 2837f8307e12SArchie Cobbs /* Find command by matching typecookie and command number */ 283830400f03SJulian Elischer for (c = here->nd_type->cmdlist; 2839f8307e12SArchie Cobbs c != NULL && c->name != NULL; c++) { 2840f8307e12SArchie Cobbs if (binary->header.typecookie == c->cookie 2841f8307e12SArchie Cobbs && binary->header.cmd == c->cmd) 2842f8307e12SArchie Cobbs break; 2843f8307e12SArchie Cobbs } 2844f8307e12SArchie Cobbs if (c == NULL || c->name == NULL) { 2845f8307e12SArchie Cobbs for (c = ng_generic_cmds; c->name != NULL; c++) { 2846f8307e12SArchie Cobbs if (binary->header.typecookie == c->cookie 2847f8307e12SArchie Cobbs && binary->header.cmd == c->cmd) 2848f8307e12SArchie Cobbs break; 2849f8307e12SArchie Cobbs } 2850f8307e12SArchie Cobbs if (c->name == NULL) { 2851069154d5SJulian Elischer NG_FREE_MSG(resp); 2852f8307e12SArchie Cobbs error = ENOSYS; 2853f8307e12SArchie Cobbs break; 2854f8307e12SArchie Cobbs } 2855f8307e12SArchie Cobbs } 2856f8307e12SArchie Cobbs 2857f8307e12SArchie Cobbs /* Convert command name to ASCII */ 2858f8307e12SArchie Cobbs snprintf(ascii->header.cmdstr, sizeof(ascii->header.cmdstr), 2859f8307e12SArchie Cobbs "%s", c->name); 2860f8307e12SArchie Cobbs 2861f8307e12SArchie Cobbs /* Convert command arguments to ASCII */ 2862f8307e12SArchie Cobbs argstype = (binary->header.flags & NGF_RESP) ? 2863f8307e12SArchie Cobbs c->respType : c->mesgType; 286470de87f2SJulian Elischer if (argstype == NULL) { 2865f8307e12SArchie Cobbs *ascii->data = '\0'; 286670de87f2SJulian Elischer } else { 2867f8307e12SArchie Cobbs if ((error = ng_unparse(argstype, 2868f8307e12SArchie Cobbs (u_char *)binary->data, 2869f8307e12SArchie Cobbs ascii->data, bufSize)) != 0) { 2870069154d5SJulian Elischer NG_FREE_MSG(resp); 2871f8307e12SArchie Cobbs break; 2872f8307e12SArchie Cobbs } 2873f8307e12SArchie Cobbs } 2874f8307e12SArchie Cobbs 2875f8307e12SArchie Cobbs /* Return the result as struct ng_mesg plus ASCII string */ 2876f8307e12SArchie Cobbs bufSize = strlen(ascii->data) + 1; 2877f8307e12SArchie Cobbs ascii->header.arglen = bufSize; 2878069154d5SJulian Elischer resp->header.arglen = sizeof(*ascii) + bufSize; 2879f8307e12SArchie Cobbs break; 2880f8307e12SArchie Cobbs } 2881f8307e12SArchie Cobbs 2882f8307e12SArchie Cobbs case NGM_ASCII2BINARY: 2883f8307e12SArchie Cobbs { 2884f8307e12SArchie Cobbs int bufSize = 2000; /* XXX hard coded constant */ 2885f8307e12SArchie Cobbs const struct ng_cmdlist *c; 2886f8307e12SArchie Cobbs const struct ng_parse_type *argstype; 2887069154d5SJulian Elischer struct ng_mesg *ascii, *binary; 288852ec4a03SArchie Cobbs int off = 0; 2889f8307e12SArchie Cobbs 2890f8307e12SArchie Cobbs /* Data area must contain at least a struct ng_mesg + '\0' */ 2891f8307e12SArchie Cobbs ascii = (struct ng_mesg *)msg->data; 28924c9b5910SGleb Smirnoff if ((msg->header.arglen < sizeof(*ascii) + 1) || 28934c9b5910SGleb Smirnoff (ascii->header.arglen < 1) || 28944c9b5910SGleb Smirnoff (msg->header.arglen < sizeof(*ascii) + 28954c9b5910SGleb Smirnoff ascii->header.arglen)) { 28966b795970SJulian Elischer TRAP_ERROR(); 2897f8307e12SArchie Cobbs error = EINVAL; 2898f8307e12SArchie Cobbs break; 2899f8307e12SArchie Cobbs } 2900f8307e12SArchie Cobbs ascii->data[ascii->header.arglen - 1] = '\0'; 2901f8307e12SArchie Cobbs 2902f8307e12SArchie Cobbs /* Get a response message with lots of room */ 2903069154d5SJulian Elischer NG_MKRESPONSE(resp, msg, sizeof(*binary) + bufSize, M_NOWAIT); 2904069154d5SJulian Elischer if (resp == NULL) { 2905f8307e12SArchie Cobbs error = ENOMEM; 2906f8307e12SArchie Cobbs break; 2907f8307e12SArchie Cobbs } 2908069154d5SJulian Elischer binary = (struct ng_mesg *)resp->data; 2909f8307e12SArchie Cobbs 2910f8307e12SArchie Cobbs /* Copy ASCII message header to response message payload */ 2911f8307e12SArchie Cobbs bcopy(ascii, binary, sizeof(*ascii)); 2912f8307e12SArchie Cobbs 2913f8307e12SArchie Cobbs /* Find command by matching ASCII command string */ 291430400f03SJulian Elischer for (c = here->nd_type->cmdlist; 2915f8307e12SArchie Cobbs c != NULL && c->name != NULL; c++) { 2916f8307e12SArchie Cobbs if (strcmp(ascii->header.cmdstr, c->name) == 0) 2917f8307e12SArchie Cobbs break; 2918f8307e12SArchie Cobbs } 2919f8307e12SArchie Cobbs if (c == NULL || c->name == NULL) { 2920f8307e12SArchie Cobbs for (c = ng_generic_cmds; c->name != NULL; c++) { 2921f8307e12SArchie Cobbs if (strcmp(ascii->header.cmdstr, c->name) == 0) 2922f8307e12SArchie Cobbs break; 2923f8307e12SArchie Cobbs } 2924f8307e12SArchie Cobbs if (c->name == NULL) { 2925069154d5SJulian Elischer NG_FREE_MSG(resp); 2926f8307e12SArchie Cobbs error = ENOSYS; 2927f8307e12SArchie Cobbs break; 2928f8307e12SArchie Cobbs } 2929f8307e12SArchie Cobbs } 2930f8307e12SArchie Cobbs 2931f8307e12SArchie Cobbs /* Convert command name to binary */ 2932f8307e12SArchie Cobbs binary->header.cmd = c->cmd; 2933f8307e12SArchie Cobbs binary->header.typecookie = c->cookie; 2934f8307e12SArchie Cobbs 2935f8307e12SArchie Cobbs /* Convert command arguments to binary */ 2936f8307e12SArchie Cobbs argstype = (binary->header.flags & NGF_RESP) ? 2937f8307e12SArchie Cobbs c->respType : c->mesgType; 293870de87f2SJulian Elischer if (argstype == NULL) { 2939f8307e12SArchie Cobbs bufSize = 0; 294070de87f2SJulian Elischer } else { 2941f8307e12SArchie Cobbs if ((error = ng_parse(argstype, ascii->data, 2942f8307e12SArchie Cobbs &off, (u_char *)binary->data, &bufSize)) != 0) { 2943069154d5SJulian Elischer NG_FREE_MSG(resp); 2944f8307e12SArchie Cobbs break; 2945f8307e12SArchie Cobbs } 2946f8307e12SArchie Cobbs } 2947f8307e12SArchie Cobbs 2948f8307e12SArchie Cobbs /* Return the result */ 2949f8307e12SArchie Cobbs binary->header.arglen = bufSize; 2950069154d5SJulian Elischer resp->header.arglen = sizeof(*binary) + bufSize; 2951f8307e12SArchie Cobbs break; 2952f8307e12SArchie Cobbs } 2953f8307e12SArchie Cobbs 29547095e097SPoul-Henning Kamp case NGM_TEXT_CONFIG: 29554cf49a43SJulian Elischer case NGM_TEXT_STATUS: 29564cf49a43SJulian Elischer /* 29574cf49a43SJulian Elischer * This one is tricky as it passes the command down to the 29584cf49a43SJulian Elischer * actual node, even though it is a generic type command. 2959069154d5SJulian Elischer * This means we must assume that the item/msg is already freed 29604cf49a43SJulian Elischer * when control passes back to us. 29614cf49a43SJulian Elischer */ 296230400f03SJulian Elischer if (here->nd_type->rcvmsg != NULL) { 2963069154d5SJulian Elischer NGI_MSG(item) = msg; /* put it back as we found it */ 296430400f03SJulian Elischer return((*here->nd_type->rcvmsg)(here, item, lasthook)); 29654cf49a43SJulian Elischer } 29664cf49a43SJulian Elischer /* Fall through if rcvmsg not supported */ 29674cf49a43SJulian Elischer default: 29686b795970SJulian Elischer TRAP_ERROR(); 29694cf49a43SJulian Elischer error = EINVAL; 29704cf49a43SJulian Elischer } 2971069154d5SJulian Elischer /* 2972069154d5SJulian Elischer * Sometimes a generic message may be statically allocated 2973069154d5SJulian Elischer * to avoid problems with allocating when in tight memeory situations. 2974069154d5SJulian Elischer * Don't free it if it is so. 2975069154d5SJulian Elischer * I break them appart here, because erros may cause a free if the item 2976069154d5SJulian Elischer * in which case we'd be doing it twice. 2977069154d5SJulian Elischer * they are kept together above, to simplify freeing. 2978069154d5SJulian Elischer */ 2979069154d5SJulian Elischer out: 2980069154d5SJulian Elischer NG_RESPOND_MSG(error, here, item, resp); 29811acb27c6SJulian Elischer if (msg) 2982069154d5SJulian Elischer NG_FREE_MSG(msg); 29834cf49a43SJulian Elischer return (error); 29844cf49a43SJulian Elischer } 29854cf49a43SJulian Elischer 29864cf49a43SJulian Elischer /************************************************************************ 29878253c060SGleb Smirnoff Queue element get/free routines 29888253c060SGleb Smirnoff ************************************************************************/ 29898253c060SGleb Smirnoff 29908253c060SGleb Smirnoff uma_zone_t ng_qzone; 29918253c060SGleb Smirnoff static int maxalloc = 512; /* limit the damage of a leak */ 29928253c060SGleb Smirnoff 29938253c060SGleb Smirnoff TUNABLE_INT("net.graph.maxalloc", &maxalloc); 29948253c060SGleb Smirnoff SYSCTL_INT(_net_graph, OID_AUTO, maxalloc, CTLFLAG_RDTUN, &maxalloc, 29958253c060SGleb Smirnoff 0, "Maximum number of queue items to allocate"); 29968253c060SGleb Smirnoff 29978253c060SGleb Smirnoff #ifdef NETGRAPH_DEBUG 29988253c060SGleb Smirnoff static TAILQ_HEAD(, ng_item) ng_itemlist = TAILQ_HEAD_INITIALIZER(ng_itemlist); 29998253c060SGleb Smirnoff static int allocated; /* number of items malloc'd */ 30008253c060SGleb Smirnoff #endif 30018253c060SGleb Smirnoff 30028253c060SGleb Smirnoff /* 30038253c060SGleb Smirnoff * Get a queue entry. 30048253c060SGleb Smirnoff * This is usually called when a packet first enters netgraph. 30058253c060SGleb Smirnoff * By definition, this is usually from an interrupt, or from a user. 30068253c060SGleb Smirnoff * Users are not so important, but try be quick for the times that it's 30078253c060SGleb Smirnoff * an interrupt. 30088253c060SGleb Smirnoff */ 30098253c060SGleb Smirnoff static __inline item_p 301042282202SGleb Smirnoff ng_getqblk(int flags) 30118253c060SGleb Smirnoff { 30128253c060SGleb Smirnoff item_p item = NULL; 301342282202SGleb Smirnoff int wait; 30148253c060SGleb Smirnoff 301542282202SGleb Smirnoff wait = (flags & NG_WAITOK) ? M_WAITOK : M_NOWAIT; 301642282202SGleb Smirnoff 301742282202SGleb Smirnoff item = uma_zalloc(ng_qzone, wait | M_ZERO); 30188253c060SGleb Smirnoff 30198253c060SGleb Smirnoff #ifdef NETGRAPH_DEBUG 30208253c060SGleb Smirnoff if (item) { 30218253c060SGleb Smirnoff mtx_lock(&ngq_mtx); 30228253c060SGleb Smirnoff TAILQ_INSERT_TAIL(&ng_itemlist, item, all); 30238253c060SGleb Smirnoff allocated++; 30248253c060SGleb Smirnoff mtx_unlock(&ngq_mtx); 30258253c060SGleb Smirnoff } 30268253c060SGleb Smirnoff #endif 30278253c060SGleb Smirnoff 30288253c060SGleb Smirnoff return (item); 30298253c060SGleb Smirnoff } 30308253c060SGleb Smirnoff 30318253c060SGleb Smirnoff /* 30328253c060SGleb Smirnoff * Release a queue entry 30338253c060SGleb Smirnoff */ 30348253c060SGleb Smirnoff void 30358253c060SGleb Smirnoff ng_free_item(item_p item) 30368253c060SGleb Smirnoff { 3037eb2405ddSGleb Smirnoff KASSERT(item->apply == NULL, ("%s: leaking apply callback", __func__)); 3038eb2405ddSGleb Smirnoff 30398253c060SGleb Smirnoff /* 30408253c060SGleb Smirnoff * The item may hold resources on it's own. We need to free 30418253c060SGleb Smirnoff * these before we can free the item. What they are depends upon 30428253c060SGleb Smirnoff * what kind of item it is. it is important that nodes zero 30438253c060SGleb Smirnoff * out pointers to resources that they remove from the item 30448253c060SGleb Smirnoff * or we release them again here. 30458253c060SGleb Smirnoff */ 30468253c060SGleb Smirnoff switch (item->el_flags & NGQF_TYPE) { 30478253c060SGleb Smirnoff case NGQF_DATA: 30488253c060SGleb Smirnoff /* If we have an mbuf still attached.. */ 30498253c060SGleb Smirnoff NG_FREE_M(_NGI_M(item)); 30508253c060SGleb Smirnoff break; 30518253c060SGleb Smirnoff case NGQF_MESG: 30528253c060SGleb Smirnoff _NGI_RETADDR(item) = 0; 30538253c060SGleb Smirnoff NG_FREE_MSG(_NGI_MSG(item)); 30548253c060SGleb Smirnoff break; 30558253c060SGleb Smirnoff case NGQF_FN: 30568253c060SGleb Smirnoff /* nothing to free really, */ 30578253c060SGleb Smirnoff _NGI_FN(item) = NULL; 30588253c060SGleb Smirnoff _NGI_ARG1(item) = NULL; 30598253c060SGleb Smirnoff _NGI_ARG2(item) = 0; 30608253c060SGleb Smirnoff case NGQF_UNDEF: 30618253c060SGleb Smirnoff break; 30628253c060SGleb Smirnoff } 30638253c060SGleb Smirnoff /* If we still have a node or hook referenced... */ 30648253c060SGleb Smirnoff _NGI_CLR_NODE(item); 30658253c060SGleb Smirnoff _NGI_CLR_HOOK(item); 30668253c060SGleb Smirnoff 30678253c060SGleb Smirnoff #ifdef NETGRAPH_DEBUG 30688253c060SGleb Smirnoff mtx_lock(&ngq_mtx); 30698253c060SGleb Smirnoff TAILQ_REMOVE(&ng_itemlist, item, all); 30708253c060SGleb Smirnoff allocated--; 30718253c060SGleb Smirnoff mtx_unlock(&ngq_mtx); 30728253c060SGleb Smirnoff #endif 30738253c060SGleb Smirnoff uma_zfree(ng_qzone, item); 30748253c060SGleb Smirnoff } 30758253c060SGleb Smirnoff 30768253c060SGleb Smirnoff /************************************************************************ 30774cf49a43SJulian Elischer Module routines 30784cf49a43SJulian Elischer ************************************************************************/ 30794cf49a43SJulian Elischer 30804cf49a43SJulian Elischer /* 30814cf49a43SJulian Elischer * Handle the loading/unloading of a netgraph node type module 30824cf49a43SJulian Elischer */ 30834cf49a43SJulian Elischer int 30844cf49a43SJulian Elischer ng_mod_event(module_t mod, int event, void *data) 30854cf49a43SJulian Elischer { 30864cf49a43SJulian Elischer struct ng_type *const type = data; 30874cf49a43SJulian Elischer int s, error = 0; 30884cf49a43SJulian Elischer 30894cf49a43SJulian Elischer switch (event) { 30904cf49a43SJulian Elischer case MOD_LOAD: 30914cf49a43SJulian Elischer 30924cf49a43SJulian Elischer /* Register new netgraph node type */ 30934cf49a43SJulian Elischer s = splnet(); 30944cf49a43SJulian Elischer if ((error = ng_newtype(type)) != 0) { 30954cf49a43SJulian Elischer splx(s); 30964cf49a43SJulian Elischer break; 30974cf49a43SJulian Elischer } 30984cf49a43SJulian Elischer 30994cf49a43SJulian Elischer /* Call type specific code */ 31004cf49a43SJulian Elischer if (type->mod_event != NULL) 3101069154d5SJulian Elischer if ((error = (*type->mod_event)(mod, event, data))) { 31029ed346baSBosko Milekic mtx_lock(&ng_typelist_mtx); 3103c73b94a2SJulian Elischer type->refs--; /* undo it */ 31044cf49a43SJulian Elischer LIST_REMOVE(type, types); 31059ed346baSBosko Milekic mtx_unlock(&ng_typelist_mtx); 3106069154d5SJulian Elischer } 31074cf49a43SJulian Elischer splx(s); 31084cf49a43SJulian Elischer break; 31094cf49a43SJulian Elischer 31104cf49a43SJulian Elischer case MOD_UNLOAD: 31114cf49a43SJulian Elischer s = splnet(); 3112c73b94a2SJulian Elischer if (type->refs > 1) { /* make sure no nodes exist! */ 31134cf49a43SJulian Elischer error = EBUSY; 3114c73b94a2SJulian Elischer } else { 3115c73b94a2SJulian Elischer if (type->refs == 0) { 3116c73b94a2SJulian Elischer /* failed load, nothing to undo */ 3117c73b94a2SJulian Elischer splx(s); 3118c73b94a2SJulian Elischer break; 3119c73b94a2SJulian Elischer } 31204cf49a43SJulian Elischer if (type->mod_event != NULL) { /* check with type */ 31214cf49a43SJulian Elischer error = (*type->mod_event)(mod, event, data); 31224cf49a43SJulian Elischer if (error != 0) { /* type refuses.. */ 31234cf49a43SJulian Elischer splx(s); 31244cf49a43SJulian Elischer break; 31254cf49a43SJulian Elischer } 31264cf49a43SJulian Elischer } 31279ed346baSBosko Milekic mtx_lock(&ng_typelist_mtx); 31284cf49a43SJulian Elischer LIST_REMOVE(type, types); 31299ed346baSBosko Milekic mtx_unlock(&ng_typelist_mtx); 31304cf49a43SJulian Elischer } 31314cf49a43SJulian Elischer splx(s); 31324cf49a43SJulian Elischer break; 31334cf49a43SJulian Elischer 31344cf49a43SJulian Elischer default: 31354cf49a43SJulian Elischer if (type->mod_event != NULL) 31364cf49a43SJulian Elischer error = (*type->mod_event)(mod, event, data); 31374cf49a43SJulian Elischer else 31383e019deaSPoul-Henning Kamp error = EOPNOTSUPP; /* XXX ? */ 31394cf49a43SJulian Elischer break; 31404cf49a43SJulian Elischer } 31414cf49a43SJulian Elischer return (error); 31424cf49a43SJulian Elischer } 31434cf49a43SJulian Elischer 31444cf49a43SJulian Elischer /* 31454cf49a43SJulian Elischer * Handle loading and unloading for this code. 31464cf49a43SJulian Elischer * The only thing we need to link into is the NETISR strucure. 31474cf49a43SJulian Elischer */ 31484cf49a43SJulian Elischer static int 31494cf49a43SJulian Elischer ngb_mod_event(module_t mod, int event, void *data) 31504cf49a43SJulian Elischer { 31511489164fSGleb Smirnoff int error = 0; 31524cf49a43SJulian Elischer 31534cf49a43SJulian Elischer switch (event) { 31544cf49a43SJulian Elischer case MOD_LOAD: 31551489164fSGleb Smirnoff /* Initialize everything. */ 31562c8dda8dSWojciech A. Koszek NG_WORKLIST_LOCK_INIT(); 3157efd8e7c9SDon Lewis mtx_init(&ng_typelist_mtx, "netgraph types mutex", NULL, 3158efd8e7c9SDon Lewis MTX_DEF); 3159efd8e7c9SDon Lewis mtx_init(&ng_nodelist_mtx, "netgraph nodelist mutex", NULL, 3160efd8e7c9SDon Lewis MTX_DEF); 3161efd8e7c9SDon Lewis mtx_init(&ng_idhash_mtx, "netgraph idhash mutex", NULL, 3162efd8e7c9SDon Lewis MTX_DEF); 3163ac5dd141SGleb Smirnoff mtx_init(&ng_topo_mtx, "netgraph topology mutex", NULL, 3164ac5dd141SGleb Smirnoff MTX_DEF); 31651489164fSGleb Smirnoff #ifdef NETGRAPH_DEBUG 31661489164fSGleb Smirnoff mtx_init(&ngq_mtx, "netgraph item list mutex", NULL, 3167efd8e7c9SDon Lewis MTX_DEF); 31681489164fSGleb Smirnoff #endif 31691489164fSGleb Smirnoff ng_qzone = uma_zcreate("NetGraph items", sizeof(struct ng_item), 31701489164fSGleb Smirnoff NULL, NULL, NULL, NULL, UMA_ALIGN_CACHE, 0); 31711489164fSGleb Smirnoff uma_zone_set_max(ng_qzone, maxalloc); 317268780975SGleb Smirnoff netisr_register(NETISR_NETGRAPH, (netisr_t *)ngintr, NULL, 317368780975SGleb Smirnoff NETISR_MPSAFE); 31744cf49a43SJulian Elischer break; 31754cf49a43SJulian Elischer case MOD_UNLOAD: 317664efc707SRobert Watson /* You can't unload it because an interface may be using it. */ 31774cf49a43SJulian Elischer error = EBUSY; 31784cf49a43SJulian Elischer break; 31794cf49a43SJulian Elischer default: 31804cf49a43SJulian Elischer error = EOPNOTSUPP; 31814cf49a43SJulian Elischer break; 31824cf49a43SJulian Elischer } 31834cf49a43SJulian Elischer return (error); 31844cf49a43SJulian Elischer } 31854cf49a43SJulian Elischer 31864cf49a43SJulian Elischer static moduledata_t netgraph_mod = { 31874cf49a43SJulian Elischer "netgraph", 31884cf49a43SJulian Elischer ngb_mod_event, 31894cf49a43SJulian Elischer (NULL) 31904cf49a43SJulian Elischer }; 3191aa38f8f9SMaksim Yevmenkin DECLARE_MODULE(netgraph, netgraph_mod, SI_SUB_NETGRAPH, SI_ORDER_MIDDLE); 3192bfa7e882SJulian Elischer SYSCTL_NODE(_net, OID_AUTO, graph, CTLFLAG_RW, 0, "netgraph Family"); 3193bfa7e882SJulian Elischer SYSCTL_INT(_net_graph, OID_AUTO, abi_version, CTLFLAG_RD, 0, NG_ABI_VERSION,""); 3194bfa7e882SJulian Elischer SYSCTL_INT(_net_graph, OID_AUTO, msg_version, CTLFLAG_RD, 0, NG_VERSION, ""); 31954cf49a43SJulian Elischer 319630400f03SJulian Elischer #ifdef NETGRAPH_DEBUG 319730400f03SJulian Elischer void 319830400f03SJulian Elischer dumphook (hook_p hook, char *file, int line) 319930400f03SJulian Elischer { 320030400f03SJulian Elischer printf("hook: name %s, %d refs, Last touched:\n", 320130400f03SJulian Elischer _NG_HOOK_NAME(hook), hook->hk_refs); 320230400f03SJulian Elischer printf(" Last active @ %s, line %d\n", 320330400f03SJulian Elischer hook->lastfile, hook->lastline); 320430400f03SJulian Elischer if (line) { 320530400f03SJulian Elischer printf(" problem discovered at file %s, line %d\n", file, line); 320630400f03SJulian Elischer } 320730400f03SJulian Elischer } 320830400f03SJulian Elischer 320930400f03SJulian Elischer void 321030400f03SJulian Elischer dumpnode(node_p node, char *file, int line) 321130400f03SJulian Elischer { 321230400f03SJulian Elischer printf("node: ID [%x]: type '%s', %d hooks, flags 0x%x, %d refs, %s:\n", 32136b795970SJulian Elischer _NG_NODE_ID(node), node->nd_type->name, 321430400f03SJulian Elischer node->nd_numhooks, node->nd_flags, 321530400f03SJulian Elischer node->nd_refs, node->nd_name); 321630400f03SJulian Elischer printf(" Last active @ %s, line %d\n", 321730400f03SJulian Elischer node->lastfile, node->lastline); 321830400f03SJulian Elischer if (line) { 321930400f03SJulian Elischer printf(" problem discovered at file %s, line %d\n", file, line); 322030400f03SJulian Elischer } 322130400f03SJulian Elischer } 322230400f03SJulian Elischer 3223069154d5SJulian Elischer void 3224069154d5SJulian Elischer dumpitem(item_p item, char *file, int line) 3225069154d5SJulian Elischer { 3226069154d5SJulian Elischer printf(" ACTIVE item, last used at %s, line %d", 3227069154d5SJulian Elischer item->lastfile, item->lastline); 32286b795970SJulian Elischer switch(item->el_flags & NGQF_TYPE) { 32296b795970SJulian Elischer case NGQF_DATA: 3230069154d5SJulian Elischer printf(" - [data]\n"); 32316b795970SJulian Elischer break; 32326b795970SJulian Elischer case NGQF_MESG: 32336b795970SJulian Elischer printf(" - retaddr[%d]:\n", _NGI_RETADDR(item)); 32346b795970SJulian Elischer break; 32356b795970SJulian Elischer case NGQF_FN: 32366b795970SJulian Elischer printf(" - fn@%p (%p, %p, %p, %d (%x))\n", 32376b795970SJulian Elischer item->body.fn.fn_fn, 32386064e568SGleb Smirnoff _NGI_NODE(item), 32396064e568SGleb Smirnoff _NGI_HOOK(item), 32406b795970SJulian Elischer item->body.fn.fn_arg1, 32416b795970SJulian Elischer item->body.fn.fn_arg2, 32426b795970SJulian Elischer item->body.fn.fn_arg2); 32436b795970SJulian Elischer break; 32446b795970SJulian Elischer case NGQF_UNDEF: 32456b795970SJulian Elischer printf(" - UNDEFINED!\n"); 3246069154d5SJulian Elischer } 324730400f03SJulian Elischer if (line) { 3248069154d5SJulian Elischer printf(" problem discovered at file %s, line %d\n", file, line); 32496064e568SGleb Smirnoff if (_NGI_NODE(item)) { 325030400f03SJulian Elischer printf("node %p ([%x])\n", 32516064e568SGleb Smirnoff _NGI_NODE(item), ng_node2ID(_NGI_NODE(item))); 3252069154d5SJulian Elischer } 325330400f03SJulian Elischer } 325430400f03SJulian Elischer } 325530400f03SJulian Elischer 325630400f03SJulian Elischer static void 325730400f03SJulian Elischer ng_dumpitems(void) 325830400f03SJulian Elischer { 325930400f03SJulian Elischer item_p item; 326030400f03SJulian Elischer int i = 1; 326130400f03SJulian Elischer TAILQ_FOREACH(item, &ng_itemlist, all) { 326230400f03SJulian Elischer printf("[%d] ", i++); 326330400f03SJulian Elischer dumpitem(item, NULL, 0); 326430400f03SJulian Elischer } 326530400f03SJulian Elischer } 326630400f03SJulian Elischer 326730400f03SJulian Elischer static void 326830400f03SJulian Elischer ng_dumpnodes(void) 326930400f03SJulian Elischer { 327030400f03SJulian Elischer node_p node; 327130400f03SJulian Elischer int i = 1; 327253f9c5e9SRobert Watson mtx_lock(&ng_nodelist_mtx); 327330400f03SJulian Elischer SLIST_FOREACH(node, &ng_allnodes, nd_all) { 327430400f03SJulian Elischer printf("[%d] ", i++); 327530400f03SJulian Elischer dumpnode(node, NULL, 0); 327630400f03SJulian Elischer } 327753f9c5e9SRobert Watson mtx_unlock(&ng_nodelist_mtx); 327830400f03SJulian Elischer } 327930400f03SJulian Elischer 328030400f03SJulian Elischer static void 328130400f03SJulian Elischer ng_dumphooks(void) 328230400f03SJulian Elischer { 328330400f03SJulian Elischer hook_p hook; 328430400f03SJulian Elischer int i = 1; 328553f9c5e9SRobert Watson mtx_lock(&ng_nodelist_mtx); 328630400f03SJulian Elischer SLIST_FOREACH(hook, &ng_allhooks, hk_all) { 328730400f03SJulian Elischer printf("[%d] ", i++); 328830400f03SJulian Elischer dumphook(hook, NULL, 0); 328930400f03SJulian Elischer } 329053f9c5e9SRobert Watson mtx_unlock(&ng_nodelist_mtx); 329130400f03SJulian Elischer } 3292069154d5SJulian Elischer 3293069154d5SJulian Elischer static int 3294069154d5SJulian Elischer sysctl_debug_ng_dump_items(SYSCTL_HANDLER_ARGS) 3295069154d5SJulian Elischer { 3296069154d5SJulian Elischer int error; 3297069154d5SJulian Elischer int val; 3298069154d5SJulian Elischer int i; 3299069154d5SJulian Elischer 3300069154d5SJulian Elischer val = allocated; 3301069154d5SJulian Elischer i = 1; 3302069154d5SJulian Elischer error = sysctl_handle_int(oidp, &val, sizeof(int), req); 33036b795970SJulian Elischer if (error != 0 || req->newptr == NULL) 33046b795970SJulian Elischer return (error); 33056b795970SJulian Elischer if (val == 42) { 330630400f03SJulian Elischer ng_dumpitems(); 330730400f03SJulian Elischer ng_dumpnodes(); 330830400f03SJulian Elischer ng_dumphooks(); 3309069154d5SJulian Elischer } 33106b795970SJulian Elischer return (0); 3311069154d5SJulian Elischer } 3312069154d5SJulian Elischer 33136b795970SJulian Elischer SYSCTL_PROC(_debug, OID_AUTO, ng_dump_items, CTLTYPE_INT | CTLFLAG_RW, 33146b795970SJulian Elischer 0, sizeof(int), sysctl_debug_ng_dump_items, "I", "Number of allocated items"); 331530400f03SJulian Elischer #endif /* NETGRAPH_DEBUG */ 3316069154d5SJulian Elischer 3317069154d5SJulian Elischer 3318069154d5SJulian Elischer /*********************************************************************** 3319069154d5SJulian Elischer * Worklist routines 3320069154d5SJulian Elischer **********************************************************************/ 3321069154d5SJulian Elischer /* NETISR thread enters here */ 3322069154d5SJulian Elischer /* 3323069154d5SJulian Elischer * Pick a node off the list of nodes with work, 3324069154d5SJulian Elischer * try get an item to process off it. 3325069154d5SJulian Elischer * If there are no more, remove the node from the list. 3326069154d5SJulian Elischer */ 3327069154d5SJulian Elischer static void 3328069154d5SJulian Elischer ngintr(void) 3329069154d5SJulian Elischer { 3330069154d5SJulian Elischer item_p item; 3331069154d5SJulian Elischer node_p node = NULL; 3332069154d5SJulian Elischer 3333069154d5SJulian Elischer for (;;) { 33342c8dda8dSWojciech A. Koszek NG_WORKLIST_LOCK(); 3335069154d5SJulian Elischer node = TAILQ_FIRST(&ng_worklist); 3336069154d5SJulian Elischer if (!node) { 33372c8dda8dSWojciech A. Koszek NG_WORKLIST_UNLOCK(); 3338069154d5SJulian Elischer break; 3339069154d5SJulian Elischer } 3340be4252b3SJulian Elischer node->nd_flags &= ~NGF_WORKQ; 334130400f03SJulian Elischer TAILQ_REMOVE(&ng_worklist, node, nd_work); 33422c8dda8dSWojciech A. Koszek NG_WORKLIST_UNLOCK(); 33432955ee18SGleb Smirnoff CTR3(KTR_NET, "%20s: node [%x] (%p) taken off worklist", 33442955ee18SGleb Smirnoff __func__, node->nd_ID, node); 3345069154d5SJulian Elischer /* 3346069154d5SJulian Elischer * We have the node. We also take over the reference 3347069154d5SJulian Elischer * that the list had on it. 3348069154d5SJulian Elischer * Now process as much as you can, until it won't 3349069154d5SJulian Elischer * let you have another item off the queue. 3350069154d5SJulian Elischer * All this time, keep the reference 3351069154d5SJulian Elischer * that lets us be sure that the node still exists. 3352069154d5SJulian Elischer * Let the reference go at the last minute. 3353b57a7965SJulian Elischer * ng_dequeue will put us back on the worklist 3354b57a7965SJulian Elischer * if there is more too do. This may be of use if there 3355b57a7965SJulian Elischer * are Multiple Processors and multiple Net threads in the 3356b57a7965SJulian Elischer * future. 3357069154d5SJulian Elischer */ 3358069154d5SJulian Elischer for (;;) { 3359714fb865SGleb Smirnoff int rw; 3360714fb865SGleb Smirnoff 33612c8dda8dSWojciech A. Koszek NG_QUEUE_LOCK(&node->nd_input_queue); 3362714fb865SGleb Smirnoff item = ng_dequeue(&node->nd_input_queue, &rw); 3363069154d5SJulian Elischer if (item == NULL) { 33642c8dda8dSWojciech A. Koszek NG_QUEUE_UNLOCK(&node->nd_input_queue); 3365069154d5SJulian Elischer break; /* go look for another node */ 3366069154d5SJulian Elischer } else { 33672c8dda8dSWojciech A. Koszek NG_QUEUE_UNLOCK(&node->nd_input_queue); 33685951069aSJulian Elischer NGI_GET_NODE(item, node); /* zaps stored node */ 3369714fb865SGleb Smirnoff ng_apply_item(node, item, rw); 33705951069aSJulian Elischer NG_NODE_UNREF(node); 3371069154d5SJulian Elischer } 3372069154d5SJulian Elischer } 3373a96dcd84SJulian Elischer NG_NODE_UNREF(node); 3374069154d5SJulian Elischer } 3375069154d5SJulian Elischer } 3376069154d5SJulian Elischer 3377069154d5SJulian Elischer static void 3378069154d5SJulian Elischer ng_worklist_remove(node_p node) 3379069154d5SJulian Elischer { 33803b33fbe7SGleb Smirnoff mtx_assert(&node->nd_input_queue.q_mtx, MA_OWNED); 33813b33fbe7SGleb Smirnoff 33822c8dda8dSWojciech A. Koszek NG_WORKLIST_LOCK(); 3383be4252b3SJulian Elischer if (node->nd_flags & NGF_WORKQ) { 3384be4252b3SJulian Elischer node->nd_flags &= ~NGF_WORKQ; 338533338e73SJulian Elischer TAILQ_REMOVE(&ng_worklist, node, nd_work); 33862c8dda8dSWojciech A. Koszek NG_WORKLIST_UNLOCK(); 338733338e73SJulian Elischer NG_NODE_UNREF(node); 33882955ee18SGleb Smirnoff CTR3(KTR_NET, "%20s: node [%x] (%p) removed from worklist", 33892955ee18SGleb Smirnoff __func__, node->nd_ID, node); 339033338e73SJulian Elischer } else { 33912c8dda8dSWojciech A. Koszek NG_WORKLIST_UNLOCK(); 3392069154d5SJulian Elischer } 339333338e73SJulian Elischer } 3394069154d5SJulian Elischer 339533338e73SJulian Elischer /* 339633338e73SJulian Elischer * XXX 339733338e73SJulian Elischer * It's posible that a debugging NG_NODE_REF may need 339833338e73SJulian Elischer * to be outside the mutex zone 339933338e73SJulian Elischer */ 3400069154d5SJulian Elischer static void 3401069154d5SJulian Elischer ng_setisr(node_p node) 3402069154d5SJulian Elischer { 3403f912c0f7SGleb Smirnoff 34045bc15201SGleb Smirnoff mtx_assert(&node->nd_input_queue.q_mtx, MA_OWNED); 3405f912c0f7SGleb Smirnoff 3406be4252b3SJulian Elischer if ((node->nd_flags & NGF_WORKQ) == 0) { 3407069154d5SJulian Elischer /* 3408069154d5SJulian Elischer * If we are not already on the work queue, 3409069154d5SJulian Elischer * then put us on. 3410069154d5SJulian Elischer */ 3411be4252b3SJulian Elischer node->nd_flags |= NGF_WORKQ; 34122c8dda8dSWojciech A. Koszek NG_WORKLIST_LOCK(); 341330400f03SJulian Elischer TAILQ_INSERT_TAIL(&ng_worklist, node, nd_work); 34142c8dda8dSWojciech A. Koszek NG_WORKLIST_UNLOCK(); 341533338e73SJulian Elischer NG_NODE_REF(node); /* XXX fafe in mutex? */ 34162955ee18SGleb Smirnoff CTR3(KTR_NET, "%20s: node [%x] (%p) put on worklist", __func__, 34172955ee18SGleb Smirnoff node->nd_ID, node); 34183b33fbe7SGleb Smirnoff } else 34192955ee18SGleb Smirnoff CTR3(KTR_NET, "%20s: node [%x] (%p) already on worklist", 34202955ee18SGleb Smirnoff __func__, node->nd_ID, node); 3421069154d5SJulian Elischer schednetisr(NETISR_NETGRAPH); 3422069154d5SJulian Elischer } 3423069154d5SJulian Elischer 3424069154d5SJulian Elischer 3425069154d5SJulian Elischer /*********************************************************************** 3426069154d5SJulian Elischer * Externally useable functions to set up a queue item ready for sending 3427069154d5SJulian Elischer ***********************************************************************/ 3428069154d5SJulian Elischer 342930400f03SJulian Elischer #ifdef NETGRAPH_DEBUG 343030400f03SJulian Elischer #define ITEM_DEBUG_CHECKS \ 34314cf49a43SJulian Elischer do { \ 34321acb27c6SJulian Elischer if (NGI_NODE(item) ) { \ 3433069154d5SJulian Elischer printf("item already has node"); \ 3434f33ca0c9SMarcel Moolenaar kdb_enter("has node"); \ 34351acb27c6SJulian Elischer NGI_CLR_NODE(item); \ 3436069154d5SJulian Elischer } \ 34371acb27c6SJulian Elischer if (NGI_HOOK(item) ) { \ 3438069154d5SJulian Elischer printf("item already has hook"); \ 3439f33ca0c9SMarcel Moolenaar kdb_enter("has hook"); \ 34401acb27c6SJulian Elischer NGI_CLR_HOOK(item); \ 3441069154d5SJulian Elischer } \ 3442069154d5SJulian Elischer } while (0) 3443069154d5SJulian Elischer #else 344430400f03SJulian Elischer #define ITEM_DEBUG_CHECKS 3445069154d5SJulian Elischer #endif 3446069154d5SJulian Elischer 3447069154d5SJulian Elischer /* 34488ed370fdSJulian Elischer * Put mbuf into the item. 3449069154d5SJulian Elischer * Hook and node references will be removed when the item is dequeued. 3450069154d5SJulian Elischer * (or equivalent) 3451069154d5SJulian Elischer * (XXX) Unsafe because no reference held by peer on remote node. 3452069154d5SJulian Elischer * remote node might go away in this timescale. 3453069154d5SJulian Elischer * We know the hooks can't go away because that would require getting 3454069154d5SJulian Elischer * a writer item on both nodes and we must have at least a reader 34554be59335SGleb Smirnoff * here to be able to do this. 3456069154d5SJulian Elischer * Note that the hook loaded is the REMOTE hook. 3457069154d5SJulian Elischer * 3458069154d5SJulian Elischer * This is possibly in the critical path for new data. 3459069154d5SJulian Elischer */ 3460069154d5SJulian Elischer item_p 346142282202SGleb Smirnoff ng_package_data(struct mbuf *m, int flags) 3462069154d5SJulian Elischer { 3463069154d5SJulian Elischer item_p item; 3464069154d5SJulian Elischer 346542282202SGleb Smirnoff if ((item = ng_getqblk(flags)) == NULL) { 3466069154d5SJulian Elischer NG_FREE_M(m); 3467069154d5SJulian Elischer return (NULL); 3468069154d5SJulian Elischer } 346930400f03SJulian Elischer ITEM_DEBUG_CHECKS; 34706f683eeeSGleb Smirnoff item->el_flags = NGQF_DATA | NGQF_READER; 3471069154d5SJulian Elischer item->el_next = NULL; 3472069154d5SJulian Elischer NGI_M(item) = m; 3473069154d5SJulian Elischer return (item); 3474069154d5SJulian Elischer } 3475069154d5SJulian Elischer 3476069154d5SJulian Elischer /* 3477069154d5SJulian Elischer * Allocate a queue item and put items into it.. 3478069154d5SJulian Elischer * Evaluate the address as this will be needed to queue it and 3479069154d5SJulian Elischer * to work out what some of the fields should be. 3480069154d5SJulian Elischer * Hook and node references will be removed when the item is dequeued. 3481069154d5SJulian Elischer * (or equivalent) 3482069154d5SJulian Elischer */ 3483069154d5SJulian Elischer item_p 348442282202SGleb Smirnoff ng_package_msg(struct ng_mesg *msg, int flags) 3485069154d5SJulian Elischer { 3486069154d5SJulian Elischer item_p item; 3487069154d5SJulian Elischer 348842282202SGleb Smirnoff if ((item = ng_getqblk(flags)) == NULL) { 3489069154d5SJulian Elischer NG_FREE_MSG(msg); 3490069154d5SJulian Elischer return (NULL); 3491069154d5SJulian Elischer } 349230400f03SJulian Elischer ITEM_DEBUG_CHECKS; 34936f683eeeSGleb Smirnoff /* Messages items count as writers unless explicitly exempted. */ 34946f683eeeSGleb Smirnoff if (msg->header.cmd & NGM_READONLY) 34956f683eeeSGleb Smirnoff item->el_flags = NGQF_MESG | NGQF_READER; 34966f683eeeSGleb Smirnoff else 34976f683eeeSGleb Smirnoff item->el_flags = NGQF_MESG | NGQF_WRITER; 3498069154d5SJulian Elischer item->el_next = NULL; 3499069154d5SJulian Elischer /* 3500069154d5SJulian Elischer * Set the current lasthook into the queue item 3501069154d5SJulian Elischer */ 3502069154d5SJulian Elischer NGI_MSG(item) = msg; 3503facfd889SArchie Cobbs NGI_RETADDR(item) = 0; 3504069154d5SJulian Elischer return (item); 3505069154d5SJulian Elischer } 3506069154d5SJulian Elischer 3507069154d5SJulian Elischer 3508069154d5SJulian Elischer 35091acb27c6SJulian Elischer #define SET_RETADDR(item, here, retaddr) \ 35106b795970SJulian Elischer do { /* Data or fn items don't have retaddrs */ \ 35116b795970SJulian Elischer if ((item->el_flags & NGQF_TYPE) == NGQF_MESG) { \ 3512069154d5SJulian Elischer if (retaddr) { \ 3513069154d5SJulian Elischer NGI_RETADDR(item) = retaddr; \ 35144cf49a43SJulian Elischer } else { \ 3515069154d5SJulian Elischer /* \ 3516069154d5SJulian Elischer * The old return address should be ok. \ 3517069154d5SJulian Elischer * If there isn't one, use the address \ 3518069154d5SJulian Elischer * here. \ 3519069154d5SJulian Elischer */ \ 3520069154d5SJulian Elischer if (NGI_RETADDR(item) == 0) { \ 3521069154d5SJulian Elischer NGI_RETADDR(item) \ 3522069154d5SJulian Elischer = ng_node2ID(here); \ 3523069154d5SJulian Elischer } \ 3524069154d5SJulian Elischer } \ 35254cf49a43SJulian Elischer } \ 35264cf49a43SJulian Elischer } while (0) 35274cf49a43SJulian Elischer 35284cf49a43SJulian Elischer int 3529069154d5SJulian Elischer ng_address_hook(node_p here, item_p item, hook_p hook, ng_ID_t retaddr) 35304cf49a43SJulian Elischer { 35311acb27c6SJulian Elischer hook_p peer; 35321acb27c6SJulian Elischer node_p peernode; 353330400f03SJulian Elischer ITEM_DEBUG_CHECKS; 3534069154d5SJulian Elischer /* 3535069154d5SJulian Elischer * Quick sanity check.. 353630400f03SJulian Elischer * Since a hook holds a reference on it's node, once we know 353730400f03SJulian Elischer * that the peer is still connected (even if invalid,) we know 353830400f03SJulian Elischer * that the peer node is present, though maybe invalid. 3539069154d5SJulian Elischer */ 3540069154d5SJulian Elischer if ((hook == NULL) 354130400f03SJulian Elischer || NG_HOOK_NOT_VALID(hook) 354230400f03SJulian Elischer || (NG_HOOK_PEER(hook) == NULL) 354330400f03SJulian Elischer || NG_HOOK_NOT_VALID(NG_HOOK_PEER(hook)) 354430400f03SJulian Elischer || NG_NODE_NOT_VALID(NG_PEER_NODE(hook))) { 3545069154d5SJulian Elischer NG_FREE_ITEM(item); 35466b795970SJulian Elischer TRAP_ERROR(); 3547e08d3e3cSJulian Elischer return (ENETDOWN); 35484cf49a43SJulian Elischer } 35494cf49a43SJulian Elischer 35504cf49a43SJulian Elischer /* 3551069154d5SJulian Elischer * Transfer our interest to the other (peer) end. 35524cf49a43SJulian Elischer */ 35531acb27c6SJulian Elischer peer = NG_HOOK_PEER(hook); 35541acb27c6SJulian Elischer NG_HOOK_REF(peer); 35551acb27c6SJulian Elischer NGI_SET_HOOK(item, peer); 35561acb27c6SJulian Elischer peernode = NG_PEER_NODE(hook); 35571acb27c6SJulian Elischer NG_NODE_REF(peernode); 35581acb27c6SJulian Elischer NGI_SET_NODE(item, peernode); 35598b68f82fSJulian Elischer SET_RETADDR(item, here, retaddr); 3560069154d5SJulian Elischer return (0); 3561069154d5SJulian Elischer } 3562069154d5SJulian Elischer 35634cf49a43SJulian Elischer int 3564069154d5SJulian Elischer ng_address_path(node_p here, item_p item, char *address, ng_ID_t retaddr) 35654cf49a43SJulian Elischer { 35664cf49a43SJulian Elischer node_p dest = NULL; 3567069154d5SJulian Elischer hook_p hook = NULL; 35684cf49a43SJulian Elischer int error; 3569069154d5SJulian Elischer 357030400f03SJulian Elischer ITEM_DEBUG_CHECKS; 3571069154d5SJulian Elischer /* 3572069154d5SJulian Elischer * Note that ng_path2noderef increments the reference count 3573069154d5SJulian Elischer * on the node for us if it finds one. So we don't have to. 3574069154d5SJulian Elischer */ 3575069154d5SJulian Elischer error = ng_path2noderef(here, address, &dest, &hook); 3576069154d5SJulian Elischer if (error) { 3577069154d5SJulian Elischer NG_FREE_ITEM(item); 357830400f03SJulian Elischer return (error); 3579069154d5SJulian Elischer } 35801acb27c6SJulian Elischer NGI_SET_NODE(item, dest); 35811acb27c6SJulian Elischer if ( hook) { 358230400f03SJulian Elischer NG_HOOK_REF(hook); /* don't let it go while on the queue */ 35831acb27c6SJulian Elischer NGI_SET_HOOK(item, hook); 35841acb27c6SJulian Elischer } 35851acb27c6SJulian Elischer SET_RETADDR(item, here, retaddr); 3586069154d5SJulian Elischer return (0); 3587069154d5SJulian Elischer } 3588069154d5SJulian Elischer 3589069154d5SJulian Elischer int 3590069154d5SJulian Elischer ng_address_ID(node_p here, item_p item, ng_ID_t ID, ng_ID_t retaddr) 3591069154d5SJulian Elischer { 3592069154d5SJulian Elischer node_p dest; 3593069154d5SJulian Elischer 359430400f03SJulian Elischer ITEM_DEBUG_CHECKS; 3595069154d5SJulian Elischer /* 3596069154d5SJulian Elischer * Find the target node. 3597069154d5SJulian Elischer */ 3598069154d5SJulian Elischer dest = ng_ID2noderef(ID); /* GETS REFERENCE! */ 3599069154d5SJulian Elischer if (dest == NULL) { 3600069154d5SJulian Elischer NG_FREE_ITEM(item); 36016b795970SJulian Elischer TRAP_ERROR(); 3602069154d5SJulian Elischer return(EINVAL); 3603069154d5SJulian Elischer } 3604069154d5SJulian Elischer /* Fill out the contents */ 36051acb27c6SJulian Elischer NGI_SET_NODE(item, dest); 36061acb27c6SJulian Elischer NGI_CLR_HOOK(item); 36071acb27c6SJulian Elischer SET_RETADDR(item, here, retaddr); 3608069154d5SJulian Elischer return (0); 3609069154d5SJulian Elischer } 3610069154d5SJulian Elischer 3611069154d5SJulian Elischer /* 3612069154d5SJulian Elischer * special case to send a message to self (e.g. destroy node) 3613069154d5SJulian Elischer * Possibly indicate an arrival hook too. 3614069154d5SJulian Elischer * Useful for removing that hook :-) 3615069154d5SJulian Elischer */ 3616069154d5SJulian Elischer item_p 3617069154d5SJulian Elischer ng_package_msg_self(node_p here, hook_p hook, struct ng_mesg *msg) 3618069154d5SJulian Elischer { 3619069154d5SJulian Elischer item_p item; 36204cf49a43SJulian Elischer 3621859a4d16SJulian Elischer /* 3622859a4d16SJulian Elischer * Find the target node. 3623859a4d16SJulian Elischer * If there is a HOOK argument, then use that in preference 3624859a4d16SJulian Elischer * to the address. 3625859a4d16SJulian Elischer */ 362642282202SGleb Smirnoff if ((item = ng_getqblk(NG_NOFLAGS)) == NULL) { 3627069154d5SJulian Elischer NG_FREE_MSG(msg); 3628069154d5SJulian Elischer return (NULL); 36294cf49a43SJulian Elischer } 36304cf49a43SJulian Elischer 36314cf49a43SJulian Elischer /* Fill out the contents */ 36326f683eeeSGleb Smirnoff item->el_flags = NGQF_MESG | NGQF_WRITER; 3633069154d5SJulian Elischer item->el_next = NULL; 363430400f03SJulian Elischer NG_NODE_REF(here); 36351acb27c6SJulian Elischer NGI_SET_NODE(item, here); 36361acb27c6SJulian Elischer if (hook) { 363730400f03SJulian Elischer NG_HOOK_REF(hook); 36381acb27c6SJulian Elischer NGI_SET_HOOK(item, hook); 36391acb27c6SJulian Elischer } 3640069154d5SJulian Elischer NGI_MSG(item) = msg; 3641069154d5SJulian Elischer NGI_RETADDR(item) = ng_node2ID(here); 3642069154d5SJulian Elischer return (item); 36434cf49a43SJulian Elischer } 36444cf49a43SJulian Elischer 364542282202SGleb Smirnoff int 3646aacdb114SGleb Smirnoff ng_send_fn1(node_p node, hook_p hook, ng_item_fn *fn, void * arg1, int arg2, 364742282202SGleb Smirnoff int flags) 36486b795970SJulian Elischer { 36496b795970SJulian Elischer item_p item; 36506b795970SJulian Elischer 365142282202SGleb Smirnoff if ((item = ng_getqblk(flags)) == NULL) { 36526b795970SJulian Elischer return (ENOMEM); 36536b795970SJulian Elischer } 36546b795970SJulian Elischer item->el_flags = NGQF_FN | NGQF_WRITER; 3655a96dcd84SJulian Elischer NG_NODE_REF(node); /* and one for the item */ 36561acb27c6SJulian Elischer NGI_SET_NODE(item, node); 36571acb27c6SJulian Elischer if (hook) { 36586b795970SJulian Elischer NG_HOOK_REF(hook); 36591acb27c6SJulian Elischer NGI_SET_HOOK(item, hook); 36606b795970SJulian Elischer } 36616b795970SJulian Elischer NGI_FN(item) = fn; 36626b795970SJulian Elischer NGI_ARG1(item) = arg1; 36636b795970SJulian Elischer NGI_ARG2(item) = arg2; 366442282202SGleb Smirnoff return(ng_snd_item(item, flags)); 36656b795970SJulian Elischer } 36666b795970SJulian Elischer 36674cf49a43SJulian Elischer /* 3668d2ca21a9SJulian Elischer * Official timeout routines for Netgraph nodes. 3669d2ca21a9SJulian Elischer */ 3670d2ca21a9SJulian Elischer static void 36711fbb36ffSGleb Smirnoff ng_callout_trampoline(void *arg) 3672d2ca21a9SJulian Elischer { 3673d2ca21a9SJulian Elischer item_p item = arg; 3674d2ca21a9SJulian Elischer 3675d2ca21a9SJulian Elischer ng_snd_item(item, 0); 3676d2ca21a9SJulian Elischer } 3677d2ca21a9SJulian Elischer 3678d2ca21a9SJulian Elischer 367930bef41bSGleb Smirnoff int 3680f9d9e1b4SGleb Smirnoff ng_callout(struct callout *c, node_p node, hook_p hook, int ticks, 3681d2ca21a9SJulian Elischer ng_item_fn *fn, void * arg1, int arg2) 3682d2ca21a9SJulian Elischer { 36831bf8e0faSGleb Smirnoff item_p item, oitem; 3684d2ca21a9SJulian Elischer 368542282202SGleb Smirnoff if ((item = ng_getqblk(NG_NOFLAGS)) == NULL) 368630bef41bSGleb Smirnoff return (ENOMEM); 368730bef41bSGleb Smirnoff 3688d2ca21a9SJulian Elischer item->el_flags = NGQF_FN | NGQF_WRITER; 3689d2ca21a9SJulian Elischer NG_NODE_REF(node); /* and one for the item */ 3690d2ca21a9SJulian Elischer NGI_SET_NODE(item, node); 3691d2ca21a9SJulian Elischer if (hook) { 3692d2ca21a9SJulian Elischer NG_HOOK_REF(hook); 3693d2ca21a9SJulian Elischer NGI_SET_HOOK(item, hook); 3694d2ca21a9SJulian Elischer } 3695d2ca21a9SJulian Elischer NGI_FN(item) = fn; 3696d2ca21a9SJulian Elischer NGI_ARG1(item) = arg1; 3697d2ca21a9SJulian Elischer NGI_ARG2(item) = arg2; 36981bf8e0faSGleb Smirnoff oitem = c->c_arg; 36991bf8e0faSGleb Smirnoff if (callout_reset(c, ticks, &ng_callout_trampoline, item) == 1 && 37001bf8e0faSGleb Smirnoff oitem != NULL) 37011bf8e0faSGleb Smirnoff NG_FREE_ITEM(oitem); 370230bef41bSGleb Smirnoff return (0); 3703d2ca21a9SJulian Elischer } 3704d2ca21a9SJulian Elischer 3705d2ca21a9SJulian Elischer /* A special modified version of untimeout() */ 3706d2ca21a9SJulian Elischer int 3707f9d9e1b4SGleb Smirnoff ng_uncallout(struct callout *c, node_p node) 3708d2ca21a9SJulian Elischer { 3709d2ca21a9SJulian Elischer item_p item; 371030bef41bSGleb Smirnoff int rval; 3711d2ca21a9SJulian Elischer 371203b25f5dSGleb Smirnoff KASSERT(c != NULL, ("ng_uncallout: NULL callout")); 371303b25f5dSGleb Smirnoff KASSERT(node != NULL, ("ng_uncallout: NULL node")); 371403b25f5dSGleb Smirnoff 37153eadb26dSGleb Smirnoff rval = callout_stop(c); 371630bef41bSGleb Smirnoff item = c->c_arg; 371730bef41bSGleb Smirnoff /* Do an extra check */ 37181fbb36ffSGleb Smirnoff if ((rval > 0) && (c->c_func == &ng_callout_trampoline) && 371930bef41bSGleb Smirnoff (NGI_NODE(item) == node)) { 3720d2ca21a9SJulian Elischer /* 3721d2ca21a9SJulian Elischer * We successfully removed it from the queue before it ran 3722d2ca21a9SJulian Elischer * So now we need to unreference everything that was 3723d2ca21a9SJulian Elischer * given extra references. (NG_FREE_ITEM does this). 3724d2ca21a9SJulian Elischer */ 3725d2ca21a9SJulian Elischer NG_FREE_ITEM(item); 3726d2ca21a9SJulian Elischer } 37271bf8e0faSGleb Smirnoff c->c_arg = NULL; 372830bef41bSGleb Smirnoff 372930bef41bSGleb Smirnoff return (rval); 3730d2ca21a9SJulian Elischer } 3731d2ca21a9SJulian Elischer 3732d2ca21a9SJulian Elischer /* 3733069154d5SJulian Elischer * Set the address, if none given, give the node here. 37344cf49a43SJulian Elischer */ 3735069154d5SJulian Elischer void 3736069154d5SJulian Elischer ng_replace_retaddr(node_p here, item_p item, ng_ID_t retaddr) 37374cf49a43SJulian Elischer { 3738069154d5SJulian Elischer if (retaddr) { 3739069154d5SJulian Elischer NGI_RETADDR(item) = retaddr; 3740069154d5SJulian Elischer } else { 3741069154d5SJulian Elischer /* 3742069154d5SJulian Elischer * The old return address should be ok. 3743069154d5SJulian Elischer * If there isn't one, use the address here. 3744069154d5SJulian Elischer */ 3745069154d5SJulian Elischer NGI_RETADDR(item) = ng_node2ID(here); 3746069154d5SJulian Elischer } 3747069154d5SJulian Elischer } 3748069154d5SJulian Elischer 3749069154d5SJulian Elischer #define TESTING 3750069154d5SJulian Elischer #ifdef TESTING 3751069154d5SJulian Elischer /* just test all the macros */ 3752069154d5SJulian Elischer void 3753069154d5SJulian Elischer ng_macro_test(item_p item); 3754069154d5SJulian Elischer void 3755069154d5SJulian Elischer ng_macro_test(item_p item) 3756069154d5SJulian Elischer { 3757069154d5SJulian Elischer node_p node = NULL; 3758069154d5SJulian Elischer hook_p hook = NULL; 37594cf49a43SJulian Elischer struct mbuf *m; 37604cf49a43SJulian Elischer struct ng_mesg *msg; 3761069154d5SJulian Elischer ng_ID_t retaddr; 3762069154d5SJulian Elischer int error; 37634cf49a43SJulian Elischer 3764069154d5SJulian Elischer NGI_GET_M(item, m); 3765069154d5SJulian Elischer NGI_GET_MSG(item, msg); 3766069154d5SJulian Elischer retaddr = NGI_RETADDR(item); 37678ed370fdSJulian Elischer NG_SEND_DATA(error, hook, m, NULL); 3768069154d5SJulian Elischer NG_SEND_DATA_ONLY(error, hook, m); 3769069154d5SJulian Elischer NG_FWD_NEW_DATA(error, item, hook, m); 377030400f03SJulian Elischer NG_FWD_ITEM_HOOK(error, item, hook); 3771069154d5SJulian Elischer NG_SEND_MSG_HOOK(error, node, msg, hook, retaddr); 3772069154d5SJulian Elischer NG_SEND_MSG_ID(error, node, msg, retaddr, retaddr); 3773069154d5SJulian Elischer NG_SEND_MSG_PATH(error, node, msg, ".:", retaddr); 3774069154d5SJulian Elischer NG_FWD_MSG_HOOK(error, node, item, hook, retaddr); 37754cf49a43SJulian Elischer } 3776069154d5SJulian Elischer #endif /* TESTING */ 37774cf49a43SJulian Elischer 3778