1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2016 Intel Corporation
9 #include <linux/major.h>
11 #include <sys/sysmacros.h>
12 #include <sys/socket.h>
14 #include <rte_malloc.h>
15 #include <rte_kvargs.h>
16 #include <ethdev_vdev.h>
17 #include <rte_bus_vdev.h>
18 #include <rte_alarm.h>
19 #include <rte_cycles.h>
21 #include "virtio_ethdev.h"
22 #include "virtio_logs.h"
24 #include "virtqueue.h"
25 #include "virtio_rxtx.h"
26 #include "virtio_user/virtio_user_dev.h"
27 #include "virtio_user/vhost.h"
29 #define virtio_user_get_dev(hwp) container_of(hwp, struct virtio_user_dev, hw)
32 virtio_user_reset_queues_packed(struct rte_eth_dev *eth_dev)
34 struct virtio_user_dev *dev = eth_dev->data->dev_private;
35 struct virtio_hw *hw = &dev->hw;
36 struct virtnet_rx *rxvq;
37 struct virtnet_tx *txvq;
40 /* Add lock to avoid queue contention. */
41 rte_spinlock_lock(&hw->state_lock);
45 * Waitting for datapath to complete before resetting queues.
46 * 1 ms should be enough for the ongoing Tx/Rx function to finish.
50 /* Vring reset for each Tx queue and Rx queue. */
51 for (i = 0; i < eth_dev->data->nb_rx_queues; i++) {
52 rxvq = eth_dev->data->rx_queues[i];
53 virtqueue_rxvq_reset_packed(rxvq->vq);
54 virtio_dev_rx_queue_setup_finish(eth_dev, i);
57 for (i = 0; i < eth_dev->data->nb_tx_queues; i++) {
58 txvq = eth_dev->data->tx_queues[i];
59 virtqueue_txvq_reset_packed(txvq->vq);
63 rte_spinlock_unlock(&hw->state_lock);
68 virtio_user_server_reconnect(struct virtio_user_dev *dev)
70 int ret, connectfd, old_status;
71 struct rte_eth_dev *eth_dev = &rte_eth_devices[dev->port_id];
72 struct virtio_hw *hw = &dev->hw;
73 uint64_t protocol_features;
75 connectfd = accept(dev->listenfd, NULL, NULL);
79 dev->vhostfd = connectfd;
80 old_status = dev->status;
84 virtio_set_status(hw, VIRTIO_CONFIG_STATUS_ACK);
86 virtio_set_status(hw, VIRTIO_CONFIG_STATUS_DRIVER);
88 if (dev->ops->get_features(dev, &dev->device_features) < 0) {
89 PMD_INIT_LOG(ERR, "get_features failed: %s",
94 if (dev->device_features &
95 (1ULL << VHOST_USER_F_PROTOCOL_FEATURES)) {
96 if (dev->ops->get_protocol_features(dev, &protocol_features))
99 /* Offer VHOST_USER_PROTOCOL_F_STATUS */
100 dev->protocol_features |= (1ULL << VHOST_USER_PROTOCOL_F_STATUS);
101 dev->protocol_features &= protocol_features;
103 if (dev->ops->set_protocol_features(dev, dev->protocol_features))
106 if (!(dev->protocol_features & (1ULL << VHOST_USER_PROTOCOL_F_MQ)))
107 dev->unsupported_features |= (1ull << VIRTIO_NET_F_MQ);
110 dev->device_features |= dev->frontend_features;
112 /* umask vhost-user unsupported features */
113 dev->device_features &= ~(dev->unsupported_features);
115 dev->features &= dev->device_features;
117 /* For packed ring, resetting queues is required in reconnection. */
118 if (virtio_with_packed_queue(hw) &&
119 (old_status & VIRTIO_CONFIG_STATUS_DRIVER_OK)) {
120 PMD_INIT_LOG(NOTICE, "Packets on the fly will be dropped"
121 " when packed ring reconnecting.");
122 virtio_user_reset_queues_packed(eth_dev);
125 virtio_set_status(hw, VIRTIO_CONFIG_STATUS_FEATURES_OK);
127 /* Start the device */
128 virtio_set_status(hw, VIRTIO_CONFIG_STATUS_DRIVER_OK);
132 if (dev->queue_pairs > 1) {
133 ret = virtio_user_handle_mq(dev, dev->queue_pairs);
135 PMD_INIT_LOG(ERR, "Fails to enable multi-queue pairs!");
139 if (eth_dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC) {
140 if (rte_intr_disable(eth_dev->intr_handle) < 0) {
141 PMD_DRV_LOG(ERR, "interrupt disable failed");
144 rte_intr_callback_unregister(eth_dev->intr_handle,
145 virtio_interrupt_handler,
147 eth_dev->intr_handle->fd = connectfd;
148 rte_intr_callback_register(eth_dev->intr_handle,
149 virtio_interrupt_handler, eth_dev);
151 if (rte_intr_enable(eth_dev->intr_handle) < 0) {
152 PMD_DRV_LOG(ERR, "interrupt enable failed");
156 PMD_INIT_LOG(NOTICE, "server mode virtio-user reconnection succeeds!");
161 virtio_user_delayed_handler(void *param)
163 struct virtio_hw *hw = (struct virtio_hw *)param;
164 struct rte_eth_dev *eth_dev = &rte_eth_devices[hw->port_id];
165 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
167 if (rte_intr_disable(eth_dev->intr_handle) < 0) {
168 PMD_DRV_LOG(ERR, "interrupt disable failed");
171 rte_intr_callback_unregister(eth_dev->intr_handle,
172 virtio_interrupt_handler, eth_dev);
173 if (dev->is_server) {
174 if (dev->vhostfd >= 0) {
178 eth_dev->intr_handle->fd = dev->listenfd;
179 rte_intr_callback_register(eth_dev->intr_handle,
180 virtio_interrupt_handler, eth_dev);
181 if (rte_intr_enable(eth_dev->intr_handle) < 0) {
182 PMD_DRV_LOG(ERR, "interrupt enable failed");
189 virtio_user_read_dev_config(struct virtio_hw *hw, size_t offset,
190 void *dst, int length)
193 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
195 if (offset == offsetof(struct virtio_net_config, mac) &&
196 length == RTE_ETHER_ADDR_LEN) {
197 for (i = 0; i < RTE_ETHER_ADDR_LEN; ++i)
198 ((uint8_t *)dst)[i] = dev->mac_addr[i];
202 if (offset == offsetof(struct virtio_net_config, status)) {
205 if (dev->vhostfd >= 0) {
209 flags = fcntl(dev->vhostfd, F_GETFL);
210 if (fcntl(dev->vhostfd, F_SETFL,
211 flags | O_NONBLOCK) == -1) {
212 PMD_DRV_LOG(ERR, "error setting O_NONBLOCK flag");
215 r = recv(dev->vhostfd, buf, 128, MSG_PEEK);
216 if (r == 0 || (r < 0 && errno != EAGAIN)) {
217 dev->net_status &= (~VIRTIO_NET_S_LINK_UP);
218 PMD_DRV_LOG(ERR, "virtio-user port %u is down",
221 /* This function could be called in the process
222 * of interrupt handling, callback cannot be
223 * unregistered here, set an alarm to do it.
226 virtio_user_delayed_handler,
229 dev->net_status |= VIRTIO_NET_S_LINK_UP;
231 if (fcntl(dev->vhostfd, F_SETFL,
232 flags & ~O_NONBLOCK) == -1) {
233 PMD_DRV_LOG(ERR, "error clearing O_NONBLOCK flag");
236 } else if (dev->is_server) {
237 dev->net_status &= (~VIRTIO_NET_S_LINK_UP);
238 if (virtio_user_server_reconnect(dev) >= 0)
239 dev->net_status |= VIRTIO_NET_S_LINK_UP;
242 *(uint16_t *)dst = dev->net_status;
245 if (offset == offsetof(struct virtio_net_config, max_virtqueue_pairs))
246 *(uint16_t *)dst = dev->max_queue_pairs;
250 virtio_user_write_dev_config(struct virtio_hw *hw, size_t offset,
251 const void *src, int length)
254 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
256 if ((offset == offsetof(struct virtio_net_config, mac)) &&
257 (length == RTE_ETHER_ADDR_LEN))
258 for (i = 0; i < RTE_ETHER_ADDR_LEN; ++i)
259 dev->mac_addr[i] = ((const uint8_t *)src)[i];
261 PMD_DRV_LOG(ERR, "not supported offset=%zu, len=%d",
266 virtio_user_reset(struct virtio_hw *hw)
268 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
270 if (dev->status & VIRTIO_CONFIG_STATUS_DRIVER_OK)
271 virtio_user_stop_device(dev);
275 virtio_user_set_status(struct virtio_hw *hw, uint8_t status)
277 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
278 uint8_t old_status = dev->status;
280 if (status & VIRTIO_CONFIG_STATUS_FEATURES_OK &&
281 ~old_status & VIRTIO_CONFIG_STATUS_FEATURES_OK)
282 virtio_user_dev_set_features(dev);
283 if (status & VIRTIO_CONFIG_STATUS_DRIVER_OK)
284 virtio_user_start_device(dev);
285 else if (status == VIRTIO_CONFIG_STATUS_RESET)
286 virtio_user_reset(hw);
288 virtio_user_dev_set_status(dev, status);
292 virtio_user_get_status(struct virtio_hw *hw)
294 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
296 virtio_user_dev_update_status(dev);
302 virtio_user_get_features(struct virtio_hw *hw)
304 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
306 /* unmask feature bits defined in vhost user protocol */
307 return dev->device_features & VIRTIO_PMD_SUPPORTED_GUEST_FEATURES;
311 virtio_user_set_features(struct virtio_hw *hw, uint64_t features)
313 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
315 dev->features = features & dev->device_features;
319 virtio_user_features_ok(struct virtio_hw *hw __rte_unused)
325 virtio_user_get_isr(struct virtio_hw *hw __rte_unused)
327 /* rxq interrupts and config interrupt are separated in virtio-user,
328 * here we only report config change.
330 return VIRTIO_ISR_CONFIG;
334 virtio_user_set_config_irq(struct virtio_hw *hw __rte_unused,
335 uint16_t vec __rte_unused)
341 virtio_user_set_queue_irq(struct virtio_hw *hw __rte_unused,
342 struct virtqueue *vq __rte_unused,
345 /* pretend we have done that */
349 /* This function is to get the queue size, aka, number of descs, of a specified
350 * queue. Different with the VHOST_USER_GET_QUEUE_NUM, which is used to get the
351 * max supported queues.
354 virtio_user_get_queue_num(struct virtio_hw *hw, uint16_t queue_id __rte_unused)
356 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
358 /* Currently, each queue has same queue size */
359 return dev->queue_size;
363 virtio_user_setup_queue_packed(struct virtqueue *vq,
364 struct virtio_user_dev *dev)
366 uint16_t queue_idx = vq->vq_queue_index;
367 struct vring_packed *vring;
373 vring = &dev->packed_vrings[queue_idx];
374 desc_addr = (uintptr_t)vq->vq_ring_virt_mem;
375 avail_addr = desc_addr + vq->vq_nentries *
376 sizeof(struct vring_packed_desc);
377 used_addr = RTE_ALIGN_CEIL(avail_addr +
378 sizeof(struct vring_packed_desc_event),
380 vring->num = vq->vq_nentries;
381 vring->desc = (void *)(uintptr_t)desc_addr;
382 vring->driver = (void *)(uintptr_t)avail_addr;
383 vring->device = (void *)(uintptr_t)used_addr;
384 dev->packed_queues[queue_idx].avail_wrap_counter = true;
385 dev->packed_queues[queue_idx].used_wrap_counter = true;
387 for (i = 0; i < vring->num; i++)
388 vring->desc[i].flags = 0;
392 virtio_user_setup_queue_split(struct virtqueue *vq, struct virtio_user_dev *dev)
394 uint16_t queue_idx = vq->vq_queue_index;
395 uint64_t desc_addr, avail_addr, used_addr;
397 desc_addr = (uintptr_t)vq->vq_ring_virt_mem;
398 avail_addr = desc_addr + vq->vq_nentries * sizeof(struct vring_desc);
399 used_addr = RTE_ALIGN_CEIL(avail_addr + offsetof(struct vring_avail,
400 ring[vq->vq_nentries]),
403 dev->vrings[queue_idx].num = vq->vq_nentries;
404 dev->vrings[queue_idx].desc = (void *)(uintptr_t)desc_addr;
405 dev->vrings[queue_idx].avail = (void *)(uintptr_t)avail_addr;
406 dev->vrings[queue_idx].used = (void *)(uintptr_t)used_addr;
410 virtio_user_setup_queue(struct virtio_hw *hw, struct virtqueue *vq)
412 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
414 if (virtio_with_packed_queue(hw))
415 virtio_user_setup_queue_packed(vq, dev);
417 virtio_user_setup_queue_split(vq, dev);
423 virtio_user_del_queue(struct virtio_hw *hw, struct virtqueue *vq)
425 /* For legacy devices, write 0 to VIRTIO_PCI_QUEUE_PFN port, QEMU
426 * correspondingly stops the ioeventfds, and reset the status of
428 * For modern devices, set queue desc, avail, used in PCI bar to 0,
429 * not see any more behavior in QEMU.
431 * Here we just care about what information to deliver to vhost-user
432 * or vhost-kernel. So we just close ioeventfd for now.
434 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
436 close(dev->callfds[vq->vq_queue_index]);
437 close(dev->kickfds[vq->vq_queue_index]);
441 virtio_user_notify_queue(struct virtio_hw *hw, struct virtqueue *vq)
444 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
446 if (hw->cvq && (hw->cvq->vq == vq)) {
447 if (virtio_with_packed_queue(vq->hw))
448 virtio_user_handle_cq_packed(dev, vq->vq_queue_index);
450 virtio_user_handle_cq(dev, vq->vq_queue_index);
454 if (write(dev->kickfds[vq->vq_queue_index], &buf, sizeof(buf)) < 0)
455 PMD_DRV_LOG(ERR, "failed to kick backend: %s",
460 virtio_user_dev_close(struct virtio_hw *hw)
462 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
464 virtio_user_dev_uninit(dev);
469 const struct virtio_ops virtio_user_ops = {
470 .read_dev_cfg = virtio_user_read_dev_config,
471 .write_dev_cfg = virtio_user_write_dev_config,
472 .get_status = virtio_user_get_status,
473 .set_status = virtio_user_set_status,
474 .get_features = virtio_user_get_features,
475 .set_features = virtio_user_set_features,
476 .features_ok = virtio_user_features_ok,
477 .get_isr = virtio_user_get_isr,
478 .set_config_irq = virtio_user_set_config_irq,
479 .set_queue_irq = virtio_user_set_queue_irq,
480 .get_queue_num = virtio_user_get_queue_num,
481 .setup_queue = virtio_user_setup_queue,
482 .del_queue = virtio_user_del_queue,
483 .notify_queue = virtio_user_notify_queue,
484 .dev_close = virtio_user_dev_close,
487 static const char *valid_args[] = {
488 #define VIRTIO_USER_ARG_QUEUES_NUM "queues"
489 VIRTIO_USER_ARG_QUEUES_NUM,
490 #define VIRTIO_USER_ARG_CQ_NUM "cq"
491 VIRTIO_USER_ARG_CQ_NUM,
492 #define VIRTIO_USER_ARG_MAC "mac"
494 #define VIRTIO_USER_ARG_PATH "path"
495 VIRTIO_USER_ARG_PATH,
496 #define VIRTIO_USER_ARG_QUEUE_SIZE "queue_size"
497 VIRTIO_USER_ARG_QUEUE_SIZE,
498 #define VIRTIO_USER_ARG_INTERFACE_NAME "iface"
499 VIRTIO_USER_ARG_INTERFACE_NAME,
500 #define VIRTIO_USER_ARG_SERVER_MODE "server"
501 VIRTIO_USER_ARG_SERVER_MODE,
502 #define VIRTIO_USER_ARG_MRG_RXBUF "mrg_rxbuf"
503 VIRTIO_USER_ARG_MRG_RXBUF,
504 #define VIRTIO_USER_ARG_IN_ORDER "in_order"
505 VIRTIO_USER_ARG_IN_ORDER,
506 #define VIRTIO_USER_ARG_PACKED_VQ "packed_vq"
507 VIRTIO_USER_ARG_PACKED_VQ,
508 #define VIRTIO_USER_ARG_SPEED "speed"
509 VIRTIO_USER_ARG_SPEED,
510 #define VIRTIO_USER_ARG_VECTORIZED "vectorized"
511 VIRTIO_USER_ARG_VECTORIZED,
515 #define VIRTIO_USER_DEF_CQ_EN 0
516 #define VIRTIO_USER_DEF_Q_NUM 1
517 #define VIRTIO_USER_DEF_Q_SZ 256
518 #define VIRTIO_USER_DEF_SERVER_MODE 0
521 get_string_arg(const char *key __rte_unused,
522 const char *value, void *extra_args)
524 if (!value || !extra_args)
527 *(char **)extra_args = strdup(value);
529 if (!*(char **)extra_args)
536 get_integer_arg(const char *key __rte_unused,
537 const char *value, void *extra_args)
539 uint64_t integer = 0;
540 if (!value || !extra_args)
543 integer = strtoull(value, NULL, 0);
544 /* extra_args keeps default value, it should be replaced
545 * only in case of successful parsing of the 'value' arg
548 *(uint64_t *)extra_args = integer;
553 vdpa_dynamic_major_num(void)
562 fp = fopen("/proc/devices", "r");
564 PMD_INIT_LOG(ERR, "Cannot open /proc/devices: %s",
566 return UNNAMED_MAJOR;
569 while (getline(&line, &size, fp) > 0) {
570 char *stripped = line + strspn(line, " ");
571 if ((sscanf(stripped, "%u %10s", &num, name) == 2) &&
572 (strncmp(name, "vhost-vdpa", 10) == 0)) {
578 return found ? num : UNNAMED_MAJOR;
581 static enum virtio_user_backend_type
582 virtio_user_backend_type(const char *path)
586 if (stat(path, &sb) == -1) {
588 return VIRTIO_USER_BACKEND_VHOST_USER;
590 PMD_INIT_LOG(ERR, "Stat fails: %s (%s)\n", path,
592 return VIRTIO_USER_BACKEND_UNKNOWN;
595 if (S_ISSOCK(sb.st_mode)) {
596 return VIRTIO_USER_BACKEND_VHOST_USER;
597 } else if (S_ISCHR(sb.st_mode)) {
598 if (major(sb.st_rdev) == MISC_MAJOR)
599 return VIRTIO_USER_BACKEND_VHOST_KERNEL;
600 if (major(sb.st_rdev) == vdpa_dynamic_major_num())
601 return VIRTIO_USER_BACKEND_VHOST_VDPA;
603 return VIRTIO_USER_BACKEND_UNKNOWN;
606 static struct rte_eth_dev *
607 virtio_user_eth_dev_alloc(struct rte_vdev_device *vdev)
609 struct rte_eth_dev *eth_dev;
610 struct rte_eth_dev_data *data;
611 struct virtio_hw *hw;
612 struct virtio_user_dev *dev;
614 eth_dev = rte_eth_vdev_allocate(vdev, sizeof(*dev));
616 PMD_INIT_LOG(ERR, "cannot alloc rte_eth_dev");
620 data = eth_dev->data;
621 dev = eth_dev->data->dev_private;
624 hw->port_id = data->port_id;
625 dev->port_id = data->port_id;
626 VIRTIO_OPS(hw) = &virtio_user_ops;
631 hw->use_inorder_rx = 0;
632 hw->use_inorder_tx = 0;
638 virtio_user_eth_dev_free(struct rte_eth_dev *eth_dev)
640 rte_eth_dev_release_port(eth_dev);
643 /* Dev initialization routine. Invoked once for each virtio vdev at
644 * EAL init time, see rte_bus_probe().
645 * Returns 0 on success.
648 virtio_user_pmd_probe(struct rte_vdev_device *vdev)
650 struct rte_kvargs *kvlist = NULL;
651 struct rte_eth_dev *eth_dev;
652 struct virtio_hw *hw;
653 struct virtio_user_dev *dev;
654 enum virtio_user_backend_type backend_type = VIRTIO_USER_BACKEND_UNKNOWN;
655 uint64_t queues = VIRTIO_USER_DEF_Q_NUM;
656 uint64_t cq = VIRTIO_USER_DEF_CQ_EN;
657 uint64_t queue_size = VIRTIO_USER_DEF_Q_SZ;
658 uint64_t server_mode = VIRTIO_USER_DEF_SERVER_MODE;
659 uint64_t mrg_rxbuf = 1;
660 uint64_t in_order = 1;
661 uint64_t packed_vq = 0;
662 uint64_t vectorized = 0;
665 char *mac_addr = NULL;
668 RTE_BUILD_BUG_ON(offsetof(struct virtio_user_dev, hw) != 0);
670 if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
671 const char *name = rte_vdev_device_name(vdev);
672 eth_dev = rte_eth_dev_attach_secondary(name);
674 PMD_INIT_LOG(ERR, "Failed to probe %s", name);
678 dev = eth_dev->data->dev_private;
680 VIRTIO_OPS(hw) = &virtio_user_ops;
682 if (eth_virtio_dev_init(eth_dev) < 0) {
683 PMD_INIT_LOG(ERR, "eth_virtio_dev_init fails");
684 rte_eth_dev_release_port(eth_dev);
688 eth_dev->dev_ops = &virtio_user_secondary_eth_dev_ops;
689 eth_dev->device = &vdev->device;
690 rte_eth_dev_probing_finish(eth_dev);
694 kvlist = rte_kvargs_parse(rte_vdev_device_args(vdev), valid_args);
696 PMD_INIT_LOG(ERR, "error when parsing param");
700 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_PATH) == 1) {
701 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_PATH,
702 &get_string_arg, &path) < 0) {
703 PMD_INIT_LOG(ERR, "error to parse %s",
704 VIRTIO_USER_ARG_PATH);
708 PMD_INIT_LOG(ERR, "arg %s is mandatory for virtio_user",
709 VIRTIO_USER_ARG_PATH);
713 backend_type = virtio_user_backend_type(path);
714 if (backend_type == VIRTIO_USER_BACKEND_UNKNOWN) {
716 "unable to determine backend type for path %s",
720 PMD_INIT_LOG(INFO, "Backend type detected: %s",
721 virtio_user_backend_strings[backend_type]);
723 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_INTERFACE_NAME) == 1) {
724 if (backend_type != VIRTIO_USER_BACKEND_VHOST_KERNEL) {
726 "arg %s applies only to vhost-kernel backend",
727 VIRTIO_USER_ARG_INTERFACE_NAME);
731 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_INTERFACE_NAME,
732 &get_string_arg, &ifname) < 0) {
733 PMD_INIT_LOG(ERR, "error to parse %s",
734 VIRTIO_USER_ARG_INTERFACE_NAME);
739 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_MAC) == 1) {
740 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_MAC,
741 &get_string_arg, &mac_addr) < 0) {
742 PMD_INIT_LOG(ERR, "error to parse %s",
743 VIRTIO_USER_ARG_MAC);
748 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_QUEUE_SIZE) == 1) {
749 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_QUEUE_SIZE,
750 &get_integer_arg, &queue_size) < 0) {
751 PMD_INIT_LOG(ERR, "error to parse %s",
752 VIRTIO_USER_ARG_QUEUE_SIZE);
757 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_QUEUES_NUM) == 1) {
758 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_QUEUES_NUM,
759 &get_integer_arg, &queues) < 0) {
760 PMD_INIT_LOG(ERR, "error to parse %s",
761 VIRTIO_USER_ARG_QUEUES_NUM);
766 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_SERVER_MODE) == 1) {
767 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_SERVER_MODE,
768 &get_integer_arg, &server_mode) < 0) {
769 PMD_INIT_LOG(ERR, "error to parse %s",
770 VIRTIO_USER_ARG_SERVER_MODE);
775 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_CQ_NUM) == 1) {
776 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_CQ_NUM,
777 &get_integer_arg, &cq) < 0) {
778 PMD_INIT_LOG(ERR, "error to parse %s",
779 VIRTIO_USER_ARG_CQ_NUM);
782 } else if (queues > 1) {
786 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_PACKED_VQ) == 1) {
787 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_PACKED_VQ,
788 &get_integer_arg, &packed_vq) < 0) {
789 PMD_INIT_LOG(ERR, "error to parse %s",
790 VIRTIO_USER_ARG_PACKED_VQ);
795 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_VECTORIZED) == 1) {
796 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_VECTORIZED,
797 &get_integer_arg, &vectorized) < 0) {
798 PMD_INIT_LOG(ERR, "error to parse %s",
799 VIRTIO_USER_ARG_VECTORIZED);
804 if (queues > 1 && cq == 0) {
805 PMD_INIT_LOG(ERR, "multi-q requires ctrl-q");
809 if (queues > VIRTIO_MAX_VIRTQUEUE_PAIRS) {
810 PMD_INIT_LOG(ERR, "arg %s %" PRIu64 " exceeds the limit %u",
811 VIRTIO_USER_ARG_QUEUES_NUM, queues,
812 VIRTIO_MAX_VIRTQUEUE_PAIRS);
816 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_MRG_RXBUF) == 1) {
817 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_MRG_RXBUF,
818 &get_integer_arg, &mrg_rxbuf) < 0) {
819 PMD_INIT_LOG(ERR, "error to parse %s",
820 VIRTIO_USER_ARG_MRG_RXBUF);
825 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_IN_ORDER) == 1) {
826 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_IN_ORDER,
827 &get_integer_arg, &in_order) < 0) {
828 PMD_INIT_LOG(ERR, "error to parse %s",
829 VIRTIO_USER_ARG_IN_ORDER);
834 eth_dev = virtio_user_eth_dev_alloc(vdev);
836 PMD_INIT_LOG(ERR, "virtio_user fails to alloc device");
840 dev = eth_dev->data->dev_private;
842 if (virtio_user_dev_init(dev, path, queues, cq,
843 queue_size, mac_addr, &ifname, server_mode,
844 mrg_rxbuf, in_order, packed_vq, backend_type) < 0) {
845 PMD_INIT_LOG(ERR, "virtio_user_dev_init fails");
846 virtio_user_eth_dev_free(eth_dev);
850 /* previously called by pci probing for physical dev */
851 if (eth_virtio_dev_init(eth_dev) < 0) {
852 PMD_INIT_LOG(ERR, "eth_virtio_dev_init fails");
853 virtio_user_eth_dev_free(eth_dev);
859 #if defined(CC_AVX512_SUPPORT) || defined(RTE_ARCH_ARM)
864 "building environment do not support packed ring vectorized");
871 rte_eth_dev_probing_finish(eth_dev);
876 rte_kvargs_free(kvlist);
887 virtio_user_pmd_remove(struct rte_vdev_device *vdev)
890 struct rte_eth_dev *eth_dev;
895 name = rte_vdev_device_name(vdev);
896 PMD_DRV_LOG(INFO, "Un-Initializing %s", name);
897 eth_dev = rte_eth_dev_allocated(name);
898 /* Port has already been released by close. */
902 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
903 return rte_eth_dev_release_port(eth_dev);
905 /* make sure the device is stopped, queues freed */
906 return rte_eth_dev_close(eth_dev->data->port_id);
909 static int virtio_user_pmd_dma_map(struct rte_vdev_device *vdev, void *addr,
910 uint64_t iova, size_t len)
913 struct rte_eth_dev *eth_dev;
914 struct virtio_user_dev *dev;
919 name = rte_vdev_device_name(vdev);
920 eth_dev = rte_eth_dev_allocated(name);
921 /* Port has already been released by close. */
925 dev = eth_dev->data->dev_private;
927 if (dev->ops->dma_map)
928 return dev->ops->dma_map(dev, addr, iova, len);
933 static int virtio_user_pmd_dma_unmap(struct rte_vdev_device *vdev, void *addr,
934 uint64_t iova, size_t len)
937 struct rte_eth_dev *eth_dev;
938 struct virtio_user_dev *dev;
943 name = rte_vdev_device_name(vdev);
944 eth_dev = rte_eth_dev_allocated(name);
945 /* Port has already been released by close. */
949 dev = eth_dev->data->dev_private;
951 if (dev->ops->dma_unmap)
952 return dev->ops->dma_unmap(dev, addr, iova, len);
957 static struct rte_vdev_driver virtio_user_driver = {
958 .probe = virtio_user_pmd_probe,
959 .remove = virtio_user_pmd_remove,
960 .dma_map = virtio_user_pmd_dma_map,
961 .dma_unmap = virtio_user_pmd_dma_unmap,
962 .drv_flags = RTE_VDEV_DRV_NEED_IOVA_AS_VA,
965 RTE_PMD_REGISTER_VDEV(net_virtio_user, virtio_user_driver);
966 RTE_PMD_REGISTER_ALIAS(net_virtio_user, virtio_user);
967 RTE_PMD_REGISTER_PARAM_STRING(net_virtio_user,