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];
30 /* Called with iotlb_lock read-locked */
32 __vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
33 uint64_t iova, uint64_t *size, uint8_t perm)
35 uint64_t vva, tmp_size;
42 vva = vhost_user_iotlb_cache_find(vq, iova, &tmp_size, perm);
43 if (tmp_size == *size)
48 if (!vhost_user_iotlb_pending_miss(vq, iova, perm)) {
50 * iotlb_lock is read-locked for a full burst,
51 * but it only protects the iotlb cache.
52 * In case of IOTLB miss, we might block on the socket,
53 * which could cause a deadlock with QEMU if an IOTLB update
54 * is being handled. We can safely unlock here to avoid it.
56 vhost_user_iotlb_rd_unlock(vq);
58 vhost_user_iotlb_pending_insert(vq, iova, perm);
59 if (vhost_user_iotlb_miss(dev, iova, perm)) {
61 "IOTLB miss req failed for IOVA 0x%" PRIx64 "\n",
63 vhost_user_iotlb_pending_remove(vq, iova, 1, perm);
66 vhost_user_iotlb_rd_lock(vq);
72 #define VHOST_LOG_PAGE 4096
75 * Atomically set a bit in memory.
77 static __rte_always_inline void
78 vhost_set_bit(unsigned int nr, volatile uint8_t *addr)
80 #if defined(RTE_TOOLCHAIN_GCC) && (GCC_VERSION < 70100)
82 * __sync_ built-ins are deprecated, but __atomic_ ones
83 * are sub-optimized in older GCC versions.
85 __sync_fetch_and_or_1(addr, (1U << nr));
87 __atomic_fetch_or(addr, (1U << nr), __ATOMIC_RELAXED);
91 static __rte_always_inline void
92 vhost_log_page(uint8_t *log_base, uint64_t page)
94 vhost_set_bit(page % 8, &log_base[page / 8]);
98 __vhost_log_write(struct virtio_net *dev, uint64_t addr, uint64_t len)
102 if (unlikely(!dev->log_base || !len))
105 if (unlikely(dev->log_size <= ((addr + len - 1) / VHOST_LOG_PAGE / 8)))
108 /* To make sure guest memory updates are committed before logging */
111 page = addr / VHOST_LOG_PAGE;
112 while (page * VHOST_LOG_PAGE < addr + len) {
113 vhost_log_page((uint8_t *)(uintptr_t)dev->log_base, page);
119 __vhost_log_write_iova(struct virtio_net *dev, struct vhost_virtqueue *vq,
120 uint64_t iova, uint64_t len)
122 uint64_t hva, gpa, map_len;
125 hva = __vhost_iova_to_vva(dev, vq, iova, &map_len, VHOST_ACCESS_RW);
126 if (map_len != len) {
128 "Failed to write log for IOVA 0x%" PRIx64 ". No IOTLB entry found\n",
133 gpa = hva_to_gpa(dev, hva, len);
135 __vhost_log_write(dev, gpa, len);
139 __vhost_log_cache_sync(struct virtio_net *dev, struct vhost_virtqueue *vq)
141 unsigned long *log_base;
144 if (unlikely(!dev->log_base))
149 log_base = (unsigned long *)(uintptr_t)dev->log_base;
151 for (i = 0; i < vq->log_cache_nb_elem; i++) {
152 struct log_cache_entry *elem = vq->log_cache + i;
154 #if defined(RTE_TOOLCHAIN_GCC) && (GCC_VERSION < 70100)
156 * '__sync' builtins are deprecated, but '__atomic' ones
157 * are sub-optimized in older GCC versions.
159 __sync_fetch_and_or(log_base + elem->offset, elem->val);
161 __atomic_fetch_or(log_base + elem->offset, elem->val,
168 vq->log_cache_nb_elem = 0;
171 static __rte_always_inline void
172 vhost_log_cache_page(struct virtio_net *dev, struct vhost_virtqueue *vq,
175 uint32_t bit_nr = page % (sizeof(unsigned long) << 3);
176 uint32_t offset = page / (sizeof(unsigned long) << 3);
179 for (i = 0; i < vq->log_cache_nb_elem; i++) {
180 struct log_cache_entry *elem = vq->log_cache + i;
182 if (elem->offset == offset) {
183 elem->val |= (1UL << bit_nr);
188 if (unlikely(i >= VHOST_LOG_CACHE_NR)) {
190 * No more room for a new log cache entry,
191 * so write the dirty log map directly.
194 vhost_log_page((uint8_t *)(uintptr_t)dev->log_base, page);
199 vq->log_cache[i].offset = offset;
200 vq->log_cache[i].val = (1UL << bit_nr);
201 vq->log_cache_nb_elem++;
205 __vhost_log_cache_write(struct virtio_net *dev, struct vhost_virtqueue *vq,
206 uint64_t addr, uint64_t len)
210 if (unlikely(!dev->log_base || !len))
213 if (unlikely(dev->log_size <= ((addr + len - 1) / VHOST_LOG_PAGE / 8)))
216 page = addr / VHOST_LOG_PAGE;
217 while (page * VHOST_LOG_PAGE < addr + len) {
218 vhost_log_cache_page(dev, vq, page);
224 __vhost_log_cache_write_iova(struct virtio_net *dev, struct vhost_virtqueue *vq,
225 uint64_t iova, uint64_t len)
227 uint64_t hva, gpa, map_len;
230 hva = __vhost_iova_to_vva(dev, vq, iova, &map_len, VHOST_ACCESS_RW);
231 if (map_len != len) {
233 "Failed to write log for IOVA 0x%" PRIx64 ". No IOTLB entry found\n",
238 gpa = hva_to_gpa(dev, hva, len);
240 __vhost_log_cache_write(dev, vq, gpa, len);
244 vhost_alloc_copy_ind_table(struct virtio_net *dev, struct vhost_virtqueue *vq,
245 uint64_t desc_addr, uint64_t desc_len)
249 uint64_t len, remain = desc_len;
251 idesc = rte_malloc(__func__, desc_len, 0);
252 if (unlikely(!idesc))
255 dst = (uint64_t)(uintptr_t)idesc;
259 src = vhost_iova_to_vva(dev, vq, desc_addr, &len,
261 if (unlikely(!src || !len)) {
266 rte_memcpy((void *)(uintptr_t)dst, (void *)(uintptr_t)src, len);
277 cleanup_vq(struct vhost_virtqueue *vq, int destroy)
279 if ((vq->callfd >= 0) && (destroy != 0))
286 cleanup_vq_inflight(struct virtio_net *dev, struct vhost_virtqueue *vq)
288 if (!(dev->protocol_features &
289 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD)))
292 if (vq_is_packed(dev)) {
293 if (vq->inflight_packed)
294 vq->inflight_packed = NULL;
296 if (vq->inflight_split)
297 vq->inflight_split = NULL;
300 if (vq->resubmit_inflight) {
301 if (vq->resubmit_inflight->resubmit_list) {
302 free(vq->resubmit_inflight->resubmit_list);
303 vq->resubmit_inflight->resubmit_list = NULL;
305 free(vq->resubmit_inflight);
306 vq->resubmit_inflight = NULL;
311 * Unmap any memory, close any file descriptors and
312 * free any memory owned by a device.
315 cleanup_device(struct virtio_net *dev, int destroy)
319 vhost_backend_cleanup(dev);
321 for (i = 0; i < dev->nr_vring; i++) {
322 cleanup_vq(dev->virtqueue[i], destroy);
323 cleanup_vq_inflight(dev, dev->virtqueue[i]);
328 free_vq(struct virtio_net *dev, struct vhost_virtqueue *vq)
330 if (vq_is_packed(dev))
331 rte_free(vq->shadow_used_packed);
333 rte_free(vq->shadow_used_split);
334 if (vq->async_pkts_pending)
335 rte_free(vq->async_pkts_pending);
336 if (vq->async_pkts_info)
337 rte_free(vq->async_pkts_info);
339 rte_free(vq->batch_copy_elems);
340 rte_mempool_free(vq->iotlb_pool);
345 * Release virtqueues and device memory.
348 free_device(struct virtio_net *dev)
352 for (i = 0; i < dev->nr_vring; i++)
353 free_vq(dev, dev->virtqueue[i]);
358 static __rte_always_inline int
359 log_translate(struct virtio_net *dev, struct vhost_virtqueue *vq)
361 if (likely(!(vq->ring_addrs.flags & (1 << VHOST_VRING_F_LOG))))
364 vq->log_guest_addr = translate_log_addr(dev, vq,
365 vq->ring_addrs.log_guest_addr);
366 if (vq->log_guest_addr == 0)
373 * Converts vring log address to GPA
374 * If IOMMU is enabled, the log address is IOVA
375 * If IOMMU not enabled, the log address is already GPA
377 * Caller should have iotlb_lock read-locked
380 translate_log_addr(struct virtio_net *dev, struct vhost_virtqueue *vq,
383 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)) {
384 const uint64_t exp_size = sizeof(uint64_t);
386 uint64_t size = exp_size;
388 hva = vhost_iova_to_vva(dev, vq, log_addr,
389 &size, VHOST_ACCESS_RW);
391 if (size != exp_size)
394 gpa = hva_to_gpa(dev, hva, exp_size);
396 VHOST_LOG_CONFIG(ERR,
397 "VQ: Failed to find GPA for log_addr: 0x%"
398 PRIx64 " hva: 0x%" PRIx64 "\n",
408 /* Caller should have iotlb_lock read-locked */
410 vring_translate_split(struct virtio_net *dev, struct vhost_virtqueue *vq)
412 uint64_t req_size, size;
414 req_size = sizeof(struct vring_desc) * vq->size;
416 vq->desc = (struct vring_desc *)(uintptr_t)vhost_iova_to_vva(dev, vq,
417 vq->ring_addrs.desc_user_addr,
418 &size, VHOST_ACCESS_RW);
419 if (!vq->desc || size != req_size)
422 req_size = sizeof(struct vring_avail);
423 req_size += sizeof(uint16_t) * vq->size;
424 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))
425 req_size += sizeof(uint16_t);
427 vq->avail = (struct vring_avail *)(uintptr_t)vhost_iova_to_vva(dev, vq,
428 vq->ring_addrs.avail_user_addr,
429 &size, VHOST_ACCESS_RW);
430 if (!vq->avail || size != req_size)
433 req_size = sizeof(struct vring_used);
434 req_size += sizeof(struct vring_used_elem) * vq->size;
435 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))
436 req_size += sizeof(uint16_t);
438 vq->used = (struct vring_used *)(uintptr_t)vhost_iova_to_vva(dev, vq,
439 vq->ring_addrs.used_user_addr,
440 &size, VHOST_ACCESS_RW);
441 if (!vq->used || size != req_size)
447 /* Caller should have iotlb_lock read-locked */
449 vring_translate_packed(struct virtio_net *dev, struct vhost_virtqueue *vq)
451 uint64_t req_size, size;
453 req_size = sizeof(struct vring_packed_desc) * vq->size;
455 vq->desc_packed = (struct vring_packed_desc *)(uintptr_t)
456 vhost_iova_to_vva(dev, vq, vq->ring_addrs.desc_user_addr,
457 &size, VHOST_ACCESS_RW);
458 if (!vq->desc_packed || size != req_size)
461 req_size = sizeof(struct vring_packed_desc_event);
463 vq->driver_event = (struct vring_packed_desc_event *)(uintptr_t)
464 vhost_iova_to_vva(dev, vq, vq->ring_addrs.avail_user_addr,
465 &size, VHOST_ACCESS_RW);
466 if (!vq->driver_event || size != req_size)
469 req_size = sizeof(struct vring_packed_desc_event);
471 vq->device_event = (struct vring_packed_desc_event *)(uintptr_t)
472 vhost_iova_to_vva(dev, vq, vq->ring_addrs.used_user_addr,
473 &size, VHOST_ACCESS_RW);
474 if (!vq->device_event || size != req_size)
481 vring_translate(struct virtio_net *dev, struct vhost_virtqueue *vq)
484 if (!(dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)))
487 if (vq_is_packed(dev)) {
488 if (vring_translate_packed(dev, vq) < 0)
491 if (vring_translate_split(dev, vq) < 0)
495 if (log_translate(dev, vq) < 0)
504 vring_invalidate(struct virtio_net *dev, struct vhost_virtqueue *vq)
506 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
507 vhost_user_iotlb_wr_lock(vq);
513 vq->log_guest_addr = 0;
515 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
516 vhost_user_iotlb_wr_unlock(vq);
520 init_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
522 struct vhost_virtqueue *vq;
524 if (vring_idx >= VHOST_MAX_VRING) {
525 VHOST_LOG_CONFIG(ERR,
526 "Failed not init vring, out of bound (%d)\n",
531 vq = dev->virtqueue[vring_idx];
533 memset(vq, 0, sizeof(struct vhost_virtqueue));
535 vq->kickfd = VIRTIO_UNINITIALIZED_EVENTFD;
536 vq->callfd = VIRTIO_UNINITIALIZED_EVENTFD;
537 vq->notif_enable = VIRTIO_UNINITIALIZED_NOTIF;
539 vhost_user_iotlb_init(dev, vring_idx);
540 /* Backends are set to -1 indicating an inactive device. */
545 reset_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
547 struct vhost_virtqueue *vq;
550 if (vring_idx >= VHOST_MAX_VRING) {
551 VHOST_LOG_CONFIG(ERR,
552 "Failed not init vring, out of bound (%d)\n",
557 vq = dev->virtqueue[vring_idx];
559 init_vring_queue(dev, vring_idx);
564 alloc_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
566 struct vhost_virtqueue *vq;
568 vq = rte_malloc(NULL, sizeof(struct vhost_virtqueue), 0);
570 VHOST_LOG_CONFIG(ERR,
571 "Failed to allocate memory for vring:%u.\n", vring_idx);
575 dev->virtqueue[vring_idx] = vq;
576 init_vring_queue(dev, vring_idx);
577 rte_spinlock_init(&vq->access_lock);
578 vq->avail_wrap_counter = 1;
579 vq->used_wrap_counter = 1;
580 vq->signalled_used_valid = false;
588 * Reset some variables in device structure, while keeping few
589 * others untouched, such as vid, ifname, nr_vring: they
590 * should be same unless the device is removed.
593 reset_device(struct virtio_net *dev)
598 dev->protocol_features = 0;
599 dev->flags &= VIRTIO_DEV_BUILTIN_VIRTIO_NET;
601 for (i = 0; i < dev->nr_vring; i++)
602 reset_vring_queue(dev, i);
606 * Invoked when there is a new vhost-user connection established (when
607 * there is a new virtio device being attached).
610 vhost_new_device(void)
612 struct virtio_net *dev;
615 for (i = 0; i < MAX_VHOST_DEVICE; i++) {
616 if (vhost_devices[i] == NULL)
620 if (i == MAX_VHOST_DEVICE) {
621 VHOST_LOG_CONFIG(ERR,
622 "Failed to find a free slot for new device.\n");
626 dev = rte_zmalloc(NULL, sizeof(struct virtio_net), 0);
628 VHOST_LOG_CONFIG(ERR,
629 "Failed to allocate memory for new dev.\n");
633 vhost_devices[i] = dev;
635 dev->flags = VIRTIO_DEV_BUILTIN_VIRTIO_NET;
636 dev->slave_req_fd = -1;
637 dev->postcopy_ufd = -1;
638 rte_spinlock_init(&dev->slave_req_lock);
644 vhost_destroy_device_notify(struct virtio_net *dev)
646 struct rte_vdpa_device *vdpa_dev;
648 if (dev->flags & VIRTIO_DEV_RUNNING) {
649 vdpa_dev = dev->vdpa_dev;
651 vdpa_dev->ops->dev_close(dev->vid);
652 dev->flags &= ~VIRTIO_DEV_RUNNING;
653 dev->notify_ops->destroy_device(dev->vid);
658 * Invoked when there is the vhost-user connection is broken (when
659 * the virtio device is being detached).
662 vhost_destroy_device(int vid)
664 struct virtio_net *dev = get_device(vid);
669 vhost_destroy_device_notify(dev);
671 cleanup_device(dev, 1);
674 vhost_devices[vid] = NULL;
678 vhost_attach_vdpa_device(int vid, struct rte_vdpa_device *vdpa_dev)
680 struct virtio_net *dev = get_device(vid);
685 dev->vdpa_dev = vdpa_dev;
689 vhost_set_ifname(int vid, const char *if_name, unsigned int if_len)
691 struct virtio_net *dev;
694 dev = get_device(vid);
698 len = if_len > sizeof(dev->ifname) ?
699 sizeof(dev->ifname) : if_len;
701 strncpy(dev->ifname, if_name, len);
702 dev->ifname[sizeof(dev->ifname) - 1] = '\0';
706 vhost_set_builtin_virtio_net(int vid, bool enable)
708 struct virtio_net *dev = get_device(vid);
714 dev->flags |= VIRTIO_DEV_BUILTIN_VIRTIO_NET;
716 dev->flags &= ~VIRTIO_DEV_BUILTIN_VIRTIO_NET;
720 vhost_enable_extbuf(int vid)
722 struct virtio_net *dev = get_device(vid);
731 vhost_enable_linearbuf(int vid)
733 struct virtio_net *dev = get_device(vid);
742 rte_vhost_get_mtu(int vid, uint16_t *mtu)
744 struct virtio_net *dev = get_device(vid);
746 if (dev == NULL || mtu == NULL)
749 if (!(dev->flags & VIRTIO_DEV_READY))
752 if (!(dev->features & (1ULL << VIRTIO_NET_F_MTU)))
761 rte_vhost_get_numa_node(int vid)
763 #ifdef RTE_LIBRTE_VHOST_NUMA
764 struct virtio_net *dev = get_device(vid);
768 if (dev == NULL || numa_available() != 0)
771 ret = get_mempolicy(&numa_node, NULL, 0, dev,
772 MPOL_F_NODE | MPOL_F_ADDR);
774 VHOST_LOG_CONFIG(ERR,
775 "(%d) failed to query numa node: %s\n",
776 vid, rte_strerror(errno));
788 rte_vhost_get_queue_num(int vid)
790 struct virtio_net *dev = get_device(vid);
795 return dev->nr_vring / 2;
799 rte_vhost_get_vring_num(int vid)
801 struct virtio_net *dev = get_device(vid);
806 return dev->nr_vring;
810 rte_vhost_get_ifname(int vid, char *buf, size_t len)
812 struct virtio_net *dev = get_device(vid);
814 if (dev == NULL || buf == NULL)
817 len = RTE_MIN(len, sizeof(dev->ifname));
819 strncpy(buf, dev->ifname, len);
826 rte_vhost_get_negotiated_features(int vid, uint64_t *features)
828 struct virtio_net *dev;
830 dev = get_device(vid);
831 if (dev == NULL || features == NULL)
834 *features = dev->features;
839 rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem)
841 struct virtio_net *dev;
842 struct rte_vhost_memory *m;
845 dev = get_device(vid);
846 if (dev == NULL || mem == NULL)
849 size = dev->mem->nregions * sizeof(struct rte_vhost_mem_region);
850 m = malloc(sizeof(struct rte_vhost_memory) + size);
854 m->nregions = dev->mem->nregions;
855 memcpy(m->regions, dev->mem->regions, size);
862 rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
863 struct rte_vhost_vring *vring)
865 struct virtio_net *dev;
866 struct vhost_virtqueue *vq;
868 dev = get_device(vid);
869 if (dev == NULL || vring == NULL)
872 if (vring_idx >= VHOST_MAX_VRING)
875 vq = dev->virtqueue[vring_idx];
879 if (vq_is_packed(dev)) {
880 vring->desc_packed = vq->desc_packed;
881 vring->driver_event = vq->driver_event;
882 vring->device_event = vq->device_event;
884 vring->desc = vq->desc;
885 vring->avail = vq->avail;
886 vring->used = vq->used;
888 vring->log_guest_addr = vq->log_guest_addr;
890 vring->callfd = vq->callfd;
891 vring->kickfd = vq->kickfd;
892 vring->size = vq->size;
898 rte_vhost_get_vhost_ring_inflight(int vid, uint16_t vring_idx,
899 struct rte_vhost_ring_inflight *vring)
901 struct virtio_net *dev;
902 struct vhost_virtqueue *vq;
904 dev = get_device(vid);
908 if (vring_idx >= VHOST_MAX_VRING)
911 vq = dev->virtqueue[vring_idx];
915 if (vq_is_packed(dev)) {
916 if (unlikely(!vq->inflight_packed))
919 vring->inflight_packed = vq->inflight_packed;
921 if (unlikely(!vq->inflight_split))
924 vring->inflight_split = vq->inflight_split;
927 vring->resubmit_inflight = vq->resubmit_inflight;
933 rte_vhost_set_inflight_desc_split(int vid, uint16_t vring_idx,
936 struct vhost_virtqueue *vq;
937 struct virtio_net *dev;
939 dev = get_device(vid);
943 if (unlikely(!(dev->protocol_features &
944 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
947 if (unlikely(vq_is_packed(dev)))
950 if (unlikely(vring_idx >= VHOST_MAX_VRING))
953 vq = dev->virtqueue[vring_idx];
957 if (unlikely(!vq->inflight_split))
960 if (unlikely(idx >= vq->size))
963 vq->inflight_split->desc[idx].counter = vq->global_counter++;
964 vq->inflight_split->desc[idx].inflight = 1;
969 rte_vhost_set_inflight_desc_packed(int vid, uint16_t vring_idx,
970 uint16_t head, uint16_t last,
971 uint16_t *inflight_entry)
973 struct rte_vhost_inflight_info_packed *inflight_info;
974 struct virtio_net *dev;
975 struct vhost_virtqueue *vq;
976 struct vring_packed_desc *desc;
977 uint16_t old_free_head, free_head;
979 dev = get_device(vid);
983 if (unlikely(!(dev->protocol_features &
984 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
987 if (unlikely(!vq_is_packed(dev)))
990 if (unlikely(vring_idx >= VHOST_MAX_VRING))
993 vq = dev->virtqueue[vring_idx];
997 inflight_info = vq->inflight_packed;
998 if (unlikely(!inflight_info))
1001 if (unlikely(head >= vq->size))
1004 desc = vq->desc_packed;
1005 old_free_head = inflight_info->old_free_head;
1006 if (unlikely(old_free_head >= vq->size))
1009 free_head = old_free_head;
1011 /* init header descriptor */
1012 inflight_info->desc[old_free_head].num = 0;
1013 inflight_info->desc[old_free_head].counter = vq->global_counter++;
1014 inflight_info->desc[old_free_head].inflight = 1;
1016 /* save desc entry in flight entry */
1017 while (head != ((last + 1) % vq->size)) {
1018 inflight_info->desc[old_free_head].num++;
1019 inflight_info->desc[free_head].addr = desc[head].addr;
1020 inflight_info->desc[free_head].len = desc[head].len;
1021 inflight_info->desc[free_head].flags = desc[head].flags;
1022 inflight_info->desc[free_head].id = desc[head].id;
1024 inflight_info->desc[old_free_head].last = free_head;
1025 free_head = inflight_info->desc[free_head].next;
1026 inflight_info->free_head = free_head;
1027 head = (head + 1) % vq->size;
1030 inflight_info->old_free_head = free_head;
1031 *inflight_entry = old_free_head;
1037 rte_vhost_clr_inflight_desc_split(int vid, uint16_t vring_idx,
1038 uint16_t last_used_idx, uint16_t idx)
1040 struct virtio_net *dev;
1041 struct vhost_virtqueue *vq;
1043 dev = get_device(vid);
1047 if (unlikely(!(dev->protocol_features &
1048 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1051 if (unlikely(vq_is_packed(dev)))
1054 if (unlikely(vring_idx >= VHOST_MAX_VRING))
1057 vq = dev->virtqueue[vring_idx];
1061 if (unlikely(!vq->inflight_split))
1064 if (unlikely(idx >= vq->size))
1069 vq->inflight_split->desc[idx].inflight = 0;
1073 vq->inflight_split->used_idx = last_used_idx;
1078 rte_vhost_clr_inflight_desc_packed(int vid, uint16_t vring_idx,
1081 struct rte_vhost_inflight_info_packed *inflight_info;
1082 struct virtio_net *dev;
1083 struct vhost_virtqueue *vq;
1085 dev = get_device(vid);
1089 if (unlikely(!(dev->protocol_features &
1090 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1093 if (unlikely(!vq_is_packed(dev)))
1096 if (unlikely(vring_idx >= VHOST_MAX_VRING))
1099 vq = dev->virtqueue[vring_idx];
1103 inflight_info = vq->inflight_packed;
1104 if (unlikely(!inflight_info))
1107 if (unlikely(head >= vq->size))
1112 inflight_info->desc[head].inflight = 0;
1116 inflight_info->old_free_head = inflight_info->free_head;
1117 inflight_info->old_used_idx = inflight_info->used_idx;
1118 inflight_info->old_used_wrap_counter = inflight_info->used_wrap_counter;
1124 rte_vhost_set_last_inflight_io_split(int vid, uint16_t vring_idx,
1127 struct virtio_net *dev;
1128 struct vhost_virtqueue *vq;
1130 dev = get_device(vid);
1134 if (unlikely(!(dev->protocol_features &
1135 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1138 if (unlikely(vq_is_packed(dev)))
1141 if (unlikely(vring_idx >= VHOST_MAX_VRING))
1144 vq = dev->virtqueue[vring_idx];
1148 if (unlikely(!vq->inflight_split))
1151 vq->inflight_split->last_inflight_io = idx;
1156 rte_vhost_set_last_inflight_io_packed(int vid, uint16_t vring_idx,
1159 struct rte_vhost_inflight_info_packed *inflight_info;
1160 struct virtio_net *dev;
1161 struct vhost_virtqueue *vq;
1164 dev = get_device(vid);
1168 if (unlikely(!(dev->protocol_features &
1169 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1172 if (unlikely(!vq_is_packed(dev)))
1175 if (unlikely(vring_idx >= VHOST_MAX_VRING))
1178 vq = dev->virtqueue[vring_idx];
1182 inflight_info = vq->inflight_packed;
1183 if (unlikely(!inflight_info))
1186 if (unlikely(head >= vq->size))
1189 last = inflight_info->desc[head].last;
1190 if (unlikely(last >= vq->size))
1193 inflight_info->desc[last].next = inflight_info->free_head;
1194 inflight_info->free_head = head;
1195 inflight_info->used_idx += inflight_info->desc[head].num;
1196 if (inflight_info->used_idx >= inflight_info->desc_num) {
1197 inflight_info->used_idx -= inflight_info->desc_num;
1198 inflight_info->used_wrap_counter =
1199 !inflight_info->used_wrap_counter;
1206 rte_vhost_vring_call(int vid, uint16_t vring_idx)
1208 struct virtio_net *dev;
1209 struct vhost_virtqueue *vq;
1211 dev = get_device(vid);
1215 if (vring_idx >= VHOST_MAX_VRING)
1218 vq = dev->virtqueue[vring_idx];
1222 if (vq_is_packed(dev))
1223 vhost_vring_call_packed(dev, vq);
1225 vhost_vring_call_split(dev, vq);
1231 rte_vhost_avail_entries(int vid, uint16_t queue_id)
1233 struct virtio_net *dev;
1234 struct vhost_virtqueue *vq;
1237 dev = get_device(vid);
1241 vq = dev->virtqueue[queue_id];
1243 rte_spinlock_lock(&vq->access_lock);
1245 if (unlikely(!vq->enabled || vq->avail == NULL))
1248 ret = *(volatile uint16_t *)&vq->avail->idx - vq->last_used_idx;
1251 rte_spinlock_unlock(&vq->access_lock);
1256 vhost_enable_notify_split(struct virtio_net *dev,
1257 struct vhost_virtqueue *vq, int enable)
1259 if (vq->used == NULL)
1262 if (!(dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))) {
1264 vq->used->flags &= ~VRING_USED_F_NO_NOTIFY;
1266 vq->used->flags |= VRING_USED_F_NO_NOTIFY;
1269 vhost_avail_event(vq) = vq->last_avail_idx;
1275 vhost_enable_notify_packed(struct virtio_net *dev,
1276 struct vhost_virtqueue *vq, int enable)
1280 if (vq->device_event == NULL)
1284 vq->device_event->flags = VRING_EVENT_F_DISABLE;
1288 flags = VRING_EVENT_F_ENABLE;
1289 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX)) {
1290 flags = VRING_EVENT_F_DESC;
1291 vq->device_event->off_wrap = vq->last_avail_idx |
1292 vq->avail_wrap_counter << 15;
1297 vq->device_event->flags = flags;
1302 vhost_enable_guest_notification(struct virtio_net *dev,
1303 struct vhost_virtqueue *vq, int enable)
1306 * If the virtqueue is not ready yet, it will be applied
1307 * when it will become ready.
1312 if (vq_is_packed(dev))
1313 return vhost_enable_notify_packed(dev, vq, enable);
1315 return vhost_enable_notify_split(dev, vq, enable);
1319 rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable)
1321 struct virtio_net *dev = get_device(vid);
1322 struct vhost_virtqueue *vq;
1328 vq = dev->virtqueue[queue_id];
1330 rte_spinlock_lock(&vq->access_lock);
1332 vq->notif_enable = enable;
1333 ret = vhost_enable_guest_notification(dev, vq, enable);
1335 rte_spinlock_unlock(&vq->access_lock);
1341 rte_vhost_log_write(int vid, uint64_t addr, uint64_t len)
1343 struct virtio_net *dev = get_device(vid);
1348 vhost_log_write(dev, addr, len);
1352 rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
1353 uint64_t offset, uint64_t len)
1355 struct virtio_net *dev;
1356 struct vhost_virtqueue *vq;
1358 dev = get_device(vid);
1362 if (vring_idx >= VHOST_MAX_VRING)
1364 vq = dev->virtqueue[vring_idx];
1368 vhost_log_used_vring(dev, vq, offset, len);
1372 rte_vhost_rx_queue_count(int vid, uint16_t qid)
1374 struct virtio_net *dev;
1375 struct vhost_virtqueue *vq;
1378 dev = get_device(vid);
1382 if (unlikely(qid >= dev->nr_vring || (qid & 1) == 0)) {
1383 VHOST_LOG_DATA(ERR, "(%d) %s: invalid virtqueue idx %d.\n",
1384 dev->vid, __func__, qid);
1388 vq = dev->virtqueue[qid];
1392 rte_spinlock_lock(&vq->access_lock);
1394 if (unlikely(vq->enabled == 0 || vq->avail == NULL))
1397 ret = *((volatile uint16_t *)&vq->avail->idx) - vq->last_avail_idx;
1400 rte_spinlock_unlock(&vq->access_lock);
1404 struct rte_vdpa_device *
1405 rte_vhost_get_vdpa_device(int vid)
1407 struct virtio_net *dev = get_device(vid);
1412 return dev->vdpa_dev;
1415 int rte_vhost_get_log_base(int vid, uint64_t *log_base,
1418 struct virtio_net *dev = get_device(vid);
1420 if (dev == NULL || log_base == NULL || log_size == NULL)
1423 *log_base = dev->log_base;
1424 *log_size = dev->log_size;
1429 int rte_vhost_get_vring_base(int vid, uint16_t queue_id,
1430 uint16_t *last_avail_idx, uint16_t *last_used_idx)
1432 struct vhost_virtqueue *vq;
1433 struct virtio_net *dev = get_device(vid);
1435 if (dev == NULL || last_avail_idx == NULL || last_used_idx == NULL)
1438 vq = dev->virtqueue[queue_id];
1442 if (vq_is_packed(dev)) {
1443 *last_avail_idx = (vq->avail_wrap_counter << 15) |
1445 *last_used_idx = (vq->used_wrap_counter << 15) |
1448 *last_avail_idx = vq->last_avail_idx;
1449 *last_used_idx = vq->last_used_idx;
1455 int rte_vhost_set_vring_base(int vid, uint16_t queue_id,
1456 uint16_t last_avail_idx, uint16_t last_used_idx)
1458 struct vhost_virtqueue *vq;
1459 struct virtio_net *dev = get_device(vid);
1464 vq = dev->virtqueue[queue_id];
1468 if (vq_is_packed(dev)) {
1469 vq->last_avail_idx = last_avail_idx & 0x7fff;
1470 vq->avail_wrap_counter = !!(last_avail_idx & (1 << 15));
1471 vq->last_used_idx = last_used_idx & 0x7fff;
1472 vq->used_wrap_counter = !!(last_used_idx & (1 << 15));
1474 vq->last_avail_idx = last_avail_idx;
1475 vq->last_used_idx = last_used_idx;
1482 rte_vhost_get_vring_base_from_inflight(int vid,
1484 uint16_t *last_avail_idx,
1485 uint16_t *last_used_idx)
1487 struct rte_vhost_inflight_info_packed *inflight_info;
1488 struct virtio_net *dev = get_device(vid);
1490 if (dev == NULL || last_avail_idx == NULL || last_used_idx == NULL)
1493 if (!vq_is_packed(dev))
1496 inflight_info = dev->virtqueue[queue_id]->inflight_packed;
1500 *last_avail_idx = (inflight_info->old_used_wrap_counter << 15) |
1501 inflight_info->old_used_idx;
1502 *last_used_idx = *last_avail_idx;
1507 int rte_vhost_extern_callback_register(int vid,
1508 struct rte_vhost_user_extern_ops const * const ops, void *ctx)
1510 struct virtio_net *dev = get_device(vid);
1512 if (dev == NULL || ops == NULL)
1515 dev->extern_ops = *ops;
1516 dev->extern_data = ctx;
1520 int rte_vhost_async_channel_register(int vid, uint16_t queue_id,
1522 struct rte_vhost_async_channel_ops *ops)
1524 struct vhost_virtqueue *vq;
1525 struct virtio_net *dev = get_device(vid);
1526 struct rte_vhost_async_features f;
1528 if (dev == NULL || ops == NULL)
1531 f.intval = features;
1533 vq = dev->virtqueue[queue_id];
1535 if (unlikely(vq == NULL || !dev->async_copy))
1538 /* packed queue is not supported */
1539 if (unlikely(vq_is_packed(dev) || !f.async_inorder)) {
1540 VHOST_LOG_CONFIG(ERR,
1541 "async copy is not supported on packed queue or non-inorder mode "
1542 "(vid %d, qid: %d)\n", vid, queue_id);
1546 if (unlikely(ops->check_completed_copies == NULL ||
1547 ops->transfer_data == NULL))
1550 rte_spinlock_lock(&vq->access_lock);
1552 if (unlikely(vq->async_registered)) {
1553 VHOST_LOG_CONFIG(ERR,
1554 "async register failed: channel already registered "
1555 "(vid %d, qid: %d)\n", vid, queue_id);
1559 vq->async_pkts_pending = rte_malloc(NULL,
1560 vq->size * sizeof(uintptr_t),
1561 RTE_CACHE_LINE_SIZE);
1562 vq->async_pkts_info = rte_malloc(NULL,
1563 vq->size * sizeof(struct async_inflight_info),
1564 RTE_CACHE_LINE_SIZE);
1565 if (!vq->async_pkts_pending || !vq->async_pkts_info) {
1566 if (vq->async_pkts_pending)
1567 rte_free(vq->async_pkts_pending);
1569 if (vq->async_pkts_info)
1570 rte_free(vq->async_pkts_info);
1572 VHOST_LOG_CONFIG(ERR,
1573 "async register failed: cannot allocate memory for vq data "
1574 "(vid %d, qid: %d)\n", vid, queue_id);
1578 vq->async_ops.check_completed_copies = ops->check_completed_copies;
1579 vq->async_ops.transfer_data = ops->transfer_data;
1581 vq->async_inorder = f.async_inorder;
1582 vq->async_threshold = f.async_threshold;
1584 vq->async_registered = true;
1587 rte_spinlock_unlock(&vq->access_lock);
1592 int rte_vhost_async_channel_unregister(int vid, uint16_t queue_id)
1594 struct vhost_virtqueue *vq;
1595 struct virtio_net *dev = get_device(vid);
1601 vq = dev->virtqueue[queue_id];
1607 rte_spinlock_lock(&vq->access_lock);
1609 if (!vq->async_registered)
1612 if (vq->async_pkts_inflight_n) {
1613 VHOST_LOG_CONFIG(ERR, "Failed to unregister async channel. "
1614 "async inflight packets must be completed before unregistration.\n");
1619 if (vq->async_pkts_pending) {
1620 rte_free(vq->async_pkts_pending);
1621 vq->async_pkts_pending = NULL;
1624 if (vq->async_pkts_info) {
1625 rte_free(vq->async_pkts_info);
1626 vq->async_pkts_info = NULL;
1629 vq->async_ops.transfer_data = NULL;
1630 vq->async_ops.check_completed_copies = NULL;
1631 vq->async_registered = false;
1634 rte_spinlock_unlock(&vq->access_lock);
1639 RTE_LOG_REGISTER(vhost_config_log_level, lib.vhost.config, INFO);
1640 RTE_LOG_REGISTER(vhost_data_log_level, lib.vhost.data, WARNING);