vnet.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. /*-
  2. * SPDX-License-Identifier: BSD-2-Clause
  3. *
  4. * Copyright (c) 2006-2009 University of Zagreb
  5. * Copyright (c) 2006-2009 FreeBSD Foundation
  6. * All rights reserved.
  7. *
  8. * This software was developed by the University of Zagreb and the
  9. * FreeBSD Foundation under sponsorship by the Stichting NLnet and the
  10. * FreeBSD Foundation.
  11. *
  12. * Copyright (c) 2009 Jeffrey Roberson <jeff@freebsd.org>
  13. * Copyright (c) 2009 Robert N. M. Watson
  14. * All rights reserved.
  15. *
  16. * Redistribution and use in source and binary forms, with or without
  17. * modification, are permitted provided that the following conditions
  18. * are met:
  19. * 1. Redistributions of source code must retain the above copyright
  20. * notice, this list of conditions and the following disclaimer.
  21. * 2. Redistributions in binary form must reproduce the above copyright
  22. * notice, this list of conditions and the following disclaimer in the
  23. * documentation and/or other materials provided with the distribution.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  26. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  28. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  29. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  30. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  31. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  32. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  33. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  34. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  35. * SUCH DAMAGE.
  36. */
  37. /*-
  38. * This header file defines several sets of interfaces supporting virtualized
  39. * network stacks:
  40. *
  41. * - Definition of 'struct vnet' and functions and macros to allocate/free/
  42. * manipulate it.
  43. *
  44. * - A virtual network stack memory allocator, which provides support for
  45. * virtualized global variables via a special linker set, set_vnet.
  46. *
  47. * - Virtualized sysinits/sysuninits, which allow constructors and
  48. * destructors to be run for each network stack subsystem as virtual
  49. * instances are created and destroyed.
  50. *
  51. * If VIMAGE isn't compiled into the kernel, virtualized global variables
  52. * compile to normal global variables, and virtualized sysinits to regular
  53. * sysinits.
  54. */
  55. #ifndef _NET_VNET_H_
  56. #define _NET_VNET_H_
  57. /*
  58. * struct vnet describes a virtualized network stack, and is primarily a
  59. * pointer to storage for virtualized global variables. Expose to userspace
  60. * as required for libkvm.
  61. */
  62. #if defined(_KERNEL) || defined(_WANT_VNET)
  63. #include <machine/param.h> /* for CACHE_LINE_SIZE */
  64. #include <sys/queue.h>
  65. struct vnet {
  66. LIST_ENTRY(vnet) vnet_le; /* all vnets list */
  67. u_int vnet_magic_n;
  68. u_int vnet_ifcnt;
  69. u_int vnet_sockcnt;
  70. u_int vnet_state; /* SI_SUB_* */
  71. void *vnet_data_mem;
  72. uintptr_t vnet_data_base;
  73. bool vnet_shutdown; /* Shutdown in progress. */
  74. } __aligned(CACHE_LINE_SIZE);
  75. #define VNET_MAGIC_N 0x5e4a6f28
  76. /*
  77. * These two virtual network stack allocator definitions are also required
  78. * for libkvm so that it can evaluate virtualized global variables.
  79. */
  80. #define VNET_SETNAME "set_vnet"
  81. #define VNET_SYMPREFIX "vnet_entry_"
  82. #endif
  83. #ifdef _KERNEL
  84. #define VNET_PCPUSTAT_DECLARE(type, name) \
  85. VNET_DECLARE(counter_u64_t, name[sizeof(type) / sizeof(uint64_t)])
  86. #define VNET_PCPUSTAT_DEFINE(type, name) \
  87. VNET_DEFINE(counter_u64_t, name[sizeof(type) / sizeof(uint64_t)])
  88. #define VNET_PCPUSTAT_DEFINE_STATIC(type, name) \
  89. VNET_DEFINE_STATIC(counter_u64_t, name[sizeof(type) / sizeof(uint64_t)])
  90. #define VNET_PCPUSTAT_ALLOC(name, wait) \
  91. COUNTER_ARRAY_ALLOC(VNET(name), \
  92. sizeof(VNET(name)) / sizeof(counter_u64_t), (wait))
  93. #define VNET_PCPUSTAT_FREE(name) \
  94. COUNTER_ARRAY_FREE(VNET(name), sizeof(VNET(name)) / sizeof(counter_u64_t))
  95. #define VNET_PCPUSTAT_ADD(type, name, f, v) \
  96. counter_u64_add(VNET(name)[offsetof(type, f) / sizeof(uint64_t)], (v))
  97. #define VNET_PCPUSTAT_FETCH(type, name, f) \
  98. counter_u64_fetch(VNET(name)[offsetof(type, f) / sizeof(uint64_t)])
  99. #define VNET_PCPUSTAT_SYSINIT(name) \
  100. static void \
  101. vnet_##name##_init(const void *unused) \
  102. { \
  103. VNET_PCPUSTAT_ALLOC(name, M_WAITOK); \
  104. } \
  105. VNET_SYSINIT(vnet_ ## name ## _init, SI_SUB_INIT_IF, \
  106. SI_ORDER_FIRST, vnet_ ## name ## _init, NULL)
  107. #define VNET_PCPUSTAT_SYSUNINIT(name) \
  108. static void \
  109. vnet_##name##_uninit(const void *unused) \
  110. { \
  111. VNET_PCPUSTAT_FREE(name); \
  112. } \
  113. VNET_SYSUNINIT(vnet_ ## name ## _uninit, SI_SUB_INIT_IF, \
  114. SI_ORDER_FIRST, vnet_ ## name ## _uninit, NULL)
  115. #ifdef SYSCTL_OID
  116. #define SYSCTL_VNET_PCPUSTAT(parent, nbr, name, type, array, desc) \
  117. static int \
  118. array##_sysctl(SYSCTL_HANDLER_ARGS) \
  119. { \
  120. type s; \
  121. CTASSERT((sizeof(type) / sizeof(uint64_t)) == \
  122. (sizeof(VNET(array)) / sizeof(counter_u64_t))); \
  123. COUNTER_ARRAY_COPY(VNET(array), &s, sizeof(type) / sizeof(uint64_t));\
  124. if (req->newptr) \
  125. COUNTER_ARRAY_ZERO(VNET(array), \
  126. sizeof(type) / sizeof(uint64_t)); \
  127. return (SYSCTL_OUT(req, &s, sizeof(type))); \
  128. } \
  129. SYSCTL_PROC(parent, nbr, name, \
  130. CTLFLAG_VNET | CTLTYPE_OPAQUE | CTLFLAG_RW | CTLFLAG_NEEDGIANT, \
  131. NULL, 0, array ## _sysctl, "I", desc)
  132. #endif /* SYSCTL_OID */
  133. #ifdef VIMAGE
  134. #include <sys/lock.h>
  135. #include <sys/proc.h> /* for struct thread */
  136. #include <sys/rwlock.h>
  137. #include <sys/sx.h>
  138. /*
  139. * Location of the kernel's 'set_vnet' linker set.
  140. */
  141. extern uintptr_t *__start_set_vnet;
  142. __GLOBL(__start_set_vnet);
  143. extern uintptr_t *__stop_set_vnet;
  144. __GLOBL(__stop_set_vnet);
  145. #define VNET_START (uintptr_t)&__start_set_vnet
  146. #define VNET_STOP (uintptr_t)&__stop_set_vnet
  147. /*
  148. * Functions to allocate and destroy virtual network stacks.
  149. */
  150. struct vnet *vnet_alloc(void);
  151. void vnet_destroy(struct vnet *vnet);
  152. /*
  153. * The current virtual network stack -- we may wish to move this to struct
  154. * pcpu in the future.
  155. */
  156. #define curvnet curthread->td_vnet
  157. /*
  158. * Various macros -- get and set the current network stack, but also
  159. * assertions.
  160. */
  161. #if defined(INVARIANTS) || defined(VNET_DEBUG)
  162. #define VNET_ASSERT(exp, msg) do { \
  163. if (!(exp)) \
  164. panic msg; \
  165. } while (0)
  166. #else
  167. #define VNET_ASSERT(exp, msg) do { \
  168. } while (0)
  169. #endif
  170. #ifdef VNET_DEBUG
  171. void vnet_log_recursion(struct vnet *, const char *, int);
  172. #define CURVNET_SET_QUIET(arg) \
  173. VNET_ASSERT((arg) != NULL && (arg)->vnet_magic_n == VNET_MAGIC_N, \
  174. ("CURVNET_SET at %s:%d %s() curvnet=%p vnet=%p", \
  175. __FILE__, __LINE__, __func__, curvnet, (arg))); \
  176. struct vnet *saved_vnet = curvnet; \
  177. const char *saved_vnet_lpush = curthread->td_vnet_lpush; \
  178. curvnet = arg; \
  179. curthread->td_vnet_lpush = __func__;
  180. #define CURVNET_SET_VERBOSE(arg) \
  181. CURVNET_SET_QUIET(arg) \
  182. if (saved_vnet) \
  183. vnet_log_recursion(saved_vnet, saved_vnet_lpush, __LINE__);
  184. #define CURVNET_SET(arg) CURVNET_SET_VERBOSE(arg)
  185. #define CURVNET_RESTORE() \
  186. VNET_ASSERT(curvnet != NULL && (saved_vnet == NULL || \
  187. saved_vnet->vnet_magic_n == VNET_MAGIC_N), \
  188. ("CURVNET_RESTORE at %s:%d %s() curvnet=%p saved_vnet=%p", \
  189. __FILE__, __LINE__, __func__, curvnet, saved_vnet)); \
  190. curvnet = saved_vnet; \
  191. curthread->td_vnet_lpush = saved_vnet_lpush;
  192. #else /* !VNET_DEBUG */
  193. #define CURVNET_SET_QUIET(arg) \
  194. VNET_ASSERT((arg) != NULL && (arg)->vnet_magic_n == VNET_MAGIC_N, \
  195. ("CURVNET_SET at %s:%d %s() curvnet=%p vnet=%p", \
  196. __FILE__, __LINE__, __func__, curvnet, (arg))); \
  197. struct vnet *saved_vnet = curvnet; \
  198. curvnet = arg;
  199. #define CURVNET_SET_VERBOSE(arg) \
  200. CURVNET_SET_QUIET(arg)
  201. #define CURVNET_SET(arg) CURVNET_SET_VERBOSE(arg)
  202. #define CURVNET_RESTORE() \
  203. VNET_ASSERT(curvnet != NULL && (saved_vnet == NULL || \
  204. saved_vnet->vnet_magic_n == VNET_MAGIC_N), \
  205. ("CURVNET_RESTORE at %s:%d %s() curvnet=%p saved_vnet=%p", \
  206. __FILE__, __LINE__, __func__, curvnet, saved_vnet)); \
  207. curvnet = saved_vnet;
  208. #endif /* VNET_DEBUG */
  209. #define CURVNET_ASSERT_SET() \
  210. VNET_ASSERT(curvnet != NULL, ("vnet is not set at %s:%d %s()", \
  211. __FILE__, __LINE__, __func__))
  212. extern struct vnet *vnet0;
  213. #define IS_DEFAULT_VNET(arg) ((arg) == vnet0)
  214. #define CRED_TO_VNET(cr) (cr)->cr_prison->pr_vnet
  215. #define TD_TO_VNET(td) CRED_TO_VNET((td)->td_ucred)
  216. #define P_TO_VNET(p) CRED_TO_VNET((p)->p_ucred)
  217. /*
  218. * Global linked list of all virtual network stacks, along with read locks to
  219. * access it. If a caller may sleep while accessing the list, it must use
  220. * the sleepable lock macros.
  221. */
  222. LIST_HEAD(vnet_list_head, vnet);
  223. extern struct vnet_list_head vnet_head;
  224. extern struct rwlock vnet_rwlock;
  225. extern struct sx vnet_sxlock;
  226. #define VNET_LIST_RLOCK() sx_slock(&vnet_sxlock)
  227. #define VNET_LIST_RLOCK_NOSLEEP() rw_rlock(&vnet_rwlock)
  228. #define VNET_LIST_RUNLOCK() sx_sunlock(&vnet_sxlock)
  229. #define VNET_LIST_RUNLOCK_NOSLEEP() rw_runlock(&vnet_rwlock)
  230. /*
  231. * Iteration macros to walk the global list of virtual network stacks.
  232. */
  233. #define VNET_ITERATOR_DECL(arg) struct vnet *arg
  234. #define VNET_FOREACH(arg) LIST_FOREACH((arg), &vnet_head, vnet_le)
  235. /*
  236. * Virtual network stack memory allocator, which allows global variables to
  237. * be automatically instantiated for each network stack instance.
  238. */
  239. #define VNET_NAME(n) vnet_entry_##n
  240. #define VNET_DECLARE(t, n) extern t VNET_NAME(n)
  241. /* struct _hack is to stop this from being used with static data */
  242. #define VNET_DEFINE(t, n) \
  243. struct _hack; t VNET_NAME(n) __section(VNET_SETNAME) __used
  244. #if defined(KLD_MODULE) && (defined(__aarch64__) || defined(__riscv) \
  245. || defined(__powerpc64__) || defined(__i386__))
  246. /*
  247. * As with DPCPU_DEFINE_STATIC we are unable to mark this data as static
  248. * in modules on some architectures.
  249. */
  250. #define VNET_DEFINE_STATIC(t, n) \
  251. t VNET_NAME(n) __section(VNET_SETNAME) __used
  252. #else
  253. #define VNET_DEFINE_STATIC(t, n) \
  254. static t VNET_NAME(n) __section(VNET_SETNAME) __used
  255. #endif
  256. #define _VNET_PTR(b, n) (__typeof(VNET_NAME(n))*) \
  257. ((b) + (uintptr_t)&VNET_NAME(n))
  258. #define _VNET(b, n) (*_VNET_PTR(b, n))
  259. /*
  260. * Virtualized global variable accessor macros.
  261. */
  262. #define VNET_VNET_PTR(vnet, n) _VNET_PTR((vnet)->vnet_data_base, n)
  263. #define VNET_VNET(vnet, n) (*VNET_VNET_PTR((vnet), n))
  264. #define VNET_PTR(n) VNET_VNET_PTR(curvnet, n)
  265. #define VNET(n) VNET_VNET(curvnet, n)
  266. /*
  267. * Virtual network stack allocator interfaces from the kernel linker.
  268. */
  269. void *vnet_data_alloc(int size);
  270. void vnet_data_copy(void *start, int size);
  271. void vnet_data_free(void *start_arg, int size);
  272. /*
  273. * Interfaces to manipulate the initial values of virtualized global variables.
  274. */
  275. void vnet_save_init(void *, size_t);
  276. void vnet_restore_init(void *, size_t);
  277. /*
  278. * Virtual sysinit mechanism, allowing network stack components to declare
  279. * startup and shutdown methods to be run when virtual network stack
  280. * instances are created and destroyed.
  281. */
  282. #include <sys/kernel.h>
  283. /*
  284. * SYSINIT/SYSUNINIT variants that provide per-vnet constructors and
  285. * destructors.
  286. */
  287. struct vnet_sysinit {
  288. enum sysinit_sub_id subsystem;
  289. enum sysinit_elem_order order;
  290. sysinit_cfunc_t func;
  291. const void *arg;
  292. TAILQ_ENTRY(vnet_sysinit) link;
  293. };
  294. #define VNET_SYSINIT(ident, subsystem, order, func, arg) \
  295. CTASSERT((subsystem) > SI_SUB_VNET && \
  296. (subsystem) <= SI_SUB_VNET_DONE); \
  297. static struct vnet_sysinit ident ## _vnet_init = { \
  298. subsystem, \
  299. order, \
  300. (sysinit_cfunc_t)(sysinit_nfunc_t)func, \
  301. (arg) \
  302. }; \
  303. SYSINIT(vnet_init_ ## ident, subsystem, order, \
  304. vnet_register_sysinit, &ident ## _vnet_init); \
  305. SYSUNINIT(vnet_init_ ## ident, subsystem, order, \
  306. vnet_deregister_sysinit, &ident ## _vnet_init)
  307. #define VNET_SYSUNINIT(ident, subsystem, order, func, arg) \
  308. CTASSERT((subsystem) > SI_SUB_VNET && \
  309. (subsystem) <= SI_SUB_VNET_DONE); \
  310. static struct vnet_sysinit ident ## _vnet_uninit = { \
  311. subsystem, \
  312. order, \
  313. (sysinit_cfunc_t)(sysinit_nfunc_t)func, \
  314. (arg) \
  315. }; \
  316. SYSINIT(vnet_uninit_ ## ident, subsystem, order, \
  317. vnet_register_sysuninit, &ident ## _vnet_uninit); \
  318. SYSUNINIT(vnet_uninit_ ## ident, subsystem, order, \
  319. vnet_deregister_sysuninit, &ident ## _vnet_uninit)
  320. /*
  321. * Interfaces for managing per-vnet constructors and destructors.
  322. */
  323. void vnet_register_sysinit(void *arg);
  324. void vnet_register_sysuninit(void *arg);
  325. void vnet_deregister_sysinit(void *arg);
  326. void vnet_deregister_sysuninit(void *arg);
  327. /*
  328. * EVENTHANDLER(9) extensions.
  329. */
  330. #include <sys/eventhandler.h>
  331. void vnet_global_eventhandler_iterator_func(void *, ...);
  332. #define VNET_GLOBAL_EVENTHANDLER_REGISTER_TAG(tag, name, func, arg, priority) \
  333. do { \
  334. if (IS_DEFAULT_VNET(curvnet)) { \
  335. (tag) = vimage_eventhandler_register(NULL, #name, func, \
  336. arg, priority, \
  337. vnet_global_eventhandler_iterator_func); \
  338. } \
  339. } while(0)
  340. #define VNET_GLOBAL_EVENTHANDLER_REGISTER(name, func, arg, priority) \
  341. do { \
  342. if (IS_DEFAULT_VNET(curvnet)) { \
  343. vimage_eventhandler_register(NULL, #name, func, \
  344. arg, priority, \
  345. vnet_global_eventhandler_iterator_func); \
  346. } \
  347. } while(0)
  348. #else /* !VIMAGE */
  349. /*
  350. * Various virtual network stack macros compile to no-ops without VIMAGE.
  351. */
  352. #define curvnet NULL
  353. #define VNET_ASSERT(exp, msg)
  354. #define CURVNET_SET(arg)
  355. #define CURVNET_SET_QUIET(arg)
  356. #define CURVNET_RESTORE()
  357. #define CURVNET_ASSERT_SET()
  358. #define VNET_LIST_RLOCK()
  359. #define VNET_LIST_RLOCK_NOSLEEP()
  360. #define VNET_LIST_RUNLOCK()
  361. #define VNET_LIST_RUNLOCK_NOSLEEP()
  362. #define VNET_ITERATOR_DECL(arg)
  363. #define VNET_FOREACH(arg) for (int _vn = 0; _vn == 0; _vn++)
  364. #define IS_DEFAULT_VNET(arg) 1
  365. #define CRED_TO_VNET(cr) NULL
  366. #define TD_TO_VNET(td) NULL
  367. #define P_TO_VNET(p) NULL
  368. /*
  369. * Versions of the VNET macros that compile to normal global variables and
  370. * standard sysctl definitions.
  371. */
  372. #define VNET_NAME(n) n
  373. #define VNET_DECLARE(t, n) extern t n
  374. #define VNET_DEFINE(t, n) struct _hack; t n
  375. #define VNET_DEFINE_STATIC(t, n) static t n
  376. #define _VNET_PTR(b, n) &VNET_NAME(n)
  377. /*
  378. * Virtualized global variable accessor macros.
  379. */
  380. #define VNET_VNET_PTR(vnet, n) (&(n))
  381. #define VNET_VNET(vnet, n) (n)
  382. #define VNET_PTR(n) (&(n))
  383. #define VNET(n) (n)
  384. /*
  385. * When VIMAGE isn't compiled into the kernel, VNET_SYSINIT/VNET_SYSUNINIT
  386. * map into normal sysinits, which have the same ordering properties.
  387. */
  388. #define VNET_SYSINIT(ident, subsystem, order, func, arg) \
  389. SYSINIT(ident, subsystem, order, func, arg)
  390. #define VNET_SYSUNINIT(ident, subsystem, order, func, arg) \
  391. SYSUNINIT(ident, subsystem, order, func, arg)
  392. /*
  393. * Without VIMAGE revert to the default implementation.
  394. */
  395. #define VNET_GLOBAL_EVENTHANDLER_REGISTER_TAG(tag, name, func, arg, priority) \
  396. (tag) = eventhandler_register(NULL, #name, func, arg, priority)
  397. #define VNET_GLOBAL_EVENTHANDLER_REGISTER(name, func, arg, priority) \
  398. eventhandler_register(NULL, #name, func, arg, priority)
  399. #endif /* VIMAGE */
  400. #endif /* _KERNEL */
  401. #endif /* !_NET_VNET_H_ */