Index: head/share/man/man9/vnet.9 =================================================================== --- head/share/man/man9/vnet.9 (revision 336673) +++ head/share/man/man9/vnet.9 (revision 336674) @@ -1,511 +1,519 @@ .\"- .\" Copyright (c) 2010 The FreeBSD Foundation .\" All rights reserved. .\" .\" This documentation was written by CK Software GmbH under sponsorship from .\" the FreeBSD Foundation. .\" .\" Redistribution and use in source and binary forms, with or without .\" modification, are permitted provided that the following conditions .\" are met: .\" 1. Redistributions of source code must retain the above copyright .\" notice, this list of conditions and the following disclaimer. .\" 2. Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in the .\" documentation and/or other materials provided with the distribution. .\" .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE .\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" .\" $FreeBSD$ .\" .Dd May 16, 2018 .Dt VNET 9 .Os .Sh NAME .Nm VNET .Nd "network subsystem virtualization infrastructure" .Sh SYNOPSIS .Cd "options VIMAGE" .Cd "options VNET_DEBUG" .Pp .In sys/vnet.h .Pp .\"------------------------------------------------------------ .Ss "Constants and Global Variables" .\" .Dv VNET_SETNAME .\" "set_vnet" .Dv VNET_SYMPREFIX .\" "vnet_entry_" .Vt extern struct vnet *vnet0; .\"------------------------------------------------------------ .Ss "Variable Declaration" .Fo VNET .Fa "name" .Fc .\" .Fo VNET_NAME .Fa "name" .Fc .\" .Fo VNET_DECLARE .Fa "type" "name" .Fc .\" .Fo VNET_DEFINE .Fa "type" "name" .Fc .\" +.Fo VNET_DEFINE_STATIC +.Fa "type" "name" +.Fc +.\" .Bd -literal #define V_name VNET(name) .Ed .\" ------------------------------------------------------------ .Ss "Virtual Instance Selection" .\" .Fo CRED_TO_VNET .Fa "struct ucred *" .Fc .\" .Fo TD_TO_VNET .Fa "struct thread *" .Fc .\" .Fo P_TO_VNET .Fa "struct proc *" .Fc .\" .Fo IS_DEFAULT_VNET .Fa "struct vnet *" .Fc .\" .Fo VNET_ASSERT .Fa exp msg .Fc .\" .Fo CURVNET_SET .Fa "struct vnet *" .Fc .\" .Fo CURVNET_SET_QUIET .Fa "struct vnet *" .Fc .\" .Fn CURVNET_RESTORE .\" .Fo VNET_ITERATOR_DECL .Fa "struct vnet *" .Fc .\" .Fo VNET_FOREACH .Fa "struct vnet *" .Fc .\" ------------------------------------------------------------ .Ss "Locking" .\" .Fn VNET_LIST_RLOCK .Fn VNET_LIST_RUNLOCK .Fn VNET_LIST_RLOCK_NOSLEEP .Fn VNET_LIST_RUNLOCK_NOSLEEP .\" ------------------------------------------------------------ .Ss "Startup and Teardown Functions" .\" .Ft "struct vnet *" .Fo vnet_alloc .Fa void .Fc .\" .Ft void .Fo vnet_destroy .Fa "struct vnet *" .Fc .\" .Fo VNET_SYSINIT .Fa ident .Fa "enum sysinit_sub_id subsystem" .Fa "enum sysinit_elem_order order" .Fa "sysinit_cfunc_t func" .Fa "const void *arg" .Fc .\" .Fo VNET_SYSUNINIT .Fa ident .Fa "enum sysinit_sub_id subsystem" .Fa "enum sysinit_elem_order order" .Fa "sysinit_cfunc_t func" .Fa "const void *arg" .Fc .\" ------------------------------------------------------------ .Ss "Eventhandlers" .\" .Fo VNET_GLOBAL_EVENTHANDLER_REGISTER .Fa "const char *name" .Fa "void *func" .Fa "void *arg" .Fa "int priority" .Fc .\" .Fo VNET_GLOBAL_EVENTHANDLER_REGISTER_TAG .Fa "eventhandler_tag tag" .Fa "const char *name" .Fa "void *func" .Fa "void *arg" .Fa "int priority" .Fc .\" ------------------------------------------------------------ .Ss "Sysctl Handling" .Fo SYSCTL_VNET_INT .Fa parent nbr name access ptr val descr .Fc .Fo SYSCTL_VNET_PROC .Fa parent nbr name access ptr arg handler fmt descr .Fc .Fo SYSCTL_VNET_STRING .Fa parent nbr name access arg len descr .Fc .Fo SYSCTL_VNET_STRUCT .Fa parent nbr name access ptr type descr .Fc .Fo SYSCTL_VNET_UINT .Fa parent nbr name access ptr val descr .Fc .Fo VNET_SYSCTL_ARG .Fa req arg1 .Fc .\" ------------------------------------------------------------ .Sh DESCRIPTION .Nm is the name of a technique to virtualize the network stack. The basic idea is to change global resources most notably variables into per network stack resources and have functions, sysctls, eventhandlers, etc. access and handle them in the context of the correct instance. Each (virtual) network stack is attached to a .Em prison , with .Vt vnet0 being the unrestricted default network stack of the base system. .Pp The global defines for .Dv VNET_SETNAME and .Dv VNET_SYMPREFIX are shared with .Xr kvm 3 to access internals for debugging reasons. .\" ------------------------------------------------------------ .Ss "Variable Declaration" .\" Variables are virtualized by using the .Fn VNET_DEFINE macro rather than writing them out as .Em type name . -One can still use static initialization or storage class specifiers, e.g., +One can still use static initialization, e.g., .Pp -.Dl Li static VNET_DEFINE(int, foo) = 1; -or -.Dl Li static VNET_DEFINE(SLIST_HEAD(, bar), bars); +.Dl Li VNET_DEFINE(int, foo) = 1; +.Pp +Variables declared with the static keyword can use the +.Fn VNET_DEFINE_STATIC +macro, e.g., +.Pp +.Dl Li VNET_DEFINE_STATIC(SLIST_HEAD(, bar), bars); .Pp Static initialization is not possible when the virtualized variable would need to be referenced, e.g., with .Dq TAILQ_HEAD_INITIALIZER() . In that case a .Fn VNET_SYSINIT based initialization function must be used. .Pp External variables have to be declared using the .Fn VNET_DECLARE macro. In either case the convention is to define another macro, that is then used throughout the implementation to access that variable. The variable name is usually prefixed by .Em V_ to express that it is virtualized. The .Fn VNET macro will then translate accesses to that variable to the copy of the currently selected instance (see the .Sx "Virtual instance selection" section): .Pp .Dl Li #define V_name VNET(name) .Pp .Em NOTE: Do not confuse this with the convention used by .Xr VFS 9 . .Pp The .Fn VNET_NAME macro returns the offset within the memory region of the virtual network stack instance. It is usually only used with .Fn SYSCTL_VNET_* macros. .\" ------------------------------------------------------------ .Ss "Virtual Instance Selection" .\" There are three different places where the current virtual network stack pointer is stored and can be taken from: .Bl -enum -offset indent .It a .Em prison : .Dl "(struct prison *)->pr_vnet" .Pp For convenience the following macros are provided: .Bd -literal -compact -offset indent .Fn CRED_TO_VNET "struct ucred *" .Fn TD_TO_VNET "struct thread *" .Fn P_TO_VNET "struct proc *" .Ed .It a .Em socket : .Dl "(struct socket *)->so_vnet" .It an .Em interface : .Dl "(struct ifnet *)->if_vnet" .El .Pp .\" In addition the currently active instance is cached in .Dq "curthread->td_vnet" which is usually only accessed through the .Dv curvnet macro. .Pp .\" To set the correct context of the current virtual network instance, use the .Fn CURVNET_SET or .Fn CURVNET_SET_QUIET macros. The .Fn CURVNET_SET_QUIET version will not record vnet recursions in case the kernel was compiled with .Cd "options VNET_DEBUG" and should thus only be used in well known cases, where recursion is unavoidable. Both macros will save the previous state on the stack and it must be restored with the .Fn CURVNET_RESTORE macro. .Pp .Em NOTE: As the previous state is saved on the stack, you cannot have multiple .Fn CURVNET_SET calls in the same block. .Pp .Em NOTE: As the previous state is saved on the stack, a .Fn CURVNET_RESTORE call has to be in the same block as the .Fn CURVNET_SET call or in a subblock with the same idea of the saved instances as the outer block. .Pp .Em NOTE: As each macro is a set of operations and, as previously explained, cannot be put into its own block when defined, one cannot conditionally set the current vnet context. The following will .Em not work: .Bd -literal -offset indent if (condition) CURVNET_SET(vnet); .Ed .Pp nor would this work: .Bd -literal -offset indent if (condition) { CURVNET_SET(vnet); } CURVNET_RESTORE(); .Ed .Pp .\" Sometimes one needs to loop over all virtual instances, for example to update virtual from global state, to run a function from a .Xr callout 9 for each instance, etc. For those cases the .Fn VNET_ITERATOR_DECL and .Fn VNET_FOREACH macros are provided. The former macro defines the variable that iterates over the loop, and the latter loops over all of the virtual network stack instances. See .Sx "Locking" for how to savely traverse the list of all virtual instances. .Pp .\" The .Fn IS_DEFAULT_VNET macro provides a safe way to check whether the currently active instance is the unrestricted default network stack of the base system .Pq Vt vnet0 . .Pp .\" The .Fn VNET_ASSERT macro provides a way to conditionally add assertions that are only active with .Cd "options VIMAGE" compiled in and either .Cd "options VNET_DEBUG" or .Cd "options INVARIANTS" enabled as well. It uses the same semantics as .Xr KASSERT 9 . .\" ------------------------------------------------------------ .Ss "Locking" .\" For public access to the list of virtual network stack instances e.g., by the .Fn VNET_FOREACH macro, read locks are provided. Macros are used to abstract from the actual type of the locks. If a caller may sleep while traversing the list, it must use the .Fn VNET_LIST_RLOCK and .Fn VNET_LIST_RUNLOCK macros. Otherwise, the caller can use .Fn VNET_LIST_RLOCK_NOSLEEP and .Fn VNET_LIST_RUNLOCK_NOSLEEP . .\" ------------------------------------------------------------ .Ss "Startup and Teardown Functions" .\" To start or tear down a virtual network stack instance the internal functions .Fn vnet_alloc and .Fn vnet_destroy are provided and called from the jail framework. They run the publicly provided methods to handle network stack startup and teardown. .Pp For public control, the system startup interface has been enhanced to not only handle a system boot but to also handle a virtual network stack startup and teardown. To the base system the .Fn VNET_SYSINIT and .Fn VNET_SYSUNINIT macros look exactly as if there were no virtual network stack. In fact, if .Cd "options VIMAGE" is not compiled in they are compiled to the standard .Fn SYSINIT macros. In addition to that they are run for each virtual network stack when starting or, in reverse order, when shutting down. .\" ------------------------------------------------------------ .Ss "Eventhandlers" .\" Eventhandlers can be handled in two ways: .Pp .Bl -enum -offset indent -compact .It save the .Em tags returned in each virtual instance and properly free the eventhandlers on teardown using those, or .It use one eventhandler that will iterate over all virtual network stack instances. .El .Pp For the first case one can just use the normal .Xr EVENTHANDLER 9 functions, while for the second case the .Fn VNET_GLOBAL_EVENTHANDLER_REGISTER and .Fn VNET_GLOBAL_EVENTHANDLER_REGISTER_TAG macros are provided. These differ in that .Fn VNET_GLOBAL_EVENTHANDLER_REGISTER_TAG takes an extra first argument that will carry the .Fa "tag" upon return. Eventhandlers registered with either of these will not run .Fa func directly but .Fa func will be called from an internal iterator function for each vnet. Both macros can only be used for eventhandlers that do not take additional arguments, as the variadic arguments from an .Xr EVENTHANDLER_INVOKE 9 call will be ignored. .\" ------------------------------------------------------------ .Ss "Sysctl Handling" .\" A .Xr sysctl 9 can be virtualized by using one of the .Fn SYSCTL_VNET_* macros. .Pp They take the same arguments as the standard .Xr sysctl 9 functions, with the only difference, that the .Fa ptr argument has to be passed as .Ql &VNET_NAME(foo) instead of .Ql &foo so that the variable can be selected from the correct memory region of the virtual network stack instance of the caller. .Pp For the very rare case a sysctl handler function would want to handle .Fa arg1 itself the .Fn VNET_SYSCTL_ARG req arg1 is provided that will translate the .Fa arg1 argument to the correct memory address in the virtual network stack context of the caller. .\" ------------------------------------------------------------ .Sh SEE ALSO .Xr jail 2 , .Xr kvm 3 , .Xr EVENTHANDLER 9 , .\" .Xr pcpu 9 , .Xr KASSERT 9 , .Xr sysctl 9 .\" .Xr SYSINIT 9 .Pp Marko Zec, Implementing a Clonable Network Stack in the FreeBSD Kernel, USENIX ATC'03, June 2003, Boston .Sh HISTORY The virtual network stack implementation first appeared in .Fx 8.0 . .Sh AUTHORS .An -nosplit The .Nm framework has been designed and implemented at the University of Zagreb by .An Marko Zec , and later extended and refined by .An Bjoern A. Zeeb and .An Robert Watson , under contract to the FreeBSD Foundation. .Pp This manual page was written by .An Bjoern A. Zeeb, CK Software GmbH, under sponsorship from the FreeBSD Foundation. Index: head/sys/net/vnet.h =================================================================== --- head/sys/net/vnet.h (revision 336673) +++ head/sys/net/vnet.h (revision 336674) @@ -1,434 +1,440 @@ /*- * SPDX-License-Identifier: BSD-2-Clause-FreeBSD * * Copyright (c) 2006-2009 University of Zagreb * Copyright (c) 2006-2009 FreeBSD Foundation * All rights reserved. * * This software was developed by the University of Zagreb and the * FreeBSD Foundation under sponsorship by the Stichting NLnet and the * FreeBSD Foundation. * * Copyright (c) 2009 Jeffrey Roberson * Copyright (c) 2009 Robert N. M. Watson * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $FreeBSD$ */ /*- * This header file defines several sets of interfaces supporting virtualized * network stacks: * * - Definition of 'struct vnet' and functions and macros to allocate/free/ * manipulate it. * * - A virtual network stack memory allocator, which provides support for * virtualized global variables via a special linker set, set_vnet. * * - Virtualized sysinits/sysuninits, which allow constructors and * destructors to be run for each network stack subsystem as virtual * instances are created and destroyed. * * If VIMAGE isn't compiled into the kernel, virtualized global variables * compile to normal global variables, and virtualized sysinits to regular * sysinits. */ #ifndef _NET_VNET_H_ #define _NET_VNET_H_ /* * struct vnet describes a virtualized network stack, and is primarily a * pointer to storage for virtualized global variables. Expose to userspace * as required for libkvm. */ #if defined(_KERNEL) || defined(_WANT_VNET) #include struct vnet { LIST_ENTRY(vnet) vnet_le; /* all vnets list */ u_int vnet_magic_n; u_int vnet_ifcnt; u_int vnet_sockcnt; u_int vnet_state; /* SI_SUB_* */ void *vnet_data_mem; uintptr_t vnet_data_base; }; #define VNET_MAGIC_N 0x3e0d8f29 /* * These two virtual network stack allocator definitions are also required * for libkvm so that it can evaluate virtualized global variables. */ #define VNET_SETNAME "set_vnet" #define VNET_SYMPREFIX "vnet_entry_" #endif #ifdef _KERNEL #define VNET_PCPUSTAT_DECLARE(type, name) \ VNET_DECLARE(counter_u64_t, name[sizeof(type) / sizeof(uint64_t)]) #define VNET_PCPUSTAT_DEFINE(type, name) \ VNET_DEFINE(counter_u64_t, name[sizeof(type) / sizeof(uint64_t)]) +#define VNET_PCPUSTAT_DEFINE_STATIC(type, name) \ + VNET_DEFINE_STATIC(counter_u64_t, name[sizeof(type) / sizeof(uint64_t)]) #define VNET_PCPUSTAT_ALLOC(name, wait) \ COUNTER_ARRAY_ALLOC(VNET(name), \ sizeof(VNET(name)) / sizeof(counter_u64_t), (wait)) #define VNET_PCPUSTAT_FREE(name) \ COUNTER_ARRAY_FREE(VNET(name), sizeof(VNET(name)) / sizeof(counter_u64_t)) #define VNET_PCPUSTAT_ADD(type, name, f, v) \ counter_u64_add(VNET(name)[offsetof(type, f) / sizeof(uint64_t)], (v)) #define VNET_PCPUSTAT_FETCH(type, name, f) \ counter_u64_fetch(VNET(name)[offsetof(type, f) / sizeof(uint64_t)]) #define VNET_PCPUSTAT_SYSINIT(name) \ static void \ vnet_##name##_init(const void *unused) \ { \ VNET_PCPUSTAT_ALLOC(name, M_WAITOK); \ } \ VNET_SYSINIT(vnet_ ## name ## _init, SI_SUB_INIT_IF, \ SI_ORDER_FIRST, vnet_ ## name ## _init, NULL) #define VNET_PCPUSTAT_SYSUNINIT(name) \ static void \ vnet_##name##_uninit(const void *unused) \ { \ VNET_PCPUSTAT_FREE(name); \ } \ VNET_SYSUNINIT(vnet_ ## name ## _uninit, SI_SUB_INIT_IF, \ SI_ORDER_FIRST, vnet_ ## name ## _uninit, NULL) #ifdef SYSCTL_OID #define SYSCTL_VNET_PCPUSTAT(parent, nbr, name, type, array, desc) \ static int \ array##_sysctl(SYSCTL_HANDLER_ARGS) \ { \ type s; \ CTASSERT((sizeof(type) / sizeof(uint64_t)) == \ (sizeof(VNET(array)) / sizeof(counter_u64_t))); \ COUNTER_ARRAY_COPY(VNET(array), &s, sizeof(type) / sizeof(uint64_t));\ if (req->newptr) \ COUNTER_ARRAY_ZERO(VNET(array), \ sizeof(type) / sizeof(uint64_t)); \ return (SYSCTL_OUT(req, &s, sizeof(type))); \ } \ SYSCTL_PROC(parent, nbr, name, CTLFLAG_VNET | CTLTYPE_OPAQUE | CTLFLAG_RW, \ NULL, 0, array ## _sysctl, "I", desc) #endif /* SYSCTL_OID */ #ifdef VIMAGE #include #include /* for struct thread */ #include #include /* * Location of the kernel's 'set_vnet' linker set. */ extern uintptr_t *__start_set_vnet; __GLOBL(__start_set_vnet); extern uintptr_t *__stop_set_vnet; __GLOBL(__stop_set_vnet); #define VNET_START (uintptr_t)&__start_set_vnet #define VNET_STOP (uintptr_t)&__stop_set_vnet /* * Functions to allocate and destroy virtual network stacks. */ struct vnet *vnet_alloc(void); void vnet_destroy(struct vnet *vnet); /* * The current virtual network stack -- we may wish to move this to struct * pcpu in the future. */ #define curvnet curthread->td_vnet /* * Various macros -- get and set the current network stack, but also * assertions. */ #if defined(INVARIANTS) || defined(VNET_DEBUG) #define VNET_ASSERT(exp, msg) do { \ if (!(exp)) \ panic msg; \ } while (0) #else #define VNET_ASSERT(exp, msg) do { \ } while (0) #endif #ifdef VNET_DEBUG void vnet_log_recursion(struct vnet *, const char *, int); #define CURVNET_SET_QUIET(arg) \ VNET_ASSERT((arg) != NULL && (arg)->vnet_magic_n == VNET_MAGIC_N, \ ("CURVNET_SET at %s:%d %s() curvnet=%p vnet=%p", \ __FILE__, __LINE__, __func__, curvnet, (arg))); \ struct vnet *saved_vnet = curvnet; \ const char *saved_vnet_lpush = curthread->td_vnet_lpush; \ curvnet = arg; \ curthread->td_vnet_lpush = __func__; #define CURVNET_SET_VERBOSE(arg) \ CURVNET_SET_QUIET(arg) \ if (saved_vnet) \ vnet_log_recursion(saved_vnet, saved_vnet_lpush, __LINE__); #define CURVNET_SET(arg) CURVNET_SET_VERBOSE(arg) #define CURVNET_RESTORE() \ VNET_ASSERT(curvnet != NULL && (saved_vnet == NULL || \ saved_vnet->vnet_magic_n == VNET_MAGIC_N), \ ("CURVNET_RESTORE at %s:%d %s() curvnet=%p saved_vnet=%p", \ __FILE__, __LINE__, __func__, curvnet, saved_vnet)); \ curvnet = saved_vnet; \ curthread->td_vnet_lpush = saved_vnet_lpush; #else /* !VNET_DEBUG */ #define CURVNET_SET_QUIET(arg) \ VNET_ASSERT((arg) != NULL && (arg)->vnet_magic_n == VNET_MAGIC_N, \ ("CURVNET_SET at %s:%d %s() curvnet=%p vnet=%p", \ __FILE__, __LINE__, __func__, curvnet, (arg))); \ struct vnet *saved_vnet = curvnet; \ curvnet = arg; #define CURVNET_SET_VERBOSE(arg) \ CURVNET_SET_QUIET(arg) #define CURVNET_SET(arg) CURVNET_SET_VERBOSE(arg) #define CURVNET_RESTORE() \ VNET_ASSERT(curvnet != NULL && (saved_vnet == NULL || \ saved_vnet->vnet_magic_n == VNET_MAGIC_N), \ ("CURVNET_RESTORE at %s:%d %s() curvnet=%p saved_vnet=%p", \ __FILE__, __LINE__, __func__, curvnet, saved_vnet)); \ curvnet = saved_vnet; #endif /* VNET_DEBUG */ extern struct vnet *vnet0; #define IS_DEFAULT_VNET(arg) ((arg) == vnet0) #define CRED_TO_VNET(cr) (cr)->cr_prison->pr_vnet #define TD_TO_VNET(td) CRED_TO_VNET((td)->td_ucred) #define P_TO_VNET(p) CRED_TO_VNET((p)->p_ucred) /* * Global linked list of all virtual network stacks, along with read locks to * access it. If a caller may sleep while accessing the list, it must use * the sleepable lock macros. */ LIST_HEAD(vnet_list_head, vnet); extern struct vnet_list_head vnet_head; extern struct rwlock vnet_rwlock; extern struct sx vnet_sxlock; #define VNET_LIST_RLOCK() sx_slock(&vnet_sxlock) #define VNET_LIST_RLOCK_NOSLEEP() rw_rlock(&vnet_rwlock) #define VNET_LIST_RUNLOCK() sx_sunlock(&vnet_sxlock) #define VNET_LIST_RUNLOCK_NOSLEEP() rw_runlock(&vnet_rwlock) /* * Iteration macros to walk the global list of virtual network stacks. */ #define VNET_ITERATOR_DECL(arg) struct vnet *arg #define VNET_FOREACH(arg) LIST_FOREACH((arg), &vnet_head, vnet_le) /* * Virtual network stack memory allocator, which allows global variables to * be automatically instantiated for each network stack instance. */ #define VNET_NAME(n) vnet_entry_##n #define VNET_DECLARE(t, n) extern t VNET_NAME(n) -#define VNET_DEFINE(t, n) t VNET_NAME(n) __section(VNET_SETNAME) __used +#define VNET_DEFINE(t, n) \ + t VNET_NAME(n) __section(VNET_SETNAME) __used +#define VNET_DEFINE_STATIC(t, n) \ + static t VNET_NAME(n) __section(VNET_SETNAME) __used #define _VNET_PTR(b, n) (__typeof(VNET_NAME(n))*) \ ((b) + (uintptr_t)&VNET_NAME(n)) #define _VNET(b, n) (*_VNET_PTR(b, n)) /* * Virtualized global variable accessor macros. */ #define VNET_VNET_PTR(vnet, n) _VNET_PTR((vnet)->vnet_data_base, n) #define VNET_VNET(vnet, n) (*VNET_VNET_PTR((vnet), n)) #define VNET_PTR(n) VNET_VNET_PTR(curvnet, n) #define VNET(n) VNET_VNET(curvnet, n) /* * Virtual network stack allocator interfaces from the kernel linker. */ void *vnet_data_alloc(int size); void vnet_data_copy(void *start, int size); void vnet_data_free(void *start_arg, int size); /* * Virtual sysinit mechanism, allowing network stack components to declare * startup and shutdown methods to be run when virtual network stack * instances are created and destroyed. */ #include /* * SYSINIT/SYSUNINIT variants that provide per-vnet constructors and * destructors. */ struct vnet_sysinit { enum sysinit_sub_id subsystem; enum sysinit_elem_order order; sysinit_cfunc_t func; const void *arg; TAILQ_ENTRY(vnet_sysinit) link; }; #define VNET_SYSINIT(ident, subsystem, order, func, arg) \ static struct vnet_sysinit ident ## _vnet_init = { \ subsystem, \ order, \ (sysinit_cfunc_t)(sysinit_nfunc_t)func, \ (arg) \ }; \ SYSINIT(vnet_init_ ## ident, subsystem, order, \ vnet_register_sysinit, &ident ## _vnet_init); \ SYSUNINIT(vnet_init_ ## ident, subsystem, order, \ vnet_deregister_sysinit, &ident ## _vnet_init) #define VNET_SYSUNINIT(ident, subsystem, order, func, arg) \ static struct vnet_sysinit ident ## _vnet_uninit = { \ subsystem, \ order, \ (sysinit_cfunc_t)(sysinit_nfunc_t)func, \ (arg) \ }; \ SYSINIT(vnet_uninit_ ## ident, subsystem, order, \ vnet_register_sysuninit, &ident ## _vnet_uninit); \ SYSUNINIT(vnet_uninit_ ## ident, subsystem, order, \ vnet_deregister_sysuninit, &ident ## _vnet_uninit) /* * Run per-vnet sysinits or sysuninits during vnet creation/destruction. */ void vnet_sysinit(void); void vnet_sysuninit(void); /* * Interfaces for managing per-vnet constructors and destructors. */ void vnet_register_sysinit(void *arg); void vnet_register_sysuninit(void *arg); void vnet_deregister_sysinit(void *arg); void vnet_deregister_sysuninit(void *arg); /* * EVENTHANDLER(9) extensions. */ #include void vnet_global_eventhandler_iterator_func(void *, ...); #define VNET_GLOBAL_EVENTHANDLER_REGISTER_TAG(tag, name, func, arg, priority) \ do { \ if (IS_DEFAULT_VNET(curvnet)) { \ (tag) = vimage_eventhandler_register(NULL, #name, func, \ arg, priority, \ vnet_global_eventhandler_iterator_func); \ } \ } while(0) #define VNET_GLOBAL_EVENTHANDLER_REGISTER(name, func, arg, priority) \ do { \ if (IS_DEFAULT_VNET(curvnet)) { \ vimage_eventhandler_register(NULL, #name, func, \ arg, priority, \ vnet_global_eventhandler_iterator_func); \ } \ } while(0) #else /* !VIMAGE */ /* * Various virtual network stack macros compile to no-ops without VIMAGE. */ #define curvnet NULL #define VNET_ASSERT(exp, msg) #define CURVNET_SET(arg) #define CURVNET_SET_QUIET(arg) #define CURVNET_RESTORE() #define VNET_LIST_RLOCK() #define VNET_LIST_RLOCK_NOSLEEP() #define VNET_LIST_RUNLOCK() #define VNET_LIST_RUNLOCK_NOSLEEP() #define VNET_ITERATOR_DECL(arg) #define VNET_FOREACH(arg) #define IS_DEFAULT_VNET(arg) 1 #define CRED_TO_VNET(cr) NULL #define TD_TO_VNET(td) NULL #define P_TO_VNET(p) NULL /* * Versions of the VNET macros that compile to normal global variables and * standard sysctl definitions. */ #define VNET_NAME(n) n #define VNET_DECLARE(t, n) extern t n -#define VNET_DEFINE(t, n) t n +#define VNET_DEFINE(t, n) struct _hack; t n +#define VNET_DEFINE_STATIC(t, n) static t n #define _VNET_PTR(b, n) &VNET_NAME(n) /* * Virtualized global variable accessor macros. */ #define VNET_VNET_PTR(vnet, n) (&(n)) #define VNET_VNET(vnet, n) (n) #define VNET_PTR(n) (&(n)) #define VNET(n) (n) /* * When VIMAGE isn't compiled into the kernel, VNET_SYSINIT/VNET_SYSUNINIT * map into normal sysinits, which have the same ordering properties. */ #define VNET_SYSINIT(ident, subsystem, order, func, arg) \ SYSINIT(ident, subsystem, order, func, arg) #define VNET_SYSUNINIT(ident, subsystem, order, func, arg) \ SYSUNINIT(ident, subsystem, order, func, arg) /* * Without VIMAGE revert to the default implementation. */ #define VNET_GLOBAL_EVENTHANDLER_REGISTER_TAG(tag, name, func, arg, priority) \ (tag) = eventhandler_register(NULL, #name, func, arg, priority) #define VNET_GLOBAL_EVENTHANDLER_REGISTER(name, func, arg, priority) \ eventhandler_register(NULL, #name, func, arg, priority) #endif /* VIMAGE */ #endif /* _KERNEL */ #endif /* !_NET_VNET_H_ */