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