]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - sys/net/vnet.c
Merge r203913 from head to stable/8 (original change by pjd):
[FreeBSD/stable/8.git] / sys / net / vnet.c
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/linker_set.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 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 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     8192
175 #define VNET_SIZE       roundup2(VNET_BYTES, PAGE_SIZE)
176 #define VNET_MODSIZE    (VNET_SIZE - (VNET_BYTES - VNET_MODMIN))
177
178 /*
179  * Space to store virtualized global variables from loadable kernel modules,
180  * and the free list to manage it.
181  */
182 static VNET_DEFINE(char, modspace[VNET_MODMIN]);
183
184 /*
185  * Global lists of subsystem constructor and destructors for vnets.  They are
186  * registered via VNET_SYSINIT() and VNET_SYSUNINIT().  Both lists are
187  * protected by the vnet_sysinit_sxlock global lock.
188  */
189 static TAILQ_HEAD(vnet_sysinit_head, vnet_sysinit) vnet_constructors =
190         TAILQ_HEAD_INITIALIZER(vnet_constructors);
191 static TAILQ_HEAD(vnet_sysuninit_head, vnet_sysinit) vnet_destructors =
192         TAILQ_HEAD_INITIALIZER(vnet_destructors);
193
194 struct sx               vnet_sysinit_sxlock;
195
196 #define VNET_SYSINIT_WLOCK()    sx_xlock(&vnet_sysinit_sxlock);
197 #define VNET_SYSINIT_WUNLOCK()  sx_xunlock(&vnet_sysinit_sxlock);
198 #define VNET_SYSINIT_RLOCK()    sx_slock(&vnet_sysinit_sxlock);
199 #define VNET_SYSINIT_RUNLOCK()  sx_sunlock(&vnet_sysinit_sxlock);
200
201 struct vnet_data_free {
202         uintptr_t       vnd_start;
203         int             vnd_len;
204         TAILQ_ENTRY(vnet_data_free) vnd_link;
205 };
206
207 MALLOC_DEFINE(M_VNET_DATA_FREE, "vnet_data_free", "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", "struct vnet *");
215 SDT_PROBE_DEFINE2(vnet, functions, vnet_alloc, return, "int", "struct vnet *");
216 SDT_PROBE_DEFINE2(vnet, functions, vnet_destroy, entry, "int", "struct vnet *");
217 SDT_PROBE_DEFINE1(vnet, functions, vnet_destroy, return, "int");
218
219 #ifdef DDB
220 static void db_show_vnet_print_vs(struct vnet_sysinit *, int);
221 #endif
222
223 /*
224  * Allocate a virtual network stack.
225  */
226 struct vnet *
227 vnet_alloc(void)
228 {
229         struct vnet *vnet;
230
231         SDT_PROBE1(vnet, functions, vnet_alloc, entry, __LINE__);
232         vnet = malloc(sizeof(struct vnet), M_VNET, M_WAITOK | M_ZERO);
233         vnet->vnet_magic_n = VNET_MAGIC_N;
234         SDT_PROBE2(vnet, functions, vnet_alloc, alloc, __LINE__, vnet);
235
236         /*
237          * Allocate storage for virtualized global variables and copy in
238          * initial values form our 'master' copy.
239          */
240         vnet->vnet_data_mem = malloc(VNET_SIZE, M_VNET_DATA, M_WAITOK);
241         memcpy(vnet->vnet_data_mem, (void *)VNET_START, VNET_BYTES);
242
243         /*
244          * All use of vnet-specific data will immediately subtract VNET_START
245          * from the base memory pointer, so pre-calculate that now to avoid
246          * it on each use.
247          */
248         vnet->vnet_data_base = (uintptr_t)vnet->vnet_data_mem - VNET_START;
249
250         /* Initialize / attach vnet module instances. */
251         CURVNET_SET_QUIET(vnet);
252         vnet_sysinit();
253         CURVNET_RESTORE();
254
255         VNET_LIST_WLOCK();
256         LIST_INSERT_HEAD(&vnet_head, vnet, vnet_le);
257         VNET_LIST_WUNLOCK();
258
259         SDT_PROBE2(vnet, functions, vnet_alloc, return, __LINE__, vnet);
260         return (vnet);
261 }
262
263 /*
264  * Destroy a virtual network stack.
265  */
266 void
267 vnet_destroy(struct vnet *vnet)
268 {
269         struct ifnet *ifp, *nifp;
270
271         SDT_PROBE2(vnet, functions, vnet_destroy, entry, __LINE__, vnet);
272         KASSERT(vnet->vnet_sockcnt == 0,
273             ("%s: vnet still has sockets", __func__));
274
275         VNET_LIST_WLOCK();
276         LIST_REMOVE(vnet, vnet_le);
277         VNET_LIST_WUNLOCK();
278
279         CURVNET_SET_QUIET(vnet);
280
281         /* Return all inherited interfaces to their parent vnets. */
282         TAILQ_FOREACH_SAFE(ifp, &V_ifnet, if_link, nifp) {
283                 if (ifp->if_home_vnet != ifp->if_vnet)
284                         if_vmove(ifp, ifp->if_home_vnet);
285         }
286
287         vnet_sysuninit();
288         CURVNET_RESTORE();
289
290         /*
291          * Release storage for the virtual network stack instance.
292          */
293         free(vnet->vnet_data_mem, M_VNET_DATA);
294         vnet->vnet_data_mem = NULL;
295         vnet->vnet_data_base = 0;
296         vnet->vnet_magic_n = 0xdeadbeef;
297         free(vnet, M_VNET);
298         SDT_PROBE1(vnet, functions, vnet_destroy, return, __LINE__);
299 }
300
301 /*
302  * Boot time initialization and allocation of virtual network stacks.
303  */
304 static void
305 vnet_init_prelink(void *arg)
306 {
307
308         rw_init(&vnet_rwlock, "vnet_rwlock");
309         sx_init(&vnet_sxlock, "vnet_sxlock");
310         sx_init(&vnet_sysinit_sxlock, "vnet_sysinit_sxlock");
311         LIST_INIT(&vnet_head);
312 }
313 SYSINIT(vnet_init_prelink, SI_SUB_VNET_PRELINK, SI_ORDER_FIRST,
314     vnet_init_prelink, NULL);
315
316 static void
317 vnet0_init(void *arg)
318 {
319
320         /* Warn people before take off - in case we crash early. */
321         printf("WARNING: VIMAGE (virtualized network stack) is a highly "
322             "experimental feature.\n");
323
324         /*
325          * We MUST clear curvnet in vi_init_done() before going SMP,
326          * otherwise CURVNET_SET() macros would scream about unnecessary
327          * curvnet recursions.
328          */
329         curvnet = prison0.pr_vnet = vnet0 = vnet_alloc();
330 }
331 SYSINIT(vnet0_init, SI_SUB_VNET, SI_ORDER_FIRST, vnet0_init, NULL);
332
333 static void
334 vnet_init_done(void *unused)
335 {
336
337         curvnet = NULL;
338 }
339
340 SYSINIT(vnet_init_done, SI_SUB_VNET_DONE, SI_ORDER_FIRST, vnet_init_done,
341     NULL);
342
343 /*
344  * Once on boot, initialize the modspace freelist to entirely cover modspace.
345  */
346 static void
347 vnet_data_startup(void *dummy __unused)
348 {
349         struct vnet_data_free *df;
350
351         df = malloc(sizeof(*df), M_VNET_DATA_FREE, M_WAITOK | M_ZERO);
352         df->vnd_start = (uintptr_t)&VNET_NAME(modspace);
353         df->vnd_len = VNET_MODMIN;
354         TAILQ_INSERT_HEAD(&vnet_data_free_head, df, vnd_link);
355         sx_init(&vnet_data_free_lock, "vnet_data alloc lock");
356 }
357 SYSINIT(vnet_data, SI_SUB_KLD, SI_ORDER_FIRST, vnet_data_startup, 0);
358
359 /*
360  * When a module is loaded and requires storage for a virtualized global
361  * variable, allocate space from the modspace free list.  This interface
362  * should be used only by the kernel linker.
363  */
364 void *
365 vnet_data_alloc(int size)
366 {
367         struct vnet_data_free *df;
368         void *s;
369
370         s = NULL;
371         size = roundup2(size, sizeof(void *));
372         sx_xlock(&vnet_data_free_lock);
373         TAILQ_FOREACH(df, &vnet_data_free_head, vnd_link) {
374                 if (df->vnd_len < size)
375                         continue;
376                 if (df->vnd_len == size) {
377                         s = (void *)df->vnd_start;
378                         TAILQ_REMOVE(&vnet_data_free_head, df, vnd_link);
379                         free(df, M_VNET_DATA_FREE);
380                         break;
381                 }
382                 s = (void *)df->vnd_start;
383                 df->vnd_len -= size;
384                 df->vnd_start = df->vnd_start + size;
385                 break;
386         }
387         sx_xunlock(&vnet_data_free_lock);
388
389         return (s);
390 }
391
392 /*
393  * Free space for a virtualized global variable on module unload.
394  */
395 void
396 vnet_data_free(void *start_arg, int size)
397 {
398         struct vnet_data_free *df;
399         struct vnet_data_free *dn;
400         uintptr_t start;
401         uintptr_t end;
402
403         size = roundup2(size, sizeof(void *));
404         start = (uintptr_t)start_arg;
405         end = start + size;
406         /*
407          * Free a region of space and merge it with as many neighbors as
408          * possible.  Keeping the list sorted simplifies this operation.
409          */
410         sx_xlock(&vnet_data_free_lock);
411         TAILQ_FOREACH(df, &vnet_data_free_head, vnd_link) {
412                 if (df->vnd_start > end)
413                         break;
414                 /*
415                  * If we expand at the end of an entry we may have to merge
416                  * it with the one following it as well.
417                  */
418                 if (df->vnd_start + df->vnd_len == start) {
419                         df->vnd_len += size;
420                         dn = TAILQ_NEXT(df, vnd_link);
421                         if (df->vnd_start + df->vnd_len == dn->vnd_start) {
422                                 df->vnd_len += dn->vnd_len;
423                                 TAILQ_REMOVE(&vnet_data_free_head, dn,
424                                     vnd_link);
425                                 free(dn, M_VNET_DATA_FREE);
426                         }
427                         sx_xunlock(&vnet_data_free_lock);
428                         return;
429                 }
430                 if (df->vnd_start == end) {
431                         df->vnd_start = start;
432                         df->vnd_len += size;
433                         sx_xunlock(&vnet_data_free_lock);
434                         return;
435                 }
436         }
437         dn = malloc(sizeof(*df), M_VNET_DATA_FREE, M_WAITOK | M_ZERO);
438         dn->vnd_start = start;
439         dn->vnd_len = size;
440         if (df)
441                 TAILQ_INSERT_BEFORE(df, dn, vnd_link);
442         else
443                 TAILQ_INSERT_TAIL(&vnet_data_free_head, dn, vnd_link);
444         sx_xunlock(&vnet_data_free_lock);
445 }
446
447 /*
448  * When a new virtualized global variable has been allocated, propagate its
449  * initial value to each already-allocated virtual network stack instance.
450  */
451 void
452 vnet_data_copy(void *start, int size)
453 {
454         struct vnet *vnet;
455
456         VNET_LIST_RLOCK();
457         LIST_FOREACH(vnet, &vnet_head, vnet_le)
458                 memcpy((void *)((uintptr_t)vnet->vnet_data_base +
459                     (uintptr_t)start), start, size);
460         VNET_LIST_RUNLOCK();
461 }
462
463 /*
464  * Variants on sysctl_handle_foo that know how to handle virtualized global
465  * variables: if 'arg1' is a pointer, then we transform it to the local vnet
466  * offset.
467  */
468 int
469 vnet_sysctl_handle_int(SYSCTL_HANDLER_ARGS)
470 {
471
472         if (arg1 != NULL)
473                 arg1 = (void *)(curvnet->vnet_data_base + (uintptr_t)arg1);
474         return (sysctl_handle_int(oidp, arg1, arg2, req));
475 }
476
477 int
478 vnet_sysctl_handle_opaque(SYSCTL_HANDLER_ARGS)
479 {
480
481         if (arg1 != NULL)
482                 arg1 = (void *)(curvnet->vnet_data_base + (uintptr_t)arg1);
483         return (sysctl_handle_opaque(oidp, arg1, arg2, req));
484 }
485
486 int
487 vnet_sysctl_handle_string(SYSCTL_HANDLER_ARGS)
488 {
489
490         if (arg1 != NULL)
491                 arg1 = (void *)(curvnet->vnet_data_base + (uintptr_t)arg1);
492         return (sysctl_handle_string(oidp, arg1, arg2, req));
493 }
494
495 int
496 vnet_sysctl_handle_uint(SYSCTL_HANDLER_ARGS)
497 {
498
499         if (arg1 != NULL)
500                 arg1 = (void *)(curvnet->vnet_data_base + (uintptr_t)arg1);
501         return (sysctl_handle_int(oidp, arg1, arg2, req));
502 }
503
504 /*
505  * Support for special SYSINIT handlers registered via VNET_SYSINIT()
506  * and VNET_SYSUNINIT().
507  */
508 void
509 vnet_register_sysinit(void *arg)
510 {
511         struct vnet_sysinit *vs, *vs2;  
512         struct vnet *vnet;
513
514         vs = arg;
515         KASSERT(vs->subsystem > SI_SUB_VNET, ("vnet sysinit too early"));
516
517         /* Add the constructor to the global list of vnet constructors. */
518         VNET_SYSINIT_WLOCK();
519         TAILQ_FOREACH(vs2, &vnet_constructors, link) {
520                 if (vs2->subsystem > vs->subsystem)
521                         break;
522                 if (vs2->subsystem == vs->subsystem && vs2->order > vs->order)
523                         break;
524         }
525         if (vs2 != NULL)
526                 TAILQ_INSERT_BEFORE(vs2, vs, link);
527         else
528                 TAILQ_INSERT_TAIL(&vnet_constructors, vs, link);
529
530         /*
531          * Invoke the constructor on all the existing vnets when it is
532          * registered.
533          */
534         VNET_FOREACH(vnet) {
535                 CURVNET_SET_QUIET(vnet);
536                 vs->func(vs->arg);
537                 CURVNET_RESTORE();
538         }
539         VNET_SYSINIT_WUNLOCK();
540 }
541
542 void
543 vnet_deregister_sysinit(void *arg)
544 {
545         struct vnet_sysinit *vs;
546
547         vs = arg;
548
549         /* Remove the constructor from the global list of vnet constructors. */
550         VNET_SYSINIT_WLOCK();
551         TAILQ_REMOVE(&vnet_constructors, vs, link);
552         VNET_SYSINIT_WUNLOCK();
553 }
554
555 void
556 vnet_register_sysuninit(void *arg)
557 {
558         struct vnet_sysinit *vs, *vs2;
559
560         vs = arg;
561
562         /* Add the destructor to the global list of vnet destructors. */
563         VNET_SYSINIT_WLOCK();
564         TAILQ_FOREACH(vs2, &vnet_destructors, link) {
565                 if (vs2->subsystem > vs->subsystem)
566                         break;
567                 if (vs2->subsystem == vs->subsystem && vs2->order > vs->order)
568                         break;
569         }
570         if (vs2 != NULL)
571                 TAILQ_INSERT_BEFORE(vs2, vs, link);
572         else
573                 TAILQ_INSERT_TAIL(&vnet_destructors, vs, link);
574         VNET_SYSINIT_WUNLOCK();
575 }
576
577 void
578 vnet_deregister_sysuninit(void *arg)
579 {
580         struct vnet_sysinit *vs;
581         struct vnet *vnet;
582
583         vs = arg;
584
585         /*
586          * Invoke the destructor on all the existing vnets when it is
587          * deregistered.
588          */
589         VNET_SYSINIT_WLOCK();
590         VNET_FOREACH(vnet) {
591                 CURVNET_SET_QUIET(vnet);
592                 vs->func(vs->arg);
593                 CURVNET_RESTORE();
594         }
595
596         /* Remove the destructor from the global list of vnet destructors. */
597         TAILQ_REMOVE(&vnet_destructors, vs, link);
598         VNET_SYSINIT_WUNLOCK();
599 }
600
601 /*
602  * Invoke all registered vnet constructors on the current vnet.  Used during
603  * vnet construction.  The caller is responsible for ensuring the new vnet is
604  * the current vnet and that the vnet_sysinit_sxlock lock is locked.
605  */
606 void
607 vnet_sysinit(void)
608 {
609         struct vnet_sysinit *vs;
610
611         VNET_SYSINIT_RLOCK();
612         TAILQ_FOREACH(vs, &vnet_constructors, link) {
613                 vs->func(vs->arg);
614         }
615         VNET_SYSINIT_RUNLOCK();
616 }
617
618 /*
619  * Invoke all registered vnet destructors on the current vnet.  Used during
620  * vnet destruction.  The caller is responsible for ensuring the dying vnet
621  * the current vnet and that the vnet_sysinit_sxlock lock is locked.
622  */
623 void
624 vnet_sysuninit(void)
625 {
626         struct vnet_sysinit *vs;
627
628         VNET_SYSINIT_RLOCK();
629         TAILQ_FOREACH_REVERSE(vs, &vnet_destructors, vnet_sysuninit_head,
630             link) {
631                 vs->func(vs->arg);
632         }
633         VNET_SYSINIT_RUNLOCK();
634 }
635
636 /*
637  * EVENTHANDLER(9) extensions.
638  */
639 /*
640  * Invoke the eventhandler function originally registered with the possibly
641  * registered argument for all virtual network stack instances.
642  *
643  * This iterator can only be used for eventhandlers that do not take any
644  * additional arguments, as we do ignore the variadic arguments from the
645  * EVENTHANDLER_INVOKE() call.
646  */
647 void
648 vnet_global_eventhandler_iterator_func(void *arg, ...)
649 {
650         VNET_ITERATOR_DECL(vnet_iter);
651         struct eventhandler_entry_vimage *v_ee;
652
653         /*
654          * There is a bug here in that we should actually cast things to
655          * (struct eventhandler_entry_ ## name *)  but that's not easily
656          * possible in here so just re-using the variadic version we
657          * defined for the generic vimage case.
658          */
659         v_ee = arg;
660         VNET_LIST_RLOCK();
661         VNET_FOREACH(vnet_iter) {
662                 CURVNET_SET(vnet_iter);
663                 ((vimage_iterator_func_t)v_ee->func)(v_ee->ee_arg);
664                 CURVNET_RESTORE();
665         }
666         VNET_LIST_RUNLOCK();
667 }
668
669 #ifdef VNET_DEBUG
670 struct vnet_recursion {
671         SLIST_ENTRY(vnet_recursion)      vnr_le;
672         const char                      *prev_fn;
673         const char                      *where_fn;
674         int                              where_line;
675         struct vnet                     *old_vnet;
676         struct vnet                     *new_vnet;
677 };
678
679 static SLIST_HEAD(, vnet_recursion) vnet_recursions =
680     SLIST_HEAD_INITIALIZER(vnet_recursions);
681
682 static void
683 vnet_print_recursion(struct vnet_recursion *vnr, int brief)
684 {
685
686         if (!brief)
687                 printf("CURVNET_SET() recursion in ");
688         printf("%s() line %d, prev in %s()", vnr->where_fn, vnr->where_line,
689             vnr->prev_fn);
690         if (brief)
691                 printf(", ");
692         else
693                 printf("\n    ");
694         printf("%p -> %p\n", vnr->old_vnet, vnr->new_vnet);
695 }
696
697 void
698 vnet_log_recursion(struct vnet *old_vnet, const char *old_fn, int line)
699 {
700         struct vnet_recursion *vnr;
701
702         /* Skip already logged recursion events. */
703         SLIST_FOREACH(vnr, &vnet_recursions, vnr_le)
704                 if (vnr->prev_fn == old_fn &&
705                     vnr->where_fn == curthread->td_vnet_lpush &&
706                     vnr->where_line == line &&
707                     (vnr->old_vnet == vnr->new_vnet) == (curvnet == old_vnet))
708                         return;
709
710         vnr = malloc(sizeof(*vnr), M_VNET, M_NOWAIT | M_ZERO);
711         if (vnr == NULL)
712                 panic("%s: malloc failed", __func__);
713         vnr->prev_fn = old_fn;
714         vnr->where_fn = curthread->td_vnet_lpush;
715         vnr->where_line = line;
716         vnr->old_vnet = old_vnet;
717         vnr->new_vnet = curvnet;
718
719         SLIST_INSERT_HEAD(&vnet_recursions, vnr, vnr_le);
720
721         vnet_print_recursion(vnr, 0);
722 #ifdef KDB
723         kdb_backtrace();
724 #endif
725 }
726 #endif /* VNET_DEBUG */
727
728 /*
729  * DDB(4).
730  */
731 #ifdef DDB
732 DB_SHOW_COMMAND(vnets, db_show_vnets)
733 {
734         VNET_ITERATOR_DECL(vnet_iter);
735
736         VNET_FOREACH(vnet_iter) {
737                 db_printf("vnet            = %p\n", vnet_iter);
738                 db_printf(" vnet_magic_n   = 0x%x (%s, orig 0x%x)\n",
739                     vnet_iter->vnet_magic_n,
740                     (vnet_iter->vnet_magic_n == VNET_MAGIC_N) ?
741                         "ok" : "mismatch", VNET_MAGIC_N);
742                 db_printf(" vnet_ifcnt     = %u\n", vnet_iter->vnet_ifcnt);
743                 db_printf(" vnet_sockcnt   = %u\n", vnet_iter->vnet_sockcnt);
744                 db_printf(" vnet_data_mem  = %p\n", vnet_iter->vnet_data_mem);
745                 db_printf(" vnet_data_base = 0x%jx\n",
746                     (uintmax_t)vnet_iter->vnet_data_base);
747                 db_printf("\n");
748                 if (db_pager_quit)
749                         break;
750         }
751 }
752
753 static void
754 db_show_vnet_print_vs(struct vnet_sysinit *vs, int ddb)
755 {
756         const char *vsname, *funcname;
757         c_db_sym_t sym;
758         db_expr_t  offset;
759
760 #define xprint(...)                                                     \
761         if (ddb)                                                        \
762                 db_printf(__VA_ARGS__);                                 \
763         else                                                            \
764                 printf(__VA_ARGS__)
765
766         if (vs == NULL) {
767                 xprint("%s: no vnet_sysinit * given\n", __func__);
768                 return;
769         }
770
771         sym = db_search_symbol((vm_offset_t)vs, DB_STGY_ANY, &offset);
772         db_symbol_values(sym, &vsname, NULL);
773         sym = db_search_symbol((vm_offset_t)vs->func, DB_STGY_PROC, &offset);
774         db_symbol_values(sym, &funcname, NULL);
775         xprint("%s(%p)\n", (vsname != NULL) ? vsname : "", vs);
776         xprint("  0x%08x 0x%08x\n", vs->subsystem, vs->order);
777         xprint("  %p(%s)(%p)\n",
778             vs->func, (funcname != NULL) ? funcname : "", vs->arg);
779 #undef xprint
780 }
781
782 DB_SHOW_COMMAND(vnet_sysinit, db_show_vnet_sysinit)
783 {
784         struct vnet_sysinit *vs;
785
786         db_printf("VNET_SYSINIT vs Name(Ptr)\n");
787         db_printf("  Subsystem  Order\n");
788         db_printf("  Function(Name)(Arg)\n");
789         TAILQ_FOREACH(vs, &vnet_constructors, link) {
790                 db_show_vnet_print_vs(vs, 1);
791                 if (db_pager_quit)
792                         break;
793         }
794 }
795
796 DB_SHOW_COMMAND(vnet_sysuninit, db_show_vnet_sysuninit)
797 {
798         struct vnet_sysinit *vs;
799
800         db_printf("VNET_SYSUNINIT vs Name(Ptr)\n");
801         db_printf("  Subsystem  Order\n");
802         db_printf("  Function(Name)(Arg)\n");
803         TAILQ_FOREACH_REVERSE(vs, &vnet_destructors, vnet_sysuninit_head,
804             link) {
805                 db_show_vnet_print_vs(vs, 1);
806                 if (db_pager_quit)
807                         break;
808         }
809 }
810
811 #ifdef VNET_DEBUG
812 DB_SHOW_COMMAND(vnetrcrs, db_show_vnetrcrs)
813 {
814         struct vnet_recursion *vnr;
815
816         SLIST_FOREACH(vnr, &vnet_recursions, vnr_le)
817                 vnet_print_recursion(vnr, 1);
818 }
819 #endif
820 #endif /* DDB */