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 <rte_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"
23 #include "virtio_pci.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(hw) \
30 ((struct virtio_user_dev *)(hw)->virtio_user_dev)
33 virtio_user_reset_queues_packed(struct rte_eth_dev *dev)
35 struct virtio_hw *hw = dev->data->dev_private;
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 < dev->data->nb_rx_queues; i++) {
52 rxvq = dev->data->rx_queues[i];
53 virtqueue_rxvq_reset_packed(rxvq->vq);
54 virtio_dev_rx_queue_setup_finish(dev, i);
57 for (i = 0; i < dev->data->nb_tx_queues; i++) {
58 txvq = 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)
72 struct rte_eth_dev *eth_dev = &rte_eth_devices[dev->port_id];
73 struct virtio_hw *hw = eth_dev->data->dev_private;
74 uint64_t protocol_features;
76 connectfd = accept(dev->listenfd, NULL, NULL);
80 dev->vhostfd = connectfd;
81 if (dev->ops->send_request(dev, VHOST_USER_GET_FEATURES,
82 &dev->device_features) < 0) {
83 PMD_INIT_LOG(ERR, "get_features failed: %s",
88 if (dev->device_features &
89 (1ULL << VHOST_USER_F_PROTOCOL_FEATURES)) {
90 if (dev->ops->send_request(dev,
91 VHOST_USER_GET_PROTOCOL_FEATURES,
95 dev->protocol_features &= protocol_features;
97 if (dev->ops->send_request(dev,
98 VHOST_USER_SET_PROTOCOL_FEATURES,
99 &dev->protocol_features))
102 if (!(dev->protocol_features &
103 (1ULL << VHOST_USER_PROTOCOL_F_MQ)))
104 dev->unsupported_features |= (1ull << VIRTIO_NET_F_MQ);
107 dev->device_features |= dev->frontend_features;
109 /* umask vhost-user unsupported features */
110 dev->device_features &= ~(dev->unsupported_features);
112 dev->features &= dev->device_features;
114 /* For packed ring, resetting queues is required in reconnection. */
115 if (vtpci_packed_queue(hw) &&
116 (vtpci_get_status(hw) & VIRTIO_CONFIG_STATUS_DRIVER_OK)) {
117 PMD_INIT_LOG(NOTICE, "Packets on the fly will be dropped"
118 " when packed ring reconnecting.");
119 virtio_user_reset_queues_packed(eth_dev);
122 ret = virtio_user_start_device(dev);
126 if (dev->queue_pairs > 1) {
127 ret = virtio_user_handle_mq(dev, dev->queue_pairs);
129 PMD_INIT_LOG(ERR, "Fails to enable multi-queue pairs!");
133 if (eth_dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC) {
134 if (rte_intr_disable(eth_dev->intr_handle) < 0) {
135 PMD_DRV_LOG(ERR, "interrupt disable failed");
138 rte_intr_callback_unregister(eth_dev->intr_handle,
139 virtio_interrupt_handler,
141 eth_dev->intr_handle->fd = connectfd;
142 rte_intr_callback_register(eth_dev->intr_handle,
143 virtio_interrupt_handler, eth_dev);
145 if (rte_intr_enable(eth_dev->intr_handle) < 0) {
146 PMD_DRV_LOG(ERR, "interrupt enable failed");
150 PMD_INIT_LOG(NOTICE, "server mode virtio-user reconnection succeeds!");
155 virtio_user_delayed_handler(void *param)
157 struct virtio_hw *hw = (struct virtio_hw *)param;
158 struct rte_eth_dev *eth_dev = &rte_eth_devices[hw->port_id];
159 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
161 if (rte_intr_disable(eth_dev->intr_handle) < 0) {
162 PMD_DRV_LOG(ERR, "interrupt disable failed");
165 rte_intr_callback_unregister(eth_dev->intr_handle,
166 virtio_interrupt_handler, eth_dev);
167 if (dev->is_server) {
168 if (dev->vhostfd >= 0) {
172 eth_dev->intr_handle->fd = dev->listenfd;
173 rte_intr_callback_register(eth_dev->intr_handle,
174 virtio_interrupt_handler, eth_dev);
175 if (rte_intr_enable(eth_dev->intr_handle) < 0) {
176 PMD_DRV_LOG(ERR, "interrupt enable failed");
183 virtio_user_read_dev_config(struct virtio_hw *hw, size_t offset,
184 void *dst, int length)
187 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
189 if (offset == offsetof(struct virtio_net_config, mac) &&
190 length == RTE_ETHER_ADDR_LEN) {
191 for (i = 0; i < RTE_ETHER_ADDR_LEN; ++i)
192 ((uint8_t *)dst)[i] = dev->mac_addr[i];
196 if (offset == offsetof(struct virtio_net_config, status)) {
199 if (dev->vhostfd >= 0) {
203 flags = fcntl(dev->vhostfd, F_GETFL);
204 if (fcntl(dev->vhostfd, F_SETFL,
205 flags | O_NONBLOCK) == -1) {
206 PMD_DRV_LOG(ERR, "error setting O_NONBLOCK flag");
209 r = recv(dev->vhostfd, buf, 128, MSG_PEEK);
210 if (r == 0 || (r < 0 && errno != EAGAIN)) {
211 dev->net_status &= (~VIRTIO_NET_S_LINK_UP);
212 PMD_DRV_LOG(ERR, "virtio-user port %u is down",
215 /* This function could be called in the process
216 * of interrupt handling, callback cannot be
217 * unregistered here, set an alarm to do it.
220 virtio_user_delayed_handler,
223 dev->net_status |= VIRTIO_NET_S_LINK_UP;
225 if (fcntl(dev->vhostfd, F_SETFL,
226 flags & ~O_NONBLOCK) == -1) {
227 PMD_DRV_LOG(ERR, "error clearing O_NONBLOCK flag");
230 } else if (dev->is_server) {
231 dev->net_status &= (~VIRTIO_NET_S_LINK_UP);
232 if (virtio_user_server_reconnect(dev) >= 0)
233 dev->net_status |= VIRTIO_NET_S_LINK_UP;
236 *(uint16_t *)dst = dev->net_status;
239 if (offset == offsetof(struct virtio_net_config, max_virtqueue_pairs))
240 *(uint16_t *)dst = dev->max_queue_pairs;
244 virtio_user_write_dev_config(struct virtio_hw *hw, size_t offset,
245 const void *src, int length)
248 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
250 if ((offset == offsetof(struct virtio_net_config, mac)) &&
251 (length == RTE_ETHER_ADDR_LEN))
252 for (i = 0; i < RTE_ETHER_ADDR_LEN; ++i)
253 dev->mac_addr[i] = ((const uint8_t *)src)[i];
255 PMD_DRV_LOG(ERR, "not supported offset=%zu, len=%d",
260 virtio_user_reset(struct virtio_hw *hw)
262 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
264 if (dev->status & VIRTIO_CONFIG_STATUS_DRIVER_OK)
265 virtio_user_stop_device(dev);
269 virtio_user_set_status(struct virtio_hw *hw, uint8_t status)
271 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
273 if (status & VIRTIO_CONFIG_STATUS_DRIVER_OK)
274 virtio_user_start_device(dev);
275 else if (status == VIRTIO_CONFIG_STATUS_RESET)
276 virtio_user_reset(hw);
277 dev->status = status;
278 virtio_user_send_status_update(dev, status);
282 virtio_user_get_status(struct virtio_hw *hw)
284 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
286 virtio_user_update_status(dev);
292 virtio_user_get_features(struct virtio_hw *hw)
294 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
296 /* unmask feature bits defined in vhost user protocol */
297 return dev->device_features & VIRTIO_PMD_SUPPORTED_GUEST_FEATURES;
301 virtio_user_set_features(struct virtio_hw *hw, uint64_t features)
303 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
305 dev->features = features & dev->device_features;
309 virtio_user_get_isr(struct virtio_hw *hw __rte_unused)
311 /* rxq interrupts and config interrupt are separated in virtio-user,
312 * here we only report config change.
314 return VIRTIO_PCI_ISR_CONFIG;
318 virtio_user_set_config_irq(struct virtio_hw *hw __rte_unused,
319 uint16_t vec __rte_unused)
325 virtio_user_set_queue_irq(struct virtio_hw *hw __rte_unused,
326 struct virtqueue *vq __rte_unused,
329 /* pretend we have done that */
333 /* This function is to get the queue size, aka, number of descs, of a specified
334 * queue. Different with the VHOST_USER_GET_QUEUE_NUM, which is used to get the
335 * max supported queues.
338 virtio_user_get_queue_num(struct virtio_hw *hw, uint16_t queue_id __rte_unused)
340 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
342 /* Currently, each queue has same queue size */
343 return dev->queue_size;
347 virtio_user_setup_queue_packed(struct virtqueue *vq,
348 struct virtio_user_dev *dev)
350 uint16_t queue_idx = vq->vq_queue_index;
351 struct vring_packed *vring;
357 vring = &dev->packed_vrings[queue_idx];
358 desc_addr = (uintptr_t)vq->vq_ring_virt_mem;
359 avail_addr = desc_addr + vq->vq_nentries *
360 sizeof(struct vring_packed_desc);
361 used_addr = RTE_ALIGN_CEIL(avail_addr +
362 sizeof(struct vring_packed_desc_event),
363 VIRTIO_PCI_VRING_ALIGN);
364 vring->num = vq->vq_nentries;
365 vring->desc = (void *)(uintptr_t)desc_addr;
366 vring->driver = (void *)(uintptr_t)avail_addr;
367 vring->device = (void *)(uintptr_t)used_addr;
368 dev->packed_queues[queue_idx].avail_wrap_counter = true;
369 dev->packed_queues[queue_idx].used_wrap_counter = true;
371 for (i = 0; i < vring->num; i++)
372 vring->desc[i].flags = 0;
376 virtio_user_setup_queue_split(struct virtqueue *vq, struct virtio_user_dev *dev)
378 uint16_t queue_idx = vq->vq_queue_index;
379 uint64_t desc_addr, avail_addr, used_addr;
381 desc_addr = (uintptr_t)vq->vq_ring_virt_mem;
382 avail_addr = desc_addr + vq->vq_nentries * sizeof(struct vring_desc);
383 used_addr = RTE_ALIGN_CEIL(avail_addr + offsetof(struct vring_avail,
384 ring[vq->vq_nentries]),
385 VIRTIO_PCI_VRING_ALIGN);
387 dev->vrings[queue_idx].num = vq->vq_nentries;
388 dev->vrings[queue_idx].desc = (void *)(uintptr_t)desc_addr;
389 dev->vrings[queue_idx].avail = (void *)(uintptr_t)avail_addr;
390 dev->vrings[queue_idx].used = (void *)(uintptr_t)used_addr;
394 virtio_user_setup_queue(struct virtio_hw *hw, struct virtqueue *vq)
396 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
398 if (vtpci_packed_queue(hw))
399 virtio_user_setup_queue_packed(vq, dev);
401 virtio_user_setup_queue_split(vq, dev);
407 virtio_user_del_queue(struct virtio_hw *hw, struct virtqueue *vq)
409 /* For legacy devices, write 0 to VIRTIO_PCI_QUEUE_PFN port, QEMU
410 * correspondingly stops the ioeventfds, and reset the status of
412 * For modern devices, set queue desc, avail, used in PCI bar to 0,
413 * not see any more behavior in QEMU.
415 * Here we just care about what information to deliver to vhost-user
416 * or vhost-kernel. So we just close ioeventfd for now.
418 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
420 close(dev->callfds[vq->vq_queue_index]);
421 close(dev->kickfds[vq->vq_queue_index]);
425 virtio_user_notify_queue(struct virtio_hw *hw, struct virtqueue *vq)
428 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
430 if (hw->cvq && (hw->cvq->vq == vq)) {
431 if (vtpci_packed_queue(vq->hw))
432 virtio_user_handle_cq_packed(dev, vq->vq_queue_index);
434 virtio_user_handle_cq(dev, vq->vq_queue_index);
438 if (write(dev->kickfds[vq->vq_queue_index], &buf, sizeof(buf)) < 0)
439 PMD_DRV_LOG(ERR, "failed to kick backend: %s",
443 const struct virtio_pci_ops virtio_user_ops = {
444 .read_dev_cfg = virtio_user_read_dev_config,
445 .write_dev_cfg = virtio_user_write_dev_config,
446 .get_status = virtio_user_get_status,
447 .set_status = virtio_user_set_status,
448 .get_features = virtio_user_get_features,
449 .set_features = virtio_user_set_features,
450 .get_isr = virtio_user_get_isr,
451 .set_config_irq = virtio_user_set_config_irq,
452 .set_queue_irq = virtio_user_set_queue_irq,
453 .get_queue_num = virtio_user_get_queue_num,
454 .setup_queue = virtio_user_setup_queue,
455 .del_queue = virtio_user_del_queue,
456 .notify_queue = virtio_user_notify_queue,
459 static const char *valid_args[] = {
460 #define VIRTIO_USER_ARG_QUEUES_NUM "queues"
461 VIRTIO_USER_ARG_QUEUES_NUM,
462 #define VIRTIO_USER_ARG_CQ_NUM "cq"
463 VIRTIO_USER_ARG_CQ_NUM,
464 #define VIRTIO_USER_ARG_MAC "mac"
466 #define VIRTIO_USER_ARG_PATH "path"
467 VIRTIO_USER_ARG_PATH,
468 #define VIRTIO_USER_ARG_QUEUE_SIZE "queue_size"
469 VIRTIO_USER_ARG_QUEUE_SIZE,
470 #define VIRTIO_USER_ARG_INTERFACE_NAME "iface"
471 VIRTIO_USER_ARG_INTERFACE_NAME,
472 #define VIRTIO_USER_ARG_SERVER_MODE "server"
473 VIRTIO_USER_ARG_SERVER_MODE,
474 #define VIRTIO_USER_ARG_MRG_RXBUF "mrg_rxbuf"
475 VIRTIO_USER_ARG_MRG_RXBUF,
476 #define VIRTIO_USER_ARG_IN_ORDER "in_order"
477 VIRTIO_USER_ARG_IN_ORDER,
478 #define VIRTIO_USER_ARG_PACKED_VQ "packed_vq"
479 VIRTIO_USER_ARG_PACKED_VQ,
480 #define VIRTIO_USER_ARG_SPEED "speed"
481 VIRTIO_USER_ARG_SPEED,
482 #define VIRTIO_USER_ARG_VECTORIZED "vectorized"
483 VIRTIO_USER_ARG_VECTORIZED,
487 #define VIRTIO_USER_DEF_CQ_EN 0
488 #define VIRTIO_USER_DEF_Q_NUM 1
489 #define VIRTIO_USER_DEF_Q_SZ 256
490 #define VIRTIO_USER_DEF_SERVER_MODE 0
493 get_string_arg(const char *key __rte_unused,
494 const char *value, void *extra_args)
496 if (!value || !extra_args)
499 *(char **)extra_args = strdup(value);
501 if (!*(char **)extra_args)
508 get_integer_arg(const char *key __rte_unused,
509 const char *value, void *extra_args)
511 uint64_t integer = 0;
512 if (!value || !extra_args)
515 integer = strtoull(value, NULL, 0);
516 /* extra_args keeps default value, it should be replaced
517 * only in case of successful parsing of the 'value' arg
520 *(uint64_t *)extra_args = integer;
525 vdpa_dynamic_major_num(void)
534 fp = fopen("/proc/devices", "r");
536 PMD_INIT_LOG(ERR, "Cannot open /proc/devices: %s",
538 return UNNAMED_MAJOR;
541 while (getline(&line, &size, fp) > 0) {
542 char *stripped = line + strspn(line, " ");
543 if ((sscanf(stripped, "%u %10s", &num, name) == 2) &&
544 (strncmp(name, "vhost-vdpa", 10) == 0)) {
550 return found ? num : UNNAMED_MAJOR;
553 static enum virtio_user_backend_type
554 virtio_user_backend_type(const char *path)
558 if (stat(path, &sb) == -1) {
559 PMD_INIT_LOG(ERR, "Stat fails: %s (%s)\n", path,
561 return VIRTIO_USER_BACKEND_UNKNOWN;
564 if (S_ISSOCK(sb.st_mode)) {
565 return VIRTIO_USER_BACKEND_VHOST_USER;
566 } else if (S_ISCHR(sb.st_mode)) {
567 if (major(sb.st_rdev) == MISC_MAJOR)
568 return VIRTIO_USER_BACKEND_VHOST_KERNEL;
569 if (major(sb.st_rdev) == vdpa_dynamic_major_num())
570 return VIRTIO_USER_BACKEND_VHOST_VDPA;
572 return VIRTIO_USER_BACKEND_UNKNOWN;
575 static struct rte_eth_dev *
576 virtio_user_eth_dev_alloc(struct rte_vdev_device *vdev)
578 struct rte_eth_dev *eth_dev;
579 struct rte_eth_dev_data *data;
580 struct virtio_hw *hw;
581 struct virtio_user_dev *dev;
583 eth_dev = rte_eth_vdev_allocate(vdev, sizeof(*hw));
585 PMD_INIT_LOG(ERR, "cannot alloc rte_eth_dev");
589 data = eth_dev->data;
590 hw = eth_dev->data->dev_private;
592 dev = rte_zmalloc(NULL, sizeof(*dev), 0);
594 PMD_INIT_LOG(ERR, "malloc virtio_user_dev failed");
595 rte_eth_dev_release_port(eth_dev);
599 hw->port_id = data->port_id;
600 dev->port_id = data->port_id;
601 virtio_hw_internal[hw->port_id].vtpci_ops = &virtio_user_ops;
603 * MSIX is required to enable LSC (see virtio_init_device).
604 * Here just pretend that we support msix.
610 hw->use_inorder_rx = 0;
611 hw->use_inorder_tx = 0;
612 hw->virtio_user_dev = dev;
617 virtio_user_eth_dev_free(struct rte_eth_dev *eth_dev)
619 struct rte_eth_dev_data *data = eth_dev->data;
620 struct virtio_hw *hw = data->dev_private;
622 rte_free(hw->virtio_user_dev);
623 rte_eth_dev_release_port(eth_dev);
626 /* Dev initialization routine. Invoked once for each virtio vdev at
627 * EAL init time, see rte_bus_probe().
628 * Returns 0 on success.
631 virtio_user_pmd_probe(struct rte_vdev_device *dev)
633 struct rte_kvargs *kvlist = NULL;
634 struct rte_eth_dev *eth_dev;
635 struct virtio_hw *hw;
636 enum virtio_user_backend_type backend_type = VIRTIO_USER_BACKEND_UNKNOWN;
637 uint64_t queues = VIRTIO_USER_DEF_Q_NUM;
638 uint64_t cq = VIRTIO_USER_DEF_CQ_EN;
639 uint64_t queue_size = VIRTIO_USER_DEF_Q_SZ;
640 uint64_t server_mode = VIRTIO_USER_DEF_SERVER_MODE;
641 uint64_t mrg_rxbuf = 1;
642 uint64_t in_order = 1;
643 uint64_t packed_vq = 0;
644 uint64_t vectorized = 0;
647 char *mac_addr = NULL;
650 if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
651 const char *name = rte_vdev_device_name(dev);
652 eth_dev = rte_eth_dev_attach_secondary(name);
654 PMD_INIT_LOG(ERR, "Failed to probe %s", name);
658 if (eth_virtio_dev_init(eth_dev) < 0) {
659 PMD_INIT_LOG(ERR, "eth_virtio_dev_init fails");
660 rte_eth_dev_release_port(eth_dev);
664 eth_dev->dev_ops = &virtio_user_secondary_eth_dev_ops;
665 eth_dev->device = &dev->device;
666 rte_eth_dev_probing_finish(eth_dev);
670 kvlist = rte_kvargs_parse(rte_vdev_device_args(dev), valid_args);
672 PMD_INIT_LOG(ERR, "error when parsing param");
676 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_PATH) == 1) {
677 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_PATH,
678 &get_string_arg, &path) < 0) {
679 PMD_INIT_LOG(ERR, "error to parse %s",
680 VIRTIO_USER_ARG_PATH);
684 PMD_INIT_LOG(ERR, "arg %s is mandatory for virtio_user",
685 VIRTIO_USER_ARG_PATH);
689 backend_type = virtio_user_backend_type(path);
690 if (backend_type == VIRTIO_USER_BACKEND_UNKNOWN) {
692 "unable to determine backend type for path %s",
698 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_INTERFACE_NAME) == 1) {
699 if (backend_type != VIRTIO_USER_BACKEND_VHOST_KERNEL) {
701 "arg %s applies only to vhost-kernel backend",
702 VIRTIO_USER_ARG_INTERFACE_NAME);
706 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_INTERFACE_NAME,
707 &get_string_arg, &ifname) < 0) {
708 PMD_INIT_LOG(ERR, "error to parse %s",
709 VIRTIO_USER_ARG_INTERFACE_NAME);
714 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_MAC) == 1) {
715 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_MAC,
716 &get_string_arg, &mac_addr) < 0) {
717 PMD_INIT_LOG(ERR, "error to parse %s",
718 VIRTIO_USER_ARG_MAC);
723 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_QUEUE_SIZE) == 1) {
724 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_QUEUE_SIZE,
725 &get_integer_arg, &queue_size) < 0) {
726 PMD_INIT_LOG(ERR, "error to parse %s",
727 VIRTIO_USER_ARG_QUEUE_SIZE);
732 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_QUEUES_NUM) == 1) {
733 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_QUEUES_NUM,
734 &get_integer_arg, &queues) < 0) {
735 PMD_INIT_LOG(ERR, "error to parse %s",
736 VIRTIO_USER_ARG_QUEUES_NUM);
741 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_SERVER_MODE) == 1) {
742 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_SERVER_MODE,
743 &get_integer_arg, &server_mode) < 0) {
744 PMD_INIT_LOG(ERR, "error to parse %s",
745 VIRTIO_USER_ARG_SERVER_MODE);
750 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_CQ_NUM) == 1) {
751 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_CQ_NUM,
752 &get_integer_arg, &cq) < 0) {
753 PMD_INIT_LOG(ERR, "error to parse %s",
754 VIRTIO_USER_ARG_CQ_NUM);
757 } else if (queues > 1) {
761 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_PACKED_VQ) == 1) {
762 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_PACKED_VQ,
763 &get_integer_arg, &packed_vq) < 0) {
764 PMD_INIT_LOG(ERR, "error to parse %s",
765 VIRTIO_USER_ARG_PACKED_VQ);
770 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_VECTORIZED) == 1) {
771 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_VECTORIZED,
772 &get_integer_arg, &vectorized) < 0) {
773 PMD_INIT_LOG(ERR, "error to parse %s",
774 VIRTIO_USER_ARG_VECTORIZED);
779 if (queues > 1 && cq == 0) {
780 PMD_INIT_LOG(ERR, "multi-q requires ctrl-q");
784 if (queues > VIRTIO_MAX_VIRTQUEUE_PAIRS) {
785 PMD_INIT_LOG(ERR, "arg %s %" PRIu64 " exceeds the limit %u",
786 VIRTIO_USER_ARG_QUEUES_NUM, queues,
787 VIRTIO_MAX_VIRTQUEUE_PAIRS);
791 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_MRG_RXBUF) == 1) {
792 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_MRG_RXBUF,
793 &get_integer_arg, &mrg_rxbuf) < 0) {
794 PMD_INIT_LOG(ERR, "error to parse %s",
795 VIRTIO_USER_ARG_MRG_RXBUF);
800 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_IN_ORDER) == 1) {
801 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_IN_ORDER,
802 &get_integer_arg, &in_order) < 0) {
803 PMD_INIT_LOG(ERR, "error to parse %s",
804 VIRTIO_USER_ARG_IN_ORDER);
809 eth_dev = virtio_user_eth_dev_alloc(dev);
811 PMD_INIT_LOG(ERR, "virtio_user fails to alloc device");
815 hw = eth_dev->data->dev_private;
816 if (virtio_user_dev_init(hw->virtio_user_dev, path, queues, cq,
817 queue_size, mac_addr, &ifname, server_mode,
818 mrg_rxbuf, in_order, packed_vq, backend_type) < 0) {
819 PMD_INIT_LOG(ERR, "virtio_user_dev_init fails");
820 virtio_user_eth_dev_free(eth_dev);
824 /* previously called by pci probing for physical dev */
825 if (eth_virtio_dev_init(eth_dev) < 0) {
826 PMD_INIT_LOG(ERR, "eth_virtio_dev_init fails");
827 virtio_user_eth_dev_free(eth_dev);
833 #if defined(CC_AVX512_SUPPORT)
838 "building environment do not support packed ring vectorized");
845 rte_eth_dev_probing_finish(eth_dev);
850 rte_kvargs_free(kvlist);
861 virtio_user_pmd_remove(struct rte_vdev_device *vdev)
864 struct rte_eth_dev *eth_dev;
869 name = rte_vdev_device_name(vdev);
870 PMD_DRV_LOG(INFO, "Un-Initializing %s", name);
871 eth_dev = rte_eth_dev_allocated(name);
872 /* Port has already been released by close. */
876 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
877 return rte_eth_dev_release_port(eth_dev);
879 /* make sure the device is stopped, queues freed */
880 rte_eth_dev_close(eth_dev->data->port_id);
885 static int virtio_user_pmd_dma_map(struct rte_vdev_device *vdev, void *addr,
886 uint64_t iova, size_t len)
889 struct rte_eth_dev *eth_dev;
890 struct virtio_user_dev *dev;
891 struct virtio_hw *hw;
896 name = rte_vdev_device_name(vdev);
897 eth_dev = rte_eth_dev_allocated(name);
898 /* Port has already been released by close. */
902 hw = (struct virtio_hw *)eth_dev->data->dev_private;
903 dev = hw->virtio_user_dev;
905 if (dev->ops->dma_map)
906 return dev->ops->dma_map(dev, addr, iova, len);
911 static int virtio_user_pmd_dma_unmap(struct rte_vdev_device *vdev, void *addr,
912 uint64_t iova, size_t len)
915 struct rte_eth_dev *eth_dev;
916 struct virtio_user_dev *dev;
917 struct virtio_hw *hw;
922 name = rte_vdev_device_name(vdev);
923 eth_dev = rte_eth_dev_allocated(name);
924 /* Port has already been released by close. */
928 hw = (struct virtio_hw *)eth_dev->data->dev_private;
929 dev = hw->virtio_user_dev;
931 if (dev->ops->dma_unmap)
932 return dev->ops->dma_unmap(dev, addr, iova, len);
937 static struct rte_vdev_driver virtio_user_driver = {
938 .probe = virtio_user_pmd_probe,
939 .remove = virtio_user_pmd_remove,
940 .dma_map = virtio_user_pmd_dma_map,
941 .dma_unmap = virtio_user_pmd_dma_unmap,
944 RTE_PMD_REGISTER_VDEV(net_virtio_user, virtio_user_driver);
945 RTE_PMD_REGISTER_ALIAS(net_virtio_user, virtio_user);
946 RTE_PMD_REGISTER_PARAM_STRING(net_virtio_user,