1195699Srwatson/*-
2196019Srwatson * Copyright (c) 2004-2009 University of Zagreb
3196019Srwatson * Copyright (c) 2006-2009 FreeBSD Foundation
4196019Srwatson * All rights reserved.
5196019Srwatson *
6196019Srwatson * This software was developed by the University of Zagreb and the
7196019Srwatson * FreeBSD Foundation under sponsorship by the Stichting NLnet and the
8196019Srwatson * FreeBSD Foundation.
9196019Srwatson *
10195699Srwatson * Copyright (c) 2009 Jeffrey Roberson <jeff@freebsd.org>
11195699Srwatson * Copyright (c) 2009 Robert N. M. Watson
12195699Srwatson * All rights reserved.
13195699Srwatson *
14195699Srwatson * Redistribution and use in source and binary forms, with or without
15195699Srwatson * modification, are permitted provided that the following conditions
16195699Srwatson * are met:
17195699Srwatson * 1. Redistributions of source code must retain the above copyright
18195699Srwatson *    notice, this list of conditions and the following disclaimer.
19195699Srwatson * 2. Redistributions in binary form must reproduce the above copyright
20195699Srwatson *    notice, this list of conditions and the following disclaimer in the
21195699Srwatson *    documentation and/or other materials provided with the distribution.
22195699Srwatson *
23195699Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24195699Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25195699Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26195699Srwatson * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27195699Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28195699Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29195699Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30195699Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31195699Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32195699Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33195699Srwatson * SUCH DAMAGE.
34195699Srwatson */
35195699Srwatson
36195699Srwatson#include <sys/cdefs.h>
37195699Srwatson__FBSDID("$FreeBSD: stable/11/sys/net/vnet.c 359652 2020-04-06 07:16:31Z hselasky $");
38195699Srwatson
39196019Srwatson#include "opt_ddb.h"
40203483Szec#include "opt_kdb.h"
41196019Srwatson
42195699Srwatson#include <sys/param.h>
43203483Szec#include <sys/kdb.h>
44195699Srwatson#include <sys/kernel.h>
45196019Srwatson#include <sys/jail.h>
46203727Sbz#include <sys/sdt.h>
47195699Srwatson#include <sys/systm.h>
48195699Srwatson#include <sys/sysctl.h>
49205345Sbz#include <sys/eventhandler.h>
50195699Srwatson#include <sys/lock.h>
51195699Srwatson#include <sys/malloc.h>
52195699Srwatson#include <sys/proc.h>
53196019Srwatson#include <sys/socket.h>
54195699Srwatson#include <sys/sx.h>
55195699Srwatson#include <sys/sysctl.h>
56195699Srwatson
57205345Sbz#include <machine/stdarg.h>
58205345Sbz
59196019Srwatson#ifdef DDB
60196019Srwatson#include <ddb/ddb.h>
61203729Sbz#include <ddb/db_sym.h>
62196019Srwatson#endif
63196019Srwatson
64196019Srwatson#include <net/if.h>
65196019Srwatson#include <net/if_var.h>
66195699Srwatson#include <net/vnet.h>
67195699Srwatson
68195699Srwatson/*-
69195972Srwatson * This file implements core functions for virtual network stacks:
70195972Srwatson *
71196025Srwatson * - Virtual network stack management functions.
72196019Srwatson *
73196025Srwatson * - Virtual network stack memory allocator, which virtualizes global
74195972Srwatson *   variables in the network stack
75195972Srwatson *
76195972Srwatson * - Virtualized SYSINIT's/SYSUNINIT's, which allow network stack subsystems
77195972Srwatson *   to register startup/shutdown events to be run for each virtual network
78195972Srwatson *   stack instance.
79196019Srwatson */
80196019Srwatson
81217203SbzFEATURE(vimage, "VIMAGE kernel virtualization");
82217203Sbz
83227293Sedstatic MALLOC_DEFINE(M_VNET, "vnet", "network stack control block");
84196019Srwatson
85196019Srwatson/*
86196019Srwatson * The virtual network stack list has two read-write locks, one sleepable and
87196019Srwatson * the other not, so that the list can be stablized and walked in a variety
88196019Srwatson * of network stack contexts.  Both must be acquired exclusively to modify
89196025Srwatson * the list, but a read lock of either lock is sufficient to walk the list.
90196019Srwatson */
91196019Srwatsonstruct rwlock		vnet_rwlock;
92196019Srwatsonstruct sx		vnet_sxlock;
93196019Srwatson
94196019Srwatson#define	VNET_LIST_WLOCK() do {						\
95196019Srwatson	sx_xlock(&vnet_sxlock);						\
96196019Srwatson	rw_wlock(&vnet_rwlock);						\
97196019Srwatson} while (0)
98196019Srwatson
99196019Srwatson#define	VNET_LIST_WUNLOCK() do {					\
100196019Srwatson	rw_wunlock(&vnet_rwlock);					\
101196019Srwatson	sx_xunlock(&vnet_sxlock);					\
102196019Srwatson} while (0)
103196019Srwatson
104196019Srwatsonstruct vnet_list_head vnet_head;
105196019Srwatsonstruct vnet *vnet0;
106196019Srwatson
107196019Srwatson/*
108195972Srwatson * The virtual network stack allocator provides storage for virtualized
109195972Srwatson * global variables.  These variables are defined/declared using the
110195972Srwatson * VNET_DEFINE()/VNET_DECLARE() macros, which place them in the 'set_vnet'
111195972Srwatson * linker set.  The details of the implementation are somewhat subtle, but
112195972Srwatson * allow the majority of most network subsystems to maintain
113195699Srwatson * virtualization-agnostic.
114195699Srwatson *
115195699Srwatson * The virtual network stack allocator handles variables in the base kernel
116195699Srwatson * vs. modules in similar but different ways.  In both cases, virtualized
117195699Srwatson * global variables are marked as such by being declared to be part of the
118195699Srwatson * vnet linker set.  These "master" copies of global variables serve two
119195699Srwatson * functions:
120195699Srwatson *
121195699Srwatson * (1) They contain static initialization or "default" values for global
122195699Srwatson *     variables which will be propagated to each virtual network stack
123195699Srwatson *     instance when created.  As with normal global variables, they default
124195699Srwatson *     to zero-filled.
125195699Srwatson *
126195699Srwatson * (2) They act as unique global names by which the variable can be referred
127195699Srwatson *     to, regardless of network stack instance.  The single global symbol
128195699Srwatson *     will be used to calculate the location of a per-virtual instance
129195699Srwatson *     variable at run-time.
130195699Srwatson *
131195699Srwatson * Each virtual network stack instance has a complete copy of each
132195699Srwatson * virtualized global variable, stored in a malloc'd block of memory
133195699Srwatson * referred to by vnet->vnet_data_mem.  Critical to the design is that each
134195699Srwatson * per-instance memory block is laid out identically to the master block so
135195699Srwatson * that the offset of each global variable is the same across all blocks.  To
136195699Srwatson * optimize run-time access, a precalculated 'base' address,
137195699Srwatson * vnet->vnet_data_base, is stored in each vnet, and is the amount that can
138195699Srwatson * be added to the address of a 'master' instance of a variable to get to the
139195699Srwatson * per-vnet instance.
140195699Srwatson *
141195699Srwatson * Virtualized global variables are handled in a similar manner, but as each
142195699Srwatson * module has its own 'set_vnet' linker set, and we want to keep all
143195699Srwatson * virtualized globals togther, we reserve space in the kernel's linker set
144195699Srwatson * for potential module variables using a per-vnet character array,
145195699Srwatson * 'modspace'.  The virtual network stack allocator maintains a free list to
146195699Srwatson * track what space in the array is free (all, initially) and as modules are
147195699Srwatson * linked, allocates portions of the space to specific globals.  The kernel
148195699Srwatson * module linker queries the virtual network stack allocator and will
149195699Srwatson * bind references of the global to the location during linking.  It also
150195699Srwatson * calls into the virtual network stack allocator, once the memory is
151195699Srwatson * initialized, in order to propagate the new static initializations to all
152195699Srwatson * existing virtual network stack instances so that the soon-to-be executing
153195699Srwatson * module will find every network stack instance with proper default values.
154195699Srwatson */
155195699Srwatson
156195699Srwatson/*
157195699Srwatson * Number of bytes of data in the 'set_vnet' linker set, and hence the total
158195699Srwatson * size of all kernel virtualized global variables, and the malloc(9) type
159195699Srwatson * that will be used to allocate it.
160195699Srwatson */
161195699Srwatson#define	VNET_BYTES	(VNET_STOP - VNET_START)
162195699Srwatson
163227293Sedstatic MALLOC_DEFINE(M_VNET_DATA, "vnet_data", "VNET data");
164195699Srwatson
165195699Srwatson/*
166195699Srwatson * VNET_MODMIN is the minimum number of bytes we will reserve for the sum of
167195699Srwatson * global variables across all loaded modules.  As this actually sizes an
168195699Srwatson * array declared as a virtualized global variable in the kernel itself, and
169195699Srwatson * we want the virtualized global variable space to be page-sized, we may
170195699Srwatson * have more space than that in practice.
171195699Srwatson */
172340051Sbz#define	VNET_MODMIN	(8 * PAGE_SIZE)
173195699Srwatson#define	VNET_SIZE	roundup2(VNET_BYTES, PAGE_SIZE)
174195699Srwatson
175195699Srwatson/*
176195699Srwatson * Space to store virtualized global variables from loadable kernel modules,
177195699Srwatson * and the free list to manage it.
178195699Srwatson */
179215701Sdimstatic VNET_DEFINE(char, modspace[VNET_MODMIN]);
180195699Srwatson
181195837Srwatson/*
182196025Srwatson * Global lists of subsystem constructor and destructors for vnets.  They are
183196633Szec * registered via VNET_SYSINIT() and VNET_SYSUNINIT().  Both lists are
184196633Szec * protected by the vnet_sysinit_sxlock global lock.
185195837Srwatson */
186195837Srwatsonstatic TAILQ_HEAD(vnet_sysinit_head, vnet_sysinit) vnet_constructors =
187195837Srwatson	TAILQ_HEAD_INITIALIZER(vnet_constructors);
188195837Srwatsonstatic TAILQ_HEAD(vnet_sysuninit_head, vnet_sysinit) vnet_destructors =
189195837Srwatson	TAILQ_HEAD_INITIALIZER(vnet_destructors);
190195837Srwatson
191196633Szecstruct sx		vnet_sysinit_sxlock;
192196633Szec
193196633Szec#define	VNET_SYSINIT_WLOCK()	sx_xlock(&vnet_sysinit_sxlock);
194196633Szec#define	VNET_SYSINIT_WUNLOCK()	sx_xunlock(&vnet_sysinit_sxlock);
195196633Szec#define	VNET_SYSINIT_RLOCK()	sx_slock(&vnet_sysinit_sxlock);
196196633Szec#define	VNET_SYSINIT_RUNLOCK()	sx_sunlock(&vnet_sysinit_sxlock);
197196633Szec
198195699Srwatsonstruct vnet_data_free {
199195699Srwatson	uintptr_t	vnd_start;
200195699Srwatson	int		vnd_len;
201195699Srwatson	TAILQ_ENTRY(vnet_data_free) vnd_link;
202195699Srwatson};
203195699Srwatson
204227293Sedstatic MALLOC_DEFINE(M_VNET_DATA_FREE, "vnet_data_free",
205227293Sed    "VNET resource accounting");
206195699Srwatsonstatic TAILQ_HEAD(, vnet_data_free) vnet_data_free_head =
207195699Srwatson	    TAILQ_HEAD_INITIALIZER(vnet_data_free_head);
208195699Srwatsonstatic struct sx vnet_data_free_lock;
209195699Srwatson
210203727SbzSDT_PROVIDER_DEFINE(vnet);
211258622SavgSDT_PROBE_DEFINE1(vnet, functions, vnet_alloc, entry, "int");
212258622SavgSDT_PROBE_DEFINE2(vnet, functions, vnet_alloc, alloc, "int",
213211616Srpaulo    "struct vnet *");
214258622SavgSDT_PROBE_DEFINE2(vnet, functions, vnet_alloc, return,
215211616Srpaulo    "int", "struct vnet *");
216258622SavgSDT_PROBE_DEFINE2(vnet, functions, vnet_destroy, entry,
217211616Srpaulo    "int", "struct vnet *");
218258675SglebiusSDT_PROBE_DEFINE1(vnet, functions, vnet_destroy, return,
219211616Srpaulo    "int");
220203727Sbz
221203729Sbz#ifdef DDB
222203729Sbzstatic void db_show_vnet_print_vs(struct vnet_sysinit *, int);
223203729Sbz#endif
224203729Sbz
225195699Srwatson/*
226196019Srwatson * Allocate a virtual network stack.
227196019Srwatson */
228196019Srwatsonstruct vnet *
229196019Srwatsonvnet_alloc(void)
230196019Srwatson{
231196019Srwatson	struct vnet *vnet;
232196019Srwatson
233203727Sbz	SDT_PROBE1(vnet, functions, vnet_alloc, entry, __LINE__);
234196019Srwatson	vnet = malloc(sizeof(struct vnet), M_VNET, M_WAITOK | M_ZERO);
235196019Srwatson	vnet->vnet_magic_n = VNET_MAGIC_N;
236300155Sbz	vnet->vnet_state = 0;
237203727Sbz	SDT_PROBE2(vnet, functions, vnet_alloc, alloc, __LINE__, vnet);
238196019Srwatson
239196024Srwatson	/*
240196024Srwatson	 * Allocate storage for virtualized global variables and copy in
241196024Srwatson	 * initial values form our 'master' copy.
242196024Srwatson	 */
243196024Srwatson	vnet->vnet_data_mem = malloc(VNET_SIZE, M_VNET_DATA, M_WAITOK);
244196024Srwatson	memcpy(vnet->vnet_data_mem, (void *)VNET_START, VNET_BYTES);
245196024Srwatson
246196024Srwatson	/*
247196024Srwatson	 * All use of vnet-specific data will immediately subtract VNET_START
248196024Srwatson	 * from the base memory pointer, so pre-calculate that now to avoid
249196024Srwatson	 * it on each use.
250196024Srwatson	 */
251196024Srwatson	vnet->vnet_data_base = (uintptr_t)vnet->vnet_data_mem - VNET_START;
252196024Srwatson
253196019Srwatson	/* Initialize / attach vnet module instances. */
254196019Srwatson	CURVNET_SET_QUIET(vnet);
255196019Srwatson	vnet_sysinit();
256196019Srwatson	CURVNET_RESTORE();
257196019Srwatson
258196633Szec	VNET_LIST_WLOCK();
259196019Srwatson	LIST_INSERT_HEAD(&vnet_head, vnet, vnet_le);
260196019Srwatson	VNET_LIST_WUNLOCK();
261196019Srwatson
262203727Sbz	SDT_PROBE2(vnet, functions, vnet_alloc, return, __LINE__, vnet);
263196019Srwatson	return (vnet);
264196019Srwatson}
265196019Srwatson
266196019Srwatson/*
267196019Srwatson * Destroy a virtual network stack.
268196019Srwatson */
269196019Srwatsonvoid
270196019Srwatsonvnet_destroy(struct vnet *vnet)
271196019Srwatson{
272196019Srwatson
273203727Sbz	SDT_PROBE2(vnet, functions, vnet_destroy, entry, __LINE__, vnet);
274196019Srwatson	KASSERT(vnet->vnet_sockcnt == 0,
275196019Srwatson	    ("%s: vnet still has sockets", __func__));
276196019Srwatson
277196019Srwatson	VNET_LIST_WLOCK();
278196019Srwatson	LIST_REMOVE(vnet, vnet_le);
279196633Szec	VNET_LIST_WUNLOCK();
280196019Srwatson
281196019Srwatson	CURVNET_SET_QUIET(vnet);
282196019Srwatson	vnet_sysuninit();
283196019Srwatson	CURVNET_RESTORE();
284196019Srwatson
285196024Srwatson	/*
286196024Srwatson	 * Release storage for the virtual network stack instance.
287196024Srwatson	 */
288196024Srwatson	free(vnet->vnet_data_mem, M_VNET_DATA);
289196024Srwatson	vnet->vnet_data_mem = NULL;
290196024Srwatson	vnet->vnet_data_base = 0;
291196019Srwatson	vnet->vnet_magic_n = 0xdeadbeef;
292196019Srwatson	free(vnet, M_VNET);
293203727Sbz	SDT_PROBE1(vnet, functions, vnet_destroy, return, __LINE__);
294196019Srwatson}
295196019Srwatson
296196019Srwatson/*
297196019Srwatson * Boot time initialization and allocation of virtual network stacks.
298196019Srwatson */
299196019Srwatsonstatic void
300300001Sbzvnet_init_prelink(void *arg __unused)
301196019Srwatson{
302196019Srwatson
303196019Srwatson	rw_init(&vnet_rwlock, "vnet_rwlock");
304196019Srwatson	sx_init(&vnet_sxlock, "vnet_sxlock");
305196633Szec	sx_init(&vnet_sysinit_sxlock, "vnet_sysinit_sxlock");
306196019Srwatson	LIST_INIT(&vnet_head);
307196019Srwatson}
308196019SrwatsonSYSINIT(vnet_init_prelink, SI_SUB_VNET_PRELINK, SI_ORDER_FIRST,
309196019Srwatson    vnet_init_prelink, NULL);
310196019Srwatson
311196019Srwatsonstatic void
312300001Sbzvnet0_init(void *arg __unused)
313196019Srwatson{
314196019Srwatson
315196026Srwatson	/* Warn people before take off - in case we crash early. */
316196026Srwatson	printf("WARNING: VIMAGE (virtualized network stack) is a highly "
317196026Srwatson	    "experimental feature.\n");
318196026Srwatson
319196019Srwatson	/*
320196019Srwatson	 * We MUST clear curvnet in vi_init_done() before going SMP,
321196019Srwatson	 * otherwise CURVNET_SET() macros would scream about unnecessary
322196019Srwatson	 * curvnet recursions.
323196019Srwatson	 */
324196019Srwatson	curvnet = prison0.pr_vnet = vnet0 = vnet_alloc();
325196019Srwatson}
326196019SrwatsonSYSINIT(vnet0_init, SI_SUB_VNET, SI_ORDER_FIRST, vnet0_init, NULL);
327196019Srwatson
328196019Srwatsonstatic void
329300001Sbzvnet_init_done(void *unused __unused)
330196019Srwatson{
331196019Srwatson
332196019Srwatson	curvnet = NULL;
333196019Srwatson}
334302054SbzSYSINIT(vnet_init_done, SI_SUB_VNET_DONE, SI_ORDER_ANY, vnet_init_done,
335196019Srwatson    NULL);
336196019Srwatson
337196019Srwatson/*
338195699Srwatson * Once on boot, initialize the modspace freelist to entirely cover modspace.
339195699Srwatson */
340195699Srwatsonstatic void
341195699Srwatsonvnet_data_startup(void *dummy __unused)
342195699Srwatson{
343195699Srwatson	struct vnet_data_free *df;
344195699Srwatson
345195699Srwatson	df = malloc(sizeof(*df), M_VNET_DATA_FREE, M_WAITOK | M_ZERO);
346195699Srwatson	df->vnd_start = (uintptr_t)&VNET_NAME(modspace);
347208100Sbz	df->vnd_len = VNET_MODMIN;
348195699Srwatson	TAILQ_INSERT_HEAD(&vnet_data_free_head, df, vnd_link);
349195699Srwatson	sx_init(&vnet_data_free_lock, "vnet_data alloc lock");
350195699Srwatson}
351359652ShselaskySYSINIT(vnet_data, SI_SUB_KLD, SI_ORDER_FIRST, vnet_data_startup, NULL);
352195699Srwatson
353300151Sbz/* Dummy VNET_SYSINIT to make sure we always reach the final end state. */
354300151Sbzstatic void
355300151Sbzvnet_sysinit_done(void *unused __unused)
356300151Sbz{
357300151Sbz
358300151Sbz	return;
359300151Sbz}
360300151SbzVNET_SYSINIT(vnet_sysinit_done, SI_SUB_VNET_DONE, SI_ORDER_ANY,
361300151Sbz    vnet_sysinit_done, NULL);
362300151Sbz
363195699Srwatson/*
364195699Srwatson * When a module is loaded and requires storage for a virtualized global
365195699Srwatson * variable, allocate space from the modspace free list.  This interface
366195699Srwatson * should be used only by the kernel linker.
367195699Srwatson */
368195699Srwatsonvoid *
369195699Srwatsonvnet_data_alloc(int size)
370195699Srwatson{
371195699Srwatson	struct vnet_data_free *df;
372195699Srwatson	void *s;
373195699Srwatson
374195699Srwatson	s = NULL;
375195699Srwatson	size = roundup2(size, sizeof(void *));
376195699Srwatson	sx_xlock(&vnet_data_free_lock);
377195699Srwatson	TAILQ_FOREACH(df, &vnet_data_free_head, vnd_link) {
378195699Srwatson		if (df->vnd_len < size)
379195699Srwatson			continue;
380195699Srwatson		if (df->vnd_len == size) {
381195699Srwatson			s = (void *)df->vnd_start;
382195699Srwatson			TAILQ_REMOVE(&vnet_data_free_head, df, vnd_link);
383195699Srwatson			free(df, M_VNET_DATA_FREE);
384195699Srwatson			break;
385195699Srwatson		}
386195699Srwatson		s = (void *)df->vnd_start;
387195699Srwatson		df->vnd_len -= size;
388195699Srwatson		df->vnd_start = df->vnd_start + size;
389195699Srwatson		break;
390195699Srwatson	}
391195699Srwatson	sx_xunlock(&vnet_data_free_lock);
392195699Srwatson
393195699Srwatson	return (s);
394195699Srwatson}
395195699Srwatson
396195699Srwatson/*
397195699Srwatson * Free space for a virtualized global variable on module unload.
398195699Srwatson */
399195699Srwatsonvoid
400195699Srwatsonvnet_data_free(void *start_arg, int size)
401195699Srwatson{
402195699Srwatson	struct vnet_data_free *df;
403195699Srwatson	struct vnet_data_free *dn;
404195699Srwatson	uintptr_t start;
405195699Srwatson	uintptr_t end;
406195699Srwatson
407195699Srwatson	size = roundup2(size, sizeof(void *));
408195699Srwatson	start = (uintptr_t)start_arg;
409195699Srwatson	end = start + size;
410195699Srwatson	/*
411195699Srwatson	 * Free a region of space and merge it with as many neighbors as
412195699Srwatson	 * possible.  Keeping the list sorted simplifies this operation.
413195699Srwatson	 */
414195699Srwatson	sx_xlock(&vnet_data_free_lock);
415195699Srwatson	TAILQ_FOREACH(df, &vnet_data_free_head, vnd_link) {
416195699Srwatson		if (df->vnd_start > end)
417195699Srwatson			break;
418195699Srwatson		/*
419196025Srwatson		 * If we expand at the end of an entry we may have to merge
420196025Srwatson		 * it with the one following it as well.
421195699Srwatson		 */
422195699Srwatson		if (df->vnd_start + df->vnd_len == start) {
423195699Srwatson			df->vnd_len += size;
424195699Srwatson			dn = TAILQ_NEXT(df, vnd_link);
425195699Srwatson			if (df->vnd_start + df->vnd_len == dn->vnd_start) {
426195699Srwatson				df->vnd_len += dn->vnd_len;
427196025Srwatson				TAILQ_REMOVE(&vnet_data_free_head, dn,
428196025Srwatson				    vnd_link);
429195699Srwatson				free(dn, M_VNET_DATA_FREE);
430195699Srwatson			}
431195699Srwatson			sx_xunlock(&vnet_data_free_lock);
432195699Srwatson			return;
433195699Srwatson		}
434195699Srwatson		if (df->vnd_start == end) {
435195699Srwatson			df->vnd_start = start;
436195699Srwatson			df->vnd_len += size;
437195699Srwatson			sx_xunlock(&vnet_data_free_lock);
438195699Srwatson			return;
439195699Srwatson		}
440195699Srwatson	}
441195699Srwatson	dn = malloc(sizeof(*df), M_VNET_DATA_FREE, M_WAITOK | M_ZERO);
442195699Srwatson	dn->vnd_start = start;
443195699Srwatson	dn->vnd_len = size;
444195699Srwatson	if (df)
445195699Srwatson		TAILQ_INSERT_BEFORE(df, dn, vnd_link);
446195699Srwatson	else
447195699Srwatson		TAILQ_INSERT_TAIL(&vnet_data_free_head, dn, vnd_link);
448195699Srwatson	sx_xunlock(&vnet_data_free_lock);
449195699Srwatson}
450195699Srwatson
451195699Srwatson/*
452195699Srwatson * When a new virtualized global variable has been allocated, propagate its
453195699Srwatson * initial value to each already-allocated virtual network stack instance.
454195699Srwatson */
455195699Srwatsonvoid
456195699Srwatsonvnet_data_copy(void *start, int size)
457195699Srwatson{
458196020Srwatson	struct vnet *vnet;
459195699Srwatson
460196020Srwatson	VNET_LIST_RLOCK();
461196020Srwatson	LIST_FOREACH(vnet, &vnet_head, vnet_le)
462196020Srwatson		memcpy((void *)((uintptr_t)vnet->vnet_data_base +
463196020Srwatson		    (uintptr_t)start), start, size);
464196020Srwatson	VNET_LIST_RUNLOCK();
465195699Srwatson}
466195699Srwatson
467195699Srwatson/*
468195837Srwatson * Support for special SYSINIT handlers registered via VNET_SYSINIT()
469195837Srwatson * and VNET_SYSUNINIT().
470195837Srwatson */
471195837Srwatsonvoid
472195837Srwatsonvnet_register_sysinit(void *arg)
473195837Srwatson{
474195837Srwatson	struct vnet_sysinit *vs, *vs2;
475195837Srwatson	struct vnet *vnet;
476195837Srwatson
477195837Srwatson	vs = arg;
478195837Srwatson	KASSERT(vs->subsystem > SI_SUB_VNET, ("vnet sysinit too early"));
479195837Srwatson
480195837Srwatson	/* Add the constructor to the global list of vnet constructors. */
481196633Szec	VNET_SYSINIT_WLOCK();
482195837Srwatson	TAILQ_FOREACH(vs2, &vnet_constructors, link) {
483195837Srwatson		if (vs2->subsystem > vs->subsystem)
484195837Srwatson			break;
485195837Srwatson		if (vs2->subsystem == vs->subsystem && vs2->order > vs->order)
486195837Srwatson			break;
487195837Srwatson	}
488195837Srwatson	if (vs2 != NULL)
489195837Srwatson		TAILQ_INSERT_BEFORE(vs2, vs, link);
490195837Srwatson	else
491195837Srwatson		TAILQ_INSERT_TAIL(&vnet_constructors, vs, link);
492195837Srwatson
493195837Srwatson	/*
494195837Srwatson	 * Invoke the constructor on all the existing vnets when it is
495195837Srwatson	 * registered.
496195837Srwatson	 */
497195837Srwatson	VNET_FOREACH(vnet) {
498195837Srwatson		CURVNET_SET_QUIET(vnet);
499195837Srwatson		vs->func(vs->arg);
500195837Srwatson		CURVNET_RESTORE();
501195837Srwatson	}
502196633Szec	VNET_SYSINIT_WUNLOCK();
503195837Srwatson}
504195837Srwatson
505195837Srwatsonvoid
506195837Srwatsonvnet_deregister_sysinit(void *arg)
507195837Srwatson{
508195837Srwatson	struct vnet_sysinit *vs;
509195837Srwatson
510195837Srwatson	vs = arg;
511195837Srwatson
512195837Srwatson	/* Remove the constructor from the global list of vnet constructors. */
513196633Szec	VNET_SYSINIT_WLOCK();
514195837Srwatson	TAILQ_REMOVE(&vnet_constructors, vs, link);
515196633Szec	VNET_SYSINIT_WUNLOCK();
516195837Srwatson}
517195837Srwatson
518195837Srwatsonvoid
519195837Srwatsonvnet_register_sysuninit(void *arg)
520195837Srwatson{
521195837Srwatson	struct vnet_sysinit *vs, *vs2;
522195837Srwatson
523195837Srwatson	vs = arg;
524195837Srwatson
525195837Srwatson	/* Add the destructor to the global list of vnet destructors. */
526196633Szec	VNET_SYSINIT_WLOCK();
527195837Srwatson	TAILQ_FOREACH(vs2, &vnet_destructors, link) {
528195837Srwatson		if (vs2->subsystem > vs->subsystem)
529195837Srwatson			break;
530195837Srwatson		if (vs2->subsystem == vs->subsystem && vs2->order > vs->order)
531195837Srwatson			break;
532195837Srwatson	}
533195837Srwatson	if (vs2 != NULL)
534195837Srwatson		TAILQ_INSERT_BEFORE(vs2, vs, link);
535195837Srwatson	else
536195837Srwatson		TAILQ_INSERT_TAIL(&vnet_destructors, vs, link);
537196633Szec	VNET_SYSINIT_WUNLOCK();
538195837Srwatson}
539195837Srwatson
540195837Srwatsonvoid
541195837Srwatsonvnet_deregister_sysuninit(void *arg)
542195837Srwatson{
543195837Srwatson	struct vnet_sysinit *vs;
544195837Srwatson	struct vnet *vnet;
545195837Srwatson
546195837Srwatson	vs = arg;
547195837Srwatson
548195837Srwatson	/*
549195837Srwatson	 * Invoke the destructor on all the existing vnets when it is
550195837Srwatson	 * deregistered.
551195837Srwatson	 */
552196633Szec	VNET_SYSINIT_WLOCK();
553195837Srwatson	VNET_FOREACH(vnet) {
554195837Srwatson		CURVNET_SET_QUIET(vnet);
555195837Srwatson		vs->func(vs->arg);
556195837Srwatson		CURVNET_RESTORE();
557195837Srwatson	}
558195837Srwatson
559195837Srwatson	/* Remove the destructor from the global list of vnet destructors. */
560195837Srwatson	TAILQ_REMOVE(&vnet_destructors, vs, link);
561196633Szec	VNET_SYSINIT_WUNLOCK();
562195837Srwatson}
563195837Srwatson
564195837Srwatson/*
565196025Srwatson * Invoke all registered vnet constructors on the current vnet.  Used during
566196025Srwatson * vnet construction.  The caller is responsible for ensuring the new vnet is
567196633Szec * the current vnet and that the vnet_sysinit_sxlock lock is locked.
568195837Srwatson */
569195837Srwatsonvoid
570195837Srwatsonvnet_sysinit(void)
571195837Srwatson{
572195837Srwatson	struct vnet_sysinit *vs;
573195837Srwatson
574196633Szec	VNET_SYSINIT_RLOCK();
575195837Srwatson	TAILQ_FOREACH(vs, &vnet_constructors, link) {
576300155Sbz		curvnet->vnet_state = vs->subsystem;
577195837Srwatson		vs->func(vs->arg);
578195837Srwatson	}
579196633Szec	VNET_SYSINIT_RUNLOCK();
580195837Srwatson}
581195837Srwatson
582195837Srwatson/*
583196025Srwatson * Invoke all registered vnet destructors on the current vnet.  Used during
584196025Srwatson * vnet destruction.  The caller is responsible for ensuring the dying vnet
585196633Szec * the current vnet and that the vnet_sysinit_sxlock lock is locked.
586195837Srwatson */
587195837Srwatsonvoid
588195837Srwatsonvnet_sysuninit(void)
589195837Srwatson{
590195837Srwatson	struct vnet_sysinit *vs;
591195837Srwatson
592196633Szec	VNET_SYSINIT_RLOCK();
593195837Srwatson	TAILQ_FOREACH_REVERSE(vs, &vnet_destructors, vnet_sysuninit_head,
594195837Srwatson	    link) {
595300155Sbz		curvnet->vnet_state = vs->subsystem;
596195837Srwatson		vs->func(vs->arg);
597195837Srwatson	}
598196633Szec	VNET_SYSINIT_RUNLOCK();
599195837Srwatson}
600196019Srwatson
601205345Sbz/*
602205345Sbz * EVENTHANDLER(9) extensions.
603205345Sbz */
604205345Sbz/*
605205345Sbz * Invoke the eventhandler function originally registered with the possibly
606205345Sbz * registered argument for all virtual network stack instances.
607205345Sbz *
608205345Sbz * This iterator can only be used for eventhandlers that do not take any
609205345Sbz * additional arguments, as we do ignore the variadic arguments from the
610205345Sbz * EVENTHANDLER_INVOKE() call.
611205345Sbz */
612205345Sbzvoid
613205345Sbzvnet_global_eventhandler_iterator_func(void *arg, ...)
614205345Sbz{
615205345Sbz	VNET_ITERATOR_DECL(vnet_iter);
616205345Sbz	struct eventhandler_entry_vimage *v_ee;
617205345Sbz
618205345Sbz	/*
619205345Sbz	 * There is a bug here in that we should actually cast things to
620205345Sbz	 * (struct eventhandler_entry_ ## name *)  but that's not easily
621205345Sbz	 * possible in here so just re-using the variadic version we
622205345Sbz	 * defined for the generic vimage case.
623205345Sbz	 */
624205345Sbz	v_ee = arg;
625205345Sbz	VNET_LIST_RLOCK();
626205345Sbz	VNET_FOREACH(vnet_iter) {
627205345Sbz		CURVNET_SET(vnet_iter);
628205345Sbz		((vimage_iterator_func_t)v_ee->func)(v_ee->ee_arg);
629205345Sbz		CURVNET_RESTORE();
630205345Sbz	}
631205345Sbz	VNET_LIST_RUNLOCK();
632205345Sbz}
633205345Sbz
634203483Szec#ifdef VNET_DEBUG
635203483Szecstruct vnet_recursion {
636203483Szec	SLIST_ENTRY(vnet_recursion)	 vnr_le;
637203483Szec	const char			*prev_fn;
638203483Szec	const char			*where_fn;
639203483Szec	int				 where_line;
640203483Szec	struct vnet			*old_vnet;
641203483Szec	struct vnet			*new_vnet;
642203483Szec};
643203483Szec
644203483Szecstatic SLIST_HEAD(, vnet_recursion) vnet_recursions =
645203483Szec    SLIST_HEAD_INITIALIZER(vnet_recursions);
646203483Szec
647203483Szecstatic void
648203483Szecvnet_print_recursion(struct vnet_recursion *vnr, int brief)
649203483Szec{
650203483Szec
651203483Szec	if (!brief)
652203483Szec		printf("CURVNET_SET() recursion in ");
653203483Szec	printf("%s() line %d, prev in %s()", vnr->where_fn, vnr->where_line,
654203483Szec	    vnr->prev_fn);
655203483Szec	if (brief)
656203483Szec		printf(", ");
657203483Szec	else
658203483Szec		printf("\n    ");
659203483Szec	printf("%p -> %p\n", vnr->old_vnet, vnr->new_vnet);
660203483Szec}
661203483Szec
662203483Szecvoid
663203483Szecvnet_log_recursion(struct vnet *old_vnet, const char *old_fn, int line)
664203483Szec{
665203483Szec	struct vnet_recursion *vnr;
666203483Szec
667203483Szec	/* Skip already logged recursion events. */
668203483Szec	SLIST_FOREACH(vnr, &vnet_recursions, vnr_le)
669203483Szec		if (vnr->prev_fn == old_fn &&
670203483Szec		    vnr->where_fn == curthread->td_vnet_lpush &&
671203483Szec		    vnr->where_line == line &&
672203483Szec		    (vnr->old_vnet == vnr->new_vnet) == (curvnet == old_vnet))
673203483Szec			return;
674203483Szec
675203483Szec	vnr = malloc(sizeof(*vnr), M_VNET, M_NOWAIT | M_ZERO);
676203483Szec	if (vnr == NULL)
677203483Szec		panic("%s: malloc failed", __func__);
678203483Szec	vnr->prev_fn = old_fn;
679203483Szec	vnr->where_fn = curthread->td_vnet_lpush;
680203483Szec	vnr->where_line = line;
681203483Szec	vnr->old_vnet = old_vnet;
682203483Szec	vnr->new_vnet = curvnet;
683203483Szec
684203483Szec	SLIST_INSERT_HEAD(&vnet_recursions, vnr, vnr_le);
685203483Szec
686203483Szec	vnet_print_recursion(vnr, 0);
687203483Szec#ifdef KDB
688203483Szec	kdb_backtrace();
689203483Szec#endif
690203483Szec}
691203483Szec#endif /* VNET_DEBUG */
692203483Szec
693205345Sbz/*
694205345Sbz * DDB(4).
695205345Sbz */
696196019Srwatson#ifdef DDB
697300148Sbzstatic void
698300148Sbzdb_vnet_print(struct vnet *vnet)
699196019Srwatson{
700300148Sbz
701300148Sbz	db_printf("vnet            = %p\n", vnet);
702300148Sbz	db_printf(" vnet_magic_n   = %#08x (%s, orig %#08x)\n",
703300148Sbz	    vnet->vnet_magic_n,
704300148Sbz	    (vnet->vnet_magic_n == VNET_MAGIC_N) ?
705300148Sbz		"ok" : "mismatch", VNET_MAGIC_N);
706300148Sbz	db_printf(" vnet_ifcnt     = %u\n", vnet->vnet_ifcnt);
707300148Sbz	db_printf(" vnet_sockcnt   = %u\n", vnet->vnet_sockcnt);
708300148Sbz	db_printf(" vnet_data_mem  = %p\n", vnet->vnet_data_mem);
709300148Sbz	db_printf(" vnet_data_base = %#jx\n",
710300148Sbz	    (uintmax_t)vnet->vnet_data_base);
711300155Sbz	db_printf(" vnet_state     = %#08x\n", vnet->vnet_state);
712300148Sbz	db_printf("\n");
713300148Sbz}
714300148Sbz
715300148SbzDB_SHOW_ALL_COMMAND(vnets, db_show_all_vnets)
716300148Sbz{
717196019Srwatson	VNET_ITERATOR_DECL(vnet_iter);
718196019Srwatson
719196019Srwatson	VNET_FOREACH(vnet_iter) {
720300148Sbz		db_vnet_print(vnet_iter);
721196129Sbz		if (db_pager_quit)
722196129Sbz			break;
723196019Srwatson	}
724196019Srwatson}
725203483Szec
726300148SbzDB_SHOW_COMMAND(vnet, db_show_vnet)
727300148Sbz{
728300148Sbz
729300148Sbz	if (!have_addr) {
730300148Sbz		db_printf("usage: show vnet <struct vnet *>\n");
731300148Sbz		return;
732300148Sbz	}
733300148Sbz
734300148Sbz	db_vnet_print((struct vnet *)addr);
735300148Sbz}
736300148Sbz
737203729Sbzstatic void
738203729Sbzdb_show_vnet_print_vs(struct vnet_sysinit *vs, int ddb)
739203729Sbz{
740203729Sbz	const char *vsname, *funcname;
741203729Sbz	c_db_sym_t sym;
742203729Sbz	db_expr_t  offset;
743203729Sbz
744203729Sbz#define xprint(...)							\
745203729Sbz	if (ddb)							\
746203729Sbz		db_printf(__VA_ARGS__);					\
747203729Sbz	else								\
748203729Sbz		printf(__VA_ARGS__)
749203729Sbz
750203729Sbz	if (vs == NULL) {
751203729Sbz		xprint("%s: no vnet_sysinit * given\n", __func__);
752203729Sbz		return;
753203729Sbz	}
754203729Sbz
755203729Sbz	sym = db_search_symbol((vm_offset_t)vs, DB_STGY_ANY, &offset);
756203729Sbz	db_symbol_values(sym, &vsname, NULL);
757203729Sbz	sym = db_search_symbol((vm_offset_t)vs->func, DB_STGY_PROC, &offset);
758203729Sbz	db_symbol_values(sym, &funcname, NULL);
759203729Sbz	xprint("%s(%p)\n", (vsname != NULL) ? vsname : "", vs);
760300148Sbz	xprint("  %#08x %#08x\n", vs->subsystem, vs->order);
761203729Sbz	xprint("  %p(%s)(%p)\n",
762203729Sbz	    vs->func, (funcname != NULL) ? funcname : "", vs->arg);
763203729Sbz#undef xprint
764203729Sbz}
765203729Sbz
766203729SbzDB_SHOW_COMMAND(vnet_sysinit, db_show_vnet_sysinit)
767203729Sbz{
768203729Sbz	struct vnet_sysinit *vs;
769203729Sbz
770203729Sbz	db_printf("VNET_SYSINIT vs Name(Ptr)\n");
771203729Sbz	db_printf("  Subsystem  Order\n");
772203729Sbz	db_printf("  Function(Name)(Arg)\n");
773203729Sbz	TAILQ_FOREACH(vs, &vnet_constructors, link) {
774203729Sbz		db_show_vnet_print_vs(vs, 1);
775203729Sbz		if (db_pager_quit)
776203729Sbz			break;
777203729Sbz	}
778203729Sbz}
779203729Sbz
780203729SbzDB_SHOW_COMMAND(vnet_sysuninit, db_show_vnet_sysuninit)
781203729Sbz{
782203729Sbz	struct vnet_sysinit *vs;
783203729Sbz
784203729Sbz	db_printf("VNET_SYSUNINIT vs Name(Ptr)\n");
785203729Sbz	db_printf("  Subsystem  Order\n");
786203729Sbz	db_printf("  Function(Name)(Arg)\n");
787203729Sbz	TAILQ_FOREACH_REVERSE(vs, &vnet_destructors, vnet_sysuninit_head,
788203729Sbz	    link) {
789203729Sbz		db_show_vnet_print_vs(vs, 1);
790203729Sbz		if (db_pager_quit)
791203729Sbz			break;
792203729Sbz	}
793203729Sbz}
794203729Sbz
795203483Szec#ifdef VNET_DEBUG
796203483SzecDB_SHOW_COMMAND(vnetrcrs, db_show_vnetrcrs)
797203483Szec{
798203483Szec	struct vnet_recursion *vnr;
799203483Szec
800203483Szec	SLIST_FOREACH(vnr, &vnet_recursions, vnr_le)
801203483Szec		vnet_print_recursion(vnr, 1);
802203483Szec}
803196019Srwatson#endif
804203483Szec#endif /* DDB */
805