X-Git-Url: http://git.droids-corp.org/?a=blobdiff_plain;f=lib%2Fvhost%2Fvhost_user.c;h=1d390677fa4c2a16675742b48fdd1a62d0c8687a;hb=af74f7db384ed149fe42b21dbd7975f8a54ef227;hp=9489d03e45554c09158280b7fde0f5e21ddf0199;hpb=5fd6e93b7e76e483701453bdd70d455698ed67ce;p=dpdk.git diff --git a/lib/vhost/vhost_user.c b/lib/vhost/vhost_user.c index 9489d03e45..1d390677fa 100644 --- a/lib/vhost/vhost_user.c +++ b/lib/vhost/vhost_user.c @@ -27,10 +27,8 @@ #include #include #include -#include #include #include -#include #ifdef RTE_LIBRTE_VHOST_NUMA #include #endif @@ -93,21 +91,21 @@ static const char *vhost_message_str[VHOST_USER_MAX] = { [VHOST_USER_GET_STATUS] = "VHOST_USER_GET_STATUS", }; -static int send_vhost_reply(int sockfd, struct VhostUserMsg *msg); -static int read_vhost_message(int sockfd, struct VhostUserMsg *msg); +static int send_vhost_reply(struct virtio_net *dev, int sockfd, struct vhu_msg_context *ctx); +static int read_vhost_message(struct virtio_net *dev, int sockfd, struct vhu_msg_context *ctx); static void -close_msg_fds(struct VhostUserMsg *msg) +close_msg_fds(struct vhu_msg_context *ctx) { int i; - for (i = 0; i < msg->fd_num; i++) { - int fd = msg->fds[i]; + for (i = 0; i < ctx->fd_num; i++) { + int fd = ctx->fds[i]; if (fd == -1) continue; - msg->fds[i] = -1; + ctx->fds[i] = -1; close(fd); } } @@ -117,18 +115,17 @@ close_msg_fds(struct VhostUserMsg *msg) * close all FDs and return an error if this is not the case. */ static int -validate_msg_fds(struct VhostUserMsg *msg, int expected_fds) +validate_msg_fds(struct virtio_net *dev, struct vhu_msg_context *ctx, int expected_fds) { - if (msg->fd_num == expected_fds) + if (ctx->fd_num == expected_fds) return 0; - VHOST_LOG_CONFIG(ERR, - " Expect %d FDs for request %s, received %d\n", - expected_fds, - vhost_message_str[msg->request.master], - msg->fd_num); + VHOST_LOG_CONFIG(ERR, "(%s) expect %d FDs for request %s, received %d\n", + dev->ifname, expected_fds, + vhost_message_str[ctx->msg.request.master], + ctx->fd_num); - close_msg_fds(msg); + close_msg_fds(ctx); return -1; } @@ -143,57 +140,57 @@ get_blk_size(int fd) return ret == -1 ? (uint64_t)-1 : (uint64_t)stat.st_blksize; } -static int -async_dma_map(struct rte_vhost_mem_region *region, bool do_map) +static void +async_dma_map(struct virtio_net *dev, bool do_map) { - uint64_t host_iova; int ret = 0; + uint32_t i; + struct guest_page *page; - host_iova = rte_mem_virt2iova((void *)(uintptr_t)region->host_user_addr); if (do_map) { - /* Add mapped region into the default container of DPDK. */ - ret = rte_vfio_container_dma_map(RTE_VFIO_DEFAULT_CONTAINER_FD, - region->host_user_addr, - host_iova, - region->size); - if (ret) { - /* - * DMA device may bind with kernel driver, in this case, - * we don't need to program IOMMU manually. However, if no - * device is bound with vfio/uio in DPDK, and vfio kernel - * module is loaded, the API will still be called and return - * with ENODEV/ENOSUP. - * - * DPDK vfio only returns ENODEV/ENOSUP in very similar - * situations(vfio either unsupported, or supported - * but no devices found). Either way, no mappings could be - * performed. We treat it as normal case in async path. - */ - if (rte_errno == ENODEV || rte_errno == ENOTSUP) - return 0; - - VHOST_LOG_CONFIG(ERR, "DMA engine map failed\n"); - /* DMA mapping errors won't stop VHST_USER_SET_MEM_TABLE. */ - return 0; + for (i = 0; i < dev->nr_guest_pages; i++) { + page = &dev->guest_pages[i]; + ret = rte_vfio_container_dma_map(RTE_VFIO_DEFAULT_CONTAINER_FD, + page->host_user_addr, + page->host_iova, + page->size); + if (ret) { + /* + * DMA device may bind with kernel driver, in this case, + * we don't need to program IOMMU manually. However, if no + * device is bound with vfio/uio in DPDK, and vfio kernel + * module is loaded, the API will still be called and return + * with ENODEV. + * + * DPDK vfio only returns ENODEV in very similar situations + * (vfio either unsupported, or supported but no devices found). + * Either way, no mappings could be performed. We treat it as + * normal case in async path. This is a workaround. + */ + if (rte_errno == ENODEV) + return; + + /* DMA mapping errors won't stop VHOST_USER_SET_MEM_TABLE. */ + VHOST_LOG_CONFIG(ERR, "DMA engine map failed\n"); + } } } else { - /* Remove mapped region from the default container of DPDK. */ - ret = rte_vfio_container_dma_unmap(RTE_VFIO_DEFAULT_CONTAINER_FD, - region->host_user_addr, - host_iova, - region->size); - if (ret) { - /* like DMA map, ignore the kernel driver case when unmap. */ - if (rte_errno == EINVAL) - return 0; - - VHOST_LOG_CONFIG(ERR, "DMA engine unmap failed\n"); - return ret; + for (i = 0; i < dev->nr_guest_pages; i++) { + page = &dev->guest_pages[i]; + ret = rte_vfio_container_dma_unmap(RTE_VFIO_DEFAULT_CONTAINER_FD, + page->host_user_addr, + page->host_iova, + page->size); + if (ret) { + /* like DMA map, ignore the kernel driver case when unmap. */ + if (rte_errno == EINVAL) + return; + + VHOST_LOG_CONFIG(ERR, "DMA engine unmap failed\n"); + } } } - - return ret; } static void @@ -205,12 +202,12 @@ free_mem_region(struct virtio_net *dev) if (!dev || !dev->mem) return; + if (dev->async_copy && rte_vfio_is_enabled("vfio")) + async_dma_map(dev, false); + for (i = 0; i < dev->mem->nregions; i++) { reg = &dev->mem->regions[i]; if (reg->host_user_addr) { - if (dev->async_copy && rte_vfio_is_enabled("vfio")) - async_dma_map(reg, false); - munmap(reg->mmap_addr, reg->mmap_size); close(reg->fd); } @@ -220,6 +217,12 @@ free_mem_region(struct virtio_net *dev) void vhost_backend_cleanup(struct virtio_net *dev) { + struct rte_vdpa_device *vdpa_dev; + + vdpa_dev = dev->vdpa_dev; + if (vdpa_dev && vdpa_dev->ops->dev_cleanup != NULL) + vdpa_dev->ops->dev_cleanup(dev->vid); + if (dev->mem) { free_mem_region(dev); rte_free(dev->mem); @@ -287,11 +290,13 @@ vhost_user_notify_queue_state(struct virtio_net *dev, uint16_t index, * the device hasn't been initialised. */ static int -vhost_user_set_owner(struct virtio_net **pdev __rte_unused, - struct VhostUserMsg *msg, +vhost_user_set_owner(struct virtio_net **pdev, + struct vhu_msg_context *ctx, int main_fd __rte_unused) { - if (validate_msg_fds(msg, 0) != 0) + struct virtio_net *dev = *pdev; + + if (validate_msg_fds(dev, ctx, 0) != 0) return RTE_VHOST_MSG_RESULT_ERR; return RTE_VHOST_MSG_RESULT_OK; @@ -299,12 +304,12 @@ vhost_user_set_owner(struct virtio_net **pdev __rte_unused, static int vhost_user_reset_owner(struct virtio_net **pdev, - struct VhostUserMsg *msg, + struct vhu_msg_context *ctx, int main_fd __rte_unused) { struct virtio_net *dev = *pdev; - if (validate_msg_fds(msg, 0) != 0) + if (validate_msg_fds(dev, ctx, 0) != 0) return RTE_VHOST_MSG_RESULT_ERR; vhost_destroy_device_notify(dev); @@ -318,20 +323,21 @@ vhost_user_reset_owner(struct virtio_net **pdev, * The features that we support are requested. */ static int -vhost_user_get_features(struct virtio_net **pdev, struct VhostUserMsg *msg, +vhost_user_get_features(struct virtio_net **pdev, + struct vhu_msg_context *ctx, int main_fd __rte_unused) { struct virtio_net *dev = *pdev; uint64_t features = 0; - if (validate_msg_fds(msg, 0) != 0) + if (validate_msg_fds(dev, ctx, 0) != 0) return RTE_VHOST_MSG_RESULT_ERR; rte_vhost_driver_get_features(dev->ifname, &features); - msg->payload.u64 = features; - msg->size = sizeof(msg->payload.u64); - msg->fd_num = 0; + ctx->msg.payload.u64 = features; + ctx->msg.size = sizeof(ctx->msg.payload.u64); + ctx->fd_num = 0; return RTE_VHOST_MSG_RESULT_REPLY; } @@ -340,20 +346,21 @@ vhost_user_get_features(struct virtio_net **pdev, struct VhostUserMsg *msg, * The queue number that we support are requested. */ static int -vhost_user_get_queue_num(struct virtio_net **pdev, struct VhostUserMsg *msg, +vhost_user_get_queue_num(struct virtio_net **pdev, + struct vhu_msg_context *ctx, int main_fd __rte_unused) { struct virtio_net *dev = *pdev; uint32_t queue_num = 0; - if (validate_msg_fds(msg, 0) != 0) + if (validate_msg_fds(dev, ctx, 0) != 0) return RTE_VHOST_MSG_RESULT_ERR; rte_vhost_driver_get_queue_num(dev->ifname, &queue_num); - msg->payload.u64 = (uint64_t)queue_num; - msg->size = sizeof(msg->payload.u64); - msg->fd_num = 0; + ctx->msg.payload.u64 = (uint64_t)queue_num; + ctx->msg.size = sizeof(ctx->msg.payload.u64); + ctx->fd_num = 0; return RTE_VHOST_MSG_RESULT_REPLY; } @@ -362,22 +369,22 @@ vhost_user_get_queue_num(struct virtio_net **pdev, struct VhostUserMsg *msg, * We receive the negotiated features supported by us and the virtio device. */ static int -vhost_user_set_features(struct virtio_net **pdev, struct VhostUserMsg *msg, +vhost_user_set_features(struct virtio_net **pdev, + struct vhu_msg_context *ctx, int main_fd __rte_unused) { struct virtio_net *dev = *pdev; - uint64_t features = msg->payload.u64; + uint64_t features = ctx->msg.payload.u64; uint64_t vhost_features = 0; struct rte_vdpa_device *vdpa_dev; - if (validate_msg_fds(msg, 0) != 0) + if (validate_msg_fds(dev, ctx, 0) != 0) return RTE_VHOST_MSG_RESULT_ERR; rte_vhost_driver_get_features(dev->ifname, &vhost_features); if (features & ~vhost_features) { - VHOST_LOG_CONFIG(ERR, - "(%d) received invalid negotiated features.\n", - dev->vid); + VHOST_LOG_CONFIG(ERR, "(%s) received invalid negotiated features.\n", + dev->ifname); dev->flags |= VIRTIO_DEV_FEATURES_FAILED; dev->status &= ~VIRTIO_DEVICE_STATUS_FEATURES_OK; @@ -394,9 +401,8 @@ vhost_user_set_features(struct virtio_net **pdev, struct VhostUserMsg *msg, * is enabled when the live-migration starts. */ if ((dev->features ^ features) & ~(1ULL << VHOST_F_LOG_ALL)) { - VHOST_LOG_CONFIG(ERR, - "(%d) features changed while device is running.\n", - dev->vid); + VHOST_LOG_CONFIG(ERR, "(%s) features changed while device is running.\n", + dev->ifname); return RTE_VHOST_MSG_RESULT_ERR; } @@ -413,11 +419,10 @@ vhost_user_set_features(struct virtio_net **pdev, struct VhostUserMsg *msg, } else { dev->vhost_hlen = sizeof(struct virtio_net_hdr); } - VHOST_LOG_CONFIG(INFO, - "negotiated Virtio features: 0x%" PRIx64 "\n", dev->features); - VHOST_LOG_CONFIG(DEBUG, - "(%d) mergeable RX buffers %s, virtio 1 %s\n", - dev->vid, + VHOST_LOG_CONFIG(INFO, "(%s) negotiated Virtio features: 0x%" PRIx64 "\n", + dev->ifname, dev->features); + VHOST_LOG_CONFIG(DEBUG, "(%s) mergeable RX buffers %s, virtio 1 %s\n", + dev->ifname, (dev->features & (1 << VIRTIO_NET_F_MRG_RXBUF)) ? "on" : "off", (dev->features & (1ULL << VIRTIO_F_VERSION_1)) ? "on" : "off"); @@ -455,21 +460,22 @@ vhost_user_set_features(struct virtio_net **pdev, struct VhostUserMsg *msg, */ static int vhost_user_set_vring_num(struct virtio_net **pdev, - struct VhostUserMsg *msg, + struct vhu_msg_context *ctx, int main_fd __rte_unused) { struct virtio_net *dev = *pdev; - struct vhost_virtqueue *vq = dev->virtqueue[msg->payload.state.index]; + struct vhost_virtqueue *vq = dev->virtqueue[ctx->msg.payload.state.index]; - if (validate_msg_fds(msg, 0) != 0) + if (validate_msg_fds(dev, ctx, 0) != 0) return RTE_VHOST_MSG_RESULT_ERR; - if (msg->payload.state.num > 32768) { - VHOST_LOG_CONFIG(ERR, "invalid virtqueue size %u\n", msg->payload.state.num); + if (ctx->msg.payload.state.num > 32768) { + VHOST_LOG_CONFIG(ERR, "(%s) invalid virtqueue size %u\n", + dev->ifname, ctx->msg.payload.state.num); return RTE_VHOST_MSG_RESULT_ERR; } - vq->size = msg->payload.state.num; + vq->size = ctx->msg.payload.state.num; /* VIRTIO 1.0, 2.4 Virtqueues says: * @@ -482,28 +488,27 @@ vhost_user_set_vring_num(struct virtio_net **pdev, */ if (!vq_is_packed(dev)) { if (vq->size & (vq->size - 1)) { - VHOST_LOG_CONFIG(ERR, - "invalid virtqueue size %u\n", vq->size); + VHOST_LOG_CONFIG(ERR, "(%s) invalid virtqueue size %u\n", + dev->ifname, vq->size); return RTE_VHOST_MSG_RESULT_ERR; } } if (vq_is_packed(dev)) { - if (vq->shadow_used_packed) - rte_free(vq->shadow_used_packed); + rte_free(vq->shadow_used_packed); vq->shadow_used_packed = rte_malloc_socket(NULL, vq->size * sizeof(struct vring_used_elem_packed), RTE_CACHE_LINE_SIZE, vq->numa_node); if (!vq->shadow_used_packed) { VHOST_LOG_CONFIG(ERR, - "failed to allocate memory for shadow used ring.\n"); + "(%s) failed to allocate memory for shadow used ring.\n", + dev->ifname); return RTE_VHOST_MSG_RESULT_ERR; } } else { - if (vq->shadow_used_split) - rte_free(vq->shadow_used_split); + rte_free(vq->shadow_used_split); vq->shadow_used_split = rte_malloc_socket(NULL, vq->size * sizeof(struct vring_used_elem), @@ -511,19 +516,19 @@ vhost_user_set_vring_num(struct virtio_net **pdev, if (!vq->shadow_used_split) { VHOST_LOG_CONFIG(ERR, - "failed to allocate memory for vq internal data.\n"); + "(%s) failed to allocate memory for vq internal data.\n", + dev->ifname); return RTE_VHOST_MSG_RESULT_ERR; } } - if (vq->batch_copy_elems) - rte_free(vq->batch_copy_elems); + rte_free(vq->batch_copy_elems); vq->batch_copy_elems = rte_malloc_socket(NULL, vq->size * sizeof(struct batch_copy_elem), RTE_CACHE_LINE_SIZE, vq->numa_node); if (!vq->batch_copy_elems) { - VHOST_LOG_CONFIG(ERR, - "failed to allocate memory for batching copy.\n"); + VHOST_LOG_CONFIG(ERR, "(%s) failed to allocate memory for batching copy.\n", + dev->ifname); return RTE_VHOST_MSG_RESULT_ERR; } @@ -559,7 +564,8 @@ numa_realloc(struct virtio_net *dev, int index) ret = get_mempolicy(&node, NULL, 0, vq->desc, MPOL_F_NODE | MPOL_F_ADDR); if (ret) { - VHOST_LOG_CONFIG(ERR, "Unable to get virtqueue %d numa information.\n", index); + VHOST_LOG_CONFIG(ERR, "(%s) unable to get virtqueue %d numa information.\n", + dev->ifname, index); return dev; } @@ -568,13 +574,14 @@ numa_realloc(struct virtio_net *dev, int index) vq = rte_realloc_socket(vq, sizeof(*vq), 0, node); if (!vq) { - VHOST_LOG_CONFIG(ERR, "Failed to realloc virtqueue %d on node %d\n", - index, node); + VHOST_LOG_CONFIG(ERR, "(%s) failed to realloc virtqueue %d on node %d\n", + dev->ifname, index, node); return dev; } if (vq != dev->virtqueue[index]) { - VHOST_LOG_CONFIG(INFO, "reallocated virtqueue on node %d\n", node); + VHOST_LOG_CONFIG(INFO, "(%s) reallocated virtqueue on node %d\n", + dev->ifname, node); dev->virtqueue[index] = vq; vhost_user_iotlb_init(dev, index); } @@ -585,7 +592,8 @@ numa_realloc(struct virtio_net *dev, int index) sup = rte_realloc_socket(vq->shadow_used_packed, vq->size * sizeof(*sup), RTE_CACHE_LINE_SIZE, node); if (!sup) { - VHOST_LOG_CONFIG(ERR, "Failed to realloc shadow packed on node %d\n", node); + VHOST_LOG_CONFIG(ERR, "(%s) failed to realloc shadow packed on node %d\n", + dev->ifname, node); return dev; } vq->shadow_used_packed = sup; @@ -595,7 +603,8 @@ numa_realloc(struct virtio_net *dev, int index) sus = rte_realloc_socket(vq->shadow_used_split, vq->size * sizeof(*sus), RTE_CACHE_LINE_SIZE, node); if (!sus) { - VHOST_LOG_CONFIG(ERR, "Failed to realloc shadow split on node %d\n", node); + VHOST_LOG_CONFIG(ERR, "(%s) failed to realloc shadow split on node %d\n", + dev->ifname, node); return dev; } vq->shadow_used_split = sus; @@ -604,7 +613,8 @@ numa_realloc(struct virtio_net *dev, int index) bce = rte_realloc_socket(vq->batch_copy_elems, vq->size * sizeof(*bce), RTE_CACHE_LINE_SIZE, node); if (!bce) { - VHOST_LOG_CONFIG(ERR, "Failed to realloc batch copy elem on node %d\n", node); + VHOST_LOG_CONFIG(ERR, "(%s) failed to realloc batch copy elem on node %d\n", + dev->ifname, node); return dev; } vq->batch_copy_elems = bce; @@ -614,7 +624,8 @@ numa_realloc(struct virtio_net *dev, int index) lc = rte_realloc_socket(vq->log_cache, sizeof(*lc) * VHOST_LOG_CACHE_NR, 0, node); if (!lc) { - VHOST_LOG_CONFIG(ERR, "Failed to realloc log cache on node %d\n", node); + VHOST_LOG_CONFIG(ERR, "(%s) failed to realloc log cache on node %d\n", + dev->ifname, node); return dev; } vq->log_cache = lc; @@ -625,8 +636,8 @@ numa_realloc(struct virtio_net *dev, int index) ri = rte_realloc_socket(vq->resubmit_inflight, sizeof(*ri), 0, node); if (!ri) { - VHOST_LOG_CONFIG(ERR, "Failed to realloc resubmit inflight on node %d\n", - node); + VHOST_LOG_CONFIG(ERR, "(%s) failed to realloc resubmit inflight on node %d\n", + dev->ifname, node); return dev; } vq->resubmit_inflight = ri; @@ -637,8 +648,8 @@ numa_realloc(struct virtio_net *dev, int index) rd = rte_realloc_socket(ri->resubmit_list, sizeof(*rd) * ri->resubmit_num, 0, node); if (!rd) { - VHOST_LOG_CONFIG(ERR, "Failed to realloc resubmit list on node %d\n", - node); + VHOST_LOG_CONFIG(ERR, "(%s) failed to realloc resubmit list on node %d\n", + dev->ifname, node); return dev; } ri->resubmit_list = rd; @@ -654,7 +665,7 @@ out_dev_realloc: ret = get_mempolicy(&dev_node, NULL, 0, dev, MPOL_F_NODE | MPOL_F_ADDR); if (ret) { - VHOST_LOG_CONFIG(ERR, "Unable to get Virtio dev %d numa information.\n", dev->vid); + VHOST_LOG_CONFIG(ERR, "(%s) unable to get numa information.\n", dev->ifname); return dev; } @@ -663,18 +674,20 @@ out_dev_realloc: dev = rte_realloc_socket(old_dev, sizeof(*dev), 0, node); if (!dev) { - VHOST_LOG_CONFIG(ERR, "Failed to realloc dev on node %d\n", node); + VHOST_LOG_CONFIG(ERR, "(%s) failed to realloc dev on node %d\n", + old_dev->ifname, node); return old_dev; } - VHOST_LOG_CONFIG(INFO, "reallocated device on node %d\n", node); + VHOST_LOG_CONFIG(INFO, "(%s) reallocated device on node %d\n", dev->ifname, node); vhost_devices[dev->vid] = dev; mem_size = sizeof(struct rte_vhost_memory) + sizeof(struct rte_vhost_mem_region) * dev->mem->nregions; mem = rte_realloc_socket(dev->mem, mem_size, 0, node); if (!mem) { - VHOST_LOG_CONFIG(ERR, "Failed to realloc mem table on node %d\n", node); + VHOST_LOG_CONFIG(ERR, "(%s) failed to realloc mem table on node %d\n", + dev->ifname, node); return dev; } dev->mem = mem; @@ -682,7 +695,8 @@ out_dev_realloc: gp = rte_realloc_socket(dev->guest_pages, dev->max_guest_pages * sizeof(*gp), RTE_CACHE_LINE_SIZE, node); if (!gp) { - VHOST_LOG_CONFIG(ERR, "Failed to realloc guest pages on node %d\n", node); + VHOST_LOG_CONFIG(ERR, "(%s) failed to realloc guest pages on node %d\n", + dev->ifname, node); return dev; } dev->guest_pages = gp; @@ -774,9 +788,8 @@ translate_ring_addresses(struct virtio_net *dev, int vq_index) vq->log_guest_addr = log_addr_to_gpa(dev, vq); if (vq->log_guest_addr == 0) { - VHOST_LOG_CONFIG(DEBUG, - "(%d) failed to map log_guest_addr.\n", - dev->vid); + VHOST_LOG_CONFIG(DEBUG, "(%s) failed to map log_guest_addr.\n", + dev->ifname); return dev; } } @@ -788,9 +801,8 @@ translate_ring_addresses(struct virtio_net *dev, int vq_index) if (vq->desc_packed == NULL || len != sizeof(struct vring_packed_desc) * vq->size) { - VHOST_LOG_CONFIG(DEBUG, - "(%d) failed to map desc_packed ring.\n", - dev->vid); + VHOST_LOG_CONFIG(DEBUG, "(%s) failed to map desc_packed ring.\n", + dev->ifname); return dev; } @@ -804,9 +816,8 @@ translate_ring_addresses(struct virtio_net *dev, int vq_index) vq, addr->avail_user_addr, &len); if (vq->driver_event == NULL || len != sizeof(struct vring_packed_desc_event)) { - VHOST_LOG_CONFIG(DEBUG, - "(%d) failed to find driver area address.\n", - dev->vid); + VHOST_LOG_CONFIG(DEBUG, "(%s) failed to find driver area address.\n", + dev->ifname); return dev; } @@ -816,9 +827,8 @@ translate_ring_addresses(struct virtio_net *dev, int vq_index) vq, addr->used_user_addr, &len); if (vq->device_event == NULL || len != sizeof(struct vring_packed_desc_event)) { - VHOST_LOG_CONFIG(DEBUG, - "(%d) failed to find device area address.\n", - dev->vid); + VHOST_LOG_CONFIG(DEBUG, "(%s) failed to find device area address.\n", + dev->ifname); return dev; } @@ -834,9 +844,7 @@ translate_ring_addresses(struct virtio_net *dev, int vq_index) vq->desc = (struct vring_desc *)(uintptr_t)ring_addr_to_vva(dev, vq, addr->desc_user_addr, &len); if (vq->desc == 0 || len != sizeof(struct vring_desc) * vq->size) { - VHOST_LOG_CONFIG(DEBUG, - "(%d) failed to map desc ring.\n", - dev->vid); + VHOST_LOG_CONFIG(DEBUG, "(%s) failed to map desc ring.\n", dev->ifname); return dev; } @@ -851,9 +859,7 @@ translate_ring_addresses(struct virtio_net *dev, int vq_index) vq->avail = (struct vring_avail *)(uintptr_t)ring_addr_to_vva(dev, vq, addr->avail_user_addr, &len); if (vq->avail == 0 || len != expected_len) { - VHOST_LOG_CONFIG(DEBUG, - "(%d) failed to map avail ring.\n", - dev->vid); + VHOST_LOG_CONFIG(DEBUG, "(%s) failed to map avail ring.\n", dev->ifname); return dev; } @@ -865,31 +871,27 @@ translate_ring_addresses(struct virtio_net *dev, int vq_index) vq->used = (struct vring_used *)(uintptr_t)ring_addr_to_vva(dev, vq, addr->used_user_addr, &len); if (vq->used == 0 || len != expected_len) { - VHOST_LOG_CONFIG(DEBUG, - "(%d) failed to map used ring.\n", - dev->vid); + VHOST_LOG_CONFIG(DEBUG, "(%s) failed to map used ring.\n", dev->ifname); return dev; } if (vq->last_used_idx != vq->used->idx) { - VHOST_LOG_CONFIG(WARNING, - "last_used_idx (%u) and vq->used->idx (%u) mismatches; " - "some packets maybe resent for Tx and dropped for Rx\n", + VHOST_LOG_CONFIG(WARNING, "(%s) last_used_idx (%u) and vq->used->idx (%u) mismatches;\n", + dev->ifname, vq->last_used_idx, vq->used->idx); vq->last_used_idx = vq->used->idx; vq->last_avail_idx = vq->used->idx; + VHOST_LOG_CONFIG(WARNING, "(%s) some packets maybe resent for Tx and dropped for Rx\n", + dev->ifname); } vq->access_ok = true; - VHOST_LOG_CONFIG(DEBUG, "(%d) mapped address desc: %p\n", - dev->vid, vq->desc); - VHOST_LOG_CONFIG(DEBUG, "(%d) mapped address avail: %p\n", - dev->vid, vq->avail); - VHOST_LOG_CONFIG(DEBUG, "(%d) mapped address used: %p\n", - dev->vid, vq->used); - VHOST_LOG_CONFIG(DEBUG, "(%d) log_guest_addr: %" PRIx64 "\n", - dev->vid, vq->log_guest_addr); + VHOST_LOG_CONFIG(DEBUG, "(%s) mapped address desc: %p\n", dev->ifname, vq->desc); + VHOST_LOG_CONFIG(DEBUG, "(%s) mapped address avail: %p\n", dev->ifname, vq->avail); + VHOST_LOG_CONFIG(DEBUG, "(%s) mapped address used: %p\n", dev->ifname, vq->used); + VHOST_LOG_CONFIG(DEBUG, "(%s) log_guest_addr: %" PRIx64 "\n", + dev->ifname, vq->log_guest_addr); return dev; } @@ -899,22 +901,23 @@ translate_ring_addresses(struct virtio_net *dev, int vq_index) * This function then converts these to our address space. */ static int -vhost_user_set_vring_addr(struct virtio_net **pdev, struct VhostUserMsg *msg, +vhost_user_set_vring_addr(struct virtio_net **pdev, + struct vhu_msg_context *ctx, int main_fd __rte_unused) { struct virtio_net *dev = *pdev; struct vhost_virtqueue *vq; - struct vhost_vring_addr *addr = &msg->payload.addr; + struct vhost_vring_addr *addr = &ctx->msg.payload.addr; bool access_ok; - if (validate_msg_fds(msg, 0) != 0) + if (validate_msg_fds(dev, ctx, 0) != 0) return RTE_VHOST_MSG_RESULT_ERR; if (dev->mem == NULL) return RTE_VHOST_MSG_RESULT_ERR; /* addr->index refers to the queue index. The txq 1, rxq is 0. */ - vq = dev->virtqueue[msg->payload.addr.index]; + vq = dev->virtqueue[ctx->msg.payload.addr.index]; access_ok = vq->access_ok; @@ -929,7 +932,7 @@ vhost_user_set_vring_addr(struct virtio_net **pdev, struct VhostUserMsg *msg, if ((vq->enabled && (dev->features & (1ULL << VHOST_USER_F_PROTOCOL_FEATURES))) || access_ok) { - dev = translate_ring_addresses(dev, msg->payload.addr.index); + dev = translate_ring_addresses(dev, ctx->msg.payload.addr.index); if (!dev) return RTE_VHOST_MSG_RESULT_ERR; @@ -944,14 +947,14 @@ vhost_user_set_vring_addr(struct virtio_net **pdev, struct VhostUserMsg *msg, */ static int vhost_user_set_vring_base(struct virtio_net **pdev, - struct VhostUserMsg *msg, + struct vhu_msg_context *ctx, int main_fd __rte_unused) { struct virtio_net *dev = *pdev; - struct vhost_virtqueue *vq = dev->virtqueue[msg->payload.state.index]; - uint64_t val = msg->payload.state.num; + struct vhost_virtqueue *vq = dev->virtqueue[ctx->msg.payload.state.index]; + uint64_t val = ctx->msg.payload.state.num; - if (validate_msg_fds(msg, 0) != 0) + if (validate_msg_fds(dev, ctx, 0) != 0) return RTE_VHOST_MSG_RESULT_ERR; if (vq_is_packed(dev)) { @@ -969,16 +972,21 @@ vhost_user_set_vring_base(struct virtio_net **pdev, vq->last_used_idx = vq->last_avail_idx; vq->used_wrap_counter = vq->avail_wrap_counter; } else { - vq->last_used_idx = msg->payload.state.num; - vq->last_avail_idx = msg->payload.state.num; + vq->last_used_idx = ctx->msg.payload.state.num; + vq->last_avail_idx = ctx->msg.payload.state.num; } + VHOST_LOG_CONFIG(INFO, + "(%s) vring base idx:%u last_used_idx:%u last_avail_idx:%u.\n", + dev->ifname, ctx->msg.payload.state.index, vq->last_used_idx, + vq->last_avail_idx); + return RTE_VHOST_MSG_RESULT_OK; } static int add_one_guest_page(struct virtio_net *dev, uint64_t guest_phys_addr, - uint64_t host_phys_addr, uint64_t size) + uint64_t host_iova, uint64_t host_user_addr, uint64_t size) { struct guest_page *page, *last_page; struct guest_page *old_pages; @@ -999,8 +1007,9 @@ add_one_guest_page(struct virtio_net *dev, uint64_t guest_phys_addr, if (dev->nr_guest_pages > 0) { last_page = &dev->guest_pages[dev->nr_guest_pages - 1]; /* merge if the two pages are continuous */ - if (host_phys_addr == last_page->host_phys_addr + - last_page->size) { + if (host_iova == last_page->host_iova + last_page->size && + guest_phys_addr == last_page->guest_phys_addr + last_page->size && + host_user_addr == last_page->host_user_addr + last_page->size) { last_page->size += size; return 0; } @@ -1008,7 +1017,8 @@ add_one_guest_page(struct virtio_net *dev, uint64_t guest_phys_addr, page = &dev->guest_pages[dev->nr_guest_pages++]; page->guest_phys_addr = guest_phys_addr; - page->host_phys_addr = host_phys_addr; + page->host_iova = host_iova; + page->host_user_addr = host_user_addr; page->size = size; return 0; @@ -1021,14 +1031,15 @@ add_guest_pages(struct virtio_net *dev, struct rte_vhost_mem_region *reg, uint64_t reg_size = reg->size; uint64_t host_user_addr = reg->host_user_addr; uint64_t guest_phys_addr = reg->guest_phys_addr; - uint64_t host_phys_addr; + uint64_t host_iova; uint64_t size; - host_phys_addr = rte_mem_virt2iova((void *)(uintptr_t)host_user_addr); + host_iova = rte_mem_virt2iova((void *)(uintptr_t)host_user_addr); size = page_size - (guest_phys_addr & (page_size - 1)); size = RTE_MIN(size, reg_size); - if (add_one_guest_page(dev, guest_phys_addr, host_phys_addr, size) < 0) + if (add_one_guest_page(dev, guest_phys_addr, host_iova, + host_user_addr, size) < 0) return -1; host_user_addr += size; @@ -1037,10 +1048,10 @@ add_guest_pages(struct virtio_net *dev, struct rte_vhost_mem_region *reg, while (reg_size > 0) { size = RTE_MIN(reg_size, page_size); - host_phys_addr = rte_mem_virt2iova((void *)(uintptr_t) + host_iova = rte_mem_virt2iova((void *)(uintptr_t) host_user_addr); - if (add_one_guest_page(dev, guest_phys_addr, host_phys_addr, - size) < 0) + if (add_one_guest_page(dev, guest_phys_addr, host_iova, + host_user_addr, size) < 0) return -1; host_user_addr += size; @@ -1068,15 +1079,14 @@ dump_guest_pages(struct virtio_net *dev) for (i = 0; i < dev->nr_guest_pages; i++) { page = &dev->guest_pages[i]; - VHOST_LOG_CONFIG(INFO, - "guest physical page region %u\n" - "\t guest_phys_addr: %" PRIx64 "\n" - "\t host_phys_addr : %" PRIx64 "\n" - "\t size : %" PRIx64 "\n", - i, - page->guest_phys_addr, - page->host_phys_addr, - page->size); + VHOST_LOG_CONFIG(INFO, "(%s) guest physical page region %u\n", + dev->ifname, i); + VHOST_LOG_CONFIG(INFO, "(%s)\tguest_phys_addr: %" PRIx64 "\n", + dev->ifname, page->guest_phys_addr); + VHOST_LOG_CONFIG(INFO, "(%s)\thost_iova : %" PRIx64 "\n", + dev->ifname, page->host_iova); + VHOST_LOG_CONFIG(INFO, "(%s)\tsize : %" PRIx64 "\n", + dev->ifname, page->size); } } #else @@ -1115,7 +1125,7 @@ vhost_user_postcopy_region_register(struct virtio_net *dev, struct uffdio_register reg_struct; /* - * Let's register all the mmap'ed area to ensure + * Let's register all the mmapped area to ensure * alignment on page boundary. */ reg_struct.range.start = (uint64_t)(uintptr_t)reg->mmap_addr; @@ -1124,8 +1134,9 @@ vhost_user_postcopy_region_register(struct virtio_net *dev, if (ioctl(dev->postcopy_ufd, UFFDIO_REGISTER, ®_struct)) { - VHOST_LOG_CONFIG(ERR, "Failed to register ufd for region " + VHOST_LOG_CONFIG(ERR, "(%s) failed to register ufd for region " "%" PRIx64 " - %" PRIx64 " (ufd = %d) %s\n", + dev->ifname, (uint64_t)reg_struct.range.start, (uint64_t)reg_struct.range.start + (uint64_t)reg_struct.range.len - 1, @@ -1134,7 +1145,9 @@ vhost_user_postcopy_region_register(struct virtio_net *dev, return -1; } - VHOST_LOG_CONFIG(INFO, "\t userfaultfd registered for range : %" PRIx64 " - %" PRIx64 "\n", + VHOST_LOG_CONFIG(INFO, + "(%s)\t userfaultfd registered for range : %" PRIx64 " - %" PRIx64 "\n", + dev->ifname, (uint64_t)reg_struct.range.start, (uint64_t)reg_struct.range.start + (uint64_t)reg_struct.range.len - 1); @@ -1152,11 +1165,11 @@ vhost_user_postcopy_region_register(struct virtio_net *dev __rte_unused, static int vhost_user_postcopy_register(struct virtio_net *dev, int main_fd, - struct VhostUserMsg *msg) + struct vhu_msg_context *ctx) { struct VhostUserMemory *memory; struct rte_vhost_mem_region *reg; - VhostUserMsg ack_msg; + struct vhu_msg_context ack_ctx; uint32_t i; if (!dev->postcopy_listening) @@ -1167,32 +1180,31 @@ vhost_user_postcopy_register(struct virtio_net *dev, int main_fd, * DPDK's virtual address with Qemu, so that Qemu can * retrieve the region offset when handling userfaults. */ - memory = &msg->payload.memory; + memory = &ctx->msg.payload.memory; for (i = 0; i < memory->nregions; i++) { reg = &dev->mem->regions[i]; memory->regions[i].userspace_addr = reg->host_user_addr; } /* Send the addresses back to qemu */ - msg->fd_num = 0; - send_vhost_reply(main_fd, msg); + ctx->fd_num = 0; + send_vhost_reply(dev, main_fd, ctx); - /* Wait for qemu to acknolwedge it's got the addresses + /* Wait for qemu to acknowledge it got the addresses * we've got to wait before we're allowed to generate faults. */ - if (read_vhost_message(main_fd, &ack_msg) <= 0) { - VHOST_LOG_CONFIG(ERR, - "Failed to read qemu ack on postcopy set-mem-table\n"); + if (read_vhost_message(dev, main_fd, &ack_ctx) <= 0) { + VHOST_LOG_CONFIG(ERR, "(%s) failed to read qemu ack on postcopy set-mem-table\n", + dev->ifname); return -1; } - if (validate_msg_fds(&ack_msg, 0) != 0) + if (validate_msg_fds(dev, &ack_ctx, 0) != 0) return -1; - if (ack_msg.request.master != VHOST_USER_SET_MEM_TABLE) { - VHOST_LOG_CONFIG(ERR, - "Bad qemu ack on postcopy set-mem-table (%d)\n", - ack_msg.request.master); + if (ack_ctx.msg.request.master != VHOST_USER_SET_MEM_TABLE) { + VHOST_LOG_CONFIG(ERR, "(%s) bad qemu ack on postcopy set-mem-table (%d)\n", + dev->ifname, ack_ctx.msg.request.master); return -1; } @@ -1215,14 +1227,11 @@ vhost_user_mmap_region(struct virtio_net *dev, uint64_t mmap_size; uint64_t alignment; int populate; - int ret; /* Check for memory_size + mmap_offset overflow */ if (mmap_offset >= -region->size) { - VHOST_LOG_CONFIG(ERR, - "mmap_offset (%#"PRIx64") and memory_size " - "(%#"PRIx64") overflow\n", - mmap_offset, region->size); + VHOST_LOG_CONFIG(ERR, "(%s) mmap_offset (%#"PRIx64") and memory_size (%#"PRIx64") overflow\n", + dev->ifname, mmap_offset, region->size); return -1; } @@ -1236,8 +1245,8 @@ vhost_user_mmap_region(struct virtio_net *dev, */ alignment = get_blk_size(region->fd); if (alignment == (uint64_t)-1) { - VHOST_LOG_CONFIG(ERR, - "couldn't get hugepage size through fstat\n"); + VHOST_LOG_CONFIG(ERR, "(%s) couldn't get hugepage size through fstat\n", + dev->ifname); return -1; } mmap_size = RTE_ALIGN_CEIL(mmap_size, alignment); @@ -1250,9 +1259,8 @@ vhost_user_mmap_region(struct virtio_net *dev, * mmap() kernel implementation would return an error, but * better catch it before and provide useful info in the logs. */ - VHOST_LOG_CONFIG(ERR, "mmap size (0x%" PRIx64 ") " - "or alignment (0x%" PRIx64 ") is invalid\n", - region->size + mmap_offset, alignment); + VHOST_LOG_CONFIG(ERR, "(%s) mmap size (0x%" PRIx64 ") or alignment (0x%" PRIx64 ") is invalid\n", + dev->ifname, region->size + mmap_offset, alignment); return -1; } @@ -1261,7 +1269,7 @@ vhost_user_mmap_region(struct virtio_net *dev, MAP_SHARED | populate, region->fd, 0); if (mmap_addr == MAP_FAILED) { - VHOST_LOG_CONFIG(ERR, "mmap failed (%s).\n", strerror(errno)); + VHOST_LOG_CONFIG(ERR, "(%s) mmap failed (%s).\n", dev->ifname, strerror(errno)); return -1; } @@ -1271,66 +1279,58 @@ vhost_user_mmap_region(struct virtio_net *dev, if (dev->async_copy) { if (add_guest_pages(dev, region, alignment) < 0) { - VHOST_LOG_CONFIG(ERR, "adding guest pages to region failed.\n"); + VHOST_LOG_CONFIG(ERR, "(%s) adding guest pages to region failed.\n", + dev->ifname); return -1; } - - if (rte_vfio_is_enabled("vfio")) { - ret = async_dma_map(region, true); - if (ret) { - VHOST_LOG_CONFIG(ERR, "Configure IOMMU for DMA engine failed\n"); - return -1; - } - } } - VHOST_LOG_CONFIG(INFO, - "guest memory region size: 0x%" PRIx64 "\n" - "\t guest physical addr: 0x%" PRIx64 "\n" - "\t guest virtual addr: 0x%" PRIx64 "\n" - "\t host virtual addr: 0x%" PRIx64 "\n" - "\t mmap addr : 0x%" PRIx64 "\n" - "\t mmap size : 0x%" PRIx64 "\n" - "\t mmap align: 0x%" PRIx64 "\n" - "\t mmap off : 0x%" PRIx64 "\n", - region->size, - region->guest_phys_addr, - region->guest_user_addr, - region->host_user_addr, - (uint64_t)(uintptr_t)mmap_addr, - mmap_size, - alignment, - mmap_offset); + VHOST_LOG_CONFIG(INFO, "(%s) guest memory region size: 0x%" PRIx64 "\n", + dev->ifname, region->size); + VHOST_LOG_CONFIG(INFO, "(%s)\t guest physical addr: 0x%" PRIx64 "\n", + dev->ifname, region->guest_phys_addr); + VHOST_LOG_CONFIG(INFO, "(%s)\t guest virtual addr: 0x%" PRIx64 "\n", + dev->ifname, region->guest_user_addr); + VHOST_LOG_CONFIG(INFO, "(%s)\t host virtual addr: 0x%" PRIx64 "\n", + dev->ifname, region->host_user_addr); + VHOST_LOG_CONFIG(INFO, "(%s)\t mmap addr : 0x%" PRIx64 "\n", + dev->ifname, (uint64_t)(uintptr_t)mmap_addr); + VHOST_LOG_CONFIG(INFO, "(%s)\t mmap size : 0x%" PRIx64 "\n", + dev->ifname, mmap_size); + VHOST_LOG_CONFIG(INFO, "(%s)\t mmap align: 0x%" PRIx64 "\n", + dev->ifname, alignment); + VHOST_LOG_CONFIG(INFO, "(%s)\t mmap off : 0x%" PRIx64 "\n", + dev->ifname, mmap_offset); return 0; } static int -vhost_user_set_mem_table(struct virtio_net **pdev, struct VhostUserMsg *msg, +vhost_user_set_mem_table(struct virtio_net **pdev, + struct vhu_msg_context *ctx, int main_fd) { struct virtio_net *dev = *pdev; - struct VhostUserMemory *memory = &msg->payload.memory; + struct VhostUserMemory *memory = &ctx->msg.payload.memory; struct rte_vhost_mem_region *reg; int numa_node = SOCKET_ID_ANY; uint64_t mmap_offset; uint32_t i; bool async_notify = false; - if (validate_msg_fds(msg, memory->nregions) != 0) + if (validate_msg_fds(dev, ctx, memory->nregions) != 0) return RTE_VHOST_MSG_RESULT_ERR; if (memory->nregions > VHOST_MEMORY_MAX_NREGIONS) { - VHOST_LOG_CONFIG(ERR, - "too many memory regions (%u)\n", memory->nregions); + VHOST_LOG_CONFIG(ERR, "(%s) too many memory regions (%u)\n", + dev->ifname, memory->nregions); goto close_msg_fds; } if (dev->mem && !vhost_memory_changed(memory, dev->mem)) { - VHOST_LOG_CONFIG(INFO, - "(%d) memory regions not changed\n", dev->vid); + VHOST_LOG_CONFIG(INFO, "(%s) memory regions not changed\n", dev->ifname); - close_msg_fds(msg); + close_msg_fds(ctx); return RTE_VHOST_MSG_RESULT_OK; } @@ -1380,9 +1380,8 @@ vhost_user_set_mem_table(struct virtio_net **pdev, struct VhostUserMsg *msg, numa_node); if (dev->guest_pages == NULL) { VHOST_LOG_CONFIG(ERR, - "(%d) failed to allocate memory " - "for dev->guest_pages\n", - dev->vid); + "(%s) failed to allocate memory for dev->guest_pages\n", + dev->ifname); goto close_msg_fds; } } @@ -1391,8 +1390,8 @@ vhost_user_set_mem_table(struct virtio_net **pdev, struct VhostUserMsg *msg, sizeof(struct rte_vhost_mem_region) * memory->nregions, 0, numa_node); if (dev->mem == NULL) { VHOST_LOG_CONFIG(ERR, - "(%d) failed to allocate memory for dev->mem\n", - dev->vid); + "(%s) failed to allocate memory for dev->mem\n", + dev->ifname); goto free_guest_pages; } @@ -1402,25 +1401,28 @@ vhost_user_set_mem_table(struct virtio_net **pdev, struct VhostUserMsg *msg, reg->guest_phys_addr = memory->regions[i].guest_phys_addr; reg->guest_user_addr = memory->regions[i].userspace_addr; reg->size = memory->regions[i].memory_size; - reg->fd = msg->fds[i]; + reg->fd = ctx->fds[i]; /* * Assign invalid file descriptor value to avoid double * closing on error path. */ - msg->fds[i] = -1; + ctx->fds[i] = -1; mmap_offset = memory->regions[i].mmap_offset; if (vhost_user_mmap_region(dev, reg, mmap_offset) < 0) { - VHOST_LOG_CONFIG(ERR, "Failed to mmap region %u\n", i); + VHOST_LOG_CONFIG(ERR, "(%s) failed to mmap region %u\n", dev->ifname, i); goto free_mem_table; } dev->mem->nregions++; } - if (vhost_user_postcopy_register(dev, main_fd, msg) < 0) + if (dev->async_copy && rte_vfio_is_enabled("vfio")) + async_dma_map(dev, true); + + if (vhost_user_postcopy_register(dev, main_fd, ctx) < 0) goto free_mem_table; for (i = 0; i < dev->nr_vring; i++) { @@ -1465,7 +1467,7 @@ free_guest_pages: rte_free(dev->guest_pages); dev->guest_pages = NULL; close_msg_fds: - close_msg_fds(msg); + close_msg_fds(ctx); return RTE_VHOST_MSG_RESULT_ERR; } @@ -1525,13 +1527,12 @@ virtio_is_ready(struct virtio_net *dev) dev->flags |= VIRTIO_DEV_READY; if (!(dev->flags & VIRTIO_DEV_RUNNING)) - VHOST_LOG_CONFIG(INFO, - "virtio is now ready for processing.\n"); + VHOST_LOG_CONFIG(INFO, "(%s) virtio is now ready for processing.\n", dev->ifname); return 1; } static void * -inflight_mem_alloc(const char *name, size_t size, int *fd) +inflight_mem_alloc(struct virtio_net *dev, const char *name, size_t size, int *fd) { void *ptr; int mfd = -1; @@ -1546,8 +1547,8 @@ inflight_mem_alloc(const char *name, size_t size, int *fd) if (mfd == -1) { mfd = mkstemp(fname); if (mfd == -1) { - VHOST_LOG_CONFIG(ERR, - "failed to get inflight buffer fd\n"); + VHOST_LOG_CONFIG(ERR, "(%s) failed to get inflight buffer fd\n", + dev->ifname); return NULL; } @@ -1555,16 +1556,14 @@ inflight_mem_alloc(const char *name, size_t size, int *fd) } if (ftruncate(mfd, size) == -1) { - VHOST_LOG_CONFIG(ERR, - "failed to alloc inflight buffer\n"); + VHOST_LOG_CONFIG(ERR, "(%s) failed to alloc inflight buffer\n", dev->ifname); close(mfd); return NULL; } ptr = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, mfd, 0); if (ptr == MAP_FAILED) { - VHOST_LOG_CONFIG(ERR, - "failed to mmap inflight buffer\n"); + VHOST_LOG_CONFIG(ERR, "(%s) failed to mmap inflight buffer\n", dev->ifname); close(mfd); return NULL; } @@ -1592,7 +1591,7 @@ get_pervq_shm_size_packed(uint16_t queue_size) static int vhost_user_get_inflight_fd(struct virtio_net **pdev, - VhostUserMsg *msg, + struct vhu_msg_context *ctx, int main_fd __rte_unused) { struct rte_vhost_inflight_info_packed *inflight_packed; @@ -1603,10 +1602,12 @@ vhost_user_get_inflight_fd(struct virtio_net **pdev, int numa_node = SOCKET_ID_ANY; void *addr; - if (msg->size != sizeof(msg->payload.inflight)) { - VHOST_LOG_CONFIG(ERR, - "invalid get_inflight_fd message size is %d\n", - msg->size); + if (validate_msg_fds(dev, ctx, 0) != 0) + return RTE_VHOST_MSG_RESULT_ERR; + + if (ctx->msg.size != sizeof(ctx->msg.payload.inflight)) { + VHOST_LOG_CONFIG(ERR, "(%s) invalid get_inflight_fd message size is %d\n", + dev->ifname, ctx->msg.size); return RTE_VHOST_MSG_RESULT_ERR; } @@ -1621,20 +1622,20 @@ vhost_user_get_inflight_fd(struct virtio_net **pdev, dev->inflight_info = rte_zmalloc_socket("inflight_info", sizeof(struct inflight_mem_info), 0, numa_node); if (!dev->inflight_info) { - VHOST_LOG_CONFIG(ERR, - "failed to alloc dev inflight area\n"); + VHOST_LOG_CONFIG(ERR, "(%s) failed to alloc dev inflight area\n", + dev->ifname); return RTE_VHOST_MSG_RESULT_ERR; } dev->inflight_info->fd = -1; } - num_queues = msg->payload.inflight.num_queues; - queue_size = msg->payload.inflight.queue_size; + num_queues = ctx->msg.payload.inflight.num_queues; + queue_size = ctx->msg.payload.inflight.queue_size; - VHOST_LOG_CONFIG(INFO, "get_inflight_fd num_queues: %u\n", - msg->payload.inflight.num_queues); - VHOST_LOG_CONFIG(INFO, "get_inflight_fd queue_size: %u\n", - msg->payload.inflight.queue_size); + VHOST_LOG_CONFIG(INFO, "(%s) get_inflight_fd num_queues: %u\n", + dev->ifname, ctx->msg.payload.inflight.num_queues); + VHOST_LOG_CONFIG(INFO, "(%s) get_inflight_fd queue_size: %u\n", + dev->ifname, ctx->msg.payload.inflight.queue_size); if (vq_is_packed(dev)) pervq_inflight_size = get_pervq_shm_size_packed(queue_size); @@ -1642,11 +1643,10 @@ vhost_user_get_inflight_fd(struct virtio_net **pdev, pervq_inflight_size = get_pervq_shm_size_split(queue_size); mmap_size = num_queues * pervq_inflight_size; - addr = inflight_mem_alloc("vhost-inflight", mmap_size, &fd); + addr = inflight_mem_alloc(dev, "vhost-inflight", mmap_size, &fd); if (!addr) { - VHOST_LOG_CONFIG(ERR, - "failed to alloc vhost inflight area\n"); - msg->payload.inflight.mmap_size = 0; + VHOST_LOG_CONFIG(ERR, "(%s) failed to alloc vhost inflight area\n", dev->ifname); + ctx->msg.payload.inflight.mmap_size = 0; return RTE_VHOST_MSG_RESULT_ERR; } memset(addr, 0, mmap_size); @@ -1662,10 +1662,10 @@ vhost_user_get_inflight_fd(struct virtio_net **pdev, } dev->inflight_info->addr = addr; - dev->inflight_info->size = msg->payload.inflight.mmap_size = mmap_size; - dev->inflight_info->fd = msg->fds[0] = fd; - msg->payload.inflight.mmap_offset = 0; - msg->fd_num = 1; + dev->inflight_info->size = ctx->msg.payload.inflight.mmap_size = mmap_size; + dev->inflight_info->fd = ctx->fds[0] = fd; + ctx->msg.payload.inflight.mmap_offset = 0; + ctx->fd_num = 1; if (vq_is_packed(dev)) { for (i = 0; i < num_queues; i++) { @@ -1679,20 +1679,18 @@ vhost_user_get_inflight_fd(struct virtio_net **pdev, } } - VHOST_LOG_CONFIG(INFO, - "send inflight mmap_size: %"PRIu64"\n", - msg->payload.inflight.mmap_size); - VHOST_LOG_CONFIG(INFO, - "send inflight mmap_offset: %"PRIu64"\n", - msg->payload.inflight.mmap_offset); - VHOST_LOG_CONFIG(INFO, - "send inflight fd: %d\n", msg->fds[0]); + VHOST_LOG_CONFIG(INFO, "(%s) send inflight mmap_size: %"PRIu64"\n", + dev->ifname, ctx->msg.payload.inflight.mmap_size); + VHOST_LOG_CONFIG(INFO, "(%s) send inflight mmap_offset: %"PRIu64"\n", + dev->ifname, ctx->msg.payload.inflight.mmap_offset); + VHOST_LOG_CONFIG(INFO, "(%s) send inflight fd: %d\n", dev->ifname, ctx->fds[0]); return RTE_VHOST_MSG_RESULT_REPLY; } static int -vhost_user_set_inflight_fd(struct virtio_net **pdev, VhostUserMsg *msg, +vhost_user_set_inflight_fd(struct virtio_net **pdev, + struct vhu_msg_context *ctx, int main_fd __rte_unused) { uint64_t mmap_size, mmap_offset; @@ -1704,37 +1702,35 @@ vhost_user_set_inflight_fd(struct virtio_net **pdev, VhostUserMsg *msg, int fd, i; int numa_node = SOCKET_ID_ANY; - fd = msg->fds[0]; - if (msg->size != sizeof(msg->payload.inflight) || fd < 0) { - VHOST_LOG_CONFIG(ERR, - "invalid set_inflight_fd message size is %d,fd is %d\n", - msg->size, fd); + if (validate_msg_fds(dev, ctx, 1) != 0) + return RTE_VHOST_MSG_RESULT_ERR; + + fd = ctx->fds[0]; + if (ctx->msg.size != sizeof(ctx->msg.payload.inflight) || fd < 0) { + VHOST_LOG_CONFIG(ERR, "(%s) invalid set_inflight_fd message size is %d,fd is %d\n", + dev->ifname, ctx->msg.size, fd); return RTE_VHOST_MSG_RESULT_ERR; } - mmap_size = msg->payload.inflight.mmap_size; - mmap_offset = msg->payload.inflight.mmap_offset; - num_queues = msg->payload.inflight.num_queues; - queue_size = msg->payload.inflight.queue_size; + mmap_size = ctx->msg.payload.inflight.mmap_size; + mmap_offset = ctx->msg.payload.inflight.mmap_offset; + num_queues = ctx->msg.payload.inflight.num_queues; + queue_size = ctx->msg.payload.inflight.queue_size; if (vq_is_packed(dev)) pervq_inflight_size = get_pervq_shm_size_packed(queue_size); else pervq_inflight_size = get_pervq_shm_size_split(queue_size); - VHOST_LOG_CONFIG(INFO, - "set_inflight_fd mmap_size: %"PRIu64"\n", mmap_size); - VHOST_LOG_CONFIG(INFO, - "set_inflight_fd mmap_offset: %"PRIu64"\n", mmap_offset); - VHOST_LOG_CONFIG(INFO, - "set_inflight_fd num_queues: %u\n", num_queues); - VHOST_LOG_CONFIG(INFO, - "set_inflight_fd queue_size: %u\n", queue_size); - VHOST_LOG_CONFIG(INFO, - "set_inflight_fd fd: %d\n", fd); - VHOST_LOG_CONFIG(INFO, - "set_inflight_fd pervq_inflight_size: %d\n", - pervq_inflight_size); + VHOST_LOG_CONFIG(INFO, "(%s) set_inflight_fd mmap_size: %"PRIu64"\n", + dev->ifname, mmap_size); + VHOST_LOG_CONFIG(INFO, "(%s) set_inflight_fd mmap_offset: %"PRIu64"\n", + dev->ifname, mmap_offset); + VHOST_LOG_CONFIG(INFO, "(%s) set_inflight_fd num_queues: %u\n", dev->ifname, num_queues); + VHOST_LOG_CONFIG(INFO, "(%s) set_inflight_fd queue_size: %u\n", dev->ifname, queue_size); + VHOST_LOG_CONFIG(INFO, "(%s) set_inflight_fd fd: %d\n", dev->ifname, fd); + VHOST_LOG_CONFIG(INFO, "(%s) set_inflight_fd pervq_inflight_size: %d\n", + dev->ifname, pervq_inflight_size); /* * If VQ 0 has already been allocated, try to allocate on the same @@ -1747,8 +1743,8 @@ vhost_user_set_inflight_fd(struct virtio_net **pdev, VhostUserMsg *msg, dev->inflight_info = rte_zmalloc_socket("inflight_info", sizeof(struct inflight_mem_info), 0, numa_node); if (dev->inflight_info == NULL) { - VHOST_LOG_CONFIG(ERR, - "failed to alloc dev inflight area\n"); + VHOST_LOG_CONFIG(ERR, "(%s) failed to alloc dev inflight area\n", + dev->ifname); return RTE_VHOST_MSG_RESULT_ERR; } dev->inflight_info->fd = -1; @@ -1762,7 +1758,7 @@ vhost_user_set_inflight_fd(struct virtio_net **pdev, VhostUserMsg *msg, addr = mmap(0, mmap_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, mmap_offset); if (addr == MAP_FAILED) { - VHOST_LOG_CONFIG(ERR, "failed to mmap share memory.\n"); + VHOST_LOG_CONFIG(ERR, "(%s) failed to mmap share memory.\n", dev->ifname); return RTE_VHOST_MSG_RESULT_ERR; } @@ -1794,7 +1790,8 @@ vhost_user_set_inflight_fd(struct virtio_net **pdev, VhostUserMsg *msg, } static int -vhost_user_set_vring_call(struct virtio_net **pdev, struct VhostUserMsg *msg, +vhost_user_set_vring_call(struct virtio_net **pdev, + struct vhu_msg_context *ctx, int main_fd __rte_unused) { struct virtio_net *dev = *pdev; @@ -1802,17 +1799,17 @@ vhost_user_set_vring_call(struct virtio_net **pdev, struct VhostUserMsg *msg, struct vhost_virtqueue *vq; int expected_fds; - expected_fds = (msg->payload.u64 & VHOST_USER_VRING_NOFD_MASK) ? 0 : 1; - if (validate_msg_fds(msg, expected_fds) != 0) + expected_fds = (ctx->msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK) ? 0 : 1; + if (validate_msg_fds(dev, ctx, expected_fds) != 0) return RTE_VHOST_MSG_RESULT_ERR; - file.index = msg->payload.u64 & VHOST_USER_VRING_IDX_MASK; - if (msg->payload.u64 & VHOST_USER_VRING_NOFD_MASK) + file.index = ctx->msg.payload.u64 & VHOST_USER_VRING_IDX_MASK; + if (ctx->msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK) file.fd = VIRTIO_INVALID_EVENTFD; else - file.fd = msg->fds[0]; - VHOST_LOG_CONFIG(INFO, - "vring call idx:%d file:%d\n", file.index, file.fd); + file.fd = ctx->fds[0]; + VHOST_LOG_CONFIG(INFO, "(%s) vring call idx:%d file:%d\n", + dev->ifname, file.index, file.fd); vq = dev->virtqueue[file.index]; @@ -1829,19 +1826,20 @@ vhost_user_set_vring_call(struct virtio_net **pdev, struct VhostUserMsg *msg, return RTE_VHOST_MSG_RESULT_OK; } -static int vhost_user_set_vring_err(struct virtio_net **pdev __rte_unused, - struct VhostUserMsg *msg, +static int vhost_user_set_vring_err(struct virtio_net **pdev, + struct vhu_msg_context *ctx, int main_fd __rte_unused) { + struct virtio_net *dev = *pdev; int expected_fds; - expected_fds = (msg->payload.u64 & VHOST_USER_VRING_NOFD_MASK) ? 0 : 1; - if (validate_msg_fds(msg, expected_fds) != 0) + expected_fds = (ctx->msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK) ? 0 : 1; + if (validate_msg_fds(dev, ctx, expected_fds) != 0) return RTE_VHOST_MSG_RESULT_ERR; - if (!(msg->payload.u64 & VHOST_USER_VRING_NOFD_MASK)) - close(msg->fds[0]); - VHOST_LOG_CONFIG(INFO, "not implemented\n"); + if (!(ctx->msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK)) + close(ctx->fds[0]); + VHOST_LOG_CONFIG(INFO, "(%s) not implemented\n", dev->ifname); return RTE_VHOST_MSG_RESULT_OK; } @@ -1908,7 +1906,8 @@ vhost_check_queue_inflights_split(struct virtio_net *dev, 0, vq->numa_node); if (!resubmit) { VHOST_LOG_CONFIG(ERR, - "failed to allocate memory for resubmit info.\n"); + "(%s) failed to allocate memory for resubmit info.\n", + dev->ifname); return RTE_VHOST_MSG_RESULT_ERR; } @@ -1917,7 +1916,8 @@ vhost_check_queue_inflights_split(struct virtio_net *dev, 0, vq->numa_node); if (!resubmit->resubmit_list) { VHOST_LOG_CONFIG(ERR, - "failed to allocate memory for inflight desc.\n"); + "(%s) failed to allocate memory for inflight desc.\n", + dev->ifname); rte_free(resubmit); return RTE_VHOST_MSG_RESULT_ERR; } @@ -2004,7 +2004,8 @@ vhost_check_queue_inflights_packed(struct virtio_net *dev, 0, vq->numa_node); if (resubmit == NULL) { VHOST_LOG_CONFIG(ERR, - "failed to allocate memory for resubmit info.\n"); + "(%s) failed to allocate memory for resubmit info.\n", + dev->ifname); return RTE_VHOST_MSG_RESULT_ERR; } @@ -2013,7 +2014,8 @@ vhost_check_queue_inflights_packed(struct virtio_net *dev, 0, vq->numa_node); if (resubmit->resubmit_list == NULL) { VHOST_LOG_CONFIG(ERR, - "failed to allocate memory for resubmit desc.\n"); + "(%s) failed to allocate memory for resubmit desc.\n", + dev->ifname); rte_free(resubmit); return RTE_VHOST_MSG_RESULT_ERR; } @@ -2042,7 +2044,8 @@ vhost_check_queue_inflights_packed(struct virtio_net *dev, } static int -vhost_user_set_vring_kick(struct virtio_net **pdev, struct VhostUserMsg *msg, +vhost_user_set_vring_kick(struct virtio_net **pdev, + struct vhu_msg_context *ctx, int main_fd __rte_unused) { struct virtio_net *dev = *pdev; @@ -2050,17 +2053,17 @@ vhost_user_set_vring_kick(struct virtio_net **pdev, struct VhostUserMsg *msg, struct vhost_virtqueue *vq; int expected_fds; - expected_fds = (msg->payload.u64 & VHOST_USER_VRING_NOFD_MASK) ? 0 : 1; - if (validate_msg_fds(msg, expected_fds) != 0) + expected_fds = (ctx->msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK) ? 0 : 1; + if (validate_msg_fds(dev, ctx, expected_fds) != 0) return RTE_VHOST_MSG_RESULT_ERR; - file.index = msg->payload.u64 & VHOST_USER_VRING_IDX_MASK; - if (msg->payload.u64 & VHOST_USER_VRING_NOFD_MASK) + file.index = ctx->msg.payload.u64 & VHOST_USER_VRING_IDX_MASK; + if (ctx->msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK) file.fd = VIRTIO_INVALID_EVENTFD; else - file.fd = msg->fds[0]; - VHOST_LOG_CONFIG(INFO, - "vring kick idx:%d file:%d\n", file.index, file.fd); + file.fd = ctx->fds[0]; + VHOST_LOG_CONFIG(INFO, "(%s) vring kick idx:%d file:%d\n", + dev->ifname, file.index, file.fd); /* Interpret ring addresses only when ring is started. */ dev = translate_ring_addresses(dev, file.index); @@ -2095,14 +2098,14 @@ vhost_user_set_vring_kick(struct virtio_net **pdev, struct VhostUserMsg *msg, if (vq_is_packed(dev)) { if (vhost_check_queue_inflights_packed(dev, vq)) { - VHOST_LOG_CONFIG(ERR, - "failed to inflights for vq: %d\n", file.index); + VHOST_LOG_CONFIG(ERR, "(%s) failed to inflights for vq: %d\n", + dev->ifname, file.index); return RTE_VHOST_MSG_RESULT_ERR; } } else { if (vhost_check_queue_inflights_split(dev, vq)) { - VHOST_LOG_CONFIG(ERR, - "failed to inflights for vq: %d\n", file.index); + VHOST_LOG_CONFIG(ERR, "(%s) failed to inflights for vq: %d\n", + dev->ifname, file.index); return RTE_VHOST_MSG_RESULT_ERR; } } @@ -2115,14 +2118,14 @@ vhost_user_set_vring_kick(struct virtio_net **pdev, struct VhostUserMsg *msg, */ static int vhost_user_get_vring_base(struct virtio_net **pdev, - struct VhostUserMsg *msg, + struct vhu_msg_context *ctx, int main_fd __rte_unused) { struct virtio_net *dev = *pdev; - struct vhost_virtqueue *vq = dev->virtqueue[msg->payload.state.index]; + struct vhost_virtqueue *vq = dev->virtqueue[ctx->msg.payload.state.index]; uint64_t val; - if (validate_msg_fds(msg, 0) != 0) + if (validate_msg_fds(dev, ctx, 0) != 0) return RTE_VHOST_MSG_RESULT_ERR; /* We have to stop the queue (virtio) if it is running. */ @@ -2139,14 +2142,14 @@ vhost_user_get_vring_base(struct virtio_net **pdev, */ val = vq->last_avail_idx & 0x7fff; val |= vq->avail_wrap_counter << 15; - msg->payload.state.num = val; + ctx->msg.payload.state.num = val; } else { - msg->payload.state.num = vq->last_avail_idx; + ctx->msg.payload.state.num = vq->last_avail_idx; } - VHOST_LOG_CONFIG(INFO, - "vring base idx:%d file:%d\n", msg->payload.state.index, - msg->payload.state.num); + VHOST_LOG_CONFIG(INFO, "(%s) vring base idx:%d file:%d\n", + dev->ifname, ctx->msg.payload.state.index, + ctx->msg.payload.state.num); /* * Based on current qemu vhost-user implementation, this message is * sent and only sent in vhost_vring_stop. @@ -2178,8 +2181,8 @@ vhost_user_get_vring_base(struct virtio_net **pdev, rte_free(vq->log_cache); vq->log_cache = NULL; - msg->size = sizeof(msg->payload.state); - msg->fd_num = 0; + ctx->msg.size = sizeof(ctx->msg.payload.state); + ctx->fd_num = 0; vhost_user_iotlb_flush_all(vq); @@ -2194,24 +2197,24 @@ vhost_user_get_vring_base(struct virtio_net **pdev, */ static int vhost_user_set_vring_enable(struct virtio_net **pdev, - struct VhostUserMsg *msg, + struct vhu_msg_context *ctx, int main_fd __rte_unused) { struct virtio_net *dev = *pdev; - bool enable = !!msg->payload.state.num; - int index = (int)msg->payload.state.index; + bool enable = !!ctx->msg.payload.state.num; + int index = (int)ctx->msg.payload.state.index; - if (validate_msg_fds(msg, 0) != 0) + if (validate_msg_fds(dev, ctx, 0) != 0) return RTE_VHOST_MSG_RESULT_ERR; - VHOST_LOG_CONFIG(INFO, - "set queue enable: %d to qp idx: %d\n", - enable, index); + VHOST_LOG_CONFIG(INFO, "(%s) set queue enable: %d to qp idx: %d\n", + dev->ifname, enable, index); - if (enable && dev->virtqueue[index]->async_registered) { - if (dev->virtqueue[index]->async_pkts_inflight_n) { - VHOST_LOG_CONFIG(ERR, "failed to enable vring. " - "async inflight packets must be completed first\n"); + if (enable && dev->virtqueue[index]->async) { + if (dev->virtqueue[index]->async->pkts_inflight_n) { + VHOST_LOG_CONFIG(ERR, + "(%s) failed to enable vring. Inflight packets must be completed first\n", + dev->ifname); return RTE_VHOST_MSG_RESULT_ERR; } } @@ -2223,93 +2226,89 @@ vhost_user_set_vring_enable(struct virtio_net **pdev, static int vhost_user_get_protocol_features(struct virtio_net **pdev, - struct VhostUserMsg *msg, + struct vhu_msg_context *ctx, int main_fd __rte_unused) { struct virtio_net *dev = *pdev; uint64_t features, protocol_features; - if (validate_msg_fds(msg, 0) != 0) + if (validate_msg_fds(dev, ctx, 0) != 0) return RTE_VHOST_MSG_RESULT_ERR; rte_vhost_driver_get_features(dev->ifname, &features); rte_vhost_driver_get_protocol_features(dev->ifname, &protocol_features); - msg->payload.u64 = protocol_features; - msg->size = sizeof(msg->payload.u64); - msg->fd_num = 0; + ctx->msg.payload.u64 = protocol_features; + ctx->msg.size = sizeof(ctx->msg.payload.u64); + ctx->fd_num = 0; return RTE_VHOST_MSG_RESULT_REPLY; } static int vhost_user_set_protocol_features(struct virtio_net **pdev, - struct VhostUserMsg *msg, + struct vhu_msg_context *ctx, int main_fd __rte_unused) { struct virtio_net *dev = *pdev; - uint64_t protocol_features = msg->payload.u64; + uint64_t protocol_features = ctx->msg.payload.u64; uint64_t slave_protocol_features = 0; - if (validate_msg_fds(msg, 0) != 0) + if (validate_msg_fds(dev, ctx, 0) != 0) return RTE_VHOST_MSG_RESULT_ERR; rte_vhost_driver_get_protocol_features(dev->ifname, &slave_protocol_features); if (protocol_features & ~slave_protocol_features) { - VHOST_LOG_CONFIG(ERR, - "(%d) received invalid protocol features.\n", - dev->vid); + VHOST_LOG_CONFIG(ERR, "(%s) received invalid protocol features.\n", dev->ifname); return RTE_VHOST_MSG_RESULT_ERR; } dev->protocol_features = protocol_features; - VHOST_LOG_CONFIG(INFO, - "negotiated Vhost-user protocol features: 0x%" PRIx64 "\n", - dev->protocol_features); + VHOST_LOG_CONFIG(INFO, "(%s) negotiated Vhost-user protocol features: 0x%" PRIx64 "\n", + dev->ifname, dev->protocol_features); return RTE_VHOST_MSG_RESULT_OK; } static int -vhost_user_set_log_base(struct virtio_net **pdev, struct VhostUserMsg *msg, +vhost_user_set_log_base(struct virtio_net **pdev, + struct vhu_msg_context *ctx, int main_fd __rte_unused) { struct virtio_net *dev = *pdev; - int fd = msg->fds[0]; + int fd = ctx->fds[0]; uint64_t size, off; void *addr; uint32_t i; - if (validate_msg_fds(msg, 1) != 0) + if (validate_msg_fds(dev, ctx, 1) != 0) return RTE_VHOST_MSG_RESULT_ERR; if (fd < 0) { - VHOST_LOG_CONFIG(ERR, "invalid log fd: %d\n", fd); + VHOST_LOG_CONFIG(ERR, "(%s) invalid log fd: %d\n", dev->ifname, fd); return RTE_VHOST_MSG_RESULT_ERR; } - if (msg->size != sizeof(VhostUserLog)) { - VHOST_LOG_CONFIG(ERR, - "invalid log base msg size: %"PRId32" != %d\n", - msg->size, (int)sizeof(VhostUserLog)); + if (ctx->msg.size != sizeof(VhostUserLog)) { + VHOST_LOG_CONFIG(ERR, "(%s) invalid log base msg size: %"PRId32" != %d\n", + dev->ifname, ctx->msg.size, (int)sizeof(VhostUserLog)); goto close_msg_fds; } - size = msg->payload.log.mmap_size; - off = msg->payload.log.mmap_offset; + size = ctx->msg.payload.log.mmap_size; + off = ctx->msg.payload.log.mmap_offset; /* Check for mmap size and offset overflow. */ if (off >= -size) { VHOST_LOG_CONFIG(ERR, - "log offset %#"PRIx64" and log size %#"PRIx64" overflow\n", - off, size); + "(%s) log offset %#"PRIx64" and log size %#"PRIx64" overflow\n", + dev->ifname, off, size); goto close_msg_fds; } - VHOST_LOG_CONFIG(INFO, - "log mmap size: %"PRId64", offset: %"PRId64"\n", - size, off); + VHOST_LOG_CONFIG(INFO, "(%s) log mmap size: %"PRId64", offset: %"PRId64"\n", + dev->ifname, size, off); /* * mmap from 0 to workaround a hugepage mmap bug: mmap will @@ -2318,7 +2317,7 @@ vhost_user_set_log_base(struct virtio_net **pdev, struct VhostUserMsg *msg, addr = mmap(0, size + off, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); close(fd); if (addr == MAP_FAILED) { - VHOST_LOG_CONFIG(ERR, "mmap log base failed!\n"); + VHOST_LOG_CONFIG(ERR, "(%s) mmap log base failed!\n", dev->ifname); return RTE_VHOST_MSG_RESULT_ERR; } @@ -2347,32 +2346,35 @@ vhost_user_set_log_base(struct virtio_net **pdev, struct VhostUserMsg *msg, * caching will be done, which will impact performance */ if (!vq->log_cache) - VHOST_LOG_CONFIG(ERR, "Failed to allocate VQ logging cache\n"); + VHOST_LOG_CONFIG(ERR, "(%s) failed to allocate VQ logging cache\n", + dev->ifname); } /* * The spec is not clear about it (yet), but QEMU doesn't expect * any payload in the reply. */ - msg->size = 0; - msg->fd_num = 0; + ctx->msg.size = 0; + ctx->fd_num = 0; return RTE_VHOST_MSG_RESULT_REPLY; close_msg_fds: - close_msg_fds(msg); + close_msg_fds(ctx); return RTE_VHOST_MSG_RESULT_ERR; } -static int vhost_user_set_log_fd(struct virtio_net **pdev __rte_unused, - struct VhostUserMsg *msg, +static int vhost_user_set_log_fd(struct virtio_net **pdev, + struct vhu_msg_context *ctx, int main_fd __rte_unused) { - if (validate_msg_fds(msg, 1) != 0) + struct virtio_net *dev = *pdev; + + if (validate_msg_fds(dev, ctx, 1) != 0) return RTE_VHOST_MSG_RESULT_ERR; - close(msg->fds[0]); - VHOST_LOG_CONFIG(INFO, "not implemented.\n"); + close(ctx->fds[0]); + VHOST_LOG_CONFIG(INFO, "(%s) not implemented.\n", dev->ifname); return RTE_VHOST_MSG_RESULT_OK; } @@ -2386,19 +2388,19 @@ static int vhost_user_set_log_fd(struct virtio_net **pdev __rte_unused, * a flag 'broadcast_rarp' to let rte_vhost_dequeue_burst() inject it. */ static int -vhost_user_send_rarp(struct virtio_net **pdev, struct VhostUserMsg *msg, +vhost_user_send_rarp(struct virtio_net **pdev, + struct vhu_msg_context *ctx, int main_fd __rte_unused) { struct virtio_net *dev = *pdev; - uint8_t *mac = (uint8_t *)&msg->payload.u64; + uint8_t *mac = (uint8_t *)&ctx->msg.payload.u64; struct rte_vdpa_device *vdpa_dev; - if (validate_msg_fds(msg, 0) != 0) + if (validate_msg_fds(dev, ctx, 0) != 0) return RTE_VHOST_MSG_RESULT_ERR; - VHOST_LOG_CONFIG(DEBUG, - ":: mac: " RTE_ETHER_ADDR_PRT_FMT "\n", - mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); + VHOST_LOG_CONFIG(DEBUG, "(%s) MAC: " RTE_ETHER_ADDR_PRT_FMT "\n", + dev->ifname, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); memcpy(dev->mac.addr_bytes, mac, 6); /* @@ -2417,41 +2419,42 @@ vhost_user_send_rarp(struct virtio_net **pdev, struct VhostUserMsg *msg, } static int -vhost_user_net_set_mtu(struct virtio_net **pdev, struct VhostUserMsg *msg, +vhost_user_net_set_mtu(struct virtio_net **pdev, + struct vhu_msg_context *ctx, int main_fd __rte_unused) { struct virtio_net *dev = *pdev; - if (validate_msg_fds(msg, 0) != 0) + if (validate_msg_fds(dev, ctx, 0) != 0) return RTE_VHOST_MSG_RESULT_ERR; - if (msg->payload.u64 < VIRTIO_MIN_MTU || - msg->payload.u64 > VIRTIO_MAX_MTU) { - VHOST_LOG_CONFIG(ERR, "Invalid MTU size (%"PRIu64")\n", - msg->payload.u64); + if (ctx->msg.payload.u64 < VIRTIO_MIN_MTU || + ctx->msg.payload.u64 > VIRTIO_MAX_MTU) { + VHOST_LOG_CONFIG(ERR, "(%s) invalid MTU size (%"PRIu64")\n", + dev->ifname, ctx->msg.payload.u64); return RTE_VHOST_MSG_RESULT_ERR; } - dev->mtu = msg->payload.u64; + dev->mtu = ctx->msg.payload.u64; return RTE_VHOST_MSG_RESULT_OK; } static int -vhost_user_set_req_fd(struct virtio_net **pdev, struct VhostUserMsg *msg, +vhost_user_set_req_fd(struct virtio_net **pdev, + struct vhu_msg_context *ctx, int main_fd __rte_unused) { struct virtio_net *dev = *pdev; - int fd = msg->fds[0]; + int fd = ctx->fds[0]; - if (validate_msg_fds(msg, 1) != 0) + if (validate_msg_fds(dev, ctx, 1) != 0) return RTE_VHOST_MSG_RESULT_ERR; if (fd < 0) { - VHOST_LOG_CONFIG(ERR, - "Invalid file descriptor for slave channel (%d)\n", - fd); + VHOST_LOG_CONFIG(ERR, "(%s) invalid file descriptor for slave channel (%d)\n", + dev->ifname, fd); return RTE_VHOST_MSG_RESULT_ERR; } @@ -2539,15 +2542,16 @@ static int is_vring_iotlb(struct virtio_net *dev, } static int -vhost_user_iotlb_msg(struct virtio_net **pdev, struct VhostUserMsg *msg, +vhost_user_iotlb_msg(struct virtio_net **pdev, + struct vhu_msg_context *ctx, int main_fd __rte_unused) { struct virtio_net *dev = *pdev; - struct vhost_iotlb_msg *imsg = &msg->payload.iotlb; + struct vhost_iotlb_msg *imsg = &ctx->msg.payload.iotlb; uint16_t i; uint64_t vva, len; - if (validate_msg_fds(msg, 0) != 0) + if (validate_msg_fds(dev, ctx, 0) != 0) return RTE_VHOST_MSG_RESULT_ERR; switch (imsg->type) { @@ -2563,11 +2567,14 @@ vhost_user_iotlb_msg(struct virtio_net **pdev, struct VhostUserMsg *msg, if (!vq) continue; - vhost_user_iotlb_cache_insert(vq, imsg->iova, vva, + vhost_user_iotlb_cache_insert(dev, vq, imsg->iova, vva, len, imsg->perm); - if (is_vring_iotlb(dev, vq, imsg)) + if (is_vring_iotlb(dev, vq, imsg)) { + rte_spinlock_lock(&vq->access_lock); *pdev = dev = translate_ring_addresses(dev, i); + rte_spinlock_unlock(&vq->access_lock); + } } break; case VHOST_IOTLB_INVALIDATE: @@ -2580,13 +2587,16 @@ vhost_user_iotlb_msg(struct virtio_net **pdev, struct VhostUserMsg *msg, vhost_user_iotlb_cache_remove(vq, imsg->iova, imsg->size); - if (is_vring_iotlb(dev, vq, imsg)) + if (is_vring_iotlb(dev, vq, imsg)) { + rte_spinlock_lock(&vq->access_lock); vring_invalidate(dev, vq); + rte_spinlock_unlock(&vq->access_lock); + } } break; default: - VHOST_LOG_CONFIG(ERR, "Invalid IOTLB message type (%d)\n", - imsg->type); + VHOST_LOG_CONFIG(ERR, "(%s) invalid IOTLB message type (%d)\n", + dev->ifname, imsg->type); return RTE_VHOST_MSG_RESULT_ERR; } @@ -2595,39 +2605,39 @@ vhost_user_iotlb_msg(struct virtio_net **pdev, struct VhostUserMsg *msg, static int vhost_user_set_postcopy_advise(struct virtio_net **pdev, - struct VhostUserMsg *msg, + struct vhu_msg_context *ctx, int main_fd __rte_unused) { struct virtio_net *dev = *pdev; #ifdef RTE_LIBRTE_VHOST_POSTCOPY struct uffdio_api api_struct; - if (validate_msg_fds(msg, 0) != 0) + if (validate_msg_fds(dev, ctx, 0) != 0) return RTE_VHOST_MSG_RESULT_ERR; dev->postcopy_ufd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK); if (dev->postcopy_ufd == -1) { - VHOST_LOG_CONFIG(ERR, "Userfaultfd not available: %s\n", - strerror(errno)); + VHOST_LOG_CONFIG(ERR, "(%s) userfaultfd not available: %s\n", + dev->ifname, strerror(errno)); return RTE_VHOST_MSG_RESULT_ERR; } api_struct.api = UFFD_API; api_struct.features = 0; if (ioctl(dev->postcopy_ufd, UFFDIO_API, &api_struct)) { - VHOST_LOG_CONFIG(ERR, "UFFDIO_API ioctl failure: %s\n", - strerror(errno)); + VHOST_LOG_CONFIG(ERR, "(%s) UFFDIO_API ioctl failure: %s\n", + dev->ifname, strerror(errno)); close(dev->postcopy_ufd); dev->postcopy_ufd = -1; return RTE_VHOST_MSG_RESULT_ERR; } - msg->fds[0] = dev->postcopy_ufd; - msg->fd_num = 1; + ctx->fds[0] = dev->postcopy_ufd; + ctx->fd_num = 1; return RTE_VHOST_MSG_RESULT_REPLY; #else dev->postcopy_ufd = -1; - msg->fd_num = 0; + ctx->fd_num = 0; return RTE_VHOST_MSG_RESULT_ERR; #endif @@ -2635,17 +2645,17 @@ vhost_user_set_postcopy_advise(struct virtio_net **pdev, static int vhost_user_set_postcopy_listen(struct virtio_net **pdev, - struct VhostUserMsg *msg __rte_unused, + struct vhu_msg_context *ctx __rte_unused, int main_fd __rte_unused) { struct virtio_net *dev = *pdev; - if (validate_msg_fds(msg, 0) != 0) + if (validate_msg_fds(dev, ctx, 0) != 0) return RTE_VHOST_MSG_RESULT_ERR; if (dev->mem && dev->mem->nregions) { - VHOST_LOG_CONFIG(ERR, - "Regions already registered at postcopy-listen\n"); + VHOST_LOG_CONFIG(ERR, "(%s) regions already registered at postcopy-listen\n", + dev->ifname); return RTE_VHOST_MSG_RESULT_ERR; } dev->postcopy_listening = 1; @@ -2654,12 +2664,13 @@ vhost_user_set_postcopy_listen(struct virtio_net **pdev, } static int -vhost_user_postcopy_end(struct virtio_net **pdev, struct VhostUserMsg *msg, +vhost_user_postcopy_end(struct virtio_net **pdev, + struct vhu_msg_context *ctx, int main_fd __rte_unused) { struct virtio_net *dev = *pdev; - if (validate_msg_fds(msg, 0) != 0) + if (validate_msg_fds(dev, ctx, 0) != 0) return RTE_VHOST_MSG_RESULT_ERR; dev->postcopy_listening = 0; @@ -2668,50 +2679,54 @@ vhost_user_postcopy_end(struct virtio_net **pdev, struct VhostUserMsg *msg, dev->postcopy_ufd = -1; } - msg->payload.u64 = 0; - msg->size = sizeof(msg->payload.u64); - msg->fd_num = 0; + ctx->msg.payload.u64 = 0; + ctx->msg.size = sizeof(ctx->msg.payload.u64); + ctx->fd_num = 0; return RTE_VHOST_MSG_RESULT_REPLY; } static int -vhost_user_get_status(struct virtio_net **pdev, struct VhostUserMsg *msg, +vhost_user_get_status(struct virtio_net **pdev, + struct vhu_msg_context *ctx, int main_fd __rte_unused) { struct virtio_net *dev = *pdev; - if (validate_msg_fds(msg, 0) != 0) + if (validate_msg_fds(dev, ctx, 0) != 0) return RTE_VHOST_MSG_RESULT_ERR; - msg->payload.u64 = dev->status; - msg->size = sizeof(msg->payload.u64); - msg->fd_num = 0; + ctx->msg.payload.u64 = dev->status; + ctx->msg.size = sizeof(ctx->msg.payload.u64); + ctx->fd_num = 0; return RTE_VHOST_MSG_RESULT_REPLY; } static int -vhost_user_set_status(struct virtio_net **pdev, struct VhostUserMsg *msg, +vhost_user_set_status(struct virtio_net **pdev, + struct vhu_msg_context *ctx, int main_fd __rte_unused) { struct virtio_net *dev = *pdev; - if (validate_msg_fds(msg, 0) != 0) + if (validate_msg_fds(dev, ctx, 0) != 0) return RTE_VHOST_MSG_RESULT_ERR; /* As per Virtio specification, the device status is 8bits long */ - if (msg->payload.u64 > UINT8_MAX) { - VHOST_LOG_CONFIG(ERR, "Invalid VHOST_USER_SET_STATUS payload 0x%" PRIx64 "\n", - msg->payload.u64); + if (ctx->msg.payload.u64 > UINT8_MAX) { + VHOST_LOG_CONFIG(ERR, "(%s) invalid VHOST_USER_SET_STATUS payload 0x%" PRIx64 "\n", + dev->ifname, ctx->msg.payload.u64); return RTE_VHOST_MSG_RESULT_ERR; } - dev->status = msg->payload.u64; + dev->status = ctx->msg.payload.u64; if ((dev->status & VIRTIO_DEVICE_STATUS_FEATURES_OK) && (dev->flags & VIRTIO_DEV_FEATURES_FAILED)) { - VHOST_LOG_CONFIG(ERR, "FEATURES_OK bit is set but feature negotiation failed\n"); + VHOST_LOG_CONFIG(ERR, + "(%s) FEATURES_OK bit is set but feature negotiation failed\n", + dev->ifname); /* * Clear the bit to let the driver know about the feature * negotiation failure @@ -2719,29 +2734,30 @@ vhost_user_set_status(struct virtio_net **pdev, struct VhostUserMsg *msg, dev->status &= ~VIRTIO_DEVICE_STATUS_FEATURES_OK; } - VHOST_LOG_CONFIG(INFO, "New device status(0x%08x):\n" - "\t-RESET: %u\n" - "\t-ACKNOWLEDGE: %u\n" - "\t-DRIVER: %u\n" - "\t-FEATURES_OK: %u\n" - "\t-DRIVER_OK: %u\n" - "\t-DEVICE_NEED_RESET: %u\n" - "\t-FAILED: %u\n", - dev->status, - (dev->status == VIRTIO_DEVICE_STATUS_RESET), - !!(dev->status & VIRTIO_DEVICE_STATUS_ACK), - !!(dev->status & VIRTIO_DEVICE_STATUS_DRIVER), - !!(dev->status & VIRTIO_DEVICE_STATUS_FEATURES_OK), - !!(dev->status & VIRTIO_DEVICE_STATUS_DRIVER_OK), - !!(dev->status & VIRTIO_DEVICE_STATUS_DEV_NEED_RESET), + VHOST_LOG_CONFIG(INFO, "(%s) new device status(0x%08x):\n", dev->ifname, + dev->status); + VHOST_LOG_CONFIG(INFO, "(%s)\t-RESET: %u\n", dev->ifname, + (dev->status == VIRTIO_DEVICE_STATUS_RESET)); + VHOST_LOG_CONFIG(INFO, "(%s)\t-ACKNOWLEDGE: %u\n", dev->ifname, + !!(dev->status & VIRTIO_DEVICE_STATUS_ACK)); + VHOST_LOG_CONFIG(INFO, "(%s)\t-DRIVER: %u\n", dev->ifname, + !!(dev->status & VIRTIO_DEVICE_STATUS_DRIVER)); + VHOST_LOG_CONFIG(INFO, "(%s)\t-FEATURES_OK: %u\n", dev->ifname, + !!(dev->status & VIRTIO_DEVICE_STATUS_FEATURES_OK)); + VHOST_LOG_CONFIG(INFO, "(%s)\t-DRIVER_OK: %u\n", dev->ifname, + !!(dev->status & VIRTIO_DEVICE_STATUS_DRIVER_OK)); + VHOST_LOG_CONFIG(INFO, "(%s)\t-DEVICE_NEED_RESET: %u\n", dev->ifname, + !!(dev->status & VIRTIO_DEVICE_STATUS_DEV_NEED_RESET)); + VHOST_LOG_CONFIG(INFO, "(%s)\t-FAILED: %u\n", dev->ifname, !!(dev->status & VIRTIO_DEVICE_STATUS_FAILED)); return RTE_VHOST_MSG_RESULT_OK; } typedef int (*vhost_message_handler_t)(struct virtio_net **pdev, - struct VhostUserMsg *msg, + struct vhu_msg_context *ctx, int main_fd); + static vhost_message_handler_t vhost_message_handlers[VHOST_USER_MAX] = { [VHOST_USER_NONE] = NULL, [VHOST_USER_GET_FEATURES] = vhost_user_get_features, @@ -2777,32 +2793,31 @@ static vhost_message_handler_t vhost_message_handlers[VHOST_USER_MAX] = { /* return bytes# of read on success or negative val on failure. */ static int -read_vhost_message(int sockfd, struct VhostUserMsg *msg) +read_vhost_message(struct virtio_net *dev, int sockfd, struct vhu_msg_context *ctx) { int ret; - ret = read_fd_message(sockfd, (char *)msg, VHOST_USER_HDR_SIZE, - msg->fds, VHOST_MEMORY_MAX_NREGIONS, &msg->fd_num); + ret = read_fd_message(dev->ifname, sockfd, (char *)&ctx->msg, VHOST_USER_HDR_SIZE, + ctx->fds, VHOST_MEMORY_MAX_NREGIONS, &ctx->fd_num); if (ret <= 0) { return ret; } else if (ret != VHOST_USER_HDR_SIZE) { - VHOST_LOG_CONFIG(ERR, "Unexpected header size read\n"); - close_msg_fds(msg); + VHOST_LOG_CONFIG(ERR, "(%s) Unexpected header size read\n", dev->ifname); + close_msg_fds(ctx); return -1; } - if (msg->size) { - if (msg->size > sizeof(msg->payload)) { - VHOST_LOG_CONFIG(ERR, - "invalid msg size: %d\n", msg->size); + if (ctx->msg.size) { + if (ctx->msg.size > sizeof(ctx->msg.payload)) { + VHOST_LOG_CONFIG(ERR, "(%s) invalid msg size: %d\n", + dev->ifname, ctx->msg.size); return -1; } - ret = read(sockfd, &msg->payload, msg->size); + ret = read(sockfd, &ctx->msg.payload, ctx->msg.size); if (ret <= 0) return ret; - if (ret != (int)msg->size) { - VHOST_LOG_CONFIG(ERR, - "read control message failed\n"); + if (ret != (int)ctx->msg.size) { + VHOST_LOG_CONFIG(ERR, "(%s) read control message failed\n", dev->ifname); return -1; } } @@ -2811,39 +2826,40 @@ read_vhost_message(int sockfd, struct VhostUserMsg *msg) } static int -send_vhost_message(int sockfd, struct VhostUserMsg *msg) +send_vhost_message(struct virtio_net *dev, int sockfd, struct vhu_msg_context *ctx) { - if (!msg) + if (!ctx) return 0; - return send_fd_message(sockfd, (char *)msg, - VHOST_USER_HDR_SIZE + msg->size, msg->fds, msg->fd_num); + return send_fd_message(dev->ifname, sockfd, (char *)&ctx->msg, + VHOST_USER_HDR_SIZE + ctx->msg.size, ctx->fds, ctx->fd_num); } static int -send_vhost_reply(int sockfd, struct VhostUserMsg *msg) +send_vhost_reply(struct virtio_net *dev, int sockfd, struct vhu_msg_context *ctx) { - if (!msg) + if (!ctx) return 0; - msg->flags &= ~VHOST_USER_VERSION_MASK; - msg->flags &= ~VHOST_USER_NEED_REPLY; - msg->flags |= VHOST_USER_VERSION; - msg->flags |= VHOST_USER_REPLY_MASK; + ctx->msg.flags &= ~VHOST_USER_VERSION_MASK; + ctx->msg.flags &= ~VHOST_USER_NEED_REPLY; + ctx->msg.flags |= VHOST_USER_VERSION; + ctx->msg.flags |= VHOST_USER_REPLY_MASK; - return send_vhost_message(sockfd, msg); + return send_vhost_message(dev, sockfd, ctx); } static int -send_vhost_slave_message(struct virtio_net *dev, struct VhostUserMsg *msg) +send_vhost_slave_message(struct virtio_net *dev, + struct vhu_msg_context *ctx) { int ret; - if (msg->flags & VHOST_USER_NEED_REPLY) + if (ctx->msg.flags & VHOST_USER_NEED_REPLY) rte_spinlock_lock(&dev->slave_req_lock); - ret = send_vhost_message(dev->slave_req_fd, msg); - if (ret < 0 && (msg->flags & VHOST_USER_NEED_REPLY)) + ret = send_vhost_message(dev, dev->slave_req_fd, ctx); + if (ret < 0 && (ctx->msg.flags & VHOST_USER_NEED_REPLY)) rte_spinlock_unlock(&dev->slave_req_lock); return ret; @@ -2854,32 +2870,34 @@ send_vhost_slave_message(struct virtio_net *dev, struct VhostUserMsg *msg) */ static int vhost_user_check_and_alloc_queue_pair(struct virtio_net *dev, - struct VhostUserMsg *msg) + struct vhu_msg_context *ctx) { uint32_t vring_idx; - switch (msg->request.master) { + switch (ctx->msg.request.master) { case VHOST_USER_SET_VRING_KICK: case VHOST_USER_SET_VRING_CALL: case VHOST_USER_SET_VRING_ERR: - vring_idx = msg->payload.u64 & VHOST_USER_VRING_IDX_MASK; + vring_idx = ctx->msg.payload.u64 & VHOST_USER_VRING_IDX_MASK; break; case VHOST_USER_SET_VRING_NUM: case VHOST_USER_SET_VRING_BASE: case VHOST_USER_GET_VRING_BASE: case VHOST_USER_SET_VRING_ENABLE: - vring_idx = msg->payload.state.index; + vring_idx = ctx->msg.payload.state.index; break; case VHOST_USER_SET_VRING_ADDR: - vring_idx = msg->payload.addr.index; + vring_idx = ctx->msg.payload.addr.index; + break; + case VHOST_USER_SET_INFLIGHT_FD: + vring_idx = ctx->msg.payload.inflight.num_queues - 1; break; default: return 0; } if (vring_idx >= VHOST_MAX_VRING) { - VHOST_LOG_CONFIG(ERR, - "invalid vring index: %u\n", vring_idx); + VHOST_LOG_CONFIG(ERR, "(%s) invalid vring index: %u\n", dev->ifname, vring_idx); return -1; } @@ -2927,7 +2945,7 @@ int vhost_user_msg_handler(int vid, int fd) { struct virtio_net *dev; - struct VhostUserMsg msg; + struct vhu_msg_context ctx; struct rte_vdpa_device *vdpa_dev; int ret; int unlock_required = 0; @@ -2942,43 +2960,39 @@ vhost_user_msg_handler(int vid, int fd) if (!dev->notify_ops) { dev->notify_ops = vhost_driver_callback_get(dev->ifname); if (!dev->notify_ops) { - VHOST_LOG_CONFIG(ERR, - "failed to get callback ops for driver %s\n", + VHOST_LOG_CONFIG(ERR, "(%s) failed to get callback ops for driver\n", dev->ifname); return -1; } } - ret = read_vhost_message(fd, &msg); + ret = read_vhost_message(dev, fd, &ctx); if (ret <= 0) { if (ret < 0) - VHOST_LOG_CONFIG(ERR, - "vhost read message failed\n"); + VHOST_LOG_CONFIG(ERR, "(%s) vhost read message failed\n", dev->ifname); else - VHOST_LOG_CONFIG(INFO, - "vhost peer closed\n"); + VHOST_LOG_CONFIG(INFO, "(%s) vhost peer closed\n", dev->ifname); return -1; } ret = 0; - request = msg.request.master; + request = ctx.msg.request.master; if (request > VHOST_USER_NONE && request < VHOST_USER_MAX && vhost_message_str[request]) { if (request != VHOST_USER_IOTLB_MSG) - VHOST_LOG_CONFIG(INFO, "read message %s\n", - vhost_message_str[request]); + VHOST_LOG_CONFIG(INFO, "(%s) read message %s\n", + dev->ifname, vhost_message_str[request]); else - VHOST_LOG_CONFIG(DEBUG, "read message %s\n", - vhost_message_str[request]); + VHOST_LOG_CONFIG(DEBUG, "(%s) read message %s\n", + dev->ifname, vhost_message_str[request]); } else { - VHOST_LOG_CONFIG(DEBUG, "External request %d\n", request); + VHOST_LOG_CONFIG(DEBUG, "(%s) external request %d\n", dev->ifname, request); } - ret = vhost_user_check_and_alloc_queue_pair(dev, &msg); + ret = vhost_user_check_and_alloc_queue_pair(dev, &ctx); if (ret < 0) { - VHOST_LOG_CONFIG(ERR, - "failed to alloc queue\n"); + VHOST_LOG_CONFIG(ERR, "(%s) failed to alloc queue\n", dev->ifname); return -1; } @@ -3018,11 +3032,11 @@ vhost_user_msg_handler(int vid, int fd) handled = false; if (dev->extern_ops.pre_msg_handle) { - ret = (*dev->extern_ops.pre_msg_handle)(dev->vid, - (void *)&msg); + RTE_BUILD_BUG_ON(offsetof(struct vhu_msg_context, msg) != 0); + ret = (*dev->extern_ops.pre_msg_handle)(dev->vid, &ctx); switch (ret) { case RTE_VHOST_MSG_RESULT_REPLY: - send_vhost_reply(fd, &msg); + send_vhost_reply(dev, fd, &ctx); /* Fall-through */ case RTE_VHOST_MSG_RESULT_ERR: case RTE_VHOST_MSG_RESULT_OK: @@ -3037,26 +3051,23 @@ vhost_user_msg_handler(int vid, int fd) if (request > VHOST_USER_NONE && request < VHOST_USER_MAX) { if (!vhost_message_handlers[request]) goto skip_to_post_handle; - ret = vhost_message_handlers[request](&dev, &msg, fd); + ret = vhost_message_handlers[request](&dev, &ctx, fd); switch (ret) { case RTE_VHOST_MSG_RESULT_ERR: - VHOST_LOG_CONFIG(ERR, - "Processing %s failed.\n", - vhost_message_str[request]); + VHOST_LOG_CONFIG(ERR, "(%s) processing %s failed.\n", + dev->ifname, vhost_message_str[request]); handled = true; break; case RTE_VHOST_MSG_RESULT_OK: - VHOST_LOG_CONFIG(DEBUG, - "Processing %s succeeded.\n", - vhost_message_str[request]); + VHOST_LOG_CONFIG(DEBUG, "(%s) processing %s succeeded.\n", + dev->ifname, vhost_message_str[request]); handled = true; break; case RTE_VHOST_MSG_RESULT_REPLY: - VHOST_LOG_CONFIG(DEBUG, - "Processing %s succeeded and needs reply.\n", - vhost_message_str[request]); - send_vhost_reply(fd, &msg); + VHOST_LOG_CONFIG(DEBUG, "(%s) processing %s succeeded and needs reply.\n", + dev->ifname, vhost_message_str[request]); + send_vhost_reply(dev, fd, &ctx); handled = true; break; default: @@ -3067,11 +3078,11 @@ vhost_user_msg_handler(int vid, int fd) skip_to_post_handle: if (ret != RTE_VHOST_MSG_RESULT_ERR && dev->extern_ops.post_msg_handle) { - ret = (*dev->extern_ops.post_msg_handle)(dev->vid, - (void *)&msg); + RTE_BUILD_BUG_ON(offsetof(struct vhu_msg_context, msg) != 0); + ret = (*dev->extern_ops.post_msg_handle)(dev->vid, &ctx); switch (ret) { case RTE_VHOST_MSG_RESULT_REPLY: - send_vhost_reply(fd, &msg); + send_vhost_reply(dev, fd, &ctx); /* Fall-through */ case RTE_VHOST_MSG_RESULT_ERR: case RTE_VHOST_MSG_RESULT_OK: @@ -3084,9 +3095,9 @@ skip_to_post_handle: /* If message was not handled at this stage, treat it as an error */ if (!handled) { - VHOST_LOG_CONFIG(ERR, - "vhost message (req: %d) was not handled.\n", request); - close_msg_fds(&msg); + VHOST_LOG_CONFIG(ERR, "(%s) vhost message (req: %d) was not handled.\n", + dev->ifname, request); + close_msg_fds(&ctx); ret = RTE_VHOST_MSG_RESULT_ERR; } @@ -3095,14 +3106,13 @@ skip_to_post_handle: * this optional reply-ack won't be sent as the * VHOST_USER_NEED_REPLY was cleared in send_vhost_reply(). */ - if (msg.flags & VHOST_USER_NEED_REPLY) { - msg.payload.u64 = ret == RTE_VHOST_MSG_RESULT_ERR; - msg.size = sizeof(msg.payload.u64); - msg.fd_num = 0; - send_vhost_reply(fd, &msg); + if (ctx.msg.flags & VHOST_USER_NEED_REPLY) { + ctx.msg.payload.u64 = ret == RTE_VHOST_MSG_RESULT_ERR; + ctx.msg.size = sizeof(ctx.msg.payload.u64); + ctx.fd_num = 0; + send_vhost_reply(dev, fd, &ctx); } else if (ret == RTE_VHOST_MSG_RESULT_ERR) { - VHOST_LOG_CONFIG(ERR, - "vhost message handling failed.\n"); + VHOST_LOG_CONFIG(ERR, "(%s) vhost message handling failed.\n", dev->ifname); return -1; } @@ -3139,8 +3149,8 @@ skip_to_post_handle: if (!(dev->flags & VIRTIO_DEV_VDPA_CONFIGURED)) { if (vdpa_dev->ops->dev_conf(dev->vid)) - VHOST_LOG_CONFIG(ERR, - "Failed to configure vDPA device\n"); + VHOST_LOG_CONFIG(ERR, "(%s) failed to configure vDPA device\n", + dev->ifname); else dev->flags |= VIRTIO_DEV_VDPA_CONFIGURED; } @@ -3150,36 +3160,34 @@ out: } static int process_slave_message_reply(struct virtio_net *dev, - const struct VhostUserMsg *msg) + const struct vhu_msg_context *ctx) { - struct VhostUserMsg msg_reply; + struct vhu_msg_context msg_reply; int ret; - if ((msg->flags & VHOST_USER_NEED_REPLY) == 0) + if ((ctx->msg.flags & VHOST_USER_NEED_REPLY) == 0) return 0; - ret = read_vhost_message(dev->slave_req_fd, &msg_reply); + ret = read_vhost_message(dev, dev->slave_req_fd, &msg_reply); if (ret <= 0) { if (ret < 0) - VHOST_LOG_CONFIG(ERR, - "vhost read slave message reply failed\n"); + VHOST_LOG_CONFIG(ERR, "(%s) vhost read slave message reply failed\n", + dev->ifname); else - VHOST_LOG_CONFIG(INFO, - "vhost peer closed\n"); + VHOST_LOG_CONFIG(INFO, "(%s) vhost peer closed\n", dev->ifname); ret = -1; goto out; } ret = 0; - if (msg_reply.request.slave != msg->request.slave) { - VHOST_LOG_CONFIG(ERR, - "Received unexpected msg type (%u), expected %u\n", - msg_reply.request.slave, msg->request.slave); + if (msg_reply.msg.request.slave != ctx->msg.request.slave) { + VHOST_LOG_CONFIG(ERR, "(%s) received unexpected msg type (%u), expected %u\n", + dev->ifname, msg_reply.msg.request.slave, ctx->msg.request.slave); ret = -1; goto out; } - ret = msg_reply.payload.u64 ? -1 : 0; + ret = msg_reply.msg.payload.u64 ? -1 : 0; out: rte_spinlock_unlock(&dev->slave_req_lock); @@ -3190,22 +3198,23 @@ int vhost_user_iotlb_miss(struct virtio_net *dev, uint64_t iova, uint8_t perm) { int ret; - struct VhostUserMsg msg = { - .request.slave = VHOST_USER_SLAVE_IOTLB_MSG, - .flags = VHOST_USER_VERSION, - .size = sizeof(msg.payload.iotlb), - .payload.iotlb = { - .iova = iova, - .perm = perm, - .type = VHOST_IOTLB_MISS, + struct vhu_msg_context ctx = { + .msg = { + .request.slave = VHOST_USER_SLAVE_IOTLB_MSG, + .flags = VHOST_USER_VERSION, + .size = sizeof(ctx.msg.payload.iotlb), + .payload.iotlb = { + .iova = iova, + .perm = perm, + .type = VHOST_IOTLB_MISS, + }, }, }; - ret = send_vhost_message(dev->slave_req_fd, &msg); + ret = send_vhost_message(dev, dev->slave_req_fd, &ctx); if (ret < 0) { - VHOST_LOG_CONFIG(ERR, - "Failed to send IOTLB miss message (%d)\n", - ret); + VHOST_LOG_CONFIG(ERR, "(%s) failed to send IOTLB miss message (%d)\n", + dev->ifname, ret); return ret; } @@ -3216,24 +3225,25 @@ static int vhost_user_slave_config_change(struct virtio_net *dev, bool need_reply) { int ret; - struct VhostUserMsg msg = { - .request.slave = VHOST_USER_SLAVE_CONFIG_CHANGE_MSG, - .flags = VHOST_USER_VERSION, - .size = 0, + struct vhu_msg_context ctx = { + .msg = { + .request.slave = VHOST_USER_SLAVE_CONFIG_CHANGE_MSG, + .flags = VHOST_USER_VERSION, + .size = 0, + } }; if (need_reply) - msg.flags |= VHOST_USER_NEED_REPLY; + ctx.msg.flags |= VHOST_USER_NEED_REPLY; - ret = send_vhost_slave_message(dev, &msg); + ret = send_vhost_slave_message(dev, &ctx); if (ret < 0) { - VHOST_LOG_CONFIG(ERR, - "Failed to send config change (%d)\n", - ret); + VHOST_LOG_CONFIG(ERR, "(%s) failed to send config change (%d)\n", + dev->ifname, ret); return ret; } - return process_slave_message_reply(dev, &msg); + return process_slave_message_reply(dev, &ctx); } int @@ -3254,32 +3264,34 @@ static int vhost_user_slave_set_vring_host_notifier(struct virtio_net *dev, uint64_t size) { int ret; - struct VhostUserMsg msg = { - .request.slave = VHOST_USER_SLAVE_VRING_HOST_NOTIFIER_MSG, - .flags = VHOST_USER_VERSION | VHOST_USER_NEED_REPLY, - .size = sizeof(msg.payload.area), - .payload.area = { - .u64 = index & VHOST_USER_VRING_IDX_MASK, - .size = size, - .offset = offset, + struct vhu_msg_context ctx = { + .msg = { + .request.slave = VHOST_USER_SLAVE_VRING_HOST_NOTIFIER_MSG, + .flags = VHOST_USER_VERSION | VHOST_USER_NEED_REPLY, + .size = sizeof(ctx.msg.payload.area), + .payload.area = { + .u64 = index & VHOST_USER_VRING_IDX_MASK, + .size = size, + .offset = offset, + }, }, }; if (fd < 0) - msg.payload.area.u64 |= VHOST_USER_VRING_NOFD_MASK; + ctx.msg.payload.area.u64 |= VHOST_USER_VRING_NOFD_MASK; else { - msg.fds[0] = fd; - msg.fd_num = 1; + ctx.fds[0] = fd; + ctx.fd_num = 1; } - ret = send_vhost_slave_message(dev, &msg); + ret = send_vhost_slave_message(dev, &ctx); if (ret < 0) { - VHOST_LOG_CONFIG(ERR, - "Failed to set host notifier (%d)\n", ret); + VHOST_LOG_CONFIG(ERR, "(%s) failed to set host notifier (%d)\n", + dev->ifname, ret); return ret; } - return process_slave_message_reply(dev, &msg); + return process_slave_message_reply(dev, &ctx); } int rte_vhost_host_notifier_ctrl(int vid, uint16_t qid, bool enable)