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);
281 dev->status = status;
282 virtio_user_send_status_update(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_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) {
563 PMD_INIT_LOG(ERR, "Stat fails: %s (%s)\n", path,
565 return VIRTIO_USER_BACKEND_UNKNOWN;
568 if (S_ISSOCK(sb.st_mode)) {
569 return VIRTIO_USER_BACKEND_VHOST_USER;
570 } else if (S_ISCHR(sb.st_mode)) {
571 if (major(sb.st_rdev) == MISC_MAJOR)
572 return VIRTIO_USER_BACKEND_VHOST_KERNEL;
573 if (major(sb.st_rdev) == vdpa_dynamic_major_num())
574 return VIRTIO_USER_BACKEND_VHOST_VDPA;
576 return VIRTIO_USER_BACKEND_UNKNOWN;
579 static struct rte_eth_dev *
580 virtio_user_eth_dev_alloc(struct rte_vdev_device *vdev)
582 struct rte_eth_dev *eth_dev;
583 struct rte_eth_dev_data *data;
584 struct virtio_hw *hw;
585 struct virtio_user_dev *dev;
587 eth_dev = rte_eth_vdev_allocate(vdev, sizeof(*hw));
589 PMD_INIT_LOG(ERR, "cannot alloc rte_eth_dev");
593 data = eth_dev->data;
594 hw = eth_dev->data->dev_private;
596 dev = rte_zmalloc(NULL, sizeof(*dev), 0);
598 PMD_INIT_LOG(ERR, "malloc virtio_user_dev failed");
599 rte_eth_dev_release_port(eth_dev);
603 hw->port_id = data->port_id;
604 dev->port_id = data->port_id;
605 virtio_hw_internal[hw->port_id].vtpci_ops = &virtio_user_ops;
607 * MSIX is required to enable LSC (see virtio_init_device).
608 * Here just pretend that we support msix.
614 hw->use_inorder_rx = 0;
615 hw->use_inorder_tx = 0;
616 hw->virtio_user_dev = dev;
621 virtio_user_eth_dev_free(struct rte_eth_dev *eth_dev)
623 struct rte_eth_dev_data *data = eth_dev->data;
624 struct virtio_hw *hw = data->dev_private;
626 rte_free(hw->virtio_user_dev);
627 rte_eth_dev_release_port(eth_dev);
630 /* Dev initialization routine. Invoked once for each virtio vdev at
631 * EAL init time, see rte_bus_probe().
632 * Returns 0 on success.
635 virtio_user_pmd_probe(struct rte_vdev_device *dev)
637 struct rte_kvargs *kvlist = NULL;
638 struct rte_eth_dev *eth_dev;
639 struct virtio_hw *hw;
640 enum virtio_user_backend_type backend_type = VIRTIO_USER_BACKEND_UNKNOWN;
641 uint64_t queues = VIRTIO_USER_DEF_Q_NUM;
642 uint64_t cq = VIRTIO_USER_DEF_CQ_EN;
643 uint64_t queue_size = VIRTIO_USER_DEF_Q_SZ;
644 uint64_t server_mode = VIRTIO_USER_DEF_SERVER_MODE;
645 uint64_t mrg_rxbuf = 1;
646 uint64_t in_order = 1;
647 uint64_t packed_vq = 0;
648 uint64_t vectorized = 0;
651 char *mac_addr = NULL;
654 if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
655 const char *name = rte_vdev_device_name(dev);
656 eth_dev = rte_eth_dev_attach_secondary(name);
658 PMD_INIT_LOG(ERR, "Failed to probe %s", name);
662 if (eth_virtio_dev_init(eth_dev) < 0) {
663 PMD_INIT_LOG(ERR, "eth_virtio_dev_init fails");
664 rte_eth_dev_release_port(eth_dev);
668 eth_dev->dev_ops = &virtio_user_secondary_eth_dev_ops;
669 eth_dev->device = &dev->device;
670 rte_eth_dev_probing_finish(eth_dev);
674 kvlist = rte_kvargs_parse(rte_vdev_device_args(dev), valid_args);
676 PMD_INIT_LOG(ERR, "error when parsing param");
680 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_PATH) == 1) {
681 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_PATH,
682 &get_string_arg, &path) < 0) {
683 PMD_INIT_LOG(ERR, "error to parse %s",
684 VIRTIO_USER_ARG_PATH);
688 PMD_INIT_LOG(ERR, "arg %s is mandatory for virtio_user",
689 VIRTIO_USER_ARG_PATH);
693 backend_type = virtio_user_backend_type(path);
694 if (backend_type == VIRTIO_USER_BACKEND_UNKNOWN) {
696 "unable to determine backend type for path %s",
702 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_INTERFACE_NAME) == 1) {
703 if (backend_type != VIRTIO_USER_BACKEND_VHOST_KERNEL) {
705 "arg %s applies only to vhost-kernel backend",
706 VIRTIO_USER_ARG_INTERFACE_NAME);
710 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_INTERFACE_NAME,
711 &get_string_arg, &ifname) < 0) {
712 PMD_INIT_LOG(ERR, "error to parse %s",
713 VIRTIO_USER_ARG_INTERFACE_NAME);
718 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_MAC) == 1) {
719 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_MAC,
720 &get_string_arg, &mac_addr) < 0) {
721 PMD_INIT_LOG(ERR, "error to parse %s",
722 VIRTIO_USER_ARG_MAC);
727 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_QUEUE_SIZE) == 1) {
728 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_QUEUE_SIZE,
729 &get_integer_arg, &queue_size) < 0) {
730 PMD_INIT_LOG(ERR, "error to parse %s",
731 VIRTIO_USER_ARG_QUEUE_SIZE);
736 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_QUEUES_NUM) == 1) {
737 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_QUEUES_NUM,
738 &get_integer_arg, &queues) < 0) {
739 PMD_INIT_LOG(ERR, "error to parse %s",
740 VIRTIO_USER_ARG_QUEUES_NUM);
745 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_SERVER_MODE) == 1) {
746 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_SERVER_MODE,
747 &get_integer_arg, &server_mode) < 0) {
748 PMD_INIT_LOG(ERR, "error to parse %s",
749 VIRTIO_USER_ARG_SERVER_MODE);
754 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_CQ_NUM) == 1) {
755 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_CQ_NUM,
756 &get_integer_arg, &cq) < 0) {
757 PMD_INIT_LOG(ERR, "error to parse %s",
758 VIRTIO_USER_ARG_CQ_NUM);
761 } else if (queues > 1) {
765 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_PACKED_VQ) == 1) {
766 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_PACKED_VQ,
767 &get_integer_arg, &packed_vq) < 0) {
768 PMD_INIT_LOG(ERR, "error to parse %s",
769 VIRTIO_USER_ARG_PACKED_VQ);
774 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_VECTORIZED) == 1) {
775 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_VECTORIZED,
776 &get_integer_arg, &vectorized) < 0) {
777 PMD_INIT_LOG(ERR, "error to parse %s",
778 VIRTIO_USER_ARG_VECTORIZED);
783 if (queues > 1 && cq == 0) {
784 PMD_INIT_LOG(ERR, "multi-q requires ctrl-q");
788 if (queues > VIRTIO_MAX_VIRTQUEUE_PAIRS) {
789 PMD_INIT_LOG(ERR, "arg %s %" PRIu64 " exceeds the limit %u",
790 VIRTIO_USER_ARG_QUEUES_NUM, queues,
791 VIRTIO_MAX_VIRTQUEUE_PAIRS);
795 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_MRG_RXBUF) == 1) {
796 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_MRG_RXBUF,
797 &get_integer_arg, &mrg_rxbuf) < 0) {
798 PMD_INIT_LOG(ERR, "error to parse %s",
799 VIRTIO_USER_ARG_MRG_RXBUF);
804 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_IN_ORDER) == 1) {
805 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_IN_ORDER,
806 &get_integer_arg, &in_order) < 0) {
807 PMD_INIT_LOG(ERR, "error to parse %s",
808 VIRTIO_USER_ARG_IN_ORDER);
813 eth_dev = virtio_user_eth_dev_alloc(dev);
815 PMD_INIT_LOG(ERR, "virtio_user fails to alloc device");
819 hw = eth_dev->data->dev_private;
820 if (virtio_user_dev_init(hw->virtio_user_dev, path, queues, cq,
821 queue_size, mac_addr, &ifname, server_mode,
822 mrg_rxbuf, in_order, packed_vq, backend_type) < 0) {
823 PMD_INIT_LOG(ERR, "virtio_user_dev_init fails");
824 virtio_user_eth_dev_free(eth_dev);
828 /* previously called by pci probing for physical dev */
829 if (eth_virtio_dev_init(eth_dev) < 0) {
830 PMD_INIT_LOG(ERR, "eth_virtio_dev_init fails");
831 virtio_user_eth_dev_free(eth_dev);
837 #if defined(CC_AVX512_SUPPORT)
842 "building environment do not support packed ring vectorized");
849 rte_eth_dev_probing_finish(eth_dev);
854 rte_kvargs_free(kvlist);
865 virtio_user_pmd_remove(struct rte_vdev_device *vdev)
868 struct rte_eth_dev *eth_dev;
873 name = rte_vdev_device_name(vdev);
874 PMD_DRV_LOG(INFO, "Un-Initializing %s", name);
875 eth_dev = rte_eth_dev_allocated(name);
876 /* Port has already been released by close. */
880 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
881 return rte_eth_dev_release_port(eth_dev);
883 /* make sure the device is stopped, queues freed */
884 return rte_eth_dev_close(eth_dev->data->port_id);
887 static int virtio_user_pmd_dma_map(struct rte_vdev_device *vdev, void *addr,
888 uint64_t iova, size_t len)
891 struct rte_eth_dev *eth_dev;
892 struct virtio_user_dev *dev;
893 struct virtio_hw *hw;
898 name = rte_vdev_device_name(vdev);
899 eth_dev = rte_eth_dev_allocated(name);
900 /* Port has already been released by close. */
904 hw = (struct virtio_hw *)eth_dev->data->dev_private;
905 dev = hw->virtio_user_dev;
907 if (dev->ops->dma_map)
908 return dev->ops->dma_map(dev, addr, iova, len);
913 static int virtio_user_pmd_dma_unmap(struct rte_vdev_device *vdev, void *addr,
914 uint64_t iova, size_t len)
917 struct rte_eth_dev *eth_dev;
918 struct virtio_user_dev *dev;
919 struct virtio_hw *hw;
924 name = rte_vdev_device_name(vdev);
925 eth_dev = rte_eth_dev_allocated(name);
926 /* Port has already been released by close. */
930 hw = (struct virtio_hw *)eth_dev->data->dev_private;
931 dev = hw->virtio_user_dev;
933 if (dev->ops->dma_unmap)
934 return dev->ops->dma_unmap(dev, addr, iova, len);
939 static struct rte_vdev_driver virtio_user_driver = {
940 .probe = virtio_user_pmd_probe,
941 .remove = virtio_user_pmd_remove,
942 .dma_map = virtio_user_pmd_dma_map,
943 .dma_unmap = virtio_user_pmd_dma_unmap,
946 RTE_PMD_REGISTER_VDEV(net_virtio_user, virtio_user_driver);
947 RTE_PMD_REGISTER_ALIAS(net_virtio_user, virtio_user);
948 RTE_PMD_REGISTER_PARAM_STRING(net_virtio_user,