xref: /freebsd-13-stable/sys/net/vnet.h (revision d68ccfb4ea2bf5245df1267377c35d9727c38264)
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 /*-
39  * This header file defines several sets of interfaces supporting virtualized
40  * network stacks:
41  *
42  * - Definition of 'struct vnet' and functions and macros to allocate/free/
43  *   manipulate it.
44  *
45  * - A virtual network stack memory allocator, which provides support for
46  *   virtualized global variables via a special linker set, set_vnet.
47  *
48  * - Virtualized sysinits/sysuninits, which allow constructors and
49  *   destructors to be run for each network stack subsystem as virtual
50  *   instances are created and destroyed.
51  *
52  * If VIMAGE isn't compiled into the kernel, virtualized global variables
53  * compile to normal global variables, and virtualized sysinits to regular
54  * sysinits.
55  */
56 
57 #ifndef _NET_VNET_H_
58 #define	_NET_VNET_H_
59 
60 /*
61  * struct vnet describes a virtualized network stack, and is primarily a
62  * pointer to storage for virtualized global variables.  Expose to userspace
63  * as required for libkvm.
64  */
65 #if defined(_KERNEL) || defined(_WANT_VNET)
66 #include <sys/queue.h>
67 
68 struct vnet {
69 	LIST_ENTRY(vnet)	 vnet_le;	/* all vnets list */
70 	u_int			 vnet_magic_n;
71 	u_int			 vnet_ifcnt;
72 	u_int			 vnet_sockcnt;
73 	u_int			 vnet_state;	/* SI_SUB_* */
74 	void			*vnet_data_mem;
75 	uintptr_t		 vnet_data_base;
76 	bool			 vnet_shutdown;	/* Shutdown in progress. */
77 } __aligned(CACHE_LINE_SIZE);
78 #define	VNET_MAGIC_N	0x5e4a6f28
79 
80 /*
81  * These two virtual network stack allocator definitions are also required
82  * for libkvm so that it can evaluate virtualized global variables.
83  */
84 #define	VNET_SETNAME		"set_vnet"
85 #define	VNET_SYMPREFIX		"vnet_entry_"
86 #endif
87 
88 #ifdef _KERNEL
89 
90 #define	VNET_PCPUSTAT_DECLARE(type, name)	\
91     VNET_DECLARE(counter_u64_t, name[sizeof(type) / sizeof(uint64_t)])
92 
93 #define	VNET_PCPUSTAT_DEFINE(type, name)	\
94     VNET_DEFINE(counter_u64_t, name[sizeof(type) / sizeof(uint64_t)])
95 #define	VNET_PCPUSTAT_DEFINE_STATIC(type, name)	\
96     VNET_DEFINE_STATIC(counter_u64_t, name[sizeof(type) / sizeof(uint64_t)])
97 
98 #define	VNET_PCPUSTAT_ALLOC(name, wait)	\
99     COUNTER_ARRAY_ALLOC(VNET(name), \
100 	sizeof(VNET(name)) / sizeof(counter_u64_t), (wait))
101 
102 #define	VNET_PCPUSTAT_FREE(name)	\
103     COUNTER_ARRAY_FREE(VNET(name), sizeof(VNET(name)) / sizeof(counter_u64_t))
104 
105 #define	VNET_PCPUSTAT_ADD(type, name, f, v)	\
106     counter_u64_add(VNET(name)[offsetof(type, f) / sizeof(uint64_t)], (v))
107 
108 #define	VNET_PCPUSTAT_FETCH(type, name, f)	\
109     counter_u64_fetch(VNET(name)[offsetof(type, f) / sizeof(uint64_t)])
110 
111 #define	VNET_PCPUSTAT_SYSINIT(name)	\
112 static void				\
113 vnet_##name##_init(const void *unused)	\
114 {					\
115 	VNET_PCPUSTAT_ALLOC(name, M_WAITOK);	\
116 }					\
117 VNET_SYSINIT(vnet_ ## name ## _init, SI_SUB_INIT_IF,			\
118     SI_ORDER_FIRST, vnet_ ## name ## _init, NULL)
119 
120 #define	VNET_PCPUSTAT_SYSUNINIT(name)					\
121 static void								\
122 vnet_##name##_uninit(const void *unused)				\
123 {									\
124 	VNET_PCPUSTAT_FREE(name);					\
125 }									\
126 VNET_SYSUNINIT(vnet_ ## name ## _uninit, SI_SUB_INIT_IF,		\
127     SI_ORDER_FIRST, vnet_ ## name ## _uninit, NULL)
128 
129 #ifdef SYSCTL_OID
130 #define	SYSCTL_VNET_PCPUSTAT(parent, nbr, name, type, array, desc)	\
131 static int								\
132 array##_sysctl(SYSCTL_HANDLER_ARGS)					\
133 {									\
134 	type s;								\
135 	CTASSERT((sizeof(type) / sizeof(uint64_t)) ==			\
136 	    (sizeof(VNET(array)) / sizeof(counter_u64_t)));		\
137 	COUNTER_ARRAY_COPY(VNET(array), &s, sizeof(type) / sizeof(uint64_t));\
138 	if (req->newptr)						\
139 		COUNTER_ARRAY_ZERO(VNET(array),				\
140 		    sizeof(type) / sizeof(uint64_t));			\
141 	return (SYSCTL_OUT(req, &s, sizeof(type)));			\
142 }									\
143 SYSCTL_PROC(parent, nbr, name,						\
144     CTLFLAG_VNET | CTLTYPE_OPAQUE | CTLFLAG_RW | CTLFLAG_NEEDGIANT,	\
145     NULL, 0, array ## _sysctl, "I", desc)
146 #endif /* SYSCTL_OID */
147 
148 #ifdef VIMAGE
149 #include <sys/lock.h>
150 #include <sys/proc.h>			/* for struct thread */
151 #include <sys/rwlock.h>
152 #include <sys/sx.h>
153 
154 /*
155  * Location of the kernel's 'set_vnet' linker set.
156  */
157 extern uintptr_t	*__start_set_vnet;
158 __GLOBL(__start_set_vnet);
159 extern uintptr_t	*__stop_set_vnet;
160 __GLOBL(__stop_set_vnet);
161 
162 #define	VNET_START	(uintptr_t)&__start_set_vnet
163 #define	VNET_STOP	(uintptr_t)&__stop_set_vnet
164 
165 /*
166  * Functions to allocate and destroy virtual network stacks.
167  */
168 struct vnet *vnet_alloc(void);
169 void	vnet_destroy(struct vnet *vnet);
170 
171 /*
172  * The current virtual network stack -- we may wish to move this to struct
173  * pcpu in the future.
174  */
175 #define	curvnet	curthread->td_vnet
176 
177 /*
178  * Various macros -- get and set the current network stack, but also
179  * assertions.
180  */
181 #if defined(INVARIANTS) || defined(VNET_DEBUG)
182 #define	VNET_ASSERT(exp, msg)	do {					\
183 	if (!(exp))							\
184 		panic msg;						\
185 } while (0)
186 #else
187 #define	VNET_ASSERT(exp, msg)	do {					\
188 } while (0)
189 #endif
190 
191 #ifdef VNET_DEBUG
192 void vnet_log_recursion(struct vnet *, const char *, int);
193 
194 #define	CURVNET_SET_QUIET(arg)						\
195 	VNET_ASSERT((arg) != NULL && (arg)->vnet_magic_n == VNET_MAGIC_N, \
196 	    ("CURVNET_SET at %s:%d %s() curvnet=%p vnet=%p",		\
197 	    __FILE__, __LINE__, __func__, curvnet, (arg)));		\
198 	struct vnet *saved_vnet = curvnet;				\
199 	const char *saved_vnet_lpush = curthread->td_vnet_lpush;	\
200 	curvnet = arg;							\
201 	curthread->td_vnet_lpush = __func__;
202 
203 #define	CURVNET_SET_VERBOSE(arg)					\
204 	CURVNET_SET_QUIET(arg)						\
205 	if (saved_vnet)							\
206 		vnet_log_recursion(saved_vnet, saved_vnet_lpush, __LINE__);
207 
208 #define	CURVNET_SET(arg)	CURVNET_SET_VERBOSE(arg)
209 
210 #define	CURVNET_RESTORE()						\
211 	VNET_ASSERT(curvnet != NULL && (saved_vnet == NULL ||		\
212 	    saved_vnet->vnet_magic_n == VNET_MAGIC_N),			\
213 	    ("CURVNET_RESTORE at %s:%d %s() curvnet=%p saved_vnet=%p",	\
214 	    __FILE__, __LINE__, __func__, curvnet, saved_vnet));	\
215 	curvnet = saved_vnet;						\
216 	curthread->td_vnet_lpush = saved_vnet_lpush;
217 #else /* !VNET_DEBUG */
218 
219 #define	CURVNET_SET_QUIET(arg)						\
220 	VNET_ASSERT((arg) != NULL && (arg)->vnet_magic_n == VNET_MAGIC_N, \
221 	    ("CURVNET_SET at %s:%d %s() curvnet=%p vnet=%p",		\
222 	    __FILE__, __LINE__, __func__, curvnet, (arg)));		\
223 	struct vnet *saved_vnet = curvnet;				\
224 	curvnet = arg;
225 
226 #define	CURVNET_SET_VERBOSE(arg)					\
227 	CURVNET_SET_QUIET(arg)
228 
229 #define	CURVNET_SET(arg)	CURVNET_SET_VERBOSE(arg)
230 
231 #define	CURVNET_RESTORE()						\
232 	VNET_ASSERT(curvnet != NULL && (saved_vnet == NULL ||		\
233 	    saved_vnet->vnet_magic_n == VNET_MAGIC_N),			\
234 	    ("CURVNET_RESTORE at %s:%d %s() curvnet=%p saved_vnet=%p",	\
235 	    __FILE__, __LINE__, __func__, curvnet, saved_vnet));	\
236 	curvnet = saved_vnet;
237 #endif /* VNET_DEBUG */
238 
239 #define	CURVNET_ASSERT_SET()						\
240 	VNET_ASSERT(curvnet != NULL, ("vnet is not set at %s:%d %s()",  \
241 	    __FILE__, __LINE__, __func__))
242 
243 extern struct vnet *vnet0;
244 #define	IS_DEFAULT_VNET(arg)	((arg) == vnet0)
245 
246 #define	CRED_TO_VNET(cr)	(cr)->cr_prison->pr_vnet
247 #define	TD_TO_VNET(td)		CRED_TO_VNET((td)->td_ucred)
248 #define	P_TO_VNET(p)		CRED_TO_VNET((p)->p_ucred)
249 
250 /*
251  * Global linked list of all virtual network stacks, along with read locks to
252  * access it.  If a caller may sleep while accessing the list, it must use
253  * the sleepable lock macros.
254  */
255 LIST_HEAD(vnet_list_head, vnet);
256 extern struct vnet_list_head vnet_head;
257 extern struct rwlock vnet_rwlock;
258 extern struct sx vnet_sxlock;
259 
260 #define	VNET_LIST_RLOCK()		sx_slock(&vnet_sxlock)
261 #define	VNET_LIST_RLOCK_NOSLEEP()	rw_rlock(&vnet_rwlock)
262 #define	VNET_LIST_RUNLOCK()		sx_sunlock(&vnet_sxlock)
263 #define	VNET_LIST_RUNLOCK_NOSLEEP()	rw_runlock(&vnet_rwlock)
264 
265 /*
266  * Iteration macros to walk the global list of virtual network stacks.
267  */
268 #define	VNET_ITERATOR_DECL(arg)	struct vnet *arg
269 #define	VNET_FOREACH(arg)	LIST_FOREACH((arg), &vnet_head, vnet_le)
270 
271 /*
272  * Virtual network stack memory allocator, which allows global variables to
273  * be automatically instantiated for each network stack instance.
274  */
275 #define	VNET_NAME(n)		vnet_entry_##n
276 #define	VNET_DECLARE(t, n)	extern t VNET_NAME(n)
277 /* struct _hack is to stop this from being used with static data */
278 #define	VNET_DEFINE(t, n)	\
279     struct _hack; t VNET_NAME(n) __section(VNET_SETNAME) __used
280 #if defined(KLD_MODULE) && (defined(__aarch64__) || defined(__riscv) \
281 		|| defined(__powerpc64__))
282 /*
283  * As with DPCPU_DEFINE_STATIC we are unable to mark this data as static
284  * in modules on some architectures.
285  */
286 #define	VNET_DEFINE_STATIC(t, n) \
287     t VNET_NAME(n) __section(VNET_SETNAME) __used
288 #else
289 #define	VNET_DEFINE_STATIC(t, n) \
290     static t VNET_NAME(n) __section(VNET_SETNAME) __used
291 #endif
292 #define	_VNET_PTR(b, n)		(__typeof(VNET_NAME(n))*)		\
293 				    ((b) + (uintptr_t)&VNET_NAME(n))
294 
295 #define	_VNET(b, n)		(*_VNET_PTR(b, n))
296 
297 /*
298  * Virtualized global variable accessor macros.
299  */
300 #define	VNET_VNET_PTR(vnet, n)		_VNET_PTR((vnet)->vnet_data_base, n)
301 #define	VNET_VNET(vnet, n)		(*VNET_VNET_PTR((vnet), n))
302 
303 #define	VNET_PTR(n)		VNET_VNET_PTR(curvnet, n)
304 #define	VNET(n)			VNET_VNET(curvnet, n)
305 
306 /*
307  * Virtual network stack allocator interfaces from the kernel linker.
308  */
309 void	*vnet_data_alloc(int size);
310 void	 vnet_data_copy(void *start, int size);
311 void	 vnet_data_free(void *start_arg, int size);
312 
313 /*
314  * Virtual sysinit mechanism, allowing network stack components to declare
315  * startup and shutdown methods to be run when virtual network stack
316  * instances are created and destroyed.
317  */
318 #include <sys/kernel.h>
319 
320 /*
321  * SYSINIT/SYSUNINIT variants that provide per-vnet constructors and
322  * destructors.
323  */
324 struct vnet_sysinit {
325 	enum sysinit_sub_id	subsystem;
326 	enum sysinit_elem_order	order;
327 	sysinit_cfunc_t		func;
328 	const void		*arg;
329 	TAILQ_ENTRY(vnet_sysinit) link;
330 };
331 
332 #define	VNET_SYSINIT(ident, subsystem, order, func, arg)		\
333 	CTASSERT((subsystem) > SI_SUB_VNET &&				\
334 	    (subsystem) <= SI_SUB_VNET_DONE);				\
335 	static struct vnet_sysinit ident ## _vnet_init = {		\
336 		subsystem,						\
337 		order,							\
338 		(sysinit_cfunc_t)(sysinit_nfunc_t)func,			\
339 		(arg)							\
340 	};								\
341 	SYSINIT(vnet_init_ ## ident, subsystem, order,			\
342 	    vnet_register_sysinit, &ident ## _vnet_init);		\
343 	SYSUNINIT(vnet_init_ ## ident, subsystem, order,		\
344 	    vnet_deregister_sysinit, &ident ## _vnet_init)
345 
346 #define	VNET_SYSUNINIT(ident, subsystem, order, func, arg)		\
347 	CTASSERT((subsystem) > SI_SUB_VNET &&				\
348 	    (subsystem) <= SI_SUB_VNET_DONE);				\
349 	static struct vnet_sysinit ident ## _vnet_uninit = {		\
350 		subsystem,						\
351 		order,							\
352 		(sysinit_cfunc_t)(sysinit_nfunc_t)func,			\
353 		(arg)							\
354 	};								\
355 	SYSINIT(vnet_uninit_ ## ident, subsystem, order,		\
356 	    vnet_register_sysuninit, &ident ## _vnet_uninit);		\
357 	SYSUNINIT(vnet_uninit_ ## ident, subsystem, order,		\
358 	    vnet_deregister_sysuninit, &ident ## _vnet_uninit)
359 
360 /*
361  * Interfaces for managing per-vnet constructors and destructors.
362  */
363 void	vnet_register_sysinit(void *arg);
364 void	vnet_register_sysuninit(void *arg);
365 void	vnet_deregister_sysinit(void *arg);
366 void	vnet_deregister_sysuninit(void *arg);
367 
368 /*
369  * EVENTHANDLER(9) extensions.
370  */
371 #include <sys/eventhandler.h>
372 
373 void	vnet_global_eventhandler_iterator_func(void *, ...);
374 #define VNET_GLOBAL_EVENTHANDLER_REGISTER_TAG(tag, name, func, arg, priority) \
375 do {									\
376 	if (IS_DEFAULT_VNET(curvnet)) {					\
377 		(tag) = vimage_eventhandler_register(NULL, #name, func,	\
378 		    arg, priority,					\
379 		    vnet_global_eventhandler_iterator_func);		\
380 	}								\
381 } while(0)
382 #define VNET_GLOBAL_EVENTHANDLER_REGISTER(name, func, arg, priority)	\
383 do {									\
384 	if (IS_DEFAULT_VNET(curvnet)) {					\
385 		vimage_eventhandler_register(NULL, #name, func,		\
386 		    arg, priority,					\
387 		    vnet_global_eventhandler_iterator_func);		\
388 	}								\
389 } while(0)
390 
391 #else /* !VIMAGE */
392 
393 /*
394  * Various virtual network stack macros compile to no-ops without VIMAGE.
395  */
396 #define	curvnet			NULL
397 
398 #define	VNET_ASSERT(exp, msg)
399 #define	CURVNET_SET(arg)
400 #define	CURVNET_SET_QUIET(arg)
401 #define	CURVNET_RESTORE()
402 #define	CURVNET_ASSERT_SET()						\
403 
404 #define	VNET_LIST_RLOCK()
405 #define	VNET_LIST_RLOCK_NOSLEEP()
406 #define	VNET_LIST_RUNLOCK()
407 #define	VNET_LIST_RUNLOCK_NOSLEEP()
408 #define	VNET_ITERATOR_DECL(arg)
409 #define	VNET_FOREACH(arg)
410 
411 #define	IS_DEFAULT_VNET(arg)	1
412 #define	CRED_TO_VNET(cr)	NULL
413 #define	TD_TO_VNET(td)		NULL
414 #define	P_TO_VNET(p)		NULL
415 
416 /*
417  * Versions of the VNET macros that compile to normal global variables and
418  * standard sysctl definitions.
419  */
420 #define	VNET_NAME(n)		n
421 #define	VNET_DECLARE(t, n)	extern t n
422 #define	VNET_DEFINE(t, n)	struct _hack; t n
423 #define	VNET_DEFINE_STATIC(t, n)	static t n
424 #define	_VNET_PTR(b, n)		&VNET_NAME(n)
425 
426 /*
427  * Virtualized global variable accessor macros.
428  */
429 #define	VNET_VNET_PTR(vnet, n)		(&(n))
430 #define	VNET_VNET(vnet, n)		(n)
431 
432 #define	VNET_PTR(n)		(&(n))
433 #define	VNET(n)			(n)
434 
435 /*
436  * When VIMAGE isn't compiled into the kernel, VNET_SYSINIT/VNET_SYSUNINIT
437  * map into normal sysinits, which have the same ordering properties.
438  */
439 #define	VNET_SYSINIT(ident, subsystem, order, func, arg)		\
440 	SYSINIT(ident, subsystem, order, func, arg)
441 #define	VNET_SYSUNINIT(ident, subsystem, order, func, arg)		\
442 	SYSUNINIT(ident, subsystem, order, func, arg)
443 
444 /*
445  * Without VIMAGE revert to the default implementation.
446  */
447 #define VNET_GLOBAL_EVENTHANDLER_REGISTER_TAG(tag, name, func, arg, priority) \
448 	(tag) = eventhandler_register(NULL, #name, func, arg, priority)
449 #define VNET_GLOBAL_EVENTHANDLER_REGISTER(name, func, arg, priority)	\
450 	eventhandler_register(NULL, #name, func, arg, priority)
451 #endif /* VIMAGE */
452 #endif /* _KERNEL */
453 
454 #endif /* !_NET_VNET_H_ */
455