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]);
357 vring_translate_split(struct virtio_net *dev, struct vhost_virtqueue *vq)
359 uint64_t req_size, size;
361 req_size = sizeof(struct vring_desc) * vq->size;
363 vq->desc = (struct vring_desc *)(uintptr_t)vhost_iova_to_vva(dev, vq,
364 vq->ring_addrs.desc_user_addr,
365 &size, VHOST_ACCESS_RW);
366 if (!vq->desc || size != req_size)
369 req_size = sizeof(struct vring_avail);
370 req_size += sizeof(uint16_t) * vq->size;
371 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))
372 req_size += sizeof(uint16_t);
374 vq->avail = (struct vring_avail *)(uintptr_t)vhost_iova_to_vva(dev, vq,
375 vq->ring_addrs.avail_user_addr,
376 &size, VHOST_ACCESS_RW);
377 if (!vq->avail || size != req_size)
380 req_size = sizeof(struct vring_used);
381 req_size += sizeof(struct vring_used_elem) * vq->size;
382 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))
383 req_size += sizeof(uint16_t);
385 vq->used = (struct vring_used *)(uintptr_t)vhost_iova_to_vva(dev, vq,
386 vq->ring_addrs.used_user_addr,
387 &size, VHOST_ACCESS_RW);
388 if (!vq->used || size != req_size)
395 vring_translate_packed(struct virtio_net *dev, struct vhost_virtqueue *vq)
397 uint64_t req_size, size;
399 req_size = sizeof(struct vring_packed_desc) * vq->size;
401 vq->desc_packed = (struct vring_packed_desc *)(uintptr_t)
402 vhost_iova_to_vva(dev, vq, vq->ring_addrs.desc_user_addr,
403 &size, VHOST_ACCESS_RW);
404 if (!vq->desc_packed || size != req_size)
407 req_size = sizeof(struct vring_packed_desc_event);
409 vq->driver_event = (struct vring_packed_desc_event *)(uintptr_t)
410 vhost_iova_to_vva(dev, vq, vq->ring_addrs.avail_user_addr,
411 &size, VHOST_ACCESS_RW);
412 if (!vq->driver_event || size != req_size)
415 req_size = sizeof(struct vring_packed_desc_event);
417 vq->device_event = (struct vring_packed_desc_event *)(uintptr_t)
418 vhost_iova_to_vva(dev, vq, vq->ring_addrs.used_user_addr,
419 &size, VHOST_ACCESS_RW);
420 if (!vq->device_event || size != req_size)
427 vring_translate(struct virtio_net *dev, struct vhost_virtqueue *vq)
430 if (!(dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)))
433 if (vq_is_packed(dev)) {
434 if (vring_translate_packed(dev, vq) < 0)
437 if (vring_translate_split(dev, vq) < 0)
446 vring_invalidate(struct virtio_net *dev, struct vhost_virtqueue *vq)
448 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
449 vhost_user_iotlb_wr_lock(vq);
455 vq->log_guest_addr = 0;
457 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
458 vhost_user_iotlb_wr_unlock(vq);
462 init_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
464 struct vhost_virtqueue *vq;
466 if (vring_idx >= VHOST_MAX_VRING) {
467 VHOST_LOG_CONFIG(ERR,
468 "Failed not init vring, out of bound (%d)\n",
473 vq = dev->virtqueue[vring_idx];
475 memset(vq, 0, sizeof(struct vhost_virtqueue));
477 vq->kickfd = VIRTIO_UNINITIALIZED_EVENTFD;
478 vq->callfd = VIRTIO_UNINITIALIZED_EVENTFD;
480 vhost_user_iotlb_init(dev, vring_idx);
481 /* Backends are set to -1 indicating an inactive device. */
484 TAILQ_INIT(&vq->zmbuf_list);
488 reset_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
490 struct vhost_virtqueue *vq;
493 if (vring_idx >= VHOST_MAX_VRING) {
494 VHOST_LOG_CONFIG(ERR,
495 "Failed not init vring, out of bound (%d)\n",
500 vq = dev->virtqueue[vring_idx];
502 init_vring_queue(dev, vring_idx);
507 alloc_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
509 struct vhost_virtqueue *vq;
511 vq = rte_malloc(NULL, sizeof(struct vhost_virtqueue), 0);
513 VHOST_LOG_CONFIG(ERR,
514 "Failed to allocate memory for vring:%u.\n", vring_idx);
518 dev->virtqueue[vring_idx] = vq;
519 init_vring_queue(dev, vring_idx);
520 rte_spinlock_init(&vq->access_lock);
521 vq->avail_wrap_counter = 1;
522 vq->used_wrap_counter = 1;
523 vq->signalled_used_valid = false;
531 * Reset some variables in device structure, while keeping few
532 * others untouched, such as vid, ifname, nr_vring: they
533 * should be same unless the device is removed.
536 reset_device(struct virtio_net *dev)
541 dev->protocol_features = 0;
542 dev->flags &= VIRTIO_DEV_BUILTIN_VIRTIO_NET;
544 for (i = 0; i < dev->nr_vring; i++)
545 reset_vring_queue(dev, i);
549 * Invoked when there is a new vhost-user connection established (when
550 * there is a new virtio device being attached).
553 vhost_new_device(void)
555 struct virtio_net *dev;
558 for (i = 0; i < MAX_VHOST_DEVICE; i++) {
559 if (vhost_devices[i] == NULL)
563 if (i == MAX_VHOST_DEVICE) {
564 VHOST_LOG_CONFIG(ERR,
565 "Failed to find a free slot for new device.\n");
569 dev = rte_zmalloc(NULL, sizeof(struct virtio_net), 0);
571 VHOST_LOG_CONFIG(ERR,
572 "Failed to allocate memory for new dev.\n");
576 vhost_devices[i] = dev;
578 dev->flags = VIRTIO_DEV_BUILTIN_VIRTIO_NET;
579 dev->slave_req_fd = -1;
580 dev->vdpa_dev_id = -1;
581 dev->postcopy_ufd = -1;
582 rte_spinlock_init(&dev->slave_req_lock);
588 vhost_destroy_device_notify(struct virtio_net *dev)
590 struct rte_vdpa_device *vdpa_dev;
593 if (dev->flags & VIRTIO_DEV_RUNNING) {
594 did = dev->vdpa_dev_id;
595 vdpa_dev = rte_vdpa_get_device(did);
596 if (vdpa_dev && vdpa_dev->ops->dev_close)
597 vdpa_dev->ops->dev_close(dev->vid);
598 dev->flags &= ~VIRTIO_DEV_RUNNING;
599 dev->notify_ops->destroy_device(dev->vid);
604 * Invoked when there is the vhost-user connection is broken (when
605 * the virtio device is being detached).
608 vhost_destroy_device(int vid)
610 struct virtio_net *dev = get_device(vid);
615 vhost_destroy_device_notify(dev);
617 cleanup_device(dev, 1);
620 vhost_devices[vid] = NULL;
624 vhost_attach_vdpa_device(int vid, int did)
626 struct virtio_net *dev = get_device(vid);
631 if (rte_vdpa_get_device(did) == NULL)
634 dev->vdpa_dev_id = did;
638 vhost_set_ifname(int vid, const char *if_name, unsigned int if_len)
640 struct virtio_net *dev;
643 dev = get_device(vid);
647 len = if_len > sizeof(dev->ifname) ?
648 sizeof(dev->ifname) : if_len;
650 strncpy(dev->ifname, if_name, len);
651 dev->ifname[sizeof(dev->ifname) - 1] = '\0';
655 vhost_enable_dequeue_zero_copy(int vid)
657 struct virtio_net *dev = get_device(vid);
662 dev->dequeue_zero_copy = 1;
666 vhost_set_builtin_virtio_net(int vid, bool enable)
668 struct virtio_net *dev = get_device(vid);
674 dev->flags |= VIRTIO_DEV_BUILTIN_VIRTIO_NET;
676 dev->flags &= ~VIRTIO_DEV_BUILTIN_VIRTIO_NET;
680 vhost_enable_extbuf(int vid)
682 struct virtio_net *dev = get_device(vid);
691 vhost_enable_linearbuf(int vid)
693 struct virtio_net *dev = get_device(vid);
702 rte_vhost_get_mtu(int vid, uint16_t *mtu)
704 struct virtio_net *dev = get_device(vid);
706 if (dev == NULL || mtu == NULL)
709 if (!(dev->flags & VIRTIO_DEV_READY))
712 if (!(dev->features & (1ULL << VIRTIO_NET_F_MTU)))
721 rte_vhost_get_numa_node(int vid)
723 #ifdef RTE_LIBRTE_VHOST_NUMA
724 struct virtio_net *dev = get_device(vid);
728 if (dev == NULL || numa_available() != 0)
731 ret = get_mempolicy(&numa_node, NULL, 0, dev,
732 MPOL_F_NODE | MPOL_F_ADDR);
734 VHOST_LOG_CONFIG(ERR,
735 "(%d) failed to query numa node: %s\n",
736 vid, rte_strerror(errno));
748 rte_vhost_get_queue_num(int vid)
750 struct virtio_net *dev = get_device(vid);
755 return dev->nr_vring / 2;
759 rte_vhost_get_vring_num(int vid)
761 struct virtio_net *dev = get_device(vid);
766 return dev->nr_vring;
770 rte_vhost_get_ifname(int vid, char *buf, size_t len)
772 struct virtio_net *dev = get_device(vid);
774 if (dev == NULL || buf == NULL)
777 len = RTE_MIN(len, sizeof(dev->ifname));
779 strncpy(buf, dev->ifname, len);
786 rte_vhost_get_negotiated_features(int vid, uint64_t *features)
788 struct virtio_net *dev;
790 dev = get_device(vid);
791 if (dev == NULL || features == NULL)
794 *features = dev->features;
799 rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem)
801 struct virtio_net *dev;
802 struct rte_vhost_memory *m;
805 dev = get_device(vid);
806 if (dev == NULL || mem == NULL)
809 size = dev->mem->nregions * sizeof(struct rte_vhost_mem_region);
810 m = malloc(sizeof(struct rte_vhost_memory) + size);
814 m->nregions = dev->mem->nregions;
815 memcpy(m->regions, dev->mem->regions, size);
822 rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
823 struct rte_vhost_vring *vring)
825 struct virtio_net *dev;
826 struct vhost_virtqueue *vq;
828 dev = get_device(vid);
829 if (dev == NULL || vring == NULL)
832 if (vring_idx >= VHOST_MAX_VRING)
835 vq = dev->virtqueue[vring_idx];
839 if (vq_is_packed(dev)) {
840 vring->desc_packed = vq->desc_packed;
841 vring->driver_event = vq->driver_event;
842 vring->device_event = vq->device_event;
844 vring->desc = vq->desc;
845 vring->avail = vq->avail;
846 vring->used = vq->used;
848 vring->log_guest_addr = vq->log_guest_addr;
850 vring->callfd = vq->callfd;
851 vring->kickfd = vq->kickfd;
852 vring->size = vq->size;
858 rte_vhost_get_vhost_ring_inflight(int vid, uint16_t vring_idx,
859 struct rte_vhost_ring_inflight *vring)
861 struct virtio_net *dev;
862 struct vhost_virtqueue *vq;
864 dev = get_device(vid);
868 if (vring_idx >= VHOST_MAX_VRING)
871 vq = dev->virtqueue[vring_idx];
875 if (vq_is_packed(dev)) {
876 if (unlikely(!vq->inflight_packed))
879 vring->inflight_packed = vq->inflight_packed;
881 if (unlikely(!vq->inflight_split))
884 vring->inflight_split = vq->inflight_split;
887 vring->resubmit_inflight = vq->resubmit_inflight;
893 rte_vhost_set_inflight_desc_split(int vid, uint16_t vring_idx,
896 struct vhost_virtqueue *vq;
897 struct virtio_net *dev;
899 dev = get_device(vid);
903 if (unlikely(!(dev->protocol_features &
904 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
907 if (unlikely(vq_is_packed(dev)))
910 if (unlikely(vring_idx >= VHOST_MAX_VRING))
913 vq = dev->virtqueue[vring_idx];
917 if (unlikely(!vq->inflight_split))
920 if (unlikely(idx >= vq->size))
923 vq->inflight_split->desc[idx].counter = vq->global_counter++;
924 vq->inflight_split->desc[idx].inflight = 1;
929 rte_vhost_set_inflight_desc_packed(int vid, uint16_t vring_idx,
930 uint16_t head, uint16_t last,
931 uint16_t *inflight_entry)
933 struct rte_vhost_inflight_info_packed *inflight_info;
934 struct virtio_net *dev;
935 struct vhost_virtqueue *vq;
936 struct vring_packed_desc *desc;
937 uint16_t old_free_head, free_head;
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 inflight_info = vq->inflight_packed;
958 if (unlikely(!inflight_info))
961 if (unlikely(head >= vq->size))
964 desc = vq->desc_packed;
965 old_free_head = inflight_info->old_free_head;
966 if (unlikely(old_free_head >= vq->size))
969 free_head = old_free_head;
971 /* init header descriptor */
972 inflight_info->desc[old_free_head].num = 0;
973 inflight_info->desc[old_free_head].counter = vq->global_counter++;
974 inflight_info->desc[old_free_head].inflight = 1;
976 /* save desc entry in flight entry */
977 while (head != ((last + 1) % vq->size)) {
978 inflight_info->desc[old_free_head].num++;
979 inflight_info->desc[free_head].addr = desc[head].addr;
980 inflight_info->desc[free_head].len = desc[head].len;
981 inflight_info->desc[free_head].flags = desc[head].flags;
982 inflight_info->desc[free_head].id = desc[head].id;
984 inflight_info->desc[old_free_head].last = free_head;
985 free_head = inflight_info->desc[free_head].next;
986 inflight_info->free_head = free_head;
987 head = (head + 1) % vq->size;
990 inflight_info->old_free_head = free_head;
991 *inflight_entry = old_free_head;
997 rte_vhost_clr_inflight_desc_split(int vid, uint16_t vring_idx,
998 uint16_t last_used_idx, uint16_t idx)
1000 struct virtio_net *dev;
1001 struct vhost_virtqueue *vq;
1003 dev = get_device(vid);
1007 if (unlikely(!(dev->protocol_features &
1008 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1011 if (unlikely(vq_is_packed(dev)))
1014 if (unlikely(vring_idx >= VHOST_MAX_VRING))
1017 vq = dev->virtqueue[vring_idx];
1021 if (unlikely(!vq->inflight_split))
1024 if (unlikely(idx >= vq->size))
1029 vq->inflight_split->desc[idx].inflight = 0;
1033 vq->inflight_split->used_idx = last_used_idx;
1038 rte_vhost_clr_inflight_desc_packed(int vid, uint16_t vring_idx,
1041 struct rte_vhost_inflight_info_packed *inflight_info;
1042 struct virtio_net *dev;
1043 struct vhost_virtqueue *vq;
1045 dev = get_device(vid);
1049 if (unlikely(!(dev->protocol_features &
1050 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1053 if (unlikely(!vq_is_packed(dev)))
1056 if (unlikely(vring_idx >= VHOST_MAX_VRING))
1059 vq = dev->virtqueue[vring_idx];
1063 inflight_info = vq->inflight_packed;
1064 if (unlikely(!inflight_info))
1067 if (unlikely(head >= vq->size))
1072 inflight_info->desc[head].inflight = 0;
1076 inflight_info->old_free_head = inflight_info->free_head;
1077 inflight_info->old_used_idx = inflight_info->used_idx;
1078 inflight_info->old_used_wrap_counter = inflight_info->used_wrap_counter;
1084 rte_vhost_set_last_inflight_io_split(int vid, uint16_t vring_idx,
1087 struct virtio_net *dev;
1088 struct vhost_virtqueue *vq;
1090 dev = get_device(vid);
1094 if (unlikely(!(dev->protocol_features &
1095 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1098 if (unlikely(vq_is_packed(dev)))
1101 if (unlikely(vring_idx >= VHOST_MAX_VRING))
1104 vq = dev->virtqueue[vring_idx];
1108 if (unlikely(!vq->inflight_split))
1111 vq->inflight_split->last_inflight_io = idx;
1116 rte_vhost_set_last_inflight_io_packed(int vid, uint16_t vring_idx,
1119 struct rte_vhost_inflight_info_packed *inflight_info;
1120 struct virtio_net *dev;
1121 struct vhost_virtqueue *vq;
1124 dev = get_device(vid);
1128 if (unlikely(!(dev->protocol_features &
1129 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1132 if (unlikely(!vq_is_packed(dev)))
1135 if (unlikely(vring_idx >= VHOST_MAX_VRING))
1138 vq = dev->virtqueue[vring_idx];
1142 inflight_info = vq->inflight_packed;
1143 if (unlikely(!inflight_info))
1146 if (unlikely(head >= vq->size))
1149 last = inflight_info->desc[head].last;
1150 if (unlikely(last >= vq->size))
1153 inflight_info->desc[last].next = inflight_info->free_head;
1154 inflight_info->free_head = head;
1155 inflight_info->used_idx += inflight_info->desc[head].num;
1156 if (inflight_info->used_idx >= inflight_info->desc_num) {
1157 inflight_info->used_idx -= inflight_info->desc_num;
1158 inflight_info->used_wrap_counter =
1159 !inflight_info->used_wrap_counter;
1166 rte_vhost_vring_call(int vid, uint16_t vring_idx)
1168 struct virtio_net *dev;
1169 struct vhost_virtqueue *vq;
1171 dev = get_device(vid);
1175 if (vring_idx >= VHOST_MAX_VRING)
1178 vq = dev->virtqueue[vring_idx];
1182 if (vq_is_packed(dev))
1183 vhost_vring_call_packed(dev, vq);
1185 vhost_vring_call_split(dev, vq);
1191 rte_vhost_avail_entries(int vid, uint16_t queue_id)
1193 struct virtio_net *dev;
1194 struct vhost_virtqueue *vq;
1197 dev = get_device(vid);
1201 vq = dev->virtqueue[queue_id];
1203 rte_spinlock_lock(&vq->access_lock);
1205 if (unlikely(!vq->enabled || vq->avail == NULL))
1208 ret = *(volatile uint16_t *)&vq->avail->idx - vq->last_used_idx;
1211 rte_spinlock_unlock(&vq->access_lock);
1216 vhost_enable_notify_split(struct virtio_net *dev,
1217 struct vhost_virtqueue *vq, int enable)
1219 if (vq->used == NULL)
1222 if (!(dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))) {
1224 vq->used->flags &= ~VRING_USED_F_NO_NOTIFY;
1226 vq->used->flags |= VRING_USED_F_NO_NOTIFY;
1229 vhost_avail_event(vq) = vq->last_avail_idx;
1235 vhost_enable_notify_packed(struct virtio_net *dev,
1236 struct vhost_virtqueue *vq, int enable)
1240 if (vq->device_event == NULL)
1244 vq->device_event->flags = VRING_EVENT_F_DISABLE;
1248 flags = VRING_EVENT_F_ENABLE;
1249 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX)) {
1250 flags = VRING_EVENT_F_DESC;
1251 vq->device_event->off_wrap = vq->last_avail_idx |
1252 vq->avail_wrap_counter << 15;
1257 vq->device_event->flags = flags;
1262 rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable)
1264 struct virtio_net *dev = get_device(vid);
1265 struct vhost_virtqueue *vq;
1271 vq = dev->virtqueue[queue_id];
1273 rte_spinlock_lock(&vq->access_lock);
1275 if (vq_is_packed(dev))
1276 ret = vhost_enable_notify_packed(dev, vq, enable);
1278 ret = vhost_enable_notify_split(dev, vq, enable);
1280 rte_spinlock_unlock(&vq->access_lock);
1286 rte_vhost_log_write(int vid, uint64_t addr, uint64_t len)
1288 struct virtio_net *dev = get_device(vid);
1293 vhost_log_write(dev, addr, len);
1297 rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
1298 uint64_t offset, uint64_t len)
1300 struct virtio_net *dev;
1301 struct vhost_virtqueue *vq;
1303 dev = get_device(vid);
1307 if (vring_idx >= VHOST_MAX_VRING)
1309 vq = dev->virtqueue[vring_idx];
1313 vhost_log_used_vring(dev, vq, offset, len);
1317 rte_vhost_rx_queue_count(int vid, uint16_t qid)
1319 struct virtio_net *dev;
1320 struct vhost_virtqueue *vq;
1323 dev = get_device(vid);
1327 if (unlikely(qid >= dev->nr_vring || (qid & 1) == 0)) {
1328 VHOST_LOG_DATA(ERR, "(%d) %s: invalid virtqueue idx %d.\n",
1329 dev->vid, __func__, qid);
1333 vq = dev->virtqueue[qid];
1337 rte_spinlock_lock(&vq->access_lock);
1339 if (unlikely(vq->enabled == 0 || vq->avail == NULL))
1342 ret = *((volatile uint16_t *)&vq->avail->idx) - vq->last_avail_idx;
1345 rte_spinlock_unlock(&vq->access_lock);
1349 int rte_vhost_get_vdpa_device_id(int vid)
1351 struct virtio_net *dev = get_device(vid);
1356 return dev->vdpa_dev_id;
1359 int rte_vhost_get_log_base(int vid, uint64_t *log_base,
1362 struct virtio_net *dev = get_device(vid);
1364 if (dev == NULL || log_base == NULL || log_size == NULL)
1367 *log_base = dev->log_base;
1368 *log_size = dev->log_size;
1373 int rte_vhost_get_vring_base(int vid, uint16_t queue_id,
1374 uint16_t *last_avail_idx, uint16_t *last_used_idx)
1376 struct vhost_virtqueue *vq;
1377 struct virtio_net *dev = get_device(vid);
1379 if (dev == NULL || last_avail_idx == NULL || last_used_idx == NULL)
1382 vq = dev->virtqueue[queue_id];
1386 if (vq_is_packed(dev)) {
1387 *last_avail_idx = (vq->avail_wrap_counter << 15) |
1389 *last_used_idx = (vq->used_wrap_counter << 15) |
1392 *last_avail_idx = vq->last_avail_idx;
1393 *last_used_idx = vq->last_used_idx;
1399 int rte_vhost_set_vring_base(int vid, uint16_t queue_id,
1400 uint16_t last_avail_idx, uint16_t last_used_idx)
1402 struct vhost_virtqueue *vq;
1403 struct virtio_net *dev = get_device(vid);
1408 vq = dev->virtqueue[queue_id];
1412 if (vq_is_packed(dev)) {
1413 vq->last_avail_idx = last_avail_idx & 0x7fff;
1414 vq->avail_wrap_counter = !!(last_avail_idx & (1 << 15));
1415 vq->last_used_idx = last_used_idx & 0x7fff;
1416 vq->used_wrap_counter = !!(last_used_idx & (1 << 15));
1418 vq->last_avail_idx = last_avail_idx;
1419 vq->last_used_idx = last_used_idx;
1426 rte_vhost_get_vring_base_from_inflight(int vid,
1428 uint16_t *last_avail_idx,
1429 uint16_t *last_used_idx)
1431 struct rte_vhost_inflight_info_packed *inflight_info;
1432 struct virtio_net *dev = get_device(vid);
1434 if (dev == NULL || last_avail_idx == NULL || last_used_idx == NULL)
1437 if (!vq_is_packed(dev))
1440 inflight_info = dev->virtqueue[queue_id]->inflight_packed;
1444 *last_avail_idx = (inflight_info->old_used_wrap_counter << 15) |
1445 inflight_info->old_used_idx;
1446 *last_used_idx = *last_avail_idx;
1451 int rte_vhost_extern_callback_register(int vid,
1452 struct rte_vhost_user_extern_ops const * const ops, void *ctx)
1454 struct virtio_net *dev = get_device(vid);
1456 if (dev == NULL || ops == NULL)
1459 dev->extern_ops = *ops;
1460 dev->extern_data = ctx;
1464 RTE_INIT(vhost_log_init)
1466 vhost_config_log_level = rte_log_register("lib.vhost.config");
1467 if (vhost_config_log_level >= 0)
1468 rte_log_set_level(vhost_config_log_level, RTE_LOG_INFO);
1470 vhost_data_log_level = rte_log_register("lib.vhost.data");
1471 if (vhost_data_log_level >= 0)
1472 rte_log_set_level(vhost_data_log_level, RTE_LOG_WARNING);