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 /* Offer VHOST_USER_PROTOCOL_F_STATUS */
96 dev->protocol_features |=
97 (1ULL << VHOST_USER_PROTOCOL_F_STATUS);
98 dev->protocol_features &= protocol_features;
100 if (dev->ops->send_request(dev,
101 VHOST_USER_SET_PROTOCOL_FEATURES,
102 &dev->protocol_features))
105 if (!(dev->protocol_features &
106 (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 (vtpci_packed_queue(hw) &&
119 (vtpci_get_status(hw) & 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 ret = virtio_user_start_device(dev);
129 if (dev->queue_pairs > 1) {
130 ret = virtio_user_handle_mq(dev, dev->queue_pairs);
132 PMD_INIT_LOG(ERR, "Fails to enable multi-queue pairs!");
136 if (eth_dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC) {
137 if (rte_intr_disable(eth_dev->intr_handle) < 0) {
138 PMD_DRV_LOG(ERR, "interrupt disable failed");
141 rte_intr_callback_unregister(eth_dev->intr_handle,
142 virtio_interrupt_handler,
144 eth_dev->intr_handle->fd = connectfd;
145 rte_intr_callback_register(eth_dev->intr_handle,
146 virtio_interrupt_handler, eth_dev);
148 if (rte_intr_enable(eth_dev->intr_handle) < 0) {
149 PMD_DRV_LOG(ERR, "interrupt enable failed");
153 PMD_INIT_LOG(NOTICE, "server mode virtio-user reconnection succeeds!");
158 virtio_user_delayed_handler(void *param)
160 struct virtio_hw *hw = (struct virtio_hw *)param;
161 struct rte_eth_dev *eth_dev = &rte_eth_devices[hw->port_id];
162 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
164 if (rte_intr_disable(eth_dev->intr_handle) < 0) {
165 PMD_DRV_LOG(ERR, "interrupt disable failed");
168 rte_intr_callback_unregister(eth_dev->intr_handle,
169 virtio_interrupt_handler, eth_dev);
170 if (dev->is_server) {
171 if (dev->vhostfd >= 0) {
174 /* Until the featuers are negotiated again, don't assume
175 * the backend supports VHOST_USER_PROTOCOL_F_STATUS
177 dev->protocol_features &=
178 ~(1ULL << VHOST_USER_PROTOCOL_F_STATUS);
180 eth_dev->intr_handle->fd = dev->listenfd;
181 rte_intr_callback_register(eth_dev->intr_handle,
182 virtio_interrupt_handler, eth_dev);
183 if (rte_intr_enable(eth_dev->intr_handle) < 0) {
184 PMD_DRV_LOG(ERR, "interrupt enable failed");
191 virtio_user_read_dev_config(struct virtio_hw *hw, size_t offset,
192 void *dst, int length)
195 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
197 if (offset == offsetof(struct virtio_net_config, mac) &&
198 length == RTE_ETHER_ADDR_LEN) {
199 for (i = 0; i < RTE_ETHER_ADDR_LEN; ++i)
200 ((uint8_t *)dst)[i] = dev->mac_addr[i];
204 if (offset == offsetof(struct virtio_net_config, status)) {
207 if (dev->vhostfd >= 0) {
211 flags = fcntl(dev->vhostfd, F_GETFL);
212 if (fcntl(dev->vhostfd, F_SETFL,
213 flags | O_NONBLOCK) == -1) {
214 PMD_DRV_LOG(ERR, "error setting O_NONBLOCK flag");
217 r = recv(dev->vhostfd, buf, 128, MSG_PEEK);
218 if (r == 0 || (r < 0 && errno != EAGAIN)) {
219 dev->net_status &= (~VIRTIO_NET_S_LINK_UP);
220 PMD_DRV_LOG(ERR, "virtio-user port %u is down",
223 /* This function could be called in the process
224 * of interrupt handling, callback cannot be
225 * unregistered here, set an alarm to do it.
228 virtio_user_delayed_handler,
231 dev->net_status |= VIRTIO_NET_S_LINK_UP;
233 if (fcntl(dev->vhostfd, F_SETFL,
234 flags & ~O_NONBLOCK) == -1) {
235 PMD_DRV_LOG(ERR, "error clearing O_NONBLOCK flag");
238 } else if (dev->is_server) {
239 dev->net_status &= (~VIRTIO_NET_S_LINK_UP);
240 if (virtio_user_server_reconnect(dev) >= 0)
241 dev->net_status |= VIRTIO_NET_S_LINK_UP;
244 *(uint16_t *)dst = dev->net_status;
247 if (offset == offsetof(struct virtio_net_config, max_virtqueue_pairs))
248 *(uint16_t *)dst = dev->max_queue_pairs;
252 virtio_user_write_dev_config(struct virtio_hw *hw, size_t offset,
253 const void *src, int length)
256 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
258 if ((offset == offsetof(struct virtio_net_config, mac)) &&
259 (length == RTE_ETHER_ADDR_LEN))
260 for (i = 0; i < RTE_ETHER_ADDR_LEN; ++i)
261 dev->mac_addr[i] = ((const uint8_t *)src)[i];
263 PMD_DRV_LOG(ERR, "not supported offset=%zu, len=%d",
268 virtio_user_reset(struct virtio_hw *hw)
270 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
272 if (dev->status & VIRTIO_CONFIG_STATUS_DRIVER_OK)
273 virtio_user_stop_device(dev);
277 virtio_user_set_status(struct virtio_hw *hw, uint8_t status)
279 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
280 uint8_t old_status = dev->status;
282 if (status & VIRTIO_CONFIG_STATUS_FEATURES_OK &&
283 ~old_status & VIRTIO_CONFIG_STATUS_FEATURES_OK)
284 virtio_user_dev_set_features(dev);
285 if (status & VIRTIO_CONFIG_STATUS_DRIVER_OK)
286 virtio_user_start_device(dev);
287 else if (status == VIRTIO_CONFIG_STATUS_RESET)
288 virtio_user_reset(hw);
290 virtio_user_dev_set_status(dev, status);
294 virtio_user_get_status(struct virtio_hw *hw)
296 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
298 virtio_user_dev_update_status(dev);
304 virtio_user_get_features(struct virtio_hw *hw)
306 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
308 /* unmask feature bits defined in vhost user protocol */
309 return dev->device_features & VIRTIO_PMD_SUPPORTED_GUEST_FEATURES;
313 virtio_user_set_features(struct virtio_hw *hw, uint64_t features)
315 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
317 dev->features = features & dev->device_features;
321 virtio_user_get_isr(struct virtio_hw *hw __rte_unused)
323 /* rxq interrupts and config interrupt are separated in virtio-user,
324 * here we only report config change.
326 return VIRTIO_PCI_ISR_CONFIG;
330 virtio_user_set_config_irq(struct virtio_hw *hw __rte_unused,
331 uint16_t vec __rte_unused)
337 virtio_user_set_queue_irq(struct virtio_hw *hw __rte_unused,
338 struct virtqueue *vq __rte_unused,
341 /* pretend we have done that */
345 /* This function is to get the queue size, aka, number of descs, of a specified
346 * queue. Different with the VHOST_USER_GET_QUEUE_NUM, which is used to get the
347 * max supported queues.
350 virtio_user_get_queue_num(struct virtio_hw *hw, uint16_t queue_id __rte_unused)
352 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
354 /* Currently, each queue has same queue size */
355 return dev->queue_size;
359 virtio_user_setup_queue_packed(struct virtqueue *vq,
360 struct virtio_user_dev *dev)
362 uint16_t queue_idx = vq->vq_queue_index;
363 struct vring_packed *vring;
369 vring = &dev->packed_vrings[queue_idx];
370 desc_addr = (uintptr_t)vq->vq_ring_virt_mem;
371 avail_addr = desc_addr + vq->vq_nentries *
372 sizeof(struct vring_packed_desc);
373 used_addr = RTE_ALIGN_CEIL(avail_addr +
374 sizeof(struct vring_packed_desc_event),
375 VIRTIO_PCI_VRING_ALIGN);
376 vring->num = vq->vq_nentries;
377 vring->desc = (void *)(uintptr_t)desc_addr;
378 vring->driver = (void *)(uintptr_t)avail_addr;
379 vring->device = (void *)(uintptr_t)used_addr;
380 dev->packed_queues[queue_idx].avail_wrap_counter = true;
381 dev->packed_queues[queue_idx].used_wrap_counter = true;
383 for (i = 0; i < vring->num; i++)
384 vring->desc[i].flags = 0;
388 virtio_user_setup_queue_split(struct virtqueue *vq, struct virtio_user_dev *dev)
390 uint16_t queue_idx = vq->vq_queue_index;
391 uint64_t desc_addr, avail_addr, used_addr;
393 desc_addr = (uintptr_t)vq->vq_ring_virt_mem;
394 avail_addr = desc_addr + vq->vq_nentries * sizeof(struct vring_desc);
395 used_addr = RTE_ALIGN_CEIL(avail_addr + offsetof(struct vring_avail,
396 ring[vq->vq_nentries]),
397 VIRTIO_PCI_VRING_ALIGN);
399 dev->vrings[queue_idx].num = vq->vq_nentries;
400 dev->vrings[queue_idx].desc = (void *)(uintptr_t)desc_addr;
401 dev->vrings[queue_idx].avail = (void *)(uintptr_t)avail_addr;
402 dev->vrings[queue_idx].used = (void *)(uintptr_t)used_addr;
406 virtio_user_setup_queue(struct virtio_hw *hw, struct virtqueue *vq)
408 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
410 if (vtpci_packed_queue(hw))
411 virtio_user_setup_queue_packed(vq, dev);
413 virtio_user_setup_queue_split(vq, dev);
419 virtio_user_del_queue(struct virtio_hw *hw, struct virtqueue *vq)
421 /* For legacy devices, write 0 to VIRTIO_PCI_QUEUE_PFN port, QEMU
422 * correspondingly stops the ioeventfds, and reset the status of
424 * For modern devices, set queue desc, avail, used in PCI bar to 0,
425 * not see any more behavior in QEMU.
427 * Here we just care about what information to deliver to vhost-user
428 * or vhost-kernel. So we just close ioeventfd for now.
430 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
432 close(dev->callfds[vq->vq_queue_index]);
433 close(dev->kickfds[vq->vq_queue_index]);
437 virtio_user_notify_queue(struct virtio_hw *hw, struct virtqueue *vq)
440 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
442 if (hw->cvq && (hw->cvq->vq == vq)) {
443 if (vtpci_packed_queue(vq->hw))
444 virtio_user_handle_cq_packed(dev, vq->vq_queue_index);
446 virtio_user_handle_cq(dev, vq->vq_queue_index);
450 if (write(dev->kickfds[vq->vq_queue_index], &buf, sizeof(buf)) < 0)
451 PMD_DRV_LOG(ERR, "failed to kick backend: %s",
455 const struct virtio_pci_ops virtio_user_ops = {
456 .read_dev_cfg = virtio_user_read_dev_config,
457 .write_dev_cfg = virtio_user_write_dev_config,
458 .get_status = virtio_user_get_status,
459 .set_status = virtio_user_set_status,
460 .get_features = virtio_user_get_features,
461 .set_features = virtio_user_set_features,
462 .get_isr = virtio_user_get_isr,
463 .set_config_irq = virtio_user_set_config_irq,
464 .set_queue_irq = virtio_user_set_queue_irq,
465 .get_queue_num = virtio_user_get_queue_num,
466 .setup_queue = virtio_user_setup_queue,
467 .del_queue = virtio_user_del_queue,
468 .notify_queue = virtio_user_notify_queue,
471 static const char *valid_args[] = {
472 #define VIRTIO_USER_ARG_QUEUES_NUM "queues"
473 VIRTIO_USER_ARG_QUEUES_NUM,
474 #define VIRTIO_USER_ARG_CQ_NUM "cq"
475 VIRTIO_USER_ARG_CQ_NUM,
476 #define VIRTIO_USER_ARG_MAC "mac"
478 #define VIRTIO_USER_ARG_PATH "path"
479 VIRTIO_USER_ARG_PATH,
480 #define VIRTIO_USER_ARG_QUEUE_SIZE "queue_size"
481 VIRTIO_USER_ARG_QUEUE_SIZE,
482 #define VIRTIO_USER_ARG_INTERFACE_NAME "iface"
483 VIRTIO_USER_ARG_INTERFACE_NAME,
484 #define VIRTIO_USER_ARG_SERVER_MODE "server"
485 VIRTIO_USER_ARG_SERVER_MODE,
486 #define VIRTIO_USER_ARG_MRG_RXBUF "mrg_rxbuf"
487 VIRTIO_USER_ARG_MRG_RXBUF,
488 #define VIRTIO_USER_ARG_IN_ORDER "in_order"
489 VIRTIO_USER_ARG_IN_ORDER,
490 #define VIRTIO_USER_ARG_PACKED_VQ "packed_vq"
491 VIRTIO_USER_ARG_PACKED_VQ,
492 #define VIRTIO_USER_ARG_SPEED "speed"
493 VIRTIO_USER_ARG_SPEED,
494 #define VIRTIO_USER_ARG_VECTORIZED "vectorized"
495 VIRTIO_USER_ARG_VECTORIZED,
499 #define VIRTIO_USER_DEF_CQ_EN 0
500 #define VIRTIO_USER_DEF_Q_NUM 1
501 #define VIRTIO_USER_DEF_Q_SZ 256
502 #define VIRTIO_USER_DEF_SERVER_MODE 0
505 get_string_arg(const char *key __rte_unused,
506 const char *value, void *extra_args)
508 if (!value || !extra_args)
511 *(char **)extra_args = strdup(value);
513 if (!*(char **)extra_args)
520 get_integer_arg(const char *key __rte_unused,
521 const char *value, void *extra_args)
523 uint64_t integer = 0;
524 if (!value || !extra_args)
527 integer = strtoull(value, NULL, 0);
528 /* extra_args keeps default value, it should be replaced
529 * only in case of successful parsing of the 'value' arg
532 *(uint64_t *)extra_args = integer;
537 vdpa_dynamic_major_num(void)
546 fp = fopen("/proc/devices", "r");
548 PMD_INIT_LOG(ERR, "Cannot open /proc/devices: %s",
550 return UNNAMED_MAJOR;
553 while (getline(&line, &size, fp) > 0) {
554 char *stripped = line + strspn(line, " ");
555 if ((sscanf(stripped, "%u %10s", &num, name) == 2) &&
556 (strncmp(name, "vhost-vdpa", 10) == 0)) {
562 return found ? num : UNNAMED_MAJOR;
565 static enum virtio_user_backend_type
566 virtio_user_backend_type(const char *path)
570 if (stat(path, &sb) == -1) {
572 return VIRTIO_USER_BACKEND_VHOST_USER;
574 PMD_INIT_LOG(ERR, "Stat fails: %s (%s)\n", path,
576 return VIRTIO_USER_BACKEND_UNKNOWN;
579 if (S_ISSOCK(sb.st_mode)) {
580 return VIRTIO_USER_BACKEND_VHOST_USER;
581 } else if (S_ISCHR(sb.st_mode)) {
582 if (major(sb.st_rdev) == MISC_MAJOR)
583 return VIRTIO_USER_BACKEND_VHOST_KERNEL;
584 if (major(sb.st_rdev) == vdpa_dynamic_major_num())
585 return VIRTIO_USER_BACKEND_VHOST_VDPA;
587 return VIRTIO_USER_BACKEND_UNKNOWN;
590 static struct rte_eth_dev *
591 virtio_user_eth_dev_alloc(struct rte_vdev_device *vdev)
593 struct rte_eth_dev *eth_dev;
594 struct rte_eth_dev_data *data;
595 struct virtio_hw *hw;
596 struct virtio_user_dev *dev;
598 eth_dev = rte_eth_vdev_allocate(vdev, sizeof(*hw));
600 PMD_INIT_LOG(ERR, "cannot alloc rte_eth_dev");
604 data = eth_dev->data;
605 hw = eth_dev->data->dev_private;
607 dev = rte_zmalloc(NULL, sizeof(*dev), 0);
609 PMD_INIT_LOG(ERR, "malloc virtio_user_dev failed");
610 rte_eth_dev_release_port(eth_dev);
614 hw->port_id = data->port_id;
615 dev->port_id = data->port_id;
616 virtio_hw_internal[hw->port_id].vtpci_ops = &virtio_user_ops;
618 * MSIX is required to enable LSC (see virtio_init_device).
619 * Here just pretend that we support msix.
625 hw->use_inorder_rx = 0;
626 hw->use_inorder_tx = 0;
627 hw->virtio_user_dev = dev;
632 virtio_user_eth_dev_free(struct rte_eth_dev *eth_dev)
634 struct rte_eth_dev_data *data = eth_dev->data;
635 struct virtio_hw *hw = data->dev_private;
637 rte_free(hw->virtio_user_dev);
638 rte_eth_dev_release_port(eth_dev);
641 /* Dev initialization routine. Invoked once for each virtio vdev at
642 * EAL init time, see rte_bus_probe().
643 * Returns 0 on success.
646 virtio_user_pmd_probe(struct rte_vdev_device *dev)
648 struct rte_kvargs *kvlist = NULL;
649 struct rte_eth_dev *eth_dev;
650 struct virtio_hw *hw;
651 enum virtio_user_backend_type backend_type = VIRTIO_USER_BACKEND_UNKNOWN;
652 uint64_t queues = VIRTIO_USER_DEF_Q_NUM;
653 uint64_t cq = VIRTIO_USER_DEF_CQ_EN;
654 uint64_t queue_size = VIRTIO_USER_DEF_Q_SZ;
655 uint64_t server_mode = VIRTIO_USER_DEF_SERVER_MODE;
656 uint64_t mrg_rxbuf = 1;
657 uint64_t in_order = 1;
658 uint64_t packed_vq = 0;
659 uint64_t vectorized = 0;
662 char *mac_addr = NULL;
665 if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
666 const char *name = rte_vdev_device_name(dev);
667 eth_dev = rte_eth_dev_attach_secondary(name);
669 PMD_INIT_LOG(ERR, "Failed to probe %s", name);
673 if (eth_virtio_dev_init(eth_dev) < 0) {
674 PMD_INIT_LOG(ERR, "eth_virtio_dev_init fails");
675 rte_eth_dev_release_port(eth_dev);
679 eth_dev->dev_ops = &virtio_user_secondary_eth_dev_ops;
680 eth_dev->device = &dev->device;
681 rte_eth_dev_probing_finish(eth_dev);
685 kvlist = rte_kvargs_parse(rte_vdev_device_args(dev), valid_args);
687 PMD_INIT_LOG(ERR, "error when parsing param");
691 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_PATH) == 1) {
692 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_PATH,
693 &get_string_arg, &path) < 0) {
694 PMD_INIT_LOG(ERR, "error to parse %s",
695 VIRTIO_USER_ARG_PATH);
699 PMD_INIT_LOG(ERR, "arg %s is mandatory for virtio_user",
700 VIRTIO_USER_ARG_PATH);
704 backend_type = virtio_user_backend_type(path);
705 if (backend_type == VIRTIO_USER_BACKEND_UNKNOWN) {
707 "unable to determine backend type for path %s",
711 PMD_INIT_LOG(INFO, "Backend type detected: %s",
712 virtio_user_backend_strings[backend_type]);
714 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_INTERFACE_NAME) == 1) {
715 if (backend_type != VIRTIO_USER_BACKEND_VHOST_KERNEL) {
717 "arg %s applies only to vhost-kernel backend",
718 VIRTIO_USER_ARG_INTERFACE_NAME);
722 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_INTERFACE_NAME,
723 &get_string_arg, &ifname) < 0) {
724 PMD_INIT_LOG(ERR, "error to parse %s",
725 VIRTIO_USER_ARG_INTERFACE_NAME);
730 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_MAC) == 1) {
731 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_MAC,
732 &get_string_arg, &mac_addr) < 0) {
733 PMD_INIT_LOG(ERR, "error to parse %s",
734 VIRTIO_USER_ARG_MAC);
739 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_QUEUE_SIZE) == 1) {
740 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_QUEUE_SIZE,
741 &get_integer_arg, &queue_size) < 0) {
742 PMD_INIT_LOG(ERR, "error to parse %s",
743 VIRTIO_USER_ARG_QUEUE_SIZE);
748 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_QUEUES_NUM) == 1) {
749 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_QUEUES_NUM,
750 &get_integer_arg, &queues) < 0) {
751 PMD_INIT_LOG(ERR, "error to parse %s",
752 VIRTIO_USER_ARG_QUEUES_NUM);
757 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_SERVER_MODE) == 1) {
758 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_SERVER_MODE,
759 &get_integer_arg, &server_mode) < 0) {
760 PMD_INIT_LOG(ERR, "error to parse %s",
761 VIRTIO_USER_ARG_SERVER_MODE);
766 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_CQ_NUM) == 1) {
767 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_CQ_NUM,
768 &get_integer_arg, &cq) < 0) {
769 PMD_INIT_LOG(ERR, "error to parse %s",
770 VIRTIO_USER_ARG_CQ_NUM);
773 } else if (queues > 1) {
777 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_PACKED_VQ) == 1) {
778 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_PACKED_VQ,
779 &get_integer_arg, &packed_vq) < 0) {
780 PMD_INIT_LOG(ERR, "error to parse %s",
781 VIRTIO_USER_ARG_PACKED_VQ);
786 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_VECTORIZED) == 1) {
787 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_VECTORIZED,
788 &get_integer_arg, &vectorized) < 0) {
789 PMD_INIT_LOG(ERR, "error to parse %s",
790 VIRTIO_USER_ARG_VECTORIZED);
795 if (queues > 1 && cq == 0) {
796 PMD_INIT_LOG(ERR, "multi-q requires ctrl-q");
800 if (queues > VIRTIO_MAX_VIRTQUEUE_PAIRS) {
801 PMD_INIT_LOG(ERR, "arg %s %" PRIu64 " exceeds the limit %u",
802 VIRTIO_USER_ARG_QUEUES_NUM, queues,
803 VIRTIO_MAX_VIRTQUEUE_PAIRS);
807 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_MRG_RXBUF) == 1) {
808 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_MRG_RXBUF,
809 &get_integer_arg, &mrg_rxbuf) < 0) {
810 PMD_INIT_LOG(ERR, "error to parse %s",
811 VIRTIO_USER_ARG_MRG_RXBUF);
816 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_IN_ORDER) == 1) {
817 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_IN_ORDER,
818 &get_integer_arg, &in_order) < 0) {
819 PMD_INIT_LOG(ERR, "error to parse %s",
820 VIRTIO_USER_ARG_IN_ORDER);
825 eth_dev = virtio_user_eth_dev_alloc(dev);
827 PMD_INIT_LOG(ERR, "virtio_user fails to alloc device");
831 hw = eth_dev->data->dev_private;
832 if (virtio_user_dev_init(hw->virtio_user_dev, path, queues, cq,
833 queue_size, mac_addr, &ifname, server_mode,
834 mrg_rxbuf, in_order, packed_vq, backend_type) < 0) {
835 PMD_INIT_LOG(ERR, "virtio_user_dev_init fails");
836 virtio_user_eth_dev_free(eth_dev);
840 /* previously called by pci probing for physical dev */
841 if (eth_virtio_dev_init(eth_dev) < 0) {
842 PMD_INIT_LOG(ERR, "eth_virtio_dev_init fails");
843 virtio_user_eth_dev_free(eth_dev);
849 #if defined(CC_AVX512_SUPPORT)
854 "building environment do not support packed ring vectorized");
861 rte_eth_dev_probing_finish(eth_dev);
866 rte_kvargs_free(kvlist);
877 virtio_user_pmd_remove(struct rte_vdev_device *vdev)
880 struct rte_eth_dev *eth_dev;
885 name = rte_vdev_device_name(vdev);
886 PMD_DRV_LOG(INFO, "Un-Initializing %s", name);
887 eth_dev = rte_eth_dev_allocated(name);
888 /* Port has already been released by close. */
892 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
893 return rte_eth_dev_release_port(eth_dev);
895 /* make sure the device is stopped, queues freed */
896 return rte_eth_dev_close(eth_dev->data->port_id);
899 static int virtio_user_pmd_dma_map(struct rte_vdev_device *vdev, void *addr,
900 uint64_t iova, size_t len)
903 struct rte_eth_dev *eth_dev;
904 struct virtio_user_dev *dev;
905 struct virtio_hw *hw;
910 name = rte_vdev_device_name(vdev);
911 eth_dev = rte_eth_dev_allocated(name);
912 /* Port has already been released by close. */
916 hw = (struct virtio_hw *)eth_dev->data->dev_private;
917 dev = hw->virtio_user_dev;
919 if (dev->ops->dma_map)
920 return dev->ops->dma_map(dev, addr, iova, len);
925 static int virtio_user_pmd_dma_unmap(struct rte_vdev_device *vdev, void *addr,
926 uint64_t iova, size_t len)
929 struct rte_eth_dev *eth_dev;
930 struct virtio_user_dev *dev;
931 struct virtio_hw *hw;
936 name = rte_vdev_device_name(vdev);
937 eth_dev = rte_eth_dev_allocated(name);
938 /* Port has already been released by close. */
942 hw = (struct virtio_hw *)eth_dev->data->dev_private;
943 dev = hw->virtio_user_dev;
945 if (dev->ops->dma_unmap)
946 return dev->ops->dma_unmap(dev, addr, iova, len);
951 static struct rte_vdev_driver virtio_user_driver = {
952 .probe = virtio_user_pmd_probe,
953 .remove = virtio_user_pmd_remove,
954 .dma_map = virtio_user_pmd_dma_map,
955 .dma_unmap = virtio_user_pmd_dma_unmap,
958 RTE_PMD_REGISTER_VDEV(net_virtio_user, virtio_user_driver);
959 RTE_PMD_REGISTER_ALIAS(net_virtio_user, virtio_user);
960 RTE_PMD_REGISTER_PARAM_STRING(net_virtio_user,