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/param.h>
43 static void config_action(const char *filename, int dryrun);
44 static void delete_action(const char *device, int dryrun);
45 static void print_schema(const char *device);
48 * Fetch the config schema from the kernel via ioctl. This function has to
49 * call the ioctl twice: the first returns the amount of memory that we need
50 * to allocate for the schema, and the second actually fetches the schema.
55 struct pci_iov_schema arg;
59 /* Do the ioctl() once to fetch the size of the schema. */
63 error = ioctl(fd, IOV_GET_SCHEMA, &arg);
65 err(1, "Could not fetch size of config schema");
67 arg.schema = malloc(arg.len);
68 if (arg.schema == NULL)
69 err(1, "Could not allocate %zu bytes for schema",
72 /* Now do the ioctl() for real to get the schema. */
73 error = ioctl(fd, IOV_GET_SCHEMA, &arg);
74 if (error != 0 || arg.error != 0) {
77 err(1, "Could not fetch config schema");
80 schema = nvlist_unpack(arg.schema, arg.len, NV_FLAG_IGNORE_CASE);
82 err(1, "Could not unpack schema");
89 * Call the ioctl that activates SR-IOV and creates the VFs.
92 config_iov(int fd, const char *dev_name, const nvlist_t *config, int dryrun)
94 struct pci_iov_arg arg;
97 arg.config = nvlist_pack(config, &arg.len);
98 if (arg.config == NULL)
99 err(1, "Could not pack configuration");
102 printf("Would enable SR-IOV on device '%s'.\n", dev_name);
104 "The following configuration parameters would be used:\n");
105 nvlist_fdump(config, stdout);
107 "The configuration parameters consume %zu bytes when packed.\n",
110 error = ioctl(fd, IOV_CONFIG, &arg);
112 err(1, "Failed to configure SR-IOV");
119 open_device(const char *dev_name)
126 path_max = pathconf("/dev", _PC_PATH_MAX);
128 err(1, "Could not get maximum path length");
133 err(1, "Could not allocate memory for device path");
135 if (dev_name[0] == '/')
136 copied = strlcpy(dev, dev_name, size);
138 copied = snprintf(dev, size, "/dev/iov/%s", dev_name);
140 /* >= to account for null terminator. */
142 errx(1, "Provided file name too long");
144 fd = open(dev, O_RDWR);
146 err(1, "Could not open device '%s'", dev);
156 warnx("Usage: iovctl -C -f <config file> [-n]");
157 warnx(" iovctl -D [-d <PF device> | -f <config file>] [-n]");
158 warnx(" iovctl -S [-d <PF device> | -f <config file>]");
171 main(int argc, char **argv)
174 const char *filename;
176 enum main_action action;
183 while ((ch = getopt(argc, argv, "Cd:Df:nS")) != -1) {
186 if (action != NONE) {
188 "Only one of -C, -D or -S may be specified");
194 device = strdup(optarg);
197 if (action != NONE) {
199 "Only one of -C, -D or -S may be specified");
211 if (action != NONE) {
213 "Only one of -C, -D or -S may be specified");
216 action = PRINT_SCHEMA;
219 warnx("Unrecognized argument '-%c'\n", optopt);
225 if (device != NULL && filename != NULL) {
226 warnx("Only one of the -d and -f flags may be specified");
230 if (device == NULL && filename == NULL && action != CONFIG) {
231 warnx("Either the -d or -f flag must be specified");
237 if (device != NULL) {
238 warnx("-d flag cannot be used with the -C flag");
241 if (filename == NULL) {
242 warnx("The -f flag must be specified");
245 config_action(filename, dryrun);
249 device = find_device(filename);
250 delete_action(device, dryrun);
255 warnx("-n flag cannot be used with the -S flag");
259 device = find_device(filename);
260 print_schema(device);
272 config_action(const char *filename, int dryrun)
275 nvlist_t *schema, *config;
278 dev = find_device(filename);
279 fd = open(dev, O_RDWR);
281 err(1, "Could not open device '%s'", dev);
283 schema = get_schema(fd);
284 config = parse_config_file(filename, schema);
286 errx(1, "Could not parse config");
288 config_iov(fd, dev, config, dryrun);
290 nvlist_destroy(config);
291 nvlist_destroy(schema);
297 delete_action(const char *dev_name, int dryrun)
301 fd = open_device(dev_name);
304 printf("Would attempt to delete all VF children of '%s'\n",
307 error = ioctl(fd, IOV_DELETE);
309 err(1, "Failed to delete VFs");
316 print_default_value(const nvlist_t *parameter, const char *type)
321 if (strcasecmp(type, "bool") == 0)
322 printf(" (default = %s)",
323 nvlist_get_bool(parameter, DEFAULT_SCHEMA_NAME) ? "true" :
325 else if (strcasecmp(type, "string") == 0)
326 printf(" (default = %s)",
327 nvlist_get_string(parameter, DEFAULT_SCHEMA_NAME));
328 else if (strcasecmp(type, "uint8_t") == 0)
329 printf(" (default = %ju)",
330 (uintmax_t)nvlist_get_number(parameter,
331 DEFAULT_SCHEMA_NAME));
332 else if (strcasecmp(type, "uint16_t") == 0)
333 printf(" (default = %ju)",
334 (uintmax_t)nvlist_get_number(parameter,
335 DEFAULT_SCHEMA_NAME));
336 else if (strcasecmp(type, "uint32_t") == 0)
337 printf(" (default = %ju)",
338 (uintmax_t)nvlist_get_number(parameter,
339 DEFAULT_SCHEMA_NAME));
340 else if (strcasecmp(type, "uint64_t") == 0)
341 printf(" (default = %ju)",
342 (uintmax_t)nvlist_get_number(parameter,
343 DEFAULT_SCHEMA_NAME));
344 else if (strcasecmp(type, "unicast-mac") == 0) {
345 mac = nvlist_get_binary(parameter, DEFAULT_SCHEMA_NAME, &size);
346 printf(" (default = %02x:%02x:%02x:%02x:%02x:%02x)", mac[0],
347 mac[1], mac[2], mac[3], mac[4], mac[5]);
349 errx(1, "Unexpected type in schema: '%s'", type);
353 print_subsystem_schema(const nvlist_t * subsystem_schema)
355 const char *name, *type;
356 const nvlist_t *parameter;
361 while ((name = nvlist_next(subsystem_schema, &nvtype, &it)) != NULL) {
362 parameter = nvlist_get_nvlist(subsystem_schema, name);
363 type = nvlist_get_string(parameter, TYPE_SCHEMA_NAME);
365 printf("\t%s : %s", name, type);
366 if (dnvlist_get_bool(parameter, REQUIRED_SCHEMA_NAME, false))
367 printf(" (required)");
368 else if (nvlist_exists(parameter, DEFAULT_SCHEMA_NAME))
369 print_default_value(parameter, type);
371 printf(" (optional)");
377 print_schema(const char *dev_name)
380 const nvlist_t *iov_schema, *driver_schema, *pf_schema, *vf_schema;
383 fd = open_device(dev_name);
384 schema = get_schema(fd);
386 pf_schema = nvlist_get_nvlist(schema, PF_CONFIG_NAME);
387 iov_schema = nvlist_get_nvlist(pf_schema, IOV_CONFIG_NAME);
388 driver_schema = nvlist_get_nvlist(pf_schema, DRIVER_CONFIG_NAME);
390 "The following configuration parameters may be configured on the PF:\n");
391 print_subsystem_schema(iov_schema);
392 print_subsystem_schema(driver_schema);
394 vf_schema = nvlist_get_nvlist(schema, VF_SCHEMA_NAME);
395 iov_schema = nvlist_get_nvlist(vf_schema, IOV_CONFIG_NAME);
396 driver_schema = nvlist_get_nvlist(vf_schema, DRIVER_CONFIG_NAME);
398 "\nThe following configuration parameters may be configured on a VF:\n");
399 print_subsystem_schema(iov_schema);
400 print_subsystem_schema(driver_schema);
402 nvlist_destroy(schema);