1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2017 Intel Corporation
5 #include <linux/vhost.h>
6 #include <linux/virtio_net.h>
10 #ifdef RTE_LIBRTE_VHOST_NUMA
15 #include <rte_errno.h>
16 #include <rte_ethdev.h>
18 #include <rte_string_fns.h>
19 #include <rte_memory.h>
20 #include <rte_malloc.h>
21 #include <rte_vhost.h>
22 #include <rte_rwlock.h>
26 #include "vhost_user.h"
28 struct virtio_net *vhost_devices[MAX_VHOST_DEVICE];
30 /* Called with iotlb_lock read-locked */
32 __vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
33 uint64_t iova, uint64_t *size, uint8_t perm)
35 uint64_t vva, tmp_size;
42 vva = vhost_user_iotlb_cache_find(vq, iova, &tmp_size, perm);
43 if (tmp_size == *size)
48 if (!vhost_user_iotlb_pending_miss(vq, iova, perm)) {
50 * iotlb_lock is read-locked for a full burst,
51 * but it only protects the iotlb cache.
52 * In case of IOTLB miss, we might block on the socket,
53 * which could cause a deadlock with QEMU if an IOTLB update
54 * is being handled. We can safely unlock here to avoid it.
56 vhost_user_iotlb_rd_unlock(vq);
58 vhost_user_iotlb_pending_insert(vq, iova, perm);
59 if (vhost_user_iotlb_miss(dev, iova, perm)) {
60 RTE_LOG(ERR, VHOST_CONFIG,
61 "IOTLB miss req failed for IOVA 0x%" PRIx64 "\n",
63 vhost_user_iotlb_pending_remove(vq, iova, 1, perm);
66 vhost_user_iotlb_rd_lock(vq);
72 #define VHOST_LOG_PAGE 4096
75 * Atomically set a bit in memory.
77 static __rte_always_inline void
78 vhost_set_bit(unsigned int nr, volatile uint8_t *addr)
80 #if defined(RTE_TOOLCHAIN_GCC) && (GCC_VERSION < 70100)
82 * __sync_ built-ins are deprecated, but __atomic_ ones
83 * are sub-optimized in older GCC versions.
85 __sync_fetch_and_or_1(addr, (1U << nr));
87 __atomic_fetch_or(addr, (1U << nr), __ATOMIC_RELAXED);
91 static __rte_always_inline void
92 vhost_log_page(uint8_t *log_base, uint64_t page)
94 vhost_set_bit(page % 8, &log_base[page / 8]);
98 __vhost_log_write(struct virtio_net *dev, uint64_t addr, uint64_t len)
102 if (unlikely(!dev->log_base || !len))
105 if (unlikely(dev->log_size <= ((addr + len - 1) / VHOST_LOG_PAGE / 8)))
108 /* To make sure guest memory updates are committed before logging */
111 page = addr / VHOST_LOG_PAGE;
112 while (page * VHOST_LOG_PAGE < addr + len) {
113 vhost_log_page((uint8_t *)(uintptr_t)dev->log_base, page);
119 __vhost_log_cache_sync(struct virtio_net *dev, struct vhost_virtqueue *vq)
121 unsigned long *log_base;
124 if (unlikely(!dev->log_base))
129 log_base = (unsigned long *)(uintptr_t)dev->log_base;
131 for (i = 0; i < vq->log_cache_nb_elem; i++) {
132 struct log_cache_entry *elem = vq->log_cache + i;
134 #if defined(RTE_TOOLCHAIN_GCC) && (GCC_VERSION < 70100)
136 * '__sync' builtins are deprecated, but '__atomic' ones
137 * are sub-optimized in older GCC versions.
139 __sync_fetch_and_or(log_base + elem->offset, elem->val);
141 __atomic_fetch_or(log_base + elem->offset, elem->val,
148 vq->log_cache_nb_elem = 0;
151 static __rte_always_inline void
152 vhost_log_cache_page(struct virtio_net *dev, struct vhost_virtqueue *vq,
155 uint32_t bit_nr = page % (sizeof(unsigned long) << 3);
156 uint32_t offset = page / (sizeof(unsigned long) << 3);
159 for (i = 0; i < vq->log_cache_nb_elem; i++) {
160 struct log_cache_entry *elem = vq->log_cache + i;
162 if (elem->offset == offset) {
163 elem->val |= (1UL << bit_nr);
168 if (unlikely(i >= VHOST_LOG_CACHE_NR)) {
170 * No more room for a new log cache entry,
171 * so write the dirty log map directly.
174 vhost_log_page((uint8_t *)(uintptr_t)dev->log_base, page);
179 vq->log_cache[i].offset = offset;
180 vq->log_cache[i].val = (1UL << bit_nr);
181 vq->log_cache_nb_elem++;
185 __vhost_log_cache_write(struct virtio_net *dev, struct vhost_virtqueue *vq,
186 uint64_t addr, uint64_t len)
190 if (unlikely(!dev->log_base || !len))
193 if (unlikely(dev->log_size <= ((addr + len - 1) / VHOST_LOG_PAGE / 8)))
196 page = addr / VHOST_LOG_PAGE;
197 while (page * VHOST_LOG_PAGE < addr + len) {
198 vhost_log_cache_page(dev, vq, page);
204 vhost_alloc_copy_ind_table(struct virtio_net *dev, struct vhost_virtqueue *vq,
205 uint64_t desc_addr, uint64_t desc_len)
209 uint64_t len, remain = desc_len;
211 idesc = rte_malloc(__func__, desc_len, 0);
212 if (unlikely(!idesc))
215 dst = (uint64_t)(uintptr_t)idesc;
219 src = vhost_iova_to_vva(dev, vq, desc_addr, &len,
221 if (unlikely(!src || !len)) {
226 rte_memcpy((void *)(uintptr_t)dst, (void *)(uintptr_t)src, len);
237 cleanup_vq(struct vhost_virtqueue *vq, int destroy)
239 if ((vq->callfd >= 0) && (destroy != 0))
246 * Unmap any memory, close any file descriptors and
247 * free any memory owned by a device.
250 cleanup_device(struct virtio_net *dev, int destroy)
254 vhost_backend_cleanup(dev);
256 for (i = 0; i < dev->nr_vring; i++)
257 cleanup_vq(dev->virtqueue[i], destroy);
261 free_vq(struct virtio_net *dev, struct vhost_virtqueue *vq)
263 if (vq_is_packed(dev))
264 rte_free(vq->shadow_used_packed);
266 rte_free(vq->shadow_used_split);
267 rte_free(vq->batch_copy_elems);
268 rte_mempool_free(vq->iotlb_pool);
273 * Release virtqueues and device memory.
276 free_device(struct virtio_net *dev)
280 for (i = 0; i < dev->nr_vring; i++)
281 free_vq(dev, dev->virtqueue[i]);
287 vring_translate_split(struct virtio_net *dev, struct vhost_virtqueue *vq)
289 uint64_t req_size, size;
291 req_size = sizeof(struct vring_desc) * vq->size;
293 vq->desc = (struct vring_desc *)(uintptr_t)vhost_iova_to_vva(dev, vq,
294 vq->ring_addrs.desc_user_addr,
295 &size, VHOST_ACCESS_RW);
296 if (!vq->desc || size != req_size)
299 req_size = sizeof(struct vring_avail);
300 req_size += sizeof(uint16_t) * vq->size;
301 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))
302 req_size += sizeof(uint16_t);
304 vq->avail = (struct vring_avail *)(uintptr_t)vhost_iova_to_vva(dev, vq,
305 vq->ring_addrs.avail_user_addr,
306 &size, VHOST_ACCESS_RW);
307 if (!vq->avail || size != req_size)
310 req_size = sizeof(struct vring_used);
311 req_size += sizeof(struct vring_used_elem) * vq->size;
312 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))
313 req_size += sizeof(uint16_t);
315 vq->used = (struct vring_used *)(uintptr_t)vhost_iova_to_vva(dev, vq,
316 vq->ring_addrs.used_user_addr,
317 &size, VHOST_ACCESS_RW);
318 if (!vq->used || size != req_size)
325 vring_translate_packed(struct virtio_net *dev, struct vhost_virtqueue *vq)
327 uint64_t req_size, size;
329 req_size = sizeof(struct vring_packed_desc) * vq->size;
331 vq->desc_packed = (struct vring_packed_desc *)(uintptr_t)
332 vhost_iova_to_vva(dev, vq, vq->ring_addrs.desc_user_addr,
333 &size, VHOST_ACCESS_RW);
334 if (!vq->desc_packed || size != req_size)
337 req_size = sizeof(struct vring_packed_desc_event);
339 vq->driver_event = (struct vring_packed_desc_event *)(uintptr_t)
340 vhost_iova_to_vva(dev, vq, vq->ring_addrs.avail_user_addr,
341 &size, VHOST_ACCESS_RW);
342 if (!vq->driver_event || size != req_size)
345 req_size = sizeof(struct vring_packed_desc_event);
347 vq->device_event = (struct vring_packed_desc_event *)(uintptr_t)
348 vhost_iova_to_vva(dev, vq, vq->ring_addrs.used_user_addr,
349 &size, VHOST_ACCESS_RW);
350 if (!vq->device_event || size != req_size)
357 vring_translate(struct virtio_net *dev, struct vhost_virtqueue *vq)
360 if (!(dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)))
363 if (vq_is_packed(dev)) {
364 if (vring_translate_packed(dev, vq) < 0)
367 if (vring_translate_split(dev, vq) < 0)
377 vring_invalidate(struct virtio_net *dev, struct vhost_virtqueue *vq)
379 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
380 vhost_user_iotlb_wr_lock(vq);
387 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
388 vhost_user_iotlb_wr_unlock(vq);
392 init_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
394 struct vhost_virtqueue *vq;
396 if (vring_idx >= VHOST_MAX_VRING) {
397 RTE_LOG(ERR, VHOST_CONFIG,
398 "Failed not init vring, out of bound (%d)\n",
403 vq = dev->virtqueue[vring_idx];
405 memset(vq, 0, sizeof(struct vhost_virtqueue));
407 vq->kickfd = VIRTIO_UNINITIALIZED_EVENTFD;
408 vq->callfd = VIRTIO_UNINITIALIZED_EVENTFD;
410 vhost_user_iotlb_init(dev, vring_idx);
411 /* Backends are set to -1 indicating an inactive device. */
414 TAILQ_INIT(&vq->zmbuf_list);
418 reset_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
420 struct vhost_virtqueue *vq;
423 if (vring_idx >= VHOST_MAX_VRING) {
424 RTE_LOG(ERR, VHOST_CONFIG,
425 "Failed not init vring, out of bound (%d)\n",
430 vq = dev->virtqueue[vring_idx];
432 init_vring_queue(dev, vring_idx);
437 alloc_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
439 struct vhost_virtqueue *vq;
441 vq = rte_malloc(NULL, sizeof(struct vhost_virtqueue), 0);
443 RTE_LOG(ERR, VHOST_CONFIG,
444 "Failed to allocate memory for vring:%u.\n", vring_idx);
448 dev->virtqueue[vring_idx] = vq;
449 init_vring_queue(dev, vring_idx);
450 rte_spinlock_init(&vq->access_lock);
451 vq->avail_wrap_counter = 1;
452 vq->used_wrap_counter = 1;
453 vq->signalled_used_valid = false;
461 * Reset some variables in device structure, while keeping few
462 * others untouched, such as vid, ifname, nr_vring: they
463 * should be same unless the device is removed.
466 reset_device(struct virtio_net *dev)
471 dev->protocol_features = 0;
472 dev->flags &= VIRTIO_DEV_BUILTIN_VIRTIO_NET;
474 for (i = 0; i < dev->nr_vring; i++)
475 reset_vring_queue(dev, i);
479 * Invoked when there is a new vhost-user connection established (when
480 * there is a new virtio device being attached).
483 vhost_new_device(void)
485 struct virtio_net *dev;
488 for (i = 0; i < MAX_VHOST_DEVICE; i++) {
489 if (vhost_devices[i] == NULL)
493 if (i == MAX_VHOST_DEVICE) {
494 RTE_LOG(ERR, VHOST_CONFIG,
495 "Failed to find a free slot for new device.\n");
499 dev = rte_zmalloc(NULL, sizeof(struct virtio_net), 0);
501 RTE_LOG(ERR, VHOST_CONFIG,
502 "Failed to allocate memory for new dev.\n");
506 vhost_devices[i] = dev;
508 dev->flags = VIRTIO_DEV_BUILTIN_VIRTIO_NET;
509 dev->slave_req_fd = -1;
510 dev->vdpa_dev_id = -1;
511 dev->postcopy_ufd = -1;
512 rte_spinlock_init(&dev->slave_req_lock);
518 vhost_destroy_device_notify(struct virtio_net *dev)
520 struct rte_vdpa_device *vdpa_dev;
523 if (dev->flags & VIRTIO_DEV_RUNNING) {
524 did = dev->vdpa_dev_id;
525 vdpa_dev = rte_vdpa_get_device(did);
526 if (vdpa_dev && vdpa_dev->ops->dev_close)
527 vdpa_dev->ops->dev_close(dev->vid);
528 dev->flags &= ~VIRTIO_DEV_RUNNING;
529 dev->notify_ops->destroy_device(dev->vid);
534 * Invoked when there is the vhost-user connection is broken (when
535 * the virtio device is being detached).
538 vhost_destroy_device(int vid)
540 struct virtio_net *dev = get_device(vid);
545 vhost_destroy_device_notify(dev);
547 cleanup_device(dev, 1);
550 vhost_devices[vid] = NULL;
554 vhost_attach_vdpa_device(int vid, int did)
556 struct virtio_net *dev = get_device(vid);
561 if (rte_vdpa_get_device(did) == NULL)
564 dev->vdpa_dev_id = did;
568 vhost_set_ifname(int vid, const char *if_name, unsigned int if_len)
570 struct virtio_net *dev;
573 dev = get_device(vid);
577 len = if_len > sizeof(dev->ifname) ?
578 sizeof(dev->ifname) : if_len;
580 strncpy(dev->ifname, if_name, len);
581 dev->ifname[sizeof(dev->ifname) - 1] = '\0';
585 vhost_enable_dequeue_zero_copy(int vid)
587 struct virtio_net *dev = get_device(vid);
592 dev->dequeue_zero_copy = 1;
596 vhost_set_builtin_virtio_net(int vid, bool enable)
598 struct virtio_net *dev = get_device(vid);
604 dev->flags |= VIRTIO_DEV_BUILTIN_VIRTIO_NET;
606 dev->flags &= ~VIRTIO_DEV_BUILTIN_VIRTIO_NET;
610 rte_vhost_get_mtu(int vid, uint16_t *mtu)
612 struct virtio_net *dev = get_device(vid);
614 if (dev == NULL || mtu == NULL)
617 if (!(dev->flags & VIRTIO_DEV_READY))
620 if (!(dev->features & (1ULL << VIRTIO_NET_F_MTU)))
629 rte_vhost_get_numa_node(int vid)
631 #ifdef RTE_LIBRTE_VHOST_NUMA
632 struct virtio_net *dev = get_device(vid);
636 if (dev == NULL || numa_available() != 0)
639 ret = get_mempolicy(&numa_node, NULL, 0, dev,
640 MPOL_F_NODE | MPOL_F_ADDR);
642 RTE_LOG(ERR, VHOST_CONFIG,
643 "(%d) failed to query numa node: %s\n",
644 vid, rte_strerror(errno));
656 rte_vhost_get_queue_num(int vid)
658 struct virtio_net *dev = get_device(vid);
663 return dev->nr_vring / 2;
667 rte_vhost_get_vring_num(int vid)
669 struct virtio_net *dev = get_device(vid);
674 return dev->nr_vring;
678 rte_vhost_get_ifname(int vid, char *buf, size_t len)
680 struct virtio_net *dev = get_device(vid);
682 if (dev == NULL || buf == NULL)
685 len = RTE_MIN(len, sizeof(dev->ifname));
687 strncpy(buf, dev->ifname, len);
694 rte_vhost_get_negotiated_features(int vid, uint64_t *features)
696 struct virtio_net *dev;
698 dev = get_device(vid);
699 if (dev == NULL || features == NULL)
702 *features = dev->features;
707 rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem)
709 struct virtio_net *dev;
710 struct rte_vhost_memory *m;
713 dev = get_device(vid);
714 if (dev == NULL || mem == NULL)
717 size = dev->mem->nregions * sizeof(struct rte_vhost_mem_region);
718 m = malloc(sizeof(struct rte_vhost_memory) + size);
722 m->nregions = dev->mem->nregions;
723 memcpy(m->regions, dev->mem->regions, size);
730 rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
731 struct rte_vhost_vring *vring)
733 struct virtio_net *dev;
734 struct vhost_virtqueue *vq;
736 dev = get_device(vid);
737 if (dev == NULL || vring == NULL)
740 if (vring_idx >= VHOST_MAX_VRING)
743 vq = dev->virtqueue[vring_idx];
747 vring->desc = vq->desc;
748 vring->avail = vq->avail;
749 vring->used = vq->used;
750 vring->log_guest_addr = vq->log_guest_addr;
752 vring->callfd = vq->callfd;
753 vring->kickfd = vq->kickfd;
754 vring->size = vq->size;
760 rte_vhost_vring_call(int vid, uint16_t vring_idx)
762 struct virtio_net *dev;
763 struct vhost_virtqueue *vq;
765 dev = get_device(vid);
769 if (vring_idx >= VHOST_MAX_VRING)
772 vq = dev->virtqueue[vring_idx];
776 if (vq_is_packed(dev))
777 vhost_vring_call_packed(dev, vq);
779 vhost_vring_call_split(dev, vq);
785 rte_vhost_avail_entries(int vid, uint16_t queue_id)
787 struct virtio_net *dev;
788 struct vhost_virtqueue *vq;
790 dev = get_device(vid);
794 vq = dev->virtqueue[queue_id];
798 return *(volatile uint16_t *)&vq->avail->idx - vq->last_used_idx;
802 vhost_enable_notify_split(struct virtio_net *dev,
803 struct vhost_virtqueue *vq, int enable)
805 if (!(dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))) {
807 vq->used->flags &= ~VRING_USED_F_NO_NOTIFY;
809 vq->used->flags |= VRING_USED_F_NO_NOTIFY;
812 vhost_avail_event(vq) = vq->last_avail_idx;
817 vhost_enable_notify_packed(struct virtio_net *dev,
818 struct vhost_virtqueue *vq, int enable)
823 vq->device_event->flags = VRING_EVENT_F_DISABLE;
827 flags = VRING_EVENT_F_ENABLE;
828 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX)) {
829 flags = VRING_EVENT_F_DESC;
830 vq->device_event->off_wrap = vq->last_avail_idx |
831 vq->avail_wrap_counter << 15;
836 vq->device_event->flags = flags;
840 rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable)
842 struct virtio_net *dev = get_device(vid);
843 struct vhost_virtqueue *vq;
848 vq = dev->virtqueue[queue_id];
850 if (vq_is_packed(dev))
851 vhost_enable_notify_packed(dev, vq, enable);
853 vhost_enable_notify_split(dev, vq, enable);
859 rte_vhost_log_write(int vid, uint64_t addr, uint64_t len)
861 struct virtio_net *dev = get_device(vid);
866 vhost_log_write(dev, addr, len);
870 rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
871 uint64_t offset, uint64_t len)
873 struct virtio_net *dev;
874 struct vhost_virtqueue *vq;
876 dev = get_device(vid);
880 if (vring_idx >= VHOST_MAX_VRING)
882 vq = dev->virtqueue[vring_idx];
886 vhost_log_used_vring(dev, vq, offset, len);
890 rte_vhost_rx_queue_count(int vid, uint16_t qid)
892 struct virtio_net *dev;
893 struct vhost_virtqueue *vq;
895 dev = get_device(vid);
899 if (unlikely(qid >= dev->nr_vring || (qid & 1) == 0)) {
900 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
901 dev->vid, __func__, qid);
905 vq = dev->virtqueue[qid];
909 if (unlikely(vq->enabled == 0 || vq->avail == NULL))
912 return *((volatile uint16_t *)&vq->avail->idx) - vq->last_avail_idx;
915 int rte_vhost_get_vdpa_device_id(int vid)
917 struct virtio_net *dev = get_device(vid);
922 return dev->vdpa_dev_id;
925 int rte_vhost_get_log_base(int vid, uint64_t *log_base,
928 struct virtio_net *dev = get_device(vid);
930 if (dev == NULL || log_base == NULL || log_size == NULL)
933 *log_base = dev->log_base;
934 *log_size = dev->log_size;
939 int rte_vhost_get_vring_base(int vid, uint16_t queue_id,
940 uint16_t *last_avail_idx, uint16_t *last_used_idx)
942 struct virtio_net *dev = get_device(vid);
944 if (dev == NULL || last_avail_idx == NULL || last_used_idx == NULL)
947 *last_avail_idx = dev->virtqueue[queue_id]->last_avail_idx;
948 *last_used_idx = dev->virtqueue[queue_id]->last_used_idx;
953 int rte_vhost_set_vring_base(int vid, uint16_t queue_id,
954 uint16_t last_avail_idx, uint16_t last_used_idx)
956 struct virtio_net *dev = get_device(vid);
961 dev->virtqueue[queue_id]->last_avail_idx = last_avail_idx;
962 dev->virtqueue[queue_id]->last_used_idx = last_used_idx;
967 int rte_vhost_extern_callback_register(int vid,
968 struct rte_vhost_user_extern_ops const * const ops, void *ctx)
970 struct virtio_net *dev = get_device(vid);
972 if (dev == NULL || ops == NULL)
975 dev->extern_ops = *ops;
976 dev->extern_data = ctx;