xref: /illumos-gate/usr/src/uts/common/sys/nvpair.h (revision 4de2612967d06c4fdbf524a62556a1e8118a006f)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #ifndef	_SYS_NVPAIR_H
28 #define	_SYS_NVPAIR_H
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 #include <sys/types.h>
33 #include <sys/errno.h>
34 #include <sys/va_list.h>
35 
36 #if defined(_KERNEL) && !defined(_BOOT)
37 #include <sys/kmem.h>
38 #endif
39 
40 #ifdef	__cplusplus
41 extern "C" {
42 #endif
43 
44 typedef enum {
45 	DATA_TYPE_UNKNOWN = 0,
46 	DATA_TYPE_BOOLEAN,
47 	DATA_TYPE_BYTE,
48 	DATA_TYPE_INT16,
49 	DATA_TYPE_UINT16,
50 	DATA_TYPE_INT32,
51 	DATA_TYPE_UINT32,
52 	DATA_TYPE_INT64,
53 	DATA_TYPE_UINT64,
54 	DATA_TYPE_STRING,
55 	DATA_TYPE_BYTE_ARRAY,
56 	DATA_TYPE_INT16_ARRAY,
57 	DATA_TYPE_UINT16_ARRAY,
58 	DATA_TYPE_INT32_ARRAY,
59 	DATA_TYPE_UINT32_ARRAY,
60 	DATA_TYPE_INT64_ARRAY,
61 	DATA_TYPE_UINT64_ARRAY,
62 	DATA_TYPE_STRING_ARRAY,
63 	DATA_TYPE_HRTIME,
64 	DATA_TYPE_NVLIST,
65 	DATA_TYPE_NVLIST_ARRAY,
66 	DATA_TYPE_BOOLEAN_VALUE,
67 	DATA_TYPE_INT8,
68 	DATA_TYPE_UINT8,
69 	DATA_TYPE_BOOLEAN_ARRAY,
70 	DATA_TYPE_INT8_ARRAY,
71 	DATA_TYPE_UINT8_ARRAY
72 } data_type_t;
73 
74 typedef struct nvpair {
75 	int32_t nvp_size;	/* size of this nvpair */
76 	int16_t	nvp_name_sz;	/* length of name string */
77 	int16_t	nvp_reserve;	/* not used */
78 	int32_t	nvp_value_elem;	/* number of elements for array types */
79 	data_type_t nvp_type;	/* type of value */
80 	/* name string */
81 	/* aligned ptr array for string arrays */
82 	/* aligned array of data for value */
83 } nvpair_t;
84 
85 /* nvlist header */
86 typedef struct nvlist {
87 	int32_t		nvl_version;
88 	uint32_t	nvl_nvflag;	/* persistent flags */
89 	uint64_t	nvl_priv;	/* ptr to private data if not packed */
90 	uint32_t	nvl_flag;
91 	int32_t		nvl_pad;	/* currently not used, for alignment */
92 } nvlist_t;
93 
94 /* nvp implementation version */
95 #define	NV_VERSION	0
96 
97 /* nvlist pack encoding */
98 #define	NV_ENCODE_NATIVE	0
99 #define	NV_ENCODE_XDR		1
100 
101 /* nvlist persistent unique name flags, stored in nvl_nvflags */
102 #define	NV_UNIQUE_NAME		0x1
103 #define	NV_UNIQUE_NAME_TYPE	0x2
104 
105 /* nvlist lookup pairs related flags */
106 #define	NV_FLAG_NOENTOK		0x1
107 
108 /* convenience macros */
109 #define	NV_ALIGN(x)		(((ulong_t)(x) + 7ul) & ~7ul)
110 #define	NV_ALIGN4(x)		(((x) + 3) & ~3)
111 
112 #define	NVP_SIZE(nvp)		((nvp)->nvp_size)
113 #define	NVP_NAME(nvp)		((char *)(nvp) + sizeof (nvpair_t))
114 #define	NVP_TYPE(nvp)		((nvp)->nvp_type)
115 #define	NVP_NELEM(nvp)		((nvp)->nvp_value_elem)
116 #define	NVP_VALUE(nvp)		((char *)(nvp) + NV_ALIGN(sizeof (nvpair_t) \
117 				+ (nvp)->nvp_name_sz))
118 
119 #define	NVL_VERSION(nvl)	((nvl)->nvl_version)
120 #define	NVL_SIZE(nvl)		((nvl)->nvl_size)
121 #define	NVL_FLAG(nvl)		((nvl)->nvl_flag)
122 
123 /* NV allocator framework */
124 typedef struct nv_alloc_ops nv_alloc_ops_t;
125 
126 typedef struct nv_alloc {
127 	const nv_alloc_ops_t *nva_ops;
128 	void *nva_arg;
129 } nv_alloc_t;
130 
131 struct nv_alloc_ops {
132 	int (*nv_ao_init)(nv_alloc_t *, __va_list);
133 	void (*nv_ao_fini)(nv_alloc_t *);
134 	void *(*nv_ao_alloc)(nv_alloc_t *, size_t);
135 	void (*nv_ao_free)(nv_alloc_t *, void *, size_t);
136 	void (*nv_ao_reset)(nv_alloc_t *);
137 };
138 
139 extern const nv_alloc_ops_t *nv_fixed_ops;
140 extern nv_alloc_t *nv_alloc_nosleep;
141 
142 #if defined(_KERNEL) && !defined(_BOOT)
143 extern nv_alloc_t *nv_alloc_sleep;
144 #endif
145 
146 int nv_alloc_init(nv_alloc_t *, const nv_alloc_ops_t *, /* args */ ...);
147 void nv_alloc_reset(nv_alloc_t *);
148 void nv_alloc_fini(nv_alloc_t *);
149 
150 /* list management */
151 int nvlist_alloc(nvlist_t **, uint_t, int);
152 void nvlist_free(nvlist_t *);
153 int nvlist_size(nvlist_t *, size_t *, int);
154 int nvlist_pack(nvlist_t *, char **, size_t *, int, int);
155 int nvlist_unpack(char *, size_t, nvlist_t **, int);
156 int nvlist_dup(nvlist_t *, nvlist_t **, int);
157 int nvlist_merge(nvlist_t *, nvlist_t *, int);
158 
159 int nvlist_xalloc(nvlist_t **, uint_t, nv_alloc_t *);
160 int nvlist_xpack(nvlist_t *, char **, size_t *, int, nv_alloc_t *);
161 int nvlist_xunpack(char *, size_t, nvlist_t **, nv_alloc_t *);
162 int nvlist_xdup(nvlist_t *, nvlist_t **, nv_alloc_t *);
163 nv_alloc_t *nvlist_lookup_nv_alloc(nvlist_t *);
164 
165 int nvlist_add_nvpair(nvlist_t *, nvpair_t *);
166 int nvlist_add_boolean(nvlist_t *, const char *);
167 int nvlist_add_boolean_value(nvlist_t *, const char *, boolean_t);
168 int nvlist_add_byte(nvlist_t *, const char *, uchar_t);
169 int nvlist_add_int8(nvlist_t *, const char *, int8_t);
170 int nvlist_add_uint8(nvlist_t *, const char *, uint8_t);
171 int nvlist_add_int16(nvlist_t *, const char *, int16_t);
172 int nvlist_add_uint16(nvlist_t *, const char *, uint16_t);
173 int nvlist_add_int32(nvlist_t *, const char *, int32_t);
174 int nvlist_add_uint32(nvlist_t *, const char *, uint32_t);
175 int nvlist_add_int64(nvlist_t *, const char *, int64_t);
176 int nvlist_add_uint64(nvlist_t *, const char *, uint64_t);
177 int nvlist_add_string(nvlist_t *, const char *, const char *);
178 int nvlist_add_nvlist(nvlist_t *, const char *, nvlist_t *);
179 int nvlist_add_boolean_array(nvlist_t *, const char *, boolean_t *, uint_t);
180 int nvlist_add_byte_array(nvlist_t *, const char *, uchar_t *, uint_t);
181 int nvlist_add_int8_array(nvlist_t *, const char *, int8_t *, uint_t);
182 int nvlist_add_uint8_array(nvlist_t *, const char *, uint8_t *, uint_t);
183 int nvlist_add_int16_array(nvlist_t *, const char *, int16_t *, uint_t);
184 int nvlist_add_uint16_array(nvlist_t *, const char *, uint16_t *, uint_t);
185 int nvlist_add_int32_array(nvlist_t *, const char *, int32_t *, uint_t);
186 int nvlist_add_uint32_array(nvlist_t *, const char *, uint32_t *, uint_t);
187 int nvlist_add_int64_array(nvlist_t *, const char *, int64_t *, uint_t);
188 int nvlist_add_uint64_array(nvlist_t *, const char *, uint64_t *, uint_t);
189 int nvlist_add_string_array(nvlist_t *, const char *, char *const *, uint_t);
190 int nvlist_add_nvlist_array(nvlist_t *, const char *, nvlist_t **, uint_t);
191 int nvlist_add_hrtime(nvlist_t *, const char *, hrtime_t);
192 
193 int nvlist_remove(nvlist_t *, const char *, data_type_t);
194 int nvlist_remove_all(nvlist_t *, const char *);
195 
196 int nvlist_lookup_boolean(nvlist_t *, const char *);
197 int nvlist_lookup_boolean_value(nvlist_t *, const char *, boolean_t *);
198 int nvlist_lookup_byte(nvlist_t *, const char *, uchar_t *);
199 int nvlist_lookup_int8(nvlist_t *, const char *, int8_t *);
200 int nvlist_lookup_uint8(nvlist_t *, const char *, uint8_t *);
201 int nvlist_lookup_int16(nvlist_t *, const char *, int16_t *);
202 int nvlist_lookup_uint16(nvlist_t *, const char *, uint16_t *);
203 int nvlist_lookup_int32(nvlist_t *, const char *, int32_t *);
204 int nvlist_lookup_uint32(nvlist_t *, const char *, uint32_t *);
205 int nvlist_lookup_int64(nvlist_t *, const char *, int64_t *);
206 int nvlist_lookup_uint64(nvlist_t *, const char *, uint64_t *);
207 int nvlist_lookup_string(nvlist_t *, const char *, char **);
208 int nvlist_lookup_nvlist(nvlist_t *, const char *, nvlist_t **);
209 int nvlist_lookup_boolean_array(nvlist_t *, const char *,
210     boolean_t **, uint_t *);
211 int nvlist_lookup_byte_array(nvlist_t *, const char *, uchar_t **, uint_t *);
212 int nvlist_lookup_int8_array(nvlist_t *, const char *, int8_t **, uint_t *);
213 int nvlist_lookup_uint8_array(nvlist_t *, const char *, uint8_t **, uint_t *);
214 int nvlist_lookup_int16_array(nvlist_t *, const char *, int16_t **, uint_t *);
215 int nvlist_lookup_uint16_array(nvlist_t *, const char *, uint16_t **, uint_t *);
216 int nvlist_lookup_int32_array(nvlist_t *, const char *, int32_t **, uint_t *);
217 int nvlist_lookup_uint32_array(nvlist_t *, const char *, uint32_t **, uint_t *);
218 int nvlist_lookup_int64_array(nvlist_t *, const char *, int64_t **, uint_t *);
219 int nvlist_lookup_uint64_array(nvlist_t *, const char *, uint64_t **, uint_t *);
220 int nvlist_lookup_string_array(nvlist_t *, const char *, char ***, uint_t *);
221 int nvlist_lookup_nvlist_array(nvlist_t *, const char *,
222     nvlist_t ***, uint_t *);
223 int nvlist_lookup_hrtime(nvlist_t *, const char *, hrtime_t *);
224 int nvlist_lookup_pairs(nvlist_t *nvl, int, ...);
225 
226 /* processing nvpair */
227 nvpair_t *nvlist_next_nvpair(nvlist_t *nvl, nvpair_t *);
228 char *nvpair_name(nvpair_t *);
229 data_type_t nvpair_type(nvpair_t *);
230 int nvpair_value_boolean_value(nvpair_t *, boolean_t *);
231 int nvpair_value_byte(nvpair_t *, uchar_t *);
232 int nvpair_value_int8(nvpair_t *, int8_t *);
233 int nvpair_value_uint8(nvpair_t *, uint8_t *);
234 int nvpair_value_int16(nvpair_t *, int16_t *);
235 int nvpair_value_uint16(nvpair_t *, uint16_t *);
236 int nvpair_value_int32(nvpair_t *, int32_t *);
237 int nvpair_value_uint32(nvpair_t *, uint32_t *);
238 int nvpair_value_int64(nvpair_t *, int64_t *);
239 int nvpair_value_uint64(nvpair_t *, uint64_t *);
240 int nvpair_value_string(nvpair_t *, char **);
241 int nvpair_value_nvlist(nvpair_t *, nvlist_t **);
242 int nvpair_value_boolean_array(nvpair_t *, boolean_t **, uint_t *);
243 int nvpair_value_byte_array(nvpair_t *, uchar_t **, uint_t *);
244 int nvpair_value_int8_array(nvpair_t *, int8_t **, uint_t *);
245 int nvpair_value_uint8_array(nvpair_t *, uint8_t **, uint_t *);
246 int nvpair_value_int16_array(nvpair_t *, int16_t **, uint_t *);
247 int nvpair_value_uint16_array(nvpair_t *, uint16_t **, uint_t *);
248 int nvpair_value_int32_array(nvpair_t *, int32_t **, uint_t *);
249 int nvpair_value_uint32_array(nvpair_t *, uint32_t **, uint_t *);
250 int nvpair_value_int64_array(nvpair_t *, int64_t **, uint_t *);
251 int nvpair_value_uint64_array(nvpair_t *, uint64_t **, uint_t *);
252 int nvpair_value_string_array(nvpair_t *, char ***, uint_t *);
253 int nvpair_value_nvlist_array(nvpair_t *, nvlist_t ***, uint_t *);
254 int nvpair_value_hrtime(nvpair_t *, hrtime_t *);
255 
256 #ifdef	__cplusplus
257 }
258 #endif
259 
260 #endif	/* _SYS_NVPAIR_H */
261