1/*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Mike Karels at Berkeley Software Design, Inc.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35#ifndef _SYS_SYSCTL_H_
36#define _SYS_SYSCTL_H_
37
38#ifdef _KERNEL
39#include <sys/cdefs.h>
40#include <sys/queue.h>
41#include <sys/tree.h>
42#endif
43
44/*
45 * Definitions for sysctl call. The sysctl call uses a hierarchical name
46 * for objects that can be examined or modified. The name is expressed as
47 * a sequence of integers. Like a file path name, the meaning of each
48 * component depends on its place in the hierarchy. The top-level and kern
49 * identifiers are defined here, and other identifiers are defined in the
50 * respective subsystem header files.
51 *
52 * Each subsystem defined by sysctl defines a list of variables for that
53 * subsystem. Each name is either a node with further levels defined below it,
54 * or it is a leaf of some particular type given below. Each sysctl level
55 * defines a set of name/type pairs to be used by sysctl(8) in manipulating the
56 * subsystem.
57 */
58
59#define CTL_MAXNAME 24 /* largest number of components supported */
60
61#define CTLTYPE 0xf /* mask for the type */
62#define CTLTYPE_NODE 1 /* name is a node */
63#define CTLTYPE_INT 2 /* name describes an integer */
64#define CTLTYPE_STRING 3 /* name describes a string */
65#define CTLTYPE_S64 4 /* name describes a signed 64-bit number */
66#define CTLTYPE_OPAQUE 5 /* name describes a structure */
67#define CTLTYPE_STRUCT CTLTYPE_OPAQUE /* name describes a structure */
68#define CTLTYPE_UINT 6 /* name describes an unsigned integer */
69#define CTLTYPE_LONG 7 /* name describes a long */
70#define CTLTYPE_ULONG 8 /* name describes an unsigned long */
71#define CTLTYPE_U64 9 /* name describes an unsigned 64-bit number */
72#define CTLTYPE_U8 0xa /* name describes an unsigned 8-bit number */
73#define CTLTYPE_U16 0xb /* name describes an unsigned 16-bit number */
74#define CTLTYPE_S8 0xc /* name describes a signed 8-bit number */
75#define CTLTYPE_S16 0xd /* name describes a signed 16-bit number */
76#define CTLTYPE_S32 0xe /* name describes a signed 32-bit number */
77#define CTLTYPE_U32 0xf /* name describes an unsigned 32-bit number */
78
79#define CTLFLAG_RD 0x80000000 /* Allow reads of variable */
80#define CTLFLAG_WR 0x40000000 /* Allow writes to the variable */
81#define CTLFLAG_RW (CTLFLAG_RD|CTLFLAG_WR)
82#define CTLFLAG_DORMANT 0x20000000 /* This sysctl is not active yet */
83#define CTLFLAG_ANYBODY 0x10000000 /* All users can set this var */
84#define CTLFLAG_SECURE 0x08000000 /* Permit set only if securelevel<=0 */
85#define CTLFLAG_PRISON 0x04000000 /* Prisoned roots can fiddle */
86#define CTLFLAG_DYN 0x02000000 /* Dynamic oid - can be freed */
87#define CTLFLAG_SKIP 0x01000000 /* Skip this sysctl when listing */
88#define CTLMASK_SECURE 0x00F00000 /* Secure level */
89#define CTLFLAG_TUN 0x00080000 /* Default value is loaded from getenv() */
90#define CTLFLAG_RDTUN (CTLFLAG_RD|CTLFLAG_TUN)
91#define CTLFLAG_RWTUN (CTLFLAG_RW|CTLFLAG_TUN)
92#define CTLFLAG_MPSAFE 0x00040000 /* Handler is MP safe */
93#define CTLFLAG_VNET 0x00020000 /* Prisons with vnet can fiddle */
94#define CTLFLAG_DYING 0x00010000 /* Oid is being removed */
95#define CTLFLAG_CAPRD 0x00008000 /* Can be read in capability mode */
96#define CTLFLAG_CAPWR 0x00004000 /* Can be written in capability mode */
97#define CTLFLAG_STATS 0x00002000 /* Statistics, not a tuneable */
98#define CTLFLAG_NOFETCH 0x00001000 /* Don't fetch tunable from getenv() */
99#define CTLFLAG_CAPRW (CTLFLAG_CAPRD|CTLFLAG_CAPWR)
100/*
101 * This is transient flag to be used until all sysctl handlers are converted
102 * to not lock Giant.
103 * One, and only one of CTLFLAG_MPSAFE or CTLFLAG_NEEDGIANT is required
104 * for SYSCTL_PROC and SYSCTL_NODE.
105 */
106#define CTLFLAG_NEEDGIANT 0x00000800 /* Handler require Giant */
107
108/*
109 * Secure level. Note that CTLFLAG_SECURE == CTLFLAG_SECURE1.
110 *
111 * Secure when the securelevel is raised to at least N.
112 */
113#define CTLSHIFT_SECURE 20
114#define CTLFLAG_SECURE1 (CTLFLAG_SECURE | (0 << CTLSHIFT_SECURE))
115#define CTLFLAG_SECURE2 (CTLFLAG_SECURE | (1 << CTLSHIFT_SECURE))
116#define CTLFLAG_SECURE3 (CTLFLAG_SECURE | (2 << CTLSHIFT_SECURE))
117
118/*
119 * USE THIS instead of a hardwired number from the categories below
120 * to get dynamically assigned sysctl entries using the linker-set
121 * technology. This is the way nearly all new sysctl variables should
122 * be implemented.
123 * e.g. SYSCTL_INT(_parent, OID_AUTO, name, CTLFLAG_RW, &variable, 0, "");
124 */
125#define OID_AUTO (-1)
126
127/*
128 * The starting number for dynamically-assigned entries. WARNING!
129 * ALL static sysctl entries should have numbers LESS than this!
130 */
131#define CTL_AUTO_START 0x100
132
133#ifdef _KERNEL
134#include <sys/linker_set.h>
135
136#ifdef KLD_MODULE
137/* XXX allow overspecification of type in external kernel modules */
138#define SYSCTL_CT_ASSERT_MASK CTLTYPE
139#else
140#define SYSCTL_CT_ASSERT_MASK 0
141#endif
142
143#define SYSCTL_HANDLER_ARGS struct sysctl_oid *oidp, void *arg1, \
144 intmax_t arg2, struct sysctl_req *req
145
146/* definitions for sysctl_req 'lock' member */
147#define REQ_UNWIRED 1
148#define REQ_WIRED 2
149
150/* definitions for sysctl_req 'flags' member */
151#ifdef COMPAT_FREEBSD32
152#define SCTL_MASK32 1 /* 32 bit emulation */
153#endif
154
155/*
156 * This describes the access space for a sysctl request. This is needed
157 * so that we can use the interface from the kernel or from user-space.
158 */
159struct thread;
160struct sysctl_req {
161 struct thread *td; /* used for access checking */
162 int lock; /* wiring state */
163 void *oldptr;
164 size_t oldlen;
165 size_t oldidx;
166 int (*oldfunc)(struct sysctl_req *, const void *, size_t);
167 const void *newptr;
168 size_t newlen;
169 size_t newidx;
170 int (*newfunc)(struct sysctl_req *, void *, size_t);
171 size_t validlen;
172 int flags;
173};
174
175struct sysctl_oid;
176
177/* RB Tree handling */
178RB_HEAD(sysctl_oid_list, sysctl_oid);
179
180/*
181 * This describes one "oid" in the MIB tree. Potentially more nodes can
182 * be hidden behind it, expanded by the handler.
183 */
184struct sysctl_oid {
185 struct sysctl_oid_list oid_children;
186 struct sysctl_oid_list* oid_parent;
187 RB_ENTRY(sysctl_oid) oid_link;
188 /* Sort key for all siblings, and lookup key for userland */
189 int oid_number;
190 u_int oid_kind;
191 void *oid_arg1;
192 intmax_t oid_arg2;
193 /* Must be unique amongst all siblings. */
194 const char *oid_name;
195 int (*oid_handler)(SYSCTL_HANDLER_ARGS);
196 const char *oid_fmt;
197 int oid_refcnt;
198 u_int oid_running;
199 const char *oid_descr;
200 const char *oid_label;
201};
202
203static inline int
204cmp_sysctl_oid(struct sysctl_oid *a, struct sysctl_oid *b)
205{
206 if (a->oid_number > b->oid_number)
207 return (1);
208 else if (a->oid_number < b->oid_number)
209 return (-1);
210 else
211 return (0);
212}
213
214RB_PROTOTYPE(sysctl_oid_list, sysctl_oid, oid_link, cmp_sysctl_oid);
215
216#define SYSCTL_IN(r, p, l) (r->newfunc)(r, p, l)
217#define SYSCTL_OUT(r, p, l) (r->oldfunc)(r, p, l)
218#define SYSCTL_OUT_STR(r, p) (r->oldfunc)(r, p, strlen(p) + 1)
219
220int sysctl_handle_bool(SYSCTL_HANDLER_ARGS);
221int sysctl_handle_8(SYSCTL_HANDLER_ARGS);
222int sysctl_handle_16(SYSCTL_HANDLER_ARGS);
223int sysctl_handle_32(SYSCTL_HANDLER_ARGS);
224int sysctl_handle_64(SYSCTL_HANDLER_ARGS);
225int sysctl_handle_int(SYSCTL_HANDLER_ARGS);
226int sysctl_msec_to_ticks(SYSCTL_HANDLER_ARGS);
227int sysctl_handle_long(SYSCTL_HANDLER_ARGS);
228int sysctl_handle_string(SYSCTL_HANDLER_ARGS);
229int sysctl_handle_opaque(SYSCTL_HANDLER_ARGS);
230int sysctl_handle_counter_u64(SYSCTL_HANDLER_ARGS);
231int sysctl_handle_counter_u64_array(SYSCTL_HANDLER_ARGS);
232
233int sysctl_handle_uma_zone_max(SYSCTL_HANDLER_ARGS);
234int sysctl_handle_uma_zone_cur(SYSCTL_HANDLER_ARGS);
235
236int sysctl_msec_to_sbintime(SYSCTL_HANDLER_ARGS);
237int sysctl_usec_to_sbintime(SYSCTL_HANDLER_ARGS);
238int sysctl_sec_to_timeval(SYSCTL_HANDLER_ARGS);
239
240int sysctl_dpcpu_int(SYSCTL_HANDLER_ARGS);
241int sysctl_dpcpu_long(SYSCTL_HANDLER_ARGS);
242int sysctl_dpcpu_quad(SYSCTL_HANDLER_ARGS);
243
244/*
245 * These functions are used to add/remove an oid from the mib.
246 */
247void sysctl_register_oid(struct sysctl_oid *oidp);
248void sysctl_register_disabled_oid(struct sysctl_oid *oidp);
249void sysctl_enable_oid(struct sysctl_oid *oidp);
250void sysctl_unregister_oid(struct sysctl_oid *oidp);
251
252/* Declare a static oid to allow child oids to be added to it. */
253#define SYSCTL_DECL(name) \
254 extern struct sysctl_oid sysctl__##name
255
256/* Hide these in macros. */
257#define SYSCTL_CHILDREN(oid_ptr) (&(oid_ptr)->oid_children)
258#define SYSCTL_PARENT(oid_ptr) \
259 (((oid_ptr)->oid_parent != &sysctl__children) ? \
260 __containerof((oid_ptr)->oid_parent, struct sysctl_oid, \
261 oid_children) : (struct sysctl_oid *)NULL)
262#define SYSCTL_STATIC_CHILDREN(oid_name) (&sysctl__##oid_name.oid_children)
263
264/* === Structs and macros related to context handling. === */
265
266/* All dynamically created sysctls can be tracked in a context list. */
267struct sysctl_ctx_entry {
268 struct sysctl_oid *entry;
269 TAILQ_ENTRY(sysctl_ctx_entry) link;
270};
271
272TAILQ_HEAD(sysctl_ctx_list, sysctl_ctx_entry);
273
274#define SYSCTL_NODE_CHILDREN(parent, name) \
275 sysctl__##parent##_##name.oid_children
276
277#ifndef NO_SYSCTL_DESCR
278#define __DESCR(d) d
279#else
280#define __DESCR(d) ""
281#endif
282
283#ifdef notyet
284#define SYSCTL_ENFORCE_FLAGS(x) \
285 _Static_assert((((x) & CTLFLAG_MPSAFE) != 0) ^ (((x) & CTLFLAG_NEEDGIANT) != 0), \
286 "Has to be either CTLFLAG_MPSAFE or CTLFLAG_NEEDGIANT")
287#else
288#define SYSCTL_ENFORCE_FLAGS(x)
289#endif
290
291/* This macro is only for internal use */
292#define SYSCTL_OID_RAW(id, parent_child_head, nbr, name, kind, a1, a2, handler, fmt, descr, label) \
293 struct sysctl_oid id = { \
294 .oid_parent = (parent_child_head), \
295 .oid_children = RB_INITIALIZER(&id.oid_children), \
296 .oid_number = (nbr), \
297 .oid_kind = (kind), \
298 .oid_arg1 = (a1), \
299 .oid_arg2 = (a2), \
300 .oid_name = (name), \
301 .oid_handler = (handler), \
302 .oid_fmt = (fmt), \
303 .oid_descr = __DESCR(descr), \
304 .oid_label = (label), \
305 }; \
306 DATA_SET(sysctl_set, id); \
307 SYSCTL_ENFORCE_FLAGS(kind)
308
309/* This constructs a static "raw" MIB oid. */
310#define SYSCTL_OID(parent, nbr, name, kind, a1, a2, handler, fmt, descr) \
311 SYSCTL_OID_WITH_LABEL(parent, nbr, name, kind, a1, a2, \
312 handler, fmt, descr, NULL)
313
314#define SYSCTL_OID_WITH_LABEL(parent, nbr, name, kind, a1, a2, handler, fmt, descr, label) \
315 static SYSCTL_OID_RAW(sysctl__##parent##_##name, \
316 SYSCTL_CHILDREN(&sysctl__##parent), \
317 nbr, #name, kind, a1, a2, handler, fmt, descr, label)
318
319/* This constructs a global "raw" MIB oid. */
320#define SYSCTL_OID_GLOBAL(parent, nbr, name, kind, a1, a2, handler, fmt, descr, label) \
321 SYSCTL_OID_RAW(sysctl__##parent##_##name, \
322 SYSCTL_CHILDREN(&sysctl__##parent), \
323 nbr, #name, kind, a1, a2, handler, fmt, descr, label)
324
325#define SYSCTL_ADD_OID(ctx, parent, nbr, name, kind, a1, a2, handler, fmt, descr) \
326({ \
327 SYSCTL_ENFORCE_FLAGS(kind); \
328 sysctl_add_oid(ctx, parent, nbr, name, kind, a1, a2,handler, \
329 fmt, __DESCR(descr), NULL); \
330})
331
332/* This constructs a root node from which other nodes can hang. */
333#define SYSCTL_ROOT_NODE(nbr, name, access, handler, descr) \
334 SYSCTL_OID_RAW(sysctl___##name, &sysctl__children, \
335 nbr, #name, CTLTYPE_NODE|(access), NULL, 0, \
336 handler, "N", descr, NULL); \
337 CTASSERT(((access) & CTLTYPE) == 0 || \
338 ((access) & SYSCTL_CT_ASSERT_MASK) == CTLTYPE_NODE)
339
340/* This constructs a node from which other oids can hang. */
341#define SYSCTL_NODE(parent, nbr, name, access, handler, descr) \
342 SYSCTL_NODE_WITH_LABEL(parent, nbr, name, access, handler, descr, NULL)
343
344#define SYSCTL_NODE_WITH_LABEL(parent, nbr, name, access, handler, descr, label) \
345 SYSCTL_OID_GLOBAL(parent, nbr, name, CTLTYPE_NODE|(access), \
346 NULL, 0, handler, "N", descr, label); \
347 SYSCTL_ENFORCE_FLAGS(access); \
348 CTASSERT(((access) & CTLTYPE) == 0 || \
349 ((access) & SYSCTL_CT_ASSERT_MASK) == CTLTYPE_NODE)
350
351#define SYSCTL_ADD_NODE(ctx, parent, nbr, name, access, handler, descr) \
352 SYSCTL_ADD_NODE_WITH_LABEL(ctx, parent, nbr, name, access, \
353 handler, descr, NULL)
354
355#define SYSCTL_ADD_NODE_WITH_LABEL(ctx, parent, nbr, name, access, handler, descr, label) \
356({ \
357 CTASSERT(((access) & CTLTYPE) == 0 || \
358 ((access) & SYSCTL_CT_ASSERT_MASK) == CTLTYPE_NODE); \
359 SYSCTL_ENFORCE_FLAGS(access); \
360 sysctl_add_oid(ctx, parent, nbr, name, CTLTYPE_NODE|(access), \
361 NULL, 0, handler, "N", __DESCR(descr), label); \
362})
363
364#define SYSCTL_ADD_ROOT_NODE(ctx, nbr, name, access, handler, descr) \
365({ \
366 CTASSERT(((access) & CTLTYPE) == 0 || \
367 ((access) & SYSCTL_CT_ASSERT_MASK) == CTLTYPE_NODE); \
368 SYSCTL_ENFORCE_FLAGS(access); \
369 sysctl_add_oid(ctx, &sysctl__children, nbr, name, \
370 CTLTYPE_NODE|(access), \
371 NULL, 0, handler, "N", __DESCR(descr), NULL); \
372})
373
374/* Oid for a string. len can be 0 to indicate '\0' termination. */
375#define SYSCTL_STRING(parent, nbr, name, access, arg, len, descr) \
376 SYSCTL_OID(parent, nbr, name, \
377 CTLTYPE_STRING | CTLFLAG_MPSAFE | (access), \
378 arg, len, sysctl_handle_string, "A", descr); \
379 CTASSERT(((access) & CTLTYPE) == 0 || \
380 ((access) & SYSCTL_CT_ASSERT_MASK) == CTLTYPE_STRING)
381
382#define SYSCTL_ADD_STRING(ctx, parent, nbr, name, access, arg, len, descr) \
383({ \
384 char *__arg = (arg); \
385 CTASSERT(((access) & CTLTYPE) == 0 || \
386 ((access) & SYSCTL_CT_ASSERT_MASK) == CTLTYPE_STRING); \
387 sysctl_add_oid(ctx, parent, nbr, name, \
388 CTLTYPE_STRING | CTLFLAG_MPSAFE | (access), \
389 __arg, len, sysctl_handle_string, "A", __DESCR(descr), \
390 NULL); \
391})
392
393/* Oid for a constant '\0' terminated string. */
394#define SYSCTL_CONST_STRING(parent, nbr, name, access, arg, descr) \
395 SYSCTL_OID(parent, nbr, name, CTLTYPE_STRING | CTLFLAG_MPSAFE | (access),\
396 __DECONST(char *, arg), 0, sysctl_handle_string, "A", descr); \
397 CTASSERT(!((access) & CTLFLAG_WR)); \
398 CTASSERT(((access) & CTLTYPE) == 0 || \
399 ((access) & SYSCTL_CT_ASSERT_MASK) == CTLTYPE_STRING)
400
401#define SYSCTL_ADD_CONST_STRING(ctx, parent, nbr, name, access, arg, descr) \
402({ \
403 char *__arg = __DECONST(char *, arg); \
404 CTASSERT(!((access) & CTLFLAG_WR)); \
405 CTASSERT(((access) & CTLTYPE) == 0 || \
406 ((access) & SYSCTL_CT_ASSERT_MASK) == CTLTYPE_STRING); \
407 sysctl_add_oid(ctx, parent, nbr, name, CTLTYPE_STRING | \
408 CTLFLAG_MPSAFE | (access), __arg, 0, sysctl_handle_string, "A",\
409 __DESCR(descr), NULL); \
410})
411
412/* Oid for a bool. If ptr is NULL, val is returned. */
413#define SYSCTL_NULL_BOOL_PTR ((bool *)NULL)
414#define SYSCTL_BOOL(parent, nbr, name, access, ptr, val, descr) \
415 SYSCTL_OID(parent, nbr, name, \
416 CTLTYPE_U8 | CTLFLAG_MPSAFE | (access), \
417 ptr, val, sysctl_handle_bool, "CU", descr); \
418 CTASSERT(((access) & CTLTYPE) == 0 && \
419 sizeof(bool) == sizeof(*(ptr)))
420
421#define SYSCTL_ADD_BOOL(ctx, parent, nbr, name, access, ptr, val, descr) \
422({ \
423 bool *__ptr = (ptr); \
424 CTASSERT(((access) & CTLTYPE) == 0); \
425 sysctl_add_oid(ctx, parent, nbr, name, \
426 CTLTYPE_U8 | CTLFLAG_MPSAFE | (access), \
427 __ptr, val, sysctl_handle_bool, "CU", __DESCR(descr), \
428 NULL); \
429})
430
431/* Oid for a signed 8-bit int. If ptr is NULL, val is returned. */
432#define SYSCTL_NULL_S8_PTR ((int8_t *)NULL)
433#define SYSCTL_S8(parent, nbr, name, access, ptr, val, descr) \
434 SYSCTL_OID(parent, nbr, name, \
435 CTLTYPE_S8 | CTLFLAG_MPSAFE | (access), \
436 ptr, val, sysctl_handle_8, "C", descr); \
437 CTASSERT((((access) & CTLTYPE) == 0 || \
438 ((access) & SYSCTL_CT_ASSERT_MASK) == CTLTYPE_S8) && \
439 sizeof(int8_t) == sizeof(*(ptr)))
440
441#define SYSCTL_ADD_S8(ctx, parent, nbr, name, access, ptr, val, descr) \
442({ \
443 int8_t *__ptr = (ptr); \
444 CTASSERT(((access) & CTLTYPE) == 0 || \
445 ((access) & SYSCTL_CT_ASSERT_MASK) == CTLTYPE_S8); \
446 sysctl_add_oid(ctx, parent, nbr, name, \
447 CTLTYPE_S8 | CTLFLAG_MPSAFE | (access), \
448 __ptr, val, sysctl_handle_8, "C", __DESCR(descr), NULL); \
449})
450
451/* Oid for an unsigned 8-bit int. If ptr is NULL, val is returned. */
452#define SYSCTL_NULL_U8_PTR ((uint8_t *)NULL)
453#define SYSCTL_U8(parent, nbr, name, access, ptr, val, descr) \
454 SYSCTL_OID(parent, nbr, name, \
455 CTLTYPE_U8 | CTLFLAG_MPSAFE | (access), \
456 ptr, val, sysctl_handle_8, "CU", descr); \
457 CTASSERT((((access) & CTLTYPE) == 0 || \
458 ((access) & SYSCTL_CT_ASSERT_MASK) == CTLTYPE_U8) && \
459 sizeof(uint8_t) == sizeof(*(ptr)))
460
461#define SYSCTL_ADD_U8(ctx, parent, nbr, name, access, ptr, val, descr) \
462({ \
463 uint8_t *__ptr = (ptr); \
464 CTASSERT(((access) & CTLTYPE) == 0 || \
465 ((access) & SYSCTL_CT_ASSERT_MASK) == CTLTYPE_U8); \
466 sysctl_add_oid(ctx, parent, nbr, name, \
467 CTLTYPE_U8 | CTLFLAG_MPSAFE | (access), \
468 __ptr, val, sysctl_handle_8, "CU", __DESCR(descr), NULL); \
469})
470
471/* Oid for a signed 16-bit int. If ptr is NULL, val is returned. */
472#define SYSCTL_NULL_S16_PTR ((int16_t *)NULL)
473#define SYSCTL_S16(parent, nbr, name, access, ptr, val, descr) \
474 SYSCTL_OID(parent, nbr, name, \
475 CTLTYPE_S16 | CTLFLAG_MPSAFE | (access), \
476 ptr, val, sysctl_handle_16, "S", descr); \
477 CTASSERT((((access) & CTLTYPE) == 0 || \
478 ((access) & SYSCTL_CT_ASSERT_MASK) == CTLTYPE_S16) && \
479 sizeof(int16_t) == sizeof(*(ptr)))
480
481#define SYSCTL_ADD_S16(ctx, parent, nbr, name, access, ptr, val, descr) \
482({ \
483 int16_t *__ptr = (ptr); \
484 CTASSERT(((access) & CTLTYPE) == 0 || \
485 ((access) & SYSCTL_CT_ASSERT_MASK) == CTLTYPE_S16); \
486 sysctl_add_oid(ctx, parent, nbr, name, \
487 CTLTYPE_S16 | CTLFLAG_MPSAFE | (access), \
488 __ptr, val, sysctl_handle_16, "S", __DESCR(descr), NULL); \
489})
490
491/* Oid for an unsigned 16-bit int. If ptr is NULL, val is returned. */
492#define SYSCTL_NULL_U16_PTR ((uint16_t *)NULL)
493#define SYSCTL_U16(parent, nbr, name, access, ptr, val, descr) \
494 SYSCTL_OID(parent, nbr, name, \
495 CTLTYPE_U16 | CTLFLAG_MPSAFE | (access), \
496 ptr, val, sysctl_handle_16, "SU", descr); \
497 CTASSERT((((access) & CTLTYPE) == 0 || \
498 ((access) & SYSCTL_CT_ASSERT_MASK) == CTLTYPE_U16) && \
499 sizeof(uint16_t) == sizeof(*(ptr)))
500
501#define SYSCTL_ADD_U16(ctx, parent, nbr, name, access, ptr, val, descr) \
502({ \
503 uint16_t *__ptr = (ptr); \
504 CTASSERT(((access) & CTLTYPE) == 0 || \
505 ((access) & SYSCTL_CT_ASSERT_MASK) == CTLTYPE_U16); \
506 sysctl_add_oid(ctx, parent, nbr, name, \
507 CTLTYPE_U16 | CTLFLAG_MPSAFE | (access), \
508 __ptr, val, sysctl_handle_16, "SU", __DESCR(descr), NULL); \
509})
510
511/* Oid for a signed 32-bit int. If ptr is NULL, val is returned. */
512#define SYSCTL_NULL_S32_PTR ((int32_t *)NULL)
513#define SYSCTL_S32(parent, nbr, name, access, ptr, val, descr) \
514 SYSCTL_OID(parent, nbr, name, \
515 CTLTYPE_S32 | CTLFLAG_MPSAFE | (access), \
516 ptr, val, sysctl_handle_32, "I", descr); \
517 CTASSERT((((access) & CTLTYPE) == 0 || \
518 ((access) & SYSCTL_CT_ASSERT_MASK) == CTLTYPE_S32) && \
519 sizeof(int32_t) == sizeof(*(ptr)))
520
521#define SYSCTL_ADD_S32(ctx, parent, nbr, name, access, ptr, val, descr) \
522({ \
523 int32_t *__ptr = (ptr); \
524 CTASSERT(((access) & CTLTYPE) == 0 || \
525 ((access) & SYSCTL_CT_ASSERT_MASK) == CTLTYPE_S32); \
526 sysctl_add_oid(ctx, parent, nbr, name, \
527 CTLTYPE_S32 | CTLFLAG_MPSAFE | (access), \
528 __ptr, val, sysctl_handle_32, "I", __DESCR(descr), NULL); \
529})
530
531/* Oid for an unsigned 32-bit int. If ptr is NULL, val is returned. */
532#define SYSCTL_NULL_U32_PTR ((uint32_t *)NULL)
533#define SYSCTL_U32(parent, nbr, name, access, ptr, val, descr) \
534 SYSCTL_OID(parent, nbr, name, \
535 CTLTYPE_U32 | CTLFLAG_MPSAFE | (access), \
536 ptr, val, sysctl_handle_32, "IU", descr); \
537 CTASSERT((((access) & CTLTYPE) == 0 || \
538 ((access) & SYSCTL_CT_ASSERT_MASK) == CTLTYPE_U32) && \
539 sizeof(uint32_t) == sizeof(*(ptr)))
540
541#define SYSCTL_ADD_U32(ctx, parent, nbr, name, access, ptr, val, descr) \
542({ \
543 uint32_t *__ptr = (ptr); \
544 CTASSERT(((access) & CTLTYPE) == 0 || \
545 ((access) & SYSCTL_CT_ASSERT_MASK) == CTLTYPE_U32); \
546 sysctl_add_oid(ctx, parent, nbr, name, \
547 CTLTYPE_U32 | CTLFLAG_MPSAFE | (access), \
548 __ptr, val, sysctl_handle_32, "IU", __DESCR(descr), NULL); \
549})
550
551/* Oid for a signed 64-bit int. If ptr is NULL, val is returned. */
552#define SYSCTL_NULL_S64_PTR ((int64_t *)NULL)
553#define SYSCTL_S64(parent, nbr, name, access, ptr, val, descr) \
554 SYSCTL_OID(parent, nbr, name, \
555 CTLTYPE_S64 | CTLFLAG_MPSAFE | (access), \
556 ptr, val, sysctl_handle_64, "Q", descr); \
557 CTASSERT((((access) & CTLTYPE) == 0 || \
558 ((access) & SYSCTL_CT_ASSERT_MASK) == CTLTYPE_S64) && \
559 sizeof(int64_t) == sizeof(*(ptr)))
560
561#define SYSCTL_ADD_S64(ctx, parent, nbr, name, access, ptr, val, descr) \
562({ \
563 int64_t *__ptr = (ptr); \
564 CTASSERT(((access) & CTLTYPE) == 0 || \
565 ((access) & SYSCTL_CT_ASSERT_MASK) == CTLTYPE_S64); \
566 sysctl_add_oid(ctx, parent, nbr, name, \
567 CTLTYPE_S64 | CTLFLAG_MPSAFE | (access), \
568 __ptr, val, sysctl_handle_64, "Q", __DESCR(descr), NULL); \
569})
570
571/* Oid for an unsigned 64-bit int. If ptr is NULL, val is returned. */
572#define SYSCTL_NULL_U64_PTR ((uint64_t *)NULL)
573#define SYSCTL_U64(parent, nbr, name, access, ptr, val, descr) \
574 SYSCTL_OID(parent, nbr, name, \
575 CTLTYPE_U64 | CTLFLAG_MPSAFE | (access), \
576 ptr, val, sysctl_handle_64, "QU", descr); \
577 CTASSERT((((access) & CTLTYPE) == 0 || \
578 ((access) & SYSCTL_CT_ASSERT_MASK) == CTLTYPE_U64) && \
579 sizeof(uint64_t) == sizeof(*(ptr)))
580
581#define SYSCTL_ADD_U64(ctx, parent, nbr, name, access, ptr, val, descr) \
582({ \
583 uint64_t *__ptr = (ptr); \
584 CTASSERT(((access) & CTLTYPE) == 0 || \
585 ((access) & SYSCTL_CT_ASSERT_MASK) == CTLTYPE_U64); \
586 sysctl_add_oid(ctx, parent, nbr, name, \
587 CTLTYPE_U64 | CTLFLAG_MPSAFE | (access), \
588 __ptr, val, sysctl_handle_64, "QU", __DESCR(descr), NULL); \
589})
590
591/* Oid for an int. If ptr is SYSCTL_NULL_INT_PTR, val is returned. */
592#define SYSCTL_NULL_INT_PTR ((int *)NULL)
593#define SYSCTL_INT(parent, nbr, name, access, ptr, val, descr) \
594 SYSCTL_INT_WITH_LABEL(parent, nbr, name, access, ptr, val, descr, NULL)
595
596#define SYSCTL_INT_WITH_LABEL(parent, nbr, name, access, ptr, val, descr, label) \
597 SYSCTL_OID_WITH_LABEL(parent, nbr, name, \
598 CTLTYPE_INT | CTLFLAG_MPSAFE | (access), \
599 ptr, val, sysctl_handle_int, "I", descr, label); \
600 CTASSERT((((access) & CTLTYPE) == 0 || \
601 ((access) & SYSCTL_CT_ASSERT_MASK) == CTLTYPE_INT) && \
602 sizeof(int) == sizeof(*(ptr)))
603
604#define SYSCTL_ADD_INT(ctx, parent, nbr, name, access, ptr, val, descr) \
605({ \
606 int *__ptr = (ptr); \
607 CTASSERT(((access) & CTLTYPE) == 0 || \
608 ((access) & SYSCTL_CT_ASSERT_MASK) == CTLTYPE_INT); \
609 sysctl_add_oid(ctx, parent, nbr, name, \
610 CTLTYPE_INT | CTLFLAG_MPSAFE | (access), \
611 __ptr, val, sysctl_handle_int, "I", __DESCR(descr), NULL); \
612})
613
614/* Oid for an unsigned int. If ptr is NULL, val is returned. */
615#define SYSCTL_NULL_UINT_PTR ((unsigned *)NULL)
616#define SYSCTL_UINT(parent, nbr, name, access, ptr, val, descr) \
617 SYSCTL_OID(parent, nbr, name, \
618 CTLTYPE_UINT | CTLFLAG_MPSAFE | (access), \
619 ptr, val, sysctl_handle_int, "IU", descr); \
620 CTASSERT((((access) & CTLTYPE) == 0 || \
621 ((access) & SYSCTL_CT_ASSERT_MASK) == CTLTYPE_UINT) && \
622 sizeof(unsigned) == sizeof(*(ptr)))
623
624#define SYSCTL_ADD_UINT(ctx, parent, nbr, name, access, ptr, val, descr) \
625({ \
626 unsigned *__ptr = (ptr); \
627 CTASSERT(((access) & CTLTYPE) == 0 || \
628 ((access) & SYSCTL_CT_ASSERT_MASK) == CTLTYPE_UINT); \
629 sysctl_add_oid(ctx, parent, nbr, name, \
630 CTLTYPE_UINT | CTLFLAG_MPSAFE | (access), \
631 __ptr, val, sysctl_handle_int, "IU", __DESCR(descr), NULL); \
632})
633
634/* Oid for a long. The pointer must be non NULL. */
635#define SYSCTL_NULL_LONG_PTR ((long *)NULL)
636#define SYSCTL_LONG(parent, nbr, name, access, ptr, val, descr) \
637 SYSCTL_OID(parent, nbr, name, \
638 CTLTYPE_LONG | CTLFLAG_MPSAFE | (access), \
639 ptr, val, sysctl_handle_long, "L", descr); \
640 CTASSERT((((access) & CTLTYPE) == 0 || \
641 ((access) & SYSCTL_CT_ASSERT_MASK) == CTLTYPE_LONG) && \
642 sizeof(long) == sizeof(*(ptr)))
643
644#define SYSCTL_ADD_LONG(ctx, parent, nbr, name, access, ptr, descr) \
645({ \
646 long *__ptr = (ptr); \
647 CTASSERT(((access) & CTLTYPE) == 0 || \
648 ((access) & SYSCTL_CT_ASSERT_MASK) == CTLTYPE_LONG); \
649 sysctl_add_oid(ctx, parent, nbr, name, \
650 CTLTYPE_LONG | CTLFLAG_MPSAFE | (access), \
651 __ptr, 0, sysctl_handle_long, "L", __DESCR(descr), NULL); \
652})
653
654/* Oid for an unsigned long. The pointer must be non NULL. */
655#define SYSCTL_NULL_ULONG_PTR ((unsigned long *)NULL)
656#define SYSCTL_ULONG(parent, nbr, name, access, ptr, val, descr) \
657 SYSCTL_OID(parent, nbr, name, \
658 CTLTYPE_ULONG | CTLFLAG_MPSAFE | (access), \
659 ptr, val, sysctl_handle_long, "LU", descr); \
660 CTASSERT((((access) & CTLTYPE) == 0 || \
661 ((access) & SYSCTL_CT_ASSERT_MASK) == CTLTYPE_ULONG) && \
662 sizeof(unsigned long) == sizeof(*(ptr)))
663
664#define SYSCTL_ADD_ULONG(ctx, parent, nbr, name, access, ptr, descr) \
665({ \
666 unsigned long *__ptr = (ptr); \
667 CTASSERT(((access) & CTLTYPE) == 0 || \
668 ((access) & SYSCTL_CT_ASSERT_MASK) == CTLTYPE_ULONG); \
669 sysctl_add_oid(ctx, parent, nbr, name, \
670 CTLTYPE_ULONG | CTLFLAG_MPSAFE | (access), \
671 __ptr, 0, sysctl_handle_long, "LU", __DESCR(descr), NULL); \
672})
673
674/* Oid for a quad. The pointer must be non NULL. */
675#define SYSCTL_NULL_QUAD_PTR ((int64_t *)NULL)
676#define SYSCTL_QUAD(parent, nbr, name, access, ptr, val, descr) \
677 SYSCTL_OID(parent, nbr, name, \
678 CTLTYPE_S64 | CTLFLAG_MPSAFE | (access), \
679 ptr, val, sysctl_handle_64, "Q", descr); \
680 CTASSERT((((access) & CTLTYPE) == 0 || \
681 ((access) & SYSCTL_CT_ASSERT_MASK) == CTLTYPE_S64) && \
682 sizeof(int64_t) == sizeof(*(ptr)))
683
684#define SYSCTL_ADD_QUAD(ctx, parent, nbr, name, access, ptr, descr) \
685({ \
686 int64_t *__ptr = (ptr); \
687 CTASSERT(((access) & CTLTYPE) == 0 || \
688 ((access) & SYSCTL_CT_ASSERT_MASK) == CTLTYPE_S64); \
689 sysctl_add_oid(ctx, parent, nbr, name, \
690 CTLTYPE_S64 | CTLFLAG_MPSAFE | (access), \
691 __ptr, 0, sysctl_handle_64, "Q", __DESCR(descr), NULL); \
692})
693
694#define SYSCTL_NULL_UQUAD_PTR ((uint64_t *)NULL)
695#define SYSCTL_UQUAD(parent, nbr, name, access, ptr, val, descr) \
696 SYSCTL_OID(parent, nbr, name, \
697 CTLTYPE_U64 | CTLFLAG_MPSAFE | (access), \
698 ptr, val, sysctl_handle_64, "QU", descr); \
699 CTASSERT((((access) & CTLTYPE) == 0 || \
700 ((access) & SYSCTL_CT_ASSERT_MASK) == CTLTYPE_U64) && \
701 sizeof(uint64_t) == sizeof(*(ptr)))
702
703#define SYSCTL_ADD_UQUAD(ctx, parent, nbr, name, access, ptr, descr) \
704({ \
705 uint64_t *__ptr = (ptr); \
706 CTASSERT(((access) & CTLTYPE) == 0 || \
707 ((access) & SYSCTL_CT_ASSERT_MASK) == CTLTYPE_U64); \
708 sysctl_add_oid(ctx, parent, nbr, name, \
709 CTLTYPE_U64 | CTLFLAG_MPSAFE | (access), \
710 __ptr, 0, sysctl_handle_64, "QU", __DESCR(descr), NULL); \
711})
712
713/* Oid for a CPU dependent variable */
714#define SYSCTL_ADD_UAUTO(ctx, parent, nbr, name, access, ptr, descr) \
715({ \
716 struct sysctl_oid *__ret; \
717 CTASSERT((sizeof(uint64_t) == sizeof(*(ptr)) || \
718 sizeof(unsigned) == sizeof(*(ptr))) && \
719 ((access) & CTLTYPE) == 0); \
720 if (sizeof(uint64_t) == sizeof(*(ptr))) { \
721 __ret = sysctl_add_oid(ctx, parent, nbr, name, \
722 CTLTYPE_U64 | CTLFLAG_MPSAFE | (access), \
723 (ptr), 0, sysctl_handle_64, "QU", \
724 __DESCR(descr), NULL); \
725 } else { \
726 __ret = sysctl_add_oid(ctx, parent, nbr, name, \
727 CTLTYPE_UINT | CTLFLAG_MPSAFE | (access), \
728 (ptr), 0, sysctl_handle_int, "IU", \
729 __DESCR(descr), NULL); \
730 } \
731 __ret; \
732})
733
734/* Oid for a 64-bit unsigned counter(9). The pointer must be non NULL. */
735#define SYSCTL_COUNTER_U64(parent, nbr, name, access, ptr, descr) \
736 SYSCTL_OID(parent, nbr, name, \
737 CTLTYPE_U64 | CTLFLAG_MPSAFE | CTLFLAG_STATS | (access), \
738 (ptr), 0, sysctl_handle_counter_u64, "QU", descr); \
739 CTASSERT((((access) & CTLTYPE) == 0 || \
740 ((access) & SYSCTL_CT_ASSERT_MASK) == CTLTYPE_U64) && \
741 sizeof(counter_u64_t) == sizeof(*(ptr)) && \
742 sizeof(uint64_t) == sizeof(**(ptr)))
743
744#define SYSCTL_ADD_COUNTER_U64(ctx, parent, nbr, name, access, ptr, descr) \
745({ \
746 counter_u64_t *__ptr = (ptr); \
747 CTASSERT(((access) & CTLTYPE) == 0 || \
748 ((access) & SYSCTL_CT_ASSERT_MASK) == CTLTYPE_U64); \
749 sysctl_add_oid(ctx, parent, nbr, name, \
750 CTLTYPE_U64 | CTLFLAG_MPSAFE | CTLFLAG_STATS | (access), \
751 __ptr, 0, sysctl_handle_counter_u64, "QU", __DESCR(descr), \
752 NULL); \
753})
754
755/* Oid for an array of counter(9)s. The pointer and length must be non zero. */
756#define SYSCTL_COUNTER_U64_ARRAY(parent, nbr, name, access, ptr, len, descr) \
757 SYSCTL_OID(parent, nbr, name, \
758 CTLTYPE_U64 | CTLFLAG_MPSAFE | CTLFLAG_STATS | (access), \
759 (ptr), (len), sysctl_handle_counter_u64_array, "QU", descr);\
760 CTASSERT((((access) & CTLTYPE) == 0 || \
761 ((access) & SYSCTL_CT_ASSERT_MASK) == CTLTYPE_OPAQUE) && \
762 sizeof(counter_u64_t) == sizeof(*(ptr)) && \
763 sizeof(uint64_t) == sizeof(**(ptr)))
764
765#define SYSCTL_ADD_COUNTER_U64_ARRAY(ctx, parent, nbr, name, access, \
766 ptr, len, descr) \
767({ \
768 counter_u64_t *__ptr = (ptr); \
769 CTASSERT(((access) & CTLTYPE) == 0 || \
770 ((access) & SYSCTL_CT_ASSERT_MASK) == CTLTYPE_OPAQUE); \
771 sysctl_add_oid(ctx, parent, nbr, name, \
772 CTLTYPE_OPAQUE | CTLFLAG_MPSAFE | CTLFLAG_STATS | (access), \
773 __ptr, len, sysctl_handle_counter_u64_array, "S", \
774 __DESCR(descr), NULL); \
775})
776
777/* Oid for an opaque object. Specified by a pointer and a length. */
778#define SYSCTL_OPAQUE(parent, nbr, name, access, ptr, len, fmt, descr) \
779 SYSCTL_OID(parent, nbr, name, \
780 CTLTYPE_OPAQUE | CTLFLAG_MPSAFE | (access), \
781 ptr, len, sysctl_handle_opaque, fmt, descr); \
782 CTASSERT(((access) & CTLTYPE) == 0 || \
783 ((access) & SYSCTL_CT_ASSERT_MASK) == CTLTYPE_OPAQUE)
784
785#define SYSCTL_ADD_OPAQUE(ctx, parent, nbr, name, access, ptr, len, fmt, descr) \
786({ \
787 CTASSERT(((access) & CTLTYPE) == 0 || \
788 ((access) & SYSCTL_CT_ASSERT_MASK) == CTLTYPE_OPAQUE); \
789 sysctl_add_oid(ctx, parent, nbr, name, \
790 CTLTYPE_OPAQUE | CTLFLAG_MPSAFE | (access), \
791 ptr, len, sysctl_handle_opaque, fmt, __DESCR(descr), NULL); \
792})
793
794/* Oid for a struct. Specified by a pointer and a type. */
795#define SYSCTL_STRUCT(parent, nbr, name, access, ptr, type, descr) \
796 SYSCTL_OID(parent, nbr, name, \
797 CTLTYPE_OPAQUE | CTLFLAG_MPSAFE | (access), \
798 ptr, sizeof(struct type), sysctl_handle_opaque, \
799 "S," #type, descr); \
800 CTASSERT(((access) & CTLTYPE) == 0 || \
801 ((access) & SYSCTL_CT_ASSERT_MASK) == CTLTYPE_OPAQUE)
802
803#define SYSCTL_ADD_STRUCT(ctx, parent, nbr, name, access, ptr, type, descr) \
804({ \
805 CTASSERT(((access) & CTLTYPE) == 0 || \
806 ((access) & SYSCTL_CT_ASSERT_MASK) == CTLTYPE_OPAQUE); \
807 sysctl_add_oid(ctx, parent, nbr, name, \
808 CTLTYPE_OPAQUE | CTLFLAG_MPSAFE | (access), \
809 (ptr), sizeof(struct type), \
810 sysctl_handle_opaque, "S," #type, __DESCR(descr), NULL); \
811})
812
813/* Oid for a procedure. Specified by a pointer and an arg. */
814#define SYSCTL_PROC(parent, nbr, name, access, ptr, arg, handler, fmt, descr) \
815 SYSCTL_OID(parent, nbr, name, (access), \
816 ptr, arg, handler, fmt, descr); \
817 CTASSERT(((access) & CTLTYPE) != 0)
818
819#define SYSCTL_ADD_PROC(ctx, parent, nbr, name, access, ptr, arg, handler, fmt, descr) \
820({ \
821 CTASSERT(((access) & CTLTYPE) != 0); \
822 SYSCTL_ENFORCE_FLAGS(access); \
823 sysctl_add_oid(ctx, parent, nbr, name, (access), \
824 (ptr), (arg), (handler), (fmt), __DESCR(descr), NULL); \
825})
826
827/* Oid to handle limits on uma(9) zone specified by pointer. */
828#define SYSCTL_UMA_MAX(parent, nbr, name, access, ptr, descr) \
829 SYSCTL_OID(parent, nbr, name, \
830 CTLTYPE_INT | CTLFLAG_MPSAFE | (access), \
831 (ptr), 0, sysctl_handle_uma_zone_max, "I", descr); \
832 CTASSERT(((access) & CTLTYPE) == 0 || \
833 ((access) & SYSCTL_CT_ASSERT_MASK) == CTLTYPE_INT)
834
835#define SYSCTL_ADD_UMA_MAX(ctx, parent, nbr, name, access, ptr, descr) \
836({ \
837 uma_zone_t __ptr = (ptr); \
838 CTASSERT(((access) & CTLTYPE) == 0 || \
839 ((access) & SYSCTL_CT_ASSERT_MASK) == CTLTYPE_INT); \
840 sysctl_add_oid(ctx, parent, nbr, name, \
841 CTLTYPE_INT | CTLFLAG_MPSAFE | (access), \
842 __ptr, 0, sysctl_handle_uma_zone_max, "I", __DESCR(descr), \
843 NULL); \
844})
845
846/* Oid to obtain current use of uma(9) zone specified by pointer. */
847#define SYSCTL_UMA_CUR(parent, nbr, name, access, ptr, descr) \
848 SYSCTL_OID(parent, nbr, name, \
849 CTLTYPE_INT | CTLFLAG_MPSAFE | CTLFLAG_RD | (access), \
850 (ptr), 0, sysctl_handle_uma_zone_cur, "I", descr); \
851 CTASSERT(((access) & CTLTYPE) == 0 || \
852 ((access) & SYSCTL_CT_ASSERT_MASK) == CTLTYPE_INT)
853
854#define SYSCTL_ADD_UMA_CUR(ctx, parent, nbr, name, access, ptr, descr) \
855({ \
856 uma_zone_t __ptr = (ptr); \
857 CTASSERT(((access) & CTLTYPE) == 0 || \
858 ((access) & SYSCTL_CT_ASSERT_MASK) == CTLTYPE_INT); \
859 sysctl_add_oid(ctx, parent, nbr, name, \
860 CTLTYPE_INT | CTLFLAG_MPSAFE | CTLFLAG_RD | (access), \
861 __ptr, 0, sysctl_handle_uma_zone_cur, "I", __DESCR(descr), \
862 NULL); \
863})
864
865/* OID expressing a sbintime_t as microseconds */
866#define SYSCTL_SBINTIME_USEC(parent, nbr, name, access, ptr, descr) \
867 SYSCTL_OID(parent, nbr, name, \
868 CTLTYPE_S64 | CTLFLAG_MPSAFE | CTLFLAG_RD | (access), \
869 (ptr), 0, sysctl_usec_to_sbintime, "Q", descr); \
870 CTASSERT(((access) & CTLTYPE) == 0 || \
871 ((access) & SYSCTL_CT_ASSERT_MASK) == CTLTYPE_S64)
872#define SYSCTL_ADD_SBINTIME_USEC(ctx, parent, nbr, name, access, ptr, descr) \
873({ \
874 sbintime_t *__ptr = (ptr); \
875 CTASSERT(((access) & CTLTYPE) == 0 || \
876 ((access) & SYSCTL_CT_ASSERT_MASK) == CTLTYPE_S64); \
877 sysctl_add_oid(ctx, parent, nbr, name, \
878 CTLTYPE_S64 | CTLFLAG_MPSAFE | CTLFLAG_RD | (access), \
879 __ptr, 0, sysctl_usec_to_sbintime, "Q", __DESCR(descr), \
880 NULL); \
881})
882
883/* OID expressing a sbintime_t as milliseconds */
884#define SYSCTL_SBINTIME_MSEC(parent, nbr, name, access, ptr, descr) \
885 SYSCTL_OID(parent, nbr, name, \
886 CTLTYPE_S64 | CTLFLAG_MPSAFE | CTLFLAG_RD | (access), \
887 (ptr), 0, sysctl_msec_to_sbintime, "Q", descr); \
888 CTASSERT(((access) & CTLTYPE) == 0 || \
889 ((access) & SYSCTL_CT_ASSERT_MASK) == CTLTYPE_S64)
890#define SYSCTL_ADD_SBINTIME_MSEC(ctx, parent, nbr, name, access, ptr, descr) \
891({ \
892 sbintime_t *__ptr = (ptr); \
893 CTASSERT(((access) & CTLTYPE) == 0 || \
894 ((access) & SYSCTL_CT_ASSERT_MASK) == CTLTYPE_S64); \
895 sysctl_add_oid(ctx, parent, nbr, name, \
896 CTLTYPE_S64 | CTLFLAG_MPSAFE | CTLFLAG_RD | (access), \
897 __ptr, 0, sysctl_msec_to_sbintime, "Q", __DESCR(descr), \
898 NULL); \
899})
900
901/* OID expressing a struct timeval as seconds */
902#define SYSCTL_TIMEVAL_SEC(parent, nbr, name, access, ptr, descr) \
903 SYSCTL_OID(parent, nbr, name, \
904 CTLTYPE_INT | CTLFLAG_MPSAFE | CTLFLAG_RD | (access), \
905 (ptr), 0, sysctl_sec_to_timeval, "I", descr); \
906 CTASSERT(((access) & CTLTYPE) == 0 || \
907 ((access) & SYSCTL_CT_ASSERT_MASK) == CTLTYPE_INT)
908#define SYSCTL_ADD_TIMEVAL_SEC(ctx, parent, nbr, name, access, ptr, descr) \
909({ \
910 struct timeval *__ptr = (ptr); \
911 CTASSERT(((access) & CTLTYPE) == 0 || \
912 ((access) & SYSCTL_CT_ASSERT_MASK) == CTLTYPE_INT); \
913 sysctl_add_oid(ctx, parent, nbr, name, \
914 CTLTYPE_INT | CTLFLAG_MPSAFE | CTLFLAG_RD | (access), \
915 __ptr, 0, sysctl_sec_to_timeval, "I", __DESCR(descr), \
916 NULL); \
917})
918
919#define SYSCTL_FOREACH(oidp, list) \
920 RB_FOREACH(oidp, sysctl_oid_list, list)
921
922/*
923 * A macro to generate a read-only sysctl to indicate the presence of optional
924 * kernel features.
925 */
926#define FEATURE(name, desc) \
927 SYSCTL_INT_WITH_LABEL(_kern_features, OID_AUTO, name, \
928 CTLFLAG_RD | CTLFLAG_CAPRD, SYSCTL_NULL_INT_PTR, 1, desc, "feature")
929/* Same for the dynamic registration. */
930#define FEATURE_ADD(name, desc) \
931 sysctl_add_oid(NULL, SYSCTL_CHILDREN(&sysctl___kern_features), \
932 OID_AUTO, name, \
933 CTLFLAG_RD | CTLFLAG_CAPRD | CTLTYPE_INT | CTLFLAG_MPSAFE, \
934 NULL, 1, sysctl_handle_int, "I", desc, "feature");
935
936/*
937 * Adding new leaves to the 'debug.sizeof' MIB tree for ad-hoc reasons is
938 * discouraged, and in particular for reporting to developers the size of some
939 * kernel structures, which can be obtained by the following alternative means:
940 * 1. In GDB, load a full kernel image and use 'print(sizeof(struct XXX))'.
941 * Alternatively, use 'ptype/o struct XXX' to additionally get the offsets
942 * and size of all structure's fields.
943 * 2. If the structure is allocated from UMA, then 'vmstat -z' reports its size
944 * (the mapping between structure types and zones is usually
945 * straightforward).
946 */
947/* Generates a read-only sysctl reporting the size of an object/structure. */
948#define SYSCTL_SIZEOF(name, expr) \
949 SYSCTL_INT(_debug_sizeof, OID_AUTO, name, CTLFLAG_RD, \
950 SYSCTL_NULL_INT_PTR, sizeof(expr), \
951 "sizeof(" __STRING(expr) ")");
952/* Same, specialized for structures. */
953#define SYSCTL_SIZEOF_STRUCT(struct_name) \
954 SYSCTL_SIZEOF(struct_name, struct struct_name)
955
956#endif /* _KERNEL */
957
958/*
959 * Top-level identifiers
960 */
961#define CTL_SYSCTL 0 /* "magic" numbers */
962#define CTL_KERN 1 /* "high kernel": proc, limits */
963#define CTL_VM 2 /* virtual memory */
964#define CTL_VFS 3 /* filesystem, mount type is next */
965#define CTL_NET 4 /* network, see socket.h */
966#define CTL_DEBUG 5 /* debugging parameters */
967#define CTL_HW 6 /* generic cpu/io */
968#define CTL_MACHDEP 7 /* machine dependent */
969#define CTL_USER 8 /* user-level */
970#define CTL_P1003_1B 9 /* POSIX 1003.1B */
971
972/*
973 * CTL_SYSCTL identifiers
974 */
975#define CTL_SYSCTL_DEBUG 0 /* printf all nodes */
976#define CTL_SYSCTL_NAME 1 /* string name of OID */
977#define CTL_SYSCTL_NEXT 2 /* next OID, honoring CTLFLAG_SKIP */
978#define CTL_SYSCTL_NAME2OID 3 /* int array of name */
979#define CTL_SYSCTL_OIDFMT 4 /* OID's kind and format */
980#define CTL_SYSCTL_OIDDESCR 5 /* OID's description */
981#define CTL_SYSCTL_OIDLABEL 6 /* aggregation label */
982#define CTL_SYSCTL_NEXTNOSKIP 7 /* next OID, ignoring CTLFLAG_SKIP */
983
984/*
985 * CTL_KERN identifiers
986 */
987#define KERN_OSTYPE 1 /* string: system version */
988#define KERN_OSRELEASE 2 /* string: system release */
989#define KERN_OSREV 3 /* int: system revision */
990#define KERN_VERSION 4 /* string: compile time info */
991#define KERN_MAXVNODES 5 /* int: max vnodes */
992#define KERN_MAXPROC 6 /* int: max processes */
993#define KERN_MAXFILES 7 /* int: max open files */
994#define KERN_ARGMAX 8 /* int: max arguments to exec */
995#define KERN_SECURELVL 9 /* int: system security level */
996#define KERN_HOSTNAME 10 /* string: hostname */
997#define KERN_HOSTID 11 /* int: host identifier */
998#define KERN_CLOCKRATE 12 /* struct: struct clockrate */
999/* was: #define KERN_VNODE 13 ; disabled in 2003 and removed in 2023 */
1000#define KERN_PROC 14 /* struct: process entries */
1001#define KERN_FILE 15 /* struct: file entries */
1002#define KERN_PROF 16 /* node: kernel profiling info */
1003#define KERN_POSIX1 17 /* int: POSIX.1 version */
1004#define KERN_NGROUPS 18 /* int: # of supplemental group ids */
1005#define KERN_JOB_CONTROL 19 /* int: is job control available */
1006#define KERN_SAVED_IDS 20 /* int: saved set-user/group-ID */
1007#define KERN_BOOTTIME 21 /* struct: time kernel was booted */
1008#define KERN_NISDOMAINNAME 22 /* string: YP domain name */
1009#define KERN_UPDATEINTERVAL 23 /* int: update process sleep time */
1010#define KERN_OSRELDATE 24 /* int: kernel release date */
1011#define KERN_NTP_PLL 25 /* node: NTP PLL control */
1012#define KERN_BOOTFILE 26 /* string: name of booted kernel */
1013#define KERN_MAXFILESPERPROC 27 /* int: max open files per proc */
1014#define KERN_MAXPROCPERUID 28 /* int: max processes per uid */
1015#define KERN_DUMPDEV 29 /* struct cdev *: device to dump on */
1016#define KERN_IPC 30 /* node: anything related to IPC */
1017#define KERN_DUMMY 31 /* unused */
1018#define KERN_PS_STRINGS 32 /* int: address of PS_STRINGS */
1019#define KERN_USRSTACK 33 /* int: address of USRSTACK */
1020#define KERN_LOGSIGEXIT 34 /* int: do we log sigexit procs? */
1021#define KERN_IOV_MAX 35 /* int: value of UIO_MAXIOV */
1022#define KERN_HOSTUUID 36 /* string: host UUID identifier */
1023#define KERN_ARND 37 /* int: from arc4rand() */
1024#define KERN_MAXPHYS 38 /* int: MAXPHYS value */
1025#define KERN_LOCKF 39 /* struct: lockf reports */
1026/*
1027 * KERN_PROC subtypes
1028 */
1029#define KERN_PROC_ALL 0 /* everything */
1030#define KERN_PROC_PID 1 /* by process id */
1031#define KERN_PROC_PGRP 2 /* by process group id */
1032#define KERN_PROC_SESSION 3 /* by session of pid */
1033#define KERN_PROC_TTY 4 /* by controlling tty */
1034#define KERN_PROC_UID 5 /* by effective uid */
1035#define KERN_PROC_RUID 6 /* by real uid */
1036#define KERN_PROC_ARGS 7 /* get/set arguments/proctitle */
1037#define KERN_PROC_PROC 8 /* only return procs */
1038#define KERN_PROC_SV_NAME 9 /* get syscall vector name */
1039#define KERN_PROC_RGID 10 /* by real group id */
1040#define KERN_PROC_GID 11 /* by effective group id */
1041#define KERN_PROC_PATHNAME 12 /* path to executable */
1042#define KERN_PROC_OVMMAP 13 /* Old VM map entries for process */
1043#define KERN_PROC_OFILEDESC 14 /* Old file descriptors for process */
1044#define KERN_PROC_KSTACK 15 /* Kernel stacks for process */
1045#define KERN_PROC_INC_THREAD 0x10 /*
1046 * modifier for pid, pgrp, tty,
1047 * uid, ruid, gid, rgid and proc
1048 * This effectively uses 16-31
1049 */
1050#define KERN_PROC_VMMAP 32 /* VM map entries for process */
1051#define KERN_PROC_FILEDESC 33 /* File descriptors for process */
1052#define KERN_PROC_GROUPS 34 /* process groups */
1053#define KERN_PROC_ENV 35 /* get environment */
1054#define KERN_PROC_AUXV 36 /* get ELF auxiliary vector */
1055#define KERN_PROC_RLIMIT 37 /* process resource limits */
1056#define KERN_PROC_PS_STRINGS 38 /* get ps_strings location */
1057#define KERN_PROC_UMASK 39 /* process umask */
1058#define KERN_PROC_OSREL 40 /* osreldate for process binary */
1059#define KERN_PROC_SIGTRAMP 41 /* signal trampoline location */
1060#define KERN_PROC_CWD 42 /* process current working directory */
1061#define KERN_PROC_NFDS 43 /* number of open file descriptors */
1062#define KERN_PROC_SIGFASTBLK 44 /* address of fastsigblk magic word */
1063#define KERN_PROC_VM_LAYOUT 45 /* virtual address space layout info */
1064#define KERN_PROC_RLIMIT_USAGE 46 /* array of rlim_t */
1065#define KERN_PROC_KQUEUE 47 /* array of struct kinfo_knote */
1066
1067/*
1068 * KERN_IPC identifiers
1069 */
1070#define KIPC_MAXSOCKBUF 1 /* int: max size of a socket buffer */
1071#define KIPC_SOCKBUF_WASTE 2 /* int: wastage factor in sockbuf */
1072#define KIPC_SOMAXCONN 3 /* int: max length of connection q */
1073#define KIPC_MAX_LINKHDR 4 /* int: max length of link header */
1074#define KIPC_MAX_PROTOHDR 5 /* int: max length of network header */
1075#define KIPC_MAX_HDR 6 /* int: max total length of headers */
1076#define KIPC_MAX_DATALEN 7 /* int: max length of data? */
1077
1078/*
1079 * CTL_HW identifiers
1080 */
1081#define HW_MACHINE 1 /* string: machine class */
1082#define HW_MODEL 2 /* string: specific machine model */
1083#define HW_NCPU 3 /* int: number of cpus */
1084#define HW_BYTEORDER 4 /* int: machine byte order */
1085#define HW_PHYSMEM 5 /* int: total memory */
1086#define HW_USERMEM 6 /* int: non-kernel memory */
1087#define HW_PAGESIZE 7 /* int: software page size */
1088#define HW_DISKNAMES 8 /* strings: disk drive names */
1089#define HW_DISKSTATS 9 /* struct: diskstats[] */
1090#define HW_FLOATINGPT 10 /* int: has HW floating point? */
1091#define HW_MACHINE_ARCH 11 /* string: machine architecture */
1092#define HW_REALMEM 12 /* int: 'real' memory */
1093
1094/*
1095 * CTL_USER definitions
1096 */
1097#define USER_CS_PATH 1 /* string: _CS_PATH */
1098#define USER_BC_BASE_MAX 2 /* int: BC_BASE_MAX */
1099#define USER_BC_DIM_MAX 3 /* int: BC_DIM_MAX */
1100#define USER_BC_SCALE_MAX 4 /* int: BC_SCALE_MAX */
1101#define USER_BC_STRING_MAX 5 /* int: BC_STRING_MAX */
1102#define USER_COLL_WEIGHTS_MAX 6 /* int: COLL_WEIGHTS_MAX */
1103#define USER_EXPR_NEST_MAX 7 /* int: EXPR_NEST_MAX */
1104#define USER_LINE_MAX 8 /* int: LINE_MAX */
1105#define USER_RE_DUP_MAX 9 /* int: RE_DUP_MAX */
1106#define USER_POSIX2_VERSION 10 /* int: POSIX2_VERSION */
1107#define USER_POSIX2_C_BIND 11 /* int: POSIX2_C_BIND */
1108#define USER_POSIX2_C_DEV 12 /* int: POSIX2_C_DEV */
1109#define USER_POSIX2_CHAR_TERM 13 /* int: POSIX2_CHAR_TERM */
1110#define USER_POSIX2_FORT_DEV 14 /* int: POSIX2_FORT_DEV */
1111#define USER_POSIX2_FORT_RUN 15 /* int: POSIX2_FORT_RUN */
1112#define USER_POSIX2_LOCALEDEF 16 /* int: POSIX2_LOCALEDEF */
1113#define USER_POSIX2_SW_DEV 17 /* int: POSIX2_SW_DEV */
1114#define USER_POSIX2_UPE 18 /* int: POSIX2_UPE */
1115#define USER_STREAM_MAX 19 /* int: POSIX2_STREAM_MAX */
1116#define USER_TZNAME_MAX 20 /* int: POSIX2_TZNAME_MAX */
1117#define USER_LOCALBASE 21 /* string: _PATH_LOCALBASE */
1118
1119#define CTL_P1003_1B_ASYNCHRONOUS_IO 1 /* boolean */
1120#define CTL_P1003_1B_MAPPED_FILES 2 /* boolean */
1121#define CTL_P1003_1B_MEMLOCK 3 /* boolean */
1122#define CTL_P1003_1B_MEMLOCK_RANGE 4 /* boolean */
1123#define CTL_P1003_1B_MEMORY_PROTECTION 5 /* boolean */
1124#define CTL_P1003_1B_MESSAGE_PASSING 6 /* boolean */
1125#define CTL_P1003_1B_PRIORITIZED_IO 7 /* boolean */
1126#define CTL_P1003_1B_PRIORITY_SCHEDULING 8 /* boolean */
1127#define CTL_P1003_1B_REALTIME_SIGNALS 9 /* boolean */
1128#define CTL_P1003_1B_SEMAPHORES 10 /* boolean */
1129#define CTL_P1003_1B_FSYNC 11 /* boolean */
1130#define CTL_P1003_1B_SHARED_MEMORY_OBJECTS 12 /* boolean */
1131#define CTL_P1003_1B_SYNCHRONIZED_IO 13 /* boolean */
1132#define CTL_P1003_1B_TIMERS 14 /* boolean */
1133#define CTL_P1003_1B_AIO_LISTIO_MAX 15 /* int */
1134#define CTL_P1003_1B_AIO_MAX 16 /* int */
1135#define CTL_P1003_1B_AIO_PRIO_DELTA_MAX 17 /* int */
1136#define CTL_P1003_1B_DELAYTIMER_MAX 18 /* int */
1137#define CTL_P1003_1B_MQ_OPEN_MAX 19 /* int */
1138#define CTL_P1003_1B_PAGESIZE 20 /* int */
1139#define CTL_P1003_1B_RTSIG_MAX 21 /* int */
1140#define CTL_P1003_1B_SEM_NSEMS_MAX 22 /* int */
1141#define CTL_P1003_1B_SEM_VALUE_MAX 23 /* int */
1142#define CTL_P1003_1B_SIGQUEUE_MAX 24 /* int */
1143#define CTL_P1003_1B_TIMER_MAX 25 /* int */
1144
1145#ifdef _KERNEL
1146
1147#define CTL_P1003_1B_MAXID 26
1148
1149/*
1150 * Declare some common oids.
1151 */
1152extern struct sysctl_oid_list sysctl__children;
1153SYSCTL_DECL(_kern);
1154SYSCTL_DECL(_kern_features);
1155SYSCTL_DECL(_kern_ipc);
1156SYSCTL_DECL(_kern_proc);
1157SYSCTL_DECL(_kern_sched);
1158SYSCTL_DECL(_kern_sched_stats);
1159SYSCTL_DECL(_sysctl);
1160SYSCTL_DECL(_vm);
1161SYSCTL_DECL(_vm_stats);
1162SYSCTL_DECL(_vm_stats_misc);
1163SYSCTL_DECL(_vfs);
1164SYSCTL_DECL(_net);
1165SYSCTL_DECL(_debug);
1166SYSCTL_DECL(_debug_sizeof);
1167SYSCTL_DECL(_dev);
1168SYSCTL_DECL(_hw);
1169SYSCTL_DECL(_hw_bus);
1170SYSCTL_DECL(_hw_bus_devices);
1171SYSCTL_DECL(_machdep);
1172SYSCTL_DECL(_machdep_mitigations);
1173SYSCTL_DECL(_user);
1174SYSCTL_DECL(_compat);
1175SYSCTL_DECL(_regression);
1176SYSCTL_DECL(_security);
1177SYSCTL_DECL(_security_bsd);
1178
1179extern const char machine[];
1180extern const char osrelease[];
1181extern const char ostype[];
1182extern const char kern_ident[];
1183
1184/* Dynamic oid handling */
1185struct sysctl_oid *sysctl_add_oid(struct sysctl_ctx_list *clist,
1186 struct sysctl_oid_list *parent, int nbr, const char *name, int kind,
1187 void *arg1, intmax_t arg2, int (*handler)(SYSCTL_HANDLER_ARGS),
1188 const char *fmt, const char *descr, const char *label);
1189int sysctl_remove_name(struct sysctl_oid *parent, const char *name, int del,
1190 int recurse);
1191void sysctl_rename_oid(struct sysctl_oid *oidp, const char *name);
1192int sysctl_move_oid(struct sysctl_oid *oidp,
1193 struct sysctl_oid_list *parent);
1194int sysctl_remove_oid(struct sysctl_oid *oidp, int del, int recurse);
1195int sysctl_ctx_init(struct sysctl_ctx_list *clist);
1196int sysctl_ctx_free(struct sysctl_ctx_list *clist);
1197struct sysctl_ctx_entry *sysctl_ctx_entry_add(struct sysctl_ctx_list *clist,
1198 struct sysctl_oid *oidp);
1199struct sysctl_ctx_entry *sysctl_ctx_entry_find(struct sysctl_ctx_list *clist,
1200 struct sysctl_oid *oidp);
1201int sysctl_ctx_entry_del(struct sysctl_ctx_list *clist,
1202 struct sysctl_oid *oidp);
1203
1204int kernel_sysctl(struct thread *td, int *name, u_int namelen, void *old,
1205 size_t *oldlenp, void *new, size_t newlen, size_t *retval,
1206 int flags);
1207int kernel_sysctlbyname(struct thread *td, char *name, void *old,
1208 size_t *oldlenp, void *new, size_t newlen, size_t *retval,
1209 int flags);
1210int userland_sysctl(struct thread *td, int *name, u_int namelen, void *old,
1211 size_t *oldlenp, int inkernel, const void *new, size_t newlen,
1212 size_t *retval, int flags);
1213int sysctl_find_oid(int *name, u_int namelen, struct sysctl_oid **noid,
1214 int *nindx, struct sysctl_req *req);
1215void sysctl_wlock(void);
1216void sysctl_wunlock(void);
1217int sysctl_wire_old_buffer(struct sysctl_req *req, size_t len);
1218int kern___sysctlbyname(struct thread *td, const char *name,
1219 size_t namelen, void *old, size_t *oldlenp, void *new,
1220 size_t newlen, size_t *retval, int flags, bool inkernel);
1221
1222struct sbuf;
1223struct sbuf *sbuf_new_for_sysctl(struct sbuf *, char *, int,
1224 struct sysctl_req *);
1225#else /* !_KERNEL */
1226#include <sys/cdefs.h>
1227#include <sys/_types.h>
1228#ifndef _SIZE_T_DECLARED
1229typedef __size_t size_t;
1230#define _SIZE_T_DECLARED
1231#endif
1232
1233__BEGIN_DECLS
1234int sysctl(const int *, unsigned int, void *, size_t *, const void *, size_t);
1235int sysctlbyname(const char *, void *, size_t *, const void *, size_t);
1236int sysctlnametomib(const char *, int *, size_t *);
1237__END_DECLS
1238#endif /* _KERNEL */
1239
1240#endif /* !_SYS_SYSCTL_H_ */