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);
272 uint8_t old_status = dev->status;
274 if (status & VIRTIO_CONFIG_STATUS_FEATURES_OK &&
275 ~old_status & VIRTIO_CONFIG_STATUS_FEATURES_OK)
276 virtio_user_dev_set_features(dev);
277 if (status & VIRTIO_CONFIG_STATUS_DRIVER_OK)
278 virtio_user_start_device(dev);
279 else if (status == VIRTIO_CONFIG_STATUS_RESET)
280 virtio_user_reset(hw);
282 virtio_user_dev_set_status(dev, status);
286 virtio_user_get_status(struct virtio_hw *hw)
288 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
290 virtio_user_dev_update_status(dev);
296 virtio_user_get_features(struct virtio_hw *hw)
298 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
300 /* unmask feature bits defined in vhost user protocol */
301 return dev->device_features & VIRTIO_PMD_SUPPORTED_GUEST_FEATURES;
305 virtio_user_set_features(struct virtio_hw *hw, uint64_t features)
307 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
309 dev->features = features & dev->device_features;
313 virtio_user_get_isr(struct virtio_hw *hw __rte_unused)
315 /* rxq interrupts and config interrupt are separated in virtio-user,
316 * here we only report config change.
318 return VIRTIO_PCI_ISR_CONFIG;
322 virtio_user_set_config_irq(struct virtio_hw *hw __rte_unused,
323 uint16_t vec __rte_unused)
329 virtio_user_set_queue_irq(struct virtio_hw *hw __rte_unused,
330 struct virtqueue *vq __rte_unused,
333 /* pretend we have done that */
337 /* This function is to get the queue size, aka, number of descs, of a specified
338 * queue. Different with the VHOST_USER_GET_QUEUE_NUM, which is used to get the
339 * max supported queues.
342 virtio_user_get_queue_num(struct virtio_hw *hw, uint16_t queue_id __rte_unused)
344 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
346 /* Currently, each queue has same queue size */
347 return dev->queue_size;
351 virtio_user_setup_queue_packed(struct virtqueue *vq,
352 struct virtio_user_dev *dev)
354 uint16_t queue_idx = vq->vq_queue_index;
355 struct vring_packed *vring;
361 vring = &dev->packed_vrings[queue_idx];
362 desc_addr = (uintptr_t)vq->vq_ring_virt_mem;
363 avail_addr = desc_addr + vq->vq_nentries *
364 sizeof(struct vring_packed_desc);
365 used_addr = RTE_ALIGN_CEIL(avail_addr +
366 sizeof(struct vring_packed_desc_event),
367 VIRTIO_PCI_VRING_ALIGN);
368 vring->num = vq->vq_nentries;
369 vring->desc = (void *)(uintptr_t)desc_addr;
370 vring->driver = (void *)(uintptr_t)avail_addr;
371 vring->device = (void *)(uintptr_t)used_addr;
372 dev->packed_queues[queue_idx].avail_wrap_counter = true;
373 dev->packed_queues[queue_idx].used_wrap_counter = true;
375 for (i = 0; i < vring->num; i++)
376 vring->desc[i].flags = 0;
380 virtio_user_setup_queue_split(struct virtqueue *vq, struct virtio_user_dev *dev)
382 uint16_t queue_idx = vq->vq_queue_index;
383 uint64_t desc_addr, avail_addr, used_addr;
385 desc_addr = (uintptr_t)vq->vq_ring_virt_mem;
386 avail_addr = desc_addr + vq->vq_nentries * sizeof(struct vring_desc);
387 used_addr = RTE_ALIGN_CEIL(avail_addr + offsetof(struct vring_avail,
388 ring[vq->vq_nentries]),
389 VIRTIO_PCI_VRING_ALIGN);
391 dev->vrings[queue_idx].num = vq->vq_nentries;
392 dev->vrings[queue_idx].desc = (void *)(uintptr_t)desc_addr;
393 dev->vrings[queue_idx].avail = (void *)(uintptr_t)avail_addr;
394 dev->vrings[queue_idx].used = (void *)(uintptr_t)used_addr;
398 virtio_user_setup_queue(struct virtio_hw *hw, struct virtqueue *vq)
400 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
402 if (vtpci_packed_queue(hw))
403 virtio_user_setup_queue_packed(vq, dev);
405 virtio_user_setup_queue_split(vq, dev);
411 virtio_user_del_queue(struct virtio_hw *hw, struct virtqueue *vq)
413 /* For legacy devices, write 0 to VIRTIO_PCI_QUEUE_PFN port, QEMU
414 * correspondingly stops the ioeventfds, and reset the status of
416 * For modern devices, set queue desc, avail, used in PCI bar to 0,
417 * not see any more behavior in QEMU.
419 * Here we just care about what information to deliver to vhost-user
420 * or vhost-kernel. So we just close ioeventfd for now.
422 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
424 close(dev->callfds[vq->vq_queue_index]);
425 close(dev->kickfds[vq->vq_queue_index]);
429 virtio_user_notify_queue(struct virtio_hw *hw, struct virtqueue *vq)
432 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
434 if (hw->cvq && (hw->cvq->vq == vq)) {
435 if (vtpci_packed_queue(vq->hw))
436 virtio_user_handle_cq_packed(dev, vq->vq_queue_index);
438 virtio_user_handle_cq(dev, vq->vq_queue_index);
442 if (write(dev->kickfds[vq->vq_queue_index], &buf, sizeof(buf)) < 0)
443 PMD_DRV_LOG(ERR, "failed to kick backend: %s",
447 const struct virtio_pci_ops virtio_user_ops = {
448 .read_dev_cfg = virtio_user_read_dev_config,
449 .write_dev_cfg = virtio_user_write_dev_config,
450 .get_status = virtio_user_get_status,
451 .set_status = virtio_user_set_status,
452 .get_features = virtio_user_get_features,
453 .set_features = virtio_user_set_features,
454 .get_isr = virtio_user_get_isr,
455 .set_config_irq = virtio_user_set_config_irq,
456 .set_queue_irq = virtio_user_set_queue_irq,
457 .get_queue_num = virtio_user_get_queue_num,
458 .setup_queue = virtio_user_setup_queue,
459 .del_queue = virtio_user_del_queue,
460 .notify_queue = virtio_user_notify_queue,
463 static const char *valid_args[] = {
464 #define VIRTIO_USER_ARG_QUEUES_NUM "queues"
465 VIRTIO_USER_ARG_QUEUES_NUM,
466 #define VIRTIO_USER_ARG_CQ_NUM "cq"
467 VIRTIO_USER_ARG_CQ_NUM,
468 #define VIRTIO_USER_ARG_MAC "mac"
470 #define VIRTIO_USER_ARG_PATH "path"
471 VIRTIO_USER_ARG_PATH,
472 #define VIRTIO_USER_ARG_QUEUE_SIZE "queue_size"
473 VIRTIO_USER_ARG_QUEUE_SIZE,
474 #define VIRTIO_USER_ARG_INTERFACE_NAME "iface"
475 VIRTIO_USER_ARG_INTERFACE_NAME,
476 #define VIRTIO_USER_ARG_SERVER_MODE "server"
477 VIRTIO_USER_ARG_SERVER_MODE,
478 #define VIRTIO_USER_ARG_MRG_RXBUF "mrg_rxbuf"
479 VIRTIO_USER_ARG_MRG_RXBUF,
480 #define VIRTIO_USER_ARG_IN_ORDER "in_order"
481 VIRTIO_USER_ARG_IN_ORDER,
482 #define VIRTIO_USER_ARG_PACKED_VQ "packed_vq"
483 VIRTIO_USER_ARG_PACKED_VQ,
484 #define VIRTIO_USER_ARG_SPEED "speed"
485 VIRTIO_USER_ARG_SPEED,
486 #define VIRTIO_USER_ARG_VECTORIZED "vectorized"
487 VIRTIO_USER_ARG_VECTORIZED,
491 #define VIRTIO_USER_DEF_CQ_EN 0
492 #define VIRTIO_USER_DEF_Q_NUM 1
493 #define VIRTIO_USER_DEF_Q_SZ 256
494 #define VIRTIO_USER_DEF_SERVER_MODE 0
497 get_string_arg(const char *key __rte_unused,
498 const char *value, void *extra_args)
500 if (!value || !extra_args)
503 *(char **)extra_args = strdup(value);
505 if (!*(char **)extra_args)
512 get_integer_arg(const char *key __rte_unused,
513 const char *value, void *extra_args)
515 uint64_t integer = 0;
516 if (!value || !extra_args)
519 integer = strtoull(value, NULL, 0);
520 /* extra_args keeps default value, it should be replaced
521 * only in case of successful parsing of the 'value' arg
524 *(uint64_t *)extra_args = integer;
529 vdpa_dynamic_major_num(void)
538 fp = fopen("/proc/devices", "r");
540 PMD_INIT_LOG(ERR, "Cannot open /proc/devices: %s",
542 return UNNAMED_MAJOR;
545 while (getline(&line, &size, fp) > 0) {
546 char *stripped = line + strspn(line, " ");
547 if ((sscanf(stripped, "%u %10s", &num, name) == 2) &&
548 (strncmp(name, "vhost-vdpa", 10) == 0)) {
554 return found ? num : UNNAMED_MAJOR;
557 static enum virtio_user_backend_type
558 virtio_user_backend_type(const char *path)
562 if (stat(path, &sb) == -1) {
564 return VIRTIO_USER_BACKEND_VHOST_USER;
566 PMD_INIT_LOG(ERR, "Stat fails: %s (%s)\n", path,
568 return VIRTIO_USER_BACKEND_UNKNOWN;
571 if (S_ISSOCK(sb.st_mode)) {
572 return VIRTIO_USER_BACKEND_VHOST_USER;
573 } else if (S_ISCHR(sb.st_mode)) {
574 if (major(sb.st_rdev) == MISC_MAJOR)
575 return VIRTIO_USER_BACKEND_VHOST_KERNEL;
576 if (major(sb.st_rdev) == vdpa_dynamic_major_num())
577 return VIRTIO_USER_BACKEND_VHOST_VDPA;
579 return VIRTIO_USER_BACKEND_UNKNOWN;
582 static struct rte_eth_dev *
583 virtio_user_eth_dev_alloc(struct rte_vdev_device *vdev)
585 struct rte_eth_dev *eth_dev;
586 struct rte_eth_dev_data *data;
587 struct virtio_hw *hw;
588 struct virtio_user_dev *dev;
590 eth_dev = rte_eth_vdev_allocate(vdev, sizeof(*hw));
592 PMD_INIT_LOG(ERR, "cannot alloc rte_eth_dev");
596 data = eth_dev->data;
597 hw = eth_dev->data->dev_private;
599 dev = rte_zmalloc(NULL, sizeof(*dev), 0);
601 PMD_INIT_LOG(ERR, "malloc virtio_user_dev failed");
602 rte_eth_dev_release_port(eth_dev);
606 hw->port_id = data->port_id;
607 dev->port_id = data->port_id;
608 virtio_hw_internal[hw->port_id].vtpci_ops = &virtio_user_ops;
610 * MSIX is required to enable LSC (see virtio_init_device).
611 * Here just pretend that we support msix.
617 hw->use_inorder_rx = 0;
618 hw->use_inorder_tx = 0;
619 hw->virtio_user_dev = dev;
624 virtio_user_eth_dev_free(struct rte_eth_dev *eth_dev)
626 struct rte_eth_dev_data *data = eth_dev->data;
627 struct virtio_hw *hw = data->dev_private;
629 rte_free(hw->virtio_user_dev);
630 rte_eth_dev_release_port(eth_dev);
633 /* Dev initialization routine. Invoked once for each virtio vdev at
634 * EAL init time, see rte_bus_probe().
635 * Returns 0 on success.
638 virtio_user_pmd_probe(struct rte_vdev_device *dev)
640 struct rte_kvargs *kvlist = NULL;
641 struct rte_eth_dev *eth_dev;
642 struct virtio_hw *hw;
643 enum virtio_user_backend_type backend_type = VIRTIO_USER_BACKEND_UNKNOWN;
644 uint64_t queues = VIRTIO_USER_DEF_Q_NUM;
645 uint64_t cq = VIRTIO_USER_DEF_CQ_EN;
646 uint64_t queue_size = VIRTIO_USER_DEF_Q_SZ;
647 uint64_t server_mode = VIRTIO_USER_DEF_SERVER_MODE;
648 uint64_t mrg_rxbuf = 1;
649 uint64_t in_order = 1;
650 uint64_t packed_vq = 0;
651 uint64_t vectorized = 0;
654 char *mac_addr = NULL;
657 if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
658 const char *name = rte_vdev_device_name(dev);
659 eth_dev = rte_eth_dev_attach_secondary(name);
661 PMD_INIT_LOG(ERR, "Failed to probe %s", name);
665 if (eth_virtio_dev_init(eth_dev) < 0) {
666 PMD_INIT_LOG(ERR, "eth_virtio_dev_init fails");
667 rte_eth_dev_release_port(eth_dev);
671 eth_dev->dev_ops = &virtio_user_secondary_eth_dev_ops;
672 eth_dev->device = &dev->device;
673 rte_eth_dev_probing_finish(eth_dev);
677 kvlist = rte_kvargs_parse(rte_vdev_device_args(dev), valid_args);
679 PMD_INIT_LOG(ERR, "error when parsing param");
683 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_PATH) == 1) {
684 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_PATH,
685 &get_string_arg, &path) < 0) {
686 PMD_INIT_LOG(ERR, "error to parse %s",
687 VIRTIO_USER_ARG_PATH);
691 PMD_INIT_LOG(ERR, "arg %s is mandatory for virtio_user",
692 VIRTIO_USER_ARG_PATH);
696 backend_type = virtio_user_backend_type(path);
697 if (backend_type == VIRTIO_USER_BACKEND_UNKNOWN) {
699 "unable to determine backend type for path %s",
703 PMD_INIT_LOG(INFO, "Backend type detected: %s",
704 virtio_user_backend_strings[backend_type]);
706 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_INTERFACE_NAME) == 1) {
707 if (backend_type != VIRTIO_USER_BACKEND_VHOST_KERNEL) {
709 "arg %s applies only to vhost-kernel backend",
710 VIRTIO_USER_ARG_INTERFACE_NAME);
714 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_INTERFACE_NAME,
715 &get_string_arg, &ifname) < 0) {
716 PMD_INIT_LOG(ERR, "error to parse %s",
717 VIRTIO_USER_ARG_INTERFACE_NAME);
722 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_MAC) == 1) {
723 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_MAC,
724 &get_string_arg, &mac_addr) < 0) {
725 PMD_INIT_LOG(ERR, "error to parse %s",
726 VIRTIO_USER_ARG_MAC);
731 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_QUEUE_SIZE) == 1) {
732 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_QUEUE_SIZE,
733 &get_integer_arg, &queue_size) < 0) {
734 PMD_INIT_LOG(ERR, "error to parse %s",
735 VIRTIO_USER_ARG_QUEUE_SIZE);
740 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_QUEUES_NUM) == 1) {
741 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_QUEUES_NUM,
742 &get_integer_arg, &queues) < 0) {
743 PMD_INIT_LOG(ERR, "error to parse %s",
744 VIRTIO_USER_ARG_QUEUES_NUM);
749 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_SERVER_MODE) == 1) {
750 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_SERVER_MODE,
751 &get_integer_arg, &server_mode) < 0) {
752 PMD_INIT_LOG(ERR, "error to parse %s",
753 VIRTIO_USER_ARG_SERVER_MODE);
758 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_CQ_NUM) == 1) {
759 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_CQ_NUM,
760 &get_integer_arg, &cq) < 0) {
761 PMD_INIT_LOG(ERR, "error to parse %s",
762 VIRTIO_USER_ARG_CQ_NUM);
765 } else if (queues > 1) {
769 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_PACKED_VQ) == 1) {
770 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_PACKED_VQ,
771 &get_integer_arg, &packed_vq) < 0) {
772 PMD_INIT_LOG(ERR, "error to parse %s",
773 VIRTIO_USER_ARG_PACKED_VQ);
778 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_VECTORIZED) == 1) {
779 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_VECTORIZED,
780 &get_integer_arg, &vectorized) < 0) {
781 PMD_INIT_LOG(ERR, "error to parse %s",
782 VIRTIO_USER_ARG_VECTORIZED);
787 if (queues > 1 && cq == 0) {
788 PMD_INIT_LOG(ERR, "multi-q requires ctrl-q");
792 if (queues > VIRTIO_MAX_VIRTQUEUE_PAIRS) {
793 PMD_INIT_LOG(ERR, "arg %s %" PRIu64 " exceeds the limit %u",
794 VIRTIO_USER_ARG_QUEUES_NUM, queues,
795 VIRTIO_MAX_VIRTQUEUE_PAIRS);
799 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_MRG_RXBUF) == 1) {
800 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_MRG_RXBUF,
801 &get_integer_arg, &mrg_rxbuf) < 0) {
802 PMD_INIT_LOG(ERR, "error to parse %s",
803 VIRTIO_USER_ARG_MRG_RXBUF);
808 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_IN_ORDER) == 1) {
809 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_IN_ORDER,
810 &get_integer_arg, &in_order) < 0) {
811 PMD_INIT_LOG(ERR, "error to parse %s",
812 VIRTIO_USER_ARG_IN_ORDER);
817 eth_dev = virtio_user_eth_dev_alloc(dev);
819 PMD_INIT_LOG(ERR, "virtio_user fails to alloc device");
823 hw = eth_dev->data->dev_private;
824 if (virtio_user_dev_init(hw->virtio_user_dev, path, queues, cq,
825 queue_size, mac_addr, &ifname, server_mode,
826 mrg_rxbuf, in_order, packed_vq, backend_type) < 0) {
827 PMD_INIT_LOG(ERR, "virtio_user_dev_init fails");
828 virtio_user_eth_dev_free(eth_dev);
832 /* previously called by pci probing for physical dev */
833 if (eth_virtio_dev_init(eth_dev) < 0) {
834 PMD_INIT_LOG(ERR, "eth_virtio_dev_init fails");
835 virtio_user_eth_dev_free(eth_dev);
841 #if defined(CC_AVX512_SUPPORT)
846 "building environment do not support packed ring vectorized");
853 rte_eth_dev_probing_finish(eth_dev);
858 rte_kvargs_free(kvlist);
869 virtio_user_pmd_remove(struct rte_vdev_device *vdev)
872 struct rte_eth_dev *eth_dev;
877 name = rte_vdev_device_name(vdev);
878 PMD_DRV_LOG(INFO, "Un-Initializing %s", name);
879 eth_dev = rte_eth_dev_allocated(name);
880 /* Port has already been released by close. */
884 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
885 return rte_eth_dev_release_port(eth_dev);
887 /* make sure the device is stopped, queues freed */
888 return rte_eth_dev_close(eth_dev->data->port_id);
891 static int virtio_user_pmd_dma_map(struct rte_vdev_device *vdev, void *addr,
892 uint64_t iova, size_t len)
895 struct rte_eth_dev *eth_dev;
896 struct virtio_user_dev *dev;
897 struct virtio_hw *hw;
902 name = rte_vdev_device_name(vdev);
903 eth_dev = rte_eth_dev_allocated(name);
904 /* Port has already been released by close. */
908 hw = (struct virtio_hw *)eth_dev->data->dev_private;
909 dev = hw->virtio_user_dev;
911 if (dev->ops->dma_map)
912 return dev->ops->dma_map(dev, addr, iova, len);
917 static int virtio_user_pmd_dma_unmap(struct rte_vdev_device *vdev, void *addr,
918 uint64_t iova, size_t len)
921 struct rte_eth_dev *eth_dev;
922 struct virtio_user_dev *dev;
923 struct virtio_hw *hw;
928 name = rte_vdev_device_name(vdev);
929 eth_dev = rte_eth_dev_allocated(name);
930 /* Port has already been released by close. */
934 hw = (struct virtio_hw *)eth_dev->data->dev_private;
935 dev = hw->virtio_user_dev;
937 if (dev->ops->dma_unmap)
938 return dev->ops->dma_unmap(dev, addr, iova, len);
943 static struct rte_vdev_driver virtio_user_driver = {
944 .probe = virtio_user_pmd_probe,
945 .remove = virtio_user_pmd_remove,
946 .dma_map = virtio_user_pmd_dma_map,
947 .dma_unmap = virtio_user_pmd_dma_unmap,
950 RTE_PMD_REGISTER_VDEV(net_virtio_user, virtio_user_driver);
951 RTE_PMD_REGISTER_ALIAS(net_virtio_user, virtio_user);
952 RTE_PMD_REGISTER_PARAM_STRING(net_virtio_user,