1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2018 Intel Corporation
7 * The vhost-user protocol connection is an external interface, so it must be
8 * robust against invalid inputs.
10 * This is important because the vhost-user master is only one step removed
11 * from the guest. Malicious guests that have escaped will then launch further
12 * attacks from the vhost-user master.
14 * Even in deployments where guests are trusted, a bug in the vhost-user master
15 * can still cause invalid messages to be sent. Such messages must not
16 * compromise the stability of the DPDK application by causing crashes, memory
17 * corruption, or other problematic behavior.
19 * Do not assume received VhostUserMsg fields contain sensible values!
28 #include <sys/ioctl.h>
30 #include <sys/types.h>
32 #include <sys/syscall.h>
34 #ifdef RTE_LIBRTE_VHOST_NUMA
37 #ifdef RTE_LIBRTE_VHOST_POSTCOPY
38 #include <linux/userfaultfd.h>
40 #ifdef F_ADD_SEALS /* if file sealing is supported, so is memfd */
41 #include <linux/memfd.h>
42 #define MEMFD_SUPPORTED
45 #include <rte_common.h>
46 #include <rte_malloc.h>
49 #include <rte_errno.h>
53 #include "vhost_user.h"
55 #define VIRTIO_MIN_MTU 68
56 #define VIRTIO_MAX_MTU 65535
58 #define INFLIGHT_ALIGNMENT 64
59 #define INFLIGHT_VERSION 0x1
61 static const char *vhost_message_str[VHOST_USER_MAX] = {
62 [VHOST_USER_NONE] = "VHOST_USER_NONE",
63 [VHOST_USER_GET_FEATURES] = "VHOST_USER_GET_FEATURES",
64 [VHOST_USER_SET_FEATURES] = "VHOST_USER_SET_FEATURES",
65 [VHOST_USER_SET_OWNER] = "VHOST_USER_SET_OWNER",
66 [VHOST_USER_RESET_OWNER] = "VHOST_USER_RESET_OWNER",
67 [VHOST_USER_SET_MEM_TABLE] = "VHOST_USER_SET_MEM_TABLE",
68 [VHOST_USER_SET_LOG_BASE] = "VHOST_USER_SET_LOG_BASE",
69 [VHOST_USER_SET_LOG_FD] = "VHOST_USER_SET_LOG_FD",
70 [VHOST_USER_SET_VRING_NUM] = "VHOST_USER_SET_VRING_NUM",
71 [VHOST_USER_SET_VRING_ADDR] = "VHOST_USER_SET_VRING_ADDR",
72 [VHOST_USER_SET_VRING_BASE] = "VHOST_USER_SET_VRING_BASE",
73 [VHOST_USER_GET_VRING_BASE] = "VHOST_USER_GET_VRING_BASE",
74 [VHOST_USER_SET_VRING_KICK] = "VHOST_USER_SET_VRING_KICK",
75 [VHOST_USER_SET_VRING_CALL] = "VHOST_USER_SET_VRING_CALL",
76 [VHOST_USER_SET_VRING_ERR] = "VHOST_USER_SET_VRING_ERR",
77 [VHOST_USER_GET_PROTOCOL_FEATURES] = "VHOST_USER_GET_PROTOCOL_FEATURES",
78 [VHOST_USER_SET_PROTOCOL_FEATURES] = "VHOST_USER_SET_PROTOCOL_FEATURES",
79 [VHOST_USER_GET_QUEUE_NUM] = "VHOST_USER_GET_QUEUE_NUM",
80 [VHOST_USER_SET_VRING_ENABLE] = "VHOST_USER_SET_VRING_ENABLE",
81 [VHOST_USER_SEND_RARP] = "VHOST_USER_SEND_RARP",
82 [VHOST_USER_NET_SET_MTU] = "VHOST_USER_NET_SET_MTU",
83 [VHOST_USER_SET_SLAVE_REQ_FD] = "VHOST_USER_SET_SLAVE_REQ_FD",
84 [VHOST_USER_IOTLB_MSG] = "VHOST_USER_IOTLB_MSG",
85 [VHOST_USER_CRYPTO_CREATE_SESS] = "VHOST_USER_CRYPTO_CREATE_SESS",
86 [VHOST_USER_CRYPTO_CLOSE_SESS] = "VHOST_USER_CRYPTO_CLOSE_SESS",
87 [VHOST_USER_POSTCOPY_ADVISE] = "VHOST_USER_POSTCOPY_ADVISE",
88 [VHOST_USER_POSTCOPY_LISTEN] = "VHOST_USER_POSTCOPY_LISTEN",
89 [VHOST_USER_POSTCOPY_END] = "VHOST_USER_POSTCOPY_END",
90 [VHOST_USER_GET_INFLIGHT_FD] = "VHOST_USER_GET_INFLIGHT_FD",
91 [VHOST_USER_SET_INFLIGHT_FD] = "VHOST_USER_SET_INFLIGHT_FD",
92 [VHOST_USER_SET_STATUS] = "VHOST_USER_SET_STATUS",
93 [VHOST_USER_GET_STATUS] = "VHOST_USER_GET_STATUS",
96 static int send_vhost_reply(struct virtio_net *dev, int sockfd, struct vhu_msg_context *ctx);
97 static int read_vhost_message(struct virtio_net *dev, int sockfd, struct vhu_msg_context *ctx);
100 close_msg_fds(struct vhu_msg_context *ctx)
104 for (i = 0; i < ctx->fd_num; i++) {
105 int fd = ctx->fds[i];
116 * Ensure the expected number of FDs is received,
117 * close all FDs and return an error if this is not the case.
120 validate_msg_fds(struct virtio_net *dev, struct vhu_msg_context *ctx, int expected_fds)
122 if (ctx->fd_num == expected_fds)
125 VHOST_LOG_CONFIG(ERR, "(%s) expect %d FDs for request %s, received %d\n",
126 dev->ifname, expected_fds,
127 vhost_message_str[ctx->msg.request.master],
141 ret = fstat(fd, &stat);
142 return ret == -1 ? (uint64_t)-1 : (uint64_t)stat.st_blksize;
146 async_dma_map(struct virtio_net *dev, struct rte_vhost_mem_region *region, bool do_map)
151 host_iova = rte_mem_virt2iova((void *)(uintptr_t)region->host_user_addr);
153 /* Add mapped region into the default container of DPDK. */
154 ret = rte_vfio_container_dma_map(RTE_VFIO_DEFAULT_CONTAINER_FD,
155 region->host_user_addr,
160 * DMA device may bind with kernel driver, in this case,
161 * we don't need to program IOMMU manually. However, if no
162 * device is bound with vfio/uio in DPDK, and vfio kernel
163 * module is loaded, the API will still be called and return
164 * with ENODEV/ENOSUP.
166 * DPDK vfio only returns ENODEV/ENOSUP in very similar
167 * situations(vfio either unsupported, or supported
168 * but no devices found). Either way, no mappings could be
169 * performed. We treat it as normal case in async path.
171 if (rte_errno == ENODEV || rte_errno == ENOTSUP)
174 VHOST_LOG_CONFIG(ERR, "(%s) DMA engine map failed\n", dev->ifname);
175 /* DMA mapping errors won't stop VHST_USER_SET_MEM_TABLE. */
180 /* Remove mapped region from the default container of DPDK. */
181 ret = rte_vfio_container_dma_unmap(RTE_VFIO_DEFAULT_CONTAINER_FD,
182 region->host_user_addr,
186 /* like DMA map, ignore the kernel driver case when unmap. */
187 if (rte_errno == EINVAL)
190 VHOST_LOG_CONFIG(ERR, "(%s) DMA engine unmap failed\n", dev->ifname);
199 free_mem_region(struct virtio_net *dev)
202 struct rte_vhost_mem_region *reg;
204 if (!dev || !dev->mem)
207 for (i = 0; i < dev->mem->nregions; i++) {
208 reg = &dev->mem->regions[i];
209 if (reg->host_user_addr) {
210 if (dev->async_copy && rte_vfio_is_enabled("vfio"))
211 async_dma_map(dev, reg, false);
213 munmap(reg->mmap_addr, reg->mmap_size);
220 vhost_backend_cleanup(struct virtio_net *dev)
223 free_mem_region(dev);
228 rte_free(dev->guest_pages);
229 dev->guest_pages = NULL;
232 munmap((void *)(uintptr_t)dev->log_addr, dev->log_size);
236 if (dev->inflight_info) {
237 if (dev->inflight_info->addr) {
238 munmap(dev->inflight_info->addr,
239 dev->inflight_info->size);
240 dev->inflight_info->addr = NULL;
243 if (dev->inflight_info->fd >= 0) {
244 close(dev->inflight_info->fd);
245 dev->inflight_info->fd = -1;
248 rte_free(dev->inflight_info);
249 dev->inflight_info = NULL;
252 if (dev->slave_req_fd >= 0) {
253 close(dev->slave_req_fd);
254 dev->slave_req_fd = -1;
257 if (dev->postcopy_ufd >= 0) {
258 close(dev->postcopy_ufd);
259 dev->postcopy_ufd = -1;
262 dev->postcopy_listening = 0;
266 vhost_user_notify_queue_state(struct virtio_net *dev, uint16_t index,
269 struct rte_vdpa_device *vdpa_dev = dev->vdpa_dev;
270 struct vhost_virtqueue *vq = dev->virtqueue[index];
272 /* Configure guest notifications on enable */
273 if (enable && vq->notif_enable != VIRTIO_UNINITIALIZED_NOTIF)
274 vhost_enable_guest_notification(dev, vq, vq->notif_enable);
276 if (vdpa_dev && vdpa_dev->ops->set_vring_state)
277 vdpa_dev->ops->set_vring_state(dev->vid, index, enable);
279 if (dev->notify_ops->vring_state_changed)
280 dev->notify_ops->vring_state_changed(dev->vid,
285 * This function just returns success at the moment unless
286 * the device hasn't been initialised.
289 vhost_user_set_owner(struct virtio_net **pdev,
290 struct vhu_msg_context *ctx,
291 int main_fd __rte_unused)
293 struct virtio_net *dev = *pdev;
295 if (validate_msg_fds(dev, ctx, 0) != 0)
296 return RTE_VHOST_MSG_RESULT_ERR;
298 return RTE_VHOST_MSG_RESULT_OK;
302 vhost_user_reset_owner(struct virtio_net **pdev,
303 struct vhu_msg_context *ctx,
304 int main_fd __rte_unused)
306 struct virtio_net *dev = *pdev;
308 if (validate_msg_fds(dev, ctx, 0) != 0)
309 return RTE_VHOST_MSG_RESULT_ERR;
311 vhost_destroy_device_notify(dev);
313 cleanup_device(dev, 0);
315 return RTE_VHOST_MSG_RESULT_OK;
319 * The features that we support are requested.
322 vhost_user_get_features(struct virtio_net **pdev,
323 struct vhu_msg_context *ctx,
324 int main_fd __rte_unused)
326 struct virtio_net *dev = *pdev;
327 uint64_t features = 0;
329 if (validate_msg_fds(dev, ctx, 0) != 0)
330 return RTE_VHOST_MSG_RESULT_ERR;
332 rte_vhost_driver_get_features(dev->ifname, &features);
334 ctx->msg.payload.u64 = features;
335 ctx->msg.size = sizeof(ctx->msg.payload.u64);
338 return RTE_VHOST_MSG_RESULT_REPLY;
342 * The queue number that we support are requested.
345 vhost_user_get_queue_num(struct virtio_net **pdev,
346 struct vhu_msg_context *ctx,
347 int main_fd __rte_unused)
349 struct virtio_net *dev = *pdev;
350 uint32_t queue_num = 0;
352 if (validate_msg_fds(dev, ctx, 0) != 0)
353 return RTE_VHOST_MSG_RESULT_ERR;
355 rte_vhost_driver_get_queue_num(dev->ifname, &queue_num);
357 ctx->msg.payload.u64 = (uint64_t)queue_num;
358 ctx->msg.size = sizeof(ctx->msg.payload.u64);
361 return RTE_VHOST_MSG_RESULT_REPLY;
365 * We receive the negotiated features supported by us and the virtio device.
368 vhost_user_set_features(struct virtio_net **pdev,
369 struct vhu_msg_context *ctx,
370 int main_fd __rte_unused)
372 struct virtio_net *dev = *pdev;
373 uint64_t features = ctx->msg.payload.u64;
374 uint64_t vhost_features = 0;
375 struct rte_vdpa_device *vdpa_dev;
377 if (validate_msg_fds(dev, ctx, 0) != 0)
378 return RTE_VHOST_MSG_RESULT_ERR;
380 rte_vhost_driver_get_features(dev->ifname, &vhost_features);
381 if (features & ~vhost_features) {
382 VHOST_LOG_CONFIG(ERR, "(%s) received invalid negotiated features.\n",
384 dev->flags |= VIRTIO_DEV_FEATURES_FAILED;
385 dev->status &= ~VIRTIO_DEVICE_STATUS_FEATURES_OK;
387 return RTE_VHOST_MSG_RESULT_ERR;
390 if (dev->flags & VIRTIO_DEV_RUNNING) {
391 if (dev->features == features)
392 return RTE_VHOST_MSG_RESULT_OK;
395 * Error out if master tries to change features while device is
396 * in running state. The exception being VHOST_F_LOG_ALL, which
397 * is enabled when the live-migration starts.
399 if ((dev->features ^ features) & ~(1ULL << VHOST_F_LOG_ALL)) {
400 VHOST_LOG_CONFIG(ERR, "(%s) features changed while device is running.\n",
402 return RTE_VHOST_MSG_RESULT_ERR;
405 if (dev->notify_ops->features_changed)
406 dev->notify_ops->features_changed(dev->vid, features);
409 dev->features = features;
411 ((1ULL << VIRTIO_NET_F_MRG_RXBUF) |
412 (1ULL << VIRTIO_F_VERSION_1) |
413 (1ULL << VIRTIO_F_RING_PACKED))) {
414 dev->vhost_hlen = sizeof(struct virtio_net_hdr_mrg_rxbuf);
416 dev->vhost_hlen = sizeof(struct virtio_net_hdr);
418 VHOST_LOG_CONFIG(INFO, "(%s) negotiated Virtio features: 0x%" PRIx64 "\n",
419 dev->ifname, dev->features);
420 VHOST_LOG_CONFIG(DEBUG, "(%s) mergeable RX buffers %s, virtio 1 %s\n",
422 (dev->features & (1 << VIRTIO_NET_F_MRG_RXBUF)) ? "on" : "off",
423 (dev->features & (1ULL << VIRTIO_F_VERSION_1)) ? "on" : "off");
425 if ((dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET) &&
426 !(dev->features & (1ULL << VIRTIO_NET_F_MQ))) {
428 * Remove all but first queue pair if MQ hasn't been
429 * negotiated. This is safe because the device is not
430 * running at this stage.
432 while (dev->nr_vring > 2) {
433 struct vhost_virtqueue *vq;
435 vq = dev->virtqueue[--dev->nr_vring];
439 dev->virtqueue[dev->nr_vring] = NULL;
441 cleanup_vq_inflight(dev, vq);
446 vdpa_dev = dev->vdpa_dev;
448 vdpa_dev->ops->set_features(dev->vid);
450 dev->flags &= ~VIRTIO_DEV_FEATURES_FAILED;
451 return RTE_VHOST_MSG_RESULT_OK;
455 * The virtio device sends us the size of the descriptor ring.
458 vhost_user_set_vring_num(struct virtio_net **pdev,
459 struct vhu_msg_context *ctx,
460 int main_fd __rte_unused)
462 struct virtio_net *dev = *pdev;
463 struct vhost_virtqueue *vq = dev->virtqueue[ctx->msg.payload.state.index];
465 if (validate_msg_fds(dev, ctx, 0) != 0)
466 return RTE_VHOST_MSG_RESULT_ERR;
468 if (ctx->msg.payload.state.num > 32768) {
469 VHOST_LOG_CONFIG(ERR, "(%s) invalid virtqueue size %u\n",
470 dev->ifname, ctx->msg.payload.state.num);
471 return RTE_VHOST_MSG_RESULT_ERR;
474 vq->size = ctx->msg.payload.state.num;
476 /* VIRTIO 1.0, 2.4 Virtqueues says:
478 * Queue Size value is always a power of 2. The maximum Queue Size
481 * VIRTIO 1.1 2.7 Virtqueues says:
483 * Packed virtqueues support up to 2^15 entries each.
485 if (!vq_is_packed(dev)) {
486 if (vq->size & (vq->size - 1)) {
487 VHOST_LOG_CONFIG(ERR, "(%s) invalid virtqueue size %u\n",
488 dev->ifname, vq->size);
489 return RTE_VHOST_MSG_RESULT_ERR;
493 if (vq_is_packed(dev)) {
494 rte_free(vq->shadow_used_packed);
495 vq->shadow_used_packed = rte_malloc_socket(NULL,
497 sizeof(struct vring_used_elem_packed),
498 RTE_CACHE_LINE_SIZE, vq->numa_node);
499 if (!vq->shadow_used_packed) {
500 VHOST_LOG_CONFIG(ERR,
501 "(%s) failed to allocate memory for shadow used ring.\n",
503 return RTE_VHOST_MSG_RESULT_ERR;
507 rte_free(vq->shadow_used_split);
509 vq->shadow_used_split = rte_malloc_socket(NULL,
510 vq->size * sizeof(struct vring_used_elem),
511 RTE_CACHE_LINE_SIZE, vq->numa_node);
513 if (!vq->shadow_used_split) {
514 VHOST_LOG_CONFIG(ERR,
515 "(%s) failed to allocate memory for vq internal data.\n",
517 return RTE_VHOST_MSG_RESULT_ERR;
521 rte_free(vq->batch_copy_elems);
522 vq->batch_copy_elems = rte_malloc_socket(NULL,
523 vq->size * sizeof(struct batch_copy_elem),
524 RTE_CACHE_LINE_SIZE, vq->numa_node);
525 if (!vq->batch_copy_elems) {
526 VHOST_LOG_CONFIG(ERR, "(%s) failed to allocate memory for batching copy.\n",
528 return RTE_VHOST_MSG_RESULT_ERR;
531 return RTE_VHOST_MSG_RESULT_OK;
535 * Reallocate virtio_dev, vhost_virtqueue and related data structures to
536 * make them on the same numa node as the memory of vring descriptor.
538 #ifdef RTE_LIBRTE_VHOST_NUMA
539 static struct virtio_net*
540 numa_realloc(struct virtio_net *dev, int index)
543 struct virtio_net *old_dev;
544 struct vhost_virtqueue *vq;
545 struct batch_copy_elem *bce;
546 struct guest_page *gp;
547 struct rte_vhost_memory *mem;
552 vq = dev->virtqueue[index];
555 * If VQ is ready, it is too late to reallocate, it certainly already
556 * happened anyway on VHOST_USER_SET_VRING_ADRR.
561 ret = get_mempolicy(&node, NULL, 0, vq->desc, MPOL_F_NODE | MPOL_F_ADDR);
563 VHOST_LOG_CONFIG(ERR, "(%s) unable to get virtqueue %d numa information.\n",
568 if (node == vq->numa_node)
569 goto out_dev_realloc;
571 vq = rte_realloc_socket(vq, sizeof(*vq), 0, node);
573 VHOST_LOG_CONFIG(ERR, "(%s) failed to realloc virtqueue %d on node %d\n",
574 dev->ifname, index, node);
578 if (vq != dev->virtqueue[index]) {
579 VHOST_LOG_CONFIG(INFO, "(%s) reallocated virtqueue on node %d\n",
581 dev->virtqueue[index] = vq;
582 vhost_user_iotlb_init(dev, index);
585 if (vq_is_packed(dev)) {
586 struct vring_used_elem_packed *sup;
588 sup = rte_realloc_socket(vq->shadow_used_packed, vq->size * sizeof(*sup),
589 RTE_CACHE_LINE_SIZE, node);
591 VHOST_LOG_CONFIG(ERR, "(%s) failed to realloc shadow packed on node %d\n",
595 vq->shadow_used_packed = sup;
597 struct vring_used_elem *sus;
599 sus = rte_realloc_socket(vq->shadow_used_split, vq->size * sizeof(*sus),
600 RTE_CACHE_LINE_SIZE, node);
602 VHOST_LOG_CONFIG(ERR, "(%s) failed to realloc shadow split on node %d\n",
606 vq->shadow_used_split = sus;
609 bce = rte_realloc_socket(vq->batch_copy_elems, vq->size * sizeof(*bce),
610 RTE_CACHE_LINE_SIZE, node);
612 VHOST_LOG_CONFIG(ERR, "(%s) failed to realloc batch copy elem on node %d\n",
616 vq->batch_copy_elems = bce;
619 struct log_cache_entry *lc;
621 lc = rte_realloc_socket(vq->log_cache, sizeof(*lc) * VHOST_LOG_CACHE_NR, 0, node);
623 VHOST_LOG_CONFIG(ERR, "(%s) failed to realloc log cache on node %d\n",
630 if (vq->resubmit_inflight) {
631 struct rte_vhost_resubmit_info *ri;
633 ri = rte_realloc_socket(vq->resubmit_inflight, sizeof(*ri), 0, node);
635 VHOST_LOG_CONFIG(ERR, "(%s) failed to realloc resubmit inflight on node %d\n",
639 vq->resubmit_inflight = ri;
641 if (ri->resubmit_list) {
642 struct rte_vhost_resubmit_desc *rd;
644 rd = rte_realloc_socket(ri->resubmit_list, sizeof(*rd) * ri->resubmit_num,
647 VHOST_LOG_CONFIG(ERR, "(%s) failed to realloc resubmit list on node %d\n",
651 ri->resubmit_list = rd;
655 vq->numa_node = node;
659 if (dev->flags & VIRTIO_DEV_RUNNING)
662 ret = get_mempolicy(&dev_node, NULL, 0, dev, MPOL_F_NODE | MPOL_F_ADDR);
664 VHOST_LOG_CONFIG(ERR, "(%s) unable to get numa information.\n", dev->ifname);
668 if (dev_node == node)
671 dev = rte_realloc_socket(old_dev, sizeof(*dev), 0, node);
673 VHOST_LOG_CONFIG(ERR, "(%s) failed to realloc dev on node %d\n",
678 VHOST_LOG_CONFIG(INFO, "(%s) reallocated device on node %d\n", dev->ifname, node);
679 vhost_devices[dev->vid] = dev;
681 mem_size = sizeof(struct rte_vhost_memory) +
682 sizeof(struct rte_vhost_mem_region) * dev->mem->nregions;
683 mem = rte_realloc_socket(dev->mem, mem_size, 0, node);
685 VHOST_LOG_CONFIG(ERR, "(%s) failed to realloc mem table on node %d\n",
691 gp = rte_realloc_socket(dev->guest_pages, dev->max_guest_pages * sizeof(*gp),
692 RTE_CACHE_LINE_SIZE, node);
694 VHOST_LOG_CONFIG(ERR, "(%s) failed to realloc guest pages on node %d\n",
698 dev->guest_pages = gp;
703 static struct virtio_net*
704 numa_realloc(struct virtio_net *dev, int index __rte_unused)
710 /* Converts QEMU virtual address to Vhost virtual address. */
712 qva_to_vva(struct virtio_net *dev, uint64_t qva, uint64_t *len)
714 struct rte_vhost_mem_region *r;
717 if (unlikely(!dev || !dev->mem))
720 /* Find the region where the address lives. */
721 for (i = 0; i < dev->mem->nregions; i++) {
722 r = &dev->mem->regions[i];
724 if (qva >= r->guest_user_addr &&
725 qva < r->guest_user_addr + r->size) {
727 if (unlikely(*len > r->guest_user_addr + r->size - qva))
728 *len = r->guest_user_addr + r->size - qva;
730 return qva - r->guest_user_addr +
742 * Converts ring address to Vhost virtual address.
743 * If IOMMU is enabled, the ring address is a guest IO virtual address,
744 * else it is a QEMU virtual address.
747 ring_addr_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
748 uint64_t ra, uint64_t *size)
750 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)) {
753 vhost_user_iotlb_rd_lock(vq);
754 vva = vhost_iova_to_vva(dev, vq, ra,
755 size, VHOST_ACCESS_RW);
756 vhost_user_iotlb_rd_unlock(vq);
761 return qva_to_vva(dev, ra, size);
765 log_addr_to_gpa(struct virtio_net *dev, struct vhost_virtqueue *vq)
769 vhost_user_iotlb_rd_lock(vq);
770 log_gpa = translate_log_addr(dev, vq, vq->ring_addrs.log_guest_addr);
771 vhost_user_iotlb_rd_unlock(vq);
776 static struct virtio_net *
777 translate_ring_addresses(struct virtio_net *dev, int vq_index)
779 struct vhost_virtqueue *vq = dev->virtqueue[vq_index];
780 struct vhost_vring_addr *addr = &vq->ring_addrs;
781 uint64_t len, expected_len;
783 if (addr->flags & (1 << VHOST_VRING_F_LOG)) {
785 log_addr_to_gpa(dev, vq);
786 if (vq->log_guest_addr == 0) {
787 VHOST_LOG_CONFIG(DEBUG, "(%s) failed to map log_guest_addr.\n",
793 if (vq_is_packed(dev)) {
794 len = sizeof(struct vring_packed_desc) * vq->size;
795 vq->desc_packed = (struct vring_packed_desc *)(uintptr_t)
796 ring_addr_to_vva(dev, vq, addr->desc_user_addr, &len);
797 if (vq->desc_packed == NULL ||
798 len != sizeof(struct vring_packed_desc) *
800 VHOST_LOG_CONFIG(DEBUG, "(%s) failed to map desc_packed ring.\n",
805 dev = numa_realloc(dev, vq_index);
806 vq = dev->virtqueue[vq_index];
807 addr = &vq->ring_addrs;
809 len = sizeof(struct vring_packed_desc_event);
810 vq->driver_event = (struct vring_packed_desc_event *)
811 (uintptr_t)ring_addr_to_vva(dev,
812 vq, addr->avail_user_addr, &len);
813 if (vq->driver_event == NULL ||
814 len != sizeof(struct vring_packed_desc_event)) {
815 VHOST_LOG_CONFIG(DEBUG, "(%s) failed to find driver area address.\n",
820 len = sizeof(struct vring_packed_desc_event);
821 vq->device_event = (struct vring_packed_desc_event *)
822 (uintptr_t)ring_addr_to_vva(dev,
823 vq, addr->used_user_addr, &len);
824 if (vq->device_event == NULL ||
825 len != sizeof(struct vring_packed_desc_event)) {
826 VHOST_LOG_CONFIG(DEBUG, "(%s) failed to find device area address.\n",
831 vq->access_ok = true;
835 /* The addresses are converted from QEMU virtual to Vhost virtual. */
836 if (vq->desc && vq->avail && vq->used)
839 len = sizeof(struct vring_desc) * vq->size;
840 vq->desc = (struct vring_desc *)(uintptr_t)ring_addr_to_vva(dev,
841 vq, addr->desc_user_addr, &len);
842 if (vq->desc == 0 || len != sizeof(struct vring_desc) * vq->size) {
843 VHOST_LOG_CONFIG(DEBUG, "(%s) failed to map desc ring.\n", dev->ifname);
847 dev = numa_realloc(dev, vq_index);
848 vq = dev->virtqueue[vq_index];
849 addr = &vq->ring_addrs;
851 len = sizeof(struct vring_avail) + sizeof(uint16_t) * vq->size;
852 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))
853 len += sizeof(uint16_t);
855 vq->avail = (struct vring_avail *)(uintptr_t)ring_addr_to_vva(dev,
856 vq, addr->avail_user_addr, &len);
857 if (vq->avail == 0 || len != expected_len) {
858 VHOST_LOG_CONFIG(DEBUG, "(%s) failed to map avail ring.\n", dev->ifname);
862 len = sizeof(struct vring_used) +
863 sizeof(struct vring_used_elem) * vq->size;
864 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))
865 len += sizeof(uint16_t);
867 vq->used = (struct vring_used *)(uintptr_t)ring_addr_to_vva(dev,
868 vq, addr->used_user_addr, &len);
869 if (vq->used == 0 || len != expected_len) {
870 VHOST_LOG_CONFIG(DEBUG, "(%s) failed to map used ring.\n", dev->ifname);
874 if (vq->last_used_idx != vq->used->idx) {
875 VHOST_LOG_CONFIG(WARNING, "(%s) last_used_idx (%u) and vq->used->idx (%u) mismatches;\n",
877 vq->last_used_idx, vq->used->idx);
878 vq->last_used_idx = vq->used->idx;
879 vq->last_avail_idx = vq->used->idx;
880 VHOST_LOG_CONFIG(WARNING, "(%s) some packets maybe resent for Tx and dropped for Rx\n",
884 vq->access_ok = true;
886 VHOST_LOG_CONFIG(DEBUG, "(%s) mapped address desc: %p\n", dev->ifname, vq->desc);
887 VHOST_LOG_CONFIG(DEBUG, "(%s) mapped address avail: %p\n", dev->ifname, vq->avail);
888 VHOST_LOG_CONFIG(DEBUG, "(%s) mapped address used: %p\n", dev->ifname, vq->used);
889 VHOST_LOG_CONFIG(DEBUG, "(%s) log_guest_addr: %" PRIx64 "\n",
890 dev->ifname, vq->log_guest_addr);
896 * The virtio device sends us the desc, used and avail ring addresses.
897 * This function then converts these to our address space.
900 vhost_user_set_vring_addr(struct virtio_net **pdev,
901 struct vhu_msg_context *ctx,
902 int main_fd __rte_unused)
904 struct virtio_net *dev = *pdev;
905 struct vhost_virtqueue *vq;
906 struct vhost_vring_addr *addr = &ctx->msg.payload.addr;
909 if (validate_msg_fds(dev, ctx, 0) != 0)
910 return RTE_VHOST_MSG_RESULT_ERR;
912 if (dev->mem == NULL)
913 return RTE_VHOST_MSG_RESULT_ERR;
915 /* addr->index refers to the queue index. The txq 1, rxq is 0. */
916 vq = dev->virtqueue[ctx->msg.payload.addr.index];
918 access_ok = vq->access_ok;
921 * Rings addresses should not be interpreted as long as the ring is not
922 * started and enabled
924 memcpy(&vq->ring_addrs, addr, sizeof(*addr));
926 vring_invalidate(dev, vq);
928 if ((vq->enabled && (dev->features &
929 (1ULL << VHOST_USER_F_PROTOCOL_FEATURES))) ||
931 dev = translate_ring_addresses(dev, ctx->msg.payload.addr.index);
933 return RTE_VHOST_MSG_RESULT_ERR;
938 return RTE_VHOST_MSG_RESULT_OK;
942 * The virtio device sends us the available ring last used index.
945 vhost_user_set_vring_base(struct virtio_net **pdev,
946 struct vhu_msg_context *ctx,
947 int main_fd __rte_unused)
949 struct virtio_net *dev = *pdev;
950 struct vhost_virtqueue *vq = dev->virtqueue[ctx->msg.payload.state.index];
951 uint64_t val = ctx->msg.payload.state.num;
953 if (validate_msg_fds(dev, ctx, 0) != 0)
954 return RTE_VHOST_MSG_RESULT_ERR;
956 if (vq_is_packed(dev)) {
958 * Bit[0:14]: avail index
959 * Bit[15]: avail wrap counter
961 vq->last_avail_idx = val & 0x7fff;
962 vq->avail_wrap_counter = !!(val & (0x1 << 15));
964 * Set used index to same value as available one, as
965 * their values should be the same since ring processing
966 * was stopped at get time.
968 vq->last_used_idx = vq->last_avail_idx;
969 vq->used_wrap_counter = vq->avail_wrap_counter;
971 vq->last_used_idx = ctx->msg.payload.state.num;
972 vq->last_avail_idx = ctx->msg.payload.state.num;
975 VHOST_LOG_CONFIG(INFO,
976 "(%s) vring base idx:%u last_used_idx:%u last_avail_idx:%u.\n",
977 dev->ifname, ctx->msg.payload.state.index, vq->last_used_idx,
980 return RTE_VHOST_MSG_RESULT_OK;
984 add_one_guest_page(struct virtio_net *dev, uint64_t guest_phys_addr,
985 uint64_t host_phys_addr, uint64_t size)
987 struct guest_page *page, *last_page;
988 struct guest_page *old_pages;
990 if (dev->nr_guest_pages == dev->max_guest_pages) {
991 dev->max_guest_pages *= 2;
992 old_pages = dev->guest_pages;
993 dev->guest_pages = rte_realloc(dev->guest_pages,
994 dev->max_guest_pages * sizeof(*page),
995 RTE_CACHE_LINE_SIZE);
996 if (dev->guest_pages == NULL) {
997 VHOST_LOG_CONFIG(ERR, "(%s) cannot realloc guest_pages\n", dev->ifname);
1003 if (dev->nr_guest_pages > 0) {
1004 last_page = &dev->guest_pages[dev->nr_guest_pages - 1];
1005 /* merge if the two pages are continuous */
1006 if (host_phys_addr == last_page->host_phys_addr +
1008 last_page->size += size;
1013 page = &dev->guest_pages[dev->nr_guest_pages++];
1014 page->guest_phys_addr = guest_phys_addr;
1015 page->host_phys_addr = host_phys_addr;
1022 add_guest_pages(struct virtio_net *dev, struct rte_vhost_mem_region *reg,
1025 uint64_t reg_size = reg->size;
1026 uint64_t host_user_addr = reg->host_user_addr;
1027 uint64_t guest_phys_addr = reg->guest_phys_addr;
1028 uint64_t host_phys_addr;
1031 host_phys_addr = rte_mem_virt2iova((void *)(uintptr_t)host_user_addr);
1032 size = page_size - (guest_phys_addr & (page_size - 1));
1033 size = RTE_MIN(size, reg_size);
1035 if (add_one_guest_page(dev, guest_phys_addr, host_phys_addr, size) < 0)
1038 host_user_addr += size;
1039 guest_phys_addr += size;
1042 while (reg_size > 0) {
1043 size = RTE_MIN(reg_size, page_size);
1044 host_phys_addr = rte_mem_virt2iova((void *)(uintptr_t)
1046 if (add_one_guest_page(dev, guest_phys_addr, host_phys_addr,
1050 host_user_addr += size;
1051 guest_phys_addr += size;
1055 /* sort guest page array if over binary search threshold */
1056 if (dev->nr_guest_pages >= VHOST_BINARY_SEARCH_THRESH) {
1057 qsort((void *)dev->guest_pages, dev->nr_guest_pages,
1058 sizeof(struct guest_page), guest_page_addrcmp);
1064 #ifdef RTE_LIBRTE_VHOST_DEBUG
1065 /* TODO: enable it only in debug mode? */
1067 dump_guest_pages(struct virtio_net *dev)
1070 struct guest_page *page;
1072 for (i = 0; i < dev->nr_guest_pages; i++) {
1073 page = &dev->guest_pages[i];
1075 VHOST_LOG_CONFIG(INFO, "(%s) guest physical page region %u\n",
1077 VHOST_LOG_CONFIG(INFO, "(%s)\tguest_phys_addr: %" PRIx64 "\n",
1078 dev->ifname, page->guest_phys_addr);
1079 VHOST_LOG_CONFIG(INFO, "(%s)\thost_phys_addr : %" PRIx64 "\n",
1080 dev->ifname, page->host_phys_addr);
1081 VHOST_LOG_CONFIG(INFO, "(%s)\tsize : %" PRIx64 "\n",
1082 dev->ifname, page->size);
1086 #define dump_guest_pages(dev)
1090 vhost_memory_changed(struct VhostUserMemory *new,
1091 struct rte_vhost_memory *old)
1095 if (new->nregions != old->nregions)
1098 for (i = 0; i < new->nregions; ++i) {
1099 VhostUserMemoryRegion *new_r = &new->regions[i];
1100 struct rte_vhost_mem_region *old_r = &old->regions[i];
1102 if (new_r->guest_phys_addr != old_r->guest_phys_addr)
1104 if (new_r->memory_size != old_r->size)
1106 if (new_r->userspace_addr != old_r->guest_user_addr)
1113 #ifdef RTE_LIBRTE_VHOST_POSTCOPY
1115 vhost_user_postcopy_region_register(struct virtio_net *dev,
1116 struct rte_vhost_mem_region *reg)
1118 struct uffdio_register reg_struct;
1121 * Let's register all the mmapped area to ensure
1122 * alignment on page boundary.
1124 reg_struct.range.start = (uint64_t)(uintptr_t)reg->mmap_addr;
1125 reg_struct.range.len = reg->mmap_size;
1126 reg_struct.mode = UFFDIO_REGISTER_MODE_MISSING;
1128 if (ioctl(dev->postcopy_ufd, UFFDIO_REGISTER,
1130 VHOST_LOG_CONFIG(ERR, "(%s) failed to register ufd for region "
1131 "%" PRIx64 " - %" PRIx64 " (ufd = %d) %s\n",
1133 (uint64_t)reg_struct.range.start,
1134 (uint64_t)reg_struct.range.start +
1135 (uint64_t)reg_struct.range.len - 1,
1141 VHOST_LOG_CONFIG(INFO,
1142 "(%s)\t userfaultfd registered for range : %" PRIx64 " - %" PRIx64 "\n",
1144 (uint64_t)reg_struct.range.start,
1145 (uint64_t)reg_struct.range.start +
1146 (uint64_t)reg_struct.range.len - 1);
1152 vhost_user_postcopy_region_register(struct virtio_net *dev __rte_unused,
1153 struct rte_vhost_mem_region *reg __rte_unused)
1160 vhost_user_postcopy_register(struct virtio_net *dev, int main_fd,
1161 struct vhu_msg_context *ctx)
1163 struct VhostUserMemory *memory;
1164 struct rte_vhost_mem_region *reg;
1165 struct vhu_msg_context ack_ctx;
1168 if (!dev->postcopy_listening)
1172 * We haven't a better way right now than sharing
1173 * DPDK's virtual address with Qemu, so that Qemu can
1174 * retrieve the region offset when handling userfaults.
1176 memory = &ctx->msg.payload.memory;
1177 for (i = 0; i < memory->nregions; i++) {
1178 reg = &dev->mem->regions[i];
1179 memory->regions[i].userspace_addr = reg->host_user_addr;
1182 /* Send the addresses back to qemu */
1184 send_vhost_reply(dev, main_fd, ctx);
1186 /* Wait for qemu to acknowledge it got the addresses
1187 * we've got to wait before we're allowed to generate faults.
1189 if (read_vhost_message(dev, main_fd, &ack_ctx) <= 0) {
1190 VHOST_LOG_CONFIG(ERR, "(%s) failed to read qemu ack on postcopy set-mem-table\n",
1195 if (validate_msg_fds(dev, &ack_ctx, 0) != 0)
1198 if (ack_ctx.msg.request.master != VHOST_USER_SET_MEM_TABLE) {
1199 VHOST_LOG_CONFIG(ERR, "(%s) bad qemu ack on postcopy set-mem-table (%d)\n",
1200 dev->ifname, ack_ctx.msg.request.master);
1204 /* Now userfault register and we can use the memory */
1205 for (i = 0; i < memory->nregions; i++) {
1206 reg = &dev->mem->regions[i];
1207 if (vhost_user_postcopy_region_register(dev, reg) < 0)
1215 vhost_user_mmap_region(struct virtio_net *dev,
1216 struct rte_vhost_mem_region *region,
1217 uint64_t mmap_offset)
1225 /* Check for memory_size + mmap_offset overflow */
1226 if (mmap_offset >= -region->size) {
1227 VHOST_LOG_CONFIG(ERR, "(%s) mmap_offset (%#"PRIx64") and memory_size (%#"PRIx64") overflow\n",
1228 dev->ifname, mmap_offset, region->size);
1232 mmap_size = region->size + mmap_offset;
1234 /* mmap() without flag of MAP_ANONYMOUS, should be called with length
1235 * argument aligned with hugepagesz at older longterm version Linux,
1236 * like 2.6.32 and 3.2.72, or mmap() will fail with EINVAL.
1238 * To avoid failure, make sure in caller to keep length aligned.
1240 alignment = get_blk_size(region->fd);
1241 if (alignment == (uint64_t)-1) {
1242 VHOST_LOG_CONFIG(ERR, "(%s) couldn't get hugepage size through fstat\n",
1246 mmap_size = RTE_ALIGN_CEIL(mmap_size, alignment);
1247 if (mmap_size == 0) {
1249 * It could happen if initial mmap_size + alignment overflows
1250 * the sizeof uint64, which could happen if either mmap_size or
1251 * alignment value is wrong.
1253 * mmap() kernel implementation would return an error, but
1254 * better catch it before and provide useful info in the logs.
1256 VHOST_LOG_CONFIG(ERR, "(%s) mmap size (0x%" PRIx64 ") or alignment (0x%" PRIx64 ") is invalid\n",
1257 dev->ifname, region->size + mmap_offset, alignment);
1261 populate = dev->async_copy ? MAP_POPULATE : 0;
1262 mmap_addr = mmap(NULL, mmap_size, PROT_READ | PROT_WRITE,
1263 MAP_SHARED | populate, region->fd, 0);
1265 if (mmap_addr == MAP_FAILED) {
1266 VHOST_LOG_CONFIG(ERR, "(%s) mmap failed (%s).\n", dev->ifname, strerror(errno));
1270 region->mmap_addr = mmap_addr;
1271 region->mmap_size = mmap_size;
1272 region->host_user_addr = (uint64_t)(uintptr_t)mmap_addr + mmap_offset;
1274 if (dev->async_copy) {
1275 if (add_guest_pages(dev, region, alignment) < 0) {
1276 VHOST_LOG_CONFIG(ERR, "(%s) adding guest pages to region failed.\n",
1281 if (rte_vfio_is_enabled("vfio")) {
1282 ret = async_dma_map(dev, region, true);
1284 VHOST_LOG_CONFIG(ERR,
1285 "(%s) configure IOMMU for DMA engine failed\n",
1292 VHOST_LOG_CONFIG(INFO, "(%s) guest memory region size: 0x%" PRIx64 "\n",
1293 dev->ifname, region->size);
1294 VHOST_LOG_CONFIG(INFO, "(%s)\t guest physical addr: 0x%" PRIx64 "\n",
1295 dev->ifname, region->guest_phys_addr);
1296 VHOST_LOG_CONFIG(INFO, "(%s)\t guest virtual addr: 0x%" PRIx64 "\n",
1297 dev->ifname, region->guest_user_addr);
1298 VHOST_LOG_CONFIG(INFO, "(%s)\t host virtual addr: 0x%" PRIx64 "\n",
1299 dev->ifname, region->host_user_addr);
1300 VHOST_LOG_CONFIG(INFO, "(%s)\t mmap addr : 0x%" PRIx64 "\n",
1301 dev->ifname, (uint64_t)(uintptr_t)mmap_addr);
1302 VHOST_LOG_CONFIG(INFO, "(%s)\t mmap size : 0x%" PRIx64 "\n",
1303 dev->ifname, mmap_size);
1304 VHOST_LOG_CONFIG(INFO, "(%s)\t mmap align: 0x%" PRIx64 "\n",
1305 dev->ifname, alignment);
1306 VHOST_LOG_CONFIG(INFO, "(%s)\t mmap off : 0x%" PRIx64 "\n",
1307 dev->ifname, mmap_offset);
1313 vhost_user_set_mem_table(struct virtio_net **pdev,
1314 struct vhu_msg_context *ctx,
1317 struct virtio_net *dev = *pdev;
1318 struct VhostUserMemory *memory = &ctx->msg.payload.memory;
1319 struct rte_vhost_mem_region *reg;
1320 int numa_node = SOCKET_ID_ANY;
1321 uint64_t mmap_offset;
1323 bool async_notify = false;
1325 if (validate_msg_fds(dev, ctx, memory->nregions) != 0)
1326 return RTE_VHOST_MSG_RESULT_ERR;
1328 if (memory->nregions > VHOST_MEMORY_MAX_NREGIONS) {
1329 VHOST_LOG_CONFIG(ERR, "(%s) too many memory regions (%u)\n",
1330 dev->ifname, memory->nregions);
1334 if (dev->mem && !vhost_memory_changed(memory, dev->mem)) {
1335 VHOST_LOG_CONFIG(INFO, "(%s) memory regions not changed\n", dev->ifname);
1339 return RTE_VHOST_MSG_RESULT_OK;
1343 if (dev->flags & VIRTIO_DEV_VDPA_CONFIGURED) {
1344 struct rte_vdpa_device *vdpa_dev = dev->vdpa_dev;
1346 if (vdpa_dev && vdpa_dev->ops->dev_close)
1347 vdpa_dev->ops->dev_close(dev->vid);
1348 dev->flags &= ~VIRTIO_DEV_VDPA_CONFIGURED;
1351 /* notify the vhost application to stop DMA transfers */
1352 if (dev->async_copy && dev->notify_ops->vring_state_changed) {
1353 for (i = 0; i < dev->nr_vring; i++) {
1354 dev->notify_ops->vring_state_changed(dev->vid,
1357 async_notify = true;
1360 free_mem_region(dev);
1365 /* Flush IOTLB cache as previous HVAs are now invalid */
1366 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1367 for (i = 0; i < dev->nr_vring; i++)
1368 vhost_user_iotlb_flush_all(dev->virtqueue[i]);
1371 * If VQ 0 has already been allocated, try to allocate on the same
1372 * NUMA node. It can be reallocated later in numa_realloc().
1374 if (dev->nr_vring > 0)
1375 numa_node = dev->virtqueue[0]->numa_node;
1377 dev->nr_guest_pages = 0;
1378 if (dev->guest_pages == NULL) {
1379 dev->max_guest_pages = 8;
1380 dev->guest_pages = rte_zmalloc_socket(NULL,
1381 dev->max_guest_pages *
1382 sizeof(struct guest_page),
1383 RTE_CACHE_LINE_SIZE,
1385 if (dev->guest_pages == NULL) {
1386 VHOST_LOG_CONFIG(ERR,
1387 "(%s) failed to allocate memory for dev->guest_pages\n",
1393 dev->mem = rte_zmalloc_socket("vhost-mem-table", sizeof(struct rte_vhost_memory) +
1394 sizeof(struct rte_vhost_mem_region) * memory->nregions, 0, numa_node);
1395 if (dev->mem == NULL) {
1396 VHOST_LOG_CONFIG(ERR,
1397 "(%s) failed to allocate memory for dev->mem\n",
1399 goto free_guest_pages;
1402 for (i = 0; i < memory->nregions; i++) {
1403 reg = &dev->mem->regions[i];
1405 reg->guest_phys_addr = memory->regions[i].guest_phys_addr;
1406 reg->guest_user_addr = memory->regions[i].userspace_addr;
1407 reg->size = memory->regions[i].memory_size;
1408 reg->fd = ctx->fds[i];
1411 * Assign invalid file descriptor value to avoid double
1412 * closing on error path.
1416 mmap_offset = memory->regions[i].mmap_offset;
1418 if (vhost_user_mmap_region(dev, reg, mmap_offset) < 0) {
1419 VHOST_LOG_CONFIG(ERR, "(%s) failed to mmap region %u\n", dev->ifname, i);
1420 goto free_mem_table;
1423 dev->mem->nregions++;
1426 if (vhost_user_postcopy_register(dev, main_fd, ctx) < 0)
1427 goto free_mem_table;
1429 for (i = 0; i < dev->nr_vring; i++) {
1430 struct vhost_virtqueue *vq = dev->virtqueue[i];
1435 if (vq->desc || vq->avail || vq->used) {
1437 * If the memory table got updated, the ring addresses
1438 * need to be translated again as virtual addresses have
1441 vring_invalidate(dev, vq);
1443 dev = translate_ring_addresses(dev, i);
1446 goto free_mem_table;
1453 dump_guest_pages(dev);
1456 for (i = 0; i < dev->nr_vring; i++)
1457 dev->notify_ops->vring_state_changed(dev->vid, i, 1);
1460 return RTE_VHOST_MSG_RESULT_OK;
1463 free_mem_region(dev);
1468 rte_free(dev->guest_pages);
1469 dev->guest_pages = NULL;
1472 return RTE_VHOST_MSG_RESULT_ERR;
1476 vq_is_ready(struct virtio_net *dev, struct vhost_virtqueue *vq)
1483 if (vq_is_packed(dev))
1484 rings_ok = vq->desc_packed && vq->driver_event &&
1487 rings_ok = vq->desc && vq->avail && vq->used;
1490 vq->kickfd != VIRTIO_UNINITIALIZED_EVENTFD &&
1491 vq->callfd != VIRTIO_UNINITIALIZED_EVENTFD &&
1495 #define VIRTIO_BUILTIN_NUM_VQS_TO_BE_READY 2u
1498 virtio_is_ready(struct virtio_net *dev)
1500 struct vhost_virtqueue *vq;
1501 uint32_t i, nr_vring = dev->nr_vring;
1503 if (dev->flags & VIRTIO_DEV_READY)
1509 if (dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET) {
1510 nr_vring = VIRTIO_BUILTIN_NUM_VQS_TO_BE_READY;
1512 if (dev->nr_vring < nr_vring)
1516 for (i = 0; i < nr_vring; i++) {
1517 vq = dev->virtqueue[i];
1519 if (!vq_is_ready(dev, vq))
1523 /* If supported, ensure the frontend is really done with config */
1524 if (dev->protocol_features & (1ULL << VHOST_USER_PROTOCOL_F_STATUS))
1525 if (!(dev->status & VIRTIO_DEVICE_STATUS_DRIVER_OK))
1528 dev->flags |= VIRTIO_DEV_READY;
1530 if (!(dev->flags & VIRTIO_DEV_RUNNING))
1531 VHOST_LOG_CONFIG(INFO, "(%s) virtio is now ready for processing.\n", dev->ifname);
1536 inflight_mem_alloc(struct virtio_net *dev, const char *name, size_t size, int *fd)
1540 char fname[20] = "/tmp/memfd-XXXXXX";
1543 #ifdef MEMFD_SUPPORTED
1544 mfd = memfd_create(name, MFD_CLOEXEC);
1549 mfd = mkstemp(fname);
1551 VHOST_LOG_CONFIG(ERR, "(%s) failed to get inflight buffer fd\n",
1559 if (ftruncate(mfd, size) == -1) {
1560 VHOST_LOG_CONFIG(ERR, "(%s) failed to alloc inflight buffer\n", dev->ifname);
1565 ptr = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, mfd, 0);
1566 if (ptr == MAP_FAILED) {
1567 VHOST_LOG_CONFIG(ERR, "(%s) failed to mmap inflight buffer\n", dev->ifname);
1577 get_pervq_shm_size_split(uint16_t queue_size)
1579 return RTE_ALIGN_MUL_CEIL(sizeof(struct rte_vhost_inflight_desc_split) *
1580 queue_size + sizeof(uint64_t) +
1581 sizeof(uint16_t) * 4, INFLIGHT_ALIGNMENT);
1585 get_pervq_shm_size_packed(uint16_t queue_size)
1587 return RTE_ALIGN_MUL_CEIL(sizeof(struct rte_vhost_inflight_desc_packed)
1588 * queue_size + sizeof(uint64_t) +
1589 sizeof(uint16_t) * 6 + sizeof(uint8_t) * 9,
1590 INFLIGHT_ALIGNMENT);
1594 vhost_user_get_inflight_fd(struct virtio_net **pdev,
1595 struct vhu_msg_context *ctx,
1596 int main_fd __rte_unused)
1598 struct rte_vhost_inflight_info_packed *inflight_packed;
1599 uint64_t pervq_inflight_size, mmap_size;
1600 uint16_t num_queues, queue_size;
1601 struct virtio_net *dev = *pdev;
1603 int numa_node = SOCKET_ID_ANY;
1606 if (ctx->msg.size != sizeof(ctx->msg.payload.inflight)) {
1607 VHOST_LOG_CONFIG(ERR, "(%s) invalid get_inflight_fd message size is %d\n",
1608 dev->ifname, ctx->msg.size);
1609 return RTE_VHOST_MSG_RESULT_ERR;
1613 * If VQ 0 has already been allocated, try to allocate on the same
1614 * NUMA node. It can be reallocated later in numa_realloc().
1616 if (dev->nr_vring > 0)
1617 numa_node = dev->virtqueue[0]->numa_node;
1619 if (dev->inflight_info == NULL) {
1620 dev->inflight_info = rte_zmalloc_socket("inflight_info",
1621 sizeof(struct inflight_mem_info), 0, numa_node);
1622 if (!dev->inflight_info) {
1623 VHOST_LOG_CONFIG(ERR, "(%s) failed to alloc dev inflight area\n",
1625 return RTE_VHOST_MSG_RESULT_ERR;
1627 dev->inflight_info->fd = -1;
1630 num_queues = ctx->msg.payload.inflight.num_queues;
1631 queue_size = ctx->msg.payload.inflight.queue_size;
1633 VHOST_LOG_CONFIG(INFO, "(%s) get_inflight_fd num_queues: %u\n",
1634 dev->ifname, ctx->msg.payload.inflight.num_queues);
1635 VHOST_LOG_CONFIG(INFO, "(%s) get_inflight_fd queue_size: %u\n",
1636 dev->ifname, ctx->msg.payload.inflight.queue_size);
1638 if (vq_is_packed(dev))
1639 pervq_inflight_size = get_pervq_shm_size_packed(queue_size);
1641 pervq_inflight_size = get_pervq_shm_size_split(queue_size);
1643 mmap_size = num_queues * pervq_inflight_size;
1644 addr = inflight_mem_alloc(dev, "vhost-inflight", mmap_size, &fd);
1646 VHOST_LOG_CONFIG(ERR, "(%s) failed to alloc vhost inflight area\n", dev->ifname);
1647 ctx->msg.payload.inflight.mmap_size = 0;
1648 return RTE_VHOST_MSG_RESULT_ERR;
1650 memset(addr, 0, mmap_size);
1652 if (dev->inflight_info->addr) {
1653 munmap(dev->inflight_info->addr, dev->inflight_info->size);
1654 dev->inflight_info->addr = NULL;
1657 if (dev->inflight_info->fd >= 0) {
1658 close(dev->inflight_info->fd);
1659 dev->inflight_info->fd = -1;
1662 dev->inflight_info->addr = addr;
1663 dev->inflight_info->size = ctx->msg.payload.inflight.mmap_size = mmap_size;
1664 dev->inflight_info->fd = ctx->fds[0] = fd;
1665 ctx->msg.payload.inflight.mmap_offset = 0;
1668 if (vq_is_packed(dev)) {
1669 for (i = 0; i < num_queues; i++) {
1671 (struct rte_vhost_inflight_info_packed *)addr;
1672 inflight_packed->used_wrap_counter = 1;
1673 inflight_packed->old_used_wrap_counter = 1;
1674 for (j = 0; j < queue_size; j++)
1675 inflight_packed->desc[j].next = j + 1;
1676 addr = (void *)((char *)addr + pervq_inflight_size);
1680 VHOST_LOG_CONFIG(INFO, "(%s) send inflight mmap_size: %"PRIu64"\n",
1681 dev->ifname, ctx->msg.payload.inflight.mmap_size);
1682 VHOST_LOG_CONFIG(INFO, "(%s) send inflight mmap_offset: %"PRIu64"\n",
1683 dev->ifname, ctx->msg.payload.inflight.mmap_offset);
1684 VHOST_LOG_CONFIG(INFO, "(%s) send inflight fd: %d\n", dev->ifname, ctx->fds[0]);
1686 return RTE_VHOST_MSG_RESULT_REPLY;
1690 vhost_user_set_inflight_fd(struct virtio_net **pdev,
1691 struct vhu_msg_context *ctx,
1692 int main_fd __rte_unused)
1694 uint64_t mmap_size, mmap_offset;
1695 uint16_t num_queues, queue_size;
1696 struct virtio_net *dev = *pdev;
1697 uint32_t pervq_inflight_size;
1698 struct vhost_virtqueue *vq;
1701 int numa_node = SOCKET_ID_ANY;
1704 if (ctx->msg.size != sizeof(ctx->msg.payload.inflight) || fd < 0) {
1705 VHOST_LOG_CONFIG(ERR, "(%s) invalid set_inflight_fd message size is %d,fd is %d\n",
1706 dev->ifname, ctx->msg.size, fd);
1707 return RTE_VHOST_MSG_RESULT_ERR;
1710 mmap_size = ctx->msg.payload.inflight.mmap_size;
1711 mmap_offset = ctx->msg.payload.inflight.mmap_offset;
1712 num_queues = ctx->msg.payload.inflight.num_queues;
1713 queue_size = ctx->msg.payload.inflight.queue_size;
1715 if (vq_is_packed(dev))
1716 pervq_inflight_size = get_pervq_shm_size_packed(queue_size);
1718 pervq_inflight_size = get_pervq_shm_size_split(queue_size);
1720 VHOST_LOG_CONFIG(INFO, "(%s) set_inflight_fd mmap_size: %"PRIu64"\n",
1721 dev->ifname, mmap_size);
1722 VHOST_LOG_CONFIG(INFO, "(%s) set_inflight_fd mmap_offset: %"PRIu64"\n",
1723 dev->ifname, mmap_offset);
1724 VHOST_LOG_CONFIG(INFO, "(%s) set_inflight_fd num_queues: %u\n", dev->ifname, num_queues);
1725 VHOST_LOG_CONFIG(INFO, "(%s) set_inflight_fd queue_size: %u\n", dev->ifname, queue_size);
1726 VHOST_LOG_CONFIG(INFO, "(%s) set_inflight_fd fd: %d\n", dev->ifname, fd);
1727 VHOST_LOG_CONFIG(INFO, "(%s) set_inflight_fd pervq_inflight_size: %d\n",
1728 dev->ifname, pervq_inflight_size);
1731 * If VQ 0 has already been allocated, try to allocate on the same
1732 * NUMA node. It can be reallocated later in numa_realloc().
1734 if (dev->nr_vring > 0)
1735 numa_node = dev->virtqueue[0]->numa_node;
1737 if (!dev->inflight_info) {
1738 dev->inflight_info = rte_zmalloc_socket("inflight_info",
1739 sizeof(struct inflight_mem_info), 0, numa_node);
1740 if (dev->inflight_info == NULL) {
1741 VHOST_LOG_CONFIG(ERR, "(%s) failed to alloc dev inflight area\n",
1743 return RTE_VHOST_MSG_RESULT_ERR;
1745 dev->inflight_info->fd = -1;
1748 if (dev->inflight_info->addr) {
1749 munmap(dev->inflight_info->addr, dev->inflight_info->size);
1750 dev->inflight_info->addr = NULL;
1753 addr = mmap(0, mmap_size, PROT_READ | PROT_WRITE, MAP_SHARED,
1755 if (addr == MAP_FAILED) {
1756 VHOST_LOG_CONFIG(ERR, "(%s) failed to mmap share memory.\n", dev->ifname);
1757 return RTE_VHOST_MSG_RESULT_ERR;
1760 if (dev->inflight_info->fd >= 0) {
1761 close(dev->inflight_info->fd);
1762 dev->inflight_info->fd = -1;
1765 dev->inflight_info->fd = fd;
1766 dev->inflight_info->addr = addr;
1767 dev->inflight_info->size = mmap_size;
1769 for (i = 0; i < num_queues; i++) {
1770 vq = dev->virtqueue[i];
1774 if (vq_is_packed(dev)) {
1775 vq->inflight_packed = addr;
1776 vq->inflight_packed->desc_num = queue_size;
1778 vq->inflight_split = addr;
1779 vq->inflight_split->desc_num = queue_size;
1781 addr = (void *)((char *)addr + pervq_inflight_size);
1784 return RTE_VHOST_MSG_RESULT_OK;
1788 vhost_user_set_vring_call(struct virtio_net **pdev,
1789 struct vhu_msg_context *ctx,
1790 int main_fd __rte_unused)
1792 struct virtio_net *dev = *pdev;
1793 struct vhost_vring_file file;
1794 struct vhost_virtqueue *vq;
1797 expected_fds = (ctx->msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK) ? 0 : 1;
1798 if (validate_msg_fds(dev, ctx, expected_fds) != 0)
1799 return RTE_VHOST_MSG_RESULT_ERR;
1801 file.index = ctx->msg.payload.u64 & VHOST_USER_VRING_IDX_MASK;
1802 if (ctx->msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK)
1803 file.fd = VIRTIO_INVALID_EVENTFD;
1805 file.fd = ctx->fds[0];
1806 VHOST_LOG_CONFIG(INFO, "(%s) vring call idx:%d file:%d\n",
1807 dev->ifname, file.index, file.fd);
1809 vq = dev->virtqueue[file.index];
1813 vhost_user_notify_queue_state(dev, file.index, 0);
1816 if (vq->callfd >= 0)
1819 vq->callfd = file.fd;
1821 return RTE_VHOST_MSG_RESULT_OK;
1824 static int vhost_user_set_vring_err(struct virtio_net **pdev,
1825 struct vhu_msg_context *ctx,
1826 int main_fd __rte_unused)
1828 struct virtio_net *dev = *pdev;
1831 expected_fds = (ctx->msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK) ? 0 : 1;
1832 if (validate_msg_fds(dev, ctx, expected_fds) != 0)
1833 return RTE_VHOST_MSG_RESULT_ERR;
1835 if (!(ctx->msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK))
1837 VHOST_LOG_CONFIG(INFO, "(%s) not implemented\n", dev->ifname);
1839 return RTE_VHOST_MSG_RESULT_OK;
1843 resubmit_desc_compare(const void *a, const void *b)
1845 const struct rte_vhost_resubmit_desc *desc0 = a;
1846 const struct rte_vhost_resubmit_desc *desc1 = b;
1848 if (desc1->counter > desc0->counter)
1855 vhost_check_queue_inflights_split(struct virtio_net *dev,
1856 struct vhost_virtqueue *vq)
1859 uint16_t resubmit_num = 0, last_io, num;
1860 struct vring_used *used = vq->used;
1861 struct rte_vhost_resubmit_info *resubmit;
1862 struct rte_vhost_inflight_info_split *inflight_split;
1864 if (!(dev->protocol_features &
1865 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD)))
1866 return RTE_VHOST_MSG_RESULT_OK;
1868 /* The frontend may still not support the inflight feature
1869 * although we negotiate the protocol feature.
1871 if ((!vq->inflight_split))
1872 return RTE_VHOST_MSG_RESULT_OK;
1874 if (!vq->inflight_split->version) {
1875 vq->inflight_split->version = INFLIGHT_VERSION;
1876 return RTE_VHOST_MSG_RESULT_OK;
1879 if (vq->resubmit_inflight)
1880 return RTE_VHOST_MSG_RESULT_OK;
1882 inflight_split = vq->inflight_split;
1883 vq->global_counter = 0;
1884 last_io = inflight_split->last_inflight_io;
1886 if (inflight_split->used_idx != used->idx) {
1887 inflight_split->desc[last_io].inflight = 0;
1888 rte_atomic_thread_fence(__ATOMIC_SEQ_CST);
1889 inflight_split->used_idx = used->idx;
1892 for (i = 0; i < inflight_split->desc_num; i++) {
1893 if (inflight_split->desc[i].inflight == 1)
1897 vq->last_avail_idx += resubmit_num;
1900 resubmit = rte_zmalloc_socket("resubmit", sizeof(struct rte_vhost_resubmit_info),
1903 VHOST_LOG_CONFIG(ERR,
1904 "(%s) failed to allocate memory for resubmit info.\n",
1906 return RTE_VHOST_MSG_RESULT_ERR;
1909 resubmit->resubmit_list = rte_zmalloc_socket("resubmit_list",
1910 resubmit_num * sizeof(struct rte_vhost_resubmit_desc),
1912 if (!resubmit->resubmit_list) {
1913 VHOST_LOG_CONFIG(ERR,
1914 "(%s) failed to allocate memory for inflight desc.\n",
1917 return RTE_VHOST_MSG_RESULT_ERR;
1921 for (i = 0; i < vq->inflight_split->desc_num; i++) {
1922 if (vq->inflight_split->desc[i].inflight == 1) {
1923 resubmit->resubmit_list[num].index = i;
1924 resubmit->resubmit_list[num].counter =
1925 inflight_split->desc[i].counter;
1929 resubmit->resubmit_num = num;
1931 if (resubmit->resubmit_num > 1)
1932 qsort(resubmit->resubmit_list, resubmit->resubmit_num,
1933 sizeof(struct rte_vhost_resubmit_desc),
1934 resubmit_desc_compare);
1936 vq->global_counter = resubmit->resubmit_list[0].counter + 1;
1937 vq->resubmit_inflight = resubmit;
1940 return RTE_VHOST_MSG_RESULT_OK;
1944 vhost_check_queue_inflights_packed(struct virtio_net *dev,
1945 struct vhost_virtqueue *vq)
1948 uint16_t resubmit_num = 0, old_used_idx, num;
1949 struct rte_vhost_resubmit_info *resubmit;
1950 struct rte_vhost_inflight_info_packed *inflight_packed;
1952 if (!(dev->protocol_features &
1953 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD)))
1954 return RTE_VHOST_MSG_RESULT_OK;
1956 /* The frontend may still not support the inflight feature
1957 * although we negotiate the protocol feature.
1959 if ((!vq->inflight_packed))
1960 return RTE_VHOST_MSG_RESULT_OK;
1962 if (!vq->inflight_packed->version) {
1963 vq->inflight_packed->version = INFLIGHT_VERSION;
1964 return RTE_VHOST_MSG_RESULT_OK;
1967 if (vq->resubmit_inflight)
1968 return RTE_VHOST_MSG_RESULT_OK;
1970 inflight_packed = vq->inflight_packed;
1971 vq->global_counter = 0;
1972 old_used_idx = inflight_packed->old_used_idx;
1974 if (inflight_packed->used_idx != old_used_idx) {
1975 if (inflight_packed->desc[old_used_idx].inflight == 0) {
1976 inflight_packed->old_used_idx =
1977 inflight_packed->used_idx;
1978 inflight_packed->old_used_wrap_counter =
1979 inflight_packed->used_wrap_counter;
1980 inflight_packed->old_free_head =
1981 inflight_packed->free_head;
1983 inflight_packed->used_idx =
1984 inflight_packed->old_used_idx;
1985 inflight_packed->used_wrap_counter =
1986 inflight_packed->old_used_wrap_counter;
1987 inflight_packed->free_head =
1988 inflight_packed->old_free_head;
1992 for (i = 0; i < inflight_packed->desc_num; i++) {
1993 if (inflight_packed->desc[i].inflight == 1)
1998 resubmit = rte_zmalloc_socket("resubmit", sizeof(struct rte_vhost_resubmit_info),
2000 if (resubmit == NULL) {
2001 VHOST_LOG_CONFIG(ERR,
2002 "(%s) failed to allocate memory for resubmit info.\n",
2004 return RTE_VHOST_MSG_RESULT_ERR;
2007 resubmit->resubmit_list = rte_zmalloc_socket("resubmit_list",
2008 resubmit_num * sizeof(struct rte_vhost_resubmit_desc),
2010 if (resubmit->resubmit_list == NULL) {
2011 VHOST_LOG_CONFIG(ERR,
2012 "(%s) failed to allocate memory for resubmit desc.\n",
2015 return RTE_VHOST_MSG_RESULT_ERR;
2019 for (i = 0; i < inflight_packed->desc_num; i++) {
2020 if (vq->inflight_packed->desc[i].inflight == 1) {
2021 resubmit->resubmit_list[num].index = i;
2022 resubmit->resubmit_list[num].counter =
2023 inflight_packed->desc[i].counter;
2027 resubmit->resubmit_num = num;
2029 if (resubmit->resubmit_num > 1)
2030 qsort(resubmit->resubmit_list, resubmit->resubmit_num,
2031 sizeof(struct rte_vhost_resubmit_desc),
2032 resubmit_desc_compare);
2034 vq->global_counter = resubmit->resubmit_list[0].counter + 1;
2035 vq->resubmit_inflight = resubmit;
2038 return RTE_VHOST_MSG_RESULT_OK;
2042 vhost_user_set_vring_kick(struct virtio_net **pdev,
2043 struct vhu_msg_context *ctx,
2044 int main_fd __rte_unused)
2046 struct virtio_net *dev = *pdev;
2047 struct vhost_vring_file file;
2048 struct vhost_virtqueue *vq;
2051 expected_fds = (ctx->msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK) ? 0 : 1;
2052 if (validate_msg_fds(dev, ctx, expected_fds) != 0)
2053 return RTE_VHOST_MSG_RESULT_ERR;
2055 file.index = ctx->msg.payload.u64 & VHOST_USER_VRING_IDX_MASK;
2056 if (ctx->msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK)
2057 file.fd = VIRTIO_INVALID_EVENTFD;
2059 file.fd = ctx->fds[0];
2060 VHOST_LOG_CONFIG(INFO, "(%s) vring kick idx:%d file:%d\n",
2061 dev->ifname, file.index, file.fd);
2063 /* Interpret ring addresses only when ring is started. */
2064 dev = translate_ring_addresses(dev, file.index);
2066 if (file.fd != VIRTIO_INVALID_EVENTFD)
2069 return RTE_VHOST_MSG_RESULT_ERR;
2074 vq = dev->virtqueue[file.index];
2077 * When VHOST_USER_F_PROTOCOL_FEATURES is not negotiated,
2078 * the ring starts already enabled. Otherwise, it is enabled via
2079 * the SET_VRING_ENABLE message.
2081 if (!(dev->features & (1ULL << VHOST_USER_F_PROTOCOL_FEATURES))) {
2087 vhost_user_notify_queue_state(dev, file.index, 0);
2090 if (vq->kickfd >= 0)
2092 vq->kickfd = file.fd;
2094 if (vq_is_packed(dev)) {
2095 if (vhost_check_queue_inflights_packed(dev, vq)) {
2096 VHOST_LOG_CONFIG(ERR, "(%s) failed to inflights for vq: %d\n",
2097 dev->ifname, file.index);
2098 return RTE_VHOST_MSG_RESULT_ERR;
2101 if (vhost_check_queue_inflights_split(dev, vq)) {
2102 VHOST_LOG_CONFIG(ERR, "(%s) failed to inflights for vq: %d\n",
2103 dev->ifname, file.index);
2104 return RTE_VHOST_MSG_RESULT_ERR;
2108 return RTE_VHOST_MSG_RESULT_OK;
2112 * when virtio is stopped, qemu will send us the GET_VRING_BASE message.
2115 vhost_user_get_vring_base(struct virtio_net **pdev,
2116 struct vhu_msg_context *ctx,
2117 int main_fd __rte_unused)
2119 struct virtio_net *dev = *pdev;
2120 struct vhost_virtqueue *vq = dev->virtqueue[ctx->msg.payload.state.index];
2123 if (validate_msg_fds(dev, ctx, 0) != 0)
2124 return RTE_VHOST_MSG_RESULT_ERR;
2126 /* We have to stop the queue (virtio) if it is running. */
2127 vhost_destroy_device_notify(dev);
2129 dev->flags &= ~VIRTIO_DEV_READY;
2130 dev->flags &= ~VIRTIO_DEV_VDPA_CONFIGURED;
2132 /* Here we are safe to get the indexes */
2133 if (vq_is_packed(dev)) {
2135 * Bit[0:14]: avail index
2136 * Bit[15]: avail wrap counter
2138 val = vq->last_avail_idx & 0x7fff;
2139 val |= vq->avail_wrap_counter << 15;
2140 ctx->msg.payload.state.num = val;
2142 ctx->msg.payload.state.num = vq->last_avail_idx;
2145 VHOST_LOG_CONFIG(INFO, "(%s) vring base idx:%d file:%d\n",
2146 dev->ifname, ctx->msg.payload.state.index,
2147 ctx->msg.payload.state.num);
2149 * Based on current qemu vhost-user implementation, this message is
2150 * sent and only sent in vhost_vring_stop.
2151 * TODO: cleanup the vring, it isn't usable since here.
2153 if (vq->kickfd >= 0)
2156 vq->kickfd = VIRTIO_UNINITIALIZED_EVENTFD;
2158 if (vq->callfd >= 0)
2161 vq->callfd = VIRTIO_UNINITIALIZED_EVENTFD;
2163 vq->signalled_used_valid = false;
2165 if (vq_is_packed(dev)) {
2166 rte_free(vq->shadow_used_packed);
2167 vq->shadow_used_packed = NULL;
2169 rte_free(vq->shadow_used_split);
2170 vq->shadow_used_split = NULL;
2173 rte_free(vq->batch_copy_elems);
2174 vq->batch_copy_elems = NULL;
2176 rte_free(vq->log_cache);
2177 vq->log_cache = NULL;
2179 ctx->msg.size = sizeof(ctx->msg.payload.state);
2182 vhost_user_iotlb_flush_all(vq);
2184 vring_invalidate(dev, vq);
2186 return RTE_VHOST_MSG_RESULT_REPLY;
2190 * when virtio queues are ready to work, qemu will send us to
2191 * enable the virtio queue pair.
2194 vhost_user_set_vring_enable(struct virtio_net **pdev,
2195 struct vhu_msg_context *ctx,
2196 int main_fd __rte_unused)
2198 struct virtio_net *dev = *pdev;
2199 bool enable = !!ctx->msg.payload.state.num;
2200 int index = (int)ctx->msg.payload.state.index;
2202 if (validate_msg_fds(dev, ctx, 0) != 0)
2203 return RTE_VHOST_MSG_RESULT_ERR;
2205 VHOST_LOG_CONFIG(INFO, "(%s) set queue enable: %d to qp idx: %d\n",
2206 dev->ifname, enable, index);
2208 if (enable && dev->virtqueue[index]->async) {
2209 if (dev->virtqueue[index]->async->pkts_inflight_n) {
2210 VHOST_LOG_CONFIG(ERR,
2211 "(%s) failed to enable vring. Inflight packets must be completed first\n",
2213 return RTE_VHOST_MSG_RESULT_ERR;
2217 dev->virtqueue[index]->enabled = enable;
2219 return RTE_VHOST_MSG_RESULT_OK;
2223 vhost_user_get_protocol_features(struct virtio_net **pdev,
2224 struct vhu_msg_context *ctx,
2225 int main_fd __rte_unused)
2227 struct virtio_net *dev = *pdev;
2228 uint64_t features, protocol_features;
2230 if (validate_msg_fds(dev, ctx, 0) != 0)
2231 return RTE_VHOST_MSG_RESULT_ERR;
2233 rte_vhost_driver_get_features(dev->ifname, &features);
2234 rte_vhost_driver_get_protocol_features(dev->ifname, &protocol_features);
2236 ctx->msg.payload.u64 = protocol_features;
2237 ctx->msg.size = sizeof(ctx->msg.payload.u64);
2240 return RTE_VHOST_MSG_RESULT_REPLY;
2244 vhost_user_set_protocol_features(struct virtio_net **pdev,
2245 struct vhu_msg_context *ctx,
2246 int main_fd __rte_unused)
2248 struct virtio_net *dev = *pdev;
2249 uint64_t protocol_features = ctx->msg.payload.u64;
2250 uint64_t slave_protocol_features = 0;
2252 if (validate_msg_fds(dev, ctx, 0) != 0)
2253 return RTE_VHOST_MSG_RESULT_ERR;
2255 rte_vhost_driver_get_protocol_features(dev->ifname,
2256 &slave_protocol_features);
2257 if (protocol_features & ~slave_protocol_features) {
2258 VHOST_LOG_CONFIG(ERR, "(%s) received invalid protocol features.\n", dev->ifname);
2259 return RTE_VHOST_MSG_RESULT_ERR;
2262 dev->protocol_features = protocol_features;
2263 VHOST_LOG_CONFIG(INFO, "(%s) negotiated Vhost-user protocol features: 0x%" PRIx64 "\n",
2264 dev->ifname, dev->protocol_features);
2266 return RTE_VHOST_MSG_RESULT_OK;
2270 vhost_user_set_log_base(struct virtio_net **pdev,
2271 struct vhu_msg_context *ctx,
2272 int main_fd __rte_unused)
2274 struct virtio_net *dev = *pdev;
2275 int fd = ctx->fds[0];
2280 if (validate_msg_fds(dev, ctx, 1) != 0)
2281 return RTE_VHOST_MSG_RESULT_ERR;
2284 VHOST_LOG_CONFIG(ERR, "(%s) invalid log fd: %d\n", dev->ifname, fd);
2285 return RTE_VHOST_MSG_RESULT_ERR;
2288 if (ctx->msg.size != sizeof(VhostUserLog)) {
2289 VHOST_LOG_CONFIG(ERR, "(%s) invalid log base msg size: %"PRId32" != %d\n",
2290 dev->ifname, ctx->msg.size, (int)sizeof(VhostUserLog));
2294 size = ctx->msg.payload.log.mmap_size;
2295 off = ctx->msg.payload.log.mmap_offset;
2297 /* Check for mmap size and offset overflow. */
2299 VHOST_LOG_CONFIG(ERR,
2300 "(%s) log offset %#"PRIx64" and log size %#"PRIx64" overflow\n",
2301 dev->ifname, off, size);
2305 VHOST_LOG_CONFIG(INFO, "(%s) log mmap size: %"PRId64", offset: %"PRId64"\n",
2306 dev->ifname, size, off);
2309 * mmap from 0 to workaround a hugepage mmap bug: mmap will
2310 * fail when offset is not page size aligned.
2312 addr = mmap(0, size + off, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
2314 if (addr == MAP_FAILED) {
2315 VHOST_LOG_CONFIG(ERR, "(%s) mmap log base failed!\n", dev->ifname);
2316 return RTE_VHOST_MSG_RESULT_ERR;
2320 * Free previously mapped log memory on occasionally
2321 * multiple VHOST_USER_SET_LOG_BASE.
2323 if (dev->log_addr) {
2324 munmap((void *)(uintptr_t)dev->log_addr, dev->log_size);
2326 dev->log_addr = (uint64_t)(uintptr_t)addr;
2327 dev->log_base = dev->log_addr + off;
2328 dev->log_size = size;
2330 for (i = 0; i < dev->nr_vring; i++) {
2331 struct vhost_virtqueue *vq = dev->virtqueue[i];
2333 rte_free(vq->log_cache);
2334 vq->log_cache = NULL;
2335 vq->log_cache_nb_elem = 0;
2336 vq->log_cache = rte_malloc_socket("vq log cache",
2337 sizeof(struct log_cache_entry) * VHOST_LOG_CACHE_NR,
2340 * If log cache alloc fail, don't fail migration, but no
2341 * caching will be done, which will impact performance
2344 VHOST_LOG_CONFIG(ERR, "(%s) failed to allocate VQ logging cache\n",
2349 * The spec is not clear about it (yet), but QEMU doesn't expect
2350 * any payload in the reply.
2355 return RTE_VHOST_MSG_RESULT_REPLY;
2359 return RTE_VHOST_MSG_RESULT_ERR;
2362 static int vhost_user_set_log_fd(struct virtio_net **pdev,
2363 struct vhu_msg_context *ctx,
2364 int main_fd __rte_unused)
2366 struct virtio_net *dev = *pdev;
2368 if (validate_msg_fds(dev, ctx, 1) != 0)
2369 return RTE_VHOST_MSG_RESULT_ERR;
2372 VHOST_LOG_CONFIG(INFO, "(%s) not implemented.\n", dev->ifname);
2374 return RTE_VHOST_MSG_RESULT_OK;
2378 * An rarp packet is constructed and broadcasted to notify switches about
2379 * the new location of the migrated VM, so that packets from outside will
2380 * not be lost after migration.
2382 * However, we don't actually "send" a rarp packet here, instead, we set
2383 * a flag 'broadcast_rarp' to let rte_vhost_dequeue_burst() inject it.
2386 vhost_user_send_rarp(struct virtio_net **pdev,
2387 struct vhu_msg_context *ctx,
2388 int main_fd __rte_unused)
2390 struct virtio_net *dev = *pdev;
2391 uint8_t *mac = (uint8_t *)&ctx->msg.payload.u64;
2392 struct rte_vdpa_device *vdpa_dev;
2394 if (validate_msg_fds(dev, ctx, 0) != 0)
2395 return RTE_VHOST_MSG_RESULT_ERR;
2397 VHOST_LOG_CONFIG(DEBUG, "(%s) MAC: " RTE_ETHER_ADDR_PRT_FMT "\n",
2398 dev->ifname, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
2399 memcpy(dev->mac.addr_bytes, mac, 6);
2402 * Set the flag to inject a RARP broadcast packet at
2403 * rte_vhost_dequeue_burst().
2405 * __ATOMIC_RELEASE ordering is for making sure the mac is
2406 * copied before the flag is set.
2408 __atomic_store_n(&dev->broadcast_rarp, 1, __ATOMIC_RELEASE);
2409 vdpa_dev = dev->vdpa_dev;
2410 if (vdpa_dev && vdpa_dev->ops->migration_done)
2411 vdpa_dev->ops->migration_done(dev->vid);
2413 return RTE_VHOST_MSG_RESULT_OK;
2417 vhost_user_net_set_mtu(struct virtio_net **pdev,
2418 struct vhu_msg_context *ctx,
2419 int main_fd __rte_unused)
2421 struct virtio_net *dev = *pdev;
2423 if (validate_msg_fds(dev, ctx, 0) != 0)
2424 return RTE_VHOST_MSG_RESULT_ERR;
2426 if (ctx->msg.payload.u64 < VIRTIO_MIN_MTU ||
2427 ctx->msg.payload.u64 > VIRTIO_MAX_MTU) {
2428 VHOST_LOG_CONFIG(ERR, "(%s) invalid MTU size (%"PRIu64")\n",
2429 dev->ifname, ctx->msg.payload.u64);
2431 return RTE_VHOST_MSG_RESULT_ERR;
2434 dev->mtu = ctx->msg.payload.u64;
2436 return RTE_VHOST_MSG_RESULT_OK;
2440 vhost_user_set_req_fd(struct virtio_net **pdev,
2441 struct vhu_msg_context *ctx,
2442 int main_fd __rte_unused)
2444 struct virtio_net *dev = *pdev;
2445 int fd = ctx->fds[0];
2447 if (validate_msg_fds(dev, ctx, 1) != 0)
2448 return RTE_VHOST_MSG_RESULT_ERR;
2451 VHOST_LOG_CONFIG(ERR, "(%s) invalid file descriptor for slave channel (%d)\n",
2453 return RTE_VHOST_MSG_RESULT_ERR;
2456 if (dev->slave_req_fd >= 0)
2457 close(dev->slave_req_fd);
2459 dev->slave_req_fd = fd;
2461 return RTE_VHOST_MSG_RESULT_OK;
2465 is_vring_iotlb_split(struct vhost_virtqueue *vq, struct vhost_iotlb_msg *imsg)
2467 struct vhost_vring_addr *ra;
2468 uint64_t start, end, len;
2471 end = start + imsg->size;
2473 ra = &vq->ring_addrs;
2474 len = sizeof(struct vring_desc) * vq->size;
2475 if (ra->desc_user_addr < end && (ra->desc_user_addr + len) > start)
2478 len = sizeof(struct vring_avail) + sizeof(uint16_t) * vq->size;
2479 if (ra->avail_user_addr < end && (ra->avail_user_addr + len) > start)
2482 len = sizeof(struct vring_used) +
2483 sizeof(struct vring_used_elem) * vq->size;
2484 if (ra->used_user_addr < end && (ra->used_user_addr + len) > start)
2487 if (ra->flags & (1 << VHOST_VRING_F_LOG)) {
2488 len = sizeof(uint64_t);
2489 if (ra->log_guest_addr < end &&
2490 (ra->log_guest_addr + len) > start)
2498 is_vring_iotlb_packed(struct vhost_virtqueue *vq, struct vhost_iotlb_msg *imsg)
2500 struct vhost_vring_addr *ra;
2501 uint64_t start, end, len;
2504 end = start + imsg->size;
2506 ra = &vq->ring_addrs;
2507 len = sizeof(struct vring_packed_desc) * vq->size;
2508 if (ra->desc_user_addr < end && (ra->desc_user_addr + len) > start)
2511 len = sizeof(struct vring_packed_desc_event);
2512 if (ra->avail_user_addr < end && (ra->avail_user_addr + len) > start)
2515 len = sizeof(struct vring_packed_desc_event);
2516 if (ra->used_user_addr < end && (ra->used_user_addr + len) > start)
2519 if (ra->flags & (1 << VHOST_VRING_F_LOG)) {
2520 len = sizeof(uint64_t);
2521 if (ra->log_guest_addr < end &&
2522 (ra->log_guest_addr + len) > start)
2529 static int is_vring_iotlb(struct virtio_net *dev,
2530 struct vhost_virtqueue *vq,
2531 struct vhost_iotlb_msg *imsg)
2533 if (vq_is_packed(dev))
2534 return is_vring_iotlb_packed(vq, imsg);
2536 return is_vring_iotlb_split(vq, imsg);
2540 vhost_user_iotlb_msg(struct virtio_net **pdev,
2541 struct vhu_msg_context *ctx,
2542 int main_fd __rte_unused)
2544 struct virtio_net *dev = *pdev;
2545 struct vhost_iotlb_msg *imsg = &ctx->msg.payload.iotlb;
2549 if (validate_msg_fds(dev, ctx, 0) != 0)
2550 return RTE_VHOST_MSG_RESULT_ERR;
2552 switch (imsg->type) {
2553 case VHOST_IOTLB_UPDATE:
2555 vva = qva_to_vva(dev, imsg->uaddr, &len);
2557 return RTE_VHOST_MSG_RESULT_ERR;
2559 for (i = 0; i < dev->nr_vring; i++) {
2560 struct vhost_virtqueue *vq = dev->virtqueue[i];
2565 vhost_user_iotlb_cache_insert(dev, vq, imsg->iova, vva,
2568 if (is_vring_iotlb(dev, vq, imsg))
2569 *pdev = dev = translate_ring_addresses(dev, i);
2572 case VHOST_IOTLB_INVALIDATE:
2573 for (i = 0; i < dev->nr_vring; i++) {
2574 struct vhost_virtqueue *vq = dev->virtqueue[i];
2579 vhost_user_iotlb_cache_remove(vq, imsg->iova,
2582 if (is_vring_iotlb(dev, vq, imsg))
2583 vring_invalidate(dev, vq);
2587 VHOST_LOG_CONFIG(ERR, "(%s) invalid IOTLB message type (%d)\n",
2588 dev->ifname, imsg->type);
2589 return RTE_VHOST_MSG_RESULT_ERR;
2592 return RTE_VHOST_MSG_RESULT_OK;
2596 vhost_user_set_postcopy_advise(struct virtio_net **pdev,
2597 struct vhu_msg_context *ctx,
2598 int main_fd __rte_unused)
2600 struct virtio_net *dev = *pdev;
2601 #ifdef RTE_LIBRTE_VHOST_POSTCOPY
2602 struct uffdio_api api_struct;
2604 if (validate_msg_fds(dev, ctx, 0) != 0)
2605 return RTE_VHOST_MSG_RESULT_ERR;
2607 dev->postcopy_ufd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK);
2609 if (dev->postcopy_ufd == -1) {
2610 VHOST_LOG_CONFIG(ERR, "(%s) userfaultfd not available: %s\n",
2611 dev->ifname, strerror(errno));
2612 return RTE_VHOST_MSG_RESULT_ERR;
2614 api_struct.api = UFFD_API;
2615 api_struct.features = 0;
2616 if (ioctl(dev->postcopy_ufd, UFFDIO_API, &api_struct)) {
2617 VHOST_LOG_CONFIG(ERR, "(%s) UFFDIO_API ioctl failure: %s\n",
2618 dev->ifname, strerror(errno));
2619 close(dev->postcopy_ufd);
2620 dev->postcopy_ufd = -1;
2621 return RTE_VHOST_MSG_RESULT_ERR;
2623 ctx->fds[0] = dev->postcopy_ufd;
2626 return RTE_VHOST_MSG_RESULT_REPLY;
2628 dev->postcopy_ufd = -1;
2631 return RTE_VHOST_MSG_RESULT_ERR;
2636 vhost_user_set_postcopy_listen(struct virtio_net **pdev,
2637 struct vhu_msg_context *ctx __rte_unused,
2638 int main_fd __rte_unused)
2640 struct virtio_net *dev = *pdev;
2642 if (validate_msg_fds(dev, ctx, 0) != 0)
2643 return RTE_VHOST_MSG_RESULT_ERR;
2645 if (dev->mem && dev->mem->nregions) {
2646 VHOST_LOG_CONFIG(ERR, "(%s) regions already registered at postcopy-listen\n",
2648 return RTE_VHOST_MSG_RESULT_ERR;
2650 dev->postcopy_listening = 1;
2652 return RTE_VHOST_MSG_RESULT_OK;
2656 vhost_user_postcopy_end(struct virtio_net **pdev,
2657 struct vhu_msg_context *ctx,
2658 int main_fd __rte_unused)
2660 struct virtio_net *dev = *pdev;
2662 if (validate_msg_fds(dev, ctx, 0) != 0)
2663 return RTE_VHOST_MSG_RESULT_ERR;
2665 dev->postcopy_listening = 0;
2666 if (dev->postcopy_ufd >= 0) {
2667 close(dev->postcopy_ufd);
2668 dev->postcopy_ufd = -1;
2671 ctx->msg.payload.u64 = 0;
2672 ctx->msg.size = sizeof(ctx->msg.payload.u64);
2675 return RTE_VHOST_MSG_RESULT_REPLY;
2679 vhost_user_get_status(struct virtio_net **pdev,
2680 struct vhu_msg_context *ctx,
2681 int main_fd __rte_unused)
2683 struct virtio_net *dev = *pdev;
2685 if (validate_msg_fds(dev, ctx, 0) != 0)
2686 return RTE_VHOST_MSG_RESULT_ERR;
2688 ctx->msg.payload.u64 = dev->status;
2689 ctx->msg.size = sizeof(ctx->msg.payload.u64);
2692 return RTE_VHOST_MSG_RESULT_REPLY;
2696 vhost_user_set_status(struct virtio_net **pdev,
2697 struct vhu_msg_context *ctx,
2698 int main_fd __rte_unused)
2700 struct virtio_net *dev = *pdev;
2702 if (validate_msg_fds(dev, ctx, 0) != 0)
2703 return RTE_VHOST_MSG_RESULT_ERR;
2705 /* As per Virtio specification, the device status is 8bits long */
2706 if (ctx->msg.payload.u64 > UINT8_MAX) {
2707 VHOST_LOG_CONFIG(ERR, "(%s) invalid VHOST_USER_SET_STATUS payload 0x%" PRIx64 "\n",
2708 dev->ifname, ctx->msg.payload.u64);
2709 return RTE_VHOST_MSG_RESULT_ERR;
2712 dev->status = ctx->msg.payload.u64;
2714 if ((dev->status & VIRTIO_DEVICE_STATUS_FEATURES_OK) &&
2715 (dev->flags & VIRTIO_DEV_FEATURES_FAILED)) {
2716 VHOST_LOG_CONFIG(ERR,
2717 "(%s) FEATURES_OK bit is set but feature negotiation failed\n",
2720 * Clear the bit to let the driver know about the feature
2721 * negotiation failure
2723 dev->status &= ~VIRTIO_DEVICE_STATUS_FEATURES_OK;
2726 VHOST_LOG_CONFIG(INFO, "(%s) new device status(0x%08x):\n", dev->ifname,
2728 VHOST_LOG_CONFIG(INFO, "(%s)\t-RESET: %u\n", dev->ifname,
2729 (dev->status == VIRTIO_DEVICE_STATUS_RESET));
2730 VHOST_LOG_CONFIG(INFO, "(%s)\t-ACKNOWLEDGE: %u\n", dev->ifname,
2731 !!(dev->status & VIRTIO_DEVICE_STATUS_ACK));
2732 VHOST_LOG_CONFIG(INFO, "(%s)\t-DRIVER: %u\n", dev->ifname,
2733 !!(dev->status & VIRTIO_DEVICE_STATUS_DRIVER));
2734 VHOST_LOG_CONFIG(INFO, "(%s)\t-FEATURES_OK: %u\n", dev->ifname,
2735 !!(dev->status & VIRTIO_DEVICE_STATUS_FEATURES_OK));
2736 VHOST_LOG_CONFIG(INFO, "(%s)\t-DRIVER_OK: %u\n", dev->ifname,
2737 !!(dev->status & VIRTIO_DEVICE_STATUS_DRIVER_OK));
2738 VHOST_LOG_CONFIG(INFO, "(%s)\t-DEVICE_NEED_RESET: %u\n", dev->ifname,
2739 !!(dev->status & VIRTIO_DEVICE_STATUS_DEV_NEED_RESET));
2740 VHOST_LOG_CONFIG(INFO, "(%s)\t-FAILED: %u\n", dev->ifname,
2741 !!(dev->status & VIRTIO_DEVICE_STATUS_FAILED));
2743 return RTE_VHOST_MSG_RESULT_OK;
2746 typedef int (*vhost_message_handler_t)(struct virtio_net **pdev,
2747 struct vhu_msg_context *ctx,
2750 static vhost_message_handler_t vhost_message_handlers[VHOST_USER_MAX] = {
2751 [VHOST_USER_NONE] = NULL,
2752 [VHOST_USER_GET_FEATURES] = vhost_user_get_features,
2753 [VHOST_USER_SET_FEATURES] = vhost_user_set_features,
2754 [VHOST_USER_SET_OWNER] = vhost_user_set_owner,
2755 [VHOST_USER_RESET_OWNER] = vhost_user_reset_owner,
2756 [VHOST_USER_SET_MEM_TABLE] = vhost_user_set_mem_table,
2757 [VHOST_USER_SET_LOG_BASE] = vhost_user_set_log_base,
2758 [VHOST_USER_SET_LOG_FD] = vhost_user_set_log_fd,
2759 [VHOST_USER_SET_VRING_NUM] = vhost_user_set_vring_num,
2760 [VHOST_USER_SET_VRING_ADDR] = vhost_user_set_vring_addr,
2761 [VHOST_USER_SET_VRING_BASE] = vhost_user_set_vring_base,
2762 [VHOST_USER_GET_VRING_BASE] = vhost_user_get_vring_base,
2763 [VHOST_USER_SET_VRING_KICK] = vhost_user_set_vring_kick,
2764 [VHOST_USER_SET_VRING_CALL] = vhost_user_set_vring_call,
2765 [VHOST_USER_SET_VRING_ERR] = vhost_user_set_vring_err,
2766 [VHOST_USER_GET_PROTOCOL_FEATURES] = vhost_user_get_protocol_features,
2767 [VHOST_USER_SET_PROTOCOL_FEATURES] = vhost_user_set_protocol_features,
2768 [VHOST_USER_GET_QUEUE_NUM] = vhost_user_get_queue_num,
2769 [VHOST_USER_SET_VRING_ENABLE] = vhost_user_set_vring_enable,
2770 [VHOST_USER_SEND_RARP] = vhost_user_send_rarp,
2771 [VHOST_USER_NET_SET_MTU] = vhost_user_net_set_mtu,
2772 [VHOST_USER_SET_SLAVE_REQ_FD] = vhost_user_set_req_fd,
2773 [VHOST_USER_IOTLB_MSG] = vhost_user_iotlb_msg,
2774 [VHOST_USER_POSTCOPY_ADVISE] = vhost_user_set_postcopy_advise,
2775 [VHOST_USER_POSTCOPY_LISTEN] = vhost_user_set_postcopy_listen,
2776 [VHOST_USER_POSTCOPY_END] = vhost_user_postcopy_end,
2777 [VHOST_USER_GET_INFLIGHT_FD] = vhost_user_get_inflight_fd,
2778 [VHOST_USER_SET_INFLIGHT_FD] = vhost_user_set_inflight_fd,
2779 [VHOST_USER_SET_STATUS] = vhost_user_set_status,
2780 [VHOST_USER_GET_STATUS] = vhost_user_get_status,
2783 /* return bytes# of read on success or negative val on failure. */
2785 read_vhost_message(struct virtio_net *dev, int sockfd, struct vhu_msg_context *ctx)
2789 ret = read_fd_message(dev->ifname, sockfd, (char *)&ctx->msg, VHOST_USER_HDR_SIZE,
2790 ctx->fds, VHOST_MEMORY_MAX_NREGIONS, &ctx->fd_num);
2793 } else if (ret != VHOST_USER_HDR_SIZE) {
2794 VHOST_LOG_CONFIG(ERR, "(%s) Unexpected header size read\n", dev->ifname);
2799 if (ctx->msg.size) {
2800 if (ctx->msg.size > sizeof(ctx->msg.payload)) {
2801 VHOST_LOG_CONFIG(ERR, "(%s) invalid msg size: %d\n",
2802 dev->ifname, ctx->msg.size);
2805 ret = read(sockfd, &ctx->msg.payload, ctx->msg.size);
2808 if (ret != (int)ctx->msg.size) {
2809 VHOST_LOG_CONFIG(ERR, "(%s) read control message failed\n", dev->ifname);
2818 send_vhost_message(struct virtio_net *dev, int sockfd, struct vhu_msg_context *ctx)
2823 return send_fd_message(dev->ifname, sockfd, (char *)&ctx->msg,
2824 VHOST_USER_HDR_SIZE + ctx->msg.size, ctx->fds, ctx->fd_num);
2828 send_vhost_reply(struct virtio_net *dev, int sockfd, struct vhu_msg_context *ctx)
2833 ctx->msg.flags &= ~VHOST_USER_VERSION_MASK;
2834 ctx->msg.flags &= ~VHOST_USER_NEED_REPLY;
2835 ctx->msg.flags |= VHOST_USER_VERSION;
2836 ctx->msg.flags |= VHOST_USER_REPLY_MASK;
2838 return send_vhost_message(dev, sockfd, ctx);
2842 send_vhost_slave_message(struct virtio_net *dev,
2843 struct vhu_msg_context *ctx)
2847 if (ctx->msg.flags & VHOST_USER_NEED_REPLY)
2848 rte_spinlock_lock(&dev->slave_req_lock);
2850 ret = send_vhost_message(dev, dev->slave_req_fd, ctx);
2851 if (ret < 0 && (ctx->msg.flags & VHOST_USER_NEED_REPLY))
2852 rte_spinlock_unlock(&dev->slave_req_lock);
2858 * Allocate a queue pair if it hasn't been allocated yet
2861 vhost_user_check_and_alloc_queue_pair(struct virtio_net *dev,
2862 struct vhu_msg_context *ctx)
2866 switch (ctx->msg.request.master) {
2867 case VHOST_USER_SET_VRING_KICK:
2868 case VHOST_USER_SET_VRING_CALL:
2869 case VHOST_USER_SET_VRING_ERR:
2870 vring_idx = ctx->msg.payload.u64 & VHOST_USER_VRING_IDX_MASK;
2872 case VHOST_USER_SET_VRING_NUM:
2873 case VHOST_USER_SET_VRING_BASE:
2874 case VHOST_USER_GET_VRING_BASE:
2875 case VHOST_USER_SET_VRING_ENABLE:
2876 vring_idx = ctx->msg.payload.state.index;
2878 case VHOST_USER_SET_VRING_ADDR:
2879 vring_idx = ctx->msg.payload.addr.index;
2885 if (vring_idx >= VHOST_MAX_VRING) {
2886 VHOST_LOG_CONFIG(ERR, "(%s) invalid vring index: %u\n", dev->ifname, vring_idx);
2890 if (dev->virtqueue[vring_idx])
2893 return alloc_vring_queue(dev, vring_idx);
2897 vhost_user_lock_all_queue_pairs(struct virtio_net *dev)
2900 unsigned int vq_num = 0;
2902 while (vq_num < dev->nr_vring) {
2903 struct vhost_virtqueue *vq = dev->virtqueue[i];
2906 rte_spinlock_lock(&vq->access_lock);
2914 vhost_user_unlock_all_queue_pairs(struct virtio_net *dev)
2917 unsigned int vq_num = 0;
2919 while (vq_num < dev->nr_vring) {
2920 struct vhost_virtqueue *vq = dev->virtqueue[i];
2923 rte_spinlock_unlock(&vq->access_lock);
2931 vhost_user_msg_handler(int vid, int fd)
2933 struct virtio_net *dev;
2934 struct vhu_msg_context ctx;
2935 struct rte_vdpa_device *vdpa_dev;
2937 int unlock_required = 0;
2942 dev = get_device(vid);
2946 if (!dev->notify_ops) {
2947 dev->notify_ops = vhost_driver_callback_get(dev->ifname);
2948 if (!dev->notify_ops) {
2949 VHOST_LOG_CONFIG(ERR, "(%s) failed to get callback ops for driver\n",
2955 ret = read_vhost_message(dev, fd, &ctx);
2958 VHOST_LOG_CONFIG(ERR, "(%s) vhost read message failed\n", dev->ifname);
2960 VHOST_LOG_CONFIG(INFO, "(%s) vhost peer closed\n", dev->ifname);
2966 request = ctx.msg.request.master;
2967 if (request > VHOST_USER_NONE && request < VHOST_USER_MAX &&
2968 vhost_message_str[request]) {
2969 if (request != VHOST_USER_IOTLB_MSG)
2970 VHOST_LOG_CONFIG(INFO, "(%s) read message %s\n",
2971 dev->ifname, vhost_message_str[request]);
2973 VHOST_LOG_CONFIG(DEBUG, "(%s) read message %s\n",
2974 dev->ifname, vhost_message_str[request]);
2976 VHOST_LOG_CONFIG(DEBUG, "(%s) external request %d\n", dev->ifname, request);
2979 ret = vhost_user_check_and_alloc_queue_pair(dev, &ctx);
2981 VHOST_LOG_CONFIG(ERR, "(%s) failed to alloc queue\n", dev->ifname);
2986 * Note: we don't lock all queues on VHOST_USER_GET_VRING_BASE
2987 * and VHOST_USER_RESET_OWNER, since it is sent when virtio stops
2988 * and device is destroyed. destroy_device waits for queues to be
2989 * inactive, so it is safe. Otherwise taking the access_lock
2990 * would cause a dead lock.
2993 case VHOST_USER_SET_FEATURES:
2994 case VHOST_USER_SET_PROTOCOL_FEATURES:
2995 case VHOST_USER_SET_OWNER:
2996 case VHOST_USER_SET_MEM_TABLE:
2997 case VHOST_USER_SET_LOG_BASE:
2998 case VHOST_USER_SET_LOG_FD:
2999 case VHOST_USER_SET_VRING_NUM:
3000 case VHOST_USER_SET_VRING_ADDR:
3001 case VHOST_USER_SET_VRING_BASE:
3002 case VHOST_USER_SET_VRING_KICK:
3003 case VHOST_USER_SET_VRING_CALL:
3004 case VHOST_USER_SET_VRING_ERR:
3005 case VHOST_USER_SET_VRING_ENABLE:
3006 case VHOST_USER_SEND_RARP:
3007 case VHOST_USER_NET_SET_MTU:
3008 case VHOST_USER_SET_SLAVE_REQ_FD:
3009 if (!(dev->flags & VIRTIO_DEV_VDPA_CONFIGURED)) {
3010 vhost_user_lock_all_queue_pairs(dev);
3011 unlock_required = 1;
3020 if (dev->extern_ops.pre_msg_handle) {
3021 ret = (*dev->extern_ops.pre_msg_handle)(dev->vid,
3024 case RTE_VHOST_MSG_RESULT_REPLY:
3025 send_vhost_reply(dev, fd, &ctx);
3027 case RTE_VHOST_MSG_RESULT_ERR:
3028 case RTE_VHOST_MSG_RESULT_OK:
3030 goto skip_to_post_handle;
3031 case RTE_VHOST_MSG_RESULT_NOT_HANDLED:
3037 if (request > VHOST_USER_NONE && request < VHOST_USER_MAX) {
3038 if (!vhost_message_handlers[request])
3039 goto skip_to_post_handle;
3040 ret = vhost_message_handlers[request](&dev, &ctx, fd);
3043 case RTE_VHOST_MSG_RESULT_ERR:
3044 VHOST_LOG_CONFIG(ERR, "(%s) processing %s failed.\n",
3045 dev->ifname, vhost_message_str[request]);
3048 case RTE_VHOST_MSG_RESULT_OK:
3049 VHOST_LOG_CONFIG(DEBUG, "(%s) processing %s succeeded.\n",
3050 dev->ifname, vhost_message_str[request]);
3053 case RTE_VHOST_MSG_RESULT_REPLY:
3054 VHOST_LOG_CONFIG(DEBUG, "(%s) processing %s succeeded and needs reply.\n",
3055 dev->ifname, vhost_message_str[request]);
3056 send_vhost_reply(dev, fd, &ctx);
3064 skip_to_post_handle:
3065 if (ret != RTE_VHOST_MSG_RESULT_ERR &&
3066 dev->extern_ops.post_msg_handle) {
3067 ret = (*dev->extern_ops.post_msg_handle)(dev->vid,
3070 case RTE_VHOST_MSG_RESULT_REPLY:
3071 send_vhost_reply(dev, fd, &ctx);
3073 case RTE_VHOST_MSG_RESULT_ERR:
3074 case RTE_VHOST_MSG_RESULT_OK:
3076 case RTE_VHOST_MSG_RESULT_NOT_HANDLED:
3082 /* If message was not handled at this stage, treat it as an error */
3084 VHOST_LOG_CONFIG(ERR, "(%s) vhost message (req: %d) was not handled.\n",
3085 dev->ifname, request);
3086 close_msg_fds(&ctx);
3087 ret = RTE_VHOST_MSG_RESULT_ERR;
3091 * If the request required a reply that was already sent,
3092 * this optional reply-ack won't be sent as the
3093 * VHOST_USER_NEED_REPLY was cleared in send_vhost_reply().
3095 if (ctx.msg.flags & VHOST_USER_NEED_REPLY) {
3096 ctx.msg.payload.u64 = ret == RTE_VHOST_MSG_RESULT_ERR;
3097 ctx.msg.size = sizeof(ctx.msg.payload.u64);
3099 send_vhost_reply(dev, fd, &ctx);
3100 } else if (ret == RTE_VHOST_MSG_RESULT_ERR) {
3101 VHOST_LOG_CONFIG(ERR, "(%s) vhost message handling failed.\n", dev->ifname);
3105 for (i = 0; i < dev->nr_vring; i++) {
3106 struct vhost_virtqueue *vq = dev->virtqueue[i];
3107 bool cur_ready = vq_is_ready(dev, vq);
3109 if (cur_ready != (vq && vq->ready)) {
3110 vq->ready = cur_ready;
3111 vhost_user_notify_queue_state(dev, i, cur_ready);
3115 if (unlock_required)
3116 vhost_user_unlock_all_queue_pairs(dev);
3118 if (!virtio_is_ready(dev))
3122 * Virtio is now ready. If not done already, it is time
3123 * to notify the application it can process the rings and
3124 * configure the vDPA device if present.
3127 if (!(dev->flags & VIRTIO_DEV_RUNNING)) {
3128 if (dev->notify_ops->new_device(dev->vid) == 0)
3129 dev->flags |= VIRTIO_DEV_RUNNING;
3132 vdpa_dev = dev->vdpa_dev;
3136 if (!(dev->flags & VIRTIO_DEV_VDPA_CONFIGURED)) {
3137 if (vdpa_dev->ops->dev_conf(dev->vid))
3138 VHOST_LOG_CONFIG(ERR, "(%s) failed to configure vDPA device\n",
3141 dev->flags |= VIRTIO_DEV_VDPA_CONFIGURED;
3148 static int process_slave_message_reply(struct virtio_net *dev,
3149 const struct vhu_msg_context *ctx)
3151 struct vhu_msg_context msg_reply;
3154 if ((ctx->msg.flags & VHOST_USER_NEED_REPLY) == 0)
3157 ret = read_vhost_message(dev, dev->slave_req_fd, &msg_reply);
3160 VHOST_LOG_CONFIG(ERR, "(%s) vhost read slave message reply failed\n",
3163 VHOST_LOG_CONFIG(INFO, "(%s) vhost peer closed\n", dev->ifname);
3169 if (msg_reply.msg.request.slave != ctx->msg.request.slave) {
3170 VHOST_LOG_CONFIG(ERR, "(%s) received unexpected msg type (%u), expected %u\n",
3171 dev->ifname, msg_reply.msg.request.slave, ctx->msg.request.slave);
3176 ret = msg_reply.msg.payload.u64 ? -1 : 0;
3179 rte_spinlock_unlock(&dev->slave_req_lock);
3184 vhost_user_iotlb_miss(struct virtio_net *dev, uint64_t iova, uint8_t perm)
3187 struct vhu_msg_context ctx = {
3189 .request.slave = VHOST_USER_SLAVE_IOTLB_MSG,
3190 .flags = VHOST_USER_VERSION,
3191 .size = sizeof(ctx.msg.payload.iotlb),
3195 .type = VHOST_IOTLB_MISS,
3200 ret = send_vhost_message(dev, dev->slave_req_fd, &ctx);
3202 VHOST_LOG_CONFIG(ERR, "(%s) failed to send IOTLB miss message (%d)\n",
3211 vhost_user_slave_config_change(struct virtio_net *dev, bool need_reply)
3214 struct vhu_msg_context ctx = {
3216 .request.slave = VHOST_USER_SLAVE_CONFIG_CHANGE_MSG,
3217 .flags = VHOST_USER_VERSION,
3223 ctx.msg.flags |= VHOST_USER_NEED_REPLY;
3225 ret = send_vhost_slave_message(dev, &ctx);
3227 VHOST_LOG_CONFIG(ERR, "(%s) failed to send config change (%d)\n",
3232 return process_slave_message_reply(dev, &ctx);
3236 rte_vhost_slave_config_change(int vid, bool need_reply)
3238 struct virtio_net *dev;
3240 dev = get_device(vid);
3244 return vhost_user_slave_config_change(dev, need_reply);
3247 static int vhost_user_slave_set_vring_host_notifier(struct virtio_net *dev,
3253 struct vhu_msg_context ctx = {
3255 .request.slave = VHOST_USER_SLAVE_VRING_HOST_NOTIFIER_MSG,
3256 .flags = VHOST_USER_VERSION | VHOST_USER_NEED_REPLY,
3257 .size = sizeof(ctx.msg.payload.area),
3259 .u64 = index & VHOST_USER_VRING_IDX_MASK,
3267 ctx.msg.payload.area.u64 |= VHOST_USER_VRING_NOFD_MASK;
3273 ret = send_vhost_slave_message(dev, &ctx);
3275 VHOST_LOG_CONFIG(ERR, "(%s) failed to set host notifier (%d)\n",
3280 return process_slave_message_reply(dev, &ctx);
3283 int rte_vhost_host_notifier_ctrl(int vid, uint16_t qid, bool enable)
3285 struct virtio_net *dev;
3286 struct rte_vdpa_device *vdpa_dev;
3287 int vfio_device_fd, ret = 0;
3288 uint64_t offset, size;
3289 unsigned int i, q_start, q_last;
3291 dev = get_device(vid);
3295 vdpa_dev = dev->vdpa_dev;
3296 if (vdpa_dev == NULL)
3299 if (!(dev->features & (1ULL << VIRTIO_F_VERSION_1)) ||
3300 !(dev->features & (1ULL << VHOST_USER_F_PROTOCOL_FEATURES)) ||
3301 !(dev->protocol_features &
3302 (1ULL << VHOST_USER_PROTOCOL_F_SLAVE_REQ)) ||
3303 !(dev->protocol_features &
3304 (1ULL << VHOST_USER_PROTOCOL_F_SLAVE_SEND_FD)) ||
3305 !(dev->protocol_features &
3306 (1ULL << VHOST_USER_PROTOCOL_F_HOST_NOTIFIER)))
3309 if (qid == RTE_VHOST_QUEUE_ALL) {
3311 q_last = dev->nr_vring - 1;
3313 if (qid >= dev->nr_vring)
3319 RTE_FUNC_PTR_OR_ERR_RET(vdpa_dev->ops->get_vfio_device_fd, -ENOTSUP);
3320 RTE_FUNC_PTR_OR_ERR_RET(vdpa_dev->ops->get_notify_area, -ENOTSUP);
3322 vfio_device_fd = vdpa_dev->ops->get_vfio_device_fd(vid);
3323 if (vfio_device_fd < 0)
3327 for (i = q_start; i <= q_last; i++) {
3328 if (vdpa_dev->ops->get_notify_area(vid, i, &offset,
3334 if (vhost_user_slave_set_vring_host_notifier(dev, i,
3335 vfio_device_fd, offset, size) < 0) {
3342 for (i = q_start; i <= q_last; i++) {
3343 vhost_user_slave_set_vring_host_notifier(dev, i, -1,