xref: /xnu-11417.121.6/bsd/skywalk/core/skywalk_var.h (revision a1e26a70f38d1d7daa7b49b258e2f8538ad81650)
1 /*
2  * Copyright (c) 2015-2023 Apple Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
6  * This file contains Original Code and/or Modifications of Original Code
7  * as defined in and that are subject to the Apple Public Source License
8  * Version 2.0 (the 'License'). You may not use this file except in
9  * compliance with the License. The rights granted to you under the License
10  * may not be used to create, or enable the creation or redistribution of,
11  * unlawful or unlicensed copies of an Apple operating system, or to
12  * circumvent, violate, or enable the circumvention or violation of, any
13  * terms of an Apple operating system software license agreement.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
18  * The Original Code and all software distributed under the License are
19  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23  * Please see the License for the specific language governing rights and
24  * limitations under the License.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 
29 /*
30  * Copyright (C) 2012-2014 Matteo Landi, Luigi Rizzo, Giuseppe Lettieri.
31  * All rights reserved.
32  * Copyright (C) 2013-2014 Universita` di Pisa. All rights reserved.
33  *
34  * Redistribution and use in source and binary forms, with or without
35  * modification, are permitted provided that the following conditions
36  * are met:
37  *   1. Redistributions of source code must retain the above copyright
38  *      notice, this list of conditions and the following disclaimer.
39  *   2. Redistributions in binary form must reproduce the above copyright
40  *      notice, this list of conditions and the following disclaimer in the
41  *    documentation and/or other materials provided with the distribution.
42  *
43  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
44  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
47  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53  * SUCH DAMAGE.
54  */
55 
56 #ifndef _SKYWALK_VAR_H_
57 #define _SKYWALK_VAR_H_
58 
59 #ifdef BSD_KERNEL_PRIVATE
60 #include <stdint.h>
61 #include <sys/param.h>
62 #include <sys/systm.h>
63 #include <sys/types.h>
64 #include <sys/cdefs.h>
65 #include <sys/errno.h>
66 #include <sys/malloc.h>
67 #include <sys/mbuf.h>
68 #include <sys/protosw.h>
69 #include <sys/queue.h>
70 #include <sys/tree.h>
71 #include <sys/sysctl.h>
72 #include <sys/poll.h>
73 #include <sys/priv.h>
74 #include <sys/random.h>
75 #include <sys/kernel.h>
76 #include <sys/guarded.h>
77 #include <uuid/uuid.h>
78 #include <kern/bits.h>
79 #include <kern/locks.h>
80 #include <kern/task.h>
81 #include <kern/thread.h>
82 #include <kern/zalloc.h>
83 #include <mach/boolean.h>
84 #include <machine/atomic.h>
85 #include <machine/endian.h>
86 #include <netinet/ip.h>
87 #include <net/dlil.h>
88 #include <net/necp.h>
89 #include <libkern/libkern.h>
90 #include <libkern/OSAtomic.h>
91 #include <libkern/OSByteOrder.h>
92 #include <IOKit/skywalk/IOSkywalkSupport.h>
93 #include <skywalk/os_nexus_private.h>
94 #include <skywalk/os_channel_private.h>
95 #include <skywalk/namespace/netns.h>
96 #include <skywalk/namespace/protons.h>
97 #include <skywalk/namespace/flowidns.h>
98 #include <vm/vm_kern.h>
99 #include <san/kasan.h>
100 
101 /*
102  * General byte order swapping functions.
103  */
104 #define bswap16(x)      OSSwapInt16(x)
105 #define bswap32(x)      OSSwapInt32(x)
106 #define bswap64(x)      OSSwapInt64(x)
107 
108 /*
109  * Atomic operations.
110  */
111 #define SK_ATOMIC_TEST_AND_SET(p)       (!os_atomic_cmpxchg((p), 0, 1, acq_rel))
112 #define SK_ATOMIC_CLEAR(p)              os_atomic_store((p), 0, release)
113 
114 extern uint32_t sk_debug;
115 
116 /*
117  * feature bits defined in os_skywalk_private.h
118  */
119 extern uint64_t sk_features;
120 
121 SYSCTL_DECL(_kern_skywalk);
122 SYSCTL_DECL(_kern_skywalk_stats);
123 
124 #define SK_LOCK()                       \
125 	lck_mtx_lock(&sk_lock)
126 #define SK_LOCK_TRY()                   \
127 	lck_mtx_try_lock(&sk_lock)
128 #define SK_LOCK_ASSERT_HELD()           \
129 	LCK_MTX_ASSERT(&sk_lock, LCK_MTX_ASSERT_OWNED)
130 #define SK_LOCK_ASSERT_NOTHELD()        \
131 	LCK_MTX_ASSERT(&sk_lock, LCK_MTX_ASSERT_NOTOWNED)
132 #define SK_UNLOCK()                     \
133 	lck_mtx_unlock(&sk_lock)
134 
135 decl_lck_mtx_data(extern, sk_lock);
136 extern lck_grp_t        sk_lock_group;
137 extern lck_attr_t       sk_lock_attr;
138 
139 /*
140  * Ring Types.
141  */
142 enum txrx {
143 	NR_RX = 0,              /* RX only */
144 	NR_TX = 1,              /* TX only */
145 	NR_TXRX,                /* RX+TX (alias) */
146 	NR_A = NR_TXRX,         /* alloc only */
147 	NR_F,                   /* free only */
148 	NR_TXRXAF,              /* alloc+free (alias) */
149 	NR_EV = NR_TXRXAF,      /* event only */
150 	NR_LBA,                 /* large buf alloc */
151 	NR_ALL                  /* all of the above */
152 };
153 
154 __attribute__((always_inline))
155 static inline const char *
sk_ring2str(enum txrx t)156 sk_ring2str(enum txrx t)
157 {
158 	switch (t) {
159 	case NR_TX:
160 		return "TX";
161 	case NR_RX:
162 		return "RX";
163 	case NR_A:
164 		return "ALLOC";
165 	case NR_F:
166 		return "FREE";
167 	case NR_EV:
168 		return "EVENT";
169 	case NR_LBA:
170 		return "LARGE ALLOC";
171 	default:
172 		VERIFY(0);
173 		/* NOTREACHED */
174 		__builtin_unreachable();
175 	}
176 }
177 
178 __attribute__((always_inline))
179 static inline enum txrx
sk_txrx_swap(enum txrx t)180 sk_txrx_swap(enum txrx t)
181 {
182 	return t == NR_RX ? NR_TX : NR_RX;
183 }
184 
185 #define for_rx_tx(t)    for ((t) = 0; (t) < NR_TXRX; (t)++)
186 #define for_a_f(t)      for ((t) = NR_A; (t) <= NR_F; (t)++)
187 #define for_all_rings(t)    for ((t) = 0; (t) < NR_ALL; (t)++)
188 
189 /* return the next index, with wraparound */
190 __attribute__((always_inline))
191 static inline uint32_t
SLOT_NEXT(uint32_t i,uint32_t lim)192 SLOT_NEXT(uint32_t i, uint32_t lim)
193 {
194 	return __improbable(i == lim) ? 0 : i + 1;
195 }
196 
197 /* return the previous index, with wraparound */
198 __attribute__((always_inline))
199 static inline uint32_t
SLOT_PREV(uint32_t i,uint32_t lim)200 SLOT_PREV(uint32_t i, uint32_t lim)
201 {
202 	return __improbable(i == 0) ? lim : i - 1;
203 }
204 
205 /* return the incremented index, with wraparound */
206 static inline uint32_t
SLOT_INCREMENT(uint32_t i,uint32_t n,uint32_t lim)207 SLOT_INCREMENT(uint32_t i, uint32_t n, uint32_t lim)
208 {
209 	i += n;
210 	return __improbable(i > lim) ? i - lim - 1 : i;
211 }
212 
213 /*
214  * Nexus metadata.
215  */
216 #define NX_METADATA_QUANTUM_SZ          \
217 	(MAX(sizeof (struct __user_quantum), sizeof (struct __kern_quantum)))
218 #define NX_METADATA_PACKET_SZ(_n)       \
219 	(MAX(_USER_PACKET_SIZE(_n), _KERN_PACKET_SIZE(_n)))
220 
221 /* {min,max} internal user metadata object size */
222 #define NX_METADATA_OBJ_MIN_SZ  \
223 	(METADATA_PREAMBLE_SZ + NX_METADATA_QUANTUM_SZ)
224 #define NX_METADATA_OBJ_MAX_SZ  512
225 
226 /* {min,max} client metadata size */
227 #define NX_METADATA_USR_MIN_SZ  0
228 #define NX_METADATA_USR_MAX_SZ  \
229 	(NX_METADATA_OBJ_MAX_SZ - NX_METADATA_OBJ_MIN_SZ)
230 
231 /*
232  * User-visible statistics.
233  */
234 #define NX_STATS_MIN_SZ         0
235 #define NX_STATS_MAX_SZ         (16 * 1024)
236 
237 /*
238  * Flow advisory entries.
239  */
240 #define NX_FLOWADV_DEFAULT      512
241 #define NX_FLOWADV_MAX          (64 * 1024)
242 #define FO_FLOWADV_CHUNK        64
243 
244 /*
245  * Nexus advisory.
246  */
247 #define NX_NEXUSADV_MAX_SZ      (16 * 1024)
248 
249 /* {min,max} number of ring pairs in a nexus */
250 #define NX_MIN_NUM_RING_PAIR    1
251 #define NX_MAX_NUM_RING_PAIR    8 /* xxx unclear how many */
252 #define NX_MIN_NUM_RING         (NX_MIN_NUM_RING_PAIR * 2)
253 #define NX_MAX_NUM_RING         (NX_MAX_NUM_RING_PAIR * 2)
254 
255 #define NX_MIN_NUM_SLOT_PER_RING        2
256 #define NX_MAX_NUM_SLOT_PER_RING        (16 * 1024)
257 
258 #define NX_MIN_BUF_OBJ_SIZE     64
259 #define NX_MAX_BUF_OBJ_SIZE     (64 * 1024)
260 
261 #define NX_PBUF_FRAGS_MIN       1
262 #define NX_PBUF_FRAGS_DEFAULT   NX_PBUF_FRAGS_MIN
263 #define NX_PBUF_FRAGS_MAX       64
264 
265 #define NX_MAX_AGGR_PKT_SIZE IP_MAXPACKET /* max aggregated pkt size */
266 
267 /*
268  * Compat netif transmit models.
269  */
270 /* uses default parameters as set by driver */
271 #define NETIF_COMPAT_TXMODEL_DEFAULT            0
272 /* override driver parameters and force IFEF_ENQUEUE_MULTI */
273 #define NETIF_COMPAT_TXMODEL_ENQUEUE_MULTI      1
274 
275 /*
276  * Native netif transmit models.
277  */
278 /* uses default parameters as set by driver */
279 #define NETIF_NATIVE_TXMODEL_DEFAULT            0
280 /* override driver parameters and force IFEF_ENQUEUE_MULTI */
281 #define NETIF_NATIVE_TXMODEL_ENQUEUE_MULTI      1
282 
283 #define _timerisset(tvp)        ((tvp)->tv_sec || (tvp)->tv_nsec)
284 #define _timersub(tvp, uvp, vvp) do {                                   \
285 	        (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec;          \
286 	        (vvp)->tv_nsec = (tvp)->tv_nsec - (uvp)->tv_nsec;       \
287 	        if ((vvp)->tv_nsec < 0) {                               \
288 	                (vvp)->tv_sec--;                                \
289 	                (vvp)->tv_nsec += NSEC_PER_SEC;                 \
290 	        }                                                       \
291 } while (0)
292 #define _timernsec(tvp, nsp) do {                                       \
293 	        *(nsp) = (tvp)->tv_nsec;                                \
294 	        if ((tvp)->tv_sec > 0)                                  \
295 	                *(nsp) += ((tvp)->tv_sec * NSEC_PER_SEC);       \
296 } while (0)
297 
298 struct nexus_adapter;
299 struct kern_pbufpool;
300 
301 extern uint32_t sk_opp_defunct;
302 extern uint32_t sk_cksum_tx;
303 extern uint32_t sk_cksum_rx;
304 extern uint32_t sk_guard;
305 extern uint32_t sk_headguard_sz;
306 extern uint32_t sk_tailguard_sz;
307 
308 #if (DEVELOPMENT || DEBUG)
309 extern uint32_t sk_txring_sz;
310 extern uint32_t sk_rxring_sz;
311 extern uint32_t sk_net_txring_sz;
312 extern uint32_t sk_net_rxring_sz;
313 #endif /* !DEVELOPMENT && !DEBUG */
314 
315 extern uint32_t sk_max_flows;
316 extern uint32_t sk_fadv_nchunks;
317 extern uint32_t sk_netif_compat_txmodel;
318 extern uint32_t sk_netif_native_txmodel;
319 extern uint16_t sk_tx_delay_qlen;
320 extern uint16_t sk_tx_delay_timeout;
321 extern uint32_t sk_netif_compat_aux_cell_tx_ring_sz;
322 extern uint32_t sk_netif_compat_aux_cell_rx_ring_sz;
323 extern uint32_t sk_netif_compat_wap_tx_ring_sz;
324 extern uint32_t sk_netif_compat_wap_rx_ring_sz;
325 extern uint32_t sk_netif_compat_awdl_tx_ring_sz;
326 extern uint32_t sk_netif_compat_awdl_rx_ring_sz;
327 extern uint32_t sk_netif_compat_wif_tx_ring_sz;
328 extern uint32_t sk_netif_compat_wif_rx_ring_sz;
329 extern uint32_t sk_netif_compat_usb_eth_tx_ring_sz;
330 extern uint32_t sk_netif_compat_usb_eth_rx_ring_sz;
331 extern int sk_netif_compat_rx_mbq_limit;
332 extern char sk_ll_prefix[IFNAMSIZ];
333 extern uint32_t sk_fsw_rx_agg_tcp;
334 extern uint32_t sk_fsw_tx_agg_tcp;
335 extern uint32_t sk_fsw_gso_mtu;
336 
337 typedef enum fsw_rx_agg_tcp_host {
338 	SK_FSW_RX_AGG_TCP_HOST_OFF = 0,
339 	SK_FSW_RX_AGG_TCP_HOST_ON = 1,
340 	SK_FSW_RX_AGG_TCP_HOST_AUTO
341 } fsw_rx_agg_tcp_host_t;
342 extern uint32_t sk_fsw_rx_agg_tcp_host;
343 extern uint32_t sk_fsw_max_bufs;
344 
345 typedef enum netif_mit_cfg {
346 	SK_NETIF_MIT_FORCE_OFF = 0,     /* force mitigation OFF */
347 	SK_NETIF_MIT_FORCE_SIMPLE,      /* force mitigation ON (simple) */
348 	SK_NETIF_MIT_FORCE_ADVANCED,    /* force mitigation ON (advanced) */
349 	SK_NETIF_MIT_AUTO,              /* automatic (default) */
350 	SK_NETIF_MIT_MAX = SK_NETIF_MIT_AUTO,
351 } netif_mit_cfg_t;
352 extern uint32_t sk_netif_tx_mit;
353 extern uint32_t sk_netif_rx_mit;
354 extern uint32_t sk_rx_sync_packets;
355 extern uint32_t sk_channel_buflet_alloc;
356 extern uint32_t sk_min_pool_size;
357 extern uint32_t sk_netif_queue_stat_enable;
358 
359 struct sk_protect;
360 typedef const struct sk_protect *__single sk_protect_t;
361 
362 __attribute__((always_inline))
363 static inline boolean_t
sk_is_sync_protected(void)364 sk_is_sync_protected(void)
365 {
366 	return net_thread_is_marked(NET_THREAD_CHANNEL_SYNC) != 0;
367 }
368 
369 __attribute__((always_inline))
370 static inline sk_protect_t
sk_sync_protect(void)371 sk_sync_protect(void)
372 {
373 	return (sk_protect_t)(const void *)
374 	       net_thread_marks_push(NET_THREAD_CHANNEL_SYNC);
375 }
376 
377 
378 __attribute__((always_inline))
379 static inline boolean_t
sk_is_rx_notify_protected(void)380 sk_is_rx_notify_protected(void)
381 {
382 	return net_thread_is_marked(NET_THREAD_RX_NOTIFY) != 0;
383 }
384 
385 __attribute__((always_inline))
386 static inline sk_protect_t
sk_rx_notify_protect(void)387 sk_rx_notify_protect(void)
388 {
389 	return (sk_protect_t)(const void *)
390 	       net_thread_marks_push(NET_THREAD_RX_NOTIFY);
391 }
392 
393 __attribute__((always_inline))
394 static inline sk_protect_t
sk_tx_notify_protect(void)395 sk_tx_notify_protect(void)
396 {
397 	return (sk_protect_t)(const void *)
398 	       net_thread_marks_push(NET_THREAD_TX_NOTIFY);
399 }
400 
401 __attribute__((always_inline))
402 static inline boolean_t
sk_is_tx_notify_protected(void)403 sk_is_tx_notify_protected(void)
404 {
405 	return net_thread_is_marked(NET_THREAD_TX_NOTIFY) != 0;
406 }
407 
408 __attribute__((always_inline))
409 static inline boolean_t
sk_is_cache_update_protected(void)410 sk_is_cache_update_protected(void)
411 {
412 	return net_thread_is_marked(NET_THREAD_CACHE_UPDATE) != 0;
413 }
414 
415 __attribute__((always_inline))
416 static inline sk_protect_t
sk_cache_update_protect(void)417 sk_cache_update_protect(void)
418 {
419 	return (sk_protect_t)(const void *)
420 	       net_thread_marks_push(NET_THREAD_CACHE_UPDATE);
421 }
422 
423 __attribute__((always_inline))
424 static inline boolean_t
sk_is_region_update_protected(void)425 sk_is_region_update_protected(void)
426 {
427 	return net_thread_is_marked(NET_THREAD_REGION_UPDATE) != 0;
428 }
429 
430 __attribute__((always_inline))
431 static inline sk_protect_t
sk_region_update_protect(void)432 sk_region_update_protect(void)
433 {
434 	return (sk_protect_t)(const void *)
435 	       net_thread_marks_push(NET_THREAD_REGION_UPDATE);
436 }
437 
438 __attribute__((always_inline))
439 static inline boolean_t
sk_is_async_transmit_protected(void)440 sk_is_async_transmit_protected(void)
441 {
442 	return net_thread_is_marked(NET_THREAD_AYSYNC_TX) != 0;
443 }
444 
445 __attribute__((always_inline))
446 static inline sk_protect_t
sk_async_transmit_protect(void)447 sk_async_transmit_protect(void)
448 {
449 	return (sk_protect_t)(const void *)
450 	       net_thread_marks_push(NET_THREAD_AYSYNC_TX);
451 }
452 
453 #define sk_sync_unprotect sk_unprotect
454 #define sk_cache_update_unprotect sk_unprotect
455 #define sk_region_update_unprotect sk_unprotect
456 #define sk_tx_notify_unprotect sk_unprotect
457 #define sk_async_transmit_unprotect sk_unprotect
458 
459 __attribute__((always_inline))
460 static inline void
sk_unprotect(sk_protect_t protect)461 sk_unprotect(sk_protect_t protect)
462 {
463 	net_thread_marks_pop((net_thread_marks_t)(const void*)protect);
464 }
465 
466 
467 
468 /*
469  * For sysctls that allocate a buffer to fill then copyout at completion,
470  * set an upper bound on the size of the buffer we'll allocate.
471  */
472 #define SK_SYSCTL_ALLOC_MAX             ((size_t)(100 * 1024 * 1024))
473 
474 #if (DEVELOPMENT || DEBUG)
475 typedef void (*_null_func_t)(void);
476 #define null_func       ((_null_func_t)NULL)
477 
478 extern uint32_t sk_inject_error_rmask;
479 #define _SK_INJECT_ERROR(_ie, _en, _ev, _ec, _ej, _f, ...) do {         \
480 	if (__improbable(((_ie) & (1ULL << (_en))) != 0)) {             \
481 	        if ((random() & sk_inject_error_rmask) !=               \
482 	            sk_inject_error_rmask)                              \
483 	                break;                                          \
484 	        if ((_ej) != NULL) (*(_ej))++;                          \
485 	        SK_DF(SK_VERB_ERROR_INJECT, "injecting error %d", (_en));\
486 	        if ((_f) != NULL)                                       \
487 	                (_f)(__VA_ARGS__);                              \
488 	        (_ev) = (_ec);                                          \
489 	}                                                               \
490 } while (0)
491 #else
492 #define _SK_INJECT_ERROR(_en, _ev, _ec, _f, ...)
493 #endif /* DEVELOPMENT || DEBUG */
494 
495 __BEGIN_DECLS
496 extern int skywalk_init(void);
497 extern int skywalk_priv_check_cred(proc_t, kauth_cred_t, int);
498 extern int skywalk_priv_check_proc_cred(proc_t, int);
499 #if CONFIG_MACF
500 extern int skywalk_mac_system_check_proc_cred(proc_t, const char *);
501 #endif /* CONFIG_MACF */
502 extern int skywalk_nxctl_check_privileges(proc_t, kauth_cred_t);
503 extern boolean_t skywalk_check_platform_binary(proc_t);
504 extern boolean_t skywalk_netif_direct_allowed(const char *);
505 extern boolean_t skywalk_netif_direct_enabled(void);
506 extern void sk_gen_guard_id(boolean_t, const uuid_t, guardid_t *);
507 extern char *__counted_by(sizeof(uuid_string_t)) sk_uuid_unparse(const uuid_t, uuid_string_t);
508 #if SK_LOG
509 extern const char *__counted_by(lim) sk_dump(const char *label,
510     const void *__sized_by(len) obj, int len, int dumplen,
511     char *__counted_by(lim) dst, int lim);
512 extern const char *sk_proc_name_address(struct proc *);
513 extern int sk_proc_pid(struct proc *);
514 extern const char *sk_sa_ntop(struct sockaddr *, char *__counted_by(addr_strlen),
515     size_t addr_strlen);
516 extern const char *sk_memstatus2str(uint32_t);
517 #endif /* SK_LOG */
518 
519 extern bool sk_sa_has_addr(struct sockaddr *sa);
520 extern bool sk_sa_has_port(struct sockaddr *sa);
521 extern uint16_t sk_sa_get_port(struct sockaddr *sa);
522 
523 extern void skywalk_kill_process(struct proc *, uint64_t);
524 
525 enum skywalk_kill_reason {
526 	SKYWALK_KILL_REASON_GENERIC = 0,
527 	SKYWALK_KILL_REASON_HEAD_OOB,
528 	SKYWALK_KILL_REASON_HEAD_OOB_WRAPPED,
529 	SKYWALK_KILL_REASON_CUR_OOB,
530 	SKYWALK_KILL_REASON_CUR_OOB_WRAPPED_1,
531 	SKYWALK_KILL_REASON_CUR_OOB_WRAPPED_2,
532 	SKYWALK_KILL_REASON_TAIL_MISMATCH,
533 	SKYWALK_KILL_REASON_BASIC_SANITY,
534 	SKYWALK_KILL_REASON_UNALLOCATED_PKT,
535 	SKYWALK_KILL_REASON_SLOT_NOT_DETACHED,
536 	SKYWALK_KILL_REASON_QUM_IDX_MISMATCH,
537 	SKYWALK_KILL_REASON_SYNC_FAILED,
538 	SKYWALK_KILL_REASON_INCONSISTENT_READY_BYTES,
539 	SKYWALK_KILL_REASON_BAD_BUFLET_CHAIN,
540 	SKYWALK_KILL_REASON_INTERNALIZE_FAILED,
541 };
542 
543 #define SKYWALK_KILL_REASON_TX_SYNC             0x0000000000000000ULL
544 #define SKYWALK_KILL_REASON_EVENT_SYNC          0x1000000000000000ULL
545 #define SKYWALK_KILL_REASON_FREE_SYNC           0x2000000000000000ULL
546 #define SKYWALK_KILL_REASON_ALLOC_SYNC          0x4000000000000000ULL
547 #define SKYWALK_KILL_REASON_RX_SYNC             0x8000000000000000ULL
548 
549 /* for convenience */
550 extern const char *proc_name_address(void *p);
551 
552 /*
553  * skoid is the glue that holds the Skywalk struct model and sysctl properties
554  * together. It's supposed to be embedded in other Skywalk struct, for instance
555  * channel, nexus, etc. skoid can holds variable number of properties, which
556  * is automatically made available to the sysctl interface under the parent
557  * skoid sysctl node.
558  *
559  * The embedding struct should call skoid_create, which does the initialization
560  * and registration of the associated sysctl_oid under the parent node. All
561  * first level dynamic skoid nodes must hang under static sysctl nodes defined
562  * with traditional SYSCTL_NODE macro in linker set.
563  *     skoid_create(1st_level_skoid, skoid_SNODE(_linker_sysctl), name, kind)
564  *
565  * The fields in embedding skoid can be expressed as properties of the skoid,
566  * or separate skoid, depending on the model. If the field is of primitive
567  * types, then properties should be used. If the field is of compound types
568  * (e.g. struct), another layer of skoid might be created under the parent.
569  *
570  * To add properties to the skoid, call one of the skoid_add_* functions.
571  *     skoid_add_int(&skoid, name, flags, int_ptr)
572  * To add another skoid as child of a skoid, allocate and call skoid_create
573  * with the skoid_DNODE(parent_skoid) as parent argument.
574  *     skoid_create(2+_level_skoid, skoid_DNODE(parent_skoid), name, kind)
575  *
576  * About life cycle: the embedding struct of skoid must outlive the skoid.
577  * skoid itself store a cached name, so there is no restriction of the name
578  * buffer life cycle. Property name should be a const string or string with
579  * longer life cycle than the skoid. Most often, the skoid has a variable name
580  * reflecting the Skywalk struct name (e.g. "ms.en0", while the properties has
581  * a fixed name same as the struct member variable name.
582  *
583  * Please use caution regarding access control of skoid properties.
584  */
585 #define SKOID_SNODE(static_parent)      (&(sysctl_##static_parent##_children))
586 #define SKOID_DNODE(dynamic_parent)     (&(dynamic_parent.sko_oid_list))
587 #define SKOID_NAME_SIZE 32
588 
589 struct skoid {
590 	struct sysctl_oid_list  sko_oid_list;   /* self sko_oid & properties */
591 	struct sysctl_oid       sko_oid;        /* self sysctl oid storage */
592 	char                    sko_name[SKOID_NAME_SIZE];      /* skoid name */
593 };
594 
595 extern void skoid_init(void);
596 extern void skoid_create(struct skoid *skoid, struct sysctl_oid_list *parent,
597     const char *name, int kind);
598 extern void skoid_add_int(struct skoid *skoid, const char *name, int flags,
599     int *ptr);
600 extern void skoid_add_uint(struct skoid *skoid, const char *name, int flags,
601     unsigned int *ptr);
602 extern void skoid_add_handler(struct skoid *skoid, const char *name, int kind,
603     int (*handler)SYSCTL_HANDLER_ARGS, void *arg1, int arg2);
604 extern void skoid_destroy(struct skoid *skoid);
605 
606 /*
607  * To avoid accidentally invoking skoid procedure by `sysctl` tool, use this
608  * macro as guard, so proc is only called with a parameter, e.g.
609  *     sysctl <skoid_proc_name>=1
610  */
611 #define SKOID_PROC_CALL_GUARD do {                      \
612 	if (req->newptr == USER_ADDR_NULL)              \
613 	        return (0);                             \
614 } while (0)
615 
616 extern kern_allocation_name_t skmem_tag_oid;
617 extern kern_allocation_name_t skmem_tag_sysctl_buf;
618 
619 __END_DECLS
620 #endif /* BSD_KERNEL_PRIVATE */
621 #endif /* _SKYWALK_VAR_H_ */
622