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"
27 #define VDEV_MP_KEY "bus_vdev_mp"
31 /* Forward declare to access virtual bus name */
32 static struct rte_bus rte_vdev_bus;
34 /** Double linked list of virtual device drivers. */
35 TAILQ_HEAD(vdev_device_list, rte_vdev_device);
37 static struct vdev_device_list vdev_device_list =
38 TAILQ_HEAD_INITIALIZER(vdev_device_list);
39 static rte_spinlock_t vdev_device_list_lock = RTE_SPINLOCK_INITIALIZER;
41 struct vdev_driver_list vdev_driver_list =
42 TAILQ_HEAD_INITIALIZER(vdev_driver_list);
44 struct vdev_custom_scan {
45 TAILQ_ENTRY(vdev_custom_scan) next;
46 rte_vdev_scan_callback callback;
49 TAILQ_HEAD(vdev_custom_scans, vdev_custom_scan);
50 static struct vdev_custom_scans vdev_custom_scans =
51 TAILQ_HEAD_INITIALIZER(vdev_custom_scans);
52 static rte_spinlock_t vdev_custom_scan_lock = RTE_SPINLOCK_INITIALIZER;
54 /* register a driver */
56 rte_vdev_register(struct rte_vdev_driver *driver)
58 TAILQ_INSERT_TAIL(&vdev_driver_list, driver, next);
61 /* unregister a driver */
63 rte_vdev_unregister(struct rte_vdev_driver *driver)
65 TAILQ_REMOVE(&vdev_driver_list, driver, next);
69 rte_vdev_add_custom_scan(rte_vdev_scan_callback callback, void *user_arg)
71 struct vdev_custom_scan *custom_scan;
73 rte_spinlock_lock(&vdev_custom_scan_lock);
75 /* check if already registered */
76 TAILQ_FOREACH(custom_scan, &vdev_custom_scans, next) {
77 if (custom_scan->callback == callback &&
78 custom_scan->user_arg == user_arg)
82 if (custom_scan == NULL) {
83 custom_scan = malloc(sizeof(struct vdev_custom_scan));
84 if (custom_scan != NULL) {
85 custom_scan->callback = callback;
86 custom_scan->user_arg = user_arg;
87 TAILQ_INSERT_TAIL(&vdev_custom_scans, custom_scan, next);
91 rte_spinlock_unlock(&vdev_custom_scan_lock);
93 return (custom_scan == NULL) ? -1 : 0;
97 rte_vdev_remove_custom_scan(rte_vdev_scan_callback callback, void *user_arg)
99 struct vdev_custom_scan *custom_scan, *tmp_scan;
101 rte_spinlock_lock(&vdev_custom_scan_lock);
102 TAILQ_FOREACH_SAFE(custom_scan, &vdev_custom_scans, next, tmp_scan) {
103 if (custom_scan->callback != callback ||
104 (custom_scan->user_arg != (void *)-1 &&
105 custom_scan->user_arg != user_arg))
107 TAILQ_REMOVE(&vdev_custom_scans, custom_scan, next);
110 rte_spinlock_unlock(&vdev_custom_scan_lock);
116 vdev_parse(const char *name, void *addr)
118 struct rte_vdev_driver **out = addr;
119 struct rte_vdev_driver *driver = NULL;
121 TAILQ_FOREACH(driver, &vdev_driver_list, next) {
122 if (strncmp(driver->driver.name, name,
123 strlen(driver->driver.name)) == 0)
125 if (driver->driver.alias &&
126 strncmp(driver->driver.alias, name,
127 strlen(driver->driver.alias)) == 0)
130 if (driver != NULL &&
133 return driver == NULL;
137 vdev_probe_all_drivers(struct rte_vdev_device *dev)
140 struct rte_vdev_driver *driver;
143 name = rte_vdev_device_name(dev);
145 VDEV_LOG(DEBUG, "Search driver %s to probe device %s\n", name,
146 rte_vdev_device_name(dev));
148 if (vdev_parse(name, &driver))
150 dev->device.driver = &driver->driver;
151 ret = driver->probe(dev);
153 dev->device.driver = NULL;
157 /* The caller shall be responsible for thread-safe */
158 static struct rte_vdev_device *
159 find_vdev(const char *name)
161 struct rte_vdev_device *dev;
166 TAILQ_FOREACH(dev, &vdev_device_list, next) {
167 const char *devname = rte_vdev_device_name(dev);
169 if (!strcmp(devname, name))
176 static struct rte_devargs *
177 alloc_devargs(const char *name, const char *args)
179 struct rte_devargs *devargs;
182 devargs = calloc(1, sizeof(*devargs));
186 devargs->bus = &rte_vdev_bus;
188 devargs->args = strdup(args);
190 devargs->args = strdup("");
192 ret = snprintf(devargs->name, sizeof(devargs->name), "%s", name);
193 if (ret < 0 || ret >= (int)sizeof(devargs->name)) {
203 insert_vdev(const char *name, const char *args, struct rte_vdev_device **p_dev)
205 struct rte_vdev_device *dev;
206 struct rte_devargs *devargs;
212 devargs = alloc_devargs(name, args);
216 dev = calloc(1, sizeof(*dev));
222 dev->device.devargs = devargs;
223 dev->device.numa_node = SOCKET_ID_ANY;
224 dev->device.name = devargs->name;
226 if (find_vdev(name)) {
231 TAILQ_INSERT_TAIL(&vdev_device_list, dev, next);
232 rte_devargs_insert(devargs);
246 rte_vdev_init(const char *name, const char *args)
248 struct rte_vdev_device *dev;
249 struct rte_devargs *devargs;
252 rte_spinlock_lock(&vdev_device_list_lock);
253 ret = insert_vdev(name, args, &dev);
255 ret = vdev_probe_all_drivers(dev);
258 VDEV_LOG(ERR, "no driver found for %s\n", name);
259 /* If fails, remove it from vdev list */
260 devargs = dev->device.devargs;
261 TAILQ_REMOVE(&vdev_device_list, dev, next);
262 rte_devargs_remove(devargs->bus->name, devargs->name);
266 rte_spinlock_unlock(&vdev_device_list_lock);
271 vdev_remove_driver(struct rte_vdev_device *dev)
273 const char *name = rte_vdev_device_name(dev);
274 const struct rte_vdev_driver *driver;
276 if (!dev->device.driver) {
277 VDEV_LOG(DEBUG, "no driver attach to device %s\n", name);
281 driver = container_of(dev->device.driver, const struct rte_vdev_driver,
283 return driver->remove(dev);
287 rte_vdev_uninit(const char *name)
289 struct rte_vdev_device *dev;
290 struct rte_devargs *devargs;
296 rte_spinlock_lock(&vdev_device_list_lock);
298 dev = find_vdev(name);
304 ret = vdev_remove_driver(dev);
308 TAILQ_REMOVE(&vdev_device_list, dev, next);
309 devargs = dev->device.devargs;
310 rte_devargs_remove(devargs->bus->name, devargs->name);
314 rte_spinlock_unlock(&vdev_device_list_lock);
319 #define VDEV_SCAN_REQ 1
320 #define VDEV_SCAN_ONE 2
321 #define VDEV_SCAN_REP 3
324 char name[RTE_DEV_NAME_MAX_LEN];
327 static int vdev_plug(struct rte_device *dev);
330 * This function works as the action for both primary and secondary process
331 * for static vdev discovery when a secondary process is booting.
333 * step 1, secondary process sends a sync request to ask for vdev in primary;
334 * step 2, primary process receives the request, and send vdevs one by one;
335 * step 3, primary process sends back reply, which indicates how many vdevs
339 vdev_action(const struct rte_mp_msg *mp_msg, const void *peer)
341 struct rte_vdev_device *dev;
342 struct rte_mp_msg mp_resp;
343 struct vdev_param *ou = (struct vdev_param *)&mp_resp.param;
344 const struct vdev_param *in = (const struct vdev_param *)mp_msg->param;
348 strlcpy(mp_resp.name, VDEV_MP_KEY, sizeof(mp_resp.name));
349 mp_resp.len_param = sizeof(*ou);
354 ou->type = VDEV_SCAN_ONE;
358 rte_spinlock_lock(&vdev_device_list_lock);
359 TAILQ_FOREACH(dev, &vdev_device_list, next) {
360 devname = rte_vdev_device_name(dev);
361 if (strlen(devname) == 0) {
362 VDEV_LOG(INFO, "vdev with no name is not sent");
365 VDEV_LOG(INFO, "send vdev, %s", devname);
366 strlcpy(ou->name, devname, RTE_DEV_NAME_MAX_LEN);
367 if (rte_mp_sendmsg(&mp_resp) < 0)
368 VDEV_LOG(ERR, "send vdev, %s, failed, %s",
369 devname, strerror(rte_errno));
372 rte_spinlock_unlock(&vdev_device_list_lock);
374 ou->type = VDEV_SCAN_REP;
376 if (rte_mp_reply(&mp_resp, peer) < 0)
377 VDEV_LOG(ERR, "Failed to reply a scan request");
380 VDEV_LOG(INFO, "receive vdev, %s", in->name);
381 if (insert_vdev(in->name, NULL, NULL) < 0)
382 VDEV_LOG(ERR, "failed to add vdev, %s", in->name);
385 VDEV_LOG(ERR, "vdev cannot recognize this message");
394 struct rte_vdev_device *dev;
395 struct rte_devargs *devargs;
396 struct vdev_custom_scan *custom_scan;
398 if (rte_mp_action_register(VDEV_MP_KEY, vdev_action) < 0 &&
399 rte_errno != EEXIST) {
400 VDEV_LOG(ERR, "Failed to add vdev mp action");
404 if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
405 struct rte_mp_msg mp_req, *mp_rep;
406 struct rte_mp_reply mp_reply;
407 struct timespec ts = {.tv_sec = 5, .tv_nsec = 0};
408 struct vdev_param *req = (struct vdev_param *)mp_req.param;
409 struct vdev_param *resp;
411 strlcpy(mp_req.name, VDEV_MP_KEY, sizeof(mp_req.name));
412 mp_req.len_param = sizeof(*req);
414 req->type = VDEV_SCAN_REQ;
415 if (rte_mp_request_sync(&mp_req, &mp_reply, &ts) == 0 &&
416 mp_reply.nb_received == 1) {
417 mp_rep = &mp_reply.msgs[0];
418 resp = (struct vdev_param *)mp_rep->param;
419 VDEV_LOG(INFO, "Received %d vdevs", resp->num);
421 VDEV_LOG(ERR, "Failed to request vdev from primary");
423 /* Fall through to allow private vdevs in secondary process */
426 /* call custom scan callbacks if any */
427 rte_spinlock_lock(&vdev_custom_scan_lock);
428 TAILQ_FOREACH(custom_scan, &vdev_custom_scans, next) {
429 if (custom_scan->callback != NULL)
431 * the callback should update devargs list
432 * by calling rte_devargs_insert() with
433 * devargs.bus = rte_bus_find_by_name("vdev");
434 * devargs.type = RTE_DEVTYPE_VIRTUAL;
435 * devargs.policy = RTE_DEV_WHITELISTED;
437 custom_scan->callback(custom_scan->user_arg);
439 rte_spinlock_unlock(&vdev_custom_scan_lock);
441 /* for virtual devices we scan the devargs_list populated via cmdline */
442 RTE_EAL_DEVARGS_FOREACH("vdev", devargs) {
444 dev = calloc(1, sizeof(*dev));
448 rte_spinlock_lock(&vdev_device_list_lock);
450 if (find_vdev(devargs->name)) {
451 rte_spinlock_unlock(&vdev_device_list_lock);
456 dev->device.devargs = devargs;
457 dev->device.numa_node = SOCKET_ID_ANY;
458 dev->device.name = devargs->name;
460 TAILQ_INSERT_TAIL(&vdev_device_list, dev, next);
462 rte_spinlock_unlock(&vdev_device_list_lock);
471 struct rte_vdev_device *dev;
474 /* call the init function for each virtual device */
475 TAILQ_FOREACH(dev, &vdev_device_list, next) {
476 /* we don't use the vdev lock here, as it's only used in DPDK
477 * initialization; and we don't want to hold such a lock when
478 * we call each driver probe.
481 if (dev->device.driver)
484 if (vdev_probe_all_drivers(dev)) {
485 VDEV_LOG(ERR, "failed to initialize %s device\n",
486 rte_vdev_device_name(dev));
494 static struct rte_device *
495 vdev_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
498 struct rte_vdev_device *dev;
500 rte_spinlock_lock(&vdev_device_list_lock);
501 TAILQ_FOREACH(dev, &vdev_device_list, next) {
502 if (start && &dev->device == start) {
506 if (cmp(&dev->device, data) == 0)
509 rte_spinlock_unlock(&vdev_device_list_lock);
511 return dev ? &dev->device : NULL;
515 vdev_plug(struct rte_device *dev)
517 return vdev_probe_all_drivers(RTE_DEV_TO_VDEV(dev));
521 vdev_unplug(struct rte_device *dev)
523 return rte_vdev_uninit(dev->name);
526 static struct rte_bus rte_vdev_bus = {
529 .find_device = vdev_find_device,
531 .unplug = vdev_unplug,
535 RTE_REGISTER_BUS(vdev, rte_vdev_bus);
537 RTE_INIT(vdev_init_log)
539 vdev_logtype_bus = rte_log_register("bus.vdev");
540 if (vdev_logtype_bus >= 0)
541 rte_log_set_level(vdev_logtype_bus, RTE_LOG_NOTICE);