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