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