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