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

Cache object: b4f18a4cc827bbf15e346db872891598


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