summaryrefslogtreecommitdiffstats
path: root/usr.sbin/iovctl/parse.c
blob: 347b35fb417a3b193cb200e268cc6a8e877ca82b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/*-
 * Copyright (c) 2014-2015 Sandvine Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/param.h>
#include <sys/iov.h>
#include <sys/nv.h>
#include <net/ethernet.h>

#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <regex.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ucl.h>
#include <unistd.h>

#include "iovctl.h"

static void
report_config_error(const char *key, const ucl_object_t *obj, const char *type)
{

	errx(1, "Value '%s' of key '%s' is not of type %s",
	    ucl_object_tostring(obj), key, type);
}

/*
 * Verifies that the value specified in the config file is a boolean value, and
 * then adds the value to the configuration.
 */
static void
add_bool_config(const char *key, const ucl_object_t *obj, nvlist_t *config)
{
	bool val;

	if (!ucl_object_toboolean_safe(obj, &val))
		report_config_error(key, obj, "bool");

	nvlist_add_bool(config, key, val);
}

/*
 * Verifies that the value specified in the config file is a string, and then
 * adds the value to the configuration.
 */
static void
add_string_config(const char *key, const ucl_object_t *obj, nvlist_t *config)
{
	const char *val;

	if (!ucl_object_tostring_safe(obj, &val))
		report_config_error(key, obj, "string");

	nvlist_add_string(config, key, val);
}

/*
 * Verifies that the value specified in the config file is a integer value
 * within the specified range, and then adds the value to the configuration.
 */
static void
add_uint_config(const char *key, const ucl_object_t *obj, nvlist_t *config,
    const char *type, uint64_t max)
{
	int64_t val;
	uint64_t uval;

	/* I must use a signed type here as libucl doesn't provide unsigned. */
	if (!ucl_object_toint_safe(obj, &val))
		report_config_error(key, obj, type);

	if (val < 0)
		report_config_error(key, obj, type);

	uval = val;
	if (uval > max)
		report_config_error(key, obj, type);

	nvlist_add_number(config, key, uval);
}

/*
 * Verifies that the value specified in the config file is a unicast MAC
 * address, and then adds the value to the configuration.
 */
static void
add_unicast_mac_config(const char *key, const ucl_object_t *obj, nvlist_t *config)
{
	uint8_t mac[ETHER_ADDR_LEN];
	const char *val, *token;
	char *parse, *orig_parse, *tokpos, *endpos;
	size_t len;
	u_long value;
	int i;

	if (!ucl_object_tostring_safe(obj, &val))
		report_config_error(key, obj, "unicast-mac");

	parse = strdup(val);
	orig_parse = parse;

	i = 0;
	while ((token = strtok_r(parse, ":", &tokpos)) != NULL) {
		parse = NULL;

		len = strlen(token);
		if (len < 1 || len > 2)
			report_config_error(key, obj, "unicast-mac");

		value = strtoul(token, &endpos, 16);

		if (*endpos != '\0')
			report_config_error(key, obj, "unicast-mac");

		if (value > UINT8_MAX)
			report_config_error(key, obj, "unicast-mac");

		if (i >= ETHER_ADDR_LEN)
			report_config_error(key, obj, "unicast-mac");

		mac[i] = value;
		i++;
	}

	free(orig_parse);

	if (i != ETHER_ADDR_LEN)
		report_config_error(key, obj, "unicast-mac");

	if (ETHER_IS_MULTICAST(mac))
		errx(1, "Value '%s' of key '%s' is a multicast address",
		    ucl_object_tostring(obj), key);

	nvlist_add_binary(config, key, mac, ETHER_ADDR_LEN);
}

/*
 * Validates that the given configuation value has the right type as specified
 * in the schema, and then adds the value to the configuation node.
 */
