eal: add name field to generic 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 <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 static int
76 vdev_probe_all_drivers(struct rte_vdev_device *dev)
77 {
78         const char *name = rte_vdev_device_name(dev);
79         struct rte_vdev_driver *driver;
80         int ret;
81
82         TAILQ_FOREACH(driver, &vdev_driver_list, next) {
83                 /*
84                  * search a driver prefix in virtual device name.
85                  * For example, if the driver is pcap PMD, driver->name
86                  * will be "net_pcap", but "name" will be "net_pcapN".
87                  * So use strncmp to compare.
88                  */
89                 if (!strncmp(driver->driver.name, name,
90                             strlen(driver->driver.name))) {
91                         dev->device.driver = &driver->driver;
92                         ret = driver->probe(dev);
93                         if (ret)
94                                 dev->device.driver = NULL;
95                         return ret;
96                 }
97         }
98
99         /* Give new names precedence over aliases. */
100         TAILQ_FOREACH(driver, &vdev_driver_list, next) {
101                 if (driver->driver.alias &&
102                     !strncmp(driver->driver.alias, name,
103                             strlen(driver->driver.alias))) {
104                         dev->device.driver = &driver->driver;
105                         ret = driver->probe(dev);
106                         if (ret)
107                                 dev->device.driver = NULL;
108                         return ret;
109                 }
110         }
111
112         return 1;
113 }
114
115 static struct rte_vdev_device *
116 find_vdev(const char *name)
117 {
118         struct rte_vdev_device *dev;
119
120         if (!name)
121                 return NULL;
122
123         TAILQ_FOREACH(dev, &vdev_device_list, next) {
124                 const char *devname = rte_vdev_device_name(dev);
125                 if (!strncmp(devname, name, strlen(name)))
126                         return dev;
127         }
128
129         return NULL;
130 }
131
132 static struct rte_devargs *
133 alloc_devargs(const char *name, const char *args)
134 {
135         struct rte_devargs *devargs;
136         int ret;
137
138         devargs = calloc(1, sizeof(*devargs));
139         if (!devargs)
140                 return NULL;
141
142         devargs->type = RTE_DEVTYPE_VIRTUAL;
143         if (args)
144                 devargs->args = strdup(args);
145
146         ret = snprintf(devargs->virt.drv_name,
147                                sizeof(devargs->virt.drv_name), "%s", name);
148         if (ret < 0 || ret >= (int)sizeof(devargs->virt.drv_name)) {
149                 free(devargs->args);
150                 free(devargs);
151                 return NULL;
152         }
153
154         return devargs;
155 }
156
157 int
158 rte_eal_vdev_init(const char *name, const char *args)
159 {
160         struct rte_vdev_device *dev;
161         struct rte_devargs *devargs;
162         int ret;
163
164         if (name == NULL)
165                 return -EINVAL;
166
167         dev = find_vdev(name);
168         if (dev)
169                 return -EEXIST;
170
171         devargs = alloc_devargs(name, args);
172         if (!devargs)
173                 return -ENOMEM;
174
175         dev = calloc(1, sizeof(*dev));
176         if (!dev) {
177                 ret = -ENOMEM;
178                 goto fail;
179         }
180
181         dev->device.devargs = devargs;
182         dev->device.numa_node = SOCKET_ID_ANY;
183         dev->device.name = devargs->virt.drv_name;
184
185         ret = vdev_probe_all_drivers(dev);
186         if (ret) {
187                 if (ret > 0)
188                         RTE_LOG(ERR, EAL, "no driver found for %s\n", name);
189                 goto fail;
190         }
191
192         TAILQ_INSERT_TAIL(&devargs_list, devargs, next);
193
194         rte_eal_device_insert(&dev->device);
195         TAILQ_INSERT_TAIL(&vdev_device_list, dev, next);
196         return 0;
197
198 fail:
199         free(devargs->args);
200         free(devargs);
201         free(dev);
202         return ret;
203 }
204
205 static int
206 vdev_remove_driver(struct rte_vdev_device *dev)
207 {
208         const char *name = rte_vdev_device_name(dev);
209         const struct rte_vdev_driver *driver;
210
211         if (!dev->device.driver) {
212                 RTE_LOG(DEBUG, EAL, "no driver attach to device %s\n", name);
213                 return 1;
214         }
215
216         driver = container_of(dev->device.driver, const struct rte_vdev_driver,
217                 driver);
218         return driver->remove(dev);
219 }
220
221 int
222 rte_eal_vdev_uninit(const char *name)
223 {
224         struct rte_vdev_device *dev;
225         struct rte_devargs *devargs;
226         int ret;
227
228         if (name == NULL)
229                 return -EINVAL;
230
231         dev = find_vdev(name);
232         if (!dev)
233                 return -ENOENT;
234
235         devargs = dev->device.devargs;
236
237         ret = vdev_remove_driver(dev);
238         if (ret)
239                 return ret;
240
241         TAILQ_REMOVE(&vdev_device_list, dev, next);
242         rte_eal_device_remove(&dev->device);
243
244         TAILQ_REMOVE(&devargs_list, devargs, next);
245
246         free(devargs->args);
247         free(devargs);
248         free(dev);
249         return 0;
250 }
251
252 static int
253 vdev_scan(void)
254 {
255         struct rte_vdev_device *dev;
256         struct rte_devargs *devargs;
257
258         /* for virtual devices we scan the devargs_list populated via cmdline */
259
260         TAILQ_FOREACH(devargs, &devargs_list, next) {
261
262                 if (devargs->type != RTE_DEVTYPE_VIRTUAL)
263                         continue;
264
265                 dev = find_vdev(devargs->virt.drv_name);
266                 if (dev)
267                         continue;
268
269                 dev = calloc(1, sizeof(*dev));
270                 if (!dev)
271                         return -1;
272
273                 dev->device.devargs = devargs;
274                 dev->device.numa_node = SOCKET_ID_ANY;
275                 dev->device.name = devargs->virt.drv_name;
276
277                 rte_eal_device_insert(&dev->device);
278                 TAILQ_INSERT_TAIL(&vdev_device_list, dev, next);
279         }
280
281         return 0;
282 }
283
284 static int
285 vdev_probe(void)
286 {
287         struct rte_vdev_device *dev;
288
289         /*
290          * Note that the dev_driver_list is populated here
291          * from calls made to rte_eal_driver_register from constructor functions
292          * embedded into PMD modules via the RTE_PMD_REGISTER_VDEV macro
293          */
294
295         /* call the init function for each virtual device */
296         TAILQ_FOREACH(dev, &vdev_device_list, next) {
297
298                 if (dev->device.driver)
299                         continue;
300
301                 if (vdev_probe_all_drivers(dev)) {
302                         RTE_LOG(ERR, EAL, "failed to initialize %s device\n",
303                                 rte_vdev_device_name(dev));
304                         return -1;
305                 }
306         }
307
308         return 0;
309 }
310
311 static struct rte_bus rte_vdev_bus = {
312         .scan = vdev_scan,
313         .probe = vdev_probe,
314 };
315
316 RTE_INIT(rte_vdev_bus_register);
317
318 static void rte_vdev_bus_register(void)
319 {
320         static int registered;
321
322         if (registered)
323                 return;
324
325         registered = 1;
326         rte_vdev_bus.name = RTE_STR(virtual);
327         rte_bus_register(&rte_vdev_bus);
328 }