Deleted Added
full compact
vnet.h (215318) vnet.h (215701)
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 *
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: head/sys/net/vnet.h 215318 2010-11-14 20:40:55Z dim $
35 * $FreeBSD: head/sys/net/vnet.h 215701 2010-11-22 19:32:54Z dim $
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
68struct 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 */
97extern uintptr_t *__start_set_vnet;
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
68struct 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 */
97extern uintptr_t *__start_set_vnet;
98__GLOBL(__start_set_vnet);
98extern uintptr_t *__stop_set_vnet;
99extern uintptr_t *__stop_set_vnet;
100__GLOBL(__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 */
106struct vnet *vnet_alloc(void);
107void 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
120void 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
162extern 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 */
174LIST_HEAD(vnet_list_head, vnet);
175extern struct vnet_list_head vnet_head;
176extern struct rwlock vnet_rwlock;
177extern 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)
101
102#define VNET_START (uintptr_t)&__start_set_vnet
103#define VNET_STOP (uintptr_t)&__stop_set_vnet
104
105/*
106 * Functions to allocate and destroy virtual network stacks.
107 */
108struct vnet *vnet_alloc(void);
109void vnet_destroy(struct vnet *vnet);
110
111/*
112 * The current virtual network stack -- we may wish to move this to struct
113 * pcpu in the future.
114 */
115#define curvnet curthread->td_vnet
116
117/*
118 * Various macros -- get and set the current network stack, but also
119 * assertions.
120 */
121#ifdef VNET_DEBUG
122void vnet_log_recursion(struct vnet *, const char *, int);
123
124#define VNET_ASSERT(condition) \
125 if (!(condition)) { \
126 printf("VNET_ASSERT @ %s:%d %s():\n", \
127 __FILE__, __LINE__, __FUNCTION__); \
128 panic(#condition); \
129 }
130
131#define CURVNET_SET_QUIET(arg) \
132 VNET_ASSERT((arg)->vnet_magic_n == VNET_MAGIC_N); \
133 struct vnet *saved_vnet = curvnet; \
134 const char *saved_vnet_lpush = curthread->td_vnet_lpush; \
135 curvnet = arg; \
136 curthread->td_vnet_lpush = __FUNCTION__;
137
138#define CURVNET_SET_VERBOSE(arg) \
139 CURVNET_SET_QUIET(arg) \
140 if (saved_vnet) \
141 vnet_log_recursion(saved_vnet, saved_vnet_lpush, __LINE__);
142
143#define CURVNET_SET(arg) CURVNET_SET_VERBOSE(arg)
144
145#define CURVNET_RESTORE() \
146 VNET_ASSERT(saved_vnet == NULL || \
147 saved_vnet->vnet_magic_n == VNET_MAGIC_N); \
148 curvnet = saved_vnet; \
149 curthread->td_vnet_lpush = saved_vnet_lpush;
150#else /* !VNET_DEBUG */
151#define VNET_ASSERT(condition)
152
153#define CURVNET_SET(arg) \
154 struct vnet *saved_vnet = curvnet; \
155 curvnet = arg;
156
157#define CURVNET_SET_VERBOSE(arg) CURVNET_SET(arg)
158#define CURVNET_SET_QUIET(arg) CURVNET_SET(arg)
159
160#define CURVNET_RESTORE() \
161 curvnet = saved_vnet;
162#endif /* VNET_DEBUG */
163
164extern struct vnet *vnet0;
165#define IS_DEFAULT_VNET(arg) ((arg) == vnet0)
166
167#define CRED_TO_VNET(cr) (cr)->cr_prison->pr_vnet
168#define TD_TO_VNET(td) CRED_TO_VNET((td)->td_ucred)
169#define P_TO_VNET(p) CRED_TO_VNET((p)->p_ucred)
170
171/*
172 * Global linked list of all virtual network stacks, along with read locks to
173 * access it. If a caller may sleep while accessing the list, it must use
174 * the sleepable lock macros.
175 */
176LIST_HEAD(vnet_list_head, vnet);
177extern struct vnet_list_head vnet_head;
178extern struct rwlock vnet_rwlock;
179extern struct sx vnet_sxlock;
180
181#define VNET_LIST_RLOCK() sx_slock(&vnet_sxlock)
182#define VNET_LIST_RLOCK_NOSLEEP() rw_rlock(&vnet_rwlock)
183#define VNET_LIST_RUNLOCK() sx_sunlock(&vnet_sxlock)
184#define VNET_LIST_RUNLOCK_NOSLEEP() rw_runlock(&vnet_rwlock)
185
186/*
187 * Iteration macros to walk the global list of virtual network stacks.
188 */
189#define VNET_ITERATOR_DECL(arg) struct vnet *arg
190#define VNET_FOREACH(arg) LIST_FOREACH((arg), &vnet_head, vnet_le)
191
192/*
193 * Virtual network stack memory allocator, which allows global variables to
194 * be automatically instantiated for each network stack instance.
195 */
196#define VNET_NAME(n) vnet_entry_##n
197#define VNET_DECLARE(t, n) extern t VNET_NAME(n)
196#define VNET_DEFINE(t, n) \
197 __GLOBL("__start_" VNET_SETNAME); \
198 __GLOBL("__stop_" VNET_SETNAME); \
199 t VNET_NAME(n) __section(VNET_SETNAME) __used
200#define STATIC_VNET_DEFINE(t, n) \
201 VNET_DEFINE(static t, n)
202#define _VNET_PTR(b, n) \
203 (__typeof(VNET_NAME(n))*)((b) + (uintptr_t)&VNET_NAME(n))
198#define VNET_DEFINE(t, n) t VNET_NAME(n) __section(VNET_SETNAME) __used
199#define _VNET_PTR(b, n) (__typeof(VNET_NAME(n))*) \
200 ((b) + (uintptr_t)&VNET_NAME(n))
204
205#define _VNET(b, n) (*_VNET_PTR(b, n))
206
207/*
208 * Virtualized global variable accessor macros.
209 */
210#define VNET_VNET_PTR(vnet, n) _VNET_PTR((vnet)->vnet_data_base, n)
211#define VNET_VNET(vnet, n) (*VNET_VNET_PTR((vnet), n))
212
213#define VNET_PTR(n) VNET_VNET_PTR(curvnet, n)
214#define VNET(n) VNET_VNET(curvnet, n)
215
216/*
217 * Virtual network stack allocator interfaces from the kernel linker.
218 */
219void *vnet_data_alloc(int size);
220void vnet_data_copy(void *start, int size);
221void vnet_data_free(void *start_arg, int size);
222
223/*
224 * Sysctl variants for vnet-virtualized global variables. Include
225 * <sys/sysctl.h> to expose these definitions.
226 *
227 * Note: SYSCTL_PROC() handler functions will need to resolve pointer
228 * arguments themselves, if required.
229 */
230#ifdef SYSCTL_OID
231int vnet_sysctl_handle_int(SYSCTL_HANDLER_ARGS);
232int vnet_sysctl_handle_opaque(SYSCTL_HANDLER_ARGS);
233int vnet_sysctl_handle_string(SYSCTL_HANDLER_ARGS);
234int vnet_sysctl_handle_uint(SYSCTL_HANDLER_ARGS);
235
236#define SYSCTL_VNET_INT(parent, nbr, name, access, ptr, val, descr) \
237 SYSCTL_OID(parent, nbr, name, \
238 CTLTYPE_INT|CTLFLAG_MPSAFE|CTLFLAG_VNET|(access), \
239 ptr, val, vnet_sysctl_handle_int, "I", descr)
240#define SYSCTL_VNET_PROC(parent, nbr, name, access, ptr, arg, handler, \
241 fmt, descr) \
242 SYSCTL_OID(parent, nbr, name, CTLFLAG_VNET|(access), ptr, arg, \
243 handler, fmt, descr)
244#define SYSCTL_VNET_OPAQUE(parent, nbr, name, access, ptr, len, fmt, \
245 descr) \
246 SYSCTL_OID(parent, nbr, name, \
247 CTLTYPE_OPAQUE|CTLFLAG_VNET|(access), ptr, len, \
248 vnet_sysctl_handle_opaque, fmt, descr)
249#define SYSCTL_VNET_STRING(parent, nbr, name, access, arg, len, descr) \
250 SYSCTL_OID(parent, nbr, name, \
251 CTLTYPE_STRING|CTLFLAG_VNET|(access), \
252 arg, len, vnet_sysctl_handle_string, "A", descr)
253#define SYSCTL_VNET_STRUCT(parent, nbr, name, access, ptr, type, descr) \
254 SYSCTL_OID(parent, nbr, name, \
255 CTLTYPE_OPAQUE|CTLFLAG_VNET|(access), ptr, \
256 sizeof(struct type), vnet_sysctl_handle_opaque, "S," #type, \
257 descr)
258#define SYSCTL_VNET_UINT(parent, nbr, name, access, ptr, val, descr) \
259 SYSCTL_OID(parent, nbr, name, \
260 CTLTYPE_UINT|CTLFLAG_MPSAFE|CTLFLAG_VNET|(access), \
261 ptr, val, vnet_sysctl_handle_uint, "IU", descr)
262#define VNET_SYSCTL_ARG(req, arg1) do { \
263 if (arg1 != NULL) \
264 arg1 = (void *)(TD_TO_VNET((req)->td)->vnet_data_base + \
265 (uintptr_t)(arg1)); \
266} while (0)
267#endif /* SYSCTL_OID */
268
269/*
270 * Virtual sysinit mechanism, allowing network stack components to declare
271 * startup and shutdown methods to be run when virtual network stack
272 * instances are created and destroyed.
273 */
274#include <sys/kernel.h>
275
276/*
277 * SYSINIT/SYSUNINIT variants that provide per-vnet constructors and
278 * destructors.
279 */
280struct vnet_sysinit {
281 enum sysinit_sub_id subsystem;
282 enum sysinit_elem_order order;
283 sysinit_cfunc_t func;
284 const void *arg;
285 TAILQ_ENTRY(vnet_sysinit) link;
286};
287
288#define VNET_SYSINIT(ident, subsystem, order, func, arg) \
289 static struct vnet_sysinit ident ## _vnet_init = { \
290 subsystem, \
291 order, \
292 (sysinit_cfunc_t)(sysinit_nfunc_t)func, \
293 (arg) \
294 }; \
295 SYSINIT(vnet_init_ ## ident, subsystem, order, \
296 vnet_register_sysinit, &ident ## _vnet_init); \
297 SYSUNINIT(vnet_init_ ## ident, subsystem, order, \
298 vnet_deregister_sysinit, &ident ## _vnet_init)
299
300#define VNET_SYSUNINIT(ident, subsystem, order, func, arg) \
301 static struct vnet_sysinit ident ## _vnet_uninit = { \
302 subsystem, \
303 order, \
304 (sysinit_cfunc_t)(sysinit_nfunc_t)func, \
305 (arg) \
306 }; \
307 SYSINIT(vnet_uninit_ ## ident, subsystem, order, \
308 vnet_register_sysuninit, &ident ## _vnet_uninit); \
309 SYSUNINIT(vnet_uninit_ ## ident, subsystem, order, \
310 vnet_deregister_sysuninit, &ident ## _vnet_uninit)
311
312/*
313 * Run per-vnet sysinits or sysuninits during vnet creation/destruction.
314 */
315void vnet_sysinit(void);
316void vnet_sysuninit(void);
317
318/*
319 * Interfaces for managing per-vnet constructors and destructors.
320 */
321void vnet_register_sysinit(void *arg);
322void vnet_register_sysuninit(void *arg);
323void vnet_deregister_sysinit(void *arg);
324void vnet_deregister_sysuninit(void *arg);
325
326/*
327 * EVENTHANDLER(9) extensions.
328 */
329#include <sys/eventhandler.h>
330
331void vnet_global_eventhandler_iterator_func(void *, ...);
332#define VNET_GLOBAL_EVENTHANDLER_REGISTER_TAG(tag, name, func, arg, priority) \
333do { \
334 if (IS_DEFAULT_VNET(curvnet)) { \
335 (tag) = vimage_eventhandler_register(NULL, #name, func, \
336 arg, priority, \
337 vnet_global_eventhandler_iterator_func); \
338 } \
339} while(0)
340#define VNET_GLOBAL_EVENTHANDLER_REGISTER(name, func, arg, priority) \
341do { \
342 if (IS_DEFAULT_VNET(curvnet)) { \
343 vimage_eventhandler_register(NULL, #name, func, \
344 arg, priority, \
345 vnet_global_eventhandler_iterator_func); \
346 } \
347} while(0)
348
349#else /* !VIMAGE */
350
351/*
352 * Various virtual network stack macros compile to no-ops without VIMAGE.
353 */
354#define curvnet NULL
355
356#define VNET_ASSERT(condition)
357#define CURVNET_SET(arg)
358#define CURVNET_SET_QUIET(arg)
359#define CURVNET_RESTORE()
360
361#define VNET_LIST_RLOCK()
362#define VNET_LIST_RLOCK_NOSLEEP()
363#define VNET_LIST_RUNLOCK()
364#define VNET_LIST_RUNLOCK_NOSLEEP()
365#define VNET_ITERATOR_DECL(arg)
366#define VNET_FOREACH(arg)
367
368#define IS_DEFAULT_VNET(arg) 1
369#define CRED_TO_VNET(cr) NULL
370#define TD_TO_VNET(td) NULL
371#define P_TO_VNET(p) NULL
372
373/*
374 * Versions of the VNET macros that compile to normal global variables and
375 * standard sysctl definitions.
376 */
201
202#define _VNET(b, n) (*_VNET_PTR(b, n))
203
204/*
205 * Virtualized global variable accessor macros.
206 */
207#define VNET_VNET_PTR(vnet, n) _VNET_PTR((vnet)->vnet_data_base, n)
208#define VNET_VNET(vnet, n) (*VNET_VNET_PTR((vnet), n))
209
210#define VNET_PTR(n) VNET_VNET_PTR(curvnet, n)
211#define VNET(n) VNET_VNET(curvnet, n)
212
213/*
214 * Virtual network stack allocator interfaces from the kernel linker.
215 */
216void *vnet_data_alloc(int size);
217void vnet_data_copy(void *start, int size);
218void vnet_data_free(void *start_arg, int size);
219
220/*
221 * Sysctl variants for vnet-virtualized global variables. Include
222 * <sys/sysctl.h> to expose these definitions.
223 *
224 * Note: SYSCTL_PROC() handler functions will need to resolve pointer
225 * arguments themselves, if required.
226 */
227#ifdef SYSCTL_OID
228int vnet_sysctl_handle_int(SYSCTL_HANDLER_ARGS);
229int vnet_sysctl_handle_opaque(SYSCTL_HANDLER_ARGS);
230int vnet_sysctl_handle_string(SYSCTL_HANDLER_ARGS);
231int vnet_sysctl_handle_uint(SYSCTL_HANDLER_ARGS);
232
233#define SYSCTL_VNET_INT(parent, nbr, name, access, ptr, val, descr) \
234 SYSCTL_OID(parent, nbr, name, \
235 CTLTYPE_INT|CTLFLAG_MPSAFE|CTLFLAG_VNET|(access), \
236 ptr, val, vnet_sysctl_handle_int, "I", descr)
237#define SYSCTL_VNET_PROC(parent, nbr, name, access, ptr, arg, handler, \
238 fmt, descr) \
239 SYSCTL_OID(parent, nbr, name, CTLFLAG_VNET|(access), ptr, arg, \
240 handler, fmt, descr)
241#define SYSCTL_VNET_OPAQUE(parent, nbr, name, access, ptr, len, fmt, \
242 descr) \
243 SYSCTL_OID(parent, nbr, name, \
244 CTLTYPE_OPAQUE|CTLFLAG_VNET|(access), ptr, len, \
245 vnet_sysctl_handle_opaque, fmt, descr)
246#define SYSCTL_VNET_STRING(parent, nbr, name, access, arg, len, descr) \
247 SYSCTL_OID(parent, nbr, name, \
248 CTLTYPE_STRING|CTLFLAG_VNET|(access), \
249 arg, len, vnet_sysctl_handle_string, "A", descr)
250#define SYSCTL_VNET_STRUCT(parent, nbr, name, access, ptr, type, descr) \
251 SYSCTL_OID(parent, nbr, name, \
252 CTLTYPE_OPAQUE|CTLFLAG_VNET|(access), ptr, \
253 sizeof(struct type), vnet_sysctl_handle_opaque, "S," #type, \
254 descr)
255#define SYSCTL_VNET_UINT(parent, nbr, name, access, ptr, val, descr) \
256 SYSCTL_OID(parent, nbr, name, \
257 CTLTYPE_UINT|CTLFLAG_MPSAFE|CTLFLAG_VNET|(access), \
258 ptr, val, vnet_sysctl_handle_uint, "IU", descr)
259#define VNET_SYSCTL_ARG(req, arg1) do { \
260 if (arg1 != NULL) \
261 arg1 = (void *)(TD_TO_VNET((req)->td)->vnet_data_base + \
262 (uintptr_t)(arg1)); \
263} while (0)
264#endif /* SYSCTL_OID */
265
266/*
267 * Virtual sysinit mechanism, allowing network stack components to declare
268 * startup and shutdown methods to be run when virtual network stack
269 * instances are created and destroyed.
270 */
271#include <sys/kernel.h>
272
273/*
274 * SYSINIT/SYSUNINIT variants that provide per-vnet constructors and
275 * destructors.
276 */
277struct vnet_sysinit {
278 enum sysinit_sub_id subsystem;
279 enum sysinit_elem_order order;
280 sysinit_cfunc_t func;
281 const void *arg;
282 TAILQ_ENTRY(vnet_sysinit) link;
283};
284
285#define VNET_SYSINIT(ident, subsystem, order, func, arg) \
286 static struct vnet_sysinit ident ## _vnet_init = { \
287 subsystem, \
288 order, \
289 (sysinit_cfunc_t)(sysinit_nfunc_t)func, \
290 (arg) \
291 }; \
292 SYSINIT(vnet_init_ ## ident, subsystem, order, \
293 vnet_register_sysinit, &ident ## _vnet_init); \
294 SYSUNINIT(vnet_init_ ## ident, subsystem, order, \
295 vnet_deregister_sysinit, &ident ## _vnet_init)
296
297#define VNET_SYSUNINIT(ident, subsystem, order, func, arg) \
298 static struct vnet_sysinit ident ## _vnet_uninit = { \
299 subsystem, \
300 order, \
301 (sysinit_cfunc_t)(sysinit_nfunc_t)func, \
302 (arg) \
303 }; \
304 SYSINIT(vnet_uninit_ ## ident, subsystem, order, \
305 vnet_register_sysuninit, &ident ## _vnet_uninit); \
306 SYSUNINIT(vnet_uninit_ ## ident, subsystem, order, \
307 vnet_deregister_sysuninit, &ident ## _vnet_uninit)
308
309/*
310 * Run per-vnet sysinits or sysuninits during vnet creation/destruction.
311 */
312void vnet_sysinit(void);
313void vnet_sysuninit(void);
314
315/*
316 * Interfaces for managing per-vnet constructors and destructors.
317 */
318void vnet_register_sysinit(void *arg);
319void vnet_register_sysuninit(void *arg);
320void vnet_deregister_sysinit(void *arg);
321void vnet_deregister_sysuninit(void *arg);
322
323/*
324 * EVENTHANDLER(9) extensions.
325 */
326#include <sys/eventhandler.h>
327
328void vnet_global_eventhandler_iterator_func(void *, ...);
329#define VNET_GLOBAL_EVENTHANDLER_REGISTER_TAG(tag, name, func, arg, priority) \
330do { \
331 if (IS_DEFAULT_VNET(curvnet)) { \
332 (tag) = vimage_eventhandler_register(NULL, #name, func, \
333 arg, priority, \
334 vnet_global_eventhandler_iterator_func); \
335 } \
336} while(0)
337#define VNET_GLOBAL_EVENTHANDLER_REGISTER(name, func, arg, priority) \
338do { \
339 if (IS_DEFAULT_VNET(curvnet)) { \
340 vimage_eventhandler_register(NULL, #name, func, \
341 arg, priority, \
342 vnet_global_eventhandler_iterator_func); \
343 } \
344} while(0)
345
346#else /* !VIMAGE */
347
348/*
349 * Various virtual network stack macros compile to no-ops without VIMAGE.
350 */
351#define curvnet NULL
352
353#define VNET_ASSERT(condition)
354#define CURVNET_SET(arg)
355#define CURVNET_SET_QUIET(arg)
356#define CURVNET_RESTORE()
357
358#define VNET_LIST_RLOCK()
359#define VNET_LIST_RLOCK_NOSLEEP()
360#define VNET_LIST_RUNLOCK()
361#define VNET_LIST_RUNLOCK_NOSLEEP()
362#define VNET_ITERATOR_DECL(arg)
363#define VNET_FOREACH(arg)
364
365#define IS_DEFAULT_VNET(arg) 1
366#define CRED_TO_VNET(cr) NULL
367#define TD_TO_VNET(td) NULL
368#define P_TO_VNET(p) NULL
369
370/*
371 * Versions of the VNET macros that compile to normal global variables and
372 * standard sysctl definitions.
373 */
377#define VNET_NAME(n) n
378#define VNET_DECLARE(t, n) extern t n
379#define VNET_DEFINE(t, n) t n
380#define STATIC_VNET_DEFINE(t, n) static t n
381#define _VNET_PTR(b, n) &VNET_NAME(n)
374#define VNET_NAME(n) n
375#define VNET_DECLARE(t, n) extern t n
376#define VNET_DEFINE(t, n) t n
377#define _VNET_PTR(b, n) &VNET_NAME(n)
382
383/*
384 * Virtualized global variable accessor macros.
385 */
386#define VNET_VNET_PTR(vnet, n) (&(n))
387#define VNET_VNET(vnet, n) (n)
388
389#define VNET_PTR(n) (&(n))
390#define VNET(n) (n)
391
392/*
393 * When VIMAGE isn't compiled into the kernel, virtaulized SYSCTLs simply
394 * become normal SYSCTLs.
395 */
396#ifdef SYSCTL_OID
397#define SYSCTL_VNET_INT(parent, nbr, name, access, ptr, val, descr) \
398 SYSCTL_INT(parent, nbr, name, access, ptr, val, descr)
399#define SYSCTL_VNET_PROC(parent, nbr, name, access, ptr, arg, handler, \
400 fmt, descr) \
401 SYSCTL_PROC(parent, nbr, name, access, ptr, arg, handler, fmt, \
402 descr)
403#define SYSCTL_VNET_OPAQUE(parent, nbr, name, access, ptr, len, fmt, \
404 descr) \
405 SYSCTL_OPAQUE(parent, nbr, name, access, ptr, len, fmt, descr)
406#define SYSCTL_VNET_STRING(parent, nbr, name, access, arg, len, descr) \
407 SYSCTL_STRING(parent, nbr, name, access, arg, len, descr)
408#define SYSCTL_VNET_STRUCT(parent, nbr, name, access, ptr, type, descr) \
409 SYSCTL_STRUCT(parent, nbr, name, access, ptr, type, descr)
410#define SYSCTL_VNET_UINT(parent, nbr, name, access, ptr, val, descr) \
411 SYSCTL_UINT(parent, nbr, name, access, ptr, val, descr)
412#define VNET_SYSCTL_ARG(req, arg1)
413#endif /* SYSCTL_OID */
414
415/*
416 * When VIMAGE isn't compiled into the kernel, VNET_SYSINIT/VNET_SYSUNINIT
417 * map into normal sysinits, which have the same ordering properties.
418 */
419#define VNET_SYSINIT(ident, subsystem, order, func, arg) \
420 SYSINIT(ident, subsystem, order, func, arg)
421#define VNET_SYSUNINIT(ident, subsystem, order, func, arg) \
422 SYSUNINIT(ident, subsystem, order, func, arg)
423
424/*
425 * Without VIMAGE revert to the default implementation.
426 */
427#define VNET_GLOBAL_EVENTHANDLER_REGISTER_TAG(tag, name, func, arg, priority) \
428 (tag) = eventhandler_register(NULL, #name, func, arg, priority)
429#define VNET_GLOBAL_EVENTHANDLER_REGISTER(name, func, arg, priority) \
430 eventhandler_register(NULL, #name, func, arg, priority)
431#endif /* VIMAGE */
432#endif /* _KERNEL */
433
434#endif /* !_NET_VNET_H_ */
378
379/*
380 * Virtualized global variable accessor macros.
381 */
382#define VNET_VNET_PTR(vnet, n) (&(n))
383#define VNET_VNET(vnet, n) (n)
384
385#define VNET_PTR(n) (&(n))
386#define VNET(n) (n)
387
388/*
389 * When VIMAGE isn't compiled into the kernel, virtaulized SYSCTLs simply
390 * become normal SYSCTLs.
391 */
392#ifdef SYSCTL_OID
393#define SYSCTL_VNET_INT(parent, nbr, name, access, ptr, val, descr) \
394 SYSCTL_INT(parent, nbr, name, access, ptr, val, descr)
395#define SYSCTL_VNET_PROC(parent, nbr, name, access, ptr, arg, handler, \
396 fmt, descr) \
397 SYSCTL_PROC(parent, nbr, name, access, ptr, arg, handler, fmt, \
398 descr)
399#define SYSCTL_VNET_OPAQUE(parent, nbr, name, access, ptr, len, fmt, \
400 descr) \
401 SYSCTL_OPAQUE(parent, nbr, name, access, ptr, len, fmt, descr)
402#define SYSCTL_VNET_STRING(parent, nbr, name, access, arg, len, descr) \
403 SYSCTL_STRING(parent, nbr, name, access, arg, len, descr)
404#define SYSCTL_VNET_STRUCT(parent, nbr, name, access, ptr, type, descr) \
405 SYSCTL_STRUCT(parent, nbr, name, access, ptr, type, descr)
406#define SYSCTL_VNET_UINT(parent, nbr, name, access, ptr, val, descr) \
407 SYSCTL_UINT(parent, nbr, name, access, ptr, val, descr)
408#define VNET_SYSCTL_ARG(req, arg1)
409#endif /* SYSCTL_OID */
410
411/*
412 * When VIMAGE isn't compiled into the kernel, VNET_SYSINIT/VNET_SYSUNINIT
413 * map into normal sysinits, which have the same ordering properties.
414 */
415#define VNET_SYSINIT(ident, subsystem, order, func, arg) \
416 SYSINIT(ident, subsystem, order, func, arg)
417#define VNET_SYSUNINIT(ident, subsystem, order, func, arg) \
418 SYSUNINIT(ident, subsystem, order, func, arg)
419
420/*
421 * Without VIMAGE revert to the default implementation.
422 */
423#define VNET_GLOBAL_EVENTHANDLER_REGISTER_TAG(tag, name, func, arg, priority) \
424 (tag) = eventhandler_register(NULL, #name, func, arg, priority)
425#define VNET_GLOBAL_EVENTHANDLER_REGISTER(name, func, arg, priority) \
426 eventhandler_register(NULL, #name, func, arg, priority)
427#endif /* VIMAGE */
428#endif /* _KERNEL */
429
430#endif /* !_NET_VNET_H_ */