xref: /freebsd/sys/net/vnet.c (revision a7acce3491987c116e607697a4906807f4ea1c10)
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  */
174c955c6cdSBjoern A. Zeeb #define	VNET_MODMIN	(8 * PAGE_SIZE)
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  */
181cd2106eaSAndrew 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 
223fb9b76e0SZhenlei Huang /*
224fb9b76e0SZhenlei Huang  * Run per-vnet sysinits or sysuninits during vnet creation/destruction.
225fb9b76e0SZhenlei Huang  */
226fb9b76e0SZhenlei Huang static void vnet_sysinit(void);
227fb9b76e0SZhenlei Huang static void vnet_sysuninit(void);
228fb9b76e0SZhenlei Huang 
2293e0490b3SBjoern A. Zeeb #ifdef DDB
2303e0490b3SBjoern A. Zeeb static void db_show_vnet_print_vs(struct vnet_sysinit *, int);
2313e0490b3SBjoern A. Zeeb #endif
2323e0490b3SBjoern A. Zeeb 
233eddfbb76SRobert Watson /*
234530c0060SRobert Watson  * Allocate a virtual network stack.
235530c0060SRobert Watson  */
236530c0060SRobert Watson struct vnet *
237530c0060SRobert Watson vnet_alloc(void)
238530c0060SRobert Watson {
239530c0060SRobert Watson 	struct vnet *vnet;
240530c0060SRobert Watson 
24161d033d4SBjoern A. Zeeb 	SDT_PROBE1(vnet, functions, vnet_alloc, entry, __LINE__);
242530c0060SRobert Watson 	vnet = malloc(sizeof(struct vnet), M_VNET, M_WAITOK | M_ZERO);
243530c0060SRobert Watson 	vnet->vnet_magic_n = VNET_MAGIC_N;
24461d033d4SBjoern A. Zeeb 	SDT_PROBE2(vnet, functions, vnet_alloc, alloc, __LINE__, vnet);
2456bc2c7b7SRobert Watson 
2466bc2c7b7SRobert Watson 	/*
2476bc2c7b7SRobert Watson 	 * Allocate storage for virtualized global variables and copy in
248*a7acce34SZhenlei Huang 	 * initial values from our 'master' copy.
2496bc2c7b7SRobert Watson 	 */
2506bc2c7b7SRobert Watson 	vnet->vnet_data_mem = malloc(VNET_SIZE, M_VNET_DATA, M_WAITOK);
2516bc2c7b7SRobert Watson 	memcpy(vnet->vnet_data_mem, (void *)VNET_START, VNET_BYTES);
2526bc2c7b7SRobert Watson 
2536bc2c7b7SRobert Watson 	/*
2546bc2c7b7SRobert Watson 	 * All use of vnet-specific data will immediately subtract VNET_START
2556bc2c7b7SRobert Watson 	 * from the base memory pointer, so pre-calculate that now to avoid
2566bc2c7b7SRobert Watson 	 * it on each use.
2576bc2c7b7SRobert Watson 	 */
2586bc2c7b7SRobert Watson 	vnet->vnet_data_base = (uintptr_t)vnet->vnet_data_mem - VNET_START;
259530c0060SRobert Watson 
260530c0060SRobert Watson 	/* Initialize / attach vnet module instances. */
261530c0060SRobert Watson 	CURVNET_SET_QUIET(vnet);
262530c0060SRobert Watson 	vnet_sysinit();
263530c0060SRobert Watson 	CURVNET_RESTORE();
264530c0060SRobert Watson 
265a99fcfd4SMarko Zec 	VNET_LIST_WLOCK();
266530c0060SRobert Watson 	LIST_INSERT_HEAD(&vnet_head, vnet, vnet_le);
267530c0060SRobert Watson 	VNET_LIST_WUNLOCK();
268530c0060SRobert Watson 
26961d033d4SBjoern A. Zeeb 	SDT_PROBE2(vnet, functions, vnet_alloc, return, __LINE__, vnet);
270530c0060SRobert Watson 	return (vnet);
271530c0060SRobert Watson }
272530c0060SRobert Watson 
273530c0060SRobert Watson /*
274530c0060SRobert Watson  * Destroy a virtual network stack.
275530c0060SRobert Watson  */
276530c0060SRobert Watson void
277530c0060SRobert Watson vnet_destroy(struct vnet *vnet)
278530c0060SRobert Watson {
279530c0060SRobert Watson 
28061d033d4SBjoern A. Zeeb 	SDT_PROBE2(vnet, functions, vnet_destroy, entry, __LINE__, vnet);
281530c0060SRobert Watson 	KASSERT(vnet->vnet_sockcnt == 0,
282530c0060SRobert Watson 	    ("%s: vnet still has sockets", __func__));
283530c0060SRobert Watson 
284530c0060SRobert Watson 	VNET_LIST_WLOCK();
285530c0060SRobert Watson 	LIST_REMOVE(vnet, vnet_le);
286a99fcfd4SMarko Zec 	VNET_LIST_WUNLOCK();
287530c0060SRobert Watson 
28810108cb6SBjoern A. Zeeb 	/* Signal that VNET is being shutdown. */
28910108cb6SBjoern A. Zeeb 	vnet->vnet_shutdown = true;
29010108cb6SBjoern A. Zeeb 
291530c0060SRobert Watson 	CURVNET_SET_QUIET(vnet);
2926d2a10d9SKristof Provost 	sx_xlock(&ifnet_detach_sxlock);
293530c0060SRobert Watson 	vnet_sysuninit();
2946d2a10d9SKristof Provost 	sx_xunlock(&ifnet_detach_sxlock);
295530c0060SRobert Watson 	CURVNET_RESTORE();
296530c0060SRobert Watson 
2976bc2c7b7SRobert Watson 	/*
2986bc2c7b7SRobert Watson 	 * Release storage for the virtual network stack instance.
2996bc2c7b7SRobert Watson 	 */
3006bc2c7b7SRobert Watson 	free(vnet->vnet_data_mem, M_VNET_DATA);
3016bc2c7b7SRobert Watson 	vnet->vnet_data_mem = NULL;
3026bc2c7b7SRobert Watson 	vnet->vnet_data_base = 0;
303530c0060SRobert Watson 	vnet->vnet_magic_n = 0xdeadbeef;
304530c0060SRobert Watson 	free(vnet, M_VNET);
30561d033d4SBjoern A. Zeeb 	SDT_PROBE1(vnet, functions, vnet_destroy, return, __LINE__);
306530c0060SRobert Watson }
307530c0060SRobert Watson 
308530c0060SRobert Watson /*
309530c0060SRobert Watson  * Boot time initialization and allocation of virtual network stacks.
310530c0060SRobert Watson  */
311530c0060SRobert Watson static void
31254d9f34eSBjoern A. Zeeb vnet_init_prelink(void *arg __unused)
313530c0060SRobert Watson {
314530c0060SRobert Watson 
315530c0060SRobert Watson 	rw_init(&vnet_rwlock, "vnet_rwlock");
316530c0060SRobert Watson 	sx_init(&vnet_sxlock, "vnet_sxlock");
317a99fcfd4SMarko Zec 	sx_init(&vnet_sysinit_sxlock, "vnet_sysinit_sxlock");
318530c0060SRobert Watson 	LIST_INIT(&vnet_head);
319530c0060SRobert Watson }
320530c0060SRobert Watson SYSINIT(vnet_init_prelink, SI_SUB_VNET_PRELINK, SI_ORDER_FIRST,
321530c0060SRobert Watson     vnet_init_prelink, NULL);
322530c0060SRobert Watson 
323530c0060SRobert Watson static void
32454d9f34eSBjoern A. Zeeb vnet0_init(void *arg __unused)
325530c0060SRobert Watson {
326530c0060SRobert Watson 
3278e94025bSBjoern A. Zeeb 	if (bootverbose)
3288e94025bSBjoern A. Zeeb 		printf("VIMAGE (virtualized network stack) enabled\n");
3296aad5c1cSRobert Watson 
330530c0060SRobert Watson 	/*
331530c0060SRobert Watson 	 * We MUST clear curvnet in vi_init_done() before going SMP,
332530c0060SRobert Watson 	 * otherwise CURVNET_SET() macros would scream about unnecessary
333530c0060SRobert Watson 	 * curvnet recursions.
334530c0060SRobert Watson 	 */
335530c0060SRobert Watson 	curvnet = prison0.pr_vnet = vnet0 = vnet_alloc();
336530c0060SRobert Watson }
337530c0060SRobert Watson SYSINIT(vnet0_init, SI_SUB_VNET, SI_ORDER_FIRST, vnet0_init, NULL);
338530c0060SRobert Watson 
339530c0060SRobert Watson static void
34054d9f34eSBjoern A. Zeeb vnet_init_done(void *unused __unused)
341530c0060SRobert Watson {
342530c0060SRobert Watson 
343530c0060SRobert Watson 	curvnet = NULL;
344530c0060SRobert Watson }
34589856f7eSBjoern A. Zeeb SYSINIT(vnet_init_done, SI_SUB_VNET_DONE, SI_ORDER_ANY, vnet_init_done,
346530c0060SRobert Watson     NULL);
347530c0060SRobert Watson 
348530c0060SRobert Watson /*
349eddfbb76SRobert Watson  * Once on boot, initialize the modspace freelist to entirely cover modspace.
350eddfbb76SRobert Watson  */
351eddfbb76SRobert Watson static void
352eddfbb76SRobert Watson vnet_data_startup(void *dummy __unused)
353eddfbb76SRobert Watson {
354eddfbb76SRobert Watson 	struct vnet_data_free *df;
355eddfbb76SRobert Watson 
356eddfbb76SRobert Watson 	df = malloc(sizeof(*df), M_VNET_DATA_FREE, M_WAITOK | M_ZERO);
357eddfbb76SRobert Watson 	df->vnd_start = (uintptr_t)&VNET_NAME(modspace);
358793f71bfSBjoern A. Zeeb 	df->vnd_len = VNET_MODMIN;
359eddfbb76SRobert Watson 	TAILQ_INSERT_HEAD(&vnet_data_free_head, df, vnd_link);
360eddfbb76SRobert Watson 	sx_init(&vnet_data_free_lock, "vnet_data alloc lock");
361eddfbb76SRobert Watson }
362891cf3edSEd Maste SYSINIT(vnet_data, SI_SUB_KLD, SI_ORDER_FIRST, vnet_data_startup, NULL);
363eddfbb76SRobert Watson 
36410108cb6SBjoern A. Zeeb /* Dummy VNET_SYSINIT to make sure we always reach the final end state. */
36500e36a5cSBjoern A. Zeeb static void
36610108cb6SBjoern A. Zeeb vnet_sysinit_done(void *unused __unused)
36700e36a5cSBjoern A. Zeeb {
36800e36a5cSBjoern A. Zeeb 
36910108cb6SBjoern A. Zeeb 	return;
37000e36a5cSBjoern A. Zeeb }
37110108cb6SBjoern A. Zeeb VNET_SYSINIT(vnet_sysinit_done, SI_SUB_VNET_DONE, SI_ORDER_ANY,
37210108cb6SBjoern A. Zeeb     vnet_sysinit_done, NULL);
37300e36a5cSBjoern A. Zeeb 
374eddfbb76SRobert Watson /*
375eddfbb76SRobert Watson  * When a module is loaded and requires storage for a virtualized global
376eddfbb76SRobert Watson  * variable, allocate space from the modspace free list.  This interface
377eddfbb76SRobert Watson  * should be used only by the kernel linker.
378eddfbb76SRobert Watson  */
379eddfbb76SRobert Watson void *
380eddfbb76SRobert Watson vnet_data_alloc(int size)
381eddfbb76SRobert Watson {
382eddfbb76SRobert Watson 	struct vnet_data_free *df;
383eddfbb76SRobert Watson 	void *s;
384eddfbb76SRobert Watson 
385eddfbb76SRobert Watson 	s = NULL;
386eddfbb76SRobert Watson 	size = roundup2(size, sizeof(void *));
387eddfbb76SRobert Watson 	sx_xlock(&vnet_data_free_lock);
388eddfbb76SRobert Watson 	TAILQ_FOREACH(df, &vnet_data_free_head, vnd_link) {
389eddfbb76SRobert Watson 		if (df->vnd_len < size)
390eddfbb76SRobert Watson 			continue;
391eddfbb76SRobert Watson 		if (df->vnd_len == size) {
392eddfbb76SRobert Watson 			s = (void *)df->vnd_start;
393eddfbb76SRobert Watson 			TAILQ_REMOVE(&vnet_data_free_head, df, vnd_link);
394eddfbb76SRobert Watson 			free(df, M_VNET_DATA_FREE);
395eddfbb76SRobert Watson 			break;
396eddfbb76SRobert Watson 		}
397eddfbb76SRobert Watson 		s = (void *)df->vnd_start;
398eddfbb76SRobert Watson 		df->vnd_len -= size;
399eddfbb76SRobert Watson 		df->vnd_start = df->vnd_start + size;
400eddfbb76SRobert Watson 		break;
401eddfbb76SRobert Watson 	}
402eddfbb76SRobert Watson 	sx_xunlock(&vnet_data_free_lock);
403eddfbb76SRobert Watson 
404eddfbb76SRobert Watson 	return (s);
405eddfbb76SRobert Watson }
406eddfbb76SRobert Watson 
407eddfbb76SRobert Watson /*
408eddfbb76SRobert Watson  * Free space for a virtualized global variable on module unload.
409eddfbb76SRobert Watson  */
410eddfbb76SRobert Watson void
411eddfbb76SRobert Watson vnet_data_free(void *start_arg, int size)
412eddfbb76SRobert Watson {
413eddfbb76SRobert Watson 	struct vnet_data_free *df;
414eddfbb76SRobert Watson 	struct vnet_data_free *dn;
415eddfbb76SRobert Watson 	uintptr_t start;
416eddfbb76SRobert Watson 	uintptr_t end;
417eddfbb76SRobert Watson 
418eddfbb76SRobert Watson 	size = roundup2(size, sizeof(void *));
419eddfbb76SRobert Watson 	start = (uintptr_t)start_arg;
420eddfbb76SRobert Watson 	end = start + size;
421eddfbb76SRobert Watson 	/*
422eddfbb76SRobert Watson 	 * Free a region of space and merge it with as many neighbors as
423eddfbb76SRobert Watson 	 * possible.  Keeping the list sorted simplifies this operation.
424eddfbb76SRobert Watson 	 */
425eddfbb76SRobert Watson 	sx_xlock(&vnet_data_free_lock);
426eddfbb76SRobert Watson 	TAILQ_FOREACH(df, &vnet_data_free_head, vnd_link) {
427eddfbb76SRobert Watson 		if (df->vnd_start > end)
428eddfbb76SRobert Watson 			break;
429eddfbb76SRobert Watson 		/*
430c8f6a138SRobert Watson 		 * If we expand at the end of an entry we may have to merge
431c8f6a138SRobert Watson 		 * it with the one following it as well.
432eddfbb76SRobert Watson 		 */
433eddfbb76SRobert Watson 		if (df->vnd_start + df->vnd_len == start) {
434eddfbb76SRobert Watson 			df->vnd_len += size;
435eddfbb76SRobert Watson 			dn = TAILQ_NEXT(df, vnd_link);
436eddfbb76SRobert Watson 			if (df->vnd_start + df->vnd_len == dn->vnd_start) {
437eddfbb76SRobert Watson 				df->vnd_len += dn->vnd_len;
438c8f6a138SRobert Watson 				TAILQ_REMOVE(&vnet_data_free_head, dn,
439c8f6a138SRobert Watson 				    vnd_link);
440eddfbb76SRobert Watson 				free(dn, M_VNET_DATA_FREE);
441eddfbb76SRobert Watson 			}
442eddfbb76SRobert Watson 			sx_xunlock(&vnet_data_free_lock);
443eddfbb76SRobert Watson 			return;
444eddfbb76SRobert Watson 		}
445eddfbb76SRobert Watson 		if (df->vnd_start == end) {
446eddfbb76SRobert Watson 			df->vnd_start = start;
447eddfbb76SRobert Watson 			df->vnd_len += size;
448eddfbb76SRobert Watson 			sx_xunlock(&vnet_data_free_lock);
449eddfbb76SRobert Watson 			return;
450eddfbb76SRobert Watson 		}
451eddfbb76SRobert Watson 	}
452eddfbb76SRobert Watson 	dn = malloc(sizeof(*df), M_VNET_DATA_FREE, M_WAITOK | M_ZERO);
453eddfbb76SRobert Watson 	dn->vnd_start = start;
454eddfbb76SRobert Watson 	dn->vnd_len = size;
455eddfbb76SRobert Watson 	if (df)
456eddfbb76SRobert Watson 		TAILQ_INSERT_BEFORE(df, dn, vnd_link);
457eddfbb76SRobert Watson 	else
458eddfbb76SRobert Watson 		TAILQ_INSERT_TAIL(&vnet_data_free_head, dn, vnd_link);
459eddfbb76SRobert Watson 	sx_xunlock(&vnet_data_free_lock);
460eddfbb76SRobert Watson }
461eddfbb76SRobert Watson 
462eddfbb76SRobert Watson /*
463eddfbb76SRobert Watson  * When a new virtualized global variable has been allocated, propagate its
464eddfbb76SRobert Watson  * initial value to each already-allocated virtual network stack instance.
465eddfbb76SRobert Watson  */
466eddfbb76SRobert Watson void
467eddfbb76SRobert Watson vnet_data_copy(void *start, int size)
468eddfbb76SRobert Watson {
4697429a3f3SRobert Watson 	struct vnet *vnet;
470eddfbb76SRobert Watson 
4717429a3f3SRobert Watson 	VNET_LIST_RLOCK();
4727429a3f3SRobert Watson 	LIST_FOREACH(vnet, &vnet_head, vnet_le)
4737429a3f3SRobert Watson 		memcpy((void *)((uintptr_t)vnet->vnet_data_base +
4747429a3f3SRobert Watson 		    (uintptr_t)start), start, size);
4757429a3f3SRobert Watson 	VNET_LIST_RUNLOCK();
476eddfbb76SRobert Watson }
477eddfbb76SRobert Watson 
478eddfbb76SRobert Watson /*
479d0728d71SRobert Watson  * Support for special SYSINIT handlers registered via VNET_SYSINIT()
480d0728d71SRobert Watson  * and VNET_SYSUNINIT().
481d0728d71SRobert Watson  */
482d0728d71SRobert Watson void
483d0728d71SRobert Watson vnet_register_sysinit(void *arg)
484d0728d71SRobert Watson {
485d0728d71SRobert Watson 	struct vnet_sysinit *vs, *vs2;
486d0728d71SRobert Watson 	struct vnet *vnet;
487d0728d71SRobert Watson 
488d0728d71SRobert Watson 	vs = arg;
489d0728d71SRobert Watson 	KASSERT(vs->subsystem > SI_SUB_VNET, ("vnet sysinit too early"));
490d0728d71SRobert Watson 
491d0728d71SRobert Watson 	/* Add the constructor to the global list of vnet constructors. */
492a99fcfd4SMarko Zec 	VNET_SYSINIT_WLOCK();
493d0728d71SRobert Watson 	TAILQ_FOREACH(vs2, &vnet_constructors, link) {
494d0728d71SRobert Watson 		if (vs2->subsystem > vs->subsystem)
495d0728d71SRobert Watson 			break;
496d0728d71SRobert Watson 		if (vs2->subsystem == vs->subsystem && vs2->order > vs->order)
497d0728d71SRobert Watson 			break;
498d0728d71SRobert Watson 	}
499d0728d71SRobert Watson 	if (vs2 != NULL)
500d0728d71SRobert Watson 		TAILQ_INSERT_BEFORE(vs2, vs, link);
501d0728d71SRobert Watson 	else
502d0728d71SRobert Watson 		TAILQ_INSERT_TAIL(&vnet_constructors, vs, link);
503d0728d71SRobert Watson 
504d0728d71SRobert Watson 	/*
505d0728d71SRobert Watson 	 * Invoke the constructor on all the existing vnets when it is
506d0728d71SRobert Watson 	 * registered.
507d0728d71SRobert Watson 	 */
508d0728d71SRobert Watson 	VNET_FOREACH(vnet) {
509d0728d71SRobert Watson 		CURVNET_SET_QUIET(vnet);
510d0728d71SRobert Watson 		vs->func(vs->arg);
511d0728d71SRobert Watson 		CURVNET_RESTORE();
512d0728d71SRobert Watson 	}
513a99fcfd4SMarko Zec 	VNET_SYSINIT_WUNLOCK();
514d0728d71SRobert Watson }
515d0728d71SRobert Watson 
516d0728d71SRobert Watson void
517d0728d71SRobert Watson vnet_deregister_sysinit(void *arg)
518d0728d71SRobert Watson {
519d0728d71SRobert Watson 	struct vnet_sysinit *vs;
520d0728d71SRobert Watson 
521d0728d71SRobert Watson 	vs = arg;
522d0728d71SRobert Watson 
523d0728d71SRobert Watson 	/* Remove the constructor from the global list of vnet constructors. */
524a99fcfd4SMarko Zec 	VNET_SYSINIT_WLOCK();
525d0728d71SRobert Watson 	TAILQ_REMOVE(&vnet_constructors, vs, link);
526a99fcfd4SMarko Zec 	VNET_SYSINIT_WUNLOCK();
527d0728d71SRobert Watson }
528d0728d71SRobert Watson 
529d0728d71SRobert Watson void
530d0728d71SRobert Watson vnet_register_sysuninit(void *arg)
531d0728d71SRobert Watson {
532d0728d71SRobert Watson 	struct vnet_sysinit *vs, *vs2;
533d0728d71SRobert Watson 
534d0728d71SRobert Watson 	vs = arg;
535d0728d71SRobert Watson 
536d0728d71SRobert Watson 	/* Add the destructor to the global list of vnet destructors. */
537a99fcfd4SMarko Zec 	VNET_SYSINIT_WLOCK();
538d0728d71SRobert Watson 	TAILQ_FOREACH(vs2, &vnet_destructors, link) {
539d0728d71SRobert Watson 		if (vs2->subsystem > vs->subsystem)
540d0728d71SRobert Watson 			break;
541d0728d71SRobert Watson 		if (vs2->subsystem == vs->subsystem && vs2->order > vs->order)
542d0728d71SRobert Watson 			break;
543d0728d71SRobert Watson 	}
544d0728d71SRobert Watson 	if (vs2 != NULL)
545d0728d71SRobert Watson 		TAILQ_INSERT_BEFORE(vs2, vs, link);
546d0728d71SRobert Watson 	else
547d0728d71SRobert Watson 		TAILQ_INSERT_TAIL(&vnet_destructors, vs, link);
548a99fcfd4SMarko Zec 	VNET_SYSINIT_WUNLOCK();
549d0728d71SRobert Watson }
550d0728d71SRobert Watson 
551d0728d71SRobert Watson void
552d0728d71SRobert Watson vnet_deregister_sysuninit(void *arg)
553d0728d71SRobert Watson {
554d0728d71SRobert Watson 	struct vnet_sysinit *vs;
555d0728d71SRobert Watson 	struct vnet *vnet;
556d0728d71SRobert Watson 
557d0728d71SRobert Watson 	vs = arg;
558d0728d71SRobert Watson 
559d0728d71SRobert Watson 	/*
560d0728d71SRobert Watson 	 * Invoke the destructor on all the existing vnets when it is
561d0728d71SRobert Watson 	 * deregistered.
562d0728d71SRobert Watson 	 */
563a99fcfd4SMarko Zec 	VNET_SYSINIT_WLOCK();
564d0728d71SRobert Watson 	VNET_FOREACH(vnet) {
565d0728d71SRobert Watson 		CURVNET_SET_QUIET(vnet);
566d0728d71SRobert Watson 		vs->func(vs->arg);
567d0728d71SRobert Watson 		CURVNET_RESTORE();
568d0728d71SRobert Watson 	}
569d0728d71SRobert Watson 
570d0728d71SRobert Watson 	/* Remove the destructor from the global list of vnet destructors. */
571d0728d71SRobert Watson 	TAILQ_REMOVE(&vnet_destructors, vs, link);
572a99fcfd4SMarko Zec 	VNET_SYSINIT_WUNLOCK();
573d0728d71SRobert Watson }
574d0728d71SRobert Watson 
575d0728d71SRobert Watson /*
576c8f6a138SRobert Watson  * Invoke all registered vnet constructors on the current vnet.  Used during
577c8f6a138SRobert Watson  * vnet construction.  The caller is responsible for ensuring the new vnet is
578a99fcfd4SMarko Zec  * the current vnet and that the vnet_sysinit_sxlock lock is locked.
579d0728d71SRobert Watson  */
580fb9b76e0SZhenlei Huang static void
581d0728d71SRobert Watson vnet_sysinit(void)
582d0728d71SRobert Watson {
583d0728d71SRobert Watson 	struct vnet_sysinit *vs;
584d0728d71SRobert Watson 
585a99fcfd4SMarko Zec 	VNET_SYSINIT_RLOCK();
58610108cb6SBjoern A. Zeeb 	TAILQ_FOREACH(vs, &vnet_constructors, link) {
58710108cb6SBjoern A. Zeeb 		curvnet->vnet_state = vs->subsystem;
588d0728d71SRobert Watson 		vs->func(vs->arg);
58910108cb6SBjoern A. Zeeb 	}
590a99fcfd4SMarko Zec 	VNET_SYSINIT_RUNLOCK();
591d0728d71SRobert Watson }
592d0728d71SRobert Watson 
593d0728d71SRobert Watson /*
594c8f6a138SRobert Watson  * Invoke all registered vnet destructors on the current vnet.  Used during
595c8f6a138SRobert Watson  * vnet destruction.  The caller is responsible for ensuring the dying vnet
596a99fcfd4SMarko Zec  * the current vnet and that the vnet_sysinit_sxlock lock is locked.
597d0728d71SRobert Watson  */
598fb9b76e0SZhenlei Huang static void
599d0728d71SRobert Watson vnet_sysuninit(void)
600d0728d71SRobert Watson {
601d0728d71SRobert Watson 	struct vnet_sysinit *vs;
602d0728d71SRobert Watson 
603a99fcfd4SMarko Zec 	VNET_SYSINIT_RLOCK();
604d0728d71SRobert Watson 	TAILQ_FOREACH_REVERSE(vs, &vnet_destructors, vnet_sysuninit_head,
60510108cb6SBjoern A. Zeeb 	    link) {
60610108cb6SBjoern A. Zeeb 		curvnet->vnet_state = vs->subsystem;
607d0728d71SRobert Watson 		vs->func(vs->arg);
60810108cb6SBjoern A. Zeeb 	}
609a99fcfd4SMarko Zec 	VNET_SYSINIT_RUNLOCK();
610d0728d71SRobert Watson }
611530c0060SRobert Watson 
61242eedeacSBjoern A. Zeeb /*
61342eedeacSBjoern A. Zeeb  * EVENTHANDLER(9) extensions.
61442eedeacSBjoern A. Zeeb  */
61542eedeacSBjoern A. Zeeb /*
61642eedeacSBjoern A. Zeeb  * Invoke the eventhandler function originally registered with the possibly
61742eedeacSBjoern A. Zeeb  * registered argument for all virtual network stack instances.
61842eedeacSBjoern A. Zeeb  *
61942eedeacSBjoern A. Zeeb  * This iterator can only be used for eventhandlers that do not take any
62042eedeacSBjoern A. Zeeb  * additional arguments, as we do ignore the variadic arguments from the
62142eedeacSBjoern A. Zeeb  * EVENTHANDLER_INVOKE() call.
62242eedeacSBjoern A. Zeeb  */
62342eedeacSBjoern A. Zeeb void
62442eedeacSBjoern A. Zeeb vnet_global_eventhandler_iterator_func(void *arg, ...)
62542eedeacSBjoern A. Zeeb {
62642eedeacSBjoern A. Zeeb 	VNET_ITERATOR_DECL(vnet_iter);
62742eedeacSBjoern A. Zeeb 	struct eventhandler_entry_vimage *v_ee;
62842eedeacSBjoern A. Zeeb 
62942eedeacSBjoern A. Zeeb 	/*
63042eedeacSBjoern A. Zeeb 	 * There is a bug here in that we should actually cast things to
63142eedeacSBjoern A. Zeeb 	 * (struct eventhandler_entry_ ## name *)  but that's not easily
63242eedeacSBjoern A. Zeeb 	 * possible in here so just re-using the variadic version we
63342eedeacSBjoern A. Zeeb 	 * defined for the generic vimage case.
63442eedeacSBjoern A. Zeeb 	 */
63542eedeacSBjoern A. Zeeb 	v_ee = arg;
63642eedeacSBjoern A. Zeeb 	VNET_LIST_RLOCK();
63742eedeacSBjoern A. Zeeb 	VNET_FOREACH(vnet_iter) {
63842eedeacSBjoern A. Zeeb 		CURVNET_SET(vnet_iter);
63942eedeacSBjoern A. Zeeb 		((vimage_iterator_func_t)v_ee->func)(v_ee->ee_arg);
64042eedeacSBjoern A. Zeeb 		CURVNET_RESTORE();
64142eedeacSBjoern A. Zeeb 	}
64242eedeacSBjoern A. Zeeb 	VNET_LIST_RUNLOCK();
64342eedeacSBjoern A. Zeeb }
64442eedeacSBjoern A. Zeeb 
6450a705ab6SMarko Zec #ifdef VNET_DEBUG
6460a705ab6SMarko Zec struct vnet_recursion {
6470a705ab6SMarko Zec 	SLIST_ENTRY(vnet_recursion)	 vnr_le;
6480a705ab6SMarko Zec 	const char			*prev_fn;
6490a705ab6SMarko Zec 	const char			*where_fn;
6500a705ab6SMarko Zec 	int				 where_line;
6510a705ab6SMarko Zec 	struct vnet			*old_vnet;
6520a705ab6SMarko Zec 	struct vnet			*new_vnet;
6530a705ab6SMarko Zec };
6540a705ab6SMarko Zec 
6550a705ab6SMarko Zec static SLIST_HEAD(, vnet_recursion) vnet_recursions =
6560a705ab6SMarko Zec     SLIST_HEAD_INITIALIZER(vnet_recursions);
6570a705ab6SMarko Zec 
6580a705ab6SMarko Zec static void
6590a705ab6SMarko Zec vnet_print_recursion(struct vnet_recursion *vnr, int brief)
6600a705ab6SMarko Zec {
6610a705ab6SMarko Zec 
6620a705ab6SMarko Zec 	if (!brief)
6630a705ab6SMarko Zec 		printf("CURVNET_SET() recursion in ");
6640a705ab6SMarko Zec 	printf("%s() line %d, prev in %s()", vnr->where_fn, vnr->where_line,
6650a705ab6SMarko Zec 	    vnr->prev_fn);
6660a705ab6SMarko Zec 	if (brief)
6670a705ab6SMarko Zec 		printf(", ");
6680a705ab6SMarko Zec 	else
6690a705ab6SMarko Zec 		printf("\n    ");
6700a705ab6SMarko Zec 	printf("%p -> %p\n", vnr->old_vnet, vnr->new_vnet);
6710a705ab6SMarko Zec }
6720a705ab6SMarko Zec 
6730a705ab6SMarko Zec void
6740a705ab6SMarko Zec vnet_log_recursion(struct vnet *old_vnet, const char *old_fn, int line)
6750a705ab6SMarko Zec {
6760a705ab6SMarko Zec 	struct vnet_recursion *vnr;
6770a705ab6SMarko Zec 
6780a705ab6SMarko Zec 	/* Skip already logged recursion events. */
6790a705ab6SMarko Zec 	SLIST_FOREACH(vnr, &vnet_recursions, vnr_le)
6800a705ab6SMarko Zec 		if (vnr->prev_fn == old_fn &&
6810a705ab6SMarko Zec 		    vnr->where_fn == curthread->td_vnet_lpush &&
6820a705ab6SMarko Zec 		    vnr->where_line == line &&
6830a705ab6SMarko Zec 		    (vnr->old_vnet == vnr->new_vnet) == (curvnet == old_vnet))
6840a705ab6SMarko Zec 			return;
6850a705ab6SMarko Zec 
6860a705ab6SMarko Zec 	vnr = malloc(sizeof(*vnr), M_VNET, M_NOWAIT | M_ZERO);
6870a705ab6SMarko Zec 	if (vnr == NULL)
6880a705ab6SMarko Zec 		panic("%s: malloc failed", __func__);
6890a705ab6SMarko Zec 	vnr->prev_fn = old_fn;
6900a705ab6SMarko Zec 	vnr->where_fn = curthread->td_vnet_lpush;
6910a705ab6SMarko Zec 	vnr->where_line = line;
6920a705ab6SMarko Zec 	vnr->old_vnet = old_vnet;
6930a705ab6SMarko Zec 	vnr->new_vnet = curvnet;
6940a705ab6SMarko Zec 
6950a705ab6SMarko Zec 	SLIST_INSERT_HEAD(&vnet_recursions, vnr, vnr_le);
6960a705ab6SMarko Zec 
6970a705ab6SMarko Zec 	vnet_print_recursion(vnr, 0);
6980a705ab6SMarko Zec #ifdef KDB
6990a705ab6SMarko Zec 	kdb_backtrace();
7000a705ab6SMarko Zec #endif
7010a705ab6SMarko Zec }
7020a705ab6SMarko Zec #endif /* VNET_DEBUG */
7030a705ab6SMarko Zec 
70442eedeacSBjoern A. Zeeb /*
70542eedeacSBjoern A. Zeeb  * DDB(4).
70642eedeacSBjoern A. Zeeb  */
707530c0060SRobert Watson #ifdef DDB
7085fa0728bSBjoern A. Zeeb static void
7095fa0728bSBjoern A. Zeeb db_vnet_print(struct vnet *vnet)
7105fa0728bSBjoern A. Zeeb {
7115fa0728bSBjoern A. Zeeb 
7125fa0728bSBjoern A. Zeeb 	db_printf("vnet            = %p\n", vnet);
7135fa0728bSBjoern A. Zeeb 	db_printf(" vnet_magic_n   = %#08x (%s, orig %#08x)\n",
7145fa0728bSBjoern A. Zeeb 	    vnet->vnet_magic_n,
7155fa0728bSBjoern A. Zeeb 	    (vnet->vnet_magic_n == VNET_MAGIC_N) ?
7165fa0728bSBjoern A. Zeeb 		"ok" : "mismatch", VNET_MAGIC_N);
7175fa0728bSBjoern A. Zeeb 	db_printf(" vnet_ifcnt     = %u\n", vnet->vnet_ifcnt);
7185fa0728bSBjoern A. Zeeb 	db_printf(" vnet_sockcnt   = %u\n", vnet->vnet_sockcnt);
7195fa0728bSBjoern A. Zeeb 	db_printf(" vnet_data_mem  = %p\n", vnet->vnet_data_mem);
7205fa0728bSBjoern A. Zeeb 	db_printf(" vnet_data_base = %#jx\n",
7215fa0728bSBjoern A. Zeeb 	    (uintmax_t)vnet->vnet_data_base);
72210108cb6SBjoern A. Zeeb 	db_printf(" vnet_state     = %#08x\n", vnet->vnet_state);
72310108cb6SBjoern A. Zeeb 	db_printf(" vnet_shutdown  = %#03x\n", vnet->vnet_shutdown);
7245fa0728bSBjoern A. Zeeb 	db_printf("\n");
7255fa0728bSBjoern A. Zeeb }
7265fa0728bSBjoern A. Zeeb 
7275fa0728bSBjoern A. Zeeb DB_SHOW_ALL_COMMAND(vnets, db_show_all_vnets)
728530c0060SRobert Watson {
729530c0060SRobert Watson 	VNET_ITERATOR_DECL(vnet_iter);
730530c0060SRobert Watson 
731530c0060SRobert Watson 	VNET_FOREACH(vnet_iter) {
7325fa0728bSBjoern A. Zeeb 		db_vnet_print(vnet_iter);
733281c86a4SBjoern A. Zeeb 		if (db_pager_quit)
734281c86a4SBjoern A. Zeeb 			break;
735530c0060SRobert Watson 	}
736530c0060SRobert Watson }
7370a705ab6SMarko Zec 
7385fa0728bSBjoern A. Zeeb DB_SHOW_COMMAND(vnet, db_show_vnet)
7395fa0728bSBjoern A. Zeeb {
7405fa0728bSBjoern A. Zeeb 
7415fa0728bSBjoern A. Zeeb 	if (!have_addr) {
7425fa0728bSBjoern A. Zeeb 		db_printf("usage: show vnet <struct vnet *>\n");
7435fa0728bSBjoern A. Zeeb 		return;
7445fa0728bSBjoern A. Zeeb 	}
7455fa0728bSBjoern A. Zeeb 
7465fa0728bSBjoern A. Zeeb 	db_vnet_print((struct vnet *)addr);
7475fa0728bSBjoern A. Zeeb }
7485fa0728bSBjoern A. Zeeb 
7493e0490b3SBjoern A. Zeeb static void
7503e0490b3SBjoern A. Zeeb db_show_vnet_print_vs(struct vnet_sysinit *vs, int ddb)
7513e0490b3SBjoern A. Zeeb {
7523e0490b3SBjoern A. Zeeb 	const char *vsname, *funcname;
7533e0490b3SBjoern A. Zeeb 	c_db_sym_t sym;
7543e0490b3SBjoern A. Zeeb 	db_expr_t  offset;
7553e0490b3SBjoern A. Zeeb 
7563e0490b3SBjoern A. Zeeb #define xprint(...)							\
7573e0490b3SBjoern A. Zeeb 	if (ddb)							\
7583e0490b3SBjoern A. Zeeb 		db_printf(__VA_ARGS__);					\
7593e0490b3SBjoern A. Zeeb 	else								\
7603e0490b3SBjoern A. Zeeb 		printf(__VA_ARGS__)
7613e0490b3SBjoern A. Zeeb 
7623e0490b3SBjoern A. Zeeb 	if (vs == NULL) {
7633e0490b3SBjoern A. Zeeb 		xprint("%s: no vnet_sysinit * given\n", __func__);
7643e0490b3SBjoern A. Zeeb 		return;
7653e0490b3SBjoern A. Zeeb 	}
7663e0490b3SBjoern A. Zeeb 
7673e0490b3SBjoern A. Zeeb 	sym = db_search_symbol((vm_offset_t)vs, DB_STGY_ANY, &offset);
7683e0490b3SBjoern A. Zeeb 	db_symbol_values(sym, &vsname, NULL);
7693e0490b3SBjoern A. Zeeb 	sym = db_search_symbol((vm_offset_t)vs->func, DB_STGY_PROC, &offset);
7703e0490b3SBjoern A. Zeeb 	db_symbol_values(sym, &funcname, NULL);
7713e0490b3SBjoern A. Zeeb 	xprint("%s(%p)\n", (vsname != NULL) ? vsname : "", vs);
7725fa0728bSBjoern A. Zeeb 	xprint("  %#08x %#08x\n", vs->subsystem, vs->order);
7733e0490b3SBjoern A. Zeeb 	xprint("  %p(%s)(%p)\n",
7743e0490b3SBjoern A. Zeeb 	    vs->func, (funcname != NULL) ? funcname : "", vs->arg);
7753e0490b3SBjoern A. Zeeb #undef xprint
7763e0490b3SBjoern A. Zeeb }
7773e0490b3SBjoern A. Zeeb 
778c84c5e00SMitchell Horne DB_SHOW_COMMAND_FLAGS(vnet_sysinit, db_show_vnet_sysinit, DB_CMD_MEMSAFE)
7793e0490b3SBjoern A. Zeeb {
7803e0490b3SBjoern A. Zeeb 	struct vnet_sysinit *vs;
7813e0490b3SBjoern A. Zeeb 
7823e0490b3SBjoern A. Zeeb 	db_printf("VNET_SYSINIT vs Name(Ptr)\n");
7833e0490b3SBjoern A. Zeeb 	db_printf("  Subsystem  Order\n");
7843e0490b3SBjoern A. Zeeb 	db_printf("  Function(Name)(Arg)\n");
7853e0490b3SBjoern A. Zeeb 	TAILQ_FOREACH(vs, &vnet_constructors, link) {
7863e0490b3SBjoern A. Zeeb 		db_show_vnet_print_vs(vs, 1);
7873e0490b3SBjoern A. Zeeb 		if (db_pager_quit)
7883e0490b3SBjoern A. Zeeb 			break;
7893e0490b3SBjoern A. Zeeb 	}
7903e0490b3SBjoern A. Zeeb }
7913e0490b3SBjoern A. Zeeb 
792c84c5e00SMitchell Horne DB_SHOW_COMMAND_FLAGS(vnet_sysuninit, db_show_vnet_sysuninit, DB_CMD_MEMSAFE)
7933e0490b3SBjoern A. Zeeb {
7943e0490b3SBjoern A. Zeeb 	struct vnet_sysinit *vs;
7953e0490b3SBjoern A. Zeeb 
7963e0490b3SBjoern A. Zeeb 	db_printf("VNET_SYSUNINIT vs Name(Ptr)\n");
7973e0490b3SBjoern A. Zeeb 	db_printf("  Subsystem  Order\n");
7983e0490b3SBjoern A. Zeeb 	db_printf("  Function(Name)(Arg)\n");
7993e0490b3SBjoern A. Zeeb 	TAILQ_FOREACH_REVERSE(vs, &vnet_destructors, vnet_sysuninit_head,
8003e0490b3SBjoern A. Zeeb 	    link) {
8013e0490b3SBjoern A. Zeeb 		db_show_vnet_print_vs(vs, 1);
8023e0490b3SBjoern A. Zeeb 		if (db_pager_quit)
8033e0490b3SBjoern A. Zeeb 			break;
8043e0490b3SBjoern A. Zeeb 	}
8053e0490b3SBjoern A. Zeeb }
8063e0490b3SBjoern A. Zeeb 
8070a705ab6SMarko Zec #ifdef VNET_DEBUG
808c84c5e00SMitchell Horne DB_SHOW_COMMAND_FLAGS(vnetrcrs, db_show_vnetrcrs, DB_CMD_MEMSAFE)
8090a705ab6SMarko Zec {
8100a705ab6SMarko Zec 	struct vnet_recursion *vnr;
8110a705ab6SMarko Zec 
8120a705ab6SMarko Zec 	SLIST_FOREACH(vnr, &vnet_recursions, vnr_le)
8130a705ab6SMarko Zec 		vnet_print_recursion(vnr, 1);
8140a705ab6SMarko Zec }
815530c0060SRobert Watson #endif
8160a705ab6SMarko Zec #endif /* DDB */
817