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/netgraph.h

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  * netgraph.h
    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  * Author: Julian Elischer <julian@freebsd.org>
   39  *
   40  * $FreeBSD$
   41  * $Whistle: netgraph.h,v 1.29 1999/11/01 07:56:13 julian Exp $
   42  */
   43 
   44 #ifndef _NETGRAPH_NETGRAPH_H_
   45 #define _NETGRAPH_NETGRAPH_H_
   46 
   47 #ifndef _KERNEL
   48 #error "This file should not be included in user level programs"
   49 #endif
   50 
   51 #include <sys/queue.h>
   52 #include <sys/lock.h>
   53 #include <sys/malloc.h>
   54 #include <sys/module.h>
   55 #include <sys/mutex.h>
   56 #include <sys/refcount.h>
   57 
   58 #ifdef HAVE_KERNEL_OPTION_HEADERS
   59 #include "opt_netgraph.h"
   60 #include "opt_kdb.h"
   61 #endif
   62 
   63 /* debugging options */
   64 #define NG_SEPARATE_MALLOC      /* make modules use their own malloc types */
   65 
   66 /*
   67  * This defines the in-kernel binary interface version.
   68  * It is possible to change this but leave the external message
   69  * API the same. Each type also has it's own cookies for versioning as well.
   70  * Change it for NETGRAPH_DEBUG version so we cannot mix debug and non debug
   71  * modules.
   72  */
   73 #define _NG_ABI_VERSION 12
   74 #ifdef  NETGRAPH_DEBUG /*----------------------------------------------*/
   75 #define NG_ABI_VERSION  (_NG_ABI_VERSION + 0x10000)
   76 #else   /* NETGRAPH_DEBUG */ /*----------------------------------------------*/
   77 #define NG_ABI_VERSION  _NG_ABI_VERSION
   78 #endif  /* NETGRAPH_DEBUG */ /*----------------------------------------------*/
   79 
   80 
   81 /*
   82  * Forward references for the basic structures so we can
   83  * define the typedefs and use them in the structures themselves.
   84  */
   85 struct ng_hook ;
   86 struct ng_node ;
   87 struct ng_item ;
   88 typedef struct ng_item *item_p;
   89 typedef struct ng_node *node_p;
   90 typedef struct ng_hook *hook_p;
   91 typedef struct ng_item const *item_cp;
   92 typedef struct ng_hook const *hook_cp;
   93 #ifdef  NETGRAPH_DEBUG
   94 typedef struct ng_node       *node_cp; /* annotated during debug */
   95 #else   /* NETGRAPH_DEBUG */
   96 typedef struct ng_node const *node_cp;
   97 #endif  /* NETGRAPH_DEBUG */
   98 
   99 /* node method definitions */
  100 typedef int     ng_constructor_t(node_p node);
  101 typedef int     ng_close_t(node_p node);
  102 typedef int     ng_shutdown_t(node_p node);
  103 typedef int     ng_newhook_t(node_p node, hook_p hook, const char *name);
  104 typedef hook_p  ng_findhook_t(node_p node, const char *name);
  105 typedef int     ng_connect_t(hook_p hook);
  106 typedef int     ng_rcvmsg_t(node_p node, item_p item, hook_p lasthook);
  107 typedef int     ng_rcvdata_t(hook_p hook, item_p item);
  108 typedef int     ng_disconnect_t(hook_p hook);
  109 typedef int     ng_rcvitem (node_p node, hook_p hook, item_p item);
  110 
  111 /***********************************************************************
  112  ***************** Hook Structure and Methods **************************
  113  ***********************************************************************
  114  *
  115  * Structure of a hook
  116  */
  117 struct ng_hook {
  118         char    hk_name[NG_HOOKSIZ];    /* what this node knows this link as */
  119         void   *hk_private;             /* node dependent ID for this hook */
  120         int     hk_flags;               /* info about this hook/link */
  121         int     hk_type;                /* tbd: hook data link type */
  122         struct  ng_hook *hk_peer;       /* the other end of this link */
  123         struct  ng_node *hk_node;       /* The node this hook is attached to */
  124         LIST_ENTRY(ng_hook) hk_hooks;   /* linked list of all hooks on node */
  125         ng_rcvmsg_t     *hk_rcvmsg;     /* control messages come here */
  126         ng_rcvdata_t    *hk_rcvdata;    /* data comes here */
  127         int     hk_refs;                /* dont actually free this till 0 */
  128 #ifdef  NETGRAPH_DEBUG /*----------------------------------------------*/
  129 #define HK_MAGIC 0x78573011
  130         int     hk_magic;
  131         char    *lastfile;
  132         int     lastline;
  133         SLIST_ENTRY(ng_hook)      hk_all;               /* all existing items */
  134 #endif  /* NETGRAPH_DEBUG */ /*----------------------------------------------*/
  135 };
  136 /* Flags for a hook */
  137 #define HK_INVALID              0x0001  /* don't trust it! */
  138 #define HK_QUEUE                0x0002  /* queue for later delivery */
  139 #define HK_FORCE_WRITER         0x0004  /* Incoming data queued as a writer */
  140 #define HK_DEAD                 0x0008  /* This is the dead hook.. don't free */
  141 #define HK_HI_STACK             0x0010  /* Hook has hi stack usage */
  142 #define HK_TO_INBOUND           0x0020  /* Hook on ntw. stack inbound path. */
  143 
  144 /*
  145  * Public Methods for hook
  146  * If you can't do it with these you probably shouldn;t be doing it.
  147  */
  148 void ng_unref_hook(hook_p hook); /* don't move this */
  149 #define _NG_HOOK_REF(hook)      refcount_acquire(&(hook)->hk_refs)
  150 #define _NG_HOOK_NAME(hook)     ((hook)->hk_name)
  151 #define _NG_HOOK_UNREF(hook)    ng_unref_hook(hook)
  152 #define _NG_HOOK_SET_PRIVATE(hook, val) do {(hook)->hk_private = val;} while (0)
  153 #define _NG_HOOK_SET_RCVMSG(hook, val)  do {(hook)->hk_rcvmsg = val;} while (0)
  154 #define _NG_HOOK_SET_RCVDATA(hook, val) do {(hook)->hk_rcvdata = val;} while (0)
  155 #define _NG_HOOK_PRIVATE(hook)  ((hook)->hk_private)
  156 #define _NG_HOOK_NOT_VALID(hook)        ((hook)->hk_flags & HK_INVALID)
  157 #define _NG_HOOK_IS_VALID(hook) (!((hook)->hk_flags & HK_INVALID))
  158 #define _NG_HOOK_NODE(hook)     ((hook)->hk_node) /* only rvalue! */
  159 #define _NG_HOOK_PEER(hook)     ((hook)->hk_peer) /* only rvalue! */
  160 #define _NG_HOOK_FORCE_WRITER(hook)                             \
  161                 do { hook->hk_flags |= HK_FORCE_WRITER; } while (0)
  162 #define _NG_HOOK_FORCE_QUEUE(hook) do { hook->hk_flags |= HK_QUEUE; } while (0)
  163 #define _NG_HOOK_SET_TO_INBOUND(hook)                           \
  164                 do { hook->hk_flags |= HK_TO_INBOUND; } while (0)
  165 #define _NG_HOOK_HI_STACK(hook) do { hook->hk_flags |= HK_HI_STACK; } while (0)
  166 
  167 /* Some shortcuts */
  168 #define NG_PEER_NODE(hook)      NG_HOOK_NODE(NG_HOOK_PEER(hook))
  169 #define NG_PEER_HOOK_NAME(hook) NG_HOOK_NAME(NG_HOOK_PEER(hook))
  170 #define NG_PEER_NODE_NAME(hook) NG_NODE_NAME(NG_PEER_NODE(hook))
  171 
  172 #ifdef  NETGRAPH_DEBUG /*----------------------------------------------*/
  173 #define _NN_ __FILE__,__LINE__
  174 void    dumphook (hook_p hook, char *file, int line);
  175 static __inline void    _chkhook(hook_p hook, char *file, int line);
  176 static __inline void    _ng_hook_ref(hook_p hook, char * file, int line);
  177 static __inline char *  _ng_hook_name(hook_p hook, char * file, int line);
  178 static __inline void    _ng_hook_unref(hook_p hook, char * file, int line);
  179 static __inline void    _ng_hook_set_private(hook_p hook,
  180                                 void * val, char * file, int line);
  181 static __inline void    _ng_hook_set_rcvmsg(hook_p hook,
  182                                 ng_rcvmsg_t *val, char * file, int line);
  183 static __inline void    _ng_hook_set_rcvdata(hook_p hook,
  184                                 ng_rcvdata_t *val, char * file, int line);
  185 static __inline void *  _ng_hook_private(hook_p hook, char * file, int line);
  186 static __inline int     _ng_hook_not_valid(hook_p hook, char * file, int line);
  187 static __inline int     _ng_hook_is_valid(hook_p hook, char * file, int line);
  188 static __inline node_p  _ng_hook_node(hook_p hook, char * file, int line);
  189 static __inline hook_p  _ng_hook_peer(hook_p hook, char * file, int line);
  190 static __inline void    _ng_hook_force_writer(hook_p hook, char * file,
  191                                 int line);
  192 static __inline void    _ng_hook_force_queue(hook_p hook, char * file,
  193                                 int line);
  194 static __inline void    _ng_hook_set_to_inbound(hook_p hook, char * file,
  195                                 int line);
  196 
  197 static __inline void
  198 _chkhook(hook_p hook, char *file, int line)
  199 {
  200         if (hook->hk_magic != HK_MAGIC) {
  201                 printf("Accessing freed ");
  202                 dumphook(hook, file, line);
  203         }
  204         hook->lastline = line;
  205         hook->lastfile = file;
  206 }
  207 
  208 static __inline void
  209 _ng_hook_ref(hook_p hook, char * file, int line)
  210 {
  211         _chkhook(hook, file, line);
  212         _NG_HOOK_REF(hook);
  213 }
  214 
  215 static __inline char *
  216 _ng_hook_name(hook_p hook, char * file, int line)
  217 {
  218         _chkhook(hook, file, line);
  219         return (_NG_HOOK_NAME(hook));
  220 }
  221 
  222 static __inline void
  223 _ng_hook_unref(hook_p hook, char * file, int line)
  224 {
  225         _chkhook(hook, file, line);
  226         _NG_HOOK_UNREF(hook);
  227 }
  228 
  229 static __inline void
  230 _ng_hook_set_private(hook_p hook, void *val, char * file, int line)
  231 {
  232         _chkhook(hook, file, line);
  233         _NG_HOOK_SET_PRIVATE(hook, val);
  234 }
  235 
  236 static __inline void
  237 _ng_hook_set_rcvmsg(hook_p hook, ng_rcvmsg_t *val, char * file, int line)
  238 {
  239         _chkhook(hook, file, line);
  240         _NG_HOOK_SET_RCVMSG(hook, val);
  241 }
  242 
  243 static __inline void
  244 _ng_hook_set_rcvdata(hook_p hook, ng_rcvdata_t *val, char * file, int line)
  245 {
  246         _chkhook(hook, file, line);
  247         _NG_HOOK_SET_RCVDATA(hook, val);
  248 }
  249 
  250 static __inline void *
  251 _ng_hook_private(hook_p hook, char * file, int line)
  252 {
  253         _chkhook(hook, file, line);
  254         return (_NG_HOOK_PRIVATE(hook));
  255 }
  256 
  257 static __inline int
  258 _ng_hook_not_valid(hook_p hook, char * file, int line)
  259 {
  260         _chkhook(hook, file, line);
  261         return (_NG_HOOK_NOT_VALID(hook));
  262 }
  263 
  264 static __inline int
  265 _ng_hook_is_valid(hook_p hook, char * file, int line)
  266 {
  267         _chkhook(hook, file, line);
  268         return (_NG_HOOK_IS_VALID(hook));
  269 }
  270 
  271 static __inline node_p
  272 _ng_hook_node(hook_p hook, char * file, int line)
  273 {
  274         _chkhook(hook, file, line);
  275         return (_NG_HOOK_NODE(hook));
  276 }
  277 
  278 static __inline hook_p
  279 _ng_hook_peer(hook_p hook, char * file, int line)
  280 {
  281         _chkhook(hook, file, line);
  282         return (_NG_HOOK_PEER(hook));
  283 }
  284 
  285 static __inline void
  286 _ng_hook_force_writer(hook_p hook, char * file, int line)
  287 {
  288         _chkhook(hook, file, line);
  289         _NG_HOOK_FORCE_WRITER(hook);
  290 }
  291 
  292 static __inline void
  293 _ng_hook_force_queue(hook_p hook, char * file, int line)
  294 {
  295         _chkhook(hook, file, line);
  296         _NG_HOOK_FORCE_QUEUE(hook);
  297 }
  298 
  299 static __inline void
  300 _ng_hook_set_to_inbound(hook_p hook, char * file, int line)
  301 {
  302         _chkhook(hook, file, line);
  303         _NG_HOOK_SET_TO_INBOUND(hook);
  304 }
  305 
  306 static __inline void
  307 _ng_hook_hi_stack(hook_p hook, char * file, int line)
  308 {
  309         _chkhook(hook, file, line);
  310         _NG_HOOK_HI_STACK(hook);
  311 }
  312 
  313 
  314 #define NG_HOOK_REF(hook)               _ng_hook_ref(hook, _NN_)
  315 #define NG_HOOK_NAME(hook)              _ng_hook_name(hook, _NN_)
  316 #define NG_HOOK_UNREF(hook)             _ng_hook_unref(hook, _NN_)
  317 #define NG_HOOK_SET_PRIVATE(hook, val)  _ng_hook_set_private(hook, val, _NN_)
  318 #define NG_HOOK_SET_RCVMSG(hook, val)   _ng_hook_set_rcvmsg(hook, val, _NN_)
  319 #define NG_HOOK_SET_RCVDATA(hook, val)  _ng_hook_set_rcvdata(hook, val, _NN_)
  320 #define NG_HOOK_PRIVATE(hook)           _ng_hook_private(hook, _NN_)
  321 #define NG_HOOK_NOT_VALID(hook)         _ng_hook_not_valid(hook, _NN_)
  322 #define NG_HOOK_IS_VALID(hook)          _ng_hook_is_valid(hook, _NN_)
  323 #define NG_HOOK_NODE(hook)              _ng_hook_node(hook, _NN_)
  324 #define NG_HOOK_PEER(hook)              _ng_hook_peer(hook, _NN_)
  325 #define NG_HOOK_FORCE_WRITER(hook)      _ng_hook_force_writer(hook, _NN_)
  326 #define NG_HOOK_FORCE_QUEUE(hook)       _ng_hook_force_queue(hook, _NN_)
  327 #define NG_HOOK_SET_TO_INBOUND(hook)    _ng_hook_set_to_inbound(hook, _NN_)
  328 #define NG_HOOK_HI_STACK(hook)          _ng_hook_hi_stack(hook, _NN_)
  329 
  330 #else   /* NETGRAPH_DEBUG */ /*----------------------------------------------*/
  331 
  332 #define NG_HOOK_REF(hook)               _NG_HOOK_REF(hook)
  333 #define NG_HOOK_NAME(hook)              _NG_HOOK_NAME(hook)
  334 #define NG_HOOK_UNREF(hook)             _NG_HOOK_UNREF(hook)
  335 #define NG_HOOK_SET_PRIVATE(hook, val)  _NG_HOOK_SET_PRIVATE(hook, val)
  336 #define NG_HOOK_SET_RCVMSG(hook, val)   _NG_HOOK_SET_RCVMSG(hook, val)
  337 #define NG_HOOK_SET_RCVDATA(hook, val)  _NG_HOOK_SET_RCVDATA(hook, val)
  338 #define NG_HOOK_PRIVATE(hook)           _NG_HOOK_PRIVATE(hook)
  339 #define NG_HOOK_NOT_VALID(hook)         _NG_HOOK_NOT_VALID(hook)
  340 #define NG_HOOK_IS_VALID(hook)          _NG_HOOK_IS_VALID(hook)
  341 #define NG_HOOK_NODE(hook)              _NG_HOOK_NODE(hook)
  342 #define NG_HOOK_PEER(hook)              _NG_HOOK_PEER(hook)
  343 #define NG_HOOK_FORCE_WRITER(hook)      _NG_HOOK_FORCE_WRITER(hook)
  344 #define NG_HOOK_FORCE_QUEUE(hook)       _NG_HOOK_FORCE_QUEUE(hook)
  345 #define NG_HOOK_SET_TO_INBOUND(hook)    _NG_HOOK_SET_TO_INBOUND(hook)
  346 #define NG_HOOK_HI_STACK(hook)          _NG_HOOK_HI_STACK(hook)
  347 
  348 #endif  /* NETGRAPH_DEBUG */ /*----------------------------------------------*/
  349 
  350 /***********************************************************************
  351  ***************** Node Structure and Methods **************************
  352  ***********************************************************************
  353  * Structure of a node
  354  * including the eembedded queue structure.
  355  *
  356  * The structure for queueing Netgraph request items
  357  * embedded in the node structure
  358  */
  359 struct ng_queue {
  360         u_int           q_flags;        /* Current r/w/q lock flags */
  361         u_int           q_flags2;       /* Other queue flags */
  362         struct mtx      q_mtx;
  363         STAILQ_ENTRY(ng_node)   q_work; /* nodes with work to do */
  364         STAILQ_HEAD(, ng_item)  queue;  /* actually items queue */
  365 };
  366 
  367 struct ng_node {
  368         char    nd_name[NG_NODESIZ];    /* optional globally unique name */
  369         struct  ng_type *nd_type;       /* the installed 'type' */
  370         int     nd_flags;               /* see below for bit definitions */
  371         int     nd_numhooks;            /* number of hooks */
  372         void   *nd_private;             /* node type dependent node ID */
  373         ng_ID_t nd_ID;                  /* Unique per node */
  374         LIST_HEAD(hooks, ng_hook) nd_hooks;     /* linked list of node hooks */
  375         LIST_ENTRY(ng_node)       nd_nodes;     /* name hash collision list */
  376         LIST_ENTRY(ng_node)       nd_idnodes;   /* ID hash collision list */
  377         struct  ng_queue          nd_input_queue; /* input queue for locking */
  378         int     nd_refs;                /* # of references to this node */
  379         struct  vnet             *nd_vnet;      /* network stack instance */
  380 #ifdef  NETGRAPH_DEBUG /*----------------------------------------------*/
  381 #define ND_MAGIC 0x59264837
  382         int     nd_magic;
  383         char    *lastfile;
  384         int     lastline;
  385         SLIST_ENTRY(ng_node)      nd_all;       /* all existing nodes */
  386 #endif  /* NETGRAPH_DEBUG */ /*----------------------------------------------*/
  387 };
  388 
  389 /* Flags for a node */
  390 #define NGF_INVALID     0x00000001      /* free when refs go to 0 */
  391 #define NG_INVALID      NGF_INVALID     /* compat for old code */
  392 #define NGF_FORCE_WRITER        0x00000004      /* Never multithread this node */
  393 #define NG_FORCE_WRITER NGF_FORCE_WRITER /* compat for old code */
  394 #define NGF_CLOSING     0x00000008      /* ng_rmnode() at work */
  395 #define NG_CLOSING      NGF_CLOSING     /* compat for old code */
  396 #define NGF_REALLY_DIE  0x00000010      /* "persistent" node is unloading */
  397 #define NG_REALLY_DIE   NGF_REALLY_DIE  /* compat for old code */
  398 #define NGF_HI_STACK    0x00000020      /* node has hi stack usage */
  399 #define NGF_TYPE1       0x10000000      /* reserved for type specific storage */
  400 #define NGF_TYPE2       0x20000000      /* reserved for type specific storage */
  401 #define NGF_TYPE3       0x40000000      /* reserved for type specific storage */
  402 #define NGF_TYPE4       0x80000000      /* reserved for type specific storage */
  403 
  404 /*
  405  * Public methods for nodes.
  406  * If you can't do it with these you probably shouldn't be doing it.
  407  */
  408 void    ng_unref_node(node_p node); /* don't move this */
  409 #define _NG_NODE_NAME(node)     ((node)->nd_name + 0)
  410 #define _NG_NODE_HAS_NAME(node) ((node)->nd_name[0] + 0)
  411 #define _NG_NODE_ID(node)       ((node)->nd_ID + 0)
  412 #define _NG_NODE_REF(node)      refcount_acquire(&(node)->nd_refs)
  413 #define _NG_NODE_UNREF(node)    ng_unref_node(node)
  414 #define _NG_NODE_SET_PRIVATE(node, val) do {(node)->nd_private = val;} while (0)
  415 #define _NG_NODE_PRIVATE(node)  ((node)->nd_private)
  416 #define _NG_NODE_IS_VALID(node) (!((node)->nd_flags & NGF_INVALID))
  417 #define _NG_NODE_NOT_VALID(node)        ((node)->nd_flags & NGF_INVALID)
  418 #define _NG_NODE_NUMHOOKS(node) ((node)->nd_numhooks + 0) /* rvalue */
  419 #define _NG_NODE_FORCE_WRITER(node)                                     \
  420         do{ node->nd_flags |= NGF_FORCE_WRITER; }while (0)
  421 #define _NG_NODE_HI_STACK(node)                                         \
  422         do{ node->nd_flags |= NGF_HI_STACK; }while (0)
  423 #define _NG_NODE_REALLY_DIE(node)                                       \
  424         do{ node->nd_flags |= (NGF_REALLY_DIE|NGF_INVALID); }while (0)
  425 #define _NG_NODE_REVIVE(node) \
  426         do { node->nd_flags &= ~NGF_INVALID; } while (0)
  427 /*
  428  * The hook iterator.
  429  * This macro will call a function of type ng_fn_eachhook for each
  430  * hook attached to the node. If the function returns 0, then the
  431  * iterator will stop and return a pointer to the hook that returned 0.
  432  */
  433 typedef int     ng_fn_eachhook(hook_p hook, void* arg);
  434 #define _NG_NODE_FOREACH_HOOK(node, fn, arg, rethook)                   \
  435         do {                                                            \
  436                 hook_p _hook;                                           \
  437                 (rethook) = NULL;                                       \
  438                 LIST_FOREACH(_hook, &((node)->nd_hooks), hk_hooks) {    \
  439                         if ((fn)(_hook, arg) == 0) {                    \
  440                                 (rethook) = _hook;                      \
  441                                 break;                                  \
  442                         }                                               \
  443                 }                                                       \
  444         } while (0)
  445 
  446 #ifdef  NETGRAPH_DEBUG /*----------------------------------------------*/
  447 void    dumpnode(node_p node, char *file, int line);
  448 static __inline void _chknode(node_p node, char *file, int line);
  449 static __inline char * _ng_node_name(node_p node, char *file, int line);
  450 static __inline int _ng_node_has_name(node_p node, char *file, int line);
  451 static __inline ng_ID_t _ng_node_id(node_p node, char *file, int line);
  452 static __inline void _ng_node_ref(node_p node, char *file, int line);
  453 static __inline void _ng_node_unref(node_p node, char *file, int line);
  454 static __inline void _ng_node_set_private(node_p node, void * val,
  455                                                         char *file, int line);
  456 static __inline void * _ng_node_private(node_p node, char *file, int line);
  457 static __inline int _ng_node_is_valid(node_p node, char *file, int line);
  458 static __inline int _ng_node_not_valid(node_p node, char *file, int line);
  459 static __inline int _ng_node_numhooks(node_p node, char *file, int line);
  460 static __inline void _ng_node_force_writer(node_p node, char *file, int line);
  461 static __inline hook_p _ng_node_foreach_hook(node_p node,
  462                         ng_fn_eachhook *fn, void *arg, char *file, int line);
  463 static __inline void _ng_node_revive(node_p node, char *file, int line);
  464 
  465 static __inline void
  466 _chknode(node_p node, char *file, int line)
  467 {
  468         if (node->nd_magic != ND_MAGIC) {
  469                 printf("Accessing freed ");
  470                 dumpnode(node, file, line);
  471         }
  472         node->lastline = line;
  473         node->lastfile = file;
  474 }
  475 
  476 static __inline char *
  477 _ng_node_name(node_p node, char *file, int line)
  478 {
  479         _chknode(node, file, line);
  480         return(_NG_NODE_NAME(node));
  481 }
  482 
  483 static __inline int
  484 _ng_node_has_name(node_p node, char *file, int line)
  485 {
  486         _chknode(node, file, line);
  487         return(_NG_NODE_HAS_NAME(node));
  488 }
  489 
  490 static __inline ng_ID_t
  491 _ng_node_id(node_p node, char *file, int line)
  492 {
  493         _chknode(node, file, line);
  494         return(_NG_NODE_ID(node));
  495 }
  496 
  497 static __inline void
  498 _ng_node_ref(node_p node, char *file, int line)
  499 {
  500         _chknode(node, file, line);
  501         _NG_NODE_REF(node);
  502 }
  503 
  504 static __inline void
  505 _ng_node_unref(node_p node, char *file, int line)
  506 {
  507         _chknode(node, file, line);
  508         _NG_NODE_UNREF(node);
  509 }
  510 
  511 static __inline void
  512 _ng_node_set_private(node_p node, void * val, char *file, int line)
  513 {
  514         _chknode(node, file, line);
  515         _NG_NODE_SET_PRIVATE(node, val);
  516 }
  517 
  518 static __inline void *
  519 _ng_node_private(node_p node, char *file, int line)
  520 {
  521         _chknode(node, file, line);
  522         return (_NG_NODE_PRIVATE(node));
  523 }
  524 
  525 static __inline int
  526 _ng_node_is_valid(node_p node, char *file, int line)
  527 {
  528         _chknode(node, file, line);
  529         return(_NG_NODE_IS_VALID(node));
  530 }
  531 
  532 static __inline int
  533 _ng_node_not_valid(node_p node, char *file, int line)
  534 {
  535         _chknode(node, file, line);
  536         return(_NG_NODE_NOT_VALID(node));
  537 }
  538 
  539 static __inline int
  540 _ng_node_numhooks(node_p node, char *file, int line)
  541 {
  542         _chknode(node, file, line);
  543         return(_NG_NODE_NUMHOOKS(node));
  544 }
  545 
  546 static __inline void
  547 _ng_node_force_writer(node_p node, char *file, int line)
  548 {
  549         _chknode(node, file, line);
  550         _NG_NODE_FORCE_WRITER(node);
  551 }
  552 
  553 static __inline void
  554 _ng_node_hi_stack(node_p node, char *file, int line)
  555 {
  556         _chknode(node, file, line);
  557         _NG_NODE_HI_STACK(node);
  558 }
  559 
  560 static __inline void
  561 _ng_node_really_die(node_p node, char *file, int line)
  562 {
  563         _chknode(node, file, line);
  564         _NG_NODE_REALLY_DIE(node);
  565 }
  566 
  567 static __inline void
  568 _ng_node_revive(node_p node, char *file, int line)
  569 {
  570         _chknode(node, file, line);
  571         _NG_NODE_REVIVE(node);
  572 }
  573 
  574 static __inline hook_p
  575 _ng_node_foreach_hook(node_p node, ng_fn_eachhook *fn, void *arg,
  576                                                 char *file, int line)
  577 {
  578         hook_p hook;
  579         _chknode(node, file, line);
  580         _NG_NODE_FOREACH_HOOK(node, fn, arg, hook);
  581         return (hook);
  582 }
  583 
  584 #define NG_NODE_NAME(node)              _ng_node_name(node, _NN_)       
  585 #define NG_NODE_HAS_NAME(node)          _ng_node_has_name(node, _NN_)   
  586 #define NG_NODE_ID(node)                _ng_node_id(node, _NN_)
  587 #define NG_NODE_REF(node)               _ng_node_ref(node, _NN_)
  588 #define NG_NODE_UNREF(node)             _ng_node_unref(node, _NN_)
  589 #define NG_NODE_SET_PRIVATE(node, val)  _ng_node_set_private(node, val, _NN_)
  590 #define NG_NODE_PRIVATE(node)           _ng_node_private(node, _NN_)
  591 #define NG_NODE_IS_VALID(node)          _ng_node_is_valid(node, _NN_)
  592 #define NG_NODE_NOT_VALID(node)         _ng_node_not_valid(node, _NN_)
  593 #define NG_NODE_FORCE_WRITER(node)      _ng_node_force_writer(node, _NN_)
  594 #define NG_NODE_HI_STACK(node)          _ng_node_hi_stack(node, _NN_)
  595 #define NG_NODE_REALLY_DIE(node)        _ng_node_really_die(node, _NN_)
  596 #define NG_NODE_NUMHOOKS(node)          _ng_node_numhooks(node, _NN_)
  597 #define NG_NODE_REVIVE(node)            _ng_node_revive(node, _NN_)
  598 #define NG_NODE_FOREACH_HOOK(node, fn, arg, rethook)                          \
  599         do {                                                                  \
  600                 rethook = _ng_node_foreach_hook(node, fn, (void *)arg, _NN_); \
  601         } while (0)
  602 
  603 #else   /* NETGRAPH_DEBUG */ /*----------------------------------------------*/
  604 
  605 #define NG_NODE_NAME(node)              _NG_NODE_NAME(node)     
  606 #define NG_NODE_HAS_NAME(node)          _NG_NODE_HAS_NAME(node) 
  607 #define NG_NODE_ID(node)                _NG_NODE_ID(node)       
  608 #define NG_NODE_REF(node)               _NG_NODE_REF(node)      
  609 #define NG_NODE_UNREF(node)             _NG_NODE_UNREF(node)    
  610 #define NG_NODE_SET_PRIVATE(node, val)  _NG_NODE_SET_PRIVATE(node, val) 
  611 #define NG_NODE_PRIVATE(node)           _NG_NODE_PRIVATE(node)  
  612 #define NG_NODE_IS_VALID(node)          _NG_NODE_IS_VALID(node) 
  613 #define NG_NODE_NOT_VALID(node)         _NG_NODE_NOT_VALID(node)        
  614 #define NG_NODE_FORCE_WRITER(node)      _NG_NODE_FORCE_WRITER(node)
  615 #define NG_NODE_HI_STACK(node)          _NG_NODE_HI_STACK(node)
  616 #define NG_NODE_REALLY_DIE(node)        _NG_NODE_REALLY_DIE(node)
  617 #define NG_NODE_NUMHOOKS(node)          _NG_NODE_NUMHOOKS(node) 
  618 #define NG_NODE_REVIVE(node)            _NG_NODE_REVIVE(node)
  619 #define NG_NODE_FOREACH_HOOK(node, fn, arg, rethook)                    \
  620                 _NG_NODE_FOREACH_HOOK(node, fn, arg, rethook)
  621 #endif  /* NETGRAPH_DEBUG */ /*----------------------------------------------*/
  622 
  623 /***********************************************************************
  624  ************* Node Queue and Item Structures and Methods **************
  625  ***********************************************************************
  626  *
  627  */
  628 typedef void    ng_item_fn(node_p node, hook_p hook, void *arg1, int arg2);
  629 typedef int     ng_item_fn2(node_p node, struct ng_item *item, hook_p hook);
  630 typedef void    ng_apply_t(void *context, int error);
  631 struct ng_apply_info {
  632         ng_apply_t      *apply;
  633         void            *context;
  634         int             refs;
  635         int             error;
  636 };
  637 struct ng_item {
  638         u_long  el_flags;
  639         STAILQ_ENTRY(ng_item)   el_next;
  640         node_p  el_dest; /* The node it will be applied against (or NULL) */
  641         hook_p  el_hook; /* Entering hook. Optional in Control messages */
  642         union {
  643                 struct mbuf     *da_m;
  644                 struct {
  645                         struct ng_mesg  *msg_msg;
  646                         ng_ID_t         msg_retaddr;
  647                 } msg;
  648                 struct {
  649                         union {
  650                                 ng_item_fn      *fn_fn;
  651                                 ng_item_fn2     *fn_fn2;
  652                         } fn_fn;
  653                         void            *fn_arg1;
  654                         int             fn_arg2;
  655                 } fn;
  656         } body;
  657         /*
  658          * Optional callback called when item is being applied,
  659          * and its context.
  660          */
  661         struct ng_apply_info    *apply;
  662         u_int   depth;
  663 #ifdef  NETGRAPH_DEBUG /*----------------------------------------------*/
  664         char *lastfile;
  665         int  lastline;
  666         TAILQ_ENTRY(ng_item)      all;          /* all existing items */
  667 #endif  /* NETGRAPH_DEBUG */ /*----------------------------------------------*/
  668 };
  669 
  670 #define NGQF_TYPE       0x03            /* MASK of content definition */
  671 #define NGQF_MESG       0x00            /* the queue element is a message */
  672 #define NGQF_DATA       0x01            /* the queue element is data */
  673 #define NGQF_FN         0x02            /* the queue element is a function */
  674 #define NGQF_FN2        0x03            /* the queue element is a new function */
  675 
  676 #define NGQF_RW         0x04            /* MASK for wanted queue mode */
  677 #define NGQF_READER     0x04            /* wants to be a reader */
  678 #define NGQF_WRITER     0x00            /* wants to be a writer */
  679 
  680 #define NGQF_QMODE      0x08            /* MASK for how it was queued */
  681 #define NGQF_QREADER    0x08            /* was queued as a reader */
  682 #define NGQF_QWRITER    0x00            /* was queued as a writer */
  683 
  684 /*
  685  * Get the mbuf (etc) out of an item.
  686  * Sets the value in the item to NULL in case we need to call NG_FREE_ITEM()
  687  * with it, (to avoid freeing the things twice).
  688  * If you don't want to zero out the item then realise that the
  689  * item still owns it.
  690  * Retaddr is different. There are no references on that. It's just a number.
  691  * The debug versions must be either all used everywhere or not at all.
  692  */
  693 
  694 #define _NGI_M(i) ((i)->body.da_m)
  695 #define _NGI_MSG(i) ((i)->body.msg.msg_msg)
  696 #define _NGI_RETADDR(i) ((i)->body.msg.msg_retaddr)
  697 #define _NGI_FN(i) ((i)->body.fn.fn_fn.fn_fn)
  698 #define _NGI_FN2(i) ((i)->body.fn.fn_fn.fn_fn2)
  699 #define _NGI_ARG1(i) ((i)->body.fn.fn_arg1)
  700 #define _NGI_ARG2(i) ((i)->body.fn.fn_arg2)
  701 #define _NGI_NODE(i) ((i)->el_dest)
  702 #define _NGI_HOOK(i) ((i)->el_hook)
  703 #define _NGI_SET_HOOK(i,h) do { _NGI_HOOK(i) = h; h = NULL;} while (0)
  704 #define _NGI_CLR_HOOK(i)   do {                                         \
  705                 hook_p _hook = _NGI_HOOK(i);                            \
  706                 if (_hook) {                                            \
  707                         _NG_HOOK_UNREF(_hook);                          \
  708                         _NGI_HOOK(i) = NULL;                            \
  709                 }                                                       \
  710         } while (0)
  711 #define _NGI_SET_NODE(i,n) do { _NGI_NODE(i) = n; n = NULL;} while (0)
  712 #define _NGI_CLR_NODE(i)   do {                                         \
  713                 node_p _node = _NGI_NODE(i);                            \
  714                 if (_node) {                                            \
  715                         _NG_NODE_UNREF(_node);                          \
  716                         _NGI_NODE(i) = NULL;                            \
  717                 }                                                       \
  718         } while (0)
  719 
  720 #ifdef NETGRAPH_DEBUG /*----------------------------------------------*/
  721 void                            dumpitem(item_p item, char *file, int line);
  722 static __inline void            _ngi_check(item_p item, char *file, int line) ;
  723 static __inline struct mbuf **  _ngi_m(item_p item, char *file, int line) ;
  724 static __inline ng_ID_t *       _ngi_retaddr(item_p item, char *file, int line);
  725 static __inline struct ng_mesg ** _ngi_msg(item_p item, char *file, int line) ;
  726 static __inline ng_item_fn **   _ngi_fn(item_p item, char *file, int line) ;
  727 static __inline ng_item_fn2 **  _ngi_fn2(item_p item, char *file, int line) ;
  728 static __inline void **         _ngi_arg1(item_p item, char *file, int line) ;
  729 static __inline int *           _ngi_arg2(item_p item, char *file, int line) ;
  730 static __inline node_p          _ngi_node(item_p item, char *file, int line);
  731 static __inline hook_p          _ngi_hook(item_p item, char *file, int line);
  732 
  733 static __inline void
  734 _ngi_check(item_p item, char *file, int line)
  735 {
  736         (item)->lastline = line;
  737         (item)->lastfile = file;
  738 }
  739 
  740 static __inline struct mbuf **
  741 _ngi_m(item_p item, char *file, int line)
  742 {
  743         _ngi_check(item, file, line);
  744         return (&_NGI_M(item));
  745 }
  746 
  747 static __inline struct ng_mesg **
  748 _ngi_msg(item_p item, char *file, int line)
  749 {
  750         _ngi_check(item, file, line);
  751         return (&_NGI_MSG(item));
  752 }
  753 
  754 static __inline ng_ID_t *
  755 _ngi_retaddr(item_p item, char *file, int line)
  756 {
  757         _ngi_check(item, file, line);
  758         return (&_NGI_RETADDR(item));
  759 }
  760 
  761 static __inline ng_item_fn **
  762 _ngi_fn(item_p item, char *file, int line)
  763 {
  764         _ngi_check(item, file, line);
  765         return (&_NGI_FN(item));
  766 }
  767 
  768 static __inline ng_item_fn2 **
  769 _ngi_fn2(item_p item, char *file, int line)
  770 {
  771         _ngi_check(item, file, line);
  772         return (&_NGI_FN2(item));
  773 }
  774 
  775 static __inline void **
  776 _ngi_arg1(item_p item, char *file, int line)
  777 {
  778         _ngi_check(item, file, line);
  779         return (&_NGI_ARG1(item));
  780 }
  781 
  782 static __inline int *
  783 _ngi_arg2(item_p item, char *file, int line)
  784 {
  785         _ngi_check(item, file, line);
  786         return (&_NGI_ARG2(item));
  787 }
  788 
  789 static __inline node_p
  790 _ngi_node(item_p item, char *file, int line)
  791 {
  792         _ngi_check(item, file, line);
  793         return (_NGI_NODE(item));
  794 }
  795 
  796 static __inline hook_p
  797 _ngi_hook(item_p item, char *file, int line)
  798 {
  799         _ngi_check(item, file, line);
  800         return (_NGI_HOOK(item));
  801 }
  802 
  803 #define NGI_M(i)        (*_ngi_m(i, _NN_))
  804 #define NGI_MSG(i)      (*_ngi_msg(i, _NN_))
  805 #define NGI_RETADDR(i)  (*_ngi_retaddr(i, _NN_))
  806 #define NGI_FN(i)       (*_ngi_fn(i, _NN_))
  807 #define NGI_FN2(i)      (*_ngi_fn2(i, _NN_))
  808 #define NGI_ARG1(i)     (*_ngi_arg1(i, _NN_))
  809 #define NGI_ARG2(i)     (*_ngi_arg2(i, _NN_))
  810 #define NGI_HOOK(i)     _ngi_hook(i, _NN_)
  811 #define NGI_NODE(i)     _ngi_node(i, _NN_)
  812 #define NGI_SET_HOOK(i,h)                                               \
  813         do { _ngi_check(i, _NN_); _NGI_SET_HOOK(i, h); } while (0)
  814 #define NGI_CLR_HOOK(i)                                                 \
  815         do { _ngi_check(i, _NN_); _NGI_CLR_HOOK(i); } while (0)
  816 #define NGI_SET_NODE(i,n)                                               \
  817         do { _ngi_check(i, _NN_); _NGI_SET_NODE(i, n); } while (0)
  818 #define NGI_CLR_NODE(i)                                                 \
  819         do { _ngi_check(i, _NN_); _NGI_CLR_NODE(i); } while (0)
  820 
  821 #define NG_FREE_ITEM(item)                                              \
  822         do {                                                            \
  823                 _ngi_check(item, _NN_);                                 \
  824                 ng_free_item((item));                                   \
  825         } while (0)
  826 
  827 #define SAVE_LINE(item)                                                 \
  828         do {                                                            \
  829                 (item)->lastline = __LINE__;                            \
  830                 (item)->lastfile = __FILE__;                            \
  831         } while (0)
  832 
  833 #else   /* NETGRAPH_DEBUG */ /*----------------------------------------------*/
  834 
  835 #define NGI_M(i)        _NGI_M(i)
  836 #define NGI_MSG(i)      _NGI_MSG(i)
  837 #define NGI_RETADDR(i)  _NGI_RETADDR(i)
  838 #define NGI_FN(i)       _NGI_FN(i)
  839 #define NGI_FN2(i)      _NGI_FN2(i)
  840 #define NGI_ARG1(i)     _NGI_ARG1(i)
  841 #define NGI_ARG2(i)     _NGI_ARG2(i)
  842 #define NGI_NODE(i)     _NGI_NODE(i)
  843 #define NGI_HOOK(i)     _NGI_HOOK(i)
  844 #define NGI_SET_HOOK(i,h) _NGI_SET_HOOK(i,h)
  845 #define NGI_CLR_HOOK(i)   _NGI_CLR_HOOK(i)
  846 #define NGI_SET_NODE(i,n) _NGI_SET_NODE(i,n)
  847 #define NGI_CLR_NODE(i)   _NGI_CLR_NODE(i)
  848 
  849 #define NG_FREE_ITEM(item)      ng_free_item((item))
  850 #define SAVE_LINE(item)         do {} while (0)
  851 
  852 #endif  /* NETGRAPH_DEBUG */ /*----------------------------------------------*/
  853 
  854 #define NGI_GET_M(i,m)                                                  \
  855         do {                                                            \
  856                 (m) = NGI_M(i);                                         \
  857                 _NGI_M(i) = NULL;                                       \
  858         } while (0)
  859 
  860 #define NGI_GET_MSG(i,m)                                                \
  861         do {                                                            \
  862                 (m) = NGI_MSG(i);                                       \
  863                 _NGI_MSG(i) = NULL;                                     \
  864         } while (0)
  865 
  866 #define NGI_GET_NODE(i,n)       /* YOU NOW HAVE THE REFERENCE */        \
  867         do {                                                            \
  868                 (n) = NGI_NODE(i);                                      \
  869                 _NGI_NODE(i) = NULL;                                    \
  870         } while (0)
  871 
  872 #define NGI_GET_HOOK(i,h)                                               \
  873         do {                                                            \
  874                 (h) = NGI_HOOK(i);                                      \
  875                 _NGI_HOOK(i) = NULL;                                    \
  876         } while (0)
  877 
  878 #define NGI_SET_WRITER(i)       ((i)->el_flags &= ~NGQF_QMODE)
  879 #define NGI_SET_READER(i)       ((i)->el_flags |= NGQF_QREADER)
  880 
  881 #define NGI_QUEUED_READER(i)    ((i)->el_flags & NGQF_QREADER)
  882 #define NGI_QUEUED_WRITER(i)    (((i)->el_flags & NGQF_QMODE) == NGQF_QWRITER)
  883         
  884 /**********************************************************************
  885 * Data macros.  Send, manipulate and free.
  886 **********************************************************************/
  887 /*
  888  * Assuming the data is already ok, just set the new address and send
  889  */
  890 #define NG_FWD_ITEM_HOOK_FLAGS(error, item, hook, flags)                \
  891         do {                                                            \
  892                 (error) =                                               \
  893                     ng_address_hook(NULL, (item), (hook), NG_NOFLAGS);  \
  894                 if (error == 0) {                                       \
  895                         SAVE_LINE(item);                                \
  896                         (error) = ng_snd_item((item), (flags));         \
  897                 }                                                       \
  898                 (item) = NULL;                                          \
  899         } while (0)
  900 #define NG_FWD_ITEM_HOOK(error, item, hook)     \
  901                 NG_FWD_ITEM_HOOK_FLAGS(error, item, hook, NG_NOFLAGS)
  902 
  903 /*
  904  * Forward a data packet. Mbuf pointer is updated to new value. We
  905  * presume you dealt with the old one when you update it to the new one
  906  * (or it maybe the old one). We got a packet and possibly had to modify
  907  * the mbuf. You should probably use NGI_GET_M() if you are going to use
  908  * this too.
  909  */
  910 #define NG_FWD_NEW_DATA_FLAGS(error, item, hook, m, flags)              \
  911         do {                                                            \
  912                 NGI_M(item) = (m);                                      \
  913                 (m) = NULL;                                             \
  914                 NG_FWD_ITEM_HOOK_FLAGS(error, item, hook, flags);       \
  915         } while (0)
  916 #define NG_FWD_NEW_DATA(error, item, hook, m)   \
  917                 NG_FWD_NEW_DATA_FLAGS(error, item, hook, m, NG_NOFLAGS)
  918 
  919 /* Send a previously unpackaged mbuf. XXX: This should be called
  920  * NG_SEND_DATA in future, but this name is kept for compatibility
  921  * reasons.
  922  */
  923 #define NG_SEND_DATA_FLAGS(error, hook, m, flags)                       \
  924         do {                                                            \
  925                 item_p _item;                                           \
  926                 if ((_item = ng_package_data((m), flags))) {            \
  927                         NG_FWD_ITEM_HOOK_FLAGS(error, _item, hook, flags);\
  928                 } else {                                                \
  929                         (error) = ENOMEM;                               \
  930                 }                                                       \
  931                 (m) = NULL;                                             \
  932         } while (0)
  933 
  934 #define NG_SEND_DATA_ONLY(error, hook, m)       \
  935                 NG_SEND_DATA_FLAGS(error, hook, m, NG_NOFLAGS)
  936 /* NG_SEND_DATA() compat for meta-data times */
  937 #define NG_SEND_DATA(error, hook, m, x) \
  938                 NG_SEND_DATA_FLAGS(error, hook, m, NG_NOFLAGS)
  939 
  940 #define NG_FREE_MSG(msg)                                                \
  941         do {                                                            \
  942                 if ((msg)) {                                            \
  943                         free((msg), M_NETGRAPH_MSG);                    \
  944                         (msg) = NULL;                                   \
  945                 }                                                       \
  946         } while (0)
  947 
  948 #define NG_FREE_M(m)                                                    \
  949         do {                                                            \
  950                 if ((m)) {                                              \
  951                         m_freem((m));                                   \
  952                         (m) = NULL;                                     \
  953                 }                                                       \
  954         } while (0)
  955 
  956 /*****************************************
  957 * Message macros
  958 *****************************************/
  959 
  960 #define NG_SEND_MSG_HOOK(error, here, msg, hook, retaddr)               \
  961         do {                                                            \
  962                 item_p _item;                                           \
  963                 if ((_item = ng_package_msg(msg, NG_NOFLAGS)) == NULL) {\
  964                         (msg) = NULL;                                   \
  965                         (error) = ENOMEM;                               \
  966                         break;                                          \
  967                 }                                                       \
  968                 if (((error) = ng_address_hook((here), (_item),         \
  969                                         (hook), (retaddr))) == 0) {     \
  970                         SAVE_LINE(_item);                               \
  971                         (error) = ng_snd_item((_item), 0);              \
  972                 }                                                       \
  973                 (msg) = NULL;                                           \
  974         } while (0)
  975 
  976 #define NG_SEND_MSG_PATH(error, here, msg, path, retaddr)               \
  977         do {                                                            \
  978                 item_p _item;                                           \
  979                 if ((_item = ng_package_msg(msg, NG_NOFLAGS)) == NULL) {\
  980                         (msg) = NULL;                                   \
  981                         (error) = ENOMEM;                               \
  982                         break;                                          \
  983                 }                                                       \
  984                 if (((error) = ng_address_path((here), (_item),         \
  985                                         (path), (retaddr))) == 0) {     \
  986                         SAVE_LINE(_item);                               \
  987                         (error) = ng_snd_item((_item), 0);              \
  988                 }                                                       \
  989                 (msg) = NULL;                                           \
  990         } while (0)
  991 
  992 #define NG_SEND_MSG_ID(error, here, msg, ID, retaddr)                   \
  993         do {                                                            \
  994                 item_p _item;                                           \
  995                 if ((_item = ng_package_msg(msg, NG_NOFLAGS)) == NULL) {\
  996                         (msg) = NULL;                                   \
  997                         (error) = ENOMEM;                               \
  998                         break;                                          \
  999                 }                                                       \
 1000                 if (((error) = ng_address_ID((here), (_item),           \
 1001                                         (ID), (retaddr))) == 0) {       \
 1002                         SAVE_LINE(_item);                               \
 1003                         (error) = ng_snd_item((_item), 0);              \
 1004                 }                                                       \
 1005                 (msg) = NULL;                                           \
 1006         } while (0)
 1007 
 1008 /*
 1009  * Redirect the message to the next hop using the given hook.
 1010  * ng_retarget_msg() frees the item if there is an error
 1011  * and returns an error code.  It returns 0 on success.
 1012  */
 1013 #define NG_FWD_MSG_HOOK(error, here, item, hook, retaddr)               \
 1014         do {                                                            \
 1015                 if (((error) = ng_address_hook((here), (item),          \
 1016                                         (hook), (retaddr))) == 0) {     \
 1017                         SAVE_LINE(item);                                \
 1018                         (error) = ng_snd_item((item), 0);               \
 1019                 }                                                       \
 1020                 (item) = NULL;                                          \
 1021         } while (0)
 1022 
 1023 /*
 1024  * Send a queue item back to it's originator with a response message.
 1025  * Assume original message was removed and freed separatly.
 1026  */
 1027 #define NG_RESPOND_MSG(error, here, item, resp)                         \
 1028         do {                                                            \
 1029                 if (resp) {                                             \
 1030                         ng_ID_t _dest = NGI_RETADDR(item);              \
 1031                         NGI_RETADDR(item) = 0;                          \
 1032                         NGI_MSG(item) = resp;                           \
 1033                         if ((error = ng_address_ID((here), (item),      \
 1034                                         _dest, 0)) == 0) {              \
 1035                                 SAVE_LINE(item);                        \
 1036                                 (error) = ng_snd_item((item), NG_QUEUE);\
 1037                         }                                               \
 1038                 } else                                                  \
 1039                         NG_FREE_ITEM(item);                             \
 1040                 (item) = NULL;                                          \
 1041         } while (0)
 1042 
 1043 
 1044 /***********************************************************************
 1045  ******** Structures Definitions and Macros for defining a node  *******
 1046  ***********************************************************************
 1047  *
 1048  * Here we define the structures needed to actually define a new node
 1049  * type.
 1050  */
 1051 
 1052 /*
 1053  * Command list -- each node type specifies the command that it knows
 1054  * how to convert between ASCII and binary using an array of these.
 1055  * The last element in the array must be a terminator with cookie=0.
 1056  */
 1057 
 1058 struct ng_cmdlist {
 1059         u_int32_t                       cookie;         /* command typecookie */
 1060         int                             cmd;            /* command number */
 1061         const char                      *name;          /* command name */
 1062         const struct ng_parse_type      *mesgType;      /* args if !NGF_RESP */
 1063         const struct ng_parse_type      *respType;      /* args if NGF_RESP */
 1064 };
 1065 
 1066 /*
 1067  * Structure of a node type
 1068  * If data is sent to the "rcvdata()" entrypoint then the system
 1069  * may decide to defer it until later by queing it with the normal netgraph
 1070  * input queuing system.  This is decidde by the HK_QUEUE flag being set in
 1071  * the flags word of the peer (receiving) hook. The dequeuing mechanism will
 1072  * ensure it is not requeued again.
 1073  * Note the input queueing system is to allow modules
 1074  * to 'release the stack' or to pass data across spl layers.
 1075  * The data will be redelivered as soon as the NETISR code runs
 1076  * which may be almost immediately.  A node may also do it's own queueing
 1077  * for other reasons (e.g. device output queuing).
 1078  */
 1079 struct ng_type {
 1080 
 1081         u_int32_t       version;        /* must equal NG_API_VERSION */
 1082         const char      *name;          /* Unique type name */
 1083         modeventhand_t  mod_event;      /* Module event handler (optional) */
 1084         ng_constructor_t *constructor;  /* Node constructor */
 1085         ng_rcvmsg_t     *rcvmsg;        /* control messages come here */
 1086         ng_close_t      *close;         /* warn about forthcoming shutdown */
 1087         ng_shutdown_t   *shutdown;      /* reset, and free resources */
 1088         ng_newhook_t    *newhook;       /* first notification of new hook */
 1089         ng_findhook_t   *findhook;      /* only if you have lots of hooks */
 1090         ng_connect_t    *connect;       /* final notification of new hook */
 1091         ng_rcvdata_t    *rcvdata;       /* data comes here */
 1092         ng_disconnect_t *disconnect;    /* notify on disconnect */
 1093 
 1094         const struct    ng_cmdlist *cmdlist;    /* commands we can convert */
 1095 
 1096         /* R/W data private to the base netgraph code DON'T TOUCH! */
 1097         LIST_ENTRY(ng_type) types;              /* linked list of all types */
 1098         int                 refs;               /* number of instances */
 1099 };
 1100 
 1101 /*
 1102  * Use the NETGRAPH_INIT() macro to link a node type into the
 1103  * netgraph system. This works for types compiled into the kernel
 1104  * as well as KLD modules. The first argument should be the type
 1105  * name (eg, echo) and the second a pointer to the type struct.
 1106  *
 1107  * If a different link time is desired, e.g., a device driver that
 1108  * needs to install its netgraph type before probing, use the
 1109  * NETGRAPH_INIT_ORDERED() macro instead.  Device drivers probably
 1110  * want to use SI_SUB_DRIVERS/SI_ORDER_FIRST.
 1111  */
 1112 
 1113 #define NETGRAPH_INIT_ORDERED(typename, typestructp, sub, order)        \
 1114 static moduledata_t ng_##typename##_mod = {                             \
 1115         "ng_" #typename,                                                \
 1116         ng_mod_event,                                                   \
 1117         (typestructp)                                                   \
 1118 };                                                                      \
 1119 DECLARE_MODULE(ng_##typename, ng_##typename##_mod, sub, order);         \
 1120 MODULE_DEPEND(ng_##typename, netgraph,  NG_ABI_VERSION,                 \
 1121                                         NG_ABI_VERSION,                 \
 1122                                         NG_ABI_VERSION)
 1123 
 1124 #define NETGRAPH_INIT(tn, tp)                                           \
 1125         NETGRAPH_INIT_ORDERED(tn, tp, SI_SUB_PSEUDO, SI_ORDER_MIDDLE)
 1126 
 1127 /* Special malloc() type for netgraph structs and ctrl messages */
 1128 /* Only these two types should be visible to nodes */
 1129 MALLOC_DECLARE(M_NETGRAPH);
 1130 MALLOC_DECLARE(M_NETGRAPH_MSG);
 1131 
 1132 /* declare the base of the netgraph sysclt hierarchy */
 1133 /* but only if this file cares about sysctls */
 1134 #ifdef  SYSCTL_DECL
 1135 SYSCTL_DECL(_net_graph);
 1136 #endif
 1137 
 1138 /*
 1139  * Methods that the nodes can use.
 1140  * Many of these methods should usually NOT be used directly but via
 1141  * Macros above.
 1142  */
 1143 int     ng_address_ID(node_p here, item_p item, ng_ID_t ID, ng_ID_t retaddr);
 1144 int     ng_address_hook(node_p here, item_p item, hook_p hook, ng_ID_t retaddr);
 1145 int     ng_address_path(node_p here, item_p item, const char *address, ng_ID_t raddr);
 1146 int     ng_bypass(hook_p hook1, hook_p hook2);
 1147 hook_p  ng_findhook(node_p node, const char *name);
 1148 struct  ng_type *ng_findtype(const char *type);
 1149 int     ng_make_node_common(struct ng_type *typep, node_p *nodep);
 1150 int     ng_name_node(node_p node, const char *name);
 1151 node_p  ng_name2noderef(node_p node, const char *name);
 1152 int     ng_newtype(struct ng_type *tp);
 1153 ng_ID_t ng_node2ID(node_cp node);
 1154 item_p  ng_package_data(struct mbuf *m, int flags);
 1155 item_p  ng_package_msg(struct ng_mesg *msg, int flags);
 1156 item_p  ng_package_msg_self(node_p here, hook_p hook, struct ng_mesg *msg);
 1157 void    ng_replace_retaddr(node_p here, item_p item, ng_ID_t retaddr);
 1158 int     ng_rmhook_self(hook_p hook);    /* if a node wants to kill a hook */
 1159 int     ng_rmnode_self(node_p here);    /* if a node wants to suicide */
 1160 int     ng_rmtype(struct ng_type *tp);
 1161 int     ng_snd_item(item_p item, int queue);
 1162 int     ng_send_fn(node_p node, hook_p hook, ng_item_fn *fn, void *arg1,
 1163         int arg2);
 1164 int     ng_send_fn1(node_p node, hook_p hook, ng_item_fn *fn, void *arg1,
 1165         int arg2, int flags);
 1166 int     ng_send_fn2(node_p node, hook_p hook, item_p pitem, ng_item_fn2 *fn,
 1167         void *arg1, int arg2, int flags);
 1168 int     ng_uncallout(struct callout *c, node_p node);
 1169 int     ng_callout(struct callout *c, node_p node, hook_p hook, int ticks,
 1170             ng_item_fn *fn, void * arg1, int arg2);
 1171 #define ng_callout_init(c)      callout_init(c, 1)
 1172 
 1173 /* Flags for netgraph functions. */
 1174 #define NG_NOFLAGS      0x00000000      /* no special options */
 1175 #define NG_QUEUE        0x00000001      /* enqueue item, don't dispatch */
 1176 #define NG_WAITOK       0x00000002      /* use M_WAITOK, etc. */
 1177 /* XXXGL: NG_PROGRESS unused since ng_base.c rev. 1.136. Should be deleted? */
 1178 #define NG_PROGRESS     0x00000004      /* return EINPROGRESS if queued */
 1179 #define NG_REUSE_ITEM   0x00000008      /* supplied item should be reused */
 1180 
 1181 /*
 1182  * prototypes the user should DEFINITELY not use directly
 1183  */
 1184 void    ng_free_item(item_p item); /* Use NG_FREE_ITEM instead */
 1185 int     ng_mod_event(module_t mod, int what, void *arg);
 1186 
 1187 /*
 1188  * Tag definitions and constants
 1189  */
 1190 
 1191 #define NG_TAG_PRIO     1
 1192 
 1193 struct ng_tag_prio {
 1194         struct m_tag    tag;
 1195         char    priority;
 1196         char    discardability;
 1197 };
 1198 
 1199 #define NG_PRIO_CUTOFF          32
 1200 #define NG_PRIO_LINKSTATE       64
 1201 
 1202 /* Macros and declarations to keep compatibility with metadata, which
 1203  * is obsoleted now. To be deleted.
 1204  */
 1205 typedef void *meta_p;
 1206 #define _NGI_META(i)    NULL
 1207 #define NGI_META(i)     NULL
 1208 #define NG_FREE_META(meta)
 1209 #define NGI_GET_META(i,m)
 1210 #define ng_copy_meta(meta) NULL
 1211 
 1212 /*
 1213  * Mark the current thread when called from the outbound path of the
 1214  * network stack, in order to enforce queuing on ng nodes calling into
 1215  * the inbound network stack path.
 1216  */
 1217 #define NG_OUTBOUND_THREAD_REF()                                        \
 1218         curthread->td_ng_outbound++
 1219 #define NG_OUTBOUND_THREAD_UNREF()                                      \
 1220         do {                                                            \
 1221                 curthread->td_ng_outbound--;                            \
 1222                 KASSERT(curthread->td_ng_outbound >= 0,                 \
 1223                     ("%s: negative td_ng_outbound", __func__));         \
 1224         } while (0)
 1225 
 1226 #endif /* _NETGRAPH_NETGRAPH_H_ */

Cache object: d1d033fe6d538da48c0f53361c704cc6


[ 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.