2 * Copyright (c) 2014-2015 Sandvine Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
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.
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.
51 find_config(nvlist_t *config, const char * device)
53 nvlist_t *subsystem, *empty_driver, *empty_iov;
55 subsystem = dnvlist_take_nvlist(config, device, NULL);
57 if (subsystem != NULL)
60 empty_driver = nvlist_create(NV_FLAG_IGNORE_CASE);
61 if (empty_driver == NULL)
62 err(1, "Could not allocate config nvlist");
64 empty_iov = nvlist_create(NV_FLAG_IGNORE_CASE);
65 if (empty_iov == NULL)
66 err(1, "Could not allocate config nvlist");
68 subsystem = nvlist_create(NV_FLAG_IGNORE_CASE);
69 if (subsystem == NULL)
70 err(1, "Could not allocate config nvlist");
72 nvlist_move_nvlist(subsystem, DRIVER_CONFIG_NAME, empty_driver);
73 nvlist_move_nvlist(subsystem, IOV_CONFIG_NAME, empty_iov);
79 parse_vf_num(const char *key, regmatch_t *matches)
83 vf_num = strtoul(key + matches[1].rm_so, NULL, 10);
85 if (vf_num > UINT16_MAX)
86 errx(1, "VF number %lu is too large to be valid",
93 * Apply the default values specified in device_defaults to the specified
94 * subsystem in the given device_config.
96 * This function assumes that the values specified in device_defaults have
97 * already been validated.
100 apply_subsystem_defaults(nvlist_t *device_config, const char *subsystem,
101 const nvlist_t *device_defaults)
104 const nvlist_t *defaults;
111 config = nvlist_take_nvlist(device_config, subsystem);
112 defaults = nvlist_get_nvlist(device_defaults, subsystem);
115 while ((name = nvlist_next(defaults, &type, &cookie)) != NULL) {
116 if (nvlist_exists(config, name))
121 nvlist_add_bool(config, name,
122 nvlist_get_bool(defaults, name));
125 nvlist_add_number(config, name,
126 nvlist_get_number(defaults, name));
129 nvlist_add_string(config, name,
130 nvlist_get_string(defaults, name));
133 nvlist_add_nvlist(config, name,
134 nvlist_get_nvlist(defaults, name));
137 bin = nvlist_get_binary(defaults, name, &len);
138 nvlist_add_binary(config, name, bin, len);
141 errx(1, "Unexpected type '%d'", type);
144 nvlist_move_nvlist(device_config, subsystem, config);
148 * Iterate over every subsystem in the given VF device and apply default values
149 * for parameters that were not configured with a value.
151 * This function assumes that the values specified in defaults have already been
155 apply_defaults(nvlist_t *vf, const nvlist_t *defaults)
158 apply_subsystem_defaults(vf, DRIVER_CONFIG_NAME, defaults);
159 apply_subsystem_defaults(vf, IOV_CONFIG_NAME, defaults);
163 * Validate that all required parameters have been configured in the specified
167 validate_subsystem(const nvlist_t *device, const nvlist_t *device_schema,
168 const char *subsystem_name, const char *config_name)
170 const nvlist_t *subsystem, *schema, *config;
175 subsystem = nvlist_get_nvlist(device, subsystem_name);
176 schema = nvlist_get_nvlist(device_schema, subsystem_name);
179 while ((name = nvlist_next(schema, &type, &cookie)) != NULL) {
180 config = nvlist_get_nvlist(schema, name);
182 if (dnvlist_get_bool(config, REQUIRED_SCHEMA_NAME, false)) {
183 if (!nvlist_exists(subsystem, name))
185 "Required parameter '%s' not found in '%s'",
192 * Validate that all required parameters have been configured in all subsystems
196 validate_device(const nvlist_t *device, const nvlist_t *schema,
197 const char *config_name)
200 validate_subsystem(device, schema, DRIVER_CONFIG_NAME, config_name);
201 validate_subsystem(device, schema, IOV_CONFIG_NAME, config_name);
205 get_num_vfs(const nvlist_t *pf)
209 iov = nvlist_get_nvlist(pf, IOV_CONFIG_NAME);
210 return (nvlist_get_number(iov, "num_vfs"));
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.
225 validate_config(nvlist_t *config, const nvlist_t *schema, const regex_t *vf_pat)
227 char device_name[VF_MAX_NAME];
228 regmatch_t matches[2];
229 nvlist_t *defaults, *pf, *vf;
230 const nvlist_t *vf_schema;
234 uint16_t vf_num, num_vfs;
236 pf = find_config(config, PF_CONFIG_NAME);
237 validate_device(pf, nvlist_get_nvlist(schema, PF_CONFIG_NAME),
239 nvlist_move_nvlist(config, PF_CONFIG_NAME, pf);
241 num_vfs = get_num_vfs(pf);
242 vf_schema = nvlist_get_nvlist(schema, VF_SCHEMA_NAME);
245 errx(1, "PF.num_vfs must be at least 1");
247 defaults = dnvlist_take_nvlist(config, DEFAULT_SCHEMA_NAME, NULL);
249 for (i = 0; i < num_vfs; i++) {
250 snprintf(device_name, sizeof(device_name), VF_PREFIX"%d",
253 vf = find_config(config, device_name);
255 if (defaults != NULL)
256 apply_defaults(vf, defaults);
258 validate_device(vf, vf_schema, device_name);
259 nvlist_move_nvlist(config, device_name, vf);
261 nvlist_destroy(defaults);
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)
269 "VF number %d is out of bounds (num_vfs=%d)",