vnet.c 22 KB

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