vnet.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852
  1. /*-
  2. * SPDX-License-Identifier: BSD-2-Clause
  3. *
  4. * Copyright (c) 2004-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. #include <sys/cdefs.h>
  38. #include "opt_ddb.h"
  39. #include "opt_kdb.h"
  40. #include <sys/param.h>
  41. #include <sys/kdb.h>
  42. #include <sys/kernel.h>
  43. #include <sys/jail.h>
  44. #include <sys/sdt.h>
  45. #include <sys/systm.h>
  46. #include <sys/sysctl.h>
  47. #include <sys/eventhandler.h>
  48. #include <sys/lock.h>
  49. #include <sys/malloc.h>
  50. #include <sys/proc.h>
  51. #include <sys/socket.h>
  52. #include <sys/sx.h>
  53. #include <sys/sysctl.h>
  54. #include <machine/stdarg.h>
  55. #ifdef DDB
  56. #include <ddb/ddb.h>
  57. #include <ddb/db_sym.h>
  58. #endif
  59. #include <net/if.h>
  60. #include <net/if_var.h>
  61. #include <net/vnet.h>
  62. /*-
  63. * This file implements core functions for virtual network stacks:
  64. *
  65. * - Virtual network stack management functions.
  66. *
  67. * - Virtual network stack memory allocator, which virtualizes global
  68. * variables in the network stack
  69. *
  70. * - Virtualized SYSINIT's/SYSUNINIT's, which allow network stack subsystems
  71. * to register startup/shutdown events to be run for each virtual network
  72. * stack instance.
  73. */
  74. FEATURE(vimage, "VIMAGE kernel virtualization");
  75. static MALLOC_DEFINE(M_VNET, "vnet", "network stack control block");
  76. /*
  77. * The virtual network stack list has two read-write locks, one sleepable and
  78. * the other not, so that the list can be stablized and walked in a variety
  79. * of network stack contexts. Both must be acquired exclusively to modify
  80. * the list, but a read lock of either lock is sufficient to walk the list.
  81. */
  82. struct rwlock vnet_rwlock;
  83. struct sx vnet_sxlock;
  84. #define VNET_LIST_WLOCK() do { \
  85. sx_xlock(&vnet_sxlock); \
  86. rw_wlock(&vnet_rwlock); \
  87. } while (0)
  88. #define VNET_LIST_WUNLOCK() do { \
  89. rw_wunlock(&vnet_rwlock); \
  90. sx_xunlock(&vnet_sxlock); \
  91. } while (0)
  92. struct vnet_list_head vnet_head;
  93. struct vnet *vnet0;
  94. /*
  95. * The virtual network stack allocator provides storage for virtualized
  96. * global variables. These variables are defined/declared using the
  97. * VNET_DEFINE()/VNET_DECLARE() macros, which place them in the 'set_vnet'
  98. * linker set. The details of the implementation are somewhat subtle, but
  99. * allow the majority of most network subsystems to maintain
  100. * virtualization-agnostic.
  101. *
  102. * The virtual network stack allocator handles variables in the base kernel
  103. * vs. modules in similar but different ways. In both cases, virtualized
  104. * global variables are marked as such by being declared to be part of the
  105. * vnet linker set. These "master" copies of global variables serve two
  106. * functions:
  107. *
  108. * (1) They contain static initialization or "default" values for global
  109. * variables which will be propagated to each virtual network stack
  110. * instance when created. As with normal global variables, they default
  111. * to zero-filled.
  112. *
  113. * (2) They act as unique global names by which the variable can be referred
  114. * to, regardless of network stack instance. The single global symbol
  115. * will be used to calculate the location of a per-virtual instance
  116. * variable at run-time.
  117. *
  118. * Each virtual network stack instance has a complete copy of each
  119. * virtualized global variable, stored in a malloc'd block of memory
  120. * referred to by vnet->vnet_data_mem. Critical to the design is that each
  121. * per-instance memory block is laid out identically to the master block so
  122. * that the offset of each global variable is the same across all blocks. To
  123. * optimize run-time access, a precalculated 'base' address,
  124. * vnet->vnet_data_base, is stored in each vnet, and is the amount that can
  125. * be added to the address of a 'master' instance of a variable to get to the
  126. * per-vnet instance.
  127. *
  128. * Virtualized global variables are handled in a similar manner, but as each
  129. * module has its own 'set_vnet' linker set, and we want to keep all
  130. * virtualized globals togther, we reserve space in the kernel's linker set
  131. * for potential module variables using a per-vnet character array,
  132. * 'modspace'. The virtual network stack allocator maintains a free list to
  133. * track what space in the array is free (all, initially) and as modules are
  134. * linked, allocates portions of the space to specific globals. The kernel
  135. * module linker queries the virtual network stack allocator and will
  136. * bind references of the global to the location during linking. It also
  137. * calls into the virtual network stack allocator, once the memory is
  138. * initialized, in order to propagate the new static initializations to all
  139. * existing virtual network stack instances so that the soon-to-be executing
  140. * module will find every network stack instance with proper default values.
  141. */
  142. /*
  143. * Number of bytes of data in the 'set_vnet' linker set, and hence the total
  144. * size of all kernel virtualized global variables, and the malloc(9) type
  145. * that will be used to allocate it.
  146. */
  147. #define VNET_BYTES (VNET_STOP - VNET_START)
  148. static MALLOC_DEFINE(M_VNET_DATA, "vnet_data", "VNET data");
  149. /*
  150. * VNET_MODMIN is the minimum number of bytes we will reserve for the sum of
  151. * global variables across all loaded modules. As this actually sizes an
  152. * array declared as a virtualized global variable in the kernel itself, and
  153. * we want the virtualized global variable space to be page-sized, we may
  154. * have more space than that in practice.
  155. */
  156. #define VNET_MODMIN (8 * PAGE_SIZE)
  157. #define VNET_SIZE roundup2(VNET_BYTES, PAGE_SIZE)
  158. /*
  159. * Space to store virtualized global variables from loadable kernel modules,
  160. * and the free list to manage it.
  161. */
  162. VNET_DEFINE_STATIC(char, modspace[VNET_MODMIN] __aligned(__alignof(void *)));
  163. /*
  164. * A copy of the initial values of all virtualized global variables.
  165. */
  166. static uintptr_t vnet_init_var;
  167. /*
  168. * Global lists of subsystem constructor and destructors for vnets. They are
  169. * registered via VNET_SYSINIT() and VNET_SYSUNINIT(). Both lists are
  170. * protected by the vnet_sysinit_sxlock global lock.
  171. */
  172. static TAILQ_HEAD(vnet_sysinit_head, vnet_sysinit) vnet_constructors =
  173. TAILQ_HEAD_INITIALIZER(vnet_constructors);
  174. static TAILQ_HEAD(vnet_sysuninit_head, vnet_sysinit) vnet_destructors =
  175. TAILQ_HEAD_INITIALIZER(vnet_destructors);
  176. struct sx vnet_sysinit_sxlock;
  177. #define VNET_SYSINIT_WLOCK() sx_xlock(&vnet_sysinit_sxlock);
  178. #define VNET_SYSINIT_WUNLOCK() sx_xunlock(&vnet_sysinit_sxlock);
  179. #define VNET_SYSINIT_RLOCK() sx_slock(&vnet_sysinit_sxlock);
  180. #define VNET_SYSINIT_RUNLOCK() sx_sunlock(&vnet_sysinit_sxlock);
  181. struct vnet_data_free {
  182. uintptr_t vnd_start;
  183. int vnd_len;
  184. TAILQ_ENTRY(vnet_data_free) vnd_link;
  185. };
  186. static MALLOC_DEFINE(M_VNET_DATA_FREE, "vnet_data_free",
  187. "VNET resource accounting");
  188. static TAILQ_HEAD(, vnet_data_free) vnet_data_free_head =
  189. TAILQ_HEAD_INITIALIZER(vnet_data_free_head);
  190. static struct sx vnet_data_free_lock;
  191. SDT_PROVIDER_DEFINE(vnet);
  192. SDT_PROBE_DEFINE1(vnet, functions, vnet_alloc, entry, "int");
  193. SDT_PROBE_DEFINE2(vnet, functions, vnet_alloc, alloc, "int",
  194. "struct vnet *");
  195. SDT_PROBE_DEFINE2(vnet, functions, vnet_alloc, return,
  196. "int", "struct vnet *");
  197. SDT_PROBE_DEFINE2(vnet, functions, vnet_destroy, entry,
  198. "int", "struct vnet *");
  199. SDT_PROBE_DEFINE1(vnet, functions, vnet_destroy, return,
  200. "int");
  201. /*
  202. * Run per-vnet sysinits or sysuninits during vnet creation/destruction.
  203. */
  204. static void vnet_sysinit(void);
  205. static void vnet_sysuninit(void);
  206. #ifdef DDB
  207. static void db_show_vnet_print_vs(struct vnet_sysinit *, int);
  208. #endif
  209. /*
  210. * Allocate a virtual network stack.
  211. */
  212. struct vnet *
  213. vnet_alloc(void)
  214. {
  215. struct vnet *vnet;
  216. SDT_PROBE1(vnet, functions, vnet_alloc, entry, __LINE__);
  217. vnet = malloc(sizeof(struct vnet), M_VNET, M_WAITOK | M_ZERO);
  218. vnet->vnet_magic_n = VNET_MAGIC_N;
  219. SDT_PROBE2(vnet, functions, vnet_alloc, alloc, __LINE__, vnet);
  220. /*
  221. * Allocate storage for virtualized global variables and copy in
  222. * initial values from our 'master' copy.
  223. */
  224. vnet->vnet_data_mem = malloc(VNET_SIZE, M_VNET_DATA, M_WAITOK);
  225. memcpy(vnet->vnet_data_mem, (void *)VNET_START, VNET_BYTES);
  226. /*
  227. * All use of vnet-specific data will immediately subtract VNET_START
  228. * from the base memory pointer, so pre-calculate that now to avoid
  229. * it on each use.
  230. */
  231. vnet->vnet_data_base = (uintptr_t)vnet->vnet_data_mem - VNET_START;
  232. /* Initialize / attach vnet module instances. */
  233. CURVNET_SET_QUIET(vnet);
  234. vnet_sysinit();
  235. CURVNET_RESTORE();
  236. VNET_LIST_WLOCK();
  237. LIST_INSERT_HEAD(&vnet_head, vnet, vnet_le);
  238. VNET_LIST_WUNLOCK();
  239. SDT_PROBE2(vnet, functions, vnet_alloc, return, __LINE__, vnet);
  240. return (vnet);
  241. }
  242. /*
  243. * Destroy a virtual network stack.
  244. */
  245. void
  246. vnet_destroy(struct vnet *vnet)
  247. {
  248. SDT_PROBE2(vnet, functions, vnet_destroy, entry, __LINE__, vnet);
  249. KASSERT(vnet->vnet_sockcnt == 0,
  250. ("%s: vnet still has sockets", __func__));
  251. VNET_LIST_WLOCK();
  252. LIST_REMOVE(vnet, vnet_le);
  253. VNET_LIST_WUNLOCK();
  254. /* Signal that VNET is being shutdown. */
  255. vnet->vnet_shutdown = true;
  256. CURVNET_SET_QUIET(vnet);
  257. sx_xlock(&ifnet_detach_sxlock);
  258. vnet_sysuninit();
  259. sx_xunlock(&ifnet_detach_sxlock);
  260. CURVNET_RESTORE();
  261. /*
  262. * Release storage for the virtual network stack instance.
  263. */
  264. free(vnet->vnet_data_mem, M_VNET_DATA);
  265. vnet->vnet_data_mem = NULL;
  266. vnet->vnet_data_base = 0;
  267. vnet->vnet_magic_n = 0xdeadbeef;
  268. free(vnet, M_VNET);
  269. SDT_PROBE1(vnet, functions, vnet_destroy, return, __LINE__);
  270. }
  271. /*
  272. * Boot time initialization and allocation of virtual network stacks.
  273. */
  274. static void
  275. vnet_init_prelink(void *arg __unused)
  276. {
  277. rw_init(&vnet_rwlock, "vnet_rwlock");
  278. sx_init(&vnet_sxlock, "vnet_sxlock");
  279. sx_init(&vnet_sysinit_sxlock, "vnet_sysinit_sxlock");
  280. LIST_INIT(&vnet_head);
  281. }
  282. SYSINIT(vnet_init_prelink, SI_SUB_VNET_PRELINK, SI_ORDER_FIRST,
  283. vnet_init_prelink, NULL);
  284. static void
  285. vnet0_init(void *arg __unused)
  286. {
  287. if (bootverbose)
  288. printf("VIMAGE (virtualized network stack) enabled\n");
  289. /*
  290. * We MUST clear curvnet in vi_init_done() before going SMP,
  291. * otherwise CURVNET_SET() macros would scream about unnecessary
  292. * curvnet recursions.
  293. */
  294. curvnet = prison0.pr_vnet = vnet0 = vnet_alloc();
  295. }
  296. SYSINIT(vnet0_init, SI_SUB_VNET, SI_ORDER_FIRST, vnet0_init, NULL);
  297. static void
  298. vnet_init_done(void *unused __unused)
  299. {
  300. curvnet = NULL;
  301. }
  302. SYSINIT(vnet_init_done, SI_SUB_VNET_DONE, SI_ORDER_ANY, vnet_init_done,
  303. NULL);
  304. /*
  305. * Once on boot, initialize the modspace freelist to entirely cover modspace.
  306. */
  307. static void
  308. vnet_data_startup(void *dummy __unused)
  309. {
  310. struct vnet_data_free *df;
  311. df = malloc(sizeof(*df), M_VNET_DATA_FREE, M_WAITOK | M_ZERO);
  312. df->vnd_start = (uintptr_t)&VNET_NAME(modspace);
  313. df->vnd_len = VNET_MODMIN;
  314. TAILQ_INSERT_HEAD(&vnet_data_free_head, df, vnd_link);
  315. sx_init(&vnet_data_free_lock, "vnet_data alloc lock");
  316. vnet_init_var = (uintptr_t)malloc(VNET_BYTES, M_VNET_DATA, M_WAITOK);
  317. }
  318. SYSINIT(vnet_data, SI_SUB_KLD, SI_ORDER_FIRST, vnet_data_startup, NULL);
  319. /* Dummy VNET_SYSINIT to make sure we always reach the final end state. */
  320. static void
  321. vnet_sysinit_done(void *unused __unused)
  322. {
  323. return;
  324. }
  325. VNET_SYSINIT(vnet_sysinit_done, SI_SUB_VNET_DONE, SI_ORDER_ANY,
  326. vnet_sysinit_done, NULL);
  327. /*
  328. * When a module is loaded and requires storage for a virtualized global
  329. * variable, allocate space from the modspace free list. This interface
  330. * should be used only by the kernel linker.
  331. */
  332. void *
  333. vnet_data_alloc(int size)
  334. {
  335. struct vnet_data_free *df;
  336. void *s;
  337. s = NULL;
  338. size = roundup2(size, sizeof(void *));
  339. sx_xlock(&vnet_data_free_lock);
  340. TAILQ_FOREACH(df, &vnet_data_free_head, vnd_link) {
  341. if (df->vnd_len < size)
  342. continue;
  343. if (df->vnd_len == size) {
  344. s = (void *)df->vnd_start;
  345. TAILQ_REMOVE(&vnet_data_free_head, df, vnd_link);
  346. free(df, M_VNET_DATA_FREE);
  347. break;
  348. }
  349. s = (void *)df->vnd_start;
  350. df->vnd_len -= size;
  351. df->vnd_start = df->vnd_start + size;
  352. break;
  353. }
  354. sx_xunlock(&vnet_data_free_lock);
  355. return (s);
  356. }
  357. /*
  358. * Free space for a virtualized global variable on module unload.
  359. */
  360. void
  361. vnet_data_free(void *start_arg, int size)
  362. {
  363. struct vnet_data_free *df;
  364. struct vnet_data_free *dn;
  365. uintptr_t start;
  366. uintptr_t end;
  367. size = roundup2(size, sizeof(void *));
  368. start = (uintptr_t)start_arg;
  369. end = start + size;
  370. /*
  371. * Free a region of space and merge it with as many neighbors as
  372. * possible. Keeping the list sorted simplifies this operation.
  373. */
  374. sx_xlock(&vnet_data_free_lock);
  375. TAILQ_FOREACH(df, &vnet_data_free_head, vnd_link) {
  376. if (df->vnd_start > end)
  377. break;
  378. /*
  379. * If we expand at the end of an entry we may have to merge
  380. * it with the one following it as well.
  381. */
  382. if (df->vnd_start + df->vnd_len == start) {
  383. df->vnd_len += size;
  384. dn = TAILQ_NEXT(df, vnd_link);
  385. if (df->vnd_start + df->vnd_len == dn->vnd_start) {
  386. df->vnd_len += dn->vnd_len;
  387. TAILQ_REMOVE(&vnet_data_free_head, dn,
  388. vnd_link);
  389. free(dn, M_VNET_DATA_FREE);
  390. }
  391. sx_xunlock(&vnet_data_free_lock);
  392. return;
  393. }
  394. if (df->vnd_start == end) {
  395. df->vnd_start = start;
  396. df->vnd_len += size;
  397. sx_xunlock(&vnet_data_free_lock);
  398. return;
  399. }
  400. }
  401. dn = malloc(sizeof(*df), M_VNET_DATA_FREE, M_WAITOK | M_ZERO);
  402. dn->vnd_start = start;
  403. dn->vnd_len = size;
  404. if (df)
  405. TAILQ_INSERT_BEFORE(df, dn, vnd_link);
  406. else
  407. TAILQ_INSERT_TAIL(&vnet_data_free_head, dn, vnd_link);
  408. sx_xunlock(&vnet_data_free_lock);
  409. }
  410. /*
  411. * When a new virtualized global variable has been allocated, propagate its
  412. * initial value to each already-allocated virtual network stack instance.
  413. */
  414. void
  415. vnet_data_copy(void *start, int size)
  416. {
  417. struct vnet *vnet;
  418. VNET_LIST_RLOCK();
  419. LIST_FOREACH(vnet, &vnet_head, vnet_le)
  420. memcpy((void *)((uintptr_t)vnet->vnet_data_base +
  421. (uintptr_t)start), start, size);
  422. VNET_LIST_RUNLOCK();
  423. }
  424. /*
  425. * Save a copy of the initial values of virtualized global variables.
  426. */
  427. void
  428. vnet_save_init(void *start, size_t size)
  429. {
  430. MPASS(vnet_init_var != 0);
  431. MPASS(VNET_START <= (uintptr_t)start &&
  432. (uintptr_t)start + size <= VNET_STOP);
  433. memcpy((void *)(vnet_init_var + ((uintptr_t)start - VNET_START)),
  434. start, size);
  435. }
  436. /*
  437. * Restore the 'master' copies of virtualized global variables to theirs
  438. * initial values.
  439. */
  440. void
  441. vnet_restore_init(void *start, size_t size)
  442. {
  443. MPASS(vnet_init_var != 0);
  444. MPASS(VNET_START <= (uintptr_t)start &&
  445. (uintptr_t)start + size <= VNET_STOP);
  446. memcpy(start,
  447. (void *)(vnet_init_var + ((uintptr_t)start - VNET_START)), size);
  448. }
  449. /*
  450. * Support for special SYSINIT handlers registered via VNET_SYSINIT()
  451. * and VNET_SYSUNINIT().
  452. */
  453. void
  454. vnet_register_sysinit(void *arg)
  455. {
  456. struct vnet_sysinit *vs, *vs2;
  457. struct vnet *vnet;
  458. vs = arg;
  459. KASSERT(vs->subsystem > SI_SUB_VNET, ("vnet sysinit too early"));
  460. /* Add the constructor to the global list of vnet constructors. */
  461. VNET_SYSINIT_WLOCK();
  462. TAILQ_FOREACH(vs2, &vnet_constructors, link) {
  463. if (vs2->subsystem > vs->subsystem)
  464. break;
  465. if (vs2->subsystem == vs->subsystem && vs2->order > vs->order)
  466. break;
  467. }
  468. if (vs2 != NULL)
  469. TAILQ_INSERT_BEFORE(vs2, vs, link);
  470. else
  471. TAILQ_INSERT_TAIL(&vnet_constructors, vs, link);
  472. /*
  473. * Invoke the constructor on all the existing vnets when it is
  474. * registered.
  475. */
  476. VNET_LIST_RLOCK();
  477. VNET_FOREACH(vnet) {
  478. CURVNET_SET_QUIET(vnet);
  479. vs->func(vs->arg);
  480. CURVNET_RESTORE();
  481. }
  482. VNET_LIST_RUNLOCK();
  483. VNET_SYSINIT_WUNLOCK();
  484. }
  485. void
  486. vnet_deregister_sysinit(void *arg)
  487. {
  488. struct vnet_sysinit *vs;
  489. vs = arg;
  490. /* Remove the constructor from the global list of vnet constructors. */
  491. VNET_SYSINIT_WLOCK();
  492. TAILQ_REMOVE(&vnet_constructors, vs, link);
  493. VNET_SYSINIT_WUNLOCK();
  494. }
  495. void
  496. vnet_register_sysuninit(void *arg)
  497. {
  498. struct vnet_sysinit *vs, *vs2;
  499. vs = arg;
  500. /* Add the destructor to the global list of vnet destructors. */
  501. VNET_SYSINIT_WLOCK();
  502. TAILQ_FOREACH(vs2, &vnet_destructors, link) {
  503. if (vs2->subsystem > vs->subsystem)
  504. break;
  505. if (vs2->subsystem == vs->subsystem && vs2->order > vs->order)
  506. break;
  507. }
  508. if (vs2 != NULL)
  509. TAILQ_INSERT_BEFORE(vs2, vs, link);
  510. else
  511. TAILQ_INSERT_TAIL(&vnet_destructors, vs, link);
  512. VNET_SYSINIT_WUNLOCK();
  513. }
  514. void
  515. vnet_deregister_sysuninit(void *arg)
  516. {
  517. struct vnet_sysinit *vs;
  518. struct vnet *vnet;
  519. vs = arg;
  520. /*
  521. * Invoke the destructor on all the existing vnets when it is
  522. * deregistered.
  523. */
  524. VNET_SYSINIT_WLOCK();
  525. VNET_LIST_RLOCK();
  526. VNET_FOREACH(vnet) {
  527. CURVNET_SET_QUIET(vnet);
  528. vs->func(vs->arg);
  529. CURVNET_RESTORE();
  530. }
  531. /* Remove the destructor from the global list of vnet destructors. */
  532. TAILQ_REMOVE(&vnet_destructors, vs, link);
  533. VNET_SYSINIT_WUNLOCK();
  534. VNET_LIST_RUNLOCK();
  535. }
  536. /*
  537. * Invoke all registered vnet constructors on the current vnet. Used during
  538. * vnet construction. The caller is responsible for ensuring the new vnet is
  539. * the current vnet and that the vnet_sysinit_sxlock lock is locked.
  540. */
  541. static void
  542. vnet_sysinit(void)
  543. {
  544. struct vnet_sysinit *vs;
  545. VNET_SYSINIT_RLOCK();
  546. TAILQ_FOREACH(vs, &vnet_constructors, link) {
  547. curvnet->vnet_state = vs->subsystem;
  548. vs->func(vs->arg);
  549. }
  550. VNET_SYSINIT_RUNLOCK();
  551. }
  552. /*
  553. * Invoke all registered vnet destructors on the current vnet. Used during
  554. * vnet destruction. The caller is responsible for ensuring the dying vnet
  555. * the current vnet and that the vnet_sysinit_sxlock lock is locked.
  556. */
  557. static void
  558. vnet_sysuninit(void)
  559. {
  560. struct vnet_sysinit *vs;
  561. VNET_SYSINIT_RLOCK();
  562. TAILQ_FOREACH_REVERSE(vs, &vnet_destructors, vnet_sysuninit_head,
  563. link) {
  564. curvnet->vnet_state = vs->subsystem;
  565. vs->func(vs->arg);
  566. }
  567. VNET_SYSINIT_RUNLOCK();
  568. }
  569. /*
  570. * EVENTHANDLER(9) extensions.
  571. */
  572. /*
  573. * Invoke the eventhandler function originally registered with the possibly
  574. * registered argument for all virtual network stack instances.
  575. *
  576. * This iterator can only be used for eventhandlers that do not take any
  577. * additional arguments, as we do ignore the variadic arguments from the
  578. * EVENTHANDLER_INVOKE() call.
  579. */
  580. void
  581. vnet_global_eventhandler_iterator_func(void *arg, ...)
  582. {
  583. VNET_ITERATOR_DECL(vnet_iter);
  584. struct eventhandler_entry_vimage *v_ee;
  585. /*
  586. * There is a bug here in that we should actually cast things to
  587. * (struct eventhandler_entry_ ## name *) but that's not easily
  588. * possible in here so just re-using the variadic version we
  589. * defined for the generic vimage case.
  590. */
  591. v_ee = arg;
  592. VNET_LIST_RLOCK();
  593. VNET_FOREACH(vnet_iter) {
  594. CURVNET_SET(vnet_iter);
  595. ((vimage_iterator_func_t)v_ee->func)(v_ee->ee_arg);
  596. CURVNET_RESTORE();
  597. }
  598. VNET_LIST_RUNLOCK();
  599. }
  600. #ifdef VNET_DEBUG
  601. struct vnet_recursion {
  602. SLIST_ENTRY(vnet_recursion) vnr_le;
  603. const char *prev_fn;
  604. const char *where_fn;
  605. int where_line;
  606. struct vnet *old_vnet;
  607. struct vnet *new_vnet;
  608. };
  609. static SLIST_HEAD(, vnet_recursion) vnet_recursions =
  610. SLIST_HEAD_INITIALIZER(vnet_recursions);
  611. static void
  612. vnet_print_recursion(struct vnet_recursion *vnr, int brief)
  613. {
  614. if (!brief)
  615. printf("CURVNET_SET() recursion in ");
  616. printf("%s() line %d, prev in %s()", vnr->where_fn, vnr->where_line,
  617. vnr->prev_fn);
  618. if (brief)
  619. printf(", ");
  620. else
  621. printf("\n ");
  622. printf("%p -> %p\n", vnr->old_vnet, vnr->new_vnet);
  623. }
  624. void
  625. vnet_log_recursion(struct vnet *old_vnet, const char *old_fn, int line)
  626. {
  627. struct vnet_recursion *vnr;
  628. /* Skip already logged recursion events. */
  629. SLIST_FOREACH(vnr, &vnet_recursions, vnr_le)
  630. if (vnr->prev_fn == old_fn &&
  631. vnr->where_fn == curthread->td_vnet_lpush &&
  632. vnr->where_line == line &&
  633. (vnr->old_vnet == vnr->new_vnet) == (curvnet == old_vnet))
  634. return;
  635. vnr = malloc(sizeof(*vnr), M_VNET, M_NOWAIT | M_ZERO);
  636. if (vnr == NULL)
  637. panic("%s: malloc failed", __func__);
  638. vnr->prev_fn = old_fn;
  639. vnr->where_fn = curthread->td_vnet_lpush;
  640. vnr->where_line = line;
  641. vnr->old_vnet = old_vnet;
  642. vnr->new_vnet = curvnet;
  643. SLIST_INSERT_HEAD(&vnet_recursions, vnr, vnr_le);
  644. vnet_print_recursion(vnr, 0);
  645. #ifdef KDB
  646. kdb_backtrace();
  647. #endif
  648. }
  649. #endif /* VNET_DEBUG */
  650. /*
  651. * DDB(4).
  652. */
  653. #ifdef DDB
  654. static void
  655. db_vnet_print(struct vnet *vnet)
  656. {
  657. db_printf("vnet = %p\n", vnet);
  658. db_printf(" vnet_magic_n = %#08x (%s, orig %#08x)\n",
  659. vnet->vnet_magic_n,
  660. (vnet->vnet_magic_n == VNET_MAGIC_N) ?
  661. "ok" : "mismatch", VNET_MAGIC_N);
  662. db_printf(" vnet_ifcnt = %u\n", vnet->vnet_ifcnt);
  663. db_printf(" vnet_sockcnt = %u\n", vnet->vnet_sockcnt);
  664. db_printf(" vnet_data_mem = %p\n", vnet->vnet_data_mem);
  665. db_printf(" vnet_data_base = %#jx\n",
  666. (uintmax_t)vnet->vnet_data_base);
  667. db_printf(" vnet_state = %#08x\n", vnet->vnet_state);
  668. db_printf(" vnet_shutdown = %#03x\n", vnet->vnet_shutdown);
  669. db_printf("\n");
  670. }
  671. DB_SHOW_ALL_COMMAND(vnets, db_show_all_vnets)
  672. {
  673. VNET_ITERATOR_DECL(vnet_iter);
  674. VNET_FOREACH(vnet_iter) {
  675. db_vnet_print(vnet_iter);
  676. if (db_pager_quit)
  677. break;
  678. }
  679. }
  680. DB_SHOW_COMMAND(vnet, db_show_vnet)
  681. {
  682. if (!have_addr) {
  683. db_printf("usage: show vnet <struct vnet *>\n");
  684. return;
  685. }
  686. db_vnet_print((struct vnet *)addr);
  687. }
  688. static void
  689. db_show_vnet_print_vs(struct vnet_sysinit *vs, int ddb)
  690. {
  691. const char *vsname, *funcname;
  692. c_db_sym_t sym;
  693. db_expr_t offset;
  694. #define xprint(...) \
  695. if (ddb) \
  696. db_printf(__VA_ARGS__); \
  697. else \
  698. printf(__VA_ARGS__)
  699. if (vs == NULL) {
  700. xprint("%s: no vnet_sysinit * given\n", __func__);
  701. return;
  702. }
  703. sym = db_search_symbol((vm_offset_t)vs, DB_STGY_ANY, &offset);
  704. db_symbol_values(sym, &vsname, NULL);
  705. sym = db_search_symbol((vm_offset_t)vs->func, DB_STGY_PROC, &offset);
  706. db_symbol_values(sym, &funcname, NULL);
  707. xprint("%s(%p)\n", (vsname != NULL) ? vsname : "", vs);
  708. xprint(" %#08x %#08x\n", vs->subsystem, vs->order);
  709. xprint(" %p(%s)(%p)\n",
  710. vs->func, (funcname != NULL) ? funcname : "", vs->arg);
  711. #undef xprint
  712. }
  713. DB_SHOW_COMMAND_FLAGS(vnet_sysinit, db_show_vnet_sysinit, DB_CMD_MEMSAFE)
  714. {
  715. struct vnet_sysinit *vs;
  716. db_printf("VNET_SYSINIT vs Name(Ptr)\n");
  717. db_printf(" Subsystem Order\n");
  718. db_printf(" Function(Name)(Arg)\n");
  719. TAILQ_FOREACH(vs, &vnet_constructors, link) {
  720. db_show_vnet_print_vs(vs, 1);
  721. if (db_pager_quit)
  722. break;
  723. }
  724. }
  725. DB_SHOW_COMMAND_FLAGS(vnet_sysuninit, db_show_vnet_sysuninit, DB_CMD_MEMSAFE)
  726. {
  727. struct vnet_sysinit *vs;
  728. db_printf("VNET_SYSUNINIT vs Name(Ptr)\n");
  729. db_printf(" Subsystem Order\n");
  730. db_printf(" Function(Name)(Arg)\n");
  731. TAILQ_FOREACH_REVERSE(vs, &vnet_destructors, vnet_sysuninit_head,
  732. link) {
  733. db_show_vnet_print_vs(vs, 1);
  734. if (db_pager_quit)
  735. break;
  736. }
  737. }
  738. #ifdef VNET_DEBUG
  739. DB_SHOW_COMMAND_FLAGS(vnetrcrs, db_show_vnetrcrs, DB_CMD_MEMSAFE)
  740. {
  741. struct vnet_recursion *vnr;
  742. SLIST_FOREACH(vnr, &vnet_recursions, vnr_le)
  743. vnet_print_recursion(vnr, 1);
  744. }
  745. #endif
  746. #endif /* DDB */