1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2018 Intel Corporation
7 * The vhost-user protocol connection is an external interface, so it must be
8 * robust against invalid inputs.
10 * This is important because the vhost-user master is only one step removed
11 * from the guest. Malicious guests that have escaped will then launch further
12 * attacks from the vhost-user master.
14 * Even in deployments where guests are trusted, a bug in the vhost-user master
15 * can still cause invalid messages to be sent. Such messages must not
16 * compromise the stability of the DPDK application by causing crashes, memory
17 * corruption, or other problematic behavior.
19 * Do not assume received VhostUserMsg fields contain sensible values!
28 #include <sys/ioctl.h>
30 #include <sys/types.h>
32 #include <sys/syscall.h>
34 #ifdef RTE_LIBRTE_VHOST_NUMA
37 #ifdef RTE_LIBRTE_VHOST_POSTCOPY
38 #include <linux/userfaultfd.h>
41 #include <rte_common.h>
42 #include <rte_malloc.h>
47 #include "vhost_user.h"
49 #define VIRTIO_MIN_MTU 68
50 #define VIRTIO_MAX_MTU 65535
52 static const char *vhost_message_str[VHOST_USER_MAX] = {
53 [VHOST_USER_NONE] = "VHOST_USER_NONE",
54 [VHOST_USER_GET_FEATURES] = "VHOST_USER_GET_FEATURES",
55 [VHOST_USER_SET_FEATURES] = "VHOST_USER_SET_FEATURES",
56 [VHOST_USER_SET_OWNER] = "VHOST_USER_SET_OWNER",
57 [VHOST_USER_RESET_OWNER] = "VHOST_USER_RESET_OWNER",
58 [VHOST_USER_SET_MEM_TABLE] = "VHOST_USER_SET_MEM_TABLE",
59 [VHOST_USER_SET_LOG_BASE] = "VHOST_USER_SET_LOG_BASE",
60 [VHOST_USER_SET_LOG_FD] = "VHOST_USER_SET_LOG_FD",
61 [VHOST_USER_SET_VRING_NUM] = "VHOST_USER_SET_VRING_NUM",
62 [VHOST_USER_SET_VRING_ADDR] = "VHOST_USER_SET_VRING_ADDR",
63 [VHOST_USER_SET_VRING_BASE] = "VHOST_USER_SET_VRING_BASE",
64 [VHOST_USER_GET_VRING_BASE] = "VHOST_USER_GET_VRING_BASE",
65 [VHOST_USER_SET_VRING_KICK] = "VHOST_USER_SET_VRING_KICK",
66 [VHOST_USER_SET_VRING_CALL] = "VHOST_USER_SET_VRING_CALL",
67 [VHOST_USER_SET_VRING_ERR] = "VHOST_USER_SET_VRING_ERR",
68 [VHOST_USER_GET_PROTOCOL_FEATURES] = "VHOST_USER_GET_PROTOCOL_FEATURES",
69 [VHOST_USER_SET_PROTOCOL_FEATURES] = "VHOST_USER_SET_PROTOCOL_FEATURES",
70 [VHOST_USER_GET_QUEUE_NUM] = "VHOST_USER_GET_QUEUE_NUM",
71 [VHOST_USER_SET_VRING_ENABLE] = "VHOST_USER_SET_VRING_ENABLE",
72 [VHOST_USER_SEND_RARP] = "VHOST_USER_SEND_RARP",
73 [VHOST_USER_NET_SET_MTU] = "VHOST_USER_NET_SET_MTU",
74 [VHOST_USER_SET_SLAVE_REQ_FD] = "VHOST_USER_SET_SLAVE_REQ_FD",
75 [VHOST_USER_IOTLB_MSG] = "VHOST_USER_IOTLB_MSG",
76 [VHOST_USER_CRYPTO_CREATE_SESS] = "VHOST_USER_CRYPTO_CREATE_SESS",
77 [VHOST_USER_CRYPTO_CLOSE_SESS] = "VHOST_USER_CRYPTO_CLOSE_SESS",
78 [VHOST_USER_POSTCOPY_ADVISE] = "VHOST_USER_POSTCOPY_ADVISE",
79 [VHOST_USER_POSTCOPY_LISTEN] = "VHOST_USER_POSTCOPY_LISTEN",
80 [VHOST_USER_POSTCOPY_END] = "VHOST_USER_POSTCOPY_END",
83 static int send_vhost_reply(int sockfd, struct VhostUserMsg *msg);
84 static int read_vhost_message(int sockfd, struct VhostUserMsg *msg);
92 ret = fstat(fd, &stat);
93 return ret == -1 ? (uint64_t)-1 : (uint64_t)stat.st_blksize;
97 free_mem_region(struct virtio_net *dev)
100 struct rte_vhost_mem_region *reg;
102 if (!dev || !dev->mem)
105 for (i = 0; i < dev->mem->nregions; i++) {
106 reg = &dev->mem->regions[i];
107 if (reg->host_user_addr) {
108 munmap(reg->mmap_addr, reg->mmap_size);
115 vhost_backend_cleanup(struct virtio_net *dev)
118 free_mem_region(dev);
123 free(dev->guest_pages);
124 dev->guest_pages = NULL;
127 munmap((void *)(uintptr_t)dev->log_addr, dev->log_size);
131 if (dev->slave_req_fd >= 0) {
132 close(dev->slave_req_fd);
133 dev->slave_req_fd = -1;
136 if (dev->postcopy_ufd >= 0) {
137 close(dev->postcopy_ufd);
138 dev->postcopy_ufd = -1;
141 dev->postcopy_listening = 0;
145 * This function just returns success at the moment unless
146 * the device hasn't been initialised.
149 vhost_user_set_owner(struct virtio_net **pdev __rte_unused,
150 struct VhostUserMsg *msg __rte_unused,
151 int main_fd __rte_unused)
157 vhost_user_reset_owner(struct virtio_net **pdev,
158 struct VhostUserMsg *msg __rte_unused,
159 int main_fd __rte_unused)
161 struct virtio_net *dev = *pdev;
162 vhost_destroy_device_notify(dev);
164 cleanup_device(dev, 0);
170 * The features that we support are requested.
173 vhost_user_get_features(struct virtio_net **pdev, struct VhostUserMsg *msg,
174 int main_fd __rte_unused)
176 struct virtio_net *dev = *pdev;
177 uint64_t features = 0;
179 rte_vhost_driver_get_features(dev->ifname, &features);
181 msg->payload.u64 = features;
182 msg->size = sizeof(msg->payload.u64);
185 return VH_RESULT_REPLY;
189 * The queue number that we support are requested.
192 vhost_user_get_queue_num(struct virtio_net **pdev, struct VhostUserMsg *msg,
193 int main_fd __rte_unused)
195 struct virtio_net *dev = *pdev;
196 uint32_t queue_num = 0;
198 rte_vhost_driver_get_queue_num(dev->ifname, &queue_num);
200 msg->payload.u64 = (uint64_t)queue_num;
201 msg->size = sizeof(msg->payload.u64);
204 return VH_RESULT_REPLY;
208 * We receive the negotiated features supported by us and the virtio device.
211 vhost_user_set_features(struct virtio_net **pdev, struct VhostUserMsg *msg,
212 int main_fd __rte_unused)
214 struct virtio_net *dev = *pdev;
215 uint64_t features = msg->payload.u64;
216 uint64_t vhost_features = 0;
217 struct rte_vdpa_device *vdpa_dev;
220 rte_vhost_driver_get_features(dev->ifname, &vhost_features);
221 if (features & ~vhost_features) {
222 RTE_LOG(ERR, VHOST_CONFIG,
223 "(%d) received invalid negotiated features.\n",
225 return VH_RESULT_ERR;
228 if (dev->flags & VIRTIO_DEV_RUNNING) {
229 if (dev->features == features)
233 * Error out if master tries to change features while device is
234 * in running state. The exception being VHOST_F_LOG_ALL, which
235 * is enabled when the live-migration starts.
237 if ((dev->features ^ features) & ~(1ULL << VHOST_F_LOG_ALL)) {
238 RTE_LOG(ERR, VHOST_CONFIG,
239 "(%d) features changed while device is running.\n",
241 return VH_RESULT_ERR;
244 if (dev->notify_ops->features_changed)
245 dev->notify_ops->features_changed(dev->vid, features);
248 dev->features = features;
250 ((1 << VIRTIO_NET_F_MRG_RXBUF) | (1ULL << VIRTIO_F_VERSION_1))) {
251 dev->vhost_hlen = sizeof(struct virtio_net_hdr_mrg_rxbuf);
253 dev->vhost_hlen = sizeof(struct virtio_net_hdr);
255 VHOST_LOG_DEBUG(VHOST_CONFIG,
256 "(%d) mergeable RX buffers %s, virtio 1 %s\n",
258 (dev->features & (1 << VIRTIO_NET_F_MRG_RXBUF)) ? "on" : "off",
259 (dev->features & (1ULL << VIRTIO_F_VERSION_1)) ? "on" : "off");
261 if ((dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET) &&
262 !(dev->features & (1ULL << VIRTIO_NET_F_MQ))) {
264 * Remove all but first queue pair if MQ hasn't been
265 * negotiated. This is safe because the device is not
266 * running at this stage.
268 while (dev->nr_vring > 2) {
269 struct vhost_virtqueue *vq;
271 vq = dev->virtqueue[--dev->nr_vring];
275 dev->virtqueue[dev->nr_vring] = NULL;
281 did = dev->vdpa_dev_id;
282 vdpa_dev = rte_vdpa_get_device(did);
283 if (vdpa_dev && vdpa_dev->ops->set_features)
284 vdpa_dev->ops->set_features(dev->vid);
290 * The virtio device sends us the size of the descriptor ring.
293 vhost_user_set_vring_num(struct virtio_net **pdev,
294 struct VhostUserMsg *msg,
295 int main_fd __rte_unused)
297 struct virtio_net *dev = *pdev;
298 struct vhost_virtqueue *vq = dev->virtqueue[msg->payload.state.index];
300 vq->size = msg->payload.state.num;
302 /* VIRTIO 1.0, 2.4 Virtqueues says:
304 * Queue Size value is always a power of 2. The maximum Queue Size
307 if ((vq->size & (vq->size - 1)) || vq->size > 32768) {
308 RTE_LOG(ERR, VHOST_CONFIG,
309 "invalid virtqueue size %u\n", vq->size);
310 return VH_RESULT_ERR;
313 if (dev->dequeue_zero_copy) {
315 vq->last_zmbuf_idx = 0;
316 vq->zmbuf_size = vq->size;
317 vq->zmbufs = rte_zmalloc(NULL, vq->zmbuf_size *
318 sizeof(struct zcopy_mbuf), 0);
319 if (vq->zmbufs == NULL) {
320 RTE_LOG(WARNING, VHOST_CONFIG,
321 "failed to allocate mem for zero copy; "
322 "zero copy is force disabled\n");
323 dev->dequeue_zero_copy = 0;
325 TAILQ_INIT(&vq->zmbuf_list);
328 if (vq_is_packed(dev)) {
329 vq->shadow_used_packed = rte_malloc(NULL,
331 sizeof(struct vring_used_elem_packed),
332 RTE_CACHE_LINE_SIZE);
333 if (!vq->shadow_used_packed) {
334 RTE_LOG(ERR, VHOST_CONFIG,
335 "failed to allocate memory for shadow used ring.\n");
336 return VH_RESULT_ERR;
340 vq->shadow_used_split = rte_malloc(NULL,
341 vq->size * sizeof(struct vring_used_elem),
342 RTE_CACHE_LINE_SIZE);
343 if (!vq->shadow_used_split) {
344 RTE_LOG(ERR, VHOST_CONFIG,
345 "failed to allocate memory for shadow used ring.\n");
346 return VH_RESULT_ERR;
350 vq->batch_copy_elems = rte_malloc(NULL,
351 vq->size * sizeof(struct batch_copy_elem),
352 RTE_CACHE_LINE_SIZE);
353 if (!vq->batch_copy_elems) {
354 RTE_LOG(ERR, VHOST_CONFIG,
355 "failed to allocate memory for batching copy.\n");
356 return VH_RESULT_ERR;
363 * Reallocate virtio_dev and vhost_virtqueue data structure to make them on the
364 * same numa node as the memory of vring descriptor.
366 #ifdef RTE_LIBRTE_VHOST_NUMA
367 static struct virtio_net*
368 numa_realloc(struct virtio_net *dev, int index)
370 int oldnode, newnode;
371 struct virtio_net *old_dev;
372 struct vhost_virtqueue *old_vq, *vq;
373 struct zcopy_mbuf *new_zmbuf;
374 struct vring_used_elem *new_shadow_used_split;
375 struct vring_used_elem_packed *new_shadow_used_packed;
376 struct batch_copy_elem *new_batch_copy_elems;
380 vq = old_vq = dev->virtqueue[index];
382 ret = get_mempolicy(&newnode, NULL, 0, old_vq->desc,
383 MPOL_F_NODE | MPOL_F_ADDR);
385 /* check if we need to reallocate vq */
386 ret |= get_mempolicy(&oldnode, NULL, 0, old_vq,
387 MPOL_F_NODE | MPOL_F_ADDR);
389 RTE_LOG(ERR, VHOST_CONFIG,
390 "Unable to get vq numa information.\n");
393 if (oldnode != newnode) {
394 RTE_LOG(INFO, VHOST_CONFIG,
395 "reallocate vq from %d to %d node\n", oldnode, newnode);
396 vq = rte_malloc_socket(NULL, sizeof(*vq), 0, newnode);
400 memcpy(vq, old_vq, sizeof(*vq));
401 TAILQ_INIT(&vq->zmbuf_list);
403 if (dev->dequeue_zero_copy) {
404 new_zmbuf = rte_malloc_socket(NULL, vq->zmbuf_size *
405 sizeof(struct zcopy_mbuf), 0, newnode);
407 rte_free(vq->zmbufs);
408 vq->zmbufs = new_zmbuf;
412 if (vq_is_packed(dev)) {
413 new_shadow_used_packed = rte_malloc_socket(NULL,
415 sizeof(struct vring_used_elem_packed),
418 if (new_shadow_used_packed) {
419 rte_free(vq->shadow_used_packed);
420 vq->shadow_used_packed = new_shadow_used_packed;
423 new_shadow_used_split = rte_malloc_socket(NULL,
425 sizeof(struct vring_used_elem),
428 if (new_shadow_used_split) {
429 rte_free(vq->shadow_used_split);
430 vq->shadow_used_split = new_shadow_used_split;
434 new_batch_copy_elems = rte_malloc_socket(NULL,
435 vq->size * sizeof(struct batch_copy_elem),
438 if (new_batch_copy_elems) {
439 rte_free(vq->batch_copy_elems);
440 vq->batch_copy_elems = new_batch_copy_elems;
446 /* check if we need to reallocate dev */
447 ret = get_mempolicy(&oldnode, NULL, 0, old_dev,
448 MPOL_F_NODE | MPOL_F_ADDR);
450 RTE_LOG(ERR, VHOST_CONFIG,
451 "Unable to get dev numa information.\n");
454 if (oldnode != newnode) {
455 RTE_LOG(INFO, VHOST_CONFIG,
456 "reallocate dev from %d to %d node\n",
458 dev = rte_malloc_socket(NULL, sizeof(*dev), 0, newnode);
464 memcpy(dev, old_dev, sizeof(*dev));
469 dev->virtqueue[index] = vq;
470 vhost_devices[dev->vid] = dev;
473 vhost_user_iotlb_init(dev, index);
478 static struct virtio_net*
479 numa_realloc(struct virtio_net *dev, int index __rte_unused)
485 /* Converts QEMU virtual address to Vhost virtual address. */
487 qva_to_vva(struct virtio_net *dev, uint64_t qva, uint64_t *len)
489 struct rte_vhost_mem_region *r;
492 if (unlikely(!dev || !dev->mem))
495 /* Find the region where the address lives. */
496 for (i = 0; i < dev->mem->nregions; i++) {
497 r = &dev->mem->regions[i];
499 if (qva >= r->guest_user_addr &&
500 qva < r->guest_user_addr + r->size) {
502 if (unlikely(*len > r->guest_user_addr + r->size - qva))
503 *len = r->guest_user_addr + r->size - qva;
505 return qva - r->guest_user_addr +
517 * Converts ring address to Vhost virtual address.
518 * If IOMMU is enabled, the ring address is a guest IO virtual address,
519 * else it is a QEMU virtual address.
522 ring_addr_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
523 uint64_t ra, uint64_t *size)
525 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)) {
528 vva = vhost_user_iotlb_cache_find(vq, ra,
529 size, VHOST_ACCESS_RW);
531 vhost_user_iotlb_miss(dev, ra, VHOST_ACCESS_RW);
536 return qva_to_vva(dev, ra, size);
539 static struct virtio_net *
540 translate_ring_addresses(struct virtio_net *dev, int vq_index)
542 struct vhost_virtqueue *vq = dev->virtqueue[vq_index];
543 struct vhost_vring_addr *addr = &vq->ring_addrs;
546 if (vq_is_packed(dev)) {
547 len = sizeof(struct vring_packed_desc) * vq->size;
548 vq->desc_packed = (struct vring_packed_desc *)(uintptr_t)
549 ring_addr_to_vva(dev, vq, addr->desc_user_addr, &len);
550 vq->log_guest_addr = 0;
551 if (vq->desc_packed == NULL ||
552 len != sizeof(struct vring_packed_desc) *
554 RTE_LOG(DEBUG, VHOST_CONFIG,
555 "(%d) failed to map desc_packed ring.\n",
560 dev = numa_realloc(dev, vq_index);
561 vq = dev->virtqueue[vq_index];
562 addr = &vq->ring_addrs;
564 len = sizeof(struct vring_packed_desc_event);
565 vq->driver_event = (struct vring_packed_desc_event *)
566 (uintptr_t)ring_addr_to_vva(dev,
567 vq, addr->avail_user_addr, &len);
568 if (vq->driver_event == NULL ||
569 len != sizeof(struct vring_packed_desc_event)) {
570 RTE_LOG(DEBUG, VHOST_CONFIG,
571 "(%d) failed to find driver area address.\n",
576 len = sizeof(struct vring_packed_desc_event);
577 vq->device_event = (struct vring_packed_desc_event *)
578 (uintptr_t)ring_addr_to_vva(dev,
579 vq, addr->used_user_addr, &len);
580 if (vq->device_event == NULL ||
581 len != sizeof(struct vring_packed_desc_event)) {
582 RTE_LOG(DEBUG, VHOST_CONFIG,
583 "(%d) failed to find device area address.\n",
591 /* The addresses are converted from QEMU virtual to Vhost virtual. */
592 if (vq->desc && vq->avail && vq->used)
595 len = sizeof(struct vring_desc) * vq->size;
596 vq->desc = (struct vring_desc *)(uintptr_t)ring_addr_to_vva(dev,
597 vq, addr->desc_user_addr, &len);
598 if (vq->desc == 0 || len != sizeof(struct vring_desc) * vq->size) {
599 RTE_LOG(DEBUG, VHOST_CONFIG,
600 "(%d) failed to map desc ring.\n",
605 dev = numa_realloc(dev, vq_index);
606 vq = dev->virtqueue[vq_index];
607 addr = &vq->ring_addrs;
609 len = sizeof(struct vring_avail) + sizeof(uint16_t) * vq->size;
610 vq->avail = (struct vring_avail *)(uintptr_t)ring_addr_to_vva(dev,
611 vq, addr->avail_user_addr, &len);
612 if (vq->avail == 0 ||
613 len != sizeof(struct vring_avail) +
614 sizeof(uint16_t) * vq->size) {
615 RTE_LOG(DEBUG, VHOST_CONFIG,
616 "(%d) failed to map avail ring.\n",
621 len = sizeof(struct vring_used) +
622 sizeof(struct vring_used_elem) * vq->size;
623 vq->used = (struct vring_used *)(uintptr_t)ring_addr_to_vva(dev,
624 vq, addr->used_user_addr, &len);
625 if (vq->used == 0 || len != sizeof(struct vring_used) +
626 sizeof(struct vring_used_elem) * vq->size) {
627 RTE_LOG(DEBUG, VHOST_CONFIG,
628 "(%d) failed to map used ring.\n",
633 if (vq->last_used_idx != vq->used->idx) {
634 RTE_LOG(WARNING, VHOST_CONFIG,
635 "last_used_idx (%u) and vq->used->idx (%u) mismatches; "
636 "some packets maybe resent for Tx and dropped for Rx\n",
637 vq->last_used_idx, vq->used->idx);
638 vq->last_used_idx = vq->used->idx;
639 vq->last_avail_idx = vq->used->idx;
642 vq->log_guest_addr = addr->log_guest_addr;
644 VHOST_LOG_DEBUG(VHOST_CONFIG, "(%d) mapped address desc: %p\n",
646 VHOST_LOG_DEBUG(VHOST_CONFIG, "(%d) mapped address avail: %p\n",
647 dev->vid, vq->avail);
648 VHOST_LOG_DEBUG(VHOST_CONFIG, "(%d) mapped address used: %p\n",
650 VHOST_LOG_DEBUG(VHOST_CONFIG, "(%d) log_guest_addr: %" PRIx64 "\n",
651 dev->vid, vq->log_guest_addr);
657 * The virtio device sends us the desc, used and avail ring addresses.
658 * This function then converts these to our address space.
661 vhost_user_set_vring_addr(struct virtio_net **pdev, struct VhostUserMsg *msg,
662 int main_fd __rte_unused)
664 struct virtio_net *dev = *pdev;
665 struct vhost_virtqueue *vq;
666 struct vhost_vring_addr *addr = &msg->payload.addr;
668 if (dev->mem == NULL)
669 return VH_RESULT_ERR;
671 /* addr->index refers to the queue index. The txq 1, rxq is 0. */
672 vq = dev->virtqueue[msg->payload.addr.index];
675 * Rings addresses should not be interpreted as long as the ring is not
676 * started and enabled
678 memcpy(&vq->ring_addrs, addr, sizeof(*addr));
680 vring_invalidate(dev, vq);
682 if (vq->enabled && (dev->features &
683 (1ULL << VHOST_USER_F_PROTOCOL_FEATURES))) {
684 dev = translate_ring_addresses(dev, msg->payload.addr.index);
686 return VH_RESULT_ERR;
695 * The virtio device sends us the available ring last used index.
698 vhost_user_set_vring_base(struct virtio_net **pdev,
699 struct VhostUserMsg *msg,
700 int main_fd __rte_unused)
702 struct virtio_net *dev = *pdev;
703 struct vhost_virtqueue *vq = dev->virtqueue[msg->payload.state.index];
704 uint64_t val = msg->payload.state.num;
706 if (vq_is_packed(dev)) {
708 * Bit[0:14]: avail index
709 * Bit[15]: avail wrap counter
711 vq->last_avail_idx = val & 0x7fff;
712 vq->avail_wrap_counter = !!(val & (0x1 << 15));
714 * Set used index to same value as available one, as
715 * their values should be the same since ring processing
716 * was stopped at get time.
718 vq->last_used_idx = vq->last_avail_idx;
719 vq->used_wrap_counter = vq->avail_wrap_counter;
721 vq->last_used_idx = msg->payload.state.num;
722 vq->last_avail_idx = msg->payload.state.num;
729 add_one_guest_page(struct virtio_net *dev, uint64_t guest_phys_addr,
730 uint64_t host_phys_addr, uint64_t size)
732 struct guest_page *page, *last_page;
734 if (dev->nr_guest_pages == dev->max_guest_pages) {
735 dev->max_guest_pages *= 2;
736 dev->guest_pages = realloc(dev->guest_pages,
737 dev->max_guest_pages * sizeof(*page));
738 if (!dev->guest_pages) {
739 RTE_LOG(ERR, VHOST_CONFIG, "cannot realloc guest_pages\n");
744 if (dev->nr_guest_pages > 0) {
745 last_page = &dev->guest_pages[dev->nr_guest_pages - 1];
746 /* merge if the two pages are continuous */
747 if (host_phys_addr == last_page->host_phys_addr +
749 last_page->size += size;
754 page = &dev->guest_pages[dev->nr_guest_pages++];
755 page->guest_phys_addr = guest_phys_addr;
756 page->host_phys_addr = host_phys_addr;
763 add_guest_pages(struct virtio_net *dev, struct rte_vhost_mem_region *reg,
766 uint64_t reg_size = reg->size;
767 uint64_t host_user_addr = reg->host_user_addr;
768 uint64_t guest_phys_addr = reg->guest_phys_addr;
769 uint64_t host_phys_addr;
772 host_phys_addr = rte_mem_virt2iova((void *)(uintptr_t)host_user_addr);
773 size = page_size - (guest_phys_addr & (page_size - 1));
774 size = RTE_MIN(size, reg_size);
776 if (add_one_guest_page(dev, guest_phys_addr, host_phys_addr, size) < 0)
779 host_user_addr += size;
780 guest_phys_addr += size;
783 while (reg_size > 0) {
784 size = RTE_MIN(reg_size, page_size);
785 host_phys_addr = rte_mem_virt2iova((void *)(uintptr_t)
787 if (add_one_guest_page(dev, guest_phys_addr, host_phys_addr,
791 host_user_addr += size;
792 guest_phys_addr += size;
799 #ifdef RTE_LIBRTE_VHOST_DEBUG
800 /* TODO: enable it only in debug mode? */
802 dump_guest_pages(struct virtio_net *dev)
805 struct guest_page *page;
807 for (i = 0; i < dev->nr_guest_pages; i++) {
808 page = &dev->guest_pages[i];
810 RTE_LOG(INFO, VHOST_CONFIG,
811 "guest physical page region %u\n"
812 "\t guest_phys_addr: %" PRIx64 "\n"
813 "\t host_phys_addr : %" PRIx64 "\n"
814 "\t size : %" PRIx64 "\n",
816 page->guest_phys_addr,
817 page->host_phys_addr,
822 #define dump_guest_pages(dev)
826 vhost_memory_changed(struct VhostUserMemory *new,
827 struct rte_vhost_memory *old)
831 if (new->nregions != old->nregions)
834 for (i = 0; i < new->nregions; ++i) {
835 VhostUserMemoryRegion *new_r = &new->regions[i];
836 struct rte_vhost_mem_region *old_r = &old->regions[i];
838 if (new_r->guest_phys_addr != old_r->guest_phys_addr)
840 if (new_r->memory_size != old_r->size)
842 if (new_r->userspace_addr != old_r->guest_user_addr)
850 vhost_user_set_mem_table(struct virtio_net **pdev, struct VhostUserMsg *msg,
853 struct virtio_net *dev = *pdev;
854 struct VhostUserMemory *memory = &msg->payload.memory;
855 struct rte_vhost_mem_region *reg;
858 uint64_t mmap_offset;
864 if (memory->nregions > VHOST_MEMORY_MAX_NREGIONS) {
865 RTE_LOG(ERR, VHOST_CONFIG,
866 "too many memory regions (%u)\n", memory->nregions);
867 return VH_RESULT_ERR;
870 if (dev->mem && !vhost_memory_changed(memory, dev->mem)) {
871 RTE_LOG(INFO, VHOST_CONFIG,
872 "(%d) memory regions not changed\n", dev->vid);
874 for (i = 0; i < memory->nregions; i++)
881 free_mem_region(dev);
886 /* Flush IOTLB cache as previous HVAs are now invalid */
887 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
888 for (i = 0; i < dev->nr_vring; i++)
889 vhost_user_iotlb_flush_all(dev->virtqueue[i]);
891 dev->nr_guest_pages = 0;
892 if (!dev->guest_pages) {
893 dev->max_guest_pages = 8;
894 dev->guest_pages = malloc(dev->max_guest_pages *
895 sizeof(struct guest_page));
896 if (dev->guest_pages == NULL) {
897 RTE_LOG(ERR, VHOST_CONFIG,
898 "(%d) failed to allocate memory "
899 "for dev->guest_pages\n",
901 return VH_RESULT_ERR;
905 dev->mem = rte_zmalloc("vhost-mem-table", sizeof(struct rte_vhost_memory) +
906 sizeof(struct rte_vhost_mem_region) * memory->nregions, 0);
907 if (dev->mem == NULL) {
908 RTE_LOG(ERR, VHOST_CONFIG,
909 "(%d) failed to allocate memory for dev->mem\n",
911 return VH_RESULT_ERR;
913 dev->mem->nregions = memory->nregions;
915 for (i = 0; i < memory->nregions; i++) {
917 reg = &dev->mem->regions[i];
919 reg->guest_phys_addr = memory->regions[i].guest_phys_addr;
920 reg->guest_user_addr = memory->regions[i].userspace_addr;
921 reg->size = memory->regions[i].memory_size;
924 mmap_offset = memory->regions[i].mmap_offset;
926 /* Check for memory_size + mmap_offset overflow */
927 if (mmap_offset >= -reg->size) {
928 RTE_LOG(ERR, VHOST_CONFIG,
929 "mmap_offset (%#"PRIx64") and memory_size "
930 "(%#"PRIx64") overflow\n",
931 mmap_offset, reg->size);
935 mmap_size = reg->size + mmap_offset;
937 /* mmap() without flag of MAP_ANONYMOUS, should be called
938 * with length argument aligned with hugepagesz at older
939 * longterm version Linux, like 2.6.32 and 3.2.72, or
940 * mmap() will fail with EINVAL.
942 * to avoid failure, make sure in caller to keep length
945 alignment = get_blk_size(fd);
946 if (alignment == (uint64_t)-1) {
947 RTE_LOG(ERR, VHOST_CONFIG,
948 "couldn't get hugepage size through fstat\n");
951 mmap_size = RTE_ALIGN_CEIL(mmap_size, alignment);
953 populate = (dev->dequeue_zero_copy) ? MAP_POPULATE : 0;
954 mmap_addr = mmap(NULL, mmap_size, PROT_READ | PROT_WRITE,
955 MAP_SHARED | populate, fd, 0);
957 if (mmap_addr == MAP_FAILED) {
958 RTE_LOG(ERR, VHOST_CONFIG,
959 "mmap region %u failed.\n", i);
963 reg->mmap_addr = mmap_addr;
964 reg->mmap_size = mmap_size;
965 reg->host_user_addr = (uint64_t)(uintptr_t)mmap_addr +
968 if (dev->dequeue_zero_copy)
969 if (add_guest_pages(dev, reg, alignment) < 0) {
970 RTE_LOG(ERR, VHOST_CONFIG,
971 "adding guest pages to region %u failed.\n",
976 RTE_LOG(INFO, VHOST_CONFIG,
977 "guest memory region %u, size: 0x%" PRIx64 "\n"
978 "\t guest physical addr: 0x%" PRIx64 "\n"
979 "\t guest virtual addr: 0x%" PRIx64 "\n"
980 "\t host virtual addr: 0x%" PRIx64 "\n"
981 "\t mmap addr : 0x%" PRIx64 "\n"
982 "\t mmap size : 0x%" PRIx64 "\n"
983 "\t mmap align: 0x%" PRIx64 "\n"
984 "\t mmap off : 0x%" PRIx64 "\n",
986 reg->guest_phys_addr,
987 reg->guest_user_addr,
989 (uint64_t)(uintptr_t)mmap_addr,
994 if (dev->postcopy_listening) {
996 * We haven't a better way right now than sharing
997 * DPDK's virtual address with Qemu, so that Qemu can
998 * retrieve the region offset when handling userfaults.
1000 memory->regions[i].userspace_addr =
1001 reg->host_user_addr;
1004 if (dev->postcopy_listening) {
1005 /* Send the addresses back to qemu */
1007 send_vhost_reply(main_fd, msg);
1009 /* Wait for qemu to acknolwedge it's got the addresses
1010 * we've got to wait before we're allowed to generate faults.
1012 VhostUserMsg ack_msg;
1013 if (read_vhost_message(main_fd, &ack_msg) <= 0) {
1014 RTE_LOG(ERR, VHOST_CONFIG,
1015 "Failed to read qemu ack on postcopy set-mem-table\n");
1018 if (ack_msg.request.master != VHOST_USER_SET_MEM_TABLE) {
1019 RTE_LOG(ERR, VHOST_CONFIG,
1020 "Bad qemu ack on postcopy set-mem-table (%d)\n",
1021 ack_msg.request.master);
1025 /* Now userfault register and we can use the memory */
1026 for (i = 0; i < memory->nregions; i++) {
1027 #ifdef RTE_LIBRTE_VHOST_POSTCOPY
1028 reg = &dev->mem->regions[i];
1029 struct uffdio_register reg_struct;
1032 * Let's register all the mmap'ed area to ensure
1033 * alignment on page boundary.
1035 reg_struct.range.start =
1036 (uint64_t)(uintptr_t)reg->mmap_addr;
1037 reg_struct.range.len = reg->mmap_size;
1038 reg_struct.mode = UFFDIO_REGISTER_MODE_MISSING;
1040 if (ioctl(dev->postcopy_ufd, UFFDIO_REGISTER,
1042 RTE_LOG(ERR, VHOST_CONFIG,
1043 "Failed to register ufd for region %d: (ufd = %d) %s\n",
1044 i, dev->postcopy_ufd,
1048 RTE_LOG(INFO, VHOST_CONFIG,
1049 "\t userfaultfd registered for range : %llx - %llx\n",
1050 reg_struct.range.start,
1051 reg_struct.range.start +
1052 reg_struct.range.len - 1);
1059 for (i = 0; i < dev->nr_vring; i++) {
1060 struct vhost_virtqueue *vq = dev->virtqueue[i];
1062 if (vq->desc || vq->avail || vq->used) {
1064 * If the memory table got updated, the ring addresses
1065 * need to be translated again as virtual addresses have
1068 vring_invalidate(dev, vq);
1070 dev = translate_ring_addresses(dev, i);
1080 dump_guest_pages(dev);
1082 return VH_RESULT_OK;
1085 free_mem_region(dev);
1088 return VH_RESULT_ERR;
1092 vq_is_ready(struct virtio_net *dev, struct vhost_virtqueue *vq)
1099 if (vq_is_packed(dev))
1100 rings_ok = !!vq->desc_packed;
1102 rings_ok = vq->desc && vq->avail && vq->used;
1105 vq->kickfd != VIRTIO_UNINITIALIZED_EVENTFD &&
1106 vq->callfd != VIRTIO_UNINITIALIZED_EVENTFD;
1110 virtio_is_ready(struct virtio_net *dev)
1112 struct vhost_virtqueue *vq;
1115 if (dev->nr_vring == 0)
1118 for (i = 0; i < dev->nr_vring; i++) {
1119 vq = dev->virtqueue[i];
1121 if (!vq_is_ready(dev, vq))
1125 RTE_LOG(INFO, VHOST_CONFIG,
1126 "virtio is now ready for processing.\n");
1131 vhost_user_set_vring_call(struct virtio_net **pdev, struct VhostUserMsg *msg,
1132 int main_fd __rte_unused)
1134 struct virtio_net *dev = *pdev;
1135 struct vhost_vring_file file;
1136 struct vhost_virtqueue *vq;
1138 file.index = msg->payload.u64 & VHOST_USER_VRING_IDX_MASK;
1139 if (msg->payload.u64 & VHOST_USER_VRING_NOFD_MASK)
1140 file.fd = VIRTIO_INVALID_EVENTFD;
1142 file.fd = msg->fds[0];
1143 RTE_LOG(INFO, VHOST_CONFIG,
1144 "vring call idx:%d file:%d\n", file.index, file.fd);
1146 vq = dev->virtqueue[file.index];
1147 if (vq->callfd >= 0)
1150 vq->callfd = file.fd;
1152 return VH_RESULT_OK;
1155 static int vhost_user_set_vring_err(struct virtio_net **pdev __rte_unused,
1156 struct VhostUserMsg *msg,
1157 int main_fd __rte_unused)
1159 if (!(msg->payload.u64 & VHOST_USER_VRING_NOFD_MASK))
1161 RTE_LOG(INFO, VHOST_CONFIG, "not implemented\n");
1163 return VH_RESULT_OK;
1167 vhost_user_set_vring_kick(struct virtio_net **pdev, struct VhostUserMsg *msg,
1168 int main_fd __rte_unused)
1170 struct virtio_net *dev = *pdev;
1171 struct vhost_vring_file file;
1172 struct vhost_virtqueue *vq;
1174 file.index = msg->payload.u64 & VHOST_USER_VRING_IDX_MASK;
1175 if (msg->payload.u64 & VHOST_USER_VRING_NOFD_MASK)
1176 file.fd = VIRTIO_INVALID_EVENTFD;
1178 file.fd = msg->fds[0];
1179 RTE_LOG(INFO, VHOST_CONFIG,
1180 "vring kick idx:%d file:%d\n", file.index, file.fd);
1182 /* Interpret ring addresses only when ring is started. */
1183 dev = translate_ring_addresses(dev, file.index);
1185 return VH_RESULT_ERR;
1189 vq = dev->virtqueue[file.index];
1192 * When VHOST_USER_F_PROTOCOL_FEATURES is not negotiated,
1193 * the ring starts already enabled. Otherwise, it is enabled via
1194 * the SET_VRING_ENABLE message.
1196 if (!(dev->features & (1ULL << VHOST_USER_F_PROTOCOL_FEATURES)))
1199 if (vq->kickfd >= 0)
1201 vq->kickfd = file.fd;
1203 return VH_RESULT_OK;
1207 free_zmbufs(struct vhost_virtqueue *vq)
1209 struct zcopy_mbuf *zmbuf, *next;
1211 for (zmbuf = TAILQ_FIRST(&vq->zmbuf_list);
1212 zmbuf != NULL; zmbuf = next) {
1213 next = TAILQ_NEXT(zmbuf, next);
1215 rte_pktmbuf_free(zmbuf->mbuf);
1216 TAILQ_REMOVE(&vq->zmbuf_list, zmbuf, next);
1219 rte_free(vq->zmbufs);
1223 * when virtio is stopped, qemu will send us the GET_VRING_BASE message.
1226 vhost_user_get_vring_base(struct virtio_net **pdev,
1227 struct VhostUserMsg *msg,
1228 int main_fd __rte_unused)
1230 struct virtio_net *dev = *pdev;
1231 struct vhost_virtqueue *vq = dev->virtqueue[msg->payload.state.index];
1234 /* We have to stop the queue (virtio) if it is running. */
1235 vhost_destroy_device_notify(dev);
1237 dev->flags &= ~VIRTIO_DEV_READY;
1238 dev->flags &= ~VIRTIO_DEV_VDPA_CONFIGURED;
1240 /* Here we are safe to get the indexes */
1241 if (vq_is_packed(dev)) {
1243 * Bit[0:14]: avail index
1244 * Bit[15]: avail wrap counter
1246 val = vq->last_avail_idx & 0x7fff;
1247 val |= vq->avail_wrap_counter << 15;
1248 msg->payload.state.num = val;
1250 msg->payload.state.num = vq->last_avail_idx;
1253 RTE_LOG(INFO, VHOST_CONFIG,
1254 "vring base idx:%d file:%d\n", msg->payload.state.index,
1255 msg->payload.state.num);
1257 * Based on current qemu vhost-user implementation, this message is
1258 * sent and only sent in vhost_vring_stop.
1259 * TODO: cleanup the vring, it isn't usable since here.
1261 if (vq->kickfd >= 0)
1264 vq->kickfd = VIRTIO_UNINITIALIZED_EVENTFD;
1266 if (vq->callfd >= 0)
1269 vq->callfd = VIRTIO_UNINITIALIZED_EVENTFD;
1271 if (dev->dequeue_zero_copy)
1273 if (vq_is_packed(dev)) {
1274 rte_free(vq->shadow_used_packed);
1275 vq->shadow_used_packed = NULL;
1277 rte_free(vq->shadow_used_split);
1278 vq->shadow_used_split = NULL;
1281 rte_free(vq->batch_copy_elems);
1282 vq->batch_copy_elems = NULL;
1284 msg->size = sizeof(msg->payload.state);
1287 return VH_RESULT_REPLY;
1291 * when virtio queues are ready to work, qemu will send us to
1292 * enable the virtio queue pair.
1295 vhost_user_set_vring_enable(struct virtio_net **pdev,
1296 struct VhostUserMsg *msg,
1297 int main_fd __rte_unused)
1299 struct virtio_net *dev = *pdev;
1300 int enable = (int)msg->payload.state.num;
1301 int index = (int)msg->payload.state.index;
1302 struct rte_vdpa_device *vdpa_dev;
1305 RTE_LOG(INFO, VHOST_CONFIG,
1306 "set queue enable: %d to qp idx: %d\n",
1309 did = dev->vdpa_dev_id;
1310 vdpa_dev = rte_vdpa_get_device(did);
1311 if (vdpa_dev && vdpa_dev->ops->set_vring_state)
1312 vdpa_dev->ops->set_vring_state(dev->vid, index, enable);
1314 if (dev->notify_ops->vring_state_changed)
1315 dev->notify_ops->vring_state_changed(dev->vid,
1318 dev->virtqueue[index]->enabled = enable;
1320 return VH_RESULT_OK;
1324 vhost_user_get_protocol_features(struct virtio_net **pdev,
1325 struct VhostUserMsg *msg,
1326 int main_fd __rte_unused)
1328 struct virtio_net *dev = *pdev;
1329 uint64_t features, protocol_features;
1331 rte_vhost_driver_get_features(dev->ifname, &features);
1332 rte_vhost_driver_get_protocol_features(dev->ifname, &protocol_features);
1335 * REPLY_ACK protocol feature is only mandatory for now
1336 * for IOMMU feature. If IOMMU is explicitly disabled by the
1337 * application, disable also REPLY_ACK feature for older buggy
1338 * Qemu versions (from v2.7.0 to v2.9.0).
1340 if (!(features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)))
1341 protocol_features &= ~(1ULL << VHOST_USER_PROTOCOL_F_REPLY_ACK);
1343 msg->payload.u64 = protocol_features;
1344 msg->size = sizeof(msg->payload.u64);
1347 return VH_RESULT_REPLY;
1351 vhost_user_set_protocol_features(struct virtio_net **pdev,
1352 struct VhostUserMsg *msg,
1353 int main_fd __rte_unused)
1355 struct virtio_net *dev = *pdev;
1356 uint64_t protocol_features = msg->payload.u64;
1357 uint64_t slave_protocol_features = 0;
1359 rte_vhost_driver_get_protocol_features(dev->ifname,
1360 &slave_protocol_features);
1361 if (protocol_features & ~slave_protocol_features) {
1362 RTE_LOG(ERR, VHOST_CONFIG,
1363 "(%d) received invalid protocol features.\n",
1365 return VH_RESULT_ERR;
1368 dev->protocol_features = protocol_features;
1370 return VH_RESULT_OK;
1374 vhost_user_set_log_base(struct virtio_net **pdev, struct VhostUserMsg *msg,
1375 int main_fd __rte_unused)
1377 struct virtio_net *dev = *pdev;
1378 int fd = msg->fds[0];
1383 RTE_LOG(ERR, VHOST_CONFIG, "invalid log fd: %d\n", fd);
1384 return VH_RESULT_ERR;
1387 if (msg->size != sizeof(VhostUserLog)) {
1388 RTE_LOG(ERR, VHOST_CONFIG,
1389 "invalid log base msg size: %"PRId32" != %d\n",
1390 msg->size, (int)sizeof(VhostUserLog));
1391 return VH_RESULT_ERR;
1394 size = msg->payload.log.mmap_size;
1395 off = msg->payload.log.mmap_offset;
1397 /* Don't allow mmap_offset to point outside the mmap region */
1399 RTE_LOG(ERR, VHOST_CONFIG,
1400 "log offset %#"PRIx64" exceeds log size %#"PRIx64"\n",
1402 return VH_RESULT_ERR;
1405 RTE_LOG(INFO, VHOST_CONFIG,
1406 "log mmap size: %"PRId64", offset: %"PRId64"\n",
1410 * mmap from 0 to workaround a hugepage mmap bug: mmap will
1411 * fail when offset is not page size aligned.
1413 addr = mmap(0, size + off, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
1415 if (addr == MAP_FAILED) {
1416 RTE_LOG(ERR, VHOST_CONFIG, "mmap log base failed!\n");
1417 return VH_RESULT_ERR;
1421 * Free previously mapped log memory on occasionally
1422 * multiple VHOST_USER_SET_LOG_BASE.
1424 if (dev->log_addr) {
1425 munmap((void *)(uintptr_t)dev->log_addr, dev->log_size);
1427 dev->log_addr = (uint64_t)(uintptr_t)addr;
1428 dev->log_base = dev->log_addr + off;
1429 dev->log_size = size;
1432 * The spec is not clear about it (yet), but QEMU doesn't expect
1433 * any payload in the reply.
1438 return VH_RESULT_REPLY;
1441 static int vhost_user_set_log_fd(struct virtio_net **pdev __rte_unused,
1442 struct VhostUserMsg *msg,
1443 int main_fd __rte_unused)
1446 RTE_LOG(INFO, VHOST_CONFIG, "not implemented.\n");
1448 return VH_RESULT_OK;
1452 * An rarp packet is constructed and broadcasted to notify switches about
1453 * the new location of the migrated VM, so that packets from outside will
1454 * not be lost after migration.
1456 * However, we don't actually "send" a rarp packet here, instead, we set
1457 * a flag 'broadcast_rarp' to let rte_vhost_dequeue_burst() inject it.
1460 vhost_user_send_rarp(struct virtio_net **pdev, struct VhostUserMsg *msg,
1461 int main_fd __rte_unused)
1463 struct virtio_net *dev = *pdev;
1464 uint8_t *mac = (uint8_t *)&msg->payload.u64;
1465 struct rte_vdpa_device *vdpa_dev;
1468 RTE_LOG(DEBUG, VHOST_CONFIG,
1469 ":: mac: %02x:%02x:%02x:%02x:%02x:%02x\n",
1470 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
1471 memcpy(dev->mac.addr_bytes, mac, 6);
1474 * Set the flag to inject a RARP broadcast packet at
1475 * rte_vhost_dequeue_burst().
1477 * rte_smp_wmb() is for making sure the mac is copied
1478 * before the flag is set.
1481 rte_atomic16_set(&dev->broadcast_rarp, 1);
1482 did = dev->vdpa_dev_id;
1483 vdpa_dev = rte_vdpa_get_device(did);
1484 if (vdpa_dev && vdpa_dev->ops->migration_done)
1485 vdpa_dev->ops->migration_done(dev->vid);
1487 return VH_RESULT_OK;
1491 vhost_user_net_set_mtu(struct virtio_net **pdev, struct VhostUserMsg *msg,
1492 int main_fd __rte_unused)
1494 struct virtio_net *dev = *pdev;
1495 if (msg->payload.u64 < VIRTIO_MIN_MTU ||
1496 msg->payload.u64 > VIRTIO_MAX_MTU) {
1497 RTE_LOG(ERR, VHOST_CONFIG, "Invalid MTU size (%"PRIu64")\n",
1500 return VH_RESULT_ERR;
1503 dev->mtu = msg->payload.u64;
1505 return VH_RESULT_OK;
1509 vhost_user_set_req_fd(struct virtio_net **pdev, struct VhostUserMsg *msg,
1510 int main_fd __rte_unused)
1512 struct virtio_net *dev = *pdev;
1513 int fd = msg->fds[0];
1516 RTE_LOG(ERR, VHOST_CONFIG,
1517 "Invalid file descriptor for slave channel (%d)\n",
1519 return VH_RESULT_ERR;
1522 dev->slave_req_fd = fd;
1524 return VH_RESULT_OK;
1528 is_vring_iotlb_update(struct vhost_virtqueue *vq, struct vhost_iotlb_msg *imsg)
1530 struct vhost_vring_addr *ra;
1531 uint64_t start, end;
1534 end = start + imsg->size;
1536 ra = &vq->ring_addrs;
1537 if (ra->desc_user_addr >= start && ra->desc_user_addr < end)
1539 if (ra->avail_user_addr >= start && ra->avail_user_addr < end)
1541 if (ra->used_user_addr >= start && ra->used_user_addr < end)
1548 is_vring_iotlb_invalidate(struct vhost_virtqueue *vq,
1549 struct vhost_iotlb_msg *imsg)
1551 uint64_t istart, iend, vstart, vend;
1553 istart = imsg->iova;
1554 iend = istart + imsg->size - 1;
1556 vstart = (uintptr_t)vq->desc;
1557 vend = vstart + sizeof(struct vring_desc) * vq->size - 1;
1558 if (vstart <= iend && istart <= vend)
1561 vstart = (uintptr_t)vq->avail;
1562 vend = vstart + sizeof(struct vring_avail);
1563 vend += sizeof(uint16_t) * vq->size - 1;
1564 if (vstart <= iend && istart <= vend)
1567 vstart = (uintptr_t)vq->used;
1568 vend = vstart + sizeof(struct vring_used);
1569 vend += sizeof(struct vring_used_elem) * vq->size - 1;
1570 if (vstart <= iend && istart <= vend)
1577 vhost_user_iotlb_msg(struct virtio_net **pdev, struct VhostUserMsg *msg,
1578 int main_fd __rte_unused)
1580 struct virtio_net *dev = *pdev;
1581 struct vhost_iotlb_msg *imsg = &msg->payload.iotlb;
1585 switch (imsg->type) {
1586 case VHOST_IOTLB_UPDATE:
1588 vva = qva_to_vva(dev, imsg->uaddr, &len);
1590 return VH_RESULT_ERR;
1592 for (i = 0; i < dev->nr_vring; i++) {
1593 struct vhost_virtqueue *vq = dev->virtqueue[i];
1595 vhost_user_iotlb_cache_insert(vq, imsg->iova, vva,
1598 if (is_vring_iotlb_update(vq, imsg))
1599 *pdev = dev = translate_ring_addresses(dev, i);
1602 case VHOST_IOTLB_INVALIDATE:
1603 for (i = 0; i < dev->nr_vring; i++) {
1604 struct vhost_virtqueue *vq = dev->virtqueue[i];
1606 vhost_user_iotlb_cache_remove(vq, imsg->iova,
1609 if (is_vring_iotlb_invalidate(vq, imsg))
1610 vring_invalidate(dev, vq);
1614 RTE_LOG(ERR, VHOST_CONFIG, "Invalid IOTLB message type (%d)\n",
1616 return VH_RESULT_ERR;
1619 return VH_RESULT_OK;
1623 vhost_user_set_postcopy_advise(struct virtio_net **pdev,
1624 struct VhostUserMsg *msg,
1625 int main_fd __rte_unused)
1627 struct virtio_net *dev = *pdev;
1628 #ifdef RTE_LIBRTE_VHOST_POSTCOPY
1629 struct uffdio_api api_struct;
1631 dev->postcopy_ufd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK);
1633 if (dev->postcopy_ufd == -1) {
1634 RTE_LOG(ERR, VHOST_CONFIG, "Userfaultfd not available: %s\n",
1636 return VH_RESULT_ERR;
1638 api_struct.api = UFFD_API;
1639 api_struct.features = 0;
1640 if (ioctl(dev->postcopy_ufd, UFFDIO_API, &api_struct)) {
1641 RTE_LOG(ERR, VHOST_CONFIG, "UFFDIO_API ioctl failure: %s\n",
1643 close(dev->postcopy_ufd);
1644 dev->postcopy_ufd = -1;
1645 return VH_RESULT_ERR;
1647 msg->fds[0] = dev->postcopy_ufd;
1650 return VH_RESULT_REPLY;
1652 dev->postcopy_ufd = -1;
1655 return VH_RESULT_ERR;
1660 vhost_user_set_postcopy_listen(struct virtio_net **pdev,
1661 struct VhostUserMsg *msg __rte_unused,
1662 int main_fd __rte_unused)
1664 struct virtio_net *dev = *pdev;
1666 if (dev->mem && dev->mem->nregions) {
1667 RTE_LOG(ERR, VHOST_CONFIG,
1668 "Regions already registered at postcopy-listen\n");
1669 return VH_RESULT_ERR;
1671 dev->postcopy_listening = 1;
1673 return VH_RESULT_OK;
1677 vhost_user_postcopy_end(struct virtio_net **pdev, struct VhostUserMsg *msg,
1678 int main_fd __rte_unused)
1680 struct virtio_net *dev = *pdev;
1682 dev->postcopy_listening = 0;
1683 if (dev->postcopy_ufd >= 0) {
1684 close(dev->postcopy_ufd);
1685 dev->postcopy_ufd = -1;
1688 msg->payload.u64 = 0;
1689 msg->size = sizeof(msg->payload.u64);
1692 return VH_RESULT_REPLY;
1695 typedef int (*vhost_message_handler_t)(struct virtio_net **pdev,
1696 struct VhostUserMsg *msg,
1698 static vhost_message_handler_t vhost_message_handlers[VHOST_USER_MAX] = {
1699 [VHOST_USER_NONE] = NULL,
1700 [VHOST_USER_GET_FEATURES] = vhost_user_get_features,
1701 [VHOST_USER_SET_FEATURES] = vhost_user_set_features,
1702 [VHOST_USER_SET_OWNER] = vhost_user_set_owner,
1703 [VHOST_USER_RESET_OWNER] = vhost_user_reset_owner,
1704 [VHOST_USER_SET_MEM_TABLE] = vhost_user_set_mem_table,
1705 [VHOST_USER_SET_LOG_BASE] = vhost_user_set_log_base,
1706 [VHOST_USER_SET_LOG_FD] = vhost_user_set_log_fd,
1707 [VHOST_USER_SET_VRING_NUM] = vhost_user_set_vring_num,
1708 [VHOST_USER_SET_VRING_ADDR] = vhost_user_set_vring_addr,
1709 [VHOST_USER_SET_VRING_BASE] = vhost_user_set_vring_base,
1710 [VHOST_USER_GET_VRING_BASE] = vhost_user_get_vring_base,
1711 [VHOST_USER_SET_VRING_KICK] = vhost_user_set_vring_kick,
1712 [VHOST_USER_SET_VRING_CALL] = vhost_user_set_vring_call,
1713 [VHOST_USER_SET_VRING_ERR] = vhost_user_set_vring_err,
1714 [VHOST_USER_GET_PROTOCOL_FEATURES] = vhost_user_get_protocol_features,
1715 [VHOST_USER_SET_PROTOCOL_FEATURES] = vhost_user_set_protocol_features,
1716 [VHOST_USER_GET_QUEUE_NUM] = vhost_user_get_queue_num,
1717 [VHOST_USER_SET_VRING_ENABLE] = vhost_user_set_vring_enable,
1718 [VHOST_USER_SEND_RARP] = vhost_user_send_rarp,
1719 [VHOST_USER_NET_SET_MTU] = vhost_user_net_set_mtu,
1720 [VHOST_USER_SET_SLAVE_REQ_FD] = vhost_user_set_req_fd,
1721 [VHOST_USER_IOTLB_MSG] = vhost_user_iotlb_msg,
1722 [VHOST_USER_POSTCOPY_ADVISE] = vhost_user_set_postcopy_advise,
1723 [VHOST_USER_POSTCOPY_LISTEN] = vhost_user_set_postcopy_listen,
1724 [VHOST_USER_POSTCOPY_END] = vhost_user_postcopy_end,
1728 /* return bytes# of read on success or negative val on failure. */
1730 read_vhost_message(int sockfd, struct VhostUserMsg *msg)
1734 ret = read_fd_message(sockfd, (char *)msg, VHOST_USER_HDR_SIZE,
1735 msg->fds, VHOST_MEMORY_MAX_NREGIONS, &msg->fd_num);
1740 if (msg->size > sizeof(msg->payload)) {
1741 RTE_LOG(ERR, VHOST_CONFIG,
1742 "invalid msg size: %d\n", msg->size);
1745 ret = read(sockfd, &msg->payload, msg->size);
1748 if (ret != (int)msg->size) {
1749 RTE_LOG(ERR, VHOST_CONFIG,
1750 "read control message failed\n");
1759 send_vhost_message(int sockfd, struct VhostUserMsg *msg)
1764 return send_fd_message(sockfd, (char *)msg,
1765 VHOST_USER_HDR_SIZE + msg->size, msg->fds, msg->fd_num);
1769 send_vhost_reply(int sockfd, struct VhostUserMsg *msg)
1774 msg->flags &= ~VHOST_USER_VERSION_MASK;
1775 msg->flags &= ~VHOST_USER_NEED_REPLY;
1776 msg->flags |= VHOST_USER_VERSION;
1777 msg->flags |= VHOST_USER_REPLY_MASK;
1779 return send_vhost_message(sockfd, msg);
1783 send_vhost_slave_message(struct virtio_net *dev, struct VhostUserMsg *msg)
1787 if (msg->flags & VHOST_USER_NEED_REPLY)
1788 rte_spinlock_lock(&dev->slave_req_lock);
1790 ret = send_vhost_message(dev->slave_req_fd, msg);
1791 if (ret < 0 && (msg->flags & VHOST_USER_NEED_REPLY))
1792 rte_spinlock_unlock(&dev->slave_req_lock);
1798 * Allocate a queue pair if it hasn't been allocated yet
1801 vhost_user_check_and_alloc_queue_pair(struct virtio_net *dev,
1802 struct VhostUserMsg *msg)
1806 switch (msg->request.master) {
1807 case VHOST_USER_SET_VRING_KICK:
1808 case VHOST_USER_SET_VRING_CALL:
1809 case VHOST_USER_SET_VRING_ERR:
1810 vring_idx = msg->payload.u64 & VHOST_USER_VRING_IDX_MASK;
1812 case VHOST_USER_SET_VRING_NUM:
1813 case VHOST_USER_SET_VRING_BASE:
1814 case VHOST_USER_SET_VRING_ENABLE:
1815 vring_idx = msg->payload.state.index;
1817 case VHOST_USER_SET_VRING_ADDR:
1818 vring_idx = msg->payload.addr.index;
1824 if (vring_idx >= VHOST_MAX_VRING) {
1825 RTE_LOG(ERR, VHOST_CONFIG,
1826 "invalid vring index: %u\n", vring_idx);
1830 if (dev->virtqueue[vring_idx])
1833 return alloc_vring_queue(dev, vring_idx);
1837 vhost_user_lock_all_queue_pairs(struct virtio_net *dev)
1840 unsigned int vq_num = 0;
1842 while (vq_num < dev->nr_vring) {
1843 struct vhost_virtqueue *vq = dev->virtqueue[i];
1846 rte_spinlock_lock(&vq->access_lock);
1854 vhost_user_unlock_all_queue_pairs(struct virtio_net *dev)
1857 unsigned int vq_num = 0;
1859 while (vq_num < dev->nr_vring) {
1860 struct vhost_virtqueue *vq = dev->virtqueue[i];
1863 rte_spinlock_unlock(&vq->access_lock);
1871 vhost_user_msg_handler(int vid, int fd)
1873 struct virtio_net *dev;
1874 struct VhostUserMsg msg;
1875 struct rte_vdpa_device *vdpa_dev;
1878 int unlock_required = 0;
1879 uint32_t skip_master = 0;
1882 dev = get_device(vid);
1886 if (!dev->notify_ops) {
1887 dev->notify_ops = vhost_driver_callback_get(dev->ifname);
1888 if (!dev->notify_ops) {
1889 RTE_LOG(ERR, VHOST_CONFIG,
1890 "failed to get callback ops for driver %s\n",
1896 ret = read_vhost_message(fd, &msg);
1897 if (ret <= 0 || msg.request.master >= VHOST_USER_MAX) {
1899 RTE_LOG(ERR, VHOST_CONFIG,
1900 "vhost read message failed\n");
1902 RTE_LOG(INFO, VHOST_CONFIG,
1903 "vhost peer closed\n");
1905 RTE_LOG(ERR, VHOST_CONFIG,
1906 "vhost read incorrect message\n");
1912 if (msg.request.master != VHOST_USER_IOTLB_MSG)
1913 RTE_LOG(INFO, VHOST_CONFIG, "read message %s\n",
1914 vhost_message_str[msg.request.master]);
1916 RTE_LOG(DEBUG, VHOST_CONFIG, "read message %s\n",
1917 vhost_message_str[msg.request.master]);
1919 ret = vhost_user_check_and_alloc_queue_pair(dev, &msg);
1921 RTE_LOG(ERR, VHOST_CONFIG,
1922 "failed to alloc queue\n");
1927 * Note: we don't lock all queues on VHOST_USER_GET_VRING_BASE
1928 * and VHOST_USER_RESET_OWNER, since it is sent when virtio stops
1929 * and device is destroyed. destroy_device waits for queues to be
1930 * inactive, so it is safe. Otherwise taking the access_lock
1931 * would cause a dead lock.
1933 switch (msg.request.master) {
1934 case VHOST_USER_SET_FEATURES:
1935 case VHOST_USER_SET_PROTOCOL_FEATURES:
1936 case VHOST_USER_SET_OWNER:
1937 case VHOST_USER_SET_MEM_TABLE:
1938 case VHOST_USER_SET_LOG_BASE:
1939 case VHOST_USER_SET_LOG_FD:
1940 case VHOST_USER_SET_VRING_NUM:
1941 case VHOST_USER_SET_VRING_ADDR:
1942 case VHOST_USER_SET_VRING_BASE:
1943 case VHOST_USER_SET_VRING_KICK:
1944 case VHOST_USER_SET_VRING_CALL:
1945 case VHOST_USER_SET_VRING_ERR:
1946 case VHOST_USER_SET_VRING_ENABLE:
1947 case VHOST_USER_SEND_RARP:
1948 case VHOST_USER_NET_SET_MTU:
1949 case VHOST_USER_SET_SLAVE_REQ_FD:
1950 vhost_user_lock_all_queue_pairs(dev);
1951 unlock_required = 1;
1958 if (dev->extern_ops.pre_msg_handle) {
1959 ret = (*dev->extern_ops.pre_msg_handle)(dev->vid,
1960 (void *)&msg, &skip_master);
1961 if (ret == VH_RESULT_ERR)
1963 else if (ret == VH_RESULT_REPLY)
1964 send_vhost_reply(fd, &msg);
1967 goto skip_to_post_handle;
1970 request = msg.request.master;
1971 if (request > VHOST_USER_NONE && request < VHOST_USER_MAX) {
1972 if (!vhost_message_handlers[request])
1973 goto skip_to_post_handle;
1974 ret = vhost_message_handlers[request](&dev, &msg, fd);
1978 RTE_LOG(ERR, VHOST_CONFIG,
1979 "Processing %s failed.\n",
1980 vhost_message_str[request]);
1983 RTE_LOG(DEBUG, VHOST_CONFIG,
1984 "Processing %s succeeded.\n",
1985 vhost_message_str[request]);
1987 case VH_RESULT_REPLY:
1988 RTE_LOG(DEBUG, VHOST_CONFIG,
1989 "Processing %s succeeded and needs reply.\n",
1990 vhost_message_str[request]);
1991 send_vhost_reply(fd, &msg);
1995 RTE_LOG(ERR, VHOST_CONFIG,
1996 "Requested invalid message type %d.\n", request);
1997 ret = VH_RESULT_ERR;
2000 skip_to_post_handle:
2001 if (ret != VH_RESULT_ERR && dev->extern_ops.post_msg_handle) {
2002 ret = (*dev->extern_ops.post_msg_handle)(
2003 dev->vid, (void *)&msg);
2004 if (ret == VH_RESULT_ERR)
2006 else if (ret == VH_RESULT_REPLY)
2007 send_vhost_reply(fd, &msg);
2011 if (unlock_required)
2012 vhost_user_unlock_all_queue_pairs(dev);
2015 * If the request required a reply that was already sent,
2016 * this optional reply-ack won't be sent as the
2017 * VHOST_USER_NEED_REPLY was cleared in send_vhost_reply().
2019 if (msg.flags & VHOST_USER_NEED_REPLY) {
2020 msg.payload.u64 = ret == VH_RESULT_ERR;
2021 msg.size = sizeof(msg.payload.u64);
2023 send_vhost_reply(fd, &msg);
2024 } else if (ret == VH_RESULT_ERR) {
2025 RTE_LOG(ERR, VHOST_CONFIG,
2026 "vhost message handling failed.\n");
2030 if (!(dev->flags & VIRTIO_DEV_RUNNING) && virtio_is_ready(dev)) {
2031 dev->flags |= VIRTIO_DEV_READY;
2033 if (!(dev->flags & VIRTIO_DEV_RUNNING)) {
2034 if (dev->dequeue_zero_copy) {
2035 RTE_LOG(INFO, VHOST_CONFIG,
2036 "dequeue zero copy is enabled\n");
2039 if (dev->notify_ops->new_device(dev->vid) == 0)
2040 dev->flags |= VIRTIO_DEV_RUNNING;
2044 did = dev->vdpa_dev_id;
2045 vdpa_dev = rte_vdpa_get_device(did);
2046 if (vdpa_dev && virtio_is_ready(dev) &&
2047 !(dev->flags & VIRTIO_DEV_VDPA_CONFIGURED) &&
2048 msg.request.master == VHOST_USER_SET_VRING_ENABLE) {
2049 if (vdpa_dev->ops->dev_conf)
2050 vdpa_dev->ops->dev_conf(dev->vid);
2051 dev->flags |= VIRTIO_DEV_VDPA_CONFIGURED;
2057 static int process_slave_message_reply(struct virtio_net *dev,
2058 const struct VhostUserMsg *msg)
2060 struct VhostUserMsg msg_reply;
2063 if ((msg->flags & VHOST_USER_NEED_REPLY) == 0)
2066 if (read_vhost_message(dev->slave_req_fd, &msg_reply) < 0) {
2071 if (msg_reply.request.slave != msg->request.slave) {
2072 RTE_LOG(ERR, VHOST_CONFIG,
2073 "Received unexpected msg type (%u), expected %u\n",
2074 msg_reply.request.slave, msg->request.slave);
2079 ret = msg_reply.payload.u64 ? -1 : 0;
2082 rte_spinlock_unlock(&dev->slave_req_lock);
2087 vhost_user_iotlb_miss(struct virtio_net *dev, uint64_t iova, uint8_t perm)
2090 struct VhostUserMsg msg = {
2091 .request.slave = VHOST_USER_SLAVE_IOTLB_MSG,
2092 .flags = VHOST_USER_VERSION,
2093 .size = sizeof(msg.payload.iotlb),
2097 .type = VHOST_IOTLB_MISS,
2101 ret = send_vhost_message(dev->slave_req_fd, &msg);
2103 RTE_LOG(ERR, VHOST_CONFIG,
2104 "Failed to send IOTLB miss message (%d)\n",
2112 static int vhost_user_slave_set_vring_host_notifier(struct virtio_net *dev,
2118 struct VhostUserMsg msg = {
2119 .request.slave = VHOST_USER_SLAVE_VRING_HOST_NOTIFIER_MSG,
2120 .flags = VHOST_USER_VERSION | VHOST_USER_NEED_REPLY,
2121 .size = sizeof(msg.payload.area),
2123 .u64 = index & VHOST_USER_VRING_IDX_MASK,
2130 msg.payload.area.u64 |= VHOST_USER_VRING_NOFD_MASK;
2136 ret = send_vhost_slave_message(dev, &msg);
2138 RTE_LOG(ERR, VHOST_CONFIG,
2139 "Failed to set host notifier (%d)\n", ret);
2143 return process_slave_message_reply(dev, &msg);
2146 int rte_vhost_host_notifier_ctrl(int vid, bool enable)
2148 struct virtio_net *dev;
2149 struct rte_vdpa_device *vdpa_dev;
2150 int vfio_device_fd, did, ret = 0;
2151 uint64_t offset, size;
2154 dev = get_device(vid);
2158 did = dev->vdpa_dev_id;
2162 if (!(dev->features & (1ULL << VIRTIO_F_VERSION_1)) ||
2163 !(dev->features & (1ULL << VHOST_USER_F_PROTOCOL_FEATURES)) ||
2164 !(dev->protocol_features &
2165 (1ULL << VHOST_USER_PROTOCOL_F_SLAVE_REQ)) ||
2166 !(dev->protocol_features &
2167 (1ULL << VHOST_USER_PROTOCOL_F_SLAVE_SEND_FD)) ||
2168 !(dev->protocol_features &
2169 (1ULL << VHOST_USER_PROTOCOL_F_HOST_NOTIFIER)))
2172 vdpa_dev = rte_vdpa_get_device(did);
2176 RTE_FUNC_PTR_OR_ERR_RET(vdpa_dev->ops->get_vfio_device_fd, -ENOTSUP);
2177 RTE_FUNC_PTR_OR_ERR_RET(vdpa_dev->ops->get_notify_area, -ENOTSUP);
2179 vfio_device_fd = vdpa_dev->ops->get_vfio_device_fd(vid);
2180 if (vfio_device_fd < 0)
2184 for (i = 0; i < dev->nr_vring; i++) {
2185 if (vdpa_dev->ops->get_notify_area(vid, i, &offset,
2191 if (vhost_user_slave_set_vring_host_notifier(dev, i,
2192 vfio_device_fd, offset, size) < 0) {
2199 for (i = 0; i < dev->nr_vring; i++) {
2200 vhost_user_slave_set_vring_host_notifier(dev, i, -1,