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> 62e088dd4cSAlexander Motin #include <sys/refcount.h> 6381a253a4SAlexander Motin #include <sys/proc.h> 644cf49a43SJulian Elischer 654cf49a43SJulian Elischer #include <net/netisr.h> 664cf49a43SJulian Elischer 674cf49a43SJulian Elischer #include <netgraph/ng_message.h> 684cf49a43SJulian Elischer #include <netgraph/netgraph.h> 69f8307e12SArchie Cobbs #include <netgraph/ng_parse.h> 704cf49a43SJulian Elischer 719d72a7a3SJulian Elischer MODULE_VERSION(netgraph, NG_ABI_VERSION); 7299ff8176SPeter Wemm 7330400f03SJulian Elischer /* List of all active nodes */ 74069154d5SJulian Elischer static LIST_HEAD(, ng_node) ng_nodelist; 75069154d5SJulian Elischer static struct mtx ng_nodelist_mtx; 76069154d5SJulian Elischer 77ac5dd141SGleb Smirnoff /* Mutex to protect topology events. */ 78ac5dd141SGleb Smirnoff static struct mtx ng_topo_mtx; 79ac5dd141SGleb Smirnoff 8030400f03SJulian Elischer #ifdef NETGRAPH_DEBUG 811489164fSGleb Smirnoff static struct mtx ngq_mtx; /* protects the queue item list */ 8230400f03SJulian Elischer 8330400f03SJulian Elischer static SLIST_HEAD(, ng_node) ng_allnodes; 8430400f03SJulian Elischer static LIST_HEAD(, ng_node) ng_freenodes; /* in debug, we never free() them */ 8530400f03SJulian Elischer static SLIST_HEAD(, ng_hook) ng_allhooks; 8630400f03SJulian Elischer static LIST_HEAD(, ng_hook) ng_freehooks; /* in debug, we never free() them */ 8730400f03SJulian Elischer 8830400f03SJulian Elischer static void ng_dumpitems(void); 8930400f03SJulian Elischer static void ng_dumpnodes(void); 9030400f03SJulian Elischer static void ng_dumphooks(void); 9130400f03SJulian Elischer 9230400f03SJulian Elischer #endif /* NETGRAPH_DEBUG */ 93954c4772SJulian Elischer /* 94954c4772SJulian Elischer * DEAD versions of the structures. 95954c4772SJulian Elischer * In order to avoid races, it is sometimes neccesary to point 96954c4772SJulian Elischer * at SOMETHING even though theoretically, the current entity is 97954c4772SJulian Elischer * INVALID. Use these to avoid these races. 98954c4772SJulian Elischer */ 99954c4772SJulian Elischer struct ng_type ng_deadtype = { 100954c4772SJulian Elischer NG_ABI_VERSION, 101954c4772SJulian Elischer "dead", 102954c4772SJulian Elischer NULL, /* modevent */ 103954c4772SJulian Elischer NULL, /* constructor */ 104954c4772SJulian Elischer NULL, /* rcvmsg */ 105954c4772SJulian Elischer NULL, /* shutdown */ 106954c4772SJulian Elischer NULL, /* newhook */ 107954c4772SJulian Elischer NULL, /* findhook */ 108954c4772SJulian Elischer NULL, /* connect */ 109954c4772SJulian Elischer NULL, /* rcvdata */ 110954c4772SJulian Elischer NULL, /* disconnect */ 111954c4772SJulian Elischer NULL, /* cmdlist */ 112954c4772SJulian Elischer }; 11330400f03SJulian Elischer 114954c4772SJulian Elischer struct ng_node ng_deadnode = { 115954c4772SJulian Elischer "dead", 116954c4772SJulian Elischer &ng_deadtype, 117be4252b3SJulian Elischer NGF_INVALID, 118954c4772SJulian Elischer 1, /* refs */ 119954c4772SJulian Elischer 0, /* numhooks */ 120954c4772SJulian Elischer NULL, /* private */ 121954c4772SJulian Elischer 0, /* ID */ 122954c4772SJulian Elischer LIST_HEAD_INITIALIZER(ng_deadnode.hooks), 123954c4772SJulian Elischer {}, /* all_nodes list entry */ 124954c4772SJulian Elischer {}, /* id hashtable list entry */ 125954c4772SJulian Elischer {}, /* workqueue entry */ 126954c4772SJulian Elischer { 0, 127954c4772SJulian Elischer {}, /* should never use! (should hang) */ 128954c4772SJulian Elischer NULL, 129954c4772SJulian Elischer &ng_deadnode.nd_input_queue.queue, 130954c4772SJulian Elischer &ng_deadnode 131954c4772SJulian Elischer }, 132954c4772SJulian Elischer #ifdef NETGRAPH_DEBUG 133954c4772SJulian Elischer ND_MAGIC, 134954c4772SJulian Elischer __FILE__, 135954c4772SJulian Elischer __LINE__, 136954c4772SJulian Elischer {NULL} 137954c4772SJulian Elischer #endif /* NETGRAPH_DEBUG */ 138954c4772SJulian Elischer }; 139954c4772SJulian Elischer 140954c4772SJulian Elischer struct ng_hook ng_deadhook = { 141954c4772SJulian Elischer "dead", 142954c4772SJulian Elischer NULL, /* private */ 143954c4772SJulian Elischer HK_INVALID | HK_DEAD, 144954c4772SJulian Elischer 1, /* refs always >= 1 */ 145e58d779dSGleb Smirnoff 0, /* undefined data link type */ 146954c4772SJulian Elischer &ng_deadhook, /* Peer is self */ 147954c4772SJulian Elischer &ng_deadnode, /* attached to deadnode */ 148954c4772SJulian Elischer {}, /* hooks list */ 149c4b5eea4SJulian Elischer NULL, /* override rcvmsg() */ 150c4b5eea4SJulian Elischer NULL, /* override rcvdata() */ 151954c4772SJulian Elischer #ifdef NETGRAPH_DEBUG 152954c4772SJulian Elischer HK_MAGIC, 153954c4772SJulian Elischer __FILE__, 154954c4772SJulian Elischer __LINE__, 155954c4772SJulian Elischer {NULL} 156954c4772SJulian Elischer #endif /* NETGRAPH_DEBUG */ 157954c4772SJulian Elischer }; 158954c4772SJulian Elischer 159954c4772SJulian Elischer /* 160954c4772SJulian Elischer * END DEAD STRUCTURES 161954c4772SJulian Elischer */ 162069154d5SJulian Elischer /* List nodes with unallocated work */ 163069154d5SJulian Elischer static TAILQ_HEAD(, ng_node) ng_worklist = TAILQ_HEAD_INITIALIZER(ng_worklist); 164b57a7965SJulian Elischer static struct mtx ng_worklist_mtx; /* MUST LOCK NODE FIRST */ 1654cf49a43SJulian Elischer 1664cf49a43SJulian Elischer /* List of installed types */ 167069154d5SJulian Elischer static LIST_HEAD(, ng_type) ng_typelist; 168069154d5SJulian Elischer static struct mtx ng_typelist_mtx; 1694cf49a43SJulian Elischer 170069154d5SJulian Elischer /* Hash related definitions */ 1710f150d04SJulian Elischer /* XXX Don't need to initialise them because it's a LIST */ 1720f150d04SJulian Elischer #define NG_ID_HASH_SIZE 32 /* most systems wont need even this many */ 1730f150d04SJulian Elischer static LIST_HEAD(, ng_node) ng_ID_hash[NG_ID_HASH_SIZE]; 174069154d5SJulian Elischer static struct mtx ng_idhash_mtx; 1750f150d04SJulian Elischer /* Method to find a node.. used twice so do it here */ 1760f150d04SJulian Elischer #define NG_IDHASH_FN(ID) ((ID) % (NG_ID_HASH_SIZE)) 1770f150d04SJulian Elischer #define NG_IDHASH_FIND(ID, node) \ 1780f150d04SJulian Elischer do { \ 17953f9c5e9SRobert Watson mtx_assert(&ng_idhash_mtx, MA_OWNED); \ 1800f150d04SJulian Elischer LIST_FOREACH(node, &ng_ID_hash[NG_IDHASH_FN(ID)], \ 1810f150d04SJulian Elischer nd_idnodes) { \ 1820f150d04SJulian Elischer if (NG_NODE_IS_VALID(node) \ 1830f150d04SJulian Elischer && (NG_NODE_ID(node) == ID)) { \ 1840f150d04SJulian Elischer break; \ 1850f150d04SJulian Elischer } \ 1860f150d04SJulian Elischer } \ 1870f150d04SJulian Elischer } while (0) 188069154d5SJulian Elischer 189dc90cad9SJulian Elischer 1904cf49a43SJulian Elischer /* Internal functions */ 1914cf49a43SJulian Elischer static int ng_add_hook(node_p node, const char *name, hook_p * hookp); 192069154d5SJulian Elischer static int ng_generic_msg(node_p here, item_p item, hook_p lasthook); 193dc90cad9SJulian Elischer static ng_ID_t ng_decodeidname(const char *name); 1944cf49a43SJulian Elischer static int ngb_mod_event(module_t mod, int event, void *data); 195069154d5SJulian Elischer static void ng_worklist_remove(node_p node); 1964cf49a43SJulian Elischer static void ngintr(void); 19727757487SGleb Smirnoff static int ng_apply_item(node_p node, item_p item, int rw); 198069154d5SJulian Elischer static void ng_flush_input_queue(struct ng_queue * ngq); 199069154d5SJulian Elischer static void ng_setisr(node_p node); 200069154d5SJulian Elischer static node_p ng_ID2noderef(ng_ID_t ID); 201e088dd4cSAlexander Motin static int ng_con_nodes(item_p item, node_p node, const char *name, 202e088dd4cSAlexander Motin node_p node2, const char *name2); 203e088dd4cSAlexander Motin static int ng_con_part2(node_p node, item_p item, hook_p hook); 204e088dd4cSAlexander Motin static int ng_con_part3(node_p node, item_p item, hook_p hook); 2056b795970SJulian Elischer static int ng_mkpeer(node_p node, const char *name, 2066b795970SJulian Elischer const char *name2, char *type); 207069154d5SJulian Elischer 2084c9b5910SGleb Smirnoff /* Imported, these used to be externally visible, some may go back. */ 209069154d5SJulian Elischer void ng_destroy_hook(hook_p hook); 210069154d5SJulian Elischer node_p ng_name2noderef(node_p node, const char *name); 211069154d5SJulian Elischer int ng_path2noderef(node_p here, const char *path, 212069154d5SJulian Elischer node_p *dest, hook_p *lasthook); 213069154d5SJulian Elischer int ng_make_node(const char *type, node_p *nodepp); 214069154d5SJulian Elischer int ng_path_parse(char *addr, char **node, char **path, char **hook); 2151acb27c6SJulian Elischer void ng_rmnode(node_p node, hook_p dummy1, void *dummy2, int dummy3); 21630400f03SJulian Elischer void ng_unname(node_p node); 217069154d5SJulian Elischer 2184cf49a43SJulian Elischer 2194cf49a43SJulian Elischer /* Our own netgraph malloc type */ 2204cf49a43SJulian Elischer MALLOC_DEFINE(M_NETGRAPH, "netgraph", "netgraph structures and ctrl messages"); 221069154d5SJulian Elischer MALLOC_DEFINE(M_NETGRAPH_HOOK, "netgraph_hook", "netgraph hook structures"); 222069154d5SJulian Elischer MALLOC_DEFINE(M_NETGRAPH_NODE, "netgraph_node", "netgraph node structures"); 223069154d5SJulian Elischer MALLOC_DEFINE(M_NETGRAPH_ITEM, "netgraph_item", "netgraph item structures"); 224069154d5SJulian Elischer MALLOC_DEFINE(M_NETGRAPH_MSG, "netgraph_msg", "netgraph name storage"); 225069154d5SJulian Elischer 226069154d5SJulian Elischer /* Should not be visible outside this file */ 22730400f03SJulian Elischer 22830400f03SJulian Elischer #define _NG_ALLOC_HOOK(hook) \ 22930400f03SJulian Elischer MALLOC(hook, hook_p, sizeof(*hook), M_NETGRAPH_HOOK, M_NOWAIT | M_ZERO) 23030400f03SJulian Elischer #define _NG_ALLOC_NODE(node) \ 23130400f03SJulian Elischer MALLOC(node, node_p, sizeof(*node), M_NETGRAPH_NODE, M_NOWAIT | M_ZERO) 23230400f03SJulian Elischer 2332c8dda8dSWojciech A. Koszek #define NG_QUEUE_LOCK_INIT(n) \ 2344abab3d5SWojciech A. Koszek mtx_init(&(n)->q_mtx, "ng_node", NULL, MTX_DEF) 2352c8dda8dSWojciech A. Koszek #define NG_QUEUE_LOCK(n) \ 2364abab3d5SWojciech A. Koszek mtx_lock(&(n)->q_mtx) 2372c8dda8dSWojciech A. Koszek #define NG_QUEUE_UNLOCK(n) \ 2384abab3d5SWojciech A. Koszek mtx_unlock(&(n)->q_mtx) 2392c8dda8dSWojciech A. Koszek #define NG_WORKLIST_LOCK_INIT() \ 2404abab3d5SWojciech A. Koszek mtx_init(&ng_worklist_mtx, "ng_worklist", NULL, MTX_DEF) 2412c8dda8dSWojciech A. Koszek #define NG_WORKLIST_LOCK() \ 2424abab3d5SWojciech A. Koszek mtx_lock(&ng_worklist_mtx) 2432c8dda8dSWojciech A. Koszek #define NG_WORKLIST_UNLOCK() \ 2444abab3d5SWojciech A. Koszek mtx_unlock(&ng_worklist_mtx) 2452c8dda8dSWojciech A. Koszek 24630400f03SJulian Elischer #ifdef NETGRAPH_DEBUG /*----------------------------------------------*/ 24730400f03SJulian Elischer /* 24830400f03SJulian Elischer * In debug mode: 24930400f03SJulian Elischer * In an attempt to help track reference count screwups 25030400f03SJulian Elischer * we do not free objects back to the malloc system, but keep them 25130400f03SJulian Elischer * in a local cache where we can examine them and keep information safely 25230400f03SJulian Elischer * after they have been freed. 25330400f03SJulian Elischer * We use this scheme for nodes and hooks, and to some extent for items. 25430400f03SJulian Elischer */ 25530400f03SJulian Elischer static __inline hook_p 25630400f03SJulian Elischer ng_alloc_hook(void) 25730400f03SJulian Elischer { 25830400f03SJulian Elischer hook_p hook; 25930400f03SJulian Elischer SLIST_ENTRY(ng_hook) temp; 2609ed346baSBosko Milekic mtx_lock(&ng_nodelist_mtx); 26130400f03SJulian Elischer hook = LIST_FIRST(&ng_freehooks); 26230400f03SJulian Elischer if (hook) { 26330400f03SJulian Elischer LIST_REMOVE(hook, hk_hooks); 26430400f03SJulian Elischer bcopy(&hook->hk_all, &temp, sizeof(temp)); 26530400f03SJulian Elischer bzero(hook, sizeof(struct ng_hook)); 26630400f03SJulian Elischer bcopy(&temp, &hook->hk_all, sizeof(temp)); 2679ed346baSBosko Milekic mtx_unlock(&ng_nodelist_mtx); 26830400f03SJulian Elischer hook->hk_magic = HK_MAGIC; 26930400f03SJulian Elischer } else { 2709ed346baSBosko Milekic mtx_unlock(&ng_nodelist_mtx); 27130400f03SJulian Elischer _NG_ALLOC_HOOK(hook); 27230400f03SJulian Elischer if (hook) { 27330400f03SJulian Elischer hook->hk_magic = HK_MAGIC; 2749ed346baSBosko Milekic mtx_lock(&ng_nodelist_mtx); 27530400f03SJulian Elischer SLIST_INSERT_HEAD(&ng_allhooks, hook, hk_all); 2769ed346baSBosko Milekic mtx_unlock(&ng_nodelist_mtx); 27730400f03SJulian Elischer } 27830400f03SJulian Elischer } 27930400f03SJulian Elischer return (hook); 28030400f03SJulian Elischer } 28130400f03SJulian Elischer 28230400f03SJulian Elischer static __inline node_p 28330400f03SJulian Elischer ng_alloc_node(void) 28430400f03SJulian Elischer { 28530400f03SJulian Elischer node_p node; 28630400f03SJulian Elischer SLIST_ENTRY(ng_node) temp; 2879ed346baSBosko Milekic mtx_lock(&ng_nodelist_mtx); 28830400f03SJulian Elischer node = LIST_FIRST(&ng_freenodes); 28930400f03SJulian Elischer if (node) { 29030400f03SJulian Elischer LIST_REMOVE(node, nd_nodes); 29130400f03SJulian Elischer bcopy(&node->nd_all, &temp, sizeof(temp)); 29230400f03SJulian Elischer bzero(node, sizeof(struct ng_node)); 29330400f03SJulian Elischer bcopy(&temp, &node->nd_all, sizeof(temp)); 2949ed346baSBosko Milekic mtx_unlock(&ng_nodelist_mtx); 29530400f03SJulian Elischer node->nd_magic = ND_MAGIC; 29630400f03SJulian Elischer } else { 2979ed346baSBosko Milekic mtx_unlock(&ng_nodelist_mtx); 29830400f03SJulian Elischer _NG_ALLOC_NODE(node); 29930400f03SJulian Elischer if (node) { 30030400f03SJulian Elischer node->nd_magic = ND_MAGIC; 3019ed346baSBosko Milekic mtx_lock(&ng_nodelist_mtx); 30230400f03SJulian Elischer SLIST_INSERT_HEAD(&ng_allnodes, node, nd_all); 3039ed346baSBosko Milekic mtx_unlock(&ng_nodelist_mtx); 30430400f03SJulian Elischer } 30530400f03SJulian Elischer } 30630400f03SJulian Elischer return (node); 30730400f03SJulian Elischer } 30830400f03SJulian Elischer 30930400f03SJulian Elischer #define NG_ALLOC_HOOK(hook) do { (hook) = ng_alloc_hook(); } while (0) 31030400f03SJulian Elischer #define NG_ALLOC_NODE(node) do { (node) = ng_alloc_node(); } while (0) 31130400f03SJulian Elischer 31230400f03SJulian Elischer 31330400f03SJulian Elischer #define NG_FREE_HOOK(hook) \ 31430400f03SJulian Elischer do { \ 3159ed346baSBosko Milekic mtx_lock(&ng_nodelist_mtx); \ 31630400f03SJulian Elischer LIST_INSERT_HEAD(&ng_freehooks, hook, hk_hooks); \ 31730400f03SJulian Elischer hook->hk_magic = 0; \ 3189ed346baSBosko Milekic mtx_unlock(&ng_nodelist_mtx); \ 31930400f03SJulian Elischer } while (0) 32030400f03SJulian Elischer 32130400f03SJulian Elischer #define NG_FREE_NODE(node) \ 32230400f03SJulian Elischer do { \ 3239ed346baSBosko Milekic mtx_lock(&ng_nodelist_mtx); \ 32430400f03SJulian Elischer LIST_INSERT_HEAD(&ng_freenodes, node, nd_nodes); \ 32530400f03SJulian Elischer node->nd_magic = 0; \ 3269ed346baSBosko Milekic mtx_unlock(&ng_nodelist_mtx); \ 32730400f03SJulian Elischer } while (0) 32830400f03SJulian Elischer 32930400f03SJulian Elischer #else /* NETGRAPH_DEBUG */ /*----------------------------------------------*/ 33030400f03SJulian Elischer 33130400f03SJulian Elischer #define NG_ALLOC_HOOK(hook) _NG_ALLOC_HOOK(hook) 33230400f03SJulian Elischer #define NG_ALLOC_NODE(node) _NG_ALLOC_NODE(node) 33330400f03SJulian Elischer 334069154d5SJulian Elischer #define NG_FREE_HOOK(hook) do { FREE((hook), M_NETGRAPH_HOOK); } while (0) 335069154d5SJulian Elischer #define NG_FREE_NODE(node) do { FREE((node), M_NETGRAPH_NODE); } while (0) 33630400f03SJulian Elischer 33730400f03SJulian Elischer #endif /* NETGRAPH_DEBUG */ /*----------------------------------------------*/ 33830400f03SJulian Elischer 339f33ca0c9SMarcel Moolenaar /* Set this to kdb_enter("X") to catch all errors as they occur */ 3404cf49a43SJulian Elischer #ifndef TRAP_ERROR 3416b795970SJulian Elischer #define TRAP_ERROR() 3424cf49a43SJulian Elischer #endif 3434cf49a43SJulian Elischer 344dc90cad9SJulian Elischer static ng_ID_t nextID = 1; 345dc90cad9SJulian Elischer 346b2da83c2SArchie Cobbs #ifdef INVARIANTS 347b2da83c2SArchie Cobbs #define CHECK_DATA_MBUF(m) do { \ 348b2da83c2SArchie Cobbs struct mbuf *n; \ 349b2da83c2SArchie Cobbs int total; \ 350b2da83c2SArchie Cobbs \ 351fe584538SDag-Erling Smørgrav M_ASSERTPKTHDR(m); \ 352b32cfb32SGleb Smirnoff for (total = 0, n = (m); n != NULL; n = n->m_next) { \ 353b2da83c2SArchie Cobbs total += n->m_len; \ 354b32cfb32SGleb Smirnoff if (n->m_nextpkt != NULL) \ 355b32cfb32SGleb Smirnoff panic("%s: m_nextpkt", __func__); \ 356b32cfb32SGleb Smirnoff } \ 357ba5b359aSGleb Smirnoff \ 358b2da83c2SArchie Cobbs if ((m)->m_pkthdr.len != total) { \ 359b2da83c2SArchie Cobbs panic("%s: %d != %d", \ 3606e551fb6SDavid E. O'Brien __func__, (m)->m_pkthdr.len, total); \ 361b2da83c2SArchie Cobbs } \ 362b2da83c2SArchie Cobbs } while (0) 363b2da83c2SArchie Cobbs #else 364b2da83c2SArchie Cobbs #define CHECK_DATA_MBUF(m) 365b2da83c2SArchie Cobbs #endif 366b2da83c2SArchie Cobbs 367e088dd4cSAlexander Motin #define ERROUT(x) do { error = (x); goto done; } while (0) 368dc90cad9SJulian Elischer 3694cf49a43SJulian Elischer /************************************************************************ 370f8307e12SArchie Cobbs Parse type definitions for generic messages 371f8307e12SArchie Cobbs ************************************************************************/ 372f8307e12SArchie Cobbs 373f8307e12SArchie Cobbs /* Handy structure parse type defining macro */ 374f8307e12SArchie Cobbs #define DEFINE_PARSE_STRUCT_TYPE(lo, up, args) \ 375f0184ff8SArchie Cobbs static const struct ng_parse_struct_field \ 376f0184ff8SArchie Cobbs ng_ ## lo ## _type_fields[] = NG_GENERIC_ ## up ## _INFO args; \ 377f8307e12SArchie Cobbs static const struct ng_parse_type ng_generic_ ## lo ## _type = { \ 378f8307e12SArchie Cobbs &ng_parse_struct_type, \ 379f0184ff8SArchie Cobbs &ng_ ## lo ## _type_fields \ 380f8307e12SArchie Cobbs } 381f8307e12SArchie Cobbs 382f8307e12SArchie Cobbs DEFINE_PARSE_STRUCT_TYPE(mkpeer, MKPEER, ()); 383f8307e12SArchie Cobbs DEFINE_PARSE_STRUCT_TYPE(connect, CONNECT, ()); 384f8307e12SArchie Cobbs DEFINE_PARSE_STRUCT_TYPE(name, NAME, ()); 385f8307e12SArchie Cobbs DEFINE_PARSE_STRUCT_TYPE(rmhook, RMHOOK, ()); 386f8307e12SArchie Cobbs DEFINE_PARSE_STRUCT_TYPE(nodeinfo, NODEINFO, ()); 387f8307e12SArchie Cobbs DEFINE_PARSE_STRUCT_TYPE(typeinfo, TYPEINFO, ()); 388f8307e12SArchie Cobbs DEFINE_PARSE_STRUCT_TYPE(linkinfo, LINKINFO, (&ng_generic_nodeinfo_type)); 389f8307e12SArchie Cobbs 390f8307e12SArchie Cobbs /* Get length of an array when the length is stored as a 32 bit 391d7d97eb0SJeroen Ruigrok van der Werven value immediately preceding the array -- as with struct namelist 392f8307e12SArchie Cobbs and struct typelist. */ 393f8307e12SArchie Cobbs static int 394f8307e12SArchie Cobbs ng_generic_list_getLength(const struct ng_parse_type *type, 395f8307e12SArchie Cobbs const u_char *start, const u_char *buf) 396f8307e12SArchie Cobbs { 397f8307e12SArchie Cobbs return *((const u_int32_t *)(buf - 4)); 398f8307e12SArchie Cobbs } 399f8307e12SArchie Cobbs 400f8307e12SArchie Cobbs /* Get length of the array of struct linkinfo inside a struct hooklist */ 401f8307e12SArchie Cobbs static int 402f8307e12SArchie Cobbs ng_generic_linkinfo_getLength(const struct ng_parse_type *type, 403f8307e12SArchie Cobbs const u_char *start, const u_char *buf) 404f8307e12SArchie Cobbs { 405f8307e12SArchie Cobbs const struct hooklist *hl = (const struct hooklist *)start; 406f8307e12SArchie Cobbs 407f8307e12SArchie Cobbs return hl->nodeinfo.hooks; 408f8307e12SArchie Cobbs } 409f8307e12SArchie Cobbs 410f8307e12SArchie Cobbs /* Array type for a variable length array of struct namelist */ 411f8307e12SArchie Cobbs static const struct ng_parse_array_info ng_nodeinfoarray_type_info = { 412f8307e12SArchie Cobbs &ng_generic_nodeinfo_type, 413f8307e12SArchie Cobbs &ng_generic_list_getLength 414f8307e12SArchie Cobbs }; 415f8307e12SArchie Cobbs static const struct ng_parse_type ng_generic_nodeinfoarray_type = { 416f8307e12SArchie Cobbs &ng_parse_array_type, 417f8307e12SArchie Cobbs &ng_nodeinfoarray_type_info 418f8307e12SArchie Cobbs }; 419f8307e12SArchie Cobbs 420f8307e12SArchie Cobbs /* Array type for a variable length array of struct typelist */ 421f8307e12SArchie Cobbs static const struct ng_parse_array_info ng_typeinfoarray_type_info = { 422f8307e12SArchie Cobbs &ng_generic_typeinfo_type, 423f8307e12SArchie Cobbs &ng_generic_list_getLength 424f8307e12SArchie Cobbs }; 425f8307e12SArchie Cobbs static const struct ng_parse_type ng_generic_typeinfoarray_type = { 426f8307e12SArchie Cobbs &ng_parse_array_type, 427f8307e12SArchie Cobbs &ng_typeinfoarray_type_info 428f8307e12SArchie Cobbs }; 429f8307e12SArchie Cobbs 430f8307e12SArchie Cobbs /* Array type for array of struct linkinfo in struct hooklist */ 431f8307e12SArchie Cobbs static const struct ng_parse_array_info ng_generic_linkinfo_array_type_info = { 432f8307e12SArchie Cobbs &ng_generic_linkinfo_type, 433f8307e12SArchie Cobbs &ng_generic_linkinfo_getLength 434f8307e12SArchie Cobbs }; 435f8307e12SArchie Cobbs static const struct ng_parse_type ng_generic_linkinfo_array_type = { 436f8307e12SArchie Cobbs &ng_parse_array_type, 437f8307e12SArchie Cobbs &ng_generic_linkinfo_array_type_info 438f8307e12SArchie Cobbs }; 439f8307e12SArchie Cobbs 440f8307e12SArchie Cobbs DEFINE_PARSE_STRUCT_TYPE(typelist, TYPELIST, (&ng_generic_nodeinfoarray_type)); 441f8307e12SArchie Cobbs DEFINE_PARSE_STRUCT_TYPE(hooklist, HOOKLIST, 442f8307e12SArchie Cobbs (&ng_generic_nodeinfo_type, &ng_generic_linkinfo_array_type)); 443f8307e12SArchie Cobbs DEFINE_PARSE_STRUCT_TYPE(listnodes, LISTNODES, 444f8307e12SArchie Cobbs (&ng_generic_nodeinfoarray_type)); 445f8307e12SArchie Cobbs 446f8307e12SArchie Cobbs /* List of commands and how to convert arguments to/from ASCII */ 447f8307e12SArchie Cobbs static const struct ng_cmdlist ng_generic_cmds[] = { 448f8307e12SArchie Cobbs { 449f8307e12SArchie Cobbs NGM_GENERIC_COOKIE, 450f8307e12SArchie Cobbs NGM_SHUTDOWN, 451f8307e12SArchie Cobbs "shutdown", 452f8307e12SArchie Cobbs NULL, 453f8307e12SArchie Cobbs NULL 454f8307e12SArchie Cobbs }, 455f8307e12SArchie Cobbs { 456f8307e12SArchie Cobbs NGM_GENERIC_COOKIE, 457f8307e12SArchie Cobbs NGM_MKPEER, 458f8307e12SArchie Cobbs "mkpeer", 459f8307e12SArchie Cobbs &ng_generic_mkpeer_type, 460f8307e12SArchie Cobbs NULL 461f8307e12SArchie Cobbs }, 462f8307e12SArchie Cobbs { 463f8307e12SArchie Cobbs NGM_GENERIC_COOKIE, 464f8307e12SArchie Cobbs NGM_CONNECT, 465f8307e12SArchie Cobbs "connect", 466f8307e12SArchie Cobbs &ng_generic_connect_type, 467f8307e12SArchie Cobbs NULL 468f8307e12SArchie Cobbs }, 469f8307e12SArchie Cobbs { 470f8307e12SArchie Cobbs NGM_GENERIC_COOKIE, 471f8307e12SArchie Cobbs NGM_NAME, 472f8307e12SArchie Cobbs "name", 473f8307e12SArchie Cobbs &ng_generic_name_type, 474f8307e12SArchie Cobbs NULL 475f8307e12SArchie Cobbs }, 476f8307e12SArchie Cobbs { 477f8307e12SArchie Cobbs NGM_GENERIC_COOKIE, 478f8307e12SArchie Cobbs NGM_RMHOOK, 479f8307e12SArchie Cobbs "rmhook", 480f8307e12SArchie Cobbs &ng_generic_rmhook_type, 481f8307e12SArchie Cobbs NULL 482f8307e12SArchie Cobbs }, 483f8307e12SArchie Cobbs { 484f8307e12SArchie Cobbs NGM_GENERIC_COOKIE, 485f8307e12SArchie Cobbs NGM_NODEINFO, 486f8307e12SArchie Cobbs "nodeinfo", 487f8307e12SArchie Cobbs NULL, 488f8307e12SArchie Cobbs &ng_generic_nodeinfo_type 489f8307e12SArchie Cobbs }, 490f8307e12SArchie Cobbs { 491f8307e12SArchie Cobbs NGM_GENERIC_COOKIE, 492f8307e12SArchie Cobbs NGM_LISTHOOKS, 493f8307e12SArchie Cobbs "listhooks", 494f8307e12SArchie Cobbs NULL, 495f8307e12SArchie Cobbs &ng_generic_hooklist_type 496f8307e12SArchie Cobbs }, 497f8307e12SArchie Cobbs { 498f8307e12SArchie Cobbs NGM_GENERIC_COOKIE, 499f8307e12SArchie Cobbs NGM_LISTNAMES, 500f8307e12SArchie Cobbs "listnames", 501f8307e12SArchie Cobbs NULL, 502f8307e12SArchie Cobbs &ng_generic_listnodes_type /* same as NGM_LISTNODES */ 503f8307e12SArchie Cobbs }, 504f8307e12SArchie Cobbs { 505f8307e12SArchie Cobbs NGM_GENERIC_COOKIE, 506f8307e12SArchie Cobbs NGM_LISTNODES, 507f8307e12SArchie Cobbs "listnodes", 508f8307e12SArchie Cobbs NULL, 509f8307e12SArchie Cobbs &ng_generic_listnodes_type 510f8307e12SArchie Cobbs }, 511f8307e12SArchie Cobbs { 512f8307e12SArchie Cobbs NGM_GENERIC_COOKIE, 513f8307e12SArchie Cobbs NGM_LISTTYPES, 514f8307e12SArchie Cobbs "listtypes", 515f8307e12SArchie Cobbs NULL, 516f8307e12SArchie Cobbs &ng_generic_typeinfo_type 517f8307e12SArchie Cobbs }, 518f8307e12SArchie Cobbs { 519f8307e12SArchie Cobbs NGM_GENERIC_COOKIE, 5207095e097SPoul-Henning Kamp NGM_TEXT_CONFIG, 5217095e097SPoul-Henning Kamp "textconfig", 5227095e097SPoul-Henning Kamp NULL, 5237095e097SPoul-Henning Kamp &ng_parse_string_type 5247095e097SPoul-Henning Kamp }, 5257095e097SPoul-Henning Kamp { 5267095e097SPoul-Henning Kamp NGM_GENERIC_COOKIE, 527f8307e12SArchie Cobbs NGM_TEXT_STATUS, 528f8307e12SArchie Cobbs "textstatus", 529f8307e12SArchie Cobbs NULL, 530f8307e12SArchie Cobbs &ng_parse_string_type 531f8307e12SArchie Cobbs }, 532f8307e12SArchie Cobbs { 533f8307e12SArchie Cobbs NGM_GENERIC_COOKIE, 534f8307e12SArchie Cobbs NGM_ASCII2BINARY, 535f8307e12SArchie Cobbs "ascii2binary", 536f8307e12SArchie Cobbs &ng_parse_ng_mesg_type, 537f8307e12SArchie Cobbs &ng_parse_ng_mesg_type 538f8307e12SArchie Cobbs }, 539f8307e12SArchie Cobbs { 540f8307e12SArchie Cobbs NGM_GENERIC_COOKIE, 541f8307e12SArchie Cobbs NGM_BINARY2ASCII, 542f8307e12SArchie Cobbs "binary2ascii", 543f8307e12SArchie Cobbs &ng_parse_ng_mesg_type, 544f8307e12SArchie Cobbs &ng_parse_ng_mesg_type 545f8307e12SArchie Cobbs }, 546f8307e12SArchie Cobbs { 0 } 547f8307e12SArchie Cobbs }; 548f8307e12SArchie Cobbs 549f8307e12SArchie Cobbs /************************************************************************ 5504cf49a43SJulian Elischer Node routines 5514cf49a43SJulian Elischer ************************************************************************/ 5524cf49a43SJulian Elischer 5534cf49a43SJulian Elischer /* 5544cf49a43SJulian Elischer * Instantiate a node of the requested type 5554cf49a43SJulian Elischer */ 5564cf49a43SJulian Elischer int 5574cf49a43SJulian Elischer ng_make_node(const char *typename, node_p *nodepp) 5584cf49a43SJulian Elischer { 5594cf49a43SJulian Elischer struct ng_type *type; 560069154d5SJulian Elischer int error; 5614cf49a43SJulian Elischer 5624cf49a43SJulian Elischer /* Check that the type makes sense */ 5634cf49a43SJulian Elischer if (typename == NULL) { 5646b795970SJulian Elischer TRAP_ERROR(); 5654cf49a43SJulian Elischer return (EINVAL); 5664cf49a43SJulian Elischer } 5674cf49a43SJulian Elischer 5687610f574SGleb Smirnoff /* Locate the node type. If we fail we return. Do not try to load 5697610f574SGleb Smirnoff * module. 5707610f574SGleb Smirnoff */ 5714cf49a43SJulian Elischer if ((type = ng_findtype(typename)) == NULL) 5724cf49a43SJulian Elischer return (ENXIO); 5734cf49a43SJulian Elischer 574069154d5SJulian Elischer /* 575069154d5SJulian Elischer * If we have a constructor, then make the node and 576069154d5SJulian Elischer * call the constructor to do type specific initialisation. 577069154d5SJulian Elischer */ 578069154d5SJulian Elischer if (type->constructor != NULL) { 579069154d5SJulian Elischer if ((error = ng_make_node_common(type, nodepp)) == 0) { 580069154d5SJulian Elischer if ((error = ((*type->constructor)(*nodepp)) != 0)) { 58130400f03SJulian Elischer NG_NODE_UNREF(*nodepp); 582069154d5SJulian Elischer } 583069154d5SJulian Elischer } 584069154d5SJulian Elischer } else { 585069154d5SJulian Elischer /* 586069154d5SJulian Elischer * Node has no constructor. We cannot ask for one 58764efc707SRobert Watson * to be made. It must be brought into existence by 588954c4772SJulian Elischer * some external agency. The external agency should 589069154d5SJulian Elischer * call ng_make_node_common() directly to get the 590069154d5SJulian Elischer * netgraph part initialised. 591069154d5SJulian Elischer */ 5926b795970SJulian Elischer TRAP_ERROR(); 593069154d5SJulian Elischer error = EINVAL; 594069154d5SJulian Elischer } 595069154d5SJulian Elischer return (error); 5964cf49a43SJulian Elischer } 5974cf49a43SJulian Elischer 5984cf49a43SJulian Elischer /* 599069154d5SJulian Elischer * Generic node creation. Called by node initialisation for externally 600069154d5SJulian Elischer * instantiated nodes (e.g. hardware, sockets, etc ). 6014cf49a43SJulian Elischer * The returned node has a reference count of 1. 6024cf49a43SJulian Elischer */ 6034cf49a43SJulian Elischer int 6044cf49a43SJulian Elischer ng_make_node_common(struct ng_type *type, node_p *nodepp) 6054cf49a43SJulian Elischer { 6064cf49a43SJulian Elischer node_p node; 6074cf49a43SJulian Elischer 6084cf49a43SJulian Elischer /* Require the node type to have been already installed */ 6094cf49a43SJulian Elischer if (ng_findtype(type->name) == NULL) { 6106b795970SJulian Elischer TRAP_ERROR(); 6114cf49a43SJulian Elischer return (EINVAL); 6124cf49a43SJulian Elischer } 6134cf49a43SJulian Elischer 6144cf49a43SJulian Elischer /* Make a node and try attach it to the type */ 61530400f03SJulian Elischer NG_ALLOC_NODE(node); 6164cf49a43SJulian Elischer if (node == NULL) { 6176b795970SJulian Elischer TRAP_ERROR(); 6184cf49a43SJulian Elischer return (ENOMEM); 6194cf49a43SJulian Elischer } 62030400f03SJulian Elischer node->nd_type = type; 62130400f03SJulian Elischer NG_NODE_REF(node); /* note reference */ 6224cf49a43SJulian Elischer type->refs++; 6234cf49a43SJulian Elischer 6242c8dda8dSWojciech A. Koszek NG_QUEUE_LOCK_INIT(&node->nd_input_queue); 62530400f03SJulian Elischer node->nd_input_queue.queue = NULL; 62630400f03SJulian Elischer node->nd_input_queue.last = &node->nd_input_queue.queue; 62730400f03SJulian Elischer node->nd_input_queue.q_flags = 0; 62830400f03SJulian Elischer node->nd_input_queue.q_node = node; 6294cf49a43SJulian Elischer 6304cf49a43SJulian Elischer /* Initialize hook list for new node */ 63130400f03SJulian Elischer LIST_INIT(&node->nd_hooks); 6324cf49a43SJulian Elischer 633069154d5SJulian Elischer /* Link us into the node linked list */ 6349ed346baSBosko Milekic mtx_lock(&ng_nodelist_mtx); 63530400f03SJulian Elischer LIST_INSERT_HEAD(&ng_nodelist, node, nd_nodes); 6369ed346baSBosko Milekic mtx_unlock(&ng_nodelist_mtx); 637069154d5SJulian Elischer 638069154d5SJulian Elischer 639dc90cad9SJulian Elischer /* get an ID and put us in the hash chain */ 6409ed346baSBosko Milekic mtx_lock(&ng_idhash_mtx); 64130400f03SJulian Elischer for (;;) { /* wrap protection, even if silly */ 642069154d5SJulian Elischer node_p node2 = NULL; 64330400f03SJulian Elischer node->nd_ID = nextID++; /* 137/second for 1 year before wrap */ 6440f150d04SJulian Elischer 64530400f03SJulian Elischer /* Is there a problem with the new number? */ 6460f150d04SJulian Elischer NG_IDHASH_FIND(node->nd_ID, node2); /* already taken? */ 6470f150d04SJulian Elischer if ((node->nd_ID != 0) && (node2 == NULL)) { 64830400f03SJulian Elischer break; 649069154d5SJulian Elischer } 65030400f03SJulian Elischer } 6510f150d04SJulian Elischer LIST_INSERT_HEAD(&ng_ID_hash[NG_IDHASH_FN(node->nd_ID)], 65230400f03SJulian Elischer node, nd_idnodes); 6539ed346baSBosko Milekic mtx_unlock(&ng_idhash_mtx); 654dc90cad9SJulian Elischer 6554cf49a43SJulian Elischer /* Done */ 6564cf49a43SJulian Elischer *nodepp = node; 6574cf49a43SJulian Elischer return (0); 6584cf49a43SJulian Elischer } 6594cf49a43SJulian Elischer 6604cf49a43SJulian Elischer /* 6614cf49a43SJulian Elischer * Forceably start the shutdown process on a node. Either call 66264efc707SRobert Watson * its shutdown method, or do the default shutdown if there is 6634cf49a43SJulian Elischer * no type-specific method. 6644cf49a43SJulian Elischer * 66564efc707SRobert Watson * We can only be called from a shutdown message, so we know we have 6663e4084c8SJulian Elischer * a writer lock, and therefore exclusive access. It also means 6673e4084c8SJulian Elischer * that we should not be on the work queue, but we check anyhow. 668069154d5SJulian Elischer * 669069154d5SJulian Elischer * Persistent node types must have a type-specific method which 67064efc707SRobert Watson * allocates a new node in which case, this one is irretrievably going away, 6713e4084c8SJulian Elischer * or cleans up anything it needs, and just makes the node valid again, 6723e4084c8SJulian Elischer * in which case we allow the node to survive. 6733e4084c8SJulian Elischer * 67464efc707SRobert Watson * XXX We need to think of how to tell a persistent node that we 6753e4084c8SJulian Elischer * REALLY need to go away because the hardware has gone or we 6763e4084c8SJulian Elischer * are rebooting.... etc. 6774cf49a43SJulian Elischer */ 6784cf49a43SJulian Elischer void 6791acb27c6SJulian Elischer ng_rmnode(node_p node, hook_p dummy1, void *dummy2, int dummy3) 6804cf49a43SJulian Elischer { 6813e4084c8SJulian Elischer hook_p hook; 6823e4084c8SJulian Elischer 6834cf49a43SJulian Elischer /* Check if it's already shutting down */ 684be4252b3SJulian Elischer if ((node->nd_flags & NGF_CLOSING) != 0) 6854cf49a43SJulian Elischer return; 6864cf49a43SJulian Elischer 6871acb27c6SJulian Elischer if (node == &ng_deadnode) { 6881acb27c6SJulian Elischer printf ("shutdown called on deadnode\n"); 6891acb27c6SJulian Elischer return; 6901acb27c6SJulian Elischer } 6911acb27c6SJulian Elischer 6924cf49a43SJulian Elischer /* Add an extra reference so it doesn't go away during this */ 69330400f03SJulian Elischer NG_NODE_REF(node); 6944cf49a43SJulian Elischer 69530400f03SJulian Elischer /* 69630400f03SJulian Elischer * Mark it invalid so any newcomers know not to try use it 69730400f03SJulian Elischer * Also add our own mark so we can't recurse 698be4252b3SJulian Elischer * note that NGF_INVALID does not do this as it's also set during 69930400f03SJulian Elischer * creation 70030400f03SJulian Elischer */ 701be4252b3SJulian Elischer node->nd_flags |= NGF_INVALID|NGF_CLOSING; 7024cf49a43SJulian Elischer 703991fc65aSJulian Elischer /* If node has its pre-shutdown method, then call it first*/ 704991fc65aSJulian Elischer if (node->nd_type && node->nd_type->close) 705991fc65aSJulian Elischer (*node->nd_type->close)(node); 706991fc65aSJulian Elischer 7073e4084c8SJulian Elischer /* Notify all remaining connected nodes to disconnect */ 7083e4084c8SJulian Elischer while ((hook = LIST_FIRST(&node->nd_hooks)) != NULL) 7093e4084c8SJulian Elischer ng_destroy_hook(hook); 71030400f03SJulian Elischer 711069154d5SJulian Elischer /* 712069154d5SJulian Elischer * Drain the input queue forceably. 71330400f03SJulian Elischer * it has no hooks so what's it going to do, bleed on someone? 71430400f03SJulian Elischer * Theoretically we came here from a queue entry that was added 71530400f03SJulian Elischer * Just before the queue was closed, so it should be empty anyway. 716b57a7965SJulian Elischer * Also removes us from worklist if needed. 717069154d5SJulian Elischer */ 71830400f03SJulian Elischer ng_flush_input_queue(&node->nd_input_queue); 719069154d5SJulian Elischer 720069154d5SJulian Elischer /* Ask the type if it has anything to do in this case */ 72130400f03SJulian Elischer if (node->nd_type && node->nd_type->shutdown) { 72230400f03SJulian Elischer (*node->nd_type->shutdown)(node); 72330400f03SJulian Elischer if (NG_NODE_IS_VALID(node)) { 72430400f03SJulian Elischer /* 72530400f03SJulian Elischer * Well, blow me down if the node code hasn't declared 72630400f03SJulian Elischer * that it doesn't want to die. 72730400f03SJulian Elischer * Presumably it is a persistant node. 7281acb27c6SJulian Elischer * If we REALLY want it to go away, 7291acb27c6SJulian Elischer * e.g. hardware going away, 730be4252b3SJulian Elischer * Our caller should set NGF_REALLY_DIE in nd_flags. 73130400f03SJulian Elischer */ 732be4252b3SJulian Elischer node->nd_flags &= ~(NGF_INVALID|NGF_CLOSING); 7331acb27c6SJulian Elischer NG_NODE_UNREF(node); /* Assume they still have theirs */ 73430400f03SJulian Elischer return; 7354cf49a43SJulian Elischer } 7361acb27c6SJulian Elischer } else { /* do the default thing */ 7371acb27c6SJulian Elischer NG_NODE_UNREF(node); 7381acb27c6SJulian Elischer } 7394cf49a43SJulian Elischer 74030400f03SJulian Elischer ng_unname(node); /* basically a NOP these days */ 74130400f03SJulian Elischer 74230400f03SJulian Elischer /* 74330400f03SJulian Elischer * Remove extra reference, possibly the last 74430400f03SJulian Elischer * Possible other holders of references may include 74530400f03SJulian Elischer * timeout callouts, but theoretically the node's supposed to 74630400f03SJulian Elischer * have cancelled them. Possibly hardware dependencies may 74730400f03SJulian Elischer * force a driver to 'linger' with a reference. 74830400f03SJulian Elischer */ 74930400f03SJulian Elischer NG_NODE_UNREF(node); 7504cf49a43SJulian Elischer } 7514cf49a43SJulian Elischer 7525951069aSJulian Elischer /* 7535951069aSJulian Elischer * Remove a reference to the node, possibly the last. 7545951069aSJulian Elischer * deadnode always acts as it it were the last. 7555951069aSJulian Elischer */ 7565951069aSJulian Elischer int 75730400f03SJulian Elischer ng_unref_node(node_p node) 7584cf49a43SJulian Elischer { 75930400f03SJulian Elischer int v; 7606b795970SJulian Elischer 7616b795970SJulian Elischer if (node == &ng_deadnode) { 7625951069aSJulian Elischer return (0); 7636b795970SJulian Elischer } 7646b795970SJulian Elischer 76530400f03SJulian Elischer do { 7665951069aSJulian Elischer v = node->nd_refs - 1; 7675951069aSJulian Elischer } while (! atomic_cmpset_int(&node->nd_refs, v + 1, v)); 768e8a49db2SJulian Elischer 7695951069aSJulian Elischer if (v == 0) { /* we were the last */ 770069154d5SJulian Elischer 7719ed346baSBosko Milekic mtx_lock(&ng_nodelist_mtx); 77230400f03SJulian Elischer node->nd_type->refs--; /* XXX maybe should get types lock? */ 77330400f03SJulian Elischer LIST_REMOVE(node, nd_nodes); 7749ed346baSBosko Milekic mtx_unlock(&ng_nodelist_mtx); 775069154d5SJulian Elischer 7769ed346baSBosko Milekic mtx_lock(&ng_idhash_mtx); 77730400f03SJulian Elischer LIST_REMOVE(node, nd_idnodes); 7789ed346baSBosko Milekic mtx_unlock(&ng_idhash_mtx); 779069154d5SJulian Elischer 78012574a02SJulian Elischer mtx_destroy(&node->nd_input_queue.q_mtx); 781069154d5SJulian Elischer NG_FREE_NODE(node); 7824cf49a43SJulian Elischer } 7835951069aSJulian Elischer return (v); 7844cf49a43SJulian Elischer } 7854cf49a43SJulian Elischer 7864cf49a43SJulian Elischer /************************************************************************ 787dc90cad9SJulian Elischer Node ID handling 788dc90cad9SJulian Elischer ************************************************************************/ 789dc90cad9SJulian Elischer static node_p 790069154d5SJulian Elischer ng_ID2noderef(ng_ID_t ID) 791dc90cad9SJulian Elischer { 79230400f03SJulian Elischer node_p node; 7939ed346baSBosko Milekic mtx_lock(&ng_idhash_mtx); 7940f150d04SJulian Elischer NG_IDHASH_FIND(ID, node); 79530400f03SJulian Elischer if(node) 79630400f03SJulian Elischer NG_NODE_REF(node); 7979ed346baSBosko Milekic mtx_unlock(&ng_idhash_mtx); 79830400f03SJulian Elischer return(node); 799dc90cad9SJulian Elischer } 800dc90cad9SJulian Elischer 801dc90cad9SJulian Elischer ng_ID_t 802dc90cad9SJulian Elischer ng_node2ID(node_p node) 803dc90cad9SJulian Elischer { 80470de87f2SJulian Elischer return (node ? NG_NODE_ID(node) : 0); 805dc90cad9SJulian Elischer } 806dc90cad9SJulian Elischer 807dc90cad9SJulian Elischer /************************************************************************ 8084cf49a43SJulian Elischer Node name handling 8094cf49a43SJulian Elischer ************************************************************************/ 8104cf49a43SJulian Elischer 8114cf49a43SJulian Elischer /* 8124cf49a43SJulian Elischer * Assign a node a name. Once assigned, the name cannot be changed. 8134cf49a43SJulian Elischer */ 8144cf49a43SJulian Elischer int 8154cf49a43SJulian Elischer ng_name_node(node_p node, const char *name) 8164cf49a43SJulian Elischer { 8174cf49a43SJulian Elischer int i; 818069154d5SJulian Elischer node_p node2; 8194cf49a43SJulian Elischer 8204cf49a43SJulian Elischer /* Check the name is valid */ 82187e2c66aSHartmut Brandt for (i = 0; i < NG_NODESIZ; i++) { 8224cf49a43SJulian Elischer if (name[i] == '\0' || name[i] == '.' || name[i] == ':') 8234cf49a43SJulian Elischer break; 8244cf49a43SJulian Elischer } 8254cf49a43SJulian Elischer if (i == 0 || name[i] != '\0') { 8266b795970SJulian Elischer TRAP_ERROR(); 8274cf49a43SJulian Elischer return (EINVAL); 8284cf49a43SJulian Elischer } 829dc90cad9SJulian Elischer if (ng_decodeidname(name) != 0) { /* valid IDs not allowed here */ 8306b795970SJulian Elischer TRAP_ERROR(); 8314cf49a43SJulian Elischer return (EINVAL); 8324cf49a43SJulian Elischer } 8334cf49a43SJulian Elischer 8344cf49a43SJulian Elischer /* Check the name isn't already being used */ 835069154d5SJulian Elischer if ((node2 = ng_name2noderef(node, name)) != NULL) { 83630400f03SJulian Elischer NG_NODE_UNREF(node2); 8376b795970SJulian Elischer TRAP_ERROR(); 8384cf49a43SJulian Elischer return (EADDRINUSE); 8394cf49a43SJulian Elischer } 8404cf49a43SJulian Elischer 841069154d5SJulian Elischer /* copy it */ 84287e2c66aSHartmut Brandt strlcpy(NG_NODE_NAME(node), name, NG_NODESIZ); 8434cf49a43SJulian Elischer 8444cf49a43SJulian Elischer return (0); 8454cf49a43SJulian Elischer } 8464cf49a43SJulian Elischer 8474cf49a43SJulian Elischer /* 8484cf49a43SJulian Elischer * Find a node by absolute name. The name should NOT end with ':' 8494cf49a43SJulian Elischer * The name "." means "this node" and "[xxx]" means "the node 8504cf49a43SJulian Elischer * with ID (ie, at address) xxx". 8514cf49a43SJulian Elischer * 8524cf49a43SJulian Elischer * Returns the node if found, else NULL. 853069154d5SJulian Elischer * Eventually should add something faster than a sequential search. 854e1e8f51bSRobert Watson * Note it acquires a reference on the node so you can be sure it's still 855e1e8f51bSRobert Watson * there. 8564cf49a43SJulian Elischer */ 8574cf49a43SJulian Elischer node_p 858069154d5SJulian Elischer ng_name2noderef(node_p here, const char *name) 8594cf49a43SJulian Elischer { 860dc90cad9SJulian Elischer node_p node; 861dc90cad9SJulian Elischer ng_ID_t temp; 8624cf49a43SJulian Elischer 8634cf49a43SJulian Elischer /* "." means "this node" */ 864069154d5SJulian Elischer if (strcmp(name, ".") == 0) { 86530400f03SJulian Elischer NG_NODE_REF(here); 866069154d5SJulian Elischer return(here); 867069154d5SJulian Elischer } 8684cf49a43SJulian Elischer 8694cf49a43SJulian Elischer /* Check for name-by-ID */ 870dc90cad9SJulian Elischer if ((temp = ng_decodeidname(name)) != 0) { 871069154d5SJulian Elischer return (ng_ID2noderef(temp)); 8724cf49a43SJulian Elischer } 8734cf49a43SJulian Elischer 8744cf49a43SJulian Elischer /* Find node by name */ 8759ed346baSBosko Milekic mtx_lock(&ng_nodelist_mtx); 87630400f03SJulian Elischer LIST_FOREACH(node, &ng_nodelist, nd_nodes) { 87770de87f2SJulian Elischer if (NG_NODE_IS_VALID(node) 87870de87f2SJulian Elischer && NG_NODE_HAS_NAME(node) 87970de87f2SJulian Elischer && (strcmp(NG_NODE_NAME(node), name) == 0)) { 8804cf49a43SJulian Elischer break; 8814cf49a43SJulian Elischer } 88270de87f2SJulian Elischer } 883069154d5SJulian Elischer if (node) 88430400f03SJulian Elischer NG_NODE_REF(node); 8859ed346baSBosko Milekic mtx_unlock(&ng_nodelist_mtx); 8864cf49a43SJulian Elischer return (node); 8874cf49a43SJulian Elischer } 8884cf49a43SJulian Elischer 8894cf49a43SJulian Elischer /* 8909d5abbddSJens Schweikhardt * Decode an ID name, eg. "[f03034de]". Returns 0 if the 891dc90cad9SJulian Elischer * string is not valid, otherwise returns the value. 8924cf49a43SJulian Elischer */ 893dc90cad9SJulian Elischer static ng_ID_t 8944cf49a43SJulian Elischer ng_decodeidname(const char *name) 8954cf49a43SJulian Elischer { 8962b70adcbSArchie Cobbs const int len = strlen(name); 89725792ef3SArchie Cobbs char *eptr; 8982b70adcbSArchie Cobbs u_long val; 8994cf49a43SJulian Elischer 9002b70adcbSArchie Cobbs /* Check for proper length, brackets, no leading junk */ 90170de87f2SJulian Elischer if ((len < 3) 90270de87f2SJulian Elischer || (name[0] != '[') 90370de87f2SJulian Elischer || (name[len - 1] != ']') 90470de87f2SJulian Elischer || (!isxdigit(name[1]))) { 90570de87f2SJulian Elischer return ((ng_ID_t)0); 90670de87f2SJulian Elischer } 9074cf49a43SJulian Elischer 9082b70adcbSArchie Cobbs /* Decode number */ 9092b70adcbSArchie Cobbs val = strtoul(name + 1, &eptr, 16); 91070de87f2SJulian Elischer if ((eptr - name != len - 1) 91170de87f2SJulian Elischer || (val == ULONG_MAX) 91270de87f2SJulian Elischer || (val == 0)) { 91312f035e0SJulian Elischer return ((ng_ID_t)0); 91470de87f2SJulian Elischer } 9152b70adcbSArchie Cobbs return (ng_ID_t)val; 9164cf49a43SJulian Elischer } 9174cf49a43SJulian Elischer 9184cf49a43SJulian Elischer /* 9194cf49a43SJulian Elischer * Remove a name from a node. This should only be called 9204cf49a43SJulian Elischer * when shutting down and removing the node. 92164efc707SRobert Watson * IF we allow name changing this may be more resurrected. 9224cf49a43SJulian Elischer */ 9234cf49a43SJulian Elischer void 9244cf49a43SJulian Elischer ng_unname(node_p node) 9254cf49a43SJulian Elischer { 9264cf49a43SJulian Elischer } 9274cf49a43SJulian Elischer 9284cf49a43SJulian Elischer /************************************************************************ 9294cf49a43SJulian Elischer Hook routines 9304cf49a43SJulian Elischer Names are not optional. Hooks are always connected, except for a 9313e4084c8SJulian Elischer brief moment within these routines. On invalidation or during creation 9323e4084c8SJulian Elischer they are connected to the 'dead' hook. 9334cf49a43SJulian Elischer ************************************************************************/ 9344cf49a43SJulian Elischer 9354cf49a43SJulian Elischer /* 9364cf49a43SJulian Elischer * Remove a hook reference 9374cf49a43SJulian Elischer */ 93830400f03SJulian Elischer void 9394cf49a43SJulian Elischer ng_unref_hook(hook_p hook) 9404cf49a43SJulian Elischer { 94130400f03SJulian Elischer int v; 9426b795970SJulian Elischer 9436b795970SJulian Elischer if (hook == &ng_deadhook) { 9446b795970SJulian Elischer return; 9456b795970SJulian Elischer } 94630400f03SJulian Elischer do { 94730400f03SJulian Elischer v = hook->hk_refs; 94830400f03SJulian Elischer } while (! atomic_cmpset_int(&hook->hk_refs, v, v - 1)); 949e8a49db2SJulian Elischer 95030400f03SJulian Elischer if (v == 1) { /* we were the last */ 9516b795970SJulian Elischer if (_NG_HOOK_NODE(hook)) { /* it'll probably be ng_deadnode */ 9526b795970SJulian Elischer _NG_NODE_UNREF((_NG_HOOK_NODE(hook))); 95330400f03SJulian Elischer hook->hk_node = NULL; 954069154d5SJulian Elischer } 955069154d5SJulian Elischer NG_FREE_HOOK(hook); 956069154d5SJulian Elischer } 9574cf49a43SJulian Elischer } 9584cf49a43SJulian Elischer 9594cf49a43SJulian Elischer /* 9604cf49a43SJulian Elischer * Add an unconnected hook to a node. Only used internally. 9613e4084c8SJulian Elischer * Assumes node is locked. (XXX not yet true ) 9624cf49a43SJulian Elischer */ 9634cf49a43SJulian Elischer static int 9644cf49a43SJulian Elischer ng_add_hook(node_p node, const char *name, hook_p *hookp) 9654cf49a43SJulian Elischer { 9664cf49a43SJulian Elischer hook_p hook; 9674cf49a43SJulian Elischer int error = 0; 9684cf49a43SJulian Elischer 9694cf49a43SJulian Elischer /* Check that the given name is good */ 9704cf49a43SJulian Elischer if (name == NULL) { 9716b795970SJulian Elischer TRAP_ERROR(); 9724cf49a43SJulian Elischer return (EINVAL); 9734cf49a43SJulian Elischer } 974899e9c4eSArchie Cobbs if (ng_findhook(node, name) != NULL) { 9756b795970SJulian Elischer TRAP_ERROR(); 9764cf49a43SJulian Elischer return (EEXIST); 9774cf49a43SJulian Elischer } 9784cf49a43SJulian Elischer 9794cf49a43SJulian Elischer /* Allocate the hook and link it up */ 98030400f03SJulian Elischer NG_ALLOC_HOOK(hook); 9814cf49a43SJulian Elischer if (hook == NULL) { 9826b795970SJulian Elischer TRAP_ERROR(); 9834cf49a43SJulian Elischer return (ENOMEM); 9844cf49a43SJulian Elischer } 9853e4084c8SJulian Elischer hook->hk_refs = 1; /* add a reference for us to return */ 98630400f03SJulian Elischer hook->hk_flags = HK_INVALID; 9873e4084c8SJulian Elischer hook->hk_peer = &ng_deadhook; /* start off this way */ 98830400f03SJulian Elischer hook->hk_node = node; 98930400f03SJulian Elischer NG_NODE_REF(node); /* each hook counts as a reference */ 9904cf49a43SJulian Elischer 9913e4084c8SJulian Elischer /* Set hook name */ 99287e2c66aSHartmut Brandt strlcpy(NG_HOOK_NAME(hook), name, NG_HOOKSIZ); 9933e4084c8SJulian Elischer 9943e4084c8SJulian Elischer /* 9953e4084c8SJulian Elischer * Check if the node type code has something to say about it 9963e4084c8SJulian Elischer * If it fails, the unref of the hook will also unref the node. 9973e4084c8SJulian Elischer */ 998954c4772SJulian Elischer if (node->nd_type->newhook != NULL) { 999954c4772SJulian Elischer if ((error = (*node->nd_type->newhook)(node, hook, name))) { 100030400f03SJulian Elischer NG_HOOK_UNREF(hook); /* this frees the hook */ 1001069154d5SJulian Elischer return (error); 1002069154d5SJulian Elischer } 1003954c4772SJulian Elischer } 10044cf49a43SJulian Elischer /* 10054cf49a43SJulian Elischer * The 'type' agrees so far, so go ahead and link it in. 10064cf49a43SJulian Elischer * We'll ask again later when we actually connect the hooks. 10074cf49a43SJulian Elischer */ 100830400f03SJulian Elischer LIST_INSERT_HEAD(&node->nd_hooks, hook, hk_hooks); 100930400f03SJulian Elischer node->nd_numhooks++; 10103e4084c8SJulian Elischer NG_HOOK_REF(hook); /* one for the node */ 10114cf49a43SJulian Elischer 10124cf49a43SJulian Elischer if (hookp) 10134cf49a43SJulian Elischer *hookp = hook; 10143e4084c8SJulian Elischer return (0); 10154cf49a43SJulian Elischer } 10164cf49a43SJulian Elischer 10174cf49a43SJulian Elischer /* 1018899e9c4eSArchie Cobbs * Find a hook 1019899e9c4eSArchie Cobbs * 1020899e9c4eSArchie Cobbs * Node types may supply their own optimized routines for finding 1021899e9c4eSArchie Cobbs * hooks. If none is supplied, we just do a linear search. 10223e4084c8SJulian Elischer * XXX Possibly we should add a reference to the hook? 1023899e9c4eSArchie Cobbs */ 1024899e9c4eSArchie Cobbs hook_p 1025899e9c4eSArchie Cobbs ng_findhook(node_p node, const char *name) 1026899e9c4eSArchie Cobbs { 1027899e9c4eSArchie Cobbs hook_p hook; 1028899e9c4eSArchie Cobbs 102930400f03SJulian Elischer if (node->nd_type->findhook != NULL) 103030400f03SJulian Elischer return (*node->nd_type->findhook)(node, name); 103130400f03SJulian Elischer LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) { 103270de87f2SJulian Elischer if (NG_HOOK_IS_VALID(hook) 1033ce5e5f99SArchie Cobbs && (strcmp(NG_HOOK_NAME(hook), name) == 0)) 1034899e9c4eSArchie Cobbs return (hook); 1035899e9c4eSArchie Cobbs } 1036899e9c4eSArchie Cobbs return (NULL); 1037899e9c4eSArchie Cobbs } 1038899e9c4eSArchie Cobbs 1039899e9c4eSArchie Cobbs /* 10404cf49a43SJulian Elischer * Destroy a hook 10414cf49a43SJulian Elischer * 10424cf49a43SJulian Elischer * As hooks are always attached, this really destroys two hooks. 10434cf49a43SJulian Elischer * The one given, and the one attached to it. Disconnect the hooks 10443e4084c8SJulian Elischer * from each other first. We reconnect the peer hook to the 'dead' 10453e4084c8SJulian Elischer * hook so that it can still exist after we depart. We then 10463e4084c8SJulian Elischer * send the peer its own destroy message. This ensures that we only 10473e4084c8SJulian Elischer * interact with the peer's structures when it is locked processing that 10483e4084c8SJulian Elischer * message. We hold a reference to the peer hook so we are guaranteed that 10493e4084c8SJulian Elischer * the peer hook and node are still going to exist until 10503e4084c8SJulian Elischer * we are finished there as the hook holds a ref on the node. 10513e4084c8SJulian Elischer * We run this same code again on the peer hook, but that time it is already 10523e4084c8SJulian Elischer * attached to the 'dead' hook. 10536b795970SJulian Elischer * 10546b795970SJulian Elischer * This routine is called at all stages of hook creation 10556b795970SJulian Elischer * on error detection and must be able to handle any such stage. 10564cf49a43SJulian Elischer */ 10574cf49a43SJulian Elischer void 10584cf49a43SJulian Elischer ng_destroy_hook(hook_p hook) 10594cf49a43SJulian Elischer { 1060ac5dd141SGleb Smirnoff hook_p peer; 1061ac5dd141SGleb Smirnoff node_p node; 10624cf49a43SJulian Elischer 10636b795970SJulian Elischer if (hook == &ng_deadhook) { /* better safe than sorry */ 10646b795970SJulian Elischer printf("ng_destroy_hook called on deadhook\n"); 10656b795970SJulian Elischer return; 10666b795970SJulian Elischer } 1067ac5dd141SGleb Smirnoff 1068ac5dd141SGleb Smirnoff /* 1069ac5dd141SGleb Smirnoff * Protect divorce process with mutex, to avoid races on 1070ac5dd141SGleb Smirnoff * simultaneous disconnect. 1071ac5dd141SGleb Smirnoff */ 1072ac5dd141SGleb Smirnoff mtx_lock(&ng_topo_mtx); 1073ac5dd141SGleb Smirnoff 1074ac5dd141SGleb Smirnoff hook->hk_flags |= HK_INVALID; 1075ac5dd141SGleb Smirnoff 1076ac5dd141SGleb Smirnoff peer = NG_HOOK_PEER(hook); 1077ac5dd141SGleb Smirnoff node = NG_HOOK_NODE(hook); 1078ac5dd141SGleb Smirnoff 10793e4084c8SJulian Elischer if (peer && (peer != &ng_deadhook)) { 10803e4084c8SJulian Elischer /* 10813e4084c8SJulian Elischer * Set the peer to point to ng_deadhook 10823e4084c8SJulian Elischer * from this moment on we are effectively independent it. 10833e4084c8SJulian Elischer * send it an rmhook message of it's own. 10843e4084c8SJulian Elischer */ 10853e4084c8SJulian Elischer peer->hk_peer = &ng_deadhook; /* They no longer know us */ 10863e4084c8SJulian Elischer hook->hk_peer = &ng_deadhook; /* Nor us, them */ 10876b795970SJulian Elischer if (NG_HOOK_NODE(peer) == &ng_deadnode) { 10886b795970SJulian Elischer /* 10896b795970SJulian Elischer * If it's already divorced from a node, 10906b795970SJulian Elischer * just free it. 10916b795970SJulian Elischer */ 1092ac5dd141SGleb Smirnoff mtx_unlock(&ng_topo_mtx); 10936b795970SJulian Elischer } else { 1094ac5dd141SGleb Smirnoff mtx_unlock(&ng_topo_mtx); 10956b795970SJulian Elischer ng_rmhook_self(peer); /* Send it a surprise */ 10966b795970SJulian Elischer } 109752fa3556SJulian Elischer NG_HOOK_UNREF(peer); /* account for peer link */ 109852fa3556SJulian Elischer NG_HOOK_UNREF(hook); /* account for peer link */ 1099ac5dd141SGleb Smirnoff } else 1100ac5dd141SGleb Smirnoff mtx_unlock(&ng_topo_mtx); 1101ac5dd141SGleb Smirnoff 1102ac5dd141SGleb Smirnoff mtx_assert(&ng_topo_mtx, MA_NOTOWNED); 11034cf49a43SJulian Elischer 11044cf49a43SJulian Elischer /* 11054cf49a43SJulian Elischer * Remove the hook from the node's list to avoid possible recursion 11064cf49a43SJulian Elischer * in case the disconnection results in node shutdown. 11074cf49a43SJulian Elischer */ 11086b795970SJulian Elischer if (node == &ng_deadnode) { /* happens if called from ng_con_nodes() */ 11096b795970SJulian Elischer return; 11106b795970SJulian Elischer } 111130400f03SJulian Elischer LIST_REMOVE(hook, hk_hooks); 111230400f03SJulian Elischer node->nd_numhooks--; 111330400f03SJulian Elischer if (node->nd_type->disconnect) { 11144cf49a43SJulian Elischer /* 11156b795970SJulian Elischer * The type handler may elect to destroy the node so don't 111664efc707SRobert Watson * trust its existence after this point. (except 11176b795970SJulian Elischer * that we still hold a reference on it. (which we 11186b795970SJulian Elischer * inherrited from the hook we are destroying) 11194cf49a43SJulian Elischer */ 112030400f03SJulian Elischer (*node->nd_type->disconnect) (hook); 11214cf49a43SJulian Elischer } 11226b795970SJulian Elischer 11236b795970SJulian Elischer /* 11246b795970SJulian Elischer * Note that because we will point to ng_deadnode, the original node 11256b795970SJulian Elischer * is not decremented automatically so we do that manually. 11266b795970SJulian Elischer */ 11276b795970SJulian Elischer _NG_HOOK_NODE(hook) = &ng_deadnode; 11286b795970SJulian Elischer NG_NODE_UNREF(node); /* We no longer point to it so adjust count */ 11296b795970SJulian Elischer NG_HOOK_UNREF(hook); /* Account for linkage (in list) to node */ 11304cf49a43SJulian Elischer } 11314cf49a43SJulian Elischer 11324cf49a43SJulian Elischer /* 11334cf49a43SJulian Elischer * Take two hooks on a node and merge the connection so that the given node 11344cf49a43SJulian Elischer * is effectively bypassed. 11354cf49a43SJulian Elischer */ 11364cf49a43SJulian Elischer int 11374cf49a43SJulian Elischer ng_bypass(hook_p hook1, hook_p hook2) 11384cf49a43SJulian Elischer { 113930400f03SJulian Elischer if (hook1->hk_node != hook2->hk_node) { 11406b795970SJulian Elischer TRAP_ERROR(); 11414cf49a43SJulian Elischer return (EINVAL); 114230400f03SJulian Elischer } 114330400f03SJulian Elischer hook1->hk_peer->hk_peer = hook2->hk_peer; 114430400f03SJulian Elischer hook2->hk_peer->hk_peer = hook1->hk_peer; 11454cf49a43SJulian Elischer 11463e4084c8SJulian Elischer hook1->hk_peer = &ng_deadhook; 11473e4084c8SJulian Elischer hook2->hk_peer = &ng_deadhook; 11483e4084c8SJulian Elischer 1149cf3254aaSGleb Smirnoff NG_HOOK_UNREF(hook1); 1150cf3254aaSGleb Smirnoff NG_HOOK_UNREF(hook2); 1151cf3254aaSGleb Smirnoff 11524cf49a43SJulian Elischer /* XXX If we ever cache methods on hooks update them as well */ 11534cf49a43SJulian Elischer ng_destroy_hook(hook1); 11544cf49a43SJulian Elischer ng_destroy_hook(hook2); 11554cf49a43SJulian Elischer return (0); 11564cf49a43SJulian Elischer } 11574cf49a43SJulian Elischer 11584cf49a43SJulian Elischer /* 11594cf49a43SJulian Elischer * Install a new netgraph type 11604cf49a43SJulian Elischer */ 11614cf49a43SJulian Elischer int 11624cf49a43SJulian Elischer ng_newtype(struct ng_type *tp) 11634cf49a43SJulian Elischer { 11644cf49a43SJulian Elischer const size_t namelen = strlen(tp->name); 11654cf49a43SJulian Elischer 11664cf49a43SJulian Elischer /* Check version and type name fields */ 1167589f6ed8SJulian Elischer if ((tp->version != NG_ABI_VERSION) 1168589f6ed8SJulian Elischer || (namelen == 0) 116987e2c66aSHartmut Brandt || (namelen >= NG_TYPESIZ)) { 11706b795970SJulian Elischer TRAP_ERROR(); 11718ed370fdSJulian Elischer if (tp->version != NG_ABI_VERSION) { 11728ed370fdSJulian Elischer printf("Netgraph: Node type rejected. ABI mismatch. Suggest recompile\n"); 11738ed370fdSJulian Elischer } 11744cf49a43SJulian Elischer return (EINVAL); 11754cf49a43SJulian Elischer } 11764cf49a43SJulian Elischer 11774cf49a43SJulian Elischer /* Check for name collision */ 11784cf49a43SJulian Elischer if (ng_findtype(tp->name) != NULL) { 11796b795970SJulian Elischer TRAP_ERROR(); 11804cf49a43SJulian Elischer return (EEXIST); 11814cf49a43SJulian Elischer } 11824cf49a43SJulian Elischer 1183069154d5SJulian Elischer 1184069154d5SJulian Elischer /* Link in new type */ 11859ed346baSBosko Milekic mtx_lock(&ng_typelist_mtx); 1186069154d5SJulian Elischer LIST_INSERT_HEAD(&ng_typelist, tp, types); 1187c73b94a2SJulian Elischer tp->refs = 1; /* first ref is linked list */ 11889ed346baSBosko Milekic mtx_unlock(&ng_typelist_mtx); 11894cf49a43SJulian Elischer return (0); 11904cf49a43SJulian Elischer } 11914cf49a43SJulian Elischer 11924cf49a43SJulian Elischer /* 1193c31b4a53SJulian Elischer * unlink a netgraph type 1194c31b4a53SJulian Elischer * If no examples exist 1195c31b4a53SJulian Elischer */ 1196c31b4a53SJulian Elischer int 1197c31b4a53SJulian Elischer ng_rmtype(struct ng_type *tp) 1198c31b4a53SJulian Elischer { 1199c31b4a53SJulian Elischer /* Check for name collision */ 1200c31b4a53SJulian Elischer if (tp->refs != 1) { 1201c31b4a53SJulian Elischer TRAP_ERROR(); 1202c31b4a53SJulian Elischer return (EBUSY); 1203c31b4a53SJulian Elischer } 1204c31b4a53SJulian Elischer 1205c31b4a53SJulian Elischer /* Unlink type */ 1206c31b4a53SJulian Elischer mtx_lock(&ng_typelist_mtx); 1207c31b4a53SJulian Elischer LIST_REMOVE(tp, types); 1208c31b4a53SJulian Elischer mtx_unlock(&ng_typelist_mtx); 1209c31b4a53SJulian Elischer return (0); 1210c31b4a53SJulian Elischer } 1211c31b4a53SJulian Elischer 1212c31b4a53SJulian Elischer /* 12134cf49a43SJulian Elischer * Look for a type of the name given 12144cf49a43SJulian Elischer */ 12154cf49a43SJulian Elischer struct ng_type * 12164cf49a43SJulian Elischer ng_findtype(const char *typename) 12174cf49a43SJulian Elischer { 12184cf49a43SJulian Elischer struct ng_type *type; 12194cf49a43SJulian Elischer 12209ed346baSBosko Milekic mtx_lock(&ng_typelist_mtx); 1221069154d5SJulian Elischer LIST_FOREACH(type, &ng_typelist, types) { 12224cf49a43SJulian Elischer if (strcmp(type->name, typename) == 0) 12234cf49a43SJulian Elischer break; 12244cf49a43SJulian Elischer } 12259ed346baSBosko Milekic mtx_unlock(&ng_typelist_mtx); 12264cf49a43SJulian Elischer return (type); 12274cf49a43SJulian Elischer } 12284cf49a43SJulian Elischer 12294cf49a43SJulian Elischer /************************************************************************ 12304cf49a43SJulian Elischer Composite routines 12314cf49a43SJulian Elischer ************************************************************************/ 12324cf49a43SJulian Elischer /* 12336b795970SJulian Elischer * Connect two nodes using the specified hooks, using queued functions. 12344cf49a43SJulian Elischer */ 1235e088dd4cSAlexander Motin static int 1236e088dd4cSAlexander Motin ng_con_part3(node_p node, item_p item, hook_p hook) 12374cf49a43SJulian Elischer { 1238e088dd4cSAlexander Motin int error = 0; 12394cf49a43SJulian Elischer 12406b795970SJulian Elischer /* 12416b795970SJulian Elischer * When we run, we know that the node 'node' is locked for us. 12426b795970SJulian Elischer * Our caller has a reference on the hook. 12436b795970SJulian Elischer * Our caller has a reference on the node. 12446b795970SJulian Elischer * (In this case our caller is ng_apply_item() ). 12456b795970SJulian Elischer * The peer hook has a reference on the hook. 12461acb27c6SJulian Elischer * We are all set up except for the final call to the node, and 12471acb27c6SJulian Elischer * the clearing of the INVALID flag. 12486b795970SJulian Elischer */ 12496b795970SJulian Elischer if (NG_HOOK_NODE(hook) == &ng_deadnode) { 12506b795970SJulian Elischer /* 12516b795970SJulian Elischer * The node must have been freed again since we last visited 12526b795970SJulian Elischer * here. ng_destry_hook() has this effect but nothing else does. 12536b795970SJulian Elischer * We should just release our references and 12546b795970SJulian Elischer * free anything we can think of. 12556b795970SJulian Elischer * Since we know it's been destroyed, and it's our caller 12566b795970SJulian Elischer * that holds the references, just return. 12576b795970SJulian Elischer */ 1258e088dd4cSAlexander Motin ERROUT(ENOENT); 12596b795970SJulian Elischer } 12606b795970SJulian Elischer if (hook->hk_node->nd_type->connect) { 1261e088dd4cSAlexander Motin if ((error = (*hook->hk_node->nd_type->connect) (hook))) { 12626b795970SJulian Elischer ng_destroy_hook(hook); /* also zaps peer */ 12631acb27c6SJulian Elischer printf("failed in ng_con_part3()\n"); 1264e088dd4cSAlexander Motin ERROUT(error); 12654cf49a43SJulian Elischer } 12666b795970SJulian Elischer } 12676b795970SJulian Elischer /* 12686b795970SJulian Elischer * XXX this is wrong for SMP. Possibly we need 12696b795970SJulian Elischer * to separate out 'create' and 'invalid' flags. 12706b795970SJulian Elischer * should only set flags on hooks we have locked under our node. 12716b795970SJulian Elischer */ 12726b795970SJulian Elischer hook->hk_flags &= ~HK_INVALID; 1273e088dd4cSAlexander Motin done: 1274e088dd4cSAlexander Motin NG_FREE_ITEM(item); 1275e088dd4cSAlexander Motin return (error); 12766b795970SJulian Elischer } 12776b795970SJulian Elischer 1278e088dd4cSAlexander Motin static int 1279e088dd4cSAlexander Motin ng_con_part2(node_p node, item_p item, hook_p hook) 12806b795970SJulian Elischer { 1281ac5dd141SGleb Smirnoff hook_p peer; 1282e088dd4cSAlexander Motin int error = 0; 12836b795970SJulian Elischer 12846b795970SJulian Elischer /* 12856b795970SJulian Elischer * When we run, we know that the node 'node' is locked for us. 12866b795970SJulian Elischer * Our caller has a reference on the hook. 12876b795970SJulian Elischer * Our caller has a reference on the node. 12886b795970SJulian Elischer * (In this case our caller is ng_apply_item() ). 12896b795970SJulian Elischer * The peer hook has a reference on the hook. 12906b795970SJulian Elischer * our node pointer points to the 'dead' node. 12916b795970SJulian Elischer * First check the hook name is unique. 12921acb27c6SJulian Elischer * Should not happen because we checked before queueing this. 12936b795970SJulian Elischer */ 12946b795970SJulian Elischer if (ng_findhook(node, NG_HOOK_NAME(hook)) != NULL) { 12956b795970SJulian Elischer TRAP_ERROR(); 12966b795970SJulian Elischer ng_destroy_hook(hook); /* should destroy peer too */ 12971acb27c6SJulian Elischer printf("failed in ng_con_part2()\n"); 1298e088dd4cSAlexander Motin ERROUT(EEXIST); 12996b795970SJulian Elischer } 13006b795970SJulian Elischer /* 13016b795970SJulian Elischer * Check if the node type code has something to say about it 13026b795970SJulian Elischer * If it fails, the unref of the hook will also unref the attached node, 13036b795970SJulian Elischer * however since that node is 'ng_deadnode' this will do nothing. 13046b795970SJulian Elischer * The peer hook will also be destroyed. 13056b795970SJulian Elischer */ 13066b795970SJulian Elischer if (node->nd_type->newhook != NULL) { 1307e088dd4cSAlexander Motin if ((error = (*node->nd_type->newhook)(node, hook, 1308e088dd4cSAlexander Motin hook->hk_name))) { 13096b795970SJulian Elischer ng_destroy_hook(hook); /* should destroy peer too */ 13101acb27c6SJulian Elischer printf("failed in ng_con_part2()\n"); 1311e088dd4cSAlexander Motin ERROUT(error); 13126b795970SJulian Elischer } 13136b795970SJulian Elischer } 13146b795970SJulian Elischer 13156b795970SJulian Elischer /* 13166b795970SJulian Elischer * The 'type' agrees so far, so go ahead and link it in. 13176b795970SJulian Elischer * We'll ask again later when we actually connect the hooks. 13186b795970SJulian Elischer */ 13196b795970SJulian Elischer hook->hk_node = node; /* just overwrite ng_deadnode */ 13206b795970SJulian Elischer NG_NODE_REF(node); /* each hook counts as a reference */ 13216b795970SJulian Elischer LIST_INSERT_HEAD(&node->nd_hooks, hook, hk_hooks); 13226b795970SJulian Elischer node->nd_numhooks++; 13236b795970SJulian Elischer NG_HOOK_REF(hook); /* one for the node */ 13246b795970SJulian Elischer 13256b795970SJulian Elischer /* 132664efc707SRobert Watson * We now have a symmetrical situation, where both hooks have been 13275951069aSJulian Elischer * linked to their nodes, the newhook methods have been called 13286b795970SJulian Elischer * And the references are all correct. The hooks are still marked 13296b795970SJulian Elischer * as invalid, as we have not called the 'connect' methods 13306b795970SJulian Elischer * yet. 133164efc707SRobert Watson * We can call the local one immediately as we have the 13326b795970SJulian Elischer * node locked, but we need to queue the remote one. 13336b795970SJulian Elischer */ 13346b795970SJulian Elischer if (hook->hk_node->nd_type->connect) { 1335e088dd4cSAlexander Motin if ((error = (*hook->hk_node->nd_type->connect) (hook))) { 13366b795970SJulian Elischer ng_destroy_hook(hook); /* also zaps peer */ 13371acb27c6SJulian Elischer printf("failed in ng_con_part2(A)\n"); 1338e088dd4cSAlexander Motin ERROUT(error); 13396b795970SJulian Elischer } 13406b795970SJulian Elischer } 1341ac5dd141SGleb Smirnoff 1342ac5dd141SGleb Smirnoff /* 1343ac5dd141SGleb Smirnoff * Acquire topo mutex to avoid race with ng_destroy_hook(). 1344ac5dd141SGleb Smirnoff */ 1345ac5dd141SGleb Smirnoff mtx_lock(&ng_topo_mtx); 1346ac5dd141SGleb Smirnoff peer = hook->hk_peer; 1347ac5dd141SGleb Smirnoff if (peer == &ng_deadhook) { 1348ac5dd141SGleb Smirnoff mtx_unlock(&ng_topo_mtx); 1349ac5dd141SGleb Smirnoff printf("failed in ng_con_part2(B)\n"); 1350ac5dd141SGleb Smirnoff ng_destroy_hook(hook); 1351e088dd4cSAlexander Motin ERROUT(ENOENT); 1352ac5dd141SGleb Smirnoff } 1353ac5dd141SGleb Smirnoff mtx_unlock(&ng_topo_mtx); 1354ac5dd141SGleb Smirnoff 1355b332b91fSGleb Smirnoff if ((error = ng_send_fn2(peer->hk_node, peer, item, &ng_con_part3, 1356b332b91fSGleb Smirnoff NULL, 0, NG_REUSE_ITEM))) { 1357ac5dd141SGleb Smirnoff printf("failed in ng_con_part2(C)\n"); 13581acb27c6SJulian Elischer ng_destroy_hook(hook); /* also zaps peer */ 1359e088dd4cSAlexander Motin return (error); /* item was consumed. */ 13601acb27c6SJulian Elischer } 13616b795970SJulian Elischer hook->hk_flags &= ~HK_INVALID; /* need both to be able to work */ 1362e088dd4cSAlexander Motin return (0); /* item was consumed. */ 1363e088dd4cSAlexander Motin done: 1364e088dd4cSAlexander Motin NG_FREE_ITEM(item); 1365e088dd4cSAlexander Motin return (error); 13664cf49a43SJulian Elischer } 13674cf49a43SJulian Elischer 13684cf49a43SJulian Elischer /* 13696b795970SJulian Elischer * Connect this node with another node. We assume that this node is 13706b795970SJulian Elischer * currently locked, as we are only called from an NGM_CONNECT message. 13714cf49a43SJulian Elischer */ 13726b795970SJulian Elischer static int 1373e088dd4cSAlexander Motin ng_con_nodes(item_p item, node_p node, const char *name, 1374e088dd4cSAlexander Motin node_p node2, const char *name2) 13754cf49a43SJulian Elischer { 13764cf49a43SJulian Elischer int error; 13774cf49a43SJulian Elischer hook_p hook; 13784cf49a43SJulian Elischer hook_p hook2; 13794cf49a43SJulian Elischer 13801acb27c6SJulian Elischer if (ng_findhook(node2, name2) != NULL) { 13811acb27c6SJulian Elischer return(EEXIST); 13821acb27c6SJulian Elischer } 13833e4084c8SJulian Elischer if ((error = ng_add_hook(node, name, &hook))) /* gives us a ref */ 13844cf49a43SJulian Elischer return (error); 13856b795970SJulian Elischer /* Allocate the other hook and link it up */ 13866b795970SJulian Elischer NG_ALLOC_HOOK(hook2); 138719724144SGleb Smirnoff if (hook2 == NULL) { 13886b795970SJulian Elischer TRAP_ERROR(); 13896b795970SJulian Elischer ng_destroy_hook(hook); /* XXX check ref counts so far */ 13906b795970SJulian Elischer NG_HOOK_UNREF(hook); /* including our ref */ 13916b795970SJulian Elischer return (ENOMEM); 13926b795970SJulian Elischer } 13936b795970SJulian Elischer hook2->hk_refs = 1; /* start with a reference for us. */ 13946b795970SJulian Elischer hook2->hk_flags = HK_INVALID; 13956b795970SJulian Elischer hook2->hk_peer = hook; /* Link the two together */ 13966b795970SJulian Elischer hook->hk_peer = hook2; 13976b795970SJulian Elischer NG_HOOK_REF(hook); /* Add a ref for the peer to each*/ 13986b795970SJulian Elischer NG_HOOK_REF(hook2); 13996b795970SJulian Elischer hook2->hk_node = &ng_deadnode; 140087e2c66aSHartmut Brandt strlcpy(NG_HOOK_NAME(hook2), name2, NG_HOOKSIZ); 14016b795970SJulian Elischer 14026b795970SJulian Elischer /* 14036b795970SJulian Elischer * Queue the function above. 14046b795970SJulian Elischer * Procesing continues in that function in the lock context of 14056b795970SJulian Elischer * the other node. 14066b795970SJulian Elischer */ 1407b332b91fSGleb Smirnoff if ((error = ng_send_fn2(node2, hook2, item, &ng_con_part2, NULL, 0, 1408b332b91fSGleb Smirnoff NG_NOFLAGS))) { 14093fb87c24SAlexander Motin printf("failed in ng_con_nodes(): %d\n", error); 14103fb87c24SAlexander Motin ng_destroy_hook(hook); /* also zaps peer */ 14113fb87c24SAlexander Motin } 14126b795970SJulian Elischer 14136b795970SJulian Elischer NG_HOOK_UNREF(hook); /* Let each hook go if it wants to */ 14146b795970SJulian Elischer NG_HOOK_UNREF(hook2); 14153fb87c24SAlexander Motin return (error); 14164cf49a43SJulian Elischer } 14176b795970SJulian Elischer 14186b795970SJulian Elischer /* 14196b795970SJulian Elischer * Make a peer and connect. 14206b795970SJulian Elischer * We assume that the local node is locked. 14216b795970SJulian Elischer * The new node probably doesn't need a lock until 14226b795970SJulian Elischer * it has a hook, because it cannot really have any work until then, 14236b795970SJulian Elischer * but we should think about it a bit more. 14246b795970SJulian Elischer * 14256b795970SJulian Elischer * The problem may come if the other node also fires up 14266b795970SJulian Elischer * some hardware or a timer or some other source of activation, 14276b795970SJulian Elischer * also it may already get a command msg via it's ID. 14286b795970SJulian Elischer * 14296b795970SJulian Elischer * We could use the same method as ng_con_nodes() but we'd have 14306b795970SJulian Elischer * to add ability to remove the node when failing. (Not hard, just 14316b795970SJulian Elischer * make arg1 point to the node to remove). 14326b795970SJulian Elischer * Unless of course we just ignore failure to connect and leave 14336b795970SJulian Elischer * an unconnected node? 14346b795970SJulian Elischer */ 14356b795970SJulian Elischer static int 14366b795970SJulian Elischer ng_mkpeer(node_p node, const char *name, const char *name2, char *type) 14376b795970SJulian Elischer { 14386b795970SJulian Elischer node_p node2; 14394c9b5910SGleb Smirnoff hook_p hook1, hook2; 14406b795970SJulian Elischer int error; 14416b795970SJulian Elischer 14426b795970SJulian Elischer if ((error = ng_make_node(type, &node2))) { 14436b795970SJulian Elischer return (error); 14446b795970SJulian Elischer } 14456b795970SJulian Elischer 14466b795970SJulian Elischer if ((error = ng_add_hook(node, name, &hook1))) { /* gives us a ref */ 14471acb27c6SJulian Elischer ng_rmnode(node2, NULL, NULL, 0); 14486b795970SJulian Elischer return (error); 14496b795970SJulian Elischer } 14506b795970SJulian Elischer 14516b795970SJulian Elischer if ((error = ng_add_hook(node2, name2, &hook2))) { 14521acb27c6SJulian Elischer ng_rmnode(node2, NULL, NULL, 0); 14536b795970SJulian Elischer ng_destroy_hook(hook1); 14546b795970SJulian Elischer NG_HOOK_UNREF(hook1); 14556b795970SJulian Elischer return (error); 14566b795970SJulian Elischer } 14576b795970SJulian Elischer 14586b795970SJulian Elischer /* 14596b795970SJulian Elischer * Actually link the two hooks together. 14606b795970SJulian Elischer */ 14616b795970SJulian Elischer hook1->hk_peer = hook2; 14626b795970SJulian Elischer hook2->hk_peer = hook1; 14636b795970SJulian Elischer 14646b795970SJulian Elischer /* Each hook is referenced by the other */ 14656b795970SJulian Elischer NG_HOOK_REF(hook1); 14666b795970SJulian Elischer NG_HOOK_REF(hook2); 14676b795970SJulian Elischer 14686b795970SJulian Elischer /* Give each node the opportunity to veto the pending connection */ 14696b795970SJulian Elischer if (hook1->hk_node->nd_type->connect) { 14706b795970SJulian Elischer error = (*hook1->hk_node->nd_type->connect) (hook1); 14716b795970SJulian Elischer } 14726b795970SJulian Elischer 14736b795970SJulian Elischer if ((error == 0) && hook2->hk_node->nd_type->connect) { 14746b795970SJulian Elischer error = (*hook2->hk_node->nd_type->connect) (hook2); 14756b795970SJulian Elischer 14766b795970SJulian Elischer } 14773e4084c8SJulian Elischer 14783e4084c8SJulian Elischer /* 14793e4084c8SJulian Elischer * drop the references we were holding on the two hooks. 14803e4084c8SJulian Elischer */ 14816b795970SJulian Elischer if (error) { 14826b795970SJulian Elischer ng_destroy_hook(hook2); /* also zaps hook1 */ 14831acb27c6SJulian Elischer ng_rmnode(node2, NULL, NULL, 0); 14846b795970SJulian Elischer } else { 14856b795970SJulian Elischer /* As a last act, allow the hooks to be used */ 14866b795970SJulian Elischer hook1->hk_flags &= ~HK_INVALID; 14876b795970SJulian Elischer hook2->hk_flags &= ~HK_INVALID; 14886b795970SJulian Elischer } 14896b795970SJulian Elischer NG_HOOK_UNREF(hook1); 14903e4084c8SJulian Elischer NG_HOOK_UNREF(hook2); 14913e4084c8SJulian Elischer return (error); 14924cf49a43SJulian Elischer } 14936b795970SJulian Elischer 1494069154d5SJulian Elischer /************************************************************************ 1495069154d5SJulian Elischer Utility routines to send self messages 1496069154d5SJulian Elischer ************************************************************************/ 1497069154d5SJulian Elischer 14981acb27c6SJulian Elischer /* Shut this node down as soon as everyone is clear of it */ 149964efc707SRobert Watson /* Should add arg "immediately" to jump the queue */ 1500069154d5SJulian Elischer int 15011acb27c6SJulian Elischer ng_rmnode_self(node_p node) 1502069154d5SJulian Elischer { 15031acb27c6SJulian Elischer int error; 15044cf49a43SJulian Elischer 15051acb27c6SJulian Elischer if (node == &ng_deadnode) 15061acb27c6SJulian Elischer return (0); 1507be4252b3SJulian Elischer node->nd_flags |= NGF_INVALID; 1508be4252b3SJulian Elischer if (node->nd_flags & NGF_CLOSING) 15091acb27c6SJulian Elischer return (0); 1510069154d5SJulian Elischer 15111acb27c6SJulian Elischer error = ng_send_fn(node, NULL, &ng_rmnode, NULL, 0); 15121acb27c6SJulian Elischer return (error); 1513069154d5SJulian Elischer } 1514069154d5SJulian Elischer 15151acb27c6SJulian Elischer static void 15166b795970SJulian Elischer ng_rmhook_part2(node_p node, hook_p hook, void *arg1, int arg2) 15176b795970SJulian Elischer { 15186b795970SJulian Elischer ng_destroy_hook(hook); 15191acb27c6SJulian Elischer return ; 15206b795970SJulian Elischer } 15216b795970SJulian Elischer 1522954c4772SJulian Elischer int 1523954c4772SJulian Elischer ng_rmhook_self(hook_p hook) 1524954c4772SJulian Elischer { 15256b795970SJulian Elischer int error; 1526954c4772SJulian Elischer node_p node = NG_HOOK_NODE(hook); 1527954c4772SJulian Elischer 15286b795970SJulian Elischer if (node == &ng_deadnode) 15296b795970SJulian Elischer return (0); 15306b795970SJulian Elischer 15316b795970SJulian Elischer error = ng_send_fn(node, hook, &ng_rmhook_part2, NULL, 0); 15326b795970SJulian Elischer return (error); 1533954c4772SJulian Elischer } 1534954c4772SJulian Elischer 1535069154d5SJulian Elischer /*********************************************************************** 15364cf49a43SJulian Elischer * Parse and verify a string of the form: <NODE:><PATH> 15374cf49a43SJulian Elischer * 15384cf49a43SJulian Elischer * Such a string can refer to a specific node or a specific hook 15394cf49a43SJulian Elischer * on a specific node, depending on how you look at it. In the 15404cf49a43SJulian Elischer * latter case, the PATH component must not end in a dot. 15414cf49a43SJulian Elischer * 15424cf49a43SJulian Elischer * Both <NODE:> and <PATH> are optional. The <PATH> is a string 15434cf49a43SJulian Elischer * of hook names separated by dots. This breaks out the original 15444cf49a43SJulian Elischer * string, setting *nodep to "NODE" (or NULL if none) and *pathp 15454cf49a43SJulian Elischer * to "PATH" (or NULL if degenerate). Also, *hookp will point to 15464cf49a43SJulian Elischer * the final hook component of <PATH>, if any, otherwise NULL. 15474cf49a43SJulian Elischer * 15484cf49a43SJulian Elischer * This returns -1 if the path is malformed. The char ** are optional. 1549069154d5SJulian Elischer ***********************************************************************/ 15504cf49a43SJulian Elischer int 15514cf49a43SJulian Elischer ng_path_parse(char *addr, char **nodep, char **pathp, char **hookp) 15524cf49a43SJulian Elischer { 15534cf49a43SJulian Elischer char *node, *path, *hook; 15544cf49a43SJulian Elischer int k; 15554cf49a43SJulian Elischer 15564cf49a43SJulian Elischer /* 15574cf49a43SJulian Elischer * Extract absolute NODE, if any 15584cf49a43SJulian Elischer */ 15594cf49a43SJulian Elischer for (path = addr; *path && *path != ':'; path++); 15604cf49a43SJulian Elischer if (*path) { 15614cf49a43SJulian Elischer node = addr; /* Here's the NODE */ 15624cf49a43SJulian Elischer *path++ = '\0'; /* Here's the PATH */ 15634cf49a43SJulian Elischer 15644cf49a43SJulian Elischer /* Node name must not be empty */ 15654cf49a43SJulian Elischer if (!*node) 15664cf49a43SJulian Elischer return -1; 15674cf49a43SJulian Elischer 15684cf49a43SJulian Elischer /* A name of "." is OK; otherwise '.' not allowed */ 15694cf49a43SJulian Elischer if (strcmp(node, ".") != 0) { 15704cf49a43SJulian Elischer for (k = 0; node[k]; k++) 15714cf49a43SJulian Elischer if (node[k] == '.') 15724cf49a43SJulian Elischer return -1; 15734cf49a43SJulian Elischer } 15744cf49a43SJulian Elischer } else { 15754cf49a43SJulian Elischer node = NULL; /* No absolute NODE */ 15764cf49a43SJulian Elischer path = addr; /* Here's the PATH */ 15774cf49a43SJulian Elischer } 15784cf49a43SJulian Elischer 15794cf49a43SJulian Elischer /* Snoop for illegal characters in PATH */ 15804cf49a43SJulian Elischer for (k = 0; path[k]; k++) 15814cf49a43SJulian Elischer if (path[k] == ':') 15824cf49a43SJulian Elischer return -1; 15834cf49a43SJulian Elischer 15844cf49a43SJulian Elischer /* Check for no repeated dots in PATH */ 15854cf49a43SJulian Elischer for (k = 0; path[k]; k++) 15864cf49a43SJulian Elischer if (path[k] == '.' && path[k + 1] == '.') 15874cf49a43SJulian Elischer return -1; 15884cf49a43SJulian Elischer 15894cf49a43SJulian Elischer /* Remove extra (degenerate) dots from beginning or end of PATH */ 15904cf49a43SJulian Elischer if (path[0] == '.') 15914cf49a43SJulian Elischer path++; 15924cf49a43SJulian Elischer if (*path && path[strlen(path) - 1] == '.') 15934cf49a43SJulian Elischer path[strlen(path) - 1] = 0; 15944cf49a43SJulian Elischer 15954cf49a43SJulian Elischer /* If PATH has a dot, then we're not talking about a hook */ 15964cf49a43SJulian Elischer if (*path) { 15974cf49a43SJulian Elischer for (hook = path, k = 0; path[k]; k++) 15984cf49a43SJulian Elischer if (path[k] == '.') { 15994cf49a43SJulian Elischer hook = NULL; 16004cf49a43SJulian Elischer break; 16014cf49a43SJulian Elischer } 16024cf49a43SJulian Elischer } else 16034cf49a43SJulian Elischer path = hook = NULL; 16044cf49a43SJulian Elischer 16054cf49a43SJulian Elischer /* Done */ 16064cf49a43SJulian Elischer if (nodep) 16074cf49a43SJulian Elischer *nodep = node; 16084cf49a43SJulian Elischer if (pathp) 16094cf49a43SJulian Elischer *pathp = path; 16104cf49a43SJulian Elischer if (hookp) 16114cf49a43SJulian Elischer *hookp = hook; 16124cf49a43SJulian Elischer return (0); 16134cf49a43SJulian Elischer } 16144cf49a43SJulian Elischer 16154cf49a43SJulian Elischer /* 16164cf49a43SJulian Elischer * Given a path, which may be absolute or relative, and a starting node, 1617069154d5SJulian Elischer * return the destination node. 16184cf49a43SJulian Elischer */ 16194cf49a43SJulian Elischer int 1620069154d5SJulian Elischer ng_path2noderef(node_p here, const char *address, 1621069154d5SJulian Elischer node_p *destp, hook_p *lasthook) 16224cf49a43SJulian Elischer { 162387e2c66aSHartmut Brandt char fullpath[NG_PATHSIZ]; 16244cf49a43SJulian Elischer char *nodename, *path, pbuf[2]; 1625069154d5SJulian Elischer node_p node, oldnode; 16264cf49a43SJulian Elischer char *cp; 1627a4ec03cfSJulian Elischer hook_p hook = NULL; 16284cf49a43SJulian Elischer 16294cf49a43SJulian Elischer /* Initialize */ 163030400f03SJulian Elischer if (destp == NULL) { 16316b795970SJulian Elischer TRAP_ERROR(); 16324cf49a43SJulian Elischer return EINVAL; 163330400f03SJulian Elischer } 16344cf49a43SJulian Elischer *destp = NULL; 16354cf49a43SJulian Elischer 16364cf49a43SJulian Elischer /* Make a writable copy of address for ng_path_parse() */ 16374cf49a43SJulian Elischer strncpy(fullpath, address, sizeof(fullpath) - 1); 16384cf49a43SJulian Elischer fullpath[sizeof(fullpath) - 1] = '\0'; 16394cf49a43SJulian Elischer 16404cf49a43SJulian Elischer /* Parse out node and sequence of hooks */ 16414cf49a43SJulian Elischer if (ng_path_parse(fullpath, &nodename, &path, NULL) < 0) { 16426b795970SJulian Elischer TRAP_ERROR(); 16434cf49a43SJulian Elischer return EINVAL; 16444cf49a43SJulian Elischer } 16454cf49a43SJulian Elischer if (path == NULL) { 16464cf49a43SJulian Elischer pbuf[0] = '.'; /* Needs to be writable */ 16474cf49a43SJulian Elischer pbuf[1] = '\0'; 16484cf49a43SJulian Elischer path = pbuf; 16494cf49a43SJulian Elischer } 16504cf49a43SJulian Elischer 1651069154d5SJulian Elischer /* 1652069154d5SJulian Elischer * For an absolute address, jump to the starting node. 1653069154d5SJulian Elischer * Note that this holds a reference on the node for us. 1654069154d5SJulian Elischer * Don't forget to drop the reference if we don't need it. 1655069154d5SJulian Elischer */ 16564cf49a43SJulian Elischer if (nodename) { 1657069154d5SJulian Elischer node = ng_name2noderef(here, nodename); 16584cf49a43SJulian Elischer if (node == NULL) { 16596b795970SJulian Elischer TRAP_ERROR(); 16604cf49a43SJulian Elischer return (ENOENT); 16614cf49a43SJulian Elischer } 1662069154d5SJulian Elischer } else { 1663069154d5SJulian Elischer if (here == NULL) { 16646b795970SJulian Elischer TRAP_ERROR(); 1665069154d5SJulian Elischer return (EINVAL); 1666069154d5SJulian Elischer } 16674cf49a43SJulian Elischer node = here; 166830400f03SJulian Elischer NG_NODE_REF(node); 1669069154d5SJulian Elischer } 16704cf49a43SJulian Elischer 1671069154d5SJulian Elischer /* 1672069154d5SJulian Elischer * Now follow the sequence of hooks 1673069154d5SJulian Elischer * XXX 1674069154d5SJulian Elischer * We actually cannot guarantee that the sequence 1675069154d5SJulian Elischer * is not being demolished as we crawl along it 1676069154d5SJulian Elischer * without extra-ordinary locking etc. 1677069154d5SJulian Elischer * So this is a bit dodgy to say the least. 1678069154d5SJulian Elischer * We can probably hold up some things by holding 1679069154d5SJulian Elischer * the nodelist mutex for the time of this 1680069154d5SJulian Elischer * crawl if we wanted.. At least that way we wouldn't have to 168164efc707SRobert Watson * worry about the nodes disappearing, but the hooks would still 1682069154d5SJulian Elischer * be a problem. 1683069154d5SJulian Elischer */ 16844cf49a43SJulian Elischer for (cp = path; node != NULL && *cp != '\0'; ) { 16854cf49a43SJulian Elischer char *segment; 16864cf49a43SJulian Elischer 16874cf49a43SJulian Elischer /* 16884cf49a43SJulian Elischer * Break out the next path segment. Replace the dot we just 16894cf49a43SJulian Elischer * found with a NUL; "cp" points to the next segment (or the 16904cf49a43SJulian Elischer * NUL at the end). 16914cf49a43SJulian Elischer */ 16924cf49a43SJulian Elischer for (segment = cp; *cp != '\0'; cp++) { 16934cf49a43SJulian Elischer if (*cp == '.') { 16944cf49a43SJulian Elischer *cp++ = '\0'; 16954cf49a43SJulian Elischer break; 16964cf49a43SJulian Elischer } 16974cf49a43SJulian Elischer } 16984cf49a43SJulian Elischer 16994cf49a43SJulian Elischer /* Empty segment */ 17004cf49a43SJulian Elischer if (*segment == '\0') 17014cf49a43SJulian Elischer continue; 17024cf49a43SJulian Elischer 17034cf49a43SJulian Elischer /* We have a segment, so look for a hook by that name */ 1704899e9c4eSArchie Cobbs hook = ng_findhook(node, segment); 17054cf49a43SJulian Elischer 17064cf49a43SJulian Elischer /* Can't get there from here... */ 17074cf49a43SJulian Elischer if (hook == NULL 170830400f03SJulian Elischer || NG_HOOK_PEER(hook) == NULL 170930400f03SJulian Elischer || NG_HOOK_NOT_VALID(hook) 171030400f03SJulian Elischer || NG_HOOK_NOT_VALID(NG_HOOK_PEER(hook))) { 17116b795970SJulian Elischer TRAP_ERROR(); 171230400f03SJulian Elischer NG_NODE_UNREF(node); 171330400f03SJulian Elischer #if 0 171430400f03SJulian Elischer printf("hooknotvalid %s %s %d %d %d %d ", 171530400f03SJulian Elischer path, 171630400f03SJulian Elischer segment, 171730400f03SJulian Elischer hook == NULL, 171830400f03SJulian Elischer NG_HOOK_PEER(hook) == NULL, 171930400f03SJulian Elischer NG_HOOK_NOT_VALID(hook), 172030400f03SJulian Elischer NG_HOOK_NOT_VALID(NG_HOOK_PEER(hook))); 172130400f03SJulian Elischer #endif 17224cf49a43SJulian Elischer return (ENOENT); 17234cf49a43SJulian Elischer } 17244cf49a43SJulian Elischer 1725069154d5SJulian Elischer /* 1726069154d5SJulian Elischer * Hop on over to the next node 1727069154d5SJulian Elischer * XXX 1728069154d5SJulian Elischer * Big race conditions here as hooks and nodes go away 1729069154d5SJulian Elischer * *** Idea.. store an ng_ID_t in each hook and use that 1730069154d5SJulian Elischer * instead of the direct hook in this crawl? 1731069154d5SJulian Elischer */ 1732069154d5SJulian Elischer oldnode = node; 173330400f03SJulian Elischer if ((node = NG_PEER_NODE(hook))) 173430400f03SJulian Elischer NG_NODE_REF(node); /* XXX RACE */ 173530400f03SJulian Elischer NG_NODE_UNREF(oldnode); /* XXX another race */ 173630400f03SJulian Elischer if (NG_NODE_NOT_VALID(node)) { 173730400f03SJulian Elischer NG_NODE_UNREF(node); /* XXX more races */ 1738069154d5SJulian Elischer node = NULL; 1739069154d5SJulian Elischer } 17404cf49a43SJulian Elischer } 17414cf49a43SJulian Elischer 17424cf49a43SJulian Elischer /* If node somehow missing, fail here (probably this is not needed) */ 17434cf49a43SJulian Elischer if (node == NULL) { 17446b795970SJulian Elischer TRAP_ERROR(); 17454cf49a43SJulian Elischer return (ENXIO); 17464cf49a43SJulian Elischer } 17474cf49a43SJulian Elischer 17484cf49a43SJulian Elischer /* Done */ 17494cf49a43SJulian Elischer *destp = node; 17501bdebe4dSArchie Cobbs if (lasthook != NULL) 175130400f03SJulian Elischer *lasthook = (hook ? NG_HOOK_PEER(hook) : NULL); 1752069154d5SJulian Elischer return (0); 1753069154d5SJulian Elischer } 1754069154d5SJulian Elischer 1755069154d5SJulian Elischer /***************************************************************\ 1756069154d5SJulian Elischer * Input queue handling. 1757069154d5SJulian Elischer * All activities are submitted to the node via the input queue 1758069154d5SJulian Elischer * which implements a multiple-reader/single-writer gate. 175964efc707SRobert Watson * Items which cannot be handled immediately are queued. 1760069154d5SJulian Elischer * 1761069154d5SJulian Elischer * read-write queue locking inline functions * 1762069154d5SJulian Elischer \***************************************************************/ 1763069154d5SJulian Elischer 1764714fb865SGleb Smirnoff static __inline item_p ng_dequeue(struct ng_queue * ngq, int *rw); 1765069154d5SJulian Elischer static __inline item_p ng_acquire_read(struct ng_queue * ngq, 1766069154d5SJulian Elischer item_p item); 1767069154d5SJulian Elischer static __inline item_p ng_acquire_write(struct ng_queue * ngq, 1768069154d5SJulian Elischer item_p item); 1769069154d5SJulian Elischer static __inline void ng_leave_read(struct ng_queue * ngq); 1770069154d5SJulian Elischer static __inline void ng_leave_write(struct ng_queue * ngq); 1771069154d5SJulian Elischer static __inline void ng_queue_rw(struct ng_queue * ngq, 1772069154d5SJulian Elischer item_p item, int rw); 1773069154d5SJulian Elischer 1774069154d5SJulian Elischer /* 1775069154d5SJulian Elischer * Definition of the bits fields in the ng_queue flag word. 1776069154d5SJulian Elischer * Defined here rather than in netgraph.h because no-one should fiddle 1777069154d5SJulian Elischer * with them. 1778069154d5SJulian Elischer * 1779b57a7965SJulian Elischer * The ordering here may be important! don't shuffle these. 1780069154d5SJulian Elischer */ 1781069154d5SJulian Elischer /*- 1782069154d5SJulian Elischer Safety Barrier--------+ (adjustable to suit taste) (not used yet) 1783069154d5SJulian Elischer | 1784069154d5SJulian Elischer V 1785069154d5SJulian Elischer +-------+-------+-------+-------+-------+-------+-------+-------+ 17864be59335SGleb Smirnoff | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 17874be59335SGleb Smirnoff | |A|c|t|i|v|e| |R|e|a|d|e|r| |C|o|u|n|t| | | | | | | | | |P|A| 17884be59335SGleb Smirnoff | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |O|W| 1789069154d5SJulian Elischer +-------+-------+-------+-------+-------+-------+-------+-------+ 17904be59335SGleb Smirnoff \___________________________ ____________________________/ | | 17914be59335SGleb Smirnoff V | | 17924be59335SGleb Smirnoff [active reader count] | | 1793069154d5SJulian Elischer | | 17944be59335SGleb Smirnoff Operation Pending -------------------------------+ | 1795069154d5SJulian Elischer | 17964be59335SGleb Smirnoff Active Writer ---------------------------------------+ 1797b57a7965SJulian Elischer 1798b57a7965SJulian Elischer 1799069154d5SJulian Elischer */ 18004be59335SGleb Smirnoff #define WRITER_ACTIVE 0x00000001 18014be59335SGleb Smirnoff #define OP_PENDING 0x00000002 18024be59335SGleb Smirnoff #define READER_INCREMENT 0x00000004 18034be59335SGleb Smirnoff #define READER_MASK 0xfffffffc /* Not valid if WRITER_ACTIVE is set */ 18044be59335SGleb Smirnoff #define SAFETY_BARRIER 0x00100000 /* 128K items queued should be enough */ 1805b57a7965SJulian Elischer 1806b57a7965SJulian Elischer /* Defines of more elaborate states on the queue */ 18074be59335SGleb Smirnoff /* Mask of bits a new read cares about */ 18084be59335SGleb Smirnoff #define NGQ_RMASK (WRITER_ACTIVE|OP_PENDING) 1809b57a7965SJulian Elischer 18104be59335SGleb Smirnoff /* Mask of bits a new write cares about */ 1811b57a7965SJulian Elischer #define NGQ_WMASK (NGQ_RMASK|READER_MASK) 1812b57a7965SJulian Elischer 18134be59335SGleb Smirnoff /* Test to decide if there is something on the queue. */ 18144be59335SGleb Smirnoff #define QUEUE_ACTIVE(QP) ((QP)->q_flags & OP_PENDING) 18154be59335SGleb Smirnoff 18164be59335SGleb Smirnoff /* How to decide what the next queued item is. */ 18174be59335SGleb Smirnoff #define HEAD_IS_READER(QP) NGI_QUEUED_READER((QP)->queue) 18184be59335SGleb Smirnoff #define HEAD_IS_WRITER(QP) NGI_QUEUED_WRITER((QP)->queue) /* notused */ 18194be59335SGleb Smirnoff 18204be59335SGleb Smirnoff /* Read the status to decide if the next item on the queue can now run. */ 18214be59335SGleb Smirnoff #define QUEUED_READER_CAN_PROCEED(QP) \ 18224be59335SGleb Smirnoff (((QP)->q_flags & (NGQ_RMASK & ~OP_PENDING)) == 0) 18234be59335SGleb Smirnoff #define QUEUED_WRITER_CAN_PROCEED(QP) \ 18244be59335SGleb Smirnoff (((QP)->q_flags & (NGQ_WMASK & ~OP_PENDING)) == 0) 1825b57a7965SJulian Elischer 1826b57a7965SJulian Elischer /* Is there a chance of getting ANY work off the queue? */ 18274be59335SGleb Smirnoff #define NEXT_QUEUED_ITEM_CAN_PROCEED(QP) \ 18284be59335SGleb Smirnoff (QUEUE_ACTIVE(QP) && \ 18294be59335SGleb Smirnoff ((HEAD_IS_READER(QP)) ? QUEUED_READER_CAN_PROCEED(QP) : \ 18304be59335SGleb Smirnoff QUEUED_WRITER_CAN_PROCEED(QP))) 18314be59335SGleb Smirnoff 1832b57a7965SJulian Elischer 1833714fb865SGleb Smirnoff #define NGQRW_R 0 1834714fb865SGleb Smirnoff #define NGQRW_W 1 1835714fb865SGleb Smirnoff 1836069154d5SJulian Elischer /* 1837069154d5SJulian Elischer * Taking into account the current state of the queue and node, possibly take 1838069154d5SJulian Elischer * the next entry off the queue and return it. Return NULL if there was 1839069154d5SJulian Elischer * nothing we could return, either because there really was nothing there, or 1840069154d5SJulian Elischer * because the node was in a state where it cannot yet process the next item 1841069154d5SJulian Elischer * on the queue. 1842069154d5SJulian Elischer * 1843069154d5SJulian Elischer * This MUST MUST MUST be called with the mutex held. 1844069154d5SJulian Elischer */ 1845069154d5SJulian Elischer static __inline item_p 1846714fb865SGleb Smirnoff ng_dequeue(struct ng_queue *ngq, int *rw) 1847069154d5SJulian Elischer { 1848069154d5SJulian Elischer item_p item; 1849069154d5SJulian Elischer u_int add_arg; 1850b57a7965SJulian Elischer 1851d58e678fSGleb Smirnoff mtx_assert(&ngq->q_mtx, MA_OWNED); 18524be59335SGleb Smirnoff /* 18534be59335SGleb Smirnoff * If there is nothing queued, then just return. 18544be59335SGleb Smirnoff * No point in continuing. 18554be59335SGleb Smirnoff * XXXGL: assert this? 18564be59335SGleb Smirnoff */ 18574be59335SGleb Smirnoff if (!QUEUE_ACTIVE(ngq)) { 18582955ee18SGleb Smirnoff CTR4(KTR_NET, "%20s: node [%x] (%p) queue empty; " 18592955ee18SGleb Smirnoff "queue flags 0x%lx", __func__, 18602955ee18SGleb Smirnoff ngq->q_node->nd_ID, ngq->q_node, ngq->q_flags); 18614be59335SGleb Smirnoff return (NULL); 18624be59335SGleb Smirnoff } 1863d58e678fSGleb Smirnoff 18644be59335SGleb Smirnoff /* 18654be59335SGleb Smirnoff * From here, we can assume there is a head item. 18664be59335SGleb Smirnoff * We need to find out what it is and if it can be dequeued, given 18674be59335SGleb Smirnoff * the current state of the node. 18684be59335SGleb Smirnoff */ 18694be59335SGleb Smirnoff if (HEAD_IS_READER(ngq)) { 18704be59335SGleb Smirnoff if (!QUEUED_READER_CAN_PROCEED(ngq)) { 18714be59335SGleb Smirnoff /* 18724be59335SGleb Smirnoff * It's a reader but we can't use it. 18734be59335SGleb Smirnoff * We are stalled so make sure we don't 18744be59335SGleb Smirnoff * get called again until something changes. 18754be59335SGleb Smirnoff */ 18764be59335SGleb Smirnoff ng_worklist_remove(ngq->q_node); 18772955ee18SGleb Smirnoff CTR4(KTR_NET, "%20s: node [%x] (%p) queued reader " 18782955ee18SGleb Smirnoff "can't proceed; queue flags 0x%lx", __func__, 18792955ee18SGleb Smirnoff ngq->q_node->nd_ID, ngq->q_node, ngq->q_flags); 18804be59335SGleb Smirnoff return (NULL); 18814be59335SGleb Smirnoff } 1882069154d5SJulian Elischer /* 1883b57a7965SJulian Elischer * Head of queue is a reader and we have no write active. 1884b57a7965SJulian Elischer * We don't care how many readers are already active. 18854be59335SGleb Smirnoff * Add the correct increment for the reader count. 1886069154d5SJulian Elischer */ 18874be59335SGleb Smirnoff add_arg = READER_INCREMENT; 1888714fb865SGleb Smirnoff *rw = NGQRW_R; 18894be59335SGleb Smirnoff } else if (QUEUED_WRITER_CAN_PROCEED(ngq)) { 1890069154d5SJulian Elischer /* 1891069154d5SJulian Elischer * There is a pending write, no readers and no active writer. 1892069154d5SJulian Elischer * This means we can go ahead with the pending writer. Note 1893069154d5SJulian Elischer * the fact that we now have a writer, ready for when we take 1894069154d5SJulian Elischer * it off the queue. 1895069154d5SJulian Elischer * 1896069154d5SJulian Elischer * We don't need to worry about a possible collision with the 1897069154d5SJulian Elischer * fasttrack reader. 1898069154d5SJulian Elischer * 1899069154d5SJulian Elischer * The fasttrack thread may take a long time to discover that we 1900069154d5SJulian Elischer * are running so we would have an inconsistent state in the 1901069154d5SJulian Elischer * flags for a while. Since we ignore the reader count 1902069154d5SJulian Elischer * entirely when the WRITER_ACTIVE flag is set, this should 1903069154d5SJulian Elischer * not matter (in fact it is defined that way). If it tests 19044be59335SGleb Smirnoff * the flag before this operation, the OP_PENDING flag 1905069154d5SJulian Elischer * will make it fail, and if it tests it later, the 1906b57a7965SJulian Elischer * WRITER_ACTIVE flag will do the same. If it is SO slow that 1907069154d5SJulian Elischer * we have actually completed the operation, and neither flag 19084be59335SGleb Smirnoff * is set by the time that it tests the flags, then it is 19094be59335SGleb Smirnoff * actually ok for it to continue. If it completes and we've 19104be59335SGleb Smirnoff * finished and the read pending is set it still fails. 1911069154d5SJulian Elischer * 1912069154d5SJulian Elischer * So we can just ignore it, as long as we can ensure that the 1913069154d5SJulian Elischer * transition from WRITE_PENDING state to the WRITER_ACTIVE 1914069154d5SJulian Elischer * state is atomic. 1915069154d5SJulian Elischer * 1916069154d5SJulian Elischer * After failing, first it will be held back by the mutex, then 1917069154d5SJulian Elischer * when it can proceed, it will queue its request, then it 1918069154d5SJulian Elischer * would arrive at this function. Usually it will have to 1919b57a7965SJulian Elischer * leave empty handed because the ACTIVE WRITER bit will be 1920069154d5SJulian Elischer * set. 1921b57a7965SJulian Elischer * 19224be59335SGleb Smirnoff * Adjust the flags for the new active writer. 1923069154d5SJulian Elischer */ 19244be59335SGleb Smirnoff add_arg = WRITER_ACTIVE; 1925714fb865SGleb Smirnoff *rw = NGQRW_W; 1926069154d5SJulian Elischer /* 1927069154d5SJulian Elischer * We want to write "active writer, no readers " Now go make 1928069154d5SJulian Elischer * it true. In fact there may be a number in the readers 1929069154d5SJulian Elischer * count but we know it is not true and will be fixed soon. 1930069154d5SJulian Elischer * We will fix the flags for the next pending entry in a 1931069154d5SJulian Elischer * moment. 1932069154d5SJulian Elischer */ 1933069154d5SJulian Elischer } else { 1934069154d5SJulian Elischer /* 1935069154d5SJulian Elischer * We can't dequeue anything.. return and say so. Probably we 1936069154d5SJulian Elischer * have a write pending and the readers count is non zero. If 1937069154d5SJulian Elischer * we got here because a reader hit us just at the wrong 1938069154d5SJulian Elischer * moment with the fasttrack code, and put us in a strange 19394be59335SGleb Smirnoff * state, then it will be coming through in just a moment, 19404be59335SGleb Smirnoff * (just as soon as we release the mutex) and keep things 19414be59335SGleb Smirnoff * moving. 19424be59335SGleb Smirnoff * Make sure we remove ourselves from the work queue. It 19434be59335SGleb Smirnoff * would be a waste of effort to do all this again. 1944069154d5SJulian Elischer */ 1945b57a7965SJulian Elischer ng_worklist_remove(ngq->q_node); 19462955ee18SGleb Smirnoff CTR4(KTR_NET, "%20s: node [%x] (%p) can't dequeue anything; " 19472955ee18SGleb Smirnoff "queue flags 0x%lx", __func__, 19482955ee18SGleb Smirnoff ngq->q_node->nd_ID, ngq->q_node, ngq->q_flags); 19494be59335SGleb Smirnoff return (NULL); 19504cf49a43SJulian Elischer } 19514cf49a43SJulian Elischer 19524cf49a43SJulian Elischer /* 1953069154d5SJulian Elischer * Now we dequeue the request (whatever it may be) and correct the 1954069154d5SJulian Elischer * pending flags and the next and last pointers. 19554cf49a43SJulian Elischer */ 1956069154d5SJulian Elischer item = ngq->queue; 1957069154d5SJulian Elischer ngq->queue = item->el_next; 19582955ee18SGleb Smirnoff CTR6(KTR_NET, "%20s: node [%x] (%p) dequeued item %p with flags 0x%lx; " 19592955ee18SGleb Smirnoff "queue flags 0x%lx", __func__, 19602955ee18SGleb Smirnoff ngq->q_node->nd_ID,ngq->q_node, item, item->el_flags, ngq->q_flags); 1961069154d5SJulian Elischer if (ngq->last == &(item->el_next)) { 19624cf49a43SJulian Elischer /* 1963069154d5SJulian Elischer * that was the last entry in the queue so set the 'last 19644be59335SGleb Smirnoff * pointer up correctly and make sure the pending flag is 1965069154d5SJulian Elischer * clear. 19664cf49a43SJulian Elischer */ 19674be59335SGleb Smirnoff add_arg += -OP_PENDING; 1968069154d5SJulian Elischer ngq->last = &(ngq->queue); 1969859a4d16SJulian Elischer /* 1970b57a7965SJulian Elischer * Whatever flag was set will be cleared and 1971b57a7965SJulian Elischer * the new acive field will be set by the add as well, 1972b57a7965SJulian Elischer * so we don't need to change add_arg. 1973b57a7965SJulian Elischer * But we know we don't need to be on the work list. 1974859a4d16SJulian Elischer */ 1975b57a7965SJulian Elischer atomic_add_long(&ngq->q_flags, add_arg); 1976b57a7965SJulian Elischer ng_worklist_remove(ngq->q_node); 1977859a4d16SJulian Elischer } else { 1978069154d5SJulian Elischer /* 19794be59335SGleb Smirnoff * Since there is still something on the queue 19804be59335SGleb Smirnoff * we don't need to change the PENDING flag. 1981069154d5SJulian Elischer */ 1982069154d5SJulian Elischer atomic_add_long(&ngq->q_flags, add_arg); 1983069154d5SJulian Elischer /* 1984b57a7965SJulian Elischer * If we see more doable work, make sure we are 1985b57a7965SJulian Elischer * on the work queue. 1986b57a7965SJulian Elischer */ 19874be59335SGleb Smirnoff if (NEXT_QUEUED_ITEM_CAN_PROCEED(ngq)) { 1988b57a7965SJulian Elischer ng_setisr(ngq->q_node); 1989b57a7965SJulian Elischer } 1990b57a7965SJulian Elischer } 19912955ee18SGleb Smirnoff CTR6(KTR_NET, "%20s: node [%x] (%p) returning item %p as %s; " 19922955ee18SGleb Smirnoff "queue flags 0x%lx", __func__, 19932955ee18SGleb Smirnoff ngq->q_node->nd_ID, ngq->q_node, item, *rw ? "WRITER" : "READER" , 19943b33fbe7SGleb Smirnoff ngq->q_flags); 1995069154d5SJulian Elischer return (item); 1996069154d5SJulian Elischer } 1997859a4d16SJulian Elischer 1998859a4d16SJulian Elischer /* 1999069154d5SJulian Elischer * Queue a packet to be picked up by someone else. 2000069154d5SJulian Elischer * We really don't care who, but we can't or don't want to hang around 2001069154d5SJulian Elischer * to process it ourselves. We are probably an interrupt routine.. 20024be59335SGleb Smirnoff * If the queue could be run, flag the netisr handler to start. 2003859a4d16SJulian Elischer */ 2004069154d5SJulian Elischer static __inline void 2005069154d5SJulian Elischer ng_queue_rw(struct ng_queue * ngq, item_p item, int rw) 2006069154d5SJulian Elischer { 2007d58e678fSGleb Smirnoff mtx_assert(&ngq->q_mtx, MA_OWNED); 2008d58e678fSGleb Smirnoff 20094be59335SGleb Smirnoff if (rw == NGQRW_W) 20104be59335SGleb Smirnoff NGI_SET_WRITER(item); 20114be59335SGleb Smirnoff else 20124be59335SGleb Smirnoff NGI_SET_READER(item); 2013069154d5SJulian Elischer item->el_next = NULL; /* maybe not needed */ 2014069154d5SJulian Elischer *ngq->last = item; 20152955ee18SGleb Smirnoff CTR5(KTR_NET, "%20s: node [%x] (%p) queued item %p as %s", __func__, 20162955ee18SGleb Smirnoff ngq->q_node->nd_ID, ngq->q_node, item, rw ? "WRITER" : "READER" ); 2017069154d5SJulian Elischer /* 2018069154d5SJulian Elischer * If it was the first item in the queue then we need to 2019069154d5SJulian Elischer * set the last pointer and the type flags. 2020069154d5SJulian Elischer */ 20213b33fbe7SGleb Smirnoff if (ngq->last == &(ngq->queue)) { 20224be59335SGleb Smirnoff atomic_add_long(&ngq->q_flags, OP_PENDING); 20232955ee18SGleb Smirnoff CTR3(KTR_NET, "%20s: node [%x] (%p) set OP_PENDING", __func__, 20242955ee18SGleb Smirnoff ngq->q_node->nd_ID, ngq->q_node); 20253b33fbe7SGleb Smirnoff } 20264be59335SGleb Smirnoff 2027069154d5SJulian Elischer ngq->last = &(item->el_next); 20284be59335SGleb Smirnoff /* 20294be59335SGleb Smirnoff * We can take the worklist lock with the node locked 20304be59335SGleb Smirnoff * BUT NOT THE REVERSE! 20314be59335SGleb Smirnoff */ 20324be59335SGleb Smirnoff if (NEXT_QUEUED_ITEM_CAN_PROCEED(ngq)) 20334be59335SGleb Smirnoff ng_setisr(ngq->q_node); 2034069154d5SJulian Elischer } 2035069154d5SJulian Elischer 2036069154d5SJulian Elischer 2037069154d5SJulian Elischer /* 2038069154d5SJulian Elischer * This function 'cheats' in that it first tries to 'grab' the use of the 2039069154d5SJulian Elischer * node, without going through the mutex. We can do this becasue of the 2040069154d5SJulian Elischer * semantics of the lock. The semantics include a clause that says that the 2041069154d5SJulian Elischer * value of the readers count is invalid if the WRITER_ACTIVE flag is set. It 2042069154d5SJulian Elischer * also says that the WRITER_ACTIVE flag cannot be set if the readers count 2043069154d5SJulian Elischer * is not zero. Note that this talks about what is valid to SET the 2044069154d5SJulian Elischer * WRITER_ACTIVE flag, because from the moment it is set, the value if the 2045069154d5SJulian Elischer * reader count is immaterial, and not valid. The two 'pending' flags have a 2046069154d5SJulian Elischer * similar effect, in that If they are orthogonal to the two active fields in 2047069154d5SJulian Elischer * how they are set, but if either is set, the attempted 'grab' need to be 2048069154d5SJulian Elischer * backed out because there is earlier work, and we maintain ordering in the 2049069154d5SJulian Elischer * queue. The result of this is that the reader request can try obtain use of 2050069154d5SJulian Elischer * the node with only a single atomic addition, and without any of the mutex 2051069154d5SJulian Elischer * overhead. If this fails the operation degenerates to the same as for other 2052069154d5SJulian Elischer * cases. 2053069154d5SJulian Elischer * 2054069154d5SJulian Elischer */ 2055069154d5SJulian Elischer static __inline item_p 2056069154d5SJulian Elischer ng_acquire_read(struct ng_queue *ngq, item_p item) 2057069154d5SJulian Elischer { 2058ac5dd141SGleb Smirnoff KASSERT(ngq != &ng_deadnode.nd_input_queue, 2059ac5dd141SGleb Smirnoff ("%s: working on deadnode", __func__)); 2060069154d5SJulian Elischer 2061069154d5SJulian Elischer /* ######### Hack alert ######### */ 2062069154d5SJulian Elischer atomic_add_long(&ngq->q_flags, READER_INCREMENT); 2063b57a7965SJulian Elischer if ((ngq->q_flags & NGQ_RMASK) == 0) { 2064069154d5SJulian Elischer /* Successfully grabbed node */ 20652955ee18SGleb Smirnoff CTR4(KTR_NET, "%20s: node [%x] (%p) fast acquired item %p", 20662955ee18SGleb Smirnoff __func__, ngq->q_node->nd_ID, ngq->q_node, item); 2067069154d5SJulian Elischer return (item); 2068069154d5SJulian Elischer } 2069069154d5SJulian Elischer /* undo the damage if we didn't succeed */ 2070069154d5SJulian Elischer atomic_subtract_long(&ngq->q_flags, READER_INCREMENT); 2071069154d5SJulian Elischer 2072069154d5SJulian Elischer /* ######### End Hack alert ######### */ 20732c8dda8dSWojciech A. Koszek NG_QUEUE_LOCK(ngq); 2074069154d5SJulian Elischer /* 2075069154d5SJulian Elischer * Try again. Another processor (or interrupt for that matter) may 2076069154d5SJulian Elischer * have removed the last queued item that was stopping us from 2077069154d5SJulian Elischer * running, between the previous test, and the moment that we took 2078069154d5SJulian Elischer * the mutex. (Or maybe a writer completed.) 20794be59335SGleb Smirnoff * Even if another fast-track reader hits during this period 20804be59335SGleb Smirnoff * we don't care as multiple readers is OK. 2081069154d5SJulian Elischer */ 2082b57a7965SJulian Elischer if ((ngq->q_flags & NGQ_RMASK) == 0) { 2083069154d5SJulian Elischer atomic_add_long(&ngq->q_flags, READER_INCREMENT); 20842c8dda8dSWojciech A. Koszek NG_QUEUE_UNLOCK(ngq); 20852955ee18SGleb Smirnoff CTR4(KTR_NET, "%20s: node [%x] (%p) slow acquired item %p", 20862955ee18SGleb Smirnoff __func__, ngq->q_node->nd_ID, ngq->q_node, item); 2087069154d5SJulian Elischer return (item); 2088069154d5SJulian Elischer } 2089069154d5SJulian Elischer 2090069154d5SJulian Elischer /* 2091069154d5SJulian Elischer * and queue the request for later. 2092069154d5SJulian Elischer */ 2093069154d5SJulian Elischer ng_queue_rw(ngq, item, NGQRW_R); 20942c8dda8dSWojciech A. Koszek NG_QUEUE_UNLOCK(ngq); 2095f912c0f7SGleb Smirnoff 2096f912c0f7SGleb Smirnoff return (NULL); 2097069154d5SJulian Elischer } 2098069154d5SJulian Elischer 2099069154d5SJulian Elischer static __inline item_p 2100069154d5SJulian Elischer ng_acquire_write(struct ng_queue *ngq, item_p item) 2101069154d5SJulian Elischer { 2102ac5dd141SGleb Smirnoff KASSERT(ngq != &ng_deadnode.nd_input_queue, 2103ac5dd141SGleb Smirnoff ("%s: working on deadnode", __func__)); 2104ac5dd141SGleb Smirnoff 2105069154d5SJulian Elischer restart: 21062c8dda8dSWojciech A. Koszek NG_QUEUE_LOCK(ngq); 2107069154d5SJulian Elischer /* 2108069154d5SJulian Elischer * If there are no readers, no writer, and no pending packets, then 2109069154d5SJulian Elischer * we can just go ahead. In all other situations we need to queue the 2110069154d5SJulian Elischer * request 2111069154d5SJulian Elischer */ 2112b57a7965SJulian Elischer if ((ngq->q_flags & NGQ_WMASK) == 0) { 21134be59335SGleb Smirnoff /* collision could happen *HERE* */ 2114069154d5SJulian Elischer atomic_add_long(&ngq->q_flags, WRITER_ACTIVE); 21152c8dda8dSWojciech A. Koszek NG_QUEUE_UNLOCK(ngq); 2116069154d5SJulian Elischer if (ngq->q_flags & READER_MASK) { 2117069154d5SJulian Elischer /* Collision with fast-track reader */ 2118b57a7965SJulian Elischer atomic_subtract_long(&ngq->q_flags, WRITER_ACTIVE); 2119069154d5SJulian Elischer goto restart; 2120069154d5SJulian Elischer } 21212955ee18SGleb Smirnoff CTR4(KTR_NET, "%20s: node [%x] (%p) acquired item %p", 21222955ee18SGleb Smirnoff __func__, ngq->q_node->nd_ID, ngq->q_node, item); 2123069154d5SJulian Elischer return (item); 2124069154d5SJulian Elischer } 2125069154d5SJulian Elischer 2126069154d5SJulian Elischer /* 2127069154d5SJulian Elischer * and queue the request for later. 2128069154d5SJulian Elischer */ 2129069154d5SJulian Elischer ng_queue_rw(ngq, item, NGQRW_W); 21302c8dda8dSWojciech A. Koszek NG_QUEUE_UNLOCK(ngq); 2131f912c0f7SGleb Smirnoff 2132f912c0f7SGleb Smirnoff return (NULL); 2133069154d5SJulian Elischer } 2134069154d5SJulian Elischer 2135262dfd7fSJulian Elischer #if 0 2136262dfd7fSJulian Elischer static __inline item_p 2137262dfd7fSJulian Elischer ng_upgrade_write(struct ng_queue *ngq, item_p item) 2138262dfd7fSJulian Elischer { 2139262dfd7fSJulian Elischer KASSERT(ngq != &ng_deadnode.nd_input_queue, 2140262dfd7fSJulian Elischer ("%s: working on deadnode", __func__)); 2141262dfd7fSJulian Elischer 2142262dfd7fSJulian Elischer NGI_SET_WRITER(item); 2143262dfd7fSJulian Elischer 2144262dfd7fSJulian Elischer mtx_lock_spin(&(ngq->q_mtx)); 2145262dfd7fSJulian Elischer 2146262dfd7fSJulian Elischer /* 2147262dfd7fSJulian Elischer * There will never be no readers as we are there ourselves. 2148262dfd7fSJulian Elischer * Set the WRITER_ACTIVE flags ASAP to block out fast track readers. 2149262dfd7fSJulian Elischer * The caller we are running from will call ng_leave_read() 2150262dfd7fSJulian Elischer * soon, so we must account for that. We must leave again with the 2151262dfd7fSJulian Elischer * READER lock. If we find other readers, then 2152262dfd7fSJulian Elischer * queue the request for later. However "later" may be rignt now 2153262dfd7fSJulian Elischer * if there are no readers. We don't really care if there are queued 2154262dfd7fSJulian Elischer * items as we will bypass them anyhow. 2155262dfd7fSJulian Elischer */ 2156262dfd7fSJulian Elischer atomic_add_long(&ngq->q_flags, WRITER_ACTIVE - READER_INCREMENT); 2157262dfd7fSJulian Elischer if (ngq->q_flags & (NGQ_WMASK & ~OP_PENDING) == WRITER_ACTIVE) { 2158262dfd7fSJulian Elischer mtx_unlock_spin(&(ngq->q_mtx)); 2159262dfd7fSJulian Elischer 2160262dfd7fSJulian Elischer /* It's just us, act on the item. */ 2161262dfd7fSJulian Elischer /* will NOT drop writer lock when done */ 2162262dfd7fSJulian Elischer ng_apply_item(node, item, 0); 2163262dfd7fSJulian Elischer 2164262dfd7fSJulian Elischer /* 2165262dfd7fSJulian Elischer * Having acted on the item, atomically 2166262dfd7fSJulian Elischer * down grade back to READER and finish up 2167262dfd7fSJulian Elischer */ 2168262dfd7fSJulian Elischer atomic_add_long(&ngq->q_flags, 2169262dfd7fSJulian Elischer READER_INCREMENT - WRITER_ACTIVE); 2170262dfd7fSJulian Elischer 2171262dfd7fSJulian Elischer /* Our caller will call ng_leave_read() */ 2172262dfd7fSJulian Elischer return; 2173262dfd7fSJulian Elischer } 2174262dfd7fSJulian Elischer /* 2175262dfd7fSJulian Elischer * It's not just us active, so queue us AT THE HEAD. 2176262dfd7fSJulian Elischer * "Why?" I hear you ask. 2177262dfd7fSJulian Elischer * Put us at the head of the queue as we've already been 2178262dfd7fSJulian Elischer * through it once. If there is nothing else waiting, 2179262dfd7fSJulian Elischer * set the correct flags. 2180262dfd7fSJulian Elischer */ 2181262dfd7fSJulian Elischer if ((item->el_next = ngq->queue) == NULL) { 2182262dfd7fSJulian Elischer /* 2183262dfd7fSJulian Elischer * Set up the "last" pointer. 2184262dfd7fSJulian Elischer * We are the only (and thus last) item 2185262dfd7fSJulian Elischer */ 2186262dfd7fSJulian Elischer ngq->last = &(item->el_next); 2187262dfd7fSJulian Elischer 2188262dfd7fSJulian Elischer /* We've gone from, 0 to 1 item in the queue */ 2189262dfd7fSJulian Elischer atomic_add_long(&ngq->q_flags, OP_PENDING); 2190262dfd7fSJulian Elischer 2191262dfd7fSJulian Elischer CTR3(KTR_NET, "%20s: node [%x] (%p) set OP_PENDING", __func__, 2192262dfd7fSJulian Elischer ngq->q_node->nd_ID, ngq->q_node); 2193262dfd7fSJulian Elischer }; 2194262dfd7fSJulian Elischer ngq->queue = item; 2195262dfd7fSJulian Elischer CTR5(KTR_NET, "%20s: node [%x] (%p) requeued item %p as WRITER", 2196262dfd7fSJulian Elischer __func__, ngq->q_node->nd_ID, ngq->q_node, item ); 2197262dfd7fSJulian Elischer 2198262dfd7fSJulian Elischer /* Reverse what we did above. That downgrades us back to reader */ 2199262dfd7fSJulian Elischer atomic_add_long(&ngq->q_flags, READER_INCREMENT - WRITER_ACTIVE); 2200262dfd7fSJulian Elischer if (NEXT_QUEUED_ITEM_CAN_PROCEED(ngq)) 2201262dfd7fSJulian Elischer ng_setisr(ngq->q_node); 2202262dfd7fSJulian Elischer mtx_unlock_spin(&(ngq->q_mtx)); 2203262dfd7fSJulian Elischer 2204262dfd7fSJulian Elischer return; 2205262dfd7fSJulian Elischer } 2206262dfd7fSJulian Elischer 2207262dfd7fSJulian Elischer #endif 2208262dfd7fSJulian Elischer 2209069154d5SJulian Elischer static __inline void 2210069154d5SJulian Elischer ng_leave_read(struct ng_queue *ngq) 2211069154d5SJulian Elischer { 2212069154d5SJulian Elischer atomic_subtract_long(&ngq->q_flags, READER_INCREMENT); 2213069154d5SJulian Elischer } 2214069154d5SJulian Elischer 2215069154d5SJulian Elischer static __inline void 2216069154d5SJulian Elischer ng_leave_write(struct ng_queue *ngq) 2217069154d5SJulian Elischer { 2218069154d5SJulian Elischer atomic_subtract_long(&ngq->q_flags, WRITER_ACTIVE); 2219069154d5SJulian Elischer } 2220069154d5SJulian Elischer 2221069154d5SJulian Elischer static void 2222069154d5SJulian Elischer ng_flush_input_queue(struct ng_queue * ngq) 2223069154d5SJulian Elischer { 2224069154d5SJulian Elischer item_p item; 2225069154d5SJulian Elischer 22262c8dda8dSWojciech A. Koszek NG_QUEUE_LOCK(ngq); 22274be59335SGleb Smirnoff while (ngq->queue) { 2228069154d5SJulian Elischer item = ngq->queue; 2229069154d5SJulian Elischer ngq->queue = item->el_next; 2230069154d5SJulian Elischer if (ngq->last == &(item->el_next)) { 2231069154d5SJulian Elischer ngq->last = &(ngq->queue); 22324be59335SGleb Smirnoff atomic_add_long(&ngq->q_flags, -OP_PENDING); 2233069154d5SJulian Elischer } 22342c8dda8dSWojciech A. Koszek NG_QUEUE_UNLOCK(ngq); 22354be59335SGleb Smirnoff 22364be59335SGleb Smirnoff /* If the item is supplying a callback, call it with an error */ 2237e088dd4cSAlexander Motin if (item->apply != NULL && 2238e088dd4cSAlexander Motin refcount_release(&item->apply->refs)) { 2239e088dd4cSAlexander Motin (*item->apply->apply)(item->apply->context, ENOENT); 2240eb2405ddSGleb Smirnoff } 2241505fad52SJulian Elischer NG_FREE_ITEM(item); 22422c8dda8dSWojciech A. Koszek NG_QUEUE_LOCK(ngq); 2243069154d5SJulian Elischer } 2244b57a7965SJulian Elischer /* 2245b57a7965SJulian Elischer * Take us off the work queue if we are there. 224664efc707SRobert Watson * We definately have no work to be done. 2247b57a7965SJulian Elischer */ 2248b57a7965SJulian Elischer ng_worklist_remove(ngq->q_node); 22492c8dda8dSWojciech A. Koszek NG_QUEUE_UNLOCK(ngq); 2250069154d5SJulian Elischer } 2251069154d5SJulian Elischer 2252069154d5SJulian Elischer /*********************************************************************** 2253069154d5SJulian Elischer * Externally visible method for sending or queueing messages or data. 2254069154d5SJulian Elischer ***********************************************************************/ 2255069154d5SJulian Elischer 2256069154d5SJulian Elischer /* 22571acb27c6SJulian Elischer * The module code should have filled out the item correctly by this stage: 2258069154d5SJulian Elischer * Common: 2259069154d5SJulian Elischer * reference to destination node. 2260069154d5SJulian Elischer * Reference to destination rcv hook if relevant. 2261e088dd4cSAlexander Motin * apply pointer must be or NULL or reference valid struct ng_apply_info. 2262069154d5SJulian Elischer * Data: 2263069154d5SJulian Elischer * pointer to mbuf 2264069154d5SJulian Elischer * Control_Message: 2265069154d5SJulian Elischer * pointer to msg. 2266069154d5SJulian Elischer * ID of original sender node. (return address) 22671acb27c6SJulian Elischer * Function: 22681acb27c6SJulian Elischer * Function pointer 22691acb27c6SJulian Elischer * void * argument 22701acb27c6SJulian Elischer * integer argument 2271069154d5SJulian Elischer * 2272069154d5SJulian Elischer * The nodes have several routines and macros to help with this task: 2273069154d5SJulian Elischer */ 2274069154d5SJulian Elischer 2275069154d5SJulian Elischer int 227642282202SGleb Smirnoff ng_snd_item(item_p item, int flags) 2277069154d5SJulian Elischer { 2278e088dd4cSAlexander Motin hook_p hook; 2279e088dd4cSAlexander Motin node_p node; 228042282202SGleb Smirnoff int queue, rw; 2281e088dd4cSAlexander Motin struct ng_queue *ngq; 228232b33288SGleb Smirnoff int error = 0; 2283069154d5SJulian Elischer 2284069154d5SJulian Elischer if (item == NULL) { 22856b795970SJulian Elischer TRAP_ERROR(); 2286069154d5SJulian Elischer return (EINVAL); /* failed to get queue element */ 2287069154d5SJulian Elischer } 2288e088dd4cSAlexander Motin 2289e088dd4cSAlexander Motin #ifdef NETGRAPH_DEBUG 2290e088dd4cSAlexander Motin _ngi_check(item, __FILE__, __LINE__); 2291e088dd4cSAlexander Motin #endif 2292e088dd4cSAlexander Motin 2293e088dd4cSAlexander Motin if (item->apply) 2294e088dd4cSAlexander Motin refcount_acquire(&item->apply->refs); 2295e088dd4cSAlexander Motin 2296e088dd4cSAlexander Motin node = NGI_NODE(item); 2297b57a7965SJulian Elischer if (node == NULL) { 22986b795970SJulian Elischer TRAP_ERROR(); 2299e088dd4cSAlexander Motin ERROUT(EINVAL); /* No address */ 2300069154d5SJulian Elischer } 2301e088dd4cSAlexander Motin 2302e72a98f4SAlexander Motin hook = NGI_HOOK(item); 23036b795970SJulian Elischer switch(item->el_flags & NGQF_TYPE) { 23046b795970SJulian Elischer case NGQF_DATA: 2305069154d5SJulian Elischer /* 2306069154d5SJulian Elischer * DATA MESSAGE 2307069154d5SJulian Elischer * Delivered to a node via a non-optional hook. 2308069154d5SJulian Elischer * Both should be present in the item even though 2309069154d5SJulian Elischer * the node is derivable from the hook. 2310069154d5SJulian Elischer * References are held on both by the item. 2311069154d5SJulian Elischer */ 23121cf3fa79SJulian Elischer 23131cf3fa79SJulian Elischer /* Protect nodes from sending NULL pointers 23141cf3fa79SJulian Elischer * to each other 23151cf3fa79SJulian Elischer */ 231610204449SJulian Elischer if (NGI_M(item) == NULL) 2317e088dd4cSAlexander Motin ERROUT(EINVAL); 23181cf3fa79SJulian Elischer 2319069154d5SJulian Elischer CHECK_DATA_MBUF(NGI_M(item)); 2320069154d5SJulian Elischer if (hook == NULL) { 23216b795970SJulian Elischer TRAP_ERROR(); 2322e088dd4cSAlexander Motin ERROUT(EINVAL); 2323069154d5SJulian Elischer } 232430400f03SJulian Elischer if ((NG_HOOK_NOT_VALID(hook)) 232530400f03SJulian Elischer || (NG_NODE_NOT_VALID(NG_HOOK_NODE(hook)))) { 2326e088dd4cSAlexander Motin ERROUT(ENOTCONN); 2327859a4d16SJulian Elischer } 23286b795970SJulian Elischer break; 23296b795970SJulian Elischer case NGQF_MESG: 23304cf49a43SJulian Elischer /* 2331069154d5SJulian Elischer * CONTROL MESSAGE 2332069154d5SJulian Elischer * Delivered to a node. 2333069154d5SJulian Elischer * Hook is optional. 2334069154d5SJulian Elischer * References are held by the item on the node and 2335069154d5SJulian Elischer * the hook if it is present. 23364cf49a43SJulian Elischer */ 23376b795970SJulian Elischer break; 23386b795970SJulian Elischer case NGQF_FN: 2339e088dd4cSAlexander Motin case NGQF_FN2: 23406b795970SJulian Elischer break; 23416b795970SJulian Elischer default: 23426b795970SJulian Elischer TRAP_ERROR(); 2343e088dd4cSAlexander Motin ERROUT(EINVAL); 2344069154d5SJulian Elischer } 23456f683eeeSGleb Smirnoff switch(item->el_flags & NGQF_RW) { 23466f683eeeSGleb Smirnoff case NGQF_READER: 23476f683eeeSGleb Smirnoff rw = NGQRW_R; 23486f683eeeSGleb Smirnoff break; 23496f683eeeSGleb Smirnoff case NGQF_WRITER: 23506f683eeeSGleb Smirnoff rw = NGQRW_W; 23516f683eeeSGleb Smirnoff break; 23526f683eeeSGleb Smirnoff default: 23536f683eeeSGleb Smirnoff panic("%s: invalid item flags %lx", __func__, item->el_flags); 23546f683eeeSGleb Smirnoff } 23556f683eeeSGleb Smirnoff 2356069154d5SJulian Elischer /* 23576f683eeeSGleb Smirnoff * If the node specifies single threading, force writer semantics. 23586f683eeeSGleb Smirnoff * Similarly, the node may say one hook always produces writers. 2359714fb865SGleb Smirnoff * These are overrides. 2360069154d5SJulian Elischer */ 2361be4252b3SJulian Elischer if ((node->nd_flags & NGF_FORCE_WRITER) 2362714fb865SGleb Smirnoff || (hook && (hook->hk_flags & HK_FORCE_WRITER))) 2363069154d5SJulian Elischer rw = NGQRW_W; 23646f683eeeSGleb Smirnoff 236581a253a4SAlexander Motin /* 236681a253a4SAlexander Motin * If sender or receiver requests queued delivery or stack usage 236781a253a4SAlexander Motin * level is dangerous - enqueue message. 236881a253a4SAlexander Motin */ 236981a253a4SAlexander Motin queue = 0; 237081a253a4SAlexander Motin if ((flags & NG_QUEUE) || (hook && (hook->hk_flags & HK_QUEUE))) { 237181a253a4SAlexander Motin queue = 1; 237281a253a4SAlexander Motin } 237381a253a4SAlexander Motin #ifdef GET_STACK_USAGE 237481a253a4SAlexander Motin else { 2375d4529f98SAlexander Motin /* 2376942fe01fSDmitry Morozovsky * Most of netgraph nodes have small stack consumption and 2377942fe01fSDmitry Morozovsky * for them 20% of free stack space is more than enough. 2378d4529f98SAlexander Motin * Nodes/hooks with higher stack usage should be marked as 2379942fe01fSDmitry Morozovsky * HI_STACK. For them 50% of stack will be guarantied then. 2380d4529f98SAlexander Motin * XXX: Values 50% (64/128) and 80% (100/128) are completely 2381d4529f98SAlexander Motin * empirical. 2382d4529f98SAlexander Motin */ 238381a253a4SAlexander Motin size_t st, su; 238481a253a4SAlexander Motin GET_STACK_USAGE(st, su); 238581a253a4SAlexander Motin su = (su * 128) / st; 238681a253a4SAlexander Motin if ((su > 100) || 238781a253a4SAlexander Motin ((su > 64) && ((node->nd_flags & NGF_HI_STACK) || 238881a253a4SAlexander Motin (hook && (hook->hk_flags & HK_HI_STACK))))) { 238981a253a4SAlexander Motin queue = 1; 239081a253a4SAlexander Motin } 239181a253a4SAlexander Motin } 239281a253a4SAlexander Motin #endif 239381a253a4SAlexander Motin 2394e72a98f4SAlexander Motin ngq = &node->nd_input_queue; 2395069154d5SJulian Elischer if (queue) { 2396069154d5SJulian Elischer /* Put it on the queue for that node*/ 239730400f03SJulian Elischer #ifdef NETGRAPH_DEBUG 2398069154d5SJulian Elischer _ngi_check(item, __FILE__, __LINE__); 2399069154d5SJulian Elischer #endif 24002c8dda8dSWojciech A. Koszek NG_QUEUE_LOCK(ngq); 2401069154d5SJulian Elischer ng_queue_rw(ngq, item, rw); 24022c8dda8dSWojciech A. Koszek NG_QUEUE_UNLOCK(ngq); 24038afe16d5SGleb Smirnoff 24048afe16d5SGleb Smirnoff if (flags & NG_PROGRESS) 24058afe16d5SGleb Smirnoff return (EINPROGRESS); 24068afe16d5SGleb Smirnoff else 2407069154d5SJulian Elischer return (0); 2408069154d5SJulian Elischer } 2409069154d5SJulian Elischer 2410069154d5SJulian Elischer /* 2411069154d5SJulian Elischer * We already decided how we will be queueud or treated. 2412069154d5SJulian Elischer * Try get the appropriate operating permission. 2413069154d5SJulian Elischer */ 241432b33288SGleb Smirnoff if (rw == NGQRW_R) 2415069154d5SJulian Elischer item = ng_acquire_read(ngq, item); 241632b33288SGleb Smirnoff else 2417069154d5SJulian Elischer item = ng_acquire_write(ngq, item); 24184cf49a43SJulian Elischer 24194be59335SGleb Smirnoff 2420069154d5SJulian Elischer if (item == NULL) { 24218afe16d5SGleb Smirnoff if (flags & NG_PROGRESS) 24228afe16d5SGleb Smirnoff return (EINPROGRESS); 24238afe16d5SGleb Smirnoff else 2424069154d5SJulian Elischer return (0); 2425069154d5SJulian Elischer } 2426069154d5SJulian Elischer 242730400f03SJulian Elischer #ifdef NETGRAPH_DEBUG 2428069154d5SJulian Elischer _ngi_check(item, __FILE__, __LINE__); 2429069154d5SJulian Elischer #endif 24304be59335SGleb Smirnoff 24315951069aSJulian Elischer NGI_GET_NODE(item, node); /* zaps stored node */ 24325951069aSJulian Elischer 243327757487SGleb Smirnoff error = ng_apply_item(node, item, rw); /* drops r/w lock when done */ 2434069154d5SJulian Elischer 2435069154d5SJulian Elischer /* 24365951069aSJulian Elischer * If the node goes away when we remove the reference, 2437376958b4SBrian Somers * whatever we just did caused it.. whatever we do, DO NOT 24385951069aSJulian Elischer * access the node again! 24395951069aSJulian Elischer */ 24405951069aSJulian Elischer if (NG_NODE_UNREF(node) == 0) { 24415951069aSJulian Elischer return (error); 24425951069aSJulian Elischer } 24435951069aSJulian Elischer 24442c8dda8dSWojciech A. Koszek NG_QUEUE_LOCK(ngq); 24454be59335SGleb Smirnoff if (NEXT_QUEUED_ITEM_CAN_PROCEED(ngq)) 2446f912c0f7SGleb Smirnoff ng_setisr(ngq->q_node); 24472c8dda8dSWojciech A. Koszek NG_QUEUE_UNLOCK(ngq); 2448069154d5SJulian Elischer 24491acb27c6SJulian Elischer return (error); 2450e088dd4cSAlexander Motin 2451e088dd4cSAlexander Motin done: 2452e088dd4cSAlexander Motin /* Apply callback. */ 2453e72a98f4SAlexander Motin if (item->apply != NULL && refcount_release(&item->apply->refs)) 2454e088dd4cSAlexander Motin (*item->apply->apply)(item->apply->context, error); 2455e72a98f4SAlexander Motin 2456e088dd4cSAlexander Motin NG_FREE_ITEM(item); 2457e088dd4cSAlexander Motin return (error); 2458069154d5SJulian Elischer } 2459069154d5SJulian Elischer 2460069154d5SJulian Elischer /* 2461069154d5SJulian Elischer * We have an item that was possibly queued somewhere. 2462069154d5SJulian Elischer * It should contain all the information needed 2463069154d5SJulian Elischer * to run it on the appropriate node/hook. 2464e088dd4cSAlexander Motin * If there is apply pointer and we own the last reference, call apply(). 24654cf49a43SJulian Elischer */ 246627757487SGleb Smirnoff static int 2467714fb865SGleb Smirnoff ng_apply_item(node_p node, item_p item, int rw) 2468069154d5SJulian Elischer { 2469069154d5SJulian Elischer hook_p hook; 2470a54a69d7SJulian Elischer int error = 0; 2471069154d5SJulian Elischer ng_rcvdata_t *rcvdata; 2472c4b5eea4SJulian Elischer ng_rcvmsg_t *rcvmsg; 2473e088dd4cSAlexander Motin struct ng_apply_info *apply; 2474069154d5SJulian Elischer 24751acb27c6SJulian Elischer NGI_GET_HOOK(item, hook); /* clears stored hook */ 247630400f03SJulian Elischer #ifdef NETGRAPH_DEBUG 2477069154d5SJulian Elischer _ngi_check(item, __FILE__, __LINE__); 2478069154d5SJulian Elischer #endif 24798afe16d5SGleb Smirnoff 24808afe16d5SGleb Smirnoff apply = item->apply; 24818afe16d5SGleb Smirnoff 24826b795970SJulian Elischer switch (item->el_flags & NGQF_TYPE) { 2483069154d5SJulian Elischer case NGQF_DATA: 2484069154d5SJulian Elischer /* 2485069154d5SJulian Elischer * Check things are still ok as when we were queued. 2486069154d5SJulian Elischer */ 2487069154d5SJulian Elischer if ((hook == NULL) 248830400f03SJulian Elischer || NG_HOOK_NOT_VALID(hook) 2489c4b5eea4SJulian Elischer || NG_NODE_NOT_VALID(node) ) { 2490a54a69d7SJulian Elischer error = EIO; 2491069154d5SJulian Elischer NG_FREE_ITEM(item); 2492c4b5eea4SJulian Elischer break; 2493069154d5SJulian Elischer } 2494c4b5eea4SJulian Elischer /* 2495c4b5eea4SJulian Elischer * If no receive method, just silently drop it. 2496c4b5eea4SJulian Elischer * Give preference to the hook over-ride method 2497c4b5eea4SJulian Elischer */ 2498c4b5eea4SJulian Elischer if ((!(rcvdata = hook->hk_rcvdata)) 2499c4b5eea4SJulian Elischer && (!(rcvdata = NG_HOOK_NODE(hook)->nd_type->rcvdata))) { 2500c4b5eea4SJulian Elischer error = 0; 2501c4b5eea4SJulian Elischer NG_FREE_ITEM(item); 2502c4b5eea4SJulian Elischer break; 2503c4b5eea4SJulian Elischer } 2504a54a69d7SJulian Elischer error = (*rcvdata)(hook, item); 2505069154d5SJulian Elischer break; 2506069154d5SJulian Elischer case NGQF_MESG: 2507e72a98f4SAlexander Motin if (hook && NG_HOOK_NOT_VALID(hook)) { 2508069154d5SJulian Elischer /* 2509e72a98f4SAlexander Motin * The hook has been zapped then we can't use it. 2510e72a98f4SAlexander Motin * Immediately drop its reference. 2511069154d5SJulian Elischer * The message may not need it. 2512069154d5SJulian Elischer */ 251330400f03SJulian Elischer NG_HOOK_UNREF(hook); 2514069154d5SJulian Elischer hook = NULL; 2515069154d5SJulian Elischer } 2516069154d5SJulian Elischer /* 2517069154d5SJulian Elischer * Similarly, if the node is a zombie there is 2518069154d5SJulian Elischer * nothing we can do with it, drop everything. 2519069154d5SJulian Elischer */ 252030400f03SJulian Elischer if (NG_NODE_NOT_VALID(node)) { 25216b795970SJulian Elischer TRAP_ERROR(); 2522a54a69d7SJulian Elischer error = EINVAL; 2523069154d5SJulian Elischer NG_FREE_ITEM(item); 2524e72a98f4SAlexander Motin break; 2525e72a98f4SAlexander Motin } 2526069154d5SJulian Elischer /* 2527069154d5SJulian Elischer * Call the appropriate message handler for the object. 2528069154d5SJulian Elischer * It is up to the message handler to free the message. 2529069154d5SJulian Elischer * If it's a generic message, handle it generically, 2530e72a98f4SAlexander Motin * otherwise call the type's message handler (if it exists). 2531069154d5SJulian Elischer * XXX (race). Remember that a queued message may 2532069154d5SJulian Elischer * reference a node or hook that has just been 2533069154d5SJulian Elischer * invalidated. It will exist as the queue code 2534069154d5SJulian Elischer * is holding a reference, but.. 2535069154d5SJulian Elischer */ 2536e72a98f4SAlexander Motin if ((NGI_MSG(item)->header.typecookie == NGM_GENERIC_COOKIE) && 2537e72a98f4SAlexander Motin ((NGI_MSG(item)->header.flags & NGF_RESP) == 0)) { 2538a54a69d7SJulian Elischer error = ng_generic_msg(node, item, hook); 2539c4b5eea4SJulian Elischer break; 2540c4b5eea4SJulian Elischer } 2541e72a98f4SAlexander Motin if (((!hook) || (!(rcvmsg = hook->hk_rcvmsg))) && 2542e72a98f4SAlexander Motin (!(rcvmsg = node->nd_type->rcvmsg))) { 25436b795970SJulian Elischer TRAP_ERROR(); 2544a54a69d7SJulian Elischer error = 0; 2545069154d5SJulian Elischer NG_FREE_ITEM(item); 2546c4b5eea4SJulian Elischer break; 2547069154d5SJulian Elischer } 2548a54a69d7SJulian Elischer error = (*rcvmsg)(node, item, hook); 2549069154d5SJulian Elischer break; 25506b795970SJulian Elischer case NGQF_FN: 2551e088dd4cSAlexander Motin case NGQF_FN2: 2552e088dd4cSAlexander Motin /* 2553e088dd4cSAlexander Motin * We have to implicitly trust the hook, 2554e088dd4cSAlexander Motin * as some of these are used for system purposes 2555e088dd4cSAlexander Motin * where the hook is invalid. In the case of 2556e088dd4cSAlexander Motin * the shutdown message we allow it to hit 2557e088dd4cSAlexander Motin * even if the node is invalid. 2558e088dd4cSAlexander Motin */ 2559e088dd4cSAlexander Motin if ((NG_NODE_NOT_VALID(node)) 2560e088dd4cSAlexander Motin && (NGI_FN(item) != &ng_rmnode)) { 2561e088dd4cSAlexander Motin TRAP_ERROR(); 2562e088dd4cSAlexander Motin error = EINVAL; 2563e088dd4cSAlexander Motin NG_FREE_ITEM(item); 2564e088dd4cSAlexander Motin break; 2565e088dd4cSAlexander Motin } 2566b332b91fSGleb Smirnoff if ((item->el_flags & NGQF_TYPE) == NGQF_FN) { 2567b332b91fSGleb Smirnoff (*NGI_FN(item))(node, hook, NGI_ARG1(item), 2568b332b91fSGleb Smirnoff NGI_ARG2(item)); 2569b332b91fSGleb Smirnoff NG_FREE_ITEM(item); 2570b332b91fSGleb Smirnoff } else /* it is NGQF_FN2 */ 2571e088dd4cSAlexander Motin error = (*NGI_FN2(item))(node, item, hook); 2572e088dd4cSAlexander Motin break; 2573069154d5SJulian Elischer } 2574069154d5SJulian Elischer /* 2575069154d5SJulian Elischer * We held references on some of the resources 2576069154d5SJulian Elischer * that we took from the item. Now that we have 2577069154d5SJulian Elischer * finished doing everything, drop those references. 2578069154d5SJulian Elischer */ 2579e72a98f4SAlexander Motin if (hook) 258030400f03SJulian Elischer NG_HOOK_UNREF(hook); 2581069154d5SJulian Elischer 2582714fb865SGleb Smirnoff if (rw == NGQRW_R) { 258330400f03SJulian Elischer ng_leave_read(&node->nd_input_queue); 2584a54a69d7SJulian Elischer } else { 258530400f03SJulian Elischer ng_leave_write(&node->nd_input_queue); 2586a54a69d7SJulian Elischer } 25878afe16d5SGleb Smirnoff 25888afe16d5SGleb Smirnoff /* Apply callback. */ 2589e72a98f4SAlexander Motin if (apply != NULL && refcount_release(&apply->refs)) 2590e088dd4cSAlexander Motin (*apply->apply)(apply->context, error); 25918afe16d5SGleb Smirnoff 259227757487SGleb Smirnoff return (error); 2593069154d5SJulian Elischer } 2594069154d5SJulian Elischer 2595069154d5SJulian Elischer /*********************************************************************** 2596069154d5SJulian Elischer * Implement the 'generic' control messages 2597069154d5SJulian Elischer ***********************************************************************/ 2598069154d5SJulian Elischer static int 2599069154d5SJulian Elischer ng_generic_msg(node_p here, item_p item, hook_p lasthook) 26004cf49a43SJulian Elischer { 26014cf49a43SJulian Elischer int error = 0; 2602069154d5SJulian Elischer struct ng_mesg *msg; 2603069154d5SJulian Elischer struct ng_mesg *resp = NULL; 26044cf49a43SJulian Elischer 2605069154d5SJulian Elischer NGI_GET_MSG(item, msg); 26064cf49a43SJulian Elischer if (msg->header.typecookie != NGM_GENERIC_COOKIE) { 26076b795970SJulian Elischer TRAP_ERROR(); 2608069154d5SJulian Elischer error = EINVAL; 2609069154d5SJulian Elischer goto out; 26104cf49a43SJulian Elischer } 26114cf49a43SJulian Elischer switch (msg->header.cmd) { 26124cf49a43SJulian Elischer case NGM_SHUTDOWN: 26131acb27c6SJulian Elischer ng_rmnode(here, NULL, NULL, 0); 26144cf49a43SJulian Elischer break; 26154cf49a43SJulian Elischer case NGM_MKPEER: 26164cf49a43SJulian Elischer { 26174cf49a43SJulian Elischer struct ngm_mkpeer *const mkp = (struct ngm_mkpeer *) msg->data; 26184cf49a43SJulian Elischer 26194cf49a43SJulian Elischer if (msg->header.arglen != sizeof(*mkp)) { 26206b795970SJulian Elischer TRAP_ERROR(); 2621069154d5SJulian Elischer error = EINVAL; 2622069154d5SJulian Elischer break; 26234cf49a43SJulian Elischer } 26244cf49a43SJulian Elischer mkp->type[sizeof(mkp->type) - 1] = '\0'; 26254cf49a43SJulian Elischer mkp->ourhook[sizeof(mkp->ourhook) - 1] = '\0'; 26264cf49a43SJulian Elischer mkp->peerhook[sizeof(mkp->peerhook) - 1] = '\0'; 26274cf49a43SJulian Elischer error = ng_mkpeer(here, mkp->ourhook, mkp->peerhook, mkp->type); 26284cf49a43SJulian Elischer break; 26294cf49a43SJulian Elischer } 26304cf49a43SJulian Elischer case NGM_CONNECT: 26314cf49a43SJulian Elischer { 26324cf49a43SJulian Elischer struct ngm_connect *const con = 26334cf49a43SJulian Elischer (struct ngm_connect *) msg->data; 26344cf49a43SJulian Elischer node_p node2; 26354cf49a43SJulian Elischer 26364cf49a43SJulian Elischer if (msg->header.arglen != sizeof(*con)) { 26376b795970SJulian Elischer TRAP_ERROR(); 2638069154d5SJulian Elischer error = EINVAL; 2639069154d5SJulian Elischer break; 26404cf49a43SJulian Elischer } 26414cf49a43SJulian Elischer con->path[sizeof(con->path) - 1] = '\0'; 26424cf49a43SJulian Elischer con->ourhook[sizeof(con->ourhook) - 1] = '\0'; 26434cf49a43SJulian Elischer con->peerhook[sizeof(con->peerhook) - 1] = '\0'; 2644069154d5SJulian Elischer /* Don't forget we get a reference.. */ 2645069154d5SJulian Elischer error = ng_path2noderef(here, con->path, &node2, NULL); 26464cf49a43SJulian Elischer if (error) 26474cf49a43SJulian Elischer break; 2648e088dd4cSAlexander Motin error = ng_con_nodes(item, here, con->ourhook, 2649e088dd4cSAlexander Motin node2, con->peerhook); 265030400f03SJulian Elischer NG_NODE_UNREF(node2); 26514cf49a43SJulian Elischer break; 26524cf49a43SJulian Elischer } 26534cf49a43SJulian Elischer case NGM_NAME: 26544cf49a43SJulian Elischer { 26554cf49a43SJulian Elischer struct ngm_name *const nam = (struct ngm_name *) msg->data; 26564cf49a43SJulian Elischer 26574cf49a43SJulian Elischer if (msg->header.arglen != sizeof(*nam)) { 26586b795970SJulian Elischer TRAP_ERROR(); 2659069154d5SJulian Elischer error = EINVAL; 2660069154d5SJulian Elischer break; 26614cf49a43SJulian Elischer } 26624cf49a43SJulian Elischer nam->name[sizeof(nam->name) - 1] = '\0'; 26634cf49a43SJulian Elischer error = ng_name_node(here, nam->name); 26644cf49a43SJulian Elischer break; 26654cf49a43SJulian Elischer } 26664cf49a43SJulian Elischer case NGM_RMHOOK: 26674cf49a43SJulian Elischer { 26684cf49a43SJulian Elischer struct ngm_rmhook *const rmh = (struct ngm_rmhook *) msg->data; 26694cf49a43SJulian Elischer hook_p hook; 26704cf49a43SJulian Elischer 26714cf49a43SJulian Elischer if (msg->header.arglen != sizeof(*rmh)) { 26726b795970SJulian Elischer TRAP_ERROR(); 2673069154d5SJulian Elischer error = EINVAL; 2674069154d5SJulian Elischer break; 26754cf49a43SJulian Elischer } 26764cf49a43SJulian Elischer rmh->ourhook[sizeof(rmh->ourhook) - 1] = '\0'; 2677899e9c4eSArchie Cobbs if ((hook = ng_findhook(here, rmh->ourhook)) != NULL) 26784cf49a43SJulian Elischer ng_destroy_hook(hook); 26794cf49a43SJulian Elischer break; 26804cf49a43SJulian Elischer } 26814cf49a43SJulian Elischer case NGM_NODEINFO: 26824cf49a43SJulian Elischer { 26834cf49a43SJulian Elischer struct nodeinfo *ni; 26844cf49a43SJulian Elischer 2685069154d5SJulian Elischer NG_MKRESPONSE(resp, msg, sizeof(*ni), M_NOWAIT); 26864cf49a43SJulian Elischer if (resp == NULL) { 26874cf49a43SJulian Elischer error = ENOMEM; 26884cf49a43SJulian Elischer break; 26894cf49a43SJulian Elischer } 26904cf49a43SJulian Elischer 26914cf49a43SJulian Elischer /* Fill in node info */ 2692069154d5SJulian Elischer ni = (struct nodeinfo *) resp->data; 269330400f03SJulian Elischer if (NG_NODE_HAS_NAME(here)) 269487e2c66aSHartmut Brandt strcpy(ni->name, NG_NODE_NAME(here)); 269587e2c66aSHartmut Brandt strcpy(ni->type, here->nd_type->name); 2696dc90cad9SJulian Elischer ni->id = ng_node2ID(here); 269730400f03SJulian Elischer ni->hooks = here->nd_numhooks; 26984cf49a43SJulian Elischer break; 26994cf49a43SJulian Elischer } 27004cf49a43SJulian Elischer case NGM_LISTHOOKS: 27014cf49a43SJulian Elischer { 270230400f03SJulian Elischer const int nhooks = here->nd_numhooks; 27034cf49a43SJulian Elischer struct hooklist *hl; 27044cf49a43SJulian Elischer struct nodeinfo *ni; 27054cf49a43SJulian Elischer hook_p hook; 27064cf49a43SJulian Elischer 27074cf49a43SJulian Elischer /* Get response struct */ 2708069154d5SJulian Elischer NG_MKRESPONSE(resp, msg, sizeof(*hl) 27094cf49a43SJulian Elischer + (nhooks * sizeof(struct linkinfo)), M_NOWAIT); 2710069154d5SJulian Elischer if (resp == NULL) { 27114cf49a43SJulian Elischer error = ENOMEM; 27124cf49a43SJulian Elischer break; 27134cf49a43SJulian Elischer } 2714069154d5SJulian Elischer hl = (struct hooklist *) resp->data; 27154cf49a43SJulian Elischer ni = &hl->nodeinfo; 27164cf49a43SJulian Elischer 27174cf49a43SJulian Elischer /* Fill in node info */ 271830400f03SJulian Elischer if (NG_NODE_HAS_NAME(here)) 271987e2c66aSHartmut Brandt strcpy(ni->name, NG_NODE_NAME(here)); 272087e2c66aSHartmut Brandt strcpy(ni->type, here->nd_type->name); 2721dc90cad9SJulian Elischer ni->id = ng_node2ID(here); 27224cf49a43SJulian Elischer 27234cf49a43SJulian Elischer /* Cycle through the linked list of hooks */ 27244cf49a43SJulian Elischer ni->hooks = 0; 272530400f03SJulian Elischer LIST_FOREACH(hook, &here->nd_hooks, hk_hooks) { 27264cf49a43SJulian Elischer struct linkinfo *const link = &hl->link[ni->hooks]; 27274cf49a43SJulian Elischer 27284cf49a43SJulian Elischer if (ni->hooks >= nhooks) { 27294cf49a43SJulian Elischer log(LOG_ERR, "%s: number of %s changed\n", 27306e551fb6SDavid E. O'Brien __func__, "hooks"); 27314cf49a43SJulian Elischer break; 27324cf49a43SJulian Elischer } 273330400f03SJulian Elischer if (NG_HOOK_NOT_VALID(hook)) 27344cf49a43SJulian Elischer continue; 273587e2c66aSHartmut Brandt strcpy(link->ourhook, NG_HOOK_NAME(hook)); 273687e2c66aSHartmut Brandt strcpy(link->peerhook, NG_PEER_HOOK_NAME(hook)); 273730400f03SJulian Elischer if (NG_PEER_NODE_NAME(hook)[0] != '\0') 273887e2c66aSHartmut Brandt strcpy(link->nodeinfo.name, 273987e2c66aSHartmut Brandt NG_PEER_NODE_NAME(hook)); 274087e2c66aSHartmut Brandt strcpy(link->nodeinfo.type, 274187e2c66aSHartmut Brandt NG_PEER_NODE(hook)->nd_type->name); 274230400f03SJulian Elischer link->nodeinfo.id = ng_node2ID(NG_PEER_NODE(hook)); 274330400f03SJulian Elischer link->nodeinfo.hooks = NG_PEER_NODE(hook)->nd_numhooks; 27444cf49a43SJulian Elischer ni->hooks++; 27454cf49a43SJulian Elischer } 27464cf49a43SJulian Elischer break; 27474cf49a43SJulian Elischer } 27484cf49a43SJulian Elischer 27494cf49a43SJulian Elischer case NGM_LISTNAMES: 27504cf49a43SJulian Elischer case NGM_LISTNODES: 27514cf49a43SJulian Elischer { 27524cf49a43SJulian Elischer const int unnamed = (msg->header.cmd == NGM_LISTNODES); 27534cf49a43SJulian Elischer struct namelist *nl; 27544cf49a43SJulian Elischer node_p node; 27554cf49a43SJulian Elischer int num = 0; 27564cf49a43SJulian Elischer 27579ed346baSBosko Milekic mtx_lock(&ng_nodelist_mtx); 27584cf49a43SJulian Elischer /* Count number of nodes */ 275930400f03SJulian Elischer LIST_FOREACH(node, &ng_nodelist, nd_nodes) { 276070de87f2SJulian Elischer if (NG_NODE_IS_VALID(node) 276170de87f2SJulian Elischer && (unnamed || NG_NODE_HAS_NAME(node))) { 27624cf49a43SJulian Elischer num++; 27634cf49a43SJulian Elischer } 276470de87f2SJulian Elischer } 27659ed346baSBosko Milekic mtx_unlock(&ng_nodelist_mtx); 27664cf49a43SJulian Elischer 27674cf49a43SJulian Elischer /* Get response struct */ 2768069154d5SJulian Elischer NG_MKRESPONSE(resp, msg, sizeof(*nl) 27694cf49a43SJulian Elischer + (num * sizeof(struct nodeinfo)), M_NOWAIT); 2770069154d5SJulian Elischer if (resp == NULL) { 27714cf49a43SJulian Elischer error = ENOMEM; 27724cf49a43SJulian Elischer break; 27734cf49a43SJulian Elischer } 2774069154d5SJulian Elischer nl = (struct namelist *) resp->data; 27754cf49a43SJulian Elischer 27764cf49a43SJulian Elischer /* Cycle through the linked list of nodes */ 27774cf49a43SJulian Elischer nl->numnames = 0; 27789ed346baSBosko Milekic mtx_lock(&ng_nodelist_mtx); 277930400f03SJulian Elischer LIST_FOREACH(node, &ng_nodelist, nd_nodes) { 27804cf49a43SJulian Elischer struct nodeinfo *const np = &nl->nodeinfo[nl->numnames]; 27814cf49a43SJulian Elischer 2782b96baf0aSGleb Smirnoff if (NG_NODE_NOT_VALID(node)) 2783b96baf0aSGleb Smirnoff continue; 2784b96baf0aSGleb Smirnoff if (!unnamed && (! NG_NODE_HAS_NAME(node))) 2785b96baf0aSGleb Smirnoff continue; 27864cf49a43SJulian Elischer if (nl->numnames >= num) { 27874cf49a43SJulian Elischer log(LOG_ERR, "%s: number of %s changed\n", 27886e551fb6SDavid E. O'Brien __func__, "nodes"); 27894cf49a43SJulian Elischer break; 27904cf49a43SJulian Elischer } 279130400f03SJulian Elischer if (NG_NODE_HAS_NAME(node)) 279287e2c66aSHartmut Brandt strcpy(np->name, NG_NODE_NAME(node)); 279387e2c66aSHartmut Brandt strcpy(np->type, node->nd_type->name); 2794dc90cad9SJulian Elischer np->id = ng_node2ID(node); 279530400f03SJulian Elischer np->hooks = node->nd_numhooks; 27964cf49a43SJulian Elischer nl->numnames++; 27974cf49a43SJulian Elischer } 27989ed346baSBosko Milekic mtx_unlock(&ng_nodelist_mtx); 27994cf49a43SJulian Elischer break; 28004cf49a43SJulian Elischer } 28014cf49a43SJulian Elischer 28024cf49a43SJulian Elischer case NGM_LISTTYPES: 28034cf49a43SJulian Elischer { 28044cf49a43SJulian Elischer struct typelist *tl; 28054cf49a43SJulian Elischer struct ng_type *type; 28064cf49a43SJulian Elischer int num = 0; 28074cf49a43SJulian Elischer 28089ed346baSBosko Milekic mtx_lock(&ng_typelist_mtx); 28094cf49a43SJulian Elischer /* Count number of types */ 281070de87f2SJulian Elischer LIST_FOREACH(type, &ng_typelist, types) { 28114cf49a43SJulian Elischer num++; 281270de87f2SJulian Elischer } 28139ed346baSBosko Milekic mtx_unlock(&ng_typelist_mtx); 28144cf49a43SJulian Elischer 28154cf49a43SJulian Elischer /* Get response struct */ 2816069154d5SJulian Elischer NG_MKRESPONSE(resp, msg, sizeof(*tl) 28174cf49a43SJulian Elischer + (num * sizeof(struct typeinfo)), M_NOWAIT); 2818069154d5SJulian Elischer if (resp == NULL) { 28194cf49a43SJulian Elischer error = ENOMEM; 28204cf49a43SJulian Elischer break; 28214cf49a43SJulian Elischer } 2822069154d5SJulian Elischer tl = (struct typelist *) resp->data; 28234cf49a43SJulian Elischer 28244cf49a43SJulian Elischer /* Cycle through the linked list of types */ 28254cf49a43SJulian Elischer tl->numtypes = 0; 28269ed346baSBosko Milekic mtx_lock(&ng_typelist_mtx); 2827069154d5SJulian Elischer LIST_FOREACH(type, &ng_typelist, types) { 28284cf49a43SJulian Elischer struct typeinfo *const tp = &tl->typeinfo[tl->numtypes]; 28294cf49a43SJulian Elischer 28304cf49a43SJulian Elischer if (tl->numtypes >= num) { 28314cf49a43SJulian Elischer log(LOG_ERR, "%s: number of %s changed\n", 28326e551fb6SDavid E. O'Brien __func__, "types"); 28334cf49a43SJulian Elischer break; 28344cf49a43SJulian Elischer } 283587e2c66aSHartmut Brandt strcpy(tp->type_name, type->name); 2836c73b94a2SJulian Elischer tp->numnodes = type->refs - 1; /* don't count list */ 28374cf49a43SJulian Elischer tl->numtypes++; 28384cf49a43SJulian Elischer } 28399ed346baSBosko Milekic mtx_unlock(&ng_typelist_mtx); 28404cf49a43SJulian Elischer break; 28414cf49a43SJulian Elischer } 28424cf49a43SJulian Elischer 2843f8307e12SArchie Cobbs case NGM_BINARY2ASCII: 2844f8307e12SArchie Cobbs { 28457133ac27SArchie Cobbs int bufSize = 20 * 1024; /* XXX hard coded constant */ 2846f8307e12SArchie Cobbs const struct ng_parse_type *argstype; 2847f8307e12SArchie Cobbs const struct ng_cmdlist *c; 2848069154d5SJulian Elischer struct ng_mesg *binary, *ascii; 2849f8307e12SArchie Cobbs 2850f8307e12SArchie Cobbs /* Data area must contain a valid netgraph message */ 2851f8307e12SArchie Cobbs binary = (struct ng_mesg *)msg->data; 28524c9b5910SGleb Smirnoff if (msg->header.arglen < sizeof(struct ng_mesg) || 28534c9b5910SGleb Smirnoff (msg->header.arglen - sizeof(struct ng_mesg) < 28544c9b5910SGleb Smirnoff binary->header.arglen)) { 28556b795970SJulian Elischer TRAP_ERROR(); 2856f8307e12SArchie Cobbs error = EINVAL; 2857f8307e12SArchie Cobbs break; 2858f8307e12SArchie Cobbs } 2859f8307e12SArchie Cobbs 2860f8307e12SArchie Cobbs /* Get a response message with lots of room */ 2861069154d5SJulian Elischer NG_MKRESPONSE(resp, msg, sizeof(*ascii) + bufSize, M_NOWAIT); 2862069154d5SJulian Elischer if (resp == NULL) { 2863f8307e12SArchie Cobbs error = ENOMEM; 2864f8307e12SArchie Cobbs break; 2865f8307e12SArchie Cobbs } 2866069154d5SJulian Elischer ascii = (struct ng_mesg *)resp->data; 2867f8307e12SArchie Cobbs 2868f8307e12SArchie Cobbs /* Copy binary message header to response message payload */ 2869f8307e12SArchie Cobbs bcopy(binary, ascii, sizeof(*binary)); 2870f8307e12SArchie Cobbs 2871f8307e12SArchie Cobbs /* Find command by matching typecookie and command number */ 287230400f03SJulian Elischer for (c = here->nd_type->cmdlist; 2873f8307e12SArchie Cobbs c != NULL && c->name != NULL; c++) { 2874f8307e12SArchie Cobbs if (binary->header.typecookie == c->cookie 2875f8307e12SArchie Cobbs && binary->header.cmd == c->cmd) 2876f8307e12SArchie Cobbs break; 2877f8307e12SArchie Cobbs } 2878f8307e12SArchie Cobbs if (c == NULL || c->name == NULL) { 2879f8307e12SArchie Cobbs for (c = ng_generic_cmds; c->name != NULL; c++) { 2880f8307e12SArchie Cobbs if (binary->header.typecookie == c->cookie 2881f8307e12SArchie Cobbs && binary->header.cmd == c->cmd) 2882f8307e12SArchie Cobbs break; 2883f8307e12SArchie Cobbs } 2884f8307e12SArchie Cobbs if (c->name == NULL) { 2885069154d5SJulian Elischer NG_FREE_MSG(resp); 2886f8307e12SArchie Cobbs error = ENOSYS; 2887f8307e12SArchie Cobbs break; 2888f8307e12SArchie Cobbs } 2889f8307e12SArchie Cobbs } 2890f8307e12SArchie Cobbs 2891f8307e12SArchie Cobbs /* Convert command name to ASCII */ 2892f8307e12SArchie Cobbs snprintf(ascii->header.cmdstr, sizeof(ascii->header.cmdstr), 2893f8307e12SArchie Cobbs "%s", c->name); 2894f8307e12SArchie Cobbs 2895f8307e12SArchie Cobbs /* Convert command arguments to ASCII */ 2896f8307e12SArchie Cobbs argstype = (binary->header.flags & NGF_RESP) ? 2897f8307e12SArchie Cobbs c->respType : c->mesgType; 289870de87f2SJulian Elischer if (argstype == NULL) { 2899f8307e12SArchie Cobbs *ascii->data = '\0'; 290070de87f2SJulian Elischer } else { 2901f8307e12SArchie Cobbs if ((error = ng_unparse(argstype, 2902f8307e12SArchie Cobbs (u_char *)binary->data, 2903f8307e12SArchie Cobbs ascii->data, bufSize)) != 0) { 2904069154d5SJulian Elischer NG_FREE_MSG(resp); 2905f8307e12SArchie Cobbs break; 2906f8307e12SArchie Cobbs } 2907f8307e12SArchie Cobbs } 2908f8307e12SArchie Cobbs 2909f8307e12SArchie Cobbs /* Return the result as struct ng_mesg plus ASCII string */ 2910f8307e12SArchie Cobbs bufSize = strlen(ascii->data) + 1; 2911f8307e12SArchie Cobbs ascii->header.arglen = bufSize; 2912069154d5SJulian Elischer resp->header.arglen = sizeof(*ascii) + bufSize; 2913f8307e12SArchie Cobbs break; 2914f8307e12SArchie Cobbs } 2915f8307e12SArchie Cobbs 2916f8307e12SArchie Cobbs case NGM_ASCII2BINARY: 2917f8307e12SArchie Cobbs { 2918f8307e12SArchie Cobbs int bufSize = 2000; /* XXX hard coded constant */ 2919f8307e12SArchie Cobbs const struct ng_cmdlist *c; 2920f8307e12SArchie Cobbs const struct ng_parse_type *argstype; 2921069154d5SJulian Elischer struct ng_mesg *ascii, *binary; 292252ec4a03SArchie Cobbs int off = 0; 2923f8307e12SArchie Cobbs 2924f8307e12SArchie Cobbs /* Data area must contain at least a struct ng_mesg + '\0' */ 2925f8307e12SArchie Cobbs ascii = (struct ng_mesg *)msg->data; 29264c9b5910SGleb Smirnoff if ((msg->header.arglen < sizeof(*ascii) + 1) || 29274c9b5910SGleb Smirnoff (ascii->header.arglen < 1) || 29284c9b5910SGleb Smirnoff (msg->header.arglen < sizeof(*ascii) + 29294c9b5910SGleb Smirnoff ascii->header.arglen)) { 29306b795970SJulian Elischer TRAP_ERROR(); 2931f8307e12SArchie Cobbs error = EINVAL; 2932f8307e12SArchie Cobbs break; 2933f8307e12SArchie Cobbs } 2934f8307e12SArchie Cobbs ascii->data[ascii->header.arglen - 1] = '\0'; 2935f8307e12SArchie Cobbs 2936f8307e12SArchie Cobbs /* Get a response message with lots of room */ 2937069154d5SJulian Elischer NG_MKRESPONSE(resp, msg, sizeof(*binary) + bufSize, M_NOWAIT); 2938069154d5SJulian Elischer if (resp == NULL) { 2939f8307e12SArchie Cobbs error = ENOMEM; 2940f8307e12SArchie Cobbs break; 2941f8307e12SArchie Cobbs } 2942069154d5SJulian Elischer binary = (struct ng_mesg *)resp->data; 2943f8307e12SArchie Cobbs 2944f8307e12SArchie Cobbs /* Copy ASCII message header to response message payload */ 2945f8307e12SArchie Cobbs bcopy(ascii, binary, sizeof(*ascii)); 2946f8307e12SArchie Cobbs 2947f8307e12SArchie Cobbs /* Find command by matching ASCII command string */ 294830400f03SJulian Elischer for (c = here->nd_type->cmdlist; 2949f8307e12SArchie Cobbs c != NULL && c->name != NULL; c++) { 2950f8307e12SArchie Cobbs if (strcmp(ascii->header.cmdstr, c->name) == 0) 2951f8307e12SArchie Cobbs break; 2952f8307e12SArchie Cobbs } 2953f8307e12SArchie Cobbs if (c == NULL || c->name == NULL) { 2954f8307e12SArchie Cobbs for (c = ng_generic_cmds; c->name != NULL; c++) { 2955f8307e12SArchie Cobbs if (strcmp(ascii->header.cmdstr, c->name) == 0) 2956f8307e12SArchie Cobbs break; 2957f8307e12SArchie Cobbs } 2958f8307e12SArchie Cobbs if (c->name == NULL) { 2959069154d5SJulian Elischer NG_FREE_MSG(resp); 2960f8307e12SArchie Cobbs error = ENOSYS; 2961f8307e12SArchie Cobbs break; 2962f8307e12SArchie Cobbs } 2963f8307e12SArchie Cobbs } 2964f8307e12SArchie Cobbs 2965f8307e12SArchie Cobbs /* Convert command name to binary */ 2966f8307e12SArchie Cobbs binary->header.cmd = c->cmd; 2967f8307e12SArchie Cobbs binary->header.typecookie = c->cookie; 2968f8307e12SArchie Cobbs 2969f8307e12SArchie Cobbs /* Convert command arguments to binary */ 2970f8307e12SArchie Cobbs argstype = (binary->header.flags & NGF_RESP) ? 2971f8307e12SArchie Cobbs c->respType : c->mesgType; 297270de87f2SJulian Elischer if (argstype == NULL) { 2973f8307e12SArchie Cobbs bufSize = 0; 297470de87f2SJulian Elischer } else { 2975f8307e12SArchie Cobbs if ((error = ng_parse(argstype, ascii->data, 2976f8307e12SArchie Cobbs &off, (u_char *)binary->data, &bufSize)) != 0) { 2977069154d5SJulian Elischer NG_FREE_MSG(resp); 2978f8307e12SArchie Cobbs break; 2979f8307e12SArchie Cobbs } 2980f8307e12SArchie Cobbs } 2981f8307e12SArchie Cobbs 2982f8307e12SArchie Cobbs /* Return the result */ 2983f8307e12SArchie Cobbs binary->header.arglen = bufSize; 2984069154d5SJulian Elischer resp->header.arglen = sizeof(*binary) + bufSize; 2985f8307e12SArchie Cobbs break; 2986f8307e12SArchie Cobbs } 2987f8307e12SArchie Cobbs 29887095e097SPoul-Henning Kamp case NGM_TEXT_CONFIG: 29894cf49a43SJulian Elischer case NGM_TEXT_STATUS: 29904cf49a43SJulian Elischer /* 29914cf49a43SJulian Elischer * This one is tricky as it passes the command down to the 29924cf49a43SJulian Elischer * actual node, even though it is a generic type command. 2993069154d5SJulian Elischer * This means we must assume that the item/msg is already freed 29944cf49a43SJulian Elischer * when control passes back to us. 29954cf49a43SJulian Elischer */ 299630400f03SJulian Elischer if (here->nd_type->rcvmsg != NULL) { 2997069154d5SJulian Elischer NGI_MSG(item) = msg; /* put it back as we found it */ 299830400f03SJulian Elischer return((*here->nd_type->rcvmsg)(here, item, lasthook)); 29994cf49a43SJulian Elischer } 30004cf49a43SJulian Elischer /* Fall through if rcvmsg not supported */ 30014cf49a43SJulian Elischer default: 30026b795970SJulian Elischer TRAP_ERROR(); 30034cf49a43SJulian Elischer error = EINVAL; 30044cf49a43SJulian Elischer } 3005069154d5SJulian Elischer /* 3006069154d5SJulian Elischer * Sometimes a generic message may be statically allocated 3007069154d5SJulian Elischer * to avoid problems with allocating when in tight memeory situations. 3008069154d5SJulian Elischer * Don't free it if it is so. 3009069154d5SJulian Elischer * I break them appart here, because erros may cause a free if the item 3010069154d5SJulian Elischer * in which case we'd be doing it twice. 3011069154d5SJulian Elischer * they are kept together above, to simplify freeing. 3012069154d5SJulian Elischer */ 3013069154d5SJulian Elischer out: 3014069154d5SJulian Elischer NG_RESPOND_MSG(error, here, item, resp); 30151acb27c6SJulian Elischer if (msg) 3016069154d5SJulian Elischer NG_FREE_MSG(msg); 30174cf49a43SJulian Elischer return (error); 30184cf49a43SJulian Elischer } 30194cf49a43SJulian Elischer 30204cf49a43SJulian Elischer /************************************************************************ 30218253c060SGleb Smirnoff Queue element get/free routines 30228253c060SGleb Smirnoff ************************************************************************/ 30238253c060SGleb Smirnoff 30248253c060SGleb Smirnoff uma_zone_t ng_qzone; 30258253c060SGleb Smirnoff static int maxalloc = 512; /* limit the damage of a leak */ 30268253c060SGleb Smirnoff 30278253c060SGleb Smirnoff TUNABLE_INT("net.graph.maxalloc", &maxalloc); 30288253c060SGleb Smirnoff SYSCTL_INT(_net_graph, OID_AUTO, maxalloc, CTLFLAG_RDTUN, &maxalloc, 30298253c060SGleb Smirnoff 0, "Maximum number of queue items to allocate"); 30308253c060SGleb Smirnoff 30318253c060SGleb Smirnoff #ifdef NETGRAPH_DEBUG 30328253c060SGleb Smirnoff static TAILQ_HEAD(, ng_item) ng_itemlist = TAILQ_HEAD_INITIALIZER(ng_itemlist); 30338253c060SGleb Smirnoff static int allocated; /* number of items malloc'd */ 30348253c060SGleb Smirnoff #endif 30358253c060SGleb Smirnoff 30368253c060SGleb Smirnoff /* 30378253c060SGleb Smirnoff * Get a queue entry. 30388253c060SGleb Smirnoff * This is usually called when a packet first enters netgraph. 30398253c060SGleb Smirnoff * By definition, this is usually from an interrupt, or from a user. 30408253c060SGleb Smirnoff * Users are not so important, but try be quick for the times that it's 30418253c060SGleb Smirnoff * an interrupt. 30428253c060SGleb Smirnoff */ 30438253c060SGleb Smirnoff static __inline item_p 304442282202SGleb Smirnoff ng_getqblk(int flags) 30458253c060SGleb Smirnoff { 30468253c060SGleb Smirnoff item_p item = NULL; 304742282202SGleb Smirnoff int wait; 30488253c060SGleb Smirnoff 304942282202SGleb Smirnoff wait = (flags & NG_WAITOK) ? M_WAITOK : M_NOWAIT; 305042282202SGleb Smirnoff 305142282202SGleb Smirnoff item = uma_zalloc(ng_qzone, wait | M_ZERO); 30528253c060SGleb Smirnoff 30538253c060SGleb Smirnoff #ifdef NETGRAPH_DEBUG 30548253c060SGleb Smirnoff if (item) { 30558253c060SGleb Smirnoff mtx_lock(&ngq_mtx); 30568253c060SGleb Smirnoff TAILQ_INSERT_TAIL(&ng_itemlist, item, all); 30578253c060SGleb Smirnoff allocated++; 30588253c060SGleb Smirnoff mtx_unlock(&ngq_mtx); 30598253c060SGleb Smirnoff } 30608253c060SGleb Smirnoff #endif 30618253c060SGleb Smirnoff 30628253c060SGleb Smirnoff return (item); 30638253c060SGleb Smirnoff } 30648253c060SGleb Smirnoff 30658253c060SGleb Smirnoff /* 30668253c060SGleb Smirnoff * Release a queue entry 30678253c060SGleb Smirnoff */ 30688253c060SGleb Smirnoff void 30698253c060SGleb Smirnoff ng_free_item(item_p item) 30708253c060SGleb Smirnoff { 30718253c060SGleb Smirnoff /* 30728253c060SGleb Smirnoff * The item may hold resources on it's own. We need to free 30738253c060SGleb Smirnoff * these before we can free the item. What they are depends upon 30748253c060SGleb Smirnoff * what kind of item it is. it is important that nodes zero 30758253c060SGleb Smirnoff * out pointers to resources that they remove from the item 30768253c060SGleb Smirnoff * or we release them again here. 30778253c060SGleb Smirnoff */ 30788253c060SGleb Smirnoff switch (item->el_flags & NGQF_TYPE) { 30798253c060SGleb Smirnoff case NGQF_DATA: 30808253c060SGleb Smirnoff /* If we have an mbuf still attached.. */ 30818253c060SGleb Smirnoff NG_FREE_M(_NGI_M(item)); 30828253c060SGleb Smirnoff break; 30838253c060SGleb Smirnoff case NGQF_MESG: 30848253c060SGleb Smirnoff _NGI_RETADDR(item) = 0; 30858253c060SGleb Smirnoff NG_FREE_MSG(_NGI_MSG(item)); 30868253c060SGleb Smirnoff break; 30878253c060SGleb Smirnoff case NGQF_FN: 3088e088dd4cSAlexander Motin case NGQF_FN2: 30898253c060SGleb Smirnoff /* nothing to free really, */ 30908253c060SGleb Smirnoff _NGI_FN(item) = NULL; 30918253c060SGleb Smirnoff _NGI_ARG1(item) = NULL; 30928253c060SGleb Smirnoff _NGI_ARG2(item) = 0; 30938253c060SGleb Smirnoff break; 30948253c060SGleb Smirnoff } 30958253c060SGleb Smirnoff /* If we still have a node or hook referenced... */ 30968253c060SGleb Smirnoff _NGI_CLR_NODE(item); 30978253c060SGleb Smirnoff _NGI_CLR_HOOK(item); 30988253c060SGleb Smirnoff 30998253c060SGleb Smirnoff #ifdef NETGRAPH_DEBUG 31008253c060SGleb Smirnoff mtx_lock(&ngq_mtx); 31018253c060SGleb Smirnoff TAILQ_REMOVE(&ng_itemlist, item, all); 31028253c060SGleb Smirnoff allocated--; 31038253c060SGleb Smirnoff mtx_unlock(&ngq_mtx); 31048253c060SGleb Smirnoff #endif 31058253c060SGleb Smirnoff uma_zfree(ng_qzone, item); 31068253c060SGleb Smirnoff } 31078253c060SGleb Smirnoff 31088253c060SGleb Smirnoff /************************************************************************ 31094cf49a43SJulian Elischer Module routines 31104cf49a43SJulian Elischer ************************************************************************/ 31114cf49a43SJulian Elischer 31124cf49a43SJulian Elischer /* 31134cf49a43SJulian Elischer * Handle the loading/unloading of a netgraph node type module 31144cf49a43SJulian Elischer */ 31154cf49a43SJulian Elischer int 31164cf49a43SJulian Elischer ng_mod_event(module_t mod, int event, void *data) 31174cf49a43SJulian Elischer { 31184cf49a43SJulian Elischer struct ng_type *const type = data; 31194cf49a43SJulian Elischer int s, error = 0; 31204cf49a43SJulian Elischer 31214cf49a43SJulian Elischer switch (event) { 31224cf49a43SJulian Elischer case MOD_LOAD: 31234cf49a43SJulian Elischer 31244cf49a43SJulian Elischer /* Register new netgraph node type */ 31254cf49a43SJulian Elischer s = splnet(); 31264cf49a43SJulian Elischer if ((error = ng_newtype(type)) != 0) { 31274cf49a43SJulian Elischer splx(s); 31284cf49a43SJulian Elischer break; 31294cf49a43SJulian Elischer } 31304cf49a43SJulian Elischer 31314cf49a43SJulian Elischer /* Call type specific code */ 31324cf49a43SJulian Elischer if (type->mod_event != NULL) 3133069154d5SJulian Elischer if ((error = (*type->mod_event)(mod, event, data))) { 31349ed346baSBosko Milekic mtx_lock(&ng_typelist_mtx); 3135c73b94a2SJulian Elischer type->refs--; /* undo it */ 31364cf49a43SJulian Elischer LIST_REMOVE(type, types); 31379ed346baSBosko Milekic mtx_unlock(&ng_typelist_mtx); 3138069154d5SJulian Elischer } 31394cf49a43SJulian Elischer splx(s); 31404cf49a43SJulian Elischer break; 31414cf49a43SJulian Elischer 31424cf49a43SJulian Elischer case MOD_UNLOAD: 31434cf49a43SJulian Elischer s = splnet(); 3144c73b94a2SJulian Elischer if (type->refs > 1) { /* make sure no nodes exist! */ 31454cf49a43SJulian Elischer error = EBUSY; 3146c73b94a2SJulian Elischer } else { 3147c73b94a2SJulian Elischer if (type->refs == 0) { 3148c73b94a2SJulian Elischer /* failed load, nothing to undo */ 3149c73b94a2SJulian Elischer splx(s); 3150c73b94a2SJulian Elischer break; 3151c73b94a2SJulian Elischer } 31524cf49a43SJulian Elischer if (type->mod_event != NULL) { /* check with type */ 31534cf49a43SJulian Elischer error = (*type->mod_event)(mod, event, data); 31544cf49a43SJulian Elischer if (error != 0) { /* type refuses.. */ 31554cf49a43SJulian Elischer splx(s); 31564cf49a43SJulian Elischer break; 31574cf49a43SJulian Elischer } 31584cf49a43SJulian Elischer } 31599ed346baSBosko Milekic mtx_lock(&ng_typelist_mtx); 31604cf49a43SJulian Elischer LIST_REMOVE(type, types); 31619ed346baSBosko Milekic mtx_unlock(&ng_typelist_mtx); 31624cf49a43SJulian Elischer } 31634cf49a43SJulian Elischer splx(s); 31644cf49a43SJulian Elischer break; 31654cf49a43SJulian Elischer 31664cf49a43SJulian Elischer default: 31674cf49a43SJulian Elischer if (type->mod_event != NULL) 31684cf49a43SJulian Elischer error = (*type->mod_event)(mod, event, data); 31694cf49a43SJulian Elischer else 31703e019deaSPoul-Henning Kamp error = EOPNOTSUPP; /* XXX ? */ 31714cf49a43SJulian Elischer break; 31724cf49a43SJulian Elischer } 31734cf49a43SJulian Elischer return (error); 31744cf49a43SJulian Elischer } 31754cf49a43SJulian Elischer 31764cf49a43SJulian Elischer /* 31774cf49a43SJulian Elischer * Handle loading and unloading for this code. 31784cf49a43SJulian Elischer * The only thing we need to link into is the NETISR strucure. 31794cf49a43SJulian Elischer */ 31804cf49a43SJulian Elischer static int 31814cf49a43SJulian Elischer ngb_mod_event(module_t mod, int event, void *data) 31824cf49a43SJulian Elischer { 31831489164fSGleb Smirnoff int error = 0; 31844cf49a43SJulian Elischer 31854cf49a43SJulian Elischer switch (event) { 31864cf49a43SJulian Elischer case MOD_LOAD: 31871489164fSGleb Smirnoff /* Initialize everything. */ 31882c8dda8dSWojciech A. Koszek NG_WORKLIST_LOCK_INIT(); 3189efd8e7c9SDon Lewis mtx_init(&ng_typelist_mtx, "netgraph types mutex", NULL, 3190efd8e7c9SDon Lewis MTX_DEF); 3191efd8e7c9SDon Lewis mtx_init(&ng_nodelist_mtx, "netgraph nodelist mutex", NULL, 3192efd8e7c9SDon Lewis MTX_DEF); 3193efd8e7c9SDon Lewis mtx_init(&ng_idhash_mtx, "netgraph idhash mutex", NULL, 3194efd8e7c9SDon Lewis MTX_DEF); 3195ac5dd141SGleb Smirnoff mtx_init(&ng_topo_mtx, "netgraph topology mutex", NULL, 3196ac5dd141SGleb Smirnoff MTX_DEF); 31971489164fSGleb Smirnoff #ifdef NETGRAPH_DEBUG 31981489164fSGleb Smirnoff mtx_init(&ngq_mtx, "netgraph item list mutex", NULL, 3199efd8e7c9SDon Lewis MTX_DEF); 32001489164fSGleb Smirnoff #endif 32011489164fSGleb Smirnoff ng_qzone = uma_zcreate("NetGraph items", sizeof(struct ng_item), 32021489164fSGleb Smirnoff NULL, NULL, NULL, NULL, UMA_ALIGN_CACHE, 0); 32031489164fSGleb Smirnoff uma_zone_set_max(ng_qzone, maxalloc); 320468780975SGleb Smirnoff netisr_register(NETISR_NETGRAPH, (netisr_t *)ngintr, NULL, 320568780975SGleb Smirnoff NETISR_MPSAFE); 32064cf49a43SJulian Elischer break; 32074cf49a43SJulian Elischer case MOD_UNLOAD: 320864efc707SRobert Watson /* You can't unload it because an interface may be using it. */ 32094cf49a43SJulian Elischer error = EBUSY; 32104cf49a43SJulian Elischer break; 32114cf49a43SJulian Elischer default: 32124cf49a43SJulian Elischer error = EOPNOTSUPP; 32134cf49a43SJulian Elischer break; 32144cf49a43SJulian Elischer } 32154cf49a43SJulian Elischer return (error); 32164cf49a43SJulian Elischer } 32174cf49a43SJulian Elischer 32184cf49a43SJulian Elischer static moduledata_t netgraph_mod = { 32194cf49a43SJulian Elischer "netgraph", 32204cf49a43SJulian Elischer ngb_mod_event, 32214cf49a43SJulian Elischer (NULL) 32224cf49a43SJulian Elischer }; 3223aa38f8f9SMaksim Yevmenkin DECLARE_MODULE(netgraph, netgraph_mod, SI_SUB_NETGRAPH, SI_ORDER_MIDDLE); 3224bfa7e882SJulian Elischer SYSCTL_NODE(_net, OID_AUTO, graph, CTLFLAG_RW, 0, "netgraph Family"); 3225bfa7e882SJulian Elischer SYSCTL_INT(_net_graph, OID_AUTO, abi_version, CTLFLAG_RD, 0, NG_ABI_VERSION,""); 3226bfa7e882SJulian Elischer SYSCTL_INT(_net_graph, OID_AUTO, msg_version, CTLFLAG_RD, 0, NG_VERSION, ""); 32274cf49a43SJulian Elischer 322830400f03SJulian Elischer #ifdef NETGRAPH_DEBUG 322930400f03SJulian Elischer void 323030400f03SJulian Elischer dumphook (hook_p hook, char *file, int line) 323130400f03SJulian Elischer { 323230400f03SJulian Elischer printf("hook: name %s, %d refs, Last touched:\n", 323330400f03SJulian Elischer _NG_HOOK_NAME(hook), hook->hk_refs); 323430400f03SJulian Elischer printf(" Last active @ %s, line %d\n", 323530400f03SJulian Elischer hook->lastfile, hook->lastline); 323630400f03SJulian Elischer if (line) { 323730400f03SJulian Elischer printf(" problem discovered at file %s, line %d\n", file, line); 323830400f03SJulian Elischer } 323930400f03SJulian Elischer } 324030400f03SJulian Elischer 324130400f03SJulian Elischer void 324230400f03SJulian Elischer dumpnode(node_p node, char *file, int line) 324330400f03SJulian Elischer { 324430400f03SJulian Elischer printf("node: ID [%x]: type '%s', %d hooks, flags 0x%x, %d refs, %s:\n", 32456b795970SJulian Elischer _NG_NODE_ID(node), node->nd_type->name, 324630400f03SJulian Elischer node->nd_numhooks, node->nd_flags, 324730400f03SJulian Elischer node->nd_refs, node->nd_name); 324830400f03SJulian Elischer printf(" Last active @ %s, line %d\n", 324930400f03SJulian Elischer node->lastfile, node->lastline); 325030400f03SJulian Elischer if (line) { 325130400f03SJulian Elischer printf(" problem discovered at file %s, line %d\n", file, line); 325230400f03SJulian Elischer } 325330400f03SJulian Elischer } 325430400f03SJulian Elischer 3255069154d5SJulian Elischer void 3256069154d5SJulian Elischer dumpitem(item_p item, char *file, int line) 3257069154d5SJulian Elischer { 3258069154d5SJulian Elischer printf(" ACTIVE item, last used at %s, line %d", 3259069154d5SJulian Elischer item->lastfile, item->lastline); 32606b795970SJulian Elischer switch(item->el_flags & NGQF_TYPE) { 32616b795970SJulian Elischer case NGQF_DATA: 3262069154d5SJulian Elischer printf(" - [data]\n"); 32636b795970SJulian Elischer break; 32646b795970SJulian Elischer case NGQF_MESG: 32656b795970SJulian Elischer printf(" - retaddr[%d]:\n", _NGI_RETADDR(item)); 32666b795970SJulian Elischer break; 32676b795970SJulian Elischer case NGQF_FN: 3268857304e6SRuslan Ermilov printf(" - fn@%p (%p, %p, %p, %d (%x))\n", 3269857304e6SRuslan Ermilov _NGI_FN(item), 3270857304e6SRuslan Ermilov _NGI_NODE(item), 3271857304e6SRuslan Ermilov _NGI_HOOK(item), 3272857304e6SRuslan Ermilov item->body.fn.fn_arg1, 3273857304e6SRuslan Ermilov item->body.fn.fn_arg2, 3274857304e6SRuslan Ermilov item->body.fn.fn_arg2); 3275857304e6SRuslan Ermilov break; 3276e088dd4cSAlexander Motin case NGQF_FN2: 3277eb4687d2SAlexander Motin printf(" - fn2@%p (%p, %p, %p, %d (%x))\n", 3278857304e6SRuslan Ermilov _NGI_FN2(item), 32796064e568SGleb Smirnoff _NGI_NODE(item), 32806064e568SGleb Smirnoff _NGI_HOOK(item), 32816b795970SJulian Elischer item->body.fn.fn_arg1, 32826b795970SJulian Elischer item->body.fn.fn_arg2, 32836b795970SJulian Elischer item->body.fn.fn_arg2); 32846b795970SJulian Elischer break; 3285069154d5SJulian Elischer } 328630400f03SJulian Elischer if (line) { 3287069154d5SJulian Elischer printf(" problem discovered at file %s, line %d\n", file, line); 32886064e568SGleb Smirnoff if (_NGI_NODE(item)) { 328930400f03SJulian Elischer printf("node %p ([%x])\n", 32906064e568SGleb Smirnoff _NGI_NODE(item), ng_node2ID(_NGI_NODE(item))); 3291069154d5SJulian Elischer } 329230400f03SJulian Elischer } 329330400f03SJulian Elischer } 329430400f03SJulian Elischer 329530400f03SJulian Elischer static void 329630400f03SJulian Elischer ng_dumpitems(void) 329730400f03SJulian Elischer { 329830400f03SJulian Elischer item_p item; 329930400f03SJulian Elischer int i = 1; 330030400f03SJulian Elischer TAILQ_FOREACH(item, &ng_itemlist, all) { 330130400f03SJulian Elischer printf("[%d] ", i++); 330230400f03SJulian Elischer dumpitem(item, NULL, 0); 330330400f03SJulian Elischer } 330430400f03SJulian Elischer } 330530400f03SJulian Elischer 330630400f03SJulian Elischer static void 330730400f03SJulian Elischer ng_dumpnodes(void) 330830400f03SJulian Elischer { 330930400f03SJulian Elischer node_p node; 331030400f03SJulian Elischer int i = 1; 331153f9c5e9SRobert Watson mtx_lock(&ng_nodelist_mtx); 331230400f03SJulian Elischer SLIST_FOREACH(node, &ng_allnodes, nd_all) { 331330400f03SJulian Elischer printf("[%d] ", i++); 331430400f03SJulian Elischer dumpnode(node, NULL, 0); 331530400f03SJulian Elischer } 331653f9c5e9SRobert Watson mtx_unlock(&ng_nodelist_mtx); 331730400f03SJulian Elischer } 331830400f03SJulian Elischer 331930400f03SJulian Elischer static void 332030400f03SJulian Elischer ng_dumphooks(void) 332130400f03SJulian Elischer { 332230400f03SJulian Elischer hook_p hook; 332330400f03SJulian Elischer int i = 1; 332453f9c5e9SRobert Watson mtx_lock(&ng_nodelist_mtx); 332530400f03SJulian Elischer SLIST_FOREACH(hook, &ng_allhooks, hk_all) { 332630400f03SJulian Elischer printf("[%d] ", i++); 332730400f03SJulian Elischer dumphook(hook, NULL, 0); 332830400f03SJulian Elischer } 332953f9c5e9SRobert Watson mtx_unlock(&ng_nodelist_mtx); 333030400f03SJulian Elischer } 3331069154d5SJulian Elischer 3332069154d5SJulian Elischer static int 3333069154d5SJulian Elischer sysctl_debug_ng_dump_items(SYSCTL_HANDLER_ARGS) 3334069154d5SJulian Elischer { 3335069154d5SJulian Elischer int error; 3336069154d5SJulian Elischer int val; 3337069154d5SJulian Elischer int i; 3338069154d5SJulian Elischer 3339069154d5SJulian Elischer val = allocated; 3340069154d5SJulian Elischer i = 1; 3341041b706bSDavid Malone error = sysctl_handle_int(oidp, &val, 0, req); 33426b795970SJulian Elischer if (error != 0 || req->newptr == NULL) 33436b795970SJulian Elischer return (error); 33446b795970SJulian Elischer if (val == 42) { 334530400f03SJulian Elischer ng_dumpitems(); 334630400f03SJulian Elischer ng_dumpnodes(); 334730400f03SJulian Elischer ng_dumphooks(); 3348069154d5SJulian Elischer } 33496b795970SJulian Elischer return (0); 3350069154d5SJulian Elischer } 3351069154d5SJulian Elischer 33526b795970SJulian Elischer SYSCTL_PROC(_debug, OID_AUTO, ng_dump_items, CTLTYPE_INT | CTLFLAG_RW, 33536b795970SJulian Elischer 0, sizeof(int), sysctl_debug_ng_dump_items, "I", "Number of allocated items"); 335430400f03SJulian Elischer #endif /* NETGRAPH_DEBUG */ 3355069154d5SJulian Elischer 3356069154d5SJulian Elischer 3357069154d5SJulian Elischer /*********************************************************************** 3358069154d5SJulian Elischer * Worklist routines 3359069154d5SJulian Elischer **********************************************************************/ 3360069154d5SJulian Elischer /* NETISR thread enters here */ 3361069154d5SJulian Elischer /* 3362069154d5SJulian Elischer * Pick a node off the list of nodes with work, 3363069154d5SJulian Elischer * try get an item to process off it. 3364069154d5SJulian Elischer * If there are no more, remove the node from the list. 3365069154d5SJulian Elischer */ 3366069154d5SJulian Elischer static void 3367069154d5SJulian Elischer ngintr(void) 3368069154d5SJulian Elischer { 3369069154d5SJulian Elischer item_p item; 3370069154d5SJulian Elischer node_p node = NULL; 3371069154d5SJulian Elischer 3372069154d5SJulian Elischer for (;;) { 33732c8dda8dSWojciech A. Koszek NG_WORKLIST_LOCK(); 3374069154d5SJulian Elischer node = TAILQ_FIRST(&ng_worklist); 3375069154d5SJulian Elischer if (!node) { 33762c8dda8dSWojciech A. Koszek NG_WORKLIST_UNLOCK(); 3377069154d5SJulian Elischer break; 3378069154d5SJulian Elischer } 3379be4252b3SJulian Elischer node->nd_flags &= ~NGF_WORKQ; 338030400f03SJulian Elischer TAILQ_REMOVE(&ng_worklist, node, nd_work); 33812c8dda8dSWojciech A. Koszek NG_WORKLIST_UNLOCK(); 33822955ee18SGleb Smirnoff CTR3(KTR_NET, "%20s: node [%x] (%p) taken off worklist", 33832955ee18SGleb Smirnoff __func__, node->nd_ID, node); 3384069154d5SJulian Elischer /* 3385069154d5SJulian Elischer * We have the node. We also take over the reference 3386069154d5SJulian Elischer * that the list had on it. 3387069154d5SJulian Elischer * Now process as much as you can, until it won't 3388069154d5SJulian Elischer * let you have another item off the queue. 3389069154d5SJulian Elischer * All this time, keep the reference 3390069154d5SJulian Elischer * that lets us be sure that the node still exists. 3391069154d5SJulian Elischer * Let the reference go at the last minute. 3392b57a7965SJulian Elischer * ng_dequeue will put us back on the worklist 3393b57a7965SJulian Elischer * if there is more too do. This may be of use if there 3394b57a7965SJulian Elischer * are Multiple Processors and multiple Net threads in the 3395b57a7965SJulian Elischer * future. 3396069154d5SJulian Elischer */ 3397069154d5SJulian Elischer for (;;) { 3398714fb865SGleb Smirnoff int rw; 3399714fb865SGleb Smirnoff 34002c8dda8dSWojciech A. Koszek NG_QUEUE_LOCK(&node->nd_input_queue); 3401714fb865SGleb Smirnoff item = ng_dequeue(&node->nd_input_queue, &rw); 3402069154d5SJulian Elischer if (item == NULL) { 34032c8dda8dSWojciech A. Koszek NG_QUEUE_UNLOCK(&node->nd_input_queue); 3404069154d5SJulian Elischer break; /* go look for another node */ 3405069154d5SJulian Elischer } else { 34062c8dda8dSWojciech A. Koszek NG_QUEUE_UNLOCK(&node->nd_input_queue); 34075951069aSJulian Elischer NGI_GET_NODE(item, node); /* zaps stored node */ 3408714fb865SGleb Smirnoff ng_apply_item(node, item, rw); 34095951069aSJulian Elischer NG_NODE_UNREF(node); 3410069154d5SJulian Elischer } 3411069154d5SJulian Elischer } 3412a96dcd84SJulian Elischer NG_NODE_UNREF(node); 3413069154d5SJulian Elischer } 3414069154d5SJulian Elischer } 3415069154d5SJulian Elischer 3416069154d5SJulian Elischer static void 3417069154d5SJulian Elischer ng_worklist_remove(node_p node) 3418069154d5SJulian Elischer { 34193b33fbe7SGleb Smirnoff mtx_assert(&node->nd_input_queue.q_mtx, MA_OWNED); 34203b33fbe7SGleb Smirnoff 34212c8dda8dSWojciech A. Koszek NG_WORKLIST_LOCK(); 3422be4252b3SJulian Elischer if (node->nd_flags & NGF_WORKQ) { 3423be4252b3SJulian Elischer node->nd_flags &= ~NGF_WORKQ; 342433338e73SJulian Elischer TAILQ_REMOVE(&ng_worklist, node, nd_work); 34252c8dda8dSWojciech A. Koszek NG_WORKLIST_UNLOCK(); 342633338e73SJulian Elischer NG_NODE_UNREF(node); 34272955ee18SGleb Smirnoff CTR3(KTR_NET, "%20s: node [%x] (%p) removed from worklist", 34282955ee18SGleb Smirnoff __func__, node->nd_ID, node); 342933338e73SJulian Elischer } else { 34302c8dda8dSWojciech A. Koszek NG_WORKLIST_UNLOCK(); 3431069154d5SJulian Elischer } 343233338e73SJulian Elischer } 3433069154d5SJulian Elischer 343433338e73SJulian Elischer /* 343533338e73SJulian Elischer * XXX 343633338e73SJulian Elischer * It's posible that a debugging NG_NODE_REF may need 343733338e73SJulian Elischer * to be outside the mutex zone 343833338e73SJulian Elischer */ 3439069154d5SJulian Elischer static void 3440069154d5SJulian Elischer ng_setisr(node_p node) 3441069154d5SJulian Elischer { 3442f912c0f7SGleb Smirnoff 34435bc15201SGleb Smirnoff mtx_assert(&node->nd_input_queue.q_mtx, MA_OWNED); 3444f912c0f7SGleb Smirnoff 3445be4252b3SJulian Elischer if ((node->nd_flags & NGF_WORKQ) == 0) { 3446069154d5SJulian Elischer /* 3447069154d5SJulian Elischer * If we are not already on the work queue, 3448069154d5SJulian Elischer * then put us on. 3449069154d5SJulian Elischer */ 3450be4252b3SJulian Elischer node->nd_flags |= NGF_WORKQ; 34512c8dda8dSWojciech A. Koszek NG_WORKLIST_LOCK(); 345230400f03SJulian Elischer TAILQ_INSERT_TAIL(&ng_worklist, node, nd_work); 34532c8dda8dSWojciech A. Koszek NG_WORKLIST_UNLOCK(); 345433338e73SJulian Elischer NG_NODE_REF(node); /* XXX fafe in mutex? */ 34552955ee18SGleb Smirnoff CTR3(KTR_NET, "%20s: node [%x] (%p) put on worklist", __func__, 34562955ee18SGleb Smirnoff node->nd_ID, node); 34573b33fbe7SGleb Smirnoff } else 34582955ee18SGleb Smirnoff CTR3(KTR_NET, "%20s: node [%x] (%p) already on worklist", 34592955ee18SGleb Smirnoff __func__, node->nd_ID, node); 3460069154d5SJulian Elischer schednetisr(NETISR_NETGRAPH); 3461069154d5SJulian Elischer } 3462069154d5SJulian Elischer 3463069154d5SJulian Elischer 3464069154d5SJulian Elischer /*********************************************************************** 3465069154d5SJulian Elischer * Externally useable functions to set up a queue item ready for sending 3466069154d5SJulian Elischer ***********************************************************************/ 3467069154d5SJulian Elischer 346830400f03SJulian Elischer #ifdef NETGRAPH_DEBUG 346930400f03SJulian Elischer #define ITEM_DEBUG_CHECKS \ 34704cf49a43SJulian Elischer do { \ 34711acb27c6SJulian Elischer if (NGI_NODE(item) ) { \ 3472069154d5SJulian Elischer printf("item already has node"); \ 34733de213ccSRobert Watson kdb_enter(KDB_WHY_NETGRAPH, "has node"); \ 34741acb27c6SJulian Elischer NGI_CLR_NODE(item); \ 3475069154d5SJulian Elischer } \ 34761acb27c6SJulian Elischer if (NGI_HOOK(item) ) { \ 3477069154d5SJulian Elischer printf("item already has hook"); \ 34783de213ccSRobert Watson kdb_enter(KDB_WHY_NETGRAPH, "has hook"); \ 34791acb27c6SJulian Elischer NGI_CLR_HOOK(item); \ 3480069154d5SJulian Elischer } \ 3481069154d5SJulian Elischer } while (0) 3482069154d5SJulian Elischer #else 348330400f03SJulian Elischer #define ITEM_DEBUG_CHECKS 3484069154d5SJulian Elischer #endif 3485069154d5SJulian Elischer 3486069154d5SJulian Elischer /* 34878ed370fdSJulian Elischer * Put mbuf into the item. 3488069154d5SJulian Elischer * Hook and node references will be removed when the item is dequeued. 3489069154d5SJulian Elischer * (or equivalent) 3490069154d5SJulian Elischer * (XXX) Unsafe because no reference held by peer on remote node. 3491069154d5SJulian Elischer * remote node might go away in this timescale. 3492069154d5SJulian Elischer * We know the hooks can't go away because that would require getting 3493069154d5SJulian Elischer * a writer item on both nodes and we must have at least a reader 34944be59335SGleb Smirnoff * here to be able to do this. 3495069154d5SJulian Elischer * Note that the hook loaded is the REMOTE hook. 3496069154d5SJulian Elischer * 3497069154d5SJulian Elischer * This is possibly in the critical path for new data. 3498069154d5SJulian Elischer */ 3499069154d5SJulian Elischer item_p 350042282202SGleb Smirnoff ng_package_data(struct mbuf *m, int flags) 3501069154d5SJulian Elischer { 3502069154d5SJulian Elischer item_p item; 3503069154d5SJulian Elischer 350442282202SGleb Smirnoff if ((item = ng_getqblk(flags)) == NULL) { 3505069154d5SJulian Elischer NG_FREE_M(m); 3506069154d5SJulian Elischer return (NULL); 3507069154d5SJulian Elischer } 350830400f03SJulian Elischer ITEM_DEBUG_CHECKS; 35096f683eeeSGleb Smirnoff item->el_flags = NGQF_DATA | NGQF_READER; 3510069154d5SJulian Elischer item->el_next = NULL; 3511069154d5SJulian Elischer NGI_M(item) = m; 3512069154d5SJulian Elischer return (item); 3513069154d5SJulian Elischer } 3514069154d5SJulian Elischer 3515069154d5SJulian Elischer /* 3516069154d5SJulian Elischer * Allocate a queue item and put items into it.. 3517069154d5SJulian Elischer * Evaluate the address as this will be needed to queue it and 3518069154d5SJulian Elischer * to work out what some of the fields should be. 3519069154d5SJulian Elischer * Hook and node references will be removed when the item is dequeued. 3520069154d5SJulian Elischer * (or equivalent) 3521069154d5SJulian Elischer */ 3522069154d5SJulian Elischer item_p 352342282202SGleb Smirnoff ng_package_msg(struct ng_mesg *msg, int flags) 3524069154d5SJulian Elischer { 3525069154d5SJulian Elischer item_p item; 3526069154d5SJulian Elischer 352742282202SGleb Smirnoff if ((item = ng_getqblk(flags)) == NULL) { 3528069154d5SJulian Elischer NG_FREE_MSG(msg); 3529069154d5SJulian Elischer return (NULL); 3530069154d5SJulian Elischer } 353130400f03SJulian Elischer ITEM_DEBUG_CHECKS; 35326f683eeeSGleb Smirnoff /* Messages items count as writers unless explicitly exempted. */ 35336f683eeeSGleb Smirnoff if (msg->header.cmd & NGM_READONLY) 35346f683eeeSGleb Smirnoff item->el_flags = NGQF_MESG | NGQF_READER; 35356f683eeeSGleb Smirnoff else 35366f683eeeSGleb Smirnoff item->el_flags = NGQF_MESG | NGQF_WRITER; 3537069154d5SJulian Elischer item->el_next = NULL; 3538069154d5SJulian Elischer /* 3539069154d5SJulian Elischer * Set the current lasthook into the queue item 3540069154d5SJulian Elischer */ 3541069154d5SJulian Elischer NGI_MSG(item) = msg; 3542facfd889SArchie Cobbs NGI_RETADDR(item) = 0; 3543069154d5SJulian Elischer return (item); 3544069154d5SJulian Elischer } 3545069154d5SJulian Elischer 3546069154d5SJulian Elischer 3547069154d5SJulian Elischer 35481acb27c6SJulian Elischer #define SET_RETADDR(item, here, retaddr) \ 35496b795970SJulian Elischer do { /* Data or fn items don't have retaddrs */ \ 35506b795970SJulian Elischer if ((item->el_flags & NGQF_TYPE) == NGQF_MESG) { \ 3551069154d5SJulian Elischer if (retaddr) { \ 3552069154d5SJulian Elischer NGI_RETADDR(item) = retaddr; \ 35534cf49a43SJulian Elischer } else { \ 3554069154d5SJulian Elischer /* \ 3555069154d5SJulian Elischer * The old return address should be ok. \ 3556069154d5SJulian Elischer * If there isn't one, use the address \ 3557069154d5SJulian Elischer * here. \ 3558069154d5SJulian Elischer */ \ 3559069154d5SJulian Elischer if (NGI_RETADDR(item) == 0) { \ 3560069154d5SJulian Elischer NGI_RETADDR(item) \ 3561069154d5SJulian Elischer = ng_node2ID(here); \ 3562069154d5SJulian Elischer } \ 3563069154d5SJulian Elischer } \ 35644cf49a43SJulian Elischer } \ 35654cf49a43SJulian Elischer } while (0) 35664cf49a43SJulian Elischer 35674cf49a43SJulian Elischer int 3568069154d5SJulian Elischer ng_address_hook(node_p here, item_p item, hook_p hook, ng_ID_t retaddr) 35694cf49a43SJulian Elischer { 35701acb27c6SJulian Elischer hook_p peer; 35711acb27c6SJulian Elischer node_p peernode; 357230400f03SJulian Elischer ITEM_DEBUG_CHECKS; 3573069154d5SJulian Elischer /* 3574069154d5SJulian Elischer * Quick sanity check.. 357530400f03SJulian Elischer * Since a hook holds a reference on it's node, once we know 357630400f03SJulian Elischer * that the peer is still connected (even if invalid,) we know 357730400f03SJulian Elischer * that the peer node is present, though maybe invalid. 3578069154d5SJulian Elischer */ 3579069154d5SJulian Elischer if ((hook == NULL) 358030400f03SJulian Elischer || NG_HOOK_NOT_VALID(hook) 358130400f03SJulian Elischer || (NG_HOOK_PEER(hook) == NULL) 358230400f03SJulian Elischer || NG_HOOK_NOT_VALID(NG_HOOK_PEER(hook)) 358330400f03SJulian Elischer || NG_NODE_NOT_VALID(NG_PEER_NODE(hook))) { 3584069154d5SJulian Elischer NG_FREE_ITEM(item); 35856b795970SJulian Elischer TRAP_ERROR(); 3586e08d3e3cSJulian Elischer return (ENETDOWN); 35874cf49a43SJulian Elischer } 35884cf49a43SJulian Elischer 35894cf49a43SJulian Elischer /* 3590069154d5SJulian Elischer * Transfer our interest to the other (peer) end. 35914cf49a43SJulian Elischer */ 35921acb27c6SJulian Elischer peer = NG_HOOK_PEER(hook); 35931acb27c6SJulian Elischer NG_HOOK_REF(peer); 35941acb27c6SJulian Elischer NGI_SET_HOOK(item, peer); 35951acb27c6SJulian Elischer peernode = NG_PEER_NODE(hook); 35961acb27c6SJulian Elischer NG_NODE_REF(peernode); 35971acb27c6SJulian Elischer NGI_SET_NODE(item, peernode); 35988b68f82fSJulian Elischer SET_RETADDR(item, here, retaddr); 3599069154d5SJulian Elischer return (0); 3600069154d5SJulian Elischer } 3601069154d5SJulian Elischer 36024cf49a43SJulian Elischer int 3603069154d5SJulian Elischer ng_address_path(node_p here, item_p item, char *address, ng_ID_t retaddr) 36044cf49a43SJulian Elischer { 36054cf49a43SJulian Elischer node_p dest = NULL; 3606069154d5SJulian Elischer hook_p hook = NULL; 36074cf49a43SJulian Elischer int error; 3608069154d5SJulian Elischer 360930400f03SJulian Elischer ITEM_DEBUG_CHECKS; 3610069154d5SJulian Elischer /* 3611069154d5SJulian Elischer * Note that ng_path2noderef increments the reference count 3612069154d5SJulian Elischer * on the node for us if it finds one. So we don't have to. 3613069154d5SJulian Elischer */ 3614069154d5SJulian Elischer error = ng_path2noderef(here, address, &dest, &hook); 3615069154d5SJulian Elischer if (error) { 3616069154d5SJulian Elischer NG_FREE_ITEM(item); 361730400f03SJulian Elischer return (error); 3618069154d5SJulian Elischer } 36191acb27c6SJulian Elischer NGI_SET_NODE(item, dest); 36201acb27c6SJulian Elischer if ( hook) { 362130400f03SJulian Elischer NG_HOOK_REF(hook); /* don't let it go while on the queue */ 36221acb27c6SJulian Elischer NGI_SET_HOOK(item, hook); 36231acb27c6SJulian Elischer } 36241acb27c6SJulian Elischer SET_RETADDR(item, here, retaddr); 3625069154d5SJulian Elischer return (0); 3626069154d5SJulian Elischer } 3627069154d5SJulian Elischer 3628069154d5SJulian Elischer int 3629069154d5SJulian Elischer ng_address_ID(node_p here, item_p item, ng_ID_t ID, ng_ID_t retaddr) 3630069154d5SJulian Elischer { 3631069154d5SJulian Elischer node_p dest; 3632069154d5SJulian Elischer 363330400f03SJulian Elischer ITEM_DEBUG_CHECKS; 3634069154d5SJulian Elischer /* 3635069154d5SJulian Elischer * Find the target node. 3636069154d5SJulian Elischer */ 3637069154d5SJulian Elischer dest = ng_ID2noderef(ID); /* GETS REFERENCE! */ 3638069154d5SJulian Elischer if (dest == NULL) { 3639069154d5SJulian Elischer NG_FREE_ITEM(item); 36406b795970SJulian Elischer TRAP_ERROR(); 3641069154d5SJulian Elischer return(EINVAL); 3642069154d5SJulian Elischer } 3643069154d5SJulian Elischer /* Fill out the contents */ 36441acb27c6SJulian Elischer NGI_SET_NODE(item, dest); 36451acb27c6SJulian Elischer NGI_CLR_HOOK(item); 36461acb27c6SJulian Elischer SET_RETADDR(item, here, retaddr); 3647069154d5SJulian Elischer return (0); 3648069154d5SJulian Elischer } 3649069154d5SJulian Elischer 3650069154d5SJulian Elischer /* 3651069154d5SJulian Elischer * special case to send a message to self (e.g. destroy node) 3652069154d5SJulian Elischer * Possibly indicate an arrival hook too. 3653069154d5SJulian Elischer * Useful for removing that hook :-) 3654069154d5SJulian Elischer */ 3655069154d5SJulian Elischer item_p 3656069154d5SJulian Elischer ng_package_msg_self(node_p here, hook_p hook, struct ng_mesg *msg) 3657069154d5SJulian Elischer { 3658069154d5SJulian Elischer item_p item; 36594cf49a43SJulian Elischer 3660859a4d16SJulian Elischer /* 3661859a4d16SJulian Elischer * Find the target node. 3662859a4d16SJulian Elischer * If there is a HOOK argument, then use that in preference 3663859a4d16SJulian Elischer * to the address. 3664859a4d16SJulian Elischer */ 366542282202SGleb Smirnoff if ((item = ng_getqblk(NG_NOFLAGS)) == NULL) { 3666069154d5SJulian Elischer NG_FREE_MSG(msg); 3667069154d5SJulian Elischer return (NULL); 36684cf49a43SJulian Elischer } 36694cf49a43SJulian Elischer 36704cf49a43SJulian Elischer /* Fill out the contents */ 36716f683eeeSGleb Smirnoff item->el_flags = NGQF_MESG | NGQF_WRITER; 3672069154d5SJulian Elischer item->el_next = NULL; 367330400f03SJulian Elischer NG_NODE_REF(here); 36741acb27c6SJulian Elischer NGI_SET_NODE(item, here); 36751acb27c6SJulian Elischer if (hook) { 367630400f03SJulian Elischer NG_HOOK_REF(hook); 36771acb27c6SJulian Elischer NGI_SET_HOOK(item, hook); 36781acb27c6SJulian Elischer } 3679069154d5SJulian Elischer NGI_MSG(item) = msg; 3680069154d5SJulian Elischer NGI_RETADDR(item) = ng_node2ID(here); 3681069154d5SJulian Elischer return (item); 36824cf49a43SJulian Elischer } 36834cf49a43SJulian Elischer 3684e088dd4cSAlexander Motin /* 3685e088dd4cSAlexander Motin * Send ng_item_fn function call to the specified node. 3686e088dd4cSAlexander Motin */ 3687e088dd4cSAlexander Motin 368842282202SGleb Smirnoff int 3689b332b91fSGleb Smirnoff ng_send_fn(node_p node, hook_p hook, ng_item_fn *fn, void * arg1, int arg2) 3690b332b91fSGleb Smirnoff { 3691b332b91fSGleb Smirnoff 3692b332b91fSGleb Smirnoff return ng_send_fn1(node, hook, fn, arg1, arg2, NG_NOFLAGS); 3693b332b91fSGleb Smirnoff } 3694b332b91fSGleb Smirnoff 3695b332b91fSGleb Smirnoff int 3696aacdb114SGleb Smirnoff ng_send_fn1(node_p node, hook_p hook, ng_item_fn *fn, void * arg1, int arg2, 369742282202SGleb Smirnoff int flags) 36986b795970SJulian Elischer { 36996b795970SJulian Elischer item_p item; 37006b795970SJulian Elischer 370142282202SGleb Smirnoff if ((item = ng_getqblk(flags)) == NULL) { 37026b795970SJulian Elischer return (ENOMEM); 37036b795970SJulian Elischer } 37046b795970SJulian Elischer item->el_flags = NGQF_FN | NGQF_WRITER; 3705a96dcd84SJulian Elischer NG_NODE_REF(node); /* and one for the item */ 37061acb27c6SJulian Elischer NGI_SET_NODE(item, node); 37071acb27c6SJulian Elischer if (hook) { 37086b795970SJulian Elischer NG_HOOK_REF(hook); 37091acb27c6SJulian Elischer NGI_SET_HOOK(item, hook); 37106b795970SJulian Elischer } 37116b795970SJulian Elischer NGI_FN(item) = fn; 37126b795970SJulian Elischer NGI_ARG1(item) = arg1; 37136b795970SJulian Elischer NGI_ARG2(item) = arg2; 371442282202SGleb Smirnoff return(ng_snd_item(item, flags)); 37156b795970SJulian Elischer } 37166b795970SJulian Elischer 37174cf49a43SJulian Elischer /* 3718b332b91fSGleb Smirnoff * Send ng_item_fn2 function call to the specified node. 3719b332b91fSGleb Smirnoff * 3720b332b91fSGleb Smirnoff * If an optional pitem parameter is supplied, its apply 3721b332b91fSGleb Smirnoff * callback will be copied to the new item. If also NG_REUSE_ITEM 3722b332b91fSGleb Smirnoff * flag is set, no new item will be allocated, but pitem will 3723b332b91fSGleb Smirnoff * be used. 3724e088dd4cSAlexander Motin */ 3725e088dd4cSAlexander Motin int 3726b332b91fSGleb Smirnoff ng_send_fn2(node_p node, hook_p hook, item_p pitem, ng_item_fn2 *fn, void *arg1, 3727b332b91fSGleb Smirnoff int arg2, int flags) 3728e088dd4cSAlexander Motin { 3729e088dd4cSAlexander Motin item_p item; 3730e088dd4cSAlexander Motin 3731b332b91fSGleb Smirnoff KASSERT((pitem != NULL || (flags & NG_REUSE_ITEM) == 0), 3732b332b91fSGleb Smirnoff ("%s: NG_REUSE_ITEM but no pitem", __func__)); 3733e088dd4cSAlexander Motin 3734e088dd4cSAlexander Motin /* 3735b332b91fSGleb Smirnoff * Allocate a new item if no supplied or 3736b332b91fSGleb Smirnoff * if we can't use supplied one. 3737e088dd4cSAlexander Motin */ 3738b332b91fSGleb Smirnoff if (pitem == NULL || (flags & NG_REUSE_ITEM) == 0) { 3739b332b91fSGleb Smirnoff if ((item = ng_getqblk(flags)) == NULL) 3740e088dd4cSAlexander Motin return (ENOMEM); 3741b332b91fSGleb Smirnoff } else 3742b332b91fSGleb Smirnoff item = pitem; 3743b332b91fSGleb Smirnoff 3744e088dd4cSAlexander Motin item->el_flags = NGQF_FN2 | NGQF_WRITER; 3745e088dd4cSAlexander Motin NG_NODE_REF(node); /* and one for the item */ 3746e088dd4cSAlexander Motin NGI_SET_NODE(item, node); 3747e088dd4cSAlexander Motin if (hook) { 3748e088dd4cSAlexander Motin NG_HOOK_REF(hook); 3749e088dd4cSAlexander Motin NGI_SET_HOOK(item, hook); 3750e088dd4cSAlexander Motin } 3751e088dd4cSAlexander Motin NGI_FN2(item) = fn; 3752e088dd4cSAlexander Motin NGI_ARG1(item) = arg1; 3753e088dd4cSAlexander Motin NGI_ARG2(item) = arg2; 3754b332b91fSGleb Smirnoff if (pitem != NULL && (flags & NG_REUSE_ITEM) == 0) 3755e088dd4cSAlexander Motin item->apply = pitem->apply; 3756e088dd4cSAlexander Motin return(ng_snd_item(item, flags)); 3757e088dd4cSAlexander Motin } 3758e088dd4cSAlexander Motin 3759e088dd4cSAlexander Motin /* 3760d2ca21a9SJulian Elischer * Official timeout routines for Netgraph nodes. 3761d2ca21a9SJulian Elischer */ 3762d2ca21a9SJulian Elischer static void 37631fbb36ffSGleb Smirnoff ng_callout_trampoline(void *arg) 3764d2ca21a9SJulian Elischer { 3765d2ca21a9SJulian Elischer item_p item = arg; 3766d2ca21a9SJulian Elischer 3767d2ca21a9SJulian Elischer ng_snd_item(item, 0); 3768d2ca21a9SJulian Elischer } 3769d2ca21a9SJulian Elischer 3770d2ca21a9SJulian Elischer 377130bef41bSGleb Smirnoff int 3772f9d9e1b4SGleb Smirnoff ng_callout(struct callout *c, node_p node, hook_p hook, int ticks, 3773d2ca21a9SJulian Elischer ng_item_fn *fn, void * arg1, int arg2) 3774d2ca21a9SJulian Elischer { 37751bf8e0faSGleb Smirnoff item_p item, oitem; 3776d2ca21a9SJulian Elischer 377742282202SGleb Smirnoff if ((item = ng_getqblk(NG_NOFLAGS)) == NULL) 377830bef41bSGleb Smirnoff return (ENOMEM); 377930bef41bSGleb Smirnoff 3780d2ca21a9SJulian Elischer item->el_flags = NGQF_FN | NGQF_WRITER; 3781d2ca21a9SJulian Elischer NG_NODE_REF(node); /* and one for the item */ 3782d2ca21a9SJulian Elischer NGI_SET_NODE(item, node); 3783d2ca21a9SJulian Elischer if (hook) { 3784d2ca21a9SJulian Elischer NG_HOOK_REF(hook); 3785d2ca21a9SJulian Elischer NGI_SET_HOOK(item, hook); 3786d2ca21a9SJulian Elischer } 3787d2ca21a9SJulian Elischer NGI_FN(item) = fn; 3788d2ca21a9SJulian Elischer NGI_ARG1(item) = arg1; 3789d2ca21a9SJulian Elischer NGI_ARG2(item) = arg2; 37901bf8e0faSGleb Smirnoff oitem = c->c_arg; 37911bf8e0faSGleb Smirnoff if (callout_reset(c, ticks, &ng_callout_trampoline, item) == 1 && 37921bf8e0faSGleb Smirnoff oitem != NULL) 37931bf8e0faSGleb Smirnoff NG_FREE_ITEM(oitem); 379430bef41bSGleb Smirnoff return (0); 3795d2ca21a9SJulian Elischer } 3796d2ca21a9SJulian Elischer 3797d2ca21a9SJulian Elischer /* A special modified version of untimeout() */ 3798d2ca21a9SJulian Elischer int 3799f9d9e1b4SGleb Smirnoff ng_uncallout(struct callout *c, node_p node) 3800d2ca21a9SJulian Elischer { 3801d2ca21a9SJulian Elischer item_p item; 380230bef41bSGleb Smirnoff int rval; 3803d2ca21a9SJulian Elischer 380403b25f5dSGleb Smirnoff KASSERT(c != NULL, ("ng_uncallout: NULL callout")); 380503b25f5dSGleb Smirnoff KASSERT(node != NULL, ("ng_uncallout: NULL node")); 380603b25f5dSGleb Smirnoff 38073eadb26dSGleb Smirnoff rval = callout_stop(c); 380830bef41bSGleb Smirnoff item = c->c_arg; 380930bef41bSGleb Smirnoff /* Do an extra check */ 38101fbb36ffSGleb Smirnoff if ((rval > 0) && (c->c_func == &ng_callout_trampoline) && 381130bef41bSGleb Smirnoff (NGI_NODE(item) == node)) { 3812d2ca21a9SJulian Elischer /* 3813d2ca21a9SJulian Elischer * We successfully removed it from the queue before it ran 3814d2ca21a9SJulian Elischer * So now we need to unreference everything that was 3815d2ca21a9SJulian Elischer * given extra references. (NG_FREE_ITEM does this). 3816d2ca21a9SJulian Elischer */ 3817d2ca21a9SJulian Elischer NG_FREE_ITEM(item); 3818d2ca21a9SJulian Elischer } 38191bf8e0faSGleb Smirnoff c->c_arg = NULL; 382030bef41bSGleb Smirnoff 382130bef41bSGleb Smirnoff return (rval); 3822d2ca21a9SJulian Elischer } 3823d2ca21a9SJulian Elischer 3824d2ca21a9SJulian Elischer /* 3825069154d5SJulian Elischer * Set the address, if none given, give the node here. 38264cf49a43SJulian Elischer */ 3827069154d5SJulian Elischer void 3828069154d5SJulian Elischer ng_replace_retaddr(node_p here, item_p item, ng_ID_t retaddr) 38294cf49a43SJulian Elischer { 3830069154d5SJulian Elischer if (retaddr) { 3831069154d5SJulian Elischer NGI_RETADDR(item) = retaddr; 3832069154d5SJulian Elischer } else { 3833069154d5SJulian Elischer /* 3834069154d5SJulian Elischer * The old return address should be ok. 3835069154d5SJulian Elischer * If there isn't one, use the address here. 3836069154d5SJulian Elischer */ 3837069154d5SJulian Elischer NGI_RETADDR(item) = ng_node2ID(here); 3838069154d5SJulian Elischer } 3839069154d5SJulian Elischer } 3840069154d5SJulian Elischer 3841069154d5SJulian Elischer #define TESTING 3842069154d5SJulian Elischer #ifdef TESTING 3843069154d5SJulian Elischer /* just test all the macros */ 3844069154d5SJulian Elischer void 3845069154d5SJulian Elischer ng_macro_test(item_p item); 3846069154d5SJulian Elischer void 3847069154d5SJulian Elischer ng_macro_test(item_p item) 3848069154d5SJulian Elischer { 3849069154d5SJulian Elischer node_p node = NULL; 3850069154d5SJulian Elischer hook_p hook = NULL; 38514cf49a43SJulian Elischer struct mbuf *m; 38524cf49a43SJulian Elischer struct ng_mesg *msg; 3853069154d5SJulian Elischer ng_ID_t retaddr; 3854069154d5SJulian Elischer int error; 38554cf49a43SJulian Elischer 3856069154d5SJulian Elischer NGI_GET_M(item, m); 3857069154d5SJulian Elischer NGI_GET_MSG(item, msg); 3858069154d5SJulian Elischer retaddr = NGI_RETADDR(item); 38598ed370fdSJulian Elischer NG_SEND_DATA(error, hook, m, NULL); 3860069154d5SJulian Elischer NG_SEND_DATA_ONLY(error, hook, m); 3861069154d5SJulian Elischer NG_FWD_NEW_DATA(error, item, hook, m); 386230400f03SJulian Elischer NG_FWD_ITEM_HOOK(error, item, hook); 3863069154d5SJulian Elischer NG_SEND_MSG_HOOK(error, node, msg, hook, retaddr); 3864069154d5SJulian Elischer NG_SEND_MSG_ID(error, node, msg, retaddr, retaddr); 3865069154d5SJulian Elischer NG_SEND_MSG_PATH(error, node, msg, ".:", retaddr); 3866069154d5SJulian Elischer NG_FWD_MSG_HOOK(error, node, item, hook, retaddr); 38674cf49a43SJulian Elischer } 3868069154d5SJulian Elischer #endif /* TESTING */ 38694cf49a43SJulian Elischer 3870