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>
45 #include <rte_common.h>
46 #include <rte_devargs.h>
47 #include <rte_memory.h>
48 #include <rte_errno.h>
50 /** Double linked list of virtual device drivers. */
51 TAILQ_HEAD(vdev_device_list, rte_vdev_device);
53 static struct vdev_device_list vdev_device_list =
54 TAILQ_HEAD_INITIALIZER(vdev_device_list);
55 struct vdev_driver_list vdev_driver_list =
56 TAILQ_HEAD_INITIALIZER(vdev_driver_list);
58 /* register a driver */
60 rte_vdev_register(struct rte_vdev_driver *driver)
62 TAILQ_INSERT_TAIL(&vdev_driver_list, driver, next);
65 /* unregister a driver */
67 rte_vdev_unregister(struct rte_vdev_driver *driver)
69 TAILQ_REMOVE(&vdev_driver_list, driver, next);
73 * Parse "driver" devargs without adding a dependency on rte_kvargs.h
75 static char *parse_driver_arg(const char *args)
80 if (!args || args[0] == '\0')
86 if (strncmp(c, "driver=", 7) == 0) {
105 vdev_probe_all_drivers(struct rte_vdev_device *dev)
109 struct rte_vdev_driver *driver;
112 drv_name = parse_driver_arg(rte_vdev_device_args(dev));
113 name = drv_name ? drv_name : rte_vdev_device_name(dev);
115 RTE_LOG(DEBUG, EAL, "Search driver %s to probe device %s\n", name,
116 rte_vdev_device_name(dev));
118 TAILQ_FOREACH(driver, &vdev_driver_list, next) {
120 * search a driver prefix in virtual device name.
121 * For example, if the driver is pcap PMD, driver->name
122 * will be "net_pcap", but "name" will be "net_pcapN".
123 * So use strncmp to compare.
125 if (!strncmp(driver->driver.name, name,
126 strlen(driver->driver.name))) {
127 dev->device.driver = &driver->driver;
128 ret = driver->probe(dev);
130 dev->device.driver = NULL;
135 /* Give new names precedence over aliases. */
136 TAILQ_FOREACH(driver, &vdev_driver_list, next) {
137 if (driver->driver.alias &&
138 !strncmp(driver->driver.alias, name,
139 strlen(driver->driver.alias))) {
140 dev->device.driver = &driver->driver;
141 ret = driver->probe(dev);
143 dev->device.driver = NULL;
153 static struct rte_vdev_device *
154 find_vdev(const char *name)
156 struct rte_vdev_device *dev;
161 TAILQ_FOREACH(dev, &vdev_device_list, next) {
162 const char *devname = rte_vdev_device_name(dev);
163 if (!strncmp(devname, name, strlen(name)))
170 static struct rte_devargs *
171 alloc_devargs(const char *name, const char *args)
173 struct rte_devargs *devargs;
176 devargs = calloc(1, sizeof(*devargs));
180 devargs->type = RTE_DEVTYPE_VIRTUAL;
182 devargs->args = strdup(args);
184 ret = snprintf(devargs->virt.drv_name,
185 sizeof(devargs->virt.drv_name), "%s", name);
186 if (ret < 0 || ret >= (int)sizeof(devargs->virt.drv_name)) {
196 rte_vdev_init(const char *name, const char *args)
198 struct rte_vdev_device *dev;
199 struct rte_devargs *devargs;
205 dev = find_vdev(name);
209 devargs = alloc_devargs(name, args);
213 dev = calloc(1, sizeof(*dev));
219 dev->device.devargs = devargs;
220 dev->device.numa_node = SOCKET_ID_ANY;
221 dev->device.name = devargs->virt.drv_name;
223 ret = vdev_probe_all_drivers(dev);
226 RTE_LOG(ERR, EAL, "no driver found for %s\n", name);
230 TAILQ_INSERT_TAIL(&devargs_list, devargs, next);
232 TAILQ_INSERT_TAIL(&vdev_device_list, dev, next);
243 vdev_remove_driver(struct rte_vdev_device *dev)
245 const char *name = rte_vdev_device_name(dev);
246 const struct rte_vdev_driver *driver;
248 if (!dev->device.driver) {
249 RTE_LOG(DEBUG, EAL, "no driver attach to device %s\n", name);
253 driver = container_of(dev->device.driver, const struct rte_vdev_driver,
255 return driver->remove(dev);
259 rte_vdev_uninit(const char *name)
261 struct rte_vdev_device *dev;
262 struct rte_devargs *devargs;
268 dev = find_vdev(name);
272 devargs = dev->device.devargs;
274 ret = vdev_remove_driver(dev);
278 TAILQ_REMOVE(&vdev_device_list, dev, next);
280 TAILQ_REMOVE(&devargs_list, devargs, next);
291 struct rte_vdev_device *dev;
292 struct rte_devargs *devargs;
294 /* for virtual devices we scan the devargs_list populated via cmdline */
296 TAILQ_FOREACH(devargs, &devargs_list, next) {
298 if (devargs->type != RTE_DEVTYPE_VIRTUAL)
301 dev = find_vdev(devargs->virt.drv_name);
305 dev = calloc(1, sizeof(*dev));
309 dev->device.devargs = devargs;
310 dev->device.numa_node = SOCKET_ID_ANY;
311 dev->device.name = devargs->virt.drv_name;
313 TAILQ_INSERT_TAIL(&vdev_device_list, dev, next);
322 struct rte_vdev_device *dev;
324 /* call the init function for each virtual device */
325 TAILQ_FOREACH(dev, &vdev_device_list, next) {
327 if (dev->device.driver)
330 if (vdev_probe_all_drivers(dev)) {
331 RTE_LOG(ERR, EAL, "failed to initialize %s device\n",
332 rte_vdev_device_name(dev));
340 static struct rte_device *
341 vdev_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
344 struct rte_vdev_device *dev;
345 bool start_found = !start;
347 TAILQ_FOREACH(dev, &vdev_device_list, next) {
348 if (start_found == 0) {
349 if (&dev->device == start)
353 if (cmp(&dev->device, data) == 0)
360 vdev_unplug(struct rte_device *dev)
363 * The virtual bus doesn't support 'unattached' devices so this is
364 * actually equal to hotplugging removal of it.
366 return rte_vdev_uninit(dev->name);
369 static struct rte_bus rte_vdev_bus = {
372 .find_device = vdev_find_device,
373 /* .plug = NULL, see comment on vdev_unplug */
374 .unplug = vdev_unplug,
377 RTE_REGISTER_BUS(VIRTUAL_BUS_NAME, rte_vdev_bus);