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 for (i = 0; i < MAX_VHOST_DEVICE; i++) {
272 if (vhost_devices[i] == NULL)
276 if (i == MAX_VHOST_DEVICE) {
277 RTE_LOG(ERR, VHOST_CONFIG,
278 "Failed to find a free slot for new device.\n");
282 dev = rte_zmalloc(NULL, sizeof(struct virtio_net), 0);
284 RTE_LOG(ERR, VHOST_CONFIG,
285 "Failed to allocate memory for new dev.\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;
294 rte_spinlock_init(&dev->slave_req_lock);
300 * Invoked when there is the vhost-user connection is broken (when
301 * the virtio device is being detached).
304 vhost_destroy_device(int vid)
306 struct virtio_net *dev = get_device(vid);
307 struct rte_vdpa_device *vdpa_dev;
313 if (dev->flags & VIRTIO_DEV_RUNNING) {
314 did = dev->vdpa_dev_id;
315 vdpa_dev = rte_vdpa_get_device(did);
316 if (vdpa_dev && vdpa_dev->ops->dev_close)
317 vdpa_dev->ops->dev_close(dev->vid);
318 dev->flags &= ~VIRTIO_DEV_RUNNING;
319 dev->notify_ops->destroy_device(vid);
322 cleanup_device(dev, 1);
325 vhost_devices[vid] = NULL;
329 vhost_attach_vdpa_device(int vid, int did)
331 struct virtio_net *dev = get_device(vid);
336 if (rte_vdpa_get_device(did) == NULL)
339 dev->vdpa_dev_id = did;
343 vhost_detach_vdpa_device(int vid)
345 struct virtio_net *dev = get_device(vid);
350 vhost_user_host_notifier_ctrl(vid, false);
352 dev->vdpa_dev_id = -1;
356 vhost_set_ifname(int vid, const char *if_name, unsigned int if_len)
358 struct virtio_net *dev;
361 dev = get_device(vid);
365 len = if_len > sizeof(dev->ifname) ?
366 sizeof(dev->ifname) : if_len;
368 strncpy(dev->ifname, if_name, len);
369 dev->ifname[sizeof(dev->ifname) - 1] = '\0';
373 vhost_enable_dequeue_zero_copy(int vid)
375 struct virtio_net *dev = get_device(vid);
380 dev->dequeue_zero_copy = 1;
384 vhost_set_builtin_virtio_net(int vid, bool enable)
386 struct virtio_net *dev = get_device(vid);
392 dev->flags |= VIRTIO_DEV_BUILTIN_VIRTIO_NET;
394 dev->flags &= ~VIRTIO_DEV_BUILTIN_VIRTIO_NET;
398 rte_vhost_get_mtu(int vid, uint16_t *mtu)
400 struct virtio_net *dev = get_device(vid);
405 if (!(dev->flags & VIRTIO_DEV_READY))
408 if (!(dev->features & (1ULL << VIRTIO_NET_F_MTU)))
417 rte_vhost_get_numa_node(int vid)
419 #ifdef RTE_LIBRTE_VHOST_NUMA
420 struct virtio_net *dev = get_device(vid);
427 ret = get_mempolicy(&numa_node, NULL, 0, dev,
428 MPOL_F_NODE | MPOL_F_ADDR);
430 RTE_LOG(ERR, VHOST_CONFIG,
431 "(%d) failed to query numa node: %s\n",
432 vid, rte_strerror(errno));
444 rte_vhost_get_queue_num(int vid)
446 struct virtio_net *dev = get_device(vid);
451 return dev->nr_vring / 2;
455 rte_vhost_get_vring_num(int vid)
457 struct virtio_net *dev = get_device(vid);
462 return dev->nr_vring;
466 rte_vhost_get_ifname(int vid, char *buf, size_t len)
468 struct virtio_net *dev = get_device(vid);
473 len = RTE_MIN(len, sizeof(dev->ifname));
475 strncpy(buf, dev->ifname, len);
482 rte_vhost_get_negotiated_features(int vid, uint64_t *features)
484 struct virtio_net *dev;
486 dev = get_device(vid);
490 *features = dev->features;
495 rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem)
497 struct virtio_net *dev;
498 struct rte_vhost_memory *m;
501 dev = get_device(vid);
505 size = dev->mem->nregions * sizeof(struct rte_vhost_mem_region);
506 m = malloc(sizeof(struct rte_vhost_memory) + size);
510 m->nregions = dev->mem->nregions;
511 memcpy(m->regions, dev->mem->regions, size);
518 rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
519 struct rte_vhost_vring *vring)
521 struct virtio_net *dev;
522 struct vhost_virtqueue *vq;
524 dev = get_device(vid);
528 if (vring_idx >= VHOST_MAX_VRING)
531 vq = dev->virtqueue[vring_idx];
535 vring->desc = vq->desc;
536 vring->avail = vq->avail;
537 vring->used = vq->used;
538 vring->log_guest_addr = vq->log_guest_addr;
540 vring->callfd = vq->callfd;
541 vring->kickfd = vq->kickfd;
542 vring->size = vq->size;
548 rte_vhost_vring_call(int vid, uint16_t vring_idx)
550 struct virtio_net *dev;
551 struct vhost_virtqueue *vq;
553 dev = get_device(vid);
557 if (vring_idx >= VHOST_MAX_VRING)
560 vq = dev->virtqueue[vring_idx];
564 vhost_vring_call(dev, vq);
569 rte_vhost_avail_entries(int vid, uint16_t queue_id)
571 struct virtio_net *dev;
572 struct vhost_virtqueue *vq;
574 dev = get_device(vid);
578 vq = dev->virtqueue[queue_id];
582 return *(volatile uint16_t *)&vq->avail->idx - vq->last_used_idx;
586 rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable)
588 struct virtio_net *dev = get_device(vid);
594 dev->virtqueue[queue_id]->used->flags &=
595 ~VRING_USED_F_NO_NOTIFY;
597 dev->virtqueue[queue_id]->used->flags |= VRING_USED_F_NO_NOTIFY;
602 rte_vhost_log_write(int vid, uint64_t addr, uint64_t len)
604 struct virtio_net *dev = get_device(vid);
609 vhost_log_write(dev, addr, len);
613 rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
614 uint64_t offset, uint64_t len)
616 struct virtio_net *dev;
617 struct vhost_virtqueue *vq;
619 dev = get_device(vid);
623 if (vring_idx >= VHOST_MAX_VRING)
625 vq = dev->virtqueue[vring_idx];
629 vhost_log_used_vring(dev, vq, offset, len);
633 rte_vhost_rx_queue_count(int vid, uint16_t qid)
635 struct virtio_net *dev;
636 struct vhost_virtqueue *vq;
638 dev = get_device(vid);
642 if (unlikely(qid >= dev->nr_vring || (qid & 1) == 0)) {
643 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
644 dev->vid, __func__, qid);
648 vq = dev->virtqueue[qid];
652 if (unlikely(vq->enabled == 0 || vq->avail == NULL))
655 return *((volatile uint16_t *)&vq->avail->idx) - vq->last_avail_idx;
658 int rte_vhost_get_vdpa_device_id(int vid)
660 struct virtio_net *dev = get_device(vid);
665 return dev->vdpa_dev_id;
668 int rte_vhost_get_log_base(int vid, uint64_t *log_base,
671 struct virtio_net *dev = get_device(vid);
676 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
677 RTE_LOG(ERR, VHOST_DATA,
678 "(%d) %s: built-in vhost net backend is disabled.\n",
683 *log_base = dev->log_base;
684 *log_size = dev->log_size;
689 int rte_vhost_get_vring_base(int vid, uint16_t queue_id,
690 uint16_t *last_avail_idx, uint16_t *last_used_idx)
692 struct virtio_net *dev = get_device(vid);
697 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
698 RTE_LOG(ERR, VHOST_DATA,
699 "(%d) %s: built-in vhost net backend is disabled.\n",
704 *last_avail_idx = dev->virtqueue[queue_id]->last_avail_idx;
705 *last_used_idx = dev->virtqueue[queue_id]->last_used_idx;
710 int rte_vhost_set_vring_base(int vid, uint16_t queue_id,
711 uint16_t last_avail_idx, uint16_t last_used_idx)
713 struct virtio_net *dev = get_device(vid);
718 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
719 RTE_LOG(ERR, VHOST_DATA,
720 "(%d) %s: built-in vhost net backend is disabled.\n",
725 dev->virtqueue[queue_id]->last_avail_idx = last_avail_idx;
726 dev->virtqueue[queue_id]->last_used_idx = last_used_idx;