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 vhost_destroy_device_notify(struct virtio_net *dev)
302 struct rte_vdpa_device *vdpa_dev;
305 if (dev->flags & VIRTIO_DEV_RUNNING) {
306 did = dev->vdpa_dev_id;
307 vdpa_dev = rte_vdpa_get_device(did);
308 if (vdpa_dev && vdpa_dev->ops->dev_close)
309 vdpa_dev->ops->dev_close(dev->vid);
310 dev->flags &= ~VIRTIO_DEV_RUNNING;
311 dev->notify_ops->destroy_device(dev->vid);
316 * Invoked when there is the vhost-user connection is broken (when
317 * the virtio device is being detached).
320 vhost_destroy_device(int vid)
322 struct virtio_net *dev = get_device(vid);
327 vhost_destroy_device_notify(dev);
329 cleanup_device(dev, 1);
332 vhost_devices[vid] = NULL;
336 vhost_attach_vdpa_device(int vid, int did)
338 struct virtio_net *dev = get_device(vid);
343 if (rte_vdpa_get_device(did) == NULL)
346 dev->vdpa_dev_id = did;
350 vhost_detach_vdpa_device(int vid)
352 struct virtio_net *dev = get_device(vid);
357 vhost_user_host_notifier_ctrl(vid, false);
359 dev->vdpa_dev_id = -1;
363 vhost_set_ifname(int vid, const char *if_name, unsigned int if_len)
365 struct virtio_net *dev;
368 dev = get_device(vid);
372 len = if_len > sizeof(dev->ifname) ?
373 sizeof(dev->ifname) : if_len;
375 strncpy(dev->ifname, if_name, len);
376 dev->ifname[sizeof(dev->ifname) - 1] = '\0';
380 vhost_enable_dequeue_zero_copy(int vid)
382 struct virtio_net *dev = get_device(vid);
387 dev->dequeue_zero_copy = 1;
391 vhost_set_builtin_virtio_net(int vid, bool enable)
393 struct virtio_net *dev = get_device(vid);
399 dev->flags |= VIRTIO_DEV_BUILTIN_VIRTIO_NET;
401 dev->flags &= ~VIRTIO_DEV_BUILTIN_VIRTIO_NET;
405 rte_vhost_get_mtu(int vid, uint16_t *mtu)
407 struct virtio_net *dev = get_device(vid);
412 if (!(dev->flags & VIRTIO_DEV_READY))
415 if (!(dev->features & (1ULL << VIRTIO_NET_F_MTU)))
424 rte_vhost_get_numa_node(int vid)
426 #ifdef RTE_LIBRTE_VHOST_NUMA
427 struct virtio_net *dev = get_device(vid);
434 ret = get_mempolicy(&numa_node, NULL, 0, dev,
435 MPOL_F_NODE | MPOL_F_ADDR);
437 RTE_LOG(ERR, VHOST_CONFIG,
438 "(%d) failed to query numa node: %s\n",
439 vid, rte_strerror(errno));
451 rte_vhost_get_queue_num(int vid)
453 struct virtio_net *dev = get_device(vid);
458 return dev->nr_vring / 2;
462 rte_vhost_get_vring_num(int vid)
464 struct virtio_net *dev = get_device(vid);
469 return dev->nr_vring;
473 rte_vhost_get_ifname(int vid, char *buf, size_t len)
475 struct virtio_net *dev = get_device(vid);
480 len = RTE_MIN(len, sizeof(dev->ifname));
482 strncpy(buf, dev->ifname, len);
489 rte_vhost_get_negotiated_features(int vid, uint64_t *features)
491 struct virtio_net *dev;
493 dev = get_device(vid);
497 *features = dev->features;
502 rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem)
504 struct virtio_net *dev;
505 struct rte_vhost_memory *m;
508 dev = get_device(vid);
512 size = dev->mem->nregions * sizeof(struct rte_vhost_mem_region);
513 m = malloc(sizeof(struct rte_vhost_memory) + size);
517 m->nregions = dev->mem->nregions;
518 memcpy(m->regions, dev->mem->regions, size);
525 rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
526 struct rte_vhost_vring *vring)
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 vring->desc = vq->desc;
543 vring->avail = vq->avail;
544 vring->used = vq->used;
545 vring->log_guest_addr = vq->log_guest_addr;
547 vring->callfd = vq->callfd;
548 vring->kickfd = vq->kickfd;
549 vring->size = vq->size;
555 rte_vhost_vring_call(int vid, uint16_t vring_idx)
557 struct virtio_net *dev;
558 struct vhost_virtqueue *vq;
560 dev = get_device(vid);
564 if (vring_idx >= VHOST_MAX_VRING)
567 vq = dev->virtqueue[vring_idx];
571 vhost_vring_call(dev, vq);
576 rte_vhost_avail_entries(int vid, uint16_t queue_id)
578 struct virtio_net *dev;
579 struct vhost_virtqueue *vq;
581 dev = get_device(vid);
585 vq = dev->virtqueue[queue_id];
589 return *(volatile uint16_t *)&vq->avail->idx - vq->last_used_idx;
593 rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable)
595 struct virtio_net *dev = get_device(vid);
601 dev->virtqueue[queue_id]->used->flags &=
602 ~VRING_USED_F_NO_NOTIFY;
604 dev->virtqueue[queue_id]->used->flags |= VRING_USED_F_NO_NOTIFY;
609 rte_vhost_log_write(int vid, uint64_t addr, uint64_t len)
611 struct virtio_net *dev = get_device(vid);
616 vhost_log_write(dev, addr, len);
620 rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
621 uint64_t offset, uint64_t len)
623 struct virtio_net *dev;
624 struct vhost_virtqueue *vq;
626 dev = get_device(vid);
630 if (vring_idx >= VHOST_MAX_VRING)
632 vq = dev->virtqueue[vring_idx];
636 vhost_log_used_vring(dev, vq, offset, len);
640 rte_vhost_rx_queue_count(int vid, uint16_t qid)
642 struct virtio_net *dev;
643 struct vhost_virtqueue *vq;
645 dev = get_device(vid);
649 if (unlikely(qid >= dev->nr_vring || (qid & 1) == 0)) {
650 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
651 dev->vid, __func__, qid);
655 vq = dev->virtqueue[qid];
659 if (unlikely(vq->enabled == 0 || vq->avail == NULL))
662 return *((volatile uint16_t *)&vq->avail->idx) - vq->last_avail_idx;
665 int rte_vhost_get_vdpa_device_id(int vid)
667 struct virtio_net *dev = get_device(vid);
672 return dev->vdpa_dev_id;
675 int rte_vhost_get_log_base(int vid, uint64_t *log_base,
678 struct virtio_net *dev = get_device(vid);
683 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
684 RTE_LOG(ERR, VHOST_DATA,
685 "(%d) %s: built-in vhost net backend is disabled.\n",
690 *log_base = dev->log_base;
691 *log_size = dev->log_size;
696 int rte_vhost_get_vring_base(int vid, uint16_t queue_id,
697 uint16_t *last_avail_idx, uint16_t *last_used_idx)
699 struct virtio_net *dev = get_device(vid);
704 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
705 RTE_LOG(ERR, VHOST_DATA,
706 "(%d) %s: built-in vhost net backend is disabled.\n",
711 *last_avail_idx = dev->virtqueue[queue_id]->last_avail_idx;
712 *last_used_idx = dev->virtqueue[queue_id]->last_used_idx;
717 int rte_vhost_set_vring_base(int vid, uint16_t queue_id,
718 uint16_t last_avail_idx, uint16_t last_used_idx)
720 struct virtio_net *dev = get_device(vid);
725 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
726 RTE_LOG(ERR, VHOST_DATA,
727 "(%d) %s: built-in vhost net backend is disabled.\n",
732 dev->virtqueue[queue_id]->last_avail_idx = last_avail_idx;
733 dev->virtqueue[queue_id]->last_used_idx = last_used_idx;