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