| Commit | Line | Data |
|---|---|---|
| b06ebda0 MD |
1 | /* |
| 2 | * ng_base.c | |
| 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 | * Authors: Julian Elischer <julian@freebsd.org> | |
| 39 | * Archie Cobbs <archie@freebsd.org> | |
| 40 | * | |
| 41 | * $FreeBSD: src/sys/netgraph/ng_base.c,v 1.159 2008/04/19 05:30:49 mav Exp $ | |
| 95af0087 | 42 | * $DragonFly: src/sys/netgraph7/ng_base.c,v 1.4 2008/09/24 14:26:39 sephe Exp $ |
| b06ebda0 MD |
43 | * $Whistle: ng_base.c,v 1.39 1999/01/28 23:54:53 julian Exp $ |
| 44 | */ | |
| 45 | ||
| 46 | /* | |
| 47 | * This file implements the base netgraph code. | |
| 48 | */ | |
| 49 | ||
| 50 | #include <sys/param.h> | |
| 51 | #include <sys/systm.h> | |
| 52 | #include <sys/ctype.h> | |
| 53 | #include <sys/errno.h> | |
| 5a975a3d | 54 | /*#include <sys/kdb.h>*/ |
| b06ebda0 MD |
55 | #include <sys/kernel.h> |
| 56 | #include <sys/ktr.h> | |
| 57 | #include <sys/limits.h> | |
| 58 | #include <sys/malloc.h> | |
| 59 | #include <sys/mbuf.h> | |
| 60 | #include <sys/queue.h> | |
| 61 | #include <sys/sysctl.h> | |
| 62 | #include <sys/syslog.h> | |
| 63 | #include <sys/refcount.h> | |
| 64 | #include <sys/proc.h> | |
| 65 | #include <machine/cpu.h> | |
| 66 | ||
| 67 | #include <net/netisr.h> | |
| 68 | ||
| 5a975a3d MD |
69 | #include "ng_message.h" |
| 70 | #include "netgraph.h" | |
| 71 | #include "ng_parse.h" | |
| b06ebda0 MD |
72 | |
| 73 | MODULE_VERSION(netgraph, NG_ABI_VERSION); | |
| 74 | ||
| 75 | /* Mutex to protect topology events. */ | |
| 76 | static struct mtx ng_topo_mtx; | |
| 77 | ||
| 78 | #ifdef NETGRAPH_DEBUG | |
| 79 | static struct mtx ng_nodelist_mtx; /* protects global node/hook lists */ | |
| 80 | static struct mtx ngq_mtx; /* protects the queue item list */ | |
| 81 | ||
| 82 | static SLIST_HEAD(, ng_node) ng_allnodes; | |
| 83 | static LIST_HEAD(, ng_node) ng_freenodes; /* in debug, we never free() them */ | |
| 84 | static SLIST_HEAD(, ng_hook) ng_allhooks; | |
| 85 | static LIST_HEAD(, ng_hook) ng_freehooks; /* in debug, we never free() them */ | |
| 86 | ||
| 87 | static void ng_dumpitems(void); | |
| 88 | static void ng_dumpnodes(void); | |
| 89 | static void ng_dumphooks(void); | |
| 90 | ||
| 91 | #endif /* NETGRAPH_DEBUG */ | |
| 92 | /* | |
| 93 | * DEAD versions of the structures. | |
| 94 | * In order to avoid races, it is sometimes neccesary to point | |
| 95 | * at SOMETHING even though theoretically, the current entity is | |
| 96 | * INVALID. Use these to avoid these races. | |
| 97 | */ | |
| 98 | struct ng_type ng_deadtype = { | |
| 99 | NG_ABI_VERSION, | |
| 100 | "dead", | |
| 101 | NULL, /* modevent */ | |
| 102 | NULL, /* constructor */ | |
| 103 | NULL, /* rcvmsg */ | |
| 104 | NULL, /* shutdown */ | |
| 105 | NULL, /* newhook */ | |
| 106 | NULL, /* findhook */ | |
| 107 | NULL, /* connect */ | |
| 108 | NULL, /* rcvdata */ | |
| 109 | NULL, /* disconnect */ | |
| 110 | NULL, /* cmdlist */ | |
| 111 | }; | |
| 112 | ||
| 113 | struct ng_node ng_deadnode = { | |
| 114 | "dead", | |
| 115 | &ng_deadtype, | |
| 116 | NGF_INVALID, | |
| 117 | 0, /* numhooks */ | |
| 118 | NULL, /* private */ | |
| 119 | 0, /* ID */ | |
| 120 | LIST_HEAD_INITIALIZER(ng_deadnode.hooks), | |
| 121 | {}, /* all_nodes list entry */ | |
| 122 | {}, /* id hashtable list entry */ | |
| 123 | { 0, | |
| 124 | 0, | |
| 125 | {}, /* should never use! (should hang) */ | |
| 126 | {}, /* workqueue entry */ | |
| 127 | STAILQ_HEAD_INITIALIZER(ng_deadnode.nd_input_queue.queue), | |
| 128 | }, | |
| 129 | 1, /* refs */ | |
| 130 | #ifdef NETGRAPH_DEBUG | |
| 131 | ND_MAGIC, | |
| 132 | __FILE__, | |
| 133 | __LINE__, | |
| 134 | {NULL} | |
| 135 | #endif /* NETGRAPH_DEBUG */ | |
| 136 | }; | |
| 137 | ||
| 138 | struct ng_hook ng_deadhook = { | |
| 139 | "dead", | |
| 140 | NULL, /* private */ | |
| 141 | HK_INVALID | HK_DEAD, | |
| 142 | 0, /* undefined data link type */ | |
| 143 | &ng_deadhook, /* Peer is self */ | |
| 144 | &ng_deadnode, /* attached to deadnode */ | |
| 145 | {}, /* hooks list */ | |
| 146 | NULL, /* override rcvmsg() */ | |
| 147 | NULL, /* override rcvdata() */ | |
| 148 | 1, /* refs always >= 1 */ | |
| 149 | #ifdef NETGRAPH_DEBUG | |
| 150 | HK_MAGIC, | |
| 151 | __FILE__, | |
| 152 | __LINE__, | |
| 153 | {NULL} | |
| 154 | #endif /* NETGRAPH_DEBUG */ | |
| 155 | }; | |
| 156 | ||
| 157 | /* | |
| 158 | * END DEAD STRUCTURES | |
| 159 | */ | |
| 160 | /* List nodes with unallocated work */ | |
| 161 | static STAILQ_HEAD(, ng_node) ng_worklist = STAILQ_HEAD_INITIALIZER(ng_worklist); | |
| 162 | static struct mtx ng_worklist_mtx; /* MUST LOCK NODE FIRST */ | |
| 163 | ||
| 164 | /* List of installed types */ | |
| 165 | static LIST_HEAD(, ng_type) ng_typelist; | |
| 166 | static struct mtx ng_typelist_mtx; | |
| 167 | ||
| 168 | /* Hash related definitions */ | |
| 169 | /* XXX Don't need to initialise them because it's a LIST */ | |
| 170 | #define NG_ID_HASH_SIZE 128 /* most systems wont need even this many */ | |
| 171 | static LIST_HEAD(, ng_node) ng_ID_hash[NG_ID_HASH_SIZE]; | |
| 172 | static struct mtx ng_idhash_mtx; | |
| 173 | /* Method to find a node.. used twice so do it here */ | |
| 174 | #define NG_IDHASH_FN(ID) ((ID) % (NG_ID_HASH_SIZE)) | |
| 175 | #define NG_IDHASH_FIND(ID, node) \ | |
| 176 | do { \ | |
| 177 | mtx_assert(&ng_idhash_mtx, MA_OWNED); \ | |
| 178 | LIST_FOREACH(node, &ng_ID_hash[NG_IDHASH_FN(ID)], \ | |
| 179 | nd_idnodes) { \ | |
| 180 | if (NG_NODE_IS_VALID(node) \ | |
| 181 | && (NG_NODE_ID(node) == ID)) { \ | |
| 182 | break; \ | |
| 183 | } \ | |
| 184 | } \ | |
| 185 | } while (0) | |
| 186 | ||
| 187 | #define NG_NAME_HASH_SIZE 128 /* most systems wont need even this many */ | |
| 188 | static LIST_HEAD(, ng_node) ng_name_hash[NG_NAME_HASH_SIZE]; | |
| 189 | static struct mtx ng_namehash_mtx; | |
| 190 | #define NG_NAMEHASH(NAME, HASH) \ | |
| 191 | do { \ | |
| 192 | u_char h = 0; \ | |
| 193 | const u_char *c; \ | |
| 194 | for (c = (const u_char*)(NAME); *c; c++)\ | |
| 195 | h += *c; \ | |
| 196 | (HASH) = h % (NG_NAME_HASH_SIZE); \ | |
| 197 | } while (0) | |
| 198 | ||
| 199 | ||
| 200 | /* Internal functions */ | |
| 201 | static int ng_add_hook(node_p node, const char *name, hook_p * hookp); | |
| 202 | static int ng_generic_msg(node_p here, item_p item, hook_p lasthook); | |
| 203 | static ng_ID_t ng_decodeidname(const char *name); | |
| 204 | static int ngb_mod_event(module_t mod, int event, void *data); | |
| 205 | static void ng_worklist_add(node_p node); | |
| 206 | static void ngintr(void); | |
| 207 | static int ng_apply_item(node_p node, item_p item, int rw); | |
| 208 | static void ng_flush_input_queue(node_p node); | |
| 209 | static node_p ng_ID2noderef(ng_ID_t ID); | |
| 210 | static int ng_con_nodes(item_p item, node_p node, const char *name, | |
| 211 | node_p node2, const char *name2); | |
| 212 | static int ng_con_part2(node_p node, item_p item, hook_p hook); | |
| 213 | static int ng_con_part3(node_p node, item_p item, hook_p hook); | |
| 214 | static int ng_mkpeer(node_p node, const char *name, | |
| 215 | const char *name2, char *type); | |
| 216 | ||
| 217 | /* Imported, these used to be externally visible, some may go back. */ | |
| 218 | void ng_destroy_hook(hook_p hook); | |
| 219 | node_p ng_name2noderef(node_p node, const char *name); | |
| 220 | int ng_path2noderef(node_p here, const char *path, | |
| 221 | node_p *dest, hook_p *lasthook); | |
| 222 | int ng_make_node(const char *type, node_p *nodepp); | |
| 223 | int ng_path_parse(char *addr, char **node, char **path, char **hook); | |
| 224 | void ng_rmnode(node_p node, hook_p dummy1, void *dummy2, int dummy3); | |
| 225 | void ng_unname(node_p node); | |
| 226 | ||
| 227 | ||
| 228 | /* Our own netgraph malloc type */ | |
| 229 | MALLOC_DEFINE(M_NETGRAPH, "netgraph", "netgraph structures and ctrl messages"); | |
| 230 | MALLOC_DEFINE(M_NETGRAPH_HOOK, "netgraph_hook", "netgraph hook structures"); | |
| 231 | MALLOC_DEFINE(M_NETGRAPH_NODE, "netgraph_node", "netgraph node structures"); | |
| 232 | MALLOC_DEFINE(M_NETGRAPH_ITEM, "netgraph_item", "netgraph item structures"); | |
| 233 | MALLOC_DEFINE(M_NETGRAPH_MSG, "netgraph_msg", "netgraph name storage"); | |
| 234 | ||
| 235 | /* Should not be visible outside this file */ | |
| 236 | ||
| 237 | #define _NG_ALLOC_HOOK(hook) \ | |
| fc025606 SW |
238 | hook = kmalloc(sizeof(*hook), M_NETGRAPH_HOOK, \ |
| 239 | M_WAITOK | M_NULLOK | M_ZERO) | |
| b06ebda0 | 240 | #define _NG_ALLOC_NODE(node) \ |
| fc025606 SW |
241 | node = kmalloc(sizeof(*node), M_NETGRAPH_NODE, \ |
| 242 | M_WAITOK | M_NULLOK | M_ZERO) | |
| b06ebda0 MD |
243 | |
| 244 | #define NG_QUEUE_LOCK_INIT(n) \ | |
| 245 | mtx_init(&(n)->q_mtx, "ng_node", NULL, MTX_DEF) | |
| 246 | #define NG_QUEUE_LOCK(n) \ | |
| 247 | mtx_lock(&(n)->q_mtx) | |
| 248 | #define NG_QUEUE_UNLOCK(n) \ | |
| 249 | mtx_unlock(&(n)->q_mtx) | |
| 250 | #define NG_WORKLIST_LOCK_INIT() \ | |
| 251 | mtx_init(&ng_worklist_mtx, "ng_worklist", NULL, MTX_DEF) | |
| 252 | #define NG_WORKLIST_LOCK() \ | |
| 253 | mtx_lock(&ng_worklist_mtx) | |
| 254 | #define NG_WORKLIST_UNLOCK() \ | |
| 255 | mtx_unlock(&ng_worklist_mtx) | |
| 256 | ||
| 257 | #ifdef NETGRAPH_DEBUG /*----------------------------------------------*/ | |
| 258 | /* | |
| 259 | * In debug mode: | |
| 260 | * In an attempt to help track reference count screwups | |
| 261 | * we do not free objects back to the malloc system, but keep them | |
| 262 | * in a local cache where we can examine them and keep information safely | |
| 263 | * after they have been freed. | |
| 264 | * We use this scheme for nodes and hooks, and to some extent for items. | |
| 265 | */ | |
| 266 | static __inline hook_p | |
| 267 | ng_alloc_hook(void) | |
| 268 | { | |
| 269 | hook_p hook; | |
| 270 | SLIST_ENTRY(ng_hook) temp; | |
| 271 | mtx_lock(&ng_nodelist_mtx); | |
| 272 | hook = LIST_FIRST(&ng_freehooks); | |
| 273 | if (hook) { | |
| 274 | LIST_REMOVE(hook, hk_hooks); | |
| 275 | bcopy(&hook->hk_all, &temp, sizeof(temp)); | |
| 276 | bzero(hook, sizeof(struct ng_hook)); | |
| 277 | bcopy(&temp, &hook->hk_all, sizeof(temp)); | |
| 278 | mtx_unlock(&ng_nodelist_mtx); | |
| 279 | hook->hk_magic = HK_MAGIC; | |
| 280 | } else { | |
| 281 | mtx_unlock(&ng_nodelist_mtx); | |
| 282 | _NG_ALLOC_HOOK(hook); | |
| 283 | if (hook) { | |
| 284 | hook->hk_magic = HK_MAGIC; | |
| 285 | mtx_lock(&ng_nodelist_mtx); | |
| 286 | SLIST_INSERT_HEAD(&ng_allhooks, hook, hk_all); | |
| 287 | mtx_unlock(&ng_nodelist_mtx); | |
| 288 | } | |
| 289 | } | |
| 290 | return (hook); | |
| 291 | } | |
| 292 | ||
| 293 | static __inline node_p | |
| 294 | ng_alloc_node(void) | |
| 295 | { | |
| 296 | node_p node; | |
| 297 | SLIST_ENTRY(ng_node) temp; | |
| 298 | mtx_lock(&ng_nodelist_mtx); | |
| 299 | node = LIST_FIRST(&ng_freenodes); | |
| 300 | if (node) { | |
| 301 | LIST_REMOVE(node, nd_nodes); | |
| 302 | bcopy(&node->nd_all, &temp, sizeof(temp)); | |
| 303 | bzero(node, sizeof(struct ng_node)); | |
| 304 | bcopy(&temp, &node->nd_all, sizeof(temp)); | |
| 305 | mtx_unlock(&ng_nodelist_mtx); | |
| 306 | node->nd_magic = ND_MAGIC; | |
| 307 | } else { | |
| 308 | mtx_unlock(&ng_nodelist_mtx); | |
| 309 | _NG_ALLOC_NODE(node); | |
| 310 | if (node) { | |
| 311 | node->nd_magic = ND_MAGIC; | |
| 312 | mtx_lock(&ng_nodelist_mtx); | |
| 313 | SLIST_INSERT_HEAD(&ng_allnodes, node, nd_all); | |
| 314 | mtx_unlock(&ng_nodelist_mtx); | |
| 315 | } | |
| 316 | } | |
| 317 | return (node); | |
| 318 | } | |
| 319 | ||
| 320 | #define NG_ALLOC_HOOK(hook) do { (hook) = ng_alloc_hook(); } while (0) | |
| 321 | #define NG_ALLOC_NODE(node) do { (node) = ng_alloc_node(); } while (0) | |
| 322 | ||
| 323 | ||
| 324 | #define NG_FREE_HOOK(hook) \ | |
| 325 | do { \ | |
| 326 | mtx_lock(&ng_nodelist_mtx); \ | |
| 327 | LIST_INSERT_HEAD(&ng_freehooks, hook, hk_hooks); \ | |
| 328 | hook->hk_magic = 0; \ | |
| 329 | mtx_unlock(&ng_nodelist_mtx); \ | |
| 330 | } while (0) | |
| 331 | ||
| 332 | #define NG_FREE_NODE(node) \ | |
| 333 | do { \ | |
| 334 | mtx_lock(&ng_nodelist_mtx); \ | |
| 335 | LIST_INSERT_HEAD(&ng_freenodes, node, nd_nodes); \ | |
| 336 | node->nd_magic = 0; \ | |
| 337 | mtx_unlock(&ng_nodelist_mtx); \ | |
| 338 | } while (0) | |
| 339 | ||
| 340 | #else /* NETGRAPH_DEBUG */ /*----------------------------------------------*/ | |
| 341 | ||
| 342 | #define NG_ALLOC_HOOK(hook) _NG_ALLOC_HOOK(hook) | |
| 343 | #define NG_ALLOC_NODE(node) _NG_ALLOC_NODE(node) | |
| 344 | ||
| fc025606 SW |
345 | #define NG_FREE_HOOK(hook) do { kfree((hook), M_NETGRAPH_HOOK); } while (0) |
| 346 | #define NG_FREE_NODE(node) do { kfree((node), M_NETGRAPH_NODE); } while (0) | |
| b06ebda0 MD |
347 | |
| 348 | #endif /* NETGRAPH_DEBUG */ /*----------------------------------------------*/ | |
| 349 | ||
| 350 | /* Set this to kdb_enter("X") to catch all errors as they occur */ | |
| 351 | #ifndef TRAP_ERROR | |
| 352 | #define TRAP_ERROR() | |
| 353 | #endif | |
| 354 | ||
| 355 | static ng_ID_t nextID = 1; | |
| 356 | ||
| 357 | #ifdef INVARIANTS | |
| 358 | #define CHECK_DATA_MBUF(m) do { \ | |
| 359 | struct mbuf *n; \ | |
| 360 | int total; \ | |
| 361 | \ | |
| 362 | M_ASSERTPKTHDR(m); \ | |
| 363 | for (total = 0, n = (m); n != NULL; n = n->m_next) { \ | |
| 364 | total += n->m_len; \ | |
| 365 | if (n->m_nextpkt != NULL) \ | |
| 366 | panic("%s: m_nextpkt", __func__); \ | |
| 367 | } \ | |
| 368 | \ | |
| 369 | if ((m)->m_pkthdr.len != total) { \ | |
| 370 | panic("%s: %d != %d", \ | |
| 371 | __func__, (m)->m_pkthdr.len, total); \ | |
| 372 | } \ | |
| 373 | } while (0) | |
| 374 | #else | |
| 375 | #define CHECK_DATA_MBUF(m) | |
| 376 | #endif | |
| 377 | ||
| 378 | #define ERROUT(x) do { error = (x); goto done; } while (0) | |
| 379 | ||
| 380 | /************************************************************************ | |
| 381 | Parse type definitions for generic messages | |
| 382 | ************************************************************************/ | |
| 383 | ||
| 384 | /* Handy structure parse type defining macro */ | |
| 385 | #define DEFINE_PARSE_STRUCT_TYPE(lo, up, args) \ | |
| 386 | static const struct ng_parse_struct_field \ | |
| 387 | ng_ ## lo ## _type_fields[] = NG_GENERIC_ ## up ## _INFO args; \ | |
| 388 | static const struct ng_parse_type ng_generic_ ## lo ## _type = { \ | |
| 389 | &ng_parse_struct_type, \ | |
| 390 | &ng_ ## lo ## _type_fields \ | |
| 391 | } | |
| 392 | ||
| 393 | DEFINE_PARSE_STRUCT_TYPE(mkpeer, MKPEER, ()); | |
| 394 | DEFINE_PARSE_STRUCT_TYPE(connect, CONNECT, ()); | |
| 395 | DEFINE_PARSE_STRUCT_TYPE(name, NAME, ()); | |
| 396 | DEFINE_PARSE_STRUCT_TYPE(rmhook, RMHOOK, ()); | |
| 397 | DEFINE_PARSE_STRUCT_TYPE(nodeinfo, NODEINFO, ()); | |
| 398 | DEFINE_PARSE_STRUCT_TYPE(typeinfo, TYPEINFO, ()); | |
| 399 | DEFINE_PARSE_STRUCT_TYPE(linkinfo, LINKINFO, (&ng_generic_nodeinfo_type)); | |
| 400 | ||
| 401 | /* Get length of an array when the length is stored as a 32 bit | |
| 402 | value immediately preceding the array -- as with struct namelist | |
| 403 | and struct typelist. */ | |
| 404 | static int | |
| 405 | ng_generic_list_getLength(const struct ng_parse_type *type, | |
| 406 | const u_char *start, const u_char *buf) | |
| 407 | { | |
| 408 | return *((const u_int32_t *)(buf - 4)); | |
| 409 | } | |
| 410 | ||
| 411 | /* Get length of the array of struct linkinfo inside a struct hooklist */ | |
| 412 | static int | |
| 413 | ng_generic_linkinfo_getLength(const struct ng_parse_type *type, | |
| 414 | const u_char *start, const u_char *buf) | |
| 415 | { | |
| 416 | const struct hooklist *hl = (const struct hooklist *)start; | |
| 417 | ||
| 418 | return hl->nodeinfo.hooks; | |
| 419 | } | |
| 420 | ||
| 421 | /* Array type for a variable length array of struct namelist */ | |
| 422 | static const struct ng_parse_array_info ng_nodeinfoarray_type_info = { | |
| 423 | &ng_generic_nodeinfo_type, | |
| 424 | &ng_generic_list_getLength | |
| 425 | }; | |
| 426 | static const struct ng_parse_type ng_generic_nodeinfoarray_type = { | |
| 427 | &ng_parse_array_type, | |
| 428 | &ng_nodeinfoarray_type_info | |
| 429 | }; | |
| 430 | ||
| 431 | /* Array type for a variable length array of struct typelist */ | |
| 432 | static const struct ng_parse_array_info ng_typeinfoarray_type_info = { | |
| 433 | &ng_generic_typeinfo_type, | |
| 434 | &ng_generic_list_getLength | |
| 435 | }; | |
| 436 | static const struct ng_parse_type ng_generic_typeinfoarray_type = { | |
| 437 | &ng_parse_array_type, | |
| 438 | &ng_typeinfoarray_type_info | |
| 439 | }; | |
| 440 | ||
| 441 | /* Array type for array of struct linkinfo in struct hooklist */ | |
| 442 | static const struct ng_parse_array_info ng_generic_linkinfo_array_type_info = { | |
| 443 | &ng_generic_linkinfo_type, | |
| 444 | &ng_generic_linkinfo_getLength | |
| 445 | }; | |
| 446 | static const struct ng_parse_type ng_generic_linkinfo_array_type = { | |
| 447 | &ng_parse_array_type, | |
| 448 | &ng_generic_linkinfo_array_type_info | |
| 449 | }; | |
| 450 | ||
| 451 | DEFINE_PARSE_STRUCT_TYPE(typelist, TYPELIST, (&ng_generic_nodeinfoarray_type)); | |
| 452 | DEFINE_PARSE_STRUCT_TYPE(hooklist, HOOKLIST, | |
| 453 | (&ng_generic_nodeinfo_type, &ng_generic_linkinfo_array_type)); | |
| 454 | DEFINE_PARSE_STRUCT_TYPE(listnodes, LISTNODES, | |
| 455 | (&ng_generic_nodeinfoarray_type)); | |
| 456 | ||
| 457 | /* List of commands and how to convert arguments to/from ASCII */ | |
| 458 | static const struct ng_cmdlist ng_generic_cmds[] = { | |
| 459 | { | |
| 460 | NGM_GENERIC_COOKIE, | |
| 461 | NGM_SHUTDOWN, | |
| 462 | "shutdown", | |
| 463 | NULL, | |
| 464 | NULL | |
| 465 | }, | |
| 466 | { | |
| 467 | NGM_GENERIC_COOKIE, | |
| 468 | NGM_MKPEER, | |
| 469 | "mkpeer", | |
| 470 | &ng_generic_mkpeer_type, | |
| 471 | NULL | |
| 472 | }, | |
| 473 | { | |
| 474 | NGM_GENERIC_COOKIE, | |
| 475 | NGM_CONNECT, | |
| 476 | "connect", | |
| 477 | &ng_generic_connect_type, | |
| 478 | NULL | |
| 479 | }, | |
| 480 | { | |
| 481 | NGM_GENERIC_COOKIE, | |
| 482 | NGM_NAME, | |
| 483 | "name", | |
| 484 | &ng_generic_name_type, | |
| 485 | NULL | |
| 486 | }, | |
| 487 | { | |
| 488 | NGM_GENERIC_COOKIE, | |
| 489 | NGM_RMHOOK, | |
| 490 | "rmhook", | |
| 491 | &ng_generic_rmhook_type, | |
| 492 | NULL | |
| 493 | }, | |
| 494 | { | |
| 495 | NGM_GENERIC_COOKIE, | |
| 496 | NGM_NODEINFO, | |
| 497 | "nodeinfo", | |
| 498 | NULL, | |
| 499 | &ng_generic_nodeinfo_type | |
| 500 | }, | |
| 501 | { | |
| 502 | NGM_GENERIC_COOKIE, | |
| 503 | NGM_LISTHOOKS, | |
| 504 | "listhooks", | |
| 505 | NULL, | |
| 506 | &ng_generic_hooklist_type | |
| 507 | }, | |
| 508 | { | |
| 509 | NGM_GENERIC_COOKIE, | |
| 510 | NGM_LISTNAMES, | |
| 511 | "listnames", | |
| 512 | NULL, | |
| 513 | &ng_generic_listnodes_type /* same as NGM_LISTNODES */ | |
| 514 | }, | |
| 515 | { | |
| 516 | NGM_GENERIC_COOKIE, | |
| 517 | NGM_LISTNODES, | |
| 518 | "listnodes", | |
| 519 | NULL, | |
| 520 | &ng_generic_listnodes_type | |
| 521 | }, | |
| 522 | { | |
| 523 | NGM_GENERIC_COOKIE, | |
| 524 | NGM_LISTTYPES, | |
| 525 | "listtypes", | |
| 526 | NULL, | |
| 527 | &ng_generic_typeinfo_type | |
| 528 | }, | |
| 529 | { | |
| 530 | NGM_GENERIC_COOKIE, | |
| 531 | NGM_TEXT_CONFIG, | |
| 532 | "textconfig", | |
| 533 | NULL, | |
| 534 | &ng_parse_string_type | |
| 535 | }, | |
| 536 | { | |
| 537 | NGM_GENERIC_COOKIE, | |
| 538 | NGM_TEXT_STATUS, | |
| 539 | "textstatus", | |
| 540 | NULL, | |
| 541 | &ng_parse_string_type | |
| 542 | }, | |
| 543 | { | |
| 544 | NGM_GENERIC_COOKIE, | |
| 545 | NGM_ASCII2BINARY, | |
| 546 | "ascii2binary", | |
| 547 | &ng_parse_ng_mesg_type, | |
| 548 | &ng_parse_ng_mesg_type | |
| 549 | }, | |
| 550 | { | |
| 551 | NGM_GENERIC_COOKIE, | |
| 552 | NGM_BINARY2ASCII, | |
| 553 | "binary2ascii", | |
| 554 | &ng_parse_ng_mesg_type, | |
| 555 | &ng_parse_ng_mesg_type | |
| 556 | }, | |
| 557 | { 0 } | |
| 558 | }; | |
| 559 | ||
| 560 | /************************************************************************ | |
| 561 | Node routines | |
| 562 | ************************************************************************/ | |
| 563 | ||
| 564 | /* | |
| 565 | * Instantiate a node of the requested type | |
| 566 | */ | |
| 567 | int | |
| 568 | ng_make_node(const char *typename, node_p *nodepp) | |
| 569 | { | |
| 570 | struct ng_type *type; | |
| 571 | int error; | |
| 572 | ||
| 573 | /* Check that the type makes sense */ | |
| 574 | if (typename == NULL) { | |
| 575 | TRAP_ERROR(); | |
| 576 | return (EINVAL); | |
| 577 | } | |
| 578 | ||
| 579 | /* Locate the node type. If we fail we return. Do not try to load | |
| 580 | * module. | |
| 581 | */ | |
| 582 | if ((type = ng_findtype(typename)) == NULL) | |
| 583 | return (ENXIO); | |
| 584 | ||
| 585 | /* | |
| 586 | * If we have a constructor, then make the node and | |
| 587 | * call the constructor to do type specific initialisation. | |
| 588 | */ | |
| 589 | if (type->constructor != NULL) { | |
| 590 | if ((error = ng_make_node_common(type, nodepp)) == 0) { | |
| 591 | if ((error = ((*type->constructor)(*nodepp)) != 0)) { | |
| 592 | NG_NODE_UNREF(*nodepp); | |
| 593 | } | |
| 594 | } | |
| 595 | } else { | |
| 596 | /* | |
| 597 | * Node has no constructor. We cannot ask for one | |
| 598 | * to be made. It must be brought into existence by | |
| 599 | * some external agency. The external agency should | |
| 600 | * call ng_make_node_common() directly to get the | |
| 601 | * netgraph part initialised. | |
| 602 | */ | |
| 603 | TRAP_ERROR(); | |
| 604 | error = EINVAL; | |
| 605 | } | |
| 606 | return (error); | |
| 607 | } | |
| 608 | ||
| 609 | /* | |
| 610 | * Generic node creation. Called by node initialisation for externally | |
| 611 | * instantiated nodes (e.g. hardware, sockets, etc ). | |
| 612 | * The returned node has a reference count of 1. | |
| 613 | */ | |
| 614 | int | |
| 615 | ng_make_node_common(struct ng_type *type, node_p *nodepp) | |
| 616 | { | |
| 617 | node_p node; | |
| 618 | ||
| 619 | /* Require the node type to have been already installed */ | |
| 620 | if (ng_findtype(type->name) == NULL) { | |
| 621 | TRAP_ERROR(); | |
| 622 | return (EINVAL); | |
| 623 | } | |
| 624 | ||
| 625 | /* Make a node and try attach it to the type */ | |
| 626 | NG_ALLOC_NODE(node); | |
| 627 | if (node == NULL) { | |
| 628 | TRAP_ERROR(); | |
| 629 | return (ENOMEM); | |
| 630 | } | |
| 631 | node->nd_type = type; | |
| 632 | NG_NODE_REF(node); /* note reference */ | |
| 633 | type->refs++; | |
| 634 | ||
| 635 | NG_QUEUE_LOCK_INIT(&node->nd_input_queue); | |
| 636 | STAILQ_INIT(&node->nd_input_queue.queue); | |
| 637 | node->nd_input_queue.q_flags = 0; | |
| 638 | ||
| 639 | /* Initialize hook list for new node */ | |
| 640 | LIST_INIT(&node->nd_hooks); | |
| 641 | ||
| 642 | /* Link us into the name hash. */ | |
| 643 | mtx_lock(&ng_namehash_mtx); | |
| 644 | LIST_INSERT_HEAD(&ng_name_hash[0], node, nd_nodes); | |
| 645 | mtx_unlock(&ng_namehash_mtx); | |
| 646 | ||
| 647 | /* get an ID and put us in the hash chain */ | |
| 648 | mtx_lock(&ng_idhash_mtx); | |
| 649 | for (;;) { /* wrap protection, even if silly */ | |
| 650 | node_p node2 = NULL; | |
| 651 | node->nd_ID = nextID++; /* 137/second for 1 year before wrap */ | |
| 652 | ||
| 653 | /* Is there a problem with the new number? */ | |
| 654 | NG_IDHASH_FIND(node->nd_ID, node2); /* already taken? */ | |
| 655 | if ((node->nd_ID != 0) && (node2 == NULL)) { | |
| 656 | break; | |
| 657 | } | |
| 658 | } | |
| 659 | LIST_INSERT_HEAD(&ng_ID_hash[NG_IDHASH_FN(node->nd_ID)], | |
| 660 | node, nd_idnodes); | |
| 661 | mtx_unlock(&ng_idhash_mtx); | |
| 662 | ||
| 663 | /* Done */ | |
| 664 | *nodepp = node; | |
| 665 | return (0); | |
| 666 | } | |
| 667 | ||
| 668 | /* | |
| 669 | * Forceably start the shutdown process on a node. Either call | |
| 670 | * its shutdown method, or do the default shutdown if there is | |
| 671 | * no type-specific method. | |
| 672 | * | |
| 673 | * We can only be called from a shutdown message, so we know we have | |
| 674 | * a writer lock, and therefore exclusive access. It also means | |
| 675 | * that we should not be on the work queue, but we check anyhow. | |
| 676 | * | |
| 677 | * Persistent node types must have a type-specific method which | |
| 678 | * allocates a new node in which case, this one is irretrievably going away, | |
| 679 | * or cleans up anything it needs, and just makes the node valid again, | |
| 680 | * in which case we allow the node to survive. | |
| 681 | * | |
| 682 | * XXX We need to think of how to tell a persistent node that we | |
| 683 | * REALLY need to go away because the hardware has gone or we | |
| 684 | * are rebooting.... etc. | |
| 685 | */ | |
| 686 | void | |
| 687 | ng_rmnode(node_p node, hook_p dummy1, void *dummy2, int dummy3) | |
| 688 | { | |
| 689 | hook_p hook; | |
| 690 | ||
| 691 | /* Check if it's already shutting down */ | |
| 692 | if ((node->nd_flags & NGF_CLOSING) != 0) | |
| 693 | return; | |
| 694 | ||
| 695 | if (node == &ng_deadnode) { | |
| 696 | printf ("shutdown called on deadnode\n"); | |
| 697 | return; | |
| 698 | } | |
| 699 | ||
| 700 | /* Add an extra reference so it doesn't go away during this */ | |
| 701 | NG_NODE_REF(node); | |
| 702 | ||
| 703 | /* | |
| 704 | * Mark it invalid so any newcomers know not to try use it | |
| 705 | * Also add our own mark so we can't recurse | |
| 706 | * note that NGF_INVALID does not do this as it's also set during | |
| 707 | * creation | |
| 708 | */ | |
| 709 | node->nd_flags |= NGF_INVALID|NGF_CLOSING; | |
| 710 | ||
| 711 | /* If node has its pre-shutdown method, then call it first*/ | |
| 712 | if (node->nd_type && node->nd_type->close) | |
| 713 | (*node->nd_type->close)(node); | |
| 714 | ||
| 715 | /* Notify all remaining connected nodes to disconnect */ | |
| 716 | while ((hook = LIST_FIRST(&node->nd_hooks)) != NULL) | |
| 717 | ng_destroy_hook(hook); | |
| 718 | ||
| 719 | /* | |
| 720 | * Drain the input queue forceably. | |
| 721 | * it has no hooks so what's it going to do, bleed on someone? | |
| 722 | * Theoretically we came here from a queue entry that was added | |
| 723 | * Just before the queue was closed, so it should be empty anyway. | |
| 724 | * Also removes us from worklist if needed. | |
| 725 | */ | |
| 726 | ng_flush_input_queue(node); | |
| 727 | ||
| 728 | /* Ask the type if it has anything to do in this case */ | |
| 729 | if (node->nd_type && node->nd_type->shutdown) { | |
| 730 | (*node->nd_type->shutdown)(node); | |
| 731 | if (NG_NODE_IS_VALID(node)) { | |
| 732 | /* | |
| 733 | * Well, blow me down if the node code hasn't declared | |
| 734 | * that it doesn't want to die. | |
| 735 | * Presumably it is a persistant node. | |
| 736 | * If we REALLY want it to go away, | |
| 737 | * e.g. hardware going away, | |
| 738 | * Our caller should set NGF_REALLY_DIE in nd_flags. | |
| 739 | */ | |
| 740 | node->nd_flags &= ~(NGF_INVALID|NGF_CLOSING); | |
| 741 | NG_NODE_UNREF(node); /* Assume they still have theirs */ | |
| 742 | return; | |
| 743 | } | |
| 744 | } else { /* do the default thing */ | |
| 745 | NG_NODE_UNREF(node); | |
| 746 | } | |
| 747 | ||
| 748 | ng_unname(node); /* basically a NOP these days */ | |
| 749 | ||
| 750 | /* | |
| 751 | * Remove extra reference, possibly the last | |
| 752 | * Possible other holders of references may include | |
| 753 | * timeout callouts, but theoretically the node's supposed to | |
| 754 | * have cancelled them. Possibly hardware dependencies may | |
| 755 | * force a driver to 'linger' with a reference. | |
| 756 | */ | |
| 757 | NG_NODE_UNREF(node); | |
| 758 | } | |
| 759 | ||
| 760 | /* | |
| 761 | * Remove a reference to the node, possibly the last. | |
| 762 | * deadnode always acts as it it were the last. | |
| 763 | */ | |
| 764 | int | |
| 765 | ng_unref_node(node_p node) | |
| 766 | { | |
| 767 | int v; | |
| 768 | ||
| 769 | if (node == &ng_deadnode) { | |
| 770 | return (0); | |
| 771 | } | |
| 772 | ||
| 773 | v = atomic_fetchadd_int(&node->nd_refs, -1); | |
| 774 | ||
| 775 | if (v == 1) { /* we were the last */ | |
| 776 | ||
| 777 | mtx_lock(&ng_namehash_mtx); | |
| 778 | node->nd_type->refs--; /* XXX maybe should get types lock? */ | |
| 779 | LIST_REMOVE(node, nd_nodes); | |
| 780 | mtx_unlock(&ng_namehash_mtx); | |
| 781 | ||
| 782 | mtx_lock(&ng_idhash_mtx); | |
| 783 | LIST_REMOVE(node, nd_idnodes); | |
| 784 | mtx_unlock(&ng_idhash_mtx); | |
| 785 | ||
| 786 | mtx_destroy(&node->nd_input_queue.q_mtx); | |
| 787 | NG_FREE_NODE(node); | |
| 788 | } | |
| 789 | return (v - 1); | |
| 790 | } | |
| 791 | ||
| 792 | /************************************************************************ | |
| 793 | Node ID handling | |
| 794 | ************************************************************************/ | |
| 795 | static node_p | |
| 796 | ng_ID2noderef(ng_ID_t ID) | |
| 797 | { | |
| 798 | node_p node; | |
| 799 | mtx_lock(&ng_idhash_mtx); | |
| 800 | NG_IDHASH_FIND(ID, node); | |
| 801 | if(node) | |
| 802 | NG_NODE_REF(node); | |
| 803 | mtx_unlock(&ng_idhash_mtx); | |
| 804 | return(node); | |
| 805 | } | |
| 806 | ||
| 807 | ng_ID_t | |
| 808 | ng_node2ID(node_p node) | |
| 809 | { | |
| 810 | return (node ? NG_NODE_ID(node) : 0); | |
| 811 | } | |
| 812 | ||
| 813 | /************************************************************************ | |
| 814 | Node name handling | |
| 815 | ************************************************************************/ | |
| 816 | ||
| 817 | /* | |
| 818 | * Assign a node a name. Once assigned, the name cannot be changed. | |
| 819 | */ | |
| 820 | int | |
| 821 | ng_name_node(node_p node, const char *name) | |
| 822 | { | |
| 823 | int i, hash; | |
| 824 | node_p node2; | |
| 825 | ||
| 826 | /* Check the name is valid */ | |
| 827 | for (i = 0; i < NG_NODESIZ; i++) { | |
| 828 | if (name[i] == '\0' || name[i] == '.' || name[i] == ':') | |
| 829 | break; | |
| 830 | } | |
| 831 | if (i == 0 || name[i] != '\0') { | |
| 832 | TRAP_ERROR(); | |
| 833 | return (EINVAL); | |
| 834 | } | |
| 835 | if (ng_decodeidname(name) != 0) { /* valid IDs not allowed here */ | |
| 836 | TRAP_ERROR(); | |
| 837 | return (EINVAL); | |
| 838 | } | |
| 839 | ||
| 840 | /* Check the name isn't already being used */ | |
| 841 | if ((node2 = ng_name2noderef(node, name)) != NULL) { | |
| 842 | NG_NODE_UNREF(node2); | |
| 843 | TRAP_ERROR(); | |
| 844 | return (EADDRINUSE); | |
| 845 | } | |
| 846 | ||
| 847 | /* copy it */ | |
| 848 | strlcpy(NG_NODE_NAME(node), name, NG_NODESIZ); | |
| 849 | ||
| 850 | /* Update name hash. */ | |
| 851 | NG_NAMEHASH(name, hash); | |
| 852 | mtx_lock(&ng_namehash_mtx); | |
| 853 | LIST_REMOVE(node, nd_nodes); | |
| 854 | LIST_INSERT_HEAD(&ng_name_hash[hash], node, nd_nodes); | |
| 855 | mtx_unlock(&ng_namehash_mtx); | |
| 856 | ||
| 857 | return (0); | |
| 858 | } | |
| 859 | ||
| 860 | /* | |
| 861 | * Find a node by absolute name. The name should NOT end with ':' | |
| 862 | * The name "." means "this node" and "[xxx]" means "the node | |
| 863 | * with ID (ie, at address) xxx". | |
| 864 | * | |
| 865 | * Returns the node if found, else NULL. | |
| 866 | * Eventually should add something faster than a sequential search. | |
| 867 | * Note it acquires a reference on the node so you can be sure it's still | |
| 868 | * there. | |
| 869 | */ | |
| 870 | node_p | |
| 871 | ng_name2noderef(node_p here, const char *name) | |
| 872 | { | |
| 873 | node_p node; | |
| 874 | ng_ID_t temp; | |
| 875 | int hash; | |
| 876 | ||
| 877 | /* "." means "this node" */ | |
| 878 | if (strcmp(name, ".") == 0) { | |
| 879 | NG_NODE_REF(here); | |
| 880 | return(here); | |
| 881 | } | |
| 882 | ||
| 883 | /* Check for name-by-ID */ | |
| 884 | if ((temp = ng_decodeidname(name)) != 0) { | |
| 885 | return (ng_ID2noderef(temp)); | |
| 886 | } | |
| 887 | ||
| 888 | /* Find node by name */ | |
| 889 | NG_NAMEHASH(name, hash); | |
| 890 | mtx_lock(&ng_namehash_mtx); | |
| 891 | LIST_FOREACH(node, &ng_name_hash[hash], nd_nodes) { | |
| 892 | if (NG_NODE_IS_VALID(node) && | |
| 893 | (strcmp(NG_NODE_NAME(node), name) == 0)) { | |
| 894 | break; | |
| 895 | } | |
| 896 | } | |
| 897 | if (node) | |
| 898 | NG_NODE_REF(node); | |
| 899 | mtx_unlock(&ng_namehash_mtx); | |
| 900 | return (node); | |
| 901 | } | |
| 902 | ||
| 903 | /* | |
| 904 | * Decode an ID name, eg. "[f03034de]". Returns 0 if the | |
| 905 | * string is not valid, otherwise returns the value. | |
| 906 | */ | |
| 907 | static ng_ID_t | |
| 908 | ng_decodeidname(const char *name) | |
| 909 | { | |
| 910 | const int len = strlen(name); | |
| 911 | char *eptr; | |
| 912 | u_long val; | |
| 913 | ||
| 914 | /* Check for proper length, brackets, no leading junk */ | |
| 915 | if ((len < 3) | |
| 916 | || (name[0] != '[') | |
| 917 | || (name[len - 1] != ']') | |
| 918 | || (!isxdigit(name[1]))) { | |
| 919 | return ((ng_ID_t)0); | |
| 920 | } | |
| 921 | ||
| 922 | /* Decode number */ | |
| 923 | val = strtoul(name + 1, &eptr, 16); | |
| 924 | if ((eptr - name != len - 1) | |
| 925 | || (val == ULONG_MAX) | |
| 926 | || (val == 0)) { | |
| 927 | return ((ng_ID_t)0); | |
| 928 | } | |
| 929 | return (ng_ID_t)val; | |
| 930 | } | |
| 931 | ||
| 932 | /* | |
| 933 | * Remove a name from a node. This should only be called | |
| 934 | * when shutting down and removing the node. | |
| 935 | * IF we allow name changing this may be more resurrected. | |
| 936 | */ | |
| 937 | void | |
| 938 | ng_unname(node_p node) | |
| 939 | { | |
| 940 | } | |
| 941 | ||
| 942 | /************************************************************************ | |
| 943 | Hook routines | |
| 944 | Names are not optional. Hooks are always connected, except for a | |
| 945 | brief moment within these routines. On invalidation or during creation | |
| 946 | they are connected to the 'dead' hook. | |
| 947 | ************************************************************************/ | |
| 948 | ||
| 949 | /* | |
| 950 | * Remove a hook reference | |
| 951 | */ | |
| 952 | void | |
| 953 | ng_unref_hook(hook_p hook) | |
| 954 | { | |
| 955 | int v; | |
| 956 | ||
| 957 | if (hook == &ng_deadhook) { | |
| 958 | return; | |
| 959 | } | |
| 960 | ||
| 961 | v = atomic_fetchadd_int(&hook->hk_refs, -1); | |
| 962 | ||
| 963 | if (v == 1) { /* we were the last */ | |
| 964 | if (_NG_HOOK_NODE(hook)) /* it'll probably be ng_deadnode */ | |
| 965 | _NG_NODE_UNREF((_NG_HOOK_NODE(hook))); | |
| 966 | NG_FREE_HOOK(hook); | |
| 967 | } | |
| 968 | } | |
| 969 | ||
| 970 | /* | |
| 971 | * Add an unconnected hook to a node. Only used internally. | |
| 972 | * Assumes node is locked. (XXX not yet true ) | |
| 973 | */ | |
| 974 | static int | |
| 975 | ng_add_hook(node_p node, const char *name, hook_p *hookp) | |
| 976 | { | |
| 977 | hook_p hook; | |
| 978 | int error = 0; | |
| 979 | ||
| 980 | /* Check that the given name is good */ | |
| 981 | if (name == NULL) { | |
| 982 | TRAP_ERROR(); | |
| 983 | return (EINVAL); | |
| 984 | } | |
| 985 | if (ng_findhook(node, name) != NULL) { | |
| 986 | TRAP_ERROR(); | |
| 987 | return (EEXIST); | |
| 988 | } | |
| 989 | ||
| 990 | /* Allocate the hook and link it up */ | |
| 991 | NG_ALLOC_HOOK(hook); | |
| 992 | if (hook == NULL) { | |
| 993 | TRAP_ERROR(); | |
| 994 | return (ENOMEM); | |
| 995 | } | |
| 996 | hook->hk_refs = 1; /* add a reference for us to return */ | |
| 997 | hook->hk_flags = HK_INVALID; | |
| 998 | hook->hk_peer = &ng_deadhook; /* start off this way */ | |
| 999 | hook->hk_node = node; | |
| 1000 | NG_NODE_REF(node); /* each hook counts as a reference */ | |
| 1001 | ||
| 1002 | /* Set hook name */ | |
| 1003 | strlcpy(NG_HOOK_NAME(hook), name, NG_HOOKSIZ); | |
| 1004 | ||
| 1005 | /* | |
| 1006 | * Check if the node type code has something to say about it | |
| 1007 | * If it fails, the unref of the hook will also unref the node. | |
| 1008 | */ | |
| 1009 | if (node->nd_type->newhook != NULL) { | |
| 1010 | if ((error = (*node->nd_type->newhook)(node, hook, name))) { | |
| 1011 | NG_HOOK_UNREF(hook); /* this frees the hook */ | |
| 1012 | return (error); | |
| 1013 | } | |
| 1014 | } | |
| 1015 | /* | |
| 1016 | * The 'type' agrees so far, so go ahead and link it in. | |
| 1017 | * We'll ask again later when we actually connect the hooks. | |
| 1018 | */ | |
| 1019 | LIST_INSERT_HEAD(&node->nd_hooks, hook, hk_hooks); | |
| 1020 | node->nd_numhooks++; | |
| 1021 | NG_HOOK_REF(hook); /* one for the node */ | |
| 1022 | ||
| 1023 | if (hookp) | |
| 1024 | *hookp = hook; | |
| 1025 | return (0); | |
| 1026 | } | |
| 1027 | ||
| 1028 | /* | |
| 1029 | * Find a hook | |
| 1030 | * | |
| 1031 | * Node types may supply their own optimized routines for finding | |
| 1032 | * hooks. If none is supplied, we just do a linear search. | |
| 1033 | * XXX Possibly we should add a reference to the hook? | |
| 1034 | */ | |
| 1035 | hook_p | |
| 1036 | ng_findhook(node_p node, const char *name) | |
| 1037 | { | |
| 1038 | hook_p hook; | |
| 1039 | ||
| 1040 | if (node->nd_type->findhook != NULL) | |
| 1041 | return (*node->nd_type->findhook)(node, name); | |
| 1042 | LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) { | |
| 1043 | if (NG_HOOK_IS_VALID(hook) | |
| 1044 | && (strcmp(NG_HOOK_NAME(hook), name) == 0)) | |
| 1045 | return (hook); | |
| 1046 | } | |
| 1047 | return (NULL); | |
| 1048 | } | |
| 1049 | ||
| 1050 | /* | |
| 1051 | * Destroy a hook | |
| 1052 | * | |
| 1053 | * As hooks are always attached, this really destroys two hooks. | |
| 1054 | * The one given, and the one attached to it. Disconnect the hooks | |
| 1055 | * from each other first. We reconnect the peer hook to the 'dead' | |
| 1056 | * hook so that it can still exist after we depart. We then | |
| 1057 | * send the peer its own destroy message. This ensures that we only | |
| 1058 | * interact with the peer's structures when it is locked processing that | |
| 1059 | * message. We hold a reference to the peer hook so we are guaranteed that | |
| 1060 | * the peer hook and node are still going to exist until | |
| 1061 | * we are finished there as the hook holds a ref on the node. | |
| 1062 | * We run this same code again on the peer hook, but that time it is already | |
| 1063 | * attached to the 'dead' hook. | |
| 1064 | * | |
| 1065 | * This routine is called at all stages of hook creation | |
| 1066 | * on error detection and must be able to handle any such stage. | |
| 1067 | */ | |
| 1068 | void | |
| 1069 | ng_destroy_hook(hook_p hook) | |
| 1070 | { | |
| 1071 | hook_p peer; | |
| 1072 | node_p node; | |
| 1073 | ||
| 1074 | if (hook == &ng_deadhook) { /* better safe than sorry */ | |
| 1075 | printf("ng_destroy_hook called on deadhook\n"); | |
| 1076 | return; | |
| 1077 | } | |
| 1078 | ||
| 1079 | /* | |
| 1080 | * Protect divorce process with mutex, to avoid races on | |
| 1081 | * simultaneous disconnect. | |
| 1082 | */ | |
| 1083 | mtx_lock(&ng_topo_mtx); | |
| 1084 | ||
| 1085 | hook->hk_flags |= HK_INVALID; | |
| 1086 | ||
| 1087 | peer = NG_HOOK_PEER(hook); | |
| 1088 | node = NG_HOOK_NODE(hook); | |
| 1089 | ||
| 1090 | if (peer && (peer != &ng_deadhook)) { | |
| 1091 | /* | |
| 1092 | * Set the peer to point to ng_deadhook | |
| 1093 | * from this moment on we are effectively independent it. | |
| 1094 | * send it an rmhook message of it's own. | |
| 1095 | */ | |
| 1096 | peer->hk_peer = &ng_deadhook; /* They no longer know us */ | |
| 1097 | hook->hk_peer = &ng_deadhook; /* Nor us, them */ | |
| 1098 | if (NG_HOOK_NODE(peer) == &ng_deadnode) { | |
| 1099 | /* | |
| 1100 | * If it's already divorced from a node, | |
| 1101 | * just free it. | |
| 1102 | */ | |
| 1103 | mtx_unlock(&ng_topo_mtx); | |
| 1104 | } else { | |
| 1105 | mtx_unlock(&ng_topo_mtx); | |
| 1106 | ng_rmhook_self(peer); /* Send it a surprise */ | |
| 1107 | } | |
| 1108 | NG_HOOK_UNREF(peer); /* account for peer link */ | |
| 1109 | NG_HOOK_UNREF(hook); /* account for peer link */ | |
| 1110 | } else | |
| 1111 | mtx_unlock(&ng_topo_mtx); | |
| 1112 | ||
| 1113 | mtx_assert(&ng_topo_mtx, MA_NOTOWNED); | |
| 1114 | ||
| 1115 | /* | |
| 1116 | * Remove the hook from the node's list to avoid possible recursion | |
| 1117 | * in case the disconnection results in node shutdown. | |
| 1118 | */ | |
| 1119 | if (node == &ng_deadnode) { /* happens if called from ng_con_nodes() */ | |
| 1120 | return; | |
| 1121 | } | |
| 1122 | LIST_REMOVE(hook, hk_hooks); | |
| 1123 | node->nd_numhooks--; | |
| 1124 | if (node->nd_type->disconnect) { | |
| 1125 | /* | |
| 1126 | * The type handler may elect to destroy the node so don't | |
| 1127 | * trust its existence after this point. (except | |
| 1128 | * that we still hold a reference on it. (which we | |
| 1129 | * inherrited from the hook we are destroying) | |
| 1130 | */ | |
| 1131 | (*node->nd_type->disconnect) (hook); | |
| 1132 | } | |
| 1133 | ||
| 1134 | /* | |
| 1135 | * Note that because we will point to ng_deadnode, the original node | |
| 1136 | * is not decremented automatically so we do that manually. | |
| 1137 | */ | |
| 1138 | _NG_HOOK_NODE(hook) = &ng_deadnode; | |
| 1139 | NG_NODE_UNREF(node); /* We no longer point to it so adjust count */ | |
| 1140 | NG_HOOK_UNREF(hook); /* Account for linkage (in list) to node */ | |
| 1141 | } | |
| 1142 | ||
| 1143 | /* | |
| 1144 | * Take two hooks on a node and merge the connection so that the given node | |
| 1145 | * is effectively bypassed. | |
| 1146 | */ | |
| 1147 | int | |
| 1148 | ng_bypass(hook_p hook1, hook_p hook2) | |
| 1149 | { | |
| 1150 | if (hook1->hk_node != hook2->hk_node) { | |
| 1151 | TRAP_ERROR(); | |
| 1152 | return (EINVAL); | |
| 1153 | } | |
| 1154 | hook1->hk_peer->hk_peer = hook2->hk_peer; | |
| 1155 | hook2->hk_peer->hk_peer = hook1->hk_peer; | |
| 1156 | ||
| 1157 | hook1->hk_peer = &ng_deadhook; | |
| 1158 | hook2->hk_peer = &ng_deadhook; | |
| 1159 | ||
| 1160 | NG_HOOK_UNREF(hook1); | |
| 1161 | NG_HOOK_UNREF(hook2); | |
| 1162 | ||
| 1163 | /* XXX If we ever cache methods on hooks update them as well */ | |
| 1164 | ng_destroy_hook(hook1); | |
| 1165 | ng_destroy_hook(hook2); | |
| 1166 | return (0); | |
| 1167 | } | |
| 1168 | ||
| 1169 | /* | |
| 1170 | * Install a new netgraph type | |
| 1171 | */ | |
| 1172 | int | |
| 1173 | ng_newtype(struct ng_type *tp) | |
| 1174 | { | |
| 1175 | const size_t namelen = strlen(tp->name); | |
| 1176 | ||
| 1177 | /* Check version and type name fields */ | |
| 1178 | if ((tp->version != NG_ABI_VERSION) | |
| 1179 | || (namelen == 0) | |
| 1180 | || (namelen >= NG_TYPESIZ)) { | |
| 1181 | TRAP_ERROR(); | |
| 1182 | if (tp->version != NG_ABI_VERSION) { | |
| 1183 | printf("Netgraph: Node type rejected. ABI mismatch. Suggest recompile\n"); | |
| 1184 | } | |
| 1185 | return (EINVAL); | |
| 1186 | } | |
| 1187 | ||
| 1188 | /* Check for name collision */ | |
| 1189 | if (ng_findtype(tp->name) != NULL) { | |
| 1190 | TRAP_ERROR(); | |
| 1191 | return (EEXIST); | |
| 1192 | } | |
| 1193 | ||
| 1194 | ||
| 1195 | /* Link in new type */ | |
| 1196 | mtx_lock(&ng_typelist_mtx); | |
| 1197 | LIST_INSERT_HEAD(&ng_typelist, tp, types); | |
| 1198 | tp->refs = 1; /* first ref is linked list */ | |
| 1199 | mtx_unlock(&ng_typelist_mtx); | |
| 1200 | return (0); | |
| 1201 | } | |
| 1202 | ||
| 1203 | /* | |
| 1204 | * unlink a netgraph type | |
| 1205 | * If no examples exist | |
| 1206 | */ | |
| 1207 | int | |
| 1208 | ng_rmtype(struct ng_type *tp) | |
| 1209 | { | |
| 1210 | /* Check for name collision */ | |
| 1211 | if (tp->refs != 1) { | |
| 1212 | TRAP_ERROR(); | |
| 1213 | return (EBUSY); | |
| 1214 | } | |
| 1215 | ||
| 1216 | /* Unlink type */ | |
| 1217 | mtx_lock(&ng_typelist_mtx); | |
| 1218 | LIST_REMOVE(tp, types); | |
| 1219 | mtx_unlock(&ng_typelist_mtx); | |
| 1220 | return (0); | |
| 1221 | } | |
| 1222 | ||
| 1223 | /* | |
| 1224 | * Look for a type of the name given | |
| 1225 | */ | |
| 1226 | struct ng_type * | |
| 1227 | ng_findtype(const char *typename) | |
| 1228 | { | |
| 1229 | struct ng_type *type; | |
| 1230 | ||
| 1231 | mtx_lock(&ng_typelist_mtx); | |
| 1232 | LIST_FOREACH(type, &ng_typelist, types) { | |
| 1233 | if (strcmp(type->name, typename) == 0) | |
| 1234 | break; | |
| 1235 | } | |
| 1236 | mtx_unlock(&ng_typelist_mtx); | |
| 1237 | return (type); | |
| 1238 | } | |
| 1239 | ||
| 1240 | /************************************************************************ | |
| 1241 | Composite routines | |
| 1242 | ************************************************************************/ | |
| 1243 | /* | |
| 1244 | * Connect two nodes using the specified hooks, using queued functions. | |
| 1245 | */ | |
| 1246 | static int | |
| 1247 | ng_con_part3(node_p node, item_p item, hook_p hook) | |
| 1248 | { | |
| 1249 | int error = 0; | |
| 1250 | ||
| 1251 | /* | |
| 1252 | * When we run, we know that the node 'node' is locked for us. | |
| 1253 | * Our caller has a reference on the hook. | |
| 1254 | * Our caller has a reference on the node. | |
| 1255 | * (In this case our caller is ng_apply_item() ). | |
| 1256 | * The peer hook has a reference on the hook. | |
| 1257 | * We are all set up except for the final call to the node, and | |
| 1258 | * the clearing of the INVALID flag. | |
| 1259 | */ | |
| 1260 | if (NG_HOOK_NODE(hook) == &ng_deadnode) { | |
| 1261 | /* | |
| 1262 | * The node must have been freed again since we last visited | |
| 1263 | * here. ng_destry_hook() has this effect but nothing else does. | |
| 1264 | * We should just release our references and | |
| 1265 | * free anything we can think of. | |
| 1266 | * Since we know it's been destroyed, and it's our caller | |
| 1267 | * that holds the references, just return. | |
| 1268 | */ | |
| 1269 | ERROUT(ENOENT); | |
| 1270 | } | |
| 1271 | if (hook->hk_node->nd_type->connect) { | |
| 1272 | if ((error = (*hook->hk_node->nd_type->connect) (hook))) { | |
| 1273 | ng_destroy_hook(hook); /* also zaps peer */ | |
| 1274 | printf("failed in ng_con_part3()\n"); | |
| 1275 | ERROUT(error); | |
| 1276 | } | |
| 1277 | } | |
| 1278 | /* | |
| 1279 | * XXX this is wrong for SMP. Possibly we need | |
| 1280 | * to separate out 'create' and 'invalid' flags. | |
| 1281 | * should only set flags on hooks we have locked under our node. | |
| 1282 | */ | |
| 1283 | hook->hk_flags &= ~HK_INVALID; | |
| 1284 | done: | |
| 1285 | NG_FREE_ITEM(item); | |
| 1286 | return (error); | |
| 1287 | } | |
| 1288 | ||
| 1289 | static int | |
| 1290 | ng_con_part2(node_p node, item_p item, hook_p hook) | |
| 1291 | { | |
| 1292 | hook_p peer; | |
| 1293 | int error = 0; | |
| 1294 | ||
| 1295 | /* | |
| 1296 | * When we run, we know that the node 'node' is locked for us. | |
| 1297 | * Our caller has a reference on the hook. | |
| 1298 | * Our caller has a reference on the node. | |
| 1299 | * (In this case our caller is ng_apply_item() ). | |
| 1300 | * The peer hook has a reference on the hook. | |
| 1301 | * our node pointer points to the 'dead' node. | |
| 1302 | * First check the hook name is unique. | |
| 1303 | * Should not happen because we checked before queueing this. | |
| 1304 | */ | |
| 1305 | if (ng_findhook(node, NG_HOOK_NAME(hook)) != NULL) { | |
| 1306 | TRAP_ERROR(); | |
| 1307 | ng_destroy_hook(hook); /* should destroy peer too */ | |
| 1308 | printf("failed in ng_con_part2()\n"); | |
| 1309 | ERROUT(EEXIST); | |
| 1310 | } | |
| 1311 | /* | |
| 1312 | * Check if the node type code has something to say about it | |
| 1313 | * If it fails, the unref of the hook will also unref the attached node, | |
| 1314 | * however since that node is 'ng_deadnode' this will do nothing. | |
| 1315 | * The peer hook will also be destroyed. | |
| 1316 | */ | |
| 1317 | if (node->nd_type->newhook != NULL) { | |
| 1318 | if ((error = (*node->nd_type->newhook)(node, hook, | |
| 1319 | hook->hk_name))) { | |
| 1320 | ng_destroy_hook(hook); /* should destroy peer too */ | |
| 1321 | printf("failed in ng_con_part2()\n"); | |
| 1322 | ERROUT(error); | |
| 1323 | } | |
| 1324 | } | |
| 1325 | ||
| 1326 | /* | |
| 1327 | * The 'type' agrees so far, so go ahead and link it in. | |
| 1328 | * We'll ask again later when we actually connect the hooks. | |
| 1329 | */ | |
| 1330 | hook->hk_node = node; /* just overwrite ng_deadnode */ | |
| 1331 | NG_NODE_REF(node); /* each hook counts as a reference */ | |
| 1332 | LIST_INSERT_HEAD(&node->nd_hooks, hook, hk_hooks); | |
| 1333 | node->nd_numhooks++; | |
| 1334 | NG_HOOK_REF(hook); /* one for the node */ | |
| 1335 | ||
| 1336 | /* | |
| 1337 | * We now have a symmetrical situation, where both hooks have been | |
| 1338 | * linked to their nodes, the newhook methods have been called | |
| 1339 | * And the references are all correct. The hooks are still marked | |
| 1340 | * as invalid, as we have not called the 'connect' methods | |
| 1341 | * yet. | |
| 1342 | * We can call the local one immediately as we have the | |
| 1343 | * node locked, but we need to queue the remote one. | |
| 1344 | */ | |
| 1345 | if (hook->hk_node->nd_type->connect) { | |
| 1346 | if ((error = (*hook->hk_node->nd_type->connect) (hook))) { | |
| 1347 | ng_destroy_hook(hook); /* also zaps peer */ | |
| 1348 | printf("failed in ng_con_part2(A)\n"); | |
| 1349 | ERROUT(error); | |
| 1350 | } | |
| 1351 | } | |
| 1352 | ||
| 1353 | /* | |
| 1354 | * Acquire topo mutex to avoid race with ng_destroy_hook(). | |
| 1355 | */ | |
| 1356 | mtx_lock(&ng_topo_mtx); | |
| 1357 | peer = hook->hk_peer; | |
| 1358 | if (peer == &ng_deadhook) { | |
| 1359 | mtx_unlock(&ng_topo_mtx); | |
| 1360 | printf("failed in ng_con_part2(B)\n"); | |
| 1361 | ng_destroy_hook(hook); | |
| 1362 | ERROUT(ENOENT); | |
| 1363 | } | |
| 1364 | mtx_unlock(&ng_topo_mtx); | |
| 1365 | ||
| 1366 | if ((error = ng_send_fn2(peer->hk_node, peer, item, &ng_con_part3, | |
| 1367 | NULL, 0, NG_REUSE_ITEM))) { | |
| 1368 | printf("failed in ng_con_part2(C)\n"); | |
| 1369 | ng_destroy_hook(hook); /* also zaps peer */ | |
| 1370 | return (error); /* item was consumed. */ | |
| 1371 | } | |
| 1372 | hook->hk_flags &= ~HK_INVALID; /* need both to be able to work */ | |
| 1373 | return (0); /* item was consumed. */ | |
| 1374 | done: | |
| 1375 | NG_FREE_ITEM(item); | |
| 1376 | return (error); | |
| 1377 | } | |
| 1378 | ||
| 1379 | /* | |
| 1380 | * Connect this node with another node. We assume that this node is | |
| 1381 | * currently locked, as we are only called from an NGM_CONNECT message. | |
| 1382 | */ | |
| 1383 | static int | |
| 1384 | ng_con_nodes(item_p item, node_p node, const char *name, | |
| 1385 | node_p node2, const char *name2) | |
| 1386 | { | |
| 1387 | int error; | |
| 1388 | hook_p hook; | |
| 1389 | hook_p hook2; | |
| 1390 | ||
| 1391 | if (ng_findhook(node2, name2) != NULL) { | |
| 1392 | return(EEXIST); | |
| 1393 | } | |
| 1394 | if ((error = ng_add_hook(node, name, &hook))) /* gives us a ref */ | |
| 1395 | return (error); | |
| 1396 | /* Allocate the other hook and link it up */ | |
| 1397 | NG_ALLOC_HOOK(hook2); | |
| 1398 | if (hook2 == NULL) { | |
| 1399 | TRAP_ERROR(); | |
| 1400 | ng_destroy_hook(hook); /* XXX check ref counts so far */ | |
| 1401 | NG_HOOK_UNREF(hook); /* including our ref */ | |
| 1402 | return (ENOMEM); | |
| 1403 | } | |
| 1404 | hook2->hk_refs = 1; /* start with a reference for us. */ | |
| 1405 | hook2->hk_flags = HK_INVALID; | |
| 1406 | hook2->hk_peer = hook; /* Link the two together */ | |
| 1407 | hook->hk_peer = hook2; | |
| 1408 | NG_HOOK_REF(hook); /* Add a ref for the peer to each*/ | |
| 1409 | NG_HOOK_REF(hook2); | |
| 1410 | hook2->hk_node = &ng_deadnode; | |
| 1411 | strlcpy(NG_HOOK_NAME(hook2), name2, NG_HOOKSIZ); | |
| 1412 | ||
| 1413 | /* | |
| 1414 | * Queue the function above. | |
| 1415 | * Procesing continues in that function in the lock context of | |
| 1416 | * the other node. | |
| 1417 | */ | |
| 1418 | if ((error = ng_send_fn2(node2, hook2, item, &ng_con_part2, NULL, 0, | |
| 1419 | NG_NOFLAGS))) { | |
| 1420 | printf("failed in ng_con_nodes(): %d\n", error); | |
| 1421 | ng_destroy_hook(hook); /* also zaps peer */ | |
| 1422 | } | |
| 1423 | ||
| 1424 | NG_HOOK_UNREF(hook); /* Let each hook go if it wants to */ | |
| 1425 | NG_HOOK_UNREF(hook2); | |
| 1426 | return (error); | |
| 1427 | } | |
| 1428 | ||
| 1429 | /* | |
| 1430 | * Make a peer and connect. | |
| 1431 | * We assume that the local node is locked. | |
| 1432 | * The new node probably doesn't need a lock until | |
| 1433 | * it has a hook, because it cannot really have any work until then, | |
| 1434 | * but we should think about it a bit more. | |
| 1435 | * | |
| 1436 | * The problem may come if the other node also fires up | |
| 1437 | * some hardware or a timer or some other source of activation, | |
| 1438 | * also it may already get a command msg via it's ID. | |
| 1439 | * | |
| 1440 | * We could use the same method as ng_con_nodes() but we'd have | |
| 1441 | * to add ability to remove the node when failing. (Not hard, just | |
| 1442 | * make arg1 point to the node to remove). | |
| 1443 | * Unless of course we just ignore failure to connect and leave | |
| 1444 | * an unconnected node? | |
| 1445 | */ | |
| 1446 | static int | |
| 1447 | ng_mkpeer(node_p node, const char *name, const char *name2, char *type) | |
| 1448 | { | |
| 1449 | node_p node2; | |
| 1450 | hook_p hook1, hook2; | |
| 1451 | int error; | |
| 1452 | ||
| 1453 | if ((error = ng_make_node(type, &node2))) { | |
| 1454 | return (error); | |
| 1455 | } | |
| 1456 | ||
| 1457 | if ((error = ng_add_hook(node, name, &hook1))) { /* gives us a ref */ | |
| 1458 | ng_rmnode(node2, NULL, NULL, 0); | |
| 1459 | return (error); | |
| 1460 | } | |
| 1461 | ||
| 1462 | if ((error = ng_add_hook(node2, name2, &hook2))) { | |
| 1463 | ng_rmnode(node2, NULL, NULL, 0); | |
| 1464 | ng_destroy_hook(hook1); | |
| 1465 | NG_HOOK_UNREF(hook1); | |
| 1466 | return (error); | |
| 1467 | } | |
| 1468 | ||
| 1469 | /* | |
| 1470 | * Actually link the two hooks together. | |
| 1471 | */ | |
| 1472 | hook1->hk_peer = hook2; | |
| 1473 | hook2->hk_peer = hook1; | |
| 1474 | ||
| 1475 | /* Each hook is referenced by the other */ | |
| 1476 | NG_HOOK_REF(hook1); | |
| 1477 | NG_HOOK_REF(hook2); | |
| 1478 | ||
| 1479 | /* Give each node the opportunity to veto the pending connection */ | |
| 1480 | if (hook1->hk_node->nd_type->connect) { | |
| 1481 | error = (*hook1->hk_node->nd_type->connect) (hook1); | |
| 1482 | } | |
| 1483 | ||
| 1484 | if ((error == 0) && hook2->hk_node->nd_type->connect) { | |
| 1485 | error = (*hook2->hk_node->nd_type->connect) (hook2); | |
| 1486 | ||
| 1487 | } | |
| 1488 | ||
| 1489 | /* | |
| 1490 | * drop the references we were holding on the two hooks. | |
| 1491 | */ | |
| 1492 | if (error) { | |
| 1493 | ng_destroy_hook(hook2); /* also zaps hook1 */ | |
| 1494 | ng_rmnode(node2, NULL, NULL, 0); | |
| 1495 | } else { | |
| 1496 | /* As a last act, allow the hooks to be used */ | |
| 1497 | hook1->hk_flags &= ~HK_INVALID; | |
| 1498 | hook2->hk_flags &= ~HK_INVALID; | |
| 1499 | } | |
| 1500 | NG_HOOK_UNREF(hook1); | |
| 1501 | NG_HOOK_UNREF(hook2); | |
| 1502 | return (error); | |
| 1503 | } | |
| 1504 | ||
| 1505 | /************************************************************************ | |
| 1506 | Utility routines to send self messages | |
| 1507 | ************************************************************************/ | |
| 1508 | ||
| 1509 | /* Shut this node down as soon as everyone is clear of it */ | |
| 1510 | /* Should add arg "immediately" to jump the queue */ | |
| 1511 | int | |
| 1512 | ng_rmnode_self(node_p node) | |
| 1513 | { | |
| 1514 | int error; | |
| 1515 | ||
| 1516 | if (node == &ng_deadnode) | |
| 1517 | return (0); | |
| 1518 | node->nd_flags |= NGF_INVALID; | |
| 1519 | if (node->nd_flags & NGF_CLOSING) | |
| 1520 | return (0); | |
| 1521 | ||
| 1522 | error = ng_send_fn(node, NULL, &ng_rmnode, NULL, 0); | |
| 1523 | return (error); | |
| 1524 | } | |
| 1525 | ||
| 1526 | static void | |
| 1527 | ng_rmhook_part2(node_p node, hook_p hook, void *arg1, int arg2) | |
| 1528 | { | |
| 1529 | ng_destroy_hook(hook); | |
| 1530 | return ; | |
| 1531 | } | |
| 1532 | ||
| 1533 | int | |
| 1534 | ng_rmhook_self(hook_p hook) | |
| 1535 | { | |
| 1536 | int error; | |
| 1537 | node_p node = NG_HOOK_NODE(hook); | |
| 1538 | ||
| 1539 | if (node == &ng_deadnode) | |
| 1540 | return (0); | |
| 1541 | ||
| 1542 | error = ng_send_fn(node, hook, &ng_rmhook_part2, NULL, 0); | |
| 1543 | return (error); | |
| 1544 | } | |
| 1545 | ||
| 1546 | /*********************************************************************** | |
| 1547 | * Parse and verify a string of the form: <NODE:><PATH> | |
| 1548 | * | |
| 1549 | * Such a string can refer to a specific node or a specific hook | |
| 1550 | * on a specific node, depending on how you look at it. In the | |
| 1551 | * latter case, the PATH component must not end in a dot. | |
| 1552 | * | |
| 1553 | * Both <NODE:> and <PATH> are optional. The <PATH> is a string | |
| 1554 | * of hook names separated by dots. This breaks out the original | |
| 1555 | * string, setting *nodep to "NODE" (or NULL if none) and *pathp | |
| 1556 | * to "PATH" (or NULL if degenerate). Also, *hookp will point to | |
| 1557 | * the final hook component of <PATH>, if any, otherwise NULL. | |
| 1558 | * | |
| 1559 | * This returns -1 if the path is malformed. The char ** are optional. | |
| 1560 | ***********************************************************************/ | |
| 1561 | int | |
| 1562 | ng_path_parse(char *addr, char **nodep, char **pathp, char **hookp) | |
| 1563 | { | |
| 1564 | char *node, *path, *hook; | |
| 1565 | int k; | |
| 1566 | ||
| 1567 | /* | |
| 1568 | * Extract absolute NODE, if any | |
| 1569 | */ | |
| 1570 | for (path = addr; *path && *path != ':'; path++); | |
| 1571 | if (*path) { | |
| 1572 | node = addr; /* Here's the NODE */ | |
| 1573 | *path++ = '\0'; /* Here's the PATH */ | |
| 1574 | ||
| 1575 | /* Node name must not be empty */ | |
| 1576 | if (!*node) | |
| 1577 | return -1; | |
| 1578 | ||
| 1579 | /* A name of "." is OK; otherwise '.' not allowed */ | |
| 1580 | if (strcmp(node, ".") != 0) { | |
| 1581 | for (k = 0; node[k]; k++) | |
| 1582 | if (node[k] == '.') | |
| 1583 | return -1; | |
| 1584 | } | |
| 1585 | } else { | |
| 1586 | node = NULL; /* No absolute NODE */ | |
| 1587 | path = addr; /* Here's the PATH */ | |
| 1588 | } | |
| 1589 | ||
| 1590 | /* Snoop for illegal characters in PATH */ | |
| 1591 | for (k = 0; path[k]; k++) | |
| 1592 | if (path[k] == ':') | |
| 1593 | return -1; | |
| 1594 | ||
| 1595 | /* Check for no repeated dots in PATH */ | |
| 1596 | for (k = 0; path[k]; k++) | |
| 1597 | if (path[k] == '.' && path[k + 1] == '.') | |
| 1598 | return -1; | |
| 1599 | ||
| 1600 | /* Remove extra (degenerate) dots from beginning or end of PATH */ | |
| 1601 | if (path[0] == '.') | |
| 1602 | path++; | |
| 1603 | if (*path && path[strlen(path) - 1] == '.') | |
| 1604 | path[strlen(path) - 1] = 0; | |
| 1605 | ||
| 1606 | /* If PATH has a dot, then we're not talking about a hook */ | |
| 1607 | if (*path) { | |
| 1608 | for (hook = path, k = 0; path[k]; k++) | |
| 1609 | if (path[k] == '.') { | |
| 1610 | hook = NULL; | |
| 1611 | break; | |
| 1612 | } | |
| 1613 | } else | |
| 1614 | path = hook = NULL; | |
| 1615 | ||
| 1616 | /* Done */ | |
| 1617 | if (nodep) | |
| 1618 | *nodep = node; | |
| 1619 | if (pathp) | |
| 1620 | *pathp = path; | |
| 1621 | if (hookp) | |
| 1622 | *hookp = hook; | |
| 1623 | return (0); | |
| 1624 | } | |
| 1625 | ||
| 1626 | /* | |
| 1627 | * Given a path, which may be absolute or relative, and a starting node, | |
| 1628 | * return the destination node. | |
| 1629 | */ | |
| 1630 | int | |
| 1631 | ng_path2noderef(node_p here, const char *address, | |
| 1632 | node_p *destp, hook_p *lasthook) | |
| 1633 | { | |
| 1634 | char fullpath[NG_PATHSIZ]; | |
| 1635 | char *nodename, *path, pbuf[2]; | |
| 1636 | node_p node, oldnode; | |
| 1637 | char *cp; | |
| 1638 | hook_p hook = NULL; | |
| 1639 | ||
| 1640 | /* Initialize */ | |
| 1641 | if (destp == NULL) { | |
| 1642 | TRAP_ERROR(); | |
| 1643 | return EINVAL; | |
| 1644 | } | |
| 1645 | *destp = NULL; | |
| 1646 | ||
| 1647 | /* Make a writable copy of address for ng_path_parse() */ | |
| 1648 | strncpy(fullpath, address, sizeof(fullpath) - 1); | |
| 1649 | fullpath[sizeof(fullpath) - 1] = '\0'; | |
| 1650 | ||
| 1651 | /* Parse out node and sequence of hooks */ | |
| 1652 | if (ng_path_parse(fullpath, &nodename, &path, NULL) < 0) { | |
| 1653 | TRAP_ERROR(); | |
| 1654 | return EINVAL; | |
| 1655 | } | |
| 1656 | if (path == NULL) { | |
| 1657 | pbuf[0] = '.'; /* Needs to be writable */ | |
| 1658 | pbuf[1] = '\0'; | |
| 1659 | path = pbuf; | |
| 1660 | } | |
| 1661 | ||
| 1662 | /* | |
| 1663 | * For an absolute address, jump to the starting node. | |
| 1664 | * Note that this holds a reference on the node for us. | |
| 1665 | * Don't forget to drop the reference if we don't need it. | |
| 1666 | */ | |
| 1667 | if (nodename) { | |
| 1668 | node = ng_name2noderef(here, nodename); | |
| 1669 | if (node == NULL) { | |
| 1670 | TRAP_ERROR(); | |
| 1671 | return (ENOENT); | |
| 1672 | } | |
| 1673 | } else { | |
| 1674 | if (here == NULL) { | |
| 1675 | TRAP_ERROR(); | |
| 1676 | return (EINVAL); | |
| 1677 | } | |
| 1678 | node = here; | |
| 1679 | NG_NODE_REF(node); | |
| 1680 | } | |
| 1681 | ||
| 1682 | /* | |
| 1683 | * Now follow the sequence of hooks | |
| 1684 | * XXX | |
| 1685 | * We actually cannot guarantee that the sequence | |
| 1686 | * is not being demolished as we crawl along it | |
| 1687 | * without extra-ordinary locking etc. | |
| 1688 | * So this is a bit dodgy to say the least. | |
| 1689 | * We can probably hold up some things by holding | |
| 1690 | * the nodelist mutex for the time of this | |
| 1691 | * crawl if we wanted.. At least that way we wouldn't have to | |
| 1692 | * worry about the nodes disappearing, but the hooks would still | |
| 1693 | * be a problem. | |
| 1694 | */ | |
| 1695 | for (cp = path; node != NULL && *cp != '\0'; ) { | |
| 1696 | char *segment; | |
| 1697 | ||
| 1698 | /* | |
| 1699 | * Break out the next path segment. Replace the dot we just | |
| 1700 | * found with a NUL; "cp" points to the next segment (or the | |
| 1701 | * NUL at the end). | |
| 1702 | */ | |
| 1703 | for (segment = cp; *cp != '\0'; cp++) { | |
| 1704 | if (*cp == '.') { | |
| 1705 | *cp++ = '\0'; | |
| 1706 | break; | |
| 1707 | } | |
| 1708 | } | |
| 1709 | ||
| 1710 | /* Empty segment */ | |
| 1711 | if (*segment == '\0') | |
| 1712 | continue; | |
| 1713 | ||
| 1714 | /* We have a segment, so look for a hook by that name */ | |
| 1715 | hook = ng_findhook(node, segment); | |
| 1716 | ||
| 1717 | /* Can't get there from here... */ | |
| 1718 | if (hook == NULL | |
| 1719 | || NG_HOOK_PEER(hook) == NULL | |
| 1720 | || NG_HOOK_NOT_VALID(hook) | |
| 1721 | || NG_HOOK_NOT_VALID(NG_HOOK_PEER(hook))) { | |
| 1722 | TRAP_ERROR(); | |
| 1723 | NG_NODE_UNREF(node); | |
| 1724 | #if 0 | |
| 1725 | printf("hooknotvalid %s %s %d %d %d %d ", | |
| 1726 | path, | |
| 1727 | segment, | |
| 1728 | hook == NULL, | |
| 1729 | NG_HOOK_PEER(hook) == NULL, | |
| 1730 | NG_HOOK_NOT_VALID(hook), | |
| 1731 | NG_HOOK_NOT_VALID(NG_HOOK_PEER(hook))); | |
| 1732 | #endif | |
| 1733 | return (ENOENT); | |
| 1734 | } | |
| 1735 | ||
| 1736 | /* | |
| 1737 | * Hop on over to the next node | |
| 1738 | * XXX | |
| 1739 | * Big race conditions here as hooks and nodes go away | |
| 1740 | * *** Idea.. store an ng_ID_t in each hook and use that | |
| 1741 | * instead of the direct hook in this crawl? | |
| 1742 | */ | |
| 1743 | oldnode = node; | |
| 1744 | if ((node = NG_PEER_NODE(hook))) | |
| 1745 | NG_NODE_REF(node); /* XXX RACE */ | |
| 1746 | NG_NODE_UNREF(oldnode); /* XXX another race */ | |
| 1747 | if (NG_NODE_NOT_VALID(node)) { | |
| 1748 | NG_NODE_UNREF(node); /* XXX more races */ | |
| 1749 | node = NULL; | |
| 1750 | } | |
| 1751 | } | |
| 1752 | ||
| 1753 | /* If node somehow missing, fail here (probably this is not needed) */ | |
| 1754 | if (node == NULL) { | |
| 1755 | TRAP_ERROR(); | |
| 1756 | return (ENXIO); | |
| 1757 | } | |
| 1758 | ||
| 1759 | /* Done */ | |
| 1760 | *destp = node; | |
| 1761 | if (lasthook != NULL) | |
| 1762 | *lasthook = (hook ? NG_HOOK_PEER(hook) : NULL); | |
| 1763 | return (0); | |
| 1764 | } | |
| 1765 | ||
| 1766 | /***************************************************************\ | |
| 1767 | * Input queue handling. | |
| 1768 | * All activities are submitted to the node via the input queue | |
| 1769 | * which implements a multiple-reader/single-writer gate. | |
| 1770 | * Items which cannot be handled immediately are queued. | |
| 1771 | * | |
| 1772 | * read-write queue locking inline functions * | |
| 1773 | \***************************************************************/ | |
| 1774 | ||
| 1775 | static __inline void ng_queue_rw(node_p node, item_p item, int rw); | |
| 1776 | static __inline item_p ng_dequeue(node_p node, int *rw); | |
| 1777 | static __inline item_p ng_acquire_read(node_p node, item_p item); | |
| 1778 | static __inline item_p ng_acquire_write(node_p node, item_p item); | |
| 1779 | static __inline void ng_leave_read(node_p node); | |
| 1780 | static __inline void ng_leave_write(node_p node); | |
| 1781 | ||
| 1782 | /* | |
| 1783 | * Definition of the bits fields in the ng_queue flag word. | |
| 1784 | * Defined here rather than in netgraph.h because no-one should fiddle | |
| 1785 | * with them. | |
| 1786 | * | |
| 1787 | * The ordering here may be important! don't shuffle these. | |
| 1788 | */ | |
| 1789 | /*- | |
| 1790 | Safety Barrier--------+ (adjustable to suit taste) (not used yet) | |
| 1791 | | | |
| 1792 | V | |
| 1793 | +-------+-------+-------+-------+-------+-------+-------+-------+ | |
| 1794 | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
| 1795 | | |A|c|t|i|v|e| |R|e|a|d|e|r| |C|o|u|n|t| | | | | | | | | |P|A| | |
| 1796 | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |O|W| | |
| 1797 | +-------+-------+-------+-------+-------+-------+-------+-------+ | |
| 1798 | \___________________________ ____________________________/ | | | |
| 1799 | V | | | |
| 1800 | [active reader count] | | | |
| 1801 | | | | |
| 1802 | Operation Pending -------------------------------+ | | |
| 1803 | | | |
| 1804 | Active Writer ---------------------------------------+ | |
| 1805 | ||
| 1806 | Node queue has such semantics: | |
| 1807 | - All flags modifications are atomic. | |
| 1808 | - Reader count can be incremented only if there is no writer or pending flags. | |
| 1809 | As soon as this can't be done with single operation, it is implemented with | |
| 1810 | spin loop and atomic_cmpset(). | |
| 1811 | - Writer flag can be set only if there is no any bits set. | |
| 1812 | It is implemented with atomic_cmpset(). | |
| 1813 | - Pending flag can be set any time, but to avoid collision on queue processing | |
| 1814 | all queue fields are protected by the mutex. | |
| 1815 | - Queue processing thread reads queue holding the mutex, but releases it while | |
| 1816 | processing. When queue is empty pending flag is removed. | |
| 1817 | */ | |
| 1818 | ||
| 1819 | #define WRITER_ACTIVE 0x00000001 | |
| 1820 | #define OP_PENDING 0x00000002 | |
| 1821 | #define READER_INCREMENT 0x00000004 | |
| 1822 | #define READER_MASK 0xfffffffc /* Not valid if WRITER_ACTIVE is set */ | |
| 1823 | #define SAFETY_BARRIER 0x00100000 /* 128K items queued should be enough */ | |
| 1824 | ||
| 1825 | /* Defines of more elaborate states on the queue */ | |
| 1826 | /* Mask of bits a new read cares about */ | |
| 1827 | #define NGQ_RMASK (WRITER_ACTIVE|OP_PENDING) | |
| 1828 | ||
| 1829 | /* Mask of bits a new write cares about */ | |
| 1830 | #define NGQ_WMASK (NGQ_RMASK|READER_MASK) | |
| 1831 | ||
| 1832 | /* Test to decide if there is something on the queue. */ | |
| 1833 | #define QUEUE_ACTIVE(QP) ((QP)->q_flags & OP_PENDING) | |
| 1834 | ||
| 1835 | /* How to decide what the next queued item is. */ | |
| 1836 | #define HEAD_IS_READER(QP) NGI_QUEUED_READER(STAILQ_FIRST(&(QP)->queue)) | |
| 1837 | #define HEAD_IS_WRITER(QP) NGI_QUEUED_WRITER(STAILQ_FIRST(&(QP)->queue)) /* notused */ | |
| 1838 | ||
| 1839 | /* Read the status to decide if the next item on the queue can now run. */ | |
| 1840 | #define QUEUED_READER_CAN_PROCEED(QP) \ | |
| 1841 | (((QP)->q_flags & (NGQ_RMASK & ~OP_PENDING)) == 0) | |
| 1842 | #define QUEUED_WRITER_CAN_PROCEED(QP) \ | |
| 1843 | (((QP)->q_flags & (NGQ_WMASK & ~OP_PENDING)) == 0) | |
| 1844 | ||
| 1845 | /* Is there a chance of getting ANY work off the queue? */ | |
| 1846 | #define NEXT_QUEUED_ITEM_CAN_PROCEED(QP) \ | |
| 1847 | ((HEAD_IS_READER(QP)) ? QUEUED_READER_CAN_PROCEED(QP) : \ | |
| 1848 | QUEUED_WRITER_CAN_PROCEED(QP)) | |
| 1849 | ||
| 1850 | #define NGQRW_R 0 | |
| 1851 | #define NGQRW_W 1 | |
| 1852 | ||
| 1853 | #define NGQ2_WORKQ 0x00000001 | |
| 1854 | ||
| 1855 | /* | |
| 1856 | * Taking into account the current state of the queue and node, possibly take | |
| 1857 | * the next entry off the queue and return it. Return NULL if there was | |
| 1858 | * nothing we could return, either because there really was nothing there, or | |
| 1859 | * because the node was in a state where it cannot yet process the next item | |
| 1860 | * on the queue. | |
| 1861 | */ | |
| 1862 | static __inline item_p | |
| 1863 | ng_dequeue(node_p node, int *rw) | |
| 1864 | { | |
| 1865 | item_p item; | |
| 1866 | struct ng_queue *ngq = &node->nd_input_queue; | |
| 1867 | ||
| 1868 | /* This MUST be called with the mutex held. */ | |
| 1869 | mtx_assert(&ngq->q_mtx, MA_OWNED); | |
| 1870 | ||
| 1871 | /* If there is nothing queued, then just return. */ | |
| 1872 | if (!QUEUE_ACTIVE(ngq)) { | |
| 1873 | CTR4(KTR_NET, "%20s: node [%x] (%p) queue empty; " | |
| 1874 | "queue flags 0x%lx", __func__, | |
| 1875 | node->nd_ID, node, ngq->q_flags); | |
| 1876 | return (NULL); | |
| 1877 | } | |
| 1878 | ||
| 1879 | /* | |
| 1880 | * From here, we can assume there is a head item. | |
| 1881 | * We need to find out what it is and if it can be dequeued, given | |
| 1882 | * the current state of the node. | |
| 1883 | */ | |
| 1884 | if (HEAD_IS_READER(ngq)) { | |
| 1885 | while (1) { | |
| 1886 | long t = ngq->q_flags; | |
| 1887 | if (t & WRITER_ACTIVE) { | |
| 1888 | /* There is writer, reader can't proceed. */ | |
| 1889 | CTR4(KTR_NET, "%20s: node [%x] (%p) queued reader " | |
| 1890 | "can't proceed; queue flags 0x%lx", __func__, | |
| 1891 | node->nd_ID, node, t); | |
| 1892 | return (NULL); | |
| 1893 | } | |
| 1894 | if (atomic_cmpset_acq_int(&ngq->q_flags, t, | |
| 1895 | t + READER_INCREMENT)) | |
| 1896 | break; | |
| 1897 | cpu_spinwait(); | |
| 1898 | } | |
| 1899 | /* We have got reader lock for the node. */ | |
| 1900 | *rw = NGQRW_R; | |
| 1901 | } else if (atomic_cmpset_acq_int(&ngq->q_flags, OP_PENDING, | |
| 1902 | OP_PENDING + WRITER_ACTIVE)) { | |
| 1903 | /* We have got writer lock for the node. */ | |
| 1904 | *rw = NGQRW_W; | |
| 1905 | } else { | |
| 1906 | /* There is somebody other, writer can't proceed. */ | |
| 1907 | CTR4(KTR_NET, "%20s: node [%x] (%p) queued writer " | |
| 1908 | "can't proceed; queue flags 0x%lx", __func__, | |
| 1909 | node->nd_ID, node, ngq->q_flags); | |
| 1910 | return (NULL); | |
| 1911 | } | |
| 1912 | ||
| 1913 | /* | |
| 1914 | * Now we dequeue the request (whatever it may be) and correct the | |
| 1915 | * pending flags and the next and last pointers. | |
| 1916 | */ | |
| 1917 | item = STAILQ_FIRST(&ngq->queue); | |
| 1918 | STAILQ_REMOVE_HEAD(&ngq->queue, el_next); | |
| 1919 | if (STAILQ_EMPTY(&ngq->queue)) | |
| 1920 | atomic_clear_int(&ngq->q_flags, OP_PENDING); | |
| 1921 | CTR6(KTR_NET, "%20s: node [%x] (%p) returning item %p as %s; " | |
| 1922 | "queue flags 0x%lx", __func__, | |
| 1923 | node->nd_ID, node, item, *rw ? "WRITER" : "READER" , | |
| 1924 | ngq->q_flags); | |
| 1925 | return (item); | |
| 1926 | } | |
| 1927 | ||
| 1928 | /* | |
| 1929 | * Queue a packet to be picked up later by someone else. | |
| 1930 | * If the queue could be run now, add node to the queue handler's worklist. | |
| 1931 | */ | |
| 1932 | static __inline void | |
| 1933 | ng_queue_rw(node_p node, item_p item, int rw) | |
| 1934 | { | |
| 1935 | struct ng_queue *ngq = &node->nd_input_queue; | |
| 1936 | if (rw == NGQRW_W) | |
| 1937 | NGI_SET_WRITER(item); | |
| 1938 | else | |
| 1939 | NGI_SET_READER(item); | |
| 1940 | ||
| 1941 | NG_QUEUE_LOCK(ngq); | |
| 1942 | /* Set OP_PENDING flag and enqueue the item. */ | |
| 1943 | atomic_set_int(&ngq->q_flags, OP_PENDING); | |
| 1944 | STAILQ_INSERT_TAIL(&ngq->queue, item, el_next); | |
| 1945 | ||
| 1946 | CTR5(KTR_NET, "%20s: node [%x] (%p) queued item %p as %s", __func__, | |
| 1947 | node->nd_ID, node, item, rw ? "WRITER" : "READER" ); | |
| 1948 | ||
| 1949 | /* | |
| 1950 | * We can take the worklist lock with the node locked | |
| 1951 | * BUT NOT THE REVERSE! | |
| 1952 | */ | |
| 1953 | if (NEXT_QUEUED_ITEM_CAN_PROCEED(ngq)) | |
| 1954 | ng_worklist_add(node); | |
| 1955 | NG_QUEUE_UNLOCK(ngq); | |
| 1956 | } | |
| 1957 | ||
| 1958 | /* Acquire reader lock on node. If node is busy, queue the packet. */ | |
| 1959 | static __inline item_p | |
| 1960 | ng_acquire_read(node_p node, item_p item) | |
| 1961 | { | |
| 1962 | KASSERT(node != &ng_deadnode, | |
| 1963 | ("%s: working on deadnode", __func__)); | |
| 1964 | ||
| 1965 | /* Reader needs node without writer and pending items. */ | |
| 1966 | while (1) { | |
| 1967 | long t = node->nd_input_queue.q_flags; | |
| 1968 | if (t & NGQ_RMASK) | |
| 1969 | break; /* Node is not ready for reader. */ | |
| 1970 | if (atomic_cmpset_acq_int(&node->nd_input_queue.q_flags, | |
| 1971 | t, t + READER_INCREMENT)) { | |
| 1972 | /* Successfully grabbed node */ | |
| 1973 | CTR4(KTR_NET, "%20s: node [%x] (%p) acquired item %p", | |
| 1974 | __func__, node->nd_ID, node, item); | |
| 1975 | return (item); | |
| 1976 | } | |
| 1977 | cpu_spinwait(); | |
| 1978 | }; | |
| 1979 | ||
| 1980 | /* Queue the request for later. */ | |
| 1981 | ng_queue_rw(node, item, NGQRW_R); | |
| 1982 | ||
| 1983 | return (NULL); | |
| 1984 | } | |
| 1985 | ||
| 1986 | /* Acquire writer lock on node. If node is busy, queue the packet. */ | |
| 1987 | static __inline item_p | |
| 1988 | ng_acquire_write(node_p node, item_p item) | |
| 1989 | { | |
| 1990 | KASSERT(node != &ng_deadnode, | |
| 1991 | ("%s: working on deadnode", __func__)); | |
| 1992 | ||
| 1993 | /* Writer needs completely idle node. */ | |
| 1994 | if (atomic_cmpset_acq_int(&node->nd_input_queue.q_flags, | |
| 1995 | 0, WRITER_ACTIVE)) { | |
| 1996 | /* Successfully grabbed node */ | |
| 1997 | CTR4(KTR_NET, "%20s: node [%x] (%p) acquired item %p", | |
| 1998 | __func__, node->nd_ID, node, item); | |
| 1999 | return (item); | |
| 2000 | } | |
| 2001 | ||
| 2002 | /* Queue the request for later. */ | |
| 2003 | ng_queue_rw(node, item, NGQRW_W); | |
| 2004 | ||
| 2005 | return (NULL); | |
| 2006 | } | |
| 2007 | ||
| 2008 | #if 0 | |
| 2009 | static __inline item_p | |
| 2010 | ng_upgrade_write(node_p node, item_p item) | |
| 2011 | { | |
| 2012 | struct ng_queue *ngq = &node->nd_input_queue; | |
| 2013 | KASSERT(node != &ng_deadnode, | |
| 2014 | ("%s: working on deadnode", __func__)); | |
| 2015 | ||
| 2016 | NGI_SET_WRITER(item); | |
| 2017 | ||
| 2018 | NG_QUEUE_LOCK(ngq); | |
| 2019 | ||
| 2020 | /* | |
| 2021 | * There will never be no readers as we are there ourselves. | |
| 2022 | * Set the WRITER_ACTIVE flags ASAP to block out fast track readers. | |
| 2023 | * The caller we are running from will call ng_leave_read() | |
| 2024 | * soon, so we must account for that. We must leave again with the | |
| 2025 | * READER lock. If we find other readers, then | |
| 2026 | * queue the request for later. However "later" may be rignt now | |
| 2027 | * if there are no readers. We don't really care if there are queued | |
| 2028 | * items as we will bypass them anyhow. | |
| 2029 | */ | |
| 2030 | atomic_add_int(&ngq->q_flags, WRITER_ACTIVE - READER_INCREMENT); | |
| 2031 | if ((ngq->q_flags & (NGQ_WMASK & ~OP_PENDING)) == WRITER_ACTIVE) { | |
| 2032 | NG_QUEUE_UNLOCK(ngq); | |
| 2033 | ||
| 2034 | /* It's just us, act on the item. */ | |
| 2035 | /* will NOT drop writer lock when done */ | |
| 2036 | ng_apply_item(node, item, 0); | |
| 2037 | ||
| 2038 | /* | |
| 2039 | * Having acted on the item, atomically | |
| 2040 | * down grade back to READER and finish up | |
| 2041 | */ | |
| 2042 | atomic_add_int(&ngq->q_flags, | |
| 2043 | READER_INCREMENT - WRITER_ACTIVE); | |
| 2044 | ||
| 2045 | /* Our caller will call ng_leave_read() */ | |
| 2046 | return; | |
| 2047 | } | |
| 2048 | /* | |
| 2049 | * It's not just us active, so queue us AT THE HEAD. | |
| 2050 | * "Why?" I hear you ask. | |
| 2051 | * Put us at the head of the queue as we've already been | |
| 2052 | * through it once. If there is nothing else waiting, | |
| 2053 | * set the correct flags. | |
| 2054 | */ | |
| 2055 | if (STAILQ_EMPTY(&ngq->queue)) { | |
| 2056 | /* We've gone from, 0 to 1 item in the queue */ | |
| 2057 | atomic_set_int(&ngq->q_flags, OP_PENDING); | |
| 2058 | ||
| 2059 | CTR3(KTR_NET, "%20s: node [%x] (%p) set OP_PENDING", __func__, | |
| 2060 | node->nd_ID, node); | |
| 2061 | }; | |
| 2062 | STAILQ_INSERT_HEAD(&ngq->queue, item, el_next); | |
| 2063 | CTR4(KTR_NET, "%20s: node [%x] (%p) requeued item %p as WRITER", | |
| 2064 | __func__, node->nd_ID, node, item ); | |
| 2065 | ||
| 2066 | /* Reverse what we did above. That downgrades us back to reader */ | |
| 2067 | atomic_add_int(&ngq->q_flags, READER_INCREMENT - WRITER_ACTIVE); | |
| 2068 | if (QUEUE_ACTIVE(ngq) && NEXT_QUEUED_ITEM_CAN_PROCEED(ngq)) | |
| 2069 | ng_worklist_add(node); | |
| 2070 | NG_QUEUE_UNLOCK(ngq); | |
| 2071 | ||
| 2072 | return; | |
| 2073 | } | |
| 2074 | #endif | |
| 2075 | ||
| 2076 | /* Release reader lock. */ | |
| 2077 | static __inline void | |
| 2078 | ng_leave_read(node_p node) | |
| 2079 | { | |
| 2080 | atomic_subtract_rel_int(&node->nd_input_queue.q_flags, READER_INCREMENT); | |
| 2081 | } | |
| 2082 | ||
| 2083 | /* Release writer lock. */ | |
| 2084 | static __inline void | |
| 2085 | ng_leave_write(node_p node) | |
| 2086 | { | |
| 2087 | atomic_clear_rel_int(&node->nd_input_queue.q_flags, WRITER_ACTIVE); | |
| 2088 | } | |
| 2089 | ||
| 2090 | /* Purge node queue. Called on node shutdown. */ | |
| 2091 | static void | |
| 2092 | ng_flush_input_queue(node_p node) | |
| 2093 | { | |
| 2094 | struct ng_queue *ngq = &node->nd_input_queue; | |
| 2095 | item_p item; | |
| 2096 | ||
| 2097 | NG_QUEUE_LOCK(ngq); | |
| 2098 | while ((item = STAILQ_FIRST(&ngq->queue)) != NULL) { | |
| 2099 | STAILQ_REMOVE_HEAD(&ngq->queue, el_next); | |
| 2100 | if (STAILQ_EMPTY(&ngq->queue)) | |
| 2101 | atomic_clear_int(&ngq->q_flags, OP_PENDING); | |
| 2102 | NG_QUEUE_UNLOCK(ngq); | |
| 2103 | ||
| 2104 | /* If the item is supplying a callback, call it with an error */ | |
| 2105 | if (item->apply != NULL) { | |
| 2106 | if (item->depth == 1) | |
| 2107 | item->apply->error = ENOENT; | |
| 2108 | if (refcount_release(&item->apply->refs)) { | |
| 2109 | (*item->apply->apply)(item->apply->context, | |
| 2110 | item->apply->error); | |
| 2111 | } | |
| 2112 | } | |
| 2113 | NG_FREE_ITEM(item); | |
| 2114 | NG_QUEUE_LOCK(ngq); | |
| 2115 | } | |
| 2116 | NG_QUEUE_UNLOCK(ngq); | |
| 2117 | } | |
| 2118 | ||
| 2119 | /*********************************************************************** | |
| 2120 | * Externally visible method for sending or queueing messages or data. | |
| 2121 | ***********************************************************************/ | |
| 2122 | ||
| 2123 | /* | |
| 2124 | * The module code should have filled out the item correctly by this stage: | |
| 2125 | * Common: | |
| 2126 | * reference to destination node. | |
| 2127 | * Reference to destination rcv hook if relevant. | |
| 2128 | * apply pointer must be or NULL or reference valid struct ng_apply_info. | |
| 2129 | * Data: | |
| 2130 | * pointer to mbuf | |
| 2131 | * Control_Message: | |
| 2132 | * pointer to msg. | |
| 2133 | * ID of original sender node. (return address) | |
| 2134 | * Function: | |
| 2135 | * Function pointer | |
| 2136 | * void * argument | |
| 2137 | * integer argument | |
| 2138 | * | |
| 2139 | * The nodes have several routines and macros to help with this task: | |
| 2140 | */ | |
| 2141 | ||
| 2142 | int | |
| 2143 | ng_snd_item(item_p item, int flags) | |
| 2144 | { | |
| 2145 | hook_p hook; | |
| 2146 | node_p node; | |
| 2147 | int queue, rw; | |
| 2148 | struct ng_queue *ngq; | |
| 2149 | int error = 0; | |
| 2150 | ||
| 2151 | /* We are sending item, so it must be present! */ | |
| 2152 | KASSERT(item != NULL, ("ng_snd_item: item is NULL")); | |
| 2153 | ||
| 2154 | #ifdef NETGRAPH_DEBUG | |
| 2155 | _ngi_check(item, __FILE__, __LINE__); | |
| 2156 | #endif | |
| 2157 | ||
| 2158 | /* Item was sent once more, postpone apply() call. */ | |
| 2159 | if (item->apply) | |
| 2160 | refcount_acquire(&item->apply->refs); | |
| 2161 | ||
| 2162 | node = NGI_NODE(item); | |
| 2163 | /* Node is never optional. */ | |
| 2164 | KASSERT(node != NULL, ("ng_snd_item: node is NULL")); | |
| 2165 | ||
| 2166 | hook = NGI_HOOK(item); | |
| 2167 | /* Valid hook and mbuf are mandatory for data. */ | |
| 2168 | if ((item->el_flags & NGQF_TYPE) == NGQF_DATA) { | |
| 2169 | KASSERT(hook != NULL, ("ng_snd_item: hook for data is NULL")); | |
| 2170 | if (NGI_M(item) == NULL) | |
| 2171 | ERROUT(EINVAL); | |
| 2172 | CHECK_DATA_MBUF(NGI_M(item)); | |
| 2173 | } | |
| 2174 | ||
| 2175 | /* | |
| 2176 | * If the item or the node specifies single threading, force | |
| 2177 | * writer semantics. Similarly, the node may say one hook always | |
| 2178 | * produces writers. These are overrides. | |
| 2179 | */ | |
| 2180 | if (((item->el_flags & NGQF_RW) == NGQF_WRITER) || | |
| 2181 | (node->nd_flags & NGF_FORCE_WRITER) || | |
| 2182 | (hook && (hook->hk_flags & HK_FORCE_WRITER))) { | |
| 2183 | rw = NGQRW_W; | |
| 2184 | } else { | |
| 2185 | rw = NGQRW_R; | |
| 2186 | } | |
| 2187 | ||
| 2188 | /* | |
| 2189 | * If sender or receiver requests queued delivery or stack usage | |
| 2190 | * level is dangerous - enqueue message. | |
| 2191 | */ | |
| 2192 | if ((flags & NG_QUEUE) || (hook && (hook->hk_flags & HK_QUEUE))) { | |
| 2193 | queue = 1; | |
| 2194 | } else { | |
| 2195 | queue = 0; | |
| 2196 | #ifdef GET_STACK_USAGE | |
| 2197 | /* | |
| 2198 | * Most of netgraph nodes have small stack consumption and | |
| 2199 | * for them 25% of free stack space is more than enough. | |
| 2200 | * Nodes/hooks with higher stack usage should be marked as | |
| 2201 | * HI_STACK. For them 50% of stack will be guaranteed then. | |
| 2202 | * XXX: Values 25% and 50% are completely empirical. | |
| 2203 | */ | |
| 2204 | size_t st, su, sl; | |
| 2205 | GET_STACK_USAGE(st, su); | |
| 2206 | sl = st - su; | |
| 2207 | if ((sl * 4 < st) || | |
| 2208 | ((sl * 2 < st) && ((node->nd_flags & NGF_HI_STACK) || | |
| 2209 | (hook && (hook->hk_flags & HK_HI_STACK))))) { | |
| 2210 | queue = 1; | |
| 2211 | } | |
| 2212 | #endif | |
| 2213 | } | |
| 2214 | ||
| 2215 | if (queue) { | |
| 2216 | item->depth = 1; | |
| 2217 | /* Put it on the queue for that node*/ | |
| 2218 | ng_queue_rw(node, item, rw); | |
| 2219 | return ((flags & NG_PROGRESS) ? EINPROGRESS : 0); | |
| 2220 | } | |
| 2221 | ||
| 2222 | /* | |
| 2223 | * We already decided how we will be queueud or treated. | |
| 2224 | * Try get the appropriate operating permission. | |
| 2225 | */ | |
| 2226 | if (rw == NGQRW_R) | |
| 2227 | item = ng_acquire_read(node, item); | |
| 2228 | else | |
| 2229 | item = ng_acquire_write(node, item); | |
| 2230 | ||
| 2231 | /* Item was queued while trying to get permission. */ | |
| 2232 | if (item == NULL) | |
| 2233 | return ((flags & NG_PROGRESS) ? EINPROGRESS : 0); | |
| 2234 | ||
| 2235 | NGI_GET_NODE(item, node); /* zaps stored node */ | |
| 2236 | ||
| 2237 | item->depth++; | |
| 2238 | error = ng_apply_item(node, item, rw); /* drops r/w lock when done */ | |
| 2239 | ||
| 2240 | /* If something is waiting on queue and ready, schedule it. */ | |
| 2241 | ngq = &node->nd_input_queue; | |
| 2242 | if (QUEUE_ACTIVE(ngq)) { | |
| 2243 | NG_QUEUE_LOCK(ngq); | |
| 2244 | if (QUEUE_ACTIVE(ngq) && NEXT_QUEUED_ITEM_CAN_PROCEED(ngq)) | |
| 2245 | ng_worklist_add(node); | |
| 2246 | NG_QUEUE_UNLOCK(ngq); | |
| 2247 | } | |
| 2248 | ||
| 2249 | /* | |
| 2250 | * Node may go away as soon as we remove the reference. | |
| 2251 | * Whatever we do, DO NOT access the node again! | |
| 2252 | */ | |
| 2253 | NG_NODE_UNREF(node); | |
| 2254 | ||
| 2255 | return (error); | |
| 2256 | ||
| 2257 | done: | |
| 2258 | /* If was not sent, apply callback here. */ | |
| 2259 | if (item->apply != NULL) { | |
| 2260 | if (item->depth == 0 && error != 0) | |
| 2261 | item->apply->error = error; | |
| 2262 | if (refcount_release(&item->apply->refs)) { | |
| 2263 | (*item->apply->apply)(item->apply->context, | |
| 2264 | item->apply->error); | |
| 2265 | } | |
| 2266 | } | |
| 2267 | ||
| 2268 | NG_FREE_ITEM(item); | |
| 2269 | return (error); | |
| 2270 | } | |
| 2271 | ||
| 2272 | /* | |
| 2273 | * We have an item that was possibly queued somewhere. | |
| 2274 | * It should contain all the information needed | |
| 2275 | * to run it on the appropriate node/hook. | |
| 2276 | * If there is apply pointer and we own the last reference, call apply(). | |
| 2277 | */ | |
| 2278 | static int | |
| 2279 | ng_apply_item(node_p node, item_p item, int rw) | |
| 2280 | { | |
| 2281 | hook_p hook; | |
| 2282 | ng_rcvdata_t *rcvdata; | |
| 2283 | ng_rcvmsg_t *rcvmsg; | |
| 2284 | struct ng_apply_info *apply; | |
| 2285 | int error = 0, depth; | |
| 2286 | ||
| 2287 | /* Node and item are never optional. */ | |
| 2288 | KASSERT(node != NULL, ("ng_apply_item: node is NULL")); | |
| 2289 | KASSERT(item != NULL, ("ng_apply_item: item is NULL")); | |
| 2290 | ||
| 2291 | NGI_GET_HOOK(item, hook); /* clears stored hook */ | |
| 2292 | #ifdef NETGRAPH_DEBUG | |
| 2293 | _ngi_check(item, __FILE__, __LINE__); | |
| 2294 | #endif | |
| 2295 | ||
| 2296 | apply = item->apply; | |
| 2297 | depth = item->depth; | |
| 2298 | ||
| 2299 | switch (item->el_flags & NGQF_TYPE) { | |
| 2300 | case NGQF_DATA: | |
| 2301 | /* | |
| 2302 | * Check things are still ok as when we were queued. | |
| 2303 | */ | |
| 2304 | KASSERT(hook != NULL, ("ng_apply_item: hook for data is NULL")); | |
| 2305 | if (NG_HOOK_NOT_VALID(hook) || | |
| 2306 | NG_NODE_NOT_VALID(node)) { | |
| 2307 | error = EIO; | |
| 2308 | NG_FREE_ITEM(item); | |
| 2309 | break; | |
| 2310 | } | |
| 2311 | /* | |
| 2312 | * If no receive method, just silently drop it. | |
| 2313 | * Give preference to the hook over-ride method | |
| 2314 | */ | |
| 2315 | if ((!(rcvdata = hook->hk_rcvdata)) | |
| 2316 | && (!(rcvdata = NG_HOOK_NODE(hook)->nd_type->rcvdata))) { | |
| 2317 | error = 0; | |
| 2318 | NG_FREE_ITEM(item); | |
| 2319 | break; | |
| 2320 | } | |
| 2321 | error = (*rcvdata)(hook, item); | |
| 2322 | break; | |
| 2323 | case NGQF_MESG: | |
| 2324 | if (hook && NG_HOOK_NOT_VALID(hook)) { | |
| 2325 | /* | |
| 2326 | * The hook has been zapped then we can't use it. | |
| 2327 | * Immediately drop its reference. | |
| 2328 | * The message may not need it. | |
| 2329 | */ | |
| 2330 | NG_HOOK_UNREF(hook); | |
| 2331 | hook = NULL; | |
| 2332 | } | |
| 2333 | /* | |
| 2334 | * Similarly, if the node is a zombie there is | |
| 2335 | * nothing we can do with it, drop everything. | |
| 2336 | */ | |
| 2337 | if (NG_NODE_NOT_VALID(node)) { | |
| 2338 | TRAP_ERROR(); | |
| 2339 | error = EINVAL; | |
| 2340 | NG_FREE_ITEM(item); | |
| 2341 | break; | |
| 2342 | } | |
| 2343 | /* | |
| 2344 | * Call the appropriate message handler for the object. | |
| 2345 | * It is up to the message handler to free the message. | |
| 2346 | * If it's a generic message, handle it generically, | |
| 2347 | * otherwise call the type's message handler (if it exists). | |
| 2348 | * XXX (race). Remember that a queued message may | |
| 2349 | * reference a node or hook that has just been | |
| 2350 | * invalidated. It will exist as the queue code | |
| 2351 | * is holding a reference, but.. | |
| 2352 | */ | |
| 2353 | if ((NGI_MSG(item)->header.typecookie == NGM_GENERIC_COOKIE) && | |
| 2354 | ((NGI_MSG(item)->header.flags & NGF_RESP) == 0)) { | |
| 2355 | error = ng_generic_msg(node, item, hook); | |
| 2356 | break; | |
| 2357 | } | |
| 2358 | if (((!hook) || (!(rcvmsg = hook->hk_rcvmsg))) && | |
| 2359 | (!(rcvmsg = node->nd_type->rcvmsg))) { | |
| 2360 | TRAP_ERROR(); | |
| 2361 | error = 0; | |
| 2362 | NG_FREE_ITEM(item); | |
| 2363 | break; | |
| 2364 | } | |
| 2365 | error = (*rcvmsg)(node, item, hook); | |
| 2366 | break; | |
| 2367 | case NGQF_FN: | |
| 2368 | case NGQF_FN2: | |
| 2369 | /* | |
| 2370 | * We have to implicitly trust the hook, | |
| 2371 | * as some of these are used for system purposes | |
| 2372 | * where the hook is invalid. In the case of | |
| 2373 | * the shutdown message we allow it to hit | |
| 2374 | * even if the node is invalid. | |
| 2375 | */ | |
| 2376 | if ((NG_NODE_NOT_VALID(node)) | |
| 2377 | && (NGI_FN(item) != &ng_rmnode)) { | |
| 2378 | TRAP_ERROR(); | |
| 2379 | error = EINVAL; | |
| 2380 | NG_FREE_ITEM(item); | |
| 2381 | break; | |
| 2382 | } | |
| 2383 | if ((item->el_flags & NGQF_TYPE) == NGQF_FN) { | |
| 2384 | (*NGI_FN(item))(node, hook, NGI_ARG1(item), | |
| 2385 | NGI_ARG2(item)); | |
| 2386 | NG_FREE_ITEM(item); | |
| 2387 | } else /* it is NGQF_FN2 */ | |
| 2388 | error = (*NGI_FN2(item))(node, item, hook); | |
| 2389 | break; | |
| 2390 | } | |
| 2391 | /* | |
| 2392 | * We held references on some of the resources | |
| 2393 | * that we took from the item. Now that we have | |
| 2394 | * finished doing everything, drop those references. | |
| 2395 | */ | |
| 2396 | if (hook) | |
| 2397 | NG_HOOK_UNREF(hook); | |
| 2398 | ||
| 2399 | if (rw == NGQRW_R) | |
| 2400 | ng_leave_read(node); | |
| 2401 | else | |
| 2402 | ng_leave_write(node); | |
| 2403 | ||
| 2404 | /* Apply callback. */ | |
| 2405 | if (apply != NULL) { | |
| 2406 | if (depth == 1 && error != 0) | |
| 2407 | apply->error = error; | |
| 2408 | if (refcount_release(&apply->refs)) | |
| 2409 | (*apply->apply)(apply->context, apply->error); | |
| 2410 | } | |
| 2411 | ||
| 2412 | return (error); | |
| 2413 | } | |
| 2414 | ||
| 2415 | /*********************************************************************** | |
| 2416 | * Implement the 'generic' control messages | |
| 2417 | ***********************************************************************/ | |
| 2418 | static int | |
| 2419 | ng_generic_msg(node_p here, item_p item, hook_p lasthook) | |
| 2420 | { | |
| 2421 | int error = 0; | |
| 2422 | struct ng_mesg *msg; | |
| 2423 | struct ng_mesg *resp = NULL; | |
| 2424 | ||
| 2425 | NGI_GET_MSG(item, msg); | |
| 2426 | if (msg->header.typecookie != NGM_GENERIC_COOKIE) { | |
| 2427 | TRAP_ERROR(); | |
| 2428 | error = EINVAL; | |
| 2429 | goto out; | |
| 2430 | } | |
| 2431 | switch (msg->header.cmd) { | |
| 2432 | case NGM_SHUTDOWN: | |
| 2433 | ng_rmnode(here, NULL, NULL, 0); | |
| 2434 | break; | |
| 2435 | case NGM_MKPEER: | |
| 2436 | { | |
| 2437 | struct ngm_mkpeer *const mkp = (struct ngm_mkpeer *) msg->data; | |
| 2438 | ||
| 2439 | if (msg->header.arglen != sizeof(*mkp)) { | |
| 2440 | TRAP_ERROR(); | |
| 2441 | error = EINVAL; | |
| 2442 | break; | |
| 2443 | } | |
| 2444 | mkp->type[sizeof(mkp->type) - 1] = '\0'; | |
| 2445 | mkp->ourhook[sizeof(mkp->ourhook) - 1] = '\0'; | |
| 2446 | mkp->peerhook[sizeof(mkp->peerhook) - 1] = '\0'; | |
| 2447 | error = ng_mkpeer(here, mkp->ourhook, mkp->peerhook, mkp->type); | |
| 2448 | break; | |
| 2449 | } | |
| 2450 | case NGM_CONNECT: | |
| 2451 | { | |
| 2452 | struct ngm_connect *const con = | |
| 2453 | (struct ngm_connect *) msg->data; | |
| 2454 | node_p node2; | |
| 2455 | ||
| 2456 | if (msg->header.arglen != sizeof(*con)) { | |
| 2457 | TRAP_ERROR(); | |
| 2458 | error = EINVAL; | |
| 2459 | break; | |
| 2460 | } | |
| 2461 | con->path[sizeof(con->path) - 1] = '\0'; | |
| 2462 | con->ourhook[sizeof(con->ourhook) - 1] = '\0'; | |
| 2463 | con->peerhook[sizeof(con->peerhook) - 1] = '\0'; | |
| 2464 | /* Don't forget we get a reference.. */ | |
| 2465 | error = ng_path2noderef(here, con->path, &node2, NULL); | |
| 2466 | if (error) | |
| 2467 | break; | |
| 2468 | error = ng_con_nodes(item, here, con->ourhook, | |
| 2469 | node2, con->peerhook); | |
| 2470 | NG_NODE_UNREF(node2); | |
| 2471 | break; | |
| 2472 | } | |
| 2473 | case NGM_NAME: | |
| 2474 | { | |
| 2475 | struct ngm_name *const nam = (struct ngm_name *) msg->data; | |
| 2476 | ||
| 2477 | if (msg->header.arglen != sizeof(*nam)) { | |
| 2478 | TRAP_ERROR(); | |
| 2479 | error = EINVAL; | |
| 2480 | break; | |
| 2481 | } | |
| 2482 | nam->name[sizeof(nam->name) - 1] = '\0'; | |
| 2483 | error = ng_name_node(here, nam->name); | |
| 2484 | break; | |
| 2485 | } | |
| 2486 | case NGM_RMHOOK: | |
| 2487 | { | |
| 2488 | struct ngm_rmhook *const rmh = (struct ngm_rmhook *) msg->data; | |
| 2489 | hook_p hook; | |
| 2490 | ||
| 2491 | if (msg->header.arglen != sizeof(*rmh)) { | |
| 2492 | TRAP_ERROR(); | |
| 2493 | error = EINVAL; | |
| 2494 | break; | |
| 2495 | } | |
| 2496 | rmh->ourhook[sizeof(rmh->ourhook) - 1] = '\0'; | |
| 2497 | if ((hook = ng_findhook(here, rmh->ourhook)) != NULL) | |
| 2498 | ng_destroy_hook(hook); | |
| 2499 | break; | |
| 2500 | } | |
| 2501 | case NGM_NODEINFO: | |
| 2502 | { | |
| 2503 | struct nodeinfo *ni; | |
| 2504 | ||
| 5a975a3d | 2505 | NG_MKRESPONSE(resp, msg, sizeof(*ni), M_WAITOK | M_NULLOK); |
| b06ebda0 MD |
2506 | if (resp == NULL) { |
| 2507 | error = ENOMEM; | |
| 2508 | break; | |
| 2509 | } | |
| 2510 | ||
| 2511 | /* Fill in node info */ | |
| 2512 | ni = (struct nodeinfo *) resp->data; | |
| 2513 | if (NG_NODE_HAS_NAME(here)) | |
| 2514 | strcpy(ni->name, NG_NODE_NAME(here)); | |
| 2515 | strcpy(ni->type, here->nd_type->name); | |
| 2516 | ni->id = ng_node2ID(here); | |
| 2517 | ni->hooks = here->nd_numhooks; | |
| 2518 | break; | |
| 2519 | } | |
| 2520 | case NGM_LISTHOOKS: | |
| 2521 | { | |
| 2522 | const int nhooks = here->nd_numhooks; | |
| 2523 | struct hooklist *hl; | |
| 2524 | struct nodeinfo *ni; | |
| 2525 | hook_p hook; | |
| 2526 | ||
| 2527 | /* Get response struct */ | |
| 2528 | NG_MKRESPONSE(resp, msg, sizeof(*hl) | |
| 5a975a3d | 2529 | + (nhooks * sizeof(struct linkinfo)), M_WAITOK | M_NULLOK); |
| b06ebda0 MD |
2530 | if (resp == NULL) { |
| 2531 | error = ENOMEM; | |
| 2532 | break; | |
| 2533 | } | |
| 2534 | hl = (struct hooklist *) resp->data; | |
| 2535 | ni = &hl->nodeinfo; | |
| 2536 | ||
| 2537 | /* Fill in node info */ | |
| 2538 | if (NG_NODE_HAS_NAME(here)) | |
| 2539 | strcpy(ni->name, NG_NODE_NAME(here)); | |
| 2540 | strcpy(ni->type, here->nd_type->name); | |
| 2541 | ni->id = ng_node2ID(here); | |
| 2542 | ||
| 2543 | /* Cycle through the linked list of hooks */ | |
| 2544 | ni->hooks = 0; | |
| 2545 | LIST_FOREACH(hook, &here->nd_hooks, hk_hooks) { | |
| 2546 | struct linkinfo *const link = &hl->link[ni->hooks]; | |
| 2547 | ||
| 2548 | if (ni->hooks >= nhooks) { | |
| 2549 | log(LOG_ERR, "%s: number of %s changed\n", | |
| 2550 | __func__, "hooks"); | |
| 2551 | break; | |
| 2552 | } | |
| 2553 | if (NG_HOOK_NOT_VALID(hook)) | |
| 2554 | continue; | |
| 2555 | strcpy(link->ourhook, NG_HOOK_NAME(hook)); | |
| 2556 | strcpy(link->peerhook, NG_PEER_HOOK_NAME(hook)); | |
| 2557 | if (NG_PEER_NODE_NAME(hook)[0] != '\0') | |
| 2558 | strcpy(link->nodeinfo.name, | |
| 2559 | NG_PEER_NODE_NAME(hook)); | |
| 2560 | strcpy(link->nodeinfo.type, | |
| 2561 | NG_PEER_NODE(hook)->nd_type->name); | |
| 2562 | link->nodeinfo.id = ng_node2ID(NG_PEER_NODE(hook)); | |
| 2563 | link->nodeinfo.hooks = NG_PEER_NODE(hook)->nd_numhooks; | |
| 2564 | ni->hooks++; | |
| 2565 | } | |
| 2566 | break; | |
| 2567 | } | |
| 2568 | ||
| 2569 | case NGM_LISTNAMES: | |
| 2570 | case NGM_LISTNODES: | |
| 2571 | { | |
| 2572 | const int unnamed = (msg->header.cmd == NGM_LISTNODES); | |
| 2573 | struct namelist *nl; | |
| 2574 | node_p node; | |
| 2575 | int num = 0, i; | |
| 2576 | ||
| 2577 | mtx_lock(&ng_namehash_mtx); | |
| 2578 | /* Count number of nodes */ | |
| 2579 | for (i = 0; i < NG_NAME_HASH_SIZE; i++) { | |
| 2580 | LIST_FOREACH(node, &ng_name_hash[i], nd_nodes) { | |
| 2581 | if (NG_NODE_IS_VALID(node) && | |
| 2582 | (unnamed || NG_NODE_HAS_NAME(node))) { | |
| 2583 | num++; | |
| 2584 | } | |
| 2585 | } | |
| 2586 | } | |
| 2587 | mtx_unlock(&ng_namehash_mtx); | |
| 2588 | ||
| 2589 | /* Get response struct */ | |
| 2590 | NG_MKRESPONSE(resp, msg, sizeof(*nl) | |
| 5a975a3d | 2591 | + (num * sizeof(struct nodeinfo)), M_WAITOK | M_NULLOK); |
| b06ebda0 MD |
2592 | if (resp == NULL) { |
| 2593 | error = ENOMEM; | |
| 2594 | break; | |
| 2595 | } | |
| 2596 | nl = (struct namelist *) resp->data; | |
| 2597 | ||
| 2598 | /* Cycle through the linked list of nodes */ | |
| 2599 | nl->numnames = 0; | |
| 2600 | mtx_lock(&ng_namehash_mtx); | |
| 2601 | for (i = 0; i < NG_NAME_HASH_SIZE; i++) { | |
| 2602 | LIST_FOREACH(node, &ng_name_hash[i], nd_nodes) { | |
| 2603 | struct nodeinfo *const np = | |
| 2604 | &nl->nodeinfo[nl->numnames]; | |
| 2605 | ||
| 2606 | if (NG_NODE_NOT_VALID(node)) | |
| 2607 | continue; | |
| 2608 | if (!unnamed && (! NG_NODE_HAS_NAME(node))) | |
| 2609 | continue; | |
| 2610 | if (nl->numnames >= num) { | |
| 2611 | log(LOG_ERR, "%s: number of nodes changed\n", | |
| 2612 | __func__); | |
| 2613 | break; | |
| 2614 | } | |
| 2615 | if (NG_NODE_HAS_NAME(node)) | |
| 2616 | strcpy(np->name, NG_NODE_NAME(node)); | |
| 2617 | strcpy(np->type, node->nd_type->name); | |
| 2618 | np->id = ng_node2ID(node); | |
| 2619 | np->hooks = node->nd_numhooks; | |
| 2620 | nl->numnames++; | |
| 2621 | } | |
| 2622 | } | |
| 2623 | mtx_unlock(&ng_namehash_mtx); | |
| 2624 | break; | |
| 2625 | } | |
| 2626 | ||
| 2627 | case NGM_LISTTYPES: | |
| 2628 | { | |
| 2629 | struct typelist *tl; | |
| 2630 | struct ng_type *type; | |
| 2631 | int num = 0; | |
| 2632 | ||
| 2633 | mtx_lock(&ng_typelist_mtx); | |
| 2634 | /* Count number of types */ | |
| 2635 | LIST_FOREACH(type, &ng_typelist, types) { | |
| 2636 | num++; | |
| 2637 | } | |
| 2638 | mtx_unlock(&ng_typelist_mtx); | |
| 2639 | ||
| 2640 | /* Get response struct */ | |
| 2641 | NG_MKRESPONSE(resp, msg, sizeof(*tl) | |
| 5a975a3d | 2642 | + (num * sizeof(struct typeinfo)), M_WAITOK | M_NULLOK); |
| b06ebda0 MD |
2643 | if (resp == NULL) { |
| 2644 | error = ENOMEM; | |
| 2645 | break; | |
| 2646 | } | |
| 2647 | tl = (struct typelist *) resp->data; | |
| 2648 | ||
| 2649 | /* Cycle through the linked list of types */ | |
| 2650 | tl->numtypes = 0; | |
| 2651 | mtx_lock(&ng_typelist_mtx); | |
| 2652 | LIST_FOREACH(type, &ng_typelist, types) { | |
| 2653 | struct typeinfo *const tp = &tl->typeinfo[tl->numtypes]; | |
| 2654 | ||
| 2655 | if (tl->numtypes >= num) { | |
| 2656 | log(LOG_ERR, "%s: number of %s changed\n", | |
| 2657 | __func__, "types"); | |
| 2658 | break; | |
| 2659 | } | |
| 2660 | strcpy(tp->type_name, type->name); | |
| 2661 | tp->numnodes = type->refs - 1; /* don't count list */ | |
| 2662 | tl->numtypes++; | |
| 2663 | } | |
| 2664 | mtx_unlock(&ng_typelist_mtx); | |
| 2665 | break; | |
| 2666 | } | |
| 2667 | ||
| 2668 | case NGM_BINARY2ASCII: | |
| 2669 | { | |
| 2670 | int bufSize = 20 * 1024; /* XXX hard coded constant */ | |
| 2671 | const struct ng_parse_type *argstype; | |
| 2672 | const struct ng_cmdlist *c; | |
| 2673 | struct ng_mesg *binary, *ascii; | |
| 2674 | ||
| 2675 | /* Data area must contain a valid netgraph message */ | |
| 2676 | binary = (struct ng_mesg *)msg->data; | |
| 2677 | if (msg->header.arglen < sizeof(struct ng_mesg) || | |
| 2678 | (msg->header.arglen - sizeof(struct ng_mesg) < | |
| 2679 | binary->header.arglen)) { | |
| 2680 | TRAP_ERROR(); | |
| 2681 | error = EINVAL; | |
| 2682 | break; | |
| 2683 | } | |
| 2684 | ||
| 2685 | /* Get a response message with lots of room */ | |
| 5a975a3d | 2686 | NG_MKRESPONSE(resp, msg, sizeof(*ascii) + bufSize, M_WAITOK | M_NULLOK); |
| b06ebda0 MD |
2687 | if (resp == NULL) { |
| 2688 | error = ENOMEM; | |
| 2689 | break; | |
| 2690 | } | |
| 2691 | ascii = (struct ng_mesg *)resp->data; | |
| 2692 | ||
| 2693 | /* Copy binary message header to response message payload */ | |
| 2694 | bcopy(binary, ascii, sizeof(*binary)); | |
| 2695 | ||
| 2696 | /* Find command by matching typecookie and command number */ | |
| 2697 | for (c = here->nd_type->cmdlist; | |
| 2698 | c != NULL && c->name != NULL; c++) { | |
| 2699 | if (binary->header.typecookie == c->cookie | |
| 2700 | && binary->header.cmd == c->cmd) | |
| 2701 | break; | |
| 2702 | } | |
| 2703 | if (c == NULL || c->name == NULL) { | |
| 2704 | for (c = ng_generic_cmds; c->name != NULL; c++) { | |
| 2705 | if (binary->header.typecookie == c->cookie | |
| 2706 | && binary->header.cmd == c->cmd) | |
| 2707 | break; | |
| 2708 | } | |
| 2709 | if (c->name == NULL) { | |
| 2710 | NG_FREE_MSG(resp); | |
| 2711 | error = ENOSYS; | |
| 2712 | break; | |
| 2713 | } | |
| 2714 | } | |
| 2715 | ||
| 2716 | /* Convert command name to ASCII */ | |
| 2717 | snprintf(ascii->header.cmdstr, sizeof(ascii->header.cmdstr), | |
| 2718 | "%s", c->name); | |
| 2719 | ||
| 2720 | /* Convert command arguments to ASCII */ | |
| 2721 | argstype = (binary->header.flags & NGF_RESP) ? | |
| 2722 | c->respType : c->mesgType; | |
| 2723 | if (argstype == NULL) { | |
| 2724 | *ascii->data = '\0'; | |
| 2725 | } else { | |
| 2726 | if ((error = ng_unparse(argstype, | |
| 2727 | (u_char *)binary->data, | |
| 2728 | ascii->data, bufSize)) != 0) { | |
| 2729 | NG_FREE_MSG(resp); | |
| 2730 | break; | |
| 2731 | } | |
| 2732 | } | |
| 2733 | ||
| 2734 | /* Return the result as struct ng_mesg plus ASCII string */ | |
| 2735 | bufSize = strlen(ascii->data) + 1; | |
| 2736 | ascii->header.arglen = bufSize; | |
| 2737 | resp->header.arglen = sizeof(*ascii) + bufSize; | |
| 2738 | break; | |
| 2739 | } | |
| 2740 | ||
| 2741 | case NGM_ASCII2BINARY: | |
| 2742 | { | |
| 2743 | int bufSize = 2000; /* XXX hard coded constant */ | |
| 2744 | const struct ng_cmdlist *c; | |
| 2745 | const struct ng_parse_type *argstype; | |
| 2746 | struct ng_mesg *ascii, *binary; | |
| 2747 | int off = 0; | |
| 2748 | ||
| 2749 | /* Data area must contain at least a struct ng_mesg + '\0' */ | |
| 2750 | ascii = (struct ng_mesg *)msg->data; | |
| 2751 | if ((msg->header.arglen < sizeof(*ascii) + 1) || | |
| 2752 | (ascii->header.arglen < 1) || | |
| 2753 | (msg->header.arglen < sizeof(*ascii) + | |
| 2754 | ascii->header.arglen)) { | |
| 2755 | TRAP_ERROR(); | |
| 2756 | error = EINVAL; | |
| 2757 | break; | |
| 2758 | } | |
| 2759 | ascii->data[ascii->header.arglen - 1] = '\0'; | |
| 2760 | ||
| 2761 | /* Get a response message with lots of room */ | |
| 5a975a3d | 2762 | NG_MKRESPONSE(resp, msg, sizeof(*binary) + bufSize, M_WAITOK | M_NULLOK); |
| b06ebda0 MD |
2763 | if (resp == NULL) { |
| 2764 | error = ENOMEM; | |
| 2765 | break; | |
| 2766 | } | |
| 2767 | binary = (struct ng_mesg *)resp->data; | |
| 2768 | ||
| 2769 | /* Copy ASCII message header to response message payload */ | |
| 2770 | bcopy(ascii, binary, sizeof(*ascii)); | |
| 2771 | ||
| 2772 | /* Find command by matching ASCII command string */ | |
| 2773 | for (c = here->nd_type->cmdlist; | |
| 2774 | c != NULL && c->name != NULL; c++) { | |
| 2775 | if (strcmp(ascii->header.cmdstr, c->name) == 0) | |
| 2776 | break; | |
| 2777 | } | |
| 2778 | if (c == NULL || c->name == NULL) { | |
| 2779 | for (c = ng_generic_cmds; c->name != NULL; c++) { | |
| 2780 | if (strcmp(ascii->header.cmdstr, c->name) == 0) | |
| 2781 | break; | |
| 2782 | } | |
| 2783 | if (c->name == NULL) { | |
| 2784 | NG_FREE_MSG(resp); | |
| 2785 | error = ENOSYS; | |
| 2786 | break; | |
| 2787 | } | |
| 2788 | } | |
| 2789 | ||
| 2790 | /* Convert command name to binary */ | |
| 2791 | binary->header.cmd = c->cmd; | |
| 2792 | binary->header.typecookie = c->cookie; | |
| 2793 | ||
| 2794 | /* Convert command arguments to binary */ | |
| 2795 | argstype = (binary->header.flags & NGF_RESP) ? | |
| 2796 | c->respType : c->mesgType; | |
| 2797 | if (argstype == NULL) { | |
| 2798 | bufSize = 0; | |
| 2799 | } else { | |
| 2800 | if ((error = ng_parse(argstype, ascii->data, | |
| 2801 | &off, (u_char *)binary->data, &bufSize)) != 0) { | |
| 2802 | NG_FREE_MSG(resp); | |
| 2803 | break; | |
| 2804 | } | |
| 2805 | } | |
| 2806 | ||
| 2807 | /* Return the result */ | |
| 2808 | binary->header.arglen = bufSize; | |
| 2809 | resp->header.arglen = sizeof(*binary) + bufSize; | |
| 2810 | break; | |
| 2811 | } | |
| 2812 | ||
| 2813 | case NGM_TEXT_CONFIG: | |
| 2814 | case NGM_TEXT_STATUS: | |
| 2815 | /* | |
| 2816 | * This one is tricky as it passes the command down to the | |
| 2817 | * actual node, even though it is a generic type command. | |
| 2818 | * This means we must assume that the item/msg is already freed | |
| 2819 | * when control passes back to us. | |
| 2820 | */ | |
| 2821 | if (here->nd_type->rcvmsg != NULL) { | |
| 2822 | NGI_MSG(item) = msg; /* put it back as we found it */ | |
| 2823 | return((*here->nd_type->rcvmsg)(here, item, lasthook)); | |
| 2824 | } | |
| 2825 | /* Fall through if rcvmsg not supported */ | |
| 2826 | default: | |
| 2827 | TRAP_ERROR(); | |
| 2828 | error = EINVAL; | |
| 2829 | } | |
| 2830 | /* | |
| 2831 | * Sometimes a generic message may be statically allocated | |
| 2832 | * to avoid problems with allocating when in tight memeory situations. | |
| 2833 | * Don't free it if it is so. | |
| 2834 | * I break them appart here, because erros may cause a free if the item | |
| 2835 | * in which case we'd be doing it twice. | |
| 2836 | * they are kept together above, to simplify freeing. | |
| 2837 | */ | |
| 2838 | out: | |
| 2839 | NG_RESPOND_MSG(error, here, item, resp); | |
| 2840 | if (msg) | |
| 2841 | NG_FREE_MSG(msg); | |
| 2842 | return (error); | |
| 2843 | } | |
| 2844 | ||
| 2845 | /************************************************************************ | |
| 2846 | Queue element get/free routines | |
| 2847 | ************************************************************************/ | |
| 2848 | ||
| 2849 | uma_zone_t ng_qzone; | |
| 2850 | uma_zone_t ng_qdzone; | |
| 2851 | static int maxalloc = 4096;/* limit the damage of a leak */ | |
| 2852 | static int maxdata = 512; /* limit the damage of a DoS */ | |
| 2853 | ||
| 2854 | TUNABLE_INT("net.graph.maxalloc", &maxalloc); | |
| 2855 | SYSCTL_INT(_net_graph, OID_AUTO, maxalloc, CTLFLAG_RDTUN, &maxalloc, | |
| 2856 | 0, "Maximum number of non-data queue items to allocate"); | |
| 2857 | TUNABLE_INT("net.graph.maxdata", &maxdata); | |
| 2858 | SYSCTL_INT(_net_graph, OID_AUTO, maxdata, CTLFLAG_RDTUN, &maxdata, | |
| 2859 | 0, "Maximum number of data queue items to allocate"); | |
| 2860 | ||
| 2861 | #ifdef NETGRAPH_DEBUG | |
| 2862 | static TAILQ_HEAD(, ng_item) ng_itemlist = TAILQ_HEAD_INITIALIZER(ng_itemlist); | |
| 2863 | static int allocated; /* number of items malloc'd */ | |
| 2864 | #endif | |
| 2865 | ||
| 2866 | /* | |
| 2867 | * Get a queue entry. | |
| 2868 | * This is usually called when a packet first enters netgraph. | |
| 2869 | * By definition, this is usually from an interrupt, or from a user. | |
| 2870 | * Users are not so important, but try be quick for the times that it's | |
| 2871 | * an interrupt. | |
| 2872 | */ | |
| 2873 | static __inline item_p | |
| 2874 | ng_alloc_item(int type, int flags) | |
| 2875 | { | |
| 2876 | item_p item; | |
| 2877 | ||
| 2878 | KASSERT(((type & ~NGQF_TYPE) == 0), | |
| 2879 | ("%s: incorrect item type: %d", __func__, type)); | |
| 2880 | ||
| 2881 | item = uma_zalloc((type == NGQF_DATA)?ng_qdzone:ng_qzone, | |
| 2882 | ((flags & NG_WAITOK) ? M_WAITOK : M_NOWAIT) | M_ZERO); | |
| 2883 | ||
| 2884 | if (item) { | |
| 2885 | item->el_flags = type; | |
| 2886 | #ifdef NETGRAPH_DEBUG | |
| 2887 | mtx_lock(&ngq_mtx); | |
| 2888 | TAILQ_INSERT_TAIL(&ng_itemlist, item, all); | |
| 2889 | allocated++; | |
| 2890 | mtx_unlock(&ngq_mtx); | |
| 2891 | #endif | |
| 2892 | } | |
| 2893 | ||
| 2894 | return (item); | |
| 2895 | } | |
| 2896 | ||
| 2897 | /* | |
| 2898 | * Release a queue entry | |
| 2899 | */ | |
| 2900 | void | |
| 2901 | ng_free_item(item_p item) | |
| 2902 | { | |
| 2903 | /* | |
| 2904 | * The item may hold resources on it's own. We need to free | |
| 2905 | * these before we can free the item. What they are depends upon | |
| 2906 | * what kind of item it is. it is important that nodes zero | |
| 2907 | * out pointers to resources that they remove from the item | |
| 2908 | * or we release them again here. | |
| 2909 | */ | |
| 2910 | switch (item->el_flags & NGQF_TYPE) { | |
| 2911 | case NGQF_DATA: | |
| 2912 | /* If we have an mbuf still attached.. */ | |
| 2913 | NG_FREE_M(_NGI_M(item)); | |
| 2914 | break; | |
| 2915 | case NGQF_MESG: | |
| 2916 | _NGI_RETADDR(item) = 0; | |
| 2917 | NG_FREE_MSG(_NGI_MSG(item)); | |
| 2918 | break; | |
| 2919 | case NGQF_FN: | |
| 2920 | case NGQF_FN2: | |
| 2921 | /* nothing to free really, */ | |
| 2922 | _NGI_FN(item) = NULL; | |
| 2923 | _NGI_ARG1(item) = NULL; | |
| 2924 | _NGI_ARG2(item) = 0; | |
| 2925 | break; | |
| 2926 | } | |
| 2927 | /* If we still have a node or hook referenced... */ | |
| 2928 | _NGI_CLR_NODE(item); | |
| 2929 | _NGI_CLR_HOOK(item); | |
| 2930 | ||
| 2931 | #ifdef NETGRAPH_DEBUG | |
| 2932 | mtx_lock(&ngq_mtx); | |
| 2933 | TAILQ_REMOVE(&ng_itemlist, item, all); | |
| 2934 | allocated--; | |
| 2935 | mtx_unlock(&ngq_mtx); | |
| 2936 | #endif | |
| 2937 | uma_zfree(((item->el_flags & NGQF_TYPE) == NGQF_DATA)? | |
| 2938 | ng_qdzone:ng_qzone, item); | |
| 2939 | } | |
| 2940 | ||
| 2941 | /* | |
| 2942 | * Change type of the queue entry. | |
| 2943 | * Possibly reallocates it from another UMA zone. | |
| 2944 | */ | |
| 2945 | static __inline item_p | |
| 2946 | ng_realloc_item(item_p pitem, int type, int flags) | |
| 2947 | { | |
| 2948 | item_p item; | |
| 2949 | int from, to; | |
| 2950 | ||
| 2951 | KASSERT((pitem != NULL), ("%s: can't reallocate NULL", __func__)); | |
| 2952 | KASSERT(((type & ~NGQF_TYPE) == 0), | |
| 2953 | ("%s: incorrect item type: %d", __func__, type)); | |
| 2954 | ||
| 2955 | from = ((pitem->el_flags & NGQF_TYPE) == NGQF_DATA); | |
| 2956 | to = (type == NGQF_DATA); | |
| 2957 | if (from != to) { | |
| 2958 | /* If reallocation is required do it and copy item. */ | |
| 2959 | if ((item = ng_alloc_item(type, flags)) == NULL) { | |
| 2960 | ng_free_item(pitem); | |
| 2961 | return (NULL); | |
| 2962 | } | |
| 2963 | *item = *pitem; | |
| 2964 | ng_free_item(pitem); | |
| 2965 | } else | |
| 2966 | item = pitem; | |
| 2967 | item->el_flags = (item->el_flags & ~NGQF_TYPE) | type; | |
| 2968 | ||
| 2969 | return (item); | |
| 2970 | } | |
| 2971 | ||
| 2972 | /************************************************************************ | |
| 2973 | Module routines | |
| 2974 | ************************************************************************/ | |
| 2975 | ||
| 2976 | /* | |
| 2977 | * Handle the loading/unloading of a netgraph node type module | |
| 2978 | */ | |
| 2979 | int | |
| 2980 | ng_mod_event(module_t mod, int event, void *data) | |
| 2981 | { | |
| 2982 | struct ng_type *const type = data; | |
| 2983 | int s, error = 0; | |
| 2984 | ||
| 2985 | switch (event) { | |
| 2986 | case MOD_LOAD: | |
| 2987 | ||
| 2988 | /* Register new netgraph node type */ | |
| 2989 | s = splnet(); | |
| 2990 | if ((error = ng_newtype(type)) != 0) { | |
| 2991 | splx(s); | |
| 2992 | break; | |
| 2993 | } | |
| 2994 | ||
| 2995 | /* Call type specific code */ | |
| 2996 | if (type->mod_event != NULL) | |
| 2997 | if ((error = (*type->mod_event)(mod, event, data))) { | |
| 2998 | mtx_lock(&ng_typelist_mtx); | |
| 2999 | type->refs--; /* undo it */ | |
| 3000 | LIST_REMOVE(type, types); | |
| 3001 | mtx_unlock(&ng_typelist_mtx); | |
| 3002 | } | |
| 3003 | splx(s); | |
| 3004 | break; | |
| 3005 | ||
| 3006 | case MOD_UNLOAD: | |
| 3007 | s = splnet(); | |
| 3008 | if (type->refs > 1) { /* make sure no nodes exist! */ | |
| 3009 | error = EBUSY; | |
| 3010 | } else { | |
| 3011 | if (type->refs == 0) { | |
| 3012 | /* failed load, nothing to undo */ | |
| 3013 | splx(s); | |
| 3014 | break; | |
| 3015 | } | |
| 3016 | if (type->mod_event != NULL) { /* check with type */ | |
| 3017 | error = (*type->mod_event)(mod, event, data); | |
| 3018 | if (error != 0) { /* type refuses.. */ | |
| 3019 | splx(s); | |
| 3020 | break; | |
| 3021 | } | |
| 3022 | } | |
| 3023 | mtx_lock(&ng_typelist_mtx); | |
| 3024 | LIST_REMOVE(type, types); | |
| 3025 | mtx_unlock(&ng_typelist_mtx); | |
| 3026 | } | |
| 3027 | splx(s); | |
| 3028 | break; | |
| 3029 | ||
| 3030 | default: | |
| 3031 | if (type->mod_event != NULL) | |
| 3032 | error = (*type->mod_event)(mod, event, data); | |
| 3033 | else | |
| 3034 | error = EOPNOTSUPP; /* XXX ? */ | |
| 3035 | break; | |
| 3036 | } | |
| 3037 | return (error); | |
| 3038 | } | |
| 3039 | ||
| 3040 | /* | |
| 3041 | * Handle loading and unloading for this code. | |
| 3042 | * The only thing we need to link into is the NETISR strucure. | |
| 3043 | */ | |
| 3044 | static int | |
| 3045 | ngb_mod_event(module_t mod, int event, void *data) | |
| 3046 | { | |
| 3047 | int error = 0; | |
| 3048 | ||
| 3049 | switch (event) { | |
| 3050 | case MOD_LOAD: | |
| 3051 | /* Initialize everything. */ | |
| 3052 | NG_WORKLIST_LOCK_INIT(); | |
| 3053 | mtx_init(&ng_typelist_mtx, "netgraph types mutex", NULL, | |
| 3054 | MTX_DEF); | |
| 3055 | mtx_init(&ng_idhash_mtx, "netgraph idhash mutex", NULL, | |
| 3056 | MTX_DEF); | |
| 3057 | mtx_init(&ng_namehash_mtx, "netgraph namehash mutex", NULL, | |
| 3058 | MTX_DEF); | |
| 3059 | mtx_init(&ng_topo_mtx, "netgraph topology mutex", NULL, | |
| 3060 | MTX_DEF); | |
| 3061 | #ifdef NETGRAPH_DEBUG | |
| 3062 | mtx_init(&ng_nodelist_mtx, "netgraph nodelist mutex", NULL, | |
| 3063 | MTX_DEF); | |
| 3064 | mtx_init(&ngq_mtx, "netgraph item list mutex", NULL, | |
| 3065 | MTX_DEF); | |
| 3066 | #endif | |
| 3067 | ng_qzone = uma_zcreate("NetGraph items", sizeof(struct ng_item), | |
| 3068 | NULL, NULL, NULL, NULL, UMA_ALIGN_CACHE, 0); | |
| 3069 | uma_zone_set_max(ng_qzone, maxalloc); | |
| 3070 | ng_qdzone = uma_zcreate("NetGraph data items", sizeof(struct ng_item), | |
| 3071 | NULL, NULL, NULL, NULL, UMA_ALIGN_CACHE, 0); | |
| 3072 | uma_zone_set_max(ng_qdzone, maxdata); | |
| c3c96e44 | 3073 | netisr_register(NETISR_NETGRAPH, (netisr_t *)ngintr, NULL); |
| b06ebda0 MD |
3074 | break; |
| 3075 | case MOD_UNLOAD: | |
| 3076 | /* You can't unload it because an interface may be using it. */ | |
| 3077 | error = EBUSY; | |
| 3078 | break; | |
| 3079 | default: | |
| 3080 | error = EOPNOTSUPP; | |
| 3081 | break; | |
| 3082 | } | |
| 3083 | return (error); | |
| 3084 | } | |
| 3085 | ||
| 3086 | static moduledata_t netgraph_mod = { | |
| 3087 | "netgraph", | |
| 3088 | ngb_mod_event, | |
| 3089 | (NULL) | |
| 3090 | }; | |
| 3091 | DECLARE_MODULE(netgraph, netgraph_mod, SI_SUB_NETGRAPH, SI_ORDER_MIDDLE); | |
| 3092 | SYSCTL_NODE(_net, OID_AUTO, graph, CTLFLAG_RW, 0, "netgraph Family"); | |
| 3093 | SYSCTL_INT(_net_graph, OID_AUTO, abi_version, CTLFLAG_RD, 0, NG_ABI_VERSION,""); | |
| 3094 | SYSCTL_INT(_net_graph, OID_AUTO, msg_version, CTLFLAG_RD, 0, NG_VERSION, ""); | |
| 3095 | ||
| 3096 | #ifdef NETGRAPH_DEBUG | |
| 3097 | void | |
| 3098 | dumphook (hook_p hook, char *file, int line) | |
| 3099 | { | |
| 3100 | printf("hook: name %s, %d refs, Last touched:\n", | |
| 3101 | _NG_HOOK_NAME(hook), hook->hk_refs); | |
| 3102 | printf(" Last active @ %s, line %d\n", | |
| 3103 | hook->lastfile, hook->lastline); | |
| 3104 | if (line) { | |
| 3105 | printf(" problem discovered at file %s, line %d\n", file, line); | |
| 3106 | } | |
| 3107 | } | |
| 3108 | ||
| 3109 | void | |
| 3110 | dumpnode(node_p node, char *file, int line) | |
| 3111 | { | |
| 3112 | printf("node: ID [%x]: type '%s', %d hooks, flags 0x%x, %d refs, %s:\n", | |
| 3113 | _NG_NODE_ID(node), node->nd_type->name, | |
| 3114 | node->nd_numhooks, node->nd_flags, | |
| 3115 | node->nd_refs, node->nd_name); | |
| 3116 | printf(" Last active @ %s, line %d\n", | |
| 3117 | node->lastfile, node->lastline); | |
| 3118 | if (line) { | |
| 3119 | printf(" problem discovered at file %s, line %d\n", file, line); | |
| 3120 | } | |
| 3121 | } | |
| 3122 | ||
| 3123 | void | |
| 3124 | dumpitem(item_p item, char *file, int line) | |
| 3125 | { | |
| 3126 | printf(" ACTIVE item, last used at %s, line %d", | |
| 3127 | item->lastfile, item->lastline); | |
| 3128 | switch(item->el_flags & NGQF_TYPE) { | |
| 3129 | case NGQF_DATA: | |
| 3130 | printf(" - [data]\n"); | |
| 3131 | break; | |
| 3132 | case NGQF_MESG: | |
| 3133 | printf(" - retaddr[%d]:\n", _NGI_RETADDR(item)); | |
| 3134 | break; | |
| 3135 | case NGQF_FN: | |
| 3136 | printf(" - fn@%p (%p, %p, %p, %d (%x))\n", | |
| 3137 | _NGI_FN(item), | |
| 3138 | _NGI_NODE(item), | |
| 3139 | _NGI_HOOK(item), | |
| 3140 | item->body.fn.fn_arg1, | |
| 3141 | item->body.fn.fn_arg2, | |
| 3142 | item->body.fn.fn_arg2); | |
| 3143 | break; | |
| 3144 | case NGQF_FN2: | |
| 3145 | printf(" - fn2@%p (%p, %p, %p, %d (%x))\n", | |
| 3146 | _NGI_FN2(item), | |
| 3147 | _NGI_NODE(item), | |
| 3148 | _NGI_HOOK(item), | |
| 3149 | item->body.fn.fn_arg1, | |
| 3150 | item->body.fn.fn_arg2, | |
| 3151 | item->body.fn.fn_arg2); | |
| 3152 | break; | |
| 3153 | } | |
| 3154 | if (line) { | |
| 3155 | printf(" problem discovered at file %s, line %d\n", file, line); | |
| 3156 | if (_NGI_NODE(item)) { | |
| 3157 | printf("node %p ([%x])\n", | |
| 3158 | _NGI_NODE(item), ng_node2ID(_NGI_NODE(item))); | |
| 3159 | } | |
| 3160 | } | |
| 3161 | } | |
| 3162 | ||
| 3163 | static void | |
| 3164 | ng_dumpitems(void) | |
| 3165 | { | |
| 3166 | item_p item; | |
| 3167 | int i = 1; | |
| 3168 | TAILQ_FOREACH(item, &ng_itemlist, all) { | |
| 3169 | printf("[%d] ", i++); | |
| 3170 | dumpitem(item, NULL, 0); | |
| 3171 | } | |
| 3172 | } | |
| 3173 | ||
| 3174 | static void | |
| 3175 | ng_dumpnodes(void) | |
| 3176 | { | |
| 3177 | node_p node; | |
| 3178 | int i = 1; | |
| 3179 | mtx_lock(&ng_nodelist_mtx); | |
| 3180 | SLIST_FOREACH(node, &ng_allnodes, nd_all) { | |
| 3181 | printf("[%d] ", i++); | |
| 3182 | dumpnode(node, NULL, 0); | |
| 3183 | } | |
| 3184 | mtx_unlock(&ng_nodelist_mtx); | |
| 3185 | } | |
| 3186 | ||
| 3187 | static void | |
| 3188 | ng_dumphooks(void) | |
| 3189 | { | |
| 3190 | hook_p hook; | |
| 3191 | int i = 1; | |
| 3192 | mtx_lock(&ng_nodelist_mtx); | |
| 3193 | SLIST_FOREACH(hook, &ng_allhooks, hk_all) { | |
| 3194 | printf("[%d] ", i++); | |
| 3195 | dumphook(hook, NULL, 0); | |
| 3196 | } | |
| 3197 | mtx_unlock(&ng_nodelist_mtx); | |
| 3198 | } | |
| 3199 | ||
| 3200 | static int | |
| 3201 | sysctl_debug_ng_dump_items(SYSCTL_HANDLER_ARGS) | |
| 3202 | { | |
| 3203 | int error; | |
| 3204 | int val; | |
| 3205 | int i; | |
| 3206 | ||
| 3207 | val = allocated; | |
| 3208 | i = 1; | |
| 3209 | error = sysctl_handle_int(oidp, &val, 0, req); | |
| 3210 | if (error != 0 || req->newptr == NULL) | |
| 3211 | return (error); | |
| 3212 | if (val == 42) { | |
| 3213 | ng_dumpitems(); | |
| 3214 | ng_dumpnodes(); | |
| 3215 | ng_dumphooks(); | |
| 3216 | } | |
| 3217 | return (0); | |
| 3218 | } | |
| 3219 | ||
| 3220 | SYSCTL_PROC(_debug, OID_AUTO, ng_dump_items, CTLTYPE_INT | CTLFLAG_RW, | |
| 3221 | 0, sizeof(int), sysctl_debug_ng_dump_items, "I", "Number of allocated items"); | |
| 3222 | #endif /* NETGRAPH_DEBUG */ | |
| 3223 | ||
| 3224 | ||
| 3225 | /*********************************************************************** | |
| 3226 | * Worklist routines | |
| 3227 | **********************************************************************/ | |
| 3228 | /* NETISR thread enters here */ | |
| 3229 | /* | |
| 3230 | * Pick a node off the list of nodes with work, | |
| 3231 | * try get an item to process off it. | |
| 3232 | * If there are no more, remove the node from the list. | |
| 3233 | */ | |
| 3234 | static void | |
| 3235 | ngintr(void) | |
| 3236 | { | |
| c3c96e44 | 3237 | XXX replymsg XXX |
| b06ebda0 MD |
3238 | for (;;) { |
| 3239 | node_p node; | |
| 3240 | ||
| 3241 | /* Get node from the worklist. */ | |
| 3242 | NG_WORKLIST_LOCK(); | |
| 3243 | node = STAILQ_FIRST(&ng_worklist); | |
| 3244 | if (!node) { | |
| 3245 | NG_WORKLIST_UNLOCK(); | |
| 3246 | break; | |
| 3247 | } | |
| 3248 | STAILQ_REMOVE_HEAD(&ng_worklist, nd_input_queue.q_work); | |
| 3249 | NG_WORKLIST_UNLOCK(); | |
| 3250 | CTR3(KTR_NET, "%20s: node [%x] (%p) taken off worklist", | |
| 3251 | __func__, node->nd_ID, node); | |
| 3252 | /* | |
| 3253 | * We have the node. We also take over the reference | |
| 3254 | * that the list had on it. | |
| 3255 | * Now process as much as you can, until it won't | |
| 3256 | * let you have another item off the queue. | |
| 3257 | * All this time, keep the reference | |
| 3258 | * that lets us be sure that the node still exists. | |
| 3259 | * Let the reference go at the last minute. | |
| 3260 | */ | |
| 3261 | for (;;) { | |
| 3262 | item_p item; | |
| 3263 | int rw; | |
| 3264 | ||
| 3265 | NG_QUEUE_LOCK(&node->nd_input_queue); | |
| 3266 | item = ng_dequeue(node, &rw); | |
| 3267 | if (item == NULL) { | |
| 3268 | node->nd_input_queue.q_flags2 &= ~NGQ2_WORKQ; | |
| 3269 | NG_QUEUE_UNLOCK(&node->nd_input_queue); | |
| 3270 | break; /* go look for another node */ | |
| 3271 | } else { | |
| 3272 | NG_QUEUE_UNLOCK(&node->nd_input_queue); | |
| 3273 | NGI_GET_NODE(item, node); /* zaps stored node */ | |
| 3274 | ng_apply_item(node, item, rw); | |
| 3275 | NG_NODE_UNREF(node); | |
| 3276 | } | |
| 3277 | } | |
| 3278 | NG_NODE_UNREF(node); | |
| 3279 | } | |
| 3280 | } | |
| 3281 | ||
| 3282 | /* | |
| 3283 | * XXX | |
| 3284 | * It's posible that a debugging NG_NODE_REF may need | |
| 3285 | * to be outside the mutex zone | |
| 3286 | */ | |
| 3287 | static void | |
| 3288 | ng_worklist_add(node_p node) | |
| 3289 | { | |
| 3290 | ||
| 3291 | mtx_assert(&node->nd_input_queue.q_mtx, MA_OWNED); | |
| 3292 | ||
| 3293 | if ((node->nd_input_queue.q_flags2 & NGQ2_WORKQ) == 0) { | |
| 3294 | /* | |
| 3295 | * If we are not already on the work queue, | |
| 3296 | * then put us on. | |
| 3297 | */ | |
| 3298 | node->nd_input_queue.q_flags2 |= NGQ2_WORKQ; | |
| 3299 | NG_NODE_REF(node); /* XXX fafe in mutex? */ | |
| 3300 | NG_WORKLIST_LOCK(); | |
| 3301 | STAILQ_INSERT_TAIL(&ng_worklist, node, nd_input_queue.q_work); | |
| 3302 | NG_WORKLIST_UNLOCK(); | |
| 3303 | schednetisr(NETISR_NETGRAPH); | |
| 3304 | CTR3(KTR_NET, "%20s: node [%x] (%p) put on worklist", __func__, | |
| 3305 | node->nd_ID, node); | |
| 3306 | } else { | |
| 3307 | CTR3(KTR_NET, "%20s: node [%x] (%p) already on worklist", | |
| 3308 | __func__, node->nd_ID, node); | |
| 3309 | } | |
| 3310 | } | |
| 3311 | ||
| 3312 | ||
| 3313 | /*********************************************************************** | |
| 3314 | * Externally useable functions to set up a queue item ready for sending | |
| 3315 | ***********************************************************************/ | |
| 3316 | ||
| 3317 | #ifdef NETGRAPH_DEBUG | |
| 3318 | #define ITEM_DEBUG_CHECKS \ | |
| 3319 | do { \ | |
| 3320 | if (NGI_NODE(item) ) { \ | |
| 3321 | printf("item already has node"); \ | |
| 3322 | kdb_enter(KDB_WHY_NETGRAPH, "has node"); \ | |
| 3323 | NGI_CLR_NODE(item); \ | |
| 3324 | } \ | |
| 3325 | if (NGI_HOOK(item) ) { \ | |
| 3326 | printf("item already has hook"); \ | |
| 3327 | kdb_enter(KDB_WHY_NETGRAPH, "has hook"); \ | |
| 3328 | NGI_CLR_HOOK(item); \ | |
| 3329 | } \ | |
| 3330 | } while (0) | |
| 3331 | #else | |
| 3332 | #define ITEM_DEBUG_CHECKS | |
| 3333 | #endif | |
| 3334 | ||
| 3335 | /* | |
| 3336 | * Put mbuf into the item. | |
| 3337 | * Hook and node references will be removed when the item is dequeued. | |
| 3338 | * (or equivalent) | |
| 3339 | * (XXX) Unsafe because no reference held by peer on remote node. | |
| 3340 | * remote node might go away in this timescale. | |
| 3341 | * We know the hooks can't go away because that would require getting | |
| 3342 | * a writer item on both nodes and we must have at least a reader | |
| 3343 | * here to be able to do this. | |
| 3344 | * Note that the hook loaded is the REMOTE hook. | |
| 3345 | * | |
| 3346 | * This is possibly in the critical path for new data. | |
| 3347 | */ | |
| 3348 | item_p | |
| 3349 | ng_package_data(struct mbuf *m, int flags) | |
| 3350 | { | |
| 3351 | item_p item; | |
| 3352 | ||
| 3353 | if ((item = ng_alloc_item(NGQF_DATA, flags)) == NULL) { | |
| 3354 | NG_FREE_M(m); | |
| 3355 | return (NULL); | |
| 3356 | } | |
| 3357 | ITEM_DEBUG_CHECKS; | |
| 3358 | item->el_flags |= NGQF_READER; | |
| 3359 | NGI_M(item) = m; | |
| 3360 | return (item); | |
| 3361 | } | |
| 3362 | ||
| 3363 | /* | |
| 3364 | * Allocate a queue item and put items into it.. | |
| 3365 | * Evaluate the address as this will be needed to queue it and | |
| 3366 | * to work out what some of the fields should be. | |
| 3367 | * Hook and node references will be removed when the item is dequeued. | |
| 3368 | * (or equivalent) | |
| 3369 | */ | |
| 3370 | item_p | |
| 3371 | ng_package_msg(struct ng_mesg *msg, int flags) | |
| 3372 | { | |
| 3373 | item_p item; | |
| 3374 | ||
| 3375 | if ((item = ng_alloc_item(NGQF_MESG, flags)) == NULL) { | |
| 3376 | NG_FREE_MSG(msg); | |
| 3377 | return (NULL); | |
| 3378 | } | |
| 3379 | ITEM_DEBUG_CHECKS; | |
| 3380 | /* Messages items count as writers unless explicitly exempted. */ | |
| 3381 | if (msg->header.cmd & NGM_READONLY) | |
| 3382 | item->el_flags |= NGQF_READER; | |
| 3383 | else | |
| 3384 | item->el_flags |= NGQF_WRITER; | |
| 3385 | /* | |
| 3386 | * Set the current lasthook into the queue item | |
| 3387 | */ | |
| 3388 | NGI_MSG(item) = msg; | |
| 3389 | NGI_RETADDR(item) = 0; | |
| 3390 | return (item); | |
| 3391 | } | |
| 3392 | ||
| 3393 | ||
| 3394 | ||
| 3395 | #define SET_RETADDR(item, here, retaddr) \ | |
| 3396 | do { /* Data or fn items don't have retaddrs */ \ | |
| 3397 | if ((item->el_flags & NGQF_TYPE) == NGQF_MESG) { \ | |
| 3398 | if (retaddr) { \ | |
| 3399 | NGI_RETADDR(item) = retaddr; \ | |
| 3400 | } else { \ | |
| 3401 | /* \ | |
| 3402 | * The old return address should be ok. \ | |
| 3403 | * If there isn't one, use the address \ | |
| 3404 | * here. \ | |
| 3405 | */ \ | |
| 3406 | if (NGI_RETADDR(item) == 0) { \ | |
| 3407 | NGI_RETADDR(item) \ | |
| 3408 | = ng_node2ID(here); \ | |
| 3409 | } \ | |
| 3410 | } \ | |
| 3411 | } \ | |
| 3412 | } while (0) | |
| 3413 | ||
| 3414 | int | |
| 3415 | ng_address_hook(node_p here, item_p item, hook_p hook, ng_ID_t retaddr) | |
| 3416 | { | |
| 3417 | hook_p peer; | |
| 3418 | node_p peernode; | |
| 3419 | ITEM_DEBUG_CHECKS; | |
| 3420 | /* | |
| 3421 | * Quick sanity check.. | |
| 3422 | * Since a hook holds a reference on it's node, once we know | |
| 3423 | * that the peer is still connected (even if invalid,) we know | |
| 3424 | * that the peer node is present, though maybe invalid. | |
| 3425 | */ | |
| 3426 | if ((hook == NULL) || | |
| 3427 | NG_HOOK_NOT_VALID(hook) || | |
| 3428 | NG_HOOK_NOT_VALID(peer = NG_HOOK_PEER(hook)) || | |
| 3429 | NG_NODE_NOT_VALID(peernode = NG_PEER_NODE(hook))) { | |
| 3430 | NG_FREE_ITEM(item); | |
| 3431 | TRAP_ERROR(); | |
| 3432 | return (ENETDOWN); | |
| 3433 | } | |
| 3434 | ||
| 3435 | /* | |
| 3436 | * Transfer our interest to the other (peer) end. | |
| 3437 | */ | |
| 3438 | NG_HOOK_REF(peer); | |
| 3439 | NG_NODE_REF(peernode); | |
| 3440 | NGI_SET_HOOK(item, peer); | |
| 3441 | NGI_SET_NODE(item, peernode); | |
| 3442 | SET_RETADDR(item, here, retaddr); | |
| 3443 | return (0); | |
| 3444 | } | |
| 3445 | ||
| 3446 | int | |
| 3447 | ng_address_path(node_p here, item_p item, char *address, ng_ID_t retaddr) | |
| 3448 | { | |
| 3449 | node_p dest = NULL; | |
| 3450 | hook_p hook = NULL; | |
| 3451 | int error; | |
| 3452 | ||
| 3453 | ITEM_DEBUG_CHECKS; | |
| 3454 | /* | |
| 3455 | * Note that ng_path2noderef increments the reference count | |
| 3456 | * on the node for us if it finds one. So we don't have to. | |
| 3457 | */ | |
| 3458 | error = ng_path2noderef(here, address, &dest, &hook); | |
| 3459 | if (error) { | |
| 3460 | NG_FREE_ITEM(item); | |
| 3461 | return (error); | |
| 3462 | } | |
| 3463 | NGI_SET_NODE(item, dest); | |
| 3464 | if ( hook) { | |
| 3465 | NG_HOOK_REF(hook); /* don't let it go while on the queue */ | |
| 3466 | NGI_SET_HOOK(item, hook); | |
| 3467 | } | |
| 3468 | SET_RETADDR(item, here, retaddr); | |
| 3469 | return (0); | |
| 3470 | } | |
| 3471 | ||
| 3472 | int | |
| 3473 | ng_address_ID(node_p here, item_p item, ng_ID_t ID, ng_ID_t retaddr) | |
| 3474 | { | |
| 3475 | node_p dest; | |
| 3476 | ||
| 3477 | ITEM_DEBUG_CHECKS; | |
| 3478 | /* | |
| 3479 | * Find the target node. | |
| 3480 | */ | |
| 3481 | dest = ng_ID2noderef(ID); /* GETS REFERENCE! */ | |
| 3482 | if (dest == NULL) { | |
| 3483 | NG_FREE_ITEM(item); | |
| 3484 | TRAP_ERROR(); | |
| 3485 | return(EINVAL); | |
| 3486 | } | |
| 3487 | /* Fill out the contents */ | |
| 3488 | NGI_SET_NODE(item, dest); | |
| 3489 | NGI_CLR_HOOK(item); | |
| 3490 | SET_RETADDR(item, here, retaddr); | |
| 3491 | return (0); | |
| 3492 | } | |
| 3493 | ||
| 3494 | /* | |
| 3495 | * special case to send a message to self (e.g. destroy node) | |
| 3496 | * Possibly indicate an arrival hook too. | |
| 3497 | * Useful for removing that hook :-) | |
| 3498 | */ | |
| 3499 | item_p | |
| 3500 | ng_package_msg_self(node_p here, hook_p hook, struct ng_mesg *msg) | |
| 3501 | { | |
| 3502 | item_p item; | |
| 3503 | ||
| 3504 | /* | |
| 3505 | * Find the target node. | |
| 3506 | * If there is a HOOK argument, then use that in preference | |
| 3507 | * to the address. | |
| 3508 | */ | |
| 3509 | if ((item = ng_alloc_item(NGQF_MESG, NG_NOFLAGS)) == NULL) { | |
| 3510 | NG_FREE_MSG(msg); | |
| 3511 | return (NULL); | |
| 3512 | } | |
| 3513 | ||
| 3514 | /* Fill out the contents */ | |
| 3515 | item->el_flags |= NGQF_WRITER; | |
| 3516 | NG_NODE_REF(here); | |
| 3517 | NGI_SET_NODE(item, here); | |
| 3518 | if (hook) { | |
| 3519 | NG_HOOK_REF(hook); | |
| 3520 | NGI_SET_HOOK(item, hook); | |
| 3521 | } | |
| 3522 | NGI_MSG(item) = msg; | |
| 3523 | NGI_RETADDR(item) = ng_node2ID(here); | |
| 3524 | return (item); | |
| 3525 | } | |
| 3526 | ||
| 3527 | /* | |
| 3528 | * Send ng_item_fn function call to the specified node. | |
| 3529 | */ | |
| 3530 | ||
| 3531 | int | |
| 3532 | ng_send_fn(node_p node, hook_p hook, ng_item_fn *fn, void * arg1, int arg2) | |
| 3533 | { | |
| 3534 | ||
| 3535 | return ng_send_fn1(node, hook, fn, arg1, arg2, NG_NOFLAGS); | |
| 3536 | } | |
| 3537 | ||
| 3538 | int | |
| 3539 | ng_send_fn1(node_p node, hook_p hook, ng_item_fn *fn, void * arg1, int arg2, | |
| 3540 | int flags) | |
| 3541 | { | |
| 3542 | item_p item; | |
| 3543 | ||
| 3544 | if ((item = ng_alloc_item(NGQF_FN, flags)) == NULL) { | |
| 3545 | return (ENOMEM); | |
| 3546 | } | |
| 3547 | item->el_flags |= NGQF_WRITER; | |
| 3548 | NG_NODE_REF(node); /* and one for the item */ | |
| 3549 | NGI_SET_NODE(item, node); | |
| 3550 | if (hook) { | |
| 3551 | NG_HOOK_REF(hook); | |
| 3552 | NGI_SET_HOOK(item, hook); | |
| 3553 | } | |
| 3554 | NGI_FN(item) = fn; | |
| 3555 | NGI_ARG1(item) = arg1; | |
| 3556 | NGI_ARG2(item) = arg2; | |
| 3557 | return(ng_snd_item(item, flags)); | |
| 3558 | } | |
| 3559 | ||
| 3560 | /* | |
| 3561 | * Send ng_item_fn2 function call to the specified node. | |
| 3562 | * | |
| 3563 | * If an optional pitem parameter is supplied, its apply | |
| 3564 | * callback will be copied to the new item. If also NG_REUSE_ITEM | |
| 3565 | * flag is set, no new item will be allocated, but pitem will | |
| 3566 | * be used. | |
| 3567 | */ | |
| 3568 | int | |
| 3569 | ng_send_fn2(node_p node, hook_p hook, item_p pitem, ng_item_fn2 *fn, void *arg1, | |
| 3570 | int arg2, int flags) | |
| 3571 | { | |
| 3572 | item_p item; | |
| 3573 | ||
| 3574 | KASSERT((pitem != NULL || (flags & NG_REUSE_ITEM) == 0), | |
| 3575 | ("%s: NG_REUSE_ITEM but no pitem", __func__)); | |
| 3576 | ||
| 3577 | /* | |
| 3578 | * Allocate a new item if no supplied or | |
| 3579 | * if we can't use supplied one. | |
| 3580 | */ | |
| 3581 | if (pitem == NULL || (flags & NG_REUSE_ITEM) == 0) { | |
| 3582 | if ((item = ng_alloc_item(NGQF_FN2, flags)) == NULL) | |
| 3583 | return (ENOMEM); | |
| 3584 | if (pitem != NULL) | |
| 3585 | item->apply = pitem->apply; | |
| 3586 | } else { | |
| 3587 | if ((item = ng_realloc_item(pitem, NGQF_FN2, flags)) == NULL) | |
| 3588 | return (ENOMEM); | |
| 3589 | } | |
| 3590 | ||
| 3591 | item->el_flags = (item->el_flags & ~NGQF_RW) | NGQF_WRITER; | |
| 3592 | NG_NODE_REF(node); /* and one for the item */ | |
| 3593 | NGI_SET_NODE(item, node); | |
| 3594 | if (hook) { | |
| 3595 | NG_HOOK_REF(hook); | |
| 3596 | NGI_SET_HOOK(item, hook); | |
| 3597 | } | |
| 3598 | NGI_FN2(item) = fn; | |
| 3599 | NGI_ARG1(item) = arg1; | |
| 3600 | NGI_ARG2(item) = arg2; | |
| 3601 | return(ng_snd_item(item, flags)); | |
| 3602 | } | |
| 3603 | ||
| 3604 | /* | |
| 3605 | * Official timeout routines for Netgraph nodes. | |
| 3606 | */ | |
| 3607 | static void | |
| 3608 | ng_callout_trampoline(void *arg) | |
| 3609 | { | |
| 3610 | item_p item = arg; | |
| 3611 | ||
| 3612 | ng_snd_item(item, 0); | |
| 3613 | } | |
| 3614 | ||
| 3615 | ||
| 3616 | int | |
| 3617 | ng_callout(struct callout *c, node_p node, hook_p hook, int ticks, | |
| 3618 | ng_item_fn *fn, void * arg1, int arg2) | |
| 3619 | { | |
| 3620 | item_p item, oitem; | |
| 3621 | ||
| 3622 | if ((item = ng_alloc_item(NGQF_FN, NG_NOFLAGS)) == NULL) | |
| 3623 | return (ENOMEM); | |
| 3624 | ||
| 3625 | item->el_flags |= NGQF_WRITER; | |
| 3626 | NG_NODE_REF(node); /* and one for the item */ | |
| 3627 | NGI_SET_NODE(item, node); | |
| 3628 | if (hook) { | |
| 3629 | NG_HOOK_REF(hook); | |
| 3630 | NGI_SET_HOOK(item, hook); | |
| 3631 | } | |
| 3632 | NGI_FN(item) = fn; | |
| 3633 | NGI_ARG1(item) = arg1; | |
| 3634 | NGI_ARG2(item) = arg2; | |
| 3635 | oitem = c->c_arg; | |
| 3636 | if (callout_reset(c, ticks, &ng_callout_trampoline, item) == 1 && | |
| 3637 | oitem != NULL) | |
| 3638 | NG_FREE_ITEM(oitem); | |
| 3639 | return (0); | |
| 3640 | } | |
| 3641 | ||
| 3642 | /* A special modified version of untimeout() */ | |
| 3643 | int | |
| 3644 | ng_uncallout(struct callout *c, node_p node) | |
| 3645 | { | |
| 3646 | item_p item; | |
| 3647 | int rval; | |
| 3648 | ||
| 3649 | KASSERT(c != NULL, ("ng_uncallout: NULL callout")); | |
| 3650 | KASSERT(node != NULL, ("ng_uncallout: NULL node")); | |
| 3651 | ||
| 3652 | rval = callout_stop(c); | |
| 3653 | item = c->c_arg; | |
| 3654 | /* Do an extra check */ | |
| 3655 | if ((rval > 0) && (c->c_func == &ng_callout_trampoline) && | |
| 3656 | (NGI_NODE(item) == node)) { | |
| 3657 | /* | |
| 3658 | * We successfully removed it from the queue before it ran | |
| 3659 | * So now we need to unreference everything that was | |
| 3660 | * given extra references. (NG_FREE_ITEM does this). | |
| 3661 | */ | |
| 3662 | NG_FREE_ITEM(item); | |
| 3663 | } | |
| 3664 | c->c_arg = NULL; | |
| 3665 | ||
| 3666 | return (rval); | |
| 3667 | } | |
| 3668 | ||
| 3669 | /* | |
| 3670 | * Set the address, if none given, give the node here. | |
| 3671 | */ | |
| 3672 | void | |
| 3673 | ng_replace_retaddr(node_p here, item_p item, ng_ID_t retaddr) | |
| 3674 | { | |
| 3675 | if (retaddr) { | |
| 3676 | NGI_RETADDR(item) = retaddr; | |
| 3677 | } else { | |
| 3678 | /* | |
| 3679 | * The old return address should be ok. | |
| 3680 | * If there isn't one, use the address here. | |
| 3681 | */ | |
| 3682 | NGI_RETADDR(item) = ng_node2ID(here); | |
| 3683 | } | |
| 3684 | } | |
| 3685 | ||
| 3686 | #define TESTING | |
| 3687 | #ifdef TESTING | |
| 3688 | /* just test all the macros */ | |
| 3689 | void | |
| 3690 | ng_macro_test(item_p item); | |
| 3691 | void | |
| 3692 | ng_macro_test(item_p item) | |
| 3693 | { | |
| 3694 | node_p node = NULL; | |
| 3695 | hook_p hook = NULL; | |
| 3696 | struct mbuf *m; | |
| 3697 | struct ng_mesg *msg; | |
| 3698 | ng_ID_t retaddr; | |
| 3699 | int error; | |
| 3700 | ||
| 3701 | NGI_GET_M(item, m); | |
| 3702 | NGI_GET_MSG(item, msg); | |
| 3703 | retaddr = NGI_RETADDR(item); | |
| 3704 | NG_SEND_DATA(error, hook, m, NULL); | |
| 3705 | NG_SEND_DATA_ONLY(error, hook, m); | |
| 3706 | NG_FWD_NEW_DATA(error, item, hook, m); | |
| 3707 | NG_FWD_ITEM_HOOK(error, item, hook); | |
| 3708 | NG_SEND_MSG_HOOK(error, node, msg, hook, retaddr); | |
| 3709 | NG_SEND_MSG_ID(error, node, msg, retaddr, retaddr); | |
| 3710 | NG_SEND_MSG_PATH(error, node, msg, ".:", retaddr); | |
| 3711 | NG_FWD_MSG_HOOK(error, node, item, hook, retaddr); | |
| 3712 | } | |
| 3713 | #endif /* TESTING */ | |
| 3714 |