vnet.h revision 195727
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 195727 2009-07-16 21:13:04Z 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(b, n)		(*_VNET_PTR(b, n))
57
58/*
59 * Virtualized global variable accessor macros.
60 */
61#define	VNET_VNET_PTR(vnet, n)		_VNET_PTR((vnet)->vnet_data_base, n)
62#define	VNET_VNET(vnet, n)		(*VNET_VNET_PTR((vnet), n))
63
64#define	VNET_PTR(n)		VNET_VNET_PTR(curvnet, n)
65#define	VNET(n)			VNET_VNET(curvnet, n)
66
67/*
68 * Sysctl variants for vnet-virtualized global variables.  Include
69 * <sys/sysctl.h> to expose these definitions.
70 *
71 * Note: SYSCTL_PROC() handler functions will need to resolve pointer
72 * arguments themselves, if required.
73 */
74#ifdef SYSCTL_OID
75int	vnet_sysctl_handle_int(SYSCTL_HANDLER_ARGS);
76int	vnet_sysctl_handle_opaque(SYSCTL_HANDLER_ARGS);
77int	vnet_sysctl_handle_string(SYSCTL_HANDLER_ARGS);
78int	vnet_sysctl_handle_uint(SYSCTL_HANDLER_ARGS);
79
80#define	SYSCTL_VNET_INT(parent, nbr, name, access, ptr, val, descr)	\
81	SYSCTL_OID(parent, nbr, name, CTLTYPE_INT|CTLFLAG_MPSAFE|(access), \
82	    ptr, val, vnet_sysctl_handle_int, "I", descr)
83#define	SYSCTL_VNET_PROC(parent, nbr, name, access, ptr, arg, handler,	\
84	    fmt, descr)							\
85	SYSCTL_OID(parent, nbr, name, access, ptr, arg, handler, fmt,	\
86	    descr)
87#define	SYSCTL_VNET_STRING(parent, nbr, name, access, arg, len, descr)	\
88	SYSCTL_OID(parent, nbr, name, CTLTYPE_STRING|(access), arg,	\
89	    len, vnet_sysctl_handle_string, "A", descr)
90#define	SYSCTL_VNET_STRUCT(parent, nbr, name, access, ptr, type, descr)	\
91	SYSCTL_OID(parent, nbr, name, CTLTYPE_OPAQUE|(access), ptr,	\
92	    sizeof(struct type), vnet_sysctl_handle_opaque, "S," #type,	\
93	    descr)
94#define	SYSCTL_VNET_UINT(parent, nbr, name, access, ptr, val, descr)	\
95	SYSCTL_OID(parent, nbr, name, CTLTYPE_UINT|CTLFLAG_MPSAFE|(access), \
96	    ptr, val, vnet_sysctl_handle_uint, "IU", descr)
97#endif /* SYSCTL_OID */
98
99/*
100 * Interfaces from the kernel linker.
101 */
102void	*vnet_data_alloc(int size);
103void	 vnet_data_copy(void *start, int size);
104void	 vnet_data_free(void *start_arg, int size);
105
106/*
107 * Interfaces for vnet setup/teardown.
108 */
109struct vnet;
110void	 vnet_data_init(struct vnet *vnet);
111void	 vnet_data_destroy(struct vnet *vnet);
112
113#else /* !VIMAGE */
114
115/*
116 * Versions of the VNET macros that compile to normal global variables and
117 * standard sysctl definitions.
118 */
119#define	VNET_NAME(n)		n
120#define	VNET_DECLARE(t, n)	extern t n
121#define	VNET_DEFINE(t, n)	t n
122#define	_VNET_PTR(b, n)		&VNET_NAME(n)
123
124#ifdef SYSCTL_OID
125#define	SYSCTL_VNET_INT(parent, nbr, name, access, ptr, val, descr)	\
126	SYSCTL_INT(parent, nbr, name, access, ptr, val, descr)
127#define	SYSCTL_VNET_PROC(parent, nbr, name, access, ptr, arg, handler,	\
128	    fmt, descr)							\
129	SYSCTL_PROC(parent, nbr, name, access, ptr, arg, handler, fmt,	\
130	    descr)
131#define	SYSCTL_VNET_STRING(parent, nbr, name, access, arg, len, descr)	\
132	SYSCTL_STRING(parent, nbr, name, access, arg, len, descr)
133#define	SYSCTL_VNET_STRUCT(parent, nbr, name, access, ptr, type, descr)	\
134	SYSCTL_STRUCT(parent, nbr, name, access, ptr, type, descr)
135#define	SYSCTL_VNET_UINT(parent, nbr, name, access, ptr, val, descr)	\
136	SYSCTL_UINT(parent, nbr, name, access, ptr, val, descr)
137#endif /* SYSCTL_OID */
138
139/*
140 * Virtualized global variable accessor macros.
141 */
142#define	VNET_VNET_PTR(vnet, n)		(&(n))
143#define	VNET_VNET(vnet, n)		(n)
144
145#define	VNET_PTR(n)		(&(n))
146#define	VNET(n)			(n)
147
148#endif /* VIMAGE */
149
150#endif /* _KERNEL */
151
152#endif /* !_NET_VNET_H_ */
153