The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/net/vnet.h

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright (c) 2006-2009 University of Zagreb
    3  * Copyright (c) 2006-2009 FreeBSD Foundation
    4  * All rights reserved.
    5  *
    6  * This software was developed by the University of Zagreb and the
    7  * FreeBSD Foundation under sponsorship by the Stichting NLnet and the
    8  * FreeBSD Foundation.
    9  *
   10  * Copyright (c) 2009 Jeffrey Roberson <jeff@freebsd.org>
   11  * Copyright (c) 2009 Robert N. M. Watson
   12  * All rights reserved.
   13  *
   14  * Redistribution and use in source and binary forms, with or without
   15  * modification, are permitted provided that the following conditions
   16  * are met:
   17  * 1. Redistributions of source code must retain the above copyright
   18  *    notice, this list of conditions and the following disclaimer.
   19  * 2. Redistributions in binary form must reproduce the above copyright
   20  *    notice, this list of conditions and the following disclaimer in the
   21  *    documentation and/or other materials provided with the distribution.
   22  *
   23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   33  * SUCH DAMAGE.
   34  *
   35  * $FreeBSD: releng/8.3/sys/net/vnet.h 220501 2011-04-09 14:27:27Z bz $
   36  */
   37 
   38 /*-
   39  * This header file defines several sets of interfaces supporting virtualized
   40  * network stacks:
   41  *
   42  * - Definition of 'struct vnet' and functions and macros to allocate/free/
   43  *   manipulate it.
   44  *
   45  * - A virtual network stack memory allocator, which provides support for
   46  *   virtualized global variables via a special linker set, set_vnet.
   47  *
   48  * - Virtualized sysinits/sysuninits, which allow constructors and
   49  *   destructors to be run for each network stack subsystem as virtual
   50  *   instances are created and destroyed.
   51  *
   52  * If VIMAGE isn't compiled into the kernel, virtualized global variables
   53  * compile to normal global variables, and virtualized sysinits to regular
   54  * sysinits.
   55  */
   56 
   57 #ifndef _NET_VNET_H_
   58 #define _NET_VNET_H_
   59 
   60 /*
   61  * struct vnet describes a virtualized network stack, and is primarily a
   62  * pointer to storage for virtualized global variables.  Expose to userspace
   63  * as required for libkvm.
   64  */
   65 #if defined(_KERNEL) || defined(_WANT_VNET)
   66 #include <sys/queue.h>
   67 
   68 struct vnet {
   69         LIST_ENTRY(vnet)         vnet_le;       /* all vnets list */
   70         u_int                    vnet_magic_n;
   71         u_int                    vnet_ifcnt;
   72         u_int                    vnet_sockcnt;
   73         void                    *vnet_data_mem;
   74         uintptr_t                vnet_data_base;
   75 };
   76 #define VNET_MAGIC_N    0x3e0d8f29
   77 
   78 /*
   79  * These two virtual network stack allocator definitions are also required
   80  * for libkvm so that it can evaluate virtualized global variables.
   81  */
   82 #define VNET_SETNAME            "set_vnet"
   83 #define VNET_SYMPREFIX          "vnet_entry_"
   84 #endif
   85 
   86 #ifdef _KERNEL
   87 
   88 #ifdef VIMAGE
   89 #include <sys/lock.h>
   90 #include <sys/proc.h>                   /* for struct thread */
   91 #include <sys/rwlock.h>
   92 #include <sys/sx.h>
   93 
   94 /*
   95  * Location of the kernel's 'set_vnet' linker set.
   96  */
   97 extern uintptr_t        *__start_set_vnet;
   98 extern uintptr_t        *__stop_set_vnet;
   99 
  100 #define VNET_START      (uintptr_t)&__start_set_vnet
  101 #define VNET_STOP       (uintptr_t)&__stop_set_vnet
  102 
  103 /*
  104  * Functions to allocate and destroy virtual network stacks.
  105  */
  106 struct vnet *vnet_alloc(void);
  107 void    vnet_destroy(struct vnet *vnet);
  108 
  109 /*
  110  * The current virtual network stack -- we may wish to move this to struct
  111  * pcpu in the future.
  112  */
  113 #define curvnet curthread->td_vnet
  114 
  115 /*
  116  * Various macros -- get and set the current network stack, but also
  117  * assertions.
  118  */
  119 #if defined(INVARIANTS) || defined(VNET_DEBUG)
  120 #define VNET_ASSERT(exp, msg)   do {                                    \
  121         if (!(exp))                                                     \
  122                 panic msg;                                              \
  123 } while (0)
  124 #else
  125 #define VNET_ASSERT(exp, msg)   do {                                    \
  126 } while (0)
  127 #endif
  128 
  129 #ifdef VNET_DEBUG
  130 void vnet_log_recursion(struct vnet *, const char *, int);
  131 
  132 #define CURVNET_SET_QUIET(arg)                                          \
  133         VNET_ASSERT((arg) != NULL && (arg)->vnet_magic_n == VNET_MAGIC_N, \
  134             ("CURVNET_SET at %s:%d %s() curvnet=%p vnet=%p",            \
  135             __FILE__, __LINE__, __func__, curvnet, (arg)));             \
  136         struct vnet *saved_vnet = curvnet;                              \
  137         const char *saved_vnet_lpush = curthread->td_vnet_lpush;        \
  138         curvnet = arg;                                                  \
  139         curthread->td_vnet_lpush = __func__;
  140  
  141 #define CURVNET_SET_VERBOSE(arg)                                        \
  142         CURVNET_SET_QUIET(arg)                                          \
  143         if (saved_vnet)                                                 \
  144                 vnet_log_recursion(saved_vnet, saved_vnet_lpush, __LINE__);
  145 
  146 #define CURVNET_SET(arg)        CURVNET_SET_VERBOSE(arg)
  147  
  148 #define CURVNET_RESTORE()                                               \
  149         VNET_ASSERT(curvnet != NULL && (saved_vnet == NULL ||           \
  150             saved_vnet->vnet_magic_n == VNET_MAGIC_N),                  \
  151             ("CURVNET_RESTORE at %s:%d %s() curvnet=%p saved_vnet=%p",  \
  152             __FILE__, __LINE__, __func__, curvnet, saved_vnet));        \
  153         curvnet = saved_vnet;                                           \
  154         curthread->td_vnet_lpush = saved_vnet_lpush;
  155 #else /* !VNET_DEBUG */
  156 
  157 #define CURVNET_SET_QUIET(arg)                                          \
  158         VNET_ASSERT((arg) != NULL && (arg)->vnet_magic_n == VNET_MAGIC_N, \
  159             ("CURVNET_SET at %s:%d %s() curvnet=%p vnet=%p",            \
  160             __FILE__, __LINE__, __func__, curvnet, (arg)));             \
  161         struct vnet *saved_vnet = curvnet;                              \
  162         curvnet = arg;  
  163  
  164 #define CURVNET_SET_VERBOSE(arg)                                        \
  165         CURVNET_SET_QUIET(arg)
  166 
  167 #define CURVNET_SET(arg)        CURVNET_SET_VERBOSE(arg)
  168  
  169 #define CURVNET_RESTORE()                                               \
  170         VNET_ASSERT(curvnet != NULL && (saved_vnet == NULL ||           \
  171             saved_vnet->vnet_magic_n == VNET_MAGIC_N),                  \
  172             ("CURVNET_RESTORE at %s:%d %s() curvnet=%p saved_vnet=%p",  \
  173             __FILE__, __LINE__, __func__, curvnet, saved_vnet));        \
  174         curvnet = saved_vnet;
  175 #endif /* VNET_DEBUG */
  176 
  177 extern struct vnet *vnet0;
  178 #define IS_DEFAULT_VNET(arg)    ((arg) == vnet0)
  179 
  180 #define CRED_TO_VNET(cr)        (cr)->cr_prison->pr_vnet
  181 #define TD_TO_VNET(td)          CRED_TO_VNET((td)->td_ucred)
  182 #define P_TO_VNET(p)            CRED_TO_VNET((p)->p_ucred)
  183 
  184 /*
  185  * Global linked list of all virtual network stacks, along with read locks to
  186  * access it.  If a caller may sleep while accessing the list, it must use
  187  * the sleepable lock macros.
  188  */
  189 LIST_HEAD(vnet_list_head, vnet);
  190 extern struct vnet_list_head vnet_head;
  191 extern struct rwlock vnet_rwlock;
  192 extern struct sx vnet_sxlock;
  193 
  194 #define VNET_LIST_RLOCK()               sx_slock(&vnet_sxlock)
  195 #define VNET_LIST_RLOCK_NOSLEEP()       rw_rlock(&vnet_rwlock)
  196 #define VNET_LIST_RUNLOCK()             sx_sunlock(&vnet_sxlock)
  197 #define VNET_LIST_RUNLOCK_NOSLEEP()     rw_runlock(&vnet_rwlock)
  198 
  199 /*
  200  * Iteration macros to walk the global list of virtual network stacks.
  201  */
  202 #define VNET_ITERATOR_DECL(arg) struct vnet *arg
  203 #define VNET_FOREACH(arg)       LIST_FOREACH((arg), &vnet_head, vnet_le)
  204 
  205 /*
  206  * Virtual network stack memory allocator, which allows global variables to
  207  * be automatically instantiated for each network stack instance.
  208  */
  209 #define VNET_NAME(n)            vnet_entry_##n
  210 #define VNET_DECLARE(t, n)      extern t VNET_NAME(n)
  211 #define VNET_DEFINE(t, n)       t VNET_NAME(n) __section(VNET_SETNAME) __used
  212 #define _VNET_PTR(b, n)         (__typeof(VNET_NAME(n))*)               \
  213                                     ((b) + (uintptr_t)&VNET_NAME(n))
  214 
  215 #define _VNET(b, n)             (*_VNET_PTR(b, n))
  216 
  217 /*
  218  * Virtualized global variable accessor macros.
  219  */
  220 #define VNET_VNET_PTR(vnet, n)          _VNET_PTR((vnet)->vnet_data_base, n)
  221 #define VNET_VNET(vnet, n)              (*VNET_VNET_PTR((vnet), n))
  222 
  223 #define VNET_PTR(n)             VNET_VNET_PTR(curvnet, n)
  224 #define VNET(n)                 VNET_VNET(curvnet, n)
  225 
  226 /*
  227  * Virtual network stack allocator interfaces from the kernel linker.
  228  */
  229 void    *vnet_data_alloc(int size);
  230 void     vnet_data_copy(void *start, int size);
  231 void     vnet_data_free(void *start_arg, int size);
  232 
  233 /*
  234  * Sysctl variants for vnet-virtualized global variables.  Include
  235  * <sys/sysctl.h> to expose these definitions.
  236  *
  237  * Note: SYSCTL_PROC() handler functions will need to resolve pointer
  238  * arguments themselves, if required.
  239  */
  240 #ifdef SYSCTL_OID
  241 int     vnet_sysctl_handle_int(SYSCTL_HANDLER_ARGS);
  242 int     vnet_sysctl_handle_opaque(SYSCTL_HANDLER_ARGS);
  243 int     vnet_sysctl_handle_string(SYSCTL_HANDLER_ARGS);
  244 int     vnet_sysctl_handle_uint(SYSCTL_HANDLER_ARGS);
  245 
  246 #define SYSCTL_VNET_INT(parent, nbr, name, access, ptr, val, descr)     \
  247         SYSCTL_OID(parent, nbr, name,                                   \
  248             CTLTYPE_INT|CTLFLAG_MPSAFE|CTLFLAG_VNET|(access),           \
  249             ptr, val, vnet_sysctl_handle_int, "I", descr)
  250 #define SYSCTL_VNET_PROC(parent, nbr, name, access, ptr, arg, handler,  \
  251             fmt, descr)                                                 \
  252         SYSCTL_OID(parent, nbr, name, CTLFLAG_VNET|(access), ptr, arg,  \
  253             handler, fmt, descr)
  254 #define SYSCTL_VNET_OPAQUE(parent, nbr, name, access, ptr, len, fmt,    \
  255             descr)                                                      \
  256         SYSCTL_OID(parent, nbr, name,                                   \
  257             CTLTYPE_OPAQUE|CTLFLAG_VNET|(access), ptr, len,             \
  258             vnet_sysctl_handle_opaque, fmt, descr)
  259 #define SYSCTL_VNET_STRING(parent, nbr, name, access, arg, len, descr)  \
  260         SYSCTL_OID(parent, nbr, name,                                   \
  261             CTLTYPE_STRING|CTLFLAG_VNET|(access),                       \
  262             arg, len, vnet_sysctl_handle_string, "A", descr)
  263 #define SYSCTL_VNET_STRUCT(parent, nbr, name, access, ptr, type, descr) \
  264         SYSCTL_OID(parent, nbr, name,                                   \
  265             CTLTYPE_OPAQUE|CTLFLAG_VNET|(access), ptr,                  \
  266             sizeof(struct type), vnet_sysctl_handle_opaque, "S," #type, \
  267             descr)
  268 #define SYSCTL_VNET_UINT(parent, nbr, name, access, ptr, val, descr)    \
  269         SYSCTL_OID(parent, nbr, name,                                   \
  270             CTLTYPE_UINT|CTLFLAG_MPSAFE|CTLFLAG_VNET|(access),          \
  271             ptr, val, vnet_sysctl_handle_uint, "IU", descr)
  272 #define VNET_SYSCTL_ARG(req, arg1) do {                                 \
  273         if (arg1 != NULL)                                               \
  274                 arg1 = (void *)(TD_TO_VNET((req)->td)->vnet_data_base + \
  275                     (uintptr_t)(arg1));                                 \
  276 } while (0)
  277 #endif /* SYSCTL_OID */
  278 
  279 /*
  280  * Virtual sysinit mechanism, allowing network stack components to declare
  281  * startup and shutdown methods to be run when virtual network stack
  282  * instances are created and destroyed.
  283  */
  284 #include <sys/kernel.h>
  285 
  286 /*
  287  * SYSINIT/SYSUNINIT variants that provide per-vnet constructors and
  288  * destructors.
  289  */
  290 struct vnet_sysinit {
  291         enum sysinit_sub_id     subsystem;
  292         enum sysinit_elem_order order;
  293         sysinit_cfunc_t         func;
  294         const void              *arg;
  295         TAILQ_ENTRY(vnet_sysinit) link;
  296 };
  297 
  298 #define VNET_SYSINIT(ident, subsystem, order, func, arg)                \
  299         static struct vnet_sysinit ident ## _vnet_init = {              \
  300                 subsystem,                                              \
  301                 order,                                                  \
  302                 (sysinit_cfunc_t)(sysinit_nfunc_t)func,                 \
  303                 (arg)                                                   \
  304         };                                                              \
  305         SYSINIT(vnet_init_ ## ident, subsystem, order,                  \
  306             vnet_register_sysinit, &ident ## _vnet_init);               \
  307         SYSUNINIT(vnet_init_ ## ident, subsystem, order,                \
  308             vnet_deregister_sysinit, &ident ## _vnet_init)
  309 
  310 #define VNET_SYSUNINIT(ident, subsystem, order, func, arg)              \
  311         static struct vnet_sysinit ident ## _vnet_uninit = {            \
  312                 subsystem,                                              \
  313                 order,                                                  \
  314                 (sysinit_cfunc_t)(sysinit_nfunc_t)func,                 \
  315                 (arg)                                                   \
  316         };                                                              \
  317         SYSINIT(vnet_uninit_ ## ident, subsystem, order,                \
  318             vnet_register_sysuninit, &ident ## _vnet_uninit);           \
  319         SYSUNINIT(vnet_uninit_ ## ident, subsystem, order,              \
  320             vnet_deregister_sysuninit, &ident ## _vnet_uninit)
  321 
  322 /*
  323  * Run per-vnet sysinits or sysuninits during vnet creation/destruction.
  324  */
  325 void     vnet_sysinit(void);
  326 void     vnet_sysuninit(void);
  327 
  328 /*
  329  * Interfaces for managing per-vnet constructors and destructors.
  330  */
  331 void    vnet_register_sysinit(void *arg);
  332 void    vnet_register_sysuninit(void *arg);
  333 void    vnet_deregister_sysinit(void *arg);
  334 void    vnet_deregister_sysuninit(void *arg);
  335 
  336 /*
  337  * EVENTHANDLER(9) extensions.
  338  */
  339 #include <sys/eventhandler.h>
  340 
  341 void    vnet_global_eventhandler_iterator_func(void *, ...);
  342 #define VNET_GLOBAL_EVENTHANDLER_REGISTER_TAG(tag, name, func, arg, priority) \
  343 do {                                                                    \
  344         if (IS_DEFAULT_VNET(curvnet)) {                                 \
  345                 (tag) = vimage_eventhandler_register(NULL, #name, func, \
  346                     arg, priority,                                      \
  347                     vnet_global_eventhandler_iterator_func);            \
  348         }                                                               \
  349 } while(0)
  350 #define VNET_GLOBAL_EVENTHANDLER_REGISTER(name, func, arg, priority)    \
  351 do {                                                                    \
  352         if (IS_DEFAULT_VNET(curvnet)) {                                 \
  353                 vimage_eventhandler_register(NULL, #name, func,         \
  354                     arg, priority,                                      \
  355                     vnet_global_eventhandler_iterator_func);            \
  356         }                                                               \
  357 } while(0)
  358 
  359 #else /* !VIMAGE */
  360 
  361 /*
  362  * Various virtual network stack macros compile to no-ops without VIMAGE.
  363  */
  364 #define curvnet                 NULL
  365 
  366 #define VNET_ASSERT(exp, msg)
  367 #define CURVNET_SET(arg)
  368 #define CURVNET_SET_QUIET(arg)
  369 #define CURVNET_RESTORE()
  370 
  371 #define VNET_LIST_RLOCK()
  372 #define VNET_LIST_RLOCK_NOSLEEP()
  373 #define VNET_LIST_RUNLOCK()
  374 #define VNET_LIST_RUNLOCK_NOSLEEP()
  375 #define VNET_ITERATOR_DECL(arg)
  376 #define VNET_FOREACH(arg)
  377 
  378 #define IS_DEFAULT_VNET(arg)    1
  379 #define CRED_TO_VNET(cr)        NULL
  380 #define TD_TO_VNET(td)          NULL
  381 #define P_TO_VNET(p)            NULL
  382 
  383 /*
  384  * Versions of the VNET macros that compile to normal global variables and
  385  * standard sysctl definitions.
  386  */
  387 #define VNET_NAME(n)            n
  388 #define VNET_DECLARE(t, n)      extern t n
  389 #define VNET_DEFINE(t, n)       t n
  390 #define _VNET_PTR(b, n)         &VNET_NAME(n)
  391 
  392 /*
  393  * Virtualized global variable accessor macros.
  394  */
  395 #define VNET_VNET_PTR(vnet, n)          (&(n))
  396 #define VNET_VNET(vnet, n)              (n)
  397 
  398 #define VNET_PTR(n)             (&(n))
  399 #define VNET(n)                 (n)
  400 
  401 /*
  402  * When VIMAGE isn't compiled into the kernel, virtaulized SYSCTLs simply
  403  * become normal SYSCTLs.
  404  */
  405 #ifdef SYSCTL_OID
  406 #define SYSCTL_VNET_INT(parent, nbr, name, access, ptr, val, descr)     \
  407         SYSCTL_INT(parent, nbr, name, access, ptr, val, descr)
  408 #define SYSCTL_VNET_PROC(parent, nbr, name, access, ptr, arg, handler,  \
  409             fmt, descr)                                                 \
  410         SYSCTL_PROC(parent, nbr, name, access, ptr, arg, handler, fmt,  \
  411             descr)
  412 #define SYSCTL_VNET_OPAQUE(parent, nbr, name, access, ptr, len, fmt,    \
  413             descr)                                                      \
  414         SYSCTL_OPAQUE(parent, nbr, name, access, ptr, len, fmt, descr)
  415 #define SYSCTL_VNET_STRING(parent, nbr, name, access, arg, len, descr)  \
  416         SYSCTL_STRING(parent, nbr, name, access, arg, len, descr)
  417 #define SYSCTL_VNET_STRUCT(parent, nbr, name, access, ptr, type, descr) \
  418         SYSCTL_STRUCT(parent, nbr, name, access, ptr, type, descr)
  419 #define SYSCTL_VNET_UINT(parent, nbr, name, access, ptr, val, descr)    \
  420         SYSCTL_UINT(parent, nbr, name, access, ptr, val, descr)
  421 #define VNET_SYSCTL_ARG(req, arg1)
  422 #endif /* SYSCTL_OID */
  423 
  424 /*
  425  * When VIMAGE isn't compiled into the kernel, VNET_SYSINIT/VNET_SYSUNINIT
  426  * map into normal sysinits, which have the same ordering properties.
  427  */
  428 #define VNET_SYSINIT(ident, subsystem, order, func, arg)                \
  429         SYSINIT(ident, subsystem, order, func, arg)
  430 #define VNET_SYSUNINIT(ident, subsystem, order, func, arg)              \
  431         SYSUNINIT(ident, subsystem, order, func, arg)
  432 
  433 /*
  434  * Without VIMAGE revert to the default implementation.
  435  */
  436 #define VNET_GLOBAL_EVENTHANDLER_REGISTER_TAG(tag, name, func, arg, priority) \
  437         (tag) = eventhandler_register(NULL, #name, func, arg, priority)
  438 #define VNET_GLOBAL_EVENTHANDLER_REGISTER(name, func, arg, priority)    \
  439         eventhandler_register(NULL, #name, func, arg, priority)
  440 #endif /* VIMAGE */
  441 #endif /* _KERNEL */
  442 
  443 #endif /* !_NET_VNET_H_ */

Cache object: f3cb40034325a32fae8955b6ed4e2a71


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.