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>
40 #ifdef F_ADD_SEALS /* if file sealing is supported, so is memfd */
41 #include <linux/memfd.h>
42 #define MEMFD_SUPPORTED
45 #include <rte_common.h>
46 #include <rte_malloc.h>
51 #include "vhost_user.h"
53 #define VIRTIO_MIN_MTU 68
54 #define VIRTIO_MAX_MTU 65535
56 #define INFLIGHT_ALIGNMENT 64
57 #define INFLIGHT_VERSION 0x1
59 static const char *vhost_message_str[VHOST_USER_MAX] = {
60 [VHOST_USER_NONE] = "VHOST_USER_NONE",
61 [VHOST_USER_GET_FEATURES] = "VHOST_USER_GET_FEATURES",
62 [VHOST_USER_SET_FEATURES] = "VHOST_USER_SET_FEATURES",
63 [VHOST_USER_SET_OWNER] = "VHOST_USER_SET_OWNER",
64 [VHOST_USER_RESET_OWNER] = "VHOST_USER_RESET_OWNER",
65 [VHOST_USER_SET_MEM_TABLE] = "VHOST_USER_SET_MEM_TABLE",
66 [VHOST_USER_SET_LOG_BASE] = "VHOST_USER_SET_LOG_BASE",
67 [VHOST_USER_SET_LOG_FD] = "VHOST_USER_SET_LOG_FD",
68 [VHOST_USER_SET_VRING_NUM] = "VHOST_USER_SET_VRING_NUM",
69 [VHOST_USER_SET_VRING_ADDR] = "VHOST_USER_SET_VRING_ADDR",
70 [VHOST_USER_SET_VRING_BASE] = "VHOST_USER_SET_VRING_BASE",
71 [VHOST_USER_GET_VRING_BASE] = "VHOST_USER_GET_VRING_BASE",
72 [VHOST_USER_SET_VRING_KICK] = "VHOST_USER_SET_VRING_KICK",
73 [VHOST_USER_SET_VRING_CALL] = "VHOST_USER_SET_VRING_CALL",
74 [VHOST_USER_SET_VRING_ERR] = "VHOST_USER_SET_VRING_ERR",
75 [VHOST_USER_GET_PROTOCOL_FEATURES] = "VHOST_USER_GET_PROTOCOL_FEATURES",
76 [VHOST_USER_SET_PROTOCOL_FEATURES] = "VHOST_USER_SET_PROTOCOL_FEATURES",
77 [VHOST_USER_GET_QUEUE_NUM] = "VHOST_USER_GET_QUEUE_NUM",
78 [VHOST_USER_SET_VRING_ENABLE] = "VHOST_USER_SET_VRING_ENABLE",
79 [VHOST_USER_SEND_RARP] = "VHOST_USER_SEND_RARP",
80 [VHOST_USER_NET_SET_MTU] = "VHOST_USER_NET_SET_MTU",
81 [VHOST_USER_SET_SLAVE_REQ_FD] = "VHOST_USER_SET_SLAVE_REQ_FD",
82 [VHOST_USER_IOTLB_MSG] = "VHOST_USER_IOTLB_MSG",
83 [VHOST_USER_CRYPTO_CREATE_SESS] = "VHOST_USER_CRYPTO_CREATE_SESS",
84 [VHOST_USER_CRYPTO_CLOSE_SESS] = "VHOST_USER_CRYPTO_CLOSE_SESS",
85 [VHOST_USER_POSTCOPY_ADVISE] = "VHOST_USER_POSTCOPY_ADVISE",
86 [VHOST_USER_POSTCOPY_LISTEN] = "VHOST_USER_POSTCOPY_LISTEN",
87 [VHOST_USER_POSTCOPY_END] = "VHOST_USER_POSTCOPY_END",
88 [VHOST_USER_GET_INFLIGHT_FD] = "VHOST_USER_GET_INFLIGHT_FD",
89 [VHOST_USER_SET_INFLIGHT_FD] = "VHOST_USER_SET_INFLIGHT_FD",
92 static int send_vhost_reply(int sockfd, struct VhostUserMsg *msg);
93 static int read_vhost_message(int sockfd, struct VhostUserMsg *msg);
96 close_msg_fds(struct VhostUserMsg *msg)
100 for (i = 0; i < msg->fd_num; i++)
105 * Ensure the expected number of FDs is received,
106 * close all FDs and return an error if this is not the case.
109 validate_msg_fds(struct VhostUserMsg *msg, int expected_fds)
111 if (msg->fd_num == expected_fds)
114 VHOST_LOG_CONFIG(ERR,
115 " Expect %d FDs for request %s, received %d\n",
117 vhost_message_str[msg->request.master],
131 ret = fstat(fd, &stat);
132 return ret == -1 ? (uint64_t)-1 : (uint64_t)stat.st_blksize;
136 * Reclaim all the outstanding zmbufs for a virtqueue.
139 drain_zmbuf_list(struct vhost_virtqueue *vq)
141 struct zcopy_mbuf *zmbuf, *next;
143 for (zmbuf = TAILQ_FIRST(&vq->zmbuf_list);
144 zmbuf != NULL; zmbuf = next) {
145 next = TAILQ_NEXT(zmbuf, next);
147 while (!mbuf_is_consumed(zmbuf->mbuf))
150 TAILQ_REMOVE(&vq->zmbuf_list, zmbuf, next);
151 restore_mbuf(zmbuf->mbuf);
152 rte_pktmbuf_free(zmbuf->mbuf);
159 free_mem_region(struct virtio_net *dev)
162 struct rte_vhost_mem_region *reg;
163 struct vhost_virtqueue *vq;
165 if (!dev || !dev->mem)
168 if (dev->dequeue_zero_copy) {
169 for (i = 0; i < dev->nr_vring; i++) {
170 vq = dev->virtqueue[i];
172 drain_zmbuf_list(vq);
176 for (i = 0; i < dev->mem->nregions; i++) {
177 reg = &dev->mem->regions[i];
178 if (reg->host_user_addr) {
179 munmap(reg->mmap_addr, reg->mmap_size);
186 vhost_backend_cleanup(struct virtio_net *dev)
189 free_mem_region(dev);
194 rte_free(dev->guest_pages);
195 dev->guest_pages = NULL;
198 munmap((void *)(uintptr_t)dev->log_addr, dev->log_size);
202 if (dev->inflight_info) {
203 if (dev->inflight_info->addr) {
204 munmap(dev->inflight_info->addr,
205 dev->inflight_info->size);
206 dev->inflight_info->addr = NULL;
209 if (dev->inflight_info->fd >= 0) {
210 close(dev->inflight_info->fd);
211 dev->inflight_info->fd = -1;
214 free(dev->inflight_info);
215 dev->inflight_info = NULL;
218 if (dev->slave_req_fd >= 0) {
219 close(dev->slave_req_fd);
220 dev->slave_req_fd = -1;
223 if (dev->postcopy_ufd >= 0) {
224 close(dev->postcopy_ufd);
225 dev->postcopy_ufd = -1;
228 dev->postcopy_listening = 0;
232 vhost_user_notify_queue_state(struct virtio_net *dev, uint16_t index,
235 struct rte_vdpa_device *vdpa_dev = dev->vdpa_dev;
237 if (vdpa_dev && vdpa_dev->ops->set_vring_state)
238 vdpa_dev->ops->set_vring_state(dev->vid, index, enable);
240 if (dev->notify_ops->vring_state_changed)
241 dev->notify_ops->vring_state_changed(dev->vid,
246 * This function just returns success at the moment unless
247 * the device hasn't been initialised.
250 vhost_user_set_owner(struct virtio_net **pdev __rte_unused,
251 struct VhostUserMsg *msg,
252 int main_fd __rte_unused)
254 if (validate_msg_fds(msg, 0) != 0)
255 return RTE_VHOST_MSG_RESULT_ERR;
257 return RTE_VHOST_MSG_RESULT_OK;
261 vhost_user_reset_owner(struct virtio_net **pdev,
262 struct VhostUserMsg *msg,
263 int main_fd __rte_unused)
265 struct virtio_net *dev = *pdev;
267 if (validate_msg_fds(msg, 0) != 0)
268 return RTE_VHOST_MSG_RESULT_ERR;
270 vhost_destroy_device_notify(dev);
272 cleanup_device(dev, 0);
274 return RTE_VHOST_MSG_RESULT_OK;
278 * The features that we support are requested.
281 vhost_user_get_features(struct virtio_net **pdev, struct VhostUserMsg *msg,
282 int main_fd __rte_unused)
284 struct virtio_net *dev = *pdev;
285 uint64_t features = 0;
287 if (validate_msg_fds(msg, 0) != 0)
288 return RTE_VHOST_MSG_RESULT_ERR;
290 rte_vhost_driver_get_features(dev->ifname, &features);
292 msg->payload.u64 = features;
293 msg->size = sizeof(msg->payload.u64);
296 return RTE_VHOST_MSG_RESULT_REPLY;
300 * The queue number that we support are requested.
303 vhost_user_get_queue_num(struct virtio_net **pdev, struct VhostUserMsg *msg,
304 int main_fd __rte_unused)
306 struct virtio_net *dev = *pdev;
307 uint32_t queue_num = 0;
309 if (validate_msg_fds(msg, 0) != 0)
310 return RTE_VHOST_MSG_RESULT_ERR;
312 rte_vhost_driver_get_queue_num(dev->ifname, &queue_num);
314 msg->payload.u64 = (uint64_t)queue_num;
315 msg->size = sizeof(msg->payload.u64);
318 return RTE_VHOST_MSG_RESULT_REPLY;
322 * We receive the negotiated features supported by us and the virtio device.
325 vhost_user_set_features(struct virtio_net **pdev, struct VhostUserMsg *msg,
326 int main_fd __rte_unused)
328 struct virtio_net *dev = *pdev;
329 uint64_t features = msg->payload.u64;
330 uint64_t vhost_features = 0;
331 struct rte_vdpa_device *vdpa_dev;
333 if (validate_msg_fds(msg, 0) != 0)
334 return RTE_VHOST_MSG_RESULT_ERR;
336 rte_vhost_driver_get_features(dev->ifname, &vhost_features);
337 if (features & ~vhost_features) {
338 VHOST_LOG_CONFIG(ERR,
339 "(%d) received invalid negotiated features.\n",
341 return RTE_VHOST_MSG_RESULT_ERR;
344 if (dev->flags & VIRTIO_DEV_RUNNING) {
345 if (dev->features == features)
346 return RTE_VHOST_MSG_RESULT_OK;
349 * Error out if master tries to change features while device is
350 * in running state. The exception being VHOST_F_LOG_ALL, which
351 * is enabled when the live-migration starts.
353 if ((dev->features ^ features) & ~(1ULL << VHOST_F_LOG_ALL)) {
354 VHOST_LOG_CONFIG(ERR,
355 "(%d) features changed while device is running.\n",
357 return RTE_VHOST_MSG_RESULT_ERR;
360 if (dev->notify_ops->features_changed)
361 dev->notify_ops->features_changed(dev->vid, features);
364 dev->features = features;
366 ((1 << VIRTIO_NET_F_MRG_RXBUF) | (1ULL << VIRTIO_F_VERSION_1))) {
367 dev->vhost_hlen = sizeof(struct virtio_net_hdr_mrg_rxbuf);
369 dev->vhost_hlen = sizeof(struct virtio_net_hdr);
371 VHOST_LOG_CONFIG(INFO,
372 "negotiated Virtio features: 0x%" PRIx64 "\n", dev->features);
373 VHOST_LOG_CONFIG(DEBUG,
374 "(%d) mergeable RX buffers %s, virtio 1 %s\n",
376 (dev->features & (1 << VIRTIO_NET_F_MRG_RXBUF)) ? "on" : "off",
377 (dev->features & (1ULL << VIRTIO_F_VERSION_1)) ? "on" : "off");
379 if ((dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET) &&
380 !(dev->features & (1ULL << VIRTIO_NET_F_MQ))) {
382 * Remove all but first queue pair if MQ hasn't been
383 * negotiated. This is safe because the device is not
384 * running at this stage.
386 while (dev->nr_vring > 2) {
387 struct vhost_virtqueue *vq;
389 vq = dev->virtqueue[--dev->nr_vring];
393 dev->virtqueue[dev->nr_vring] = NULL;
395 cleanup_vq_inflight(dev, vq);
400 vdpa_dev = dev->vdpa_dev;
401 if (vdpa_dev && vdpa_dev->ops->set_features)
402 vdpa_dev->ops->set_features(dev->vid);
404 return RTE_VHOST_MSG_RESULT_OK;
408 * The virtio device sends us the size of the descriptor ring.
411 vhost_user_set_vring_num(struct virtio_net **pdev,
412 struct VhostUserMsg *msg,
413 int main_fd __rte_unused)
415 struct virtio_net *dev = *pdev;
416 struct vhost_virtqueue *vq = dev->virtqueue[msg->payload.state.index];
418 if (validate_msg_fds(msg, 0) != 0)
419 return RTE_VHOST_MSG_RESULT_ERR;
421 vq->size = msg->payload.state.num;
423 /* VIRTIO 1.0, 2.4 Virtqueues says:
425 * Queue Size value is always a power of 2. The maximum Queue Size
428 * VIRTIO 1.1 2.7 Virtqueues says:
430 * Packed virtqueues support up to 2^15 entries each.
432 if (!vq_is_packed(dev)) {
433 if (vq->size & (vq->size - 1)) {
434 VHOST_LOG_CONFIG(ERR,
435 "invalid virtqueue size %u\n", vq->size);
436 return RTE_VHOST_MSG_RESULT_ERR;
440 if (vq->size > 32768) {
441 VHOST_LOG_CONFIG(ERR,
442 "invalid virtqueue size %u\n", vq->size);
443 return RTE_VHOST_MSG_RESULT_ERR;
446 if (dev->dequeue_zero_copy) {
448 vq->last_zmbuf_idx = 0;
449 vq->zmbuf_size = vq->size;
451 rte_free(vq->zmbufs);
452 vq->zmbufs = rte_zmalloc(NULL, vq->zmbuf_size *
453 sizeof(struct zcopy_mbuf), 0);
454 if (vq->zmbufs == NULL) {
455 VHOST_LOG_CONFIG(WARNING,
456 "failed to allocate mem for zero copy; "
457 "zero copy is force disabled\n");
458 dev->dequeue_zero_copy = 0;
460 TAILQ_INIT(&vq->zmbuf_list);
463 if (vq_is_packed(dev)) {
464 if (vq->shadow_used_packed)
465 rte_free(vq->shadow_used_packed);
466 vq->shadow_used_packed = rte_malloc(NULL,
468 sizeof(struct vring_used_elem_packed),
469 RTE_CACHE_LINE_SIZE);
470 if (!vq->shadow_used_packed) {
471 VHOST_LOG_CONFIG(ERR,
472 "failed to allocate memory for shadow used ring.\n");
473 return RTE_VHOST_MSG_RESULT_ERR;
477 if (vq->shadow_used_split)
478 rte_free(vq->shadow_used_split);
479 vq->shadow_used_split = rte_malloc(NULL,
480 vq->size * sizeof(struct vring_used_elem),
481 RTE_CACHE_LINE_SIZE);
482 if (!vq->shadow_used_split) {
483 VHOST_LOG_CONFIG(ERR,
484 "failed to allocate memory for shadow used ring.\n");
485 return RTE_VHOST_MSG_RESULT_ERR;
489 if (vq->batch_copy_elems)
490 rte_free(vq->batch_copy_elems);
491 vq->batch_copy_elems = rte_malloc(NULL,
492 vq->size * sizeof(struct batch_copy_elem),
493 RTE_CACHE_LINE_SIZE);
494 if (!vq->batch_copy_elems) {
495 VHOST_LOG_CONFIG(ERR,
496 "failed to allocate memory for batching copy.\n");
497 return RTE_VHOST_MSG_RESULT_ERR;
500 return RTE_VHOST_MSG_RESULT_OK;
504 * Reallocate virtio_dev and vhost_virtqueue data structure to make them on the
505 * same numa node as the memory of vring descriptor.
507 #ifdef RTE_LIBRTE_VHOST_NUMA
508 static struct virtio_net*
509 numa_realloc(struct virtio_net *dev, int index)
511 int oldnode, newnode;
512 struct virtio_net *old_dev;
513 struct vhost_virtqueue *old_vq, *vq;
514 struct zcopy_mbuf *new_zmbuf;
515 struct vring_used_elem *new_shadow_used_split;
516 struct vring_used_elem_packed *new_shadow_used_packed;
517 struct batch_copy_elem *new_batch_copy_elems;
520 if (dev->flags & VIRTIO_DEV_RUNNING)
524 vq = old_vq = dev->virtqueue[index];
526 ret = get_mempolicy(&newnode, NULL, 0, old_vq->desc,
527 MPOL_F_NODE | MPOL_F_ADDR);
529 /* check if we need to reallocate vq */
530 ret |= get_mempolicy(&oldnode, NULL, 0, old_vq,
531 MPOL_F_NODE | MPOL_F_ADDR);
533 VHOST_LOG_CONFIG(ERR,
534 "Unable to get vq numa information.\n");
537 if (oldnode != newnode) {
538 VHOST_LOG_CONFIG(INFO,
539 "reallocate vq from %d to %d node\n", oldnode, newnode);
540 vq = rte_malloc_socket(NULL, sizeof(*vq), 0, newnode);
544 memcpy(vq, old_vq, sizeof(*vq));
545 TAILQ_INIT(&vq->zmbuf_list);
547 if (dev->dequeue_zero_copy) {
548 new_zmbuf = rte_malloc_socket(NULL, vq->zmbuf_size *
549 sizeof(struct zcopy_mbuf), 0, newnode);
551 rte_free(vq->zmbufs);
552 vq->zmbufs = new_zmbuf;
556 if (vq_is_packed(dev)) {
557 new_shadow_used_packed = rte_malloc_socket(NULL,
559 sizeof(struct vring_used_elem_packed),
562 if (new_shadow_used_packed) {
563 rte_free(vq->shadow_used_packed);
564 vq->shadow_used_packed = new_shadow_used_packed;
567 new_shadow_used_split = rte_malloc_socket(NULL,
569 sizeof(struct vring_used_elem),
572 if (new_shadow_used_split) {
573 rte_free(vq->shadow_used_split);
574 vq->shadow_used_split = new_shadow_used_split;
578 new_batch_copy_elems = rte_malloc_socket(NULL,
579 vq->size * sizeof(struct batch_copy_elem),
582 if (new_batch_copy_elems) {
583 rte_free(vq->batch_copy_elems);
584 vq->batch_copy_elems = new_batch_copy_elems;
590 /* check if we need to reallocate dev */
591 ret = get_mempolicy(&oldnode, NULL, 0, old_dev,
592 MPOL_F_NODE | MPOL_F_ADDR);
594 VHOST_LOG_CONFIG(ERR,
595 "Unable to get dev numa information.\n");
598 if (oldnode != newnode) {
599 VHOST_LOG_CONFIG(INFO,
600 "reallocate dev from %d to %d node\n",
602 dev = rte_malloc_socket(NULL, sizeof(*dev), 0, newnode);
608 memcpy(dev, old_dev, sizeof(*dev));
613 dev->virtqueue[index] = vq;
614 vhost_devices[dev->vid] = dev;
617 vhost_user_iotlb_init(dev, index);
622 static struct virtio_net*
623 numa_realloc(struct virtio_net *dev, int index __rte_unused)
629 /* Converts QEMU virtual address to Vhost virtual address. */
631 qva_to_vva(struct virtio_net *dev, uint64_t qva, uint64_t *len)
633 struct rte_vhost_mem_region *r;
636 if (unlikely(!dev || !dev->mem))
639 /* Find the region where the address lives. */
640 for (i = 0; i < dev->mem->nregions; i++) {
641 r = &dev->mem->regions[i];
643 if (qva >= r->guest_user_addr &&
644 qva < r->guest_user_addr + r->size) {
646 if (unlikely(*len > r->guest_user_addr + r->size - qva))
647 *len = r->guest_user_addr + r->size - qva;
649 return qva - r->guest_user_addr +
661 * Converts ring address to Vhost virtual address.
662 * If IOMMU is enabled, the ring address is a guest IO virtual address,
663 * else it is a QEMU virtual address.
666 ring_addr_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
667 uint64_t ra, uint64_t *size)
669 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)) {
672 vhost_user_iotlb_rd_lock(vq);
673 vva = vhost_iova_to_vva(dev, vq, ra,
674 size, VHOST_ACCESS_RW);
675 vhost_user_iotlb_rd_unlock(vq);
680 return qva_to_vva(dev, ra, size);
684 log_addr_to_gpa(struct virtio_net *dev, struct vhost_virtqueue *vq)
688 vhost_user_iotlb_rd_lock(vq);
689 log_gpa = translate_log_addr(dev, vq, vq->ring_addrs.log_guest_addr);
690 vhost_user_iotlb_rd_unlock(vq);
695 static struct virtio_net *
696 translate_ring_addresses(struct virtio_net *dev, int vq_index)
698 struct vhost_virtqueue *vq = dev->virtqueue[vq_index];
699 struct vhost_vring_addr *addr = &vq->ring_addrs;
700 uint64_t len, expected_len;
702 if (addr->flags & (1 << VHOST_VRING_F_LOG)) {
704 log_addr_to_gpa(dev, vq);
705 if (vq->log_guest_addr == 0) {
706 VHOST_LOG_CONFIG(DEBUG,
707 "(%d) failed to map log_guest_addr.\n",
713 if (vq_is_packed(dev)) {
714 len = sizeof(struct vring_packed_desc) * vq->size;
715 vq->desc_packed = (struct vring_packed_desc *)(uintptr_t)
716 ring_addr_to_vva(dev, vq, addr->desc_user_addr, &len);
717 if (vq->desc_packed == NULL ||
718 len != sizeof(struct vring_packed_desc) *
720 VHOST_LOG_CONFIG(DEBUG,
721 "(%d) failed to map desc_packed ring.\n",
726 dev = numa_realloc(dev, vq_index);
727 vq = dev->virtqueue[vq_index];
728 addr = &vq->ring_addrs;
730 len = sizeof(struct vring_packed_desc_event);
731 vq->driver_event = (struct vring_packed_desc_event *)
732 (uintptr_t)ring_addr_to_vva(dev,
733 vq, addr->avail_user_addr, &len);
734 if (vq->driver_event == NULL ||
735 len != sizeof(struct vring_packed_desc_event)) {
736 VHOST_LOG_CONFIG(DEBUG,
737 "(%d) failed to find driver area address.\n",
742 len = sizeof(struct vring_packed_desc_event);
743 vq->device_event = (struct vring_packed_desc_event *)
744 (uintptr_t)ring_addr_to_vva(dev,
745 vq, addr->used_user_addr, &len);
746 if (vq->device_event == NULL ||
747 len != sizeof(struct vring_packed_desc_event)) {
748 VHOST_LOG_CONFIG(DEBUG,
749 "(%d) failed to find device area address.\n",
758 /* The addresses are converted from QEMU virtual to Vhost virtual. */
759 if (vq->desc && vq->avail && vq->used)
762 len = sizeof(struct vring_desc) * vq->size;
763 vq->desc = (struct vring_desc *)(uintptr_t)ring_addr_to_vva(dev,
764 vq, addr->desc_user_addr, &len);
765 if (vq->desc == 0 || len != sizeof(struct vring_desc) * vq->size) {
766 VHOST_LOG_CONFIG(DEBUG,
767 "(%d) failed to map desc ring.\n",
772 dev = numa_realloc(dev, vq_index);
773 vq = dev->virtqueue[vq_index];
774 addr = &vq->ring_addrs;
776 len = sizeof(struct vring_avail) + sizeof(uint16_t) * vq->size;
777 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))
778 len += sizeof(uint16_t);
780 vq->avail = (struct vring_avail *)(uintptr_t)ring_addr_to_vva(dev,
781 vq, addr->avail_user_addr, &len);
782 if (vq->avail == 0 || len != expected_len) {
783 VHOST_LOG_CONFIG(DEBUG,
784 "(%d) failed to map avail ring.\n",
789 len = sizeof(struct vring_used) +
790 sizeof(struct vring_used_elem) * vq->size;
791 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))
792 len += sizeof(uint16_t);
794 vq->used = (struct vring_used *)(uintptr_t)ring_addr_to_vva(dev,
795 vq, addr->used_user_addr, &len);
796 if (vq->used == 0 || len != expected_len) {
797 VHOST_LOG_CONFIG(DEBUG,
798 "(%d) failed to map used ring.\n",
803 if (vq->last_used_idx != vq->used->idx) {
804 VHOST_LOG_CONFIG(WARNING,
805 "last_used_idx (%u) and vq->used->idx (%u) mismatches; "
806 "some packets maybe resent for Tx and dropped for Rx\n",
807 vq->last_used_idx, vq->used->idx);
808 vq->last_used_idx = vq->used->idx;
809 vq->last_avail_idx = vq->used->idx;
814 VHOST_LOG_CONFIG(DEBUG, "(%d) mapped address desc: %p\n",
816 VHOST_LOG_CONFIG(DEBUG, "(%d) mapped address avail: %p\n",
817 dev->vid, vq->avail);
818 VHOST_LOG_CONFIG(DEBUG, "(%d) mapped address used: %p\n",
820 VHOST_LOG_CONFIG(DEBUG, "(%d) log_guest_addr: %" PRIx64 "\n",
821 dev->vid, vq->log_guest_addr);
827 * The virtio device sends us the desc, used and avail ring addresses.
828 * This function then converts these to our address space.
831 vhost_user_set_vring_addr(struct virtio_net **pdev, struct VhostUserMsg *msg,
832 int main_fd __rte_unused)
834 struct virtio_net *dev = *pdev;
835 struct vhost_virtqueue *vq;
836 struct vhost_vring_addr *addr = &msg->payload.addr;
839 if (validate_msg_fds(msg, 0) != 0)
840 return RTE_VHOST_MSG_RESULT_ERR;
842 if (dev->mem == NULL)
843 return RTE_VHOST_MSG_RESULT_ERR;
845 /* addr->index refers to the queue index. The txq 1, rxq is 0. */
846 vq = dev->virtqueue[msg->payload.addr.index];
848 access_ok = vq->access_ok;
851 * Rings addresses should not be interpreted as long as the ring is not
852 * started and enabled
854 memcpy(&vq->ring_addrs, addr, sizeof(*addr));
856 vring_invalidate(dev, vq);
858 if ((vq->enabled && (dev->features &
859 (1ULL << VHOST_USER_F_PROTOCOL_FEATURES))) ||
861 dev = translate_ring_addresses(dev, msg->payload.addr.index);
863 return RTE_VHOST_MSG_RESULT_ERR;
868 return RTE_VHOST_MSG_RESULT_OK;
872 * The virtio device sends us the available ring last used index.
875 vhost_user_set_vring_base(struct virtio_net **pdev,
876 struct VhostUserMsg *msg,
877 int main_fd __rte_unused)
879 struct virtio_net *dev = *pdev;
880 struct vhost_virtqueue *vq = dev->virtqueue[msg->payload.state.index];
881 uint64_t val = msg->payload.state.num;
883 if (validate_msg_fds(msg, 0) != 0)
884 return RTE_VHOST_MSG_RESULT_ERR;
886 if (vq_is_packed(dev)) {
888 * Bit[0:14]: avail index
889 * Bit[15]: avail wrap counter
891 vq->last_avail_idx = val & 0x7fff;
892 vq->avail_wrap_counter = !!(val & (0x1 << 15));
894 * Set used index to same value as available one, as
895 * their values should be the same since ring processing
896 * was stopped at get time.
898 vq->last_used_idx = vq->last_avail_idx;
899 vq->used_wrap_counter = vq->avail_wrap_counter;
901 vq->last_used_idx = msg->payload.state.num;
902 vq->last_avail_idx = msg->payload.state.num;
905 return RTE_VHOST_MSG_RESULT_OK;
909 add_one_guest_page(struct virtio_net *dev, uint64_t guest_phys_addr,
910 uint64_t host_phys_addr, uint64_t size)
912 struct guest_page *page, *last_page;
913 struct guest_page *old_pages;
915 if (dev->nr_guest_pages == dev->max_guest_pages) {
916 dev->max_guest_pages *= 2;
917 old_pages = dev->guest_pages;
918 dev->guest_pages = rte_realloc(dev->guest_pages,
919 dev->max_guest_pages * sizeof(*page),
920 RTE_CACHE_LINE_SIZE);
921 if (dev->guest_pages == NULL) {
922 VHOST_LOG_CONFIG(ERR, "cannot realloc guest_pages\n");
928 if (dev->nr_guest_pages > 0) {
929 last_page = &dev->guest_pages[dev->nr_guest_pages - 1];
930 /* merge if the two pages are continuous */
931 if (host_phys_addr == last_page->host_phys_addr +
933 last_page->size += size;
938 page = &dev->guest_pages[dev->nr_guest_pages++];
939 page->guest_phys_addr = guest_phys_addr;
940 page->host_phys_addr = host_phys_addr;
947 add_guest_pages(struct virtio_net *dev, struct rte_vhost_mem_region *reg,
950 uint64_t reg_size = reg->size;
951 uint64_t host_user_addr = reg->host_user_addr;
952 uint64_t guest_phys_addr = reg->guest_phys_addr;
953 uint64_t host_phys_addr;
956 host_phys_addr = rte_mem_virt2iova((void *)(uintptr_t)host_user_addr);
957 size = page_size - (guest_phys_addr & (page_size - 1));
958 size = RTE_MIN(size, reg_size);
960 if (add_one_guest_page(dev, guest_phys_addr, host_phys_addr, size) < 0)
963 host_user_addr += size;
964 guest_phys_addr += size;
967 while (reg_size > 0) {
968 size = RTE_MIN(reg_size, page_size);
969 host_phys_addr = rte_mem_virt2iova((void *)(uintptr_t)
971 if (add_one_guest_page(dev, guest_phys_addr, host_phys_addr,
975 host_user_addr += size;
976 guest_phys_addr += size;
980 /* sort guest page array if over binary search threshold */
981 if (dev->nr_guest_pages >= VHOST_BINARY_SEARCH_THRESH) {
982 qsort((void *)dev->guest_pages, dev->nr_guest_pages,
983 sizeof(struct guest_page), guest_page_addrcmp);
989 #ifdef RTE_LIBRTE_VHOST_DEBUG
990 /* TODO: enable it only in debug mode? */
992 dump_guest_pages(struct virtio_net *dev)
995 struct guest_page *page;
997 for (i = 0; i < dev->nr_guest_pages; i++) {
998 page = &dev->guest_pages[i];
1000 VHOST_LOG_CONFIG(INFO,
1001 "guest physical page region %u\n"
1002 "\t guest_phys_addr: %" PRIx64 "\n"
1003 "\t host_phys_addr : %" PRIx64 "\n"
1004 "\t size : %" PRIx64 "\n",
1006 page->guest_phys_addr,
1007 page->host_phys_addr,
1012 #define dump_guest_pages(dev)
1016 vhost_memory_changed(struct VhostUserMemory *new,
1017 struct rte_vhost_memory *old)
1021 if (new->nregions != old->nregions)
1024 for (i = 0; i < new->nregions; ++i) {
1025 VhostUserMemoryRegion *new_r = &new->regions[i];
1026 struct rte_vhost_mem_region *old_r = &old->regions[i];
1028 if (new_r->guest_phys_addr != old_r->guest_phys_addr)
1030 if (new_r->memory_size != old_r->size)
1032 if (new_r->userspace_addr != old_r->guest_user_addr)
1040 vhost_user_set_mem_table(struct virtio_net **pdev, struct VhostUserMsg *msg,
1043 struct virtio_net *dev = *pdev;
1044 struct VhostUserMemory *memory = &msg->payload.memory;
1045 struct rte_vhost_mem_region *reg;
1048 uint64_t mmap_offset;
1054 if (validate_msg_fds(msg, memory->nregions) != 0)
1055 return RTE_VHOST_MSG_RESULT_ERR;
1057 if (memory->nregions > VHOST_MEMORY_MAX_NREGIONS) {
1058 VHOST_LOG_CONFIG(ERR,
1059 "too many memory regions (%u)\n", memory->nregions);
1060 return RTE_VHOST_MSG_RESULT_ERR;
1063 if (dev->mem && !vhost_memory_changed(memory, dev->mem)) {
1064 VHOST_LOG_CONFIG(INFO,
1065 "(%d) memory regions not changed\n", dev->vid);
1069 return RTE_VHOST_MSG_RESULT_OK;
1073 free_mem_region(dev);
1078 /* Flush IOTLB cache as previous HVAs are now invalid */
1079 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1080 for (i = 0; i < dev->nr_vring; i++)
1081 vhost_user_iotlb_flush_all(dev->virtqueue[i]);
1083 dev->nr_guest_pages = 0;
1084 if (dev->guest_pages == NULL) {
1085 dev->max_guest_pages = 8;
1086 dev->guest_pages = rte_zmalloc(NULL,
1087 dev->max_guest_pages *
1088 sizeof(struct guest_page),
1089 RTE_CACHE_LINE_SIZE);
1090 if (dev->guest_pages == NULL) {
1091 VHOST_LOG_CONFIG(ERR,
1092 "(%d) failed to allocate memory "
1093 "for dev->guest_pages\n",
1095 return RTE_VHOST_MSG_RESULT_ERR;
1099 dev->mem = rte_zmalloc("vhost-mem-table", sizeof(struct rte_vhost_memory) +
1100 sizeof(struct rte_vhost_mem_region) * memory->nregions, 0);
1101 if (dev->mem == NULL) {
1102 VHOST_LOG_CONFIG(ERR,
1103 "(%d) failed to allocate memory for dev->mem\n",
1105 return RTE_VHOST_MSG_RESULT_ERR;
1107 dev->mem->nregions = memory->nregions;
1109 for (i = 0; i < memory->nregions; i++) {
1111 reg = &dev->mem->regions[i];
1113 reg->guest_phys_addr = memory->regions[i].guest_phys_addr;
1114 reg->guest_user_addr = memory->regions[i].userspace_addr;
1115 reg->size = memory->regions[i].memory_size;
1118 mmap_offset = memory->regions[i].mmap_offset;
1120 /* Check for memory_size + mmap_offset overflow */
1121 if (mmap_offset >= -reg->size) {
1122 VHOST_LOG_CONFIG(ERR,
1123 "mmap_offset (%#"PRIx64") and memory_size "
1124 "(%#"PRIx64") overflow\n",
1125 mmap_offset, reg->size);
1129 mmap_size = reg->size + mmap_offset;
1131 /* mmap() without flag of MAP_ANONYMOUS, should be called
1132 * with length argument aligned with hugepagesz at older
1133 * longterm version Linux, like 2.6.32 and 3.2.72, or
1134 * mmap() will fail with EINVAL.
1136 * to avoid failure, make sure in caller to keep length
1139 alignment = get_blk_size(fd);
1140 if (alignment == (uint64_t)-1) {
1141 VHOST_LOG_CONFIG(ERR,
1142 "couldn't get hugepage size through fstat\n");
1145 mmap_size = RTE_ALIGN_CEIL(mmap_size, alignment);
1146 if (mmap_size == 0) {
1148 * It could happen if initial mmap_size + alignment
1149 * overflows the sizeof uint64, which could happen if
1150 * either mmap_size or alignment value is wrong.
1152 * mmap() kernel implementation would return an error,
1153 * but better catch it before and provide useful info
1156 VHOST_LOG_CONFIG(ERR, "mmap size (0x%" PRIx64 ") "
1157 "or alignment (0x%" PRIx64 ") is invalid\n",
1158 reg->size + mmap_offset, alignment);
1162 populate = (dev->dequeue_zero_copy) ? MAP_POPULATE : 0;
1163 mmap_addr = mmap(NULL, mmap_size, PROT_READ | PROT_WRITE,
1164 MAP_SHARED | populate, fd, 0);
1166 if (mmap_addr == MAP_FAILED) {
1167 VHOST_LOG_CONFIG(ERR,
1168 "mmap region %u failed.\n", i);
1172 reg->mmap_addr = mmap_addr;
1173 reg->mmap_size = mmap_size;
1174 reg->host_user_addr = (uint64_t)(uintptr_t)mmap_addr +
1177 if (dev->dequeue_zero_copy)
1178 if (add_guest_pages(dev, reg, alignment) < 0) {
1179 VHOST_LOG_CONFIG(ERR,
1180 "adding guest pages to region %u failed.\n",
1185 VHOST_LOG_CONFIG(INFO,
1186 "guest memory region %u, size: 0x%" PRIx64 "\n"
1187 "\t guest physical addr: 0x%" PRIx64 "\n"
1188 "\t guest virtual addr: 0x%" PRIx64 "\n"
1189 "\t host virtual addr: 0x%" PRIx64 "\n"
1190 "\t mmap addr : 0x%" PRIx64 "\n"
1191 "\t mmap size : 0x%" PRIx64 "\n"
1192 "\t mmap align: 0x%" PRIx64 "\n"
1193 "\t mmap off : 0x%" PRIx64 "\n",
1195 reg->guest_phys_addr,
1196 reg->guest_user_addr,
1197 reg->host_user_addr,
1198 (uint64_t)(uintptr_t)mmap_addr,
1203 if (dev->postcopy_listening) {
1205 * We haven't a better way right now than sharing
1206 * DPDK's virtual address with Qemu, so that Qemu can
1207 * retrieve the region offset when handling userfaults.
1209 memory->regions[i].userspace_addr =
1210 reg->host_user_addr;
1213 if (dev->postcopy_listening) {
1214 /* Send the addresses back to qemu */
1216 send_vhost_reply(main_fd, msg);
1218 /* Wait for qemu to acknolwedge it's got the addresses
1219 * we've got to wait before we're allowed to generate faults.
1221 VhostUserMsg ack_msg;
1222 if (read_vhost_message(main_fd, &ack_msg) <= 0) {
1223 VHOST_LOG_CONFIG(ERR,
1224 "Failed to read qemu ack on postcopy set-mem-table\n");
1228 if (validate_msg_fds(&ack_msg, 0) != 0)
1231 if (ack_msg.request.master != VHOST_USER_SET_MEM_TABLE) {
1232 VHOST_LOG_CONFIG(ERR,
1233 "Bad qemu ack on postcopy set-mem-table (%d)\n",
1234 ack_msg.request.master);
1238 /* Now userfault register and we can use the memory */
1239 for (i = 0; i < memory->nregions; i++) {
1240 #ifdef RTE_LIBRTE_VHOST_POSTCOPY
1241 reg = &dev->mem->regions[i];
1242 struct uffdio_register reg_struct;
1245 * Let's register all the mmap'ed area to ensure
1246 * alignment on page boundary.
1248 reg_struct.range.start =
1249 (uint64_t)(uintptr_t)reg->mmap_addr;
1250 reg_struct.range.len = reg->mmap_size;
1251 reg_struct.mode = UFFDIO_REGISTER_MODE_MISSING;
1253 if (ioctl(dev->postcopy_ufd, UFFDIO_REGISTER,
1255 VHOST_LOG_CONFIG(ERR,
1256 "Failed to register ufd for region %d: (ufd = %d) %s\n",
1257 i, dev->postcopy_ufd,
1261 VHOST_LOG_CONFIG(INFO,
1262 "\t userfaultfd registered for range : "
1263 "%" PRIx64 " - %" PRIx64 "\n",
1264 (uint64_t)reg_struct.range.start,
1265 (uint64_t)reg_struct.range.start +
1266 (uint64_t)reg_struct.range.len - 1);
1273 for (i = 0; i < dev->nr_vring; i++) {
1274 struct vhost_virtqueue *vq = dev->virtqueue[i];
1276 if (vq->desc || vq->avail || vq->used) {
1278 * If the memory table got updated, the ring addresses
1279 * need to be translated again as virtual addresses have
1282 vring_invalidate(dev, vq);
1284 dev = translate_ring_addresses(dev, i);
1294 dump_guest_pages(dev);
1296 return RTE_VHOST_MSG_RESULT_OK;
1299 free_mem_region(dev);
1302 return RTE_VHOST_MSG_RESULT_ERR;
1306 vq_is_ready(struct virtio_net *dev, struct vhost_virtqueue *vq)
1313 if (vq_is_packed(dev))
1314 rings_ok = vq->desc_packed && vq->driver_event &&
1317 rings_ok = vq->desc && vq->avail && vq->used;
1320 vq->kickfd != VIRTIO_UNINITIALIZED_EVENTFD &&
1321 vq->callfd != VIRTIO_UNINITIALIZED_EVENTFD &&
1325 #define VIRTIO_DEV_NUM_VQS_TO_BE_READY 2u
1328 virtio_is_ready(struct virtio_net *dev)
1330 struct vhost_virtqueue *vq;
1333 if (dev->nr_vring < VIRTIO_DEV_NUM_VQS_TO_BE_READY)
1336 for (i = 0; i < VIRTIO_DEV_NUM_VQS_TO_BE_READY; i++) {
1337 vq = dev->virtqueue[i];
1339 if (!vq_is_ready(dev, vq))
1343 if (!(dev->flags & VIRTIO_DEV_RUNNING))
1344 VHOST_LOG_CONFIG(INFO,
1345 "virtio is now ready for processing.\n");
1350 inflight_mem_alloc(const char *name, size_t size, int *fd)
1354 char fname[20] = "/tmp/memfd-XXXXXX";
1357 #ifdef MEMFD_SUPPORTED
1358 mfd = memfd_create(name, MFD_CLOEXEC);
1363 mfd = mkstemp(fname);
1365 VHOST_LOG_CONFIG(ERR,
1366 "failed to get inflight buffer fd\n");
1373 if (ftruncate(mfd, size) == -1) {
1374 VHOST_LOG_CONFIG(ERR,
1375 "failed to alloc inflight buffer\n");
1380 ptr = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, mfd, 0);
1381 if (ptr == MAP_FAILED) {
1382 VHOST_LOG_CONFIG(ERR,
1383 "failed to mmap inflight buffer\n");
1393 get_pervq_shm_size_split(uint16_t queue_size)
1395 return RTE_ALIGN_MUL_CEIL(sizeof(struct rte_vhost_inflight_desc_split) *
1396 queue_size + sizeof(uint64_t) +
1397 sizeof(uint16_t) * 4, INFLIGHT_ALIGNMENT);
1401 get_pervq_shm_size_packed(uint16_t queue_size)
1403 return RTE_ALIGN_MUL_CEIL(sizeof(struct rte_vhost_inflight_desc_packed)
1404 * queue_size + sizeof(uint64_t) +
1405 sizeof(uint16_t) * 6 + sizeof(uint8_t) * 9,
1406 INFLIGHT_ALIGNMENT);
1410 vhost_user_get_inflight_fd(struct virtio_net **pdev,
1412 int main_fd __rte_unused)
1414 struct rte_vhost_inflight_info_packed *inflight_packed;
1415 uint64_t pervq_inflight_size, mmap_size;
1416 uint16_t num_queues, queue_size;
1417 struct virtio_net *dev = *pdev;
1421 if (msg->size != sizeof(msg->payload.inflight)) {
1422 VHOST_LOG_CONFIG(ERR,
1423 "invalid get_inflight_fd message size is %d\n",
1425 return RTE_VHOST_MSG_RESULT_ERR;
1428 if (dev->inflight_info == NULL) {
1429 dev->inflight_info = calloc(1,
1430 sizeof(struct inflight_mem_info));
1431 if (!dev->inflight_info) {
1432 VHOST_LOG_CONFIG(ERR,
1433 "failed to alloc dev inflight area\n");
1434 return RTE_VHOST_MSG_RESULT_ERR;
1436 dev->inflight_info->fd = -1;
1439 num_queues = msg->payload.inflight.num_queues;
1440 queue_size = msg->payload.inflight.queue_size;
1442 VHOST_LOG_CONFIG(INFO, "get_inflight_fd num_queues: %u\n",
1443 msg->payload.inflight.num_queues);
1444 VHOST_LOG_CONFIG(INFO, "get_inflight_fd queue_size: %u\n",
1445 msg->payload.inflight.queue_size);
1447 if (vq_is_packed(dev))
1448 pervq_inflight_size = get_pervq_shm_size_packed(queue_size);
1450 pervq_inflight_size = get_pervq_shm_size_split(queue_size);
1452 mmap_size = num_queues * pervq_inflight_size;
1453 addr = inflight_mem_alloc("vhost-inflight", mmap_size, &fd);
1455 VHOST_LOG_CONFIG(ERR,
1456 "failed to alloc vhost inflight area\n");
1457 msg->payload.inflight.mmap_size = 0;
1458 return RTE_VHOST_MSG_RESULT_ERR;
1460 memset(addr, 0, mmap_size);
1462 if (dev->inflight_info->addr) {
1463 munmap(dev->inflight_info->addr, dev->inflight_info->size);
1464 dev->inflight_info->addr = NULL;
1467 if (dev->inflight_info->fd >= 0) {
1468 close(dev->inflight_info->fd);
1469 dev->inflight_info->fd = -1;
1472 dev->inflight_info->addr = addr;
1473 dev->inflight_info->size = msg->payload.inflight.mmap_size = mmap_size;
1474 dev->inflight_info->fd = msg->fds[0] = fd;
1475 msg->payload.inflight.mmap_offset = 0;
1478 if (vq_is_packed(dev)) {
1479 for (i = 0; i < num_queues; i++) {
1481 (struct rte_vhost_inflight_info_packed *)addr;
1482 inflight_packed->used_wrap_counter = 1;
1483 inflight_packed->old_used_wrap_counter = 1;
1484 for (j = 0; j < queue_size; j++)
1485 inflight_packed->desc[j].next = j + 1;
1486 addr = (void *)((char *)addr + pervq_inflight_size);
1490 VHOST_LOG_CONFIG(INFO,
1491 "send inflight mmap_size: %"PRIu64"\n",
1492 msg->payload.inflight.mmap_size);
1493 VHOST_LOG_CONFIG(INFO,
1494 "send inflight mmap_offset: %"PRIu64"\n",
1495 msg->payload.inflight.mmap_offset);
1496 VHOST_LOG_CONFIG(INFO,
1497 "send inflight fd: %d\n", msg->fds[0]);
1499 return RTE_VHOST_MSG_RESULT_REPLY;
1503 vhost_user_set_inflight_fd(struct virtio_net **pdev, VhostUserMsg *msg,
1504 int main_fd __rte_unused)
1506 uint64_t mmap_size, mmap_offset;
1507 uint16_t num_queues, queue_size;
1508 struct virtio_net *dev = *pdev;
1509 uint32_t pervq_inflight_size;
1510 struct vhost_virtqueue *vq;
1515 if (msg->size != sizeof(msg->payload.inflight) || fd < 0) {
1516 VHOST_LOG_CONFIG(ERR,
1517 "invalid set_inflight_fd message size is %d,fd is %d\n",
1519 return RTE_VHOST_MSG_RESULT_ERR;
1522 mmap_size = msg->payload.inflight.mmap_size;
1523 mmap_offset = msg->payload.inflight.mmap_offset;
1524 num_queues = msg->payload.inflight.num_queues;
1525 queue_size = msg->payload.inflight.queue_size;
1527 if (vq_is_packed(dev))
1528 pervq_inflight_size = get_pervq_shm_size_packed(queue_size);
1530 pervq_inflight_size = get_pervq_shm_size_split(queue_size);
1532 VHOST_LOG_CONFIG(INFO,
1533 "set_inflight_fd mmap_size: %"PRIu64"\n", mmap_size);
1534 VHOST_LOG_CONFIG(INFO,
1535 "set_inflight_fd mmap_offset: %"PRIu64"\n", mmap_offset);
1536 VHOST_LOG_CONFIG(INFO,
1537 "set_inflight_fd num_queues: %u\n", num_queues);
1538 VHOST_LOG_CONFIG(INFO,
1539 "set_inflight_fd queue_size: %u\n", queue_size);
1540 VHOST_LOG_CONFIG(INFO,
1541 "set_inflight_fd fd: %d\n", fd);
1542 VHOST_LOG_CONFIG(INFO,
1543 "set_inflight_fd pervq_inflight_size: %d\n",
1544 pervq_inflight_size);
1546 if (!dev->inflight_info) {
1547 dev->inflight_info = calloc(1,
1548 sizeof(struct inflight_mem_info));
1549 if (dev->inflight_info == NULL) {
1550 VHOST_LOG_CONFIG(ERR,
1551 "failed to alloc dev inflight area\n");
1552 return RTE_VHOST_MSG_RESULT_ERR;
1554 dev->inflight_info->fd = -1;
1557 if (dev->inflight_info->addr) {
1558 munmap(dev->inflight_info->addr, dev->inflight_info->size);
1559 dev->inflight_info->addr = NULL;
1562 addr = mmap(0, mmap_size, PROT_READ | PROT_WRITE, MAP_SHARED,
1564 if (addr == MAP_FAILED) {
1565 VHOST_LOG_CONFIG(ERR, "failed to mmap share memory.\n");
1566 return RTE_VHOST_MSG_RESULT_ERR;
1569 if (dev->inflight_info->fd >= 0) {
1570 close(dev->inflight_info->fd);
1571 dev->inflight_info->fd = -1;
1574 dev->inflight_info->fd = fd;
1575 dev->inflight_info->addr = addr;
1576 dev->inflight_info->size = mmap_size;
1578 for (i = 0; i < num_queues; i++) {
1579 vq = dev->virtqueue[i];
1580 if (vq_is_packed(dev)) {
1581 vq->inflight_packed = addr;
1582 vq->inflight_packed->desc_num = queue_size;
1584 vq->inflight_split = addr;
1585 vq->inflight_split->desc_num = queue_size;
1587 addr = (void *)((char *)addr + pervq_inflight_size);
1590 return RTE_VHOST_MSG_RESULT_OK;
1594 vhost_user_set_vring_call(struct virtio_net **pdev, struct VhostUserMsg *msg,
1595 int main_fd __rte_unused)
1597 struct virtio_net *dev = *pdev;
1598 struct vhost_vring_file file;
1599 struct vhost_virtqueue *vq;
1602 expected_fds = (msg->payload.u64 & VHOST_USER_VRING_NOFD_MASK) ? 0 : 1;
1603 if (validate_msg_fds(msg, expected_fds) != 0)
1604 return RTE_VHOST_MSG_RESULT_ERR;
1606 file.index = msg->payload.u64 & VHOST_USER_VRING_IDX_MASK;
1607 if (msg->payload.u64 & VHOST_USER_VRING_NOFD_MASK)
1608 file.fd = VIRTIO_INVALID_EVENTFD;
1610 file.fd = msg->fds[0];
1611 VHOST_LOG_CONFIG(INFO,
1612 "vring call idx:%d file:%d\n", file.index, file.fd);
1614 vq = dev->virtqueue[file.index];
1615 if (vq->callfd >= 0)
1618 vq->callfd = file.fd;
1620 return RTE_VHOST_MSG_RESULT_OK;
1623 static int vhost_user_set_vring_err(struct virtio_net **pdev __rte_unused,
1624 struct VhostUserMsg *msg,
1625 int main_fd __rte_unused)
1629 expected_fds = (msg->payload.u64 & VHOST_USER_VRING_NOFD_MASK) ? 0 : 1;
1630 if (validate_msg_fds(msg, expected_fds) != 0)
1631 return RTE_VHOST_MSG_RESULT_ERR;
1633 if (!(msg->payload.u64 & VHOST_USER_VRING_NOFD_MASK))
1635 VHOST_LOG_CONFIG(INFO, "not implemented\n");
1637 return RTE_VHOST_MSG_RESULT_OK;
1641 resubmit_desc_compare(const void *a, const void *b)
1643 const struct rte_vhost_resubmit_desc *desc0 = a;
1644 const struct rte_vhost_resubmit_desc *desc1 = b;
1646 if (desc1->counter > desc0->counter)
1653 vhost_check_queue_inflights_split(struct virtio_net *dev,
1654 struct vhost_virtqueue *vq)
1657 uint16_t resubmit_num = 0, last_io, num;
1658 struct vring_used *used = vq->used;
1659 struct rte_vhost_resubmit_info *resubmit;
1660 struct rte_vhost_inflight_info_split *inflight_split;
1662 if (!(dev->protocol_features &
1663 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD)))
1664 return RTE_VHOST_MSG_RESULT_OK;
1666 /* The frontend may still not support the inflight feature
1667 * although we negotiate the protocol feature.
1669 if ((!vq->inflight_split))
1670 return RTE_VHOST_MSG_RESULT_OK;
1672 if (!vq->inflight_split->version) {
1673 vq->inflight_split->version = INFLIGHT_VERSION;
1674 return RTE_VHOST_MSG_RESULT_OK;
1677 if (vq->resubmit_inflight)
1678 return RTE_VHOST_MSG_RESULT_OK;
1680 inflight_split = vq->inflight_split;
1681 vq->global_counter = 0;
1682 last_io = inflight_split->last_inflight_io;
1684 if (inflight_split->used_idx != used->idx) {
1685 inflight_split->desc[last_io].inflight = 0;
1687 inflight_split->used_idx = used->idx;
1690 for (i = 0; i < inflight_split->desc_num; i++) {
1691 if (inflight_split->desc[i].inflight == 1)
1695 vq->last_avail_idx += resubmit_num;
1698 resubmit = calloc(1, sizeof(struct rte_vhost_resubmit_info));
1700 VHOST_LOG_CONFIG(ERR,
1701 "failed to allocate memory for resubmit info.\n");
1702 return RTE_VHOST_MSG_RESULT_ERR;
1705 resubmit->resubmit_list = calloc(resubmit_num,
1706 sizeof(struct rte_vhost_resubmit_desc));
1707 if (!resubmit->resubmit_list) {
1708 VHOST_LOG_CONFIG(ERR,
1709 "failed to allocate memory for inflight desc.\n");
1711 return RTE_VHOST_MSG_RESULT_ERR;
1715 for (i = 0; i < vq->inflight_split->desc_num; i++) {
1716 if (vq->inflight_split->desc[i].inflight == 1) {
1717 resubmit->resubmit_list[num].index = i;
1718 resubmit->resubmit_list[num].counter =
1719 inflight_split->desc[i].counter;
1723 resubmit->resubmit_num = num;
1725 if (resubmit->resubmit_num > 1)
1726 qsort(resubmit->resubmit_list, resubmit->resubmit_num,
1727 sizeof(struct rte_vhost_resubmit_desc),
1728 resubmit_desc_compare);
1730 vq->global_counter = resubmit->resubmit_list[0].counter + 1;
1731 vq->resubmit_inflight = resubmit;
1734 return RTE_VHOST_MSG_RESULT_OK;
1738 vhost_check_queue_inflights_packed(struct virtio_net *dev,
1739 struct vhost_virtqueue *vq)
1742 uint16_t resubmit_num = 0, old_used_idx, num;
1743 struct rte_vhost_resubmit_info *resubmit;
1744 struct rte_vhost_inflight_info_packed *inflight_packed;
1746 if (!(dev->protocol_features &
1747 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD)))
1748 return RTE_VHOST_MSG_RESULT_OK;
1750 /* The frontend may still not support the inflight feature
1751 * although we negotiate the protocol feature.
1753 if ((!vq->inflight_packed))
1754 return RTE_VHOST_MSG_RESULT_OK;
1756 if (!vq->inflight_packed->version) {
1757 vq->inflight_packed->version = INFLIGHT_VERSION;
1758 return RTE_VHOST_MSG_RESULT_OK;
1761 if (vq->resubmit_inflight)
1762 return RTE_VHOST_MSG_RESULT_OK;
1764 inflight_packed = vq->inflight_packed;
1765 vq->global_counter = 0;
1766 old_used_idx = inflight_packed->old_used_idx;
1768 if (inflight_packed->used_idx != old_used_idx) {
1769 if (inflight_packed->desc[old_used_idx].inflight == 0) {
1770 inflight_packed->old_used_idx =
1771 inflight_packed->used_idx;
1772 inflight_packed->old_used_wrap_counter =
1773 inflight_packed->used_wrap_counter;
1774 inflight_packed->old_free_head =
1775 inflight_packed->free_head;
1777 inflight_packed->used_idx =
1778 inflight_packed->old_used_idx;
1779 inflight_packed->used_wrap_counter =
1780 inflight_packed->old_used_wrap_counter;
1781 inflight_packed->free_head =
1782 inflight_packed->old_free_head;
1786 for (i = 0; i < inflight_packed->desc_num; i++) {
1787 if (inflight_packed->desc[i].inflight == 1)
1792 resubmit = calloc(1, sizeof(struct rte_vhost_resubmit_info));
1793 if (resubmit == NULL) {
1794 VHOST_LOG_CONFIG(ERR,
1795 "failed to allocate memory for resubmit info.\n");
1796 return RTE_VHOST_MSG_RESULT_ERR;
1799 resubmit->resubmit_list = calloc(resubmit_num,
1800 sizeof(struct rte_vhost_resubmit_desc));
1801 if (resubmit->resubmit_list == NULL) {
1802 VHOST_LOG_CONFIG(ERR,
1803 "failed to allocate memory for resubmit desc.\n");
1805 return RTE_VHOST_MSG_RESULT_ERR;
1809 for (i = 0; i < inflight_packed->desc_num; i++) {
1810 if (vq->inflight_packed->desc[i].inflight == 1) {
1811 resubmit->resubmit_list[num].index = i;
1812 resubmit->resubmit_list[num].counter =
1813 inflight_packed->desc[i].counter;
1817 resubmit->resubmit_num = num;
1819 if (resubmit->resubmit_num > 1)
1820 qsort(resubmit->resubmit_list, resubmit->resubmit_num,
1821 sizeof(struct rte_vhost_resubmit_desc),
1822 resubmit_desc_compare);
1824 vq->global_counter = resubmit->resubmit_list[0].counter + 1;
1825 vq->resubmit_inflight = resubmit;
1828 return RTE_VHOST_MSG_RESULT_OK;
1832 vhost_user_set_vring_kick(struct virtio_net **pdev, struct VhostUserMsg *msg,
1833 int main_fd __rte_unused)
1835 struct virtio_net *dev = *pdev;
1836 struct vhost_vring_file file;
1837 struct vhost_virtqueue *vq;
1840 expected_fds = (msg->payload.u64 & VHOST_USER_VRING_NOFD_MASK) ? 0 : 1;
1841 if (validate_msg_fds(msg, expected_fds) != 0)
1842 return RTE_VHOST_MSG_RESULT_ERR;
1844 file.index = msg->payload.u64 & VHOST_USER_VRING_IDX_MASK;
1845 if (msg->payload.u64 & VHOST_USER_VRING_NOFD_MASK)
1846 file.fd = VIRTIO_INVALID_EVENTFD;
1848 file.fd = msg->fds[0];
1849 VHOST_LOG_CONFIG(INFO,
1850 "vring kick idx:%d file:%d\n", file.index, file.fd);
1852 /* Interpret ring addresses only when ring is started. */
1853 dev = translate_ring_addresses(dev, file.index);
1855 return RTE_VHOST_MSG_RESULT_ERR;
1859 vq = dev->virtqueue[file.index];
1862 * When VHOST_USER_F_PROTOCOL_FEATURES is not negotiated,
1863 * the ring starts already enabled. Otherwise, it is enabled via
1864 * the SET_VRING_ENABLE message.
1866 if (!(dev->features & (1ULL << VHOST_USER_F_PROTOCOL_FEATURES))) {
1868 if (dev->notify_ops->vring_state_changed)
1869 dev->notify_ops->vring_state_changed(
1870 dev->vid, file.index, 1);
1873 if (vq->kickfd >= 0)
1875 vq->kickfd = file.fd;
1877 if (vq_is_packed(dev)) {
1878 if (vhost_check_queue_inflights_packed(dev, vq)) {
1879 VHOST_LOG_CONFIG(ERR,
1880 "failed to inflights for vq: %d\n", file.index);
1881 return RTE_VHOST_MSG_RESULT_ERR;
1884 if (vhost_check_queue_inflights_split(dev, vq)) {
1885 VHOST_LOG_CONFIG(ERR,
1886 "failed to inflights for vq: %d\n", file.index);
1887 return RTE_VHOST_MSG_RESULT_ERR;
1891 return RTE_VHOST_MSG_RESULT_OK;
1895 free_zmbufs(struct vhost_virtqueue *vq)
1897 drain_zmbuf_list(vq);
1899 rte_free(vq->zmbufs);
1903 * when virtio is stopped, qemu will send us the GET_VRING_BASE message.
1906 vhost_user_get_vring_base(struct virtio_net **pdev,
1907 struct VhostUserMsg *msg,
1908 int main_fd __rte_unused)
1910 struct virtio_net *dev = *pdev;
1911 struct vhost_virtqueue *vq = dev->virtqueue[msg->payload.state.index];
1914 if (validate_msg_fds(msg, 0) != 0)
1915 return RTE_VHOST_MSG_RESULT_ERR;
1917 /* We have to stop the queue (virtio) if it is running. */
1918 vhost_destroy_device_notify(dev);
1920 dev->flags &= ~VIRTIO_DEV_READY;
1921 dev->flags &= ~VIRTIO_DEV_VDPA_CONFIGURED;
1923 /* Here we are safe to get the indexes */
1924 if (vq_is_packed(dev)) {
1926 * Bit[0:14]: avail index
1927 * Bit[15]: avail wrap counter
1929 val = vq->last_avail_idx & 0x7fff;
1930 val |= vq->avail_wrap_counter << 15;
1931 msg->payload.state.num = val;
1933 msg->payload.state.num = vq->last_avail_idx;
1936 VHOST_LOG_CONFIG(INFO,
1937 "vring base idx:%d file:%d\n", msg->payload.state.index,
1938 msg->payload.state.num);
1940 * Based on current qemu vhost-user implementation, this message is
1941 * sent and only sent in vhost_vring_stop.
1942 * TODO: cleanup the vring, it isn't usable since here.
1944 if (vq->kickfd >= 0)
1947 vq->kickfd = VIRTIO_UNINITIALIZED_EVENTFD;
1949 if (vq->callfd >= 0)
1952 vq->callfd = VIRTIO_UNINITIALIZED_EVENTFD;
1954 vq->signalled_used_valid = false;
1956 if (dev->dequeue_zero_copy)
1958 if (vq_is_packed(dev)) {
1959 rte_free(vq->shadow_used_packed);
1960 vq->shadow_used_packed = NULL;
1962 rte_free(vq->shadow_used_split);
1963 vq->shadow_used_split = NULL;
1966 rte_free(vq->batch_copy_elems);
1967 vq->batch_copy_elems = NULL;
1969 msg->size = sizeof(msg->payload.state);
1972 vring_invalidate(dev, vq);
1974 return RTE_VHOST_MSG_RESULT_REPLY;
1978 * when virtio queues are ready to work, qemu will send us to
1979 * enable the virtio queue pair.
1982 vhost_user_set_vring_enable(struct virtio_net **pdev,
1983 struct VhostUserMsg *msg,
1984 int main_fd __rte_unused)
1986 struct virtio_net *dev = *pdev;
1987 int enable = (int)msg->payload.state.num;
1988 int index = (int)msg->payload.state.index;
1990 if (validate_msg_fds(msg, 0) != 0)
1991 return RTE_VHOST_MSG_RESULT_ERR;
1993 VHOST_LOG_CONFIG(INFO,
1994 "set queue enable: %d to qp idx: %d\n",
1997 /* On disable, rings have to be stopped being processed. */
1998 if (!enable && dev->dequeue_zero_copy)
1999 drain_zmbuf_list(dev->virtqueue[index]);
2001 dev->virtqueue[index]->enabled = enable;
2003 return RTE_VHOST_MSG_RESULT_OK;
2007 vhost_user_get_protocol_features(struct virtio_net **pdev,
2008 struct VhostUserMsg *msg,
2009 int main_fd __rte_unused)
2011 struct virtio_net *dev = *pdev;
2012 uint64_t features, protocol_features;
2014 if (validate_msg_fds(msg, 0) != 0)
2015 return RTE_VHOST_MSG_RESULT_ERR;
2017 rte_vhost_driver_get_features(dev->ifname, &features);
2018 rte_vhost_driver_get_protocol_features(dev->ifname, &protocol_features);
2020 msg->payload.u64 = protocol_features;
2021 msg->size = sizeof(msg->payload.u64);
2024 return RTE_VHOST_MSG_RESULT_REPLY;
2028 vhost_user_set_protocol_features(struct virtio_net **pdev,
2029 struct VhostUserMsg *msg,
2030 int main_fd __rte_unused)
2032 struct virtio_net *dev = *pdev;
2033 uint64_t protocol_features = msg->payload.u64;
2034 uint64_t slave_protocol_features = 0;
2036 if (validate_msg_fds(msg, 0) != 0)
2037 return RTE_VHOST_MSG_RESULT_ERR;
2039 rte_vhost_driver_get_protocol_features(dev->ifname,
2040 &slave_protocol_features);
2041 if (protocol_features & ~slave_protocol_features) {
2042 VHOST_LOG_CONFIG(ERR,
2043 "(%d) received invalid protocol features.\n",
2045 return RTE_VHOST_MSG_RESULT_ERR;
2048 dev->protocol_features = protocol_features;
2049 VHOST_LOG_CONFIG(INFO,
2050 "negotiated Vhost-user protocol features: 0x%" PRIx64 "\n",
2051 dev->protocol_features);
2053 return RTE_VHOST_MSG_RESULT_OK;
2057 vhost_user_set_log_base(struct virtio_net **pdev, struct VhostUserMsg *msg,
2058 int main_fd __rte_unused)
2060 struct virtio_net *dev = *pdev;
2061 int fd = msg->fds[0];
2065 if (validate_msg_fds(msg, 1) != 0)
2066 return RTE_VHOST_MSG_RESULT_ERR;
2069 VHOST_LOG_CONFIG(ERR, "invalid log fd: %d\n", fd);
2070 return RTE_VHOST_MSG_RESULT_ERR;
2073 if (msg->size != sizeof(VhostUserLog)) {
2074 VHOST_LOG_CONFIG(ERR,
2075 "invalid log base msg size: %"PRId32" != %d\n",
2076 msg->size, (int)sizeof(VhostUserLog));
2077 return RTE_VHOST_MSG_RESULT_ERR;
2080 size = msg->payload.log.mmap_size;
2081 off = msg->payload.log.mmap_offset;
2083 /* Check for mmap size and offset overflow. */
2085 VHOST_LOG_CONFIG(ERR,
2086 "log offset %#"PRIx64" and log size %#"PRIx64" overflow\n",
2088 return RTE_VHOST_MSG_RESULT_ERR;
2091 VHOST_LOG_CONFIG(INFO,
2092 "log mmap size: %"PRId64", offset: %"PRId64"\n",
2096 * mmap from 0 to workaround a hugepage mmap bug: mmap will
2097 * fail when offset is not page size aligned.
2099 addr = mmap(0, size + off, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
2101 if (addr == MAP_FAILED) {
2102 VHOST_LOG_CONFIG(ERR, "mmap log base failed!\n");
2103 return RTE_VHOST_MSG_RESULT_ERR;
2107 * Free previously mapped log memory on occasionally
2108 * multiple VHOST_USER_SET_LOG_BASE.
2110 if (dev->log_addr) {
2111 munmap((void *)(uintptr_t)dev->log_addr, dev->log_size);
2113 dev->log_addr = (uint64_t)(uintptr_t)addr;
2114 dev->log_base = dev->log_addr + off;
2115 dev->log_size = size;
2118 * The spec is not clear about it (yet), but QEMU doesn't expect
2119 * any payload in the reply.
2124 return RTE_VHOST_MSG_RESULT_REPLY;
2127 static int vhost_user_set_log_fd(struct virtio_net **pdev __rte_unused,
2128 struct VhostUserMsg *msg,
2129 int main_fd __rte_unused)
2131 if (validate_msg_fds(msg, 1) != 0)
2132 return RTE_VHOST_MSG_RESULT_ERR;
2135 VHOST_LOG_CONFIG(INFO, "not implemented.\n");
2137 return RTE_VHOST_MSG_RESULT_OK;
2141 * An rarp packet is constructed and broadcasted to notify switches about
2142 * the new location of the migrated VM, so that packets from outside will
2143 * not be lost after migration.
2145 * However, we don't actually "send" a rarp packet here, instead, we set
2146 * a flag 'broadcast_rarp' to let rte_vhost_dequeue_burst() inject it.
2149 vhost_user_send_rarp(struct virtio_net **pdev, struct VhostUserMsg *msg,
2150 int main_fd __rte_unused)
2152 struct virtio_net *dev = *pdev;
2153 uint8_t *mac = (uint8_t *)&msg->payload.u64;
2154 struct rte_vdpa_device *vdpa_dev;
2156 if (validate_msg_fds(msg, 0) != 0)
2157 return RTE_VHOST_MSG_RESULT_ERR;
2159 VHOST_LOG_CONFIG(DEBUG,
2160 ":: mac: %02x:%02x:%02x:%02x:%02x:%02x\n",
2161 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
2162 memcpy(dev->mac.addr_bytes, mac, 6);
2165 * Set the flag to inject a RARP broadcast packet at
2166 * rte_vhost_dequeue_burst().
2168 * __ATOMIC_RELEASE ordering is for making sure the mac is
2169 * copied before the flag is set.
2171 __atomic_store_n(&dev->broadcast_rarp, 1, __ATOMIC_RELEASE);
2172 vdpa_dev = dev->vdpa_dev;
2173 if (vdpa_dev && vdpa_dev->ops->migration_done)
2174 vdpa_dev->ops->migration_done(dev->vid);
2176 return RTE_VHOST_MSG_RESULT_OK;
2180 vhost_user_net_set_mtu(struct virtio_net **pdev, struct VhostUserMsg *msg,
2181 int main_fd __rte_unused)
2183 struct virtio_net *dev = *pdev;
2185 if (validate_msg_fds(msg, 0) != 0)
2186 return RTE_VHOST_MSG_RESULT_ERR;
2188 if (msg->payload.u64 < VIRTIO_MIN_MTU ||
2189 msg->payload.u64 > VIRTIO_MAX_MTU) {
2190 VHOST_LOG_CONFIG(ERR, "Invalid MTU size (%"PRIu64")\n",
2193 return RTE_VHOST_MSG_RESULT_ERR;
2196 dev->mtu = msg->payload.u64;
2198 return RTE_VHOST_MSG_RESULT_OK;
2202 vhost_user_set_req_fd(struct virtio_net **pdev, struct VhostUserMsg *msg,
2203 int main_fd __rte_unused)
2205 struct virtio_net *dev = *pdev;
2206 int fd = msg->fds[0];
2208 if (validate_msg_fds(msg, 1) != 0)
2209 return RTE_VHOST_MSG_RESULT_ERR;
2212 VHOST_LOG_CONFIG(ERR,
2213 "Invalid file descriptor for slave channel (%d)\n",
2215 return RTE_VHOST_MSG_RESULT_ERR;
2218 if (dev->slave_req_fd >= 0)
2219 close(dev->slave_req_fd);
2221 dev->slave_req_fd = fd;
2223 return RTE_VHOST_MSG_RESULT_OK;
2227 is_vring_iotlb_split(struct vhost_virtqueue *vq, struct vhost_iotlb_msg *imsg)
2229 struct vhost_vring_addr *ra;
2230 uint64_t start, end, len;
2233 end = start + imsg->size;
2235 ra = &vq->ring_addrs;
2236 len = sizeof(struct vring_desc) * vq->size;
2237 if (ra->desc_user_addr < end && (ra->desc_user_addr + len) > start)
2240 len = sizeof(struct vring_avail) + sizeof(uint16_t) * vq->size;
2241 if (ra->avail_user_addr < end && (ra->avail_user_addr + len) > start)
2244 len = sizeof(struct vring_used) +
2245 sizeof(struct vring_used_elem) * vq->size;
2246 if (ra->used_user_addr < end && (ra->used_user_addr + len) > start)
2249 if (ra->flags & (1 << VHOST_VRING_F_LOG)) {
2250 len = sizeof(uint64_t);
2251 if (ra->log_guest_addr < end &&
2252 (ra->log_guest_addr + len) > start)
2260 is_vring_iotlb_packed(struct vhost_virtqueue *vq, struct vhost_iotlb_msg *imsg)
2262 struct vhost_vring_addr *ra;
2263 uint64_t start, end, len;
2266 end = start + imsg->size;
2268 ra = &vq->ring_addrs;
2269 len = sizeof(struct vring_packed_desc) * vq->size;
2270 if (ra->desc_user_addr < end && (ra->desc_user_addr + len) > start)
2273 len = sizeof(struct vring_packed_desc_event);
2274 if (ra->avail_user_addr < end && (ra->avail_user_addr + len) > start)
2277 len = sizeof(struct vring_packed_desc_event);
2278 if (ra->used_user_addr < end && (ra->used_user_addr + len) > start)
2281 if (ra->flags & (1 << VHOST_VRING_F_LOG)) {
2282 len = sizeof(uint64_t);
2283 if (ra->log_guest_addr < end &&
2284 (ra->log_guest_addr + len) > start)
2291 static int is_vring_iotlb(struct virtio_net *dev,
2292 struct vhost_virtqueue *vq,
2293 struct vhost_iotlb_msg *imsg)
2295 if (vq_is_packed(dev))
2296 return is_vring_iotlb_packed(vq, imsg);
2298 return is_vring_iotlb_split(vq, imsg);
2302 vhost_user_iotlb_msg(struct virtio_net **pdev, struct VhostUserMsg *msg,
2303 int main_fd __rte_unused)
2305 struct virtio_net *dev = *pdev;
2306 struct vhost_iotlb_msg *imsg = &msg->payload.iotlb;
2310 if (validate_msg_fds(msg, 0) != 0)
2311 return RTE_VHOST_MSG_RESULT_ERR;
2313 switch (imsg->type) {
2314 case VHOST_IOTLB_UPDATE:
2316 vva = qva_to_vva(dev, imsg->uaddr, &len);
2318 return RTE_VHOST_MSG_RESULT_ERR;
2320 for (i = 0; i < dev->nr_vring; i++) {
2321 struct vhost_virtqueue *vq = dev->virtqueue[i];
2323 vhost_user_iotlb_cache_insert(vq, imsg->iova, vva,
2326 if (is_vring_iotlb(dev, vq, imsg))
2327 *pdev = dev = translate_ring_addresses(dev, i);
2330 case VHOST_IOTLB_INVALIDATE:
2331 for (i = 0; i < dev->nr_vring; i++) {
2332 struct vhost_virtqueue *vq = dev->virtqueue[i];
2334 vhost_user_iotlb_cache_remove(vq, imsg->iova,
2337 if (is_vring_iotlb(dev, vq, imsg))
2338 vring_invalidate(dev, vq);
2342 VHOST_LOG_CONFIG(ERR, "Invalid IOTLB message type (%d)\n",
2344 return RTE_VHOST_MSG_RESULT_ERR;
2347 return RTE_VHOST_MSG_RESULT_OK;
2351 vhost_user_set_postcopy_advise(struct virtio_net **pdev,
2352 struct VhostUserMsg *msg,
2353 int main_fd __rte_unused)
2355 struct virtio_net *dev = *pdev;
2356 #ifdef RTE_LIBRTE_VHOST_POSTCOPY
2357 struct uffdio_api api_struct;
2359 if (validate_msg_fds(msg, 0) != 0)
2360 return RTE_VHOST_MSG_RESULT_ERR;
2362 dev->postcopy_ufd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK);
2364 if (dev->postcopy_ufd == -1) {
2365 VHOST_LOG_CONFIG(ERR, "Userfaultfd not available: %s\n",
2367 return RTE_VHOST_MSG_RESULT_ERR;
2369 api_struct.api = UFFD_API;
2370 api_struct.features = 0;
2371 if (ioctl(dev->postcopy_ufd, UFFDIO_API, &api_struct)) {
2372 VHOST_LOG_CONFIG(ERR, "UFFDIO_API ioctl failure: %s\n",
2374 close(dev->postcopy_ufd);
2375 dev->postcopy_ufd = -1;
2376 return RTE_VHOST_MSG_RESULT_ERR;
2378 msg->fds[0] = dev->postcopy_ufd;
2381 return RTE_VHOST_MSG_RESULT_REPLY;
2383 dev->postcopy_ufd = -1;
2386 return RTE_VHOST_MSG_RESULT_ERR;
2391 vhost_user_set_postcopy_listen(struct virtio_net **pdev,
2392 struct VhostUserMsg *msg __rte_unused,
2393 int main_fd __rte_unused)
2395 struct virtio_net *dev = *pdev;
2397 if (validate_msg_fds(msg, 0) != 0)
2398 return RTE_VHOST_MSG_RESULT_ERR;
2400 if (dev->mem && dev->mem->nregions) {
2401 VHOST_LOG_CONFIG(ERR,
2402 "Regions already registered at postcopy-listen\n");
2403 return RTE_VHOST_MSG_RESULT_ERR;
2405 dev->postcopy_listening = 1;
2407 return RTE_VHOST_MSG_RESULT_OK;
2411 vhost_user_postcopy_end(struct virtio_net **pdev, struct VhostUserMsg *msg,
2412 int main_fd __rte_unused)
2414 struct virtio_net *dev = *pdev;
2416 if (validate_msg_fds(msg, 0) != 0)
2417 return RTE_VHOST_MSG_RESULT_ERR;
2419 dev->postcopy_listening = 0;
2420 if (dev->postcopy_ufd >= 0) {
2421 close(dev->postcopy_ufd);
2422 dev->postcopy_ufd = -1;
2425 msg->payload.u64 = 0;
2426 msg->size = sizeof(msg->payload.u64);
2429 return RTE_VHOST_MSG_RESULT_REPLY;
2432 typedef int (*vhost_message_handler_t)(struct virtio_net **pdev,
2433 struct VhostUserMsg *msg,
2435 static vhost_message_handler_t vhost_message_handlers[VHOST_USER_MAX] = {
2436 [VHOST_USER_NONE] = NULL,
2437 [VHOST_USER_GET_FEATURES] = vhost_user_get_features,
2438 [VHOST_USER_SET_FEATURES] = vhost_user_set_features,
2439 [VHOST_USER_SET_OWNER] = vhost_user_set_owner,
2440 [VHOST_USER_RESET_OWNER] = vhost_user_reset_owner,
2441 [VHOST_USER_SET_MEM_TABLE] = vhost_user_set_mem_table,
2442 [VHOST_USER_SET_LOG_BASE] = vhost_user_set_log_base,
2443 [VHOST_USER_SET_LOG_FD] = vhost_user_set_log_fd,
2444 [VHOST_USER_SET_VRING_NUM] = vhost_user_set_vring_num,
2445 [VHOST_USER_SET_VRING_ADDR] = vhost_user_set_vring_addr,
2446 [VHOST_USER_SET_VRING_BASE] = vhost_user_set_vring_base,
2447 [VHOST_USER_GET_VRING_BASE] = vhost_user_get_vring_base,
2448 [VHOST_USER_SET_VRING_KICK] = vhost_user_set_vring_kick,
2449 [VHOST_USER_SET_VRING_CALL] = vhost_user_set_vring_call,
2450 [VHOST_USER_SET_VRING_ERR] = vhost_user_set_vring_err,
2451 [VHOST_USER_GET_PROTOCOL_FEATURES] = vhost_user_get_protocol_features,
2452 [VHOST_USER_SET_PROTOCOL_FEATURES] = vhost_user_set_protocol_features,
2453 [VHOST_USER_GET_QUEUE_NUM] = vhost_user_get_queue_num,
2454 [VHOST_USER_SET_VRING_ENABLE] = vhost_user_set_vring_enable,
2455 [VHOST_USER_SEND_RARP] = vhost_user_send_rarp,
2456 [VHOST_USER_NET_SET_MTU] = vhost_user_net_set_mtu,
2457 [VHOST_USER_SET_SLAVE_REQ_FD] = vhost_user_set_req_fd,
2458 [VHOST_USER_IOTLB_MSG] = vhost_user_iotlb_msg,
2459 [VHOST_USER_POSTCOPY_ADVISE] = vhost_user_set_postcopy_advise,
2460 [VHOST_USER_POSTCOPY_LISTEN] = vhost_user_set_postcopy_listen,
2461 [VHOST_USER_POSTCOPY_END] = vhost_user_postcopy_end,
2462 [VHOST_USER_GET_INFLIGHT_FD] = vhost_user_get_inflight_fd,
2463 [VHOST_USER_SET_INFLIGHT_FD] = vhost_user_set_inflight_fd,
2466 /* return bytes# of read on success or negative val on failure. */
2468 read_vhost_message(int sockfd, struct VhostUserMsg *msg)
2472 ret = read_fd_message(sockfd, (char *)msg, VHOST_USER_HDR_SIZE,
2473 msg->fds, VHOST_MEMORY_MAX_NREGIONS, &msg->fd_num);
2476 } else if (ret != VHOST_USER_HDR_SIZE) {
2477 VHOST_LOG_CONFIG(ERR, "Unexpected header size read\n");
2483 if (msg->size > sizeof(msg->payload)) {
2484 VHOST_LOG_CONFIG(ERR,
2485 "invalid msg size: %d\n", msg->size);
2488 ret = read(sockfd, &msg->payload, msg->size);
2491 if (ret != (int)msg->size) {
2492 VHOST_LOG_CONFIG(ERR,
2493 "read control message failed\n");
2502 send_vhost_message(int sockfd, struct VhostUserMsg *msg)
2507 return send_fd_message(sockfd, (char *)msg,
2508 VHOST_USER_HDR_SIZE + msg->size, msg->fds, msg->fd_num);
2512 send_vhost_reply(int sockfd, struct VhostUserMsg *msg)
2517 msg->flags &= ~VHOST_USER_VERSION_MASK;
2518 msg->flags &= ~VHOST_USER_NEED_REPLY;
2519 msg->flags |= VHOST_USER_VERSION;
2520 msg->flags |= VHOST_USER_REPLY_MASK;
2522 return send_vhost_message(sockfd, msg);
2526 send_vhost_slave_message(struct virtio_net *dev, struct VhostUserMsg *msg)
2530 if (msg->flags & VHOST_USER_NEED_REPLY)
2531 rte_spinlock_lock(&dev->slave_req_lock);
2533 ret = send_vhost_message(dev->slave_req_fd, msg);
2534 if (ret < 0 && (msg->flags & VHOST_USER_NEED_REPLY))
2535 rte_spinlock_unlock(&dev->slave_req_lock);
2541 * Allocate a queue pair if it hasn't been allocated yet
2544 vhost_user_check_and_alloc_queue_pair(struct virtio_net *dev,
2545 struct VhostUserMsg *msg)
2549 switch (msg->request.master) {
2550 case VHOST_USER_SET_VRING_KICK:
2551 case VHOST_USER_SET_VRING_CALL:
2552 case VHOST_USER_SET_VRING_ERR:
2553 vring_idx = msg->payload.u64 & VHOST_USER_VRING_IDX_MASK;
2555 case VHOST_USER_SET_VRING_NUM:
2556 case VHOST_USER_SET_VRING_BASE:
2557 case VHOST_USER_SET_VRING_ENABLE:
2558 vring_idx = msg->payload.state.index;
2560 case VHOST_USER_SET_VRING_ADDR:
2561 vring_idx = msg->payload.addr.index;
2567 if (vring_idx >= VHOST_MAX_VRING) {
2568 VHOST_LOG_CONFIG(ERR,
2569 "invalid vring index: %u\n", vring_idx);
2573 if (dev->virtqueue[vring_idx])
2576 return alloc_vring_queue(dev, vring_idx);
2580 vhost_user_lock_all_queue_pairs(struct virtio_net *dev)
2583 unsigned int vq_num = 0;
2585 while (vq_num < dev->nr_vring) {
2586 struct vhost_virtqueue *vq = dev->virtqueue[i];
2589 rte_spinlock_lock(&vq->access_lock);
2597 vhost_user_unlock_all_queue_pairs(struct virtio_net *dev)
2600 unsigned int vq_num = 0;
2602 while (vq_num < dev->nr_vring) {
2603 struct vhost_virtqueue *vq = dev->virtqueue[i];
2606 rte_spinlock_unlock(&vq->access_lock);
2614 vhost_user_msg_handler(int vid, int fd)
2616 struct virtio_net *dev;
2617 struct VhostUserMsg msg;
2618 struct rte_vdpa_device *vdpa_dev;
2620 int unlock_required = 0;
2625 dev = get_device(vid);
2629 if (!dev->notify_ops) {
2630 dev->notify_ops = vhost_driver_callback_get(dev->ifname);
2631 if (!dev->notify_ops) {
2632 VHOST_LOG_CONFIG(ERR,
2633 "failed to get callback ops for driver %s\n",
2639 ret = read_vhost_message(fd, &msg);
2642 VHOST_LOG_CONFIG(ERR,
2643 "vhost read message failed\n");
2645 VHOST_LOG_CONFIG(INFO,
2646 "vhost peer closed\n");
2652 request = msg.request.master;
2653 if (request > VHOST_USER_NONE && request < VHOST_USER_MAX &&
2654 vhost_message_str[request]) {
2655 if (request != VHOST_USER_IOTLB_MSG)
2656 VHOST_LOG_CONFIG(INFO, "read message %s\n",
2657 vhost_message_str[request]);
2659 VHOST_LOG_CONFIG(DEBUG, "read message %s\n",
2660 vhost_message_str[request]);
2662 VHOST_LOG_CONFIG(DEBUG, "External request %d\n", request);
2665 ret = vhost_user_check_and_alloc_queue_pair(dev, &msg);
2667 VHOST_LOG_CONFIG(ERR,
2668 "failed to alloc queue\n");
2673 * Note: we don't lock all queues on VHOST_USER_GET_VRING_BASE
2674 * and VHOST_USER_RESET_OWNER, since it is sent when virtio stops
2675 * and device is destroyed. destroy_device waits for queues to be
2676 * inactive, so it is safe. Otherwise taking the access_lock
2677 * would cause a dead lock.
2680 case VHOST_USER_SET_FEATURES:
2681 case VHOST_USER_SET_PROTOCOL_FEATURES:
2682 case VHOST_USER_SET_OWNER:
2683 case VHOST_USER_SET_MEM_TABLE:
2684 case VHOST_USER_SET_LOG_BASE:
2685 case VHOST_USER_SET_LOG_FD:
2686 case VHOST_USER_SET_VRING_NUM:
2687 case VHOST_USER_SET_VRING_ADDR:
2688 case VHOST_USER_SET_VRING_BASE:
2689 case VHOST_USER_SET_VRING_KICK:
2690 case VHOST_USER_SET_VRING_CALL:
2691 case VHOST_USER_SET_VRING_ERR:
2692 case VHOST_USER_SET_VRING_ENABLE:
2693 case VHOST_USER_SEND_RARP:
2694 case VHOST_USER_NET_SET_MTU:
2695 case VHOST_USER_SET_SLAVE_REQ_FD:
2696 if (!(dev->flags & VIRTIO_DEV_VDPA_CONFIGURED)) {
2697 vhost_user_lock_all_queue_pairs(dev);
2698 unlock_required = 1;
2707 if (dev->extern_ops.pre_msg_handle) {
2708 ret = (*dev->extern_ops.pre_msg_handle)(dev->vid,
2711 case RTE_VHOST_MSG_RESULT_REPLY:
2712 send_vhost_reply(fd, &msg);
2714 case RTE_VHOST_MSG_RESULT_ERR:
2715 case RTE_VHOST_MSG_RESULT_OK:
2717 goto skip_to_post_handle;
2718 case RTE_VHOST_MSG_RESULT_NOT_HANDLED:
2724 if (request > VHOST_USER_NONE && request < VHOST_USER_MAX) {
2725 if (!vhost_message_handlers[request])
2726 goto skip_to_post_handle;
2727 ret = vhost_message_handlers[request](&dev, &msg, fd);
2730 case RTE_VHOST_MSG_RESULT_ERR:
2731 VHOST_LOG_CONFIG(ERR,
2732 "Processing %s failed.\n",
2733 vhost_message_str[request]);
2736 case RTE_VHOST_MSG_RESULT_OK:
2737 VHOST_LOG_CONFIG(DEBUG,
2738 "Processing %s succeeded.\n",
2739 vhost_message_str[request]);
2742 case RTE_VHOST_MSG_RESULT_REPLY:
2743 VHOST_LOG_CONFIG(DEBUG,
2744 "Processing %s succeeded and needs reply.\n",
2745 vhost_message_str[request]);
2746 send_vhost_reply(fd, &msg);
2754 skip_to_post_handle:
2755 if (ret != RTE_VHOST_MSG_RESULT_ERR &&
2756 dev->extern_ops.post_msg_handle) {
2757 ret = (*dev->extern_ops.post_msg_handle)(dev->vid,
2760 case RTE_VHOST_MSG_RESULT_REPLY:
2761 send_vhost_reply(fd, &msg);
2763 case RTE_VHOST_MSG_RESULT_ERR:
2764 case RTE_VHOST_MSG_RESULT_OK:
2766 case RTE_VHOST_MSG_RESULT_NOT_HANDLED:
2772 if (unlock_required)
2773 vhost_user_unlock_all_queue_pairs(dev);
2775 /* If message was not handled at this stage, treat it as an error */
2777 VHOST_LOG_CONFIG(ERR,
2778 "vhost message (req: %d) was not handled.\n", request);
2779 close_msg_fds(&msg);
2780 ret = RTE_VHOST_MSG_RESULT_ERR;
2784 * If the request required a reply that was already sent,
2785 * this optional reply-ack won't be sent as the
2786 * VHOST_USER_NEED_REPLY was cleared in send_vhost_reply().
2788 if (msg.flags & VHOST_USER_NEED_REPLY) {
2789 msg.payload.u64 = ret == RTE_VHOST_MSG_RESULT_ERR;
2790 msg.size = sizeof(msg.payload.u64);
2792 send_vhost_reply(fd, &msg);
2793 } else if (ret == RTE_VHOST_MSG_RESULT_ERR) {
2794 VHOST_LOG_CONFIG(ERR,
2795 "vhost message handling failed.\n");
2799 for (i = 0; i < dev->nr_vring; i++) {
2800 struct vhost_virtqueue *vq = dev->virtqueue[i];
2801 bool cur_ready = vq_is_ready(dev, vq);
2803 if (cur_ready != (vq && vq->ready)) {
2804 vhost_user_notify_queue_state(dev, i, cur_ready);
2805 vq->ready = cur_ready;
2810 if (!(dev->flags & VIRTIO_DEV_RUNNING) && virtio_is_ready(dev)) {
2811 dev->flags |= VIRTIO_DEV_READY;
2813 if (!(dev->flags & VIRTIO_DEV_RUNNING)) {
2814 if (dev->dequeue_zero_copy) {
2815 VHOST_LOG_CONFIG(INFO,
2816 "dequeue zero copy is enabled\n");
2819 if (dev->notify_ops->new_device(dev->vid) == 0)
2820 dev->flags |= VIRTIO_DEV_RUNNING;
2824 vdpa_dev = dev->vdpa_dev;
2825 if (vdpa_dev && virtio_is_ready(dev) &&
2826 !(dev->flags & VIRTIO_DEV_VDPA_CONFIGURED)) {
2827 if (vdpa_dev->ops->dev_conf)
2828 vdpa_dev->ops->dev_conf(dev->vid);
2829 dev->flags |= VIRTIO_DEV_VDPA_CONFIGURED;
2835 static int process_slave_message_reply(struct virtio_net *dev,
2836 const struct VhostUserMsg *msg)
2838 struct VhostUserMsg msg_reply;
2841 if ((msg->flags & VHOST_USER_NEED_REPLY) == 0)
2844 ret = read_vhost_message(dev->slave_req_fd, &msg_reply);
2847 VHOST_LOG_CONFIG(ERR,
2848 "vhost read slave message reply failed\n");
2850 VHOST_LOG_CONFIG(INFO,
2851 "vhost peer closed\n");
2857 if (msg_reply.request.slave != msg->request.slave) {
2858 VHOST_LOG_CONFIG(ERR,
2859 "Received unexpected msg type (%u), expected %u\n",
2860 msg_reply.request.slave, msg->request.slave);
2865 ret = msg_reply.payload.u64 ? -1 : 0;
2868 rte_spinlock_unlock(&dev->slave_req_lock);
2873 vhost_user_iotlb_miss(struct virtio_net *dev, uint64_t iova, uint8_t perm)
2876 struct VhostUserMsg msg = {
2877 .request.slave = VHOST_USER_SLAVE_IOTLB_MSG,
2878 .flags = VHOST_USER_VERSION,
2879 .size = sizeof(msg.payload.iotlb),
2883 .type = VHOST_IOTLB_MISS,
2887 ret = send_vhost_message(dev->slave_req_fd, &msg);
2889 VHOST_LOG_CONFIG(ERR,
2890 "Failed to send IOTLB miss message (%d)\n",
2899 vhost_user_slave_config_change(struct virtio_net *dev, bool need_reply)
2902 struct VhostUserMsg msg = {
2903 .request.slave = VHOST_USER_SLAVE_CONFIG_CHANGE_MSG,
2904 .flags = VHOST_USER_VERSION,
2909 msg.flags |= VHOST_USER_NEED_REPLY;
2911 ret = send_vhost_slave_message(dev, &msg);
2913 VHOST_LOG_CONFIG(ERR,
2914 "Failed to send config change (%d)\n",
2919 return process_slave_message_reply(dev, &msg);
2923 rte_vhost_slave_config_change(int vid, bool need_reply)
2925 struct virtio_net *dev;
2927 dev = get_device(vid);
2931 return vhost_user_slave_config_change(dev, need_reply);
2934 static int vhost_user_slave_set_vring_host_notifier(struct virtio_net *dev,
2940 struct VhostUserMsg msg = {
2941 .request.slave = VHOST_USER_SLAVE_VRING_HOST_NOTIFIER_MSG,
2942 .flags = VHOST_USER_VERSION | VHOST_USER_NEED_REPLY,
2943 .size = sizeof(msg.payload.area),
2945 .u64 = index & VHOST_USER_VRING_IDX_MASK,
2952 msg.payload.area.u64 |= VHOST_USER_VRING_NOFD_MASK;
2958 ret = send_vhost_slave_message(dev, &msg);
2960 VHOST_LOG_CONFIG(ERR,
2961 "Failed to set host notifier (%d)\n", ret);
2965 return process_slave_message_reply(dev, &msg);
2968 int rte_vhost_host_notifier_ctrl(int vid, uint16_t qid, bool enable)
2970 struct virtio_net *dev;
2971 struct rte_vdpa_device *vdpa_dev;
2972 int vfio_device_fd, ret = 0;
2973 uint64_t offset, size;
2974 unsigned int i, q_start, q_last;
2976 dev = get_device(vid);
2980 vdpa_dev = dev->vdpa_dev;
2981 if (vdpa_dev == NULL)
2984 if (!(dev->features & (1ULL << VIRTIO_F_VERSION_1)) ||
2985 !(dev->features & (1ULL << VHOST_USER_F_PROTOCOL_FEATURES)) ||
2986 !(dev->protocol_features &
2987 (1ULL << VHOST_USER_PROTOCOL_F_SLAVE_REQ)) ||
2988 !(dev->protocol_features &
2989 (1ULL << VHOST_USER_PROTOCOL_F_SLAVE_SEND_FD)) ||
2990 !(dev->protocol_features &
2991 (1ULL << VHOST_USER_PROTOCOL_F_HOST_NOTIFIER)))
2994 if (qid == RTE_VHOST_QUEUE_ALL) {
2996 q_last = dev->nr_vring - 1;
2998 if (qid >= dev->nr_vring)
3004 RTE_FUNC_PTR_OR_ERR_RET(vdpa_dev->ops->get_vfio_device_fd, -ENOTSUP);
3005 RTE_FUNC_PTR_OR_ERR_RET(vdpa_dev->ops->get_notify_area, -ENOTSUP);
3007 vfio_device_fd = vdpa_dev->ops->get_vfio_device_fd(vid);
3008 if (vfio_device_fd < 0)
3012 for (i = q_start; i <= q_last; i++) {
3013 if (vdpa_dev->ops->get_notify_area(vid, i, &offset,
3019 if (vhost_user_slave_set_vring_host_notifier(dev, i,
3020 vfio_device_fd, offset, size) < 0) {
3027 for (i = q_start; i <= q_last; i++) {
3028 vhost_user_slave_set_vring_host_notifier(dev, i, -1,