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