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>
31 #include <sys/syscall.h>
32 #ifdef RTE_LIBRTE_VHOST_NUMA
35 #ifdef RTE_LIBRTE_VHOST_POSTCOPY
36 #include <linux/userfaultfd.h>
38 #ifdef F_ADD_SEALS /* if file sealing is supported, so is memfd */
39 #include <linux/memfd.h>
40 #define MEMFD_SUPPORTED
43 #include <rte_common.h>
44 #include <rte_malloc.h>
47 #include <rte_errno.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",
90 [VHOST_USER_SET_STATUS] = "VHOST_USER_SET_STATUS",
91 [VHOST_USER_GET_STATUS] = "VHOST_USER_GET_STATUS",
94 static int send_vhost_reply(struct virtio_net *dev, int sockfd, struct vhu_msg_context *ctx);
95 static int read_vhost_message(struct virtio_net *dev, int sockfd, struct vhu_msg_context *ctx);
98 close_msg_fds(struct vhu_msg_context *ctx)
102 for (i = 0; i < ctx->fd_num; i++) {
103 int fd = ctx->fds[i];
114 * Ensure the expected number of FDs is received,
115 * close all FDs and return an error if this is not the case.
118 validate_msg_fds(struct virtio_net *dev, struct vhu_msg_context *ctx, int expected_fds)
120 if (ctx->fd_num == expected_fds)
123 VHOST_LOG_CONFIG(ERR, "(%s) expect %d FDs for request %s, received %d\n",
124 dev->ifname, expected_fds,
125 vhost_message_str[ctx->msg.request.master],
139 ret = fstat(fd, &stat);
140 return ret == -1 ? (uint64_t)-1 : (uint64_t)stat.st_blksize;
144 async_dma_map(struct virtio_net *dev, bool do_map)
148 struct guest_page *page;
151 for (i = 0; i < dev->nr_guest_pages; i++) {
152 page = &dev->guest_pages[i];
153 ret = rte_vfio_container_dma_map(RTE_VFIO_DEFAULT_CONTAINER_FD,
154 page->host_user_addr,
159 * DMA device may bind with kernel driver, in this case,
160 * we don't need to program IOMMU manually. However, if no
161 * device is bound with vfio/uio in DPDK, and vfio kernel
162 * module is loaded, the API will still be called and return
165 * DPDK vfio only returns ENODEV in very similar situations
166 * (vfio either unsupported, or supported but no devices found).
167 * Either way, no mappings could be performed. We treat it as
168 * normal case in async path. This is a workaround.
170 if (rte_errno == ENODEV)
173 /* DMA mapping errors won't stop VHOST_USER_SET_MEM_TABLE. */
174 VHOST_LOG_CONFIG(ERR, "DMA engine map failed\n");
179 for (i = 0; i < dev->nr_guest_pages; i++) {
180 page = &dev->guest_pages[i];
181 ret = rte_vfio_container_dma_unmap(RTE_VFIO_DEFAULT_CONTAINER_FD,
182 page->host_user_addr,
186 /* like DMA map, ignore the kernel driver case when unmap. */
187 if (rte_errno == EINVAL)
190 VHOST_LOG_CONFIG(ERR, "DMA engine unmap failed\n");
197 free_mem_region(struct virtio_net *dev)
200 struct rte_vhost_mem_region *reg;
202 if (!dev || !dev->mem)
205 if (dev->async_copy && rte_vfio_is_enabled("vfio"))
206 async_dma_map(dev, false);
208 for (i = 0; i < dev->mem->nregions; i++) {
209 reg = &dev->mem->regions[i];
210 if (reg->host_user_addr) {
211 munmap(reg->mmap_addr, reg->mmap_size);
218 vhost_backend_cleanup(struct virtio_net *dev)
220 struct rte_vdpa_device *vdpa_dev;
222 vdpa_dev = dev->vdpa_dev;
223 if (vdpa_dev && vdpa_dev->ops->dev_cleanup != NULL)
224 vdpa_dev->ops->dev_cleanup(dev->vid);
227 free_mem_region(dev);
232 rte_free(dev->guest_pages);
233 dev->guest_pages = NULL;
236 munmap((void *)(uintptr_t)dev->log_addr, dev->log_size);
240 if (dev->inflight_info) {
241 if (dev->inflight_info->addr) {
242 munmap(dev->inflight_info->addr,
243 dev->inflight_info->size);
244 dev->inflight_info->addr = NULL;
247 if (dev->inflight_info->fd >= 0) {
248 close(dev->inflight_info->fd);
249 dev->inflight_info->fd = -1;
252 rte_free(dev->inflight_info);
253 dev->inflight_info = NULL;
256 if (dev->slave_req_fd >= 0) {
257 close(dev->slave_req_fd);
258 dev->slave_req_fd = -1;
261 if (dev->postcopy_ufd >= 0) {
262 close(dev->postcopy_ufd);
263 dev->postcopy_ufd = -1;
266 dev->postcopy_listening = 0;
270 vhost_user_notify_queue_state(struct virtio_net *dev, uint16_t index,
273 struct rte_vdpa_device *vdpa_dev = dev->vdpa_dev;
274 struct vhost_virtqueue *vq = dev->virtqueue[index];
276 /* Configure guest notifications on enable */
277 if (enable && vq->notif_enable != VIRTIO_UNINITIALIZED_NOTIF)
278 vhost_enable_guest_notification(dev, vq, vq->notif_enable);
280 if (vdpa_dev && vdpa_dev->ops->set_vring_state)
281 vdpa_dev->ops->set_vring_state(dev->vid, index, enable);
283 if (dev->notify_ops->vring_state_changed)
284 dev->notify_ops->vring_state_changed(dev->vid,
289 * This function just returns success at the moment unless
290 * the device hasn't been initialised.
293 vhost_user_set_owner(struct virtio_net **pdev,
294 struct vhu_msg_context *ctx,
295 int main_fd __rte_unused)
297 struct virtio_net *dev = *pdev;
299 if (validate_msg_fds(dev, ctx, 0) != 0)
300 return RTE_VHOST_MSG_RESULT_ERR;
302 return RTE_VHOST_MSG_RESULT_OK;
306 vhost_user_reset_owner(struct virtio_net **pdev,
307 struct vhu_msg_context *ctx,
308 int main_fd __rte_unused)
310 struct virtio_net *dev = *pdev;
312 if (validate_msg_fds(dev, ctx, 0) != 0)
313 return RTE_VHOST_MSG_RESULT_ERR;
315 vhost_destroy_device_notify(dev);
317 cleanup_device(dev, 0);
319 return RTE_VHOST_MSG_RESULT_OK;
323 * The features that we support are requested.
326 vhost_user_get_features(struct virtio_net **pdev,
327 struct vhu_msg_context *ctx,
328 int main_fd __rte_unused)
330 struct virtio_net *dev = *pdev;
331 uint64_t features = 0;
333 if (validate_msg_fds(dev, ctx, 0) != 0)
334 return RTE_VHOST_MSG_RESULT_ERR;
336 rte_vhost_driver_get_features(dev->ifname, &features);
338 ctx->msg.payload.u64 = features;
339 ctx->msg.size = sizeof(ctx->msg.payload.u64);
342 return RTE_VHOST_MSG_RESULT_REPLY;
346 * The queue number that we support are requested.
349 vhost_user_get_queue_num(struct virtio_net **pdev,
350 struct vhu_msg_context *ctx,
351 int main_fd __rte_unused)
353 struct virtio_net *dev = *pdev;
354 uint32_t queue_num = 0;
356 if (validate_msg_fds(dev, ctx, 0) != 0)
357 return RTE_VHOST_MSG_RESULT_ERR;
359 rte_vhost_driver_get_queue_num(dev->ifname, &queue_num);
361 ctx->msg.payload.u64 = (uint64_t)queue_num;
362 ctx->msg.size = sizeof(ctx->msg.payload.u64);
365 return RTE_VHOST_MSG_RESULT_REPLY;
369 * We receive the negotiated features supported by us and the virtio device.
372 vhost_user_set_features(struct virtio_net **pdev,
373 struct vhu_msg_context *ctx,
374 int main_fd __rte_unused)
376 struct virtio_net *dev = *pdev;
377 uint64_t features = ctx->msg.payload.u64;
378 uint64_t vhost_features = 0;
379 struct rte_vdpa_device *vdpa_dev;
381 if (validate_msg_fds(dev, ctx, 0) != 0)
382 return RTE_VHOST_MSG_RESULT_ERR;
384 rte_vhost_driver_get_features(dev->ifname, &vhost_features);
385 if (features & ~vhost_features) {
386 VHOST_LOG_CONFIG(ERR, "(%s) received invalid negotiated features.\n",
388 dev->flags |= VIRTIO_DEV_FEATURES_FAILED;
389 dev->status &= ~VIRTIO_DEVICE_STATUS_FEATURES_OK;
391 return RTE_VHOST_MSG_RESULT_ERR;
394 if (dev->flags & VIRTIO_DEV_RUNNING) {
395 if (dev->features == features)
396 return RTE_VHOST_MSG_RESULT_OK;
399 * Error out if master tries to change features while device is
400 * in running state. The exception being VHOST_F_LOG_ALL, which
401 * is enabled when the live-migration starts.
403 if ((dev->features ^ features) & ~(1ULL << VHOST_F_LOG_ALL)) {
404 VHOST_LOG_CONFIG(ERR, "(%s) features changed while device is running.\n",
406 return RTE_VHOST_MSG_RESULT_ERR;
409 if (dev->notify_ops->features_changed)
410 dev->notify_ops->features_changed(dev->vid, features);
413 dev->features = features;
415 ((1ULL << VIRTIO_NET_F_MRG_RXBUF) |
416 (1ULL << VIRTIO_F_VERSION_1) |
417 (1ULL << VIRTIO_F_RING_PACKED))) {
418 dev->vhost_hlen = sizeof(struct virtio_net_hdr_mrg_rxbuf);
420 dev->vhost_hlen = sizeof(struct virtio_net_hdr);
422 VHOST_LOG_CONFIG(INFO, "(%s) negotiated Virtio features: 0x%" PRIx64 "\n",
423 dev->ifname, dev->features);
424 VHOST_LOG_CONFIG(DEBUG, "(%s) mergeable RX buffers %s, virtio 1 %s\n",
426 (dev->features & (1 << VIRTIO_NET_F_MRG_RXBUF)) ? "on" : "off",
427 (dev->features & (1ULL << VIRTIO_F_VERSION_1)) ? "on" : "off");
429 if ((dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET) &&
430 !(dev->features & (1ULL << VIRTIO_NET_F_MQ))) {
432 * Remove all but first queue pair if MQ hasn't been
433 * negotiated. This is safe because the device is not
434 * running at this stage.
436 while (dev->nr_vring > 2) {
437 struct vhost_virtqueue *vq;
439 vq = dev->virtqueue[--dev->nr_vring];
443 dev->virtqueue[dev->nr_vring] = NULL;
445 cleanup_vq_inflight(dev, vq);
450 vdpa_dev = dev->vdpa_dev;
452 vdpa_dev->ops->set_features(dev->vid);
454 dev->flags &= ~VIRTIO_DEV_FEATURES_FAILED;
455 return RTE_VHOST_MSG_RESULT_OK;
459 * The virtio device sends us the size of the descriptor ring.
462 vhost_user_set_vring_num(struct virtio_net **pdev,
463 struct vhu_msg_context *ctx,
464 int main_fd __rte_unused)
466 struct virtio_net *dev = *pdev;
467 struct vhost_virtqueue *vq = dev->virtqueue[ctx->msg.payload.state.index];
469 if (validate_msg_fds(dev, ctx, 0) != 0)
470 return RTE_VHOST_MSG_RESULT_ERR;
472 if (ctx->msg.payload.state.num > 32768) {
473 VHOST_LOG_CONFIG(ERR, "(%s) invalid virtqueue size %u\n",
474 dev->ifname, ctx->msg.payload.state.num);
475 return RTE_VHOST_MSG_RESULT_ERR;
478 vq->size = ctx->msg.payload.state.num;
480 /* VIRTIO 1.0, 2.4 Virtqueues says:
482 * Queue Size value is always a power of 2. The maximum Queue Size
485 * VIRTIO 1.1 2.7 Virtqueues says:
487 * Packed virtqueues support up to 2^15 entries each.
489 if (!vq_is_packed(dev)) {
490 if (vq->size & (vq->size - 1)) {
491 VHOST_LOG_CONFIG(ERR, "(%s) invalid virtqueue size %u\n",
492 dev->ifname, vq->size);
493 return RTE_VHOST_MSG_RESULT_ERR;
497 if (vq_is_packed(dev)) {
498 rte_free(vq->shadow_used_packed);
499 vq->shadow_used_packed = rte_malloc_socket(NULL,
501 sizeof(struct vring_used_elem_packed),
502 RTE_CACHE_LINE_SIZE, vq->numa_node);
503 if (!vq->shadow_used_packed) {
504 VHOST_LOG_CONFIG(ERR,
505 "(%s) failed to allocate memory for shadow used ring.\n",
507 return RTE_VHOST_MSG_RESULT_ERR;
511 rte_free(vq->shadow_used_split);
513 vq->shadow_used_split = rte_malloc_socket(NULL,
514 vq->size * sizeof(struct vring_used_elem),
515 RTE_CACHE_LINE_SIZE, vq->numa_node);
517 if (!vq->shadow_used_split) {
518 VHOST_LOG_CONFIG(ERR,
519 "(%s) failed to allocate memory for vq internal data.\n",
521 return RTE_VHOST_MSG_RESULT_ERR;
525 rte_free(vq->batch_copy_elems);
526 vq->batch_copy_elems = rte_malloc_socket(NULL,
527 vq->size * sizeof(struct batch_copy_elem),
528 RTE_CACHE_LINE_SIZE, vq->numa_node);
529 if (!vq->batch_copy_elems) {
530 VHOST_LOG_CONFIG(ERR, "(%s) failed to allocate memory for batching copy.\n",
532 return RTE_VHOST_MSG_RESULT_ERR;
535 return RTE_VHOST_MSG_RESULT_OK;
539 * Reallocate virtio_dev, vhost_virtqueue and related data structures to
540 * make them on the same numa node as the memory of vring descriptor.
542 #ifdef RTE_LIBRTE_VHOST_NUMA
543 static struct virtio_net*
544 numa_realloc(struct virtio_net *dev, int index)
547 struct virtio_net *old_dev;
548 struct vhost_virtqueue *vq;
549 struct batch_copy_elem *bce;
550 struct guest_page *gp;
551 struct rte_vhost_memory *mem;
556 vq = dev->virtqueue[index];
559 * If VQ is ready, it is too late to reallocate, it certainly already
560 * happened anyway on VHOST_USER_SET_VRING_ADRR.
565 ret = get_mempolicy(&node, NULL, 0, vq->desc, MPOL_F_NODE | MPOL_F_ADDR);
567 VHOST_LOG_CONFIG(ERR, "(%s) unable to get virtqueue %d numa information.\n",
572 if (node == vq->numa_node)
573 goto out_dev_realloc;
575 vq = rte_realloc_socket(vq, sizeof(*vq), 0, node);
577 VHOST_LOG_CONFIG(ERR, "(%s) failed to realloc virtqueue %d on node %d\n",
578 dev->ifname, index, node);
582 if (vq != dev->virtqueue[index]) {
583 VHOST_LOG_CONFIG(INFO, "(%s) reallocated virtqueue on node %d\n",
585 dev->virtqueue[index] = vq;
586 vhost_user_iotlb_init(dev, index);
589 if (vq_is_packed(dev)) {
590 struct vring_used_elem_packed *sup;
592 sup = rte_realloc_socket(vq->shadow_used_packed, vq->size * sizeof(*sup),
593 RTE_CACHE_LINE_SIZE, node);
595 VHOST_LOG_CONFIG(ERR, "(%s) failed to realloc shadow packed on node %d\n",
599 vq->shadow_used_packed = sup;
601 struct vring_used_elem *sus;
603 sus = rte_realloc_socket(vq->shadow_used_split, vq->size * sizeof(*sus),
604 RTE_CACHE_LINE_SIZE, node);
606 VHOST_LOG_CONFIG(ERR, "(%s) failed to realloc shadow split on node %d\n",
610 vq->shadow_used_split = sus;
613 bce = rte_realloc_socket(vq->batch_copy_elems, vq->size * sizeof(*bce),
614 RTE_CACHE_LINE_SIZE, node);
616 VHOST_LOG_CONFIG(ERR, "(%s) failed to realloc batch copy elem on node %d\n",
620 vq->batch_copy_elems = bce;
623 struct log_cache_entry *lc;
625 lc = rte_realloc_socket(vq->log_cache, sizeof(*lc) * VHOST_LOG_CACHE_NR, 0, node);
627 VHOST_LOG_CONFIG(ERR, "(%s) failed to realloc log cache on node %d\n",
634 if (vq->resubmit_inflight) {
635 struct rte_vhost_resubmit_info *ri;
637 ri = rte_realloc_socket(vq->resubmit_inflight, sizeof(*ri), 0, node);
639 VHOST_LOG_CONFIG(ERR, "(%s) failed to realloc resubmit inflight on node %d\n",
643 vq->resubmit_inflight = ri;
645 if (ri->resubmit_list) {
646 struct rte_vhost_resubmit_desc *rd;
648 rd = rte_realloc_socket(ri->resubmit_list, sizeof(*rd) * ri->resubmit_num,
651 VHOST_LOG_CONFIG(ERR, "(%s) failed to realloc resubmit list on node %d\n",
655 ri->resubmit_list = rd;
659 vq->numa_node = node;
663 if (dev->flags & VIRTIO_DEV_RUNNING)
666 ret = get_mempolicy(&dev_node, NULL, 0, dev, MPOL_F_NODE | MPOL_F_ADDR);
668 VHOST_LOG_CONFIG(ERR, "(%s) unable to get numa information.\n", dev->ifname);
672 if (dev_node == node)
675 dev = rte_realloc_socket(old_dev, sizeof(*dev), 0, node);
677 VHOST_LOG_CONFIG(ERR, "(%s) failed to realloc dev on node %d\n",
678 old_dev->ifname, node);
682 VHOST_LOG_CONFIG(INFO, "(%s) reallocated device on node %d\n", dev->ifname, node);
683 vhost_devices[dev->vid] = dev;
685 mem_size = sizeof(struct rte_vhost_memory) +
686 sizeof(struct rte_vhost_mem_region) * dev->mem->nregions;
687 mem = rte_realloc_socket(dev->mem, mem_size, 0, node);
689 VHOST_LOG_CONFIG(ERR, "(%s) failed to realloc mem table on node %d\n",
695 gp = rte_realloc_socket(dev->guest_pages, dev->max_guest_pages * sizeof(*gp),
696 RTE_CACHE_LINE_SIZE, node);
698 VHOST_LOG_CONFIG(ERR, "(%s) failed to realloc guest pages on node %d\n",
702 dev->guest_pages = gp;
707 static struct virtio_net*
708 numa_realloc(struct virtio_net *dev, int index __rte_unused)
714 /* Converts QEMU virtual address to Vhost virtual address. */
716 qva_to_vva(struct virtio_net *dev, uint64_t qva, uint64_t *len)
718 struct rte_vhost_mem_region *r;
721 if (unlikely(!dev || !dev->mem))
724 /* Find the region where the address lives. */
725 for (i = 0; i < dev->mem->nregions; i++) {
726 r = &dev->mem->regions[i];
728 if (qva >= r->guest_user_addr &&
729 qva < r->guest_user_addr + r->size) {
731 if (unlikely(*len > r->guest_user_addr + r->size - qva))
732 *len = r->guest_user_addr + r->size - qva;
734 return qva - r->guest_user_addr +
746 * Converts ring address to Vhost virtual address.
747 * If IOMMU is enabled, the ring address is a guest IO virtual address,
748 * else it is a QEMU virtual address.
751 ring_addr_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
752 uint64_t ra, uint64_t *size)
754 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)) {
757 vhost_user_iotlb_rd_lock(vq);
758 vva = vhost_iova_to_vva(dev, vq, ra,
759 size, VHOST_ACCESS_RW);
760 vhost_user_iotlb_rd_unlock(vq);
765 return qva_to_vva(dev, ra, size);
769 log_addr_to_gpa(struct virtio_net *dev, struct vhost_virtqueue *vq)
773 vhost_user_iotlb_rd_lock(vq);
774 log_gpa = translate_log_addr(dev, vq, vq->ring_addrs.log_guest_addr);
775 vhost_user_iotlb_rd_unlock(vq);
780 static struct virtio_net *
781 translate_ring_addresses(struct virtio_net *dev, int vq_index)
783 struct vhost_virtqueue *vq = dev->virtqueue[vq_index];
784 struct vhost_vring_addr *addr = &vq->ring_addrs;
785 uint64_t len, expected_len;
787 if (addr->flags & (1 << VHOST_VRING_F_LOG)) {
789 log_addr_to_gpa(dev, vq);
790 if (vq->log_guest_addr == 0) {
791 VHOST_LOG_CONFIG(DEBUG, "(%s) failed to map log_guest_addr.\n",
797 if (vq_is_packed(dev)) {
798 len = sizeof(struct vring_packed_desc) * vq->size;
799 vq->desc_packed = (struct vring_packed_desc *)(uintptr_t)
800 ring_addr_to_vva(dev, vq, addr->desc_user_addr, &len);
801 if (vq->desc_packed == NULL ||
802 len != sizeof(struct vring_packed_desc) *
804 VHOST_LOG_CONFIG(DEBUG, "(%s) failed to map desc_packed ring.\n",
809 dev = numa_realloc(dev, vq_index);
810 vq = dev->virtqueue[vq_index];
811 addr = &vq->ring_addrs;
813 len = sizeof(struct vring_packed_desc_event);
814 vq->driver_event = (struct vring_packed_desc_event *)
815 (uintptr_t)ring_addr_to_vva(dev,
816 vq, addr->avail_user_addr, &len);
817 if (vq->driver_event == NULL ||
818 len != sizeof(struct vring_packed_desc_event)) {
819 VHOST_LOG_CONFIG(DEBUG, "(%s) failed to find driver area address.\n",
824 len = sizeof(struct vring_packed_desc_event);
825 vq->device_event = (struct vring_packed_desc_event *)
826 (uintptr_t)ring_addr_to_vva(dev,
827 vq, addr->used_user_addr, &len);
828 if (vq->device_event == NULL ||
829 len != sizeof(struct vring_packed_desc_event)) {
830 VHOST_LOG_CONFIG(DEBUG, "(%s) failed to find device area address.\n",
835 vq->access_ok = true;
839 /* The addresses are converted from QEMU virtual to Vhost virtual. */
840 if (vq->desc && vq->avail && vq->used)
843 len = sizeof(struct vring_desc) * vq->size;
844 vq->desc = (struct vring_desc *)(uintptr_t)ring_addr_to_vva(dev,
845 vq, addr->desc_user_addr, &len);
846 if (vq->desc == 0 || len != sizeof(struct vring_desc) * vq->size) {
847 VHOST_LOG_CONFIG(DEBUG, "(%s) failed to map desc ring.\n", dev->ifname);
851 dev = numa_realloc(dev, vq_index);
852 vq = dev->virtqueue[vq_index];
853 addr = &vq->ring_addrs;
855 len = sizeof(struct vring_avail) + sizeof(uint16_t) * vq->size;
856 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))
857 len += sizeof(uint16_t);
859 vq->avail = (struct vring_avail *)(uintptr_t)ring_addr_to_vva(dev,
860 vq, addr->avail_user_addr, &len);
861 if (vq->avail == 0 || len != expected_len) {
862 VHOST_LOG_CONFIG(DEBUG, "(%s) failed to map avail ring.\n", dev->ifname);
866 len = sizeof(struct vring_used) +
867 sizeof(struct vring_used_elem) * vq->size;
868 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))
869 len += sizeof(uint16_t);
871 vq->used = (struct vring_used *)(uintptr_t)ring_addr_to_vva(dev,
872 vq, addr->used_user_addr, &len);
873 if (vq->used == 0 || len != expected_len) {
874 VHOST_LOG_CONFIG(DEBUG, "(%s) failed to map used ring.\n", dev->ifname);
878 if (vq->last_used_idx != vq->used->idx) {
879 VHOST_LOG_CONFIG(WARNING, "(%s) last_used_idx (%u) and vq->used->idx (%u) mismatches;\n",
881 vq->last_used_idx, vq->used->idx);
882 vq->last_used_idx = vq->used->idx;
883 vq->last_avail_idx = vq->used->idx;
884 VHOST_LOG_CONFIG(WARNING, "(%s) some packets maybe resent for Tx and dropped for Rx\n",
888 vq->access_ok = true;
890 VHOST_LOG_CONFIG(DEBUG, "(%s) mapped address desc: %p\n", dev->ifname, vq->desc);
891 VHOST_LOG_CONFIG(DEBUG, "(%s) mapped address avail: %p\n", dev->ifname, vq->avail);
892 VHOST_LOG_CONFIG(DEBUG, "(%s) mapped address used: %p\n", dev->ifname, vq->used);
893 VHOST_LOG_CONFIG(DEBUG, "(%s) log_guest_addr: %" PRIx64 "\n",
894 dev->ifname, vq->log_guest_addr);
900 * The virtio device sends us the desc, used and avail ring addresses.
901 * This function then converts these to our address space.
904 vhost_user_set_vring_addr(struct virtio_net **pdev,
905 struct vhu_msg_context *ctx,
906 int main_fd __rte_unused)
908 struct virtio_net *dev = *pdev;
909 struct vhost_virtqueue *vq;
910 struct vhost_vring_addr *addr = &ctx->msg.payload.addr;
913 if (validate_msg_fds(dev, ctx, 0) != 0)
914 return RTE_VHOST_MSG_RESULT_ERR;
916 if (dev->mem == NULL)
917 return RTE_VHOST_MSG_RESULT_ERR;
919 /* addr->index refers to the queue index. The txq 1, rxq is 0. */
920 vq = dev->virtqueue[ctx->msg.payload.addr.index];
922 access_ok = vq->access_ok;
925 * Rings addresses should not be interpreted as long as the ring is not
926 * started and enabled
928 memcpy(&vq->ring_addrs, addr, sizeof(*addr));
930 vring_invalidate(dev, vq);
932 if ((vq->enabled && (dev->features &
933 (1ULL << VHOST_USER_F_PROTOCOL_FEATURES))) ||
935 dev = translate_ring_addresses(dev, ctx->msg.payload.addr.index);
937 return RTE_VHOST_MSG_RESULT_ERR;
942 return RTE_VHOST_MSG_RESULT_OK;
946 * The virtio device sends us the available ring last used index.
949 vhost_user_set_vring_base(struct virtio_net **pdev,
950 struct vhu_msg_context *ctx,
951 int main_fd __rte_unused)
953 struct virtio_net *dev = *pdev;
954 struct vhost_virtqueue *vq = dev->virtqueue[ctx->msg.payload.state.index];
955 uint64_t val = ctx->msg.payload.state.num;
957 if (validate_msg_fds(dev, ctx, 0) != 0)
958 return RTE_VHOST_MSG_RESULT_ERR;
960 if (vq_is_packed(dev)) {
962 * Bit[0:14]: avail index
963 * Bit[15]: avail wrap counter
965 vq->last_avail_idx = val & 0x7fff;
966 vq->avail_wrap_counter = !!(val & (0x1 << 15));
968 * Set used index to same value as available one, as
969 * their values should be the same since ring processing
970 * was stopped at get time.
972 vq->last_used_idx = vq->last_avail_idx;
973 vq->used_wrap_counter = vq->avail_wrap_counter;
975 vq->last_used_idx = ctx->msg.payload.state.num;
976 vq->last_avail_idx = ctx->msg.payload.state.num;
979 VHOST_LOG_CONFIG(INFO,
980 "(%s) vring base idx:%u last_used_idx:%u last_avail_idx:%u.\n",
981 dev->ifname, ctx->msg.payload.state.index, vq->last_used_idx,
984 return RTE_VHOST_MSG_RESULT_OK;
988 add_one_guest_page(struct virtio_net *dev, uint64_t guest_phys_addr,
989 uint64_t host_iova, uint64_t host_user_addr, uint64_t size)
991 struct guest_page *page, *last_page;
992 struct guest_page *old_pages;
994 if (dev->nr_guest_pages == dev->max_guest_pages) {
995 dev->max_guest_pages *= 2;
996 old_pages = dev->guest_pages;
997 dev->guest_pages = rte_realloc(dev->guest_pages,
998 dev->max_guest_pages * sizeof(*page),
999 RTE_CACHE_LINE_SIZE);
1000 if (dev->guest_pages == NULL) {
1001 VHOST_LOG_CONFIG(ERR, "cannot realloc guest_pages\n");
1002 rte_free(old_pages);
1007 if (dev->nr_guest_pages > 0) {
1008 last_page = &dev->guest_pages[dev->nr_guest_pages - 1];
1009 /* merge if the two pages are continuous */
1010 if (host_iova == last_page->host_iova + last_page->size &&
1011 guest_phys_addr == last_page->guest_phys_addr + last_page->size &&
1012 host_user_addr == last_page->host_user_addr + last_page->size) {
1013 last_page->size += size;
1018 page = &dev->guest_pages[dev->nr_guest_pages++];
1019 page->guest_phys_addr = guest_phys_addr;
1020 page->host_iova = host_iova;
1021 page->host_user_addr = host_user_addr;
1028 add_guest_pages(struct virtio_net *dev, struct rte_vhost_mem_region *reg,
1031 uint64_t reg_size = reg->size;
1032 uint64_t host_user_addr = reg->host_user_addr;
1033 uint64_t guest_phys_addr = reg->guest_phys_addr;
1037 host_iova = rte_mem_virt2iova((void *)(uintptr_t)host_user_addr);
1038 size = page_size - (guest_phys_addr & (page_size - 1));
1039 size = RTE_MIN(size, reg_size);
1041 if (add_one_guest_page(dev, guest_phys_addr, host_iova,
1042 host_user_addr, size) < 0)
1045 host_user_addr += size;
1046 guest_phys_addr += size;
1049 while (reg_size > 0) {
1050 size = RTE_MIN(reg_size, page_size);
1051 host_iova = rte_mem_virt2iova((void *)(uintptr_t)
1053 if (add_one_guest_page(dev, guest_phys_addr, host_iova,
1054 host_user_addr, size) < 0)
1057 host_user_addr += size;
1058 guest_phys_addr += size;
1062 /* sort guest page array if over binary search threshold */
1063 if (dev->nr_guest_pages >= VHOST_BINARY_SEARCH_THRESH) {
1064 qsort((void *)dev->guest_pages, dev->nr_guest_pages,
1065 sizeof(struct guest_page), guest_page_addrcmp);
1071 #ifdef RTE_LIBRTE_VHOST_DEBUG
1072 /* TODO: enable it only in debug mode? */
1074 dump_guest_pages(struct virtio_net *dev)
1077 struct guest_page *page;
1079 for (i = 0; i < dev->nr_guest_pages; i++) {
1080 page = &dev->guest_pages[i];
1082 VHOST_LOG_CONFIG(INFO, "(%s) guest physical page region %u\n",
1084 VHOST_LOG_CONFIG(INFO, "(%s)\tguest_phys_addr: %" PRIx64 "\n",
1085 dev->ifname, page->guest_phys_addr);
1086 VHOST_LOG_CONFIG(INFO, "(%s)\thost_iova : %" PRIx64 "\n",
1087 dev->ifname, page->host_iova);
1088 VHOST_LOG_CONFIG(INFO, "(%s)\tsize : %" PRIx64 "\n",
1089 dev->ifname, page->size);
1093 #define dump_guest_pages(dev)
1097 vhost_memory_changed(struct VhostUserMemory *new,
1098 struct rte_vhost_memory *old)
1102 if (new->nregions != old->nregions)
1105 for (i = 0; i < new->nregions; ++i) {
1106 VhostUserMemoryRegion *new_r = &new->regions[i];
1107 struct rte_vhost_mem_region *old_r = &old->regions[i];
1109 if (new_r->guest_phys_addr != old_r->guest_phys_addr)
1111 if (new_r->memory_size != old_r->size)
1113 if (new_r->userspace_addr != old_r->guest_user_addr)
1120 #ifdef RTE_LIBRTE_VHOST_POSTCOPY
1122 vhost_user_postcopy_region_register(struct virtio_net *dev,
1123 struct rte_vhost_mem_region *reg)
1125 struct uffdio_register reg_struct;
1128 * Let's register all the mmapped area to ensure
1129 * alignment on page boundary.
1131 reg_struct.range.start = (uint64_t)(uintptr_t)reg->mmap_addr;
1132 reg_struct.range.len = reg->mmap_size;
1133 reg_struct.mode = UFFDIO_REGISTER_MODE_MISSING;
1135 if (ioctl(dev->postcopy_ufd, UFFDIO_REGISTER,
1137 VHOST_LOG_CONFIG(ERR, "(%s) failed to register ufd for region "
1138 "%" PRIx64 " - %" PRIx64 " (ufd = %d) %s\n",
1140 (uint64_t)reg_struct.range.start,
1141 (uint64_t)reg_struct.range.start +
1142 (uint64_t)reg_struct.range.len - 1,
1148 VHOST_LOG_CONFIG(INFO,
1149 "(%s)\t userfaultfd registered for range : %" PRIx64 " - %" PRIx64 "\n",
1151 (uint64_t)reg_struct.range.start,
1152 (uint64_t)reg_struct.range.start +
1153 (uint64_t)reg_struct.range.len - 1);
1159 vhost_user_postcopy_region_register(struct virtio_net *dev __rte_unused,
1160 struct rte_vhost_mem_region *reg __rte_unused)
1167 vhost_user_postcopy_register(struct virtio_net *dev, int main_fd,
1168 struct vhu_msg_context *ctx)
1170 struct VhostUserMemory *memory;
1171 struct rte_vhost_mem_region *reg;
1172 struct vhu_msg_context ack_ctx;
1175 if (!dev->postcopy_listening)
1179 * We haven't a better way right now than sharing
1180 * DPDK's virtual address with Qemu, so that Qemu can
1181 * retrieve the region offset when handling userfaults.
1183 memory = &ctx->msg.payload.memory;
1184 for (i = 0; i < memory->nregions; i++) {
1185 reg = &dev->mem->regions[i];
1186 memory->regions[i].userspace_addr = reg->host_user_addr;
1189 /* Send the addresses back to qemu */
1191 send_vhost_reply(dev, main_fd, ctx);
1193 /* Wait for qemu to acknowledge it got the addresses
1194 * we've got to wait before we're allowed to generate faults.
1196 if (read_vhost_message(dev, main_fd, &ack_ctx) <= 0) {
1197 VHOST_LOG_CONFIG(ERR, "(%s) failed to read qemu ack on postcopy set-mem-table\n",
1202 if (validate_msg_fds(dev, &ack_ctx, 0) != 0)
1205 if (ack_ctx.msg.request.master != VHOST_USER_SET_MEM_TABLE) {
1206 VHOST_LOG_CONFIG(ERR, "(%s) bad qemu ack on postcopy set-mem-table (%d)\n",
1207 dev->ifname, ack_ctx.msg.request.master);
1211 /* Now userfault register and we can use the memory */
1212 for (i = 0; i < memory->nregions; i++) {
1213 reg = &dev->mem->regions[i];
1214 if (vhost_user_postcopy_region_register(dev, reg) < 0)
1222 vhost_user_mmap_region(struct virtio_net *dev,
1223 struct rte_vhost_mem_region *region,
1224 uint64_t mmap_offset)
1231 /* Check for memory_size + mmap_offset overflow */
1232 if (mmap_offset >= -region->size) {
1233 VHOST_LOG_CONFIG(ERR, "(%s) mmap_offset (%#"PRIx64") and memory_size (%#"PRIx64") overflow\n",
1234 dev->ifname, mmap_offset, region->size);
1238 mmap_size = region->size + mmap_offset;
1240 /* mmap() without flag of MAP_ANONYMOUS, should be called with length
1241 * argument aligned with hugepagesz at older longterm version Linux,
1242 * like 2.6.32 and 3.2.72, or mmap() will fail with EINVAL.
1244 * To avoid failure, make sure in caller to keep length aligned.
1246 alignment = get_blk_size(region->fd);
1247 if (alignment == (uint64_t)-1) {
1248 VHOST_LOG_CONFIG(ERR, "(%s) couldn't get hugepage size through fstat\n",
1252 mmap_size = RTE_ALIGN_CEIL(mmap_size, alignment);
1253 if (mmap_size == 0) {
1255 * It could happen if initial mmap_size + alignment overflows
1256 * the sizeof uint64, which could happen if either mmap_size or
1257 * alignment value is wrong.
1259 * mmap() kernel implementation would return an error, but
1260 * better catch it before and provide useful info in the logs.
1262 VHOST_LOG_CONFIG(ERR, "(%s) mmap size (0x%" PRIx64 ") or alignment (0x%" PRIx64 ") is invalid\n",
1263 dev->ifname, region->size + mmap_offset, alignment);
1267 populate = dev->async_copy ? MAP_POPULATE : 0;
1268 mmap_addr = mmap(NULL, mmap_size, PROT_READ | PROT_WRITE,
1269 MAP_SHARED | populate, region->fd, 0);
1271 if (mmap_addr == MAP_FAILED) {
1272 VHOST_LOG_CONFIG(ERR, "(%s) mmap failed (%s).\n", dev->ifname, strerror(errno));
1276 region->mmap_addr = mmap_addr;
1277 region->mmap_size = mmap_size;
1278 region->host_user_addr = (uint64_t)(uintptr_t)mmap_addr + mmap_offset;
1280 if (dev->async_copy) {
1281 if (add_guest_pages(dev, region, alignment) < 0) {
1282 VHOST_LOG_CONFIG(ERR, "(%s) adding guest pages to region failed.\n",
1288 VHOST_LOG_CONFIG(INFO, "(%s) guest memory region size: 0x%" PRIx64 "\n",
1289 dev->ifname, region->size);
1290 VHOST_LOG_CONFIG(INFO, "(%s)\t guest physical addr: 0x%" PRIx64 "\n",
1291 dev->ifname, region->guest_phys_addr);
1292 VHOST_LOG_CONFIG(INFO, "(%s)\t guest virtual addr: 0x%" PRIx64 "\n",
1293 dev->ifname, region->guest_user_addr);
1294 VHOST_LOG_CONFIG(INFO, "(%s)\t host virtual addr: 0x%" PRIx64 "\n",
1295 dev->ifname, region->host_user_addr);
1296 VHOST_LOG_CONFIG(INFO, "(%s)\t mmap addr : 0x%" PRIx64 "\n",
1297 dev->ifname, (uint64_t)(uintptr_t)mmap_addr);
1298 VHOST_LOG_CONFIG(INFO, "(%s)\t mmap size : 0x%" PRIx64 "\n",
1299 dev->ifname, mmap_size);
1300 VHOST_LOG_CONFIG(INFO, "(%s)\t mmap align: 0x%" PRIx64 "\n",
1301 dev->ifname, alignment);
1302 VHOST_LOG_CONFIG(INFO, "(%s)\t mmap off : 0x%" PRIx64 "\n",
1303 dev->ifname, mmap_offset);
1309 vhost_user_set_mem_table(struct virtio_net **pdev,
1310 struct vhu_msg_context *ctx,
1313 struct virtio_net *dev = *pdev;
1314 struct VhostUserMemory *memory = &ctx->msg.payload.memory;
1315 struct rte_vhost_mem_region *reg;
1316 int numa_node = SOCKET_ID_ANY;
1317 uint64_t mmap_offset;
1319 bool async_notify = false;
1321 if (validate_msg_fds(dev, ctx, memory->nregions) != 0)
1322 return RTE_VHOST_MSG_RESULT_ERR;
1324 if (memory->nregions > VHOST_MEMORY_MAX_NREGIONS) {
1325 VHOST_LOG_CONFIG(ERR, "(%s) too many memory regions (%u)\n",
1326 dev->ifname, memory->nregions);
1330 if (dev->mem && !vhost_memory_changed(memory, dev->mem)) {
1331 VHOST_LOG_CONFIG(INFO, "(%s) memory regions not changed\n", dev->ifname);
1335 return RTE_VHOST_MSG_RESULT_OK;
1339 if (dev->flags & VIRTIO_DEV_VDPA_CONFIGURED) {
1340 struct rte_vdpa_device *vdpa_dev = dev->vdpa_dev;
1342 if (vdpa_dev && vdpa_dev->ops->dev_close)
1343 vdpa_dev->ops->dev_close(dev->vid);
1344 dev->flags &= ~VIRTIO_DEV_VDPA_CONFIGURED;
1347 /* notify the vhost application to stop DMA transfers */
1348 if (dev->async_copy && dev->notify_ops->vring_state_changed) {
1349 for (i = 0; i < dev->nr_vring; i++) {
1350 dev->notify_ops->vring_state_changed(dev->vid,
1353 async_notify = true;
1356 free_mem_region(dev);
1361 /* Flush IOTLB cache as previous HVAs are now invalid */
1362 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1363 for (i = 0; i < dev->nr_vring; i++)
1364 vhost_user_iotlb_flush_all(dev->virtqueue[i]);
1367 * If VQ 0 has already been allocated, try to allocate on the same
1368 * NUMA node. It can be reallocated later in numa_realloc().
1370 if (dev->nr_vring > 0)
1371 numa_node = dev->virtqueue[0]->numa_node;
1373 dev->nr_guest_pages = 0;
1374 if (dev->guest_pages == NULL) {
1375 dev->max_guest_pages = 8;
1376 dev->guest_pages = rte_zmalloc_socket(NULL,
1377 dev->max_guest_pages *
1378 sizeof(struct guest_page),
1379 RTE_CACHE_LINE_SIZE,
1381 if (dev->guest_pages == NULL) {
1382 VHOST_LOG_CONFIG(ERR,
1383 "(%s) failed to allocate memory for dev->guest_pages\n",
1389 dev->mem = rte_zmalloc_socket("vhost-mem-table", sizeof(struct rte_vhost_memory) +
1390 sizeof(struct rte_vhost_mem_region) * memory->nregions, 0, numa_node);
1391 if (dev->mem == NULL) {
1392 VHOST_LOG_CONFIG(ERR,
1393 "(%s) failed to allocate memory for dev->mem\n",
1395 goto free_guest_pages;
1398 for (i = 0; i < memory->nregions; i++) {
1399 reg = &dev->mem->regions[i];
1401 reg->guest_phys_addr = memory->regions[i].guest_phys_addr;
1402 reg->guest_user_addr = memory->regions[i].userspace_addr;
1403 reg->size = memory->regions[i].memory_size;
1404 reg->fd = ctx->fds[i];
1407 * Assign invalid file descriptor value to avoid double
1408 * closing on error path.
1412 mmap_offset = memory->regions[i].mmap_offset;
1414 if (vhost_user_mmap_region(dev, reg, mmap_offset) < 0) {
1415 VHOST_LOG_CONFIG(ERR, "(%s) failed to mmap region %u\n", dev->ifname, i);
1416 goto free_mem_table;
1419 dev->mem->nregions++;
1422 if (dev->async_copy && rte_vfio_is_enabled("vfio"))
1423 async_dma_map(dev, true);
1425 if (vhost_user_postcopy_register(dev, main_fd, ctx) < 0)
1426 goto free_mem_table;
1428 for (i = 0; i < dev->nr_vring; i++) {
1429 struct vhost_virtqueue *vq = dev->virtqueue[i];
1434 if (vq->desc || vq->avail || vq->used) {
1436 * If the memory table got updated, the ring addresses
1437 * need to be translated again as virtual addresses have
1440 vring_invalidate(dev, vq);
1442 dev = translate_ring_addresses(dev, i);
1445 goto free_mem_table;
1452 dump_guest_pages(dev);
1455 for (i = 0; i < dev->nr_vring; i++)
1456 dev->notify_ops->vring_state_changed(dev->vid, i, 1);
1459 return RTE_VHOST_MSG_RESULT_OK;
1462 free_mem_region(dev);
1467 rte_free(dev->guest_pages);
1468 dev->guest_pages = NULL;
1471 return RTE_VHOST_MSG_RESULT_ERR;
1475 vq_is_ready(struct virtio_net *dev, struct vhost_virtqueue *vq)
1482 if (vq_is_packed(dev))
1483 rings_ok = vq->desc_packed && vq->driver_event &&
1486 rings_ok = vq->desc && vq->avail && vq->used;
1489 vq->kickfd != VIRTIO_UNINITIALIZED_EVENTFD &&
1490 vq->callfd != VIRTIO_UNINITIALIZED_EVENTFD &&
1494 #define VIRTIO_BUILTIN_NUM_VQS_TO_BE_READY 2u
1497 virtio_is_ready(struct virtio_net *dev)
1499 struct vhost_virtqueue *vq;
1500 uint32_t i, nr_vring = dev->nr_vring;
1502 if (dev->flags & VIRTIO_DEV_READY)
1508 if (dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET) {
1509 nr_vring = VIRTIO_BUILTIN_NUM_VQS_TO_BE_READY;
1511 if (dev->nr_vring < nr_vring)
1515 for (i = 0; i < nr_vring; i++) {
1516 vq = dev->virtqueue[i];
1518 if (!vq_is_ready(dev, vq))
1522 /* If supported, ensure the frontend is really done with config */
1523 if (dev->protocol_features & (1ULL << VHOST_USER_PROTOCOL_F_STATUS))
1524 if (!(dev->status & VIRTIO_DEVICE_STATUS_DRIVER_OK))
1527 dev->flags |= VIRTIO_DEV_READY;
1529 if (!(dev->flags & VIRTIO_DEV_RUNNING))
1530 VHOST_LOG_CONFIG(INFO, "(%s) virtio is now ready for processing.\n", dev->ifname);
1535 inflight_mem_alloc(struct virtio_net *dev, const char *name, size_t size, int *fd)
1539 char fname[20] = "/tmp/memfd-XXXXXX";
1542 #ifdef MEMFD_SUPPORTED
1543 mfd = memfd_create(name, MFD_CLOEXEC);
1548 mfd = mkstemp(fname);
1550 VHOST_LOG_CONFIG(ERR, "(%s) failed to get inflight buffer fd\n",
1558 if (ftruncate(mfd, size) == -1) {
1559 VHOST_LOG_CONFIG(ERR, "(%s) failed to alloc inflight buffer\n", dev->ifname);
1564 ptr = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, mfd, 0);
1565 if (ptr == MAP_FAILED) {
1566 VHOST_LOG_CONFIG(ERR, "(%s) failed to mmap inflight buffer\n", dev->ifname);
1576 get_pervq_shm_size_split(uint16_t queue_size)
1578 return RTE_ALIGN_MUL_CEIL(sizeof(struct rte_vhost_inflight_desc_split) *
1579 queue_size + sizeof(uint64_t) +
1580 sizeof(uint16_t) * 4, INFLIGHT_ALIGNMENT);
1584 get_pervq_shm_size_packed(uint16_t queue_size)
1586 return RTE_ALIGN_MUL_CEIL(sizeof(struct rte_vhost_inflight_desc_packed)
1587 * queue_size + sizeof(uint64_t) +
1588 sizeof(uint16_t) * 6 + sizeof(uint8_t) * 9,
1589 INFLIGHT_ALIGNMENT);
1593 vhost_user_get_inflight_fd(struct virtio_net **pdev,
1594 struct vhu_msg_context *ctx,
1595 int main_fd __rte_unused)
1597 struct rte_vhost_inflight_info_packed *inflight_packed;
1598 uint64_t pervq_inflight_size, mmap_size;
1599 uint16_t num_queues, queue_size;
1600 struct virtio_net *dev = *pdev;
1602 int numa_node = SOCKET_ID_ANY;
1605 if (validate_msg_fds(dev, ctx, 0) != 0)
1606 return RTE_VHOST_MSG_RESULT_ERR;
1608 if (ctx->msg.size != sizeof(ctx->msg.payload.inflight)) {
1609 VHOST_LOG_CONFIG(ERR, "(%s) invalid get_inflight_fd message size is %d\n",
1610 dev->ifname, ctx->msg.size);
1611 return RTE_VHOST_MSG_RESULT_ERR;
1615 * If VQ 0 has already been allocated, try to allocate on the same
1616 * NUMA node. It can be reallocated later in numa_realloc().
1618 if (dev->nr_vring > 0)
1619 numa_node = dev->virtqueue[0]->numa_node;
1621 if (dev->inflight_info == NULL) {
1622 dev->inflight_info = rte_zmalloc_socket("inflight_info",
1623 sizeof(struct inflight_mem_info), 0, numa_node);
1624 if (!dev->inflight_info) {
1625 VHOST_LOG_CONFIG(ERR, "(%s) failed to alloc dev inflight area\n",
1627 return RTE_VHOST_MSG_RESULT_ERR;
1629 dev->inflight_info->fd = -1;
1632 num_queues = ctx->msg.payload.inflight.num_queues;
1633 queue_size = ctx->msg.payload.inflight.queue_size;
1635 VHOST_LOG_CONFIG(INFO, "(%s) get_inflight_fd num_queues: %u\n",
1636 dev->ifname, ctx->msg.payload.inflight.num_queues);
1637 VHOST_LOG_CONFIG(INFO, "(%s) get_inflight_fd queue_size: %u\n",
1638 dev->ifname, ctx->msg.payload.inflight.queue_size);
1640 if (vq_is_packed(dev))
1641 pervq_inflight_size = get_pervq_shm_size_packed(queue_size);
1643 pervq_inflight_size = get_pervq_shm_size_split(queue_size);
1645 mmap_size = num_queues * pervq_inflight_size;
1646 addr = inflight_mem_alloc(dev, "vhost-inflight", mmap_size, &fd);
1648 VHOST_LOG_CONFIG(ERR, "(%s) failed to alloc vhost inflight area\n", dev->ifname);
1649 ctx->msg.payload.inflight.mmap_size = 0;
1650 return RTE_VHOST_MSG_RESULT_ERR;
1652 memset(addr, 0, mmap_size);
1654 if (dev->inflight_info->addr) {
1655 munmap(dev->inflight_info->addr, dev->inflight_info->size);
1656 dev->inflight_info->addr = NULL;
1659 if (dev->inflight_info->fd >= 0) {
1660 close(dev->inflight_info->fd);
1661 dev->inflight_info->fd = -1;
1664 dev->inflight_info->addr = addr;
1665 dev->inflight_info->size = ctx->msg.payload.inflight.mmap_size = mmap_size;
1666 dev->inflight_info->fd = ctx->fds[0] = fd;
1667 ctx->msg.payload.inflight.mmap_offset = 0;
1670 if (vq_is_packed(dev)) {
1671 for (i = 0; i < num_queues; i++) {
1673 (struct rte_vhost_inflight_info_packed *)addr;
1674 inflight_packed->used_wrap_counter = 1;
1675 inflight_packed->old_used_wrap_counter = 1;
1676 for (j = 0; j < queue_size; j++)
1677 inflight_packed->desc[j].next = j + 1;
1678 addr = (void *)((char *)addr + pervq_inflight_size);
1682 VHOST_LOG_CONFIG(INFO, "(%s) send inflight mmap_size: %"PRIu64"\n",
1683 dev->ifname, ctx->msg.payload.inflight.mmap_size);
1684 VHOST_LOG_CONFIG(INFO, "(%s) send inflight mmap_offset: %"PRIu64"\n",
1685 dev->ifname, ctx->msg.payload.inflight.mmap_offset);
1686 VHOST_LOG_CONFIG(INFO, "(%s) send inflight fd: %d\n", dev->ifname, ctx->fds[0]);
1688 return RTE_VHOST_MSG_RESULT_REPLY;
1692 vhost_user_set_inflight_fd(struct virtio_net **pdev,
1693 struct vhu_msg_context *ctx,
1694 int main_fd __rte_unused)
1696 uint64_t mmap_size, mmap_offset;
1697 uint16_t num_queues, queue_size;
1698 struct virtio_net *dev = *pdev;
1699 uint32_t pervq_inflight_size;
1700 struct vhost_virtqueue *vq;
1703 int numa_node = SOCKET_ID_ANY;
1705 if (validate_msg_fds(dev, ctx, 1) != 0)
1706 return RTE_VHOST_MSG_RESULT_ERR;
1709 if (ctx->msg.size != sizeof(ctx->msg.payload.inflight) || fd < 0) {
1710 VHOST_LOG_CONFIG(ERR, "(%s) invalid set_inflight_fd message size is %d,fd is %d\n",
1711 dev->ifname, ctx->msg.size, fd);
1712 return RTE_VHOST_MSG_RESULT_ERR;
1715 mmap_size = ctx->msg.payload.inflight.mmap_size;
1716 mmap_offset = ctx->msg.payload.inflight.mmap_offset;
1717 num_queues = ctx->msg.payload.inflight.num_queues;
1718 queue_size = ctx->msg.payload.inflight.queue_size;
1720 if (vq_is_packed(dev))
1721 pervq_inflight_size = get_pervq_shm_size_packed(queue_size);
1723 pervq_inflight_size = get_pervq_shm_size_split(queue_size);
1725 VHOST_LOG_CONFIG(INFO, "(%s) set_inflight_fd mmap_size: %"PRIu64"\n",
1726 dev->ifname, mmap_size);
1727 VHOST_LOG_CONFIG(INFO, "(%s) set_inflight_fd mmap_offset: %"PRIu64"\n",
1728 dev->ifname, mmap_offset);
1729 VHOST_LOG_CONFIG(INFO, "(%s) set_inflight_fd num_queues: %u\n", dev->ifname, num_queues);
1730 VHOST_LOG_CONFIG(INFO, "(%s) set_inflight_fd queue_size: %u\n", dev->ifname, queue_size);
1731 VHOST_LOG_CONFIG(INFO, "(%s) set_inflight_fd fd: %d\n", dev->ifname, fd);
1732 VHOST_LOG_CONFIG(INFO, "(%s) set_inflight_fd pervq_inflight_size: %d\n",
1733 dev->ifname, pervq_inflight_size);
1736 * If VQ 0 has already been allocated, try to allocate on the same
1737 * NUMA node. It can be reallocated later in numa_realloc().
1739 if (dev->nr_vring > 0)
1740 numa_node = dev->virtqueue[0]->numa_node;
1742 if (!dev->inflight_info) {
1743 dev->inflight_info = rte_zmalloc_socket("inflight_info",
1744 sizeof(struct inflight_mem_info), 0, numa_node);
1745 if (dev->inflight_info == NULL) {
1746 VHOST_LOG_CONFIG(ERR, "(%s) failed to alloc dev inflight area\n",
1748 return RTE_VHOST_MSG_RESULT_ERR;
1750 dev->inflight_info->fd = -1;
1753 if (dev->inflight_info->addr) {
1754 munmap(dev->inflight_info->addr, dev->inflight_info->size);
1755 dev->inflight_info->addr = NULL;
1758 addr = mmap(0, mmap_size, PROT_READ | PROT_WRITE, MAP_SHARED,
1760 if (addr == MAP_FAILED) {
1761 VHOST_LOG_CONFIG(ERR, "(%s) failed to mmap share memory.\n", dev->ifname);
1762 return RTE_VHOST_MSG_RESULT_ERR;
1765 if (dev->inflight_info->fd >= 0) {
1766 close(dev->inflight_info->fd);
1767 dev->inflight_info->fd = -1;
1770 dev->inflight_info->fd = fd;
1771 dev->inflight_info->addr = addr;
1772 dev->inflight_info->size = mmap_size;
1774 for (i = 0; i < num_queues; i++) {
1775 vq = dev->virtqueue[i];
1779 if (vq_is_packed(dev)) {
1780 vq->inflight_packed = addr;
1781 vq->inflight_packed->desc_num = queue_size;
1783 vq->inflight_split = addr;
1784 vq->inflight_split->desc_num = queue_size;
1786 addr = (void *)((char *)addr + pervq_inflight_size);
1789 return RTE_VHOST_MSG_RESULT_OK;
1793 vhost_user_set_vring_call(struct virtio_net **pdev,
1794 struct vhu_msg_context *ctx,
1795 int main_fd __rte_unused)
1797 struct virtio_net *dev = *pdev;
1798 struct vhost_vring_file file;
1799 struct vhost_virtqueue *vq;
1802 expected_fds = (ctx->msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK) ? 0 : 1;
1803 if (validate_msg_fds(dev, ctx, expected_fds) != 0)
1804 return RTE_VHOST_MSG_RESULT_ERR;
1806 file.index = ctx->msg.payload.u64 & VHOST_USER_VRING_IDX_MASK;
1807 if (ctx->msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK)
1808 file.fd = VIRTIO_INVALID_EVENTFD;
1810 file.fd = ctx->fds[0];
1811 VHOST_LOG_CONFIG(INFO, "(%s) vring call idx:%d file:%d\n",
1812 dev->ifname, file.index, file.fd);
1814 vq = dev->virtqueue[file.index];
1818 vhost_user_notify_queue_state(dev, file.index, 0);
1821 if (vq->callfd >= 0)
1824 vq->callfd = file.fd;
1826 return RTE_VHOST_MSG_RESULT_OK;
1829 static int vhost_user_set_vring_err(struct virtio_net **pdev,
1830 struct vhu_msg_context *ctx,
1831 int main_fd __rte_unused)
1833 struct virtio_net *dev = *pdev;
1836 expected_fds = (ctx->msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK) ? 0 : 1;
1837 if (validate_msg_fds(dev, ctx, expected_fds) != 0)
1838 return RTE_VHOST_MSG_RESULT_ERR;
1840 if (!(ctx->msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK))
1842 VHOST_LOG_CONFIG(INFO, "(%s) not implemented\n", dev->ifname);
1844 return RTE_VHOST_MSG_RESULT_OK;
1848 resubmit_desc_compare(const void *a, const void *b)
1850 const struct rte_vhost_resubmit_desc *desc0 = a;
1851 const struct rte_vhost_resubmit_desc *desc1 = b;
1853 if (desc1->counter > desc0->counter)
1860 vhost_check_queue_inflights_split(struct virtio_net *dev,
1861 struct vhost_virtqueue *vq)
1864 uint16_t resubmit_num = 0, last_io, num;
1865 struct vring_used *used = vq->used;
1866 struct rte_vhost_resubmit_info *resubmit;
1867 struct rte_vhost_inflight_info_split *inflight_split;
1869 if (!(dev->protocol_features &
1870 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD)))
1871 return RTE_VHOST_MSG_RESULT_OK;
1873 /* The frontend may still not support the inflight feature
1874 * although we negotiate the protocol feature.
1876 if ((!vq->inflight_split))
1877 return RTE_VHOST_MSG_RESULT_OK;
1879 if (!vq->inflight_split->version) {
1880 vq->inflight_split->version = INFLIGHT_VERSION;
1881 return RTE_VHOST_MSG_RESULT_OK;
1884 if (vq->resubmit_inflight)
1885 return RTE_VHOST_MSG_RESULT_OK;
1887 inflight_split = vq->inflight_split;
1888 vq->global_counter = 0;
1889 last_io = inflight_split->last_inflight_io;
1891 if (inflight_split->used_idx != used->idx) {
1892 inflight_split->desc[last_io].inflight = 0;
1893 rte_atomic_thread_fence(__ATOMIC_SEQ_CST);
1894 inflight_split->used_idx = used->idx;
1897 for (i = 0; i < inflight_split->desc_num; i++) {
1898 if (inflight_split->desc[i].inflight == 1)
1902 vq->last_avail_idx += resubmit_num;
1905 resubmit = rte_zmalloc_socket("resubmit", sizeof(struct rte_vhost_resubmit_info),
1908 VHOST_LOG_CONFIG(ERR,
1909 "(%s) failed to allocate memory for resubmit info.\n",
1911 return RTE_VHOST_MSG_RESULT_ERR;
1914 resubmit->resubmit_list = rte_zmalloc_socket("resubmit_list",
1915 resubmit_num * sizeof(struct rte_vhost_resubmit_desc),
1917 if (!resubmit->resubmit_list) {
1918 VHOST_LOG_CONFIG(ERR,
1919 "(%s) failed to allocate memory for inflight desc.\n",
1922 return RTE_VHOST_MSG_RESULT_ERR;
1926 for (i = 0; i < vq->inflight_split->desc_num; i++) {
1927 if (vq->inflight_split->desc[i].inflight == 1) {
1928 resubmit->resubmit_list[num].index = i;
1929 resubmit->resubmit_list[num].counter =
1930 inflight_split->desc[i].counter;
1934 resubmit->resubmit_num = num;
1936 if (resubmit->resubmit_num > 1)
1937 qsort(resubmit->resubmit_list, resubmit->resubmit_num,
1938 sizeof(struct rte_vhost_resubmit_desc),
1939 resubmit_desc_compare);
1941 vq->global_counter = resubmit->resubmit_list[0].counter + 1;
1942 vq->resubmit_inflight = resubmit;
1945 return RTE_VHOST_MSG_RESULT_OK;
1949 vhost_check_queue_inflights_packed(struct virtio_net *dev,
1950 struct vhost_virtqueue *vq)
1953 uint16_t resubmit_num = 0, old_used_idx, num;
1954 struct rte_vhost_resubmit_info *resubmit;
1955 struct rte_vhost_inflight_info_packed *inflight_packed;
1957 if (!(dev->protocol_features &
1958 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD)))
1959 return RTE_VHOST_MSG_RESULT_OK;
1961 /* The frontend may still not support the inflight feature
1962 * although we negotiate the protocol feature.
1964 if ((!vq->inflight_packed))
1965 return RTE_VHOST_MSG_RESULT_OK;
1967 if (!vq->inflight_packed->version) {
1968 vq->inflight_packed->version = INFLIGHT_VERSION;
1969 return RTE_VHOST_MSG_RESULT_OK;
1972 if (vq->resubmit_inflight)
1973 return RTE_VHOST_MSG_RESULT_OK;
1975 inflight_packed = vq->inflight_packed;
1976 vq->global_counter = 0;
1977 old_used_idx = inflight_packed->old_used_idx;
1979 if (inflight_packed->used_idx != old_used_idx) {
1980 if (inflight_packed->desc[old_used_idx].inflight == 0) {
1981 inflight_packed->old_used_idx =
1982 inflight_packed->used_idx;
1983 inflight_packed->old_used_wrap_counter =
1984 inflight_packed->used_wrap_counter;
1985 inflight_packed->old_free_head =
1986 inflight_packed->free_head;
1988 inflight_packed->used_idx =
1989 inflight_packed->old_used_idx;
1990 inflight_packed->used_wrap_counter =
1991 inflight_packed->old_used_wrap_counter;
1992 inflight_packed->free_head =
1993 inflight_packed->old_free_head;
1997 for (i = 0; i < inflight_packed->desc_num; i++) {
1998 if (inflight_packed->desc[i].inflight == 1)
2003 resubmit = rte_zmalloc_socket("resubmit", sizeof(struct rte_vhost_resubmit_info),
2005 if (resubmit == NULL) {
2006 VHOST_LOG_CONFIG(ERR,
2007 "(%s) failed to allocate memory for resubmit info.\n",
2009 return RTE_VHOST_MSG_RESULT_ERR;
2012 resubmit->resubmit_list = rte_zmalloc_socket("resubmit_list",
2013 resubmit_num * sizeof(struct rte_vhost_resubmit_desc),
2015 if (resubmit->resubmit_list == NULL) {
2016 VHOST_LOG_CONFIG(ERR,
2017 "(%s) failed to allocate memory for resubmit desc.\n",
2020 return RTE_VHOST_MSG_RESULT_ERR;
2024 for (i = 0; i < inflight_packed->desc_num; i++) {
2025 if (vq->inflight_packed->desc[i].inflight == 1) {
2026 resubmit->resubmit_list[num].index = i;
2027 resubmit->resubmit_list[num].counter =
2028 inflight_packed->desc[i].counter;
2032 resubmit->resubmit_num = num;
2034 if (resubmit->resubmit_num > 1)
2035 qsort(resubmit->resubmit_list, resubmit->resubmit_num,
2036 sizeof(struct rte_vhost_resubmit_desc),
2037 resubmit_desc_compare);
2039 vq->global_counter = resubmit->resubmit_list[0].counter + 1;
2040 vq->resubmit_inflight = resubmit;
2043 return RTE_VHOST_MSG_RESULT_OK;
2047 vhost_user_set_vring_kick(struct virtio_net **pdev,
2048 struct vhu_msg_context *ctx,
2049 int main_fd __rte_unused)
2051 struct virtio_net *dev = *pdev;
2052 struct vhost_vring_file file;
2053 struct vhost_virtqueue *vq;
2056 expected_fds = (ctx->msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK) ? 0 : 1;
2057 if (validate_msg_fds(dev, ctx, expected_fds) != 0)
2058 return RTE_VHOST_MSG_RESULT_ERR;
2060 file.index = ctx->msg.payload.u64 & VHOST_USER_VRING_IDX_MASK;
2061 if (ctx->msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK)
2062 file.fd = VIRTIO_INVALID_EVENTFD;
2064 file.fd = ctx->fds[0];
2065 VHOST_LOG_CONFIG(INFO, "(%s) vring kick idx:%d file:%d\n",
2066 dev->ifname, file.index, file.fd);
2068 /* Interpret ring addresses only when ring is started. */
2069 dev = translate_ring_addresses(dev, file.index);
2071 if (file.fd != VIRTIO_INVALID_EVENTFD)
2074 return RTE_VHOST_MSG_RESULT_ERR;
2079 vq = dev->virtqueue[file.index];
2082 * When VHOST_USER_F_PROTOCOL_FEATURES is not negotiated,
2083 * the ring starts already enabled. Otherwise, it is enabled via
2084 * the SET_VRING_ENABLE message.
2086 if (!(dev->features & (1ULL << VHOST_USER_F_PROTOCOL_FEATURES))) {
2092 vhost_user_notify_queue_state(dev, file.index, 0);
2095 if (vq->kickfd >= 0)
2097 vq->kickfd = file.fd;
2099 if (vq_is_packed(dev)) {
2100 if (vhost_check_queue_inflights_packed(dev, vq)) {
2101 VHOST_LOG_CONFIG(ERR, "(%s) failed to inflights for vq: %d\n",
2102 dev->ifname, file.index);
2103 return RTE_VHOST_MSG_RESULT_ERR;
2106 if (vhost_check_queue_inflights_split(dev, vq)) {
2107 VHOST_LOG_CONFIG(ERR, "(%s) failed to inflights for vq: %d\n",
2108 dev->ifname, file.index);
2109 return RTE_VHOST_MSG_RESULT_ERR;
2113 return RTE_VHOST_MSG_RESULT_OK;
2117 * when virtio is stopped, qemu will send us the GET_VRING_BASE message.
2120 vhost_user_get_vring_base(struct virtio_net **pdev,
2121 struct vhu_msg_context *ctx,
2122 int main_fd __rte_unused)
2124 struct virtio_net *dev = *pdev;
2125 struct vhost_virtqueue *vq = dev->virtqueue[ctx->msg.payload.state.index];
2128 if (validate_msg_fds(dev, ctx, 0) != 0)
2129 return RTE_VHOST_MSG_RESULT_ERR;
2131 /* We have to stop the queue (virtio) if it is running. */
2132 vhost_destroy_device_notify(dev);
2134 dev->flags &= ~VIRTIO_DEV_READY;
2135 dev->flags &= ~VIRTIO_DEV_VDPA_CONFIGURED;
2137 /* Here we are safe to get the indexes */
2138 if (vq_is_packed(dev)) {
2140 * Bit[0:14]: avail index
2141 * Bit[15]: avail wrap counter
2143 val = vq->last_avail_idx & 0x7fff;
2144 val |= vq->avail_wrap_counter << 15;
2145 ctx->msg.payload.state.num = val;
2147 ctx->msg.payload.state.num = vq->last_avail_idx;
2150 VHOST_LOG_CONFIG(INFO, "(%s) vring base idx:%d file:%d\n",
2151 dev->ifname, ctx->msg.payload.state.index,
2152 ctx->msg.payload.state.num);
2154 * Based on current qemu vhost-user implementation, this message is
2155 * sent and only sent in vhost_vring_stop.
2156 * TODO: cleanup the vring, it isn't usable since here.
2158 if (vq->kickfd >= 0)
2161 vq->kickfd = VIRTIO_UNINITIALIZED_EVENTFD;
2163 if (vq->callfd >= 0)
2166 vq->callfd = VIRTIO_UNINITIALIZED_EVENTFD;
2168 vq->signalled_used_valid = false;
2170 if (vq_is_packed(dev)) {
2171 rte_free(vq->shadow_used_packed);
2172 vq->shadow_used_packed = NULL;
2174 rte_free(vq->shadow_used_split);
2175 vq->shadow_used_split = NULL;
2178 rte_free(vq->batch_copy_elems);
2179 vq->batch_copy_elems = NULL;
2181 rte_free(vq->log_cache);
2182 vq->log_cache = NULL;
2184 ctx->msg.size = sizeof(ctx->msg.payload.state);
2187 vhost_user_iotlb_flush_all(vq);
2189 vring_invalidate(dev, vq);
2191 return RTE_VHOST_MSG_RESULT_REPLY;
2195 * when virtio queues are ready to work, qemu will send us to
2196 * enable the virtio queue pair.
2199 vhost_user_set_vring_enable(struct virtio_net **pdev,
2200 struct vhu_msg_context *ctx,
2201 int main_fd __rte_unused)
2203 struct virtio_net *dev = *pdev;
2204 bool enable = !!ctx->msg.payload.state.num;
2205 int index = (int)ctx->msg.payload.state.index;
2207 if (validate_msg_fds(dev, ctx, 0) != 0)
2208 return RTE_VHOST_MSG_RESULT_ERR;
2210 VHOST_LOG_CONFIG(INFO, "(%s) set queue enable: %d to qp idx: %d\n",
2211 dev->ifname, enable, index);
2213 if (enable && dev->virtqueue[index]->async) {
2214 if (dev->virtqueue[index]->async->pkts_inflight_n) {
2215 VHOST_LOG_CONFIG(ERR,
2216 "(%s) failed to enable vring. Inflight packets must be completed first\n",
2218 return RTE_VHOST_MSG_RESULT_ERR;
2222 dev->virtqueue[index]->enabled = enable;
2224 return RTE_VHOST_MSG_RESULT_OK;
2228 vhost_user_get_protocol_features(struct virtio_net **pdev,
2229 struct vhu_msg_context *ctx,
2230 int main_fd __rte_unused)
2232 struct virtio_net *dev = *pdev;
2233 uint64_t features, protocol_features;
2235 if (validate_msg_fds(dev, ctx, 0) != 0)
2236 return RTE_VHOST_MSG_RESULT_ERR;
2238 rte_vhost_driver_get_features(dev->ifname, &features);
2239 rte_vhost_driver_get_protocol_features(dev->ifname, &protocol_features);
2241 ctx->msg.payload.u64 = protocol_features;
2242 ctx->msg.size = sizeof(ctx->msg.payload.u64);
2245 return RTE_VHOST_MSG_RESULT_REPLY;
2249 vhost_user_set_protocol_features(struct virtio_net **pdev,
2250 struct vhu_msg_context *ctx,
2251 int main_fd __rte_unused)
2253 struct virtio_net *dev = *pdev;
2254 uint64_t protocol_features = ctx->msg.payload.u64;
2255 uint64_t slave_protocol_features = 0;
2257 if (validate_msg_fds(dev, ctx, 0) != 0)
2258 return RTE_VHOST_MSG_RESULT_ERR;
2260 rte_vhost_driver_get_protocol_features(dev->ifname,
2261 &slave_protocol_features);
2262 if (protocol_features & ~slave_protocol_features) {
2263 VHOST_LOG_CONFIG(ERR, "(%s) received invalid protocol features.\n", dev->ifname);
2264 return RTE_VHOST_MSG_RESULT_ERR;
2267 dev->protocol_features = protocol_features;
2268 VHOST_LOG_CONFIG(INFO, "(%s) negotiated Vhost-user protocol features: 0x%" PRIx64 "\n",
2269 dev->ifname, dev->protocol_features);
2271 return RTE_VHOST_MSG_RESULT_OK;
2275 vhost_user_set_log_base(struct virtio_net **pdev,
2276 struct vhu_msg_context *ctx,
2277 int main_fd __rte_unused)
2279 struct virtio_net *dev = *pdev;
2280 int fd = ctx->fds[0];
2285 if (validate_msg_fds(dev, ctx, 1) != 0)
2286 return RTE_VHOST_MSG_RESULT_ERR;
2289 VHOST_LOG_CONFIG(ERR, "(%s) invalid log fd: %d\n", dev->ifname, fd);
2290 return RTE_VHOST_MSG_RESULT_ERR;
2293 if (ctx->msg.size != sizeof(VhostUserLog)) {
2294 VHOST_LOG_CONFIG(ERR, "(%s) invalid log base msg size: %"PRId32" != %d\n",
2295 dev->ifname, ctx->msg.size, (int)sizeof(VhostUserLog));
2299 size = ctx->msg.payload.log.mmap_size;
2300 off = ctx->msg.payload.log.mmap_offset;
2302 /* Check for mmap size and offset overflow. */
2304 VHOST_LOG_CONFIG(ERR,
2305 "(%s) log offset %#"PRIx64" and log size %#"PRIx64" overflow\n",
2306 dev->ifname, off, size);
2310 VHOST_LOG_CONFIG(INFO, "(%s) log mmap size: %"PRId64", offset: %"PRId64"\n",
2311 dev->ifname, size, off);
2314 * mmap from 0 to workaround a hugepage mmap bug: mmap will
2315 * fail when offset is not page size aligned.
2317 addr = mmap(0, size + off, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
2319 if (addr == MAP_FAILED) {
2320 VHOST_LOG_CONFIG(ERR, "(%s) mmap log base failed!\n", dev->ifname);
2321 return RTE_VHOST_MSG_RESULT_ERR;
2325 * Free previously mapped log memory on occasionally
2326 * multiple VHOST_USER_SET_LOG_BASE.
2328 if (dev->log_addr) {
2329 munmap((void *)(uintptr_t)dev->log_addr, dev->log_size);
2331 dev->log_addr = (uint64_t)(uintptr_t)addr;
2332 dev->log_base = dev->log_addr + off;
2333 dev->log_size = size;
2335 for (i = 0; i < dev->nr_vring; i++) {
2336 struct vhost_virtqueue *vq = dev->virtqueue[i];
2338 rte_free(vq->log_cache);
2339 vq->log_cache = NULL;
2340 vq->log_cache_nb_elem = 0;
2341 vq->log_cache = rte_malloc_socket("vq log cache",
2342 sizeof(struct log_cache_entry) * VHOST_LOG_CACHE_NR,
2345 * If log cache alloc fail, don't fail migration, but no
2346 * caching will be done, which will impact performance
2349 VHOST_LOG_CONFIG(ERR, "(%s) failed to allocate VQ logging cache\n",
2354 * The spec is not clear about it (yet), but QEMU doesn't expect
2355 * any payload in the reply.
2360 return RTE_VHOST_MSG_RESULT_REPLY;
2364 return RTE_VHOST_MSG_RESULT_ERR;
2367 static int vhost_user_set_log_fd(struct virtio_net **pdev,
2368 struct vhu_msg_context *ctx,
2369 int main_fd __rte_unused)
2371 struct virtio_net *dev = *pdev;
2373 if (validate_msg_fds(dev, ctx, 1) != 0)
2374 return RTE_VHOST_MSG_RESULT_ERR;
2377 VHOST_LOG_CONFIG(INFO, "(%s) not implemented.\n", dev->ifname);
2379 return RTE_VHOST_MSG_RESULT_OK;
2383 * An rarp packet is constructed and broadcasted to notify switches about
2384 * the new location of the migrated VM, so that packets from outside will
2385 * not be lost after migration.
2387 * However, we don't actually "send" a rarp packet here, instead, we set
2388 * a flag 'broadcast_rarp' to let rte_vhost_dequeue_burst() inject it.
2391 vhost_user_send_rarp(struct virtio_net **pdev,
2392 struct vhu_msg_context *ctx,
2393 int main_fd __rte_unused)
2395 struct virtio_net *dev = *pdev;
2396 uint8_t *mac = (uint8_t *)&ctx->msg.payload.u64;
2397 struct rte_vdpa_device *vdpa_dev;
2399 if (validate_msg_fds(dev, ctx, 0) != 0)
2400 return RTE_VHOST_MSG_RESULT_ERR;
2402 VHOST_LOG_CONFIG(DEBUG, "(%s) MAC: " RTE_ETHER_ADDR_PRT_FMT "\n",
2403 dev->ifname, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
2404 memcpy(dev->mac.addr_bytes, mac, 6);
2407 * Set the flag to inject a RARP broadcast packet at
2408 * rte_vhost_dequeue_burst().
2410 * __ATOMIC_RELEASE ordering is for making sure the mac is
2411 * copied before the flag is set.
2413 __atomic_store_n(&dev->broadcast_rarp, 1, __ATOMIC_RELEASE);
2414 vdpa_dev = dev->vdpa_dev;
2415 if (vdpa_dev && vdpa_dev->ops->migration_done)
2416 vdpa_dev->ops->migration_done(dev->vid);
2418 return RTE_VHOST_MSG_RESULT_OK;
2422 vhost_user_net_set_mtu(struct virtio_net **pdev,
2423 struct vhu_msg_context *ctx,
2424 int main_fd __rte_unused)
2426 struct virtio_net *dev = *pdev;
2428 if (validate_msg_fds(dev, ctx, 0) != 0)
2429 return RTE_VHOST_MSG_RESULT_ERR;
2431 if (ctx->msg.payload.u64 < VIRTIO_MIN_MTU ||
2432 ctx->msg.payload.u64 > VIRTIO_MAX_MTU) {
2433 VHOST_LOG_CONFIG(ERR, "(%s) invalid MTU size (%"PRIu64")\n",
2434 dev->ifname, ctx->msg.payload.u64);
2436 return RTE_VHOST_MSG_RESULT_ERR;
2439 dev->mtu = ctx->msg.payload.u64;
2441 return RTE_VHOST_MSG_RESULT_OK;
2445 vhost_user_set_req_fd(struct virtio_net **pdev,
2446 struct vhu_msg_context *ctx,
2447 int main_fd __rte_unused)
2449 struct virtio_net *dev = *pdev;
2450 int fd = ctx->fds[0];
2452 if (validate_msg_fds(dev, ctx, 1) != 0)
2453 return RTE_VHOST_MSG_RESULT_ERR;
2456 VHOST_LOG_CONFIG(ERR, "(%s) invalid file descriptor for slave channel (%d)\n",
2458 return RTE_VHOST_MSG_RESULT_ERR;
2461 if (dev->slave_req_fd >= 0)
2462 close(dev->slave_req_fd);
2464 dev->slave_req_fd = fd;
2466 return RTE_VHOST_MSG_RESULT_OK;
2470 is_vring_iotlb_split(struct vhost_virtqueue *vq, struct vhost_iotlb_msg *imsg)
2472 struct vhost_vring_addr *ra;
2473 uint64_t start, end, len;
2476 end = start + imsg->size;
2478 ra = &vq->ring_addrs;
2479 len = sizeof(struct vring_desc) * vq->size;
2480 if (ra->desc_user_addr < end && (ra->desc_user_addr + len) > start)
2483 len = sizeof(struct vring_avail) + sizeof(uint16_t) * vq->size;
2484 if (ra->avail_user_addr < end && (ra->avail_user_addr + len) > start)
2487 len = sizeof(struct vring_used) +
2488 sizeof(struct vring_used_elem) * vq->size;
2489 if (ra->used_user_addr < end && (ra->used_user_addr + len) > start)
2492 if (ra->flags & (1 << VHOST_VRING_F_LOG)) {
2493 len = sizeof(uint64_t);
2494 if (ra->log_guest_addr < end &&
2495 (ra->log_guest_addr + len) > start)
2503 is_vring_iotlb_packed(struct vhost_virtqueue *vq, struct vhost_iotlb_msg *imsg)
2505 struct vhost_vring_addr *ra;
2506 uint64_t start, end, len;
2509 end = start + imsg->size;
2511 ra = &vq->ring_addrs;
2512 len = sizeof(struct vring_packed_desc) * vq->size;
2513 if (ra->desc_user_addr < end && (ra->desc_user_addr + len) > start)
2516 len = sizeof(struct vring_packed_desc_event);
2517 if (ra->avail_user_addr < end && (ra->avail_user_addr + len) > start)
2520 len = sizeof(struct vring_packed_desc_event);
2521 if (ra->used_user_addr < end && (ra->used_user_addr + len) > start)
2524 if (ra->flags & (1 << VHOST_VRING_F_LOG)) {
2525 len = sizeof(uint64_t);
2526 if (ra->log_guest_addr < end &&
2527 (ra->log_guest_addr + len) > start)
2534 static int is_vring_iotlb(struct virtio_net *dev,
2535 struct vhost_virtqueue *vq,
2536 struct vhost_iotlb_msg *imsg)
2538 if (vq_is_packed(dev))
2539 return is_vring_iotlb_packed(vq, imsg);
2541 return is_vring_iotlb_split(vq, imsg);
2545 vhost_user_iotlb_msg(struct virtio_net **pdev,
2546 struct vhu_msg_context *ctx,
2547 int main_fd __rte_unused)
2549 struct virtio_net *dev = *pdev;
2550 struct vhost_iotlb_msg *imsg = &ctx->msg.payload.iotlb;
2554 if (validate_msg_fds(dev, ctx, 0) != 0)
2555 return RTE_VHOST_MSG_RESULT_ERR;
2557 switch (imsg->type) {
2558 case VHOST_IOTLB_UPDATE:
2560 vva = qva_to_vva(dev, imsg->uaddr, &len);
2562 return RTE_VHOST_MSG_RESULT_ERR;
2564 for (i = 0; i < dev->nr_vring; i++) {
2565 struct vhost_virtqueue *vq = dev->virtqueue[i];
2570 vhost_user_iotlb_cache_insert(dev, vq, imsg->iova, vva,
2573 if (is_vring_iotlb(dev, vq, imsg)) {
2574 rte_spinlock_lock(&vq->access_lock);
2575 *pdev = dev = translate_ring_addresses(dev, i);
2576 rte_spinlock_unlock(&vq->access_lock);
2580 case VHOST_IOTLB_INVALIDATE:
2581 for (i = 0; i < dev->nr_vring; i++) {
2582 struct vhost_virtqueue *vq = dev->virtqueue[i];
2587 vhost_user_iotlb_cache_remove(vq, imsg->iova,
2590 if (is_vring_iotlb(dev, vq, imsg)) {
2591 rte_spinlock_lock(&vq->access_lock);
2592 vring_invalidate(dev, vq);
2593 rte_spinlock_unlock(&vq->access_lock);
2598 VHOST_LOG_CONFIG(ERR, "(%s) invalid IOTLB message type (%d)\n",
2599 dev->ifname, imsg->type);
2600 return RTE_VHOST_MSG_RESULT_ERR;
2603 return RTE_VHOST_MSG_RESULT_OK;
2607 vhost_user_set_postcopy_advise(struct virtio_net **pdev,
2608 struct vhu_msg_context *ctx,
2609 int main_fd __rte_unused)
2611 struct virtio_net *dev = *pdev;
2612 #ifdef RTE_LIBRTE_VHOST_POSTCOPY
2613 struct uffdio_api api_struct;
2615 if (validate_msg_fds(dev, ctx, 0) != 0)
2616 return RTE_VHOST_MSG_RESULT_ERR;
2618 dev->postcopy_ufd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK);
2620 if (dev->postcopy_ufd == -1) {
2621 VHOST_LOG_CONFIG(ERR, "(%s) userfaultfd not available: %s\n",
2622 dev->ifname, strerror(errno));
2623 return RTE_VHOST_MSG_RESULT_ERR;
2625 api_struct.api = UFFD_API;
2626 api_struct.features = 0;
2627 if (ioctl(dev->postcopy_ufd, UFFDIO_API, &api_struct)) {
2628 VHOST_LOG_CONFIG(ERR, "(%s) UFFDIO_API ioctl failure: %s\n",
2629 dev->ifname, strerror(errno));
2630 close(dev->postcopy_ufd);
2631 dev->postcopy_ufd = -1;
2632 return RTE_VHOST_MSG_RESULT_ERR;
2634 ctx->fds[0] = dev->postcopy_ufd;
2637 return RTE_VHOST_MSG_RESULT_REPLY;
2639 dev->postcopy_ufd = -1;
2642 return RTE_VHOST_MSG_RESULT_ERR;
2647 vhost_user_set_postcopy_listen(struct virtio_net **pdev,
2648 struct vhu_msg_context *ctx __rte_unused,
2649 int main_fd __rte_unused)
2651 struct virtio_net *dev = *pdev;
2653 if (validate_msg_fds(dev, ctx, 0) != 0)
2654 return RTE_VHOST_MSG_RESULT_ERR;
2656 if (dev->mem && dev->mem->nregions) {
2657 VHOST_LOG_CONFIG(ERR, "(%s) regions already registered at postcopy-listen\n",
2659 return RTE_VHOST_MSG_RESULT_ERR;
2661 dev->postcopy_listening = 1;
2663 return RTE_VHOST_MSG_RESULT_OK;
2667 vhost_user_postcopy_end(struct virtio_net **pdev,
2668 struct vhu_msg_context *ctx,
2669 int main_fd __rte_unused)
2671 struct virtio_net *dev = *pdev;
2673 if (validate_msg_fds(dev, ctx, 0) != 0)
2674 return RTE_VHOST_MSG_RESULT_ERR;
2676 dev->postcopy_listening = 0;
2677 if (dev->postcopy_ufd >= 0) {
2678 close(dev->postcopy_ufd);
2679 dev->postcopy_ufd = -1;
2682 ctx->msg.payload.u64 = 0;
2683 ctx->msg.size = sizeof(ctx->msg.payload.u64);
2686 return RTE_VHOST_MSG_RESULT_REPLY;
2690 vhost_user_get_status(struct virtio_net **pdev,
2691 struct vhu_msg_context *ctx,
2692 int main_fd __rte_unused)
2694 struct virtio_net *dev = *pdev;
2696 if (validate_msg_fds(dev, ctx, 0) != 0)
2697 return RTE_VHOST_MSG_RESULT_ERR;
2699 ctx->msg.payload.u64 = dev->status;
2700 ctx->msg.size = sizeof(ctx->msg.payload.u64);
2703 return RTE_VHOST_MSG_RESULT_REPLY;
2707 vhost_user_set_status(struct virtio_net **pdev,
2708 struct vhu_msg_context *ctx,
2709 int main_fd __rte_unused)
2711 struct virtio_net *dev = *pdev;
2713 if (validate_msg_fds(dev, ctx, 0) != 0)
2714 return RTE_VHOST_MSG_RESULT_ERR;
2716 /* As per Virtio specification, the device status is 8bits long */
2717 if (ctx->msg.payload.u64 > UINT8_MAX) {
2718 VHOST_LOG_CONFIG(ERR, "(%s) invalid VHOST_USER_SET_STATUS payload 0x%" PRIx64 "\n",
2719 dev->ifname, ctx->msg.payload.u64);
2720 return RTE_VHOST_MSG_RESULT_ERR;
2723 dev->status = ctx->msg.payload.u64;
2725 if ((dev->status & VIRTIO_DEVICE_STATUS_FEATURES_OK) &&
2726 (dev->flags & VIRTIO_DEV_FEATURES_FAILED)) {
2727 VHOST_LOG_CONFIG(ERR,
2728 "(%s) FEATURES_OK bit is set but feature negotiation failed\n",
2731 * Clear the bit to let the driver know about the feature
2732 * negotiation failure
2734 dev->status &= ~VIRTIO_DEVICE_STATUS_FEATURES_OK;
2737 VHOST_LOG_CONFIG(INFO, "(%s) new device status(0x%08x):\n", dev->ifname,
2739 VHOST_LOG_CONFIG(INFO, "(%s)\t-RESET: %u\n", dev->ifname,
2740 (dev->status == VIRTIO_DEVICE_STATUS_RESET));
2741 VHOST_LOG_CONFIG(INFO, "(%s)\t-ACKNOWLEDGE: %u\n", dev->ifname,
2742 !!(dev->status & VIRTIO_DEVICE_STATUS_ACK));
2743 VHOST_LOG_CONFIG(INFO, "(%s)\t-DRIVER: %u\n", dev->ifname,
2744 !!(dev->status & VIRTIO_DEVICE_STATUS_DRIVER));
2745 VHOST_LOG_CONFIG(INFO, "(%s)\t-FEATURES_OK: %u\n", dev->ifname,
2746 !!(dev->status & VIRTIO_DEVICE_STATUS_FEATURES_OK));
2747 VHOST_LOG_CONFIG(INFO, "(%s)\t-DRIVER_OK: %u\n", dev->ifname,
2748 !!(dev->status & VIRTIO_DEVICE_STATUS_DRIVER_OK));
2749 VHOST_LOG_CONFIG(INFO, "(%s)\t-DEVICE_NEED_RESET: %u\n", dev->ifname,
2750 !!(dev->status & VIRTIO_DEVICE_STATUS_DEV_NEED_RESET));
2751 VHOST_LOG_CONFIG(INFO, "(%s)\t-FAILED: %u\n", dev->ifname,
2752 !!(dev->status & VIRTIO_DEVICE_STATUS_FAILED));
2754 return RTE_VHOST_MSG_RESULT_OK;
2757 typedef int (*vhost_message_handler_t)(struct virtio_net **pdev,
2758 struct vhu_msg_context *ctx,
2761 static vhost_message_handler_t vhost_message_handlers[VHOST_USER_MAX] = {
2762 [VHOST_USER_NONE] = NULL,
2763 [VHOST_USER_GET_FEATURES] = vhost_user_get_features,
2764 [VHOST_USER_SET_FEATURES] = vhost_user_set_features,
2765 [VHOST_USER_SET_OWNER] = vhost_user_set_owner,
2766 [VHOST_USER_RESET_OWNER] = vhost_user_reset_owner,
2767 [VHOST_USER_SET_MEM_TABLE] = vhost_user_set_mem_table,
2768 [VHOST_USER_SET_LOG_BASE] = vhost_user_set_log_base,
2769 [VHOST_USER_SET_LOG_FD] = vhost_user_set_log_fd,
2770 [VHOST_USER_SET_VRING_NUM] = vhost_user_set_vring_num,
2771 [VHOST_USER_SET_VRING_ADDR] = vhost_user_set_vring_addr,
2772 [VHOST_USER_SET_VRING_BASE] = vhost_user_set_vring_base,
2773 [VHOST_USER_GET_VRING_BASE] = vhost_user_get_vring_base,
2774 [VHOST_USER_SET_VRING_KICK] = vhost_user_set_vring_kick,
2775 [VHOST_USER_SET_VRING_CALL] = vhost_user_set_vring_call,
2776 [VHOST_USER_SET_VRING_ERR] = vhost_user_set_vring_err,
2777 [VHOST_USER_GET_PROTOCOL_FEATURES] = vhost_user_get_protocol_features,
2778 [VHOST_USER_SET_PROTOCOL_FEATURES] = vhost_user_set_protocol_features,
2779 [VHOST_USER_GET_QUEUE_NUM] = vhost_user_get_queue_num,
2780 [VHOST_USER_SET_VRING_ENABLE] = vhost_user_set_vring_enable,
2781 [VHOST_USER_SEND_RARP] = vhost_user_send_rarp,
2782 [VHOST_USER_NET_SET_MTU] = vhost_user_net_set_mtu,
2783 [VHOST_USER_SET_SLAVE_REQ_FD] = vhost_user_set_req_fd,
2784 [VHOST_USER_IOTLB_MSG] = vhost_user_iotlb_msg,
2785 [VHOST_USER_POSTCOPY_ADVISE] = vhost_user_set_postcopy_advise,
2786 [VHOST_USER_POSTCOPY_LISTEN] = vhost_user_set_postcopy_listen,
2787 [VHOST_USER_POSTCOPY_END] = vhost_user_postcopy_end,
2788 [VHOST_USER_GET_INFLIGHT_FD] = vhost_user_get_inflight_fd,
2789 [VHOST_USER_SET_INFLIGHT_FD] = vhost_user_set_inflight_fd,
2790 [VHOST_USER_SET_STATUS] = vhost_user_set_status,
2791 [VHOST_USER_GET_STATUS] = vhost_user_get_status,
2794 /* return bytes# of read on success or negative val on failure. */
2796 read_vhost_message(struct virtio_net *dev, int sockfd, struct vhu_msg_context *ctx)
2800 ret = read_fd_message(dev->ifname, sockfd, (char *)&ctx->msg, VHOST_USER_HDR_SIZE,
2801 ctx->fds, VHOST_MEMORY_MAX_NREGIONS, &ctx->fd_num);
2804 } else if (ret != VHOST_USER_HDR_SIZE) {
2805 VHOST_LOG_CONFIG(ERR, "(%s) Unexpected header size read\n", dev->ifname);
2810 if (ctx->msg.size) {
2811 if (ctx->msg.size > sizeof(ctx->msg.payload)) {
2812 VHOST_LOG_CONFIG(ERR, "(%s) invalid msg size: %d\n",
2813 dev->ifname, ctx->msg.size);
2816 ret = read(sockfd, &ctx->msg.payload, ctx->msg.size);
2819 if (ret != (int)ctx->msg.size) {
2820 VHOST_LOG_CONFIG(ERR, "(%s) read control message failed\n", dev->ifname);
2829 send_vhost_message(struct virtio_net *dev, int sockfd, struct vhu_msg_context *ctx)
2834 return send_fd_message(dev->ifname, sockfd, (char *)&ctx->msg,
2835 VHOST_USER_HDR_SIZE + ctx->msg.size, ctx->fds, ctx->fd_num);
2839 send_vhost_reply(struct virtio_net *dev, int sockfd, struct vhu_msg_context *ctx)
2844 ctx->msg.flags &= ~VHOST_USER_VERSION_MASK;
2845 ctx->msg.flags &= ~VHOST_USER_NEED_REPLY;
2846 ctx->msg.flags |= VHOST_USER_VERSION;
2847 ctx->msg.flags |= VHOST_USER_REPLY_MASK;
2849 return send_vhost_message(dev, sockfd, ctx);
2853 send_vhost_slave_message(struct virtio_net *dev,
2854 struct vhu_msg_context *ctx)
2858 if (ctx->msg.flags & VHOST_USER_NEED_REPLY)
2859 rte_spinlock_lock(&dev->slave_req_lock);
2861 ret = send_vhost_message(dev, dev->slave_req_fd, ctx);
2862 if (ret < 0 && (ctx->msg.flags & VHOST_USER_NEED_REPLY))
2863 rte_spinlock_unlock(&dev->slave_req_lock);
2869 * Allocate a queue pair if it hasn't been allocated yet
2872 vhost_user_check_and_alloc_queue_pair(struct virtio_net *dev,
2873 struct vhu_msg_context *ctx)
2877 switch (ctx->msg.request.master) {
2878 case VHOST_USER_SET_VRING_KICK:
2879 case VHOST_USER_SET_VRING_CALL:
2880 case VHOST_USER_SET_VRING_ERR:
2881 vring_idx = ctx->msg.payload.u64 & VHOST_USER_VRING_IDX_MASK;
2883 case VHOST_USER_SET_VRING_NUM:
2884 case VHOST_USER_SET_VRING_BASE:
2885 case VHOST_USER_GET_VRING_BASE:
2886 case VHOST_USER_SET_VRING_ENABLE:
2887 vring_idx = ctx->msg.payload.state.index;
2889 case VHOST_USER_SET_VRING_ADDR:
2890 vring_idx = ctx->msg.payload.addr.index;
2892 case VHOST_USER_SET_INFLIGHT_FD:
2893 vring_idx = ctx->msg.payload.inflight.num_queues - 1;
2899 if (vring_idx >= VHOST_MAX_VRING) {
2900 VHOST_LOG_CONFIG(ERR, "(%s) invalid vring index: %u\n", dev->ifname, vring_idx);
2904 if (dev->virtqueue[vring_idx])
2907 return alloc_vring_queue(dev, vring_idx);
2911 vhost_user_lock_all_queue_pairs(struct virtio_net *dev)
2914 unsigned int vq_num = 0;
2916 while (vq_num < dev->nr_vring) {
2917 struct vhost_virtqueue *vq = dev->virtqueue[i];
2920 rte_spinlock_lock(&vq->access_lock);
2928 vhost_user_unlock_all_queue_pairs(struct virtio_net *dev)
2931 unsigned int vq_num = 0;
2933 while (vq_num < dev->nr_vring) {
2934 struct vhost_virtqueue *vq = dev->virtqueue[i];
2937 rte_spinlock_unlock(&vq->access_lock);
2945 vhost_user_msg_handler(int vid, int fd)
2947 struct virtio_net *dev;
2948 struct vhu_msg_context ctx;
2949 struct rte_vdpa_device *vdpa_dev;
2951 int unlock_required = 0;
2956 dev = get_device(vid);
2960 if (!dev->notify_ops) {
2961 dev->notify_ops = vhost_driver_callback_get(dev->ifname);
2962 if (!dev->notify_ops) {
2963 VHOST_LOG_CONFIG(ERR, "(%s) failed to get callback ops for driver\n",
2969 ret = read_vhost_message(dev, fd, &ctx);
2972 VHOST_LOG_CONFIG(ERR, "(%s) vhost read message failed\n", dev->ifname);
2974 VHOST_LOG_CONFIG(INFO, "(%s) vhost peer closed\n", dev->ifname);
2980 request = ctx.msg.request.master;
2981 if (request > VHOST_USER_NONE && request < VHOST_USER_MAX &&
2982 vhost_message_str[request]) {
2983 if (request != VHOST_USER_IOTLB_MSG)
2984 VHOST_LOG_CONFIG(INFO, "(%s) read message %s\n",
2985 dev->ifname, vhost_message_str[request]);
2987 VHOST_LOG_CONFIG(DEBUG, "(%s) read message %s\n",
2988 dev->ifname, vhost_message_str[request]);
2990 VHOST_LOG_CONFIG(DEBUG, "(%s) external request %d\n", dev->ifname, request);
2993 ret = vhost_user_check_and_alloc_queue_pair(dev, &ctx);
2995 VHOST_LOG_CONFIG(ERR, "(%s) failed to alloc queue\n", dev->ifname);
3000 * Note: we don't lock all queues on VHOST_USER_GET_VRING_BASE
3001 * and VHOST_USER_RESET_OWNER, since it is sent when virtio stops
3002 * and device is destroyed. destroy_device waits for queues to be
3003 * inactive, so it is safe. Otherwise taking the access_lock
3004 * would cause a dead lock.
3007 case VHOST_USER_SET_FEATURES:
3008 case VHOST_USER_SET_PROTOCOL_FEATURES:
3009 case VHOST_USER_SET_OWNER:
3010 case VHOST_USER_SET_MEM_TABLE:
3011 case VHOST_USER_SET_LOG_BASE:
3012 case VHOST_USER_SET_LOG_FD:
3013 case VHOST_USER_SET_VRING_NUM:
3014 case VHOST_USER_SET_VRING_ADDR:
3015 case VHOST_USER_SET_VRING_BASE:
3016 case VHOST_USER_SET_VRING_KICK:
3017 case VHOST_USER_SET_VRING_CALL:
3018 case VHOST_USER_SET_VRING_ERR:
3019 case VHOST_USER_SET_VRING_ENABLE:
3020 case VHOST_USER_SEND_RARP:
3021 case VHOST_USER_NET_SET_MTU:
3022 case VHOST_USER_SET_SLAVE_REQ_FD:
3023 if (!(dev->flags & VIRTIO_DEV_VDPA_CONFIGURED)) {
3024 vhost_user_lock_all_queue_pairs(dev);
3025 unlock_required = 1;
3034 if (dev->extern_ops.pre_msg_handle) {
3035 RTE_BUILD_BUG_ON(offsetof(struct vhu_msg_context, msg) != 0);
3036 ret = (*dev->extern_ops.pre_msg_handle)(dev->vid, &ctx);
3038 case RTE_VHOST_MSG_RESULT_REPLY:
3039 send_vhost_reply(dev, fd, &ctx);
3041 case RTE_VHOST_MSG_RESULT_ERR:
3042 case RTE_VHOST_MSG_RESULT_OK:
3044 goto skip_to_post_handle;
3045 case RTE_VHOST_MSG_RESULT_NOT_HANDLED:
3051 if (request > VHOST_USER_NONE && request < VHOST_USER_MAX) {
3052 if (!vhost_message_handlers[request])
3053 goto skip_to_post_handle;
3054 ret = vhost_message_handlers[request](&dev, &ctx, fd);
3057 case RTE_VHOST_MSG_RESULT_ERR:
3058 VHOST_LOG_CONFIG(ERR, "(%s) processing %s failed.\n",
3059 dev->ifname, vhost_message_str[request]);
3062 case RTE_VHOST_MSG_RESULT_OK:
3063 VHOST_LOG_CONFIG(DEBUG, "(%s) processing %s succeeded.\n",
3064 dev->ifname, vhost_message_str[request]);
3067 case RTE_VHOST_MSG_RESULT_REPLY:
3068 VHOST_LOG_CONFIG(DEBUG, "(%s) processing %s succeeded and needs reply.\n",
3069 dev->ifname, vhost_message_str[request]);
3070 send_vhost_reply(dev, fd, &ctx);
3078 skip_to_post_handle:
3079 if (ret != RTE_VHOST_MSG_RESULT_ERR &&
3080 dev->extern_ops.post_msg_handle) {
3081 RTE_BUILD_BUG_ON(offsetof(struct vhu_msg_context, msg) != 0);
3082 ret = (*dev->extern_ops.post_msg_handle)(dev->vid, &ctx);
3084 case RTE_VHOST_MSG_RESULT_REPLY:
3085 send_vhost_reply(dev, fd, &ctx);
3087 case RTE_VHOST_MSG_RESULT_ERR:
3088 case RTE_VHOST_MSG_RESULT_OK:
3090 case RTE_VHOST_MSG_RESULT_NOT_HANDLED:
3096 /* If message was not handled at this stage, treat it as an error */
3098 VHOST_LOG_CONFIG(ERR, "(%s) vhost message (req: %d) was not handled.\n",
3099 dev->ifname, request);
3100 close_msg_fds(&ctx);
3101 ret = RTE_VHOST_MSG_RESULT_ERR;
3105 * If the request required a reply that was already sent,
3106 * this optional reply-ack won't be sent as the
3107 * VHOST_USER_NEED_REPLY was cleared in send_vhost_reply().
3109 if (ctx.msg.flags & VHOST_USER_NEED_REPLY) {
3110 ctx.msg.payload.u64 = ret == RTE_VHOST_MSG_RESULT_ERR;
3111 ctx.msg.size = sizeof(ctx.msg.payload.u64);
3113 send_vhost_reply(dev, fd, &ctx);
3114 } else if (ret == RTE_VHOST_MSG_RESULT_ERR) {
3115 VHOST_LOG_CONFIG(ERR, "(%s) vhost message handling failed.\n", dev->ifname);
3119 for (i = 0; i < dev->nr_vring; i++) {
3120 struct vhost_virtqueue *vq = dev->virtqueue[i];
3121 bool cur_ready = vq_is_ready(dev, vq);
3123 if (cur_ready != (vq && vq->ready)) {
3124 vq->ready = cur_ready;
3125 vhost_user_notify_queue_state(dev, i, cur_ready);
3129 if (unlock_required)
3130 vhost_user_unlock_all_queue_pairs(dev);
3132 if (!virtio_is_ready(dev))
3136 * Virtio is now ready. If not done already, it is time
3137 * to notify the application it can process the rings and
3138 * configure the vDPA device if present.
3141 if (!(dev->flags & VIRTIO_DEV_RUNNING)) {
3142 if (dev->notify_ops->new_device(dev->vid) == 0)
3143 dev->flags |= VIRTIO_DEV_RUNNING;
3146 vdpa_dev = dev->vdpa_dev;
3150 if (!(dev->flags & VIRTIO_DEV_VDPA_CONFIGURED)) {
3151 if (vdpa_dev->ops->dev_conf(dev->vid))
3152 VHOST_LOG_CONFIG(ERR, "(%s) failed to configure vDPA device\n",
3155 dev->flags |= VIRTIO_DEV_VDPA_CONFIGURED;
3162 static int process_slave_message_reply(struct virtio_net *dev,
3163 const struct vhu_msg_context *ctx)
3165 struct vhu_msg_context msg_reply;
3168 if ((ctx->msg.flags & VHOST_USER_NEED_REPLY) == 0)
3171 ret = read_vhost_message(dev, dev->slave_req_fd, &msg_reply);
3174 VHOST_LOG_CONFIG(ERR, "(%s) vhost read slave message reply failed\n",
3177 VHOST_LOG_CONFIG(INFO, "(%s) vhost peer closed\n", dev->ifname);
3183 if (msg_reply.msg.request.slave != ctx->msg.request.slave) {
3184 VHOST_LOG_CONFIG(ERR, "(%s) received unexpected msg type (%u), expected %u\n",
3185 dev->ifname, msg_reply.msg.request.slave, ctx->msg.request.slave);
3190 ret = msg_reply.msg.payload.u64 ? -1 : 0;
3193 rte_spinlock_unlock(&dev->slave_req_lock);
3198 vhost_user_iotlb_miss(struct virtio_net *dev, uint64_t iova, uint8_t perm)
3201 struct vhu_msg_context ctx = {
3203 .request.slave = VHOST_USER_SLAVE_IOTLB_MSG,
3204 .flags = VHOST_USER_VERSION,
3205 .size = sizeof(ctx.msg.payload.iotlb),
3209 .type = VHOST_IOTLB_MISS,
3214 ret = send_vhost_message(dev, dev->slave_req_fd, &ctx);
3216 VHOST_LOG_CONFIG(ERR, "(%s) failed to send IOTLB miss message (%d)\n",
3225 vhost_user_slave_config_change(struct virtio_net *dev, bool need_reply)
3228 struct vhu_msg_context ctx = {
3230 .request.slave = VHOST_USER_SLAVE_CONFIG_CHANGE_MSG,
3231 .flags = VHOST_USER_VERSION,
3237 ctx.msg.flags |= VHOST_USER_NEED_REPLY;
3239 ret = send_vhost_slave_message(dev, &ctx);
3241 VHOST_LOG_CONFIG(ERR, "(%s) failed to send config change (%d)\n",
3246 return process_slave_message_reply(dev, &ctx);
3250 rte_vhost_slave_config_change(int vid, bool need_reply)
3252 struct virtio_net *dev;
3254 dev = get_device(vid);
3258 return vhost_user_slave_config_change(dev, need_reply);
3261 static int vhost_user_slave_set_vring_host_notifier(struct virtio_net *dev,
3267 struct vhu_msg_context ctx = {
3269 .request.slave = VHOST_USER_SLAVE_VRING_HOST_NOTIFIER_MSG,
3270 .flags = VHOST_USER_VERSION | VHOST_USER_NEED_REPLY,
3271 .size = sizeof(ctx.msg.payload.area),
3273 .u64 = index & VHOST_USER_VRING_IDX_MASK,
3281 ctx.msg.payload.area.u64 |= VHOST_USER_VRING_NOFD_MASK;
3287 ret = send_vhost_slave_message(dev, &ctx);
3289 VHOST_LOG_CONFIG(ERR, "(%s) failed to set host notifier (%d)\n",
3294 return process_slave_message_reply(dev, &ctx);
3297 int rte_vhost_host_notifier_ctrl(int vid, uint16_t qid, bool enable)
3299 struct virtio_net *dev;
3300 struct rte_vdpa_device *vdpa_dev;
3301 int vfio_device_fd, ret = 0;
3302 uint64_t offset, size;
3303 unsigned int i, q_start, q_last;
3305 dev = get_device(vid);
3309 vdpa_dev = dev->vdpa_dev;
3310 if (vdpa_dev == NULL)
3313 if (!(dev->features & (1ULL << VIRTIO_F_VERSION_1)) ||
3314 !(dev->features & (1ULL << VHOST_USER_F_PROTOCOL_FEATURES)) ||
3315 !(dev->protocol_features &
3316 (1ULL << VHOST_USER_PROTOCOL_F_SLAVE_REQ)) ||
3317 !(dev->protocol_features &
3318 (1ULL << VHOST_USER_PROTOCOL_F_SLAVE_SEND_FD)) ||
3319 !(dev->protocol_features &
3320 (1ULL << VHOST_USER_PROTOCOL_F_HOST_NOTIFIER)))
3323 if (qid == RTE_VHOST_QUEUE_ALL) {
3325 q_last = dev->nr_vring - 1;
3327 if (qid >= dev->nr_vring)
3333 RTE_FUNC_PTR_OR_ERR_RET(vdpa_dev->ops->get_vfio_device_fd, -ENOTSUP);
3334 RTE_FUNC_PTR_OR_ERR_RET(vdpa_dev->ops->get_notify_area, -ENOTSUP);
3336 vfio_device_fd = vdpa_dev->ops->get_vfio_device_fd(vid);
3337 if (vfio_device_fd < 0)
3341 for (i = q_start; i <= q_last; i++) {
3342 if (vdpa_dev->ops->get_notify_area(vid, i, &offset,
3348 if (vhost_user_slave_set_vring_host_notifier(dev, i,
3349 vfio_device_fd, offset, size) < 0) {
3356 for (i = q_start; i <= q_last; i++) {
3357 vhost_user_slave_set_vring_host_notifier(dev, i, -1,