]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - sys/net/vnet.h
MFC r215238 (originally by kib):
[FreeBSD/stable/8.git] / sys / net / vnet.h
1 /*-
2  * Copyright (c) 2006-2009 University of Zagreb
3  * Copyright (c) 2006-2009 FreeBSD Foundation
4  * All rights reserved.
5  *
6  * This software was developed by the University of Zagreb and the
7  * FreeBSD Foundation under sponsorship by the Stichting NLnet and the
8  * FreeBSD Foundation.
9  *
10  * Copyright (c) 2009 Jeffrey Roberson <jeff@freebsd.org>
11  * Copyright (c) 2009 Robert N. M. Watson
12  * All rights reserved.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
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.
22  *
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
33  * SUCH DAMAGE.
34  *
35  * $FreeBSD$
36  */
37
38 /*-
39  * This header file defines several sets of interfaces supporting virtualized
40  * network stacks:
41  *
42  * - Definition of 'struct vnet' and functions and macros to allocate/free/
43  *   manipulate it.
44  *
45  * - A virtual network stack memory allocator, which provides support for
46  *   virtualized global variables via a special linker set, set_vnet.
47  *
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.
51  *
52  * If VIMAGE isn't compiled into the kernel, virtualized global variables
53  * compile to normal global variables, and virtualized sysinits to regular
54  * sysinits.
55  */
56
57 #ifndef _NET_VNET_H_
58 #define _NET_VNET_H_
59
60 /*
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.
64  */
65 #if defined(_KERNEL) || defined(_WANT_VNET)
66 #include <sys/queue.h>
67
68 struct vnet {
69         LIST_ENTRY(vnet)         vnet_le;       /* all vnets list */
70         u_int                    vnet_magic_n;
71         u_int                    vnet_ifcnt;
72         u_int                    vnet_sockcnt;
73         void                    *vnet_data_mem;
74         uintptr_t                vnet_data_base;
75 };
76 #define VNET_MAGIC_N    0x3e0d8f29
77
78 /*
79  * These two virtual network stack allocator definitions are also required
80  * for libkvm so that it can evaluate virtualized global variables.
81  */
82 #define VNET_SETNAME            "set_vnet"
83 #define VNET_SYMPREFIX          "vnet_entry_"
84 #endif
85
86 #ifdef _KERNEL
87
88 #ifdef VIMAGE
89 #include <sys/lock.h>
90 #include <sys/proc.h>                   /* for struct thread */
91 #include <sys/rwlock.h>
92 #include <sys/sx.h>
93
94 /*
95  * Location of the kernel's 'set_vnet' linker set.
96  */
97 extern uintptr_t        *__start_set_vnet;
98 extern uintptr_t        *__stop_set_vnet;
99
100 #define VNET_START      (uintptr_t)&__start_set_vnet
101 #define VNET_STOP       (uintptr_t)&__stop_set_vnet
102
103 /*
104  * Functions to allocate and destroy virtual network stacks.
105  */
106 struct vnet *vnet_alloc(void);
107 void    vnet_destroy(struct vnet *vnet);
108
109 /*
110  * The current virtual network stack -- we may wish to move this to struct
111  * pcpu in the future.
112  */
113 #define curvnet curthread->td_vnet
114
115 /*
116  * Various macros -- get and set the current network stack, but also
117  * assertions.
118  */
119 #ifdef VNET_DEBUG
120 void vnet_log_recursion(struct vnet *, const char *, int);
121
122 #define VNET_ASSERT(condition)                                          \
123         if (!(condition)) {                                             \
124                 printf("VNET_ASSERT @ %s:%d %s():\n",                   \
125                         __FILE__, __LINE__, __FUNCTION__);              \
126                 panic(#condition);                                      \
127         }
128
129 #define CURVNET_SET_QUIET(arg)                                          \
130         VNET_ASSERT((arg)->vnet_magic_n == VNET_MAGIC_N);               \
131         struct vnet *saved_vnet = curvnet;                              \
132         const char *saved_vnet_lpush = curthread->td_vnet_lpush;        \
133         curvnet = arg;                                                  \
134         curthread->td_vnet_lpush = __FUNCTION__;
135  
136 #define CURVNET_SET_VERBOSE(arg)                                        \
137         CURVNET_SET_QUIET(arg)                                          \
138         if (saved_vnet)                                                 \
139                 vnet_log_recursion(saved_vnet, saved_vnet_lpush, __LINE__);
140
141 #define CURVNET_SET(arg)        CURVNET_SET_VERBOSE(arg)
142  
143 #define CURVNET_RESTORE()                                               \
144         VNET_ASSERT(saved_vnet == NULL ||                               \
145                     saved_vnet->vnet_magic_n == VNET_MAGIC_N);          \
146         curvnet = saved_vnet;                                           \
147         curthread->td_vnet_lpush = saved_vnet_lpush;
148 #else /* !VNET_DEBUG */
149 #define VNET_ASSERT(condition)
150
151 #define CURVNET_SET(arg)                                                \
152         struct vnet *saved_vnet = curvnet;                              \
153         curvnet = arg;  
154  
155 #define CURVNET_SET_VERBOSE(arg)        CURVNET_SET(arg)
156 #define CURVNET_SET_QUIET(arg)          CURVNET_SET(arg)
157  
158 #define CURVNET_RESTORE()                                               \
159         curvnet = saved_vnet;
160 #endif /* VNET_DEBUG */
161
162 extern struct vnet *vnet0;
163 #define IS_DEFAULT_VNET(arg)    ((arg) == vnet0)
164
165 #define CRED_TO_VNET(cr)        (cr)->cr_prison->pr_vnet
166 #define TD_TO_VNET(td)          CRED_TO_VNET((td)->td_ucred)
167 #define P_TO_VNET(p)            CRED_TO_VNET((p)->p_ucred)
168
169 /*
170  * Global linked list of all virtual network stacks, along with read locks to
171  * access it.  If a caller may sleep while accessing the list, it must use
172  * the sleepable lock macros.
173  */
174 LIST_HEAD(vnet_list_head, vnet);
175 extern struct vnet_list_head vnet_head;
176 extern struct rwlock vnet_rwlock;
177 extern struct sx vnet_sxlock;
178
179 #define VNET_LIST_RLOCK()               sx_slock(&vnet_sxlock)
180 #define VNET_LIST_RLOCK_NOSLEEP()       rw_rlock(&vnet_rwlock)
181 #define VNET_LIST_RUNLOCK()             sx_sunlock(&vnet_sxlock)
182 #define VNET_LIST_RUNLOCK_NOSLEEP()     rw_runlock(&vnet_rwlock)
183
184 /*
185  * Iteration macros to walk the global list of virtual network stacks.
186  */
187 #define VNET_ITERATOR_DECL(arg) struct vnet *arg
188 #define VNET_FOREACH(arg)       LIST_FOREACH((arg), &vnet_head, vnet_le)
189
190 /*
191  * Virtual network stack memory allocator, which allows global variables to
192  * be automatically instantiated for each network stack instance.
193  */
194 #define VNET_NAME(n)            vnet_entry_##n
195 #define VNET_DECLARE(t, n)      extern t VNET_NAME(n)
196 #define VNET_DEFINE(t, n)       t VNET_NAME(n) __section(VNET_SETNAME) __used
197 #define _VNET_PTR(b, n)         (__typeof(VNET_NAME(n))*)               \
198                                     ((b) + (uintptr_t)&VNET_NAME(n))
199
200 #define _VNET(b, n)             (*_VNET_PTR(b, n))
201
202 /*
203  * Virtualized global variable accessor macros.
204  */
205 #define VNET_VNET_PTR(vnet, n)          _VNET_PTR((vnet)->vnet_data_base, n)
206 #define VNET_VNET(vnet, n)              (*VNET_VNET_PTR((vnet), n))
207
208 #define VNET_PTR(n)             VNET_VNET_PTR(curvnet, n)
209 #define VNET(n)                 VNET_VNET(curvnet, n)
210
211 /*
212  * Virtual network stack allocator interfaces from the kernel linker.
213  */
214 void    *vnet_data_alloc(int size);
215 void     vnet_data_copy(void *start, int size);
216 void     vnet_data_free(void *start_arg, int size);
217
218 /*
219  * Sysctl variants for vnet-virtualized global variables.  Include
220  * <sys/sysctl.h> to expose these definitions.
221  *
222  * Note: SYSCTL_PROC() handler functions will need to resolve pointer
223  * arguments themselves, if required.
224  */
225 #ifdef SYSCTL_OID
226 int     vnet_sysctl_handle_int(SYSCTL_HANDLER_ARGS);
227 int     vnet_sysctl_handle_opaque(SYSCTL_HANDLER_ARGS);
228 int     vnet_sysctl_handle_string(SYSCTL_HANDLER_ARGS);
229 int     vnet_sysctl_handle_uint(SYSCTL_HANDLER_ARGS);
230
231 #define SYSCTL_VNET_INT(parent, nbr, name, access, ptr, val, descr)     \
232         SYSCTL_OID(parent, nbr, name,                                   \
233             CTLTYPE_INT|CTLFLAG_MPSAFE|CTLFLAG_VNET|(access),           \
234             ptr, val, vnet_sysctl_handle_int, "I", descr)
235 #define SYSCTL_VNET_PROC(parent, nbr, name, access, ptr, arg, handler,  \
236             fmt, descr)                                                 \
237         SYSCTL_OID(parent, nbr, name, CTLFLAG_VNET|(access), ptr, arg,  \
238             handler, fmt, descr)
239 #define SYSCTL_VNET_OPAQUE(parent, nbr, name, access, ptr, len, fmt,    \
240             descr)                                                      \
241         SYSCTL_OID(parent, nbr, name,                                   \
242             CTLTYPE_OPAQUE|CTLFLAG_VNET|(access), ptr, len,             \
243             vnet_sysctl_handle_opaque, fmt, descr)
244 #define SYSCTL_VNET_STRING(parent, nbr, name, access, arg, len, descr)  \
245         SYSCTL_OID(parent, nbr, name,                                   \
246             CTLTYPE_STRING|CTLFLAG_VNET|(access),                       \
247             arg, len, vnet_sysctl_handle_string, "A", descr)
248 #define SYSCTL_VNET_STRUCT(parent, nbr, name, access, ptr, type, descr) \
249         SYSCTL_OID(parent, nbr, name,                                   \
250             CTLTYPE_OPAQUE|CTLFLAG_VNET|(access), ptr,                  \
251             sizeof(struct type), vnet_sysctl_handle_opaque, "S," #type, \
252             descr)
253 #define SYSCTL_VNET_UINT(parent, nbr, name, access, ptr, val, descr)    \
254         SYSCTL_OID(parent, nbr, name,                                   \
255             CTLTYPE_UINT|CTLFLAG_MPSAFE|CTLFLAG_VNET|(access),          \
256             ptr, val, vnet_sysctl_handle_uint, "IU", descr)
257 #define VNET_SYSCTL_ARG(req, arg1) do {                                 \
258         if (arg1 != NULL)                                               \
259                 arg1 = (void *)(TD_TO_VNET((req)->td)->vnet_data_base + \
260                     (uintptr_t)(arg1));                                 \
261 } while (0)
262 #endif /* SYSCTL_OID */
263
264 /*
265  * Virtual sysinit mechanism, allowing network stack components to declare
266  * startup and shutdown methods to be run when virtual network stack
267  * instances are created and destroyed.
268  */
269 #include <sys/kernel.h>
270
271 /*
272  * SYSINIT/SYSUNINIT variants that provide per-vnet constructors and
273  * destructors.
274  */
275 struct vnet_sysinit {
276         enum sysinit_sub_id     subsystem;
277         enum sysinit_elem_order order;
278         sysinit_cfunc_t         func;
279         const void              *arg;
280         TAILQ_ENTRY(vnet_sysinit) link;
281 };
282
283 #define VNET_SYSINIT(ident, subsystem, order, func, arg)                \
284         static struct vnet_sysinit ident ## _vnet_init = {              \
285                 subsystem,                                              \
286                 order,                                                  \
287                 (sysinit_cfunc_t)(sysinit_nfunc_t)func,                 \
288                 (arg)                                                   \
289         };                                                              \
290         SYSINIT(vnet_init_ ## ident, subsystem, order,                  \
291             vnet_register_sysinit, &ident ## _vnet_init);               \
292         SYSUNINIT(vnet_init_ ## ident, subsystem, order,                \
293             vnet_deregister_sysinit, &ident ## _vnet_init)
294
295 #define VNET_SYSUNINIT(ident, subsystem, order, func, arg)              \
296         static struct vnet_sysinit ident ## _vnet_uninit = {            \
297                 subsystem,                                              \
298                 order,                                                  \
299                 (sysinit_cfunc_t)(sysinit_nfunc_t)func,                 \
300                 (arg)                                                   \
301         };                                                              \
302         SYSINIT(vnet_uninit_ ## ident, subsystem, order,                \
303             vnet_register_sysuninit, &ident ## _vnet_uninit);           \
304         SYSUNINIT(vnet_uninit_ ## ident, subsystem, order,              \
305             vnet_deregister_sysuninit, &ident ## _vnet_uninit)
306
307 /*
308  * Run per-vnet sysinits or sysuninits during vnet creation/destruction.
309  */
310 void     vnet_sysinit(void);
311 void     vnet_sysuninit(void);
312
313 /*
314  * Interfaces for managing per-vnet constructors and destructors.
315  */
316 void    vnet_register_sysinit(void *arg);
317 void    vnet_register_sysuninit(void *arg);
318 void    vnet_deregister_sysinit(void *arg);
319 void    vnet_deregister_sysuninit(void *arg);
320
321 /*
322  * EVENTHANDLER(9) extensions.
323  */
324 #include <sys/eventhandler.h>
325
326 void    vnet_global_eventhandler_iterator_func(void *, ...);
327 #define VNET_GLOBAL_EVENTHANDLER_REGISTER_TAG(tag, name, func, arg, priority) \
328 do {                                                                    \
329         if (IS_DEFAULT_VNET(curvnet)) {                                 \
330                 (tag) = vimage_eventhandler_register(NULL, #name, func, \
331                     arg, priority,                                      \
332                     vnet_global_eventhandler_iterator_func);            \
333         }                                                               \
334 } while(0)
335 #define VNET_GLOBAL_EVENTHANDLER_REGISTER(name, func, arg, priority)    \
336 do {                                                                    \
337         if (IS_DEFAULT_VNET(curvnet)) {                                 \
338                 vimage_eventhandler_register(NULL, #name, func,         \
339                     arg, priority,                                      \
340                     vnet_global_eventhandler_iterator_func);            \
341         }                                                               \
342 } while(0)
343
344 #else /* !VIMAGE */
345
346 /*
347  * Various virtual network stack macros compile to no-ops without VIMAGE.
348  */
349 #define curvnet                 NULL
350
351 #define VNET_ASSERT(condition)
352 #define CURVNET_SET(arg)
353 #define CURVNET_SET_QUIET(arg)
354 #define CURVNET_RESTORE()
355
356 #define VNET_LIST_RLOCK()
357 #define VNET_LIST_RLOCK_NOSLEEP()
358 #define VNET_LIST_RUNLOCK()
359 #define VNET_LIST_RUNLOCK_NOSLEEP()
360 #define VNET_ITERATOR_DECL(arg)
361 #define VNET_FOREACH(arg)
362
363 #define IS_DEFAULT_VNET(arg)    1
364 #define CRED_TO_VNET(cr)        NULL
365 #define TD_TO_VNET(td)          NULL
366 #define P_TO_VNET(p)            NULL
367
368 /*
369  * Versions of the VNET macros that compile to normal global variables and
370  * standard sysctl definitions.
371  */
372 #define VNET_NAME(n)            n
373 #define VNET_DECLARE(t, n)      extern t n
374 #define VNET_DEFINE(t, n)       t n
375 #define _VNET_PTR(b, n)         &VNET_NAME(n)
376
377 /*
378  * Virtualized global variable accessor macros.
379  */
380 #define VNET_VNET_PTR(vnet, n)          (&(n))
381 #define VNET_VNET(vnet, n)              (n)
382
383 #define VNET_PTR(n)             (&(n))
384 #define VNET(n)                 (n)
385
386 /*
387  * When VIMAGE isn't compiled into the kernel, virtaulized SYSCTLs simply
388  * become normal SYSCTLs.
389  */
390 #ifdef SYSCTL_OID
391 #define SYSCTL_VNET_INT(parent, nbr, name, access, ptr, val, descr)     \
392         SYSCTL_INT(parent, nbr, name, access, ptr, val, descr)
393 #define SYSCTL_VNET_PROC(parent, nbr, name, access, ptr, arg, handler,  \
394             fmt, descr)                                                 \
395         SYSCTL_PROC(parent, nbr, name, access, ptr, arg, handler, fmt,  \
396             descr)
397 #define SYSCTL_VNET_OPAQUE(parent, nbr, name, access, ptr, len, fmt,    \
398             descr)                                                      \
399         SYSCTL_OPAQUE(parent, nbr, name, access, ptr, len, fmt, descr)
400 #define SYSCTL_VNET_STRING(parent, nbr, name, access, arg, len, descr)  \
401         SYSCTL_STRING(parent, nbr, name, access, arg, len, descr)
402 #define SYSCTL_VNET_STRUCT(parent, nbr, name, access, ptr, type, descr) \
403         SYSCTL_STRUCT(parent, nbr, name, access, ptr, type, descr)
404 #define SYSCTL_VNET_UINT(parent, nbr, name, access, ptr, val, descr)    \
405         SYSCTL_UINT(parent, nbr, name, access, ptr, val, descr)
406 #define VNET_SYSCTL_ARG(req, arg1)
407 #endif /* SYSCTL_OID */
408
409 /*
410  * When VIMAGE isn't compiled into the kernel, VNET_SYSINIT/VNET_SYSUNINIT
411  * map into normal sysinits, which have the same ordering properties.
412  */
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)
417
418 /*
419  * Without VIMAGE revert to the default implementation.
420  */
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)
425 #endif /* VIMAGE */
426 #endif /* _KERNEL */
427
428 #endif /* !_NET_VNET_H_ */