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 int vhost_config_log_level;
31 int vhost_data_log_level;
33 /* Called with iotlb_lock read-locked */
35 __vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
36 uint64_t iova, uint64_t *size, uint8_t perm)
38 uint64_t vva, tmp_size;
45 vva = vhost_user_iotlb_cache_find(vq, iova, &tmp_size, perm);
46 if (tmp_size == *size)
51 if (!vhost_user_iotlb_pending_miss(vq, iova, perm)) {
53 * iotlb_lock is read-locked for a full burst,
54 * but it only protects the iotlb cache.
55 * In case of IOTLB miss, we might block on the socket,
56 * which could cause a deadlock with QEMU if an IOTLB update
57 * is being handled. We can safely unlock here to avoid it.
59 vhost_user_iotlb_rd_unlock(vq);
61 vhost_user_iotlb_pending_insert(vq, iova, perm);
62 if (vhost_user_iotlb_miss(dev, iova, perm)) {
64 "IOTLB miss req failed for IOVA 0x%" PRIx64 "\n",
66 vhost_user_iotlb_pending_remove(vq, iova, 1, perm);
69 vhost_user_iotlb_rd_lock(vq);
75 #define VHOST_LOG_PAGE 4096
78 * Atomically set a bit in memory.
80 static __rte_always_inline void
81 vhost_set_bit(unsigned int nr, volatile uint8_t *addr)
83 #if defined(RTE_TOOLCHAIN_GCC) && (GCC_VERSION < 70100)
85 * __sync_ built-ins are deprecated, but __atomic_ ones
86 * are sub-optimized in older GCC versions.
88 __sync_fetch_and_or_1(addr, (1U << nr));
90 __atomic_fetch_or(addr, (1U << nr), __ATOMIC_RELAXED);
94 static __rte_always_inline void
95 vhost_log_page(uint8_t *log_base, uint64_t page)
97 vhost_set_bit(page % 8, &log_base[page / 8]);
101 __vhost_log_write(struct virtio_net *dev, uint64_t addr, uint64_t len)
105 if (unlikely(!dev->log_base || !len))
108 if (unlikely(dev->log_size <= ((addr + len - 1) / VHOST_LOG_PAGE / 8)))
111 /* To make sure guest memory updates are committed before logging */
114 page = addr / VHOST_LOG_PAGE;
115 while (page * VHOST_LOG_PAGE < addr + len) {
116 vhost_log_page((uint8_t *)(uintptr_t)dev->log_base, page);
122 __vhost_log_write_iova(struct virtio_net *dev, struct vhost_virtqueue *vq,
123 uint64_t iova, uint64_t len)
125 uint64_t hva, gpa, map_len;
128 hva = __vhost_iova_to_vva(dev, vq, iova, &map_len, VHOST_ACCESS_RW);
129 if (map_len != len) {
131 "Failed to write log for IOVA 0x%" PRIx64 ". No IOTLB entry found\n",
136 gpa = hva_to_gpa(dev, hva, len);
138 __vhost_log_write(dev, gpa, len);
142 __vhost_log_cache_sync(struct virtio_net *dev, struct vhost_virtqueue *vq)
144 unsigned long *log_base;
147 if (unlikely(!dev->log_base))
152 log_base = (unsigned long *)(uintptr_t)dev->log_base;
154 for (i = 0; i < vq->log_cache_nb_elem; i++) {
155 struct log_cache_entry *elem = vq->log_cache + i;
157 #if defined(RTE_TOOLCHAIN_GCC) && (GCC_VERSION < 70100)
159 * '__sync' builtins are deprecated, but '__atomic' ones
160 * are sub-optimized in older GCC versions.
162 __sync_fetch_and_or(log_base + elem->offset, elem->val);
164 __atomic_fetch_or(log_base + elem->offset, elem->val,
171 vq->log_cache_nb_elem = 0;
174 static __rte_always_inline void
175 vhost_log_cache_page(struct virtio_net *dev, struct vhost_virtqueue *vq,
178 uint32_t bit_nr = page % (sizeof(unsigned long) << 3);
179 uint32_t offset = page / (sizeof(unsigned long) << 3);
182 for (i = 0; i < vq->log_cache_nb_elem; i++) {
183 struct log_cache_entry *elem = vq->log_cache + i;
185 if (elem->offset == offset) {
186 elem->val |= (1UL << bit_nr);
191 if (unlikely(i >= VHOST_LOG_CACHE_NR)) {
193 * No more room for a new log cache entry,
194 * so write the dirty log map directly.
197 vhost_log_page((uint8_t *)(uintptr_t)dev->log_base, page);
202 vq->log_cache[i].offset = offset;
203 vq->log_cache[i].val = (1UL << bit_nr);
204 vq->log_cache_nb_elem++;
208 __vhost_log_cache_write(struct virtio_net *dev, struct vhost_virtqueue *vq,
209 uint64_t addr, uint64_t len)
213 if (unlikely(!dev->log_base || !len))
216 if (unlikely(dev->log_size <= ((addr + len - 1) / VHOST_LOG_PAGE / 8)))
219 page = addr / VHOST_LOG_PAGE;
220 while (page * VHOST_LOG_PAGE < addr + len) {
221 vhost_log_cache_page(dev, vq, page);
227 __vhost_log_cache_write_iova(struct virtio_net *dev, struct vhost_virtqueue *vq,
228 uint64_t iova, uint64_t len)
230 uint64_t hva, gpa, map_len;
233 hva = __vhost_iova_to_vva(dev, vq, iova, &map_len, VHOST_ACCESS_RW);
234 if (map_len != len) {
236 "Failed to write log for IOVA 0x%" PRIx64 ". No IOTLB entry found\n",
241 gpa = hva_to_gpa(dev, hva, len);
243 __vhost_log_cache_write(dev, vq, gpa, len);
247 vhost_alloc_copy_ind_table(struct virtio_net *dev, struct vhost_virtqueue *vq,
248 uint64_t desc_addr, uint64_t desc_len)
252 uint64_t len, remain = desc_len;
254 idesc = rte_malloc(__func__, desc_len, 0);
255 if (unlikely(!idesc))
258 dst = (uint64_t)(uintptr_t)idesc;
262 src = vhost_iova_to_vva(dev, vq, desc_addr, &len,
264 if (unlikely(!src || !len)) {
269 rte_memcpy((void *)(uintptr_t)dst, (void *)(uintptr_t)src, len);
280 cleanup_vq(struct vhost_virtqueue *vq, int destroy)
282 if ((vq->callfd >= 0) && (destroy != 0))
289 cleanup_vq_inflight(struct virtio_net *dev, struct vhost_virtqueue *vq)
291 if (!(dev->protocol_features &
292 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD)))
295 if (vq_is_packed(dev)) {
296 if (vq->inflight_packed)
297 vq->inflight_packed = NULL;
299 if (vq->inflight_split)
300 vq->inflight_split = NULL;
303 if (vq->resubmit_inflight) {
304 if (vq->resubmit_inflight->resubmit_list) {
305 free(vq->resubmit_inflight->resubmit_list);
306 vq->resubmit_inflight->resubmit_list = NULL;
308 free(vq->resubmit_inflight);
309 vq->resubmit_inflight = NULL;
314 * Unmap any memory, close any file descriptors and
315 * free any memory owned by a device.
318 cleanup_device(struct virtio_net *dev, int destroy)
322 vhost_backend_cleanup(dev);
324 for (i = 0; i < dev->nr_vring; i++) {
325 cleanup_vq(dev->virtqueue[i], destroy);
326 cleanup_vq_inflight(dev, dev->virtqueue[i]);
331 free_vq(struct virtio_net *dev, struct vhost_virtqueue *vq)
333 if (vq_is_packed(dev))
334 rte_free(vq->shadow_used_packed);
336 rte_free(vq->shadow_used_split);
337 rte_free(vq->batch_copy_elems);
338 rte_mempool_free(vq->iotlb_pool);
343 * Release virtqueues and device memory.
346 free_device(struct virtio_net *dev)
350 for (i = 0; i < dev->nr_vring; i++)
351 free_vq(dev, dev->virtqueue[i]);
356 static __rte_always_inline int
357 log_translate(struct virtio_net *dev, struct vhost_virtqueue *vq)
359 if (likely(!(vq->ring_addrs.flags & (1 << VHOST_VRING_F_LOG))))
362 vq->log_guest_addr = translate_log_addr(dev, vq,
363 vq->ring_addrs.log_guest_addr);
364 if (vq->log_guest_addr == 0)
371 * Converts vring log address to GPA
372 * If IOMMU is enabled, the log address is IOVA
373 * If IOMMU not enabled, the log address is already GPA
375 * Caller should have iotlb_lock read-locked
378 translate_log_addr(struct virtio_net *dev, struct vhost_virtqueue *vq,
381 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)) {
382 const uint64_t exp_size = sizeof(uint64_t);
384 uint64_t size = exp_size;
386 hva = vhost_iova_to_vva(dev, vq, log_addr,
387 &size, VHOST_ACCESS_RW);
389 if (size != exp_size)
392 gpa = hva_to_gpa(dev, hva, exp_size);
394 VHOST_LOG_CONFIG(ERR,
395 "VQ: Failed to find GPA for log_addr: 0x%"
396 PRIx64 " hva: 0x%" PRIx64 "\n",
406 /* Caller should have iotlb_lock read-locked */
408 vring_translate_split(struct virtio_net *dev, struct vhost_virtqueue *vq)
410 uint64_t req_size, size;
412 req_size = sizeof(struct vring_desc) * vq->size;
414 vq->desc = (struct vring_desc *)(uintptr_t)vhost_iova_to_vva(dev, vq,
415 vq->ring_addrs.desc_user_addr,
416 &size, VHOST_ACCESS_RW);
417 if (!vq->desc || size != req_size)
420 req_size = sizeof(struct vring_avail);
421 req_size += sizeof(uint16_t) * vq->size;
422 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))
423 req_size += sizeof(uint16_t);
425 vq->avail = (struct vring_avail *)(uintptr_t)vhost_iova_to_vva(dev, vq,
426 vq->ring_addrs.avail_user_addr,
427 &size, VHOST_ACCESS_RW);
428 if (!vq->avail || size != req_size)
431 req_size = sizeof(struct vring_used);
432 req_size += sizeof(struct vring_used_elem) * vq->size;
433 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))
434 req_size += sizeof(uint16_t);
436 vq->used = (struct vring_used *)(uintptr_t)vhost_iova_to_vva(dev, vq,
437 vq->ring_addrs.used_user_addr,
438 &size, VHOST_ACCESS_RW);
439 if (!vq->used || size != req_size)
445 /* Caller should have iotlb_lock read-locked */
447 vring_translate_packed(struct virtio_net *dev, struct vhost_virtqueue *vq)
449 uint64_t req_size, size;
451 req_size = sizeof(struct vring_packed_desc) * vq->size;
453 vq->desc_packed = (struct vring_packed_desc *)(uintptr_t)
454 vhost_iova_to_vva(dev, vq, vq->ring_addrs.desc_user_addr,
455 &size, VHOST_ACCESS_RW);
456 if (!vq->desc_packed || size != req_size)
459 req_size = sizeof(struct vring_packed_desc_event);
461 vq->driver_event = (struct vring_packed_desc_event *)(uintptr_t)
462 vhost_iova_to_vva(dev, vq, vq->ring_addrs.avail_user_addr,
463 &size, VHOST_ACCESS_RW);
464 if (!vq->driver_event || size != req_size)
467 req_size = sizeof(struct vring_packed_desc_event);
469 vq->device_event = (struct vring_packed_desc_event *)(uintptr_t)
470 vhost_iova_to_vva(dev, vq, vq->ring_addrs.used_user_addr,
471 &size, VHOST_ACCESS_RW);
472 if (!vq->device_event || size != req_size)
479 vring_translate(struct virtio_net *dev, struct vhost_virtqueue *vq)
482 if (!(dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)))
485 if (vq_is_packed(dev)) {
486 if (vring_translate_packed(dev, vq) < 0)
489 if (vring_translate_split(dev, vq) < 0)
493 if (log_translate(dev, vq) < 0)
502 vring_invalidate(struct virtio_net *dev, struct vhost_virtqueue *vq)
504 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
505 vhost_user_iotlb_wr_lock(vq);
511 vq->log_guest_addr = 0;
513 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
514 vhost_user_iotlb_wr_unlock(vq);
518 init_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
520 struct vhost_virtqueue *vq;
522 if (vring_idx >= VHOST_MAX_VRING) {
523 VHOST_LOG_CONFIG(ERR,
524 "Failed not init vring, out of bound (%d)\n",
529 vq = dev->virtqueue[vring_idx];
531 memset(vq, 0, sizeof(struct vhost_virtqueue));
533 vq->kickfd = VIRTIO_UNINITIALIZED_EVENTFD;
534 vq->callfd = VIRTIO_UNINITIALIZED_EVENTFD;
536 vhost_user_iotlb_init(dev, vring_idx);
537 /* Backends are set to -1 indicating an inactive device. */
540 TAILQ_INIT(&vq->zmbuf_list);
544 reset_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
546 struct vhost_virtqueue *vq;
549 if (vring_idx >= VHOST_MAX_VRING) {
550 VHOST_LOG_CONFIG(ERR,
551 "Failed not init vring, out of bound (%d)\n",
556 vq = dev->virtqueue[vring_idx];
558 init_vring_queue(dev, vring_idx);
563 alloc_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
565 struct vhost_virtqueue *vq;
567 vq = rte_malloc(NULL, sizeof(struct vhost_virtqueue), 0);
569 VHOST_LOG_CONFIG(ERR,
570 "Failed to allocate memory for vring:%u.\n", vring_idx);
574 dev->virtqueue[vring_idx] = vq;
575 init_vring_queue(dev, vring_idx);
576 rte_spinlock_init(&vq->access_lock);
577 vq->avail_wrap_counter = 1;
578 vq->used_wrap_counter = 1;
579 vq->signalled_used_valid = false;
587 * Reset some variables in device structure, while keeping few
588 * others untouched, such as vid, ifname, nr_vring: they
589 * should be same unless the device is removed.
592 reset_device(struct virtio_net *dev)
597 dev->protocol_features = 0;
598 dev->flags &= VIRTIO_DEV_BUILTIN_VIRTIO_NET;
600 for (i = 0; i < dev->nr_vring; i++)
601 reset_vring_queue(dev, i);
605 * Invoked when there is a new vhost-user connection established (when
606 * there is a new virtio device being attached).
609 vhost_new_device(void)
611 struct virtio_net *dev;
614 for (i = 0; i < MAX_VHOST_DEVICE; i++) {
615 if (vhost_devices[i] == NULL)
619 if (i == MAX_VHOST_DEVICE) {
620 VHOST_LOG_CONFIG(ERR,
621 "Failed to find a free slot for new device.\n");
625 dev = rte_zmalloc(NULL, sizeof(struct virtio_net), 0);
627 VHOST_LOG_CONFIG(ERR,
628 "Failed to allocate memory for new dev.\n");
632 vhost_devices[i] = dev;
634 dev->flags = VIRTIO_DEV_BUILTIN_VIRTIO_NET;
635 dev->slave_req_fd = -1;
636 dev->vdpa_dev_id = -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;
649 if (dev->flags & VIRTIO_DEV_RUNNING) {
650 did = dev->vdpa_dev_id;
651 vdpa_dev = rte_vdpa_get_device(did);
652 if (vdpa_dev && vdpa_dev->ops->dev_close)
653 vdpa_dev->ops->dev_close(dev->vid);
654 dev->flags &= ~VIRTIO_DEV_RUNNING;
655 dev->notify_ops->destroy_device(dev->vid);
660 * Invoked when there is the vhost-user connection is broken (when
661 * the virtio device is being detached).
664 vhost_destroy_device(int vid)
666 struct virtio_net *dev = get_device(vid);
671 vhost_destroy_device_notify(dev);
673 cleanup_device(dev, 1);
676 vhost_devices[vid] = NULL;
680 vhost_attach_vdpa_device(int vid, int did)
682 struct virtio_net *dev = get_device(vid);
687 if (rte_vdpa_get_device(did) == NULL)
690 dev->vdpa_dev_id = did;
694 vhost_set_ifname(int vid, const char *if_name, unsigned int if_len)
696 struct virtio_net *dev;
699 dev = get_device(vid);
703 len = if_len > sizeof(dev->ifname) ?
704 sizeof(dev->ifname) : if_len;
706 strncpy(dev->ifname, if_name, len);
707 dev->ifname[sizeof(dev->ifname) - 1] = '\0';
711 vhost_enable_dequeue_zero_copy(int vid)
713 struct virtio_net *dev = get_device(vid);
718 dev->dequeue_zero_copy = 1;
722 vhost_set_builtin_virtio_net(int vid, bool enable)
724 struct virtio_net *dev = get_device(vid);
730 dev->flags |= VIRTIO_DEV_BUILTIN_VIRTIO_NET;
732 dev->flags &= ~VIRTIO_DEV_BUILTIN_VIRTIO_NET;
736 vhost_enable_extbuf(int vid)
738 struct virtio_net *dev = get_device(vid);
747 vhost_enable_linearbuf(int vid)
749 struct virtio_net *dev = get_device(vid);
758 rte_vhost_get_mtu(int vid, uint16_t *mtu)
760 struct virtio_net *dev = get_device(vid);
762 if (dev == NULL || mtu == NULL)
765 if (!(dev->flags & VIRTIO_DEV_READY))
768 if (!(dev->features & (1ULL << VIRTIO_NET_F_MTU)))
777 rte_vhost_get_numa_node(int vid)
779 #ifdef RTE_LIBRTE_VHOST_NUMA
780 struct virtio_net *dev = get_device(vid);
784 if (dev == NULL || numa_available() != 0)
787 ret = get_mempolicy(&numa_node, NULL, 0, dev,
788 MPOL_F_NODE | MPOL_F_ADDR);
790 VHOST_LOG_CONFIG(ERR,
791 "(%d) failed to query numa node: %s\n",
792 vid, rte_strerror(errno));
804 rte_vhost_get_queue_num(int vid)
806 struct virtio_net *dev = get_device(vid);
811 return dev->nr_vring / 2;
815 rte_vhost_get_vring_num(int vid)
817 struct virtio_net *dev = get_device(vid);
822 return dev->nr_vring;
826 rte_vhost_get_ifname(int vid, char *buf, size_t len)
828 struct virtio_net *dev = get_device(vid);
830 if (dev == NULL || buf == NULL)
833 len = RTE_MIN(len, sizeof(dev->ifname));
835 strncpy(buf, dev->ifname, len);
842 rte_vhost_get_negotiated_features(int vid, uint64_t *features)
844 struct virtio_net *dev;
846 dev = get_device(vid);
847 if (dev == NULL || features == NULL)
850 *features = dev->features;
855 rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem)
857 struct virtio_net *dev;
858 struct rte_vhost_memory *m;
861 dev = get_device(vid);
862 if (dev == NULL || mem == NULL)
865 size = dev->mem->nregions * sizeof(struct rte_vhost_mem_region);
866 m = malloc(sizeof(struct rte_vhost_memory) + size);
870 m->nregions = dev->mem->nregions;
871 memcpy(m->regions, dev->mem->regions, size);
878 rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
879 struct rte_vhost_vring *vring)
881 struct virtio_net *dev;
882 struct vhost_virtqueue *vq;
884 dev = get_device(vid);
885 if (dev == NULL || vring == NULL)
888 if (vring_idx >= VHOST_MAX_VRING)
891 vq = dev->virtqueue[vring_idx];
895 if (vq_is_packed(dev)) {
896 vring->desc_packed = vq->desc_packed;
897 vring->driver_event = vq->driver_event;
898 vring->device_event = vq->device_event;
900 vring->desc = vq->desc;
901 vring->avail = vq->avail;
902 vring->used = vq->used;
904 vring->log_guest_addr = vq->log_guest_addr;
906 vring->callfd = vq->callfd;
907 vring->kickfd = vq->kickfd;
908 vring->size = vq->size;
914 rte_vhost_get_vhost_ring_inflight(int vid, uint16_t vring_idx,
915 struct rte_vhost_ring_inflight *vring)
917 struct virtio_net *dev;
918 struct vhost_virtqueue *vq;
920 dev = get_device(vid);
924 if (vring_idx >= VHOST_MAX_VRING)
927 vq = dev->virtqueue[vring_idx];
931 if (vq_is_packed(dev)) {
932 if (unlikely(!vq->inflight_packed))
935 vring->inflight_packed = vq->inflight_packed;
937 if (unlikely(!vq->inflight_split))
940 vring->inflight_split = vq->inflight_split;
943 vring->resubmit_inflight = vq->resubmit_inflight;
949 rte_vhost_set_inflight_desc_split(int vid, uint16_t vring_idx,
952 struct vhost_virtqueue *vq;
953 struct virtio_net *dev;
955 dev = get_device(vid);
959 if (unlikely(!(dev->protocol_features &
960 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
963 if (unlikely(vq_is_packed(dev)))
966 if (unlikely(vring_idx >= VHOST_MAX_VRING))
969 vq = dev->virtqueue[vring_idx];
973 if (unlikely(!vq->inflight_split))
976 if (unlikely(idx >= vq->size))
979 vq->inflight_split->desc[idx].counter = vq->global_counter++;
980 vq->inflight_split->desc[idx].inflight = 1;
985 rte_vhost_set_inflight_desc_packed(int vid, uint16_t vring_idx,
986 uint16_t head, uint16_t last,
987 uint16_t *inflight_entry)
989 struct rte_vhost_inflight_info_packed *inflight_info;
990 struct virtio_net *dev;
991 struct vhost_virtqueue *vq;
992 struct vring_packed_desc *desc;
993 uint16_t old_free_head, free_head;
995 dev = get_device(vid);
999 if (unlikely(!(dev->protocol_features &
1000 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1003 if (unlikely(!vq_is_packed(dev)))
1006 if (unlikely(vring_idx >= VHOST_MAX_VRING))
1009 vq = dev->virtqueue[vring_idx];
1013 inflight_info = vq->inflight_packed;
1014 if (unlikely(!inflight_info))
1017 if (unlikely(head >= vq->size))
1020 desc = vq->desc_packed;
1021 old_free_head = inflight_info->old_free_head;
1022 if (unlikely(old_free_head >= vq->size))
1025 free_head = old_free_head;
1027 /* init header descriptor */
1028 inflight_info->desc[old_free_head].num = 0;
1029 inflight_info->desc[old_free_head].counter = vq->global_counter++;
1030 inflight_info->desc[old_free_head].inflight = 1;
1032 /* save desc entry in flight entry */
1033 while (head != ((last + 1) % vq->size)) {
1034 inflight_info->desc[old_free_head].num++;
1035 inflight_info->desc[free_head].addr = desc[head].addr;
1036 inflight_info->desc[free_head].len = desc[head].len;
1037 inflight_info->desc[free_head].flags = desc[head].flags;
1038 inflight_info->desc[free_head].id = desc[head].id;
1040 inflight_info->desc[old_free_head].last = free_head;
1041 free_head = inflight_info->desc[free_head].next;
1042 inflight_info->free_head = free_head;
1043 head = (head + 1) % vq->size;
1046 inflight_info->old_free_head = free_head;
1047 *inflight_entry = old_free_head;
1053 rte_vhost_clr_inflight_desc_split(int vid, uint16_t vring_idx,
1054 uint16_t last_used_idx, uint16_t idx)
1056 struct virtio_net *dev;
1057 struct vhost_virtqueue *vq;
1059 dev = get_device(vid);
1063 if (unlikely(!(dev->protocol_features &
1064 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1067 if (unlikely(vq_is_packed(dev)))
1070 if (unlikely(vring_idx >= VHOST_MAX_VRING))
1073 vq = dev->virtqueue[vring_idx];
1077 if (unlikely(!vq->inflight_split))
1080 if (unlikely(idx >= vq->size))
1085 vq->inflight_split->desc[idx].inflight = 0;
1089 vq->inflight_split->used_idx = last_used_idx;
1094 rte_vhost_clr_inflight_desc_packed(int vid, uint16_t vring_idx,
1097 struct rte_vhost_inflight_info_packed *inflight_info;
1098 struct virtio_net *dev;
1099 struct vhost_virtqueue *vq;
1101 dev = get_device(vid);
1105 if (unlikely(!(dev->protocol_features &
1106 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1109 if (unlikely(!vq_is_packed(dev)))
1112 if (unlikely(vring_idx >= VHOST_MAX_VRING))
1115 vq = dev->virtqueue[vring_idx];
1119 inflight_info = vq->inflight_packed;
1120 if (unlikely(!inflight_info))
1123 if (unlikely(head >= vq->size))
1128 inflight_info->desc[head].inflight = 0;
1132 inflight_info->old_free_head = inflight_info->free_head;
1133 inflight_info->old_used_idx = inflight_info->used_idx;
1134 inflight_info->old_used_wrap_counter = inflight_info->used_wrap_counter;
1140 rte_vhost_set_last_inflight_io_split(int vid, uint16_t vring_idx,
1143 struct virtio_net *dev;
1144 struct vhost_virtqueue *vq;
1146 dev = get_device(vid);
1150 if (unlikely(!(dev->protocol_features &
1151 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1154 if (unlikely(vq_is_packed(dev)))
1157 if (unlikely(vring_idx >= VHOST_MAX_VRING))
1160 vq = dev->virtqueue[vring_idx];
1164 if (unlikely(!vq->inflight_split))
1167 vq->inflight_split->last_inflight_io = idx;
1172 rte_vhost_set_last_inflight_io_packed(int vid, uint16_t vring_idx,
1175 struct rte_vhost_inflight_info_packed *inflight_info;
1176 struct virtio_net *dev;
1177 struct vhost_virtqueue *vq;
1180 dev = get_device(vid);
1184 if (unlikely(!(dev->protocol_features &
1185 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1188 if (unlikely(!vq_is_packed(dev)))
1191 if (unlikely(vring_idx >= VHOST_MAX_VRING))
1194 vq = dev->virtqueue[vring_idx];
1198 inflight_info = vq->inflight_packed;
1199 if (unlikely(!inflight_info))
1202 if (unlikely(head >= vq->size))
1205 last = inflight_info->desc[head].last;
1206 if (unlikely(last >= vq->size))
1209 inflight_info->desc[last].next = inflight_info->free_head;
1210 inflight_info->free_head = head;
1211 inflight_info->used_idx += inflight_info->desc[head].num;
1212 if (inflight_info->used_idx >= inflight_info->desc_num) {
1213 inflight_info->used_idx -= inflight_info->desc_num;
1214 inflight_info->used_wrap_counter =
1215 !inflight_info->used_wrap_counter;
1222 rte_vhost_vring_call(int vid, uint16_t vring_idx)
1224 struct virtio_net *dev;
1225 struct vhost_virtqueue *vq;
1227 dev = get_device(vid);
1231 if (vring_idx >= VHOST_MAX_VRING)
1234 vq = dev->virtqueue[vring_idx];
1238 if (vq_is_packed(dev))
1239 vhost_vring_call_packed(dev, vq);
1241 vhost_vring_call_split(dev, vq);
1247 rte_vhost_avail_entries(int vid, uint16_t queue_id)
1249 struct virtio_net *dev;
1250 struct vhost_virtqueue *vq;
1253 dev = get_device(vid);
1257 vq = dev->virtqueue[queue_id];
1259 rte_spinlock_lock(&vq->access_lock);
1261 if (unlikely(!vq->enabled || vq->avail == NULL))
1264 ret = *(volatile uint16_t *)&vq->avail->idx - vq->last_used_idx;
1267 rte_spinlock_unlock(&vq->access_lock);
1272 vhost_enable_notify_split(struct virtio_net *dev,
1273 struct vhost_virtqueue *vq, int enable)
1275 if (vq->used == NULL)
1278 if (!(dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))) {
1280 vq->used->flags &= ~VRING_USED_F_NO_NOTIFY;
1282 vq->used->flags |= VRING_USED_F_NO_NOTIFY;
1285 vhost_avail_event(vq) = vq->last_avail_idx;
1291 vhost_enable_notify_packed(struct virtio_net *dev,
1292 struct vhost_virtqueue *vq, int enable)
1296 if (vq->device_event == NULL)
1300 vq->device_event->flags = VRING_EVENT_F_DISABLE;
1304 flags = VRING_EVENT_F_ENABLE;
1305 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX)) {
1306 flags = VRING_EVENT_F_DESC;
1307 vq->device_event->off_wrap = vq->last_avail_idx |
1308 vq->avail_wrap_counter << 15;
1313 vq->device_event->flags = flags;
1318 rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable)
1320 struct virtio_net *dev = get_device(vid);
1321 struct vhost_virtqueue *vq;
1327 vq = dev->virtqueue[queue_id];
1329 rte_spinlock_lock(&vq->access_lock);
1331 if (vq_is_packed(dev))
1332 ret = vhost_enable_notify_packed(dev, vq, enable);
1334 ret = vhost_enable_notify_split(dev, vq, enable);
1336 rte_spinlock_unlock(&vq->access_lock);
1342 rte_vhost_log_write(int vid, uint64_t addr, uint64_t len)
1344 struct virtio_net *dev = get_device(vid);
1349 vhost_log_write(dev, addr, len);
1353 rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
1354 uint64_t offset, uint64_t len)
1356 struct virtio_net *dev;
1357 struct vhost_virtqueue *vq;
1359 dev = get_device(vid);
1363 if (vring_idx >= VHOST_MAX_VRING)
1365 vq = dev->virtqueue[vring_idx];
1369 vhost_log_used_vring(dev, vq, offset, len);
1373 rte_vhost_rx_queue_count(int vid, uint16_t qid)
1375 struct virtio_net *dev;
1376 struct vhost_virtqueue *vq;
1379 dev = get_device(vid);
1383 if (unlikely(qid >= dev->nr_vring || (qid & 1) == 0)) {
1384 VHOST_LOG_DATA(ERR, "(%d) %s: invalid virtqueue idx %d.\n",
1385 dev->vid, __func__, qid);
1389 vq = dev->virtqueue[qid];
1393 rte_spinlock_lock(&vq->access_lock);
1395 if (unlikely(vq->enabled == 0 || vq->avail == NULL))
1398 ret = *((volatile uint16_t *)&vq->avail->idx) - vq->last_avail_idx;
1401 rte_spinlock_unlock(&vq->access_lock);
1405 int rte_vhost_get_vdpa_device_id(int vid)
1407 struct virtio_net *dev = get_device(vid);
1412 return dev->vdpa_dev_id;
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 RTE_INIT(vhost_log_init)
1522 vhost_config_log_level = rte_log_register("lib.vhost.config");
1523 if (vhost_config_log_level >= 0)
1524 rte_log_set_level(vhost_config_log_level, RTE_LOG_INFO);
1526 vhost_data_log_level = rte_log_register("lib.vhost.data");
1527 if (vhost_data_log_level >= 0)
1528 rte_log_set_level(vhost_data_log_level, RTE_LOG_WARNING);