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