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) {
177 /* Until the featuers are negotiated again, don't assume
178 * the backend supports VHOST_USER_PROTOCOL_F_STATUS
180 dev->protocol_features &=
181 ~(1ULL << VHOST_USER_PROTOCOL_F_STATUS);
183 eth_dev->intr_handle->fd = dev->listenfd;
184 rte_intr_callback_register(eth_dev->intr_handle,
185 virtio_interrupt_handler, eth_dev);
186 if (rte_intr_enable(eth_dev->intr_handle) < 0) {
187 PMD_DRV_LOG(ERR, "interrupt enable failed");
194 virtio_user_read_dev_config(struct virtio_hw *hw, size_t offset,
195 void *dst, int length)
198 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
200 if (offset == offsetof(struct virtio_net_config, mac) &&
201 length == RTE_ETHER_ADDR_LEN) {
202 for (i = 0; i < RTE_ETHER_ADDR_LEN; ++i)
203 ((uint8_t *)dst)[i] = dev->mac_addr[i];
207 if (offset == offsetof(struct virtio_net_config, status)) {
210 if (dev->vhostfd >= 0) {
214 flags = fcntl(dev->vhostfd, F_GETFL);
215 if (fcntl(dev->vhostfd, F_SETFL,
216 flags | O_NONBLOCK) == -1) {
217 PMD_DRV_LOG(ERR, "error setting O_NONBLOCK flag");
220 r = recv(dev->vhostfd, buf, 128, MSG_PEEK);
221 if (r == 0 || (r < 0 && errno != EAGAIN)) {
222 dev->net_status &= (~VIRTIO_NET_S_LINK_UP);
223 PMD_DRV_LOG(ERR, "virtio-user port %u is down",
226 /* This function could be called in the process
227 * of interrupt handling, callback cannot be
228 * unregistered here, set an alarm to do it.
231 virtio_user_delayed_handler,
234 dev->net_status |= VIRTIO_NET_S_LINK_UP;
236 if (fcntl(dev->vhostfd, F_SETFL,
237 flags & ~O_NONBLOCK) == -1) {
238 PMD_DRV_LOG(ERR, "error clearing O_NONBLOCK flag");
241 } else if (dev->is_server) {
242 dev->net_status &= (~VIRTIO_NET_S_LINK_UP);
243 if (virtio_user_server_reconnect(dev) >= 0)
244 dev->net_status |= VIRTIO_NET_S_LINK_UP;
247 *(uint16_t *)dst = dev->net_status;
250 if (offset == offsetof(struct virtio_net_config, max_virtqueue_pairs))
251 *(uint16_t *)dst = dev->max_queue_pairs;
255 virtio_user_write_dev_config(struct virtio_hw *hw, size_t offset,
256 const void *src, int length)
259 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
261 if ((offset == offsetof(struct virtio_net_config, mac)) &&
262 (length == RTE_ETHER_ADDR_LEN))
263 for (i = 0; i < RTE_ETHER_ADDR_LEN; ++i)
264 dev->mac_addr[i] = ((const uint8_t *)src)[i];
266 PMD_DRV_LOG(ERR, "not supported offset=%zu, len=%d",
271 virtio_user_reset(struct virtio_hw *hw)
273 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
275 if (dev->status & VIRTIO_CONFIG_STATUS_DRIVER_OK)
276 virtio_user_stop_device(dev);
280 virtio_user_set_status(struct virtio_hw *hw, uint8_t status)
282 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
283 uint8_t old_status = dev->status;
285 if (status & VIRTIO_CONFIG_STATUS_FEATURES_OK &&
286 ~old_status & VIRTIO_CONFIG_STATUS_FEATURES_OK)
287 virtio_user_dev_set_features(dev);
288 if (status & VIRTIO_CONFIG_STATUS_DRIVER_OK)
289 virtio_user_start_device(dev);
290 else if (status == VIRTIO_CONFIG_STATUS_RESET)
291 virtio_user_reset(hw);
293 virtio_user_dev_set_status(dev, status);
297 virtio_user_get_status(struct virtio_hw *hw)
299 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
301 virtio_user_dev_update_status(dev);
307 virtio_user_get_features(struct virtio_hw *hw)
309 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
311 /* unmask feature bits defined in vhost user protocol */
312 return dev->device_features & VIRTIO_PMD_SUPPORTED_GUEST_FEATURES;
316 virtio_user_set_features(struct virtio_hw *hw, uint64_t features)
318 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
320 dev->features = features & dev->device_features;
324 virtio_user_features_ok(struct virtio_hw *hw __rte_unused)
330 virtio_user_get_isr(struct virtio_hw *hw __rte_unused)
332 /* rxq interrupts and config interrupt are separated in virtio-user,
333 * here we only report config change.
335 return VIRTIO_ISR_CONFIG;
339 virtio_user_set_config_irq(struct virtio_hw *hw __rte_unused,
340 uint16_t vec __rte_unused)
346 virtio_user_set_queue_irq(struct virtio_hw *hw __rte_unused,
347 struct virtqueue *vq __rte_unused,
350 /* pretend we have done that */
354 /* This function is to get the queue size, aka, number of descs, of a specified
355 * queue. Different with the VHOST_USER_GET_QUEUE_NUM, which is used to get the
356 * max supported queues.
359 virtio_user_get_queue_num(struct virtio_hw *hw, uint16_t queue_id __rte_unused)
361 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
363 /* Currently, each queue has same queue size */
364 return dev->queue_size;
368 virtio_user_setup_queue_packed(struct virtqueue *vq,
369 struct virtio_user_dev *dev)
371 uint16_t queue_idx = vq->vq_queue_index;
372 struct vring_packed *vring;
378 vring = &dev->packed_vrings[queue_idx];
379 desc_addr = (uintptr_t)vq->vq_ring_virt_mem;
380 avail_addr = desc_addr + vq->vq_nentries *
381 sizeof(struct vring_packed_desc);
382 used_addr = RTE_ALIGN_CEIL(avail_addr +
383 sizeof(struct vring_packed_desc_event),
385 vring->num = vq->vq_nentries;
386 vring->desc = (void *)(uintptr_t)desc_addr;
387 vring->driver = (void *)(uintptr_t)avail_addr;
388 vring->device = (void *)(uintptr_t)used_addr;
389 dev->packed_queues[queue_idx].avail_wrap_counter = true;
390 dev->packed_queues[queue_idx].used_wrap_counter = true;
392 for (i = 0; i < vring->num; i++)
393 vring->desc[i].flags = 0;
397 virtio_user_setup_queue_split(struct virtqueue *vq, struct virtio_user_dev *dev)
399 uint16_t queue_idx = vq->vq_queue_index;
400 uint64_t desc_addr, avail_addr, used_addr;
402 desc_addr = (uintptr_t)vq->vq_ring_virt_mem;
403 avail_addr = desc_addr + vq->vq_nentries * sizeof(struct vring_desc);
404 used_addr = RTE_ALIGN_CEIL(avail_addr + offsetof(struct vring_avail,
405 ring[vq->vq_nentries]),
408 dev->vrings[queue_idx].num = vq->vq_nentries;
409 dev->vrings[queue_idx].desc = (void *)(uintptr_t)desc_addr;
410 dev->vrings[queue_idx].avail = (void *)(uintptr_t)avail_addr;
411 dev->vrings[queue_idx].used = (void *)(uintptr_t)used_addr;
415 virtio_user_setup_queue(struct virtio_hw *hw, struct virtqueue *vq)
417 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
419 if (virtio_with_packed_queue(hw))
420 virtio_user_setup_queue_packed(vq, dev);
422 virtio_user_setup_queue_split(vq, dev);
428 virtio_user_del_queue(struct virtio_hw *hw, struct virtqueue *vq)
430 /* For legacy devices, write 0 to VIRTIO_PCI_QUEUE_PFN port, QEMU
431 * correspondingly stops the ioeventfds, and reset the status of
433 * For modern devices, set queue desc, avail, used in PCI bar to 0,
434 * not see any more behavior in QEMU.
436 * Here we just care about what information to deliver to vhost-user
437 * or vhost-kernel. So we just close ioeventfd for now.
439 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
441 close(dev->callfds[vq->vq_queue_index]);
442 close(dev->kickfds[vq->vq_queue_index]);
446 virtio_user_notify_queue(struct virtio_hw *hw, struct virtqueue *vq)
449 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
451 if (hw->cvq && (hw->cvq->vq == vq)) {
452 if (virtio_with_packed_queue(vq->hw))
453 virtio_user_handle_cq_packed(dev, vq->vq_queue_index);
455 virtio_user_handle_cq(dev, vq->vq_queue_index);
459 if (write(dev->kickfds[vq->vq_queue_index], &buf, sizeof(buf)) < 0)
460 PMD_DRV_LOG(ERR, "failed to kick backend: %s",
465 virtio_user_dev_close(struct virtio_hw *hw)
467 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
469 virtio_user_dev_uninit(dev);
474 const struct virtio_ops virtio_user_ops = {
475 .read_dev_cfg = virtio_user_read_dev_config,
476 .write_dev_cfg = virtio_user_write_dev_config,
477 .get_status = virtio_user_get_status,
478 .set_status = virtio_user_set_status,
479 .get_features = virtio_user_get_features,
480 .set_features = virtio_user_set_features,
481 .features_ok = virtio_user_features_ok,
482 .get_isr = virtio_user_get_isr,
483 .set_config_irq = virtio_user_set_config_irq,
484 .set_queue_irq = virtio_user_set_queue_irq,
485 .get_queue_num = virtio_user_get_queue_num,
486 .setup_queue = virtio_user_setup_queue,
487 .del_queue = virtio_user_del_queue,
488 .notify_queue = virtio_user_notify_queue,
489 .dev_close = virtio_user_dev_close,
492 static const char *valid_args[] = {
493 #define VIRTIO_USER_ARG_QUEUES_NUM "queues"
494 VIRTIO_USER_ARG_QUEUES_NUM,
495 #define VIRTIO_USER_ARG_CQ_NUM "cq"
496 VIRTIO_USER_ARG_CQ_NUM,
497 #define VIRTIO_USER_ARG_MAC "mac"
499 #define VIRTIO_USER_ARG_PATH "path"
500 VIRTIO_USER_ARG_PATH,
501 #define VIRTIO_USER_ARG_QUEUE_SIZE "queue_size"
502 VIRTIO_USER_ARG_QUEUE_SIZE,
503 #define VIRTIO_USER_ARG_INTERFACE_NAME "iface"
504 VIRTIO_USER_ARG_INTERFACE_NAME,
505 #define VIRTIO_USER_ARG_SERVER_MODE "server"
506 VIRTIO_USER_ARG_SERVER_MODE,
507 #define VIRTIO_USER_ARG_MRG_RXBUF "mrg_rxbuf"
508 VIRTIO_USER_ARG_MRG_RXBUF,
509 #define VIRTIO_USER_ARG_IN_ORDER "in_order"
510 VIRTIO_USER_ARG_IN_ORDER,
511 #define VIRTIO_USER_ARG_PACKED_VQ "packed_vq"
512 VIRTIO_USER_ARG_PACKED_VQ,
513 #define VIRTIO_USER_ARG_SPEED "speed"
514 VIRTIO_USER_ARG_SPEED,
515 #define VIRTIO_USER_ARG_VECTORIZED "vectorized"
516 VIRTIO_USER_ARG_VECTORIZED,
520 #define VIRTIO_USER_DEF_CQ_EN 0
521 #define VIRTIO_USER_DEF_Q_NUM 1
522 #define VIRTIO_USER_DEF_Q_SZ 256
523 #define VIRTIO_USER_DEF_SERVER_MODE 0
526 get_string_arg(const char *key __rte_unused,
527 const char *value, void *extra_args)
529 if (!value || !extra_args)
532 *(char **)extra_args = strdup(value);
534 if (!*(char **)extra_args)
541 get_integer_arg(const char *key __rte_unused,
542 const char *value, void *extra_args)
544 uint64_t integer = 0;
545 if (!value || !extra_args)
548 integer = strtoull(value, NULL, 0);
549 /* extra_args keeps default value, it should be replaced
550 * only in case of successful parsing of the 'value' arg
553 *(uint64_t *)extra_args = integer;
558 vdpa_dynamic_major_num(void)
567 fp = fopen("/proc/devices", "r");
569 PMD_INIT_LOG(ERR, "Cannot open /proc/devices: %s",
571 return UNNAMED_MAJOR;
574 while (getline(&line, &size, fp) > 0) {
575 char *stripped = line + strspn(line, " ");
576 if ((sscanf(stripped, "%u %10s", &num, name) == 2) &&
577 (strncmp(name, "vhost-vdpa", 10) == 0)) {
583 return found ? num : UNNAMED_MAJOR;
586 static enum virtio_user_backend_type
587 virtio_user_backend_type(const char *path)
591 if (stat(path, &sb) == -1) {
593 return VIRTIO_USER_BACKEND_VHOST_USER;
595 PMD_INIT_LOG(ERR, "Stat fails: %s (%s)\n", path,
597 return VIRTIO_USER_BACKEND_UNKNOWN;
600 if (S_ISSOCK(sb.st_mode)) {
601 return VIRTIO_USER_BACKEND_VHOST_USER;
602 } else if (S_ISCHR(sb.st_mode)) {
603 if (major(sb.st_rdev) == MISC_MAJOR)
604 return VIRTIO_USER_BACKEND_VHOST_KERNEL;
605 if (major(sb.st_rdev) == vdpa_dynamic_major_num())
606 return VIRTIO_USER_BACKEND_VHOST_VDPA;
608 return VIRTIO_USER_BACKEND_UNKNOWN;
611 static struct rte_eth_dev *
612 virtio_user_eth_dev_alloc(struct rte_vdev_device *vdev)
614 struct rte_eth_dev *eth_dev;
615 struct rte_eth_dev_data *data;
616 struct virtio_hw *hw;
617 struct virtio_user_dev *dev;
619 eth_dev = rte_eth_vdev_allocate(vdev, sizeof(*dev));
621 PMD_INIT_LOG(ERR, "cannot alloc rte_eth_dev");
625 data = eth_dev->data;
626 dev = eth_dev->data->dev_private;
629 hw->port_id = data->port_id;
630 dev->port_id = data->port_id;
631 VIRTIO_OPS(hw) = &virtio_user_ops;
636 hw->use_inorder_rx = 0;
637 hw->use_inorder_tx = 0;
643 virtio_user_eth_dev_free(struct rte_eth_dev *eth_dev)
645 rte_eth_dev_release_port(eth_dev);
648 /* Dev initialization routine. Invoked once for each virtio vdev at
649 * EAL init time, see rte_bus_probe().
650 * Returns 0 on success.
653 virtio_user_pmd_probe(struct rte_vdev_device *vdev)
655 struct rte_kvargs *kvlist = NULL;
656 struct rte_eth_dev *eth_dev;
657 struct virtio_hw *hw;
658 struct virtio_user_dev *dev;
659 enum virtio_user_backend_type backend_type = VIRTIO_USER_BACKEND_UNKNOWN;
660 uint64_t queues = VIRTIO_USER_DEF_Q_NUM;
661 uint64_t cq = VIRTIO_USER_DEF_CQ_EN;
662 uint64_t queue_size = VIRTIO_USER_DEF_Q_SZ;
663 uint64_t server_mode = VIRTIO_USER_DEF_SERVER_MODE;
664 uint64_t mrg_rxbuf = 1;
665 uint64_t in_order = 1;
666 uint64_t packed_vq = 0;
667 uint64_t vectorized = 0;
670 char *mac_addr = NULL;
673 RTE_BUILD_BUG_ON(offsetof(struct virtio_user_dev, hw) != 0);
675 if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
676 const char *name = rte_vdev_device_name(vdev);
677 eth_dev = rte_eth_dev_attach_secondary(name);
679 PMD_INIT_LOG(ERR, "Failed to probe %s", name);
683 dev = eth_dev->data->dev_private;
685 VIRTIO_OPS(hw) = &virtio_user_ops;
687 if (eth_virtio_dev_init(eth_dev) < 0) {
688 PMD_INIT_LOG(ERR, "eth_virtio_dev_init fails");
689 rte_eth_dev_release_port(eth_dev);
693 eth_dev->dev_ops = &virtio_user_secondary_eth_dev_ops;
694 eth_dev->device = &vdev->device;
695 rte_eth_dev_probing_finish(eth_dev);
699 kvlist = rte_kvargs_parse(rte_vdev_device_args(vdev), valid_args);
701 PMD_INIT_LOG(ERR, "error when parsing param");
705 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_PATH) == 1) {
706 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_PATH,
707 &get_string_arg, &path) < 0) {
708 PMD_INIT_LOG(ERR, "error to parse %s",
709 VIRTIO_USER_ARG_PATH);
713 PMD_INIT_LOG(ERR, "arg %s is mandatory for virtio_user",
714 VIRTIO_USER_ARG_PATH);
718 backend_type = virtio_user_backend_type(path);
719 if (backend_type == VIRTIO_USER_BACKEND_UNKNOWN) {
721 "unable to determine backend type for path %s",
725 PMD_INIT_LOG(INFO, "Backend type detected: %s",
726 virtio_user_backend_strings[backend_type]);
728 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_INTERFACE_NAME) == 1) {
729 if (backend_type != VIRTIO_USER_BACKEND_VHOST_KERNEL) {
731 "arg %s applies only to vhost-kernel backend",
732 VIRTIO_USER_ARG_INTERFACE_NAME);
736 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_INTERFACE_NAME,
737 &get_string_arg, &ifname) < 0) {
738 PMD_INIT_LOG(ERR, "error to parse %s",
739 VIRTIO_USER_ARG_INTERFACE_NAME);
744 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_MAC) == 1) {
745 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_MAC,
746 &get_string_arg, &mac_addr) < 0) {
747 PMD_INIT_LOG(ERR, "error to parse %s",
748 VIRTIO_USER_ARG_MAC);
753 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_QUEUE_SIZE) == 1) {
754 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_QUEUE_SIZE,
755 &get_integer_arg, &queue_size) < 0) {
756 PMD_INIT_LOG(ERR, "error to parse %s",
757 VIRTIO_USER_ARG_QUEUE_SIZE);
762 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_QUEUES_NUM) == 1) {
763 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_QUEUES_NUM,
764 &get_integer_arg, &queues) < 0) {
765 PMD_INIT_LOG(ERR, "error to parse %s",
766 VIRTIO_USER_ARG_QUEUES_NUM);
771 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_SERVER_MODE) == 1) {
772 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_SERVER_MODE,
773 &get_integer_arg, &server_mode) < 0) {
774 PMD_INIT_LOG(ERR, "error to parse %s",
775 VIRTIO_USER_ARG_SERVER_MODE);
780 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_CQ_NUM) == 1) {
781 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_CQ_NUM,
782 &get_integer_arg, &cq) < 0) {
783 PMD_INIT_LOG(ERR, "error to parse %s",
784 VIRTIO_USER_ARG_CQ_NUM);
787 } else if (queues > 1) {
791 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_PACKED_VQ) == 1) {
792 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_PACKED_VQ,
793 &get_integer_arg, &packed_vq) < 0) {
794 PMD_INIT_LOG(ERR, "error to parse %s",
795 VIRTIO_USER_ARG_PACKED_VQ);
800 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_VECTORIZED) == 1) {
801 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_VECTORIZED,
802 &get_integer_arg, &vectorized) < 0) {
803 PMD_INIT_LOG(ERR, "error to parse %s",
804 VIRTIO_USER_ARG_VECTORIZED);
809 if (queues > 1 && cq == 0) {
810 PMD_INIT_LOG(ERR, "multi-q requires ctrl-q");
814 if (queues > VIRTIO_MAX_VIRTQUEUE_PAIRS) {
815 PMD_INIT_LOG(ERR, "arg %s %" PRIu64 " exceeds the limit %u",
816 VIRTIO_USER_ARG_QUEUES_NUM, queues,
817 VIRTIO_MAX_VIRTQUEUE_PAIRS);
821 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_MRG_RXBUF) == 1) {
822 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_MRG_RXBUF,
823 &get_integer_arg, &mrg_rxbuf) < 0) {
824 PMD_INIT_LOG(ERR, "error to parse %s",
825 VIRTIO_USER_ARG_MRG_RXBUF);
830 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_IN_ORDER) == 1) {
831 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_IN_ORDER,
832 &get_integer_arg, &in_order) < 0) {
833 PMD_INIT_LOG(ERR, "error to parse %s",
834 VIRTIO_USER_ARG_IN_ORDER);
839 eth_dev = virtio_user_eth_dev_alloc(vdev);
841 PMD_INIT_LOG(ERR, "virtio_user fails to alloc device");
845 dev = eth_dev->data->dev_private;
847 if (virtio_user_dev_init(dev, path, queues, cq,
848 queue_size, mac_addr, &ifname, server_mode,
849 mrg_rxbuf, in_order, packed_vq, backend_type) < 0) {
850 PMD_INIT_LOG(ERR, "virtio_user_dev_init fails");
851 virtio_user_eth_dev_free(eth_dev);
855 /* previously called by pci probing for physical dev */
856 if (eth_virtio_dev_init(eth_dev) < 0) {
857 PMD_INIT_LOG(ERR, "eth_virtio_dev_init fails");
858 virtio_user_eth_dev_free(eth_dev);
864 #if defined(CC_AVX512_SUPPORT) || defined(RTE_ARCH_ARM)
869 "building environment do not support packed ring vectorized");
876 rte_eth_dev_probing_finish(eth_dev);
881 rte_kvargs_free(kvlist);
892 virtio_user_pmd_remove(struct rte_vdev_device *vdev)
895 struct rte_eth_dev *eth_dev;
900 name = rte_vdev_device_name(vdev);
901 PMD_DRV_LOG(INFO, "Un-Initializing %s", name);
902 eth_dev = rte_eth_dev_allocated(name);
903 /* Port has already been released by close. */
907 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
908 return rte_eth_dev_release_port(eth_dev);
910 /* make sure the device is stopped, queues freed */
911 return rte_eth_dev_close(eth_dev->data->port_id);
914 static int virtio_user_pmd_dma_map(struct rte_vdev_device *vdev, void *addr,
915 uint64_t iova, size_t len)
918 struct rte_eth_dev *eth_dev;
919 struct virtio_user_dev *dev;
924 name = rte_vdev_device_name(vdev);
925 eth_dev = rte_eth_dev_allocated(name);
926 /* Port has already been released by close. */
930 dev = eth_dev->data->dev_private;
932 if (dev->ops->dma_map)
933 return dev->ops->dma_map(dev, addr, iova, len);
938 static int virtio_user_pmd_dma_unmap(struct rte_vdev_device *vdev, void *addr,
939 uint64_t iova, size_t len)
942 struct rte_eth_dev *eth_dev;
943 struct virtio_user_dev *dev;
948 name = rte_vdev_device_name(vdev);
949 eth_dev = rte_eth_dev_allocated(name);
950 /* Port has already been released by close. */
954 dev = eth_dev->data->dev_private;
956 if (dev->ops->dma_unmap)
957 return dev->ops->dma_unmap(dev, addr, iova, len);
962 static struct rte_vdev_driver virtio_user_driver = {
963 .probe = virtio_user_pmd_probe,
964 .remove = virtio_user_pmd_remove,
965 .dma_map = virtio_user_pmd_dma_map,
966 .dma_unmap = virtio_user_pmd_dma_unmap,
967 .drv_flags = RTE_VDEV_DRV_NEED_IOVA_AS_VA,
970 RTE_PMD_REGISTER_VDEV(net_virtio_user, virtio_user_driver);
971 RTE_PMD_REGISTER_ALIAS(net_virtio_user, virtio_user);
972 RTE_PMD_REGISTER_PARAM_STRING(net_virtio_user,