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[RTE_MAX_VHOST_DEVICE];
29 pthread_mutex_t vhost_dev_lock = PTHREAD_MUTEX_INITIALIZER;
31 /* Called with iotlb_lock read-locked */
33 __vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
34 uint64_t iova, uint64_t *size, uint8_t perm)
36 uint64_t vva, tmp_size;
43 vva = vhost_user_iotlb_cache_find(vq, iova, &tmp_size, perm);
44 if (tmp_size == *size)
49 if (!vhost_user_iotlb_pending_miss(vq, iova, perm)) {
51 * iotlb_lock is read-locked for a full burst,
52 * but it only protects the iotlb cache.
53 * In case of IOTLB miss, we might block on the socket,
54 * which could cause a deadlock with QEMU if an IOTLB update
55 * is being handled. We can safely unlock here to avoid it.
57 vhost_user_iotlb_rd_unlock(vq);
59 vhost_user_iotlb_pending_insert(dev, vq, iova, perm);
60 if (vhost_user_iotlb_miss(dev, iova, perm)) {
61 VHOST_LOG_DATA(ERR, "(%s) 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 */
109 rte_atomic_thread_fence(__ATOMIC_RELEASE);
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 "(%s) 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))
147 /* No cache, nothing to sync */
148 if (unlikely(!vq->log_cache))
151 rte_atomic_thread_fence(__ATOMIC_RELEASE);
153 log_base = (unsigned long *)(uintptr_t)dev->log_base;
155 for (i = 0; i < vq->log_cache_nb_elem; i++) {
156 struct log_cache_entry *elem = vq->log_cache + i;
158 #if defined(RTE_TOOLCHAIN_GCC) && (GCC_VERSION < 70100)
160 * '__sync' builtins are deprecated, but '__atomic' ones
161 * are sub-optimized in older GCC versions.
163 __sync_fetch_and_or(log_base + elem->offset, elem->val);
165 __atomic_fetch_or(log_base + elem->offset, elem->val,
170 rte_atomic_thread_fence(__ATOMIC_RELEASE);
172 vq->log_cache_nb_elem = 0;
175 static __rte_always_inline void
176 vhost_log_cache_page(struct virtio_net *dev, struct vhost_virtqueue *vq,
179 uint32_t bit_nr = page % (sizeof(unsigned long) << 3);
180 uint32_t offset = page / (sizeof(unsigned long) << 3);
183 if (unlikely(!vq->log_cache)) {
184 /* No logging cache allocated, write dirty log map directly */
185 rte_atomic_thread_fence(__ATOMIC_RELEASE);
186 vhost_log_page((uint8_t *)(uintptr_t)dev->log_base, page);
191 for (i = 0; i < vq->log_cache_nb_elem; i++) {
192 struct log_cache_entry *elem = vq->log_cache + i;
194 if (elem->offset == offset) {
195 elem->val |= (1UL << bit_nr);
200 if (unlikely(i >= VHOST_LOG_CACHE_NR)) {
202 * No more room for a new log cache entry,
203 * so write the dirty log map directly.
205 rte_atomic_thread_fence(__ATOMIC_RELEASE);
206 vhost_log_page((uint8_t *)(uintptr_t)dev->log_base, page);
211 vq->log_cache[i].offset = offset;
212 vq->log_cache[i].val = (1UL << bit_nr);
213 vq->log_cache_nb_elem++;
217 __vhost_log_cache_write(struct virtio_net *dev, struct vhost_virtqueue *vq,
218 uint64_t addr, uint64_t len)
222 if (unlikely(!dev->log_base || !len))
225 if (unlikely(dev->log_size <= ((addr + len - 1) / VHOST_LOG_PAGE / 8)))
228 page = addr / VHOST_LOG_PAGE;
229 while (page * VHOST_LOG_PAGE < addr + len) {
230 vhost_log_cache_page(dev, vq, page);
236 __vhost_log_cache_write_iova(struct virtio_net *dev, struct vhost_virtqueue *vq,
237 uint64_t iova, uint64_t len)
239 uint64_t hva, gpa, map_len;
242 hva = __vhost_iova_to_vva(dev, vq, iova, &map_len, VHOST_ACCESS_RW);
243 if (map_len != len) {
245 "(%s) failed to write log for IOVA 0x%" PRIx64 ". No IOTLB entry found\n",
250 gpa = hva_to_gpa(dev, hva, len);
252 __vhost_log_cache_write(dev, vq, gpa, len);
256 vhost_alloc_copy_ind_table(struct virtio_net *dev, struct vhost_virtqueue *vq,
257 uint64_t desc_addr, uint64_t desc_len)
261 uint64_t len, remain = desc_len;
263 idesc = rte_malloc_socket(__func__, desc_len, 0, vq->numa_node);
264 if (unlikely(!idesc))
267 dst = (uint64_t)(uintptr_t)idesc;
271 src = vhost_iova_to_vva(dev, vq, desc_addr, &len,
273 if (unlikely(!src || !len)) {
278 rte_memcpy((void *)(uintptr_t)dst, (void *)(uintptr_t)src, len);
289 cleanup_vq(struct vhost_virtqueue *vq, int destroy)
291 if ((vq->callfd >= 0) && (destroy != 0))
298 cleanup_vq_inflight(struct virtio_net *dev, struct vhost_virtqueue *vq)
300 if (!(dev->protocol_features &
301 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD)))
304 if (vq_is_packed(dev)) {
305 if (vq->inflight_packed)
306 vq->inflight_packed = NULL;
308 if (vq->inflight_split)
309 vq->inflight_split = NULL;
312 if (vq->resubmit_inflight) {
313 if (vq->resubmit_inflight->resubmit_list) {
314 rte_free(vq->resubmit_inflight->resubmit_list);
315 vq->resubmit_inflight->resubmit_list = NULL;
317 rte_free(vq->resubmit_inflight);
318 vq->resubmit_inflight = NULL;
323 * Unmap any memory, close any file descriptors and
324 * free any memory owned by a device.
327 cleanup_device(struct virtio_net *dev, int destroy)
331 vhost_backend_cleanup(dev);
333 for (i = 0; i < dev->nr_vring; i++) {
334 cleanup_vq(dev->virtqueue[i], destroy);
335 cleanup_vq_inflight(dev, dev->virtqueue[i]);
340 vhost_free_async_mem(struct vhost_virtqueue *vq)
345 rte_free(vq->async->pkts_info);
346 rte_free(vq->async->pkts_cmpl_flag);
348 rte_free(vq->async->buffers_packed);
349 vq->async->buffers_packed = NULL;
350 rte_free(vq->async->descs_split);
351 vq->async->descs_split = NULL;
358 free_vq(struct virtio_net *dev, struct vhost_virtqueue *vq)
360 if (vq_is_packed(dev))
361 rte_free(vq->shadow_used_packed);
363 rte_free(vq->shadow_used_split);
365 vhost_free_async_mem(vq);
366 rte_free(vq->batch_copy_elems);
367 rte_mempool_free(vq->iotlb_pool);
368 rte_free(vq->log_cache);
373 * Release virtqueues and device memory.
376 free_device(struct virtio_net *dev)
380 for (i = 0; i < dev->nr_vring; i++)
381 free_vq(dev, dev->virtqueue[i]);
386 static __rte_always_inline int
387 log_translate(struct virtio_net *dev, struct vhost_virtqueue *vq)
389 if (likely(!(vq->ring_addrs.flags & (1 << VHOST_VRING_F_LOG))))
392 vq->log_guest_addr = translate_log_addr(dev, vq,
393 vq->ring_addrs.log_guest_addr);
394 if (vq->log_guest_addr == 0)
401 * Converts vring log address to GPA
402 * If IOMMU is enabled, the log address is IOVA
403 * If IOMMU not enabled, the log address is already GPA
405 * Caller should have iotlb_lock read-locked
408 translate_log_addr(struct virtio_net *dev, struct vhost_virtqueue *vq,
411 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)) {
412 const uint64_t exp_size = sizeof(uint64_t);
414 uint64_t size = exp_size;
416 hva = vhost_iova_to_vva(dev, vq, log_addr,
417 &size, VHOST_ACCESS_RW);
419 if (size != exp_size)
422 gpa = hva_to_gpa(dev, hva, exp_size);
425 "(%s) failed to find GPA for log_addr: 0x%"
426 PRIx64 " hva: 0x%" PRIx64 "\n",
427 dev->ifname, log_addr, hva);
436 /* Caller should have iotlb_lock read-locked */
438 vring_translate_split(struct virtio_net *dev, struct vhost_virtqueue *vq)
440 uint64_t req_size, size;
442 req_size = sizeof(struct vring_desc) * vq->size;
444 vq->desc = (struct vring_desc *)(uintptr_t)vhost_iova_to_vva(dev, vq,
445 vq->ring_addrs.desc_user_addr,
446 &size, VHOST_ACCESS_RW);
447 if (!vq->desc || size != req_size)
450 req_size = sizeof(struct vring_avail);
451 req_size += sizeof(uint16_t) * vq->size;
452 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))
453 req_size += sizeof(uint16_t);
455 vq->avail = (struct vring_avail *)(uintptr_t)vhost_iova_to_vva(dev, vq,
456 vq->ring_addrs.avail_user_addr,
457 &size, VHOST_ACCESS_RW);
458 if (!vq->avail || size != req_size)
461 req_size = sizeof(struct vring_used);
462 req_size += sizeof(struct vring_used_elem) * vq->size;
463 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))
464 req_size += sizeof(uint16_t);
466 vq->used = (struct vring_used *)(uintptr_t)vhost_iova_to_vva(dev, vq,
467 vq->ring_addrs.used_user_addr,
468 &size, VHOST_ACCESS_RW);
469 if (!vq->used || size != req_size)
475 /* Caller should have iotlb_lock read-locked */
477 vring_translate_packed(struct virtio_net *dev, struct vhost_virtqueue *vq)
479 uint64_t req_size, size;
481 req_size = sizeof(struct vring_packed_desc) * vq->size;
483 vq->desc_packed = (struct vring_packed_desc *)(uintptr_t)
484 vhost_iova_to_vva(dev, vq, vq->ring_addrs.desc_user_addr,
485 &size, VHOST_ACCESS_RW);
486 if (!vq->desc_packed || size != req_size)
489 req_size = sizeof(struct vring_packed_desc_event);
491 vq->driver_event = (struct vring_packed_desc_event *)(uintptr_t)
492 vhost_iova_to_vva(dev, vq, vq->ring_addrs.avail_user_addr,
493 &size, VHOST_ACCESS_RW);
494 if (!vq->driver_event || size != req_size)
497 req_size = sizeof(struct vring_packed_desc_event);
499 vq->device_event = (struct vring_packed_desc_event *)(uintptr_t)
500 vhost_iova_to_vva(dev, vq, vq->ring_addrs.used_user_addr,
501 &size, VHOST_ACCESS_RW);
502 if (!vq->device_event || size != req_size)
509 vring_translate(struct virtio_net *dev, struct vhost_virtqueue *vq)
512 if (!(dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)))
515 if (vq_is_packed(dev)) {
516 if (vring_translate_packed(dev, vq) < 0)
519 if (vring_translate_split(dev, vq) < 0)
523 if (log_translate(dev, vq) < 0)
526 vq->access_ok = true;
532 vring_invalidate(struct virtio_net *dev, struct vhost_virtqueue *vq)
534 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
535 vhost_user_iotlb_wr_lock(vq);
537 vq->access_ok = false;
541 vq->log_guest_addr = 0;
543 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
544 vhost_user_iotlb_wr_unlock(vq);
548 init_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
550 struct vhost_virtqueue *vq;
551 int numa_node = SOCKET_ID_ANY;
553 if (vring_idx >= VHOST_MAX_VRING) {
554 VHOST_LOG_CONFIG(ERR, "(%s) failed to init vring, out of bound (%d)\n",
555 dev->ifname, vring_idx);
559 vq = dev->virtqueue[vring_idx];
561 VHOST_LOG_CONFIG(ERR, "(%s) virtqueue not allocated (%d)\n",
562 dev->ifname, vring_idx);
566 memset(vq, 0, sizeof(struct vhost_virtqueue));
568 vq->kickfd = VIRTIO_UNINITIALIZED_EVENTFD;
569 vq->callfd = VIRTIO_UNINITIALIZED_EVENTFD;
570 vq->notif_enable = VIRTIO_UNINITIALIZED_NOTIF;
572 #ifdef RTE_LIBRTE_VHOST_NUMA
573 if (get_mempolicy(&numa_node, NULL, 0, vq, MPOL_F_NODE | MPOL_F_ADDR)) {
574 VHOST_LOG_CONFIG(ERR, "(%s) failed to query numa node: %s\n",
575 dev->ifname, rte_strerror(errno));
576 numa_node = SOCKET_ID_ANY;
579 vq->numa_node = numa_node;
581 vhost_user_iotlb_init(dev, vring_idx);
585 reset_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
587 struct vhost_virtqueue *vq;
590 if (vring_idx >= VHOST_MAX_VRING) {
591 VHOST_LOG_CONFIG(ERR,
592 "(%s) failed to reset vring, out of bound (%d)\n",
593 dev->ifname, vring_idx);
597 vq = dev->virtqueue[vring_idx];
599 VHOST_LOG_CONFIG(ERR, "(%s) failed to reset vring, virtqueue not allocated (%d)\n",
600 dev->ifname, vring_idx);
605 init_vring_queue(dev, vring_idx);
610 alloc_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
612 struct vhost_virtqueue *vq;
615 /* Also allocate holes, if any, up to requested vring index. */
616 for (i = 0; i <= vring_idx; i++) {
617 if (dev->virtqueue[i])
620 vq = rte_zmalloc(NULL, sizeof(struct vhost_virtqueue), 0);
622 VHOST_LOG_CONFIG(ERR, "(%s) failed to allocate memory for vring %u.\n",
627 dev->virtqueue[i] = vq;
628 init_vring_queue(dev, i);
629 rte_spinlock_init(&vq->access_lock);
630 vq->avail_wrap_counter = 1;
631 vq->used_wrap_counter = 1;
632 vq->signalled_used_valid = false;
635 dev->nr_vring = RTE_MAX(dev->nr_vring, vring_idx + 1);
641 * Reset some variables in device structure, while keeping few
642 * others untouched, such as vid, ifname, nr_vring: they
643 * should be same unless the device is removed.
646 reset_device(struct virtio_net *dev)
651 dev->protocol_features = 0;
652 dev->flags &= VIRTIO_DEV_BUILTIN_VIRTIO_NET;
654 for (i = 0; i < dev->nr_vring; i++)
655 reset_vring_queue(dev, i);
659 * Invoked when there is a new vhost-user connection established (when
660 * there is a new virtio device being attached).
663 vhost_new_device(void)
665 struct virtio_net *dev;
668 pthread_mutex_lock(&vhost_dev_lock);
669 for (i = 0; i < RTE_MAX_VHOST_DEVICE; i++) {
670 if (vhost_devices[i] == NULL)
674 if (i == RTE_MAX_VHOST_DEVICE) {
675 VHOST_LOG_CONFIG(ERR, "failed to find a free slot for new device.\n");
676 pthread_mutex_unlock(&vhost_dev_lock);
680 dev = rte_zmalloc(NULL, sizeof(struct virtio_net), 0);
682 VHOST_LOG_CONFIG(ERR, "failed to allocate memory for new device.\n");
683 pthread_mutex_unlock(&vhost_dev_lock);
687 vhost_devices[i] = dev;
688 pthread_mutex_unlock(&vhost_dev_lock);
691 dev->flags = VIRTIO_DEV_BUILTIN_VIRTIO_NET;
692 dev->slave_req_fd = -1;
693 dev->postcopy_ufd = -1;
694 rte_spinlock_init(&dev->slave_req_lock);
700 vhost_destroy_device_notify(struct virtio_net *dev)
702 struct rte_vdpa_device *vdpa_dev;
704 if (dev->flags & VIRTIO_DEV_RUNNING) {
705 vdpa_dev = dev->vdpa_dev;
707 vdpa_dev->ops->dev_close(dev->vid);
708 dev->flags &= ~VIRTIO_DEV_RUNNING;
709 dev->notify_ops->destroy_device(dev->vid);
714 * Invoked when there is the vhost-user connection is broken (when
715 * the virtio device is being detached).
718 vhost_destroy_device(int vid)
720 struct virtio_net *dev = get_device(vid);
725 vhost_destroy_device_notify(dev);
727 cleanup_device(dev, 1);
730 vhost_devices[vid] = NULL;
734 vhost_attach_vdpa_device(int vid, struct rte_vdpa_device *vdpa_dev)
736 struct virtio_net *dev = get_device(vid);
741 dev->vdpa_dev = vdpa_dev;
745 vhost_set_ifname(int vid, const char *if_name, unsigned int if_len)
747 struct virtio_net *dev;
750 dev = get_device(vid);
754 len = if_len > sizeof(dev->ifname) ?
755 sizeof(dev->ifname) : if_len;
757 strncpy(dev->ifname, if_name, len);
758 dev->ifname[sizeof(dev->ifname) - 1] = '\0';
762 vhost_setup_virtio_net(int vid, bool enable, bool compliant_ol_flags)
764 struct virtio_net *dev = get_device(vid);
770 dev->flags |= VIRTIO_DEV_BUILTIN_VIRTIO_NET;
772 dev->flags &= ~VIRTIO_DEV_BUILTIN_VIRTIO_NET;
773 if (!compliant_ol_flags)
774 dev->flags |= VIRTIO_DEV_LEGACY_OL_FLAGS;
776 dev->flags &= ~VIRTIO_DEV_LEGACY_OL_FLAGS;
780 vhost_enable_extbuf(int vid)
782 struct virtio_net *dev = get_device(vid);
791 vhost_enable_linearbuf(int vid)
793 struct virtio_net *dev = get_device(vid);
802 rte_vhost_get_mtu(int vid, uint16_t *mtu)
804 struct virtio_net *dev = get_device(vid);
806 if (dev == NULL || mtu == NULL)
809 if (!(dev->flags & VIRTIO_DEV_READY))
812 if (!(dev->features & (1ULL << VIRTIO_NET_F_MTU)))
821 rte_vhost_get_numa_node(int vid)
823 #ifdef RTE_LIBRTE_VHOST_NUMA
824 struct virtio_net *dev = get_device(vid);
828 if (dev == NULL || numa_available() != 0)
831 ret = get_mempolicy(&numa_node, NULL, 0, dev,
832 MPOL_F_NODE | MPOL_F_ADDR);
834 VHOST_LOG_CONFIG(ERR, "(%s) failed to query numa node: %s\n",
835 dev->ifname, rte_strerror(errno));
847 rte_vhost_get_queue_num(int vid)
849 struct virtio_net *dev = get_device(vid);
854 return dev->nr_vring / 2;
858 rte_vhost_get_vring_num(int vid)
860 struct virtio_net *dev = get_device(vid);
865 return dev->nr_vring;
869 rte_vhost_get_ifname(int vid, char *buf, size_t len)
871 struct virtio_net *dev = get_device(vid);
873 if (dev == NULL || buf == NULL)
876 len = RTE_MIN(len, sizeof(dev->ifname));
878 strncpy(buf, dev->ifname, len);
885 rte_vhost_get_negotiated_features(int vid, uint64_t *features)
887 struct virtio_net *dev;
889 dev = get_device(vid);
890 if (dev == NULL || features == NULL)
893 *features = dev->features;
898 rte_vhost_get_negotiated_protocol_features(int vid,
899 uint64_t *protocol_features)
901 struct virtio_net *dev;
903 dev = get_device(vid);
904 if (dev == NULL || protocol_features == NULL)
907 *protocol_features = dev->protocol_features;
912 rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem)
914 struct virtio_net *dev;
915 struct rte_vhost_memory *m;
918 dev = get_device(vid);
919 if (dev == NULL || mem == NULL)
922 size = dev->mem->nregions * sizeof(struct rte_vhost_mem_region);
923 m = malloc(sizeof(struct rte_vhost_memory) + size);
927 m->nregions = dev->mem->nregions;
928 memcpy(m->regions, dev->mem->regions, size);
935 rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
936 struct rte_vhost_vring *vring)
938 struct virtio_net *dev;
939 struct vhost_virtqueue *vq;
941 dev = get_device(vid);
942 if (dev == NULL || vring == NULL)
945 if (vring_idx >= VHOST_MAX_VRING)
948 vq = dev->virtqueue[vring_idx];
952 if (vq_is_packed(dev)) {
953 vring->desc_packed = vq->desc_packed;
954 vring->driver_event = vq->driver_event;
955 vring->device_event = vq->device_event;
957 vring->desc = vq->desc;
958 vring->avail = vq->avail;
959 vring->used = vq->used;
961 vring->log_guest_addr = vq->log_guest_addr;
963 vring->callfd = vq->callfd;
964 vring->kickfd = vq->kickfd;
965 vring->size = vq->size;
971 rte_vhost_get_vhost_ring_inflight(int vid, uint16_t vring_idx,
972 struct rte_vhost_ring_inflight *vring)
974 struct virtio_net *dev;
975 struct vhost_virtqueue *vq;
977 dev = get_device(vid);
981 if (vring_idx >= VHOST_MAX_VRING)
984 vq = dev->virtqueue[vring_idx];
988 if (vq_is_packed(dev)) {
989 if (unlikely(!vq->inflight_packed))
992 vring->inflight_packed = vq->inflight_packed;
994 if (unlikely(!vq->inflight_split))
997 vring->inflight_split = vq->inflight_split;
1000 vring->resubmit_inflight = vq->resubmit_inflight;
1006 rte_vhost_set_inflight_desc_split(int vid, uint16_t vring_idx,
1009 struct vhost_virtqueue *vq;
1010 struct virtio_net *dev;
1012 dev = get_device(vid);
1016 if (unlikely(!(dev->protocol_features &
1017 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1020 if (unlikely(vq_is_packed(dev)))
1023 if (unlikely(vring_idx >= VHOST_MAX_VRING))
1026 vq = dev->virtqueue[vring_idx];
1030 if (unlikely(!vq->inflight_split))
1033 if (unlikely(idx >= vq->size))
1036 vq->inflight_split->desc[idx].counter = vq->global_counter++;
1037 vq->inflight_split->desc[idx].inflight = 1;
1042 rte_vhost_set_inflight_desc_packed(int vid, uint16_t vring_idx,
1043 uint16_t head, uint16_t last,
1044 uint16_t *inflight_entry)
1046 struct rte_vhost_inflight_info_packed *inflight_info;
1047 struct virtio_net *dev;
1048 struct vhost_virtqueue *vq;
1049 struct vring_packed_desc *desc;
1050 uint16_t old_free_head, free_head;
1052 dev = get_device(vid);
1056 if (unlikely(!(dev->protocol_features &
1057 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1060 if (unlikely(!vq_is_packed(dev)))
1063 if (unlikely(vring_idx >= VHOST_MAX_VRING))
1066 vq = dev->virtqueue[vring_idx];
1070 inflight_info = vq->inflight_packed;
1071 if (unlikely(!inflight_info))
1074 if (unlikely(head >= vq->size))
1077 desc = vq->desc_packed;
1078 old_free_head = inflight_info->old_free_head;
1079 if (unlikely(old_free_head >= vq->size))
1082 free_head = old_free_head;
1084 /* init header descriptor */
1085 inflight_info->desc[old_free_head].num = 0;
1086 inflight_info->desc[old_free_head].counter = vq->global_counter++;
1087 inflight_info->desc[old_free_head].inflight = 1;
1089 /* save desc entry in flight entry */
1090 while (head != ((last + 1) % vq->size)) {
1091 inflight_info->desc[old_free_head].num++;
1092 inflight_info->desc[free_head].addr = desc[head].addr;
1093 inflight_info->desc[free_head].len = desc[head].len;
1094 inflight_info->desc[free_head].flags = desc[head].flags;
1095 inflight_info->desc[free_head].id = desc[head].id;
1097 inflight_info->desc[old_free_head].last = free_head;
1098 free_head = inflight_info->desc[free_head].next;
1099 inflight_info->free_head = free_head;
1100 head = (head + 1) % vq->size;
1103 inflight_info->old_free_head = free_head;
1104 *inflight_entry = old_free_head;
1110 rte_vhost_clr_inflight_desc_split(int vid, uint16_t vring_idx,
1111 uint16_t last_used_idx, uint16_t idx)
1113 struct virtio_net *dev;
1114 struct vhost_virtqueue *vq;
1116 dev = get_device(vid);
1120 if (unlikely(!(dev->protocol_features &
1121 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1124 if (unlikely(vq_is_packed(dev)))
1127 if (unlikely(vring_idx >= VHOST_MAX_VRING))
1130 vq = dev->virtqueue[vring_idx];
1134 if (unlikely(!vq->inflight_split))
1137 if (unlikely(idx >= vq->size))
1140 rte_atomic_thread_fence(__ATOMIC_SEQ_CST);
1142 vq->inflight_split->desc[idx].inflight = 0;
1144 rte_atomic_thread_fence(__ATOMIC_SEQ_CST);
1146 vq->inflight_split->used_idx = last_used_idx;
1151 rte_vhost_clr_inflight_desc_packed(int vid, uint16_t vring_idx,
1154 struct rte_vhost_inflight_info_packed *inflight_info;
1155 struct virtio_net *dev;
1156 struct vhost_virtqueue *vq;
1158 dev = get_device(vid);
1162 if (unlikely(!(dev->protocol_features &
1163 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1166 if (unlikely(!vq_is_packed(dev)))
1169 if (unlikely(vring_idx >= VHOST_MAX_VRING))
1172 vq = dev->virtqueue[vring_idx];
1176 inflight_info = vq->inflight_packed;
1177 if (unlikely(!inflight_info))
1180 if (unlikely(head >= vq->size))
1183 rte_atomic_thread_fence(__ATOMIC_SEQ_CST);
1185 inflight_info->desc[head].inflight = 0;
1187 rte_atomic_thread_fence(__ATOMIC_SEQ_CST);
1189 inflight_info->old_free_head = inflight_info->free_head;
1190 inflight_info->old_used_idx = inflight_info->used_idx;
1191 inflight_info->old_used_wrap_counter = inflight_info->used_wrap_counter;
1197 rte_vhost_set_last_inflight_io_split(int vid, uint16_t vring_idx,
1200 struct virtio_net *dev;
1201 struct vhost_virtqueue *vq;
1203 dev = get_device(vid);
1207 if (unlikely(!(dev->protocol_features &
1208 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1211 if (unlikely(vq_is_packed(dev)))
1214 if (unlikely(vring_idx >= VHOST_MAX_VRING))
1217 vq = dev->virtqueue[vring_idx];
1221 if (unlikely(!vq->inflight_split))
1224 if (unlikely(idx >= vq->size))
1227 vq->inflight_split->last_inflight_io = idx;
1232 rte_vhost_set_last_inflight_io_packed(int vid, uint16_t vring_idx,
1235 struct rte_vhost_inflight_info_packed *inflight_info;
1236 struct virtio_net *dev;
1237 struct vhost_virtqueue *vq;
1240 dev = get_device(vid);
1244 if (unlikely(!(dev->protocol_features &
1245 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1248 if (unlikely(!vq_is_packed(dev)))
1251 if (unlikely(vring_idx >= VHOST_MAX_VRING))
1254 vq = dev->virtqueue[vring_idx];
1258 inflight_info = vq->inflight_packed;
1259 if (unlikely(!inflight_info))
1262 if (unlikely(head >= vq->size))
1265 last = inflight_info->desc[head].last;
1266 if (unlikely(last >= vq->size))
1269 inflight_info->desc[last].next = inflight_info->free_head;
1270 inflight_info->free_head = head;
1271 inflight_info->used_idx += inflight_info->desc[head].num;
1272 if (inflight_info->used_idx >= inflight_info->desc_num) {
1273 inflight_info->used_idx -= inflight_info->desc_num;
1274 inflight_info->used_wrap_counter =
1275 !inflight_info->used_wrap_counter;
1282 rte_vhost_vring_call(int vid, uint16_t vring_idx)
1284 struct virtio_net *dev;
1285 struct vhost_virtqueue *vq;
1287 dev = get_device(vid);
1291 if (vring_idx >= VHOST_MAX_VRING)
1294 vq = dev->virtqueue[vring_idx];
1298 if (vq_is_packed(dev))
1299 vhost_vring_call_packed(dev, vq);
1301 vhost_vring_call_split(dev, vq);
1307 rte_vhost_avail_entries(int vid, uint16_t queue_id)
1309 struct virtio_net *dev;
1310 struct vhost_virtqueue *vq;
1313 dev = get_device(vid);
1317 if (queue_id >= VHOST_MAX_VRING)
1320 vq = dev->virtqueue[queue_id];
1324 rte_spinlock_lock(&vq->access_lock);
1326 if (unlikely(!vq->enabled || vq->avail == NULL))
1329 ret = *(volatile uint16_t *)&vq->avail->idx - vq->last_used_idx;
1332 rte_spinlock_unlock(&vq->access_lock);
1337 vhost_enable_notify_split(struct virtio_net *dev,
1338 struct vhost_virtqueue *vq, int enable)
1340 if (vq->used == NULL)
1343 if (!(dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))) {
1345 vq->used->flags &= ~VRING_USED_F_NO_NOTIFY;
1347 vq->used->flags |= VRING_USED_F_NO_NOTIFY;
1350 vhost_avail_event(vq) = vq->last_avail_idx;
1356 vhost_enable_notify_packed(struct virtio_net *dev,
1357 struct vhost_virtqueue *vq, int enable)
1361 if (vq->device_event == NULL)
1365 vq->device_event->flags = VRING_EVENT_F_DISABLE;
1369 flags = VRING_EVENT_F_ENABLE;
1370 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX)) {
1371 flags = VRING_EVENT_F_DESC;
1372 vq->device_event->off_wrap = vq->last_avail_idx |
1373 vq->avail_wrap_counter << 15;
1376 rte_atomic_thread_fence(__ATOMIC_RELEASE);
1378 vq->device_event->flags = flags;
1383 vhost_enable_guest_notification(struct virtio_net *dev,
1384 struct vhost_virtqueue *vq, int enable)
1387 * If the virtqueue is not ready yet, it will be applied
1388 * when it will become ready.
1393 if (vq_is_packed(dev))
1394 return vhost_enable_notify_packed(dev, vq, enable);
1396 return vhost_enable_notify_split(dev, vq, enable);
1400 rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable)
1402 struct virtio_net *dev = get_device(vid);
1403 struct vhost_virtqueue *vq;
1409 if (queue_id >= VHOST_MAX_VRING)
1412 vq = dev->virtqueue[queue_id];
1416 rte_spinlock_lock(&vq->access_lock);
1418 vq->notif_enable = enable;
1419 ret = vhost_enable_guest_notification(dev, vq, enable);
1421 rte_spinlock_unlock(&vq->access_lock);
1427 rte_vhost_log_write(int vid, uint64_t addr, uint64_t len)
1429 struct virtio_net *dev = get_device(vid);
1434 vhost_log_write(dev, addr, len);
1438 rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
1439 uint64_t offset, uint64_t len)
1441 struct virtio_net *dev;
1442 struct vhost_virtqueue *vq;
1444 dev = get_device(vid);
1448 if (vring_idx >= VHOST_MAX_VRING)
1450 vq = dev->virtqueue[vring_idx];
1454 vhost_log_used_vring(dev, vq, offset, len);
1458 rte_vhost_rx_queue_count(int vid, uint16_t qid)
1460 struct virtio_net *dev;
1461 struct vhost_virtqueue *vq;
1464 dev = get_device(vid);
1468 if (unlikely(qid >= dev->nr_vring || (qid & 1) == 0)) {
1469 VHOST_LOG_DATA(ERR, "(%s) %s: invalid virtqueue idx %d.\n",
1470 dev->ifname, __func__, qid);
1474 vq = dev->virtqueue[qid];
1478 rte_spinlock_lock(&vq->access_lock);
1480 if (unlikely(!vq->enabled || vq->avail == NULL))
1483 ret = *((volatile uint16_t *)&vq->avail->idx) - vq->last_avail_idx;
1486 rte_spinlock_unlock(&vq->access_lock);
1490 struct rte_vdpa_device *
1491 rte_vhost_get_vdpa_device(int vid)
1493 struct virtio_net *dev = get_device(vid);
1498 return dev->vdpa_dev;
1502 rte_vhost_get_log_base(int vid, uint64_t *log_base,
1505 struct virtio_net *dev = get_device(vid);
1507 if (dev == NULL || log_base == NULL || log_size == NULL)
1510 *log_base = dev->log_base;
1511 *log_size = dev->log_size;
1517 rte_vhost_get_vring_base(int vid, uint16_t queue_id,
1518 uint16_t *last_avail_idx, uint16_t *last_used_idx)
1520 struct vhost_virtqueue *vq;
1521 struct virtio_net *dev = get_device(vid);
1523 if (dev == NULL || last_avail_idx == NULL || last_used_idx == NULL)
1526 if (queue_id >= VHOST_MAX_VRING)
1529 vq = dev->virtqueue[queue_id];
1533 if (vq_is_packed(dev)) {
1534 *last_avail_idx = (vq->avail_wrap_counter << 15) |
1536 *last_used_idx = (vq->used_wrap_counter << 15) |
1539 *last_avail_idx = vq->last_avail_idx;
1540 *last_used_idx = vq->last_used_idx;
1547 rte_vhost_set_vring_base(int vid, uint16_t queue_id,
1548 uint16_t last_avail_idx, uint16_t last_used_idx)
1550 struct vhost_virtqueue *vq;
1551 struct virtio_net *dev = get_device(vid);
1556 if (queue_id >= VHOST_MAX_VRING)
1559 vq = dev->virtqueue[queue_id];
1563 if (vq_is_packed(dev)) {
1564 vq->last_avail_idx = last_avail_idx & 0x7fff;
1565 vq->avail_wrap_counter = !!(last_avail_idx & (1 << 15));
1566 vq->last_used_idx = last_used_idx & 0x7fff;
1567 vq->used_wrap_counter = !!(last_used_idx & (1 << 15));
1569 vq->last_avail_idx = last_avail_idx;
1570 vq->last_used_idx = last_used_idx;
1577 rte_vhost_get_vring_base_from_inflight(int vid,
1579 uint16_t *last_avail_idx,
1580 uint16_t *last_used_idx)
1582 struct rte_vhost_inflight_info_packed *inflight_info;
1583 struct vhost_virtqueue *vq;
1584 struct virtio_net *dev = get_device(vid);
1586 if (dev == NULL || last_avail_idx == NULL || last_used_idx == NULL)
1589 if (queue_id >= VHOST_MAX_VRING)
1592 vq = dev->virtqueue[queue_id];
1596 if (!vq_is_packed(dev))
1599 inflight_info = vq->inflight_packed;
1603 *last_avail_idx = (inflight_info->old_used_wrap_counter << 15) |
1604 inflight_info->old_used_idx;
1605 *last_used_idx = *last_avail_idx;
1611 rte_vhost_extern_callback_register(int vid,
1612 struct rte_vhost_user_extern_ops const * const ops, void *ctx)
1614 struct virtio_net *dev = get_device(vid);
1616 if (dev == NULL || ops == NULL)
1619 dev->extern_ops = *ops;
1620 dev->extern_data = ctx;
1624 static __rte_always_inline int
1625 async_channel_register(int vid, uint16_t queue_id)
1627 struct virtio_net *dev = get_device(vid);
1628 struct vhost_virtqueue *vq = dev->virtqueue[queue_id];
1629 struct vhost_async *async;
1630 int node = vq->numa_node;
1632 if (unlikely(vq->async)) {
1633 VHOST_LOG_CONFIG(ERR,
1634 "(%s) async register failed: already registered (qid: %d)\n",
1635 dev->ifname, queue_id);
1639 async = rte_zmalloc_socket(NULL, sizeof(struct vhost_async), 0, node);
1641 VHOST_LOG_CONFIG(ERR, "(%s) failed to allocate async metadata (qid: %d)\n",
1642 dev->ifname, queue_id);
1646 async->pkts_info = rte_malloc_socket(NULL, vq->size * sizeof(struct async_inflight_info),
1647 RTE_CACHE_LINE_SIZE, node);
1648 if (!async->pkts_info) {
1649 VHOST_LOG_CONFIG(ERR, "(%s) failed to allocate async_pkts_info (qid: %d)\n",
1650 dev->ifname, queue_id);
1651 goto out_free_async;
1654 async->pkts_cmpl_flag = rte_zmalloc_socket(NULL, vq->size * sizeof(bool),
1655 RTE_CACHE_LINE_SIZE, node);
1656 if (!async->pkts_cmpl_flag) {
1657 VHOST_LOG_CONFIG(ERR, "(%s) failed to allocate async pkts_cmpl_flag (qid: %d)\n",
1658 dev->ifname, queue_id);
1659 goto out_free_async;
1662 if (vq_is_packed(dev)) {
1663 async->buffers_packed = rte_malloc_socket(NULL,
1664 vq->size * sizeof(struct vring_used_elem_packed),
1665 RTE_CACHE_LINE_SIZE, node);
1666 if (!async->buffers_packed) {
1667 VHOST_LOG_CONFIG(ERR, "(%s) failed to allocate async buffers (qid: %d)\n",
1668 dev->ifname, queue_id);
1669 goto out_free_inflight;
1672 async->descs_split = rte_malloc_socket(NULL,
1673 vq->size * sizeof(struct vring_used_elem),
1674 RTE_CACHE_LINE_SIZE, node);
1675 if (!async->descs_split) {
1676 VHOST_LOG_CONFIG(ERR, "(%s) failed to allocate async descs (qid: %d)\n",
1677 dev->ifname, queue_id);
1678 goto out_free_inflight;
1686 rte_free(async->pkts_info);
1694 rte_vhost_async_channel_register(int vid, uint16_t queue_id)
1696 struct vhost_virtqueue *vq;
1697 struct virtio_net *dev = get_device(vid);
1703 if (queue_id >= VHOST_MAX_VRING)
1706 vq = dev->virtqueue[queue_id];
1708 if (unlikely(vq == NULL || !dev->async_copy))
1711 rte_spinlock_lock(&vq->access_lock);
1712 ret = async_channel_register(vid, queue_id);
1713 rte_spinlock_unlock(&vq->access_lock);
1719 rte_vhost_async_channel_register_thread_unsafe(int vid, uint16_t queue_id)
1721 struct vhost_virtqueue *vq;
1722 struct virtio_net *dev = get_device(vid);
1727 if (queue_id >= VHOST_MAX_VRING)
1730 vq = dev->virtqueue[queue_id];
1732 if (unlikely(vq == NULL || !dev->async_copy))
1735 return async_channel_register(vid, queue_id);
1739 rte_vhost_async_channel_unregister(int vid, uint16_t queue_id)
1741 struct vhost_virtqueue *vq;
1742 struct virtio_net *dev = get_device(vid);
1748 if (queue_id >= VHOST_MAX_VRING)
1751 vq = dev->virtqueue[queue_id];
1761 if (!rte_spinlock_trylock(&vq->access_lock)) {
1762 VHOST_LOG_CONFIG(ERR, "(%s) failed to unregister async channel, virtqueue busy.\n",
1767 if (vq->async->pkts_inflight_n) {
1768 VHOST_LOG_CONFIG(ERR, "(%s) failed to unregister async channel.\n", dev->ifname);
1769 VHOST_LOG_CONFIG(ERR, "(%s) inflight packets must be completed before unregistration.\n",
1775 vhost_free_async_mem(vq);
1777 rte_spinlock_unlock(&vq->access_lock);
1783 rte_vhost_async_channel_unregister_thread_unsafe(int vid, uint16_t queue_id)
1785 struct vhost_virtqueue *vq;
1786 struct virtio_net *dev = get_device(vid);
1791 if (queue_id >= VHOST_MAX_VRING)
1794 vq = dev->virtqueue[queue_id];
1802 if (vq->async->pkts_inflight_n) {
1803 VHOST_LOG_CONFIG(ERR, "(%s) failed to unregister async channel.\n", dev->ifname);
1804 VHOST_LOG_CONFIG(ERR, "(%s) inflight packets must be completed before unregistration.\n",
1809 vhost_free_async_mem(vq);
1815 rte_vhost_async_dma_configure(int16_t dma_id, uint16_t vchan_id)
1817 struct rte_dma_info info;
1818 void *pkts_cmpl_flag_addr;
1821 if (!rte_dma_is_valid(dma_id)) {
1822 VHOST_LOG_CONFIG(ERR, "DMA %d is not found.\n", dma_id);
1826 rte_dma_info_get(dma_id, &info);
1827 if (vchan_id >= info.max_vchans) {
1828 VHOST_LOG_CONFIG(ERR, "Invalid DMA %d vChannel %u.\n", dma_id, vchan_id);
1832 if (!dma_copy_track[dma_id].vchans) {
1833 struct async_dma_vchan_info *vchans;
1835 vchans = rte_zmalloc(NULL, sizeof(struct async_dma_vchan_info) * info.max_vchans,
1836 RTE_CACHE_LINE_SIZE);
1837 if (vchans == NULL) {
1838 VHOST_LOG_CONFIG(ERR, "Failed to allocate vchans for DMA %d vChannel %u.\n",
1843 dma_copy_track[dma_id].vchans = vchans;
1846 if (dma_copy_track[dma_id].vchans[vchan_id].pkts_cmpl_flag_addr) {
1847 VHOST_LOG_CONFIG(INFO, "DMA %d vChannel %u already registered.\n", dma_id,
1852 max_desc = info.max_desc;
1853 if (!rte_is_power_of_2(max_desc))
1854 max_desc = rte_align32pow2(max_desc);
1856 pkts_cmpl_flag_addr = rte_zmalloc(NULL, sizeof(bool *) * max_desc, RTE_CACHE_LINE_SIZE);
1857 if (!pkts_cmpl_flag_addr) {
1858 VHOST_LOG_CONFIG(ERR, "Failed to allocate pkts_cmpl_flag_addr for DMA %d "
1859 "vChannel %u.\n", dma_id, vchan_id);
1861 if (dma_copy_track[dma_id].nr_vchans == 0) {
1862 rte_free(dma_copy_track[dma_id].vchans);
1863 dma_copy_track[dma_id].vchans = NULL;
1868 dma_copy_track[dma_id].vchans[vchan_id].pkts_cmpl_flag_addr = pkts_cmpl_flag_addr;
1869 dma_copy_track[dma_id].vchans[vchan_id].ring_size = max_desc;
1870 dma_copy_track[dma_id].vchans[vchan_id].ring_mask = max_desc - 1;
1871 dma_copy_track[dma_id].nr_vchans++;
1877 rte_vhost_async_get_inflight(int vid, uint16_t queue_id)
1879 struct vhost_virtqueue *vq;
1880 struct virtio_net *dev = get_device(vid);
1886 if (queue_id >= VHOST_MAX_VRING)
1889 vq = dev->virtqueue[queue_id];
1897 if (!rte_spinlock_trylock(&vq->access_lock)) {
1898 VHOST_LOG_CONFIG(DEBUG,
1899 "(%s) failed to check in-flight packets. virtqueue busy.\n",
1904 ret = vq->async->pkts_inflight_n;
1905 rte_spinlock_unlock(&vq->access_lock);
1911 rte_vhost_get_monitor_addr(int vid, uint16_t queue_id,
1912 struct rte_vhost_power_monitor_cond *pmc)
1914 struct virtio_net *dev = get_device(vid);
1915 struct vhost_virtqueue *vq;
1919 if (queue_id >= VHOST_MAX_VRING)
1922 vq = dev->virtqueue[queue_id];
1926 if (vq_is_packed(dev)) {
1927 struct vring_packed_desc *desc;
1928 desc = vq->desc_packed;
1929 pmc->addr = &desc[vq->last_avail_idx].flags;
1930 if (vq->avail_wrap_counter)
1931 pmc->val = VRING_DESC_F_AVAIL;
1933 pmc->val = VRING_DESC_F_USED;
1934 pmc->mask = VRING_DESC_F_AVAIL | VRING_DESC_F_USED;
1935 pmc->size = sizeof(desc[vq->last_avail_idx].flags);
1938 pmc->addr = &vq->avail->idx;
1939 pmc->val = vq->last_avail_idx & (vq->size - 1);
1940 pmc->mask = vq->size - 1;
1941 pmc->size = sizeof(vq->avail->idx);
1948 RTE_LOG_REGISTER_SUFFIX(vhost_config_log_level, config, INFO);
1949 RTE_LOG_REGISTER_SUFFIX(vhost_data_log_level, data, WARNING);