1eddfbb76SRobert Watson /*- 2fe267a55SPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3fe267a55SPedro F. Giffuni * 4530c0060SRobert Watson * Copyright (c) 2004-2009 University of Zagreb 5530c0060SRobert Watson * Copyright (c) 2006-2009 FreeBSD Foundation 6530c0060SRobert Watson * All rights reserved. 7530c0060SRobert Watson * 8530c0060SRobert Watson * This software was developed by the University of Zagreb and the 9530c0060SRobert Watson * FreeBSD Foundation under sponsorship by the Stichting NLnet and the 10530c0060SRobert Watson * FreeBSD Foundation. 11530c0060SRobert Watson * 12eddfbb76SRobert Watson * Copyright (c) 2009 Jeffrey Roberson <jeff@freebsd.org> 13eddfbb76SRobert Watson * Copyright (c) 2009 Robert N. M. Watson 14eddfbb76SRobert Watson * All rights reserved. 15eddfbb76SRobert Watson * 16eddfbb76SRobert Watson * Redistribution and use in source and binary forms, with or without 17eddfbb76SRobert Watson * modification, are permitted provided that the following conditions 18eddfbb76SRobert Watson * are met: 19eddfbb76SRobert Watson * 1. Redistributions of source code must retain the above copyright 20eddfbb76SRobert Watson * notice, this list of conditions and the following disclaimer. 21eddfbb76SRobert Watson * 2. Redistributions in binary form must reproduce the above copyright 22eddfbb76SRobert Watson * notice, this list of conditions and the following disclaimer in the 23eddfbb76SRobert Watson * documentation and/or other materials provided with the distribution. 24eddfbb76SRobert Watson * 25eddfbb76SRobert Watson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 26eddfbb76SRobert Watson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27eddfbb76SRobert Watson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28eddfbb76SRobert Watson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 29eddfbb76SRobert Watson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30eddfbb76SRobert Watson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31eddfbb76SRobert Watson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32eddfbb76SRobert Watson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33eddfbb76SRobert Watson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34eddfbb76SRobert Watson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35eddfbb76SRobert Watson * SUCH DAMAGE. 36eddfbb76SRobert Watson */ 37eddfbb76SRobert Watson 38eddfbb76SRobert Watson #include <sys/cdefs.h> 39eddfbb76SRobert Watson __FBSDID("$FreeBSD$"); 40eddfbb76SRobert Watson 41530c0060SRobert Watson #include "opt_ddb.h" 420a705ab6SMarko Zec #include "opt_kdb.h" 43530c0060SRobert Watson 44eddfbb76SRobert Watson #include <sys/param.h> 450a705ab6SMarko Zec #include <sys/kdb.h> 46eddfbb76SRobert Watson #include <sys/kernel.h> 47530c0060SRobert Watson #include <sys/jail.h> 4861d033d4SBjoern A. Zeeb #include <sys/sdt.h> 49eddfbb76SRobert Watson #include <sys/systm.h> 50eddfbb76SRobert Watson #include <sys/sysctl.h> 5142eedeacSBjoern A. Zeeb #include <sys/eventhandler.h> 52eddfbb76SRobert Watson #include <sys/lock.h> 53eddfbb76SRobert Watson #include <sys/malloc.h> 54eddfbb76SRobert Watson #include <sys/proc.h> 55530c0060SRobert Watson #include <sys/socket.h> 56eddfbb76SRobert Watson #include <sys/sx.h> 57eddfbb76SRobert Watson #include <sys/sysctl.h> 58eddfbb76SRobert Watson 5942eedeacSBjoern A. Zeeb #include <machine/stdarg.h> 6042eedeacSBjoern A. Zeeb 61530c0060SRobert Watson #ifdef DDB 62530c0060SRobert Watson #include <ddb/ddb.h> 633e0490b3SBjoern A. Zeeb #include <ddb/db_sym.h> 64530c0060SRobert Watson #endif 65530c0060SRobert Watson 66530c0060SRobert Watson #include <net/if.h> 67530c0060SRobert Watson #include <net/if_var.h> 68eddfbb76SRobert Watson #include <net/vnet.h> 69eddfbb76SRobert Watson 70eddfbb76SRobert Watson /*- 71ed3db012SRobert Watson * This file implements core functions for virtual network stacks: 72ed3db012SRobert Watson * 73c8f6a138SRobert Watson * - Virtual network stack management functions. 74530c0060SRobert Watson * 75c8f6a138SRobert Watson * - Virtual network stack memory allocator, which virtualizes global 76ed3db012SRobert Watson * variables in the network stack 77ed3db012SRobert Watson * 78ed3db012SRobert Watson * - Virtualized SYSINIT's/SYSUNINIT's, which allow network stack subsystems 79ed3db012SRobert Watson * to register startup/shutdown events to be run for each virtual network 80ed3db012SRobert Watson * stack instance. 81530c0060SRobert Watson */ 82530c0060SRobert Watson 839269189fSBjoern A. Zeeb FEATURE(vimage, "VIMAGE kernel virtualization"); 849269189fSBjoern A. Zeeb 85d745c852SEd Schouten static MALLOC_DEFINE(M_VNET, "vnet", "network stack control block"); 86530c0060SRobert Watson 87530c0060SRobert Watson /* 88530c0060SRobert Watson * The virtual network stack list has two read-write locks, one sleepable and 89530c0060SRobert Watson * the other not, so that the list can be stablized and walked in a variety 90530c0060SRobert Watson * of network stack contexts. Both must be acquired exclusively to modify 91c8f6a138SRobert Watson * the list, but a read lock of either lock is sufficient to walk the list. 92530c0060SRobert Watson */ 93530c0060SRobert Watson struct rwlock vnet_rwlock; 94530c0060SRobert Watson struct sx vnet_sxlock; 95530c0060SRobert Watson 96530c0060SRobert Watson #define VNET_LIST_WLOCK() do { \ 97530c0060SRobert Watson sx_xlock(&vnet_sxlock); \ 98530c0060SRobert Watson rw_wlock(&vnet_rwlock); \ 99530c0060SRobert Watson } while (0) 100530c0060SRobert Watson 101530c0060SRobert Watson #define VNET_LIST_WUNLOCK() do { \ 102530c0060SRobert Watson rw_wunlock(&vnet_rwlock); \ 103530c0060SRobert Watson sx_xunlock(&vnet_sxlock); \ 104530c0060SRobert Watson } while (0) 105530c0060SRobert Watson 106530c0060SRobert Watson struct vnet_list_head vnet_head; 107530c0060SRobert Watson struct vnet *vnet0; 108530c0060SRobert Watson 109530c0060SRobert Watson /* 110ed3db012SRobert Watson * The virtual network stack allocator provides storage for virtualized 111ed3db012SRobert Watson * global variables. These variables are defined/declared using the 112ed3db012SRobert Watson * VNET_DEFINE()/VNET_DECLARE() macros, which place them in the 'set_vnet' 113ed3db012SRobert Watson * linker set. The details of the implementation are somewhat subtle, but 114ed3db012SRobert Watson * allow the majority of most network subsystems to maintain 115eddfbb76SRobert Watson * virtualization-agnostic. 116eddfbb76SRobert Watson * 117eddfbb76SRobert Watson * The virtual network stack allocator handles variables in the base kernel 118eddfbb76SRobert Watson * vs. modules in similar but different ways. In both cases, virtualized 119eddfbb76SRobert Watson * global variables are marked as such by being declared to be part of the 120eddfbb76SRobert Watson * vnet linker set. These "master" copies of global variables serve two 121eddfbb76SRobert Watson * functions: 122eddfbb76SRobert Watson * 123eddfbb76SRobert Watson * (1) They contain static initialization or "default" values for global 124eddfbb76SRobert Watson * variables which will be propagated to each virtual network stack 125eddfbb76SRobert Watson * instance when created. As with normal global variables, they default 126eddfbb76SRobert Watson * to zero-filled. 127eddfbb76SRobert Watson * 128eddfbb76SRobert Watson * (2) They act as unique global names by which the variable can be referred 129eddfbb76SRobert Watson * to, regardless of network stack instance. The single global symbol 130eddfbb76SRobert Watson * will be used to calculate the location of a per-virtual instance 131eddfbb76SRobert Watson * variable at run-time. 132eddfbb76SRobert Watson * 133eddfbb76SRobert Watson * Each virtual network stack instance has a complete copy of each 134eddfbb76SRobert Watson * virtualized global variable, stored in a malloc'd block of memory 135eddfbb76SRobert Watson * referred to by vnet->vnet_data_mem. Critical to the design is that each 136eddfbb76SRobert Watson * per-instance memory block is laid out identically to the master block so 137eddfbb76SRobert Watson * that the offset of each global variable is the same across all blocks. To 138eddfbb76SRobert Watson * optimize run-time access, a precalculated 'base' address, 139eddfbb76SRobert Watson * vnet->vnet_data_base, is stored in each vnet, and is the amount that can 140eddfbb76SRobert Watson * be added to the address of a 'master' instance of a variable to get to the 141eddfbb76SRobert Watson * per-vnet instance. 142eddfbb76SRobert Watson * 143eddfbb76SRobert Watson * Virtualized global variables are handled in a similar manner, but as each 144eddfbb76SRobert Watson * module has its own 'set_vnet' linker set, and we want to keep all 145eddfbb76SRobert Watson * virtualized globals togther, we reserve space in the kernel's linker set 146eddfbb76SRobert Watson * for potential module variables using a per-vnet character array, 147eddfbb76SRobert Watson * 'modspace'. The virtual network stack allocator maintains a free list to 148eddfbb76SRobert Watson * track what space in the array is free (all, initially) and as modules are 149eddfbb76SRobert Watson * linked, allocates portions of the space to specific globals. The kernel 150eddfbb76SRobert Watson * module linker queries the virtual network stack allocator and will 151eddfbb76SRobert Watson * bind references of the global to the location during linking. It also 152eddfbb76SRobert Watson * calls into the virtual network stack allocator, once the memory is 153eddfbb76SRobert Watson * initialized, in order to propagate the new static initializations to all 154eddfbb76SRobert Watson * existing virtual network stack instances so that the soon-to-be executing 155eddfbb76SRobert Watson * module will find every network stack instance with proper default values. 156eddfbb76SRobert Watson */ 157eddfbb76SRobert Watson 158eddfbb76SRobert Watson /* 159eddfbb76SRobert Watson * Number of bytes of data in the 'set_vnet' linker set, and hence the total 160eddfbb76SRobert Watson * size of all kernel virtualized global variables, and the malloc(9) type 161eddfbb76SRobert Watson * that will be used to allocate it. 162eddfbb76SRobert Watson */ 163eddfbb76SRobert Watson #define VNET_BYTES (VNET_STOP - VNET_START) 164eddfbb76SRobert Watson 165d745c852SEd Schouten static MALLOC_DEFINE(M_VNET_DATA, "vnet_data", "VNET data"); 166eddfbb76SRobert Watson 167eddfbb76SRobert Watson /* 168eddfbb76SRobert Watson * VNET_MODMIN is the minimum number of bytes we will reserve for the sum of 169eddfbb76SRobert Watson * global variables across all loaded modules. As this actually sizes an 170eddfbb76SRobert Watson * array declared as a virtualized global variable in the kernel itself, and 171eddfbb76SRobert Watson * we want the virtualized global variable space to be page-sized, we may 172eddfbb76SRobert Watson * have more space than that in practice. 173eddfbb76SRobert Watson */ 174eddfbb76SRobert Watson #define VNET_MODMIN 8192 175eddfbb76SRobert Watson #define VNET_SIZE roundup2(VNET_BYTES, PAGE_SIZE) 176eddfbb76SRobert Watson 177eddfbb76SRobert Watson /* 178eddfbb76SRobert Watson * Space to store virtualized global variables from loadable kernel modules, 179eddfbb76SRobert Watson * and the free list to manage it. 180eddfbb76SRobert Watson */ 181*cd2106eaSAndrew Turner VNET_DEFINE_STATIC(char, modspace[VNET_MODMIN] __aligned(__alignof(void *))); 182eddfbb76SRobert Watson 183d0728d71SRobert Watson /* 184c8f6a138SRobert Watson * Global lists of subsystem constructor and destructors for vnets. They are 185a99fcfd4SMarko Zec * registered via VNET_SYSINIT() and VNET_SYSUNINIT(). Both lists are 186a99fcfd4SMarko Zec * protected by the vnet_sysinit_sxlock global lock. 187d0728d71SRobert Watson */ 188d0728d71SRobert Watson static TAILQ_HEAD(vnet_sysinit_head, vnet_sysinit) vnet_constructors = 189d0728d71SRobert Watson TAILQ_HEAD_INITIALIZER(vnet_constructors); 190d0728d71SRobert Watson static TAILQ_HEAD(vnet_sysuninit_head, vnet_sysinit) vnet_destructors = 191d0728d71SRobert Watson TAILQ_HEAD_INITIALIZER(vnet_destructors); 192d0728d71SRobert Watson 193a99fcfd4SMarko Zec struct sx vnet_sysinit_sxlock; 194a99fcfd4SMarko Zec 195a99fcfd4SMarko Zec #define VNET_SYSINIT_WLOCK() sx_xlock(&vnet_sysinit_sxlock); 196a99fcfd4SMarko Zec #define VNET_SYSINIT_WUNLOCK() sx_xunlock(&vnet_sysinit_sxlock); 197a99fcfd4SMarko Zec #define VNET_SYSINIT_RLOCK() sx_slock(&vnet_sysinit_sxlock); 198a99fcfd4SMarko Zec #define VNET_SYSINIT_RUNLOCK() sx_sunlock(&vnet_sysinit_sxlock); 199a99fcfd4SMarko Zec 200eddfbb76SRobert Watson struct vnet_data_free { 201eddfbb76SRobert Watson uintptr_t vnd_start; 202eddfbb76SRobert Watson int vnd_len; 203eddfbb76SRobert Watson TAILQ_ENTRY(vnet_data_free) vnd_link; 204eddfbb76SRobert Watson }; 205eddfbb76SRobert Watson 206d745c852SEd Schouten static MALLOC_DEFINE(M_VNET_DATA_FREE, "vnet_data_free", 207d745c852SEd Schouten "VNET resource accounting"); 208eddfbb76SRobert Watson static TAILQ_HEAD(, vnet_data_free) vnet_data_free_head = 209eddfbb76SRobert Watson TAILQ_HEAD_INITIALIZER(vnet_data_free_head); 210eddfbb76SRobert Watson static struct sx vnet_data_free_lock; 211eddfbb76SRobert Watson 21261d033d4SBjoern A. Zeeb SDT_PROVIDER_DEFINE(vnet); 213d9fae5abSAndriy Gapon SDT_PROBE_DEFINE1(vnet, functions, vnet_alloc, entry, "int"); 214d9fae5abSAndriy Gapon SDT_PROBE_DEFINE2(vnet, functions, vnet_alloc, alloc, "int", 21579856499SRui Paulo "struct vnet *"); 216d9fae5abSAndriy Gapon SDT_PROBE_DEFINE2(vnet, functions, vnet_alloc, return, 21779856499SRui Paulo "int", "struct vnet *"); 218d9fae5abSAndriy Gapon SDT_PROBE_DEFINE2(vnet, functions, vnet_destroy, entry, 21979856499SRui Paulo "int", "struct vnet *"); 2204678c740SGleb Smirnoff SDT_PROBE_DEFINE1(vnet, functions, vnet_destroy, return, 22179856499SRui Paulo "int"); 22261d033d4SBjoern A. Zeeb 2233e0490b3SBjoern A. Zeeb #ifdef DDB 2243e0490b3SBjoern A. Zeeb static void db_show_vnet_print_vs(struct vnet_sysinit *, int); 2253e0490b3SBjoern A. Zeeb #endif 2263e0490b3SBjoern A. Zeeb 227eddfbb76SRobert Watson /* 228530c0060SRobert Watson * Allocate a virtual network stack. 229530c0060SRobert Watson */ 230530c0060SRobert Watson struct vnet * 231530c0060SRobert Watson vnet_alloc(void) 232530c0060SRobert Watson { 233530c0060SRobert Watson struct vnet *vnet; 234530c0060SRobert Watson 23561d033d4SBjoern A. Zeeb SDT_PROBE1(vnet, functions, vnet_alloc, entry, __LINE__); 236530c0060SRobert Watson vnet = malloc(sizeof(struct vnet), M_VNET, M_WAITOK | M_ZERO); 237530c0060SRobert Watson vnet->vnet_magic_n = VNET_MAGIC_N; 23894081f88SBjoern A. Zeeb vnet->vnet_state = 0; 23961d033d4SBjoern A. Zeeb SDT_PROBE2(vnet, functions, vnet_alloc, alloc, __LINE__, vnet); 2406bc2c7b7SRobert Watson 2416bc2c7b7SRobert Watson /* 2426bc2c7b7SRobert Watson * Allocate storage for virtualized global variables and copy in 2436bc2c7b7SRobert Watson * initial values form our 'master' copy. 2446bc2c7b7SRobert Watson */ 2456bc2c7b7SRobert Watson vnet->vnet_data_mem = malloc(VNET_SIZE, M_VNET_DATA, M_WAITOK); 2466bc2c7b7SRobert Watson memcpy(vnet->vnet_data_mem, (void *)VNET_START, VNET_BYTES); 2476bc2c7b7SRobert Watson 2486bc2c7b7SRobert Watson /* 2496bc2c7b7SRobert Watson * All use of vnet-specific data will immediately subtract VNET_START 2506bc2c7b7SRobert Watson * from the base memory pointer, so pre-calculate that now to avoid 2516bc2c7b7SRobert Watson * it on each use. 2526bc2c7b7SRobert Watson */ 2536bc2c7b7SRobert Watson vnet->vnet_data_base = (uintptr_t)vnet->vnet_data_mem - VNET_START; 254530c0060SRobert Watson 255530c0060SRobert Watson /* Initialize / attach vnet module instances. */ 256530c0060SRobert Watson CURVNET_SET_QUIET(vnet); 257530c0060SRobert Watson vnet_sysinit(); 258530c0060SRobert Watson CURVNET_RESTORE(); 259530c0060SRobert Watson 260a99fcfd4SMarko Zec VNET_LIST_WLOCK(); 261530c0060SRobert Watson LIST_INSERT_HEAD(&vnet_head, vnet, vnet_le); 262530c0060SRobert Watson VNET_LIST_WUNLOCK(); 263530c0060SRobert Watson 26461d033d4SBjoern A. Zeeb SDT_PROBE2(vnet, functions, vnet_alloc, return, __LINE__, vnet); 265530c0060SRobert Watson return (vnet); 266530c0060SRobert Watson } 267530c0060SRobert Watson 268530c0060SRobert Watson /* 269530c0060SRobert Watson * Destroy a virtual network stack. 270530c0060SRobert Watson */ 271530c0060SRobert Watson void 272530c0060SRobert Watson vnet_destroy(struct vnet *vnet) 273530c0060SRobert Watson { 274530c0060SRobert Watson 27561d033d4SBjoern A. Zeeb SDT_PROBE2(vnet, functions, vnet_destroy, entry, __LINE__, vnet); 276530c0060SRobert Watson KASSERT(vnet->vnet_sockcnt == 0, 277530c0060SRobert Watson ("%s: vnet still has sockets", __func__)); 278530c0060SRobert Watson 279530c0060SRobert Watson VNET_LIST_WLOCK(); 280530c0060SRobert Watson LIST_REMOVE(vnet, vnet_le); 281a99fcfd4SMarko Zec VNET_LIST_WUNLOCK(); 282530c0060SRobert Watson 283530c0060SRobert Watson CURVNET_SET_QUIET(vnet); 284530c0060SRobert Watson vnet_sysuninit(); 285530c0060SRobert Watson CURVNET_RESTORE(); 286530c0060SRobert Watson 2876bc2c7b7SRobert Watson /* 2886bc2c7b7SRobert Watson * Release storage for the virtual network stack instance. 2896bc2c7b7SRobert Watson */ 2906bc2c7b7SRobert Watson free(vnet->vnet_data_mem, M_VNET_DATA); 2916bc2c7b7SRobert Watson vnet->vnet_data_mem = NULL; 2926bc2c7b7SRobert Watson vnet->vnet_data_base = 0; 293530c0060SRobert Watson vnet->vnet_magic_n = 0xdeadbeef; 294530c0060SRobert Watson free(vnet, M_VNET); 29561d033d4SBjoern A. Zeeb SDT_PROBE1(vnet, functions, vnet_destroy, return, __LINE__); 296530c0060SRobert Watson } 297530c0060SRobert Watson 298530c0060SRobert Watson /* 299530c0060SRobert Watson * Boot time initialization and allocation of virtual network stacks. 300530c0060SRobert Watson */ 301530c0060SRobert Watson static void 30254d9f34eSBjoern A. Zeeb vnet_init_prelink(void *arg __unused) 303530c0060SRobert Watson { 304530c0060SRobert Watson 305530c0060SRobert Watson rw_init(&vnet_rwlock, "vnet_rwlock"); 306530c0060SRobert Watson sx_init(&vnet_sxlock, "vnet_sxlock"); 307a99fcfd4SMarko Zec sx_init(&vnet_sysinit_sxlock, "vnet_sysinit_sxlock"); 308530c0060SRobert Watson LIST_INIT(&vnet_head); 309530c0060SRobert Watson } 310530c0060SRobert Watson SYSINIT(vnet_init_prelink, SI_SUB_VNET_PRELINK, SI_ORDER_FIRST, 311530c0060SRobert Watson vnet_init_prelink, NULL); 312530c0060SRobert Watson 313530c0060SRobert Watson static void 31454d9f34eSBjoern A. Zeeb vnet0_init(void *arg __unused) 315530c0060SRobert Watson { 316530c0060SRobert Watson 3178e94025bSBjoern A. Zeeb if (bootverbose) 3188e94025bSBjoern A. Zeeb printf("VIMAGE (virtualized network stack) enabled\n"); 3196aad5c1cSRobert Watson 320530c0060SRobert Watson /* 321530c0060SRobert Watson * We MUST clear curvnet in vi_init_done() before going SMP, 322530c0060SRobert Watson * otherwise CURVNET_SET() macros would scream about unnecessary 323530c0060SRobert Watson * curvnet recursions. 324530c0060SRobert Watson */ 325530c0060SRobert Watson curvnet = prison0.pr_vnet = vnet0 = vnet_alloc(); 326530c0060SRobert Watson } 327530c0060SRobert Watson SYSINIT(vnet0_init, SI_SUB_VNET, SI_ORDER_FIRST, vnet0_init, NULL); 328530c0060SRobert Watson 329530c0060SRobert Watson static void 33054d9f34eSBjoern A. Zeeb vnet_init_done(void *unused __unused) 331530c0060SRobert Watson { 332530c0060SRobert Watson 333530c0060SRobert Watson curvnet = NULL; 334530c0060SRobert Watson } 33589856f7eSBjoern A. Zeeb SYSINIT(vnet_init_done, SI_SUB_VNET_DONE, SI_ORDER_ANY, vnet_init_done, 336530c0060SRobert Watson NULL); 337530c0060SRobert Watson 338530c0060SRobert Watson /* 339eddfbb76SRobert Watson * Once on boot, initialize the modspace freelist to entirely cover modspace. 340eddfbb76SRobert Watson */ 341eddfbb76SRobert Watson static void 342eddfbb76SRobert Watson vnet_data_startup(void *dummy __unused) 343eddfbb76SRobert Watson { 344eddfbb76SRobert Watson struct vnet_data_free *df; 345eddfbb76SRobert Watson 346eddfbb76SRobert Watson df = malloc(sizeof(*df), M_VNET_DATA_FREE, M_WAITOK | M_ZERO); 347eddfbb76SRobert Watson df->vnd_start = (uintptr_t)&VNET_NAME(modspace); 348793f71bfSBjoern A. Zeeb df->vnd_len = VNET_MODMIN; 349eddfbb76SRobert Watson TAILQ_INSERT_HEAD(&vnet_data_free_head, df, vnd_link); 350eddfbb76SRobert Watson sx_init(&vnet_data_free_lock, "vnet_data alloc lock"); 351eddfbb76SRobert Watson } 352891cf3edSEd Maste SYSINIT(vnet_data, SI_SUB_KLD, SI_ORDER_FIRST, vnet_data_startup, NULL); 353eddfbb76SRobert Watson 35400e36a5cSBjoern A. Zeeb /* Dummy VNET_SYSINIT to make sure we always reach the final end state. */ 35500e36a5cSBjoern A. Zeeb static void 35600e36a5cSBjoern A. Zeeb vnet_sysinit_done(void *unused __unused) 35700e36a5cSBjoern A. Zeeb { 35800e36a5cSBjoern A. Zeeb 35900e36a5cSBjoern A. Zeeb return; 36000e36a5cSBjoern A. Zeeb } 36100e36a5cSBjoern A. Zeeb VNET_SYSINIT(vnet_sysinit_done, SI_SUB_VNET_DONE, SI_ORDER_ANY, 36200e36a5cSBjoern A. Zeeb vnet_sysinit_done, NULL); 36300e36a5cSBjoern A. Zeeb 364eddfbb76SRobert Watson /* 365eddfbb76SRobert Watson * When a module is loaded and requires storage for a virtualized global 366eddfbb76SRobert Watson * variable, allocate space from the modspace free list. This interface 367eddfbb76SRobert Watson * should be used only by the kernel linker. 368eddfbb76SRobert Watson */ 369eddfbb76SRobert Watson void * 370eddfbb76SRobert Watson vnet_data_alloc(int size) 371eddfbb76SRobert Watson { 372eddfbb76SRobert Watson struct vnet_data_free *df; 373eddfbb76SRobert Watson void *s; 374eddfbb76SRobert Watson 375eddfbb76SRobert Watson s = NULL; 376eddfbb76SRobert Watson size = roundup2(size, sizeof(void *)); 377eddfbb76SRobert Watson sx_xlock(&vnet_data_free_lock); 378eddfbb76SRobert Watson TAILQ_FOREACH(df, &vnet_data_free_head, vnd_link) { 379eddfbb76SRobert Watson if (df->vnd_len < size) 380eddfbb76SRobert Watson continue; 381eddfbb76SRobert Watson if (df->vnd_len == size) { 382eddfbb76SRobert Watson s = (void *)df->vnd_start; 383eddfbb76SRobert Watson TAILQ_REMOVE(&vnet_data_free_head, df, vnd_link); 384eddfbb76SRobert Watson free(df, M_VNET_DATA_FREE); 385eddfbb76SRobert Watson break; 386eddfbb76SRobert Watson } 387eddfbb76SRobert Watson s = (void *)df->vnd_start; 388eddfbb76SRobert Watson df->vnd_len -= size; 389eddfbb76SRobert Watson df->vnd_start = df->vnd_start + size; 390eddfbb76SRobert Watson break; 391eddfbb76SRobert Watson } 392eddfbb76SRobert Watson sx_xunlock(&vnet_data_free_lock); 393eddfbb76SRobert Watson 394eddfbb76SRobert Watson return (s); 395eddfbb76SRobert Watson } 396eddfbb76SRobert Watson 397eddfbb76SRobert Watson /* 398eddfbb76SRobert Watson * Free space for a virtualized global variable on module unload. 399eddfbb76SRobert Watson */ 400eddfbb76SRobert Watson void 401eddfbb76SRobert Watson vnet_data_free(void *start_arg, int size) 402eddfbb76SRobert Watson { 403eddfbb76SRobert Watson struct vnet_data_free *df; 404eddfbb76SRobert Watson struct vnet_data_free *dn; 405eddfbb76SRobert Watson uintptr_t start; 406eddfbb76SRobert Watson uintptr_t end; 407eddfbb76SRobert Watson 408eddfbb76SRobert Watson size = roundup2(size, sizeof(void *)); 409eddfbb76SRobert Watson start = (uintptr_t)start_arg; 410eddfbb76SRobert Watson end = start + size; 411eddfbb76SRobert Watson /* 412eddfbb76SRobert Watson * Free a region of space and merge it with as many neighbors as 413eddfbb76SRobert Watson * possible. Keeping the list sorted simplifies this operation. 414eddfbb76SRobert Watson */ 415eddfbb76SRobert Watson sx_xlock(&vnet_data_free_lock); 416eddfbb76SRobert Watson TAILQ_FOREACH(df, &vnet_data_free_head, vnd_link) { 417eddfbb76SRobert Watson if (df->vnd_start > end) 418eddfbb76SRobert Watson break; 419eddfbb76SRobert Watson /* 420c8f6a138SRobert Watson * If we expand at the end of an entry we may have to merge 421c8f6a138SRobert Watson * it with the one following it as well. 422eddfbb76SRobert Watson */ 423eddfbb76SRobert Watson if (df->vnd_start + df->vnd_len == start) { 424eddfbb76SRobert Watson df->vnd_len += size; 425eddfbb76SRobert Watson dn = TAILQ_NEXT(df, vnd_link); 426eddfbb76SRobert Watson if (df->vnd_start + df->vnd_len == dn->vnd_start) { 427eddfbb76SRobert Watson df->vnd_len += dn->vnd_len; 428c8f6a138SRobert Watson TAILQ_REMOVE(&vnet_data_free_head, dn, 429c8f6a138SRobert Watson vnd_link); 430eddfbb76SRobert Watson free(dn, M_VNET_DATA_FREE); 431eddfbb76SRobert Watson } 432eddfbb76SRobert Watson sx_xunlock(&vnet_data_free_lock); 433eddfbb76SRobert Watson return; 434eddfbb76SRobert Watson } 435eddfbb76SRobert Watson if (df->vnd_start == end) { 436eddfbb76SRobert Watson df->vnd_start = start; 437eddfbb76SRobert Watson df->vnd_len += size; 438eddfbb76SRobert Watson sx_xunlock(&vnet_data_free_lock); 439eddfbb76SRobert Watson return; 440eddfbb76SRobert Watson } 441eddfbb76SRobert Watson } 442eddfbb76SRobert Watson dn = malloc(sizeof(*df), M_VNET_DATA_FREE, M_WAITOK | M_ZERO); 443eddfbb76SRobert Watson dn->vnd_start = start; 444eddfbb76SRobert Watson dn->vnd_len = size; 445eddfbb76SRobert Watson if (df) 446eddfbb76SRobert Watson TAILQ_INSERT_BEFORE(df, dn, vnd_link); 447eddfbb76SRobert Watson else 448eddfbb76SRobert Watson TAILQ_INSERT_TAIL(&vnet_data_free_head, dn, vnd_link); 449eddfbb76SRobert Watson sx_xunlock(&vnet_data_free_lock); 450eddfbb76SRobert Watson } 451eddfbb76SRobert Watson 452eddfbb76SRobert Watson /* 453eddfbb76SRobert Watson * When a new virtualized global variable has been allocated, propagate its 454eddfbb76SRobert Watson * initial value to each already-allocated virtual network stack instance. 455eddfbb76SRobert Watson */ 456eddfbb76SRobert Watson void 457eddfbb76SRobert Watson vnet_data_copy(void *start, int size) 458eddfbb76SRobert Watson { 4597429a3f3SRobert Watson struct vnet *vnet; 460eddfbb76SRobert Watson 4617429a3f3SRobert Watson VNET_LIST_RLOCK(); 4627429a3f3SRobert Watson LIST_FOREACH(vnet, &vnet_head, vnet_le) 4637429a3f3SRobert Watson memcpy((void *)((uintptr_t)vnet->vnet_data_base + 4647429a3f3SRobert Watson (uintptr_t)start), start, size); 4657429a3f3SRobert Watson VNET_LIST_RUNLOCK(); 466eddfbb76SRobert Watson } 467eddfbb76SRobert Watson 468eddfbb76SRobert Watson /* 469d0728d71SRobert Watson * Support for special SYSINIT handlers registered via VNET_SYSINIT() 470d0728d71SRobert Watson * and VNET_SYSUNINIT(). 471d0728d71SRobert Watson */ 472d0728d71SRobert Watson void 473d0728d71SRobert Watson vnet_register_sysinit(void *arg) 474d0728d71SRobert Watson { 475d0728d71SRobert Watson struct vnet_sysinit *vs, *vs2; 476d0728d71SRobert Watson struct vnet *vnet; 477d0728d71SRobert Watson 478d0728d71SRobert Watson vs = arg; 479d0728d71SRobert Watson KASSERT(vs->subsystem > SI_SUB_VNET, ("vnet sysinit too early")); 480d0728d71SRobert Watson 481d0728d71SRobert Watson /* Add the constructor to the global list of vnet constructors. */ 482a99fcfd4SMarko Zec VNET_SYSINIT_WLOCK(); 483d0728d71SRobert Watson TAILQ_FOREACH(vs2, &vnet_constructors, link) { 484d0728d71SRobert Watson if (vs2->subsystem > vs->subsystem) 485d0728d71SRobert Watson break; 486d0728d71SRobert Watson if (vs2->subsystem == vs->subsystem && vs2->order > vs->order) 487d0728d71SRobert Watson break; 488d0728d71SRobert Watson } 489d0728d71SRobert Watson if (vs2 != NULL) 490d0728d71SRobert Watson TAILQ_INSERT_BEFORE(vs2, vs, link); 491d0728d71SRobert Watson else 492d0728d71SRobert Watson TAILQ_INSERT_TAIL(&vnet_constructors, vs, link); 493d0728d71SRobert Watson 494d0728d71SRobert Watson /* 495d0728d71SRobert Watson * Invoke the constructor on all the existing vnets when it is 496d0728d71SRobert Watson * registered. 497d0728d71SRobert Watson */ 498d0728d71SRobert Watson VNET_FOREACH(vnet) { 499d0728d71SRobert Watson CURVNET_SET_QUIET(vnet); 500d0728d71SRobert Watson vs->func(vs->arg); 501d0728d71SRobert Watson CURVNET_RESTORE(); 502d0728d71SRobert Watson } 503a99fcfd4SMarko Zec VNET_SYSINIT_WUNLOCK(); 504d0728d71SRobert Watson } 505d0728d71SRobert Watson 506d0728d71SRobert Watson void 507d0728d71SRobert Watson vnet_deregister_sysinit(void *arg) 508d0728d71SRobert Watson { 509d0728d71SRobert Watson struct vnet_sysinit *vs; 510d0728d71SRobert Watson 511d0728d71SRobert Watson vs = arg; 512d0728d71SRobert Watson 513d0728d71SRobert Watson /* Remove the constructor from the global list of vnet constructors. */ 514a99fcfd4SMarko Zec VNET_SYSINIT_WLOCK(); 515d0728d71SRobert Watson TAILQ_REMOVE(&vnet_constructors, vs, link); 516a99fcfd4SMarko Zec VNET_SYSINIT_WUNLOCK(); 517d0728d71SRobert Watson } 518d0728d71SRobert Watson 519d0728d71SRobert Watson void 520d0728d71SRobert Watson vnet_register_sysuninit(void *arg) 521d0728d71SRobert Watson { 522d0728d71SRobert Watson struct vnet_sysinit *vs, *vs2; 523d0728d71SRobert Watson 524d0728d71SRobert Watson vs = arg; 525d0728d71SRobert Watson 526d0728d71SRobert Watson /* Add the destructor to the global list of vnet destructors. */ 527a99fcfd4SMarko Zec VNET_SYSINIT_WLOCK(); 528d0728d71SRobert Watson TAILQ_FOREACH(vs2, &vnet_destructors, link) { 529d0728d71SRobert Watson if (vs2->subsystem > vs->subsystem) 530d0728d71SRobert Watson break; 531d0728d71SRobert Watson if (vs2->subsystem == vs->subsystem && vs2->order > vs->order) 532d0728d71SRobert Watson break; 533d0728d71SRobert Watson } 534d0728d71SRobert Watson if (vs2 != NULL) 535d0728d71SRobert Watson TAILQ_INSERT_BEFORE(vs2, vs, link); 536d0728d71SRobert Watson else 537d0728d71SRobert Watson TAILQ_INSERT_TAIL(&vnet_destructors, vs, link); 538a99fcfd4SMarko Zec VNET_SYSINIT_WUNLOCK(); 539d0728d71SRobert Watson } 540d0728d71SRobert Watson 541d0728d71SRobert Watson void 542d0728d71SRobert Watson vnet_deregister_sysuninit(void *arg) 543d0728d71SRobert Watson { 544d0728d71SRobert Watson struct vnet_sysinit *vs; 545d0728d71SRobert Watson struct vnet *vnet; 546d0728d71SRobert Watson 547d0728d71SRobert Watson vs = arg; 548d0728d71SRobert Watson 549d0728d71SRobert Watson /* 550d0728d71SRobert Watson * Invoke the destructor on all the existing vnets when it is 551d0728d71SRobert Watson * deregistered. 552d0728d71SRobert Watson */ 553a99fcfd4SMarko Zec VNET_SYSINIT_WLOCK(); 554d0728d71SRobert Watson VNET_FOREACH(vnet) { 555d0728d71SRobert Watson CURVNET_SET_QUIET(vnet); 556d0728d71SRobert Watson vs->func(vs->arg); 557d0728d71SRobert Watson CURVNET_RESTORE(); 558d0728d71SRobert Watson } 559d0728d71SRobert Watson 560d0728d71SRobert Watson /* Remove the destructor from the global list of vnet destructors. */ 561d0728d71SRobert Watson TAILQ_REMOVE(&vnet_destructors, vs, link); 562a99fcfd4SMarko Zec VNET_SYSINIT_WUNLOCK(); 563d0728d71SRobert Watson } 564d0728d71SRobert Watson 565d0728d71SRobert Watson /* 566c8f6a138SRobert Watson * Invoke all registered vnet constructors on the current vnet. Used during 567c8f6a138SRobert Watson * vnet construction. The caller is responsible for ensuring the new vnet is 568a99fcfd4SMarko Zec * the current vnet and that the vnet_sysinit_sxlock lock is locked. 569d0728d71SRobert Watson */ 570d0728d71SRobert Watson void 571d0728d71SRobert Watson vnet_sysinit(void) 572d0728d71SRobert Watson { 573d0728d71SRobert Watson struct vnet_sysinit *vs; 574d0728d71SRobert Watson 575a99fcfd4SMarko Zec VNET_SYSINIT_RLOCK(); 576d0728d71SRobert Watson TAILQ_FOREACH(vs, &vnet_constructors, link) { 57794081f88SBjoern A. Zeeb curvnet->vnet_state = vs->subsystem; 578d0728d71SRobert Watson vs->func(vs->arg); 579d0728d71SRobert Watson } 580a99fcfd4SMarko Zec VNET_SYSINIT_RUNLOCK(); 581d0728d71SRobert Watson } 582d0728d71SRobert Watson 583d0728d71SRobert Watson /* 584c8f6a138SRobert Watson * Invoke all registered vnet destructors on the current vnet. Used during 585c8f6a138SRobert Watson * vnet destruction. The caller is responsible for ensuring the dying vnet 586a99fcfd4SMarko Zec * the current vnet and that the vnet_sysinit_sxlock lock is locked. 587d0728d71SRobert Watson */ 588d0728d71SRobert Watson void 589d0728d71SRobert Watson vnet_sysuninit(void) 590d0728d71SRobert Watson { 591d0728d71SRobert Watson struct vnet_sysinit *vs; 592d0728d71SRobert Watson 593a99fcfd4SMarko Zec VNET_SYSINIT_RLOCK(); 594d0728d71SRobert Watson TAILQ_FOREACH_REVERSE(vs, &vnet_destructors, vnet_sysuninit_head, 595d0728d71SRobert Watson link) { 59694081f88SBjoern A. Zeeb curvnet->vnet_state = vs->subsystem; 597d0728d71SRobert Watson vs->func(vs->arg); 598d0728d71SRobert Watson } 599a99fcfd4SMarko Zec VNET_SYSINIT_RUNLOCK(); 600d0728d71SRobert Watson } 601530c0060SRobert Watson 60242eedeacSBjoern A. Zeeb /* 60342eedeacSBjoern A. Zeeb * EVENTHANDLER(9) extensions. 60442eedeacSBjoern A. Zeeb */ 60542eedeacSBjoern A. Zeeb /* 60642eedeacSBjoern A. Zeeb * Invoke the eventhandler function originally registered with the possibly 60742eedeacSBjoern A. Zeeb * registered argument for all virtual network stack instances. 60842eedeacSBjoern A. Zeeb * 60942eedeacSBjoern A. Zeeb * This iterator can only be used for eventhandlers that do not take any 61042eedeacSBjoern A. Zeeb * additional arguments, as we do ignore the variadic arguments from the 61142eedeacSBjoern A. Zeeb * EVENTHANDLER_INVOKE() call. 61242eedeacSBjoern A. Zeeb */ 61342eedeacSBjoern A. Zeeb void 61442eedeacSBjoern A. Zeeb vnet_global_eventhandler_iterator_func(void *arg, ...) 61542eedeacSBjoern A. Zeeb { 61642eedeacSBjoern A. Zeeb VNET_ITERATOR_DECL(vnet_iter); 61742eedeacSBjoern A. Zeeb struct eventhandler_entry_vimage *v_ee; 61842eedeacSBjoern A. Zeeb 61942eedeacSBjoern A. Zeeb /* 62042eedeacSBjoern A. Zeeb * There is a bug here in that we should actually cast things to 62142eedeacSBjoern A. Zeeb * (struct eventhandler_entry_ ## name *) but that's not easily 62242eedeacSBjoern A. Zeeb * possible in here so just re-using the variadic version we 62342eedeacSBjoern A. Zeeb * defined for the generic vimage case. 62442eedeacSBjoern A. Zeeb */ 62542eedeacSBjoern A. Zeeb v_ee = arg; 62642eedeacSBjoern A. Zeeb VNET_LIST_RLOCK(); 62742eedeacSBjoern A. Zeeb VNET_FOREACH(vnet_iter) { 62842eedeacSBjoern A. Zeeb CURVNET_SET(vnet_iter); 62942eedeacSBjoern A. Zeeb ((vimage_iterator_func_t)v_ee->func)(v_ee->ee_arg); 63042eedeacSBjoern A. Zeeb CURVNET_RESTORE(); 63142eedeacSBjoern A. Zeeb } 63242eedeacSBjoern A. Zeeb VNET_LIST_RUNLOCK(); 63342eedeacSBjoern A. Zeeb } 63442eedeacSBjoern A. Zeeb 6350a705ab6SMarko Zec #ifdef VNET_DEBUG 6360a705ab6SMarko Zec struct vnet_recursion { 6370a705ab6SMarko Zec SLIST_ENTRY(vnet_recursion) vnr_le; 6380a705ab6SMarko Zec const char *prev_fn; 6390a705ab6SMarko Zec const char *where_fn; 6400a705ab6SMarko Zec int where_line; 6410a705ab6SMarko Zec struct vnet *old_vnet; 6420a705ab6SMarko Zec struct vnet *new_vnet; 6430a705ab6SMarko Zec }; 6440a705ab6SMarko Zec 6450a705ab6SMarko Zec static SLIST_HEAD(, vnet_recursion) vnet_recursions = 6460a705ab6SMarko Zec SLIST_HEAD_INITIALIZER(vnet_recursions); 6470a705ab6SMarko Zec 6480a705ab6SMarko Zec static void 6490a705ab6SMarko Zec vnet_print_recursion(struct vnet_recursion *vnr, int brief) 6500a705ab6SMarko Zec { 6510a705ab6SMarko Zec 6520a705ab6SMarko Zec if (!brief) 6530a705ab6SMarko Zec printf("CURVNET_SET() recursion in "); 6540a705ab6SMarko Zec printf("%s() line %d, prev in %s()", vnr->where_fn, vnr->where_line, 6550a705ab6SMarko Zec vnr->prev_fn); 6560a705ab6SMarko Zec if (brief) 6570a705ab6SMarko Zec printf(", "); 6580a705ab6SMarko Zec else 6590a705ab6SMarko Zec printf("\n "); 6600a705ab6SMarko Zec printf("%p -> %p\n", vnr->old_vnet, vnr->new_vnet); 6610a705ab6SMarko Zec } 6620a705ab6SMarko Zec 6630a705ab6SMarko Zec void 6640a705ab6SMarko Zec vnet_log_recursion(struct vnet *old_vnet, const char *old_fn, int line) 6650a705ab6SMarko Zec { 6660a705ab6SMarko Zec struct vnet_recursion *vnr; 6670a705ab6SMarko Zec 6680a705ab6SMarko Zec /* Skip already logged recursion events. */ 6690a705ab6SMarko Zec SLIST_FOREACH(vnr, &vnet_recursions, vnr_le) 6700a705ab6SMarko Zec if (vnr->prev_fn == old_fn && 6710a705ab6SMarko Zec vnr->where_fn == curthread->td_vnet_lpush && 6720a705ab6SMarko Zec vnr->where_line == line && 6730a705ab6SMarko Zec (vnr->old_vnet == vnr->new_vnet) == (curvnet == old_vnet)) 6740a705ab6SMarko Zec return; 6750a705ab6SMarko Zec 6760a705ab6SMarko Zec vnr = malloc(sizeof(*vnr), M_VNET, M_NOWAIT | M_ZERO); 6770a705ab6SMarko Zec if (vnr == NULL) 6780a705ab6SMarko Zec panic("%s: malloc failed", __func__); 6790a705ab6SMarko Zec vnr->prev_fn = old_fn; 6800a705ab6SMarko Zec vnr->where_fn = curthread->td_vnet_lpush; 6810a705ab6SMarko Zec vnr->where_line = line; 6820a705ab6SMarko Zec vnr->old_vnet = old_vnet; 6830a705ab6SMarko Zec vnr->new_vnet = curvnet; 6840a705ab6SMarko Zec 6850a705ab6SMarko Zec SLIST_INSERT_HEAD(&vnet_recursions, vnr, vnr_le); 6860a705ab6SMarko Zec 6870a705ab6SMarko Zec vnet_print_recursion(vnr, 0); 6880a705ab6SMarko Zec #ifdef KDB 6890a705ab6SMarko Zec kdb_backtrace(); 6900a705ab6SMarko Zec #endif 6910a705ab6SMarko Zec } 6920a705ab6SMarko Zec #endif /* VNET_DEBUG */ 6930a705ab6SMarko Zec 69442eedeacSBjoern A. Zeeb /* 69542eedeacSBjoern A. Zeeb * DDB(4). 69642eedeacSBjoern A. Zeeb */ 697530c0060SRobert Watson #ifdef DDB 6985fa0728bSBjoern A. Zeeb static void 6995fa0728bSBjoern A. Zeeb db_vnet_print(struct vnet *vnet) 7005fa0728bSBjoern A. Zeeb { 7015fa0728bSBjoern A. Zeeb 7025fa0728bSBjoern A. Zeeb db_printf("vnet = %p\n", vnet); 7035fa0728bSBjoern A. Zeeb db_printf(" vnet_magic_n = %#08x (%s, orig %#08x)\n", 7045fa0728bSBjoern A. Zeeb vnet->vnet_magic_n, 7055fa0728bSBjoern A. Zeeb (vnet->vnet_magic_n == VNET_MAGIC_N) ? 7065fa0728bSBjoern A. Zeeb "ok" : "mismatch", VNET_MAGIC_N); 7075fa0728bSBjoern A. Zeeb db_printf(" vnet_ifcnt = %u\n", vnet->vnet_ifcnt); 7085fa0728bSBjoern A. Zeeb db_printf(" vnet_sockcnt = %u\n", vnet->vnet_sockcnt); 7095fa0728bSBjoern A. Zeeb db_printf(" vnet_data_mem = %p\n", vnet->vnet_data_mem); 7105fa0728bSBjoern A. Zeeb db_printf(" vnet_data_base = %#jx\n", 7115fa0728bSBjoern A. Zeeb (uintmax_t)vnet->vnet_data_base); 71294081f88SBjoern A. Zeeb db_printf(" vnet_state = %#08x\n", vnet->vnet_state); 7135fa0728bSBjoern A. Zeeb db_printf("\n"); 7145fa0728bSBjoern A. Zeeb } 7155fa0728bSBjoern A. Zeeb 7165fa0728bSBjoern A. Zeeb DB_SHOW_ALL_COMMAND(vnets, db_show_all_vnets) 717530c0060SRobert Watson { 718530c0060SRobert Watson VNET_ITERATOR_DECL(vnet_iter); 719530c0060SRobert Watson 720530c0060SRobert Watson VNET_FOREACH(vnet_iter) { 7215fa0728bSBjoern A. Zeeb db_vnet_print(vnet_iter); 722281c86a4SBjoern A. Zeeb if (db_pager_quit) 723281c86a4SBjoern A. Zeeb break; 724530c0060SRobert Watson } 725530c0060SRobert Watson } 7260a705ab6SMarko Zec 7275fa0728bSBjoern A. Zeeb DB_SHOW_COMMAND(vnet, db_show_vnet) 7285fa0728bSBjoern A. Zeeb { 7295fa0728bSBjoern A. Zeeb 7305fa0728bSBjoern A. Zeeb if (!have_addr) { 7315fa0728bSBjoern A. Zeeb db_printf("usage: show vnet <struct vnet *>\n"); 7325fa0728bSBjoern A. Zeeb return; 7335fa0728bSBjoern A. Zeeb } 7345fa0728bSBjoern A. Zeeb 7355fa0728bSBjoern A. Zeeb db_vnet_print((struct vnet *)addr); 7365fa0728bSBjoern A. Zeeb } 7375fa0728bSBjoern A. Zeeb 7383e0490b3SBjoern A. Zeeb static void 7393e0490b3SBjoern A. Zeeb db_show_vnet_print_vs(struct vnet_sysinit *vs, int ddb) 7403e0490b3SBjoern A. Zeeb { 7413e0490b3SBjoern A. Zeeb const char *vsname, *funcname; 7423e0490b3SBjoern A. Zeeb c_db_sym_t sym; 7433e0490b3SBjoern A. Zeeb db_expr_t offset; 7443e0490b3SBjoern A. Zeeb 7453e0490b3SBjoern A. Zeeb #define xprint(...) \ 7463e0490b3SBjoern A. Zeeb if (ddb) \ 7473e0490b3SBjoern A. Zeeb db_printf(__VA_ARGS__); \ 7483e0490b3SBjoern A. Zeeb else \ 7493e0490b3SBjoern A. Zeeb printf(__VA_ARGS__) 7503e0490b3SBjoern A. Zeeb 7513e0490b3SBjoern A. Zeeb if (vs == NULL) { 7523e0490b3SBjoern A. Zeeb xprint("%s: no vnet_sysinit * given\n", __func__); 7533e0490b3SBjoern A. Zeeb return; 7543e0490b3SBjoern A. Zeeb } 7553e0490b3SBjoern A. Zeeb 7563e0490b3SBjoern A. Zeeb sym = db_search_symbol((vm_offset_t)vs, DB_STGY_ANY, &offset); 7573e0490b3SBjoern A. Zeeb db_symbol_values(sym, &vsname, NULL); 7583e0490b3SBjoern A. Zeeb sym = db_search_symbol((vm_offset_t)vs->func, DB_STGY_PROC, &offset); 7593e0490b3SBjoern A. Zeeb db_symbol_values(sym, &funcname, NULL); 7603e0490b3SBjoern A. Zeeb xprint("%s(%p)\n", (vsname != NULL) ? vsname : "", vs); 7615fa0728bSBjoern A. Zeeb xprint(" %#08x %#08x\n", vs->subsystem, vs->order); 7623e0490b3SBjoern A. Zeeb xprint(" %p(%s)(%p)\n", 7633e0490b3SBjoern A. Zeeb vs->func, (funcname != NULL) ? funcname : "", vs->arg); 7643e0490b3SBjoern A. Zeeb #undef xprint 7653e0490b3SBjoern A. Zeeb } 7663e0490b3SBjoern A. Zeeb 7673e0490b3SBjoern A. Zeeb DB_SHOW_COMMAND(vnet_sysinit, db_show_vnet_sysinit) 7683e0490b3SBjoern A. Zeeb { 7693e0490b3SBjoern A. Zeeb struct vnet_sysinit *vs; 7703e0490b3SBjoern A. Zeeb 7713e0490b3SBjoern A. Zeeb db_printf("VNET_SYSINIT vs Name(Ptr)\n"); 7723e0490b3SBjoern A. Zeeb db_printf(" Subsystem Order\n"); 7733e0490b3SBjoern A. Zeeb db_printf(" Function(Name)(Arg)\n"); 7743e0490b3SBjoern A. Zeeb TAILQ_FOREACH(vs, &vnet_constructors, link) { 7753e0490b3SBjoern A. Zeeb db_show_vnet_print_vs(vs, 1); 7763e0490b3SBjoern A. Zeeb if (db_pager_quit) 7773e0490b3SBjoern A. Zeeb break; 7783e0490b3SBjoern A. Zeeb } 7793e0490b3SBjoern A. Zeeb } 7803e0490b3SBjoern A. Zeeb 7813e0490b3SBjoern A. Zeeb DB_SHOW_COMMAND(vnet_sysuninit, db_show_vnet_sysuninit) 7823e0490b3SBjoern A. Zeeb { 7833e0490b3SBjoern A. Zeeb struct vnet_sysinit *vs; 7843e0490b3SBjoern A. Zeeb 7853e0490b3SBjoern A. Zeeb db_printf("VNET_SYSUNINIT vs Name(Ptr)\n"); 7863e0490b3SBjoern A. Zeeb db_printf(" Subsystem Order\n"); 7873e0490b3SBjoern A. Zeeb db_printf(" Function(Name)(Arg)\n"); 7883e0490b3SBjoern A. Zeeb TAILQ_FOREACH_REVERSE(vs, &vnet_destructors, vnet_sysuninit_head, 7893e0490b3SBjoern A. Zeeb link) { 7903e0490b3SBjoern A. Zeeb db_show_vnet_print_vs(vs, 1); 7913e0490b3SBjoern A. Zeeb if (db_pager_quit) 7923e0490b3SBjoern A. Zeeb break; 7933e0490b3SBjoern A. Zeeb } 7943e0490b3SBjoern A. Zeeb } 7953e0490b3SBjoern A. Zeeb 7960a705ab6SMarko Zec #ifdef VNET_DEBUG 7970a705ab6SMarko Zec DB_SHOW_COMMAND(vnetrcrs, db_show_vnetrcrs) 7980a705ab6SMarko Zec { 7990a705ab6SMarko Zec struct vnet_recursion *vnr; 8000a705ab6SMarko Zec 8010a705ab6SMarko Zec SLIST_FOREACH(vnr, &vnet_recursions, vnr_le) 8020a705ab6SMarko Zec vnet_print_recursion(vnr, 1); 8030a705ab6SMarko Zec } 804530c0060SRobert Watson #endif 8050a705ab6SMarko Zec #endif /* DDB */ 806