1/*-
2 * Copyright (c) 1989, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Mike Karels at Berkeley Software Design, Inc.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 *    may be used to endorse or promote products derived from this software
18 *    without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 *	@(#)sysctl.h	8.1 (Berkeley) 6/2/93
33 * $FreeBSD: stable/10/sys/sys/sysctl.h 324749 2017-10-19 08:00:34Z avg $
34 */
35
36#ifndef _SYS_SYSCTL_H_
37#define	_SYS_SYSCTL_H_
38
39#include <sys/queue.h>
40
41struct thread;
42/*
43 * Definitions for sysctl call.  The sysctl call uses a hierarchical name
44 * for objects that can be examined or modified.  The name is expressed as
45 * a sequence of integers.  Like a file path name, the meaning of each
46 * component depends on its place in the hierarchy.  The top-level and kern
47 * identifiers are defined here, and other identifiers are defined in the
48 * respective subsystem header files.
49 */
50
51#define	CTL_MAXNAME	24	/* largest number of components supported */
52
53/*
54 * Each subsystem defined by sysctl defines a list of variables
55 * for that subsystem. Each name is either a node with further
56 * levels defined below it, or it is a leaf of some particular
57 * type given below. Each sysctl level defines a set of name/type
58 * pairs to be used by sysctl(8) in manipulating the subsystem.
59 */
60struct ctlname {
61	char	*ctl_name;	/* subsystem name */
62	int	 ctl_type;	/* type of name */
63};
64
65#define	CTLTYPE		0xf	/* mask for the type */
66#define	CTLTYPE_NODE	1	/* name is a node */
67#define	CTLTYPE_INT	2	/* name describes an integer */
68#define	CTLTYPE_STRING	3	/* name describes a string */
69#define	CTLTYPE_S64	4	/* name describes a signed 64-bit number */
70#define	CTLTYPE_OPAQUE	5	/* name describes a structure */
71#define	CTLTYPE_STRUCT	CTLTYPE_OPAQUE	/* name describes a structure */
72#define	CTLTYPE_UINT	6	/* name describes an unsigned integer */
73#define	CTLTYPE_LONG	7	/* name describes a long */
74#define	CTLTYPE_ULONG	8	/* name describes an unsigned long */
75#define	CTLTYPE_U64	9	/* name describes an unsigned 64-bit number */
76
77#define	CTLFLAG_RD	0x80000000	/* Allow reads of variable */
78#define	CTLFLAG_WR	0x40000000	/* Allow writes to the variable */
79#define	CTLFLAG_RW	(CTLFLAG_RD|CTLFLAG_WR)
80#define	CTLFLAG_DORMANT	0x20000000	/* This sysctl is not active yet */
81#define	CTLFLAG_ANYBODY	0x10000000	/* All users can set this var */
82#define	CTLFLAG_SECURE	0x08000000	/* Permit set only if securelevel<=0 */
83#define	CTLFLAG_PRISON	0x04000000	/* Prisoned roots can fiddle */
84#define	CTLFLAG_DYN	0x02000000	/* Dynamic oid - can be freed */
85#define	CTLFLAG_SKIP	0x01000000	/* Skip this sysctl when listing */
86#define	CTLMASK_SECURE	0x00F00000	/* Secure level */
87#define	CTLFLAG_TUN	0x00080000	/* Tunable variable */
88#define	CTLFLAG_RDTUN	(CTLFLAG_RD|CTLFLAG_TUN)
89#define	CTLFLAG_RWTUN	(CTLFLAG_RW|CTLFLAG_TUN)
90#define	CTLFLAG_MPSAFE	0x00040000	/* Handler is MP safe */
91#define	CTLFLAG_VNET	0x00020000	/* Prisons with vnet can fiddle */
92#define	CTLFLAG_DYING	0x00010000	/* Oid is being removed */
93#define	CTLFLAG_CAPRD	0x00008000	/* Can be read in capability mode */
94#define	CTLFLAG_CAPWR	0x00004000	/* Can be written in capability mode */
95#define	CTLFLAG_STATS	0x00002000	/* Statistics, not a tuneable */
96#define	CTLFLAG_NOFETCH	0x00001000	/* Don't fetch tunable from getenv() */
97#define	CTLFLAG_CAPRW	(CTLFLAG_CAPRD|CTLFLAG_CAPWR)
98
99/*
100 * Secure level.   Note that CTLFLAG_SECURE == CTLFLAG_SECURE1.
101 *
102 * Secure when the securelevel is raised to at least N.
103 */
104#define	CTLSHIFT_SECURE	20
105#define	CTLFLAG_SECURE1	(CTLFLAG_SECURE | (0 << CTLSHIFT_SECURE))
106#define	CTLFLAG_SECURE2	(CTLFLAG_SECURE | (1 << CTLSHIFT_SECURE))
107#define	CTLFLAG_SECURE3	(CTLFLAG_SECURE | (2 << CTLSHIFT_SECURE))
108
109/*
110 * USE THIS instead of a hardwired number from the categories below
111 * to get dynamically assigned sysctl entries using the linker-set
112 * technology. This is the way nearly all new sysctl variables should
113 * be implemented.
114 * e.g. SYSCTL_INT(_parent, OID_AUTO, name, CTLFLAG_RW, &variable, 0, "");
115 */
116#define	OID_AUTO	(-1)
117
118/*
119 * The starting number for dynamically-assigned entries.  WARNING!
120 * ALL static sysctl entries should have numbers LESS than this!
121 */
122#define	CTL_AUTO_START	0x100
123
124#ifdef _KERNEL
125#include <sys/linker_set.h>
126
127#ifdef KLD_MODULE
128/* XXX allow overspecification of type in external kernel modules */
129#define	SYSCTL_CT_ASSERT_MASK CTLTYPE
130#else
131#define	SYSCTL_CT_ASSERT_MASK 0
132#endif
133
134#define	SYSCTL_HANDLER_ARGS struct sysctl_oid *oidp, void *arg1,	\
135	intptr_t arg2, struct sysctl_req *req
136
137/* definitions for sysctl_req 'lock' member */
138#define	REQ_UNWIRED	1
139#define	REQ_WIRED	2
140
141/* definitions for sysctl_req 'flags' member */
142#if defined(__amd64__) || defined(__ia64__) || defined(__powerpc64__) ||\
143    (defined(__mips__) && defined(__mips_n64))
144#define	SCTL_MASK32	1	/* 32 bit emulation */
145#endif
146
147/*
148 * This describes the access space for a sysctl request.  This is needed
149 * so that we can use the interface from the kernel or from user-space.
150 */
151struct sysctl_req {
152	struct thread	*td;		/* used for access checking */
153	int		 lock;		/* wiring state */
154	void		*oldptr;
155	size_t		 oldlen;
156	size_t		 oldidx;
157	int		(*oldfunc)(struct sysctl_req *, const void *, size_t);
158	void		*newptr;
159	size_t		 newlen;
160	size_t		 newidx;
161	int		(*newfunc)(struct sysctl_req *, void *, size_t);
162	size_t		 validlen;
163	int		 flags;
164};
165
166SLIST_HEAD(sysctl_oid_list, sysctl_oid);
167
168/*
169 * This describes one "oid" in the MIB tree.  Potentially more nodes can
170 * be hidden behind it, expanded by the handler.
171 */
172struct sysctl_oid {
173	struct sysctl_oid_list *oid_parent;
174	SLIST_ENTRY(sysctl_oid) oid_link;
175	int		 oid_number;
176	u_int		 oid_kind;
177	void		*oid_arg1;
178	intptr_t	 oid_arg2;
179	const char	*oid_name;
180	int		(*oid_handler)(SYSCTL_HANDLER_ARGS);
181	const char	*oid_fmt;
182	int		 oid_refcnt;
183	u_int		 oid_running;
184	const char	*oid_descr;
185};
186
187#define	SYSCTL_IN(r, p, l)	(r->newfunc)(r, p, l)
188#define	SYSCTL_OUT(r, p, l)	(r->oldfunc)(r, p, l)
189
190int sysctl_handle_int(SYSCTL_HANDLER_ARGS);
191int sysctl_msec_to_ticks(SYSCTL_HANDLER_ARGS);
192int sysctl_handle_long(SYSCTL_HANDLER_ARGS);
193int sysctl_handle_64(SYSCTL_HANDLER_ARGS);
194int sysctl_handle_string(SYSCTL_HANDLER_ARGS);
195int sysctl_handle_opaque(SYSCTL_HANDLER_ARGS);
196int sysctl_handle_counter_u64(SYSCTL_HANDLER_ARGS);
197
198int sysctl_handle_uma_zone_max(SYSCTL_HANDLER_ARGS);
199int sysctl_handle_uma_zone_cur(SYSCTL_HANDLER_ARGS);
200
201int sysctl_dpcpu_int(SYSCTL_HANDLER_ARGS);
202int sysctl_dpcpu_long(SYSCTL_HANDLER_ARGS);
203int sysctl_dpcpu_quad(SYSCTL_HANDLER_ARGS);
204
205/*
206 * These functions are used to add/remove an oid from the mib.
207 */
208void sysctl_register_oid(struct sysctl_oid *oidp);
209void sysctl_register_disabled_oid(struct sysctl_oid *oidp);
210void sysctl_enable_oid(struct sysctl_oid *oidp);
211void sysctl_unregister_oid(struct sysctl_oid *oidp);
212
213/* Declare a static oid to allow child oids to be added to it. */
214#define	SYSCTL_DECL(name)						\
215	extern struct sysctl_oid_list sysctl_##name##_children
216
217/* Hide these in macros. */
218#define	SYSCTL_CHILDREN(oid_ptr)					\
219	(struct sysctl_oid_list *)(oid_ptr)->oid_arg1
220#define	SYSCTL_PARENT(oid_ptr)			NULL	/* not supported */
221#define	SYSCTL_CHILDREN_SET(oid_ptr, val)	(oid_ptr)->oid_arg1 = (val)
222#define	SYSCTL_STATIC_CHILDREN(oid_name)	(&sysctl_##oid_name##_children)
223
224/* === Structs and macros related to context handling. === */
225
226/* All dynamically created sysctls can be tracked in a context list. */
227struct sysctl_ctx_entry {
228	struct sysctl_oid *entry;
229	TAILQ_ENTRY(sysctl_ctx_entry) link;
230};
231
232TAILQ_HEAD(sysctl_ctx_list, sysctl_ctx_entry);
233
234#define	SYSCTL_NODE_CHILDREN(parent, name) \
235	sysctl_##parent##_##name##_children
236
237#ifndef NO_SYSCTL_DESCR
238#define	__DESCR(d) d
239#else
240#define	__DESCR(d) ""
241#endif
242
243/* This constructs a "raw" MIB oid. */
244#define	SYSCTL_OID(parent, nbr, name, kind, a1, a2, handler, fmt, descr)\
245	static struct sysctl_oid sysctl__##parent##_##name = {		\
246		&sysctl_##parent##_children,				\
247		{ NULL },						\
248		nbr,							\
249		kind,							\
250		a1,							\
251		a2,							\
252		#name,							\
253		handler,						\
254		fmt,							\
255		0,							\
256		0,							\
257		__DESCR(descr)						\
258		};							\
259	DATA_SET(sysctl_set, sysctl__##parent##_##name)
260
261#define	SYSCTL_ADD_OID(ctx, parent, nbr, name, kind, a1, a2, handler, fmt, descr) \
262	sysctl_add_oid(ctx, parent, nbr, name, kind, a1, a2, handler, fmt, __DESCR(descr))
263
264/* This constructs a root node from which other nodes can hang. */
265#define	SYSCTL_ROOT_NODE(nbr, name, access, handler, descr)		\
266	SYSCTL_NODE(, nbr, name, access, handler, descr);		\
267	CTASSERT(((access) & CTLTYPE) == 0 ||			\
268	    ((access) & SYSCTL_CT_ASSERT_MASK) == CTLTYPE_NODE)
269
270/* This constructs a node from which other oids can hang. */
271#define	SYSCTL_NODE(parent, nbr, name, access, handler, descr)		    \
272	struct sysctl_oid_list SYSCTL_NODE_CHILDREN(parent, name);	    \
273	SYSCTL_OID(parent, nbr, name, CTLTYPE_NODE|(access),		    \
274	    (void*)&SYSCTL_NODE_CHILDREN(parent, name), 0, handler, "N", descr); \
275	CTASSERT(((access) & CTLTYPE) == 0 ||				\
276	    ((access) & SYSCTL_CT_ASSERT_MASK) == CTLTYPE_NODE)
277
278#define	SYSCTL_ADD_ROOT_NODE(ctx, nbr, name, access, handler, descr) \
279	SYSCTL_ADD_NODE(ctx, SYSCTL_STATIC_CHILDREN(), nbr, name, access, handler, descr)
280
281#define	SYSCTL_ADD_NODE(ctx, parent, nbr, name, access, handler, descr)	\
282({									\
283	CTASSERT(((access) & CTLTYPE) == 0 ||				\
284	    ((access) & SYSCTL_CT_ASSERT_MASK) == CTLTYPE_NODE);	\
285	sysctl_add_oid(ctx, parent, nbr, name, CTLTYPE_NODE|(access),	\
286	    NULL, 0, handler, "N", __DESCR(descr));			\
287})
288
289/* Oid for a string.  len can be 0 to indicate '\0' termination. */
290#define	SYSCTL_STRING(parent, nbr, name, access, arg, len, descr)	\
291	SYSCTL_OID(parent, nbr, name, CTLTYPE_STRING|(access),		\
292	    arg, len, sysctl_handle_string, "A", descr);		\
293	CTASSERT(((access) & CTLTYPE) == 0 ||				\
294	    ((access) & SYSCTL_CT_ASSERT_MASK) == CTLTYPE_STRING)
295
296#define	SYSCTL_ADD_STRING(ctx, parent, nbr, name, access, arg, len, descr) \
297({									\
298	char *__arg = (arg);						\
299	CTASSERT(((access) & CTLTYPE) == 0 ||				\
300	    ((access) & SYSCTL_CT_ASSERT_MASK) == CTLTYPE_STRING);	\
301	sysctl_add_oid(ctx, parent, nbr, name, CTLTYPE_STRING|(access),	\
302	    __arg, len, sysctl_handle_string, "A", __DESCR(descr));	\
303})
304
305/* Oid for an int.  If ptr is SYSCTL_NULL_INT_PTR, val is returned. */
306#define	SYSCTL_NULL_INT_PTR ((int *)NULL)
307#define	SYSCTL_INT(parent, nbr, name, access, ptr, val, descr)	\
308	SYSCTL_OID(parent, nbr, name,				\
309	    CTLTYPE_INT | CTLFLAG_MPSAFE | (access),		\
310	    ptr, val, sysctl_handle_int, "I", descr);		\
311	CTASSERT(((access) & CTLTYPE) == 0 ||			\
312	    ((access) & SYSCTL_CT_ASSERT_MASK) == CTLTYPE_INT);	\
313	CTASSERT(sizeof(int) == sizeof(*(ptr)))
314
315#define	SYSCTL_ADD_INT(ctx, parent, nbr, name, access, ptr, val, descr)	\
316({									\
317	int *__ptr = (ptr);						\
318	CTASSERT(((access) & CTLTYPE) == 0 ||				\
319	    ((access) & SYSCTL_CT_ASSERT_MASK) == CTLTYPE_INT);		\
320	sysctl_add_oid(ctx, parent, nbr, name,				\
321	    CTLTYPE_INT | CTLFLAG_MPSAFE | (access),			\
322	    __ptr, val, sysctl_handle_int, "I", __DESCR(descr));	\
323})
324
325/* Oid for an unsigned int.  If ptr is NULL, val is returned. */
326#define	SYSCTL_NULL_UINT_PTR ((unsigned *)NULL)
327#define	SYSCTL_UINT(parent, nbr, name, access, ptr, val, descr)	\
328	SYSCTL_OID(parent, nbr, name,				\
329	    CTLTYPE_UINT | CTLFLAG_MPSAFE | (access),		\
330	    ptr, val, sysctl_handle_int, "IU", descr);		\
331	CTASSERT(((access) & CTLTYPE) == 0 ||			\
332	    ((access) & SYSCTL_CT_ASSERT_MASK) == CTLTYPE_UINT);\
333	CTASSERT(sizeof(unsigned) == sizeof(*(ptr)))
334
335#define	SYSCTL_ADD_UINT(ctx, parent, nbr, name, access, ptr, val, descr) \
336({									\
337	unsigned *__ptr = (ptr);					\
338	CTASSERT(((access) & CTLTYPE) == 0 ||				\
339	    ((access) & SYSCTL_CT_ASSERT_MASK) == CTLTYPE_UINT);	\
340	sysctl_add_oid(ctx, parent, nbr, name,				\
341	    CTLTYPE_UINT | CTLFLAG_MPSAFE | (access),			\
342	    __ptr, val, sysctl_handle_int, "IU", __DESCR(descr));	\
343})
344
345/* Oid for a long.  The pointer must be non NULL. */
346#define	SYSCTL_NULL_LONG_PTR ((long *)NULL)
347#define	SYSCTL_LONG(parent, nbr, name, access, ptr, val, descr)	\
348	SYSCTL_OID(parent, nbr, name,				\
349	    CTLTYPE_LONG | CTLFLAG_MPSAFE | (access),		\
350	    ptr, val, sysctl_handle_long, "L", descr);		\
351	CTASSERT(((access) & CTLTYPE) == 0 ||			\
352	    ((access) & SYSCTL_CT_ASSERT_MASK) == CTLTYPE_LONG);\
353	CTASSERT(sizeof(long) == sizeof(*(ptr)))
354
355#define	SYSCTL_ADD_LONG(ctx, parent, nbr, name, access, ptr, descr)	\
356({									\
357	long *__ptr = (ptr);						\
358	CTASSERT(((access) & CTLTYPE) == 0 ||				\
359	    ((access) & SYSCTL_CT_ASSERT_MASK) == CTLTYPE_LONG);	\
360	sysctl_add_oid(ctx, parent, nbr, name,				\
361	    CTLTYPE_LONG | CTLFLAG_MPSAFE | (access),			\
362	    __ptr, 0, sysctl_handle_long, "L", __DESCR(descr));		\
363})
364
365/* Oid for an unsigned long.  The pointer must be non NULL. */
366#define	SYSCTL_NULL_ULONG_PTR ((unsigned long *)NULL)
367#define	SYSCTL_ULONG(parent, nbr, name, access, ptr, val, descr)	\
368	SYSCTL_OID(parent, nbr, name,					\
369	    CTLTYPE_ULONG | CTLFLAG_MPSAFE | (access),			\
370	    ptr, val, sysctl_handle_long, "LU", descr);			\
371	CTASSERT(((access) & CTLTYPE) == 0 ||				\
372	    ((access) & SYSCTL_CT_ASSERT_MASK) == CTLTYPE_ULONG);	\
373	CTASSERT(sizeof(unsigned long) == sizeof(*(ptr)))
374
375#define	SYSCTL_ADD_ULONG(ctx, parent, nbr, name, access, ptr, descr)	\
376({									\
377	unsigned long *__ptr = (ptr);					\
378	CTASSERT(((access) & CTLTYPE) == 0 ||				\
379	    ((access) & SYSCTL_CT_ASSERT_MASK) == CTLTYPE_ULONG);	\
380	sysctl_add_oid(ctx, parent, nbr, name,				\
381	    CTLTYPE_ULONG | CTLFLAG_MPSAFE | (access),			\
382	    __ptr, 0, sysctl_handle_long, "LU", __DESCR(descr));	\
383})
384
385/* Oid for a quad.  The pointer must be non NULL. */
386#define	SYSCTL_NULL_QUAD_PTR ((int64_t *)NULL)
387#define	SYSCTL_QUAD(parent, nbr, name, access, ptr, val, descr)	\
388	SYSCTL_OID(parent, nbr, name,				\
389	    CTLTYPE_S64 | CTLFLAG_MPSAFE | (access),		\
390	    ptr, val, sysctl_handle_64, "Q", descr);		\
391	CTASSERT(((access) & CTLTYPE) == 0 ||			\
392	    ((access) & SYSCTL_CT_ASSERT_MASK) == CTLTYPE_S64);	\
393	CTASSERT(sizeof(int64_t) == sizeof(*(ptr)))
394
395#define	SYSCTL_ADD_QUAD(ctx, parent, nbr, name, access, ptr, descr)	\
396({									\
397	int64_t *__ptr = (ptr);						\
398	CTASSERT(((access) & CTLTYPE) == 0 ||				\
399	    ((access) & SYSCTL_CT_ASSERT_MASK) == CTLTYPE_S64);		\
400	sysctl_add_oid(ctx, parent, nbr, name,				\
401	    CTLTYPE_S64 | CTLFLAG_MPSAFE | (access),			\
402	    __ptr, 0, sysctl_handle_64, "Q", __DESCR(descr));		\
403})
404
405#define	SYSCTL_NULL_UQUAD_PTR ((uint64_t *)NULL)
406#define	SYSCTL_UQUAD(parent, nbr, name, access, ptr, val, descr)	\
407	SYSCTL_OID(parent, nbr, name,					\
408	    CTLTYPE_U64 | CTLFLAG_MPSAFE | (access),			\
409	     ptr, val, sysctl_handle_64, "QU", descr);			\
410	CTASSERT(((access) & CTLTYPE) == 0 ||				\
411	    ((access) & SYSCTL_CT_ASSERT_MASK) == CTLTYPE_U64);		\
412	CTASSERT(sizeof(uint64_t) == sizeof(*(ptr)))
413
414#define	SYSCTL_ADD_UQUAD(ctx, parent, nbr, name, access, ptr, descr)	\
415({									\
416	uint64_t *__ptr = (ptr);					\
417	CTASSERT(((access) & CTLTYPE) == 0 ||				\
418	    ((access) & SYSCTL_CT_ASSERT_MASK) == CTLTYPE_U64);		\
419	sysctl_add_oid(ctx, parent, nbr, name,				\
420	    CTLTYPE_U64 | CTLFLAG_MPSAFE | (access),			\
421	    __ptr, 0, sysctl_handle_64, "QU", __DESCR(descr));		\
422})
423
424/* Oid for a CPU dependent variable */
425#define	SYSCTL_ADD_UAUTO(ctx, parent, nbr, name, access, ptr, descr)	\
426({									\
427	struct sysctl_oid *__ret;					\
428	CTASSERT(sizeof(uint64_t) == sizeof(*(ptr)) ||			\
429	    sizeof(unsigned) == sizeof(*(ptr)));			\
430	CTASSERT(((access) & CTLTYPE) == 0);				\
431	if (sizeof(uint64_t) == sizeof(*(ptr))) {			\
432		__ret = sysctl_add_oid(ctx, parent, nbr, name,		\
433		    CTLTYPE_U64 | CTLFLAG_MPSAFE | (access),		\
434		    (ptr), 0, sysctl_handle_64, "QU",			\
435		    __DESCR(descr));					\
436	} else {							\
437		__ret = sysctl_add_oid(ctx, parent, nbr, name,		\
438		    CTLTYPE_UINT | CTLFLAG_MPSAFE | (access),		\
439		    (ptr), 0, sysctl_handle_int, "IU",			\
440		    __DESCR(descr));					\
441	}								\
442	__ret;								\
443})
444
445/* Oid for a 64-bit unsigned counter(9).  The pointer must be non NULL. */
446#define	SYSCTL_COUNTER_U64(parent, nbr, name, access, ptr, descr)	\
447	SYSCTL_OID(parent, nbr, name,					\
448	    CTLTYPE_U64 | CTLFLAG_MPSAFE | (access),			\
449	    (ptr), 0, sysctl_handle_counter_u64, "QU", descr);		\
450	CTASSERT(((access) & CTLTYPE) == 0 ||				\
451	    ((access) & SYSCTL_CT_ASSERT_MASK) == CTLTYPE_U64);		\
452	CTASSERT(sizeof(counter_u64_t) == sizeof(*(ptr)));		\
453	CTASSERT(sizeof(uint64_t) == sizeof(**(ptr)))
454
455#define	SYSCTL_ADD_COUNTER_U64(ctx, parent, nbr, name, access, ptr, descr) \
456({									\
457	counter_u64_t *__ptr = (ptr);					\
458	CTASSERT(((access) & CTLTYPE) == 0 ||				\
459	    ((access) & SYSCTL_CT_ASSERT_MASK) == CTLTYPE_U64);		\
460	sysctl_add_oid(ctx, parent, nbr, name,				\
461	    CTLTYPE_U64 | CTLFLAG_MPSAFE | (access),			\
462	    __ptr, 0, sysctl_handle_counter_u64, "QU", __DESCR(descr));	\
463})
464
465/* Oid for an opaque object.  Specified by a pointer and a length. */
466#define	SYSCTL_OPAQUE(parent, nbr, name, access, ptr, len, fmt, descr)	\
467	SYSCTL_OID(parent, nbr, name, CTLTYPE_OPAQUE|(access),		\
468	    ptr, len, sysctl_handle_opaque, fmt, descr);		\
469	CTASSERT(((access) & CTLTYPE) == 0 ||				\
470	    ((access) & SYSCTL_CT_ASSERT_MASK) == CTLTYPE_OPAQUE)
471
472#define	SYSCTL_ADD_OPAQUE(ctx, parent, nbr, name, access, ptr, len, fmt, descr)	\
473({									\
474	CTASSERT(((access) & CTLTYPE) == 0 ||				\
475	    ((access) & SYSCTL_CT_ASSERT_MASK) == CTLTYPE_OPAQUE);	\
476	sysctl_add_oid(ctx, parent, nbr, name, CTLTYPE_OPAQUE|(access),	\
477	    ptr, len, sysctl_handle_opaque, fmt, __DESCR(descr));	\
478})
479
480/* Oid for a struct.  Specified by a pointer and a type. */
481#define	SYSCTL_STRUCT(parent, nbr, name, access, ptr, type, descr)	\
482	SYSCTL_OID(parent, nbr, name, CTLTYPE_OPAQUE|(access),		\
483	    ptr, sizeof(struct type), sysctl_handle_opaque,		\
484	    "S," #type, descr);						\
485	CTASSERT(((access) & CTLTYPE) == 0 ||				\
486	    ((access) & SYSCTL_CT_ASSERT_MASK) == CTLTYPE_OPAQUE)
487
488#define	SYSCTL_ADD_STRUCT(ctx, parent, nbr, name, access, ptr, type, descr) \
489({									\
490	CTASSERT(((access) & CTLTYPE) == 0 ||				\
491	    ((access) & SYSCTL_CT_ASSERT_MASK) == CTLTYPE_OPAQUE);	\
492	sysctl_add_oid(ctx, parent, nbr, name, CTLTYPE_OPAQUE|(access),	\
493	    (ptr), sizeof(struct type),					\
494	    sysctl_handle_opaque, "S," #type, __DESCR(descr));		\
495})
496
497/* Oid for a procedure.  Specified by a pointer and an arg. */
498#define	SYSCTL_PROC(parent, nbr, name, access, ptr, arg, handler, fmt, descr) \
499	SYSCTL_OID(parent, nbr, name, (access),				\
500	    ptr, arg, handler, fmt, descr);				\
501	CTASSERT(((access) & CTLTYPE) != 0)
502
503#define	SYSCTL_ADD_PROC(ctx, parent, nbr, name, access, ptr, arg, handler, fmt, descr) \
504({									\
505	CTASSERT(((access) & CTLTYPE) != 0);				\
506	sysctl_add_oid(ctx, parent, nbr, name, (access),		\
507	    (ptr), (arg), (handler), (fmt), __DESCR(descr));		\
508})
509
510/* Oid to handle limits on uma(9) zone specified by pointer. */
511#define	SYSCTL_UMA_MAX(parent, nbr, name, access, ptr, descr)	\
512	SYSCTL_OID(parent, nbr, name,				\
513	    CTLTYPE_INT | CTLFLAG_MPSAFE | (access),		\
514	    (ptr), 0, sysctl_handle_uma_zone_max, "I", descr);	\
515	CTASSERT(((access) & CTLTYPE) == 0 ||			\
516	    ((access) & SYSCTL_CT_ASSERT_MASK) == CTLTYPE_INT)
517
518#define	SYSCTL_ADD_UMA_MAX(ctx, parent, nbr, name, access, ptr, descr)	\
519({									\
520	uma_zone_t __ptr = (ptr);					\
521	CTASSERT(((access) & CTLTYPE) == 0 ||				\
522	    ((access) & SYSCTL_CT_ASSERT_MASK) == CTLTYPE_INT);		\
523	sysctl_add_oid(ctx, parent, nbr, name,				\
524	    CTLTYPE_INT | CTLFLAG_MPSAFE | (access),			\
525	    __ptr, 0, sysctl_handle_uma_zone_max, "I", __DESCR(descr));	\
526})
527
528/* Oid to obtain current use of uma(9) zone specified by pointer. */
529#define	SYSCTL_UMA_CUR(parent, nbr, name, access, ptr, descr)		\
530	SYSCTL_OID(parent, nbr, name,					\
531	    CTLTYPE_INT | CTLFLAG_MPSAFE | CTLFLAG_RD | (access),	\
532	    (ptr), 0, sysctl_handle_uma_zone_cur, "I", descr);		\
533	CTASSERT(((access) & CTLTYPE) == 0 ||				\
534	    ((access) & SYSCTL_CT_ASSERT_MASK) == CTLTYPE_INT)
535
536#define	SYSCTL_ADD_UMA_CUR(ctx, parent, nbr, name, access, ptr, descr)	\
537({									\
538	uma_zone_t __ptr = (ptr);					\
539	CTASSERT(((access) & CTLTYPE) == 0 ||				\
540	    ((access) & SYSCTL_CT_ASSERT_MASK) == CTLTYPE_INT);		\
541	sysctl_add_oid(ctx, parent, nbr, name,				\
542	    CTLTYPE_INT | CTLFLAG_MPSAFE | CTLFLAG_RD | (access),	\
543	    __ptr, 0, sysctl_handle_uma_zone_cur, "I", __DESCR(descr));	\
544})
545
546/*
547 * A macro to generate a read-only sysctl to indicate the presence of optional
548 * kernel features.
549 */
550#define	FEATURE(name, desc)						\
551	SYSCTL_INT(_kern_features, OID_AUTO, name, CTLFLAG_RD | CTLFLAG_CAPRD, \
552	    SYSCTL_NULL_INT_PTR, 1, desc)
553
554#endif /* _KERNEL */
555
556/*
557 * Top-level identifiers
558 */
559#define	CTL_UNSPEC	0		/* unused */
560#define	CTL_KERN	1		/* "high kernel": proc, limits */
561#define	CTL_VM		2		/* virtual memory */
562#define	CTL_VFS		3		/* filesystem, mount type is next */
563#define	CTL_NET		4		/* network, see socket.h */
564#define	CTL_DEBUG	5		/* debugging parameters */
565#define	CTL_HW		6		/* generic cpu/io */
566#define	CTL_MACHDEP	7		/* machine dependent */
567#define	CTL_USER	8		/* user-level */
568#define	CTL_P1003_1B	9		/* POSIX 1003.1B */
569#define	CTL_MAXID	10		/* number of valid top-level ids */
570
571/*
572 * CTL_KERN identifiers
573 */
574#define	KERN_OSTYPE		 1	/* string: system version */
575#define	KERN_OSRELEASE		 2	/* string: system release */
576#define	KERN_OSREV		 3	/* int: system revision */
577#define	KERN_VERSION		 4	/* string: compile time info */
578#define	KERN_MAXVNODES		 5	/* int: max vnodes */
579#define	KERN_MAXPROC		 6	/* int: max processes */
580#define	KERN_MAXFILES		 7	/* int: max open files */
581#define	KERN_ARGMAX		 8	/* int: max arguments to exec */
582#define	KERN_SECURELVL		 9	/* int: system security level */
583#define	KERN_HOSTNAME		10	/* string: hostname */
584#define	KERN_HOSTID		11	/* int: host identifier */
585#define	KERN_CLOCKRATE		12	/* struct: struct clockrate */
586#define	KERN_VNODE		13	/* struct: vnode structures */
587#define	KERN_PROC		14	/* struct: process entries */
588#define	KERN_FILE		15	/* struct: file entries */
589#define	KERN_PROF		16	/* node: kernel profiling info */
590#define	KERN_POSIX1		17	/* int: POSIX.1 version */
591#define	KERN_NGROUPS		18	/* int: # of supplemental group ids */
592#define	KERN_JOB_CONTROL	19	/* int: is job control available */
593#define	KERN_SAVED_IDS		20	/* int: saved set-user/group-ID */
594#define	KERN_BOOTTIME		21	/* struct: time kernel was booted */
595#define	KERN_NISDOMAINNAME	22	/* string: YP domain name */
596#define	KERN_UPDATEINTERVAL	23	/* int: update process sleep time */
597#define	KERN_OSRELDATE		24	/* int: kernel release date */
598#define	KERN_NTP_PLL		25	/* node: NTP PLL control */
599#define	KERN_BOOTFILE		26	/* string: name of booted kernel */
600#define	KERN_MAXFILESPERPROC	27	/* int: max open files per proc */
601#define	KERN_MAXPROCPERUID	28	/* int: max processes per uid */
602#define	KERN_DUMPDEV		29	/* struct cdev *: device to dump on */
603#define	KERN_IPC		30	/* node: anything related to IPC */
604#define	KERN_DUMMY		31	/* unused */
605#define	KERN_PS_STRINGS		32	/* int: address of PS_STRINGS */
606#define	KERN_USRSTACK		33	/* int: address of USRSTACK */
607#define	KERN_LOGSIGEXIT		34	/* int: do we log sigexit procs? */
608#define	KERN_IOV_MAX		35	/* int: value of UIO_MAXIOV */
609#define	KERN_HOSTUUID		36	/* string: host UUID identifier */
610#define	KERN_ARND		37	/* int: from arc4rand() */
611#define	KERN_MAXID		38	/* number of valid kern ids */
612/*
613 * KERN_PROC subtypes
614 */
615#define	KERN_PROC_ALL		0	/* everything */
616#define	KERN_PROC_PID		1	/* by process id */
617#define	KERN_PROC_PGRP		2	/* by process group id */
618#define	KERN_PROC_SESSION	3	/* by session of pid */
619#define	KERN_PROC_TTY		4	/* by controlling tty */
620#define	KERN_PROC_UID		5	/* by effective uid */
621#define	KERN_PROC_RUID		6	/* by real uid */
622#define	KERN_PROC_ARGS		7	/* get/set arguments/proctitle */
623#define	KERN_PROC_PROC		8	/* only return procs */
624#define	KERN_PROC_SV_NAME	9	/* get syscall vector name */
625#define	KERN_PROC_RGID		10	/* by real group id */
626#define	KERN_PROC_GID		11	/* by effective group id */
627#define	KERN_PROC_PATHNAME	12	/* path to executable */
628#define	KERN_PROC_OVMMAP	13	/* Old VM map entries for process */
629#define	KERN_PROC_OFILEDESC	14	/* Old file descriptors for process */
630#define	KERN_PROC_KSTACK	15	/* Kernel stacks for process */
631#define	KERN_PROC_INC_THREAD	0x10	/*
632					 * modifier for pid, pgrp, tty,
633					 * uid, ruid, gid, rgid and proc
634					 * This effectively uses 16-31
635					 */
636#define	KERN_PROC_VMMAP		32	/* VM map entries for process */
637#define	KERN_PROC_FILEDESC	33	/* File descriptors for process */
638#define	KERN_PROC_GROUPS	34	/* process groups */
639#define	KERN_PROC_ENV		35	/* get environment */
640#define	KERN_PROC_AUXV		36	/* get ELF auxiliary vector */
641#define	KERN_PROC_RLIMIT	37	/* process resource limits */
642#define	KERN_PROC_PS_STRINGS	38	/* get ps_strings location */
643#define	KERN_PROC_UMASK		39	/* process umask */
644#define	KERN_PROC_OSREL		40	/* osreldate for process binary */
645#define	KERN_PROC_SIGTRAMP	41	/* signal trampoline location */
646
647/*
648 * KERN_IPC identifiers
649 */
650#define	KIPC_MAXSOCKBUF		1	/* int: max size of a socket buffer */
651#define	KIPC_SOCKBUF_WASTE	2	/* int: wastage factor in sockbuf */
652#define	KIPC_SOMAXCONN		3	/* int: max length of connection q */
653#define	KIPC_MAX_LINKHDR	4	/* int: max length of link header */
654#define	KIPC_MAX_PROTOHDR	5	/* int: max length of network header */
655#define	KIPC_MAX_HDR		6	/* int: max total length of headers */
656#define	KIPC_MAX_DATALEN	7	/* int: max length of data? */
657
658/*
659 * CTL_HW identifiers
660 */
661#define	HW_MACHINE	 1		/* string: machine class */
662#define	HW_MODEL	 2		/* string: specific machine model */
663#define	HW_NCPU		 3		/* int: number of cpus */
664#define	HW_BYTEORDER	 4		/* int: machine byte order */
665#define	HW_PHYSMEM	 5		/* int: total memory */
666#define	HW_USERMEM	 6		/* int: non-kernel memory */
667#define	HW_PAGESIZE	 7		/* int: software page size */
668#define	HW_DISKNAMES	 8		/* strings: disk drive names */
669#define	HW_DISKSTATS	 9		/* struct: diskstats[] */
670#define	HW_FLOATINGPT	10		/* int: has HW floating point? */
671#define	HW_MACHINE_ARCH	11		/* string: machine architecture */
672#define	HW_REALMEM	12		/* int: 'real' memory */
673#define	HW_MAXID	13		/* number of valid hw ids */
674
675/*
676 * CTL_USER definitions
677 */
678#define	USER_CS_PATH		 1	/* string: _CS_PATH */
679#define	USER_BC_BASE_MAX	 2	/* int: BC_BASE_MAX */
680#define	USER_BC_DIM_MAX		 3	/* int: BC_DIM_MAX */
681#define	USER_BC_SCALE_MAX	 4	/* int: BC_SCALE_MAX */
682#define	USER_BC_STRING_MAX	 5	/* int: BC_STRING_MAX */
683#define	USER_COLL_WEIGHTS_MAX	 6	/* int: COLL_WEIGHTS_MAX */
684#define	USER_EXPR_NEST_MAX	 7	/* int: EXPR_NEST_MAX */
685#define	USER_LINE_MAX		 8	/* int: LINE_MAX */
686#define	USER_RE_DUP_MAX		 9	/* int: RE_DUP_MAX */
687#define	USER_POSIX2_VERSION	10	/* int: POSIX2_VERSION */
688#define	USER_POSIX2_C_BIND	11	/* int: POSIX2_C_BIND */
689#define	USER_POSIX2_C_DEV	12	/* int: POSIX2_C_DEV */
690#define	USER_POSIX2_CHAR_TERM	13	/* int: POSIX2_CHAR_TERM */
691#define	USER_POSIX2_FORT_DEV	14	/* int: POSIX2_FORT_DEV */
692#define	USER_POSIX2_FORT_RUN	15	/* int: POSIX2_FORT_RUN */
693#define	USER_POSIX2_LOCALEDEF	16	/* int: POSIX2_LOCALEDEF */
694#define	USER_POSIX2_SW_DEV	17	/* int: POSIX2_SW_DEV */
695#define	USER_POSIX2_UPE		18	/* int: POSIX2_UPE */
696#define	USER_STREAM_MAX		19	/* int: POSIX2_STREAM_MAX */
697#define	USER_TZNAME_MAX		20	/* int: POSIX2_TZNAME_MAX */
698#define	USER_MAXID		21	/* number of valid user ids */
699
700#define	CTL_P1003_1B_ASYNCHRONOUS_IO		1	/* boolean */
701#define	CTL_P1003_1B_MAPPED_FILES		2	/* boolean */
702#define	CTL_P1003_1B_MEMLOCK			3	/* boolean */
703#define	CTL_P1003_1B_MEMLOCK_RANGE		4	/* boolean */
704#define	CTL_P1003_1B_MEMORY_PROTECTION		5	/* boolean */
705#define	CTL_P1003_1B_MESSAGE_PASSING		6	/* boolean */
706#define	CTL_P1003_1B_PRIORITIZED_IO		7	/* boolean */
707#define	CTL_P1003_1B_PRIORITY_SCHEDULING	8	/* boolean */
708#define	CTL_P1003_1B_REALTIME_SIGNALS		9	/* boolean */
709#define	CTL_P1003_1B_SEMAPHORES			10	/* boolean */
710#define	CTL_P1003_1B_FSYNC			11	/* boolean */
711#define	CTL_P1003_1B_SHARED_MEMORY_OBJECTS	12	/* boolean */
712#define	CTL_P1003_1B_SYNCHRONIZED_IO		13	/* boolean */
713#define	CTL_P1003_1B_TIMERS			14	/* boolean */
714#define	CTL_P1003_1B_AIO_LISTIO_MAX		15	/* int */
715#define	CTL_P1003_1B_AIO_MAX			16	/* int */
716#define	CTL_P1003_1B_AIO_PRIO_DELTA_MAX		17	/* int */
717#define	CTL_P1003_1B_DELAYTIMER_MAX		18	/* int */
718#define	CTL_P1003_1B_MQ_OPEN_MAX		19	/* int */
719#define	CTL_P1003_1B_PAGESIZE			20	/* int */
720#define	CTL_P1003_1B_RTSIG_MAX			21	/* int */
721#define	CTL_P1003_1B_SEM_NSEMS_MAX		22	/* int */
722#define	CTL_P1003_1B_SEM_VALUE_MAX		23	/* int */
723#define	CTL_P1003_1B_SIGQUEUE_MAX		24	/* int */
724#define	CTL_P1003_1B_TIMER_MAX			25	/* int */
725
726#define	CTL_P1003_1B_MAXID		26
727
728#ifdef _KERNEL
729
730/*
731 * Declare some common oids.
732 */
733extern struct sysctl_oid_list sysctl__children;
734SYSCTL_DECL(_kern);
735SYSCTL_DECL(_kern_features);
736SYSCTL_DECL(_kern_ipc);
737SYSCTL_DECL(_kern_proc);
738SYSCTL_DECL(_kern_sched);
739SYSCTL_DECL(_kern_sched_stats);
740SYSCTL_DECL(_sysctl);
741SYSCTL_DECL(_vm);
742SYSCTL_DECL(_vm_stats);
743SYSCTL_DECL(_vm_stats_misc);
744SYSCTL_DECL(_vfs);
745SYSCTL_DECL(_net);
746SYSCTL_DECL(_debug);
747SYSCTL_DECL(_debug_sizeof);
748SYSCTL_DECL(_dev);
749SYSCTL_DECL(_hw);
750SYSCTL_DECL(_hw_bus);
751SYSCTL_DECL(_hw_bus_devices);
752SYSCTL_DECL(_hw_bus_info);
753SYSCTL_DECL(_machdep);
754SYSCTL_DECL(_user);
755SYSCTL_DECL(_compat);
756SYSCTL_DECL(_regression);
757SYSCTL_DECL(_security);
758SYSCTL_DECL(_security_bsd);
759
760extern char	machine[];
761extern char	osrelease[];
762extern char	ostype[];
763extern char	kern_ident[];
764
765/* Dynamic oid handling */
766struct sysctl_oid *sysctl_add_oid(struct sysctl_ctx_list *clist,
767	    struct sysctl_oid_list *parent, int nbr, const char *name, int kind,
768	    void *arg1, intptr_t arg2, int (*handler)(SYSCTL_HANDLER_ARGS),
769	    const char *fmt, const char *descr);
770int	sysctl_remove_name(struct sysctl_oid *parent, const char *name, int del,
771	    int recurse);
772void	sysctl_rename_oid(struct sysctl_oid *oidp, const char *name);
773int	sysctl_move_oid(struct sysctl_oid *oidp,
774	    struct sysctl_oid_list *parent);
775int	sysctl_remove_oid(struct sysctl_oid *oidp, int del, int recurse);
776int	sysctl_ctx_init(struct sysctl_ctx_list *clist);
777int	sysctl_ctx_free(struct sysctl_ctx_list *clist);
778struct	sysctl_ctx_entry *sysctl_ctx_entry_add(struct sysctl_ctx_list *clist,
779	    struct sysctl_oid *oidp);
780struct	sysctl_ctx_entry *sysctl_ctx_entry_find(struct sysctl_ctx_list *clist,
781	    struct sysctl_oid *oidp);
782int	sysctl_ctx_entry_del(struct sysctl_ctx_list *clist,
783	    struct sysctl_oid *oidp);
784
785int	kernel_sysctl(struct thread *td, int *name, u_int namelen, void *old,
786	    size_t *oldlenp, void *new, size_t newlen, size_t *retval,
787	    int flags);
788int	kernel_sysctlbyname(struct thread *td, char *name, void *old,
789	    size_t *oldlenp, void *new, size_t newlen, size_t *retval,
790	    int flags);
791int	userland_sysctl(struct thread *td, int *name, u_int namelen, void *old,
792	    size_t *oldlenp, int inkernel, void *new, size_t newlen,
793	    size_t *retval, int flags);
794int	sysctl_find_oid(int *name, u_int namelen, struct sysctl_oid **noid,
795	    int *nindx, struct sysctl_req *req);
796void	sysctl_lock(void);
797void	sysctl_unlock(void);
798int	sysctl_wire_old_buffer(struct sysctl_req *req, size_t len);
799
800struct sbuf;
801struct sbuf *sbuf_new_for_sysctl(struct sbuf *, char *, int,
802	    struct sysctl_req *);
803#else	/* !_KERNEL */
804#include <sys/cdefs.h>
805
806__BEGIN_DECLS
807int	sysctl(const int *, u_int, void *, size_t *, const void *, size_t);
808int	sysctlbyname(const char *, void *, size_t *, const void *, size_t);
809int	sysctlnametomib(const char *, int *, size_t *);
810__END_DECLS
811#endif	/* _KERNEL */
812
813#endif	/* !_SYS_SYSCTL_H_ */
814