vdev: add virtual device struct
[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
46 /** Double linked list of virtual device drivers. */
47 TAILQ_HEAD(vdev_device_list, rte_vdev_device);
48
49 static struct vdev_device_list vdev_device_list =
50         TAILQ_HEAD_INITIALIZER(vdev_device_list);
51 struct vdev_driver_list vdev_driver_list =
52         TAILQ_HEAD_INITIALIZER(vdev_driver_list);
53
54 static void rte_vdev_bus_register(void);
55
56 /* register a driver */
57 void
58 rte_eal_vdrv_register(struct rte_vdev_driver *driver)
59 {
60         rte_vdev_bus_register();
61
62         TAILQ_INSERT_TAIL(&vdev_driver_list, driver, next);
63         rte_eal_driver_register(&driver->driver);
64 }
65
66 /* unregister a driver */
67 void
68 rte_eal_vdrv_unregister(struct rte_vdev_driver *driver)
69 {
70         rte_eal_driver_unregister(&driver->driver);
71         TAILQ_REMOVE(&vdev_driver_list, driver, next);
72 }
73
74 static int
75 vdev_probe_all_drivers(const char *name, const char *args)
76 {
77         struct rte_vdev_driver *driver;
78
79         TAILQ_FOREACH(driver, &vdev_driver_list, next) {
80                 /*
81                  * search a driver prefix in virtual device name.
82                  * For example, if the driver is pcap PMD, driver->name
83                  * will be "net_pcap", but "name" will be "net_pcapN".
84                  * So use strncmp to compare.
85                  */
86                 if (!strncmp(driver->driver.name, name,
87                             strlen(driver->driver.name)))
88                         return driver->probe(name, args);
89         }
90
91         /* Give new names precedence over aliases. */
92         TAILQ_FOREACH(driver, &vdev_driver_list, next) {
93                 if (driver->driver.alias &&
94                     !strncmp(driver->driver.alias, name,
95                             strlen(driver->driver.alias)))
96                         return driver->probe(name, args);
97         }
98
99         return 1;
100 }
101
102 int
103 rte_eal_vdev_init(const char *name, const char *args)
104 {
105         int ret;
106
107         if (name == NULL)
108                 return -EINVAL;
109
110         ret = vdev_probe_all_drivers(name, args);
111         if (ret  > 0)
112                 RTE_LOG(ERR, EAL, "no driver found for %s\n", name);
113
114         return ret;
115 }
116
117 static int
118 vdev_remove_driver(const char *name)
119 {
120         struct rte_vdev_driver *driver;
121
122         TAILQ_FOREACH(driver, &vdev_driver_list, next) {
123                 /*
124                  * search a driver prefix in virtual device name.
125                  * For example, if the driver is pcap PMD, driver->name
126                  * will be "net_pcap", but "name" will be "net_pcapN".
127                  * So use strncmp to compare.
128                  */
129                 if (!strncmp(driver->driver.name, name,
130                              strlen(driver->driver.name)))
131                         return driver->remove(name);
132         }
133
134         /* Give new names precedence over aliases. */
135         TAILQ_FOREACH(driver, &vdev_driver_list, next) {
136                 if (driver->driver.alias &&
137                     !strncmp(driver->driver.alias, name,
138                             strlen(driver->driver.alias)))
139                         return driver->remove(name);
140         }
141
142         return 1;
143 }
144
145 int
146 rte_eal_vdev_uninit(const char *name)
147 {
148         int ret;
149
150         if (name == NULL)
151                 return -EINVAL;
152
153         ret = vdev_remove_driver(name);
154         if (ret > 0)
155                 RTE_LOG(ERR, EAL, "no driver found for %s\n", name);
156
157         return ret;
158 }
159
160 static int
161 vdev_scan(void)
162 {
163         /* for virtual devices we don't need to scan anything */
164         return 0;
165 }
166
167 static int
168 vdev_probe(void)
169 {
170         struct rte_devargs *devargs;
171
172         /*
173          * Note that the dev_driver_list is populated here
174          * from calls made to rte_eal_driver_register from constructor functions
175          * embedded into PMD modules via the RTE_PMD_REGISTER_VDEV macro
176          */
177
178         /* call the init function for each virtual device */
179         TAILQ_FOREACH(devargs, &devargs_list, next) {
180
181                 if (devargs->type != RTE_DEVTYPE_VIRTUAL)
182                         continue;
183
184                 if (rte_eal_vdev_init(devargs->virt.drv_name,
185                                       devargs->args)) {
186                         RTE_LOG(ERR, EAL, "failed to initialize %s device\n",
187                                 devargs->virt.drv_name);
188                         return -1;
189                 }
190         }
191
192         return 0;
193 }
194
195 static struct rte_bus rte_vdev_bus = {
196         .scan = vdev_scan,
197         .probe = vdev_probe,
198 };
199
200 RTE_INIT(rte_vdev_bus_register);
201
202 static void rte_vdev_bus_register(void)
203 {
204         static int registered;
205
206         if (registered)
207                 return;
208
209         registered = 1;
210         rte_vdev_bus.name = RTE_STR(virtual);
211         rte_bus_register(&rte_vdev_bus);
212 }