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 #include <sys/param.h>
31 #include <net/ethernet.h>
47 report_config_error(const char *key, const ucl_object_t *obj, const char *type)
50 errx(1, "Value '%s' of key '%s' is not of type %s",
51 ucl_object_tostring(obj), key, type);
55 * Verifies that the value specified in the config file is a boolean value, and
56 * then adds the value to the configuration.
59 add_bool_config(const char *key, const ucl_object_t *obj, nvlist_t *config)
63 if (!ucl_object_toboolean_safe(obj, &val))
64 report_config_error(key, obj, "bool");
66 nvlist_add_bool(config, key, val);
70 * Verifies that the value specified in the config file is a string, and then
71 * adds the value to the configuration.
74 add_string_config(const char *key, const ucl_object_t *obj, nvlist_t *config)
78 if (!ucl_object_tostring_safe(obj, &val))
79 report_config_error(key, obj, "string");
81 nvlist_add_string(config, key, val);
85 * Verifies that the value specified in the config file is a integer value
86 * within the specified range, and then adds the value to the configuration.
89 add_uint_config(const char *key, const ucl_object_t *obj, nvlist_t *config,
90 const char *type, uint64_t max)
95 /* I must use a signed type here as libucl doesn't provide unsigned. */
96 if (!ucl_object_toint_safe(obj, &val))
97 report_config_error(key, obj, type);
100 report_config_error(key, obj, type);
104 report_config_error(key, obj, type);
106 nvlist_add_number(config, key, uval);
110 * Verifies that the value specified in the config file is a unicast MAC
111 * address, and then adds the value to the configuration.
114 add_unicast_mac_config(const char *key, const ucl_object_t *obj, nvlist_t *config)
116 uint8_t mac[ETHER_ADDR_LEN];
117 const char *val, *token;
118 char *parse, *orig_parse, *tokpos, *endpos;
123 if (!ucl_object_tostring_safe(obj, &val))
124 report_config_error(key, obj, "unicast-mac");
130 while ((token = strtok_r(parse, ":", &tokpos)) != NULL) {
134 if (len < 1 || len > 2)
135 report_config_error(key, obj, "unicast-mac");
137 value = strtoul(token, &endpos, 16);
140 report_config_error(key, obj, "unicast-mac");
142 if (value > UINT8_MAX)
143 report_config_error(key, obj, "unicast-mac");
145 if (i >= ETHER_ADDR_LEN)
146 report_config_error(key, obj, "unicast-mac");
154 if (i != ETHER_ADDR_LEN)
155 report_config_error(key, obj, "unicast-mac");
157 if (ETHER_IS_MULTICAST(mac))
158 errx(1, "Value '%s' of key '%s' is a multicast address",
159 ucl_object_tostring(obj), key);
161 nvlist_add_binary(config, key, mac, ETHER_ADDR_LEN);
165 * Validates that the given configuration value has the right type as specified
166 * in the schema, and then adds the value to the configuration node.
169 add_config(const char *key, const ucl_object_t *obj, nvlist_t *config,
170 const nvlist_t *schema)
174 type = nvlist_get_string(schema, TYPE_SCHEMA_NAME);
176 if (strcasecmp(type, "bool") == 0)
177 add_bool_config(key, obj, config);
178 else if (strcasecmp(type, "string") == 0)
179 add_string_config(key, obj, config);
180 else if (strcasecmp(type, "uint8_t") == 0)
181 add_uint_config(key, obj, config, type, UINT8_MAX);
182 else if (strcasecmp(type, "uint16_t") == 0)
183 add_uint_config(key, obj, config, type, UINT16_MAX);
184 else if (strcasecmp(type, "uint32_t") == 0)
185 add_uint_config(key, obj, config, type, UINT32_MAX);
186 else if (strcasecmp(type, "uint64_t") == 0)
187 add_uint_config(key, obj, config, type, UINT64_MAX);
188 else if (strcasecmp(type, "unicast-mac") == 0)
189 add_unicast_mac_config(key, obj, config);
191 errx(1, "Unexpected type '%s' in schema", type);
195 * Parses all values specified in a device section in the configuration file,
196 * validates that the key/value pair is valid in the schema, and then adds
197 * the key/value pair to the correct subsystem in the config.
200 parse_device_config(const ucl_object_t *top, nvlist_t *config,
201 const char *subsystem, const nvlist_t *schema)
203 ucl_object_iter_t it;
204 const ucl_object_t *obj;
205 nvlist_t *subsystem_config, *driver_config, *iov_config;
206 const nvlist_t *driver_schema, *iov_schema;
209 if (nvlist_exists(config, subsystem))
210 errx(1, "Multiple definitions of '%s' in config file",
213 driver_schema = nvlist_get_nvlist(schema, DRIVER_CONFIG_NAME);
214 iov_schema = nvlist_get_nvlist(schema, IOV_CONFIG_NAME);
216 driver_config = nvlist_create(NV_FLAG_IGNORE_CASE);
217 if (driver_config == NULL)
218 err(1, "Could not allocate config nvlist");
220 iov_config = nvlist_create(NV_FLAG_IGNORE_CASE);
221 if (iov_config == NULL)
222 err(1, "Could not allocate config nvlist");
224 subsystem_config = nvlist_create(NV_FLAG_IGNORE_CASE);
225 if (subsystem_config == NULL)
226 err(1, "Could not allocate config nvlist");
229 while ((obj = ucl_iterate_object(top, &it, true)) != NULL) {
230 key = ucl_object_key(obj);
232 if (nvlist_exists_nvlist(iov_schema, key))
233 add_config(key, obj, iov_config,
234 nvlist_get_nvlist(iov_schema, key));
235 else if (nvlist_exists_nvlist(driver_schema, key))
236 add_config(key, obj, driver_config,
237 nvlist_get_nvlist(driver_schema, key));
239 errx(1, "%s: Invalid config key '%s'", subsystem, key);
242 nvlist_move_nvlist(subsystem_config, DRIVER_CONFIG_NAME, driver_config);
243 nvlist_move_nvlist(subsystem_config, IOV_CONFIG_NAME, iov_config);
244 nvlist_move_nvlist(config, subsystem, subsystem_config);
248 * Parses the specified config file using the given schema, and returns an
249 * nvlist containing the configuration specified by the file.
251 * Exits with a message to stderr and an error if any config validation fails.
254 parse_config_file(const char *filename, const nvlist_t *schema)
256 ucl_object_iter_t it;
257 struct ucl_parser *parser;
259 const ucl_object_t *obj;
261 const nvlist_t *pf_schema, *vf_schema;
262 const char *errmsg, *key;
264 int regex_err, processed_vf;
266 regex_err = regcomp(&vf_pat, "^"VF_PREFIX"([1-9][0-9]*|0)$",
267 REG_EXTENDED | REG_ICASE);
269 errx(1, "Could not compile VF regex");
271 parser = ucl_parser_new(0);
273 err(1, "Could not allocate parser");
275 if (!ucl_parser_add_file(parser, filename))
276 err(1, "Could not open '%s' for reading", filename);
278 errmsg = ucl_parser_get_error(parser);
280 errx(1, "Could not parse '%s': %s", filename, errmsg);
282 config = nvlist_create(NV_FLAG_IGNORE_CASE);
284 err(1, "Could not allocate config nvlist");
286 pf_schema = nvlist_get_nvlist(schema, PF_CONFIG_NAME);
287 vf_schema = nvlist_get_nvlist(schema, VF_SCHEMA_NAME);
290 top = ucl_parser_get_object(parser);
292 while ((obj = ucl_iterate_object(top, &it, true)) != NULL) {
293 key = ucl_object_key(obj);
295 if (strcasecmp(key, PF_CONFIG_NAME) == 0)
296 parse_device_config(obj, config, key, pf_schema);
297 else if (strcasecmp(key, DEFAULT_SCHEMA_NAME) == 0) {
299 * Enforce that the default section must come before all
300 * VF sections. This will hopefully prevent confusing
301 * the user by having a default value apply to a VF
302 * that was declared earlier in the file.
304 * This also gives us the flexibility to extend the file
305 * format in the future to allow for multiple default
306 * sections that do only apply to subsequent VF
311 "'default' section must precede all VF sections");
313 parse_device_config(obj, config, key, vf_schema);
314 } else if (regexec(&vf_pat, key, 0, NULL, 0) == 0) {
316 parse_device_config(obj, config, key, vf_schema);
318 errx(1, "Unexpected top-level node: %s", key);
321 validate_config(config, schema, &vf_pat);
323 ucl_object_unref(top);
324 ucl_parser_free(parser);
331 * Parse the PF configuration section for and return the value specified for
332 * the device parameter, or NULL if the device is not specified.
335 find_pf_device(const ucl_object_t *pf)
337 ucl_object_iter_t it;
338 const ucl_object_t *obj;
339 const char *key, *device;
342 while ((obj = ucl_iterate_object(pf, &it, true)) != NULL) {
343 key = ucl_object_key(obj);
345 if (strcasecmp(key, "device") == 0) {
346 if (!ucl_object_tostring_safe(obj, &device))
348 "Config PF.device must be a string");
358 * Manually parse the config file looking for the name of the PF device. We
359 * have to do this separately because we need the config schema to call the
360 * normal config file parsing code, and we need to know the name of the PF
361 * device so that we can fetch the schema from it.
363 * This will always exit on failure, so if it returns then it is guaranteed to
364 * have returned a valid device name.
367 find_device(const char *filename)
370 const char *deviceName;
371 ucl_object_iter_t it;
372 struct ucl_parser *parser;
374 const ucl_object_t *obj;
375 const char *errmsg, *key;
381 parser = ucl_parser_new(0);
383 err(1, "Could not allocate parser");
385 if (!ucl_parser_add_file(parser, filename))
386 err(1, "Could not open '%s' for reading", filename);
388 errmsg = ucl_parser_get_error(parser);
390 errx(1, "Could not parse '%s': %s", filename, errmsg);
392 top = ucl_parser_get_object (parser);
394 while ((obj = ucl_iterate_object(top, &it, true)) != NULL) {
395 key = ucl_object_key(obj);
397 if (strcasecmp(key, PF_CONFIG_NAME) == 0) {
398 deviceName = find_pf_device(obj);
403 if (deviceName == NULL)
404 errx(1, "Config file does not specify device");
406 error = asprintf(&device, "/dev/iov/%s", deviceName);
408 err(1, "Could not allocate memory for device");
410 ucl_object_unref(top);
411 ucl_parser_free(parser);