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
15 #include <rte_errno.h>
16 #include <rte_ethdev.h>
18 #include <rte_string_fns.h>
19 #include <rte_memory.h>
20 #include <rte_malloc.h>
21 #include <rte_vhost.h>
22 #include <rte_rwlock.h>
26 #include "vhost_user.h"
28 struct virtio_net *vhost_devices[MAX_VHOST_DEVICE];
30 /* Called with iotlb_lock read-locked */
32 __vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
33 uint64_t iova, uint64_t *size, uint8_t perm)
35 uint64_t vva, tmp_size;
42 vva = vhost_user_iotlb_cache_find(vq, iova, &tmp_size, perm);
43 if (tmp_size == *size)
48 if (!vhost_user_iotlb_pending_miss(vq, iova, perm)) {
50 * iotlb_lock is read-locked for a full burst,
51 * but it only protects the iotlb cache.
52 * In case of IOTLB miss, we might block on the socket,
53 * which could cause a deadlock with QEMU if an IOTLB update
54 * is being handled. We can safely unlock here to avoid it.
56 vhost_user_iotlb_rd_unlock(vq);
58 vhost_user_iotlb_pending_insert(vq, iova, perm);
59 if (vhost_user_iotlb_miss(dev, iova, perm)) {
60 RTE_LOG(ERR, VHOST_CONFIG,
61 "IOTLB miss req failed for IOVA 0x%" PRIx64 "\n",
63 vhost_user_iotlb_pending_remove(vq, iova, 1, perm);
66 vhost_user_iotlb_rd_lock(vq);
73 cleanup_vq(struct vhost_virtqueue *vq, int destroy)
75 if ((vq->callfd >= 0) && (destroy != 0))
82 * Unmap any memory, close any file descriptors and
83 * free any memory owned by a device.
86 cleanup_device(struct virtio_net *dev, int destroy)
90 vhost_backend_cleanup(dev);
92 for (i = 0; i < dev->nr_vring; i++)
93 cleanup_vq(dev->virtqueue[i], destroy);
97 free_vq(struct virtio_net *dev, struct vhost_virtqueue *vq)
99 if (vq_is_packed(dev))
100 rte_free(vq->shadow_used_packed);
102 rte_free(vq->shadow_used_split);
103 rte_free(vq->batch_copy_elems);
104 rte_mempool_free(vq->iotlb_pool);
109 * Release virtqueues and device memory.
112 free_device(struct virtio_net *dev)
116 for (i = 0; i < dev->nr_vring; i++)
117 free_vq(dev, dev->virtqueue[i]);
123 vring_translate_split(struct virtio_net *dev, struct vhost_virtqueue *vq)
125 uint64_t req_size, size;
127 req_size = sizeof(struct vring_desc) * vq->size;
129 vq->desc = (struct vring_desc *)(uintptr_t)vhost_iova_to_vva(dev, vq,
130 vq->ring_addrs.desc_user_addr,
131 &size, VHOST_ACCESS_RW);
132 if (!vq->desc || size != req_size)
135 req_size = sizeof(struct vring_avail);
136 req_size += sizeof(uint16_t) * vq->size;
137 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))
138 req_size += sizeof(uint16_t);
140 vq->avail = (struct vring_avail *)(uintptr_t)vhost_iova_to_vva(dev, vq,
141 vq->ring_addrs.avail_user_addr,
142 &size, VHOST_ACCESS_RW);
143 if (!vq->avail || size != req_size)
146 req_size = sizeof(struct vring_used);
147 req_size += sizeof(struct vring_used_elem) * vq->size;
148 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))
149 req_size += sizeof(uint16_t);
151 vq->used = (struct vring_used *)(uintptr_t)vhost_iova_to_vva(dev, vq,
152 vq->ring_addrs.used_user_addr,
153 &size, VHOST_ACCESS_RW);
154 if (!vq->used || size != req_size)
161 vring_translate_packed(struct virtio_net *dev, struct vhost_virtqueue *vq)
163 uint64_t req_size, size;
165 req_size = sizeof(struct vring_packed_desc) * vq->size;
167 vq->desc_packed = (struct vring_packed_desc *)(uintptr_t)
168 vhost_iova_to_vva(dev, vq, vq->ring_addrs.desc_user_addr,
169 &size, VHOST_ACCESS_RW);
170 if (!vq->desc_packed || size != req_size)
173 req_size = sizeof(struct vring_packed_desc_event);
175 vq->driver_event = (struct vring_packed_desc_event *)(uintptr_t)
176 vhost_iova_to_vva(dev, vq, vq->ring_addrs.avail_user_addr,
177 &size, VHOST_ACCESS_RW);
178 if (!vq->driver_event || size != req_size)
181 req_size = sizeof(struct vring_packed_desc_event);
183 vq->device_event = (struct vring_packed_desc_event *)(uintptr_t)
184 vhost_iova_to_vva(dev, vq, vq->ring_addrs.used_user_addr,
185 &size, VHOST_ACCESS_RW);
186 if (!vq->device_event || size != req_size)
193 vring_translate(struct virtio_net *dev, struct vhost_virtqueue *vq)
196 if (!(dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)))
199 if (vq_is_packed(dev)) {
200 if (vring_translate_packed(dev, vq) < 0)
203 if (vring_translate_split(dev, vq) < 0)
213 vring_invalidate(struct virtio_net *dev, struct vhost_virtqueue *vq)
215 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
216 vhost_user_iotlb_wr_lock(vq);
223 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
224 vhost_user_iotlb_wr_unlock(vq);
228 init_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
230 struct vhost_virtqueue *vq;
232 if (vring_idx >= VHOST_MAX_VRING) {
233 RTE_LOG(ERR, VHOST_CONFIG,
234 "Failed not init vring, out of bound (%d)\n",
239 vq = dev->virtqueue[vring_idx];
241 memset(vq, 0, sizeof(struct vhost_virtqueue));
243 vq->kickfd = VIRTIO_UNINITIALIZED_EVENTFD;
244 vq->callfd = VIRTIO_UNINITIALIZED_EVENTFD;
246 vhost_user_iotlb_init(dev, vring_idx);
247 /* Backends are set to -1 indicating an inactive device. */
250 TAILQ_INIT(&vq->zmbuf_list);
254 reset_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
256 struct vhost_virtqueue *vq;
259 if (vring_idx >= VHOST_MAX_VRING) {
260 RTE_LOG(ERR, VHOST_CONFIG,
261 "Failed not init vring, out of bound (%d)\n",
266 vq = dev->virtqueue[vring_idx];
268 init_vring_queue(dev, vring_idx);
273 alloc_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
275 struct vhost_virtqueue *vq;
277 vq = rte_malloc(NULL, sizeof(struct vhost_virtqueue), 0);
279 RTE_LOG(ERR, VHOST_CONFIG,
280 "Failed to allocate memory for vring:%u.\n", vring_idx);
284 dev->virtqueue[vring_idx] = vq;
285 init_vring_queue(dev, vring_idx);
286 rte_spinlock_init(&vq->access_lock);
287 vq->avail_wrap_counter = 1;
288 vq->used_wrap_counter = 1;
289 vq->signalled_used_valid = false;
297 * Reset some variables in device structure, while keeping few
298 * others untouched, such as vid, ifname, nr_vring: they
299 * should be same unless the device is removed.
302 reset_device(struct virtio_net *dev)
307 dev->protocol_features = 0;
308 dev->flags &= VIRTIO_DEV_BUILTIN_VIRTIO_NET;
310 for (i = 0; i < dev->nr_vring; i++)
311 reset_vring_queue(dev, i);
315 * Invoked when there is a new vhost-user connection established (when
316 * there is a new virtio device being attached).
319 vhost_new_device(void)
321 struct virtio_net *dev;
324 for (i = 0; i < MAX_VHOST_DEVICE; i++) {
325 if (vhost_devices[i] == NULL)
329 if (i == MAX_VHOST_DEVICE) {
330 RTE_LOG(ERR, VHOST_CONFIG,
331 "Failed to find a free slot for new device.\n");
335 dev = rte_zmalloc(NULL, sizeof(struct virtio_net), 0);
337 RTE_LOG(ERR, VHOST_CONFIG,
338 "Failed to allocate memory for new dev.\n");
342 vhost_devices[i] = dev;
344 dev->flags = VIRTIO_DEV_BUILTIN_VIRTIO_NET;
345 dev->slave_req_fd = -1;
346 dev->vdpa_dev_id = -1;
347 rte_spinlock_init(&dev->slave_req_lock);
353 vhost_destroy_device_notify(struct virtio_net *dev)
355 struct rte_vdpa_device *vdpa_dev;
358 if (dev->flags & VIRTIO_DEV_RUNNING) {
359 did = dev->vdpa_dev_id;
360 vdpa_dev = rte_vdpa_get_device(did);
361 if (vdpa_dev && vdpa_dev->ops->dev_close)
362 vdpa_dev->ops->dev_close(dev->vid);
363 dev->flags &= ~VIRTIO_DEV_RUNNING;
364 dev->notify_ops->destroy_device(dev->vid);
369 * Invoked when there is the vhost-user connection is broken (when
370 * the virtio device is being detached).
373 vhost_destroy_device(int vid)
375 struct virtio_net *dev = get_device(vid);
380 vhost_destroy_device_notify(dev);
382 cleanup_device(dev, 1);
385 vhost_devices[vid] = NULL;
389 vhost_attach_vdpa_device(int vid, int did)
391 struct virtio_net *dev = get_device(vid);
396 if (rte_vdpa_get_device(did) == NULL)
399 dev->vdpa_dev_id = did;
403 vhost_detach_vdpa_device(int vid)
405 struct virtio_net *dev = get_device(vid);
410 vhost_user_host_notifier_ctrl(vid, false);
412 dev->vdpa_dev_id = -1;
416 vhost_set_ifname(int vid, const char *if_name, unsigned int if_len)
418 struct virtio_net *dev;
421 dev = get_device(vid);
425 len = if_len > sizeof(dev->ifname) ?
426 sizeof(dev->ifname) : if_len;
428 strncpy(dev->ifname, if_name, len);
429 dev->ifname[sizeof(dev->ifname) - 1] = '\0';
433 vhost_enable_dequeue_zero_copy(int vid)
435 struct virtio_net *dev = get_device(vid);
440 dev->dequeue_zero_copy = 1;
444 vhost_set_builtin_virtio_net(int vid, bool enable)
446 struct virtio_net *dev = get_device(vid);
452 dev->flags |= VIRTIO_DEV_BUILTIN_VIRTIO_NET;
454 dev->flags &= ~VIRTIO_DEV_BUILTIN_VIRTIO_NET;
458 rte_vhost_get_mtu(int vid, uint16_t *mtu)
460 struct virtio_net *dev = get_device(vid);
465 if (!(dev->flags & VIRTIO_DEV_READY))
468 if (!(dev->features & (1ULL << VIRTIO_NET_F_MTU)))
477 rte_vhost_get_numa_node(int vid)
479 #ifdef RTE_LIBRTE_VHOST_NUMA
480 struct virtio_net *dev = get_device(vid);
484 if (dev == NULL || numa_available() != 0)
487 ret = get_mempolicy(&numa_node, NULL, 0, dev,
488 MPOL_F_NODE | MPOL_F_ADDR);
490 RTE_LOG(ERR, VHOST_CONFIG,
491 "(%d) failed to query numa node: %s\n",
492 vid, rte_strerror(errno));
504 rte_vhost_get_queue_num(int vid)
506 struct virtio_net *dev = get_device(vid);
511 return dev->nr_vring / 2;
515 rte_vhost_get_vring_num(int vid)
517 struct virtio_net *dev = get_device(vid);
522 return dev->nr_vring;
526 rte_vhost_get_ifname(int vid, char *buf, size_t len)
528 struct virtio_net *dev = get_device(vid);
533 len = RTE_MIN(len, sizeof(dev->ifname));
535 strncpy(buf, dev->ifname, len);
542 rte_vhost_get_negotiated_features(int vid, uint64_t *features)
544 struct virtio_net *dev;
546 dev = get_device(vid);
550 *features = dev->features;
555 rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem)
557 struct virtio_net *dev;
558 struct rte_vhost_memory *m;
561 dev = get_device(vid);
565 size = dev->mem->nregions * sizeof(struct rte_vhost_mem_region);
566 m = malloc(sizeof(struct rte_vhost_memory) + size);
570 m->nregions = dev->mem->nregions;
571 memcpy(m->regions, dev->mem->regions, size);
578 rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
579 struct rte_vhost_vring *vring)
581 struct virtio_net *dev;
582 struct vhost_virtqueue *vq;
584 dev = get_device(vid);
588 if (vring_idx >= VHOST_MAX_VRING)
591 vq = dev->virtqueue[vring_idx];
595 vring->desc = vq->desc;
596 vring->avail = vq->avail;
597 vring->used = vq->used;
598 vring->log_guest_addr = vq->log_guest_addr;
600 vring->callfd = vq->callfd;
601 vring->kickfd = vq->kickfd;
602 vring->size = vq->size;
608 rte_vhost_vring_call(int vid, uint16_t vring_idx)
610 struct virtio_net *dev;
611 struct vhost_virtqueue *vq;
613 dev = get_device(vid);
617 if (vring_idx >= VHOST_MAX_VRING)
620 vq = dev->virtqueue[vring_idx];
624 if (vq_is_packed(dev))
625 vhost_vring_call_packed(dev, vq);
627 vhost_vring_call_split(dev, vq);
633 rte_vhost_avail_entries(int vid, uint16_t queue_id)
635 struct virtio_net *dev;
636 struct vhost_virtqueue *vq;
638 dev = get_device(vid);
642 vq = dev->virtqueue[queue_id];
646 return *(volatile uint16_t *)&vq->avail->idx - vq->last_used_idx;
650 vhost_enable_notify_split(struct vhost_virtqueue *vq, int enable)
653 vq->used->flags &= ~VRING_USED_F_NO_NOTIFY;
655 vq->used->flags |= VRING_USED_F_NO_NOTIFY;
659 vhost_enable_notify_packed(struct virtio_net *dev,
660 struct vhost_virtqueue *vq, int enable)
665 vq->device_event->flags = VRING_EVENT_F_DISABLE;
667 flags = VRING_EVENT_F_ENABLE;
668 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX)) {
669 flags = VRING_EVENT_F_DESC;
670 vq->device_event->off_wrap = vq->last_avail_idx |
671 vq->avail_wrap_counter << 15;
676 vq->device_event->flags = flags;
680 rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable)
682 struct virtio_net *dev = get_device(vid);
683 struct vhost_virtqueue *vq;
688 vq = dev->virtqueue[queue_id];
690 if (vq_is_packed(dev))
691 vhost_enable_notify_packed(dev, vq, enable);
693 vhost_enable_notify_split(vq, enable);
699 rte_vhost_log_write(int vid, uint64_t addr, uint64_t len)
701 struct virtio_net *dev = get_device(vid);
706 vhost_log_write(dev, addr, len);
710 rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
711 uint64_t offset, uint64_t len)
713 struct virtio_net *dev;
714 struct vhost_virtqueue *vq;
716 dev = get_device(vid);
720 if (vring_idx >= VHOST_MAX_VRING)
722 vq = dev->virtqueue[vring_idx];
726 vhost_log_used_vring(dev, vq, offset, len);
730 rte_vhost_rx_queue_count(int vid, uint16_t qid)
732 struct virtio_net *dev;
733 struct vhost_virtqueue *vq;
735 dev = get_device(vid);
739 if (unlikely(qid >= dev->nr_vring || (qid & 1) == 0)) {
740 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
741 dev->vid, __func__, qid);
745 vq = dev->virtqueue[qid];
749 if (unlikely(vq->enabled == 0 || vq->avail == NULL))
752 return *((volatile uint16_t *)&vq->avail->idx) - vq->last_avail_idx;
755 int rte_vhost_get_vdpa_device_id(int vid)
757 struct virtio_net *dev = get_device(vid);
762 return dev->vdpa_dev_id;
765 int rte_vhost_get_log_base(int vid, uint64_t *log_base,
768 struct virtio_net *dev = get_device(vid);
773 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
774 RTE_LOG(ERR, VHOST_DATA,
775 "(%d) %s: built-in vhost net backend is disabled.\n",
780 *log_base = dev->log_base;
781 *log_size = dev->log_size;
786 int rte_vhost_get_vring_base(int vid, uint16_t queue_id,
787 uint16_t *last_avail_idx, uint16_t *last_used_idx)
789 struct virtio_net *dev = get_device(vid);
794 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
795 RTE_LOG(ERR, VHOST_DATA,
796 "(%d) %s: built-in vhost net backend is disabled.\n",
801 *last_avail_idx = dev->virtqueue[queue_id]->last_avail_idx;
802 *last_used_idx = dev->virtqueue[queue_id]->last_used_idx;
807 int rte_vhost_set_vring_base(int vid, uint16_t queue_id,
808 uint16_t last_avail_idx, uint16_t last_used_idx)
810 struct virtio_net *dev = get_device(vid);
815 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
816 RTE_LOG(ERR, VHOST_DATA,
817 "(%d) %s: built-in vhost net backend is disabled.\n",
822 dev->virtqueue[queue_id]->last_avail_idx = last_avail_idx;
823 dev->virtqueue[queue_id]->last_used_idx = last_used_idx;