The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/netgraph/ng_base.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*
    2  * ng_base.c
    3  */
    4 
    5 /*-
    6  * Copyright (c) 1996-1999 Whistle Communications, Inc.
    7  * All rights reserved.
    8  *
    9  * Subject to the following obligations and disclaimer of warranty, use and
   10  * redistribution of this software, in source or object code forms, with or
   11  * without modifications are expressly permitted by Whistle Communications;
   12  * provided, however, that:
   13  * 1. Any and all reproductions of the source or object code must include the
   14  *    copyright notice above and the following disclaimer of warranties; and
   15  * 2. No rights are granted, in any manner or form, to use Whistle
   16  *    Communications, Inc. trademarks, including the mark "WHISTLE
   17  *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
   18  *    such appears in the above copyright notice or in the software.
   19  *
   20  * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
   21  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
   22  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
   23  * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
   24  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
   25  * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
   26  * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
   27  * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
   28  * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
   29  * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
   30  * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
   31  * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
   32  * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
   33  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   34  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
   35  * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
   36  * OF SUCH DAMAGE.
   37  *
   38  * Authors: Julian Elischer <julian@freebsd.org>
   39  *          Archie Cobbs <archie@freebsd.org>
   40  *
   41  * $FreeBSD: releng/6.3/sys/netgraph/ng_base.c 173818 2007-11-21 12:14:00Z glebius $
   42  * $Whistle: ng_base.c,v 1.39 1999/01/28 23:54:53 julian Exp $
   43  */
   44 
   45 /*
   46  * This file implements the base netgraph code.
   47  */
   48 
   49 #include <sys/param.h>
   50 #include <sys/systm.h>
   51 #include <sys/ctype.h>
   52 #include <sys/errno.h>
   53 #include <sys/kdb.h>
   54 #include <sys/kernel.h>
   55 #include <sys/ktr.h>
   56 #include <sys/limits.h>
   57 #include <sys/malloc.h>
   58 #include <sys/mbuf.h>
   59 #include <sys/queue.h>
   60 #include <sys/sysctl.h>
   61 #include <sys/syslog.h>
   62 #include <sys/refcount.h>
   63 
   64 #include <net/netisr.h>
   65 
   66 #include <netgraph/ng_message.h>
   67 #include <netgraph/netgraph.h>
   68 #include <netgraph/ng_parse.h>
   69 
   70 MODULE_VERSION(netgraph, NG_ABI_VERSION);
   71 
   72 /* List of all active nodes */
   73 static LIST_HEAD(, ng_node) ng_nodelist;
   74 static struct mtx       ng_nodelist_mtx;
   75 
   76 /* Mutex to protect topology events. */
   77 static struct mtx       ng_topo_mtx;
   78 
   79 #ifdef  NETGRAPH_DEBUG
   80 static struct mtx       ngq_mtx;        /* protects the queue item list */
   81 
   82 static SLIST_HEAD(, ng_node) ng_allnodes;
   83 static LIST_HEAD(, ng_node) ng_freenodes; /* in debug, we never free() them */
   84 static SLIST_HEAD(, ng_hook) ng_allhooks;
   85 static LIST_HEAD(, ng_hook) ng_freehooks; /* in debug, we never free() them */
   86 
   87 static void ng_dumpitems(void);
   88 static void ng_dumpnodes(void);
   89 static void ng_dumphooks(void);
   90 
   91 #endif  /* NETGRAPH_DEBUG */
   92 /*
   93  * DEAD versions of the structures.
   94  * In order to avoid races, it is sometimes neccesary to point
   95  * at SOMETHING even though theoretically, the current entity is
   96  * INVALID. Use these to avoid these races.
   97  */
   98 struct ng_type ng_deadtype = {
   99         NG_ABI_VERSION,
  100         "dead",
  101         NULL,   /* modevent */
  102         NULL,   /* constructor */
  103         NULL,   /* rcvmsg */
  104         NULL,   /* shutdown */
  105         NULL,   /* newhook */
  106         NULL,   /* findhook */
  107         NULL,   /* connect */
  108         NULL,   /* rcvdata */
  109         NULL,   /* disconnect */
  110         NULL,   /* cmdlist */
  111 };
  112 
  113 struct ng_node ng_deadnode = {
  114         "dead",
  115         &ng_deadtype,   
  116         NGF_INVALID,
  117         1,      /* refs */
  118         0,      /* numhooks */
  119         NULL,   /* private */
  120         0,      /* ID */
  121         LIST_HEAD_INITIALIZER(ng_deadnode.hooks),
  122         {},     /* all_nodes list entry */
  123         {},     /* id hashtable list entry */
  124         {},     /* workqueue entry */
  125         {       0,
  126                 {}, /* should never use! (should hang) */
  127                 NULL,
  128                 &ng_deadnode.nd_input_queue.queue,
  129                 &ng_deadnode
  130         },
  131 #ifdef  NETGRAPH_DEBUG
  132         ND_MAGIC,
  133         __FILE__,
  134         __LINE__,
  135         {NULL}
  136 #endif  /* NETGRAPH_DEBUG */
  137 };
  138 
  139 struct ng_hook ng_deadhook = {
  140         "dead",
  141         NULL,           /* private */
  142         HK_INVALID | HK_DEAD,
  143         1,              /* refs always >= 1 */
  144         0,              /* undefined data link type */
  145         &ng_deadhook,   /* Peer is self */
  146         &ng_deadnode,   /* attached to deadnode */
  147         {},             /* hooks list */
  148         NULL,           /* override rcvmsg() */
  149         NULL,           /* override rcvdata() */
  150 #ifdef  NETGRAPH_DEBUG
  151         HK_MAGIC,
  152         __FILE__,
  153         __LINE__,
  154         {NULL}
  155 #endif  /* NETGRAPH_DEBUG */
  156 };
  157 
  158 /*
  159  * END DEAD STRUCTURES
  160  */
  161 /* List nodes with unallocated work */
  162 static TAILQ_HEAD(, ng_node) ng_worklist = TAILQ_HEAD_INITIALIZER(ng_worklist);
  163 static struct mtx       ng_worklist_mtx;   /* MUST LOCK NODE FIRST */
  164 
  165 /* List of installed types */
  166 static LIST_HEAD(, ng_type) ng_typelist;
  167 static struct mtx       ng_typelist_mtx;
  168 
  169 /* Hash related definitions */
  170 /* XXX Don't need to initialise them because it's a LIST */
  171 #define NG_ID_HASH_SIZE 32 /* most systems wont need even this many */
  172 static LIST_HEAD(, ng_node) ng_ID_hash[NG_ID_HASH_SIZE];
  173 static struct mtx       ng_idhash_mtx;
  174 /* Method to find a node.. used twice so do it here */
  175 #define NG_IDHASH_FN(ID) ((ID) % (NG_ID_HASH_SIZE))
  176 #define NG_IDHASH_FIND(ID, node)                                        \
  177         do {                                                            \
  178                 mtx_assert(&ng_idhash_mtx, MA_OWNED);                   \
  179                 LIST_FOREACH(node, &ng_ID_hash[NG_IDHASH_FN(ID)],       \
  180                                                 nd_idnodes) {           \
  181                         if (NG_NODE_IS_VALID(node)                      \
  182                         && (NG_NODE_ID(node) == ID)) {                  \
  183                                 break;                                  \
  184                         }                                               \
  185                 }                                                       \
  186         } while (0)
  187 
  188 
  189 /* Internal functions */
  190 static int      ng_add_hook(node_p node, const char *name, hook_p * hookp);
  191 static int      ng_generic_msg(node_p here, item_p item, hook_p lasthook);
  192 static ng_ID_t  ng_decodeidname(const char *name);
  193 static int      ngb_mod_event(module_t mod, int event, void *data);
  194 static void     ng_worklist_remove(node_p node);
  195 static void     ngintr(void);
  196 static int      ng_apply_item(node_p node, item_p item, int rw);
  197 static void     ng_flush_input_queue(struct ng_queue * ngq);
  198 static void     ng_setisr(node_p node);
  199 static node_p   ng_ID2noderef(ng_ID_t ID);
  200 static int      ng_con_nodes(item_p item, node_p node, const char *name,
  201                     node_p node2, const char *name2);
  202 static int      ng_con_part2(node_p node, item_p item, hook_p hook);
  203 static int      ng_con_part3(node_p node, item_p item, hook_p hook);
  204 static int      ng_mkpeer(node_p node, const char *name,
  205                                                 const char *name2, char *type);
  206 
  207 /* Imported, these used to be externally visible, some may go back. */
  208 void    ng_destroy_hook(hook_p hook);
  209 node_p  ng_name2noderef(node_p node, const char *name);
  210 int     ng_path2noderef(node_p here, const char *path,
  211         node_p *dest, hook_p *lasthook);
  212 int     ng_make_node(const char *type, node_p *nodepp);
  213 int     ng_path_parse(char *addr, char **node, char **path, char **hook);
  214 void    ng_rmnode(node_p node, hook_p dummy1, void *dummy2, int dummy3);
  215 void    ng_unname(node_p node);
  216 
  217 
  218 /* Our own netgraph malloc type */
  219 MALLOC_DEFINE(M_NETGRAPH, "netgraph", "netgraph structures and ctrl messages");
  220 MALLOC_DEFINE(M_NETGRAPH_HOOK, "netgraph_hook", "netgraph hook structures");
  221 MALLOC_DEFINE(M_NETGRAPH_NODE, "netgraph_node", "netgraph node structures");
  222 MALLOC_DEFINE(M_NETGRAPH_ITEM, "netgraph_item", "netgraph item structures");
  223 MALLOC_DEFINE(M_NETGRAPH_MSG, "netgraph_msg", "netgraph name storage");
  224 
  225 /* Should not be visible outside this file */
  226 
  227 #define _NG_ALLOC_HOOK(hook) \
  228         MALLOC(hook, hook_p, sizeof(*hook), M_NETGRAPH_HOOK, M_NOWAIT | M_ZERO)
  229 #define _NG_ALLOC_NODE(node) \
  230         MALLOC(node, node_p, sizeof(*node), M_NETGRAPH_NODE, M_NOWAIT | M_ZERO)
  231 
  232 #ifdef NETGRAPH_DEBUG /*----------------------------------------------*/
  233 /*
  234  * In debug mode:
  235  * In an attempt to help track reference count screwups
  236  * we do not free objects back to the malloc system, but keep them
  237  * in a local cache where we can examine them and keep information safely
  238  * after they have been freed.
  239  * We use this scheme for nodes and hooks, and to some extent for items.
  240  */
  241 static __inline hook_p
  242 ng_alloc_hook(void)
  243 {
  244         hook_p hook;
  245         SLIST_ENTRY(ng_hook) temp;
  246         mtx_lock(&ng_nodelist_mtx);
  247         hook = LIST_FIRST(&ng_freehooks);
  248         if (hook) {
  249                 LIST_REMOVE(hook, hk_hooks);
  250                 bcopy(&hook->hk_all, &temp, sizeof(temp));
  251                 bzero(hook, sizeof(struct ng_hook));
  252                 bcopy(&temp, &hook->hk_all, sizeof(temp));
  253                 mtx_unlock(&ng_nodelist_mtx);
  254                 hook->hk_magic = HK_MAGIC;
  255         } else {
  256                 mtx_unlock(&ng_nodelist_mtx);
  257                 _NG_ALLOC_HOOK(hook);
  258                 if (hook) {
  259                         hook->hk_magic = HK_MAGIC;
  260                         mtx_lock(&ng_nodelist_mtx);
  261                         SLIST_INSERT_HEAD(&ng_allhooks, hook, hk_all);
  262                         mtx_unlock(&ng_nodelist_mtx);
  263                 }
  264         }
  265         return (hook);
  266 }
  267 
  268 static __inline node_p
  269 ng_alloc_node(void)
  270 {
  271         node_p node;
  272         SLIST_ENTRY(ng_node) temp;
  273         mtx_lock(&ng_nodelist_mtx);
  274         node = LIST_FIRST(&ng_freenodes);
  275         if (node) {
  276                 LIST_REMOVE(node, nd_nodes);
  277                 bcopy(&node->nd_all, &temp, sizeof(temp));
  278                 bzero(node, sizeof(struct ng_node));
  279                 bcopy(&temp, &node->nd_all, sizeof(temp));
  280                 mtx_unlock(&ng_nodelist_mtx);
  281                 node->nd_magic = ND_MAGIC;
  282         } else {
  283                 mtx_unlock(&ng_nodelist_mtx);
  284                 _NG_ALLOC_NODE(node);
  285                 if (node) {
  286                         node->nd_magic = ND_MAGIC;
  287                         mtx_lock(&ng_nodelist_mtx);
  288                         SLIST_INSERT_HEAD(&ng_allnodes, node, nd_all);
  289                         mtx_unlock(&ng_nodelist_mtx);
  290                 }
  291         }
  292         return (node);
  293 }
  294 
  295 #define NG_ALLOC_HOOK(hook) do { (hook) = ng_alloc_hook(); } while (0)
  296 #define NG_ALLOC_NODE(node) do { (node) = ng_alloc_node(); } while (0)
  297 
  298 
  299 #define NG_FREE_HOOK(hook)                                              \
  300         do {                                                            \
  301                 mtx_lock(&ng_nodelist_mtx);                     \
  302                 LIST_INSERT_HEAD(&ng_freehooks, hook, hk_hooks);        \
  303                 hook->hk_magic = 0;                                     \
  304                 mtx_unlock(&ng_nodelist_mtx);                   \
  305         } while (0)
  306 
  307 #define NG_FREE_NODE(node)                                              \
  308         do {                                                            \
  309                 mtx_lock(&ng_nodelist_mtx);                     \
  310                 LIST_INSERT_HEAD(&ng_freenodes, node, nd_nodes);        \
  311                 node->nd_magic = 0;                                     \
  312                 mtx_unlock(&ng_nodelist_mtx);                   \
  313         } while (0)
  314 
  315 #else /* NETGRAPH_DEBUG */ /*----------------------------------------------*/
  316 
  317 #define NG_ALLOC_HOOK(hook) _NG_ALLOC_HOOK(hook)
  318 #define NG_ALLOC_NODE(node) _NG_ALLOC_NODE(node)
  319 
  320 #define NG_FREE_HOOK(hook) do { FREE((hook), M_NETGRAPH_HOOK); } while (0)
  321 #define NG_FREE_NODE(node) do { FREE((node), M_NETGRAPH_NODE); } while (0)
  322 
  323 #endif /* NETGRAPH_DEBUG */ /*----------------------------------------------*/
  324 
  325 /* Set this to kdb_enter("X") to catch all errors as they occur */
  326 #ifndef TRAP_ERROR
  327 #define TRAP_ERROR()
  328 #endif
  329 
  330 static  ng_ID_t nextID = 1;
  331 
  332 #ifdef INVARIANTS
  333 #define CHECK_DATA_MBUF(m)      do {                                    \
  334                 struct mbuf *n;                                         \
  335                 int total;                                              \
  336                                                                         \
  337                 M_ASSERTPKTHDR(m);                                      \
  338                 for (total = 0, n = (m); n != NULL; n = n->m_next) {    \
  339                         total += n->m_len;                              \
  340                         if (n->m_nextpkt != NULL)                       \
  341                                 panic("%s: m_nextpkt", __func__);       \
  342                 }                                                       \
  343                                                                         \
  344                 if ((m)->m_pkthdr.len != total) {                       \
  345                         panic("%s: %d != %d",                           \
  346                             __func__, (m)->m_pkthdr.len, total);        \
  347                 }                                                       \
  348         } while (0)
  349 #else
  350 #define CHECK_DATA_MBUF(m)
  351 #endif
  352 
  353 #define ERROUT(x)       do { error = (x); goto done; } while (0)
  354 
  355 /************************************************************************
  356         Parse type definitions for generic messages
  357 ************************************************************************/
  358 
  359 /* Handy structure parse type defining macro */
  360 #define DEFINE_PARSE_STRUCT_TYPE(lo, up, args)                          \
  361 static const struct ng_parse_struct_field                               \
  362         ng_ ## lo ## _type_fields[] = NG_GENERIC_ ## up ## _INFO args;  \
  363 static const struct ng_parse_type ng_generic_ ## lo ## _type = {        \
  364         &ng_parse_struct_type,                                          \
  365         &ng_ ## lo ## _type_fields                                      \
  366 }
  367 
  368 DEFINE_PARSE_STRUCT_TYPE(mkpeer, MKPEER, ());
  369 DEFINE_PARSE_STRUCT_TYPE(connect, CONNECT, ());
  370 DEFINE_PARSE_STRUCT_TYPE(name, NAME, ());
  371 DEFINE_PARSE_STRUCT_TYPE(rmhook, RMHOOK, ());
  372 DEFINE_PARSE_STRUCT_TYPE(nodeinfo, NODEINFO, ());
  373 DEFINE_PARSE_STRUCT_TYPE(typeinfo, TYPEINFO, ());
  374 DEFINE_PARSE_STRUCT_TYPE(linkinfo, LINKINFO, (&ng_generic_nodeinfo_type));
  375 
  376 /* Get length of an array when the length is stored as a 32 bit
  377    value immediately preceding the array -- as with struct namelist
  378    and struct typelist. */
  379 static int
  380 ng_generic_list_getLength(const struct ng_parse_type *type,
  381         const u_char *start, const u_char *buf)
  382 {
  383         return *((const u_int32_t *)(buf - 4));
  384 }
  385 
  386 /* Get length of the array of struct linkinfo inside a struct hooklist */
  387 static int
  388 ng_generic_linkinfo_getLength(const struct ng_parse_type *type,
  389         const u_char *start, const u_char *buf)
  390 {
  391         const struct hooklist *hl = (const struct hooklist *)start;
  392 
  393         return hl->nodeinfo.hooks;
  394 }
  395 
  396 /* Array type for a variable length array of struct namelist */
  397 static const struct ng_parse_array_info ng_nodeinfoarray_type_info = {
  398         &ng_generic_nodeinfo_type,
  399         &ng_generic_list_getLength
  400 };
  401 static const struct ng_parse_type ng_generic_nodeinfoarray_type = {
  402         &ng_parse_array_type,
  403         &ng_nodeinfoarray_type_info
  404 };
  405 
  406 /* Array type for a variable length array of struct typelist */
  407 static const struct ng_parse_array_info ng_typeinfoarray_type_info = {
  408         &ng_generic_typeinfo_type,
  409         &ng_generic_list_getLength
  410 };
  411 static const struct ng_parse_type ng_generic_typeinfoarray_type = {
  412         &ng_parse_array_type,
  413         &ng_typeinfoarray_type_info
  414 };
  415 
  416 /* Array type for array of struct linkinfo in struct hooklist */
  417 static const struct ng_parse_array_info ng_generic_linkinfo_array_type_info = {
  418         &ng_generic_linkinfo_type,
  419         &ng_generic_linkinfo_getLength
  420 };
  421 static const struct ng_parse_type ng_generic_linkinfo_array_type = {
  422         &ng_parse_array_type,
  423         &ng_generic_linkinfo_array_type_info
  424 };
  425 
  426 DEFINE_PARSE_STRUCT_TYPE(typelist, TYPELIST, (&ng_generic_nodeinfoarray_type));
  427 DEFINE_PARSE_STRUCT_TYPE(hooklist, HOOKLIST,
  428         (&ng_generic_nodeinfo_type, &ng_generic_linkinfo_array_type));
  429 DEFINE_PARSE_STRUCT_TYPE(listnodes, LISTNODES,
  430         (&ng_generic_nodeinfoarray_type));
  431 
  432 /* List of commands and how to convert arguments to/from ASCII */
  433 static const struct ng_cmdlist ng_generic_cmds[] = {
  434         {
  435           NGM_GENERIC_COOKIE,
  436           NGM_SHUTDOWN,
  437           "shutdown",
  438           NULL,
  439           NULL
  440         },
  441         {
  442           NGM_GENERIC_COOKIE,
  443           NGM_MKPEER,
  444           "mkpeer",
  445           &ng_generic_mkpeer_type,
  446           NULL
  447         },
  448         {
  449           NGM_GENERIC_COOKIE,
  450           NGM_CONNECT,
  451           "connect",
  452           &ng_generic_connect_type,
  453           NULL
  454         },
  455         {
  456           NGM_GENERIC_COOKIE,
  457           NGM_NAME,
  458           "name",
  459           &ng_generic_name_type,
  460           NULL
  461         },
  462         {
  463           NGM_GENERIC_COOKIE,
  464           NGM_RMHOOK,
  465           "rmhook",
  466           &ng_generic_rmhook_type,
  467           NULL
  468         },
  469         {
  470           NGM_GENERIC_COOKIE,
  471           NGM_NODEINFO,
  472           "nodeinfo",
  473           NULL,
  474           &ng_generic_nodeinfo_type
  475         },
  476         {
  477           NGM_GENERIC_COOKIE,
  478           NGM_LISTHOOKS,
  479           "listhooks",
  480           NULL,
  481           &ng_generic_hooklist_type
  482         },
  483         {
  484           NGM_GENERIC_COOKIE,
  485           NGM_LISTNAMES,
  486           "listnames",
  487           NULL,
  488           &ng_generic_listnodes_type    /* same as NGM_LISTNODES */
  489         },
  490         {
  491           NGM_GENERIC_COOKIE,
  492           NGM_LISTNODES,
  493           "listnodes",
  494           NULL,
  495           &ng_generic_listnodes_type
  496         },
  497         {
  498           NGM_GENERIC_COOKIE,
  499           NGM_LISTTYPES,
  500           "listtypes",
  501           NULL,
  502           &ng_generic_typeinfo_type
  503         },
  504         {
  505           NGM_GENERIC_COOKIE,
  506           NGM_TEXT_CONFIG,
  507           "textconfig",
  508           NULL,
  509           &ng_parse_string_type
  510         },
  511         {
  512           NGM_GENERIC_COOKIE,
  513           NGM_TEXT_STATUS,
  514           "textstatus",
  515           NULL,
  516           &ng_parse_string_type
  517         },
  518         {
  519           NGM_GENERIC_COOKIE,
  520           NGM_ASCII2BINARY,
  521           "ascii2binary",
  522           &ng_parse_ng_mesg_type,
  523           &ng_parse_ng_mesg_type
  524         },
  525         {
  526           NGM_GENERIC_COOKIE,
  527           NGM_BINARY2ASCII,
  528           "binary2ascii",
  529           &ng_parse_ng_mesg_type,
  530           &ng_parse_ng_mesg_type
  531         },
  532         { 0 }
  533 };
  534 
  535 /************************************************************************
  536                         Node routines
  537 ************************************************************************/
  538 
  539 /*
  540  * Instantiate a node of the requested type
  541  */
  542 int
  543 ng_make_node(const char *typename, node_p *nodepp)
  544 {
  545         struct ng_type *type;
  546         int     error;
  547 
  548         /* Check that the type makes sense */
  549         if (typename == NULL) {
  550                 TRAP_ERROR();
  551                 return (EINVAL);
  552         }
  553 
  554         /* Locate the node type. If we fail we return. Do not try to load
  555          * module.
  556          */
  557         if ((type = ng_findtype(typename)) == NULL)
  558                 return (ENXIO);
  559 
  560         /*
  561          * If we have a constructor, then make the node and
  562          * call the constructor to do type specific initialisation.
  563          */
  564         if (type->constructor != NULL) {
  565                 if ((error = ng_make_node_common(type, nodepp)) == 0) {
  566                         if ((error = ((*type->constructor)(*nodepp)) != 0)) {
  567                                 NG_NODE_UNREF(*nodepp);
  568                         }
  569                 }
  570         } else {
  571                 /*
  572                  * Node has no constructor. We cannot ask for one
  573                  * to be made. It must be brought into existence by
  574                  * some external agency. The external agency should
  575                  * call ng_make_node_common() directly to get the
  576                  * netgraph part initialised.
  577                  */
  578                 TRAP_ERROR();
  579                 error = EINVAL;
  580         }
  581         return (error);
  582 }
  583 
  584 /*
  585  * Generic node creation. Called by node initialisation for externally
  586  * instantiated nodes (e.g. hardware, sockets, etc ).
  587  * The returned node has a reference count of 1.
  588  */
  589 int
  590 ng_make_node_common(struct ng_type *type, node_p *nodepp)
  591 {
  592         node_p node;
  593 
  594         /* Require the node type to have been already installed */
  595         if (ng_findtype(type->name) == NULL) {
  596                 TRAP_ERROR();
  597                 return (EINVAL);
  598         }
  599 
  600         /* Make a node and try attach it to the type */
  601         NG_ALLOC_NODE(node);
  602         if (node == NULL) {
  603                 TRAP_ERROR();
  604                 return (ENOMEM);
  605         }
  606         node->nd_type = type;
  607         NG_NODE_REF(node);                              /* note reference */
  608         type->refs++;
  609 
  610         mtx_init(&node->nd_input_queue.q_mtx, "ng_node", NULL, MTX_SPIN);
  611         node->nd_input_queue.queue = NULL;
  612         node->nd_input_queue.last = &node->nd_input_queue.queue;
  613         node->nd_input_queue.q_flags = 0;
  614         node->nd_input_queue.q_node = node;
  615 
  616         /* Initialize hook list for new node */
  617         LIST_INIT(&node->nd_hooks);
  618 
  619         /* Link us into the node linked list */
  620         mtx_lock(&ng_nodelist_mtx);
  621         LIST_INSERT_HEAD(&ng_nodelist, node, nd_nodes);
  622         mtx_unlock(&ng_nodelist_mtx);
  623 
  624 
  625         /* get an ID and put us in the hash chain */
  626         mtx_lock(&ng_idhash_mtx);
  627         for (;;) { /* wrap protection, even if silly */
  628                 node_p node2 = NULL;
  629                 node->nd_ID = nextID++; /* 137/second for 1 year before wrap */
  630 
  631                 /* Is there a problem with the new number? */
  632                 NG_IDHASH_FIND(node->nd_ID, node2); /* already taken? */
  633                 if ((node->nd_ID != 0) && (node2 == NULL)) {
  634                         break;
  635                 }
  636         }
  637         LIST_INSERT_HEAD(&ng_ID_hash[NG_IDHASH_FN(node->nd_ID)],
  638                                                         node, nd_idnodes);
  639         mtx_unlock(&ng_idhash_mtx);
  640 
  641         /* Done */
  642         *nodepp = node;
  643         return (0);
  644 }
  645 
  646 /*
  647  * Forceably start the shutdown process on a node. Either call
  648  * its shutdown method, or do the default shutdown if there is
  649  * no type-specific method.
  650  *
  651  * We can only be called from a shutdown message, so we know we have
  652  * a writer lock, and therefore exclusive access. It also means
  653  * that we should not be on the work queue, but we check anyhow.
  654  *
  655  * Persistent node types must have a type-specific method which
  656  * allocates a new node in which case, this one is irretrievably going away,
  657  * or cleans up anything it needs, and just makes the node valid again,
  658  * in which case we allow the node to survive.
  659  *
  660  * XXX We need to think of how to tell a persistent node that we
  661  * REALLY need to go away because the hardware has gone or we
  662  * are rebooting.... etc.
  663  */
  664 void
  665 ng_rmnode(node_p node, hook_p dummy1, void *dummy2, int dummy3)
  666 {
  667         hook_p hook;
  668 
  669         /* Check if it's already shutting down */
  670         if ((node->nd_flags & NGF_CLOSING) != 0)
  671                 return;
  672 
  673         if (node == &ng_deadnode) {
  674                 printf ("shutdown called on deadnode\n");
  675                 return;
  676         }
  677 
  678         /* Add an extra reference so it doesn't go away during this */
  679         NG_NODE_REF(node);
  680 
  681         /*
  682          * Mark it invalid so any newcomers know not to try use it
  683          * Also add our own mark so we can't recurse
  684          * note that NGF_INVALID does not do this as it's also set during
  685          * creation
  686          */
  687         node->nd_flags |= NGF_INVALID|NGF_CLOSING;
  688 
  689         /* If node has its pre-shutdown method, then call it first*/
  690         if (node->nd_type && node->nd_type->close)
  691                 (*node->nd_type->close)(node);
  692 
  693         /* Notify all remaining connected nodes to disconnect */
  694         while ((hook = LIST_FIRST(&node->nd_hooks)) != NULL)
  695                 ng_destroy_hook(hook);
  696 
  697         /*
  698          * Drain the input queue forceably.
  699          * it has no hooks so what's it going to do, bleed on someone?
  700          * Theoretically we came here from a queue entry that was added
  701          * Just before the queue was closed, so it should be empty anyway.
  702          * Also removes us from worklist if needed.
  703          */
  704         ng_flush_input_queue(&node->nd_input_queue);
  705 
  706         /* Ask the type if it has anything to do in this case */
  707         if (node->nd_type && node->nd_type->shutdown) {
  708                 (*node->nd_type->shutdown)(node);
  709                 if (NG_NODE_IS_VALID(node)) {
  710                         /*
  711                          * Well, blow me down if the node code hasn't declared
  712                          * that it doesn't want to die.
  713                          * Presumably it is a persistant node.
  714                          * If we REALLY want it to go away,
  715                          *  e.g. hardware going away,
  716                          * Our caller should set NGF_REALLY_DIE in nd_flags.
  717                          */
  718                         node->nd_flags &= ~(NGF_INVALID|NGF_CLOSING);
  719                         NG_NODE_UNREF(node); /* Assume they still have theirs */
  720                         return;
  721                 }
  722         } else {                                /* do the default thing */
  723                 NG_NODE_UNREF(node);
  724         }
  725 
  726         ng_unname(node); /* basically a NOP these days */
  727 
  728         /*
  729          * Remove extra reference, possibly the last
  730          * Possible other holders of references may include
  731          * timeout callouts, but theoretically the node's supposed to
  732          * have cancelled them. Possibly hardware dependencies may
  733          * force a driver to 'linger' with a reference.
  734          */
  735         NG_NODE_UNREF(node);
  736 }
  737 
  738 /*
  739  * Remove a reference to the node, possibly the last.
  740  * deadnode always acts as it it were the last.
  741  */
  742 int
  743 ng_unref_node(node_p node)
  744 {
  745         int v;
  746 
  747         if (node == &ng_deadnode) {
  748                 return (0);
  749         }
  750 
  751         do {
  752                 v = node->nd_refs - 1;
  753         } while (! atomic_cmpset_int(&node->nd_refs, v + 1, v));
  754 
  755         if (v == 0) { /* we were the last */
  756 
  757                 mtx_lock(&ng_nodelist_mtx);
  758                 node->nd_type->refs--; /* XXX maybe should get types lock? */
  759                 LIST_REMOVE(node, nd_nodes);
  760                 mtx_unlock(&ng_nodelist_mtx);
  761 
  762                 mtx_lock(&ng_idhash_mtx);
  763                 LIST_REMOVE(node, nd_idnodes);
  764                 mtx_unlock(&ng_idhash_mtx);
  765 
  766                 mtx_destroy(&node->nd_input_queue.q_mtx);
  767                 NG_FREE_NODE(node);
  768         }
  769         return (v);
  770 }
  771 
  772 /************************************************************************
  773                         Node ID handling
  774 ************************************************************************/
  775 static node_p
  776 ng_ID2noderef(ng_ID_t ID)
  777 {
  778         node_p node;
  779         mtx_lock(&ng_idhash_mtx);
  780         NG_IDHASH_FIND(ID, node);
  781         if(node)
  782                 NG_NODE_REF(node);
  783         mtx_unlock(&ng_idhash_mtx);
  784         return(node);
  785 }
  786 
  787 ng_ID_t
  788 ng_node2ID(node_p node)
  789 {
  790         return (node ? NG_NODE_ID(node) : 0);
  791 }
  792 
  793 /************************************************************************
  794                         Node name handling
  795 ************************************************************************/
  796 
  797 /*
  798  * Assign a node a name. Once assigned, the name cannot be changed.
  799  */
  800 int
  801 ng_name_node(node_p node, const char *name)
  802 {
  803         int i;
  804         node_p node2;
  805 
  806         /* Check the name is valid */
  807         for (i = 0; i < NG_NODESIZ; i++) {
  808                 if (name[i] == '\0' || name[i] == '.' || name[i] == ':')
  809                         break;
  810         }
  811         if (i == 0 || name[i] != '\0') {
  812                 TRAP_ERROR();
  813                 return (EINVAL);
  814         }
  815         if (ng_decodeidname(name) != 0) { /* valid IDs not allowed here */
  816                 TRAP_ERROR();
  817                 return (EINVAL);
  818         }
  819 
  820         /* Check the name isn't already being used */
  821         if ((node2 = ng_name2noderef(node, name)) != NULL) {
  822                 NG_NODE_UNREF(node2);
  823                 TRAP_ERROR();
  824                 return (EADDRINUSE);
  825         }
  826 
  827         /* copy it */
  828         strlcpy(NG_NODE_NAME(node), name, NG_NODESIZ);
  829 
  830         return (0);
  831 }
  832 
  833 /*
  834  * Find a node by absolute name. The name should NOT end with ':'
  835  * The name "." means "this node" and "[xxx]" means "the node
  836  * with ID (ie, at address) xxx".
  837  *
  838  * Returns the node if found, else NULL.
  839  * Eventually should add something faster than a sequential search.
  840  * Note it acquires a reference on the node so you can be sure it's still
  841  * there.
  842  */
  843 node_p
  844 ng_name2noderef(node_p here, const char *name)
  845 {
  846         node_p node;
  847         ng_ID_t temp;
  848 
  849         /* "." means "this node" */
  850         if (strcmp(name, ".") == 0) {
  851                 NG_NODE_REF(here);
  852                 return(here);
  853         }
  854 
  855         /* Check for name-by-ID */
  856         if ((temp = ng_decodeidname(name)) != 0) {
  857                 return (ng_ID2noderef(temp));
  858         }
  859 
  860         /* Find node by name */
  861         mtx_lock(&ng_nodelist_mtx);
  862         LIST_FOREACH(node, &ng_nodelist, nd_nodes) {
  863                 if (NG_NODE_IS_VALID(node)
  864                 && NG_NODE_HAS_NAME(node)
  865                 && (strcmp(NG_NODE_NAME(node), name) == 0)) {
  866                         break;
  867                 }
  868         }
  869         if (node)
  870                 NG_NODE_REF(node);
  871         mtx_unlock(&ng_nodelist_mtx);
  872         return (node);
  873 }
  874 
  875 /*
  876  * Decode an ID name, eg. "[f03034de]". Returns 0 if the
  877  * string is not valid, otherwise returns the value.
  878  */
  879 static ng_ID_t
  880 ng_decodeidname(const char *name)
  881 {
  882         const int len = strlen(name);
  883         char *eptr;
  884         u_long val;
  885 
  886         /* Check for proper length, brackets, no leading junk */
  887         if ((len < 3)
  888         || (name[0] != '[')
  889         || (name[len - 1] != ']')
  890         || (!isxdigit(name[1]))) {
  891                 return ((ng_ID_t)0);
  892         }
  893 
  894         /* Decode number */
  895         val = strtoul(name + 1, &eptr, 16);
  896         if ((eptr - name != len - 1)
  897         || (val == ULONG_MAX)
  898         || (val == 0)) {
  899                 return ((ng_ID_t)0);
  900         }
  901         return (ng_ID_t)val;
  902 }
  903 
  904 /*
  905  * Remove a name from a node. This should only be called
  906  * when shutting down and removing the node.
  907  * IF we allow name changing this may be more resurrected.
  908  */
  909 void
  910 ng_unname(node_p node)
  911 {
  912 }
  913 
  914 /************************************************************************
  915                         Hook routines
  916  Names are not optional. Hooks are always connected, except for a
  917  brief moment within these routines. On invalidation or during creation
  918  they are connected to the 'dead' hook.
  919 ************************************************************************/
  920 
  921 /*
  922  * Remove a hook reference
  923  */
  924 void
  925 ng_unref_hook(hook_p hook)
  926 {
  927         int v;
  928 
  929         if (hook == &ng_deadhook) {
  930                 return;
  931         }
  932         do {
  933                 v = hook->hk_refs;
  934         } while (! atomic_cmpset_int(&hook->hk_refs, v, v - 1));
  935 
  936         if (v == 1) { /* we were the last */
  937                 if (_NG_HOOK_NODE(hook)) { /* it'll probably be ng_deadnode */
  938                         _NG_NODE_UNREF((_NG_HOOK_NODE(hook)));
  939                         hook->hk_node = NULL;
  940                 }
  941                 NG_FREE_HOOK(hook);
  942         }
  943 }
  944 
  945 /*
  946  * Add an unconnected hook to a node. Only used internally.
  947  * Assumes node is locked. (XXX not yet true )
  948  */
  949 static int
  950 ng_add_hook(node_p node, const char *name, hook_p *hookp)
  951 {
  952         hook_p hook;
  953         int error = 0;
  954 
  955         /* Check that the given name is good */
  956         if (name == NULL) {
  957                 TRAP_ERROR();
  958                 return (EINVAL);
  959         }
  960         if (ng_findhook(node, name) != NULL) {
  961                 TRAP_ERROR();
  962                 return (EEXIST);
  963         }
  964 
  965         /* Allocate the hook and link it up */
  966         NG_ALLOC_HOOK(hook);
  967         if (hook == NULL) {
  968                 TRAP_ERROR();
  969                 return (ENOMEM);
  970         }
  971         hook->hk_refs = 1;              /* add a reference for us to return */
  972         hook->hk_flags = HK_INVALID;
  973         hook->hk_peer = &ng_deadhook;   /* start off this way */
  974         hook->hk_node = node;
  975         NG_NODE_REF(node);              /* each hook counts as a reference */
  976 
  977         /* Set hook name */
  978         strlcpy(NG_HOOK_NAME(hook), name, NG_HOOKSIZ);
  979 
  980         /*
  981          * Check if the node type code has something to say about it
  982          * If it fails, the unref of the hook will also unref the node.
  983          */
  984         if (node->nd_type->newhook != NULL) {
  985                 if ((error = (*node->nd_type->newhook)(node, hook, name))) {
  986                         NG_HOOK_UNREF(hook);    /* this frees the hook */
  987                         return (error);
  988                 }
  989         }
  990         /*
  991          * The 'type' agrees so far, so go ahead and link it in.
  992          * We'll ask again later when we actually connect the hooks.
  993          */
  994         LIST_INSERT_HEAD(&node->nd_hooks, hook, hk_hooks);
  995         node->nd_numhooks++;
  996         NG_HOOK_REF(hook);      /* one for the node */
  997 
  998         if (hookp)
  999                 *hookp = hook;
 1000         return (0);
 1001 }
 1002 
 1003 /*
 1004  * Find a hook
 1005  *
 1006  * Node types may supply their own optimized routines for finding
 1007  * hooks.  If none is supplied, we just do a linear search.
 1008  * XXX Possibly we should add a reference to the hook?
 1009  */
 1010 hook_p
 1011 ng_findhook(node_p node, const char *name)
 1012 {
 1013         hook_p hook;
 1014 
 1015         if (node->nd_type->findhook != NULL)
 1016                 return (*node->nd_type->findhook)(node, name);
 1017         LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) {
 1018                 if (NG_HOOK_IS_VALID(hook)
 1019                 && (strcmp(NG_HOOK_NAME(hook), name) == 0))
 1020                         return (hook);
 1021         }
 1022         return (NULL);
 1023 }
 1024 
 1025 /*
 1026  * Destroy a hook
 1027  *
 1028  * As hooks are always attached, this really destroys two hooks.
 1029  * The one given, and the one attached to it. Disconnect the hooks
 1030  * from each other first. We reconnect the peer hook to the 'dead'
 1031  * hook so that it can still exist after we depart. We then
 1032  * send the peer its own destroy message. This ensures that we only
 1033  * interact with the peer's structures when it is locked processing that
 1034  * message. We hold a reference to the peer hook so we are guaranteed that
 1035  * the peer hook and node are still going to exist until
 1036  * we are finished there as the hook holds a ref on the node.
 1037  * We run this same code again on the peer hook, but that time it is already
 1038  * attached to the 'dead' hook.
 1039  *
 1040  * This routine is called at all stages of hook creation
 1041  * on error detection and must be able to handle any such stage.
 1042  */
 1043 void
 1044 ng_destroy_hook(hook_p hook)
 1045 {
 1046         hook_p peer;
 1047         node_p node;
 1048 
 1049         if (hook == &ng_deadhook) {     /* better safe than sorry */
 1050                 printf("ng_destroy_hook called on deadhook\n");
 1051                 return;
 1052         }
 1053 
 1054         /*
 1055          * Protect divorce process with mutex, to avoid races on
 1056          * simultaneous disconnect.
 1057          */
 1058         mtx_lock(&ng_topo_mtx);
 1059 
 1060         hook->hk_flags |= HK_INVALID;
 1061 
 1062         peer = NG_HOOK_PEER(hook);
 1063         node = NG_HOOK_NODE(hook);
 1064 
 1065         if (peer && (peer != &ng_deadhook)) {
 1066                 /*
 1067                  * Set the peer to point to ng_deadhook
 1068                  * from this moment on we are effectively independent it.
 1069                  * send it an rmhook message of it's own.
 1070                  */
 1071                 peer->hk_peer = &ng_deadhook;   /* They no longer know us */
 1072                 hook->hk_peer = &ng_deadhook;   /* Nor us, them */
 1073                 if (NG_HOOK_NODE(peer) == &ng_deadnode) {
 1074                         /*
 1075                          * If it's already divorced from a node,
 1076                          * just free it.
 1077                          */
 1078                         mtx_unlock(&ng_topo_mtx);
 1079                 } else {
 1080                         mtx_unlock(&ng_topo_mtx);
 1081                         ng_rmhook_self(peer);   /* Send it a surprise */
 1082                 }
 1083                 NG_HOOK_UNREF(peer);            /* account for peer link */
 1084                 NG_HOOK_UNREF(hook);            /* account for peer link */
 1085         } else
 1086                 mtx_unlock(&ng_topo_mtx);
 1087 
 1088         mtx_assert(&ng_topo_mtx, MA_NOTOWNED);
 1089 
 1090         /*
 1091          * Remove the hook from the node's list to avoid possible recursion
 1092          * in case the disconnection results in node shutdown.
 1093          */
 1094         if (node == &ng_deadnode) { /* happens if called from ng_con_nodes() */
 1095                 return;
 1096         }
 1097         LIST_REMOVE(hook, hk_hooks);
 1098         node->nd_numhooks--;
 1099         if (node->nd_type->disconnect) {
 1100                 /*
 1101                  * The type handler may elect to destroy the node so don't
 1102                  * trust its existence after this point. (except
 1103                  * that we still hold a reference on it. (which we
 1104                  * inherrited from the hook we are destroying)
 1105                  */
 1106                 (*node->nd_type->disconnect) (hook);
 1107         }
 1108 
 1109         /*
 1110          * Note that because we will point to ng_deadnode, the original node
 1111          * is not decremented automatically so we do that manually.
 1112          */
 1113         _NG_HOOK_NODE(hook) = &ng_deadnode;
 1114         NG_NODE_UNREF(node);    /* We no longer point to it so adjust count */
 1115         NG_HOOK_UNREF(hook);    /* Account for linkage (in list) to node */
 1116 }
 1117 
 1118 /*
 1119  * Take two hooks on a node and merge the connection so that the given node
 1120  * is effectively bypassed.
 1121  */
 1122 int
 1123 ng_bypass(hook_p hook1, hook_p hook2)
 1124 {
 1125         if (hook1->hk_node != hook2->hk_node) {
 1126                 TRAP_ERROR();
 1127                 return (EINVAL);
 1128         }
 1129         hook1->hk_peer->hk_peer = hook2->hk_peer;
 1130         hook2->hk_peer->hk_peer = hook1->hk_peer;
 1131 
 1132         hook1->hk_peer = &ng_deadhook;
 1133         hook2->hk_peer = &ng_deadhook;
 1134 
 1135         NG_HOOK_UNREF(hook1);
 1136         NG_HOOK_UNREF(hook2);
 1137 
 1138         /* XXX If we ever cache methods on hooks update them as well */
 1139         ng_destroy_hook(hook1);
 1140         ng_destroy_hook(hook2);
 1141         return (0);
 1142 }
 1143 
 1144 /*
 1145  * Install a new netgraph type
 1146  */
 1147 int
 1148 ng_newtype(struct ng_type *tp)
 1149 {
 1150         const size_t namelen = strlen(tp->name);
 1151 
 1152         /* Check version and type name fields */
 1153         if ((tp->version != NG_ABI_VERSION)
 1154         || (namelen == 0)
 1155         || (namelen >= NG_TYPESIZ)) {
 1156                 TRAP_ERROR();
 1157                 if (tp->version != NG_ABI_VERSION) {
 1158                         printf("Netgraph: Node type rejected. ABI mismatch. Suggest recompile\n");
 1159                 }
 1160                 return (EINVAL);
 1161         }
 1162 
 1163         /* Check for name collision */
 1164         if (ng_findtype(tp->name) != NULL) {
 1165                 TRAP_ERROR();
 1166                 return (EEXIST);
 1167         }
 1168 
 1169 
 1170         /* Link in new type */
 1171         mtx_lock(&ng_typelist_mtx);
 1172         LIST_INSERT_HEAD(&ng_typelist, tp, types);
 1173         tp->refs = 1;   /* first ref is linked list */
 1174         mtx_unlock(&ng_typelist_mtx);
 1175         return (0);
 1176 }
 1177 
 1178 /*
 1179  * unlink a netgraph type
 1180  * If no examples exist
 1181  */
 1182 int
 1183 ng_rmtype(struct ng_type *tp)
 1184 {
 1185         /* Check for name collision */
 1186         if (tp->refs != 1) {
 1187                 TRAP_ERROR();
 1188                 return (EBUSY);
 1189         }
 1190 
 1191         /* Unlink type */
 1192         mtx_lock(&ng_typelist_mtx);
 1193         LIST_REMOVE(tp, types);
 1194         mtx_unlock(&ng_typelist_mtx);
 1195         return (0);
 1196 }
 1197 
 1198 /*
 1199  * Look for a type of the name given
 1200  */
 1201 struct ng_type *
 1202 ng_findtype(const char *typename)
 1203 {
 1204         struct ng_type *type;
 1205 
 1206         mtx_lock(&ng_typelist_mtx);
 1207         LIST_FOREACH(type, &ng_typelist, types) {
 1208                 if (strcmp(type->name, typename) == 0)
 1209                         break;
 1210         }
 1211         mtx_unlock(&ng_typelist_mtx);
 1212         return (type);
 1213 }
 1214 
 1215 /************************************************************************
 1216                         Composite routines
 1217 ************************************************************************/
 1218 /*
 1219  * Connect two nodes using the specified hooks, using queued functions.
 1220  */
 1221 static int
 1222 ng_con_part3(node_p node, item_p item, hook_p hook)
 1223 {
 1224         int     error = 0;
 1225 
 1226         /*
 1227          * When we run, we know that the node 'node' is locked for us.
 1228          * Our caller has a reference on the hook.
 1229          * Our caller has a reference on the node.
 1230          * (In this case our caller is ng_apply_item() ).
 1231          * The peer hook has a reference on the hook.
 1232          * We are all set up except for the final call to the node, and
 1233          * the clearing of the INVALID flag.
 1234          */
 1235         if (NG_HOOK_NODE(hook) == &ng_deadnode) {
 1236                 /*
 1237                  * The node must have been freed again since we last visited
 1238                  * here. ng_destry_hook() has this effect but nothing else does.
 1239                  * We should just release our references and
 1240                  * free anything we can think of.
 1241                  * Since we know it's been destroyed, and it's our caller
 1242                  * that holds the references, just return.
 1243                  */
 1244                 ERROUT(ENOENT);
 1245         }
 1246         if (hook->hk_node->nd_type->connect) {
 1247                 if ((error = (*hook->hk_node->nd_type->connect) (hook))) {
 1248                         ng_destroy_hook(hook);  /* also zaps peer */
 1249                         printf("failed in ng_con_part3()\n");
 1250                         ERROUT(error);
 1251                 }
 1252         }
 1253         /*
 1254          *  XXX this is wrong for SMP. Possibly we need
 1255          * to separate out 'create' and 'invalid' flags.
 1256          * should only set flags on hooks we have locked under our node.
 1257          */
 1258         hook->hk_flags &= ~HK_INVALID;
 1259 done:
 1260         NG_FREE_ITEM(item);
 1261         return (error);
 1262 }
 1263 
 1264 static int
 1265 ng_con_part2(node_p node, item_p item, hook_p hook)
 1266 {
 1267         hook_p  peer;
 1268         int     error = 0;
 1269 
 1270         /*
 1271          * When we run, we know that the node 'node' is locked for us.
 1272          * Our caller has a reference on the hook.
 1273          * Our caller has a reference on the node.
 1274          * (In this case our caller is ng_apply_item() ).
 1275          * The peer hook has a reference on the hook.
 1276          * our node pointer points to the 'dead' node.
 1277          * First check the hook name is unique.
 1278          * Should not happen because we checked before queueing this.
 1279          */
 1280         if (ng_findhook(node, NG_HOOK_NAME(hook)) != NULL) {
 1281                 TRAP_ERROR();
 1282                 ng_destroy_hook(hook); /* should destroy peer too */
 1283                 printf("failed in ng_con_part2()\n");
 1284                 ERROUT(EEXIST);
 1285         }
 1286         /*
 1287          * Check if the node type code has something to say about it
 1288          * If it fails, the unref of the hook will also unref the attached node,
 1289          * however since that node is 'ng_deadnode' this will do nothing.
 1290          * The peer hook will also be destroyed.
 1291          */
 1292         if (node->nd_type->newhook != NULL) {
 1293                 if ((error = (*node->nd_type->newhook)(node, hook,
 1294                     hook->hk_name))) {
 1295                         ng_destroy_hook(hook); /* should destroy peer too */
 1296                         printf("failed in ng_con_part2()\n");
 1297                         ERROUT(error);
 1298                 }
 1299         }
 1300 
 1301         /*
 1302          * The 'type' agrees so far, so go ahead and link it in.
 1303          * We'll ask again later when we actually connect the hooks.
 1304          */
 1305         hook->hk_node = node;           /* just overwrite ng_deadnode */
 1306         NG_NODE_REF(node);              /* each hook counts as a reference */
 1307         LIST_INSERT_HEAD(&node->nd_hooks, hook, hk_hooks);
 1308         node->nd_numhooks++;
 1309         NG_HOOK_REF(hook);      /* one for the node */
 1310         
 1311         /*
 1312          * We now have a symmetrical situation, where both hooks have been
 1313          * linked to their nodes, the newhook methods have been called
 1314          * And the references are all correct. The hooks are still marked
 1315          * as invalid, as we have not called the 'connect' methods
 1316          * yet.
 1317          * We can call the local one immediately as we have the
 1318          * node locked, but we need to queue the remote one.
 1319          */
 1320         if (hook->hk_node->nd_type->connect) {
 1321                 if ((error = (*hook->hk_node->nd_type->connect) (hook))) {
 1322                         ng_destroy_hook(hook);  /* also zaps peer */
 1323                         printf("failed in ng_con_part2(A)\n");
 1324                         ERROUT(error);
 1325                 }
 1326         }
 1327 
 1328         /*
 1329          * Acquire topo mutex to avoid race with ng_destroy_hook().
 1330          */
 1331         mtx_lock(&ng_topo_mtx);
 1332         peer = hook->hk_peer;
 1333         if (peer == &ng_deadhook) {
 1334                 mtx_unlock(&ng_topo_mtx);
 1335                 printf("failed in ng_con_part2(B)\n");
 1336                 ng_destroy_hook(hook);
 1337                 ERROUT(ENOENT);
 1338         }
 1339         mtx_unlock(&ng_topo_mtx);
 1340 
 1341         if ((error = ng_send_fn2(peer->hk_node, peer, item, &ng_con_part3,
 1342             NULL, 0, NG_REUSE_ITEM))) {
 1343                 printf("failed in ng_con_part2(C)\n");
 1344                 ng_destroy_hook(hook);  /* also zaps peer */
 1345                 return (error);         /* item was consumed. */
 1346         }
 1347         hook->hk_flags &= ~HK_INVALID; /* need both to be able to work */
 1348         return (0);                     /* item was consumed. */
 1349 done:
 1350         NG_FREE_ITEM(item);
 1351         return (error);
 1352 }
 1353 
 1354 /*
 1355  * Connect this node with another node. We assume that this node is
 1356  * currently locked, as we are only called from an NGM_CONNECT message.
 1357  */
 1358 static int
 1359 ng_con_nodes(item_p item, node_p node, const char *name,
 1360     node_p node2, const char *name2)
 1361 {
 1362         int     error;
 1363         hook_p  hook;
 1364         hook_p  hook2;
 1365 
 1366         if (ng_findhook(node2, name2) != NULL) {
 1367                 return(EEXIST);
 1368         }
 1369         if ((error = ng_add_hook(node, name, &hook)))  /* gives us a ref */
 1370                 return (error);
 1371         /* Allocate the other hook and link it up */
 1372         NG_ALLOC_HOOK(hook2);
 1373         if (hook2 == NULL) {
 1374                 TRAP_ERROR();
 1375                 ng_destroy_hook(hook);  /* XXX check ref counts so far */
 1376                 NG_HOOK_UNREF(hook);    /* including our ref */
 1377                 return (ENOMEM);
 1378         }
 1379         hook2->hk_refs = 1;             /* start with a reference for us. */
 1380         hook2->hk_flags = HK_INVALID;
 1381         hook2->hk_peer = hook;          /* Link the two together */
 1382         hook->hk_peer = hook2;  
 1383         NG_HOOK_REF(hook);              /* Add a ref for the peer to each*/
 1384         NG_HOOK_REF(hook2);
 1385         hook2->hk_node = &ng_deadnode;
 1386         strlcpy(NG_HOOK_NAME(hook2), name2, NG_HOOKSIZ);
 1387 
 1388         /*
 1389          * Queue the function above.
 1390          * Procesing continues in that function in the lock context of
 1391          * the other node.
 1392          */
 1393         if ((error = ng_send_fn2(node2, hook2, item, &ng_con_part2, NULL, 0,
 1394             NG_NOFLAGS))) {
 1395                 printf("failed in ng_con_nodes(): %d\n", error);
 1396                 ng_destroy_hook(hook);  /* also zaps peer */
 1397         }
 1398 
 1399         NG_HOOK_UNREF(hook);            /* Let each hook go if it wants to */
 1400         NG_HOOK_UNREF(hook2);
 1401         return (error);
 1402 }
 1403 
 1404 /*
 1405  * Make a peer and connect.
 1406  * We assume that the local node is locked.
 1407  * The new node probably doesn't need a lock until
 1408  * it has a hook, because it cannot really have any work until then,
 1409  * but we should think about it a bit more.
 1410  *
 1411  * The problem may come if the other node also fires up
 1412  * some hardware or a timer or some other source of activation,
 1413  * also it may already get a command msg via it's ID.
 1414  *
 1415  * We could use the same method as ng_con_nodes() but we'd have
 1416  * to add ability to remove the node when failing. (Not hard, just
 1417  * make arg1 point to the node to remove).
 1418  * Unless of course we just ignore failure to connect and leave
 1419  * an unconnected node?
 1420  */
 1421 static int
 1422 ng_mkpeer(node_p node, const char *name, const char *name2, char *type)
 1423 {
 1424         node_p  node2;
 1425         hook_p  hook1, hook2;
 1426         int     error;
 1427 
 1428         if ((error = ng_make_node(type, &node2))) {
 1429                 return (error);
 1430         }
 1431 
 1432         if ((error = ng_add_hook(node, name, &hook1))) { /* gives us a ref */
 1433                 ng_rmnode(node2, NULL, NULL, 0);
 1434                 return (error);
 1435         }
 1436 
 1437         if ((error = ng_add_hook(node2, name2, &hook2))) {
 1438                 ng_rmnode(node2, NULL, NULL, 0);
 1439                 ng_destroy_hook(hook1);
 1440                 NG_HOOK_UNREF(hook1);
 1441                 return (error);
 1442         }
 1443 
 1444         /*
 1445          * Actually link the two hooks together.
 1446          */
 1447         hook1->hk_peer = hook2;
 1448         hook2->hk_peer = hook1;
 1449 
 1450         /* Each hook is referenced by the other */
 1451         NG_HOOK_REF(hook1);
 1452         NG_HOOK_REF(hook2);
 1453 
 1454         /* Give each node the opportunity to veto the pending connection */
 1455         if (hook1->hk_node->nd_type->connect) {
 1456                 error = (*hook1->hk_node->nd_type->connect) (hook1);
 1457         }
 1458 
 1459         if ((error == 0) && hook2->hk_node->nd_type->connect) {
 1460                 error = (*hook2->hk_node->nd_type->connect) (hook2);
 1461 
 1462         }
 1463 
 1464         /*
 1465          * drop the references we were holding on the two hooks.
 1466          */
 1467         if (error) {
 1468                 ng_destroy_hook(hook2); /* also zaps hook1 */
 1469                 ng_rmnode(node2, NULL, NULL, 0);
 1470         } else {
 1471                 /* As a last act, allow the hooks to be used */
 1472                 hook1->hk_flags &= ~HK_INVALID;
 1473                 hook2->hk_flags &= ~HK_INVALID;
 1474         }
 1475         NG_HOOK_UNREF(hook1);
 1476         NG_HOOK_UNREF(hook2);
 1477         return (error);
 1478 }
 1479 
 1480 /************************************************************************
 1481                 Utility routines to send self messages
 1482 ************************************************************************/
 1483         
 1484 /* Shut this node down as soon as everyone is clear of it */
 1485 /* Should add arg "immediately" to jump the queue */
 1486 int
 1487 ng_rmnode_self(node_p node)
 1488 {
 1489         int             error;
 1490 
 1491         if (node == &ng_deadnode)
 1492                 return (0);
 1493         node->nd_flags |= NGF_INVALID;
 1494         if (node->nd_flags & NGF_CLOSING)
 1495                 return (0);
 1496 
 1497         error = ng_send_fn(node, NULL, &ng_rmnode, NULL, 0);
 1498         return (error);
 1499 }
 1500 
 1501 static void
 1502 ng_rmhook_part2(node_p node, hook_p hook, void *arg1, int arg2)
 1503 {
 1504         ng_destroy_hook(hook);
 1505         return ;
 1506 }
 1507 
 1508 int
 1509 ng_rmhook_self(hook_p hook)
 1510 {
 1511         int             error;
 1512         node_p node = NG_HOOK_NODE(hook);
 1513 
 1514         if (node == &ng_deadnode)
 1515                 return (0);
 1516 
 1517         error = ng_send_fn(node, hook, &ng_rmhook_part2, NULL, 0);
 1518         return (error);
 1519 }
 1520 
 1521 /***********************************************************************
 1522  * Parse and verify a string of the form:  <NODE:><PATH>
 1523  *
 1524  * Such a string can refer to a specific node or a specific hook
 1525  * on a specific node, depending on how you look at it. In the
 1526  * latter case, the PATH component must not end in a dot.
 1527  *
 1528  * Both <NODE:> and <PATH> are optional. The <PATH> is a string
 1529  * of hook names separated by dots. This breaks out the original
 1530  * string, setting *nodep to "NODE" (or NULL if none) and *pathp
 1531  * to "PATH" (or NULL if degenerate). Also, *hookp will point to
 1532  * the final hook component of <PATH>, if any, otherwise NULL.
 1533  *
 1534  * This returns -1 if the path is malformed. The char ** are optional.
 1535  ***********************************************************************/
 1536 int
 1537 ng_path_parse(char *addr, char **nodep, char **pathp, char **hookp)
 1538 {
 1539         char    *node, *path, *hook;
 1540         int     k;
 1541 
 1542         /*
 1543          * Extract absolute NODE, if any
 1544          */
 1545         for (path = addr; *path && *path != ':'; path++);
 1546         if (*path) {
 1547                 node = addr;    /* Here's the NODE */
 1548                 *path++ = '\0'; /* Here's the PATH */
 1549 
 1550                 /* Node name must not be empty */
 1551                 if (!*node)
 1552                         return -1;
 1553 
 1554                 /* A name of "." is OK; otherwise '.' not allowed */
 1555                 if (strcmp(node, ".") != 0) {
 1556                         for (k = 0; node[k]; k++)
 1557                                 if (node[k] == '.')
 1558                                         return -1;
 1559                 }
 1560         } else {
 1561                 node = NULL;    /* No absolute NODE */
 1562                 path = addr;    /* Here's the PATH */
 1563         }
 1564 
 1565         /* Snoop for illegal characters in PATH */
 1566         for (k = 0; path[k]; k++)
 1567                 if (path[k] == ':')
 1568                         return -1;
 1569 
 1570         /* Check for no repeated dots in PATH */
 1571         for (k = 0; path[k]; k++)
 1572                 if (path[k] == '.' && path[k + 1] == '.')
 1573                         return -1;
 1574 
 1575         /* Remove extra (degenerate) dots from beginning or end of PATH */
 1576         if (path[0] == '.')
 1577                 path++;
 1578         if (*path && path[strlen(path) - 1] == '.')
 1579                 path[strlen(path) - 1] = 0;
 1580 
 1581         /* If PATH has a dot, then we're not talking about a hook */
 1582         if (*path) {
 1583                 for (hook = path, k = 0; path[k]; k++)
 1584                         if (path[k] == '.') {
 1585                                 hook = NULL;
 1586                                 break;
 1587                         }
 1588         } else
 1589                 path = hook = NULL;
 1590 
 1591         /* Done */
 1592         if (nodep)
 1593                 *nodep = node;
 1594         if (pathp)
 1595                 *pathp = path;
 1596         if (hookp)
 1597                 *hookp = hook;
 1598         return (0);
 1599 }
 1600 
 1601 /*
 1602  * Given a path, which may be absolute or relative, and a starting node,
 1603  * return the destination node.
 1604  */
 1605 int
 1606 ng_path2noderef(node_p here, const char *address,
 1607                                 node_p *destp, hook_p *lasthook)
 1608 {
 1609         char    fullpath[NG_PATHSIZ];
 1610         char   *nodename, *path, pbuf[2];
 1611         node_p  node, oldnode;
 1612         char   *cp;
 1613         hook_p hook = NULL;
 1614 
 1615         /* Initialize */
 1616         if (destp == NULL) {
 1617                 TRAP_ERROR();
 1618                 return EINVAL;
 1619         }
 1620         *destp = NULL;
 1621 
 1622         /* Make a writable copy of address for ng_path_parse() */
 1623         strncpy(fullpath, address, sizeof(fullpath) - 1);
 1624         fullpath[sizeof(fullpath) - 1] = '\0';
 1625 
 1626         /* Parse out node and sequence of hooks */
 1627         if (ng_path_parse(fullpath, &nodename, &path, NULL) < 0) {
 1628                 TRAP_ERROR();
 1629                 return EINVAL;
 1630         }
 1631         if (path == NULL) {
 1632                 pbuf[0] = '.';  /* Needs to be writable */
 1633                 pbuf[1] = '\0';
 1634                 path = pbuf;
 1635         }
 1636 
 1637         /*
 1638          * For an absolute address, jump to the starting node.
 1639          * Note that this holds a reference on the node for us.
 1640          * Don't forget to drop the reference if we don't need it.
 1641          */
 1642         if (nodename) {
 1643                 node = ng_name2noderef(here, nodename);
 1644                 if (node == NULL) {
 1645                         TRAP_ERROR();
 1646                         return (ENOENT);
 1647                 }
 1648         } else {
 1649                 if (here == NULL) {
 1650                         TRAP_ERROR();
 1651                         return (EINVAL);
 1652                 }
 1653                 node = here;
 1654                 NG_NODE_REF(node);
 1655         }
 1656 
 1657         /*
 1658          * Now follow the sequence of hooks
 1659          * XXX
 1660          * We actually cannot guarantee that the sequence
 1661          * is not being demolished as we crawl along it
 1662          * without extra-ordinary locking etc.
 1663          * So this is a bit dodgy to say the least.
 1664          * We can probably hold up some things by holding
 1665          * the nodelist mutex for the time of this
 1666          * crawl if we wanted.. At least that way we wouldn't have to
 1667          * worry about the nodes disappearing, but the hooks would still
 1668          * be a problem.
 1669          */
 1670         for (cp = path; node != NULL && *cp != '\0'; ) {
 1671                 char *segment;
 1672 
 1673                 /*
 1674                  * Break out the next path segment. Replace the dot we just
 1675                  * found with a NUL; "cp" points to the next segment (or the
 1676                  * NUL at the end).
 1677                  */
 1678                 for (segment = cp; *cp != '\0'; cp++) {
 1679                         if (*cp == '.') {
 1680                                 *cp++ = '\0';
 1681                                 break;
 1682                         }
 1683                 }
 1684 
 1685                 /* Empty segment */
 1686                 if (*segment == '\0')
 1687                         continue;
 1688 
 1689                 /* We have a segment, so look for a hook by that name */
 1690                 hook = ng_findhook(node, segment);
 1691 
 1692                 /* Can't get there from here... */
 1693                 if (hook == NULL
 1694                     || NG_HOOK_PEER(hook) == NULL
 1695                     || NG_HOOK_NOT_VALID(hook)
 1696                     || NG_HOOK_NOT_VALID(NG_HOOK_PEER(hook))) {
 1697                         TRAP_ERROR();
 1698                         NG_NODE_UNREF(node);
 1699 #if 0
 1700                         printf("hooknotvalid %s %s %d %d %d %d ",
 1701                                         path,
 1702                                         segment,
 1703                                         hook == NULL,
 1704                                         NG_HOOK_PEER(hook) == NULL,
 1705                                         NG_HOOK_NOT_VALID(hook),
 1706                                         NG_HOOK_NOT_VALID(NG_HOOK_PEER(hook)));
 1707 #endif
 1708                         return (ENOENT);
 1709                 }
 1710 
 1711                 /*
 1712                  * Hop on over to the next node
 1713                  * XXX
 1714                  * Big race conditions here as hooks and nodes go away
 1715                  * *** Idea.. store an ng_ID_t in each hook and use that
 1716                  * instead of the direct hook in this crawl?
 1717                  */
 1718                 oldnode = node;
 1719                 if ((node = NG_PEER_NODE(hook)))
 1720                         NG_NODE_REF(node);      /* XXX RACE */
 1721                 NG_NODE_UNREF(oldnode); /* XXX another race */
 1722                 if (NG_NODE_NOT_VALID(node)) {
 1723                         NG_NODE_UNREF(node);    /* XXX more races */
 1724                         node = NULL;
 1725                 }
 1726         }
 1727 
 1728         /* If node somehow missing, fail here (probably this is not needed) */
 1729         if (node == NULL) {
 1730                 TRAP_ERROR();
 1731                 return (ENXIO);
 1732         }
 1733 
 1734         /* Done */
 1735         *destp = node;
 1736         if (lasthook != NULL)
 1737                 *lasthook = (hook ? NG_HOOK_PEER(hook) : NULL);
 1738         return (0);
 1739 }
 1740 
 1741 /***************************************************************\
 1742 * Input queue handling.
 1743 * All activities are submitted to the node via the input queue
 1744 * which implements a multiple-reader/single-writer gate.
 1745 * Items which cannot be handled immediately are queued.
 1746 *
 1747 * read-write queue locking inline functions                     *
 1748 \***************************************************************/
 1749 
 1750 static __inline item_p ng_dequeue(struct ng_queue * ngq, int *rw);
 1751 static __inline item_p ng_acquire_read(struct ng_queue * ngq,
 1752                                         item_p  item);
 1753 static __inline item_p ng_acquire_write(struct ng_queue * ngq,
 1754                                         item_p  item);
 1755 static __inline void    ng_leave_read(struct ng_queue * ngq);
 1756 static __inline void    ng_leave_write(struct ng_queue * ngq);
 1757 static __inline void    ng_queue_rw(struct ng_queue * ngq,
 1758                                         item_p  item, int rw);
 1759 
 1760 /*
 1761  * Definition of the bits fields in the ng_queue flag word.
 1762  * Defined here rather than in netgraph.h because no-one should fiddle
 1763  * with them.
 1764  *
 1765  * The ordering here may be important! don't shuffle these.
 1766  */
 1767 /*-
 1768  Safety Barrier--------+ (adjustable to suit taste) (not used yet)
 1769                        |
 1770                        V
 1771 +-------+-------+-------+-------+-------+-------+-------+-------+
 1772   | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
 1773   | |A|c|t|i|v|e| |R|e|a|d|e|r| |C|o|u|n|t| | | | | | | | | |P|A|
 1774   | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |O|W|
 1775 +-------+-------+-------+-------+-------+-------+-------+-------+
 1776   \___________________________ ____________________________/ | |
 1777                             V                                | |
 1778                   [active reader count]                      | |
 1779                                                              | |
 1780             Operation Pending -------------------------------+ |
 1781                                                                |
 1782           Active Writer ---------------------------------------+
 1783 
 1784 
 1785 */
 1786 #define WRITER_ACTIVE   0x00000001
 1787 #define OP_PENDING      0x00000002
 1788 #define READER_INCREMENT 0x00000004
 1789 #define READER_MASK     0xfffffffc      /* Not valid if WRITER_ACTIVE is set */
 1790 #define SAFETY_BARRIER  0x00100000      /* 128K items queued should be enough */
 1791 
 1792 /* Defines of more elaborate states on the queue */
 1793 /* Mask of bits a new read cares about */
 1794 #define NGQ_RMASK       (WRITER_ACTIVE|OP_PENDING)
 1795 
 1796 /* Mask of bits a new write cares about */
 1797 #define NGQ_WMASK       (NGQ_RMASK|READER_MASK)
 1798 
 1799 /* Test to decide if there is something on the queue. */
 1800 #define QUEUE_ACTIVE(QP) ((QP)->q_flags & OP_PENDING)
 1801 
 1802 /* How to decide what the next queued item is. */
 1803 #define HEAD_IS_READER(QP)  NGI_QUEUED_READER((QP)->queue)
 1804 #define HEAD_IS_WRITER(QP)  NGI_QUEUED_WRITER((QP)->queue) /* notused */
 1805 
 1806 /* Read the status to decide if the next item on the queue can now run. */
 1807 #define QUEUED_READER_CAN_PROCEED(QP)                   \
 1808                 (((QP)->q_flags & (NGQ_RMASK & ~OP_PENDING)) == 0)
 1809 #define QUEUED_WRITER_CAN_PROCEED(QP)                   \
 1810                 (((QP)->q_flags & (NGQ_WMASK & ~OP_PENDING)) == 0)
 1811 
 1812 /* Is there a chance of getting ANY work off the queue? */
 1813 #define NEXT_QUEUED_ITEM_CAN_PROCEED(QP)                                \
 1814         (QUEUE_ACTIVE(QP) &&                                            \
 1815         ((HEAD_IS_READER(QP)) ? QUEUED_READER_CAN_PROCEED(QP) :         \
 1816                                 QUEUED_WRITER_CAN_PROCEED(QP)))
 1817 
 1818 
 1819 #define NGQRW_R 0
 1820 #define NGQRW_W 1
 1821 
 1822 /*
 1823  * Taking into account the current state of the queue and node, possibly take
 1824  * the next entry off the queue and return it. Return NULL if there was
 1825  * nothing we could return, either because there really was nothing there, or
 1826  * because the node was in a state where it cannot yet process the next item
 1827  * on the queue.
 1828  *
 1829  * This MUST MUST MUST be called with the mutex held.
 1830  */
 1831 static __inline item_p
 1832 ng_dequeue(struct ng_queue *ngq, int *rw)
 1833 {
 1834         item_p item;
 1835         u_int           add_arg;
 1836 
 1837         mtx_assert(&ngq->q_mtx, MA_OWNED);
 1838         /*
 1839          * If there is nothing queued, then just return.
 1840          * No point in continuing.
 1841          * XXXGL: assert this?
 1842          */
 1843         if (!QUEUE_ACTIVE(ngq)) {
 1844                 CTR4(KTR_NET, "%20s: node [%x] (%p) queue empty; "
 1845                     "queue flags 0x%lx", __func__,
 1846                     ngq->q_node->nd_ID, ngq->q_node, ngq->q_flags);
 1847                 return (NULL);
 1848         }
 1849 
 1850         /*
 1851          * From here, we can assume there is a head item.
 1852          * We need to find out what it is and if it can be dequeued, given
 1853          * the current state of the node.
 1854          */
 1855         if (HEAD_IS_READER(ngq)) {
 1856                 if (!QUEUED_READER_CAN_PROCEED(ngq)) {
 1857                         /*
 1858                          * It's a reader but we can't use it.
 1859                          * We are stalled so make sure we don't
 1860                          * get called again until something changes.
 1861                          */
 1862                         ng_worklist_remove(ngq->q_node);
 1863                         CTR4(KTR_NET, "%20s: node [%x] (%p) queued reader "
 1864                             "can't proceed; queue flags 0x%lx", __func__,
 1865                             ngq->q_node->nd_ID, ngq->q_node, ngq->q_flags);
 1866                         return (NULL);
 1867                 }
 1868                 /*
 1869                  * Head of queue is a reader and we have no write active.
 1870                  * We don't care how many readers are already active.
 1871                  * Add the correct increment for the reader count.
 1872                  */
 1873                 add_arg = READER_INCREMENT;
 1874                 *rw = NGQRW_R;
 1875         } else if (QUEUED_WRITER_CAN_PROCEED(ngq)) {
 1876                 /*
 1877                  * There is a pending write, no readers and no active writer.
 1878                  * This means we can go ahead with the pending writer. Note
 1879                  * the fact that we now have a writer, ready for when we take
 1880                  * it off the queue.
 1881                  *
 1882                  * We don't need to worry about a possible collision with the
 1883                  * fasttrack reader.
 1884                  *
 1885                  * The fasttrack thread may take a long time to discover that we
 1886                  * are running so we would have an inconsistent state in the
 1887                  * flags for a while. Since we ignore the reader count
 1888                  * entirely when the WRITER_ACTIVE flag is set, this should
 1889                  * not matter (in fact it is defined that way). If it tests
 1890                  * the flag before this operation, the OP_PENDING flag
 1891                  * will make it fail, and if it tests it later, the
 1892                  * WRITER_ACTIVE flag will do the same. If it is SO slow that
 1893                  * we have actually completed the operation, and neither flag
 1894                  * is set by the time that it tests the flags, then it is
 1895                  * actually ok for it to continue. If it completes and we've
 1896                  * finished and the read pending is set it still fails.
 1897                  *
 1898                  * So we can just ignore it,  as long as we can ensure that the
 1899                  * transition from WRITE_PENDING state to the WRITER_ACTIVE
 1900                  * state is atomic.
 1901                  *
 1902                  * After failing, first it will be held back by the mutex, then
 1903                  * when it can proceed, it will queue its request, then it
 1904                  * would arrive at this function. Usually it will have to
 1905                  * leave empty handed because the ACTIVE WRITER bit will be
 1906                  * set.
 1907                  *
 1908                  * Adjust the flags for the new active writer.
 1909                  */
 1910                 add_arg = WRITER_ACTIVE;
 1911                 *rw = NGQRW_W;
 1912                 /*
 1913                  * We want to write "active writer, no readers " Now go make
 1914                  * it true. In fact there may be a number in the readers
 1915                  * count but we know it is not true and will be fixed soon.
 1916                  * We will fix the flags for the next pending entry in a
 1917                  * moment.
 1918                  */
 1919         } else {
 1920                 /*
 1921                  * We can't dequeue anything.. return and say so. Probably we
 1922                  * have a write pending and the readers count is non zero. If
 1923                  * we got here because a reader hit us just at the wrong
 1924                  * moment with the fasttrack code, and put us in a strange
 1925                  * state, then it will be coming through in just a moment,
 1926                  * (just as soon as we release the mutex) and keep things
 1927                  * moving.
 1928                  * Make sure we remove ourselves from the work queue. It
 1929                  * would be a waste of effort to do all this again.
 1930                  */
 1931                 ng_worklist_remove(ngq->q_node);
 1932                 CTR4(KTR_NET, "%20s: node [%x] (%p) can't dequeue anything; "
 1933                     "queue flags 0x%lx", __func__,
 1934                     ngq->q_node->nd_ID, ngq->q_node, ngq->q_flags);
 1935                 return (NULL);
 1936         }
 1937 
 1938         /*
 1939          * Now we dequeue the request (whatever it may be) and correct the
 1940          * pending flags and the next and last pointers.
 1941          */
 1942         item = ngq->queue;
 1943         ngq->queue = item->el_next;
 1944         CTR6(KTR_NET, "%20s: node [%x] (%p) dequeued item %p with flags 0x%lx; "
 1945             "queue flags 0x%lx", __func__,
 1946             ngq->q_node->nd_ID,ngq->q_node, item, item->el_flags, ngq->q_flags);
 1947         if (ngq->last == &(item->el_next)) {
 1948                 /*
 1949                  * that was the last entry in the queue so set the 'last
 1950                  * pointer up correctly and make sure the pending flag is
 1951                  * clear.
 1952                  */
 1953                 add_arg += -OP_PENDING;
 1954                 ngq->last = &(ngq->queue);
 1955                 /*
 1956                  * Whatever flag was set will be cleared and
 1957                  * the new acive field will be set by the add as well,
 1958                  * so we don't need to change add_arg.
 1959                  * But we know we don't need to be on the work list.
 1960                  */
 1961                 atomic_add_long(&ngq->q_flags, add_arg);
 1962                 ng_worklist_remove(ngq->q_node);
 1963         } else {
 1964                 /*
 1965                  * Since there is still something on the queue
 1966                  * we don't need to change the PENDING flag.
 1967                  */
 1968                 atomic_add_long(&ngq->q_flags, add_arg);
 1969                 /*
 1970                  * If we see more doable work, make sure we are
 1971                  * on the work queue.
 1972                  */
 1973                 if (NEXT_QUEUED_ITEM_CAN_PROCEED(ngq)) {
 1974                         ng_setisr(ngq->q_node);
 1975                 }
 1976         }
 1977         CTR6(KTR_NET, "%20s: node [%x] (%p) returning item %p as %s; "
 1978             "queue flags 0x%lx", __func__,
 1979             ngq->q_node->nd_ID, ngq->q_node, item, *rw ? "WRITER" : "READER" ,
 1980             ngq->q_flags);
 1981         return (item);
 1982 }
 1983 
 1984 /*
 1985  * Queue a packet to be picked up by someone else.
 1986  * We really don't care who, but we can't or don't want to hang around
 1987  * to process it ourselves. We are probably an interrupt routine..
 1988  * If the queue could be run, flag the netisr handler to start.
 1989  */
 1990 static __inline void
 1991 ng_queue_rw(struct ng_queue * ngq, item_p  item, int rw)
 1992 {
 1993         mtx_assert(&ngq->q_mtx, MA_OWNED);
 1994 
 1995         if (rw == NGQRW_W)
 1996                 NGI_SET_WRITER(item);
 1997         else
 1998                 NGI_SET_READER(item);
 1999         item->el_next = NULL;   /* maybe not needed */
 2000         *ngq->last = item;
 2001         CTR5(KTR_NET, "%20s: node [%x] (%p) queued item %p as %s", __func__,
 2002             ngq->q_node->nd_ID, ngq->q_node, item, rw ? "WRITER" : "READER" );
 2003         /*
 2004          * If it was the first item in the queue then we need to
 2005          * set the last pointer and the type flags.
 2006          */
 2007         if (ngq->last == &(ngq->queue)) {
 2008                 atomic_add_long(&ngq->q_flags, OP_PENDING);
 2009                 CTR3(KTR_NET, "%20s: node [%x] (%p) set OP_PENDING", __func__,
 2010                     ngq->q_node->nd_ID, ngq->q_node);
 2011         }
 2012 
 2013         ngq->last = &(item->el_next);
 2014         /*
 2015          * We can take the worklist lock with the node locked
 2016          * BUT NOT THE REVERSE!
 2017          */
 2018         if (NEXT_QUEUED_ITEM_CAN_PROCEED(ngq))
 2019                 ng_setisr(ngq->q_node);
 2020 }
 2021 
 2022 
 2023 /*
 2024  * This function 'cheats' in that it first tries to 'grab' the use of the
 2025  * node, without going through the mutex. We can do this becasue of the
 2026  * semantics of the lock. The semantics include a clause that says that the
 2027  * value of the readers count is invalid if the WRITER_ACTIVE flag is set. It
 2028  * also says that the WRITER_ACTIVE flag cannot be set if the readers count
 2029  * is not zero. Note that this talks about what is valid to SET the
 2030  * WRITER_ACTIVE flag, because from the moment it is set, the value if the
 2031  * reader count is immaterial, and not valid. The two 'pending' flags have a
 2032  * similar effect, in that If they are orthogonal to the two active fields in
 2033  * how they are set, but if either is set, the attempted 'grab' need to be
 2034  * backed out because there is earlier work, and we maintain ordering in the
 2035  * queue. The result of this is that the reader request can try obtain use of
 2036  * the node with only a single atomic addition, and without any of the mutex
 2037  * overhead. If this fails the operation degenerates to the same as for other
 2038  * cases.
 2039  *
 2040  */
 2041 static __inline item_p
 2042 ng_acquire_read(struct ng_queue *ngq, item_p item)
 2043 {
 2044         KASSERT(ngq != &ng_deadnode.nd_input_queue,
 2045             ("%s: working on deadnode", __func__));
 2046 
 2047         /* ######### Hack alert ######### */
 2048         atomic_add_long(&ngq->q_flags, READER_INCREMENT);
 2049         if ((ngq->q_flags & NGQ_RMASK) == 0) {
 2050                 /* Successfully grabbed node */
 2051                 CTR4(KTR_NET, "%20s: node [%x] (%p) fast acquired item %p",
 2052                     __func__, ngq->q_node->nd_ID, ngq->q_node, item);
 2053                 return (item);
 2054         }
 2055         /* undo the damage if we didn't succeed */
 2056         atomic_subtract_long(&ngq->q_flags, READER_INCREMENT);
 2057 
 2058         /* ######### End Hack alert ######### */
 2059         mtx_lock_spin((&ngq->q_mtx));
 2060         /*
 2061          * Try again. Another processor (or interrupt for that matter) may
 2062          * have removed the last queued item that was stopping us from
 2063          * running, between the previous test, and the moment that we took
 2064          * the mutex. (Or maybe a writer completed.)
 2065          * Even if another fast-track reader hits during this period
 2066          * we don't care as multiple readers is OK.
 2067          */
 2068         if ((ngq->q_flags & NGQ_RMASK) == 0) {
 2069                 atomic_add_long(&ngq->q_flags, READER_INCREMENT);
 2070                 mtx_unlock_spin((&ngq->q_mtx));
 2071                 CTR4(KTR_NET, "%20s: node [%x] (%p) slow acquired item %p",
 2072                     __func__, ngq->q_node->nd_ID, ngq->q_node, item);
 2073                 return (item);
 2074         }
 2075 
 2076         /*
 2077          * and queue the request for later.
 2078          */
 2079         ng_queue_rw(ngq, item, NGQRW_R);
 2080         mtx_unlock_spin(&(ngq->q_mtx));
 2081 
 2082         return (NULL);
 2083 }
 2084 
 2085 static __inline item_p
 2086 ng_acquire_write(struct ng_queue *ngq, item_p item)
 2087 {
 2088         KASSERT(ngq != &ng_deadnode.nd_input_queue,
 2089             ("%s: working on deadnode", __func__));
 2090 
 2091 restart:
 2092         mtx_lock_spin(&(ngq->q_mtx));
 2093         /*
 2094          * If there are no readers, no writer, and no pending packets, then
 2095          * we can just go ahead. In all other situations we need to queue the
 2096          * request
 2097          */
 2098         if ((ngq->q_flags & NGQ_WMASK) == 0) {
 2099                 /* collision could happen *HERE* */
 2100                 atomic_add_long(&ngq->q_flags, WRITER_ACTIVE);
 2101                 mtx_unlock_spin((&ngq->q_mtx));
 2102                 if (ngq->q_flags & READER_MASK) {
 2103                         /* Collision with fast-track reader */
 2104                         atomic_subtract_long(&ngq->q_flags, WRITER_ACTIVE);
 2105                         goto restart;
 2106                 }
 2107                 CTR4(KTR_NET, "%20s: node [%x] (%p) acquired item %p",
 2108                     __func__, ngq->q_node->nd_ID, ngq->q_node, item);
 2109                 return (item);
 2110         }
 2111 
 2112         /*
 2113          * and queue the request for later.
 2114          */
 2115         ng_queue_rw(ngq, item, NGQRW_W);
 2116         mtx_unlock_spin(&(ngq->q_mtx));
 2117 
 2118         return (NULL);
 2119 }
 2120 
 2121 static __inline void
 2122 ng_leave_read(struct ng_queue *ngq)
 2123 {
 2124         atomic_subtract_long(&ngq->q_flags, READER_INCREMENT);
 2125 }
 2126 
 2127 static __inline void
 2128 ng_leave_write(struct ng_queue *ngq)
 2129 {
 2130         atomic_subtract_long(&ngq->q_flags, WRITER_ACTIVE);
 2131 }
 2132 
 2133 static void
 2134 ng_flush_input_queue(struct ng_queue * ngq)
 2135 {
 2136         item_p item;
 2137 
 2138         mtx_lock_spin(&ngq->q_mtx);
 2139         while (ngq->queue) {
 2140                 item = ngq->queue;
 2141                 ngq->queue = item->el_next;
 2142                 if (ngq->last == &(item->el_next)) {
 2143                         ngq->last = &(ngq->queue);
 2144                         atomic_add_long(&ngq->q_flags, -OP_PENDING);
 2145                 }
 2146                 mtx_unlock_spin(&ngq->q_mtx);
 2147                 /* If the item is supplying a callback, call it with an error */
 2148                 if (item->apply != NULL &&
 2149                     refcount_release(&item->apply->refs)) {
 2150                         (*item->apply->apply)(item->apply->context, ENOENT);
 2151                 }
 2152                 NG_FREE_ITEM(item);
 2153                 mtx_lock_spin(&ngq->q_mtx);
 2154         }
 2155         /*
 2156          * Take us off the work queue if we are there.
 2157          * We definately have no work to be done.
 2158          */
 2159         ng_worklist_remove(ngq->q_node);
 2160         mtx_unlock_spin(&ngq->q_mtx);
 2161 }
 2162 
 2163 /***********************************************************************
 2164 * Externally visible method for sending or queueing messages or data.
 2165 ***********************************************************************/
 2166 
 2167 /*
 2168  * The module code should have filled out the item correctly by this stage:
 2169  * Common:
 2170  *    reference to destination node.
 2171  *    Reference to destination rcv hook if relevant.
 2172  *    apply pointer must be or NULL or reference valid struct ng_apply_info.
 2173  * Data:
 2174  *    pointer to mbuf
 2175  * Control_Message:
 2176  *    pointer to msg.
 2177  *    ID of original sender node. (return address)
 2178  * Function:
 2179  *    Function pointer
 2180  *    void * argument
 2181  *    integer argument
 2182  *
 2183  * The nodes have several routines and macros to help with this task:
 2184  */
 2185 
 2186 int
 2187 ng_snd_item(item_p item, int flags)
 2188 {
 2189         hook_p hook;
 2190         node_p node;
 2191         int queue, rw;
 2192         struct ng_queue *ngq;
 2193         int error = 0;
 2194 
 2195         if (item == NULL) {
 2196                 TRAP_ERROR();
 2197                 return (EINVAL);        /* failed to get queue element */
 2198         }
 2199 
 2200 #ifdef  NETGRAPH_DEBUG
 2201         _ngi_check(item, __FILE__, __LINE__);
 2202 #endif
 2203 
 2204         if (item->apply)
 2205                 refcount_acquire(&item->apply->refs);
 2206 
 2207         hook = NGI_HOOK(item);
 2208         node = NGI_NODE(item);
 2209         ngq = &node->nd_input_queue;
 2210         if (node == NULL) {
 2211                 TRAP_ERROR();
 2212                 ERROUT(EINVAL); /* No address */
 2213         }
 2214 
 2215         queue = (flags & NG_QUEUE) ? 1 : 0;
 2216 
 2217         switch(item->el_flags & NGQF_TYPE) {
 2218         case NGQF_DATA:
 2219                 /*
 2220                  * DATA MESSAGE
 2221                  * Delivered to a node via a non-optional hook.
 2222                  * Both should be present in the item even though
 2223                  * the node is derivable from the hook.
 2224                  * References are held on both by the item.
 2225                  */
 2226 
 2227                 /* Protect nodes from sending NULL pointers
 2228                  * to each other
 2229                  */
 2230                 if (NGI_M(item) == NULL)
 2231                         ERROUT(EINVAL);
 2232 
 2233                 CHECK_DATA_MBUF(NGI_M(item));
 2234                 if (hook == NULL) {
 2235                         TRAP_ERROR();
 2236                         ERROUT(EINVAL);
 2237                 }
 2238                 if ((NG_HOOK_NOT_VALID(hook))
 2239                 || (NG_NODE_NOT_VALID(NG_HOOK_NODE(hook)))) {
 2240                         ERROUT(ENOTCONN);
 2241                 }
 2242                 if ((hook->hk_flags & HK_QUEUE)) {
 2243                         queue = 1;
 2244                 }
 2245                 break;
 2246         case NGQF_MESG:
 2247                 /*
 2248                  * CONTROL MESSAGE
 2249                  * Delivered to a node.
 2250                  * Hook is optional.
 2251                  * References are held by the item on the node and
 2252                  * the hook if it is present.
 2253                  */
 2254                 if (hook && (hook->hk_flags & HK_QUEUE)) {
 2255                         queue = 1;
 2256                 }
 2257                 break;
 2258         case NGQF_FN:
 2259         case NGQF_FN2:
 2260                 break;
 2261         default:
 2262                 TRAP_ERROR();
 2263                 ERROUT(EINVAL);
 2264         }
 2265         switch(item->el_flags & NGQF_RW) {
 2266         case NGQF_READER:
 2267                 rw = NGQRW_R;
 2268                 break;
 2269         case NGQF_WRITER:
 2270                 rw = NGQRW_W;
 2271                 break;
 2272         default:
 2273                 panic("%s: invalid item flags %lx", __func__, item->el_flags);
 2274         }
 2275 
 2276         /*
 2277          * If the node specifies single threading, force writer semantics.
 2278          * Similarly, the node may say one hook always produces writers.
 2279          * These are overrides.
 2280          */
 2281         if ((node->nd_flags & NGF_FORCE_WRITER)
 2282             || (hook && (hook->hk_flags & HK_FORCE_WRITER)))
 2283                         rw = NGQRW_W;
 2284 
 2285         if (queue) {
 2286                 /* Put it on the queue for that node*/
 2287 #ifdef  NETGRAPH_DEBUG
 2288                 _ngi_check(item, __FILE__, __LINE__);
 2289 #endif
 2290                 mtx_lock_spin(&(ngq->q_mtx));
 2291                 ng_queue_rw(ngq, item, rw);
 2292                 mtx_unlock_spin(&(ngq->q_mtx));
 2293 
 2294                 if (flags & NG_PROGRESS)
 2295                         return (EINPROGRESS);
 2296                 else
 2297                         return (0);
 2298         }
 2299 
 2300         /*
 2301          * We already decided how we will be queueud or treated.
 2302          * Try get the appropriate operating permission.
 2303          */
 2304         if (rw == NGQRW_R)
 2305                 item = ng_acquire_read(ngq, item);
 2306         else
 2307                 item = ng_acquire_write(ngq, item);
 2308 
 2309 
 2310         if (item == NULL) {
 2311                 if (flags & NG_PROGRESS)
 2312                         return (EINPROGRESS);
 2313                 else
 2314                         return (0);
 2315         }
 2316 
 2317 #ifdef  NETGRAPH_DEBUG
 2318         _ngi_check(item, __FILE__, __LINE__);
 2319 #endif
 2320 
 2321         NGI_GET_NODE(item, node); /* zaps stored node */
 2322 
 2323         error = ng_apply_item(node, item, rw); /* drops r/w lock when done */
 2324 
 2325         /*
 2326          * If the node goes away when we remove the reference,
 2327          * whatever we just did caused it.. whatever we do, DO NOT
 2328          * access the node again!
 2329          */
 2330         if (NG_NODE_UNREF(node) == 0) {
 2331                 return (error);
 2332         }
 2333 
 2334         mtx_lock_spin(&(ngq->q_mtx));
 2335         if (NEXT_QUEUED_ITEM_CAN_PROCEED(ngq))
 2336                 ng_setisr(ngq->q_node);
 2337         mtx_unlock_spin(&(ngq->q_mtx));
 2338 
 2339         return (error);
 2340 
 2341 done:
 2342         /* Apply callback. */
 2343         if (item->apply != NULL &&
 2344             refcount_release(&item->apply->refs)) {
 2345                 (*item->apply->apply)(item->apply->context, error);
 2346         }
 2347         NG_FREE_ITEM(item);
 2348         return (error);
 2349 }
 2350 
 2351 /*
 2352  * We have an item that was possibly queued somewhere.
 2353  * It should contain all the information needed
 2354  * to run it on the appropriate node/hook.
 2355  * If there is apply pointer and we own the last reference, call apply().
 2356  */
 2357 static int
 2358 ng_apply_item(node_p node, item_p item, int rw)
 2359 {
 2360         hook_p  hook;
 2361         int     error = 0;
 2362         ng_rcvdata_t *rcvdata;
 2363         ng_rcvmsg_t *rcvmsg;
 2364         struct ng_apply_info *apply;
 2365 
 2366         NGI_GET_HOOK(item, hook); /* clears stored hook */
 2367 #ifdef  NETGRAPH_DEBUG
 2368         _ngi_check(item, __FILE__, __LINE__);
 2369 #endif
 2370 
 2371         apply = item->apply;
 2372 
 2373         switch (item->el_flags & NGQF_TYPE) {
 2374         case NGQF_DATA:
 2375                 /*
 2376                  * Check things are still ok as when we were queued.
 2377                  */
 2378                 if ((hook == NULL)
 2379                 || NG_HOOK_NOT_VALID(hook)
 2380                 || NG_NODE_NOT_VALID(node) ) {
 2381                         error = EIO;
 2382                         NG_FREE_ITEM(item);
 2383                         break;
 2384                 }
 2385                 /*
 2386                  * If no receive method, just silently drop it.
 2387                  * Give preference to the hook over-ride method
 2388                  */
 2389                 if ((!(rcvdata = hook->hk_rcvdata))
 2390                 && (!(rcvdata = NG_HOOK_NODE(hook)->nd_type->rcvdata))) {
 2391                         error = 0;
 2392                         NG_FREE_ITEM(item);
 2393                         break;
 2394                 }
 2395                 error = (*rcvdata)(hook, item);
 2396                 break;
 2397         case NGQF_MESG:
 2398                 if (hook) {
 2399                         if (NG_HOOK_NOT_VALID(hook)) {
 2400                                 /*
 2401                                  * The hook has been zapped then we can't
 2402                                  * use it. Immediately drop its reference.
 2403                                  * The message may not need it.
 2404                                  */
 2405                                 NG_HOOK_UNREF(hook);
 2406                                 hook = NULL;
 2407                         }
 2408                 }
 2409                 /*
 2410                  * Similarly, if the node is a zombie there is
 2411                  * nothing we can do with it, drop everything.
 2412                  */
 2413                 if (NG_NODE_NOT_VALID(node)) {
 2414                         TRAP_ERROR();
 2415                         error = EINVAL;
 2416                         NG_FREE_ITEM(item);
 2417                 } else {
 2418                         /*
 2419                          * Call the appropriate message handler for the object.
 2420                          * It is up to the message handler to free the message.
 2421                          * If it's a generic message, handle it generically,
 2422                          * otherwise call the type's message handler
 2423                          * (if it exists)
 2424                          * XXX (race). Remember that a queued message may
 2425                          * reference a node or hook that has just been
 2426                          * invalidated. It will exist as the queue code
 2427                          * is holding a reference, but..
 2428                          */
 2429 
 2430                         struct ng_mesg *msg = NGI_MSG(item);
 2431 
 2432                         /*
 2433                          * check if the generic handler owns it.
 2434                          */
 2435                         if ((msg->header.typecookie == NGM_GENERIC_COOKIE)
 2436                         && ((msg->header.flags & NGF_RESP) == 0)) {
 2437                                 error = ng_generic_msg(node, item, hook);
 2438                                 break;
 2439                         }
 2440                         /*
 2441                          * Now see if there is a handler (hook or node specific)
 2442                          * in the target node. If none, silently discard.
 2443                          */
 2444                         if (((!hook) || (!(rcvmsg = hook->hk_rcvmsg)))
 2445                         && (!(rcvmsg = node->nd_type->rcvmsg))) {
 2446                                 TRAP_ERROR();
 2447                                 error = 0;
 2448                                 NG_FREE_ITEM(item);
 2449                                 break;
 2450                         }
 2451                         error = (*rcvmsg)(node, item, hook);
 2452                 }
 2453                 break;
 2454         case NGQF_FN:
 2455         case NGQF_FN2:
 2456                 /*
 2457                  *  We have to implicitly trust the hook,
 2458                  * as some of these are used for system purposes
 2459                  * where the hook is invalid. In the case of
 2460                  * the shutdown message we allow it to hit
 2461                  * even if the node is invalid.
 2462                  */
 2463                 if ((NG_NODE_NOT_VALID(node))
 2464                 && (NGI_FN(item) != &ng_rmnode)) {
 2465                         TRAP_ERROR();
 2466                         error = EINVAL;
 2467                         NG_FREE_ITEM(item);
 2468                         break;
 2469                 }
 2470                 if ((item->el_flags & NGQF_TYPE) == NGQF_FN) {
 2471                         (*NGI_FN(item))(node, hook, NGI_ARG1(item),
 2472                             NGI_ARG2(item));
 2473                         NG_FREE_ITEM(item);
 2474                 } else  /* it is NGQF_FN2 */
 2475                         error = (*NGI_FN2(item))(node, item, hook);
 2476                 break;
 2477         }
 2478         /*
 2479          * We held references on some of the resources
 2480          * that we took from the item. Now that we have
 2481          * finished doing everything, drop those references.
 2482          */
 2483         if (hook) {
 2484                 NG_HOOK_UNREF(hook);
 2485         }
 2486 
 2487         if (rw == NGQRW_R) {
 2488                 ng_leave_read(&node->nd_input_queue);
 2489         } else {
 2490                 ng_leave_write(&node->nd_input_queue);
 2491         }
 2492 
 2493         /* Apply callback. */
 2494         if (apply != NULL &&
 2495             refcount_release(&apply->refs)) {
 2496                 (*apply->apply)(apply->context, error);
 2497         }
 2498 
 2499         return (error);
 2500 }
 2501 
 2502 /***********************************************************************
 2503  * Implement the 'generic' control messages
 2504  ***********************************************************************/
 2505 static int
 2506 ng_generic_msg(node_p here, item_p item, hook_p lasthook)
 2507 {
 2508         int error = 0;
 2509         struct ng_mesg *msg;
 2510         struct ng_mesg *resp = NULL;
 2511 
 2512         NGI_GET_MSG(item, msg);
 2513         if (msg->header.typecookie != NGM_GENERIC_COOKIE) {
 2514                 TRAP_ERROR();
 2515                 error = EINVAL;
 2516                 goto out;
 2517         }
 2518         switch (msg->header.cmd) {
 2519         case NGM_SHUTDOWN:
 2520                 ng_rmnode(here, NULL, NULL, 0);
 2521                 break;
 2522         case NGM_MKPEER:
 2523             {
 2524                 struct ngm_mkpeer *const mkp = (struct ngm_mkpeer *) msg->data;
 2525 
 2526                 if (msg->header.arglen != sizeof(*mkp)) {
 2527                         TRAP_ERROR();
 2528                         error = EINVAL;
 2529                         break;
 2530                 }
 2531                 mkp->type[sizeof(mkp->type) - 1] = '\0';
 2532                 mkp->ourhook[sizeof(mkp->ourhook) - 1] = '\0';
 2533                 mkp->peerhook[sizeof(mkp->peerhook) - 1] = '\0';
 2534                 error = ng_mkpeer(here, mkp->ourhook, mkp->peerhook, mkp->type);
 2535                 break;
 2536             }
 2537         case NGM_CONNECT:
 2538             {
 2539                 struct ngm_connect *const con =
 2540                         (struct ngm_connect *) msg->data;
 2541                 node_p node2;
 2542 
 2543                 if (msg->header.arglen != sizeof(*con)) {
 2544                         TRAP_ERROR();
 2545                         error = EINVAL;
 2546                         break;
 2547                 }
 2548                 con->path[sizeof(con->path) - 1] = '\0';
 2549                 con->ourhook[sizeof(con->ourhook) - 1] = '\0';
 2550                 con->peerhook[sizeof(con->peerhook) - 1] = '\0';
 2551                 /* Don't forget we get a reference.. */
 2552                 error = ng_path2noderef(here, con->path, &node2, NULL);
 2553                 if (error)
 2554                         break;
 2555                 error = ng_con_nodes(item, here, con->ourhook,
 2556                     node2, con->peerhook);
 2557                 NG_NODE_UNREF(node2);
 2558                 break;
 2559             }
 2560         case NGM_NAME:
 2561             {
 2562                 struct ngm_name *const nam = (struct ngm_name *) msg->data;
 2563 
 2564                 if (msg->header.arglen != sizeof(*nam)) {
 2565                         TRAP_ERROR();
 2566                         error = EINVAL;
 2567                         break;
 2568                 }
 2569                 nam->name[sizeof(nam->name) - 1] = '\0';
 2570                 error = ng_name_node(here, nam->name);
 2571                 break;
 2572             }
 2573         case NGM_RMHOOK:
 2574             {
 2575                 struct ngm_rmhook *const rmh = (struct ngm_rmhook *) msg->data;
 2576                 hook_p hook;
 2577 
 2578                 if (msg->header.arglen != sizeof(*rmh)) {
 2579                         TRAP_ERROR();
 2580                         error = EINVAL;
 2581                         break;
 2582                 }
 2583                 rmh->ourhook[sizeof(rmh->ourhook) - 1] = '\0';
 2584                 if ((hook = ng_findhook(here, rmh->ourhook)) != NULL)
 2585                         ng_destroy_hook(hook);
 2586                 break;
 2587             }
 2588         case NGM_NODEINFO:
 2589             {
 2590                 struct nodeinfo *ni;
 2591 
 2592                 NG_MKRESPONSE(resp, msg, sizeof(*ni), M_NOWAIT);
 2593                 if (resp == NULL) {
 2594                         error = ENOMEM;
 2595                         break;
 2596                 }
 2597 
 2598                 /* Fill in node info */
 2599                 ni = (struct nodeinfo *) resp->data;
 2600                 if (NG_NODE_HAS_NAME(here))
 2601                         strcpy(ni->name, NG_NODE_NAME(here));
 2602                 strcpy(ni->type, here->nd_type->name);
 2603                 ni->id = ng_node2ID(here);
 2604                 ni->hooks = here->nd_numhooks;
 2605                 break;
 2606             }
 2607         case NGM_LISTHOOKS:
 2608             {
 2609                 const int nhooks = here->nd_numhooks;
 2610                 struct hooklist *hl;
 2611                 struct nodeinfo *ni;
 2612                 hook_p hook;
 2613 
 2614                 /* Get response struct */
 2615                 NG_MKRESPONSE(resp, msg, sizeof(*hl)
 2616                     + (nhooks * sizeof(struct linkinfo)), M_NOWAIT);
 2617                 if (resp == NULL) {
 2618                         error = ENOMEM;
 2619                         break;
 2620                 }
 2621                 hl = (struct hooklist *) resp->data;
 2622                 ni = &hl->nodeinfo;
 2623 
 2624                 /* Fill in node info */
 2625                 if (NG_NODE_HAS_NAME(here))
 2626                         strcpy(ni->name, NG_NODE_NAME(here));
 2627                 strcpy(ni->type, here->nd_type->name);
 2628                 ni->id = ng_node2ID(here);
 2629 
 2630                 /* Cycle through the linked list of hooks */
 2631                 ni->hooks = 0;
 2632                 LIST_FOREACH(hook, &here->nd_hooks, hk_hooks) {
 2633                         struct linkinfo *const link = &hl->link[ni->hooks];
 2634 
 2635                         if (ni->hooks >= nhooks) {
 2636                                 log(LOG_ERR, "%s: number of %s changed\n",
 2637                                     __func__, "hooks");
 2638                                 break;
 2639                         }
 2640                         if (NG_HOOK_NOT_VALID(hook))
 2641                                 continue;
 2642                         strcpy(link->ourhook, NG_HOOK_NAME(hook));
 2643                         strcpy(link->peerhook, NG_PEER_HOOK_NAME(hook));
 2644                         if (NG_PEER_NODE_NAME(hook)[0] != '\0')
 2645                                 strcpy(link->nodeinfo.name,
 2646                                     NG_PEER_NODE_NAME(hook));
 2647                         strcpy(link->nodeinfo.type,
 2648                            NG_PEER_NODE(hook)->nd_type->name);
 2649                         link->nodeinfo.id = ng_node2ID(NG_PEER_NODE(hook));
 2650                         link->nodeinfo.hooks = NG_PEER_NODE(hook)->nd_numhooks;
 2651                         ni->hooks++;
 2652                 }
 2653                 break;
 2654             }
 2655 
 2656         case NGM_LISTNAMES:
 2657         case NGM_LISTNODES:
 2658             {
 2659                 const int unnamed = (msg->header.cmd == NGM_LISTNODES);
 2660                 struct namelist *nl;
 2661                 node_p node;
 2662                 int num = 0;
 2663 
 2664                 mtx_lock(&ng_nodelist_mtx);
 2665                 /* Count number of nodes */
 2666                 LIST_FOREACH(node, &ng_nodelist, nd_nodes) {
 2667                         if (NG_NODE_IS_VALID(node)
 2668                         && (unnamed || NG_NODE_HAS_NAME(node))) {
 2669                                 num++;
 2670                         }
 2671                 }
 2672                 mtx_unlock(&ng_nodelist_mtx);
 2673 
 2674                 /* Get response struct */
 2675                 NG_MKRESPONSE(resp, msg, sizeof(*nl)
 2676                     + (num * sizeof(struct nodeinfo)), M_NOWAIT);
 2677                 if (resp == NULL) {
 2678                         error = ENOMEM;
 2679                         break;
 2680                 }
 2681                 nl = (struct namelist *) resp->data;
 2682 
 2683                 /* Cycle through the linked list of nodes */
 2684                 nl->numnames = 0;
 2685                 mtx_lock(&ng_nodelist_mtx);
 2686                 LIST_FOREACH(node, &ng_nodelist, nd_nodes) {
 2687                         struct nodeinfo *const np = &nl->nodeinfo[nl->numnames];
 2688 
 2689                         if (NG_NODE_NOT_VALID(node))
 2690                                 continue;
 2691                         if (!unnamed && (! NG_NODE_HAS_NAME(node)))
 2692                                 continue;
 2693                         if (nl->numnames >= num) {
 2694                                 log(LOG_ERR, "%s: number of %s changed\n",
 2695                                     __func__, "nodes");
 2696                                 break;
 2697                         }
 2698                         if (NG_NODE_HAS_NAME(node))
 2699                                 strcpy(np->name, NG_NODE_NAME(node));
 2700                         strcpy(np->type, node->nd_type->name);
 2701                         np->id = ng_node2ID(node);
 2702                         np->hooks = node->nd_numhooks;
 2703                         nl->numnames++;
 2704                 }
 2705                 mtx_unlock(&ng_nodelist_mtx);
 2706                 break;
 2707             }
 2708 
 2709         case NGM_LISTTYPES:
 2710             {
 2711                 struct typelist *tl;
 2712                 struct ng_type *type;
 2713                 int num = 0;
 2714 
 2715                 mtx_lock(&ng_typelist_mtx);
 2716                 /* Count number of types */
 2717                 LIST_FOREACH(type, &ng_typelist, types) {
 2718                         num++;
 2719                 }
 2720                 mtx_unlock(&ng_typelist_mtx);
 2721 
 2722                 /* Get response struct */
 2723                 NG_MKRESPONSE(resp, msg, sizeof(*tl)
 2724                     + (num * sizeof(struct typeinfo)), M_NOWAIT);
 2725                 if (resp == NULL) {
 2726                         error = ENOMEM;
 2727                         break;
 2728                 }
 2729                 tl = (struct typelist *) resp->data;
 2730 
 2731                 /* Cycle through the linked list of types */
 2732                 tl->numtypes = 0;
 2733                 mtx_lock(&ng_typelist_mtx);
 2734                 LIST_FOREACH(type, &ng_typelist, types) {
 2735                         struct typeinfo *const tp = &tl->typeinfo[tl->numtypes];
 2736 
 2737                         if (tl->numtypes >= num) {
 2738                                 log(LOG_ERR, "%s: number of %s changed\n",
 2739                                     __func__, "types");
 2740                                 break;
 2741                         }
 2742                         strcpy(tp->type_name, type->name);
 2743                         tp->numnodes = type->refs - 1; /* don't count list */
 2744                         tl->numtypes++;
 2745                 }
 2746                 mtx_unlock(&ng_typelist_mtx);
 2747                 break;
 2748             }
 2749 
 2750         case NGM_BINARY2ASCII:
 2751             {
 2752                 int bufSize = 20 * 1024;        /* XXX hard coded constant */
 2753                 const struct ng_parse_type *argstype;
 2754                 const struct ng_cmdlist *c;
 2755                 struct ng_mesg *binary, *ascii;
 2756 
 2757                 /* Data area must contain a valid netgraph message */
 2758                 binary = (struct ng_mesg *)msg->data;
 2759                 if (msg->header.arglen < sizeof(struct ng_mesg) ||
 2760                     (msg->header.arglen - sizeof(struct ng_mesg) <
 2761                     binary->header.arglen)) {
 2762                         TRAP_ERROR();
 2763                         error = EINVAL;
 2764                         break;
 2765                 }
 2766 
 2767                 /* Get a response message with lots of room */
 2768                 NG_MKRESPONSE(resp, msg, sizeof(*ascii) + bufSize, M_NOWAIT);
 2769                 if (resp == NULL) {
 2770                         error = ENOMEM;
 2771                         break;
 2772                 }
 2773                 ascii = (struct ng_mesg *)resp->data;
 2774 
 2775                 /* Copy binary message header to response message payload */
 2776                 bcopy(binary, ascii, sizeof(*binary));
 2777 
 2778                 /* Find command by matching typecookie and command number */
 2779                 for (c = here->nd_type->cmdlist;
 2780                     c != NULL && c->name != NULL; c++) {
 2781                         if (binary->header.typecookie == c->cookie
 2782                             && binary->header.cmd == c->cmd)
 2783                                 break;
 2784                 }
 2785                 if (c == NULL || c->name == NULL) {
 2786                         for (c = ng_generic_cmds; c->name != NULL; c++) {
 2787                                 if (binary->header.typecookie == c->cookie
 2788                                     && binary->header.cmd == c->cmd)
 2789                                         break;
 2790                         }
 2791                         if (c->name == NULL) {
 2792                                 NG_FREE_MSG(resp);
 2793                                 error = ENOSYS;
 2794                                 break;
 2795                         }
 2796                 }
 2797 
 2798                 /* Convert command name to ASCII */
 2799                 snprintf(ascii->header.cmdstr, sizeof(ascii->header.cmdstr),
 2800                     "%s", c->name);
 2801 
 2802                 /* Convert command arguments to ASCII */
 2803                 argstype = (binary->header.flags & NGF_RESP) ?
 2804                     c->respType : c->mesgType;
 2805                 if (argstype == NULL) {
 2806                         *ascii->data = '\0';
 2807                 } else {
 2808                         if ((error = ng_unparse(argstype,
 2809                             (u_char *)binary->data,
 2810                             ascii->data, bufSize)) != 0) {
 2811                                 NG_FREE_MSG(resp);
 2812                                 break;
 2813                         }
 2814                 }
 2815 
 2816                 /* Return the result as struct ng_mesg plus ASCII string */
 2817                 bufSize = strlen(ascii->data) + 1;
 2818                 ascii->header.arglen = bufSize;
 2819                 resp->header.arglen = sizeof(*ascii) + bufSize;
 2820                 break;
 2821             }
 2822 
 2823         case NGM_ASCII2BINARY:
 2824             {
 2825                 int bufSize = 2000;     /* XXX hard coded constant */
 2826                 const struct ng_cmdlist *c;
 2827                 const struct ng_parse_type *argstype;
 2828                 struct ng_mesg *ascii, *binary;
 2829                 int off = 0;
 2830 
 2831                 /* Data area must contain at least a struct ng_mesg + '\0' */
 2832                 ascii = (struct ng_mesg *)msg->data;
 2833                 if ((msg->header.arglen < sizeof(*ascii) + 1) ||
 2834                     (ascii->header.arglen < 1) ||
 2835                     (msg->header.arglen < sizeof(*ascii) +
 2836                     ascii->header.arglen)) {
 2837                         TRAP_ERROR();
 2838                         error = EINVAL;
 2839                         break;
 2840                 }
 2841                 ascii->data[ascii->header.arglen - 1] = '\0';
 2842 
 2843                 /* Get a response message with lots of room */
 2844                 NG_MKRESPONSE(resp, msg, sizeof(*binary) + bufSize, M_NOWAIT);
 2845                 if (resp == NULL) {
 2846                         error = ENOMEM;
 2847                         break;
 2848                 }
 2849                 binary = (struct ng_mesg *)resp->data;
 2850 
 2851                 /* Copy ASCII message header to response message payload */
 2852                 bcopy(ascii, binary, sizeof(*ascii));
 2853 
 2854                 /* Find command by matching ASCII command string */
 2855                 for (c = here->nd_type->cmdlist;
 2856                     c != NULL && c->name != NULL; c++) {
 2857                         if (strcmp(ascii->header.cmdstr, c->name) == 0)
 2858                                 break;
 2859                 }
 2860                 if (c == NULL || c->name == NULL) {
 2861                         for (c = ng_generic_cmds; c->name != NULL; c++) {
 2862                                 if (strcmp(ascii->header.cmdstr, c->name) == 0)
 2863                                         break;
 2864                         }
 2865                         if (c->name == NULL) {
 2866                                 NG_FREE_MSG(resp);
 2867                                 error = ENOSYS;
 2868                                 break;
 2869                         }
 2870                 }
 2871 
 2872                 /* Convert command name to binary */
 2873                 binary->header.cmd = c->cmd;
 2874                 binary->header.typecookie = c->cookie;
 2875 
 2876                 /* Convert command arguments to binary */
 2877                 argstype = (binary->header.flags & NGF_RESP) ?
 2878                     c->respType : c->mesgType;
 2879                 if (argstype == NULL) {
 2880                         bufSize = 0;
 2881                 } else {
 2882                         if ((error = ng_parse(argstype, ascii->data,
 2883                             &off, (u_char *)binary->data, &bufSize)) != 0) {
 2884                                 NG_FREE_MSG(resp);
 2885                                 break;
 2886                         }
 2887                 }
 2888 
 2889                 /* Return the result */
 2890                 binary->header.arglen = bufSize;
 2891                 resp->header.arglen = sizeof(*binary) + bufSize;
 2892                 break;
 2893             }
 2894 
 2895         case NGM_TEXT_CONFIG:
 2896         case NGM_TEXT_STATUS:
 2897                 /*
 2898                  * This one is tricky as it passes the command down to the
 2899                  * actual node, even though it is a generic type command.
 2900                  * This means we must assume that the item/msg is already freed
 2901                  * when control passes back to us.
 2902                  */
 2903                 if (here->nd_type->rcvmsg != NULL) {
 2904                         NGI_MSG(item) = msg; /* put it back as we found it */
 2905                         return((*here->nd_type->rcvmsg)(here, item, lasthook));
 2906                 }
 2907                 /* Fall through if rcvmsg not supported */
 2908         default:
 2909                 TRAP_ERROR();
 2910                 error = EINVAL;
 2911         }
 2912         /*
 2913          * Sometimes a generic message may be statically allocated
 2914          * to avoid problems with allocating when in tight memeory situations.
 2915          * Don't free it if it is so.
 2916          * I break them appart here, because erros may cause a free if the item
 2917          * in which case we'd be doing it twice.
 2918          * they are kept together above, to simplify freeing.
 2919          */
 2920 out:
 2921         NG_RESPOND_MSG(error, here, item, resp);
 2922         if (msg)
 2923                 NG_FREE_MSG(msg);
 2924         return (error);
 2925 }
 2926 
 2927 /************************************************************************
 2928                         Queue element get/free routines
 2929 ************************************************************************/
 2930 
 2931 uma_zone_t                      ng_qzone;
 2932 static int                      maxalloc = 512; /* limit the damage of a leak */
 2933 
 2934 TUNABLE_INT("net.graph.maxalloc", &maxalloc);
 2935 SYSCTL_INT(_net_graph, OID_AUTO, maxalloc, CTLFLAG_RDTUN, &maxalloc,
 2936     0, "Maximum number of queue items to allocate");
 2937 
 2938 #ifdef  NETGRAPH_DEBUG
 2939 static TAILQ_HEAD(, ng_item) ng_itemlist = TAILQ_HEAD_INITIALIZER(ng_itemlist);
 2940 static int                      allocated;      /* number of items malloc'd */
 2941 #endif
 2942 
 2943 /*
 2944  * Get a queue entry.
 2945  * This is usually called when a packet first enters netgraph.
 2946  * By definition, this is usually from an interrupt, or from a user.
 2947  * Users are not so important, but try be quick for the times that it's
 2948  * an interrupt.
 2949  */
 2950 static __inline item_p
 2951 ng_getqblk(int flags)
 2952 {
 2953         item_p item = NULL;
 2954         int wait;
 2955 
 2956         wait = (flags & NG_WAITOK) ? M_WAITOK : M_NOWAIT;
 2957 
 2958         item = uma_zalloc(ng_qzone, wait | M_ZERO);
 2959 
 2960 #ifdef  NETGRAPH_DEBUG
 2961         if (item) {
 2962                         mtx_lock(&ngq_mtx);
 2963                         TAILQ_INSERT_TAIL(&ng_itemlist, item, all);
 2964                         allocated++;
 2965                         mtx_unlock(&ngq_mtx);
 2966         }
 2967 #endif
 2968 
 2969         return (item);
 2970 }
 2971 
 2972 /*
 2973  * Release a queue entry
 2974  */
 2975 void
 2976 ng_free_item(item_p item)
 2977 {
 2978         /*
 2979          * The item may hold resources on it's own. We need to free
 2980          * these before we can free the item. What they are depends upon
 2981          * what kind of item it is. it is important that nodes zero
 2982          * out pointers to resources that they remove from the item
 2983          * or we release them again here.
 2984          */
 2985         switch (item->el_flags & NGQF_TYPE) {
 2986         case NGQF_DATA:
 2987                 /* If we have an mbuf still attached.. */
 2988                 NG_FREE_M(_NGI_M(item));
 2989                 break;
 2990         case NGQF_MESG:
 2991                 _NGI_RETADDR(item) = 0;
 2992                 NG_FREE_MSG(_NGI_MSG(item));
 2993                 break;
 2994         case NGQF_FN:
 2995         case NGQF_FN2:
 2996                 /* nothing to free really, */
 2997                 _NGI_FN(item) = NULL;
 2998                 _NGI_ARG1(item) = NULL;
 2999                 _NGI_ARG2(item) = 0;
 3000                 break;
 3001         }
 3002         /* If we still have a node or hook referenced... */
 3003         _NGI_CLR_NODE(item);
 3004         _NGI_CLR_HOOK(item);
 3005 
 3006 #ifdef  NETGRAPH_DEBUG
 3007         mtx_lock(&ngq_mtx);
 3008         TAILQ_REMOVE(&ng_itemlist, item, all);
 3009         allocated--;
 3010         mtx_unlock(&ngq_mtx);
 3011 #endif
 3012         uma_zfree(ng_qzone, item);
 3013 }
 3014 
 3015 /************************************************************************
 3016                         Module routines
 3017 ************************************************************************/
 3018 
 3019 /*
 3020  * Handle the loading/unloading of a netgraph node type module
 3021  */
 3022 int
 3023 ng_mod_event(module_t mod, int event, void *data)
 3024 {
 3025         struct ng_type *const type = data;
 3026         int s, error = 0;
 3027 
 3028         switch (event) {
 3029         case MOD_LOAD:
 3030 
 3031                 /* Register new netgraph node type */
 3032                 s = splnet();
 3033                 if ((error = ng_newtype(type)) != 0) {
 3034                         splx(s);
 3035                         break;
 3036                 }
 3037 
 3038                 /* Call type specific code */
 3039                 if (type->mod_event != NULL)
 3040                         if ((error = (*type->mod_event)(mod, event, data))) {
 3041                                 mtx_lock(&ng_typelist_mtx);
 3042                                 type->refs--;   /* undo it */
 3043                                 LIST_REMOVE(type, types);
 3044                                 mtx_unlock(&ng_typelist_mtx);
 3045                         }
 3046                 splx(s);
 3047                 break;
 3048 
 3049         case MOD_UNLOAD:
 3050                 s = splnet();
 3051                 if (type->refs > 1) {           /* make sure no nodes exist! */
 3052                         error = EBUSY;
 3053                 } else {
 3054                         if (type->refs == 0) {
 3055                                 /* failed load, nothing to undo */
 3056                                 splx(s);
 3057                                 break;
 3058                         }
 3059                         if (type->mod_event != NULL) {  /* check with type */
 3060                                 error = (*type->mod_event)(mod, event, data);
 3061                                 if (error != 0) {       /* type refuses.. */
 3062                                         splx(s);
 3063                                         break;
 3064                                 }
 3065                         }
 3066                         mtx_lock(&ng_typelist_mtx);
 3067                         LIST_REMOVE(type, types);
 3068                         mtx_unlock(&ng_typelist_mtx);
 3069                 }
 3070                 splx(s);
 3071                 break;
 3072 
 3073         default:
 3074                 if (type->mod_event != NULL)
 3075                         error = (*type->mod_event)(mod, event, data);
 3076                 else
 3077                         error = EOPNOTSUPP;             /* XXX ? */
 3078                 break;
 3079         }
 3080         return (error);
 3081 }
 3082 
 3083 /*
 3084  * Handle loading and unloading for this code.
 3085  * The only thing we need to link into is the NETISR strucure.
 3086  */
 3087 static int
 3088 ngb_mod_event(module_t mod, int event, void *data)
 3089 {
 3090         int error = 0;
 3091 
 3092         switch (event) {
 3093         case MOD_LOAD:
 3094                 /* Initialize everything. */
 3095                 mtx_init(&ng_worklist_mtx, "ng_worklist", NULL, MTX_SPIN);
 3096                 mtx_init(&ng_typelist_mtx, "netgraph types mutex", NULL,
 3097                     MTX_DEF);
 3098                 mtx_init(&ng_nodelist_mtx, "netgraph nodelist mutex", NULL,
 3099                     MTX_DEF);
 3100                 mtx_init(&ng_idhash_mtx, "netgraph idhash mutex", NULL,
 3101                     MTX_DEF);
 3102                 mtx_init(&ng_topo_mtx, "netgraph topology mutex", NULL,
 3103                     MTX_DEF);
 3104 #ifdef  NETGRAPH_DEBUG
 3105                 mtx_init(&ngq_mtx, "netgraph item list mutex", NULL,
 3106                     MTX_DEF);
 3107 #endif
 3108                 ng_qzone = uma_zcreate("NetGraph items", sizeof(struct ng_item),
 3109                     NULL, NULL, NULL, NULL, UMA_ALIGN_CACHE, 0);
 3110                 uma_zone_set_max(ng_qzone, maxalloc);
 3111                 netisr_register(NETISR_NETGRAPH, (netisr_t *)ngintr, NULL,
 3112                     NETISR_MPSAFE);
 3113                 break;
 3114         case MOD_UNLOAD:
 3115                 /* You can't unload it because an interface may be using it. */
 3116                 error = EBUSY;
 3117                 break;
 3118         default:
 3119                 error = EOPNOTSUPP;
 3120                 break;
 3121         }
 3122         return (error);
 3123 }
 3124 
 3125 static moduledata_t netgraph_mod = {
 3126         "netgraph",
 3127         ngb_mod_event,
 3128         (NULL)
 3129 };
 3130 DECLARE_MODULE(netgraph, netgraph_mod, SI_SUB_NETGRAPH, SI_ORDER_MIDDLE);
 3131 SYSCTL_NODE(_net, OID_AUTO, graph, CTLFLAG_RW, 0, "netgraph Family");
 3132 SYSCTL_INT(_net_graph, OID_AUTO, abi_version, CTLFLAG_RD, 0, NG_ABI_VERSION,"");
 3133 SYSCTL_INT(_net_graph, OID_AUTO, msg_version, CTLFLAG_RD, 0, NG_VERSION, "");
 3134 
 3135 #ifdef  NETGRAPH_DEBUG
 3136 void
 3137 dumphook (hook_p hook, char *file, int line)
 3138 {
 3139         printf("hook: name %s, %d refs, Last touched:\n",
 3140                 _NG_HOOK_NAME(hook), hook->hk_refs);
 3141         printf("        Last active @ %s, line %d\n",
 3142                 hook->lastfile, hook->lastline);
 3143         if (line) {
 3144                 printf(" problem discovered at file %s, line %d\n", file, line);
 3145         }
 3146 }
 3147 
 3148 void
 3149 dumpnode(node_p node, char *file, int line)
 3150 {
 3151         printf("node: ID [%x]: type '%s', %d hooks, flags 0x%x, %d refs, %s:\n",
 3152                 _NG_NODE_ID(node), node->nd_type->name,
 3153                 node->nd_numhooks, node->nd_flags,
 3154                 node->nd_refs, node->nd_name);
 3155         printf("        Last active @ %s, line %d\n",
 3156                 node->lastfile, node->lastline);
 3157         if (line) {
 3158                 printf(" problem discovered at file %s, line %d\n", file, line);
 3159         }
 3160 }
 3161 
 3162 void
 3163 dumpitem(item_p item, char *file, int line)
 3164 {
 3165         printf(" ACTIVE item, last used at %s, line %d",
 3166                 item->lastfile, item->lastline);
 3167         switch(item->el_flags & NGQF_TYPE) {
 3168         case NGQF_DATA:
 3169                 printf(" - [data]\n");
 3170                 break;
 3171         case NGQF_MESG:
 3172                 printf(" - retaddr[%d]:\n", _NGI_RETADDR(item));
 3173                 break;
 3174         case NGQF_FN:
 3175                 printf(" - fn@%p (%p, %p, %p, %d (%x))\n",
 3176                         _NGI_FN(item),
 3177                         _NGI_NODE(item),
 3178                         _NGI_HOOK(item),
 3179                         item->body.fn.fn_arg1,
 3180                         item->body.fn.fn_arg2,
 3181                         item->body.fn.fn_arg2);
 3182                 break;
 3183         case NGQF_FN2:
 3184                 printf(" - fn@%p (%p, %p, %p, %d (%x))\n",
 3185                         _NGI_FN2(item),
 3186                         _NGI_NODE(item),
 3187                         _NGI_HOOK(item),
 3188                         item->body.fn.fn_arg1,
 3189                         item->body.fn.fn_arg2,
 3190                         item->body.fn.fn_arg2);
 3191                 break;
 3192         }
 3193         if (line) {
 3194                 printf(" problem discovered at file %s, line %d\n", file, line);
 3195                 if (_NGI_NODE(item)) {
 3196                         printf("node %p ([%x])\n",
 3197                                 _NGI_NODE(item), ng_node2ID(_NGI_NODE(item)));
 3198                 }
 3199         }
 3200 }
 3201 
 3202 static void
 3203 ng_dumpitems(void)
 3204 {
 3205         item_p item;
 3206         int i = 1;
 3207         TAILQ_FOREACH(item, &ng_itemlist, all) {
 3208                 printf("[%d] ", i++);
 3209                 dumpitem(item, NULL, 0);
 3210         }
 3211 }
 3212 
 3213 static void
 3214 ng_dumpnodes(void)
 3215 {
 3216         node_p node;
 3217         int i = 1;
 3218         mtx_lock(&ng_nodelist_mtx);
 3219         SLIST_FOREACH(node, &ng_allnodes, nd_all) {
 3220                 printf("[%d] ", i++);
 3221                 dumpnode(node, NULL, 0);
 3222         }
 3223         mtx_unlock(&ng_nodelist_mtx);
 3224 }
 3225 
 3226 static void
 3227 ng_dumphooks(void)
 3228 {
 3229         hook_p hook;
 3230         int i = 1;
 3231         mtx_lock(&ng_nodelist_mtx);
 3232         SLIST_FOREACH(hook, &ng_allhooks, hk_all) {
 3233                 printf("[%d] ", i++);
 3234                 dumphook(hook, NULL, 0);
 3235         }
 3236         mtx_unlock(&ng_nodelist_mtx);
 3237 }
 3238 
 3239 static int
 3240 sysctl_debug_ng_dump_items(SYSCTL_HANDLER_ARGS)
 3241 {
 3242         int error;
 3243         int val;
 3244         int i;
 3245 
 3246         val = allocated;
 3247         i = 1;
 3248         error = sysctl_handle_int(oidp, &val, sizeof(int), req);
 3249         if (error != 0 || req->newptr == NULL)
 3250                 return (error);
 3251         if (val == 42) {
 3252                 ng_dumpitems();
 3253                 ng_dumpnodes();
 3254                 ng_dumphooks();
 3255         }
 3256         return (0);
 3257 }
 3258 
 3259 SYSCTL_PROC(_debug, OID_AUTO, ng_dump_items, CTLTYPE_INT | CTLFLAG_RW,
 3260     0, sizeof(int), sysctl_debug_ng_dump_items, "I", "Number of allocated items");
 3261 #endif  /* NETGRAPH_DEBUG */
 3262 
 3263 
 3264 /***********************************************************************
 3265 * Worklist routines
 3266 **********************************************************************/
 3267 /* NETISR thread enters here */
 3268 /*
 3269  * Pick a node off the list of nodes with work,
 3270  * try get an item to process off it.
 3271  * If there are no more, remove the node from the list.
 3272  */
 3273 static void
 3274 ngintr(void)
 3275 {
 3276         item_p item;
 3277         node_p  node = NULL;
 3278 
 3279         for (;;) {
 3280                 mtx_lock_spin(&ng_worklist_mtx);
 3281                 node = TAILQ_FIRST(&ng_worklist);
 3282                 if (!node) {
 3283                         mtx_unlock_spin(&ng_worklist_mtx);
 3284                         break;
 3285                 }
 3286                 node->nd_flags &= ~NGF_WORKQ;   
 3287                 TAILQ_REMOVE(&ng_worklist, node, nd_work);
 3288                 mtx_unlock_spin(&ng_worklist_mtx);
 3289                 CTR3(KTR_NET, "%20s: node [%x] (%p) taken off worklist",
 3290                     __func__, node->nd_ID, node);
 3291                 /*
 3292                  * We have the node. We also take over the reference
 3293                  * that the list had on it.
 3294                  * Now process as much as you can, until it won't
 3295                  * let you have another item off the queue.
 3296                  * All this time, keep the reference
 3297                  * that lets us be sure that the node still exists.
 3298                  * Let the reference go at the last minute.
 3299                  * ng_dequeue will put us back on the worklist
 3300                  * if there is more too do. This may be of use if there
 3301                  * are Multiple Processors and multiple Net threads in the
 3302                  * future.
 3303                  */
 3304                 for (;;) {
 3305                         int rw;
 3306 
 3307                         mtx_lock_spin(&node->nd_input_queue.q_mtx);
 3308                         item = ng_dequeue(&node->nd_input_queue, &rw);
 3309                         if (item == NULL) {
 3310                                 mtx_unlock_spin(&node->nd_input_queue.q_mtx);
 3311                                 break; /* go look for another node */
 3312                         } else {
 3313                                 mtx_unlock_spin(&node->nd_input_queue.q_mtx);
 3314                                 NGI_GET_NODE(item, node); /* zaps stored node */
 3315                                 ng_apply_item(node, item, rw);
 3316                                 NG_NODE_UNREF(node);
 3317                         }
 3318                 }
 3319                 NG_NODE_UNREF(node);
 3320         }
 3321 }
 3322 
 3323 static void
 3324 ng_worklist_remove(node_p node)
 3325 {
 3326         mtx_assert(&node->nd_input_queue.q_mtx, MA_OWNED);
 3327 
 3328         mtx_lock_spin(&ng_worklist_mtx);
 3329         if (node->nd_flags & NGF_WORKQ) {
 3330                 node->nd_flags &= ~NGF_WORKQ;
 3331                 TAILQ_REMOVE(&ng_worklist, node, nd_work);
 3332                 mtx_unlock_spin(&ng_worklist_mtx);
 3333                 NG_NODE_UNREF(node);
 3334                 CTR3(KTR_NET, "%20s: node [%x] (%p) removed from worklist",
 3335                     __func__, node->nd_ID, node);
 3336         } else {
 3337                 mtx_unlock_spin(&ng_worklist_mtx);
 3338         }
 3339 }
 3340 
 3341 /*
 3342  * XXX
 3343  * It's posible that a debugging NG_NODE_REF may need
 3344  * to be outside the mutex zone
 3345  */
 3346 static void
 3347 ng_setisr(node_p node)
 3348 {
 3349 
 3350         mtx_assert(&node->nd_input_queue.q_mtx, MA_OWNED);
 3351 
 3352         if ((node->nd_flags & NGF_WORKQ) == 0) {
 3353                 /*
 3354                  * If we are not already on the work queue,
 3355                  * then put us on.
 3356                  */
 3357                 node->nd_flags |= NGF_WORKQ;
 3358                 mtx_lock_spin(&ng_worklist_mtx);
 3359                 TAILQ_INSERT_TAIL(&ng_worklist, node, nd_work);
 3360                 mtx_unlock_spin(&ng_worklist_mtx);
 3361                 NG_NODE_REF(node); /* XXX fafe in mutex? */
 3362                 CTR3(KTR_NET, "%20s: node [%x] (%p) put on worklist", __func__,
 3363                     node->nd_ID, node);
 3364         } else
 3365                 CTR3(KTR_NET, "%20s: node [%x] (%p) already on worklist",
 3366                     __func__, node->nd_ID, node);
 3367         schednetisr(NETISR_NETGRAPH);
 3368 }
 3369 
 3370 
 3371 /***********************************************************************
 3372 * Externally useable functions to set up a queue item ready for sending
 3373 ***********************************************************************/
 3374 
 3375 #ifdef  NETGRAPH_DEBUG
 3376 #define ITEM_DEBUG_CHECKS                                               \
 3377         do {                                                            \
 3378                 if (NGI_NODE(item) ) {                                  \
 3379                         printf("item already has node");                \
 3380                         kdb_enter("has node");                          \
 3381                         NGI_CLR_NODE(item);                             \
 3382                 }                                                       \
 3383                 if (NGI_HOOK(item) ) {                                  \
 3384                         printf("item already has hook");                \
 3385                         kdb_enter("has hook");                          \
 3386                         NGI_CLR_HOOK(item);                             \
 3387                 }                                                       \
 3388         } while (0)
 3389 #else
 3390 #define ITEM_DEBUG_CHECKS
 3391 #endif
 3392 
 3393 /*
 3394  * Put mbuf into the item.
 3395  * Hook and node references will be removed when the item is dequeued.
 3396  * (or equivalent)
 3397  * (XXX) Unsafe because no reference held by peer on remote node.
 3398  * remote node might go away in this timescale.
 3399  * We know the hooks can't go away because that would require getting
 3400  * a writer item on both nodes and we must have at least a  reader
 3401  * here to be able to do this.
 3402  * Note that the hook loaded is the REMOTE hook.
 3403  *
 3404  * This is possibly in the critical path for new data.
 3405  */
 3406 item_p
 3407 ng_package_data(struct mbuf *m, int flags)
 3408 {
 3409         item_p item;
 3410 
 3411         if ((item = ng_getqblk(flags)) == NULL) {
 3412                 NG_FREE_M(m);
 3413                 return (NULL);
 3414         }
 3415         ITEM_DEBUG_CHECKS;
 3416         item->el_flags = NGQF_DATA | NGQF_READER;
 3417         item->el_next = NULL;
 3418         NGI_M(item) = m;
 3419         return (item);
 3420 }
 3421 
 3422 /*
 3423  * Allocate a queue item and put items into it..
 3424  * Evaluate the address as this will be needed to queue it and
 3425  * to work out what some of the fields should be.
 3426  * Hook and node references will be removed when the item is dequeued.
 3427  * (or equivalent)
 3428  */
 3429 item_p
 3430 ng_package_msg(struct ng_mesg *msg, int flags)
 3431 {
 3432         item_p item;
 3433 
 3434         if ((item = ng_getqblk(flags)) == NULL) {
 3435                 NG_FREE_MSG(msg);
 3436                 return (NULL);
 3437         }
 3438         ITEM_DEBUG_CHECKS;
 3439         /* Messages items count as writers unless explicitly exempted. */
 3440         if (msg->header.cmd & NGM_READONLY)
 3441                 item->el_flags = NGQF_MESG | NGQF_READER;
 3442         else
 3443                 item->el_flags = NGQF_MESG | NGQF_WRITER;
 3444         item->el_next = NULL;
 3445         /*
 3446          * Set the current lasthook into the queue item
 3447          */
 3448         NGI_MSG(item) = msg;
 3449         NGI_RETADDR(item) = 0;
 3450         return (item);
 3451 }
 3452 
 3453 
 3454 
 3455 #define SET_RETADDR(item, here, retaddr)                                \
 3456         do {    /* Data or fn items don't have retaddrs */              \
 3457                 if ((item->el_flags & NGQF_TYPE) == NGQF_MESG) {        \
 3458                         if (retaddr) {                                  \
 3459                                 NGI_RETADDR(item) = retaddr;            \
 3460                         } else {                                        \
 3461                                 /*                                      \
 3462                                  * The old return address should be ok. \
 3463                                  * If there isn't one, use the address  \
 3464                                  * here.                                \
 3465                                  */                                     \
 3466                                 if (NGI_RETADDR(item) == 0) {           \
 3467                                         NGI_RETADDR(item)               \
 3468                                                 = ng_node2ID(here);     \
 3469                                 }                                       \
 3470                         }                                               \
 3471                 }                                                       \
 3472         } while (0)
 3473 
 3474 int
 3475 ng_address_hook(node_p here, item_p item, hook_p hook, ng_ID_t retaddr)
 3476 {
 3477         hook_p peer;
 3478         node_p peernode;
 3479         ITEM_DEBUG_CHECKS;
 3480         /*
 3481          * Quick sanity check..
 3482          * Since a hook holds a reference on it's node, once we know
 3483          * that the peer is still connected (even if invalid,) we know
 3484          * that the peer node is present, though maybe invalid.
 3485          */
 3486         if ((hook == NULL)
 3487         || NG_HOOK_NOT_VALID(hook)
 3488         || (NG_HOOK_PEER(hook) == NULL)
 3489         || NG_HOOK_NOT_VALID(NG_HOOK_PEER(hook))
 3490         || NG_NODE_NOT_VALID(NG_PEER_NODE(hook))) {
 3491                 NG_FREE_ITEM(item);
 3492                 TRAP_ERROR();
 3493                 return (ENETDOWN);
 3494         }
 3495 
 3496         /*
 3497          * Transfer our interest to the other (peer) end.
 3498          */
 3499         peer = NG_HOOK_PEER(hook);
 3500         NG_HOOK_REF(peer);
 3501         NGI_SET_HOOK(item, peer);
 3502         peernode = NG_PEER_NODE(hook);
 3503         NG_NODE_REF(peernode);
 3504         NGI_SET_NODE(item, peernode);
 3505         SET_RETADDR(item, here, retaddr);
 3506         return (0);
 3507 }
 3508 
 3509 int
 3510 ng_address_path(node_p here, item_p item, char *address, ng_ID_t retaddr)
 3511 {
 3512         node_p  dest = NULL;
 3513         hook_p  hook = NULL;
 3514         int     error;
 3515 
 3516         ITEM_DEBUG_CHECKS;
 3517         /*
 3518          * Note that ng_path2noderef increments the reference count
 3519          * on the node for us if it finds one. So we don't have to.
 3520          */
 3521         error = ng_path2noderef(here, address, &dest, &hook);
 3522         if (error) {
 3523                 NG_FREE_ITEM(item);
 3524                 return (error);
 3525         }
 3526         NGI_SET_NODE(item, dest);
 3527         if ( hook) {
 3528                 NG_HOOK_REF(hook);      /* don't let it go while on the queue */
 3529                 NGI_SET_HOOK(item, hook);
 3530         }
 3531         SET_RETADDR(item, here, retaddr);
 3532         return (0);
 3533 }
 3534 
 3535 int
 3536 ng_address_ID(node_p here, item_p item, ng_ID_t ID, ng_ID_t retaddr)
 3537 {
 3538         node_p dest;
 3539 
 3540         ITEM_DEBUG_CHECKS;
 3541         /*
 3542          * Find the target node.
 3543          */
 3544         dest = ng_ID2noderef(ID); /* GETS REFERENCE! */
 3545         if (dest == NULL) {
 3546                 NG_FREE_ITEM(item);
 3547                 TRAP_ERROR();
 3548                 return(EINVAL);
 3549         }
 3550         /* Fill out the contents */
 3551         NGI_SET_NODE(item, dest);
 3552         NGI_CLR_HOOK(item);
 3553         SET_RETADDR(item, here, retaddr);
 3554         return (0);
 3555 }
 3556 
 3557 /*
 3558  * special case to send a message to self (e.g. destroy node)
 3559  * Possibly indicate an arrival hook too.
 3560  * Useful for removing that hook :-)
 3561  */
 3562 item_p
 3563 ng_package_msg_self(node_p here, hook_p hook, struct ng_mesg *msg)
 3564 {
 3565         item_p item;
 3566 
 3567         /*
 3568          * Find the target node.
 3569          * If there is a HOOK argument, then use that in preference
 3570          * to the address.
 3571          */
 3572         if ((item = ng_getqblk(NG_NOFLAGS)) == NULL) {
 3573                 NG_FREE_MSG(msg);
 3574                 return (NULL);
 3575         }
 3576 
 3577         /* Fill out the contents */
 3578         item->el_flags = NGQF_MESG | NGQF_WRITER;
 3579         item->el_next = NULL;
 3580         NG_NODE_REF(here);
 3581         NGI_SET_NODE(item, here);
 3582         if (hook) {
 3583                 NG_HOOK_REF(hook);
 3584                 NGI_SET_HOOK(item, hook);
 3585         }
 3586         NGI_MSG(item) = msg;
 3587         NGI_RETADDR(item) = ng_node2ID(here);
 3588         return (item);
 3589 }
 3590 
 3591 /*
 3592  * Send ng_item_fn function call to the specified node.
 3593  */
 3594 
 3595 int
 3596 ng_send_fn(node_p node, hook_p hook, ng_item_fn *fn, void * arg1, int arg2)
 3597 {
 3598 
 3599         return ng_send_fn1(node, hook, fn, arg1, arg2, NG_NOFLAGS);
 3600 }
 3601 
 3602 int
 3603 ng_send_fn1(node_p node, hook_p hook, ng_item_fn *fn, void * arg1, int arg2,
 3604         int flags)
 3605 {
 3606         item_p item;
 3607 
 3608         if ((item = ng_getqblk(flags)) == NULL) {
 3609                 return (ENOMEM);
 3610         }
 3611         item->el_flags = NGQF_FN | NGQF_WRITER;
 3612         NG_NODE_REF(node); /* and one for the item */
 3613         NGI_SET_NODE(item, node);
 3614         if (hook) {
 3615                 NG_HOOK_REF(hook);
 3616                 NGI_SET_HOOK(item, hook);
 3617         }
 3618         NGI_FN(item) = fn;
 3619         NGI_ARG1(item) = arg1;
 3620         NGI_ARG2(item) = arg2;
 3621         return(ng_snd_item(item, flags));
 3622 }
 3623 
 3624 /*
 3625  * Send ng_item_fn2 function call to the specified node.
 3626  *
 3627  * If an optional pitem parameter is supplied, its apply
 3628  * callback will be copied to the new item. If also NG_REUSE_ITEM
 3629  * flag is set, no new item will be allocated, but pitem will
 3630  * be used.
 3631  */
 3632 int
 3633 ng_send_fn2(node_p node, hook_p hook, item_p pitem, ng_item_fn2 *fn, void *arg1,
 3634         int arg2, int flags)
 3635 {
 3636         item_p item;
 3637 
 3638         KASSERT((pitem != NULL || (flags & NG_REUSE_ITEM) == 0),
 3639             ("%s: NG_REUSE_ITEM but no pitem", __func__));
 3640 
 3641         /*
 3642          * Allocate a new item if no supplied or
 3643          * if we can't use supplied one.
 3644          */
 3645         if (pitem == NULL || (flags & NG_REUSE_ITEM) == 0) {
 3646                 if ((item = ng_getqblk(flags)) == NULL)
 3647                         return (ENOMEM);
 3648         } else
 3649                 item = pitem;
 3650 
 3651         item->el_flags = NGQF_FN2 | NGQF_WRITER;
 3652         NG_NODE_REF(node); /* and one for the item */
 3653         NGI_SET_NODE(item, node);
 3654         if (hook) {
 3655                 NG_HOOK_REF(hook);
 3656                 NGI_SET_HOOK(item, hook);
 3657         }
 3658         NGI_FN2(item) = fn;
 3659         NGI_ARG1(item) = arg1;
 3660         NGI_ARG2(item) = arg2;
 3661         if (pitem != NULL && (flags & NG_REUSE_ITEM) == 0)
 3662                 item->apply = pitem->apply;
 3663         return(ng_snd_item(item, flags));
 3664 }
 3665 
 3666 /*
 3667  * Official timeout routines for Netgraph nodes.
 3668  */
 3669 static void
 3670 ng_callout_trampoline(void *arg)
 3671 {
 3672         item_p item = arg;
 3673 
 3674         ng_snd_item(item, 0);
 3675 }
 3676 
 3677 
 3678 int
 3679 ng_callout(struct callout *c, node_p node, hook_p hook, int ticks,
 3680     ng_item_fn *fn, void * arg1, int arg2)
 3681 {
 3682         item_p item, oitem;
 3683 
 3684         if ((item = ng_getqblk(NG_NOFLAGS)) == NULL)
 3685                 return (ENOMEM);
 3686 
 3687         item->el_flags = NGQF_FN | NGQF_WRITER;
 3688         NG_NODE_REF(node);              /* and one for the item */
 3689         NGI_SET_NODE(item, node);
 3690         if (hook) {
 3691                 NG_HOOK_REF(hook);
 3692                 NGI_SET_HOOK(item, hook);
 3693         }
 3694         NGI_FN(item) = fn;
 3695         NGI_ARG1(item) = arg1;
 3696         NGI_ARG2(item) = arg2;
 3697         oitem = c->c_arg;
 3698         if (callout_reset(c, ticks, &ng_callout_trampoline, item) == 1 &&
 3699             oitem != NULL)
 3700                 NG_FREE_ITEM(oitem);
 3701         return (0);
 3702 }
 3703 
 3704 /* A special modified version of untimeout() */
 3705 int
 3706 ng_uncallout(struct callout *c, node_p node)
 3707 {
 3708         item_p item;
 3709         int rval;
 3710 
 3711         KASSERT(c != NULL, ("ng_uncallout: NULL callout"));
 3712         KASSERT(node != NULL, ("ng_uncallout: NULL node"));
 3713 
 3714         rval = callout_stop(c);
 3715         item = c->c_arg;
 3716         /* Do an extra check */
 3717         if ((rval > 0) && (c->c_func == &ng_callout_trampoline) &&
 3718             (NGI_NODE(item) == node)) {
 3719                 /*
 3720                  * We successfully removed it from the queue before it ran
 3721                  * So now we need to unreference everything that was
 3722                  * given extra references. (NG_FREE_ITEM does this).
 3723                  */
 3724                 NG_FREE_ITEM(item);
 3725         }
 3726         c->c_arg = NULL;
 3727 
 3728         return (rval);
 3729 }
 3730 
 3731 /*
 3732  * Set the address, if none given, give the node here.
 3733  */
 3734 void
 3735 ng_replace_retaddr(node_p here, item_p item, ng_ID_t retaddr)
 3736 {
 3737         if (retaddr) {
 3738                 NGI_RETADDR(item) = retaddr;
 3739         } else {
 3740                 /*
 3741                  * The old return address should be ok.
 3742                  * If there isn't one, use the address here.
 3743                  */
 3744                 NGI_RETADDR(item) = ng_node2ID(here);
 3745         }
 3746 }
 3747 
 3748 #define TESTING
 3749 #ifdef TESTING
 3750 /* just test all the macros */
 3751 void
 3752 ng_macro_test(item_p item);
 3753 void
 3754 ng_macro_test(item_p item)
 3755 {
 3756         node_p node = NULL;
 3757         hook_p hook = NULL;
 3758         struct mbuf *m;
 3759         struct ng_mesg *msg;
 3760         ng_ID_t retaddr;
 3761         int     error;
 3762 
 3763         NGI_GET_M(item, m);
 3764         NGI_GET_MSG(item, msg);
 3765         retaddr = NGI_RETADDR(item);
 3766         NG_SEND_DATA(error, hook, m, NULL);
 3767         NG_SEND_DATA_ONLY(error, hook, m);
 3768         NG_FWD_NEW_DATA(error, item, hook, m);
 3769         NG_FWD_ITEM_HOOK(error, item, hook);
 3770         NG_SEND_MSG_HOOK(error, node, msg, hook, retaddr);
 3771         NG_SEND_MSG_ID(error, node, msg, retaddr, retaddr);
 3772         NG_SEND_MSG_PATH(error, node, msg, ".:", retaddr);
 3773         NG_FWD_MSG_HOOK(error, node, item, hook, retaddr);
 3774 }
 3775 #endif /* TESTING */
 3776 

Cache object: 409aaa6f13697c31a164f6aeba5b68b4


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.