1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2016 RehiveTech. All rights reserved.
11 #include <sys/queue.h>
16 #include <rte_common.h>
17 #include <rte_devargs.h>
18 #include <rte_memory.h>
19 #include <rte_tailq.h>
20 #include <rte_spinlock.h>
21 #include <rte_string_fns.h>
22 #include <rte_errno.h>
24 #include "rte_bus_vdev.h"
25 #include "vdev_logs.h"
26 #include "vdev_private.h"
28 #define VDEV_MP_KEY "bus_vdev_mp"
30 /* Forward declare to access virtual bus name */
31 static struct rte_bus rte_vdev_bus;
33 /** Double linked list of virtual device drivers. */
34 TAILQ_HEAD(vdev_device_list, rte_vdev_device);
36 static struct vdev_device_list vdev_device_list =
37 TAILQ_HEAD_INITIALIZER(vdev_device_list);
38 /* The lock needs to be recursive because a vdev can manage another vdev. */
39 static rte_spinlock_recursive_t vdev_device_list_lock =
40 RTE_SPINLOCK_RECURSIVE_INITIALIZER;
42 static struct vdev_driver_list vdev_driver_list =
43 TAILQ_HEAD_INITIALIZER(vdev_driver_list);
45 struct vdev_custom_scan {
46 TAILQ_ENTRY(vdev_custom_scan) next;
47 rte_vdev_scan_callback callback;
50 TAILQ_HEAD(vdev_custom_scans, vdev_custom_scan);
51 static struct vdev_custom_scans vdev_custom_scans =
52 TAILQ_HEAD_INITIALIZER(vdev_custom_scans);
53 static rte_spinlock_t vdev_custom_scan_lock = RTE_SPINLOCK_INITIALIZER;
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 rte_vdev_add_custom_scan(rte_vdev_scan_callback callback, void *user_arg)
72 struct vdev_custom_scan *custom_scan;
74 rte_spinlock_lock(&vdev_custom_scan_lock);
76 /* check if already registered */
77 TAILQ_FOREACH(custom_scan, &vdev_custom_scans, next) {
78 if (custom_scan->callback == callback &&
79 custom_scan->user_arg == user_arg)
83 if (custom_scan == NULL) {
84 custom_scan = malloc(sizeof(struct vdev_custom_scan));
85 if (custom_scan != NULL) {
86 custom_scan->callback = callback;
87 custom_scan->user_arg = user_arg;
88 TAILQ_INSERT_TAIL(&vdev_custom_scans, custom_scan, next);
92 rte_spinlock_unlock(&vdev_custom_scan_lock);
94 return (custom_scan == NULL) ? -1 : 0;
98 rte_vdev_remove_custom_scan(rte_vdev_scan_callback callback, void *user_arg)
100 struct vdev_custom_scan *custom_scan, *tmp_scan;
102 rte_spinlock_lock(&vdev_custom_scan_lock);
103 TAILQ_FOREACH_SAFE(custom_scan, &vdev_custom_scans, next, tmp_scan) {
104 if (custom_scan->callback != callback ||
105 (custom_scan->user_arg != (void *)-1 &&
106 custom_scan->user_arg != user_arg))
108 TAILQ_REMOVE(&vdev_custom_scans, custom_scan, next);
111 rte_spinlock_unlock(&vdev_custom_scan_lock);
117 vdev_parse(const char *name, void *addr)
119 struct rte_vdev_driver **out = addr;
120 struct rte_vdev_driver *driver = NULL;
122 TAILQ_FOREACH(driver, &vdev_driver_list, next) {
123 if (strncmp(driver->driver.name, name,
124 strlen(driver->driver.name)) == 0)
126 if (driver->driver.alias &&
127 strncmp(driver->driver.alias, name,
128 strlen(driver->driver.alias)) == 0)
131 if (driver != NULL &&
134 return driver == NULL;
138 vdev_probe_all_drivers(struct rte_vdev_device *dev)
141 struct rte_vdev_driver *driver;
144 if (rte_dev_is_probed(&dev->device))
147 name = rte_vdev_device_name(dev);
148 VDEV_LOG(DEBUG, "Search driver to probe device %s", name);
150 if (vdev_parse(name, &driver))
152 ret = driver->probe(dev);
154 dev->device.driver = &driver->driver;
158 /* The caller shall be responsible for thread-safe */
159 static struct rte_vdev_device *
160 find_vdev(const char *name)
162 struct rte_vdev_device *dev;
167 TAILQ_FOREACH(dev, &vdev_device_list, next) {
168 const char *devname = rte_vdev_device_name(dev);
170 if (!strcmp(devname, name))
177 static struct rte_devargs *
178 alloc_devargs(const char *name, const char *args)
180 struct rte_devargs *devargs;
183 devargs = calloc(1, sizeof(*devargs));
187 devargs->bus = &rte_vdev_bus;
189 devargs->args = strdup(args);
191 devargs->args = strdup("");
193 ret = strlcpy(devargs->name, name, sizeof(devargs->name));
194 if (ret < 0 || ret >= (int)sizeof(devargs->name)) {
204 insert_vdev(const char *name, const char *args,
205 struct rte_vdev_device **p_dev,
208 struct rte_vdev_device *dev;
209 struct rte_devargs *devargs;
215 devargs = alloc_devargs(name, args);
219 dev = calloc(1, sizeof(*dev));
225 dev->device.bus = &rte_vdev_bus;
226 dev->device.numa_node = SOCKET_ID_ANY;
227 dev->device.name = devargs->name;
229 if (find_vdev(name)) {
231 * A vdev is expected to have only one port.
232 * So there is no reason to try probing again,
233 * even with new arguments.
240 rte_devargs_insert(&devargs);
241 dev->device.devargs = devargs;
242 TAILQ_INSERT_TAIL(&vdev_device_list, dev, next);
256 rte_vdev_init(const char *name, const char *args)
258 struct rte_vdev_device *dev;
261 rte_spinlock_recursive_lock(&vdev_device_list_lock);
262 ret = insert_vdev(name, args, &dev, true);
264 ret = vdev_probe_all_drivers(dev);
267 VDEV_LOG(ERR, "no driver found for %s", name);
268 /* If fails, remove it from vdev list */
269 TAILQ_REMOVE(&vdev_device_list, dev, next);
270 rte_devargs_remove(dev->device.devargs);
274 rte_spinlock_recursive_unlock(&vdev_device_list_lock);
279 vdev_remove_driver(struct rte_vdev_device *dev)
281 const char *name = rte_vdev_device_name(dev);
282 const struct rte_vdev_driver *driver;
284 if (!dev->device.driver) {
285 VDEV_LOG(DEBUG, "no driver attach to device %s", name);
289 driver = container_of(dev->device.driver, const struct rte_vdev_driver,
291 return driver->remove(dev);
295 rte_vdev_uninit(const char *name)
297 struct rte_vdev_device *dev;
303 rte_spinlock_recursive_lock(&vdev_device_list_lock);
305 dev = find_vdev(name);
311 ret = vdev_remove_driver(dev);
315 TAILQ_REMOVE(&vdev_device_list, dev, next);
316 rte_devargs_remove(dev->device.devargs);
320 rte_spinlock_recursive_unlock(&vdev_device_list_lock);
325 #define VDEV_SCAN_REQ 1
326 #define VDEV_SCAN_ONE 2
327 #define VDEV_SCAN_REP 3
330 char name[RTE_DEV_NAME_MAX_LEN];
333 static int vdev_plug(struct rte_device *dev);
336 * This function works as the action for both primary and secondary process
337 * for static vdev discovery when a secondary process is booting.
339 * step 1, secondary process sends a sync request to ask for vdev in primary;
340 * step 2, primary process receives the request, and send vdevs one by one;
341 * step 3, primary process sends back reply, which indicates how many vdevs
345 vdev_action(const struct rte_mp_msg *mp_msg, const void *peer)
347 struct rte_vdev_device *dev;
348 struct rte_mp_msg mp_resp;
349 struct vdev_param *ou = (struct vdev_param *)&mp_resp.param;
350 const struct vdev_param *in = (const struct vdev_param *)mp_msg->param;
355 strlcpy(mp_resp.name, VDEV_MP_KEY, sizeof(mp_resp.name));
356 mp_resp.len_param = sizeof(*ou);
361 ou->type = VDEV_SCAN_ONE;
365 rte_spinlock_recursive_lock(&vdev_device_list_lock);
366 TAILQ_FOREACH(dev, &vdev_device_list, next) {
367 devname = rte_vdev_device_name(dev);
368 if (strlen(devname) == 0) {
369 VDEV_LOG(INFO, "vdev with no name is not sent");
372 VDEV_LOG(INFO, "send vdev, %s", devname);
373 strlcpy(ou->name, devname, RTE_DEV_NAME_MAX_LEN);
374 if (rte_mp_sendmsg(&mp_resp) < 0)
375 VDEV_LOG(ERR, "send vdev, %s, failed, %s",
376 devname, strerror(rte_errno));
379 rte_spinlock_recursive_unlock(&vdev_device_list_lock);
381 ou->type = VDEV_SCAN_REP;
383 if (rte_mp_reply(&mp_resp, peer) < 0)
384 VDEV_LOG(ERR, "Failed to reply a scan request");
387 VDEV_LOG(INFO, "receive vdev, %s", in->name);
388 ret = insert_vdev(in->name, NULL, NULL, false);
390 VDEV_LOG(DEBUG, "device already exist, %s", in->name);
392 VDEV_LOG(ERR, "failed to add vdev, %s", in->name);
395 VDEV_LOG(ERR, "vdev cannot recognize this message");
404 struct rte_vdev_device *dev;
405 struct rte_devargs *devargs;
406 struct vdev_custom_scan *custom_scan;
408 if (rte_mp_action_register(VDEV_MP_KEY, vdev_action) < 0 &&
409 rte_errno != EEXIST) {
410 /* for primary, unsupported IPC is not an error */
411 if (rte_eal_process_type() == RTE_PROC_PRIMARY &&
412 rte_errno == ENOTSUP)
414 VDEV_LOG(ERR, "Failed to add vdev mp action");
418 if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
419 struct rte_mp_msg mp_req, *mp_rep;
420 struct rte_mp_reply mp_reply;
421 struct timespec ts = {.tv_sec = 5, .tv_nsec = 0};
422 struct vdev_param *req = (struct vdev_param *)mp_req.param;
423 struct vdev_param *resp;
425 strlcpy(mp_req.name, VDEV_MP_KEY, sizeof(mp_req.name));
426 mp_req.len_param = sizeof(*req);
428 req->type = VDEV_SCAN_REQ;
429 if (rte_mp_request_sync(&mp_req, &mp_reply, &ts) == 0 &&
430 mp_reply.nb_received == 1) {
431 mp_rep = &mp_reply.msgs[0];
432 resp = (struct vdev_param *)mp_rep->param;
433 VDEV_LOG(INFO, "Received %d vdevs", resp->num);
436 VDEV_LOG(ERR, "Failed to request vdev from primary");
438 /* Fall through to allow private vdevs in secondary process */
442 /* call custom scan callbacks if any */
443 rte_spinlock_lock(&vdev_custom_scan_lock);
444 TAILQ_FOREACH(custom_scan, &vdev_custom_scans, next) {
445 if (custom_scan->callback != NULL)
447 * the callback should update devargs list
448 * by calling rte_devargs_insert() with
449 * devargs.bus = rte_bus_find_by_name("vdev");
450 * devargs.type = RTE_DEVTYPE_VIRTUAL;
451 * devargs.policy = RTE_DEV_WHITELISTED;
453 custom_scan->callback(custom_scan->user_arg);
455 rte_spinlock_unlock(&vdev_custom_scan_lock);
457 /* for virtual devices we scan the devargs_list populated via cmdline */
458 RTE_EAL_DEVARGS_FOREACH("vdev", devargs) {
460 dev = calloc(1, sizeof(*dev));
464 rte_spinlock_recursive_lock(&vdev_device_list_lock);
466 if (find_vdev(devargs->name)) {
467 rte_spinlock_recursive_unlock(&vdev_device_list_lock);
472 dev->device.bus = &rte_vdev_bus;
473 dev->device.devargs = devargs;
474 dev->device.numa_node = SOCKET_ID_ANY;
475 dev->device.name = devargs->name;
477 TAILQ_INSERT_TAIL(&vdev_device_list, dev, next);
479 rte_spinlock_recursive_unlock(&vdev_device_list_lock);
488 struct rte_vdev_device *dev;
491 /* call the init function for each virtual device */
492 TAILQ_FOREACH(dev, &vdev_device_list, next) {
493 /* we don't use the vdev lock here, as it's only used in DPDK
494 * initialization; and we don't want to hold such a lock when
495 * we call each driver probe.
498 r = vdev_probe_all_drivers(dev);
502 VDEV_LOG(ERR, "failed to initialize %s device",
503 rte_vdev_device_name(dev));
512 rte_vdev_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
515 const struct rte_vdev_device *vstart;
516 struct rte_vdev_device *dev;
518 rte_spinlock_recursive_lock(&vdev_device_list_lock);
520 vstart = RTE_DEV_TO_VDEV_CONST(start);
521 dev = TAILQ_NEXT(vstart, next);
523 dev = TAILQ_FIRST(&vdev_device_list);
525 while (dev != NULL) {
526 if (cmp(&dev->device, data) == 0)
528 dev = TAILQ_NEXT(dev, next);
530 rte_spinlock_recursive_unlock(&vdev_device_list_lock);
532 return dev ? &dev->device : NULL;
536 vdev_plug(struct rte_device *dev)
538 return vdev_probe_all_drivers(RTE_DEV_TO_VDEV(dev));
542 vdev_unplug(struct rte_device *dev)
544 return rte_vdev_uninit(dev->name);
547 static struct rte_bus rte_vdev_bus = {
550 .find_device = rte_vdev_find_device,
552 .unplug = vdev_unplug,
554 .dev_iterate = rte_vdev_dev_iterate,
557 RTE_REGISTER_BUS(vdev, rte_vdev_bus);
558 RTE_LOG_REGISTER(vdev_logtype_bus, bus.vdev, NOTICE);