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);
73 cleanup_vq(struct vhost_virtqueue *vq, int destroy)
75 if ((vq->callfd >= 0) && (destroy != 0))
82 * Unmap any memory, close any file descriptors and
83 * free any memory owned by a device.
86 cleanup_device(struct virtio_net *dev, int destroy)
90 vhost_backend_cleanup(dev);
92 for (i = 0; i < dev->nr_vring; i++)
93 cleanup_vq(dev->virtqueue[i], destroy);
97 free_vq(struct virtio_net *dev, struct vhost_virtqueue *vq)
99 if (vq_is_packed(dev))
100 rte_free(vq->shadow_used_packed);
102 rte_free(vq->shadow_used_split);
103 rte_free(vq->batch_copy_elems);
104 rte_mempool_free(vq->iotlb_pool);
109 * Release virtqueues and device memory.
112 free_device(struct virtio_net *dev)
116 for (i = 0; i < dev->nr_vring; i++)
117 free_vq(dev, dev->virtqueue[i]);
123 vring_translate_split(struct virtio_net *dev, struct vhost_virtqueue *vq)
125 uint64_t req_size, size;
127 req_size = sizeof(struct vring_desc) * vq->size;
129 vq->desc = (struct vring_desc *)(uintptr_t)vhost_iova_to_vva(dev, vq,
130 vq->ring_addrs.desc_user_addr,
131 &size, VHOST_ACCESS_RW);
132 if (!vq->desc || size != req_size)
135 req_size = sizeof(struct vring_avail);
136 req_size += sizeof(uint16_t) * vq->size;
137 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))
138 req_size += sizeof(uint16_t);
140 vq->avail = (struct vring_avail *)(uintptr_t)vhost_iova_to_vva(dev, vq,
141 vq->ring_addrs.avail_user_addr,
142 &size, VHOST_ACCESS_RW);
143 if (!vq->avail || size != req_size)
146 req_size = sizeof(struct vring_used);
147 req_size += sizeof(struct vring_used_elem) * vq->size;
148 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))
149 req_size += sizeof(uint16_t);
151 vq->used = (struct vring_used *)(uintptr_t)vhost_iova_to_vva(dev, vq,
152 vq->ring_addrs.used_user_addr,
153 &size, VHOST_ACCESS_RW);
154 if (!vq->used || size != req_size)
161 vring_translate_packed(struct virtio_net *dev, struct vhost_virtqueue *vq)
163 uint64_t req_size, size;
165 req_size = sizeof(struct vring_packed_desc) * vq->size;
167 vq->desc_packed = (struct vring_packed_desc *)(uintptr_t)
168 vhost_iova_to_vva(dev, vq, vq->ring_addrs.desc_user_addr,
169 &size, VHOST_ACCESS_RW);
170 if (!vq->desc_packed || size != req_size)
173 req_size = sizeof(struct vring_packed_desc_event);
175 vq->driver_event = (struct vring_packed_desc_event *)(uintptr_t)
176 vhost_iova_to_vva(dev, vq, vq->ring_addrs.avail_user_addr,
177 &size, VHOST_ACCESS_RW);
178 if (!vq->driver_event || size != req_size)
181 req_size = sizeof(struct vring_packed_desc_event);
183 vq->device_event = (struct vring_packed_desc_event *)(uintptr_t)
184 vhost_iova_to_vva(dev, vq, vq->ring_addrs.used_user_addr,
185 &size, VHOST_ACCESS_RW);
186 if (!vq->device_event || size != req_size)
193 vring_translate(struct virtio_net *dev, struct vhost_virtqueue *vq)
196 if (!(dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)))
199 if (vq_is_packed(dev)) {
200 if (vring_translate_packed(dev, vq) < 0)
203 if (vring_translate_split(dev, vq) < 0)
213 vring_invalidate(struct virtio_net *dev, struct vhost_virtqueue *vq)
215 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
216 vhost_user_iotlb_wr_lock(vq);
223 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
224 vhost_user_iotlb_wr_unlock(vq);
228 init_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
230 struct vhost_virtqueue *vq;
232 if (vring_idx >= VHOST_MAX_VRING) {
233 RTE_LOG(ERR, VHOST_CONFIG,
234 "Failed not init vring, out of bound (%d)\n",
239 vq = dev->virtqueue[vring_idx];
241 memset(vq, 0, sizeof(struct vhost_virtqueue));
243 vq->kickfd = VIRTIO_UNINITIALIZED_EVENTFD;
244 vq->callfd = VIRTIO_UNINITIALIZED_EVENTFD;
246 vhost_user_iotlb_init(dev, vring_idx);
247 /* Backends are set to -1 indicating an inactive device. */
250 TAILQ_INIT(&vq->zmbuf_list);
254 reset_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
256 struct vhost_virtqueue *vq;
259 if (vring_idx >= VHOST_MAX_VRING) {
260 RTE_LOG(ERR, VHOST_CONFIG,
261 "Failed not init vring, out of bound (%d)\n",
266 vq = dev->virtqueue[vring_idx];
268 init_vring_queue(dev, vring_idx);
273 alloc_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
275 struct vhost_virtqueue *vq;
277 vq = rte_malloc(NULL, sizeof(struct vhost_virtqueue), 0);
279 RTE_LOG(ERR, VHOST_CONFIG,
280 "Failed to allocate memory for vring:%u.\n", vring_idx);
284 dev->virtqueue[vring_idx] = vq;
285 init_vring_queue(dev, vring_idx);
286 rte_spinlock_init(&vq->access_lock);
287 vq->avail_wrap_counter = 1;
288 vq->used_wrap_counter = 1;
289 vq->signalled_used_valid = false;
297 * Reset some variables in device structure, while keeping few
298 * others untouched, such as vid, ifname, nr_vring: they
299 * should be same unless the device is removed.
302 reset_device(struct virtio_net *dev)
307 dev->protocol_features = 0;
308 dev->flags &= VIRTIO_DEV_BUILTIN_VIRTIO_NET;
310 for (i = 0; i < dev->nr_vring; i++)
311 reset_vring_queue(dev, i);
315 * Invoked when there is a new vhost-user connection established (when
316 * there is a new virtio device being attached).
319 vhost_new_device(void)
321 struct virtio_net *dev;
324 for (i = 0; i < MAX_VHOST_DEVICE; i++) {
325 if (vhost_devices[i] == NULL)
329 if (i == MAX_VHOST_DEVICE) {
330 RTE_LOG(ERR, VHOST_CONFIG,
331 "Failed to find a free slot for new device.\n");
335 dev = rte_zmalloc(NULL, sizeof(struct virtio_net), 0);
337 RTE_LOG(ERR, VHOST_CONFIG,
338 "Failed to allocate memory for new dev.\n");
342 vhost_devices[i] = dev;
344 dev->flags = VIRTIO_DEV_BUILTIN_VIRTIO_NET;
345 dev->slave_req_fd = -1;
346 dev->vdpa_dev_id = -1;
347 dev->postcopy_ufd = -1;
348 rte_spinlock_init(&dev->slave_req_lock);
354 vhost_destroy_device_notify(struct virtio_net *dev)
356 struct rte_vdpa_device *vdpa_dev;
359 if (dev->flags & VIRTIO_DEV_RUNNING) {
360 did = dev->vdpa_dev_id;
361 vdpa_dev = rte_vdpa_get_device(did);
362 if (vdpa_dev && vdpa_dev->ops->dev_close)
363 vdpa_dev->ops->dev_close(dev->vid);
364 dev->flags &= ~VIRTIO_DEV_RUNNING;
365 dev->notify_ops->destroy_device(dev->vid);
370 * Invoked when there is the vhost-user connection is broken (when
371 * the virtio device is being detached).
374 vhost_destroy_device(int vid)
376 struct virtio_net *dev = get_device(vid);
381 vhost_destroy_device_notify(dev);
383 cleanup_device(dev, 1);
386 vhost_devices[vid] = NULL;
390 vhost_attach_vdpa_device(int vid, int did)
392 struct virtio_net *dev = get_device(vid);
397 if (rte_vdpa_get_device(did) == NULL)
400 dev->vdpa_dev_id = did;
404 vhost_set_ifname(int vid, const char *if_name, unsigned int if_len)
406 struct virtio_net *dev;
409 dev = get_device(vid);
413 len = if_len > sizeof(dev->ifname) ?
414 sizeof(dev->ifname) : if_len;
416 strncpy(dev->ifname, if_name, len);
417 dev->ifname[sizeof(dev->ifname) - 1] = '\0';
421 vhost_enable_dequeue_zero_copy(int vid)
423 struct virtio_net *dev = get_device(vid);
428 dev->dequeue_zero_copy = 1;
432 vhost_set_builtin_virtio_net(int vid, bool enable)
434 struct virtio_net *dev = get_device(vid);
440 dev->flags |= VIRTIO_DEV_BUILTIN_VIRTIO_NET;
442 dev->flags &= ~VIRTIO_DEV_BUILTIN_VIRTIO_NET;
446 rte_vhost_get_mtu(int vid, uint16_t *mtu)
448 struct virtio_net *dev = get_device(vid);
453 if (!(dev->flags & VIRTIO_DEV_READY))
456 if (!(dev->features & (1ULL << VIRTIO_NET_F_MTU)))
465 rte_vhost_get_numa_node(int vid)
467 #ifdef RTE_LIBRTE_VHOST_NUMA
468 struct virtio_net *dev = get_device(vid);
472 if (dev == NULL || numa_available() != 0)
475 ret = get_mempolicy(&numa_node, NULL, 0, dev,
476 MPOL_F_NODE | MPOL_F_ADDR);
478 RTE_LOG(ERR, VHOST_CONFIG,
479 "(%d) failed to query numa node: %s\n",
480 vid, rte_strerror(errno));
492 rte_vhost_get_queue_num(int vid)
494 struct virtio_net *dev = get_device(vid);
499 return dev->nr_vring / 2;
503 rte_vhost_get_vring_num(int vid)
505 struct virtio_net *dev = get_device(vid);
510 return dev->nr_vring;
514 rte_vhost_get_ifname(int vid, char *buf, size_t len)
516 struct virtio_net *dev = get_device(vid);
521 len = RTE_MIN(len, sizeof(dev->ifname));
523 strncpy(buf, dev->ifname, len);
530 rte_vhost_get_negotiated_features(int vid, uint64_t *features)
532 struct virtio_net *dev;
534 dev = get_device(vid);
538 *features = dev->features;
543 rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem)
545 struct virtio_net *dev;
546 struct rte_vhost_memory *m;
549 dev = get_device(vid);
553 size = dev->mem->nregions * sizeof(struct rte_vhost_mem_region);
554 m = malloc(sizeof(struct rte_vhost_memory) + size);
558 m->nregions = dev->mem->nregions;
559 memcpy(m->regions, dev->mem->regions, size);
566 rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
567 struct rte_vhost_vring *vring)
569 struct virtio_net *dev;
570 struct vhost_virtqueue *vq;
572 dev = get_device(vid);
576 if (vring_idx >= VHOST_MAX_VRING)
579 vq = dev->virtqueue[vring_idx];
583 vring->desc = vq->desc;
584 vring->avail = vq->avail;
585 vring->used = vq->used;
586 vring->log_guest_addr = vq->log_guest_addr;
588 vring->callfd = vq->callfd;
589 vring->kickfd = vq->kickfd;
590 vring->size = vq->size;
596 rte_vhost_vring_call(int vid, uint16_t vring_idx)
598 struct virtio_net *dev;
599 struct vhost_virtqueue *vq;
601 dev = get_device(vid);
605 if (vring_idx >= VHOST_MAX_VRING)
608 vq = dev->virtqueue[vring_idx];
612 if (vq_is_packed(dev))
613 vhost_vring_call_packed(dev, vq);
615 vhost_vring_call_split(dev, vq);
621 rte_vhost_avail_entries(int vid, uint16_t queue_id)
623 struct virtio_net *dev;
624 struct vhost_virtqueue *vq;
626 dev = get_device(vid);
630 vq = dev->virtqueue[queue_id];
634 return *(volatile uint16_t *)&vq->avail->idx - vq->last_used_idx;
638 vhost_enable_notify_split(struct virtio_net *dev,
639 struct vhost_virtqueue *vq, int enable)
641 if (!(dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))) {
643 vq->used->flags &= ~VRING_USED_F_NO_NOTIFY;
645 vq->used->flags |= VRING_USED_F_NO_NOTIFY;
648 vhost_avail_event(vq) = vq->last_avail_idx;
653 vhost_enable_notify_packed(struct virtio_net *dev,
654 struct vhost_virtqueue *vq, int enable)
659 vq->device_event->flags = VRING_EVENT_F_DISABLE;
663 flags = VRING_EVENT_F_ENABLE;
664 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX)) {
665 flags = VRING_EVENT_F_DESC;
666 vq->device_event->off_wrap = vq->last_avail_idx |
667 vq->avail_wrap_counter << 15;
672 vq->device_event->flags = flags;
676 rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable)
678 struct virtio_net *dev = get_device(vid);
679 struct vhost_virtqueue *vq;
684 vq = dev->virtqueue[queue_id];
686 if (vq_is_packed(dev))
687 vhost_enable_notify_packed(dev, vq, enable);
689 vhost_enable_notify_split(dev, vq, enable);
695 rte_vhost_log_write(int vid, uint64_t addr, uint64_t len)
697 struct virtio_net *dev = get_device(vid);
702 vhost_log_write(dev, addr, len);
706 rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
707 uint64_t offset, uint64_t len)
709 struct virtio_net *dev;
710 struct vhost_virtqueue *vq;
712 dev = get_device(vid);
716 if (vring_idx >= VHOST_MAX_VRING)
718 vq = dev->virtqueue[vring_idx];
722 vhost_log_used_vring(dev, vq, offset, len);
726 rte_vhost_rx_queue_count(int vid, uint16_t qid)
728 struct virtio_net *dev;
729 struct vhost_virtqueue *vq;
731 dev = get_device(vid);
735 if (unlikely(qid >= dev->nr_vring || (qid & 1) == 0)) {
736 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
737 dev->vid, __func__, qid);
741 vq = dev->virtqueue[qid];
745 if (unlikely(vq->enabled == 0 || vq->avail == NULL))
748 return *((volatile uint16_t *)&vq->avail->idx) - vq->last_avail_idx;
751 int rte_vhost_get_vdpa_device_id(int vid)
753 struct virtio_net *dev = get_device(vid);
758 return dev->vdpa_dev_id;
761 int rte_vhost_get_log_base(int vid, uint64_t *log_base,
764 struct virtio_net *dev = get_device(vid);
769 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
770 RTE_LOG(ERR, VHOST_DATA,
771 "(%d) %s: built-in vhost net backend is disabled.\n",
776 *log_base = dev->log_base;
777 *log_size = dev->log_size;
782 int rte_vhost_get_vring_base(int vid, uint16_t queue_id,
783 uint16_t *last_avail_idx, uint16_t *last_used_idx)
785 struct virtio_net *dev = get_device(vid);
790 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
791 RTE_LOG(ERR, VHOST_DATA,
792 "(%d) %s: built-in vhost net backend is disabled.\n",
797 *last_avail_idx = dev->virtqueue[queue_id]->last_avail_idx;
798 *last_used_idx = dev->virtqueue[queue_id]->last_used_idx;
803 int rte_vhost_set_vring_base(int vid, uint16_t queue_id,
804 uint16_t last_avail_idx, uint16_t last_used_idx)
806 struct virtio_net *dev = get_device(vid);
811 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
812 RTE_LOG(ERR, VHOST_DATA,
813 "(%d) %s: built-in vhost net backend is disabled.\n",
818 dev->virtqueue[queue_id]->last_avail_idx = last_avail_idx;
819 dev->virtqueue[queue_id]->last_used_idx = last_used_idx;