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 /* register a driver */
57 rte_vdev_register(struct rte_vdev_driver *driver)
59 TAILQ_INSERT_TAIL(&vdev_driver_list, driver, next);
62 /* unregister a driver */
64 rte_vdev_unregister(struct rte_vdev_driver *driver)
66 TAILQ_REMOVE(&vdev_driver_list, driver, next);
70 * Parse "driver" devargs without adding a dependency on rte_kvargs.h
72 static char *parse_driver_arg(const char *args)
77 if (!args || args[0] == '\0')
83 if (strncmp(c, "driver=", 7) == 0) {
102 vdev_probe_all_drivers(struct rte_vdev_device *dev)
106 struct rte_vdev_driver *driver;
109 drv_name = parse_driver_arg(rte_vdev_device_args(dev));
110 name = drv_name ? drv_name : rte_vdev_device_name(dev);
112 RTE_LOG(DEBUG, EAL, "Search driver %s to probe device %s\n", name,
113 rte_vdev_device_name(dev));
115 TAILQ_FOREACH(driver, &vdev_driver_list, next) {
117 * search a driver prefix in virtual device name.
118 * For example, if the driver is pcap PMD, driver->name
119 * will be "net_pcap", but "name" will be "net_pcapN".
120 * So use strncmp to compare.
122 if (!strncmp(driver->driver.name, name,
123 strlen(driver->driver.name))) {
124 dev->device.driver = &driver->driver;
125 ret = driver->probe(dev);
127 dev->device.driver = NULL;
132 /* Give new names precedence over aliases. */
133 TAILQ_FOREACH(driver, &vdev_driver_list, next) {
134 if (driver->driver.alias &&
135 !strncmp(driver->driver.alias, name,
136 strlen(driver->driver.alias))) {
137 dev->device.driver = &driver->driver;
138 ret = driver->probe(dev);
140 dev->device.driver = NULL;
150 static struct rte_vdev_device *
151 find_vdev(const char *name)
153 struct rte_vdev_device *dev;
158 TAILQ_FOREACH(dev, &vdev_device_list, next) {
159 const char *devname = rte_vdev_device_name(dev);
160 if (!strncmp(devname, name, strlen(name)))
167 static struct rte_devargs *
168 alloc_devargs(const char *name, const char *args)
170 struct rte_devargs *devargs;
173 devargs = calloc(1, sizeof(*devargs));
177 devargs->type = RTE_DEVTYPE_VIRTUAL;
179 devargs->args = strdup(args);
181 ret = snprintf(devargs->virt.drv_name,
182 sizeof(devargs->virt.drv_name), "%s", name);
183 if (ret < 0 || ret >= (int)sizeof(devargs->virt.drv_name)) {
193 rte_vdev_init(const char *name, const char *args)
195 struct rte_vdev_device *dev;
196 struct rte_devargs *devargs;
202 dev = find_vdev(name);
206 devargs = alloc_devargs(name, args);
210 dev = calloc(1, sizeof(*dev));
216 dev->device.devargs = devargs;
217 dev->device.numa_node = SOCKET_ID_ANY;
218 dev->device.name = devargs->virt.drv_name;
220 ret = vdev_probe_all_drivers(dev);
223 RTE_LOG(ERR, EAL, "no driver found for %s\n", name);
227 TAILQ_INSERT_TAIL(&devargs_list, devargs, next);
229 TAILQ_INSERT_TAIL(&vdev_device_list, dev, next);
240 vdev_remove_driver(struct rte_vdev_device *dev)
242 const char *name = rte_vdev_device_name(dev);
243 const struct rte_vdev_driver *driver;
245 if (!dev->device.driver) {
246 RTE_LOG(DEBUG, EAL, "no driver attach to device %s\n", name);
250 driver = container_of(dev->device.driver, const struct rte_vdev_driver,
252 return driver->remove(dev);
256 rte_vdev_uninit(const char *name)
258 struct rte_vdev_device *dev;
259 struct rte_devargs *devargs;
265 dev = find_vdev(name);
269 devargs = dev->device.devargs;
271 ret = vdev_remove_driver(dev);
275 TAILQ_REMOVE(&vdev_device_list, dev, next);
277 TAILQ_REMOVE(&devargs_list, devargs, next);
288 struct rte_vdev_device *dev;
289 struct rte_devargs *devargs;
291 /* for virtual devices we scan the devargs_list populated via cmdline */
293 TAILQ_FOREACH(devargs, &devargs_list, next) {
295 if (devargs->type != RTE_DEVTYPE_VIRTUAL)
298 dev = find_vdev(devargs->virt.drv_name);
302 dev = calloc(1, sizeof(*dev));
306 dev->device.devargs = devargs;
307 dev->device.numa_node = SOCKET_ID_ANY;
308 dev->device.name = devargs->virt.drv_name;
310 TAILQ_INSERT_TAIL(&vdev_device_list, dev, next);
319 struct rte_vdev_device *dev;
321 /* call the init function for each virtual device */
322 TAILQ_FOREACH(dev, &vdev_device_list, next) {
324 if (dev->device.driver)
327 if (vdev_probe_all_drivers(dev)) {
328 RTE_LOG(ERR, EAL, "failed to initialize %s device\n",
329 rte_vdev_device_name(dev));
337 static struct rte_bus rte_vdev_bus = {
342 RTE_REGISTER_BUS(VIRTUAL_BUS_NAME, rte_vdev_bus);