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 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;
553 if (vring_idx >= VHOST_MAX_VRING) {
554 VHOST_LOG_CONFIG(ERR,
555 "Failed not init vring, out of bound (%d)\n",
560 vq = dev->virtqueue[vring_idx];
562 VHOST_LOG_CONFIG(ERR, "Virtqueue not allocated (%d)\n",
567 memset(vq, 0, sizeof(struct vhost_virtqueue));
569 vq->kickfd = VIRTIO_UNINITIALIZED_EVENTFD;
570 vq->callfd = VIRTIO_UNINITIALIZED_EVENTFD;
571 vq->notif_enable = VIRTIO_UNINITIALIZED_NOTIF;
573 vhost_user_iotlb_init(dev, vring_idx);
577 reset_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
579 struct vhost_virtqueue *vq;
582 if (vring_idx >= VHOST_MAX_VRING) {
583 VHOST_LOG_CONFIG(ERR,
584 "Failed not init vring, out of bound (%d)\n",
589 vq = dev->virtqueue[vring_idx];
591 VHOST_LOG_CONFIG(ERR, "Virtqueue not allocated (%d)\n",
597 init_vring_queue(dev, vring_idx);
602 alloc_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
604 struct vhost_virtqueue *vq;
607 /* Also allocate holes, if any, up to requested vring index. */
608 for (i = 0; i <= vring_idx; i++) {
609 if (dev->virtqueue[i])
612 vq = rte_zmalloc(NULL, sizeof(struct vhost_virtqueue), 0);
614 VHOST_LOG_CONFIG(ERR,
615 "Failed to allocate memory for vring:%u.\n", i);
619 dev->virtqueue[i] = vq;
620 init_vring_queue(dev, i);
621 rte_spinlock_init(&vq->access_lock);
622 vq->avail_wrap_counter = 1;
623 vq->used_wrap_counter = 1;
624 vq->signalled_used_valid = false;
627 dev->nr_vring = RTE_MAX(dev->nr_vring, vring_idx + 1);
633 * Reset some variables in device structure, while keeping few
634 * others untouched, such as vid, ifname, nr_vring: they
635 * should be same unless the device is removed.
638 reset_device(struct virtio_net *dev)
643 dev->protocol_features = 0;
644 dev->flags &= VIRTIO_DEV_BUILTIN_VIRTIO_NET;
646 for (i = 0; i < dev->nr_vring; i++)
647 reset_vring_queue(dev, i);
651 * Invoked when there is a new vhost-user connection established (when
652 * there is a new virtio device being attached).
655 vhost_new_device(void)
657 struct virtio_net *dev;
660 pthread_mutex_lock(&vhost_dev_lock);
661 for (i = 0; i < MAX_VHOST_DEVICE; i++) {
662 if (vhost_devices[i] == NULL)
666 if (i == MAX_VHOST_DEVICE) {
667 VHOST_LOG_CONFIG(ERR,
668 "Failed to find a free slot for new device.\n");
669 pthread_mutex_unlock(&vhost_dev_lock);
673 dev = rte_zmalloc(NULL, sizeof(struct virtio_net), 0);
675 VHOST_LOG_CONFIG(ERR,
676 "Failed to allocate memory for new dev.\n");
677 pthread_mutex_unlock(&vhost_dev_lock);
681 vhost_devices[i] = dev;
682 pthread_mutex_unlock(&vhost_dev_lock);
685 dev->flags = VIRTIO_DEV_BUILTIN_VIRTIO_NET;
686 dev->slave_req_fd = -1;
687 dev->postcopy_ufd = -1;
688 rte_spinlock_init(&dev->slave_req_lock);
694 vhost_destroy_device_notify(struct virtio_net *dev)
696 struct rte_vdpa_device *vdpa_dev;
698 if (dev->flags & VIRTIO_DEV_RUNNING) {
699 vdpa_dev = dev->vdpa_dev;
701 vdpa_dev->ops->dev_close(dev->vid);
702 dev->flags &= ~VIRTIO_DEV_RUNNING;
703 dev->notify_ops->destroy_device(dev->vid);
708 * Invoked when there is the vhost-user connection is broken (when
709 * the virtio device is being detached).
712 vhost_destroy_device(int vid)
714 struct virtio_net *dev = get_device(vid);
719 vhost_destroy_device_notify(dev);
721 cleanup_device(dev, 1);
724 vhost_devices[vid] = NULL;
728 vhost_attach_vdpa_device(int vid, struct rte_vdpa_device *vdpa_dev)
730 struct virtio_net *dev = get_device(vid);
735 dev->vdpa_dev = vdpa_dev;
739 vhost_set_ifname(int vid, const char *if_name, unsigned int if_len)
741 struct virtio_net *dev;
744 dev = get_device(vid);
748 len = if_len > sizeof(dev->ifname) ?
749 sizeof(dev->ifname) : if_len;
751 strncpy(dev->ifname, if_name, len);
752 dev->ifname[sizeof(dev->ifname) - 1] = '\0';
756 vhost_setup_virtio_net(int vid, bool enable, bool compliant_ol_flags)
758 struct virtio_net *dev = get_device(vid);
764 dev->flags |= VIRTIO_DEV_BUILTIN_VIRTIO_NET;
766 dev->flags &= ~VIRTIO_DEV_BUILTIN_VIRTIO_NET;
767 if (!compliant_ol_flags)
768 dev->flags |= VIRTIO_DEV_LEGACY_OL_FLAGS;
770 dev->flags &= ~VIRTIO_DEV_LEGACY_OL_FLAGS;
774 vhost_enable_extbuf(int vid)
776 struct virtio_net *dev = get_device(vid);
785 vhost_enable_linearbuf(int vid)
787 struct virtio_net *dev = get_device(vid);
796 rte_vhost_get_mtu(int vid, uint16_t *mtu)
798 struct virtio_net *dev = get_device(vid);
800 if (dev == NULL || mtu == NULL)
803 if (!(dev->flags & VIRTIO_DEV_READY))
806 if (!(dev->features & (1ULL << VIRTIO_NET_F_MTU)))
815 rte_vhost_get_numa_node(int vid)
817 #ifdef RTE_LIBRTE_VHOST_NUMA
818 struct virtio_net *dev = get_device(vid);
822 if (dev == NULL || numa_available() != 0)
825 ret = get_mempolicy(&numa_node, NULL, 0, dev,
826 MPOL_F_NODE | MPOL_F_ADDR);
828 VHOST_LOG_CONFIG(ERR,
829 "(%d) failed to query numa node: %s\n",
830 vid, rte_strerror(errno));
842 rte_vhost_get_queue_num(int vid)
844 struct virtio_net *dev = get_device(vid);
849 return dev->nr_vring / 2;
853 rte_vhost_get_vring_num(int vid)
855 struct virtio_net *dev = get_device(vid);
860 return dev->nr_vring;
864 rte_vhost_get_ifname(int vid, char *buf, size_t len)
866 struct virtio_net *dev = get_device(vid);
868 if (dev == NULL || buf == NULL)
871 len = RTE_MIN(len, sizeof(dev->ifname));
873 strncpy(buf, dev->ifname, len);
880 rte_vhost_get_negotiated_features(int vid, uint64_t *features)
882 struct virtio_net *dev;
884 dev = get_device(vid);
885 if (dev == NULL || features == NULL)
888 *features = dev->features;
893 rte_vhost_get_negotiated_protocol_features(int vid,
894 uint64_t *protocol_features)
896 struct virtio_net *dev;
898 dev = get_device(vid);
899 if (dev == NULL || protocol_features == NULL)
902 *protocol_features = dev->protocol_features;
907 rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem)
909 struct virtio_net *dev;
910 struct rte_vhost_memory *m;
913 dev = get_device(vid);
914 if (dev == NULL || mem == NULL)
917 size = dev->mem->nregions * sizeof(struct rte_vhost_mem_region);
918 m = malloc(sizeof(struct rte_vhost_memory) + size);
922 m->nregions = dev->mem->nregions;
923 memcpy(m->regions, dev->mem->regions, size);
930 rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
931 struct rte_vhost_vring *vring)
933 struct virtio_net *dev;
934 struct vhost_virtqueue *vq;
936 dev = get_device(vid);
937 if (dev == NULL || vring == NULL)
940 if (vring_idx >= VHOST_MAX_VRING)
943 vq = dev->virtqueue[vring_idx];
947 if (vq_is_packed(dev)) {
948 vring->desc_packed = vq->desc_packed;
949 vring->driver_event = vq->driver_event;
950 vring->device_event = vq->device_event;
952 vring->desc = vq->desc;
953 vring->avail = vq->avail;
954 vring->used = vq->used;
956 vring->log_guest_addr = vq->log_guest_addr;
958 vring->callfd = vq->callfd;
959 vring->kickfd = vq->kickfd;
960 vring->size = vq->size;
966 rte_vhost_get_vhost_ring_inflight(int vid, uint16_t vring_idx,
967 struct rte_vhost_ring_inflight *vring)
969 struct virtio_net *dev;
970 struct vhost_virtqueue *vq;
972 dev = get_device(vid);
976 if (vring_idx >= VHOST_MAX_VRING)
979 vq = dev->virtqueue[vring_idx];
983 if (vq_is_packed(dev)) {
984 if (unlikely(!vq->inflight_packed))
987 vring->inflight_packed = vq->inflight_packed;
989 if (unlikely(!vq->inflight_split))
992 vring->inflight_split = vq->inflight_split;
995 vring->resubmit_inflight = vq->resubmit_inflight;
1001 rte_vhost_set_inflight_desc_split(int vid, uint16_t vring_idx,
1004 struct vhost_virtqueue *vq;
1005 struct virtio_net *dev;
1007 dev = get_device(vid);
1011 if (unlikely(!(dev->protocol_features &
1012 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1015 if (unlikely(vq_is_packed(dev)))
1018 if (unlikely(vring_idx >= VHOST_MAX_VRING))
1021 vq = dev->virtqueue[vring_idx];
1025 if (unlikely(!vq->inflight_split))
1028 if (unlikely(idx >= vq->size))
1031 vq->inflight_split->desc[idx].counter = vq->global_counter++;
1032 vq->inflight_split->desc[idx].inflight = 1;
1037 rte_vhost_set_inflight_desc_packed(int vid, uint16_t vring_idx,
1038 uint16_t head, uint16_t last,
1039 uint16_t *inflight_entry)
1041 struct rte_vhost_inflight_info_packed *inflight_info;
1042 struct virtio_net *dev;
1043 struct vhost_virtqueue *vq;
1044 struct vring_packed_desc *desc;
1045 uint16_t old_free_head, free_head;
1047 dev = get_device(vid);
1051 if (unlikely(!(dev->protocol_features &
1052 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1055 if (unlikely(!vq_is_packed(dev)))
1058 if (unlikely(vring_idx >= VHOST_MAX_VRING))
1061 vq = dev->virtqueue[vring_idx];
1065 inflight_info = vq->inflight_packed;
1066 if (unlikely(!inflight_info))
1069 if (unlikely(head >= vq->size))
1072 desc = vq->desc_packed;
1073 old_free_head = inflight_info->old_free_head;
1074 if (unlikely(old_free_head >= vq->size))
1077 free_head = old_free_head;
1079 /* init header descriptor */
1080 inflight_info->desc[old_free_head].num = 0;
1081 inflight_info->desc[old_free_head].counter = vq->global_counter++;
1082 inflight_info->desc[old_free_head].inflight = 1;
1084 /* save desc entry in flight entry */
1085 while (head != ((last + 1) % vq->size)) {
1086 inflight_info->desc[old_free_head].num++;
1087 inflight_info->desc[free_head].addr = desc[head].addr;
1088 inflight_info->desc[free_head].len = desc[head].len;
1089 inflight_info->desc[free_head].flags = desc[head].flags;
1090 inflight_info->desc[free_head].id = desc[head].id;
1092 inflight_info->desc[old_free_head].last = free_head;
1093 free_head = inflight_info->desc[free_head].next;
1094 inflight_info->free_head = free_head;
1095 head = (head + 1) % vq->size;
1098 inflight_info->old_free_head = free_head;
1099 *inflight_entry = old_free_head;
1105 rte_vhost_clr_inflight_desc_split(int vid, uint16_t vring_idx,
1106 uint16_t last_used_idx, uint16_t idx)
1108 struct virtio_net *dev;
1109 struct vhost_virtqueue *vq;
1111 dev = get_device(vid);
1115 if (unlikely(!(dev->protocol_features &
1116 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1119 if (unlikely(vq_is_packed(dev)))
1122 if (unlikely(vring_idx >= VHOST_MAX_VRING))
1125 vq = dev->virtqueue[vring_idx];
1129 if (unlikely(!vq->inflight_split))
1132 if (unlikely(idx >= vq->size))
1135 rte_atomic_thread_fence(__ATOMIC_SEQ_CST);
1137 vq->inflight_split->desc[idx].inflight = 0;
1139 rte_atomic_thread_fence(__ATOMIC_SEQ_CST);
1141 vq->inflight_split->used_idx = last_used_idx;
1146 rte_vhost_clr_inflight_desc_packed(int vid, uint16_t vring_idx,
1149 struct rte_vhost_inflight_info_packed *inflight_info;
1150 struct virtio_net *dev;
1151 struct vhost_virtqueue *vq;
1153 dev = get_device(vid);
1157 if (unlikely(!(dev->protocol_features &
1158 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1161 if (unlikely(!vq_is_packed(dev)))
1164 if (unlikely(vring_idx >= VHOST_MAX_VRING))
1167 vq = dev->virtqueue[vring_idx];
1171 inflight_info = vq->inflight_packed;
1172 if (unlikely(!inflight_info))
1175 if (unlikely(head >= vq->size))
1178 rte_atomic_thread_fence(__ATOMIC_SEQ_CST);
1180 inflight_info->desc[head].inflight = 0;
1182 rte_atomic_thread_fence(__ATOMIC_SEQ_CST);
1184 inflight_info->old_free_head = inflight_info->free_head;
1185 inflight_info->old_used_idx = inflight_info->used_idx;
1186 inflight_info->old_used_wrap_counter = inflight_info->used_wrap_counter;
1192 rte_vhost_set_last_inflight_io_split(int vid, uint16_t vring_idx,
1195 struct virtio_net *dev;
1196 struct vhost_virtqueue *vq;
1198 dev = get_device(vid);
1202 if (unlikely(!(dev->protocol_features &
1203 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1206 if (unlikely(vq_is_packed(dev)))
1209 if (unlikely(vring_idx >= VHOST_MAX_VRING))
1212 vq = dev->virtqueue[vring_idx];
1216 if (unlikely(!vq->inflight_split))
1219 vq->inflight_split->last_inflight_io = idx;
1224 rte_vhost_set_last_inflight_io_packed(int vid, uint16_t vring_idx,
1227 struct rte_vhost_inflight_info_packed *inflight_info;
1228 struct virtio_net *dev;
1229 struct vhost_virtqueue *vq;
1232 dev = get_device(vid);
1236 if (unlikely(!(dev->protocol_features &
1237 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1240 if (unlikely(!vq_is_packed(dev)))
1243 if (unlikely(vring_idx >= VHOST_MAX_VRING))
1246 vq = dev->virtqueue[vring_idx];
1250 inflight_info = vq->inflight_packed;
1251 if (unlikely(!inflight_info))
1254 if (unlikely(head >= vq->size))
1257 last = inflight_info->desc[head].last;
1258 if (unlikely(last >= vq->size))
1261 inflight_info->desc[last].next = inflight_info->free_head;
1262 inflight_info->free_head = head;
1263 inflight_info->used_idx += inflight_info->desc[head].num;
1264 if (inflight_info->used_idx >= inflight_info->desc_num) {
1265 inflight_info->used_idx -= inflight_info->desc_num;
1266 inflight_info->used_wrap_counter =
1267 !inflight_info->used_wrap_counter;
1274 rte_vhost_vring_call(int vid, uint16_t vring_idx)
1276 struct virtio_net *dev;
1277 struct vhost_virtqueue *vq;
1279 dev = get_device(vid);
1283 if (vring_idx >= VHOST_MAX_VRING)
1286 vq = dev->virtqueue[vring_idx];
1290 if (vq_is_packed(dev))
1291 vhost_vring_call_packed(dev, vq);
1293 vhost_vring_call_split(dev, vq);
1299 rte_vhost_avail_entries(int vid, uint16_t queue_id)
1301 struct virtio_net *dev;
1302 struct vhost_virtqueue *vq;
1305 dev = get_device(vid);
1309 if (queue_id >= VHOST_MAX_VRING)
1312 vq = dev->virtqueue[queue_id];
1316 rte_spinlock_lock(&vq->access_lock);
1318 if (unlikely(!vq->enabled || vq->avail == NULL))
1321 ret = *(volatile uint16_t *)&vq->avail->idx - vq->last_used_idx;
1324 rte_spinlock_unlock(&vq->access_lock);
1329 vhost_enable_notify_split(struct virtio_net *dev,
1330 struct vhost_virtqueue *vq, int enable)
1332 if (vq->used == NULL)
1335 if (!(dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))) {
1337 vq->used->flags &= ~VRING_USED_F_NO_NOTIFY;
1339 vq->used->flags |= VRING_USED_F_NO_NOTIFY;
1342 vhost_avail_event(vq) = vq->last_avail_idx;
1348 vhost_enable_notify_packed(struct virtio_net *dev,
1349 struct vhost_virtqueue *vq, int enable)
1353 if (vq->device_event == NULL)
1357 vq->device_event->flags = VRING_EVENT_F_DISABLE;
1361 flags = VRING_EVENT_F_ENABLE;
1362 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX)) {
1363 flags = VRING_EVENT_F_DESC;
1364 vq->device_event->off_wrap = vq->last_avail_idx |
1365 vq->avail_wrap_counter << 15;
1368 rte_atomic_thread_fence(__ATOMIC_RELEASE);
1370 vq->device_event->flags = flags;
1375 vhost_enable_guest_notification(struct virtio_net *dev,
1376 struct vhost_virtqueue *vq, int enable)
1379 * If the virtqueue is not ready yet, it will be applied
1380 * when it will become ready.
1385 if (vq_is_packed(dev))
1386 return vhost_enable_notify_packed(dev, vq, enable);
1388 return vhost_enable_notify_split(dev, vq, enable);
1392 rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable)
1394 struct virtio_net *dev = get_device(vid);
1395 struct vhost_virtqueue *vq;
1401 if (queue_id >= VHOST_MAX_VRING)
1404 vq = dev->virtqueue[queue_id];
1408 rte_spinlock_lock(&vq->access_lock);
1410 vq->notif_enable = enable;
1411 ret = vhost_enable_guest_notification(dev, vq, enable);
1413 rte_spinlock_unlock(&vq->access_lock);
1419 rte_vhost_log_write(int vid, uint64_t addr, uint64_t len)
1421 struct virtio_net *dev = get_device(vid);
1426 vhost_log_write(dev, addr, len);
1430 rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
1431 uint64_t offset, uint64_t len)
1433 struct virtio_net *dev;
1434 struct vhost_virtqueue *vq;
1436 dev = get_device(vid);
1440 if (vring_idx >= VHOST_MAX_VRING)
1442 vq = dev->virtqueue[vring_idx];
1446 vhost_log_used_vring(dev, vq, offset, len);
1450 rte_vhost_rx_queue_count(int vid, uint16_t qid)
1452 struct virtio_net *dev;
1453 struct vhost_virtqueue *vq;
1456 dev = get_device(vid);
1460 if (unlikely(qid >= dev->nr_vring || (qid & 1) == 0)) {
1461 VHOST_LOG_DATA(ERR, "(%d) %s: invalid virtqueue idx %d.\n",
1462 dev->vid, __func__, qid);
1466 vq = dev->virtqueue[qid];
1470 rte_spinlock_lock(&vq->access_lock);
1472 if (unlikely(!vq->enabled || vq->avail == NULL))
1475 ret = *((volatile uint16_t *)&vq->avail->idx) - vq->last_avail_idx;
1478 rte_spinlock_unlock(&vq->access_lock);
1482 struct rte_vdpa_device *
1483 rte_vhost_get_vdpa_device(int vid)
1485 struct virtio_net *dev = get_device(vid);
1490 return dev->vdpa_dev;
1493 int rte_vhost_get_log_base(int vid, uint64_t *log_base,
1496 struct virtio_net *dev = get_device(vid);
1498 if (dev == NULL || log_base == NULL || log_size == NULL)
1501 *log_base = dev->log_base;
1502 *log_size = dev->log_size;
1507 int rte_vhost_get_vring_base(int vid, uint16_t queue_id,
1508 uint16_t *last_avail_idx, uint16_t *last_used_idx)
1510 struct vhost_virtqueue *vq;
1511 struct virtio_net *dev = get_device(vid);
1513 if (dev == NULL || last_avail_idx == NULL || last_used_idx == NULL)
1516 if (queue_id >= VHOST_MAX_VRING)
1519 vq = dev->virtqueue[queue_id];
1523 if (vq_is_packed(dev)) {
1524 *last_avail_idx = (vq->avail_wrap_counter << 15) |
1526 *last_used_idx = (vq->used_wrap_counter << 15) |
1529 *last_avail_idx = vq->last_avail_idx;
1530 *last_used_idx = vq->last_used_idx;
1536 int rte_vhost_set_vring_base(int vid, uint16_t queue_id,
1537 uint16_t last_avail_idx, uint16_t last_used_idx)
1539 struct vhost_virtqueue *vq;
1540 struct virtio_net *dev = get_device(vid);
1545 if (queue_id >= VHOST_MAX_VRING)
1548 vq = dev->virtqueue[queue_id];
1552 if (vq_is_packed(dev)) {
1553 vq->last_avail_idx = last_avail_idx & 0x7fff;
1554 vq->avail_wrap_counter = !!(last_avail_idx & (1 << 15));
1555 vq->last_used_idx = last_used_idx & 0x7fff;
1556 vq->used_wrap_counter = !!(last_used_idx & (1 << 15));
1558 vq->last_avail_idx = last_avail_idx;
1559 vq->last_used_idx = last_used_idx;
1566 rte_vhost_get_vring_base_from_inflight(int vid,
1568 uint16_t *last_avail_idx,
1569 uint16_t *last_used_idx)
1571 struct rte_vhost_inflight_info_packed *inflight_info;
1572 struct vhost_virtqueue *vq;
1573 struct virtio_net *dev = get_device(vid);
1575 if (dev == NULL || last_avail_idx == NULL || last_used_idx == NULL)
1578 if (queue_id >= VHOST_MAX_VRING)
1581 vq = dev->virtqueue[queue_id];
1585 if (!vq_is_packed(dev))
1588 inflight_info = vq->inflight_packed;
1592 *last_avail_idx = (inflight_info->old_used_wrap_counter << 15) |
1593 inflight_info->old_used_idx;
1594 *last_used_idx = *last_avail_idx;
1599 int rte_vhost_extern_callback_register(int vid,
1600 struct rte_vhost_user_extern_ops const * const ops, void *ctx)
1602 struct virtio_net *dev = get_device(vid);
1604 if (dev == NULL || ops == NULL)
1607 dev->extern_ops = *ops;
1608 dev->extern_data = ctx;
1612 int rte_vhost_async_channel_register(int vid, uint16_t queue_id,
1614 struct rte_vhost_async_channel_ops *ops)
1616 struct vhost_virtqueue *vq;
1617 struct virtio_net *dev = get_device(vid);
1618 struct rte_vhost_async_features f;
1621 if (dev == NULL || ops == NULL)
1624 f.intval = features;
1626 if (queue_id >= VHOST_MAX_VRING)
1629 vq = dev->virtqueue[queue_id];
1631 if (unlikely(vq == NULL || !dev->async_copy))
1634 if (unlikely(!f.async_inorder)) {
1635 VHOST_LOG_CONFIG(ERR,
1636 "async copy is not supported on non-inorder mode "
1637 "(vid %d, qid: %d)\n", vid, queue_id);
1641 if (unlikely(ops->check_completed_copies == NULL ||
1642 ops->transfer_data == NULL))
1645 rte_spinlock_lock(&vq->access_lock);
1647 if (unlikely(vq->async_registered)) {
1648 VHOST_LOG_CONFIG(ERR,
1649 "async register failed: channel already registered "
1650 "(vid %d, qid: %d)\n", vid, queue_id);
1654 #ifdef RTE_LIBRTE_VHOST_NUMA
1655 if (get_mempolicy(&node, NULL, 0, vq, MPOL_F_NODE | MPOL_F_ADDR)) {
1656 VHOST_LOG_CONFIG(ERR,
1657 "unable to get numa information in async register. "
1658 "allocating async buffer memory on the caller thread node\n");
1659 node = SOCKET_ID_ANY;
1662 node = SOCKET_ID_ANY;
1665 vq->async_pkts_info = rte_malloc_socket(NULL,
1666 vq->size * sizeof(struct async_inflight_info),
1667 RTE_CACHE_LINE_SIZE, node);
1668 if (!vq->async_pkts_info) {
1669 vhost_free_async_mem(vq);
1670 VHOST_LOG_CONFIG(ERR,
1671 "async register failed: cannot allocate memory for async_pkts_info "
1672 "(vid %d, qid: %d)\n", vid, queue_id);
1676 vq->it_pool = rte_malloc_socket(NULL,
1677 VHOST_MAX_ASYNC_IT * sizeof(struct rte_vhost_iov_iter),
1678 RTE_CACHE_LINE_SIZE, node);
1680 vhost_free_async_mem(vq);
1681 VHOST_LOG_CONFIG(ERR,
1682 "async register failed: cannot allocate memory for it_pool "
1683 "(vid %d, qid: %d)\n", vid, queue_id);
1687 vq->vec_pool = rte_malloc_socket(NULL,
1688 VHOST_MAX_ASYNC_VEC * sizeof(struct iovec),
1689 RTE_CACHE_LINE_SIZE, node);
1690 if (!vq->vec_pool) {
1691 vhost_free_async_mem(vq);
1692 VHOST_LOG_CONFIG(ERR,
1693 "async register failed: cannot allocate memory for vec_pool "
1694 "(vid %d, qid: %d)\n", vid, queue_id);
1698 if (vq_is_packed(dev)) {
1699 vq->async_buffers_packed = rte_malloc_socket(NULL,
1700 vq->size * sizeof(struct vring_used_elem_packed),
1701 RTE_CACHE_LINE_SIZE, node);
1702 if (!vq->async_buffers_packed) {
1703 vhost_free_async_mem(vq);
1704 VHOST_LOG_CONFIG(ERR,
1705 "async register failed: cannot allocate memory for async buffers "
1706 "(vid %d, qid: %d)\n", vid, queue_id);
1710 vq->async_descs_split = rte_malloc_socket(NULL,
1711 vq->size * sizeof(struct vring_used_elem),
1712 RTE_CACHE_LINE_SIZE, node);
1713 if (!vq->async_descs_split) {
1714 vhost_free_async_mem(vq);
1715 VHOST_LOG_CONFIG(ERR,
1716 "async register failed: cannot allocate memory for async descs "
1717 "(vid %d, qid: %d)\n", vid, queue_id);
1722 vq->async_ops.check_completed_copies = ops->check_completed_copies;
1723 vq->async_ops.transfer_data = ops->transfer_data;
1725 vq->async_inorder = f.async_inorder;
1726 vq->async_threshold = f.async_threshold;
1728 vq->async_registered = true;
1731 rte_spinlock_unlock(&vq->access_lock);
1736 int rte_vhost_async_channel_unregister(int vid, uint16_t queue_id)
1738 struct vhost_virtqueue *vq;
1739 struct virtio_net *dev = get_device(vid);
1745 if (queue_id >= VHOST_MAX_VRING)
1748 vq = dev->virtqueue[queue_id];
1755 if (!vq->async_registered)
1758 if (!rte_spinlock_trylock(&vq->access_lock)) {
1759 VHOST_LOG_CONFIG(ERR, "Failed to unregister async channel. "
1760 "virt queue busy.\n");
1764 if (vq->async_pkts_inflight_n) {
1765 VHOST_LOG_CONFIG(ERR, "Failed to unregister async channel. "
1766 "async inflight packets must be completed before unregistration.\n");
1771 vhost_free_async_mem(vq);
1773 vq->async_ops.transfer_data = NULL;
1774 vq->async_ops.check_completed_copies = NULL;
1775 vq->async_registered = false;
1778 rte_spinlock_unlock(&vq->access_lock);
1783 RTE_LOG_REGISTER_SUFFIX(vhost_config_log_level, config, INFO);
1784 RTE_LOG_REGISTER_SUFFIX(vhost_data_log_level, data, WARNING);