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