xref: /freebsd/sys/net/vnet.c (revision bd7b2f95019e9715150c34736279805de0818d09)
1eddfbb76SRobert Watson /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
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>
39530c0060SRobert Watson #include "opt_ddb.h"
400a705ab6SMarko Zec #include "opt_kdb.h"
41530c0060SRobert Watson 
42eddfbb76SRobert Watson #include <sys/param.h>
430a705ab6SMarko Zec #include <sys/kdb.h>
44eddfbb76SRobert Watson #include <sys/kernel.h>
45530c0060SRobert Watson #include <sys/jail.h>
4661d033d4SBjoern A. Zeeb #include <sys/sdt.h>
47eddfbb76SRobert Watson #include <sys/systm.h>
48eddfbb76SRobert Watson #include <sys/sysctl.h>
4942eedeacSBjoern A. Zeeb #include <sys/eventhandler.h>
50eddfbb76SRobert Watson #include <sys/lock.h>
51eddfbb76SRobert Watson #include <sys/malloc.h>
52eddfbb76SRobert Watson #include <sys/proc.h>
53530c0060SRobert Watson #include <sys/socket.h>
54eddfbb76SRobert Watson #include <sys/sx.h>
55eddfbb76SRobert Watson #include <sys/sysctl.h>
56eddfbb76SRobert Watson 
5742eedeacSBjoern A. Zeeb #include <machine/stdarg.h>
5842eedeacSBjoern A. Zeeb 
59530c0060SRobert Watson #ifdef DDB
60530c0060SRobert Watson #include <ddb/ddb.h>
613e0490b3SBjoern A. Zeeb #include <ddb/db_sym.h>
62530c0060SRobert Watson #endif
63530c0060SRobert Watson 
64530c0060SRobert Watson #include <net/if.h>
65530c0060SRobert Watson #include <net/if_var.h>
66eddfbb76SRobert Watson #include <net/vnet.h>
67eddfbb76SRobert Watson 
68eddfbb76SRobert Watson /*-
69ed3db012SRobert Watson  * This file implements core functions for virtual network stacks:
70ed3db012SRobert Watson  *
71c8f6a138SRobert Watson  * - Virtual network stack management functions.
72530c0060SRobert Watson  *
73c8f6a138SRobert Watson  * - Virtual network stack memory allocator, which virtualizes global
74ed3db012SRobert Watson  *   variables in the network stack
75ed3db012SRobert Watson  *
76ed3db012SRobert Watson  * - Virtualized SYSINIT's/SYSUNINIT's, which allow network stack subsystems
77ed3db012SRobert Watson  *   to register startup/shutdown events to be run for each virtual network
78ed3db012SRobert Watson  *   stack instance.
79530c0060SRobert Watson  */
80530c0060SRobert Watson 
819269189fSBjoern A. Zeeb FEATURE(vimage, "VIMAGE kernel virtualization");
829269189fSBjoern A. Zeeb 
83d745c852SEd Schouten static MALLOC_DEFINE(M_VNET, "vnet", "network stack control block");
84530c0060SRobert Watson 
85530c0060SRobert Watson /*
86530c0060SRobert Watson  * The virtual network stack list has two read-write locks, one sleepable and
87530c0060SRobert Watson  * the other not, so that the list can be stablized and walked in a variety
88530c0060SRobert Watson  * of network stack contexts.  Both must be acquired exclusively to modify
89c8f6a138SRobert Watson  * the list, but a read lock of either lock is sufficient to walk the list.
90530c0060SRobert Watson  */
91530c0060SRobert Watson struct rwlock		vnet_rwlock;
92530c0060SRobert Watson struct sx		vnet_sxlock;
93530c0060SRobert Watson 
94530c0060SRobert Watson #define	VNET_LIST_WLOCK() do {						\
95530c0060SRobert Watson 	sx_xlock(&vnet_sxlock);						\
96530c0060SRobert Watson 	rw_wlock(&vnet_rwlock);						\
97530c0060SRobert Watson } while (0)
98530c0060SRobert Watson 
99530c0060SRobert Watson #define	VNET_LIST_WUNLOCK() do {					\
100530c0060SRobert Watson 	rw_wunlock(&vnet_rwlock);					\
101530c0060SRobert Watson 	sx_xunlock(&vnet_sxlock);					\
102530c0060SRobert Watson } while (0)
103530c0060SRobert Watson 
104530c0060SRobert Watson struct vnet_list_head vnet_head;
105530c0060SRobert Watson struct vnet *vnet0;
106530c0060SRobert Watson 
107530c0060SRobert Watson /*
108ed3db012SRobert Watson  * The virtual network stack allocator provides storage for virtualized
109ed3db012SRobert Watson  * global variables.  These variables are defined/declared using the
110ed3db012SRobert Watson  * VNET_DEFINE()/VNET_DECLARE() macros, which place them in the 'set_vnet'
111ed3db012SRobert Watson  * linker set.  The details of the implementation are somewhat subtle, but
112ed3db012SRobert Watson  * allow the majority of most network subsystems to maintain
113eddfbb76SRobert Watson  * virtualization-agnostic.
114eddfbb76SRobert Watson  *
115eddfbb76SRobert Watson  * The virtual network stack allocator handles variables in the base kernel
116eddfbb76SRobert Watson  * vs. modules in similar but different ways.  In both cases, virtualized
117eddfbb76SRobert Watson  * global variables are marked as such by being declared to be part of the
118eddfbb76SRobert Watson  * vnet linker set.  These "master" copies of global variables serve two
119eddfbb76SRobert Watson  * functions:
120eddfbb76SRobert Watson  *
121eddfbb76SRobert Watson  * (1) They contain static initialization or "default" values for global
122eddfbb76SRobert Watson  *     variables which will be propagated to each virtual network stack
123eddfbb76SRobert Watson  *     instance when created.  As with normal global variables, they default
124eddfbb76SRobert Watson  *     to zero-filled.
125eddfbb76SRobert Watson  *
126eddfbb76SRobert Watson  * (2) They act as unique global names by which the variable can be referred
127eddfbb76SRobert Watson  *     to, regardless of network stack instance.  The single global symbol
128eddfbb76SRobert Watson  *     will be used to calculate the location of a per-virtual instance
129eddfbb76SRobert Watson  *     variable at run-time.
130eddfbb76SRobert Watson  *
131eddfbb76SRobert Watson  * Each virtual network stack instance has a complete copy of each
132eddfbb76SRobert Watson  * virtualized global variable, stored in a malloc'd block of memory
133eddfbb76SRobert Watson  * referred to by vnet->vnet_data_mem.  Critical to the design is that each
134eddfbb76SRobert Watson  * per-instance memory block is laid out identically to the master block so
135eddfbb76SRobert Watson  * that the offset of each global variable is the same across all blocks.  To
136eddfbb76SRobert Watson  * optimize run-time access, a precalculated 'base' address,
137eddfbb76SRobert Watson  * vnet->vnet_data_base, is stored in each vnet, and is the amount that can
138eddfbb76SRobert Watson  * be added to the address of a 'master' instance of a variable to get to the
139eddfbb76SRobert Watson  * per-vnet instance.
140eddfbb76SRobert Watson  *
141eddfbb76SRobert Watson  * Virtualized global variables are handled in a similar manner, but as each
142eddfbb76SRobert Watson  * module has its own 'set_vnet' linker set, and we want to keep all
143eddfbb76SRobert Watson  * virtualized globals togther, we reserve space in the kernel's linker set
144eddfbb76SRobert Watson  * for potential module variables using a per-vnet character array,
145eddfbb76SRobert Watson  * 'modspace'.  The virtual network stack allocator maintains a free list to
146eddfbb76SRobert Watson  * track what space in the array is free (all, initially) and as modules are
147eddfbb76SRobert Watson  * linked, allocates portions of the space to specific globals.  The kernel
148eddfbb76SRobert Watson  * module linker queries the virtual network stack allocator and will
149eddfbb76SRobert Watson  * bind references of the global to the location during linking.  It also
150eddfbb76SRobert Watson  * calls into the virtual network stack allocator, once the memory is
151eddfbb76SRobert Watson  * initialized, in order to propagate the new static initializations to all
152eddfbb76SRobert Watson  * existing virtual network stack instances so that the soon-to-be executing
153eddfbb76SRobert Watson  * module will find every network stack instance with proper default values.
154eddfbb76SRobert Watson  */
155eddfbb76SRobert Watson 
156eddfbb76SRobert Watson /*
157eddfbb76SRobert Watson  * Number of bytes of data in the 'set_vnet' linker set, and hence the total
158eddfbb76SRobert Watson  * size of all kernel virtualized global variables, and the malloc(9) type
159eddfbb76SRobert Watson  * that will be used to allocate it.
160eddfbb76SRobert Watson  */
161eddfbb76SRobert Watson #define	VNET_BYTES	(VNET_STOP - VNET_START)
162eddfbb76SRobert Watson 
163d745c852SEd Schouten static MALLOC_DEFINE(M_VNET_DATA, "vnet_data", "VNET data");
164eddfbb76SRobert Watson 
165eddfbb76SRobert Watson /*
166eddfbb76SRobert Watson  * VNET_MODMIN is the minimum number of bytes we will reserve for the sum of
167eddfbb76SRobert Watson  * global variables across all loaded modules.  As this actually sizes an
168eddfbb76SRobert Watson  * array declared as a virtualized global variable in the kernel itself, and
169eddfbb76SRobert Watson  * we want the virtualized global variable space to be page-sized, we may
170eddfbb76SRobert Watson  * have more space than that in practice.
171eddfbb76SRobert Watson  */
172c955c6cdSBjoern A. Zeeb #define	VNET_MODMIN	(8 * PAGE_SIZE)
173eddfbb76SRobert Watson #define	VNET_SIZE	roundup2(VNET_BYTES, PAGE_SIZE)
174eddfbb76SRobert Watson 
175eddfbb76SRobert Watson /*
176eddfbb76SRobert Watson  * Space to store virtualized global variables from loadable kernel modules,
177eddfbb76SRobert Watson  * and the free list to manage it.
178eddfbb76SRobert Watson  */
179cd2106eaSAndrew Turner VNET_DEFINE_STATIC(char, modspace[VNET_MODMIN] __aligned(__alignof(void *)));
180eddfbb76SRobert Watson 
181d0728d71SRobert Watson /*
182cf7974fdSZhenlei Huang  * A copy of the initial values of all virtualized global variables.
183cf7974fdSZhenlei Huang  */
184cf7974fdSZhenlei Huang static uintptr_t vnet_init_var;
185cf7974fdSZhenlei Huang 
186cf7974fdSZhenlei Huang /*
187c8f6a138SRobert Watson  * Global lists of subsystem constructor and destructors for vnets.  They are
188a99fcfd4SMarko Zec  * registered via VNET_SYSINIT() and VNET_SYSUNINIT().  Both lists are
189a99fcfd4SMarko Zec  * protected by the vnet_sysinit_sxlock global lock.
190d0728d71SRobert Watson  */
191d0728d71SRobert Watson static TAILQ_HEAD(vnet_sysinit_head, vnet_sysinit) vnet_constructors =
192d0728d71SRobert Watson 	TAILQ_HEAD_INITIALIZER(vnet_constructors);
193d0728d71SRobert Watson static TAILQ_HEAD(vnet_sysuninit_head, vnet_sysinit) vnet_destructors =
194d0728d71SRobert Watson 	TAILQ_HEAD_INITIALIZER(vnet_destructors);
195d0728d71SRobert Watson 
196a99fcfd4SMarko Zec struct sx		vnet_sysinit_sxlock;
197a99fcfd4SMarko Zec 
198a99fcfd4SMarko Zec #define	VNET_SYSINIT_WLOCK()	sx_xlock(&vnet_sysinit_sxlock);
199a99fcfd4SMarko Zec #define	VNET_SYSINIT_WUNLOCK()	sx_xunlock(&vnet_sysinit_sxlock);
200a99fcfd4SMarko Zec #define	VNET_SYSINIT_RLOCK()	sx_slock(&vnet_sysinit_sxlock);
201a99fcfd4SMarko Zec #define	VNET_SYSINIT_RUNLOCK()	sx_sunlock(&vnet_sysinit_sxlock);
202a99fcfd4SMarko Zec 
203eddfbb76SRobert Watson struct vnet_data_free {
204eddfbb76SRobert Watson 	uintptr_t	vnd_start;
205eddfbb76SRobert Watson 	int		vnd_len;
206eddfbb76SRobert Watson 	TAILQ_ENTRY(vnet_data_free) vnd_link;
207eddfbb76SRobert Watson };
208eddfbb76SRobert Watson 
209d745c852SEd Schouten static MALLOC_DEFINE(M_VNET_DATA_FREE, "vnet_data_free",
210d745c852SEd Schouten     "VNET resource accounting");
211eddfbb76SRobert Watson static TAILQ_HEAD(, vnet_data_free) vnet_data_free_head =
212eddfbb76SRobert Watson 	    TAILQ_HEAD_INITIALIZER(vnet_data_free_head);
213eddfbb76SRobert Watson static struct sx vnet_data_free_lock;
214eddfbb76SRobert Watson 
21561d033d4SBjoern A. Zeeb SDT_PROVIDER_DEFINE(vnet);
216d9fae5abSAndriy Gapon SDT_PROBE_DEFINE1(vnet, functions, vnet_alloc, entry, "int");
217d9fae5abSAndriy Gapon SDT_PROBE_DEFINE2(vnet, functions, vnet_alloc, alloc, "int",
21879856499SRui Paulo     "struct vnet *");
219d9fae5abSAndriy Gapon SDT_PROBE_DEFINE2(vnet, functions, vnet_alloc, return,
22079856499SRui Paulo     "int", "struct vnet *");
221d9fae5abSAndriy Gapon SDT_PROBE_DEFINE2(vnet, functions, vnet_destroy, entry,
22279856499SRui Paulo     "int", "struct vnet *");
2234678c740SGleb Smirnoff SDT_PROBE_DEFINE1(vnet, functions, vnet_destroy, return,
22479856499SRui Paulo     "int");
22561d033d4SBjoern A. Zeeb 
226fb9b76e0SZhenlei Huang /*
227fb9b76e0SZhenlei Huang  * Run per-vnet sysinits or sysuninits during vnet creation/destruction.
228fb9b76e0SZhenlei Huang  */
229fb9b76e0SZhenlei Huang static void vnet_sysinit(void);
230fb9b76e0SZhenlei Huang static void vnet_sysuninit(void);
231fb9b76e0SZhenlei Huang 
2323e0490b3SBjoern A. Zeeb #ifdef DDB
2333e0490b3SBjoern A. Zeeb static void db_show_vnet_print_vs(struct vnet_sysinit *, int);
2343e0490b3SBjoern A. Zeeb #endif
2353e0490b3SBjoern A. Zeeb 
236eddfbb76SRobert Watson /*
237530c0060SRobert Watson  * Allocate a virtual network stack.
238530c0060SRobert Watson  */
239530c0060SRobert Watson struct vnet *
vnet_alloc(void)240530c0060SRobert Watson vnet_alloc(void)
241530c0060SRobert Watson {
242530c0060SRobert Watson 	struct vnet *vnet;
243530c0060SRobert Watson 
24461d033d4SBjoern A. Zeeb 	SDT_PROBE1(vnet, functions, vnet_alloc, entry, __LINE__);
245530c0060SRobert Watson 	vnet = malloc(sizeof(struct vnet), M_VNET, M_WAITOK | M_ZERO);
246530c0060SRobert Watson 	vnet->vnet_magic_n = VNET_MAGIC_N;
24761d033d4SBjoern A. Zeeb 	SDT_PROBE2(vnet, functions, vnet_alloc, alloc, __LINE__, vnet);
2486bc2c7b7SRobert Watson 
2496bc2c7b7SRobert Watson 	/*
2506bc2c7b7SRobert Watson 	 * Allocate storage for virtualized global variables and copy in
251a7acce34SZhenlei Huang 	 * initial values from our 'master' copy.
2526bc2c7b7SRobert Watson 	 */
2536bc2c7b7SRobert Watson 	vnet->vnet_data_mem = malloc(VNET_SIZE, M_VNET_DATA, M_WAITOK);
2546bc2c7b7SRobert Watson 	memcpy(vnet->vnet_data_mem, (void *)VNET_START, VNET_BYTES);
2556bc2c7b7SRobert Watson 
2566bc2c7b7SRobert Watson 	/*
2576bc2c7b7SRobert Watson 	 * All use of vnet-specific data will immediately subtract VNET_START
2586bc2c7b7SRobert Watson 	 * from the base memory pointer, so pre-calculate that now to avoid
2596bc2c7b7SRobert Watson 	 * it on each use.
2606bc2c7b7SRobert Watson 	 */
2616bc2c7b7SRobert Watson 	vnet->vnet_data_base = (uintptr_t)vnet->vnet_data_mem - VNET_START;
262530c0060SRobert Watson 
263530c0060SRobert Watson 	/* Initialize / attach vnet module instances. */
264530c0060SRobert Watson 	CURVNET_SET_QUIET(vnet);
265530c0060SRobert Watson 	vnet_sysinit();
266530c0060SRobert Watson 	CURVNET_RESTORE();
267530c0060SRobert Watson 
268a99fcfd4SMarko Zec 	VNET_LIST_WLOCK();
269530c0060SRobert Watson 	LIST_INSERT_HEAD(&vnet_head, vnet, vnet_le);
270530c0060SRobert Watson 	VNET_LIST_WUNLOCK();
271530c0060SRobert Watson 
27261d033d4SBjoern A. Zeeb 	SDT_PROBE2(vnet, functions, vnet_alloc, return, __LINE__, vnet);
273530c0060SRobert Watson 	return (vnet);
274530c0060SRobert Watson }
275530c0060SRobert Watson 
276530c0060SRobert Watson /*
277530c0060SRobert Watson  * Destroy a virtual network stack.
278530c0060SRobert Watson  */
279530c0060SRobert Watson void
vnet_destroy(struct vnet * vnet)280530c0060SRobert Watson vnet_destroy(struct vnet *vnet)
281530c0060SRobert Watson {
282530c0060SRobert Watson 
28361d033d4SBjoern A. Zeeb 	SDT_PROBE2(vnet, functions, vnet_destroy, entry, __LINE__, vnet);
284530c0060SRobert Watson 	KASSERT(vnet->vnet_sockcnt == 0,
285530c0060SRobert Watson 	    ("%s: vnet still has sockets", __func__));
286530c0060SRobert Watson 
287530c0060SRobert Watson 	VNET_LIST_WLOCK();
288530c0060SRobert Watson 	LIST_REMOVE(vnet, vnet_le);
289a99fcfd4SMarko Zec 	VNET_LIST_WUNLOCK();
290530c0060SRobert Watson 
29110108cb6SBjoern A. Zeeb 	/* Signal that VNET is being shutdown. */
29210108cb6SBjoern A. Zeeb 	vnet->vnet_shutdown = true;
29310108cb6SBjoern A. Zeeb 
294530c0060SRobert Watson 	CURVNET_SET_QUIET(vnet);
2956d2a10d9SKristof Provost 	sx_xlock(&ifnet_detach_sxlock);
296530c0060SRobert Watson 	vnet_sysuninit();
2976d2a10d9SKristof Provost 	sx_xunlock(&ifnet_detach_sxlock);
298530c0060SRobert Watson 	CURVNET_RESTORE();
299530c0060SRobert Watson 
3006bc2c7b7SRobert Watson 	/*
3016bc2c7b7SRobert Watson 	 * Release storage for the virtual network stack instance.
3026bc2c7b7SRobert Watson 	 */
3036bc2c7b7SRobert Watson 	free(vnet->vnet_data_mem, M_VNET_DATA);
3046bc2c7b7SRobert Watson 	vnet->vnet_data_mem = NULL;
3056bc2c7b7SRobert Watson 	vnet->vnet_data_base = 0;
306530c0060SRobert Watson 	vnet->vnet_magic_n = 0xdeadbeef;
307530c0060SRobert Watson 	free(vnet, M_VNET);
30861d033d4SBjoern A. Zeeb 	SDT_PROBE1(vnet, functions, vnet_destroy, return, __LINE__);
309530c0060SRobert Watson }
310530c0060SRobert Watson 
311530c0060SRobert Watson /*
312530c0060SRobert Watson  * Boot time initialization and allocation of virtual network stacks.
313530c0060SRobert Watson  */
314530c0060SRobert Watson static void
vnet_init_prelink(void * arg __unused)31554d9f34eSBjoern A. Zeeb vnet_init_prelink(void *arg __unused)
316530c0060SRobert Watson {
317530c0060SRobert Watson 
318530c0060SRobert Watson 	rw_init(&vnet_rwlock, "vnet_rwlock");
319530c0060SRobert Watson 	sx_init(&vnet_sxlock, "vnet_sxlock");
320a99fcfd4SMarko Zec 	sx_init(&vnet_sysinit_sxlock, "vnet_sysinit_sxlock");
321530c0060SRobert Watson 	LIST_INIT(&vnet_head);
322530c0060SRobert Watson }
323530c0060SRobert Watson SYSINIT(vnet_init_prelink, SI_SUB_VNET_PRELINK, SI_ORDER_FIRST,
324530c0060SRobert Watson     vnet_init_prelink, NULL);
325530c0060SRobert Watson 
326530c0060SRobert Watson static void
vnet0_init(void * arg __unused)32754d9f34eSBjoern A. Zeeb vnet0_init(void *arg __unused)
328530c0060SRobert Watson {
329530c0060SRobert Watson 
3308e94025bSBjoern A. Zeeb 	if (bootverbose)
3318e94025bSBjoern A. Zeeb 		printf("VIMAGE (virtualized network stack) enabled\n");
3326aad5c1cSRobert Watson 
333530c0060SRobert Watson 	/*
334530c0060SRobert Watson 	 * We MUST clear curvnet in vi_init_done() before going SMP,
335530c0060SRobert Watson 	 * otherwise CURVNET_SET() macros would scream about unnecessary
336530c0060SRobert Watson 	 * curvnet recursions.
337530c0060SRobert Watson 	 */
338530c0060SRobert Watson 	curvnet = prison0.pr_vnet = vnet0 = vnet_alloc();
339530c0060SRobert Watson }
340530c0060SRobert Watson SYSINIT(vnet0_init, SI_SUB_VNET, SI_ORDER_FIRST, vnet0_init, NULL);
341530c0060SRobert Watson 
342530c0060SRobert Watson static void
vnet_init_done(void * unused __unused)34354d9f34eSBjoern A. Zeeb vnet_init_done(void *unused __unused)
344530c0060SRobert Watson {
345530c0060SRobert Watson 
346530c0060SRobert Watson 	curvnet = NULL;
347530c0060SRobert Watson }
34889856f7eSBjoern A. Zeeb SYSINIT(vnet_init_done, SI_SUB_VNET_DONE, SI_ORDER_ANY, vnet_init_done,
349530c0060SRobert Watson     NULL);
350530c0060SRobert Watson 
351530c0060SRobert Watson /*
352eddfbb76SRobert Watson  * Once on boot, initialize the modspace freelist to entirely cover modspace.
353eddfbb76SRobert Watson  */
354eddfbb76SRobert Watson static void
vnet_data_startup(void * dummy __unused)355eddfbb76SRobert Watson vnet_data_startup(void *dummy __unused)
356eddfbb76SRobert Watson {
357eddfbb76SRobert Watson 	struct vnet_data_free *df;
358eddfbb76SRobert Watson 
359eddfbb76SRobert Watson 	df = malloc(sizeof(*df), M_VNET_DATA_FREE, M_WAITOK | M_ZERO);
360eddfbb76SRobert Watson 	df->vnd_start = (uintptr_t)&VNET_NAME(modspace);
361793f71bfSBjoern A. Zeeb 	df->vnd_len = VNET_MODMIN;
362eddfbb76SRobert Watson 	TAILQ_INSERT_HEAD(&vnet_data_free_head, df, vnd_link);
363eddfbb76SRobert Watson 	sx_init(&vnet_data_free_lock, "vnet_data alloc lock");
364cf7974fdSZhenlei Huang 	vnet_init_var = (uintptr_t)malloc(VNET_BYTES, M_VNET_DATA, M_WAITOK);
365eddfbb76SRobert Watson }
366891cf3edSEd Maste SYSINIT(vnet_data, SI_SUB_KLD, SI_ORDER_FIRST, vnet_data_startup, NULL);
367eddfbb76SRobert Watson 
36810108cb6SBjoern A. Zeeb /* Dummy VNET_SYSINIT to make sure we always reach the final end state. */
36900e36a5cSBjoern A. Zeeb static void
vnet_sysinit_done(void * unused __unused)37010108cb6SBjoern A. Zeeb vnet_sysinit_done(void *unused __unused)
37100e36a5cSBjoern A. Zeeb {
37200e36a5cSBjoern A. Zeeb 
37310108cb6SBjoern A. Zeeb 	return;
37400e36a5cSBjoern A. Zeeb }
37510108cb6SBjoern A. Zeeb VNET_SYSINIT(vnet_sysinit_done, SI_SUB_VNET_DONE, SI_ORDER_ANY,
37610108cb6SBjoern A. Zeeb     vnet_sysinit_done, NULL);
37700e36a5cSBjoern A. Zeeb 
378eddfbb76SRobert Watson /*
379eddfbb76SRobert Watson  * When a module is loaded and requires storage for a virtualized global
380eddfbb76SRobert Watson  * variable, allocate space from the modspace free list.  This interface
381eddfbb76SRobert Watson  * should be used only by the kernel linker.
382eddfbb76SRobert Watson  */
383eddfbb76SRobert Watson void *
vnet_data_alloc(int size)384eddfbb76SRobert Watson vnet_data_alloc(int size)
385eddfbb76SRobert Watson {
386eddfbb76SRobert Watson 	struct vnet_data_free *df;
387eddfbb76SRobert Watson 	void *s;
388eddfbb76SRobert Watson 
389eddfbb76SRobert Watson 	s = NULL;
390eddfbb76SRobert Watson 	size = roundup2(size, sizeof(void *));
391eddfbb76SRobert Watson 	sx_xlock(&vnet_data_free_lock);
392eddfbb76SRobert Watson 	TAILQ_FOREACH(df, &vnet_data_free_head, vnd_link) {
393eddfbb76SRobert Watson 		if (df->vnd_len < size)
394eddfbb76SRobert Watson 			continue;
395eddfbb76SRobert Watson 		if (df->vnd_len == size) {
396eddfbb76SRobert Watson 			s = (void *)df->vnd_start;
397eddfbb76SRobert Watson 			TAILQ_REMOVE(&vnet_data_free_head, df, vnd_link);
398eddfbb76SRobert Watson 			free(df, M_VNET_DATA_FREE);
399eddfbb76SRobert Watson 			break;
400eddfbb76SRobert Watson 		}
401eddfbb76SRobert Watson 		s = (void *)df->vnd_start;
402eddfbb76SRobert Watson 		df->vnd_len -= size;
403eddfbb76SRobert Watson 		df->vnd_start = df->vnd_start + size;
404eddfbb76SRobert Watson 		break;
405eddfbb76SRobert Watson 	}
406eddfbb76SRobert Watson 	sx_xunlock(&vnet_data_free_lock);
407eddfbb76SRobert Watson 
408eddfbb76SRobert Watson 	return (s);
409eddfbb76SRobert Watson }
410eddfbb76SRobert Watson 
411eddfbb76SRobert Watson /*
412eddfbb76SRobert Watson  * Free space for a virtualized global variable on module unload.
413eddfbb76SRobert Watson  */
414eddfbb76SRobert Watson void
vnet_data_free(void * start_arg,int size)415eddfbb76SRobert Watson vnet_data_free(void *start_arg, int size)
416eddfbb76SRobert Watson {
417eddfbb76SRobert Watson 	struct vnet_data_free *df;
418eddfbb76SRobert Watson 	struct vnet_data_free *dn;
419eddfbb76SRobert Watson 	uintptr_t start;
420eddfbb76SRobert Watson 	uintptr_t end;
421eddfbb76SRobert Watson 
422eddfbb76SRobert Watson 	size = roundup2(size, sizeof(void *));
423eddfbb76SRobert Watson 	start = (uintptr_t)start_arg;
424eddfbb76SRobert Watson 	end = start + size;
425eddfbb76SRobert Watson 	/*
426eddfbb76SRobert Watson 	 * Free a region of space and merge it with as many neighbors as
427eddfbb76SRobert Watson 	 * possible.  Keeping the list sorted simplifies this operation.
428eddfbb76SRobert Watson 	 */
429eddfbb76SRobert Watson 	sx_xlock(&vnet_data_free_lock);
430eddfbb76SRobert Watson 	TAILQ_FOREACH(df, &vnet_data_free_head, vnd_link) {
431eddfbb76SRobert Watson 		if (df->vnd_start > end)
432eddfbb76SRobert Watson 			break;
433eddfbb76SRobert Watson 		/*
434c8f6a138SRobert Watson 		 * If we expand at the end of an entry we may have to merge
435c8f6a138SRobert Watson 		 * it with the one following it as well.
436eddfbb76SRobert Watson 		 */
437eddfbb76SRobert Watson 		if (df->vnd_start + df->vnd_len == start) {
438eddfbb76SRobert Watson 			df->vnd_len += size;
439eddfbb76SRobert Watson 			dn = TAILQ_NEXT(df, vnd_link);
440eddfbb76SRobert Watson 			if (df->vnd_start + df->vnd_len == dn->vnd_start) {
441eddfbb76SRobert Watson 				df->vnd_len += dn->vnd_len;
442c8f6a138SRobert Watson 				TAILQ_REMOVE(&vnet_data_free_head, dn,
443c8f6a138SRobert Watson 				    vnd_link);
444eddfbb76SRobert Watson 				free(dn, M_VNET_DATA_FREE);
445eddfbb76SRobert Watson 			}
446eddfbb76SRobert Watson 			sx_xunlock(&vnet_data_free_lock);
447eddfbb76SRobert Watson 			return;
448eddfbb76SRobert Watson 		}
449eddfbb76SRobert Watson 		if (df->vnd_start == end) {
450eddfbb76SRobert Watson 			df->vnd_start = start;
451eddfbb76SRobert Watson 			df->vnd_len += size;
452eddfbb76SRobert Watson 			sx_xunlock(&vnet_data_free_lock);
453eddfbb76SRobert Watson 			return;
454eddfbb76SRobert Watson 		}
455eddfbb76SRobert Watson 	}
456eddfbb76SRobert Watson 	dn = malloc(sizeof(*df), M_VNET_DATA_FREE, M_WAITOK | M_ZERO);
457eddfbb76SRobert Watson 	dn->vnd_start = start;
458eddfbb76SRobert Watson 	dn->vnd_len = size;
459eddfbb76SRobert Watson 	if (df)
460eddfbb76SRobert Watson 		TAILQ_INSERT_BEFORE(df, dn, vnd_link);
461eddfbb76SRobert Watson 	else
462eddfbb76SRobert Watson 		TAILQ_INSERT_TAIL(&vnet_data_free_head, dn, vnd_link);
463eddfbb76SRobert Watson 	sx_xunlock(&vnet_data_free_lock);
464eddfbb76SRobert Watson }
465eddfbb76SRobert Watson 
466eddfbb76SRobert Watson /*
467eddfbb76SRobert Watson  * When a new virtualized global variable has been allocated, propagate its
468eddfbb76SRobert Watson  * initial value to each already-allocated virtual network stack instance.
469eddfbb76SRobert Watson  */
470eddfbb76SRobert Watson void
vnet_data_copy(void * start,int size)471eddfbb76SRobert Watson vnet_data_copy(void *start, int size)
472eddfbb76SRobert Watson {
4737429a3f3SRobert Watson 	struct vnet *vnet;
474eddfbb76SRobert Watson 
4757429a3f3SRobert Watson 	VNET_LIST_RLOCK();
4767429a3f3SRobert Watson 	LIST_FOREACH(vnet, &vnet_head, vnet_le)
4777429a3f3SRobert Watson 		memcpy((void *)((uintptr_t)vnet->vnet_data_base +
4787429a3f3SRobert Watson 		    (uintptr_t)start), start, size);
4797429a3f3SRobert Watson 	VNET_LIST_RUNLOCK();
480eddfbb76SRobert Watson }
481eddfbb76SRobert Watson 
482eddfbb76SRobert Watson /*
483cf7974fdSZhenlei Huang  * Save a copy of the initial values of virtualized global variables.
484cf7974fdSZhenlei Huang  */
485cf7974fdSZhenlei Huang void
vnet_save_init(void * start,size_t size)486cf7974fdSZhenlei Huang vnet_save_init(void *start, size_t size)
487cf7974fdSZhenlei Huang {
488cf7974fdSZhenlei Huang 	MPASS(vnet_init_var != 0);
489cf7974fdSZhenlei Huang 	MPASS(VNET_START <= (uintptr_t)start &&
490cf7974fdSZhenlei Huang 	    (uintptr_t)start + size <= VNET_STOP);
491cf7974fdSZhenlei Huang 	memcpy((void *)(vnet_init_var + ((uintptr_t)start - VNET_START)),
492cf7974fdSZhenlei Huang 	    start, size);
493cf7974fdSZhenlei Huang }
494cf7974fdSZhenlei Huang 
495cf7974fdSZhenlei Huang /*
496cf7974fdSZhenlei Huang  * Restore the 'master' copies of virtualized global variables to theirs
497cf7974fdSZhenlei Huang  * initial values.
498cf7974fdSZhenlei Huang  */
499cf7974fdSZhenlei Huang void
vnet_restore_init(void * start,size_t size)500cf7974fdSZhenlei Huang vnet_restore_init(void *start, size_t size)
501cf7974fdSZhenlei Huang {
502cf7974fdSZhenlei Huang 	MPASS(vnet_init_var != 0);
503cf7974fdSZhenlei Huang 	MPASS(VNET_START <= (uintptr_t)start &&
504cf7974fdSZhenlei Huang 	    (uintptr_t)start + size <= VNET_STOP);
505cf7974fdSZhenlei Huang 	memcpy(start,
506cf7974fdSZhenlei Huang 	    (void *)(vnet_init_var + ((uintptr_t)start - VNET_START)), size);
507cf7974fdSZhenlei Huang }
508cf7974fdSZhenlei Huang 
509cf7974fdSZhenlei Huang /*
510d0728d71SRobert Watson  * Support for special SYSINIT handlers registered via VNET_SYSINIT()
511d0728d71SRobert Watson  * and VNET_SYSUNINIT().
512d0728d71SRobert Watson  */
513d0728d71SRobert Watson void
vnet_register_sysinit(void * arg)514d0728d71SRobert Watson vnet_register_sysinit(void *arg)
515d0728d71SRobert Watson {
516d0728d71SRobert Watson 	struct vnet_sysinit *vs, *vs2;
517d0728d71SRobert Watson 	struct vnet *vnet;
518d0728d71SRobert Watson 
519d0728d71SRobert Watson 	vs = arg;
520d0728d71SRobert Watson 	KASSERT(vs->subsystem > SI_SUB_VNET, ("vnet sysinit too early"));
521d0728d71SRobert Watson 
522d0728d71SRobert Watson 	/* Add the constructor to the global list of vnet constructors. */
523a99fcfd4SMarko Zec 	VNET_SYSINIT_WLOCK();
524d0728d71SRobert Watson 	TAILQ_FOREACH(vs2, &vnet_constructors, link) {
525d0728d71SRobert Watson 		if (vs2->subsystem > vs->subsystem)
526d0728d71SRobert Watson 			break;
527d0728d71SRobert Watson 		if (vs2->subsystem == vs->subsystem && vs2->order > vs->order)
528d0728d71SRobert Watson 			break;
529d0728d71SRobert Watson 	}
530d0728d71SRobert Watson 	if (vs2 != NULL)
531d0728d71SRobert Watson 		TAILQ_INSERT_BEFORE(vs2, vs, link);
532d0728d71SRobert Watson 	else
533d0728d71SRobert Watson 		TAILQ_INSERT_TAIL(&vnet_constructors, vs, link);
534d0728d71SRobert Watson 
535d0728d71SRobert Watson 	/*
536d0728d71SRobert Watson 	 * Invoke the constructor on all the existing vnets when it is
537d0728d71SRobert Watson 	 * registered.
538d0728d71SRobert Watson 	 */
539*bd7b2f95SKristof Provost 	VNET_LIST_RLOCK();
540d0728d71SRobert Watson 	VNET_FOREACH(vnet) {
541d0728d71SRobert Watson 		CURVNET_SET_QUIET(vnet);
542d0728d71SRobert Watson 		vs->func(vs->arg);
543d0728d71SRobert Watson 		CURVNET_RESTORE();
544d0728d71SRobert Watson 	}
545*bd7b2f95SKristof Provost 	VNET_LIST_RUNLOCK();
546a99fcfd4SMarko Zec 	VNET_SYSINIT_WUNLOCK();
547d0728d71SRobert Watson }
548d0728d71SRobert Watson 
549d0728d71SRobert Watson void
vnet_deregister_sysinit(void * arg)550d0728d71SRobert Watson vnet_deregister_sysinit(void *arg)
551d0728d71SRobert Watson {
552d0728d71SRobert Watson 	struct vnet_sysinit *vs;
553d0728d71SRobert Watson 
554d0728d71SRobert Watson 	vs = arg;
555d0728d71SRobert Watson 
556d0728d71SRobert Watson 	/* Remove the constructor from the global list of vnet constructors. */
557a99fcfd4SMarko Zec 	VNET_SYSINIT_WLOCK();
558d0728d71SRobert Watson 	TAILQ_REMOVE(&vnet_constructors, vs, link);
559a99fcfd4SMarko Zec 	VNET_SYSINIT_WUNLOCK();
560d0728d71SRobert Watson }
561d0728d71SRobert Watson 
562d0728d71SRobert Watson void
vnet_register_sysuninit(void * arg)563d0728d71SRobert Watson vnet_register_sysuninit(void *arg)
564d0728d71SRobert Watson {
565d0728d71SRobert Watson 	struct vnet_sysinit *vs, *vs2;
566d0728d71SRobert Watson 
567d0728d71SRobert Watson 	vs = arg;
568d0728d71SRobert Watson 
569d0728d71SRobert Watson 	/* Add the destructor to the global list of vnet destructors. */
570a99fcfd4SMarko Zec 	VNET_SYSINIT_WLOCK();
571d0728d71SRobert Watson 	TAILQ_FOREACH(vs2, &vnet_destructors, link) {
572d0728d71SRobert Watson 		if (vs2->subsystem > vs->subsystem)
573d0728d71SRobert Watson 			break;
574d0728d71SRobert Watson 		if (vs2->subsystem == vs->subsystem && vs2->order > vs->order)
575d0728d71SRobert Watson 			break;
576d0728d71SRobert Watson 	}
577d0728d71SRobert Watson 	if (vs2 != NULL)
578d0728d71SRobert Watson 		TAILQ_INSERT_BEFORE(vs2, vs, link);
579d0728d71SRobert Watson 	else
580d0728d71SRobert Watson 		TAILQ_INSERT_TAIL(&vnet_destructors, vs, link);
581a99fcfd4SMarko Zec 	VNET_SYSINIT_WUNLOCK();
582d0728d71SRobert Watson }
583d0728d71SRobert Watson 
584d0728d71SRobert Watson void
vnet_deregister_sysuninit(void * arg)585d0728d71SRobert Watson vnet_deregister_sysuninit(void *arg)
586d0728d71SRobert Watson {
587d0728d71SRobert Watson 	struct vnet_sysinit *vs;
588d0728d71SRobert Watson 	struct vnet *vnet;
589d0728d71SRobert Watson 
590d0728d71SRobert Watson 	vs = arg;
591d0728d71SRobert Watson 
592d0728d71SRobert Watson 	/*
593d0728d71SRobert Watson 	 * Invoke the destructor on all the existing vnets when it is
594d0728d71SRobert Watson 	 * deregistered.
595d0728d71SRobert Watson 	 */
596a99fcfd4SMarko Zec 	VNET_SYSINIT_WLOCK();
597*bd7b2f95SKristof Provost 	VNET_LIST_RLOCK();
598d0728d71SRobert Watson 	VNET_FOREACH(vnet) {
599d0728d71SRobert Watson 		CURVNET_SET_QUIET(vnet);
600d0728d71SRobert Watson 		vs->func(vs->arg);
601d0728d71SRobert Watson 		CURVNET_RESTORE();
602d0728d71SRobert Watson 	}
603d0728d71SRobert Watson 
604d0728d71SRobert Watson 	/* Remove the destructor from the global list of vnet destructors. */
605d0728d71SRobert Watson 	TAILQ_REMOVE(&vnet_destructors, vs, link);
606a99fcfd4SMarko Zec 	VNET_SYSINIT_WUNLOCK();
607*bd7b2f95SKristof Provost 	VNET_LIST_RUNLOCK();
608d0728d71SRobert Watson }
609d0728d71SRobert Watson 
610d0728d71SRobert Watson /*
611c8f6a138SRobert Watson  * Invoke all registered vnet constructors on the current vnet.  Used during
612c8f6a138SRobert Watson  * vnet construction.  The caller is responsible for ensuring the new vnet is
613a99fcfd4SMarko Zec  * the current vnet and that the vnet_sysinit_sxlock lock is locked.
614d0728d71SRobert Watson  */
615fb9b76e0SZhenlei Huang static void
vnet_sysinit(void)616d0728d71SRobert Watson vnet_sysinit(void)
617d0728d71SRobert Watson {
618d0728d71SRobert Watson 	struct vnet_sysinit *vs;
619d0728d71SRobert Watson 
620a99fcfd4SMarko Zec 	VNET_SYSINIT_RLOCK();
62110108cb6SBjoern A. Zeeb 	TAILQ_FOREACH(vs, &vnet_constructors, link) {
62210108cb6SBjoern A. Zeeb 		curvnet->vnet_state = vs->subsystem;
623d0728d71SRobert Watson 		vs->func(vs->arg);
62410108cb6SBjoern A. Zeeb 	}
625a99fcfd4SMarko Zec 	VNET_SYSINIT_RUNLOCK();
626d0728d71SRobert Watson }
627d0728d71SRobert Watson 
628d0728d71SRobert Watson /*
629c8f6a138SRobert Watson  * Invoke all registered vnet destructors on the current vnet.  Used during
630c8f6a138SRobert Watson  * vnet destruction.  The caller is responsible for ensuring the dying vnet
631a99fcfd4SMarko Zec  * the current vnet and that the vnet_sysinit_sxlock lock is locked.
632d0728d71SRobert Watson  */
633fb9b76e0SZhenlei Huang static void
vnet_sysuninit(void)634d0728d71SRobert Watson vnet_sysuninit(void)
635d0728d71SRobert Watson {
636d0728d71SRobert Watson 	struct vnet_sysinit *vs;
637d0728d71SRobert Watson 
638a99fcfd4SMarko Zec 	VNET_SYSINIT_RLOCK();
639d0728d71SRobert Watson 	TAILQ_FOREACH_REVERSE(vs, &vnet_destructors, vnet_sysuninit_head,
64010108cb6SBjoern A. Zeeb 	    link) {
64110108cb6SBjoern A. Zeeb 		curvnet->vnet_state = vs->subsystem;
642d0728d71SRobert Watson 		vs->func(vs->arg);
64310108cb6SBjoern A. Zeeb 	}
644a99fcfd4SMarko Zec 	VNET_SYSINIT_RUNLOCK();
645d0728d71SRobert Watson }
646530c0060SRobert Watson 
64742eedeacSBjoern A. Zeeb /*
64842eedeacSBjoern A. Zeeb  * EVENTHANDLER(9) extensions.
64942eedeacSBjoern A. Zeeb  */
65042eedeacSBjoern A. Zeeb /*
65142eedeacSBjoern A. Zeeb  * Invoke the eventhandler function originally registered with the possibly
65242eedeacSBjoern A. Zeeb  * registered argument for all virtual network stack instances.
65342eedeacSBjoern A. Zeeb  *
65442eedeacSBjoern A. Zeeb  * This iterator can only be used for eventhandlers that do not take any
65542eedeacSBjoern A. Zeeb  * additional arguments, as we do ignore the variadic arguments from the
65642eedeacSBjoern A. Zeeb  * EVENTHANDLER_INVOKE() call.
65742eedeacSBjoern A. Zeeb  */
65842eedeacSBjoern A. Zeeb void
vnet_global_eventhandler_iterator_func(void * arg,...)65942eedeacSBjoern A. Zeeb vnet_global_eventhandler_iterator_func(void *arg, ...)
66042eedeacSBjoern A. Zeeb {
66142eedeacSBjoern A. Zeeb 	VNET_ITERATOR_DECL(vnet_iter);
66242eedeacSBjoern A. Zeeb 	struct eventhandler_entry_vimage *v_ee;
66342eedeacSBjoern A. Zeeb 
66442eedeacSBjoern A. Zeeb 	/*
66542eedeacSBjoern A. Zeeb 	 * There is a bug here in that we should actually cast things to
66642eedeacSBjoern A. Zeeb 	 * (struct eventhandler_entry_ ## name *)  but that's not easily
66742eedeacSBjoern A. Zeeb 	 * possible in here so just re-using the variadic version we
66842eedeacSBjoern A. Zeeb 	 * defined for the generic vimage case.
66942eedeacSBjoern A. Zeeb 	 */
67042eedeacSBjoern A. Zeeb 	v_ee = arg;
67142eedeacSBjoern A. Zeeb 	VNET_LIST_RLOCK();
67242eedeacSBjoern A. Zeeb 	VNET_FOREACH(vnet_iter) {
67342eedeacSBjoern A. Zeeb 		CURVNET_SET(vnet_iter);
67442eedeacSBjoern A. Zeeb 		((vimage_iterator_func_t)v_ee->func)(v_ee->ee_arg);
67542eedeacSBjoern A. Zeeb 		CURVNET_RESTORE();
67642eedeacSBjoern A. Zeeb 	}
67742eedeacSBjoern A. Zeeb 	VNET_LIST_RUNLOCK();
67842eedeacSBjoern A. Zeeb }
67942eedeacSBjoern A. Zeeb 
6800a705ab6SMarko Zec #ifdef VNET_DEBUG
6810a705ab6SMarko Zec struct vnet_recursion {
6820a705ab6SMarko Zec 	SLIST_ENTRY(vnet_recursion)	 vnr_le;
6830a705ab6SMarko Zec 	const char			*prev_fn;
6840a705ab6SMarko Zec 	const char			*where_fn;
6850a705ab6SMarko Zec 	int				 where_line;
6860a705ab6SMarko Zec 	struct vnet			*old_vnet;
6870a705ab6SMarko Zec 	struct vnet			*new_vnet;
6880a705ab6SMarko Zec };
6890a705ab6SMarko Zec 
6900a705ab6SMarko Zec static SLIST_HEAD(, vnet_recursion) vnet_recursions =
6910a705ab6SMarko Zec     SLIST_HEAD_INITIALIZER(vnet_recursions);
6920a705ab6SMarko Zec 
6930a705ab6SMarko Zec static void
vnet_print_recursion(struct vnet_recursion * vnr,int brief)6940a705ab6SMarko Zec vnet_print_recursion(struct vnet_recursion *vnr, int brief)
6950a705ab6SMarko Zec {
6960a705ab6SMarko Zec 
6970a705ab6SMarko Zec 	if (!brief)
6980a705ab6SMarko Zec 		printf("CURVNET_SET() recursion in ");
6990a705ab6SMarko Zec 	printf("%s() line %d, prev in %s()", vnr->where_fn, vnr->where_line,
7000a705ab6SMarko Zec 	    vnr->prev_fn);
7010a705ab6SMarko Zec 	if (brief)
7020a705ab6SMarko Zec 		printf(", ");
7030a705ab6SMarko Zec 	else
7040a705ab6SMarko Zec 		printf("\n    ");
7050a705ab6SMarko Zec 	printf("%p -> %p\n", vnr->old_vnet, vnr->new_vnet);
7060a705ab6SMarko Zec }
7070a705ab6SMarko Zec 
7080a705ab6SMarko Zec void
vnet_log_recursion(struct vnet * old_vnet,const char * old_fn,int line)7090a705ab6SMarko Zec vnet_log_recursion(struct vnet *old_vnet, const char *old_fn, int line)
7100a705ab6SMarko Zec {
7110a705ab6SMarko Zec 	struct vnet_recursion *vnr;
7120a705ab6SMarko Zec 
7130a705ab6SMarko Zec 	/* Skip already logged recursion events. */
7140a705ab6SMarko Zec 	SLIST_FOREACH(vnr, &vnet_recursions, vnr_le)
7150a705ab6SMarko Zec 		if (vnr->prev_fn == old_fn &&
7160a705ab6SMarko Zec 		    vnr->where_fn == curthread->td_vnet_lpush &&
7170a705ab6SMarko Zec 		    vnr->where_line == line &&
7180a705ab6SMarko Zec 		    (vnr->old_vnet == vnr->new_vnet) == (curvnet == old_vnet))
7190a705ab6SMarko Zec 			return;
7200a705ab6SMarko Zec 
7210a705ab6SMarko Zec 	vnr = malloc(sizeof(*vnr), M_VNET, M_NOWAIT | M_ZERO);
7220a705ab6SMarko Zec 	if (vnr == NULL)
7230a705ab6SMarko Zec 		panic("%s: malloc failed", __func__);
7240a705ab6SMarko Zec 	vnr->prev_fn = old_fn;
7250a705ab6SMarko Zec 	vnr->where_fn = curthread->td_vnet_lpush;
7260a705ab6SMarko Zec 	vnr->where_line = line;
7270a705ab6SMarko Zec 	vnr->old_vnet = old_vnet;
7280a705ab6SMarko Zec 	vnr->new_vnet = curvnet;
7290a705ab6SMarko Zec 
7300a705ab6SMarko Zec 	SLIST_INSERT_HEAD(&vnet_recursions, vnr, vnr_le);
7310a705ab6SMarko Zec 
7320a705ab6SMarko Zec 	vnet_print_recursion(vnr, 0);
7330a705ab6SMarko Zec #ifdef KDB
7340a705ab6SMarko Zec 	kdb_backtrace();
7350a705ab6SMarko Zec #endif
7360a705ab6SMarko Zec }
7370a705ab6SMarko Zec #endif /* VNET_DEBUG */
7380a705ab6SMarko Zec 
73942eedeacSBjoern A. Zeeb /*
74042eedeacSBjoern A. Zeeb  * DDB(4).
74142eedeacSBjoern A. Zeeb  */
742530c0060SRobert Watson #ifdef DDB
7435fa0728bSBjoern A. Zeeb static void
db_vnet_print(struct vnet * vnet)7445fa0728bSBjoern A. Zeeb db_vnet_print(struct vnet *vnet)
7455fa0728bSBjoern A. Zeeb {
7465fa0728bSBjoern A. Zeeb 
7475fa0728bSBjoern A. Zeeb 	db_printf("vnet            = %p\n", vnet);
7485fa0728bSBjoern A. Zeeb 	db_printf(" vnet_magic_n   = %#08x (%s, orig %#08x)\n",
7495fa0728bSBjoern A. Zeeb 	    vnet->vnet_magic_n,
7505fa0728bSBjoern A. Zeeb 	    (vnet->vnet_magic_n == VNET_MAGIC_N) ?
7515fa0728bSBjoern A. Zeeb 		"ok" : "mismatch", VNET_MAGIC_N);
7525fa0728bSBjoern A. Zeeb 	db_printf(" vnet_ifcnt     = %u\n", vnet->vnet_ifcnt);
7535fa0728bSBjoern A. Zeeb 	db_printf(" vnet_sockcnt   = %u\n", vnet->vnet_sockcnt);
7545fa0728bSBjoern A. Zeeb 	db_printf(" vnet_data_mem  = %p\n", vnet->vnet_data_mem);
7555fa0728bSBjoern A. Zeeb 	db_printf(" vnet_data_base = %#jx\n",
7565fa0728bSBjoern A. Zeeb 	    (uintmax_t)vnet->vnet_data_base);
75710108cb6SBjoern A. Zeeb 	db_printf(" vnet_state     = %#08x\n", vnet->vnet_state);
75810108cb6SBjoern A. Zeeb 	db_printf(" vnet_shutdown  = %#03x\n", vnet->vnet_shutdown);
7595fa0728bSBjoern A. Zeeb 	db_printf("\n");
7605fa0728bSBjoern A. Zeeb }
7615fa0728bSBjoern A. Zeeb 
DB_SHOW_ALL_COMMAND(vnets,db_show_all_vnets)7625fa0728bSBjoern A. Zeeb DB_SHOW_ALL_COMMAND(vnets, db_show_all_vnets)
763530c0060SRobert Watson {
764530c0060SRobert Watson 	VNET_ITERATOR_DECL(vnet_iter);
765530c0060SRobert Watson 
766530c0060SRobert Watson 	VNET_FOREACH(vnet_iter) {
7675fa0728bSBjoern A. Zeeb 		db_vnet_print(vnet_iter);
768281c86a4SBjoern A. Zeeb 		if (db_pager_quit)
769281c86a4SBjoern A. Zeeb 			break;
770530c0060SRobert Watson 	}
771530c0060SRobert Watson }
7720a705ab6SMarko Zec 
DB_SHOW_COMMAND(vnet,db_show_vnet)7735fa0728bSBjoern A. Zeeb DB_SHOW_COMMAND(vnet, db_show_vnet)
7745fa0728bSBjoern A. Zeeb {
7755fa0728bSBjoern A. Zeeb 
7765fa0728bSBjoern A. Zeeb 	if (!have_addr) {
7775fa0728bSBjoern A. Zeeb 		db_printf("usage: show vnet <struct vnet *>\n");
7785fa0728bSBjoern A. Zeeb 		return;
7795fa0728bSBjoern A. Zeeb 	}
7805fa0728bSBjoern A. Zeeb 
7815fa0728bSBjoern A. Zeeb 	db_vnet_print((struct vnet *)addr);
7825fa0728bSBjoern A. Zeeb }
7835fa0728bSBjoern A. Zeeb 
7843e0490b3SBjoern A. Zeeb static void
db_show_vnet_print_vs(struct vnet_sysinit * vs,int ddb)7853e0490b3SBjoern A. Zeeb db_show_vnet_print_vs(struct vnet_sysinit *vs, int ddb)
7863e0490b3SBjoern A. Zeeb {
7873e0490b3SBjoern A. Zeeb 	const char *vsname, *funcname;
7883e0490b3SBjoern A. Zeeb 	c_db_sym_t sym;
7893e0490b3SBjoern A. Zeeb 	db_expr_t  offset;
7903e0490b3SBjoern A. Zeeb 
7913e0490b3SBjoern A. Zeeb #define xprint(...)							\
7923e0490b3SBjoern A. Zeeb 	if (ddb)							\
7933e0490b3SBjoern A. Zeeb 		db_printf(__VA_ARGS__);					\
7943e0490b3SBjoern A. Zeeb 	else								\
7953e0490b3SBjoern A. Zeeb 		printf(__VA_ARGS__)
7963e0490b3SBjoern A. Zeeb 
7973e0490b3SBjoern A. Zeeb 	if (vs == NULL) {
7983e0490b3SBjoern A. Zeeb 		xprint("%s: no vnet_sysinit * given\n", __func__);
7993e0490b3SBjoern A. Zeeb 		return;
8003e0490b3SBjoern A. Zeeb 	}
8013e0490b3SBjoern A. Zeeb 
8023e0490b3SBjoern A. Zeeb 	sym = db_search_symbol((vm_offset_t)vs, DB_STGY_ANY, &offset);
8033e0490b3SBjoern A. Zeeb 	db_symbol_values(sym, &vsname, NULL);
8043e0490b3SBjoern A. Zeeb 	sym = db_search_symbol((vm_offset_t)vs->func, DB_STGY_PROC, &offset);
8053e0490b3SBjoern A. Zeeb 	db_symbol_values(sym, &funcname, NULL);
8063e0490b3SBjoern A. Zeeb 	xprint("%s(%p)\n", (vsname != NULL) ? vsname : "", vs);
8075fa0728bSBjoern A. Zeeb 	xprint("  %#08x %#08x\n", vs->subsystem, vs->order);
8083e0490b3SBjoern A. Zeeb 	xprint("  %p(%s)(%p)\n",
8093e0490b3SBjoern A. Zeeb 	    vs->func, (funcname != NULL) ? funcname : "", vs->arg);
8103e0490b3SBjoern A. Zeeb #undef xprint
8113e0490b3SBjoern A. Zeeb }
8123e0490b3SBjoern A. Zeeb 
DB_SHOW_COMMAND_FLAGS(vnet_sysinit,db_show_vnet_sysinit,DB_CMD_MEMSAFE)813c84c5e00SMitchell Horne DB_SHOW_COMMAND_FLAGS(vnet_sysinit, db_show_vnet_sysinit, DB_CMD_MEMSAFE)
8143e0490b3SBjoern A. Zeeb {
8153e0490b3SBjoern A. Zeeb 	struct vnet_sysinit *vs;
8163e0490b3SBjoern A. Zeeb 
8173e0490b3SBjoern A. Zeeb 	db_printf("VNET_SYSINIT vs Name(Ptr)\n");
8183e0490b3SBjoern A. Zeeb 	db_printf("  Subsystem  Order\n");
8193e0490b3SBjoern A. Zeeb 	db_printf("  Function(Name)(Arg)\n");
8203e0490b3SBjoern A. Zeeb 	TAILQ_FOREACH(vs, &vnet_constructors, link) {
8213e0490b3SBjoern A. Zeeb 		db_show_vnet_print_vs(vs, 1);
8223e0490b3SBjoern A. Zeeb 		if (db_pager_quit)
8233e0490b3SBjoern A. Zeeb 			break;
8243e0490b3SBjoern A. Zeeb 	}
8253e0490b3SBjoern A. Zeeb }
8263e0490b3SBjoern A. Zeeb 
DB_SHOW_COMMAND_FLAGS(vnet_sysuninit,db_show_vnet_sysuninit,DB_CMD_MEMSAFE)827c84c5e00SMitchell Horne DB_SHOW_COMMAND_FLAGS(vnet_sysuninit, db_show_vnet_sysuninit, DB_CMD_MEMSAFE)
8283e0490b3SBjoern A. Zeeb {
8293e0490b3SBjoern A. Zeeb 	struct vnet_sysinit *vs;
8303e0490b3SBjoern A. Zeeb 
8313e0490b3SBjoern A. Zeeb 	db_printf("VNET_SYSUNINIT vs Name(Ptr)\n");
8323e0490b3SBjoern A. Zeeb 	db_printf("  Subsystem  Order\n");
8333e0490b3SBjoern A. Zeeb 	db_printf("  Function(Name)(Arg)\n");
8343e0490b3SBjoern A. Zeeb 	TAILQ_FOREACH_REVERSE(vs, &vnet_destructors, vnet_sysuninit_head,
8353e0490b3SBjoern A. Zeeb 	    link) {
8363e0490b3SBjoern A. Zeeb 		db_show_vnet_print_vs(vs, 1);
8373e0490b3SBjoern A. Zeeb 		if (db_pager_quit)
8383e0490b3SBjoern A. Zeeb 			break;
8393e0490b3SBjoern A. Zeeb 	}
8403e0490b3SBjoern A. Zeeb }
8413e0490b3SBjoern A. Zeeb 
8420a705ab6SMarko Zec #ifdef VNET_DEBUG
DB_SHOW_COMMAND_FLAGS(vnetrcrs,db_show_vnetrcrs,DB_CMD_MEMSAFE)843c84c5e00SMitchell Horne DB_SHOW_COMMAND_FLAGS(vnetrcrs, db_show_vnetrcrs, DB_CMD_MEMSAFE)
8440a705ab6SMarko Zec {
8450a705ab6SMarko Zec 	struct vnet_recursion *vnr;
8460a705ab6SMarko Zec 
8470a705ab6SMarko Zec 	SLIST_FOREACH(vnr, &vnet_recursions, vnr_le)
8480a705ab6SMarko Zec 		vnet_print_recursion(vnr, 1);
8490a705ab6SMarko Zec }
850530c0060SRobert Watson #endif
8510a705ab6SMarko Zec #endif /* DDB */
852