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(__func__, desc_len, 0);
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 free(vq->resubmit_inflight->resubmit_list);
316 vq->resubmit_inflight->resubmit_list = NULL;
318 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 if (vq->async_pkts_info)
344 rte_free(vq->async_pkts_info);
345 if (vq->async_descs_split)
346 rte_free(vq->async_descs_split);
348 rte_free(vq->it_pool);
350 rte_free(vq->vec_pool);
352 vq->async_pkts_info = NULL;
353 vq->async_descs_split = 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);
365 vhost_free_async_mem(vq);
367 rte_free(vq->batch_copy_elems);
369 rte_mempool_free(vq->iotlb_pool);
370 rte_free(vq->log_cache);
375 * Release virtqueues and device memory.
378 free_device(struct virtio_net *dev)
382 for (i = 0; i < dev->nr_vring; i++)
383 free_vq(dev, dev->virtqueue[i]);
388 static __rte_always_inline int
389 log_translate(struct virtio_net *dev, struct vhost_virtqueue *vq)
391 if (likely(!(vq->ring_addrs.flags & (1 << VHOST_VRING_F_LOG))))
394 vq->log_guest_addr = translate_log_addr(dev, vq,
395 vq->ring_addrs.log_guest_addr);
396 if (vq->log_guest_addr == 0)
403 * Converts vring log address to GPA
404 * If IOMMU is enabled, the log address is IOVA
405 * If IOMMU not enabled, the log address is already GPA
407 * Caller should have iotlb_lock read-locked
410 translate_log_addr(struct virtio_net *dev, struct vhost_virtqueue *vq,
413 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)) {
414 const uint64_t exp_size = sizeof(uint64_t);
416 uint64_t size = exp_size;
418 hva = vhost_iova_to_vva(dev, vq, log_addr,
419 &size, VHOST_ACCESS_RW);
421 if (size != exp_size)
424 gpa = hva_to_gpa(dev, hva, exp_size);
426 VHOST_LOG_CONFIG(ERR,
427 "VQ: Failed to find GPA for log_addr: 0x%"
428 PRIx64 " hva: 0x%" PRIx64 "\n",
438 /* Caller should have iotlb_lock read-locked */
440 vring_translate_split(struct virtio_net *dev, struct vhost_virtqueue *vq)
442 uint64_t req_size, size;
444 req_size = sizeof(struct vring_desc) * vq->size;
446 vq->desc = (struct vring_desc *)(uintptr_t)vhost_iova_to_vva(dev, vq,
447 vq->ring_addrs.desc_user_addr,
448 &size, VHOST_ACCESS_RW);
449 if (!vq->desc || size != req_size)
452 req_size = sizeof(struct vring_avail);
453 req_size += sizeof(uint16_t) * vq->size;
454 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))
455 req_size += sizeof(uint16_t);
457 vq->avail = (struct vring_avail *)(uintptr_t)vhost_iova_to_vva(dev, vq,
458 vq->ring_addrs.avail_user_addr,
459 &size, VHOST_ACCESS_RW);
460 if (!vq->avail || size != req_size)
463 req_size = sizeof(struct vring_used);
464 req_size += sizeof(struct vring_used_elem) * vq->size;
465 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))
466 req_size += sizeof(uint16_t);
468 vq->used = (struct vring_used *)(uintptr_t)vhost_iova_to_vva(dev, vq,
469 vq->ring_addrs.used_user_addr,
470 &size, VHOST_ACCESS_RW);
471 if (!vq->used || size != req_size)
477 /* Caller should have iotlb_lock read-locked */
479 vring_translate_packed(struct virtio_net *dev, struct vhost_virtqueue *vq)
481 uint64_t req_size, size;
483 req_size = sizeof(struct vring_packed_desc) * vq->size;
485 vq->desc_packed = (struct vring_packed_desc *)(uintptr_t)
486 vhost_iova_to_vva(dev, vq, vq->ring_addrs.desc_user_addr,
487 &size, VHOST_ACCESS_RW);
488 if (!vq->desc_packed || size != req_size)
491 req_size = sizeof(struct vring_packed_desc_event);
493 vq->driver_event = (struct vring_packed_desc_event *)(uintptr_t)
494 vhost_iova_to_vva(dev, vq, vq->ring_addrs.avail_user_addr,
495 &size, VHOST_ACCESS_RW);
496 if (!vq->driver_event || size != req_size)
499 req_size = sizeof(struct vring_packed_desc_event);
501 vq->device_event = (struct vring_packed_desc_event *)(uintptr_t)
502 vhost_iova_to_vva(dev, vq, vq->ring_addrs.used_user_addr,
503 &size, VHOST_ACCESS_RW);
504 if (!vq->device_event || size != req_size)
511 vring_translate(struct virtio_net *dev, struct vhost_virtqueue *vq)
514 if (!(dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)))
517 if (vq_is_packed(dev)) {
518 if (vring_translate_packed(dev, vq) < 0)
521 if (vring_translate_split(dev, vq) < 0)
525 if (log_translate(dev, vq) < 0)
528 vq->access_ok = true;
534 vring_invalidate(struct virtio_net *dev, struct vhost_virtqueue *vq)
536 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
537 vhost_user_iotlb_wr_lock(vq);
539 vq->access_ok = false;
543 vq->log_guest_addr = 0;
545 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
546 vhost_user_iotlb_wr_unlock(vq);
550 init_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
552 struct vhost_virtqueue *vq;
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;
576 reset_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
578 struct vhost_virtqueue *vq;
581 if (vring_idx >= VHOST_MAX_VRING) {
582 VHOST_LOG_CONFIG(ERR,
583 "Failed not init vring, out of bound (%d)\n",
588 vq = dev->virtqueue[vring_idx];
590 VHOST_LOG_CONFIG(ERR, "Virtqueue not allocated (%d)\n",
596 init_vring_queue(dev, vring_idx);
601 alloc_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
603 struct vhost_virtqueue *vq;
606 /* Also allocate holes, if any, up to requested vring index. */
607 for (i = 0; i <= vring_idx; i++) {
608 if (dev->virtqueue[i])
611 vq = rte_malloc(NULL, sizeof(struct vhost_virtqueue), 0);
613 VHOST_LOG_CONFIG(ERR,
614 "Failed to allocate memory for vring:%u.\n", i);
618 dev->virtqueue[i] = vq;
619 init_vring_queue(dev, i);
620 rte_spinlock_init(&vq->access_lock);
621 vq->avail_wrap_counter = 1;
622 vq->used_wrap_counter = 1;
623 vq->signalled_used_valid = false;
626 dev->nr_vring = RTE_MAX(dev->nr_vring, vring_idx + 1);
632 * Reset some variables in device structure, while keeping few
633 * others untouched, such as vid, ifname, nr_vring: they
634 * should be same unless the device is removed.
637 reset_device(struct virtio_net *dev)
642 dev->protocol_features = 0;
643 dev->flags &= VIRTIO_DEV_BUILTIN_VIRTIO_NET;
645 for (i = 0; i < dev->nr_vring; i++)
646 reset_vring_queue(dev, i);
650 * Invoked when there is a new vhost-user connection established (when
651 * there is a new virtio device being attached).
654 vhost_new_device(void)
656 struct virtio_net *dev;
659 pthread_mutex_lock(&vhost_dev_lock);
660 for (i = 0; i < MAX_VHOST_DEVICE; i++) {
661 if (vhost_devices[i] == NULL)
665 if (i == MAX_VHOST_DEVICE) {
666 VHOST_LOG_CONFIG(ERR,
667 "Failed to find a free slot for new device.\n");
668 pthread_mutex_unlock(&vhost_dev_lock);
672 dev = rte_zmalloc(NULL, sizeof(struct virtio_net), 0);
674 VHOST_LOG_CONFIG(ERR,
675 "Failed to allocate memory for new dev.\n");
676 pthread_mutex_unlock(&vhost_dev_lock);
680 vhost_devices[i] = dev;
681 pthread_mutex_unlock(&vhost_dev_lock);
684 dev->flags = VIRTIO_DEV_BUILTIN_VIRTIO_NET;
685 dev->slave_req_fd = -1;
686 dev->postcopy_ufd = -1;
687 rte_spinlock_init(&dev->slave_req_lock);
693 vhost_destroy_device_notify(struct virtio_net *dev)
695 struct rte_vdpa_device *vdpa_dev;
697 if (dev->flags & VIRTIO_DEV_RUNNING) {
698 vdpa_dev = dev->vdpa_dev;
700 vdpa_dev->ops->dev_close(dev->vid);
701 dev->flags &= ~VIRTIO_DEV_RUNNING;
702 dev->notify_ops->destroy_device(dev->vid);
707 * Invoked when there is the vhost-user connection is broken (when
708 * the virtio device is being detached).
711 vhost_destroy_device(int vid)
713 struct virtio_net *dev = get_device(vid);
718 vhost_destroy_device_notify(dev);
720 cleanup_device(dev, 1);
723 vhost_devices[vid] = NULL;
727 vhost_attach_vdpa_device(int vid, struct rte_vdpa_device *vdpa_dev)
729 struct virtio_net *dev = get_device(vid);
734 dev->vdpa_dev = vdpa_dev;
738 vhost_set_ifname(int vid, const char *if_name, unsigned int if_len)
740 struct virtio_net *dev;
743 dev = get_device(vid);
747 len = if_len > sizeof(dev->ifname) ?
748 sizeof(dev->ifname) : if_len;
750 strncpy(dev->ifname, if_name, len);
751 dev->ifname[sizeof(dev->ifname) - 1] = '\0';
755 vhost_set_builtin_virtio_net(int vid, bool enable)
757 struct virtio_net *dev = get_device(vid);
763 dev->flags |= VIRTIO_DEV_BUILTIN_VIRTIO_NET;
765 dev->flags &= ~VIRTIO_DEV_BUILTIN_VIRTIO_NET;
769 vhost_enable_extbuf(int vid)
771 struct virtio_net *dev = get_device(vid);
780 vhost_enable_linearbuf(int vid)
782 struct virtio_net *dev = get_device(vid);
791 rte_vhost_get_mtu(int vid, uint16_t *mtu)
793 struct virtio_net *dev = get_device(vid);
795 if (dev == NULL || mtu == NULL)
798 if (!(dev->flags & VIRTIO_DEV_READY))
801 if (!(dev->features & (1ULL << VIRTIO_NET_F_MTU)))
810 rte_vhost_get_numa_node(int vid)
812 #ifdef RTE_LIBRTE_VHOST_NUMA
813 struct virtio_net *dev = get_device(vid);
817 if (dev == NULL || numa_available() != 0)
820 ret = get_mempolicy(&numa_node, NULL, 0, dev,
821 MPOL_F_NODE | MPOL_F_ADDR);
823 VHOST_LOG_CONFIG(ERR,
824 "(%d) failed to query numa node: %s\n",
825 vid, rte_strerror(errno));
837 rte_vhost_get_queue_num(int vid)
839 struct virtio_net *dev = get_device(vid);
844 return dev->nr_vring / 2;
848 rte_vhost_get_vring_num(int vid)
850 struct virtio_net *dev = get_device(vid);
855 return dev->nr_vring;
859 rte_vhost_get_ifname(int vid, char *buf, size_t len)
861 struct virtio_net *dev = get_device(vid);
863 if (dev == NULL || buf == NULL)
866 len = RTE_MIN(len, sizeof(dev->ifname));
868 strncpy(buf, dev->ifname, len);
875 rte_vhost_get_negotiated_features(int vid, uint64_t *features)
877 struct virtio_net *dev;
879 dev = get_device(vid);
880 if (dev == NULL || features == NULL)
883 *features = dev->features;
888 rte_vhost_get_negotiated_protocol_features(int vid,
889 uint64_t *protocol_features)
891 struct virtio_net *dev;
893 dev = get_device(vid);
894 if (dev == NULL || protocol_features == NULL)
897 *protocol_features = dev->protocol_features;
902 rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem)
904 struct virtio_net *dev;
905 struct rte_vhost_memory *m;
908 dev = get_device(vid);
909 if (dev == NULL || mem == NULL)
912 size = dev->mem->nregions * sizeof(struct rte_vhost_mem_region);
913 m = malloc(sizeof(struct rte_vhost_memory) + size);
917 m->nregions = dev->mem->nregions;
918 memcpy(m->regions, dev->mem->regions, size);
925 rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
926 struct rte_vhost_vring *vring)
928 struct virtio_net *dev;
929 struct vhost_virtqueue *vq;
931 dev = get_device(vid);
932 if (dev == NULL || vring == NULL)
935 if (vring_idx >= VHOST_MAX_VRING)
938 vq = dev->virtqueue[vring_idx];
942 if (vq_is_packed(dev)) {
943 vring->desc_packed = vq->desc_packed;
944 vring->driver_event = vq->driver_event;
945 vring->device_event = vq->device_event;
947 vring->desc = vq->desc;
948 vring->avail = vq->avail;
949 vring->used = vq->used;
951 vring->log_guest_addr = vq->log_guest_addr;
953 vring->callfd = vq->callfd;
954 vring->kickfd = vq->kickfd;
955 vring->size = vq->size;
961 rte_vhost_get_vhost_ring_inflight(int vid, uint16_t vring_idx,
962 struct rte_vhost_ring_inflight *vring)
964 struct virtio_net *dev;
965 struct vhost_virtqueue *vq;
967 dev = get_device(vid);
971 if (vring_idx >= VHOST_MAX_VRING)
974 vq = dev->virtqueue[vring_idx];
978 if (vq_is_packed(dev)) {
979 if (unlikely(!vq->inflight_packed))
982 vring->inflight_packed = vq->inflight_packed;
984 if (unlikely(!vq->inflight_split))
987 vring->inflight_split = vq->inflight_split;
990 vring->resubmit_inflight = vq->resubmit_inflight;
996 rte_vhost_set_inflight_desc_split(int vid, uint16_t vring_idx,
999 struct vhost_virtqueue *vq;
1000 struct virtio_net *dev;
1002 dev = get_device(vid);
1006 if (unlikely(!(dev->protocol_features &
1007 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1010 if (unlikely(vq_is_packed(dev)))
1013 if (unlikely(vring_idx >= VHOST_MAX_VRING))
1016 vq = dev->virtqueue[vring_idx];
1020 if (unlikely(!vq->inflight_split))
1023 if (unlikely(idx >= vq->size))
1026 vq->inflight_split->desc[idx].counter = vq->global_counter++;
1027 vq->inflight_split->desc[idx].inflight = 1;
1032 rte_vhost_set_inflight_desc_packed(int vid, uint16_t vring_idx,
1033 uint16_t head, uint16_t last,
1034 uint16_t *inflight_entry)
1036 struct rte_vhost_inflight_info_packed *inflight_info;
1037 struct virtio_net *dev;
1038 struct vhost_virtqueue *vq;
1039 struct vring_packed_desc *desc;
1040 uint16_t old_free_head, free_head;
1042 dev = get_device(vid);
1046 if (unlikely(!(dev->protocol_features &
1047 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1050 if (unlikely(!vq_is_packed(dev)))
1053 if (unlikely(vring_idx >= VHOST_MAX_VRING))
1056 vq = dev->virtqueue[vring_idx];
1060 inflight_info = vq->inflight_packed;
1061 if (unlikely(!inflight_info))
1064 if (unlikely(head >= vq->size))
1067 desc = vq->desc_packed;
1068 old_free_head = inflight_info->old_free_head;
1069 if (unlikely(old_free_head >= vq->size))
1072 free_head = old_free_head;
1074 /* init header descriptor */
1075 inflight_info->desc[old_free_head].num = 0;
1076 inflight_info->desc[old_free_head].counter = vq->global_counter++;
1077 inflight_info->desc[old_free_head].inflight = 1;
1079 /* save desc entry in flight entry */
1080 while (head != ((last + 1) % vq->size)) {
1081 inflight_info->desc[old_free_head].num++;
1082 inflight_info->desc[free_head].addr = desc[head].addr;
1083 inflight_info->desc[free_head].len = desc[head].len;
1084 inflight_info->desc[free_head].flags = desc[head].flags;
1085 inflight_info->desc[free_head].id = desc[head].id;
1087 inflight_info->desc[old_free_head].last = free_head;
1088 free_head = inflight_info->desc[free_head].next;
1089 inflight_info->free_head = free_head;
1090 head = (head + 1) % vq->size;
1093 inflight_info->old_free_head = free_head;
1094 *inflight_entry = old_free_head;
1100 rte_vhost_clr_inflight_desc_split(int vid, uint16_t vring_idx,
1101 uint16_t last_used_idx, uint16_t idx)
1103 struct virtio_net *dev;
1104 struct vhost_virtqueue *vq;
1106 dev = get_device(vid);
1110 if (unlikely(!(dev->protocol_features &
1111 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1114 if (unlikely(vq_is_packed(dev)))
1117 if (unlikely(vring_idx >= VHOST_MAX_VRING))
1120 vq = dev->virtqueue[vring_idx];
1124 if (unlikely(!vq->inflight_split))
1127 if (unlikely(idx >= vq->size))
1130 rte_atomic_thread_fence(__ATOMIC_SEQ_CST);
1132 vq->inflight_split->desc[idx].inflight = 0;
1134 rte_atomic_thread_fence(__ATOMIC_SEQ_CST);
1136 vq->inflight_split->used_idx = last_used_idx;
1141 rte_vhost_clr_inflight_desc_packed(int vid, uint16_t vring_idx,
1144 struct rte_vhost_inflight_info_packed *inflight_info;
1145 struct virtio_net *dev;
1146 struct vhost_virtqueue *vq;
1148 dev = get_device(vid);
1152 if (unlikely(!(dev->protocol_features &
1153 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1156 if (unlikely(!vq_is_packed(dev)))
1159 if (unlikely(vring_idx >= VHOST_MAX_VRING))
1162 vq = dev->virtqueue[vring_idx];
1166 inflight_info = vq->inflight_packed;
1167 if (unlikely(!inflight_info))
1170 if (unlikely(head >= vq->size))
1173 rte_atomic_thread_fence(__ATOMIC_SEQ_CST);
1175 inflight_info->desc[head].inflight = 0;
1177 rte_atomic_thread_fence(__ATOMIC_SEQ_CST);
1179 inflight_info->old_free_head = inflight_info->free_head;
1180 inflight_info->old_used_idx = inflight_info->used_idx;
1181 inflight_info->old_used_wrap_counter = inflight_info->used_wrap_counter;
1187 rte_vhost_set_last_inflight_io_split(int vid, uint16_t vring_idx,
1190 struct virtio_net *dev;
1191 struct vhost_virtqueue *vq;
1193 dev = get_device(vid);
1197 if (unlikely(!(dev->protocol_features &
1198 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1201 if (unlikely(vq_is_packed(dev)))
1204 if (unlikely(vring_idx >= VHOST_MAX_VRING))
1207 vq = dev->virtqueue[vring_idx];
1211 if (unlikely(!vq->inflight_split))
1214 vq->inflight_split->last_inflight_io = idx;
1219 rte_vhost_set_last_inflight_io_packed(int vid, uint16_t vring_idx,
1222 struct rte_vhost_inflight_info_packed *inflight_info;
1223 struct virtio_net *dev;
1224 struct vhost_virtqueue *vq;
1227 dev = get_device(vid);
1231 if (unlikely(!(dev->protocol_features &
1232 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1235 if (unlikely(!vq_is_packed(dev)))
1238 if (unlikely(vring_idx >= VHOST_MAX_VRING))
1241 vq = dev->virtqueue[vring_idx];
1245 inflight_info = vq->inflight_packed;
1246 if (unlikely(!inflight_info))
1249 if (unlikely(head >= vq->size))
1252 last = inflight_info->desc[head].last;
1253 if (unlikely(last >= vq->size))
1256 inflight_info->desc[last].next = inflight_info->free_head;
1257 inflight_info->free_head = head;
1258 inflight_info->used_idx += inflight_info->desc[head].num;
1259 if (inflight_info->used_idx >= inflight_info->desc_num) {
1260 inflight_info->used_idx -= inflight_info->desc_num;
1261 inflight_info->used_wrap_counter =
1262 !inflight_info->used_wrap_counter;
1269 rte_vhost_vring_call(int vid, uint16_t vring_idx)
1271 struct virtio_net *dev;
1272 struct vhost_virtqueue *vq;
1274 dev = get_device(vid);
1278 if (vring_idx >= VHOST_MAX_VRING)
1281 vq = dev->virtqueue[vring_idx];
1285 if (vq_is_packed(dev))
1286 vhost_vring_call_packed(dev, vq);
1288 vhost_vring_call_split(dev, vq);
1294 rte_vhost_avail_entries(int vid, uint16_t queue_id)
1296 struct virtio_net *dev;
1297 struct vhost_virtqueue *vq;
1300 dev = get_device(vid);
1304 if (queue_id >= VHOST_MAX_VRING)
1307 vq = dev->virtqueue[queue_id];
1311 rte_spinlock_lock(&vq->access_lock);
1313 if (unlikely(!vq->enabled || vq->avail == NULL))
1316 ret = *(volatile uint16_t *)&vq->avail->idx - vq->last_used_idx;
1319 rte_spinlock_unlock(&vq->access_lock);
1324 vhost_enable_notify_split(struct virtio_net *dev,
1325 struct vhost_virtqueue *vq, int enable)
1327 if (vq->used == NULL)
1330 if (!(dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))) {
1332 vq->used->flags &= ~VRING_USED_F_NO_NOTIFY;
1334 vq->used->flags |= VRING_USED_F_NO_NOTIFY;
1337 vhost_avail_event(vq) = vq->last_avail_idx;
1343 vhost_enable_notify_packed(struct virtio_net *dev,
1344 struct vhost_virtqueue *vq, int enable)
1348 if (vq->device_event == NULL)
1352 vq->device_event->flags = VRING_EVENT_F_DISABLE;
1356 flags = VRING_EVENT_F_ENABLE;
1357 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX)) {
1358 flags = VRING_EVENT_F_DESC;
1359 vq->device_event->off_wrap = vq->last_avail_idx |
1360 vq->avail_wrap_counter << 15;
1363 rte_atomic_thread_fence(__ATOMIC_RELEASE);
1365 vq->device_event->flags = flags;
1370 vhost_enable_guest_notification(struct virtio_net *dev,
1371 struct vhost_virtqueue *vq, int enable)
1374 * If the virtqueue is not ready yet, it will be applied
1375 * when it will become ready.
1380 if (vq_is_packed(dev))
1381 return vhost_enable_notify_packed(dev, vq, enable);
1383 return vhost_enable_notify_split(dev, vq, enable);
1387 rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable)
1389 struct virtio_net *dev = get_device(vid);
1390 struct vhost_virtqueue *vq;
1396 if (queue_id >= VHOST_MAX_VRING)
1399 vq = dev->virtqueue[queue_id];
1403 rte_spinlock_lock(&vq->access_lock);
1405 vq->notif_enable = enable;
1406 ret = vhost_enable_guest_notification(dev, vq, enable);
1408 rte_spinlock_unlock(&vq->access_lock);
1414 rte_vhost_log_write(int vid, uint64_t addr, uint64_t len)
1416 struct virtio_net *dev = get_device(vid);
1421 vhost_log_write(dev, addr, len);
1425 rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
1426 uint64_t offset, uint64_t len)
1428 struct virtio_net *dev;
1429 struct vhost_virtqueue *vq;
1431 dev = get_device(vid);
1435 if (vring_idx >= VHOST_MAX_VRING)
1437 vq = dev->virtqueue[vring_idx];
1441 vhost_log_used_vring(dev, vq, offset, len);
1445 rte_vhost_rx_queue_count(int vid, uint16_t qid)
1447 struct virtio_net *dev;
1448 struct vhost_virtqueue *vq;
1451 dev = get_device(vid);
1455 if (unlikely(qid >= dev->nr_vring || (qid & 1) == 0)) {
1456 VHOST_LOG_DATA(ERR, "(%d) %s: invalid virtqueue idx %d.\n",
1457 dev->vid, __func__, qid);
1461 vq = dev->virtqueue[qid];
1465 rte_spinlock_lock(&vq->access_lock);
1467 if (unlikely(!vq->enabled || vq->avail == NULL))
1470 ret = *((volatile uint16_t *)&vq->avail->idx) - vq->last_avail_idx;
1473 rte_spinlock_unlock(&vq->access_lock);
1477 struct rte_vdpa_device *
1478 rte_vhost_get_vdpa_device(int vid)
1480 struct virtio_net *dev = get_device(vid);
1485 return dev->vdpa_dev;
1488 int rte_vhost_get_log_base(int vid, uint64_t *log_base,
1491 struct virtio_net *dev = get_device(vid);
1493 if (dev == NULL || log_base == NULL || log_size == NULL)
1496 *log_base = dev->log_base;
1497 *log_size = dev->log_size;
1502 int rte_vhost_get_vring_base(int vid, uint16_t queue_id,
1503 uint16_t *last_avail_idx, uint16_t *last_used_idx)
1505 struct vhost_virtqueue *vq;
1506 struct virtio_net *dev = get_device(vid);
1508 if (dev == NULL || last_avail_idx == NULL || last_used_idx == NULL)
1511 if (queue_id >= VHOST_MAX_VRING)
1514 vq = dev->virtqueue[queue_id];
1518 if (vq_is_packed(dev)) {
1519 *last_avail_idx = (vq->avail_wrap_counter << 15) |
1521 *last_used_idx = (vq->used_wrap_counter << 15) |
1524 *last_avail_idx = vq->last_avail_idx;
1525 *last_used_idx = vq->last_used_idx;
1531 int rte_vhost_set_vring_base(int vid, uint16_t queue_id,
1532 uint16_t last_avail_idx, uint16_t last_used_idx)
1534 struct vhost_virtqueue *vq;
1535 struct virtio_net *dev = get_device(vid);
1540 if (queue_id >= VHOST_MAX_VRING)
1543 vq = dev->virtqueue[queue_id];
1547 if (vq_is_packed(dev)) {
1548 vq->last_avail_idx = last_avail_idx & 0x7fff;
1549 vq->avail_wrap_counter = !!(last_avail_idx & (1 << 15));
1550 vq->last_used_idx = last_used_idx & 0x7fff;
1551 vq->used_wrap_counter = !!(last_used_idx & (1 << 15));
1553 vq->last_avail_idx = last_avail_idx;
1554 vq->last_used_idx = last_used_idx;
1561 rte_vhost_get_vring_base_from_inflight(int vid,
1563 uint16_t *last_avail_idx,
1564 uint16_t *last_used_idx)
1566 struct rte_vhost_inflight_info_packed *inflight_info;
1567 struct vhost_virtqueue *vq;
1568 struct virtio_net *dev = get_device(vid);
1570 if (dev == NULL || last_avail_idx == NULL || last_used_idx == NULL)
1573 if (queue_id >= VHOST_MAX_VRING)
1576 vq = dev->virtqueue[queue_id];
1580 if (!vq_is_packed(dev))
1583 inflight_info = vq->inflight_packed;
1587 *last_avail_idx = (inflight_info->old_used_wrap_counter << 15) |
1588 inflight_info->old_used_idx;
1589 *last_used_idx = *last_avail_idx;
1594 int rte_vhost_extern_callback_register(int vid,
1595 struct rte_vhost_user_extern_ops const * const ops, void *ctx)
1597 struct virtio_net *dev = get_device(vid);
1599 if (dev == NULL || ops == NULL)
1602 dev->extern_ops = *ops;
1603 dev->extern_data = ctx;
1607 int rte_vhost_async_channel_register(int vid, uint16_t queue_id,
1609 struct rte_vhost_async_channel_ops *ops)
1611 struct vhost_virtqueue *vq;
1612 struct virtio_net *dev = get_device(vid);
1613 struct rte_vhost_async_features f;
1616 if (dev == NULL || ops == NULL)
1619 f.intval = features;
1621 if (queue_id >= VHOST_MAX_VRING)
1624 vq = dev->virtqueue[queue_id];
1626 if (unlikely(vq == NULL || !dev->async_copy))
1629 /* packed queue is not supported */
1630 if (unlikely(vq_is_packed(dev) || !f.async_inorder)) {
1631 VHOST_LOG_CONFIG(ERR,
1632 "async copy is not supported on packed queue or non-inorder mode "
1633 "(vid %d, qid: %d)\n", vid, queue_id);
1637 if (unlikely(ops->check_completed_copies == NULL ||
1638 ops->transfer_data == NULL))
1641 rte_spinlock_lock(&vq->access_lock);
1643 if (unlikely(vq->async_registered)) {
1644 VHOST_LOG_CONFIG(ERR,
1645 "async register failed: channel already registered "
1646 "(vid %d, qid: %d)\n", vid, queue_id);
1650 #ifdef RTE_LIBRTE_VHOST_NUMA
1651 if (get_mempolicy(&node, NULL, 0, vq, MPOL_F_NODE | MPOL_F_ADDR)) {
1652 VHOST_LOG_CONFIG(ERR,
1653 "unable to get numa information in async register. "
1654 "allocating async buffer memory on the caller thread node\n");
1655 node = SOCKET_ID_ANY;
1658 node = SOCKET_ID_ANY;
1661 vq->async_pkts_info = rte_malloc_socket(NULL,
1662 vq->size * sizeof(struct async_inflight_info),
1663 RTE_CACHE_LINE_SIZE, node);
1664 vq->it_pool = rte_malloc_socket(NULL,
1665 VHOST_MAX_ASYNC_IT * sizeof(struct rte_vhost_iov_iter),
1666 RTE_CACHE_LINE_SIZE, node);
1667 vq->vec_pool = rte_malloc_socket(NULL,
1668 VHOST_MAX_ASYNC_VEC * sizeof(struct iovec),
1669 RTE_CACHE_LINE_SIZE, node);
1670 vq->async_descs_split = rte_malloc_socket(NULL,
1671 vq->size * sizeof(struct vring_used_elem),
1672 RTE_CACHE_LINE_SIZE, node);
1673 if (!vq->async_descs_split || !vq->async_pkts_info ||
1674 !vq->it_pool || !vq->vec_pool) {
1675 vhost_free_async_mem(vq);
1676 VHOST_LOG_CONFIG(ERR,
1677 "async register failed: cannot allocate memory for vq data "
1678 "(vid %d, qid: %d)\n", vid, queue_id);
1682 vq->async_ops.check_completed_copies = ops->check_completed_copies;
1683 vq->async_ops.transfer_data = ops->transfer_data;
1685 vq->async_inorder = f.async_inorder;
1686 vq->async_threshold = f.async_threshold;
1688 vq->async_registered = true;
1691 rte_spinlock_unlock(&vq->access_lock);
1696 int rte_vhost_async_channel_unregister(int vid, uint16_t queue_id)
1698 struct vhost_virtqueue *vq;
1699 struct virtio_net *dev = get_device(vid);
1705 if (queue_id >= VHOST_MAX_VRING)
1708 vq = dev->virtqueue[queue_id];
1715 if (!vq->async_registered)
1718 if (!rte_spinlock_trylock(&vq->access_lock)) {
1719 VHOST_LOG_CONFIG(ERR, "Failed to unregister async channel. "
1720 "virt queue busy.\n");
1724 if (vq->async_pkts_inflight_n) {
1725 VHOST_LOG_CONFIG(ERR, "Failed to unregister async channel. "
1726 "async inflight packets must be completed before unregistration.\n");
1731 vhost_free_async_mem(vq);
1733 vq->async_ops.transfer_data = NULL;
1734 vq->async_ops.check_completed_copies = NULL;
1735 vq->async_registered = false;
1738 rte_spinlock_unlock(&vq->access_lock);
1743 RTE_LOG_REGISTER(vhost_config_log_level, lib.vhost.config, INFO);
1744 RTE_LOG_REGISTER(vhost_data_log_level, lib.vhost.data, WARNING);