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-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-2  -  FREEBSD-11-1  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-4  -  FREEBSD-10-3  -  FREEBSD-10-2  -  FREEBSD-10-1  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-3  -  FREEBSD-9-2  -  FREEBSD-9-1  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-4  -  FREEBSD-8-3  -  FREEBSD-8-2  -  FREEBSD-8-1  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-4  -  FREEBSD-7-3  -  FREEBSD-7-2  -  FREEBSD-7-1  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-4  -  FREEBSD-6-3  -  FREEBSD-6-2  -  FREEBSD-6-1  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-5  -  FREEBSD-5-4  -  FREEBSD-5-3  -  FREEBSD-5-2  -  FREEBSD-5-1  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  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 
SearchContext: -  none  -  3  -  10 

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

Cache object: 732309c6e919e4b9b51d32e9995f19e3


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