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