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