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