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)
222 struct rte_vdpa_device *vdpa_dev;
224 vdpa_dev = dev->vdpa_dev;
225 if (vdpa_dev && vdpa_dev->ops->dev_cleanup != NULL)
226 vdpa_dev->ops->dev_cleanup(dev->vid);
229 free_mem_region(dev);
234 rte_free(dev->guest_pages);
235 dev->guest_pages = NULL;
238 munmap((void *)(uintptr_t)dev->log_addr, dev->log_size);
242 if (dev->inflight_info) {
243 if (dev->inflight_info->addr) {
244 munmap(dev->inflight_info->addr,
245 dev->inflight_info->size);
246 dev->inflight_info->addr = NULL;
249 if (dev->inflight_info->fd >= 0) {
250 close(dev->inflight_info->fd);
251 dev->inflight_info->fd = -1;
254 rte_free(dev->inflight_info);
255 dev->inflight_info = NULL;
258 if (dev->slave_req_fd >= 0) {
259 close(dev->slave_req_fd);
260 dev->slave_req_fd = -1;
263 if (dev->postcopy_ufd >= 0) {
264 close(dev->postcopy_ufd);
265 dev->postcopy_ufd = -1;
268 dev->postcopy_listening = 0;
272 vhost_user_notify_queue_state(struct virtio_net *dev, uint16_t index,
275 struct rte_vdpa_device *vdpa_dev = dev->vdpa_dev;
276 struct vhost_virtqueue *vq = dev->virtqueue[index];
278 /* Configure guest notifications on enable */
279 if (enable && vq->notif_enable != VIRTIO_UNINITIALIZED_NOTIF)
280 vhost_enable_guest_notification(dev, vq, vq->notif_enable);
282 if (vdpa_dev && vdpa_dev->ops->set_vring_state)
283 vdpa_dev->ops->set_vring_state(dev->vid, index, enable);
285 if (dev->notify_ops->vring_state_changed)
286 dev->notify_ops->vring_state_changed(dev->vid,
291 * This function just returns success at the moment unless
292 * the device hasn't been initialised.
295 vhost_user_set_owner(struct virtio_net **pdev,
296 struct vhu_msg_context *ctx,
297 int main_fd __rte_unused)
299 struct virtio_net *dev = *pdev;
301 if (validate_msg_fds(dev, ctx, 0) != 0)
302 return RTE_VHOST_MSG_RESULT_ERR;
304 return RTE_VHOST_MSG_RESULT_OK;
308 vhost_user_reset_owner(struct virtio_net **pdev,
309 struct vhu_msg_context *ctx,
310 int main_fd __rte_unused)
312 struct virtio_net *dev = *pdev;
314 if (validate_msg_fds(dev, ctx, 0) != 0)
315 return RTE_VHOST_MSG_RESULT_ERR;
317 vhost_destroy_device_notify(dev);
319 cleanup_device(dev, 0);
321 return RTE_VHOST_MSG_RESULT_OK;
325 * The features that we support are requested.
328 vhost_user_get_features(struct virtio_net **pdev,
329 struct vhu_msg_context *ctx,
330 int main_fd __rte_unused)
332 struct virtio_net *dev = *pdev;
333 uint64_t features = 0;
335 if (validate_msg_fds(dev, ctx, 0) != 0)
336 return RTE_VHOST_MSG_RESULT_ERR;
338 rte_vhost_driver_get_features(dev->ifname, &features);
340 ctx->msg.payload.u64 = features;
341 ctx->msg.size = sizeof(ctx->msg.payload.u64);
344 return RTE_VHOST_MSG_RESULT_REPLY;
348 * The queue number that we support are requested.
351 vhost_user_get_queue_num(struct virtio_net **pdev,
352 struct vhu_msg_context *ctx,
353 int main_fd __rte_unused)
355 struct virtio_net *dev = *pdev;
356 uint32_t queue_num = 0;
358 if (validate_msg_fds(dev, ctx, 0) != 0)
359 return RTE_VHOST_MSG_RESULT_ERR;
361 rte_vhost_driver_get_queue_num(dev->ifname, &queue_num);
363 ctx->msg.payload.u64 = (uint64_t)queue_num;
364 ctx->msg.size = sizeof(ctx->msg.payload.u64);
367 return RTE_VHOST_MSG_RESULT_REPLY;
371 * We receive the negotiated features supported by us and the virtio device.
374 vhost_user_set_features(struct virtio_net **pdev,
375 struct vhu_msg_context *ctx,
376 int main_fd __rte_unused)
378 struct virtio_net *dev = *pdev;
379 uint64_t features = ctx->msg.payload.u64;
380 uint64_t vhost_features = 0;
381 struct rte_vdpa_device *vdpa_dev;
383 if (validate_msg_fds(dev, ctx, 0) != 0)
384 return RTE_VHOST_MSG_RESULT_ERR;
386 rte_vhost_driver_get_features(dev->ifname, &vhost_features);
387 if (features & ~vhost_features) {
388 VHOST_LOG_CONFIG(ERR, "(%s) received invalid negotiated features.\n",
390 dev->flags |= VIRTIO_DEV_FEATURES_FAILED;
391 dev->status &= ~VIRTIO_DEVICE_STATUS_FEATURES_OK;
393 return RTE_VHOST_MSG_RESULT_ERR;
396 if (dev->flags & VIRTIO_DEV_RUNNING) {
397 if (dev->features == features)
398 return RTE_VHOST_MSG_RESULT_OK;
401 * Error out if master tries to change features while device is
402 * in running state. The exception being VHOST_F_LOG_ALL, which
403 * is enabled when the live-migration starts.
405 if ((dev->features ^ features) & ~(1ULL << VHOST_F_LOG_ALL)) {
406 VHOST_LOG_CONFIG(ERR, "(%s) features changed while device is running.\n",
408 return RTE_VHOST_MSG_RESULT_ERR;
411 if (dev->notify_ops->features_changed)
412 dev->notify_ops->features_changed(dev->vid, features);
415 dev->features = features;
417 ((1ULL << VIRTIO_NET_F_MRG_RXBUF) |
418 (1ULL << VIRTIO_F_VERSION_1) |
419 (1ULL << VIRTIO_F_RING_PACKED))) {
420 dev->vhost_hlen = sizeof(struct virtio_net_hdr_mrg_rxbuf);
422 dev->vhost_hlen = sizeof(struct virtio_net_hdr);
424 VHOST_LOG_CONFIG(INFO, "(%s) negotiated Virtio features: 0x%" PRIx64 "\n",
425 dev->ifname, dev->features);
426 VHOST_LOG_CONFIG(DEBUG, "(%s) mergeable RX buffers %s, virtio 1 %s\n",
428 (dev->features & (1 << VIRTIO_NET_F_MRG_RXBUF)) ? "on" : "off",
429 (dev->features & (1ULL << VIRTIO_F_VERSION_1)) ? "on" : "off");
431 if ((dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET) &&
432 !(dev->features & (1ULL << VIRTIO_NET_F_MQ))) {
434 * Remove all but first queue pair if MQ hasn't been
435 * negotiated. This is safe because the device is not
436 * running at this stage.
438 while (dev->nr_vring > 2) {
439 struct vhost_virtqueue *vq;
441 vq = dev->virtqueue[--dev->nr_vring];
445 dev->virtqueue[dev->nr_vring] = NULL;
447 cleanup_vq_inflight(dev, vq);
452 vdpa_dev = dev->vdpa_dev;
454 vdpa_dev->ops->set_features(dev->vid);
456 dev->flags &= ~VIRTIO_DEV_FEATURES_FAILED;
457 return RTE_VHOST_MSG_RESULT_OK;
461 * The virtio device sends us the size of the descriptor ring.
464 vhost_user_set_vring_num(struct virtio_net **pdev,
465 struct vhu_msg_context *ctx,
466 int main_fd __rte_unused)
468 struct virtio_net *dev = *pdev;
469 struct vhost_virtqueue *vq = dev->virtqueue[ctx->msg.payload.state.index];
471 if (validate_msg_fds(dev, ctx, 0) != 0)
472 return RTE_VHOST_MSG_RESULT_ERR;
474 if (ctx->msg.payload.state.num > 32768) {
475 VHOST_LOG_CONFIG(ERR, "(%s) invalid virtqueue size %u\n",
476 dev->ifname, ctx->msg.payload.state.num);
477 return RTE_VHOST_MSG_RESULT_ERR;
480 vq->size = ctx->msg.payload.state.num;
482 /* VIRTIO 1.0, 2.4 Virtqueues says:
484 * Queue Size value is always a power of 2. The maximum Queue Size
487 * VIRTIO 1.1 2.7 Virtqueues says:
489 * Packed virtqueues support up to 2^15 entries each.
491 if (!vq_is_packed(dev)) {
492 if (vq->size & (vq->size - 1)) {
493 VHOST_LOG_CONFIG(ERR, "(%s) invalid virtqueue size %u\n",
494 dev->ifname, vq->size);
495 return RTE_VHOST_MSG_RESULT_ERR;
499 if (vq_is_packed(dev)) {
500 rte_free(vq->shadow_used_packed);
501 vq->shadow_used_packed = rte_malloc_socket(NULL,
503 sizeof(struct vring_used_elem_packed),
504 RTE_CACHE_LINE_SIZE, vq->numa_node);
505 if (!vq->shadow_used_packed) {
506 VHOST_LOG_CONFIG(ERR,
507 "(%s) failed to allocate memory for shadow used ring.\n",
509 return RTE_VHOST_MSG_RESULT_ERR;
513 rte_free(vq->shadow_used_split);
515 vq->shadow_used_split = rte_malloc_socket(NULL,
516 vq->size * sizeof(struct vring_used_elem),
517 RTE_CACHE_LINE_SIZE, vq->numa_node);
519 if (!vq->shadow_used_split) {
520 VHOST_LOG_CONFIG(ERR,
521 "(%s) failed to allocate memory for vq internal data.\n",
523 return RTE_VHOST_MSG_RESULT_ERR;
527 rte_free(vq->batch_copy_elems);
528 vq->batch_copy_elems = rte_malloc_socket(NULL,
529 vq->size * sizeof(struct batch_copy_elem),
530 RTE_CACHE_LINE_SIZE, vq->numa_node);
531 if (!vq->batch_copy_elems) {
532 VHOST_LOG_CONFIG(ERR, "(%s) failed to allocate memory for batching copy.\n",
534 return RTE_VHOST_MSG_RESULT_ERR;
537 return RTE_VHOST_MSG_RESULT_OK;
541 * Reallocate virtio_dev, vhost_virtqueue and related data structures to
542 * make them on the same numa node as the memory of vring descriptor.
544 #ifdef RTE_LIBRTE_VHOST_NUMA
545 static struct virtio_net*
546 numa_realloc(struct virtio_net *dev, int index)
549 struct virtio_net *old_dev;
550 struct vhost_virtqueue *vq;
551 struct batch_copy_elem *bce;
552 struct guest_page *gp;
553 struct rte_vhost_memory *mem;
558 vq = dev->virtqueue[index];
561 * If VQ is ready, it is too late to reallocate, it certainly already
562 * happened anyway on VHOST_USER_SET_VRING_ADRR.
567 ret = get_mempolicy(&node, NULL, 0, vq->desc, MPOL_F_NODE | MPOL_F_ADDR);
569 VHOST_LOG_CONFIG(ERR, "(%s) unable to get virtqueue %d numa information.\n",
574 if (node == vq->numa_node)
575 goto out_dev_realloc;
577 vq = rte_realloc_socket(vq, sizeof(*vq), 0, node);
579 VHOST_LOG_CONFIG(ERR, "(%s) failed to realloc virtqueue %d on node %d\n",
580 dev->ifname, index, node);
584 if (vq != dev->virtqueue[index]) {
585 VHOST_LOG_CONFIG(INFO, "(%s) reallocated virtqueue on node %d\n",
587 dev->virtqueue[index] = vq;
588 vhost_user_iotlb_init(dev, index);
591 if (vq_is_packed(dev)) {
592 struct vring_used_elem_packed *sup;
594 sup = rte_realloc_socket(vq->shadow_used_packed, vq->size * sizeof(*sup),
595 RTE_CACHE_LINE_SIZE, node);
597 VHOST_LOG_CONFIG(ERR, "(%s) failed to realloc shadow packed on node %d\n",
601 vq->shadow_used_packed = sup;
603 struct vring_used_elem *sus;
605 sus = rte_realloc_socket(vq->shadow_used_split, vq->size * sizeof(*sus),
606 RTE_CACHE_LINE_SIZE, node);
608 VHOST_LOG_CONFIG(ERR, "(%s) failed to realloc shadow split on node %d\n",
612 vq->shadow_used_split = sus;
615 bce = rte_realloc_socket(vq->batch_copy_elems, vq->size * sizeof(*bce),
616 RTE_CACHE_LINE_SIZE, node);
618 VHOST_LOG_CONFIG(ERR, "(%s) failed to realloc batch copy elem on node %d\n",
622 vq->batch_copy_elems = bce;
625 struct log_cache_entry *lc;
627 lc = rte_realloc_socket(vq->log_cache, sizeof(*lc) * VHOST_LOG_CACHE_NR, 0, node);
629 VHOST_LOG_CONFIG(ERR, "(%s) failed to realloc log cache on node %d\n",
636 if (vq->resubmit_inflight) {
637 struct rte_vhost_resubmit_info *ri;
639 ri = rte_realloc_socket(vq->resubmit_inflight, sizeof(*ri), 0, node);
641 VHOST_LOG_CONFIG(ERR, "(%s) failed to realloc resubmit inflight on node %d\n",
645 vq->resubmit_inflight = ri;
647 if (ri->resubmit_list) {
648 struct rte_vhost_resubmit_desc *rd;
650 rd = rte_realloc_socket(ri->resubmit_list, sizeof(*rd) * ri->resubmit_num,
653 VHOST_LOG_CONFIG(ERR, "(%s) failed to realloc resubmit list on node %d\n",
657 ri->resubmit_list = rd;
661 vq->numa_node = node;
665 if (dev->flags & VIRTIO_DEV_RUNNING)
668 ret = get_mempolicy(&dev_node, NULL, 0, dev, MPOL_F_NODE | MPOL_F_ADDR);
670 VHOST_LOG_CONFIG(ERR, "(%s) unable to get numa information.\n", dev->ifname);
674 if (dev_node == node)
677 dev = rte_realloc_socket(old_dev, sizeof(*dev), 0, node);
679 VHOST_LOG_CONFIG(ERR, "(%s) failed to realloc dev on node %d\n",
680 old_dev->ifname, node);
684 VHOST_LOG_CONFIG(INFO, "(%s) reallocated device on node %d\n", dev->ifname, node);
685 vhost_devices[dev->vid] = dev;
687 mem_size = sizeof(struct rte_vhost_memory) +
688 sizeof(struct rte_vhost_mem_region) * dev->mem->nregions;
689 mem = rte_realloc_socket(dev->mem, mem_size, 0, node);
691 VHOST_LOG_CONFIG(ERR, "(%s) failed to realloc mem table on node %d\n",
697 gp = rte_realloc_socket(dev->guest_pages, dev->max_guest_pages * sizeof(*gp),
698 RTE_CACHE_LINE_SIZE, node);
700 VHOST_LOG_CONFIG(ERR, "(%s) failed to realloc guest pages on node %d\n",
704 dev->guest_pages = gp;
709 static struct virtio_net*
710 numa_realloc(struct virtio_net *dev, int index __rte_unused)
716 /* Converts QEMU virtual address to Vhost virtual address. */
718 qva_to_vva(struct virtio_net *dev, uint64_t qva, uint64_t *len)
720 struct rte_vhost_mem_region *r;
723 if (unlikely(!dev || !dev->mem))
726 /* Find the region where the address lives. */
727 for (i = 0; i < dev->mem->nregions; i++) {
728 r = &dev->mem->regions[i];
730 if (qva >= r->guest_user_addr &&
731 qva < r->guest_user_addr + r->size) {
733 if (unlikely(*len > r->guest_user_addr + r->size - qva))
734 *len = r->guest_user_addr + r->size - qva;
736 return qva - r->guest_user_addr +
748 * Converts ring address to Vhost virtual address.
749 * If IOMMU is enabled, the ring address is a guest IO virtual address,
750 * else it is a QEMU virtual address.
753 ring_addr_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
754 uint64_t ra, uint64_t *size)
756 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)) {
759 vhost_user_iotlb_rd_lock(vq);
760 vva = vhost_iova_to_vva(dev, vq, ra,
761 size, VHOST_ACCESS_RW);
762 vhost_user_iotlb_rd_unlock(vq);
767 return qva_to_vva(dev, ra, size);
771 log_addr_to_gpa(struct virtio_net *dev, struct vhost_virtqueue *vq)
775 vhost_user_iotlb_rd_lock(vq);
776 log_gpa = translate_log_addr(dev, vq, vq->ring_addrs.log_guest_addr);
777 vhost_user_iotlb_rd_unlock(vq);
782 static struct virtio_net *
783 translate_ring_addresses(struct virtio_net *dev, int vq_index)
785 struct vhost_virtqueue *vq = dev->virtqueue[vq_index];
786 struct vhost_vring_addr *addr = &vq->ring_addrs;
787 uint64_t len, expected_len;
789 if (addr->flags & (1 << VHOST_VRING_F_LOG)) {
791 log_addr_to_gpa(dev, vq);
792 if (vq->log_guest_addr == 0) {
793 VHOST_LOG_CONFIG(DEBUG, "(%s) failed to map log_guest_addr.\n",
799 if (vq_is_packed(dev)) {
800 len = sizeof(struct vring_packed_desc) * vq->size;
801 vq->desc_packed = (struct vring_packed_desc *)(uintptr_t)
802 ring_addr_to_vva(dev, vq, addr->desc_user_addr, &len);
803 if (vq->desc_packed == NULL ||
804 len != sizeof(struct vring_packed_desc) *
806 VHOST_LOG_CONFIG(DEBUG, "(%s) failed to map desc_packed ring.\n",
811 dev = numa_realloc(dev, vq_index);
812 vq = dev->virtqueue[vq_index];
813 addr = &vq->ring_addrs;
815 len = sizeof(struct vring_packed_desc_event);
816 vq->driver_event = (struct vring_packed_desc_event *)
817 (uintptr_t)ring_addr_to_vva(dev,
818 vq, addr->avail_user_addr, &len);
819 if (vq->driver_event == NULL ||
820 len != sizeof(struct vring_packed_desc_event)) {
821 VHOST_LOG_CONFIG(DEBUG, "(%s) failed to find driver area address.\n",
826 len = sizeof(struct vring_packed_desc_event);
827 vq->device_event = (struct vring_packed_desc_event *)
828 (uintptr_t)ring_addr_to_vva(dev,
829 vq, addr->used_user_addr, &len);
830 if (vq->device_event == NULL ||
831 len != sizeof(struct vring_packed_desc_event)) {
832 VHOST_LOG_CONFIG(DEBUG, "(%s) failed to find device area address.\n",
837 vq->access_ok = true;
841 /* The addresses are converted from QEMU virtual to Vhost virtual. */
842 if (vq->desc && vq->avail && vq->used)
845 len = sizeof(struct vring_desc) * vq->size;
846 vq->desc = (struct vring_desc *)(uintptr_t)ring_addr_to_vva(dev,
847 vq, addr->desc_user_addr, &len);
848 if (vq->desc == 0 || len != sizeof(struct vring_desc) * vq->size) {
849 VHOST_LOG_CONFIG(DEBUG, "(%s) failed to map desc ring.\n", dev->ifname);
853 dev = numa_realloc(dev, vq_index);
854 vq = dev->virtqueue[vq_index];
855 addr = &vq->ring_addrs;
857 len = sizeof(struct vring_avail) + sizeof(uint16_t) * vq->size;
858 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))
859 len += sizeof(uint16_t);
861 vq->avail = (struct vring_avail *)(uintptr_t)ring_addr_to_vva(dev,
862 vq, addr->avail_user_addr, &len);
863 if (vq->avail == 0 || len != expected_len) {
864 VHOST_LOG_CONFIG(DEBUG, "(%s) failed to map avail ring.\n", dev->ifname);
868 len = sizeof(struct vring_used) +
869 sizeof(struct vring_used_elem) * vq->size;
870 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))
871 len += sizeof(uint16_t);
873 vq->used = (struct vring_used *)(uintptr_t)ring_addr_to_vva(dev,
874 vq, addr->used_user_addr, &len);
875 if (vq->used == 0 || len != expected_len) {
876 VHOST_LOG_CONFIG(DEBUG, "(%s) failed to map used ring.\n", dev->ifname);
880 if (vq->last_used_idx != vq->used->idx) {
881 VHOST_LOG_CONFIG(WARNING, "(%s) last_used_idx (%u) and vq->used->idx (%u) mismatches;\n",
883 vq->last_used_idx, vq->used->idx);
884 vq->last_used_idx = vq->used->idx;
885 vq->last_avail_idx = vq->used->idx;
886 VHOST_LOG_CONFIG(WARNING, "(%s) some packets maybe resent for Tx and dropped for Rx\n",
890 vq->access_ok = true;
892 VHOST_LOG_CONFIG(DEBUG, "(%s) mapped address desc: %p\n", dev->ifname, vq->desc);
893 VHOST_LOG_CONFIG(DEBUG, "(%s) mapped address avail: %p\n", dev->ifname, vq->avail);
894 VHOST_LOG_CONFIG(DEBUG, "(%s) mapped address used: %p\n", dev->ifname, vq->used);
895 VHOST_LOG_CONFIG(DEBUG, "(%s) log_guest_addr: %" PRIx64 "\n",
896 dev->ifname, vq->log_guest_addr);
902 * The virtio device sends us the desc, used and avail ring addresses.
903 * This function then converts these to our address space.
906 vhost_user_set_vring_addr(struct virtio_net **pdev,
907 struct vhu_msg_context *ctx,
908 int main_fd __rte_unused)
910 struct virtio_net *dev = *pdev;
911 struct vhost_virtqueue *vq;
912 struct vhost_vring_addr *addr = &ctx->msg.payload.addr;
915 if (validate_msg_fds(dev, ctx, 0) != 0)
916 return RTE_VHOST_MSG_RESULT_ERR;
918 if (dev->mem == NULL)
919 return RTE_VHOST_MSG_RESULT_ERR;
921 /* addr->index refers to the queue index. The txq 1, rxq is 0. */
922 vq = dev->virtqueue[ctx->msg.payload.addr.index];
924 access_ok = vq->access_ok;
927 * Rings addresses should not be interpreted as long as the ring is not
928 * started and enabled
930 memcpy(&vq->ring_addrs, addr, sizeof(*addr));
932 vring_invalidate(dev, vq);
934 if ((vq->enabled && (dev->features &
935 (1ULL << VHOST_USER_F_PROTOCOL_FEATURES))) ||
937 dev = translate_ring_addresses(dev, ctx->msg.payload.addr.index);
939 return RTE_VHOST_MSG_RESULT_ERR;
944 return RTE_VHOST_MSG_RESULT_OK;
948 * The virtio device sends us the available ring last used index.
951 vhost_user_set_vring_base(struct virtio_net **pdev,
952 struct vhu_msg_context *ctx,
953 int main_fd __rte_unused)
955 struct virtio_net *dev = *pdev;
956 struct vhost_virtqueue *vq = dev->virtqueue[ctx->msg.payload.state.index];
957 uint64_t val = ctx->msg.payload.state.num;
959 if (validate_msg_fds(dev, ctx, 0) != 0)
960 return RTE_VHOST_MSG_RESULT_ERR;
962 if (vq_is_packed(dev)) {
964 * Bit[0:14]: avail index
965 * Bit[15]: avail wrap counter
967 vq->last_avail_idx = val & 0x7fff;
968 vq->avail_wrap_counter = !!(val & (0x1 << 15));
970 * Set used index to same value as available one, as
971 * their values should be the same since ring processing
972 * was stopped at get time.
974 vq->last_used_idx = vq->last_avail_idx;
975 vq->used_wrap_counter = vq->avail_wrap_counter;
977 vq->last_used_idx = ctx->msg.payload.state.num;
978 vq->last_avail_idx = ctx->msg.payload.state.num;
981 VHOST_LOG_CONFIG(INFO,
982 "(%s) vring base idx:%u last_used_idx:%u last_avail_idx:%u.\n",
983 dev->ifname, ctx->msg.payload.state.index, vq->last_used_idx,
986 return RTE_VHOST_MSG_RESULT_OK;
990 add_one_guest_page(struct virtio_net *dev, uint64_t guest_phys_addr,
991 uint64_t host_phys_addr, uint64_t size)
993 struct guest_page *page, *last_page;
994 struct guest_page *old_pages;
996 if (dev->nr_guest_pages == dev->max_guest_pages) {
997 dev->max_guest_pages *= 2;
998 old_pages = dev->guest_pages;
999 dev->guest_pages = rte_realloc(dev->guest_pages,
1000 dev->max_guest_pages * sizeof(*page),
1001 RTE_CACHE_LINE_SIZE);
1002 if (dev->guest_pages == NULL) {
1003 VHOST_LOG_CONFIG(ERR, "(%s) cannot realloc guest_pages\n", dev->ifname);
1004 rte_free(old_pages);
1009 if (dev->nr_guest_pages > 0) {
1010 last_page = &dev->guest_pages[dev->nr_guest_pages - 1];
1011 /* merge if the two pages are continuous */
1012 if (host_phys_addr == last_page->host_phys_addr +
1014 last_page->size += size;
1019 page = &dev->guest_pages[dev->nr_guest_pages++];
1020 page->guest_phys_addr = guest_phys_addr;
1021 page->host_phys_addr = host_phys_addr;
1028 add_guest_pages(struct virtio_net *dev, struct rte_vhost_mem_region *reg,
1031 uint64_t reg_size = reg->size;
1032 uint64_t host_user_addr = reg->host_user_addr;
1033 uint64_t guest_phys_addr = reg->guest_phys_addr;
1034 uint64_t host_phys_addr;
1037 host_phys_addr = rte_mem_virt2iova((void *)(uintptr_t)host_user_addr);
1038 size = page_size - (guest_phys_addr & (page_size - 1));
1039 size = RTE_MIN(size, reg_size);
1041 if (add_one_guest_page(dev, guest_phys_addr, host_phys_addr, size) < 0)
1044 host_user_addr += size;
1045 guest_phys_addr += size;
1048 while (reg_size > 0) {
1049 size = RTE_MIN(reg_size, page_size);
1050 host_phys_addr = rte_mem_virt2iova((void *)(uintptr_t)
1052 if (add_one_guest_page(dev, guest_phys_addr, host_phys_addr,
1056 host_user_addr += size;
1057 guest_phys_addr += size;
1061 /* sort guest page array if over binary search threshold */
1062 if (dev->nr_guest_pages >= VHOST_BINARY_SEARCH_THRESH) {
1063 qsort((void *)dev->guest_pages, dev->nr_guest_pages,
1064 sizeof(struct guest_page), guest_page_addrcmp);
1070 #ifdef RTE_LIBRTE_VHOST_DEBUG
1071 /* TODO: enable it only in debug mode? */
1073 dump_guest_pages(struct virtio_net *dev)
1076 struct guest_page *page;
1078 for (i = 0; i < dev->nr_guest_pages; i++) {
1079 page = &dev->guest_pages[i];
1081 VHOST_LOG_CONFIG(INFO, "(%s) guest physical page region %u\n",
1083 VHOST_LOG_CONFIG(INFO, "(%s)\tguest_phys_addr: %" PRIx64 "\n",
1084 dev->ifname, page->guest_phys_addr);
1085 VHOST_LOG_CONFIG(INFO, "(%s)\thost_phys_addr : %" PRIx64 "\n",
1086 dev->ifname, page->host_phys_addr);
1087 VHOST_LOG_CONFIG(INFO, "(%s)\tsize : %" PRIx64 "\n",
1088 dev->ifname, page->size);
1092 #define dump_guest_pages(dev)
1096 vhost_memory_changed(struct VhostUserMemory *new,
1097 struct rte_vhost_memory *old)
1101 if (new->nregions != old->nregions)
1104 for (i = 0; i < new->nregions; ++i) {
1105 VhostUserMemoryRegion *new_r = &new->regions[i];
1106 struct rte_vhost_mem_region *old_r = &old->regions[i];
1108 if (new_r->guest_phys_addr != old_r->guest_phys_addr)
1110 if (new_r->memory_size != old_r->size)
1112 if (new_r->userspace_addr != old_r->guest_user_addr)
1119 #ifdef RTE_LIBRTE_VHOST_POSTCOPY
1121 vhost_user_postcopy_region_register(struct virtio_net *dev,
1122 struct rte_vhost_mem_region *reg)
1124 struct uffdio_register reg_struct;
1127 * Let's register all the mmapped area to ensure
1128 * alignment on page boundary.
1130 reg_struct.range.start = (uint64_t)(uintptr_t)reg->mmap_addr;
1131 reg_struct.range.len = reg->mmap_size;
1132 reg_struct.mode = UFFDIO_REGISTER_MODE_MISSING;
1134 if (ioctl(dev->postcopy_ufd, UFFDIO_REGISTER,
1136 VHOST_LOG_CONFIG(ERR, "(%s) failed to register ufd for region "
1137 "%" PRIx64 " - %" PRIx64 " (ufd = %d) %s\n",
1139 (uint64_t)reg_struct.range.start,
1140 (uint64_t)reg_struct.range.start +
1141 (uint64_t)reg_struct.range.len - 1,
1147 VHOST_LOG_CONFIG(INFO,
1148 "(%s)\t userfaultfd registered for range : %" PRIx64 " - %" PRIx64 "\n",
1150 (uint64_t)reg_struct.range.start,
1151 (uint64_t)reg_struct.range.start +
1152 (uint64_t)reg_struct.range.len - 1);
1158 vhost_user_postcopy_region_register(struct virtio_net *dev __rte_unused,
1159 struct rte_vhost_mem_region *reg __rte_unused)
1166 vhost_user_postcopy_register(struct virtio_net *dev, int main_fd,
1167 struct vhu_msg_context *ctx)
1169 struct VhostUserMemory *memory;
1170 struct rte_vhost_mem_region *reg;
1171 struct vhu_msg_context ack_ctx;
1174 if (!dev->postcopy_listening)
1178 * We haven't a better way right now than sharing
1179 * DPDK's virtual address with Qemu, so that Qemu can
1180 * retrieve the region offset when handling userfaults.
1182 memory = &ctx->msg.payload.memory;
1183 for (i = 0; i < memory->nregions; i++) {
1184 reg = &dev->mem->regions[i];
1185 memory->regions[i].userspace_addr = reg->host_user_addr;
1188 /* Send the addresses back to qemu */
1190 send_vhost_reply(dev, main_fd, ctx);
1192 /* Wait for qemu to acknowledge it got the addresses
1193 * we've got to wait before we're allowed to generate faults.
1195 if (read_vhost_message(dev, main_fd, &ack_ctx) <= 0) {
1196 VHOST_LOG_CONFIG(ERR, "(%s) failed to read qemu ack on postcopy set-mem-table\n",
1201 if (validate_msg_fds(dev, &ack_ctx, 0) != 0)
1204 if (ack_ctx.msg.request.master != VHOST_USER_SET_MEM_TABLE) {
1205 VHOST_LOG_CONFIG(ERR, "(%s) bad qemu ack on postcopy set-mem-table (%d)\n",
1206 dev->ifname, ack_ctx.msg.request.master);
1210 /* Now userfault register and we can use the memory */
1211 for (i = 0; i < memory->nregions; i++) {
1212 reg = &dev->mem->regions[i];
1213 if (vhost_user_postcopy_region_register(dev, reg) < 0)
1221 vhost_user_mmap_region(struct virtio_net *dev,
1222 struct rte_vhost_mem_region *region,
1223 uint64_t mmap_offset)
1231 /* Check for memory_size + mmap_offset overflow */
1232 if (mmap_offset >= -region->size) {
1233 VHOST_LOG_CONFIG(ERR, "(%s) mmap_offset (%#"PRIx64") and memory_size (%#"PRIx64") overflow\n",
1234 dev->ifname, mmap_offset, region->size);
1238 mmap_size = region->size + mmap_offset;
1240 /* mmap() without flag of MAP_ANONYMOUS, should be called with length
1241 * argument aligned with hugepagesz at older longterm version Linux,
1242 * like 2.6.32 and 3.2.72, or mmap() will fail with EINVAL.
1244 * To avoid failure, make sure in caller to keep length aligned.
1246 alignment = get_blk_size(region->fd);
1247 if (alignment == (uint64_t)-1) {
1248 VHOST_LOG_CONFIG(ERR, "(%s) couldn't get hugepage size through fstat\n",
1252 mmap_size = RTE_ALIGN_CEIL(mmap_size, alignment);
1253 if (mmap_size == 0) {
1255 * It could happen if initial mmap_size + alignment overflows
1256 * the sizeof uint64, which could happen if either mmap_size or
1257 * alignment value is wrong.
1259 * mmap() kernel implementation would return an error, but
1260 * better catch it before and provide useful info in the logs.
1262 VHOST_LOG_CONFIG(ERR, "(%s) mmap size (0x%" PRIx64 ") or alignment (0x%" PRIx64 ") is invalid\n",
1263 dev->ifname, region->size + mmap_offset, alignment);
1267 populate = dev->async_copy ? MAP_POPULATE : 0;
1268 mmap_addr = mmap(NULL, mmap_size, PROT_READ | PROT_WRITE,
1269 MAP_SHARED | populate, region->fd, 0);
1271 if (mmap_addr == MAP_FAILED) {
1272 VHOST_LOG_CONFIG(ERR, "(%s) mmap failed (%s).\n", dev->ifname, strerror(errno));
1276 region->mmap_addr = mmap_addr;
1277 region->mmap_size = mmap_size;
1278 region->host_user_addr = (uint64_t)(uintptr_t)mmap_addr + mmap_offset;
1280 if (dev->async_copy) {
1281 if (add_guest_pages(dev, region, alignment) < 0) {
1282 VHOST_LOG_CONFIG(ERR, "(%s) adding guest pages to region failed.\n",
1287 if (rte_vfio_is_enabled("vfio")) {
1288 ret = async_dma_map(dev, region, true);
1290 VHOST_LOG_CONFIG(ERR,
1291 "(%s) configure IOMMU for DMA engine failed\n",
1298 VHOST_LOG_CONFIG(INFO, "(%s) guest memory region size: 0x%" PRIx64 "\n",
1299 dev->ifname, region->size);
1300 VHOST_LOG_CONFIG(INFO, "(%s)\t guest physical addr: 0x%" PRIx64 "\n",
1301 dev->ifname, region->guest_phys_addr);
1302 VHOST_LOG_CONFIG(INFO, "(%s)\t guest virtual addr: 0x%" PRIx64 "\n",
1303 dev->ifname, region->guest_user_addr);
1304 VHOST_LOG_CONFIG(INFO, "(%s)\t host virtual addr: 0x%" PRIx64 "\n",
1305 dev->ifname, region->host_user_addr);
1306 VHOST_LOG_CONFIG(INFO, "(%s)\t mmap addr : 0x%" PRIx64 "\n",
1307 dev->ifname, (uint64_t)(uintptr_t)mmap_addr);
1308 VHOST_LOG_CONFIG(INFO, "(%s)\t mmap size : 0x%" PRIx64 "\n",
1309 dev->ifname, mmap_size);
1310 VHOST_LOG_CONFIG(INFO, "(%s)\t mmap align: 0x%" PRIx64 "\n",
1311 dev->ifname, alignment);
1312 VHOST_LOG_CONFIG(INFO, "(%s)\t mmap off : 0x%" PRIx64 "\n",
1313 dev->ifname, mmap_offset);
1319 vhost_user_set_mem_table(struct virtio_net **pdev,
1320 struct vhu_msg_context *ctx,
1323 struct virtio_net *dev = *pdev;
1324 struct VhostUserMemory *memory = &ctx->msg.payload.memory;
1325 struct rte_vhost_mem_region *reg;
1326 int numa_node = SOCKET_ID_ANY;
1327 uint64_t mmap_offset;
1329 bool async_notify = false;
1331 if (validate_msg_fds(dev, ctx, memory->nregions) != 0)
1332 return RTE_VHOST_MSG_RESULT_ERR;
1334 if (memory->nregions > VHOST_MEMORY_MAX_NREGIONS) {
1335 VHOST_LOG_CONFIG(ERR, "(%s) too many memory regions (%u)\n",
1336 dev->ifname, memory->nregions);
1340 if (dev->mem && !vhost_memory_changed(memory, dev->mem)) {
1341 VHOST_LOG_CONFIG(INFO, "(%s) memory regions not changed\n", dev->ifname);
1345 return RTE_VHOST_MSG_RESULT_OK;
1349 if (dev->flags & VIRTIO_DEV_VDPA_CONFIGURED) {
1350 struct rte_vdpa_device *vdpa_dev = dev->vdpa_dev;
1352 if (vdpa_dev && vdpa_dev->ops->dev_close)
1353 vdpa_dev->ops->dev_close(dev->vid);
1354 dev->flags &= ~VIRTIO_DEV_VDPA_CONFIGURED;
1357 /* notify the vhost application to stop DMA transfers */
1358 if (dev->async_copy && dev->notify_ops->vring_state_changed) {
1359 for (i = 0; i < dev->nr_vring; i++) {
1360 dev->notify_ops->vring_state_changed(dev->vid,
1363 async_notify = true;
1366 free_mem_region(dev);
1371 /* Flush IOTLB cache as previous HVAs are now invalid */
1372 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1373 for (i = 0; i < dev->nr_vring; i++)
1374 vhost_user_iotlb_flush_all(dev->virtqueue[i]);
1377 * If VQ 0 has already been allocated, try to allocate on the same
1378 * NUMA node. It can be reallocated later in numa_realloc().
1380 if (dev->nr_vring > 0)
1381 numa_node = dev->virtqueue[0]->numa_node;
1383 dev->nr_guest_pages = 0;
1384 if (dev->guest_pages == NULL) {
1385 dev->max_guest_pages = 8;
1386 dev->guest_pages = rte_zmalloc_socket(NULL,
1387 dev->max_guest_pages *
1388 sizeof(struct guest_page),
1389 RTE_CACHE_LINE_SIZE,
1391 if (dev->guest_pages == NULL) {
1392 VHOST_LOG_CONFIG(ERR,
1393 "(%s) failed to allocate memory for dev->guest_pages\n",
1399 dev->mem = rte_zmalloc_socket("vhost-mem-table", sizeof(struct rte_vhost_memory) +
1400 sizeof(struct rte_vhost_mem_region) * memory->nregions, 0, numa_node);
1401 if (dev->mem == NULL) {
1402 VHOST_LOG_CONFIG(ERR,
1403 "(%s) failed to allocate memory for dev->mem\n",
1405 goto free_guest_pages;
1408 for (i = 0; i < memory->nregions; i++) {
1409 reg = &dev->mem->regions[i];
1411 reg->guest_phys_addr = memory->regions[i].guest_phys_addr;
1412 reg->guest_user_addr = memory->regions[i].userspace_addr;
1413 reg->size = memory->regions[i].memory_size;
1414 reg->fd = ctx->fds[i];
1417 * Assign invalid file descriptor value to avoid double
1418 * closing on error path.
1422 mmap_offset = memory->regions[i].mmap_offset;
1424 if (vhost_user_mmap_region(dev, reg, mmap_offset) < 0) {
1425 VHOST_LOG_CONFIG(ERR, "(%s) failed to mmap region %u\n", dev->ifname, i);
1426 goto free_mem_table;
1429 dev->mem->nregions++;
1432 if (vhost_user_postcopy_register(dev, main_fd, ctx) < 0)
1433 goto free_mem_table;
1435 for (i = 0; i < dev->nr_vring; i++) {
1436 struct vhost_virtqueue *vq = dev->virtqueue[i];
1441 if (vq->desc || vq->avail || vq->used) {
1443 * If the memory table got updated, the ring addresses
1444 * need to be translated again as virtual addresses have
1447 vring_invalidate(dev, vq);
1449 dev = translate_ring_addresses(dev, i);
1452 goto free_mem_table;
1459 dump_guest_pages(dev);
1462 for (i = 0; i < dev->nr_vring; i++)
1463 dev->notify_ops->vring_state_changed(dev->vid, i, 1);
1466 return RTE_VHOST_MSG_RESULT_OK;
1469 free_mem_region(dev);
1474 rte_free(dev->guest_pages);
1475 dev->guest_pages = NULL;
1478 return RTE_VHOST_MSG_RESULT_ERR;
1482 vq_is_ready(struct virtio_net *dev, struct vhost_virtqueue *vq)
1489 if (vq_is_packed(dev))
1490 rings_ok = vq->desc_packed && vq->driver_event &&
1493 rings_ok = vq->desc && vq->avail && vq->used;
1496 vq->kickfd != VIRTIO_UNINITIALIZED_EVENTFD &&
1497 vq->callfd != VIRTIO_UNINITIALIZED_EVENTFD &&
1501 #define VIRTIO_BUILTIN_NUM_VQS_TO_BE_READY 2u
1504 virtio_is_ready(struct virtio_net *dev)
1506 struct vhost_virtqueue *vq;
1507 uint32_t i, nr_vring = dev->nr_vring;
1509 if (dev->flags & VIRTIO_DEV_READY)
1515 if (dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET) {
1516 nr_vring = VIRTIO_BUILTIN_NUM_VQS_TO_BE_READY;
1518 if (dev->nr_vring < nr_vring)
1522 for (i = 0; i < nr_vring; i++) {
1523 vq = dev->virtqueue[i];
1525 if (!vq_is_ready(dev, vq))
1529 /* If supported, ensure the frontend is really done with config */
1530 if (dev->protocol_features & (1ULL << VHOST_USER_PROTOCOL_F_STATUS))
1531 if (!(dev->status & VIRTIO_DEVICE_STATUS_DRIVER_OK))
1534 dev->flags |= VIRTIO_DEV_READY;
1536 if (!(dev->flags & VIRTIO_DEV_RUNNING))
1537 VHOST_LOG_CONFIG(INFO, "(%s) virtio is now ready for processing.\n", dev->ifname);
1542 inflight_mem_alloc(struct virtio_net *dev, const char *name, size_t size, int *fd)
1546 char fname[20] = "/tmp/memfd-XXXXXX";
1549 #ifdef MEMFD_SUPPORTED
1550 mfd = memfd_create(name, MFD_CLOEXEC);
1555 mfd = mkstemp(fname);
1557 VHOST_LOG_CONFIG(ERR, "(%s) failed to get inflight buffer fd\n",
1565 if (ftruncate(mfd, size) == -1) {
1566 VHOST_LOG_CONFIG(ERR, "(%s) failed to alloc inflight buffer\n", dev->ifname);
1571 ptr = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, mfd, 0);
1572 if (ptr == MAP_FAILED) {
1573 VHOST_LOG_CONFIG(ERR, "(%s) failed to mmap inflight buffer\n", dev->ifname);
1583 get_pervq_shm_size_split(uint16_t queue_size)
1585 return RTE_ALIGN_MUL_CEIL(sizeof(struct rte_vhost_inflight_desc_split) *
1586 queue_size + sizeof(uint64_t) +
1587 sizeof(uint16_t) * 4, INFLIGHT_ALIGNMENT);
1591 get_pervq_shm_size_packed(uint16_t queue_size)
1593 return RTE_ALIGN_MUL_CEIL(sizeof(struct rte_vhost_inflight_desc_packed)
1594 * queue_size + sizeof(uint64_t) +
1595 sizeof(uint16_t) * 6 + sizeof(uint8_t) * 9,
1596 INFLIGHT_ALIGNMENT);
1600 vhost_user_get_inflight_fd(struct virtio_net **pdev,
1601 struct vhu_msg_context *ctx,
1602 int main_fd __rte_unused)
1604 struct rte_vhost_inflight_info_packed *inflight_packed;
1605 uint64_t pervq_inflight_size, mmap_size;
1606 uint16_t num_queues, queue_size;
1607 struct virtio_net *dev = *pdev;
1609 int numa_node = SOCKET_ID_ANY;
1612 if (ctx->msg.size != sizeof(ctx->msg.payload.inflight)) {
1613 VHOST_LOG_CONFIG(ERR, "(%s) invalid get_inflight_fd message size is %d\n",
1614 dev->ifname, ctx->msg.size);
1615 return RTE_VHOST_MSG_RESULT_ERR;
1619 * If VQ 0 has already been allocated, try to allocate on the same
1620 * NUMA node. It can be reallocated later in numa_realloc().
1622 if (dev->nr_vring > 0)
1623 numa_node = dev->virtqueue[0]->numa_node;
1625 if (dev->inflight_info == NULL) {
1626 dev->inflight_info = rte_zmalloc_socket("inflight_info",
1627 sizeof(struct inflight_mem_info), 0, numa_node);
1628 if (!dev->inflight_info) {
1629 VHOST_LOG_CONFIG(ERR, "(%s) failed to alloc dev inflight area\n",
1631 return RTE_VHOST_MSG_RESULT_ERR;
1633 dev->inflight_info->fd = -1;
1636 num_queues = ctx->msg.payload.inflight.num_queues;
1637 queue_size = ctx->msg.payload.inflight.queue_size;
1639 VHOST_LOG_CONFIG(INFO, "(%s) get_inflight_fd num_queues: %u\n",
1640 dev->ifname, ctx->msg.payload.inflight.num_queues);
1641 VHOST_LOG_CONFIG(INFO, "(%s) get_inflight_fd queue_size: %u\n",
1642 dev->ifname, ctx->msg.payload.inflight.queue_size);
1644 if (vq_is_packed(dev))
1645 pervq_inflight_size = get_pervq_shm_size_packed(queue_size);
1647 pervq_inflight_size = get_pervq_shm_size_split(queue_size);
1649 mmap_size = num_queues * pervq_inflight_size;
1650 addr = inflight_mem_alloc(dev, "vhost-inflight", mmap_size, &fd);
1652 VHOST_LOG_CONFIG(ERR, "(%s) failed to alloc vhost inflight area\n", dev->ifname);
1653 ctx->msg.payload.inflight.mmap_size = 0;
1654 return RTE_VHOST_MSG_RESULT_ERR;
1656 memset(addr, 0, mmap_size);
1658 if (dev->inflight_info->addr) {
1659 munmap(dev->inflight_info->addr, dev->inflight_info->size);
1660 dev->inflight_info->addr = NULL;
1663 if (dev->inflight_info->fd >= 0) {
1664 close(dev->inflight_info->fd);
1665 dev->inflight_info->fd = -1;
1668 dev->inflight_info->addr = addr;
1669 dev->inflight_info->size = ctx->msg.payload.inflight.mmap_size = mmap_size;
1670 dev->inflight_info->fd = ctx->fds[0] = fd;
1671 ctx->msg.payload.inflight.mmap_offset = 0;
1674 if (vq_is_packed(dev)) {
1675 for (i = 0; i < num_queues; i++) {
1677 (struct rte_vhost_inflight_info_packed *)addr;
1678 inflight_packed->used_wrap_counter = 1;
1679 inflight_packed->old_used_wrap_counter = 1;
1680 for (j = 0; j < queue_size; j++)
1681 inflight_packed->desc[j].next = j + 1;
1682 addr = (void *)((char *)addr + pervq_inflight_size);
1686 VHOST_LOG_CONFIG(INFO, "(%s) send inflight mmap_size: %"PRIu64"\n",
1687 dev->ifname, ctx->msg.payload.inflight.mmap_size);
1688 VHOST_LOG_CONFIG(INFO, "(%s) send inflight mmap_offset: %"PRIu64"\n",
1689 dev->ifname, ctx->msg.payload.inflight.mmap_offset);
1690 VHOST_LOG_CONFIG(INFO, "(%s) send inflight fd: %d\n", dev->ifname, ctx->fds[0]);
1692 return RTE_VHOST_MSG_RESULT_REPLY;
1696 vhost_user_set_inflight_fd(struct virtio_net **pdev,
1697 struct vhu_msg_context *ctx,
1698 int main_fd __rte_unused)
1700 uint64_t mmap_size, mmap_offset;
1701 uint16_t num_queues, queue_size;
1702 struct virtio_net *dev = *pdev;
1703 uint32_t pervq_inflight_size;
1704 struct vhost_virtqueue *vq;
1707 int numa_node = SOCKET_ID_ANY;
1710 if (ctx->msg.size != sizeof(ctx->msg.payload.inflight) || fd < 0) {
1711 VHOST_LOG_CONFIG(ERR, "(%s) invalid set_inflight_fd message size is %d,fd is %d\n",
1712 dev->ifname, ctx->msg.size, fd);
1713 return RTE_VHOST_MSG_RESULT_ERR;
1716 mmap_size = ctx->msg.payload.inflight.mmap_size;
1717 mmap_offset = ctx->msg.payload.inflight.mmap_offset;
1718 num_queues = ctx->msg.payload.inflight.num_queues;
1719 queue_size = ctx->msg.payload.inflight.queue_size;
1721 if (vq_is_packed(dev))
1722 pervq_inflight_size = get_pervq_shm_size_packed(queue_size);
1724 pervq_inflight_size = get_pervq_shm_size_split(queue_size);
1726 VHOST_LOG_CONFIG(INFO, "(%s) set_inflight_fd mmap_size: %"PRIu64"\n",
1727 dev->ifname, mmap_size);
1728 VHOST_LOG_CONFIG(INFO, "(%s) set_inflight_fd mmap_offset: %"PRIu64"\n",
1729 dev->ifname, mmap_offset);
1730 VHOST_LOG_CONFIG(INFO, "(%s) set_inflight_fd num_queues: %u\n", dev->ifname, num_queues);
1731 VHOST_LOG_CONFIG(INFO, "(%s) set_inflight_fd queue_size: %u\n", dev->ifname, queue_size);
1732 VHOST_LOG_CONFIG(INFO, "(%s) set_inflight_fd fd: %d\n", dev->ifname, fd);
1733 VHOST_LOG_CONFIG(INFO, "(%s) set_inflight_fd pervq_inflight_size: %d\n",
1734 dev->ifname, pervq_inflight_size);
1737 * If VQ 0 has already been allocated, try to allocate on the same
1738 * NUMA node. It can be reallocated later in numa_realloc().
1740 if (dev->nr_vring > 0)
1741 numa_node = dev->virtqueue[0]->numa_node;
1743 if (!dev->inflight_info) {
1744 dev->inflight_info = rte_zmalloc_socket("inflight_info",
1745 sizeof(struct inflight_mem_info), 0, numa_node);
1746 if (dev->inflight_info == NULL) {
1747 VHOST_LOG_CONFIG(ERR, "(%s) failed to alloc dev inflight area\n",
1749 return RTE_VHOST_MSG_RESULT_ERR;
1751 dev->inflight_info->fd = -1;
1754 if (dev->inflight_info->addr) {
1755 munmap(dev->inflight_info->addr, dev->inflight_info->size);
1756 dev->inflight_info->addr = NULL;
1759 addr = mmap(0, mmap_size, PROT_READ | PROT_WRITE, MAP_SHARED,
1761 if (addr == MAP_FAILED) {
1762 VHOST_LOG_CONFIG(ERR, "(%s) failed to mmap share memory.\n", dev->ifname);
1763 return RTE_VHOST_MSG_RESULT_ERR;
1766 if (dev->inflight_info->fd >= 0) {
1767 close(dev->inflight_info->fd);
1768 dev->inflight_info->fd = -1;
1771 dev->inflight_info->fd = fd;
1772 dev->inflight_info->addr = addr;
1773 dev->inflight_info->size = mmap_size;
1775 for (i = 0; i < num_queues; i++) {
1776 vq = dev->virtqueue[i];
1780 if (vq_is_packed(dev)) {
1781 vq->inflight_packed = addr;
1782 vq->inflight_packed->desc_num = queue_size;
1784 vq->inflight_split = addr;
1785 vq->inflight_split->desc_num = queue_size;
1787 addr = (void *)((char *)addr + pervq_inflight_size);
1790 return RTE_VHOST_MSG_RESULT_OK;
1794 vhost_user_set_vring_call(struct virtio_net **pdev,
1795 struct vhu_msg_context *ctx,
1796 int main_fd __rte_unused)
1798 struct virtio_net *dev = *pdev;
1799 struct vhost_vring_file file;
1800 struct vhost_virtqueue *vq;
1803 expected_fds = (ctx->msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK) ? 0 : 1;
1804 if (validate_msg_fds(dev, ctx, expected_fds) != 0)
1805 return RTE_VHOST_MSG_RESULT_ERR;
1807 file.index = ctx->msg.payload.u64 & VHOST_USER_VRING_IDX_MASK;
1808 if (ctx->msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK)
1809 file.fd = VIRTIO_INVALID_EVENTFD;
1811 file.fd = ctx->fds[0];
1812 VHOST_LOG_CONFIG(INFO, "(%s) vring call idx:%d file:%d\n",
1813 dev->ifname, file.index, file.fd);
1815 vq = dev->virtqueue[file.index];
1819 vhost_user_notify_queue_state(dev, file.index, 0);
1822 if (vq->callfd >= 0)
1825 vq->callfd = file.fd;
1827 return RTE_VHOST_MSG_RESULT_OK;
1830 static int vhost_user_set_vring_err(struct virtio_net **pdev,
1831 struct vhu_msg_context *ctx,
1832 int main_fd __rte_unused)
1834 struct virtio_net *dev = *pdev;
1837 expected_fds = (ctx->msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK) ? 0 : 1;
1838 if (validate_msg_fds(dev, ctx, expected_fds) != 0)
1839 return RTE_VHOST_MSG_RESULT_ERR;
1841 if (!(ctx->msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK))
1843 VHOST_LOG_CONFIG(INFO, "(%s) not implemented\n", dev->ifname);
1845 return RTE_VHOST_MSG_RESULT_OK;
1849 resubmit_desc_compare(const void *a, const void *b)
1851 const struct rte_vhost_resubmit_desc *desc0 = a;
1852 const struct rte_vhost_resubmit_desc *desc1 = b;
1854 if (desc1->counter > desc0->counter)
1861 vhost_check_queue_inflights_split(struct virtio_net *dev,
1862 struct vhost_virtqueue *vq)
1865 uint16_t resubmit_num = 0, last_io, num;
1866 struct vring_used *used = vq->used;
1867 struct rte_vhost_resubmit_info *resubmit;
1868 struct rte_vhost_inflight_info_split *inflight_split;
1870 if (!(dev->protocol_features &
1871 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD)))
1872 return RTE_VHOST_MSG_RESULT_OK;
1874 /* The frontend may still not support the inflight feature
1875 * although we negotiate the protocol feature.
1877 if ((!vq->inflight_split))
1878 return RTE_VHOST_MSG_RESULT_OK;
1880 if (!vq->inflight_split->version) {
1881 vq->inflight_split->version = INFLIGHT_VERSION;
1882 return RTE_VHOST_MSG_RESULT_OK;
1885 if (vq->resubmit_inflight)
1886 return RTE_VHOST_MSG_RESULT_OK;
1888 inflight_split = vq->inflight_split;
1889 vq->global_counter = 0;
1890 last_io = inflight_split->last_inflight_io;
1892 if (inflight_split->used_idx != used->idx) {
1893 inflight_split->desc[last_io].inflight = 0;
1894 rte_atomic_thread_fence(__ATOMIC_SEQ_CST);
1895 inflight_split->used_idx = used->idx;
1898 for (i = 0; i < inflight_split->desc_num; i++) {
1899 if (inflight_split->desc[i].inflight == 1)
1903 vq->last_avail_idx += resubmit_num;
1906 resubmit = rte_zmalloc_socket("resubmit", sizeof(struct rte_vhost_resubmit_info),
1909 VHOST_LOG_CONFIG(ERR,
1910 "(%s) failed to allocate memory for resubmit info.\n",
1912 return RTE_VHOST_MSG_RESULT_ERR;
1915 resubmit->resubmit_list = rte_zmalloc_socket("resubmit_list",
1916 resubmit_num * sizeof(struct rte_vhost_resubmit_desc),
1918 if (!resubmit->resubmit_list) {
1919 VHOST_LOG_CONFIG(ERR,
1920 "(%s) failed to allocate memory for inflight desc.\n",
1923 return RTE_VHOST_MSG_RESULT_ERR;
1927 for (i = 0; i < vq->inflight_split->desc_num; i++) {
1928 if (vq->inflight_split->desc[i].inflight == 1) {
1929 resubmit->resubmit_list[num].index = i;
1930 resubmit->resubmit_list[num].counter =
1931 inflight_split->desc[i].counter;
1935 resubmit->resubmit_num = num;
1937 if (resubmit->resubmit_num > 1)
1938 qsort(resubmit->resubmit_list, resubmit->resubmit_num,
1939 sizeof(struct rte_vhost_resubmit_desc),
1940 resubmit_desc_compare);
1942 vq->global_counter = resubmit->resubmit_list[0].counter + 1;
1943 vq->resubmit_inflight = resubmit;
1946 return RTE_VHOST_MSG_RESULT_OK;
1950 vhost_check_queue_inflights_packed(struct virtio_net *dev,
1951 struct vhost_virtqueue *vq)
1954 uint16_t resubmit_num = 0, old_used_idx, num;
1955 struct rte_vhost_resubmit_info *resubmit;
1956 struct rte_vhost_inflight_info_packed *inflight_packed;
1958 if (!(dev->protocol_features &
1959 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD)))
1960 return RTE_VHOST_MSG_RESULT_OK;
1962 /* The frontend may still not support the inflight feature
1963 * although we negotiate the protocol feature.
1965 if ((!vq->inflight_packed))
1966 return RTE_VHOST_MSG_RESULT_OK;
1968 if (!vq->inflight_packed->version) {
1969 vq->inflight_packed->version = INFLIGHT_VERSION;
1970 return RTE_VHOST_MSG_RESULT_OK;
1973 if (vq->resubmit_inflight)
1974 return RTE_VHOST_MSG_RESULT_OK;
1976 inflight_packed = vq->inflight_packed;
1977 vq->global_counter = 0;
1978 old_used_idx = inflight_packed->old_used_idx;
1980 if (inflight_packed->used_idx != old_used_idx) {
1981 if (inflight_packed->desc[old_used_idx].inflight == 0) {
1982 inflight_packed->old_used_idx =
1983 inflight_packed->used_idx;
1984 inflight_packed->old_used_wrap_counter =
1985 inflight_packed->used_wrap_counter;
1986 inflight_packed->old_free_head =
1987 inflight_packed->free_head;
1989 inflight_packed->used_idx =
1990 inflight_packed->old_used_idx;
1991 inflight_packed->used_wrap_counter =
1992 inflight_packed->old_used_wrap_counter;
1993 inflight_packed->free_head =
1994 inflight_packed->old_free_head;
1998 for (i = 0; i < inflight_packed->desc_num; i++) {
1999 if (inflight_packed->desc[i].inflight == 1)
2004 resubmit = rte_zmalloc_socket("resubmit", sizeof(struct rte_vhost_resubmit_info),
2006 if (resubmit == NULL) {
2007 VHOST_LOG_CONFIG(ERR,
2008 "(%s) failed to allocate memory for resubmit info.\n",
2010 return RTE_VHOST_MSG_RESULT_ERR;
2013 resubmit->resubmit_list = rte_zmalloc_socket("resubmit_list",
2014 resubmit_num * sizeof(struct rte_vhost_resubmit_desc),
2016 if (resubmit->resubmit_list == NULL) {
2017 VHOST_LOG_CONFIG(ERR,
2018 "(%s) failed to allocate memory for resubmit desc.\n",
2021 return RTE_VHOST_MSG_RESULT_ERR;
2025 for (i = 0; i < inflight_packed->desc_num; i++) {
2026 if (vq->inflight_packed->desc[i].inflight == 1) {
2027 resubmit->resubmit_list[num].index = i;
2028 resubmit->resubmit_list[num].counter =
2029 inflight_packed->desc[i].counter;
2033 resubmit->resubmit_num = num;
2035 if (resubmit->resubmit_num > 1)
2036 qsort(resubmit->resubmit_list, resubmit->resubmit_num,
2037 sizeof(struct rte_vhost_resubmit_desc),
2038 resubmit_desc_compare);
2040 vq->global_counter = resubmit->resubmit_list[0].counter + 1;
2041 vq->resubmit_inflight = resubmit;
2044 return RTE_VHOST_MSG_RESULT_OK;
2048 vhost_user_set_vring_kick(struct virtio_net **pdev,
2049 struct vhu_msg_context *ctx,
2050 int main_fd __rte_unused)
2052 struct virtio_net *dev = *pdev;
2053 struct vhost_vring_file file;
2054 struct vhost_virtqueue *vq;
2057 expected_fds = (ctx->msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK) ? 0 : 1;
2058 if (validate_msg_fds(dev, ctx, expected_fds) != 0)
2059 return RTE_VHOST_MSG_RESULT_ERR;
2061 file.index = ctx->msg.payload.u64 & VHOST_USER_VRING_IDX_MASK;
2062 if (ctx->msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK)
2063 file.fd = VIRTIO_INVALID_EVENTFD;
2065 file.fd = ctx->fds[0];
2066 VHOST_LOG_CONFIG(INFO, "(%s) vring kick idx:%d file:%d\n",
2067 dev->ifname, file.index, file.fd);
2069 /* Interpret ring addresses only when ring is started. */
2070 dev = translate_ring_addresses(dev, file.index);
2072 if (file.fd != VIRTIO_INVALID_EVENTFD)
2075 return RTE_VHOST_MSG_RESULT_ERR;
2080 vq = dev->virtqueue[file.index];
2083 * When VHOST_USER_F_PROTOCOL_FEATURES is not negotiated,
2084 * the ring starts already enabled. Otherwise, it is enabled via
2085 * the SET_VRING_ENABLE message.
2087 if (!(dev->features & (1ULL << VHOST_USER_F_PROTOCOL_FEATURES))) {
2093 vhost_user_notify_queue_state(dev, file.index, 0);
2096 if (vq->kickfd >= 0)
2098 vq->kickfd = file.fd;
2100 if (vq_is_packed(dev)) {
2101 if (vhost_check_queue_inflights_packed(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;
2107 if (vhost_check_queue_inflights_split(dev, vq)) {
2108 VHOST_LOG_CONFIG(ERR, "(%s) failed to inflights for vq: %d\n",
2109 dev->ifname, file.index);
2110 return RTE_VHOST_MSG_RESULT_ERR;
2114 return RTE_VHOST_MSG_RESULT_OK;
2118 * when virtio is stopped, qemu will send us the GET_VRING_BASE message.
2121 vhost_user_get_vring_base(struct virtio_net **pdev,
2122 struct vhu_msg_context *ctx,
2123 int main_fd __rte_unused)
2125 struct virtio_net *dev = *pdev;
2126 struct vhost_virtqueue *vq = dev->virtqueue[ctx->msg.payload.state.index];
2129 if (validate_msg_fds(dev, ctx, 0) != 0)
2130 return RTE_VHOST_MSG_RESULT_ERR;
2132 /* We have to stop the queue (virtio) if it is running. */
2133 vhost_destroy_device_notify(dev);
2135 dev->flags &= ~VIRTIO_DEV_READY;
2136 dev->flags &= ~VIRTIO_DEV_VDPA_CONFIGURED;
2138 /* Here we are safe to get the indexes */
2139 if (vq_is_packed(dev)) {
2141 * Bit[0:14]: avail index
2142 * Bit[15]: avail wrap counter
2144 val = vq->last_avail_idx & 0x7fff;
2145 val |= vq->avail_wrap_counter << 15;
2146 ctx->msg.payload.state.num = val;
2148 ctx->msg.payload.state.num = vq->last_avail_idx;
2151 VHOST_LOG_CONFIG(INFO, "(%s) vring base idx:%d file:%d\n",
2152 dev->ifname, ctx->msg.payload.state.index,
2153 ctx->msg.payload.state.num);
2155 * Based on current qemu vhost-user implementation, this message is
2156 * sent and only sent in vhost_vring_stop.
2157 * TODO: cleanup the vring, it isn't usable since here.
2159 if (vq->kickfd >= 0)
2162 vq->kickfd = VIRTIO_UNINITIALIZED_EVENTFD;
2164 if (vq->callfd >= 0)
2167 vq->callfd = VIRTIO_UNINITIALIZED_EVENTFD;
2169 vq->signalled_used_valid = false;
2171 if (vq_is_packed(dev)) {
2172 rte_free(vq->shadow_used_packed);
2173 vq->shadow_used_packed = NULL;
2175 rte_free(vq->shadow_used_split);
2176 vq->shadow_used_split = NULL;
2179 rte_free(vq->batch_copy_elems);
2180 vq->batch_copy_elems = NULL;
2182 rte_free(vq->log_cache);
2183 vq->log_cache = NULL;
2185 ctx->msg.size = sizeof(ctx->msg.payload.state);
2188 vhost_user_iotlb_flush_all(vq);
2190 vring_invalidate(dev, vq);
2192 return RTE_VHOST_MSG_RESULT_REPLY;
2196 * when virtio queues are ready to work, qemu will send us to
2197 * enable the virtio queue pair.
2200 vhost_user_set_vring_enable(struct virtio_net **pdev,
2201 struct vhu_msg_context *ctx,
2202 int main_fd __rte_unused)
2204 struct virtio_net *dev = *pdev;
2205 bool enable = !!ctx->msg.payload.state.num;
2206 int index = (int)ctx->msg.payload.state.index;
2208 if (validate_msg_fds(dev, ctx, 0) != 0)
2209 return RTE_VHOST_MSG_RESULT_ERR;
2211 VHOST_LOG_CONFIG(INFO, "(%s) set queue enable: %d to qp idx: %d\n",
2212 dev->ifname, enable, index);
2214 if (enable && dev->virtqueue[index]->async) {
2215 if (dev->virtqueue[index]->async->pkts_inflight_n) {
2216 VHOST_LOG_CONFIG(ERR,
2217 "(%s) failed to enable vring. Inflight packets must be completed first\n",
2219 return RTE_VHOST_MSG_RESULT_ERR;
2223 dev->virtqueue[index]->enabled = enable;
2225 return RTE_VHOST_MSG_RESULT_OK;
2229 vhost_user_get_protocol_features(struct virtio_net **pdev,
2230 struct vhu_msg_context *ctx,
2231 int main_fd __rte_unused)
2233 struct virtio_net *dev = *pdev;
2234 uint64_t features, protocol_features;
2236 if (validate_msg_fds(dev, ctx, 0) != 0)
2237 return RTE_VHOST_MSG_RESULT_ERR;
2239 rte_vhost_driver_get_features(dev->ifname, &features);
2240 rte_vhost_driver_get_protocol_features(dev->ifname, &protocol_features);
2242 ctx->msg.payload.u64 = protocol_features;
2243 ctx->msg.size = sizeof(ctx->msg.payload.u64);
2246 return RTE_VHOST_MSG_RESULT_REPLY;
2250 vhost_user_set_protocol_features(struct virtio_net **pdev,
2251 struct vhu_msg_context *ctx,
2252 int main_fd __rte_unused)
2254 struct virtio_net *dev = *pdev;
2255 uint64_t protocol_features = ctx->msg.payload.u64;
2256 uint64_t slave_protocol_features = 0;
2258 if (validate_msg_fds(dev, ctx, 0) != 0)
2259 return RTE_VHOST_MSG_RESULT_ERR;
2261 rte_vhost_driver_get_protocol_features(dev->ifname,
2262 &slave_protocol_features);
2263 if (protocol_features & ~slave_protocol_features) {
2264 VHOST_LOG_CONFIG(ERR, "(%s) received invalid protocol features.\n", dev->ifname);
2265 return RTE_VHOST_MSG_RESULT_ERR;
2268 dev->protocol_features = protocol_features;
2269 VHOST_LOG_CONFIG(INFO, "(%s) negotiated Vhost-user protocol features: 0x%" PRIx64 "\n",
2270 dev->ifname, dev->protocol_features);
2272 return RTE_VHOST_MSG_RESULT_OK;
2276 vhost_user_set_log_base(struct virtio_net **pdev,
2277 struct vhu_msg_context *ctx,
2278 int main_fd __rte_unused)
2280 struct virtio_net *dev = *pdev;
2281 int fd = ctx->fds[0];
2286 if (validate_msg_fds(dev, ctx, 1) != 0)
2287 return RTE_VHOST_MSG_RESULT_ERR;
2290 VHOST_LOG_CONFIG(ERR, "(%s) invalid log fd: %d\n", dev->ifname, fd);
2291 return RTE_VHOST_MSG_RESULT_ERR;
2294 if (ctx->msg.size != sizeof(VhostUserLog)) {
2295 VHOST_LOG_CONFIG(ERR, "(%s) invalid log base msg size: %"PRId32" != %d\n",
2296 dev->ifname, ctx->msg.size, (int)sizeof(VhostUserLog));
2300 size = ctx->msg.payload.log.mmap_size;
2301 off = ctx->msg.payload.log.mmap_offset;
2303 /* Check for mmap size and offset overflow. */
2305 VHOST_LOG_CONFIG(ERR,
2306 "(%s) log offset %#"PRIx64" and log size %#"PRIx64" overflow\n",
2307 dev->ifname, off, size);
2311 VHOST_LOG_CONFIG(INFO, "(%s) log mmap size: %"PRId64", offset: %"PRId64"\n",
2312 dev->ifname, size, off);
2315 * mmap from 0 to workaround a hugepage mmap bug: mmap will
2316 * fail when offset is not page size aligned.
2318 addr = mmap(0, size + off, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
2320 if (addr == MAP_FAILED) {
2321 VHOST_LOG_CONFIG(ERR, "(%s) mmap log base failed!\n", dev->ifname);
2322 return RTE_VHOST_MSG_RESULT_ERR;
2326 * Free previously mapped log memory on occasionally
2327 * multiple VHOST_USER_SET_LOG_BASE.
2329 if (dev->log_addr) {
2330 munmap((void *)(uintptr_t)dev->log_addr, dev->log_size);
2332 dev->log_addr = (uint64_t)(uintptr_t)addr;
2333 dev->log_base = dev->log_addr + off;
2334 dev->log_size = size;
2336 for (i = 0; i < dev->nr_vring; i++) {
2337 struct vhost_virtqueue *vq = dev->virtqueue[i];
2339 rte_free(vq->log_cache);
2340 vq->log_cache = NULL;
2341 vq->log_cache_nb_elem = 0;
2342 vq->log_cache = rte_malloc_socket("vq log cache",
2343 sizeof(struct log_cache_entry) * VHOST_LOG_CACHE_NR,
2346 * If log cache alloc fail, don't fail migration, but no
2347 * caching will be done, which will impact performance
2350 VHOST_LOG_CONFIG(ERR, "(%s) failed to allocate VQ logging cache\n",
2355 * The spec is not clear about it (yet), but QEMU doesn't expect
2356 * any payload in the reply.
2361 return RTE_VHOST_MSG_RESULT_REPLY;
2365 return RTE_VHOST_MSG_RESULT_ERR;
2368 static int vhost_user_set_log_fd(struct virtio_net **pdev,
2369 struct vhu_msg_context *ctx,
2370 int main_fd __rte_unused)
2372 struct virtio_net *dev = *pdev;
2374 if (validate_msg_fds(dev, ctx, 1) != 0)
2375 return RTE_VHOST_MSG_RESULT_ERR;
2378 VHOST_LOG_CONFIG(INFO, "(%s) not implemented.\n", dev->ifname);
2380 return RTE_VHOST_MSG_RESULT_OK;
2384 * An rarp packet is constructed and broadcasted to notify switches about
2385 * the new location of the migrated VM, so that packets from outside will
2386 * not be lost after migration.
2388 * However, we don't actually "send" a rarp packet here, instead, we set
2389 * a flag 'broadcast_rarp' to let rte_vhost_dequeue_burst() inject it.
2392 vhost_user_send_rarp(struct virtio_net **pdev,
2393 struct vhu_msg_context *ctx,
2394 int main_fd __rte_unused)
2396 struct virtio_net *dev = *pdev;
2397 uint8_t *mac = (uint8_t *)&ctx->msg.payload.u64;
2398 struct rte_vdpa_device *vdpa_dev;
2400 if (validate_msg_fds(dev, ctx, 0) != 0)
2401 return RTE_VHOST_MSG_RESULT_ERR;
2403 VHOST_LOG_CONFIG(DEBUG, "(%s) MAC: " RTE_ETHER_ADDR_PRT_FMT "\n",
2404 dev->ifname, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
2405 memcpy(dev->mac.addr_bytes, mac, 6);
2408 * Set the flag to inject a RARP broadcast packet at
2409 * rte_vhost_dequeue_burst().
2411 * __ATOMIC_RELEASE ordering is for making sure the mac is
2412 * copied before the flag is set.
2414 __atomic_store_n(&dev->broadcast_rarp, 1, __ATOMIC_RELEASE);
2415 vdpa_dev = dev->vdpa_dev;
2416 if (vdpa_dev && vdpa_dev->ops->migration_done)
2417 vdpa_dev->ops->migration_done(dev->vid);
2419 return RTE_VHOST_MSG_RESULT_OK;
2423 vhost_user_net_set_mtu(struct virtio_net **pdev,
2424 struct vhu_msg_context *ctx,
2425 int main_fd __rte_unused)
2427 struct virtio_net *dev = *pdev;
2429 if (validate_msg_fds(dev, ctx, 0) != 0)
2430 return RTE_VHOST_MSG_RESULT_ERR;
2432 if (ctx->msg.payload.u64 < VIRTIO_MIN_MTU ||
2433 ctx->msg.payload.u64 > VIRTIO_MAX_MTU) {
2434 VHOST_LOG_CONFIG(ERR, "(%s) invalid MTU size (%"PRIu64")\n",
2435 dev->ifname, ctx->msg.payload.u64);
2437 return RTE_VHOST_MSG_RESULT_ERR;
2440 dev->mtu = ctx->msg.payload.u64;
2442 return RTE_VHOST_MSG_RESULT_OK;
2446 vhost_user_set_req_fd(struct virtio_net **pdev,
2447 struct vhu_msg_context *ctx,
2448 int main_fd __rte_unused)
2450 struct virtio_net *dev = *pdev;
2451 int fd = ctx->fds[0];
2453 if (validate_msg_fds(dev, ctx, 1) != 0)
2454 return RTE_VHOST_MSG_RESULT_ERR;
2457 VHOST_LOG_CONFIG(ERR, "(%s) invalid file descriptor for slave channel (%d)\n",
2459 return RTE_VHOST_MSG_RESULT_ERR;
2462 if (dev->slave_req_fd >= 0)
2463 close(dev->slave_req_fd);
2465 dev->slave_req_fd = fd;
2467 return RTE_VHOST_MSG_RESULT_OK;
2471 is_vring_iotlb_split(struct vhost_virtqueue *vq, struct vhost_iotlb_msg *imsg)
2473 struct vhost_vring_addr *ra;
2474 uint64_t start, end, len;
2477 end = start + imsg->size;
2479 ra = &vq->ring_addrs;
2480 len = sizeof(struct vring_desc) * vq->size;
2481 if (ra->desc_user_addr < end && (ra->desc_user_addr + len) > start)
2484 len = sizeof(struct vring_avail) + sizeof(uint16_t) * vq->size;
2485 if (ra->avail_user_addr < end && (ra->avail_user_addr + len) > start)
2488 len = sizeof(struct vring_used) +
2489 sizeof(struct vring_used_elem) * vq->size;
2490 if (ra->used_user_addr < end && (ra->used_user_addr + len) > start)
2493 if (ra->flags & (1 << VHOST_VRING_F_LOG)) {
2494 len = sizeof(uint64_t);
2495 if (ra->log_guest_addr < end &&
2496 (ra->log_guest_addr + len) > start)
2504 is_vring_iotlb_packed(struct vhost_virtqueue *vq, struct vhost_iotlb_msg *imsg)
2506 struct vhost_vring_addr *ra;
2507 uint64_t start, end, len;
2510 end = start + imsg->size;
2512 ra = &vq->ring_addrs;
2513 len = sizeof(struct vring_packed_desc) * vq->size;
2514 if (ra->desc_user_addr < end && (ra->desc_user_addr + len) > start)
2517 len = sizeof(struct vring_packed_desc_event);
2518 if (ra->avail_user_addr < end && (ra->avail_user_addr + len) > start)
2521 len = sizeof(struct vring_packed_desc_event);
2522 if (ra->used_user_addr < end && (ra->used_user_addr + len) > start)
2525 if (ra->flags & (1 << VHOST_VRING_F_LOG)) {
2526 len = sizeof(uint64_t);
2527 if (ra->log_guest_addr < end &&
2528 (ra->log_guest_addr + len) > start)
2535 static int is_vring_iotlb(struct virtio_net *dev,
2536 struct vhost_virtqueue *vq,
2537 struct vhost_iotlb_msg *imsg)
2539 if (vq_is_packed(dev))
2540 return is_vring_iotlb_packed(vq, imsg);
2542 return is_vring_iotlb_split(vq, imsg);
2546 vhost_user_iotlb_msg(struct virtio_net **pdev,
2547 struct vhu_msg_context *ctx,
2548 int main_fd __rte_unused)
2550 struct virtio_net *dev = *pdev;
2551 struct vhost_iotlb_msg *imsg = &ctx->msg.payload.iotlb;
2555 if (validate_msg_fds(dev, ctx, 0) != 0)
2556 return RTE_VHOST_MSG_RESULT_ERR;
2558 switch (imsg->type) {
2559 case VHOST_IOTLB_UPDATE:
2561 vva = qva_to_vva(dev, imsg->uaddr, &len);
2563 return RTE_VHOST_MSG_RESULT_ERR;
2565 for (i = 0; i < dev->nr_vring; i++) {
2566 struct vhost_virtqueue *vq = dev->virtqueue[i];
2571 vhost_user_iotlb_cache_insert(dev, vq, imsg->iova, vva,
2574 if (is_vring_iotlb(dev, vq, imsg))
2575 *pdev = dev = translate_ring_addresses(dev, i);
2578 case VHOST_IOTLB_INVALIDATE:
2579 for (i = 0; i < dev->nr_vring; i++) {
2580 struct vhost_virtqueue *vq = dev->virtqueue[i];
2585 vhost_user_iotlb_cache_remove(vq, imsg->iova,
2588 if (is_vring_iotlb(dev, vq, imsg))
2589 vring_invalidate(dev, vq);
2593 VHOST_LOG_CONFIG(ERR, "(%s) invalid IOTLB message type (%d)\n",
2594 dev->ifname, imsg->type);
2595 return RTE_VHOST_MSG_RESULT_ERR;
2598 return RTE_VHOST_MSG_RESULT_OK;
2602 vhost_user_set_postcopy_advise(struct virtio_net **pdev,
2603 struct vhu_msg_context *ctx,
2604 int main_fd __rte_unused)
2606 struct virtio_net *dev = *pdev;
2607 #ifdef RTE_LIBRTE_VHOST_POSTCOPY
2608 struct uffdio_api api_struct;
2610 if (validate_msg_fds(dev, ctx, 0) != 0)
2611 return RTE_VHOST_MSG_RESULT_ERR;
2613 dev->postcopy_ufd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK);
2615 if (dev->postcopy_ufd == -1) {
2616 VHOST_LOG_CONFIG(ERR, "(%s) userfaultfd not available: %s\n",
2617 dev->ifname, strerror(errno));
2618 return RTE_VHOST_MSG_RESULT_ERR;
2620 api_struct.api = UFFD_API;
2621 api_struct.features = 0;
2622 if (ioctl(dev->postcopy_ufd, UFFDIO_API, &api_struct)) {
2623 VHOST_LOG_CONFIG(ERR, "(%s) UFFDIO_API ioctl failure: %s\n",
2624 dev->ifname, strerror(errno));
2625 close(dev->postcopy_ufd);
2626 dev->postcopy_ufd = -1;
2627 return RTE_VHOST_MSG_RESULT_ERR;
2629 ctx->fds[0] = dev->postcopy_ufd;
2632 return RTE_VHOST_MSG_RESULT_REPLY;
2634 dev->postcopy_ufd = -1;
2637 return RTE_VHOST_MSG_RESULT_ERR;
2642 vhost_user_set_postcopy_listen(struct virtio_net **pdev,
2643 struct vhu_msg_context *ctx __rte_unused,
2644 int main_fd __rte_unused)
2646 struct virtio_net *dev = *pdev;
2648 if (validate_msg_fds(dev, ctx, 0) != 0)
2649 return RTE_VHOST_MSG_RESULT_ERR;
2651 if (dev->mem && dev->mem->nregions) {
2652 VHOST_LOG_CONFIG(ERR, "(%s) regions already registered at postcopy-listen\n",
2654 return RTE_VHOST_MSG_RESULT_ERR;
2656 dev->postcopy_listening = 1;
2658 return RTE_VHOST_MSG_RESULT_OK;
2662 vhost_user_postcopy_end(struct virtio_net **pdev,
2663 struct vhu_msg_context *ctx,
2664 int main_fd __rte_unused)
2666 struct virtio_net *dev = *pdev;
2668 if (validate_msg_fds(dev, ctx, 0) != 0)
2669 return RTE_VHOST_MSG_RESULT_ERR;
2671 dev->postcopy_listening = 0;
2672 if (dev->postcopy_ufd >= 0) {
2673 close(dev->postcopy_ufd);
2674 dev->postcopy_ufd = -1;
2677 ctx->msg.payload.u64 = 0;
2678 ctx->msg.size = sizeof(ctx->msg.payload.u64);
2681 return RTE_VHOST_MSG_RESULT_REPLY;
2685 vhost_user_get_status(struct virtio_net **pdev,
2686 struct vhu_msg_context *ctx,
2687 int main_fd __rte_unused)
2689 struct virtio_net *dev = *pdev;
2691 if (validate_msg_fds(dev, ctx, 0) != 0)
2692 return RTE_VHOST_MSG_RESULT_ERR;
2694 ctx->msg.payload.u64 = dev->status;
2695 ctx->msg.size = sizeof(ctx->msg.payload.u64);
2698 return RTE_VHOST_MSG_RESULT_REPLY;
2702 vhost_user_set_status(struct virtio_net **pdev,
2703 struct vhu_msg_context *ctx,
2704 int main_fd __rte_unused)
2706 struct virtio_net *dev = *pdev;
2708 if (validate_msg_fds(dev, ctx, 0) != 0)
2709 return RTE_VHOST_MSG_RESULT_ERR;
2711 /* As per Virtio specification, the device status is 8bits long */
2712 if (ctx->msg.payload.u64 > UINT8_MAX) {
2713 VHOST_LOG_CONFIG(ERR, "(%s) invalid VHOST_USER_SET_STATUS payload 0x%" PRIx64 "\n",
2714 dev->ifname, ctx->msg.payload.u64);
2715 return RTE_VHOST_MSG_RESULT_ERR;
2718 dev->status = ctx->msg.payload.u64;
2720 if ((dev->status & VIRTIO_DEVICE_STATUS_FEATURES_OK) &&
2721 (dev->flags & VIRTIO_DEV_FEATURES_FAILED)) {
2722 VHOST_LOG_CONFIG(ERR,
2723 "(%s) FEATURES_OK bit is set but feature negotiation failed\n",
2726 * Clear the bit to let the driver know about the feature
2727 * negotiation failure
2729 dev->status &= ~VIRTIO_DEVICE_STATUS_FEATURES_OK;
2732 VHOST_LOG_CONFIG(INFO, "(%s) new device status(0x%08x):\n", dev->ifname,
2734 VHOST_LOG_CONFIG(INFO, "(%s)\t-RESET: %u\n", dev->ifname,
2735 (dev->status == VIRTIO_DEVICE_STATUS_RESET));
2736 VHOST_LOG_CONFIG(INFO, "(%s)\t-ACKNOWLEDGE: %u\n", dev->ifname,
2737 !!(dev->status & VIRTIO_DEVICE_STATUS_ACK));
2738 VHOST_LOG_CONFIG(INFO, "(%s)\t-DRIVER: %u\n", dev->ifname,
2739 !!(dev->status & VIRTIO_DEVICE_STATUS_DRIVER));
2740 VHOST_LOG_CONFIG(INFO, "(%s)\t-FEATURES_OK: %u\n", dev->ifname,
2741 !!(dev->status & VIRTIO_DEVICE_STATUS_FEATURES_OK));
2742 VHOST_LOG_CONFIG(INFO, "(%s)\t-DRIVER_OK: %u\n", dev->ifname,
2743 !!(dev->status & VIRTIO_DEVICE_STATUS_DRIVER_OK));
2744 VHOST_LOG_CONFIG(INFO, "(%s)\t-DEVICE_NEED_RESET: %u\n", dev->ifname,
2745 !!(dev->status & VIRTIO_DEVICE_STATUS_DEV_NEED_RESET));
2746 VHOST_LOG_CONFIG(INFO, "(%s)\t-FAILED: %u\n", dev->ifname,
2747 !!(dev->status & VIRTIO_DEVICE_STATUS_FAILED));
2749 return RTE_VHOST_MSG_RESULT_OK;
2752 typedef int (*vhost_message_handler_t)(struct virtio_net **pdev,
2753 struct vhu_msg_context *ctx,
2756 static vhost_message_handler_t vhost_message_handlers[VHOST_USER_MAX] = {
2757 [VHOST_USER_NONE] = NULL,
2758 [VHOST_USER_GET_FEATURES] = vhost_user_get_features,
2759 [VHOST_USER_SET_FEATURES] = vhost_user_set_features,
2760 [VHOST_USER_SET_OWNER] = vhost_user_set_owner,
2761 [VHOST_USER_RESET_OWNER] = vhost_user_reset_owner,
2762 [VHOST_USER_SET_MEM_TABLE] = vhost_user_set_mem_table,
2763 [VHOST_USER_SET_LOG_BASE] = vhost_user_set_log_base,
2764 [VHOST_USER_SET_LOG_FD] = vhost_user_set_log_fd,
2765 [VHOST_USER_SET_VRING_NUM] = vhost_user_set_vring_num,
2766 [VHOST_USER_SET_VRING_ADDR] = vhost_user_set_vring_addr,
2767 [VHOST_USER_SET_VRING_BASE] = vhost_user_set_vring_base,
2768 [VHOST_USER_GET_VRING_BASE] = vhost_user_get_vring_base,
2769 [VHOST_USER_SET_VRING_KICK] = vhost_user_set_vring_kick,
2770 [VHOST_USER_SET_VRING_CALL] = vhost_user_set_vring_call,
2771 [VHOST_USER_SET_VRING_ERR] = vhost_user_set_vring_err,
2772 [VHOST_USER_GET_PROTOCOL_FEATURES] = vhost_user_get_protocol_features,
2773 [VHOST_USER_SET_PROTOCOL_FEATURES] = vhost_user_set_protocol_features,
2774 [VHOST_USER_GET_QUEUE_NUM] = vhost_user_get_queue_num,
2775 [VHOST_USER_SET_VRING_ENABLE] = vhost_user_set_vring_enable,
2776 [VHOST_USER_SEND_RARP] = vhost_user_send_rarp,
2777 [VHOST_USER_NET_SET_MTU] = vhost_user_net_set_mtu,
2778 [VHOST_USER_SET_SLAVE_REQ_FD] = vhost_user_set_req_fd,
2779 [VHOST_USER_IOTLB_MSG] = vhost_user_iotlb_msg,
2780 [VHOST_USER_POSTCOPY_ADVISE] = vhost_user_set_postcopy_advise,
2781 [VHOST_USER_POSTCOPY_LISTEN] = vhost_user_set_postcopy_listen,
2782 [VHOST_USER_POSTCOPY_END] = vhost_user_postcopy_end,
2783 [VHOST_USER_GET_INFLIGHT_FD] = vhost_user_get_inflight_fd,
2784 [VHOST_USER_SET_INFLIGHT_FD] = vhost_user_set_inflight_fd,
2785 [VHOST_USER_SET_STATUS] = vhost_user_set_status,
2786 [VHOST_USER_GET_STATUS] = vhost_user_get_status,
2789 /* return bytes# of read on success or negative val on failure. */
2791 read_vhost_message(struct virtio_net *dev, int sockfd, struct vhu_msg_context *ctx)
2795 ret = read_fd_message(dev->ifname, sockfd, (char *)&ctx->msg, VHOST_USER_HDR_SIZE,
2796 ctx->fds, VHOST_MEMORY_MAX_NREGIONS, &ctx->fd_num);
2799 } else if (ret != VHOST_USER_HDR_SIZE) {
2800 VHOST_LOG_CONFIG(ERR, "(%s) Unexpected header size read\n", dev->ifname);
2805 if (ctx->msg.size) {
2806 if (ctx->msg.size > sizeof(ctx->msg.payload)) {
2807 VHOST_LOG_CONFIG(ERR, "(%s) invalid msg size: %d\n",
2808 dev->ifname, ctx->msg.size);
2811 ret = read(sockfd, &ctx->msg.payload, ctx->msg.size);
2814 if (ret != (int)ctx->msg.size) {
2815 VHOST_LOG_CONFIG(ERR, "(%s) read control message failed\n", dev->ifname);
2824 send_vhost_message(struct virtio_net *dev, int sockfd, struct vhu_msg_context *ctx)
2829 return send_fd_message(dev->ifname, sockfd, (char *)&ctx->msg,
2830 VHOST_USER_HDR_SIZE + ctx->msg.size, ctx->fds, ctx->fd_num);
2834 send_vhost_reply(struct virtio_net *dev, int sockfd, struct vhu_msg_context *ctx)
2839 ctx->msg.flags &= ~VHOST_USER_VERSION_MASK;
2840 ctx->msg.flags &= ~VHOST_USER_NEED_REPLY;
2841 ctx->msg.flags |= VHOST_USER_VERSION;
2842 ctx->msg.flags |= VHOST_USER_REPLY_MASK;
2844 return send_vhost_message(dev, sockfd, ctx);
2848 send_vhost_slave_message(struct virtio_net *dev,
2849 struct vhu_msg_context *ctx)
2853 if (ctx->msg.flags & VHOST_USER_NEED_REPLY)
2854 rte_spinlock_lock(&dev->slave_req_lock);
2856 ret = send_vhost_message(dev, dev->slave_req_fd, ctx);
2857 if (ret < 0 && (ctx->msg.flags & VHOST_USER_NEED_REPLY))
2858 rte_spinlock_unlock(&dev->slave_req_lock);
2864 * Allocate a queue pair if it hasn't been allocated yet
2867 vhost_user_check_and_alloc_queue_pair(struct virtio_net *dev,
2868 struct vhu_msg_context *ctx)
2872 switch (ctx->msg.request.master) {
2873 case VHOST_USER_SET_VRING_KICK:
2874 case VHOST_USER_SET_VRING_CALL:
2875 case VHOST_USER_SET_VRING_ERR:
2876 vring_idx = ctx->msg.payload.u64 & VHOST_USER_VRING_IDX_MASK;
2878 case VHOST_USER_SET_VRING_NUM:
2879 case VHOST_USER_SET_VRING_BASE:
2880 case VHOST_USER_GET_VRING_BASE:
2881 case VHOST_USER_SET_VRING_ENABLE:
2882 vring_idx = ctx->msg.payload.state.index;
2884 case VHOST_USER_SET_VRING_ADDR:
2885 vring_idx = ctx->msg.payload.addr.index;
2891 if (vring_idx >= VHOST_MAX_VRING) {
2892 VHOST_LOG_CONFIG(ERR, "(%s) invalid vring index: %u\n", dev->ifname, vring_idx);
2896 if (dev->virtqueue[vring_idx])
2899 return alloc_vring_queue(dev, vring_idx);
2903 vhost_user_lock_all_queue_pairs(struct virtio_net *dev)
2906 unsigned int vq_num = 0;
2908 while (vq_num < dev->nr_vring) {
2909 struct vhost_virtqueue *vq = dev->virtqueue[i];
2912 rte_spinlock_lock(&vq->access_lock);
2920 vhost_user_unlock_all_queue_pairs(struct virtio_net *dev)
2923 unsigned int vq_num = 0;
2925 while (vq_num < dev->nr_vring) {
2926 struct vhost_virtqueue *vq = dev->virtqueue[i];
2929 rte_spinlock_unlock(&vq->access_lock);
2937 vhost_user_msg_handler(int vid, int fd)
2939 struct virtio_net *dev;
2940 struct vhu_msg_context ctx;
2941 struct rte_vdpa_device *vdpa_dev;
2943 int unlock_required = 0;
2948 dev = get_device(vid);
2952 if (!dev->notify_ops) {
2953 dev->notify_ops = vhost_driver_callback_get(dev->ifname);
2954 if (!dev->notify_ops) {
2955 VHOST_LOG_CONFIG(ERR, "(%s) failed to get callback ops for driver\n",
2961 ret = read_vhost_message(dev, fd, &ctx);
2964 VHOST_LOG_CONFIG(ERR, "(%s) vhost read message failed\n", dev->ifname);
2966 VHOST_LOG_CONFIG(INFO, "(%s) vhost peer closed\n", dev->ifname);
2972 request = ctx.msg.request.master;
2973 if (request > VHOST_USER_NONE && request < VHOST_USER_MAX &&
2974 vhost_message_str[request]) {
2975 if (request != VHOST_USER_IOTLB_MSG)
2976 VHOST_LOG_CONFIG(INFO, "(%s) read message %s\n",
2977 dev->ifname, vhost_message_str[request]);
2979 VHOST_LOG_CONFIG(DEBUG, "(%s) read message %s\n",
2980 dev->ifname, vhost_message_str[request]);
2982 VHOST_LOG_CONFIG(DEBUG, "(%s) external request %d\n", dev->ifname, request);
2985 ret = vhost_user_check_and_alloc_queue_pair(dev, &ctx);
2987 VHOST_LOG_CONFIG(ERR, "(%s) failed to alloc queue\n", dev->ifname);
2992 * Note: we don't lock all queues on VHOST_USER_GET_VRING_BASE
2993 * and VHOST_USER_RESET_OWNER, since it is sent when virtio stops
2994 * and device is destroyed. destroy_device waits for queues to be
2995 * inactive, so it is safe. Otherwise taking the access_lock
2996 * would cause a dead lock.
2999 case VHOST_USER_SET_FEATURES:
3000 case VHOST_USER_SET_PROTOCOL_FEATURES:
3001 case VHOST_USER_SET_OWNER:
3002 case VHOST_USER_SET_MEM_TABLE:
3003 case VHOST_USER_SET_LOG_BASE:
3004 case VHOST_USER_SET_LOG_FD:
3005 case VHOST_USER_SET_VRING_NUM:
3006 case VHOST_USER_SET_VRING_ADDR:
3007 case VHOST_USER_SET_VRING_BASE:
3008 case VHOST_USER_SET_VRING_KICK:
3009 case VHOST_USER_SET_VRING_CALL:
3010 case VHOST_USER_SET_VRING_ERR:
3011 case VHOST_USER_SET_VRING_ENABLE:
3012 case VHOST_USER_SEND_RARP:
3013 case VHOST_USER_NET_SET_MTU:
3014 case VHOST_USER_SET_SLAVE_REQ_FD:
3015 if (!(dev->flags & VIRTIO_DEV_VDPA_CONFIGURED)) {
3016 vhost_user_lock_all_queue_pairs(dev);
3017 unlock_required = 1;
3026 if (dev->extern_ops.pre_msg_handle) {
3027 ret = (*dev->extern_ops.pre_msg_handle)(dev->vid,
3030 case RTE_VHOST_MSG_RESULT_REPLY:
3031 send_vhost_reply(dev, fd, &ctx);
3033 case RTE_VHOST_MSG_RESULT_ERR:
3034 case RTE_VHOST_MSG_RESULT_OK:
3036 goto skip_to_post_handle;
3037 case RTE_VHOST_MSG_RESULT_NOT_HANDLED:
3043 if (request > VHOST_USER_NONE && request < VHOST_USER_MAX) {
3044 if (!vhost_message_handlers[request])
3045 goto skip_to_post_handle;
3046 ret = vhost_message_handlers[request](&dev, &ctx, fd);
3049 case RTE_VHOST_MSG_RESULT_ERR:
3050 VHOST_LOG_CONFIG(ERR, "(%s) processing %s failed.\n",
3051 dev->ifname, vhost_message_str[request]);
3054 case RTE_VHOST_MSG_RESULT_OK:
3055 VHOST_LOG_CONFIG(DEBUG, "(%s) processing %s succeeded.\n",
3056 dev->ifname, vhost_message_str[request]);
3059 case RTE_VHOST_MSG_RESULT_REPLY:
3060 VHOST_LOG_CONFIG(DEBUG, "(%s) processing %s succeeded and needs reply.\n",
3061 dev->ifname, vhost_message_str[request]);
3062 send_vhost_reply(dev, fd, &ctx);
3070 skip_to_post_handle:
3071 if (ret != RTE_VHOST_MSG_RESULT_ERR &&
3072 dev->extern_ops.post_msg_handle) {
3073 ret = (*dev->extern_ops.post_msg_handle)(dev->vid,
3076 case RTE_VHOST_MSG_RESULT_REPLY:
3077 send_vhost_reply(dev, fd, &ctx);
3079 case RTE_VHOST_MSG_RESULT_ERR:
3080 case RTE_VHOST_MSG_RESULT_OK:
3082 case RTE_VHOST_MSG_RESULT_NOT_HANDLED:
3088 /* If message was not handled at this stage, treat it as an error */
3090 VHOST_LOG_CONFIG(ERR, "(%s) vhost message (req: %d) was not handled.\n",
3091 dev->ifname, request);
3092 close_msg_fds(&ctx);
3093 ret = RTE_VHOST_MSG_RESULT_ERR;
3097 * If the request required a reply that was already sent,
3098 * this optional reply-ack won't be sent as the
3099 * VHOST_USER_NEED_REPLY was cleared in send_vhost_reply().
3101 if (ctx.msg.flags & VHOST_USER_NEED_REPLY) {
3102 ctx.msg.payload.u64 = ret == RTE_VHOST_MSG_RESULT_ERR;
3103 ctx.msg.size = sizeof(ctx.msg.payload.u64);
3105 send_vhost_reply(dev, fd, &ctx);
3106 } else if (ret == RTE_VHOST_MSG_RESULT_ERR) {
3107 VHOST_LOG_CONFIG(ERR, "(%s) vhost message handling failed.\n", dev->ifname);
3111 for (i = 0; i < dev->nr_vring; i++) {
3112 struct vhost_virtqueue *vq = dev->virtqueue[i];
3113 bool cur_ready = vq_is_ready(dev, vq);
3115 if (cur_ready != (vq && vq->ready)) {
3116 vq->ready = cur_ready;
3117 vhost_user_notify_queue_state(dev, i, cur_ready);
3121 if (unlock_required)
3122 vhost_user_unlock_all_queue_pairs(dev);
3124 if (!virtio_is_ready(dev))
3128 * Virtio is now ready. If not done already, it is time
3129 * to notify the application it can process the rings and
3130 * configure the vDPA device if present.
3133 if (!(dev->flags & VIRTIO_DEV_RUNNING)) {
3134 if (dev->notify_ops->new_device(dev->vid) == 0)
3135 dev->flags |= VIRTIO_DEV_RUNNING;
3138 vdpa_dev = dev->vdpa_dev;
3142 if (!(dev->flags & VIRTIO_DEV_VDPA_CONFIGURED)) {
3143 if (vdpa_dev->ops->dev_conf(dev->vid))
3144 VHOST_LOG_CONFIG(ERR, "(%s) failed to configure vDPA device\n",
3147 dev->flags |= VIRTIO_DEV_VDPA_CONFIGURED;
3154 static int process_slave_message_reply(struct virtio_net *dev,
3155 const struct vhu_msg_context *ctx)
3157 struct vhu_msg_context msg_reply;
3160 if ((ctx->msg.flags & VHOST_USER_NEED_REPLY) == 0)
3163 ret = read_vhost_message(dev, dev->slave_req_fd, &msg_reply);
3166 VHOST_LOG_CONFIG(ERR, "(%s) vhost read slave message reply failed\n",
3169 VHOST_LOG_CONFIG(INFO, "(%s) vhost peer closed\n", dev->ifname);
3175 if (msg_reply.msg.request.slave != ctx->msg.request.slave) {
3176 VHOST_LOG_CONFIG(ERR, "(%s) received unexpected msg type (%u), expected %u\n",
3177 dev->ifname, msg_reply.msg.request.slave, ctx->msg.request.slave);
3182 ret = msg_reply.msg.payload.u64 ? -1 : 0;
3185 rte_spinlock_unlock(&dev->slave_req_lock);
3190 vhost_user_iotlb_miss(struct virtio_net *dev, uint64_t iova, uint8_t perm)
3193 struct vhu_msg_context ctx = {
3195 .request.slave = VHOST_USER_SLAVE_IOTLB_MSG,
3196 .flags = VHOST_USER_VERSION,
3197 .size = sizeof(ctx.msg.payload.iotlb),
3201 .type = VHOST_IOTLB_MISS,
3206 ret = send_vhost_message(dev, dev->slave_req_fd, &ctx);
3208 VHOST_LOG_CONFIG(ERR, "(%s) failed to send IOTLB miss message (%d)\n",
3217 vhost_user_slave_config_change(struct virtio_net *dev, bool need_reply)
3220 struct vhu_msg_context ctx = {
3222 .request.slave = VHOST_USER_SLAVE_CONFIG_CHANGE_MSG,
3223 .flags = VHOST_USER_VERSION,
3229 ctx.msg.flags |= VHOST_USER_NEED_REPLY;
3231 ret = send_vhost_slave_message(dev, &ctx);
3233 VHOST_LOG_CONFIG(ERR, "(%s) failed to send config change (%d)\n",
3238 return process_slave_message_reply(dev, &ctx);
3242 rte_vhost_slave_config_change(int vid, bool need_reply)
3244 struct virtio_net *dev;
3246 dev = get_device(vid);
3250 return vhost_user_slave_config_change(dev, need_reply);
3253 static int vhost_user_slave_set_vring_host_notifier(struct virtio_net *dev,
3259 struct vhu_msg_context ctx = {
3261 .request.slave = VHOST_USER_SLAVE_VRING_HOST_NOTIFIER_MSG,
3262 .flags = VHOST_USER_VERSION | VHOST_USER_NEED_REPLY,
3263 .size = sizeof(ctx.msg.payload.area),
3265 .u64 = index & VHOST_USER_VRING_IDX_MASK,
3273 ctx.msg.payload.area.u64 |= VHOST_USER_VRING_NOFD_MASK;
3279 ret = send_vhost_slave_message(dev, &ctx);
3281 VHOST_LOG_CONFIG(ERR, "(%s) failed to set host notifier (%d)\n",
3286 return process_slave_message_reply(dev, &ctx);
3289 int rte_vhost_host_notifier_ctrl(int vid, uint16_t qid, bool enable)
3291 struct virtio_net *dev;
3292 struct rte_vdpa_device *vdpa_dev;
3293 int vfio_device_fd, ret = 0;
3294 uint64_t offset, size;
3295 unsigned int i, q_start, q_last;
3297 dev = get_device(vid);
3301 vdpa_dev = dev->vdpa_dev;
3302 if (vdpa_dev == NULL)
3305 if (!(dev->features & (1ULL << VIRTIO_F_VERSION_1)) ||
3306 !(dev->features & (1ULL << VHOST_USER_F_PROTOCOL_FEATURES)) ||
3307 !(dev->protocol_features &
3308 (1ULL << VHOST_USER_PROTOCOL_F_SLAVE_REQ)) ||
3309 !(dev->protocol_features &
3310 (1ULL << VHOST_USER_PROTOCOL_F_SLAVE_SEND_FD)) ||
3311 !(dev->protocol_features &
3312 (1ULL << VHOST_USER_PROTOCOL_F_HOST_NOTIFIER)))
3315 if (qid == RTE_VHOST_QUEUE_ALL) {
3317 q_last = dev->nr_vring - 1;
3319 if (qid >= dev->nr_vring)
3325 RTE_FUNC_PTR_OR_ERR_RET(vdpa_dev->ops->get_vfio_device_fd, -ENOTSUP);
3326 RTE_FUNC_PTR_OR_ERR_RET(vdpa_dev->ops->get_notify_area, -ENOTSUP);
3328 vfio_device_fd = vdpa_dev->ops->get_vfio_device_fd(vid);
3329 if (vfio_device_fd < 0)
3333 for (i = q_start; i <= q_last; i++) {
3334 if (vdpa_dev->ops->get_notify_area(vid, i, &offset,
3340 if (vhost_user_slave_set_vring_host_notifier(dev, i,
3341 vfio_device_fd, offset, size) < 0) {
3348 for (i = q_start; i <= q_last; i++) {
3349 vhost_user_slave_set_vring_host_notifier(dev, i, -1,