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>
51 #include "vhost_user.h"
53 #define VIRTIO_MIN_MTU 68
54 #define VIRTIO_MAX_MTU 65535
56 #define INFLIGHT_ALIGNMENT 64
57 #define INFLIGHT_VERSION 0x1
59 static const char *vhost_message_str[VHOST_USER_MAX] = {
60 [VHOST_USER_NONE] = "VHOST_USER_NONE",
61 [VHOST_USER_GET_FEATURES] = "VHOST_USER_GET_FEATURES",
62 [VHOST_USER_SET_FEATURES] = "VHOST_USER_SET_FEATURES",
63 [VHOST_USER_SET_OWNER] = "VHOST_USER_SET_OWNER",
64 [VHOST_USER_RESET_OWNER] = "VHOST_USER_RESET_OWNER",
65 [VHOST_USER_SET_MEM_TABLE] = "VHOST_USER_SET_MEM_TABLE",
66 [VHOST_USER_SET_LOG_BASE] = "VHOST_USER_SET_LOG_BASE",
67 [VHOST_USER_SET_LOG_FD] = "VHOST_USER_SET_LOG_FD",
68 [VHOST_USER_SET_VRING_NUM] = "VHOST_USER_SET_VRING_NUM",
69 [VHOST_USER_SET_VRING_ADDR] = "VHOST_USER_SET_VRING_ADDR",
70 [VHOST_USER_SET_VRING_BASE] = "VHOST_USER_SET_VRING_BASE",
71 [VHOST_USER_GET_VRING_BASE] = "VHOST_USER_GET_VRING_BASE",
72 [VHOST_USER_SET_VRING_KICK] = "VHOST_USER_SET_VRING_KICK",
73 [VHOST_USER_SET_VRING_CALL] = "VHOST_USER_SET_VRING_CALL",
74 [VHOST_USER_SET_VRING_ERR] = "VHOST_USER_SET_VRING_ERR",
75 [VHOST_USER_GET_PROTOCOL_FEATURES] = "VHOST_USER_GET_PROTOCOL_FEATURES",
76 [VHOST_USER_SET_PROTOCOL_FEATURES] = "VHOST_USER_SET_PROTOCOL_FEATURES",
77 [VHOST_USER_GET_QUEUE_NUM] = "VHOST_USER_GET_QUEUE_NUM",
78 [VHOST_USER_SET_VRING_ENABLE] = "VHOST_USER_SET_VRING_ENABLE",
79 [VHOST_USER_SEND_RARP] = "VHOST_USER_SEND_RARP",
80 [VHOST_USER_NET_SET_MTU] = "VHOST_USER_NET_SET_MTU",
81 [VHOST_USER_SET_SLAVE_REQ_FD] = "VHOST_USER_SET_SLAVE_REQ_FD",
82 [VHOST_USER_IOTLB_MSG] = "VHOST_USER_IOTLB_MSG",
83 [VHOST_USER_CRYPTO_CREATE_SESS] = "VHOST_USER_CRYPTO_CREATE_SESS",
84 [VHOST_USER_CRYPTO_CLOSE_SESS] = "VHOST_USER_CRYPTO_CLOSE_SESS",
85 [VHOST_USER_POSTCOPY_ADVISE] = "VHOST_USER_POSTCOPY_ADVISE",
86 [VHOST_USER_POSTCOPY_LISTEN] = "VHOST_USER_POSTCOPY_LISTEN",
87 [VHOST_USER_POSTCOPY_END] = "VHOST_USER_POSTCOPY_END",
88 [VHOST_USER_GET_INFLIGHT_FD] = "VHOST_USER_GET_INFLIGHT_FD",
89 [VHOST_USER_SET_INFLIGHT_FD] = "VHOST_USER_SET_INFLIGHT_FD",
90 [VHOST_USER_SET_STATUS] = "VHOST_USER_SET_STATUS",
91 [VHOST_USER_GET_STATUS] = "VHOST_USER_GET_STATUS",
94 static int send_vhost_reply(int sockfd, struct VhostUserMsg *msg);
95 static int read_vhost_message(int sockfd, struct VhostUserMsg *msg);
98 close_msg_fds(struct VhostUserMsg *msg)
102 for (i = 0; i < msg->fd_num; i++) {
103 int fd = msg->fds[i];
114 * Ensure the expected number of FDs is received,
115 * close all FDs and return an error if this is not the case.
118 validate_msg_fds(struct VhostUserMsg *msg, int expected_fds)
120 if (msg->fd_num == expected_fds)
123 VHOST_LOG_CONFIG(ERR,
124 " Expect %d FDs for request %s, received %d\n",
126 vhost_message_str[msg->request.master],
140 ret = fstat(fd, &stat);
141 return ret == -1 ? (uint64_t)-1 : (uint64_t)stat.st_blksize;
145 free_mem_region(struct virtio_net *dev)
148 struct rte_vhost_mem_region *reg;
150 if (!dev || !dev->mem)
153 for (i = 0; i < dev->mem->nregions; i++) {
154 reg = &dev->mem->regions[i];
155 if (reg->host_user_addr) {
156 munmap(reg->mmap_addr, reg->mmap_size);
163 vhost_backend_cleanup(struct virtio_net *dev)
166 free_mem_region(dev);
171 rte_free(dev->guest_pages);
172 dev->guest_pages = NULL;
175 munmap((void *)(uintptr_t)dev->log_addr, dev->log_size);
179 if (dev->inflight_info) {
180 if (dev->inflight_info->addr) {
181 munmap(dev->inflight_info->addr,
182 dev->inflight_info->size);
183 dev->inflight_info->addr = NULL;
186 if (dev->inflight_info->fd >= 0) {
187 close(dev->inflight_info->fd);
188 dev->inflight_info->fd = -1;
191 rte_free(dev->inflight_info);
192 dev->inflight_info = NULL;
195 if (dev->slave_req_fd >= 0) {
196 close(dev->slave_req_fd);
197 dev->slave_req_fd = -1;
200 if (dev->postcopy_ufd >= 0) {
201 close(dev->postcopy_ufd);
202 dev->postcopy_ufd = -1;
205 dev->postcopy_listening = 0;
209 vhost_user_notify_queue_state(struct virtio_net *dev, uint16_t index,
212 struct rte_vdpa_device *vdpa_dev = dev->vdpa_dev;
213 struct vhost_virtqueue *vq = dev->virtqueue[index];
215 /* Configure guest notifications on enable */
216 if (enable && vq->notif_enable != VIRTIO_UNINITIALIZED_NOTIF)
217 vhost_enable_guest_notification(dev, vq, vq->notif_enable);
219 if (vdpa_dev && vdpa_dev->ops->set_vring_state)
220 vdpa_dev->ops->set_vring_state(dev->vid, index, enable);
222 if (dev->notify_ops->vring_state_changed)
223 dev->notify_ops->vring_state_changed(dev->vid,
228 * This function just returns success at the moment unless
229 * the device hasn't been initialised.
232 vhost_user_set_owner(struct virtio_net **pdev __rte_unused,
233 struct VhostUserMsg *msg,
234 int main_fd __rte_unused)
236 if (validate_msg_fds(msg, 0) != 0)
237 return RTE_VHOST_MSG_RESULT_ERR;
239 return RTE_VHOST_MSG_RESULT_OK;
243 vhost_user_reset_owner(struct virtio_net **pdev,
244 struct VhostUserMsg *msg,
245 int main_fd __rte_unused)
247 struct virtio_net *dev = *pdev;
249 if (validate_msg_fds(msg, 0) != 0)
250 return RTE_VHOST_MSG_RESULT_ERR;
252 vhost_destroy_device_notify(dev);
254 cleanup_device(dev, 0);
256 return RTE_VHOST_MSG_RESULT_OK;
260 * The features that we support are requested.
263 vhost_user_get_features(struct virtio_net **pdev, struct VhostUserMsg *msg,
264 int main_fd __rte_unused)
266 struct virtio_net *dev = *pdev;
267 uint64_t features = 0;
269 if (validate_msg_fds(msg, 0) != 0)
270 return RTE_VHOST_MSG_RESULT_ERR;
272 rte_vhost_driver_get_features(dev->ifname, &features);
274 msg->payload.u64 = features;
275 msg->size = sizeof(msg->payload.u64);
278 return RTE_VHOST_MSG_RESULT_REPLY;
282 * The queue number that we support are requested.
285 vhost_user_get_queue_num(struct virtio_net **pdev, struct VhostUserMsg *msg,
286 int main_fd __rte_unused)
288 struct virtio_net *dev = *pdev;
289 uint32_t queue_num = 0;
291 if (validate_msg_fds(msg, 0) != 0)
292 return RTE_VHOST_MSG_RESULT_ERR;
294 rte_vhost_driver_get_queue_num(dev->ifname, &queue_num);
296 msg->payload.u64 = (uint64_t)queue_num;
297 msg->size = sizeof(msg->payload.u64);
300 return RTE_VHOST_MSG_RESULT_REPLY;
304 * We receive the negotiated features supported by us and the virtio device.
307 vhost_user_set_features(struct virtio_net **pdev, struct VhostUserMsg *msg,
308 int main_fd __rte_unused)
310 struct virtio_net *dev = *pdev;
311 uint64_t features = msg->payload.u64;
312 uint64_t vhost_features = 0;
313 struct rte_vdpa_device *vdpa_dev;
315 if (validate_msg_fds(msg, 0) != 0)
316 return RTE_VHOST_MSG_RESULT_ERR;
318 rte_vhost_driver_get_features(dev->ifname, &vhost_features);
319 if (features & ~vhost_features) {
320 VHOST_LOG_CONFIG(ERR,
321 "(%d) received invalid negotiated features.\n",
323 dev->flags |= VIRTIO_DEV_FEATURES_FAILED;
324 dev->status &= ~VIRTIO_DEVICE_STATUS_FEATURES_OK;
326 return RTE_VHOST_MSG_RESULT_ERR;
329 if (dev->flags & VIRTIO_DEV_RUNNING) {
330 if (dev->features == features)
331 return RTE_VHOST_MSG_RESULT_OK;
334 * Error out if master tries to change features while device is
335 * in running state. The exception being VHOST_F_LOG_ALL, which
336 * is enabled when the live-migration starts.
338 if ((dev->features ^ features) & ~(1ULL << VHOST_F_LOG_ALL)) {
339 VHOST_LOG_CONFIG(ERR,
340 "(%d) features changed while device is running.\n",
342 return RTE_VHOST_MSG_RESULT_ERR;
345 if (dev->notify_ops->features_changed)
346 dev->notify_ops->features_changed(dev->vid, features);
349 dev->features = features;
351 ((1ULL << VIRTIO_NET_F_MRG_RXBUF) |
352 (1ULL << VIRTIO_F_VERSION_1) |
353 (1ULL << VIRTIO_F_RING_PACKED))) {
354 dev->vhost_hlen = sizeof(struct virtio_net_hdr_mrg_rxbuf);
356 dev->vhost_hlen = sizeof(struct virtio_net_hdr);
358 VHOST_LOG_CONFIG(INFO,
359 "negotiated Virtio features: 0x%" PRIx64 "\n", dev->features);
360 VHOST_LOG_CONFIG(DEBUG,
361 "(%d) mergeable RX buffers %s, virtio 1 %s\n",
363 (dev->features & (1 << VIRTIO_NET_F_MRG_RXBUF)) ? "on" : "off",
364 (dev->features & (1ULL << VIRTIO_F_VERSION_1)) ? "on" : "off");
366 if ((dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET) &&
367 !(dev->features & (1ULL << VIRTIO_NET_F_MQ))) {
369 * Remove all but first queue pair if MQ hasn't been
370 * negotiated. This is safe because the device is not
371 * running at this stage.
373 while (dev->nr_vring > 2) {
374 struct vhost_virtqueue *vq;
376 vq = dev->virtqueue[--dev->nr_vring];
380 dev->virtqueue[dev->nr_vring] = NULL;
382 cleanup_vq_inflight(dev, vq);
387 vdpa_dev = dev->vdpa_dev;
389 vdpa_dev->ops->set_features(dev->vid);
391 dev->flags &= ~VIRTIO_DEV_FEATURES_FAILED;
392 return RTE_VHOST_MSG_RESULT_OK;
396 * The virtio device sends us the size of the descriptor ring.
399 vhost_user_set_vring_num(struct virtio_net **pdev,
400 struct VhostUserMsg *msg,
401 int main_fd __rte_unused)
403 struct virtio_net *dev = *pdev;
404 struct vhost_virtqueue *vq = dev->virtqueue[msg->payload.state.index];
406 if (validate_msg_fds(msg, 0) != 0)
407 return RTE_VHOST_MSG_RESULT_ERR;
409 if (msg->payload.state.num > 32768) {
410 VHOST_LOG_CONFIG(ERR, "invalid virtqueue size %u\n", msg->payload.state.num);
411 return RTE_VHOST_MSG_RESULT_ERR;
414 vq->size = msg->payload.state.num;
416 /* VIRTIO 1.0, 2.4 Virtqueues says:
418 * Queue Size value is always a power of 2. The maximum Queue Size
421 * VIRTIO 1.1 2.7 Virtqueues says:
423 * Packed virtqueues support up to 2^15 entries each.
425 if (!vq_is_packed(dev)) {
426 if (vq->size & (vq->size - 1)) {
427 VHOST_LOG_CONFIG(ERR,
428 "invalid virtqueue size %u\n", vq->size);
429 return RTE_VHOST_MSG_RESULT_ERR;
433 if (vq_is_packed(dev)) {
434 if (vq->shadow_used_packed)
435 rte_free(vq->shadow_used_packed);
436 vq->shadow_used_packed = rte_malloc_socket(NULL,
438 sizeof(struct vring_used_elem_packed),
439 RTE_CACHE_LINE_SIZE, vq->numa_node);
440 if (!vq->shadow_used_packed) {
441 VHOST_LOG_CONFIG(ERR,
442 "failed to allocate memory for shadow used ring.\n");
443 return RTE_VHOST_MSG_RESULT_ERR;
447 if (vq->shadow_used_split)
448 rte_free(vq->shadow_used_split);
450 vq->shadow_used_split = rte_malloc_socket(NULL,
451 vq->size * sizeof(struct vring_used_elem),
452 RTE_CACHE_LINE_SIZE, vq->numa_node);
454 if (!vq->shadow_used_split) {
455 VHOST_LOG_CONFIG(ERR,
456 "failed to allocate memory for vq internal data.\n");
457 return RTE_VHOST_MSG_RESULT_ERR;
461 if (vq->batch_copy_elems)
462 rte_free(vq->batch_copy_elems);
463 vq->batch_copy_elems = rte_malloc_socket(NULL,
464 vq->size * sizeof(struct batch_copy_elem),
465 RTE_CACHE_LINE_SIZE, vq->numa_node);
466 if (!vq->batch_copy_elems) {
467 VHOST_LOG_CONFIG(ERR,
468 "failed to allocate memory for batching copy.\n");
469 return RTE_VHOST_MSG_RESULT_ERR;
472 return RTE_VHOST_MSG_RESULT_OK;
476 * Reallocate virtio_dev, vhost_virtqueue and related data structures to
477 * make them on the same numa node as the memory of vring descriptor.
479 #ifdef RTE_LIBRTE_VHOST_NUMA
480 static struct virtio_net*
481 numa_realloc(struct virtio_net *dev, int index)
484 struct virtio_net *old_dev;
485 struct vhost_virtqueue *vq;
486 struct batch_copy_elem *bce;
487 struct guest_page *gp;
488 struct rte_vhost_memory *mem;
493 vq = dev->virtqueue[index];
496 * If VQ is ready, it is too late to reallocate, it certainly already
497 * happened anyway on VHOST_USER_SET_VRING_ADRR.
502 ret = get_mempolicy(&node, NULL, 0, vq->desc, MPOL_F_NODE | MPOL_F_ADDR);
504 VHOST_LOG_CONFIG(ERR, "Unable to get virtqueue %d numa information.\n", index);
508 if (node == vq->numa_node)
509 goto out_dev_realloc;
511 vq = rte_realloc_socket(vq, sizeof(*vq), 0, node);
513 VHOST_LOG_CONFIG(ERR, "Failed to realloc virtqueue %d on node %d\n",
518 if (vq != dev->virtqueue[index]) {
519 VHOST_LOG_CONFIG(INFO, "reallocated virtqueue on node %d\n", node);
520 dev->virtqueue[index] = vq;
521 vhost_user_iotlb_init(dev, index);
524 if (vq_is_packed(dev)) {
525 struct vring_used_elem_packed *sup;
527 sup = rte_realloc_socket(vq->shadow_used_packed, vq->size * sizeof(*sup),
528 RTE_CACHE_LINE_SIZE, node);
530 VHOST_LOG_CONFIG(ERR, "Failed to realloc shadow packed on node %d\n", node);
533 vq->shadow_used_packed = sup;
535 struct vring_used_elem *sus;
537 sus = rte_realloc_socket(vq->shadow_used_split, vq->size * sizeof(*sus),
538 RTE_CACHE_LINE_SIZE, node);
540 VHOST_LOG_CONFIG(ERR, "Failed to realloc shadow split on node %d\n", node);
543 vq->shadow_used_split = sus;
546 bce = rte_realloc_socket(vq->batch_copy_elems, vq->size * sizeof(*bce),
547 RTE_CACHE_LINE_SIZE, node);
549 VHOST_LOG_CONFIG(ERR, "Failed to realloc batch copy elem on node %d\n", node);
552 vq->batch_copy_elems = bce;
555 struct log_cache_entry *lc;
557 lc = rte_realloc_socket(vq->log_cache, sizeof(*lc) * VHOST_LOG_CACHE_NR, 0, node);
559 VHOST_LOG_CONFIG(ERR, "Failed to realloc log cache on node %d\n", node);
565 if (vq->resubmit_inflight) {
566 struct rte_vhost_resubmit_info *ri;
568 ri = rte_realloc_socket(vq->resubmit_inflight, sizeof(*ri), 0, node);
570 VHOST_LOG_CONFIG(ERR, "Failed to realloc resubmit inflight on node %d\n",
574 vq->resubmit_inflight = ri;
576 if (ri->resubmit_list) {
577 struct rte_vhost_resubmit_desc *rd;
579 rd = rte_realloc_socket(ri->resubmit_list, sizeof(*rd) * ri->resubmit_num,
582 VHOST_LOG_CONFIG(ERR, "Failed to realloc resubmit list on node %d\n",
586 ri->resubmit_list = rd;
590 vq->numa_node = node;
594 if (dev->flags & VIRTIO_DEV_RUNNING)
597 ret = get_mempolicy(&dev_node, NULL, 0, dev, MPOL_F_NODE | MPOL_F_ADDR);
599 VHOST_LOG_CONFIG(ERR, "Unable to get Virtio dev %d numa information.\n", dev->vid);
603 if (dev_node == node)
606 dev = rte_realloc_socket(old_dev, sizeof(*dev), 0, node);
608 VHOST_LOG_CONFIG(ERR, "Failed to realloc dev on node %d\n", node);
612 VHOST_LOG_CONFIG(INFO, "reallocated device on node %d\n", node);
613 vhost_devices[dev->vid] = dev;
615 mem_size = sizeof(struct rte_vhost_memory) +
616 sizeof(struct rte_vhost_mem_region) * dev->mem->nregions;
617 mem = rte_realloc_socket(dev->mem, mem_size, 0, node);
619 VHOST_LOG_CONFIG(ERR, "Failed to realloc mem table on node %d\n", node);
624 gp = rte_realloc_socket(dev->guest_pages, dev->max_guest_pages * sizeof(*gp),
625 RTE_CACHE_LINE_SIZE, node);
627 VHOST_LOG_CONFIG(ERR, "Failed to realloc guest pages on node %d\n", node);
630 dev->guest_pages = gp;
635 static struct virtio_net*
636 numa_realloc(struct virtio_net *dev, int index __rte_unused)
642 /* Converts QEMU virtual address to Vhost virtual address. */
644 qva_to_vva(struct virtio_net *dev, uint64_t qva, uint64_t *len)
646 struct rte_vhost_mem_region *r;
649 if (unlikely(!dev || !dev->mem))
652 /* Find the region where the address lives. */
653 for (i = 0; i < dev->mem->nregions; i++) {
654 r = &dev->mem->regions[i];
656 if (qva >= r->guest_user_addr &&
657 qva < r->guest_user_addr + r->size) {
659 if (unlikely(*len > r->guest_user_addr + r->size - qva))
660 *len = r->guest_user_addr + r->size - qva;
662 return qva - r->guest_user_addr +
674 * Converts ring address to Vhost virtual address.
675 * If IOMMU is enabled, the ring address is a guest IO virtual address,
676 * else it is a QEMU virtual address.
679 ring_addr_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
680 uint64_t ra, uint64_t *size)
682 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)) {
685 vhost_user_iotlb_rd_lock(vq);
686 vva = vhost_iova_to_vva(dev, vq, ra,
687 size, VHOST_ACCESS_RW);
688 vhost_user_iotlb_rd_unlock(vq);
693 return qva_to_vva(dev, ra, size);
697 log_addr_to_gpa(struct virtio_net *dev, struct vhost_virtqueue *vq)
701 vhost_user_iotlb_rd_lock(vq);
702 log_gpa = translate_log_addr(dev, vq, vq->ring_addrs.log_guest_addr);
703 vhost_user_iotlb_rd_unlock(vq);
708 static struct virtio_net *
709 translate_ring_addresses(struct virtio_net *dev, int vq_index)
711 struct vhost_virtqueue *vq = dev->virtqueue[vq_index];
712 struct vhost_vring_addr *addr = &vq->ring_addrs;
713 uint64_t len, expected_len;
715 if (addr->flags & (1 << VHOST_VRING_F_LOG)) {
717 log_addr_to_gpa(dev, vq);
718 if (vq->log_guest_addr == 0) {
719 VHOST_LOG_CONFIG(DEBUG,
720 "(%d) failed to map log_guest_addr.\n",
726 if (vq_is_packed(dev)) {
727 len = sizeof(struct vring_packed_desc) * vq->size;
728 vq->desc_packed = (struct vring_packed_desc *)(uintptr_t)
729 ring_addr_to_vva(dev, vq, addr->desc_user_addr, &len);
730 if (vq->desc_packed == NULL ||
731 len != sizeof(struct vring_packed_desc) *
733 VHOST_LOG_CONFIG(DEBUG,
734 "(%d) failed to map desc_packed ring.\n",
739 dev = numa_realloc(dev, vq_index);
740 vq = dev->virtqueue[vq_index];
741 addr = &vq->ring_addrs;
743 len = sizeof(struct vring_packed_desc_event);
744 vq->driver_event = (struct vring_packed_desc_event *)
745 (uintptr_t)ring_addr_to_vva(dev,
746 vq, addr->avail_user_addr, &len);
747 if (vq->driver_event == NULL ||
748 len != sizeof(struct vring_packed_desc_event)) {
749 VHOST_LOG_CONFIG(DEBUG,
750 "(%d) failed to find driver area address.\n",
755 len = sizeof(struct vring_packed_desc_event);
756 vq->device_event = (struct vring_packed_desc_event *)
757 (uintptr_t)ring_addr_to_vva(dev,
758 vq, addr->used_user_addr, &len);
759 if (vq->device_event == NULL ||
760 len != sizeof(struct vring_packed_desc_event)) {
761 VHOST_LOG_CONFIG(DEBUG,
762 "(%d) failed to find device area address.\n",
767 vq->access_ok = true;
771 /* The addresses are converted from QEMU virtual to Vhost virtual. */
772 if (vq->desc && vq->avail && vq->used)
775 len = sizeof(struct vring_desc) * vq->size;
776 vq->desc = (struct vring_desc *)(uintptr_t)ring_addr_to_vva(dev,
777 vq, addr->desc_user_addr, &len);
778 if (vq->desc == 0 || len != sizeof(struct vring_desc) * vq->size) {
779 VHOST_LOG_CONFIG(DEBUG,
780 "(%d) failed to map desc ring.\n",
785 dev = numa_realloc(dev, vq_index);
786 vq = dev->virtqueue[vq_index];
787 addr = &vq->ring_addrs;
789 len = sizeof(struct vring_avail) + sizeof(uint16_t) * vq->size;
790 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))
791 len += sizeof(uint16_t);
793 vq->avail = (struct vring_avail *)(uintptr_t)ring_addr_to_vva(dev,
794 vq, addr->avail_user_addr, &len);
795 if (vq->avail == 0 || len != expected_len) {
796 VHOST_LOG_CONFIG(DEBUG,
797 "(%d) failed to map avail ring.\n",
802 len = sizeof(struct vring_used) +
803 sizeof(struct vring_used_elem) * vq->size;
804 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))
805 len += sizeof(uint16_t);
807 vq->used = (struct vring_used *)(uintptr_t)ring_addr_to_vva(dev,
808 vq, addr->used_user_addr, &len);
809 if (vq->used == 0 || len != expected_len) {
810 VHOST_LOG_CONFIG(DEBUG,
811 "(%d) failed to map used ring.\n",
816 if (vq->last_used_idx != vq->used->idx) {
817 VHOST_LOG_CONFIG(WARNING,
818 "last_used_idx (%u) and vq->used->idx (%u) mismatches; "
819 "some packets maybe resent for Tx and dropped for Rx\n",
820 vq->last_used_idx, vq->used->idx);
821 vq->last_used_idx = vq->used->idx;
822 vq->last_avail_idx = vq->used->idx;
825 vq->access_ok = true;
827 VHOST_LOG_CONFIG(DEBUG, "(%d) mapped address desc: %p\n",
829 VHOST_LOG_CONFIG(DEBUG, "(%d) mapped address avail: %p\n",
830 dev->vid, vq->avail);
831 VHOST_LOG_CONFIG(DEBUG, "(%d) mapped address used: %p\n",
833 VHOST_LOG_CONFIG(DEBUG, "(%d) log_guest_addr: %" PRIx64 "\n",
834 dev->vid, vq->log_guest_addr);
840 * The virtio device sends us the desc, used and avail ring addresses.
841 * This function then converts these to our address space.
844 vhost_user_set_vring_addr(struct virtio_net **pdev, struct VhostUserMsg *msg,
845 int main_fd __rte_unused)
847 struct virtio_net *dev = *pdev;
848 struct vhost_virtqueue *vq;
849 struct vhost_vring_addr *addr = &msg->payload.addr;
852 if (validate_msg_fds(msg, 0) != 0)
853 return RTE_VHOST_MSG_RESULT_ERR;
855 if (dev->mem == NULL)
856 return RTE_VHOST_MSG_RESULT_ERR;
858 /* addr->index refers to the queue index. The txq 1, rxq is 0. */
859 vq = dev->virtqueue[msg->payload.addr.index];
861 access_ok = vq->access_ok;
864 * Rings addresses should not be interpreted as long as the ring is not
865 * started and enabled
867 memcpy(&vq->ring_addrs, addr, sizeof(*addr));
869 vring_invalidate(dev, vq);
871 if ((vq->enabled && (dev->features &
872 (1ULL << VHOST_USER_F_PROTOCOL_FEATURES))) ||
874 dev = translate_ring_addresses(dev, msg->payload.addr.index);
876 return RTE_VHOST_MSG_RESULT_ERR;
881 return RTE_VHOST_MSG_RESULT_OK;
885 * The virtio device sends us the available ring last used index.
888 vhost_user_set_vring_base(struct virtio_net **pdev,
889 struct VhostUserMsg *msg,
890 int main_fd __rte_unused)
892 struct virtio_net *dev = *pdev;
893 struct vhost_virtqueue *vq = dev->virtqueue[msg->payload.state.index];
894 uint64_t val = msg->payload.state.num;
896 if (validate_msg_fds(msg, 0) != 0)
897 return RTE_VHOST_MSG_RESULT_ERR;
899 if (vq_is_packed(dev)) {
901 * Bit[0:14]: avail index
902 * Bit[15]: avail wrap counter
904 vq->last_avail_idx = val & 0x7fff;
905 vq->avail_wrap_counter = !!(val & (0x1 << 15));
907 * Set used index to same value as available one, as
908 * their values should be the same since ring processing
909 * was stopped at get time.
911 vq->last_used_idx = vq->last_avail_idx;
912 vq->used_wrap_counter = vq->avail_wrap_counter;
914 vq->last_used_idx = msg->payload.state.num;
915 vq->last_avail_idx = msg->payload.state.num;
918 return RTE_VHOST_MSG_RESULT_OK;
922 add_one_guest_page(struct virtio_net *dev, uint64_t guest_phys_addr,
923 uint64_t host_phys_addr, uint64_t size)
925 struct guest_page *page, *last_page;
926 struct guest_page *old_pages;
928 if (dev->nr_guest_pages == dev->max_guest_pages) {
929 dev->max_guest_pages *= 2;
930 old_pages = dev->guest_pages;
931 dev->guest_pages = rte_realloc(dev->guest_pages,
932 dev->max_guest_pages * sizeof(*page),
933 RTE_CACHE_LINE_SIZE);
934 if (dev->guest_pages == NULL) {
935 VHOST_LOG_CONFIG(ERR, "cannot realloc guest_pages\n");
941 if (dev->nr_guest_pages > 0) {
942 last_page = &dev->guest_pages[dev->nr_guest_pages - 1];
943 /* merge if the two pages are continuous */
944 if (host_phys_addr == last_page->host_phys_addr +
946 last_page->size += size;
951 page = &dev->guest_pages[dev->nr_guest_pages++];
952 page->guest_phys_addr = guest_phys_addr;
953 page->host_phys_addr = host_phys_addr;
960 add_guest_pages(struct virtio_net *dev, struct rte_vhost_mem_region *reg,
963 uint64_t reg_size = reg->size;
964 uint64_t host_user_addr = reg->host_user_addr;
965 uint64_t guest_phys_addr = reg->guest_phys_addr;
966 uint64_t host_phys_addr;
969 host_phys_addr = rte_mem_virt2iova((void *)(uintptr_t)host_user_addr);
970 size = page_size - (guest_phys_addr & (page_size - 1));
971 size = RTE_MIN(size, reg_size);
973 if (add_one_guest_page(dev, guest_phys_addr, host_phys_addr, size) < 0)
976 host_user_addr += size;
977 guest_phys_addr += size;
980 while (reg_size > 0) {
981 size = RTE_MIN(reg_size, page_size);
982 host_phys_addr = rte_mem_virt2iova((void *)(uintptr_t)
984 if (add_one_guest_page(dev, guest_phys_addr, host_phys_addr,
988 host_user_addr += size;
989 guest_phys_addr += size;
993 /* sort guest page array if over binary search threshold */
994 if (dev->nr_guest_pages >= VHOST_BINARY_SEARCH_THRESH) {
995 qsort((void *)dev->guest_pages, dev->nr_guest_pages,
996 sizeof(struct guest_page), guest_page_addrcmp);
1002 #ifdef RTE_LIBRTE_VHOST_DEBUG
1003 /* TODO: enable it only in debug mode? */
1005 dump_guest_pages(struct virtio_net *dev)
1008 struct guest_page *page;
1010 for (i = 0; i < dev->nr_guest_pages; i++) {
1011 page = &dev->guest_pages[i];
1013 VHOST_LOG_CONFIG(INFO,
1014 "guest physical page region %u\n"
1015 "\t guest_phys_addr: %" PRIx64 "\n"
1016 "\t host_phys_addr : %" PRIx64 "\n"
1017 "\t size : %" PRIx64 "\n",
1019 page->guest_phys_addr,
1020 page->host_phys_addr,
1025 #define dump_guest_pages(dev)
1029 vhost_memory_changed(struct VhostUserMemory *new,
1030 struct rte_vhost_memory *old)
1034 if (new->nregions != old->nregions)
1037 for (i = 0; i < new->nregions; ++i) {
1038 VhostUserMemoryRegion *new_r = &new->regions[i];
1039 struct rte_vhost_mem_region *old_r = &old->regions[i];
1041 if (new_r->guest_phys_addr != old_r->guest_phys_addr)
1043 if (new_r->memory_size != old_r->size)
1045 if (new_r->userspace_addr != old_r->guest_user_addr)
1052 #ifdef RTE_LIBRTE_VHOST_POSTCOPY
1054 vhost_user_postcopy_region_register(struct virtio_net *dev,
1055 struct rte_vhost_mem_region *reg)
1057 struct uffdio_register reg_struct;
1060 * Let's register all the mmap'ed area to ensure
1061 * alignment on page boundary.
1063 reg_struct.range.start = (uint64_t)(uintptr_t)reg->mmap_addr;
1064 reg_struct.range.len = reg->mmap_size;
1065 reg_struct.mode = UFFDIO_REGISTER_MODE_MISSING;
1067 if (ioctl(dev->postcopy_ufd, UFFDIO_REGISTER,
1069 VHOST_LOG_CONFIG(ERR, "Failed to register ufd for region "
1070 "%" PRIx64 " - %" PRIx64 " (ufd = %d) %s\n",
1071 (uint64_t)reg_struct.range.start,
1072 (uint64_t)reg_struct.range.start +
1073 (uint64_t)reg_struct.range.len - 1,
1079 VHOST_LOG_CONFIG(INFO, "\t userfaultfd registered for range : %" PRIx64 " - %" PRIx64 "\n",
1080 (uint64_t)reg_struct.range.start,
1081 (uint64_t)reg_struct.range.start +
1082 (uint64_t)reg_struct.range.len - 1);
1088 vhost_user_postcopy_region_register(struct virtio_net *dev __rte_unused,
1089 struct rte_vhost_mem_region *reg __rte_unused)
1096 vhost_user_postcopy_register(struct virtio_net *dev, int main_fd,
1097 struct VhostUserMsg *msg)
1099 struct VhostUserMemory *memory;
1100 struct rte_vhost_mem_region *reg;
1101 VhostUserMsg ack_msg;
1104 if (!dev->postcopy_listening)
1108 * We haven't a better way right now than sharing
1109 * DPDK's virtual address with Qemu, so that Qemu can
1110 * retrieve the region offset when handling userfaults.
1112 memory = &msg->payload.memory;
1113 for (i = 0; i < memory->nregions; i++) {
1114 reg = &dev->mem->regions[i];
1115 memory->regions[i].userspace_addr = reg->host_user_addr;
1118 /* Send the addresses back to qemu */
1120 send_vhost_reply(main_fd, msg);
1122 /* Wait for qemu to acknolwedge it's got the addresses
1123 * we've got to wait before we're allowed to generate faults.
1125 if (read_vhost_message(main_fd, &ack_msg) <= 0) {
1126 VHOST_LOG_CONFIG(ERR,
1127 "Failed to read qemu ack on postcopy set-mem-table\n");
1131 if (validate_msg_fds(&ack_msg, 0) != 0)
1134 if (ack_msg.request.master != VHOST_USER_SET_MEM_TABLE) {
1135 VHOST_LOG_CONFIG(ERR,
1136 "Bad qemu ack on postcopy set-mem-table (%d)\n",
1137 ack_msg.request.master);
1141 /* Now userfault register and we can use the memory */
1142 for (i = 0; i < memory->nregions; i++) {
1143 reg = &dev->mem->regions[i];
1144 if (vhost_user_postcopy_region_register(dev, reg) < 0)
1152 vhost_user_mmap_region(struct virtio_net *dev,
1153 struct rte_vhost_mem_region *region,
1154 uint64_t mmap_offset)
1161 /* Check for memory_size + mmap_offset overflow */
1162 if (mmap_offset >= -region->size) {
1163 VHOST_LOG_CONFIG(ERR,
1164 "mmap_offset (%#"PRIx64") and memory_size "
1165 "(%#"PRIx64") overflow\n",
1166 mmap_offset, region->size);
1170 mmap_size = region->size + mmap_offset;
1172 /* mmap() without flag of MAP_ANONYMOUS, should be called with length
1173 * argument aligned with hugepagesz at older longterm version Linux,
1174 * like 2.6.32 and 3.2.72, or mmap() will fail with EINVAL.
1176 * To avoid failure, make sure in caller to keep length aligned.
1178 alignment = get_blk_size(region->fd);
1179 if (alignment == (uint64_t)-1) {
1180 VHOST_LOG_CONFIG(ERR,
1181 "couldn't get hugepage size through fstat\n");
1184 mmap_size = RTE_ALIGN_CEIL(mmap_size, alignment);
1185 if (mmap_size == 0) {
1187 * It could happen if initial mmap_size + alignment overflows
1188 * the sizeof uint64, which could happen if either mmap_size or
1189 * alignment value is wrong.
1191 * mmap() kernel implementation would return an error, but
1192 * better catch it before and provide useful info in the logs.
1194 VHOST_LOG_CONFIG(ERR, "mmap size (0x%" PRIx64 ") "
1195 "or alignment (0x%" PRIx64 ") is invalid\n",
1196 region->size + mmap_offset, alignment);
1200 populate = dev->async_copy ? MAP_POPULATE : 0;
1201 mmap_addr = mmap(NULL, mmap_size, PROT_READ | PROT_WRITE,
1202 MAP_SHARED | populate, region->fd, 0);
1204 if (mmap_addr == MAP_FAILED) {
1205 VHOST_LOG_CONFIG(ERR, "mmap failed (%s).\n", strerror(errno));
1209 region->mmap_addr = mmap_addr;
1210 region->mmap_size = mmap_size;
1211 region->host_user_addr = (uint64_t)(uintptr_t)mmap_addr + mmap_offset;
1213 if (dev->async_copy)
1214 if (add_guest_pages(dev, region, alignment) < 0) {
1215 VHOST_LOG_CONFIG(ERR,
1216 "adding guest pages to region failed.\n");
1220 VHOST_LOG_CONFIG(INFO,
1221 "guest memory region size: 0x%" PRIx64 "\n"
1222 "\t guest physical addr: 0x%" PRIx64 "\n"
1223 "\t guest virtual addr: 0x%" PRIx64 "\n"
1224 "\t host virtual addr: 0x%" PRIx64 "\n"
1225 "\t mmap addr : 0x%" PRIx64 "\n"
1226 "\t mmap size : 0x%" PRIx64 "\n"
1227 "\t mmap align: 0x%" PRIx64 "\n"
1228 "\t mmap off : 0x%" PRIx64 "\n",
1230 region->guest_phys_addr,
1231 region->guest_user_addr,
1232 region->host_user_addr,
1233 (uint64_t)(uintptr_t)mmap_addr,
1242 vhost_user_set_mem_table(struct virtio_net **pdev, struct VhostUserMsg *msg,
1245 struct virtio_net *dev = *pdev;
1246 struct VhostUserMemory *memory = &msg->payload.memory;
1247 struct rte_vhost_mem_region *reg;
1248 int numa_node = SOCKET_ID_ANY;
1249 uint64_t mmap_offset;
1251 bool async_notify = false;
1253 if (validate_msg_fds(msg, memory->nregions) != 0)
1254 return RTE_VHOST_MSG_RESULT_ERR;
1256 if (memory->nregions > VHOST_MEMORY_MAX_NREGIONS) {
1257 VHOST_LOG_CONFIG(ERR,
1258 "too many memory regions (%u)\n", memory->nregions);
1262 if (dev->mem && !vhost_memory_changed(memory, dev->mem)) {
1263 VHOST_LOG_CONFIG(INFO,
1264 "(%d) memory regions not changed\n", dev->vid);
1268 return RTE_VHOST_MSG_RESULT_OK;
1272 if (dev->flags & VIRTIO_DEV_VDPA_CONFIGURED) {
1273 struct rte_vdpa_device *vdpa_dev = dev->vdpa_dev;
1275 if (vdpa_dev && vdpa_dev->ops->dev_close)
1276 vdpa_dev->ops->dev_close(dev->vid);
1277 dev->flags &= ~VIRTIO_DEV_VDPA_CONFIGURED;
1280 /* notify the vhost application to stop DMA transfers */
1281 if (dev->async_copy && dev->notify_ops->vring_state_changed) {
1282 for (i = 0; i < dev->nr_vring; i++) {
1283 dev->notify_ops->vring_state_changed(dev->vid,
1286 async_notify = true;
1289 free_mem_region(dev);
1294 /* Flush IOTLB cache as previous HVAs are now invalid */
1295 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1296 for (i = 0; i < dev->nr_vring; i++)
1297 vhost_user_iotlb_flush_all(dev->virtqueue[i]);
1300 * If VQ 0 has already been allocated, try to allocate on the same
1301 * NUMA node. It can be reallocated later in numa_realloc().
1303 if (dev->nr_vring > 0)
1304 numa_node = dev->virtqueue[0]->numa_node;
1306 dev->nr_guest_pages = 0;
1307 if (dev->guest_pages == NULL) {
1308 dev->max_guest_pages = 8;
1309 dev->guest_pages = rte_zmalloc_socket(NULL,
1310 dev->max_guest_pages *
1311 sizeof(struct guest_page),
1312 RTE_CACHE_LINE_SIZE,
1314 if (dev->guest_pages == NULL) {
1315 VHOST_LOG_CONFIG(ERR,
1316 "(%d) failed to allocate memory "
1317 "for dev->guest_pages\n",
1323 dev->mem = rte_zmalloc_socket("vhost-mem-table", sizeof(struct rte_vhost_memory) +
1324 sizeof(struct rte_vhost_mem_region) * memory->nregions, 0, numa_node);
1325 if (dev->mem == NULL) {
1326 VHOST_LOG_CONFIG(ERR,
1327 "(%d) failed to allocate memory for dev->mem\n",
1329 goto free_guest_pages;
1332 for (i = 0; i < memory->nregions; i++) {
1333 reg = &dev->mem->regions[i];
1335 reg->guest_phys_addr = memory->regions[i].guest_phys_addr;
1336 reg->guest_user_addr = memory->regions[i].userspace_addr;
1337 reg->size = memory->regions[i].memory_size;
1338 reg->fd = msg->fds[i];
1341 * Assign invalid file descriptor value to avoid double
1342 * closing on error path.
1346 mmap_offset = memory->regions[i].mmap_offset;
1348 if (vhost_user_mmap_region(dev, reg, mmap_offset) < 0) {
1349 VHOST_LOG_CONFIG(ERR, "Failed to mmap region %u\n", i);
1350 goto free_mem_table;
1353 dev->mem->nregions++;
1356 if (vhost_user_postcopy_register(dev, main_fd, msg) < 0)
1357 goto free_mem_table;
1359 for (i = 0; i < dev->nr_vring; i++) {
1360 struct vhost_virtqueue *vq = dev->virtqueue[i];
1365 if (vq->desc || vq->avail || vq->used) {
1367 * If the memory table got updated, the ring addresses
1368 * need to be translated again as virtual addresses have
1371 vring_invalidate(dev, vq);
1373 dev = translate_ring_addresses(dev, i);
1376 goto free_mem_table;
1383 dump_guest_pages(dev);
1386 for (i = 0; i < dev->nr_vring; i++)
1387 dev->notify_ops->vring_state_changed(dev->vid, i, 1);
1390 return RTE_VHOST_MSG_RESULT_OK;
1393 free_mem_region(dev);
1397 rte_free(dev->guest_pages);
1398 dev->guest_pages = NULL;
1401 return RTE_VHOST_MSG_RESULT_ERR;
1405 vq_is_ready(struct virtio_net *dev, struct vhost_virtqueue *vq)
1412 if (vq_is_packed(dev))
1413 rings_ok = vq->desc_packed && vq->driver_event &&
1416 rings_ok = vq->desc && vq->avail && vq->used;
1419 vq->kickfd != VIRTIO_UNINITIALIZED_EVENTFD &&
1420 vq->callfd != VIRTIO_UNINITIALIZED_EVENTFD &&
1424 #define VIRTIO_BUILTIN_NUM_VQS_TO_BE_READY 2u
1427 virtio_is_ready(struct virtio_net *dev)
1429 struct vhost_virtqueue *vq;
1430 uint32_t i, nr_vring = dev->nr_vring;
1432 if (dev->flags & VIRTIO_DEV_READY)
1438 if (dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET) {
1439 nr_vring = VIRTIO_BUILTIN_NUM_VQS_TO_BE_READY;
1441 if (dev->nr_vring < nr_vring)
1445 for (i = 0; i < nr_vring; i++) {
1446 vq = dev->virtqueue[i];
1448 if (!vq_is_ready(dev, vq))
1452 /* If supported, ensure the frontend is really done with config */
1453 if (dev->protocol_features & (1ULL << VHOST_USER_PROTOCOL_F_STATUS))
1454 if (!(dev->status & VIRTIO_DEVICE_STATUS_DRIVER_OK))
1457 dev->flags |= VIRTIO_DEV_READY;
1459 if (!(dev->flags & VIRTIO_DEV_RUNNING))
1460 VHOST_LOG_CONFIG(INFO,
1461 "virtio is now ready for processing.\n");
1466 inflight_mem_alloc(const char *name, size_t size, int *fd)
1470 char fname[20] = "/tmp/memfd-XXXXXX";
1473 #ifdef MEMFD_SUPPORTED
1474 mfd = memfd_create(name, MFD_CLOEXEC);
1479 mfd = mkstemp(fname);
1481 VHOST_LOG_CONFIG(ERR,
1482 "failed to get inflight buffer fd\n");
1489 if (ftruncate(mfd, size) == -1) {
1490 VHOST_LOG_CONFIG(ERR,
1491 "failed to alloc inflight buffer\n");
1496 ptr = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, mfd, 0);
1497 if (ptr == MAP_FAILED) {
1498 VHOST_LOG_CONFIG(ERR,
1499 "failed to mmap inflight buffer\n");
1509 get_pervq_shm_size_split(uint16_t queue_size)
1511 return RTE_ALIGN_MUL_CEIL(sizeof(struct rte_vhost_inflight_desc_split) *
1512 queue_size + sizeof(uint64_t) +
1513 sizeof(uint16_t) * 4, INFLIGHT_ALIGNMENT);
1517 get_pervq_shm_size_packed(uint16_t queue_size)
1519 return RTE_ALIGN_MUL_CEIL(sizeof(struct rte_vhost_inflight_desc_packed)
1520 * queue_size + sizeof(uint64_t) +
1521 sizeof(uint16_t) * 6 + sizeof(uint8_t) * 9,
1522 INFLIGHT_ALIGNMENT);
1526 vhost_user_get_inflight_fd(struct virtio_net **pdev,
1528 int main_fd __rte_unused)
1530 struct rte_vhost_inflight_info_packed *inflight_packed;
1531 uint64_t pervq_inflight_size, mmap_size;
1532 uint16_t num_queues, queue_size;
1533 struct virtio_net *dev = *pdev;
1535 int numa_node = SOCKET_ID_ANY;
1538 if (msg->size != sizeof(msg->payload.inflight)) {
1539 VHOST_LOG_CONFIG(ERR,
1540 "invalid get_inflight_fd message size is %d\n",
1542 return RTE_VHOST_MSG_RESULT_ERR;
1546 * If VQ 0 has already been allocated, try to allocate on the same
1547 * NUMA node. It can be reallocated later in numa_realloc().
1549 if (dev->nr_vring > 0)
1550 numa_node = dev->virtqueue[0]->numa_node;
1552 if (dev->inflight_info == NULL) {
1553 dev->inflight_info = rte_zmalloc_socket("inflight_info",
1554 sizeof(struct inflight_mem_info), 0, numa_node);
1555 if (!dev->inflight_info) {
1556 VHOST_LOG_CONFIG(ERR,
1557 "failed to alloc dev inflight area\n");
1558 return RTE_VHOST_MSG_RESULT_ERR;
1560 dev->inflight_info->fd = -1;
1563 num_queues = msg->payload.inflight.num_queues;
1564 queue_size = msg->payload.inflight.queue_size;
1566 VHOST_LOG_CONFIG(INFO, "get_inflight_fd num_queues: %u\n",
1567 msg->payload.inflight.num_queues);
1568 VHOST_LOG_CONFIG(INFO, "get_inflight_fd queue_size: %u\n",
1569 msg->payload.inflight.queue_size);
1571 if (vq_is_packed(dev))
1572 pervq_inflight_size = get_pervq_shm_size_packed(queue_size);
1574 pervq_inflight_size = get_pervq_shm_size_split(queue_size);
1576 mmap_size = num_queues * pervq_inflight_size;
1577 addr = inflight_mem_alloc("vhost-inflight", mmap_size, &fd);
1579 VHOST_LOG_CONFIG(ERR,
1580 "failed to alloc vhost inflight area\n");
1581 msg->payload.inflight.mmap_size = 0;
1582 return RTE_VHOST_MSG_RESULT_ERR;
1584 memset(addr, 0, mmap_size);
1586 if (dev->inflight_info->addr) {
1587 munmap(dev->inflight_info->addr, dev->inflight_info->size);
1588 dev->inflight_info->addr = NULL;
1591 if (dev->inflight_info->fd >= 0) {
1592 close(dev->inflight_info->fd);
1593 dev->inflight_info->fd = -1;
1596 dev->inflight_info->addr = addr;
1597 dev->inflight_info->size = msg->payload.inflight.mmap_size = mmap_size;
1598 dev->inflight_info->fd = msg->fds[0] = fd;
1599 msg->payload.inflight.mmap_offset = 0;
1602 if (vq_is_packed(dev)) {
1603 for (i = 0; i < num_queues; i++) {
1605 (struct rte_vhost_inflight_info_packed *)addr;
1606 inflight_packed->used_wrap_counter = 1;
1607 inflight_packed->old_used_wrap_counter = 1;
1608 for (j = 0; j < queue_size; j++)
1609 inflight_packed->desc[j].next = j + 1;
1610 addr = (void *)((char *)addr + pervq_inflight_size);
1614 VHOST_LOG_CONFIG(INFO,
1615 "send inflight mmap_size: %"PRIu64"\n",
1616 msg->payload.inflight.mmap_size);
1617 VHOST_LOG_CONFIG(INFO,
1618 "send inflight mmap_offset: %"PRIu64"\n",
1619 msg->payload.inflight.mmap_offset);
1620 VHOST_LOG_CONFIG(INFO,
1621 "send inflight fd: %d\n", msg->fds[0]);
1623 return RTE_VHOST_MSG_RESULT_REPLY;
1627 vhost_user_set_inflight_fd(struct virtio_net **pdev, VhostUserMsg *msg,
1628 int main_fd __rte_unused)
1630 uint64_t mmap_size, mmap_offset;
1631 uint16_t num_queues, queue_size;
1632 struct virtio_net *dev = *pdev;
1633 uint32_t pervq_inflight_size;
1634 struct vhost_virtqueue *vq;
1637 int numa_node = SOCKET_ID_ANY;
1640 if (msg->size != sizeof(msg->payload.inflight) || fd < 0) {
1641 VHOST_LOG_CONFIG(ERR,
1642 "invalid set_inflight_fd message size is %d,fd is %d\n",
1644 return RTE_VHOST_MSG_RESULT_ERR;
1647 mmap_size = msg->payload.inflight.mmap_size;
1648 mmap_offset = msg->payload.inflight.mmap_offset;
1649 num_queues = msg->payload.inflight.num_queues;
1650 queue_size = msg->payload.inflight.queue_size;
1652 if (vq_is_packed(dev))
1653 pervq_inflight_size = get_pervq_shm_size_packed(queue_size);
1655 pervq_inflight_size = get_pervq_shm_size_split(queue_size);
1657 VHOST_LOG_CONFIG(INFO,
1658 "set_inflight_fd mmap_size: %"PRIu64"\n", mmap_size);
1659 VHOST_LOG_CONFIG(INFO,
1660 "set_inflight_fd mmap_offset: %"PRIu64"\n", mmap_offset);
1661 VHOST_LOG_CONFIG(INFO,
1662 "set_inflight_fd num_queues: %u\n", num_queues);
1663 VHOST_LOG_CONFIG(INFO,
1664 "set_inflight_fd queue_size: %u\n", queue_size);
1665 VHOST_LOG_CONFIG(INFO,
1666 "set_inflight_fd fd: %d\n", fd);
1667 VHOST_LOG_CONFIG(INFO,
1668 "set_inflight_fd pervq_inflight_size: %d\n",
1669 pervq_inflight_size);
1672 * If VQ 0 has already been allocated, try to allocate on the same
1673 * NUMA node. It can be reallocated later in numa_realloc().
1675 if (dev->nr_vring > 0)
1676 numa_node = dev->virtqueue[0]->numa_node;
1678 if (!dev->inflight_info) {
1679 dev->inflight_info = rte_zmalloc_socket("inflight_info",
1680 sizeof(struct inflight_mem_info), 0, numa_node);
1681 if (dev->inflight_info == NULL) {
1682 VHOST_LOG_CONFIG(ERR,
1683 "failed to alloc dev inflight area\n");
1684 return RTE_VHOST_MSG_RESULT_ERR;
1686 dev->inflight_info->fd = -1;
1689 if (dev->inflight_info->addr) {
1690 munmap(dev->inflight_info->addr, dev->inflight_info->size);
1691 dev->inflight_info->addr = NULL;
1694 addr = mmap(0, mmap_size, PROT_READ | PROT_WRITE, MAP_SHARED,
1696 if (addr == MAP_FAILED) {
1697 VHOST_LOG_CONFIG(ERR, "failed to mmap share memory.\n");
1698 return RTE_VHOST_MSG_RESULT_ERR;
1701 if (dev->inflight_info->fd >= 0) {
1702 close(dev->inflight_info->fd);
1703 dev->inflight_info->fd = -1;
1706 dev->inflight_info->fd = fd;
1707 dev->inflight_info->addr = addr;
1708 dev->inflight_info->size = mmap_size;
1710 for (i = 0; i < num_queues; i++) {
1711 vq = dev->virtqueue[i];
1715 if (vq_is_packed(dev)) {
1716 vq->inflight_packed = addr;
1717 vq->inflight_packed->desc_num = queue_size;
1719 vq->inflight_split = addr;
1720 vq->inflight_split->desc_num = queue_size;
1722 addr = (void *)((char *)addr + pervq_inflight_size);
1725 return RTE_VHOST_MSG_RESULT_OK;
1729 vhost_user_set_vring_call(struct virtio_net **pdev, struct VhostUserMsg *msg,
1730 int main_fd __rte_unused)
1732 struct virtio_net *dev = *pdev;
1733 struct vhost_vring_file file;
1734 struct vhost_virtqueue *vq;
1737 expected_fds = (msg->payload.u64 & VHOST_USER_VRING_NOFD_MASK) ? 0 : 1;
1738 if (validate_msg_fds(msg, expected_fds) != 0)
1739 return RTE_VHOST_MSG_RESULT_ERR;
1741 file.index = msg->payload.u64 & VHOST_USER_VRING_IDX_MASK;
1742 if (msg->payload.u64 & VHOST_USER_VRING_NOFD_MASK)
1743 file.fd = VIRTIO_INVALID_EVENTFD;
1745 file.fd = msg->fds[0];
1746 VHOST_LOG_CONFIG(INFO,
1747 "vring call idx:%d file:%d\n", file.index, file.fd);
1749 vq = dev->virtqueue[file.index];
1753 vhost_user_notify_queue_state(dev, file.index, 0);
1756 if (vq->callfd >= 0)
1759 vq->callfd = file.fd;
1761 return RTE_VHOST_MSG_RESULT_OK;
1764 static int vhost_user_set_vring_err(struct virtio_net **pdev __rte_unused,
1765 struct VhostUserMsg *msg,
1766 int main_fd __rte_unused)
1770 expected_fds = (msg->payload.u64 & VHOST_USER_VRING_NOFD_MASK) ? 0 : 1;
1771 if (validate_msg_fds(msg, expected_fds) != 0)
1772 return RTE_VHOST_MSG_RESULT_ERR;
1774 if (!(msg->payload.u64 & VHOST_USER_VRING_NOFD_MASK))
1776 VHOST_LOG_CONFIG(INFO, "not implemented\n");
1778 return RTE_VHOST_MSG_RESULT_OK;
1782 resubmit_desc_compare(const void *a, const void *b)
1784 const struct rte_vhost_resubmit_desc *desc0 = a;
1785 const struct rte_vhost_resubmit_desc *desc1 = b;
1787 if (desc1->counter > desc0->counter)
1794 vhost_check_queue_inflights_split(struct virtio_net *dev,
1795 struct vhost_virtqueue *vq)
1798 uint16_t resubmit_num = 0, last_io, num;
1799 struct vring_used *used = vq->used;
1800 struct rte_vhost_resubmit_info *resubmit;
1801 struct rte_vhost_inflight_info_split *inflight_split;
1803 if (!(dev->protocol_features &
1804 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD)))
1805 return RTE_VHOST_MSG_RESULT_OK;
1807 /* The frontend may still not support the inflight feature
1808 * although we negotiate the protocol feature.
1810 if ((!vq->inflight_split))
1811 return RTE_VHOST_MSG_RESULT_OK;
1813 if (!vq->inflight_split->version) {
1814 vq->inflight_split->version = INFLIGHT_VERSION;
1815 return RTE_VHOST_MSG_RESULT_OK;
1818 if (vq->resubmit_inflight)
1819 return RTE_VHOST_MSG_RESULT_OK;
1821 inflight_split = vq->inflight_split;
1822 vq->global_counter = 0;
1823 last_io = inflight_split->last_inflight_io;
1825 if (inflight_split->used_idx != used->idx) {
1826 inflight_split->desc[last_io].inflight = 0;
1827 rte_atomic_thread_fence(__ATOMIC_SEQ_CST);
1828 inflight_split->used_idx = used->idx;
1831 for (i = 0; i < inflight_split->desc_num; i++) {
1832 if (inflight_split->desc[i].inflight == 1)
1836 vq->last_avail_idx += resubmit_num;
1839 resubmit = rte_zmalloc_socket("resubmit", sizeof(struct rte_vhost_resubmit_info),
1842 VHOST_LOG_CONFIG(ERR,
1843 "failed to allocate memory for resubmit info.\n");
1844 return RTE_VHOST_MSG_RESULT_ERR;
1847 resubmit->resubmit_list = rte_zmalloc_socket("resubmit_list",
1848 resubmit_num * sizeof(struct rte_vhost_resubmit_desc),
1850 if (!resubmit->resubmit_list) {
1851 VHOST_LOG_CONFIG(ERR,
1852 "failed to allocate memory for inflight desc.\n");
1854 return RTE_VHOST_MSG_RESULT_ERR;
1858 for (i = 0; i < vq->inflight_split->desc_num; i++) {
1859 if (vq->inflight_split->desc[i].inflight == 1) {
1860 resubmit->resubmit_list[num].index = i;
1861 resubmit->resubmit_list[num].counter =
1862 inflight_split->desc[i].counter;
1866 resubmit->resubmit_num = num;
1868 if (resubmit->resubmit_num > 1)
1869 qsort(resubmit->resubmit_list, resubmit->resubmit_num,
1870 sizeof(struct rte_vhost_resubmit_desc),
1871 resubmit_desc_compare);
1873 vq->global_counter = resubmit->resubmit_list[0].counter + 1;
1874 vq->resubmit_inflight = resubmit;
1877 return RTE_VHOST_MSG_RESULT_OK;
1881 vhost_check_queue_inflights_packed(struct virtio_net *dev,
1882 struct vhost_virtqueue *vq)
1885 uint16_t resubmit_num = 0, old_used_idx, num;
1886 struct rte_vhost_resubmit_info *resubmit;
1887 struct rte_vhost_inflight_info_packed *inflight_packed;
1889 if (!(dev->protocol_features &
1890 (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD)))
1891 return RTE_VHOST_MSG_RESULT_OK;
1893 /* The frontend may still not support the inflight feature
1894 * although we negotiate the protocol feature.
1896 if ((!vq->inflight_packed))
1897 return RTE_VHOST_MSG_RESULT_OK;
1899 if (!vq->inflight_packed->version) {
1900 vq->inflight_packed->version = INFLIGHT_VERSION;
1901 return RTE_VHOST_MSG_RESULT_OK;
1904 if (vq->resubmit_inflight)
1905 return RTE_VHOST_MSG_RESULT_OK;
1907 inflight_packed = vq->inflight_packed;
1908 vq->global_counter = 0;
1909 old_used_idx = inflight_packed->old_used_idx;
1911 if (inflight_packed->used_idx != old_used_idx) {
1912 if (inflight_packed->desc[old_used_idx].inflight == 0) {
1913 inflight_packed->old_used_idx =
1914 inflight_packed->used_idx;
1915 inflight_packed->old_used_wrap_counter =
1916 inflight_packed->used_wrap_counter;
1917 inflight_packed->old_free_head =
1918 inflight_packed->free_head;
1920 inflight_packed->used_idx =
1921 inflight_packed->old_used_idx;
1922 inflight_packed->used_wrap_counter =
1923 inflight_packed->old_used_wrap_counter;
1924 inflight_packed->free_head =
1925 inflight_packed->old_free_head;
1929 for (i = 0; i < inflight_packed->desc_num; i++) {
1930 if (inflight_packed->desc[i].inflight == 1)
1935 resubmit = rte_zmalloc_socket("resubmit", sizeof(struct rte_vhost_resubmit_info),
1937 if (resubmit == NULL) {
1938 VHOST_LOG_CONFIG(ERR,
1939 "failed to allocate memory for resubmit info.\n");
1940 return RTE_VHOST_MSG_RESULT_ERR;
1943 resubmit->resubmit_list = rte_zmalloc_socket("resubmit_list",
1944 resubmit_num * sizeof(struct rte_vhost_resubmit_desc),
1946 if (resubmit->resubmit_list == NULL) {
1947 VHOST_LOG_CONFIG(ERR,
1948 "failed to allocate memory for resubmit desc.\n");
1950 return RTE_VHOST_MSG_RESULT_ERR;
1954 for (i = 0; i < inflight_packed->desc_num; i++) {
1955 if (vq->inflight_packed->desc[i].inflight == 1) {
1956 resubmit->resubmit_list[num].index = i;
1957 resubmit->resubmit_list[num].counter =
1958 inflight_packed->desc[i].counter;
1962 resubmit->resubmit_num = num;
1964 if (resubmit->resubmit_num > 1)
1965 qsort(resubmit->resubmit_list, resubmit->resubmit_num,
1966 sizeof(struct rte_vhost_resubmit_desc),
1967 resubmit_desc_compare);
1969 vq->global_counter = resubmit->resubmit_list[0].counter + 1;
1970 vq->resubmit_inflight = resubmit;
1973 return RTE_VHOST_MSG_RESULT_OK;
1977 vhost_user_set_vring_kick(struct virtio_net **pdev, struct VhostUserMsg *msg,
1978 int main_fd __rte_unused)
1980 struct virtio_net *dev = *pdev;
1981 struct vhost_vring_file file;
1982 struct vhost_virtqueue *vq;
1985 expected_fds = (msg->payload.u64 & VHOST_USER_VRING_NOFD_MASK) ? 0 : 1;
1986 if (validate_msg_fds(msg, expected_fds) != 0)
1987 return RTE_VHOST_MSG_RESULT_ERR;
1989 file.index = msg->payload.u64 & VHOST_USER_VRING_IDX_MASK;
1990 if (msg->payload.u64 & VHOST_USER_VRING_NOFD_MASK)
1991 file.fd = VIRTIO_INVALID_EVENTFD;
1993 file.fd = msg->fds[0];
1994 VHOST_LOG_CONFIG(INFO,
1995 "vring kick idx:%d file:%d\n", file.index, file.fd);
1997 /* Interpret ring addresses only when ring is started. */
1998 dev = translate_ring_addresses(dev, file.index);
2000 if (file.fd != VIRTIO_INVALID_EVENTFD)
2003 return RTE_VHOST_MSG_RESULT_ERR;
2008 vq = dev->virtqueue[file.index];
2011 * When VHOST_USER_F_PROTOCOL_FEATURES is not negotiated,
2012 * the ring starts already enabled. Otherwise, it is enabled via
2013 * the SET_VRING_ENABLE message.
2015 if (!(dev->features & (1ULL << VHOST_USER_F_PROTOCOL_FEATURES))) {
2021 vhost_user_notify_queue_state(dev, file.index, 0);
2024 if (vq->kickfd >= 0)
2026 vq->kickfd = file.fd;
2028 if (vq_is_packed(dev)) {
2029 if (vhost_check_queue_inflights_packed(dev, vq)) {
2030 VHOST_LOG_CONFIG(ERR,
2031 "failed to inflights for vq: %d\n", file.index);
2032 return RTE_VHOST_MSG_RESULT_ERR;
2035 if (vhost_check_queue_inflights_split(dev, vq)) {
2036 VHOST_LOG_CONFIG(ERR,
2037 "failed to inflights for vq: %d\n", file.index);
2038 return RTE_VHOST_MSG_RESULT_ERR;
2042 return RTE_VHOST_MSG_RESULT_OK;
2046 * when virtio is stopped, qemu will send us the GET_VRING_BASE message.
2049 vhost_user_get_vring_base(struct virtio_net **pdev,
2050 struct VhostUserMsg *msg,
2051 int main_fd __rte_unused)
2053 struct virtio_net *dev = *pdev;
2054 struct vhost_virtqueue *vq = dev->virtqueue[msg->payload.state.index];
2057 if (validate_msg_fds(msg, 0) != 0)
2058 return RTE_VHOST_MSG_RESULT_ERR;
2060 /* We have to stop the queue (virtio) if it is running. */
2061 vhost_destroy_device_notify(dev);
2063 dev->flags &= ~VIRTIO_DEV_READY;
2064 dev->flags &= ~VIRTIO_DEV_VDPA_CONFIGURED;
2066 /* Here we are safe to get the indexes */
2067 if (vq_is_packed(dev)) {
2069 * Bit[0:14]: avail index
2070 * Bit[15]: avail wrap counter
2072 val = vq->last_avail_idx & 0x7fff;
2073 val |= vq->avail_wrap_counter << 15;
2074 msg->payload.state.num = val;
2076 msg->payload.state.num = vq->last_avail_idx;
2079 VHOST_LOG_CONFIG(INFO,
2080 "vring base idx:%d file:%d\n", msg->payload.state.index,
2081 msg->payload.state.num);
2083 * Based on current qemu vhost-user implementation, this message is
2084 * sent and only sent in vhost_vring_stop.
2085 * TODO: cleanup the vring, it isn't usable since here.
2087 if (vq->kickfd >= 0)
2090 vq->kickfd = VIRTIO_UNINITIALIZED_EVENTFD;
2092 if (vq->callfd >= 0)
2095 vq->callfd = VIRTIO_UNINITIALIZED_EVENTFD;
2097 vq->signalled_used_valid = false;
2099 if (vq_is_packed(dev)) {
2100 rte_free(vq->shadow_used_packed);
2101 vq->shadow_used_packed = NULL;
2103 rte_free(vq->shadow_used_split);
2104 vq->shadow_used_split = NULL;
2107 rte_free(vq->batch_copy_elems);
2108 vq->batch_copy_elems = NULL;
2110 rte_free(vq->log_cache);
2111 vq->log_cache = NULL;
2113 msg->size = sizeof(msg->payload.state);
2116 vring_invalidate(dev, vq);
2118 return RTE_VHOST_MSG_RESULT_REPLY;
2122 * when virtio queues are ready to work, qemu will send us to
2123 * enable the virtio queue pair.
2126 vhost_user_set_vring_enable(struct virtio_net **pdev,
2127 struct VhostUserMsg *msg,
2128 int main_fd __rte_unused)
2130 struct virtio_net *dev = *pdev;
2131 bool enable = !!msg->payload.state.num;
2132 int index = (int)msg->payload.state.index;
2134 if (validate_msg_fds(msg, 0) != 0)
2135 return RTE_VHOST_MSG_RESULT_ERR;
2137 VHOST_LOG_CONFIG(INFO,
2138 "set queue enable: %d to qp idx: %d\n",
2141 if (enable && dev->virtqueue[index]->async_registered) {
2142 if (dev->virtqueue[index]->async_pkts_inflight_n) {
2143 VHOST_LOG_CONFIG(ERR, "failed to enable vring. "
2144 "async inflight packets must be completed first\n");
2145 return RTE_VHOST_MSG_RESULT_ERR;
2149 dev->virtqueue[index]->enabled = enable;
2151 return RTE_VHOST_MSG_RESULT_OK;
2155 vhost_user_get_protocol_features(struct virtio_net **pdev,
2156 struct VhostUserMsg *msg,
2157 int main_fd __rte_unused)
2159 struct virtio_net *dev = *pdev;
2160 uint64_t features, protocol_features;
2162 if (validate_msg_fds(msg, 0) != 0)
2163 return RTE_VHOST_MSG_RESULT_ERR;
2165 rte_vhost_driver_get_features(dev->ifname, &features);
2166 rte_vhost_driver_get_protocol_features(dev->ifname, &protocol_features);
2168 msg->payload.u64 = protocol_features;
2169 msg->size = sizeof(msg->payload.u64);
2172 return RTE_VHOST_MSG_RESULT_REPLY;
2176 vhost_user_set_protocol_features(struct virtio_net **pdev,
2177 struct VhostUserMsg *msg,
2178 int main_fd __rte_unused)
2180 struct virtio_net *dev = *pdev;
2181 uint64_t protocol_features = msg->payload.u64;
2182 uint64_t slave_protocol_features = 0;
2184 if (validate_msg_fds(msg, 0) != 0)
2185 return RTE_VHOST_MSG_RESULT_ERR;
2187 rte_vhost_driver_get_protocol_features(dev->ifname,
2188 &slave_protocol_features);
2189 if (protocol_features & ~slave_protocol_features) {
2190 VHOST_LOG_CONFIG(ERR,
2191 "(%d) received invalid protocol features.\n",
2193 return RTE_VHOST_MSG_RESULT_ERR;
2196 dev->protocol_features = protocol_features;
2197 VHOST_LOG_CONFIG(INFO,
2198 "negotiated Vhost-user protocol features: 0x%" PRIx64 "\n",
2199 dev->protocol_features);
2201 return RTE_VHOST_MSG_RESULT_OK;
2205 vhost_user_set_log_base(struct virtio_net **pdev, struct VhostUserMsg *msg,
2206 int main_fd __rte_unused)
2208 struct virtio_net *dev = *pdev;
2209 int fd = msg->fds[0];
2214 if (validate_msg_fds(msg, 1) != 0)
2215 return RTE_VHOST_MSG_RESULT_ERR;
2218 VHOST_LOG_CONFIG(ERR, "invalid log fd: %d\n", fd);
2219 return RTE_VHOST_MSG_RESULT_ERR;
2222 if (msg->size != sizeof(VhostUserLog)) {
2223 VHOST_LOG_CONFIG(ERR,
2224 "invalid log base msg size: %"PRId32" != %d\n",
2225 msg->size, (int)sizeof(VhostUserLog));
2229 size = msg->payload.log.mmap_size;
2230 off = msg->payload.log.mmap_offset;
2232 /* Check for mmap size and offset overflow. */
2234 VHOST_LOG_CONFIG(ERR,
2235 "log offset %#"PRIx64" and log size %#"PRIx64" overflow\n",
2240 VHOST_LOG_CONFIG(INFO,
2241 "log mmap size: %"PRId64", offset: %"PRId64"\n",
2245 * mmap from 0 to workaround a hugepage mmap bug: mmap will
2246 * fail when offset is not page size aligned.
2248 addr = mmap(0, size + off, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
2250 if (addr == MAP_FAILED) {
2251 VHOST_LOG_CONFIG(ERR, "mmap log base failed!\n");
2252 return RTE_VHOST_MSG_RESULT_ERR;
2256 * Free previously mapped log memory on occasionally
2257 * multiple VHOST_USER_SET_LOG_BASE.
2259 if (dev->log_addr) {
2260 munmap((void *)(uintptr_t)dev->log_addr, dev->log_size);
2262 dev->log_addr = (uint64_t)(uintptr_t)addr;
2263 dev->log_base = dev->log_addr + off;
2264 dev->log_size = size;
2266 for (i = 0; i < dev->nr_vring; i++) {
2267 struct vhost_virtqueue *vq = dev->virtqueue[i];
2269 rte_free(vq->log_cache);
2270 vq->log_cache = NULL;
2271 vq->log_cache_nb_elem = 0;
2272 vq->log_cache = rte_malloc_socket("vq log cache",
2273 sizeof(struct log_cache_entry) * VHOST_LOG_CACHE_NR,
2276 * If log cache alloc fail, don't fail migration, but no
2277 * caching will be done, which will impact performance
2280 VHOST_LOG_CONFIG(ERR, "Failed to allocate VQ logging cache\n");
2284 * The spec is not clear about it (yet), but QEMU doesn't expect
2285 * any payload in the reply.
2290 return RTE_VHOST_MSG_RESULT_REPLY;
2294 return RTE_VHOST_MSG_RESULT_ERR;
2297 static int vhost_user_set_log_fd(struct virtio_net **pdev __rte_unused,
2298 struct VhostUserMsg *msg,
2299 int main_fd __rte_unused)
2301 if (validate_msg_fds(msg, 1) != 0)
2302 return RTE_VHOST_MSG_RESULT_ERR;
2305 VHOST_LOG_CONFIG(INFO, "not implemented.\n");
2307 return RTE_VHOST_MSG_RESULT_OK;
2311 * An rarp packet is constructed and broadcasted to notify switches about
2312 * the new location of the migrated VM, so that packets from outside will
2313 * not be lost after migration.
2315 * However, we don't actually "send" a rarp packet here, instead, we set
2316 * a flag 'broadcast_rarp' to let rte_vhost_dequeue_burst() inject it.
2319 vhost_user_send_rarp(struct virtio_net **pdev, struct VhostUserMsg *msg,
2320 int main_fd __rte_unused)
2322 struct virtio_net *dev = *pdev;
2323 uint8_t *mac = (uint8_t *)&msg->payload.u64;
2324 struct rte_vdpa_device *vdpa_dev;
2326 if (validate_msg_fds(msg, 0) != 0)
2327 return RTE_VHOST_MSG_RESULT_ERR;
2329 VHOST_LOG_CONFIG(DEBUG,
2330 ":: mac: " RTE_ETHER_ADDR_PRT_FMT "\n",
2331 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
2332 memcpy(dev->mac.addr_bytes, mac, 6);
2335 * Set the flag to inject a RARP broadcast packet at
2336 * rte_vhost_dequeue_burst().
2338 * __ATOMIC_RELEASE ordering is for making sure the mac is
2339 * copied before the flag is set.
2341 __atomic_store_n(&dev->broadcast_rarp, 1, __ATOMIC_RELEASE);
2342 vdpa_dev = dev->vdpa_dev;
2343 if (vdpa_dev && vdpa_dev->ops->migration_done)
2344 vdpa_dev->ops->migration_done(dev->vid);
2346 return RTE_VHOST_MSG_RESULT_OK;
2350 vhost_user_net_set_mtu(struct virtio_net **pdev, struct VhostUserMsg *msg,
2351 int main_fd __rte_unused)
2353 struct virtio_net *dev = *pdev;
2355 if (validate_msg_fds(msg, 0) != 0)
2356 return RTE_VHOST_MSG_RESULT_ERR;
2358 if (msg->payload.u64 < VIRTIO_MIN_MTU ||
2359 msg->payload.u64 > VIRTIO_MAX_MTU) {
2360 VHOST_LOG_CONFIG(ERR, "Invalid MTU size (%"PRIu64")\n",
2363 return RTE_VHOST_MSG_RESULT_ERR;
2366 dev->mtu = msg->payload.u64;
2368 return RTE_VHOST_MSG_RESULT_OK;
2372 vhost_user_set_req_fd(struct virtio_net **pdev, struct VhostUserMsg *msg,
2373 int main_fd __rte_unused)
2375 struct virtio_net *dev = *pdev;
2376 int fd = msg->fds[0];
2378 if (validate_msg_fds(msg, 1) != 0)
2379 return RTE_VHOST_MSG_RESULT_ERR;
2382 VHOST_LOG_CONFIG(ERR,
2383 "Invalid file descriptor for slave channel (%d)\n",
2385 return RTE_VHOST_MSG_RESULT_ERR;
2388 if (dev->slave_req_fd >= 0)
2389 close(dev->slave_req_fd);
2391 dev->slave_req_fd = fd;
2393 return RTE_VHOST_MSG_RESULT_OK;
2397 is_vring_iotlb_split(struct vhost_virtqueue *vq, struct vhost_iotlb_msg *imsg)
2399 struct vhost_vring_addr *ra;
2400 uint64_t start, end, len;
2403 end = start + imsg->size;
2405 ra = &vq->ring_addrs;
2406 len = sizeof(struct vring_desc) * vq->size;
2407 if (ra->desc_user_addr < end && (ra->desc_user_addr + len) > start)
2410 len = sizeof(struct vring_avail) + sizeof(uint16_t) * vq->size;
2411 if (ra->avail_user_addr < end && (ra->avail_user_addr + len) > start)
2414 len = sizeof(struct vring_used) +
2415 sizeof(struct vring_used_elem) * vq->size;
2416 if (ra->used_user_addr < end && (ra->used_user_addr + len) > start)
2419 if (ra->flags & (1 << VHOST_VRING_F_LOG)) {
2420 len = sizeof(uint64_t);
2421 if (ra->log_guest_addr < end &&
2422 (ra->log_guest_addr + len) > start)
2430 is_vring_iotlb_packed(struct vhost_virtqueue *vq, struct vhost_iotlb_msg *imsg)
2432 struct vhost_vring_addr *ra;
2433 uint64_t start, end, len;
2436 end = start + imsg->size;
2438 ra = &vq->ring_addrs;
2439 len = sizeof(struct vring_packed_desc) * vq->size;
2440 if (ra->desc_user_addr < end && (ra->desc_user_addr + len) > start)
2443 len = sizeof(struct vring_packed_desc_event);
2444 if (ra->avail_user_addr < end && (ra->avail_user_addr + len) > start)
2447 len = sizeof(struct vring_packed_desc_event);
2448 if (ra->used_user_addr < end && (ra->used_user_addr + len) > start)
2451 if (ra->flags & (1 << VHOST_VRING_F_LOG)) {
2452 len = sizeof(uint64_t);
2453 if (ra->log_guest_addr < end &&
2454 (ra->log_guest_addr + len) > start)
2461 static int is_vring_iotlb(struct virtio_net *dev,
2462 struct vhost_virtqueue *vq,
2463 struct vhost_iotlb_msg *imsg)
2465 if (vq_is_packed(dev))
2466 return is_vring_iotlb_packed(vq, imsg);
2468 return is_vring_iotlb_split(vq, imsg);
2472 vhost_user_iotlb_msg(struct virtio_net **pdev, struct VhostUserMsg *msg,
2473 int main_fd __rte_unused)
2475 struct virtio_net *dev = *pdev;
2476 struct vhost_iotlb_msg *imsg = &msg->payload.iotlb;
2480 if (validate_msg_fds(msg, 0) != 0)
2481 return RTE_VHOST_MSG_RESULT_ERR;
2483 switch (imsg->type) {
2484 case VHOST_IOTLB_UPDATE:
2486 vva = qva_to_vva(dev, imsg->uaddr, &len);
2488 return RTE_VHOST_MSG_RESULT_ERR;
2490 for (i = 0; i < dev->nr_vring; i++) {
2491 struct vhost_virtqueue *vq = dev->virtqueue[i];
2496 vhost_user_iotlb_cache_insert(vq, imsg->iova, vva,
2499 if (is_vring_iotlb(dev, vq, imsg))
2500 *pdev = dev = translate_ring_addresses(dev, i);
2503 case VHOST_IOTLB_INVALIDATE:
2504 for (i = 0; i < dev->nr_vring; i++) {
2505 struct vhost_virtqueue *vq = dev->virtqueue[i];
2510 vhost_user_iotlb_cache_remove(vq, imsg->iova,
2513 if (is_vring_iotlb(dev, vq, imsg))
2514 vring_invalidate(dev, vq);
2518 VHOST_LOG_CONFIG(ERR, "Invalid IOTLB message type (%d)\n",
2520 return RTE_VHOST_MSG_RESULT_ERR;
2523 return RTE_VHOST_MSG_RESULT_OK;
2527 vhost_user_set_postcopy_advise(struct virtio_net **pdev,
2528 struct VhostUserMsg *msg,
2529 int main_fd __rte_unused)
2531 struct virtio_net *dev = *pdev;
2532 #ifdef RTE_LIBRTE_VHOST_POSTCOPY
2533 struct uffdio_api api_struct;
2535 if (validate_msg_fds(msg, 0) != 0)
2536 return RTE_VHOST_MSG_RESULT_ERR;
2538 dev->postcopy_ufd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK);
2540 if (dev->postcopy_ufd == -1) {
2541 VHOST_LOG_CONFIG(ERR, "Userfaultfd not available: %s\n",
2543 return RTE_VHOST_MSG_RESULT_ERR;
2545 api_struct.api = UFFD_API;
2546 api_struct.features = 0;
2547 if (ioctl(dev->postcopy_ufd, UFFDIO_API, &api_struct)) {
2548 VHOST_LOG_CONFIG(ERR, "UFFDIO_API ioctl failure: %s\n",
2550 close(dev->postcopy_ufd);
2551 dev->postcopy_ufd = -1;
2552 return RTE_VHOST_MSG_RESULT_ERR;
2554 msg->fds[0] = dev->postcopy_ufd;
2557 return RTE_VHOST_MSG_RESULT_REPLY;
2559 dev->postcopy_ufd = -1;
2562 return RTE_VHOST_MSG_RESULT_ERR;
2567 vhost_user_set_postcopy_listen(struct virtio_net **pdev,
2568 struct VhostUserMsg *msg __rte_unused,
2569 int main_fd __rte_unused)
2571 struct virtio_net *dev = *pdev;
2573 if (validate_msg_fds(msg, 0) != 0)
2574 return RTE_VHOST_MSG_RESULT_ERR;
2576 if (dev->mem && dev->mem->nregions) {
2577 VHOST_LOG_CONFIG(ERR,
2578 "Regions already registered at postcopy-listen\n");
2579 return RTE_VHOST_MSG_RESULT_ERR;
2581 dev->postcopy_listening = 1;
2583 return RTE_VHOST_MSG_RESULT_OK;
2587 vhost_user_postcopy_end(struct virtio_net **pdev, struct VhostUserMsg *msg,
2588 int main_fd __rte_unused)
2590 struct virtio_net *dev = *pdev;
2592 if (validate_msg_fds(msg, 0) != 0)
2593 return RTE_VHOST_MSG_RESULT_ERR;
2595 dev->postcopy_listening = 0;
2596 if (dev->postcopy_ufd >= 0) {
2597 close(dev->postcopy_ufd);
2598 dev->postcopy_ufd = -1;
2601 msg->payload.u64 = 0;
2602 msg->size = sizeof(msg->payload.u64);
2605 return RTE_VHOST_MSG_RESULT_REPLY;
2609 vhost_user_get_status(struct virtio_net **pdev, struct VhostUserMsg *msg,
2610 int main_fd __rte_unused)
2612 struct virtio_net *dev = *pdev;
2614 if (validate_msg_fds(msg, 0) != 0)
2615 return RTE_VHOST_MSG_RESULT_ERR;
2617 msg->payload.u64 = dev->status;
2618 msg->size = sizeof(msg->payload.u64);
2621 return RTE_VHOST_MSG_RESULT_REPLY;
2625 vhost_user_set_status(struct virtio_net **pdev, struct VhostUserMsg *msg,
2626 int main_fd __rte_unused)
2628 struct virtio_net *dev = *pdev;
2630 if (validate_msg_fds(msg, 0) != 0)
2631 return RTE_VHOST_MSG_RESULT_ERR;
2633 /* As per Virtio specification, the device status is 8bits long */
2634 if (msg->payload.u64 > UINT8_MAX) {
2635 VHOST_LOG_CONFIG(ERR, "Invalid VHOST_USER_SET_STATUS payload 0x%" PRIx64 "\n",
2637 return RTE_VHOST_MSG_RESULT_ERR;
2640 dev->status = msg->payload.u64;
2642 if ((dev->status & VIRTIO_DEVICE_STATUS_FEATURES_OK) &&
2643 (dev->flags & VIRTIO_DEV_FEATURES_FAILED)) {
2644 VHOST_LOG_CONFIG(ERR, "FEATURES_OK bit is set but feature negotiation failed\n");
2646 * Clear the bit to let the driver know about the feature
2647 * negotiation failure
2649 dev->status &= ~VIRTIO_DEVICE_STATUS_FEATURES_OK;
2652 VHOST_LOG_CONFIG(INFO, "New device status(0x%08x):\n"
2654 "\t-ACKNOWLEDGE: %u\n"
2656 "\t-FEATURES_OK: %u\n"
2657 "\t-DRIVER_OK: %u\n"
2658 "\t-DEVICE_NEED_RESET: %u\n"
2661 (dev->status == VIRTIO_DEVICE_STATUS_RESET),
2662 !!(dev->status & VIRTIO_DEVICE_STATUS_ACK),
2663 !!(dev->status & VIRTIO_DEVICE_STATUS_DRIVER),
2664 !!(dev->status & VIRTIO_DEVICE_STATUS_FEATURES_OK),
2665 !!(dev->status & VIRTIO_DEVICE_STATUS_DRIVER_OK),
2666 !!(dev->status & VIRTIO_DEVICE_STATUS_DEV_NEED_RESET),
2667 !!(dev->status & VIRTIO_DEVICE_STATUS_FAILED));
2669 return RTE_VHOST_MSG_RESULT_OK;
2672 typedef int (*vhost_message_handler_t)(struct virtio_net **pdev,
2673 struct VhostUserMsg *msg,
2675 static vhost_message_handler_t vhost_message_handlers[VHOST_USER_MAX] = {
2676 [VHOST_USER_NONE] = NULL,
2677 [VHOST_USER_GET_FEATURES] = vhost_user_get_features,
2678 [VHOST_USER_SET_FEATURES] = vhost_user_set_features,
2679 [VHOST_USER_SET_OWNER] = vhost_user_set_owner,
2680 [VHOST_USER_RESET_OWNER] = vhost_user_reset_owner,
2681 [VHOST_USER_SET_MEM_TABLE] = vhost_user_set_mem_table,
2682 [VHOST_USER_SET_LOG_BASE] = vhost_user_set_log_base,
2683 [VHOST_USER_SET_LOG_FD] = vhost_user_set_log_fd,
2684 [VHOST_USER_SET_VRING_NUM] = vhost_user_set_vring_num,
2685 [VHOST_USER_SET_VRING_ADDR] = vhost_user_set_vring_addr,
2686 [VHOST_USER_SET_VRING_BASE] = vhost_user_set_vring_base,
2687 [VHOST_USER_GET_VRING_BASE] = vhost_user_get_vring_base,
2688 [VHOST_USER_SET_VRING_KICK] = vhost_user_set_vring_kick,
2689 [VHOST_USER_SET_VRING_CALL] = vhost_user_set_vring_call,
2690 [VHOST_USER_SET_VRING_ERR] = vhost_user_set_vring_err,
2691 [VHOST_USER_GET_PROTOCOL_FEATURES] = vhost_user_get_protocol_features,
2692 [VHOST_USER_SET_PROTOCOL_FEATURES] = vhost_user_set_protocol_features,
2693 [VHOST_USER_GET_QUEUE_NUM] = vhost_user_get_queue_num,
2694 [VHOST_USER_SET_VRING_ENABLE] = vhost_user_set_vring_enable,
2695 [VHOST_USER_SEND_RARP] = vhost_user_send_rarp,
2696 [VHOST_USER_NET_SET_MTU] = vhost_user_net_set_mtu,
2697 [VHOST_USER_SET_SLAVE_REQ_FD] = vhost_user_set_req_fd,
2698 [VHOST_USER_IOTLB_MSG] = vhost_user_iotlb_msg,
2699 [VHOST_USER_POSTCOPY_ADVISE] = vhost_user_set_postcopy_advise,
2700 [VHOST_USER_POSTCOPY_LISTEN] = vhost_user_set_postcopy_listen,
2701 [VHOST_USER_POSTCOPY_END] = vhost_user_postcopy_end,
2702 [VHOST_USER_GET_INFLIGHT_FD] = vhost_user_get_inflight_fd,
2703 [VHOST_USER_SET_INFLIGHT_FD] = vhost_user_set_inflight_fd,
2704 [VHOST_USER_SET_STATUS] = vhost_user_set_status,
2705 [VHOST_USER_GET_STATUS] = vhost_user_get_status,
2708 /* return bytes# of read on success or negative val on failure. */
2710 read_vhost_message(int sockfd, struct VhostUserMsg *msg)
2714 ret = read_fd_message(sockfd, (char *)msg, VHOST_USER_HDR_SIZE,
2715 msg->fds, VHOST_MEMORY_MAX_NREGIONS, &msg->fd_num);
2718 } else if (ret != VHOST_USER_HDR_SIZE) {
2719 VHOST_LOG_CONFIG(ERR, "Unexpected header size read\n");
2725 if (msg->size > sizeof(msg->payload)) {
2726 VHOST_LOG_CONFIG(ERR,
2727 "invalid msg size: %d\n", msg->size);
2730 ret = read(sockfd, &msg->payload, msg->size);
2733 if (ret != (int)msg->size) {
2734 VHOST_LOG_CONFIG(ERR,
2735 "read control message failed\n");
2744 send_vhost_message(int sockfd, struct VhostUserMsg *msg)
2749 return send_fd_message(sockfd, (char *)msg,
2750 VHOST_USER_HDR_SIZE + msg->size, msg->fds, msg->fd_num);
2754 send_vhost_reply(int sockfd, struct VhostUserMsg *msg)
2759 msg->flags &= ~VHOST_USER_VERSION_MASK;
2760 msg->flags &= ~VHOST_USER_NEED_REPLY;
2761 msg->flags |= VHOST_USER_VERSION;
2762 msg->flags |= VHOST_USER_REPLY_MASK;
2764 return send_vhost_message(sockfd, msg);
2768 send_vhost_slave_message(struct virtio_net *dev, struct VhostUserMsg *msg)
2772 if (msg->flags & VHOST_USER_NEED_REPLY)
2773 rte_spinlock_lock(&dev->slave_req_lock);
2775 ret = send_vhost_message(dev->slave_req_fd, msg);
2776 if (ret < 0 && (msg->flags & VHOST_USER_NEED_REPLY))
2777 rte_spinlock_unlock(&dev->slave_req_lock);
2783 * Allocate a queue pair if it hasn't been allocated yet
2786 vhost_user_check_and_alloc_queue_pair(struct virtio_net *dev,
2787 struct VhostUserMsg *msg)
2791 switch (msg->request.master) {
2792 case VHOST_USER_SET_VRING_KICK:
2793 case VHOST_USER_SET_VRING_CALL:
2794 case VHOST_USER_SET_VRING_ERR:
2795 vring_idx = msg->payload.u64 & VHOST_USER_VRING_IDX_MASK;
2797 case VHOST_USER_SET_VRING_NUM:
2798 case VHOST_USER_SET_VRING_BASE:
2799 case VHOST_USER_GET_VRING_BASE:
2800 case VHOST_USER_SET_VRING_ENABLE:
2801 vring_idx = msg->payload.state.index;
2803 case VHOST_USER_SET_VRING_ADDR:
2804 vring_idx = msg->payload.addr.index;
2810 if (vring_idx >= VHOST_MAX_VRING) {
2811 VHOST_LOG_CONFIG(ERR,
2812 "invalid vring index: %u\n", vring_idx);
2816 if (dev->virtqueue[vring_idx])
2819 return alloc_vring_queue(dev, vring_idx);
2823 vhost_user_lock_all_queue_pairs(struct virtio_net *dev)
2826 unsigned int vq_num = 0;
2828 while (vq_num < dev->nr_vring) {
2829 struct vhost_virtqueue *vq = dev->virtqueue[i];
2832 rte_spinlock_lock(&vq->access_lock);
2840 vhost_user_unlock_all_queue_pairs(struct virtio_net *dev)
2843 unsigned int vq_num = 0;
2845 while (vq_num < dev->nr_vring) {
2846 struct vhost_virtqueue *vq = dev->virtqueue[i];
2849 rte_spinlock_unlock(&vq->access_lock);
2857 vhost_user_msg_handler(int vid, int fd)
2859 struct virtio_net *dev;
2860 struct VhostUserMsg msg;
2861 struct rte_vdpa_device *vdpa_dev;
2863 int unlock_required = 0;
2868 dev = get_device(vid);
2872 if (!dev->notify_ops) {
2873 dev->notify_ops = vhost_driver_callback_get(dev->ifname);
2874 if (!dev->notify_ops) {
2875 VHOST_LOG_CONFIG(ERR,
2876 "failed to get callback ops for driver %s\n",
2882 ret = read_vhost_message(fd, &msg);
2885 VHOST_LOG_CONFIG(ERR,
2886 "vhost read message failed\n");
2888 VHOST_LOG_CONFIG(INFO,
2889 "vhost peer closed\n");
2895 request = msg.request.master;
2896 if (request > VHOST_USER_NONE && request < VHOST_USER_MAX &&
2897 vhost_message_str[request]) {
2898 if (request != VHOST_USER_IOTLB_MSG)
2899 VHOST_LOG_CONFIG(INFO, "read message %s\n",
2900 vhost_message_str[request]);
2902 VHOST_LOG_CONFIG(DEBUG, "read message %s\n",
2903 vhost_message_str[request]);
2905 VHOST_LOG_CONFIG(DEBUG, "External request %d\n", request);
2908 ret = vhost_user_check_and_alloc_queue_pair(dev, &msg);
2910 VHOST_LOG_CONFIG(ERR,
2911 "failed to alloc queue\n");
2916 * Note: we don't lock all queues on VHOST_USER_GET_VRING_BASE
2917 * and VHOST_USER_RESET_OWNER, since it is sent when virtio stops
2918 * and device is destroyed. destroy_device waits for queues to be
2919 * inactive, so it is safe. Otherwise taking the access_lock
2920 * would cause a dead lock.
2923 case VHOST_USER_SET_FEATURES:
2924 case VHOST_USER_SET_PROTOCOL_FEATURES:
2925 case VHOST_USER_SET_OWNER:
2926 case VHOST_USER_SET_MEM_TABLE:
2927 case VHOST_USER_SET_LOG_BASE:
2928 case VHOST_USER_SET_LOG_FD:
2929 case VHOST_USER_SET_VRING_NUM:
2930 case VHOST_USER_SET_VRING_ADDR:
2931 case VHOST_USER_SET_VRING_BASE:
2932 case VHOST_USER_SET_VRING_KICK:
2933 case VHOST_USER_SET_VRING_CALL:
2934 case VHOST_USER_SET_VRING_ERR:
2935 case VHOST_USER_SET_VRING_ENABLE:
2936 case VHOST_USER_SEND_RARP:
2937 case VHOST_USER_NET_SET_MTU:
2938 case VHOST_USER_SET_SLAVE_REQ_FD:
2939 if (!(dev->flags & VIRTIO_DEV_VDPA_CONFIGURED)) {
2940 vhost_user_lock_all_queue_pairs(dev);
2941 unlock_required = 1;
2950 if (dev->extern_ops.pre_msg_handle) {
2951 ret = (*dev->extern_ops.pre_msg_handle)(dev->vid,
2954 case RTE_VHOST_MSG_RESULT_REPLY:
2955 send_vhost_reply(fd, &msg);
2957 case RTE_VHOST_MSG_RESULT_ERR:
2958 case RTE_VHOST_MSG_RESULT_OK:
2960 goto skip_to_post_handle;
2961 case RTE_VHOST_MSG_RESULT_NOT_HANDLED:
2967 if (request > VHOST_USER_NONE && request < VHOST_USER_MAX) {
2968 if (!vhost_message_handlers[request])
2969 goto skip_to_post_handle;
2970 ret = vhost_message_handlers[request](&dev, &msg, fd);
2973 case RTE_VHOST_MSG_RESULT_ERR:
2974 VHOST_LOG_CONFIG(ERR,
2975 "Processing %s failed.\n",
2976 vhost_message_str[request]);
2979 case RTE_VHOST_MSG_RESULT_OK:
2980 VHOST_LOG_CONFIG(DEBUG,
2981 "Processing %s succeeded.\n",
2982 vhost_message_str[request]);
2985 case RTE_VHOST_MSG_RESULT_REPLY:
2986 VHOST_LOG_CONFIG(DEBUG,
2987 "Processing %s succeeded and needs reply.\n",
2988 vhost_message_str[request]);
2989 send_vhost_reply(fd, &msg);
2997 skip_to_post_handle:
2998 if (ret != RTE_VHOST_MSG_RESULT_ERR &&
2999 dev->extern_ops.post_msg_handle) {
3000 ret = (*dev->extern_ops.post_msg_handle)(dev->vid,
3003 case RTE_VHOST_MSG_RESULT_REPLY:
3004 send_vhost_reply(fd, &msg);
3006 case RTE_VHOST_MSG_RESULT_ERR:
3007 case RTE_VHOST_MSG_RESULT_OK:
3009 case RTE_VHOST_MSG_RESULT_NOT_HANDLED:
3015 /* If message was not handled at this stage, treat it as an error */
3017 VHOST_LOG_CONFIG(ERR,
3018 "vhost message (req: %d) was not handled.\n", request);
3019 close_msg_fds(&msg);
3020 ret = RTE_VHOST_MSG_RESULT_ERR;
3024 * If the request required a reply that was already sent,
3025 * this optional reply-ack won't be sent as the
3026 * VHOST_USER_NEED_REPLY was cleared in send_vhost_reply().
3028 if (msg.flags & VHOST_USER_NEED_REPLY) {
3029 msg.payload.u64 = ret == RTE_VHOST_MSG_RESULT_ERR;
3030 msg.size = sizeof(msg.payload.u64);
3032 send_vhost_reply(fd, &msg);
3033 } else if (ret == RTE_VHOST_MSG_RESULT_ERR) {
3034 VHOST_LOG_CONFIG(ERR,
3035 "vhost message handling failed.\n");
3039 for (i = 0; i < dev->nr_vring; i++) {
3040 struct vhost_virtqueue *vq = dev->virtqueue[i];
3041 bool cur_ready = vq_is_ready(dev, vq);
3043 if (cur_ready != (vq && vq->ready)) {
3044 vq->ready = cur_ready;
3045 vhost_user_notify_queue_state(dev, i, cur_ready);
3049 if (unlock_required)
3050 vhost_user_unlock_all_queue_pairs(dev);
3052 if (!virtio_is_ready(dev))
3056 * Virtio is now ready. If not done already, it is time
3057 * to notify the application it can process the rings and
3058 * configure the vDPA device if present.
3061 if (!(dev->flags & VIRTIO_DEV_RUNNING)) {
3062 if (dev->notify_ops->new_device(dev->vid) == 0)
3063 dev->flags |= VIRTIO_DEV_RUNNING;
3066 vdpa_dev = dev->vdpa_dev;
3070 if (!(dev->flags & VIRTIO_DEV_VDPA_CONFIGURED)) {
3071 if (vdpa_dev->ops->dev_conf(dev->vid))
3072 VHOST_LOG_CONFIG(ERR,
3073 "Failed to configure vDPA device\n");
3075 dev->flags |= VIRTIO_DEV_VDPA_CONFIGURED;
3082 static int process_slave_message_reply(struct virtio_net *dev,
3083 const struct VhostUserMsg *msg)
3085 struct VhostUserMsg msg_reply;
3088 if ((msg->flags & VHOST_USER_NEED_REPLY) == 0)
3091 ret = read_vhost_message(dev->slave_req_fd, &msg_reply);
3094 VHOST_LOG_CONFIG(ERR,
3095 "vhost read slave message reply failed\n");
3097 VHOST_LOG_CONFIG(INFO,
3098 "vhost peer closed\n");
3104 if (msg_reply.request.slave != msg->request.slave) {
3105 VHOST_LOG_CONFIG(ERR,
3106 "Received unexpected msg type (%u), expected %u\n",
3107 msg_reply.request.slave, msg->request.slave);
3112 ret = msg_reply.payload.u64 ? -1 : 0;
3115 rte_spinlock_unlock(&dev->slave_req_lock);
3120 vhost_user_iotlb_miss(struct virtio_net *dev, uint64_t iova, uint8_t perm)
3123 struct VhostUserMsg msg = {
3124 .request.slave = VHOST_USER_SLAVE_IOTLB_MSG,
3125 .flags = VHOST_USER_VERSION,
3126 .size = sizeof(msg.payload.iotlb),
3130 .type = VHOST_IOTLB_MISS,
3134 ret = send_vhost_message(dev->slave_req_fd, &msg);
3136 VHOST_LOG_CONFIG(ERR,
3137 "Failed to send IOTLB miss message (%d)\n",
3146 vhost_user_slave_config_change(struct virtio_net *dev, bool need_reply)
3149 struct VhostUserMsg msg = {
3150 .request.slave = VHOST_USER_SLAVE_CONFIG_CHANGE_MSG,
3151 .flags = VHOST_USER_VERSION,
3156 msg.flags |= VHOST_USER_NEED_REPLY;
3158 ret = send_vhost_slave_message(dev, &msg);
3160 VHOST_LOG_CONFIG(ERR,
3161 "Failed to send config change (%d)\n",
3166 return process_slave_message_reply(dev, &msg);
3170 rte_vhost_slave_config_change(int vid, bool need_reply)
3172 struct virtio_net *dev;
3174 dev = get_device(vid);
3178 return vhost_user_slave_config_change(dev, need_reply);
3181 static int vhost_user_slave_set_vring_host_notifier(struct virtio_net *dev,
3187 struct VhostUserMsg msg = {
3188 .request.slave = VHOST_USER_SLAVE_VRING_HOST_NOTIFIER_MSG,
3189 .flags = VHOST_USER_VERSION | VHOST_USER_NEED_REPLY,
3190 .size = sizeof(msg.payload.area),
3192 .u64 = index & VHOST_USER_VRING_IDX_MASK,
3199 msg.payload.area.u64 |= VHOST_USER_VRING_NOFD_MASK;
3205 ret = send_vhost_slave_message(dev, &msg);
3207 VHOST_LOG_CONFIG(ERR,
3208 "Failed to set host notifier (%d)\n", ret);
3212 return process_slave_message_reply(dev, &msg);
3215 int rte_vhost_host_notifier_ctrl(int vid, uint16_t qid, bool enable)
3217 struct virtio_net *dev;
3218 struct rte_vdpa_device *vdpa_dev;
3219 int vfio_device_fd, ret = 0;
3220 uint64_t offset, size;
3221 unsigned int i, q_start, q_last;
3223 dev = get_device(vid);
3227 vdpa_dev = dev->vdpa_dev;
3228 if (vdpa_dev == NULL)
3231 if (!(dev->features & (1ULL << VIRTIO_F_VERSION_1)) ||
3232 !(dev->features & (1ULL << VHOST_USER_F_PROTOCOL_FEATURES)) ||
3233 !(dev->protocol_features &
3234 (1ULL << VHOST_USER_PROTOCOL_F_SLAVE_REQ)) ||
3235 !(dev->protocol_features &
3236 (1ULL << VHOST_USER_PROTOCOL_F_SLAVE_SEND_FD)) ||
3237 !(dev->protocol_features &
3238 (1ULL << VHOST_USER_PROTOCOL_F_HOST_NOTIFIER)))
3241 if (qid == RTE_VHOST_QUEUE_ALL) {
3243 q_last = dev->nr_vring - 1;
3245 if (qid >= dev->nr_vring)
3251 RTE_FUNC_PTR_OR_ERR_RET(vdpa_dev->ops->get_vfio_device_fd, -ENOTSUP);
3252 RTE_FUNC_PTR_OR_ERR_RET(vdpa_dev->ops->get_notify_area, -ENOTSUP);
3254 vfio_device_fd = vdpa_dev->ops->get_vfio_device_fd(vid);
3255 if (vfio_device_fd < 0)
3259 for (i = q_start; i <= q_last; i++) {
3260 if (vdpa_dev->ops->get_notify_area(vid, i, &offset,
3266 if (vhost_user_slave_set_vring_host_notifier(dev, i,
3267 vfio_device_fd, offset, size) < 0) {
3274 for (i = q_start; i <= q_last; i++) {
3275 vhost_user_slave_set_vring_host_notifier(dev, i, -1,