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/param.h>
30 #include <net/ethernet.h>
46 report_config_error(const char *key, const ucl_object_t *obj, const char *type)
49 errx(1, "Value '%s' of key '%s' is not of type %s",
50 ucl_object_tostring(obj), key, type);
54 * Verifies that the value specified in the config file is a boolean value, and
55 * then adds the value to the configuration.
58 add_bool_config(const char *key, const ucl_object_t *obj, nvlist_t *config)
62 if (!ucl_object_toboolean_safe(obj, &val))
63 report_config_error(key, obj, "bool");
65 nvlist_add_bool(config, key, val);
69 * Verifies that the value specified in the config file is a string, and then
70 * adds the value to the configuration.
73 add_string_config(const char *key, const ucl_object_t *obj, nvlist_t *config)
77 if (!ucl_object_tostring_safe(obj, &val))
78 report_config_error(key, obj, "string");
80 nvlist_add_string(config, key, val);
84 * Verifies that the value specified in the config file is a integer value
85 * within the specified range, and then adds the value to the configuration.
88 add_uint_config(const char *key, const ucl_object_t *obj, nvlist_t *config,
89 const char *type, uint64_t max)
94 /* I must use a signed type here as libucl doesn't provide unsigned. */
95 if (!ucl_object_toint_safe(obj, &val))
96 report_config_error(key, obj, type);
99 report_config_error(key, obj, type);
103 report_config_error(key, obj, type);
105 nvlist_add_number(config, key, uval);
109 * Verifies that the value specified in the config file is a unicast MAC
110 * address, and then adds the value to the configuration.
113 add_unicast_mac_config(const char *key, const ucl_object_t *obj, nvlist_t *config)
115 uint8_t mac[ETHER_ADDR_LEN];
116 const char *val, *token;
117 char *parse, *orig_parse, *tokpos, *endpos;
122 if (!ucl_object_tostring_safe(obj, &val))
123 report_config_error(key, obj, "unicast-mac");
129 while ((token = strtok_r(parse, ":", &tokpos)) != NULL) {
133 if (len < 1 || len > 2)
134 report_config_error(key, obj, "unicast-mac");
136 value = strtoul(token, &endpos, 16);
139 report_config_error(key, obj, "unicast-mac");
141 if (value > UINT8_MAX)
142 report_config_error(key, obj, "unicast-mac");
144 if (i >= ETHER_ADDR_LEN)
145 report_config_error(key, obj, "unicast-mac");
153 if (i != ETHER_ADDR_LEN)
154 report_config_error(key, obj, "unicast-mac");
156 if (ETHER_IS_MULTICAST(mac))
157 errx(1, "Value '%s' of key '%s' is a multicast address",
158 ucl_object_tostring(obj), key);
160 nvlist_add_binary(config, key, mac, ETHER_ADDR_LEN);
164 * Validates that the given configuration value has the right type as specified
165 * in the schema, and then adds the value to the configuration node.
168 add_config(const char *key, const ucl_object_t *obj, nvlist_t *config,
169 const nvlist_t *schema)
173 type = nvlist_get_string(schema, TYPE_SCHEMA_NAME);
175 if (strcasecmp(type, "bool") == 0)
176 add_bool_config(key, obj, config);
177 else if (strcasecmp(type, "string") == 0)
178 add_string_config(key, obj, config);
179 else if (strcasecmp(type, "uint8_t") == 0)
180 add_uint_config(key, obj, config, type, UINT8_MAX);
181 else if (strcasecmp(type, "uint16_t") == 0)
182 add_uint_config(key, obj, config, type, UINT16_MAX);
183 else if (strcasecmp(type, "uint32_t") == 0)
184 add_uint_config(key, obj, config, type, UINT32_MAX);
185 else if (strcasecmp(type, "uint64_t") == 0)
186 add_uint_config(key, obj, config, type, UINT64_MAX);
187 else if (strcasecmp(type, "unicast-mac") == 0)
188 add_unicast_mac_config(key, obj, config);
190 errx(1, "Unexpected type '%s' in schema", type);
194 * Parses all values specified in a device section in the configuration file,
195 * validates that the key/value pair is valid in the schema, and then adds
196 * the key/value pair to the correct subsystem in the config.
199 parse_device_config(const ucl_object_t *top, nvlist_t *config,
200 const char *subsystem, const nvlist_t *schema)
202 ucl_object_iter_t it;
203 const ucl_object_t *obj;
204 nvlist_t *subsystem_config, *driver_config, *iov_config;
205 const nvlist_t *driver_schema, *iov_schema;
208 if (nvlist_exists(config, subsystem))
209 errx(1, "Multiple definitions of '%s' in config file",
212 driver_schema = nvlist_get_nvlist(schema, DRIVER_CONFIG_NAME);
213 iov_schema = nvlist_get_nvlist(schema, IOV_CONFIG_NAME);
215 driver_config = nvlist_create(NV_FLAG_IGNORE_CASE);
216 if (driver_config == NULL)
217 err(1, "Could not allocate config nvlist");
219 iov_config = nvlist_create(NV_FLAG_IGNORE_CASE);
220 if (iov_config == NULL)
221 err(1, "Could not allocate config nvlist");
223 subsystem_config = nvlist_create(NV_FLAG_IGNORE_CASE);
224 if (subsystem_config == NULL)
225 err(1, "Could not allocate config nvlist");
228 while ((obj = ucl_iterate_object(top, &it, true)) != NULL) {
229 key = ucl_object_key(obj);
231 if (nvlist_exists_nvlist(iov_schema, key))
232 add_config(key, obj, iov_config,
233 nvlist_get_nvlist(iov_schema, key));
234 else if (nvlist_exists_nvlist(driver_schema, key))
235 add_config(key, obj, driver_config,
236 nvlist_get_nvlist(driver_schema, key));
238 errx(1, "%s: Invalid config key '%s'", subsystem, key);
241 nvlist_move_nvlist(subsystem_config, DRIVER_CONFIG_NAME, driver_config);
242 nvlist_move_nvlist(subsystem_config, IOV_CONFIG_NAME, iov_config);
243 nvlist_move_nvlist(config, subsystem, subsystem_config);
247 * Parses the specified config file using the given schema, and returns an
248 * nvlist containing the configuration specified by the file.
250 * Exits with a message to stderr and an error if any config validation fails.
253 parse_config_file(const char *filename, const nvlist_t *schema)
255 ucl_object_iter_t it;
256 struct ucl_parser *parser;
258 const ucl_object_t *obj;
260 const nvlist_t *pf_schema, *vf_schema;
261 const char *errmsg, *key;
263 int regex_err, processed_vf;
265 regex_err = regcomp(&vf_pat, "^"VF_PREFIX"([1-9][0-9]*|0)$",
266 REG_EXTENDED | REG_ICASE);
268 errx(1, "Could not compile VF regex");
270 parser = ucl_parser_new(0);
272 err(1, "Could not allocate parser");
274 if (!ucl_parser_add_file(parser, filename))
275 err(1, "Could not open '%s' for reading", filename);
277 errmsg = ucl_parser_get_error(parser);
279 errx(1, "Could not parse '%s': %s", filename, errmsg);
281 config = nvlist_create(NV_FLAG_IGNORE_CASE);
283 err(1, "Could not allocate config nvlist");
285 pf_schema = nvlist_get_nvlist(schema, PF_CONFIG_NAME);
286 vf_schema = nvlist_get_nvlist(schema, VF_SCHEMA_NAME);
289 top = ucl_parser_get_object(parser);
291 while ((obj = ucl_iterate_object(top, &it, true)) != NULL) {
292 key = ucl_object_key(obj);
294 if (strcasecmp(key, PF_CONFIG_NAME) == 0)
295 parse_device_config(obj, config, key, pf_schema);
296 else if (strcasecmp(key, DEFAULT_SCHEMA_NAME) == 0) {
298 * Enforce that the default section must come before all
299 * VF sections. This will hopefully prevent confusing
300 * the user by having a default value apply to a VF
301 * that was declared earlier in the file.
303 * This also gives us the flexibility to extend the file
304 * format in the future to allow for multiple default
305 * sections that do only apply to subsequent VF
310 "'default' section must precede all VF sections");
312 parse_device_config(obj, config, key, vf_schema);
313 } else if (regexec(&vf_pat, key, 0, NULL, 0) == 0) {
315 parse_device_config(obj, config, key, vf_schema);
317 errx(1, "Unexpected top-level node: %s", key);
320 validate_config(config, schema, &vf_pat);
322 ucl_object_unref(top);
323 ucl_parser_free(parser);
330 * Parse the PF configuration section for and return the value specified for
331 * the device parameter, or NULL if the device is not specified.
334 find_pf_device(const ucl_object_t *pf)
336 ucl_object_iter_t it;
337 const ucl_object_t *obj;
338 const char *key, *device;
341 while ((obj = ucl_iterate_object(pf, &it, true)) != NULL) {
342 key = ucl_object_key(obj);
344 if (strcasecmp(key, "device") == 0) {
345 if (!ucl_object_tostring_safe(obj, &device))
347 "Config PF.device must be a string");
357 * Manually parse the config file looking for the name of the PF device. We
358 * have to do this separately because we need the config schema to call the
359 * normal config file parsing code, and we need to know the name of the PF
360 * device so that we can fetch the schema from it.
362 * This will always exit on failure, so if it returns then it is guaranteed to
363 * have returned a valid device name.
366 find_device(const char *filename)
369 const char *deviceName;
370 ucl_object_iter_t it;
371 struct ucl_parser *parser;
373 const ucl_object_t *obj;
374 const char *errmsg, *key;
380 parser = ucl_parser_new(0);
382 err(1, "Could not allocate parser");
384 if (!ucl_parser_add_file(parser, filename))
385 err(1, "Could not open '%s' for reading", filename);
387 errmsg = ucl_parser_get_error(parser);
389 errx(1, "Could not parse '%s': %s", filename, errmsg);
391 top = ucl_parser_get_object (parser);
393 while ((obj = ucl_iterate_object(top, &it, true)) != NULL) {
394 key = ucl_object_key(obj);
396 if (strcasecmp(key, PF_CONFIG_NAME) == 0) {
397 deviceName = find_pf_device(obj);
402 if (deviceName == NULL)
403 errx(1, "Config file does not specify device");
405 error = asprintf(&device, "/dev/iov/%s", deviceName);
407 err(1, "Could not allocate memory for device");
409 ucl_object_unref(top);
410 ucl_parser_free(parser);