vnet.h revision 195705
1/*-
2 * Copyright (c) 2009 Jeffrey Roberson <jeff@freebsd.org>
3 * Copyright (c) 2009 Robert N. M. Watson
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
27 * $FreeBSD: head/sys/net/vnet.h 195705 2009-07-15 00:56:15Z rwatson $
28 */
29
30/*
31 * This is the virtual network stack memory allocator, which provides support
32 * for virtualized global variables via a special linker set, set_vnet.  When
33 * "options VIMAGE" isn't defined, virtualized global variables are compiled
34 * as normal globals.
35 */
36
37#ifndef _NET_VNET_H_
38#define	_NET_VNET_H_
39
40#ifdef _KERNEL
41#ifdef VIMAGE
42
43#if defined(__arm__)
44__asm__(".section set_vnet, \"aw\", %progbits");
45#else
46__asm__(".section set_vnet, \"aw\", @progbits");
47#endif
48__asm__(".previous");
49
50#define	VNET_NAME(n)		vnet_entry_##n
51#define	VNET_DECLARE(t, n)	extern t VNET_NAME(n)
52#define	VNET_DEFINE(t, n)	t VNET_NAME(n) __section("set_vnet") __used
53#define	_VNET_PTR(b, n)		(__typeof(VNET_NAME(n))*)		\
54				    ((b) + (uintptr_t)&VNET_NAME(n))
55
56#define	_VNET_GET(b, n)		(*_VNET_PTR(b, n))
57#define	_VNET_SET(b, n, v)	(*_VNET_PTR(b, n) = v)
58
59/*
60 * Virtualized global variable accessor macros.
61 */
62#define	VNET_VNET_PTR(vnet, n)		_VNET_PTR((vnet)->vnet_data_base, n)
63#define	VNET_VNET_GET(vnet, n)		(*VNET_VNET_PTR((vnet), n))
64#define	VNET_VNET_SET(vnet, n, v)	((*VNET_VNET_PTR((vnet), n)) = v)
65
66#define	VNET_PTR(n)		VNET_VNET_PTR(curvnet, n)
67#define	VNET_GET(n)		VNET_VNET_GET(curvnet, n)
68#define	VNET_SET(n, v)		VNET_VNET_SET(curvnet, n, v)
69
70/*
71 * Sysctl variants for vnet-virtualized global variables.  Include
72 * <sys/sysctl.h> to expose these definitions.
73 *
74 * Note: SYSCTL_PROC() handler functions will need to resolve pointer
75 * arguments themselves, if required.
76 */
77#ifdef SYSCTL_OID
78int	vnet_sysctl_handle_int(SYSCTL_HANDLER_ARGS);
79int	vnet_sysctl_handle_opaque(SYSCTL_HANDLER_ARGS);
80int	vnet_sysctl_handle_string(SYSCTL_HANDLER_ARGS);
81int	vnet_sysctl_handle_uint(SYSCTL_HANDLER_ARGS);
82
83#define	SYSCTL_VNET_INT(parent, nbr, name, access, ptr, val, descr)	\
84	SYSCTL_OID(parent, nbr, name, CTLTYPE_INT|CTLFLAG_MPSAFE|(access), \
85	    ptr, val, vnet_sysctl_handle_int, "I", descr)
86#define	SYSCTL_VNET_PROC(parent, nbr, name, access, ptr, arg, handler,	\
87	    fmt, descr)							\
88	SYSCTL_OID(parent, nbr, name, access, ptr, arg, handler, fmt,	\
89	    descr)
90#define	SYSCTL_VNET_STRING(parent, nbr, name, access, arg, len, descr)	\
91	SYSCTL_OID(parent, nbr, name, CTLTYPE_STRING|(access), arg,	\
92	    len, vnet_sysctl_handle_string, "A", descr)
93#define	SYSCTL_VNET_STRUCT(parent, nbr, name, access, ptr, type, descr)	\
94	SYSCTL_OID(parent, nbr, name, CTLTYPE_OPAQUE|(access), ptr,	\
95	    sizeof(struct type), vnet_sysctl_handle_opaque, "S," #type,	\
96	    descr)
97#define	SYSCTL_VNET_UINT(parent, nbr, name, access, ptr, val, descr)	\
98	SYSCTL_OID(parent, nbr, name, CTLTYPE_UINT|CTLFLAG_MPSAFE|(access), \
99	    ptr, val, vnet_sysctl_handle_uint, "IU", descr)
100#endif /* SYSCTL_OID */
101
102/*
103 * Interfaces from the kernel linker.
104 */
105void	*vnet_data_alloc(int size);
106void	 vnet_data_copy(void *start, int size);
107void	 vnet_data_free(void *start_arg, int size);
108
109/*
110 * Interfaces for vnet setup/teardown.
111 */
112struct vnet;
113void	 vnet_data_init(struct vnet *vnet);
114void	 vnet_data_destroy(struct vnet *vnet);
115
116#else /* !VIMAGE */
117
118/*
119 * Versions of the VNET macros that compile to normal global variables and
120 * standard sysctl definitions.
121 */
122#define	VNET_NAME(n)		n
123#define	VNET_DECLARE(t, n)	extern t n
124#define	VNET_DEFINE(t, n)	t n
125#define	_VNET_PTR(b, n)		&VNET_NAME(n)
126
127#ifdef SYSCTL_OID
128#define	SYSCTL_VNET_INT(parent, nbr, name, access, ptr, val, descr)	\
129	SYSCTL_INT(parent, nbr, name, access, ptr, val, descr)
130#define	SYSCTL_VNET_PROC(parent, nbr, name, access, ptr, arg, handler,	\
131	    fmt, descr)							\
132	SYSCTL_PROC(parent, nbr, name, access, ptr, arg, handler, fmt,	\
133	    descr)
134#define	SYSCTL_VNET_STRING(parent, nbr, name, access, arg, len, descr)	\
135	SYSCTL_STRING(parent, nbr, name, access, arg, len, descr)
136#define	SYSCTL_VNET_STRUCT(parent, nbr, name, access, ptr, type, descr)	\
137	SYSCTL_STRUCT(parent, nbr, name, access, ptr, type, descr)
138#define	SYSCTL_VNET_UINT(parent, nbr, name, access, ptr, val, descr)	\
139	SYSCTL_UINT(parent, nbr, name, access, ptr, val, descr)
140#endif /* SYSCTL_OID */
141
142/*
143 * Virtualized global variable accessor macros.
144 */
145#define	VNET_VNET_PTR(vnet, n)		(&(n))
146#define	VNET_VNET_GET(vnet, n)		(n)
147#define	VNET_VNET_SET(vnet, n, v)	((n) = (v))
148
149#define	VNET_PTR(n)		(&(n))
150#define	VNET_GET(n)		(n)
151#define	VNET_SET(n, v)		((n) = (v))
152
153#endif /* VIMAGE */
154
155#endif /* _KERNEL */
156
157#endif /* !_NET_VNET_H_ */
158