2 * Copyright (c) 2013-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 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
46 static void config_action(const char *filename, int dryrun);
47 static void delete_action(const char *device, int dryrun);
48 static void print_schema(const char *device);
51 * Fetch the config schema from the kernel via ioctl. This function has to
52 * call the ioctl twice: the first returns the amount of memory that we need
53 * to allocate for the schema, and the second actually fetches the schema.
58 struct pci_iov_schema arg;
62 /* Do the ioctl() once to fetch the size of the schema. */
66 error = ioctl(fd, IOV_GET_SCHEMA, &arg);
68 err(1, "Could not fetch size of config schema");
70 arg.schema = malloc(arg.len);
71 if (arg.schema == NULL)
72 err(1, "Could not allocate %zu bytes for schema",
75 /* Now do the ioctl() for real to get the schema. */
76 error = ioctl(fd, IOV_GET_SCHEMA, &arg);
77 if (error != 0 || arg.error != 0) {
80 err(1, "Could not fetch config schema");
83 schema = nvlist_unpack(arg.schema, arg.len, NV_FLAG_IGNORE_CASE);
85 err(1, "Could not unpack schema");
92 * Call the ioctl that activates SR-IOV and creates the VFs.
95 config_iov(int fd, const char *dev_name, const nvlist_t *config, int dryrun)
97 struct pci_iov_arg arg;
100 arg.config = nvlist_pack(config, &arg.len);
101 if (arg.config == NULL)
102 err(1, "Could not pack configuration");
105 printf("Would enable SR-IOV on device '%s'.\n", dev_name);
107 "The following configuration parameters would be used:\n");
108 nvlist_fdump(config, stdout);
110 "The configuration parameters consume %zu bytes when packed.\n",
113 error = ioctl(fd, IOV_CONFIG, &arg);
115 err(1, "Failed to configure SR-IOV");
122 open_device(const char *dev_name)
129 path_max = pathconf("/dev", _PC_PATH_MAX);
131 err(1, "Could not get maximum path length");
136 err(1, "Could not allocate memory for device path");
138 if (dev_name[0] == '/')
139 copied = strlcpy(dev, dev_name, size);
141 copied = snprintf(dev, size, "/dev/iov/%s", dev_name);
143 /* >= to account for null terminator. */
145 errx(1, "Provided file name too long");
147 fd = open(dev, O_RDWR);
149 err(1, "Could not open device '%s'", dev);
159 warnx("Usage: iovctl -C -f <config file> [-n]");
160 warnx(" iovctl -D [-d <PF device> | -f <config file>] [-n]");
161 warnx(" iovctl -S [-d <PF device> | -f <config file>]");
174 main(int argc, char **argv)
177 const char *filename;
179 enum main_action action;
186 while ((ch = getopt(argc, argv, "Cd:Df:nS")) != -1) {
189 if (action != NONE) {
191 "Only one of -C, -D or -S may be specified");
197 device = strdup(optarg);
200 if (action != NONE) {
202 "Only one of -C, -D or -S may be specified");
214 if (action != NONE) {
216 "Only one of -C, -D or -S may be specified");
219 action = PRINT_SCHEMA;
222 warnx("Unrecognized argument '-%c'\n", optopt);
228 if (device != NULL && filename != NULL) {
229 warnx("Only one of the -d and -f flags may be specified");
233 if (device == NULL && filename == NULL && action != CONFIG) {
234 warnx("Either the -d or -f flag must be specified");
240 if (device != NULL) {
241 warnx("-d flag cannot be used with the -C flag");
244 if (filename == NULL) {
245 warnx("The -f flag must be specified");
248 config_action(filename, dryrun);
252 device = find_device(filename);
253 delete_action(device, dryrun);
258 warnx("-n flag cannot be used with the -S flag");
262 device = find_device(filename);
263 print_schema(device);
275 config_action(const char *filename, int dryrun)
278 nvlist_t *schema, *config;
281 dev = find_device(filename);
282 fd = open(dev, O_RDWR);
284 err(1, "Could not open device '%s'", dev);
286 schema = get_schema(fd);
287 config = parse_config_file(filename, schema);
289 errx(1, "Could not parse config");
291 config_iov(fd, dev, config, dryrun);
293 nvlist_destroy(config);
294 nvlist_destroy(schema);
300 delete_action(const char *dev_name, int dryrun)
304 fd = open_device(dev_name);
307 printf("Would attempt to delete all VF children of '%s'\n",
310 error = ioctl(fd, IOV_DELETE);
312 err(1, "Failed to delete VFs");
319 print_default_value(const nvlist_t *parameter, const char *type)
324 if (strcasecmp(type, "bool") == 0)
325 printf(" (default = %s)",
326 nvlist_get_bool(parameter, DEFAULT_SCHEMA_NAME) ? "true" :
328 else if (strcasecmp(type, "string") == 0)
329 printf(" (default = %s)",
330 nvlist_get_string(parameter, DEFAULT_SCHEMA_NAME));
331 else if (strcasecmp(type, "uint8_t") == 0)
332 printf(" (default = %ju)",
333 (uintmax_t)nvlist_get_number(parameter,
334 DEFAULT_SCHEMA_NAME));
335 else if (strcasecmp(type, "uint16_t") == 0)
336 printf(" (default = %ju)",
337 (uintmax_t)nvlist_get_number(parameter,
338 DEFAULT_SCHEMA_NAME));
339 else if (strcasecmp(type, "uint32_t") == 0)
340 printf(" (default = %ju)",
341 (uintmax_t)nvlist_get_number(parameter,
342 DEFAULT_SCHEMA_NAME));
343 else if (strcasecmp(type, "uint64_t") == 0)
344 printf(" (default = %ju)",
345 (uintmax_t)nvlist_get_number(parameter,
346 DEFAULT_SCHEMA_NAME));
347 else if (strcasecmp(type, "unicast-mac") == 0) {
348 mac = nvlist_get_binary(parameter, DEFAULT_SCHEMA_NAME, &size);
349 printf(" (default = %02x:%02x:%02x:%02x:%02x:%02x)", mac[0],
350 mac[1], mac[2], mac[3], mac[4], mac[5]);
352 errx(1, "Unexpected type in schema: '%s'", type);
356 print_subsystem_schema(const nvlist_t * subsystem_schema)
358 const char *name, *type;
359 const nvlist_t *parameter;
364 while ((name = nvlist_next(subsystem_schema, &nvtype, &it)) != NULL) {
365 parameter = nvlist_get_nvlist(subsystem_schema, name);
366 type = nvlist_get_string(parameter, TYPE_SCHEMA_NAME);
368 printf("\t%s : %s", name, type);
369 if (dnvlist_get_bool(parameter, REQUIRED_SCHEMA_NAME, false))
370 printf(" (required)");
371 else if (nvlist_exists(parameter, DEFAULT_SCHEMA_NAME))
372 print_default_value(parameter, type);
374 printf(" (optional)");
380 print_schema(const char *dev_name)
383 const nvlist_t *iov_schema, *driver_schema, *pf_schema, *vf_schema;
386 fd = open_device(dev_name);
387 schema = get_schema(fd);
389 pf_schema = nvlist_get_nvlist(schema, PF_CONFIG_NAME);
390 iov_schema = nvlist_get_nvlist(pf_schema, IOV_CONFIG_NAME);
391 driver_schema = nvlist_get_nvlist(pf_schema, DRIVER_CONFIG_NAME);
393 "The following configuration parameters may be configured on the PF:\n");
394 print_subsystem_schema(iov_schema);
395 print_subsystem_schema(driver_schema);
397 vf_schema = nvlist_get_nvlist(schema, VF_SCHEMA_NAME);
398 iov_schema = nvlist_get_nvlist(vf_schema, IOV_CONFIG_NAME);
399 driver_schema = nvlist_get_nvlist(vf_schema, DRIVER_CONFIG_NAME);
401 "\nThe following configuration parameters may be configured on a VF:\n");
402 print_subsystem_schema(iov_schema);
403 print_subsystem_schema(driver_schema);
405 nvlist_destroy(schema);