22fe2ca62add0146bed681e9abb679aa2cb8e1cb
[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
184         ret = vdev_probe_all_drivers(dev);
185         if (ret) {
186                 if (ret > 0)
187                         RTE_LOG(ERR, EAL, "no driver found for %s\n", name);
188                 goto fail;
189         }
190
191         TAILQ_INSERT_TAIL(&devargs_list, devargs, next);
192
193         rte_eal_device_insert(&dev->device);
194         TAILQ_INSERT_TAIL(&vdev_device_list, dev, next);
195         return 0;
196
197 fail:
198         free(devargs->args);
199         free(devargs);
200         free(dev);
201         return ret;
202 }
203
204 static int
205 vdev_remove_driver(struct rte_vdev_device *dev)
206 {
207         const char *name = rte_vdev_device_name(dev);
208         const struct rte_vdev_driver *driver;
209
210         if (!dev->device.driver) {
211                 RTE_LOG(DEBUG, EAL, "no driver attach to device %s\n", name);
212                 return 1;
213         }
214
215         driver = container_of(dev->device.driver, const struct rte_vdev_driver,
216                 driver);
217         return driver->remove(dev);
218 }
219
220 int
221 rte_eal_vdev_uninit(const char *name)
222 {
223         struct rte_vdev_device *dev;
224         struct rte_devargs *devargs;
225         int ret;
226
227         if (name == NULL)
228                 return -EINVAL;
229
230         dev = find_vdev(name);
231         if (!dev)
232                 return -ENOENT;
233
234         devargs = dev->device.devargs;
235
236         ret = vdev_remove_driver(dev);
237         if (ret)
238                 return ret;
239
240         TAILQ_REMOVE(&vdev_device_list, dev, next);
241         rte_eal_device_remove(&dev->device);
242
243         TAILQ_REMOVE(&devargs_list, devargs, next);
244
245         free(devargs->args);
246         free(devargs);
247         free(dev);
248         return 0;
249 }
250
251 static int
252 vdev_scan(void)
253 {
254         struct rte_vdev_device *dev;
255         struct rte_devargs *devargs;
256
257         /* for virtual devices we scan the devargs_list populated via cmdline */
258
259         TAILQ_FOREACH(devargs, &devargs_list, next) {
260
261                 if (devargs->type != RTE_DEVTYPE_VIRTUAL)
262                         continue;
263
264                 dev = find_vdev(devargs->virt.drv_name);
265                 if (dev)
266                         continue;
267
268                 dev = calloc(1, sizeof(*dev));
269                 if (!dev)
270                         return -1;
271
272                 dev->device.devargs = devargs;
273                 dev->device.numa_node = SOCKET_ID_ANY;
274
275                 rte_eal_device_insert(&dev->device);
276                 TAILQ_INSERT_TAIL(&vdev_device_list, dev, next);
277         }
278
279         return 0;
280 }
281
282 static int
283 vdev_probe(void)
284 {
285         struct rte_vdev_device *dev;
286
287         /*
288          * Note that the dev_driver_list is populated here
289          * from calls made to rte_eal_driver_register from constructor functions
290          * embedded into PMD modules via the RTE_PMD_REGISTER_VDEV macro
291          */
292
293         /* call the init function for each virtual device */
294         TAILQ_FOREACH(dev, &vdev_device_list, next) {
295
296                 if (dev->device.driver)
297                         continue;
298
299                 if (vdev_probe_all_drivers(dev)) {
300                         RTE_LOG(ERR, EAL, "failed to initialize %s device\n",
301                                 rte_vdev_device_name(dev));
302                         return -1;
303                 }
304         }
305
306         return 0;
307 }
308
309 static struct rte_bus rte_vdev_bus = {
310         .scan = vdev_scan,
311         .probe = vdev_probe,
312 };
313
314 RTE_INIT(rte_vdev_bus_register);
315
316 static void rte_vdev_bus_register(void)
317 {
318         static int registered;
319
320         if (registered)
321                 return;
322
323         registered = 1;
324         rte_vdev_bus.name = RTE_STR(virtual);
325         rte_bus_register(&rte_vdev_bus);
326 }