[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ]

FreeBSD/Linux Kernel Cross Reference
sys/netgraph/netgraph.h

Version: -  FREEBSD  -  FREEBSD7  -  FREEBSD70  -  FREEBSD6  -  FREEBSD64  -  FREEBSD63  -  FREEBSD62  -  FREEBSD61  -  FREEBSD60  -  FREEBSD5  -  FREEBSD55  -  FREEBSD54  -  FREEBSD53  -  FREEBSD52  -  FREEBSD51  -  FREEBSD50  -  FREEBSD4  -  FREEBSD3  -  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  -  OPENSOLARIS  -  minix-3-1-1  -  TRUSTEDBSD-SEBSD  -  FREEBSD-LIBC  -  FREEBSD7-LIBC  -  FREEBSD6-LIBC  -  GLIBC27 
SearchContext: -  none  -  excerpts  -  bigexcerpts 

  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: src/sys/netgraph/netgraph.h,v 1.78 2008/11/28 23:30:51 zec Exp $
 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 12
 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_type;                /* tbd: hook data link type */
113         struct  ng_hook *hk_peer;       /* the other end of this link */
114         struct  ng_node *hk_node;       /* The node this hook is attached to */
115         LIST_ENTRY(ng_hook) hk_hooks;   /* linked list of all hooks on node */
116         ng_rcvmsg_t     *hk_rcvmsg;     /* control messages come here */
117         ng_rcvdata_t    *hk_rcvdata;    /* data comes here */
118         int     hk_refs;                /* dont actually free this till 0 */
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_int           q_flags;        /* Current r/w/q lock flags */
337         u_int           q_flags2;       /* Other queue flags */
338         struct mtx      q_mtx;
339         STAILQ_ENTRY(ng_node)   q_work; /* nodes with work to do */
340         STAILQ_HEAD(, ng_item)  queue;  /* actually items queue */
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_numhooks;            /* number of hooks */
348         void   *nd_private;             /* node type dependant node ID */
349         ng_ID_t nd_ID;                  /* Unique per node */
350         LIST_HEAD(hooks, ng_hook) nd_hooks;     /* linked list of node hooks */
351         LIST_ENTRY(ng_node)       nd_nodes;     /* linked list of all nodes */
352         LIST_ENTRY(ng_node)       nd_idnodes;   /* ID hash collision list */
353         struct  ng_queue          nd_input_queue; /* input queue for locking */
354         int     nd_refs;                /* # of references to this node */
355 #ifdef  NETGRAPH_DEBUG /*----------------------------------------------*/
356 #define ND_MAGIC 0x59264837
357         int     nd_magic;
358         char    *lastfile;
359         int     lastline;
360         SLIST_ENTRY(ng_node)      nd_all;       /* all existing nodes */
361 #endif  /* NETGRAPH_DEBUG */ /*----------------------------------------------*/
362 };
363 
364 /* Flags for a node */
365 #define NGF_INVALID     0x00000001      /* free when refs go to 0 */
366 #define NG_INVALID      NGF_INVALID     /* compat for old code */
367 #define NGF_FORCE_WRITER        0x00000004      /* Never multithread this node */
368 #define NG_FORCE_WRITER NGF_FORCE_WRITER /* compat for old code */
369 #define NGF_CLOSING     0x00000008      /* ng_rmnode() at work */
370 #define NG_CLOSING      NGF_CLOSING     /* compat for old code */
371 #define NGF_REALLY_DIE  0x00000010      /* "persistent" node is unloading */
372 #define NG_REALLY_DIE   NGF_REALLY_DIE  /* compat for old code */
373 #define NGF_HI_STACK    0x00000020      /* node has hi stack usage */
374 #define NGF_TYPE1       0x10000000      /* reserved for type specific storage */
375 #define NGF_TYPE2       0x20000000      /* reserved for type specific storage */
376 #define NGF_TYPE3       0x40000000      /* reserved for type specific storage */
377 #define NGF_TYPE4       0x80000000      /* reserved for type specific storage */
378 
379 /*
380  * Public methods for nodes.
381  * If you can't do it with these you probably shouldn't be doing it.
382  */
383 int     ng_unref_node(node_p node); /* don't move this */
384 #define _NG_NODE_NAME(node)     ((node)->nd_name + 0)
385 #define _NG_NODE_HAS_NAME(node) ((node)->nd_name[0] + 0)
386 #define _NG_NODE_ID(node)       ((node)->nd_ID + 0)
387 #define _NG_NODE_REF(node)      atomic_add_int(&(node)->nd_refs, 1)
388 #define _NG_NODE_UNREF(node)    ng_unref_node(node)
389 #define _NG_NODE_SET_PRIVATE(node, val) do {(node)->nd_private = val;} while (0)
390 #define _NG_NODE_PRIVATE(node)  ((node)->nd_private)
391 #define _NG_NODE_IS_VALID(node) (!((node)->nd_flags & NGF_INVALID))
392 #define _NG_NODE_NOT_VALID(node)        ((node)->nd_flags & NGF_INVALID)
393 #define _NG_NODE_NUMHOOKS(node) ((node)->nd_numhooks + 0) /* rvalue */
394 #define _NG_NODE_FORCE_WRITER(node)                                     \
395         do{ node->nd_flags |= NGF_FORCE_WRITER; }while (0)
396 #define _NG_NODE_HI_STACK(node)                                         \
397         do{ node->nd_flags |= NGF_HI_STACK; }while (0)
398 #define _NG_NODE_REALLY_DIE(node)                                       \
399         do{ node->nd_flags |= (NGF_REALLY_DIE|NGF_INVALID); }while (0)
400 #define _NG_NODE_REVIVE(node) \
401         do { node->nd_flags &= ~NGF_INVALID; } while (0)
402 /*
403  * The hook iterator.
404  * This macro will call a function of type ng_fn_eachhook for each
405  * hook attached to the node. If the function returns 0, then the
406  * iterator will stop and return a pointer to the hook that returned 0.
407  */
408 typedef int     ng_fn_eachhook(hook_p hook, void* arg);
409 #define _NG_NODE_FOREACH_HOOK(node, fn, arg, rethook)                   \
410         do {                                                            \
411                 hook_p _hook;                                           \
412                 (rethook) = NULL;                                       \
413                 LIST_FOREACH(_hook, &((node)->nd_hooks), hk_hooks) {    \
414                         if ((fn)(_hook, arg) == 0) {                    \
415                                 (rethook) = _hook;                      \
416                                 break;                                  \
417                         }                                               \
418                 }                                                       \
419         } while (0)
420 
421 #ifdef  NETGRAPH_DEBUG /*----------------------------------------------*/
422 void    dumpnode(node_p node, char *file, int line);
423 static __inline void _chknode(node_p node, char *file, int line);
424 static __inline char * _ng_node_name(node_p node, char *file, int line);
425 static __inline int _ng_node_has_name(node_p node, char *file, int line);
426 static __inline ng_ID_t _ng_node_id(node_p node, char *file, int line);
427 static __inline void _ng_node_ref(node_p node, char *file, int line);
428 static __inline int _ng_node_unref(node_p node, char *file, int line);
429 static __inline void _ng_node_set_private(node_p node, void * val,
430                                                         char *file, int line);
431 static __inline void * _ng_node_private(node_p node, char *file, int line);
432 static __inline int _ng_node_is_valid(node_p node, char *file, int line);
433 static __inline int _ng_node_not_valid(node_p node, char *file, int line);
434 static __inline int _ng_node_numhooks(node_p node, char *file, int line);
435 static __inline void _ng_node_force_writer(node_p node, char *file, int line);
436 static __inline hook_p _ng_node_foreach_hook(node_p node,
437                         ng_fn_eachhook *fn, void *arg, char *file, int line);
438 static __inline void _ng_node_revive(node_p node, char *file, int line);
439 
440 static __inline void
441 _chknode(node_p node, char *file, int line)
442 {
443         if (node->nd_magic != ND_MAGIC) {
444                 printf("Accessing freed node ");
445                 dumpnode(node, file, line);
446         }
447         node->lastline = line;
448         node->lastfile = file;
449 }
450 
451 static __inline char *
452 _ng_node_name(node_p node, char *file, int line)
453 {
454         _chknode(node, file, line);
455         return(_NG_NODE_NAME(node));
456 }
457 
458 static __inline int
459 _ng_node_has_name(node_p node, char *file, int line)
460 {
461         _chknode(node, file, line);
462         return(_NG_NODE_HAS_NAME(node));
463 }
464 
465 static __inline ng_ID_t
466 _ng_node_id(node_p node, char *file, int line)
467 {
468         _chknode(node, file, line);
469         return(_NG_NODE_ID(node));
470 }
471 
472 static __inline void
473 _ng_node_ref(node_p node, char *file, int line)
474 {
475         _chknode(node, file, line);
476         _NG_NODE_REF(node);
477 }
478 
479 static __inline int
480 _ng_node_unref(node_p node, char *file, int line)
481 {
482         _chknode(node, file, line);
483         return (_NG_NODE_UNREF(node));
484 }
485 
486 static __inline void
487 _ng_node_set_private(node_p node, void * val, char *file, int line)
488 {
489         _chknode(node, file, line);
490         _NG_NODE_SET_PRIVATE(node, val);
491 }
492 
493 static __inline void *
494 _ng_node_private(node_p node, char *file, int line)
495 {
496         _chknode(node, file, line);
497         return (_NG_NODE_PRIVATE(node));
498 }
499 
500 static __inline int
501 _ng_node_is_valid(node_p node, char *file, int line)
502 {
503         _chknode(node, file, line);
504         return(_NG_NODE_IS_VALID(node));
505 }
506 
507 static __inline int
508 _ng_node_not_valid(node_p node, char *file, int line)
509 {
510         _chknode(node, file, line);
511         return(_NG_NODE_NOT_VALID(node));
512 }
513 
514 static __inline int
515 _ng_node_numhooks(node_p node, char *file, int line)
516 {
517         _chknode(node, file, line);
518         return(_NG_NODE_NUMHOOKS(node));
519 }
520 
521 static __inline void
522 _ng_node_force_writer(node_p node, char *file, int line)
523 {
524         _chknode(node, file, line);
525         _NG_NODE_FORCE_WRITER(node);
526 }
527 
528 static __inline void
529 _ng_node_hi_stack(node_p node, char *file, int line)
530 {
531         _chknode(node, file, line);
532         _NG_NODE_HI_STACK(node);
533 }
534 
535 static __inline void
536 _ng_node_really_die(node_p node, char *file, int line)
537 {
538         _chknode(node, file, line);
539         _NG_NODE_REALLY_DIE(node);
540 }
541 
542 static __inline void
543 _ng_node_revive(node_p node, char *file, int line)
544 {
545         _chknode(node, file, line);
546         _NG_NODE_REVIVE(node);
547 }
548 
549 static __inline hook_p
550 _ng_node_foreach_hook(node_p node, ng_fn_eachhook *fn, void *arg,
551                                                 char *file, int line)
552 {
553         hook_p hook;
554         _chknode(node, file, line);
555         _NG_NODE_FOREACH_HOOK(node, fn, arg, hook);
556         return (hook);
557 }
558 
559 #define NG_NODE_NAME(node)              _ng_node_name(node, _NN_)       
560 #define NG_NODE_HAS_NAME(node)          _ng_node_has_name(node, _NN_)   
561 #define NG_NODE_ID(node)                _ng_node_id(node, _NN_)
562 #define NG_NODE_REF(node)               _ng_node_ref(node, _NN_)
563 #define NG_NODE_UNREF(node)             _ng_node_unref(node, _NN_)
564 #define NG_NODE_SET_PRIVATE(node, val)  _ng_node_set_private(node, val, _NN_)
565 #define NG_NODE_PRIVATE(node)           _ng_node_private(node, _NN_)
566 #define NG_NODE_IS_VALID(node)          _ng_node_is_valid(node, _NN_)
567 #define NG_NODE_NOT_VALID(node)         _ng_node_not_valid(node, _NN_)
568 #define NG_NODE_FORCE_WRITER(node)      _ng_node_force_writer(node, _NN_)
569 #define NG_NODE_HI_STACK(node)          _ng_node_hi_stack(node, _NN_)
570 #define NG_NODE_REALLY_DIE(node)        _ng_node_really_die(node, _NN_)
571 #define NG_NODE_NUMHOOKS(node)          _ng_node_numhooks(node, _NN_)
572 #define NG_NODE_REVIVE(node)            _ng_node_revive(node, _NN_)
573 #define NG_NODE_FOREACH_HOOK(node, fn, arg, rethook)                          \
574         do {                                                                  \
575                 rethook = _ng_node_foreach_hook(node, fn, (void *)arg, _NN_); \
576         } while (0)
577 
578 #else   /* NETGRAPH_DEBUG */ /*----------------------------------------------*/
579 
580 #define NG_NODE_NAME(node)              _NG_NODE_NAME(node)     
581 #define NG_NODE_HAS_NAME(node)          _NG_NODE_HAS_NAME(node) 
582 #define NG_NODE_ID(node)                _NG_NODE_ID(node)       
583 #define NG_NODE_REF(node)               _NG_NODE_REF(node)      
584 #define NG_NODE_UNREF(node)             _NG_NODE_UNREF(node)    
585 #define NG_NODE_SET_PRIVATE(node, val)  _NG_NODE_SET_PRIVATE(node, val) 
586 #define NG_NODE_PRIVATE(node)           _NG_NODE_PRIVATE(node)  
587 #define NG_NODE_IS_VALID(node)          _NG_NODE_IS_VALID(node) 
588 #define NG_NODE_NOT_VALID(node)         _NG_NODE_NOT_VALID(node)        
589 #define NG_NODE_FORCE_WRITER(node)      _NG_NODE_FORCE_WRITER(node)
590 #define NG_NODE_HI_STACK(node)          _NG_NODE_HI_STACK(node)
591 #define NG_NODE_REALLY_DIE(node)        _NG_NODE_REALLY_DIE(node)
592 #define NG_NODE_NUMHOOKS(node)          _NG_NODE_NUMHOOKS(node) 
593 #define NG_NODE_REVIVE(node)            _NG_NODE_REVIVE(node)
594 #define NG_NODE_FOREACH_HOOK(node, fn, arg, rethook)                    \
595                 _NG_NODE_FOREACH_HOOK(node, fn, arg, rethook)
596 #endif  /* NETGRAPH_DEBUG */ /*----------------------------------------------*/
597 
598 /***********************************************************************
599  ************* Node Queue and Item Structures and Methods **************
600  ***********************************************************************
601  *
602  */
603 typedef void    ng_item_fn(node_p node, hook_p hook, void *arg1, int arg2);
604 typedef int     ng_item_fn2(node_p node, struct ng_item *item, hook_p hook);
605 typedef void    ng_apply_t(void *context, int error);
606 struct ng_apply_info {
607         ng_apply_t      *apply;
608         void            *context;
609         int             refs;
610         int             error;
611 };
612 struct ng_item {
613         u_long  el_flags;
614         STAILQ_ENTRY(ng_item)   el_next;
615         node_p  el_dest; /* The node it will be applied against (or NULL) */
616         hook_p  el_hook; /* Entering hook. Optional in Control messages */
617         union {
618                 struct mbuf     *da_m;
619                 struct {
620                         struct ng_mesg  *msg_msg;
621                         ng_ID_t         msg_retaddr;
622                 } msg;
623                 struct {
624                         union {
625                                 ng_item_fn      *fn_fn;
626                                 ng_item_fn2     *fn_fn2;
627                         } fn_fn;
628                         void            *fn_arg1;
629                         int             fn_arg2;
630                 } fn;
631         } body;
632         /*
633          * Optional callback called when item is being applied,
634          * and its context.
635          */
636         struct ng_apply_info    *apply;
637         u_int   depth;
638 #ifdef  NETGRAPH_DEBUG /*----------------------------------------------*/
639         char *lastfile;
640         int  lastline;
641         TAILQ_ENTRY(ng_item)      all;          /* all existing items */
642 #endif  /* NETGRAPH_DEBUG */ /*----------------------------------------------*/
643 };
644 
645 #define NGQF_TYPE       0x03            /* MASK of content definition */
646 #define NGQF_MESG       0x00            /* the queue element is a message */
647 #define NGQF_DATA       0x01            /* the queue element is data */
648 #define NGQF_FN         0x02            /* the queue element is a function */
649 #define NGQF_FN2        0x03            /* the queue element is a new function */
650 
651 #define NGQF_RW         0x04            /* MASK for wanted queue mode */
652 #define NGQF_READER     0x04            /* wants to be a reader */
653 #define NGQF_WRITER     0x00            /* wants to be a writer */
654 
655 #define NGQF_QMODE      0x08            /* MASK for how it was queued */
656 #define NGQF_QREADER    0x08            /* was queued as a reader */
657 #define NGQF_QWRITER    0x00            /* was queued as a writer */
658 
659 /*
660  * Get the mbuf (etc) out of an item.
661  * Sets the value in the item to NULL in case we need to call NG_FREE_ITEM()
662  * with it, (to avoid freeing the things twice).
663  * If you don't want to zero out the item then realise that the
664  * item still owns it.
665  * Retaddr is different. There are no references on that. It's just a number.
666  * The debug versions must be either all used everywhere or not at all.
667  */
668 
669 #define _NGI_M(i) ((i)->body.da_m)
670 #define _NGI_MSG(i) ((i)->body.msg.msg_msg)
671 #define _NGI_RETADDR(i) ((i)->body.msg.msg_retaddr)
672 #define _NGI_FN(i) ((i)->body.fn.fn_fn.fn_fn)
673 #define _NGI_FN2(i) ((i)->body.fn.fn_fn.fn_fn2)
674 #define _NGI_ARG1(i) ((i)->body.fn.fn_arg1)
675 #define _NGI_ARG2(i) ((i)->body.fn.fn_arg2)
676 #define _NGI_NODE(i) ((i)->el_dest)
677 #define _NGI_HOOK(i) ((i)->el_hook)
678 #define _NGI_SET_HOOK(i,h) do { _NGI_HOOK(i) = h; h = NULL;} while (0)
679 #define _NGI_CLR_HOOK(i)   do {                                         \
680                 hook_p _hook = _NGI_HOOK(i);                            \
681                 if (_hook) {                                            \
682                         _NG_HOOK_UNREF(_hook);                          \
683                         _NGI_HOOK(i) = NULL;                            \
684                 }                                                       \
685         } while (0)
686 #define _NGI_SET_NODE(i,n) do { _NGI_NODE(i) = n; n = NULL;} while (0)
687 #define _NGI_CLR_NODE(i)   do {                                         \
688                 node_p _node = _NGI_NODE(i);                            \
689                 if (_node) {                                            \
690                         _NG_NODE_UNREF(_node);                          \
691                         _NGI_NODE(i) = NULL;                            \
692                 }                                                       \
693         } while (0)
694 
695 #ifdef NETGRAPH_DEBUG /*----------------------------------------------*/
696 void                            dumpitem(item_p item, char *file, int line);
697 static __inline void            _ngi_check(item_p item, char *file, int line) ;
698 static __inline struct mbuf **  _ngi_m(item_p item, char *file, int line) ;
699 static __inline ng_ID_t *       _ngi_retaddr(item_p item, char *file, int line);
700 static __inline struct ng_mesg ** _ngi_msg(item_p item, char *file, int line) ;
701 static __inline ng_item_fn **   _ngi_fn(item_p item, char *file, int line) ;
702 static __inline ng_item_fn2 **  _ngi_fn2(item_p item, char *file, int line) ;
703 static __inline void **         _ngi_arg1(item_p item, char *file, int line) ;
704 static __inline int *           _ngi_arg2(item_p item, char *file, int line) ;
705 static __inline node_p          _ngi_node(item_p item, char *file, int line);
706 static __inline hook_p          _ngi_hook(item_p item, char *file, int line);
707 
708 static __inline void
709 _ngi_check(item_p item, char *file, int line)
710 {
711         (item)->lastline = line;
712         (item)->lastfile = file;
713 }
714 
715 static __inline struct mbuf **
716 _ngi_m(item_p item, char *file, int line)
717 {
718         _ngi_check(item, file, line);
719         return (&_NGI_M(item));
720 }
721 
722 static __inline struct ng_mesg **
723 _ngi_msg(item_p item, char *file, int line)
724 {
725         _ngi_check(item, file, line);
726         return (&_NGI_MSG(item));
727 }
728 
729 static __inline ng_ID_t *
730 _ngi_retaddr(item_p item, char *file, int line)
731 {
732         _ngi_check(item, file, line);
733         return (&_NGI_RETADDR(item));
734 }
735 
736 static __inline ng_item_fn **
737 _ngi_fn(item_p item, char *file, int line)
738 {
739         _ngi_check(item, file, line);
740         return (&_NGI_FN(item));
741 }
742 
743 static __inline ng_item_fn2 **
744 _ngi_fn2(item_p item, char *file, int line)
745 {
746         _ngi_check(item, file, line);
747         return (&_NGI_FN2(item));
748 }
749 
750 static __inline void **
751 _ngi_arg1(item_p item, char *file, int line)
752 {
753         _ngi_check(item, file, line);
754         return (&_NGI_ARG1(item));
755 }
756 
757 static __inline int *
758 _ngi_arg2(item_p item, char *file, int line)
759 {
760         _ngi_check(item, file, line);
761         return (&_NGI_ARG2(item));
762 }
763 
764 static __inline node_p
765 _ngi_node(item_p item, char *file, int line)
766 {
767         _ngi_check(item, file, line);
768         return (_NGI_NODE(item));
769 }
770 
771 static __inline hook_p
772 _ngi_hook(item_p item, char *file, int line)
773 {
774         _ngi_check(item, file, line);
775         return (_NGI_HOOK(item));
776 }
777 
778 #define NGI_M(i)        (*_ngi_m(i, _NN_))
779 #define NGI_MSG(i)      (*_ngi_msg(i, _NN_))
780 #define NGI_RETADDR(i)  (*_ngi_retaddr(i, _NN_))
781 #define NGI_FN(i)       (*_ngi_fn(i, _NN_))
782 #define NGI_FN2(i)      (*_ngi_fn2(i, _NN_))
783 #define NGI_ARG1(i)     (*_ngi_arg1(i, _NN_))
784 #define NGI_ARG2(i)     (*_ngi_arg2(i, _NN_))
785 #define NGI_HOOK(i)     _ngi_hook(i, _NN_)
786 #define NGI_NODE(i)     _ngi_node(i, _NN_)
787 #define NGI_SET_HOOK(i,h)                                               \
788         do { _ngi_check(i, _NN_); _NGI_SET_HOOK(i, h); } while (0)
789 #define NGI_CLR_HOOK(i)                                                 \
790         do { _ngi_check(i, _NN_); _NGI_CLR_HOOK(i); } while (0)
791 #define NGI_SET_NODE(i,n)                                               \
792         do { _ngi_check(i, _NN_); _NGI_SET_NODE(i, n); } while (0)
793 #define NGI_CLR_NODE(i)                                                 \
794         do { _ngi_check(i, _NN_); _NGI_CLR_NODE(i); } while (0)
795 
796 #define NG_FREE_ITEM(item)                                              \
797         do {                                                            \
798                 _ngi_check(item, _NN_);                                 \
799                 ng_free_item((item));                                   \
800         } while (0)
801 
802 #define SAVE_LINE(item)                                                 \
803         do {                                                            \
804                 (item)->lastline = __LINE__;                            \
805                 (item)->lastfile = __FILE__;                            \
806         } while (0)
807 
808 #else   /* NETGRAPH_DEBUG */ /*----------------------------------------------*/
809 
810 #define NGI_M(i)        _NGI_M(i)
811 #define NGI_MSG(i)      _NGI_MSG(i)
812 #define NGI_RETADDR(i)  _NGI_RETADDR(i)
813 #