1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2004-2009 University of Zagreb 5 * Copyright (c) 2006-2009 FreeBSD Foundation 6 * All rights reserved. 7 * 8 * This software was developed by the University of Zagreb and the 9 * FreeBSD Foundation under sponsorship by the Stichting NLnet and the 10 * FreeBSD Foundation. 11 * 12 * Copyright (c) 2009 Jeffrey Roberson <jeff@freebsd.org> 13 * Copyright (c) 2009 Robert N. M. Watson 14 * All rights reserved. 15 * 16 * Redistribution and use in source and binary forms, with or without 17 * modification, are permitted provided that the following conditions 18 * are met: 19 * 1. Redistributions of source code must retain the above copyright 20 * notice, this list of conditions and the following disclaimer. 21 * 2. Redistributions in binary form must reproduce the above copyright 22 * notice, this list of conditions and the following disclaimer in the 23 * documentation and/or other materials provided with the distribution. 24 * 25 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35 * SUCH DAMAGE. 36 */ 37 38 #include <sys/cdefs.h> 39 __FBSDID("$FreeBSD$"); 40 41 #include "opt_ddb.h" 42 #include "opt_kdb.h" 43 44 #include <sys/param.h> 45 #include <sys/kdb.h> 46 #include <sys/kernel.h> 47 #include <sys/jail.h> 48 #include <sys/sdt.h> 49 #include <sys/systm.h> 50 #include <sys/sysctl.h> 51 #include <sys/eventhandler.h> 52 #include <sys/lock.h> 53 #include <sys/malloc.h> 54 #include <sys/proc.h> 55 #include <sys/socket.h> 56 #include <sys/sx.h> 57 #include <sys/sysctl.h> 58 59 #include <machine/stdarg.h> 60 61 #ifdef DDB 62 #include <ddb/ddb.h> 63 #include <ddb/db_sym.h> 64 #endif 65 66 #include <net/if.h> 67 #include <net/if_var.h> 68 #include <net/vnet.h> 69 70 /*- 71 * This file implements core functions for virtual network stacks: 72 * 73 * - Virtual network stack management functions. 74 * 75 * - Virtual network stack memory allocator, which virtualizes global 76 * variables in the network stack 77 * 78 * - Virtualized SYSINIT's/SYSUNINIT's, which allow network stack subsystems 79 * to register startup/shutdown events to be run for each virtual network 80 * stack instance. 81 */ 82 83 FEATURE(vimage, "VIMAGE kernel virtualization"); 84 85 static MALLOC_DEFINE(M_VNET, "vnet", "network stack control block"); 86 87 /* 88 * The virtual network stack list has two read-write locks, one sleepable and 89 * the other not, so that the list can be stablized and walked in a variety 90 * of network stack contexts. Both must be acquired exclusively to modify 91 * the list, but a read lock of either lock is sufficient to walk the list. 92 */ 93 struct rwlock vnet_rwlock; 94 struct sx vnet_sxlock; 95 96 #define VNET_LIST_WLOCK() do { \ 97 sx_xlock(&vnet_sxlock); \ 98 rw_wlock(&vnet_rwlock); \ 99 } while (0) 100 101 #define VNET_LIST_WUNLOCK() do { \ 102 rw_wunlock(&vnet_rwlock); \ 103 sx_xunlock(&vnet_sxlock); \ 104 } while (0) 105 106 struct vnet_list_head vnet_head; 107 struct vnet *vnet0; 108 109 /* 110 * The virtual network stack allocator provides storage for virtualized 111 * global variables. These variables are defined/declared using the 112 * VNET_DEFINE()/VNET_DECLARE() macros, which place them in the 'set_vnet' 113 * linker set. The details of the implementation are somewhat subtle, but 114 * allow the majority of most network subsystems to maintain 115 * virtualization-agnostic. 116 * 117 * The virtual network stack allocator handles variables in the base kernel 118 * vs. modules in similar but different ways. In both cases, virtualized 119 * global variables are marked as such by being declared to be part of the 120 * vnet linker set. These "master" copies of global variables serve two 121 * functions: 122 * 123 * (1) They contain static initialization or "default" values for global 124 * variables which will be propagated to each virtual network stack 125 * instance when created. As with normal global variables, they default 126 * to zero-filled. 127 * 128 * (2) They act as unique global names by which the variable can be referred 129 * to, regardless of network stack instance. The single global symbol 130 * will be used to calculate the location of a per-virtual instance 131 * variable at run-time. 132 * 133 * Each virtual network stack instance has a complete copy of each 134 * virtualized global variable, stored in a malloc'd block of memory 135 * referred to by vnet->vnet_data_mem. Critical to the design is that each 136 * per-instance memory block is laid out identically to the master block so 137 * that the offset of each global variable is the same across all blocks. To 138 * optimize run-time access, a precalculated 'base' address, 139 * vnet->vnet_data_base, is stored in each vnet, and is the amount that can 140 * be added to the address of a 'master' instance of a variable to get to the 141 * per-vnet instance. 142 * 143 * Virtualized global variables are handled in a similar manner, but as each 144 * module has its own 'set_vnet' linker set, and we want to keep all 145 * virtualized globals togther, we reserve space in the kernel's linker set 146 * for potential module variables using a per-vnet character array, 147 * 'modspace'. The virtual network stack allocator maintains a free list to 148 * track what space in the array is free (all, initially) and as modules are 149 * linked, allocates portions of the space to specific globals. The kernel 150 * module linker queries the virtual network stack allocator and will 151 * bind references of the global to the location during linking. It also 152 * calls into the virtual network stack allocator, once the memory is 153 * initialized, in order to propagate the new static initializations to all 154 * existing virtual network stack instances so that the soon-to-be executing 155 * module will find every network stack instance with proper default values. 156 */ 157 158 /* 159 * Number of bytes of data in the 'set_vnet' linker set, and hence the total 160 * size of all kernel virtualized global variables, and the malloc(9) type 161 * that will be used to allocate it. 162 */ 163 #define VNET_BYTES (VNET_STOP - VNET_START) 164 165 static MALLOC_DEFINE(M_VNET_DATA, "vnet_data", "VNET data"); 166 167 /* 168 * VNET_MODMIN is the minimum number of bytes we will reserve for the sum of 169 * global variables across all loaded modules. As this actually sizes an 170 * array declared as a virtualized global variable in the kernel itself, and 171 * we want the virtualized global variable space to be page-sized, we may 172 * have more space than that in practice. 173 */ 174 #define VNET_MODMIN (8 * PAGE_SIZE) 175 #define VNET_SIZE roundup2(VNET_BYTES, PAGE_SIZE) 176 177 /* 178 * Space to store virtualized global variables from loadable kernel modules, 179 * and the free list to manage it. 180 */ 181 VNET_DEFINE_STATIC(char, modspace[VNET_MODMIN] __aligned(__alignof(void *))); 182 183 /* 184 * Global lists of subsystem constructor and destructors for vnets. They are 185 * registered via VNET_SYSINIT() and VNET_SYSUNINIT(). Both lists are 186 * protected by the vnet_sysinit_sxlock global lock. 187 */ 188 static TAILQ_HEAD(vnet_sysinit_head, vnet_sysinit) vnet_constructors = 189 TAILQ_HEAD_INITIALIZER(vnet_constructors); 190 static TAILQ_HEAD(vnet_sysuninit_head, vnet_sysinit) vnet_destructors = 191 TAILQ_HEAD_INITIALIZER(vnet_destructors); 192 193 struct sx vnet_sysinit_sxlock; 194 195 #define VNET_SYSINIT_WLOCK() sx_xlock(&vnet_sysinit_sxlock); 196 #define VNET_SYSINIT_WUNLOCK() sx_xunlock(&vnet_sysinit_sxlock); 197 #define VNET_SYSINIT_RLOCK() sx_slock(&vnet_sysinit_sxlock); 198 #define VNET_SYSINIT_RUNLOCK() sx_sunlock(&vnet_sysinit_sxlock); 199 200 struct vnet_data_free { 201 uintptr_t vnd_start; 202 int vnd_len; 203 TAILQ_ENTRY(vnet_data_free) vnd_link; 204 }; 205 206 static MALLOC_DEFINE(M_VNET_DATA_FREE, "vnet_data_free", 207 "VNET resource accounting"); 208 static TAILQ_HEAD(, vnet_data_free) vnet_data_free_head = 209 TAILQ_HEAD_INITIALIZER(vnet_data_free_head); 210 static struct sx vnet_data_free_lock; 211 212 SDT_PROVIDER_DEFINE(vnet); 213 SDT_PROBE_DEFINE1(vnet, functions, vnet_alloc, entry, "int"); 214 SDT_PROBE_DEFINE2(vnet, functions, vnet_alloc, alloc, "int", 215 "struct vnet *"); 216 SDT_PROBE_DEFINE2(vnet, functions, vnet_alloc, return, 217 "int", "struct vnet *"); 218 SDT_PROBE_DEFINE2(vnet, functions, vnet_destroy, entry, 219 "int", "struct vnet *"); 220 SDT_PROBE_DEFINE1(vnet, functions, vnet_destroy, return, 221 "int"); 222 223 /* 224 * Run per-vnet sysinits or sysuninits during vnet creation/destruction. 225 */ 226 static void vnet_sysinit(void); 227 static void vnet_sysuninit(void); 228 229 #ifdef DDB 230 static void db_show_vnet_print_vs(struct vnet_sysinit *, int); 231 #endif 232 233 /* 234 * Allocate a virtual network stack. 235 */ 236 struct vnet * 237 vnet_alloc(void) 238 { 239 struct vnet *vnet; 240 241 SDT_PROBE1(vnet, functions, vnet_alloc, entry, __LINE__); 242 vnet = malloc(sizeof(struct vnet), M_VNET, M_WAITOK | M_ZERO); 243 vnet->vnet_magic_n = VNET_MAGIC_N; 244 SDT_PROBE2(vnet, functions, vnet_alloc, alloc, __LINE__, vnet); 245 246 /* 247 * Allocate storage for virtualized global variables and copy in 248 * initial values form our 'master' copy. 249 */ 250 vnet->vnet_data_mem = malloc(VNET_SIZE, M_VNET_DATA, M_WAITOK); 251 memcpy(vnet->vnet_data_mem, (void *)VNET_START, VNET_BYTES); 252 253 /* 254 * All use of vnet-specific data will immediately subtract VNET_START 255 * from the base memory pointer, so pre-calculate that now to avoid 256 * it on each use. 257 */ 258 vnet->vnet_data_base = (uintptr_t)vnet->vnet_data_mem - VNET_START; 259 260 /* Initialize / attach vnet module instances. */ 261 CURVNET_SET_QUIET(vnet); 262 vnet_sysinit(); 263 CURVNET_RESTORE(); 264 265 VNET_LIST_WLOCK(); 266 LIST_INSERT_HEAD(&vnet_head, vnet, vnet_le); 267 VNET_LIST_WUNLOCK(); 268 269 SDT_PROBE2(vnet, functions, vnet_alloc, return, __LINE__, vnet); 270 return (vnet); 271 } 272 273 /* 274 * Destroy a virtual network stack. 275 */ 276 void 277 vnet_destroy(struct vnet *vnet) 278 { 279 280 SDT_PROBE2(vnet, functions, vnet_destroy, entry, __LINE__, vnet); 281 KASSERT(vnet->vnet_sockcnt == 0, 282 ("%s: vnet still has sockets", __func__)); 283 284 VNET_LIST_WLOCK(); 285 LIST_REMOVE(vnet, vnet_le); 286 VNET_LIST_WUNLOCK(); 287 288 /* Signal that VNET is being shutdown. */ 289 vnet->vnet_shutdown = true; 290 291 CURVNET_SET_QUIET(vnet); 292 sx_xlock(&ifnet_detach_sxlock); 293 vnet_sysuninit(); 294 sx_xunlock(&ifnet_detach_sxlock); 295 CURVNET_RESTORE(); 296 297 /* 298 * Release storage for the virtual network stack instance. 299 */ 300 free(vnet->vnet_data_mem, M_VNET_DATA); 301 vnet->vnet_data_mem = NULL; 302 vnet->vnet_data_base = 0; 303 vnet->vnet_magic_n = 0xdeadbeef; 304 free(vnet, M_VNET); 305 SDT_PROBE1(vnet, functions, vnet_destroy, return, __LINE__); 306 } 307 308 /* 309 * Boot time initialization and allocation of virtual network stacks. 310 */ 311 static void 312 vnet_init_prelink(void *arg __unused) 313 { 314 315 rw_init(&vnet_rwlock, "vnet_rwlock"); 316 sx_init(&vnet_sxlock, "vnet_sxlock"); 317 sx_init(&vnet_sysinit_sxlock, "vnet_sysinit_sxlock"); 318 LIST_INIT(&vnet_head); 319 } 320 SYSINIT(vnet_init_prelink, SI_SUB_VNET_PRELINK, SI_ORDER_FIRST, 321 vnet_init_prelink, NULL); 322 323 static void 324 vnet0_init(void *arg __unused) 325 { 326 327 if (bootverbose) 328 printf("VIMAGE (virtualized network stack) enabled\n"); 329 330 /* 331 * We MUST clear curvnet in vi_init_done() before going SMP, 332 * otherwise CURVNET_SET() macros would scream about unnecessary 333 * curvnet recursions. 334 */ 335 curvnet = prison0.pr_vnet = vnet0 = vnet_alloc(); 336 } 337 SYSINIT(vnet0_init, SI_SUB_VNET, SI_ORDER_FIRST, vnet0_init, NULL); 338 339 static void 340 vnet_init_done(void *unused __unused) 341 { 342 343 curvnet = NULL; 344 } 345 SYSINIT(vnet_init_done, SI_SUB_VNET_DONE, SI_ORDER_ANY, vnet_init_done, 346 NULL); 347 348 /* 349 * Once on boot, initialize the modspace freelist to entirely cover modspace. 350 */ 351 static void 352 vnet_data_startup(void *dummy __unused) 353 { 354 struct vnet_data_free *df; 355 356 df = malloc(sizeof(*df), M_VNET_DATA_FREE, M_WAITOK | M_ZERO); 357 df->vnd_start = (uintptr_t)&VNET_NAME(modspace); 358 df->vnd_len = VNET_MODMIN; 359 TAILQ_INSERT_HEAD(&vnet_data_free_head, df, vnd_link); 360 sx_init(&vnet_data_free_lock, "vnet_data alloc lock"); 361 } 362 SYSINIT(vnet_data, SI_SUB_KLD, SI_ORDER_FIRST, vnet_data_startup, NULL); 363 364 /* Dummy VNET_SYSINIT to make sure we always reach the final end state. */ 365 static void 366 vnet_sysinit_done(void *unused __unused) 367 { 368 369 return; 370 } 371 VNET_SYSINIT(vnet_sysinit_done, SI_SUB_VNET_DONE, SI_ORDER_ANY, 372 vnet_sysinit_done, NULL); 373 374 /* 375 * When a module is loaded and requires storage for a virtualized global 376 * variable, allocate space from the modspace free list. This interface 377 * should be used only by the kernel linker. 378 */ 379 void * 380 vnet_data_alloc(int size) 381 { 382 struct vnet_data_free *df; 383 void *s; 384 385 s = NULL; 386 size = roundup2(size, sizeof(void *)); 387 sx_xlock(&vnet_data_free_lock); 388 TAILQ_FOREACH(df, &vnet_data_free_head, vnd_link) { 389 if (df->vnd_len < size) 390 continue; 391 if (df->vnd_len == size) { 392 s = (void *)df->vnd_start; 393 TAILQ_REMOVE(&vnet_data_free_head, df, vnd_link); 394 free(df, M_VNET_DATA_FREE); 395 break; 396 } 397 s = (void *)df->vnd_start; 398 df->vnd_len -= size; 399 df->vnd_start = df->vnd_start + size; 400 break; 401 } 402 sx_xunlock(&vnet_data_free_lock); 403 404 return (s); 405 } 406 407 /* 408 * Free space for a virtualized global variable on module unload. 409 */ 410 void 411 vnet_data_free(void *start_arg, int size) 412 { 413 struct vnet_data_free *df; 414 struct vnet_data_free *dn; 415 uintptr_t start; 416 uintptr_t end; 417 418 size = roundup2(size, sizeof(void *)); 419 start = (uintptr_t)start_arg; 420 end = start + size; 421 /* 422 * Free a region of space and merge it with as many neighbors as 423 * possible. Keeping the list sorted simplifies this operation. 424 */ 425 sx_xlock(&vnet_data_free_lock); 426 TAILQ_FOREACH(df, &vnet_data_free_head, vnd_link) { 427 if (df->vnd_start > end) 428 break; 429 /* 430 * If we expand at the end of an entry we may have to merge 431 * it with the one following it as well. 432 */ 433 if (df->vnd_start + df->vnd_len == start) { 434 df->vnd_len += size; 435 dn = TAILQ_NEXT(df, vnd_link); 436 if (df->vnd_start + df->vnd_len == dn->vnd_start) { 437 df->vnd_len += dn->vnd_len; 438 TAILQ_REMOVE(&vnet_data_free_head, dn, 439 vnd_link); 440 free(dn, M_VNET_DATA_FREE); 441 } 442 sx_xunlock(&vnet_data_free_lock); 443 return; 444 } 445 if (df->vnd_start == end) { 446 df->vnd_start = start; 447 df->vnd_len += size; 448 sx_xunlock(&vnet_data_free_lock); 449 return; 450 } 451 } 452 dn = malloc(sizeof(*df), M_VNET_DATA_FREE, M_WAITOK | M_ZERO); 453 dn->vnd_start = start; 454 dn->vnd_len = size; 455 if (df) 456 TAILQ_INSERT_BEFORE(df, dn, vnd_link); 457 else 458 TAILQ_INSERT_TAIL(&vnet_data_free_head, dn, vnd_link); 459 sx_xunlock(&vnet_data_free_lock); 460 } 461 462 /* 463 * When a new virtualized global variable has been allocated, propagate its 464 * initial value to each already-allocated virtual network stack instance. 465 */ 466 void 467 vnet_data_copy(void *start, int size) 468 { 469 struct vnet *vnet; 470 471 VNET_LIST_RLOCK(); 472 LIST_FOREACH(vnet, &vnet_head, vnet_le) 473 memcpy((void *)((uintptr_t)vnet->vnet_data_base + 474 (uintptr_t)start), start, size); 475 VNET_LIST_RUNLOCK(); 476 } 477 478 /* 479 * Support for special SYSINIT handlers registered via VNET_SYSINIT() 480 * and VNET_SYSUNINIT(). 481 */ 482 void 483 vnet_register_sysinit(void *arg) 484 { 485 struct vnet_sysinit *vs, *vs2; 486 struct vnet *vnet; 487 488 vs = arg; 489 KASSERT(vs->subsystem > SI_SUB_VNET, ("vnet sysinit too early")); 490 491 /* Add the constructor to the global list of vnet constructors. */ 492 VNET_SYSINIT_WLOCK(); 493 TAILQ_FOREACH(vs2, &vnet_constructors, link) { 494 if (vs2->subsystem > vs->subsystem) 495 break; 496 if (vs2->subsystem == vs->subsystem && vs2->order > vs->order) 497 break; 498 } 499 if (vs2 != NULL) 500 TAILQ_INSERT_BEFORE(vs2, vs, link); 501 else 502 TAILQ_INSERT_TAIL(&vnet_constructors, vs, link); 503 504 /* 505 * Invoke the constructor on all the existing vnets when it is 506 * registered. 507 */ 508 VNET_FOREACH(vnet) { 509 CURVNET_SET_QUIET(vnet); 510 vs->func(vs->arg); 511 CURVNET_RESTORE(); 512 } 513 VNET_SYSINIT_WUNLOCK(); 514 } 515 516 void 517 vnet_deregister_sysinit(void *arg) 518 { 519 struct vnet_sysinit *vs; 520 521 vs = arg; 522 523 /* Remove the constructor from the global list of vnet constructors. */ 524 VNET_SYSINIT_WLOCK(); 525 TAILQ_REMOVE(&vnet_constructors, vs, link); 526 VNET_SYSINIT_WUNLOCK(); 527 } 528 529 void 530 vnet_register_sysuninit(void *arg) 531 { 532 struct vnet_sysinit *vs, *vs2; 533 534 vs = arg; 535 536 /* Add the destructor to the global list of vnet destructors. */ 537 VNET_SYSINIT_WLOCK(); 538 TAILQ_FOREACH(vs2, &vnet_destructors, link) { 539 if (vs2->subsystem > vs->subsystem) 540 break; 541 if (vs2->subsystem == vs->subsystem && vs2->order > vs->order) 542 break; 543 } 544 if (vs2 != NULL) 545 TAILQ_INSERT_BEFORE(vs2, vs, link); 546 else 547 TAILQ_INSERT_TAIL(&vnet_destructors, vs, link); 548 VNET_SYSINIT_WUNLOCK(); 549 } 550 551 void 552 vnet_deregister_sysuninit(void *arg) 553 { 554 struct vnet_sysinit *vs; 555 struct vnet *vnet; 556 557 vs = arg; 558 559 /* 560 * Invoke the destructor on all the existing vnets when it is 561 * deregistered. 562 */ 563 VNET_SYSINIT_WLOCK(); 564 VNET_FOREACH(vnet) { 565 CURVNET_SET_QUIET(vnet); 566 vs->func(vs->arg); 567 CURVNET_RESTORE(); 568 } 569 570 /* Remove the destructor from the global list of vnet destructors. */ 571 TAILQ_REMOVE(&vnet_destructors, vs, link); 572 VNET_SYSINIT_WUNLOCK(); 573 } 574 575 /* 576 * Invoke all registered vnet constructors on the current vnet. Used during 577 * vnet construction. The caller is responsible for ensuring the new vnet is 578 * the current vnet and that the vnet_sysinit_sxlock lock is locked. 579 */ 580 static void 581 vnet_sysinit(void) 582 { 583 struct vnet_sysinit *vs; 584 585 VNET_SYSINIT_RLOCK(); 586 TAILQ_FOREACH(vs, &vnet_constructors, link) { 587 curvnet->vnet_state = vs->subsystem; 588 vs->func(vs->arg); 589 } 590 VNET_SYSINIT_RUNLOCK(); 591 } 592 593 /* 594 * Invoke all registered vnet destructors on the current vnet. Used during 595 * vnet destruction. The caller is responsible for ensuring the dying vnet 596 * the current vnet and that the vnet_sysinit_sxlock lock is locked. 597 */ 598 static void 599 vnet_sysuninit(void) 600 { 601 struct vnet_sysinit *vs; 602 603 VNET_SYSINIT_RLOCK(); 604 TAILQ_FOREACH_REVERSE(vs, &vnet_destructors, vnet_sysuninit_head, 605 link) { 606 curvnet->vnet_state = vs->subsystem; 607 vs->func(vs->arg); 608 } 609 VNET_SYSINIT_RUNLOCK(); 610 } 611 612 /* 613 * EVENTHANDLER(9) extensions. 614 */ 615 /* 616 * Invoke the eventhandler function originally registered with the possibly 617 * registered argument for all virtual network stack instances. 618 * 619 * This iterator can only be used for eventhandlers that do not take any 620 * additional arguments, as we do ignore the variadic arguments from the 621 * EVENTHANDLER_INVOKE() call. 622 */ 623 void 624 vnet_global_eventhandler_iterator_func(void *arg, ...) 625 { 626 VNET_ITERATOR_DECL(vnet_iter); 627 struct eventhandler_entry_vimage *v_ee; 628 629 /* 630 * There is a bug here in that we should actually cast things to 631 * (struct eventhandler_entry_ ## name *) but that's not easily 632 * possible in here so just re-using the variadic version we 633 * defined for the generic vimage case. 634 */ 635 v_ee = arg; 636 VNET_LIST_RLOCK(); 637 VNET_FOREACH(vnet_iter) { 638 CURVNET_SET(vnet_iter); 639 ((vimage_iterator_func_t)v_ee->func)(v_ee->ee_arg); 640 CURVNET_RESTORE(); 641 } 642 VNET_LIST_RUNLOCK(); 643 } 644 645 #ifdef VNET_DEBUG 646 struct vnet_recursion { 647 SLIST_ENTRY(vnet_recursion) vnr_le; 648 const char *prev_fn; 649 const char *where_fn; 650 int where_line; 651 struct vnet *old_vnet; 652 struct vnet *new_vnet; 653 }; 654 655 static SLIST_HEAD(, vnet_recursion) vnet_recursions = 656 SLIST_HEAD_INITIALIZER(vnet_recursions); 657 658 static void 659 vnet_print_recursion(struct vnet_recursion *vnr, int brief) 660 { 661 662 if (!brief) 663 printf("CURVNET_SET() recursion in "); 664 printf("%s() line %d, prev in %s()", vnr->where_fn, vnr->where_line, 665 vnr->prev_fn); 666 if (brief) 667 printf(", "); 668 else 669 printf("\n "); 670 printf("%p -> %p\n", vnr->old_vnet, vnr->new_vnet); 671 } 672 673 void 674 vnet_log_recursion(struct vnet *old_vnet, const char *old_fn, int line) 675 { 676 struct vnet_recursion *vnr; 677 678 /* Skip already logged recursion events. */ 679 SLIST_FOREACH(vnr, &vnet_recursions, vnr_le) 680 if (vnr->prev_fn == old_fn && 681 vnr->where_fn == curthread->td_vnet_lpush && 682 vnr->where_line == line && 683 (vnr->old_vnet == vnr->new_vnet) == (curvnet == old_vnet)) 684 return; 685 686 vnr = malloc(sizeof(*vnr), M_VNET, M_NOWAIT | M_ZERO); 687 if (vnr == NULL) 688 panic("%s: malloc failed", __func__); 689 vnr->prev_fn = old_fn; 690 vnr->where_fn = curthread->td_vnet_lpush; 691 vnr->where_line = line; 692 vnr->old_vnet = old_vnet; 693 vnr->new_vnet = curvnet; 694 695 SLIST_INSERT_HEAD(&vnet_recursions, vnr, vnr_le); 696 697 vnet_print_recursion(vnr, 0); 698 #ifdef KDB 699 kdb_backtrace(); 700 #endif 701 } 702 #endif /* VNET_DEBUG */ 703 704 /* 705 * DDB(4). 706 */ 707 #ifdef DDB 708 static void 709 db_vnet_print(struct vnet *vnet) 710 { 711 712 db_printf("vnet = %p\n", vnet); 713 db_printf(" vnet_magic_n = %#08x (%s, orig %#08x)\n", 714 vnet->vnet_magic_n, 715 (vnet->vnet_magic_n == VNET_MAGIC_N) ? 716 "ok" : "mismatch", VNET_MAGIC_N); 717 db_printf(" vnet_ifcnt = %u\n", vnet->vnet_ifcnt); 718 db_printf(" vnet_sockcnt = %u\n", vnet->vnet_sockcnt); 719 db_printf(" vnet_data_mem = %p\n", vnet->vnet_data_mem); 720 db_printf(" vnet_data_base = %#jx\n", 721 (uintmax_t)vnet->vnet_data_base); 722 db_printf(" vnet_state = %#08x\n", vnet->vnet_state); 723 db_printf(" vnet_shutdown = %#03x\n", vnet->vnet_shutdown); 724 db_printf("\n"); 725 } 726 727 DB_SHOW_ALL_COMMAND(vnets, db_show_all_vnets) 728 { 729 VNET_ITERATOR_DECL(vnet_iter); 730 731 VNET_FOREACH(vnet_iter) { 732 db_vnet_print(vnet_iter); 733 if (db_pager_quit) 734 break; 735 } 736 } 737 738 DB_SHOW_COMMAND(vnet, db_show_vnet) 739 { 740 741 if (!have_addr) { 742 db_printf("usage: show vnet <struct vnet *>\n"); 743 return; 744 } 745 746 db_vnet_print((struct vnet *)addr); 747 } 748 749 static void 750 db_show_vnet_print_vs(struct vnet_sysinit *vs, int ddb) 751 { 752 const char *vsname, *funcname; 753 c_db_sym_t sym; 754 db_expr_t offset; 755 756 #define xprint(...) \ 757 if (ddb) \ 758 db_printf(__VA_ARGS__); \ 759 else \ 760 printf(__VA_ARGS__) 761 762 if (vs == NULL) { 763 xprint("%s: no vnet_sysinit * given\n", __func__); 764 return; 765 } 766 767 sym = db_search_symbol((vm_offset_t)vs, DB_STGY_ANY, &offset); 768 db_symbol_values(sym, &vsname, NULL); 769 sym = db_search_symbol((vm_offset_t)vs->func, DB_STGY_PROC, &offset); 770 db_symbol_values(sym, &funcname, NULL); 771 xprint("%s(%p)\n", (vsname != NULL) ? vsname : "", vs); 772 xprint(" %#08x %#08x\n", vs->subsystem, vs->order); 773 xprint(" %p(%s)(%p)\n", 774 vs->func, (funcname != NULL) ? funcname : "", vs->arg); 775 #undef xprint 776 } 777 778 DB_SHOW_COMMAND_FLAGS(vnet_sysinit, db_show_vnet_sysinit, DB_CMD_MEMSAFE) 779 { 780 struct vnet_sysinit *vs; 781 782 db_printf("VNET_SYSINIT vs Name(Ptr)\n"); 783 db_printf(" Subsystem Order\n"); 784 db_printf(" Function(Name)(Arg)\n"); 785 TAILQ_FOREACH(vs, &vnet_constructors, link) { 786 db_show_vnet_print_vs(vs, 1); 787 if (db_pager_quit) 788 break; 789 } 790 } 791 792 DB_SHOW_COMMAND_FLAGS(vnet_sysuninit, db_show_vnet_sysuninit, DB_CMD_MEMSAFE) 793 { 794 struct vnet_sysinit *vs; 795 796 db_printf("VNET_SYSUNINIT vs Name(Ptr)\n"); 797 db_printf(" Subsystem Order\n"); 798 db_printf(" Function(Name)(Arg)\n"); 799 TAILQ_FOREACH_REVERSE(vs, &vnet_destructors, vnet_sysuninit_head, 800 link) { 801 db_show_vnet_print_vs(vs, 1); 802 if (db_pager_quit) 803 break; 804 } 805 } 806 807 #ifdef VNET_DEBUG 808 DB_SHOW_COMMAND_FLAGS(vnetrcrs, db_show_vnetrcrs, DB_CMD_MEMSAFE) 809 { 810 struct vnet_recursion *vnr; 811 812 SLIST_FOREACH(vnr, &vnet_recursions, vnr_le) 813 vnet_print_recursion(vnr, 1); 814 } 815 #endif 816 #endif /* DDB */ 817