1d9266cbaSRyan Stone /*-
2faf139ccSRyan Stone * Copyright (c) 2014-2015 Sandvine Inc.
3d9266cbaSRyan Stone * All rights reserved.
4d9266cbaSRyan Stone *
5d9266cbaSRyan Stone * Redistribution and use in source and binary forms, with or without
6d9266cbaSRyan Stone * modification, are permitted provided that the following conditions
7d9266cbaSRyan Stone * are met:
8d9266cbaSRyan Stone * 1. Redistributions of source code must retain the above copyright
9d9266cbaSRyan Stone * notice, this list of conditions and the following disclaimer.
10d9266cbaSRyan Stone * 2. Redistributions in binary form must reproduce the above copyright
11d9266cbaSRyan Stone * notice, this list of conditions and the following disclaimer in the
12d9266cbaSRyan Stone * documentation and/or other materials provided with the distribution.
13d9266cbaSRyan Stone *
14d9266cbaSRyan Stone * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15d9266cbaSRyan Stone * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16d9266cbaSRyan Stone * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17d9266cbaSRyan Stone * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18d9266cbaSRyan Stone * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19d9266cbaSRyan Stone * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20d9266cbaSRyan Stone * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21d9266cbaSRyan Stone * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22d9266cbaSRyan Stone * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23d9266cbaSRyan Stone * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24d9266cbaSRyan Stone * SUCH DAMAGE.
25d9266cbaSRyan Stone */
26d9266cbaSRyan Stone
27d9266cbaSRyan Stone #include <sys/param.h>
28d9266cbaSRyan Stone #include <sys/iov.h>
29c36e54bbSMariusz Zaborski #include <sys/nv.h>
30d9266cbaSRyan Stone #include <net/ethernet.h>
31d9266cbaSRyan Stone
32d9266cbaSRyan Stone #include <err.h>
33d9266cbaSRyan Stone #include <errno.h>
34d9266cbaSRyan Stone #include <fcntl.h>
35d9266cbaSRyan Stone #include <regex.h>
36d9266cbaSRyan Stone #include <stdint.h>
37d9266cbaSRyan Stone #include <stdio.h>
38d9266cbaSRyan Stone #include <stdlib.h>
39d9266cbaSRyan Stone #include <string.h>
40d9266cbaSRyan Stone #include <ucl.h>
41d9266cbaSRyan Stone #include <unistd.h>
42d9266cbaSRyan Stone
43d9266cbaSRyan Stone #include "iovctl.h"
44d9266cbaSRyan Stone
45d9266cbaSRyan Stone static void
report_config_error(const char * key,const ucl_object_t * obj,const char * type)46d9266cbaSRyan Stone report_config_error(const char *key, const ucl_object_t *obj, const char *type)
47d9266cbaSRyan Stone {
48d9266cbaSRyan Stone
49d9266cbaSRyan Stone errx(1, "Value '%s' of key '%s' is not of type %s",
50d9266cbaSRyan Stone ucl_object_tostring(obj), key, type);
51d9266cbaSRyan Stone }
52d9266cbaSRyan Stone
53d9266cbaSRyan Stone /*
54d9266cbaSRyan Stone * Verifies that the value specified in the config file is a boolean value, and
55d9266cbaSRyan Stone * then adds the value to the configuration.
56d9266cbaSRyan Stone */
57d9266cbaSRyan Stone static void
add_bool_config(const char * key,const ucl_object_t * obj,nvlist_t * config)58d9266cbaSRyan Stone add_bool_config(const char *key, const ucl_object_t *obj, nvlist_t *config)
59d9266cbaSRyan Stone {
60d9266cbaSRyan Stone bool val;
61d9266cbaSRyan Stone
62d9266cbaSRyan Stone if (!ucl_object_toboolean_safe(obj, &val))
63d9266cbaSRyan Stone report_config_error(key, obj, "bool");
64d9266cbaSRyan Stone
65d9266cbaSRyan Stone nvlist_add_bool(config, key, val);
66d9266cbaSRyan Stone }
67d9266cbaSRyan Stone
68d9266cbaSRyan Stone /*
69d9266cbaSRyan Stone * Verifies that the value specified in the config file is a string, and then
70d9266cbaSRyan Stone * adds the value to the configuration.
71d9266cbaSRyan Stone */
72d9266cbaSRyan Stone static void
add_string_config(const char * key,const ucl_object_t * obj,nvlist_t * config)73d9266cbaSRyan Stone add_string_config(const char *key, const ucl_object_t *obj, nvlist_t *config)
74d9266cbaSRyan Stone {
75d9266cbaSRyan Stone const char *val;
76d9266cbaSRyan Stone
77d9266cbaSRyan Stone if (!ucl_object_tostring_safe(obj, &val))
78d9266cbaSRyan Stone report_config_error(key, obj, "string");
79d9266cbaSRyan Stone
80d9266cbaSRyan Stone nvlist_add_string(config, key, val);
81d9266cbaSRyan Stone }
82d9266cbaSRyan Stone
83d9266cbaSRyan Stone /*
84d9266cbaSRyan Stone * Verifies that the value specified in the config file is a integer value
85d9266cbaSRyan Stone * within the specified range, and then adds the value to the configuration.
86d9266cbaSRyan Stone */
87d9266cbaSRyan Stone static void
add_uint_config(const char * key,const ucl_object_t * obj,nvlist_t * config,const char * type,uint64_t max)88d9266cbaSRyan Stone add_uint_config(const char *key, const ucl_object_t *obj, nvlist_t *config,
89d9266cbaSRyan Stone const char *type, uint64_t max)
90d9266cbaSRyan Stone {
91d9266cbaSRyan Stone int64_t val;
92d9266cbaSRyan Stone uint64_t uval;
93d9266cbaSRyan Stone
94d9266cbaSRyan Stone /* I must use a signed type here as libucl doesn't provide unsigned. */
95d9266cbaSRyan Stone if (!ucl_object_toint_safe(obj, &val))
96d9266cbaSRyan Stone report_config_error(key, obj, type);
97d9266cbaSRyan Stone
98d9266cbaSRyan Stone if (val < 0)
99d9266cbaSRyan Stone report_config_error(key, obj, type);
100d9266cbaSRyan Stone
101d9266cbaSRyan Stone uval = val;
102d9266cbaSRyan Stone if (uval > max)
103d9266cbaSRyan Stone report_config_error(key, obj, type);
104d9266cbaSRyan Stone
105d9266cbaSRyan Stone nvlist_add_number(config, key, uval);
106d9266cbaSRyan Stone }
107d9266cbaSRyan Stone
108d9266cbaSRyan Stone /*
109d9266cbaSRyan Stone * Verifies that the value specified in the config file is a unicast MAC
110d9266cbaSRyan Stone * address, and then adds the value to the configuration.
111d9266cbaSRyan Stone */
112d9266cbaSRyan Stone static void
add_unicast_mac_config(const char * key,const ucl_object_t * obj,nvlist_t * config)113d9266cbaSRyan Stone add_unicast_mac_config(const char *key, const ucl_object_t *obj, nvlist_t *config)
114d9266cbaSRyan Stone {
115d9266cbaSRyan Stone uint8_t mac[ETHER_ADDR_LEN];
116d9266cbaSRyan Stone const char *val, *token;
117d9266cbaSRyan Stone char *parse, *orig_parse, *tokpos, *endpos;
118d9266cbaSRyan Stone size_t len;
119d9266cbaSRyan Stone u_long value;
120d9266cbaSRyan Stone int i;
121d9266cbaSRyan Stone
122d9266cbaSRyan Stone if (!ucl_object_tostring_safe(obj, &val))
123d9266cbaSRyan Stone report_config_error(key, obj, "unicast-mac");
124d9266cbaSRyan Stone
125d9266cbaSRyan Stone parse = strdup(val);
126d9266cbaSRyan Stone orig_parse = parse;
127d9266cbaSRyan Stone
128d9266cbaSRyan Stone i = 0;
129d9266cbaSRyan Stone while ((token = strtok_r(parse, ":", &tokpos)) != NULL) {
130d9266cbaSRyan Stone parse = NULL;
131d9266cbaSRyan Stone
132d9266cbaSRyan Stone len = strlen(token);
133d9266cbaSRyan Stone if (len < 1 || len > 2)
134d9266cbaSRyan Stone report_config_error(key, obj, "unicast-mac");
135d9266cbaSRyan Stone
136d9266cbaSRyan Stone value = strtoul(token, &endpos, 16);
137d9266cbaSRyan Stone
138d9266cbaSRyan Stone if (*endpos != '\0')
139d9266cbaSRyan Stone report_config_error(key, obj, "unicast-mac");
140d9266cbaSRyan Stone
141d9266cbaSRyan Stone if (value > UINT8_MAX)
142d9266cbaSRyan Stone report_config_error(key, obj, "unicast-mac");
143d9266cbaSRyan Stone
144d9266cbaSRyan Stone if (i >= ETHER_ADDR_LEN)
145d9266cbaSRyan Stone report_config_error(key, obj, "unicast-mac");
146d9266cbaSRyan Stone
147d9266cbaSRyan Stone mac[i] = value;
148d9266cbaSRyan Stone i++;
149d9266cbaSRyan Stone }
150d9266cbaSRyan Stone
151d9266cbaSRyan Stone free(orig_parse);
152d9266cbaSRyan Stone
153d9266cbaSRyan Stone if (i != ETHER_ADDR_LEN)
154d9266cbaSRyan Stone report_config_error(key, obj, "unicast-mac");
155d9266cbaSRyan Stone
156d9266cbaSRyan Stone if (ETHER_IS_MULTICAST(mac))
157d9266cbaSRyan Stone errx(1, "Value '%s' of key '%s' is a multicast address",
158d9266cbaSRyan Stone ucl_object_tostring(obj), key);
159d9266cbaSRyan Stone
160d9266cbaSRyan Stone nvlist_add_binary(config, key, mac, ETHER_ADDR_LEN);
161d9266cbaSRyan Stone }
162d9266cbaSRyan Stone
163*c57c2617SKristof Provost static void
add_vlan_config(const char * key,const ucl_object_t * obj,nvlist_t * config)164*c57c2617SKristof Provost add_vlan_config(const char *key, const ucl_object_t *obj, nvlist_t *config)
165*c57c2617SKristof Provost {
166*c57c2617SKristof Provost int64_t val;
167*c57c2617SKristof Provost const char *strVal = "";
168*c57c2617SKristof Provost
169*c57c2617SKristof Provost if(ucl_object_tostring_safe(obj, &strVal)) {
170*c57c2617SKristof Provost if (strcasecmp(strVal, "trunk") == 0) {
171*c57c2617SKristof Provost nvlist_add_number(config, key, VF_VLAN_TRUNK);
172*c57c2617SKristof Provost return;
173*c57c2617SKristof Provost }
174*c57c2617SKristof Provost report_config_error(key, obj, "vlan");
175*c57c2617SKristof Provost }
176*c57c2617SKristof Provost
177*c57c2617SKristof Provost if (!ucl_object_toint_safe(obj, &val))
178*c57c2617SKristof Provost report_config_error(key, obj, "vlan");
179*c57c2617SKristof Provost
180*c57c2617SKristof Provost if (val < 0 || val > 4095)
181*c57c2617SKristof Provost report_config_error(key, obj, "vlan");
182*c57c2617SKristof Provost
183*c57c2617SKristof Provost nvlist_add_number(config, key, val);
184*c57c2617SKristof Provost }
185*c57c2617SKristof Provost
186d9266cbaSRyan Stone /*
187a57ca37dSElyes Haouas * Validates that the given configuration value has the right type as specified
188a57ca37dSElyes Haouas * in the schema, and then adds the value to the configuration node.
189d9266cbaSRyan Stone */
190d9266cbaSRyan Stone static void
add_config(const char * key,const ucl_object_t * obj,nvlist_t * config,const nvlist_t * schema)191d9266cbaSRyan Stone add_config(const char *key, const ucl_object_t *obj, nvlist_t *config,
192d9266cbaSRyan Stone const nvlist_t *schema)
193d9266cbaSRyan Stone {
194d9266cbaSRyan Stone const char *type;
195d9266cbaSRyan Stone
196d9266cbaSRyan Stone type = nvlist_get_string(schema, TYPE_SCHEMA_NAME);
197d9266cbaSRyan Stone
198d9266cbaSRyan Stone if (strcasecmp(type, "bool") == 0)
199d9266cbaSRyan Stone add_bool_config(key, obj, config);
200d9266cbaSRyan Stone else if (strcasecmp(type, "string") == 0)
201d9266cbaSRyan Stone add_string_config(key, obj, config);
202d9266cbaSRyan Stone else if (strcasecmp(type, "uint8_t") == 0)
203d9266cbaSRyan Stone add_uint_config(key, obj, config, type, UINT8_MAX);
204d9266cbaSRyan Stone else if (strcasecmp(type, "uint16_t") == 0)
205d9266cbaSRyan Stone add_uint_config(key, obj, config, type, UINT16_MAX);
206d9266cbaSRyan Stone else if (strcasecmp(type, "uint32_t") == 0)
207d9266cbaSRyan Stone add_uint_config(key, obj, config, type, UINT32_MAX);
208d9266cbaSRyan Stone else if (strcasecmp(type, "uint64_t") == 0)
209d9266cbaSRyan Stone add_uint_config(key, obj, config, type, UINT64_MAX);
210d9266cbaSRyan Stone else if (strcasecmp(type, "unicast-mac") == 0)
211d9266cbaSRyan Stone add_unicast_mac_config(key, obj, config);
212*c57c2617SKristof Provost else if (strcasecmp(type, "vlan") == 0)
213*c57c2617SKristof Provost add_vlan_config(key, obj, config);
214d9266cbaSRyan Stone else
215d9266cbaSRyan Stone errx(1, "Unexpected type '%s' in schema", type);
216d9266cbaSRyan Stone }
217d9266cbaSRyan Stone
218d9266cbaSRyan Stone /*
219d9266cbaSRyan Stone * Parses all values specified in a device section in the configuration file,
220d9266cbaSRyan Stone * validates that the key/value pair is valid in the schema, and then adds
221d9266cbaSRyan Stone * the key/value pair to the correct subsystem in the config.
222d9266cbaSRyan Stone */
223d9266cbaSRyan Stone static void
parse_device_config(const ucl_object_t * top,nvlist_t * config,const char * subsystem,const nvlist_t * schema)224d9266cbaSRyan Stone parse_device_config(const ucl_object_t *top, nvlist_t *config,
225d9266cbaSRyan Stone const char *subsystem, const nvlist_t *schema)
226d9266cbaSRyan Stone {
227d9266cbaSRyan Stone ucl_object_iter_t it;
228d9266cbaSRyan Stone const ucl_object_t *obj;
229d9266cbaSRyan Stone nvlist_t *subsystem_config, *driver_config, *iov_config;
230d9266cbaSRyan Stone const nvlist_t *driver_schema, *iov_schema;
231d9266cbaSRyan Stone const char *key;
232d9266cbaSRyan Stone
233d9266cbaSRyan Stone if (nvlist_exists(config, subsystem))
234d9266cbaSRyan Stone errx(1, "Multiple definitions of '%s' in config file",
235d9266cbaSRyan Stone subsystem);
236d9266cbaSRyan Stone
237d9266cbaSRyan Stone driver_schema = nvlist_get_nvlist(schema, DRIVER_CONFIG_NAME);
238d9266cbaSRyan Stone iov_schema = nvlist_get_nvlist(schema, IOV_CONFIG_NAME);
239d9266cbaSRyan Stone
240d9266cbaSRyan Stone driver_config = nvlist_create(NV_FLAG_IGNORE_CASE);
241d9266cbaSRyan Stone if (driver_config == NULL)
242d9266cbaSRyan Stone err(1, "Could not allocate config nvlist");
243d9266cbaSRyan Stone
244d9266cbaSRyan Stone iov_config = nvlist_create(NV_FLAG_IGNORE_CASE);
245d9266cbaSRyan Stone if (iov_config == NULL)
246d9266cbaSRyan Stone err(1, "Could not allocate config nvlist");
247d9266cbaSRyan Stone
248d9266cbaSRyan Stone subsystem_config = nvlist_create(NV_FLAG_IGNORE_CASE);
249d9266cbaSRyan Stone if (subsystem_config == NULL)
250d9266cbaSRyan Stone err(1, "Could not allocate config nvlist");
251d9266cbaSRyan Stone
252d9266cbaSRyan Stone it = NULL;
253d9266cbaSRyan Stone while ((obj = ucl_iterate_object(top, &it, true)) != NULL) {
254d9266cbaSRyan Stone key = ucl_object_key(obj);
255d9266cbaSRyan Stone
256d9266cbaSRyan Stone if (nvlist_exists_nvlist(iov_schema, key))
257d9266cbaSRyan Stone add_config(key, obj, iov_config,
258d9266cbaSRyan Stone nvlist_get_nvlist(iov_schema, key));
259d9266cbaSRyan Stone else if (nvlist_exists_nvlist(driver_schema, key))
260d9266cbaSRyan Stone add_config(key, obj, driver_config,
261d9266cbaSRyan Stone nvlist_get_nvlist(driver_schema, key));
262d9266cbaSRyan Stone else
263d9266cbaSRyan Stone errx(1, "%s: Invalid config key '%s'", subsystem, key);
264d9266cbaSRyan Stone }
265d9266cbaSRyan Stone
266d9266cbaSRyan Stone nvlist_move_nvlist(subsystem_config, DRIVER_CONFIG_NAME, driver_config);
267d9266cbaSRyan Stone nvlist_move_nvlist(subsystem_config, IOV_CONFIG_NAME, iov_config);
268d9266cbaSRyan Stone nvlist_move_nvlist(config, subsystem, subsystem_config);
269d9266cbaSRyan Stone }
270d9266cbaSRyan Stone
271d9266cbaSRyan Stone /*
272d9266cbaSRyan Stone * Parses the specified config file using the given schema, and returns an
273d9266cbaSRyan Stone * nvlist containing the configuration specified by the file.
274d9266cbaSRyan Stone *
275d9266cbaSRyan Stone * Exits with a message to stderr and an error if any config validation fails.
276d9266cbaSRyan Stone */
277d9266cbaSRyan Stone nvlist_t *
parse_config_file(const char * filename,const nvlist_t * schema)278d9266cbaSRyan Stone parse_config_file(const char *filename, const nvlist_t *schema)
279d9266cbaSRyan Stone {
280d9266cbaSRyan Stone ucl_object_iter_t it;
281d9266cbaSRyan Stone struct ucl_parser *parser;
282d9266cbaSRyan Stone ucl_object_t *top;
283d9266cbaSRyan Stone const ucl_object_t *obj;
284d9266cbaSRyan Stone nvlist_t *config;
285d9266cbaSRyan Stone const nvlist_t *pf_schema, *vf_schema;
286d9266cbaSRyan Stone const char *errmsg, *key;
287d9266cbaSRyan Stone regex_t vf_pat;
288d9266cbaSRyan Stone int regex_err, processed_vf;
289d9266cbaSRyan Stone
290d9266cbaSRyan Stone regex_err = regcomp(&vf_pat, "^"VF_PREFIX"([1-9][0-9]*|0)$",
291d9266cbaSRyan Stone REG_EXTENDED | REG_ICASE);
292d9266cbaSRyan Stone if (regex_err != 0)
293d9266cbaSRyan Stone errx(1, "Could not compile VF regex");
294d9266cbaSRyan Stone
295d9266cbaSRyan Stone parser = ucl_parser_new(0);
296d9266cbaSRyan Stone if (parser == NULL)
297d9266cbaSRyan Stone err(1, "Could not allocate parser");
298d9266cbaSRyan Stone
299d9266cbaSRyan Stone if (!ucl_parser_add_file(parser, filename))
300d9266cbaSRyan Stone err(1, "Could not open '%s' for reading", filename);
301d9266cbaSRyan Stone
302d9266cbaSRyan Stone errmsg = ucl_parser_get_error(parser);
303d9266cbaSRyan Stone if (errmsg != NULL)
304d9266cbaSRyan Stone errx(1, "Could not parse '%s': %s", filename, errmsg);
305d9266cbaSRyan Stone
306d9266cbaSRyan Stone config = nvlist_create(NV_FLAG_IGNORE_CASE);
307d9266cbaSRyan Stone if (config == NULL)
308d9266cbaSRyan Stone err(1, "Could not allocate config nvlist");
309d9266cbaSRyan Stone
310d9266cbaSRyan Stone pf_schema = nvlist_get_nvlist(schema, PF_CONFIG_NAME);
311d9266cbaSRyan Stone vf_schema = nvlist_get_nvlist(schema, VF_SCHEMA_NAME);
312d9266cbaSRyan Stone
313d9266cbaSRyan Stone processed_vf = 0;
314d9266cbaSRyan Stone top = ucl_parser_get_object(parser);
315d9266cbaSRyan Stone it = NULL;
316d9266cbaSRyan Stone while ((obj = ucl_iterate_object(top, &it, true)) != NULL) {
317d9266cbaSRyan Stone key = ucl_object_key(obj);
318d9266cbaSRyan Stone
319d9266cbaSRyan Stone if (strcasecmp(key, PF_CONFIG_NAME) == 0)
320d9266cbaSRyan Stone parse_device_config(obj, config, key, pf_schema);
321d9266cbaSRyan Stone else if (strcasecmp(key, DEFAULT_SCHEMA_NAME) == 0) {
322d9266cbaSRyan Stone /*
323d9266cbaSRyan Stone * Enforce that the default section must come before all
324d9266cbaSRyan Stone * VF sections. This will hopefully prevent confusing
325d9266cbaSRyan Stone * the user by having a default value apply to a VF
326d9266cbaSRyan Stone * that was declared earlier in the file.
327d9266cbaSRyan Stone *
328d9266cbaSRyan Stone * This also gives us the flexibility to extend the file
329d9266cbaSRyan Stone * format in the future to allow for multiple default
330d9266cbaSRyan Stone * sections that do only apply to subsequent VF
331d9266cbaSRyan Stone * sections.
332d9266cbaSRyan Stone */
333d9266cbaSRyan Stone if (processed_vf)
334d9266cbaSRyan Stone errx(1,
335d9266cbaSRyan Stone "'default' section must precede all VF sections");
336d9266cbaSRyan Stone
337d9266cbaSRyan Stone parse_device_config(obj, config, key, vf_schema);
338d9266cbaSRyan Stone } else if (regexec(&vf_pat, key, 0, NULL, 0) == 0) {
339d9266cbaSRyan Stone processed_vf = 1;
340d9266cbaSRyan Stone parse_device_config(obj, config, key, vf_schema);
341d9266cbaSRyan Stone } else
342d9266cbaSRyan Stone errx(1, "Unexpected top-level node: %s", key);
343d9266cbaSRyan Stone }
344d9266cbaSRyan Stone
345d9266cbaSRyan Stone validate_config(config, schema, &vf_pat);
346d9266cbaSRyan Stone
347d9266cbaSRyan Stone ucl_object_unref(top);
348d9266cbaSRyan Stone ucl_parser_free(parser);
349d9266cbaSRyan Stone regfree(&vf_pat);
350d9266cbaSRyan Stone
351d9266cbaSRyan Stone return (config);
352d9266cbaSRyan Stone }
353d9266cbaSRyan Stone
354d9266cbaSRyan Stone /*
355d9266cbaSRyan Stone * Parse the PF configuration section for and return the value specified for
356d9266cbaSRyan Stone * the device parameter, or NULL if the device is not specified.
357d9266cbaSRyan Stone */
358d9266cbaSRyan Stone static const char *
find_pf_device(const ucl_object_t * pf)359d9266cbaSRyan Stone find_pf_device(const ucl_object_t *pf)
360d9266cbaSRyan Stone {
361d9266cbaSRyan Stone ucl_object_iter_t it;
362d9266cbaSRyan Stone const ucl_object_t *obj;
363d9266cbaSRyan Stone const char *key, *device;
364d9266cbaSRyan Stone
365d9266cbaSRyan Stone it = NULL;
366d9266cbaSRyan Stone while ((obj = ucl_iterate_object(pf, &it, true)) != NULL) {
367d9266cbaSRyan Stone key = ucl_object_key(obj);
368d9266cbaSRyan Stone
369d9266cbaSRyan Stone if (strcasecmp(key, "device") == 0) {
370d9266cbaSRyan Stone if (!ucl_object_tostring_safe(obj, &device))
371d9266cbaSRyan Stone err(1,
372d9266cbaSRyan Stone "Config PF.device must be a string");
373d9266cbaSRyan Stone
374d9266cbaSRyan Stone return (device);
375d9266cbaSRyan Stone }
376d9266cbaSRyan Stone }
377d9266cbaSRyan Stone
378d9266cbaSRyan Stone return (NULL);
379d9266cbaSRyan Stone }
380d9266cbaSRyan Stone
381d9266cbaSRyan Stone /*
382d9266cbaSRyan Stone * Manually parse the config file looking for the name of the PF device. We
383d9266cbaSRyan Stone * have to do this separately because we need the config schema to call the
384d9266cbaSRyan Stone * normal config file parsing code, and we need to know the name of the PF
385d9266cbaSRyan Stone * device so that we can fetch the schema from it.
386d9266cbaSRyan Stone *
387d9266cbaSRyan Stone * This will always exit on failure, so if it returns then it is guaranteed to
388d9266cbaSRyan Stone * have returned a valid device name.
389d9266cbaSRyan Stone */
390d9266cbaSRyan Stone char *
find_device(const char * filename)391d9266cbaSRyan Stone find_device(const char *filename)
392d9266cbaSRyan Stone {
393d9266cbaSRyan Stone char *device;
394d9266cbaSRyan Stone const char *deviceName;
395d9266cbaSRyan Stone ucl_object_iter_t it;
396d9266cbaSRyan Stone struct ucl_parser *parser;
397d9266cbaSRyan Stone ucl_object_t *top;
398d9266cbaSRyan Stone const ucl_object_t *obj;
399d9266cbaSRyan Stone const char *errmsg, *key;
400d9266cbaSRyan Stone int error;
401d9266cbaSRyan Stone
402d9266cbaSRyan Stone device = NULL;
403d9266cbaSRyan Stone deviceName = NULL;
404d9266cbaSRyan Stone
405d9266cbaSRyan Stone parser = ucl_parser_new(0);
406d9266cbaSRyan Stone if (parser == NULL)
407d9266cbaSRyan Stone err(1, "Could not allocate parser");
408d9266cbaSRyan Stone
409d9266cbaSRyan Stone if (!ucl_parser_add_file(parser, filename))
410d9266cbaSRyan Stone err(1, "Could not open '%s' for reading", filename);
411d9266cbaSRyan Stone
412d9266cbaSRyan Stone errmsg = ucl_parser_get_error(parser);
413d9266cbaSRyan Stone if (errmsg != NULL)
414d9266cbaSRyan Stone errx(1, "Could not parse '%s': %s", filename, errmsg);
415d9266cbaSRyan Stone
416d9266cbaSRyan Stone top = ucl_parser_get_object (parser);
417d9266cbaSRyan Stone it = NULL;
418d9266cbaSRyan Stone while ((obj = ucl_iterate_object(top, &it, true)) != NULL) {
419d9266cbaSRyan Stone key = ucl_object_key(obj);
420d9266cbaSRyan Stone
421d9266cbaSRyan Stone if (strcasecmp(key, PF_CONFIG_NAME) == 0) {
422d9266cbaSRyan Stone deviceName = find_pf_device(obj);
423d9266cbaSRyan Stone break;
424d9266cbaSRyan Stone }
425d9266cbaSRyan Stone }
426d9266cbaSRyan Stone
427d9266cbaSRyan Stone if (deviceName == NULL)
428d9266cbaSRyan Stone errx(1, "Config file does not specify device");
429d9266cbaSRyan Stone
430d9266cbaSRyan Stone error = asprintf(&device, "/dev/iov/%s", deviceName);
431d9266cbaSRyan Stone if (error < 0)
432d9266cbaSRyan Stone err(1, "Could not allocate memory for device");
433d9266cbaSRyan Stone
434d9266cbaSRyan Stone ucl_object_unref(top);
435d9266cbaSRyan Stone ucl_parser_free(parser);
436d9266cbaSRyan Stone
437d9266cbaSRyan Stone return (device);
438d9266cbaSRyan Stone }
439