1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2017 Intel Corporation
5 #include <linux/vhost.h>
6 #include <linux/virtio_net.h>
9 #ifdef RTE_LIBRTE_VHOST_NUMA
14 #include <rte_errno.h>
16 #include <rte_memory.h>
17 #include <rte_malloc.h>
18 #include <rte_vhost.h>
22 #include "vhost_user.h"
24 struct virtio_net *vhost_devices[RTE_MAX_VHOST_DEVICE];
25 pthread_mutex_t vhost_dev_lock = PTHREAD_MUTEX_INITIALIZER;
27 /* Called with iotlb_lock read-locked */
29 __vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
30 uint64_t iova, uint64_t *size, uint8_t perm)
32 uint64_t vva, tmp_size;
39 vva = vhost_user_iotlb_cache_find(vq, iova, &tmp_size, perm);
40 if (tmp_size == *size)
45 if (!vhost_user_iotlb_pending_miss(vq, iova, perm)) {
47 * iotlb_lock is read-locked for a full burst,
48 * but it only protects the iotlb cache.
49 * In case of IOTLB miss, we might block on the socket,
50 * which could cause a deadlock with QEMU if an IOTLB update
51 * is being handled. We can safely unlock here to avoid it.
53 vhost_user_iotlb_rd_unlock(vq);
55 vhost_user_iotlb_pending_insert(dev, vq, iova, perm);
56 if (vhost_user_iotlb_miss(dev, iova, perm)) {
57 VHOST_LOG_DATA(ERR, "(%s) IOTLB miss req failed for IOVA 0x%" PRIx64 "\n",
59 vhost_user_iotlb_pending_remove(vq, iova, 1, perm);
62 vhost_user_iotlb_rd_lock(vq);
68 #define VHOST_LOG_PAGE 4096
71 * Atomically set a bit in memory.
73 static __rte_always_inline void
74 vhost_set_bit(unsigned int nr, volatile uint8_t *addr)
76 #if defined(RTE_TOOLCHAIN_GCC) && (GCC_VERSION < 70100)
78 * __sync_ built-ins are deprecated, but __atomic_ ones
79 * are sub-optimized in older GCC versions.
81 __sync_fetch_and_or_1(addr, (1U << nr));
83 __atomic_fetch_or(addr, (1U << nr), __ATOMIC_RELAXED);
87 static __rte_always_inline void
88 vhost_log_page(uint8_t *log_base, uint64_t page)
90 vhost_set_bit(page % 8, &log_base[page / 8]);
94 __vhost_log_write(struct virtio_net *dev, uint64_t addr, uint64_t len)
98 if (unlikely(!dev->log_base || !len))
101 if (unlikely(dev->log_size <= ((addr + len - 1) / VHOST_LOG_PAGE / 8)))
104 /* To make sure guest memory updates are committed before logging */
105 rte_atomic_thread_fence(__ATOMIC_RELEASE);
107 page = addr / VHOST_LOG_PAGE;
108 while (page * VHOST_LOG_PAGE < addr + len) {
109 vhost_log_page((uint8_t *)(uintptr_t)dev->log_base, page);
115 __vhost_log_write_iova(struct virtio_net *dev, struct vhost_virtqueue *vq,
116 uint64_t iova, uint64_t len)
118 uint64_t hva, gpa, map_len;
121 hva = __vhost_iova_to_vva(dev, vq, iova, &map_len, VHOST_ACCESS_RW);
122 if (map_len != len) {
124 "(%s) failed to write log for IOVA 0x%" PRIx64 ". No IOTLB entry found\n",
129 gpa = hva_to_gpa(dev, hva, len);
131 __vhost_log_write(dev, gpa, len);
135 __vhost_log_cache_sync(struct virtio_net *dev, struct vhost_virtqueue *vq)
137 unsigned long *log_base;
140 if (unlikely(!dev->log_base))
143 /* No cache, nothing to sync */
144 if (unlikely(!vq->log_cache))
147 rte_atomic_thread_fence(__ATOMIC_RELEASE);
149 log_base = (unsigned long *)(uintptr_t)dev->log_base;
151 for (i = 0; i < vq->log_cache_nb_elem; i++) {
152 struct log_cache_entry *elem = vq->log_cache + i;
154 #if defined(RTE_TOOLCHAIN_GCC) && (GCC_VERSION < 70100)
156 * '__sync' builtins are deprecated, but '__atomic' ones
157 * are sub-optimized in older GCC versions.
159 __sync_fetch_and_or(log_base + elem->offset, elem->val);
161 __atomic_fetch_or(log_base + elem->offset, elem->val,
166 rte_atomic_thread_fence(__ATOMIC_RELEASE);
168 vq->log_cache_nb_elem = 0;
171 static __rte_always_inline void
172 vhost_log_cache_page(struct virtio_net *dev, struct vhost_virtqueue *vq,
175 uint32_t bit_nr = page % (sizeof(unsigned long) << 3);
176 uint32_t offset = page / (sizeof(unsigned long) << 3);
179 if (unlikely(!vq->log_cache)) {
180 /* No logging cache allocated, write dirty log map directly */
181 rte_atomic_thread_fence(__ATOMIC_RELEASE);
182 vhost_log_page((uint8_t *)(uintptr_t)dev->log_base, page);
187 for (i = 0; i < vq->log_cache_nb_elem; i++) {
188 struct log_cache_entry *elem = vq->log_cache + i;
190 if (elem->offset == offset) {
191 elem->val |= (1UL << bit_nr);
196 if (unlikely(i >= VHOST_LOG_CACHE_NR)) {
198 * No more room for a new log cache entry,
199 * so write the dirty log map directly.
201 rte_atomic_thread_fence(__ATOMIC_RELEASE);
202 vhost_log_page((uint8_t *)(uintptr_t)dev->log_base, page);
207 vq->log_cache[i].offset = offset;
208 vq->log_cache[i].val = (1UL << bit_nr);
209 vq->log_cache_nb_elem++;
213 __vhost_log_cache_write(struct virtio_net *dev, struct vhost_virtqueue *vq,
214 uint64_t addr, uint64_t len)
218 if (unlikely(!dev->log_base || !len))
221 if (unlikely(dev->log_size <= ((addr + len - 1) / VHOST_LOG_PAGE / 8)))
224 page = addr / VHOST_LOG_PAGE;
225 while (page * VHOST_LOG_PAGE < addr + len) {
226 vhost_log_cache_page(dev, vq, page);
232 __vhost_log_cache_write_iova(struct virtio_net *dev, struct vhost_virtqueue *vq,
233 uint64_t iova, uint64_t len)
235 uint64_t hva, gpa, map_len;
238 hva = __vhost_iova_to_vva(dev, vq, iova, &map_len, VHOST_ACCESS_RW);
239 if (map_len != len) {
241 "(%s) failed to write log for IOVA 0x%" PRIx64 ". No IOTLB entry found\n",
246 gpa = hva_to_gpa(dev, hva, len);
248 __vhost_log_cache_write(dev, vq, gpa, len);
252 vhost_alloc_copy_ind_table(struct virtio_net *dev, struct vhost_virtqueue *vq,
253 uint64_t desc_addr, uint64_t desc_len)
257 uint64_t len, remain = desc_len;
259 idesc = rte_malloc_socket(__func__, desc_len, 0, vq->numa_node);
260 if (unlikely(!idesc))
263 dst = (uint64_t)(uintptr_t)idesc;
267 src = vhost_iova_to_vva(dev, vq, desc_addr, &len,
269 if (unlikely(!src || !len)) {
274 rte_memcpy((void *)(uintptr_t)dst, (void *)(uintptr_t)src, len);
285 cleanup_vq(struct vhost_virtqueue *vq, int destroy)
287 if ((vq->callfd >= 0) && (destroy != 0))
294 cleanup_vq_inflight(struct virtio_net *dev, struct vhost_virtqueue *vq)
296 if (!(dev->protocol_features &
297 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD)))
300 if (vq_is_packed(dev)) {
301 if (vq->inflight_packed)
302 vq->inflight_packed = NULL;
304 if (vq->inflight_split)
305 vq->inflight_split = NULL;
308 if (vq->resubmit_inflight) {
309 if (vq->resubmit_inflight->resubmit_list) {
310 rte_free(vq->resubmit_inflight->resubmit_list);
311 vq->resubmit_inflight->resubmit_list = NULL;
313 rte_free(vq->resubmit_inflight);
314 vq->resubmit_inflight = NULL;
319 * Unmap any memory, close any file descriptors and
320 * free any memory owned by a device.
323 cleanup_device(struct virtio_net *dev, int destroy)
327 vhost_backend_cleanup(dev);
329 for (i = 0; i < dev->nr_vring; i++) {
330 cleanup_vq(dev->virtqueue[i], destroy);
331 cleanup_vq_inflight(dev, dev->virtqueue[i]);
336 vhost_free_async_mem(struct vhost_virtqueue *vq)
341 rte_free(vq->async->pkts_info);
342 rte_free(vq->async->pkts_cmpl_flag);
344 rte_free(vq->async->buffers_packed);
345 vq->async->buffers_packed = NULL;
346 rte_free(vq->async->descs_split);
347 vq->async->descs_split = NULL;
354 free_vq(struct virtio_net *dev, struct vhost_virtqueue *vq)
356 if (vq_is_packed(dev))
357 rte_free(vq->shadow_used_packed);
359 rte_free(vq->shadow_used_split);
361 vhost_free_async_mem(vq);
362 rte_free(vq->batch_copy_elems);
363 rte_mempool_free(vq->iotlb_pool);
364 rte_free(vq->log_cache);
369 * Release virtqueues and device memory.
372 free_device(struct virtio_net *dev)
376 for (i = 0; i < dev->nr_vring; i++)
377 free_vq(dev, dev->virtqueue[i]);
382 static __rte_always_inline int
383 log_translate(struct virtio_net *dev, struct vhost_virtqueue *vq)
385 if (likely(!(vq->ring_addrs.flags & (1 << VHOST_VRING_F_LOG))))
388 vq->log_guest_addr = translate_log_addr(dev, vq,
389 vq->ring_addrs.log_guest_addr);
390 if (vq->log_guest_addr == 0)
397 * Converts vring log address to GPA
398 * If IOMMU is enabled, the log address is IOVA
399 * If IOMMU not enabled, the log address is already GPA
401 * Caller should have iotlb_lock read-locked
404 translate_log_addr(struct virtio_net *dev, struct vhost_virtqueue *vq,
407 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)) {
408 const uint64_t exp_size = sizeof(uint64_t);
410 uint64_t size = exp_size;
412 hva = vhost_iova_to_vva(dev, vq, log_addr,
413 &size, VHOST_ACCESS_RW);
415 if (size != exp_size)
418 gpa = hva_to_gpa(dev, hva, exp_size);
421 "(%s) failed to find GPA for log_addr: 0x%"
422 PRIx64 " hva: 0x%" PRIx64 "\n",
423 dev->ifname, log_addr, hva);
432 /* Caller should have iotlb_lock read-locked */
434 vring_translate_split(struct virtio_net *dev, struct vhost_virtqueue *vq)
436 uint64_t req_size, size;
438 req_size = sizeof(struct vring_desc) * vq->size;
440 vq->desc = (struct vring_desc *)(uintptr_t)vhost_iova_to_vva(dev, vq,
441 vq->ring_addrs.desc_user_addr,
442 &size, VHOST_ACCESS_RW);
443 if (!vq->desc || size != req_size)
446 req_size = sizeof(struct vring_avail);
447 req_size += sizeof(uint16_t) * vq->size;
448 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))
449 req_size += sizeof(uint16_t);
451 vq->avail = (struct vring_avail *)(uintptr_t)vhost_iova_to_vva(dev, vq,
452 vq->ring_addrs.avail_user_addr,
453 &size, VHOST_ACCESS_RW);
454 if (!vq->avail || size != req_size)
457 req_size = sizeof(struct vring_used);
458 req_size += sizeof(struct vring_used_elem) * vq->size;
459 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))
460 req_size += sizeof(uint16_t);
462 vq->used = (struct vring_used *)(uintptr_t)vhost_iova_to_vva(dev, vq,
463 vq->ring_addrs.used_user_addr,
464 &size, VHOST_ACCESS_RW);
465 if (!vq->used || size != req_size)
471 /* Caller should have iotlb_lock read-locked */
473 vring_translate_packed(struct virtio_net *dev, struct vhost_virtqueue *vq)
475 uint64_t req_size, size;
477 req_size = sizeof(struct vring_packed_desc) * vq->size;
479 vq->desc_packed = (struct vring_packed_desc *)(uintptr_t)
480 vhost_iova_to_vva(dev, vq, vq->ring_addrs.desc_user_addr,
481 &size, VHOST_ACCESS_RW);
482 if (!vq->desc_packed || size != req_size)
485 req_size = sizeof(struct vring_packed_desc_event);
487 vq->driver_event = (struct vring_packed_desc_event *)(uintptr_t)
488 vhost_iova_to_vva(dev, vq, vq->ring_addrs.avail_user_addr,
489 &size, VHOST_ACCESS_RW);
490 if (!vq->driver_event || size != req_size)
493 req_size = sizeof(struct vring_packed_desc_event);
495 vq->device_event = (struct vring_packed_desc_event *)(uintptr_t)
496 vhost_iova_to_vva(dev, vq, vq->ring_addrs.used_user_addr,
497 &size, VHOST_ACCESS_RW);
498 if (!vq->device_event || size != req_size)
505 vring_translate(struct virtio_net *dev, struct vhost_virtqueue *vq)
508 if (!(dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)))
511 if (vq_is_packed(dev)) {
512 if (vring_translate_packed(dev, vq) < 0)
515 if (vring_translate_split(dev, vq) < 0)
519 if (log_translate(dev, vq) < 0)
522 vq->access_ok = true;
528 vring_invalidate(struct virtio_net *dev, struct vhost_virtqueue *vq)
530 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
531 vhost_user_iotlb_wr_lock(vq);
533 vq->access_ok = false;
537 vq->log_guest_addr = 0;
539 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
540 vhost_user_iotlb_wr_unlock(vq);
544 init_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
546 struct vhost_virtqueue *vq;
547 int numa_node = SOCKET_ID_ANY;
549 if (vring_idx >= VHOST_MAX_VRING) {
550 VHOST_LOG_CONFIG(ERR, "(%s) failed to init vring, out of bound (%d)\n",
551 dev->ifname, vring_idx);
555 vq = dev->virtqueue[vring_idx];
557 VHOST_LOG_CONFIG(ERR, "(%s) virtqueue not allocated (%d)\n",
558 dev->ifname, vring_idx);
562 memset(vq, 0, sizeof(struct vhost_virtqueue));
564 vq->kickfd = VIRTIO_UNINITIALIZED_EVENTFD;
565 vq->callfd = VIRTIO_UNINITIALIZED_EVENTFD;
566 vq->notif_enable = VIRTIO_UNINITIALIZED_NOTIF;
568 #ifdef RTE_LIBRTE_VHOST_NUMA
569 if (get_mempolicy(&numa_node, NULL, 0, vq, MPOL_F_NODE | MPOL_F_ADDR)) {
570 VHOST_LOG_CONFIG(ERR, "(%s) failed to query numa node: %s\n",
571 dev->ifname, rte_strerror(errno));
572 numa_node = SOCKET_ID_ANY;
575 vq->numa_node = numa_node;
577 vhost_user_iotlb_init(dev, vring_idx);
581 reset_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
583 struct vhost_virtqueue *vq;
586 if (vring_idx >= VHOST_MAX_VRING) {
587 VHOST_LOG_CONFIG(ERR,
588 "(%s) failed to reset vring, out of bound (%d)\n",
589 dev->ifname, vring_idx);
593 vq = dev->virtqueue[vring_idx];
595 VHOST_LOG_CONFIG(ERR, "(%s) failed to reset vring, virtqueue not allocated (%d)\n",
596 dev->ifname, vring_idx);
601 init_vring_queue(dev, vring_idx);
606 alloc_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
608 struct vhost_virtqueue *vq;
611 /* Also allocate holes, if any, up to requested vring index. */
612 for (i = 0; i <= vring_idx; i++) {
613 if (dev->virtqueue[i])
616 vq = rte_zmalloc(NULL, sizeof(struct vhost_virtqueue), 0);
618 VHOST_LOG_CONFIG(ERR, "(%s) failed to allocate memory for vring %u.\n",
623 dev->virtqueue[i] = vq;
624 init_vring_queue(dev, i);
625 rte_spinlock_init(&vq->access_lock);
626 vq->avail_wrap_counter = 1;
627 vq->used_wrap_counter = 1;
628 vq->signalled_used_valid = false;
631 dev->nr_vring = RTE_MAX(dev->nr_vring, vring_idx + 1);
637 * Reset some variables in device structure, while keeping few
638 * others untouched, such as vid, ifname, nr_vring: they
639 * should be same unless the device is removed.
642 reset_device(struct virtio_net *dev)
647 dev->protocol_features = 0;
648 dev->flags &= VIRTIO_DEV_BUILTIN_VIRTIO_NET;
650 for (i = 0; i < dev->nr_vring; i++)
651 reset_vring_queue(dev, i);
655 * Invoked when there is a new vhost-user connection established (when
656 * there is a new virtio device being attached).
659 vhost_new_device(void)
661 struct virtio_net *dev;
664 pthread_mutex_lock(&vhost_dev_lock);
665 for (i = 0; i < RTE_MAX_VHOST_DEVICE; i++) {
666 if (vhost_devices[i] == NULL)
670 if (i == RTE_MAX_VHOST_DEVICE) {
671 VHOST_LOG_CONFIG(ERR, "failed to find a free slot for new device.\n");
672 pthread_mutex_unlock(&vhost_dev_lock);
676 dev = rte_zmalloc(NULL, sizeof(struct virtio_net), 0);
678 VHOST_LOG_CONFIG(ERR, "failed to allocate memory for new device.\n");
679 pthread_mutex_unlock(&vhost_dev_lock);
683 vhost_devices[i] = dev;
684 pthread_mutex_unlock(&vhost_dev_lock);
687 dev->flags = VIRTIO_DEV_BUILTIN_VIRTIO_NET;
688 dev->slave_req_fd = -1;
689 dev->postcopy_ufd = -1;
690 rte_spinlock_init(&dev->slave_req_lock);
696 vhost_destroy_device_notify(struct virtio_net *dev)
698 struct rte_vdpa_device *vdpa_dev;
700 if (dev->flags & VIRTIO_DEV_RUNNING) {
701 vdpa_dev = dev->vdpa_dev;
703 vdpa_dev->ops->dev_close(dev->vid);
704 dev->flags &= ~VIRTIO_DEV_RUNNING;
705 dev->notify_ops->destroy_device(dev->vid);
710 * Invoked when there is the vhost-user connection is broken (when
711 * the virtio device is being detached).
714 vhost_destroy_device(int vid)
716 struct virtio_net *dev = get_device(vid);
721 vhost_destroy_device_notify(dev);
723 cleanup_device(dev, 1);
726 vhost_devices[vid] = NULL;
730 vhost_attach_vdpa_device(int vid, struct rte_vdpa_device *vdpa_dev)
732 struct virtio_net *dev = get_device(vid);
737 dev->vdpa_dev = vdpa_dev;
741 vhost_set_ifname(int vid, const char *if_name, unsigned int if_len)
743 struct virtio_net *dev;
746 dev = get_device(vid);
750 len = if_len > sizeof(dev->ifname) ?
751 sizeof(dev->ifname) : if_len;
753 strncpy(dev->ifname, if_name, len);
754 dev->ifname[sizeof(dev->ifname) - 1] = '\0';
758 vhost_setup_virtio_net(int vid, bool enable, bool compliant_ol_flags)
760 struct virtio_net *dev = get_device(vid);
766 dev->flags |= VIRTIO_DEV_BUILTIN_VIRTIO_NET;
768 dev->flags &= ~VIRTIO_DEV_BUILTIN_VIRTIO_NET;
769 if (!compliant_ol_flags)
770 dev->flags |= VIRTIO_DEV_LEGACY_OL_FLAGS;
772 dev->flags &= ~VIRTIO_DEV_LEGACY_OL_FLAGS;
776 vhost_enable_extbuf(int vid)
778 struct virtio_net *dev = get_device(vid);
787 vhost_enable_linearbuf(int vid)
789 struct virtio_net *dev = get_device(vid);
798 rte_vhost_get_mtu(int vid, uint16_t *mtu)
800 struct virtio_net *dev = get_device(vid);
802 if (dev == NULL || mtu == NULL)
805 if (!(dev->flags & VIRTIO_DEV_READY))
808 if (!(dev->features & (1ULL << VIRTIO_NET_F_MTU)))
817 rte_vhost_get_numa_node(int vid)
819 #ifdef RTE_LIBRTE_VHOST_NUMA
820 struct virtio_net *dev = get_device(vid);
824 if (dev == NULL || numa_available() != 0)
827 ret = get_mempolicy(&numa_node, NULL, 0, dev,
828 MPOL_F_NODE | MPOL_F_ADDR);
830 VHOST_LOG_CONFIG(ERR, "(%s) failed to query numa node: %s\n",
831 dev->ifname, rte_strerror(errno));
843 rte_vhost_get_queue_num(int vid)
845 struct virtio_net *dev = get_device(vid);
850 return dev->nr_vring / 2;
854 rte_vhost_get_vring_num(int vid)
856 struct virtio_net *dev = get_device(vid);
861 return dev->nr_vring;
865 rte_vhost_get_ifname(int vid, char *buf, size_t len)
867 struct virtio_net *dev = get_device(vid);
869 if (dev == NULL || buf == NULL)
872 len = RTE_MIN(len, sizeof(dev->ifname));
874 strncpy(buf, dev->ifname, len);
881 rte_vhost_get_negotiated_features(int vid, uint64_t *features)
883 struct virtio_net *dev;
885 dev = get_device(vid);
886 if (dev == NULL || features == NULL)
889 *features = dev->features;
894 rte_vhost_get_negotiated_protocol_features(int vid,
895 uint64_t *protocol_features)
897 struct virtio_net *dev;
899 dev = get_device(vid);
900 if (dev == NULL || protocol_features == NULL)
903 *protocol_features = dev->protocol_features;
908 rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem)
910 struct virtio_net *dev;
911 struct rte_vhost_memory *m;
914 dev = get_device(vid);
915 if (dev == NULL || mem == NULL)
918 size = dev->mem->nregions * sizeof(struct rte_vhost_mem_region);
919 m = malloc(sizeof(struct rte_vhost_memory) + size);
923 m->nregions = dev->mem->nregions;
924 memcpy(m->regions, dev->mem->regions, size);
931 rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
932 struct rte_vhost_vring *vring)
934 struct virtio_net *dev;
935 struct vhost_virtqueue *vq;
937 dev = get_device(vid);
938 if (dev == NULL || vring == NULL)
941 if (vring_idx >= VHOST_MAX_VRING)
944 vq = dev->virtqueue[vring_idx];
948 if (vq_is_packed(dev)) {
949 vring->desc_packed = vq->desc_packed;
950 vring->driver_event = vq->driver_event;
951 vring->device_event = vq->device_event;
953 vring->desc = vq->desc;
954 vring->avail = vq->avail;
955 vring->used = vq->used;
957 vring->log_guest_addr = vq->log_guest_addr;
959 vring->callfd = vq->callfd;
960 vring->kickfd = vq->kickfd;
961 vring->size = vq->size;
967 rte_vhost_get_vhost_ring_inflight(int vid, uint16_t vring_idx,
968 struct rte_vhost_ring_inflight *vring)
970 struct virtio_net *dev;
971 struct vhost_virtqueue *vq;
973 dev = get_device(vid);
977 if (vring_idx >= VHOST_MAX_VRING)
980 vq = dev->virtqueue[vring_idx];
984 if (vq_is_packed(dev)) {
985 if (unlikely(!vq->inflight_packed))
988 vring->inflight_packed = vq->inflight_packed;
990 if (unlikely(!vq->inflight_split))
993 vring->inflight_split = vq->inflight_split;
996 vring->resubmit_inflight = vq->resubmit_inflight;
1002 rte_vhost_set_inflight_desc_split(int vid, uint16_t vring_idx,
1005 struct vhost_virtqueue *vq;
1006 struct virtio_net *dev;
1008 dev = get_device(vid);
1012 if (unlikely(!(dev->protocol_features &
1013 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1016 if (unlikely(vq_is_packed(dev)))
1019 if (unlikely(vring_idx >= VHOST_MAX_VRING))
1022 vq = dev->virtqueue[vring_idx];
1026 if (unlikely(!vq->inflight_split))
1029 if (unlikely(idx >= vq->size))
1032 vq->inflight_split->desc[idx].counter = vq->global_counter++;
1033 vq->inflight_split->desc[idx].inflight = 1;
1038 rte_vhost_set_inflight_desc_packed(int vid, uint16_t vring_idx,
1039 uint16_t head, uint16_t last,
1040 uint16_t *inflight_entry)
1042 struct rte_vhost_inflight_info_packed *inflight_info;
1043 struct virtio_net *dev;
1044 struct vhost_virtqueue *vq;
1045 struct vring_packed_desc *desc;
1046 uint16_t old_free_head, free_head;
1048 dev = get_device(vid);
1052 if (unlikely(!(dev->protocol_features &
1053 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1056 if (unlikely(!vq_is_packed(dev)))
1059 if (unlikely(vring_idx >= VHOST_MAX_VRING))
1062 vq = dev->virtqueue[vring_idx];
1066 inflight_info = vq->inflight_packed;
1067 if (unlikely(!inflight_info))
1070 if (unlikely(head >= vq->size))
1073 desc = vq->desc_packed;
1074 old_free_head = inflight_info->old_free_head;
1075 if (unlikely(old_free_head >= vq->size))
1078 free_head = old_free_head;
1080 /* init header descriptor */
1081 inflight_info->desc[old_free_head].num = 0;
1082 inflight_info->desc[old_free_head].counter = vq->global_counter++;
1083 inflight_info->desc[old_free_head].inflight = 1;
1085 /* save desc entry in flight entry */
1086 while (head != ((last + 1) % vq->size)) {
1087 inflight_info->desc[old_free_head].num++;
1088 inflight_info->desc[free_head].addr = desc[head].addr;
1089 inflight_info->desc[free_head].len = desc[head].len;
1090 inflight_info->desc[free_head].flags = desc[head].flags;
1091 inflight_info->desc[free_head].id = desc[head].id;
1093 inflight_info->desc[old_free_head].last = free_head;
1094 free_head = inflight_info->desc[free_head].next;
1095 inflight_info->free_head = free_head;
1096 head = (head + 1) % vq->size;
1099 inflight_info->old_free_head = free_head;
1100 *inflight_entry = old_free_head;
1106 rte_vhost_clr_inflight_desc_split(int vid, uint16_t vring_idx,
1107 uint16_t last_used_idx, uint16_t idx)
1109 struct virtio_net *dev;
1110 struct vhost_virtqueue *vq;
1112 dev = get_device(vid);
1116 if (unlikely(!(dev->protocol_features &
1117 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1120 if (unlikely(vq_is_packed(dev)))
1123 if (unlikely(vring_idx >= VHOST_MAX_VRING))
1126 vq = dev->virtqueue[vring_idx];
1130 if (unlikely(!vq->inflight_split))
1133 if (unlikely(idx >= vq->size))
1136 rte_atomic_thread_fence(__ATOMIC_SEQ_CST);
1138 vq->inflight_split->desc[idx].inflight = 0;
1140 rte_atomic_thread_fence(__ATOMIC_SEQ_CST);
1142 vq->inflight_split->used_idx = last_used_idx;
1147 rte_vhost_clr_inflight_desc_packed(int vid, uint16_t vring_idx,
1150 struct rte_vhost_inflight_info_packed *inflight_info;
1151 struct virtio_net *dev;
1152 struct vhost_virtqueue *vq;
1154 dev = get_device(vid);
1158 if (unlikely(!(dev->protocol_features &
1159 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1162 if (unlikely(!vq_is_packed(dev)))
1165 if (unlikely(vring_idx >= VHOST_MAX_VRING))
1168 vq = dev->virtqueue[vring_idx];
1172 inflight_info = vq->inflight_packed;
1173 if (unlikely(!inflight_info))
1176 if (unlikely(head >= vq->size))
1179 rte_atomic_thread_fence(__ATOMIC_SEQ_CST);
1181 inflight_info->desc[head].inflight = 0;
1183 rte_atomic_thread_fence(__ATOMIC_SEQ_CST);
1185 inflight_info->old_free_head = inflight_info->free_head;
1186 inflight_info->old_used_idx = inflight_info->used_idx;
1187 inflight_info->old_used_wrap_counter = inflight_info->used_wrap_counter;
1193 rte_vhost_set_last_inflight_io_split(int vid, uint16_t vring_idx,
1196 struct virtio_net *dev;
1197 struct vhost_virtqueue *vq;
1199 dev = get_device(vid);
1203 if (unlikely(!(dev->protocol_features &
1204 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1207 if (unlikely(vq_is_packed(dev)))
1210 if (unlikely(vring_idx >= VHOST_MAX_VRING))
1213 vq = dev->virtqueue[vring_idx];
1217 if (unlikely(!vq->inflight_split))
1220 if (unlikely(idx >= vq->size))
1223 vq->inflight_split->last_inflight_io = idx;
1228 rte_vhost_set_last_inflight_io_packed(int vid, uint16_t vring_idx,
1231 struct rte_vhost_inflight_info_packed *inflight_info;
1232 struct virtio_net *dev;
1233 struct vhost_virtqueue *vq;
1236 dev = get_device(vid);
1240 if (unlikely(!(dev->protocol_features &
1241 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1244 if (unlikely(!vq_is_packed(dev)))
1247 if (unlikely(vring_idx >= VHOST_MAX_VRING))
1250 vq = dev->virtqueue[vring_idx];
1254 inflight_info = vq->inflight_packed;
1255 if (unlikely(!inflight_info))
1258 if (unlikely(head >= vq->size))
1261 last = inflight_info->desc[head].last;
1262 if (unlikely(last >= vq->size))
1265 inflight_info->desc[last].next = inflight_info->free_head;
1266 inflight_info->free_head = head;
1267 inflight_info->used_idx += inflight_info->desc[head].num;
1268 if (inflight_info->used_idx >= inflight_info->desc_num) {
1269 inflight_info->used_idx -= inflight_info->desc_num;
1270 inflight_info->used_wrap_counter =
1271 !inflight_info->used_wrap_counter;
1278 rte_vhost_vring_call(int vid, uint16_t vring_idx)
1280 struct virtio_net *dev;
1281 struct vhost_virtqueue *vq;
1283 dev = get_device(vid);
1287 if (vring_idx >= VHOST_MAX_VRING)
1290 vq = dev->virtqueue[vring_idx];
1294 if (vq_is_packed(dev))
1295 vhost_vring_call_packed(dev, vq);
1297 vhost_vring_call_split(dev, vq);
1303 rte_vhost_avail_entries(int vid, uint16_t queue_id)
1305 struct virtio_net *dev;
1306 struct vhost_virtqueue *vq;
1309 dev = get_device(vid);
1313 if (queue_id >= VHOST_MAX_VRING)
1316 vq = dev->virtqueue[queue_id];
1320 rte_spinlock_lock(&vq->access_lock);
1322 if (unlikely(!vq->enabled || vq->avail == NULL))
1325 ret = *(volatile uint16_t *)&vq->avail->idx - vq->last_used_idx;
1328 rte_spinlock_unlock(&vq->access_lock);
1333 vhost_enable_notify_split(struct virtio_net *dev,
1334 struct vhost_virtqueue *vq, int enable)
1336 if (vq->used == NULL)
1339 if (!(dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))) {
1341 vq->used->flags &= ~VRING_USED_F_NO_NOTIFY;
1343 vq->used->flags |= VRING_USED_F_NO_NOTIFY;
1346 vhost_avail_event(vq) = vq->last_avail_idx;
1352 vhost_enable_notify_packed(struct virtio_net *dev,
1353 struct vhost_virtqueue *vq, int enable)
1357 if (vq->device_event == NULL)
1361 vq->device_event->flags = VRING_EVENT_F_DISABLE;
1365 flags = VRING_EVENT_F_ENABLE;
1366 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX)) {
1367 flags = VRING_EVENT_F_DESC;
1368 vq->device_event->off_wrap = vq->last_avail_idx |
1369 vq->avail_wrap_counter << 15;
1372 rte_atomic_thread_fence(__ATOMIC_RELEASE);
1374 vq->device_event->flags = flags;
1379 vhost_enable_guest_notification(struct virtio_net *dev,
1380 struct vhost_virtqueue *vq, int enable)
1383 * If the virtqueue is not ready yet, it will be applied
1384 * when it will become ready.
1389 if (vq_is_packed(dev))
1390 return vhost_enable_notify_packed(dev, vq, enable);
1392 return vhost_enable_notify_split(dev, vq, enable);
1396 rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable)
1398 struct virtio_net *dev = get_device(vid);
1399 struct vhost_virtqueue *vq;
1405 if (queue_id >= VHOST_MAX_VRING)
1408 vq = dev->virtqueue[queue_id];
1412 rte_spinlock_lock(&vq->access_lock);
1414 vq->notif_enable = enable;
1415 ret = vhost_enable_guest_notification(dev, vq, enable);
1417 rte_spinlock_unlock(&vq->access_lock);
1423 rte_vhost_log_write(int vid, uint64_t addr, uint64_t len)
1425 struct virtio_net *dev = get_device(vid);
1430 vhost_log_write(dev, addr, len);
1434 rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
1435 uint64_t offset, uint64_t len)
1437 struct virtio_net *dev;
1438 struct vhost_virtqueue *vq;
1440 dev = get_device(vid);
1444 if (vring_idx >= VHOST_MAX_VRING)
1446 vq = dev->virtqueue[vring_idx];
1450 vhost_log_used_vring(dev, vq, offset, len);
1454 rte_vhost_rx_queue_count(int vid, uint16_t qid)
1456 struct virtio_net *dev;
1457 struct vhost_virtqueue *vq;
1460 dev = get_device(vid);
1464 if (unlikely(qid >= dev->nr_vring || (qid & 1) == 0)) {
1465 VHOST_LOG_DATA(ERR, "(%s) %s: invalid virtqueue idx %d.\n",
1466 dev->ifname, __func__, qid);
1470 vq = dev->virtqueue[qid];
1474 rte_spinlock_lock(&vq->access_lock);
1476 if (unlikely(!vq->enabled || vq->avail == NULL))
1479 ret = *((volatile uint16_t *)&vq->avail->idx) - vq->last_avail_idx;
1482 rte_spinlock_unlock(&vq->access_lock);
1486 struct rte_vdpa_device *
1487 rte_vhost_get_vdpa_device(int vid)
1489 struct virtio_net *dev = get_device(vid);
1494 return dev->vdpa_dev;
1498 rte_vhost_get_log_base(int vid, uint64_t *log_base,
1501 struct virtio_net *dev = get_device(vid);
1503 if (dev == NULL || log_base == NULL || log_size == NULL)
1506 *log_base = dev->log_base;
1507 *log_size = dev->log_size;
1513 rte_vhost_get_vring_base(int vid, uint16_t queue_id,
1514 uint16_t *last_avail_idx, uint16_t *last_used_idx)
1516 struct vhost_virtqueue *vq;
1517 struct virtio_net *dev = get_device(vid);
1519 if (dev == NULL || last_avail_idx == NULL || last_used_idx == NULL)
1522 if (queue_id >= VHOST_MAX_VRING)
1525 vq = dev->virtqueue[queue_id];
1529 if (vq_is_packed(dev)) {
1530 *last_avail_idx = (vq->avail_wrap_counter << 15) |
1532 *last_used_idx = (vq->used_wrap_counter << 15) |
1535 *last_avail_idx = vq->last_avail_idx;
1536 *last_used_idx = vq->last_used_idx;
1543 rte_vhost_set_vring_base(int vid, uint16_t queue_id,
1544 uint16_t last_avail_idx, uint16_t last_used_idx)
1546 struct vhost_virtqueue *vq;
1547 struct virtio_net *dev = get_device(vid);
1552 if (queue_id >= VHOST_MAX_VRING)
1555 vq = dev->virtqueue[queue_id];
1559 if (vq_is_packed(dev)) {
1560 vq->last_avail_idx = last_avail_idx & 0x7fff;
1561 vq->avail_wrap_counter = !!(last_avail_idx & (1 << 15));
1562 vq->last_used_idx = last_used_idx & 0x7fff;
1563 vq->used_wrap_counter = !!(last_used_idx & (1 << 15));
1565 vq->last_avail_idx = last_avail_idx;
1566 vq->last_used_idx = last_used_idx;
1573 rte_vhost_get_vring_base_from_inflight(int vid,
1575 uint16_t *last_avail_idx,
1576 uint16_t *last_used_idx)
1578 struct rte_vhost_inflight_info_packed *inflight_info;
1579 struct vhost_virtqueue *vq;
1580 struct virtio_net *dev = get_device(vid);
1582 if (dev == NULL || last_avail_idx == NULL || last_used_idx == NULL)
1585 if (queue_id >= VHOST_MAX_VRING)
1588 vq = dev->virtqueue[queue_id];
1592 if (!vq_is_packed(dev))
1595 inflight_info = vq->inflight_packed;
1599 *last_avail_idx = (inflight_info->old_used_wrap_counter << 15) |
1600 inflight_info->old_used_idx;
1601 *last_used_idx = *last_avail_idx;
1607 rte_vhost_extern_callback_register(int vid,
1608 struct rte_vhost_user_extern_ops const * const ops, void *ctx)
1610 struct virtio_net *dev = get_device(vid);
1612 if (dev == NULL || ops == NULL)
1615 dev->extern_ops = *ops;
1616 dev->extern_data = ctx;
1620 static __rte_always_inline int
1621 async_channel_register(int vid, uint16_t queue_id)
1623 struct virtio_net *dev = get_device(vid);
1624 struct vhost_virtqueue *vq = dev->virtqueue[queue_id];
1625 struct vhost_async *async;
1626 int node = vq->numa_node;
1628 if (unlikely(vq->async)) {
1629 VHOST_LOG_CONFIG(ERR,
1630 "(%s) async register failed: already registered (qid: %d)\n",
1631 dev->ifname, queue_id);
1635 async = rte_zmalloc_socket(NULL, sizeof(struct vhost_async), 0, node);
1637 VHOST_LOG_CONFIG(ERR, "(%s) failed to allocate async metadata (qid: %d)\n",
1638 dev->ifname, queue_id);
1642 async->pkts_info = rte_malloc_socket(NULL, vq->size * sizeof(struct async_inflight_info),
1643 RTE_CACHE_LINE_SIZE, node);
1644 if (!async->pkts_info) {
1645 VHOST_LOG_CONFIG(ERR, "(%s) failed to allocate async_pkts_info (qid: %d)\n",
1646 dev->ifname, queue_id);
1647 goto out_free_async;
1650 async->pkts_cmpl_flag = rte_zmalloc_socket(NULL, vq->size * sizeof(bool),
1651 RTE_CACHE_LINE_SIZE, node);
1652 if (!async->pkts_cmpl_flag) {
1653 VHOST_LOG_CONFIG(ERR, "(%s) failed to allocate async pkts_cmpl_flag (qid: %d)\n",
1654 dev->ifname, queue_id);
1655 goto out_free_async;
1658 if (vq_is_packed(dev)) {
1659 async->buffers_packed = rte_malloc_socket(NULL,
1660 vq->size * sizeof(struct vring_used_elem_packed),
1661 RTE_CACHE_LINE_SIZE, node);
1662 if (!async->buffers_packed) {
1663 VHOST_LOG_CONFIG(ERR, "(%s) failed to allocate async buffers (qid: %d)\n",
1664 dev->ifname, queue_id);
1665 goto out_free_inflight;
1668 async->descs_split = rte_malloc_socket(NULL,
1669 vq->size * sizeof(struct vring_used_elem),
1670 RTE_CACHE_LINE_SIZE, node);
1671 if (!async->descs_split) {
1672 VHOST_LOG_CONFIG(ERR, "(%s) failed to allocate async descs (qid: %d)\n",
1673 dev->ifname, queue_id);
1674 goto out_free_inflight;
1682 rte_free(async->pkts_info);
1690 rte_vhost_async_channel_register(int vid, uint16_t queue_id)
1692 struct vhost_virtqueue *vq;
1693 struct virtio_net *dev = get_device(vid);
1699 if (queue_id >= VHOST_MAX_VRING)
1702 vq = dev->virtqueue[queue_id];
1704 if (unlikely(vq == NULL || !dev->async_copy))
1707 rte_spinlock_lock(&vq->access_lock);
1708 ret = async_channel_register(vid, queue_id);
1709 rte_spinlock_unlock(&vq->access_lock);
1715 rte_vhost_async_channel_register_thread_unsafe(int vid, uint16_t queue_id)
1717 struct vhost_virtqueue *vq;
1718 struct virtio_net *dev = get_device(vid);
1723 if (queue_id >= VHOST_MAX_VRING)
1726 vq = dev->virtqueue[queue_id];
1728 if (unlikely(vq == NULL || !dev->async_copy))
1731 return async_channel_register(vid, queue_id);
1735 rte_vhost_async_channel_unregister(int vid, uint16_t queue_id)
1737 struct vhost_virtqueue *vq;
1738 struct virtio_net *dev = get_device(vid);
1744 if (queue_id >= VHOST_MAX_VRING)
1747 vq = dev->virtqueue[queue_id];
1757 if (!rte_spinlock_trylock(&vq->access_lock)) {
1758 VHOST_LOG_CONFIG(ERR, "(%s) failed to unregister async channel, virtqueue busy.\n",
1763 if (vq->async->pkts_inflight_n) {
1764 VHOST_LOG_CONFIG(ERR, "(%s) failed to unregister async channel.\n", dev->ifname);
1765 VHOST_LOG_CONFIG(ERR, "(%s) inflight packets must be completed before unregistration.\n",
1771 vhost_free_async_mem(vq);
1773 rte_spinlock_unlock(&vq->access_lock);
1779 rte_vhost_async_channel_unregister_thread_unsafe(int vid, uint16_t queue_id)
1781 struct vhost_virtqueue *vq;
1782 struct virtio_net *dev = get_device(vid);
1787 if (queue_id >= VHOST_MAX_VRING)
1790 vq = dev->virtqueue[queue_id];
1798 if (vq->async->pkts_inflight_n) {
1799 VHOST_LOG_CONFIG(ERR, "(%s) failed to unregister async channel.\n", dev->ifname);
1800 VHOST_LOG_CONFIG(ERR, "(%s) inflight packets must be completed before unregistration.\n",
1805 vhost_free_async_mem(vq);
1811 rte_vhost_async_dma_configure(int16_t dma_id, uint16_t vchan_id)
1813 struct rte_dma_info info;
1814 void *pkts_cmpl_flag_addr;
1817 if (!rte_dma_is_valid(dma_id)) {
1818 VHOST_LOG_CONFIG(ERR, "DMA %d is not found.\n", dma_id);
1822 rte_dma_info_get(dma_id, &info);
1823 if (vchan_id >= info.max_vchans) {
1824 VHOST_LOG_CONFIG(ERR, "Invalid DMA %d vChannel %u.\n", dma_id, vchan_id);
1828 if (!dma_copy_track[dma_id].vchans) {
1829 struct async_dma_vchan_info *vchans;
1831 vchans = rte_zmalloc(NULL, sizeof(struct async_dma_vchan_info) * info.max_vchans,
1832 RTE_CACHE_LINE_SIZE);
1833 if (vchans == NULL) {
1834 VHOST_LOG_CONFIG(ERR, "Failed to allocate vchans for DMA %d vChannel %u.\n",
1839 dma_copy_track[dma_id].vchans = vchans;
1842 if (dma_copy_track[dma_id].vchans[vchan_id].pkts_cmpl_flag_addr) {
1843 VHOST_LOG_CONFIG(INFO, "DMA %d vChannel %u already registered.\n", dma_id,
1848 max_desc = info.max_desc;
1849 if (!rte_is_power_of_2(max_desc))
1850 max_desc = rte_align32pow2(max_desc);
1852 pkts_cmpl_flag_addr = rte_zmalloc(NULL, sizeof(bool *) * max_desc, RTE_CACHE_LINE_SIZE);
1853 if (!pkts_cmpl_flag_addr) {
1854 VHOST_LOG_CONFIG(ERR, "Failed to allocate pkts_cmpl_flag_addr for DMA %d "
1855 "vChannel %u.\n", dma_id, vchan_id);
1857 if (dma_copy_track[dma_id].nr_vchans == 0) {
1858 rte_free(dma_copy_track[dma_id].vchans);
1859 dma_copy_track[dma_id].vchans = NULL;
1864 dma_copy_track[dma_id].vchans[vchan_id].pkts_cmpl_flag_addr = pkts_cmpl_flag_addr;
1865 dma_copy_track[dma_id].vchans[vchan_id].ring_size = max_desc;
1866 dma_copy_track[dma_id].vchans[vchan_id].ring_mask = max_desc - 1;
1867 dma_copy_track[dma_id].nr_vchans++;
1873 rte_vhost_async_get_inflight(int vid, uint16_t queue_id)
1875 struct vhost_virtqueue *vq;
1876 struct virtio_net *dev = get_device(vid);
1882 if (queue_id >= VHOST_MAX_VRING)
1885 vq = dev->virtqueue[queue_id];
1893 if (!rte_spinlock_trylock(&vq->access_lock)) {
1894 VHOST_LOG_CONFIG(DEBUG,
1895 "(%s) failed to check in-flight packets. virtqueue busy.\n",
1900 ret = vq->async->pkts_inflight_n;
1901 rte_spinlock_unlock(&vq->access_lock);
1907 rte_vhost_async_get_inflight_thread_unsafe(int vid, uint16_t queue_id)
1909 struct vhost_virtqueue *vq;
1910 struct virtio_net *dev = get_device(vid);
1916 if (queue_id >= VHOST_MAX_VRING)
1919 vq = dev->virtqueue[queue_id];
1927 ret = vq->async->pkts_inflight_n;
1933 rte_vhost_get_monitor_addr(int vid, uint16_t queue_id,
1934 struct rte_vhost_power_monitor_cond *pmc)
1936 struct virtio_net *dev = get_device(vid);
1937 struct vhost_virtqueue *vq;
1941 if (queue_id >= VHOST_MAX_VRING)
1944 vq = dev->virtqueue[queue_id];
1948 if (vq_is_packed(dev)) {
1949 struct vring_packed_desc *desc;
1950 desc = vq->desc_packed;
1951 pmc->addr = &desc[vq->last_avail_idx].flags;
1952 if (vq->avail_wrap_counter)
1953 pmc->val = VRING_DESC_F_AVAIL;
1955 pmc->val = VRING_DESC_F_USED;
1956 pmc->mask = VRING_DESC_F_AVAIL | VRING_DESC_F_USED;
1957 pmc->size = sizeof(desc[vq->last_avail_idx].flags);
1960 pmc->addr = &vq->avail->idx;
1961 pmc->val = vq->last_avail_idx & (vq->size - 1);
1962 pmc->mask = vq->size - 1;
1963 pmc->size = sizeof(vq->avail->idx);
1970 RTE_LOG_REGISTER_SUFFIX(vhost_config_log_level, config, INFO);
1971 RTE_LOG_REGISTER_SUFFIX(vhost_data_log_level, data, WARNING);