xref: /freebsd/share/man/man9/vnet.9 (revision 00492fb846a59a3ca77525c626bcd3631fb2ecc0)
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.\"
31*00492fb8SNavdeep Parhar.Dd December 10, 2020
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
41*00492fb8SNavdeep Parhar.In net/vnet.h
429ca24f65SCraig Rodrigues.\"------------------------------------------------------------
439ca24f65SCraig Rodrigues.Ss "Constants and Global Variables"
449ca24f65SCraig Rodrigues.\"
459ca24f65SCraig Rodrigues.Dv VNET_SETNAME
469ca24f65SCraig Rodrigues.\"	"set_vnet"
479ca24f65SCraig Rodrigues.Dv VNET_SYMPREFIX
489ca24f65SCraig Rodrigues.\"	"vnet_entry_"
499ca24f65SCraig Rodrigues.Vt extern struct vnet *vnet0;
509ca24f65SCraig Rodrigues.\"------------------------------------------------------------
519ca24f65SCraig Rodrigues.Ss "Variable Declaration"
529ca24f65SCraig Rodrigues.Fo VNET
539ca24f65SCraig Rodrigues.Fa "name"
549ca24f65SCraig Rodrigues.Fc
559ca24f65SCraig Rodrigues.\"
569ca24f65SCraig Rodrigues.Fo VNET_NAME
579ca24f65SCraig Rodrigues.Fa "name"
589ca24f65SCraig Rodrigues.Fc
599ca24f65SCraig Rodrigues.\"
609ca24f65SCraig Rodrigues.Fo VNET_DECLARE
619ca24f65SCraig Rodrigues.Fa "type" "name"
629ca24f65SCraig Rodrigues.Fc
639ca24f65SCraig Rodrigues.\"
649ca24f65SCraig Rodrigues.Fo VNET_DEFINE
659ca24f65SCraig Rodrigues.Fa "type" "name"
669ca24f65SCraig Rodrigues.Fc
679ca24f65SCraig Rodrigues.\"
68fceba23fSAndrew Turner.Fo VNET_DEFINE_STATIC
69fceba23fSAndrew Turner.Fa "type" "name"
70fceba23fSAndrew Turner.Fc
71fceba23fSAndrew Turner.\"
729ca24f65SCraig Rodrigues.Bd -literal
739ca24f65SCraig Rodrigues#define	V_name	VNET(name)
749ca24f65SCraig Rodrigues.Ed
759ca24f65SCraig Rodrigues.\" ------------------------------------------------------------
769ca24f65SCraig Rodrigues.Ss "Virtual Instance Selection"
779ca24f65SCraig Rodrigues.\"
789ca24f65SCraig Rodrigues.Fo CRED_TO_VNET
799ca24f65SCraig Rodrigues.Fa "struct ucred *"
809ca24f65SCraig Rodrigues.Fc
819ca24f65SCraig Rodrigues.\"
829ca24f65SCraig Rodrigues.Fo TD_TO_VNET
839ca24f65SCraig Rodrigues.Fa "struct thread *"
849ca24f65SCraig Rodrigues.Fc
859ca24f65SCraig Rodrigues.\"
869ca24f65SCraig Rodrigues.Fo P_TO_VNET
879ca24f65SCraig Rodrigues.Fa "struct proc *"
889ca24f65SCraig Rodrigues.Fc
899ca24f65SCraig Rodrigues.\"
909ca24f65SCraig Rodrigues.Fo IS_DEFAULT_VNET
919ca24f65SCraig Rodrigues.Fa "struct vnet *"
929ca24f65SCraig Rodrigues.Fc
939ca24f65SCraig Rodrigues.\"
949ca24f65SCraig Rodrigues.Fo VNET_ASSERT
959ca24f65SCraig Rodrigues.Fa exp msg
969ca24f65SCraig Rodrigues.Fc
979ca24f65SCraig Rodrigues.\"
989ca24f65SCraig Rodrigues.Fo CURVNET_SET
999ca24f65SCraig Rodrigues.Fa "struct vnet *"
1009ca24f65SCraig Rodrigues.Fc
1019ca24f65SCraig Rodrigues.\"
1029ca24f65SCraig Rodrigues.Fo CURVNET_SET_QUIET
1039ca24f65SCraig Rodrigues.Fa "struct vnet *"
1049ca24f65SCraig Rodrigues.Fc
1059ca24f65SCraig Rodrigues.\"
1061e9469d1SChristian Brueffer.Fn CURVNET_RESTORE
1079ca24f65SCraig Rodrigues.\"
1089ca24f65SCraig Rodrigues.Fo VNET_ITERATOR_DECL
1099ca24f65SCraig Rodrigues.Fa "struct vnet *"
1109ca24f65SCraig Rodrigues.Fc
1119ca24f65SCraig Rodrigues.\"
1129ca24f65SCraig Rodrigues.Fo VNET_FOREACH
1139ca24f65SCraig Rodrigues.Fa "struct vnet *"
1149ca24f65SCraig Rodrigues.Fc
1159ca24f65SCraig Rodrigues.\" ------------------------------------------------------------
1169ca24f65SCraig Rodrigues.Ss "Locking"
1179ca24f65SCraig Rodrigues.\"
1181e9469d1SChristian Brueffer.Fn VNET_LIST_RLOCK
1191e9469d1SChristian Brueffer.Fn VNET_LIST_RUNLOCK
1201e9469d1SChristian Brueffer.Fn VNET_LIST_RLOCK_NOSLEEP
1211e9469d1SChristian Brueffer.Fn VNET_LIST_RUNLOCK_NOSLEEP
1229ca24f65SCraig Rodrigues.\" ------------------------------------------------------------
1239ca24f65SCraig Rodrigues.Ss "Startup and Teardown Functions"
1249ca24f65SCraig Rodrigues.\"
1259ca24f65SCraig Rodrigues.Ft "struct vnet *"
1269ca24f65SCraig Rodrigues.Fo vnet_alloc
1279ca24f65SCraig Rodrigues.Fa void
1289ca24f65SCraig Rodrigues.Fc
1299ca24f65SCraig Rodrigues.\"
1309ca24f65SCraig Rodrigues.Ft void
1319ca24f65SCraig Rodrigues.Fo vnet_destroy
1329ca24f65SCraig Rodrigues.Fa "struct vnet *"
1339ca24f65SCraig Rodrigues.Fc
1349ca24f65SCraig Rodrigues.\"
1359ca24f65SCraig Rodrigues.Fo VNET_SYSINIT
1369ca24f65SCraig Rodrigues.Fa ident
1379ca24f65SCraig Rodrigues.Fa "enum sysinit_sub_id subsystem"
1389ca24f65SCraig Rodrigues.Fa "enum sysinit_elem_order order"
1399ca24f65SCraig Rodrigues.Fa "sysinit_cfunc_t func"
1409ca24f65SCraig Rodrigues.Fa "const void *arg"
1419ca24f65SCraig Rodrigues.Fc
1429ca24f65SCraig Rodrigues.\"
1439ca24f65SCraig Rodrigues.Fo VNET_SYSUNINIT
1449ca24f65SCraig Rodrigues.Fa ident
1459ca24f65SCraig Rodrigues.Fa "enum sysinit_sub_id subsystem"
1469ca24f65SCraig Rodrigues.Fa "enum sysinit_elem_order order"
1479ca24f65SCraig Rodrigues.Fa "sysinit_cfunc_t func"
1489ca24f65SCraig Rodrigues.Fa "const void *arg"
1499ca24f65SCraig Rodrigues.Fc
1509ca24f65SCraig Rodrigues.\" ------------------------------------------------------------
1519ca24f65SCraig Rodrigues.Ss "Eventhandlers"
1529ca24f65SCraig Rodrigues.\"
1539ca24f65SCraig Rodrigues.Fo VNET_GLOBAL_EVENTHANDLER_REGISTER
1549ca24f65SCraig Rodrigues.Fa "const char *name"
1559ca24f65SCraig Rodrigues.Fa "void *func"
1569ca24f65SCraig Rodrigues.Fa "void *arg"
1579ca24f65SCraig Rodrigues.Fa "int priority"
1589ca24f65SCraig Rodrigues.Fc
1599ca24f65SCraig Rodrigues.\"
1609ca24f65SCraig Rodrigues.Fo VNET_GLOBAL_EVENTHANDLER_REGISTER_TAG
1619ca24f65SCraig Rodrigues.Fa "eventhandler_tag tag"
1629ca24f65SCraig Rodrigues.Fa "const char *name"
1639ca24f65SCraig Rodrigues.Fa "void *func"
1649ca24f65SCraig Rodrigues.Fa "void *arg"
1659ca24f65SCraig Rodrigues.Fa "int priority"
1669ca24f65SCraig Rodrigues.Fc
1679ca24f65SCraig Rodrigues.\" ------------------------------------------------------------
1689ca24f65SCraig Rodrigues.Ss "Sysctl Handling"
1699ca24f65SCraig Rodrigues.Fo SYSCTL_VNET_INT
1709ca24f65SCraig Rodrigues.Fa parent nbr name access ptr val descr
1719ca24f65SCraig Rodrigues.Fc
1729ca24f65SCraig Rodrigues.Fo SYSCTL_VNET_PROC
1739ca24f65SCraig Rodrigues.Fa parent nbr name access ptr arg handler fmt descr
1749ca24f65SCraig Rodrigues.Fc
1759ca24f65SCraig Rodrigues.Fo SYSCTL_VNET_STRING
1769ca24f65SCraig Rodrigues.Fa parent nbr name access arg len descr
1779ca24f65SCraig Rodrigues.Fc
1789ca24f65SCraig Rodrigues.Fo SYSCTL_VNET_STRUCT
1799ca24f65SCraig Rodrigues.Fa parent nbr name access ptr type descr
1809ca24f65SCraig Rodrigues.Fc
1819ca24f65SCraig Rodrigues.Fo SYSCTL_VNET_UINT
1829ca24f65SCraig Rodrigues.Fa parent nbr name access ptr val descr
1839ca24f65SCraig Rodrigues.Fc
1849ca24f65SCraig Rodrigues.Fo VNET_SYSCTL_ARG
1859ca24f65SCraig Rodrigues.Fa req arg1
1869ca24f65SCraig Rodrigues.Fc
1879ca24f65SCraig Rodrigues.\" ------------------------------------------------------------
1889ca24f65SCraig Rodrigues.Sh DESCRIPTION
1899ca24f65SCraig Rodrigues.Nm
1909ca24f65SCraig Rodriguesis the name of a technique to virtualize the network stack.
1919ca24f65SCraig RodriguesThe basic idea is to change global resources most notably variables into
1929ca24f65SCraig Rodriguesper network stack resources and have functions, sysctls, eventhandlers,
1939ca24f65SCraig Rodriguesetc. access and handle them in the context of the correct instance.
1949ca24f65SCraig RodriguesEach (virtual) network stack is attached to a
1959ca24f65SCraig Rodrigues.Em prison ,
1969ca24f65SCraig Rodrigueswith
1979ca24f65SCraig Rodrigues.Vt vnet0
1989ca24f65SCraig Rodriguesbeing the unrestricted default network stack of the base system.
1999ca24f65SCraig Rodrigues.Pp
2009ca24f65SCraig RodriguesThe global defines for
2019ca24f65SCraig Rodrigues.Dv VNET_SETNAME
2029ca24f65SCraig Rodriguesand
2039ca24f65SCraig Rodrigues.Dv VNET_SYMPREFIX
2049ca24f65SCraig Rodriguesare shared with
2059ca24f65SCraig Rodrigues.Xr kvm 3
2069ca24f65SCraig Rodriguesto access internals for debugging reasons.
2079ca24f65SCraig Rodrigues.\" ------------------------------------------------------------
2089ca24f65SCraig Rodrigues.Ss "Variable Declaration"
2099ca24f65SCraig Rodrigues.\"
2109ca24f65SCraig RodriguesVariables are virtualized by using the
2119ca24f65SCraig Rodrigues.Fn VNET_DEFINE
2129ca24f65SCraig Rodriguesmacro rather than writing them out as
2139ca24f65SCraig Rodrigues.Em type name .
214fceba23fSAndrew TurnerOne can still use static initialization, e.g.,
2159ca24f65SCraig Rodrigues.Pp
216fceba23fSAndrew Turner.Dl Li VNET_DEFINE(int, foo) = 1;
217fceba23fSAndrew Turner.Pp
218fceba23fSAndrew TurnerVariables declared with the static keyword can use the
219fceba23fSAndrew Turner.Fn VNET_DEFINE_STATIC
220fceba23fSAndrew Turnermacro, e.g.,
221fceba23fSAndrew Turner.Pp
222fceba23fSAndrew Turner.Dl Li VNET_DEFINE_STATIC(SLIST_HEAD(, bar), bars);
2239ca24f65SCraig Rodrigues.Pp
2249ca24f65SCraig RodriguesStatic initialization is not possible when the virtualized variable
2259ca24f65SCraig Rodrigueswould need to be referenced, e.g., with
2269ca24f65SCraig Rodrigues.Dq TAILQ_HEAD_INITIALIZER() .
2279ca24f65SCraig RodriguesIn that case a
2289ca24f65SCraig Rodrigues.Fn VNET_SYSINIT
2299ca24f65SCraig Rodriguesbased initialization function must be used.
2309ca24f65SCraig Rodrigues.Pp
2319ca24f65SCraig RodriguesExternal variables have to be declared using the
2329ca24f65SCraig Rodrigues.Fn VNET_DECLARE
2339ca24f65SCraig Rodriguesmacro.
2349ca24f65SCraig RodriguesIn either case the convention is to define another macro,
2359ca24f65SCraig Rodriguesthat is then used throughout the implementation to access that variable.
2369ca24f65SCraig RodriguesThe variable name is usually prefixed by
2379ca24f65SCraig Rodrigues.Em V_
2389ca24f65SCraig Rodriguesto express that it is virtualized.
2399ca24f65SCraig RodriguesThe
2409ca24f65SCraig Rodrigues.Fn VNET
2419ca24f65SCraig Rodriguesmacro will then translate accesses to that variable to the copy of the
2429ca24f65SCraig Rodriguescurrently selected instance (see the
2439ca24f65SCraig Rodrigues.Sx "Virtual instance selection"
2449ca24f65SCraig Rodriguessection):
2459ca24f65SCraig Rodrigues.Pp
2469ca24f65SCraig Rodrigues.Dl Li #define	V_name	VNET(name)
2479ca24f65SCraig Rodrigues.Pp
2489ca24f65SCraig Rodrigues.Em NOTE:
2499ca24f65SCraig RodriguesDo not confuse this with the convention used by
2509ca24f65SCraig Rodrigues.Xr VFS 9 .
2519ca24f65SCraig Rodrigues.Pp
2529ca24f65SCraig RodriguesThe
2539ca24f65SCraig Rodrigues.Fn VNET_NAME
2549ca24f65SCraig Rodriguesmacro returns the offset within the memory region of the virtual network
2559ca24f65SCraig Rodriguesstack instance.
2569ca24f65SCraig RodriguesIt is usually only used with
2579ca24f65SCraig Rodrigues.Fn SYSCTL_VNET_*
2589ca24f65SCraig Rodriguesmacros.
2599ca24f65SCraig Rodrigues.\" ------------------------------------------------------------
2609ca24f65SCraig Rodrigues.Ss "Virtual Instance Selection"
2619ca24f65SCraig Rodrigues.\"
2629ca24f65SCraig RodriguesThere are three different places where the current virtual
2639ca24f65SCraig Rodriguesnetwork stack pointer is stored and can be taken from:
2649ca24f65SCraig Rodrigues.Bl -enum -offset indent
2659ca24f65SCraig Rodrigues.It
2669ca24f65SCraig Rodriguesa
2679ca24f65SCraig Rodrigues.Em prison :
2689ca24f65SCraig Rodrigues.Dl "(struct prison *)->pr_vnet"
2699ca24f65SCraig Rodrigues.Pp
2709ca24f65SCraig RodriguesFor convenience the following macros are provided:
2719ca24f65SCraig Rodrigues.Bd -literal -compact -offset indent
2729ca24f65SCraig Rodrigues.Fn CRED_TO_VNET "struct ucred *"
2739ca24f65SCraig Rodrigues.Fn TD_TO_VNET "struct thread *"
2749ca24f65SCraig Rodrigues.Fn P_TO_VNET "struct proc *"
2759ca24f65SCraig Rodrigues.Ed
2769ca24f65SCraig Rodrigues.It
2779ca24f65SCraig Rodriguesa
2789ca24f65SCraig Rodrigues.Em socket :
2799ca24f65SCraig Rodrigues.Dl "(struct socket *)->so_vnet"
2809ca24f65SCraig Rodrigues.It
2819ca24f65SCraig Rodriguesan
2829ca24f65SCraig Rodrigues.Em interface :
2839ca24f65SCraig Rodrigues.Dl "(struct ifnet *)->if_vnet"
2849ca24f65SCraig Rodrigues.El
2859ca24f65SCraig Rodrigues.Pp
2869ca24f65SCraig Rodrigues.\"
2879ca24f65SCraig RodriguesIn addition the currently active instance is cached in
2889ca24f65SCraig Rodrigues.Dq "curthread->td_vnet"
2899ca24f65SCraig Rodrigueswhich is usually only accessed through the
2909ca24f65SCraig Rodrigues.Dv curvnet
2919ca24f65SCraig Rodriguesmacro.
2929ca24f65SCraig Rodrigues.Pp
2939ca24f65SCraig Rodrigues.\"
2949ca24f65SCraig RodriguesTo set the correct context of the current virtual network instance, use the
2959ca24f65SCraig Rodrigues.Fn CURVNET_SET
2969ca24f65SCraig Rodriguesor
2979ca24f65SCraig Rodrigues.Fn CURVNET_SET_QUIET
2989ca24f65SCraig Rodriguesmacros.
2999ca24f65SCraig RodriguesThe
3009ca24f65SCraig Rodrigues.Fn CURVNET_SET_QUIET
3019ca24f65SCraig Rodriguesversion will not record vnet recursions in case the kernel was compiled
3029ca24f65SCraig Rodrigueswith
3039ca24f65SCraig Rodrigues.Cd "options VNET_DEBUG"
3049ca24f65SCraig Rodriguesand should thus only be used in well known cases, where recursion is
3059ca24f65SCraig Rodriguesunavoidable.
3069ca24f65SCraig RodriguesBoth macros will save the previous state on the stack and it must be restored
3079ca24f65SCraig Rodrigueswith the
3089ca24f65SCraig Rodrigues.Fn CURVNET_RESTORE
3099ca24f65SCraig Rodriguesmacro.
3109ca24f65SCraig Rodrigues.Pp
3119ca24f65SCraig Rodrigues.Em NOTE:
3129ca24f65SCraig RodriguesAs the previous state is saved on the stack, you cannot have multiple
3139ca24f65SCraig Rodrigues.Fn CURVNET_SET
3149ca24f65SCraig Rodriguescalls in the same block.
3159ca24f65SCraig Rodrigues.Pp
3169ca24f65SCraig Rodrigues.Em NOTE:
3179ca24f65SCraig RodriguesAs the previous state is saved on the stack, a
3189ca24f65SCraig Rodrigues.Fn CURVNET_RESTORE
3199ca24f65SCraig Rodriguescall has to be in the same block as the
3209ca24f65SCraig Rodrigues.Fn CURVNET_SET
3219ca24f65SCraig Rodriguescall or in a subblock with the same idea of the saved instances as the
3229ca24f65SCraig Rodriguesouter block.
3239ca24f65SCraig Rodrigues.Pp
3249ca24f65SCraig Rodrigues.Em NOTE:
3259ca24f65SCraig RodriguesAs each macro is a set of operations and, as previously explained, cannot
3269ca24f65SCraig Rodriguesbe put into its own block when defined, one cannot conditionally set
3279ca24f65SCraig Rodriguesthe current vnet context.
3289ca24f65SCraig RodriguesThe following will
3299ca24f65SCraig Rodrigues.Em not
3309ca24f65SCraig Rodrigueswork:
3319ca24f65SCraig Rodrigues.Bd -literal -offset indent
3329ca24f65SCraig Rodriguesif (condition)
3339ca24f65SCraig Rodrigues	CURVNET_SET(vnet);
3349ca24f65SCraig Rodrigues.Ed
3359ca24f65SCraig Rodrigues.Pp
3369ca24f65SCraig Rodriguesnor would this work:
3379ca24f65SCraig Rodrigues.Bd -literal -offset indent
3389ca24f65SCraig Rodriguesif (condition) {
3399ca24f65SCraig Rodrigues	CURVNET_SET(vnet);
3409ca24f65SCraig Rodrigues}
3419ca24f65SCraig RodriguesCURVNET_RESTORE();
3429ca24f65SCraig Rodrigues.Ed
3439ca24f65SCraig Rodrigues.Pp
3449ca24f65SCraig Rodrigues.\"
3459ca24f65SCraig RodriguesSometimes one needs to loop over all virtual instances, for example to update
3469ca24f65SCraig Rodriguesvirtual from global state, to run a function from a
3479ca24f65SCraig Rodrigues.Xr callout 9
3489ca24f65SCraig Rodriguesfor each instance, etc.
3499ca24f65SCraig RodriguesFor those cases the
3509ca24f65SCraig Rodrigues.Fn VNET_ITERATOR_DECL
3519ca24f65SCraig Rodriguesand
3529ca24f65SCraig Rodrigues.Fn VNET_FOREACH
3539ca24f65SCraig Rodriguesmacros are provided.
3549ca24f65SCraig RodriguesThe former macro defines the variable that iterates over the loop,
3559ca24f65SCraig Rodriguesand the latter loops over all of the virtual network stack instances.
3569ca24f65SCraig RodriguesSee
3579ca24f65SCraig Rodrigues.Sx "Locking"
3589ca24f65SCraig Rodriguesfor how to savely traverse the list of all virtual instances.
3599ca24f65SCraig Rodrigues.Pp
3609ca24f65SCraig Rodrigues.\"
3619ca24f65SCraig RodriguesThe
3629ca24f65SCraig Rodrigues.Fn IS_DEFAULT_VNET
3639ca24f65SCraig Rodriguesmacro provides a safe way to check whether the currently active instance is the
3649ca24f65SCraig Rodriguesunrestricted default network stack of the base system
3659ca24f65SCraig Rodrigues.Pq Vt vnet0 .
3669ca24f65SCraig Rodrigues.Pp
3679ca24f65SCraig Rodrigues.\"
3689ca24f65SCraig RodriguesThe
3699ca24f65SCraig Rodrigues.Fn VNET_ASSERT
3709ca24f65SCraig Rodriguesmacro provides a way to conditionally add assertions that are only active with
3719ca24f65SCraig Rodrigues.Cd "options VIMAGE"
3729ca24f65SCraig Rodriguescompiled in and either
3739ca24f65SCraig Rodrigues.Cd "options VNET_DEBUG"
3749ca24f65SCraig Rodriguesor
3759ca24f65SCraig Rodrigues.Cd "options INVARIANTS"
3769ca24f65SCraig Rodriguesenabled as well.
3779ca24f65SCraig RodriguesIt uses the same semantics as
3789ca24f65SCraig Rodrigues.Xr KASSERT 9 .
3799ca24f65SCraig Rodrigues.\" ------------------------------------------------------------
3809ca24f65SCraig Rodrigues.Ss "Locking"
3819ca24f65SCraig Rodrigues.\"
3829ca24f65SCraig RodriguesFor public access to the list of virtual network stack instances
3839ca24f65SCraig Rodriguese.g., by the
3849ca24f65SCraig Rodrigues.Fn VNET_FOREACH
3859ca24f65SCraig Rodriguesmacro, read locks are provided.
3869ca24f65SCraig RodriguesMacros are used to abstract from the actual type of the locks.
3879ca24f65SCraig RodriguesIf a caller may sleep while traversing the list, it must use the
3889ca24f65SCraig Rodrigues.Fn VNET_LIST_RLOCK
3899ca24f65SCraig Rodriguesand
3909ca24f65SCraig Rodrigues.Fn VNET_LIST_RUNLOCK
3919ca24f65SCraig Rodriguesmacros.
3929ca24f65SCraig RodriguesOtherwise, the caller can use
3939ca24f65SCraig Rodrigues.Fn VNET_LIST_RLOCK_NOSLEEP
3949ca24f65SCraig Rodriguesand
3959ca24f65SCraig Rodrigues.Fn VNET_LIST_RUNLOCK_NOSLEEP .
3969ca24f65SCraig Rodrigues.\" ------------------------------------------------------------
3979ca24f65SCraig Rodrigues.Ss "Startup and Teardown Functions"
3989ca24f65SCraig Rodrigues.\"
3999ca24f65SCraig RodriguesTo start or tear down a virtual network stack instance the internal
4009ca24f65SCraig Rodriguesfunctions
4019ca24f65SCraig Rodrigues.Fn vnet_alloc
4029ca24f65SCraig Rodriguesand
4039ca24f65SCraig Rodrigues.Fn vnet_destroy
4049ca24f65SCraig Rodriguesare provided and called from the jail framework.
4059ca24f65SCraig RodriguesThey run the publicly provided methods to handle network stack
4069ca24f65SCraig Rodriguesstartup and teardown.
4079ca24f65SCraig Rodrigues.Pp
4089ca24f65SCraig RodriguesFor public control, the system startup interface has been enhanced
4099ca24f65SCraig Rodriguesto not only handle a system boot but to also handle a virtual
4109ca24f65SCraig Rodriguesnetwork stack startup and teardown.
4119ca24f65SCraig RodriguesTo the base system the
4129ca24f65SCraig Rodrigues.Fn VNET_SYSINIT
4139ca24f65SCraig Rodriguesand
4149ca24f65SCraig Rodrigues.Fn VNET_SYSUNINIT
4159ca24f65SCraig Rodriguesmacros look exactly as if there were no virtual network stack.
4169ca24f65SCraig RodriguesIn fact, if
4179ca24f65SCraig Rodrigues.Cd "options VIMAGE"
4189ca24f65SCraig Rodriguesis not compiled in they are compiled to the standard
4199ca24f65SCraig Rodrigues.Fn SYSINIT
4209ca24f65SCraig Rodriguesmacros.
4219ca24f65SCraig RodriguesIn addition to that they are run for each virtual network stack
4229ca24f65SCraig Rodrigueswhen starting or, in reverse order, when shutting down.
4239ca24f65SCraig Rodrigues.\" ------------------------------------------------------------
4249ca24f65SCraig Rodrigues.Ss "Eventhandlers"
4259ca24f65SCraig Rodrigues.\"
4269ca24f65SCraig RodriguesEventhandlers can be handled in two ways:
4279ca24f65SCraig Rodrigues.Pp
4289ca24f65SCraig Rodrigues.Bl -enum -offset indent -compact
4299ca24f65SCraig Rodrigues.It
4309ca24f65SCraig Rodriguessave the
4319ca24f65SCraig Rodrigues.Em tags
4329ca24f65SCraig Rodriguesreturned in each virtual instance and properly free the eventhandlers
4339ca24f65SCraig Rodrigueson teardown using those, or
4349ca24f65SCraig Rodrigues.It
4359ca24f65SCraig Rodriguesuse one eventhandler that will iterate over all virtual network
4369ca24f65SCraig Rodriguesstack instances.
4379ca24f65SCraig Rodrigues.El
4389ca24f65SCraig Rodrigues.Pp
4399ca24f65SCraig RodriguesFor the first case one can just use the normal
4409ca24f65SCraig Rodrigues.Xr EVENTHANDLER 9
4419ca24f65SCraig Rodriguesfunctions, while for the second case the
4429ca24f65SCraig Rodrigues.Fn VNET_GLOBAL_EVENTHANDLER_REGISTER
4439ca24f65SCraig Rodriguesand
4449ca24f65SCraig Rodrigues.Fn VNET_GLOBAL_EVENTHANDLER_REGISTER_TAG
4459ca24f65SCraig Rodriguesmacros are provided.
4469ca24f65SCraig RodriguesThese differ in that
4479ca24f65SCraig Rodrigues.Fn VNET_GLOBAL_EVENTHANDLER_REGISTER_TAG
4489ca24f65SCraig Rodriguestakes an extra first argument that will carry the
4499ca24f65SCraig Rodrigues.Fa "tag"
4509ca24f65SCraig Rodriguesupon return.
4519ca24f65SCraig RodriguesEventhandlers registered with either of these will not run
4529ca24f65SCraig Rodrigues.Fa func
4539ca24f65SCraig Rodriguesdirectly but
4549ca24f65SCraig Rodrigues.Fa func
4559ca24f65SCraig Rodrigueswill be called from an internal iterator function for each vnet.
4569ca24f65SCraig RodriguesBoth macros can only be used for eventhandlers that do not take
4579ca24f65SCraig Rodriguesadditional arguments, as the variadic arguments from an
4589ca24f65SCraig Rodrigues.Xr EVENTHANDLER_INVOKE 9
4599ca24f65SCraig Rodriguescall will be ignored.
4609ca24f65SCraig Rodrigues.\" ------------------------------------------------------------
4619ca24f65SCraig Rodrigues.Ss "Sysctl Handling"
4629ca24f65SCraig Rodrigues.\"
4639ca24f65SCraig RodriguesA
4649ca24f65SCraig Rodrigues.Xr sysctl 9
4659ca24f65SCraig Rodriguescan be virtualized by using one of the
4669ca24f65SCraig Rodrigues.Fn SYSCTL_VNET_*
4679ca24f65SCraig Rodriguesmacros.
4689ca24f65SCraig Rodrigues.Pp
4699ca24f65SCraig RodriguesThey take the same arguments as the standard
4709ca24f65SCraig Rodrigues.Xr sysctl 9
4719ca24f65SCraig Rodriguesfunctions, with the only difference, that the
4729ca24f65SCraig Rodrigues.Fa ptr
4739ca24f65SCraig Rodriguesargument has to be passed as
4749ca24f65SCraig Rodrigues.Ql &VNET_NAME(foo)
4759ca24f65SCraig Rodriguesinstead of
4769ca24f65SCraig Rodrigues.Ql &foo
4779ca24f65SCraig Rodriguesso that the variable can be selected from the correct memory
4789ca24f65SCraig Rodriguesregion of the virtual network stack instance of the caller.
4799ca24f65SCraig Rodrigues.Pp
4809ca24f65SCraig RodriguesFor the very rare case a sysctl handler function would want to
4819ca24f65SCraig Rodrigueshandle
4829ca24f65SCraig Rodrigues.Fa arg1
4839ca24f65SCraig Rodriguesitself the
4849ca24f65SCraig Rodrigues.Fn VNET_SYSCTL_ARG req arg1
4859ca24f65SCraig Rodriguesis provided that will translate the
4869ca24f65SCraig Rodrigues.Fa arg1
4879ca24f65SCraig Rodriguesargument to the correct memory address in the virtual network stack
4889ca24f65SCraig Rodriguescontext of the caller.
4899ca24f65SCraig Rodrigues.\" ------------------------------------------------------------
4909ca24f65SCraig Rodrigues.Sh SEE ALSO
4919ca24f65SCraig Rodrigues.Xr jail 2 ,
4929ca24f65SCraig Rodrigues.Xr kvm 3 ,
4939ca24f65SCraig Rodrigues.Xr EVENTHANDLER 9 ,
4949ca24f65SCraig Rodrigues.\" .Xr pcpu 9 ,
4959ca24f65SCraig Rodrigues.Xr KASSERT 9 ,
4969ca24f65SCraig Rodrigues.Xr sysctl 9
4979ca24f65SCraig Rodrigues.\" .Xr SYSINIT 9
498cf103f7eSMarko Zec.Pp
499cf103f7eSMarko ZecMarko Zec, Implementing a Clonable Network Stack in the FreeBSD Kernel,
500cf103f7eSMarko ZecUSENIX ATC'03, June 2003, Boston
5019ca24f65SCraig Rodrigues.Sh HISTORY
5029ca24f65SCraig RodriguesThe virtual network stack implementation first appeared in
5039ca24f65SCraig Rodrigues.Fx 8.0 .
5049ca24f65SCraig Rodrigues.Sh AUTHORS
505cf103f7eSMarko Zec.An -nosplit
506cf103f7eSMarko ZecThe
507cf103f7eSMarko Zec.Nm
5089fbbfa5dSEd Masteframework was designed and implemented at the University of Zagreb by
5099fbbfa5dSEd Maste.An Marko Zec
5109fbbfa5dSEd Masteunder sponsorship of the FreeBSD Foundation and NLnet Foundation,
511cf103f7eSMarko Zecand later extended and refined by
512cf103f7eSMarko Zec.An Bjoern A. Zeeb
5139fbbfa5dSEd Maste(also under FreeBSD Foundation sponsorship), and
5149fbbfa5dSEd Maste.An Robert Watson .
515cf103f7eSMarko Zec.Pp
5169ca24f65SCraig RodriguesThis manual page was written by
5179ca24f65SCraig Rodrigues.An Bjoern A. Zeeb, CK Software GmbH,
5189ca24f65SCraig Rodriguesunder sponsorship from the FreeBSD Foundation.
519