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