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 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)
121 uint64_t req_size, size;
123 if (!(dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)))
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)
163 vring_invalidate(struct virtio_net *dev, struct vhost_virtqueue *vq)
165 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
166 vhost_user_iotlb_wr_lock(vq);
173 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
174 vhost_user_iotlb_wr_unlock(vq);
178 init_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
180 struct vhost_virtqueue *vq;
182 if (vring_idx >= VHOST_MAX_VRING) {
183 RTE_LOG(ERR, VHOST_CONFIG,
184 "Failed not init vring, out of bound (%d)\n",
189 vq = dev->virtqueue[vring_idx];
191 memset(vq, 0, sizeof(struct vhost_virtqueue));
193 vq->kickfd = VIRTIO_UNINITIALIZED_EVENTFD;
194 vq->callfd = VIRTIO_UNINITIALIZED_EVENTFD;
196 vhost_user_iotlb_init(dev, vring_idx);
197 /* Backends are set to -1 indicating an inactive device. */
200 TAILQ_INIT(&vq->zmbuf_list);
204 reset_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
206 struct vhost_virtqueue *vq;
209 if (vring_idx >= VHOST_MAX_VRING) {
210 RTE_LOG(ERR, VHOST_CONFIG,
211 "Failed not init vring, out of bound (%d)\n",
216 vq = dev->virtqueue[vring_idx];
218 init_vring_queue(dev, vring_idx);
223 alloc_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
225 struct vhost_virtqueue *vq;
227 vq = rte_malloc(NULL, sizeof(struct vhost_virtqueue), 0);
229 RTE_LOG(ERR, VHOST_CONFIG,
230 "Failed to allocate memory for vring:%u.\n", vring_idx);
234 dev->virtqueue[vring_idx] = vq;
235 init_vring_queue(dev, vring_idx);
236 rte_spinlock_init(&vq->access_lock);
244 * Reset some variables in device structure, while keeping few
245 * others untouched, such as vid, ifname, nr_vring: they
246 * should be same unless the device is removed.
249 reset_device(struct virtio_net *dev)
254 dev->protocol_features = 0;
255 dev->flags &= VIRTIO_DEV_BUILTIN_VIRTIO_NET;
257 for (i = 0; i < dev->nr_vring; i++)
258 reset_vring_queue(dev, i);
262 * Invoked when there is a new vhost-user connection established (when
263 * there is a new virtio device being attached).
266 vhost_new_device(void)
268 struct virtio_net *dev;
271 dev = rte_zmalloc(NULL, sizeof(struct virtio_net), 0);
273 RTE_LOG(ERR, VHOST_CONFIG,
274 "Failed to allocate memory for new dev.\n");
278 for (i = 0; i < MAX_VHOST_DEVICE; i++) {
279 if (vhost_devices[i] == NULL)
282 if (i == MAX_VHOST_DEVICE) {
283 RTE_LOG(ERR, VHOST_CONFIG,
284 "Failed to find a free slot for new device.\n");
289 vhost_devices[i] = dev;
291 dev->flags = VIRTIO_DEV_BUILTIN_VIRTIO_NET;
292 dev->slave_req_fd = -1;
293 dev->vdpa_dev_id = -1;
299 * Invoked when there is the vhost-user connection is broken (when
300 * the virtio device is being detached).
303 vhost_destroy_device(int vid)
305 struct virtio_net *dev = get_device(vid);
306 struct rte_vdpa_device *vdpa_dev;
312 if (dev->flags & VIRTIO_DEV_RUNNING) {
313 did = dev->vdpa_dev_id;
314 vdpa_dev = rte_vdpa_get_device(did);
315 if (vdpa_dev && vdpa_dev->ops->dev_close)
316 vdpa_dev->ops->dev_close(dev->vid);
317 dev->flags &= ~VIRTIO_DEV_RUNNING;
318 dev->notify_ops->destroy_device(vid);
321 cleanup_device(dev, 1);
324 vhost_devices[vid] = NULL;
328 vhost_attach_vdpa_device(int vid, int did)
330 struct virtio_net *dev = get_device(vid);
335 if (rte_vdpa_get_device(did) == NULL)
338 dev->vdpa_dev_id = did;
342 vhost_detach_vdpa_device(int vid)
344 struct virtio_net *dev = get_device(vid);
349 dev->vdpa_dev_id = -1;
353 vhost_set_ifname(int vid, const char *if_name, unsigned int if_len)
355 struct virtio_net *dev;
358 dev = get_device(vid);
362 len = if_len > sizeof(dev->ifname) ?
363 sizeof(dev->ifname) : if_len;
365 strncpy(dev->ifname, if_name, len);
366 dev->ifname[sizeof(dev->ifname) - 1] = '\0';
370 vhost_enable_dequeue_zero_copy(int vid)
372 struct virtio_net *dev = get_device(vid);
377 dev->dequeue_zero_copy = 1;
381 vhost_set_builtin_virtio_net(int vid, bool enable)
383 struct virtio_net *dev = get_device(vid);
389 dev->flags |= VIRTIO_DEV_BUILTIN_VIRTIO_NET;
391 dev->flags &= ~VIRTIO_DEV_BUILTIN_VIRTIO_NET;
395 rte_vhost_get_mtu(int vid, uint16_t *mtu)
397 struct virtio_net *dev = get_device(vid);
402 if (!(dev->flags & VIRTIO_DEV_READY))
405 if (!(dev->features & (1ULL << VIRTIO_NET_F_MTU)))
414 rte_vhost_get_numa_node(int vid)
416 #ifdef RTE_LIBRTE_VHOST_NUMA
417 struct virtio_net *dev = get_device(vid);
424 ret = get_mempolicy(&numa_node, NULL, 0, dev,
425 MPOL_F_NODE | MPOL_F_ADDR);
427 RTE_LOG(ERR, VHOST_CONFIG,
428 "(%d) failed to query numa node: %s\n",
429 vid, rte_strerror(errno));
441 rte_vhost_get_queue_num(int vid)
443 struct virtio_net *dev = get_device(vid);
448 return dev->nr_vring / 2;
452 rte_vhost_get_vring_num(int vid)
454 struct virtio_net *dev = get_device(vid);
459 return dev->nr_vring;
463 rte_vhost_get_ifname(int vid, char *buf, size_t len)
465 struct virtio_net *dev = get_device(vid);
470 len = RTE_MIN(len, sizeof(dev->ifname));
472 strncpy(buf, dev->ifname, len);
479 rte_vhost_get_negotiated_features(int vid, uint64_t *features)
481 struct virtio_net *dev;
483 dev = get_device(vid);
487 *features = dev->features;
492 rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem)
494 struct virtio_net *dev;
495 struct rte_vhost_memory *m;
498 dev = get_device(vid);
502 size = dev->mem->nregions * sizeof(struct rte_vhost_mem_region);
503 m = malloc(sizeof(struct rte_vhost_memory) + size);
507 m->nregions = dev->mem->nregions;
508 memcpy(m->regions, dev->mem->regions, size);
515 rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
516 struct rte_vhost_vring *vring)
518 struct virtio_net *dev;
519 struct vhost_virtqueue *vq;
521 dev = get_device(vid);
525 if (vring_idx >= VHOST_MAX_VRING)
528 vq = dev->virtqueue[vring_idx];
532 vring->desc = vq->desc;
533 vring->avail = vq->avail;
534 vring->used = vq->used;
535 vring->log_guest_addr = vq->log_guest_addr;
537 vring->callfd = vq->callfd;
538 vring->kickfd = vq->kickfd;
539 vring->size = vq->size;
545 rte_vhost_vring_call(int vid, uint16_t vring_idx)
547 struct virtio_net *dev;
548 struct vhost_virtqueue *vq;
550 dev = get_device(vid);
554 if (vring_idx >= VHOST_MAX_VRING)
557 vq = dev->virtqueue[vring_idx];
561 vhost_vring_call(dev, vq);
566 rte_vhost_avail_entries(int vid, uint16_t queue_id)
568 struct virtio_net *dev;
569 struct vhost_virtqueue *vq;
571 dev = get_device(vid);
575 vq = dev->virtqueue[queue_id];
579 return *(volatile uint16_t *)&vq->avail->idx - vq->last_used_idx;
583 rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable)
585 struct virtio_net *dev = get_device(vid);
591 dev->virtqueue[queue_id]->used->flags &=
592 ~VRING_USED_F_NO_NOTIFY;
594 dev->virtqueue[queue_id]->used->flags |= VRING_USED_F_NO_NOTIFY;
599 rte_vhost_log_write(int vid, uint64_t addr, uint64_t len)
601 struct virtio_net *dev = get_device(vid);
606 vhost_log_write(dev, addr, len);
610 rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
611 uint64_t offset, uint64_t len)
613 struct virtio_net *dev;
614 struct vhost_virtqueue *vq;
616 dev = get_device(vid);
620 if (vring_idx >= VHOST_MAX_VRING)
622 vq = dev->virtqueue[vring_idx];
626 vhost_log_used_vring(dev, vq, offset, len);
630 rte_vhost_rx_queue_count(int vid, uint16_t qid)
632 struct virtio_net *dev;
633 struct vhost_virtqueue *vq;
635 dev = get_device(vid);
639 if (unlikely(qid >= dev->nr_vring || (qid & 1) == 0)) {
640 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
641 dev->vid, __func__, qid);
645 vq = dev->virtqueue[qid];
649 if (unlikely(vq->enabled == 0 || vq->avail == NULL))
652 return *((volatile uint16_t *)&vq->avail->idx) - vq->last_avail_idx;
655 int rte_vhost_get_vdpa_device_id(int vid)
657 struct virtio_net *dev = get_device(vid);
662 return dev->vdpa_dev_id;
665 int rte_vhost_get_log_base(int vid, uint64_t *log_base,
668 struct virtio_net *dev = get_device(vid);
673 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
674 RTE_LOG(ERR, VHOST_DATA,
675 "(%d) %s: built-in vhost net backend is disabled.\n",
680 *log_base = dev->log_base;
681 *log_size = dev->log_size;
686 int rte_vhost_get_vring_base(int vid, uint16_t queue_id,
687 uint16_t *last_avail_idx, uint16_t *last_used_idx)
689 struct virtio_net *dev = get_device(vid);
694 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
695 RTE_LOG(ERR, VHOST_DATA,
696 "(%d) %s: built-in vhost net backend is disabled.\n",
701 *last_avail_idx = dev->virtqueue[queue_id]->last_avail_idx;
702 *last_used_idx = dev->virtqueue[queue_id]->last_used_idx;
707 int rte_vhost_set_vring_base(int vid, uint16_t queue_id,
708 uint16_t last_avail_idx, uint16_t last_used_idx)
710 struct virtio_net *dev = get_device(vid);
715 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
716 RTE_LOG(ERR, VHOST_DATA,
717 "(%d) %s: built-in vhost net backend is disabled.\n",
722 dev->virtqueue[queue_id]->last_avail_idx = last_avail_idx;
723 dev->virtqueue[queue_id]->last_used_idx = last_used_idx;