1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2016 Intel Corporation
12 #include <sys/eventfd.h>
13 #include <sys/types.h>
16 #include <rte_alarm.h>
17 #include <rte_string_fns.h>
18 #include <rte_eal_memconfig.h>
21 #include "virtio_user_dev.h"
22 #include "../virtio_ethdev.h"
24 #define VIRTIO_USER_MEM_EVENT_CLB_NAME "virtio_user_mem_event_clb"
26 const char * const virtio_user_backend_strings[] = {
27 [VIRTIO_USER_BACKEND_UNKNOWN] = "VIRTIO_USER_BACKEND_UNKNOWN",
28 [VIRTIO_USER_BACKEND_VHOST_USER] = "VHOST_USER",
29 [VIRTIO_USER_BACKEND_VHOST_KERNEL] = "VHOST_NET",
30 [VIRTIO_USER_BACKEND_VHOST_VDPA] = "VHOST_VDPA",
34 virtio_user_create_queue(struct virtio_user_dev *dev, uint32_t queue_sel)
36 /* Of all per virtqueue MSGs, make sure VHOST_SET_VRING_CALL come
37 * firstly because vhost depends on this msg to allocate virtqueue
40 struct vhost_vring_file file;
43 file.index = queue_sel;
44 file.fd = dev->callfds[queue_sel];
45 ret = dev->ops->set_vring_call(dev, &file);
47 PMD_INIT_LOG(ERR, "(%s) Failed to create queue %u\n", dev->path, queue_sel);
55 virtio_user_kick_queue(struct virtio_user_dev *dev, uint32_t queue_sel)
58 struct vhost_vring_file file;
59 struct vhost_vring_state state;
60 struct vring *vring = &dev->vrings[queue_sel];
61 struct vring_packed *pq_vring = &dev->packed_vrings[queue_sel];
62 struct vhost_vring_addr addr = {
65 .flags = 0, /* disable log */
68 if (dev->features & (1ULL << VIRTIO_F_RING_PACKED)) {
70 (uint64_t)(uintptr_t)pq_vring->desc;
71 addr.avail_user_addr =
72 (uint64_t)(uintptr_t)pq_vring->driver;
74 (uint64_t)(uintptr_t)pq_vring->device;
76 addr.desc_user_addr = (uint64_t)(uintptr_t)vring->desc;
77 addr.avail_user_addr = (uint64_t)(uintptr_t)vring->avail;
78 addr.used_user_addr = (uint64_t)(uintptr_t)vring->used;
81 state.index = queue_sel;
82 state.num = vring->num;
83 ret = dev->ops->set_vring_num(dev, &state);
87 state.index = queue_sel;
88 state.num = 0; /* no reservation */
89 if (dev->features & (1ULL << VIRTIO_F_RING_PACKED))
90 state.num |= (1 << 15);
91 ret = dev->ops->set_vring_base(dev, &state);
95 ret = dev->ops->set_vring_addr(dev, &addr);
99 /* Of all per virtqueue MSGs, make sure VHOST_USER_SET_VRING_KICK comes
100 * lastly because vhost depends on this msg to judge if
103 file.index = queue_sel;
104 file.fd = dev->kickfds[queue_sel];
105 ret = dev->ops->set_vring_kick(dev, &file);
111 PMD_INIT_LOG(ERR, "(%s) Failed to kick queue %u\n", dev->path, queue_sel);
117 virtio_user_queue_setup(struct virtio_user_dev *dev,
118 int (*fn)(struct virtio_user_dev *, uint32_t))
120 uint32_t i, queue_sel;
122 for (i = 0; i < dev->max_queue_pairs; ++i) {
123 queue_sel = 2 * i + VTNET_SQ_RQ_QUEUE_IDX;
124 if (fn(dev, queue_sel) < 0) {
125 PMD_DRV_LOG(ERR, "(%s) setup rx vq %u failed", dev->path, i);
129 for (i = 0; i < dev->max_queue_pairs; ++i) {
130 queue_sel = 2 * i + VTNET_SQ_TQ_QUEUE_IDX;
131 if (fn(dev, queue_sel) < 0) {
132 PMD_DRV_LOG(INFO, "(%s) setup tx vq %u failed", dev->path, i);
141 virtio_user_dev_set_features(struct virtio_user_dev *dev)
146 pthread_mutex_lock(&dev->mutex);
148 /* Step 0: tell vhost to create queues */
149 if (virtio_user_queue_setup(dev, virtio_user_create_queue) < 0)
152 features = dev->features;
154 /* Strip VIRTIO_NET_F_MAC, as MAC address is handled in vdev init */
155 features &= ~(1ull << VIRTIO_NET_F_MAC);
156 /* Strip VIRTIO_NET_F_CTRL_VQ, as devices do not really need to know */
157 features &= ~(1ull << VIRTIO_NET_F_CTRL_VQ);
158 features &= ~(1ull << VIRTIO_NET_F_STATUS);
159 ret = dev->ops->set_features(dev, features);
162 PMD_DRV_LOG(INFO, "(%s) set features: 0x%" PRIx64, dev->path, features);
164 pthread_mutex_unlock(&dev->mutex);
170 virtio_user_start_device(struct virtio_user_dev *dev)
177 * We need to make sure that the locks will be
178 * taken in the correct order to avoid deadlocks.
180 * Before releasing this lock, this thread should
181 * not trigger any memory hotplug events.
183 * This is a temporary workaround, and should be
184 * replaced when we get proper supports from the
185 * memory subsystem in the future.
187 rte_mcfg_mem_read_lock();
188 pthread_mutex_lock(&dev->mutex);
190 /* Step 2: share memory regions */
191 ret = dev->ops->set_memory_table(dev);
195 /* Step 3: kick queues */
196 ret = virtio_user_queue_setup(dev, virtio_user_kick_queue);
200 /* Step 4: enable queues
201 * we enable the 1st queue pair by default.
203 ret = dev->ops->enable_qp(dev, 0, 1);
209 pthread_mutex_unlock(&dev->mutex);
210 rte_mcfg_mem_read_unlock();
214 pthread_mutex_unlock(&dev->mutex);
215 rte_mcfg_mem_read_unlock();
217 PMD_INIT_LOG(ERR, "(%s) Failed to start device\n", dev->path);
219 /* TODO: free resource here or caller to check */
223 int virtio_user_stop_device(struct virtio_user_dev *dev)
225 struct vhost_vring_state state;
229 pthread_mutex_lock(&dev->mutex);
233 for (i = 0; i < dev->max_queue_pairs; ++i) {
234 ret = dev->ops->enable_qp(dev, i, 0);
239 /* Stop the backend. */
240 for (i = 0; i < dev->max_queue_pairs * 2; ++i) {
242 ret = dev->ops->get_vring_base(dev, &state);
244 PMD_DRV_LOG(ERR, "(%s) get_vring_base failed, index=%u", dev->path, i);
249 dev->started = false;
252 pthread_mutex_unlock(&dev->mutex);
256 pthread_mutex_unlock(&dev->mutex);
258 PMD_INIT_LOG(ERR, "(%s) Failed to stop device\n", dev->path);
264 virtio_user_dev_set_mac(struct virtio_user_dev *dev)
268 if (!(dev->device_features & (1ULL << VIRTIO_NET_F_MAC)))
271 if (!dev->ops->set_config)
274 ret = dev->ops->set_config(dev, dev->mac_addr,
275 offsetof(struct virtio_net_config, mac),
278 PMD_DRV_LOG(ERR, "(%s) Failed to set MAC address in device", dev->path);
284 virtio_user_dev_get_mac(struct virtio_user_dev *dev)
288 if (!(dev->device_features & (1ULL << VIRTIO_NET_F_MAC)))
291 if (!dev->ops->get_config)
294 ret = dev->ops->get_config(dev, dev->mac_addr,
295 offsetof(struct virtio_net_config, mac),
298 PMD_DRV_LOG(ERR, "(%s) Failed to get MAC address from device", dev->path);
304 virtio_user_dev_init_mac(struct virtio_user_dev *dev, const char *mac)
306 struct rte_ether_addr cmdline_mac;
307 char buf[RTE_ETHER_ADDR_FMT_SIZE];
310 if (mac && rte_ether_unformat_addr(mac, &cmdline_mac) == 0) {
312 * MAC address was passed from command-line, try to store
313 * it in the device if it supports it. Otherwise try to use
316 memcpy(dev->mac_addr, &cmdline_mac, RTE_ETHER_ADDR_LEN);
317 dev->mac_specified = 1;
319 /* Setting MAC may fail, continue to get the device one in this case */
320 virtio_user_dev_set_mac(dev);
321 ret = virtio_user_dev_get_mac(dev);
325 if (memcmp(&cmdline_mac, dev->mac_addr, RTE_ETHER_ADDR_LEN))
326 PMD_DRV_LOG(INFO, "(%s) Device MAC update failed", dev->path);
328 ret = virtio_user_dev_get_mac(dev);
330 PMD_DRV_LOG(ERR, "(%s) No valid MAC in devargs or device, use random",
335 dev->mac_specified = 1;
338 rte_ether_format_addr(buf, RTE_ETHER_ADDR_FMT_SIZE,
339 (struct rte_ether_addr *)dev->mac_addr);
340 PMD_DRV_LOG(INFO, "(%s) MAC %s specified", dev->path, buf);
344 virtio_user_dev_init_notify(struct virtio_user_dev *dev)
350 for (i = 0; i < dev->max_queue_pairs * 2; i++) {
351 /* May use invalid flag, but some backend uses kickfd and
352 * callfd as criteria to judge if dev is alive. so finally we
355 callfd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);
357 PMD_DRV_LOG(ERR, "(%s) callfd error, %s", dev->path, strerror(errno));
360 kickfd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);
363 PMD_DRV_LOG(ERR, "(%s) kickfd error, %s", dev->path, strerror(errno));
366 dev->callfds[i] = callfd;
367 dev->kickfds[i] = kickfd;
372 for (j = 0; j < i; j++) {
373 if (dev->kickfds[j] >= 0) {
374 close(dev->kickfds[j]);
375 dev->kickfds[j] = -1;
377 if (dev->callfds[j] >= 0) {
378 close(dev->callfds[j]);
379 dev->callfds[j] = -1;
387 virtio_user_dev_uninit_notify(struct virtio_user_dev *dev)
391 for (i = 0; i < dev->max_queue_pairs * 2; ++i) {
392 if (dev->kickfds[i] >= 0) {
393 close(dev->kickfds[i]);
394 dev->kickfds[i] = -1;
396 if (dev->callfds[i] >= 0) {
397 close(dev->callfds[i]);
398 dev->callfds[i] = -1;
404 virtio_user_fill_intr_handle(struct virtio_user_dev *dev)
407 struct rte_eth_dev *eth_dev = &rte_eth_devices[dev->hw.port_id];
409 if (!eth_dev->intr_handle) {
410 eth_dev->intr_handle = malloc(sizeof(*eth_dev->intr_handle));
411 if (!eth_dev->intr_handle) {
412 PMD_DRV_LOG(ERR, "(%s) failed to allocate intr_handle", dev->path);
415 memset(eth_dev->intr_handle, 0, sizeof(*eth_dev->intr_handle));
418 for (i = 0; i < dev->max_queue_pairs; ++i)
419 eth_dev->intr_handle->efds[i] = dev->callfds[i];
420 eth_dev->intr_handle->nb_efd = dev->max_queue_pairs;
421 eth_dev->intr_handle->max_intr = dev->max_queue_pairs + 1;
422 eth_dev->intr_handle->type = RTE_INTR_HANDLE_VDEV;
423 /* For virtio vdev, no need to read counter for clean */
424 eth_dev->intr_handle->efd_counter_size = 0;
425 eth_dev->intr_handle->fd = dev->ops->get_intr_fd(dev);
431 virtio_user_mem_event_cb(enum rte_mem_event type __rte_unused,
433 size_t len __rte_unused,
436 struct virtio_user_dev *dev = arg;
437 struct rte_memseg_list *msl;
441 /* ignore externally allocated memory */
442 msl = rte_mem_virt2memseg_list(addr);
446 pthread_mutex_lock(&dev->mutex);
448 if (dev->started == false)
451 /* Step 1: pause the active queues */
452 for (i = 0; i < dev->queue_pairs; i++) {
453 ret = dev->ops->enable_qp(dev, i, 0);
458 /* Step 2: update memory regions */
459 ret = dev->ops->set_memory_table(dev);
463 /* Step 3: resume the active queues */
464 for (i = 0; i < dev->queue_pairs; i++) {
465 ret = dev->ops->enable_qp(dev, i, 1);
471 pthread_mutex_unlock(&dev->mutex);
474 PMD_DRV_LOG(ERR, "(%s) Failed to update memory table\n", dev->path);
478 virtio_user_dev_setup(struct virtio_user_dev *dev)
480 if (dev->is_server) {
481 if (dev->backend_type != VIRTIO_USER_BACKEND_VHOST_USER) {
482 PMD_DRV_LOG(ERR, "Server mode only supports vhost-user!");
487 switch (dev->backend_type) {
488 case VIRTIO_USER_BACKEND_VHOST_USER:
489 dev->ops = &virtio_ops_user;
491 case VIRTIO_USER_BACKEND_VHOST_KERNEL:
492 dev->ops = &virtio_ops_kernel;
494 case VIRTIO_USER_BACKEND_VHOST_VDPA:
495 dev->ops = &virtio_ops_vdpa;
498 PMD_DRV_LOG(ERR, "(%s) Unknown backend type", dev->path);
502 if (dev->ops->setup(dev) < 0) {
503 PMD_INIT_LOG(ERR, "(%s) Failed to setup backend\n", dev->path);
507 if (virtio_user_dev_init_notify(dev) < 0) {
508 PMD_INIT_LOG(ERR, "(%s) Failed to init notifiers\n", dev->path);
512 if (virtio_user_fill_intr_handle(dev) < 0) {
513 PMD_INIT_LOG(ERR, "(%s) Failed to init interrupt handler\n", dev->path);
520 virtio_user_dev_uninit_notify(dev);
522 dev->ops->destroy(dev);
527 /* Use below macro to filter features from vhost backend */
528 #define VIRTIO_USER_SUPPORTED_FEATURES \
529 (1ULL << VIRTIO_NET_F_MAC | \
530 1ULL << VIRTIO_NET_F_STATUS | \
531 1ULL << VIRTIO_NET_F_MQ | \
532 1ULL << VIRTIO_NET_F_CTRL_MAC_ADDR | \
533 1ULL << VIRTIO_NET_F_CTRL_VQ | \
534 1ULL << VIRTIO_NET_F_CTRL_RX | \
535 1ULL << VIRTIO_NET_F_CTRL_VLAN | \
536 1ULL << VIRTIO_NET_F_CSUM | \
537 1ULL << VIRTIO_NET_F_HOST_TSO4 | \
538 1ULL << VIRTIO_NET_F_HOST_TSO6 | \
539 1ULL << VIRTIO_NET_F_MRG_RXBUF | \
540 1ULL << VIRTIO_RING_F_INDIRECT_DESC | \
541 1ULL << VIRTIO_NET_F_GUEST_CSUM | \
542 1ULL << VIRTIO_NET_F_GUEST_TSO4 | \
543 1ULL << VIRTIO_NET_F_GUEST_TSO6 | \
544 1ULL << VIRTIO_F_IN_ORDER | \
545 1ULL << VIRTIO_F_VERSION_1 | \
546 1ULL << VIRTIO_F_RING_PACKED)
549 virtio_user_dev_init(struct virtio_user_dev *dev, char *path, int queues,
550 int cq, int queue_size, const char *mac, char **ifname,
551 int server, int mrg_rxbuf, int in_order, int packed_vq,
552 enum virtio_user_backend_type backend_type)
554 uint64_t backend_features;
557 pthread_mutex_init(&dev->mutex, NULL);
558 strlcpy(dev->path, path, PATH_MAX);
560 for (i = 0; i < VIRTIO_MAX_VIRTQUEUES; i++) {
561 dev->kickfds[i] = -1;
562 dev->callfds[i] = -1;
566 dev->max_queue_pairs = queues;
567 dev->queue_pairs = 1; /* mq disabled by default */
568 dev->queue_size = queue_size;
569 dev->is_server = server;
570 dev->mac_specified = 0;
571 dev->frontend_features = 0;
572 dev->unsupported_features = 0;
573 dev->backend_type = backend_type;
576 dev->ifname = *ifname;
580 if (virtio_user_dev_setup(dev) < 0) {
581 PMD_INIT_LOG(ERR, "(%s) backend set up fails", dev->path);
585 if (dev->ops->set_owner(dev) < 0) {
586 PMD_INIT_LOG(ERR, "(%s) Failed to set backend owner", dev->path);
590 if (dev->ops->get_backend_features(&backend_features) < 0) {
591 PMD_INIT_LOG(ERR, "(%s) Failed to get backend features", dev->path);
595 dev->unsupported_features = ~(VIRTIO_USER_SUPPORTED_FEATURES | backend_features);
597 if (dev->ops->get_features(dev, &dev->device_features) < 0) {
598 PMD_INIT_LOG(ERR, "(%s) Failed to get device features", dev->path);
602 virtio_user_dev_init_mac(dev, mac);
605 dev->unsupported_features |= (1ull << VIRTIO_NET_F_MRG_RXBUF);
608 dev->unsupported_features |= (1ull << VIRTIO_F_IN_ORDER);
611 dev->unsupported_features |= (1ull << VIRTIO_F_RING_PACKED);
613 if (dev->mac_specified)
614 dev->frontend_features |= (1ull << VIRTIO_NET_F_MAC);
616 dev->unsupported_features |= (1ull << VIRTIO_NET_F_MAC);
619 /* device does not really need to know anything about CQ,
620 * so if necessary, we just claim to support CQ
622 dev->frontend_features |= (1ull << VIRTIO_NET_F_CTRL_VQ);
624 dev->unsupported_features |= (1ull << VIRTIO_NET_F_CTRL_VQ);
625 /* Also disable features that depend on VIRTIO_NET_F_CTRL_VQ */
626 dev->unsupported_features |= (1ull << VIRTIO_NET_F_CTRL_RX);
627 dev->unsupported_features |= (1ull << VIRTIO_NET_F_CTRL_VLAN);
628 dev->unsupported_features |=
629 (1ull << VIRTIO_NET_F_GUEST_ANNOUNCE);
630 dev->unsupported_features |= (1ull << VIRTIO_NET_F_MQ);
631 dev->unsupported_features |=
632 (1ull << VIRTIO_NET_F_CTRL_MAC_ADDR);
635 /* The backend will not report this feature, we add it explicitly */
636 if (dev->backend_type == VIRTIO_USER_BACKEND_VHOST_USER)
637 dev->frontend_features |= (1ull << VIRTIO_NET_F_STATUS);
639 dev->frontend_features &= ~dev->unsupported_features;
640 dev->device_features &= ~dev->unsupported_features;
642 if (rte_mem_event_callback_register(VIRTIO_USER_MEM_EVENT_CLB_NAME,
643 virtio_user_mem_event_cb, dev)) {
644 if (rte_errno != ENOTSUP) {
645 PMD_INIT_LOG(ERR, "(%s) Failed to register mem event callback\n",
655 virtio_user_dev_uninit(struct virtio_user_dev *dev)
657 struct rte_eth_dev *eth_dev = &rte_eth_devices[dev->hw.port_id];
659 if (eth_dev->intr_handle) {
660 free(eth_dev->intr_handle);
661 eth_dev->intr_handle = NULL;
664 virtio_user_stop_device(dev);
666 rte_mem_event_callback_unregister(VIRTIO_USER_MEM_EVENT_CLB_NAME, dev);
668 virtio_user_dev_uninit_notify(dev);
675 dev->ops->destroy(dev);
679 virtio_user_handle_mq(struct virtio_user_dev *dev, uint16_t q_pairs)
684 if (q_pairs > dev->max_queue_pairs) {
685 PMD_INIT_LOG(ERR, "(%s) multi-q config %u, but only %u supported",
686 dev->path, q_pairs, dev->max_queue_pairs);
690 for (i = 0; i < q_pairs; ++i)
691 ret |= dev->ops->enable_qp(dev, i, 1);
692 for (i = q_pairs; i < dev->max_queue_pairs; ++i)
693 ret |= dev->ops->enable_qp(dev, i, 0);
695 dev->queue_pairs = q_pairs;
701 virtio_user_handle_ctrl_msg(struct virtio_user_dev *dev, struct vring *vring,
704 struct virtio_net_ctrl_hdr *hdr;
705 virtio_net_ctrl_ack status = ~0;
706 uint16_t i, idx_data, idx_status;
707 uint32_t n_descs = 0;
709 /* locate desc for header, data, and status */
710 idx_data = vring->desc[idx_hdr].next;
714 while (vring->desc[i].flags == VRING_DESC_F_NEXT) {
715 i = vring->desc[i].next;
719 /* locate desc for status */
723 hdr = (void *)(uintptr_t)vring->desc[idx_hdr].addr;
724 if (hdr->class == VIRTIO_NET_CTRL_MQ &&
725 hdr->cmd == VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET) {
728 queues = *(uint16_t *)(uintptr_t)vring->desc[idx_data].addr;
729 status = virtio_user_handle_mq(dev, queues);
730 } else if (hdr->class == VIRTIO_NET_CTRL_RX ||
731 hdr->class == VIRTIO_NET_CTRL_MAC ||
732 hdr->class == VIRTIO_NET_CTRL_VLAN) {
737 *(virtio_net_ctrl_ack *)(uintptr_t)vring->desc[idx_status].addr = status;
743 desc_is_avail(struct vring_packed_desc *desc, bool wrap_counter)
745 uint16_t flags = __atomic_load_n(&desc->flags, __ATOMIC_ACQUIRE);
747 return wrap_counter == !!(flags & VRING_PACKED_DESC_F_AVAIL) &&
748 wrap_counter != !!(flags & VRING_PACKED_DESC_F_USED);
752 virtio_user_handle_ctrl_msg_packed(struct virtio_user_dev *dev,
753 struct vring_packed *vring,
756 struct virtio_net_ctrl_hdr *hdr;
757 virtio_net_ctrl_ack status = ~0;
758 uint16_t idx_data, idx_status;
759 /* initialize to one, header is first */
760 uint32_t n_descs = 1;
762 /* locate desc for header, data, and status */
763 idx_data = idx_hdr + 1;
764 if (idx_data >= dev->queue_size)
765 idx_data -= dev->queue_size;
769 idx_status = idx_data;
770 while (vring->desc[idx_status].flags & VRING_DESC_F_NEXT) {
772 if (idx_status >= dev->queue_size)
773 idx_status -= dev->queue_size;
777 hdr = (void *)(uintptr_t)vring->desc[idx_hdr].addr;
778 if (hdr->class == VIRTIO_NET_CTRL_MQ &&
779 hdr->cmd == VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET) {
782 queues = *(uint16_t *)(uintptr_t)
783 vring->desc[idx_data].addr;
784 status = virtio_user_handle_mq(dev, queues);
785 } else if (hdr->class == VIRTIO_NET_CTRL_RX ||
786 hdr->class == VIRTIO_NET_CTRL_MAC ||
787 hdr->class == VIRTIO_NET_CTRL_VLAN) {
792 *(virtio_net_ctrl_ack *)(uintptr_t)
793 vring->desc[idx_status].addr = status;
795 /* Update used descriptor */
796 vring->desc[idx_hdr].id = vring->desc[idx_status].id;
797 vring->desc[idx_hdr].len = sizeof(status);
803 virtio_user_handle_cq_packed(struct virtio_user_dev *dev, uint16_t queue_idx)
805 struct virtio_user_queue *vq = &dev->packed_queues[queue_idx];
806 struct vring_packed *vring = &dev->packed_vrings[queue_idx];
807 uint16_t n_descs, flags;
809 /* Perform a load-acquire barrier in desc_is_avail to
810 * enforce the ordering between desc flags and desc
813 while (desc_is_avail(&vring->desc[vq->used_idx],
814 vq->used_wrap_counter)) {
816 n_descs = virtio_user_handle_ctrl_msg_packed(dev, vring,
819 flags = VRING_DESC_F_WRITE;
820 if (vq->used_wrap_counter)
821 flags |= VRING_PACKED_DESC_F_AVAIL_USED;
823 __atomic_store_n(&vring->desc[vq->used_idx].flags, flags,
826 vq->used_idx += n_descs;
827 if (vq->used_idx >= dev->queue_size) {
828 vq->used_idx -= dev->queue_size;
829 vq->used_wrap_counter ^= 1;
835 virtio_user_handle_cq(struct virtio_user_dev *dev, uint16_t queue_idx)
837 uint16_t avail_idx, desc_idx;
838 struct vring_used_elem *uep;
840 struct vring *vring = &dev->vrings[queue_idx];
842 /* Consume avail ring, using used ring idx as first one */
843 while (__atomic_load_n(&vring->used->idx, __ATOMIC_RELAXED)
844 != vring->avail->idx) {
845 avail_idx = __atomic_load_n(&vring->used->idx, __ATOMIC_RELAXED)
847 desc_idx = vring->avail->ring[avail_idx];
849 n_descs = virtio_user_handle_ctrl_msg(dev, vring, desc_idx);
851 /* Update used ring */
852 uep = &vring->used->ring[avail_idx];
856 __atomic_add_fetch(&vring->used->idx, 1, __ATOMIC_RELAXED);
861 virtio_user_dev_set_status(struct virtio_user_dev *dev, uint8_t status)
865 pthread_mutex_lock(&dev->mutex);
866 dev->status = status;
867 ret = dev->ops->set_status(dev, status);
868 if (ret && ret != -ENOTSUP)
869 PMD_INIT_LOG(ERR, "(%s) Failed to set backend status\n", dev->path);
871 pthread_mutex_unlock(&dev->mutex);
876 virtio_user_dev_update_status(struct virtio_user_dev *dev)
881 pthread_mutex_lock(&dev->mutex);
883 ret = dev->ops->get_status(dev, &status);
885 dev->status = status;
886 PMD_INIT_LOG(DEBUG, "Updated Device Status(0x%08x):\n"
888 "\t-ACKNOWLEDGE: %u\n"
891 "\t-FEATURES_OK: %u\n"
892 "\t-DEVICE_NEED_RESET: %u\n"
895 (dev->status == VIRTIO_CONFIG_STATUS_RESET),
896 !!(dev->status & VIRTIO_CONFIG_STATUS_ACK),
897 !!(dev->status & VIRTIO_CONFIG_STATUS_DRIVER),
898 !!(dev->status & VIRTIO_CONFIG_STATUS_DRIVER_OK),
899 !!(dev->status & VIRTIO_CONFIG_STATUS_FEATURES_OK),
900 !!(dev->status & VIRTIO_CONFIG_STATUS_DEV_NEED_RESET),
901 !!(dev->status & VIRTIO_CONFIG_STATUS_FAILED));
902 } else if (ret != -ENOTSUP) {
903 PMD_INIT_LOG(ERR, "(%s) Failed to get backend status\n", dev->path);
906 pthread_mutex_unlock(&dev->mutex);
911 virtio_user_dev_update_link_state(struct virtio_user_dev *dev)
913 if (dev->ops->update_link_state)
914 return dev->ops->update_link_state(dev);
920 virtio_user_dev_reset_queues_packed(struct rte_eth_dev *eth_dev)
922 struct virtio_user_dev *dev = eth_dev->data->dev_private;
923 struct virtio_hw *hw = &dev->hw;
924 struct virtnet_rx *rxvq;
925 struct virtnet_tx *txvq;
928 /* Add lock to avoid queue contention. */
929 rte_spinlock_lock(&hw->state_lock);
933 * Waiting for datapath to complete before resetting queues.
934 * 1 ms should be enough for the ongoing Tx/Rx function to finish.
938 /* Vring reset for each Tx queue and Rx queue. */
939 for (i = 0; i < eth_dev->data->nb_rx_queues; i++) {
940 rxvq = eth_dev->data->rx_queues[i];
941 virtqueue_rxvq_reset_packed(virtnet_rxq_to_vq(rxvq));
942 virtio_dev_rx_queue_setup_finish(eth_dev, i);
945 for (i = 0; i < eth_dev->data->nb_tx_queues; i++) {
946 txvq = eth_dev->data->tx_queues[i];
947 virtqueue_txvq_reset_packed(virtnet_txq_to_vq(txvq));
951 rte_spinlock_unlock(&hw->state_lock);
955 virtio_user_dev_delayed_disconnect_handler(void *param)
957 struct virtio_user_dev *dev = param;
958 struct rte_eth_dev *eth_dev = &rte_eth_devices[dev->hw.port_id];
960 if (rte_intr_disable(eth_dev->intr_handle) < 0) {
961 PMD_DRV_LOG(ERR, "interrupt disable failed");
964 PMD_DRV_LOG(DEBUG, "Unregistering intr fd: %d",
965 eth_dev->intr_handle->fd);
966 if (rte_intr_callback_unregister(eth_dev->intr_handle,
967 virtio_interrupt_handler,
969 PMD_DRV_LOG(ERR, "interrupt unregister failed");
971 if (dev->is_server) {
972 if (dev->ops->server_disconnect)
973 dev->ops->server_disconnect(dev);
975 eth_dev->intr_handle->fd = dev->ops->get_intr_fd(dev);
977 PMD_DRV_LOG(DEBUG, "Registering intr fd: %d",
978 eth_dev->intr_handle->fd);
980 if (rte_intr_callback_register(eth_dev->intr_handle,
981 virtio_interrupt_handler,
983 PMD_DRV_LOG(ERR, "interrupt register failed");
985 if (rte_intr_enable(eth_dev->intr_handle) < 0) {
986 PMD_DRV_LOG(ERR, "interrupt enable failed");
993 virtio_user_dev_delayed_intr_reconfig_handler(void *param)
995 struct virtio_user_dev *dev = param;
996 struct rte_eth_dev *eth_dev = &rte_eth_devices[dev->hw.port_id];
998 PMD_DRV_LOG(DEBUG, "Unregistering intr fd: %d",
999 eth_dev->intr_handle->fd);
1001 if (rte_intr_callback_unregister(eth_dev->intr_handle,
1002 virtio_interrupt_handler,
1004 PMD_DRV_LOG(ERR, "interrupt unregister failed");
1006 eth_dev->intr_handle->fd = dev->ops->get_intr_fd(dev);
1008 PMD_DRV_LOG(DEBUG, "Registering intr fd: %d", eth_dev->intr_handle->fd);
1010 if (rte_intr_callback_register(eth_dev->intr_handle,
1011 virtio_interrupt_handler, eth_dev))
1012 PMD_DRV_LOG(ERR, "interrupt register failed");
1014 if (rte_intr_enable(eth_dev->intr_handle) < 0)
1015 PMD_DRV_LOG(ERR, "interrupt enable failed");
1019 virtio_user_dev_server_reconnect(struct virtio_user_dev *dev)
1021 int ret, old_status;
1022 struct rte_eth_dev *eth_dev = &rte_eth_devices[dev->hw.port_id];
1023 struct virtio_hw *hw = &dev->hw;
1025 if (!dev->ops->server_reconnect) {
1026 PMD_DRV_LOG(ERR, "(%s) Missing server reconnect callback", dev->path);
1030 if (dev->ops->server_reconnect(dev)) {
1031 PMD_DRV_LOG(ERR, "(%s) Reconnect callback call failed", dev->path);
1035 old_status = dev->status;
1039 virtio_set_status(hw, VIRTIO_CONFIG_STATUS_ACK);
1041 virtio_set_status(hw, VIRTIO_CONFIG_STATUS_DRIVER);
1043 if (dev->ops->get_features(dev, &dev->device_features) < 0) {
1044 PMD_INIT_LOG(ERR, "get_features failed: %s",
1049 /* unmask vhost-user unsupported features */
1050 dev->device_features &= ~(dev->unsupported_features);
1052 dev->features &= (dev->device_features | dev->frontend_features);
1054 /* For packed ring, resetting queues is required in reconnection. */
1055 if (virtio_with_packed_queue(hw) &&
1056 (old_status & VIRTIO_CONFIG_STATUS_DRIVER_OK)) {
1057 PMD_INIT_LOG(NOTICE, "Packets on the fly will be dropped"
1058 " when packed ring reconnecting.");
1059 virtio_user_dev_reset_queues_packed(eth_dev);
1062 virtio_set_status(hw, VIRTIO_CONFIG_STATUS_FEATURES_OK);
1064 /* Start the device */
1065 virtio_set_status(hw, VIRTIO_CONFIG_STATUS_DRIVER_OK);
1069 if (dev->queue_pairs > 1) {
1070 ret = virtio_user_handle_mq(dev, dev->queue_pairs);
1072 PMD_INIT_LOG(ERR, "Fails to enable multi-queue pairs!");
1076 if (eth_dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC) {
1077 if (rte_intr_disable(eth_dev->intr_handle) < 0) {
1078 PMD_DRV_LOG(ERR, "interrupt disable failed");
1082 * This function can be called from the interrupt handler, so
1083 * we can't unregister interrupt handler here. Setting
1084 * alarm to do that later.
1086 rte_eal_alarm_set(1,
1087 virtio_user_dev_delayed_intr_reconfig_handler,
1090 PMD_INIT_LOG(NOTICE, "server mode virtio-user reconnection succeeds!");