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;
167 (struct vring_packed_desc *)(uintptr_t)vhost_iova_to_vva(dev,
168 vq, vq->ring_addrs.desc_user_addr,
169 &size, VHOST_ACCESS_RW);
170 if (!vq->desc_packed || size != req_size)
177 vring_translate(struct virtio_net *dev, struct vhost_virtqueue *vq)
180 if (!(dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)))
183 if (vq_is_packed(dev)) {
184 if (vring_translate_packed(dev, vq) < 0)
187 if (vring_translate_split(dev, vq) < 0)
197 vring_invalidate(struct virtio_net *dev, struct vhost_virtqueue *vq)
199 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
200 vhost_user_iotlb_wr_lock(vq);
207 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
208 vhost_user_iotlb_wr_unlock(vq);
212 init_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
214 struct vhost_virtqueue *vq;
216 if (vring_idx >= VHOST_MAX_VRING) {
217 RTE_LOG(ERR, VHOST_CONFIG,
218 "Failed not init vring, out of bound (%d)\n",
223 vq = dev->virtqueue[vring_idx];
225 memset(vq, 0, sizeof(struct vhost_virtqueue));
227 vq->kickfd = VIRTIO_UNINITIALIZED_EVENTFD;
228 vq->callfd = VIRTIO_UNINITIALIZED_EVENTFD;
230 vhost_user_iotlb_init(dev, vring_idx);
231 /* Backends are set to -1 indicating an inactive device. */
234 TAILQ_INIT(&vq->zmbuf_list);
238 reset_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
240 struct vhost_virtqueue *vq;
243 if (vring_idx >= VHOST_MAX_VRING) {
244 RTE_LOG(ERR, VHOST_CONFIG,
245 "Failed not init vring, out of bound (%d)\n",
250 vq = dev->virtqueue[vring_idx];
252 init_vring_queue(dev, vring_idx);
257 alloc_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
259 struct vhost_virtqueue *vq;
261 vq = rte_malloc(NULL, sizeof(struct vhost_virtqueue), 0);
263 RTE_LOG(ERR, VHOST_CONFIG,
264 "Failed to allocate memory for vring:%u.\n", vring_idx);
268 dev->virtqueue[vring_idx] = vq;
269 init_vring_queue(dev, vring_idx);
270 rte_spinlock_init(&vq->access_lock);
271 vq->avail_wrap_counter = 1;
272 vq->used_wrap_counter = 1;
280 * Reset some variables in device structure, while keeping few
281 * others untouched, such as vid, ifname, nr_vring: they
282 * should be same unless the device is removed.
285 reset_device(struct virtio_net *dev)
290 dev->protocol_features = 0;
291 dev->flags &= VIRTIO_DEV_BUILTIN_VIRTIO_NET;
293 for (i = 0; i < dev->nr_vring; i++)
294 reset_vring_queue(dev, i);
298 * Invoked when there is a new vhost-user connection established (when
299 * there is a new virtio device being attached).
302 vhost_new_device(void)
304 struct virtio_net *dev;
307 for (i = 0; i < MAX_VHOST_DEVICE; i++) {
308 if (vhost_devices[i] == NULL)
312 if (i == MAX_VHOST_DEVICE) {
313 RTE_LOG(ERR, VHOST_CONFIG,
314 "Failed to find a free slot for new device.\n");
318 dev = rte_zmalloc(NULL, sizeof(struct virtio_net), 0);
320 RTE_LOG(ERR, VHOST_CONFIG,
321 "Failed to allocate memory for new dev.\n");
325 vhost_devices[i] = dev;
327 dev->flags = VIRTIO_DEV_BUILTIN_VIRTIO_NET;
328 dev->slave_req_fd = -1;
329 dev->vdpa_dev_id = -1;
330 rte_spinlock_init(&dev->slave_req_lock);
336 vhost_destroy_device_notify(struct virtio_net *dev)
338 struct rte_vdpa_device *vdpa_dev;
341 if (dev->flags & VIRTIO_DEV_RUNNING) {
342 did = dev->vdpa_dev_id;
343 vdpa_dev = rte_vdpa_get_device(did);
344 if (vdpa_dev && vdpa_dev->ops->dev_close)
345 vdpa_dev->ops->dev_close(dev->vid);
346 dev->flags &= ~VIRTIO_DEV_RUNNING;
347 dev->notify_ops->destroy_device(dev->vid);
352 * Invoked when there is the vhost-user connection is broken (when
353 * the virtio device is being detached).
356 vhost_destroy_device(int vid)
358 struct virtio_net *dev = get_device(vid);
363 vhost_destroy_device_notify(dev);
365 cleanup_device(dev, 1);
368 vhost_devices[vid] = NULL;
372 vhost_attach_vdpa_device(int vid, int did)
374 struct virtio_net *dev = get_device(vid);
379 if (rte_vdpa_get_device(did) == NULL)
382 dev->vdpa_dev_id = did;
386 vhost_detach_vdpa_device(int vid)
388 struct virtio_net *dev = get_device(vid);
393 vhost_user_host_notifier_ctrl(vid, false);
395 dev->vdpa_dev_id = -1;
399 vhost_set_ifname(int vid, const char *if_name, unsigned int if_len)
401 struct virtio_net *dev;
404 dev = get_device(vid);
408 len = if_len > sizeof(dev->ifname) ?
409 sizeof(dev->ifname) : if_len;
411 strncpy(dev->ifname, if_name, len);
412 dev->ifname[sizeof(dev->ifname) - 1] = '\0';
416 vhost_enable_dequeue_zero_copy(int vid)
418 struct virtio_net *dev = get_device(vid);
423 dev->dequeue_zero_copy = 1;
427 vhost_set_builtin_virtio_net(int vid, bool enable)
429 struct virtio_net *dev = get_device(vid);
435 dev->flags |= VIRTIO_DEV_BUILTIN_VIRTIO_NET;
437 dev->flags &= ~VIRTIO_DEV_BUILTIN_VIRTIO_NET;
441 rte_vhost_get_mtu(int vid, uint16_t *mtu)
443 struct virtio_net *dev = get_device(vid);
448 if (!(dev->flags & VIRTIO_DEV_READY))
451 if (!(dev->features & (1ULL << VIRTIO_NET_F_MTU)))
460 rte_vhost_get_numa_node(int vid)
462 #ifdef RTE_LIBRTE_VHOST_NUMA
463 struct virtio_net *dev = get_device(vid);
470 ret = get_mempolicy(&numa_node, NULL, 0, dev,
471 MPOL_F_NODE | MPOL_F_ADDR);
473 RTE_LOG(ERR, VHOST_CONFIG,
474 "(%d) failed to query numa node: %s\n",
475 vid, rte_strerror(errno));
487 rte_vhost_get_queue_num(int vid)
489 struct virtio_net *dev = get_device(vid);
494 return dev->nr_vring / 2;
498 rte_vhost_get_vring_num(int vid)
500 struct virtio_net *dev = get_device(vid);
505 return dev->nr_vring;
509 rte_vhost_get_ifname(int vid, char *buf, size_t len)
511 struct virtio_net *dev = get_device(vid);
516 len = RTE_MIN(len, sizeof(dev->ifname));
518 strncpy(buf, dev->ifname, len);
525 rte_vhost_get_negotiated_features(int vid, uint64_t *features)
527 struct virtio_net *dev;
529 dev = get_device(vid);
533 *features = dev->features;
538 rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem)
540 struct virtio_net *dev;
541 struct rte_vhost_memory *m;
544 dev = get_device(vid);
548 size = dev->mem->nregions * sizeof(struct rte_vhost_mem_region);
549 m = malloc(sizeof(struct rte_vhost_memory) + size);
553 m->nregions = dev->mem->nregions;
554 memcpy(m->regions, dev->mem->regions, size);
561 rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
562 struct rte_vhost_vring *vring)
564 struct virtio_net *dev;
565 struct vhost_virtqueue *vq;
567 dev = get_device(vid);
571 if (vring_idx >= VHOST_MAX_VRING)
574 vq = dev->virtqueue[vring_idx];
578 vring->desc = vq->desc;
579 vring->avail = vq->avail;
580 vring->used = vq->used;
581 vring->log_guest_addr = vq->log_guest_addr;
583 vring->callfd = vq->callfd;
584 vring->kickfd = vq->kickfd;
585 vring->size = vq->size;
591 rte_vhost_vring_call(int vid, uint16_t vring_idx)
593 struct virtio_net *dev;
594 struct vhost_virtqueue *vq;
596 dev = get_device(vid);
600 if (vring_idx >= VHOST_MAX_VRING)
603 vq = dev->virtqueue[vring_idx];
607 vhost_vring_call(dev, vq);
612 rte_vhost_avail_entries(int vid, uint16_t queue_id)
614 struct virtio_net *dev;
615 struct vhost_virtqueue *vq;
617 dev = get_device(vid);
621 vq = dev->virtqueue[queue_id];
625 return *(volatile uint16_t *)&vq->avail->idx - vq->last_used_idx;
629 rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable)
631 struct virtio_net *dev = get_device(vid);
637 dev->virtqueue[queue_id]->used->flags &=
638 ~VRING_USED_F_NO_NOTIFY;
640 dev->virtqueue[queue_id]->used->flags |= VRING_USED_F_NO_NOTIFY;
645 rte_vhost_log_write(int vid, uint64_t addr, uint64_t len)
647 struct virtio_net *dev = get_device(vid);
652 vhost_log_write(dev, addr, len);
656 rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
657 uint64_t offset, uint64_t len)
659 struct virtio_net *dev;
660 struct vhost_virtqueue *vq;
662 dev = get_device(vid);
666 if (vring_idx >= VHOST_MAX_VRING)
668 vq = dev->virtqueue[vring_idx];
672 vhost_log_used_vring(dev, vq, offset, len);
676 rte_vhost_rx_queue_count(int vid, uint16_t qid)
678 struct virtio_net *dev;
679 struct vhost_virtqueue *vq;
681 dev = get_device(vid);
685 if (unlikely(qid >= dev->nr_vring || (qid & 1) == 0)) {
686 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
687 dev->vid, __func__, qid);
691 vq = dev->virtqueue[qid];
695 if (unlikely(vq->enabled == 0 || vq->avail == NULL))
698 return *((volatile uint16_t *)&vq->avail->idx) - vq->last_avail_idx;
701 int rte_vhost_get_vdpa_device_id(int vid)
703 struct virtio_net *dev = get_device(vid);
708 return dev->vdpa_dev_id;
711 int rte_vhost_get_log_base(int vid, uint64_t *log_base,
714 struct virtio_net *dev = get_device(vid);
719 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
720 RTE_LOG(ERR, VHOST_DATA,
721 "(%d) %s: built-in vhost net backend is disabled.\n",
726 *log_base = dev->log_base;
727 *log_size = dev->log_size;
732 int rte_vhost_get_vring_base(int vid, uint16_t queue_id,
733 uint16_t *last_avail_idx, uint16_t *last_used_idx)
735 struct virtio_net *dev = get_device(vid);
740 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
741 RTE_LOG(ERR, VHOST_DATA,
742 "(%d) %s: built-in vhost net backend is disabled.\n",
747 *last_avail_idx = dev->virtqueue[queue_id]->last_avail_idx;
748 *last_used_idx = dev->virtqueue[queue_id]->last_used_idx;
753 int rte_vhost_set_vring_base(int vid, uint16_t queue_id,
754 uint16_t last_avail_idx, uint16_t last_used_idx)
756 struct virtio_net *dev = get_device(vid);
761 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
762 RTE_LOG(ERR, VHOST_DATA,
763 "(%d) %s: built-in vhost net backend is disabled.\n",
768 dev->virtqueue[queue_id]->last_avail_idx = last_avail_idx;
769 dev->virtqueue[queue_id]->last_used_idx = last_used_idx;