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