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