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