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 rte_free(vq->batch_copy_elems);
335 rte_mempool_free(vq->iotlb_pool);
340 * Release virtqueues and device memory.
343 free_device(struct virtio_net *dev)
347 for (i = 0; i < dev->nr_vring; i++)
348 free_vq(dev, dev->virtqueue[i]);
353 static __rte_always_inline int
354 log_translate(struct virtio_net *dev, struct vhost_virtqueue *vq)
356 if (likely(!(vq->ring_addrs.flags & (1 << VHOST_VRING_F_LOG))))
359 vq->log_guest_addr = translate_log_addr(dev, vq,
360 vq->ring_addrs.log_guest_addr);
361 if (vq->log_guest_addr == 0)
368 * Converts vring log address to GPA
369 * If IOMMU is enabled, the log address is IOVA
370 * If IOMMU not enabled, the log address is already GPA
372 * Caller should have iotlb_lock read-locked
375 translate_log_addr(struct virtio_net *dev, struct vhost_virtqueue *vq,
378 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)) {
379 const uint64_t exp_size = sizeof(uint64_t);
381 uint64_t size = exp_size;
383 hva = vhost_iova_to_vva(dev, vq, log_addr,
384 &size, VHOST_ACCESS_RW);
386 if (size != exp_size)
389 gpa = hva_to_gpa(dev, hva, exp_size);
391 VHOST_LOG_CONFIG(ERR,
392 "VQ: Failed to find GPA for log_addr: 0x%"
393 PRIx64 " hva: 0x%" PRIx64 "\n",
403 /* Caller should have iotlb_lock read-locked */
405 vring_translate_split(struct virtio_net *dev, struct vhost_virtqueue *vq)
407 uint64_t req_size, size;
409 req_size = sizeof(struct vring_desc) * vq->size;
411 vq->desc = (struct vring_desc *)(uintptr_t)vhost_iova_to_vva(dev, vq,
412 vq->ring_addrs.desc_user_addr,
413 &size, VHOST_ACCESS_RW);
414 if (!vq->desc || size != req_size)
417 req_size = sizeof(struct vring_avail);
418 req_size += sizeof(uint16_t) * vq->size;
419 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))
420 req_size += sizeof(uint16_t);
422 vq->avail = (struct vring_avail *)(uintptr_t)vhost_iova_to_vva(dev, vq,
423 vq->ring_addrs.avail_user_addr,
424 &size, VHOST_ACCESS_RW);
425 if (!vq->avail || size != req_size)
428 req_size = sizeof(struct vring_used);
429 req_size += sizeof(struct vring_used_elem) * vq->size;
430 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))
431 req_size += sizeof(uint16_t);
433 vq->used = (struct vring_used *)(uintptr_t)vhost_iova_to_vva(dev, vq,
434 vq->ring_addrs.used_user_addr,
435 &size, VHOST_ACCESS_RW);
436 if (!vq->used || size != req_size)
442 /* Caller should have iotlb_lock read-locked */
444 vring_translate_packed(struct virtio_net *dev, struct vhost_virtqueue *vq)
446 uint64_t req_size, size;
448 req_size = sizeof(struct vring_packed_desc) * vq->size;
450 vq->desc_packed = (struct vring_packed_desc *)(uintptr_t)
451 vhost_iova_to_vva(dev, vq, vq->ring_addrs.desc_user_addr,
452 &size, VHOST_ACCESS_RW);
453 if (!vq->desc_packed || size != req_size)
456 req_size = sizeof(struct vring_packed_desc_event);
458 vq->driver_event = (struct vring_packed_desc_event *)(uintptr_t)
459 vhost_iova_to_vva(dev, vq, vq->ring_addrs.avail_user_addr,
460 &size, VHOST_ACCESS_RW);
461 if (!vq->driver_event || size != req_size)
464 req_size = sizeof(struct vring_packed_desc_event);
466 vq->device_event = (struct vring_packed_desc_event *)(uintptr_t)
467 vhost_iova_to_vva(dev, vq, vq->ring_addrs.used_user_addr,
468 &size, VHOST_ACCESS_RW);
469 if (!vq->device_event || size != req_size)
476 vring_translate(struct virtio_net *dev, struct vhost_virtqueue *vq)
479 if (!(dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)))
482 if (vq_is_packed(dev)) {
483 if (vring_translate_packed(dev, vq) < 0)
486 if (vring_translate_split(dev, vq) < 0)
490 if (log_translate(dev, vq) < 0)
499 vring_invalidate(struct virtio_net *dev, struct vhost_virtqueue *vq)
501 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
502 vhost_user_iotlb_wr_lock(vq);
508 vq->log_guest_addr = 0;
510 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
511 vhost_user_iotlb_wr_unlock(vq);
515 init_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
517 struct vhost_virtqueue *vq;
519 if (vring_idx >= VHOST_MAX_VRING) {
520 VHOST_LOG_CONFIG(ERR,
521 "Failed not init vring, out of bound (%d)\n",
526 vq = dev->virtqueue[vring_idx];
528 memset(vq, 0, sizeof(struct vhost_virtqueue));
530 vq->kickfd = VIRTIO_UNINITIALIZED_EVENTFD;
531 vq->callfd = VIRTIO_UNINITIALIZED_EVENTFD;
533 vhost_user_iotlb_init(dev, vring_idx);
534 /* Backends are set to -1 indicating an inactive device. */
537 TAILQ_INIT(&vq->zmbuf_list);
541 reset_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
543 struct vhost_virtqueue *vq;
546 if (vring_idx >= VHOST_MAX_VRING) {
547 VHOST_LOG_CONFIG(ERR,
548 "Failed not init vring, out of bound (%d)\n",
553 vq = dev->virtqueue[vring_idx];
555 init_vring_queue(dev, vring_idx);
560 alloc_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
562 struct vhost_virtqueue *vq;
564 vq = rte_malloc(NULL, sizeof(struct vhost_virtqueue), 0);
566 VHOST_LOG_CONFIG(ERR,
567 "Failed to allocate memory for vring:%u.\n", vring_idx);
571 dev->virtqueue[vring_idx] = vq;
572 init_vring_queue(dev, vring_idx);
573 rte_spinlock_init(&vq->access_lock);
574 vq->avail_wrap_counter = 1;
575 vq->used_wrap_counter = 1;
576 vq->signalled_used_valid = false;
584 * Reset some variables in device structure, while keeping few
585 * others untouched, such as vid, ifname, nr_vring: they
586 * should be same unless the device is removed.
589 reset_device(struct virtio_net *dev)
594 dev->protocol_features = 0;
595 dev->flags &= VIRTIO_DEV_BUILTIN_VIRTIO_NET;
597 for (i = 0; i < dev->nr_vring; i++)
598 reset_vring_queue(dev, i);
602 * Invoked when there is a new vhost-user connection established (when
603 * there is a new virtio device being attached).
606 vhost_new_device(void)
608 struct virtio_net *dev;
611 for (i = 0; i < MAX_VHOST_DEVICE; i++) {
612 if (vhost_devices[i] == NULL)
616 if (i == MAX_VHOST_DEVICE) {
617 VHOST_LOG_CONFIG(ERR,
618 "Failed to find a free slot for new device.\n");
622 dev = rte_zmalloc(NULL, sizeof(struct virtio_net), 0);
624 VHOST_LOG_CONFIG(ERR,
625 "Failed to allocate memory for new dev.\n");
629 vhost_devices[i] = dev;
631 dev->flags = VIRTIO_DEV_BUILTIN_VIRTIO_NET;
632 dev->slave_req_fd = -1;
633 dev->postcopy_ufd = -1;
634 rte_spinlock_init(&dev->slave_req_lock);
640 vhost_destroy_device_notify(struct virtio_net *dev)
642 struct rte_vdpa_device *vdpa_dev;
644 if (dev->flags & VIRTIO_DEV_RUNNING) {
645 vdpa_dev = dev->vdpa_dev;
646 if (vdpa_dev && vdpa_dev->ops->dev_close)
647 vdpa_dev->ops->dev_close(dev->vid);
648 dev->flags &= ~VIRTIO_DEV_RUNNING;
649 dev->notify_ops->destroy_device(dev->vid);
654 * Invoked when there is the vhost-user connection is broken (when
655 * the virtio device is being detached).
658 vhost_destroy_device(int vid)
660 struct virtio_net *dev = get_device(vid);
665 vhost_destroy_device_notify(dev);
667 cleanup_device(dev, 1);
670 vhost_devices[vid] = NULL;
674 vhost_attach_vdpa_device(int vid, struct rte_vdpa_device *vdpa_dev)
676 struct virtio_net *dev = get_device(vid);
681 dev->vdpa_dev = vdpa_dev;
685 vhost_set_ifname(int vid, const char *if_name, unsigned int if_len)
687 struct virtio_net *dev;
690 dev = get_device(vid);
694 len = if_len > sizeof(dev->ifname) ?
695 sizeof(dev->ifname) : if_len;
697 strncpy(dev->ifname, if_name, len);
698 dev->ifname[sizeof(dev->ifname) - 1] = '\0';
702 vhost_enable_dequeue_zero_copy(int vid)
704 struct virtio_net *dev = get_device(vid);
709 dev->dequeue_zero_copy = 1;
713 vhost_set_builtin_virtio_net(int vid, bool enable)
715 struct virtio_net *dev = get_device(vid);
721 dev->flags |= VIRTIO_DEV_BUILTIN_VIRTIO_NET;
723 dev->flags &= ~VIRTIO_DEV_BUILTIN_VIRTIO_NET;
727 vhost_enable_extbuf(int vid)
729 struct virtio_net *dev = get_device(vid);
738 vhost_enable_linearbuf(int vid)
740 struct virtio_net *dev = get_device(vid);
749 rte_vhost_get_mtu(int vid, uint16_t *mtu)
751 struct virtio_net *dev = get_device(vid);
753 if (dev == NULL || mtu == NULL)
756 if (!(dev->flags & VIRTIO_DEV_READY))
759 if (!(dev->features & (1ULL << VIRTIO_NET_F_MTU)))
768 rte_vhost_get_numa_node(int vid)
770 #ifdef RTE_LIBRTE_VHOST_NUMA
771 struct virtio_net *dev = get_device(vid);
775 if (dev == NULL || numa_available() != 0)
778 ret = get_mempolicy(&numa_node, NULL, 0, dev,
779 MPOL_F_NODE | MPOL_F_ADDR);
781 VHOST_LOG_CONFIG(ERR,
782 "(%d) failed to query numa node: %s\n",
783 vid, rte_strerror(errno));
795 rte_vhost_get_queue_num(int vid)
797 struct virtio_net *dev = get_device(vid);
802 return dev->nr_vring / 2;
806 rte_vhost_get_vring_num(int vid)
808 struct virtio_net *dev = get_device(vid);
813 return dev->nr_vring;
817 rte_vhost_get_ifname(int vid, char *buf, size_t len)
819 struct virtio_net *dev = get_device(vid);
821 if (dev == NULL || buf == NULL)
824 len = RTE_MIN(len, sizeof(dev->ifname));
826 strncpy(buf, dev->ifname, len);
833 rte_vhost_get_negotiated_features(int vid, uint64_t *features)
835 struct virtio_net *dev;
837 dev = get_device(vid);
838 if (dev == NULL || features == NULL)
841 *features = dev->features;
846 rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem)
848 struct virtio_net *dev;
849 struct rte_vhost_memory *m;
852 dev = get_device(vid);
853 if (dev == NULL || mem == NULL)
856 size = dev->mem->nregions * sizeof(struct rte_vhost_mem_region);
857 m = malloc(sizeof(struct rte_vhost_memory) + size);
861 m->nregions = dev->mem->nregions;
862 memcpy(m->regions, dev->mem->regions, size);
869 rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
870 struct rte_vhost_vring *vring)
872 struct virtio_net *dev;
873 struct vhost_virtqueue *vq;
875 dev = get_device(vid);
876 if (dev == NULL || vring == NULL)
879 if (vring_idx >= VHOST_MAX_VRING)
882 vq = dev->virtqueue[vring_idx];
886 if (vq_is_packed(dev)) {
887 vring->desc_packed = vq->desc_packed;
888 vring->driver_event = vq->driver_event;
889 vring->device_event = vq->device_event;
891 vring->desc = vq->desc;
892 vring->avail = vq->avail;
893 vring->used = vq->used;
895 vring->log_guest_addr = vq->log_guest_addr;
897 vring->callfd = vq->callfd;
898 vring->kickfd = vq->kickfd;
899 vring->size = vq->size;
905 rte_vhost_get_vhost_ring_inflight(int vid, uint16_t vring_idx,
906 struct rte_vhost_ring_inflight *vring)
908 struct virtio_net *dev;
909 struct vhost_virtqueue *vq;
911 dev = get_device(vid);
915 if (vring_idx >= VHOST_MAX_VRING)
918 vq = dev->virtqueue[vring_idx];
922 if (vq_is_packed(dev)) {
923 if (unlikely(!vq->inflight_packed))
926 vring->inflight_packed = vq->inflight_packed;
928 if (unlikely(!vq->inflight_split))
931 vring->inflight_split = vq->inflight_split;
934 vring->resubmit_inflight = vq->resubmit_inflight;
940 rte_vhost_set_inflight_desc_split(int vid, uint16_t vring_idx,
943 struct vhost_virtqueue *vq;
944 struct virtio_net *dev;
946 dev = get_device(vid);
950 if (unlikely(!(dev->protocol_features &
951 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
954 if (unlikely(vq_is_packed(dev)))
957 if (unlikely(vring_idx >= VHOST_MAX_VRING))
960 vq = dev->virtqueue[vring_idx];
964 if (unlikely(!vq->inflight_split))
967 if (unlikely(idx >= vq->size))
970 vq->inflight_split->desc[idx].counter = vq->global_counter++;
971 vq->inflight_split->desc[idx].inflight = 1;
976 rte_vhost_set_inflight_desc_packed(int vid, uint16_t vring_idx,
977 uint16_t head, uint16_t last,
978 uint16_t *inflight_entry)
980 struct rte_vhost_inflight_info_packed *inflight_info;
981 struct virtio_net *dev;
982 struct vhost_virtqueue *vq;
983 struct vring_packed_desc *desc;
984 uint16_t old_free_head, free_head;
986 dev = get_device(vid);
990 if (unlikely(!(dev->protocol_features &
991 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
994 if (unlikely(!vq_is_packed(dev)))
997 if (unlikely(vring_idx >= VHOST_MAX_VRING))
1000 vq = dev->virtqueue[vring_idx];
1004 inflight_info = vq->inflight_packed;
1005 if (unlikely(!inflight_info))
1008 if (unlikely(head >= vq->size))
1011 desc = vq->desc_packed;
1012 old_free_head = inflight_info->old_free_head;
1013 if (unlikely(old_free_head >= vq->size))
1016 free_head = old_free_head;
1018 /* init header descriptor */
1019 inflight_info->desc[old_free_head].num = 0;
1020 inflight_info->desc[old_free_head].counter = vq->global_counter++;
1021 inflight_info->desc[old_free_head].inflight = 1;
1023 /* save desc entry in flight entry */
1024 while (head != ((last + 1) % vq->size)) {
1025 inflight_info->desc[old_free_head].num++;
1026 inflight_info->desc[free_head].addr = desc[head].addr;
1027 inflight_info->desc[free_head].len = desc[head].len;
1028 inflight_info->desc[free_head].flags = desc[head].flags;
1029 inflight_info->desc[free_head].id = desc[head].id;
1031 inflight_info->desc[old_free_head].last = free_head;
1032 free_head = inflight_info->desc[free_head].next;
1033 inflight_info->free_head = free_head;
1034 head = (head + 1) % vq->size;
1037 inflight_info->old_free_head = free_head;
1038 *inflight_entry = old_free_head;
1044 rte_vhost_clr_inflight_desc_split(int vid, uint16_t vring_idx,
1045 uint16_t last_used_idx, uint16_t idx)
1047 struct virtio_net *dev;
1048 struct vhost_virtqueue *vq;
1050 dev = get_device(vid);
1054 if (unlikely(!(dev->protocol_features &
1055 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1058 if (unlikely(vq_is_packed(dev)))
1061 if (unlikely(vring_idx >= VHOST_MAX_VRING))
1064 vq = dev->virtqueue[vring_idx];
1068 if (unlikely(!vq->inflight_split))
1071 if (unlikely(idx >= vq->size))
1076 vq->inflight_split->desc[idx].inflight = 0;
1080 vq->inflight_split->used_idx = last_used_idx;
1085 rte_vhost_clr_inflight_desc_packed(int vid, uint16_t vring_idx,
1088 struct rte_vhost_inflight_info_packed *inflight_info;
1089 struct virtio_net *dev;
1090 struct vhost_virtqueue *vq;
1092 dev = get_device(vid);
1096 if (unlikely(!(dev->protocol_features &
1097 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1100 if (unlikely(!vq_is_packed(dev)))
1103 if (unlikely(vring_idx >= VHOST_MAX_VRING))
1106 vq = dev->virtqueue[vring_idx];
1110 inflight_info = vq->inflight_packed;
1111 if (unlikely(!inflight_info))
1114 if (unlikely(head >= vq->size))
1119 inflight_info->desc[head].inflight = 0;
1123 inflight_info->old_free_head = inflight_info->free_head;
1124 inflight_info->old_used_idx = inflight_info->used_idx;
1125 inflight_info->old_used_wrap_counter = inflight_info->used_wrap_counter;
1131 rte_vhost_set_last_inflight_io_split(int vid, uint16_t vring_idx,
1134 struct virtio_net *dev;
1135 struct vhost_virtqueue *vq;
1137 dev = get_device(vid);
1141 if (unlikely(!(dev->protocol_features &
1142 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1145 if (unlikely(vq_is_packed(dev)))
1148 if (unlikely(vring_idx >= VHOST_MAX_VRING))
1151 vq = dev->virtqueue[vring_idx];
1155 if (unlikely(!vq->inflight_split))
1158 vq->inflight_split->last_inflight_io = idx;
1163 rte_vhost_set_last_inflight_io_packed(int vid, uint16_t vring_idx,
1166 struct rte_vhost_inflight_info_packed *inflight_info;
1167 struct virtio_net *dev;
1168 struct vhost_virtqueue *vq;
1171 dev = get_device(vid);
1175 if (unlikely(!(dev->protocol_features &
1176 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1179 if (unlikely(!vq_is_packed(dev)))
1182 if (unlikely(vring_idx >= VHOST_MAX_VRING))
1185 vq = dev->virtqueue[vring_idx];
1189 inflight_info = vq->inflight_packed;
1190 if (unlikely(!inflight_info))
1193 if (unlikely(head >= vq->size))
1196 last = inflight_info->desc[head].last;
1197 if (unlikely(last >= vq->size))
1200 inflight_info->desc[last].next = inflight_info->free_head;
1201 inflight_info->free_head = head;
1202 inflight_info->used_idx += inflight_info->desc[head].num;
1203 if (inflight_info->used_idx >= inflight_info->desc_num) {
1204 inflight_info->used_idx -= inflight_info->desc_num;
1205 inflight_info->used_wrap_counter =
1206 !inflight_info->used_wrap_counter;
1213 rte_vhost_vring_call(int vid, uint16_t vring_idx)
1215 struct virtio_net *dev;
1216 struct vhost_virtqueue *vq;
1218 dev = get_device(vid);
1222 if (vring_idx >= VHOST_MAX_VRING)
1225 vq = dev->virtqueue[vring_idx];
1229 if (vq_is_packed(dev))
1230 vhost_vring_call_packed(dev, vq);
1232 vhost_vring_call_split(dev, vq);
1238 rte_vhost_avail_entries(int vid, uint16_t queue_id)
1240 struct virtio_net *dev;
1241 struct vhost_virtqueue *vq;
1244 dev = get_device(vid);
1248 vq = dev->virtqueue[queue_id];
1250 rte_spinlock_lock(&vq->access_lock);
1252 if (unlikely(!vq->enabled || vq->avail == NULL))
1255 ret = *(volatile uint16_t *)&vq->avail->idx - vq->last_used_idx;
1258 rte_spinlock_unlock(&vq->access_lock);
1263 vhost_enable_notify_split(struct virtio_net *dev,
1264 struct vhost_virtqueue *vq, int enable)
1266 if (vq->used == NULL)
1269 if (!(dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))) {
1271 vq->used->flags &= ~VRING_USED_F_NO_NOTIFY;
1273 vq->used->flags |= VRING_USED_F_NO_NOTIFY;
1276 vhost_avail_event(vq) = vq->last_avail_idx;
1282 vhost_enable_notify_packed(struct virtio_net *dev,
1283 struct vhost_virtqueue *vq, int enable)
1287 if (vq->device_event == NULL)
1291 vq->device_event->flags = VRING_EVENT_F_DISABLE;
1295 flags = VRING_EVENT_F_ENABLE;
1296 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX)) {
1297 flags = VRING_EVENT_F_DESC;
1298 vq->device_event->off_wrap = vq->last_avail_idx |
1299 vq->avail_wrap_counter << 15;
1304 vq->device_event->flags = flags;
1309 rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable)
1311 struct virtio_net *dev = get_device(vid);
1312 struct vhost_virtqueue *vq;
1318 vq = dev->virtqueue[queue_id];
1320 rte_spinlock_lock(&vq->access_lock);
1322 if (vq_is_packed(dev))
1323 ret = vhost_enable_notify_packed(dev, vq, enable);
1325 ret = vhost_enable_notify_split(dev, vq, enable);
1327 rte_spinlock_unlock(&vq->access_lock);
1333 rte_vhost_log_write(int vid, uint64_t addr, uint64_t len)
1335 struct virtio_net *dev = get_device(vid);
1340 vhost_log_write(dev, addr, len);
1344 rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
1345 uint64_t offset, uint64_t len)
1347 struct virtio_net *dev;
1348 struct vhost_virtqueue *vq;
1350 dev = get_device(vid);
1354 if (vring_idx >= VHOST_MAX_VRING)
1356 vq = dev->virtqueue[vring_idx];
1360 vhost_log_used_vring(dev, vq, offset, len);
1364 rte_vhost_rx_queue_count(int vid, uint16_t qid)
1366 struct virtio_net *dev;
1367 struct vhost_virtqueue *vq;
1370 dev = get_device(vid);
1374 if (unlikely(qid >= dev->nr_vring || (qid & 1) == 0)) {
1375 VHOST_LOG_DATA(ERR, "(%d) %s: invalid virtqueue idx %d.\n",
1376 dev->vid, __func__, qid);
1380 vq = dev->virtqueue[qid];
1384 rte_spinlock_lock(&vq->access_lock);
1386 if (unlikely(vq->enabled == 0 || vq->avail == NULL))
1389 ret = *((volatile uint16_t *)&vq->avail->idx) - vq->last_avail_idx;
1392 rte_spinlock_unlock(&vq->access_lock);
1396 struct rte_vdpa_device *
1397 rte_vhost_get_vdpa_device(int vid)
1399 struct virtio_net *dev = get_device(vid);
1404 return dev->vdpa_dev;
1407 int rte_vhost_get_log_base(int vid, uint64_t *log_base,
1410 struct virtio_net *dev = get_device(vid);
1412 if (dev == NULL || log_base == NULL || log_size == NULL)
1415 *log_base = dev->log_base;
1416 *log_size = dev->log_size;
1421 int rte_vhost_get_vring_base(int vid, uint16_t queue_id,
1422 uint16_t *last_avail_idx, uint16_t *last_used_idx)
1424 struct vhost_virtqueue *vq;
1425 struct virtio_net *dev = get_device(vid);
1427 if (dev == NULL || last_avail_idx == NULL || last_used_idx == NULL)
1430 vq = dev->virtqueue[queue_id];
1434 if (vq_is_packed(dev)) {
1435 *last_avail_idx = (vq->avail_wrap_counter << 15) |
1437 *last_used_idx = (vq->used_wrap_counter << 15) |
1440 *last_avail_idx = vq->last_avail_idx;
1441 *last_used_idx = vq->last_used_idx;
1447 int rte_vhost_set_vring_base(int vid, uint16_t queue_id,
1448 uint16_t last_avail_idx, uint16_t last_used_idx)
1450 struct vhost_virtqueue *vq;
1451 struct virtio_net *dev = get_device(vid);
1456 vq = dev->virtqueue[queue_id];
1460 if (vq_is_packed(dev)) {
1461 vq->last_avail_idx = last_avail_idx & 0x7fff;
1462 vq->avail_wrap_counter = !!(last_avail_idx & (1 << 15));
1463 vq->last_used_idx = last_used_idx & 0x7fff;
1464 vq->used_wrap_counter = !!(last_used_idx & (1 << 15));
1466 vq->last_avail_idx = last_avail_idx;
1467 vq->last_used_idx = last_used_idx;
1474 rte_vhost_get_vring_base_from_inflight(int vid,
1476 uint16_t *last_avail_idx,
1477 uint16_t *last_used_idx)
1479 struct rte_vhost_inflight_info_packed *inflight_info;
1480 struct virtio_net *dev = get_device(vid);
1482 if (dev == NULL || last_avail_idx == NULL || last_used_idx == NULL)
1485 if (!vq_is_packed(dev))
1488 inflight_info = dev->virtqueue[queue_id]->inflight_packed;
1492 *last_avail_idx = (inflight_info->old_used_wrap_counter << 15) |
1493 inflight_info->old_used_idx;
1494 *last_used_idx = *last_avail_idx;
1499 int rte_vhost_extern_callback_register(int vid,
1500 struct rte_vhost_user_extern_ops const * const ops, void *ctx)
1502 struct virtio_net *dev = get_device(vid);
1504 if (dev == NULL || ops == NULL)
1507 dev->extern_ops = *ops;
1508 dev->extern_data = ctx;
1512 RTE_LOG_REGISTER(vhost_config_log_level, lib.vhost.config, INFO);
1513 RTE_LOG_REGISTER(vhost_data_log_level, lib.vhost.data, WARNING);