static void
add_config(const char *key, const ucl_object_t *obj, nvlist_t *config,
    const nvlist_t *schema)
{
	const char *type;

	type = nvlist_get_string(schema, TYPE_SCHEMA_NAME);

	if (strcasecmp(type, "bool") == 0)
		add_bool_config(key, obj, config);
	else if (strcasecmp(type, "string") == 0)
		add_string_config(key, obj, config);
	else if (strcasecmp(type, "uint8_t") == 0)
		add_uint_config(key, obj, config, type, UINT8_MAX);
	else if (strcasecmp(type, "uint16_t") == 0)
		add_uint_config(key, obj, config, type, UINT16_MAX);
	else if (strcasecmp(type, "uint32_t") == 0)
		add_uint_config(key, obj, config, type, UINT32_MAX);
	else if (strcasecmp(type, "uint64_t") == 0)
		add_uint_config(key, obj, config, type, UINT64_MAX);
	else if (strcasecmp(type, "unicast-mac") == 0)
		add_unicast_mac_config(key, obj, config);
	else
		errx(1, "Unexpected type '%s' in schema", type);
}

/*
 * Parses all values specified in a device section in the configuration file,
 * validates that the key/value pair is valid in the schema, and then adds
 * the key/value pair to the correct subsystem in the config.
 */
static void
parse_device_config(const ucl_object_t *top, nvlist_t *config,
    const char *subsystem, const nvlist_t *schema)
{
	ucl_object_iter_t it;
	const ucl_object_t *obj;
	nvlist_t *subsystem_config, *driver_config, *iov_config;
	const nvlist_t *driver_schema, *iov_schema;
	const char *key;

	if (nvlist_exists(config, subsystem))
		errx(1, "Multiple definitions of '%s' in config file",
		    subsystem);

	driver_schema = nvlist_get_nvlist(schema, DRIVER_CONFIG_NAME);
	iov_schema = nvlist_get_nvlist(schema, IOV_CONFIG_NAME);

	driver_config = nvlist_create(NV_FLAG_IGNORE_CASE);
	if (driver_config == NULL)
		err(1, "Could not allocate config nvlist");

	iov_config = nvlist_create(NV_FLAG_IGNORE_CASE);
	if (iov_config == NULL)
		err(1, "Could not allocate config nvlist");

	subsystem_config = nvlist_create(NV_FLAG_IGNORE_CASE);
	if (subsystem_config == NULL)
		err(1, "Could not allocate config nvlist");

	it = NULL;
	while ((obj = ucl_iterate_object(top, &it, true)) != NULL) {
		key = ucl_object_key(obj);

		if (nvlist_exists_nvlist(iov_schema, key))
			add_config(key, obj, iov_config,
			    nvlist_get_nvlist(iov_schema, key));
		else if (nvlist_exists_nvlist(driver_schema, key))
			add_config(key, obj, driver_config,
			    nvlist_get_nvlist(driver_schema, key));
		else
			errx(1, "%s: Invalid config key '%s'", subsystem, key);
	}

	nvlist_move_nvlist(subsystem_config, DRIVER_CONFIG_NAME, driver_config);
	nvlist_move_nvlist(subsystem_config, IOV_CONFIG_NAME, iov_config);
	nvlist_move_nvlist(config, subsystem, subsystem_config);
}

/*
 * Parses the specified config file using the given schema, and returns an
 * nvlist containing the configuration specified by the file.
 *
 * Exits with a message to stderr and an error if any config validation fails.
 */
