]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/iovctl/validate.c
THIS BRANCH IS OBSOLETE, PLEASE READ:
[FreeBSD/FreeBSD.git] / usr.sbin / iovctl / validate.c
1 /*-
2  * Copyright (c) 2014-2015 Sandvine Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include <sys/param.h>
31 #include <sys/iov.h>
32 #include <sys/dnv.h>
33 #include <sys/nv.h>
34
35 #include <err.h>
36 #include <regex.h>
37 #include <stdlib.h>
38
39 #include "iovctl.h"
40
41 /*
42  * Returns a writeable pointer to the configuration for the given device.
43  * If no configuration exists, a new nvlist with empty driver and iov
44  * sections is allocated and returned.
45  *
46  * Returning a writeable pointer requires removing the configuration from config
47  * using nvlist_take.  It is the responsibility of the caller to re-insert the
48  * nvlist in config with nvlist_move_nvlist.
49  */
50 static nvlist_t *
51 find_config(nvlist_t *config, const char * device)
52 {
53         nvlist_t *subsystem, *empty_driver, *empty_iov;
54
55         subsystem = dnvlist_take_nvlist(config, device, NULL);
56
57         if (subsystem != NULL)
58                 return (subsystem);
59
60         empty_driver = nvlist_create(NV_FLAG_IGNORE_CASE);
61         if (empty_driver == NULL)
62                 err(1, "Could not allocate config nvlist");
63
64         empty_iov = nvlist_create(NV_FLAG_IGNORE_CASE);
65         if (empty_iov == NULL)
66                 err(1, "Could not allocate config nvlist");
67
68         subsystem = nvlist_create(NV_FLAG_IGNORE_CASE);
69         if (subsystem == NULL)
70                 err(1, "Could not allocate config nvlist");
71
72         nvlist_move_nvlist(subsystem, DRIVER_CONFIG_NAME, empty_driver);
73         nvlist_move_nvlist(subsystem, IOV_CONFIG_NAME, empty_iov);
74
75         return (subsystem);
76 }
77
78 static uint16_t
79 parse_vf_num(const char *key, regmatch_t *matches)
80 {
81         u_long vf_num;
82
83         vf_num = strtoul(key + matches[1].rm_so, NULL, 10);
84
85         if (vf_num > UINT16_MAX)
86                 errx(1, "VF number %lu is too large to be valid",
87                     vf_num);
88
89         return (vf_num);
90 }
91
92 /*
93  * Apply the default values specified in device_defaults to the specified
94  * subsystem in the given device_config.
95  *
96  * This function assumes that the values specified in device_defaults have
97  * already been validated.
98  */
99 static void
100 apply_subsystem_defaults(nvlist_t *device_config, const char *subsystem,
101     const nvlist_t *device_defaults)
102 {
103         nvlist_t *config;
104         const nvlist_t *defaults;
105         const char *name;
106         void *cookie;
107         size_t len;
108         const void *bin;
109         int type;
110
111         config = nvlist_take_nvlist(device_config, subsystem);
112         defaults = nvlist_get_nvlist(device_defaults, subsystem);
113
114         cookie = NULL;
115         while ((name = nvlist_next(defaults, &type, &cookie)) != NULL) {
116                 if (nvlist_exists(config, name))
117                         continue;
118
119                 switch (type) {
120                 case NV_TYPE_BOOL:
121                         nvlist_add_bool(config, name,
122                             nvlist_get_bool(defaults, name));
123                         break;
124                 case NV_TYPE_NUMBER:
125                         nvlist_add_number(config, name,
126                             nvlist_get_number(defaults, name));
127                         break;
128                 case NV_TYPE_STRING:
129                         nvlist_add_string(config, name,
130                             nvlist_get_string(defaults, name));
131                         break;
132                 case NV_TYPE_NVLIST:
133                         nvlist_add_nvlist(config, name,
134                             nvlist_get_nvlist(defaults, name));
135                         break;
136                 case NV_TYPE_BINARY:
137                         bin = nvlist_get_binary(defaults, name, &len);
138                         nvlist_add_binary(config, name, bin, len);
139                         break;
140                 default:
141                         errx(1, "Unexpected type '%d'", type);
142                 }
143         }
144         nvlist_move_nvlist(device_config, subsystem, config);
145 }
146
147 /*
148  * Iterate over every subsystem in the given VF device and apply default values
149  * for parameters that were not configured with a value.
150  *
151  * This function assumes that the values specified in defaults have already been
152  * validated.
153  */
154 static void
155 apply_defaults(nvlist_t *vf, const nvlist_t *defaults)
156 {
157
158         apply_subsystem_defaults(vf, DRIVER_CONFIG_NAME, defaults);
159         apply_subsystem_defaults(vf, IOV_CONFIG_NAME, defaults);
160 }
161
162 /*
163  * Validate that all required parameters have been configured in the specified
164  * subsystem.
165  */
166 static void
167 validate_subsystem(const nvlist_t *device, const nvlist_t *device_schema,
168     const char *subsystem_name, const char *config_name)
169 {
170         const nvlist_t *subsystem, *schema, *config;
171         const char *name;
172         void *cookie;
173         int type;
174
175         subsystem = nvlist_get_nvlist(device, subsystem_name);
176         schema = nvlist_get_nvlist(device_schema, subsystem_name);
177
178         cookie = NULL;
179         while ((name = nvlist_next(schema, &type, &cookie)) != NULL) {
180                 config = nvlist_get_nvlist(schema, name);
181
182                 if (dnvlist_get_bool(config, REQUIRED_SCHEMA_NAME, false)) {
183                         if (!nvlist_exists(subsystem, name))
184                                 errx(1,
185                                     "Required parameter '%s' not found in '%s'",
186                                     name, config_name);
187                 }
188         }
189 }
190
191 /*
192  * Validate that all required parameters have been configured in all subsystems
193  * in the device.
194  */
195 static void
196 validate_device(const nvlist_t *device, const nvlist_t *schema,
197     const char *config_name)
198 {
199
200         validate_subsystem(device, schema, DRIVER_CONFIG_NAME, config_name);
201         validate_subsystem(device, schema, IOV_CONFIG_NAME, config_name);
202 }
203
204 static uint16_t
205 get_num_vfs(const nvlist_t *pf)
206 {
207         const nvlist_t *iov;
208
209         iov = nvlist_get_nvlist(pf, IOV_CONFIG_NAME);
210         return (nvlist_get_number(iov, "num_vfs"));
211 }
212
213 /*
214  * Validates the configuration that has been parsed into config using the given
215  * config schema.  Note that the parser is required to not insert configuration
216  * keys that are not valid in the schema, and to not insert configuration values
217  * that are of the incorrect type.  Therefore this function will not validate
218  * either condition.  This function is only responsible for inserting config
219  * file defaults in individual VF sections and removing the DEFAULT_SCHEMA_NAME
220  * subsystem from config, validating that all required parameters in the schema
221  * are present in each PF and VF subsystem, and that there is no VF subsystem
222  * section whose number exceeds num_vfs.
223  */
224 void
225 validate_config(nvlist_t *config, const nvlist_t *schema, const regex_t *vf_pat)
226 {
227         char device_name[VF_MAX_NAME];
228         regmatch_t matches[2];
229         nvlist_t *defaults, *pf, *vf;
230         const nvlist_t *vf_schema;
231         const char *key;
232         void *cookie;
233         int i, type;
234         uint16_t vf_num, num_vfs;
235
236         pf = find_config(config, PF_CONFIG_NAME);
237         validate_device(pf, nvlist_get_nvlist(schema, PF_CONFIG_NAME),
238             PF_CONFIG_NAME);
239         nvlist_move_nvlist(config, PF_CONFIG_NAME, pf);
240
241         num_vfs = get_num_vfs(pf);
242         vf_schema = nvlist_get_nvlist(schema, VF_SCHEMA_NAME);
243
244         if (num_vfs == 0)
245                 errx(1, "PF.num_vfs must be at least 1");
246
247         defaults = dnvlist_take_nvlist(config, DEFAULT_SCHEMA_NAME, NULL);
248
249         for (i = 0; i < num_vfs; i++) {
250                 snprintf(device_name, sizeof(device_name), VF_PREFIX"%d",
251                     i);
252
253                 vf = find_config(config, device_name);
254
255                 if (defaults != NULL)
256                         apply_defaults(vf, defaults);
257
258                 validate_device(vf, vf_schema, device_name);
259                 nvlist_move_nvlist(config, device_name, vf);
260         }
261         nvlist_destroy(defaults);
262
263         cookie = NULL;
264         while ((key = nvlist_next(config, &type, &cookie)) != NULL) {
265                 if (regexec(vf_pat, key, nitems(matches), matches, 0) == 0) {
266                         vf_num = parse_vf_num(key, matches);
267                         if (vf_num >= num_vfs)
268                                 errx(1,
269                                    "VF number %d is out of bounds (num_vfs=%d)",
270                                     vf_num, num_vfs);
271                 }
272         }
273 }
274