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