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