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
|
/*-
* 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/dnv.h>
#include <sys/nv.h>
#include <err.h>
#include <regex.h>
#include <stdlib.h>
#include "iovctl.h"
/*
* Returns a writeable pointer to the configuration for the given device.
* If no configuration exists, a new nvlist with empty driver and iov
* sections is allocated and returned.
*
* Returning a writeable pointer requires removing the configuration from config
* using nvlist_take. It is the responsibility of the caller to re-insert the
* nvlist in config with nvlist_move_nvlist.
*/
static nvlist_t *
find_config(nvlist_t *config, const char * device)
{
nvlist_t *subsystem, *empty_driver, *empty_iov;
subsystem = dnvlist_take_nvlist(config, device, NULL);
if (subsystem != NULL)
return (subsystem);
empty_driver = nvlist_create(NV_FLAG_IGNORE_CASE);
if (empty_driver == NULL)
err(1, "Could not allocate config nvlist");
empty_iov = nvlist_create(NV_FLAG_IGNORE_CASE);
if (empty_iov == NULL)
err(1, "Could not allocate config nvlist");
subsystem = nvlist_create(NV_FLAG_IGNORE_CASE);
if (subsystem == NULL)
err(1, "Could not allocate config nvlist");
nvlist_move_nvlist(subsystem, DRIVER_CONFIG_NAME, empty_driver);
nvlist_move_nvlist(subsystem, IOV_CONFIG_NAME, empty_iov);
return (subsystem);
}
static uint16_t
parse_vf_num(const char *key, regmatch_t *matches)
{
u_long vf_num;
vf_num = strtoul(key + matches[1].rm_so, NULL, 10);
if (vf_num > UINT16_MAX)
errx(1, "VF number %lu is too large to be valid",
vf_num);
return (vf_num);
}
/*
* Apply the default values specified in device_defaults to the specified
* subsystem in the given device_config.
*
* This function assumes that the values specified in device_defaults have
* already been validated.
*/
static void
apply_subsystem_defaults(nvlist_t *device_config, const char *subsystem,
const nvlist_t *device_defaults)
{
nvlist_t *config;
const nvlist_t *defaults;
const char *name;
void *cookie;
size_t len;
const void *bin;
int type;
config = nvlist_take_nvlist(device_config, subsystem);
defaults = nvlist_get_nvlist(device_defaults, subsystem);
cookie = NULL;
while ((name = nvlist_next(defaults, &type, &cookie)) != NULL) {
if (nvlist_exists(config, name))
continue;
switch (type) {
case NV_TYPE_BOOL:
nvlist_add_bool(config, name,
nvlist_get_bool(defaults, name));
break;
case NV_TYPE_NUMBER:
nvlist_add_number(config, name,
nvlist_get_number(defaults, name));
break;
case NV_TYPE_STRING:
nvlist_add_string(config, name,
nvlist_get_string(defaults, name));
break;
case NV_TYPE_NVLIST:
nvlist_add_nvlist(config, name,
nvlist_get_nvlist(defaults, name));
break;
case NV_TYPE_BINARY:
bin = nvlist_get_binary(defaults, name, &len);
nvlist_add_binary(config, name, bin, len);
break;
default:
errx(1, "Unexpected type '%d'", type);
}
}
nvlist_move_nvlist(device_config, subsystem, config);
}
/*
* Iterate over every subsystem in the given VF device and apply default values
* for parameters that were not configured with a value.
*
* This function assumes that the values specified in defaults have already been
* validated.
*/
static void
apply_defaults(nvlist_t *vf, const nvlist_t *defaults)
{
apply_subsystem_defaults(vf, DRIVER_CONFIG_NAME, defaults);
apply_subsystem_defaults(vf, IOV_CONFIG_NAME, defaults);
}
/*
* Validate that all required parameters have been configured in the specified
* subsystem.
*/
static void
validate_subsystem(const nvlist_t *device, const nvlist_t *device_schema,
const char *subsystem_name, const char *config_name)
{
const nvlist_t *subsystem, *schema, *config;
const char *name;
void *cookie;
int type;
subsystem = nvlist_get_nvlist(device, subsystem_name);
schema = nvlist_get_nvlist(device_schema, subsystem_name);
cookie = NULL;
while ((name = nvlist_next(schema, &type, &cookie)) != NULL) {
config = nvlist_get_nvlist(schema, name);
if (dnvlist_get_bool(config, REQUIRED_SCHEMA_NAME, false)) {
if (!nvlist_exists(subsystem, name))
errx(1,
"Required parameter '%s' not found in '%s'",
name, config_name);
}
}
}
/*
* Validate that all required parameters have been configured in all subsystems
* in the device.
*/
static void
validate_device(const nvlist_t *device, const nvlist_t *schema,
const char *config_name)
{
validate_subsystem(device, schema, DRIVER_CONFIG_NAME, config_name);
validate_subsystem(device, schema, IOV_CONFIG_NAME, config_name);
}
static uint16_t
get_num_vfs(const nvlist_t *pf)
{
const nvlist_t *iov;
iov = nvlist_get_nvlist(pf, IOV_CONFIG_NAME);
return (nvlist_get_number(iov, "num_vfs"));
}
/*
* Validates the configuration that has been parsed into config using the given
* config schema. Note that the parser is required to not insert configuration
* keys that are not valid in the schema, and to not insert configuration values
* that are of the incorrect type. Therefore this function will not validate
* either condition. This function is only responsible for inserting config
* file defaults in individual VF sections and removing the DEFAULT_SCHEMA_NAME
* subsystem from config, validating that all required parameters in the schema
* are present in each PF and VF subsystem, and that there is no VF subsystem
* section whose number exceeds num_vfs.
*/
void
validate_config(nvlist_t *config, const nvlist_t *schema, const regex_t *vf_pat)
{
char device_name[VF_MAX_NAME];
regmatch_t matches[2];
nvlist_t *defaults, *pf, *vf;
const nvlist_t *vf_schema;
const char *key;
void *cookie;
int i, type;
uint16_t vf_num, num_vfs;
pf = find_config(config, PF_CONFIG_NAME);
validate_device(pf, nvlist_get_nvlist(schema, PF_CONFIG_NAME),
PF_CONFIG_NAME);
nvlist_move_nvlist(config, PF_CONFIG_NAME, pf);
num_vfs = get_num_vfs(pf);
vf_schema = nvlist_get_nvlist(schema, VF_SCHEMA_NAME);
if (num_vfs == 0)
errx(1, "PF.num_vfs must be at least 1");
defaults = dnvlist_take_nvlist(config, DEFAULT_SCHEMA_NAME, NULL);
for (i = 0; i < num_vfs; i++) {
snprintf(device_name, sizeof(device_name), VF_PREFIX"%d",
i);
vf = find_config(config, device_name);
if (defaults != NULL)
apply_defaults(vf, defaults);
validate_device(vf, vf_schema, device_name);
nvlist_move_nvlist(config, device_name, vf);
}
nvlist_destroy(defaults);
cookie = NULL;
while ((key = nvlist_next(config, &type, &cookie)) != NULL) {
if (regexec(vf_pat, key, nitems(matches), matches, 0) == 0) {
vf_num = parse_vf_num(key, matches);
if (vf_num >= num_vfs)
errx(1,
"VF number %d is out of bounds (num_vfs=%d)",
vf_num, num_vfs);
}
}
}
|