devargs: introduce removal function
[dpdk.git] / lib / librte_eal / common / eal_common_devargs.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright 2014 6WIND S.A.
5  *
6  *   Redistribution and use in source and binary forms, with or without
7  *   modification, are permitted provided that the following conditions
8  *   are met:
9  *
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
15  *       distribution.
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.
19  *
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.
31  */
32
33 /* This file manages the list of devices and their arguments, as given
34  * by the user at startup
35  *
36  * Code here should not call rte_log since the EAL environment
37  * may not be initialized.
38  */
39
40 #include <stdio.h>
41 #include <string.h>
42
43 #include <rte_devargs.h>
44 #include <rte_tailq.h>
45 #include "eal_private.h"
46
47 /** Global list of user devices */
48 struct rte_devargs_list devargs_list =
49         TAILQ_HEAD_INITIALIZER(devargs_list);
50
51 int
52 rte_eal_parse_devargs_str(const char *devargs_str,
53                         char **drvname, char **drvargs)
54 {
55         char *sep;
56
57         if ((devargs_str) == NULL || (drvname) == NULL || (drvargs == NULL))
58                 return -1;
59
60         *drvname = strdup(devargs_str);
61         if (*drvname == NULL)
62                 return -1;
63
64         /* set the first ',' to '\0' to split name and arguments */
65         sep = strchr(*drvname, ',');
66         if (sep != NULL) {
67                 sep[0] = '\0';
68                 *drvargs = strdup(sep + 1);
69         } else {
70                 *drvargs = strdup("");
71         }
72
73         if (*drvargs == NULL) {
74                 free(*drvname);
75                 *drvname = NULL;
76                 return -1;
77         }
78         return 0;
79 }
80
81 static int
82 bus_name_cmp(const struct rte_bus *bus, const void *name)
83 {
84         return strncmp(bus->name, name, strlen(bus->name));
85 }
86
87 int
88 rte_eal_devargs_parse(const char *dev, struct rte_devargs *da)
89 {
90         struct rte_bus *bus = NULL;
91         const char *devname;
92         const size_t maxlen = sizeof(da->name);
93         size_t i;
94
95         if (dev == NULL || da == NULL)
96                 return -EINVAL;
97         /* Retrieve eventual bus info */
98         do {
99                 devname = dev;
100                 bus = rte_bus_find(bus, bus_name_cmp, dev);
101                 if (bus == NULL)
102                         break;
103                 devname = dev + strlen(bus->name) + 1;
104                 if (rte_bus_find_by_device_name(devname) == bus)
105                         break;
106         } while (1);
107         /* Store device name */
108         i = 0;
109         while (devname[i] != '\0' && devname[i] != ',') {
110                 da->name[i] = devname[i];
111                 i++;
112                 if (i == maxlen) {
113                         fprintf(stderr, "WARNING: Parsing \"%s\": device name should be shorter than %zu\n",
114                                 dev, maxlen);
115                         da->name[i - 1] = '\0';
116                         return -EINVAL;
117                 }
118         }
119         da->name[i] = '\0';
120         if (bus == NULL) {
121                 bus = rte_bus_find_by_device_name(da->name);
122                 if (bus == NULL) {
123                         fprintf(stderr, "ERROR: failed to parse device \"%s\"\n",
124                                 da->name);
125                         return -EFAULT;
126                 }
127         }
128         da->bus = bus;
129         /* Parse eventual device arguments */
130         if (devname[i] == ',')
131                 da->args = strdup(&devname[i + 1]);
132         else
133                 da->args = strdup("");
134         if (da->args == NULL) {
135                 fprintf(stderr, "ERROR: not enough memory to parse arguments\n");
136                 return -ENOMEM;
137         }
138         return 0;
139 }
140
141 /* store a whitelist parameter for later parsing */
142 int
143 rte_eal_devargs_add(enum rte_devtype devtype, const char *devargs_str)
144 {
145         struct rte_devargs *devargs = NULL;
146         struct rte_bus *bus = NULL;
147         const char *dev = devargs_str;
148
149         /* use calloc instead of rte_zmalloc as it's called early at init */
150         devargs = calloc(1, sizeof(*devargs));
151         if (devargs == NULL)
152                 goto fail;
153
154         if (rte_eal_devargs_parse(dev, devargs))
155                 goto fail;
156         devargs->type = devtype;
157         bus = devargs->bus;
158         if (devargs->type == RTE_DEVTYPE_WHITELISTED) {
159                 if (bus->conf.scan_mode == RTE_BUS_SCAN_UNDEFINED) {
160                         bus->conf.scan_mode = RTE_BUS_SCAN_WHITELIST;
161                 } else if (bus->conf.scan_mode == RTE_BUS_SCAN_BLACKLIST) {
162                         fprintf(stderr, "ERROR: incompatible device type and bus scan mode\n");
163                         goto fail;
164                 }
165         } else if (devargs->type == RTE_DEVTYPE_BLACKLISTED) {
166                 if (bus->conf.scan_mode == RTE_BUS_SCAN_UNDEFINED) {
167                         bus->conf.scan_mode = RTE_BUS_SCAN_BLACKLIST;
168                 } else if (bus->conf.scan_mode == RTE_BUS_SCAN_WHITELIST) {
169                         fprintf(stderr, "ERROR: incompatible device type and bus scan mode\n");
170                         goto fail;
171                 }
172         }
173
174         TAILQ_INSERT_TAIL(&devargs_list, devargs, next);
175         return 0;
176
177 fail:
178         if (devargs) {
179                 free(devargs->args);
180                 free(devargs);
181         }
182
183         return -1;
184 }
185
186 int
187 rte_eal_devargs_remove(const char *busname, const char *devname)
188 {
189         struct rte_devargs *d;
190         void *tmp;
191
192         TAILQ_FOREACH_SAFE(d, &devargs_list, next, tmp) {
193                 if (strcmp(d->bus->name, busname) == 0 &&
194                     strcmp(d->name, devname) == 0) {
195                         TAILQ_REMOVE(&devargs_list, d, next);
196                         free(d->args);
197                         free(d);
198                         return 0;
199                 }
200         }
201         return 1;
202 }
203
204 /* count the number of devices of a specified type */
205 unsigned int
206 rte_eal_devargs_type_count(enum rte_devtype devtype)
207 {
208         struct rte_devargs *devargs;
209         unsigned int count = 0;
210
211         TAILQ_FOREACH(devargs, &devargs_list, next) {
212                 if (devargs->type != devtype)
213                         continue;
214                 count++;
215         }
216         return count;
217 }
218
219 /* dump the user devices on the console */
220 void
221 rte_eal_devargs_dump(FILE *f)
222 {
223         struct rte_devargs *devargs;
224
225         fprintf(f, "User device list:\n");
226         TAILQ_FOREACH(devargs, &devargs_list, next) {
227                 fprintf(f, "  [%s]: %s %s\n",
228                         (devargs->bus ? devargs->bus->name : "??"),
229                         devargs->name, devargs->args);
230         }
231 }