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->send_request(dev,
97 VHOST_USER_GET_PROTOCOL_FEATURES,
101 /* Offer VHOST_USER_PROTOCOL_F_STATUS */
102 dev->protocol_features |=
103 (1ULL << VHOST_USER_PROTOCOL_F_STATUS);
104 dev->protocol_features &= protocol_features;
106 if (dev->ops->send_request(dev,
107 VHOST_USER_SET_PROTOCOL_FEATURES,
108 &dev->protocol_features))
111 if (!(dev->protocol_features &
112 (1ULL << VHOST_USER_PROTOCOL_F_MQ)))
113 dev->unsupported_features |= (1ull << VIRTIO_NET_F_MQ);
116 dev->device_features |= dev->frontend_features;
118 /* umask vhost-user unsupported features */
119 dev->device_features &= ~(dev->unsupported_features);
121 dev->features &= dev->device_features;
123 /* For packed ring, resetting queues is required in reconnection. */
124 if (virtio_with_packed_queue(hw) &&
125 (old_status & VIRTIO_CONFIG_STATUS_DRIVER_OK)) {
126 PMD_INIT_LOG(NOTICE, "Packets on the fly will be dropped"
127 " when packed ring reconnecting.");
128 virtio_user_reset_queues_packed(eth_dev);
131 virtio_set_status(hw, VIRTIO_CONFIG_STATUS_FEATURES_OK);
133 /* Start the device */
134 virtio_set_status(hw, VIRTIO_CONFIG_STATUS_DRIVER_OK);
138 if (dev->queue_pairs > 1) {
139 ret = virtio_user_handle_mq(dev, dev->queue_pairs);
141 PMD_INIT_LOG(ERR, "Fails to enable multi-queue pairs!");
145 if (eth_dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC) {
146 if (rte_intr_disable(eth_dev->intr_handle) < 0) {
147 PMD_DRV_LOG(ERR, "interrupt disable failed");
150 rte_intr_callback_unregister(eth_dev->intr_handle,
151 virtio_interrupt_handler,
153 eth_dev->intr_handle->fd = connectfd;
154 rte_intr_callback_register(eth_dev->intr_handle,
155 virtio_interrupt_handler, eth_dev);
157 if (rte_intr_enable(eth_dev->intr_handle) < 0) {
158 PMD_DRV_LOG(ERR, "interrupt enable failed");
162 PMD_INIT_LOG(NOTICE, "server mode virtio-user reconnection succeeds!");
167 virtio_user_delayed_handler(void *param)
169 struct virtio_hw *hw = (struct virtio_hw *)param;
170 struct rte_eth_dev *eth_dev = &rte_eth_devices[hw->port_id];
171 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
173 if (rte_intr_disable(eth_dev->intr_handle) < 0) {
174 PMD_DRV_LOG(ERR, "interrupt disable failed");
177 rte_intr_callback_unregister(eth_dev->intr_handle,
178 virtio_interrupt_handler, eth_dev);
179 if (dev->is_server) {
180 if (dev->vhostfd >= 0) {
183 /* Until the featuers are negotiated again, don't assume
184 * the backend supports VHOST_USER_PROTOCOL_F_STATUS
186 dev->protocol_features &=
187 ~(1ULL << VHOST_USER_PROTOCOL_F_STATUS);
189 eth_dev->intr_handle->fd = dev->listenfd;
190 rte_intr_callback_register(eth_dev->intr_handle,
191 virtio_interrupt_handler, eth_dev);
192 if (rte_intr_enable(eth_dev->intr_handle) < 0) {
193 PMD_DRV_LOG(ERR, "interrupt enable failed");
200 virtio_user_read_dev_config(struct virtio_hw *hw, size_t offset,
201 void *dst, int length)
204 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
206 if (offset == offsetof(struct virtio_net_config, mac) &&
207 length == RTE_ETHER_ADDR_LEN) {
208 for (i = 0; i < RTE_ETHER_ADDR_LEN; ++i)
209 ((uint8_t *)dst)[i] = dev->mac_addr[i];
213 if (offset == offsetof(struct virtio_net_config, status)) {
216 if (dev->vhostfd >= 0) {
220 flags = fcntl(dev->vhostfd, F_GETFL);
221 if (fcntl(dev->vhostfd, F_SETFL,
222 flags | O_NONBLOCK) == -1) {
223 PMD_DRV_LOG(ERR, "error setting O_NONBLOCK flag");
226 r = recv(dev->vhostfd, buf, 128, MSG_PEEK);
227 if (r == 0 || (r < 0 && errno != EAGAIN)) {
228 dev->net_status &= (~VIRTIO_NET_S_LINK_UP);
229 PMD_DRV_LOG(ERR, "virtio-user port %u is down",
232 /* This function could be called in the process
233 * of interrupt handling, callback cannot be
234 * unregistered here, set an alarm to do it.
237 virtio_user_delayed_handler,
240 dev->net_status |= VIRTIO_NET_S_LINK_UP;
242 if (fcntl(dev->vhostfd, F_SETFL,
243 flags & ~O_NONBLOCK) == -1) {
244 PMD_DRV_LOG(ERR, "error clearing O_NONBLOCK flag");
247 } else if (dev->is_server) {
248 dev->net_status &= (~VIRTIO_NET_S_LINK_UP);
249 if (virtio_user_server_reconnect(dev) >= 0)
250 dev->net_status |= VIRTIO_NET_S_LINK_UP;
253 *(uint16_t *)dst = dev->net_status;
256 if (offset == offsetof(struct virtio_net_config, max_virtqueue_pairs))
257 *(uint16_t *)dst = dev->max_queue_pairs;
261 virtio_user_write_dev_config(struct virtio_hw *hw, size_t offset,
262 const void *src, int length)
265 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
267 if ((offset == offsetof(struct virtio_net_config, mac)) &&
268 (length == RTE_ETHER_ADDR_LEN))
269 for (i = 0; i < RTE_ETHER_ADDR_LEN; ++i)
270 dev->mac_addr[i] = ((const uint8_t *)src)[i];
272 PMD_DRV_LOG(ERR, "not supported offset=%zu, len=%d",
277 virtio_user_reset(struct virtio_hw *hw)
279 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
281 if (dev->status & VIRTIO_CONFIG_STATUS_DRIVER_OK)
282 virtio_user_stop_device(dev);
286 virtio_user_set_status(struct virtio_hw *hw, uint8_t status)
288 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
289 uint8_t old_status = dev->status;
291 if (status & VIRTIO_CONFIG_STATUS_FEATURES_OK &&
292 ~old_status & VIRTIO_CONFIG_STATUS_FEATURES_OK)
293 virtio_user_dev_set_features(dev);
294 if (status & VIRTIO_CONFIG_STATUS_DRIVER_OK)
295 virtio_user_start_device(dev);
296 else if (status == VIRTIO_CONFIG_STATUS_RESET)
297 virtio_user_reset(hw);
299 virtio_user_dev_set_status(dev, status);
303 virtio_user_get_status(struct virtio_hw *hw)
305 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
307 virtio_user_dev_update_status(dev);
313 virtio_user_get_features(struct virtio_hw *hw)
315 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
317 /* unmask feature bits defined in vhost user protocol */
318 return dev->device_features & VIRTIO_PMD_SUPPORTED_GUEST_FEATURES;
322 virtio_user_set_features(struct virtio_hw *hw, uint64_t features)
324 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
326 dev->features = features & dev->device_features;
330 virtio_user_features_ok(struct virtio_hw *hw __rte_unused)
336 virtio_user_get_isr(struct virtio_hw *hw __rte_unused)
338 /* rxq interrupts and config interrupt are separated in virtio-user,
339 * here we only report config change.
341 return VIRTIO_ISR_CONFIG;
345 virtio_user_set_config_irq(struct virtio_hw *hw __rte_unused,
346 uint16_t vec __rte_unused)
352 virtio_user_set_queue_irq(struct virtio_hw *hw __rte_unused,
353 struct virtqueue *vq __rte_unused,
356 /* pretend we have done that */
360 /* This function is to get the queue size, aka, number of descs, of a specified
361 * queue. Different with the VHOST_USER_GET_QUEUE_NUM, which is used to get the
362 * max supported queues.
365 virtio_user_get_queue_num(struct virtio_hw *hw, uint16_t queue_id __rte_unused)
367 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
369 /* Currently, each queue has same queue size */
370 return dev->queue_size;
374 virtio_user_setup_queue_packed(struct virtqueue *vq,
375 struct virtio_user_dev *dev)
377 uint16_t queue_idx = vq->vq_queue_index;
378 struct vring_packed *vring;
384 vring = &dev->packed_vrings[queue_idx];
385 desc_addr = (uintptr_t)vq->vq_ring_virt_mem;
386 avail_addr = desc_addr + vq->vq_nentries *
387 sizeof(struct vring_packed_desc);
388 used_addr = RTE_ALIGN_CEIL(avail_addr +
389 sizeof(struct vring_packed_desc_event),
391 vring->num = vq->vq_nentries;
392 vring->desc = (void *)(uintptr_t)desc_addr;
393 vring->driver = (void *)(uintptr_t)avail_addr;
394 vring->device = (void *)(uintptr_t)used_addr;
395 dev->packed_queues[queue_idx].avail_wrap_counter = true;
396 dev->packed_queues[queue_idx].used_wrap_counter = true;
398 for (i = 0; i < vring->num; i++)
399 vring->desc[i].flags = 0;
403 virtio_user_setup_queue_split(struct virtqueue *vq, struct virtio_user_dev *dev)
405 uint16_t queue_idx = vq->vq_queue_index;
406 uint64_t desc_addr, avail_addr, used_addr;
408 desc_addr = (uintptr_t)vq->vq_ring_virt_mem;
409 avail_addr = desc_addr + vq->vq_nentries * sizeof(struct vring_desc);
410 used_addr = RTE_ALIGN_CEIL(avail_addr + offsetof(struct vring_avail,
411 ring[vq->vq_nentries]),
414 dev->vrings[queue_idx].num = vq->vq_nentries;
415 dev->vrings[queue_idx].desc = (void *)(uintptr_t)desc_addr;
416 dev->vrings[queue_idx].avail = (void *)(uintptr_t)avail_addr;
417 dev->vrings[queue_idx].used = (void *)(uintptr_t)used_addr;
421 virtio_user_setup_queue(struct virtio_hw *hw, struct virtqueue *vq)
423 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
425 if (virtio_with_packed_queue(hw))
426 virtio_user_setup_queue_packed(vq, dev);
428 virtio_user_setup_queue_split(vq, dev);
434 virtio_user_del_queue(struct virtio_hw *hw, struct virtqueue *vq)
436 /* For legacy devices, write 0 to VIRTIO_PCI_QUEUE_PFN port, QEMU
437 * correspondingly stops the ioeventfds, and reset the status of
439 * For modern devices, set queue desc, avail, used in PCI bar to 0,
440 * not see any more behavior in QEMU.
442 * Here we just care about what information to deliver to vhost-user
443 * or vhost-kernel. So we just close ioeventfd for now.
445 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
447 close(dev->callfds[vq->vq_queue_index]);
448 close(dev->kickfds[vq->vq_queue_index]);
452 virtio_user_notify_queue(struct virtio_hw *hw, struct virtqueue *vq)
455 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
457 if (hw->cvq && (hw->cvq->vq == vq)) {
458 if (virtio_with_packed_queue(vq->hw))
459 virtio_user_handle_cq_packed(dev, vq->vq_queue_index);
461 virtio_user_handle_cq(dev, vq->vq_queue_index);
465 if (write(dev->kickfds[vq->vq_queue_index], &buf, sizeof(buf)) < 0)
466 PMD_DRV_LOG(ERR, "failed to kick backend: %s",
471 virtio_user_dev_close(struct virtio_hw *hw)
473 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
475 virtio_user_dev_uninit(dev);
480 const struct virtio_ops virtio_user_ops = {
481 .read_dev_cfg = virtio_user_read_dev_config,
482 .write_dev_cfg = virtio_user_write_dev_config,
483 .get_status = virtio_user_get_status,
484 .set_status = virtio_user_set_status,
485 .get_features = virtio_user_get_features,
486 .set_features = virtio_user_set_features,
487 .features_ok = virtio_user_features_ok,
488 .get_isr = virtio_user_get_isr,
489 .set_config_irq = virtio_user_set_config_irq,
490 .set_queue_irq = virtio_user_set_queue_irq,
491 .get_queue_num = virtio_user_get_queue_num,
492 .setup_queue = virtio_user_setup_queue,
493 .del_queue = virtio_user_del_queue,
494 .notify_queue = virtio_user_notify_queue,
495 .dev_close = virtio_user_dev_close,
498 static const char *valid_args[] = {
499 #define VIRTIO_USER_ARG_QUEUES_NUM "queues"
500 VIRTIO_USER_ARG_QUEUES_NUM,
501 #define VIRTIO_USER_ARG_CQ_NUM "cq"
502 VIRTIO_USER_ARG_CQ_NUM,
503 #define VIRTIO_USER_ARG_MAC "mac"
505 #define VIRTIO_USER_ARG_PATH "path"
506 VIRTIO_USER_ARG_PATH,
507 #define VIRTIO_USER_ARG_QUEUE_SIZE "queue_size"
508 VIRTIO_USER_ARG_QUEUE_SIZE,
509 #define VIRTIO_USER_ARG_INTERFACE_NAME "iface"
510 VIRTIO_USER_ARG_INTERFACE_NAME,
511 #define VIRTIO_USER_ARG_SERVER_MODE "server"
512 VIRTIO_USER_ARG_SERVER_MODE,
513 #define VIRTIO_USER_ARG_MRG_RXBUF "mrg_rxbuf"
514 VIRTIO_USER_ARG_MRG_RXBUF,
515 #define VIRTIO_USER_ARG_IN_ORDER "in_order"
516 VIRTIO_USER_ARG_IN_ORDER,
517 #define VIRTIO_USER_ARG_PACKED_VQ "packed_vq"
518 VIRTIO_USER_ARG_PACKED_VQ,
519 #define VIRTIO_USER_ARG_SPEED "speed"
520 VIRTIO_USER_ARG_SPEED,
521 #define VIRTIO_USER_ARG_VECTORIZED "vectorized"
522 VIRTIO_USER_ARG_VECTORIZED,
526 #define VIRTIO_USER_DEF_CQ_EN 0
527 #define VIRTIO_USER_DEF_Q_NUM 1
528 #define VIRTIO_USER_DEF_Q_SZ 256
529 #define VIRTIO_USER_DEF_SERVER_MODE 0
532 get_string_arg(const char *key __rte_unused,
533 const char *value, void *extra_args)
535 if (!value || !extra_args)
538 *(char **)extra_args = strdup(value);
540 if (!*(char **)extra_args)
547 get_integer_arg(const char *key __rte_unused,
548 const char *value, void *extra_args)
550 uint64_t integer = 0;
551 if (!value || !extra_args)
554 integer = strtoull(value, NULL, 0);
555 /* extra_args keeps default value, it should be replaced
556 * only in case of successful parsing of the 'value' arg
559 *(uint64_t *)extra_args = integer;
564 vdpa_dynamic_major_num(void)
573 fp = fopen("/proc/devices", "r");
575 PMD_INIT_LOG(ERR, "Cannot open /proc/devices: %s",
577 return UNNAMED_MAJOR;
580 while (getline(&line, &size, fp) > 0) {
581 char *stripped = line + strspn(line, " ");
582 if ((sscanf(stripped, "%u %10s", &num, name) == 2) &&
583 (strncmp(name, "vhost-vdpa", 10) == 0)) {
589 return found ? num : UNNAMED_MAJOR;
592 static enum virtio_user_backend_type
593 virtio_user_backend_type(const char *path)
597 if (stat(path, &sb) == -1) {
599 return VIRTIO_USER_BACKEND_VHOST_USER;
601 PMD_INIT_LOG(ERR, "Stat fails: %s (%s)\n", path,
603 return VIRTIO_USER_BACKEND_UNKNOWN;
606 if (S_ISSOCK(sb.st_mode)) {
607 return VIRTIO_USER_BACKEND_VHOST_USER;
608 } else if (S_ISCHR(sb.st_mode)) {
609 if (major(sb.st_rdev) == MISC_MAJOR)
610 return VIRTIO_USER_BACKEND_VHOST_KERNEL;
611 if (major(sb.st_rdev) == vdpa_dynamic_major_num())
612 return VIRTIO_USER_BACKEND_VHOST_VDPA;
614 return VIRTIO_USER_BACKEND_UNKNOWN;
617 static struct rte_eth_dev *
618 virtio_user_eth_dev_alloc(struct rte_vdev_device *vdev)
620 struct rte_eth_dev *eth_dev;
621 struct rte_eth_dev_data *data;
622 struct virtio_hw *hw;
623 struct virtio_user_dev *dev;
625 eth_dev = rte_eth_vdev_allocate(vdev, sizeof(*dev));
627 PMD_INIT_LOG(ERR, "cannot alloc rte_eth_dev");
631 data = eth_dev->data;
632 dev = eth_dev->data->dev_private;
635 hw->port_id = data->port_id;
636 dev->port_id = data->port_id;
637 VIRTIO_OPS(hw) = &virtio_user_ops;
642 hw->use_inorder_rx = 0;
643 hw->use_inorder_tx = 0;
649 virtio_user_eth_dev_free(struct rte_eth_dev *eth_dev)
651 rte_eth_dev_release_port(eth_dev);
654 /* Dev initialization routine. Invoked once for each virtio vdev at
655 * EAL init time, see rte_bus_probe().
656 * Returns 0 on success.
659 virtio_user_pmd_probe(struct rte_vdev_device *vdev)
661 struct rte_kvargs *kvlist = NULL;
662 struct rte_eth_dev *eth_dev;
663 struct virtio_hw *hw;
664 struct virtio_user_dev *dev;
665 enum virtio_user_backend_type backend_type = VIRTIO_USER_BACKEND_UNKNOWN;
666 uint64_t queues = VIRTIO_USER_DEF_Q_NUM;
667 uint64_t cq = VIRTIO_USER_DEF_CQ_EN;
668 uint64_t queue_size = VIRTIO_USER_DEF_Q_SZ;
669 uint64_t server_mode = VIRTIO_USER_DEF_SERVER_MODE;
670 uint64_t mrg_rxbuf = 1;
671 uint64_t in_order = 1;
672 uint64_t packed_vq = 0;
673 uint64_t vectorized = 0;
676 char *mac_addr = NULL;
679 RTE_BUILD_BUG_ON(offsetof(struct virtio_user_dev, hw) != 0);
681 if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
682 const char *name = rte_vdev_device_name(vdev);
683 eth_dev = rte_eth_dev_attach_secondary(name);
685 PMD_INIT_LOG(ERR, "Failed to probe %s", name);
689 dev = eth_dev->data->dev_private;
691 VIRTIO_OPS(hw) = &virtio_user_ops;
693 if (eth_virtio_dev_init(eth_dev) < 0) {
694 PMD_INIT_LOG(ERR, "eth_virtio_dev_init fails");
695 rte_eth_dev_release_port(eth_dev);
699 eth_dev->dev_ops = &virtio_user_secondary_eth_dev_ops;
700 eth_dev->device = &vdev->device;
701 rte_eth_dev_probing_finish(eth_dev);
705 kvlist = rte_kvargs_parse(rte_vdev_device_args(vdev), valid_args);
707 PMD_INIT_LOG(ERR, "error when parsing param");
711 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_PATH) == 1) {
712 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_PATH,
713 &get_string_arg, &path) < 0) {
714 PMD_INIT_LOG(ERR, "error to parse %s",
715 VIRTIO_USER_ARG_PATH);
719 PMD_INIT_LOG(ERR, "arg %s is mandatory for virtio_user",
720 VIRTIO_USER_ARG_PATH);
724 backend_type = virtio_user_backend_type(path);
725 if (backend_type == VIRTIO_USER_BACKEND_UNKNOWN) {
727 "unable to determine backend type for path %s",
731 PMD_INIT_LOG(INFO, "Backend type detected: %s",
732 virtio_user_backend_strings[backend_type]);
734 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_INTERFACE_NAME) == 1) {
735 if (backend_type != VIRTIO_USER_BACKEND_VHOST_KERNEL) {
737 "arg %s applies only to vhost-kernel backend",
738 VIRTIO_USER_ARG_INTERFACE_NAME);
742 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_INTERFACE_NAME,
743 &get_string_arg, &ifname) < 0) {
744 PMD_INIT_LOG(ERR, "error to parse %s",
745 VIRTIO_USER_ARG_INTERFACE_NAME);
750 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_MAC) == 1) {
751 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_MAC,
752 &get_string_arg, &mac_addr) < 0) {
753 PMD_INIT_LOG(ERR, "error to parse %s",
754 VIRTIO_USER_ARG_MAC);
759 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_QUEUE_SIZE) == 1) {
760 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_QUEUE_SIZE,
761 &get_integer_arg, &queue_size) < 0) {
762 PMD_INIT_LOG(ERR, "error to parse %s",
763 VIRTIO_USER_ARG_QUEUE_SIZE);
768 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_QUEUES_NUM) == 1) {
769 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_QUEUES_NUM,
770 &get_integer_arg, &queues) < 0) {
771 PMD_INIT_LOG(ERR, "error to parse %s",
772 VIRTIO_USER_ARG_QUEUES_NUM);
777 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_SERVER_MODE) == 1) {
778 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_SERVER_MODE,
779 &get_integer_arg, &server_mode) < 0) {
780 PMD_INIT_LOG(ERR, "error to parse %s",
781 VIRTIO_USER_ARG_SERVER_MODE);
786 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_CQ_NUM) == 1) {
787 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_CQ_NUM,
788 &get_integer_arg, &cq) < 0) {
789 PMD_INIT_LOG(ERR, "error to parse %s",
790 VIRTIO_USER_ARG_CQ_NUM);
793 } else if (queues > 1) {
797 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_PACKED_VQ) == 1) {
798 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_PACKED_VQ,
799 &get_integer_arg, &packed_vq) < 0) {
800 PMD_INIT_LOG(ERR, "error to parse %s",
801 VIRTIO_USER_ARG_PACKED_VQ);
806 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_VECTORIZED) == 1) {
807 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_VECTORIZED,
808 &get_integer_arg, &vectorized) < 0) {
809 PMD_INIT_LOG(ERR, "error to parse %s",
810 VIRTIO_USER_ARG_VECTORIZED);
815 if (queues > 1 && cq == 0) {
816 PMD_INIT_LOG(ERR, "multi-q requires ctrl-q");
820 if (queues > VIRTIO_MAX_VIRTQUEUE_PAIRS) {
821 PMD_INIT_LOG(ERR, "arg %s %" PRIu64 " exceeds the limit %u",
822 VIRTIO_USER_ARG_QUEUES_NUM, queues,
823 VIRTIO_MAX_VIRTQUEUE_PAIRS);
827 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_MRG_RXBUF) == 1) {
828 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_MRG_RXBUF,
829 &get_integer_arg, &mrg_rxbuf) < 0) {
830 PMD_INIT_LOG(ERR, "error to parse %s",
831 VIRTIO_USER_ARG_MRG_RXBUF);
836 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_IN_ORDER) == 1) {
837 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_IN_ORDER,
838 &get_integer_arg, &in_order) < 0) {
839 PMD_INIT_LOG(ERR, "error to parse %s",
840 VIRTIO_USER_ARG_IN_ORDER);
845 eth_dev = virtio_user_eth_dev_alloc(vdev);
847 PMD_INIT_LOG(ERR, "virtio_user fails to alloc device");
851 dev = eth_dev->data->dev_private;
853 if (virtio_user_dev_init(dev, path, queues, cq,
854 queue_size, mac_addr, &ifname, server_mode,
855 mrg_rxbuf, in_order, packed_vq, backend_type) < 0) {
856 PMD_INIT_LOG(ERR, "virtio_user_dev_init fails");
857 virtio_user_eth_dev_free(eth_dev);
861 /* previously called by pci probing for physical dev */
862 if (eth_virtio_dev_init(eth_dev) < 0) {
863 PMD_INIT_LOG(ERR, "eth_virtio_dev_init fails");
864 virtio_user_eth_dev_free(eth_dev);
870 #if defined(CC_AVX512_SUPPORT) || defined(RTE_ARCH_ARM)
875 "building environment do not support packed ring vectorized");
882 rte_eth_dev_probing_finish(eth_dev);
887 rte_kvargs_free(kvlist);
898 virtio_user_pmd_remove(struct rte_vdev_device *vdev)
901 struct rte_eth_dev *eth_dev;
906 name = rte_vdev_device_name(vdev);
907 PMD_DRV_LOG(INFO, "Un-Initializing %s", name);
908 eth_dev = rte_eth_dev_allocated(name);
909 /* Port has already been released by close. */
913 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
914 return rte_eth_dev_release_port(eth_dev);
916 /* make sure the device is stopped, queues freed */
917 return rte_eth_dev_close(eth_dev->data->port_id);
920 static int virtio_user_pmd_dma_map(struct rte_vdev_device *vdev, void *addr,
921 uint64_t iova, size_t len)
924 struct rte_eth_dev *eth_dev;
925 struct virtio_user_dev *dev;
930 name = rte_vdev_device_name(vdev);
931 eth_dev = rte_eth_dev_allocated(name);
932 /* Port has already been released by close. */
936 dev = eth_dev->data->dev_private;
938 if (dev->ops->dma_map)
939 return dev->ops->dma_map(dev, addr, iova, len);
944 static int virtio_user_pmd_dma_unmap(struct rte_vdev_device *vdev, void *addr,
945 uint64_t iova, size_t len)
948 struct rte_eth_dev *eth_dev;
949 struct virtio_user_dev *dev;
954 name = rte_vdev_device_name(vdev);
955 eth_dev = rte_eth_dev_allocated(name);
956 /* Port has already been released by close. */
960 dev = eth_dev->data->dev_private;
962 if (dev->ops->dma_unmap)
963 return dev->ops->dma_unmap(dev, addr, iova, len);
968 static struct rte_vdev_driver virtio_user_driver = {
969 .probe = virtio_user_pmd_probe,
970 .remove = virtio_user_pmd_remove,
971 .dma_map = virtio_user_pmd_dma_map,
972 .dma_unmap = virtio_user_pmd_dma_unmap,
973 .drv_flags = RTE_VDEV_DRV_NEED_IOVA_AS_VA,
976 RTE_PMD_REGISTER_VDEV(net_virtio_user, virtio_user_driver);
977 RTE_PMD_REGISTER_ALIAS(net_virtio_user, virtio_user);
978 RTE_PMD_REGISTER_PARAM_STRING(net_virtio_user,