]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/net/vnet.h
zfs: merge openzfs/zfs@a86e08941 (master) into main
[FreeBSD/FreeBSD.git] / sys / net / vnet.h
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2006-2009 University of Zagreb
5  * Copyright (c) 2006-2009 FreeBSD Foundation
6  * All rights reserved.
7  *
8  * This software was developed by the University of Zagreb and the
9  * FreeBSD Foundation under sponsorship by the Stichting NLnet and the
10  * FreeBSD Foundation.
11  *
12  * Copyright (c) 2009 Jeffrey Roberson <jeff@freebsd.org>
13  * Copyright (c) 2009 Robert N. M. Watson
14  * All rights reserved.
15  *
16  * Redistribution and use in source and binary forms, with or without
17  * modification, are permitted provided that the following conditions
18  * are met:
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.
24  *
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
35  * SUCH DAMAGE.
36  *
37  * $FreeBSD$
38  */
39
40 /*-
41  * This header file defines several sets of interfaces supporting virtualized
42  * network stacks:
43  *
44  * - Definition of 'struct vnet' and functions and macros to allocate/free/
45  *   manipulate it.
46  *
47  * - A virtual network stack memory allocator, which provides support for
48  *   virtualized global variables via a special linker set, set_vnet.
49  *
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.
53  *
54  * If VIMAGE isn't compiled into the kernel, virtualized global variables
55  * compile to normal global variables, and virtualized sysinits to regular
56  * sysinits.
57  */
58
59 #ifndef _NET_VNET_H_
60 #define _NET_VNET_H_
61
62 /*
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.
66  */
67 #if defined(_KERNEL) || defined(_WANT_VNET)
68 #include <sys/queue.h>
69
70 struct vnet {
71         LIST_ENTRY(vnet)         vnet_le;       /* all vnets list */
72         u_int                    vnet_magic_n;
73         u_int                    vnet_ifcnt;
74         u_int                    vnet_sockcnt;
75         u_int                    vnet_state;    /* SI_SUB_* */
76         void                    *vnet_data_mem;
77         uintptr_t                vnet_data_base;
78         bool                     vnet_shutdown; /* Shutdown in progress. */
79 } __aligned(CACHE_LINE_SIZE);
80 #define VNET_MAGIC_N    0x5e4a6f28
81
82 /*
83  * These two virtual network stack allocator definitions are also required
84  * for libkvm so that it can evaluate virtualized global variables.
85  */
86 #define VNET_SETNAME            "set_vnet"
87 #define VNET_SYMPREFIX          "vnet_entry_"
88 #endif
89
90 #ifdef _KERNEL
91
92 #define VNET_PCPUSTAT_DECLARE(type, name)       \
93     VNET_DECLARE(counter_u64_t, name[sizeof(type) / sizeof(uint64_t)])
94
95 #define VNET_PCPUSTAT_DEFINE(type, name)        \
96     VNET_DEFINE(counter_u64_t, name[sizeof(type) / sizeof(uint64_t)])
97 #define VNET_PCPUSTAT_DEFINE_STATIC(type, name) \
98     VNET_DEFINE_STATIC(counter_u64_t, name[sizeof(type) / sizeof(uint64_t)])
99
100 #define VNET_PCPUSTAT_ALLOC(name, wait) \
101     COUNTER_ARRAY_ALLOC(VNET(name), \
102         sizeof(VNET(name)) / sizeof(counter_u64_t), (wait))
103
104 #define VNET_PCPUSTAT_FREE(name)        \
105     COUNTER_ARRAY_FREE(VNET(name), sizeof(VNET(name)) / sizeof(counter_u64_t))
106
107 #define VNET_PCPUSTAT_ADD(type, name, f, v)     \
108     counter_u64_add(VNET(name)[offsetof(type, f) / sizeof(uint64_t)], (v))
109
110 #define VNET_PCPUSTAT_FETCH(type, name, f)      \
111     counter_u64_fetch(VNET(name)[offsetof(type, f) / sizeof(uint64_t)])
112
113 #define VNET_PCPUSTAT_SYSINIT(name)     \
114 static void                             \
115 vnet_##name##_init(const void *unused)  \
116 {                                       \
117         VNET_PCPUSTAT_ALLOC(name, M_WAITOK);    \
118 }                                       \
119 VNET_SYSINIT(vnet_ ## name ## _init, SI_SUB_INIT_IF,                    \
120     SI_ORDER_FIRST, vnet_ ## name ## _init, NULL)
121
122 #define VNET_PCPUSTAT_SYSUNINIT(name)                                   \
123 static void                                                             \
124 vnet_##name##_uninit(const void *unused)                                \
125 {                                                                       \
126         VNET_PCPUSTAT_FREE(name);                                       \
127 }                                                                       \
128 VNET_SYSUNINIT(vnet_ ## name ## _uninit, SI_SUB_INIT_IF,                \
129     SI_ORDER_FIRST, vnet_ ## name ## _uninit, NULL)
130
131 #ifdef SYSCTL_OID
132 #define SYSCTL_VNET_PCPUSTAT(parent, nbr, name, type, array, desc)      \
133 static int                                                              \
134 array##_sysctl(SYSCTL_HANDLER_ARGS)                                     \
135 {                                                                       \
136         type s;                                                         \
137         CTASSERT((sizeof(type) / sizeof(uint64_t)) ==                   \
138             (sizeof(VNET(array)) / sizeof(counter_u64_t)));             \
139         COUNTER_ARRAY_COPY(VNET(array), &s, sizeof(type) / sizeof(uint64_t));\
140         if (req->newptr)                                                \
141                 COUNTER_ARRAY_ZERO(VNET(array),                         \
142                     sizeof(type) / sizeof(uint64_t));                   \
143         return (SYSCTL_OUT(req, &s, sizeof(type)));                     \
144 }                                                                       \
145 SYSCTL_PROC(parent, nbr, name,                                          \
146     CTLFLAG_VNET | CTLTYPE_OPAQUE | CTLFLAG_RW | CTLFLAG_NEEDGIANT,     \
147     NULL, 0, array ## _sysctl, "I", desc)
148 #endif /* SYSCTL_OID */
149
150 #ifdef VIMAGE
151 #include <sys/lock.h>
152 #include <sys/proc.h>                   /* for struct thread */
153 #include <sys/rwlock.h>
154 #include <sys/sx.h>
155
156 /*
157  * Location of the kernel's 'set_vnet' linker set.
158  */
159 extern uintptr_t        *__start_set_vnet;
160 __GLOBL(__start_set_vnet);
161 extern uintptr_t        *__stop_set_vnet;
162 __GLOBL(__stop_set_vnet);
163
164 #define VNET_START      (uintptr_t)&__start_set_vnet
165 #define VNET_STOP       (uintptr_t)&__stop_set_vnet
166
167 /*
168  * Functions to allocate and destroy virtual network stacks.
169  */
170 struct vnet *vnet_alloc(void);
171 void    vnet_destroy(struct vnet *vnet);
172
173 /*
174  * The current virtual network stack -- we may wish to move this to struct
175  * pcpu in the future.
176  */
177 #define curvnet curthread->td_vnet
178
179 /*
180  * Various macros -- get and set the current network stack, but also
181  * assertions.
182  */
183 #if defined(INVARIANTS) || defined(VNET_DEBUG)
184 #define VNET_ASSERT(exp, msg)   do {                                    \
185         if (!(exp))                                                     \
186                 panic msg;                                              \
187 } while (0)
188 #else
189 #define VNET_ASSERT(exp, msg)   do {                                    \
190 } while (0)
191 #endif
192
193 #ifdef VNET_DEBUG
194 void vnet_log_recursion(struct vnet *, const char *, int);
195
196 #define CURVNET_SET_QUIET(arg)                                          \
197         VNET_ASSERT((arg) != NULL && (arg)->vnet_magic_n == VNET_MAGIC_N, \
198             ("CURVNET_SET at %s:%d %s() curvnet=%p vnet=%p",            \
199             __FILE__, __LINE__, __func__, curvnet, (arg)));             \
200         struct vnet *saved_vnet = curvnet;                              \
201         const char *saved_vnet_lpush = curthread->td_vnet_lpush;        \
202         curvnet = arg;                                                  \
203         curthread->td_vnet_lpush = __func__;
204
205 #define CURVNET_SET_VERBOSE(arg)                                        \
206         CURVNET_SET_QUIET(arg)                                          \
207         if (saved_vnet)                                                 \
208                 vnet_log_recursion(saved_vnet, saved_vnet_lpush, __LINE__);
209
210 #define CURVNET_SET(arg)        CURVNET_SET_VERBOSE(arg)
211
212 #define CURVNET_RESTORE()                                               \
213         VNET_ASSERT(curvnet != NULL && (saved_vnet == NULL ||           \
214             saved_vnet->vnet_magic_n == VNET_MAGIC_N),                  \
215             ("CURVNET_RESTORE at %s:%d %s() curvnet=%p saved_vnet=%p",  \
216             __FILE__, __LINE__, __func__, curvnet, saved_vnet));        \
217         curvnet = saved_vnet;                                           \
218         curthread->td_vnet_lpush = saved_vnet_lpush;
219 #else /* !VNET_DEBUG */
220
221 #define CURVNET_SET_QUIET(arg)                                          \
222         VNET_ASSERT((arg) != NULL && (arg)->vnet_magic_n == VNET_MAGIC_N, \
223             ("CURVNET_SET at %s:%d %s() curvnet=%p vnet=%p",            \
224             __FILE__, __LINE__, __func__, curvnet, (arg)));             \
225         struct vnet *saved_vnet = curvnet;                              \
226         curvnet = arg;  
227
228 #define CURVNET_SET_VERBOSE(arg)                                        \
229         CURVNET_SET_QUIET(arg)
230
231 #define CURVNET_SET(arg)        CURVNET_SET_VERBOSE(arg)
232
233 #define CURVNET_RESTORE()                                               \
234         VNET_ASSERT(curvnet != NULL && (saved_vnet == NULL ||           \
235             saved_vnet->vnet_magic_n == VNET_MAGIC_N),                  \
236             ("CURVNET_RESTORE at %s:%d %s() curvnet=%p saved_vnet=%p",  \
237             __FILE__, __LINE__, __func__, curvnet, saved_vnet));        \
238         curvnet = saved_vnet;
239 #endif /* VNET_DEBUG */
240
241 #define CURVNET_ASSERT_SET()                                            \
242         VNET_ASSERT(curvnet != NULL, ("vnet is not set at %s:%d %s()",  \
243             __FILE__, __LINE__, __func__))
244
245 extern struct vnet *vnet0;
246 #define IS_DEFAULT_VNET(arg)    ((arg) == vnet0)
247
248 #define CRED_TO_VNET(cr)        (cr)->cr_prison->pr_vnet
249 #define TD_TO_VNET(td)          CRED_TO_VNET((td)->td_ucred)
250 #define P_TO_VNET(p)            CRED_TO_VNET((p)->p_ucred)
251
252 /*
253  * Global linked list of all virtual network stacks, along with read locks to
254  * access it.  If a caller may sleep while accessing the list, it must use
255  * the sleepable lock macros.
256  */
257 LIST_HEAD(vnet_list_head, vnet);
258 extern struct vnet_list_head vnet_head;
259 extern struct rwlock vnet_rwlock;
260 extern struct sx vnet_sxlock;
261
262 #define VNET_LIST_RLOCK()               sx_slock(&vnet_sxlock)
263 #define VNET_LIST_RLOCK_NOSLEEP()       rw_rlock(&vnet_rwlock)
264 #define VNET_LIST_RUNLOCK()             sx_sunlock(&vnet_sxlock)
265 #define VNET_LIST_RUNLOCK_NOSLEEP()     rw_runlock(&vnet_rwlock)
266
267 /*
268  * Iteration macros to walk the global list of virtual network stacks.
269  */
270 #define VNET_ITERATOR_DECL(arg) struct vnet *arg
271 #define VNET_FOREACH(arg)       LIST_FOREACH((arg), &vnet_head, vnet_le)
272
273 /*
274  * Virtual network stack memory allocator, which allows global variables to
275  * be automatically instantiated for each network stack instance.
276  */
277 #define VNET_NAME(n)            vnet_entry_##n
278 #define VNET_DECLARE(t, n)      extern t VNET_NAME(n)
279 /* struct _hack is to stop this from being used with static data */
280 #define VNET_DEFINE(t, n)       \
281     struct _hack; t VNET_NAME(n) __section(VNET_SETNAME) __used
282 #if defined(KLD_MODULE) && (defined(__aarch64__) || defined(__riscv) \
283                 || defined(__powerpc64__))
284 /*
285  * As with DPCPU_DEFINE_STATIC we are unable to mark this data as static
286  * in modules on some architectures.
287  */
288 #define VNET_DEFINE_STATIC(t, n) \
289     t VNET_NAME(n) __section(VNET_SETNAME) __used
290 #else
291 #define VNET_DEFINE_STATIC(t, n) \
292     static t VNET_NAME(n) __section(VNET_SETNAME) __used
293 #endif
294 #define _VNET_PTR(b, n)         (__typeof(VNET_NAME(n))*)               \
295                                     ((b) + (uintptr_t)&VNET_NAME(n))
296
297 #define _VNET(b, n)             (*_VNET_PTR(b, n))
298
299 /*
300  * Virtualized global variable accessor macros.
301  */
302 #define VNET_VNET_PTR(vnet, n)          _VNET_PTR((vnet)->vnet_data_base, n)
303 #define VNET_VNET(vnet, n)              (*VNET_VNET_PTR((vnet), n))
304
305 #define VNET_PTR(n)             VNET_VNET_PTR(curvnet, n)
306 #define VNET(n)                 VNET_VNET(curvnet, n)
307
308 /*
309  * Virtual network stack allocator interfaces from the kernel linker.
310  */
311 void    *vnet_data_alloc(int size);
312 void     vnet_data_copy(void *start, int size);
313 void     vnet_data_free(void *start_arg, int size);
314
315 /*
316  * Virtual sysinit mechanism, allowing network stack components to declare
317  * startup and shutdown methods to be run when virtual network stack
318  * instances are created and destroyed.
319  */
320 #include <sys/kernel.h>
321
322 /*
323  * SYSINIT/SYSUNINIT variants that provide per-vnet constructors and
324  * destructors.
325  */
326 struct vnet_sysinit {
327         enum sysinit_sub_id     subsystem;
328         enum sysinit_elem_order order;
329         sysinit_cfunc_t         func;
330         const void              *arg;
331         TAILQ_ENTRY(vnet_sysinit) link;
332 };
333
334 #define VNET_SYSINIT(ident, subsystem, order, func, arg)                \
335         CTASSERT((subsystem) > SI_SUB_VNET &&                           \
336             (subsystem) <= SI_SUB_VNET_DONE);                           \
337         static struct vnet_sysinit ident ## _vnet_init = {              \
338                 subsystem,                                              \
339                 order,                                                  \
340                 (sysinit_cfunc_t)(sysinit_nfunc_t)func,                 \
341                 (arg)                                                   \
342         };                                                              \
343         SYSINIT(vnet_init_ ## ident, subsystem, order,                  \
344             vnet_register_sysinit, &ident ## _vnet_init);               \
345         SYSUNINIT(vnet_init_ ## ident, subsystem, order,                \
346             vnet_deregister_sysinit, &ident ## _vnet_init)
347
348 #define VNET_SYSUNINIT(ident, subsystem, order, func, arg)              \
349         CTASSERT((subsystem) > SI_SUB_VNET &&                           \
350             (subsystem) <= SI_SUB_VNET_DONE);                           \
351         static struct vnet_sysinit ident ## _vnet_uninit = {            \
352                 subsystem,                                              \
353                 order,                                                  \
354                 (sysinit_cfunc_t)(sysinit_nfunc_t)func,                 \
355                 (arg)                                                   \
356         };                                                              \
357         SYSINIT(vnet_uninit_ ## ident, subsystem, order,                \
358             vnet_register_sysuninit, &ident ## _vnet_uninit);           \
359         SYSUNINIT(vnet_uninit_ ## ident, subsystem, order,              \
360             vnet_deregister_sysuninit, &ident ## _vnet_uninit)
361
362 /*
363  * Run per-vnet sysinits or sysuninits during vnet creation/destruction.
364  */
365 void     vnet_sysinit(void);
366 void     vnet_sysuninit(void);
367
368 /*
369  * Interfaces for managing per-vnet constructors and destructors.
370  */
371 void    vnet_register_sysinit(void *arg);
372 void    vnet_register_sysuninit(void *arg);
373 void    vnet_deregister_sysinit(void *arg);
374 void    vnet_deregister_sysuninit(void *arg);
375
376 /*
377  * EVENTHANDLER(9) extensions.
378  */
379 #include <sys/eventhandler.h>
380
381 void    vnet_global_eventhandler_iterator_func(void *, ...);
382 #define VNET_GLOBAL_EVENTHANDLER_REGISTER_TAG(tag, name, func, arg, priority) \
383 do {                                                                    \
384         if (IS_DEFAULT_VNET(curvnet)) {                                 \
385                 (tag) = vimage_eventhandler_register(NULL, #name, func, \
386                     arg, priority,                                      \
387                     vnet_global_eventhandler_iterator_func);            \
388         }                                                               \
389 } while(0)
390 #define VNET_GLOBAL_EVENTHANDLER_REGISTER(name, func, arg, priority)    \
391 do {                                                                    \
392         if (IS_DEFAULT_VNET(curvnet)) {                                 \
393                 vimage_eventhandler_register(NULL, #name, func,         \
394                     arg, priority,                                      \
395                     vnet_global_eventhandler_iterator_func);            \
396         }                                                               \
397 } while(0)
398
399 #else /* !VIMAGE */
400
401 /*
402  * Various virtual network stack macros compile to no-ops without VIMAGE.
403  */
404 #define curvnet                 NULL
405
406 #define VNET_ASSERT(exp, msg)
407 #define CURVNET_SET(arg)
408 #define CURVNET_SET_QUIET(arg)
409 #define CURVNET_RESTORE()
410 #define CURVNET_ASSERT_SET()                                            \
411
412 #define VNET_LIST_RLOCK()
413 #define VNET_LIST_RLOCK_NOSLEEP()
414 #define VNET_LIST_RUNLOCK()
415 #define VNET_LIST_RUNLOCK_NOSLEEP()
416 #define VNET_ITERATOR_DECL(arg)
417 #define VNET_FOREACH(arg)
418
419 #define IS_DEFAULT_VNET(arg)    1
420 #define CRED_TO_VNET(cr)        NULL
421 #define TD_TO_VNET(td)          NULL
422 #define P_TO_VNET(p)            NULL
423
424 /*
425  * Versions of the VNET macros that compile to normal global variables and
426  * standard sysctl definitions.
427  */
428 #define VNET_NAME(n)            n
429 #define VNET_DECLARE(t, n)      extern t n
430 #define VNET_DEFINE(t, n)       struct _hack; t n
431 #define VNET_DEFINE_STATIC(t, n)        static t n
432 #define _VNET_PTR(b, n)         &VNET_NAME(n)
433
434 /*
435  * Virtualized global variable accessor macros.
436  */
437 #define VNET_VNET_PTR(vnet, n)          (&(n))
438 #define VNET_VNET(vnet, n)              (n)
439
440 #define VNET_PTR(n)             (&(n))
441 #define VNET(n)                 (n)
442
443 /*
444  * When VIMAGE isn't compiled into the kernel, VNET_SYSINIT/VNET_SYSUNINIT
445  * map into normal sysinits, which have the same ordering properties.
446  */
447 #define VNET_SYSINIT(ident, subsystem, order, func, arg)                \
448         SYSINIT(ident, subsystem, order, func, arg)
449 #define VNET_SYSUNINIT(ident, subsystem, order, func, arg)              \
450         SYSUNINIT(ident, subsystem, order, func, arg)
451
452 /*
453  * Without VIMAGE revert to the default implementation.
454  */
455 #define VNET_GLOBAL_EVENTHANDLER_REGISTER_TAG(tag, name, func, arg, priority) \
456         (tag) = eventhandler_register(NULL, #name, func, arg, priority)
457 #define VNET_GLOBAL_EVENTHANDLER_REGISTER(name, func, arg, priority)    \
458         eventhandler_register(NULL, #name, func, arg, priority)
459 #endif /* VIMAGE */
460 #endif /* _KERNEL */
461
462 #endif /* !_NET_VNET_H_ */