2 .\" Copyright (c) 2010 The FreeBSD Foundation
3 .\" All rights reserved.
5 .\" This documentation was written by CK Software GmbH under sponsorship from
6 .\" the FreeBSD Foundation.
8 .\" Redistribution and use in source and binary forms, with or without
9 .\" modification, are permitted provided that the following conditions
11 .\" 1. Redistributions of source code must retain the above copyright
12 .\" notice, this list of conditions and the following disclaimer.
13 .\" 2. Redistributions in binary form must reproduce the above copyright
14 .\" notice, this list of conditions and the following disclaimer in the
15 .\" documentation and/or other materials provided with the distribution.
17 .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 .Nd "network subsystem virtualization infrastructure"
39 .Cd "options VNET_DEBUG"
42 .\"------------------------------------------------------------
43 .Ss "Constants and Global Variables"
49 .Vt extern struct vnet *vnet0;
50 .\"------------------------------------------------------------
51 .Ss "Variable Declaration"
68 .Fo VNET_DEFINE_STATIC
73 #define V_name VNET(name)
75 .\" ------------------------------------------------------------
76 .Ss "Virtual Instance Selection"
102 .Fo CURVNET_SET_QUIET
108 .Fo VNET_ITERATOR_DECL
115 .\" ------------------------------------------------------------
119 .Fn VNET_LIST_RUNLOCK
120 .Fn VNET_LIST_RLOCK_NOSLEEP
121 .Fn VNET_LIST_RUNLOCK_NOSLEEP
122 .\" ------------------------------------------------------------
123 .Ss "Startup and Teardown Functions"
137 .Fa "enum sysinit_sub_id subsystem"
138 .Fa "enum sysinit_elem_order order"
139 .Fa "sysinit_cfunc_t func"
140 .Fa "const void *arg"
145 .Fa "enum sysinit_sub_id subsystem"
146 .Fa "enum sysinit_elem_order order"
147 .Fa "sysinit_cfunc_t func"
148 .Fa "const void *arg"
150 .\" ------------------------------------------------------------
153 .Fo VNET_GLOBAL_EVENTHANDLER_REGISTER
154 .Fa "const char *name"
160 .Fo VNET_GLOBAL_EVENTHANDLER_REGISTER_TAG
161 .Fa "eventhandler_tag tag"
162 .Fa "const char *name"
167 .\" ------------------------------------------------------------
168 .Ss "Sysctl Handling"
170 .Fa parent nbr name access ptr val descr
173 .Fa parent nbr name access ptr arg handler fmt descr
175 .Fo SYSCTL_VNET_STRING
176 .Fa parent nbr name access arg len descr
178 .Fo SYSCTL_VNET_STRUCT
179 .Fa parent nbr name access ptr type descr
182 .Fa parent nbr name access ptr val descr
187 .\" ------------------------------------------------------------
190 is the name of a technique to virtualize the network stack.
191 The basic idea is to change global resources most notably variables into
192 per network stack resources and have functions, sysctls, eventhandlers,
193 etc. access and handle them in the context of the correct instance.
194 Each (virtual) network stack is attached to a
198 being the unrestricted default network stack of the base system.
200 The global defines for
206 to access internals for debugging reasons.
207 .\" ------------------------------------------------------------
208 .Ss "Variable Declaration"
210 Variables are virtualized by using the
212 macro rather than writing them out as
214 One can still use static initialization, e.g.,
216 .Dl Li VNET_DEFINE(int, foo) = 1;
218 Variables declared with the static keyword can use the
219 .Fn VNET_DEFINE_STATIC
222 .Dl Li VNET_DEFINE_STATIC(SLIST_HEAD(, bar), bars);
224 Static initialization is not possible when the virtualized variable
225 would need to be referenced, e.g., with
226 .Dq TAILQ_HEAD_INITIALIZER() .
229 based initialization function must be used.
231 External variables have to be declared using the
234 In either case the convention is to define another macro,
235 that is then used throughout the implementation to access that variable.
236 The variable name is usually prefixed by
238 to express that it is virtualized.
241 macro will then translate accesses to that variable to the copy of the
242 currently selected instance (see the
243 .Sx "Virtual instance selection"
246 .Dl Li #define V_name VNET(name)
249 Do not confuse this with the convention used by
254 macro returns the offset within the memory region of the virtual network
256 It is usually only used with
259 .\" ------------------------------------------------------------
260 .Ss "Virtual Instance Selection"
262 There are three different places where the current virtual
263 network stack pointer is stored and can be taken from:
264 .Bl -enum -offset indent
268 .Dl "(struct prison *)->pr_vnet"
270 For convenience the following macros are provided:
271 .Bd -literal -compact -offset indent
272 .Fn CRED_TO_VNET "struct ucred *"
273 .Fn TD_TO_VNET "struct thread *"
274 .Fn P_TO_VNET "struct proc *"
279 .Dl "(struct socket *)->so_vnet"
283 .Dl "(struct ifnet *)->if_vnet"
287 In addition the currently active instance is cached in
288 .Dq "curthread->td_vnet"
289 which is usually only accessed through the
294 To set the correct context of the current virtual network instance, use the
297 .Fn CURVNET_SET_QUIET
300 .Fn CURVNET_SET_QUIET
301 version will not record vnet recursions in case the kernel was compiled
303 .Cd "options VNET_DEBUG"
304 and should thus only be used in well known cases, where recursion is
306 Both macros will save the previous state on the stack and it must be restored
312 As the previous state is saved on the stack, you cannot have multiple
314 calls in the same block.
317 As the previous state is saved on the stack, a
319 call has to be in the same block as the
321 call or in a subblock with the same idea of the saved instances as the
325 As each macro is a set of operations and, as previously explained, cannot
326 be put into its own block when defined, one cannot conditionally set
327 the current vnet context.
331 .Bd -literal -offset indent
337 .Bd -literal -offset indent
345 Sometimes one needs to loop over all virtual instances, for example to update
346 virtual from global state, to run a function from a
348 for each instance, etc.
350 .Fn VNET_ITERATOR_DECL
354 The former macro defines the variable that iterates over the loop,
355 and the latter loops over all of the virtual network stack instances.
358 for how to savely traverse the list of all virtual instances.
363 macro provides a safe way to check whether the currently active instance is the
364 unrestricted default network stack of the base system
370 macro provides a way to conditionally add assertions that are only active with
372 compiled in and either
373 .Cd "options VNET_DEBUG"
375 .Cd "options INVARIANTS"
377 It uses the same semantics as
379 .\" ------------------------------------------------------------
382 For public access to the list of virtual network stack instances
385 macro, read locks are provided.
386 Macros are used to abstract from the actual type of the locks.
387 If a caller may sleep while traversing the list, it must use the
390 .Fn VNET_LIST_RUNLOCK
392 Otherwise, the caller can use
393 .Fn VNET_LIST_RLOCK_NOSLEEP
395 .Fn VNET_LIST_RUNLOCK_NOSLEEP .
396 .\" ------------------------------------------------------------
397 .Ss "Startup and Teardown Functions"
399 To start or tear down a virtual network stack instance the internal
404 are provided and called from the jail framework.
405 They run the publicly provided methods to handle network stack
406 startup and teardown.
408 For public control, the system startup interface has been enhanced
409 to not only handle a system boot but to also handle a virtual
410 network stack startup and teardown.
411 To the base system the
415 macros look exactly as if there were no virtual network stack.
418 is not compiled in they are compiled to the standard
421 In addition to that they are run for each virtual network stack
422 when starting or, in reverse order, when shutting down.
423 .\" ------------------------------------------------------------
426 Eventhandlers can be handled in two ways:
428 .Bl -enum -offset indent -compact
432 returned in each virtual instance and properly free the eventhandlers
433 on teardown using those, or
435 use one eventhandler that will iterate over all virtual network
439 For the first case one can just use the normal
441 functions, while for the second case the
442 .Fn VNET_GLOBAL_EVENTHANDLER_REGISTER
444 .Fn VNET_GLOBAL_EVENTHANDLER_REGISTER_TAG
447 .Fn VNET_GLOBAL_EVENTHANDLER_REGISTER_TAG
448 takes an extra first argument that will carry the
451 Eventhandlers registered with either of these will not run
455 will be called from an internal iterator function for each vnet.
456 Both macros can only be used for eventhandlers that do not take
457 additional arguments, as the variadic arguments from an
458 .Xr EVENTHANDLER_INVOKE 9
459 call will be ignored.
460 .\" ------------------------------------------------------------
461 .Ss "Sysctl Handling"
465 can be virtualized by using one of the
469 They take the same arguments as the standard
471 functions, with the only difference, that the
473 argument has to be passed as
477 so that the variable can be selected from the correct memory
478 region of the virtual network stack instance of the caller.
480 For the very rare case a sysctl handler function would want to
484 .Fn VNET_SYSCTL_ARG req arg1
485 is provided that will translate the
487 argument to the correct memory address in the virtual network stack
488 context of the caller.
489 .\" ------------------------------------------------------------
499 Marko Zec, Implementing a Clonable Network Stack in the FreeBSD Kernel,
500 USENIX ATC'03, June 2003, Boston
502 The virtual network stack implementation first appeared in
508 framework was designed and implemented at the University of Zagreb by
510 under sponsorship of the FreeBSD Foundation and NLnet Foundation,
511 and later extended and refined by
513 (also under FreeBSD Foundation sponsorship), and
516 This manual page was written by
517 .An Bjoern A. Zeeb, CK Software GmbH,
518 under sponsorship from the FreeBSD Foundation.