3db07ee9997262a868ab3fd9a873f251cc978742
[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_compat.h>
44 #include <rte_dev.h>
45 #include <rte_devargs.h>
46 #include <rte_tailq.h>
47 #include "eal_private.h"
48
49 /** Global list of user devices */
50 struct rte_devargs_list devargs_list =
51         TAILQ_HEAD_INITIALIZER(devargs_list);
52
53 int
54 rte_eal_parse_devargs_str(const char *devargs_str,
55                         char **drvname, char **drvargs)
56 {
57         char *sep;
58
59         if ((devargs_str) == NULL || (drvname) == NULL || (drvargs == NULL))
60                 return -1;
61
62         *drvname = strdup(devargs_str);
63         if (*drvname == NULL)
64                 return -1;
65
66         /* set the first ',' to '\0' to split name and arguments */
67         sep = strchr(*drvname, ',');
68         if (sep != NULL) {
69                 sep[0] = '\0';
70                 *drvargs = strdup(sep + 1);
71         } else {
72                 *drvargs = strdup("");
73         }
74
75         if (*drvargs == NULL) {
76                 free(*drvname);
77                 *drvname = NULL;
78                 return -1;
79         }
80         return 0;
81 }
82
83 static int
84 bus_name_cmp(const struct rte_bus *bus, const void *name)
85 {
86         return strncmp(bus->name, name, strlen(bus->name));
87 }
88
89 int __rte_experimental
90 rte_eal_devargs_parse(const char *dev, struct rte_devargs *da)
91 {
92         struct rte_bus *bus = NULL;
93         const char *devname;
94         const size_t maxlen = sizeof(da->name);
95         size_t i;
96
97         if (dev == NULL || da == NULL)
98                 return -EINVAL;
99         /* Retrieve eventual bus info */
100         do {
101                 devname = dev;
102                 bus = rte_bus_find(bus, bus_name_cmp, dev);
103                 if (bus == NULL)
104                         break;
105                 devname = dev + strlen(bus->name) + 1;
106                 if (rte_bus_find_by_device_name(devname) == bus)
107                         break;
108         } while (1);
109         /* Store device name */
110         i = 0;
111         while (devname[i] != '\0' && devname[i] != ',') {
112                 da->name[i] = devname[i];
113                 i++;
114                 if (i == maxlen) {
115                         fprintf(stderr, "WARNING: Parsing \"%s\": device name should be shorter than %zu\n",
116                                 dev, maxlen);
117                         da->name[i - 1] = '\0';
118                         return -EINVAL;
119                 }
120         }
121         da->name[i] = '\0';
122         if (bus == NULL) {
123                 bus = rte_bus_find_by_device_name(da->name);
124                 if (bus == NULL) {
125                         fprintf(stderr, "ERROR: failed to parse device \"%s\"\n",
126                                 da->name);
127                         return -EFAULT;
128                 }
129         }
130         da->bus = bus;
131         /* Parse eventual device arguments */
132         if (devname[i] == ',')
133                 da->args = strdup(&devname[i + 1]);
134         else
135                 da->args = strdup("");
136         if (da->args == NULL) {
137                 fprintf(stderr, "ERROR: not enough memory to parse arguments\n");
138                 return -ENOMEM;
139         }
140         return 0;
141 }
142
143 int __rte_experimental
144 rte_eal_devargs_insert(struct rte_devargs *da)
145 {
146         int ret;
147
148         ret = rte_eal_devargs_remove(da->bus->name, da->name);
149         if (ret < 0)
150                 return ret;
151         TAILQ_INSERT_TAIL(&devargs_list, da, next);
152         return 0;
153 }
154
155 /* store a whitelist parameter for later parsing */
156 int
157 rte_eal_devargs_add(enum rte_devtype devtype, const char *devargs_str)
158 {
159         struct rte_devargs *devargs = NULL;
160         struct rte_bus *bus = NULL;
161         const char *dev = devargs_str;
162
163         /* use calloc instead of rte_zmalloc as it's called early at init */
164         devargs = calloc(1, sizeof(*devargs));
165         if (devargs == NULL)
166                 goto fail;
167
168         if (rte_eal_devargs_parse(dev, devargs))
169                 goto fail;
170         devargs->type = devtype;
171         bus = devargs->bus;
172         if (devargs->type == RTE_DEVTYPE_BLACKLISTED_PCI)
173                 devargs->policy = RTE_DEV_BLACKLISTED;
174         if (bus->conf.scan_mode == RTE_BUS_SCAN_UNDEFINED) {
175                 if (devargs->policy == RTE_DEV_WHITELISTED)
176                         bus->conf.scan_mode = RTE_BUS_SCAN_WHITELIST;
177                 else if (devargs->policy == RTE_DEV_BLACKLISTED)
178                         bus->conf.scan_mode = RTE_BUS_SCAN_BLACKLIST;
179         }
180         TAILQ_INSERT_TAIL(&devargs_list, devargs, next);
181         return 0;
182
183 fail:
184         if (devargs) {
185                 free(devargs->args);
186                 free(devargs);
187         }
188
189         return -1;
190 }
191
192 int __rte_experimental
193 rte_eal_devargs_remove(const char *busname, const char *devname)
194 {
195         struct rte_devargs *d;
196         void *tmp;
197
198         TAILQ_FOREACH_SAFE(d, &devargs_list, next, tmp) {
199                 if (strcmp(d->bus->name, busname) == 0 &&
200                     strcmp(d->name, devname) == 0) {
201                         TAILQ_REMOVE(&devargs_list, d, next);
202                         free(d->args);
203                         free(d);
204                         return 0;
205                 }
206         }
207         return 1;
208 }
209
210 /* count the number of devices of a specified type */
211 unsigned int
212 rte_eal_devargs_type_count(enum rte_devtype devtype)
213 {
214         struct rte_devargs *devargs;
215         unsigned int count = 0;
216
217         TAILQ_FOREACH(devargs, &devargs_list, next) {
218                 if (devargs->type != devtype)
219                         continue;
220                 count++;
221         }
222         return count;
223 }
224
225 /* dump the user devices on the console */
226 void
227 rte_eal_devargs_dump(FILE *f)
228 {
229         struct rte_devargs *devargs;
230
231         fprintf(f, "User device list:\n");
232         TAILQ_FOREACH(devargs, &devargs_list, next) {
233                 fprintf(f, "  [%s]: %s %s\n",
234                         (devargs->bus ? devargs->bus->name : "??"),
235                         devargs->name, devargs->args);
236         }
237 }