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 <[email protected]>
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/systm.h>
48 #include <sys/sysctl.h>
49 #include <sys/eventhandler.h>
50 #include <sys/lock.h>
51 #include <sys/malloc.h>
52 #include <sys/proc.h>
53 #include <sys/socket.h>
54 #include <sys/sx.h>
55 #include <sys/sysctl.h>
56
57 #include <machine/stdarg.h>
58
59 #ifdef DDB
60 #include <ddb/ddb.h>
61 #include <ddb/db_sym.h>
62 #endif
63
64 #include <net/if.h>
65 #include <net/if_var.h>
66 #include <net/vnet.h>
67
68 /*-
69 * This file implements core functions for virtual network stacks:
70 *
71 * - Virtual network stack management functions.
72 *
73 * - Virtual network stack memory allocator, which virtualizes global
74 * variables in the network stack
75 *
76 * - Virtualized SYSINIT's/SYSUNINIT's, which allow network stack subsystems
77 * to register startup/shutdown events to be run for each virtual network
78 * stack instance.
79 */
80
81 FEATURE(vimage, "VIMAGE kernel virtualization");
82
83 static 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 static 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 (8 * PAGE_SIZE)
173 #define VNET_SIZE roundup2(VNET_BYTES, PAGE_SIZE)
174
175 /*
176 * Space to store virtualized global variables from loadable kernel modules,
177 * and the free list to manage it.
178 */
179 VNET_DEFINE_STATIC(char, modspace[VNET_MODMIN] __aligned(__alignof(void *)));
180
181 /*
182 * Global lists of subsystem constructor and destructors for vnets. They are
183 * registered via VNET_SYSINIT() and VNET_SYSUNINIT(). Both lists are
184 * protected by the vnet_sysinit_sxlock global lock.
185 */
186 static TAILQ_HEAD(vnet_sysinit_head, vnet_sysinit) vnet_constructors =
187 TAILQ_HEAD_INITIALIZER(vnet_constructors);
188 static TAILQ_HEAD(vnet_sysuninit_head, vnet_sysinit) vnet_destructors =
189 TAILQ_HEAD_INITIALIZER(vnet_destructors);
190
191 struct sx vnet_sysinit_sxlock;
192
193 #define VNET_SYSINIT_WLOCK() sx_xlock(&vnet_sysinit_sxlock);
194 #define VNET_SYSINIT_WUNLOCK() sx_xunlock(&vnet_sysinit_sxlock);
195 #define VNET_SYSINIT_RLOCK() sx_slock(&vnet_sysinit_sxlock);
196 #define VNET_SYSINIT_RUNLOCK() sx_sunlock(&vnet_sysinit_sxlock);
197
198 struct vnet_data_free {
199 uintptr_t vnd_start;
200 int vnd_len;
201 TAILQ_ENTRY(vnet_data_free) vnd_link;
202 };
203
204 static MALLOC_DEFINE(M_VNET_DATA_FREE, "vnet_data_free",
205 "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",
213 "struct vnet *");
214 SDT_PROBE_DEFINE2(vnet, functions, vnet_alloc, return,
215 "int", "struct vnet *");
216 SDT_PROBE_DEFINE2(vnet, functions, vnet_destroy, entry,
217 "int", "struct vnet *");
218 SDT_PROBE_DEFINE1(vnet, functions, vnet_destroy, return,
219 "int");
220
221 /*
222 * Run per-vnet sysinits or sysuninits during vnet creation/destruction.
223 */
224 static void vnet_sysinit(void);
225 static void vnet_sysuninit(void);
226
227 #ifdef DDB
228 static void db_show_vnet_print_vs(struct vnet_sysinit *, int);
229 #endif
230
231 /*
232 * Allocate a virtual network stack.
233 */
234 struct vnet *
vnet_alloc(void)235 vnet_alloc(void)
236 {
237 struct vnet *vnet;
238
239 SDT_PROBE1(vnet, functions, vnet_alloc, entry, __LINE__);
240 vnet = malloc(sizeof(struct vnet), M_VNET, M_WAITOK | M_ZERO);
241 vnet->vnet_magic_n = VNET_MAGIC_N;
242 SDT_PROBE2(vnet, functions, vnet_alloc, alloc, __LINE__, vnet);
243
244 /*
245 * Allocate storage for virtualized global variables and copy in
246 * initial values from our 'master' copy.
247 */
248 vnet->vnet_data_mem = malloc(VNET_SIZE, M_VNET_DATA, M_WAITOK);
249 memcpy(vnet->vnet_data_mem, (void *)VNET_START, VNET_BYTES);
250
251 /*
252 * All use of vnet-specific data will immediately subtract VNET_START
253 * from the base memory pointer, so pre-calculate that now to avoid
254 * it on each use.
255 */
256 vnet->vnet_data_base = (uintptr_t)vnet->vnet_data_mem - VNET_START;
257
258 /* Initialize / attach vnet module instances. */
259 CURVNET_SET_QUIET(vnet);
260 vnet_sysinit();
261 CURVNET_RESTORE();
262
263 VNET_LIST_WLOCK();
264 LIST_INSERT_HEAD(&vnet_head, vnet, vnet_le);
265 VNET_LIST_WUNLOCK();
266
267 SDT_PROBE2(vnet, functions, vnet_alloc, return, __LINE__, vnet);
268 return (vnet);
269 }
270
271 /*
272 * Destroy a virtual network stack.
273 */
274 void
vnet_destroy(struct vnet * vnet)275 vnet_destroy(struct vnet *vnet)
276 {
277
278 SDT_PROBE2(vnet, functions, vnet_destroy, entry, __LINE__, vnet);
279 KASSERT(vnet->vnet_sockcnt == 0,
280 ("%s: vnet still has sockets", __func__));
281
282 VNET_LIST_WLOCK();
283 LIST_REMOVE(vnet, vnet_le);
284 VNET_LIST_WUNLOCK();
285
286 /* Signal that VNET is being shutdown. */
287 vnet->vnet_shutdown = true;
288
289 CURVNET_SET_QUIET(vnet);
290 sx_xlock(&ifnet_detach_sxlock);
291 vnet_sysuninit();
292 sx_xunlock(&ifnet_detach_sxlock);
293 CURVNET_RESTORE();
294
295 /*
296 * Release storage for the virtual network stack instance.
297 */
298 free(vnet->vnet_data_mem, M_VNET_DATA);
299 vnet->vnet_data_mem = NULL;
300 vnet->vnet_data_base = 0;
301 vnet->vnet_magic_n = 0xdeadbeef;
302 free(vnet, M_VNET);
303 SDT_PROBE1(vnet, functions, vnet_destroy, return, __LINE__);
304 }
305
306 /*
307 * Boot time initialization and allocation of virtual network stacks.
308 */
309 static void
vnet_init_prelink(void * arg __unused)310 vnet_init_prelink(void *arg __unused)
311 {
312
313 rw_init(&vnet_rwlock, "vnet_rwlock");
314 sx_init(&vnet_sxlock, "vnet_sxlock");
315 sx_init(&vnet_sysinit_sxlock, "vnet_sysinit_sxlock");
316 LIST_INIT(&vnet_head);
317 }
318 SYSINIT(vnet_init_prelink, SI_SUB_VNET_PRELINK, SI_ORDER_FIRST,
319 vnet_init_prelink, NULL);
320
321 static void
vnet0_init(void * arg __unused)322 vnet0_init(void *arg __unused)
323 {
324
325 if (bootverbose)
326 printf("VIMAGE (virtualized network stack) enabled\n");
327
328 /*
329 * We MUST clear curvnet in vi_init_done() before going SMP,
330 * otherwise CURVNET_SET() macros would scream about unnecessary
331 * curvnet recursions.
332 */
333 curvnet = prison0.pr_vnet = vnet0 = vnet_alloc();
334 }
335 SYSINIT(vnet0_init, SI_SUB_VNET, SI_ORDER_FIRST, vnet0_init, NULL);
336
337 static void
vnet_init_done(void * unused __unused)338 vnet_init_done(void *unused __unused)
339 {
340
341 curvnet = NULL;
342 }
343 SYSINIT(vnet_init_done, SI_SUB_VNET_DONE, SI_ORDER_ANY, vnet_init_done,
344 NULL);
345
346 /*
347 * Once on boot, initialize the modspace freelist to entirely cover modspace.
348 */
349 static void
vnet_data_startup(void * dummy __unused)350 vnet_data_startup(void *dummy __unused)
351 {
352 struct vnet_data_free *df;
353
354 df = malloc(sizeof(*df), M_VNET_DATA_FREE, M_WAITOK | M_ZERO);
355 df->vnd_start = (uintptr_t)&VNET_NAME(modspace);
356 df->vnd_len = VNET_MODMIN;
357 TAILQ_INSERT_HEAD(&vnet_data_free_head, df, vnd_link);
358 sx_init(&vnet_data_free_lock, "vnet_data alloc lock");
359 }
360 SYSINIT(vnet_data, SI_SUB_KLD, SI_ORDER_FIRST, vnet_data_startup, NULL);
361
362 /* Dummy VNET_SYSINIT to make sure we always reach the final end state. */
363 static void
vnet_sysinit_done(void * unused __unused)364 vnet_sysinit_done(void *unused __unused)
365 {
366
367 return;
368 }
369 VNET_SYSINIT(vnet_sysinit_done, SI_SUB_VNET_DONE, SI_ORDER_ANY,
370 vnet_sysinit_done, NULL);
371
372 /*
373 * When a module is loaded and requires storage for a virtualized global
374 * variable, allocate space from the modspace free list. This interface
375 * should be used only by the kernel linker.
376 */
377 void *
vnet_data_alloc(int size)378 vnet_data_alloc(int size)
379 {
380 struct vnet_data_free *df;
381 void *s;
382
383 s = NULL;
384 size = roundup2(size, sizeof(void *));
385 sx_xlock(&vnet_data_free_lock);
386 TAILQ_FOREACH(df, &vnet_data_free_head, vnd_link) {
387 if (df->vnd_len < size)
388 continue;
389 if (df->vnd_len == size) {
390 s = (void *)df->vnd_start;
391 TAILQ_REMOVE(&vnet_data_free_head, df, vnd_link);
392 free(df, M_VNET_DATA_FREE);
393 break;
394 }
395 s = (void *)df->vnd_start;
396 df->vnd_len -= size;
397 df->vnd_start = df->vnd_start + size;
398 break;
399 }
400 sx_xunlock(&vnet_data_free_lock);
401
402 return (s);
403 }
404
405 /*
406 * Free space for a virtualized global variable on module unload.
407 */
408 void
vnet_data_free(void * start_arg,int size)409 vnet_data_free(void *start_arg, int size)
410 {
411 struct vnet_data_free *df;
412 struct vnet_data_free *dn;
413 uintptr_t start;
414 uintptr_t end;
415
416 size = roundup2(size, sizeof(void *));
417 start = (uintptr_t)start_arg;
418 end = start + size;
419 /*
420 * Free a region of space and merge it with as many neighbors as
421 * possible. Keeping the list sorted simplifies this operation.
422 */
423 sx_xlock(&vnet_data_free_lock);
424 TAILQ_FOREACH(df, &vnet_data_free_head, vnd_link) {
425 if (df->vnd_start > end)
426 break;
427 /*
428 * If we expand at the end of an entry we may have to merge
429 * it with the one following it as well.
430 */
431 if (df->vnd_start + df->vnd_len == start) {
432 df->vnd_len += size;
433 dn = TAILQ_NEXT(df, vnd_link);
434 if (df->vnd_start + df->vnd_len == dn->vnd_start) {
435 df->vnd_len += dn->vnd_len;
436 TAILQ_REMOVE(&vnet_data_free_head, dn,
437 vnd_link);
438 free(dn, M_VNET_DATA_FREE);
439 }
440 sx_xunlock(&vnet_data_free_lock);
441 return;
442 }
443 if (df->vnd_start == end) {
444 df->vnd_start = start;
445 df->vnd_len += size;
446 sx_xunlock(&vnet_data_free_lock);
447 return;
448 }
449 }
450 dn = malloc(sizeof(*df), M_VNET_DATA_FREE, M_WAITOK | M_ZERO);
451 dn->vnd_start = start;
452 dn->vnd_len = size;
453 if (df)
454 TAILQ_INSERT_BEFORE(df, dn, vnd_link);
455 else
456 TAILQ_INSERT_TAIL(&vnet_data_free_head, dn, vnd_link);
457 sx_xunlock(&vnet_data_free_lock);
458 }
459
460 /*
461 * When a new virtualized global variable has been allocated, propagate its
462 * initial value to each already-allocated virtual network stack instance.
463 */
464 void
vnet_data_copy(void * start,int size)465 vnet_data_copy(void *start, int size)
466 {
467 struct vnet *vnet;
468
469 VNET_LIST_RLOCK();
470 LIST_FOREACH(vnet, &vnet_head, vnet_le)
471 memcpy((void *)((uintptr_t)vnet->vnet_data_base +
472 (uintptr_t)start), start, size);
473 VNET_LIST_RUNLOCK();
474 }
475
476 /*
477 * Support for special SYSINIT handlers registered via VNET_SYSINIT()
478 * and VNET_SYSUNINIT().
479 */
480 void
vnet_register_sysinit(void * arg)481 vnet_register_sysinit(void *arg)
482 {
483 struct vnet_sysinit *vs, *vs2;
484 struct vnet *vnet;
485
486 vs = arg;
487 KASSERT(vs->subsystem > SI_SUB_VNET, ("vnet sysinit too early"));
488
489 /* Add the constructor to the global list of vnet constructors. */
490 VNET_SYSINIT_WLOCK();
491 TAILQ_FOREACH(vs2, &vnet_constructors, link) {
492 if (vs2->subsystem > vs->subsystem)
493 break;
494 if (vs2->subsystem == vs->subsystem && vs2->order > vs->order)
495 break;
496 }
497 if (vs2 != NULL)
498 TAILQ_INSERT_BEFORE(vs2, vs, link);
499 else
500 TAILQ_INSERT_TAIL(&vnet_constructors, vs, link);
501
502 /*
503 * Invoke the constructor on all the existing vnets when it is
504 * registered.
505 */
506 VNET_LIST_RLOCK();
507 VNET_FOREACH(vnet) {
508 CURVNET_SET_QUIET(vnet);
509 vs->func(vs->arg);
510 CURVNET_RESTORE();
511 }
512 VNET_LIST_RUNLOCK();
513 VNET_SYSINIT_WUNLOCK();
514 }
515
516 void
vnet_deregister_sysinit(void * arg)517 vnet_deregister_sysinit(void *arg)
518 {
519 struct vnet_sysinit *vs;
520
521 vs = arg;
522
523 /* Remove the constructor from the global list of vnet constructors. */
524 VNET_SYSINIT_WLOCK();
525 TAILQ_REMOVE(&vnet_constructors, vs, link);
526 VNET_SYSINIT_WUNLOCK();
527 }
528
529 void
vnet_register_sysuninit(void * arg)530 vnet_register_sysuninit(void *arg)
531 {
532 struct vnet_sysinit *vs, *vs2;
533
534 vs = arg;
535
536 /* Add the destructor to the global list of vnet destructors. */
537 VNET_SYSINIT_WLOCK();
538 TAILQ_FOREACH(vs2, &vnet_destructors, link) {
539 if (vs2->subsystem > vs->subsystem)
540 break;
541 if (vs2->subsystem == vs->subsystem && vs2->order > vs->order)
542 break;
543 }
544 if (vs2 != NULL)
545 TAILQ_INSERT_BEFORE(vs2, vs, link);
546 else
547 TAILQ_INSERT_TAIL(&vnet_destructors, vs, link);
548 VNET_SYSINIT_WUNLOCK();
549 }
550
551 void
vnet_deregister_sysuninit(void * arg)552 vnet_deregister_sysuninit(void *arg)
553 {
554 struct vnet_sysinit *vs;
555 struct vnet *vnet;
556
557 vs = arg;
558
559 /*
560 * Invoke the destructor on all the existing vnets when it is
561 * deregistered.
562 */
563 VNET_SYSINIT_WLOCK();
564 VNET_LIST_RLOCK();
565 VNET_FOREACH(vnet) {
566 CURVNET_SET_QUIET(vnet);
567 vs->func(vs->arg);
568 CURVNET_RESTORE();
569 }
570
571 /* Remove the destructor from the global list of vnet destructors. */
572 TAILQ_REMOVE(&vnet_destructors, vs, link);
573 VNET_SYSINIT_WUNLOCK();
574 VNET_LIST_RUNLOCK();
575 }
576
577 /*
578 * Invoke all registered vnet constructors on the current vnet. Used during
579 * vnet construction. The caller is responsible for ensuring the new vnet is
580 * the current vnet and that the vnet_sysinit_sxlock lock is locked.
581 */
582 static void
vnet_sysinit(void)583 vnet_sysinit(void)
584 {
585 struct vnet_sysinit *vs;
586
587 VNET_SYSINIT_RLOCK();
588 TAILQ_FOREACH(vs, &vnet_constructors, link) {
589 curvnet->vnet_state = vs->subsystem;
590 vs->func(vs->arg);
591 }
592 VNET_SYSINIT_RUNLOCK();
593 }
594
595 /*
596 * Invoke all registered vnet destructors on the current vnet. Used during
597 * vnet destruction. The caller is responsible for ensuring the dying vnet
598 * the current vnet and that the vnet_sysinit_sxlock lock is locked.
599 */
600 static void
vnet_sysuninit(void)601 vnet_sysuninit(void)
602 {
603 struct vnet_sysinit *vs;
604
605 VNET_SYSINIT_RLOCK();
606 TAILQ_FOREACH_REVERSE(vs, &vnet_destructors, vnet_sysuninit_head,
607 link) {
608 curvnet->vnet_state = vs->subsystem;
609 vs->func(vs->arg);
610 }
611 VNET_SYSINIT_RUNLOCK();
612 }
613
614 /*
615 * EVENTHANDLER(9) extensions.
616 */
617 /*
618 * Invoke the eventhandler function originally registered with the possibly
619 * registered argument for all virtual network stack instances.
620 *
621 * This iterator can only be used for eventhandlers that do not take any
622 * additional arguments, as we do ignore the variadic arguments from the
623 * EVENTHANDLER_INVOKE() call.
624 */
625 void
vnet_global_eventhandler_iterator_func(void * arg,...)626 vnet_global_eventhandler_iterator_func(void *arg, ...)
627 {
628 VNET_ITERATOR_DECL(vnet_iter);
629 struct eventhandler_entry_vimage *v_ee;
630
631 /*
632 * There is a bug here in that we should actually cast things to
633 * (struct eventhandler_entry_ ## name *) but that's not easily
634 * possible in here so just re-using the variadic version we
635 * defined for the generic vimage case.
636 */
637 v_ee = arg;
638 VNET_LIST_RLOCK();
639 VNET_FOREACH(vnet_iter) {
640 CURVNET_SET(vnet_iter);
641 ((vimage_iterator_func_t)v_ee->func)(v_ee->ee_arg);
642 CURVNET_RESTORE();
643 }
644 VNET_LIST_RUNLOCK();
645 }
646
647 #ifdef VNET_DEBUG
648 struct vnet_recursion {
649 SLIST_ENTRY(vnet_recursion) vnr_le;
650 const char *prev_fn;
651 const char *where_fn;
652 int where_line;
653 struct vnet *old_vnet;
654 struct vnet *new_vnet;
655 };
656
657 static SLIST_HEAD(, vnet_recursion) vnet_recursions =
658 SLIST_HEAD_INITIALIZER(vnet_recursions);
659
660 static void
vnet_print_recursion(struct vnet_recursion * vnr,int brief)661 vnet_print_recursion(struct vnet_recursion *vnr, int brief)
662 {
663
664 if (!brief)
665 printf("CURVNET_SET() recursion in ");
666 printf("%s() line %d, prev in %s()", vnr->where_fn, vnr->where_line,
667 vnr->prev_fn);
668 if (brief)
669 printf(", ");
670 else
671 printf("\n ");
672 printf("%p -> %p\n", vnr->old_vnet, vnr->new_vnet);
673 }
674
675 void
vnet_log_recursion(struct vnet * old_vnet,const char * old_fn,int line)676 vnet_log_recursion(struct vnet *old_vnet, const char *old_fn, int line)
677 {
678 struct vnet_recursion *vnr;
679
680 /* Skip already logged recursion events. */
681 SLIST_FOREACH(vnr, &vnet_recursions, vnr_le)
682 if (vnr->prev_fn == old_fn &&
683 vnr->where_fn == curthread->td_vnet_lpush &&
684 vnr->where_line == line &&
685 (vnr->old_vnet == vnr->new_vnet) == (curvnet == old_vnet))
686 return;
687
688 vnr = malloc(sizeof(*vnr), M_VNET, M_NOWAIT | M_ZERO);
689 if (vnr == NULL)
690 panic("%s: malloc failed", __func__);
691 vnr->prev_fn = old_fn;
692 vnr->where_fn = curthread->td_vnet_lpush;
693 vnr->where_line = line;
694 vnr->old_vnet = old_vnet;
695 vnr->new_vnet = curvnet;
696
697 SLIST_INSERT_HEAD(&vnet_recursions, vnr, vnr_le);
698
699 vnet_print_recursion(vnr, 0);
700 #ifdef KDB
701 kdb_backtrace();
702 #endif
703 }
704 #endif /* VNET_DEBUG */
705
706 /*
707 * DDB(4).
708 */
709 #ifdef DDB
710 static void
db_vnet_print(struct vnet * vnet)711 db_vnet_print(struct vnet *vnet)
712 {
713
714 db_printf("vnet = %p\n", vnet);
715 db_printf(" vnet_magic_n = %#08x (%s, orig %#08x)\n",
716 vnet->vnet_magic_n,
717 (vnet->vnet_magic_n == VNET_MAGIC_N) ?
718 "ok" : "mismatch", VNET_MAGIC_N);
719 db_printf(" vnet_ifcnt = %u\n", vnet->vnet_ifcnt);
720 db_printf(" vnet_sockcnt = %u\n", vnet->vnet_sockcnt);
721 db_printf(" vnet_data_mem = %p\n", vnet->vnet_data_mem);
722 db_printf(" vnet_data_base = %#jx\n",
723 (uintmax_t)vnet->vnet_data_base);
724 db_printf(" vnet_state = %#08x\n", vnet->vnet_state);
725 db_printf(" vnet_shutdown = %#03x\n", vnet->vnet_shutdown);
726 db_printf("\n");
727 }
728
DB_SHOW_ALL_COMMAND(vnets,db_show_all_vnets)729 DB_SHOW_ALL_COMMAND(vnets, db_show_all_vnets)
730 {
731 VNET_ITERATOR_DECL(vnet_iter);
732
733 VNET_FOREACH(vnet_iter) {
734 db_vnet_print(vnet_iter);
735 if (db_pager_quit)
736 break;
737 }
738 }
739
DB_SHOW_COMMAND(vnet,db_show_vnet)740 DB_SHOW_COMMAND(vnet, db_show_vnet)
741 {
742
743 if (!have_addr) {
744 db_printf("usage: show vnet <struct vnet *>\n");
745 return;
746 }
747
748 db_vnet_print((struct vnet *)addr);
749 }
750
751 static void
db_show_vnet_print_vs(struct vnet_sysinit * vs,int ddb)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(" %#08x %#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
DB_SHOW_COMMAND_FLAGS(vnet_sysinit,db_show_vnet_sysinit,DB_CMD_MEMSAFE)780 DB_SHOW_COMMAND_FLAGS(vnet_sysinit, db_show_vnet_sysinit, DB_CMD_MEMSAFE)
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
DB_SHOW_COMMAND_FLAGS(vnet_sysuninit,db_show_vnet_sysuninit,DB_CMD_MEMSAFE)794 DB_SHOW_COMMAND_FLAGS(vnet_sysuninit, db_show_vnet_sysuninit, DB_CMD_MEMSAFE)
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
DB_SHOW_COMMAND_FLAGS(vnetrcrs,db_show_vnetrcrs,DB_CMD_MEMSAFE)810 DB_SHOW_COMMAND_FLAGS(vnetrcrs, db_show_vnetrcrs, DB_CMD_MEMSAFE)
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 */
819