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, struct rte_vhost_mem_region *region, bool do_map)
149 host_iova = rte_mem_virt2iova((void *)(uintptr_t)region->host_user_addr);
151 /* Add mapped region into the default container of DPDK. */
152 ret = rte_vfio_container_dma_map(RTE_VFIO_DEFAULT_CONTAINER_FD,
153 region->host_user_addr,
158 * DMA device may bind with kernel driver, in this case,
159 * we don't need to program IOMMU manually. However, if no
160 * device is bound with vfio/uio in DPDK, and vfio kernel
161 * module is loaded, the API will still be called and return
162 * with ENODEV/ENOSUP.
164 * DPDK vfio only returns ENODEV/ENOSUP in very similar
165 * situations(vfio either unsupported, or supported
166 * but no devices found). Either way, no mappings could be
167 * performed. We treat it as normal case in async path.
169 if (rte_errno == ENODEV || rte_errno == ENOTSUP)
172 VHOST_LOG_CONFIG(ERR, "(%s) DMA engine map failed\n", dev->ifname);
173 /* DMA mapping errors won't stop VHST_USER_SET_MEM_TABLE. */
178 /* Remove mapped region from the default container of DPDK. */
179 ret = rte_vfio_container_dma_unmap(RTE_VFIO_DEFAULT_CONTAINER_FD,
180 region->host_user_addr,
184 /* like DMA map, ignore the kernel driver case when unmap. */
185 if (rte_errno == EINVAL)
188 VHOST_LOG_CONFIG(ERR, "(%s) DMA engine unmap failed\n", dev->ifname);
197 free_mem_region(struct virtio_net *dev)
200 struct rte_vhost_mem_region *reg;
202 if (!dev || !dev->mem)
205 for (i = 0; i < dev->mem->nregions; i++) {
206 reg = &dev->mem->regions[i];
207 if (reg->host_user_addr) {
208 if (dev->async_copy && rte_vfio_is_enabled("vfio"))
209 async_dma_map(dev, reg, false);
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 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, "(%s) cannot realloc guest_pages\n", dev->ifname);
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 +
1012 last_page->size += size;
1017 page = &dev->guest_pages[dev->nr_guest_pages++];
1018 page->guest_phys_addr = guest_phys_addr;
1019 page->host_iova = host_iova;
1026 add_guest_pages(struct virtio_net *dev, struct rte_vhost_mem_region *reg,
1029 uint64_t reg_size = reg->size;
1030 uint64_t host_user_addr = reg->host_user_addr;
1031 uint64_t guest_phys_addr = reg->guest_phys_addr;
1035 host_iova = rte_mem_virt2iova((void *)(uintptr_t)host_user_addr);
1036 size = page_size - (guest_phys_addr & (page_size - 1));
1037 size = RTE_MIN(size, reg_size);
1039 if (add_one_guest_page(dev, guest_phys_addr, host_iova, size) < 0)
1042 host_user_addr += size;
1043 guest_phys_addr += size;
1046 while (reg_size > 0) {
1047 size = RTE_MIN(reg_size, page_size);
1048 host_iova = rte_mem_virt2iova((void *)(uintptr_t)
1050 if (add_one_guest_page(dev, guest_phys_addr, host_iova,
1054 host_user_addr += size;
1055 guest_phys_addr += size;
1059 /* sort guest page array if over binary search threshold */
1060 if (dev->nr_guest_pages >= VHOST_BINARY_SEARCH_THRESH) {
1061 qsort((void *)dev->guest_pages, dev->nr_guest_pages,
1062 sizeof(struct guest_page), guest_page_addrcmp);
1068 #ifdef RTE_LIBRTE_VHOST_DEBUG
1069 /* TODO: enable it only in debug mode? */
1071 dump_guest_pages(struct virtio_net *dev)
1074 struct guest_page *page;
1076 for (i = 0; i < dev->nr_guest_pages; i++) {
1077 page = &dev->guest_pages[i];
1079 VHOST_LOG_CONFIG(INFO, "(%s) guest physical page region %u\n",
1081 VHOST_LOG_CONFIG(INFO, "(%s)\tguest_phys_addr: %" PRIx64 "\n",
1082 dev->ifname, page->guest_phys_addr);
1083 VHOST_LOG_CONFIG(INFO, "(%s)\thost_iova : %" PRIx64 "\n",
1084 dev->ifname, page->host_iova);
1085 VHOST_LOG_CONFIG(INFO, "(%s)\tsize : %" PRIx64 "\n",
1086 dev->ifname, page->size);
1090 #define dump_guest_pages(dev)
1094 vhost_memory_changed(struct VhostUserMemory *new,
1095 struct rte_vhost_memory *old)
1099 if (new->nregions != old->nregions)
1102 for (i = 0; i < new->nregions; ++i) {
1103 VhostUserMemoryRegion *new_r = &new->regions[i];
1104 struct rte_vhost_mem_region *old_r = &old->regions[i];
1106 if (new_r->guest_phys_addr != old_r->guest_phys_addr)
1108 if (new_r->memory_size != old_r->size)
1110 if (new_r->userspace_addr != old_r->guest_user_addr)
1117 #ifdef RTE_LIBRTE_VHOST_POSTCOPY
1119 vhost_user_postcopy_region_register(struct virtio_net *dev,
1120 struct rte_vhost_mem_region *reg)
1122 struct uffdio_register reg_struct;
1125 * Let's register all the mmapped area to ensure
1126 * alignment on page boundary.
1128 reg_struct.range.start = (uint64_t)(uintptr_t)reg->mmap_addr;
1129 reg_struct.range.len = reg->mmap_size;
1130 reg_struct.mode = UFFDIO_REGISTER_MODE_MISSING;
1132 if (ioctl(dev->postcopy_ufd, UFFDIO_REGISTER,
1134 VHOST_LOG_CONFIG(ERR, "(%s) failed to register ufd for region "
1135 "%" PRIx64 " - %" PRIx64 " (ufd = %d) %s\n",
1137 (uint64_t)reg_struct.range.start,
1138 (uint64_t)reg_struct.range.start +
1139 (uint64_t)reg_struct.range.len - 1,
1145 VHOST_LOG_CONFIG(INFO,
1146 "(%s)\t userfaultfd registered for range : %" PRIx64 " - %" PRIx64 "\n",
1148 (uint64_t)reg_struct.range.start,
1149 (uint64_t)reg_struct.range.start +
1150 (uint64_t)reg_struct.range.len - 1);
1156 vhost_user_postcopy_region_register(struct virtio_net *dev __rte_unused,
1157 struct rte_vhost_mem_region *reg __rte_unused)
1164 vhost_user_postcopy_register(struct virtio_net *dev, int main_fd,
1165 struct vhu_msg_context *ctx)
1167 struct VhostUserMemory *memory;
1168 struct rte_vhost_mem_region *reg;
1169 struct vhu_msg_context ack_ctx;
1172 if (!dev->postcopy_listening)
1176 * We haven't a better way right now than sharing
1177 * DPDK's virtual address with Qemu, so that Qemu can
1178 * retrieve the region offset when handling userfaults.
1180 memory = &ctx->msg.payload.memory;
1181 for (i = 0; i < memory->nregions; i++) {
1182 reg = &dev->mem->regions[i];
1183 memory->regions[i].userspace_addr = reg->host_user_addr;
1186 /* Send the addresses back to qemu */
1188 send_vhost_reply(dev, main_fd, ctx);
1190 /* Wait for qemu to acknowledge it got the addresses
1191 * we've got to wait before we're allowed to generate faults.
1193 if (read_vhost_message(dev, main_fd, &ack_ctx) <= 0) {
1194 VHOST_LOG_CONFIG(ERR, "(%s) failed to read qemu ack on postcopy set-mem-table\n",
1199 if (validate_msg_fds(dev, &ack_ctx, 0) != 0)
1202 if (ack_ctx.msg.request.master != VHOST_USER_SET_MEM_TABLE) {
1203 VHOST_LOG_CONFIG(ERR, "(%s) bad qemu ack on postcopy set-mem-table (%d)\n",
1204 dev->ifname, ack_ctx.msg.request.master);
1208 /* Now userfault register and we can use the memory */
1209 for (i = 0; i < memory->nregions; i++) {
1210 reg = &dev->mem->regions[i];
1211 if (vhost_user_postcopy_region_register(dev, reg) < 0)
1219 vhost_user_mmap_region(struct virtio_net *dev,
1220 struct rte_vhost_mem_region *region,
1221 uint64_t mmap_offset)
1229 /* Check for memory_size + mmap_offset overflow */
1230 if (mmap_offset >= -region->size) {
1231 VHOST_LOG_CONFIG(ERR, "(%s) mmap_offset (%#"PRIx64") and memory_size (%#"PRIx64") overflow\n",
1232 dev->ifname, mmap_offset, region->size);
1236 mmap_size = region->size + mmap_offset;
1238 /* mmap() without flag of MAP_ANONYMOUS, should be called with length
1239 * argument aligned with hugepagesz at older longterm version Linux,
1240 * like 2.6.32 and 3.2.72, or mmap() will fail with EINVAL.
1242 * To avoid failure, make sure in caller to keep length aligned.
1244 alignment = get_blk_size(region->fd);
1245 if (alignment == (uint64_t)-1) {
1246 VHOST_LOG_CONFIG(ERR, "(%s) couldn't get hugepage size through fstat\n",
1250 mmap_size = RTE_ALIGN_CEIL(mmap_size, alignment);
1251 if (mmap_size == 0) {
1253 * It could happen if initial mmap_size + alignment overflows
1254 * the sizeof uint64, which could happen if either mmap_size or
1255 * alignment value is wrong.
1257 * mmap() kernel implementation would return an error, but
1258 * better catch it before and provide useful info in the logs.
1260 VHOST_LOG_CONFIG(ERR, "(%s) mmap size (0x%" PRIx64 ") or alignment (0x%" PRIx64 ") is invalid\n",
1261 dev->ifname, region->size + mmap_offset, alignment);
1265 populate = dev->async_copy ? MAP_POPULATE : 0;
1266 mmap_addr = mmap(NULL, mmap_size, PROT_READ | PROT_WRITE,
1267 MAP_SHARED | populate, region->fd, 0);
1269 if (mmap_addr == MAP_FAILED) {
1270 VHOST_LOG_CONFIG(ERR, "(%s) mmap failed (%s).\n", dev->ifname, strerror(errno));
1274 region->mmap_addr = mmap_addr;
1275 region->mmap_size = mmap_size;
1276 region->host_user_addr = (uint64_t)(uintptr_t)mmap_addr + mmap_offset;
1278 if (dev->async_copy) {
1279 if (add_guest_pages(dev, region, alignment) < 0) {
1280 VHOST_LOG_CONFIG(ERR, "(%s) adding guest pages to region failed.\n",
1285 if (rte_vfio_is_enabled("vfio")) {
1286 ret = async_dma_map(dev, region, true);
1288 VHOST_LOG_CONFIG(ERR,
1289 "(%s) configure IOMMU for DMA engine failed\n",
1296 VHOST_LOG_CONFIG(INFO, "(%s) guest memory region size: 0x%" PRIx64 "\n",
1297 dev->ifname, region->size);
1298 VHOST_LOG_CONFIG(INFO, "(%s)\t guest physical addr: 0x%" PRIx64 "\n",
1299 dev->ifname, region->guest_phys_addr);
1300 VHOST_LOG_CONFIG(INFO, "(%s)\t guest virtual addr: 0x%" PRIx64 "\n",
1301 dev->ifname, region->guest_user_addr);
1302 VHOST_LOG_CONFIG(INFO, "(%s)\t host virtual addr: 0x%" PRIx64 "\n",
1303 dev->ifname, region->host_user_addr);
1304 VHOST_LOG_CONFIG(INFO, "(%s)\t mmap addr : 0x%" PRIx64 "\n",
1305 dev->ifname, (uint64_t)(uintptr_t)mmap_addr);
1306 VHOST_LOG_CONFIG(INFO, "(%s)\t mmap size : 0x%" PRIx64 "\n",
1307 dev->ifname, mmap_size);
1308 VHOST_LOG_CONFIG(INFO, "(%s)\t mmap align: 0x%" PRIx64 "\n",
1309 dev->ifname, alignment);
1310 VHOST_LOG_CONFIG(INFO, "(%s)\t mmap off : 0x%" PRIx64 "\n",
1311 dev->ifname, mmap_offset);
1317 vhost_user_set_mem_table(struct virtio_net **pdev,
1318 struct vhu_msg_context *ctx,
1321 struct virtio_net *dev = *pdev;
1322 struct VhostUserMemory *memory = &ctx->msg.payload.memory;
1323 struct rte_vhost_mem_region *reg;
1324 int numa_node = SOCKET_ID_ANY;
1325 uint64_t mmap_offset;
1327 bool async_notify = false;
1329 if (validate_msg_fds(dev, ctx, memory->nregions) != 0)
1330 return RTE_VHOST_MSG_RESULT_ERR;
1332 if (memory->nregions > VHOST_MEMORY_MAX_NREGIONS) {
1333 VHOST_LOG_CONFIG(ERR, "(%s) too many memory regions (%u)\n",
1334 dev->ifname, memory->nregions);
1338 if (dev->mem && !vhost_memory_changed(memory, dev->mem)) {
1339 VHOST_LOG_CONFIG(INFO, "(%s) memory regions not changed\n", dev->ifname);
1343 return RTE_VHOST_MSG_RESULT_OK;
1347 if (dev->flags & VIRTIO_DEV_VDPA_CONFIGURED) {
1348 struct rte_vdpa_device *vdpa_dev = dev->vdpa_dev;
1350 if (vdpa_dev && vdpa_dev->ops->dev_close)
1351 vdpa_dev->ops->dev_close(dev->vid);
1352 dev->flags &= ~VIRTIO_DEV_VDPA_CONFIGURED;
1355 /* notify the vhost application to stop DMA transfers */
1356 if (dev->async_copy && dev->notify_ops->vring_state_changed) {
1357 for (i = 0; i < dev->nr_vring; i++) {
1358 dev->notify_ops->vring_state_changed(dev->vid,
1361 async_notify = true;
1364 free_mem_region(dev);
1369 /* Flush IOTLB cache as previous HVAs are now invalid */
1370 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1371 for (i = 0; i < dev->nr_vring; i++)
1372 vhost_user_iotlb_flush_all(dev->virtqueue[i]);
1375 * If VQ 0 has already been allocated, try to allocate on the same
1376 * NUMA node. It can be reallocated later in numa_realloc().
1378 if (dev->nr_vring > 0)
1379 numa_node = dev->virtqueue[0]->numa_node;
1381 dev->nr_guest_pages = 0;
1382 if (dev->guest_pages == NULL) {
1383 dev->max_guest_pages = 8;
1384 dev->guest_pages = rte_zmalloc_socket(NULL,
1385 dev->max_guest_pages *
1386 sizeof(struct guest_page),
1387 RTE_CACHE_LINE_SIZE,
1389 if (dev->guest_pages == NULL) {
1390 VHOST_LOG_CONFIG(ERR,
1391 "(%s) failed to allocate memory for dev->guest_pages\n",
1397 dev->mem = rte_zmalloc_socket("vhost-mem-table", sizeof(struct rte_vhost_memory) +
1398 sizeof(struct rte_vhost_mem_region) * memory->nregions, 0, numa_node);
1399 if (dev->mem == NULL) {
1400 VHOST_LOG_CONFIG(ERR,
1401 "(%s) failed to allocate memory for dev->mem\n",
1403 goto free_guest_pages;
1406 for (i = 0; i < memory->nregions; i++) {
1407 reg = &dev->mem->regions[i];
1409 reg->guest_phys_addr = memory->regions[i].guest_phys_addr;
1410 reg->guest_user_addr = memory->regions[i].userspace_addr;
1411 reg->size = memory->regions[i].memory_size;
1412 reg->fd = ctx->fds[i];
1415 * Assign invalid file descriptor value to avoid double
1416 * closing on error path.
1420 mmap_offset = memory->regions[i].mmap_offset;
1422 if (vhost_user_mmap_region(dev, reg, mmap_offset) < 0) {
1423 VHOST_LOG_CONFIG(ERR, "(%s) failed to mmap region %u\n", dev->ifname, i);
1424 goto free_mem_table;
1427 dev->mem->nregions++;
1430 if (vhost_user_postcopy_register(dev, main_fd, ctx) < 0)
1431 goto free_mem_table;
1433 for (i = 0; i < dev->nr_vring; i++) {
1434 struct vhost_virtqueue *vq = dev->virtqueue[i];
1439 if (vq->desc || vq->avail || vq->used) {
1441 * If the memory table got updated, the ring addresses
1442 * need to be translated again as virtual addresses have
1445 vring_invalidate(dev, vq);
1447 dev = translate_ring_addresses(dev, i);
1450 goto free_mem_table;
1457 dump_guest_pages(dev);
1460 for (i = 0; i < dev->nr_vring; i++)
1461 dev->notify_ops->vring_state_changed(dev->vid, i, 1);
1464 return RTE_VHOST_MSG_RESULT_OK;
1467 free_mem_region(dev);
1472 rte_free(dev->guest_pages);
1473 dev->guest_pages = NULL;
1476 return RTE_VHOST_MSG_RESULT_ERR;
1480 vq_is_ready(struct virtio_net *dev, struct vhost_virtqueue *vq)
1487 if (vq_is_packed(dev))
1488 rings_ok = vq->desc_packed && vq->driver_event &&
1491 rings_ok = vq->desc && vq->avail && vq->used;
1494 vq->kickfd != VIRTIO_UNINITIALIZED_EVENTFD &&
1495 vq->callfd != VIRTIO_UNINITIALIZED_EVENTFD &&
1499 #define VIRTIO_BUILTIN_NUM_VQS_TO_BE_READY 2u
1502 virtio_is_ready(struct virtio_net *dev)
1504 struct vhost_virtqueue *vq;
1505 uint32_t i, nr_vring = dev->nr_vring;
1507 if (dev->flags & VIRTIO_DEV_READY)
1513 if (dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET) {
1514 nr_vring = VIRTIO_BUILTIN_NUM_VQS_TO_BE_READY;
1516 if (dev->nr_vring < nr_vring)
1520 for (i = 0; i < nr_vring; i++) {
1521 vq = dev->virtqueue[i];
1523 if (!vq_is_ready(dev, vq))
1527 /* If supported, ensure the frontend is really done with config */
1528 if (dev->protocol_features & (1ULL << VHOST_USER_PROTOCOL_F_STATUS))
1529 if (!(dev->status & VIRTIO_DEVICE_STATUS_DRIVER_OK))
1532 dev->flags |= VIRTIO_DEV_READY;
1534 if (!(dev->flags & VIRTIO_DEV_RUNNING))
1535 VHOST_LOG_CONFIG(INFO, "(%s) virtio is now ready for processing.\n", dev->ifname);
1540 inflight_mem_alloc(struct virtio_net *dev, const char *name, size_t size, int *fd)
1544 char fname[20] = "/tmp/memfd-XXXXXX";
1547 #ifdef MEMFD_SUPPORTED
1548 mfd = memfd_create(name, MFD_CLOEXEC);
1553 mfd = mkstemp(fname);
1555 VHOST_LOG_CONFIG(ERR, "(%s) failed to get inflight buffer fd\n",
1563 if (ftruncate(mfd, size) == -1) {
1564 VHOST_LOG_CONFIG(ERR, "(%s) failed to alloc inflight buffer\n", dev->ifname);
1569 ptr = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, mfd, 0);
1570 if (ptr == MAP_FAILED) {
1571 VHOST_LOG_CONFIG(ERR, "(%s) failed to mmap inflight buffer\n", dev->ifname);
1581 get_pervq_shm_size_split(uint16_t queue_size)
1583 return RTE_ALIGN_MUL_CEIL(sizeof(struct rte_vhost_inflight_desc_split) *
1584 queue_size + sizeof(uint64_t) +
1585 sizeof(uint16_t) * 4, INFLIGHT_ALIGNMENT);
1589 get_pervq_shm_size_packed(uint16_t queue_size)
1591 return RTE_ALIGN_MUL_CEIL(sizeof(struct rte_vhost_inflight_desc_packed)
1592 * queue_size + sizeof(uint64_t) +
1593 sizeof(uint16_t) * 6 + sizeof(uint8_t) * 9,
1594 INFLIGHT_ALIGNMENT);
1598 vhost_user_get_inflight_fd(struct virtio_net **pdev,
1599 struct vhu_msg_context *ctx,
1600 int main_fd __rte_unused)
1602 struct rte_vhost_inflight_info_packed *inflight_packed;
1603 uint64_t pervq_inflight_size, mmap_size;
1604 uint16_t num_queues, queue_size;
1605 struct virtio_net *dev = *pdev;
1607 int numa_node = SOCKET_ID_ANY;
1610 if (ctx->msg.size != sizeof(ctx->msg.payload.inflight)) {
1611 VHOST_LOG_CONFIG(ERR, "(%s) invalid get_inflight_fd message size is %d\n",
1612 dev->ifname, ctx->msg.size);
1613 return RTE_VHOST_MSG_RESULT_ERR;
1617 * If VQ 0 has already been allocated, try to allocate on the same
1618 * NUMA node. It can be reallocated later in numa_realloc().
1620 if (dev->nr_vring > 0)
1621 numa_node = dev->virtqueue[0]->numa_node;
1623 if (dev->inflight_info == NULL) {
1624 dev->inflight_info = rte_zmalloc_socket("inflight_info",
1625 sizeof(struct inflight_mem_info), 0, numa_node);
1626 if (!dev->inflight_info) {
1627 VHOST_LOG_CONFIG(ERR, "(%s) failed to alloc dev inflight area\n",
1629 return RTE_VHOST_MSG_RESULT_ERR;
1631 dev->inflight_info->fd = -1;
1634 num_queues = ctx->msg.payload.inflight.num_queues;
1635 queue_size = ctx->msg.payload.inflight.queue_size;
1637 VHOST_LOG_CONFIG(INFO, "(%s) get_inflight_fd num_queues: %u\n",
1638 dev->ifname, ctx->msg.payload.inflight.num_queues);
1639 VHOST_LOG_CONFIG(INFO, "(%s) get_inflight_fd queue_size: %u\n",
1640 dev->ifname, ctx->msg.payload.inflight.queue_size);
1642 if (vq_is_packed(dev))
1643 pervq_inflight_size = get_pervq_shm_size_packed(queue_size);
1645 pervq_inflight_size = get_pervq_shm_size_split(queue_size);
1647 mmap_size = num_queues * pervq_inflight_size;
1648 addr = inflight_mem_alloc(dev, "vhost-inflight", mmap_size, &fd);
1650 VHOST_LOG_CONFIG(ERR, "(%s) failed to alloc vhost inflight area\n", dev->ifname);
1651 ctx->msg.payload.inflight.mmap_size = 0;
1652 return RTE_VHOST_MSG_RESULT_ERR;
1654 memset(addr, 0, mmap_size);
1656 if (dev->inflight_info->addr) {
1657 munmap(dev->inflight_info->addr, dev->inflight_info->size);
1658 dev->inflight_info->addr = NULL;
1661 if (dev->inflight_info->fd >= 0) {
1662 close(dev->inflight_info->fd);
1663 dev->inflight_info->fd = -1;
1666 dev->inflight_info->addr = addr;
1667 dev->inflight_info->size = ctx->msg.payload.inflight.mmap_size = mmap_size;
1668 dev->inflight_info->fd = ctx->fds[0] = fd;
1669 ctx->msg.payload.inflight.mmap_offset = 0;
1672 if (vq_is_packed(dev)) {
1673 for (i = 0; i < num_queues; i++) {
1675 (struct rte_vhost_inflight_info_packed *)addr;
1676 inflight_packed->used_wrap_counter = 1;
1677 inflight_packed->old_used_wrap_counter = 1;
1678 for (j = 0; j < queue_size; j++)
1679 inflight_packed->desc[j].next = j + 1;
1680 addr = (void *)((char *)addr + pervq_inflight_size);
1684 VHOST_LOG_CONFIG(INFO, "(%s) send inflight mmap_size: %"PRIu64"\n",
1685 dev->ifname, ctx->msg.payload.inflight.mmap_size);
1686 VHOST_LOG_CONFIG(INFO, "(%s) send inflight mmap_offset: %"PRIu64"\n",
1687 dev->ifname, ctx->msg.payload.inflight.mmap_offset);
1688 VHOST_LOG_CONFIG(INFO, "(%s) send inflight fd: %d\n", dev->ifname, ctx->fds[0]);
1690 return RTE_VHOST_MSG_RESULT_REPLY;
1694 vhost_user_set_inflight_fd(struct virtio_net **pdev,
1695 struct vhu_msg_context *ctx,
1696 int main_fd __rte_unused)
1698 uint64_t mmap_size, mmap_offset;
1699 uint16_t num_queues, queue_size;
1700 struct virtio_net *dev = *pdev;
1701 uint32_t pervq_inflight_size;
1702 struct vhost_virtqueue *vq;
1705 int numa_node = SOCKET_ID_ANY;
1708 if (ctx->msg.size != sizeof(ctx->msg.payload.inflight) || fd < 0) {
1709 VHOST_LOG_CONFIG(ERR, "(%s) invalid set_inflight_fd message size is %d,fd is %d\n",
1710 dev->ifname, ctx->msg.size, fd);
1711 return RTE_VHOST_MSG_RESULT_ERR;
1714 mmap_size = ctx->msg.payload.inflight.mmap_size;
1715 mmap_offset = ctx->msg.payload.inflight.mmap_offset;
1716 num_queues = ctx->msg.payload.inflight.num_queues;
1717 queue_size = ctx->msg.payload.inflight.queue_size;
1719 if (vq_is_packed(dev))
1720 pervq_inflight_size = get_pervq_shm_size_packed(queue_size);
1722 pervq_inflight_size = get_pervq_shm_size_split(queue_size);
1724 VHOST_LOG_CONFIG(INFO, "(%s) set_inflight_fd mmap_size: %"PRIu64"\n",
1725 dev->ifname, mmap_size);
1726 VHOST_LOG_CONFIG(INFO, "(%s) set_inflight_fd mmap_offset: %"PRIu64"\n",
1727 dev->ifname, mmap_offset);
1728 VHOST_LOG_CONFIG(INFO, "(%s) set_inflight_fd num_queues: %u\n", dev->ifname, num_queues);
1729 VHOST_LOG_CONFIG(INFO, "(%s) set_inflight_fd queue_size: %u\n", dev->ifname, queue_size);
1730 VHOST_LOG_CONFIG(INFO, "(%s) set_inflight_fd fd: %d\n", dev->ifname, fd);
1731 VHOST_LOG_CONFIG(INFO, "(%s) set_inflight_fd pervq_inflight_size: %d\n",
1732 dev->ifname, pervq_inflight_size);
1735 * If VQ 0 has already been allocated, try to allocate on the same
1736 * NUMA node. It can be reallocated later in numa_realloc().
1738 if (dev->nr_vring > 0)
1739 numa_node = dev->virtqueue[0]->numa_node;
1741 if (!dev->inflight_info) {
1742 dev->inflight_info = rte_zmalloc_socket("inflight_info",
1743 sizeof(struct inflight_mem_info), 0, numa_node);
1744 if (dev->inflight_info == NULL) {
1745 VHOST_LOG_CONFIG(ERR, "(%s) failed to alloc dev inflight area\n",
1747 return RTE_VHOST_MSG_RESULT_ERR;
1749 dev->inflight_info->fd = -1;
1752 if (dev->inflight_info->addr) {
1753 munmap(dev->inflight_info->addr, dev->inflight_info->size);
1754 dev->inflight_info->addr = NULL;
1757 addr = mmap(0, mmap_size, PROT_READ | PROT_WRITE, MAP_SHARED,
1759 if (addr == MAP_FAILED) {
1760 VHOST_LOG_CONFIG(ERR, "(%s) failed to mmap share memory.\n", dev->ifname);
1761 return RTE_VHOST_MSG_RESULT_ERR;
1764 if (dev->inflight_info->fd >= 0) {
1765 close(dev->inflight_info->fd);
1766 dev->inflight_info->fd = -1;
1769 dev->inflight_info->fd = fd;
1770 dev->inflight_info->addr = addr;
1771 dev->inflight_info->size = mmap_size;
1773 for (i = 0; i < num_queues; i++) {
1774 vq = dev->virtqueue[i];
1778 if (vq_is_packed(dev)) {
1779 vq->inflight_packed = addr;
1780 vq->inflight_packed->desc_num = queue_size;
1782 vq->inflight_split = addr;
1783 vq->inflight_split->desc_num = queue_size;
1785 addr = (void *)((char *)addr + pervq_inflight_size);
1788 return RTE_VHOST_MSG_RESULT_OK;
1792 vhost_user_set_vring_call(struct virtio_net **pdev,
1793 struct vhu_msg_context *ctx,
1794 int main_fd __rte_unused)
1796 struct virtio_net *dev = *pdev;
1797 struct vhost_vring_file file;
1798 struct vhost_virtqueue *vq;
1801 expected_fds = (ctx->msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK) ? 0 : 1;
1802 if (validate_msg_fds(dev, ctx, expected_fds) != 0)
1803 return RTE_VHOST_MSG_RESULT_ERR;
1805 file.index = ctx->msg.payload.u64 & VHOST_USER_VRING_IDX_MASK;
1806 if (ctx->msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK)
1807 file.fd = VIRTIO_INVALID_EVENTFD;
1809 file.fd = ctx->fds[0];
1810 VHOST_LOG_CONFIG(INFO, "(%s) vring call idx:%d file:%d\n",
1811 dev->ifname, file.index, file.fd);
1813 vq = dev->virtqueue[file.index];
1817 vhost_user_notify_queue_state(dev, file.index, 0);
1820 if (vq->callfd >= 0)
1823 vq->callfd = file.fd;
1825 return RTE_VHOST_MSG_RESULT_OK;
1828 static int vhost_user_set_vring_err(struct virtio_net **pdev,
1829 struct vhu_msg_context *ctx,
1830 int main_fd __rte_unused)
1832 struct virtio_net *dev = *pdev;
1835 expected_fds = (ctx->msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK) ? 0 : 1;
1836 if (validate_msg_fds(dev, ctx, expected_fds) != 0)
1837 return RTE_VHOST_MSG_RESULT_ERR;
1839 if (!(ctx->msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK))
1841 VHOST_LOG_CONFIG(INFO, "(%s) not implemented\n", dev->ifname);
1843 return RTE_VHOST_MSG_RESULT_OK;
1847 resubmit_desc_compare(const void *a, const void *b)
1849 const struct rte_vhost_resubmit_desc *desc0 = a;
1850 const struct rte_vhost_resubmit_desc *desc1 = b;
1852 if (desc1->counter > desc0->counter)
1859 vhost_check_queue_inflights_split(struct virtio_net *dev,
1860 struct vhost_virtqueue *vq)
1863 uint16_t resubmit_num = 0, last_io, num;
1864 struct vring_used *used = vq->used;
1865 struct rte_vhost_resubmit_info *resubmit;
1866 struct rte_vhost_inflight_info_split *inflight_split;
1868 if (!(dev->protocol_features &
1869 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD)))
1870 return RTE_VHOST_MSG_RESULT_OK;
1872 /* The frontend may still not support the inflight feature
1873 * although we negotiate the protocol feature.
1875 if ((!vq->inflight_split))
1876 return RTE_VHOST_MSG_RESULT_OK;
1878 if (!vq->inflight_split->version) {
1879 vq->inflight_split->version = INFLIGHT_VERSION;
1880 return RTE_VHOST_MSG_RESULT_OK;
1883 if (vq->resubmit_inflight)
1884 return RTE_VHOST_MSG_RESULT_OK;
1886 inflight_split = vq->inflight_split;
1887 vq->global_counter = 0;
1888 last_io = inflight_split->last_inflight_io;
1890 if (inflight_split->used_idx != used->idx) {
1891 inflight_split->desc[last_io].inflight = 0;
1892 rte_atomic_thread_fence(__ATOMIC_SEQ_CST);
1893 inflight_split->used_idx = used->idx;
1896 for (i = 0; i < inflight_split->desc_num; i++) {
1897 if (inflight_split->desc[i].inflight == 1)
1901 vq->last_avail_idx += resubmit_num;
1904 resubmit = rte_zmalloc_socket("resubmit", sizeof(struct rte_vhost_resubmit_info),
1907 VHOST_LOG_CONFIG(ERR,
1908 "(%s) failed to allocate memory for resubmit info.\n",
1910 return RTE_VHOST_MSG_RESULT_ERR;
1913 resubmit->resubmit_list = rte_zmalloc_socket("resubmit_list",
1914 resubmit_num * sizeof(struct rte_vhost_resubmit_desc),
1916 if (!resubmit->resubmit_list) {
1917 VHOST_LOG_CONFIG(ERR,
1918 "(%s) failed to allocate memory for inflight desc.\n",
1921 return RTE_VHOST_MSG_RESULT_ERR;
1925 for (i = 0; i < vq->inflight_split->desc_num; i++) {
1926 if (vq->inflight_split->desc[i].inflight == 1) {
1927 resubmit->resubmit_list[num].index = i;
1928 resubmit->resubmit_list[num].counter =
1929 inflight_split->desc[i].counter;
1933 resubmit->resubmit_num = num;
1935 if (resubmit->resubmit_num > 1)
1936 qsort(resubmit->resubmit_list, resubmit->resubmit_num,
1937 sizeof(struct rte_vhost_resubmit_desc),
1938 resubmit_desc_compare);
1940 vq->global_counter = resubmit->resubmit_list[0].counter + 1;
1941 vq->resubmit_inflight = resubmit;
1944 return RTE_VHOST_MSG_RESULT_OK;
1948 vhost_check_queue_inflights_packed(struct virtio_net *dev,
1949 struct vhost_virtqueue *vq)
1952 uint16_t resubmit_num = 0, old_used_idx, num;
1953 struct rte_vhost_resubmit_info *resubmit;
1954 struct rte_vhost_inflight_info_packed *inflight_packed;
1956 if (!(dev->protocol_features &
1957 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD)))
1958 return RTE_VHOST_MSG_RESULT_OK;
1960 /* The frontend may still not support the inflight feature
1961 * although we negotiate the protocol feature.
1963 if ((!vq->inflight_packed))
1964 return RTE_VHOST_MSG_RESULT_OK;
1966 if (!vq->inflight_packed->version) {
1967 vq->inflight_packed->version = INFLIGHT_VERSION;
1968 return RTE_VHOST_MSG_RESULT_OK;
1971 if (vq->resubmit_inflight)
1972 return RTE_VHOST_MSG_RESULT_OK;
1974 inflight_packed = vq->inflight_packed;
1975 vq->global_counter = 0;
1976 old_used_idx = inflight_packed->old_used_idx;
1978 if (inflight_packed->used_idx != old_used_idx) {
1979 if (inflight_packed->desc[old_used_idx].inflight == 0) {
1980 inflight_packed->old_used_idx =
1981 inflight_packed->used_idx;
1982 inflight_packed->old_used_wrap_counter =
1983 inflight_packed->used_wrap_counter;
1984 inflight_packed->old_free_head =
1985 inflight_packed->free_head;
1987 inflight_packed->used_idx =
1988 inflight_packed->old_used_idx;
1989 inflight_packed->used_wrap_counter =
1990 inflight_packed->old_used_wrap_counter;
1991 inflight_packed->free_head =
1992 inflight_packed->old_free_head;
1996 for (i = 0; i < inflight_packed->desc_num; i++) {
1997 if (inflight_packed->desc[i].inflight == 1)
2002 resubmit = rte_zmalloc_socket("resubmit", sizeof(struct rte_vhost_resubmit_info),
2004 if (resubmit == NULL) {
2005 VHOST_LOG_CONFIG(ERR,
2006 "(%s) failed to allocate memory for resubmit info.\n",
2008 return RTE_VHOST_MSG_RESULT_ERR;
2011 resubmit->resubmit_list = rte_zmalloc_socket("resubmit_list",
2012 resubmit_num * sizeof(struct rte_vhost_resubmit_desc),
2014 if (resubmit->resubmit_list == NULL) {
2015 VHOST_LOG_CONFIG(ERR,
2016 "(%s) failed to allocate memory for resubmit desc.\n",
2019 return RTE_VHOST_MSG_RESULT_ERR;
2023 for (i = 0; i < inflight_packed->desc_num; i++) {
2024 if (vq->inflight_packed->desc[i].inflight == 1) {
2025 resubmit->resubmit_list[num].index = i;
2026 resubmit->resubmit_list[num].counter =
2027 inflight_packed->desc[i].counter;
2031 resubmit->resubmit_num = num;
2033 if (resubmit->resubmit_num > 1)
2034 qsort(resubmit->resubmit_list, resubmit->resubmit_num,
2035 sizeof(struct rte_vhost_resubmit_desc),
2036 resubmit_desc_compare);
2038 vq->global_counter = resubmit->resubmit_list[0].counter + 1;
2039 vq->resubmit_inflight = resubmit;
2042 return RTE_VHOST_MSG_RESULT_OK;
2046 vhost_user_set_vring_kick(struct virtio_net **pdev,
2047 struct vhu_msg_context *ctx,
2048 int main_fd __rte_unused)
2050 struct virtio_net *dev = *pdev;
2051 struct vhost_vring_file file;
2052 struct vhost_virtqueue *vq;
2055 expected_fds = (ctx->msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK) ? 0 : 1;
2056 if (validate_msg_fds(dev, ctx, expected_fds) != 0)
2057 return RTE_VHOST_MSG_RESULT_ERR;
2059 file.index = ctx->msg.payload.u64 & VHOST_USER_VRING_IDX_MASK;
2060 if (ctx->msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK)
2061 file.fd = VIRTIO_INVALID_EVENTFD;
2063 file.fd = ctx->fds[0];
2064 VHOST_LOG_CONFIG(INFO, "(%s) vring kick idx:%d file:%d\n",
2065 dev->ifname, file.index, file.fd);
2067 /* Interpret ring addresses only when ring is started. */
2068 dev = translate_ring_addresses(dev, file.index);
2070 if (file.fd != VIRTIO_INVALID_EVENTFD)
2073 return RTE_VHOST_MSG_RESULT_ERR;
2078 vq = dev->virtqueue[file.index];
2081 * When VHOST_USER_F_PROTOCOL_FEATURES is not negotiated,
2082 * the ring starts already enabled. Otherwise, it is enabled via
2083 * the SET_VRING_ENABLE message.
2085 if (!(dev->features & (1ULL << VHOST_USER_F_PROTOCOL_FEATURES))) {
2091 vhost_user_notify_queue_state(dev, file.index, 0);
2094 if (vq->kickfd >= 0)
2096 vq->kickfd = file.fd;
2098 if (vq_is_packed(dev)) {
2099 if (vhost_check_queue_inflights_packed(dev, vq)) {
2100 VHOST_LOG_CONFIG(ERR, "(%s) failed to inflights for vq: %d\n",
2101 dev->ifname, file.index);
2102 return RTE_VHOST_MSG_RESULT_ERR;
2105 if (vhost_check_queue_inflights_split(dev, vq)) {
2106 VHOST_LOG_CONFIG(ERR, "(%s) failed to inflights for vq: %d\n",
2107 dev->ifname, file.index);
2108 return RTE_VHOST_MSG_RESULT_ERR;
2112 return RTE_VHOST_MSG_RESULT_OK;
2116 * when virtio is stopped, qemu will send us the GET_VRING_BASE message.
2119 vhost_user_get_vring_base(struct virtio_net **pdev,
2120 struct vhu_msg_context *ctx,
2121 int main_fd __rte_unused)
2123 struct virtio_net *dev = *pdev;
2124 struct vhost_virtqueue *vq = dev->virtqueue[ctx->msg.payload.state.index];
2127 if (validate_msg_fds(dev, ctx, 0) != 0)
2128 return RTE_VHOST_MSG_RESULT_ERR;
2130 /* We have to stop the queue (virtio) if it is running. */
2131 vhost_destroy_device_notify(dev);
2133 dev->flags &= ~VIRTIO_DEV_READY;
2134 dev->flags &= ~VIRTIO_DEV_VDPA_CONFIGURED;
2136 /* Here we are safe to get the indexes */
2137 if (vq_is_packed(dev)) {
2139 * Bit[0:14]: avail index
2140 * Bit[15]: avail wrap counter
2142 val = vq->last_avail_idx & 0x7fff;
2143 val |= vq->avail_wrap_counter << 15;
2144 ctx->msg.payload.state.num = val;
2146 ctx->msg.payload.state.num = vq->last_avail_idx;
2149 VHOST_LOG_CONFIG(INFO, "(%s) vring base idx:%d file:%d\n",
2150 dev->ifname, ctx->msg.payload.state.index,
2151 ctx->msg.payload.state.num);
2153 * Based on current qemu vhost-user implementation, this message is
2154 * sent and only sent in vhost_vring_stop.
2155 * TODO: cleanup the vring, it isn't usable since here.
2157 if (vq->kickfd >= 0)
2160 vq->kickfd = VIRTIO_UNINITIALIZED_EVENTFD;
2162 if (vq->callfd >= 0)
2165 vq->callfd = VIRTIO_UNINITIALIZED_EVENTFD;
2167 vq->signalled_used_valid = false;
2169 if (vq_is_packed(dev)) {
2170 rte_free(vq->shadow_used_packed);
2171 vq->shadow_used_packed = NULL;
2173 rte_free(vq->shadow_used_split);
2174 vq->shadow_used_split = NULL;
2177 rte_free(vq->batch_copy_elems);
2178 vq->batch_copy_elems = NULL;
2180 rte_free(vq->log_cache);
2181 vq->log_cache = NULL;
2183 ctx->msg.size = sizeof(ctx->msg.payload.state);
2186 vhost_user_iotlb_flush_all(vq);
2188 vring_invalidate(dev, vq);
2190 return RTE_VHOST_MSG_RESULT_REPLY;
2194 * when virtio queues are ready to work, qemu will send us to
2195 * enable the virtio queue pair.
2198 vhost_user_set_vring_enable(struct virtio_net **pdev,
2199 struct vhu_msg_context *ctx,
2200 int main_fd __rte_unused)
2202 struct virtio_net *dev = *pdev;
2203 bool enable = !!ctx->msg.payload.state.num;
2204 int index = (int)ctx->msg.payload.state.index;
2206 if (validate_msg_fds(dev, ctx, 0) != 0)
2207 return RTE_VHOST_MSG_RESULT_ERR;
2209 VHOST_LOG_CONFIG(INFO, "(%s) set queue enable: %d to qp idx: %d\n",
2210 dev->ifname, enable, index);
2212 if (enable && dev->virtqueue[index]->async) {
2213 if (dev->virtqueue[index]->async->pkts_inflight_n) {
2214 VHOST_LOG_CONFIG(ERR,
2215 "(%s) failed to enable vring. Inflight packets must be completed first\n",
2217 return RTE_VHOST_MSG_RESULT_ERR;
2221 dev->virtqueue[index]->enabled = enable;
2223 return RTE_VHOST_MSG_RESULT_OK;
2227 vhost_user_get_protocol_features(struct virtio_net **pdev,
2228 struct vhu_msg_context *ctx,
2229 int main_fd __rte_unused)
2231 struct virtio_net *dev = *pdev;
2232 uint64_t features, protocol_features;
2234 if (validate_msg_fds(dev, ctx, 0) != 0)
2235 return RTE_VHOST_MSG_RESULT_ERR;
2237 rte_vhost_driver_get_features(dev->ifname, &features);
2238 rte_vhost_driver_get_protocol_features(dev->ifname, &protocol_features);
2240 ctx->msg.payload.u64 = protocol_features;
2241 ctx->msg.size = sizeof(ctx->msg.payload.u64);
2244 return RTE_VHOST_MSG_RESULT_REPLY;
2248 vhost_user_set_protocol_features(struct virtio_net **pdev,
2249 struct vhu_msg_context *ctx,
2250 int main_fd __rte_unused)
2252 struct virtio_net *dev = *pdev;
2253 uint64_t protocol_features = ctx->msg.payload.u64;
2254 uint64_t slave_protocol_features = 0;
2256 if (validate_msg_fds(dev, ctx, 0) != 0)
2257 return RTE_VHOST_MSG_RESULT_ERR;
2259 rte_vhost_driver_get_protocol_features(dev->ifname,
2260 &slave_protocol_features);
2261 if (protocol_features & ~slave_protocol_features) {
2262 VHOST_LOG_CONFIG(ERR, "(%s) received invalid protocol features.\n", dev->ifname);
2263 return RTE_VHOST_MSG_RESULT_ERR;
2266 dev->protocol_features = protocol_features;
2267 VHOST_LOG_CONFIG(INFO, "(%s) negotiated Vhost-user protocol features: 0x%" PRIx64 "\n",
2268 dev->ifname, dev->protocol_features);
2270 return RTE_VHOST_MSG_RESULT_OK;
2274 vhost_user_set_log_base(struct virtio_net **pdev,
2275 struct vhu_msg_context *ctx,
2276 int main_fd __rte_unused)
2278 struct virtio_net *dev = *pdev;
2279 int fd = ctx->fds[0];
2284 if (validate_msg_fds(dev, ctx, 1) != 0)
2285 return RTE_VHOST_MSG_RESULT_ERR;
2288 VHOST_LOG_CONFIG(ERR, "(%s) invalid log fd: %d\n", dev->ifname, fd);
2289 return RTE_VHOST_MSG_RESULT_ERR;
2292 if (ctx->msg.size != sizeof(VhostUserLog)) {
2293 VHOST_LOG_CONFIG(ERR, "(%s) invalid log base msg size: %"PRId32" != %d\n",
2294 dev->ifname, ctx->msg.size, (int)sizeof(VhostUserLog));
2298 size = ctx->msg.payload.log.mmap_size;
2299 off = ctx->msg.payload.log.mmap_offset;
2301 /* Check for mmap size and offset overflow. */
2303 VHOST_LOG_CONFIG(ERR,
2304 "(%s) log offset %#"PRIx64" and log size %#"PRIx64" overflow\n",
2305 dev->ifname, off, size);
2309 VHOST_LOG_CONFIG(INFO, "(%s) log mmap size: %"PRId64", offset: %"PRId64"\n",
2310 dev->ifname, size, off);
2313 * mmap from 0 to workaround a hugepage mmap bug: mmap will
2314 * fail when offset is not page size aligned.
2316 addr = mmap(0, size + off, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
2318 if (addr == MAP_FAILED) {
2319 VHOST_LOG_CONFIG(ERR, "(%s) mmap log base failed!\n", dev->ifname);
2320 return RTE_VHOST_MSG_RESULT_ERR;
2324 * Free previously mapped log memory on occasionally
2325 * multiple VHOST_USER_SET_LOG_BASE.
2327 if (dev->log_addr) {
2328 munmap((void *)(uintptr_t)dev->log_addr, dev->log_size);
2330 dev->log_addr = (uint64_t)(uintptr_t)addr;
2331 dev->log_base = dev->log_addr + off;
2332 dev->log_size = size;
2334 for (i = 0; i < dev->nr_vring; i++) {
2335 struct vhost_virtqueue *vq = dev->virtqueue[i];
2337 rte_free(vq->log_cache);
2338 vq->log_cache = NULL;
2339 vq->log_cache_nb_elem = 0;
2340 vq->log_cache = rte_malloc_socket("vq log cache",
2341 sizeof(struct log_cache_entry) * VHOST_LOG_CACHE_NR,
2344 * If log cache alloc fail, don't fail migration, but no
2345 * caching will be done, which will impact performance
2348 VHOST_LOG_CONFIG(ERR, "(%s) failed to allocate VQ logging cache\n",
2353 * The spec is not clear about it (yet), but QEMU doesn't expect
2354 * any payload in the reply.
2359 return RTE_VHOST_MSG_RESULT_REPLY;
2363 return RTE_VHOST_MSG_RESULT_ERR;
2366 static int vhost_user_set_log_fd(struct virtio_net **pdev,
2367 struct vhu_msg_context *ctx,
2368 int main_fd __rte_unused)
2370 struct virtio_net *dev = *pdev;
2372 if (validate_msg_fds(dev, ctx, 1) != 0)
2373 return RTE_VHOST_MSG_RESULT_ERR;
2376 VHOST_LOG_CONFIG(INFO, "(%s) not implemented.\n", dev->ifname);
2378 return RTE_VHOST_MSG_RESULT_OK;
2382 * An rarp packet is constructed and broadcasted to notify switches about
2383 * the new location of the migrated VM, so that packets from outside will
2384 * not be lost after migration.
2386 * However, we don't actually "send" a rarp packet here, instead, we set
2387 * a flag 'broadcast_rarp' to let rte_vhost_dequeue_burst() inject it.
2390 vhost_user_send_rarp(struct virtio_net **pdev,
2391 struct vhu_msg_context *ctx,
2392 int main_fd __rte_unused)
2394 struct virtio_net *dev = *pdev;
2395 uint8_t *mac = (uint8_t *)&ctx->msg.payload.u64;
2396 struct rte_vdpa_device *vdpa_dev;
2398 if (validate_msg_fds(dev, ctx, 0) != 0)
2399 return RTE_VHOST_MSG_RESULT_ERR;
2401 VHOST_LOG_CONFIG(DEBUG, "(%s) MAC: " RTE_ETHER_ADDR_PRT_FMT "\n",
2402 dev->ifname, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
2403 memcpy(dev->mac.addr_bytes, mac, 6);
2406 * Set the flag to inject a RARP broadcast packet at
2407 * rte_vhost_dequeue_burst().
2409 * __ATOMIC_RELEASE ordering is for making sure the mac is
2410 * copied before the flag is set.
2412 __atomic_store_n(&dev->broadcast_rarp, 1, __ATOMIC_RELEASE);
2413 vdpa_dev = dev->vdpa_dev;
2414 if (vdpa_dev && vdpa_dev->ops->migration_done)
2415 vdpa_dev->ops->migration_done(dev->vid);
2417 return RTE_VHOST_MSG_RESULT_OK;
2421 vhost_user_net_set_mtu(struct virtio_net **pdev,
2422 struct vhu_msg_context *ctx,
2423 int main_fd __rte_unused)
2425 struct virtio_net *dev = *pdev;
2427 if (validate_msg_fds(dev, ctx, 0) != 0)
2428 return RTE_VHOST_MSG_RESULT_ERR;
2430 if (ctx->msg.payload.u64 < VIRTIO_MIN_MTU ||
2431 ctx->msg.payload.u64 > VIRTIO_MAX_MTU) {
2432 VHOST_LOG_CONFIG(ERR, "(%s) invalid MTU size (%"PRIu64")\n",
2433 dev->ifname, ctx->msg.payload.u64);
2435 return RTE_VHOST_MSG_RESULT_ERR;
2438 dev->mtu = ctx->msg.payload.u64;
2440 return RTE_VHOST_MSG_RESULT_OK;
2444 vhost_user_set_req_fd(struct virtio_net **pdev,
2445 struct vhu_msg_context *ctx,
2446 int main_fd __rte_unused)
2448 struct virtio_net *dev = *pdev;
2449 int fd = ctx->fds[0];
2451 if (validate_msg_fds(dev, ctx, 1) != 0)
2452 return RTE_VHOST_MSG_RESULT_ERR;
2455 VHOST_LOG_CONFIG(ERR, "(%s) invalid file descriptor for slave channel (%d)\n",
2457 return RTE_VHOST_MSG_RESULT_ERR;
2460 if (dev->slave_req_fd >= 0)
2461 close(dev->slave_req_fd);
2463 dev->slave_req_fd = fd;
2465 return RTE_VHOST_MSG_RESULT_OK;
2469 is_vring_iotlb_split(struct vhost_virtqueue *vq, struct vhost_iotlb_msg *imsg)
2471 struct vhost_vring_addr *ra;
2472 uint64_t start, end, len;
2475 end = start + imsg->size;
2477 ra = &vq->ring_addrs;
2478 len = sizeof(struct vring_desc) * vq->size;
2479 if (ra->desc_user_addr < end && (ra->desc_user_addr + len) > start)
2482 len = sizeof(struct vring_avail) + sizeof(uint16_t) * vq->size;
2483 if (ra->avail_user_addr < end && (ra->avail_user_addr + len) > start)
2486 len = sizeof(struct vring_used) +
2487 sizeof(struct vring_used_elem) * vq->size;
2488 if (ra->used_user_addr < end && (ra->used_user_addr + len) > start)
2491 if (ra->flags & (1 << VHOST_VRING_F_LOG)) {
2492 len = sizeof(uint64_t);
2493 if (ra->log_guest_addr < end &&
2494 (ra->log_guest_addr + len) > start)
2502 is_vring_iotlb_packed(struct vhost_virtqueue *vq, struct vhost_iotlb_msg *imsg)
2504 struct vhost_vring_addr *ra;
2505 uint64_t start, end, len;
2508 end = start + imsg->size;
2510 ra = &vq->ring_addrs;
2511 len = sizeof(struct vring_packed_desc) * vq->size;
2512 if (ra->desc_user_addr < end && (ra->desc_user_addr + len) > start)
2515 len = sizeof(struct vring_packed_desc_event);
2516 if (ra->avail_user_addr < end && (ra->avail_user_addr + len) > start)
2519 len = sizeof(struct vring_packed_desc_event);
2520 if (ra->used_user_addr < end && (ra->used_user_addr + len) > start)
2523 if (ra->flags & (1 << VHOST_VRING_F_LOG)) {
2524 len = sizeof(uint64_t);
2525 if (ra->log_guest_addr < end &&
2526 (ra->log_guest_addr + len) > start)
2533 static int is_vring_iotlb(struct virtio_net *dev,
2534 struct vhost_virtqueue *vq,
2535 struct vhost_iotlb_msg *imsg)
2537 if (vq_is_packed(dev))
2538 return is_vring_iotlb_packed(vq, imsg);
2540 return is_vring_iotlb_split(vq, imsg);
2544 vhost_user_iotlb_msg(struct virtio_net **pdev,
2545 struct vhu_msg_context *ctx,
2546 int main_fd __rte_unused)
2548 struct virtio_net *dev = *pdev;
2549 struct vhost_iotlb_msg *imsg = &ctx->msg.payload.iotlb;
2553 if (validate_msg_fds(dev, ctx, 0) != 0)
2554 return RTE_VHOST_MSG_RESULT_ERR;
2556 switch (imsg->type) {
2557 case VHOST_IOTLB_UPDATE:
2559 vva = qva_to_vva(dev, imsg->uaddr, &len);
2561 return RTE_VHOST_MSG_RESULT_ERR;
2563 for (i = 0; i < dev->nr_vring; i++) {
2564 struct vhost_virtqueue *vq = dev->virtqueue[i];
2569 vhost_user_iotlb_cache_insert(dev, vq, imsg->iova, vva,
2572 if (is_vring_iotlb(dev, vq, imsg))
2573 *pdev = dev = translate_ring_addresses(dev, i);
2576 case VHOST_IOTLB_INVALIDATE:
2577 for (i = 0; i < dev->nr_vring; i++) {
2578 struct vhost_virtqueue *vq = dev->virtqueue[i];
2583 vhost_user_iotlb_cache_remove(vq, imsg->iova,
2586 if (is_vring_iotlb(dev, vq, imsg))
2587 vring_invalidate(dev, vq);
2591 VHOST_LOG_CONFIG(ERR, "(%s) invalid IOTLB message type (%d)\n",
2592 dev->ifname, imsg->type);
2593 return RTE_VHOST_MSG_RESULT_ERR;
2596 return RTE_VHOST_MSG_RESULT_OK;
2600 vhost_user_set_postcopy_advise(struct virtio_net **pdev,
2601 struct vhu_msg_context *ctx,
2602 int main_fd __rte_unused)
2604 struct virtio_net *dev = *pdev;
2605 #ifdef RTE_LIBRTE_VHOST_POSTCOPY
2606 struct uffdio_api api_struct;
2608 if (validate_msg_fds(dev, ctx, 0) != 0)
2609 return RTE_VHOST_MSG_RESULT_ERR;
2611 dev->postcopy_ufd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK);
2613 if (dev->postcopy_ufd == -1) {
2614 VHOST_LOG_CONFIG(ERR, "(%s) userfaultfd not available: %s\n",
2615 dev->ifname, strerror(errno));
2616 return RTE_VHOST_MSG_RESULT_ERR;
2618 api_struct.api = UFFD_API;
2619 api_struct.features = 0;
2620 if (ioctl(dev->postcopy_ufd, UFFDIO_API, &api_struct)) {
2621 VHOST_LOG_CONFIG(ERR, "(%s) UFFDIO_API ioctl failure: %s\n",
2622 dev->ifname, strerror(errno));
2623 close(dev->postcopy_ufd);
2624 dev->postcopy_ufd = -1;
2625 return RTE_VHOST_MSG_RESULT_ERR;
2627 ctx->fds[0] = dev->postcopy_ufd;
2630 return RTE_VHOST_MSG_RESULT_REPLY;
2632 dev->postcopy_ufd = -1;
2635 return RTE_VHOST_MSG_RESULT_ERR;
2640 vhost_user_set_postcopy_listen(struct virtio_net **pdev,
2641 struct vhu_msg_context *ctx __rte_unused,
2642 int main_fd __rte_unused)
2644 struct virtio_net *dev = *pdev;
2646 if (validate_msg_fds(dev, ctx, 0) != 0)
2647 return RTE_VHOST_MSG_RESULT_ERR;
2649 if (dev->mem && dev->mem->nregions) {
2650 VHOST_LOG_CONFIG(ERR, "(%s) regions already registered at postcopy-listen\n",
2652 return RTE_VHOST_MSG_RESULT_ERR;
2654 dev->postcopy_listening = 1;
2656 return RTE_VHOST_MSG_RESULT_OK;
2660 vhost_user_postcopy_end(struct virtio_net **pdev,
2661 struct vhu_msg_context *ctx,
2662 int main_fd __rte_unused)
2664 struct virtio_net *dev = *pdev;
2666 if (validate_msg_fds(dev, ctx, 0) != 0)
2667 return RTE_VHOST_MSG_RESULT_ERR;
2669 dev->postcopy_listening = 0;
2670 if (dev->postcopy_ufd >= 0) {
2671 close(dev->postcopy_ufd);
2672 dev->postcopy_ufd = -1;
2675 ctx->msg.payload.u64 = 0;
2676 ctx->msg.size = sizeof(ctx->msg.payload.u64);
2679 return RTE_VHOST_MSG_RESULT_REPLY;
2683 vhost_user_get_status(struct virtio_net **pdev,
2684 struct vhu_msg_context *ctx,
2685 int main_fd __rte_unused)
2687 struct virtio_net *dev = *pdev;
2689 if (validate_msg_fds(dev, ctx, 0) != 0)
2690 return RTE_VHOST_MSG_RESULT_ERR;
2692 ctx->msg.payload.u64 = dev->status;
2693 ctx->msg.size = sizeof(ctx->msg.payload.u64);
2696 return RTE_VHOST_MSG_RESULT_REPLY;
2700 vhost_user_set_status(struct virtio_net **pdev,
2701 struct vhu_msg_context *ctx,
2702 int main_fd __rte_unused)
2704 struct virtio_net *dev = *pdev;
2706 if (validate_msg_fds(dev, ctx, 0) != 0)
2707 return RTE_VHOST_MSG_RESULT_ERR;
2709 /* As per Virtio specification, the device status is 8bits long */
2710 if (ctx->msg.payload.u64 > UINT8_MAX) {
2711 VHOST_LOG_CONFIG(ERR, "(%s) invalid VHOST_USER_SET_STATUS payload 0x%" PRIx64 "\n",
2712 dev->ifname, ctx->msg.payload.u64);
2713 return RTE_VHOST_MSG_RESULT_ERR;
2716 dev->status = ctx->msg.payload.u64;
2718 if ((dev->status & VIRTIO_DEVICE_STATUS_FEATURES_OK) &&
2719 (dev->flags & VIRTIO_DEV_FEATURES_FAILED)) {
2720 VHOST_LOG_CONFIG(ERR,
2721 "(%s) FEATURES_OK bit is set but feature negotiation failed\n",
2724 * Clear the bit to let the driver know about the feature
2725 * negotiation failure
2727 dev->status &= ~VIRTIO_DEVICE_STATUS_FEATURES_OK;
2730 VHOST_LOG_CONFIG(INFO, "(%s) new device status(0x%08x):\n", dev->ifname,
2732 VHOST_LOG_CONFIG(INFO, "(%s)\t-RESET: %u\n", dev->ifname,
2733 (dev->status == VIRTIO_DEVICE_STATUS_RESET));
2734 VHOST_LOG_CONFIG(INFO, "(%s)\t-ACKNOWLEDGE: %u\n", dev->ifname,
2735 !!(dev->status & VIRTIO_DEVICE_STATUS_ACK));
2736 VHOST_LOG_CONFIG(INFO, "(%s)\t-DRIVER: %u\n", dev->ifname,
2737 !!(dev->status & VIRTIO_DEVICE_STATUS_DRIVER));
2738 VHOST_LOG_CONFIG(INFO, "(%s)\t-FEATURES_OK: %u\n", dev->ifname,
2739 !!(dev->status & VIRTIO_DEVICE_STATUS_FEATURES_OK));
2740 VHOST_LOG_CONFIG(INFO, "(%s)\t-DRIVER_OK: %u\n", dev->ifname,
2741 !!(dev->status & VIRTIO_DEVICE_STATUS_DRIVER_OK));
2742 VHOST_LOG_CONFIG(INFO, "(%s)\t-DEVICE_NEED_RESET: %u\n", dev->ifname,
2743 !!(dev->status & VIRTIO_DEVICE_STATUS_DEV_NEED_RESET));
2744 VHOST_LOG_CONFIG(INFO, "(%s)\t-FAILED: %u\n", dev->ifname,
2745 !!(dev->status & VIRTIO_DEVICE_STATUS_FAILED));
2747 return RTE_VHOST_MSG_RESULT_OK;
2750 typedef int (*vhost_message_handler_t)(struct virtio_net **pdev,
2751 struct vhu_msg_context *ctx,
2754 static vhost_message_handler_t vhost_message_handlers[VHOST_USER_MAX] = {
2755 [VHOST_USER_NONE] = NULL,
2756 [VHOST_USER_GET_FEATURES] = vhost_user_get_features,
2757 [VHOST_USER_SET_FEATURES] = vhost_user_set_features,
2758 [VHOST_USER_SET_OWNER] = vhost_user_set_owner,
2759 [VHOST_USER_RESET_OWNER] = vhost_user_reset_owner,
2760 [VHOST_USER_SET_MEM_TABLE] = vhost_user_set_mem_table,
2761 [VHOST_USER_SET_LOG_BASE] = vhost_user_set_log_base,
2762 [VHOST_USER_SET_LOG_FD] = vhost_user_set_log_fd,
2763 [VHOST_USER_SET_VRING_NUM] = vhost_user_set_vring_num,
2764 [VHOST_USER_SET_VRING_ADDR] = vhost_user_set_vring_addr,
2765 [VHOST_USER_SET_VRING_BASE] = vhost_user_set_vring_base,
2766 [VHOST_USER_GET_VRING_BASE] = vhost_user_get_vring_base,
2767 [VHOST_USER_SET_VRING_KICK] = vhost_user_set_vring_kick,
2768 [VHOST_USER_SET_VRING_CALL] = vhost_user_set_vring_call,
2769 [VHOST_USER_SET_VRING_ERR] = vhost_user_set_vring_err,
2770 [VHOST_USER_GET_PROTOCOL_FEATURES] = vhost_user_get_protocol_features,
2771 [VHOST_USER_SET_PROTOCOL_FEATURES] = vhost_user_set_protocol_features,
2772 [VHOST_USER_GET_QUEUE_NUM] = vhost_user_get_queue_num,
2773 [VHOST_USER_SET_VRING_ENABLE] = vhost_user_set_vring_enable,
2774 [VHOST_USER_SEND_RARP] = vhost_user_send_rarp,
2775 [VHOST_USER_NET_SET_MTU] = vhost_user_net_set_mtu,
2776 [VHOST_USER_SET_SLAVE_REQ_FD] = vhost_user_set_req_fd,
2777 [VHOST_USER_IOTLB_MSG] = vhost_user_iotlb_msg,
2778 [VHOST_USER_POSTCOPY_ADVISE] = vhost_user_set_postcopy_advise,
2779 [VHOST_USER_POSTCOPY_LISTEN] = vhost_user_set_postcopy_listen,
2780 [VHOST_USER_POSTCOPY_END] = vhost_user_postcopy_end,
2781 [VHOST_USER_GET_INFLIGHT_FD] = vhost_user_get_inflight_fd,
2782 [VHOST_USER_SET_INFLIGHT_FD] = vhost_user_set_inflight_fd,
2783 [VHOST_USER_SET_STATUS] = vhost_user_set_status,
2784 [VHOST_USER_GET_STATUS] = vhost_user_get_status,
2787 /* return bytes# of read on success or negative val on failure. */
2789 read_vhost_message(struct virtio_net *dev, int sockfd, struct vhu_msg_context *ctx)
2793 ret = read_fd_message(dev->ifname, sockfd, (char *)&ctx->msg, VHOST_USER_HDR_SIZE,
2794 ctx->fds, VHOST_MEMORY_MAX_NREGIONS, &ctx->fd_num);
2797 } else if (ret != VHOST_USER_HDR_SIZE) {
2798 VHOST_LOG_CONFIG(ERR, "(%s) Unexpected header size read\n", dev->ifname);
2803 if (ctx->msg.size) {
2804 if (ctx->msg.size > sizeof(ctx->msg.payload)) {
2805 VHOST_LOG_CONFIG(ERR, "(%s) invalid msg size: %d\n",
2806 dev->ifname, ctx->msg.size);
2809 ret = read(sockfd, &ctx->msg.payload, ctx->msg.size);
2812 if (ret != (int)ctx->msg.size) {
2813 VHOST_LOG_CONFIG(ERR, "(%s) read control message failed\n", dev->ifname);
2822 send_vhost_message(struct virtio_net *dev, int sockfd, struct vhu_msg_context *ctx)
2827 return send_fd_message(dev->ifname, sockfd, (char *)&ctx->msg,
2828 VHOST_USER_HDR_SIZE + ctx->msg.size, ctx->fds, ctx->fd_num);
2832 send_vhost_reply(struct virtio_net *dev, int sockfd, struct vhu_msg_context *ctx)
2837 ctx->msg.flags &= ~VHOST_USER_VERSION_MASK;
2838 ctx->msg.flags &= ~VHOST_USER_NEED_REPLY;
2839 ctx->msg.flags |= VHOST_USER_VERSION;
2840 ctx->msg.flags |= VHOST_USER_REPLY_MASK;
2842 return send_vhost_message(dev, sockfd, ctx);
2846 send_vhost_slave_message(struct virtio_net *dev,
2847 struct vhu_msg_context *ctx)
2851 if (ctx->msg.flags & VHOST_USER_NEED_REPLY)
2852 rte_spinlock_lock(&dev->slave_req_lock);
2854 ret = send_vhost_message(dev, dev->slave_req_fd, ctx);
2855 if (ret < 0 && (ctx->msg.flags & VHOST_USER_NEED_REPLY))
2856 rte_spinlock_unlock(&dev->slave_req_lock);
2862 * Allocate a queue pair if it hasn't been allocated yet
2865 vhost_user_check_and_alloc_queue_pair(struct virtio_net *dev,
2866 struct vhu_msg_context *ctx)
2870 switch (ctx->msg.request.master) {
2871 case VHOST_USER_SET_VRING_KICK:
2872 case VHOST_USER_SET_VRING_CALL:
2873 case VHOST_USER_SET_VRING_ERR:
2874 vring_idx = ctx->msg.payload.u64 & VHOST_USER_VRING_IDX_MASK;
2876 case VHOST_USER_SET_VRING_NUM:
2877 case VHOST_USER_SET_VRING_BASE:
2878 case VHOST_USER_GET_VRING_BASE:
2879 case VHOST_USER_SET_VRING_ENABLE:
2880 vring_idx = ctx->msg.payload.state.index;
2882 case VHOST_USER_SET_VRING_ADDR:
2883 vring_idx = ctx->msg.payload.addr.index;
2889 if (vring_idx >= VHOST_MAX_VRING) {
2890 VHOST_LOG_CONFIG(ERR, "(%s) invalid vring index: %u\n", dev->ifname, vring_idx);
2894 if (dev->virtqueue[vring_idx])
2897 return alloc_vring_queue(dev, vring_idx);
2901 vhost_user_lock_all_queue_pairs(struct virtio_net *dev)
2904 unsigned int vq_num = 0;
2906 while (vq_num < dev->nr_vring) {
2907 struct vhost_virtqueue *vq = dev->virtqueue[i];
2910 rte_spinlock_lock(&vq->access_lock);
2918 vhost_user_unlock_all_queue_pairs(struct virtio_net *dev)
2921 unsigned int vq_num = 0;
2923 while (vq_num < dev->nr_vring) {
2924 struct vhost_virtqueue *vq = dev->virtqueue[i];
2927 rte_spinlock_unlock(&vq->access_lock);
2935 vhost_user_msg_handler(int vid, int fd)
2937 struct virtio_net *dev;
2938 struct vhu_msg_context ctx;
2939 struct rte_vdpa_device *vdpa_dev;
2941 int unlock_required = 0;
2946 dev = get_device(vid);
2950 if (!dev->notify_ops) {
2951 dev->notify_ops = vhost_driver_callback_get(dev->ifname);
2952 if (!dev->notify_ops) {
2953 VHOST_LOG_CONFIG(ERR, "(%s) failed to get callback ops for driver\n",
2959 ret = read_vhost_message(dev, fd, &ctx);
2962 VHOST_LOG_CONFIG(ERR, "(%s) vhost read message failed\n", dev->ifname);
2964 VHOST_LOG_CONFIG(INFO, "(%s) vhost peer closed\n", dev->ifname);
2970 request = ctx.msg.request.master;
2971 if (request > VHOST_USER_NONE && request < VHOST_USER_MAX &&
2972 vhost_message_str[request]) {
2973 if (request != VHOST_USER_IOTLB_MSG)
2974 VHOST_LOG_CONFIG(INFO, "(%s) read message %s\n",
2975 dev->ifname, vhost_message_str[request]);
2977 VHOST_LOG_CONFIG(DEBUG, "(%s) read message %s\n",
2978 dev->ifname, vhost_message_str[request]);
2980 VHOST_LOG_CONFIG(DEBUG, "(%s) external request %d\n", dev->ifname, request);
2983 ret = vhost_user_check_and_alloc_queue_pair(dev, &ctx);
2985 VHOST_LOG_CONFIG(ERR, "(%s) failed to alloc queue\n", dev->ifname);
2990 * Note: we don't lock all queues on VHOST_USER_GET_VRING_BASE
2991 * and VHOST_USER_RESET_OWNER, since it is sent when virtio stops
2992 * and device is destroyed. destroy_device waits for queues to be
2993 * inactive, so it is safe. Otherwise taking the access_lock
2994 * would cause a dead lock.
2997 case VHOST_USER_SET_FEATURES:
2998 case VHOST_USER_SET_PROTOCOL_FEATURES:
2999 case VHOST_USER_SET_OWNER:
3000 case VHOST_USER_SET_MEM_TABLE:
3001 case VHOST_USER_SET_LOG_BASE:
3002 case VHOST_USER_SET_LOG_FD:
3003 case VHOST_USER_SET_VRING_NUM:
3004 case VHOST_USER_SET_VRING_ADDR:
3005 case VHOST_USER_SET_VRING_BASE:
3006 case VHOST_USER_SET_VRING_KICK:
3007 case VHOST_USER_SET_VRING_CALL:
3008 case VHOST_USER_SET_VRING_ERR:
3009 case VHOST_USER_SET_VRING_ENABLE:
3010 case VHOST_USER_SEND_RARP:
3011 case VHOST_USER_NET_SET_MTU:
3012 case VHOST_USER_SET_SLAVE_REQ_FD:
3013 if (!(dev->flags & VIRTIO_DEV_VDPA_CONFIGURED)) {
3014 vhost_user_lock_all_queue_pairs(dev);
3015 unlock_required = 1;
3024 if (dev->extern_ops.pre_msg_handle) {
3025 ret = (*dev->extern_ops.pre_msg_handle)(dev->vid,
3028 case RTE_VHOST_MSG_RESULT_REPLY:
3029 send_vhost_reply(dev, fd, &ctx);
3031 case RTE_VHOST_MSG_RESULT_ERR:
3032 case RTE_VHOST_MSG_RESULT_OK:
3034 goto skip_to_post_handle;
3035 case RTE_VHOST_MSG_RESULT_NOT_HANDLED:
3041 if (request > VHOST_USER_NONE && request < VHOST_USER_MAX) {
3042 if (!vhost_message_handlers[request])
3043 goto skip_to_post_handle;
3044 ret = vhost_message_handlers[request](&dev, &ctx, fd);
3047 case RTE_VHOST_MSG_RESULT_ERR:
3048 VHOST_LOG_CONFIG(ERR, "(%s) processing %s failed.\n",
3049 dev->ifname, vhost_message_str[request]);
3052 case RTE_VHOST_MSG_RESULT_OK:
3053 VHOST_LOG_CONFIG(DEBUG, "(%s) processing %s succeeded.\n",
3054 dev->ifname, vhost_message_str[request]);
3057 case RTE_VHOST_MSG_RESULT_REPLY:
3058 VHOST_LOG_CONFIG(DEBUG, "(%s) processing %s succeeded and needs reply.\n",
3059 dev->ifname, vhost_message_str[request]);
3060 send_vhost_reply(dev, fd, &ctx);
3068 skip_to_post_handle:
3069 if (ret != RTE_VHOST_MSG_RESULT_ERR &&
3070 dev->extern_ops.post_msg_handle) {
3071 ret = (*dev->extern_ops.post_msg_handle)(dev->vid,
3074 case RTE_VHOST_MSG_RESULT_REPLY:
3075 send_vhost_reply(dev, fd, &ctx);
3077 case RTE_VHOST_MSG_RESULT_ERR:
3078 case RTE_VHOST_MSG_RESULT_OK:
3080 case RTE_VHOST_MSG_RESULT_NOT_HANDLED:
3086 /* If message was not handled at this stage, treat it as an error */
3088 VHOST_LOG_CONFIG(ERR, "(%s) vhost message (req: %d) was not handled.\n",
3089 dev->ifname, request);
3090 close_msg_fds(&ctx);
3091 ret = RTE_VHOST_MSG_RESULT_ERR;
3095 * If the request required a reply that was already sent,
3096 * this optional reply-ack won't be sent as the
3097 * VHOST_USER_NEED_REPLY was cleared in send_vhost_reply().
3099 if (ctx.msg.flags & VHOST_USER_NEED_REPLY) {
3100 ctx.msg.payload.u64 = ret == RTE_VHOST_MSG_RESULT_ERR;
3101 ctx.msg.size = sizeof(ctx.msg.payload.u64);
3103 send_vhost_reply(dev, fd, &ctx);
3104 } else if (ret == RTE_VHOST_MSG_RESULT_ERR) {
3105 VHOST_LOG_CONFIG(ERR, "(%s) vhost message handling failed.\n", dev->ifname);
3109 for (i = 0; i < dev->nr_vring; i++) {
3110 struct vhost_virtqueue *vq = dev->virtqueue[i];
3111 bool cur_ready = vq_is_ready(dev, vq);
3113 if (cur_ready != (vq && vq->ready)) {
3114 vq->ready = cur_ready;
3115 vhost_user_notify_queue_state(dev, i, cur_ready);
3119 if (unlock_required)
3120 vhost_user_unlock_all_queue_pairs(dev);
3122 if (!virtio_is_ready(dev))
3126 * Virtio is now ready. If not done already, it is time
3127 * to notify the application it can process the rings and
3128 * configure the vDPA device if present.
3131 if (!(dev->flags & VIRTIO_DEV_RUNNING)) {
3132 if (dev->notify_ops->new_device(dev->vid) == 0)
3133 dev->flags |= VIRTIO_DEV_RUNNING;
3136 vdpa_dev = dev->vdpa_dev;
3140 if (!(dev->flags & VIRTIO_DEV_VDPA_CONFIGURED)) {
3141 if (vdpa_dev->ops->dev_conf(dev->vid))
3142 VHOST_LOG_CONFIG(ERR, "(%s) failed to configure vDPA device\n",
3145 dev->flags |= VIRTIO_DEV_VDPA_CONFIGURED;
3152 static int process_slave_message_reply(struct virtio_net *dev,
3153 const struct vhu_msg_context *ctx)
3155 struct vhu_msg_context msg_reply;
3158 if ((ctx->msg.flags & VHOST_USER_NEED_REPLY) == 0)
3161 ret = read_vhost_message(dev, dev->slave_req_fd, &msg_reply);
3164 VHOST_LOG_CONFIG(ERR, "(%s) vhost read slave message reply failed\n",
3167 VHOST_LOG_CONFIG(INFO, "(%s) vhost peer closed\n", dev->ifname);
3173 if (msg_reply.msg.request.slave != ctx->msg.request.slave) {
3174 VHOST_LOG_CONFIG(ERR, "(%s) received unexpected msg type (%u), expected %u\n",
3175 dev->ifname, msg_reply.msg.request.slave, ctx->msg.request.slave);
3180 ret = msg_reply.msg.payload.u64 ? -1 : 0;
3183 rte_spinlock_unlock(&dev->slave_req_lock);
3188 vhost_user_iotlb_miss(struct virtio_net *dev, uint64_t iova, uint8_t perm)
3191 struct vhu_msg_context ctx = {
3193 .request.slave = VHOST_USER_SLAVE_IOTLB_MSG,
3194 .flags = VHOST_USER_VERSION,
3195 .size = sizeof(ctx.msg.payload.iotlb),
3199 .type = VHOST_IOTLB_MISS,
3204 ret = send_vhost_message(dev, dev->slave_req_fd, &ctx);
3206 VHOST_LOG_CONFIG(ERR, "(%s) failed to send IOTLB miss message (%d)\n",
3215 vhost_user_slave_config_change(struct virtio_net *dev, bool need_reply)
3218 struct vhu_msg_context ctx = {
3220 .request.slave = VHOST_USER_SLAVE_CONFIG_CHANGE_MSG,
3221 .flags = VHOST_USER_VERSION,
3227 ctx.msg.flags |= VHOST_USER_NEED_REPLY;
3229 ret = send_vhost_slave_message(dev, &ctx);
3231 VHOST_LOG_CONFIG(ERR, "(%s) failed to send config change (%d)\n",
3236 return process_slave_message_reply(dev, &ctx);
3240 rte_vhost_slave_config_change(int vid, bool need_reply)
3242 struct virtio_net *dev;
3244 dev = get_device(vid);
3248 return vhost_user_slave_config_change(dev, need_reply);
3251 static int vhost_user_slave_set_vring_host_notifier(struct virtio_net *dev,
3257 struct vhu_msg_context ctx = {
3259 .request.slave = VHOST_USER_SLAVE_VRING_HOST_NOTIFIER_MSG,
3260 .flags = VHOST_USER_VERSION | VHOST_USER_NEED_REPLY,
3261 .size = sizeof(ctx.msg.payload.area),
3263 .u64 = index & VHOST_USER_VRING_IDX_MASK,
3271 ctx.msg.payload.area.u64 |= VHOST_USER_VRING_NOFD_MASK;
3277 ret = send_vhost_slave_message(dev, &ctx);
3279 VHOST_LOG_CONFIG(ERR, "(%s) failed to set host notifier (%d)\n",
3284 return process_slave_message_reply(dev, &ctx);
3287 int rte_vhost_host_notifier_ctrl(int vid, uint16_t qid, bool enable)
3289 struct virtio_net *dev;
3290 struct rte_vdpa_device *vdpa_dev;
3291 int vfio_device_fd, ret = 0;
3292 uint64_t offset, size;
3293 unsigned int i, q_start, q_last;
3295 dev = get_device(vid);
3299 vdpa_dev = dev->vdpa_dev;
3300 if (vdpa_dev == NULL)
3303 if (!(dev->features & (1ULL << VIRTIO_F_VERSION_1)) ||
3304 !(dev->features & (1ULL << VHOST_USER_F_PROTOCOL_FEATURES)) ||
3305 !(dev->protocol_features &
3306 (1ULL << VHOST_USER_PROTOCOL_F_SLAVE_REQ)) ||
3307 !(dev->protocol_features &
3308 (1ULL << VHOST_USER_PROTOCOL_F_SLAVE_SEND_FD)) ||
3309 !(dev->protocol_features &
3310 (1ULL << VHOST_USER_PROTOCOL_F_HOST_NOTIFIER)))
3313 if (qid == RTE_VHOST_QUEUE_ALL) {
3315 q_last = dev->nr_vring - 1;
3317 if (qid >= dev->nr_vring)
3323 RTE_FUNC_PTR_OR_ERR_RET(vdpa_dev->ops->get_vfio_device_fd, -ENOTSUP);
3324 RTE_FUNC_PTR_OR_ERR_RET(vdpa_dev->ops->get_notify_area, -ENOTSUP);
3326 vfio_device_fd = vdpa_dev->ops->get_vfio_device_fd(vid);
3327 if (vfio_device_fd < 0)
3331 for (i = q_start; i <= q_last; i++) {
3332 if (vdpa_dev->ops->get_notify_area(vid, i, &offset,
3338 if (vhost_user_slave_set_vring_host_notifier(dev, i,
3339 vfio_device_fd, offset, size) < 0) {
3346 for (i = q_start; i <= q_last; i++) {
3347 vhost_user_slave_set_vring_host_notifier(dev, i, -1,