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);
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);
74 struct virtio_net *dev = vhost_devices[vid];
77 RTE_LOG(ERR, VHOST_CONFIG,
78 "(%d) device not found.\n", vid);
85 cleanup_vq(struct vhost_virtqueue *vq, int destroy)
87 if ((vq->callfd >= 0) && (destroy != 0))
94 * Unmap any memory, close any file descriptors and
95 * free any memory owned by a device.
98 cleanup_device(struct virtio_net *dev, int destroy)
102 vhost_backend_cleanup(dev);
104 for (i = 0; i < dev->nr_vring; i++)
105 cleanup_vq(dev->virtqueue[i], destroy);
109 free_vq(struct vhost_virtqueue *vq)
111 rte_free(vq->shadow_used_ring);
112 rte_free(vq->batch_copy_elems);
113 rte_mempool_free(vq->iotlb_pool);
118 * Release virtqueues and device memory.
121 free_device(struct virtio_net *dev)
125 for (i = 0; i < dev->nr_vring; i++)
126 free_vq(dev->virtqueue[i]);
132 vring_translate(struct virtio_net *dev, struct vhost_virtqueue *vq)
134 uint64_t req_size, size;
136 if (!(dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)))
139 req_size = sizeof(struct vring_desc) * vq->size;
141 vq->desc = (struct vring_desc *)(uintptr_t)vhost_iova_to_vva(dev, vq,
142 vq->ring_addrs.desc_user_addr,
143 &size, VHOST_ACCESS_RW);
144 if (!vq->desc || size != req_size)
147 req_size = sizeof(struct vring_avail);
148 req_size += sizeof(uint16_t) * vq->size;
149 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))
150 req_size += sizeof(uint16_t);
152 vq->avail = (struct vring_avail *)(uintptr_t)vhost_iova_to_vva(dev, vq,
153 vq->ring_addrs.avail_user_addr,
154 &size, VHOST_ACCESS_RW);
155 if (!vq->avail || size != req_size)
158 req_size = sizeof(struct vring_used);
159 req_size += sizeof(struct vring_used_elem) * vq->size;
160 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))
161 req_size += sizeof(uint16_t);
163 vq->used = (struct vring_used *)(uintptr_t)vhost_iova_to_vva(dev, vq,
164 vq->ring_addrs.used_user_addr,
165 &size, VHOST_ACCESS_RW);
166 if (!vq->used || size != req_size)
176 vring_invalidate(struct virtio_net *dev, struct vhost_virtqueue *vq)
178 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
179 vhost_user_iotlb_wr_lock(vq);
186 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
187 vhost_user_iotlb_wr_unlock(vq);
191 init_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
193 struct vhost_virtqueue *vq;
195 if (vring_idx >= VHOST_MAX_VRING) {
196 RTE_LOG(ERR, VHOST_CONFIG,
197 "Failed not init vring, out of bound (%d)\n",
202 vq = dev->virtqueue[vring_idx];
204 memset(vq, 0, sizeof(struct vhost_virtqueue));
206 vq->kickfd = VIRTIO_UNINITIALIZED_EVENTFD;
207 vq->callfd = VIRTIO_UNINITIALIZED_EVENTFD;
209 vhost_user_iotlb_init(dev, vring_idx);
210 /* Backends are set to -1 indicating an inactive device. */
213 TAILQ_INIT(&vq->zmbuf_list);
217 reset_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
219 struct vhost_virtqueue *vq;
222 if (vring_idx >= VHOST_MAX_VRING) {
223 RTE_LOG(ERR, VHOST_CONFIG,
224 "Failed not init vring, out of bound (%d)\n",
229 vq = dev->virtqueue[vring_idx];
231 init_vring_queue(dev, vring_idx);
236 alloc_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
238 struct vhost_virtqueue *vq;
240 vq = rte_malloc(NULL, sizeof(struct vhost_virtqueue), 0);
242 RTE_LOG(ERR, VHOST_CONFIG,
243 "Failed to allocate memory for vring:%u.\n", vring_idx);
247 dev->virtqueue[vring_idx] = vq;
248 init_vring_queue(dev, vring_idx);
249 rte_spinlock_init(&vq->access_lock);
257 * Reset some variables in device structure, while keeping few
258 * others untouched, such as vid, ifname, nr_vring: they
259 * should be same unless the device is removed.
262 reset_device(struct virtio_net *dev)
267 dev->protocol_features = 0;
268 dev->flags &= VIRTIO_DEV_BUILTIN_VIRTIO_NET;
270 for (i = 0; i < dev->nr_vring; i++)
271 reset_vring_queue(dev, i);
275 * Invoked when there is a new vhost-user connection established (when
276 * there is a new virtio device being attached).
279 vhost_new_device(void)
281 struct virtio_net *dev;
284 dev = rte_zmalloc(NULL, sizeof(struct virtio_net), 0);
286 RTE_LOG(ERR, VHOST_CONFIG,
287 "Failed to allocate memory for new dev.\n");
291 for (i = 0; i < MAX_VHOST_DEVICE; i++) {
292 if (vhost_devices[i] == NULL)
295 if (i == MAX_VHOST_DEVICE) {
296 RTE_LOG(ERR, VHOST_CONFIG,
297 "Failed to find a free slot for new device.\n");
302 vhost_devices[i] = dev;
304 dev->flags = VIRTIO_DEV_BUILTIN_VIRTIO_NET;
305 dev->slave_req_fd = -1;
311 * Invoked when there is the vhost-user connection is broken (when
312 * the virtio device is being detached).
315 vhost_destroy_device(int vid)
317 struct virtio_net *dev = get_device(vid);
322 if (dev->flags & VIRTIO_DEV_RUNNING) {
323 dev->flags &= ~VIRTIO_DEV_RUNNING;
324 dev->notify_ops->destroy_device(vid);
327 cleanup_device(dev, 1);
330 vhost_devices[vid] = NULL;
334 vhost_set_ifname(int vid, const char *if_name, unsigned int if_len)
336 struct virtio_net *dev;
339 dev = get_device(vid);
343 len = if_len > sizeof(dev->ifname) ?
344 sizeof(dev->ifname) : if_len;
346 strncpy(dev->ifname, if_name, len);
347 dev->ifname[sizeof(dev->ifname) - 1] = '\0';
351 vhost_enable_dequeue_zero_copy(int vid)
353 struct virtio_net *dev = get_device(vid);
358 dev->dequeue_zero_copy = 1;
362 vhost_set_builtin_virtio_net(int vid, bool enable)
364 struct virtio_net *dev = get_device(vid);
370 dev->flags |= VIRTIO_DEV_BUILTIN_VIRTIO_NET;
372 dev->flags &= ~VIRTIO_DEV_BUILTIN_VIRTIO_NET;
376 rte_vhost_get_mtu(int vid, uint16_t *mtu)
378 struct virtio_net *dev = get_device(vid);
383 if (!(dev->flags & VIRTIO_DEV_READY))
386 if (!(dev->features & (1ULL << VIRTIO_NET_F_MTU)))
395 rte_vhost_get_numa_node(int vid)
397 #ifdef RTE_LIBRTE_VHOST_NUMA
398 struct virtio_net *dev = get_device(vid);
405 ret = get_mempolicy(&numa_node, NULL, 0, dev,
406 MPOL_F_NODE | MPOL_F_ADDR);
408 RTE_LOG(ERR, VHOST_CONFIG,
409 "(%d) failed to query numa node: %s\n",
410 vid, rte_strerror(errno));
422 rte_vhost_get_queue_num(int vid)
424 struct virtio_net *dev = get_device(vid);
429 return dev->nr_vring / 2;
433 rte_vhost_get_vring_num(int vid)
435 struct virtio_net *dev = get_device(vid);
440 return dev->nr_vring;
444 rte_vhost_get_ifname(int vid, char *buf, size_t len)
446 struct virtio_net *dev = get_device(vid);
451 len = RTE_MIN(len, sizeof(dev->ifname));
453 strncpy(buf, dev->ifname, len);
460 rte_vhost_get_negotiated_features(int vid, uint64_t *features)
462 struct virtio_net *dev;
464 dev = get_device(vid);
468 *features = dev->features;
473 rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem)
475 struct virtio_net *dev;
476 struct rte_vhost_memory *m;
479 dev = get_device(vid);
483 size = dev->mem->nregions * sizeof(struct rte_vhost_mem_region);
484 m = malloc(sizeof(struct rte_vhost_memory) + size);
488 m->nregions = dev->mem->nregions;
489 memcpy(m->regions, dev->mem->regions, size);
496 rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
497 struct rte_vhost_vring *vring)
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 vring->desc = vq->desc;
514 vring->avail = vq->avail;
515 vring->used = vq->used;
516 vring->log_guest_addr = vq->log_guest_addr;
518 vring->callfd = vq->callfd;
519 vring->kickfd = vq->kickfd;
520 vring->size = vq->size;
526 rte_vhost_vring_call(int vid, uint16_t vring_idx)
528 struct virtio_net *dev;
529 struct vhost_virtqueue *vq;
531 dev = get_device(vid);
535 if (vring_idx >= VHOST_MAX_VRING)
538 vq = dev->virtqueue[vring_idx];
542 vhost_vring_call(dev, vq);
547 rte_vhost_avail_entries(int vid, uint16_t queue_id)
549 struct virtio_net *dev;
550 struct vhost_virtqueue *vq;
552 dev = get_device(vid);
556 vq = dev->virtqueue[queue_id];
560 return *(volatile uint16_t *)&vq->avail->idx - vq->last_used_idx;
564 rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable)
566 struct virtio_net *dev = get_device(vid);
572 RTE_LOG(ERR, VHOST_CONFIG,
573 "guest notification isn't supported.\n");
577 dev->virtqueue[queue_id]->used->flags = VRING_USED_F_NO_NOTIFY;
582 rte_vhost_log_write(int vid, uint64_t addr, uint64_t len)
584 struct virtio_net *dev = get_device(vid);
589 vhost_log_write(dev, addr, len);
593 rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
594 uint64_t offset, uint64_t len)
596 struct virtio_net *dev;
597 struct vhost_virtqueue *vq;
599 dev = get_device(vid);
603 if (vring_idx >= VHOST_MAX_VRING)
605 vq = dev->virtqueue[vring_idx];
609 vhost_log_used_vring(dev, vq, offset, len);
613 rte_vhost_rx_queue_count(int vid, uint16_t qid)
615 struct virtio_net *dev;
616 struct vhost_virtqueue *vq;
618 dev = get_device(vid);
622 if (unlikely(qid >= dev->nr_vring || (qid & 1) == 0)) {
623 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
624 dev->vid, __func__, qid);
628 vq = dev->virtqueue[qid];
632 if (unlikely(vq->enabled == 0 || vq->avail == NULL))
635 return *((volatile uint16_t *)&vq->avail->idx) - vq->last_avail_idx;