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)
376 vring_invalidate(struct virtio_net *dev, struct vhost_virtqueue *vq)
378 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
379 vhost_user_iotlb_wr_lock(vq);
386 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
387 vhost_user_iotlb_wr_unlock(vq);
391 init_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
393 struct vhost_virtqueue *vq;
395 if (vring_idx >= VHOST_MAX_VRING) {
396 RTE_LOG(ERR, VHOST_CONFIG,
397 "Failed not init vring, out of bound (%d)\n",
402 vq = dev->virtqueue[vring_idx];
404 memset(vq, 0, sizeof(struct vhost_virtqueue));
406 vq->kickfd = VIRTIO_UNINITIALIZED_EVENTFD;
407 vq->callfd = VIRTIO_UNINITIALIZED_EVENTFD;
409 vhost_user_iotlb_init(dev, vring_idx);
410 /* Backends are set to -1 indicating an inactive device. */
413 TAILQ_INIT(&vq->zmbuf_list);
417 reset_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
419 struct vhost_virtqueue *vq;
422 if (vring_idx >= VHOST_MAX_VRING) {
423 RTE_LOG(ERR, VHOST_CONFIG,
424 "Failed not init vring, out of bound (%d)\n",
429 vq = dev->virtqueue[vring_idx];
431 init_vring_queue(dev, vring_idx);
436 alloc_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
438 struct vhost_virtqueue *vq;
440 vq = rte_malloc(NULL, sizeof(struct vhost_virtqueue), 0);
442 RTE_LOG(ERR, VHOST_CONFIG,
443 "Failed to allocate memory for vring:%u.\n", vring_idx);
447 dev->virtqueue[vring_idx] = vq;
448 init_vring_queue(dev, vring_idx);
449 rte_spinlock_init(&vq->access_lock);
450 vq->avail_wrap_counter = 1;
451 vq->used_wrap_counter = 1;
452 vq->signalled_used_valid = false;
460 * Reset some variables in device structure, while keeping few
461 * others untouched, such as vid, ifname, nr_vring: they
462 * should be same unless the device is removed.
465 reset_device(struct virtio_net *dev)
470 dev->protocol_features = 0;
471 dev->flags &= VIRTIO_DEV_BUILTIN_VIRTIO_NET;
473 for (i = 0; i < dev->nr_vring; i++)
474 reset_vring_queue(dev, i);
478 * Invoked when there is a new vhost-user connection established (when
479 * there is a new virtio device being attached).
482 vhost_new_device(void)
484 struct virtio_net *dev;
487 for (i = 0; i < MAX_VHOST_DEVICE; i++) {
488 if (vhost_devices[i] == NULL)
492 if (i == MAX_VHOST_DEVICE) {
493 RTE_LOG(ERR, VHOST_CONFIG,
494 "Failed to find a free slot for new device.\n");
498 dev = rte_zmalloc(NULL, sizeof(struct virtio_net), 0);
500 RTE_LOG(ERR, VHOST_CONFIG,
501 "Failed to allocate memory for new dev.\n");
505 vhost_devices[i] = dev;
507 dev->flags = VIRTIO_DEV_BUILTIN_VIRTIO_NET;
508 dev->slave_req_fd = -1;
509 dev->vdpa_dev_id = -1;
510 dev->postcopy_ufd = -1;
511 rte_spinlock_init(&dev->slave_req_lock);
517 vhost_destroy_device_notify(struct virtio_net *dev)
519 struct rte_vdpa_device *vdpa_dev;
522 if (dev->flags & VIRTIO_DEV_RUNNING) {
523 did = dev->vdpa_dev_id;
524 vdpa_dev = rte_vdpa_get_device(did);
525 if (vdpa_dev && vdpa_dev->ops->dev_close)
526 vdpa_dev->ops->dev_close(dev->vid);
527 dev->flags &= ~VIRTIO_DEV_RUNNING;
528 dev->notify_ops->destroy_device(dev->vid);
533 * Invoked when there is the vhost-user connection is broken (when
534 * the virtio device is being detached).
537 vhost_destroy_device(int vid)
539 struct virtio_net *dev = get_device(vid);
544 vhost_destroy_device_notify(dev);
546 cleanup_device(dev, 1);
549 vhost_devices[vid] = NULL;
553 vhost_attach_vdpa_device(int vid, int did)
555 struct virtio_net *dev = get_device(vid);
560 if (rte_vdpa_get_device(did) == NULL)
563 dev->vdpa_dev_id = did;
567 vhost_set_ifname(int vid, const char *if_name, unsigned int if_len)
569 struct virtio_net *dev;
572 dev = get_device(vid);
576 len = if_len > sizeof(dev->ifname) ?
577 sizeof(dev->ifname) : if_len;
579 strncpy(dev->ifname, if_name, len);
580 dev->ifname[sizeof(dev->ifname) - 1] = '\0';
584 vhost_enable_dequeue_zero_copy(int vid)
586 struct virtio_net *dev = get_device(vid);
591 dev->dequeue_zero_copy = 1;
595 vhost_set_builtin_virtio_net(int vid, bool enable)
597 struct virtio_net *dev = get_device(vid);
603 dev->flags |= VIRTIO_DEV_BUILTIN_VIRTIO_NET;
605 dev->flags &= ~VIRTIO_DEV_BUILTIN_VIRTIO_NET;
609 rte_vhost_get_mtu(int vid, uint16_t *mtu)
611 struct virtio_net *dev = get_device(vid);
613 if (dev == NULL || mtu == NULL)
616 if (!(dev->flags & VIRTIO_DEV_READY))
619 if (!(dev->features & (1ULL << VIRTIO_NET_F_MTU)))
628 rte_vhost_get_numa_node(int vid)
630 #ifdef RTE_LIBRTE_VHOST_NUMA
631 struct virtio_net *dev = get_device(vid);
635 if (dev == NULL || numa_available() != 0)
638 ret = get_mempolicy(&numa_node, NULL, 0, dev,
639 MPOL_F_NODE | MPOL_F_ADDR);
641 RTE_LOG(ERR, VHOST_CONFIG,
642 "(%d) failed to query numa node: %s\n",
643 vid, rte_strerror(errno));
655 rte_vhost_get_queue_num(int vid)
657 struct virtio_net *dev = get_device(vid);
662 return dev->nr_vring / 2;
666 rte_vhost_get_vring_num(int vid)
668 struct virtio_net *dev = get_device(vid);
673 return dev->nr_vring;
677 rte_vhost_get_ifname(int vid, char *buf, size_t len)
679 struct virtio_net *dev = get_device(vid);
681 if (dev == NULL || buf == NULL)
684 len = RTE_MIN(len, sizeof(dev->ifname));
686 strncpy(buf, dev->ifname, len);
693 rte_vhost_get_negotiated_features(int vid, uint64_t *features)
695 struct virtio_net *dev;
697 dev = get_device(vid);
698 if (dev == NULL || features == NULL)
701 *features = dev->features;
706 rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem)
708 struct virtio_net *dev;
709 struct rte_vhost_memory *m;
712 dev = get_device(vid);
713 if (dev == NULL || mem == NULL)
716 size = dev->mem->nregions * sizeof(struct rte_vhost_mem_region);
717 m = malloc(sizeof(struct rte_vhost_memory) + size);
721 m->nregions = dev->mem->nregions;
722 memcpy(m->regions, dev->mem->regions, size);
729 rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
730 struct rte_vhost_vring *vring)
732 struct virtio_net *dev;
733 struct vhost_virtqueue *vq;
735 dev = get_device(vid);
736 if (dev == NULL || vring == NULL)
739 if (vring_idx >= VHOST_MAX_VRING)
742 vq = dev->virtqueue[vring_idx];
746 vring->desc = vq->desc;
747 vring->avail = vq->avail;
748 vring->used = vq->used;
749 vring->log_guest_addr = vq->log_guest_addr;
751 vring->callfd = vq->callfd;
752 vring->kickfd = vq->kickfd;
753 vring->size = vq->size;
759 rte_vhost_vring_call(int vid, uint16_t vring_idx)
761 struct virtio_net *dev;
762 struct vhost_virtqueue *vq;
764 dev = get_device(vid);
768 if (vring_idx >= VHOST_MAX_VRING)
771 vq = dev->virtqueue[vring_idx];
775 if (vq_is_packed(dev))
776 vhost_vring_call_packed(dev, vq);
778 vhost_vring_call_split(dev, vq);
784 rte_vhost_avail_entries(int vid, uint16_t queue_id)
786 struct virtio_net *dev;
787 struct vhost_virtqueue *vq;
790 dev = get_device(vid);
794 vq = dev->virtqueue[queue_id];
796 rte_spinlock_lock(&vq->access_lock);
798 if (unlikely(!vq->enabled || vq->avail == NULL))
801 ret = *(volatile uint16_t *)&vq->avail->idx - vq->last_used_idx;
804 rte_spinlock_unlock(&vq->access_lock);
809 vhost_enable_notify_split(struct virtio_net *dev,
810 struct vhost_virtqueue *vq, int enable)
812 if (vq->used == NULL)
815 if (!(dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))) {
817 vq->used->flags &= ~VRING_USED_F_NO_NOTIFY;
819 vq->used->flags |= VRING_USED_F_NO_NOTIFY;
822 vhost_avail_event(vq) = vq->last_avail_idx;
828 vhost_enable_notify_packed(struct virtio_net *dev,
829 struct vhost_virtqueue *vq, int enable)
833 if (vq->device_event == NULL)
837 vq->device_event->flags = VRING_EVENT_F_DISABLE;
841 flags = VRING_EVENT_F_ENABLE;
842 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX)) {
843 flags = VRING_EVENT_F_DESC;
844 vq->device_event->off_wrap = vq->last_avail_idx |
845 vq->avail_wrap_counter << 15;
850 vq->device_event->flags = flags;
855 rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable)
857 struct virtio_net *dev = get_device(vid);
858 struct vhost_virtqueue *vq;
864 vq = dev->virtqueue[queue_id];
866 rte_spinlock_lock(&vq->access_lock);
868 if (vq_is_packed(dev))
869 ret = vhost_enable_notify_packed(dev, vq, enable);
871 ret = vhost_enable_notify_split(dev, vq, enable);
873 rte_spinlock_unlock(&vq->access_lock);
879 rte_vhost_log_write(int vid, uint64_t addr, uint64_t len)
881 struct virtio_net *dev = get_device(vid);
886 vhost_log_write(dev, addr, len);
890 rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
891 uint64_t offset, uint64_t len)
893 struct virtio_net *dev;
894 struct vhost_virtqueue *vq;
896 dev = get_device(vid);
900 if (vring_idx >= VHOST_MAX_VRING)
902 vq = dev->virtqueue[vring_idx];
906 vhost_log_used_vring(dev, vq, offset, len);
910 rte_vhost_rx_queue_count(int vid, uint16_t qid)
912 struct virtio_net *dev;
913 struct vhost_virtqueue *vq;
916 dev = get_device(vid);
920 if (unlikely(qid >= dev->nr_vring || (qid & 1) == 0)) {
921 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
922 dev->vid, __func__, qid);
926 vq = dev->virtqueue[qid];
930 rte_spinlock_lock(&vq->access_lock);
932 if (unlikely(vq->enabled == 0 || vq->avail == NULL))
935 ret = *((volatile uint16_t *)&vq->avail->idx) - vq->last_avail_idx;
938 rte_spinlock_unlock(&vq->access_lock);
942 int rte_vhost_get_vdpa_device_id(int vid)
944 struct virtio_net *dev = get_device(vid);
949 return dev->vdpa_dev_id;
952 int rte_vhost_get_log_base(int vid, uint64_t *log_base,
955 struct virtio_net *dev = get_device(vid);
957 if (dev == NULL || log_base == NULL || log_size == NULL)
960 *log_base = dev->log_base;
961 *log_size = dev->log_size;
966 int rte_vhost_get_vring_base(int vid, uint16_t queue_id,
967 uint16_t *last_avail_idx, uint16_t *last_used_idx)
969 struct virtio_net *dev = get_device(vid);
971 if (dev == NULL || last_avail_idx == NULL || last_used_idx == NULL)
974 *last_avail_idx = dev->virtqueue[queue_id]->last_avail_idx;
975 *last_used_idx = dev->virtqueue[queue_id]->last_used_idx;
980 int rte_vhost_set_vring_base(int vid, uint16_t queue_id,
981 uint16_t last_avail_idx, uint16_t last_used_idx)
983 struct virtio_net *dev = get_device(vid);
988 dev->virtqueue[queue_id]->last_avail_idx = last_avail_idx;
989 dev->virtqueue[queue_id]->last_used_idx = last_used_idx;
994 int rte_vhost_extern_callback_register(int vid,
995 struct rte_vhost_user_extern_ops const * const ops, void *ctx)
997 struct virtio_net *dev = get_device(vid);
999 if (dev == NULL || ops == NULL)
1002 dev->extern_ops = *ops;
1003 dev->extern_data = ctx;