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