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)) {
60 RTE_LOG(ERR, VHOST_CONFIG,
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) {
127 RTE_LOG(ERR, VHOST_CONFIG,
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) {
232 RTE_LOG(ERR, VHOST_CONFIG,
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]);
354 vring_translate_split(struct virtio_net *dev, struct vhost_virtqueue *vq)
356 uint64_t req_size, size;
358 req_size = sizeof(struct vring_desc) * vq->size;
360 vq->desc = (struct vring_desc *)(uintptr_t)vhost_iova_to_vva(dev, vq,
361 vq->ring_addrs.desc_user_addr,
362 &size, VHOST_ACCESS_RW);
363 if (!vq->desc || size != req_size)
366 req_size = sizeof(struct vring_avail);
367 req_size += sizeof(uint16_t) * vq->size;
368 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))
369 req_size += sizeof(uint16_t);
371 vq->avail = (struct vring_avail *)(uintptr_t)vhost_iova_to_vva(dev, vq,
372 vq->ring_addrs.avail_user_addr,
373 &size, VHOST_ACCESS_RW);
374 if (!vq->avail || size != req_size)
377 req_size = sizeof(struct vring_used);
378 req_size += sizeof(struct vring_used_elem) * vq->size;
379 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))
380 req_size += sizeof(uint16_t);
382 vq->used = (struct vring_used *)(uintptr_t)vhost_iova_to_vva(dev, vq,
383 vq->ring_addrs.used_user_addr,
384 &size, VHOST_ACCESS_RW);
385 if (!vq->used || size != req_size)
392 vring_translate_packed(struct virtio_net *dev, struct vhost_virtqueue *vq)
394 uint64_t req_size, size;
396 req_size = sizeof(struct vring_packed_desc) * vq->size;
398 vq->desc_packed = (struct vring_packed_desc *)(uintptr_t)
399 vhost_iova_to_vva(dev, vq, vq->ring_addrs.desc_user_addr,
400 &size, VHOST_ACCESS_RW);
401 if (!vq->desc_packed || size != req_size)
404 req_size = sizeof(struct vring_packed_desc_event);
406 vq->driver_event = (struct vring_packed_desc_event *)(uintptr_t)
407 vhost_iova_to_vva(dev, vq, vq->ring_addrs.avail_user_addr,
408 &size, VHOST_ACCESS_RW);
409 if (!vq->driver_event || size != req_size)
412 req_size = sizeof(struct vring_packed_desc_event);
414 vq->device_event = (struct vring_packed_desc_event *)(uintptr_t)
415 vhost_iova_to_vva(dev, vq, vq->ring_addrs.used_user_addr,
416 &size, VHOST_ACCESS_RW);
417 if (!vq->device_event || size != req_size)
424 vring_translate(struct virtio_net *dev, struct vhost_virtqueue *vq)
427 if (!(dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)))
430 if (vq_is_packed(dev)) {
431 if (vring_translate_packed(dev, vq) < 0)
434 if (vring_translate_split(dev, vq) < 0)
443 vring_invalidate(struct virtio_net *dev, struct vhost_virtqueue *vq)
445 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
446 vhost_user_iotlb_wr_lock(vq);
452 vq->log_guest_addr = 0;
454 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
455 vhost_user_iotlb_wr_unlock(vq);
459 init_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
461 struct vhost_virtqueue *vq;
463 if (vring_idx >= VHOST_MAX_VRING) {
464 RTE_LOG(ERR, VHOST_CONFIG,
465 "Failed not init vring, out of bound (%d)\n",
470 vq = dev->virtqueue[vring_idx];
472 memset(vq, 0, sizeof(struct vhost_virtqueue));
474 vq->kickfd = VIRTIO_UNINITIALIZED_EVENTFD;
475 vq->callfd = VIRTIO_UNINITIALIZED_EVENTFD;
477 vhost_user_iotlb_init(dev, vring_idx);
478 /* Backends are set to -1 indicating an inactive device. */
481 TAILQ_INIT(&vq->zmbuf_list);
485 reset_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
487 struct vhost_virtqueue *vq;
490 if (vring_idx >= VHOST_MAX_VRING) {
491 RTE_LOG(ERR, VHOST_CONFIG,
492 "Failed not init vring, out of bound (%d)\n",
497 vq = dev->virtqueue[vring_idx];
499 init_vring_queue(dev, vring_idx);
504 alloc_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
506 struct vhost_virtqueue *vq;
508 vq = rte_malloc(NULL, sizeof(struct vhost_virtqueue), 0);
510 RTE_LOG(ERR, VHOST_CONFIG,
511 "Failed to allocate memory for vring:%u.\n", vring_idx);
515 dev->virtqueue[vring_idx] = vq;
516 init_vring_queue(dev, vring_idx);
517 rte_spinlock_init(&vq->access_lock);
518 vq->avail_wrap_counter = 1;
519 vq->used_wrap_counter = 1;
520 vq->signalled_used_valid = false;
528 * Reset some variables in device structure, while keeping few
529 * others untouched, such as vid, ifname, nr_vring: they
530 * should be same unless the device is removed.
533 reset_device(struct virtio_net *dev)
538 dev->protocol_features = 0;
539 dev->flags &= VIRTIO_DEV_BUILTIN_VIRTIO_NET;
541 for (i = 0; i < dev->nr_vring; i++)
542 reset_vring_queue(dev, i);
546 * Invoked when there is a new vhost-user connection established (when
547 * there is a new virtio device being attached).
550 vhost_new_device(void)
552 struct virtio_net *dev;
555 for (i = 0; i < MAX_VHOST_DEVICE; i++) {
556 if (vhost_devices[i] == NULL)
560 if (i == MAX_VHOST_DEVICE) {
561 RTE_LOG(ERR, VHOST_CONFIG,
562 "Failed to find a free slot for new device.\n");
566 dev = rte_zmalloc(NULL, sizeof(struct virtio_net), 0);
568 RTE_LOG(ERR, VHOST_CONFIG,
569 "Failed to allocate memory for new dev.\n");
573 vhost_devices[i] = dev;
575 dev->flags = VIRTIO_DEV_BUILTIN_VIRTIO_NET;
576 dev->slave_req_fd = -1;
577 dev->vdpa_dev_id = -1;
578 dev->postcopy_ufd = -1;
579 rte_spinlock_init(&dev->slave_req_lock);
585 vhost_destroy_device_notify(struct virtio_net *dev)
587 struct rte_vdpa_device *vdpa_dev;
590 if (dev->flags & VIRTIO_DEV_RUNNING) {
591 did = dev->vdpa_dev_id;
592 vdpa_dev = rte_vdpa_get_device(did);
593 if (vdpa_dev && vdpa_dev->ops->dev_close)
594 vdpa_dev->ops->dev_close(dev->vid);
595 dev->flags &= ~VIRTIO_DEV_RUNNING;
596 dev->notify_ops->destroy_device(dev->vid);
601 * Invoked when there is the vhost-user connection is broken (when
602 * the virtio device is being detached).
605 vhost_destroy_device(int vid)
607 struct virtio_net *dev = get_device(vid);
612 vhost_destroy_device_notify(dev);
614 cleanup_device(dev, 1);
617 vhost_devices[vid] = NULL;
621 vhost_attach_vdpa_device(int vid, int did)
623 struct virtio_net *dev = get_device(vid);
628 if (rte_vdpa_get_device(did) == NULL)
631 dev->vdpa_dev_id = did;
635 vhost_set_ifname(int vid, const char *if_name, unsigned int if_len)
637 struct virtio_net *dev;
640 dev = get_device(vid);
644 len = if_len > sizeof(dev->ifname) ?
645 sizeof(dev->ifname) : if_len;
647 strncpy(dev->ifname, if_name, len);
648 dev->ifname[sizeof(dev->ifname) - 1] = '\0';
652 vhost_enable_dequeue_zero_copy(int vid)
654 struct virtio_net *dev = get_device(vid);
659 dev->dequeue_zero_copy = 1;
663 vhost_set_builtin_virtio_net(int vid, bool enable)
665 struct virtio_net *dev = get_device(vid);
671 dev->flags |= VIRTIO_DEV_BUILTIN_VIRTIO_NET;
673 dev->flags &= ~VIRTIO_DEV_BUILTIN_VIRTIO_NET;
677 vhost_enable_extbuf(int vid)
679 struct virtio_net *dev = get_device(vid);
688 vhost_enable_linearbuf(int vid)
690 struct virtio_net *dev = get_device(vid);
699 rte_vhost_get_mtu(int vid, uint16_t *mtu)
701 struct virtio_net *dev = get_device(vid);
703 if (dev == NULL || mtu == NULL)
706 if (!(dev->flags & VIRTIO_DEV_READY))
709 if (!(dev->features & (1ULL << VIRTIO_NET_F_MTU)))
718 rte_vhost_get_numa_node(int vid)
720 #ifdef RTE_LIBRTE_VHOST_NUMA
721 struct virtio_net *dev = get_device(vid);
725 if (dev == NULL || numa_available() != 0)
728 ret = get_mempolicy(&numa_node, NULL, 0, dev,
729 MPOL_F_NODE | MPOL_F_ADDR);
731 RTE_LOG(ERR, VHOST_CONFIG,
732 "(%d) failed to query numa node: %s\n",
733 vid, rte_strerror(errno));
745 rte_vhost_get_queue_num(int vid)
747 struct virtio_net *dev = get_device(vid);
752 return dev->nr_vring / 2;
756 rte_vhost_get_vring_num(int vid)
758 struct virtio_net *dev = get_device(vid);
763 return dev->nr_vring;
767 rte_vhost_get_ifname(int vid, char *buf, size_t len)
769 struct virtio_net *dev = get_device(vid);
771 if (dev == NULL || buf == NULL)
774 len = RTE_MIN(len, sizeof(dev->ifname));
776 strncpy(buf, dev->ifname, len);
783 rte_vhost_get_negotiated_features(int vid, uint64_t *features)
785 struct virtio_net *dev;
787 dev = get_device(vid);
788 if (dev == NULL || features == NULL)
791 *features = dev->features;
796 rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem)
798 struct virtio_net *dev;
799 struct rte_vhost_memory *m;
802 dev = get_device(vid);
803 if (dev == NULL || mem == NULL)
806 size = dev->mem->nregions * sizeof(struct rte_vhost_mem_region);
807 m = malloc(sizeof(struct rte_vhost_memory) + size);
811 m->nregions = dev->mem->nregions;
812 memcpy(m->regions, dev->mem->regions, size);
819 rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
820 struct rte_vhost_vring *vring)
822 struct virtio_net *dev;
823 struct vhost_virtqueue *vq;
825 dev = get_device(vid);
826 if (dev == NULL || vring == NULL)
829 if (vring_idx >= VHOST_MAX_VRING)
832 vq = dev->virtqueue[vring_idx];
836 if (vq_is_packed(dev)) {
837 vring->desc_packed = vq->desc_packed;
838 vring->driver_event = vq->driver_event;
839 vring->device_event = vq->device_event;
841 vring->desc = vq->desc;
842 vring->avail = vq->avail;
843 vring->used = vq->used;
845 vring->log_guest_addr = vq->log_guest_addr;
847 vring->callfd = vq->callfd;
848 vring->kickfd = vq->kickfd;
849 vring->size = vq->size;
855 rte_vhost_get_vhost_ring_inflight(int vid, uint16_t vring_idx,
856 struct rte_vhost_ring_inflight *vring)
858 struct virtio_net *dev;
859 struct vhost_virtqueue *vq;
861 dev = get_device(vid);
865 if (vring_idx >= VHOST_MAX_VRING)
868 vq = dev->virtqueue[vring_idx];
872 if (vq_is_packed(dev)) {
873 if (unlikely(!vq->inflight_packed))
876 vring->inflight_packed = vq->inflight_packed;
878 if (unlikely(!vq->inflight_split))
881 vring->inflight_split = vq->inflight_split;
884 vring->resubmit_inflight = vq->resubmit_inflight;
890 rte_vhost_set_inflight_desc_split(int vid, uint16_t vring_idx,
893 struct vhost_virtqueue *vq;
894 struct virtio_net *dev;
896 dev = get_device(vid);
900 if (unlikely(!(dev->protocol_features &
901 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
904 if (unlikely(vq_is_packed(dev)))
907 if (unlikely(vring_idx >= VHOST_MAX_VRING))
910 vq = dev->virtqueue[vring_idx];
914 if (unlikely(!vq->inflight_split))
917 if (unlikely(idx >= vq->size))
920 vq->inflight_split->desc[idx].counter = vq->global_counter++;
921 vq->inflight_split->desc[idx].inflight = 1;
926 rte_vhost_set_inflight_desc_packed(int vid, uint16_t vring_idx,
927 uint16_t head, uint16_t last,
928 uint16_t *inflight_entry)
930 struct rte_vhost_inflight_info_packed *inflight_info;
931 struct virtio_net *dev;
932 struct vhost_virtqueue *vq;
933 struct vring_packed_desc *desc;
934 uint16_t old_free_head, free_head;
936 dev = get_device(vid);
940 if (unlikely(!(dev->protocol_features &
941 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
944 if (unlikely(!vq_is_packed(dev)))
947 if (unlikely(vring_idx >= VHOST_MAX_VRING))
950 vq = dev->virtqueue[vring_idx];
954 inflight_info = vq->inflight_packed;
955 if (unlikely(!inflight_info))
958 if (unlikely(head >= vq->size))
961 desc = vq->desc_packed;
962 old_free_head = inflight_info->old_free_head;
963 if (unlikely(old_free_head >= vq->size))
966 free_head = old_free_head;
968 /* init header descriptor */
969 inflight_info->desc[old_free_head].num = 0;
970 inflight_info->desc[old_free_head].counter = vq->global_counter++;
971 inflight_info->desc[old_free_head].inflight = 1;
973 /* save desc entry in flight entry */
974 while (head != ((last + 1) % vq->size)) {
975 inflight_info->desc[old_free_head].num++;
976 inflight_info->desc[free_head].addr = desc[head].addr;
977 inflight_info->desc[free_head].len = desc[head].len;
978 inflight_info->desc[free_head].flags = desc[head].flags;
979 inflight_info->desc[free_head].id = desc[head].id;
981 inflight_info->desc[old_free_head].last = free_head;
982 free_head = inflight_info->desc[free_head].next;
983 inflight_info->free_head = free_head;
984 head = (head + 1) % vq->size;
987 inflight_info->old_free_head = free_head;
988 *inflight_entry = old_free_head;
994 rte_vhost_clr_inflight_desc_split(int vid, uint16_t vring_idx,
995 uint16_t last_used_idx, uint16_t idx)
997 struct virtio_net *dev;
998 struct vhost_virtqueue *vq;
1000 dev = get_device(vid);
1004 if (unlikely(!(dev->protocol_features &
1005 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1008 if (unlikely(vq_is_packed(dev)))
1011 if (unlikely(vring_idx >= VHOST_MAX_VRING))
1014 vq = dev->virtqueue[vring_idx];
1018 if (unlikely(!vq->inflight_split))
1021 if (unlikely(idx >= vq->size))
1026 vq->inflight_split->desc[idx].inflight = 0;
1030 vq->inflight_split->used_idx = last_used_idx;
1035 rte_vhost_clr_inflight_desc_packed(int vid, uint16_t vring_idx,
1038 struct rte_vhost_inflight_info_packed *inflight_info;
1039 struct virtio_net *dev;
1040 struct vhost_virtqueue *vq;
1042 dev = get_device(vid);
1046 if (unlikely(!(dev->protocol_features &
1047 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1050 if (unlikely(!vq_is_packed(dev)))
1053 if (unlikely(vring_idx >= VHOST_MAX_VRING))
1056 vq = dev->virtqueue[vring_idx];
1060 inflight_info = vq->inflight_packed;
1061 if (unlikely(!inflight_info))
1064 if (unlikely(head >= vq->size))
1069 inflight_info->desc[head].inflight = 0;
1073 inflight_info->old_free_head = inflight_info->free_head;
1074 inflight_info->old_used_idx = inflight_info->used_idx;
1075 inflight_info->old_used_wrap_counter = inflight_info->used_wrap_counter;
1081 rte_vhost_set_last_inflight_io_split(int vid, uint16_t vring_idx,
1084 struct virtio_net *dev;
1085 struct vhost_virtqueue *vq;
1087 dev = get_device(vid);
1091 if (unlikely(!(dev->protocol_features &
1092 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1095 if (unlikely(vq_is_packed(dev)))
1098 if (unlikely(vring_idx >= VHOST_MAX_VRING))
1101 vq = dev->virtqueue[vring_idx];
1105 if (unlikely(!vq->inflight_split))
1108 vq->inflight_split->last_inflight_io = idx;
1113 rte_vhost_set_last_inflight_io_packed(int vid, uint16_t vring_idx,
1116 struct rte_vhost_inflight_info_packed *inflight_info;
1117 struct virtio_net *dev;
1118 struct vhost_virtqueue *vq;
1121 dev = get_device(vid);
1125 if (unlikely(!(dev->protocol_features &
1126 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1129 if (unlikely(!vq_is_packed(dev)))
1132 if (unlikely(vring_idx >= VHOST_MAX_VRING))
1135 vq = dev->virtqueue[vring_idx];
1139 inflight_info = vq->inflight_packed;
1140 if (unlikely(!inflight_info))
1143 if (unlikely(head >= vq->size))
1146 last = inflight_info->desc[head].last;
1147 if (unlikely(last >= vq->size))
1150 inflight_info->desc[last].next = inflight_info->free_head;
1151 inflight_info->free_head = head;
1152 inflight_info->used_idx += inflight_info->desc[head].num;
1153 if (inflight_info->used_idx >= inflight_info->desc_num) {
1154 inflight_info->used_idx -= inflight_info->desc_num;
1155 inflight_info->used_wrap_counter =
1156 !inflight_info->used_wrap_counter;
1163 rte_vhost_vring_call(int vid, uint16_t vring_idx)
1165 struct virtio_net *dev;
1166 struct vhost_virtqueue *vq;
1168 dev = get_device(vid);
1172 if (vring_idx >= VHOST_MAX_VRING)
1175 vq = dev->virtqueue[vring_idx];
1179 if (vq_is_packed(dev))
1180 vhost_vring_call_packed(dev, vq);
1182 vhost_vring_call_split(dev, vq);
1188 rte_vhost_avail_entries(int vid, uint16_t queue_id)
1190 struct virtio_net *dev;
1191 struct vhost_virtqueue *vq;
1194 dev = get_device(vid);
1198 vq = dev->virtqueue[queue_id];
1200 rte_spinlock_lock(&vq->access_lock);
1202 if (unlikely(!vq->enabled || vq->avail == NULL))
1205 ret = *(volatile uint16_t *)&vq->avail->idx - vq->last_used_idx;
1208 rte_spinlock_unlock(&vq->access_lock);
1213 vhost_enable_notify_split(struct virtio_net *dev,
1214 struct vhost_virtqueue *vq, int enable)
1216 if (vq->used == NULL)
1219 if (!(dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))) {
1221 vq->used->flags &= ~VRING_USED_F_NO_NOTIFY;
1223 vq->used->flags |= VRING_USED_F_NO_NOTIFY;
1226 vhost_avail_event(vq) = vq->last_avail_idx;
1232 vhost_enable_notify_packed(struct virtio_net *dev,
1233 struct vhost_virtqueue *vq, int enable)
1237 if (vq->device_event == NULL)
1241 vq->device_event->flags = VRING_EVENT_F_DISABLE;
1245 flags = VRING_EVENT_F_ENABLE;
1246 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX)) {
1247 flags = VRING_EVENT_F_DESC;
1248 vq->device_event->off_wrap = vq->last_avail_idx |
1249 vq->avail_wrap_counter << 15;
1254 vq->device_event->flags = flags;
1259 rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable)
1261 struct virtio_net *dev = get_device(vid);
1262 struct vhost_virtqueue *vq;
1268 vq = dev->virtqueue[queue_id];
1270 rte_spinlock_lock(&vq->access_lock);
1272 if (vq_is_packed(dev))
1273 ret = vhost_enable_notify_packed(dev, vq, enable);
1275 ret = vhost_enable_notify_split(dev, vq, enable);
1277 rte_spinlock_unlock(&vq->access_lock);
1283 rte_vhost_log_write(int vid, uint64_t addr, uint64_t len)
1285 struct virtio_net *dev = get_device(vid);
1290 vhost_log_write(dev, addr, len);
1294 rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
1295 uint64_t offset, uint64_t len)
1297 struct virtio_net *dev;
1298 struct vhost_virtqueue *vq;
1300 dev = get_device(vid);
1304 if (vring_idx >= VHOST_MAX_VRING)
1306 vq = dev->virtqueue[vring_idx];
1310 vhost_log_used_vring(dev, vq, offset, len);
1314 rte_vhost_rx_queue_count(int vid, uint16_t qid)
1316 struct virtio_net *dev;
1317 struct vhost_virtqueue *vq;
1320 dev = get_device(vid);
1324 if (unlikely(qid >= dev->nr_vring || (qid & 1) == 0)) {
1325 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
1326 dev->vid, __func__, qid);
1330 vq = dev->virtqueue[qid];
1334 rte_spinlock_lock(&vq->access_lock);
1336 if (unlikely(vq->enabled == 0 || vq->avail == NULL))
1339 ret = *((volatile uint16_t *)&vq->avail->idx) - vq->last_avail_idx;
1342 rte_spinlock_unlock(&vq->access_lock);
1346 int rte_vhost_get_vdpa_device_id(int vid)
1348 struct virtio_net *dev = get_device(vid);
1353 return dev->vdpa_dev_id;
1356 int rte_vhost_get_log_base(int vid, uint64_t *log_base,
1359 struct virtio_net *dev = get_device(vid);
1361 if (dev == NULL || log_base == NULL || log_size == NULL)
1364 *log_base = dev->log_base;
1365 *log_size = dev->log_size;
1370 int rte_vhost_get_vring_base(int vid, uint16_t queue_id,
1371 uint16_t *last_avail_idx, uint16_t *last_used_idx)
1373 struct vhost_virtqueue *vq;
1374 struct virtio_net *dev = get_device(vid);
1376 if (dev == NULL || last_avail_idx == NULL || last_used_idx == NULL)
1379 vq = dev->virtqueue[queue_id];
1383 if (vq_is_packed(dev)) {
1384 *last_avail_idx = (vq->avail_wrap_counter << 15) |
1386 *last_used_idx = (vq->used_wrap_counter << 15) |
1389 *last_avail_idx = vq->last_avail_idx;
1390 *last_used_idx = vq->last_used_idx;
1396 int rte_vhost_set_vring_base(int vid, uint16_t queue_id,
1397 uint16_t last_avail_idx, uint16_t last_used_idx)
1399 struct vhost_virtqueue *vq;
1400 struct virtio_net *dev = get_device(vid);
1405 vq = dev->virtqueue[queue_id];
1409 if (vq_is_packed(dev)) {
1410 vq->last_avail_idx = last_avail_idx & 0x7fff;
1411 vq->avail_wrap_counter = !!(last_avail_idx & (1 << 15));
1412 vq->last_used_idx = last_used_idx & 0x7fff;
1413 vq->used_wrap_counter = !!(last_used_idx & (1 << 15));
1415 vq->last_avail_idx = last_avail_idx;
1416 vq->last_used_idx = last_used_idx;
1423 rte_vhost_get_vring_base_from_inflight(int vid,
1425 uint16_t *last_avail_idx,
1426 uint16_t *last_used_idx)
1428 struct rte_vhost_inflight_info_packed *inflight_info;
1429 struct virtio_net *dev = get_device(vid);
1431 if (dev == NULL || last_avail_idx == NULL || last_used_idx == NULL)
1434 if (!vq_is_packed(dev))
1437 inflight_info = dev->virtqueue[queue_id]->inflight_packed;
1441 *last_avail_idx = (inflight_info->old_used_wrap_counter << 15) |
1442 inflight_info->old_used_idx;
1443 *last_used_idx = *last_avail_idx;
1448 int rte_vhost_extern_callback_register(int vid,
1449 struct rte_vhost_user_extern_ops const * const ops, void *ctx)
1451 struct virtio_net *dev = get_device(vid);
1453 if (dev == NULL || ops == NULL)
1456 dev->extern_ops = *ops;
1457 dev->extern_data = ctx;