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_"
88 #define VNET_PCPUSTAT_DECLARE(type, name) \
89 VNET_DECLARE(counter_u64_t, name[sizeof(type) / sizeof(uint64_t)])
91 #define VNET_PCPUSTAT_DEFINE(type, name) \
92 VNET_DEFINE(counter_u64_t, name[sizeof(type) / sizeof(uint64_t)])
94 #define VNET_PCPUSTAT_ALLOC(name, wait) \
95 COUNTER_ARRAY_ALLOC(VNET(name), \
96 sizeof(VNET(name)) / sizeof(counter_u64_t), (wait))
98 #define VNET_PCPUSTAT_FREE(name) \
99 COUNTER_ARRAY_FREE(VNET(name), sizeof(VNET(name)) / sizeof(counter_u64_t))
101 #define VNET_PCPUSTAT_ADD(type, name, f, v) \
102 counter_u64_add(VNET(name)[offsetof(type, f) / sizeof(uint64_t)], (v))
104 #define VNET_PCPUSTAT_SYSINIT(name) \
106 vnet_##name##_init(const void *unused) \
108 VNET_PCPUSTAT_ALLOC(name, M_WAITOK); \
110 VNET_SYSINIT(vnet_ ## name ## _init, SI_SUB_PROTO_IFATTACHDOMAIN, \
111 SI_ORDER_ANY, vnet_ ## name ## _init, NULL)
113 #define VNET_PCPUSTAT_SYSUNINIT(name) \
115 vnet_##name##_uninit(const void *unused) \
117 VNET_PCPUSTAT_FREE(name); \
119 VNET_SYSUNINIT(vnet_ ## name ## _uninit, SI_SUB_PROTO_IFATTACHDOMAIN, \
120 SI_ORDER_ANY, vnet_ ## name ## _uninit, NULL)
123 #define SYSCTL_VNET_PCPUSTAT(parent, nbr, name, type, array, desc) \
125 array##_sysctl(SYSCTL_HANDLER_ARGS) \
128 CTASSERT((sizeof(type) / sizeof(uint64_t)) == \
129 (sizeof(VNET(array)) / sizeof(counter_u64_t))); \
130 COUNTER_ARRAY_COPY(VNET(array), &s, sizeof(type) / sizeof(uint64_t));\
132 COUNTER_ARRAY_ZERO(VNET(array), \
133 sizeof(type) / sizeof(uint64_t)); \
134 return (SYSCTL_OUT(req, &s, sizeof(type))); \
136 SYSCTL_PROC(parent, nbr, name, CTLFLAG_VNET | CTLTYPE_OPAQUE | CTLFLAG_RW, \
137 NULL, 0, array ## _sysctl, "I", desc)
138 #endif /* SYSCTL_OID */
141 #include <sys/lock.h>
142 #include <sys/proc.h> /* for struct thread */
143 #include <sys/rwlock.h>
147 * Location of the kernel's 'set_vnet' linker set.
149 extern uintptr_t *__start_set_vnet;
150 __GLOBL(__start_set_vnet);
151 extern uintptr_t *__stop_set_vnet;
152 __GLOBL(__stop_set_vnet);
154 #define VNET_START (uintptr_t)&__start_set_vnet
155 #define VNET_STOP (uintptr_t)&__stop_set_vnet
158 * Functions to allocate and destroy virtual network stacks.
160 struct vnet *vnet_alloc(void);
161 void vnet_destroy(struct vnet *vnet);
164 * The current virtual network stack -- we may wish to move this to struct
165 * pcpu in the future.
167 #define curvnet curthread->td_vnet
170 * Various macros -- get and set the current network stack, but also
173 #if defined(INVARIANTS) || defined(VNET_DEBUG)
174 #define VNET_ASSERT(exp, msg) do { \
179 #define VNET_ASSERT(exp, msg) do { \
184 void vnet_log_recursion(struct vnet *, const char *, int);
186 #define CURVNET_SET_QUIET(arg) \
187 VNET_ASSERT((arg) != NULL && (arg)->vnet_magic_n == VNET_MAGIC_N, \
188 ("CURVNET_SET at %s:%d %s() curvnet=%p vnet=%p", \
189 __FILE__, __LINE__, __func__, curvnet, (arg))); \
190 struct vnet *saved_vnet = curvnet; \
191 const char *saved_vnet_lpush = curthread->td_vnet_lpush; \
193 curthread->td_vnet_lpush = __func__;
195 #define CURVNET_SET_VERBOSE(arg) \
196 CURVNET_SET_QUIET(arg) \
198 vnet_log_recursion(saved_vnet, saved_vnet_lpush, __LINE__);
200 #define CURVNET_SET(arg) CURVNET_SET_VERBOSE(arg)
202 #define CURVNET_RESTORE() \
203 VNET_ASSERT(curvnet != NULL && (saved_vnet == NULL || \
204 saved_vnet->vnet_magic_n == VNET_MAGIC_N), \
205 ("CURVNET_RESTORE at %s:%d %s() curvnet=%p saved_vnet=%p", \
206 __FILE__, __LINE__, __func__, curvnet, saved_vnet)); \
207 curvnet = saved_vnet; \
208 curthread->td_vnet_lpush = saved_vnet_lpush;
209 #else /* !VNET_DEBUG */
211 #define CURVNET_SET_QUIET(arg) \
212 VNET_ASSERT((arg) != NULL && (arg)->vnet_magic_n == VNET_MAGIC_N, \
213 ("CURVNET_SET at %s:%d %s() curvnet=%p vnet=%p", \
214 __FILE__, __LINE__, __func__, curvnet, (arg))); \
215 struct vnet *saved_vnet = curvnet; \
218 #define CURVNET_SET_VERBOSE(arg) \
219 CURVNET_SET_QUIET(arg)
221 #define CURVNET_SET(arg) CURVNET_SET_VERBOSE(arg)
223 #define CURVNET_RESTORE() \
224 VNET_ASSERT(curvnet != NULL && (saved_vnet == NULL || \
225 saved_vnet->vnet_magic_n == VNET_MAGIC_N), \
226 ("CURVNET_RESTORE at %s:%d %s() curvnet=%p saved_vnet=%p", \
227 __FILE__, __LINE__, __func__, curvnet, saved_vnet)); \
228 curvnet = saved_vnet;
229 #endif /* VNET_DEBUG */
231 extern struct vnet *vnet0;
232 #define IS_DEFAULT_VNET(arg) ((arg) == vnet0)
234 #define CRED_TO_VNET(cr) (cr)->cr_prison->pr_vnet
235 #define TD_TO_VNET(td) CRED_TO_VNET((td)->td_ucred)
236 #define P_TO_VNET(p) CRED_TO_VNET((p)->p_ucred)
239 * Global linked list of all virtual network stacks, along with read locks to
240 * access it. If a caller may sleep while accessing the list, it must use
241 * the sleepable lock macros.
243 LIST_HEAD(vnet_list_head, vnet);
244 extern struct vnet_list_head vnet_head;
245 extern struct rwlock vnet_rwlock;
246 extern struct sx vnet_sxlock;
248 #define VNET_LIST_RLOCK() sx_slock(&vnet_sxlock)
249 #define VNET_LIST_RLOCK_NOSLEEP() rw_rlock(&vnet_rwlock)
250 #define VNET_LIST_RUNLOCK() sx_sunlock(&vnet_sxlock)
251 #define VNET_LIST_RUNLOCK_NOSLEEP() rw_runlock(&vnet_rwlock)
254 * Iteration macros to walk the global list of virtual network stacks.
256 #define VNET_ITERATOR_DECL(arg) struct vnet *arg
257 #define VNET_FOREACH(arg) LIST_FOREACH((arg), &vnet_head, vnet_le)
260 * Virtual network stack memory allocator, which allows global variables to
261 * be automatically instantiated for each network stack instance.
263 #define VNET_NAME(n) vnet_entry_##n
264 #define VNET_DECLARE(t, n) extern t VNET_NAME(n)
265 #define VNET_DEFINE(t, n) t VNET_NAME(n) __section(VNET_SETNAME) __used
266 #define _VNET_PTR(b, n) (__typeof(VNET_NAME(n))*) \
267 ((b) + (uintptr_t)&VNET_NAME(n))
269 #define _VNET(b, n) (*_VNET_PTR(b, n))
272 * Virtualized global variable accessor macros.
274 #define VNET_VNET_PTR(vnet, n) _VNET_PTR((vnet)->vnet_data_base, n)
275 #define VNET_VNET(vnet, n) (*VNET_VNET_PTR((vnet), n))
277 #define VNET_PTR(n) VNET_VNET_PTR(curvnet, n)
278 #define VNET(n) VNET_VNET(curvnet, n)
281 * Virtual network stack allocator interfaces from the kernel linker.
283 void *vnet_data_alloc(int size);
284 void vnet_data_copy(void *start, int size);
285 void vnet_data_free(void *start_arg, int size);
288 * Virtual sysinit mechanism, allowing network stack components to declare
289 * startup and shutdown methods to be run when virtual network stack
290 * instances are created and destroyed.
292 #include <sys/kernel.h>
295 * SYSINIT/SYSUNINIT variants that provide per-vnet constructors and
298 struct vnet_sysinit {
299 enum sysinit_sub_id subsystem;
300 enum sysinit_elem_order order;
301 sysinit_cfunc_t func;
303 TAILQ_ENTRY(vnet_sysinit) link;
306 #define VNET_SYSINIT(ident, subsystem, order, func, arg) \
307 static struct vnet_sysinit ident ## _vnet_init = { \
310 (sysinit_cfunc_t)(sysinit_nfunc_t)func, \
313 SYSINIT(vnet_init_ ## ident, subsystem, order, \
314 vnet_register_sysinit, &ident ## _vnet_init); \
315 SYSUNINIT(vnet_init_ ## ident, subsystem, order, \
316 vnet_deregister_sysinit, &ident ## _vnet_init)
318 #define VNET_SYSUNINIT(ident, subsystem, order, func, arg) \
319 static struct vnet_sysinit ident ## _vnet_uninit = { \
322 (sysinit_cfunc_t)(sysinit_nfunc_t)func, \
325 SYSINIT(vnet_uninit_ ## ident, subsystem, order, \
326 vnet_register_sysuninit, &ident ## _vnet_uninit); \
327 SYSUNINIT(vnet_uninit_ ## ident, subsystem, order, \
328 vnet_deregister_sysuninit, &ident ## _vnet_uninit)
331 * Run per-vnet sysinits or sysuninits during vnet creation/destruction.
333 void vnet_sysinit(void);
334 void vnet_sysuninit(void);
337 * Interfaces for managing per-vnet constructors and destructors.
339 void vnet_register_sysinit(void *arg);
340 void vnet_register_sysuninit(void *arg);
341 void vnet_deregister_sysinit(void *arg);
342 void vnet_deregister_sysuninit(void *arg);
345 * EVENTHANDLER(9) extensions.
347 #include <sys/eventhandler.h>
349 void vnet_global_eventhandler_iterator_func(void *, ...);
350 #define VNET_GLOBAL_EVENTHANDLER_REGISTER_TAG(tag, name, func, arg, priority) \
352 if (IS_DEFAULT_VNET(curvnet)) { \
353 (tag) = vimage_eventhandler_register(NULL, #name, func, \
355 vnet_global_eventhandler_iterator_func); \
358 #define VNET_GLOBAL_EVENTHANDLER_REGISTER(name, func, arg, priority) \
360 if (IS_DEFAULT_VNET(curvnet)) { \
361 vimage_eventhandler_register(NULL, #name, func, \
363 vnet_global_eventhandler_iterator_func); \
370 * Various virtual network stack macros compile to no-ops without VIMAGE.
374 #define VNET_ASSERT(exp, msg)
375 #define CURVNET_SET(arg)
376 #define CURVNET_SET_QUIET(arg)
377 #define CURVNET_RESTORE()
379 #define VNET_LIST_RLOCK()
380 #define VNET_LIST_RLOCK_NOSLEEP()
381 #define VNET_LIST_RUNLOCK()
382 #define VNET_LIST_RUNLOCK_NOSLEEP()
383 #define VNET_ITERATOR_DECL(arg)
384 #define VNET_FOREACH(arg)
386 #define IS_DEFAULT_VNET(arg) 1
387 #define CRED_TO_VNET(cr) NULL
388 #define TD_TO_VNET(td) NULL
389 #define P_TO_VNET(p) NULL
392 * Versions of the VNET macros that compile to normal global variables and
393 * standard sysctl definitions.
395 #define VNET_NAME(n) n
396 #define VNET_DECLARE(t, n) extern t n
397 #define VNET_DEFINE(t, n) t n
398 #define _VNET_PTR(b, n) &VNET_NAME(n)
401 * Virtualized global variable accessor macros.
403 #define VNET_VNET_PTR(vnet, n) (&(n))
404 #define VNET_VNET(vnet, n) (n)
406 #define VNET_PTR(n) (&(n))
410 * When VIMAGE isn't compiled into the kernel, VNET_SYSINIT/VNET_SYSUNINIT
411 * map into normal sysinits, which have the same ordering properties.
413 #define VNET_SYSINIT(ident, subsystem, order, func, arg) \
414 SYSINIT(ident, subsystem, order, func, arg)
415 #define VNET_SYSUNINIT(ident, subsystem, order, func, arg) \
416 SYSUNINIT(ident, subsystem, order, func, arg)
419 * Without VIMAGE revert to the default implementation.
421 #define VNET_GLOBAL_EVENTHANDLER_REGISTER_TAG(tag, name, func, arg, priority) \
422 (tag) = eventhandler_register(NULL, #name, func, arg, priority)
423 #define VNET_GLOBAL_EVENTHANDLER_REGISTER(name, func, arg, priority) \
424 eventhandler_register(NULL, #name, func, arg, priority)
428 #endif /* !_NET_VNET_H_ */