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 * Release virtqueues and device memory.
105 free_device(struct virtio_net *dev)
108 struct vhost_virtqueue *vq;
110 for (i = 0; i < dev->nr_vring; i++) {
111 vq = dev->virtqueue[i];
113 rte_free(vq->shadow_used_ring);
114 rte_free(vq->batch_copy_elems);
115 rte_mempool_free(vq->iotlb_pool);
123 vring_translate(struct virtio_net *dev, struct vhost_virtqueue *vq)
127 if (!(dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)))
130 size = sizeof(struct vring_desc) * vq->size;
131 vq->desc = (struct vring_desc *)(uintptr_t)vhost_iova_to_vva(dev, vq,
132 vq->ring_addrs.desc_user_addr,
133 size, VHOST_ACCESS_RW);
137 size = sizeof(struct vring_avail);
138 size += sizeof(uint16_t) * vq->size;
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);
145 size = sizeof(struct vring_used);
146 size += sizeof(struct vring_used_elem) * vq->size;
147 vq->used = (struct vring_used *)(uintptr_t)vhost_iova_to_vva(dev, vq,
148 vq->ring_addrs.used_user_addr,
149 size, VHOST_ACCESS_RW);
160 vring_invalidate(struct virtio_net *dev, struct vhost_virtqueue *vq)
162 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
163 vhost_user_iotlb_wr_lock(vq);
170 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
171 vhost_user_iotlb_wr_unlock(vq);
175 init_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
177 struct vhost_virtqueue *vq;
179 if (vring_idx >= VHOST_MAX_VRING) {
180 RTE_LOG(ERR, VHOST_CONFIG,
181 "Failed not init vring, out of bound (%d)\n",
186 vq = dev->virtqueue[vring_idx];
188 memset(vq, 0, sizeof(struct vhost_virtqueue));
190 vq->kickfd = VIRTIO_UNINITIALIZED_EVENTFD;
191 vq->callfd = VIRTIO_UNINITIALIZED_EVENTFD;
193 vhost_user_iotlb_init(dev, vring_idx);
194 /* Backends are set to -1 indicating an inactive device. */
197 TAILQ_INIT(&vq->zmbuf_list);
201 reset_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
203 struct vhost_virtqueue *vq;
206 if (vring_idx >= VHOST_MAX_VRING) {
207 RTE_LOG(ERR, VHOST_CONFIG,
208 "Failed not init vring, out of bound (%d)\n",
213 vq = dev->virtqueue[vring_idx];
215 init_vring_queue(dev, vring_idx);
220 alloc_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
222 struct vhost_virtqueue *vq;
224 vq = rte_malloc(NULL, sizeof(struct vhost_virtqueue), 0);
226 RTE_LOG(ERR, VHOST_CONFIG,
227 "Failed to allocate memory for vring:%u.\n", vring_idx);
231 dev->virtqueue[vring_idx] = vq;
232 init_vring_queue(dev, vring_idx);
240 * Reset some variables in device structure, while keeping few
241 * others untouched, such as vid, ifname, nr_vring: they
242 * should be same unless the device is removed.
245 reset_device(struct virtio_net *dev)
250 dev->protocol_features = 0;
253 for (i = 0; i < dev->nr_vring; i++)
254 reset_vring_queue(dev, i);
258 * Invoked when there is a new vhost-user connection established (when
259 * there is a new virtio device being attached).
262 vhost_new_device(void)
264 struct virtio_net *dev;
267 dev = rte_zmalloc(NULL, sizeof(struct virtio_net), 0);
269 RTE_LOG(ERR, VHOST_CONFIG,
270 "Failed to allocate memory for new dev.\n");
274 for (i = 0; i < MAX_VHOST_DEVICE; i++) {
275 if (vhost_devices[i] == NULL)
278 if (i == MAX_VHOST_DEVICE) {
279 RTE_LOG(ERR, VHOST_CONFIG,
280 "Failed to find a free slot for new device.\n");
285 vhost_devices[i] = dev;
287 dev->slave_req_fd = -1;
293 * Invoked when there is the vhost-user connection is broken (when
294 * the virtio device is being detached).
297 vhost_destroy_device(int vid)
299 struct virtio_net *dev = get_device(vid);
304 if (dev->flags & VIRTIO_DEV_RUNNING) {
305 dev->flags &= ~VIRTIO_DEV_RUNNING;
306 dev->notify_ops->destroy_device(vid);
309 cleanup_device(dev, 1);
312 vhost_devices[vid] = NULL;
316 vhost_set_ifname(int vid, const char *if_name, unsigned int if_len)
318 struct virtio_net *dev;
321 dev = get_device(vid);
325 len = if_len > sizeof(dev->ifname) ?
326 sizeof(dev->ifname) : if_len;
328 strncpy(dev->ifname, if_name, len);
329 dev->ifname[sizeof(dev->ifname) - 1] = '\0';
333 vhost_enable_dequeue_zero_copy(int vid)
335 struct virtio_net *dev = get_device(vid);
340 dev->dequeue_zero_copy = 1;
344 rte_vhost_get_mtu(int vid, uint16_t *mtu)
346 struct virtio_net *dev = get_device(vid);
351 if (!(dev->flags & VIRTIO_DEV_READY))
354 if (!(dev->features & (1ULL << VIRTIO_NET_F_MTU)))
363 rte_vhost_get_numa_node(int vid)
365 #ifdef RTE_LIBRTE_VHOST_NUMA
366 struct virtio_net *dev = get_device(vid);
373 ret = get_mempolicy(&numa_node, NULL, 0, dev,
374 MPOL_F_NODE | MPOL_F_ADDR);
376 RTE_LOG(ERR, VHOST_CONFIG,
377 "(%d) failed to query numa node: %s\n",
378 vid, rte_strerror(errno));
390 rte_vhost_get_queue_num(int vid)
392 struct virtio_net *dev = get_device(vid);
397 return dev->nr_vring / 2;
401 rte_vhost_get_vring_num(int vid)
403 struct virtio_net *dev = get_device(vid);
408 return dev->nr_vring;
412 rte_vhost_get_ifname(int vid, char *buf, size_t len)
414 struct virtio_net *dev = get_device(vid);
419 len = RTE_MIN(len, sizeof(dev->ifname));
421 strncpy(buf, dev->ifname, len);
428 rte_vhost_get_negotiated_features(int vid, uint64_t *features)
430 struct virtio_net *dev;
432 dev = get_device(vid);
436 *features = dev->features;
441 rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem)
443 struct virtio_net *dev;
444 struct rte_vhost_memory *m;
447 dev = get_device(vid);
451 size = dev->mem->nregions * sizeof(struct rte_vhost_mem_region);
452 m = malloc(sizeof(struct rte_vhost_memory) + size);
456 m->nregions = dev->mem->nregions;
457 memcpy(m->regions, dev->mem->regions, size);
464 rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
465 struct rte_vhost_vring *vring)
467 struct virtio_net *dev;
468 struct vhost_virtqueue *vq;
470 dev = get_device(vid);
474 if (vring_idx >= VHOST_MAX_VRING)
477 vq = dev->virtqueue[vring_idx];
481 vring->desc = vq->desc;
482 vring->avail = vq->avail;
483 vring->used = vq->used;
484 vring->log_guest_addr = vq->log_guest_addr;
486 vring->callfd = vq->callfd;
487 vring->kickfd = vq->kickfd;
488 vring->size = vq->size;
494 rte_vhost_avail_entries(int vid, uint16_t queue_id)
496 struct virtio_net *dev;
497 struct vhost_virtqueue *vq;
499 dev = get_device(vid);
503 vq = dev->virtqueue[queue_id];
507 return *(volatile uint16_t *)&vq->avail->idx - vq->last_used_idx;
511 rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable)
513 struct virtio_net *dev = get_device(vid);
519 RTE_LOG(ERR, VHOST_CONFIG,
520 "guest notification isn't supported.\n");
524 dev->virtqueue[queue_id]->used->flags = VRING_USED_F_NO_NOTIFY;
529 rte_vhost_log_write(int vid, uint64_t addr, uint64_t len)
531 struct virtio_net *dev = get_device(vid);
536 vhost_log_write(dev, addr, len);
540 rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
541 uint64_t offset, uint64_t len)
543 struct virtio_net *dev;
544 struct vhost_virtqueue *vq;
546 dev = get_device(vid);
550 if (vring_idx >= VHOST_MAX_VRING)
552 vq = dev->virtqueue[vring_idx];
556 vhost_log_used_vring(dev, vq, offset, len);
560 rte_vhost_rx_queue_count(int vid, uint16_t qid)
562 struct virtio_net *dev;
563 struct vhost_virtqueue *vq;
565 dev = get_device(vid);
569 if (unlikely(qid >= dev->nr_vring || (qid & 1) == 0)) {
570 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
571 dev->vid, __func__, qid);
575 vq = dev->virtqueue[qid];
579 if (unlikely(vq->enabled == 0 || vq->avail == NULL))
582 return *((volatile uint16_t *)&vq->avail->idx) - vq->last_avail_idx;