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>
33 #include <net/ethernet.h>
49 report_config_error(const char *key, const ucl_object_t *obj, const char *type)
52 errx(1, "Value '%s' of key '%s' is not of type %s",
53 ucl_object_tostring(obj), key, type);
57 * Verifies that the value specified in the config file is a boolean value, and
58 * then adds the value to the configuration.
61 add_bool_config(const char *key, const ucl_object_t *obj, nvlist_t *config)
65 if (!ucl_object_toboolean_safe(obj, &val))
66 report_config_error(key, obj, "bool");
68 nvlist_add_bool(config, key, val);
72 * Verifies that the value specified in the config file is a string, and then
73 * adds the value to the configuration.
76 add_string_config(const char *key, const ucl_object_t *obj, nvlist_t *config)
80 if (!ucl_object_tostring_safe(obj, &val))
81 report_config_error(key, obj, "string");
83 nvlist_add_string(config, key, val);
87 * Verifies that the value specified in the config file is a integer value
88 * within the specified range, and then adds the value to the configuration.
91 add_uint_config(const char *key, const ucl_object_t *obj, nvlist_t *config,
92 const char *type, uint64_t max)
97 /* I must use a signed type here as libucl doesn't provide unsigned. */
98 if (!ucl_object_toint_safe(obj, &val))
99 report_config_error(key, obj, type);
102 report_config_error(key, obj, type);
106 report_config_error(key, obj, type);
108 nvlist_add_number(config, key, uval);
112 * Verifies that the value specified in the config file is a unicast MAC
113 * address, and then adds the value to the configuration.
116 add_unicast_mac_config(const char *key, const ucl_object_t *obj, nvlist_t *config)
118 uint8_t mac[ETHER_ADDR_LEN];
119 const char *val, *token;
120 char *parse, *orig_parse, *tokpos, *endpos;
125 if (!ucl_object_tostring_safe(obj, &val))
126 report_config_error(key, obj, "unicast-mac");
132 while ((token = strtok_r(parse, ":", &tokpos)) != NULL) {
136 if (len < 1 || len > 2)
137 report_config_error(key, obj, "unicast-mac");
139 value = strtoul(token, &endpos, 16);
142 report_config_error(key, obj, "unicast-mac");
144 if (value > UINT8_MAX)
145 report_config_error(key, obj, "unicast-mac");
147 if (i >= ETHER_ADDR_LEN)
148 report_config_error(key, obj, "unicast-mac");
156 if (i != ETHER_ADDR_LEN)
157 report_config_error(key, obj, "unicast-mac");
159 if (ETHER_IS_MULTICAST(mac))
160 errx(1, "Value '%s' of key '%s' is a multicast address",
161 ucl_object_tostring(obj), key);
163 nvlist_add_binary(config, key, mac, ETHER_ADDR_LEN);
167 * Validates that the given configuation value has the right type as specified
168 * in the schema, and then adds the value to the configuation node.
171 add_config(const char *key, const ucl_object_t *obj, nvlist_t *config,
172 const nvlist_t *schema)
176 type = nvlist_get_string(schema, TYPE_SCHEMA_NAME);
178 if (strcasecmp(type, "bool") == 0)
179 add_bool_config(key, obj, config);
180 else if (strcasecmp(type, "string") == 0)
181 add_string_config(key, obj, config);
182 else if (strcasecmp(type, "uint8_t") == 0)
183 add_uint_config(key, obj, config, type, UINT8_MAX);
184 else if (strcasecmp(type, "uint16_t") == 0)
185 add_uint_config(key, obj, config, type, UINT16_MAX);
186 else if (strcasecmp(type, "uint32_t") == 0)
187 add_uint_config(key, obj, config, type, UINT32_MAX);
188 else if (strcasecmp(type, "uint64_t") == 0)
189 add_uint_config(key, obj, config, type, UINT64_MAX);
190 else if (strcasecmp(type, "unicast-mac") == 0)
191 add_unicast_mac_config(key, obj, config);
193 errx(1, "Unexpected type '%s' in schema", type);
197 * Parses all values specified in a device section in the configuration file,
198 * validates that the key/value pair is valid in the schema, and then adds
199 * the key/value pair to the correct subsystem in the config.
202 parse_device_config(const ucl_object_t *top, nvlist_t *config,
203 const char *subsystem, const nvlist_t *schema)
205 ucl_object_iter_t it;
206 const ucl_object_t *obj;
207 nvlist_t *subsystem_config, *driver_config, *iov_config;
208 const nvlist_t *driver_schema, *iov_schema;
211 if (nvlist_exists(config, subsystem))
212 errx(1, "Multiple definitions of '%s' in config file",
215 driver_schema = nvlist_get_nvlist(schema, DRIVER_CONFIG_NAME);
216 iov_schema = nvlist_get_nvlist(schema, IOV_CONFIG_NAME);
218 driver_config = nvlist_create(NV_FLAG_IGNORE_CASE);
219 if (driver_config == NULL)
220 err(1, "Could not allocate config nvlist");
222 iov_config = nvlist_create(NV_FLAG_IGNORE_CASE);
223 if (iov_config == NULL)
224 err(1, "Could not allocate config nvlist");
226 subsystem_config = nvlist_create(NV_FLAG_IGNORE_CASE);
227 if (subsystem_config == NULL)
228 err(1, "Could not allocate config nvlist");
231 while ((obj = ucl_iterate_object(top, &it, true)) != NULL) {
232 key = ucl_object_key(obj);
234 if (nvlist_exists_nvlist(iov_schema, key))
235 add_config(key, obj, iov_config,
236 nvlist_get_nvlist(iov_schema, key));
237 else if (nvlist_exists_nvlist(driver_schema, key))
238 add_config(key, obj, driver_config,
239 nvlist_get_nvlist(driver_schema, key));
241 errx(1, "%s: Invalid config key '%s'", subsystem, key);
244 nvlist_move_nvlist(subsystem_config, DRIVER_CONFIG_NAME, driver_config);
245 nvlist_move_nvlist(subsystem_config, IOV_CONFIG_NAME, iov_config);
246 nvlist_move_nvlist(config, subsystem, subsystem_config);
250 * Parses the specified config file using the given schema, and returns an
251 * nvlist containing the configuration specified by the file.
253 * Exits with a message to stderr and an error if any config validation fails.
256 parse_config_file(const char *filename, const nvlist_t *schema)
258 ucl_object_iter_t it;
259 struct ucl_parser *parser;
261 const ucl_object_t *obj;
263 const nvlist_t *pf_schema, *vf_schema;
264 const char *errmsg, *key;
266 int regex_err, processed_vf;
268 regex_err = regcomp(&vf_pat, "^"VF_PREFIX"([1-9][0-9]*|0)$",
269 REG_EXTENDED | REG_ICASE);
271 errx(1, "Could not compile VF regex");
273 parser = ucl_parser_new(0);
275 err(1, "Could not allocate parser");
277 if (!ucl_parser_add_file(parser, filename))
278 err(1, "Could not open '%s' for reading", filename);
280 errmsg = ucl_parser_get_error(parser);
282 errx(1, "Could not parse '%s': %s", filename, errmsg);
284 config = nvlist_create(NV_FLAG_IGNORE_CASE);
286 err(1, "Could not allocate config nvlist");
288 pf_schema = nvlist_get_nvlist(schema, PF_CONFIG_NAME);
289 vf_schema = nvlist_get_nvlist(schema, VF_SCHEMA_NAME);
292 top = ucl_parser_get_object(parser);
294 while ((obj = ucl_iterate_object(top, &it, true)) != NULL) {
295 key = ucl_object_key(obj);
297 if (strcasecmp(key, PF_CONFIG_NAME) == 0)
298 parse_device_config(obj, config, key, pf_schema);
299 else if (strcasecmp(key, DEFAULT_SCHEMA_NAME) == 0) {
301 * Enforce that the default section must come before all
302 * VF sections. This will hopefully prevent confusing
303 * the user by having a default value apply to a VF
304 * that was declared earlier in the file.
306 * This also gives us the flexibility to extend the file
307 * format in the future to allow for multiple default
308 * sections that do only apply to subsequent VF
313 "'default' section must precede all VF sections");
315 parse_device_config(obj, config, key, vf_schema);
316 } else if (regexec(&vf_pat, key, 0, NULL, 0) == 0) {
318 parse_device_config(obj, config, key, vf_schema);
320 errx(1, "Unexpected top-level node: %s", key);
323 validate_config(config, schema, &vf_pat);
325 ucl_object_unref(top);
326 ucl_parser_free(parser);
333 * Parse the PF configuration section for and return the value specified for
334 * the device parameter, or NULL if the device is not specified.
337 find_pf_device(const ucl_object_t *pf)
339 ucl_object_iter_t it;
340 const ucl_object_t *obj;
341 const char *key, *device;
344 while ((obj = ucl_iterate_object(pf, &it, true)) != NULL) {
345 key = ucl_object_key(obj);
347 if (strcasecmp(key, "device") == 0) {
348 if (!ucl_object_tostring_safe(obj, &device))
350 "Config PF.device must be a string");
360 * Manually parse the config file looking for the name of the PF device. We
361 * have to do this separately because we need the config schema to call the
362 * normal config file parsing code, and we need to know the name of the PF
363 * device so that we can fetch the schema from it.
365 * This will always exit on failure, so if it returns then it is guaranteed to
366 * have returned a valid device name.
369 find_device(const char *filename)
372 const char *deviceName;
373 ucl_object_iter_t it;
374 struct ucl_parser *parser;
376 const ucl_object_t *obj;
377 const char *errmsg, *key;
383 parser = ucl_parser_new(0);
385 err(1, "Could not allocate parser");
387 if (!ucl_parser_add_file(parser, filename))
388 err(1, "Could not open '%s' for reading", filename);
390 errmsg = ucl_parser_get_error(parser);
392 errx(1, "Could not parse '%s': %s", filename, errmsg);
394 top = ucl_parser_get_object (parser);
396 while ((obj = ucl_iterate_object(top, &it, true)) != NULL) {
397 key = ucl_object_key(obj);
399 if (strcasecmp(key, PF_CONFIG_NAME) == 0) {
400 deviceName = find_pf_device(obj);
405 if (deviceName == NULL)
406 errx(1, "Config file does not specify device");
408 error = asprintf(&device, "/dev/iov/%s", deviceName);
410 err(1, "Could not allocate memory for device");
412 ucl_object_unref(top);
413 ucl_parser_free(parser);