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