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
14 #include <rte_errno.h>
15 #include <rte_ethdev.h>
17 #include <rte_string_fns.h>
18 #include <rte_memory.h>
19 #include <rte_malloc.h>
20 #include <rte_vhost.h>
21 #include <rte_rwlock.h>
25 #include "vhost_user.h"
27 struct virtio_net *vhost_devices[MAX_VHOST_DEVICE];
29 /* Called with iotlb_lock read-locked */
31 __vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
32 uint64_t iova, uint64_t *size, uint8_t perm)
34 uint64_t vva, tmp_size;
41 vva = vhost_user_iotlb_cache_find(vq, iova, &tmp_size, perm);
42 if (tmp_size == *size)
47 if (!vhost_user_iotlb_pending_miss(vq, iova, perm)) {
49 * iotlb_lock is read-locked for a full burst,
50 * but it only protects the iotlb cache.
51 * In case of IOTLB miss, we might block on the socket,
52 * which could cause a deadlock with QEMU if an IOTLB update
53 * is being handled. We can safely unlock here to avoid it.
55 vhost_user_iotlb_rd_unlock(vq);
57 vhost_user_iotlb_pending_insert(vq, iova, perm);
58 if (vhost_user_iotlb_miss(dev, iova, perm)) {
59 RTE_LOG(ERR, VHOST_CONFIG,
60 "IOTLB miss req failed for IOVA 0x%" PRIx64 "\n",
62 vhost_user_iotlb_pending_remove(vq, iova, 1, perm);
65 vhost_user_iotlb_rd_lock(vq);
72 cleanup_vq(struct vhost_virtqueue *vq, int destroy)
74 if ((vq->callfd >= 0) && (destroy != 0))
81 * Unmap any memory, close any file descriptors and
82 * free any memory owned by a device.
85 cleanup_device(struct virtio_net *dev, int destroy)
89 vhost_backend_cleanup(dev);
91 for (i = 0; i < dev->nr_vring; i++)
92 cleanup_vq(dev->virtqueue[i], destroy);
96 free_vq(struct virtio_net *dev, struct vhost_virtqueue *vq)
98 if (vq_is_packed(dev))
99 rte_free(vq->shadow_used_packed);
101 rte_free(vq->shadow_used_split);
102 rte_free(vq->batch_copy_elems);
103 rte_mempool_free(vq->iotlb_pool);
108 * Release virtqueues and device memory.
111 free_device(struct virtio_net *dev)
115 for (i = 0; i < dev->nr_vring; i++)
116 free_vq(dev, dev->virtqueue[i]);
122 vring_translate_split(struct virtio_net *dev, struct vhost_virtqueue *vq)
124 uint64_t req_size, size;
126 req_size = sizeof(struct vring_desc) * vq->size;
128 vq->desc = (struct vring_desc *)(uintptr_t)vhost_iova_to_vva(dev, vq,
129 vq->ring_addrs.desc_user_addr,
130 &size, VHOST_ACCESS_RW);
131 if (!vq->desc || size != req_size)
134 req_size = sizeof(struct vring_avail);
135 req_size += sizeof(uint16_t) * vq->size;
136 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))
137 req_size += sizeof(uint16_t);
139 vq->avail = (struct vring_avail *)(uintptr_t)vhost_iova_to_vva(dev, vq,
140 vq->ring_addrs.avail_user_addr,
141 &size, VHOST_ACCESS_RW);
142 if (!vq->avail || size != req_size)
145 req_size = sizeof(struct vring_used);
146 req_size += sizeof(struct vring_used_elem) * vq->size;
147 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))
148 req_size += sizeof(uint16_t);
150 vq->used = (struct vring_used *)(uintptr_t)vhost_iova_to_vva(dev, vq,
151 vq->ring_addrs.used_user_addr,
152 &size, VHOST_ACCESS_RW);
153 if (!vq->used || size != req_size)
160 vring_translate_packed(struct virtio_net *dev, struct vhost_virtqueue *vq)
162 uint64_t req_size, size;
164 req_size = sizeof(struct vring_packed_desc) * vq->size;
166 vq->desc_packed = (struct vring_packed_desc *)(uintptr_t)
167 vhost_iova_to_vva(dev, vq, vq->ring_addrs.desc_user_addr,
168 &size, VHOST_ACCESS_RW);
169 if (!vq->desc_packed || size != req_size)
172 req_size = sizeof(struct vring_packed_desc_event);
174 vq->driver_event = (struct vring_packed_desc_event *)(uintptr_t)
175 vhost_iova_to_vva(dev, vq, vq->ring_addrs.avail_user_addr,
176 &size, VHOST_ACCESS_RW);
177 if (!vq->driver_event || size != req_size)
180 req_size = sizeof(struct vring_packed_desc_event);
182 vq->device_event = (struct vring_packed_desc_event *)(uintptr_t)
183 vhost_iova_to_vva(dev, vq, vq->ring_addrs.used_user_addr,
184 &size, VHOST_ACCESS_RW);
185 if (!vq->device_event || size != req_size)
192 vring_translate(struct virtio_net *dev, struct vhost_virtqueue *vq)
195 if (!(dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)))
198 if (vq_is_packed(dev)) {
199 if (vring_translate_packed(dev, vq) < 0)
202 if (vring_translate_split(dev, vq) < 0)
212 vring_invalidate(struct virtio_net *dev, struct vhost_virtqueue *vq)
214 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
215 vhost_user_iotlb_wr_lock(vq);
222 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
223 vhost_user_iotlb_wr_unlock(vq);
227 init_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
229 struct vhost_virtqueue *vq;
231 if (vring_idx >= VHOST_MAX_VRING) {
232 RTE_LOG(ERR, VHOST_CONFIG,
233 "Failed not init vring, out of bound (%d)\n",
238 vq = dev->virtqueue[vring_idx];
240 memset(vq, 0, sizeof(struct vhost_virtqueue));
242 vq->kickfd = VIRTIO_UNINITIALIZED_EVENTFD;
243 vq->callfd = VIRTIO_UNINITIALIZED_EVENTFD;
245 vhost_user_iotlb_init(dev, vring_idx);
246 /* Backends are set to -1 indicating an inactive device. */
249 TAILQ_INIT(&vq->zmbuf_list);
253 reset_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
255 struct vhost_virtqueue *vq;
258 if (vring_idx >= VHOST_MAX_VRING) {
259 RTE_LOG(ERR, VHOST_CONFIG,
260 "Failed not init vring, out of bound (%d)\n",
265 vq = dev->virtqueue[vring_idx];
267 init_vring_queue(dev, vring_idx);
272 alloc_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
274 struct vhost_virtqueue *vq;
276 vq = rte_malloc(NULL, sizeof(struct vhost_virtqueue), 0);
278 RTE_LOG(ERR, VHOST_CONFIG,
279 "Failed to allocate memory for vring:%u.\n", vring_idx);
283 dev->virtqueue[vring_idx] = vq;
284 init_vring_queue(dev, vring_idx);
285 rte_spinlock_init(&vq->access_lock);
286 vq->avail_wrap_counter = 1;
287 vq->used_wrap_counter = 1;
288 vq->signalled_used_valid = false;
296 * Reset some variables in device structure, while keeping few
297 * others untouched, such as vid, ifname, nr_vring: they
298 * should be same unless the device is removed.
301 reset_device(struct virtio_net *dev)
306 dev->protocol_features = 0;
307 dev->flags &= VIRTIO_DEV_BUILTIN_VIRTIO_NET;
309 for (i = 0; i < dev->nr_vring; i++)
310 reset_vring_queue(dev, i);
314 * Invoked when there is a new vhost-user connection established (when
315 * there is a new virtio device being attached).
318 vhost_new_device(void)
320 struct virtio_net *dev;
323 for (i = 0; i < MAX_VHOST_DEVICE; i++) {
324 if (vhost_devices[i] == NULL)
328 if (i == MAX_VHOST_DEVICE) {
329 RTE_LOG(ERR, VHOST_CONFIG,
330 "Failed to find a free slot for new device.\n");
334 dev = rte_zmalloc(NULL, sizeof(struct virtio_net), 0);
336 RTE_LOG(ERR, VHOST_CONFIG,
337 "Failed to allocate memory for new dev.\n");
341 vhost_devices[i] = dev;
343 dev->flags = VIRTIO_DEV_BUILTIN_VIRTIO_NET;
344 dev->slave_req_fd = -1;
345 dev->vdpa_dev_id = -1;
346 rte_spinlock_init(&dev->slave_req_lock);
352 vhost_destroy_device_notify(struct virtio_net *dev)
354 struct rte_vdpa_device *vdpa_dev;
357 if (dev->flags & VIRTIO_DEV_RUNNING) {
358 did = dev->vdpa_dev_id;
359 vdpa_dev = rte_vdpa_get_device(did);
360 if (vdpa_dev && vdpa_dev->ops->dev_close)
361 vdpa_dev->ops->dev_close(dev->vid);
362 dev->flags &= ~VIRTIO_DEV_RUNNING;
363 dev->notify_ops->destroy_device(dev->vid);
368 * Invoked when there is the vhost-user connection is broken (when
369 * the virtio device is being detached).
372 vhost_destroy_device(int vid)
374 struct virtio_net *dev = get_device(vid);
379 vhost_destroy_device_notify(dev);
381 cleanup_device(dev, 1);
384 vhost_devices[vid] = NULL;
388 vhost_attach_vdpa_device(int vid, int did)
390 struct virtio_net *dev = get_device(vid);
395 if (rte_vdpa_get_device(did) == NULL)
398 dev->vdpa_dev_id = did;
402 vhost_detach_vdpa_device(int vid)
404 struct virtio_net *dev = get_device(vid);
409 vhost_user_host_notifier_ctrl(vid, false);
411 dev->vdpa_dev_id = -1;
415 vhost_set_ifname(int vid, const char *if_name, unsigned int if_len)
417 struct virtio_net *dev;
420 dev = get_device(vid);
424 len = if_len > sizeof(dev->ifname) ?
425 sizeof(dev->ifname) : if_len;
427 strncpy(dev->ifname, if_name, len);
428 dev->ifname[sizeof(dev->ifname) - 1] = '\0';
432 vhost_enable_dequeue_zero_copy(int vid)
434 struct virtio_net *dev = get_device(vid);
439 dev->dequeue_zero_copy = 1;
443 vhost_set_builtin_virtio_net(int vid, bool enable)
445 struct virtio_net *dev = get_device(vid);
451 dev->flags |= VIRTIO_DEV_BUILTIN_VIRTIO_NET;
453 dev->flags &= ~VIRTIO_DEV_BUILTIN_VIRTIO_NET;
457 rte_vhost_get_mtu(int vid, uint16_t *mtu)
459 struct virtio_net *dev = get_device(vid);
464 if (!(dev->flags & VIRTIO_DEV_READY))
467 if (!(dev->features & (1ULL << VIRTIO_NET_F_MTU)))
476 rte_vhost_get_numa_node(int vid)
478 #ifdef RTE_LIBRTE_VHOST_NUMA
479 struct virtio_net *dev = get_device(vid);
486 ret = get_mempolicy(&numa_node, NULL, 0, dev,
487 MPOL_F_NODE | MPOL_F_ADDR);
489 RTE_LOG(ERR, VHOST_CONFIG,
490 "(%d) failed to query numa node: %s\n",
491 vid, rte_strerror(errno));
503 rte_vhost_get_queue_num(int vid)
505 struct virtio_net *dev = get_device(vid);
510 return dev->nr_vring / 2;
514 rte_vhost_get_vring_num(int vid)
516 struct virtio_net *dev = get_device(vid);
521 return dev->nr_vring;
525 rte_vhost_get_ifname(int vid, char *buf, size_t len)
527 struct virtio_net *dev = get_device(vid);
532 len = RTE_MIN(len, sizeof(dev->ifname));
534 strncpy(buf, dev->ifname, len);
541 rte_vhost_get_negotiated_features(int vid, uint64_t *features)
543 struct virtio_net *dev;
545 dev = get_device(vid);
549 *features = dev->features;
554 rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem)
556 struct virtio_net *dev;
557 struct rte_vhost_memory *m;
560 dev = get_device(vid);
564 size = dev->mem->nregions * sizeof(struct rte_vhost_mem_region);
565 m = malloc(sizeof(struct rte_vhost_memory) + size);
569 m->nregions = dev->mem->nregions;
570 memcpy(m->regions, dev->mem->regions, size);
577 rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
578 struct rte_vhost_vring *vring)
580 struct virtio_net *dev;
581 struct vhost_virtqueue *vq;
583 dev = get_device(vid);
587 if (vring_idx >= VHOST_MAX_VRING)
590 vq = dev->virtqueue[vring_idx];
594 vring->desc = vq->desc;
595 vring->avail = vq->avail;
596 vring->used = vq->used;
597 vring->log_guest_addr = vq->log_guest_addr;
599 vring->callfd = vq->callfd;
600 vring->kickfd = vq->kickfd;
601 vring->size = vq->size;
607 rte_vhost_vring_call(int vid, uint16_t vring_idx)
609 struct virtio_net *dev;
610 struct vhost_virtqueue *vq;
612 dev = get_device(vid);
616 if (vring_idx >= VHOST_MAX_VRING)
619 vq = dev->virtqueue[vring_idx];
623 if (vq_is_packed(dev))
624 vhost_vring_call_packed(dev, vq);
626 vhost_vring_call_split(dev, vq);
632 rte_vhost_avail_entries(int vid, uint16_t queue_id)
634 struct virtio_net *dev;
635 struct vhost_virtqueue *vq;
637 dev = get_device(vid);
641 vq = dev->virtqueue[queue_id];
645 return *(volatile uint16_t *)&vq->avail->idx - vq->last_used_idx;
649 vhost_enable_notify_split(struct vhost_virtqueue *vq, int enable)
652 vq->used->flags &= ~VRING_USED_F_NO_NOTIFY;
654 vq->used->flags |= VRING_USED_F_NO_NOTIFY;
658 vhost_enable_notify_packed(struct virtio_net *dev,
659 struct vhost_virtqueue *vq, int enable)
664 vq->device_event->flags = VRING_EVENT_F_DISABLE;
666 flags = VRING_EVENT_F_ENABLE;
667 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX)) {
668 flags = VRING_EVENT_F_DESC;
669 vq->device_event->off_wrap = vq->last_avail_idx |
670 vq->avail_wrap_counter << 15;
675 vq->device_event->flags = flags;
679 rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable)
681 struct virtio_net *dev = get_device(vid);
682 struct vhost_virtqueue *vq;
687 vq = dev->virtqueue[queue_id];
689 if (vq_is_packed(dev))
690 vhost_enable_notify_packed(dev, vq, enable);
692 vhost_enable_notify_split(vq, enable);
698 rte_vhost_log_write(int vid, uint64_t addr, uint64_t len)
700 struct virtio_net *dev = get_device(vid);
705 vhost_log_write(dev, addr, len);
709 rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
710 uint64_t offset, uint64_t len)
712 struct virtio_net *dev;
713 struct vhost_virtqueue *vq;
715 dev = get_device(vid);
719 if (vring_idx >= VHOST_MAX_VRING)
721 vq = dev->virtqueue[vring_idx];
725 vhost_log_used_vring(dev, vq, offset, len);
729 rte_vhost_rx_queue_count(int vid, uint16_t qid)
731 struct virtio_net *dev;
732 struct vhost_virtqueue *vq;
734 dev = get_device(vid);
738 if (unlikely(qid >= dev->nr_vring || (qid & 1) == 0)) {
739 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
740 dev->vid, __func__, qid);
744 vq = dev->virtqueue[qid];
748 if (unlikely(vq->enabled == 0 || vq->avail == NULL))
751 return *((volatile uint16_t *)&vq->avail->idx) - vq->last_avail_idx;
754 int rte_vhost_get_vdpa_device_id(int vid)
756 struct virtio_net *dev = get_device(vid);
761 return dev->vdpa_dev_id;
764 int rte_vhost_get_log_base(int vid, uint64_t *log_base,
767 struct virtio_net *dev = get_device(vid);
772 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
773 RTE_LOG(ERR, VHOST_DATA,
774 "(%d) %s: built-in vhost net backend is disabled.\n",
779 *log_base = dev->log_base;
780 *log_size = dev->log_size;
785 int rte_vhost_get_vring_base(int vid, uint16_t queue_id,
786 uint16_t *last_avail_idx, uint16_t *last_used_idx)
788 struct virtio_net *dev = get_device(vid);
793 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
794 RTE_LOG(ERR, VHOST_DATA,
795 "(%d) %s: built-in vhost net backend is disabled.\n",
800 *last_avail_idx = dev->virtqueue[queue_id]->last_avail_idx;
801 *last_used_idx = dev->virtqueue[queue_id]->last_used_idx;
806 int rte_vhost_set_vring_base(int vid, uint16_t queue_id,
807 uint16_t last_avail_idx, uint16_t last_used_idx)
809 struct virtio_net *dev = get_device(vid);
814 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
815 RTE_LOG(ERR, VHOST_DATA,
816 "(%d) %s: built-in vhost net backend is disabled.\n",
821 dev->virtqueue[queue_id]->last_avail_idx = last_avail_idx;
822 dev->virtqueue[queue_id]->last_used_idx = last_used_idx;