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 * This function just returns success at the moment unless
233 * the device hasn't been initialised.
236 vhost_user_set_owner(struct virtio_net **pdev __rte_unused,
237 struct VhostUserMsg *msg,
238 int main_fd __rte_unused)
240 if (validate_msg_fds(msg, 0) != 0)
241 return RTE_VHOST_MSG_RESULT_ERR;
243 return RTE_VHOST_MSG_RESULT_OK;
247 vhost_user_reset_owner(struct virtio_net **pdev,
248 struct VhostUserMsg *msg,
249 int main_fd __rte_unused)
251 struct virtio_net *dev = *pdev;
253 if (validate_msg_fds(msg, 0) != 0)
254 return RTE_VHOST_MSG_RESULT_ERR;
256 vhost_destroy_device_notify(dev);
258 cleanup_device(dev, 0);
260 return RTE_VHOST_MSG_RESULT_OK;
264 * The features that we support are requested.
267 vhost_user_get_features(struct virtio_net **pdev, struct VhostUserMsg *msg,
268 int main_fd __rte_unused)
270 struct virtio_net *dev = *pdev;
271 uint64_t features = 0;
273 if (validate_msg_fds(msg, 0) != 0)
274 return RTE_VHOST_MSG_RESULT_ERR;
276 rte_vhost_driver_get_features(dev->ifname, &features);
278 msg->payload.u64 = features;
279 msg->size = sizeof(msg->payload.u64);
282 return RTE_VHOST_MSG_RESULT_REPLY;
286 * The queue number that we support are requested.
289 vhost_user_get_queue_num(struct virtio_net **pdev, struct VhostUserMsg *msg,
290 int main_fd __rte_unused)
292 struct virtio_net *dev = *pdev;
293 uint32_t queue_num = 0;
295 if (validate_msg_fds(msg, 0) != 0)
296 return RTE_VHOST_MSG_RESULT_ERR;
298 rte_vhost_driver_get_queue_num(dev->ifname, &queue_num);
300 msg->payload.u64 = (uint64_t)queue_num;
301 msg->size = sizeof(msg->payload.u64);
304 return RTE_VHOST_MSG_RESULT_REPLY;
308 * We receive the negotiated features supported by us and the virtio device.
311 vhost_user_set_features(struct virtio_net **pdev, struct VhostUserMsg *msg,
312 int main_fd __rte_unused)
314 struct virtio_net *dev = *pdev;
315 uint64_t features = msg->payload.u64;
316 uint64_t vhost_features = 0;
317 struct rte_vdpa_device *vdpa_dev;
319 if (validate_msg_fds(msg, 0) != 0)
320 return RTE_VHOST_MSG_RESULT_ERR;
322 rte_vhost_driver_get_features(dev->ifname, &vhost_features);
323 if (features & ~vhost_features) {
324 VHOST_LOG_CONFIG(ERR,
325 "(%d) received invalid negotiated features.\n",
327 return RTE_VHOST_MSG_RESULT_ERR;
330 if (dev->flags & VIRTIO_DEV_RUNNING) {
331 if (dev->features == features)
332 return RTE_VHOST_MSG_RESULT_OK;
335 * Error out if master tries to change features while device is
336 * in running state. The exception being VHOST_F_LOG_ALL, which
337 * is enabled when the live-migration starts.
339 if ((dev->features ^ features) & ~(1ULL << VHOST_F_LOG_ALL)) {
340 VHOST_LOG_CONFIG(ERR,
341 "(%d) features changed while device is running.\n",
343 return RTE_VHOST_MSG_RESULT_ERR;
346 if (dev->notify_ops->features_changed)
347 dev->notify_ops->features_changed(dev->vid, features);
350 dev->features = features;
352 ((1 << VIRTIO_NET_F_MRG_RXBUF) | (1ULL << VIRTIO_F_VERSION_1))) {
353 dev->vhost_hlen = sizeof(struct virtio_net_hdr_mrg_rxbuf);
355 dev->vhost_hlen = sizeof(struct virtio_net_hdr);
357 VHOST_LOG_CONFIG(INFO,
358 "negotiated Virtio features: 0x%" PRIx64 "\n", dev->features);
359 VHOST_LOG_CONFIG(DEBUG,
360 "(%d) mergeable RX buffers %s, virtio 1 %s\n",
362 (dev->features & (1 << VIRTIO_NET_F_MRG_RXBUF)) ? "on" : "off",
363 (dev->features & (1ULL << VIRTIO_F_VERSION_1)) ? "on" : "off");
365 if ((dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET) &&
366 !(dev->features & (1ULL << VIRTIO_NET_F_MQ))) {
368 * Remove all but first queue pair if MQ hasn't been
369 * negotiated. This is safe because the device is not
370 * running at this stage.
372 while (dev->nr_vring > 2) {
373 struct vhost_virtqueue *vq;
375 vq = dev->virtqueue[--dev->nr_vring];
379 dev->virtqueue[dev->nr_vring] = NULL;
381 cleanup_vq_inflight(dev, vq);
386 vdpa_dev = dev->vdpa_dev;
387 if (vdpa_dev && vdpa_dev->ops->set_features)
388 vdpa_dev->ops->set_features(dev->vid);
390 return RTE_VHOST_MSG_RESULT_OK;
394 * The virtio device sends us the size of the descriptor ring.
397 vhost_user_set_vring_num(struct virtio_net **pdev,
398 struct VhostUserMsg *msg,
399 int main_fd __rte_unused)
401 struct virtio_net *dev = *pdev;
402 struct vhost_virtqueue *vq = dev->virtqueue[msg->payload.state.index];
404 if (validate_msg_fds(msg, 0) != 0)
405 return RTE_VHOST_MSG_RESULT_ERR;
407 vq->size = msg->payload.state.num;
409 /* VIRTIO 1.0, 2.4 Virtqueues says:
411 * Queue Size value is always a power of 2. The maximum Queue Size
414 * VIRTIO 1.1 2.7 Virtqueues says:
416 * Packed virtqueues support up to 2^15 entries each.
418 if (!vq_is_packed(dev)) {
419 if (vq->size & (vq->size - 1)) {
420 VHOST_LOG_CONFIG(ERR,
421 "invalid virtqueue size %u\n", vq->size);
422 return RTE_VHOST_MSG_RESULT_ERR;
426 if (vq->size > 32768) {
427 VHOST_LOG_CONFIG(ERR,
428 "invalid virtqueue size %u\n", vq->size);
429 return RTE_VHOST_MSG_RESULT_ERR;
432 if (dev->dequeue_zero_copy) {
434 vq->last_zmbuf_idx = 0;
435 vq->zmbuf_size = vq->size;
437 rte_free(vq->zmbufs);
438 vq->zmbufs = rte_zmalloc(NULL, vq->zmbuf_size *
439 sizeof(struct zcopy_mbuf), 0);
440 if (vq->zmbufs == NULL) {
441 VHOST_LOG_CONFIG(WARNING,
442 "failed to allocate mem for zero copy; "
443 "zero copy is force disabled\n");
444 dev->dequeue_zero_copy = 0;
446 TAILQ_INIT(&vq->zmbuf_list);
449 if (vq_is_packed(dev)) {
450 if (vq->shadow_used_packed)
451 rte_free(vq->shadow_used_packed);
452 vq->shadow_used_packed = rte_malloc(NULL,
454 sizeof(struct vring_used_elem_packed),
455 RTE_CACHE_LINE_SIZE);
456 if (!vq->shadow_used_packed) {
457 VHOST_LOG_CONFIG(ERR,
458 "failed to allocate memory for shadow used ring.\n");
459 return RTE_VHOST_MSG_RESULT_ERR;
463 if (vq->shadow_used_split)
464 rte_free(vq->shadow_used_split);
465 vq->shadow_used_split = rte_malloc(NULL,
466 vq->size * sizeof(struct vring_used_elem),
467 RTE_CACHE_LINE_SIZE);
468 if (!vq->shadow_used_split) {
469 VHOST_LOG_CONFIG(ERR,
470 "failed to allocate memory for shadow used ring.\n");
471 return RTE_VHOST_MSG_RESULT_ERR;
475 if (vq->batch_copy_elems)
476 rte_free(vq->batch_copy_elems);
477 vq->batch_copy_elems = rte_malloc(NULL,
478 vq->size * sizeof(struct batch_copy_elem),
479 RTE_CACHE_LINE_SIZE);
480 if (!vq->batch_copy_elems) {
481 VHOST_LOG_CONFIG(ERR,
482 "failed to allocate memory for batching copy.\n");
483 return RTE_VHOST_MSG_RESULT_ERR;
486 return RTE_VHOST_MSG_RESULT_OK;
490 * Reallocate virtio_dev and vhost_virtqueue data structure to make them on the
491 * same numa node as the memory of vring descriptor.
493 #ifdef RTE_LIBRTE_VHOST_NUMA
494 static struct virtio_net*
495 numa_realloc(struct virtio_net *dev, int index)
497 int oldnode, newnode;
498 struct virtio_net *old_dev;
499 struct vhost_virtqueue *old_vq, *vq;
500 struct zcopy_mbuf *new_zmbuf;
501 struct vring_used_elem *new_shadow_used_split;
502 struct vring_used_elem_packed *new_shadow_used_packed;
503 struct batch_copy_elem *new_batch_copy_elems;
506 if (dev->flags & VIRTIO_DEV_RUNNING)
510 vq = old_vq = dev->virtqueue[index];
512 ret = get_mempolicy(&newnode, NULL, 0, old_vq->desc,
513 MPOL_F_NODE | MPOL_F_ADDR);
515 /* check if we need to reallocate vq */
516 ret |= get_mempolicy(&oldnode, NULL, 0, old_vq,
517 MPOL_F_NODE | MPOL_F_ADDR);
519 VHOST_LOG_CONFIG(ERR,
520 "Unable to get vq numa information.\n");
523 if (oldnode != newnode) {
524 VHOST_LOG_CONFIG(INFO,
525 "reallocate vq from %d to %d node\n", oldnode, newnode);
526 vq = rte_malloc_socket(NULL, sizeof(*vq), 0, newnode);
530 memcpy(vq, old_vq, sizeof(*vq));
531 TAILQ_INIT(&vq->zmbuf_list);
533 if (dev->dequeue_zero_copy) {
534 new_zmbuf = rte_malloc_socket(NULL, vq->zmbuf_size *
535 sizeof(struct zcopy_mbuf), 0, newnode);
537 rte_free(vq->zmbufs);
538 vq->zmbufs = new_zmbuf;
542 if (vq_is_packed(dev)) {
543 new_shadow_used_packed = rte_malloc_socket(NULL,
545 sizeof(struct vring_used_elem_packed),
548 if (new_shadow_used_packed) {
549 rte_free(vq->shadow_used_packed);
550 vq->shadow_used_packed = new_shadow_used_packed;
553 new_shadow_used_split = rte_malloc_socket(NULL,
555 sizeof(struct vring_used_elem),
558 if (new_shadow_used_split) {
559 rte_free(vq->shadow_used_split);
560 vq->shadow_used_split = new_shadow_used_split;
564 new_batch_copy_elems = rte_malloc_socket(NULL,
565 vq->size * sizeof(struct batch_copy_elem),
568 if (new_batch_copy_elems) {
569 rte_free(vq->batch_copy_elems);
570 vq->batch_copy_elems = new_batch_copy_elems;
576 /* check if we need to reallocate dev */
577 ret = get_mempolicy(&oldnode, NULL, 0, old_dev,
578 MPOL_F_NODE | MPOL_F_ADDR);
580 VHOST_LOG_CONFIG(ERR,
581 "Unable to get dev numa information.\n");
584 if (oldnode != newnode) {
585 VHOST_LOG_CONFIG(INFO,
586 "reallocate dev from %d to %d node\n",
588 dev = rte_malloc_socket(NULL, sizeof(*dev), 0, newnode);
594 memcpy(dev, old_dev, sizeof(*dev));
599 dev->virtqueue[index] = vq;
600 vhost_devices[dev->vid] = dev;
603 vhost_user_iotlb_init(dev, index);
608 static struct virtio_net*
609 numa_realloc(struct virtio_net *dev, int index __rte_unused)
615 /* Converts QEMU virtual address to Vhost virtual address. */
617 qva_to_vva(struct virtio_net *dev, uint64_t qva, uint64_t *len)
619 struct rte_vhost_mem_region *r;
622 if (unlikely(!dev || !dev->mem))
625 /* Find the region where the address lives. */
626 for (i = 0; i < dev->mem->nregions; i++) {
627 r = &dev->mem->regions[i];
629 if (qva >= r->guest_user_addr &&
630 qva < r->guest_user_addr + r->size) {
632 if (unlikely(*len > r->guest_user_addr + r->size - qva))
633 *len = r->guest_user_addr + r->size - qva;
635 return qva - r->guest_user_addr +
647 * Converts ring address to Vhost virtual address.
648 * If IOMMU is enabled, the ring address is a guest IO virtual address,
649 * else it is a QEMU virtual address.
652 ring_addr_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
653 uint64_t ra, uint64_t *size)
655 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)) {
658 vhost_user_iotlb_rd_lock(vq);
659 vva = vhost_iova_to_vva(dev, vq, ra,
660 size, VHOST_ACCESS_RW);
661 vhost_user_iotlb_rd_unlock(vq);
666 return qva_to_vva(dev, ra, size);
670 log_addr_to_gpa(struct virtio_net *dev, struct vhost_virtqueue *vq)
674 vhost_user_iotlb_rd_lock(vq);
675 log_gpa = translate_log_addr(dev, vq, vq->ring_addrs.log_guest_addr);
676 vhost_user_iotlb_rd_unlock(vq);
681 static struct virtio_net *
682 translate_ring_addresses(struct virtio_net *dev, int vq_index)
684 struct vhost_virtqueue *vq = dev->virtqueue[vq_index];
685 struct vhost_vring_addr *addr = &vq->ring_addrs;
686 uint64_t len, expected_len;
688 if (addr->flags & (1 << VHOST_VRING_F_LOG)) {
690 log_addr_to_gpa(dev, vq);
691 if (vq->log_guest_addr == 0) {
692 VHOST_LOG_CONFIG(DEBUG,
693 "(%d) failed to map log_guest_addr.\n",
699 if (vq_is_packed(dev)) {
700 len = sizeof(struct vring_packed_desc) * vq->size;
701 vq->desc_packed = (struct vring_packed_desc *)(uintptr_t)
702 ring_addr_to_vva(dev, vq, addr->desc_user_addr, &len);
703 if (vq->desc_packed == NULL ||
704 len != sizeof(struct vring_packed_desc) *
706 VHOST_LOG_CONFIG(DEBUG,
707 "(%d) failed to map desc_packed ring.\n",
712 dev = numa_realloc(dev, vq_index);
713 vq = dev->virtqueue[vq_index];
714 addr = &vq->ring_addrs;
716 len = sizeof(struct vring_packed_desc_event);
717 vq->driver_event = (struct vring_packed_desc_event *)
718 (uintptr_t)ring_addr_to_vva(dev,
719 vq, addr->avail_user_addr, &len);
720 if (vq->driver_event == NULL ||
721 len != sizeof(struct vring_packed_desc_event)) {
722 VHOST_LOG_CONFIG(DEBUG,
723 "(%d) failed to find driver area address.\n",
728 len = sizeof(struct vring_packed_desc_event);
729 vq->device_event = (struct vring_packed_desc_event *)
730 (uintptr_t)ring_addr_to_vva(dev,
731 vq, addr->used_user_addr, &len);
732 if (vq->device_event == NULL ||
733 len != sizeof(struct vring_packed_desc_event)) {
734 VHOST_LOG_CONFIG(DEBUG,
735 "(%d) failed to find device area address.\n",
744 /* The addresses are converted from QEMU virtual to Vhost virtual. */
745 if (vq->desc && vq->avail && vq->used)
748 len = sizeof(struct vring_desc) * vq->size;
749 vq->desc = (struct vring_desc *)(uintptr_t)ring_addr_to_vva(dev,
750 vq, addr->desc_user_addr, &len);
751 if (vq->desc == 0 || len != sizeof(struct vring_desc) * vq->size) {
752 VHOST_LOG_CONFIG(DEBUG,
753 "(%d) failed to map desc ring.\n",
758 dev = numa_realloc(dev, vq_index);
759 vq = dev->virtqueue[vq_index];
760 addr = &vq->ring_addrs;
762 len = sizeof(struct vring_avail) + sizeof(uint16_t) * vq->size;
763 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))
764 len += sizeof(uint16_t);
766 vq->avail = (struct vring_avail *)(uintptr_t)ring_addr_to_vva(dev,
767 vq, addr->avail_user_addr, &len);
768 if (vq->avail == 0 || len != expected_len) {
769 VHOST_LOG_CONFIG(DEBUG,
770 "(%d) failed to map avail ring.\n",
775 len = sizeof(struct vring_used) +
776 sizeof(struct vring_used_elem) * vq->size;
777 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))
778 len += sizeof(uint16_t);
780 vq->used = (struct vring_used *)(uintptr_t)ring_addr_to_vva(dev,
781 vq, addr->used_user_addr, &len);
782 if (vq->used == 0 || len != expected_len) {
783 VHOST_LOG_CONFIG(DEBUG,
784 "(%d) failed to map used ring.\n",
789 if (vq->last_used_idx != vq->used->idx) {
790 VHOST_LOG_CONFIG(WARNING,
791 "last_used_idx (%u) and vq->used->idx (%u) mismatches; "
792 "some packets maybe resent for Tx and dropped for Rx\n",
793 vq->last_used_idx, vq->used->idx);
794 vq->last_used_idx = vq->used->idx;
795 vq->last_avail_idx = vq->used->idx;
800 VHOST_LOG_CONFIG(DEBUG, "(%d) mapped address desc: %p\n",
802 VHOST_LOG_CONFIG(DEBUG, "(%d) mapped address avail: %p\n",
803 dev->vid, vq->avail);
804 VHOST_LOG_CONFIG(DEBUG, "(%d) mapped address used: %p\n",
806 VHOST_LOG_CONFIG(DEBUG, "(%d) log_guest_addr: %" PRIx64 "\n",
807 dev->vid, vq->log_guest_addr);
813 * The virtio device sends us the desc, used and avail ring addresses.
814 * This function then converts these to our address space.
817 vhost_user_set_vring_addr(struct virtio_net **pdev, struct VhostUserMsg *msg,
818 int main_fd __rte_unused)
820 struct virtio_net *dev = *pdev;
821 struct vhost_virtqueue *vq;
822 struct vhost_vring_addr *addr = &msg->payload.addr;
825 if (validate_msg_fds(msg, 0) != 0)
826 return RTE_VHOST_MSG_RESULT_ERR;
828 if (dev->mem == NULL)
829 return RTE_VHOST_MSG_RESULT_ERR;
831 /* addr->index refers to the queue index. The txq 1, rxq is 0. */
832 vq = dev->virtqueue[msg->payload.addr.index];
834 access_ok = vq->access_ok;
837 * Rings addresses should not be interpreted as long as the ring is not
838 * started and enabled
840 memcpy(&vq->ring_addrs, addr, sizeof(*addr));
842 vring_invalidate(dev, vq);
844 if ((vq->enabled && (dev->features &
845 (1ULL << VHOST_USER_F_PROTOCOL_FEATURES))) ||
847 dev = translate_ring_addresses(dev, msg->payload.addr.index);
849 return RTE_VHOST_MSG_RESULT_ERR;
854 return RTE_VHOST_MSG_RESULT_OK;
858 * The virtio device sends us the available ring last used index.
861 vhost_user_set_vring_base(struct virtio_net **pdev,
862 struct VhostUserMsg *msg,
863 int main_fd __rte_unused)
865 struct virtio_net *dev = *pdev;
866 struct vhost_virtqueue *vq = dev->virtqueue[msg->payload.state.index];
867 uint64_t val = msg->payload.state.num;
869 if (validate_msg_fds(msg, 0) != 0)
870 return RTE_VHOST_MSG_RESULT_ERR;
872 if (vq_is_packed(dev)) {
874 * Bit[0:14]: avail index
875 * Bit[15]: avail wrap counter
877 vq->last_avail_idx = val & 0x7fff;
878 vq->avail_wrap_counter = !!(val & (0x1 << 15));
880 * Set used index to same value as available one, as
881 * their values should be the same since ring processing
882 * was stopped at get time.
884 vq->last_used_idx = vq->last_avail_idx;
885 vq->used_wrap_counter = vq->avail_wrap_counter;
887 vq->last_used_idx = msg->payload.state.num;
888 vq->last_avail_idx = msg->payload.state.num;
891 return RTE_VHOST_MSG_RESULT_OK;
895 add_one_guest_page(struct virtio_net *dev, uint64_t guest_phys_addr,
896 uint64_t host_phys_addr, uint64_t size)
898 struct guest_page *page, *last_page;
899 struct guest_page *old_pages;
901 if (dev->nr_guest_pages == dev->max_guest_pages) {
902 dev->max_guest_pages *= 2;
903 old_pages = dev->guest_pages;
904 dev->guest_pages = rte_realloc(dev->guest_pages,
905 dev->max_guest_pages * sizeof(*page),
906 RTE_CACHE_LINE_SIZE);
907 if (dev->guest_pages == NULL) {
908 VHOST_LOG_CONFIG(ERR, "cannot realloc guest_pages\n");
914 if (dev->nr_guest_pages > 0) {
915 last_page = &dev->guest_pages[dev->nr_guest_pages - 1];
916 /* merge if the two pages are continuous */
917 if (host_phys_addr == last_page->host_phys_addr +
919 last_page->size += size;
924 page = &dev->guest_pages[dev->nr_guest_pages++];
925 page->guest_phys_addr = guest_phys_addr;
926 page->host_phys_addr = host_phys_addr;
933 add_guest_pages(struct virtio_net *dev, struct rte_vhost_mem_region *reg,
936 uint64_t reg_size = reg->size;
937 uint64_t host_user_addr = reg->host_user_addr;
938 uint64_t guest_phys_addr = reg->guest_phys_addr;
939 uint64_t host_phys_addr;
942 host_phys_addr = rte_mem_virt2iova((void *)(uintptr_t)host_user_addr);
943 size = page_size - (guest_phys_addr & (page_size - 1));
944 size = RTE_MIN(size, reg_size);
946 if (add_one_guest_page(dev, guest_phys_addr, host_phys_addr, size) < 0)
949 host_user_addr += size;
950 guest_phys_addr += size;
953 while (reg_size > 0) {
954 size = RTE_MIN(reg_size, page_size);
955 host_phys_addr = rte_mem_virt2iova((void *)(uintptr_t)
957 if (add_one_guest_page(dev, guest_phys_addr, host_phys_addr,
961 host_user_addr += size;
962 guest_phys_addr += size;
966 /* sort guest page array if over binary search threshold */
967 if (dev->nr_guest_pages >= VHOST_BINARY_SEARCH_THRESH) {
968 qsort((void *)dev->guest_pages, dev->nr_guest_pages,
969 sizeof(struct guest_page), guest_page_addrcmp);
975 #ifdef RTE_LIBRTE_VHOST_DEBUG
976 /* TODO: enable it only in debug mode? */
978 dump_guest_pages(struct virtio_net *dev)
981 struct guest_page *page;
983 for (i = 0; i < dev->nr_guest_pages; i++) {
984 page = &dev->guest_pages[i];
986 VHOST_LOG_CONFIG(INFO,
987 "guest physical page region %u\n"
988 "\t guest_phys_addr: %" PRIx64 "\n"
989 "\t host_phys_addr : %" PRIx64 "\n"
990 "\t size : %" PRIx64 "\n",
992 page->guest_phys_addr,
993 page->host_phys_addr,
998 #define dump_guest_pages(dev)
1002 vhost_memory_changed(struct VhostUserMemory *new,
1003 struct rte_vhost_memory *old)
1007 if (new->nregions != old->nregions)
1010 for (i = 0; i < new->nregions; ++i) {
1011 VhostUserMemoryRegion *new_r = &new->regions[i];
1012 struct rte_vhost_mem_region *old_r = &old->regions[i];
1014 if (new_r->guest_phys_addr != old_r->guest_phys_addr)
1016 if (new_r->memory_size != old_r->size)
1018 if (new_r->userspace_addr != old_r->guest_user_addr)
1026 vhost_user_set_mem_table(struct virtio_net **pdev, struct VhostUserMsg *msg,
1029 struct virtio_net *dev = *pdev;
1030 struct VhostUserMemory *memory = &msg->payload.memory;
1031 struct rte_vhost_mem_region *reg;
1034 uint64_t mmap_offset;
1040 if (validate_msg_fds(msg, memory->nregions) != 0)
1041 return RTE_VHOST_MSG_RESULT_ERR;
1043 if (memory->nregions > VHOST_MEMORY_MAX_NREGIONS) {
1044 VHOST_LOG_CONFIG(ERR,
1045 "too many memory regions (%u)\n", memory->nregions);
1046 return RTE_VHOST_MSG_RESULT_ERR;
1049 if (dev->mem && !vhost_memory_changed(memory, dev->mem)) {
1050 VHOST_LOG_CONFIG(INFO,
1051 "(%d) memory regions not changed\n", dev->vid);
1055 return RTE_VHOST_MSG_RESULT_OK;
1059 free_mem_region(dev);
1064 /* Flush IOTLB cache as previous HVAs are now invalid */
1065 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1066 for (i = 0; i < dev->nr_vring; i++)
1067 vhost_user_iotlb_flush_all(dev->virtqueue[i]);
1069 dev->nr_guest_pages = 0;
1070 if (dev->guest_pages == NULL) {
1071 dev->max_guest_pages = 8;
1072 dev->guest_pages = rte_zmalloc(NULL,
1073 dev->max_guest_pages *
1074 sizeof(struct guest_page),
1075 RTE_CACHE_LINE_SIZE);
1076 if (dev->guest_pages == NULL) {
1077 VHOST_LOG_CONFIG(ERR,
1078 "(%d) failed to allocate memory "
1079 "for dev->guest_pages\n",
1081 return RTE_VHOST_MSG_RESULT_ERR;
1085 dev->mem = rte_zmalloc("vhost-mem-table", sizeof(struct rte_vhost_memory) +
1086 sizeof(struct rte_vhost_mem_region) * memory->nregions, 0);
1087 if (dev->mem == NULL) {
1088 VHOST_LOG_CONFIG(ERR,
1089 "(%d) failed to allocate memory for dev->mem\n",
1091 return RTE_VHOST_MSG_RESULT_ERR;
1093 dev->mem->nregions = memory->nregions;
1095 for (i = 0; i < memory->nregions; i++) {
1097 reg = &dev->mem->regions[i];
1099 reg->guest_phys_addr = memory->regions[i].guest_phys_addr;
1100 reg->guest_user_addr = memory->regions[i].userspace_addr;
1101 reg->size = memory->regions[i].memory_size;
1104 mmap_offset = memory->regions[i].mmap_offset;
1106 /* Check for memory_size + mmap_offset overflow */
1107 if (mmap_offset >= -reg->size) {
1108 VHOST_LOG_CONFIG(ERR,
1109 "mmap_offset (%#"PRIx64") and memory_size "
1110 "(%#"PRIx64") overflow\n",
1111 mmap_offset, reg->size);
1115 mmap_size = reg->size + mmap_offset;
1117 /* mmap() without flag of MAP_ANONYMOUS, should be called
1118 * with length argument aligned with hugepagesz at older
1119 * longterm version Linux, like 2.6.32 and 3.2.72, or
1120 * mmap() will fail with EINVAL.
1122 * to avoid failure, make sure in caller to keep length
1125 alignment = get_blk_size(fd);
1126 if (alignment == (uint64_t)-1) {
1127 VHOST_LOG_CONFIG(ERR,
1128 "couldn't get hugepage size through fstat\n");
1131 mmap_size = RTE_ALIGN_CEIL(mmap_size, alignment);
1132 if (mmap_size == 0) {
1134 * It could happen if initial mmap_size + alignment
1135 * overflows the sizeof uint64, which could happen if
1136 * either mmap_size or alignment value is wrong.
1138 * mmap() kernel implementation would return an error,
1139 * but better catch it before and provide useful info
1142 VHOST_LOG_CONFIG(ERR, "mmap size (0x%" PRIx64 ") "
1143 "or alignment (0x%" PRIx64 ") is invalid\n",
1144 reg->size + mmap_offset, alignment);
1148 populate = (dev->dequeue_zero_copy) ? MAP_POPULATE : 0;
1149 mmap_addr = mmap(NULL, mmap_size, PROT_READ | PROT_WRITE,
1150 MAP_SHARED | populate, fd, 0);
1152 if (mmap_addr == MAP_FAILED) {
1153 VHOST_LOG_CONFIG(ERR,
1154 "mmap region %u failed.\n", i);
1158 reg->mmap_addr = mmap_addr;
1159 reg->mmap_size = mmap_size;
1160 reg->host_user_addr = (uint64_t)(uintptr_t)mmap_addr +
1163 if (dev->dequeue_zero_copy)
1164 if (add_guest_pages(dev, reg, alignment) < 0) {
1165 VHOST_LOG_CONFIG(ERR,
1166 "adding guest pages to region %u failed.\n",
1171 VHOST_LOG_CONFIG(INFO,
1172 "guest memory region %u, size: 0x%" PRIx64 "\n"
1173 "\t guest physical addr: 0x%" PRIx64 "\n"
1174 "\t guest virtual addr: 0x%" PRIx64 "\n"
1175 "\t host virtual addr: 0x%" PRIx64 "\n"
1176 "\t mmap addr : 0x%" PRIx64 "\n"
1177 "\t mmap size : 0x%" PRIx64 "\n"
1178 "\t mmap align: 0x%" PRIx64 "\n"
1179 "\t mmap off : 0x%" PRIx64 "\n",
1181 reg->guest_phys_addr,
1182 reg->guest_user_addr,
1183 reg->host_user_addr,
1184 (uint64_t)(uintptr_t)mmap_addr,
1189 if (dev->postcopy_listening) {
1191 * We haven't a better way right now than sharing
1192 * DPDK's virtual address with Qemu, so that Qemu can
1193 * retrieve the region offset when handling userfaults.
1195 memory->regions[i].userspace_addr =
1196 reg->host_user_addr;
1199 if (dev->postcopy_listening) {
1200 /* Send the addresses back to qemu */
1202 send_vhost_reply(main_fd, msg);
1204 /* Wait for qemu to acknolwedge it's got the addresses
1205 * we've got to wait before we're allowed to generate faults.
1207 VhostUserMsg ack_msg;
1208 if (read_vhost_message(main_fd, &ack_msg) <= 0) {
1209 VHOST_LOG_CONFIG(ERR,
1210 "Failed to read qemu ack on postcopy set-mem-table\n");
1214 if (validate_msg_fds(&ack_msg, 0) != 0)
1217 if (ack_msg.request.master != VHOST_USER_SET_MEM_TABLE) {
1218 VHOST_LOG_CONFIG(ERR,
1219 "Bad qemu ack on postcopy set-mem-table (%d)\n",
1220 ack_msg.request.master);
1224 /* Now userfault register and we can use the memory */
1225 for (i = 0; i < memory->nregions; i++) {
1226 #ifdef RTE_LIBRTE_VHOST_POSTCOPY
1227 reg = &dev->mem->regions[i];
1228 struct uffdio_register reg_struct;
1231 * Let's register all the mmap'ed area to ensure
1232 * alignment on page boundary.
1234 reg_struct.range.start =
1235 (uint64_t)(uintptr_t)reg->mmap_addr;
1236 reg_struct.range.len = reg->mmap_size;
1237 reg_struct.mode = UFFDIO_REGISTER_MODE_MISSING;
1239 if (ioctl(dev->postcopy_ufd, UFFDIO_REGISTER,
1241 VHOST_LOG_CONFIG(ERR,
1242 "Failed to register ufd for region %d: (ufd = %d) %s\n",
1243 i, dev->postcopy_ufd,
1247 VHOST_LOG_CONFIG(INFO,
1248 "\t userfaultfd registered for range : "
1249 "%" PRIx64 " - %" PRIx64 "\n",
1250 (uint64_t)reg_struct.range.start,
1251 (uint64_t)reg_struct.range.start +
1252 (uint64_t)reg_struct.range.len - 1);
1259 for (i = 0; i < dev->nr_vring; i++) {
1260 struct vhost_virtqueue *vq = dev->virtqueue[i];
1262 if (vq->desc || vq->avail || vq->used) {
1264 * If the memory table got updated, the ring addresses
1265 * need to be translated again as virtual addresses have
1268 vring_invalidate(dev, vq);
1270 dev = translate_ring_addresses(dev, i);
1280 dump_guest_pages(dev);
1282 return RTE_VHOST_MSG_RESULT_OK;
1285 free_mem_region(dev);
1288 return RTE_VHOST_MSG_RESULT_ERR;
1292 vq_is_ready(struct virtio_net *dev, struct vhost_virtqueue *vq)
1299 if (vq_is_packed(dev))
1300 rings_ok = vq->desc_packed && vq->driver_event &&
1303 rings_ok = vq->desc && vq->avail && vq->used;
1306 vq->kickfd != VIRTIO_UNINITIALIZED_EVENTFD &&
1307 vq->callfd != VIRTIO_UNINITIALIZED_EVENTFD;
1311 virtio_is_ready(struct virtio_net *dev)
1313 struct vhost_virtqueue *vq;
1316 if (dev->nr_vring == 0)
1319 for (i = 0; i < dev->nr_vring; i++) {
1320 vq = dev->virtqueue[i];
1322 if (!vq_is_ready(dev, vq))
1326 VHOST_LOG_CONFIG(INFO,
1327 "virtio is now ready for processing.\n");
1332 inflight_mem_alloc(const char *name, size_t size, int *fd)
1336 char fname[20] = "/tmp/memfd-XXXXXX";
1339 #ifdef MEMFD_SUPPORTED
1340 mfd = memfd_create(name, MFD_CLOEXEC);
1345 mfd = mkstemp(fname);
1347 VHOST_LOG_CONFIG(ERR,
1348 "failed to get inflight buffer fd\n");
1355 if (ftruncate(mfd, size) == -1) {
1356 VHOST_LOG_CONFIG(ERR,
1357 "failed to alloc inflight buffer\n");
1362 ptr = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, mfd, 0);
1363 if (ptr == MAP_FAILED) {
1364 VHOST_LOG_CONFIG(ERR,
1365 "failed to mmap inflight buffer\n");
1375 get_pervq_shm_size_split(uint16_t queue_size)
1377 return RTE_ALIGN_MUL_CEIL(sizeof(struct rte_vhost_inflight_desc_split) *
1378 queue_size + sizeof(uint64_t) +
1379 sizeof(uint16_t) * 4, INFLIGHT_ALIGNMENT);
1383 get_pervq_shm_size_packed(uint16_t queue_size)
1385 return RTE_ALIGN_MUL_CEIL(sizeof(struct rte_vhost_inflight_desc_packed)
1386 * queue_size + sizeof(uint64_t) +
1387 sizeof(uint16_t) * 6 + sizeof(uint8_t) * 9,
1388 INFLIGHT_ALIGNMENT);
1392 vhost_user_get_inflight_fd(struct virtio_net **pdev,
1394 int main_fd __rte_unused)
1396 struct rte_vhost_inflight_info_packed *inflight_packed;
1397 uint64_t pervq_inflight_size, mmap_size;
1398 uint16_t num_queues, queue_size;
1399 struct virtio_net *dev = *pdev;
1403 if (msg->size != sizeof(msg->payload.inflight)) {
1404 VHOST_LOG_CONFIG(ERR,
1405 "invalid get_inflight_fd message size is %d\n",
1407 return RTE_VHOST_MSG_RESULT_ERR;
1410 if (dev->inflight_info == NULL) {
1411 dev->inflight_info = calloc(1,
1412 sizeof(struct inflight_mem_info));
1413 if (!dev->inflight_info) {
1414 VHOST_LOG_CONFIG(ERR,
1415 "failed to alloc dev inflight area\n");
1416 return RTE_VHOST_MSG_RESULT_ERR;
1418 dev->inflight_info->fd = -1;
1421 num_queues = msg->payload.inflight.num_queues;
1422 queue_size = msg->payload.inflight.queue_size;
1424 VHOST_LOG_CONFIG(INFO, "get_inflight_fd num_queues: %u\n",
1425 msg->payload.inflight.num_queues);
1426 VHOST_LOG_CONFIG(INFO, "get_inflight_fd queue_size: %u\n",
1427 msg->payload.inflight.queue_size);
1429 if (vq_is_packed(dev))
1430 pervq_inflight_size = get_pervq_shm_size_packed(queue_size);
1432 pervq_inflight_size = get_pervq_shm_size_split(queue_size);
1434 mmap_size = num_queues * pervq_inflight_size;
1435 addr = inflight_mem_alloc("vhost-inflight", mmap_size, &fd);
1437 VHOST_LOG_CONFIG(ERR,
1438 "failed to alloc vhost inflight area\n");
1439 msg->payload.inflight.mmap_size = 0;
1440 return RTE_VHOST_MSG_RESULT_ERR;
1442 memset(addr, 0, mmap_size);
1444 if (dev->inflight_info->addr) {
1445 munmap(dev->inflight_info->addr, dev->inflight_info->size);
1446 dev->inflight_info->addr = NULL;
1449 if (dev->inflight_info->fd >= 0) {
1450 close(dev->inflight_info->fd);
1451 dev->inflight_info->fd = -1;
1454 dev->inflight_info->addr = addr;
1455 dev->inflight_info->size = msg->payload.inflight.mmap_size = mmap_size;
1456 dev->inflight_info->fd = msg->fds[0] = fd;
1457 msg->payload.inflight.mmap_offset = 0;
1460 if (vq_is_packed(dev)) {
1461 for (i = 0; i < num_queues; i++) {
1463 (struct rte_vhost_inflight_info_packed *)addr;
1464 inflight_packed->used_wrap_counter = 1;
1465 inflight_packed->old_used_wrap_counter = 1;
1466 for (j = 0; j < queue_size; j++)
1467 inflight_packed->desc[j].next = j + 1;
1468 addr = (void *)((char *)addr + pervq_inflight_size);
1472 VHOST_LOG_CONFIG(INFO,
1473 "send inflight mmap_size: %"PRIu64"\n",
1474 msg->payload.inflight.mmap_size);
1475 VHOST_LOG_CONFIG(INFO,
1476 "send inflight mmap_offset: %"PRIu64"\n",
1477 msg->payload.inflight.mmap_offset);
1478 VHOST_LOG_CONFIG(INFO,
1479 "send inflight fd: %d\n", msg->fds[0]);
1481 return RTE_VHOST_MSG_RESULT_REPLY;
1485 vhost_user_set_inflight_fd(struct virtio_net **pdev, VhostUserMsg *msg,
1486 int main_fd __rte_unused)
1488 uint64_t mmap_size, mmap_offset;
1489 uint16_t num_queues, queue_size;
1490 struct virtio_net *dev = *pdev;
1491 uint32_t pervq_inflight_size;
1492 struct vhost_virtqueue *vq;
1497 if (msg->size != sizeof(msg->payload.inflight) || fd < 0) {
1498 VHOST_LOG_CONFIG(ERR,
1499 "invalid set_inflight_fd message size is %d,fd is %d\n",
1501 return RTE_VHOST_MSG_RESULT_ERR;
1504 mmap_size = msg->payload.inflight.mmap_size;
1505 mmap_offset = msg->payload.inflight.mmap_offset;
1506 num_queues = msg->payload.inflight.num_queues;
1507 queue_size = msg->payload.inflight.queue_size;
1509 if (vq_is_packed(dev))
1510 pervq_inflight_size = get_pervq_shm_size_packed(queue_size);
1512 pervq_inflight_size = get_pervq_shm_size_split(queue_size);
1514 VHOST_LOG_CONFIG(INFO,
1515 "set_inflight_fd mmap_size: %"PRIu64"\n", mmap_size);
1516 VHOST_LOG_CONFIG(INFO,
1517 "set_inflight_fd mmap_offset: %"PRIu64"\n", mmap_offset);
1518 VHOST_LOG_CONFIG(INFO,
1519 "set_inflight_fd num_queues: %u\n", num_queues);
1520 VHOST_LOG_CONFIG(INFO,
1521 "set_inflight_fd queue_size: %u\n", queue_size);
1522 VHOST_LOG_CONFIG(INFO,
1523 "set_inflight_fd fd: %d\n", fd);
1524 VHOST_LOG_CONFIG(INFO,
1525 "set_inflight_fd pervq_inflight_size: %d\n",
1526 pervq_inflight_size);
1528 if (!dev->inflight_info) {
1529 dev->inflight_info = calloc(1,
1530 sizeof(struct inflight_mem_info));
1531 if (dev->inflight_info == NULL) {
1532 VHOST_LOG_CONFIG(ERR,
1533 "failed to alloc dev inflight area\n");
1534 return RTE_VHOST_MSG_RESULT_ERR;
1536 dev->inflight_info->fd = -1;
1539 if (dev->inflight_info->addr) {
1540 munmap(dev->inflight_info->addr, dev->inflight_info->size);
1541 dev->inflight_info->addr = NULL;
1544 addr = mmap(0, mmap_size, PROT_READ | PROT_WRITE, MAP_SHARED,
1546 if (addr == MAP_FAILED) {
1547 VHOST_LOG_CONFIG(ERR, "failed to mmap share memory.\n");
1548 return RTE_VHOST_MSG_RESULT_ERR;
1551 if (dev->inflight_info->fd >= 0) {
1552 close(dev->inflight_info->fd);
1553 dev->inflight_info->fd = -1;
1556 dev->inflight_info->fd = fd;
1557 dev->inflight_info->addr = addr;
1558 dev->inflight_info->size = mmap_size;
1560 for (i = 0; i < num_queues; i++) {
1561 vq = dev->virtqueue[i];
1562 if (vq_is_packed(dev)) {
1563 vq->inflight_packed = addr;
1564 vq->inflight_packed->desc_num = queue_size;
1566 vq->inflight_split = addr;
1567 vq->inflight_split->desc_num = queue_size;
1569 addr = (void *)((char *)addr + pervq_inflight_size);
1572 return RTE_VHOST_MSG_RESULT_OK;
1576 vhost_user_set_vring_call(struct virtio_net **pdev, struct VhostUserMsg *msg,
1577 int main_fd __rte_unused)
1579 struct virtio_net *dev = *pdev;
1580 struct vhost_vring_file file;
1581 struct vhost_virtqueue *vq;
1584 expected_fds = (msg->payload.u64 & VHOST_USER_VRING_NOFD_MASK) ? 0 : 1;
1585 if (validate_msg_fds(msg, expected_fds) != 0)
1586 return RTE_VHOST_MSG_RESULT_ERR;
1588 file.index = msg->payload.u64 & VHOST_USER_VRING_IDX_MASK;
1589 if (msg->payload.u64 & VHOST_USER_VRING_NOFD_MASK)
1590 file.fd = VIRTIO_INVALID_EVENTFD;
1592 file.fd = msg->fds[0];
1593 VHOST_LOG_CONFIG(INFO,
1594 "vring call idx:%d file:%d\n", file.index, file.fd);
1596 vq = dev->virtqueue[file.index];
1597 if (vq->callfd >= 0)
1600 vq->callfd = file.fd;
1602 return RTE_VHOST_MSG_RESULT_OK;
1605 static int vhost_user_set_vring_err(struct virtio_net **pdev __rte_unused,
1606 struct VhostUserMsg *msg,
1607 int main_fd __rte_unused)
1611 expected_fds = (msg->payload.u64 & VHOST_USER_VRING_NOFD_MASK) ? 0 : 1;
1612 if (validate_msg_fds(msg, expected_fds) != 0)
1613 return RTE_VHOST_MSG_RESULT_ERR;
1615 if (!(msg->payload.u64 & VHOST_USER_VRING_NOFD_MASK))
1617 VHOST_LOG_CONFIG(INFO, "not implemented\n");
1619 return RTE_VHOST_MSG_RESULT_OK;
1623 resubmit_desc_compare(const void *a, const void *b)
1625 const struct rte_vhost_resubmit_desc *desc0 = a;
1626 const struct rte_vhost_resubmit_desc *desc1 = b;
1628 if (desc1->counter > desc0->counter)
1635 vhost_check_queue_inflights_split(struct virtio_net *dev,
1636 struct vhost_virtqueue *vq)
1639 uint16_t resubmit_num = 0, last_io, num;
1640 struct vring_used *used = vq->used;
1641 struct rte_vhost_resubmit_info *resubmit;
1642 struct rte_vhost_inflight_info_split *inflight_split;
1644 if (!(dev->protocol_features &
1645 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD)))
1646 return RTE_VHOST_MSG_RESULT_OK;
1648 /* The frontend may still not support the inflight feature
1649 * although we negotiate the protocol feature.
1651 if ((!vq->inflight_split))
1652 return RTE_VHOST_MSG_RESULT_OK;
1654 if (!vq->inflight_split->version) {
1655 vq->inflight_split->version = INFLIGHT_VERSION;
1656 return RTE_VHOST_MSG_RESULT_OK;
1659 if (vq->resubmit_inflight)
1660 return RTE_VHOST_MSG_RESULT_OK;
1662 inflight_split = vq->inflight_split;
1663 vq->global_counter = 0;
1664 last_io = inflight_split->last_inflight_io;
1666 if (inflight_split->used_idx != used->idx) {
1667 inflight_split->desc[last_io].inflight = 0;
1669 inflight_split->used_idx = used->idx;
1672 for (i = 0; i < inflight_split->desc_num; i++) {
1673 if (inflight_split->desc[i].inflight == 1)
1677 vq->last_avail_idx += resubmit_num;
1680 resubmit = calloc(1, sizeof(struct rte_vhost_resubmit_info));
1682 VHOST_LOG_CONFIG(ERR,
1683 "failed to allocate memory for resubmit info.\n");
1684 return RTE_VHOST_MSG_RESULT_ERR;
1687 resubmit->resubmit_list = calloc(resubmit_num,
1688 sizeof(struct rte_vhost_resubmit_desc));
1689 if (!resubmit->resubmit_list) {
1690 VHOST_LOG_CONFIG(ERR,
1691 "failed to allocate memory for inflight desc.\n");
1693 return RTE_VHOST_MSG_RESULT_ERR;
1697 for (i = 0; i < vq->inflight_split->desc_num; i++) {
1698 if (vq->inflight_split->desc[i].inflight == 1) {
1699 resubmit->resubmit_list[num].index = i;
1700 resubmit->resubmit_list[num].counter =
1701 inflight_split->desc[i].counter;
1705 resubmit->resubmit_num = num;
1707 if (resubmit->resubmit_num > 1)
1708 qsort(resubmit->resubmit_list, resubmit->resubmit_num,
1709 sizeof(struct rte_vhost_resubmit_desc),
1710 resubmit_desc_compare);
1712 vq->global_counter = resubmit->resubmit_list[0].counter + 1;
1713 vq->resubmit_inflight = resubmit;
1716 return RTE_VHOST_MSG_RESULT_OK;
1720 vhost_check_queue_inflights_packed(struct virtio_net *dev,
1721 struct vhost_virtqueue *vq)
1724 uint16_t resubmit_num = 0, old_used_idx, num;
1725 struct rte_vhost_resubmit_info *resubmit;
1726 struct rte_vhost_inflight_info_packed *inflight_packed;
1728 if (!(dev->protocol_features &
1729 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD)))
1730 return RTE_VHOST_MSG_RESULT_OK;
1732 /* The frontend may still not support the inflight feature
1733 * although we negotiate the protocol feature.
1735 if ((!vq->inflight_packed))
1736 return RTE_VHOST_MSG_RESULT_OK;
1738 if (!vq->inflight_packed->version) {
1739 vq->inflight_packed->version = INFLIGHT_VERSION;
1740 return RTE_VHOST_MSG_RESULT_OK;
1743 if (vq->resubmit_inflight)
1744 return RTE_VHOST_MSG_RESULT_OK;
1746 inflight_packed = vq->inflight_packed;
1747 vq->global_counter = 0;
1748 old_used_idx = inflight_packed->old_used_idx;
1750 if (inflight_packed->used_idx != old_used_idx) {
1751 if (inflight_packed->desc[old_used_idx].inflight == 0) {
1752 inflight_packed->old_used_idx =
1753 inflight_packed->used_idx;
1754 inflight_packed->old_used_wrap_counter =
1755 inflight_packed->used_wrap_counter;
1756 inflight_packed->old_free_head =
1757 inflight_packed->free_head;
1759 inflight_packed->used_idx =
1760 inflight_packed->old_used_idx;
1761 inflight_packed->used_wrap_counter =
1762 inflight_packed->old_used_wrap_counter;
1763 inflight_packed->free_head =
1764 inflight_packed->old_free_head;
1768 for (i = 0; i < inflight_packed->desc_num; i++) {
1769 if (inflight_packed->desc[i].inflight == 1)
1774 resubmit = calloc(1, sizeof(struct rte_vhost_resubmit_info));
1775 if (resubmit == NULL) {
1776 VHOST_LOG_CONFIG(ERR,
1777 "failed to allocate memory for resubmit info.\n");
1778 return RTE_VHOST_MSG_RESULT_ERR;
1781 resubmit->resubmit_list = calloc(resubmit_num,
1782 sizeof(struct rte_vhost_resubmit_desc));
1783 if (resubmit->resubmit_list == NULL) {
1784 VHOST_LOG_CONFIG(ERR,
1785 "failed to allocate memory for resubmit desc.\n");
1787 return RTE_VHOST_MSG_RESULT_ERR;
1791 for (i = 0; i < inflight_packed->desc_num; i++) {
1792 if (vq->inflight_packed->desc[i].inflight == 1) {
1793 resubmit->resubmit_list[num].index = i;
1794 resubmit->resubmit_list[num].counter =
1795 inflight_packed->desc[i].counter;
1799 resubmit->resubmit_num = num;
1801 if (resubmit->resubmit_num > 1)
1802 qsort(resubmit->resubmit_list, resubmit->resubmit_num,
1803 sizeof(struct rte_vhost_resubmit_desc),
1804 resubmit_desc_compare);
1806 vq->global_counter = resubmit->resubmit_list[0].counter + 1;
1807 vq->resubmit_inflight = resubmit;
1810 return RTE_VHOST_MSG_RESULT_OK;
1814 vhost_user_set_vring_kick(struct virtio_net **pdev, struct VhostUserMsg *msg,
1815 int main_fd __rte_unused)
1817 struct virtio_net *dev = *pdev;
1818 struct vhost_vring_file file;
1819 struct vhost_virtqueue *vq;
1822 expected_fds = (msg->payload.u64 & VHOST_USER_VRING_NOFD_MASK) ? 0 : 1;
1823 if (validate_msg_fds(msg, expected_fds) != 0)
1824 return RTE_VHOST_MSG_RESULT_ERR;
1826 file.index = msg->payload.u64 & VHOST_USER_VRING_IDX_MASK;
1827 if (msg->payload.u64 & VHOST_USER_VRING_NOFD_MASK)
1828 file.fd = VIRTIO_INVALID_EVENTFD;
1830 file.fd = msg->fds[0];
1831 VHOST_LOG_CONFIG(INFO,
1832 "vring kick idx:%d file:%d\n", file.index, file.fd);
1834 /* Interpret ring addresses only when ring is started. */
1835 dev = translate_ring_addresses(dev, file.index);
1837 return RTE_VHOST_MSG_RESULT_ERR;
1841 vq = dev->virtqueue[file.index];
1844 * When VHOST_USER_F_PROTOCOL_FEATURES is not negotiated,
1845 * the ring starts already enabled. Otherwise, it is enabled via
1846 * the SET_VRING_ENABLE message.
1848 if (!(dev->features & (1ULL << VHOST_USER_F_PROTOCOL_FEATURES))) {
1850 if (dev->notify_ops->vring_state_changed)
1851 dev->notify_ops->vring_state_changed(
1852 dev->vid, file.index, 1);
1855 if (vq->kickfd >= 0)
1857 vq->kickfd = file.fd;
1859 if (vq_is_packed(dev)) {
1860 if (vhost_check_queue_inflights_packed(dev, vq)) {
1861 VHOST_LOG_CONFIG(ERR,
1862 "failed to inflights for vq: %d\n", file.index);
1863 return RTE_VHOST_MSG_RESULT_ERR;
1866 if (vhost_check_queue_inflights_split(dev, vq)) {
1867 VHOST_LOG_CONFIG(ERR,
1868 "failed to inflights for vq: %d\n", file.index);
1869 return RTE_VHOST_MSG_RESULT_ERR;
1873 return RTE_VHOST_MSG_RESULT_OK;
1877 free_zmbufs(struct vhost_virtqueue *vq)
1879 drain_zmbuf_list(vq);
1881 rte_free(vq->zmbufs);
1885 * when virtio is stopped, qemu will send us the GET_VRING_BASE message.
1888 vhost_user_get_vring_base(struct virtio_net **pdev,
1889 struct VhostUserMsg *msg,
1890 int main_fd __rte_unused)
1892 struct virtio_net *dev = *pdev;
1893 struct vhost_virtqueue *vq = dev->virtqueue[msg->payload.state.index];
1896 if (validate_msg_fds(msg, 0) != 0)
1897 return RTE_VHOST_MSG_RESULT_ERR;
1899 /* We have to stop the queue (virtio) if it is running. */
1900 vhost_destroy_device_notify(dev);
1902 dev->flags &= ~VIRTIO_DEV_READY;
1903 dev->flags &= ~VIRTIO_DEV_VDPA_CONFIGURED;
1905 /* Here we are safe to get the indexes */
1906 if (vq_is_packed(dev)) {
1908 * Bit[0:14]: avail index
1909 * Bit[15]: avail wrap counter
1911 val = vq->last_avail_idx & 0x7fff;
1912 val |= vq->avail_wrap_counter << 15;
1913 msg->payload.state.num = val;
1915 msg->payload.state.num = vq->last_avail_idx;
1918 VHOST_LOG_CONFIG(INFO,
1919 "vring base idx:%d file:%d\n", msg->payload.state.index,
1920 msg->payload.state.num);
1922 * Based on current qemu vhost-user implementation, this message is
1923 * sent and only sent in vhost_vring_stop.
1924 * TODO: cleanup the vring, it isn't usable since here.
1926 if (vq->kickfd >= 0)
1929 vq->kickfd = VIRTIO_UNINITIALIZED_EVENTFD;
1931 if (vq->callfd >= 0)
1934 vq->callfd = VIRTIO_UNINITIALIZED_EVENTFD;
1936 vq->signalled_used_valid = false;
1938 if (dev->dequeue_zero_copy)
1940 if (vq_is_packed(dev)) {
1941 rte_free(vq->shadow_used_packed);
1942 vq->shadow_used_packed = NULL;
1944 rte_free(vq->shadow_used_split);
1945 vq->shadow_used_split = NULL;
1948 rte_free(vq->batch_copy_elems);
1949 vq->batch_copy_elems = NULL;
1951 msg->size = sizeof(msg->payload.state);
1954 vring_invalidate(dev, vq);
1956 return RTE_VHOST_MSG_RESULT_REPLY;
1960 * when virtio queues are ready to work, qemu will send us to
1961 * enable the virtio queue pair.
1964 vhost_user_set_vring_enable(struct virtio_net **pdev,
1965 struct VhostUserMsg *msg,
1966 int main_fd __rte_unused)
1968 struct virtio_net *dev = *pdev;
1969 int enable = (int)msg->payload.state.num;
1970 int index = (int)msg->payload.state.index;
1971 struct rte_vdpa_device *vdpa_dev;
1973 if (validate_msg_fds(msg, 0) != 0)
1974 return RTE_VHOST_MSG_RESULT_ERR;
1976 VHOST_LOG_CONFIG(INFO,
1977 "set queue enable: %d to qp idx: %d\n",
1980 vdpa_dev = dev->vdpa_dev;
1981 if (vdpa_dev && vdpa_dev->ops->set_vring_state)
1982 vdpa_dev->ops->set_vring_state(dev->vid, index, enable);
1984 if (dev->notify_ops->vring_state_changed)
1985 dev->notify_ops->vring_state_changed(dev->vid,
1988 /* On disable, rings have to be stopped being processed. */
1989 if (!enable && dev->dequeue_zero_copy)
1990 drain_zmbuf_list(dev->virtqueue[index]);
1992 dev->virtqueue[index]->enabled = enable;
1994 return RTE_VHOST_MSG_RESULT_OK;
1998 vhost_user_get_protocol_features(struct virtio_net **pdev,
1999 struct VhostUserMsg *msg,
2000 int main_fd __rte_unused)
2002 struct virtio_net *dev = *pdev;
2003 uint64_t features, protocol_features;
2005 if (validate_msg_fds(msg, 0) != 0)
2006 return RTE_VHOST_MSG_RESULT_ERR;
2008 rte_vhost_driver_get_features(dev->ifname, &features);
2009 rte_vhost_driver_get_protocol_features(dev->ifname, &protocol_features);
2011 msg->payload.u64 = protocol_features;
2012 msg->size = sizeof(msg->payload.u64);
2015 return RTE_VHOST_MSG_RESULT_REPLY;
2019 vhost_user_set_protocol_features(struct virtio_net **pdev,
2020 struct VhostUserMsg *msg,
2021 int main_fd __rte_unused)
2023 struct virtio_net *dev = *pdev;
2024 uint64_t protocol_features = msg->payload.u64;
2025 uint64_t slave_protocol_features = 0;
2027 if (validate_msg_fds(msg, 0) != 0)
2028 return RTE_VHOST_MSG_RESULT_ERR;
2030 rte_vhost_driver_get_protocol_features(dev->ifname,
2031 &slave_protocol_features);
2032 if (protocol_features & ~slave_protocol_features) {
2033 VHOST_LOG_CONFIG(ERR,
2034 "(%d) received invalid protocol features.\n",
2036 return RTE_VHOST_MSG_RESULT_ERR;
2039 dev->protocol_features = protocol_features;
2040 VHOST_LOG_CONFIG(INFO,
2041 "negotiated Vhost-user protocol features: 0x%" PRIx64 "\n",
2042 dev->protocol_features);
2044 return RTE_VHOST_MSG_RESULT_OK;
2048 vhost_user_set_log_base(struct virtio_net **pdev, struct VhostUserMsg *msg,
2049 int main_fd __rte_unused)
2051 struct virtio_net *dev = *pdev;
2052 int fd = msg->fds[0];
2056 if (validate_msg_fds(msg, 1) != 0)
2057 return RTE_VHOST_MSG_RESULT_ERR;
2060 VHOST_LOG_CONFIG(ERR, "invalid log fd: %d\n", fd);
2061 return RTE_VHOST_MSG_RESULT_ERR;
2064 if (msg->size != sizeof(VhostUserLog)) {
2065 VHOST_LOG_CONFIG(ERR,
2066 "invalid log base msg size: %"PRId32" != %d\n",
2067 msg->size, (int)sizeof(VhostUserLog));
2068 return RTE_VHOST_MSG_RESULT_ERR;
2071 size = msg->payload.log.mmap_size;
2072 off = msg->payload.log.mmap_offset;
2074 /* Check for mmap size and offset overflow. */
2076 VHOST_LOG_CONFIG(ERR,
2077 "log offset %#"PRIx64" and log size %#"PRIx64" overflow\n",
2079 return RTE_VHOST_MSG_RESULT_ERR;
2082 VHOST_LOG_CONFIG(INFO,
2083 "log mmap size: %"PRId64", offset: %"PRId64"\n",
2087 * mmap from 0 to workaround a hugepage mmap bug: mmap will
2088 * fail when offset is not page size aligned.
2090 addr = mmap(0, size + off, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
2092 if (addr == MAP_FAILED) {
2093 VHOST_LOG_CONFIG(ERR, "mmap log base failed!\n");
2094 return RTE_VHOST_MSG_RESULT_ERR;
2098 * Free previously mapped log memory on occasionally
2099 * multiple VHOST_USER_SET_LOG_BASE.
2101 if (dev->log_addr) {
2102 munmap((void *)(uintptr_t)dev->log_addr, dev->log_size);
2104 dev->log_addr = (uint64_t)(uintptr_t)addr;
2105 dev->log_base = dev->log_addr + off;
2106 dev->log_size = size;
2109 * The spec is not clear about it (yet), but QEMU doesn't expect
2110 * any payload in the reply.
2115 return RTE_VHOST_MSG_RESULT_REPLY;
2118 static int vhost_user_set_log_fd(struct virtio_net **pdev __rte_unused,
2119 struct VhostUserMsg *msg,
2120 int main_fd __rte_unused)
2122 if (validate_msg_fds(msg, 1) != 0)
2123 return RTE_VHOST_MSG_RESULT_ERR;
2126 VHOST_LOG_CONFIG(INFO, "not implemented.\n");
2128 return RTE_VHOST_MSG_RESULT_OK;
2132 * An rarp packet is constructed and broadcasted to notify switches about
2133 * the new location of the migrated VM, so that packets from outside will
2134 * not be lost after migration.
2136 * However, we don't actually "send" a rarp packet here, instead, we set
2137 * a flag 'broadcast_rarp' to let rte_vhost_dequeue_burst() inject it.
2140 vhost_user_send_rarp(struct virtio_net **pdev, struct VhostUserMsg *msg,
2141 int main_fd __rte_unused)
2143 struct virtio_net *dev = *pdev;
2144 uint8_t *mac = (uint8_t *)&msg->payload.u64;
2145 struct rte_vdpa_device *vdpa_dev;
2147 if (validate_msg_fds(msg, 0) != 0)
2148 return RTE_VHOST_MSG_RESULT_ERR;
2150 VHOST_LOG_CONFIG(DEBUG,
2151 ":: mac: %02x:%02x:%02x:%02x:%02x:%02x\n",
2152 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
2153 memcpy(dev->mac.addr_bytes, mac, 6);
2156 * Set the flag to inject a RARP broadcast packet at
2157 * rte_vhost_dequeue_burst().
2159 * __ATOMIC_RELEASE ordering is for making sure the mac is
2160 * copied before the flag is set.
2162 __atomic_store_n(&dev->broadcast_rarp, 1, __ATOMIC_RELEASE);
2163 vdpa_dev = dev->vdpa_dev;
2164 if (vdpa_dev && vdpa_dev->ops->migration_done)
2165 vdpa_dev->ops->migration_done(dev->vid);
2167 return RTE_VHOST_MSG_RESULT_OK;
2171 vhost_user_net_set_mtu(struct virtio_net **pdev, struct VhostUserMsg *msg,
2172 int main_fd __rte_unused)
2174 struct virtio_net *dev = *pdev;
2176 if (validate_msg_fds(msg, 0) != 0)
2177 return RTE_VHOST_MSG_RESULT_ERR;
2179 if (msg->payload.u64 < VIRTIO_MIN_MTU ||
2180 msg->payload.u64 > VIRTIO_MAX_MTU) {
2181 VHOST_LOG_CONFIG(ERR, "Invalid MTU size (%"PRIu64")\n",
2184 return RTE_VHOST_MSG_RESULT_ERR;
2187 dev->mtu = msg->payload.u64;
2189 return RTE_VHOST_MSG_RESULT_OK;
2193 vhost_user_set_req_fd(struct virtio_net **pdev, struct VhostUserMsg *msg,
2194 int main_fd __rte_unused)
2196 struct virtio_net *dev = *pdev;
2197 int fd = msg->fds[0];
2199 if (validate_msg_fds(msg, 1) != 0)
2200 return RTE_VHOST_MSG_RESULT_ERR;
2203 VHOST_LOG_CONFIG(ERR,
2204 "Invalid file descriptor for slave channel (%d)\n",
2206 return RTE_VHOST_MSG_RESULT_ERR;
2209 if (dev->slave_req_fd >= 0)
2210 close(dev->slave_req_fd);
2212 dev->slave_req_fd = fd;
2214 return RTE_VHOST_MSG_RESULT_OK;
2218 is_vring_iotlb_split(struct vhost_virtqueue *vq, struct vhost_iotlb_msg *imsg)
2220 struct vhost_vring_addr *ra;
2221 uint64_t start, end, len;
2224 end = start + imsg->size;
2226 ra = &vq->ring_addrs;
2227 len = sizeof(struct vring_desc) * vq->size;
2228 if (ra->desc_user_addr < end && (ra->desc_user_addr + len) > start)
2231 len = sizeof(struct vring_avail) + sizeof(uint16_t) * vq->size;
2232 if (ra->avail_user_addr < end && (ra->avail_user_addr + len) > start)
2235 len = sizeof(struct vring_used) +
2236 sizeof(struct vring_used_elem) * vq->size;
2237 if (ra->used_user_addr < end && (ra->used_user_addr + len) > start)
2240 if (ra->flags & (1 << VHOST_VRING_F_LOG)) {
2241 len = sizeof(uint64_t);
2242 if (ra->log_guest_addr < end &&
2243 (ra->log_guest_addr + len) > start)
2251 is_vring_iotlb_packed(struct vhost_virtqueue *vq, struct vhost_iotlb_msg *imsg)
2253 struct vhost_vring_addr *ra;
2254 uint64_t start, end, len;
2257 end = start + imsg->size;
2259 ra = &vq->ring_addrs;
2260 len = sizeof(struct vring_packed_desc) * vq->size;
2261 if (ra->desc_user_addr < end && (ra->desc_user_addr + len) > start)
2264 len = sizeof(struct vring_packed_desc_event);
2265 if (ra->avail_user_addr < end && (ra->avail_user_addr + len) > start)
2268 len = sizeof(struct vring_packed_desc_event);
2269 if (ra->used_user_addr < end && (ra->used_user_addr + len) > start)
2272 if (ra->flags & (1 << VHOST_VRING_F_LOG)) {
2273 len = sizeof(uint64_t);
2274 if (ra->log_guest_addr < end &&
2275 (ra->log_guest_addr + len) > start)
2282 static int is_vring_iotlb(struct virtio_net *dev,
2283 struct vhost_virtqueue *vq,
2284 struct vhost_iotlb_msg *imsg)
2286 if (vq_is_packed(dev))
2287 return is_vring_iotlb_packed(vq, imsg);
2289 return is_vring_iotlb_split(vq, imsg);
2293 vhost_user_iotlb_msg(struct virtio_net **pdev, struct VhostUserMsg *msg,
2294 int main_fd __rte_unused)
2296 struct virtio_net *dev = *pdev;
2297 struct vhost_iotlb_msg *imsg = &msg->payload.iotlb;
2301 if (validate_msg_fds(msg, 0) != 0)
2302 return RTE_VHOST_MSG_RESULT_ERR;
2304 switch (imsg->type) {
2305 case VHOST_IOTLB_UPDATE:
2307 vva = qva_to_vva(dev, imsg->uaddr, &len);
2309 return RTE_VHOST_MSG_RESULT_ERR;
2311 for (i = 0; i < dev->nr_vring; i++) {
2312 struct vhost_virtqueue *vq = dev->virtqueue[i];
2314 vhost_user_iotlb_cache_insert(vq, imsg->iova, vva,
2317 if (is_vring_iotlb(dev, vq, imsg))
2318 *pdev = dev = translate_ring_addresses(dev, i);
2321 case VHOST_IOTLB_INVALIDATE:
2322 for (i = 0; i < dev->nr_vring; i++) {
2323 struct vhost_virtqueue *vq = dev->virtqueue[i];
2325 vhost_user_iotlb_cache_remove(vq, imsg->iova,
2328 if (is_vring_iotlb(dev, vq, imsg))
2329 vring_invalidate(dev, vq);
2333 VHOST_LOG_CONFIG(ERR, "Invalid IOTLB message type (%d)\n",
2335 return RTE_VHOST_MSG_RESULT_ERR;
2338 return RTE_VHOST_MSG_RESULT_OK;
2342 vhost_user_set_postcopy_advise(struct virtio_net **pdev,
2343 struct VhostUserMsg *msg,
2344 int main_fd __rte_unused)
2346 struct virtio_net *dev = *pdev;
2347 #ifdef RTE_LIBRTE_VHOST_POSTCOPY
2348 struct uffdio_api api_struct;
2350 if (validate_msg_fds(msg, 0) != 0)
2351 return RTE_VHOST_MSG_RESULT_ERR;
2353 dev->postcopy_ufd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK);
2355 if (dev->postcopy_ufd == -1) {
2356 VHOST_LOG_CONFIG(ERR, "Userfaultfd not available: %s\n",
2358 return RTE_VHOST_MSG_RESULT_ERR;
2360 api_struct.api = UFFD_API;
2361 api_struct.features = 0;
2362 if (ioctl(dev->postcopy_ufd, UFFDIO_API, &api_struct)) {
2363 VHOST_LOG_CONFIG(ERR, "UFFDIO_API ioctl failure: %s\n",
2365 close(dev->postcopy_ufd);
2366 dev->postcopy_ufd = -1;
2367 return RTE_VHOST_MSG_RESULT_ERR;
2369 msg->fds[0] = dev->postcopy_ufd;
2372 return RTE_VHOST_MSG_RESULT_REPLY;
2374 dev->postcopy_ufd = -1;
2377 return RTE_VHOST_MSG_RESULT_ERR;
2382 vhost_user_set_postcopy_listen(struct virtio_net **pdev,
2383 struct VhostUserMsg *msg __rte_unused,
2384 int main_fd __rte_unused)
2386 struct virtio_net *dev = *pdev;
2388 if (validate_msg_fds(msg, 0) != 0)
2389 return RTE_VHOST_MSG_RESULT_ERR;
2391 if (dev->mem && dev->mem->nregions) {
2392 VHOST_LOG_CONFIG(ERR,
2393 "Regions already registered at postcopy-listen\n");
2394 return RTE_VHOST_MSG_RESULT_ERR;
2396 dev->postcopy_listening = 1;
2398 return RTE_VHOST_MSG_RESULT_OK;
2402 vhost_user_postcopy_end(struct virtio_net **pdev, struct VhostUserMsg *msg,
2403 int main_fd __rte_unused)
2405 struct virtio_net *dev = *pdev;
2407 if (validate_msg_fds(msg, 0) != 0)
2408 return RTE_VHOST_MSG_RESULT_ERR;
2410 dev->postcopy_listening = 0;
2411 if (dev->postcopy_ufd >= 0) {
2412 close(dev->postcopy_ufd);
2413 dev->postcopy_ufd = -1;
2416 msg->payload.u64 = 0;
2417 msg->size = sizeof(msg->payload.u64);
2420 return RTE_VHOST_MSG_RESULT_REPLY;
2423 typedef int (*vhost_message_handler_t)(struct virtio_net **pdev,
2424 struct VhostUserMsg *msg,
2426 static vhost_message_handler_t vhost_message_handlers[VHOST_USER_MAX] = {
2427 [VHOST_USER_NONE] = NULL,
2428 [VHOST_USER_GET_FEATURES] = vhost_user_get_features,
2429 [VHOST_USER_SET_FEATURES] = vhost_user_set_features,
2430 [VHOST_USER_SET_OWNER] = vhost_user_set_owner,
2431 [VHOST_USER_RESET_OWNER] = vhost_user_reset_owner,
2432 [VHOST_USER_SET_MEM_TABLE] = vhost_user_set_mem_table,
2433 [VHOST_USER_SET_LOG_BASE] = vhost_user_set_log_base,
2434 [VHOST_USER_SET_LOG_FD] = vhost_user_set_log_fd,
2435 [VHOST_USER_SET_VRING_NUM] = vhost_user_set_vring_num,
2436 [VHOST_USER_SET_VRING_ADDR] = vhost_user_set_vring_addr,
2437 [VHOST_USER_SET_VRING_BASE] = vhost_user_set_vring_base,
2438 [VHOST_USER_GET_VRING_BASE] = vhost_user_get_vring_base,
2439 [VHOST_USER_SET_VRING_KICK] = vhost_user_set_vring_kick,
2440 [VHOST_USER_SET_VRING_CALL] = vhost_user_set_vring_call,
2441 [VHOST_USER_SET_VRING_ERR] = vhost_user_set_vring_err,
2442 [VHOST_USER_GET_PROTOCOL_FEATURES] = vhost_user_get_protocol_features,
2443 [VHOST_USER_SET_PROTOCOL_FEATURES] = vhost_user_set_protocol_features,
2444 [VHOST_USER_GET_QUEUE_NUM] = vhost_user_get_queue_num,
2445 [VHOST_USER_SET_VRING_ENABLE] = vhost_user_set_vring_enable,
2446 [VHOST_USER_SEND_RARP] = vhost_user_send_rarp,
2447 [VHOST_USER_NET_SET_MTU] = vhost_user_net_set_mtu,
2448 [VHOST_USER_SET_SLAVE_REQ_FD] = vhost_user_set_req_fd,
2449 [VHOST_USER_IOTLB_MSG] = vhost_user_iotlb_msg,
2450 [VHOST_USER_POSTCOPY_ADVISE] = vhost_user_set_postcopy_advise,
2451 [VHOST_USER_POSTCOPY_LISTEN] = vhost_user_set_postcopy_listen,
2452 [VHOST_USER_POSTCOPY_END] = vhost_user_postcopy_end,
2453 [VHOST_USER_GET_INFLIGHT_FD] = vhost_user_get_inflight_fd,
2454 [VHOST_USER_SET_INFLIGHT_FD] = vhost_user_set_inflight_fd,
2457 /* return bytes# of read on success or negative val on failure. */
2459 read_vhost_message(int sockfd, struct VhostUserMsg *msg)
2463 ret = read_fd_message(sockfd, (char *)msg, VHOST_USER_HDR_SIZE,
2464 msg->fds, VHOST_MEMORY_MAX_NREGIONS, &msg->fd_num);
2467 } else if (ret != VHOST_USER_HDR_SIZE) {
2468 VHOST_LOG_CONFIG(ERR, "Unexpected header size read\n");
2474 if (msg->size > sizeof(msg->payload)) {
2475 VHOST_LOG_CONFIG(ERR,
2476 "invalid msg size: %d\n", msg->size);
2479 ret = read(sockfd, &msg->payload, msg->size);
2482 if (ret != (int)msg->size) {
2483 VHOST_LOG_CONFIG(ERR,
2484 "read control message failed\n");
2493 send_vhost_message(int sockfd, struct VhostUserMsg *msg)
2498 return send_fd_message(sockfd, (char *)msg,
2499 VHOST_USER_HDR_SIZE + msg->size, msg->fds, msg->fd_num);
2503 send_vhost_reply(int sockfd, struct VhostUserMsg *msg)
2508 msg->flags &= ~VHOST_USER_VERSION_MASK;
2509 msg->flags &= ~VHOST_USER_NEED_REPLY;
2510 msg->flags |= VHOST_USER_VERSION;
2511 msg->flags |= VHOST_USER_REPLY_MASK;
2513 return send_vhost_message(sockfd, msg);
2517 send_vhost_slave_message(struct virtio_net *dev, struct VhostUserMsg *msg)
2521 if (msg->flags & VHOST_USER_NEED_REPLY)
2522 rte_spinlock_lock(&dev->slave_req_lock);
2524 ret = send_vhost_message(dev->slave_req_fd, msg);
2525 if (ret < 0 && (msg->flags & VHOST_USER_NEED_REPLY))
2526 rte_spinlock_unlock(&dev->slave_req_lock);
2532 * Allocate a queue pair if it hasn't been allocated yet
2535 vhost_user_check_and_alloc_queue_pair(struct virtio_net *dev,
2536 struct VhostUserMsg *msg)
2540 switch (msg->request.master) {
2541 case VHOST_USER_SET_VRING_KICK:
2542 case VHOST_USER_SET_VRING_CALL:
2543 case VHOST_USER_SET_VRING_ERR:
2544 vring_idx = msg->payload.u64 & VHOST_USER_VRING_IDX_MASK;
2546 case VHOST_USER_SET_VRING_NUM:
2547 case VHOST_USER_SET_VRING_BASE:
2548 case VHOST_USER_SET_VRING_ENABLE:
2549 vring_idx = msg->payload.state.index;
2551 case VHOST_USER_SET_VRING_ADDR:
2552 vring_idx = msg->payload.addr.index;
2558 if (vring_idx >= VHOST_MAX_VRING) {
2559 VHOST_LOG_CONFIG(ERR,
2560 "invalid vring index: %u\n", vring_idx);
2564 if (dev->virtqueue[vring_idx])
2567 return alloc_vring_queue(dev, vring_idx);
2571 vhost_user_lock_all_queue_pairs(struct virtio_net *dev)
2574 unsigned int vq_num = 0;
2576 while (vq_num < dev->nr_vring) {
2577 struct vhost_virtqueue *vq = dev->virtqueue[i];
2580 rte_spinlock_lock(&vq->access_lock);
2588 vhost_user_unlock_all_queue_pairs(struct virtio_net *dev)
2591 unsigned int vq_num = 0;
2593 while (vq_num < dev->nr_vring) {
2594 struct vhost_virtqueue *vq = dev->virtqueue[i];
2597 rte_spinlock_unlock(&vq->access_lock);
2605 vhost_user_msg_handler(int vid, int fd)
2607 struct virtio_net *dev;
2608 struct VhostUserMsg msg;
2609 struct rte_vdpa_device *vdpa_dev;
2611 int unlock_required = 0;
2615 dev = get_device(vid);
2619 if (!dev->notify_ops) {
2620 dev->notify_ops = vhost_driver_callback_get(dev->ifname);
2621 if (!dev->notify_ops) {
2622 VHOST_LOG_CONFIG(ERR,
2623 "failed to get callback ops for driver %s\n",
2629 ret = read_vhost_message(fd, &msg);
2632 VHOST_LOG_CONFIG(ERR,
2633 "vhost read message failed\n");
2635 VHOST_LOG_CONFIG(INFO,
2636 "vhost peer closed\n");
2642 request = msg.request.master;
2643 if (request > VHOST_USER_NONE && request < VHOST_USER_MAX &&
2644 vhost_message_str[request]) {
2645 if (request != VHOST_USER_IOTLB_MSG)
2646 VHOST_LOG_CONFIG(INFO, "read message %s\n",
2647 vhost_message_str[request]);
2649 VHOST_LOG_CONFIG(DEBUG, "read message %s\n",
2650 vhost_message_str[request]);
2652 VHOST_LOG_CONFIG(DEBUG, "External request %d\n", request);
2655 ret = vhost_user_check_and_alloc_queue_pair(dev, &msg);
2657 VHOST_LOG_CONFIG(ERR,
2658 "failed to alloc queue\n");
2663 * Note: we don't lock all queues on VHOST_USER_GET_VRING_BASE
2664 * and VHOST_USER_RESET_OWNER, since it is sent when virtio stops
2665 * and device is destroyed. destroy_device waits for queues to be
2666 * inactive, so it is safe. Otherwise taking the access_lock
2667 * would cause a dead lock.
2670 case VHOST_USER_SET_FEATURES:
2671 case VHOST_USER_SET_PROTOCOL_FEATURES:
2672 case VHOST_USER_SET_OWNER:
2673 case VHOST_USER_SET_MEM_TABLE:
2674 case VHOST_USER_SET_LOG_BASE:
2675 case VHOST_USER_SET_LOG_FD:
2676 case VHOST_USER_SET_VRING_NUM:
2677 case VHOST_USER_SET_VRING_ADDR:
2678 case VHOST_USER_SET_VRING_BASE:
2679 case VHOST_USER_SET_VRING_KICK:
2680 case VHOST_USER_SET_VRING_CALL:
2681 case VHOST_USER_SET_VRING_ERR:
2682 case VHOST_USER_SET_VRING_ENABLE:
2683 case VHOST_USER_SEND_RARP:
2684 case VHOST_USER_NET_SET_MTU:
2685 case VHOST_USER_SET_SLAVE_REQ_FD:
2686 vhost_user_lock_all_queue_pairs(dev);
2687 unlock_required = 1;
2695 if (dev->extern_ops.pre_msg_handle) {
2696 ret = (*dev->extern_ops.pre_msg_handle)(dev->vid,
2699 case RTE_VHOST_MSG_RESULT_REPLY:
2700 send_vhost_reply(fd, &msg);
2702 case RTE_VHOST_MSG_RESULT_ERR:
2703 case RTE_VHOST_MSG_RESULT_OK:
2705 goto skip_to_post_handle;
2706 case RTE_VHOST_MSG_RESULT_NOT_HANDLED:
2712 if (request > VHOST_USER_NONE && request < VHOST_USER_MAX) {
2713 if (!vhost_message_handlers[request])
2714 goto skip_to_post_handle;
2715 ret = vhost_message_handlers[request](&dev, &msg, fd);
2718 case RTE_VHOST_MSG_RESULT_ERR:
2719 VHOST_LOG_CONFIG(ERR,
2720 "Processing %s failed.\n",
2721 vhost_message_str[request]);
2724 case RTE_VHOST_MSG_RESULT_OK:
2725 VHOST_LOG_CONFIG(DEBUG,
2726 "Processing %s succeeded.\n",
2727 vhost_message_str[request]);
2730 case RTE_VHOST_MSG_RESULT_REPLY:
2731 VHOST_LOG_CONFIG(DEBUG,
2732 "Processing %s succeeded and needs reply.\n",
2733 vhost_message_str[request]);
2734 send_vhost_reply(fd, &msg);
2742 skip_to_post_handle:
2743 if (ret != RTE_VHOST_MSG_RESULT_ERR &&
2744 dev->extern_ops.post_msg_handle) {
2745 ret = (*dev->extern_ops.post_msg_handle)(dev->vid,
2748 case RTE_VHOST_MSG_RESULT_REPLY:
2749 send_vhost_reply(fd, &msg);
2751 case RTE_VHOST_MSG_RESULT_ERR:
2752 case RTE_VHOST_MSG_RESULT_OK:
2754 case RTE_VHOST_MSG_RESULT_NOT_HANDLED:
2760 if (unlock_required)
2761 vhost_user_unlock_all_queue_pairs(dev);
2763 /* If message was not handled at this stage, treat it as an error */
2765 VHOST_LOG_CONFIG(ERR,
2766 "vhost message (req: %d) was not handled.\n", request);
2767 close_msg_fds(&msg);
2768 ret = RTE_VHOST_MSG_RESULT_ERR;
2772 * If the request required a reply that was already sent,
2773 * this optional reply-ack won't be sent as the
2774 * VHOST_USER_NEED_REPLY was cleared in send_vhost_reply().
2776 if (msg.flags & VHOST_USER_NEED_REPLY) {
2777 msg.payload.u64 = ret == RTE_VHOST_MSG_RESULT_ERR;
2778 msg.size = sizeof(msg.payload.u64);
2780 send_vhost_reply(fd, &msg);
2781 } else if (ret == RTE_VHOST_MSG_RESULT_ERR) {
2782 VHOST_LOG_CONFIG(ERR,
2783 "vhost message handling failed.\n");
2787 if (!(dev->flags & VIRTIO_DEV_RUNNING) && virtio_is_ready(dev)) {
2788 dev->flags |= VIRTIO_DEV_READY;
2790 if (!(dev->flags & VIRTIO_DEV_RUNNING)) {
2791 if (dev->dequeue_zero_copy) {
2792 VHOST_LOG_CONFIG(INFO,
2793 "dequeue zero copy is enabled\n");
2796 if (dev->notify_ops->new_device(dev->vid) == 0)
2797 dev->flags |= VIRTIO_DEV_RUNNING;
2801 vdpa_dev = dev->vdpa_dev;
2802 if (vdpa_dev && virtio_is_ready(dev) &&
2803 !(dev->flags & VIRTIO_DEV_VDPA_CONFIGURED) &&
2804 msg.request.master == VHOST_USER_SET_VRING_CALL) {
2805 if (vdpa_dev->ops->dev_conf)
2806 vdpa_dev->ops->dev_conf(dev->vid);
2807 dev->flags |= VIRTIO_DEV_VDPA_CONFIGURED;
2813 static int process_slave_message_reply(struct virtio_net *dev,
2814 const struct VhostUserMsg *msg)
2816 struct VhostUserMsg msg_reply;
2819 if ((msg->flags & VHOST_USER_NEED_REPLY) == 0)
2822 ret = read_vhost_message(dev->slave_req_fd, &msg_reply);
2825 VHOST_LOG_CONFIG(ERR,
2826 "vhost read slave message reply failed\n");
2828 VHOST_LOG_CONFIG(INFO,
2829 "vhost peer closed\n");
2835 if (msg_reply.request.slave != msg->request.slave) {
2836 VHOST_LOG_CONFIG(ERR,
2837 "Received unexpected msg type (%u), expected %u\n",
2838 msg_reply.request.slave, msg->request.slave);
2843 ret = msg_reply.payload.u64 ? -1 : 0;
2846 rte_spinlock_unlock(&dev->slave_req_lock);
2851 vhost_user_iotlb_miss(struct virtio_net *dev, uint64_t iova, uint8_t perm)
2854 struct VhostUserMsg msg = {
2855 .request.slave = VHOST_USER_SLAVE_IOTLB_MSG,
2856 .flags = VHOST_USER_VERSION,
2857 .size = sizeof(msg.payload.iotlb),
2861 .type = VHOST_IOTLB_MISS,
2865 ret = send_vhost_message(dev->slave_req_fd, &msg);
2867 VHOST_LOG_CONFIG(ERR,
2868 "Failed to send IOTLB miss message (%d)\n",
2877 vhost_user_slave_config_change(struct virtio_net *dev, bool need_reply)
2880 struct VhostUserMsg msg = {
2881 .request.slave = VHOST_USER_SLAVE_CONFIG_CHANGE_MSG,
2882 .flags = VHOST_USER_VERSION,
2887 msg.flags |= VHOST_USER_NEED_REPLY;
2889 ret = send_vhost_slave_message(dev, &msg);
2891 VHOST_LOG_CONFIG(ERR,
2892 "Failed to send config change (%d)\n",
2897 return process_slave_message_reply(dev, &msg);
2901 rte_vhost_slave_config_change(int vid, bool need_reply)
2903 struct virtio_net *dev;
2905 dev = get_device(vid);
2909 return vhost_user_slave_config_change(dev, need_reply);
2912 static int vhost_user_slave_set_vring_host_notifier(struct virtio_net *dev,
2918 struct VhostUserMsg msg = {
2919 .request.slave = VHOST_USER_SLAVE_VRING_HOST_NOTIFIER_MSG,
2920 .flags = VHOST_USER_VERSION | VHOST_USER_NEED_REPLY,
2921 .size = sizeof(msg.payload.area),
2923 .u64 = index & VHOST_USER_VRING_IDX_MASK,
2930 msg.payload.area.u64 |= VHOST_USER_VRING_NOFD_MASK;
2936 ret = send_vhost_slave_message(dev, &msg);
2938 VHOST_LOG_CONFIG(ERR,
2939 "Failed to set host notifier (%d)\n", ret);
2943 return process_slave_message_reply(dev, &msg);
2946 int rte_vhost_host_notifier_ctrl(int vid, uint16_t qid, bool enable)
2948 struct virtio_net *dev;
2949 struct rte_vdpa_device *vdpa_dev;
2950 int vfio_device_fd, ret = 0;
2951 uint64_t offset, size;
2952 unsigned int i, q_start, q_last;
2954 dev = get_device(vid);
2958 vdpa_dev = dev->vdpa_dev;
2959 if (vdpa_dev == NULL)
2962 if (!(dev->features & (1ULL << VIRTIO_F_VERSION_1)) ||
2963 !(dev->features & (1ULL << VHOST_USER_F_PROTOCOL_FEATURES)) ||
2964 !(dev->protocol_features &
2965 (1ULL << VHOST_USER_PROTOCOL_F_SLAVE_REQ)) ||
2966 !(dev->protocol_features &
2967 (1ULL << VHOST_USER_PROTOCOL_F_SLAVE_SEND_FD)) ||
2968 !(dev->protocol_features &
2969 (1ULL << VHOST_USER_PROTOCOL_F_HOST_NOTIFIER)))
2972 if (qid == RTE_VHOST_QUEUE_ALL) {
2974 q_last = dev->nr_vring - 1;
2976 if (qid >= dev->nr_vring)
2982 RTE_FUNC_PTR_OR_ERR_RET(vdpa_dev->ops->get_vfio_device_fd, -ENOTSUP);
2983 RTE_FUNC_PTR_OR_ERR_RET(vdpa_dev->ops->get_notify_area, -ENOTSUP);
2985 vfio_device_fd = vdpa_dev->ops->get_vfio_device_fd(vid);
2986 if (vfio_device_fd < 0)
2990 for (i = q_start; i <= q_last; i++) {
2991 if (vdpa_dev->ops->get_notify_area(vid, i, &offset,
2997 if (vhost_user_slave_set_vring_host_notifier(dev, i,
2998 vfio_device_fd, offset, size) < 0) {
3005 for (i = q_start; i <= q_last; i++) {
3006 vhost_user_slave_set_vring_host_notifier(dev, i, -1,