4 * Copyright 2014 6WIND S.A.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
16 * * Neither the name of 6WIND S.A nor the names of its contributors
17 * may be used to endorse or promote products derived from this
18 * software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 /* This file manages the list of devices and their arguments, as given
34 * by the user at startup
36 * Code here should not call rte_log since the EAL environment
37 * may not be initialized.
44 #include <rte_devargs.h>
45 #include <rte_tailq.h>
46 #include "eal_private.h"
48 /** Global list of user devices */
49 struct rte_devargs_list devargs_list =
50 TAILQ_HEAD_INITIALIZER(devargs_list);
53 rte_eal_parse_devargs_str(const char *devargs_str,
54 char **drvname, char **drvargs)
58 if ((devargs_str) == NULL || (drvname) == NULL || (drvargs == NULL))
61 *drvname = strdup(devargs_str);
65 /* set the first ',' to '\0' to split name and arguments */
66 sep = strchr(*drvname, ',');
69 *drvargs = strdup(sep + 1);
71 *drvargs = strdup("");
74 if (*drvargs == NULL) {
83 bus_name_cmp(const struct rte_bus *bus, const void *name)
85 return strncmp(bus->name, name, strlen(bus->name));
89 rte_eal_devargs_parse(const char *dev, struct rte_devargs *da)
91 struct rte_bus *bus = NULL;
93 const size_t maxlen = sizeof(da->name);
96 if (dev == NULL || da == NULL)
98 /* Retrieve eventual bus info */
101 bus = rte_bus_find(bus, bus_name_cmp, dev);
104 devname = dev + strlen(bus->name) + 1;
105 if (rte_bus_find_by_device_name(devname) == bus)
108 /* Store device name */
110 while (devname[i] != '\0' && devname[i] != ',') {
111 da->name[i] = devname[i];
114 fprintf(stderr, "WARNING: Parsing \"%s\": device name should be shorter than %zu\n",
116 da->name[i - 1] = '\0';
122 bus = rte_bus_find_by_device_name(da->name);
124 fprintf(stderr, "ERROR: failed to parse device \"%s\"\n",
130 /* Parse eventual device arguments */
131 if (devname[i] == ',')
132 da->args = strdup(&devname[i + 1]);
134 da->args = strdup("");
135 if (da->args == NULL) {
136 fprintf(stderr, "ERROR: not enough memory to parse arguments\n");
143 rte_eal_devargs_insert(struct rte_devargs *da)
147 ret = rte_eal_devargs_remove(da->bus->name, da->name);
150 TAILQ_INSERT_TAIL(&devargs_list, da, next);
154 /* store a whitelist parameter for later parsing */
156 rte_eal_devargs_add(enum rte_devtype devtype, const char *devargs_str)
158 struct rte_devargs *devargs = NULL;
159 struct rte_bus *bus = NULL;
160 const char *dev = devargs_str;
162 /* use calloc instead of rte_zmalloc as it's called early at init */
163 devargs = calloc(1, sizeof(*devargs));
167 if (rte_eal_devargs_parse(dev, devargs))
169 devargs->type = devtype;
171 if (devargs->type == RTE_DEVTYPE_BLACKLISTED_PCI)
172 devargs->policy = RTE_DEV_BLACKLISTED;
173 if (bus->conf.scan_mode == RTE_BUS_SCAN_UNDEFINED) {
174 if (devargs->policy == RTE_DEV_WHITELISTED)
175 bus->conf.scan_mode = RTE_BUS_SCAN_WHITELIST;
176 else if (devargs->policy == RTE_DEV_BLACKLISTED)
177 bus->conf.scan_mode = RTE_BUS_SCAN_BLACKLIST;
179 TAILQ_INSERT_TAIL(&devargs_list, devargs, next);
192 rte_eal_devargs_remove(const char *busname, const char *devname)
194 struct rte_devargs *d;
197 TAILQ_FOREACH_SAFE(d, &devargs_list, next, tmp) {
198 if (strcmp(d->bus->name, busname) == 0 &&
199 strcmp(d->name, devname) == 0) {
200 TAILQ_REMOVE(&devargs_list, d, next);
209 /* count the number of devices of a specified type */
211 rte_eal_devargs_type_count(enum rte_devtype devtype)
213 struct rte_devargs *devargs;
214 unsigned int count = 0;
216 TAILQ_FOREACH(devargs, &devargs_list, next) {
217 if (devargs->type != devtype)
224 /* dump the user devices on the console */
226 rte_eal_devargs_dump(FILE *f)
228 struct rte_devargs *devargs;
230 fprintf(f, "User device list:\n");
231 TAILQ_FOREACH(devargs, &devargs_list, next) {
232 fprintf(f, " [%s]: %s %s\n",
233 (devargs->bus ? devargs->bus->name : "??"),
234 devargs->name, devargs->args);