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 rte_atomic_thread_fence(__ATOMIC_RELEASE);
150 log_base = (unsigned long *)(uintptr_t)dev->log_base;
152 for (i = 0; i < vq->log_cache_nb_elem; i++) {
153 struct log_cache_entry *elem = vq->log_cache + i;
155 #if defined(RTE_TOOLCHAIN_GCC) && (GCC_VERSION < 70100)
157 * '__sync' builtins are deprecated, but '__atomic' ones
158 * are sub-optimized in older GCC versions.
160 __sync_fetch_and_or(log_base + elem->offset, elem->val);
162 __atomic_fetch_or(log_base + elem->offset, elem->val,
167 rte_atomic_thread_fence(__ATOMIC_RELEASE);
169 vq->log_cache_nb_elem = 0;
172 static __rte_always_inline void
173 vhost_log_cache_page(struct virtio_net *dev, struct vhost_virtqueue *vq,
176 uint32_t bit_nr = page % (sizeof(unsigned long) << 3);
177 uint32_t offset = page / (sizeof(unsigned long) << 3);
180 for (i = 0; i < vq->log_cache_nb_elem; i++) {
181 struct log_cache_entry *elem = vq->log_cache + i;
183 if (elem->offset == offset) {
184 elem->val |= (1UL << bit_nr);
189 if (unlikely(i >= VHOST_LOG_CACHE_NR)) {
191 * No more room for a new log cache entry,
192 * so write the dirty log map directly.
194 rte_atomic_thread_fence(__ATOMIC_RELEASE);
195 vhost_log_page((uint8_t *)(uintptr_t)dev->log_base, page);
200 vq->log_cache[i].offset = offset;
201 vq->log_cache[i].val = (1UL << bit_nr);
202 vq->log_cache_nb_elem++;
206 __vhost_log_cache_write(struct virtio_net *dev, struct vhost_virtqueue *vq,
207 uint64_t addr, uint64_t len)
211 if (unlikely(!dev->log_base || !len))
214 if (unlikely(dev->log_size <= ((addr + len - 1) / VHOST_LOG_PAGE / 8)))
217 page = addr / VHOST_LOG_PAGE;
218 while (page * VHOST_LOG_PAGE < addr + len) {
219 vhost_log_cache_page(dev, vq, page);
225 __vhost_log_cache_write_iova(struct virtio_net *dev, struct vhost_virtqueue *vq,
226 uint64_t iova, uint64_t len)
228 uint64_t hva, gpa, map_len;
231 hva = __vhost_iova_to_vva(dev, vq, iova, &map_len, VHOST_ACCESS_RW);
232 if (map_len != len) {
234 "Failed to write log for IOVA 0x%" PRIx64 ". No IOTLB entry found\n",
239 gpa = hva_to_gpa(dev, hva, len);
241 __vhost_log_cache_write(dev, vq, gpa, len);
245 vhost_alloc_copy_ind_table(struct virtio_net *dev, struct vhost_virtqueue *vq,
246 uint64_t desc_addr, uint64_t desc_len)
250 uint64_t len, remain = desc_len;
252 idesc = rte_malloc(__func__, desc_len, 0);
253 if (unlikely(!idesc))
256 dst = (uint64_t)(uintptr_t)idesc;
260 src = vhost_iova_to_vva(dev, vq, desc_addr, &len,
262 if (unlikely(!src || !len)) {
267 rte_memcpy((void *)(uintptr_t)dst, (void *)(uintptr_t)src, len);
278 cleanup_vq(struct vhost_virtqueue *vq, int destroy)
280 if ((vq->callfd >= 0) && (destroy != 0))
287 cleanup_vq_inflight(struct virtio_net *dev, struct vhost_virtqueue *vq)
289 if (!(dev->protocol_features &
290 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD)))
293 if (vq_is_packed(dev)) {
294 if (vq->inflight_packed)
295 vq->inflight_packed = NULL;
297 if (vq->inflight_split)
298 vq->inflight_split = NULL;
301 if (vq->resubmit_inflight) {
302 if (vq->resubmit_inflight->resubmit_list) {
303 free(vq->resubmit_inflight->resubmit_list);
304 vq->resubmit_inflight->resubmit_list = NULL;
306 free(vq->resubmit_inflight);
307 vq->resubmit_inflight = NULL;
312 * Unmap any memory, close any file descriptors and
313 * free any memory owned by a device.
316 cleanup_device(struct virtio_net *dev, int destroy)
320 vhost_backend_cleanup(dev);
322 for (i = 0; i < dev->nr_vring; i++) {
323 cleanup_vq(dev->virtqueue[i], destroy);
324 cleanup_vq_inflight(dev, dev->virtqueue[i]);
329 vhost_free_async_mem(struct vhost_virtqueue *vq)
331 if (vq->async_pkts_info)
332 rte_free(vq->async_pkts_info);
333 if (vq->async_descs_split)
334 rte_free(vq->async_descs_split);
336 rte_free(vq->it_pool);
338 rte_free(vq->vec_pool);
340 vq->async_pkts_info = NULL;
341 vq->async_descs_split = NULL;
347 free_vq(struct virtio_net *dev, struct vhost_virtqueue *vq)
349 if (vq_is_packed(dev))
350 rte_free(vq->shadow_used_packed);
352 rte_free(vq->shadow_used_split);
353 vhost_free_async_mem(vq);
355 rte_free(vq->batch_copy_elems);
356 rte_mempool_free(vq->iotlb_pool);
361 * Release virtqueues and device memory.
364 free_device(struct virtio_net *dev)
368 for (i = 0; i < dev->nr_vring; i++)
369 free_vq(dev, dev->virtqueue[i]);
374 static __rte_always_inline int
375 log_translate(struct virtio_net *dev, struct vhost_virtqueue *vq)
377 if (likely(!(vq->ring_addrs.flags & (1 << VHOST_VRING_F_LOG))))
380 vq->log_guest_addr = translate_log_addr(dev, vq,
381 vq->ring_addrs.log_guest_addr);
382 if (vq->log_guest_addr == 0)
389 * Converts vring log address to GPA
390 * If IOMMU is enabled, the log address is IOVA
391 * If IOMMU not enabled, the log address is already GPA
393 * Caller should have iotlb_lock read-locked
396 translate_log_addr(struct virtio_net *dev, struct vhost_virtqueue *vq,
399 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)) {
400 const uint64_t exp_size = sizeof(uint64_t);
402 uint64_t size = exp_size;
404 hva = vhost_iova_to_vva(dev, vq, log_addr,
405 &size, VHOST_ACCESS_RW);
407 if (size != exp_size)
410 gpa = hva_to_gpa(dev, hva, exp_size);
412 VHOST_LOG_CONFIG(ERR,
413 "VQ: Failed to find GPA for log_addr: 0x%"
414 PRIx64 " hva: 0x%" PRIx64 "\n",
424 /* Caller should have iotlb_lock read-locked */
426 vring_translate_split(struct virtio_net *dev, struct vhost_virtqueue *vq)
428 uint64_t req_size, size;
430 req_size = sizeof(struct vring_desc) * vq->size;
432 vq->desc = (struct vring_desc *)(uintptr_t)vhost_iova_to_vva(dev, vq,
433 vq->ring_addrs.desc_user_addr,
434 &size, VHOST_ACCESS_RW);
435 if (!vq->desc || size != req_size)
438 req_size = sizeof(struct vring_avail);
439 req_size += sizeof(uint16_t) * vq->size;
440 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))
441 req_size += sizeof(uint16_t);
443 vq->avail = (struct vring_avail *)(uintptr_t)vhost_iova_to_vva(dev, vq,
444 vq->ring_addrs.avail_user_addr,
445 &size, VHOST_ACCESS_RW);
446 if (!vq->avail || size != req_size)
449 req_size = sizeof(struct vring_used);
450 req_size += sizeof(struct vring_used_elem) * vq->size;
451 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))
452 req_size += sizeof(uint16_t);
454 vq->used = (struct vring_used *)(uintptr_t)vhost_iova_to_vva(dev, vq,
455 vq->ring_addrs.used_user_addr,
456 &size, VHOST_ACCESS_RW);
457 if (!vq->used || size != req_size)
463 /* Caller should have iotlb_lock read-locked */
465 vring_translate_packed(struct virtio_net *dev, struct vhost_virtqueue *vq)
467 uint64_t req_size, size;
469 req_size = sizeof(struct vring_packed_desc) * vq->size;
471 vq->desc_packed = (struct vring_packed_desc *)(uintptr_t)
472 vhost_iova_to_vva(dev, vq, vq->ring_addrs.desc_user_addr,
473 &size, VHOST_ACCESS_RW);
474 if (!vq->desc_packed || size != req_size)
477 req_size = sizeof(struct vring_packed_desc_event);
479 vq->driver_event = (struct vring_packed_desc_event *)(uintptr_t)
480 vhost_iova_to_vva(dev, vq, vq->ring_addrs.avail_user_addr,
481 &size, VHOST_ACCESS_RW);
482 if (!vq->driver_event || size != req_size)
485 req_size = sizeof(struct vring_packed_desc_event);
487 vq->device_event = (struct vring_packed_desc_event *)(uintptr_t)
488 vhost_iova_to_vva(dev, vq, vq->ring_addrs.used_user_addr,
489 &size, VHOST_ACCESS_RW);
490 if (!vq->device_event || size != req_size)
497 vring_translate(struct virtio_net *dev, struct vhost_virtqueue *vq)
500 if (!(dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)))
503 if (vq_is_packed(dev)) {
504 if (vring_translate_packed(dev, vq) < 0)
507 if (vring_translate_split(dev, vq) < 0)
511 if (log_translate(dev, vq) < 0)
520 vring_invalidate(struct virtio_net *dev, struct vhost_virtqueue *vq)
522 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
523 vhost_user_iotlb_wr_lock(vq);
529 vq->log_guest_addr = 0;
531 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
532 vhost_user_iotlb_wr_unlock(vq);
536 init_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
538 struct vhost_virtqueue *vq;
540 if (vring_idx >= VHOST_MAX_VRING) {
541 VHOST_LOG_CONFIG(ERR,
542 "Failed not init vring, out of bound (%d)\n",
547 vq = dev->virtqueue[vring_idx];
549 VHOST_LOG_CONFIG(ERR, "Virtqueue not allocated (%d)\n",
554 memset(vq, 0, sizeof(struct vhost_virtqueue));
556 vq->kickfd = VIRTIO_UNINITIALIZED_EVENTFD;
557 vq->callfd = VIRTIO_UNINITIALIZED_EVENTFD;
558 vq->notif_enable = VIRTIO_UNINITIALIZED_NOTIF;
560 vhost_user_iotlb_init(dev, vring_idx);
561 /* Backends are set to -1 indicating an inactive device. */
566 reset_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
568 struct vhost_virtqueue *vq;
571 if (vring_idx >= VHOST_MAX_VRING) {
572 VHOST_LOG_CONFIG(ERR,
573 "Failed not init vring, out of bound (%d)\n",
578 vq = dev->virtqueue[vring_idx];
580 VHOST_LOG_CONFIG(ERR, "Virtqueue not allocated (%d)\n",
586 init_vring_queue(dev, vring_idx);
591 alloc_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
593 struct vhost_virtqueue *vq;
596 /* Also allocate holes, if any, up to requested vring index. */
597 for (i = 0; i <= vring_idx; i++) {
598 if (dev->virtqueue[i])
601 vq = rte_malloc(NULL, sizeof(struct vhost_virtqueue), 0);
603 VHOST_LOG_CONFIG(ERR,
604 "Failed to allocate memory for vring:%u.\n", i);
608 dev->virtqueue[i] = vq;
609 init_vring_queue(dev, i);
610 rte_spinlock_init(&vq->access_lock);
611 vq->avail_wrap_counter = 1;
612 vq->used_wrap_counter = 1;
613 vq->signalled_used_valid = false;
616 dev->nr_vring = RTE_MAX(dev->nr_vring, vring_idx + 1);
622 * Reset some variables in device structure, while keeping few
623 * others untouched, such as vid, ifname, nr_vring: they
624 * should be same unless the device is removed.
627 reset_device(struct virtio_net *dev)
632 dev->protocol_features = 0;
633 dev->flags &= VIRTIO_DEV_BUILTIN_VIRTIO_NET;
635 for (i = 0; i < dev->nr_vring; i++)
636 reset_vring_queue(dev, i);
640 * Invoked when there is a new vhost-user connection established (when
641 * there is a new virtio device being attached).
644 vhost_new_device(void)
646 struct virtio_net *dev;
649 pthread_mutex_lock(&vhost_dev_lock);
650 for (i = 0; i < MAX_VHOST_DEVICE; i++) {
651 if (vhost_devices[i] == NULL)
655 if (i == MAX_VHOST_DEVICE) {
656 VHOST_LOG_CONFIG(ERR,
657 "Failed to find a free slot for new device.\n");
658 pthread_mutex_unlock(&vhost_dev_lock);
662 dev = rte_zmalloc(NULL, sizeof(struct virtio_net), 0);
664 VHOST_LOG_CONFIG(ERR,
665 "Failed to allocate memory for new dev.\n");
666 pthread_mutex_unlock(&vhost_dev_lock);
670 vhost_devices[i] = dev;
671 pthread_mutex_unlock(&vhost_dev_lock);
674 dev->flags = VIRTIO_DEV_BUILTIN_VIRTIO_NET;
675 dev->slave_req_fd = -1;
676 dev->postcopy_ufd = -1;
677 rte_spinlock_init(&dev->slave_req_lock);
683 vhost_destroy_device_notify(struct virtio_net *dev)
685 struct rte_vdpa_device *vdpa_dev;
687 if (dev->flags & VIRTIO_DEV_RUNNING) {
688 vdpa_dev = dev->vdpa_dev;
690 vdpa_dev->ops->dev_close(dev->vid);
691 dev->flags &= ~VIRTIO_DEV_RUNNING;
692 dev->notify_ops->destroy_device(dev->vid);
697 * Invoked when there is the vhost-user connection is broken (when
698 * the virtio device is being detached).
701 vhost_destroy_device(int vid)
703 struct virtio_net *dev = get_device(vid);
708 vhost_destroy_device_notify(dev);
710 cleanup_device(dev, 1);
713 vhost_devices[vid] = NULL;
717 vhost_attach_vdpa_device(int vid, struct rte_vdpa_device *vdpa_dev)
719 struct virtio_net *dev = get_device(vid);
724 dev->vdpa_dev = vdpa_dev;
728 vhost_set_ifname(int vid, const char *if_name, unsigned int if_len)
730 struct virtio_net *dev;
733 dev = get_device(vid);
737 len = if_len > sizeof(dev->ifname) ?
738 sizeof(dev->ifname) : if_len;
740 strncpy(dev->ifname, if_name, len);
741 dev->ifname[sizeof(dev->ifname) - 1] = '\0';
745 vhost_set_builtin_virtio_net(int vid, bool enable)
747 struct virtio_net *dev = get_device(vid);
753 dev->flags |= VIRTIO_DEV_BUILTIN_VIRTIO_NET;
755 dev->flags &= ~VIRTIO_DEV_BUILTIN_VIRTIO_NET;
759 vhost_enable_extbuf(int vid)
761 struct virtio_net *dev = get_device(vid);
770 vhost_enable_linearbuf(int vid)
772 struct virtio_net *dev = get_device(vid);
781 rte_vhost_get_mtu(int vid, uint16_t *mtu)
783 struct virtio_net *dev = get_device(vid);
785 if (dev == NULL || mtu == NULL)
788 if (!(dev->flags & VIRTIO_DEV_READY))
791 if (!(dev->features & (1ULL << VIRTIO_NET_F_MTU)))
800 rte_vhost_get_numa_node(int vid)
802 #ifdef RTE_LIBRTE_VHOST_NUMA
803 struct virtio_net *dev = get_device(vid);
807 if (dev == NULL || numa_available() != 0)
810 ret = get_mempolicy(&numa_node, NULL, 0, dev,
811 MPOL_F_NODE | MPOL_F_ADDR);
813 VHOST_LOG_CONFIG(ERR,
814 "(%d) failed to query numa node: %s\n",
815 vid, rte_strerror(errno));
827 rte_vhost_get_queue_num(int vid)
829 struct virtio_net *dev = get_device(vid);
834 return dev->nr_vring / 2;
838 rte_vhost_get_vring_num(int vid)
840 struct virtio_net *dev = get_device(vid);
845 return dev->nr_vring;
849 rte_vhost_get_ifname(int vid, char *buf, size_t len)
851 struct virtio_net *dev = get_device(vid);
853 if (dev == NULL || buf == NULL)
856 len = RTE_MIN(len, sizeof(dev->ifname));
858 strncpy(buf, dev->ifname, len);
865 rte_vhost_get_negotiated_features(int vid, uint64_t *features)
867 struct virtio_net *dev;
869 dev = get_device(vid);
870 if (dev == NULL || features == NULL)
873 *features = dev->features;
878 rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem)
880 struct virtio_net *dev;
881 struct rte_vhost_memory *m;
884 dev = get_device(vid);
885 if (dev == NULL || mem == NULL)
888 size = dev->mem->nregions * sizeof(struct rte_vhost_mem_region);
889 m = malloc(sizeof(struct rte_vhost_memory) + size);
893 m->nregions = dev->mem->nregions;
894 memcpy(m->regions, dev->mem->regions, size);
901 rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
902 struct rte_vhost_vring *vring)
904 struct virtio_net *dev;
905 struct vhost_virtqueue *vq;
907 dev = get_device(vid);
908 if (dev == NULL || vring == NULL)
911 if (vring_idx >= VHOST_MAX_VRING)
914 vq = dev->virtqueue[vring_idx];
918 if (vq_is_packed(dev)) {
919 vring->desc_packed = vq->desc_packed;
920 vring->driver_event = vq->driver_event;
921 vring->device_event = vq->device_event;
923 vring->desc = vq->desc;
924 vring->avail = vq->avail;
925 vring->used = vq->used;
927 vring->log_guest_addr = vq->log_guest_addr;
929 vring->callfd = vq->callfd;
930 vring->kickfd = vq->kickfd;
931 vring->size = vq->size;
937 rte_vhost_get_vhost_ring_inflight(int vid, uint16_t vring_idx,
938 struct rte_vhost_ring_inflight *vring)
940 struct virtio_net *dev;
941 struct vhost_virtqueue *vq;
943 dev = get_device(vid);
947 if (vring_idx >= VHOST_MAX_VRING)
950 vq = dev->virtqueue[vring_idx];
954 if (vq_is_packed(dev)) {
955 if (unlikely(!vq->inflight_packed))
958 vring->inflight_packed = vq->inflight_packed;
960 if (unlikely(!vq->inflight_split))
963 vring->inflight_split = vq->inflight_split;
966 vring->resubmit_inflight = vq->resubmit_inflight;
972 rte_vhost_set_inflight_desc_split(int vid, uint16_t vring_idx,
975 struct vhost_virtqueue *vq;
976 struct virtio_net *dev;
978 dev = get_device(vid);
982 if (unlikely(!(dev->protocol_features &
983 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
986 if (unlikely(vq_is_packed(dev)))
989 if (unlikely(vring_idx >= VHOST_MAX_VRING))
992 vq = dev->virtqueue[vring_idx];
996 if (unlikely(!vq->inflight_split))
999 if (unlikely(idx >= vq->size))
1002 vq->inflight_split->desc[idx].counter = vq->global_counter++;
1003 vq->inflight_split->desc[idx].inflight = 1;
1008 rte_vhost_set_inflight_desc_packed(int vid, uint16_t vring_idx,
1009 uint16_t head, uint16_t last,
1010 uint16_t *inflight_entry)
1012 struct rte_vhost_inflight_info_packed *inflight_info;
1013 struct virtio_net *dev;
1014 struct vhost_virtqueue *vq;
1015 struct vring_packed_desc *desc;
1016 uint16_t old_free_head, free_head;
1018 dev = get_device(vid);
1022 if (unlikely(!(dev->protocol_features &
1023 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1026 if (unlikely(!vq_is_packed(dev)))
1029 if (unlikely(vring_idx >= VHOST_MAX_VRING))
1032 vq = dev->virtqueue[vring_idx];
1036 inflight_info = vq->inflight_packed;
1037 if (unlikely(!inflight_info))
1040 if (unlikely(head >= vq->size))
1043 desc = vq->desc_packed;
1044 old_free_head = inflight_info->old_free_head;
1045 if (unlikely(old_free_head >= vq->size))
1048 free_head = old_free_head;
1050 /* init header descriptor */
1051 inflight_info->desc[old_free_head].num = 0;
1052 inflight_info->desc[old_free_head].counter = vq->global_counter++;
1053 inflight_info->desc[old_free_head].inflight = 1;
1055 /* save desc entry in flight entry */
1056 while (head != ((last + 1) % vq->size)) {
1057 inflight_info->desc[old_free_head].num++;
1058 inflight_info->desc[free_head].addr = desc[head].addr;
1059 inflight_info->desc[free_head].len = desc[head].len;
1060 inflight_info->desc[free_head].flags = desc[head].flags;
1061 inflight_info->desc[free_head].id = desc[head].id;
1063 inflight_info->desc[old_free_head].last = free_head;
1064 free_head = inflight_info->desc[free_head].next;
1065 inflight_info->free_head = free_head;
1066 head = (head + 1) % vq->size;
1069 inflight_info->old_free_head = free_head;
1070 *inflight_entry = old_free_head;
1076 rte_vhost_clr_inflight_desc_split(int vid, uint16_t vring_idx,
1077 uint16_t last_used_idx, uint16_t idx)
1079 struct virtio_net *dev;
1080 struct vhost_virtqueue *vq;
1082 dev = get_device(vid);
1086 if (unlikely(!(dev->protocol_features &
1087 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1090 if (unlikely(vq_is_packed(dev)))
1093 if (unlikely(vring_idx >= VHOST_MAX_VRING))
1096 vq = dev->virtqueue[vring_idx];
1100 if (unlikely(!vq->inflight_split))
1103 if (unlikely(idx >= vq->size))
1106 rte_atomic_thread_fence(__ATOMIC_SEQ_CST);
1108 vq->inflight_split->desc[idx].inflight = 0;
1110 rte_atomic_thread_fence(__ATOMIC_SEQ_CST);
1112 vq->inflight_split->used_idx = last_used_idx;
1117 rte_vhost_clr_inflight_desc_packed(int vid, uint16_t vring_idx,
1120 struct rte_vhost_inflight_info_packed *inflight_info;
1121 struct virtio_net *dev;
1122 struct vhost_virtqueue *vq;
1124 dev = get_device(vid);
1128 if (unlikely(!(dev->protocol_features &
1129 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1132 if (unlikely(!vq_is_packed(dev)))
1135 if (unlikely(vring_idx >= VHOST_MAX_VRING))
1138 vq = dev->virtqueue[vring_idx];
1142 inflight_info = vq->inflight_packed;
1143 if (unlikely(!inflight_info))
1146 if (unlikely(head >= vq->size))
1149 rte_atomic_thread_fence(__ATOMIC_SEQ_CST);
1151 inflight_info->desc[head].inflight = 0;
1153 rte_atomic_thread_fence(__ATOMIC_SEQ_CST);
1155 inflight_info->old_free_head = inflight_info->free_head;
1156 inflight_info->old_used_idx = inflight_info->used_idx;
1157 inflight_info->old_used_wrap_counter = inflight_info->used_wrap_counter;
1163 rte_vhost_set_last_inflight_io_split(int vid, uint16_t vring_idx,
1166 struct virtio_net *dev;
1167 struct vhost_virtqueue *vq;
1169 dev = get_device(vid);
1173 if (unlikely(!(dev->protocol_features &
1174 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1177 if (unlikely(vq_is_packed(dev)))
1180 if (unlikely(vring_idx >= VHOST_MAX_VRING))
1183 vq = dev->virtqueue[vring_idx];
1187 if (unlikely(!vq->inflight_split))
1190 vq->inflight_split->last_inflight_io = idx;
1195 rte_vhost_set_last_inflight_io_packed(int vid, uint16_t vring_idx,
1198 struct rte_vhost_inflight_info_packed *inflight_info;
1199 struct virtio_net *dev;
1200 struct vhost_virtqueue *vq;
1203 dev = get_device(vid);
1207 if (unlikely(!(dev->protocol_features &
1208 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1211 if (unlikely(!vq_is_packed(dev)))
1214 if (unlikely(vring_idx >= VHOST_MAX_VRING))
1217 vq = dev->virtqueue[vring_idx];
1221 inflight_info = vq->inflight_packed;
1222 if (unlikely(!inflight_info))
1225 if (unlikely(head >= vq->size))
1228 last = inflight_info->desc[head].last;
1229 if (unlikely(last >= vq->size))
1232 inflight_info->desc[last].next = inflight_info->free_head;
1233 inflight_info->free_head = head;
1234 inflight_info->used_idx += inflight_info->desc[head].num;
1235 if (inflight_info->used_idx >= inflight_info->desc_num) {
1236 inflight_info->used_idx -= inflight_info->desc_num;
1237 inflight_info->used_wrap_counter =
1238 !inflight_info->used_wrap_counter;
1245 rte_vhost_vring_call(int vid, uint16_t vring_idx)
1247 struct virtio_net *dev;
1248 struct vhost_virtqueue *vq;
1250 dev = get_device(vid);
1254 if (vring_idx >= VHOST_MAX_VRING)
1257 vq = dev->virtqueue[vring_idx];
1261 if (vq_is_packed(dev))
1262 vhost_vring_call_packed(dev, vq);
1264 vhost_vring_call_split(dev, vq);
1270 rte_vhost_avail_entries(int vid, uint16_t queue_id)
1272 struct virtio_net *dev;
1273 struct vhost_virtqueue *vq;
1276 dev = get_device(vid);
1280 if (queue_id >= VHOST_MAX_VRING)
1283 vq = dev->virtqueue[queue_id];
1287 rte_spinlock_lock(&vq->access_lock);
1289 if (unlikely(!vq->enabled || vq->avail == NULL))
1292 ret = *(volatile uint16_t *)&vq->avail->idx - vq->last_used_idx;
1295 rte_spinlock_unlock(&vq->access_lock);
1300 vhost_enable_notify_split(struct virtio_net *dev,
1301 struct vhost_virtqueue *vq, int enable)
1303 if (vq->used == NULL)
1306 if (!(dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))) {
1308 vq->used->flags &= ~VRING_USED_F_NO_NOTIFY;
1310 vq->used->flags |= VRING_USED_F_NO_NOTIFY;
1313 vhost_avail_event(vq) = vq->last_avail_idx;
1319 vhost_enable_notify_packed(struct virtio_net *dev,
1320 struct vhost_virtqueue *vq, int enable)
1324 if (vq->device_event == NULL)
1328 vq->device_event->flags = VRING_EVENT_F_DISABLE;
1332 flags = VRING_EVENT_F_ENABLE;
1333 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX)) {
1334 flags = VRING_EVENT_F_DESC;
1335 vq->device_event->off_wrap = vq->last_avail_idx |
1336 vq->avail_wrap_counter << 15;
1339 rte_atomic_thread_fence(__ATOMIC_RELEASE);
1341 vq->device_event->flags = flags;
1346 vhost_enable_guest_notification(struct virtio_net *dev,
1347 struct vhost_virtqueue *vq, int enable)
1350 * If the virtqueue is not ready yet, it will be applied
1351 * when it will become ready.
1356 if (vq_is_packed(dev))
1357 return vhost_enable_notify_packed(dev, vq, enable);
1359 return vhost_enable_notify_split(dev, vq, enable);
1363 rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable)
1365 struct virtio_net *dev = get_device(vid);
1366 struct vhost_virtqueue *vq;
1372 if (queue_id >= VHOST_MAX_VRING)
1375 vq = dev->virtqueue[queue_id];
1379 rte_spinlock_lock(&vq->access_lock);
1381 vq->notif_enable = enable;
1382 ret = vhost_enable_guest_notification(dev, vq, enable);
1384 rte_spinlock_unlock(&vq->access_lock);
1390 rte_vhost_log_write(int vid, uint64_t addr, uint64_t len)
1392 struct virtio_net *dev = get_device(vid);
1397 vhost_log_write(dev, addr, len);
1401 rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
1402 uint64_t offset, uint64_t len)
1404 struct virtio_net *dev;
1405 struct vhost_virtqueue *vq;
1407 dev = get_device(vid);
1411 if (vring_idx >= VHOST_MAX_VRING)
1413 vq = dev->virtqueue[vring_idx];
1417 vhost_log_used_vring(dev, vq, offset, len);
1421 rte_vhost_rx_queue_count(int vid, uint16_t qid)
1423 struct virtio_net *dev;
1424 struct vhost_virtqueue *vq;
1427 dev = get_device(vid);
1431 if (unlikely(qid >= dev->nr_vring || (qid & 1) == 0)) {
1432 VHOST_LOG_DATA(ERR, "(%d) %s: invalid virtqueue idx %d.\n",
1433 dev->vid, __func__, qid);
1437 vq = dev->virtqueue[qid];
1441 rte_spinlock_lock(&vq->access_lock);
1443 if (unlikely(vq->enabled == 0 || vq->avail == NULL))
1446 ret = *((volatile uint16_t *)&vq->avail->idx) - vq->last_avail_idx;
1449 rte_spinlock_unlock(&vq->access_lock);
1453 struct rte_vdpa_device *
1454 rte_vhost_get_vdpa_device(int vid)
1456 struct virtio_net *dev = get_device(vid);
1461 return dev->vdpa_dev;
1464 int rte_vhost_get_log_base(int vid, uint64_t *log_base,
1467 struct virtio_net *dev = get_device(vid);
1469 if (dev == NULL || log_base == NULL || log_size == NULL)
1472 *log_base = dev->log_base;
1473 *log_size = dev->log_size;
1478 int rte_vhost_get_vring_base(int vid, uint16_t queue_id,
1479 uint16_t *last_avail_idx, uint16_t *last_used_idx)
1481 struct vhost_virtqueue *vq;
1482 struct virtio_net *dev = get_device(vid);
1484 if (dev == NULL || last_avail_idx == NULL || last_used_idx == NULL)
1487 if (queue_id >= VHOST_MAX_VRING)
1490 vq = dev->virtqueue[queue_id];
1494 if (vq_is_packed(dev)) {
1495 *last_avail_idx = (vq->avail_wrap_counter << 15) |
1497 *last_used_idx = (vq->used_wrap_counter << 15) |
1500 *last_avail_idx = vq->last_avail_idx;
1501 *last_used_idx = vq->last_used_idx;
1507 int rte_vhost_set_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);
1516 if (queue_id >= VHOST_MAX_VRING)
1519 vq = dev->virtqueue[queue_id];
1523 if (vq_is_packed(dev)) {
1524 vq->last_avail_idx = last_avail_idx & 0x7fff;
1525 vq->avail_wrap_counter = !!(last_avail_idx & (1 << 15));
1526 vq->last_used_idx = last_used_idx & 0x7fff;
1527 vq->used_wrap_counter = !!(last_used_idx & (1 << 15));
1529 vq->last_avail_idx = last_avail_idx;
1530 vq->last_used_idx = last_used_idx;
1537 rte_vhost_get_vring_base_from_inflight(int vid,
1539 uint16_t *last_avail_idx,
1540 uint16_t *last_used_idx)
1542 struct rte_vhost_inflight_info_packed *inflight_info;
1543 struct vhost_virtqueue *vq;
1544 struct virtio_net *dev = get_device(vid);
1546 if (dev == NULL || last_avail_idx == NULL || last_used_idx == NULL)
1549 if (queue_id >= VHOST_MAX_VRING)
1552 vq = dev->virtqueue[queue_id];
1556 if (!vq_is_packed(dev))
1559 inflight_info = vq->inflight_packed;
1563 *last_avail_idx = (inflight_info->old_used_wrap_counter << 15) |
1564 inflight_info->old_used_idx;
1565 *last_used_idx = *last_avail_idx;
1570 int rte_vhost_extern_callback_register(int vid,
1571 struct rte_vhost_user_extern_ops const * const ops, void *ctx)
1573 struct virtio_net *dev = get_device(vid);
1575 if (dev == NULL || ops == NULL)
1578 dev->extern_ops = *ops;
1579 dev->extern_data = ctx;
1583 int rte_vhost_async_channel_register(int vid, uint16_t queue_id,
1585 struct rte_vhost_async_channel_ops *ops)
1587 struct vhost_virtqueue *vq;
1588 struct virtio_net *dev = get_device(vid);
1589 struct rte_vhost_async_features f;
1592 if (dev == NULL || ops == NULL)
1595 f.intval = features;
1597 if (queue_id >= VHOST_MAX_VRING)
1600 vq = dev->virtqueue[queue_id];
1602 if (unlikely(vq == NULL || !dev->async_copy))
1605 /* packed queue is not supported */
1606 if (unlikely(vq_is_packed(dev) || !f.async_inorder)) {
1607 VHOST_LOG_CONFIG(ERR,
1608 "async copy is not supported on packed queue or non-inorder mode "
1609 "(vid %d, qid: %d)\n", vid, queue_id);
1613 if (unlikely(ops->check_completed_copies == NULL ||
1614 ops->transfer_data == NULL))
1617 rte_spinlock_lock(&vq->access_lock);
1619 if (unlikely(vq->async_registered)) {
1620 VHOST_LOG_CONFIG(ERR,
1621 "async register failed: channel already registered "
1622 "(vid %d, qid: %d)\n", vid, queue_id);
1626 #ifdef RTE_LIBRTE_VHOST_NUMA
1627 if (get_mempolicy(&node, NULL, 0, vq, MPOL_F_NODE | MPOL_F_ADDR)) {
1628 VHOST_LOG_CONFIG(ERR,
1629 "unable to get numa information in async register. "
1630 "allocating async buffer memory on the caller thread node\n");
1631 node = SOCKET_ID_ANY;
1634 node = SOCKET_ID_ANY;
1637 vq->async_pkts_info = rte_malloc_socket(NULL,
1638 vq->size * sizeof(struct async_inflight_info),
1639 RTE_CACHE_LINE_SIZE, node);
1640 vq->it_pool = rte_malloc_socket(NULL,
1641 VHOST_MAX_ASYNC_IT * sizeof(struct rte_vhost_iov_iter),
1642 RTE_CACHE_LINE_SIZE, node);
1643 vq->vec_pool = rte_malloc_socket(NULL,
1644 VHOST_MAX_ASYNC_VEC * sizeof(struct iovec),
1645 RTE_CACHE_LINE_SIZE, node);
1646 vq->async_descs_split = rte_malloc_socket(NULL,
1647 vq->size * sizeof(struct vring_used_elem),
1648 RTE_CACHE_LINE_SIZE, node);
1649 if (!vq->async_descs_split || !vq->async_pkts_info ||
1650 !vq->it_pool || !vq->vec_pool) {
1651 vhost_free_async_mem(vq);
1652 VHOST_LOG_CONFIG(ERR,
1653 "async register failed: cannot allocate memory for vq data "
1654 "(vid %d, qid: %d)\n", vid, queue_id);
1658 vq->async_ops.check_completed_copies = ops->check_completed_copies;
1659 vq->async_ops.transfer_data = ops->transfer_data;
1661 vq->async_inorder = f.async_inorder;
1662 vq->async_threshold = f.async_threshold;
1664 vq->async_registered = true;
1667 rte_spinlock_unlock(&vq->access_lock);
1672 int rte_vhost_async_channel_unregister(int vid, uint16_t queue_id)
1674 struct vhost_virtqueue *vq;
1675 struct virtio_net *dev = get_device(vid);
1681 if (queue_id >= VHOST_MAX_VRING)
1684 vq = dev->virtqueue[queue_id];
1691 if (!vq->async_registered)
1694 if (!rte_spinlock_trylock(&vq->access_lock)) {
1695 VHOST_LOG_CONFIG(ERR, "Failed to unregister async channel. "
1696 "virt queue busy.\n");
1700 if (vq->async_pkts_inflight_n) {
1701 VHOST_LOG_CONFIG(ERR, "Failed to unregister async channel. "
1702 "async inflight packets must be completed before unregistration.\n");
1707 vhost_free_async_mem(vq);
1709 vq->async_ops.transfer_data = NULL;
1710 vq->async_ops.check_completed_copies = NULL;
1711 vq->async_registered = false;
1714 rte_spinlock_unlock(&vq->access_lock);
1719 RTE_LOG_REGISTER(vhost_config_log_level, lib.vhost.config, INFO);
1720 RTE_LOG_REGISTER(vhost_data_log_level, lib.vhost.data, WARNING);