4 * Copyright(c) 2016 RehiveTech. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
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.
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.
38 #include <sys/queue.h>
43 #include <rte_common.h>
44 #include <rte_devargs.h>
45 #include <rte_memory.h>
47 /** Double linked list of virtual device drivers. */
48 TAILQ_HEAD(vdev_device_list, rte_vdev_device);
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);
55 static void rte_vdev_bus_register(void);
57 /* register a driver */
59 rte_eal_vdrv_register(struct rte_vdev_driver *driver)
61 rte_vdev_bus_register();
63 TAILQ_INSERT_TAIL(&vdev_driver_list, driver, next);
64 rte_eal_driver_register(&driver->driver);
67 /* unregister a driver */
69 rte_eal_vdrv_unregister(struct rte_vdev_driver *driver)
71 rte_eal_driver_unregister(&driver->driver);
72 TAILQ_REMOVE(&vdev_driver_list, driver, next);
76 vdev_probe_all_drivers(struct rte_vdev_device *dev)
78 const char *name = rte_vdev_device_name(dev);
79 struct rte_vdev_driver *driver;
82 TAILQ_FOREACH(driver, &vdev_driver_list, next) {
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.
89 if (!strncmp(driver->driver.name, name,
90 strlen(driver->driver.name))) {
91 dev->device.driver = &driver->driver;
92 ret = driver->probe(dev);
94 dev->device.driver = NULL;
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);
107 dev->device.driver = NULL;
115 static struct rte_vdev_device *
116 find_vdev(const char *name)
118 struct rte_vdev_device *dev;
123 TAILQ_FOREACH(dev, &vdev_device_list, next) {
124 const char *devname = rte_vdev_device_name(dev);
125 if (!strncmp(devname, name, strlen(name)))
132 static struct rte_devargs *
133 alloc_devargs(const char *name, const char *args)
135 struct rte_devargs *devargs;
138 devargs = calloc(1, sizeof(*devargs));
142 devargs->type = RTE_DEVTYPE_VIRTUAL;
144 devargs->args = strdup(args);
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)) {
158 rte_eal_vdev_init(const char *name, const char *args)
160 struct rte_vdev_device *dev;
161 struct rte_devargs *devargs;
167 dev = find_vdev(name);
171 devargs = alloc_devargs(name, args);
175 dev = calloc(1, sizeof(*dev));
181 dev->device.devargs = devargs;
182 dev->device.numa_node = SOCKET_ID_ANY;
184 ret = vdev_probe_all_drivers(dev);
187 RTE_LOG(ERR, EAL, "no driver found for %s\n", name);
191 TAILQ_INSERT_TAIL(&devargs_list, devargs, next);
193 rte_eal_device_insert(&dev->device);
194 TAILQ_INSERT_TAIL(&vdev_device_list, dev, next);
205 vdev_remove_driver(struct rte_vdev_device *dev)
207 const char *name = rte_vdev_device_name(dev);
208 const struct rte_vdev_driver *driver;
210 if (!dev->device.driver) {
211 RTE_LOG(DEBUG, EAL, "no driver attach to device %s\n", name);
215 driver = container_of(dev->device.driver, const struct rte_vdev_driver,
217 return driver->remove(dev);
221 rte_eal_vdev_uninit(const char *name)
223 struct rte_vdev_device *dev;
224 struct rte_devargs *devargs;
230 dev = find_vdev(name);
234 devargs = dev->device.devargs;
236 ret = vdev_remove_driver(dev);
240 TAILQ_REMOVE(&vdev_device_list, dev, next);
241 rte_eal_device_remove(&dev->device);
243 TAILQ_REMOVE(&devargs_list, devargs, next);
254 struct rte_vdev_device *dev;
255 struct rte_devargs *devargs;
257 /* for virtual devices we scan the devargs_list populated via cmdline */
259 TAILQ_FOREACH(devargs, &devargs_list, next) {
261 if (devargs->type != RTE_DEVTYPE_VIRTUAL)
264 dev = find_vdev(devargs->virt.drv_name);
268 dev = calloc(1, sizeof(*dev));
272 dev->device.devargs = devargs;
273 dev->device.numa_node = SOCKET_ID_ANY;
275 rte_eal_device_insert(&dev->device);
276 TAILQ_INSERT_TAIL(&vdev_device_list, dev, next);
285 struct rte_vdev_device *dev;
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
293 /* call the init function for each virtual device */
294 TAILQ_FOREACH(dev, &vdev_device_list, next) {
296 if (dev->device.driver)
299 if (vdev_probe_all_drivers(dev)) {
300 RTE_LOG(ERR, EAL, "failed to initialize %s device\n",
301 rte_vdev_device_name(dev));
309 static struct rte_bus rte_vdev_bus = {
314 RTE_INIT(rte_vdev_bus_register);
316 static void rte_vdev_bus_register(void)
318 static int registered;
324 rte_vdev_bus.name = RTE_STR(virtual);
325 rte_bus_register(&rte_vdev_bus);