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);
45 if (!vhost_user_iotlb_pending_miss(vq, iova + tmp_size, perm)) {
47 * iotlb_lock is read-locked for a full burst,
48 * but it only protects the iotlb cache.
49 * In case of IOTLB miss, we might block on the socket,
50 * which could cause a deadlock with QEMU if an IOTLB update
51 * is being handled. We can safely unlock here to avoid it.
53 vhost_user_iotlb_rd_unlock(vq);
55 vhost_user_iotlb_pending_insert(vq, iova + tmp_size, perm);
56 vhost_user_iotlb_miss(dev, iova + tmp_size, perm);
58 vhost_user_iotlb_rd_lock(vq);
67 struct virtio_net *dev = vhost_devices[vid];
70 RTE_LOG(ERR, VHOST_CONFIG,
71 "(%d) device not found.\n", vid);
78 cleanup_vq(struct vhost_virtqueue *vq, int destroy)
80 if ((vq->callfd >= 0) && (destroy != 0))
87 * Unmap any memory, close any file descriptors and
88 * free any memory owned by a device.
91 cleanup_device(struct virtio_net *dev, int destroy)
95 vhost_backend_cleanup(dev);
97 for (i = 0; i < dev->nr_vring; i++)
98 cleanup_vq(dev->virtqueue[i], destroy);
102 free_vq(struct vhost_virtqueue *vq)
104 rte_free(vq->shadow_used_ring);
105 rte_free(vq->batch_copy_elems);
106 rte_mempool_free(vq->iotlb_pool);
111 * Release virtqueues and device memory.
114 free_device(struct virtio_net *dev)
118 for (i = 0; i < dev->nr_vring; i++)
119 free_vq(dev->virtqueue[i]);
125 vring_translate(struct virtio_net *dev, struct vhost_virtqueue *vq)
129 if (!(dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)))
132 size = sizeof(struct vring_desc) * vq->size;
133 vq->desc = (struct vring_desc *)(uintptr_t)vhost_iova_to_vva(dev, vq,
134 vq->ring_addrs.desc_user_addr,
135 size, VHOST_ACCESS_RW);
139 size = sizeof(struct vring_avail);
140 size += sizeof(uint16_t) * vq->size;
141 vq->avail = (struct vring_avail *)(uintptr_t)vhost_iova_to_vva(dev, vq,
142 vq->ring_addrs.avail_user_addr,
143 size, VHOST_ACCESS_RW);
147 size = sizeof(struct vring_used);
148 size += sizeof(struct vring_used_elem) * vq->size;
149 vq->used = (struct vring_used *)(uintptr_t)vhost_iova_to_vva(dev, vq,
150 vq->ring_addrs.used_user_addr,
151 size, VHOST_ACCESS_RW);
162 vring_invalidate(struct virtio_net *dev, struct vhost_virtqueue *vq)
164 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
165 vhost_user_iotlb_wr_lock(vq);
172 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
173 vhost_user_iotlb_wr_unlock(vq);
177 init_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
179 struct vhost_virtqueue *vq;
181 if (vring_idx >= VHOST_MAX_VRING) {
182 RTE_LOG(ERR, VHOST_CONFIG,
183 "Failed not init vring, out of bound (%d)\n",
188 vq = dev->virtqueue[vring_idx];
190 memset(vq, 0, sizeof(struct vhost_virtqueue));
192 vq->kickfd = VIRTIO_UNINITIALIZED_EVENTFD;
193 vq->callfd = VIRTIO_UNINITIALIZED_EVENTFD;
195 vhost_user_iotlb_init(dev, vring_idx);
196 /* Backends are set to -1 indicating an inactive device. */
199 TAILQ_INIT(&vq->zmbuf_list);
203 reset_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
205 struct vhost_virtqueue *vq;
208 if (vring_idx >= VHOST_MAX_VRING) {
209 RTE_LOG(ERR, VHOST_CONFIG,
210 "Failed not init vring, out of bound (%d)\n",
215 vq = dev->virtqueue[vring_idx];
217 init_vring_queue(dev, vring_idx);
222 alloc_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
224 struct vhost_virtqueue *vq;
226 vq = rte_malloc(NULL, sizeof(struct vhost_virtqueue), 0);
228 RTE_LOG(ERR, VHOST_CONFIG,
229 "Failed to allocate memory for vring:%u.\n", vring_idx);
233 dev->virtqueue[vring_idx] = vq;
234 init_vring_queue(dev, vring_idx);
235 rte_spinlock_init(&vq->access_lock);
243 * Reset some variables in device structure, while keeping few
244 * others untouched, such as vid, ifname, nr_vring: they
245 * should be same unless the device is removed.
248 reset_device(struct virtio_net *dev)
253 dev->protocol_features = 0;
256 for (i = 0; i < dev->nr_vring; i++)
257 reset_vring_queue(dev, i);
261 * Invoked when there is a new vhost-user connection established (when
262 * there is a new virtio device being attached).
265 vhost_new_device(void)
267 struct virtio_net *dev;
270 dev = rte_zmalloc(NULL, sizeof(struct virtio_net), 0);
272 RTE_LOG(ERR, VHOST_CONFIG,
273 "Failed to allocate memory for new dev.\n");
277 for (i = 0; i < MAX_VHOST_DEVICE; i++) {
278 if (vhost_devices[i] == NULL)
281 if (i == MAX_VHOST_DEVICE) {
282 RTE_LOG(ERR, VHOST_CONFIG,
283 "Failed to find a free slot for new device.\n");
288 vhost_devices[i] = dev;
290 dev->slave_req_fd = -1;
296 * Invoked when there is the vhost-user connection is broken (when
297 * the virtio device is being detached).
300 vhost_destroy_device(int vid)
302 struct virtio_net *dev = get_device(vid);
307 if (dev->flags & VIRTIO_DEV_RUNNING) {
308 dev->flags &= ~VIRTIO_DEV_RUNNING;
309 dev->notify_ops->destroy_device(vid);
312 cleanup_device(dev, 1);
315 vhost_devices[vid] = NULL;
319 vhost_set_ifname(int vid, const char *if_name, unsigned int if_len)
321 struct virtio_net *dev;
324 dev = get_device(vid);
328 len = if_len > sizeof(dev->ifname) ?
329 sizeof(dev->ifname) : if_len;
331 strncpy(dev->ifname, if_name, len);
332 dev->ifname[sizeof(dev->ifname) - 1] = '\0';
336 vhost_enable_dequeue_zero_copy(int vid)
338 struct virtio_net *dev = get_device(vid);
343 dev->dequeue_zero_copy = 1;
347 rte_vhost_get_mtu(int vid, uint16_t *mtu)
349 struct virtio_net *dev = get_device(vid);
354 if (!(dev->flags & VIRTIO_DEV_READY))
357 if (!(dev->features & (1ULL << VIRTIO_NET_F_MTU)))
366 rte_vhost_get_numa_node(int vid)
368 #ifdef RTE_LIBRTE_VHOST_NUMA
369 struct virtio_net *dev = get_device(vid);
376 ret = get_mempolicy(&numa_node, NULL, 0, dev,
377 MPOL_F_NODE | MPOL_F_ADDR);
379 RTE_LOG(ERR, VHOST_CONFIG,
380 "(%d) failed to query numa node: %s\n",
381 vid, rte_strerror(errno));
393 rte_vhost_get_queue_num(int vid)
395 struct virtio_net *dev = get_device(vid);
400 return dev->nr_vring / 2;
404 rte_vhost_get_vring_num(int vid)
406 struct virtio_net *dev = get_device(vid);
411 return dev->nr_vring;
415 rte_vhost_get_ifname(int vid, char *buf, size_t len)
417 struct virtio_net *dev = get_device(vid);
422 len = RTE_MIN(len, sizeof(dev->ifname));
424 strncpy(buf, dev->ifname, len);
431 rte_vhost_get_negotiated_features(int vid, uint64_t *features)
433 struct virtio_net *dev;
435 dev = get_device(vid);
439 *features = dev->features;
444 rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem)
446 struct virtio_net *dev;
447 struct rte_vhost_memory *m;
450 dev = get_device(vid);
454 size = dev->mem->nregions * sizeof(struct rte_vhost_mem_region);
455 m = malloc(sizeof(struct rte_vhost_memory) + size);
459 m->nregions = dev->mem->nregions;
460 memcpy(m->regions, dev->mem->regions, size);
467 rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
468 struct rte_vhost_vring *vring)
470 struct virtio_net *dev;
471 struct vhost_virtqueue *vq;
473 dev = get_device(vid);
477 if (vring_idx >= VHOST_MAX_VRING)
480 vq = dev->virtqueue[vring_idx];
484 vring->desc = vq->desc;
485 vring->avail = vq->avail;
486 vring->used = vq->used;
487 vring->log_guest_addr = vq->log_guest_addr;
489 vring->callfd = vq->callfd;
490 vring->kickfd = vq->kickfd;
491 vring->size = vq->size;
497 rte_vhost_vring_call(int vid, uint16_t vring_idx)
499 struct virtio_net *dev;
500 struct vhost_virtqueue *vq;
502 dev = get_device(vid);
506 if (vring_idx >= VHOST_MAX_VRING)
509 vq = dev->virtqueue[vring_idx];
513 vhost_vring_call(dev, vq);
518 rte_vhost_avail_entries(int vid, uint16_t queue_id)
520 struct virtio_net *dev;
521 struct vhost_virtqueue *vq;
523 dev = get_device(vid);
527 vq = dev->virtqueue[queue_id];
531 return *(volatile uint16_t *)&vq->avail->idx - vq->last_used_idx;
535 rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable)
537 struct virtio_net *dev = get_device(vid);
543 RTE_LOG(ERR, VHOST_CONFIG,
544 "guest notification isn't supported.\n");
548 dev->virtqueue[queue_id]->used->flags = VRING_USED_F_NO_NOTIFY;
553 rte_vhost_log_write(int vid, uint64_t addr, uint64_t len)
555 struct virtio_net *dev = get_device(vid);
560 vhost_log_write(dev, addr, len);
564 rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
565 uint64_t offset, uint64_t len)
567 struct virtio_net *dev;
568 struct vhost_virtqueue *vq;
570 dev = get_device(vid);
574 if (vring_idx >= VHOST_MAX_VRING)
576 vq = dev->virtqueue[vring_idx];
580 vhost_log_used_vring(dev, vq, offset, len);
584 rte_vhost_rx_queue_count(int vid, uint16_t qid)
586 struct virtio_net *dev;
587 struct vhost_virtqueue *vq;
589 dev = get_device(vid);
593 if (unlikely(qid >= dev->nr_vring || (qid & 1) == 0)) {
594 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
595 dev->vid, __func__, qid);
599 vq = dev->virtqueue[qid];
603 if (unlikely(vq->enabled == 0 || vq->avail == NULL))
606 return *((volatile uint16_t *)&vq->avail->idx) - vq->last_avail_idx;