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