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>
9 #ifdef RTE_LIBRTE_VHOST_NUMA
14 #include <rte_errno.h>
16 #include <rte_memory.h>
17 #include <rte_malloc.h>
18 #include <rte_vhost.h>
22 #include "vhost_user.h"
24 struct virtio_net *vhost_devices[RTE_MAX_VHOST_DEVICE];
25 pthread_mutex_t vhost_dev_lock = PTHREAD_MUTEX_INITIALIZER;
27 struct vhost_vq_stats_name_off {
28 char name[RTE_VHOST_STATS_NAME_SIZE];
32 static const struct vhost_vq_stats_name_off vhost_vq_stat_strings[] = {
33 {"good_packets", offsetof(struct vhost_virtqueue, stats.packets)},
34 {"good_bytes", offsetof(struct vhost_virtqueue, stats.bytes)},
35 {"multicast_packets", offsetof(struct vhost_virtqueue, stats.multicast)},
36 {"broadcast_packets", offsetof(struct vhost_virtqueue, stats.broadcast)},
37 {"undersize_packets", offsetof(struct vhost_virtqueue, stats.size_bins[0])},
38 {"size_64_packets", offsetof(struct vhost_virtqueue, stats.size_bins[1])},
39 {"size_65_127_packets", offsetof(struct vhost_virtqueue, stats.size_bins[2])},
40 {"size_128_255_packets", offsetof(struct vhost_virtqueue, stats.size_bins[3])},
41 {"size_256_511_packets", offsetof(struct vhost_virtqueue, stats.size_bins[4])},
42 {"size_512_1023_packets", offsetof(struct vhost_virtqueue, stats.size_bins[5])},
43 {"size_1024_1518_packets", offsetof(struct vhost_virtqueue, stats.size_bins[6])},
44 {"size_1519_max_packets", offsetof(struct vhost_virtqueue, stats.size_bins[7])},
45 {"guest_notifications", offsetof(struct vhost_virtqueue, stats.guest_notifications)},
46 {"iotlb_hits", offsetof(struct vhost_virtqueue, stats.iotlb_hits)},
47 {"iotlb_misses", offsetof(struct vhost_virtqueue, stats.iotlb_misses)},
50 #define VHOST_NB_VQ_STATS RTE_DIM(vhost_vq_stat_strings)
52 /* Called with iotlb_lock read-locked */
54 __vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
55 uint64_t iova, uint64_t *size, uint8_t perm)
57 uint64_t vva, tmp_size;
64 vva = vhost_user_iotlb_cache_find(vq, iova, &tmp_size, perm);
65 if (tmp_size == *size) {
66 if (dev->flags & VIRTIO_DEV_STATS_ENABLED)
67 vq->stats.iotlb_hits++;
71 if (dev->flags & VIRTIO_DEV_STATS_ENABLED)
72 vq->stats.iotlb_misses++;
76 if (!vhost_user_iotlb_pending_miss(vq, iova, perm)) {
78 * iotlb_lock is read-locked for a full burst,
79 * but it only protects the iotlb cache.
80 * In case of IOTLB miss, we might block on the socket,
81 * which could cause a deadlock with QEMU if an IOTLB update
82 * is being handled. We can safely unlock here to avoid it.
84 vhost_user_iotlb_rd_unlock(vq);
86 vhost_user_iotlb_pending_insert(dev, vq, iova, perm);
87 if (vhost_user_iotlb_miss(dev, iova, perm)) {
88 VHOST_LOG_DATA(ERR, "(%s) IOTLB miss req failed for IOVA 0x%" PRIx64 "\n",
90 vhost_user_iotlb_pending_remove(vq, iova, 1, perm);
93 vhost_user_iotlb_rd_lock(vq);
99 #define VHOST_LOG_PAGE 4096
102 * Atomically set a bit in memory.
104 static __rte_always_inline void
105 vhost_set_bit(unsigned int nr, volatile uint8_t *addr)
107 #if defined(RTE_TOOLCHAIN_GCC) && (GCC_VERSION < 70100)
109 * __sync_ built-ins are deprecated, but __atomic_ ones
110 * are sub-optimized in older GCC versions.
112 __sync_fetch_and_or_1(addr, (1U << nr));
114 __atomic_fetch_or(addr, (1U << nr), __ATOMIC_RELAXED);
118 static __rte_always_inline void
119 vhost_log_page(uint8_t *log_base, uint64_t page)
121 vhost_set_bit(page % 8, &log_base[page / 8]);
125 __vhost_log_write(struct virtio_net *dev, uint64_t addr, uint64_t len)
129 if (unlikely(!dev->log_base || !len))
132 if (unlikely(dev->log_size <= ((addr + len - 1) / VHOST_LOG_PAGE / 8)))
135 /* To make sure guest memory updates are committed before logging */
136 rte_atomic_thread_fence(__ATOMIC_RELEASE);
138 page = addr / VHOST_LOG_PAGE;
139 while (page * VHOST_LOG_PAGE < addr + len) {
140 vhost_log_page((uint8_t *)(uintptr_t)dev->log_base, page);
146 __vhost_log_write_iova(struct virtio_net *dev, struct vhost_virtqueue *vq,
147 uint64_t iova, uint64_t len)
149 uint64_t hva, gpa, map_len;
152 hva = __vhost_iova_to_vva(dev, vq, iova, &map_len, VHOST_ACCESS_RW);
153 if (map_len != len) {
155 "(%s) failed to write log for IOVA 0x%" PRIx64 ". No IOTLB entry found\n",
160 gpa = hva_to_gpa(dev, hva, len);
162 __vhost_log_write(dev, gpa, len);
166 __vhost_log_cache_sync(struct virtio_net *dev, struct vhost_virtqueue *vq)
168 unsigned long *log_base;
171 if (unlikely(!dev->log_base))
174 /* No cache, nothing to sync */
175 if (unlikely(!vq->log_cache))
178 rte_atomic_thread_fence(__ATOMIC_RELEASE);
180 log_base = (unsigned long *)(uintptr_t)dev->log_base;
182 for (i = 0; i < vq->log_cache_nb_elem; i++) {
183 struct log_cache_entry *elem = vq->log_cache + i;
185 #if defined(RTE_TOOLCHAIN_GCC) && (GCC_VERSION < 70100)
187 * '__sync' builtins are deprecated, but '__atomic' ones
188 * are sub-optimized in older GCC versions.
190 __sync_fetch_and_or(log_base + elem->offset, elem->val);
192 __atomic_fetch_or(log_base + elem->offset, elem->val,
197 rte_atomic_thread_fence(__ATOMIC_RELEASE);
199 vq->log_cache_nb_elem = 0;
202 static __rte_always_inline void
203 vhost_log_cache_page(struct virtio_net *dev, struct vhost_virtqueue *vq,
206 uint32_t bit_nr = page % (sizeof(unsigned long) << 3);
207 uint32_t offset = page / (sizeof(unsigned long) << 3);
210 if (unlikely(!vq->log_cache)) {
211 /* No logging cache allocated, write dirty log map directly */
212 rte_atomic_thread_fence(__ATOMIC_RELEASE);
213 vhost_log_page((uint8_t *)(uintptr_t)dev->log_base, page);
218 for (i = 0; i < vq->log_cache_nb_elem; i++) {
219 struct log_cache_entry *elem = vq->log_cache + i;
221 if (elem->offset == offset) {
222 elem->val |= (1UL << bit_nr);
227 if (unlikely(i >= VHOST_LOG_CACHE_NR)) {
229 * No more room for a new log cache entry,
230 * so write the dirty log map directly.
232 rte_atomic_thread_fence(__ATOMIC_RELEASE);
233 vhost_log_page((uint8_t *)(uintptr_t)dev->log_base, page);
238 vq->log_cache[i].offset = offset;
239 vq->log_cache[i].val = (1UL << bit_nr);
240 vq->log_cache_nb_elem++;
244 __vhost_log_cache_write(struct virtio_net *dev, struct vhost_virtqueue *vq,
245 uint64_t addr, uint64_t len)
249 if (unlikely(!dev->log_base || !len))
252 if (unlikely(dev->log_size <= ((addr + len - 1) / VHOST_LOG_PAGE / 8)))
255 page = addr / VHOST_LOG_PAGE;
256 while (page * VHOST_LOG_PAGE < addr + len) {
257 vhost_log_cache_page(dev, vq, page);
263 __vhost_log_cache_write_iova(struct virtio_net *dev, struct vhost_virtqueue *vq,
264 uint64_t iova, uint64_t len)
266 uint64_t hva, gpa, map_len;
269 hva = __vhost_iova_to_vva(dev, vq, iova, &map_len, VHOST_ACCESS_RW);
270 if (map_len != len) {
272 "(%s) failed to write log for IOVA 0x%" PRIx64 ". No IOTLB entry found\n",
277 gpa = hva_to_gpa(dev, hva, len);
279 __vhost_log_cache_write(dev, vq, gpa, len);
283 vhost_alloc_copy_ind_table(struct virtio_net *dev, struct vhost_virtqueue *vq,
284 uint64_t desc_addr, uint64_t desc_len)
288 uint64_t len, remain = desc_len;
290 idesc = rte_malloc_socket(__func__, desc_len, 0, vq->numa_node);
291 if (unlikely(!idesc))
294 dst = (uint64_t)(uintptr_t)idesc;
298 src = vhost_iova_to_vva(dev, vq, desc_addr, &len,
300 if (unlikely(!src || !len)) {
305 rte_memcpy((void *)(uintptr_t)dst, (void *)(uintptr_t)src, len);
316 cleanup_vq(struct vhost_virtqueue *vq, int destroy)
318 if ((vq->callfd >= 0) && (destroy != 0))
325 cleanup_vq_inflight(struct virtio_net *dev, struct vhost_virtqueue *vq)
327 if (!(dev->protocol_features &
328 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD)))
331 if (vq_is_packed(dev)) {
332 if (vq->inflight_packed)
333 vq->inflight_packed = NULL;
335 if (vq->inflight_split)
336 vq->inflight_split = NULL;
339 if (vq->resubmit_inflight) {
340 if (vq->resubmit_inflight->resubmit_list) {
341 rte_free(vq->resubmit_inflight->resubmit_list);
342 vq->resubmit_inflight->resubmit_list = NULL;
344 rte_free(vq->resubmit_inflight);
345 vq->resubmit_inflight = NULL;
350 * Unmap any memory, close any file descriptors and
351 * free any memory owned by a device.
354 cleanup_device(struct virtio_net *dev, int destroy)
358 vhost_backend_cleanup(dev);
360 for (i = 0; i < dev->nr_vring; i++) {
361 cleanup_vq(dev->virtqueue[i], destroy);
362 cleanup_vq_inflight(dev, dev->virtqueue[i]);
367 vhost_free_async_mem(struct vhost_virtqueue *vq)
372 rte_free(vq->async->pkts_info);
373 rte_free(vq->async->pkts_cmpl_flag);
375 rte_free(vq->async->buffers_packed);
376 vq->async->buffers_packed = NULL;
377 rte_free(vq->async->descs_split);
378 vq->async->descs_split = NULL;
385 free_vq(struct virtio_net *dev, struct vhost_virtqueue *vq)
387 if (vq_is_packed(dev))
388 rte_free(vq->shadow_used_packed);
390 rte_free(vq->shadow_used_split);
392 vhost_free_async_mem(vq);
393 rte_free(vq->batch_copy_elems);
394 rte_mempool_free(vq->iotlb_pool);
395 rte_free(vq->log_cache);
400 * Release virtqueues and device memory.
403 free_device(struct virtio_net *dev)
407 for (i = 0; i < dev->nr_vring; i++)
408 free_vq(dev, dev->virtqueue[i]);
413 static __rte_always_inline int
414 log_translate(struct virtio_net *dev, struct vhost_virtqueue *vq)
416 if (likely(!(vq->ring_addrs.flags & (1 << VHOST_VRING_F_LOG))))
419 vq->log_guest_addr = translate_log_addr(dev, vq,
420 vq->ring_addrs.log_guest_addr);
421 if (vq->log_guest_addr == 0)
428 * Converts vring log address to GPA
429 * If IOMMU is enabled, the log address is IOVA
430 * If IOMMU not enabled, the log address is already GPA
432 * Caller should have iotlb_lock read-locked
435 translate_log_addr(struct virtio_net *dev, struct vhost_virtqueue *vq,
438 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)) {
439 const uint64_t exp_size = sizeof(uint64_t);
441 uint64_t size = exp_size;
443 hva = vhost_iova_to_vva(dev, vq, log_addr,
444 &size, VHOST_ACCESS_RW);
446 if (size != exp_size)
449 gpa = hva_to_gpa(dev, hva, exp_size);
452 "(%s) failed to find GPA for log_addr: 0x%"
453 PRIx64 " hva: 0x%" PRIx64 "\n",
454 dev->ifname, log_addr, hva);
463 /* Caller should have iotlb_lock read-locked */
465 vring_translate_split(struct virtio_net *dev, struct vhost_virtqueue *vq)
467 uint64_t req_size, size;
469 req_size = sizeof(struct vring_desc) * vq->size;
471 vq->desc = (struct vring_desc *)(uintptr_t)vhost_iova_to_vva(dev, vq,
472 vq->ring_addrs.desc_user_addr,
473 &size, VHOST_ACCESS_RW);
474 if (!vq->desc || size != req_size)
477 req_size = sizeof(struct vring_avail);
478 req_size += sizeof(uint16_t) * vq->size;
479 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))
480 req_size += sizeof(uint16_t);
482 vq->avail = (struct vring_avail *)(uintptr_t)vhost_iova_to_vva(dev, vq,
483 vq->ring_addrs.avail_user_addr,
484 &size, VHOST_ACCESS_RW);
485 if (!vq->avail || size != req_size)
488 req_size = sizeof(struct vring_used);
489 req_size += sizeof(struct vring_used_elem) * vq->size;
490 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))
491 req_size += sizeof(uint16_t);
493 vq->used = (struct vring_used *)(uintptr_t)vhost_iova_to_vva(dev, vq,
494 vq->ring_addrs.used_user_addr,
495 &size, VHOST_ACCESS_RW);
496 if (!vq->used || size != req_size)
502 /* Caller should have iotlb_lock read-locked */
504 vring_translate_packed(struct virtio_net *dev, struct vhost_virtqueue *vq)
506 uint64_t req_size, size;
508 req_size = sizeof(struct vring_packed_desc) * vq->size;
510 vq->desc_packed = (struct vring_packed_desc *)(uintptr_t)
511 vhost_iova_to_vva(dev, vq, vq->ring_addrs.desc_user_addr,
512 &size, VHOST_ACCESS_RW);
513 if (!vq->desc_packed || size != req_size)
516 req_size = sizeof(struct vring_packed_desc_event);
518 vq->driver_event = (struct vring_packed_desc_event *)(uintptr_t)
519 vhost_iova_to_vva(dev, vq, vq->ring_addrs.avail_user_addr,
520 &size, VHOST_ACCESS_RW);
521 if (!vq->driver_event || size != req_size)
524 req_size = sizeof(struct vring_packed_desc_event);
526 vq->device_event = (struct vring_packed_desc_event *)(uintptr_t)
527 vhost_iova_to_vva(dev, vq, vq->ring_addrs.used_user_addr,
528 &size, VHOST_ACCESS_RW);
529 if (!vq->device_event || size != req_size)
536 vring_translate(struct virtio_net *dev, struct vhost_virtqueue *vq)
539 if (!(dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)))
542 if (vq_is_packed(dev)) {
543 if (vring_translate_packed(dev, vq) < 0)
546 if (vring_translate_split(dev, vq) < 0)
550 if (log_translate(dev, vq) < 0)
553 vq->access_ok = true;
559 vring_invalidate(struct virtio_net *dev, struct vhost_virtqueue *vq)
561 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
562 vhost_user_iotlb_wr_lock(vq);
564 vq->access_ok = false;
568 vq->log_guest_addr = 0;
570 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
571 vhost_user_iotlb_wr_unlock(vq);
575 init_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
577 struct vhost_virtqueue *vq;
578 int numa_node = SOCKET_ID_ANY;
580 if (vring_idx >= VHOST_MAX_VRING) {
581 VHOST_LOG_CONFIG(ERR, "(%s) failed to init vring, out of bound (%d)\n",
582 dev->ifname, vring_idx);
586 vq = dev->virtqueue[vring_idx];
588 VHOST_LOG_CONFIG(ERR, "(%s) virtqueue not allocated (%d)\n",
589 dev->ifname, vring_idx);
593 memset(vq, 0, sizeof(struct vhost_virtqueue));
595 vq->kickfd = VIRTIO_UNINITIALIZED_EVENTFD;
596 vq->callfd = VIRTIO_UNINITIALIZED_EVENTFD;
597 vq->notif_enable = VIRTIO_UNINITIALIZED_NOTIF;
599 #ifdef RTE_LIBRTE_VHOST_NUMA
600 if (get_mempolicy(&numa_node, NULL, 0, vq, MPOL_F_NODE | MPOL_F_ADDR)) {
601 VHOST_LOG_CONFIG(ERR, "(%s) failed to query numa node: %s\n",
602 dev->ifname, rte_strerror(errno));
603 numa_node = SOCKET_ID_ANY;
606 vq->numa_node = numa_node;
608 vhost_user_iotlb_init(dev, vring_idx);
612 reset_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
614 struct vhost_virtqueue *vq;
617 if (vring_idx >= VHOST_MAX_VRING) {
618 VHOST_LOG_CONFIG(ERR,
619 "(%s) failed to reset vring, out of bound (%d)\n",
620 dev->ifname, vring_idx);
624 vq = dev->virtqueue[vring_idx];
626 VHOST_LOG_CONFIG(ERR, "(%s) failed to reset vring, virtqueue not allocated (%d)\n",
627 dev->ifname, vring_idx);
632 init_vring_queue(dev, vring_idx);
637 alloc_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
639 struct vhost_virtqueue *vq;
642 /* Also allocate holes, if any, up to requested vring index. */
643 for (i = 0; i <= vring_idx; i++) {
644 if (dev->virtqueue[i])
647 vq = rte_zmalloc(NULL, sizeof(struct vhost_virtqueue), 0);
649 VHOST_LOG_CONFIG(ERR, "(%s) failed to allocate memory for vring %u.\n",
654 dev->virtqueue[i] = vq;
655 init_vring_queue(dev, i);
656 rte_spinlock_init(&vq->access_lock);
657 vq->avail_wrap_counter = 1;
658 vq->used_wrap_counter = 1;
659 vq->signalled_used_valid = false;
662 dev->nr_vring = RTE_MAX(dev->nr_vring, vring_idx + 1);
668 * Reset some variables in device structure, while keeping few
669 * others untouched, such as vid, ifname, nr_vring: they
670 * should be same unless the device is removed.
673 reset_device(struct virtio_net *dev)
678 dev->protocol_features = 0;
679 dev->flags &= VIRTIO_DEV_BUILTIN_VIRTIO_NET;
681 for (i = 0; i < dev->nr_vring; i++)
682 reset_vring_queue(dev, i);
686 * Invoked when there is a new vhost-user connection established (when
687 * there is a new virtio device being attached).
690 vhost_new_device(void)
692 struct virtio_net *dev;
695 pthread_mutex_lock(&vhost_dev_lock);
696 for (i = 0; i < RTE_MAX_VHOST_DEVICE; i++) {
697 if (vhost_devices[i] == NULL)
701 if (i == RTE_MAX_VHOST_DEVICE) {
702 VHOST_LOG_CONFIG(ERR, "failed to find a free slot for new device.\n");
703 pthread_mutex_unlock(&vhost_dev_lock);
707 dev = rte_zmalloc(NULL, sizeof(struct virtio_net), 0);
709 VHOST_LOG_CONFIG(ERR, "failed to allocate memory for new device.\n");
710 pthread_mutex_unlock(&vhost_dev_lock);
714 vhost_devices[i] = dev;
715 pthread_mutex_unlock(&vhost_dev_lock);
718 dev->flags = VIRTIO_DEV_BUILTIN_VIRTIO_NET;
719 dev->slave_req_fd = -1;
720 dev->postcopy_ufd = -1;
721 rte_spinlock_init(&dev->slave_req_lock);
727 vhost_destroy_device_notify(struct virtio_net *dev)
729 struct rte_vdpa_device *vdpa_dev;
731 if (dev->flags & VIRTIO_DEV_RUNNING) {
732 vdpa_dev = dev->vdpa_dev;
734 vdpa_dev->ops->dev_close(dev->vid);
735 dev->flags &= ~VIRTIO_DEV_RUNNING;
736 dev->notify_ops->destroy_device(dev->vid);
741 * Invoked when there is the vhost-user connection is broken (when
742 * the virtio device is being detached).
745 vhost_destroy_device(int vid)
747 struct virtio_net *dev = get_device(vid);
752 vhost_destroy_device_notify(dev);
754 cleanup_device(dev, 1);
757 vhost_devices[vid] = NULL;
761 vhost_attach_vdpa_device(int vid, struct rte_vdpa_device *vdpa_dev)
763 struct virtio_net *dev = get_device(vid);
768 dev->vdpa_dev = vdpa_dev;
772 vhost_set_ifname(int vid, const char *if_name, unsigned int if_len)
774 struct virtio_net *dev;
777 dev = get_device(vid);
781 len = if_len > sizeof(dev->ifname) ?
782 sizeof(dev->ifname) : if_len;
784 strncpy(dev->ifname, if_name, len);
785 dev->ifname[sizeof(dev->ifname) - 1] = '\0';
789 vhost_setup_virtio_net(int vid, bool enable, bool compliant_ol_flags, bool stats_enabled)
791 struct virtio_net *dev = get_device(vid);
797 dev->flags |= VIRTIO_DEV_BUILTIN_VIRTIO_NET;
799 dev->flags &= ~VIRTIO_DEV_BUILTIN_VIRTIO_NET;
800 if (!compliant_ol_flags)
801 dev->flags |= VIRTIO_DEV_LEGACY_OL_FLAGS;
803 dev->flags &= ~VIRTIO_DEV_LEGACY_OL_FLAGS;
805 dev->flags |= VIRTIO_DEV_STATS_ENABLED;
807 dev->flags &= ~VIRTIO_DEV_STATS_ENABLED;
811 vhost_enable_extbuf(int vid)
813 struct virtio_net *dev = get_device(vid);
822 vhost_enable_linearbuf(int vid)
824 struct virtio_net *dev = get_device(vid);
833 rte_vhost_get_mtu(int vid, uint16_t *mtu)
835 struct virtio_net *dev = get_device(vid);
837 if (dev == NULL || mtu == NULL)
840 if (!(dev->flags & VIRTIO_DEV_READY))
843 if (!(dev->features & (1ULL << VIRTIO_NET_F_MTU)))
852 rte_vhost_get_numa_node(int vid)
854 #ifdef RTE_LIBRTE_VHOST_NUMA
855 struct virtio_net *dev = get_device(vid);
859 if (dev == NULL || numa_available() != 0)
862 ret = get_mempolicy(&numa_node, NULL, 0, dev,
863 MPOL_F_NODE | MPOL_F_ADDR);
865 VHOST_LOG_CONFIG(ERR, "(%s) failed to query numa node: %s\n",
866 dev->ifname, rte_strerror(errno));
878 rte_vhost_get_queue_num(int vid)
880 struct virtio_net *dev = get_device(vid);
885 return dev->nr_vring / 2;
889 rte_vhost_get_vring_num(int vid)
891 struct virtio_net *dev = get_device(vid);
896 return dev->nr_vring;
900 rte_vhost_get_ifname(int vid, char *buf, size_t len)
902 struct virtio_net *dev = get_device(vid);
904 if (dev == NULL || buf == NULL)
907 len = RTE_MIN(len, sizeof(dev->ifname));
909 strncpy(buf, dev->ifname, len);
916 rte_vhost_get_negotiated_features(int vid, uint64_t *features)
918 struct virtio_net *dev;
920 dev = get_device(vid);
921 if (dev == NULL || features == NULL)
924 *features = dev->features;
929 rte_vhost_get_negotiated_protocol_features(int vid,
930 uint64_t *protocol_features)
932 struct virtio_net *dev;
934 dev = get_device(vid);
935 if (dev == NULL || protocol_features == NULL)
938 *protocol_features = dev->protocol_features;
943 rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem)
945 struct virtio_net *dev;
946 struct rte_vhost_memory *m;
949 dev = get_device(vid);
950 if (dev == NULL || mem == NULL)
953 size = dev->mem->nregions * sizeof(struct rte_vhost_mem_region);
954 m = malloc(sizeof(struct rte_vhost_memory) + size);
958 m->nregions = dev->mem->nregions;
959 memcpy(m->regions, dev->mem->regions, size);
966 rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
967 struct rte_vhost_vring *vring)
969 struct virtio_net *dev;
970 struct vhost_virtqueue *vq;
972 dev = get_device(vid);
973 if (dev == NULL || vring == NULL)
976 if (vring_idx >= VHOST_MAX_VRING)
979 vq = dev->virtqueue[vring_idx];
983 if (vq_is_packed(dev)) {
984 vring->desc_packed = vq->desc_packed;
985 vring->driver_event = vq->driver_event;
986 vring->device_event = vq->device_event;
988 vring->desc = vq->desc;
989 vring->avail = vq->avail;
990 vring->used = vq->used;
992 vring->log_guest_addr = vq->log_guest_addr;
994 vring->callfd = vq->callfd;
995 vring->kickfd = vq->kickfd;
996 vring->size = vq->size;
1002 rte_vhost_get_vhost_ring_inflight(int vid, uint16_t vring_idx,
1003 struct rte_vhost_ring_inflight *vring)
1005 struct virtio_net *dev;
1006 struct vhost_virtqueue *vq;
1008 dev = get_device(vid);
1012 if (vring_idx >= VHOST_MAX_VRING)
1015 vq = dev->virtqueue[vring_idx];
1019 if (vq_is_packed(dev)) {
1020 if (unlikely(!vq->inflight_packed))
1023 vring->inflight_packed = vq->inflight_packed;
1025 if (unlikely(!vq->inflight_split))
1028 vring->inflight_split = vq->inflight_split;
1031 vring->resubmit_inflight = vq->resubmit_inflight;
1037 rte_vhost_set_inflight_desc_split(int vid, uint16_t vring_idx,
1040 struct vhost_virtqueue *vq;
1041 struct virtio_net *dev;
1043 dev = get_device(vid);
1047 if (unlikely(!(dev->protocol_features &
1048 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1051 if (unlikely(vq_is_packed(dev)))
1054 if (unlikely(vring_idx >= VHOST_MAX_VRING))
1057 vq = dev->virtqueue[vring_idx];
1061 if (unlikely(!vq->inflight_split))
1064 if (unlikely(idx >= vq->size))
1067 vq->inflight_split->desc[idx].counter = vq->global_counter++;
1068 vq->inflight_split->desc[idx].inflight = 1;
1073 rte_vhost_set_inflight_desc_packed(int vid, uint16_t vring_idx,
1074 uint16_t head, uint16_t last,
1075 uint16_t *inflight_entry)
1077 struct rte_vhost_inflight_info_packed *inflight_info;
1078 struct virtio_net *dev;
1079 struct vhost_virtqueue *vq;
1080 struct vring_packed_desc *desc;
1081 uint16_t old_free_head, free_head;
1083 dev = get_device(vid);
1087 if (unlikely(!(dev->protocol_features &
1088 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1091 if (unlikely(!vq_is_packed(dev)))
1094 if (unlikely(vring_idx >= VHOST_MAX_VRING))
1097 vq = dev->virtqueue[vring_idx];
1101 inflight_info = vq->inflight_packed;
1102 if (unlikely(!inflight_info))
1105 if (unlikely(head >= vq->size))
1108 desc = vq->desc_packed;
1109 old_free_head = inflight_info->old_free_head;
1110 if (unlikely(old_free_head >= vq->size))
1113 free_head = old_free_head;
1115 /* init header descriptor */
1116 inflight_info->desc[old_free_head].num = 0;
1117 inflight_info->desc[old_free_head].counter = vq->global_counter++;
1118 inflight_info->desc[old_free_head].inflight = 1;
1120 /* save desc entry in flight entry */
1121 while (head != ((last + 1) % vq->size)) {
1122 inflight_info->desc[old_free_head].num++;
1123 inflight_info->desc[free_head].addr = desc[head].addr;
1124 inflight_info->desc[free_head].len = desc[head].len;
1125 inflight_info->desc[free_head].flags = desc[head].flags;
1126 inflight_info->desc[free_head].id = desc[head].id;
1128 inflight_info->desc[old_free_head].last = free_head;
1129 free_head = inflight_info->desc[free_head].next;
1130 inflight_info->free_head = free_head;
1131 head = (head + 1) % vq->size;
1134 inflight_info->old_free_head = free_head;
1135 *inflight_entry = old_free_head;
1141 rte_vhost_clr_inflight_desc_split(int vid, uint16_t vring_idx,
1142 uint16_t last_used_idx, uint16_t idx)
1144 struct virtio_net *dev;
1145 struct vhost_virtqueue *vq;
1147 dev = get_device(vid);
1151 if (unlikely(!(dev->protocol_features &
1152 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1155 if (unlikely(vq_is_packed(dev)))
1158 if (unlikely(vring_idx >= VHOST_MAX_VRING))
1161 vq = dev->virtqueue[vring_idx];
1165 if (unlikely(!vq->inflight_split))
1168 if (unlikely(idx >= vq->size))
1171 rte_atomic_thread_fence(__ATOMIC_SEQ_CST);
1173 vq->inflight_split->desc[idx].inflight = 0;
1175 rte_atomic_thread_fence(__ATOMIC_SEQ_CST);
1177 vq->inflight_split->used_idx = last_used_idx;
1182 rte_vhost_clr_inflight_desc_packed(int vid, uint16_t vring_idx,
1185 struct rte_vhost_inflight_info_packed *inflight_info;
1186 struct virtio_net *dev;
1187 struct vhost_virtqueue *vq;
1189 dev = get_device(vid);
1193 if (unlikely(!(dev->protocol_features &
1194 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1197 if (unlikely(!vq_is_packed(dev)))
1200 if (unlikely(vring_idx >= VHOST_MAX_VRING))
1203 vq = dev->virtqueue[vring_idx];
1207 inflight_info = vq->inflight_packed;
1208 if (unlikely(!inflight_info))
1211 if (unlikely(head >= vq->size))
1214 rte_atomic_thread_fence(__ATOMIC_SEQ_CST);
1216 inflight_info->desc[head].inflight = 0;
1218 rte_atomic_thread_fence(__ATOMIC_SEQ_CST);
1220 inflight_info->old_free_head = inflight_info->free_head;
1221 inflight_info->old_used_idx = inflight_info->used_idx;
1222 inflight_info->old_used_wrap_counter = inflight_info->used_wrap_counter;
1228 rte_vhost_set_last_inflight_io_split(int vid, uint16_t vring_idx,
1231 struct virtio_net *dev;
1232 struct vhost_virtqueue *vq;
1234 dev = get_device(vid);
1238 if (unlikely(!(dev->protocol_features &
1239 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1242 if (unlikely(vq_is_packed(dev)))
1245 if (unlikely(vring_idx >= VHOST_MAX_VRING))
1248 vq = dev->virtqueue[vring_idx];
1252 if (unlikely(!vq->inflight_split))
1255 if (unlikely(idx >= vq->size))
1258 vq->inflight_split->last_inflight_io = idx;
1263 rte_vhost_set_last_inflight_io_packed(int vid, uint16_t vring_idx,
1266 struct rte_vhost_inflight_info_packed *inflight_info;
1267 struct virtio_net *dev;
1268 struct vhost_virtqueue *vq;
1271 dev = get_device(vid);
1275 if (unlikely(!(dev->protocol_features &
1276 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1279 if (unlikely(!vq_is_packed(dev)))
1282 if (unlikely(vring_idx >= VHOST_MAX_VRING))
1285 vq = dev->virtqueue[vring_idx];
1289 inflight_info = vq->inflight_packed;
1290 if (unlikely(!inflight_info))
1293 if (unlikely(head >= vq->size))
1296 last = inflight_info->desc[head].last;
1297 if (unlikely(last >= vq->size))
1300 inflight_info->desc[last].next = inflight_info->free_head;
1301 inflight_info->free_head = head;
1302 inflight_info->used_idx += inflight_info->desc[head].num;
1303 if (inflight_info->used_idx >= inflight_info->desc_num) {
1304 inflight_info->used_idx -= inflight_info->desc_num;
1305 inflight_info->used_wrap_counter =
1306 !inflight_info->used_wrap_counter;
1313 rte_vhost_vring_call(int vid, uint16_t vring_idx)
1315 struct virtio_net *dev;
1316 struct vhost_virtqueue *vq;
1318 dev = get_device(vid);
1322 if (vring_idx >= VHOST_MAX_VRING)
1325 vq = dev->virtqueue[vring_idx];
1329 rte_spinlock_lock(&vq->access_lock);
1331 if (vq_is_packed(dev))
1332 vhost_vring_call_packed(dev, vq);
1334 vhost_vring_call_split(dev, vq);
1336 rte_spinlock_unlock(&vq->access_lock);
1342 rte_vhost_avail_entries(int vid, uint16_t queue_id)
1344 struct virtio_net *dev;
1345 struct vhost_virtqueue *vq;
1348 dev = get_device(vid);
1352 if (queue_id >= VHOST_MAX_VRING)
1355 vq = dev->virtqueue[queue_id];
1359 rte_spinlock_lock(&vq->access_lock);
1361 if (unlikely(!vq->enabled || vq->avail == NULL))
1364 ret = *(volatile uint16_t *)&vq->avail->idx - vq->last_used_idx;
1367 rte_spinlock_unlock(&vq->access_lock);
1372 vhost_enable_notify_split(struct virtio_net *dev,
1373 struct vhost_virtqueue *vq, int enable)
1375 if (vq->used == NULL)
1378 if (!(dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))) {
1380 vq->used->flags &= ~VRING_USED_F_NO_NOTIFY;
1382 vq->used->flags |= VRING_USED_F_NO_NOTIFY;
1385 vhost_avail_event(vq) = vq->last_avail_idx;
1391 vhost_enable_notify_packed(struct virtio_net *dev,
1392 struct vhost_virtqueue *vq, int enable)
1396 if (vq->device_event == NULL)
1400 vq->device_event->flags = VRING_EVENT_F_DISABLE;
1404 flags = VRING_EVENT_F_ENABLE;
1405 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX)) {
1406 flags = VRING_EVENT_F_DESC;
1407 vq->device_event->off_wrap = vq->last_avail_idx |
1408 vq->avail_wrap_counter << 15;
1411 rte_atomic_thread_fence(__ATOMIC_RELEASE);
1413 vq->device_event->flags = flags;
1418 vhost_enable_guest_notification(struct virtio_net *dev,
1419 struct vhost_virtqueue *vq, int enable)
1422 * If the virtqueue is not ready yet, it will be applied
1423 * when it will become ready.
1428 if (vq_is_packed(dev))
1429 return vhost_enable_notify_packed(dev, vq, enable);
1431 return vhost_enable_notify_split(dev, vq, enable);
1435 rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable)
1437 struct virtio_net *dev = get_device(vid);
1438 struct vhost_virtqueue *vq;
1444 if (queue_id >= VHOST_MAX_VRING)
1447 vq = dev->virtqueue[queue_id];
1451 rte_spinlock_lock(&vq->access_lock);
1453 vq->notif_enable = enable;
1454 ret = vhost_enable_guest_notification(dev, vq, enable);
1456 rte_spinlock_unlock(&vq->access_lock);
1462 rte_vhost_log_write(int vid, uint64_t addr, uint64_t len)
1464 struct virtio_net *dev = get_device(vid);
1469 vhost_log_write(dev, addr, len);
1473 rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
1474 uint64_t offset, uint64_t len)
1476 struct virtio_net *dev;
1477 struct vhost_virtqueue *vq;
1479 dev = get_device(vid);
1483 if (vring_idx >= VHOST_MAX_VRING)
1485 vq = dev->virtqueue[vring_idx];
1489 vhost_log_used_vring(dev, vq, offset, len);
1493 rte_vhost_rx_queue_count(int vid, uint16_t qid)
1495 struct virtio_net *dev;
1496 struct vhost_virtqueue *vq;
1499 dev = get_device(vid);
1503 if (unlikely(qid >= dev->nr_vring || (qid & 1) == 0)) {
1504 VHOST_LOG_DATA(ERR, "(%s) %s: invalid virtqueue idx %d.\n",
1505 dev->ifname, __func__, qid);
1509 vq = dev->virtqueue[qid];
1513 rte_spinlock_lock(&vq->access_lock);
1515 if (unlikely(!vq->enabled || vq->avail == NULL))
1518 ret = *((volatile uint16_t *)&vq->avail->idx) - vq->last_avail_idx;
1521 rte_spinlock_unlock(&vq->access_lock);
1525 struct rte_vdpa_device *
1526 rte_vhost_get_vdpa_device(int vid)
1528 struct virtio_net *dev = get_device(vid);
1533 return dev->vdpa_dev;
1537 rte_vhost_get_log_base(int vid, uint64_t *log_base,
1540 struct virtio_net *dev = get_device(vid);
1542 if (dev == NULL || log_base == NULL || log_size == NULL)
1545 *log_base = dev->log_base;
1546 *log_size = dev->log_size;
1552 rte_vhost_get_vring_base(int vid, uint16_t queue_id,
1553 uint16_t *last_avail_idx, uint16_t *last_used_idx)
1555 struct vhost_virtqueue *vq;
1556 struct virtio_net *dev = get_device(vid);
1558 if (dev == NULL || last_avail_idx == NULL || last_used_idx == NULL)
1561 if (queue_id >= VHOST_MAX_VRING)
1564 vq = dev->virtqueue[queue_id];
1568 if (vq_is_packed(dev)) {
1569 *last_avail_idx = (vq->avail_wrap_counter << 15) |
1571 *last_used_idx = (vq->used_wrap_counter << 15) |
1574 *last_avail_idx = vq->last_avail_idx;
1575 *last_used_idx = vq->last_used_idx;
1582 rte_vhost_set_vring_base(int vid, uint16_t queue_id,
1583 uint16_t last_avail_idx, uint16_t last_used_idx)
1585 struct vhost_virtqueue *vq;
1586 struct virtio_net *dev = get_device(vid);
1591 if (queue_id >= VHOST_MAX_VRING)
1594 vq = dev->virtqueue[queue_id];
1598 if (vq_is_packed(dev)) {
1599 vq->last_avail_idx = last_avail_idx & 0x7fff;
1600 vq->avail_wrap_counter = !!(last_avail_idx & (1 << 15));
1601 vq->last_used_idx = last_used_idx & 0x7fff;
1602 vq->used_wrap_counter = !!(last_used_idx & (1 << 15));
1604 vq->last_avail_idx = last_avail_idx;
1605 vq->last_used_idx = last_used_idx;
1612 rte_vhost_get_vring_base_from_inflight(int vid,
1614 uint16_t *last_avail_idx,
1615 uint16_t *last_used_idx)
1617 struct rte_vhost_inflight_info_packed *inflight_info;
1618 struct vhost_virtqueue *vq;
1619 struct virtio_net *dev = get_device(vid);
1621 if (dev == NULL || last_avail_idx == NULL || last_used_idx == NULL)
1624 if (queue_id >= VHOST_MAX_VRING)
1627 vq = dev->virtqueue[queue_id];
1631 if (!vq_is_packed(dev))
1634 inflight_info = vq->inflight_packed;
1638 *last_avail_idx = (inflight_info->old_used_wrap_counter << 15) |
1639 inflight_info->old_used_idx;
1640 *last_used_idx = *last_avail_idx;
1646 rte_vhost_extern_callback_register(int vid,
1647 struct rte_vhost_user_extern_ops const * const ops, void *ctx)
1649 struct virtio_net *dev = get_device(vid);
1651 if (dev == NULL || ops == NULL)
1654 dev->extern_ops = *ops;
1655 dev->extern_data = ctx;
1659 static __rte_always_inline int
1660 async_channel_register(int vid, uint16_t queue_id)
1662 struct virtio_net *dev = get_device(vid);
1663 struct vhost_virtqueue *vq = dev->virtqueue[queue_id];
1664 struct vhost_async *async;
1665 int node = vq->numa_node;
1667 if (unlikely(vq->async)) {
1668 VHOST_LOG_CONFIG(ERR,
1669 "(%s) async register failed: already registered (qid: %d)\n",
1670 dev->ifname, queue_id);
1674 async = rte_zmalloc_socket(NULL, sizeof(struct vhost_async), 0, node);
1676 VHOST_LOG_CONFIG(ERR, "(%s) failed to allocate async metadata (qid: %d)\n",
1677 dev->ifname, queue_id);
1681 async->pkts_info = rte_malloc_socket(NULL, vq->size * sizeof(struct async_inflight_info),
1682 RTE_CACHE_LINE_SIZE, node);
1683 if (!async->pkts_info) {
1684 VHOST_LOG_CONFIG(ERR, "(%s) failed to allocate async_pkts_info (qid: %d)\n",
1685 dev->ifname, queue_id);
1686 goto out_free_async;
1689 async->pkts_cmpl_flag = rte_zmalloc_socket(NULL, vq->size * sizeof(bool),
1690 RTE_CACHE_LINE_SIZE, node);
1691 if (!async->pkts_cmpl_flag) {
1692 VHOST_LOG_CONFIG(ERR, "(%s) failed to allocate async pkts_cmpl_flag (qid: %d)\n",
1693 dev->ifname, queue_id);
1694 goto out_free_async;
1697 if (vq_is_packed(dev)) {
1698 async->buffers_packed = rte_malloc_socket(NULL,
1699 vq->size * sizeof(struct vring_used_elem_packed),
1700 RTE_CACHE_LINE_SIZE, node);
1701 if (!async->buffers_packed) {
1702 VHOST_LOG_CONFIG(ERR, "(%s) failed to allocate async buffers (qid: %d)\n",
1703 dev->ifname, queue_id);
1704 goto out_free_inflight;
1707 async->descs_split = rte_malloc_socket(NULL,
1708 vq->size * sizeof(struct vring_used_elem),
1709 RTE_CACHE_LINE_SIZE, node);
1710 if (!async->descs_split) {
1711 VHOST_LOG_CONFIG(ERR, "(%s) failed to allocate async descs (qid: %d)\n",
1712 dev->ifname, queue_id);
1713 goto out_free_inflight;
1721 rte_free(async->pkts_info);
1729 rte_vhost_async_channel_register(int vid, uint16_t queue_id)
1731 struct vhost_virtqueue *vq;
1732 struct virtio_net *dev = get_device(vid);
1738 if (queue_id >= VHOST_MAX_VRING)
1741 vq = dev->virtqueue[queue_id];
1743 if (unlikely(vq == NULL || !dev->async_copy))
1746 rte_spinlock_lock(&vq->access_lock);
1747 ret = async_channel_register(vid, queue_id);
1748 rte_spinlock_unlock(&vq->access_lock);
1754 rte_vhost_async_channel_register_thread_unsafe(int vid, uint16_t queue_id)
1756 struct vhost_virtqueue *vq;
1757 struct virtio_net *dev = get_device(vid);
1762 if (queue_id >= VHOST_MAX_VRING)
1765 vq = dev->virtqueue[queue_id];
1767 if (unlikely(vq == NULL || !dev->async_copy))
1770 return async_channel_register(vid, queue_id);
1774 rte_vhost_async_channel_unregister(int vid, uint16_t queue_id)
1776 struct vhost_virtqueue *vq;
1777 struct virtio_net *dev = get_device(vid);
1783 if (queue_id >= VHOST_MAX_VRING)
1786 vq = dev->virtqueue[queue_id];
1791 if (!rte_spinlock_trylock(&vq->access_lock)) {
1792 VHOST_LOG_CONFIG(ERR, "(%s) failed to unregister async channel, virtqueue busy.\n",
1799 } else if (vq->async->pkts_inflight_n) {
1800 VHOST_LOG_CONFIG(ERR, "(%s) failed to unregister async channel.\n", dev->ifname);
1801 VHOST_LOG_CONFIG(ERR, "(%s) inflight packets must be completed before unregistration.\n",
1804 vhost_free_async_mem(vq);
1808 rte_spinlock_unlock(&vq->access_lock);
1814 rte_vhost_async_channel_unregister_thread_unsafe(int vid, uint16_t queue_id)
1816 struct vhost_virtqueue *vq;
1817 struct virtio_net *dev = get_device(vid);
1822 if (queue_id >= VHOST_MAX_VRING)
1825 vq = dev->virtqueue[queue_id];
1833 if (vq->async->pkts_inflight_n) {
1834 VHOST_LOG_CONFIG(ERR, "(%s) failed to unregister async channel.\n", dev->ifname);
1835 VHOST_LOG_CONFIG(ERR, "(%s) inflight packets must be completed before unregistration.\n",
1840 vhost_free_async_mem(vq);
1846 rte_vhost_async_dma_configure(int16_t dma_id, uint16_t vchan_id)
1848 struct rte_dma_info info;
1849 void *pkts_cmpl_flag_addr;
1852 if (!rte_dma_is_valid(dma_id)) {
1853 VHOST_LOG_CONFIG(ERR, "DMA %d is not found.\n", dma_id);
1857 rte_dma_info_get(dma_id, &info);
1858 if (vchan_id >= info.max_vchans) {
1859 VHOST_LOG_CONFIG(ERR, "Invalid DMA %d vChannel %u.\n", dma_id, vchan_id);
1863 if (!dma_copy_track[dma_id].vchans) {
1864 struct async_dma_vchan_info *vchans;
1866 vchans = rte_zmalloc(NULL, sizeof(struct async_dma_vchan_info) * info.max_vchans,
1867 RTE_CACHE_LINE_SIZE);
1868 if (vchans == NULL) {
1869 VHOST_LOG_CONFIG(ERR, "Failed to allocate vchans for DMA %d vChannel %u.\n",
1874 dma_copy_track[dma_id].vchans = vchans;
1877 if (dma_copy_track[dma_id].vchans[vchan_id].pkts_cmpl_flag_addr) {
1878 VHOST_LOG_CONFIG(INFO, "DMA %d vChannel %u already registered.\n", dma_id,
1883 max_desc = info.max_desc;
1884 if (!rte_is_power_of_2(max_desc))
1885 max_desc = rte_align32pow2(max_desc);
1887 pkts_cmpl_flag_addr = rte_zmalloc(NULL, sizeof(bool *) * max_desc, RTE_CACHE_LINE_SIZE);
1888 if (!pkts_cmpl_flag_addr) {
1889 VHOST_LOG_CONFIG(ERR, "Failed to allocate pkts_cmpl_flag_addr for DMA %d "
1890 "vChannel %u.\n", dma_id, vchan_id);
1892 if (dma_copy_track[dma_id].nr_vchans == 0) {
1893 rte_free(dma_copy_track[dma_id].vchans);
1894 dma_copy_track[dma_id].vchans = NULL;
1899 dma_copy_track[dma_id].vchans[vchan_id].pkts_cmpl_flag_addr = pkts_cmpl_flag_addr;
1900 dma_copy_track[dma_id].vchans[vchan_id].ring_size = max_desc;
1901 dma_copy_track[dma_id].vchans[vchan_id].ring_mask = max_desc - 1;
1902 dma_copy_track[dma_id].nr_vchans++;
1908 rte_vhost_async_get_inflight(int vid, uint16_t queue_id)
1910 struct vhost_virtqueue *vq;
1911 struct virtio_net *dev = get_device(vid);
1917 if (queue_id >= VHOST_MAX_VRING)
1920 vq = dev->virtqueue[queue_id];
1925 if (!rte_spinlock_trylock(&vq->access_lock)) {
1926 VHOST_LOG_CONFIG(DEBUG,
1927 "(%s) failed to check in-flight packets. virtqueue busy.\n",
1933 ret = vq->async->pkts_inflight_n;
1935 rte_spinlock_unlock(&vq->access_lock);
1941 rte_vhost_async_get_inflight_thread_unsafe(int vid, uint16_t queue_id)
1943 struct vhost_virtqueue *vq;
1944 struct virtio_net *dev = get_device(vid);
1950 if (queue_id >= VHOST_MAX_VRING)
1953 vq = dev->virtqueue[queue_id];
1961 ret = vq->async->pkts_inflight_n;
1967 rte_vhost_get_monitor_addr(int vid, uint16_t queue_id,
1968 struct rte_vhost_power_monitor_cond *pmc)
1970 struct virtio_net *dev = get_device(vid);
1971 struct vhost_virtqueue *vq;
1975 if (queue_id >= VHOST_MAX_VRING)
1978 vq = dev->virtqueue[queue_id];
1982 if (vq_is_packed(dev)) {
1983 struct vring_packed_desc *desc;
1984 desc = vq->desc_packed;
1985 pmc->addr = &desc[vq->last_avail_idx].flags;
1986 if (vq->avail_wrap_counter)
1987 pmc->val = VRING_DESC_F_AVAIL;
1989 pmc->val = VRING_DESC_F_USED;
1990 pmc->mask = VRING_DESC_F_AVAIL | VRING_DESC_F_USED;
1991 pmc->size = sizeof(desc[vq->last_avail_idx].flags);
1994 pmc->addr = &vq->avail->idx;
1995 pmc->val = vq->last_avail_idx & (vq->size - 1);
1996 pmc->mask = vq->size - 1;
1997 pmc->size = sizeof(vq->avail->idx);
2006 rte_vhost_vring_stats_get_names(int vid, uint16_t queue_id,
2007 struct rte_vhost_stat_name *name, unsigned int size)
2009 struct virtio_net *dev = get_device(vid);
2015 if (queue_id >= dev->nr_vring)
2018 if (!(dev->flags & VIRTIO_DEV_STATS_ENABLED))
2021 if (name == NULL || size < VHOST_NB_VQ_STATS)
2022 return VHOST_NB_VQ_STATS;
2024 for (i = 0; i < VHOST_NB_VQ_STATS; i++)
2025 snprintf(name[i].name, sizeof(name[i].name), "%s_q%u_%s",
2026 (queue_id & 1) ? "rx" : "tx",
2027 queue_id / 2, vhost_vq_stat_strings[i].name);
2029 return VHOST_NB_VQ_STATS;
2033 rte_vhost_vring_stats_get(int vid, uint16_t queue_id,
2034 struct rte_vhost_stat *stats, unsigned int n)
2036 struct virtio_net *dev = get_device(vid);
2037 struct vhost_virtqueue *vq;
2043 if (queue_id >= dev->nr_vring)
2046 if (!(dev->flags & VIRTIO_DEV_STATS_ENABLED))
2049 if (stats == NULL || n < VHOST_NB_VQ_STATS)
2050 return VHOST_NB_VQ_STATS;
2052 vq = dev->virtqueue[queue_id];
2054 rte_spinlock_lock(&vq->access_lock);
2055 for (i = 0; i < VHOST_NB_VQ_STATS; i++) {
2057 *(uint64_t *)(((char *)vq) + vhost_vq_stat_strings[i].offset);
2060 rte_spinlock_unlock(&vq->access_lock);
2062 return VHOST_NB_VQ_STATS;
2065 int rte_vhost_vring_stats_reset(int vid, uint16_t queue_id)
2067 struct virtio_net *dev = get_device(vid);
2068 struct vhost_virtqueue *vq;
2073 if (queue_id >= dev->nr_vring)
2076 if (!(dev->flags & VIRTIO_DEV_STATS_ENABLED))
2079 vq = dev->virtqueue[queue_id];
2081 rte_spinlock_lock(&vq->access_lock);
2082 memset(&vq->stats, 0, sizeof(vq->stats));
2083 rte_spinlock_unlock(&vq->access_lock);
2088 RTE_LOG_REGISTER_SUFFIX(vhost_config_log_level, config, INFO);
2089 RTE_LOG_REGISTER_SUFFIX(vhost_data_log_level, data, WARNING);