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