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