xref: /freebsd/share/man/man9/vnet.9 (revision 1e9469d188355e00de1c3811dd2f983d2f5a47d9)
19ca24f65SCraig Rodrigues.\"-
29ca24f65SCraig Rodrigues.\" Copyright (c) 2010 The FreeBSD Foundation
39ca24f65SCraig Rodrigues.\" All rights reserved.
49ca24f65SCraig Rodrigues.\"
59ca24f65SCraig Rodrigues.\" This documentation was written by CK Software GmbH under sponsorship from
69ca24f65SCraig Rodrigues.\" the FreeBSD Foundation.
79ca24f65SCraig Rodrigues.\"
89ca24f65SCraig Rodrigues.\" Redistribution and use in source and binary forms, with or without
99ca24f65SCraig Rodrigues.\" modification, are permitted provided that the following conditions
109ca24f65SCraig Rodrigues.\" are met:
119ca24f65SCraig Rodrigues.\" 1. Redistributions of source code must retain the above copyright
129ca24f65SCraig Rodrigues.\"    notice, this list of conditions and the following disclaimer.
139ca24f65SCraig Rodrigues.\" 2. Redistributions in binary form must reproduce the above copyright
149ca24f65SCraig Rodrigues.\"    notice, this list of conditions and the following disclaimer in the
159ca24f65SCraig Rodrigues.\"    documentation and/or other materials provided with the distribution.
169ca24f65SCraig Rodrigues.\"
179ca24f65SCraig Rodrigues.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
189ca24f65SCraig Rodrigues.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
199ca24f65SCraig Rodrigues.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
209ca24f65SCraig Rodrigues.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
219ca24f65SCraig Rodrigues.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
229ca24f65SCraig Rodrigues.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
239ca24f65SCraig Rodrigues.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
249ca24f65SCraig Rodrigues.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
259ca24f65SCraig Rodrigues.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
269ca24f65SCraig Rodrigues.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
279ca24f65SCraig Rodrigues.\" SUCH DAMAGE.
289ca24f65SCraig Rodrigues.\"
299ca24f65SCraig Rodrigues.\" $FreeBSD$
309ca24f65SCraig Rodrigues.\"
319ca24f65SCraig Rodrigues.Dd November 20, 2014
329ca24f65SCraig Rodrigues.Dt VNET 9
339ca24f65SCraig Rodrigues.Os
349ca24f65SCraig Rodrigues.Sh NAME
359ca24f65SCraig Rodrigues.Nm VNET
369ca24f65SCraig Rodrigues.Nd "network subsystem virtualization infrastructure"
379ca24f65SCraig Rodrigues.Sh SYNOPSIS
389ca24f65SCraig Rodrigues.Cd "options VIMAGE"
399ca24f65SCraig Rodrigues.Cd "options VNET_DEBUG"
409ca24f65SCraig Rodrigues.Pp
419ca24f65SCraig Rodrigues.In sys/vnet.h
429ca24f65SCraig Rodrigues.Pp
439ca24f65SCraig Rodrigues.\"------------------------------------------------------------
449ca24f65SCraig Rodrigues.Ss "Constants and Global Variables"
459ca24f65SCraig Rodrigues.\"
469ca24f65SCraig Rodrigues.Dv VNET_SETNAME
479ca24f65SCraig Rodrigues.\"	"set_vnet"
489ca24f65SCraig Rodrigues.Dv VNET_SYMPREFIX
499ca24f65SCraig Rodrigues.\"	"vnet_entry_"
509ca24f65SCraig Rodrigues.Vt extern struct vnet *vnet0;
519ca24f65SCraig Rodrigues.\"------------------------------------------------------------
529ca24f65SCraig Rodrigues.Ss "Variable Declaration"
539ca24f65SCraig Rodrigues.Fo VNET
549ca24f65SCraig Rodrigues.Fa "name"
559ca24f65SCraig Rodrigues.Fc
569ca24f65SCraig Rodrigues.\"
579ca24f65SCraig Rodrigues.Fo VNET_NAME
589ca24f65SCraig Rodrigues.Fa "name"
599ca24f65SCraig Rodrigues.Fc
609ca24f65SCraig Rodrigues.\"
619ca24f65SCraig Rodrigues.Fo VNET_DECLARE
629ca24f65SCraig Rodrigues.Fa "type" "name"
639ca24f65SCraig Rodrigues.Fc
649ca24f65SCraig Rodrigues.\"
659ca24f65SCraig Rodrigues.Fo VNET_DEFINE
669ca24f65SCraig Rodrigues.Fa "type" "name"
679ca24f65SCraig Rodrigues.Fc
689ca24f65SCraig Rodrigues.\"
699ca24f65SCraig Rodrigues.Bd -literal
709ca24f65SCraig Rodrigues#define	V_name	VNET(name)
719ca24f65SCraig Rodrigues.Ed
729ca24f65SCraig Rodrigues.\" ------------------------------------------------------------
739ca24f65SCraig Rodrigues.Ss "Virtual Instance Selection"
749ca24f65SCraig Rodrigues.\"
759ca24f65SCraig Rodrigues.Fo CRED_TO_VNET
769ca24f65SCraig Rodrigues.Fa "struct ucred *"
779ca24f65SCraig Rodrigues.Fc
789ca24f65SCraig Rodrigues.\"
799ca24f65SCraig Rodrigues.Fo TD_TO_VNET
809ca24f65SCraig Rodrigues.Fa "struct thread *"
819ca24f65SCraig Rodrigues.Fc
829ca24f65SCraig Rodrigues.\"
839ca24f65SCraig Rodrigues.Fo P_TO_VNET
849ca24f65SCraig Rodrigues.Fa "struct proc *"
859ca24f65SCraig Rodrigues.Fc
869ca24f65SCraig Rodrigues.\"
879ca24f65SCraig Rodrigues.Fo IS_DEFAULT_VNET
889ca24f65SCraig Rodrigues.Fa "struct vnet *"
899ca24f65SCraig Rodrigues.Fc
909ca24f65SCraig Rodrigues.\"
919ca24f65SCraig Rodrigues.Fo VNET_ASSERT
929ca24f65SCraig Rodrigues.Fa exp msg
939ca24f65SCraig Rodrigues.Fc
949ca24f65SCraig Rodrigues.\"
959ca24f65SCraig Rodrigues.Fo CURVNET_SET
969ca24f65SCraig Rodrigues.Fa "struct vnet *"
979ca24f65SCraig Rodrigues.Fc
989ca24f65SCraig Rodrigues.\"
999ca24f65SCraig Rodrigues.Fo CURVNET_SET_QUIET
1009ca24f65SCraig Rodrigues.Fa "struct vnet *"
1019ca24f65SCraig Rodrigues.Fc
1029ca24f65SCraig Rodrigues.\"
103*1e9469d1SChristian Brueffer.Fn CURVNET_RESTORE
1049ca24f65SCraig Rodrigues.\"
1059ca24f65SCraig Rodrigues.Fo VNET_ITERATOR_DECL
1069ca24f65SCraig Rodrigues.Fa "struct vnet *"
1079ca24f65SCraig Rodrigues.Fc
1089ca24f65SCraig Rodrigues.\"
1099ca24f65SCraig Rodrigues.Fo VNET_FOREACH
1109ca24f65SCraig Rodrigues.Fa "struct vnet *"
1119ca24f65SCraig Rodrigues.Fc
1129ca24f65SCraig Rodrigues.\" ------------------------------------------------------------
1139ca24f65SCraig Rodrigues.Ss "Locking"
1149ca24f65SCraig Rodrigues.\"
115*1e9469d1SChristian Brueffer.Fn VNET_LIST_RLOCK
116*1e9469d1SChristian Brueffer.Fn VNET_LIST_RUNLOCK
117*1e9469d1SChristian Brueffer.Fn VNET_LIST_RLOCK_NOSLEEP
118*1e9469d1SChristian Brueffer.Fn VNET_LIST_RUNLOCK_NOSLEEP
1199ca24f65SCraig Rodrigues.\" ------------------------------------------------------------
1209ca24f65SCraig Rodrigues.Ss "Startup and Teardown Functions"
1219ca24f65SCraig Rodrigues.\"
1229ca24f65SCraig Rodrigues.Ft "struct vnet *"
1239ca24f65SCraig Rodrigues.Fo vnet_alloc
1249ca24f65SCraig Rodrigues.Fa void
1259ca24f65SCraig Rodrigues.Fc
1269ca24f65SCraig Rodrigues.\"
1279ca24f65SCraig Rodrigues.Ft void
1289ca24f65SCraig Rodrigues.Fo vnet_destroy
1299ca24f65SCraig Rodrigues.Fa "struct vnet *"
1309ca24f65SCraig Rodrigues.Fc
1319ca24f65SCraig Rodrigues.\"
1329ca24f65SCraig Rodrigues.Fo VNET_SYSINIT
1339ca24f65SCraig Rodrigues.Fa ident
1349ca24f65SCraig Rodrigues.Fa "enum sysinit_sub_id subsystem"
1359ca24f65SCraig Rodrigues.Fa "enum sysinit_elem_order order"
1369ca24f65SCraig Rodrigues.Fa "sysinit_cfunc_t func"
1379ca24f65SCraig Rodrigues.Fa "const void *arg"
1389ca24f65SCraig Rodrigues.Fc
1399ca24f65SCraig Rodrigues.\"
1409ca24f65SCraig Rodrigues.Fo VNET_SYSUNINIT
1419ca24f65SCraig Rodrigues.Fa ident
1429ca24f65SCraig Rodrigues.Fa "enum sysinit_sub_id subsystem"
1439ca24f65SCraig Rodrigues.Fa "enum sysinit_elem_order order"
1449ca24f65SCraig Rodrigues.Fa "sysinit_cfunc_t func"
1459ca24f65SCraig Rodrigues.Fa "const void *arg"
1469ca24f65SCraig Rodrigues.Fc
1479ca24f65SCraig Rodrigues.\" ------------------------------------------------------------
1489ca24f65SCraig Rodrigues.Ss "Eventhandlers"
1499ca24f65SCraig Rodrigues.\"
1509ca24f65SCraig Rodrigues.Fo VNET_GLOBAL_EVENTHANDLER_REGISTER
1519ca24f65SCraig Rodrigues.Fa "const char *name"
1529ca24f65SCraig Rodrigues.Fa "void *func"
1539ca24f65SCraig Rodrigues.Fa "void *arg"
1549ca24f65SCraig Rodrigues.Fa "int priority"
1559ca24f65SCraig Rodrigues.Fc
1569ca24f65SCraig Rodrigues.\"
1579ca24f65SCraig Rodrigues.Fo VNET_GLOBAL_EVENTHANDLER_REGISTER_TAG
1589ca24f65SCraig Rodrigues.Fa "eventhandler_tag tag"
1599ca24f65SCraig Rodrigues.Fa "const char *name"
1609ca24f65SCraig Rodrigues.Fa "void *func"
1619ca24f65SCraig Rodrigues.Fa "void *arg"
1629ca24f65SCraig Rodrigues.Fa "int priority"
1639ca24f65SCraig Rodrigues.Fc
1649ca24f65SCraig Rodrigues.\" ------------------------------------------------------------
1659ca24f65SCraig Rodrigues.Ss "Sysctl Handling"
1669ca24f65SCraig Rodrigues.Fo SYSCTL_VNET_INT
1679ca24f65SCraig Rodrigues.Fa parent nbr name access ptr val descr
1689ca24f65SCraig Rodrigues.Fc
1699ca24f65SCraig Rodrigues.Fo SYSCTL_VNET_PROC
1709ca24f65SCraig Rodrigues.Fa parent nbr name access ptr arg handler fmt descr
1719ca24f65SCraig Rodrigues.Fc
1729ca24f65SCraig Rodrigues.Fo SYSCTL_VNET_STRING
1739ca24f65SCraig Rodrigues.Fa parent nbr name access arg len descr
1749ca24f65SCraig Rodrigues.Fc
1759ca24f65SCraig Rodrigues.Fo SYSCTL_VNET_STRUCT
1769ca24f65SCraig Rodrigues.Fa parent nbr name access ptr type descr
1779ca24f65SCraig Rodrigues.Fc
1789ca24f65SCraig Rodrigues.Fo SYSCTL_VNET_UINT
1799ca24f65SCraig Rodrigues.Fa parent nbr name access ptr val descr
1809ca24f65SCraig Rodrigues.Fc
1819ca24f65SCraig Rodrigues.Fo VNET_SYSCTL_ARG
1829ca24f65SCraig Rodrigues.Fa req arg1
1839ca24f65SCraig Rodrigues.Fc
1849ca24f65SCraig Rodrigues.\" ------------------------------------------------------------
1859ca24f65SCraig Rodrigues.Sh DESCRIPTION
1869ca24f65SCraig Rodrigues.Nm
1879ca24f65SCraig Rodriguesis the name of a technique to virtualize the network stack.
1889ca24f65SCraig RodriguesThe basic idea is to change global resources most notably variables into
1899ca24f65SCraig Rodriguesper network stack resources and have functions, sysctls, eventhandlers,
1909ca24f65SCraig Rodriguesetc. access and handle them in the context of the correct instance.
1919ca24f65SCraig RodriguesEach (virtual) network stack is attached to a
1929ca24f65SCraig Rodrigues.Em prison ,
1939ca24f65SCraig Rodrigueswith
1949ca24f65SCraig Rodrigues.Vt vnet0
1959ca24f65SCraig Rodriguesbeing the unrestricted default network stack of the base system.
1969ca24f65SCraig Rodrigues.Pp
1979ca24f65SCraig RodriguesThe global defines for
1989ca24f65SCraig Rodrigues.Dv VNET_SETNAME
1999ca24f65SCraig Rodriguesand
2009ca24f65SCraig Rodrigues.Dv VNET_SYMPREFIX
2019ca24f65SCraig Rodriguesare shared with
2029ca24f65SCraig Rodrigues.Xr kvm 3
2039ca24f65SCraig Rodriguesto access internals for debugging reasons.
2049ca24f65SCraig Rodrigues.\" ------------------------------------------------------------
2059ca24f65SCraig Rodrigues.Ss "Variable Declaration"
2069ca24f65SCraig Rodrigues.\"
2079ca24f65SCraig RodriguesVariables are virtualized by using the
2089ca24f65SCraig Rodrigues.Fn VNET_DEFINE
2099ca24f65SCraig Rodriguesmacro rather than writing them out as
2109ca24f65SCraig Rodrigues.Em type name .
2119ca24f65SCraig RodriguesOne can still use static initialization or storage class specifiers, e.g.,
2129ca24f65SCraig Rodrigues.Pp
2139ca24f65SCraig Rodrigues.Dl Li static VNET_DEFINE(int, foo) = 1;
2149ca24f65SCraig Rodriguesor
2159ca24f65SCraig Rodrigues.Dl Li static VNET_DEFINE(SLIST_HEAD(, bar), bars);
2169ca24f65SCraig Rodrigues.Pp
2179ca24f65SCraig RodriguesStatic initialization is not possible when the virtualized variable
2189ca24f65SCraig Rodrigueswould need to be referenced, e.g., with
2199ca24f65SCraig Rodrigues.Dq TAILQ_HEAD_INITIALIZER() .
2209ca24f65SCraig RodriguesIn that case a
2219ca24f65SCraig Rodrigues.Fn VNET_SYSINIT
2229ca24f65SCraig Rodriguesbased initialization function must be used.
2239ca24f65SCraig Rodrigues.Pp
2249ca24f65SCraig RodriguesExternal variables have to be declared using the
2259ca24f65SCraig Rodrigues.Fn VNET_DECLARE
2269ca24f65SCraig Rodriguesmacro.
2279ca24f65SCraig RodriguesIn either case the convention is to define another macro,
2289ca24f65SCraig Rodriguesthat is then used throughout the implementation to access that variable.
2299ca24f65SCraig RodriguesThe variable name is usually prefixed by
2309ca24f65SCraig Rodrigues.Em V_
2319ca24f65SCraig Rodriguesto express that it is virtualized.
2329ca24f65SCraig RodriguesThe
2339ca24f65SCraig Rodrigues.Fn VNET
2349ca24f65SCraig Rodriguesmacro will then translate accesses to that variable to the copy of the
2359ca24f65SCraig Rodriguescurrently selected instance (see the
2369ca24f65SCraig Rodrigues.Sx "Virtual instance selection"
2379ca24f65SCraig Rodriguessection):
2389ca24f65SCraig Rodrigues.Pp
2399ca24f65SCraig Rodrigues.Dl Li #define	V_name	VNET(name)
2409ca24f65SCraig Rodrigues.Pp
2419ca24f65SCraig Rodrigues.Em NOTE:
2429ca24f65SCraig RodriguesDo not confuse this with the convention used by
2439ca24f65SCraig Rodrigues.Xr VFS 9 .
2449ca24f65SCraig Rodrigues.Pp
2459ca24f65SCraig RodriguesThe
2469ca24f65SCraig Rodrigues.Fn VNET_NAME
2479ca24f65SCraig Rodriguesmacro returns the offset within the memory region of the virtual network
2489ca24f65SCraig Rodriguesstack instance.
2499ca24f65SCraig RodriguesIt is usually only used with
2509ca24f65SCraig Rodrigues.Fn SYSCTL_VNET_*
2519ca24f65SCraig Rodriguesmacros.
2529ca24f65SCraig Rodrigues.\" ------------------------------------------------------------
2539ca24f65SCraig Rodrigues.Ss "Virtual Instance Selection"
2549ca24f65SCraig Rodrigues.\"
2559ca24f65SCraig RodriguesThere are three different places where the current virtual
2569ca24f65SCraig Rodriguesnetwork stack pointer is stored and can be taken from:
2579ca24f65SCraig Rodrigues.Bl -enum -offset indent
2589ca24f65SCraig Rodrigues.It
2599ca24f65SCraig Rodriguesa
2609ca24f65SCraig Rodrigues.Em prison :
2619ca24f65SCraig Rodrigues.Dl "(struct prison *)->pr_vnet"
2629ca24f65SCraig Rodrigues.Pp
2639ca24f65SCraig RodriguesFor convenience the following macros are provided:
2649ca24f65SCraig Rodrigues.Bd -literal -compact -offset indent
2659ca24f65SCraig Rodrigues.Fn CRED_TO_VNET "struct ucred *"
2669ca24f65SCraig Rodrigues.Fn TD_TO_VNET "struct thread *"
2679ca24f65SCraig Rodrigues.Fn P_TO_VNET "struct proc *"
2689ca24f65SCraig Rodrigues.Ed
2699ca24f65SCraig Rodrigues.It
2709ca24f65SCraig Rodriguesa
2719ca24f65SCraig Rodrigues.Em socket :
2729ca24f65SCraig Rodrigues.Dl "(struct socket *)->so_vnet"
2739ca24f65SCraig Rodrigues.It
2749ca24f65SCraig Rodriguesan
2759ca24f65SCraig Rodrigues.Em interface :
2769ca24f65SCraig Rodrigues.Dl "(struct ifnet *)->if_vnet"
2779ca24f65SCraig Rodrigues.El
2789ca24f65SCraig Rodrigues.Pp
2799ca24f65SCraig Rodrigues.\"
2809ca24f65SCraig RodriguesIn addition the currently active instance is cached in
2819ca24f65SCraig Rodrigues.Dq "curthread->td_vnet"
2829ca24f65SCraig Rodrigueswhich is usually only accessed through the
2839ca24f65SCraig Rodrigues.Dv curvnet
2849ca24f65SCraig Rodriguesmacro.
2859ca24f65SCraig Rodrigues.Pp
2869ca24f65SCraig Rodrigues.\"
2879ca24f65SCraig RodriguesTo set the correct context of the current virtual network instance, use the
2889ca24f65SCraig Rodrigues.Fn CURVNET_SET
2899ca24f65SCraig Rodriguesor
2909ca24f65SCraig Rodrigues.Fn CURVNET_SET_QUIET
2919ca24f65SCraig Rodriguesmacros.
2929ca24f65SCraig RodriguesThe
2939ca24f65SCraig Rodrigues.Fn CURVNET_SET_QUIET
2949ca24f65SCraig Rodriguesversion will not record vnet recursions in case the kernel was compiled
2959ca24f65SCraig Rodrigueswith
2969ca24f65SCraig Rodrigues.Cd "options VNET_DEBUG"
2979ca24f65SCraig Rodriguesand should thus only be used in well known cases, where recursion is
2989ca24f65SCraig Rodriguesunavoidable.
2999ca24f65SCraig RodriguesBoth macros will save the previous state on the stack and it must be restored
3009ca24f65SCraig Rodrigueswith the
3019ca24f65SCraig Rodrigues.Fn CURVNET_RESTORE
3029ca24f65SCraig Rodriguesmacro.
3039ca24f65SCraig Rodrigues.Pp
3049ca24f65SCraig Rodrigues.Em NOTE:
3059ca24f65SCraig RodriguesAs the previous state is saved on the stack, you cannot have multiple
3069ca24f65SCraig Rodrigues.Fn CURVNET_SET
3079ca24f65SCraig Rodriguescalls in the same block.
3089ca24f65SCraig Rodrigues.Pp
3099ca24f65SCraig Rodrigues.Em NOTE:
3109ca24f65SCraig RodriguesAs the previous state is saved on the stack, a
3119ca24f65SCraig Rodrigues.Fn CURVNET_RESTORE
3129ca24f65SCraig Rodriguescall has to be in the same block as the
3139ca24f65SCraig Rodrigues.Fn CURVNET_SET
3149ca24f65SCraig Rodriguescall or in a subblock with the same idea of the saved instances as the
3159ca24f65SCraig Rodriguesouter block.
3169ca24f65SCraig Rodrigues.Pp
3179ca24f65SCraig Rodrigues.Em NOTE:
3189ca24f65SCraig RodriguesAs each macro is a set of operations and, as previously explained, cannot
3199ca24f65SCraig Rodriguesbe put into its own block when defined, one cannot conditionally set
3209ca24f65SCraig Rodriguesthe current vnet context.
3219ca24f65SCraig RodriguesThe following will
3229ca24f65SCraig Rodrigues.Em not
3239ca24f65SCraig Rodrigueswork:
3249ca24f65SCraig Rodrigues.Bd -literal -offset indent
3259ca24f65SCraig Rodriguesif (condition)
3269ca24f65SCraig Rodrigues	CURVNET_SET(vnet);
3279ca24f65SCraig Rodrigues.Ed
3289ca24f65SCraig Rodrigues.Pp
3299ca24f65SCraig Rodriguesnor would this work:
3309ca24f65SCraig Rodrigues.Bd -literal -offset indent
3319ca24f65SCraig Rodriguesif (condition) {
3329ca24f65SCraig Rodrigues	CURVNET_SET(vnet);
3339ca24f65SCraig Rodrigues}
3349ca24f65SCraig RodriguesCURVNET_RESTORE();
3359ca24f65SCraig Rodrigues.Ed
3369ca24f65SCraig Rodrigues.Pp
3379ca24f65SCraig Rodrigues.\"
3389ca24f65SCraig RodriguesSometimes one needs to loop over all virtual instances, for example to update
3399ca24f65SCraig Rodriguesvirtual from global state, to run a function from a
3409ca24f65SCraig Rodrigues.Xr callout 9
3419ca24f65SCraig Rodriguesfor each instance, etc.
3429ca24f65SCraig RodriguesFor those cases the
3439ca24f65SCraig Rodrigues.Fn VNET_ITERATOR_DECL
3449ca24f65SCraig Rodriguesand
3459ca24f65SCraig Rodrigues.Fn VNET_FOREACH
3469ca24f65SCraig Rodriguesmacros are provided.
3479ca24f65SCraig RodriguesThe former macro defines the variable that iterates over the loop,
3489ca24f65SCraig Rodriguesand the latter loops over all of the virtual network stack instances.
3499ca24f65SCraig RodriguesSee
3509ca24f65SCraig Rodrigues.Sx "Locking"
3519ca24f65SCraig Rodriguesfor how to savely traverse the list of all virtual instances.
3529ca24f65SCraig Rodrigues.Pp
3539ca24f65SCraig Rodrigues.\"
3549ca24f65SCraig RodriguesThe
3559ca24f65SCraig Rodrigues.Fn IS_DEFAULT_VNET
3569ca24f65SCraig Rodriguesmacro provides a safe way to check whether the currently active instance is the
3579ca24f65SCraig Rodriguesunrestricted default network stack of the base system
3589ca24f65SCraig Rodrigues.Pq Vt vnet0 .
3599ca24f65SCraig Rodrigues.Pp
3609ca24f65SCraig Rodrigues.\"
3619ca24f65SCraig RodriguesThe
3629ca24f65SCraig Rodrigues.Fn VNET_ASSERT
3639ca24f65SCraig Rodriguesmacro provides a way to conditionally add assertions that are only active with
3649ca24f65SCraig Rodrigues.Cd "options VIMAGE"
3659ca24f65SCraig Rodriguescompiled in and either
3669ca24f65SCraig Rodrigues.Cd "options VNET_DEBUG"
3679ca24f65SCraig Rodriguesor
3689ca24f65SCraig Rodrigues.Cd "options INVARIANTS"
3699ca24f65SCraig Rodriguesenabled as well.
3709ca24f65SCraig RodriguesIt uses the same semantics as
3719ca24f65SCraig Rodrigues.Xr KASSERT 9 .
3729ca24f65SCraig Rodrigues.\" ------------------------------------------------------------
3739ca24f65SCraig Rodrigues.Ss "Locking"
3749ca24f65SCraig Rodrigues.\"
3759ca24f65SCraig RodriguesFor public access to the list of virtual network stack instances
3769ca24f65SCraig Rodriguese.g., by the
3779ca24f65SCraig Rodrigues.Fn VNET_FOREACH
3789ca24f65SCraig Rodriguesmacro, read locks are provided.
3799ca24f65SCraig RodriguesMacros are used to abstract from the actual type of the locks.
3809ca24f65SCraig RodriguesIf a caller may sleep while traversing the list, it must use the
3819ca24f65SCraig Rodrigues.Fn VNET_LIST_RLOCK
3829ca24f65SCraig Rodriguesand
3839ca24f65SCraig Rodrigues.Fn VNET_LIST_RUNLOCK
3849ca24f65SCraig Rodriguesmacros.
3859ca24f65SCraig RodriguesOtherwise, the caller can use
3869ca24f65SCraig Rodrigues.Fn VNET_LIST_RLOCK_NOSLEEP
3879ca24f65SCraig Rodriguesand
3889ca24f65SCraig Rodrigues.Fn VNET_LIST_RUNLOCK_NOSLEEP .
3899ca24f65SCraig Rodrigues.\" ------------------------------------------------------------
3909ca24f65SCraig Rodrigues.Ss "Startup and Teardown Functions"
3919ca24f65SCraig Rodrigues.\"
3929ca24f65SCraig RodriguesTo start or tear down a virtual network stack instance the internal
3939ca24f65SCraig Rodriguesfunctions
3949ca24f65SCraig Rodrigues.Fn vnet_alloc
3959ca24f65SCraig Rodriguesand
3969ca24f65SCraig Rodrigues.Fn vnet_destroy
3979ca24f65SCraig Rodriguesare provided and called from the jail framework.
3989ca24f65SCraig RodriguesThey run the publicly provided methods to handle network stack
3999ca24f65SCraig Rodriguesstartup and teardown.
4009ca24f65SCraig Rodrigues.Pp
4019ca24f65SCraig RodriguesFor public control, the system startup interface has been enhanced
4029ca24f65SCraig Rodriguesto not only handle a system boot but to also handle a virtual
4039ca24f65SCraig Rodriguesnetwork stack startup and teardown.
4049ca24f65SCraig RodriguesTo the base system the
4059ca24f65SCraig Rodrigues.Fn VNET_SYSINIT
4069ca24f65SCraig Rodriguesand
4079ca24f65SCraig Rodrigues.Fn VNET_SYSUNINIT
4089ca24f65SCraig Rodriguesmacros look exactly as if there were no virtual network stack.
4099ca24f65SCraig RodriguesIn fact, if
4109ca24f65SCraig Rodrigues.Cd "options VIMAGE"
4119ca24f65SCraig Rodriguesis not compiled in they are compiled to the standard
4129ca24f65SCraig Rodrigues.Fn SYSINIT
4139ca24f65SCraig Rodriguesmacros.
4149ca24f65SCraig RodriguesIn addition to that they are run for each virtual network stack
4159ca24f65SCraig Rodrigueswhen starting or, in reverse order, when shutting down.
4169ca24f65SCraig Rodrigues.\" ------------------------------------------------------------
4179ca24f65SCraig Rodrigues.Ss "Eventhandlers"
4189ca24f65SCraig Rodrigues.\"
4199ca24f65SCraig RodriguesEventhandlers can be handled in two ways:
4209ca24f65SCraig Rodrigues.Pp
4219ca24f65SCraig Rodrigues.Bl -enum -offset indent -compact
4229ca24f65SCraig Rodrigues.It
4239ca24f65SCraig Rodriguessave the
4249ca24f65SCraig Rodrigues.Em tags
4259ca24f65SCraig Rodriguesreturned in each virtual instance and properly free the eventhandlers
4269ca24f65SCraig Rodrigueson teardown using those, or
4279ca24f65SCraig Rodrigues.It
4289ca24f65SCraig Rodriguesuse one eventhandler that will iterate over all virtual network
4299ca24f65SCraig Rodriguesstack instances.
4309ca24f65SCraig Rodrigues.El
4319ca24f65SCraig Rodrigues.Pp
4329ca24f65SCraig RodriguesFor the first case one can just use the normal
4339ca24f65SCraig Rodrigues.Xr EVENTHANDLER 9
4349ca24f65SCraig Rodriguesfunctions, while for the second case the
4359ca24f65SCraig Rodrigues.Fn VNET_GLOBAL_EVENTHANDLER_REGISTER
4369ca24f65SCraig Rodriguesand
4379ca24f65SCraig Rodrigues.Fn VNET_GLOBAL_EVENTHANDLER_REGISTER_TAG
4389ca24f65SCraig Rodriguesmacros are provided.
4399ca24f65SCraig RodriguesThese differ in that
4409ca24f65SCraig Rodrigues.Fn VNET_GLOBAL_EVENTHANDLER_REGISTER_TAG
4419ca24f65SCraig Rodriguestakes an extra first argument that will carry the
4429ca24f65SCraig Rodrigues.Fa "tag"
4439ca24f65SCraig Rodriguesupon return.
4449ca24f65SCraig RodriguesEventhandlers registered with either of these will not run
4459ca24f65SCraig Rodrigues.Fa func
4469ca24f65SCraig Rodriguesdirectly but
4479ca24f65SCraig Rodrigues.Fa func
4489ca24f65SCraig Rodrigueswill be called from an internal iterator function for each vnet.
4499ca24f65SCraig RodriguesBoth macros can only be used for eventhandlers that do not take
4509ca24f65SCraig Rodriguesadditional arguments, as the variadic arguments from an
4519ca24f65SCraig Rodrigues.Xr EVENTHANDLER_INVOKE 9
4529ca24f65SCraig Rodriguescall will be ignored.
4539ca24f65SCraig Rodrigues.\" ------------------------------------------------------------
4549ca24f65SCraig Rodrigues.Ss "Sysctl Handling"
4559ca24f65SCraig Rodrigues.\"
4569ca24f65SCraig RodriguesA
4579ca24f65SCraig Rodrigues.Xr sysctl 9
4589ca24f65SCraig Rodriguescan be virtualized by using one of the
4599ca24f65SCraig Rodrigues.Fn SYSCTL_VNET_*
4609ca24f65SCraig Rodriguesmacros.
4619ca24f65SCraig Rodrigues.Pp
4629ca24f65SCraig RodriguesThey take the same arguments as the standard
4639ca24f65SCraig Rodrigues.Xr sysctl 9
4649ca24f65SCraig Rodriguesfunctions, with the only difference, that the
4659ca24f65SCraig Rodrigues.Fa ptr
4669ca24f65SCraig Rodriguesargument has to be passed as
4679ca24f65SCraig Rodrigues.Ql &VNET_NAME(foo)
4689ca24f65SCraig Rodriguesinstead of
4699ca24f65SCraig Rodrigues.Ql &foo
4709ca24f65SCraig Rodriguesso that the variable can be selected from the correct memory
4719ca24f65SCraig Rodriguesregion of the virtual network stack instance of the caller.
4729ca24f65SCraig Rodrigues.Pp
4739ca24f65SCraig RodriguesFor the very rare case a sysctl handler function would want to
4749ca24f65SCraig Rodrigueshandle
4759ca24f65SCraig Rodrigues.Fa arg1
4769ca24f65SCraig Rodriguesitself the
4779ca24f65SCraig Rodrigues.Fn VNET_SYSCTL_ARG req arg1
4789ca24f65SCraig Rodriguesis provided that will translate the
4799ca24f65SCraig Rodrigues.Fa arg1
4809ca24f65SCraig Rodriguesargument to the correct memory address in the virtual network stack
4819ca24f65SCraig Rodriguescontext of the caller.
4829ca24f65SCraig Rodrigues.\" ------------------------------------------------------------
4839ca24f65SCraig Rodrigues.Sh SEE ALSO
4849ca24f65SCraig Rodrigues.Xr jail 2 ,
4859ca24f65SCraig Rodrigues.Xr kvm 3 ,
4869ca24f65SCraig Rodrigues.Xr EVENTHANDLER 9 ,
4879ca24f65SCraig Rodrigues.\" .Xr pcpu 9 ,
4889ca24f65SCraig Rodrigues.Xr KASSERT 9 ,
4899ca24f65SCraig Rodrigues.Xr sysctl 9
4909ca24f65SCraig Rodrigues.\" .Xr SYSINIT 9
4919ca24f65SCraig Rodrigues.Sh HISTORY
4929ca24f65SCraig RodriguesThe virtual network stack implementation first appeared in
4939ca24f65SCraig Rodrigues.Fx 8.0 .
4949ca24f65SCraig Rodrigues.Sh AUTHORS
4959ca24f65SCraig RodriguesThis manual page was written by
4969ca24f65SCraig Rodrigues.An Bjoern A. Zeeb, CK Software GmbH,
4979ca24f65SCraig Rodriguesunder sponsorship from the FreeBSD Foundation.
498