eal: parse driver argument before probing drivers
[dpdk.git] / lib / librte_eal / common / eal_common_vdev.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2016 RehiveTech. All rights reserved.
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 RehiveTech nor the names of its
17  *       contributors may be used to endorse or promote products derived
18  *       from this 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 #include <string.h>
34 #include <inttypes.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <stdint.h>
38 #include <sys/queue.h>
39
40 #include <rte_eal.h>
41 #include <rte_bus.h>
42 #include <rte_vdev.h>
43 #include <rte_common.h>
44 #include <rte_devargs.h>
45 #include <rte_memory.h>
46
47 /** Double linked list of virtual device drivers. */
48 TAILQ_HEAD(vdev_device_list, rte_vdev_device);
49
50 static struct vdev_device_list vdev_device_list =
51         TAILQ_HEAD_INITIALIZER(vdev_device_list);
52 struct vdev_driver_list vdev_driver_list =
53         TAILQ_HEAD_INITIALIZER(vdev_driver_list);
54
55 static void rte_vdev_bus_register(void);
56
57 /* register a driver */
58 void
59 rte_eal_vdrv_register(struct rte_vdev_driver *driver)
60 {
61         rte_vdev_bus_register();
62
63         TAILQ_INSERT_TAIL(&vdev_driver_list, driver, next);
64         rte_eal_driver_register(&driver->driver);
65 }
66
67 /* unregister a driver */
68 void
69 rte_eal_vdrv_unregister(struct rte_vdev_driver *driver)
70 {
71         rte_eal_driver_unregister(&driver->driver);
72         TAILQ_REMOVE(&vdev_driver_list, driver, next);
73 }
74
75 /*
76  * Parse "driver" devargs without adding a dependency on rte_kvargs.h
77  */
78 static char *parse_driver_arg(const char *args)
79 {
80         const char *c;
81         char *str;
82
83         if (!args || args[0] == '\0')
84                 return NULL;
85
86         c = args;
87
88         do {
89                 if (strncmp(c, "driver=", 7) == 0) {
90                         c += 7;
91                         break;
92                 }
93
94                 c = strchr(c, ',');
95                 if (c)
96                         c++;
97         } while (c);
98
99         if (c)
100                 str = strdup(c);
101         else
102                 str = NULL;
103
104         return str;
105 }
106
107 static int
108 vdev_probe_all_drivers(struct rte_vdev_device *dev)
109 {
110         const char *name;
111         char *drv_name;
112         struct rte_vdev_driver *driver;
113         int ret = 1;
114
115         drv_name = parse_driver_arg(rte_vdev_device_args(dev));
116         name = drv_name ? drv_name : rte_vdev_device_name(dev);
117
118         RTE_LOG(DEBUG, EAL, "Search driver %s to probe device %s\n", name,
119                 rte_vdev_device_name(dev));
120
121         TAILQ_FOREACH(driver, &vdev_driver_list, next) {
122                 /*
123                  * search a driver prefix in virtual device name.
124                  * For example, if the driver is pcap PMD, driver->name
125                  * will be "net_pcap", but "name" will be "net_pcapN".
126                  * So use strncmp to compare.
127                  */
128                 if (!strncmp(driver->driver.name, name,
129                             strlen(driver->driver.name))) {
130                         dev->device.driver = &driver->driver;
131                         ret = driver->probe(dev);
132                         if (ret)
133                                 dev->device.driver = NULL;
134                         goto out;
135                 }
136         }
137
138         /* Give new names precedence over aliases. */
139         TAILQ_FOREACH(driver, &vdev_driver_list, next) {
140                 if (driver->driver.alias &&
141                     !strncmp(driver->driver.alias, name,
142                             strlen(driver->driver.alias))) {
143                         dev->device.driver = &driver->driver;
144                         ret = driver->probe(dev);
145                         if (ret)
146                                 dev->device.driver = NULL;
147                         break;
148                 }
149         }
150
151 out:
152         free(drv_name);
153         return ret;
154 }
155
156 static struct rte_vdev_device *
157 find_vdev(const char *name)
158 {
159         struct rte_vdev_device *dev;
160
161         if (!name)
162                 return NULL;
163
164         TAILQ_FOREACH(dev, &vdev_device_list, next) {
165                 const char *devname = rte_vdev_device_name(dev);
166                 if (!strncmp(devname, name, strlen(name)))
167                         return dev;
168         }
169
170         return NULL;
171 }
172
173 static struct rte_devargs *
174 alloc_devargs(const char *name, const char *args)
175 {
176         struct rte_devargs *devargs;
177         int ret;
178
179         devargs = calloc(1, sizeof(*devargs));
180         if (!devargs)
181                 return NULL;
182
183         devargs->type = RTE_DEVTYPE_VIRTUAL;
184         if (args)
185                 devargs->args = strdup(args);
186
187         ret = snprintf(devargs->virt.drv_name,
188                                sizeof(devargs->virt.drv_name), "%s", name);
189         if (ret < 0 || ret >= (int)sizeof(devargs->virt.drv_name)) {
190                 free(devargs->args);
191                 free(devargs);
192                 return NULL;
193         }
194
195         return devargs;
196 }
197
198 int
199 rte_eal_vdev_init(const char *name, const char *args)
200 {
201         struct rte_vdev_device *dev;
202         struct rte_devargs *devargs;
203         int ret;
204
205         if (name == NULL)
206                 return -EINVAL;
207
208         dev = find_vdev(name);
209         if (dev)
210                 return -EEXIST;
211
212         devargs = alloc_devargs(name, args);
213         if (!devargs)
214                 return -ENOMEM;
215
216         dev = calloc(1, sizeof(*dev));
217         if (!dev) {
218                 ret = -ENOMEM;
219                 goto fail;
220         }
221
222         dev->device.devargs = devargs;
223         dev->device.numa_node = SOCKET_ID_ANY;
224         dev->device.name = devargs->virt.drv_name;
225
226         ret = vdev_probe_all_drivers(dev);
227         if (ret) {
228                 if (ret > 0)
229                         RTE_LOG(ERR, EAL, "no driver found for %s\n", name);
230                 goto fail;
231         }
232
233         TAILQ_INSERT_TAIL(&devargs_list, devargs, next);
234
235         rte_eal_device_insert(&dev->device);
236         TAILQ_INSERT_TAIL(&vdev_device_list, dev, next);
237         return 0;
238
239 fail:
240         free(devargs->args);
241         free(devargs);
242         free(dev);
243         return ret;
244 }
245
246 static int
247 vdev_remove_driver(struct rte_vdev_device *dev)
248 {
249         const char *name = rte_vdev_device_name(dev);
250         const struct rte_vdev_driver *driver;
251
252         if (!dev->device.driver) {
253                 RTE_LOG(DEBUG, EAL, "no driver attach to device %s\n", name);
254                 return 1;
255         }
256
257         driver = container_of(dev->device.driver, const struct rte_vdev_driver,
258                 driver);
259         return driver->remove(dev);
260 }
261
262 int
263 rte_eal_vdev_uninit(const char *name)
264 {
265         struct rte_vdev_device *dev;
266         struct rte_devargs *devargs;
267         int ret;
268
269         if (name == NULL)
270                 return -EINVAL;
271
272         dev = find_vdev(name);
273         if (!dev)
274                 return -ENOENT;
275
276         devargs = dev->device.devargs;
277
278         ret = vdev_remove_driver(dev);
279         if (ret)
280                 return ret;
281
282         TAILQ_REMOVE(&vdev_device_list, dev, next);
283         rte_eal_device_remove(&dev->device);
284
285         TAILQ_REMOVE(&devargs_list, devargs, next);
286
287         free(devargs->args);
288         free(devargs);
289         free(dev);
290         return 0;
291 }
292
293 static int
294 vdev_scan(void)
295 {
296         struct rte_vdev_device *dev;
297         struct rte_devargs *devargs;
298
299         /* for virtual devices we scan the devargs_list populated via cmdline */
300
301         TAILQ_FOREACH(devargs, &devargs_list, next) {
302
303                 if (devargs->type != RTE_DEVTYPE_VIRTUAL)
304                         continue;
305
306                 dev = find_vdev(devargs->virt.drv_name);
307                 if (dev)
308                         continue;
309
310                 dev = calloc(1, sizeof(*dev));
311                 if (!dev)
312                         return -1;
313
314                 dev->device.devargs = devargs;
315                 dev->device.numa_node = SOCKET_ID_ANY;
316                 dev->device.name = devargs->virt.drv_name;
317
318                 rte_eal_device_insert(&dev->device);
319                 TAILQ_INSERT_TAIL(&vdev_device_list, dev, next);
320         }
321
322         return 0;
323 }
324
325 static int
326 vdev_probe(void)
327 {
328         struct rte_vdev_device *dev;
329
330         /*
331          * Note that the dev_driver_list is populated here
332          * from calls made to rte_eal_driver_register from constructor functions
333          * embedded into PMD modules via the RTE_PMD_REGISTER_VDEV macro
334          */
335
336         /* call the init function for each virtual device */
337         TAILQ_FOREACH(dev, &vdev_device_list, next) {
338
339                 if (dev->device.driver)
340                         continue;
341
342                 if (vdev_probe_all_drivers(dev)) {
343                         RTE_LOG(ERR, EAL, "failed to initialize %s device\n",
344                                 rte_vdev_device_name(dev));
345                         return -1;
346                 }
347         }
348
349         return 0;
350 }
351
352 static struct rte_bus rte_vdev_bus = {
353         .scan = vdev_scan,
354         .probe = vdev_probe,
355 };
356
357 RTE_INIT(rte_vdev_bus_register);
358
359 static void rte_vdev_bus_register(void)
360 {
361         static int registered;
362
363         if (registered)
364                 return;
365
366         registered = 1;
367         rte_vdev_bus.name = RTE_STR(virtual);
368         rte_bus_register(&rte_vdev_bus);
369 }