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;
41 file.index = queue_sel;
42 file.fd = dev->callfds[queue_sel];
43 dev->ops->send_request(dev, VHOST_USER_SET_VRING_CALL, &file);
49 virtio_user_kick_queue(struct virtio_user_dev *dev, uint32_t queue_sel)
51 struct vhost_vring_file file;
52 struct vhost_vring_state state;
53 struct vring *vring = &dev->vrings[queue_sel];
54 struct vring_packed *pq_vring = &dev->packed_vrings[queue_sel];
55 struct vhost_vring_addr addr = {
58 .flags = 0, /* disable log */
61 if (dev->features & (1ULL << VIRTIO_F_RING_PACKED)) {
63 (uint64_t)(uintptr_t)pq_vring->desc;
64 addr.avail_user_addr =
65 (uint64_t)(uintptr_t)pq_vring->driver;
67 (uint64_t)(uintptr_t)pq_vring->device;
69 addr.desc_user_addr = (uint64_t)(uintptr_t)vring->desc;
70 addr.avail_user_addr = (uint64_t)(uintptr_t)vring->avail;
71 addr.used_user_addr = (uint64_t)(uintptr_t)vring->used;
74 state.index = queue_sel;
75 state.num = vring->num;
76 dev->ops->send_request(dev, VHOST_USER_SET_VRING_NUM, &state);
78 state.index = queue_sel;
79 state.num = 0; /* no reservation */
80 if (dev->features & (1ULL << VIRTIO_F_RING_PACKED))
81 state.num |= (1 << 15);
82 dev->ops->send_request(dev, VHOST_USER_SET_VRING_BASE, &state);
84 dev->ops->send_request(dev, VHOST_USER_SET_VRING_ADDR, &addr);
86 /* Of all per virtqueue MSGs, make sure VHOST_USER_SET_VRING_KICK comes
87 * lastly because vhost depends on this msg to judge if
90 file.index = queue_sel;
91 file.fd = dev->kickfds[queue_sel];
92 dev->ops->send_request(dev, VHOST_USER_SET_VRING_KICK, &file);
98 virtio_user_queue_setup(struct virtio_user_dev *dev,
99 int (*fn)(struct virtio_user_dev *, uint32_t))
101 uint32_t i, queue_sel;
103 for (i = 0; i < dev->max_queue_pairs; ++i) {
104 queue_sel = 2 * i + VTNET_SQ_RQ_QUEUE_IDX;
105 if (fn(dev, queue_sel) < 0) {
106 PMD_DRV_LOG(INFO, "setup rx vq fails: %u", i);
110 for (i = 0; i < dev->max_queue_pairs; ++i) {
111 queue_sel = 2 * i + VTNET_SQ_TQ_QUEUE_IDX;
112 if (fn(dev, queue_sel) < 0) {
113 PMD_DRV_LOG(INFO, "setup tx vq fails: %u", i);
122 virtio_user_dev_set_features(struct virtio_user_dev *dev)
127 pthread_mutex_lock(&dev->mutex);
129 if (dev->backend_type == VIRTIO_USER_BACKEND_VHOST_USER &&
133 /* Step 0: tell vhost to create queues */
134 if (virtio_user_queue_setup(dev, virtio_user_create_queue) < 0)
137 features = dev->features;
139 /* Strip VIRTIO_NET_F_MAC, as MAC address is handled in vdev init */
140 features &= ~(1ull << VIRTIO_NET_F_MAC);
141 /* Strip VIRTIO_NET_F_CTRL_VQ, as devices do not really need to know */
142 features &= ~(1ull << VIRTIO_NET_F_CTRL_VQ);
143 features &= ~(1ull << VIRTIO_NET_F_STATUS);
144 ret = dev->ops->send_request(dev, VHOST_USER_SET_FEATURES, &features);
147 PMD_DRV_LOG(INFO, "set features: %" PRIx64, features);
149 pthread_mutex_unlock(&dev->mutex);
155 virtio_user_start_device(struct virtio_user_dev *dev)
162 * We need to make sure that the locks will be
163 * taken in the correct order to avoid deadlocks.
165 * Before releasing this lock, this thread should
166 * not trigger any memory hotplug events.
168 * This is a temporary workaround, and should be
169 * replaced when we get proper supports from the
170 * memory subsystem in the future.
172 rte_mcfg_mem_read_lock();
173 pthread_mutex_lock(&dev->mutex);
175 if (dev->backend_type == VIRTIO_USER_BACKEND_VHOST_USER &&
179 /* Step 2: share memory regions */
180 ret = dev->ops->send_request(dev, VHOST_USER_SET_MEM_TABLE, NULL);
184 /* Step 3: kick queues */
185 if (virtio_user_queue_setup(dev, virtio_user_kick_queue) < 0)
188 /* Step 4: enable queues
189 * we enable the 1st queue pair by default.
191 dev->ops->enable_qp(dev, 0, 1);
194 pthread_mutex_unlock(&dev->mutex);
195 rte_mcfg_mem_read_unlock();
199 pthread_mutex_unlock(&dev->mutex);
200 rte_mcfg_mem_read_unlock();
201 /* TODO: free resource here or caller to check */
205 int virtio_user_stop_device(struct virtio_user_dev *dev)
207 struct vhost_vring_state state;
211 pthread_mutex_lock(&dev->mutex);
215 for (i = 0; i < dev->max_queue_pairs; ++i)
216 dev->ops->enable_qp(dev, i, 0);
218 /* Stop the backend. */
219 for (i = 0; i < dev->max_queue_pairs * 2; ++i) {
221 if (dev->ops->send_request(dev, VHOST_USER_GET_VRING_BASE,
223 PMD_DRV_LOG(ERR, "get_vring_base failed, index=%u\n",
230 dev->started = false;
232 pthread_mutex_unlock(&dev->mutex);
238 parse_mac(struct virtio_user_dev *dev, const char *mac)
240 struct rte_ether_addr tmp;
245 if (rte_ether_unformat_addr(mac, &tmp) == 0) {
246 memcpy(dev->mac_addr, &tmp, RTE_ETHER_ADDR_LEN);
247 dev->mac_specified = 1;
249 /* ignore the wrong mac, use random mac */
250 PMD_DRV_LOG(ERR, "wrong format of mac: %s", mac);
255 virtio_user_dev_init_notify(struct virtio_user_dev *dev)
261 for (i = 0; i < VIRTIO_MAX_VIRTQUEUES; ++i) {
262 if (i >= dev->max_queue_pairs * 2) {
263 dev->kickfds[i] = -1;
264 dev->callfds[i] = -1;
268 /* May use invalid flag, but some backend uses kickfd and
269 * callfd as criteria to judge if dev is alive. so finally we
272 callfd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);
274 PMD_DRV_LOG(ERR, "callfd error, %s", strerror(errno));
277 kickfd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);
280 PMD_DRV_LOG(ERR, "kickfd error, %s", strerror(errno));
283 dev->callfds[i] = callfd;
284 dev->kickfds[i] = kickfd;
287 if (i < VIRTIO_MAX_VIRTQUEUES) {
288 for (j = 0; j < i; ++j) {
289 close(dev->callfds[j]);
290 close(dev->kickfds[j]);
300 virtio_user_fill_intr_handle(struct virtio_user_dev *dev)
303 struct rte_eth_dev *eth_dev = &rte_eth_devices[dev->port_id];
305 if (!eth_dev->intr_handle) {
306 eth_dev->intr_handle = malloc(sizeof(*eth_dev->intr_handle));
307 if (!eth_dev->intr_handle) {
308 PMD_DRV_LOG(ERR, "fail to allocate intr_handle");
311 memset(eth_dev->intr_handle, 0, sizeof(*eth_dev->intr_handle));
314 for (i = 0; i < dev->max_queue_pairs; ++i)
315 eth_dev->intr_handle->efds[i] = dev->callfds[i];
316 eth_dev->intr_handle->nb_efd = dev->max_queue_pairs;
317 eth_dev->intr_handle->max_intr = dev->max_queue_pairs + 1;
318 eth_dev->intr_handle->type = RTE_INTR_HANDLE_VDEV;
319 /* For virtio vdev, no need to read counter for clean */
320 eth_dev->intr_handle->efd_counter_size = 0;
321 eth_dev->intr_handle->fd = -1;
322 if (dev->vhostfd >= 0)
323 eth_dev->intr_handle->fd = dev->vhostfd;
324 else if (dev->is_server)
325 eth_dev->intr_handle->fd = dev->listenfd;
331 virtio_user_mem_event_cb(enum rte_mem_event type __rte_unused,
333 size_t len __rte_unused,
336 struct virtio_user_dev *dev = arg;
337 struct rte_memseg_list *msl;
340 /* ignore externally allocated memory */
341 msl = rte_mem_virt2memseg_list(addr);
345 pthread_mutex_lock(&dev->mutex);
347 if (dev->started == false)
350 /* Step 1: pause the active queues */
351 for (i = 0; i < dev->queue_pairs; i++)
352 dev->ops->enable_qp(dev, i, 0);
354 /* Step 2: update memory regions */
355 dev->ops->send_request(dev, VHOST_USER_SET_MEM_TABLE, NULL);
357 /* Step 3: resume the active queues */
358 for (i = 0; i < dev->queue_pairs; i++)
359 dev->ops->enable_qp(dev, i, 1);
362 pthread_mutex_unlock(&dev->mutex);
366 virtio_user_dev_setup(struct virtio_user_dev *dev)
371 dev->vhostfds = NULL;
374 if (dev->is_server) {
375 if (dev->backend_type != VIRTIO_USER_BACKEND_VHOST_USER) {
376 PMD_DRV_LOG(ERR, "Server mode only supports vhost-user!");
379 dev->ops = &virtio_ops_user;
381 if (dev->backend_type == VIRTIO_USER_BACKEND_VHOST_USER) {
382 dev->ops = &virtio_ops_user;
383 } else if (dev->backend_type ==
384 VIRTIO_USER_BACKEND_VHOST_KERNEL) {
385 dev->ops = &virtio_ops_kernel;
387 dev->vhostfds = malloc(dev->max_queue_pairs *
389 dev->tapfds = malloc(dev->max_queue_pairs *
391 if (!dev->vhostfds || !dev->tapfds) {
392 PMD_INIT_LOG(ERR, "Failed to malloc");
396 for (q = 0; q < dev->max_queue_pairs; ++q) {
397 dev->vhostfds[q] = -1;
400 } else if (dev->backend_type ==
401 VIRTIO_USER_BACKEND_VHOST_VDPA) {
402 dev->ops = &virtio_ops_vdpa;
404 PMD_DRV_LOG(ERR, "Unknown backend type");
409 if (dev->ops->setup(dev) < 0)
412 if (virtio_user_dev_init_notify(dev) < 0)
415 if (virtio_user_fill_intr_handle(dev) < 0)
421 /* Use below macro to filter features from vhost backend */
422 #define VIRTIO_USER_SUPPORTED_FEATURES \
423 (1ULL << VIRTIO_NET_F_MAC | \
424 1ULL << VIRTIO_NET_F_STATUS | \
425 1ULL << VIRTIO_NET_F_MQ | \
426 1ULL << VIRTIO_NET_F_CTRL_MAC_ADDR | \
427 1ULL << VIRTIO_NET_F_CTRL_VQ | \
428 1ULL << VIRTIO_NET_F_CTRL_RX | \
429 1ULL << VIRTIO_NET_F_CTRL_VLAN | \
430 1ULL << VIRTIO_NET_F_CSUM | \
431 1ULL << VIRTIO_NET_F_HOST_TSO4 | \
432 1ULL << VIRTIO_NET_F_HOST_TSO6 | \
433 1ULL << VIRTIO_NET_F_MRG_RXBUF | \
434 1ULL << VIRTIO_RING_F_INDIRECT_DESC | \
435 1ULL << VIRTIO_NET_F_GUEST_CSUM | \
436 1ULL << VIRTIO_NET_F_GUEST_TSO4 | \
437 1ULL << VIRTIO_NET_F_GUEST_TSO6 | \
438 1ULL << VIRTIO_F_IN_ORDER | \
439 1ULL << VIRTIO_F_VERSION_1 | \
440 1ULL << VIRTIO_F_RING_PACKED | \
441 1ULL << VHOST_USER_F_PROTOCOL_FEATURES)
443 #define VHOST_USER_SUPPORTED_PROTOCOL_FEATURES \
444 (1ULL << VHOST_USER_PROTOCOL_F_MQ | \
445 1ULL << VHOST_USER_PROTOCOL_F_REPLY_ACK | \
446 1ULL << VHOST_USER_PROTOCOL_F_STATUS)
448 #define VHOST_VDPA_SUPPORTED_PROTOCOL_FEATURES \
449 (1ULL << VHOST_BACKEND_F_IOTLB_MSG_V2 | \
450 1ULL << VHOST_BACKEND_F_IOTLB_BATCH)
452 virtio_user_dev_init(struct virtio_user_dev *dev, char *path, int queues,
453 int cq, int queue_size, const char *mac, char **ifname,
454 int server, int mrg_rxbuf, int in_order, int packed_vq,
455 enum virtio_user_backend_type backend_type)
457 uint64_t protocol_features = 0;
459 pthread_mutex_init(&dev->mutex, NULL);
460 strlcpy(dev->path, path, PATH_MAX);
462 dev->max_queue_pairs = queues;
463 dev->queue_pairs = 1; /* mq disabled by default */
464 dev->queue_size = queue_size;
465 dev->is_server = server;
466 dev->mac_specified = 0;
467 dev->frontend_features = 0;
468 dev->unsupported_features = ~VIRTIO_USER_SUPPORTED_FEATURES;
469 dev->backend_type = backend_type;
471 if (dev->backend_type == VIRTIO_USER_BACKEND_VHOST_USER)
472 dev->protocol_features = VHOST_USER_SUPPORTED_PROTOCOL_FEATURES;
473 else if (dev->backend_type == VIRTIO_USER_BACKEND_VHOST_VDPA)
474 dev->protocol_features = VHOST_VDPA_SUPPORTED_PROTOCOL_FEATURES;
479 dev->ifname = *ifname;
483 if (virtio_user_dev_setup(dev) < 0) {
484 PMD_INIT_LOG(ERR, "backend set up fails");
488 if (dev->backend_type != VIRTIO_USER_BACKEND_VHOST_USER)
489 dev->unsupported_features |=
490 (1ULL << VHOST_USER_F_PROTOCOL_FEATURES);
492 if (!dev->is_server) {
493 if (dev->ops->send_request(dev, VHOST_USER_SET_OWNER,
495 PMD_INIT_LOG(ERR, "set_owner fails: %s",
500 if (dev->ops->send_request(dev, VHOST_USER_GET_FEATURES,
501 &dev->device_features) < 0) {
502 PMD_INIT_LOG(ERR, "get_features failed: %s",
508 if ((dev->device_features & (1ULL << VHOST_USER_F_PROTOCOL_FEATURES)) ||
509 (dev->backend_type == VIRTIO_USER_BACKEND_VHOST_VDPA)) {
510 if (dev->ops->send_request(dev,
511 VHOST_USER_GET_PROTOCOL_FEATURES,
515 dev->protocol_features &= protocol_features;
517 if (dev->ops->send_request(dev,
518 VHOST_USER_SET_PROTOCOL_FEATURES,
519 &dev->protocol_features))
522 if (!(dev->protocol_features &
523 (1ULL << VHOST_USER_PROTOCOL_F_MQ)))
524 dev->unsupported_features |=
525 (1ull << VIRTIO_NET_F_MQ);
528 /* We just pretend vhost-user can support all these features.
529 * Note that this could be problematic that if some feature is
530 * negotiated but not supported by the vhost-user which comes
533 dev->device_features = VIRTIO_USER_SUPPORTED_FEATURES;
535 /* We cannot assume VHOST_USER_PROTOCOL_F_STATUS is supported
536 * until it's negotiated
538 dev->protocol_features &=
539 ~(1ULL << VHOST_USER_PROTOCOL_F_STATUS);
545 dev->unsupported_features |= (1ull << VIRTIO_NET_F_MRG_RXBUF);
548 dev->unsupported_features |= (1ull << VIRTIO_F_IN_ORDER);
551 dev->unsupported_features |= (1ull << VIRTIO_F_RING_PACKED);
553 if (dev->mac_specified)
554 dev->frontend_features |= (1ull << VIRTIO_NET_F_MAC);
556 dev->unsupported_features |= (1ull << VIRTIO_NET_F_MAC);
559 /* device does not really need to know anything about CQ,
560 * so if necessary, we just claim to support CQ
562 dev->frontend_features |= (1ull << VIRTIO_NET_F_CTRL_VQ);
564 dev->unsupported_features |= (1ull << VIRTIO_NET_F_CTRL_VQ);
565 /* Also disable features that depend on VIRTIO_NET_F_CTRL_VQ */
566 dev->unsupported_features |= (1ull << VIRTIO_NET_F_CTRL_RX);
567 dev->unsupported_features |= (1ull << VIRTIO_NET_F_CTRL_VLAN);
568 dev->unsupported_features |=
569 (1ull << VIRTIO_NET_F_GUEST_ANNOUNCE);
570 dev->unsupported_features |= (1ull << VIRTIO_NET_F_MQ);
571 dev->unsupported_features |=
572 (1ull << VIRTIO_NET_F_CTRL_MAC_ADDR);
575 /* The backend will not report this feature, we add it explicitly */
576 if (dev->backend_type == VIRTIO_USER_BACKEND_VHOST_USER)
577 dev->frontend_features |= (1ull << VIRTIO_NET_F_STATUS);
581 * (frontend_features | backend_features) & ~unsupported_features;
583 dev->device_features |= dev->frontend_features;
584 dev->device_features &= ~dev->unsupported_features;
586 if (rte_mem_event_callback_register(VIRTIO_USER_MEM_EVENT_CLB_NAME,
587 virtio_user_mem_event_cb, dev)) {
588 if (rte_errno != ENOTSUP) {
589 PMD_INIT_LOG(ERR, "Failed to register mem event"
599 virtio_user_dev_uninit(struct virtio_user_dev *dev)
603 virtio_user_stop_device(dev);
605 rte_mem_event_callback_unregister(VIRTIO_USER_MEM_EVENT_CLB_NAME, dev);
607 for (i = 0; i < dev->max_queue_pairs * 2; ++i) {
608 close(dev->callfds[i]);
609 close(dev->kickfds[i]);
612 if (dev->vhostfd >= 0)
615 if (dev->is_server && dev->listenfd >= 0) {
616 close(dev->listenfd);
621 for (i = 0; i < dev->max_queue_pairs; ++i) {
622 close(dev->vhostfds[i]);
623 if (dev->tapfds[i] >= 0)
624 close(dev->tapfds[i]);
637 virtio_user_handle_mq(struct virtio_user_dev *dev, uint16_t q_pairs)
642 if (q_pairs > dev->max_queue_pairs) {
643 PMD_INIT_LOG(ERR, "multi-q config %u, but only %u supported",
644 q_pairs, dev->max_queue_pairs);
648 /* Server mode can't enable queue pairs if vhostfd is invalid,
649 * always return 0 in this case.
651 if (!dev->is_server || dev->vhostfd >= 0) {
652 for (i = 0; i < q_pairs; ++i)
653 ret |= dev->ops->enable_qp(dev, i, 1);
654 for (i = q_pairs; i < dev->max_queue_pairs; ++i)
655 ret |= dev->ops->enable_qp(dev, i, 0);
657 dev->queue_pairs = q_pairs;
663 virtio_user_handle_ctrl_msg(struct virtio_user_dev *dev, struct vring *vring,
666 struct virtio_net_ctrl_hdr *hdr;
667 virtio_net_ctrl_ack status = ~0;
668 uint16_t i, idx_data, idx_status;
669 uint32_t n_descs = 0;
671 /* locate desc for header, data, and status */
672 idx_data = vring->desc[idx_hdr].next;
676 while (vring->desc[i].flags == VRING_DESC_F_NEXT) {
677 i = vring->desc[i].next;
681 /* locate desc for status */
685 hdr = (void *)(uintptr_t)vring->desc[idx_hdr].addr;
686 if (hdr->class == VIRTIO_NET_CTRL_MQ &&
687 hdr->cmd == VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET) {
690 queues = *(uint16_t *)(uintptr_t)vring->desc[idx_data].addr;
691 status = virtio_user_handle_mq(dev, queues);
692 } else if (hdr->class == VIRTIO_NET_CTRL_RX ||
693 hdr->class == VIRTIO_NET_CTRL_MAC ||
694 hdr->class == VIRTIO_NET_CTRL_VLAN) {
699 *(virtio_net_ctrl_ack *)(uintptr_t)vring->desc[idx_status].addr = status;
705 desc_is_avail(struct vring_packed_desc *desc, bool wrap_counter)
707 uint16_t flags = __atomic_load_n(&desc->flags, __ATOMIC_ACQUIRE);
709 return wrap_counter == !!(flags & VRING_PACKED_DESC_F_AVAIL) &&
710 wrap_counter != !!(flags & VRING_PACKED_DESC_F_USED);
714 virtio_user_handle_ctrl_msg_packed(struct virtio_user_dev *dev,
715 struct vring_packed *vring,
718 struct virtio_net_ctrl_hdr *hdr;
719 virtio_net_ctrl_ack status = ~0;
720 uint16_t idx_data, idx_status;
721 /* initialize to one, header is first */
722 uint32_t n_descs = 1;
724 /* locate desc for header, data, and status */
725 idx_data = idx_hdr + 1;
726 if (idx_data >= dev->queue_size)
727 idx_data -= dev->queue_size;
731 idx_status = idx_data;
732 while (vring->desc[idx_status].flags & VRING_DESC_F_NEXT) {
734 if (idx_status >= dev->queue_size)
735 idx_status -= dev->queue_size;
739 hdr = (void *)(uintptr_t)vring->desc[idx_hdr].addr;
740 if (hdr->class == VIRTIO_NET_CTRL_MQ &&
741 hdr->cmd == VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET) {
744 queues = *(uint16_t *)(uintptr_t)
745 vring->desc[idx_data].addr;
746 status = virtio_user_handle_mq(dev, queues);
747 } else if (hdr->class == VIRTIO_NET_CTRL_RX ||
748 hdr->class == VIRTIO_NET_CTRL_MAC ||
749 hdr->class == VIRTIO_NET_CTRL_VLAN) {
754 *(virtio_net_ctrl_ack *)(uintptr_t)
755 vring->desc[idx_status].addr = status;
757 /* Update used descriptor */
758 vring->desc[idx_hdr].id = vring->desc[idx_status].id;
759 vring->desc[idx_hdr].len = sizeof(status);
765 virtio_user_handle_cq_packed(struct virtio_user_dev *dev, uint16_t queue_idx)
767 struct virtio_user_queue *vq = &dev->packed_queues[queue_idx];
768 struct vring_packed *vring = &dev->packed_vrings[queue_idx];
769 uint16_t n_descs, flags;
771 /* Perform a load-acquire barrier in desc_is_avail to
772 * enforce the ordering between desc flags and desc
775 while (desc_is_avail(&vring->desc[vq->used_idx],
776 vq->used_wrap_counter)) {
778 n_descs = virtio_user_handle_ctrl_msg_packed(dev, vring,
781 flags = VRING_DESC_F_WRITE;
782 if (vq->used_wrap_counter)
783 flags |= VRING_PACKED_DESC_F_AVAIL_USED;
785 __atomic_store_n(&vring->desc[vq->used_idx].flags, flags,
788 vq->used_idx += n_descs;
789 if (vq->used_idx >= dev->queue_size) {
790 vq->used_idx -= dev->queue_size;
791 vq->used_wrap_counter ^= 1;
797 virtio_user_handle_cq(struct virtio_user_dev *dev, uint16_t queue_idx)
799 uint16_t avail_idx, desc_idx;
800 struct vring_used_elem *uep;
802 struct vring *vring = &dev->vrings[queue_idx];
804 /* Consume avail ring, using used ring idx as first one */
805 while (__atomic_load_n(&vring->used->idx, __ATOMIC_RELAXED)
806 != vring->avail->idx) {
807 avail_idx = __atomic_load_n(&vring->used->idx, __ATOMIC_RELAXED)
809 desc_idx = vring->avail->ring[avail_idx];
811 n_descs = virtio_user_handle_ctrl_msg(dev, vring, desc_idx);
813 /* Update used ring */
814 uep = &vring->used->ring[avail_idx];
818 __atomic_add_fetch(&vring->used->idx, 1, __ATOMIC_RELAXED);
823 virtio_user_dev_set_status(struct virtio_user_dev *dev, uint8_t status)
826 uint64_t arg = status;
828 pthread_mutex_lock(&dev->mutex);
829 dev->status = status;
830 if (dev->backend_type == VIRTIO_USER_BACKEND_VHOST_USER)
831 ret = dev->ops->send_request(dev,
832 VHOST_USER_SET_STATUS, &arg);
833 else if (dev->backend_type == VIRTIO_USER_BACKEND_VHOST_VDPA)
834 ret = dev->ops->send_request(dev,
835 VHOST_USER_SET_STATUS, &status);
839 if (ret && ret != -ENOTSUP) {
840 PMD_INIT_LOG(ERR, "VHOST_USER_SET_STATUS failed (%d): %s", ret,
844 pthread_mutex_unlock(&dev->mutex);
849 virtio_user_dev_update_status(struct virtio_user_dev *dev)
855 pthread_mutex_lock(&dev->mutex);
856 if (dev->backend_type == VIRTIO_USER_BACKEND_VHOST_USER) {
857 err = dev->ops->send_request(dev, VHOST_USER_GET_STATUS, &ret);
858 if (!err && ret > UINT8_MAX) {
859 PMD_INIT_LOG(ERR, "Invalid VHOST_USER_GET_STATUS "
860 "response 0x%" PRIx64 "\n", ret);
866 } else if (dev->backend_type == VIRTIO_USER_BACKEND_VHOST_VDPA) {
867 err = dev->ops->send_request(dev, VHOST_USER_GET_STATUS,
874 dev->status = status;
875 PMD_INIT_LOG(DEBUG, "Updated Device Status(0x%08x):\n"
877 "\t-ACKNOWLEDGE: %u\n"
880 "\t-FEATURES_OK: %u\n"
881 "\t-DEVICE_NEED_RESET: %u\n"
884 (dev->status == VIRTIO_CONFIG_STATUS_RESET),
885 !!(dev->status & VIRTIO_CONFIG_STATUS_ACK),
886 !!(dev->status & VIRTIO_CONFIG_STATUS_DRIVER),
887 !!(dev->status & VIRTIO_CONFIG_STATUS_DRIVER_OK),
888 !!(dev->status & VIRTIO_CONFIG_STATUS_FEATURES_OK),
889 !!(dev->status & VIRTIO_CONFIG_STATUS_DEV_NEED_RESET),
890 !!(dev->status & VIRTIO_CONFIG_STATUS_FAILED));
891 } else if (err != -ENOTSUP) {
892 PMD_INIT_LOG(ERR, "VHOST_USER_GET_STATUS failed (%d): %s", err,
897 pthread_mutex_unlock(&dev->mutex);