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