xref: /freebsd/share/man/man9/vnet.9 (revision fa9896e082a1046ff4fbc75fcba4d18d1f2efc19)
19ca24f65SCraig Rodrigues.\"-
29ca24f65SCraig Rodrigues.\" Copyright (c) 2010 The FreeBSD Foundation
39ca24f65SCraig Rodrigues.\"
49ca24f65SCraig Rodrigues.\" This documentation was written by CK Software GmbH under sponsorship from
59ca24f65SCraig Rodrigues.\" the FreeBSD Foundation.
69ca24f65SCraig Rodrigues.\"
79ca24f65SCraig Rodrigues.\" Redistribution and use in source and binary forms, with or without
89ca24f65SCraig Rodrigues.\" modification, are permitted provided that the following conditions
99ca24f65SCraig Rodrigues.\" are met:
109ca24f65SCraig Rodrigues.\" 1. Redistributions of source code must retain the above copyright
119ca24f65SCraig Rodrigues.\"    notice, this list of conditions and the following disclaimer.
129ca24f65SCraig Rodrigues.\" 2. Redistributions in binary form must reproduce the above copyright
139ca24f65SCraig Rodrigues.\"    notice, this list of conditions and the following disclaimer in the
149ca24f65SCraig Rodrigues.\"    documentation and/or other materials provided with the distribution.
159ca24f65SCraig Rodrigues.\"
169ca24f65SCraig Rodrigues.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
179ca24f65SCraig Rodrigues.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
189ca24f65SCraig Rodrigues.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
199ca24f65SCraig Rodrigues.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
209ca24f65SCraig Rodrigues.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
219ca24f65SCraig Rodrigues.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
229ca24f65SCraig Rodrigues.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
239ca24f65SCraig Rodrigues.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
249ca24f65SCraig Rodrigues.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
259ca24f65SCraig Rodrigues.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
269ca24f65SCraig Rodrigues.\" SUCH DAMAGE.
279ca24f65SCraig Rodrigues.\"
28*00492fb8SNavdeep Parhar.Dd December 10, 2020
299ca24f65SCraig Rodrigues.Dt VNET 9
309ca24f65SCraig Rodrigues.Os
319ca24f65SCraig Rodrigues.Sh NAME
329ca24f65SCraig Rodrigues.Nm VNET
339ca24f65SCraig Rodrigues.Nd "network subsystem virtualization infrastructure"
349ca24f65SCraig Rodrigues.Sh SYNOPSIS
359ca24f65SCraig Rodrigues.Cd "options VIMAGE"
369ca24f65SCraig Rodrigues.Cd "options VNET_DEBUG"
379ca24f65SCraig Rodrigues.Pp
38*00492fb8SNavdeep Parhar.In net/vnet.h
399ca24f65SCraig Rodrigues.\"------------------------------------------------------------
409ca24f65SCraig Rodrigues.Ss "Constants and Global Variables"
419ca24f65SCraig Rodrigues.\"
429ca24f65SCraig Rodrigues.Dv VNET_SETNAME
439ca24f65SCraig Rodrigues.\"	"set_vnet"
449ca24f65SCraig Rodrigues.Dv VNET_SYMPREFIX
459ca24f65SCraig Rodrigues.\"	"vnet_entry_"
469ca24f65SCraig Rodrigues.Vt extern struct vnet *vnet0;
479ca24f65SCraig Rodrigues.\"------------------------------------------------------------
489ca24f65SCraig Rodrigues.Ss "Variable Declaration"
499ca24f65SCraig Rodrigues.Fo VNET
509ca24f65SCraig Rodrigues.Fa "name"
519ca24f65SCraig Rodrigues.Fc
529ca24f65SCraig Rodrigues.\"
539ca24f65SCraig Rodrigues.Fo VNET_NAME
549ca24f65SCraig Rodrigues.Fa "name"
559ca24f65SCraig Rodrigues.Fc
569ca24f65SCraig Rodrigues.\"
579ca24f65SCraig Rodrigues.Fo VNET_DECLARE
589ca24f65SCraig Rodrigues.Fa "type" "name"
599ca24f65SCraig Rodrigues.Fc
609ca24f65SCraig Rodrigues.\"
619ca24f65SCraig Rodrigues.Fo VNET_DEFINE
629ca24f65SCraig Rodrigues.Fa "type" "name"
639ca24f65SCraig Rodrigues.Fc
649ca24f65SCraig Rodrigues.\"
65fceba23fSAndrew Turner.Fo VNET_DEFINE_STATIC
66fceba23fSAndrew Turner.Fa "type" "name"
67fceba23fSAndrew Turner.Fc
68fceba23fSAndrew Turner.\"
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.\"
1031e9469d1SChristian 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.\"
1151e9469d1SChristian Brueffer.Fn VNET_LIST_RLOCK
1161e9469d1SChristian Brueffer.Fn VNET_LIST_RUNLOCK
1171e9469d1SChristian Brueffer.Fn VNET_LIST_RLOCK_NOSLEEP
1181e9469d1SChristian 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 .
211fceba23fSAndrew TurnerOne can still use static initialization, e.g.,
2129ca24f65SCraig Rodrigues.Pp
213fceba23fSAndrew Turner.Dl Li VNET_DEFINE(int, foo) = 1;
214fceba23fSAndrew Turner.Pp
215fceba23fSAndrew TurnerVariables declared with the static keyword can use the
216fceba23fSAndrew Turner.Fn VNET_DEFINE_STATIC
217fceba23fSAndrew Turnermacro, e.g.,
218fceba23fSAndrew Turner.Pp
219fceba23fSAndrew Turner.Dl Li VNET_DEFINE_STATIC(SLIST_HEAD(, bar), bars);
2209ca24f65SCraig Rodrigues.Pp
2219ca24f65SCraig RodriguesStatic initialization is not possible when the virtualized variable
2229ca24f65SCraig Rodrigueswould need to be referenced, e.g., with
2239ca24f65SCraig Rodrigues.Dq TAILQ_HEAD_INITIALIZER() .
2249ca24f65SCraig RodriguesIn that case a
2259ca24f65SCraig Rodrigues.Fn VNET_SYSINIT
2269ca24f65SCraig Rodriguesbased initialization function must be used.
2279ca24f65SCraig Rodrigues.Pp
2289ca24f65SCraig RodriguesExternal variables have to be declared using the
2299ca24f65SCraig Rodrigues.Fn VNET_DECLARE
2309ca24f65SCraig Rodriguesmacro.
2319ca24f65SCraig RodriguesIn either case the convention is to define another macro,
2329ca24f65SCraig Rodriguesthat is then used throughout the implementation to access that variable.
2339ca24f65SCraig RodriguesThe variable name is usually prefixed by
2349ca24f65SCraig Rodrigues.Em V_
2359ca24f65SCraig Rodriguesto express that it is virtualized.
2369ca24f65SCraig RodriguesThe
2379ca24f65SCraig Rodrigues.Fn VNET
2389ca24f65SCraig Rodriguesmacro will then translate accesses to that variable to the copy of the
2399ca24f65SCraig Rodriguescurrently selected instance (see the
2409ca24f65SCraig Rodrigues.Sx "Virtual instance selection"
2419ca24f65SCraig Rodriguessection):
2429ca24f65SCraig Rodrigues.Pp
2439ca24f65SCraig Rodrigues.Dl Li #define	V_name	VNET(name)
2449ca24f65SCraig Rodrigues.Pp
2459ca24f65SCraig Rodrigues.Em NOTE:
2469ca24f65SCraig RodriguesDo not confuse this with the convention used by
2479ca24f65SCraig Rodrigues.Xr VFS 9 .
2489ca24f65SCraig Rodrigues.Pp
2499ca24f65SCraig RodriguesThe
2509ca24f65SCraig Rodrigues.Fn VNET_NAME
2519ca24f65SCraig Rodriguesmacro returns the offset within the memory region of the virtual network
2529ca24f65SCraig Rodriguesstack instance.
2539ca24f65SCraig RodriguesIt is usually only used with
2549ca24f65SCraig Rodrigues.Fn SYSCTL_VNET_*
2559ca24f65SCraig Rodriguesmacros.
2569ca24f65SCraig Rodrigues.\" ------------------------------------------------------------
2579ca24f65SCraig Rodrigues.Ss "Virtual Instance Selection"
2589ca24f65SCraig Rodrigues.\"
2599ca24f65SCraig RodriguesThere are three different places where the current virtual
2609ca24f65SCraig Rodriguesnetwork stack pointer is stored and can be taken from:
2619ca24f65SCraig Rodrigues.Bl -enum -offset indent
2629ca24f65SCraig Rodrigues.It
2639ca24f65SCraig Rodriguesa
2649ca24f65SCraig Rodrigues.Em prison :
2659ca24f65SCraig Rodrigues.Dl "(struct prison *)->pr_vnet"
2669ca24f65SCraig Rodrigues.Pp
2679ca24f65SCraig RodriguesFor convenience the following macros are provided:
2689ca24f65SCraig Rodrigues.Bd -literal -compact -offset indent
2699ca24f65SCraig Rodrigues.Fn CRED_TO_VNET "struct ucred *"
2709ca24f65SCraig Rodrigues.Fn TD_TO_VNET "struct thread *"
2719ca24f65SCraig Rodrigues.Fn P_TO_VNET "struct proc *"
2729ca24f65SCraig Rodrigues.Ed
2739ca24f65SCraig Rodrigues.It
2749ca24f65SCraig Rodriguesa
2759ca24f65SCraig Rodrigues.Em socket :
2769ca24f65SCraig Rodrigues.Dl "(struct socket *)->so_vnet"
2779ca24f65SCraig Rodrigues.It
2789ca24f65SCraig Rodriguesan
2799ca24f65SCraig Rodrigues.Em interface :
2809ca24f65SCraig Rodrigues.Dl "(struct ifnet *)->if_vnet"
2819ca24f65SCraig Rodrigues.El
2829ca24f65SCraig Rodrigues.Pp
2839ca24f65SCraig Rodrigues.\"
2849ca24f65SCraig RodriguesIn addition the currently active instance is cached in
2859ca24f65SCraig Rodrigues.Dq "curthread->td_vnet"
2869ca24f65SCraig Rodrigueswhich is usually only accessed through the
2879ca24f65SCraig Rodrigues.Dv curvnet
2889ca24f65SCraig Rodriguesmacro.
2899ca24f65SCraig Rodrigues.Pp
2909ca24f65SCraig Rodrigues.\"
2919ca24f65SCraig RodriguesTo set the correct context of the current virtual network instance, use the
2929ca24f65SCraig Rodrigues.Fn CURVNET_SET
2939ca24f65SCraig Rodriguesor
2949ca24f65SCraig Rodrigues.Fn CURVNET_SET_QUIET
2959ca24f65SCraig Rodriguesmacros.
2969ca24f65SCraig RodriguesThe
2979ca24f65SCraig Rodrigues.Fn CURVNET_SET_QUIET
2989ca24f65SCraig Rodriguesversion will not record vnet recursions in case the kernel was compiled
2999ca24f65SCraig Rodrigueswith
3009ca24f65SCraig Rodrigues.Cd "options VNET_DEBUG"
3019ca24f65SCraig Rodriguesand should thus only be used in well known cases, where recursion is
3029ca24f65SCraig Rodriguesunavoidable.
3039ca24f65SCraig RodriguesBoth macros will save the previous state on the stack and it must be restored
3049ca24f65SCraig Rodrigueswith the
3059ca24f65SCraig Rodrigues.Fn CURVNET_RESTORE
3069ca24f65SCraig Rodriguesmacro.
3079ca24f65SCraig Rodrigues.Pp
3089ca24f65SCraig Rodrigues.Em NOTE:
3099ca24f65SCraig RodriguesAs the previous state is saved on the stack, you cannot have multiple
3109ca24f65SCraig Rodrigues.Fn CURVNET_SET
3119ca24f65SCraig Rodriguescalls in the same block.
3129ca24f65SCraig Rodrigues.Pp
3139ca24f65SCraig Rodrigues.Em NOTE:
3149ca24f65SCraig RodriguesAs the previous state is saved on the stack, a
3159ca24f65SCraig Rodrigues.Fn CURVNET_RESTORE
3169ca24f65SCraig Rodriguescall has to be in the same block as the
3179ca24f65SCraig Rodrigues.Fn CURVNET_SET
3189ca24f65SCraig Rodriguescall or in a subblock with the same idea of the saved instances as the
3199ca24f65SCraig Rodriguesouter block.
3209ca24f65SCraig Rodrigues.Pp
3219ca24f65SCraig Rodrigues.Em NOTE:
3229ca24f65SCraig RodriguesAs each macro is a set of operations and, as previously explained, cannot
3239ca24f65SCraig Rodriguesbe put into its own block when defined, one cannot conditionally set
3249ca24f65SCraig Rodriguesthe current vnet context.
3259ca24f65SCraig RodriguesThe following will
3269ca24f65SCraig Rodrigues.Em not
3279ca24f65SCraig Rodrigueswork:
3289ca24f65SCraig Rodrigues.Bd -literal -offset indent
3299ca24f65SCraig Rodriguesif (condition)
3309ca24f65SCraig Rodrigues	CURVNET_SET(vnet);
3319ca24f65SCraig Rodrigues.Ed
3329ca24f65SCraig Rodrigues.Pp
3339ca24f65SCraig Rodriguesnor would this work:
3349ca24f65SCraig Rodrigues.Bd -literal -offset indent
3359ca24f65SCraig Rodriguesif (condition) {
3369ca24f65SCraig Rodrigues	CURVNET_SET(vnet);
3379ca24f65SCraig Rodrigues}
3389ca24f65SCraig RodriguesCURVNET_RESTORE();
3399ca24f65SCraig Rodrigues.Ed
3409ca24f65SCraig Rodrigues.Pp
3419ca24f65SCraig Rodrigues.\"
3429ca24f65SCraig RodriguesSometimes one needs to loop over all virtual instances, for example to update
3439ca24f65SCraig Rodriguesvirtual from global state, to run a function from a
3449ca24f65SCraig Rodrigues.Xr callout 9
3459ca24f65SCraig Rodriguesfor each instance, etc.
3469ca24f65SCraig RodriguesFor those cases the
3479ca24f65SCraig Rodrigues.Fn VNET_ITERATOR_DECL
3489ca24f65SCraig Rodriguesand
3499ca24f65SCraig Rodrigues.Fn VNET_FOREACH
3509ca24f65SCraig Rodriguesmacros are provided.
3519ca24f65SCraig RodriguesThe former macro defines the variable that iterates over the loop,
3529ca24f65SCraig Rodriguesand the latter loops over all of the virtual network stack instances.
3539ca24f65SCraig RodriguesSee
3549ca24f65SCraig Rodrigues.Sx "Locking"
3559ca24f65SCraig Rodriguesfor how to savely traverse the list of all virtual instances.
3569ca24f65SCraig Rodrigues.Pp
3579ca24f65SCraig Rodrigues.\"
3589ca24f65SCraig RodriguesThe
3599ca24f65SCraig Rodrigues.Fn IS_DEFAULT_VNET
3609ca24f65SCraig Rodriguesmacro provides a safe way to check whether the currently active instance is the
3619ca24f65SCraig Rodriguesunrestricted default network stack of the base system
3629ca24f65SCraig Rodrigues.Pq Vt vnet0 .
3639ca24f65SCraig Rodrigues.Pp
3649ca24f65SCraig Rodrigues.\"
3659ca24f65SCraig RodriguesThe
3669ca24f65SCraig Rodrigues.Fn VNET_ASSERT
3679ca24f65SCraig Rodriguesmacro provides a way to conditionally add assertions that are only active with
3689ca24f65SCraig Rodrigues.Cd "options VIMAGE"
3699ca24f65SCraig Rodriguescompiled in and either
3709ca24f65SCraig Rodrigues.Cd "options VNET_DEBUG"
3719ca24f65SCraig Rodriguesor
3729ca24f65SCraig Rodrigues.Cd "options INVARIANTS"
3739ca24f65SCraig Rodriguesenabled as well.
3749ca24f65SCraig RodriguesIt uses the same semantics as
3759ca24f65SCraig Rodrigues.Xr KASSERT 9 .
3769ca24f65SCraig Rodrigues.\" ------------------------------------------------------------
3779ca24f65SCraig Rodrigues.Ss "Locking"
3789ca24f65SCraig Rodrigues.\"
3799ca24f65SCraig RodriguesFor public access to the list of virtual network stack instances
3809ca24f65SCraig Rodriguese.g., by the
3819ca24f65SCraig Rodrigues.Fn VNET_FOREACH
3829ca24f65SCraig Rodriguesmacro, read locks are provided.
3839ca24f65SCraig RodriguesMacros are used to abstract from the actual type of the locks.
3849ca24f65SCraig RodriguesIf a caller may sleep while traversing the list, it must use the
3859ca24f65SCraig Rodrigues.Fn VNET_LIST_RLOCK
3869ca24f65SCraig Rodriguesand
3879ca24f65SCraig Rodrigues.Fn VNET_LIST_RUNLOCK
3889ca24f65SCraig Rodriguesmacros.
3899ca24f65SCraig RodriguesOtherwise, the caller can use
3909ca24f65SCraig Rodrigues.Fn VNET_LIST_RLOCK_NOSLEEP
3919ca24f65SCraig Rodriguesand
3929ca24f65SCraig Rodrigues.Fn VNET_LIST_RUNLOCK_NOSLEEP .
3939ca24f65SCraig Rodrigues.\" ------------------------------------------------------------
3949ca24f65SCraig Rodrigues.Ss "Startup and Teardown Functions"
3959ca24f65SCraig Rodrigues.\"
3969ca24f65SCraig RodriguesTo start or tear down a virtual network stack instance the internal
3979ca24f65SCraig Rodriguesfunctions
3989ca24f65SCraig Rodrigues.Fn vnet_alloc
3999ca24f65SCraig Rodriguesand
4009ca24f65SCraig Rodrigues.Fn vnet_destroy
4019ca24f65SCraig Rodriguesare provided and called from the jail framework.
4029ca24f65SCraig RodriguesThey run the publicly provided methods to handle network stack
4039ca24f65SCraig Rodriguesstartup and teardown.
4049ca24f65SCraig Rodrigues.Pp
4059ca24f65SCraig RodriguesFor public control, the system startup interface has been enhanced
4069ca24f65SCraig Rodriguesto not only handle a system boot but to also handle a virtual
4079ca24f65SCraig Rodriguesnetwork stack startup and teardown.
4089ca24f65SCraig RodriguesTo the base system the
4099ca24f65SCraig Rodrigues.Fn VNET_SYSINIT
4109ca24f65SCraig Rodriguesand
4119ca24f65SCraig Rodrigues.Fn VNET_SYSUNINIT
4129ca24f65SCraig Rodriguesmacros look exactly as if there were no virtual network stack.
4139ca24f65SCraig RodriguesIn fact, if
4149ca24f65SCraig Rodrigues.Cd "options VIMAGE"
4159ca24f65SCraig Rodriguesis not compiled in they are compiled to the standard
4169ca24f65SCraig Rodrigues.Fn SYSINIT
4179ca24f65SCraig Rodriguesmacros.
4189ca24f65SCraig RodriguesIn addition to that they are run for each virtual network stack
4199ca24f65SCraig Rodrigueswhen starting or, in reverse order, when shutting down.
4209ca24f65SCraig Rodrigues.\" ------------------------------------------------------------
4219ca24f65SCraig Rodrigues.Ss "Eventhandlers"
4229ca24f65SCraig Rodrigues.\"
4239ca24f65SCraig RodriguesEventhandlers can be handled in two ways:
4249ca24f65SCraig Rodrigues.Pp
4259ca24f65SCraig Rodrigues.Bl -enum -offset indent -compact
4269ca24f65SCraig Rodrigues.It
4279ca24f65SCraig Rodriguessave the
4289ca24f65SCraig Rodrigues.Em tags
4299ca24f65SCraig Rodriguesreturned in each virtual instance and properly free the eventhandlers
4309ca24f65SCraig Rodrigueson teardown using those, or
4319ca24f65SCraig Rodrigues.It
4329ca24f65SCraig Rodriguesuse one eventhandler that will iterate over all virtual network
4339ca24f65SCraig Rodriguesstack instances.
4349ca24f65SCraig Rodrigues.El
4359ca24f65SCraig Rodrigues.Pp
4369ca24f65SCraig RodriguesFor the first case one can just use the normal
4379ca24f65SCraig Rodrigues.Xr EVENTHANDLER 9
4389ca24f65SCraig Rodriguesfunctions, while for the second case the
4399ca24f65SCraig Rodrigues.Fn VNET_GLOBAL_EVENTHANDLER_REGISTER
4409ca24f65SCraig Rodriguesand
4419ca24f65SCraig Rodrigues.Fn VNET_GLOBAL_EVENTHANDLER_REGISTER_TAG
4429ca24f65SCraig Rodriguesmacros are provided.
4439ca24f65SCraig RodriguesThese differ in that
4449ca24f65SCraig Rodrigues.Fn VNET_GLOBAL_EVENTHANDLER_REGISTER_TAG
4459ca24f65SCraig Rodriguestakes an extra first argument that will carry the
4469ca24f65SCraig Rodrigues.Fa "tag"
4479ca24f65SCraig Rodriguesupon return.
4489ca24f65SCraig RodriguesEventhandlers registered with either of these will not run
4499ca24f65SCraig Rodrigues.Fa func
4509ca24f65SCraig Rodriguesdirectly but
4519ca24f65SCraig Rodrigues.Fa func
4529ca24f65SCraig Rodrigueswill be called from an internal iterator function for each vnet.
4539ca24f65SCraig RodriguesBoth macros can only be used for eventhandlers that do not take
4549ca24f65SCraig Rodriguesadditional arguments, as the variadic arguments from an
4559ca24f65SCraig Rodrigues.Xr EVENTHANDLER_INVOKE 9
4569ca24f65SCraig Rodriguescall will be ignored.
4579ca24f65SCraig Rodrigues.\" ------------------------------------------------------------
4589ca24f65SCraig Rodrigues.Ss "Sysctl Handling"
4599ca24f65SCraig Rodrigues.\"
4609ca24f65SCraig RodriguesA
4619ca24f65SCraig Rodrigues.Xr sysctl 9
4629ca24f65SCraig Rodriguescan be virtualized by using one of the
4639ca24f65SCraig Rodrigues.Fn SYSCTL_VNET_*
4649ca24f65SCraig Rodriguesmacros.
4659ca24f65SCraig Rodrigues.Pp
4669ca24f65SCraig RodriguesThey take the same arguments as the standard
4679ca24f65SCraig Rodrigues.Xr sysctl 9
4689ca24f65SCraig Rodriguesfunctions, with the only difference, that the
4699ca24f65SCraig Rodrigues.Fa ptr
4709ca24f65SCraig Rodriguesargument has to be passed as
4719ca24f65SCraig Rodrigues.Ql &VNET_NAME(foo)
4729ca24f65SCraig Rodriguesinstead of
4739ca24f65SCraig Rodrigues.Ql &foo
4749ca24f65SCraig Rodriguesso that the variable can be selected from the correct memory
4759ca24f65SCraig Rodriguesregion of the virtual network stack instance of the caller.
4769ca24f65SCraig Rodrigues.Pp
4779ca24f65SCraig RodriguesFor the very rare case a sysctl handler function would want to
4789ca24f65SCraig Rodrigueshandle
4799ca24f65SCraig Rodrigues.Fa arg1
4809ca24f65SCraig Rodriguesitself the
4819ca24f65SCraig Rodrigues.Fn VNET_SYSCTL_ARG req arg1
4829ca24f65SCraig Rodriguesis provided that will translate the
4839ca24f65SCraig Rodrigues.Fa arg1
4849ca24f65SCraig Rodriguesargument to the correct memory address in the virtual network stack
4859ca24f65SCraig Rodriguescontext of the caller.
4869ca24f65SCraig Rodrigues.\" ------------------------------------------------------------
4879ca24f65SCraig Rodrigues.Sh SEE ALSO
4889ca24f65SCraig Rodrigues.Xr jail 2 ,
4899ca24f65SCraig Rodrigues.Xr kvm 3 ,
4909ca24f65SCraig Rodrigues.Xr EVENTHANDLER 9 ,
4919ca24f65SCraig Rodrigues.\" .Xr pcpu 9 ,
4929ca24f65SCraig Rodrigues.Xr KASSERT 9 ,
4939ca24f65SCraig Rodrigues.Xr sysctl 9
4949ca24f65SCraig Rodrigues.\" .Xr SYSINIT 9
495cf103f7eSMarko Zec.Pp
496cf103f7eSMarko ZecMarko Zec, Implementing a Clonable Network Stack in the FreeBSD Kernel,
497cf103f7eSMarko ZecUSENIX ATC'03, June 2003, Boston
4989ca24f65SCraig Rodrigues.Sh HISTORY
4999ca24f65SCraig RodriguesThe virtual network stack implementation first appeared in
5009ca24f65SCraig Rodrigues.Fx 8.0 .
5019ca24f65SCraig Rodrigues.Sh AUTHORS
502cf103f7eSMarko Zec.An -nosplit
503cf103f7eSMarko ZecThe
504cf103f7eSMarko Zec.Nm
5059fbbfa5dSEd Masteframework was designed and implemented at the University of Zagreb by
5069fbbfa5dSEd Maste.An Marko Zec
5079fbbfa5dSEd Masteunder sponsorship of the FreeBSD Foundation and NLnet Foundation,
508cf103f7eSMarko Zecand later extended and refined by
509cf103f7eSMarko Zec.An Bjoern A. Zeeb
5109fbbfa5dSEd Maste(also under FreeBSD Foundation sponsorship), and
5119fbbfa5dSEd Maste.An Robert Watson .
512cf103f7eSMarko Zec.Pp
5139ca24f65SCraig RodriguesThis manual page was written by
5149ca24f65SCraig Rodrigues.An Bjoern A. Zeeb, CK Software GmbH,
5159ca24f65SCraig Rodriguesunder sponsorship from the FreeBSD Foundation.
516