nvlist_t *
parse_config_file(const char *filename, const nvlist_t *schema)
{
	ucl_object_iter_t it;
	struct ucl_parser *parser;
	ucl_object_t *top;
	const ucl_object_t *obj;
	nvlist_t *config;
	const nvlist_t *pf_schema, *vf_schema;
	const char *errmsg, *key;
	regex_t vf_pat;
	int regex_err, processed_vf;

	regex_err = regcomp(&vf_pat, "^"VF_PREFIX"([1-9][0-9]*|0)$",
	    REG_EXTENDED | REG_ICASE);
	if (regex_err != 0)
		errx(1, "Could not compile VF regex");

	parser = ucl_parser_new(0);
	if (parser == NULL)
		err(1, "Could not allocate parser");

	if (!ucl_parser_add_file(parser, filename))
		err(1, "Could not open '%s' for reading", filename);

	errmsg = ucl_parser_get_error(parser);
	if (errmsg != NULL)
		errx(1, "Could not parse '%s': %s", filename, errmsg);

	config = nvlist_create(NV_FLAG_IGNORE_CASE);
	if (config == NULL)
		err(1, "Could not allocate config nvlist");

	pf_schema = nvlist_get_nvlist(schema, PF_CONFIG_NAME);
	vf_schema = nvlist_get_nvlist(schema, VF_SCHEMA_NAME);

	processed_vf = 0;
	top = ucl_parser_get_object(parser);
	it = NULL;
	while ((obj = ucl_iterate_object(top, &it, true)) != NULL) {
		key = ucl_object_key(obj);

		if (strcasecmp(key, PF_CONFIG_NAME) == 0)
			parse_device_config(obj, config, key, pf_schema);
		else if (strcasecmp(key, DEFAULT_SCHEMA_NAME) == 0) {
			/*
			 * Enforce that the default section must come before all
			 * VF sections.  This will hopefully prevent confusing
			 * the user by having a default value apply to a VF
			 * that was declared earlier in the file.
			 *
			 * This also gives us the flexibility to extend the file
			 * format in the future to allow for multiple default
			 * sections that do only apply to subsequent VF
			 * sections.
			 */
			if (processed_vf)
				errx(1,
			"'default' section must precede all VF sections");

			parse_device_config(obj, config, key, vf_schema);
		} else if (regexec(&vf_pat, key, 0, NULL, 0) == 0) {
			processed_vf = 1;
			parse_device_config(obj, config, key, vf_schema);
		} else
			errx(1, "Unexpected top-level node: %s", key);
	}

	validate_config(config, schema, &vf_pat);

	ucl_object_unref(top);
	ucl_parser_free(parser);
	regfree(&vf_pat);

	return (config);
}

/*
 * Parse the PF configuration section for and return the value specified for
 * the device parameter, or NULL if the device is not specified.
 */
static const char *
find_pf_device(const ucl_object_t *pf)
{
	ucl_object_iter_t it;
	const ucl_object_t *obj;
	const char *key, *device;

	it = NULL;
	while ((obj = ucl_iterate_object(pf, &it, true)) != NULL) {
		key = ucl_object_key(obj);

		if (strcasecmp(key, "device") == 0) {
			if (!ucl_object_tostring_safe(obj, &device))
				err(1,
				    "Config PF.device must be a string");

			return (device);
		}
	}

	return (NULL);
}

/*
 * Manually parse the config file looking for the name of the PF device.  We
 * have to do this separately because we need the config schema to call the
 * normal config file parsing code, and we need to know the name of the PF
 * device so that we can fetch the schema from it.
 *
 * This will always exit on failure, so if it returns then it is guaranteed to
 * have returned a valid device name.
 */
char *
find_device(const char *filename)
{
	char *device;
	const char *deviceName;
	ucl_object_iter_t it;
	struct ucl_parser *parser;
	ucl_object_t *top;
	const ucl_object_t *obj;
	const char *errmsg, *key;
	int error;

	device = NULL;
	deviceName = NULL;

	parser = ucl_parser_new(0);
	if (parser == NULL)
		err(1, "Could not allocate parser");

	if (!ucl_parser_add_file(parser, filename))
		err(1, "Could not open '%s' for reading", filename);

	errmsg = ucl_parser_get_error(parser);
	if (errmsg != NULL)
		errx(1, "Could not parse '%s': %s", filename, errmsg);

	top = ucl_parser_get_object (parser);
	it = NULL;
	while ((obj = ucl_iterate_object(top, &it, true)) != NULL) {
		key = ucl_object_key(obj);

		if (strcasecmp(key, PF_CONFIG_NAME) == 0) {
			deviceName = find_pf_device(obj);
			break;
		}
	}

	if (deviceName == NULL)
		errx(1, "Config file does not specify device");

	error = asprintf(&device, "/dev/iov/%s", deviceName);
	if (error < 0)
		err(1, "Could not allocate memory for device");

	ucl_object_unref(top);
	ucl_parser_free(parser);

	return (device);
}
OpenPOWER on IntegriCloud