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)},
48 #define VHOST_NB_VQ_STATS RTE_DIM(vhost_vq_stat_strings)
50 /* Called with iotlb_lock read-locked */
52 __vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
53 uint64_t iova, uint64_t *size, uint8_t perm)
55 uint64_t vva, tmp_size;
62 vva = vhost_user_iotlb_cache_find(vq, iova, &tmp_size, perm);
63 if (tmp_size == *size)
68 if (!vhost_user_iotlb_pending_miss(vq, iova, perm)) {
70 * iotlb_lock is read-locked for a full burst,
71 * but it only protects the iotlb cache.
72 * In case of IOTLB miss, we might block on the socket,
73 * which could cause a deadlock with QEMU if an IOTLB update
74 * is being handled. We can safely unlock here to avoid it.
76 vhost_user_iotlb_rd_unlock(vq);
78 vhost_user_iotlb_pending_insert(dev, vq, iova, perm);
79 if (vhost_user_iotlb_miss(dev, iova, perm)) {
80 VHOST_LOG_DATA(ERR, "(%s) IOTLB miss req failed for IOVA 0x%" PRIx64 "\n",
82 vhost_user_iotlb_pending_remove(vq, iova, 1, perm);
85 vhost_user_iotlb_rd_lock(vq);
91 #define VHOST_LOG_PAGE 4096
94 * Atomically set a bit in memory.
96 static __rte_always_inline void
97 vhost_set_bit(unsigned int nr, volatile uint8_t *addr)
99 #if defined(RTE_TOOLCHAIN_GCC) && (GCC_VERSION < 70100)
101 * __sync_ built-ins are deprecated, but __atomic_ ones
102 * are sub-optimized in older GCC versions.
104 __sync_fetch_and_or_1(addr, (1U << nr));
106 __atomic_fetch_or(addr, (1U << nr), __ATOMIC_RELAXED);
110 static __rte_always_inline void
111 vhost_log_page(uint8_t *log_base, uint64_t page)
113 vhost_set_bit(page % 8, &log_base[page / 8]);
117 __vhost_log_write(struct virtio_net *dev, uint64_t addr, uint64_t len)
121 if (unlikely(!dev->log_base || !len))
124 if (unlikely(dev->log_size <= ((addr + len - 1) / VHOST_LOG_PAGE / 8)))
127 /* To make sure guest memory updates are committed before logging */
128 rte_atomic_thread_fence(__ATOMIC_RELEASE);
130 page = addr / VHOST_LOG_PAGE;
131 while (page * VHOST_LOG_PAGE < addr + len) {
132 vhost_log_page((uint8_t *)(uintptr_t)dev->log_base, page);
138 __vhost_log_write_iova(struct virtio_net *dev, struct vhost_virtqueue *vq,
139 uint64_t iova, uint64_t len)
141 uint64_t hva, gpa, map_len;
144 hva = __vhost_iova_to_vva(dev, vq, iova, &map_len, VHOST_ACCESS_RW);
145 if (map_len != len) {
147 "(%s) failed to write log for IOVA 0x%" PRIx64 ". No IOTLB entry found\n",
152 gpa = hva_to_gpa(dev, hva, len);
154 __vhost_log_write(dev, gpa, len);
158 __vhost_log_cache_sync(struct virtio_net *dev, struct vhost_virtqueue *vq)
160 unsigned long *log_base;
163 if (unlikely(!dev->log_base))
166 /* No cache, nothing to sync */
167 if (unlikely(!vq->log_cache))
170 rte_atomic_thread_fence(__ATOMIC_RELEASE);
172 log_base = (unsigned long *)(uintptr_t)dev->log_base;
174 for (i = 0; i < vq->log_cache_nb_elem; i++) {
175 struct log_cache_entry *elem = vq->log_cache + i;
177 #if defined(RTE_TOOLCHAIN_GCC) && (GCC_VERSION < 70100)
179 * '__sync' builtins are deprecated, but '__atomic' ones
180 * are sub-optimized in older GCC versions.
182 __sync_fetch_and_or(log_base + elem->offset, elem->val);
184 __atomic_fetch_or(log_base + elem->offset, elem->val,
189 rte_atomic_thread_fence(__ATOMIC_RELEASE);
191 vq->log_cache_nb_elem = 0;
194 static __rte_always_inline void
195 vhost_log_cache_page(struct virtio_net *dev, struct vhost_virtqueue *vq,
198 uint32_t bit_nr = page % (sizeof(unsigned long) << 3);
199 uint32_t offset = page / (sizeof(unsigned long) << 3);
202 if (unlikely(!vq->log_cache)) {
203 /* No logging cache allocated, write dirty log map directly */
204 rte_atomic_thread_fence(__ATOMIC_RELEASE);
205 vhost_log_page((uint8_t *)(uintptr_t)dev->log_base, page);
210 for (i = 0; i < vq->log_cache_nb_elem; i++) {
211 struct log_cache_entry *elem = vq->log_cache + i;
213 if (elem->offset == offset) {
214 elem->val |= (1UL << bit_nr);
219 if (unlikely(i >= VHOST_LOG_CACHE_NR)) {
221 * No more room for a new log cache entry,
222 * so write the dirty log map directly.
224 rte_atomic_thread_fence(__ATOMIC_RELEASE);
225 vhost_log_page((uint8_t *)(uintptr_t)dev->log_base, page);
230 vq->log_cache[i].offset = offset;
231 vq->log_cache[i].val = (1UL << bit_nr);
232 vq->log_cache_nb_elem++;
236 __vhost_log_cache_write(struct virtio_net *dev, struct vhost_virtqueue *vq,
237 uint64_t addr, uint64_t len)
241 if (unlikely(!dev->log_base || !len))
244 if (unlikely(dev->log_size <= ((addr + len - 1) / VHOST_LOG_PAGE / 8)))
247 page = addr / VHOST_LOG_PAGE;
248 while (page * VHOST_LOG_PAGE < addr + len) {
249 vhost_log_cache_page(dev, vq, page);
255 __vhost_log_cache_write_iova(struct virtio_net *dev, struct vhost_virtqueue *vq,
256 uint64_t iova, uint64_t len)
258 uint64_t hva, gpa, map_len;
261 hva = __vhost_iova_to_vva(dev, vq, iova, &map_len, VHOST_ACCESS_RW);
262 if (map_len != len) {
264 "(%s) failed to write log for IOVA 0x%" PRIx64 ". No IOTLB entry found\n",
269 gpa = hva_to_gpa(dev, hva, len);
271 __vhost_log_cache_write(dev, vq, gpa, len);
275 vhost_alloc_copy_ind_table(struct virtio_net *dev, struct vhost_virtqueue *vq,
276 uint64_t desc_addr, uint64_t desc_len)
280 uint64_t len, remain = desc_len;
282 idesc = rte_malloc_socket(__func__, desc_len, 0, vq->numa_node);
283 if (unlikely(!idesc))
286 dst = (uint64_t)(uintptr_t)idesc;
290 src = vhost_iova_to_vva(dev, vq, desc_addr, &len,
292 if (unlikely(!src || !len)) {
297 rte_memcpy((void *)(uintptr_t)dst, (void *)(uintptr_t)src, len);
308 cleanup_vq(struct vhost_virtqueue *vq, int destroy)
310 if ((vq->callfd >= 0) && (destroy != 0))
317 cleanup_vq_inflight(struct virtio_net *dev, struct vhost_virtqueue *vq)
319 if (!(dev->protocol_features &
320 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD)))
323 if (vq_is_packed(dev)) {
324 if (vq->inflight_packed)
325 vq->inflight_packed = NULL;
327 if (vq->inflight_split)
328 vq->inflight_split = NULL;
331 if (vq->resubmit_inflight) {
332 if (vq->resubmit_inflight->resubmit_list) {
333 rte_free(vq->resubmit_inflight->resubmit_list);
334 vq->resubmit_inflight->resubmit_list = NULL;
336 rte_free(vq->resubmit_inflight);
337 vq->resubmit_inflight = NULL;
342 * Unmap any memory, close any file descriptors and
343 * free any memory owned by a device.
346 cleanup_device(struct virtio_net *dev, int destroy)
350 vhost_backend_cleanup(dev);
352 for (i = 0; i < dev->nr_vring; i++) {
353 cleanup_vq(dev->virtqueue[i], destroy);
354 cleanup_vq_inflight(dev, dev->virtqueue[i]);
359 vhost_free_async_mem(struct vhost_virtqueue *vq)
364 rte_free(vq->async->pkts_info);
365 rte_free(vq->async->pkts_cmpl_flag);
367 rte_free(vq->async->buffers_packed);
368 vq->async->buffers_packed = NULL;
369 rte_free(vq->async->descs_split);
370 vq->async->descs_split = NULL;
377 free_vq(struct virtio_net *dev, struct vhost_virtqueue *vq)
379 if (vq_is_packed(dev))
380 rte_free(vq->shadow_used_packed);
382 rte_free(vq->shadow_used_split);
384 vhost_free_async_mem(vq);
385 rte_free(vq->batch_copy_elems);
386 rte_mempool_free(vq->iotlb_pool);
387 rte_free(vq->log_cache);
392 * Release virtqueues and device memory.
395 free_device(struct virtio_net *dev)
399 for (i = 0; i < dev->nr_vring; i++)
400 free_vq(dev, dev->virtqueue[i]);
405 static __rte_always_inline int
406 log_translate(struct virtio_net *dev, struct vhost_virtqueue *vq)
408 if (likely(!(vq->ring_addrs.flags & (1 << VHOST_VRING_F_LOG))))
411 vq->log_guest_addr = translate_log_addr(dev, vq,
412 vq->ring_addrs.log_guest_addr);
413 if (vq->log_guest_addr == 0)
420 * Converts vring log address to GPA
421 * If IOMMU is enabled, the log address is IOVA
422 * If IOMMU not enabled, the log address is already GPA
424 * Caller should have iotlb_lock read-locked
427 translate_log_addr(struct virtio_net *dev, struct vhost_virtqueue *vq,
430 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)) {
431 const uint64_t exp_size = sizeof(uint64_t);
433 uint64_t size = exp_size;
435 hva = vhost_iova_to_vva(dev, vq, log_addr,
436 &size, VHOST_ACCESS_RW);
438 if (size != exp_size)
441 gpa = hva_to_gpa(dev, hva, exp_size);
444 "(%s) failed to find GPA for log_addr: 0x%"
445 PRIx64 " hva: 0x%" PRIx64 "\n",
446 dev->ifname, log_addr, hva);
455 /* Caller should have iotlb_lock read-locked */
457 vring_translate_split(struct virtio_net *dev, struct vhost_virtqueue *vq)
459 uint64_t req_size, size;
461 req_size = sizeof(struct vring_desc) * vq->size;
463 vq->desc = (struct vring_desc *)(uintptr_t)vhost_iova_to_vva(dev, vq,
464 vq->ring_addrs.desc_user_addr,
465 &size, VHOST_ACCESS_RW);
466 if (!vq->desc || size != req_size)
469 req_size = sizeof(struct vring_avail);
470 req_size += sizeof(uint16_t) * vq->size;
471 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))
472 req_size += sizeof(uint16_t);
474 vq->avail = (struct vring_avail *)(uintptr_t)vhost_iova_to_vva(dev, vq,
475 vq->ring_addrs.avail_user_addr,
476 &size, VHOST_ACCESS_RW);
477 if (!vq->avail || size != req_size)
480 req_size = sizeof(struct vring_used);
481 req_size += sizeof(struct vring_used_elem) * vq->size;
482 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))
483 req_size += sizeof(uint16_t);
485 vq->used = (struct vring_used *)(uintptr_t)vhost_iova_to_vva(dev, vq,
486 vq->ring_addrs.used_user_addr,
487 &size, VHOST_ACCESS_RW);
488 if (!vq->used || size != req_size)
494 /* Caller should have iotlb_lock read-locked */
496 vring_translate_packed(struct virtio_net *dev, struct vhost_virtqueue *vq)
498 uint64_t req_size, size;
500 req_size = sizeof(struct vring_packed_desc) * vq->size;
502 vq->desc_packed = (struct vring_packed_desc *)(uintptr_t)
503 vhost_iova_to_vva(dev, vq, vq->ring_addrs.desc_user_addr,
504 &size, VHOST_ACCESS_RW);
505 if (!vq->desc_packed || size != req_size)
508 req_size = sizeof(struct vring_packed_desc_event);
510 vq->driver_event = (struct vring_packed_desc_event *)(uintptr_t)
511 vhost_iova_to_vva(dev, vq, vq->ring_addrs.avail_user_addr,
512 &size, VHOST_ACCESS_RW);
513 if (!vq->driver_event || size != req_size)
516 req_size = sizeof(struct vring_packed_desc_event);
518 vq->device_event = (struct vring_packed_desc_event *)(uintptr_t)
519 vhost_iova_to_vva(dev, vq, vq->ring_addrs.used_user_addr,
520 &size, VHOST_ACCESS_RW);
521 if (!vq->device_event || size != req_size)
528 vring_translate(struct virtio_net *dev, struct vhost_virtqueue *vq)
531 if (!(dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)))
534 if (vq_is_packed(dev)) {
535 if (vring_translate_packed(dev, vq) < 0)
538 if (vring_translate_split(dev, vq) < 0)
542 if (log_translate(dev, vq) < 0)
545 vq->access_ok = true;
551 vring_invalidate(struct virtio_net *dev, struct vhost_virtqueue *vq)
553 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
554 vhost_user_iotlb_wr_lock(vq);
556 vq->access_ok = false;
560 vq->log_guest_addr = 0;
562 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
563 vhost_user_iotlb_wr_unlock(vq);
567 init_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
569 struct vhost_virtqueue *vq;
570 int numa_node = SOCKET_ID_ANY;
572 if (vring_idx >= VHOST_MAX_VRING) {
573 VHOST_LOG_CONFIG(ERR, "(%s) failed to init vring, out of bound (%d)\n",
574 dev->ifname, vring_idx);
578 vq = dev->virtqueue[vring_idx];
580 VHOST_LOG_CONFIG(ERR, "(%s) virtqueue not allocated (%d)\n",
581 dev->ifname, vring_idx);
585 memset(vq, 0, sizeof(struct vhost_virtqueue));
587 vq->kickfd = VIRTIO_UNINITIALIZED_EVENTFD;
588 vq->callfd = VIRTIO_UNINITIALIZED_EVENTFD;
589 vq->notif_enable = VIRTIO_UNINITIALIZED_NOTIF;
591 #ifdef RTE_LIBRTE_VHOST_NUMA
592 if (get_mempolicy(&numa_node, NULL, 0, vq, MPOL_F_NODE | MPOL_F_ADDR)) {
593 VHOST_LOG_CONFIG(ERR, "(%s) failed to query numa node: %s\n",
594 dev->ifname, rte_strerror(errno));
595 numa_node = SOCKET_ID_ANY;
598 vq->numa_node = numa_node;
600 vhost_user_iotlb_init(dev, vring_idx);
604 reset_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
606 struct vhost_virtqueue *vq;
609 if (vring_idx >= VHOST_MAX_VRING) {
610 VHOST_LOG_CONFIG(ERR,
611 "(%s) failed to reset vring, out of bound (%d)\n",
612 dev->ifname, vring_idx);
616 vq = dev->virtqueue[vring_idx];
618 VHOST_LOG_CONFIG(ERR, "(%s) failed to reset vring, virtqueue not allocated (%d)\n",
619 dev->ifname, vring_idx);
624 init_vring_queue(dev, vring_idx);
629 alloc_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
631 struct vhost_virtqueue *vq;
634 /* Also allocate holes, if any, up to requested vring index. */
635 for (i = 0; i <= vring_idx; i++) {
636 if (dev->virtqueue[i])
639 vq = rte_zmalloc(NULL, sizeof(struct vhost_virtqueue), 0);
641 VHOST_LOG_CONFIG(ERR, "(%s) failed to allocate memory for vring %u.\n",
646 dev->virtqueue[i] = vq;
647 init_vring_queue(dev, i);
648 rte_spinlock_init(&vq->access_lock);
649 vq->avail_wrap_counter = 1;
650 vq->used_wrap_counter = 1;
651 vq->signalled_used_valid = false;
654 dev->nr_vring = RTE_MAX(dev->nr_vring, vring_idx + 1);
660 * Reset some variables in device structure, while keeping few
661 * others untouched, such as vid, ifname, nr_vring: they
662 * should be same unless the device is removed.
665 reset_device(struct virtio_net *dev)
670 dev->protocol_features = 0;
671 dev->flags &= VIRTIO_DEV_BUILTIN_VIRTIO_NET;
673 for (i = 0; i < dev->nr_vring; i++)
674 reset_vring_queue(dev, i);
678 * Invoked when there is a new vhost-user connection established (when
679 * there is a new virtio device being attached).
682 vhost_new_device(void)
684 struct virtio_net *dev;
687 pthread_mutex_lock(&vhost_dev_lock);
688 for (i = 0; i < RTE_MAX_VHOST_DEVICE; i++) {
689 if (vhost_devices[i] == NULL)
693 if (i == RTE_MAX_VHOST_DEVICE) {
694 VHOST_LOG_CONFIG(ERR, "failed to find a free slot for new device.\n");
695 pthread_mutex_unlock(&vhost_dev_lock);
699 dev = rte_zmalloc(NULL, sizeof(struct virtio_net), 0);
701 VHOST_LOG_CONFIG(ERR, "failed to allocate memory for new device.\n");
702 pthread_mutex_unlock(&vhost_dev_lock);
706 vhost_devices[i] = dev;
707 pthread_mutex_unlock(&vhost_dev_lock);
710 dev->flags = VIRTIO_DEV_BUILTIN_VIRTIO_NET;
711 dev->slave_req_fd = -1;
712 dev->postcopy_ufd = -1;
713 rte_spinlock_init(&dev->slave_req_lock);
719 vhost_destroy_device_notify(struct virtio_net *dev)
721 struct rte_vdpa_device *vdpa_dev;
723 if (dev->flags & VIRTIO_DEV_RUNNING) {
724 vdpa_dev = dev->vdpa_dev;
726 vdpa_dev->ops->dev_close(dev->vid);
727 dev->flags &= ~VIRTIO_DEV_RUNNING;
728 dev->notify_ops->destroy_device(dev->vid);
733 * Invoked when there is the vhost-user connection is broken (when
734 * the virtio device is being detached).
737 vhost_destroy_device(int vid)
739 struct virtio_net *dev = get_device(vid);
744 vhost_destroy_device_notify(dev);
746 cleanup_device(dev, 1);
749 vhost_devices[vid] = NULL;
753 vhost_attach_vdpa_device(int vid, struct rte_vdpa_device *vdpa_dev)
755 struct virtio_net *dev = get_device(vid);
760 dev->vdpa_dev = vdpa_dev;
764 vhost_set_ifname(int vid, const char *if_name, unsigned int if_len)
766 struct virtio_net *dev;
769 dev = get_device(vid);
773 len = if_len > sizeof(dev->ifname) ?
774 sizeof(dev->ifname) : if_len;
776 strncpy(dev->ifname, if_name, len);
777 dev->ifname[sizeof(dev->ifname) - 1] = '\0';
781 vhost_setup_virtio_net(int vid, bool enable, bool compliant_ol_flags, bool stats_enabled)
783 struct virtio_net *dev = get_device(vid);
789 dev->flags |= VIRTIO_DEV_BUILTIN_VIRTIO_NET;
791 dev->flags &= ~VIRTIO_DEV_BUILTIN_VIRTIO_NET;
792 if (!compliant_ol_flags)
793 dev->flags |= VIRTIO_DEV_LEGACY_OL_FLAGS;
795 dev->flags &= ~VIRTIO_DEV_LEGACY_OL_FLAGS;
797 dev->flags |= VIRTIO_DEV_STATS_ENABLED;
799 dev->flags &= ~VIRTIO_DEV_STATS_ENABLED;
803 vhost_enable_extbuf(int vid)
805 struct virtio_net *dev = get_device(vid);
814 vhost_enable_linearbuf(int vid)
816 struct virtio_net *dev = get_device(vid);
825 rte_vhost_get_mtu(int vid, uint16_t *mtu)
827 struct virtio_net *dev = get_device(vid);
829 if (dev == NULL || mtu == NULL)
832 if (!(dev->flags & VIRTIO_DEV_READY))
835 if (!(dev->features & (1ULL << VIRTIO_NET_F_MTU)))
844 rte_vhost_get_numa_node(int vid)
846 #ifdef RTE_LIBRTE_VHOST_NUMA
847 struct virtio_net *dev = get_device(vid);
851 if (dev == NULL || numa_available() != 0)
854 ret = get_mempolicy(&numa_node, NULL, 0, dev,
855 MPOL_F_NODE | MPOL_F_ADDR);
857 VHOST_LOG_CONFIG(ERR, "(%s) failed to query numa node: %s\n",
858 dev->ifname, rte_strerror(errno));
870 rte_vhost_get_queue_num(int vid)
872 struct virtio_net *dev = get_device(vid);
877 return dev->nr_vring / 2;
881 rte_vhost_get_vring_num(int vid)
883 struct virtio_net *dev = get_device(vid);
888 return dev->nr_vring;
892 rte_vhost_get_ifname(int vid, char *buf, size_t len)
894 struct virtio_net *dev = get_device(vid);
896 if (dev == NULL || buf == NULL)
899 len = RTE_MIN(len, sizeof(dev->ifname));
901 strncpy(buf, dev->ifname, len);
908 rte_vhost_get_negotiated_features(int vid, uint64_t *features)
910 struct virtio_net *dev;
912 dev = get_device(vid);
913 if (dev == NULL || features == NULL)
916 *features = dev->features;
921 rte_vhost_get_negotiated_protocol_features(int vid,
922 uint64_t *protocol_features)
924 struct virtio_net *dev;
926 dev = get_device(vid);
927 if (dev == NULL || protocol_features == NULL)
930 *protocol_features = dev->protocol_features;
935 rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem)
937 struct virtio_net *dev;
938 struct rte_vhost_memory *m;
941 dev = get_device(vid);
942 if (dev == NULL || mem == NULL)
945 size = dev->mem->nregions * sizeof(struct rte_vhost_mem_region);
946 m = malloc(sizeof(struct rte_vhost_memory) + size);
950 m->nregions = dev->mem->nregions;
951 memcpy(m->regions, dev->mem->regions, size);
958 rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
959 struct rte_vhost_vring *vring)
961 struct virtio_net *dev;
962 struct vhost_virtqueue *vq;
964 dev = get_device(vid);
965 if (dev == NULL || vring == NULL)
968 if (vring_idx >= VHOST_MAX_VRING)
971 vq = dev->virtqueue[vring_idx];
975 if (vq_is_packed(dev)) {
976 vring->desc_packed = vq->desc_packed;
977 vring->driver_event = vq->driver_event;
978 vring->device_event = vq->device_event;
980 vring->desc = vq->desc;
981 vring->avail = vq->avail;
982 vring->used = vq->used;
984 vring->log_guest_addr = vq->log_guest_addr;
986 vring->callfd = vq->callfd;
987 vring->kickfd = vq->kickfd;
988 vring->size = vq->size;
994 rte_vhost_get_vhost_ring_inflight(int vid, uint16_t vring_idx,
995 struct rte_vhost_ring_inflight *vring)
997 struct virtio_net *dev;
998 struct vhost_virtqueue *vq;
1000 dev = get_device(vid);
1004 if (vring_idx >= VHOST_MAX_VRING)
1007 vq = dev->virtqueue[vring_idx];
1011 if (vq_is_packed(dev)) {
1012 if (unlikely(!vq->inflight_packed))
1015 vring->inflight_packed = vq->inflight_packed;
1017 if (unlikely(!vq->inflight_split))
1020 vring->inflight_split = vq->inflight_split;
1023 vring->resubmit_inflight = vq->resubmit_inflight;
1029 rte_vhost_set_inflight_desc_split(int vid, uint16_t vring_idx,
1032 struct vhost_virtqueue *vq;
1033 struct virtio_net *dev;
1035 dev = get_device(vid);
1039 if (unlikely(!(dev->protocol_features &
1040 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1043 if (unlikely(vq_is_packed(dev)))
1046 if (unlikely(vring_idx >= VHOST_MAX_VRING))
1049 vq = dev->virtqueue[vring_idx];
1053 if (unlikely(!vq->inflight_split))
1056 if (unlikely(idx >= vq->size))
1059 vq->inflight_split->desc[idx].counter = vq->global_counter++;
1060 vq->inflight_split->desc[idx].inflight = 1;
1065 rte_vhost_set_inflight_desc_packed(int vid, uint16_t vring_idx,
1066 uint16_t head, uint16_t last,
1067 uint16_t *inflight_entry)
1069 struct rte_vhost_inflight_info_packed *inflight_info;
1070 struct virtio_net *dev;
1071 struct vhost_virtqueue *vq;
1072 struct vring_packed_desc *desc;
1073 uint16_t old_free_head, free_head;
1075 dev = get_device(vid);
1079 if (unlikely(!(dev->protocol_features &
1080 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1083 if (unlikely(!vq_is_packed(dev)))
1086 if (unlikely(vring_idx >= VHOST_MAX_VRING))
1089 vq = dev->virtqueue[vring_idx];
1093 inflight_info = vq->inflight_packed;
1094 if (unlikely(!inflight_info))
1097 if (unlikely(head >= vq->size))
1100 desc = vq->desc_packed;
1101 old_free_head = inflight_info->old_free_head;
1102 if (unlikely(old_free_head >= vq->size))
1105 free_head = old_free_head;
1107 /* init header descriptor */
1108 inflight_info->desc[old_free_head].num = 0;
1109 inflight_info->desc[old_free_head].counter = vq->global_counter++;
1110 inflight_info->desc[old_free_head].inflight = 1;
1112 /* save desc entry in flight entry */
1113 while (head != ((last + 1) % vq->size)) {
1114 inflight_info->desc[old_free_head].num++;
1115 inflight_info->desc[free_head].addr = desc[head].addr;
1116 inflight_info->desc[free_head].len = desc[head].len;
1117 inflight_info->desc[free_head].flags = desc[head].flags;
1118 inflight_info->desc[free_head].id = desc[head].id;
1120 inflight_info->desc[old_free_head].last = free_head;
1121 free_head = inflight_info->desc[free_head].next;
1122 inflight_info->free_head = free_head;
1123 head = (head + 1) % vq->size;
1126 inflight_info->old_free_head = free_head;
1127 *inflight_entry = old_free_head;
1133 rte_vhost_clr_inflight_desc_split(int vid, uint16_t vring_idx,
1134 uint16_t last_used_idx, uint16_t idx)
1136 struct virtio_net *dev;
1137 struct vhost_virtqueue *vq;
1139 dev = get_device(vid);
1143 if (unlikely(!(dev->protocol_features &
1144 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1147 if (unlikely(vq_is_packed(dev)))
1150 if (unlikely(vring_idx >= VHOST_MAX_VRING))
1153 vq = dev->virtqueue[vring_idx];
1157 if (unlikely(!vq->inflight_split))
1160 if (unlikely(idx >= vq->size))
1163 rte_atomic_thread_fence(__ATOMIC_SEQ_CST);
1165 vq->inflight_split->desc[idx].inflight = 0;
1167 rte_atomic_thread_fence(__ATOMIC_SEQ_CST);
1169 vq->inflight_split->used_idx = last_used_idx;
1174 rte_vhost_clr_inflight_desc_packed(int vid, uint16_t vring_idx,
1177 struct rte_vhost_inflight_info_packed *inflight_info;
1178 struct virtio_net *dev;
1179 struct vhost_virtqueue *vq;
1181 dev = get_device(vid);
1185 if (unlikely(!(dev->protocol_features &
1186 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1189 if (unlikely(!vq_is_packed(dev)))
1192 if (unlikely(vring_idx >= VHOST_MAX_VRING))
1195 vq = dev->virtqueue[vring_idx];
1199 inflight_info = vq->inflight_packed;
1200 if (unlikely(!inflight_info))
1203 if (unlikely(head >= vq->size))
1206 rte_atomic_thread_fence(__ATOMIC_SEQ_CST);
1208 inflight_info->desc[head].inflight = 0;
1210 rte_atomic_thread_fence(__ATOMIC_SEQ_CST);
1212 inflight_info->old_free_head = inflight_info->free_head;
1213 inflight_info->old_used_idx = inflight_info->used_idx;
1214 inflight_info->old_used_wrap_counter = inflight_info->used_wrap_counter;
1220 rte_vhost_set_last_inflight_io_split(int vid, uint16_t vring_idx,
1223 struct virtio_net *dev;
1224 struct vhost_virtqueue *vq;
1226 dev = get_device(vid);
1230 if (unlikely(!(dev->protocol_features &
1231 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1234 if (unlikely(vq_is_packed(dev)))
1237 if (unlikely(vring_idx >= VHOST_MAX_VRING))
1240 vq = dev->virtqueue[vring_idx];
1244 if (unlikely(!vq->inflight_split))
1247 if (unlikely(idx >= vq->size))
1250 vq->inflight_split->last_inflight_io = idx;
1255 rte_vhost_set_last_inflight_io_packed(int vid, uint16_t vring_idx,
1258 struct rte_vhost_inflight_info_packed *inflight_info;
1259 struct virtio_net *dev;
1260 struct vhost_virtqueue *vq;
1263 dev = get_device(vid);
1267 if (unlikely(!(dev->protocol_features &
1268 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1271 if (unlikely(!vq_is_packed(dev)))
1274 if (unlikely(vring_idx >= VHOST_MAX_VRING))
1277 vq = dev->virtqueue[vring_idx];
1281 inflight_info = vq->inflight_packed;
1282 if (unlikely(!inflight_info))
1285 if (unlikely(head >= vq->size))
1288 last = inflight_info->desc[head].last;
1289 if (unlikely(last >= vq->size))
1292 inflight_info->desc[last].next = inflight_info->free_head;
1293 inflight_info->free_head = head;
1294 inflight_info->used_idx += inflight_info->desc[head].num;
1295 if (inflight_info->used_idx >= inflight_info->desc_num) {
1296 inflight_info->used_idx -= inflight_info->desc_num;
1297 inflight_info->used_wrap_counter =
1298 !inflight_info->used_wrap_counter;
1305 rte_vhost_vring_call(int vid, uint16_t vring_idx)
1307 struct virtio_net *dev;
1308 struct vhost_virtqueue *vq;
1310 dev = get_device(vid);
1314 if (vring_idx >= VHOST_MAX_VRING)
1317 vq = dev->virtqueue[vring_idx];
1321 rte_spinlock_lock(&vq->access_lock);
1323 if (vq_is_packed(dev))
1324 vhost_vring_call_packed(dev, vq);
1326 vhost_vring_call_split(dev, vq);
1328 rte_spinlock_unlock(&vq->access_lock);
1334 rte_vhost_avail_entries(int vid, uint16_t queue_id)
1336 struct virtio_net *dev;
1337 struct vhost_virtqueue *vq;
1340 dev = get_device(vid);
1344 if (queue_id >= VHOST_MAX_VRING)
1347 vq = dev->virtqueue[queue_id];
1351 rte_spinlock_lock(&vq->access_lock);
1353 if (unlikely(!vq->enabled || vq->avail == NULL))
1356 ret = *(volatile uint16_t *)&vq->avail->idx - vq->last_used_idx;
1359 rte_spinlock_unlock(&vq->access_lock);
1364 vhost_enable_notify_split(struct virtio_net *dev,
1365 struct vhost_virtqueue *vq, int enable)
1367 if (vq->used == NULL)
1370 if (!(dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))) {
1372 vq->used->flags &= ~VRING_USED_F_NO_NOTIFY;
1374 vq->used->flags |= VRING_USED_F_NO_NOTIFY;
1377 vhost_avail_event(vq) = vq->last_avail_idx;
1383 vhost_enable_notify_packed(struct virtio_net *dev,
1384 struct vhost_virtqueue *vq, int enable)
1388 if (vq->device_event == NULL)
1392 vq->device_event->flags = VRING_EVENT_F_DISABLE;
1396 flags = VRING_EVENT_F_ENABLE;
1397 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX)) {
1398 flags = VRING_EVENT_F_DESC;
1399 vq->device_event->off_wrap = vq->last_avail_idx |
1400 vq->avail_wrap_counter << 15;
1403 rte_atomic_thread_fence(__ATOMIC_RELEASE);
1405 vq->device_event->flags = flags;
1410 vhost_enable_guest_notification(struct virtio_net *dev,
1411 struct vhost_virtqueue *vq, int enable)
1414 * If the virtqueue is not ready yet, it will be applied
1415 * when it will become ready.
1420 if (vq_is_packed(dev))
1421 return vhost_enable_notify_packed(dev, vq, enable);
1423 return vhost_enable_notify_split(dev, vq, enable);
1427 rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable)
1429 struct virtio_net *dev = get_device(vid);
1430 struct vhost_virtqueue *vq;
1436 if (queue_id >= VHOST_MAX_VRING)
1439 vq = dev->virtqueue[queue_id];
1443 rte_spinlock_lock(&vq->access_lock);
1445 vq->notif_enable = enable;
1446 ret = vhost_enable_guest_notification(dev, vq, enable);
1448 rte_spinlock_unlock(&vq->access_lock);
1454 rte_vhost_log_write(int vid, uint64_t addr, uint64_t len)
1456 struct virtio_net *dev = get_device(vid);
1461 vhost_log_write(dev, addr, len);
1465 rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
1466 uint64_t offset, uint64_t len)
1468 struct virtio_net *dev;
1469 struct vhost_virtqueue *vq;
1471 dev = get_device(vid);
1475 if (vring_idx >= VHOST_MAX_VRING)
1477 vq = dev->virtqueue[vring_idx];
1481 vhost_log_used_vring(dev, vq, offset, len);
1485 rte_vhost_rx_queue_count(int vid, uint16_t qid)
1487 struct virtio_net *dev;
1488 struct vhost_virtqueue *vq;
1491 dev = get_device(vid);
1495 if (unlikely(qid >= dev->nr_vring || (qid & 1) == 0)) {
1496 VHOST_LOG_DATA(ERR, "(%s) %s: invalid virtqueue idx %d.\n",
1497 dev->ifname, __func__, qid);
1501 vq = dev->virtqueue[qid];
1505 rte_spinlock_lock(&vq->access_lock);
1507 if (unlikely(!vq->enabled || vq->avail == NULL))
1510 ret = *((volatile uint16_t *)&vq->avail->idx) - vq->last_avail_idx;
1513 rte_spinlock_unlock(&vq->access_lock);
1517 struct rte_vdpa_device *
1518 rte_vhost_get_vdpa_device(int vid)
1520 struct virtio_net *dev = get_device(vid);
1525 return dev->vdpa_dev;
1529 rte_vhost_get_log_base(int vid, uint64_t *log_base,
1532 struct virtio_net *dev = get_device(vid);
1534 if (dev == NULL || log_base == NULL || log_size == NULL)
1537 *log_base = dev->log_base;
1538 *log_size = dev->log_size;
1544 rte_vhost_get_vring_base(int vid, uint16_t queue_id,
1545 uint16_t *last_avail_idx, uint16_t *last_used_idx)
1547 struct vhost_virtqueue *vq;
1548 struct virtio_net *dev = get_device(vid);
1550 if (dev == NULL || last_avail_idx == NULL || last_used_idx == NULL)
1553 if (queue_id >= VHOST_MAX_VRING)
1556 vq = dev->virtqueue[queue_id];
1560 if (vq_is_packed(dev)) {
1561 *last_avail_idx = (vq->avail_wrap_counter << 15) |
1563 *last_used_idx = (vq->used_wrap_counter << 15) |
1566 *last_avail_idx = vq->last_avail_idx;
1567 *last_used_idx = vq->last_used_idx;
1574 rte_vhost_set_vring_base(int vid, uint16_t queue_id,
1575 uint16_t last_avail_idx, uint16_t last_used_idx)
1577 struct vhost_virtqueue *vq;
1578 struct virtio_net *dev = get_device(vid);
1583 if (queue_id >= VHOST_MAX_VRING)
1586 vq = dev->virtqueue[queue_id];
1590 if (vq_is_packed(dev)) {
1591 vq->last_avail_idx = last_avail_idx & 0x7fff;
1592 vq->avail_wrap_counter = !!(last_avail_idx & (1 << 15));
1593 vq->last_used_idx = last_used_idx & 0x7fff;
1594 vq->used_wrap_counter = !!(last_used_idx & (1 << 15));
1596 vq->last_avail_idx = last_avail_idx;
1597 vq->last_used_idx = last_used_idx;
1604 rte_vhost_get_vring_base_from_inflight(int vid,
1606 uint16_t *last_avail_idx,
1607 uint16_t *last_used_idx)
1609 struct rte_vhost_inflight_info_packed *inflight_info;
1610 struct vhost_virtqueue *vq;
1611 struct virtio_net *dev = get_device(vid);
1613 if (dev == NULL || last_avail_idx == NULL || last_used_idx == NULL)
1616 if (queue_id >= VHOST_MAX_VRING)
1619 vq = dev->virtqueue[queue_id];
1623 if (!vq_is_packed(dev))
1626 inflight_info = vq->inflight_packed;
1630 *last_avail_idx = (inflight_info->old_used_wrap_counter << 15) |
1631 inflight_info->old_used_idx;
1632 *last_used_idx = *last_avail_idx;
1638 rte_vhost_extern_callback_register(int vid,
1639 struct rte_vhost_user_extern_ops const * const ops, void *ctx)
1641 struct virtio_net *dev = get_device(vid);
1643 if (dev == NULL || ops == NULL)
1646 dev->extern_ops = *ops;
1647 dev->extern_data = ctx;
1651 static __rte_always_inline int
1652 async_channel_register(int vid, uint16_t queue_id)
1654 struct virtio_net *dev = get_device(vid);
1655 struct vhost_virtqueue *vq = dev->virtqueue[queue_id];
1656 struct vhost_async *async;
1657 int node = vq->numa_node;
1659 if (unlikely(vq->async)) {
1660 VHOST_LOG_CONFIG(ERR,
1661 "(%s) async register failed: already registered (qid: %d)\n",
1662 dev->ifname, queue_id);
1666 async = rte_zmalloc_socket(NULL, sizeof(struct vhost_async), 0, node);
1668 VHOST_LOG_CONFIG(ERR, "(%s) failed to allocate async metadata (qid: %d)\n",
1669 dev->ifname, queue_id);
1673 async->pkts_info = rte_malloc_socket(NULL, vq->size * sizeof(struct async_inflight_info),
1674 RTE_CACHE_LINE_SIZE, node);
1675 if (!async->pkts_info) {
1676 VHOST_LOG_CONFIG(ERR, "(%s) failed to allocate async_pkts_info (qid: %d)\n",
1677 dev->ifname, queue_id);
1678 goto out_free_async;
1681 async->pkts_cmpl_flag = rte_zmalloc_socket(NULL, vq->size * sizeof(bool),
1682 RTE_CACHE_LINE_SIZE, node);
1683 if (!async->pkts_cmpl_flag) {
1684 VHOST_LOG_CONFIG(ERR, "(%s) failed to allocate async pkts_cmpl_flag (qid: %d)\n",
1685 dev->ifname, queue_id);
1686 goto out_free_async;
1689 if (vq_is_packed(dev)) {
1690 async->buffers_packed = rte_malloc_socket(NULL,
1691 vq->size * sizeof(struct vring_used_elem_packed),
1692 RTE_CACHE_LINE_SIZE, node);
1693 if (!async->buffers_packed) {
1694 VHOST_LOG_CONFIG(ERR, "(%s) failed to allocate async buffers (qid: %d)\n",
1695 dev->ifname, queue_id);
1696 goto out_free_inflight;
1699 async->descs_split = rte_malloc_socket(NULL,
1700 vq->size * sizeof(struct vring_used_elem),
1701 RTE_CACHE_LINE_SIZE, node);
1702 if (!async->descs_split) {
1703 VHOST_LOG_CONFIG(ERR, "(%s) failed to allocate async descs (qid: %d)\n",
1704 dev->ifname, queue_id);
1705 goto out_free_inflight;
1713 rte_free(async->pkts_info);
1721 rte_vhost_async_channel_register(int vid, uint16_t queue_id)
1723 struct vhost_virtqueue *vq;
1724 struct virtio_net *dev = get_device(vid);
1730 if (queue_id >= VHOST_MAX_VRING)
1733 vq = dev->virtqueue[queue_id];
1735 if (unlikely(vq == NULL || !dev->async_copy))
1738 rte_spinlock_lock(&vq->access_lock);
1739 ret = async_channel_register(vid, queue_id);
1740 rte_spinlock_unlock(&vq->access_lock);
1746 rte_vhost_async_channel_register_thread_unsafe(int vid, uint16_t queue_id)
1748 struct vhost_virtqueue *vq;
1749 struct virtio_net *dev = get_device(vid);
1754 if (queue_id >= VHOST_MAX_VRING)
1757 vq = dev->virtqueue[queue_id];
1759 if (unlikely(vq == NULL || !dev->async_copy))
1762 return async_channel_register(vid, queue_id);
1766 rte_vhost_async_channel_unregister(int vid, uint16_t queue_id)
1768 struct vhost_virtqueue *vq;
1769 struct virtio_net *dev = get_device(vid);
1775 if (queue_id >= VHOST_MAX_VRING)
1778 vq = dev->virtqueue[queue_id];
1783 if (!rte_spinlock_trylock(&vq->access_lock)) {
1784 VHOST_LOG_CONFIG(ERR, "(%s) failed to unregister async channel, virtqueue busy.\n",
1791 } else if (vq->async->pkts_inflight_n) {
1792 VHOST_LOG_CONFIG(ERR, "(%s) failed to unregister async channel.\n", dev->ifname);
1793 VHOST_LOG_CONFIG(ERR, "(%s) inflight packets must be completed before unregistration.\n",
1796 vhost_free_async_mem(vq);
1800 rte_spinlock_unlock(&vq->access_lock);
1806 rte_vhost_async_channel_unregister_thread_unsafe(int vid, uint16_t queue_id)
1808 struct vhost_virtqueue *vq;
1809 struct virtio_net *dev = get_device(vid);
1814 if (queue_id >= VHOST_MAX_VRING)
1817 vq = dev->virtqueue[queue_id];
1825 if (vq->async->pkts_inflight_n) {
1826 VHOST_LOG_CONFIG(ERR, "(%s) failed to unregister async channel.\n", dev->ifname);
1827 VHOST_LOG_CONFIG(ERR, "(%s) inflight packets must be completed before unregistration.\n",
1832 vhost_free_async_mem(vq);
1838 rte_vhost_async_dma_configure(int16_t dma_id, uint16_t vchan_id)
1840 struct rte_dma_info info;
1841 void *pkts_cmpl_flag_addr;
1844 if (!rte_dma_is_valid(dma_id)) {
1845 VHOST_LOG_CONFIG(ERR, "DMA %d is not found.\n", dma_id);
1849 rte_dma_info_get(dma_id, &info);
1850 if (vchan_id >= info.max_vchans) {
1851 VHOST_LOG_CONFIG(ERR, "Invalid DMA %d vChannel %u.\n", dma_id, vchan_id);
1855 if (!dma_copy_track[dma_id].vchans) {
1856 struct async_dma_vchan_info *vchans;
1858 vchans = rte_zmalloc(NULL, sizeof(struct async_dma_vchan_info) * info.max_vchans,
1859 RTE_CACHE_LINE_SIZE);
1860 if (vchans == NULL) {
1861 VHOST_LOG_CONFIG(ERR, "Failed to allocate vchans for DMA %d vChannel %u.\n",
1866 dma_copy_track[dma_id].vchans = vchans;
1869 if (dma_copy_track[dma_id].vchans[vchan_id].pkts_cmpl_flag_addr) {
1870 VHOST_LOG_CONFIG(INFO, "DMA %d vChannel %u already registered.\n", dma_id,
1875 max_desc = info.max_desc;
1876 if (!rte_is_power_of_2(max_desc))
1877 max_desc = rte_align32pow2(max_desc);
1879 pkts_cmpl_flag_addr = rte_zmalloc(NULL, sizeof(bool *) * max_desc, RTE_CACHE_LINE_SIZE);
1880 if (!pkts_cmpl_flag_addr) {
1881 VHOST_LOG_CONFIG(ERR, "Failed to allocate pkts_cmpl_flag_addr for DMA %d "
1882 "vChannel %u.\n", dma_id, vchan_id);
1884 if (dma_copy_track[dma_id].nr_vchans == 0) {
1885 rte_free(dma_copy_track[dma_id].vchans);
1886 dma_copy_track[dma_id].vchans = NULL;
1891 dma_copy_track[dma_id].vchans[vchan_id].pkts_cmpl_flag_addr = pkts_cmpl_flag_addr;
1892 dma_copy_track[dma_id].vchans[vchan_id].ring_size = max_desc;
1893 dma_copy_track[dma_id].vchans[vchan_id].ring_mask = max_desc - 1;
1894 dma_copy_track[dma_id].nr_vchans++;
1900 rte_vhost_async_get_inflight(int vid, uint16_t queue_id)
1902 struct vhost_virtqueue *vq;
1903 struct virtio_net *dev = get_device(vid);
1909 if (queue_id >= VHOST_MAX_VRING)
1912 vq = dev->virtqueue[queue_id];
1917 if (!rte_spinlock_trylock(&vq->access_lock)) {
1918 VHOST_LOG_CONFIG(DEBUG,
1919 "(%s) failed to check in-flight packets. virtqueue busy.\n",
1925 ret = vq->async->pkts_inflight_n;
1927 rte_spinlock_unlock(&vq->access_lock);
1933 rte_vhost_async_get_inflight_thread_unsafe(int vid, uint16_t queue_id)
1935 struct vhost_virtqueue *vq;
1936 struct virtio_net *dev = get_device(vid);
1942 if (queue_id >= VHOST_MAX_VRING)
1945 vq = dev->virtqueue[queue_id];
1953 ret = vq->async->pkts_inflight_n;
1959 rte_vhost_get_monitor_addr(int vid, uint16_t queue_id,
1960 struct rte_vhost_power_monitor_cond *pmc)
1962 struct virtio_net *dev = get_device(vid);
1963 struct vhost_virtqueue *vq;
1967 if (queue_id >= VHOST_MAX_VRING)
1970 vq = dev->virtqueue[queue_id];
1974 if (vq_is_packed(dev)) {
1975 struct vring_packed_desc *desc;
1976 desc = vq->desc_packed;
1977 pmc->addr = &desc[vq->last_avail_idx].flags;
1978 if (vq->avail_wrap_counter)
1979 pmc->val = VRING_DESC_F_AVAIL;
1981 pmc->val = VRING_DESC_F_USED;
1982 pmc->mask = VRING_DESC_F_AVAIL | VRING_DESC_F_USED;
1983 pmc->size = sizeof(desc[vq->last_avail_idx].flags);
1986 pmc->addr = &vq->avail->idx;
1987 pmc->val = vq->last_avail_idx & (vq->size - 1);
1988 pmc->mask = vq->size - 1;
1989 pmc->size = sizeof(vq->avail->idx);
1998 rte_vhost_vring_stats_get_names(int vid, uint16_t queue_id,
1999 struct rte_vhost_stat_name *name, unsigned int size)
2001 struct virtio_net *dev = get_device(vid);
2007 if (queue_id >= dev->nr_vring)
2010 if (!(dev->flags & VIRTIO_DEV_STATS_ENABLED))
2013 if (name == NULL || size < VHOST_NB_VQ_STATS)
2014 return VHOST_NB_VQ_STATS;
2016 for (i = 0; i < VHOST_NB_VQ_STATS; i++)
2017 snprintf(name[i].name, sizeof(name[i].name), "%s_q%u_%s",
2018 (queue_id & 1) ? "rx" : "tx",
2019 queue_id / 2, vhost_vq_stat_strings[i].name);
2021 return VHOST_NB_VQ_STATS;
2025 rte_vhost_vring_stats_get(int vid, uint16_t queue_id,
2026 struct rte_vhost_stat *stats, unsigned int n)
2028 struct virtio_net *dev = get_device(vid);
2029 struct vhost_virtqueue *vq;
2035 if (queue_id >= dev->nr_vring)
2038 if (!(dev->flags & VIRTIO_DEV_STATS_ENABLED))
2041 if (stats == NULL || n < VHOST_NB_VQ_STATS)
2042 return VHOST_NB_VQ_STATS;
2044 vq = dev->virtqueue[queue_id];
2046 rte_spinlock_lock(&vq->access_lock);
2047 for (i = 0; i < VHOST_NB_VQ_STATS; i++) {
2049 *(uint64_t *)(((char *)vq) + vhost_vq_stat_strings[i].offset);
2052 rte_spinlock_unlock(&vq->access_lock);
2054 return VHOST_NB_VQ_STATS;
2057 int rte_vhost_vring_stats_reset(int vid, uint16_t queue_id)
2059 struct virtio_net *dev = get_device(vid);
2060 struct vhost_virtqueue *vq;
2065 if (queue_id >= dev->nr_vring)
2068 if (!(dev->flags & VIRTIO_DEV_STATS_ENABLED))
2071 vq = dev->virtqueue[queue_id];
2073 rte_spinlock_lock(&vq->access_lock);
2074 memset(&vq->stats, 0, sizeof(vq->stats));
2075 rte_spinlock_unlock(&vq->access_lock);
2080 RTE_LOG_REGISTER_SUFFIX(vhost_config_log_level, config, INFO);
2081 RTE_LOG_REGISTER_SUFFIX(vhost_data_log_level, data, WARNING);