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