]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/net/vnet.h
sysctl_msec_to_ticks is used with both virtualized and
[FreeBSD/FreeBSD.git] / sys / net / vnet.h
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$
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 #if defined(_KERNEL) || defined(_WANT_VNET)
41
42 #define VNET_SETNAME            "set_vnet"
43 #define VNET_SYMPREFIX          "vnet_entry_"
44
45 #endif
46
47 #ifdef _KERNEL
48 #ifdef VIMAGE
49
50 #if defined(__arm__)
51 __asm__(".section " VNET_SETNAME ", \"aw\", %progbits");
52 #else
53 __asm__(".section " VNET_SETNAME ", \"aw\", @progbits");
54 #endif
55 __asm__(".previous");
56
57 #define VNET_NAME(n)            vnet_entry_##n
58 #define VNET_DECLARE(t, n)      extern t VNET_NAME(n)
59 #define VNET_DEFINE(t, n)       t VNET_NAME(n) __section(VNET_SETNAME) __used
60 #define _VNET_PTR(b, n)         (__typeof(VNET_NAME(n))*)               \
61                                     ((b) + (uintptr_t)&VNET_NAME(n))
62
63 #define _VNET(b, n)             (*_VNET_PTR(b, n))
64
65 /*
66  * Virtualized global variable accessor macros.
67  */
68 #define VNET_VNET_PTR(vnet, n)          _VNET_PTR((vnet)->vnet_data_base, n)
69 #define VNET_VNET(vnet, n)              (*VNET_VNET_PTR((vnet), n))
70
71 #define VNET_PTR(n)             VNET_VNET_PTR(curvnet, n)
72 #define VNET(n)                 VNET_VNET(curvnet, n)
73
74 /*
75  * Sysctl variants for vnet-virtualized global variables.  Include
76  * <sys/sysctl.h> to expose these definitions.
77  *
78  * Note: SYSCTL_PROC() handler functions will need to resolve pointer
79  * arguments themselves, if required.
80  */
81 #ifdef SYSCTL_OID
82 int     vnet_sysctl_handle_int(SYSCTL_HANDLER_ARGS);
83 int     vnet_sysctl_handle_opaque(SYSCTL_HANDLER_ARGS);
84 int     vnet_sysctl_handle_string(SYSCTL_HANDLER_ARGS);
85 int     vnet_sysctl_handle_uint(SYSCTL_HANDLER_ARGS);
86
87 #define SYSCTL_VNET_INT(parent, nbr, name, access, ptr, val, descr)     \
88         SYSCTL_OID(parent, nbr, name, CTLTYPE_INT|CTLFLAG_MPSAFE|(access), \
89             ptr, val, vnet_sysctl_handle_int, "I", descr)
90 #define SYSCTL_VNET_PROC(parent, nbr, name, access, ptr, arg, handler,  \
91             fmt, descr)                                                 \
92         SYSCTL_OID(parent, nbr, name, access, ptr, arg, handler, fmt,   \
93             descr)
94 #define SYSCTL_VNET_STRING(parent, nbr, name, access, arg, len, descr)  \
95         SYSCTL_OID(parent, nbr, name, CTLTYPE_STRING|(access), arg,     \
96             len, vnet_sysctl_handle_string, "A", descr)
97 #define SYSCTL_VNET_STRUCT(parent, nbr, name, access, ptr, type, descr) \
98         SYSCTL_OID(parent, nbr, name, CTLTYPE_OPAQUE|(access), ptr,     \
99             sizeof(struct type), vnet_sysctl_handle_opaque, "S," #type, \
100             descr)
101 #define SYSCTL_VNET_UINT(parent, nbr, name, access, ptr, val, descr)    \
102         SYSCTL_OID(parent, nbr, name, CTLTYPE_UINT|CTLFLAG_MPSAFE|(access), \
103             ptr, val, vnet_sysctl_handle_uint, "IU", descr)
104 #define VNET_SYSCTL_ARG(req, arg1) do {                                 \
105         if (arg1 != NULL)                                               \
106                 arg1 = (void *)(TD_TO_VNET((req)->td)->vnet_data_base + \
107                     (uintptr_t)(arg1));                                 \
108 } while (0)
109 #endif /* SYSCTL_OID */
110
111 /*
112  * Interfaces from the kernel linker.
113  */
114 void    *vnet_data_alloc(int size);
115 void     vnet_data_copy(void *start, int size);
116 void     vnet_data_free(void *start_arg, int size);
117
118 /*
119  * Interfaces for vnet setup/teardown.
120  */
121 struct vnet;
122 void     vnet_data_init(struct vnet *vnet);
123 void     vnet_data_destroy(struct vnet *vnet);
124
125 #else /* !VIMAGE */
126
127 /*
128  * Versions of the VNET macros that compile to normal global variables and
129  * standard sysctl definitions.
130  */
131 #define VNET_NAME(n)            n
132 #define VNET_DECLARE(t, n)      extern t n
133 #define VNET_DEFINE(t, n)       t n
134 #define _VNET_PTR(b, n)         &VNET_NAME(n)
135
136 #ifdef SYSCTL_OID
137 #define SYSCTL_VNET_INT(parent, nbr, name, access, ptr, val, descr)     \
138         SYSCTL_INT(parent, nbr, name, access, ptr, val, descr)
139 #define SYSCTL_VNET_PROC(parent, nbr, name, access, ptr, arg, handler,  \
140             fmt, descr)                                                 \
141         SYSCTL_PROC(parent, nbr, name, access, ptr, arg, handler, fmt,  \
142             descr)
143 #define SYSCTL_VNET_STRING(parent, nbr, name, access, arg, len, descr)  \
144         SYSCTL_STRING(parent, nbr, name, access, arg, len, descr)
145 #define SYSCTL_VNET_STRUCT(parent, nbr, name, access, ptr, type, descr) \
146         SYSCTL_STRUCT(parent, nbr, name, access, ptr, type, descr)
147 #define SYSCTL_VNET_UINT(parent, nbr, name, access, ptr, val, descr)    \
148         SYSCTL_UINT(parent, nbr, name, access, ptr, val, descr)
149 #define VNET_SYSCTL_ARG(req, arg1)
150 #endif /* SYSCTL_OID */
151
152 /*
153  * Virtualized global variable accessor macros.
154  */
155 #define VNET_VNET_PTR(vnet, n)          (&(n))
156 #define VNET_VNET(vnet, n)              (n)
157
158 #define VNET_PTR(n)             (&(n))
159 #define VNET(n)                 (n)
160
161 #endif /* VIMAGE */
162
163 #endif /* _KERNEL */
164
165 #endif /* !_NET_VNET_H_ */