1*3049b2f5SQuentin Thébault.\"- 2*3049b2f5SQuentin Thébault.\" Copyright (c) 2010 The FreeBSD Foundation 3*3049b2f5SQuentin Thébault.\" 4*3049b2f5SQuentin Thébault.\" This documentation was written by CK Software GmbH under sponsorship from 5*3049b2f5SQuentin Thébault.\" the FreeBSD Foundation. 6*3049b2f5SQuentin Thébault.\" 7*3049b2f5SQuentin Thébault.\" Redistribution and use in source and binary forms, with or without 8*3049b2f5SQuentin Thébault.\" modification, are permitted provided that the following conditions 9*3049b2f5SQuentin Thébault.\" are met: 10*3049b2f5SQuentin Thébault.\" 1. Redistributions of source code must retain the above copyright 11*3049b2f5SQuentin Thébault.\" notice, this list of conditions and the following disclaimer. 12*3049b2f5SQuentin Thébault.\" 2. Redistributions in binary form must reproduce the above copyright 13*3049b2f5SQuentin Thébault.\" notice, this list of conditions and the following disclaimer in the 14*3049b2f5SQuentin Thébault.\" documentation and/or other materials provided with the distribution. 15*3049b2f5SQuentin Thébault.\" 16*3049b2f5SQuentin Thébault.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17*3049b2f5SQuentin Thébault.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18*3049b2f5SQuentin Thébault.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19*3049b2f5SQuentin Thébault.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20*3049b2f5SQuentin Thébault.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21*3049b2f5SQuentin Thébault.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22*3049b2f5SQuentin Thébault.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23*3049b2f5SQuentin Thébault.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24*3049b2f5SQuentin Thébault.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25*3049b2f5SQuentin Thébault.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26*3049b2f5SQuentin Thébault.\" SUCH DAMAGE. 27*3049b2f5SQuentin Thébault.\" 28*3049b2f5SQuentin Thébault.Dd December 10, 2020 29*3049b2f5SQuentin Thébault.Dt VNET 9 30*3049b2f5SQuentin Thébault.Os 31*3049b2f5SQuentin Thébault.Sh NAME 32*3049b2f5SQuentin Thébault.Nm VNET 33*3049b2f5SQuentin Thébault.Nd "network subsystem virtualization infrastructure" 34*3049b2f5SQuentin Thébault.Sh SYNOPSIS 35*3049b2f5SQuentin Thébault.Cd "options VIMAGE" 36*3049b2f5SQuentin Thébault.Cd "options VNET_DEBUG" 37*3049b2f5SQuentin Thébault.Pp 38*3049b2f5SQuentin Thébault.In net/vnet.h 39*3049b2f5SQuentin Thébault.\"------------------------------------------------------------ 40*3049b2f5SQuentin Thébault.Ss "Constants and Global Variables" 41*3049b2f5SQuentin Thébault.\" 42*3049b2f5SQuentin Thébault.Dv VNET_SETNAME 43*3049b2f5SQuentin Thébault.\" "set_vnet" 44*3049b2f5SQuentin Thébault.Dv VNET_SYMPREFIX 45*3049b2f5SQuentin Thébault.\" "vnet_entry_" 46*3049b2f5SQuentin Thébault.Vt extern struct vnet *vnet0; 47*3049b2f5SQuentin Thébault.\"------------------------------------------------------------ 48*3049b2f5SQuentin Thébault.Ss "Variable Declaration" 49*3049b2f5SQuentin Thébault.Fo VNET 50*3049b2f5SQuentin Thébault.Fa "name" 51*3049b2f5SQuentin Thébault.Fc 52*3049b2f5SQuentin Thébault.\" 53*3049b2f5SQuentin Thébault.Fo VNET_NAME 54*3049b2f5SQuentin Thébault.Fa "name" 55*3049b2f5SQuentin Thébault.Fc 56*3049b2f5SQuentin Thébault.\" 57*3049b2f5SQuentin Thébault.Fo VNET_DECLARE 58*3049b2f5SQuentin Thébault.Fa "type" "name" 59*3049b2f5SQuentin Thébault.Fc 60*3049b2f5SQuentin Thébault.\" 61*3049b2f5SQuentin Thébault.Fo VNET_DEFINE 62*3049b2f5SQuentin Thébault.Fa "type" "name" 63*3049b2f5SQuentin Thébault.Fc 64*3049b2f5SQuentin Thébault.\" 65*3049b2f5SQuentin Thébault.Fo VNET_DEFINE_STATIC 66*3049b2f5SQuentin Thébault.Fa "type" "name" 67*3049b2f5SQuentin Thébault.Fc 68*3049b2f5SQuentin Thébault.\" 69*3049b2f5SQuentin Thébault.Bd -literal 70*3049b2f5SQuentin Thébault#define V_name VNET(name) 71*3049b2f5SQuentin Thébault.Ed 72*3049b2f5SQuentin Thébault.\" ------------------------------------------------------------ 73*3049b2f5SQuentin Thébault.Ss "Virtual Instance Selection" 74*3049b2f5SQuentin Thébault.\" 75*3049b2f5SQuentin Thébault.Fo CRED_TO_VNET 76*3049b2f5SQuentin Thébault.Fa "struct ucred *" 77*3049b2f5SQuentin Thébault.Fc 78*3049b2f5SQuentin Thébault.\" 79*3049b2f5SQuentin Thébault.Fo TD_TO_VNET 80*3049b2f5SQuentin Thébault.Fa "struct thread *" 81*3049b2f5SQuentin Thébault.Fc 82*3049b2f5SQuentin Thébault.\" 83*3049b2f5SQuentin Thébault.Fo P_TO_VNET 84*3049b2f5SQuentin Thébault.Fa "struct proc *" 85*3049b2f5SQuentin Thébault.Fc 86*3049b2f5SQuentin Thébault.\" 87*3049b2f5SQuentin Thébault.Fo IS_DEFAULT_VNET 88*3049b2f5SQuentin Thébault.Fa "struct vnet *" 89*3049b2f5SQuentin Thébault.Fc 90*3049b2f5SQuentin Thébault.\" 91*3049b2f5SQuentin Thébault.Fo VNET_ASSERT 92*3049b2f5SQuentin Thébault.Fa exp msg 93*3049b2f5SQuentin Thébault.Fc 94*3049b2f5SQuentin Thébault.\" 95*3049b2f5SQuentin Thébault.Fo CURVNET_SET 96*3049b2f5SQuentin Thébault.Fa "struct vnet *" 97*3049b2f5SQuentin Thébault.Fc 98*3049b2f5SQuentin Thébault.\" 99*3049b2f5SQuentin Thébault.Fo CURVNET_SET_QUIET 100*3049b2f5SQuentin Thébault.Fa "struct vnet *" 101*3049b2f5SQuentin Thébault.Fc 102*3049b2f5SQuentin Thébault.\" 103*3049b2f5SQuentin Thébault.Fn CURVNET_RESTORE 104*3049b2f5SQuentin Thébault.\" 105*3049b2f5SQuentin Thébault.Fo VNET_ITERATOR_DECL 106*3049b2f5SQuentin Thébault.Fa "struct vnet *" 107*3049b2f5SQuentin Thébault.Fc 108*3049b2f5SQuentin Thébault.\" 109*3049b2f5SQuentin Thébault.Fo VNET_FOREACH 110*3049b2f5SQuentin Thébault.Fa "struct vnet *" 111*3049b2f5SQuentin Thébault.Fc 112*3049b2f5SQuentin Thébault.\" ------------------------------------------------------------ 113*3049b2f5SQuentin Thébault.Ss "Locking" 114*3049b2f5SQuentin Thébault.\" 115*3049b2f5SQuentin Thébault.Fn VNET_LIST_RLOCK 116*3049b2f5SQuentin Thébault.Fn VNET_LIST_RUNLOCK 117*3049b2f5SQuentin Thébault.Fn VNET_LIST_RLOCK_NOSLEEP 118*3049b2f5SQuentin Thébault.Fn VNET_LIST_RUNLOCK_NOSLEEP 119*3049b2f5SQuentin Thébault.\" ------------------------------------------------------------ 120*3049b2f5SQuentin Thébault.Ss "Startup and Teardown Functions" 121*3049b2f5SQuentin Thébault.\" 122*3049b2f5SQuentin Thébault.Ft "struct vnet *" 123*3049b2f5SQuentin Thébault.Fo vnet_alloc 124*3049b2f5SQuentin Thébault.Fa void 125*3049b2f5SQuentin Thébault.Fc 126*3049b2f5SQuentin Thébault.\" 127*3049b2f5SQuentin Thébault.Ft void 128*3049b2f5SQuentin Thébault.Fo vnet_destroy 129*3049b2f5SQuentin Thébault.Fa "struct vnet *" 130*3049b2f5SQuentin Thébault.Fc 131*3049b2f5SQuentin Thébault.\" 132*3049b2f5SQuentin Thébault.Fo VNET_SYSINIT 133*3049b2f5SQuentin Thébault.Fa ident 134*3049b2f5SQuentin Thébault.Fa "enum sysinit_sub_id subsystem" 135*3049b2f5SQuentin Thébault.Fa "enum sysinit_elem_order order" 136*3049b2f5SQuentin Thébault.Fa "sysinit_cfunc_t func" 137*3049b2f5SQuentin Thébault.Fa "const void *arg" 138*3049b2f5SQuentin Thébault.Fc 139*3049b2f5SQuentin Thébault.\" 140*3049b2f5SQuentin Thébault.Fo VNET_SYSUNINIT 141*3049b2f5SQuentin Thébault.Fa ident 142*3049b2f5SQuentin Thébault.Fa "enum sysinit_sub_id subsystem" 143*3049b2f5SQuentin Thébault.Fa "enum sysinit_elem_order order" 144*3049b2f5SQuentin Thébault.Fa "sysinit_cfunc_t func" 145*3049b2f5SQuentin Thébault.Fa "const void *arg" 146*3049b2f5SQuentin Thébault.Fc 147*3049b2f5SQuentin Thébault.\" ------------------------------------------------------------ 148*3049b2f5SQuentin Thébault.Ss "Eventhandlers" 149*3049b2f5SQuentin Thébault.\" 150*3049b2f5SQuentin Thébault.Fo VNET_GLOBAL_EVENTHANDLER_REGISTER 151*3049b2f5SQuentin Thébault.Fa "const char *name" 152*3049b2f5SQuentin Thébault.Fa "void *func" 153*3049b2f5SQuentin Thébault.Fa "void *arg" 154*3049b2f5SQuentin Thébault.Fa "int priority" 155*3049b2f5SQuentin Thébault.Fc 156*3049b2f5SQuentin Thébault.\" 157*3049b2f5SQuentin Thébault.Fo VNET_GLOBAL_EVENTHANDLER_REGISTER_TAG 158*3049b2f5SQuentin Thébault.Fa "eventhandler_tag tag" 159*3049b2f5SQuentin Thébault.Fa "const char *name" 160*3049b2f5SQuentin Thébault.Fa "void *func" 161*3049b2f5SQuentin Thébault.Fa "void *arg" 162*3049b2f5SQuentin Thébault.Fa "int priority" 163*3049b2f5SQuentin Thébault.Fc 164*3049b2f5SQuentin Thébault.\" ------------------------------------------------------------ 165*3049b2f5SQuentin Thébault.Ss "Sysctl Handling" 166*3049b2f5SQuentin Thébault.Fo SYSCTL_VNET_INT 167*3049b2f5SQuentin Thébault.Fa parent nbr name access ptr val descr 168*3049b2f5SQuentin Thébault.Fc 169*3049b2f5SQuentin Thébault.Fo SYSCTL_VNET_PROC 170*3049b2f5SQuentin Thébault.Fa parent nbr name access ptr arg handler fmt descr 171*3049b2f5SQuentin Thébault.Fc 172*3049b2f5SQuentin Thébault.Fo SYSCTL_VNET_STRING 173*3049b2f5SQuentin Thébault.Fa parent nbr name access arg len descr 174*3049b2f5SQuentin Thébault.Fc 175*3049b2f5SQuentin Thébault.Fo SYSCTL_VNET_STRUCT 176*3049b2f5SQuentin Thébault.Fa parent nbr name access ptr type descr 177*3049b2f5SQuentin Thébault.Fc 178*3049b2f5SQuentin Thébault.Fo SYSCTL_VNET_UINT 179*3049b2f5SQuentin Thébault.Fa parent nbr name access ptr val descr 180*3049b2f5SQuentin Thébault.Fc 181*3049b2f5SQuentin Thébault.Fo VNET_SYSCTL_ARG 182*3049b2f5SQuentin Thébault.Fa req arg1 183*3049b2f5SQuentin Thébault.Fc 184*3049b2f5SQuentin Thébault.\" ------------------------------------------------------------ 185*3049b2f5SQuentin Thébault.Sh DESCRIPTION 186*3049b2f5SQuentin Thébault.Nm 187*3049b2f5SQuentin Thébaultis the name of a technique to virtualize the network stack. 188*3049b2f5SQuentin ThébaultThe basic idea is to change global resources most notably variables into 189*3049b2f5SQuentin Thébaultper network stack resources and have functions, sysctls, eventhandlers, 190*3049b2f5SQuentin Thébaultetc. access and handle them in the context of the correct instance. 191*3049b2f5SQuentin ThébaultEach (virtual) network stack is attached to a 192*3049b2f5SQuentin Thébault.Em prison , 193*3049b2f5SQuentin Thébaultwith 194*3049b2f5SQuentin Thébault.Vt vnet0 195*3049b2f5SQuentin Thébaultbeing the unrestricted default network stack of the base system. 196*3049b2f5SQuentin Thébault.Pp 197*3049b2f5SQuentin ThébaultThe global defines for 198*3049b2f5SQuentin Thébault.Dv VNET_SETNAME 199*3049b2f5SQuentin Thébaultand 200*3049b2f5SQuentin Thébault.Dv VNET_SYMPREFIX 201*3049b2f5SQuentin Thébaultare shared with 202*3049b2f5SQuentin Thébault.Xr kvm 3 203*3049b2f5SQuentin Thébaultto access internals for debugging reasons. 204*3049b2f5SQuentin Thébault.\" ------------------------------------------------------------ 205*3049b2f5SQuentin Thébault.Ss "Variable Declaration" 206*3049b2f5SQuentin Thébault.\" 207*3049b2f5SQuentin ThébaultVariables are virtualized by using the 208*3049b2f5SQuentin Thébault.Fn VNET_DEFINE 209*3049b2f5SQuentin Thébaultmacro rather than writing them out as 210*3049b2f5SQuentin Thébault.Em type name . 211*3049b2f5SQuentin ThébaultOne can still use static initialization, e.g., 212*3049b2f5SQuentin Thébault.Pp 213*3049b2f5SQuentin Thébault.Dl Li VNET_DEFINE(int, foo) = 1; 214*3049b2f5SQuentin Thébault.Pp 215*3049b2f5SQuentin ThébaultVariables declared with the static keyword can use the 216*3049b2f5SQuentin Thébault.Fn VNET_DEFINE_STATIC 217*3049b2f5SQuentin Thébaultmacro, e.g., 218*3049b2f5SQuentin Thébault.Pp 219*3049b2f5SQuentin Thébault.Dl Li VNET_DEFINE_STATIC(SLIST_HEAD(, bar), bars); 220*3049b2f5SQuentin Thébault.Pp 221*3049b2f5SQuentin ThébaultStatic initialization is not possible when the virtualized variable 222*3049b2f5SQuentin Thébaultwould need to be referenced, e.g., with 223*3049b2f5SQuentin Thébault.Dq TAILQ_HEAD_INITIALIZER() . 224*3049b2f5SQuentin ThébaultIn that case a 225*3049b2f5SQuentin Thébault.Fn VNET_SYSINIT 226*3049b2f5SQuentin Thébaultbased initialization function must be used. 227*3049b2f5SQuentin Thébault.Pp 228*3049b2f5SQuentin ThébaultExternal variables have to be declared using the 229*3049b2f5SQuentin Thébault.Fn VNET_DECLARE 230*3049b2f5SQuentin Thébaultmacro. 231*3049b2f5SQuentin ThébaultIn either case the convention is to define another macro, 232*3049b2f5SQuentin Thébaultthat is then used throughout the implementation to access that variable. 233*3049b2f5SQuentin ThébaultThe variable name is usually prefixed by 234*3049b2f5SQuentin Thébault.Em V_ 235*3049b2f5SQuentin Thébaultto express that it is virtualized. 236*3049b2f5SQuentin ThébaultThe 237*3049b2f5SQuentin Thébault.Fn VNET 238*3049b2f5SQuentin Thébaultmacro will then translate accesses to that variable to the copy of the 239*3049b2f5SQuentin Thébaultcurrently selected instance (see the 240*3049b2f5SQuentin Thébault.Sx "Virtual instance selection" 241*3049b2f5SQuentin Thébaultsection): 242*3049b2f5SQuentin Thébault.Pp 243*3049b2f5SQuentin Thébault.Dl Li #define V_name VNET(name) 244*3049b2f5SQuentin Thébault.Pp 245*3049b2f5SQuentin Thébault.Em NOTE: 246*3049b2f5SQuentin ThébaultDo not confuse this with the convention used by 247*3049b2f5SQuentin Thébault.Xr VFS 9 . 248*3049b2f5SQuentin Thébault.Pp 249*3049b2f5SQuentin ThébaultThe 250*3049b2f5SQuentin Thébault.Fn VNET_NAME 251*3049b2f5SQuentin Thébaultmacro returns the offset within the memory region of the virtual network 252*3049b2f5SQuentin Thébaultstack instance. 253*3049b2f5SQuentin ThébaultIt is usually only used with 254*3049b2f5SQuentin Thébault.Fn SYSCTL_VNET_* 255*3049b2f5SQuentin Thébaultmacros. 256*3049b2f5SQuentin Thébault.\" ------------------------------------------------------------ 257*3049b2f5SQuentin Thébault.Ss "Virtual Instance Selection" 258*3049b2f5SQuentin Thébault.\" 259*3049b2f5SQuentin ThébaultThere are three different places where the current virtual 260*3049b2f5SQuentin Thébaultnetwork stack pointer is stored and can be taken from: 261*3049b2f5SQuentin Thébault.Bl -enum -offset indent 262*3049b2f5SQuentin Thébault.It 263*3049b2f5SQuentin Thébaulta 264*3049b2f5SQuentin Thébault.Em prison : 265*3049b2f5SQuentin Thébault.Dl "(struct prison *)->pr_vnet" 266*3049b2f5SQuentin Thébault.Pp 267*3049b2f5SQuentin ThébaultFor convenience the following macros are provided: 268*3049b2f5SQuentin Thébault.Bd -literal -compact -offset indent 269*3049b2f5SQuentin Thébault.Fn CRED_TO_VNET "struct ucred *" 270*3049b2f5SQuentin Thébault.Fn TD_TO_VNET "struct thread *" 271*3049b2f5SQuentin Thébault.Fn P_TO_VNET "struct proc *" 272*3049b2f5SQuentin Thébault.Ed 273*3049b2f5SQuentin Thébault.It 274*3049b2f5SQuentin Thébaulta 275*3049b2f5SQuentin Thébault.Em socket : 276*3049b2f5SQuentin Thébault.Dl "(struct socket *)->so_vnet" 277*3049b2f5SQuentin Thébault.It 278*3049b2f5SQuentin Thébaultan 279*3049b2f5SQuentin Thébault.Em interface : 280*3049b2f5SQuentin Thébault.Dl "(struct ifnet *)->if_vnet" 281*3049b2f5SQuentin Thébault.El 282*3049b2f5SQuentin Thébault.Pp 283*3049b2f5SQuentin Thébault.\" 284*3049b2f5SQuentin ThébaultIn addition the currently active instance is cached in 285*3049b2f5SQuentin Thébault.Dq "curthread->td_vnet" 286*3049b2f5SQuentin Thébaultwhich is usually only accessed through the 287*3049b2f5SQuentin Thébault.Dv curvnet 288*3049b2f5SQuentin Thébaultmacro. 289*3049b2f5SQuentin Thébault.Pp 290*3049b2f5SQuentin Thébault.\" 291*3049b2f5SQuentin ThébaultTo set the correct context of the current virtual network instance, use the 292*3049b2f5SQuentin Thébault.Fn CURVNET_SET 293*3049b2f5SQuentin Thébaultor 294*3049b2f5SQuentin Thébault.Fn CURVNET_SET_QUIET 295*3049b2f5SQuentin Thébaultmacros. 296*3049b2f5SQuentin ThébaultThe 297*3049b2f5SQuentin Thébault.Fn CURVNET_SET_QUIET 298*3049b2f5SQuentin Thébaultversion will not record vnet recursions in case the kernel was compiled 299*3049b2f5SQuentin Thébaultwith 300*3049b2f5SQuentin Thébault.Cd "options VNET_DEBUG" 301*3049b2f5SQuentin Thébaultand should thus only be used in well known cases, where recursion is 302*3049b2f5SQuentin Thébaultunavoidable. 303*3049b2f5SQuentin ThébaultBoth macros will save the previous state on the stack and it must be restored 304*3049b2f5SQuentin Thébaultwith the 305*3049b2f5SQuentin Thébault.Fn CURVNET_RESTORE 306*3049b2f5SQuentin Thébaultmacro. 307*3049b2f5SQuentin Thébault.Pp 308*3049b2f5SQuentin Thébault.Em NOTE: 309*3049b2f5SQuentin ThébaultAs the previous state is saved on the stack, you cannot have multiple 310*3049b2f5SQuentin Thébault.Fn CURVNET_SET 311*3049b2f5SQuentin Thébaultcalls in the same block. 312*3049b2f5SQuentin Thébault.Pp 313*3049b2f5SQuentin Thébault.Em NOTE: 314*3049b2f5SQuentin ThébaultAs the previous state is saved on the stack, a 315*3049b2f5SQuentin Thébault.Fn CURVNET_RESTORE 316*3049b2f5SQuentin Thébaultcall has to be in the same block as the 317*3049b2f5SQuentin Thébault.Fn CURVNET_SET 318*3049b2f5SQuentin Thébaultcall or in a subblock with the same idea of the saved instances as the 319*3049b2f5SQuentin Thébaultouter block. 320*3049b2f5SQuentin Thébault.Pp 321*3049b2f5SQuentin Thébault.Em NOTE: 322*3049b2f5SQuentin ThébaultAs each macro is a set of operations and, as previously explained, cannot 323*3049b2f5SQuentin Thébaultbe put into its own block when defined, one cannot conditionally set 324*3049b2f5SQuentin Thébaultthe current vnet context. 325*3049b2f5SQuentin ThébaultThe following will 326*3049b2f5SQuentin Thébault.Em not 327*3049b2f5SQuentin Thébaultwork: 328*3049b2f5SQuentin Thébault.Bd -literal -offset indent 329*3049b2f5SQuentin Thébaultif (condition) 330*3049b2f5SQuentin Thébault CURVNET_SET(vnet); 331*3049b2f5SQuentin Thébault.Ed 332*3049b2f5SQuentin Thébault.Pp 333*3049b2f5SQuentin Thébaultnor would this work: 334*3049b2f5SQuentin Thébault.Bd -literal -offset indent 335*3049b2f5SQuentin Thébaultif (condition) { 336*3049b2f5SQuentin Thébault CURVNET_SET(vnet); 337*3049b2f5SQuentin Thébault} 338*3049b2f5SQuentin ThébaultCURVNET_RESTORE(); 339*3049b2f5SQuentin Thébault.Ed 340*3049b2f5SQuentin Thébault.Pp 341*3049b2f5SQuentin Thébault.\" 342*3049b2f5SQuentin ThébaultSometimes one needs to loop over all virtual instances, for example to update 343*3049b2f5SQuentin Thébaultvirtual from global state, to run a function from a 344*3049b2f5SQuentin Thébault.Xr callout 9 345*3049b2f5SQuentin Thébaultfor each instance, etc. 346*3049b2f5SQuentin ThébaultFor those cases the 347*3049b2f5SQuentin Thébault.Fn VNET_ITERATOR_DECL 348*3049b2f5SQuentin Thébaultand 349*3049b2f5SQuentin Thébault.Fn VNET_FOREACH 350*3049b2f5SQuentin Thébaultmacros are provided. 351*3049b2f5SQuentin ThébaultThe former macro defines the variable that iterates over the loop, 352*3049b2f5SQuentin Thébaultand the latter loops over all of the virtual network stack instances. 353*3049b2f5SQuentin ThébaultSee 354*3049b2f5SQuentin Thébault.Sx "Locking" 355*3049b2f5SQuentin Thébaultfor how to savely traverse the list of all virtual instances. 356*3049b2f5SQuentin Thébault.Pp 357*3049b2f5SQuentin Thébault.\" 358*3049b2f5SQuentin ThébaultThe 359*3049b2f5SQuentin Thébault.Fn IS_DEFAULT_VNET 360*3049b2f5SQuentin Thébaultmacro provides a safe way to check whether the currently active instance is the 361*3049b2f5SQuentin Thébaultunrestricted default network stack of the base system 362*3049b2f5SQuentin Thébault.Pq Vt vnet0 . 363*3049b2f5SQuentin Thébault.Pp 364*3049b2f5SQuentin Thébault.\" 365*3049b2f5SQuentin ThébaultThe 366*3049b2f5SQuentin Thébault.Fn VNET_ASSERT 367*3049b2f5SQuentin Thébaultmacro provides a way to conditionally add assertions that are only active with 368*3049b2f5SQuentin Thébault.Cd "options VIMAGE" 369*3049b2f5SQuentin Thébaultcompiled in and either 370*3049b2f5SQuentin Thébault.Cd "options VNET_DEBUG" 371*3049b2f5SQuentin Thébaultor 372*3049b2f5SQuentin Thébault.Cd "options INVARIANTS" 373*3049b2f5SQuentin Thébaultenabled as well. 374*3049b2f5SQuentin ThébaultIt uses the same semantics as 375*3049b2f5SQuentin Thébault.Xr KASSERT 9 . 376*3049b2f5SQuentin Thébault.\" ------------------------------------------------------------ 377*3049b2f5SQuentin Thébault.Ss "Locking" 378*3049b2f5SQuentin Thébault.\" 379*3049b2f5SQuentin ThébaultFor public access to the list of virtual network stack instances 380*3049b2f5SQuentin Thébaulte.g., by the 381*3049b2f5SQuentin Thébault.Fn VNET_FOREACH 382*3049b2f5SQuentin Thébaultmacro, read locks are provided. 383*3049b2f5SQuentin ThébaultMacros are used to abstract from the actual type of the locks. 384*3049b2f5SQuentin ThébaultIf a caller may sleep while traversing the list, it must use the 385*3049b2f5SQuentin Thébault.Fn VNET_LIST_RLOCK 386*3049b2f5SQuentin Thébaultand 387*3049b2f5SQuentin Thébault.Fn VNET_LIST_RUNLOCK 388*3049b2f5SQuentin Thébaultmacros. 389*3049b2f5SQuentin ThébaultOtherwise, the caller can use 390*3049b2f5SQuentin Thébault.Fn VNET_LIST_RLOCK_NOSLEEP 391*3049b2f5SQuentin Thébaultand 392*3049b2f5SQuentin Thébault.Fn VNET_LIST_RUNLOCK_NOSLEEP . 393*3049b2f5SQuentin Thébault.\" ------------------------------------------------------------ 394*3049b2f5SQuentin Thébault.Ss "Startup and Teardown Functions" 395*3049b2f5SQuentin Thébault.\" 396*3049b2f5SQuentin ThébaultTo start or tear down a virtual network stack instance the internal 397*3049b2f5SQuentin Thébaultfunctions 398*3049b2f5SQuentin Thébault.Fn vnet_alloc 399*3049b2f5SQuentin Thébaultand 400*3049b2f5SQuentin Thébault.Fn vnet_destroy 401*3049b2f5SQuentin Thébaultare provided and called from the jail framework. 402*3049b2f5SQuentin ThébaultThey run the publicly provided methods to handle network stack 403*3049b2f5SQuentin Thébaultstartup and teardown. 404*3049b2f5SQuentin Thébault.Pp 405*3049b2f5SQuentin ThébaultFor public control, the system startup interface has been enhanced 406*3049b2f5SQuentin Thébaultto not only handle a system boot but to also handle a virtual 407*3049b2f5SQuentin Thébaultnetwork stack startup and teardown. 408*3049b2f5SQuentin ThébaultTo the base system the 409*3049b2f5SQuentin Thébault.Fn VNET_SYSINIT 410*3049b2f5SQuentin Thébaultand 411*3049b2f5SQuentin Thébault.Fn VNET_SYSUNINIT 412*3049b2f5SQuentin Thébaultmacros look exactly as if there were no virtual network stack. 413*3049b2f5SQuentin ThébaultIn fact, if 414*3049b2f5SQuentin Thébault.Cd "options VIMAGE" 415*3049b2f5SQuentin Thébaultis not compiled in they are compiled to the standard 416*3049b2f5SQuentin Thébault.Fn SYSINIT 417*3049b2f5SQuentin Thébaultmacros. 418*3049b2f5SQuentin ThébaultIn addition to that they are run for each virtual network stack 419*3049b2f5SQuentin Thébaultwhen starting or, in reverse order, when shutting down. 420*3049b2f5SQuentin Thébault.\" ------------------------------------------------------------ 421*3049b2f5SQuentin Thébault.Ss "Eventhandlers" 422*3049b2f5SQuentin Thébault.\" 423*3049b2f5SQuentin ThébaultEventhandlers can be handled in two ways: 424*3049b2f5SQuentin Thébault.Pp 425*3049b2f5SQuentin Thébault.Bl -enum -offset indent -compact 426*3049b2f5SQuentin Thébault.It 427*3049b2f5SQuentin Thébaultsave the 428*3049b2f5SQuentin Thébault.Em tags 429*3049b2f5SQuentin Thébaultreturned in each virtual instance and properly free the eventhandlers 430*3049b2f5SQuentin Thébaulton teardown using those, or 431*3049b2f5SQuentin Thébault.It 432*3049b2f5SQuentin Thébaultuse one eventhandler that will iterate over all virtual network 433*3049b2f5SQuentin Thébaultstack instances. 434*3049b2f5SQuentin Thébault.El 435*3049b2f5SQuentin Thébault.Pp 436*3049b2f5SQuentin ThébaultFor the first case one can just use the normal 437*3049b2f5SQuentin Thébault.Xr EVENTHANDLER 9 438*3049b2f5SQuentin Thébaultfunctions, while for the second case the 439*3049b2f5SQuentin Thébault.Fn VNET_GLOBAL_EVENTHANDLER_REGISTER 440*3049b2f5SQuentin Thébaultand 441*3049b2f5SQuentin Thébault.Fn VNET_GLOBAL_EVENTHANDLER_REGISTER_TAG 442*3049b2f5SQuentin Thébaultmacros are provided. 443*3049b2f5SQuentin ThébaultThese differ in that 444*3049b2f5SQuentin Thébault.Fn VNET_GLOBAL_EVENTHANDLER_REGISTER_TAG 445*3049b2f5SQuentin Thébaulttakes an extra first argument that will carry the 446*3049b2f5SQuentin Thébault.Fa "tag" 447*3049b2f5SQuentin Thébaultupon return. 448*3049b2f5SQuentin ThébaultEventhandlers registered with either of these will not run 449*3049b2f5SQuentin Thébault.Fa func 450*3049b2f5SQuentin Thébaultdirectly but 451*3049b2f5SQuentin Thébault.Fa func 452*3049b2f5SQuentin Thébaultwill be called from an internal iterator function for each vnet. 453*3049b2f5SQuentin ThébaultBoth macros can only be used for eventhandlers that do not take 454*3049b2f5SQuentin Thébaultadditional arguments, as the variadic arguments from an 455*3049b2f5SQuentin Thébault.Xr EVENTHANDLER_INVOKE 9 456*3049b2f5SQuentin Thébaultcall will be ignored. 457*3049b2f5SQuentin Thébault.\" ------------------------------------------------------------ 458*3049b2f5SQuentin Thébault.Ss "Sysctl Handling" 459*3049b2f5SQuentin Thébault.\" 460*3049b2f5SQuentin ThébaultA 461*3049b2f5SQuentin Thébault.Xr sysctl 9 462*3049b2f5SQuentin Thébaultcan be virtualized by using one of the 463*3049b2f5SQuentin Thébault.Fn SYSCTL_VNET_* 464*3049b2f5SQuentin Thébaultmacros. 465*3049b2f5SQuentin Thébault.Pp 466*3049b2f5SQuentin ThébaultThey take the same arguments as the standard 467*3049b2f5SQuentin Thébault.Xr sysctl 9 468*3049b2f5SQuentin Thébaultfunctions, with the only difference, that the 469*3049b2f5SQuentin Thébault.Fa ptr 470*3049b2f5SQuentin Thébaultargument has to be passed as 471*3049b2f5SQuentin Thébault.Ql &VNET_NAME(foo) 472*3049b2f5SQuentin Thébaultinstead of 473*3049b2f5SQuentin Thébault.Ql &foo 474*3049b2f5SQuentin Thébaultso that the variable can be selected from the correct memory 475*3049b2f5SQuentin Thébaultregion of the virtual network stack instance of the caller. 476*3049b2f5SQuentin Thébault.Pp 477*3049b2f5SQuentin ThébaultFor the very rare case a sysctl handler function would want to 478*3049b2f5SQuentin Thébaulthandle 479*3049b2f5SQuentin Thébault.Fa arg1 480*3049b2f5SQuentin Thébaultitself the 481*3049b2f5SQuentin Thébault.Fn VNET_SYSCTL_ARG req arg1 482*3049b2f5SQuentin Thébaultis provided that will translate the 483*3049b2f5SQuentin Thébault.Fa arg1 484*3049b2f5SQuentin Thébaultargument to the correct memory address in the virtual network stack 485*3049b2f5SQuentin Thébaultcontext of the caller. 486*3049b2f5SQuentin Thébault.\" ------------------------------------------------------------ 487*3049b2f5SQuentin Thébault.Sh SEE ALSO 488*3049b2f5SQuentin Thébault.Xr jail 2 , 489*3049b2f5SQuentin Thébault.Xr kvm 3 , 490*3049b2f5SQuentin Thébault.Xr EVENTHANDLER 9 , 491*3049b2f5SQuentin Thébault.\" .Xr pcpu 9 , 492*3049b2f5SQuentin Thébault.Xr KASSERT 9 , 493*3049b2f5SQuentin Thébault.Xr sysctl 9 494*3049b2f5SQuentin Thébault.\" .Xr SYSINIT 9 495*3049b2f5SQuentin Thébault.Pp 496*3049b2f5SQuentin ThébaultMarko Zec, Implementing a Clonable Network Stack in the FreeBSD Kernel, 497*3049b2f5SQuentin ThébaultUSENIX ATC'03, June 2003, Boston 498*3049b2f5SQuentin Thébault.Sh HISTORY 499*3049b2f5SQuentin ThébaultThe virtual network stack implementation first appeared in 500*3049b2f5SQuentin Thébault.Fx 8.0 . 501*3049b2f5SQuentin Thébault.Sh AUTHORS 502*3049b2f5SQuentin Thébault.An -nosplit 503*3049b2f5SQuentin ThébaultThe 504*3049b2f5SQuentin Thébault.Nm 505*3049b2f5SQuentin Thébaultframework was designed and implemented at the University of Zagreb by 506*3049b2f5SQuentin Thébault.An Marko Zec 507*3049b2f5SQuentin Thébaultunder sponsorship of the FreeBSD Foundation and NLnet Foundation, 508*3049b2f5SQuentin Thébaultand later extended and refined by 509*3049b2f5SQuentin Thébault.An Bjoern A. Zeeb 510*3049b2f5SQuentin Thébault(also under FreeBSD Foundation sponsorship), and 511*3049b2f5SQuentin Thébault.An Robert Watson . 512*3049b2f5SQuentin Thébault.Pp 513*3049b2f5SQuentin ThébaultThis manual page was written by 514*3049b2f5SQuentin Thébault.An Bjoern A. Zeeb, CK Software GmbH, 515*3049b2f5SQuentin Thébaultunder sponsorship from the FreeBSD Foundation. 516