2 * Copyright (c) 2009 Jeffrey Roberson <jeff@freebsd.org>
3 * Copyright (c) 2009 Robert N. M. Watson
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * This is the virtual network stack memory allocator, which provides support
32 * for virtualized global variables via a special linker set, set_vnet. When
33 * "options VIMAGE" isn't defined, virtualized global variables are compiled
40 #if defined(_KERNEL) || defined(_WANT_VNET)
42 #define VNET_SETNAME "set_vnet"
43 #define VNET_SYMPREFIX "vnet_entry_"
51 __asm__(".section " VNET_SETNAME ", \"aw\", %progbits");
53 __asm__(".section " VNET_SETNAME ", \"aw\", @progbits");
57 #define VNET_NAME(n) vnet_entry_##n
58 #define VNET_DECLARE(t, n) extern t VNET_NAME(n)
59 #define VNET_DEFINE(t, n) t VNET_NAME(n) __section(VNET_SETNAME) __used
60 #define _VNET_PTR(b, n) (__typeof(VNET_NAME(n))*) \
61 ((b) + (uintptr_t)&VNET_NAME(n))
63 #define _VNET(b, n) (*_VNET_PTR(b, n))
66 * Virtualized global variable accessor macros.
68 #define VNET_VNET_PTR(vnet, n) _VNET_PTR((vnet)->vnet_data_base, n)
69 #define VNET_VNET(vnet, n) (*VNET_VNET_PTR((vnet), n))
71 #define VNET_PTR(n) VNET_VNET_PTR(curvnet, n)
72 #define VNET(n) VNET_VNET(curvnet, n)
75 * Sysctl variants for vnet-virtualized global variables. Include
76 * <sys/sysctl.h> to expose these definitions.
78 * Note: SYSCTL_PROC() handler functions will need to resolve pointer
79 * arguments themselves, if required.
82 int vnet_sysctl_handle_int(SYSCTL_HANDLER_ARGS);
83 int vnet_sysctl_handle_opaque(SYSCTL_HANDLER_ARGS);
84 int vnet_sysctl_handle_string(SYSCTL_HANDLER_ARGS);
85 int vnet_sysctl_handle_uint(SYSCTL_HANDLER_ARGS);
87 #define SYSCTL_VNET_INT(parent, nbr, name, access, ptr, val, descr) \
88 SYSCTL_OID(parent, nbr, name, CTLTYPE_INT|CTLFLAG_MPSAFE|(access), \
89 ptr, val, vnet_sysctl_handle_int, "I", descr)
90 #define SYSCTL_VNET_PROC(parent, nbr, name, access, ptr, arg, handler, \
92 SYSCTL_OID(parent, nbr, name, access, ptr, arg, handler, fmt, \
94 #define SYSCTL_VNET_STRING(parent, nbr, name, access, arg, len, descr) \
95 SYSCTL_OID(parent, nbr, name, CTLTYPE_STRING|(access), arg, \
96 len, vnet_sysctl_handle_string, "A", descr)
97 #define SYSCTL_VNET_STRUCT(parent, nbr, name, access, ptr, type, descr) \
98 SYSCTL_OID(parent, nbr, name, CTLTYPE_OPAQUE|(access), ptr, \
99 sizeof(struct type), vnet_sysctl_handle_opaque, "S," #type, \
101 #define SYSCTL_VNET_UINT(parent, nbr, name, access, ptr, val, descr) \
102 SYSCTL_OID(parent, nbr, name, CTLTYPE_UINT|CTLFLAG_MPSAFE|(access), \
103 ptr, val, vnet_sysctl_handle_uint, "IU", descr)
104 #define VNET_SYSCTL_ARG(req, arg1) do { \
106 arg1 = (void *)(TD_TO_VNET((req)->td)->vnet_data_base + \
107 (uintptr_t)(arg1)); \
109 #endif /* SYSCTL_OID */
112 * Interfaces from the kernel linker.
114 void *vnet_data_alloc(int size);
115 void vnet_data_copy(void *start, int size);
116 void vnet_data_free(void *start_arg, int size);
119 * Interfaces for vnet setup/teardown.
122 void vnet_data_init(struct vnet *vnet);
123 void vnet_data_destroy(struct vnet *vnet);
128 * Versions of the VNET macros that compile to normal global variables and
129 * standard sysctl definitions.
131 #define VNET_NAME(n) n
132 #define VNET_DECLARE(t, n) extern t n
133 #define VNET_DEFINE(t, n) t n
134 #define _VNET_PTR(b, n) &VNET_NAME(n)
137 #define SYSCTL_VNET_INT(parent, nbr, name, access, ptr, val, descr) \
138 SYSCTL_INT(parent, nbr, name, access, ptr, val, descr)
139 #define SYSCTL_VNET_PROC(parent, nbr, name, access, ptr, arg, handler, \
141 SYSCTL_PROC(parent, nbr, name, access, ptr, arg, handler, fmt, \
143 #define SYSCTL_VNET_STRING(parent, nbr, name, access, arg, len, descr) \
144 SYSCTL_STRING(parent, nbr, name, access, arg, len, descr)
145 #define SYSCTL_VNET_STRUCT(parent, nbr, name, access, ptr, type, descr) \
146 SYSCTL_STRUCT(parent, nbr, name, access, ptr, type, descr)
147 #define SYSCTL_VNET_UINT(parent, nbr, name, access, ptr, val, descr) \
148 SYSCTL_UINT(parent, nbr, name, access, ptr, val, descr)
149 #define VNET_SYSCTL_ARG(req, arg1)
150 #endif /* SYSCTL_OID */
153 * Virtualized global variable accessor macros.
155 #define VNET_VNET_PTR(vnet, n) (&(n))
156 #define VNET_VNET(vnet, n) (n)
158 #define VNET_PTR(n) (&(n))
165 #endif /* !_NET_VNET_H_ */