1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2014 6WIND S.A.
5 /* This file manages the list of devices and their arguments, as given
6 * by the user at startup
8 * Code here should not call rte_log since the EAL environment
9 * may not be initialized.
15 #include <rte_compat.h>
17 #include <rte_devargs.h>
18 #include <rte_tailq.h>
19 #include "eal_private.h"
21 /** Global list of user devices */
22 struct rte_devargs_list devargs_list =
23 TAILQ_HEAD_INITIALIZER(devargs_list);
26 rte_eal_parse_devargs_str(const char *devargs_str,
27 char **drvname, char **drvargs)
31 if ((devargs_str) == NULL || (drvname) == NULL || (drvargs == NULL))
34 *drvname = strdup(devargs_str);
38 /* set the first ',' to '\0' to split name and arguments */
39 sep = strchr(*drvname, ',');
42 *drvargs = strdup(sep + 1);
44 *drvargs = strdup("");
47 if (*drvargs == NULL) {
56 bus_name_cmp(const struct rte_bus *bus, const void *name)
58 return strncmp(bus->name, name, strlen(bus->name));
61 int __rte_experimental
62 rte_eal_devargs_parse(const char *dev, struct rte_devargs *da)
64 struct rte_bus *bus = NULL;
66 const size_t maxlen = sizeof(da->name);
69 if (dev == NULL || da == NULL)
71 /* Retrieve eventual bus info */
74 bus = rte_bus_find(bus, bus_name_cmp, dev);
77 devname = dev + strlen(bus->name) + 1;
78 if (rte_bus_find_by_device_name(devname) == bus)
81 /* Store device name */
83 while (devname[i] != '\0' && devname[i] != ',') {
84 da->name[i] = devname[i];
87 fprintf(stderr, "WARNING: Parsing \"%s\": device name should be shorter than %zu\n",
89 da->name[i - 1] = '\0';
95 bus = rte_bus_find_by_device_name(da->name);
97 fprintf(stderr, "ERROR: failed to parse device \"%s\"\n",
103 /* Parse eventual device arguments */
104 if (devname[i] == ',')
105 da->args = strdup(&devname[i + 1]);
107 da->args = strdup("");
108 if (da->args == NULL) {
109 fprintf(stderr, "ERROR: not enough memory to parse arguments\n");
115 int __rte_experimental
116 rte_eal_devargs_insert(struct rte_devargs *da)
120 ret = rte_eal_devargs_remove(da->bus->name, da->name);
123 TAILQ_INSERT_TAIL(&devargs_list, da, next);
127 /* store a whitelist parameter for later parsing */
129 rte_eal_devargs_add(enum rte_devtype devtype, const char *devargs_str)
131 struct rte_devargs *devargs = NULL;
132 struct rte_bus *bus = NULL;
133 const char *dev = devargs_str;
135 /* use calloc instead of rte_zmalloc as it's called early at init */
136 devargs = calloc(1, sizeof(*devargs));
140 if (rte_eal_devargs_parse(dev, devargs))
142 devargs->type = devtype;
144 if (devargs->type == RTE_DEVTYPE_BLACKLISTED_PCI)
145 devargs->policy = RTE_DEV_BLACKLISTED;
146 if (bus->conf.scan_mode == RTE_BUS_SCAN_UNDEFINED) {
147 if (devargs->policy == RTE_DEV_WHITELISTED)
148 bus->conf.scan_mode = RTE_BUS_SCAN_WHITELIST;
149 else if (devargs->policy == RTE_DEV_BLACKLISTED)
150 bus->conf.scan_mode = RTE_BUS_SCAN_BLACKLIST;
152 TAILQ_INSERT_TAIL(&devargs_list, devargs, next);
164 int __rte_experimental
165 rte_eal_devargs_remove(const char *busname, const char *devname)
167 struct rte_devargs *d;
170 TAILQ_FOREACH_SAFE(d, &devargs_list, next, tmp) {
171 if (strcmp(d->bus->name, busname) == 0 &&
172 strcmp(d->name, devname) == 0) {
173 TAILQ_REMOVE(&devargs_list, d, next);
182 /* count the number of devices of a specified type */
184 rte_eal_devargs_type_count(enum rte_devtype devtype)
186 struct rte_devargs *devargs;
187 unsigned int count = 0;
189 TAILQ_FOREACH(devargs, &devargs_list, next) {
190 if (devargs->type != devtype)
197 /* dump the user devices on the console */
199 rte_eal_devargs_dump(FILE *f)
201 struct rte_devargs *devargs;
203 fprintf(f, "User device list:\n");
204 TAILQ_FOREACH(devargs, &devargs_list, next) {
205 fprintf(f, " [%s]: %s %s\n",
206 (devargs->bus ? devargs->bus->name : "??"),
207 devargs->name, devargs->args);