2 * Copyright (c) 2006-2009 University of Zagreb
3 * Copyright (c) 2006-2009 FreeBSD Foundation
6 * This software was developed by the University of Zagreb and the
7 * FreeBSD Foundation under sponsorship by the Stichting NLnet and the
10 * Copyright (c) 2009 Jeffrey Roberson <jeff@freebsd.org>
11 * Copyright (c) 2009 Robert N. M. Watson
12 * All rights reserved.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
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.
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
39 * This header file defines several sets of interfaces supporting virtualized
42 * - Definition of 'struct vnet' and functions and macros to allocate/free/
45 * - A virtual network stack memory allocator, which provides support for
46 * virtualized global variables via a special linker set, set_vnet.
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.
52 * If VIMAGE isn't compiled into the kernel, virtualized global variables
53 * compile to normal global variables, and virtualized sysinits to regular
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.
65 #if defined(_KERNEL) || defined(_WANT_VNET)
66 #include <sys/queue.h>
69 LIST_ENTRY(vnet) vnet_le; /* all vnets list */
74 uintptr_t vnet_data_base;
76 #define VNET_MAGIC_N 0x3e0d8f29
79 * These two virtual network stack allocator definitions are also required
80 * for libkvm so that it can evaluate virtualized global variables.
82 #define VNET_SETNAME "set_vnet"
83 #define VNET_SYMPREFIX "vnet_entry_"
90 #include <sys/proc.h> /* for struct thread */
91 #include <sys/rwlock.h>
95 * Location of the kernel's 'set_vnet' linker set.
97 extern uintptr_t *__start_set_vnet;
98 __GLOBL(__start_set_vnet);
99 extern uintptr_t *__stop_set_vnet;
100 __GLOBL(__stop_set_vnet);
102 #define VNET_START (uintptr_t)&__start_set_vnet
103 #define VNET_STOP (uintptr_t)&__stop_set_vnet
106 * Functions to allocate and destroy virtual network stacks.
108 struct vnet *vnet_alloc(void);
109 void vnet_destroy(struct vnet *vnet);
112 * The current virtual network stack -- we may wish to move this to struct
113 * pcpu in the future.
115 #define curvnet curthread->td_vnet
118 * Various macros -- get and set the current network stack, but also
121 #if defined(INVARIANTS) || defined(VNET_DEBUG)
122 #define VNET_ASSERT(exp, msg) do { \
127 #define VNET_ASSERT(exp, msg) do { \
132 void vnet_log_recursion(struct vnet *, const char *, int);
134 #define CURVNET_SET_QUIET(arg) \
135 VNET_ASSERT((arg) != NULL && (arg)->vnet_magic_n == VNET_MAGIC_N, \
136 ("CURVNET_SET at %s:%d %s() curvnet=%p vnet=%p", \
137 __FILE__, __LINE__, __func__, curvnet, (arg))); \
138 struct vnet *saved_vnet = curvnet; \
139 const char *saved_vnet_lpush = curthread->td_vnet_lpush; \
141 curthread->td_vnet_lpush = __func__;
143 #define CURVNET_SET_VERBOSE(arg) \
144 CURVNET_SET_QUIET(arg) \
146 vnet_log_recursion(saved_vnet, saved_vnet_lpush, __LINE__);
148 #define CURVNET_SET(arg) CURVNET_SET_VERBOSE(arg)
150 #define CURVNET_RESTORE() \
151 VNET_ASSERT(curvnet != NULL && (saved_vnet == NULL || \
152 saved_vnet->vnet_magic_n == VNET_MAGIC_N), \
153 ("CURVNET_RESTORE at %s:%d %s() curvnet=%p saved_vnet=%p", \
154 __FILE__, __LINE__, __func__, curvnet, saved_vnet)); \
155 curvnet = saved_vnet; \
156 curthread->td_vnet_lpush = saved_vnet_lpush;
157 #else /* !VNET_DEBUG */
159 #define CURVNET_SET_QUIET(arg) \
160 VNET_ASSERT((arg) != NULL && (arg)->vnet_magic_n == VNET_MAGIC_N, \
161 ("CURVNET_SET at %s:%d %s() curvnet=%p vnet=%p", \
162 __FILE__, __LINE__, __func__, curvnet, (arg))); \
163 struct vnet *saved_vnet = curvnet; \
166 #define CURVNET_SET_VERBOSE(arg) \
167 CURVNET_SET_QUIET(arg)
169 #define CURVNET_SET(arg) CURVNET_SET_VERBOSE(arg)
171 #define CURVNET_RESTORE() \
172 VNET_ASSERT(curvnet != NULL && (saved_vnet == NULL || \
173 saved_vnet->vnet_magic_n == VNET_MAGIC_N), \
174 ("CURVNET_RESTORE at %s:%d %s() curvnet=%p saved_vnet=%p", \
175 __FILE__, __LINE__, __func__, curvnet, saved_vnet)); \
176 curvnet = saved_vnet;
177 #endif /* VNET_DEBUG */
179 extern struct vnet *vnet0;
180 #define IS_DEFAULT_VNET(arg) ((arg) == vnet0)
182 #define CRED_TO_VNET(cr) (cr)->cr_prison->pr_vnet
183 #define TD_TO_VNET(td) CRED_TO_VNET((td)->td_ucred)
184 #define P_TO_VNET(p) CRED_TO_VNET((p)->p_ucred)
187 * Global linked list of all virtual network stacks, along with read locks to
188 * access it. If a caller may sleep while accessing the list, it must use
189 * the sleepable lock macros.
191 LIST_HEAD(vnet_list_head, vnet);
192 extern struct vnet_list_head vnet_head;
193 extern struct rwlock vnet_rwlock;
194 extern struct sx vnet_sxlock;
196 #define VNET_LIST_RLOCK() sx_slock(&vnet_sxlock)
197 #define VNET_LIST_RLOCK_NOSLEEP() rw_rlock(&vnet_rwlock)
198 #define VNET_LIST_RUNLOCK() sx_sunlock(&vnet_sxlock)
199 #define VNET_LIST_RUNLOCK_NOSLEEP() rw_runlock(&vnet_rwlock)
202 * Iteration macros to walk the global list of virtual network stacks.
204 #define VNET_ITERATOR_DECL(arg) struct vnet *arg
205 #define VNET_FOREACH(arg) LIST_FOREACH((arg), &vnet_head, vnet_le)
208 * Virtual network stack memory allocator, which allows global variables to
209 * be automatically instantiated for each network stack instance.
211 #define VNET_NAME(n) vnet_entry_##n
212 #define VNET_DECLARE(t, n) extern t VNET_NAME(n)
213 #define VNET_DEFINE(t, n) t VNET_NAME(n) __section(VNET_SETNAME) __used
214 #define _VNET_PTR(b, n) (__typeof(VNET_NAME(n))*) \
215 ((b) + (uintptr_t)&VNET_NAME(n))
217 #define _VNET(b, n) (*_VNET_PTR(b, n))
220 * Virtualized global variable accessor macros.
222 #define VNET_VNET_PTR(vnet, n) _VNET_PTR((vnet)->vnet_data_base, n)
223 #define VNET_VNET(vnet, n) (*VNET_VNET_PTR((vnet), n))
225 #define VNET_PTR(n) VNET_VNET_PTR(curvnet, n)
226 #define VNET(n) VNET_VNET(curvnet, n)
229 * Virtual network stack allocator interfaces from the kernel linker.
231 void *vnet_data_alloc(int size);
232 void vnet_data_copy(void *start, int size);
233 void vnet_data_free(void *start_arg, int size);
236 * Sysctl variants for vnet-virtualized global variables. Include
237 * <sys/sysctl.h> to expose these definitions.
239 * Note: SYSCTL_PROC() handler functions will need to resolve pointer
240 * arguments themselves, if required.
243 int vnet_sysctl_handle_int(SYSCTL_HANDLER_ARGS);
244 int vnet_sysctl_handle_opaque(SYSCTL_HANDLER_ARGS);
245 int vnet_sysctl_handle_string(SYSCTL_HANDLER_ARGS);
246 int vnet_sysctl_handle_uint(SYSCTL_HANDLER_ARGS);
248 #define SYSCTL_VNET_INT(parent, nbr, name, access, ptr, val, descr) \
249 SYSCTL_OID(parent, nbr, name, \
250 CTLTYPE_INT|CTLFLAG_MPSAFE|CTLFLAG_VNET|(access), \
251 ptr, val, vnet_sysctl_handle_int, "I", descr)
252 #define SYSCTL_VNET_PROC(parent, nbr, name, access, ptr, arg, handler, \
254 CTASSERT(((access) & CTLTYPE) != 0); \
255 SYSCTL_OID(parent, nbr, name, CTLFLAG_VNET|(access), ptr, arg, \
257 #define SYSCTL_VNET_OPAQUE(parent, nbr, name, access, ptr, len, fmt, \
259 SYSCTL_OID(parent, nbr, name, \
260 CTLTYPE_OPAQUE|CTLFLAG_VNET|(access), ptr, len, \
261 vnet_sysctl_handle_opaque, fmt, descr)
262 #define SYSCTL_VNET_STRING(parent, nbr, name, access, arg, len, descr) \
263 SYSCTL_OID(parent, nbr, name, \
264 CTLTYPE_STRING|CTLFLAG_VNET|(access), \
265 arg, len, vnet_sysctl_handle_string, "A", descr)
266 #define SYSCTL_VNET_STRUCT(parent, nbr, name, access, ptr, type, descr) \
267 SYSCTL_OID(parent, nbr, name, \
268 CTLTYPE_OPAQUE|CTLFLAG_VNET|(access), ptr, \
269 sizeof(struct type), vnet_sysctl_handle_opaque, "S," #type, \
271 #define SYSCTL_VNET_UINT(parent, nbr, name, access, ptr, val, descr) \
272 SYSCTL_OID(parent, nbr, name, \
273 CTLTYPE_UINT|CTLFLAG_MPSAFE|CTLFLAG_VNET|(access), \
274 ptr, val, vnet_sysctl_handle_uint, "IU", descr)
275 #define VNET_SYSCTL_ARG(req, arg1) do { \
277 arg1 = (void *)(TD_TO_VNET((req)->td)->vnet_data_base + \
278 (uintptr_t)(arg1)); \
280 #endif /* SYSCTL_OID */
283 * Virtual sysinit mechanism, allowing network stack components to declare
284 * startup and shutdown methods to be run when virtual network stack
285 * instances are created and destroyed.
287 #include <sys/kernel.h>
290 * SYSINIT/SYSUNINIT variants that provide per-vnet constructors and
293 struct vnet_sysinit {
294 enum sysinit_sub_id subsystem;
295 enum sysinit_elem_order order;
296 sysinit_cfunc_t func;
298 TAILQ_ENTRY(vnet_sysinit) link;
301 #define VNET_SYSINIT(ident, subsystem, order, func, arg) \
302 static struct vnet_sysinit ident ## _vnet_init = { \
305 (sysinit_cfunc_t)(sysinit_nfunc_t)func, \
308 SYSINIT(vnet_init_ ## ident, subsystem, order, \
309 vnet_register_sysinit, &ident ## _vnet_init); \
310 SYSUNINIT(vnet_init_ ## ident, subsystem, order, \
311 vnet_deregister_sysinit, &ident ## _vnet_init)
313 #define VNET_SYSUNINIT(ident, subsystem, order, func, arg) \
314 static struct vnet_sysinit ident ## _vnet_uninit = { \
317 (sysinit_cfunc_t)(sysinit_nfunc_t)func, \
320 SYSINIT(vnet_uninit_ ## ident, subsystem, order, \
321 vnet_register_sysuninit, &ident ## _vnet_uninit); \
322 SYSUNINIT(vnet_uninit_ ## ident, subsystem, order, \
323 vnet_deregister_sysuninit, &ident ## _vnet_uninit)
326 * Run per-vnet sysinits or sysuninits during vnet creation/destruction.
328 void vnet_sysinit(void);
329 void vnet_sysuninit(void);
332 * Interfaces for managing per-vnet constructors and destructors.
334 void vnet_register_sysinit(void *arg);
335 void vnet_register_sysuninit(void *arg);
336 void vnet_deregister_sysinit(void *arg);
337 void vnet_deregister_sysuninit(void *arg);
340 * EVENTHANDLER(9) extensions.
342 #include <sys/eventhandler.h>
344 void vnet_global_eventhandler_iterator_func(void *, ...);
345 #define VNET_GLOBAL_EVENTHANDLER_REGISTER_TAG(tag, name, func, arg, priority) \
347 if (IS_DEFAULT_VNET(curvnet)) { \
348 (tag) = vimage_eventhandler_register(NULL, #name, func, \
350 vnet_global_eventhandler_iterator_func); \
353 #define VNET_GLOBAL_EVENTHANDLER_REGISTER(name, func, arg, priority) \
355 if (IS_DEFAULT_VNET(curvnet)) { \
356 vimage_eventhandler_register(NULL, #name, func, \
358 vnet_global_eventhandler_iterator_func); \
365 * Various virtual network stack macros compile to no-ops without VIMAGE.
369 #define VNET_ASSERT(exp, msg)
370 #define CURVNET_SET(arg)
371 #define CURVNET_SET_QUIET(arg)
372 #define CURVNET_RESTORE()
374 #define VNET_LIST_RLOCK()
375 #define VNET_LIST_RLOCK_NOSLEEP()
376 #define VNET_LIST_RUNLOCK()
377 #define VNET_LIST_RUNLOCK_NOSLEEP()
378 #define VNET_ITERATOR_DECL(arg)
379 #define VNET_FOREACH(arg)
381 #define IS_DEFAULT_VNET(arg) 1
382 #define CRED_TO_VNET(cr) NULL
383 #define TD_TO_VNET(td) NULL
384 #define P_TO_VNET(p) NULL
387 * Versions of the VNET macros that compile to normal global variables and
388 * standard sysctl definitions.
390 #define VNET_NAME(n) n
391 #define VNET_DECLARE(t, n) extern t n
392 #define VNET_DEFINE(t, n) t n
393 #define _VNET_PTR(b, n) &VNET_NAME(n)
396 * Virtualized global variable accessor macros.
398 #define VNET_VNET_PTR(vnet, n) (&(n))
399 #define VNET_VNET(vnet, n) (n)
401 #define VNET_PTR(n) (&(n))
405 * When VIMAGE isn't compiled into the kernel, virtaulized SYSCTLs simply
406 * become normal SYSCTLs.
409 #define SYSCTL_VNET_INT(parent, nbr, name, access, ptr, val, descr) \
410 SYSCTL_INT(parent, nbr, name, access, ptr, val, descr)
411 #define SYSCTL_VNET_PROC(parent, nbr, name, access, ptr, arg, handler, \
413 SYSCTL_PROC(parent, nbr, name, access, ptr, arg, handler, fmt, \
415 #define SYSCTL_VNET_OPAQUE(parent, nbr, name, access, ptr, len, fmt, \
417 SYSCTL_OPAQUE(parent, nbr, name, access, ptr, len, fmt, descr)
418 #define SYSCTL_VNET_STRING(parent, nbr, name, access, arg, len, descr) \
419 SYSCTL_STRING(parent, nbr, name, access, arg, len, descr)
420 #define SYSCTL_VNET_STRUCT(parent, nbr, name, access, ptr, type, descr) \
421 SYSCTL_STRUCT(parent, nbr, name, access, ptr, type, descr)
422 #define SYSCTL_VNET_UINT(parent, nbr, name, access, ptr, val, descr) \
423 SYSCTL_UINT(parent, nbr, name, access, ptr, val, descr)
424 #define VNET_SYSCTL_ARG(req, arg1)
425 #endif /* SYSCTL_OID */
428 * When VIMAGE isn't compiled into the kernel, VNET_SYSINIT/VNET_SYSUNINIT
429 * map into normal sysinits, which have the same ordering properties.
431 #define VNET_SYSINIT(ident, subsystem, order, func, arg) \
432 SYSINIT(ident, subsystem, order, func, arg)
433 #define VNET_SYSUNINIT(ident, subsystem, order, func, arg) \
434 SYSUNINIT(ident, subsystem, order, func, arg)
437 * Without VIMAGE revert to the default implementation.
439 #define VNET_GLOBAL_EVENTHANDLER_REGISTER_TAG(tag, name, func, arg, priority) \
440 (tag) = eventhandler_register(NULL, #name, func, arg, priority)
441 #define VNET_GLOBAL_EVENTHANDLER_REGISTER(name, func, arg, priority) \
442 eventhandler_register(NULL, #name, func, arg, priority)
446 #endif /* !_NET_VNET_H_ */