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.
39 #include <sys/queue.h>
44 #include <rte_common.h>
45 #include <rte_devargs.h>
46 #include <rte_memory.h>
48 /** Double linked list of virtual device drivers. */
49 TAILQ_HEAD(vdev_device_list, rte_vdev_device);
51 static struct vdev_device_list vdev_device_list =
52 TAILQ_HEAD_INITIALIZER(vdev_device_list);
53 struct vdev_driver_list vdev_driver_list =
54 TAILQ_HEAD_INITIALIZER(vdev_driver_list);
56 /* register a driver */
58 rte_vdev_register(struct rte_vdev_driver *driver)
60 TAILQ_INSERT_TAIL(&vdev_driver_list, driver, next);
63 /* unregister a driver */
65 rte_vdev_unregister(struct rte_vdev_driver *driver)
67 TAILQ_REMOVE(&vdev_driver_list, driver, next);
71 * Parse "driver" devargs without adding a dependency on rte_kvargs.h
73 static char *parse_driver_arg(const char *args)
78 if (!args || args[0] == '\0')
84 if (strncmp(c, "driver=", 7) == 0) {
103 vdev_probe_all_drivers(struct rte_vdev_device *dev)
107 struct rte_vdev_driver *driver;
110 drv_name = parse_driver_arg(rte_vdev_device_args(dev));
111 name = drv_name ? drv_name : rte_vdev_device_name(dev);
113 RTE_LOG(DEBUG, EAL, "Search driver %s to probe device %s\n", name,
114 rte_vdev_device_name(dev));
116 TAILQ_FOREACH(driver, &vdev_driver_list, next) {
118 * search a driver prefix in virtual device name.
119 * For example, if the driver is pcap PMD, driver->name
120 * will be "net_pcap", but "name" will be "net_pcapN".
121 * So use strncmp to compare.
123 if (!strncmp(driver->driver.name, name,
124 strlen(driver->driver.name))) {
125 dev->device.driver = &driver->driver;
126 ret = driver->probe(dev);
128 dev->device.driver = NULL;
133 /* Give new names precedence over aliases. */
134 TAILQ_FOREACH(driver, &vdev_driver_list, next) {
135 if (driver->driver.alias &&
136 !strncmp(driver->driver.alias, name,
137 strlen(driver->driver.alias))) {
138 dev->device.driver = &driver->driver;
139 ret = driver->probe(dev);
141 dev->device.driver = NULL;
151 static struct rte_vdev_device *
152 find_vdev(const char *name)
154 struct rte_vdev_device *dev;
159 TAILQ_FOREACH(dev, &vdev_device_list, next) {
160 const char *devname = rte_vdev_device_name(dev);
161 if (!strncmp(devname, name, strlen(name)))
168 static struct rte_devargs *
169 alloc_devargs(const char *name, const char *args)
171 struct rte_devargs *devargs;
174 devargs = calloc(1, sizeof(*devargs));
178 devargs->type = RTE_DEVTYPE_VIRTUAL;
180 devargs->args = strdup(args);
182 ret = snprintf(devargs->virt.drv_name,
183 sizeof(devargs->virt.drv_name), "%s", name);
184 if (ret < 0 || ret >= (int)sizeof(devargs->virt.drv_name)) {
194 rte_vdev_init(const char *name, const char *args)
196 struct rte_vdev_device *dev;
197 struct rte_devargs *devargs;
203 dev = find_vdev(name);
207 devargs = alloc_devargs(name, args);
211 dev = calloc(1, sizeof(*dev));
217 dev->device.devargs = devargs;
218 dev->device.numa_node = SOCKET_ID_ANY;
219 dev->device.name = devargs->virt.drv_name;
221 ret = vdev_probe_all_drivers(dev);
224 RTE_LOG(ERR, EAL, "no driver found for %s\n", name);
228 TAILQ_INSERT_TAIL(&devargs_list, devargs, next);
230 TAILQ_INSERT_TAIL(&vdev_device_list, dev, next);
241 vdev_remove_driver(struct rte_vdev_device *dev)
243 const char *name = rte_vdev_device_name(dev);
244 const struct rte_vdev_driver *driver;
246 if (!dev->device.driver) {
247 RTE_LOG(DEBUG, EAL, "no driver attach to device %s\n", name);
251 driver = container_of(dev->device.driver, const struct rte_vdev_driver,
253 return driver->remove(dev);
257 rte_vdev_uninit(const char *name)
259 struct rte_vdev_device *dev;
260 struct rte_devargs *devargs;
266 dev = find_vdev(name);
270 devargs = dev->device.devargs;
272 ret = vdev_remove_driver(dev);
276 TAILQ_REMOVE(&vdev_device_list, dev, next);
278 TAILQ_REMOVE(&devargs_list, devargs, next);
289 struct rte_vdev_device *dev;
290 struct rte_devargs *devargs;
292 /* for virtual devices we scan the devargs_list populated via cmdline */
294 TAILQ_FOREACH(devargs, &devargs_list, next) {
296 if (devargs->type != RTE_DEVTYPE_VIRTUAL)
299 dev = find_vdev(devargs->virt.drv_name);
303 dev = calloc(1, sizeof(*dev));
307 dev->device.devargs = devargs;
308 dev->device.numa_node = SOCKET_ID_ANY;
309 dev->device.name = devargs->virt.drv_name;
311 TAILQ_INSERT_TAIL(&vdev_device_list, dev, next);
320 struct rte_vdev_device *dev;
322 /* call the init function for each virtual device */
323 TAILQ_FOREACH(dev, &vdev_device_list, next) {
325 if (dev->device.driver)
328 if (vdev_probe_all_drivers(dev)) {
329 RTE_LOG(ERR, EAL, "failed to initialize %s device\n",
330 rte_vdev_device_name(dev));
338 static struct rte_device *
339 vdev_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
342 struct rte_vdev_device *dev;
343 bool start_found = !start;
345 TAILQ_FOREACH(dev, &vdev_device_list, next) {
346 if (start_found == 0) {
347 if (&dev->device == start)
351 if (cmp(&dev->device, data) == 0)
357 static struct rte_bus rte_vdev_bus = {
360 .find_device = vdev_find_device,
363 RTE_REGISTER_BUS(VIRTUAL_BUS_NAME, rte_vdev_bus);