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