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>
10 #ifdef RTE_LIBRTE_VHOST_NUMA
15 #include <rte_errno.h>
16 #include <rte_ethdev.h>
18 #include <rte_string_fns.h>
19 #include <rte_memory.h>
20 #include <rte_malloc.h>
21 #include <rte_vhost.h>
22 #include <rte_rwlock.h>
26 #include "vhost_user.h"
28 struct virtio_net *vhost_devices[MAX_VHOST_DEVICE];
29 pthread_mutex_t vhost_dev_lock = PTHREAD_MUTEX_INITIALIZER;
31 /* Called with iotlb_lock read-locked */
33 __vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
34 uint64_t iova, uint64_t *size, uint8_t perm)
36 uint64_t vva, tmp_size;
43 vva = vhost_user_iotlb_cache_find(vq, iova, &tmp_size, perm);
44 if (tmp_size == *size)
49 if (!vhost_user_iotlb_pending_miss(vq, iova, perm)) {
51 * iotlb_lock is read-locked for a full burst,
52 * but it only protects the iotlb cache.
53 * In case of IOTLB miss, we might block on the socket,
54 * which could cause a deadlock with QEMU if an IOTLB update
55 * is being handled. We can safely unlock here to avoid it.
57 vhost_user_iotlb_rd_unlock(vq);
59 vhost_user_iotlb_pending_insert(vq, iova, perm);
60 if (vhost_user_iotlb_miss(dev, iova, perm)) {
62 "IOTLB miss req failed for IOVA 0x%" PRIx64 "\n",
64 vhost_user_iotlb_pending_remove(vq, iova, 1, perm);
67 vhost_user_iotlb_rd_lock(vq);
73 #define VHOST_LOG_PAGE 4096
76 * Atomically set a bit in memory.
78 static __rte_always_inline void
79 vhost_set_bit(unsigned int nr, volatile uint8_t *addr)
81 #if defined(RTE_TOOLCHAIN_GCC) && (GCC_VERSION < 70100)
83 * __sync_ built-ins are deprecated, but __atomic_ ones
84 * are sub-optimized in older GCC versions.
86 __sync_fetch_and_or_1(addr, (1U << nr));
88 __atomic_fetch_or(addr, (1U << nr), __ATOMIC_RELAXED);
92 static __rte_always_inline void
93 vhost_log_page(uint8_t *log_base, uint64_t page)
95 vhost_set_bit(page % 8, &log_base[page / 8]);
99 __vhost_log_write(struct virtio_net *dev, uint64_t addr, uint64_t len)
103 if (unlikely(!dev->log_base || !len))
106 if (unlikely(dev->log_size <= ((addr + len - 1) / VHOST_LOG_PAGE / 8)))
109 /* To make sure guest memory updates are committed before logging */
110 rte_atomic_thread_fence(__ATOMIC_RELEASE);
112 page = addr / VHOST_LOG_PAGE;
113 while (page * VHOST_LOG_PAGE < addr + len) {
114 vhost_log_page((uint8_t *)(uintptr_t)dev->log_base, page);
120 __vhost_log_write_iova(struct virtio_net *dev, struct vhost_virtqueue *vq,
121 uint64_t iova, uint64_t len)
123 uint64_t hva, gpa, map_len;
126 hva = __vhost_iova_to_vva(dev, vq, iova, &map_len, VHOST_ACCESS_RW);
127 if (map_len != len) {
129 "Failed to write log for IOVA 0x%" PRIx64 ". No IOTLB entry found\n",
134 gpa = hva_to_gpa(dev, hva, len);
136 __vhost_log_write(dev, gpa, len);
140 __vhost_log_cache_sync(struct virtio_net *dev, struct vhost_virtqueue *vq)
142 unsigned long *log_base;
145 if (unlikely(!dev->log_base))
148 /* No cache, nothing to sync */
149 if (unlikely(!vq->log_cache))
152 rte_atomic_thread_fence(__ATOMIC_RELEASE);
154 log_base = (unsigned long *)(uintptr_t)dev->log_base;
156 for (i = 0; i < vq->log_cache_nb_elem; i++) {
157 struct log_cache_entry *elem = vq->log_cache + i;
159 #if defined(RTE_TOOLCHAIN_GCC) && (GCC_VERSION < 70100)
161 * '__sync' builtins are deprecated, but '__atomic' ones
162 * are sub-optimized in older GCC versions.
164 __sync_fetch_and_or(log_base + elem->offset, elem->val);
166 __atomic_fetch_or(log_base + elem->offset, elem->val,
171 rte_atomic_thread_fence(__ATOMIC_RELEASE);
173 vq->log_cache_nb_elem = 0;
176 static __rte_always_inline void
177 vhost_log_cache_page(struct virtio_net *dev, struct vhost_virtqueue *vq,
180 uint32_t bit_nr = page % (sizeof(unsigned long) << 3);
181 uint32_t offset = page / (sizeof(unsigned long) << 3);
184 if (unlikely(!vq->log_cache)) {
185 /* No logging cache allocated, write dirty log map directly */
186 rte_atomic_thread_fence(__ATOMIC_RELEASE);
187 vhost_log_page((uint8_t *)(uintptr_t)dev->log_base, page);
192 for (i = 0; i < vq->log_cache_nb_elem; i++) {
193 struct log_cache_entry *elem = vq->log_cache + i;
195 if (elem->offset == offset) {
196 elem->val |= (1UL << bit_nr);
201 if (unlikely(i >= VHOST_LOG_CACHE_NR)) {
203 * No more room for a new log cache entry,
204 * so write the dirty log map directly.
206 rte_atomic_thread_fence(__ATOMIC_RELEASE);
207 vhost_log_page((uint8_t *)(uintptr_t)dev->log_base, page);
212 vq->log_cache[i].offset = offset;
213 vq->log_cache[i].val = (1UL << bit_nr);
214 vq->log_cache_nb_elem++;
218 __vhost_log_cache_write(struct virtio_net *dev, struct vhost_virtqueue *vq,
219 uint64_t addr, uint64_t len)
223 if (unlikely(!dev->log_base || !len))
226 if (unlikely(dev->log_size <= ((addr + len - 1) / VHOST_LOG_PAGE / 8)))
229 page = addr / VHOST_LOG_PAGE;
230 while (page * VHOST_LOG_PAGE < addr + len) {
231 vhost_log_cache_page(dev, vq, page);
237 __vhost_log_cache_write_iova(struct virtio_net *dev, struct vhost_virtqueue *vq,
238 uint64_t iova, uint64_t len)
240 uint64_t hva, gpa, map_len;
243 hva = __vhost_iova_to_vva(dev, vq, iova, &map_len, VHOST_ACCESS_RW);
244 if (map_len != len) {
246 "Failed to write log for IOVA 0x%" PRIx64 ". No IOTLB entry found\n",
251 gpa = hva_to_gpa(dev, hva, len);
253 __vhost_log_cache_write(dev, vq, gpa, len);
257 vhost_alloc_copy_ind_table(struct virtio_net *dev, struct vhost_virtqueue *vq,
258 uint64_t desc_addr, uint64_t desc_len)
262 uint64_t len, remain = desc_len;
264 idesc = rte_malloc_socket(__func__, desc_len, 0, vq->numa_node);
265 if (unlikely(!idesc))
268 dst = (uint64_t)(uintptr_t)idesc;
272 src = vhost_iova_to_vva(dev, vq, desc_addr, &len,
274 if (unlikely(!src || !len)) {
279 rte_memcpy((void *)(uintptr_t)dst, (void *)(uintptr_t)src, len);
290 cleanup_vq(struct vhost_virtqueue *vq, int destroy)
292 if ((vq->callfd >= 0) && (destroy != 0))
299 cleanup_vq_inflight(struct virtio_net *dev, struct vhost_virtqueue *vq)
301 if (!(dev->protocol_features &
302 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD)))
305 if (vq_is_packed(dev)) {
306 if (vq->inflight_packed)
307 vq->inflight_packed = NULL;
309 if (vq->inflight_split)
310 vq->inflight_split = NULL;
313 if (vq->resubmit_inflight) {
314 if (vq->resubmit_inflight->resubmit_list) {
315 rte_free(vq->resubmit_inflight->resubmit_list);
316 vq->resubmit_inflight->resubmit_list = NULL;
318 rte_free(vq->resubmit_inflight);
319 vq->resubmit_inflight = NULL;
324 * Unmap any memory, close any file descriptors and
325 * free any memory owned by a device.
328 cleanup_device(struct virtio_net *dev, int destroy)
332 vhost_backend_cleanup(dev);
334 for (i = 0; i < dev->nr_vring; i++) {
335 cleanup_vq(dev->virtqueue[i], destroy);
336 cleanup_vq_inflight(dev, dev->virtqueue[i]);
341 vhost_free_async_mem(struct vhost_virtqueue *vq)
343 rte_free(vq->async_pkts_info);
345 rte_free(vq->async_buffers_packed);
346 vq->async_buffers_packed = NULL;
347 rte_free(vq->async_descs_split);
348 vq->async_descs_split = NULL;
350 rte_free(vq->it_pool);
351 rte_free(vq->vec_pool);
353 vq->async_pkts_info = NULL;
359 free_vq(struct virtio_net *dev, struct vhost_virtqueue *vq)
361 if (vq_is_packed(dev))
362 rte_free(vq->shadow_used_packed);
364 rte_free(vq->shadow_used_split);
366 vhost_free_async_mem(vq);
367 rte_free(vq->batch_copy_elems);
368 rte_mempool_free(vq->iotlb_pool);
369 rte_free(vq->log_cache);
374 * Release virtqueues and device memory.
377 free_device(struct virtio_net *dev)
381 for (i = 0; i < dev->nr_vring; i++)
382 free_vq(dev, dev->virtqueue[i]);
387 static __rte_always_inline int
388 log_translate(struct virtio_net *dev, struct vhost_virtqueue *vq)
390 if (likely(!(vq->ring_addrs.flags & (1 << VHOST_VRING_F_LOG))))
393 vq->log_guest_addr = translate_log_addr(dev, vq,
394 vq->ring_addrs.log_guest_addr);
395 if (vq->log_guest_addr == 0)
402 * Converts vring log address to GPA
403 * If IOMMU is enabled, the log address is IOVA
404 * If IOMMU not enabled, the log address is already GPA
406 * Caller should have iotlb_lock read-locked
409 translate_log_addr(struct virtio_net *dev, struct vhost_virtqueue *vq,
412 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)) {
413 const uint64_t exp_size = sizeof(uint64_t);
415 uint64_t size = exp_size;
417 hva = vhost_iova_to_vva(dev, vq, log_addr,
418 &size, VHOST_ACCESS_RW);
420 if (size != exp_size)
423 gpa = hva_to_gpa(dev, hva, exp_size);
425 VHOST_LOG_CONFIG(ERR,
426 "VQ: Failed to find GPA for log_addr: 0x%"
427 PRIx64 " hva: 0x%" PRIx64 "\n",
437 /* Caller should have iotlb_lock read-locked */
439 vring_translate_split(struct virtio_net *dev, struct vhost_virtqueue *vq)
441 uint64_t req_size, size;
443 req_size = sizeof(struct vring_desc) * vq->size;
445 vq->desc = (struct vring_desc *)(uintptr_t)vhost_iova_to_vva(dev, vq,
446 vq->ring_addrs.desc_user_addr,
447 &size, VHOST_ACCESS_RW);
448 if (!vq->desc || size != req_size)
451 req_size = sizeof(struct vring_avail);
452 req_size += sizeof(uint16_t) * vq->size;
453 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))
454 req_size += sizeof(uint16_t);
456 vq->avail = (struct vring_avail *)(uintptr_t)vhost_iova_to_vva(dev, vq,
457 vq->ring_addrs.avail_user_addr,
458 &size, VHOST_ACCESS_RW);
459 if (!vq->avail || size != req_size)
462 req_size = sizeof(struct vring_used);
463 req_size += sizeof(struct vring_used_elem) * vq->size;
464 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))
465 req_size += sizeof(uint16_t);
467 vq->used = (struct vring_used *)(uintptr_t)vhost_iova_to_vva(dev, vq,
468 vq->ring_addrs.used_user_addr,
469 &size, VHOST_ACCESS_RW);
470 if (!vq->used || size != req_size)
476 /* Caller should have iotlb_lock read-locked */
478 vring_translate_packed(struct virtio_net *dev, struct vhost_virtqueue *vq)
480 uint64_t req_size, size;
482 req_size = sizeof(struct vring_packed_desc) * vq->size;
484 vq->desc_packed = (struct vring_packed_desc *)(uintptr_t)
485 vhost_iova_to_vva(dev, vq, vq->ring_addrs.desc_user_addr,
486 &size, VHOST_ACCESS_RW);
487 if (!vq->desc_packed || size != req_size)
490 req_size = sizeof(struct vring_packed_desc_event);
492 vq->driver_event = (struct vring_packed_desc_event *)(uintptr_t)
493 vhost_iova_to_vva(dev, vq, vq->ring_addrs.avail_user_addr,
494 &size, VHOST_ACCESS_RW);
495 if (!vq->driver_event || size != req_size)
498 req_size = sizeof(struct vring_packed_desc_event);
500 vq->device_event = (struct vring_packed_desc_event *)(uintptr_t)
501 vhost_iova_to_vva(dev, vq, vq->ring_addrs.used_user_addr,
502 &size, VHOST_ACCESS_RW);
503 if (!vq->device_event || size != req_size)
510 vring_translate(struct virtio_net *dev, struct vhost_virtqueue *vq)
513 if (!(dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)))
516 if (vq_is_packed(dev)) {
517 if (vring_translate_packed(dev, vq) < 0)
520 if (vring_translate_split(dev, vq) < 0)
524 if (log_translate(dev, vq) < 0)
527 vq->access_ok = true;
533 vring_invalidate(struct virtio_net *dev, struct vhost_virtqueue *vq)
535 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
536 vhost_user_iotlb_wr_lock(vq);
538 vq->access_ok = false;
542 vq->log_guest_addr = 0;
544 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
545 vhost_user_iotlb_wr_unlock(vq);
549 init_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
551 struct vhost_virtqueue *vq;
552 int numa_node = SOCKET_ID_ANY;
554 if (vring_idx >= VHOST_MAX_VRING) {
555 VHOST_LOG_CONFIG(ERR,
556 "Failed not init vring, out of bound (%d)\n",
561 vq = dev->virtqueue[vring_idx];
563 VHOST_LOG_CONFIG(ERR, "Virtqueue not allocated (%d)\n",
568 memset(vq, 0, sizeof(struct vhost_virtqueue));
570 vq->kickfd = VIRTIO_UNINITIALIZED_EVENTFD;
571 vq->callfd = VIRTIO_UNINITIALIZED_EVENTFD;
572 vq->notif_enable = VIRTIO_UNINITIALIZED_NOTIF;
574 #ifdef RTE_LIBRTE_VHOST_NUMA
575 if (get_mempolicy(&numa_node, NULL, 0, vq, MPOL_F_NODE | MPOL_F_ADDR)) {
576 VHOST_LOG_CONFIG(ERR, "(%d) failed to query numa node: %s\n",
577 dev->vid, rte_strerror(errno));
578 numa_node = SOCKET_ID_ANY;
581 vq->numa_node = numa_node;
583 vhost_user_iotlb_init(dev, vring_idx);
587 reset_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
589 struct vhost_virtqueue *vq;
592 if (vring_idx >= VHOST_MAX_VRING) {
593 VHOST_LOG_CONFIG(ERR,
594 "Failed not init vring, out of bound (%d)\n",
599 vq = dev->virtqueue[vring_idx];
601 VHOST_LOG_CONFIG(ERR, "Virtqueue not allocated (%d)\n",
607 init_vring_queue(dev, vring_idx);
612 alloc_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
614 struct vhost_virtqueue *vq;
617 /* Also allocate holes, if any, up to requested vring index. */
618 for (i = 0; i <= vring_idx; i++) {
619 if (dev->virtqueue[i])
622 vq = rte_zmalloc(NULL, sizeof(struct vhost_virtqueue), 0);
624 VHOST_LOG_CONFIG(ERR,
625 "Failed to allocate memory for vring:%u.\n", i);
629 dev->virtqueue[i] = vq;
630 init_vring_queue(dev, i);
631 rte_spinlock_init(&vq->access_lock);
632 vq->avail_wrap_counter = 1;
633 vq->used_wrap_counter = 1;
634 vq->signalled_used_valid = false;
637 dev->nr_vring = RTE_MAX(dev->nr_vring, vring_idx + 1);
643 * Reset some variables in device structure, while keeping few
644 * others untouched, such as vid, ifname, nr_vring: they
645 * should be same unless the device is removed.
648 reset_device(struct virtio_net *dev)
653 dev->protocol_features = 0;
654 dev->flags &= VIRTIO_DEV_BUILTIN_VIRTIO_NET;
656 for (i = 0; i < dev->nr_vring; i++)
657 reset_vring_queue(dev, i);
661 * Invoked when there is a new vhost-user connection established (when
662 * there is a new virtio device being attached).
665 vhost_new_device(void)
667 struct virtio_net *dev;
670 pthread_mutex_lock(&vhost_dev_lock);
671 for (i = 0; i < MAX_VHOST_DEVICE; i++) {
672 if (vhost_devices[i] == NULL)
676 if (i == MAX_VHOST_DEVICE) {
677 VHOST_LOG_CONFIG(ERR,
678 "Failed to find a free slot for new device.\n");
679 pthread_mutex_unlock(&vhost_dev_lock);
683 dev = rte_zmalloc(NULL, sizeof(struct virtio_net), 0);
685 VHOST_LOG_CONFIG(ERR,
686 "Failed to allocate memory for new dev.\n");
687 pthread_mutex_unlock(&vhost_dev_lock);
691 vhost_devices[i] = dev;
692 pthread_mutex_unlock(&vhost_dev_lock);
695 dev->flags = VIRTIO_DEV_BUILTIN_VIRTIO_NET;
696 dev->slave_req_fd = -1;
697 dev->postcopy_ufd = -1;
698 rte_spinlock_init(&dev->slave_req_lock);
704 vhost_destroy_device_notify(struct virtio_net *dev)
706 struct rte_vdpa_device *vdpa_dev;
708 if (dev->flags & VIRTIO_DEV_RUNNING) {
709 vdpa_dev = dev->vdpa_dev;
711 vdpa_dev->ops->dev_close(dev->vid);
712 dev->flags &= ~VIRTIO_DEV_RUNNING;
713 dev->notify_ops->destroy_device(dev->vid);
718 * Invoked when there is the vhost-user connection is broken (when
719 * the virtio device is being detached).
722 vhost_destroy_device(int vid)
724 struct virtio_net *dev = get_device(vid);
729 vhost_destroy_device_notify(dev);
731 cleanup_device(dev, 1);
734 vhost_devices[vid] = NULL;
738 vhost_attach_vdpa_device(int vid, struct rte_vdpa_device *vdpa_dev)
740 struct virtio_net *dev = get_device(vid);
745 dev->vdpa_dev = vdpa_dev;
749 vhost_set_ifname(int vid, const char *if_name, unsigned int if_len)
751 struct virtio_net *dev;
754 dev = get_device(vid);
758 len = if_len > sizeof(dev->ifname) ?
759 sizeof(dev->ifname) : if_len;
761 strncpy(dev->ifname, if_name, len);
762 dev->ifname[sizeof(dev->ifname) - 1] = '\0';
766 vhost_setup_virtio_net(int vid, bool enable, bool compliant_ol_flags)
768 struct virtio_net *dev = get_device(vid);
774 dev->flags |= VIRTIO_DEV_BUILTIN_VIRTIO_NET;
776 dev->flags &= ~VIRTIO_DEV_BUILTIN_VIRTIO_NET;
777 if (!compliant_ol_flags)
778 dev->flags |= VIRTIO_DEV_LEGACY_OL_FLAGS;
780 dev->flags &= ~VIRTIO_DEV_LEGACY_OL_FLAGS;
784 vhost_enable_extbuf(int vid)
786 struct virtio_net *dev = get_device(vid);
795 vhost_enable_linearbuf(int vid)
797 struct virtio_net *dev = get_device(vid);
806 rte_vhost_get_mtu(int vid, uint16_t *mtu)
808 struct virtio_net *dev = get_device(vid);
810 if (dev == NULL || mtu == NULL)
813 if (!(dev->flags & VIRTIO_DEV_READY))
816 if (!(dev->features & (1ULL << VIRTIO_NET_F_MTU)))
825 rte_vhost_get_numa_node(int vid)
827 #ifdef RTE_LIBRTE_VHOST_NUMA
828 struct virtio_net *dev = get_device(vid);
832 if (dev == NULL || numa_available() != 0)
835 ret = get_mempolicy(&numa_node, NULL, 0, dev,
836 MPOL_F_NODE | MPOL_F_ADDR);
838 VHOST_LOG_CONFIG(ERR,
839 "(%d) failed to query numa node: %s\n",
840 vid, rte_strerror(errno));
852 rte_vhost_get_queue_num(int vid)
854 struct virtio_net *dev = get_device(vid);
859 return dev->nr_vring / 2;
863 rte_vhost_get_vring_num(int vid)
865 struct virtio_net *dev = get_device(vid);
870 return dev->nr_vring;
874 rte_vhost_get_ifname(int vid, char *buf, size_t len)
876 struct virtio_net *dev = get_device(vid);
878 if (dev == NULL || buf == NULL)
881 len = RTE_MIN(len, sizeof(dev->ifname));
883 strncpy(buf, dev->ifname, len);
890 rte_vhost_get_negotiated_features(int vid, uint64_t *features)
892 struct virtio_net *dev;
894 dev = get_device(vid);
895 if (dev == NULL || features == NULL)
898 *features = dev->features;
903 rte_vhost_get_negotiated_protocol_features(int vid,
904 uint64_t *protocol_features)
906 struct virtio_net *dev;
908 dev = get_device(vid);
909 if (dev == NULL || protocol_features == NULL)
912 *protocol_features = dev->protocol_features;
917 rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem)
919 struct virtio_net *dev;
920 struct rte_vhost_memory *m;
923 dev = get_device(vid);
924 if (dev == NULL || mem == NULL)
927 size = dev->mem->nregions * sizeof(struct rte_vhost_mem_region);
928 m = malloc(sizeof(struct rte_vhost_memory) + size);
932 m->nregions = dev->mem->nregions;
933 memcpy(m->regions, dev->mem->regions, size);
940 rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
941 struct rte_vhost_vring *vring)
943 struct virtio_net *dev;
944 struct vhost_virtqueue *vq;
946 dev = get_device(vid);
947 if (dev == NULL || vring == NULL)
950 if (vring_idx >= VHOST_MAX_VRING)
953 vq = dev->virtqueue[vring_idx];
957 if (vq_is_packed(dev)) {
958 vring->desc_packed = vq->desc_packed;
959 vring->driver_event = vq->driver_event;
960 vring->device_event = vq->device_event;
962 vring->desc = vq->desc;
963 vring->avail = vq->avail;
964 vring->used = vq->used;
966 vring->log_guest_addr = vq->log_guest_addr;
968 vring->callfd = vq->callfd;
969 vring->kickfd = vq->kickfd;
970 vring->size = vq->size;
976 rte_vhost_get_vhost_ring_inflight(int vid, uint16_t vring_idx,
977 struct rte_vhost_ring_inflight *vring)
979 struct virtio_net *dev;
980 struct vhost_virtqueue *vq;
982 dev = get_device(vid);
986 if (vring_idx >= VHOST_MAX_VRING)
989 vq = dev->virtqueue[vring_idx];
993 if (vq_is_packed(dev)) {
994 if (unlikely(!vq->inflight_packed))
997 vring->inflight_packed = vq->inflight_packed;
999 if (unlikely(!vq->inflight_split))
1002 vring->inflight_split = vq->inflight_split;
1005 vring->resubmit_inflight = vq->resubmit_inflight;
1011 rte_vhost_set_inflight_desc_split(int vid, uint16_t vring_idx,
1014 struct vhost_virtqueue *vq;
1015 struct virtio_net *dev;
1017 dev = get_device(vid);
1021 if (unlikely(!(dev->protocol_features &
1022 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1025 if (unlikely(vq_is_packed(dev)))
1028 if (unlikely(vring_idx >= VHOST_MAX_VRING))
1031 vq = dev->virtqueue[vring_idx];
1035 if (unlikely(!vq->inflight_split))
1038 if (unlikely(idx >= vq->size))
1041 vq->inflight_split->desc[idx].counter = vq->global_counter++;
1042 vq->inflight_split->desc[idx].inflight = 1;
1047 rte_vhost_set_inflight_desc_packed(int vid, uint16_t vring_idx,
1048 uint16_t head, uint16_t last,
1049 uint16_t *inflight_entry)
1051 struct rte_vhost_inflight_info_packed *inflight_info;
1052 struct virtio_net *dev;
1053 struct vhost_virtqueue *vq;
1054 struct vring_packed_desc *desc;
1055 uint16_t old_free_head, free_head;
1057 dev = get_device(vid);
1061 if (unlikely(!(dev->protocol_features &
1062 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1065 if (unlikely(!vq_is_packed(dev)))
1068 if (unlikely(vring_idx >= VHOST_MAX_VRING))
1071 vq = dev->virtqueue[vring_idx];
1075 inflight_info = vq->inflight_packed;
1076 if (unlikely(!inflight_info))
1079 if (unlikely(head >= vq->size))
1082 desc = vq->desc_packed;
1083 old_free_head = inflight_info->old_free_head;
1084 if (unlikely(old_free_head >= vq->size))
1087 free_head = old_free_head;
1089 /* init header descriptor */
1090 inflight_info->desc[old_free_head].num = 0;
1091 inflight_info->desc[old_free_head].counter = vq->global_counter++;
1092 inflight_info->desc[old_free_head].inflight = 1;
1094 /* save desc entry in flight entry */
1095 while (head != ((last + 1) % vq->size)) {
1096 inflight_info->desc[old_free_head].num++;
1097 inflight_info->desc[free_head].addr = desc[head].addr;
1098 inflight_info->desc[free_head].len = desc[head].len;
1099 inflight_info->desc[free_head].flags = desc[head].flags;
1100 inflight_info->desc[free_head].id = desc[head].id;
1102 inflight_info->desc[old_free_head].last = free_head;
1103 free_head = inflight_info->desc[free_head].next;
1104 inflight_info->free_head = free_head;
1105 head = (head + 1) % vq->size;
1108 inflight_info->old_free_head = free_head;
1109 *inflight_entry = old_free_head;
1115 rte_vhost_clr_inflight_desc_split(int vid, uint16_t vring_idx,
1116 uint16_t last_used_idx, uint16_t idx)
1118 struct virtio_net *dev;
1119 struct vhost_virtqueue *vq;
1121 dev = get_device(vid);
1125 if (unlikely(!(dev->protocol_features &
1126 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1129 if (unlikely(vq_is_packed(dev)))
1132 if (unlikely(vring_idx >= VHOST_MAX_VRING))
1135 vq = dev->virtqueue[vring_idx];
1139 if (unlikely(!vq->inflight_split))
1142 if (unlikely(idx >= vq->size))
1145 rte_atomic_thread_fence(__ATOMIC_SEQ_CST);
1147 vq->inflight_split->desc[idx].inflight = 0;
1149 rte_atomic_thread_fence(__ATOMIC_SEQ_CST);
1151 vq->inflight_split->used_idx = last_used_idx;
1156 rte_vhost_clr_inflight_desc_packed(int vid, uint16_t vring_idx,
1159 struct rte_vhost_inflight_info_packed *inflight_info;
1160 struct virtio_net *dev;
1161 struct vhost_virtqueue *vq;
1163 dev = get_device(vid);
1167 if (unlikely(!(dev->protocol_features &
1168 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1171 if (unlikely(!vq_is_packed(dev)))
1174 if (unlikely(vring_idx >= VHOST_MAX_VRING))
1177 vq = dev->virtqueue[vring_idx];
1181 inflight_info = vq->inflight_packed;
1182 if (unlikely(!inflight_info))
1185 if (unlikely(head >= vq->size))
1188 rte_atomic_thread_fence(__ATOMIC_SEQ_CST);
1190 inflight_info->desc[head].inflight = 0;
1192 rte_atomic_thread_fence(__ATOMIC_SEQ_CST);
1194 inflight_info->old_free_head = inflight_info->free_head;
1195 inflight_info->old_used_idx = inflight_info->used_idx;
1196 inflight_info->old_used_wrap_counter = inflight_info->used_wrap_counter;
1202 rte_vhost_set_last_inflight_io_split(int vid, uint16_t vring_idx,
1205 struct virtio_net *dev;
1206 struct vhost_virtqueue *vq;
1208 dev = get_device(vid);
1212 if (unlikely(!(dev->protocol_features &
1213 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1216 if (unlikely(vq_is_packed(dev)))
1219 if (unlikely(vring_idx >= VHOST_MAX_VRING))
1222 vq = dev->virtqueue[vring_idx];
1226 if (unlikely(!vq->inflight_split))
1229 vq->inflight_split->last_inflight_io = idx;
1234 rte_vhost_set_last_inflight_io_packed(int vid, uint16_t vring_idx,
1237 struct rte_vhost_inflight_info_packed *inflight_info;
1238 struct virtio_net *dev;
1239 struct vhost_virtqueue *vq;
1242 dev = get_device(vid);
1246 if (unlikely(!(dev->protocol_features &
1247 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1250 if (unlikely(!vq_is_packed(dev)))
1253 if (unlikely(vring_idx >= VHOST_MAX_VRING))
1256 vq = dev->virtqueue[vring_idx];
1260 inflight_info = vq->inflight_packed;
1261 if (unlikely(!inflight_info))
1264 if (unlikely(head >= vq->size))
1267 last = inflight_info->desc[head].last;
1268 if (unlikely(last >= vq->size))
1271 inflight_info->desc[last].next = inflight_info->free_head;
1272 inflight_info->free_head = head;
1273 inflight_info->used_idx += inflight_info->desc[head].num;
1274 if (inflight_info->used_idx >= inflight_info->desc_num) {
1275 inflight_info->used_idx -= inflight_info->desc_num;
1276 inflight_info->used_wrap_counter =
1277 !inflight_info->used_wrap_counter;
1284 rte_vhost_vring_call(int vid, uint16_t vring_idx)
1286 struct virtio_net *dev;
1287 struct vhost_virtqueue *vq;
1289 dev = get_device(vid);
1293 if (vring_idx >= VHOST_MAX_VRING)
1296 vq = dev->virtqueue[vring_idx];
1300 if (vq_is_packed(dev))
1301 vhost_vring_call_packed(dev, vq);
1303 vhost_vring_call_split(dev, vq);
1309 rte_vhost_avail_entries(int vid, uint16_t queue_id)
1311 struct virtio_net *dev;
1312 struct vhost_virtqueue *vq;
1315 dev = get_device(vid);
1319 if (queue_id >= VHOST_MAX_VRING)
1322 vq = dev->virtqueue[queue_id];
1326 rte_spinlock_lock(&vq->access_lock);
1328 if (unlikely(!vq->enabled || vq->avail == NULL))
1331 ret = *(volatile uint16_t *)&vq->avail->idx - vq->last_used_idx;
1334 rte_spinlock_unlock(&vq->access_lock);
1339 vhost_enable_notify_split(struct virtio_net *dev,
1340 struct vhost_virtqueue *vq, int enable)
1342 if (vq->used == NULL)
1345 if (!(dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))) {
1347 vq->used->flags &= ~VRING_USED_F_NO_NOTIFY;
1349 vq->used->flags |= VRING_USED_F_NO_NOTIFY;
1352 vhost_avail_event(vq) = vq->last_avail_idx;
1358 vhost_enable_notify_packed(struct virtio_net *dev,
1359 struct vhost_virtqueue *vq, int enable)
1363 if (vq->device_event == NULL)
1367 vq->device_event->flags = VRING_EVENT_F_DISABLE;
1371 flags = VRING_EVENT_F_ENABLE;
1372 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX)) {
1373 flags = VRING_EVENT_F_DESC;
1374 vq->device_event->off_wrap = vq->last_avail_idx |
1375 vq->avail_wrap_counter << 15;
1378 rte_atomic_thread_fence(__ATOMIC_RELEASE);
1380 vq->device_event->flags = flags;
1385 vhost_enable_guest_notification(struct virtio_net *dev,
1386 struct vhost_virtqueue *vq, int enable)
1389 * If the virtqueue is not ready yet, it will be applied
1390 * when it will become ready.
1395 if (vq_is_packed(dev))
1396 return vhost_enable_notify_packed(dev, vq, enable);
1398 return vhost_enable_notify_split(dev, vq, enable);
1402 rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable)
1404 struct virtio_net *dev = get_device(vid);
1405 struct vhost_virtqueue *vq;
1411 if (queue_id >= VHOST_MAX_VRING)
1414 vq = dev->virtqueue[queue_id];
1418 rte_spinlock_lock(&vq->access_lock);
1420 vq->notif_enable = enable;
1421 ret = vhost_enable_guest_notification(dev, vq, enable);
1423 rte_spinlock_unlock(&vq->access_lock);
1429 rte_vhost_log_write(int vid, uint64_t addr, uint64_t len)
1431 struct virtio_net *dev = get_device(vid);
1436 vhost_log_write(dev, addr, len);
1440 rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
1441 uint64_t offset, uint64_t len)
1443 struct virtio_net *dev;
1444 struct vhost_virtqueue *vq;
1446 dev = get_device(vid);
1450 if (vring_idx >= VHOST_MAX_VRING)
1452 vq = dev->virtqueue[vring_idx];
1456 vhost_log_used_vring(dev, vq, offset, len);
1460 rte_vhost_rx_queue_count(int vid, uint16_t qid)
1462 struct virtio_net *dev;
1463 struct vhost_virtqueue *vq;
1466 dev = get_device(vid);
1470 if (unlikely(qid >= dev->nr_vring || (qid & 1) == 0)) {
1471 VHOST_LOG_DATA(ERR, "(%d) %s: invalid virtqueue idx %d.\n",
1472 dev->vid, __func__, qid);
1476 vq = dev->virtqueue[qid];
1480 rte_spinlock_lock(&vq->access_lock);
1482 if (unlikely(!vq->enabled || vq->avail == NULL))
1485 ret = *((volatile uint16_t *)&vq->avail->idx) - vq->last_avail_idx;
1488 rte_spinlock_unlock(&vq->access_lock);
1492 struct rte_vdpa_device *
1493 rte_vhost_get_vdpa_device(int vid)
1495 struct virtio_net *dev = get_device(vid);
1500 return dev->vdpa_dev;
1503 int rte_vhost_get_log_base(int vid, uint64_t *log_base,
1506 struct virtio_net *dev = get_device(vid);
1508 if (dev == NULL || log_base == NULL || log_size == NULL)
1511 *log_base = dev->log_base;
1512 *log_size = dev->log_size;
1517 int rte_vhost_get_vring_base(int vid, uint16_t queue_id,
1518 uint16_t *last_avail_idx, uint16_t *last_used_idx)
1520 struct vhost_virtqueue *vq;
1521 struct virtio_net *dev = get_device(vid);
1523 if (dev == NULL || last_avail_idx == NULL || last_used_idx == NULL)
1526 if (queue_id >= VHOST_MAX_VRING)
1529 vq = dev->virtqueue[queue_id];
1533 if (vq_is_packed(dev)) {
1534 *last_avail_idx = (vq->avail_wrap_counter << 15) |
1536 *last_used_idx = (vq->used_wrap_counter << 15) |
1539 *last_avail_idx = vq->last_avail_idx;
1540 *last_used_idx = vq->last_used_idx;
1546 int rte_vhost_set_vring_base(int vid, uint16_t queue_id,
1547 uint16_t last_avail_idx, uint16_t last_used_idx)
1549 struct vhost_virtqueue *vq;
1550 struct virtio_net *dev = get_device(vid);
1555 if (queue_id >= VHOST_MAX_VRING)
1558 vq = dev->virtqueue[queue_id];
1562 if (vq_is_packed(dev)) {
1563 vq->last_avail_idx = last_avail_idx & 0x7fff;
1564 vq->avail_wrap_counter = !!(last_avail_idx & (1 << 15));
1565 vq->last_used_idx = last_used_idx & 0x7fff;
1566 vq->used_wrap_counter = !!(last_used_idx & (1 << 15));
1568 vq->last_avail_idx = last_avail_idx;
1569 vq->last_used_idx = last_used_idx;
1576 rte_vhost_get_vring_base_from_inflight(int vid,
1578 uint16_t *last_avail_idx,
1579 uint16_t *last_used_idx)
1581 struct rte_vhost_inflight_info_packed *inflight_info;
1582 struct vhost_virtqueue *vq;
1583 struct virtio_net *dev = get_device(vid);
1585 if (dev == NULL || last_avail_idx == NULL || last_used_idx == NULL)
1588 if (queue_id >= VHOST_MAX_VRING)
1591 vq = dev->virtqueue[queue_id];
1595 if (!vq_is_packed(dev))
1598 inflight_info = vq->inflight_packed;
1602 *last_avail_idx = (inflight_info->old_used_wrap_counter << 15) |
1603 inflight_info->old_used_idx;
1604 *last_used_idx = *last_avail_idx;
1609 int rte_vhost_extern_callback_register(int vid,
1610 struct rte_vhost_user_extern_ops const * const ops, void *ctx)
1612 struct virtio_net *dev = get_device(vid);
1614 if (dev == NULL || ops == NULL)
1617 dev->extern_ops = *ops;
1618 dev->extern_data = ctx;
1622 static __rte_always_inline int
1623 async_channel_register(int vid, uint16_t queue_id,
1624 struct rte_vhost_async_config config,
1625 struct rte_vhost_async_channel_ops *ops)
1627 struct virtio_net *dev = get_device(vid);
1628 struct vhost_virtqueue *vq = dev->virtqueue[queue_id];
1630 if (unlikely(vq->async_registered)) {
1631 VHOST_LOG_CONFIG(ERR,
1632 "async register failed: channel already registered "
1633 "(vid %d, qid: %d)\n", vid, queue_id);
1637 vq->async_pkts_info = rte_malloc_socket(NULL,
1638 vq->size * sizeof(struct async_inflight_info),
1639 RTE_CACHE_LINE_SIZE, vq->numa_node);
1640 if (!vq->async_pkts_info) {
1641 vhost_free_async_mem(vq);
1642 VHOST_LOG_CONFIG(ERR,
1643 "async register failed: cannot allocate memory for async_pkts_info "
1644 "(vid %d, qid: %d)\n", vid, queue_id);
1648 vq->it_pool = rte_malloc_socket(NULL,
1649 VHOST_MAX_ASYNC_IT * sizeof(struct rte_vhost_iov_iter),
1650 RTE_CACHE_LINE_SIZE, vq->numa_node);
1652 vhost_free_async_mem(vq);
1653 VHOST_LOG_CONFIG(ERR,
1654 "async register failed: cannot allocate memory for it_pool "
1655 "(vid %d, qid: %d)\n", vid, queue_id);
1659 vq->vec_pool = rte_malloc_socket(NULL,
1660 VHOST_MAX_ASYNC_VEC * sizeof(struct iovec),
1661 RTE_CACHE_LINE_SIZE, vq->numa_node);
1662 if (!vq->vec_pool) {
1663 vhost_free_async_mem(vq);
1664 VHOST_LOG_CONFIG(ERR,
1665 "async register failed: cannot allocate memory for vec_pool "
1666 "(vid %d, qid: %d)\n", vid, queue_id);
1670 if (vq_is_packed(dev)) {
1671 vq->async_buffers_packed = rte_malloc_socket(NULL,
1672 vq->size * sizeof(struct vring_used_elem_packed),
1673 RTE_CACHE_LINE_SIZE, vq->numa_node);
1674 if (!vq->async_buffers_packed) {
1675 vhost_free_async_mem(vq);
1676 VHOST_LOG_CONFIG(ERR,
1677 "async register failed: cannot allocate memory for async buffers "
1678 "(vid %d, qid: %d)\n", vid, queue_id);
1682 vq->async_descs_split = rte_malloc_socket(NULL,
1683 vq->size * sizeof(struct vring_used_elem),
1684 RTE_CACHE_LINE_SIZE, vq->numa_node);
1685 if (!vq->async_descs_split) {
1686 vhost_free_async_mem(vq);
1687 VHOST_LOG_CONFIG(ERR,
1688 "async register failed: cannot allocate memory for async descs "
1689 "(vid %d, qid: %d)\n", vid, queue_id);
1694 vq->async_ops.check_completed_copies = ops->check_completed_copies;
1695 vq->async_ops.transfer_data = ops->transfer_data;
1696 vq->async_threshold = config.async_threshold;
1698 vq->async_registered = true;
1704 rte_vhost_async_channel_register(int vid, uint16_t queue_id,
1705 struct rte_vhost_async_config config,
1706 struct rte_vhost_async_channel_ops *ops)
1708 struct vhost_virtqueue *vq;
1709 struct virtio_net *dev = get_device(vid);
1712 if (dev == NULL || ops == NULL)
1715 if (queue_id >= VHOST_MAX_VRING)
1718 vq = dev->virtqueue[queue_id];
1720 if (unlikely(vq == NULL || !dev->async_copy))
1723 if (unlikely(!(config.features & RTE_VHOST_ASYNC_INORDER))) {
1724 VHOST_LOG_CONFIG(ERR,
1725 "async copy is not supported on non-inorder mode "
1726 "(vid %d, qid: %d)\n", vid, queue_id);
1730 if (unlikely(ops->check_completed_copies == NULL ||
1731 ops->transfer_data == NULL))
1734 rte_spinlock_lock(&vq->access_lock);
1735 ret = async_channel_register(vid, queue_id, config, ops);
1736 rte_spinlock_unlock(&vq->access_lock);
1742 rte_vhost_async_channel_register_thread_unsafe(int vid, uint16_t queue_id,
1743 struct rte_vhost_async_config config,
1744 struct rte_vhost_async_channel_ops *ops)
1746 struct vhost_virtqueue *vq;
1747 struct virtio_net *dev = get_device(vid);
1749 if (dev == NULL || ops == NULL)
1752 if (queue_id >= VHOST_MAX_VRING)
1755 vq = dev->virtqueue[queue_id];
1757 if (unlikely(vq == NULL || !dev->async_copy))
1760 if (unlikely(!(config.features & RTE_VHOST_ASYNC_INORDER))) {
1761 VHOST_LOG_CONFIG(ERR,
1762 "async copy is not supported on non-inorder mode "
1763 "(vid %d, qid: %d)\n", vid, queue_id);
1767 if (unlikely(ops->check_completed_copies == NULL ||
1768 ops->transfer_data == NULL))
1771 return async_channel_register(vid, queue_id, config, ops);
1775 rte_vhost_async_channel_unregister(int vid, uint16_t queue_id)
1777 struct vhost_virtqueue *vq;
1778 struct virtio_net *dev = get_device(vid);
1784 if (queue_id >= VHOST_MAX_VRING)
1787 vq = dev->virtqueue[queue_id];
1794 if (!vq->async_registered)
1797 if (!rte_spinlock_trylock(&vq->access_lock)) {
1798 VHOST_LOG_CONFIG(ERR, "Failed to unregister async channel. "
1799 "virt queue busy.\n");
1803 if (vq->async_pkts_inflight_n) {
1804 VHOST_LOG_CONFIG(ERR, "Failed to unregister async channel. "
1805 "async inflight packets must be completed before unregistration.\n");
1810 vhost_free_async_mem(vq);
1812 vq->async_ops.transfer_data = NULL;
1813 vq->async_ops.check_completed_copies = NULL;
1814 vq->async_registered = false;
1817 rte_spinlock_unlock(&vq->access_lock);
1823 rte_vhost_async_channel_unregister_thread_unsafe(int vid, uint16_t queue_id)
1825 struct vhost_virtqueue *vq;
1826 struct virtio_net *dev = get_device(vid);
1831 if (queue_id >= VHOST_MAX_VRING)
1834 vq = dev->virtqueue[queue_id];
1839 if (!vq->async_registered)
1842 if (vq->async_pkts_inflight_n) {
1843 VHOST_LOG_CONFIG(ERR, "Failed to unregister async channel. "
1844 "async inflight packets must be completed before unregistration.\n");
1848 vhost_free_async_mem(vq);
1850 vq->async_ops.transfer_data = NULL;
1851 vq->async_ops.check_completed_copies = NULL;
1852 vq->async_registered = false;
1857 int rte_vhost_async_get_inflight(int vid, uint16_t queue_id)
1859 struct vhost_virtqueue *vq;
1860 struct virtio_net *dev = get_device(vid);
1866 if (queue_id >= VHOST_MAX_VRING)
1869 vq = dev->virtqueue[queue_id];
1874 if (!vq->async_registered)
1877 if (!rte_spinlock_trylock(&vq->access_lock)) {
1878 VHOST_LOG_CONFIG(DEBUG, "Failed to check in-flight packets. "
1879 "virt queue busy.\n");
1883 ret = vq->async_pkts_inflight_n;
1884 rte_spinlock_unlock(&vq->access_lock);
1889 RTE_LOG_REGISTER_SUFFIX(vhost_config_log_level, config, INFO);
1890 RTE_LOG_REGISTER_SUFFIX(vhost_data_log_level, data, WARNING);