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)},
48 {"inflight_submitted", offsetof(struct vhost_virtqueue, stats.inflight_submitted)},
49 {"inflight_completed", offsetof(struct vhost_virtqueue, stats.inflight_completed)},
52 #define VHOST_NB_VQ_STATS RTE_DIM(vhost_vq_stat_strings)
54 /* Called with iotlb_lock read-locked */
56 __vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
57 uint64_t iova, uint64_t *size, uint8_t perm)
59 uint64_t vva, tmp_size;
66 vva = vhost_user_iotlb_cache_find(vq, iova, &tmp_size, perm);
67 if (tmp_size == *size) {
68 if (dev->flags & VIRTIO_DEV_STATS_ENABLED)
69 vq->stats.iotlb_hits++;
73 if (dev->flags & VIRTIO_DEV_STATS_ENABLED)
74 vq->stats.iotlb_misses++;
78 if (!vhost_user_iotlb_pending_miss(vq, iova, perm)) {
80 * iotlb_lock is read-locked for a full burst,
81 * but it only protects the iotlb cache.
82 * In case of IOTLB miss, we might block on the socket,
83 * which could cause a deadlock with QEMU if an IOTLB update
84 * is being handled. We can safely unlock here to avoid it.
86 vhost_user_iotlb_rd_unlock(vq);
88 vhost_user_iotlb_pending_insert(dev, vq, iova, perm);
89 if (vhost_user_iotlb_miss(dev, iova, perm)) {
90 VHOST_LOG_DATA(ERR, "(%s) IOTLB miss req failed for IOVA 0x%" PRIx64 "\n",
92 vhost_user_iotlb_pending_remove(vq, iova, 1, perm);
95 vhost_user_iotlb_rd_lock(vq);
101 #define VHOST_LOG_PAGE 4096
104 * Atomically set a bit in memory.
106 static __rte_always_inline void
107 vhost_set_bit(unsigned int nr, volatile uint8_t *addr)
109 #if defined(RTE_TOOLCHAIN_GCC) && (GCC_VERSION < 70100)
111 * __sync_ built-ins are deprecated, but __atomic_ ones
112 * are sub-optimized in older GCC versions.
114 __sync_fetch_and_or_1(addr, (1U << nr));
116 __atomic_fetch_or(addr, (1U << nr), __ATOMIC_RELAXED);
120 static __rte_always_inline void
121 vhost_log_page(uint8_t *log_base, uint64_t page)
123 vhost_set_bit(page % 8, &log_base[page / 8]);
127 __vhost_log_write(struct virtio_net *dev, uint64_t addr, uint64_t len)
131 if (unlikely(!dev->log_base || !len))
134 if (unlikely(dev->log_size <= ((addr + len - 1) / VHOST_LOG_PAGE / 8)))
137 /* To make sure guest memory updates are committed before logging */
138 rte_atomic_thread_fence(__ATOMIC_RELEASE);
140 page = addr / VHOST_LOG_PAGE;
141 while (page * VHOST_LOG_PAGE < addr + len) {
142 vhost_log_page((uint8_t *)(uintptr_t)dev->log_base, page);
148 __vhost_log_write_iova(struct virtio_net *dev, struct vhost_virtqueue *vq,
149 uint64_t iova, uint64_t len)
151 uint64_t hva, gpa, map_len;
154 hva = __vhost_iova_to_vva(dev, vq, iova, &map_len, VHOST_ACCESS_RW);
155 if (map_len != len) {
157 "(%s) failed to write log for IOVA 0x%" PRIx64 ". No IOTLB entry found\n",
162 gpa = hva_to_gpa(dev, hva, len);
164 __vhost_log_write(dev, gpa, len);
168 __vhost_log_cache_sync(struct virtio_net *dev, struct vhost_virtqueue *vq)
170 unsigned long *log_base;
173 if (unlikely(!dev->log_base))
176 /* No cache, nothing to sync */
177 if (unlikely(!vq->log_cache))
180 rte_atomic_thread_fence(__ATOMIC_RELEASE);
182 log_base = (unsigned long *)(uintptr_t)dev->log_base;
184 for (i = 0; i < vq->log_cache_nb_elem; i++) {
185 struct log_cache_entry *elem = vq->log_cache + i;
187 #if defined(RTE_TOOLCHAIN_GCC) && (GCC_VERSION < 70100)
189 * '__sync' builtins are deprecated, but '__atomic' ones
190 * are sub-optimized in older GCC versions.
192 __sync_fetch_and_or(log_base + elem->offset, elem->val);
194 __atomic_fetch_or(log_base + elem->offset, elem->val,
199 rte_atomic_thread_fence(__ATOMIC_RELEASE);
201 vq->log_cache_nb_elem = 0;
204 static __rte_always_inline void
205 vhost_log_cache_page(struct virtio_net *dev, struct vhost_virtqueue *vq,
208 uint32_t bit_nr = page % (sizeof(unsigned long) << 3);
209 uint32_t offset = page / (sizeof(unsigned long) << 3);
212 if (unlikely(!vq->log_cache)) {
213 /* No logging cache allocated, write dirty log map directly */
214 rte_atomic_thread_fence(__ATOMIC_RELEASE);
215 vhost_log_page((uint8_t *)(uintptr_t)dev->log_base, page);
220 for (i = 0; i < vq->log_cache_nb_elem; i++) {
221 struct log_cache_entry *elem = vq->log_cache + i;
223 if (elem->offset == offset) {
224 elem->val |= (1UL << bit_nr);
229 if (unlikely(i >= VHOST_LOG_CACHE_NR)) {
231 * No more room for a new log cache entry,
232 * so write the dirty log map directly.
234 rte_atomic_thread_fence(__ATOMIC_RELEASE);
235 vhost_log_page((uint8_t *)(uintptr_t)dev->log_base, page);
240 vq->log_cache[i].offset = offset;
241 vq->log_cache[i].val = (1UL << bit_nr);
242 vq->log_cache_nb_elem++;
246 __vhost_log_cache_write(struct virtio_net *dev, struct vhost_virtqueue *vq,
247 uint64_t addr, uint64_t len)
251 if (unlikely(!dev->log_base || !len))
254 if (unlikely(dev->log_size <= ((addr + len - 1) / VHOST_LOG_PAGE / 8)))
257 page = addr / VHOST_LOG_PAGE;
258 while (page * VHOST_LOG_PAGE < addr + len) {
259 vhost_log_cache_page(dev, vq, page);
265 __vhost_log_cache_write_iova(struct virtio_net *dev, struct vhost_virtqueue *vq,
266 uint64_t iova, uint64_t len)
268 uint64_t hva, gpa, map_len;
271 hva = __vhost_iova_to_vva(dev, vq, iova, &map_len, VHOST_ACCESS_RW);
272 if (map_len != len) {
274 "(%s) failed to write log for IOVA 0x%" PRIx64 ". No IOTLB entry found\n",
279 gpa = hva_to_gpa(dev, hva, len);
281 __vhost_log_cache_write(dev, vq, gpa, len);
285 vhost_alloc_copy_ind_table(struct virtio_net *dev, struct vhost_virtqueue *vq,
286 uint64_t desc_addr, uint64_t desc_len)
290 uint64_t len, remain = desc_len;
292 idesc = rte_malloc_socket(__func__, desc_len, 0, vq->numa_node);
293 if (unlikely(!idesc))
296 dst = (uint64_t)(uintptr_t)idesc;
300 src = vhost_iova_to_vva(dev, vq, desc_addr, &len,
302 if (unlikely(!src || !len)) {
307 rte_memcpy((void *)(uintptr_t)dst, (void *)(uintptr_t)src, len);
318 cleanup_vq(struct vhost_virtqueue *vq, int destroy)
320 if ((vq->callfd >= 0) && (destroy != 0))
327 cleanup_vq_inflight(struct virtio_net *dev, struct vhost_virtqueue *vq)
329 if (!(dev->protocol_features &
330 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD)))
333 if (vq_is_packed(dev)) {
334 if (vq->inflight_packed)
335 vq->inflight_packed = NULL;
337 if (vq->inflight_split)
338 vq->inflight_split = NULL;
341 if (vq->resubmit_inflight) {
342 if (vq->resubmit_inflight->resubmit_list) {
343 rte_free(vq->resubmit_inflight->resubmit_list);
344 vq->resubmit_inflight->resubmit_list = NULL;
346 rte_free(vq->resubmit_inflight);
347 vq->resubmit_inflight = NULL;
352 * Unmap any memory, close any file descriptors and
353 * free any memory owned by a device.
356 cleanup_device(struct virtio_net *dev, int destroy)
360 vhost_backend_cleanup(dev);
362 for (i = 0; i < dev->nr_vring; i++) {
363 cleanup_vq(dev->virtqueue[i], destroy);
364 cleanup_vq_inflight(dev, dev->virtqueue[i]);
369 vhost_free_async_mem(struct vhost_virtqueue *vq)
374 rte_free(vq->async->pkts_info);
375 rte_free(vq->async->pkts_cmpl_flag);
377 rte_free(vq->async->buffers_packed);
378 vq->async->buffers_packed = NULL;
379 rte_free(vq->async->descs_split);
380 vq->async->descs_split = NULL;
387 free_vq(struct virtio_net *dev, struct vhost_virtqueue *vq)
389 if (vq_is_packed(dev))
390 rte_free(vq->shadow_used_packed);
392 rte_free(vq->shadow_used_split);
394 vhost_free_async_mem(vq);
395 rte_free(vq->batch_copy_elems);
396 rte_mempool_free(vq->iotlb_pool);
397 rte_free(vq->log_cache);
402 * Release virtqueues and device memory.
405 free_device(struct virtio_net *dev)
409 for (i = 0; i < dev->nr_vring; i++)
410 free_vq(dev, dev->virtqueue[i]);
415 static __rte_always_inline int
416 log_translate(struct virtio_net *dev, struct vhost_virtqueue *vq)
418 if (likely(!(vq->ring_addrs.flags & (1 << VHOST_VRING_F_LOG))))
421 vq->log_guest_addr = translate_log_addr(dev, vq,
422 vq->ring_addrs.log_guest_addr);
423 if (vq->log_guest_addr == 0)
430 * Converts vring log address to GPA
431 * If IOMMU is enabled, the log address is IOVA
432 * If IOMMU not enabled, the log address is already GPA
434 * Caller should have iotlb_lock read-locked
437 translate_log_addr(struct virtio_net *dev, struct vhost_virtqueue *vq,
440 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)) {
441 const uint64_t exp_size = sizeof(uint64_t);
443 uint64_t size = exp_size;
445 hva = vhost_iova_to_vva(dev, vq, log_addr,
446 &size, VHOST_ACCESS_RW);
448 if (size != exp_size)
451 gpa = hva_to_gpa(dev, hva, exp_size);
454 "(%s) failed to find GPA for log_addr: 0x%"
455 PRIx64 " hva: 0x%" PRIx64 "\n",
456 dev->ifname, log_addr, hva);
465 /* Caller should have iotlb_lock read-locked */
467 vring_translate_split(struct virtio_net *dev, struct vhost_virtqueue *vq)
469 uint64_t req_size, size;
471 req_size = sizeof(struct vring_desc) * vq->size;
473 vq->desc = (struct vring_desc *)(uintptr_t)vhost_iova_to_vva(dev, vq,
474 vq->ring_addrs.desc_user_addr,
475 &size, VHOST_ACCESS_RW);
476 if (!vq->desc || size != req_size)
479 req_size = sizeof(struct vring_avail);
480 req_size += sizeof(uint16_t) * vq->size;
481 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))
482 req_size += sizeof(uint16_t);
484 vq->avail = (struct vring_avail *)(uintptr_t)vhost_iova_to_vva(dev, vq,
485 vq->ring_addrs.avail_user_addr,
486 &size, VHOST_ACCESS_RW);
487 if (!vq->avail || size != req_size)
490 req_size = sizeof(struct vring_used);
491 req_size += sizeof(struct vring_used_elem) * vq->size;
492 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))
493 req_size += sizeof(uint16_t);
495 vq->used = (struct vring_used *)(uintptr_t)vhost_iova_to_vva(dev, vq,
496 vq->ring_addrs.used_user_addr,
497 &size, VHOST_ACCESS_RW);
498 if (!vq->used || size != req_size)
504 /* Caller should have iotlb_lock read-locked */
506 vring_translate_packed(struct virtio_net *dev, struct vhost_virtqueue *vq)
508 uint64_t req_size, size;
510 req_size = sizeof(struct vring_packed_desc) * vq->size;
512 vq->desc_packed = (struct vring_packed_desc *)(uintptr_t)
513 vhost_iova_to_vva(dev, vq, vq->ring_addrs.desc_user_addr,
514 &size, VHOST_ACCESS_RW);
515 if (!vq->desc_packed || size != req_size)
518 req_size = sizeof(struct vring_packed_desc_event);
520 vq->driver_event = (struct vring_packed_desc_event *)(uintptr_t)
521 vhost_iova_to_vva(dev, vq, vq->ring_addrs.avail_user_addr,
522 &size, VHOST_ACCESS_RW);
523 if (!vq->driver_event || size != req_size)
526 req_size = sizeof(struct vring_packed_desc_event);
528 vq->device_event = (struct vring_packed_desc_event *)(uintptr_t)
529 vhost_iova_to_vva(dev, vq, vq->ring_addrs.used_user_addr,
530 &size, VHOST_ACCESS_RW);
531 if (!vq->device_event || size != req_size)
538 vring_translate(struct virtio_net *dev, struct vhost_virtqueue *vq)
541 if (!(dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)))
544 if (vq_is_packed(dev)) {
545 if (vring_translate_packed(dev, vq) < 0)
548 if (vring_translate_split(dev, vq) < 0)
552 if (log_translate(dev, vq) < 0)
555 vq->access_ok = true;
561 vring_invalidate(struct virtio_net *dev, struct vhost_virtqueue *vq)
563 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
564 vhost_user_iotlb_wr_lock(vq);
566 vq->access_ok = false;
570 vq->log_guest_addr = 0;
572 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
573 vhost_user_iotlb_wr_unlock(vq);
577 init_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
579 struct vhost_virtqueue *vq;
580 int numa_node = SOCKET_ID_ANY;
582 if (vring_idx >= VHOST_MAX_VRING) {
583 VHOST_LOG_CONFIG(ERR, "(%s) failed to init vring, out of bound (%d)\n",
584 dev->ifname, vring_idx);
588 vq = dev->virtqueue[vring_idx];
590 VHOST_LOG_CONFIG(ERR, "(%s) virtqueue not allocated (%d)\n",
591 dev->ifname, vring_idx);
595 memset(vq, 0, sizeof(struct vhost_virtqueue));
597 vq->kickfd = VIRTIO_UNINITIALIZED_EVENTFD;
598 vq->callfd = VIRTIO_UNINITIALIZED_EVENTFD;
599 vq->notif_enable = VIRTIO_UNINITIALIZED_NOTIF;
601 #ifdef RTE_LIBRTE_VHOST_NUMA
602 if (get_mempolicy(&numa_node, NULL, 0, vq, MPOL_F_NODE | MPOL_F_ADDR)) {
603 VHOST_LOG_CONFIG(ERR, "(%s) failed to query numa node: %s\n",
604 dev->ifname, rte_strerror(errno));
605 numa_node = SOCKET_ID_ANY;
608 vq->numa_node = numa_node;
610 vhost_user_iotlb_init(dev, vring_idx);
614 reset_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
616 struct vhost_virtqueue *vq;
619 if (vring_idx >= VHOST_MAX_VRING) {
620 VHOST_LOG_CONFIG(ERR,
621 "(%s) failed to reset vring, out of bound (%d)\n",
622 dev->ifname, vring_idx);
626 vq = dev->virtqueue[vring_idx];
628 VHOST_LOG_CONFIG(ERR, "(%s) failed to reset vring, virtqueue not allocated (%d)\n",
629 dev->ifname, vring_idx);
634 init_vring_queue(dev, vring_idx);
639 alloc_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
641 struct vhost_virtqueue *vq;
644 /* Also allocate holes, if any, up to requested vring index. */
645 for (i = 0; i <= vring_idx; i++) {
646 if (dev->virtqueue[i])
649 vq = rte_zmalloc(NULL, sizeof(struct vhost_virtqueue), 0);
651 VHOST_LOG_CONFIG(ERR, "(%s) failed to allocate memory for vring %u.\n",
656 dev->virtqueue[i] = vq;
657 init_vring_queue(dev, i);
658 rte_spinlock_init(&vq->access_lock);
659 vq->avail_wrap_counter = 1;
660 vq->used_wrap_counter = 1;
661 vq->signalled_used_valid = false;
664 dev->nr_vring = RTE_MAX(dev->nr_vring, vring_idx + 1);
670 * Reset some variables in device structure, while keeping few
671 * others untouched, such as vid, ifname, nr_vring: they
672 * should be same unless the device is removed.
675 reset_device(struct virtio_net *dev)
680 dev->protocol_features = 0;
681 dev->flags &= VIRTIO_DEV_BUILTIN_VIRTIO_NET;
683 for (i = 0; i < dev->nr_vring; i++)
684 reset_vring_queue(dev, i);
688 * Invoked when there is a new vhost-user connection established (when
689 * there is a new virtio device being attached).
692 vhost_new_device(void)
694 struct virtio_net *dev;
697 pthread_mutex_lock(&vhost_dev_lock);
698 for (i = 0; i < RTE_MAX_VHOST_DEVICE; i++) {
699 if (vhost_devices[i] == NULL)
703 if (i == RTE_MAX_VHOST_DEVICE) {
704 VHOST_LOG_CONFIG(ERR, "failed to find a free slot for new device.\n");
705 pthread_mutex_unlock(&vhost_dev_lock);
709 dev = rte_zmalloc(NULL, sizeof(struct virtio_net), 0);
711 VHOST_LOG_CONFIG(ERR, "failed to allocate memory for new device.\n");
712 pthread_mutex_unlock(&vhost_dev_lock);
716 vhost_devices[i] = dev;
717 pthread_mutex_unlock(&vhost_dev_lock);
720 dev->flags = VIRTIO_DEV_BUILTIN_VIRTIO_NET;
721 dev->slave_req_fd = -1;
722 dev->postcopy_ufd = -1;
723 rte_spinlock_init(&dev->slave_req_lock);
729 vhost_destroy_device_notify(struct virtio_net *dev)
731 struct rte_vdpa_device *vdpa_dev;
733 if (dev->flags & VIRTIO_DEV_RUNNING) {
734 vdpa_dev = dev->vdpa_dev;
736 vdpa_dev->ops->dev_close(dev->vid);
737 dev->flags &= ~VIRTIO_DEV_RUNNING;
738 dev->notify_ops->destroy_device(dev->vid);
743 * Invoked when there is the vhost-user connection is broken (when
744 * the virtio device is being detached).
747 vhost_destroy_device(int vid)
749 struct virtio_net *dev = get_device(vid);
754 vhost_destroy_device_notify(dev);
756 cleanup_device(dev, 1);
759 vhost_devices[vid] = NULL;
763 vhost_attach_vdpa_device(int vid, struct rte_vdpa_device *vdpa_dev)
765 struct virtio_net *dev = get_device(vid);
770 dev->vdpa_dev = vdpa_dev;
774 vhost_set_ifname(int vid, const char *if_name, unsigned int if_len)
776 struct virtio_net *dev;
779 dev = get_device(vid);
783 len = if_len > sizeof(dev->ifname) ?
784 sizeof(dev->ifname) : if_len;
786 strncpy(dev->ifname, if_name, len);
787 dev->ifname[sizeof(dev->ifname) - 1] = '\0';
791 vhost_setup_virtio_net(int vid, bool enable, bool compliant_ol_flags, bool stats_enabled)
793 struct virtio_net *dev = get_device(vid);
799 dev->flags |= VIRTIO_DEV_BUILTIN_VIRTIO_NET;
801 dev->flags &= ~VIRTIO_DEV_BUILTIN_VIRTIO_NET;
802 if (!compliant_ol_flags)
803 dev->flags |= VIRTIO_DEV_LEGACY_OL_FLAGS;
805 dev->flags &= ~VIRTIO_DEV_LEGACY_OL_FLAGS;
807 dev->flags |= VIRTIO_DEV_STATS_ENABLED;
809 dev->flags &= ~VIRTIO_DEV_STATS_ENABLED;
813 vhost_enable_extbuf(int vid)
815 struct virtio_net *dev = get_device(vid);
824 vhost_enable_linearbuf(int vid)
826 struct virtio_net *dev = get_device(vid);
835 rte_vhost_get_mtu(int vid, uint16_t *mtu)
837 struct virtio_net *dev = get_device(vid);
839 if (dev == NULL || mtu == NULL)
842 if (!(dev->flags & VIRTIO_DEV_READY))
845 if (!(dev->features & (1ULL << VIRTIO_NET_F_MTU)))
854 rte_vhost_get_numa_node(int vid)
856 #ifdef RTE_LIBRTE_VHOST_NUMA
857 struct virtio_net *dev = get_device(vid);
861 if (dev == NULL || numa_available() != 0)
864 ret = get_mempolicy(&numa_node, NULL, 0, dev,
865 MPOL_F_NODE | MPOL_F_ADDR);
867 VHOST_LOG_CONFIG(ERR, "(%s) failed to query numa node: %s\n",
868 dev->ifname, rte_strerror(errno));
880 rte_vhost_get_queue_num(int vid)
882 struct virtio_net *dev = get_device(vid);
887 return dev->nr_vring / 2;
891 rte_vhost_get_vring_num(int vid)
893 struct virtio_net *dev = get_device(vid);
898 return dev->nr_vring;
902 rte_vhost_get_ifname(int vid, char *buf, size_t len)
904 struct virtio_net *dev = get_device(vid);
906 if (dev == NULL || buf == NULL)
909 len = RTE_MIN(len, sizeof(dev->ifname));
911 strncpy(buf, dev->ifname, len);
918 rte_vhost_get_negotiated_features(int vid, uint64_t *features)
920 struct virtio_net *dev;
922 dev = get_device(vid);
923 if (dev == NULL || features == NULL)
926 *features = dev->features;
931 rte_vhost_get_negotiated_protocol_features(int vid,
932 uint64_t *protocol_features)
934 struct virtio_net *dev;
936 dev = get_device(vid);
937 if (dev == NULL || protocol_features == NULL)
940 *protocol_features = dev->protocol_features;
945 rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem)
947 struct virtio_net *dev;
948 struct rte_vhost_memory *m;
951 dev = get_device(vid);
952 if (dev == NULL || mem == NULL)
955 size = dev->mem->nregions * sizeof(struct rte_vhost_mem_region);
956 m = malloc(sizeof(struct rte_vhost_memory) + size);
960 m->nregions = dev->mem->nregions;
961 memcpy(m->regions, dev->mem->regions, size);
968 rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
969 struct rte_vhost_vring *vring)
971 struct virtio_net *dev;
972 struct vhost_virtqueue *vq;
974 dev = get_device(vid);
975 if (dev == NULL || vring == NULL)
978 if (vring_idx >= VHOST_MAX_VRING)
981 vq = dev->virtqueue[vring_idx];
985 if (vq_is_packed(dev)) {
986 vring->desc_packed = vq->desc_packed;
987 vring->driver_event = vq->driver_event;
988 vring->device_event = vq->device_event;
990 vring->desc = vq->desc;
991 vring->avail = vq->avail;
992 vring->used = vq->used;
994 vring->log_guest_addr = vq->log_guest_addr;
996 vring->callfd = vq->callfd;
997 vring->kickfd = vq->kickfd;
998 vring->size = vq->size;
1004 rte_vhost_get_vhost_ring_inflight(int vid, uint16_t vring_idx,
1005 struct rte_vhost_ring_inflight *vring)
1007 struct virtio_net *dev;
1008 struct vhost_virtqueue *vq;
1010 dev = get_device(vid);
1014 if (vring_idx >= VHOST_MAX_VRING)
1017 vq = dev->virtqueue[vring_idx];
1021 if (vq_is_packed(dev)) {
1022 if (unlikely(!vq->inflight_packed))
1025 vring->inflight_packed = vq->inflight_packed;
1027 if (unlikely(!vq->inflight_split))
1030 vring->inflight_split = vq->inflight_split;
1033 vring->resubmit_inflight = vq->resubmit_inflight;
1039 rte_vhost_set_inflight_desc_split(int vid, uint16_t vring_idx,
1042 struct vhost_virtqueue *vq;
1043 struct virtio_net *dev;
1045 dev = get_device(vid);
1049 if (unlikely(!(dev->protocol_features &
1050 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1053 if (unlikely(vq_is_packed(dev)))
1056 if (unlikely(vring_idx >= VHOST_MAX_VRING))
1059 vq = dev->virtqueue[vring_idx];
1063 if (unlikely(!vq->inflight_split))
1066 if (unlikely(idx >= vq->size))
1069 vq->inflight_split->desc[idx].counter = vq->global_counter++;
1070 vq->inflight_split->desc[idx].inflight = 1;
1075 rte_vhost_set_inflight_desc_packed(int vid, uint16_t vring_idx,
1076 uint16_t head, uint16_t last,
1077 uint16_t *inflight_entry)
1079 struct rte_vhost_inflight_info_packed *inflight_info;
1080 struct virtio_net *dev;
1081 struct vhost_virtqueue *vq;
1082 struct vring_packed_desc *desc;
1083 uint16_t old_free_head, free_head;
1085 dev = get_device(vid);
1089 if (unlikely(!(dev->protocol_features &
1090 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1093 if (unlikely(!vq_is_packed(dev)))
1096 if (unlikely(vring_idx >= VHOST_MAX_VRING))
1099 vq = dev->virtqueue[vring_idx];
1103 inflight_info = vq->inflight_packed;
1104 if (unlikely(!inflight_info))
1107 if (unlikely(head >= vq->size))
1110 desc = vq->desc_packed;
1111 old_free_head = inflight_info->old_free_head;
1112 if (unlikely(old_free_head >= vq->size))
1115 free_head = old_free_head;
1117 /* init header descriptor */
1118 inflight_info->desc[old_free_head].num = 0;
1119 inflight_info->desc[old_free_head].counter = vq->global_counter++;
1120 inflight_info->desc[old_free_head].inflight = 1;
1122 /* save desc entry in flight entry */
1123 while (head != ((last + 1) % vq->size)) {
1124 inflight_info->desc[old_free_head].num++;
1125 inflight_info->desc[free_head].addr = desc[head].addr;
1126 inflight_info->desc[free_head].len = desc[head].len;
1127 inflight_info->desc[free_head].flags = desc[head].flags;
1128 inflight_info->desc[free_head].id = desc[head].id;
1130 inflight_info->desc[old_free_head].last = free_head;
1131 free_head = inflight_info->desc[free_head].next;
1132 inflight_info->free_head = free_head;
1133 head = (head + 1) % vq->size;
1136 inflight_info->old_free_head = free_head;
1137 *inflight_entry = old_free_head;
1143 rte_vhost_clr_inflight_desc_split(int vid, uint16_t vring_idx,
1144 uint16_t last_used_idx, uint16_t idx)
1146 struct virtio_net *dev;
1147 struct vhost_virtqueue *vq;
1149 dev = get_device(vid);
1153 if (unlikely(!(dev->protocol_features &
1154 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1157 if (unlikely(vq_is_packed(dev)))
1160 if (unlikely(vring_idx >= VHOST_MAX_VRING))
1163 vq = dev->virtqueue[vring_idx];
1167 if (unlikely(!vq->inflight_split))
1170 if (unlikely(idx >= vq->size))
1173 rte_atomic_thread_fence(__ATOMIC_SEQ_CST);
1175 vq->inflight_split->desc[idx].inflight = 0;
1177 rte_atomic_thread_fence(__ATOMIC_SEQ_CST);
1179 vq->inflight_split->used_idx = last_used_idx;
1184 rte_vhost_clr_inflight_desc_packed(int vid, uint16_t vring_idx,
1187 struct rte_vhost_inflight_info_packed *inflight_info;
1188 struct virtio_net *dev;
1189 struct vhost_virtqueue *vq;
1191 dev = get_device(vid);
1195 if (unlikely(!(dev->protocol_features &
1196 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1199 if (unlikely(!vq_is_packed(dev)))
1202 if (unlikely(vring_idx >= VHOST_MAX_VRING))
1205 vq = dev->virtqueue[vring_idx];
1209 inflight_info = vq->inflight_packed;
1210 if (unlikely(!inflight_info))
1213 if (unlikely(head >= vq->size))
1216 rte_atomic_thread_fence(__ATOMIC_SEQ_CST);
1218 inflight_info->desc[head].inflight = 0;
1220 rte_atomic_thread_fence(__ATOMIC_SEQ_CST);
1222 inflight_info->old_free_head = inflight_info->free_head;
1223 inflight_info->old_used_idx = inflight_info->used_idx;
1224 inflight_info->old_used_wrap_counter = inflight_info->used_wrap_counter;
1230 rte_vhost_set_last_inflight_io_split(int vid, uint16_t vring_idx,
1233 struct virtio_net *dev;
1234 struct vhost_virtqueue *vq;
1236 dev = get_device(vid);
1240 if (unlikely(!(dev->protocol_features &
1241 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1244 if (unlikely(vq_is_packed(dev)))
1247 if (unlikely(vring_idx >= VHOST_MAX_VRING))
1250 vq = dev->virtqueue[vring_idx];
1254 if (unlikely(!vq->inflight_split))
1257 if (unlikely(idx >= vq->size))
1260 vq->inflight_split->last_inflight_io = idx;
1265 rte_vhost_set_last_inflight_io_packed(int vid, uint16_t vring_idx,
1268 struct rte_vhost_inflight_info_packed *inflight_info;
1269 struct virtio_net *dev;
1270 struct vhost_virtqueue *vq;
1273 dev = get_device(vid);
1277 if (unlikely(!(dev->protocol_features &
1278 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1281 if (unlikely(!vq_is_packed(dev)))
1284 if (unlikely(vring_idx >= VHOST_MAX_VRING))
1287 vq = dev->virtqueue[vring_idx];
1291 inflight_info = vq->inflight_packed;
1292 if (unlikely(!inflight_info))
1295 if (unlikely(head >= vq->size))
1298 last = inflight_info->desc[head].last;
1299 if (unlikely(last >= vq->size))
1302 inflight_info->desc[last].next = inflight_info->free_head;
1303 inflight_info->free_head = head;
1304 inflight_info->used_idx += inflight_info->desc[head].num;
1305 if (inflight_info->used_idx >= inflight_info->desc_num) {
1306 inflight_info->used_idx -= inflight_info->desc_num;
1307 inflight_info->used_wrap_counter =
1308 !inflight_info->used_wrap_counter;
1315 rte_vhost_vring_call(int vid, uint16_t vring_idx)
1317 struct virtio_net *dev;
1318 struct vhost_virtqueue *vq;
1320 dev = get_device(vid);
1324 if (vring_idx >= VHOST_MAX_VRING)
1327 vq = dev->virtqueue[vring_idx];
1331 rte_spinlock_lock(&vq->access_lock);
1333 if (vq_is_packed(dev))
1334 vhost_vring_call_packed(dev, vq);
1336 vhost_vring_call_split(dev, vq);
1338 rte_spinlock_unlock(&vq->access_lock);
1344 rte_vhost_avail_entries(int vid, uint16_t queue_id)
1346 struct virtio_net *dev;
1347 struct vhost_virtqueue *vq;
1350 dev = get_device(vid);
1354 if (queue_id >= VHOST_MAX_VRING)
1357 vq = dev->virtqueue[queue_id];
1361 rte_spinlock_lock(&vq->access_lock);
1363 if (unlikely(!vq->enabled || vq->avail == NULL))
1366 ret = *(volatile uint16_t *)&vq->avail->idx - vq->last_used_idx;
1369 rte_spinlock_unlock(&vq->access_lock);
1374 vhost_enable_notify_split(struct virtio_net *dev,
1375 struct vhost_virtqueue *vq, int enable)
1377 if (vq->used == NULL)
1380 if (!(dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))) {
1382 vq->used->flags &= ~VRING_USED_F_NO_NOTIFY;
1384 vq->used->flags |= VRING_USED_F_NO_NOTIFY;
1387 vhost_avail_event(vq) = vq->last_avail_idx;
1393 vhost_enable_notify_packed(struct virtio_net *dev,
1394 struct vhost_virtqueue *vq, int enable)
1398 if (vq->device_event == NULL)
1402 vq->device_event->flags = VRING_EVENT_F_DISABLE;
1406 flags = VRING_EVENT_F_ENABLE;
1407 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX)) {
1408 flags = VRING_EVENT_F_DESC;
1409 vq->device_event->off_wrap = vq->last_avail_idx |
1410 vq->avail_wrap_counter << 15;
1413 rte_atomic_thread_fence(__ATOMIC_RELEASE);
1415 vq->device_event->flags = flags;
1420 vhost_enable_guest_notification(struct virtio_net *dev,
1421 struct vhost_virtqueue *vq, int enable)
1424 * If the virtqueue is not ready yet, it will be applied
1425 * when it will become ready.
1430 if (vq_is_packed(dev))
1431 return vhost_enable_notify_packed(dev, vq, enable);
1433 return vhost_enable_notify_split(dev, vq, enable);
1437 rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable)
1439 struct virtio_net *dev = get_device(vid);
1440 struct vhost_virtqueue *vq;
1446 if (queue_id >= VHOST_MAX_VRING)
1449 vq = dev->virtqueue[queue_id];
1453 rte_spinlock_lock(&vq->access_lock);
1455 vq->notif_enable = enable;
1456 ret = vhost_enable_guest_notification(dev, vq, enable);
1458 rte_spinlock_unlock(&vq->access_lock);
1464 rte_vhost_log_write(int vid, uint64_t addr, uint64_t len)
1466 struct virtio_net *dev = get_device(vid);
1471 vhost_log_write(dev, addr, len);
1475 rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
1476 uint64_t offset, uint64_t len)
1478 struct virtio_net *dev;
1479 struct vhost_virtqueue *vq;
1481 dev = get_device(vid);
1485 if (vring_idx >= VHOST_MAX_VRING)
1487 vq = dev->virtqueue[vring_idx];
1491 vhost_log_used_vring(dev, vq, offset, len);
1495 rte_vhost_rx_queue_count(int vid, uint16_t qid)
1497 struct virtio_net *dev;
1498 struct vhost_virtqueue *vq;
1501 dev = get_device(vid);
1505 if (unlikely(qid >= dev->nr_vring || (qid & 1) == 0)) {
1506 VHOST_LOG_DATA(ERR, "(%s) %s: invalid virtqueue idx %d.\n",
1507 dev->ifname, __func__, qid);
1511 vq = dev->virtqueue[qid];
1515 rte_spinlock_lock(&vq->access_lock);
1517 if (unlikely(!vq->enabled || vq->avail == NULL))
1520 ret = *((volatile uint16_t *)&vq->avail->idx) - vq->last_avail_idx;
1523 rte_spinlock_unlock(&vq->access_lock);
1527 struct rte_vdpa_device *
1528 rte_vhost_get_vdpa_device(int vid)
1530 struct virtio_net *dev = get_device(vid);
1535 return dev->vdpa_dev;
1539 rte_vhost_get_log_base(int vid, uint64_t *log_base,
1542 struct virtio_net *dev = get_device(vid);
1544 if (dev == NULL || log_base == NULL || log_size == NULL)
1547 *log_base = dev->log_base;
1548 *log_size = dev->log_size;
1554 rte_vhost_get_vring_base(int vid, uint16_t queue_id,
1555 uint16_t *last_avail_idx, uint16_t *last_used_idx)
1557 struct vhost_virtqueue *vq;
1558 struct virtio_net *dev = get_device(vid);
1560 if (dev == NULL || last_avail_idx == NULL || last_used_idx == NULL)
1563 if (queue_id >= VHOST_MAX_VRING)
1566 vq = dev->virtqueue[queue_id];
1570 if (vq_is_packed(dev)) {
1571 *last_avail_idx = (vq->avail_wrap_counter << 15) |
1573 *last_used_idx = (vq->used_wrap_counter << 15) |
1576 *last_avail_idx = vq->last_avail_idx;
1577 *last_used_idx = vq->last_used_idx;
1584 rte_vhost_set_vring_base(int vid, uint16_t queue_id,
1585 uint16_t last_avail_idx, uint16_t last_used_idx)
1587 struct vhost_virtqueue *vq;
1588 struct virtio_net *dev = get_device(vid);
1593 if (queue_id >= VHOST_MAX_VRING)
1596 vq = dev->virtqueue[queue_id];
1600 if (vq_is_packed(dev)) {
1601 vq->last_avail_idx = last_avail_idx & 0x7fff;
1602 vq->avail_wrap_counter = !!(last_avail_idx & (1 << 15));
1603 vq->last_used_idx = last_used_idx & 0x7fff;
1604 vq->used_wrap_counter = !!(last_used_idx & (1 << 15));
1606 vq->last_avail_idx = last_avail_idx;
1607 vq->last_used_idx = last_used_idx;
1614 rte_vhost_get_vring_base_from_inflight(int vid,
1616 uint16_t *last_avail_idx,
1617 uint16_t *last_used_idx)
1619 struct rte_vhost_inflight_info_packed *inflight_info;
1620 struct vhost_virtqueue *vq;
1621 struct virtio_net *dev = get_device(vid);
1623 if (dev == NULL || last_avail_idx == NULL || last_used_idx == NULL)
1626 if (queue_id >= VHOST_MAX_VRING)
1629 vq = dev->virtqueue[queue_id];
1633 if (!vq_is_packed(dev))
1636 inflight_info = vq->inflight_packed;
1640 *last_avail_idx = (inflight_info->old_used_wrap_counter << 15) |
1641 inflight_info->old_used_idx;
1642 *last_used_idx = *last_avail_idx;
1648 rte_vhost_extern_callback_register(int vid,
1649 struct rte_vhost_user_extern_ops const * const ops, void *ctx)
1651 struct virtio_net *dev = get_device(vid);
1653 if (dev == NULL || ops == NULL)
1656 dev->extern_ops = *ops;
1657 dev->extern_data = ctx;
1661 static __rte_always_inline int
1662 async_channel_register(int vid, uint16_t queue_id)
1664 struct virtio_net *dev = get_device(vid);
1665 struct vhost_virtqueue *vq = dev->virtqueue[queue_id];
1666 struct vhost_async *async;
1667 int node = vq->numa_node;
1669 if (unlikely(vq->async)) {
1670 VHOST_LOG_CONFIG(ERR,
1671 "(%s) async register failed: already registered (qid: %d)\n",
1672 dev->ifname, queue_id);
1676 async = rte_zmalloc_socket(NULL, sizeof(struct vhost_async), 0, node);
1678 VHOST_LOG_CONFIG(ERR, "(%s) failed to allocate async metadata (qid: %d)\n",
1679 dev->ifname, queue_id);
1683 async->pkts_info = rte_malloc_socket(NULL, vq->size * sizeof(struct async_inflight_info),
1684 RTE_CACHE_LINE_SIZE, node);
1685 if (!async->pkts_info) {
1686 VHOST_LOG_CONFIG(ERR, "(%s) failed to allocate async_pkts_info (qid: %d)\n",
1687 dev->ifname, queue_id);
1688 goto out_free_async;
1691 async->pkts_cmpl_flag = rte_zmalloc_socket(NULL, vq->size * sizeof(bool),
1692 RTE_CACHE_LINE_SIZE, node);
1693 if (!async->pkts_cmpl_flag) {
1694 VHOST_LOG_CONFIG(ERR, "(%s) failed to allocate async pkts_cmpl_flag (qid: %d)\n",
1695 dev->ifname, queue_id);
1696 goto out_free_async;
1699 if (vq_is_packed(dev)) {
1700 async->buffers_packed = rte_malloc_socket(NULL,
1701 vq->size * sizeof(struct vring_used_elem_packed),
1702 RTE_CACHE_LINE_SIZE, node);
1703 if (!async->buffers_packed) {
1704 VHOST_LOG_CONFIG(ERR, "(%s) failed to allocate async buffers (qid: %d)\n",
1705 dev->ifname, queue_id);
1706 goto out_free_inflight;
1709 async->descs_split = rte_malloc_socket(NULL,
1710 vq->size * sizeof(struct vring_used_elem),
1711 RTE_CACHE_LINE_SIZE, node);
1712 if (!async->descs_split) {
1713 VHOST_LOG_CONFIG(ERR, "(%s) failed to allocate async descs (qid: %d)\n",
1714 dev->ifname, queue_id);
1715 goto out_free_inflight;
1723 rte_free(async->pkts_info);
1731 rte_vhost_async_channel_register(int vid, uint16_t queue_id)
1733 struct vhost_virtqueue *vq;
1734 struct virtio_net *dev = get_device(vid);
1740 if (queue_id >= VHOST_MAX_VRING)
1743 vq = dev->virtqueue[queue_id];
1745 if (unlikely(vq == NULL || !dev->async_copy))
1748 rte_spinlock_lock(&vq->access_lock);
1749 ret = async_channel_register(vid, queue_id);
1750 rte_spinlock_unlock(&vq->access_lock);
1756 rte_vhost_async_channel_register_thread_unsafe(int vid, uint16_t queue_id)
1758 struct vhost_virtqueue *vq;
1759 struct virtio_net *dev = get_device(vid);
1764 if (queue_id >= VHOST_MAX_VRING)
1767 vq = dev->virtqueue[queue_id];
1769 if (unlikely(vq == NULL || !dev->async_copy))
1772 if (unlikely(!rte_spinlock_is_locked(&vq->access_lock))) {
1773 VHOST_LOG_CONFIG(ERR, "(%s) %s() called without access lock taken.\n",
1774 dev->ifname, __func__);
1778 return async_channel_register(vid, queue_id);
1782 rte_vhost_async_channel_unregister(int vid, uint16_t queue_id)
1784 struct vhost_virtqueue *vq;
1785 struct virtio_net *dev = get_device(vid);
1791 if (queue_id >= VHOST_MAX_VRING)
1794 vq = dev->virtqueue[queue_id];
1799 if (!rte_spinlock_trylock(&vq->access_lock)) {
1800 VHOST_LOG_CONFIG(ERR, "(%s) failed to unregister async channel, virtqueue busy.\n",
1807 } else if (vq->async->pkts_inflight_n) {
1808 VHOST_LOG_CONFIG(ERR, "(%s) failed to unregister async channel.\n", dev->ifname);
1809 VHOST_LOG_CONFIG(ERR, "(%s) inflight packets must be completed before unregistration.\n",
1812 vhost_free_async_mem(vq);
1816 rte_spinlock_unlock(&vq->access_lock);
1822 rte_vhost_async_channel_unregister_thread_unsafe(int vid, uint16_t queue_id)
1824 struct vhost_virtqueue *vq;
1825 struct virtio_net *dev = get_device(vid);
1830 if (queue_id >= VHOST_MAX_VRING)
1833 vq = dev->virtqueue[queue_id];
1838 if (unlikely(!rte_spinlock_is_locked(&vq->access_lock))) {
1839 VHOST_LOG_CONFIG(ERR, "(%s) %s() called without access lock taken.\n",
1840 dev->ifname, __func__);
1847 if (vq->async->pkts_inflight_n) {
1848 VHOST_LOG_CONFIG(ERR, "(%s) failed to unregister async channel.\n", dev->ifname);
1849 VHOST_LOG_CONFIG(ERR, "(%s) inflight packets must be completed before unregistration.\n",
1854 vhost_free_async_mem(vq);
1860 rte_vhost_async_dma_configure(int16_t dma_id, uint16_t vchan_id)
1862 struct rte_dma_info info;
1863 void *pkts_cmpl_flag_addr;
1866 if (!rte_dma_is_valid(dma_id)) {
1867 VHOST_LOG_CONFIG(ERR, "DMA %d is not found.\n", dma_id);
1871 rte_dma_info_get(dma_id, &info);
1872 if (vchan_id >= info.max_vchans) {
1873 VHOST_LOG_CONFIG(ERR, "Invalid DMA %d vChannel %u.\n", dma_id, vchan_id);
1877 if (!dma_copy_track[dma_id].vchans) {
1878 struct async_dma_vchan_info *vchans;
1880 vchans = rte_zmalloc(NULL, sizeof(struct async_dma_vchan_info) * info.max_vchans,
1881 RTE_CACHE_LINE_SIZE);
1882 if (vchans == NULL) {
1883 VHOST_LOG_CONFIG(ERR, "Failed to allocate vchans for DMA %d vChannel %u.\n",
1888 dma_copy_track[dma_id].vchans = vchans;
1891 if (dma_copy_track[dma_id].vchans[vchan_id].pkts_cmpl_flag_addr) {
1892 VHOST_LOG_CONFIG(INFO, "DMA %d vChannel %u already registered.\n", dma_id,
1897 max_desc = info.max_desc;
1898 if (!rte_is_power_of_2(max_desc))
1899 max_desc = rte_align32pow2(max_desc);
1901 pkts_cmpl_flag_addr = rte_zmalloc(NULL, sizeof(bool *) * max_desc, RTE_CACHE_LINE_SIZE);
1902 if (!pkts_cmpl_flag_addr) {
1903 VHOST_LOG_CONFIG(ERR, "Failed to allocate pkts_cmpl_flag_addr for DMA %d "
1904 "vChannel %u.\n", dma_id, vchan_id);
1906 if (dma_copy_track[dma_id].nr_vchans == 0) {
1907 rte_free(dma_copy_track[dma_id].vchans);
1908 dma_copy_track[dma_id].vchans = NULL;
1913 dma_copy_track[dma_id].vchans[vchan_id].pkts_cmpl_flag_addr = pkts_cmpl_flag_addr;
1914 dma_copy_track[dma_id].vchans[vchan_id].ring_size = max_desc;
1915 dma_copy_track[dma_id].vchans[vchan_id].ring_mask = max_desc - 1;
1916 dma_copy_track[dma_id].nr_vchans++;
1922 rte_vhost_async_get_inflight(int vid, uint16_t queue_id)
1924 struct vhost_virtqueue *vq;
1925 struct virtio_net *dev = get_device(vid);
1931 if (queue_id >= VHOST_MAX_VRING)
1934 vq = dev->virtqueue[queue_id];
1939 if (!rte_spinlock_trylock(&vq->access_lock)) {
1940 VHOST_LOG_CONFIG(DEBUG,
1941 "(%s) failed to check in-flight packets. virtqueue busy.\n",
1947 ret = vq->async->pkts_inflight_n;
1949 rte_spinlock_unlock(&vq->access_lock);
1955 rte_vhost_async_get_inflight_thread_unsafe(int vid, uint16_t queue_id)
1957 struct vhost_virtqueue *vq;
1958 struct virtio_net *dev = get_device(vid);
1964 if (queue_id >= VHOST_MAX_VRING)
1967 vq = dev->virtqueue[queue_id];
1972 if (unlikely(!rte_spinlock_is_locked(&vq->access_lock))) {
1973 VHOST_LOG_CONFIG(ERR, "(%s) %s() called without access lock taken.\n",
1974 dev->ifname, __func__);
1981 ret = vq->async->pkts_inflight_n;
1987 rte_vhost_get_monitor_addr(int vid, uint16_t queue_id,
1988 struct rte_vhost_power_monitor_cond *pmc)
1990 struct virtio_net *dev = get_device(vid);
1991 struct vhost_virtqueue *vq;
1995 if (queue_id >= VHOST_MAX_VRING)
1998 vq = dev->virtqueue[queue_id];
2002 if (vq_is_packed(dev)) {
2003 struct vring_packed_desc *desc;
2004 desc = vq->desc_packed;
2005 pmc->addr = &desc[vq->last_avail_idx].flags;
2006 if (vq->avail_wrap_counter)
2007 pmc->val = VRING_DESC_F_AVAIL;
2009 pmc->val = VRING_DESC_F_USED;
2010 pmc->mask = VRING_DESC_F_AVAIL | VRING_DESC_F_USED;
2011 pmc->size = sizeof(desc[vq->last_avail_idx].flags);
2014 pmc->addr = &vq->avail->idx;
2015 pmc->val = vq->last_avail_idx & (vq->size - 1);
2016 pmc->mask = vq->size - 1;
2017 pmc->size = sizeof(vq->avail->idx);
2026 rte_vhost_vring_stats_get_names(int vid, uint16_t queue_id,
2027 struct rte_vhost_stat_name *name, unsigned int size)
2029 struct virtio_net *dev = get_device(vid);
2035 if (queue_id >= dev->nr_vring)
2038 if (!(dev->flags & VIRTIO_DEV_STATS_ENABLED))
2041 if (name == NULL || size < VHOST_NB_VQ_STATS)
2042 return VHOST_NB_VQ_STATS;
2044 for (i = 0; i < VHOST_NB_VQ_STATS; i++)
2045 snprintf(name[i].name, sizeof(name[i].name), "%s_q%u_%s",
2046 (queue_id & 1) ? "rx" : "tx",
2047 queue_id / 2, vhost_vq_stat_strings[i].name);
2049 return VHOST_NB_VQ_STATS;
2053 rte_vhost_vring_stats_get(int vid, uint16_t queue_id,
2054 struct rte_vhost_stat *stats, unsigned int n)
2056 struct virtio_net *dev = get_device(vid);
2057 struct vhost_virtqueue *vq;
2063 if (queue_id >= dev->nr_vring)
2066 if (!(dev->flags & VIRTIO_DEV_STATS_ENABLED))
2069 if (stats == NULL || n < VHOST_NB_VQ_STATS)
2070 return VHOST_NB_VQ_STATS;
2072 vq = dev->virtqueue[queue_id];
2074 rte_spinlock_lock(&vq->access_lock);
2075 for (i = 0; i < VHOST_NB_VQ_STATS; i++) {
2077 *(uint64_t *)(((char *)vq) + vhost_vq_stat_strings[i].offset);
2080 rte_spinlock_unlock(&vq->access_lock);
2082 return VHOST_NB_VQ_STATS;
2085 int rte_vhost_vring_stats_reset(int vid, uint16_t queue_id)
2087 struct virtio_net *dev = get_device(vid);
2088 struct vhost_virtqueue *vq;
2093 if (queue_id >= dev->nr_vring)
2096 if (!(dev->flags & VIRTIO_DEV_STATS_ENABLED))
2099 vq = dev->virtqueue[queue_id];
2101 rte_spinlock_lock(&vq->access_lock);
2102 memset(&vq->stats, 0, sizeof(vq->stats));
2103 rte_spinlock_unlock(&vq->access_lock);
2108 RTE_LOG_REGISTER_SUFFIX(vhost_config_log_level, config, INFO);
2109 RTE_LOG_REGISTER_SUFFIX(vhost_data_log_level, data, WARNING);