2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2006-2009 University of Zagreb
5 * Copyright (c) 2006-2009 FreeBSD Foundation
8 * This software was developed by the University of Zagreb and the
9 * FreeBSD Foundation under sponsorship by the Stichting NLnet and the
12 * Copyright (c) 2009 Jeffrey Roberson <jeff@freebsd.org>
13 * Copyright (c) 2009 Robert N. M. Watson
14 * All rights reserved.
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
25 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41 * This header file defines several sets of interfaces supporting virtualized
44 * - Definition of 'struct vnet' and functions and macros to allocate/free/
47 * - A virtual network stack memory allocator, which provides support for
48 * virtualized global variables via a special linker set, set_vnet.
50 * - Virtualized sysinits/sysuninits, which allow constructors and
51 * destructors to be run for each network stack subsystem as virtual
52 * instances are created and destroyed.
54 * If VIMAGE isn't compiled into the kernel, virtualized global variables
55 * compile to normal global variables, and virtualized sysinits to regular
63 * struct vnet describes a virtualized network stack, and is primarily a
64 * pointer to storage for virtualized global variables. Expose to userspace
65 * as required for libkvm.
67 #if defined(_KERNEL) || defined(_WANT_VNET)
68 #include <machine/param.h> /* for CACHE_LINE_SIZE */
69 #include <sys/queue.h>
72 LIST_ENTRY(vnet) vnet_le; /* all vnets list */
76 u_int vnet_state; /* SI_SUB_* */
78 uintptr_t vnet_data_base;
79 bool vnet_shutdown; /* Shutdown in progress. */
80 } __aligned(CACHE_LINE_SIZE);
81 #define VNET_MAGIC_N 0x5e4a6f28
84 * These two virtual network stack allocator definitions are also required
85 * for libkvm so that it can evaluate virtualized global variables.
87 #define VNET_SETNAME "set_vnet"
88 #define VNET_SYMPREFIX "vnet_entry_"
93 #define VNET_PCPUSTAT_DECLARE(type, name) \
94 VNET_DECLARE(counter_u64_t, name[sizeof(type) / sizeof(uint64_t)])
96 #define VNET_PCPUSTAT_DEFINE(type, name) \
97 VNET_DEFINE(counter_u64_t, name[sizeof(type) / sizeof(uint64_t)])
98 #define VNET_PCPUSTAT_DEFINE_STATIC(type, name) \
99 VNET_DEFINE_STATIC(counter_u64_t, name[sizeof(type) / sizeof(uint64_t)])
101 #define VNET_PCPUSTAT_ALLOC(name, wait) \
102 COUNTER_ARRAY_ALLOC(VNET(name), \
103 sizeof(VNET(name)) / sizeof(counter_u64_t), (wait))
105 #define VNET_PCPUSTAT_FREE(name) \
106 COUNTER_ARRAY_FREE(VNET(name), sizeof(VNET(name)) / sizeof(counter_u64_t))
108 #define VNET_PCPUSTAT_ADD(type, name, f, v) \
109 counter_u64_add(VNET(name)[offsetof(type, f) / sizeof(uint64_t)], (v))
111 #define VNET_PCPUSTAT_FETCH(type, name, f) \
112 counter_u64_fetch(VNET(name)[offsetof(type, f) / sizeof(uint64_t)])
114 #define VNET_PCPUSTAT_SYSINIT(name) \
116 vnet_##name##_init(const void *unused) \
118 VNET_PCPUSTAT_ALLOC(name, M_WAITOK); \
120 VNET_SYSINIT(vnet_ ## name ## _init, SI_SUB_INIT_IF, \
121 SI_ORDER_FIRST, vnet_ ## name ## _init, NULL)
123 #define VNET_PCPUSTAT_SYSUNINIT(name) \
125 vnet_##name##_uninit(const void *unused) \
127 VNET_PCPUSTAT_FREE(name); \
129 VNET_SYSUNINIT(vnet_ ## name ## _uninit, SI_SUB_INIT_IF, \
130 SI_ORDER_FIRST, vnet_ ## name ## _uninit, NULL)
133 #define SYSCTL_VNET_PCPUSTAT(parent, nbr, name, type, array, desc) \
135 array##_sysctl(SYSCTL_HANDLER_ARGS) \
138 CTASSERT((sizeof(type) / sizeof(uint64_t)) == \
139 (sizeof(VNET(array)) / sizeof(counter_u64_t))); \
140 COUNTER_ARRAY_COPY(VNET(array), &s, sizeof(type) / sizeof(uint64_t));\
142 COUNTER_ARRAY_ZERO(VNET(array), \
143 sizeof(type) / sizeof(uint64_t)); \
144 return (SYSCTL_OUT(req, &s, sizeof(type))); \
146 SYSCTL_PROC(parent, nbr, name, \
147 CTLFLAG_VNET | CTLTYPE_OPAQUE | CTLFLAG_RW | CTLFLAG_NEEDGIANT, \
148 NULL, 0, array ## _sysctl, "I", desc)
149 #endif /* SYSCTL_OID */
152 #include <sys/lock.h>
153 #include <sys/proc.h> /* for struct thread */
154 #include <sys/rwlock.h>
158 * Location of the kernel's 'set_vnet' linker set.
160 extern uintptr_t *__start_set_vnet;
161 __GLOBL(__start_set_vnet);
162 extern uintptr_t *__stop_set_vnet;
163 __GLOBL(__stop_set_vnet);
165 #define VNET_START (uintptr_t)&__start_set_vnet
166 #define VNET_STOP (uintptr_t)&__stop_set_vnet
169 * Functions to allocate and destroy virtual network stacks.
171 struct vnet *vnet_alloc(void);
172 void vnet_destroy(struct vnet *vnet);
175 * The current virtual network stack -- we may wish to move this to struct
176 * pcpu in the future.
178 #define curvnet curthread->td_vnet
181 * Various macros -- get and set the current network stack, but also
184 #if defined(INVARIANTS) || defined(VNET_DEBUG)
185 #define VNET_ASSERT(exp, msg) do { \
190 #define VNET_ASSERT(exp, msg) do { \
195 void vnet_log_recursion(struct vnet *, const char *, int);
197 #define CURVNET_SET_QUIET(arg) \
198 VNET_ASSERT((arg) != NULL && (arg)->vnet_magic_n == VNET_MAGIC_N, \
199 ("CURVNET_SET at %s:%d %s() curvnet=%p vnet=%p", \
200 __FILE__, __LINE__, __func__, curvnet, (arg))); \
201 struct vnet *saved_vnet = curvnet; \
202 const char *saved_vnet_lpush = curthread->td_vnet_lpush; \
204 curthread->td_vnet_lpush = __func__;
206 #define CURVNET_SET_VERBOSE(arg) \
207 CURVNET_SET_QUIET(arg) \
209 vnet_log_recursion(saved_vnet, saved_vnet_lpush, __LINE__);
211 #define CURVNET_SET(arg) CURVNET_SET_VERBOSE(arg)
213 #define CURVNET_RESTORE() \
214 VNET_ASSERT(curvnet != NULL && (saved_vnet == NULL || \
215 saved_vnet->vnet_magic_n == VNET_MAGIC_N), \
216 ("CURVNET_RESTORE at %s:%d %s() curvnet=%p saved_vnet=%p", \
217 __FILE__, __LINE__, __func__, curvnet, saved_vnet)); \
218 curvnet = saved_vnet; \
219 curthread->td_vnet_lpush = saved_vnet_lpush;
220 #else /* !VNET_DEBUG */
222 #define CURVNET_SET_QUIET(arg) \
223 VNET_ASSERT((arg) != NULL && (arg)->vnet_magic_n == VNET_MAGIC_N, \
224 ("CURVNET_SET at %s:%d %s() curvnet=%p vnet=%p", \
225 __FILE__, __LINE__, __func__, curvnet, (arg))); \
226 struct vnet *saved_vnet = curvnet; \
229 #define CURVNET_SET_VERBOSE(arg) \
230 CURVNET_SET_QUIET(arg)
232 #define CURVNET_SET(arg) CURVNET_SET_VERBOSE(arg)
234 #define CURVNET_RESTORE() \
235 VNET_ASSERT(curvnet != NULL && (saved_vnet == NULL || \
236 saved_vnet->vnet_magic_n == VNET_MAGIC_N), \
237 ("CURVNET_RESTORE at %s:%d %s() curvnet=%p saved_vnet=%p", \
238 __FILE__, __LINE__, __func__, curvnet, saved_vnet)); \
239 curvnet = saved_vnet;
240 #endif /* VNET_DEBUG */
242 #define CURVNET_ASSERT_SET() \
243 VNET_ASSERT(curvnet != NULL, ("vnet is not set at %s:%d %s()", \
244 __FILE__, __LINE__, __func__))
246 extern struct vnet *vnet0;
247 #define IS_DEFAULT_VNET(arg) ((arg) == vnet0)
249 #define CRED_TO_VNET(cr) (cr)->cr_prison->pr_vnet
250 #define TD_TO_VNET(td) CRED_TO_VNET((td)->td_ucred)
251 #define P_TO_VNET(p) CRED_TO_VNET((p)->p_ucred)
254 * Global linked list of all virtual network stacks, along with read locks to
255 * access it. If a caller may sleep while accessing the list, it must use
256 * the sleepable lock macros.
258 LIST_HEAD(vnet_list_head, vnet);
259 extern struct vnet_list_head vnet_head;
260 extern struct rwlock vnet_rwlock;
261 extern struct sx vnet_sxlock;
263 #define VNET_LIST_RLOCK() sx_slock(&vnet_sxlock)
264 #define VNET_LIST_RLOCK_NOSLEEP() rw_rlock(&vnet_rwlock)
265 #define VNET_LIST_RUNLOCK() sx_sunlock(&vnet_sxlock)
266 #define VNET_LIST_RUNLOCK_NOSLEEP() rw_runlock(&vnet_rwlock)
269 * Iteration macros to walk the global list of virtual network stacks.
271 #define VNET_ITERATOR_DECL(arg) struct vnet *arg
272 #define VNET_FOREACH(arg) LIST_FOREACH((arg), &vnet_head, vnet_le)
275 * Virtual network stack memory allocator, which allows global variables to
276 * be automatically instantiated for each network stack instance.
278 #define VNET_NAME(n) vnet_entry_##n
279 #define VNET_DECLARE(t, n) extern t VNET_NAME(n)
280 /* struct _hack is to stop this from being used with static data */
281 #define VNET_DEFINE(t, n) \
282 struct _hack; t VNET_NAME(n) __section(VNET_SETNAME) __used
283 #if defined(KLD_MODULE) && (defined(__aarch64__) || defined(__riscv) \
284 || defined(__powerpc64__))
286 * As with DPCPU_DEFINE_STATIC we are unable to mark this data as static
287 * in modules on some architectures.
289 #define VNET_DEFINE_STATIC(t, n) \
290 t VNET_NAME(n) __section(VNET_SETNAME) __used
292 #define VNET_DEFINE_STATIC(t, n) \
293 static t VNET_NAME(n) __section(VNET_SETNAME) __used
295 #define _VNET_PTR(b, n) (__typeof(VNET_NAME(n))*) \
296 ((b) + (uintptr_t)&VNET_NAME(n))
298 #define _VNET(b, n) (*_VNET_PTR(b, n))
301 * Virtualized global variable accessor macros.
303 #define VNET_VNET_PTR(vnet, n) _VNET_PTR((vnet)->vnet_data_base, n)
304 #define VNET_VNET(vnet, n) (*VNET_VNET_PTR((vnet), n))
306 #define VNET_PTR(n) VNET_VNET_PTR(curvnet, n)
307 #define VNET(n) VNET_VNET(curvnet, n)
310 * Virtual network stack allocator interfaces from the kernel linker.
312 void *vnet_data_alloc(int size);
313 void vnet_data_copy(void *start, int size);
314 void vnet_data_free(void *start_arg, int size);
317 * Virtual sysinit mechanism, allowing network stack components to declare
318 * startup and shutdown methods to be run when virtual network stack
319 * instances are created and destroyed.
321 #include <sys/kernel.h>
324 * SYSINIT/SYSUNINIT variants that provide per-vnet constructors and
327 struct vnet_sysinit {
328 enum sysinit_sub_id subsystem;
329 enum sysinit_elem_order order;
330 sysinit_cfunc_t func;
332 TAILQ_ENTRY(vnet_sysinit) link;
335 #define VNET_SYSINIT(ident, subsystem, order, func, arg) \
336 CTASSERT((subsystem) > SI_SUB_VNET && \
337 (subsystem) <= SI_SUB_VNET_DONE); \
338 static struct vnet_sysinit ident ## _vnet_init = { \
341 (sysinit_cfunc_t)(sysinit_nfunc_t)func, \
344 SYSINIT(vnet_init_ ## ident, subsystem, order, \
345 vnet_register_sysinit, &ident ## _vnet_init); \
346 SYSUNINIT(vnet_init_ ## ident, subsystem, order, \
347 vnet_deregister_sysinit, &ident ## _vnet_init)
349 #define VNET_SYSUNINIT(ident, subsystem, order, func, arg) \
350 CTASSERT((subsystem) > SI_SUB_VNET && \
351 (subsystem) <= SI_SUB_VNET_DONE); \
352 static struct vnet_sysinit ident ## _vnet_uninit = { \
355 (sysinit_cfunc_t)(sysinit_nfunc_t)func, \
358 SYSINIT(vnet_uninit_ ## ident, subsystem, order, \
359 vnet_register_sysuninit, &ident ## _vnet_uninit); \
360 SYSUNINIT(vnet_uninit_ ## ident, subsystem, order, \
361 vnet_deregister_sysuninit, &ident ## _vnet_uninit)
364 * Interfaces for managing per-vnet constructors and destructors.
366 void vnet_register_sysinit(void *arg);
367 void vnet_register_sysuninit(void *arg);
368 void vnet_deregister_sysinit(void *arg);
369 void vnet_deregister_sysuninit(void *arg);
372 * EVENTHANDLER(9) extensions.
374 #include <sys/eventhandler.h>
376 void vnet_global_eventhandler_iterator_func(void *, ...);
377 #define VNET_GLOBAL_EVENTHANDLER_REGISTER_TAG(tag, name, func, arg, priority) \
379 if (IS_DEFAULT_VNET(curvnet)) { \
380 (tag) = vimage_eventhandler_register(NULL, #name, func, \
382 vnet_global_eventhandler_iterator_func); \
385 #define VNET_GLOBAL_EVENTHANDLER_REGISTER(name, func, arg, priority) \
387 if (IS_DEFAULT_VNET(curvnet)) { \
388 vimage_eventhandler_register(NULL, #name, func, \
390 vnet_global_eventhandler_iterator_func); \
397 * Various virtual network stack macros compile to no-ops without VIMAGE.
401 #define VNET_ASSERT(exp, msg)
402 #define CURVNET_SET(arg)
403 #define CURVNET_SET_QUIET(arg)
404 #define CURVNET_RESTORE()
405 #define CURVNET_ASSERT_SET() \
407 #define VNET_LIST_RLOCK()
408 #define VNET_LIST_RLOCK_NOSLEEP()
409 #define VNET_LIST_RUNLOCK()
410 #define VNET_LIST_RUNLOCK_NOSLEEP()
411 #define VNET_ITERATOR_DECL(arg)
412 #define VNET_FOREACH(arg) for (int _vn = 0; _vn == 0; _vn++)
414 #define IS_DEFAULT_VNET(arg) 1
415 #define CRED_TO_VNET(cr) NULL
416 #define TD_TO_VNET(td) NULL
417 #define P_TO_VNET(p) NULL
420 * Versions of the VNET macros that compile to normal global variables and
421 * standard sysctl definitions.
423 #define VNET_NAME(n) n
424 #define VNET_DECLARE(t, n) extern t n
425 #define VNET_DEFINE(t, n) struct _hack; t n
426 #define VNET_DEFINE_STATIC(t, n) static t n
427 #define _VNET_PTR(b, n) &VNET_NAME(n)
430 * Virtualized global variable accessor macros.
432 #define VNET_VNET_PTR(vnet, n) (&(n))
433 #define VNET_VNET(vnet, n) (n)
435 #define VNET_PTR(n) (&(n))
439 * When VIMAGE isn't compiled into the kernel, VNET_SYSINIT/VNET_SYSUNINIT
440 * map into normal sysinits, which have the same ordering properties.
442 #define VNET_SYSINIT(ident, subsystem, order, func, arg) \
443 SYSINIT(ident, subsystem, order, func, arg)
444 #define VNET_SYSUNINIT(ident, subsystem, order, func, arg) \
445 SYSUNINIT(ident, subsystem, order, func, arg)
448 * Without VIMAGE revert to the default implementation.
450 #define VNET_GLOBAL_EVENTHANDLER_REGISTER_TAG(tag, name, func, arg, priority) \
451 (tag) = eventhandler_register(NULL, #name, func, arg, priority)
452 #define VNET_GLOBAL_EVENTHANDLER_REGISTER(name, func, arg, priority) \
453 eventhandler_register(NULL, #name, func, arg, priority)
457 #endif /* !_NET_VNET_H_ */