xref: /xnu-8796.101.5/bsd/skywalk/core/skywalk_var.h (revision aca3beaa3dfbd42498b42c5e5ce20a938e6554e5)
1 /*
2  * Copyright (c) 2015-2022 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)       (!atomic_test_set_32((p), 0, 1))
112 #define SK_ATOMIC_CLEAR(p)              atomic_set_32((p), 0)
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_ALL                  /* all of the above */
151 };
152 
153 __attribute__((always_inline))
154 static inline const char *
sk_ring2str(enum txrx t)155 sk_ring2str(enum txrx t)
156 {
157 	switch (t) {
158 	case NR_TX:
159 		return "TX";
160 	case NR_RX:
161 		return "RX";
162 	case NR_A:
163 		return "ALLOC";
164 	case NR_F:
165 		return "FREE";
166 	case NR_EV:
167 		return "EVENT";
168 	default:
169 		VERIFY(0);
170 		/* NOTREACHED */
171 		__builtin_unreachable();
172 	}
173 }
174 
175 __attribute__((always_inline))
176 static inline enum txrx
sk_txrx_swap(enum txrx t)177 sk_txrx_swap(enum txrx t)
178 {
179 	return t == NR_RX ? NR_TX : NR_RX;
180 }
181 
182 #define for_rx_tx(t)    for ((t) = 0; (t) < NR_TXRX; (t)++)
183 #define for_a_f(t)      for ((t) = NR_A; (t) <= NR_F; (t)++)
184 #define for_all_rings(t)    for ((t) = 0; (t) < NR_ALL; (t)++)
185 
186 /* return the next index, with wraparound */
187 __attribute__((always_inline))
188 static inline uint32_t
SLOT_NEXT(uint32_t i,uint32_t lim)189 SLOT_NEXT(uint32_t i, uint32_t lim)
190 {
191 	return __improbable(i == lim) ? 0 : i + 1;
192 }
193 
194 /* return the previous index, with wraparound */
195 __attribute__((always_inline))
196 static inline uint32_t
SLOT_PREV(uint32_t i,uint32_t lim)197 SLOT_PREV(uint32_t i, uint32_t lim)
198 {
199 	return __improbable(i == 0) ? lim : i - 1;
200 }
201 
202 /* return the incremented index, with wraparound */
203 static inline uint32_t
SLOT_INCREMENT(uint32_t i,uint32_t n,uint32_t lim)204 SLOT_INCREMENT(uint32_t i, uint32_t n, uint32_t lim)
205 {
206 	i += n;
207 	return __improbable(i > lim) ? i - lim - 1 : i;
208 }
209 
210 /*
211  * Nexus metadata.
212  */
213 #define NX_METADATA_QUANTUM_SZ          \
214 	(MAX(sizeof (struct __user_quantum), sizeof (struct __kern_quantum)))
215 #define NX_METADATA_PACKET_SZ(_n)       \
216 	(MAX(_USER_PACKET_SIZE(_n), _KERN_PACKET_SIZE(_n)))
217 
218 /* {min,max} internal user metadata object size */
219 #define NX_METADATA_OBJ_MIN_SZ  \
220 	(METADATA_PREAMBLE_SZ + NX_METADATA_QUANTUM_SZ)
221 #define NX_METADATA_OBJ_MAX_SZ  512
222 
223 /* {min,max} client metadata size */
224 #define NX_METADATA_USR_MIN_SZ  0
225 #define NX_METADATA_USR_MAX_SZ  \
226 	(NX_METADATA_OBJ_MAX_SZ - NX_METADATA_OBJ_MIN_SZ)
227 
228 /*
229  * User-visible statistics.
230  */
231 #define NX_STATS_MIN_SZ         0
232 #define NX_STATS_MAX_SZ         (16 * 1024)
233 
234 /*
235  * Flow advisory entries.
236  */
237 #define NX_FLOWADV_DEFAULT      682
238 #define NX_FLOWADV_MAX          (64 * 1024)
239 #define FO_FLOWADV_CHUNK        64
240 
241 /*
242  * Nexus advisory.
243  */
244 #define NX_NEXUSADV_MAX_SZ      (16 * 1024)
245 
246 /* {min,max} number of ring pairs in a nexus */
247 #define NX_MIN_NUM_RING_PAIR    1
248 #define NX_MAX_NUM_RING_PAIR    8 /* xxx unclear how many */
249 #define NX_MIN_NUM_RING         (NX_MIN_NUM_RING_PAIR * 2)
250 #define NX_MAX_NUM_RING         (NX_MAX_NUM_RING_PAIR * 2)
251 
252 #define NX_MIN_NUM_SLOT_PER_RING        2
253 #define NX_MAX_NUM_SLOT_PER_RING        (16 * 1024)
254 
255 #define NX_MIN_BUF_OBJ_SIZE     64
256 #define NX_MAX_BUF_OBJ_SIZE     (64 * 1024)
257 
258 #define NX_PBUF_FRAGS_MIN       1
259 #define NX_PBUF_FRAGS_DEFAULT   NX_PBUF_FRAGS_MIN
260 #define NX_PBUF_FRAGS_MAX       64
261 
262 #define NX_MAX_AGGR_PKT_SIZE IP_MAXPACKET /* max aggregated pkt size */
263 
264 /*
265  * Compat netif transmit models.
266  */
267 /* uses default parameters as set by driver */
268 #define NETIF_COMPAT_TXMODEL_DEFAULT            0
269 /* override driver parameters and force IFEF_ENQUEUE_MULTI */
270 #define NETIF_COMPAT_TXMODEL_ENQUEUE_MULTI      1
271 
272 /*
273  * Native netif transmit models.
274  */
275 /* uses default parameters as set by driver */
276 #define NETIF_NATIVE_TXMODEL_DEFAULT            0
277 /* override driver parameters and force IFEF_ENQUEUE_MULTI */
278 #define NETIF_NATIVE_TXMODEL_ENQUEUE_MULTI      1
279 
280 #define _timerisset(tvp)        ((tvp)->tv_sec || (tvp)->tv_nsec)
281 #define _timersub(tvp, uvp, vvp) do {                                   \
282 	        (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec;          \
283 	        (vvp)->tv_nsec = (tvp)->tv_nsec - (uvp)->tv_nsec;       \
284 	        if ((vvp)->tv_nsec < 0) {                               \
285 	                (vvp)->tv_sec--;                                \
286 	                (vvp)->tv_nsec += NSEC_PER_SEC;                 \
287 	        }                                                       \
288 } while (0)
289 #define _timernsec(tvp, nsp) do {                                       \
290 	        *(nsp) = (tvp)->tv_nsec;                                \
291 	        if ((tvp)->tv_sec > 0)                                  \
292 	                *(nsp) += ((tvp)->tv_sec * NSEC_PER_SEC);       \
293 } while (0)
294 
295 struct nexus_adapter;
296 struct kern_pbufpool;
297 
298 extern uint32_t sk_opp_defunct;
299 extern uint32_t sk_cksum_tx;
300 extern uint32_t sk_cksum_rx;
301 extern uint32_t sk_guard;
302 extern uint32_t sk_headguard_sz;
303 extern uint32_t sk_tailguard_sz;
304 
305 #if (DEVELOPMENT || DEBUG)
306 extern uint32_t sk_txring_sz;
307 extern uint32_t sk_rxring_sz;
308 extern uint32_t sk_net_txring_sz;
309 extern uint32_t sk_net_rxring_sz;
310 #endif /* !DEVELOPMENT && !DEBUG */
311 
312 extern uint32_t sk_max_flows;
313 extern uint32_t sk_fadv_nchunks;
314 extern uint32_t sk_netif_compat_txmodel;
315 extern uint32_t sk_netif_native_txmodel;
316 extern uint16_t sk_tx_delay_qlen;
317 extern uint16_t sk_tx_delay_timeout;
318 extern uint32_t sk_netif_compat_aux_cell_tx_ring_sz;
319 extern uint32_t sk_netif_compat_aux_cell_rx_ring_sz;
320 extern uint32_t sk_netif_compat_wap_tx_ring_sz;
321 extern uint32_t sk_netif_compat_wap_rx_ring_sz;
322 extern uint32_t sk_netif_compat_awdl_tx_ring_sz;
323 extern uint32_t sk_netif_compat_awdl_rx_ring_sz;
324 extern uint32_t sk_netif_compat_wif_tx_ring_sz;
325 extern uint32_t sk_netif_compat_wif_rx_ring_sz;
326 extern uint32_t sk_netif_compat_usb_eth_tx_ring_sz;
327 extern uint32_t sk_netif_compat_usb_eth_rx_ring_sz;
328 extern int sk_netif_compat_rx_mbq_limit;
329 extern char sk_ll_prefix[IFNAMSIZ];
330 extern uint32_t sk_fsw_rx_agg_tcp;
331 extern uint32_t sk_fsw_tx_agg_tcp;
332 
333 typedef enum fsw_rx_agg_tcp_host {
334 	SK_FSW_RX_AGG_TCP_HOST_OFF = 0,
335 	SK_FSW_RX_AGG_TCP_HOST_ON = 1,
336 	SK_FSW_RX_AGG_TCP_HOST_AUTO
337 } fsw_rx_agg_tcp_host_t;
338 extern uint32_t sk_fsw_rx_agg_tcp_host;
339 extern uint32_t sk_fsw_max_bufs;
340 
341 typedef enum netif_mit_cfg {
342 	SK_NETIF_MIT_FORCE_OFF = 0,     /* force mitigation OFF */
343 	SK_NETIF_MIT_FORCE_SIMPLE,      /* force mitigation ON (simple) */
344 	SK_NETIF_MIT_FORCE_ADVANCED,    /* force mitigation ON (advanced) */
345 	SK_NETIF_MIT_AUTO,              /* automatic (default) */
346 	SK_NETIF_MIT_MAX = SK_NETIF_MIT_AUTO,
347 } netif_mit_cfg_t;
348 extern uint32_t sk_netif_tx_mit;
349 extern uint32_t sk_netif_rx_mit;
350 extern uint32_t sk_rx_sync_packets;
351 extern uint32_t sk_channel_buflet_alloc;
352 extern uint32_t sk_min_pool_size;
353 
354 struct sk_protect;
355 typedef const struct sk_protect *sk_protect_t;
356 
357 __attribute__((always_inline))
358 static inline boolean_t
sk_is_sync_protected(void)359 sk_is_sync_protected(void)
360 {
361 	return net_thread_is_marked(NET_THREAD_CHANNEL_SYNC) != 0;
362 }
363 
364 __attribute__((always_inline))
365 static inline sk_protect_t
sk_sync_protect(void)366 sk_sync_protect(void)
367 {
368 	return (sk_protect_t)(const void *)
369 	       net_thread_marks_push(NET_THREAD_CHANNEL_SYNC);
370 }
371 
372 
373 __attribute__((always_inline))
374 static inline boolean_t
sk_is_rx_notify_protected(void)375 sk_is_rx_notify_protected(void)
376 {
377 	return net_thread_is_marked(NET_THREAD_RX_NOTIFY) != 0;
378 }
379 
380 __attribute__((always_inline))
381 static inline sk_protect_t
sk_rx_notify_protect(void)382 sk_rx_notify_protect(void)
383 {
384 	return (sk_protect_t)(const void *)
385 	       net_thread_marks_push(NET_THREAD_RX_NOTIFY);
386 }
387 
388 __attribute__((always_inline))
389 static inline sk_protect_t
sk_tx_notify_protect(void)390 sk_tx_notify_protect(void)
391 {
392 	return (sk_protect_t)(const void *)
393 	       net_thread_marks_push(NET_THREAD_TX_NOTIFY);
394 }
395 
396 __attribute__((always_inline))
397 static inline boolean_t
sk_is_tx_notify_protected(void)398 sk_is_tx_notify_protected(void)
399 {
400 	return net_thread_is_marked(NET_THREAD_TX_NOTIFY) != 0;
401 }
402 
403 __attribute__((always_inline))
404 static inline boolean_t
sk_is_cache_update_protected(void)405 sk_is_cache_update_protected(void)
406 {
407 	return net_thread_is_marked(NET_THREAD_CACHE_UPDATE) != 0;
408 }
409 
410 __attribute__((always_inline))
411 static inline sk_protect_t
sk_cache_update_protect(void)412 sk_cache_update_protect(void)
413 {
414 	return (sk_protect_t)(const void *)
415 	       net_thread_marks_push(NET_THREAD_CACHE_UPDATE);
416 }
417 
418 __attribute__((always_inline))
419 static inline boolean_t
sk_is_region_update_protected(void)420 sk_is_region_update_protected(void)
421 {
422 	return net_thread_is_marked(NET_THREAD_REGION_UPDATE) != 0;
423 }
424 
425 __attribute__((always_inline))
426 static inline sk_protect_t
sk_region_update_protect(void)427 sk_region_update_protect(void)
428 {
429 	return (sk_protect_t)(const void *)
430 	       net_thread_marks_push(NET_THREAD_REGION_UPDATE);
431 }
432 
433 __attribute__((always_inline))
434 static inline boolean_t
sk_is_async_transmit_protected(void)435 sk_is_async_transmit_protected(void)
436 {
437 	return net_thread_is_marked(NET_THREAD_AYSYNC_TX) != 0;
438 }
439 
440 __attribute__((always_inline))
441 static inline sk_protect_t
sk_async_transmit_protect(void)442 sk_async_transmit_protect(void)
443 {
444 	return (sk_protect_t)(const void *)
445 	       net_thread_marks_push(NET_THREAD_AYSYNC_TX);
446 }
447 
448 #define sk_sync_unprotect sk_unprotect
449 #define sk_cache_update_unprotect sk_unprotect
450 #define sk_region_update_unprotect sk_unprotect
451 #define sk_tx_notify_unprotect sk_unprotect
452 #define sk_async_transmit_unprotect sk_unprotect
453 
454 __attribute__((always_inline))
455 static inline void
sk_unprotect(sk_protect_t protect)456 sk_unprotect(sk_protect_t protect)
457 {
458 	net_thread_marks_pop((net_thread_marks_t)(const void*)protect);
459 }
460 
461 
462 
463 /*
464  * For sysctls that allocate a buffer to fill then copyout at completion,
465  * set an upper bound on the size of the buffer we'll allocate.
466  */
467 #define SK_SYSCTL_ALLOC_MAX             ((size_t)(100 * 1024 * 1024))
468 
469 #if (DEVELOPMENT || DEBUG)
470 typedef void (*_null_func_t)(void);
471 #define null_func       ((_null_func_t)NULL)
472 
473 extern uint32_t sk_inject_error_rmask;
474 #define _SK_INJECT_ERROR(_ie, _en, _ev, _ec, _ej, _f, ...) do {         \
475 	if (__improbable(((_ie) & (1ULL << (_en))) != 0)) {             \
476 	        if ((random() & sk_inject_error_rmask) !=               \
477 	            sk_inject_error_rmask)                              \
478 	                break;                                          \
479 	        if ((_ej) != NULL) (*(_ej))++;                          \
480 	        SK_DF(SK_VERB_ERROR_INJECT, "injecting error %d", (_en));\
481 	        if ((_f) != NULL)                                       \
482 	                (_f)(__VA_ARGS__);                              \
483 	        (_ev) = (_ec);                                          \
484 	}                                                               \
485 } while (0)
486 #else
487 #define _SK_INJECT_ERROR(_en, _ev, _ec, _f, ...)
488 #endif /* DEVELOPMENT || DEBUG */
489 
490 __BEGIN_DECLS
491 extern int skywalk_init(void);
492 extern int skywalk_priv_check_cred(proc_t, kauth_cred_t, int);
493 extern int skywalk_priv_check_proc_cred(proc_t, int);
494 #if CONFIG_MACF
495 extern int skywalk_mac_system_check_proc_cred(proc_t, const char *);
496 #endif /* CONFIG_MACF */
497 extern int skywalk_nxctl_check_privileges(proc_t, kauth_cred_t);
498 extern boolean_t skywalk_check_platform_binary(proc_t);
499 extern boolean_t skywalk_netif_direct_allowed(const char *);
500 extern boolean_t skywalk_netif_direct_enabled(void);
501 extern void sk_gen_guard_id(boolean_t, const uuid_t, guardid_t *);
502 extern const char *sk_uuid_unparse(const uuid_t, uuid_string_t);
503 #if SK_LOG
504 extern const char *sk_dump(const char *, const void *, int, int,
505     char *, int);
506 extern const char *sk_proc_name_address(struct proc *);
507 extern int sk_proc_pid(struct proc *);
508 extern const char *sk_sa_ntop(struct sockaddr *, char *, size_t);
509 extern const char *sk_memstatus2str(uint32_t);
510 #endif /* SK_LOG */
511 
512 extern bool sk_sa_has_addr(struct sockaddr *sa);
513 extern bool sk_sa_has_port(struct sockaddr *sa);
514 extern uint16_t sk_sa_get_port(struct sockaddr *sa);
515 
516 extern void skywalk_kill_process(struct proc *, uint64_t);
517 
518 enum skywalk_kill_reason {
519 	SKYWALK_KILL_REASON_GENERIC = 0,
520 	SKYWALK_KILL_REASON_HEAD_OOB,
521 	SKYWALK_KILL_REASON_HEAD_OOB_WRAPPED,
522 	SKYWALK_KILL_REASON_CUR_OOB,
523 	SKYWALK_KILL_REASON_CUR_OOB_WRAPPED_1,
524 	SKYWALK_KILL_REASON_CUR_OOB_WRAPPED_2,
525 	SKYWALK_KILL_REASON_TAIL_MISMATCH,
526 	SKYWALK_KILL_REASON_BASIC_SANITY,
527 	SKYWALK_KILL_REASON_UNALLOCATED_PKT,
528 	SKYWALK_KILL_REASON_SLOT_NOT_DETACHED,
529 	SKYWALK_KILL_REASON_QUM_IDX_MISMATCH,
530 	SKYWALK_KILL_REASON_SYNC_FAILED,
531 	SKYWALK_KILL_REASON_INCONSISTENT_READY_BYTES,
532 	SKYWALK_KILL_REASON_BAD_BUFLET_CHAIN,
533 	SKYWALK_KILL_REASON_INTERNALIZE_FAILED,
534 };
535 
536 #define SKYWALK_KILL_REASON_TX_SYNC             0x0000000000000000ULL
537 #define SKYWALK_KILL_REASON_EVENT_SYNC          0x1000000000000000ULL
538 #define SKYWALK_KILL_REASON_FREE_SYNC           0x2000000000000000ULL
539 #define SKYWALK_KILL_REASON_ALLOC_SYNC          0x4000000000000000ULL
540 #define SKYWALK_KILL_REASON_RX_SYNC             0x8000000000000000ULL
541 
542 /* for convenience */
543 extern char *proc_name_address(void *p);
544 
545 /*
546  * skoid is the glue that holds the Skywalk struct model and sysctl properties
547  * together. It's supposed to be embedded in other Skywalk struct, for instance
548  * channel, nexus, etc. skoid can holds variable number of properties, which
549  * is automatically made available to the sysctl interface under the parent
550  * skoid sysctl node.
551  *
552  * The embedding struct should call skoid_create, which does the initialization
553  * and registration of the associated sysctl_oid under the parent node. All
554  * first level dynamic skoid nodes must hang under static sysctl nodes defined
555  * with traditional SYSCTL_NODE macro in linker set.
556  *     skoid_create(1st_level_skoid, skoid_SNODE(_linker_sysctl), name, kind)
557  *
558  * The fields in embedding skoid can be expressed as properties of the skoid,
559  * or separate skoid, depending on the model. If the field is of primitive
560  * types, then properties should be used. If the field is of compound types
561  * (e.g. struct), another layer of skoid might be created under the parent.
562  *
563  * To add properties to the skoid, call one of the skoid_add_* functions.
564  *     skoid_add_int(&skoid, name, flags, int_ptr)
565  * To add another skoid as child of a skoid, allocate and call skoid_create
566  * with the skoid_DNODE(parent_skoid) as parent argument.
567  *     skoid_create(2+_level_skoid, skoid_DNODE(parent_skoid), name, kind)
568  *
569  * About life cycle: the embedding struct of skoid must outlive the skoid.
570  * skoid itself store a cached name, so there is no restriction of the name
571  * buffer life cycle. Property name should be a const string or string with
572  * longer life cycle than the skoid. Most often, the skoid has a variable name
573  * reflecting the Skywalk struct name (e.g. "ms.en0", while the properties has
574  * a fixed name same as the struct member variable name.
575  *
576  * Please use caution regarding access control of skoid properties.
577  */
578 #define SKOID_SNODE(static_parent)      (&(sysctl_##static_parent##_children))
579 #define SKOID_DNODE(dynamic_parent)     (&(dynamic_parent.sko_oid_list))
580 #define SKOID_NAME_SIZE 32
581 
582 struct skoid {
583 	struct sysctl_oid_list  sko_oid_list;   /* self sko_oid & properties */
584 	struct sysctl_oid       sko_oid;        /* self sysctl oid storage */
585 	char                    sko_name[SKOID_NAME_SIZE];      /* skoid name */
586 };
587 
588 extern void skoid_init(void);
589 extern void skoid_create(struct skoid *skoid, struct sysctl_oid_list *parent,
590     const char *name, int kind);
591 extern void skoid_add_int(struct skoid *skoid, const char *name, int flags,
592     int *ptr);
593 extern void skoid_add_uint(struct skoid *skoid, const char *name, int flags,
594     unsigned int *ptr);
595 extern void skoid_add_handler(struct skoid *skoid, const char *name, int kind,
596     int (*handler)SYSCTL_HANDLER_ARGS, void *arg1, int arg2);
597 extern void skoid_destroy(struct skoid *skoid);
598 
599 /*
600  * To avoid accidentally invoking skoid procedure by `sysctl` tool, use this
601  * macro as guard, so proc is only called with a parameter, e.g.
602  *     sysctl <skoid_proc_name>=1
603  */
604 #define SKOID_PROC_CALL_GUARD do {                      \
605 	if (req->newptr == USER_ADDR_NULL)              \
606 	        return (0);                             \
607 } while (0)
608 
609 extern kern_allocation_name_t skmem_tag_oid;
610 extern kern_allocation_name_t skmem_tag_sysctl_buf;
611 
612 __END_DECLS
613 #endif /* BSD_KERNEL_PRIVATE */
614 #endif /* _SKYWALK_VAR_H_ */
615