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_string_fns.h>
17 #include <rte_eal_memconfig.h>
20 #include "virtio_user_dev.h"
21 #include "../virtio_ethdev.h"
23 #define VIRTIO_USER_MEM_EVENT_CLB_NAME "virtio_user_mem_event_clb"
25 const char * const virtio_user_backend_strings[] = {
26 [VIRTIO_USER_BACKEND_UNKNOWN] = "VIRTIO_USER_BACKEND_UNKNOWN",
27 [VIRTIO_USER_BACKEND_VHOST_USER] = "VHOST_USER",
28 [VIRTIO_USER_BACKEND_VHOST_KERNEL] = "VHOST_NET",
29 [VIRTIO_USER_BACKEND_VHOST_VDPA] = "VHOST_VDPA",
33 virtio_user_create_queue(struct virtio_user_dev *dev, uint32_t queue_sel)
35 /* Of all per virtqueue MSGs, make sure VHOST_SET_VRING_CALL come
36 * firstly because vhost depends on this msg to allocate virtqueue
39 struct vhost_vring_file file;
42 file.index = queue_sel;
43 file.fd = dev->callfds[queue_sel];
44 ret = dev->ops->set_vring_call(dev, &file);
46 PMD_INIT_LOG(ERR, "(%s) Failed to create queue %u\n", dev->path, queue_sel);
54 virtio_user_kick_queue(struct virtio_user_dev *dev, uint32_t queue_sel)
57 struct vhost_vring_file file;
58 struct vhost_vring_state state;
59 struct vring *vring = &dev->vrings[queue_sel];
60 struct vring_packed *pq_vring = &dev->packed_vrings[queue_sel];
61 struct vhost_vring_addr addr = {
64 .flags = 0, /* disable log */
67 if (dev->features & (1ULL << VIRTIO_F_RING_PACKED)) {
69 (uint64_t)(uintptr_t)pq_vring->desc;
70 addr.avail_user_addr =
71 (uint64_t)(uintptr_t)pq_vring->driver;
73 (uint64_t)(uintptr_t)pq_vring->device;
75 addr.desc_user_addr = (uint64_t)(uintptr_t)vring->desc;
76 addr.avail_user_addr = (uint64_t)(uintptr_t)vring->avail;
77 addr.used_user_addr = (uint64_t)(uintptr_t)vring->used;
80 state.index = queue_sel;
81 state.num = vring->num;
82 ret = dev->ops->set_vring_num(dev, &state);
86 state.index = queue_sel;
87 state.num = 0; /* no reservation */
88 if (dev->features & (1ULL << VIRTIO_F_RING_PACKED))
89 state.num |= (1 << 15);
90 ret = dev->ops->set_vring_base(dev, &state);
94 ret = dev->ops->set_vring_addr(dev, &addr);
98 /* Of all per virtqueue MSGs, make sure VHOST_USER_SET_VRING_KICK comes
99 * lastly because vhost depends on this msg to judge if
102 file.index = queue_sel;
103 file.fd = dev->kickfds[queue_sel];
104 ret = dev->ops->set_vring_kick(dev, &file);
110 PMD_INIT_LOG(ERR, "(%s) Failed to kick queue %u\n", dev->path, queue_sel);
116 virtio_user_queue_setup(struct virtio_user_dev *dev,
117 int (*fn)(struct virtio_user_dev *, uint32_t))
119 uint32_t i, queue_sel;
121 for (i = 0; i < dev->max_queue_pairs; ++i) {
122 queue_sel = 2 * i + VTNET_SQ_RQ_QUEUE_IDX;
123 if (fn(dev, queue_sel) < 0) {
124 PMD_DRV_LOG(ERR, "(%s) setup rx vq %u failed", dev->path, i);
128 for (i = 0; i < dev->max_queue_pairs; ++i) {
129 queue_sel = 2 * i + VTNET_SQ_TQ_QUEUE_IDX;
130 if (fn(dev, queue_sel) < 0) {
131 PMD_DRV_LOG(INFO, "(%s) setup tx vq %u failed", dev->path, i);
140 virtio_user_dev_set_features(struct virtio_user_dev *dev)
145 pthread_mutex_lock(&dev->mutex);
147 /* Step 0: tell vhost to create queues */
148 if (virtio_user_queue_setup(dev, virtio_user_create_queue) < 0)
151 features = dev->features;
153 /* Strip VIRTIO_NET_F_MAC, as MAC address is handled in vdev init */
154 features &= ~(1ull << VIRTIO_NET_F_MAC);
155 /* Strip VIRTIO_NET_F_CTRL_VQ, as devices do not really need to know */
156 features &= ~(1ull << VIRTIO_NET_F_CTRL_VQ);
157 features &= ~(1ull << VIRTIO_NET_F_STATUS);
158 ret = dev->ops->set_features(dev, features);
161 PMD_DRV_LOG(INFO, "(%s) set features: 0x%" PRIx64, dev->path, features);
163 pthread_mutex_unlock(&dev->mutex);
169 virtio_user_start_device(struct virtio_user_dev *dev)
176 * We need to make sure that the locks will be
177 * taken in the correct order to avoid deadlocks.
179 * Before releasing this lock, this thread should
180 * not trigger any memory hotplug events.
182 * This is a temporary workaround, and should be
183 * replaced when we get proper supports from the
184 * memory subsystem in the future.
186 rte_mcfg_mem_read_lock();
187 pthread_mutex_lock(&dev->mutex);
189 /* Step 2: share memory regions */
190 ret = dev->ops->set_memory_table(dev);
194 /* Step 3: kick queues */
195 ret = virtio_user_queue_setup(dev, virtio_user_kick_queue);
199 /* Step 4: enable queues
200 * we enable the 1st queue pair by default.
202 ret = dev->ops->enable_qp(dev, 0, 1);
208 pthread_mutex_unlock(&dev->mutex);
209 rte_mcfg_mem_read_unlock();
213 pthread_mutex_unlock(&dev->mutex);
214 rte_mcfg_mem_read_unlock();
216 PMD_INIT_LOG(ERR, "(%s) Failed to start device\n", dev->path);
218 /* TODO: free resource here or caller to check */
222 int virtio_user_stop_device(struct virtio_user_dev *dev)
224 struct vhost_vring_state state;
228 pthread_mutex_lock(&dev->mutex);
232 for (i = 0; i < dev->max_queue_pairs; ++i) {
233 ret = dev->ops->enable_qp(dev, i, 0);
238 /* Stop the backend. */
239 for (i = 0; i < dev->max_queue_pairs * 2; ++i) {
241 ret = dev->ops->get_vring_base(dev, &state);
243 PMD_DRV_LOG(ERR, "(%s) get_vring_base failed, index=%u", dev->path, i);
248 dev->started = false;
251 pthread_mutex_unlock(&dev->mutex);
255 pthread_mutex_unlock(&dev->mutex);
257 PMD_INIT_LOG(ERR, "(%s) Failed to stop device\n", dev->path);
263 parse_mac(struct virtio_user_dev *dev, const char *mac)
265 struct rte_ether_addr tmp;
270 if (rte_ether_unformat_addr(mac, &tmp) == 0) {
271 memcpy(dev->mac_addr, &tmp, RTE_ETHER_ADDR_LEN);
272 dev->mac_specified = 1;
274 /* ignore the wrong mac, use random mac */
275 PMD_DRV_LOG(ERR, "wrong format of mac: %s", mac);
280 virtio_user_dev_init_notify(struct virtio_user_dev *dev)
286 for (i = 0; i < VIRTIO_MAX_VIRTQUEUES; ++i) {
287 if (i >= dev->max_queue_pairs * 2) {
288 dev->kickfds[i] = -1;
289 dev->callfds[i] = -1;
293 /* May use invalid flag, but some backend uses kickfd and
294 * callfd as criteria to judge if dev is alive. so finally we
297 callfd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);
299 PMD_DRV_LOG(ERR, "(%s) callfd error, %s", dev->path, strerror(errno));
302 kickfd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);
305 PMD_DRV_LOG(ERR, "(%s) kickfd error, %s", dev->path, strerror(errno));
308 dev->callfds[i] = callfd;
309 dev->kickfds[i] = kickfd;
312 if (i < VIRTIO_MAX_VIRTQUEUES) {
313 for (j = 0; j < i; ++j) {
314 close(dev->callfds[j]);
315 close(dev->kickfds[j]);
325 virtio_user_fill_intr_handle(struct virtio_user_dev *dev)
328 struct rte_eth_dev *eth_dev = &rte_eth_devices[dev->port_id];
330 if (!eth_dev->intr_handle) {
331 eth_dev->intr_handle = malloc(sizeof(*eth_dev->intr_handle));
332 if (!eth_dev->intr_handle) {
333 PMD_DRV_LOG(ERR, "(%s) failed to allocate intr_handle", dev->path);
336 memset(eth_dev->intr_handle, 0, sizeof(*eth_dev->intr_handle));
339 for (i = 0; i < dev->max_queue_pairs; ++i)
340 eth_dev->intr_handle->efds[i] = dev->callfds[i];
341 eth_dev->intr_handle->nb_efd = dev->max_queue_pairs;
342 eth_dev->intr_handle->max_intr = dev->max_queue_pairs + 1;
343 eth_dev->intr_handle->type = RTE_INTR_HANDLE_VDEV;
344 /* For virtio vdev, no need to read counter for clean */
345 eth_dev->intr_handle->efd_counter_size = 0;
346 eth_dev->intr_handle->fd = -1;
347 if (dev->vhostfd >= 0)
348 eth_dev->intr_handle->fd = dev->vhostfd;
349 else if (dev->is_server)
350 eth_dev->intr_handle->fd = dev->listenfd;
356 virtio_user_mem_event_cb(enum rte_mem_event type __rte_unused,
358 size_t len __rte_unused,
361 struct virtio_user_dev *dev = arg;
362 struct rte_memseg_list *msl;
366 /* ignore externally allocated memory */
367 msl = rte_mem_virt2memseg_list(addr);
371 pthread_mutex_lock(&dev->mutex);
373 if (dev->started == false)
376 /* Step 1: pause the active queues */
377 for (i = 0; i < dev->queue_pairs; i++) {
378 ret = dev->ops->enable_qp(dev, i, 0);
383 /* Step 2: update memory regions */
384 ret = dev->ops->set_memory_table(dev);
388 /* Step 3: resume the active queues */
389 for (i = 0; i < dev->queue_pairs; i++) {
390 ret = dev->ops->enable_qp(dev, i, 1);
396 pthread_mutex_unlock(&dev->mutex);
399 PMD_DRV_LOG(ERR, "(%s) Failed to update memory table\n", dev->path);
403 virtio_user_dev_setup(struct virtio_user_dev *dev)
408 dev->vhostfds = NULL;
411 if (dev->is_server) {
412 if (dev->backend_type != VIRTIO_USER_BACKEND_VHOST_USER) {
413 PMD_DRV_LOG(ERR, "Server mode only supports vhost-user!");
418 if (dev->backend_type == VIRTIO_USER_BACKEND_VHOST_USER) {
419 dev->ops = &virtio_ops_user;
420 } else if (dev->backend_type ==
421 VIRTIO_USER_BACKEND_VHOST_KERNEL) {
422 dev->ops = &virtio_ops_kernel;
424 dev->vhostfds = malloc(dev->max_queue_pairs *
426 dev->tapfds = malloc(dev->max_queue_pairs *
428 if (!dev->vhostfds || !dev->tapfds) {
429 PMD_INIT_LOG(ERR, "(%s) Failed to allocate FDs", dev->path);
433 for (q = 0; q < dev->max_queue_pairs; ++q) {
434 dev->vhostfds[q] = -1;
437 } else if (dev->backend_type ==
438 VIRTIO_USER_BACKEND_VHOST_VDPA) {
439 dev->ops = &virtio_ops_vdpa;
441 PMD_DRV_LOG(ERR, "(%s) Unknown backend type", dev->path);
446 if (dev->ops->setup(dev) < 0) {
447 PMD_INIT_LOG(ERR, "(%s) Failed to setup backend\n", dev->path);
451 if (virtio_user_dev_init_notify(dev) < 0) {
452 PMD_INIT_LOG(ERR, "(%s) Failed to init notifiers\n", dev->path);
456 if (virtio_user_fill_intr_handle(dev) < 0) {
457 PMD_INIT_LOG(ERR, "(%s) Failed to init interrupt handler\n", dev->path);
464 /* Use below macro to filter features from vhost backend */
465 #define VIRTIO_USER_SUPPORTED_FEATURES \
466 (1ULL << VIRTIO_NET_F_MAC | \
467 1ULL << VIRTIO_NET_F_STATUS | \
468 1ULL << VIRTIO_NET_F_MQ | \
469 1ULL << VIRTIO_NET_F_CTRL_MAC_ADDR | \
470 1ULL << VIRTIO_NET_F_CTRL_VQ | \
471 1ULL << VIRTIO_NET_F_CTRL_RX | \
472 1ULL << VIRTIO_NET_F_CTRL_VLAN | \
473 1ULL << VIRTIO_NET_F_CSUM | \
474 1ULL << VIRTIO_NET_F_HOST_TSO4 | \
475 1ULL << VIRTIO_NET_F_HOST_TSO6 | \
476 1ULL << VIRTIO_NET_F_MRG_RXBUF | \
477 1ULL << VIRTIO_RING_F_INDIRECT_DESC | \
478 1ULL << VIRTIO_NET_F_GUEST_CSUM | \
479 1ULL << VIRTIO_NET_F_GUEST_TSO4 | \
480 1ULL << VIRTIO_NET_F_GUEST_TSO6 | \
481 1ULL << VIRTIO_F_IN_ORDER | \
482 1ULL << VIRTIO_F_VERSION_1 | \
483 1ULL << VIRTIO_F_RING_PACKED | \
484 1ULL << VHOST_USER_F_PROTOCOL_FEATURES)
486 #define VHOST_USER_SUPPORTED_PROTOCOL_FEATURES \
487 (1ULL << VHOST_USER_PROTOCOL_F_MQ | \
488 1ULL << VHOST_USER_PROTOCOL_F_REPLY_ACK | \
489 1ULL << VHOST_USER_PROTOCOL_F_STATUS)
491 #define VHOST_VDPA_SUPPORTED_PROTOCOL_FEATURES \
492 (1ULL << VHOST_BACKEND_F_IOTLB_MSG_V2 | \
493 1ULL << VHOST_BACKEND_F_IOTLB_BATCH)
495 virtio_user_dev_init(struct virtio_user_dev *dev, char *path, int queues,
496 int cq, int queue_size, const char *mac, char **ifname,
497 int server, int mrg_rxbuf, int in_order, int packed_vq,
498 enum virtio_user_backend_type backend_type)
500 uint64_t protocol_features = 0;
502 pthread_mutex_init(&dev->mutex, NULL);
503 strlcpy(dev->path, path, PATH_MAX);
505 dev->max_queue_pairs = queues;
506 dev->queue_pairs = 1; /* mq disabled by default */
507 dev->queue_size = queue_size;
508 dev->is_server = server;
509 dev->mac_specified = 0;
510 dev->frontend_features = 0;
511 dev->unsupported_features = ~VIRTIO_USER_SUPPORTED_FEATURES;
512 dev->backend_type = backend_type;
514 if (dev->backend_type == VIRTIO_USER_BACKEND_VHOST_USER)
515 dev->protocol_features = VHOST_USER_SUPPORTED_PROTOCOL_FEATURES;
516 else if (dev->backend_type == VIRTIO_USER_BACKEND_VHOST_VDPA)
517 dev->protocol_features = VHOST_VDPA_SUPPORTED_PROTOCOL_FEATURES;
522 dev->ifname = *ifname;
526 if (virtio_user_dev_setup(dev) < 0) {
527 PMD_INIT_LOG(ERR, "(%s) backend set up fails", dev->path);
531 if (dev->backend_type != VIRTIO_USER_BACKEND_VHOST_USER)
532 dev->unsupported_features |=
533 (1ULL << VHOST_USER_F_PROTOCOL_FEATURES);
535 if (dev->ops->set_owner(dev) < 0) {
536 PMD_INIT_LOG(ERR, "(%s) Failed to set backend owner", dev->path);
540 if (dev->ops->get_features(dev, &dev->device_features) < 0) {
541 PMD_INIT_LOG(ERR, "(%s) Failed to get backend features", dev->path);
545 if ((dev->device_features & (1ULL << VHOST_USER_F_PROTOCOL_FEATURES)) ||
546 dev->backend_type == VIRTIO_USER_BACKEND_VHOST_VDPA) {
547 if (dev->ops->get_protocol_features(dev, &protocol_features)) {
548 PMD_INIT_LOG(ERR, "(%s) Failed to get backend protocol features",
553 dev->protocol_features &= protocol_features;
555 if (dev->ops->set_protocol_features(dev, dev->protocol_features)) {
556 PMD_INIT_LOG(ERR, "(%s) Failed to set backend protocol features",
561 if (!(dev->protocol_features & (1ULL << VHOST_USER_PROTOCOL_F_MQ)))
562 dev->unsupported_features |= (1ull << VIRTIO_NET_F_MQ);
566 dev->unsupported_features |= (1ull << VIRTIO_NET_F_MRG_RXBUF);
569 dev->unsupported_features |= (1ull << VIRTIO_F_IN_ORDER);
572 dev->unsupported_features |= (1ull << VIRTIO_F_RING_PACKED);
574 if (dev->mac_specified)
575 dev->frontend_features |= (1ull << VIRTIO_NET_F_MAC);
577 dev->unsupported_features |= (1ull << VIRTIO_NET_F_MAC);
580 /* device does not really need to know anything about CQ,
581 * so if necessary, we just claim to support CQ
583 dev->frontend_features |= (1ull << VIRTIO_NET_F_CTRL_VQ);
585 dev->unsupported_features |= (1ull << VIRTIO_NET_F_CTRL_VQ);
586 /* Also disable features that depend on VIRTIO_NET_F_CTRL_VQ */
587 dev->unsupported_features |= (1ull << VIRTIO_NET_F_CTRL_RX);
588 dev->unsupported_features |= (1ull << VIRTIO_NET_F_CTRL_VLAN);
589 dev->unsupported_features |=
590 (1ull << VIRTIO_NET_F_GUEST_ANNOUNCE);
591 dev->unsupported_features |= (1ull << VIRTIO_NET_F_MQ);
592 dev->unsupported_features |=
593 (1ull << VIRTIO_NET_F_CTRL_MAC_ADDR);
596 /* The backend will not report this feature, we add it explicitly */
597 if (dev->backend_type == VIRTIO_USER_BACKEND_VHOST_USER)
598 dev->frontend_features |= (1ull << VIRTIO_NET_F_STATUS);
602 * (frontend_features | backend_features) & ~unsupported_features;
604 dev->device_features |= dev->frontend_features;
605 dev->device_features &= ~dev->unsupported_features;
607 if (rte_mem_event_callback_register(VIRTIO_USER_MEM_EVENT_CLB_NAME,
608 virtio_user_mem_event_cb, dev)) {
609 if (rte_errno != ENOTSUP) {
610 PMD_INIT_LOG(ERR, "(%s) Failed to register mem event callback\n",
620 virtio_user_dev_uninit(struct virtio_user_dev *dev)
624 virtio_user_stop_device(dev);
626 rte_mem_event_callback_unregister(VIRTIO_USER_MEM_EVENT_CLB_NAME, dev);
628 for (i = 0; i < dev->max_queue_pairs * 2; ++i) {
629 close(dev->callfds[i]);
630 close(dev->kickfds[i]);
633 if (dev->vhostfd >= 0)
636 if (dev->is_server && dev->listenfd >= 0) {
637 close(dev->listenfd);
642 for (i = 0; i < dev->max_queue_pairs; ++i) {
643 close(dev->vhostfds[i]);
644 if (dev->tapfds[i] >= 0)
645 close(dev->tapfds[i]);
658 virtio_user_handle_mq(struct virtio_user_dev *dev, uint16_t q_pairs)
663 if (q_pairs > dev->max_queue_pairs) {
664 PMD_INIT_LOG(ERR, "(%s) multi-q config %u, but only %u supported",
665 dev->path, q_pairs, dev->max_queue_pairs);
669 /* Server mode can't enable queue pairs if vhostfd is invalid,
670 * always return 0 in this case.
672 if (!dev->is_server || dev->vhostfd >= 0) {
673 for (i = 0; i < q_pairs; ++i)
674 ret |= dev->ops->enable_qp(dev, i, 1);
675 for (i = q_pairs; i < dev->max_queue_pairs; ++i)
676 ret |= dev->ops->enable_qp(dev, i, 0);
678 dev->queue_pairs = q_pairs;
684 virtio_user_handle_ctrl_msg(struct virtio_user_dev *dev, struct vring *vring,
687 struct virtio_net_ctrl_hdr *hdr;
688 virtio_net_ctrl_ack status = ~0;
689 uint16_t i, idx_data, idx_status;
690 uint32_t n_descs = 0;
692 /* locate desc for header, data, and status */
693 idx_data = vring->desc[idx_hdr].next;
697 while (vring->desc[i].flags == VRING_DESC_F_NEXT) {
698 i = vring->desc[i].next;
702 /* locate desc for status */
706 hdr = (void *)(uintptr_t)vring->desc[idx_hdr].addr;
707 if (hdr->class == VIRTIO_NET_CTRL_MQ &&
708 hdr->cmd == VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET) {
711 queues = *(uint16_t *)(uintptr_t)vring->desc[idx_data].addr;
712 status = virtio_user_handle_mq(dev, queues);
713 } else if (hdr->class == VIRTIO_NET_CTRL_RX ||
714 hdr->class == VIRTIO_NET_CTRL_MAC ||
715 hdr->class == VIRTIO_NET_CTRL_VLAN) {
720 *(virtio_net_ctrl_ack *)(uintptr_t)vring->desc[idx_status].addr = status;
726 desc_is_avail(struct vring_packed_desc *desc, bool wrap_counter)
728 uint16_t flags = __atomic_load_n(&desc->flags, __ATOMIC_ACQUIRE);
730 return wrap_counter == !!(flags & VRING_PACKED_DESC_F_AVAIL) &&
731 wrap_counter != !!(flags & VRING_PACKED_DESC_F_USED);
735 virtio_user_handle_ctrl_msg_packed(struct virtio_user_dev *dev,
736 struct vring_packed *vring,
739 struct virtio_net_ctrl_hdr *hdr;
740 virtio_net_ctrl_ack status = ~0;
741 uint16_t idx_data, idx_status;
742 /* initialize to one, header is first */
743 uint32_t n_descs = 1;
745 /* locate desc for header, data, and status */
746 idx_data = idx_hdr + 1;
747 if (idx_data >= dev->queue_size)
748 idx_data -= dev->queue_size;
752 idx_status = idx_data;
753 while (vring->desc[idx_status].flags & VRING_DESC_F_NEXT) {
755 if (idx_status >= dev->queue_size)
756 idx_status -= dev->queue_size;
760 hdr = (void *)(uintptr_t)vring->desc[idx_hdr].addr;
761 if (hdr->class == VIRTIO_NET_CTRL_MQ &&
762 hdr->cmd == VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET) {
765 queues = *(uint16_t *)(uintptr_t)
766 vring->desc[idx_data].addr;
767 status = virtio_user_handle_mq(dev, queues);
768 } else if (hdr->class == VIRTIO_NET_CTRL_RX ||
769 hdr->class == VIRTIO_NET_CTRL_MAC ||
770 hdr->class == VIRTIO_NET_CTRL_VLAN) {
775 *(virtio_net_ctrl_ack *)(uintptr_t)
776 vring->desc[idx_status].addr = status;
778 /* Update used descriptor */
779 vring->desc[idx_hdr].id = vring->desc[idx_status].id;
780 vring->desc[idx_hdr].len = sizeof(status);
786 virtio_user_handle_cq_packed(struct virtio_user_dev *dev, uint16_t queue_idx)
788 struct virtio_user_queue *vq = &dev->packed_queues[queue_idx];
789 struct vring_packed *vring = &dev->packed_vrings[queue_idx];
790 uint16_t n_descs, flags;
792 /* Perform a load-acquire barrier in desc_is_avail to
793 * enforce the ordering between desc flags and desc
796 while (desc_is_avail(&vring->desc[vq->used_idx],
797 vq->used_wrap_counter)) {
799 n_descs = virtio_user_handle_ctrl_msg_packed(dev, vring,
802 flags = VRING_DESC_F_WRITE;
803 if (vq->used_wrap_counter)
804 flags |= VRING_PACKED_DESC_F_AVAIL_USED;
806 __atomic_store_n(&vring->desc[vq->used_idx].flags, flags,
809 vq->used_idx += n_descs;
810 if (vq->used_idx >= dev->queue_size) {
811 vq->used_idx -= dev->queue_size;
812 vq->used_wrap_counter ^= 1;
818 virtio_user_handle_cq(struct virtio_user_dev *dev, uint16_t queue_idx)
820 uint16_t avail_idx, desc_idx;
821 struct vring_used_elem *uep;
823 struct vring *vring = &dev->vrings[queue_idx];
825 /* Consume avail ring, using used ring idx as first one */
826 while (__atomic_load_n(&vring->used->idx, __ATOMIC_RELAXED)
827 != vring->avail->idx) {
828 avail_idx = __atomic_load_n(&vring->used->idx, __ATOMIC_RELAXED)
830 desc_idx = vring->avail->ring[avail_idx];
832 n_descs = virtio_user_handle_ctrl_msg(dev, vring, desc_idx);
834 /* Update used ring */
835 uep = &vring->used->ring[avail_idx];
839 __atomic_add_fetch(&vring->used->idx, 1, __ATOMIC_RELAXED);
844 virtio_user_dev_set_status(struct virtio_user_dev *dev, uint8_t status)
848 pthread_mutex_lock(&dev->mutex);
849 dev->status = status;
850 ret = dev->ops->set_status(dev, status);
851 if (ret && ret != -ENOTSUP)
852 PMD_INIT_LOG(ERR, "(%s) Failed to set backend status\n", dev->path);
854 pthread_mutex_unlock(&dev->mutex);
859 virtio_user_dev_update_status(struct virtio_user_dev *dev)
864 pthread_mutex_lock(&dev->mutex);
866 ret = dev->ops->get_status(dev, &status);
868 dev->status = status;
869 PMD_INIT_LOG(DEBUG, "Updated Device Status(0x%08x):\n"
871 "\t-ACKNOWLEDGE: %u\n"
874 "\t-FEATURES_OK: %u\n"
875 "\t-DEVICE_NEED_RESET: %u\n"
878 (dev->status == VIRTIO_CONFIG_STATUS_RESET),
879 !!(dev->status & VIRTIO_CONFIG_STATUS_ACK),
880 !!(dev->status & VIRTIO_CONFIG_STATUS_DRIVER),
881 !!(dev->status & VIRTIO_CONFIG_STATUS_DRIVER_OK),
882 !!(dev->status & VIRTIO_CONFIG_STATUS_FEATURES_OK),
883 !!(dev->status & VIRTIO_CONFIG_STATUS_DEV_NEED_RESET),
884 !!(dev->status & VIRTIO_CONFIG_STATUS_FAILED));
885 } else if (ret != -ENOTSUP) {
886 PMD_INIT_LOG(ERR, "(%s) Failed to get backend status\n", dev->path);
889 pthread_mutex_unlock(&dev->mutex);