1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2016 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);
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 vhost_virtqueue *vq)
98 rte_free(vq->shadow_used_ring);
99 rte_free(vq->batch_copy_elems);
100 rte_mempool_free(vq->iotlb_pool);
105 * Release virtqueues and device memory.
108 free_device(struct virtio_net *dev)
112 for (i = 0; i < dev->nr_vring; i++)
113 free_vq(dev->virtqueue[i]);
119 vring_translate(struct virtio_net *dev, struct vhost_virtqueue *vq)
123 if (!(dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)))
126 size = sizeof(struct vring_desc) * vq->size;
127 vq->desc = (struct vring_desc *)(uintptr_t)vhost_iova_to_vva(dev, vq,
128 vq->ring_addrs.desc_user_addr,
129 size, VHOST_ACCESS_RW);
133 size = sizeof(struct vring_avail);
134 size += sizeof(uint16_t) * vq->size;
135 vq->avail = (struct vring_avail *)(uintptr_t)vhost_iova_to_vva(dev, vq,
136 vq->ring_addrs.avail_user_addr,
137 size, VHOST_ACCESS_RW);
141 size = sizeof(struct vring_used);
142 size += sizeof(struct vring_used_elem) * vq->size;
143 vq->used = (struct vring_used *)(uintptr_t)vhost_iova_to_vva(dev, vq,
144 vq->ring_addrs.used_user_addr,
145 size, VHOST_ACCESS_RW);
156 vring_invalidate(struct virtio_net *dev, struct vhost_virtqueue *vq)
158 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
159 vhost_user_iotlb_wr_lock(vq);
166 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
167 vhost_user_iotlb_wr_unlock(vq);
171 init_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
173 struct vhost_virtqueue *vq;
175 if (vring_idx >= VHOST_MAX_VRING) {
176 RTE_LOG(ERR, VHOST_CONFIG,
177 "Failed not init vring, out of bound (%d)\n",
182 vq = dev->virtqueue[vring_idx];
184 memset(vq, 0, sizeof(struct vhost_virtqueue));
186 vq->kickfd = VIRTIO_UNINITIALIZED_EVENTFD;
187 vq->callfd = VIRTIO_UNINITIALIZED_EVENTFD;
189 vhost_user_iotlb_init(dev, vring_idx);
190 /* Backends are set to -1 indicating an inactive device. */
193 TAILQ_INIT(&vq->zmbuf_list);
197 reset_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
199 struct vhost_virtqueue *vq;
202 if (vring_idx >= VHOST_MAX_VRING) {
203 RTE_LOG(ERR, VHOST_CONFIG,
204 "Failed not init vring, out of bound (%d)\n",
209 vq = dev->virtqueue[vring_idx];
211 init_vring_queue(dev, vring_idx);
216 alloc_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
218 struct vhost_virtqueue *vq;
220 vq = rte_malloc(NULL, sizeof(struct vhost_virtqueue), 0);
222 RTE_LOG(ERR, VHOST_CONFIG,
223 "Failed to allocate memory for vring:%u.\n", vring_idx);
227 dev->virtqueue[vring_idx] = vq;
228 init_vring_queue(dev, vring_idx);
229 rte_spinlock_init(&vq->access_lock);
237 * Reset some variables in device structure, while keeping few
238 * others untouched, such as vid, ifname, nr_vring: they
239 * should be same unless the device is removed.
242 reset_device(struct virtio_net *dev)
247 dev->protocol_features = 0;
248 dev->flags &= VIRTIO_DEV_BUILTIN_VIRTIO_NET;
250 for (i = 0; i < dev->nr_vring; i++)
251 reset_vring_queue(dev, i);
255 * Invoked when there is a new vhost-user connection established (when
256 * there is a new virtio device being attached).
259 vhost_new_device(void)
261 struct virtio_net *dev;
264 dev = rte_zmalloc(NULL, sizeof(struct virtio_net), 0);
266 RTE_LOG(ERR, VHOST_CONFIG,
267 "Failed to allocate memory for new dev.\n");
271 for (i = 0; i < MAX_VHOST_DEVICE; i++) {
272 if (vhost_devices[i] == NULL)
275 if (i == MAX_VHOST_DEVICE) {
276 RTE_LOG(ERR, VHOST_CONFIG,
277 "Failed to find a free slot for new device.\n");
282 vhost_devices[i] = dev;
284 dev->flags = VIRTIO_DEV_BUILTIN_VIRTIO_NET;
285 dev->slave_req_fd = -1;
286 dev->vdpa_dev_id = -1;
292 * Invoked when there is the vhost-user connection is broken (when
293 * the virtio device is being detached).
296 vhost_destroy_device(int vid)
298 struct virtio_net *dev = get_device(vid);
303 if (dev->flags & VIRTIO_DEV_RUNNING) {
304 dev->flags &= ~VIRTIO_DEV_RUNNING;
305 dev->notify_ops->destroy_device(vid);
308 cleanup_device(dev, 1);
311 vhost_devices[vid] = NULL;
315 vhost_attach_vdpa_device(int vid, int did)
317 struct virtio_net *dev = get_device(vid);
322 if (rte_vdpa_get_device(did) == NULL)
325 dev->vdpa_dev_id = did;
329 vhost_detach_vdpa_device(int vid)
331 struct virtio_net *dev = get_device(vid);
336 dev->vdpa_dev_id = -1;
340 vhost_set_ifname(int vid, const char *if_name, unsigned int if_len)
342 struct virtio_net *dev;
345 dev = get_device(vid);
349 len = if_len > sizeof(dev->ifname) ?
350 sizeof(dev->ifname) : if_len;
352 strncpy(dev->ifname, if_name, len);
353 dev->ifname[sizeof(dev->ifname) - 1] = '\0';
357 vhost_enable_dequeue_zero_copy(int vid)
359 struct virtio_net *dev = get_device(vid);
364 dev->dequeue_zero_copy = 1;
368 vhost_set_builtin_virtio_net(int vid, bool enable)
370 struct virtio_net *dev = get_device(vid);
376 dev->flags |= VIRTIO_DEV_BUILTIN_VIRTIO_NET;
378 dev->flags &= ~VIRTIO_DEV_BUILTIN_VIRTIO_NET;
382 rte_vhost_get_mtu(int vid, uint16_t *mtu)
384 struct virtio_net *dev = get_device(vid);
389 if (!(dev->flags & VIRTIO_DEV_READY))
392 if (!(dev->features & (1ULL << VIRTIO_NET_F_MTU)))
401 rte_vhost_get_numa_node(int vid)
403 #ifdef RTE_LIBRTE_VHOST_NUMA
404 struct virtio_net *dev = get_device(vid);
411 ret = get_mempolicy(&numa_node, NULL, 0, dev,
412 MPOL_F_NODE | MPOL_F_ADDR);
414 RTE_LOG(ERR, VHOST_CONFIG,
415 "(%d) failed to query numa node: %s\n",
416 vid, rte_strerror(errno));
428 rte_vhost_get_queue_num(int vid)
430 struct virtio_net *dev = get_device(vid);
435 return dev->nr_vring / 2;
439 rte_vhost_get_vring_num(int vid)
441 struct virtio_net *dev = get_device(vid);
446 return dev->nr_vring;
450 rte_vhost_get_ifname(int vid, char *buf, size_t len)
452 struct virtio_net *dev = get_device(vid);
457 len = RTE_MIN(len, sizeof(dev->ifname));
459 strncpy(buf, dev->ifname, len);
466 rte_vhost_get_negotiated_features(int vid, uint64_t *features)
468 struct virtio_net *dev;
470 dev = get_device(vid);
474 *features = dev->features;
479 rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem)
481 struct virtio_net *dev;
482 struct rte_vhost_memory *m;
485 dev = get_device(vid);
489 size = dev->mem->nregions * sizeof(struct rte_vhost_mem_region);
490 m = malloc(sizeof(struct rte_vhost_memory) + size);
494 m->nregions = dev->mem->nregions;
495 memcpy(m->regions, dev->mem->regions, size);
502 rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
503 struct rte_vhost_vring *vring)
505 struct virtio_net *dev;
506 struct vhost_virtqueue *vq;
508 dev = get_device(vid);
512 if (vring_idx >= VHOST_MAX_VRING)
515 vq = dev->virtqueue[vring_idx];
519 vring->desc = vq->desc;
520 vring->avail = vq->avail;
521 vring->used = vq->used;
522 vring->log_guest_addr = vq->log_guest_addr;
524 vring->callfd = vq->callfd;
525 vring->kickfd = vq->kickfd;
526 vring->size = vq->size;
532 rte_vhost_vring_call(int vid, uint16_t vring_idx)
534 struct virtio_net *dev;
535 struct vhost_virtqueue *vq;
537 dev = get_device(vid);
541 if (vring_idx >= VHOST_MAX_VRING)
544 vq = dev->virtqueue[vring_idx];
548 vhost_vring_call(dev, vq);
553 rte_vhost_avail_entries(int vid, uint16_t queue_id)
555 struct virtio_net *dev;
556 struct vhost_virtqueue *vq;
558 dev = get_device(vid);
562 vq = dev->virtqueue[queue_id];
566 return *(volatile uint16_t *)&vq->avail->idx - vq->last_used_idx;
570 rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable)
572 struct virtio_net *dev = get_device(vid);
578 RTE_LOG(ERR, VHOST_CONFIG,
579 "guest notification isn't supported.\n");
583 dev->virtqueue[queue_id]->used->flags = VRING_USED_F_NO_NOTIFY;
588 rte_vhost_log_write(int vid, uint64_t addr, uint64_t len)
590 struct virtio_net *dev = get_device(vid);
595 vhost_log_write(dev, addr, len);
599 rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
600 uint64_t offset, uint64_t len)
602 struct virtio_net *dev;
603 struct vhost_virtqueue *vq;
605 dev = get_device(vid);
609 if (vring_idx >= VHOST_MAX_VRING)
611 vq = dev->virtqueue[vring_idx];
615 vhost_log_used_vring(dev, vq, offset, len);
619 rte_vhost_rx_queue_count(int vid, uint16_t qid)
621 struct virtio_net *dev;
622 struct vhost_virtqueue *vq;
624 dev = get_device(vid);
628 if (unlikely(qid >= dev->nr_vring || (qid & 1) == 0)) {
629 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
630 dev->vid, __func__, qid);
634 vq = dev->virtqueue[qid];
638 if (unlikely(vq->enabled == 0 || vq->avail == NULL))
641 return *((volatile uint16_t *)&vq->avail->idx) - vq->last_avail_idx;
644 int rte_vhost_get_vdpa_device_id(int vid)
646 struct virtio_net *dev = get_device(vid);
651 return dev->vdpa_dev_id;