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/types.h>
31 #ifdef RTE_LIBRTE_VHOST_NUMA
35 #include <rte_common.h>
36 #include <rte_malloc.h>
41 #include "vhost_user.h"
43 #define VIRTIO_MIN_MTU 68
44 #define VIRTIO_MAX_MTU 65535
46 static const char *vhost_message_str[VHOST_USER_MAX] = {
47 [VHOST_USER_NONE] = "VHOST_USER_NONE",
48 [VHOST_USER_GET_FEATURES] = "VHOST_USER_GET_FEATURES",
49 [VHOST_USER_SET_FEATURES] = "VHOST_USER_SET_FEATURES",
50 [VHOST_USER_SET_OWNER] = "VHOST_USER_SET_OWNER",
51 [VHOST_USER_RESET_OWNER] = "VHOST_USER_RESET_OWNER",
52 [VHOST_USER_SET_MEM_TABLE] = "VHOST_USER_SET_MEM_TABLE",
53 [VHOST_USER_SET_LOG_BASE] = "VHOST_USER_SET_LOG_BASE",
54 [VHOST_USER_SET_LOG_FD] = "VHOST_USER_SET_LOG_FD",
55 [VHOST_USER_SET_VRING_NUM] = "VHOST_USER_SET_VRING_NUM",
56 [VHOST_USER_SET_VRING_ADDR] = "VHOST_USER_SET_VRING_ADDR",
57 [VHOST_USER_SET_VRING_BASE] = "VHOST_USER_SET_VRING_BASE",
58 [VHOST_USER_GET_VRING_BASE] = "VHOST_USER_GET_VRING_BASE",
59 [VHOST_USER_SET_VRING_KICK] = "VHOST_USER_SET_VRING_KICK",
60 [VHOST_USER_SET_VRING_CALL] = "VHOST_USER_SET_VRING_CALL",
61 [VHOST_USER_SET_VRING_ERR] = "VHOST_USER_SET_VRING_ERR",
62 [VHOST_USER_GET_PROTOCOL_FEATURES] = "VHOST_USER_GET_PROTOCOL_FEATURES",
63 [VHOST_USER_SET_PROTOCOL_FEATURES] = "VHOST_USER_SET_PROTOCOL_FEATURES",
64 [VHOST_USER_GET_QUEUE_NUM] = "VHOST_USER_GET_QUEUE_NUM",
65 [VHOST_USER_SET_VRING_ENABLE] = "VHOST_USER_SET_VRING_ENABLE",
66 [VHOST_USER_SEND_RARP] = "VHOST_USER_SEND_RARP",
67 [VHOST_USER_NET_SET_MTU] = "VHOST_USER_NET_SET_MTU",
68 [VHOST_USER_SET_SLAVE_REQ_FD] = "VHOST_USER_SET_SLAVE_REQ_FD",
69 [VHOST_USER_IOTLB_MSG] = "VHOST_USER_IOTLB_MSG",
70 [VHOST_USER_CRYPTO_CREATE_SESS] = "VHOST_USER_CRYPTO_CREATE_SESS",
71 [VHOST_USER_CRYPTO_CLOSE_SESS] = "VHOST_USER_CRYPTO_CLOSE_SESS",
80 ret = fstat(fd, &stat);
81 return ret == -1 ? (uint64_t)-1 : (uint64_t)stat.st_blksize;
85 free_mem_region(struct virtio_net *dev)
88 struct rte_vhost_mem_region *reg;
90 if (!dev || !dev->mem)
93 for (i = 0; i < dev->mem->nregions; i++) {
94 reg = &dev->mem->regions[i];
95 if (reg->host_user_addr) {
96 munmap(reg->mmap_addr, reg->mmap_size);
103 vhost_backend_cleanup(struct virtio_net *dev)
106 free_mem_region(dev);
111 free(dev->guest_pages);
112 dev->guest_pages = NULL;
115 munmap((void *)(uintptr_t)dev->log_addr, dev->log_size);
119 if (dev->slave_req_fd >= 0) {
120 close(dev->slave_req_fd);
121 dev->slave_req_fd = -1;
126 * This function just returns success at the moment unless
127 * the device hasn't been initialised.
130 vhost_user_set_owner(void)
136 vhost_user_reset_owner(struct virtio_net *dev)
138 vhost_destroy_device_notify(dev);
140 cleanup_device(dev, 0);
146 * The features that we support are requested.
149 vhost_user_get_features(struct virtio_net *dev)
151 uint64_t features = 0;
153 rte_vhost_driver_get_features(dev->ifname, &features);
158 * The queue number that we support are requested.
161 vhost_user_get_queue_num(struct virtio_net *dev)
163 uint32_t queue_num = 0;
165 rte_vhost_driver_get_queue_num(dev->ifname, &queue_num);
170 * We receive the negotiated features supported by us and the virtio device.
173 vhost_user_set_features(struct virtio_net *dev, uint64_t features)
175 uint64_t vhost_features = 0;
176 struct rte_vdpa_device *vdpa_dev;
179 rte_vhost_driver_get_features(dev->ifname, &vhost_features);
180 if (features & ~vhost_features) {
181 RTE_LOG(ERR, VHOST_CONFIG,
182 "(%d) received invalid negotiated features.\n",
187 if (dev->flags & VIRTIO_DEV_RUNNING) {
188 if (dev->features == features)
192 * Error out if master tries to change features while device is
193 * in running state. The exception being VHOST_F_LOG_ALL, which
194 * is enabled when the live-migration starts.
196 if ((dev->features ^ features) & ~(1ULL << VHOST_F_LOG_ALL)) {
197 RTE_LOG(ERR, VHOST_CONFIG,
198 "(%d) features changed while device is running.\n",
203 if (dev->notify_ops->features_changed)
204 dev->notify_ops->features_changed(dev->vid, features);
207 dev->features = features;
209 ((1 << VIRTIO_NET_F_MRG_RXBUF) | (1ULL << VIRTIO_F_VERSION_1))) {
210 dev->vhost_hlen = sizeof(struct virtio_net_hdr_mrg_rxbuf);
212 dev->vhost_hlen = sizeof(struct virtio_net_hdr);
214 VHOST_LOG_DEBUG(VHOST_CONFIG,
215 "(%d) mergeable RX buffers %s, virtio 1 %s\n",
217 (dev->features & (1 << VIRTIO_NET_F_MRG_RXBUF)) ? "on" : "off",
218 (dev->features & (1ULL << VIRTIO_F_VERSION_1)) ? "on" : "off");
220 if ((dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET) &&
221 !(dev->features & (1ULL << VIRTIO_NET_F_MQ))) {
223 * Remove all but first queue pair if MQ hasn't been
224 * negotiated. This is safe because the device is not
225 * running at this stage.
227 while (dev->nr_vring > 2) {
228 struct vhost_virtqueue *vq;
230 vq = dev->virtqueue[--dev->nr_vring];
234 dev->virtqueue[dev->nr_vring] = NULL;
240 did = dev->vdpa_dev_id;
241 vdpa_dev = rte_vdpa_get_device(did);
242 if (vdpa_dev && vdpa_dev->ops->set_features)
243 vdpa_dev->ops->set_features(dev->vid);
249 * The virtio device sends us the size of the descriptor ring.
252 vhost_user_set_vring_num(struct virtio_net *dev,
255 struct vhost_virtqueue *vq = dev->virtqueue[msg->payload.state.index];
257 vq->size = msg->payload.state.num;
259 /* VIRTIO 1.0, 2.4 Virtqueues says:
261 * Queue Size value is always a power of 2. The maximum Queue Size
264 if ((vq->size & (vq->size - 1)) || vq->size > 32768) {
265 RTE_LOG(ERR, VHOST_CONFIG,
266 "invalid virtqueue size %u\n", vq->size);
270 if (dev->dequeue_zero_copy) {
272 vq->last_zmbuf_idx = 0;
273 vq->zmbuf_size = vq->size;
274 vq->zmbufs = rte_zmalloc(NULL, vq->zmbuf_size *
275 sizeof(struct zcopy_mbuf), 0);
276 if (vq->zmbufs == NULL) {
277 RTE_LOG(WARNING, VHOST_CONFIG,
278 "failed to allocate mem for zero copy; "
279 "zero copy is force disabled\n");
280 dev->dequeue_zero_copy = 0;
282 TAILQ_INIT(&vq->zmbuf_list);
285 if (vq_is_packed(dev)) {
286 vq->shadow_used_packed = rte_malloc(NULL,
288 sizeof(struct vring_used_elem_packed),
289 RTE_CACHE_LINE_SIZE);
290 if (!vq->shadow_used_packed) {
291 RTE_LOG(ERR, VHOST_CONFIG,
292 "failed to allocate memory for shadow used ring.\n");
297 vq->shadow_used_split = rte_malloc(NULL,
298 vq->size * sizeof(struct vring_used_elem),
299 RTE_CACHE_LINE_SIZE);
300 if (!vq->shadow_used_split) {
301 RTE_LOG(ERR, VHOST_CONFIG,
302 "failed to allocate memory for shadow used ring.\n");
307 vq->batch_copy_elems = rte_malloc(NULL,
308 vq->size * sizeof(struct batch_copy_elem),
309 RTE_CACHE_LINE_SIZE);
310 if (!vq->batch_copy_elems) {
311 RTE_LOG(ERR, VHOST_CONFIG,
312 "failed to allocate memory for batching copy.\n");
320 * Reallocate virtio_dev and vhost_virtqueue data structure to make them on the
321 * same numa node as the memory of vring descriptor.
323 #ifdef RTE_LIBRTE_VHOST_NUMA
324 static struct virtio_net*
325 numa_realloc(struct virtio_net *dev, int index)
327 int oldnode, newnode;
328 struct virtio_net *old_dev;
329 struct vhost_virtqueue *old_vq, *vq;
330 struct zcopy_mbuf *new_zmbuf;
331 struct vring_used_elem *new_shadow_used_split;
332 struct vring_used_elem_packed *new_shadow_used_packed;
333 struct batch_copy_elem *new_batch_copy_elems;
337 vq = old_vq = dev->virtqueue[index];
339 ret = get_mempolicy(&newnode, NULL, 0, old_vq->desc,
340 MPOL_F_NODE | MPOL_F_ADDR);
342 /* check if we need to reallocate vq */
343 ret |= get_mempolicy(&oldnode, NULL, 0, old_vq,
344 MPOL_F_NODE | MPOL_F_ADDR);
346 RTE_LOG(ERR, VHOST_CONFIG,
347 "Unable to get vq numa information.\n");
350 if (oldnode != newnode) {
351 RTE_LOG(INFO, VHOST_CONFIG,
352 "reallocate vq from %d to %d node\n", oldnode, newnode);
353 vq = rte_malloc_socket(NULL, sizeof(*vq), 0, newnode);
357 memcpy(vq, old_vq, sizeof(*vq));
358 TAILQ_INIT(&vq->zmbuf_list);
360 if (dev->dequeue_zero_copy) {
361 new_zmbuf = rte_malloc_socket(NULL, vq->zmbuf_size *
362 sizeof(struct zcopy_mbuf), 0, newnode);
364 rte_free(vq->zmbufs);
365 vq->zmbufs = new_zmbuf;
369 if (vq_is_packed(dev)) {
370 new_shadow_used_packed = rte_malloc_socket(NULL,
372 sizeof(struct vring_used_elem_packed),
375 if (new_shadow_used_packed) {
376 rte_free(vq->shadow_used_packed);
377 vq->shadow_used_packed = new_shadow_used_packed;
380 new_shadow_used_split = rte_malloc_socket(NULL,
382 sizeof(struct vring_used_elem),
385 if (new_shadow_used_split) {
386 rte_free(vq->shadow_used_split);
387 vq->shadow_used_split = new_shadow_used_split;
391 new_batch_copy_elems = rte_malloc_socket(NULL,
392 vq->size * sizeof(struct batch_copy_elem),
395 if (new_batch_copy_elems) {
396 rte_free(vq->batch_copy_elems);
397 vq->batch_copy_elems = new_batch_copy_elems;
403 /* check if we need to reallocate dev */
404 ret = get_mempolicy(&oldnode, NULL, 0, old_dev,
405 MPOL_F_NODE | MPOL_F_ADDR);
407 RTE_LOG(ERR, VHOST_CONFIG,
408 "Unable to get dev numa information.\n");
411 if (oldnode != newnode) {
412 RTE_LOG(INFO, VHOST_CONFIG,
413 "reallocate dev from %d to %d node\n",
415 dev = rte_malloc_socket(NULL, sizeof(*dev), 0, newnode);
421 memcpy(dev, old_dev, sizeof(*dev));
426 dev->virtqueue[index] = vq;
427 vhost_devices[dev->vid] = dev;
430 vhost_user_iotlb_init(dev, index);
435 static struct virtio_net*
436 numa_realloc(struct virtio_net *dev, int index __rte_unused)
442 /* Converts QEMU virtual address to Vhost virtual address. */
444 qva_to_vva(struct virtio_net *dev, uint64_t qva, uint64_t *len)
446 struct rte_vhost_mem_region *r;
449 /* Find the region where the address lives. */
450 for (i = 0; i < dev->mem->nregions; i++) {
451 r = &dev->mem->regions[i];
453 if (qva >= r->guest_user_addr &&
454 qva < r->guest_user_addr + r->size) {
456 if (unlikely(*len > r->guest_user_addr + r->size - qva))
457 *len = r->guest_user_addr + r->size - qva;
459 return qva - r->guest_user_addr +
470 * Converts ring address to Vhost virtual address.
471 * If IOMMU is enabled, the ring address is a guest IO virtual address,
472 * else it is a QEMU virtual address.
475 ring_addr_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
476 uint64_t ra, uint64_t *size)
478 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)) {
481 vva = vhost_user_iotlb_cache_find(vq, ra,
482 size, VHOST_ACCESS_RW);
484 vhost_user_iotlb_miss(dev, ra, VHOST_ACCESS_RW);
489 return qva_to_vva(dev, ra, size);
492 static struct virtio_net *
493 translate_ring_addresses(struct virtio_net *dev, int vq_index)
495 struct vhost_virtqueue *vq = dev->virtqueue[vq_index];
496 struct vhost_vring_addr *addr = &vq->ring_addrs;
499 if (vq_is_packed(dev)) {
500 len = sizeof(struct vring_packed_desc) * vq->size;
501 vq->desc_packed = (struct vring_packed_desc *)(uintptr_t)
502 ring_addr_to_vva(dev, vq, addr->desc_user_addr, &len);
503 vq->log_guest_addr = 0;
504 if (vq->desc_packed == NULL ||
505 len != sizeof(struct vring_packed_desc) *
507 RTE_LOG(DEBUG, VHOST_CONFIG,
508 "(%d) failed to map desc_packed ring.\n",
513 dev = numa_realloc(dev, vq_index);
514 vq = dev->virtqueue[vq_index];
515 addr = &vq->ring_addrs;
517 len = sizeof(struct vring_packed_desc_event);
518 vq->driver_event = (struct vring_packed_desc_event *)
519 (uintptr_t)ring_addr_to_vva(dev,
520 vq, addr->avail_user_addr, &len);
521 if (vq->driver_event == NULL ||
522 len != sizeof(struct vring_packed_desc_event)) {
523 RTE_LOG(DEBUG, VHOST_CONFIG,
524 "(%d) failed to find driver area address.\n",
529 len = sizeof(struct vring_packed_desc_event);
530 vq->device_event = (struct vring_packed_desc_event *)
531 (uintptr_t)ring_addr_to_vva(dev,
532 vq, addr->used_user_addr, &len);
533 if (vq->device_event == NULL ||
534 len != sizeof(struct vring_packed_desc_event)) {
535 RTE_LOG(DEBUG, VHOST_CONFIG,
536 "(%d) failed to find device area address.\n",
544 /* The addresses are converted from QEMU virtual to Vhost virtual. */
545 if (vq->desc && vq->avail && vq->used)
548 len = sizeof(struct vring_desc) * vq->size;
549 vq->desc = (struct vring_desc *)(uintptr_t)ring_addr_to_vva(dev,
550 vq, addr->desc_user_addr, &len);
551 if (vq->desc == 0 || len != sizeof(struct vring_desc) * vq->size) {
552 RTE_LOG(DEBUG, VHOST_CONFIG,
553 "(%d) failed to map desc ring.\n",
558 dev = numa_realloc(dev, vq_index);
559 vq = dev->virtqueue[vq_index];
560 addr = &vq->ring_addrs;
562 len = sizeof(struct vring_avail) + sizeof(uint16_t) * vq->size;
563 vq->avail = (struct vring_avail *)(uintptr_t)ring_addr_to_vva(dev,
564 vq, addr->avail_user_addr, &len);
565 if (vq->avail == 0 ||
566 len != sizeof(struct vring_avail) +
567 sizeof(uint16_t) * vq->size) {
568 RTE_LOG(DEBUG, VHOST_CONFIG,
569 "(%d) failed to map avail ring.\n",
574 len = sizeof(struct vring_used) +
575 sizeof(struct vring_used_elem) * vq->size;
576 vq->used = (struct vring_used *)(uintptr_t)ring_addr_to_vva(dev,
577 vq, addr->used_user_addr, &len);
578 if (vq->used == 0 || len != sizeof(struct vring_used) +
579 sizeof(struct vring_used_elem) * vq->size) {
580 RTE_LOG(DEBUG, VHOST_CONFIG,
581 "(%d) failed to map used ring.\n",
586 if (vq->last_used_idx != vq->used->idx) {
587 RTE_LOG(WARNING, VHOST_CONFIG,
588 "last_used_idx (%u) and vq->used->idx (%u) mismatches; "
589 "some packets maybe resent for Tx and dropped for Rx\n",
590 vq->last_used_idx, vq->used->idx);
591 vq->last_used_idx = vq->used->idx;
592 vq->last_avail_idx = vq->used->idx;
595 vq->log_guest_addr = addr->log_guest_addr;
597 VHOST_LOG_DEBUG(VHOST_CONFIG, "(%d) mapped address desc: %p\n",
599 VHOST_LOG_DEBUG(VHOST_CONFIG, "(%d) mapped address avail: %p\n",
600 dev->vid, vq->avail);
601 VHOST_LOG_DEBUG(VHOST_CONFIG, "(%d) mapped address used: %p\n",
603 VHOST_LOG_DEBUG(VHOST_CONFIG, "(%d) log_guest_addr: %" PRIx64 "\n",
604 dev->vid, vq->log_guest_addr);
610 * The virtio device sends us the desc, used and avail ring addresses.
611 * This function then converts these to our address space.
614 vhost_user_set_vring_addr(struct virtio_net **pdev, VhostUserMsg *msg)
616 struct vhost_virtqueue *vq;
617 struct vhost_vring_addr *addr = &msg->payload.addr;
618 struct virtio_net *dev = *pdev;
620 if (dev->mem == NULL)
623 /* addr->index refers to the queue index. The txq 1, rxq is 0. */
624 vq = dev->virtqueue[msg->payload.addr.index];
627 * Rings addresses should not be interpreted as long as the ring is not
628 * started and enabled
630 memcpy(&vq->ring_addrs, addr, sizeof(*addr));
632 vring_invalidate(dev, vq);
634 if (vq->enabled && (dev->features &
635 (1ULL << VHOST_USER_F_PROTOCOL_FEATURES))) {
636 dev = translate_ring_addresses(dev, msg->payload.addr.index);
647 * The virtio device sends us the available ring last used index.
650 vhost_user_set_vring_base(struct virtio_net *dev,
653 dev->virtqueue[msg->payload.state.index]->last_used_idx =
654 msg->payload.state.num;
655 dev->virtqueue[msg->payload.state.index]->last_avail_idx =
656 msg->payload.state.num;
662 add_one_guest_page(struct virtio_net *dev, uint64_t guest_phys_addr,
663 uint64_t host_phys_addr, uint64_t size)
665 struct guest_page *page, *last_page;
667 if (dev->nr_guest_pages == dev->max_guest_pages) {
668 dev->max_guest_pages *= 2;
669 dev->guest_pages = realloc(dev->guest_pages,
670 dev->max_guest_pages * sizeof(*page));
671 if (!dev->guest_pages) {
672 RTE_LOG(ERR, VHOST_CONFIG, "cannot realloc guest_pages\n");
677 if (dev->nr_guest_pages > 0) {
678 last_page = &dev->guest_pages[dev->nr_guest_pages - 1];
679 /* merge if the two pages are continuous */
680 if (host_phys_addr == last_page->host_phys_addr +
682 last_page->size += size;
687 page = &dev->guest_pages[dev->nr_guest_pages++];
688 page->guest_phys_addr = guest_phys_addr;
689 page->host_phys_addr = host_phys_addr;
696 add_guest_pages(struct virtio_net *dev, struct rte_vhost_mem_region *reg,
699 uint64_t reg_size = reg->size;
700 uint64_t host_user_addr = reg->host_user_addr;
701 uint64_t guest_phys_addr = reg->guest_phys_addr;
702 uint64_t host_phys_addr;
705 host_phys_addr = rte_mem_virt2iova((void *)(uintptr_t)host_user_addr);
706 size = page_size - (guest_phys_addr & (page_size - 1));
707 size = RTE_MIN(size, reg_size);
709 if (add_one_guest_page(dev, guest_phys_addr, host_phys_addr, size) < 0)
712 host_user_addr += size;
713 guest_phys_addr += size;
716 while (reg_size > 0) {
717 size = RTE_MIN(reg_size, page_size);
718 host_phys_addr = rte_mem_virt2iova((void *)(uintptr_t)
720 if (add_one_guest_page(dev, guest_phys_addr, host_phys_addr,
724 host_user_addr += size;
725 guest_phys_addr += size;
732 #ifdef RTE_LIBRTE_VHOST_DEBUG
733 /* TODO: enable it only in debug mode? */
735 dump_guest_pages(struct virtio_net *dev)
738 struct guest_page *page;
740 for (i = 0; i < dev->nr_guest_pages; i++) {
741 page = &dev->guest_pages[i];
743 RTE_LOG(INFO, VHOST_CONFIG,
744 "guest physical page region %u\n"
745 "\t guest_phys_addr: %" PRIx64 "\n"
746 "\t host_phys_addr : %" PRIx64 "\n"
747 "\t size : %" PRIx64 "\n",
749 page->guest_phys_addr,
750 page->host_phys_addr,
755 #define dump_guest_pages(dev)
759 vhost_memory_changed(struct VhostUserMemory *new,
760 struct rte_vhost_memory *old)
764 if (new->nregions != old->nregions)
767 for (i = 0; i < new->nregions; ++i) {
768 VhostUserMemoryRegion *new_r = &new->regions[i];
769 struct rte_vhost_mem_region *old_r = &old->regions[i];
771 if (new_r->guest_phys_addr != old_r->guest_phys_addr)
773 if (new_r->memory_size != old_r->size)
775 if (new_r->userspace_addr != old_r->guest_user_addr)
783 vhost_user_set_mem_table(struct virtio_net **pdev, struct VhostUserMsg *pmsg)
785 struct virtio_net *dev = *pdev;
786 struct VhostUserMemory memory = pmsg->payload.memory;
787 struct rte_vhost_mem_region *reg;
790 uint64_t mmap_offset;
796 if (memory.nregions > VHOST_MEMORY_MAX_NREGIONS) {
797 RTE_LOG(ERR, VHOST_CONFIG,
798 "too many memory regions (%u)\n", memory.nregions);
802 if (dev->mem && !vhost_memory_changed(&memory, dev->mem)) {
803 RTE_LOG(INFO, VHOST_CONFIG,
804 "(%d) memory regions not changed\n", dev->vid);
806 for (i = 0; i < memory.nregions; i++)
813 free_mem_region(dev);
818 /* Flush IOTLB cache as previous HVAs are now invalid */
819 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
820 for (i = 0; i < dev->nr_vring; i++)
821 vhost_user_iotlb_flush_all(dev->virtqueue[i]);
823 dev->nr_guest_pages = 0;
824 if (!dev->guest_pages) {
825 dev->max_guest_pages = 8;
826 dev->guest_pages = malloc(dev->max_guest_pages *
827 sizeof(struct guest_page));
828 if (dev->guest_pages == NULL) {
829 RTE_LOG(ERR, VHOST_CONFIG,
830 "(%d) failed to allocate memory "
831 "for dev->guest_pages\n",
837 dev->mem = rte_zmalloc("vhost-mem-table", sizeof(struct rte_vhost_memory) +
838 sizeof(struct rte_vhost_mem_region) * memory.nregions, 0);
839 if (dev->mem == NULL) {
840 RTE_LOG(ERR, VHOST_CONFIG,
841 "(%d) failed to allocate memory for dev->mem\n",
845 dev->mem->nregions = memory.nregions;
847 for (i = 0; i < memory.nregions; i++) {
849 reg = &dev->mem->regions[i];
851 reg->guest_phys_addr = memory.regions[i].guest_phys_addr;
852 reg->guest_user_addr = memory.regions[i].userspace_addr;
853 reg->size = memory.regions[i].memory_size;
856 mmap_offset = memory.regions[i].mmap_offset;
858 /* Check for memory_size + mmap_offset overflow */
859 if (mmap_offset >= -reg->size) {
860 RTE_LOG(ERR, VHOST_CONFIG,
861 "mmap_offset (%#"PRIx64") and memory_size "
862 "(%#"PRIx64") overflow\n",
863 mmap_offset, reg->size);
867 mmap_size = reg->size + mmap_offset;
869 /* mmap() without flag of MAP_ANONYMOUS, should be called
870 * with length argument aligned with hugepagesz at older
871 * longterm version Linux, like 2.6.32 and 3.2.72, or
872 * mmap() will fail with EINVAL.
874 * to avoid failure, make sure in caller to keep length
877 alignment = get_blk_size(fd);
878 if (alignment == (uint64_t)-1) {
879 RTE_LOG(ERR, VHOST_CONFIG,
880 "couldn't get hugepage size through fstat\n");
883 mmap_size = RTE_ALIGN_CEIL(mmap_size, alignment);
885 populate = (dev->dequeue_zero_copy) ? MAP_POPULATE : 0;
886 mmap_addr = mmap(NULL, mmap_size, PROT_READ | PROT_WRITE,
887 MAP_SHARED | populate, fd, 0);
889 if (mmap_addr == MAP_FAILED) {
890 RTE_LOG(ERR, VHOST_CONFIG,
891 "mmap region %u failed.\n", i);
895 reg->mmap_addr = mmap_addr;
896 reg->mmap_size = mmap_size;
897 reg->host_user_addr = (uint64_t)(uintptr_t)mmap_addr +
900 if (dev->dequeue_zero_copy)
901 if (add_guest_pages(dev, reg, alignment) < 0) {
902 RTE_LOG(ERR, VHOST_CONFIG,
903 "adding guest pages to region %u failed.\n",
908 RTE_LOG(INFO, VHOST_CONFIG,
909 "guest memory region %u, size: 0x%" PRIx64 "\n"
910 "\t guest physical addr: 0x%" PRIx64 "\n"
911 "\t guest virtual addr: 0x%" PRIx64 "\n"
912 "\t host virtual addr: 0x%" PRIx64 "\n"
913 "\t mmap addr : 0x%" PRIx64 "\n"
914 "\t mmap size : 0x%" PRIx64 "\n"
915 "\t mmap align: 0x%" PRIx64 "\n"
916 "\t mmap off : 0x%" PRIx64 "\n",
918 reg->guest_phys_addr,
919 reg->guest_user_addr,
921 (uint64_t)(uintptr_t)mmap_addr,
927 for (i = 0; i < dev->nr_vring; i++) {
928 struct vhost_virtqueue *vq = dev->virtqueue[i];
930 if (vq->desc || vq->avail || vq->used) {
932 * If the memory table got updated, the ring addresses
933 * need to be translated again as virtual addresses have
936 vring_invalidate(dev, vq);
938 dev = translate_ring_addresses(dev, i);
946 dump_guest_pages(dev);
951 free_mem_region(dev);
958 vq_is_ready(struct virtio_net *dev, struct vhost_virtqueue *vq)
965 if (vq_is_packed(dev))
966 rings_ok = !!vq->desc_packed;
968 rings_ok = vq->desc && vq->avail && vq->used;
971 vq->kickfd != VIRTIO_UNINITIALIZED_EVENTFD &&
972 vq->callfd != VIRTIO_UNINITIALIZED_EVENTFD;
976 virtio_is_ready(struct virtio_net *dev)
978 struct vhost_virtqueue *vq;
981 if (dev->nr_vring == 0)
984 for (i = 0; i < dev->nr_vring; i++) {
985 vq = dev->virtqueue[i];
987 if (!vq_is_ready(dev, vq))
991 RTE_LOG(INFO, VHOST_CONFIG,
992 "virtio is now ready for processing.\n");
997 vhost_user_set_vring_call(struct virtio_net *dev, struct VhostUserMsg *pmsg)
999 struct vhost_vring_file file;
1000 struct vhost_virtqueue *vq;
1002 file.index = pmsg->payload.u64 & VHOST_USER_VRING_IDX_MASK;
1003 if (pmsg->payload.u64 & VHOST_USER_VRING_NOFD_MASK)
1004 file.fd = VIRTIO_INVALID_EVENTFD;
1006 file.fd = pmsg->fds[0];
1007 RTE_LOG(INFO, VHOST_CONFIG,
1008 "vring call idx:%d file:%d\n", file.index, file.fd);
1010 vq = dev->virtqueue[file.index];
1011 if (vq->callfd >= 0)
1014 vq->callfd = file.fd;
1018 vhost_user_set_vring_kick(struct virtio_net **pdev, struct VhostUserMsg *pmsg)
1020 struct vhost_vring_file file;
1021 struct vhost_virtqueue *vq;
1022 struct virtio_net *dev = *pdev;
1024 file.index = pmsg->payload.u64 & VHOST_USER_VRING_IDX_MASK;
1025 if (pmsg->payload.u64 & VHOST_USER_VRING_NOFD_MASK)
1026 file.fd = VIRTIO_INVALID_EVENTFD;
1028 file.fd = pmsg->fds[0];
1029 RTE_LOG(INFO, VHOST_CONFIG,
1030 "vring kick idx:%d file:%d\n", file.index, file.fd);
1032 /* Interpret ring addresses only when ring is started. */
1033 dev = translate_ring_addresses(dev, file.index);
1039 vq = dev->virtqueue[file.index];
1042 * When VHOST_USER_F_PROTOCOL_FEATURES is not negotiated,
1043 * the ring starts already enabled. Otherwise, it is enabled via
1044 * the SET_VRING_ENABLE message.
1046 if (!(dev->features & (1ULL << VHOST_USER_F_PROTOCOL_FEATURES)))
1049 if (vq->kickfd >= 0)
1051 vq->kickfd = file.fd;
1055 free_zmbufs(struct vhost_virtqueue *vq)
1057 struct zcopy_mbuf *zmbuf, *next;
1059 for (zmbuf = TAILQ_FIRST(&vq->zmbuf_list);
1060 zmbuf != NULL; zmbuf = next) {
1061 next = TAILQ_NEXT(zmbuf, next);
1063 rte_pktmbuf_free(zmbuf->mbuf);
1064 TAILQ_REMOVE(&vq->zmbuf_list, zmbuf, next);
1067 rte_free(vq->zmbufs);
1071 * when virtio is stopped, qemu will send us the GET_VRING_BASE message.
1074 vhost_user_get_vring_base(struct virtio_net *dev,
1077 struct vhost_virtqueue *vq = dev->virtqueue[msg->payload.state.index];
1079 /* We have to stop the queue (virtio) if it is running. */
1080 vhost_destroy_device_notify(dev);
1082 dev->flags &= ~VIRTIO_DEV_READY;
1083 dev->flags &= ~VIRTIO_DEV_VDPA_CONFIGURED;
1085 /* Here we are safe to get the last avail index */
1086 msg->payload.state.num = vq->last_avail_idx;
1088 RTE_LOG(INFO, VHOST_CONFIG,
1089 "vring base idx:%d file:%d\n", msg->payload.state.index,
1090 msg->payload.state.num);
1092 * Based on current qemu vhost-user implementation, this message is
1093 * sent and only sent in vhost_vring_stop.
1094 * TODO: cleanup the vring, it isn't usable since here.
1096 if (vq->kickfd >= 0)
1099 vq->kickfd = VIRTIO_UNINITIALIZED_EVENTFD;
1101 if (vq->callfd >= 0)
1104 vq->callfd = VIRTIO_UNINITIALIZED_EVENTFD;
1106 if (dev->dequeue_zero_copy)
1108 if (vq_is_packed(dev)) {
1109 rte_free(vq->shadow_used_packed);
1110 vq->shadow_used_packed = NULL;
1112 rte_free(vq->shadow_used_split);
1113 vq->shadow_used_split = NULL;
1116 rte_free(vq->batch_copy_elems);
1117 vq->batch_copy_elems = NULL;
1123 * when virtio queues are ready to work, qemu will send us to
1124 * enable the virtio queue pair.
1127 vhost_user_set_vring_enable(struct virtio_net *dev,
1130 int enable = (int)msg->payload.state.num;
1131 int index = (int)msg->payload.state.index;
1132 struct rte_vdpa_device *vdpa_dev;
1135 RTE_LOG(INFO, VHOST_CONFIG,
1136 "set queue enable: %d to qp idx: %d\n",
1139 did = dev->vdpa_dev_id;
1140 vdpa_dev = rte_vdpa_get_device(did);
1141 if (vdpa_dev && vdpa_dev->ops->set_vring_state)
1142 vdpa_dev->ops->set_vring_state(dev->vid, index, enable);
1144 if (dev->notify_ops->vring_state_changed)
1145 dev->notify_ops->vring_state_changed(dev->vid,
1148 dev->virtqueue[index]->enabled = enable;
1154 vhost_user_get_protocol_features(struct virtio_net *dev,
1155 struct VhostUserMsg *msg)
1157 uint64_t features, protocol_features;
1159 rte_vhost_driver_get_features(dev->ifname, &features);
1160 rte_vhost_driver_get_protocol_features(dev->ifname, &protocol_features);
1163 * REPLY_ACK protocol feature is only mandatory for now
1164 * for IOMMU feature. If IOMMU is explicitly disabled by the
1165 * application, disable also REPLY_ACK feature for older buggy
1166 * Qemu versions (from v2.7.0 to v2.9.0).
1168 if (!(features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)))
1169 protocol_features &= ~(1ULL << VHOST_USER_PROTOCOL_F_REPLY_ACK);
1171 msg->payload.u64 = protocol_features;
1172 msg->size = sizeof(msg->payload.u64);
1176 vhost_user_set_protocol_features(struct virtio_net *dev,
1177 uint64_t protocol_features)
1179 if (protocol_features & ~VHOST_USER_PROTOCOL_FEATURES)
1182 dev->protocol_features = protocol_features;
1186 vhost_user_set_log_base(struct virtio_net *dev, struct VhostUserMsg *msg)
1188 int fd = msg->fds[0];
1193 RTE_LOG(ERR, VHOST_CONFIG, "invalid log fd: %d\n", fd);
1197 if (msg->size != sizeof(VhostUserLog)) {
1198 RTE_LOG(ERR, VHOST_CONFIG,
1199 "invalid log base msg size: %"PRId32" != %d\n",
1200 msg->size, (int)sizeof(VhostUserLog));
1204 size = msg->payload.log.mmap_size;
1205 off = msg->payload.log.mmap_offset;
1207 /* Don't allow mmap_offset to point outside the mmap region */
1209 RTE_LOG(ERR, VHOST_CONFIG,
1210 "log offset %#"PRIx64" exceeds log size %#"PRIx64"\n",
1215 RTE_LOG(INFO, VHOST_CONFIG,
1216 "log mmap size: %"PRId64", offset: %"PRId64"\n",
1220 * mmap from 0 to workaround a hugepage mmap bug: mmap will
1221 * fail when offset is not page size aligned.
1223 addr = mmap(0, size + off, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
1225 if (addr == MAP_FAILED) {
1226 RTE_LOG(ERR, VHOST_CONFIG, "mmap log base failed!\n");
1231 * Free previously mapped log memory on occasionally
1232 * multiple VHOST_USER_SET_LOG_BASE.
1234 if (dev->log_addr) {
1235 munmap((void *)(uintptr_t)dev->log_addr, dev->log_size);
1237 dev->log_addr = (uint64_t)(uintptr_t)addr;
1238 dev->log_base = dev->log_addr + off;
1239 dev->log_size = size;
1245 * An rarp packet is constructed and broadcasted to notify switches about
1246 * the new location of the migrated VM, so that packets from outside will
1247 * not be lost after migration.
1249 * However, we don't actually "send" a rarp packet here, instead, we set
1250 * a flag 'broadcast_rarp' to let rte_vhost_dequeue_burst() inject it.
1253 vhost_user_send_rarp(struct virtio_net *dev, struct VhostUserMsg *msg)
1255 uint8_t *mac = (uint8_t *)&msg->payload.u64;
1256 struct rte_vdpa_device *vdpa_dev;
1259 RTE_LOG(DEBUG, VHOST_CONFIG,
1260 ":: mac: %02x:%02x:%02x:%02x:%02x:%02x\n",
1261 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
1262 memcpy(dev->mac.addr_bytes, mac, 6);
1265 * Set the flag to inject a RARP broadcast packet at
1266 * rte_vhost_dequeue_burst().
1268 * rte_smp_wmb() is for making sure the mac is copied
1269 * before the flag is set.
1272 rte_atomic16_set(&dev->broadcast_rarp, 1);
1273 did = dev->vdpa_dev_id;
1274 vdpa_dev = rte_vdpa_get_device(did);
1275 if (vdpa_dev && vdpa_dev->ops->migration_done)
1276 vdpa_dev->ops->migration_done(dev->vid);
1282 vhost_user_net_set_mtu(struct virtio_net *dev, struct VhostUserMsg *msg)
1284 if (msg->payload.u64 < VIRTIO_MIN_MTU ||
1285 msg->payload.u64 > VIRTIO_MAX_MTU) {
1286 RTE_LOG(ERR, VHOST_CONFIG, "Invalid MTU size (%"PRIu64")\n",
1292 dev->mtu = msg->payload.u64;
1298 vhost_user_set_req_fd(struct virtio_net *dev, struct VhostUserMsg *msg)
1300 int fd = msg->fds[0];
1303 RTE_LOG(ERR, VHOST_CONFIG,
1304 "Invalid file descriptor for slave channel (%d)\n",
1309 dev->slave_req_fd = fd;
1315 is_vring_iotlb_update(struct vhost_virtqueue *vq, struct vhost_iotlb_msg *imsg)
1317 struct vhost_vring_addr *ra;
1318 uint64_t start, end;
1321 end = start + imsg->size;
1323 ra = &vq->ring_addrs;
1324 if (ra->desc_user_addr >= start && ra->desc_user_addr < end)
1326 if (ra->avail_user_addr >= start && ra->avail_user_addr < end)
1328 if (ra->used_user_addr >= start && ra->used_user_addr < end)
1335 is_vring_iotlb_invalidate(struct vhost_virtqueue *vq,
1336 struct vhost_iotlb_msg *imsg)
1338 uint64_t istart, iend, vstart, vend;
1340 istart = imsg->iova;
1341 iend = istart + imsg->size - 1;
1343 vstart = (uintptr_t)vq->desc;
1344 vend = vstart + sizeof(struct vring_desc) * vq->size - 1;
1345 if (vstart <= iend && istart <= vend)
1348 vstart = (uintptr_t)vq->avail;
1349 vend = vstart + sizeof(struct vring_avail);
1350 vend += sizeof(uint16_t) * vq->size - 1;
1351 if (vstart <= iend && istart <= vend)
1354 vstart = (uintptr_t)vq->used;
1355 vend = vstart + sizeof(struct vring_used);
1356 vend += sizeof(struct vring_used_elem) * vq->size - 1;
1357 if (vstart <= iend && istart <= vend)
1364 vhost_user_iotlb_msg(struct virtio_net **pdev, struct VhostUserMsg *msg)
1366 struct virtio_net *dev = *pdev;
1367 struct vhost_iotlb_msg *imsg = &msg->payload.iotlb;
1371 switch (imsg->type) {
1372 case VHOST_IOTLB_UPDATE:
1374 vva = qva_to_vva(dev, imsg->uaddr, &len);
1378 for (i = 0; i < dev->nr_vring; i++) {
1379 struct vhost_virtqueue *vq = dev->virtqueue[i];
1381 vhost_user_iotlb_cache_insert(vq, imsg->iova, vva,
1384 if (is_vring_iotlb_update(vq, imsg))
1385 *pdev = dev = translate_ring_addresses(dev, i);
1388 case VHOST_IOTLB_INVALIDATE:
1389 for (i = 0; i < dev->nr_vring; i++) {
1390 struct vhost_virtqueue *vq = dev->virtqueue[i];
1392 vhost_user_iotlb_cache_remove(vq, imsg->iova,
1395 if (is_vring_iotlb_invalidate(vq, imsg))
1396 vring_invalidate(dev, vq);
1400 RTE_LOG(ERR, VHOST_CONFIG, "Invalid IOTLB message type (%d)\n",
1408 /* return bytes# of read on success or negative val on failure. */
1410 read_vhost_message(int sockfd, struct VhostUserMsg *msg)
1414 ret = read_fd_message(sockfd, (char *)msg, VHOST_USER_HDR_SIZE,
1415 msg->fds, VHOST_MEMORY_MAX_NREGIONS);
1419 if (msg && msg->size) {
1420 if (msg->size > sizeof(msg->payload)) {
1421 RTE_LOG(ERR, VHOST_CONFIG,
1422 "invalid msg size: %d\n", msg->size);
1425 ret = read(sockfd, &msg->payload, msg->size);
1428 if (ret != (int)msg->size) {
1429 RTE_LOG(ERR, VHOST_CONFIG,
1430 "read control message failed\n");
1439 send_vhost_message(int sockfd, struct VhostUserMsg *msg, int *fds, int fd_num)
1444 return send_fd_message(sockfd, (char *)msg,
1445 VHOST_USER_HDR_SIZE + msg->size, fds, fd_num);
1449 send_vhost_reply(int sockfd, struct VhostUserMsg *msg)
1454 msg->flags &= ~VHOST_USER_VERSION_MASK;
1455 msg->flags &= ~VHOST_USER_NEED_REPLY;
1456 msg->flags |= VHOST_USER_VERSION;
1457 msg->flags |= VHOST_USER_REPLY_MASK;
1459 return send_vhost_message(sockfd, msg, NULL, 0);
1463 send_vhost_slave_message(struct virtio_net *dev, struct VhostUserMsg *msg,
1464 int *fds, int fd_num)
1468 if (msg->flags & VHOST_USER_NEED_REPLY)
1469 rte_spinlock_lock(&dev->slave_req_lock);
1471 ret = send_vhost_message(dev->slave_req_fd, msg, fds, fd_num);
1472 if (ret < 0 && (msg->flags & VHOST_USER_NEED_REPLY))
1473 rte_spinlock_unlock(&dev->slave_req_lock);
1479 * Allocate a queue pair if it hasn't been allocated yet
1482 vhost_user_check_and_alloc_queue_pair(struct virtio_net *dev, VhostUserMsg *msg)
1486 switch (msg->request.master) {
1487 case VHOST_USER_SET_VRING_KICK:
1488 case VHOST_USER_SET_VRING_CALL:
1489 case VHOST_USER_SET_VRING_ERR:
1490 vring_idx = msg->payload.u64 & VHOST_USER_VRING_IDX_MASK;
1492 case VHOST_USER_SET_VRING_NUM:
1493 case VHOST_USER_SET_VRING_BASE:
1494 case VHOST_USER_SET_VRING_ENABLE:
1495 vring_idx = msg->payload.state.index;
1497 case VHOST_USER_SET_VRING_ADDR:
1498 vring_idx = msg->payload.addr.index;
1504 if (vring_idx >= VHOST_MAX_VRING) {
1505 RTE_LOG(ERR, VHOST_CONFIG,
1506 "invalid vring index: %u\n", vring_idx);
1510 if (dev->virtqueue[vring_idx])
1513 return alloc_vring_queue(dev, vring_idx);
1517 vhost_user_lock_all_queue_pairs(struct virtio_net *dev)
1520 unsigned int vq_num = 0;
1522 while (vq_num < dev->nr_vring) {
1523 struct vhost_virtqueue *vq = dev->virtqueue[i];
1526 rte_spinlock_lock(&vq->access_lock);
1534 vhost_user_unlock_all_queue_pairs(struct virtio_net *dev)
1537 unsigned int vq_num = 0;
1539 while (vq_num < dev->nr_vring) {
1540 struct vhost_virtqueue *vq = dev->virtqueue[i];
1543 rte_spinlock_unlock(&vq->access_lock);
1551 vhost_user_msg_handler(int vid, int fd)
1553 struct virtio_net *dev;
1554 struct VhostUserMsg msg;
1555 struct rte_vdpa_device *vdpa_dev;
1558 int unlock_required = 0;
1559 uint32_t skip_master = 0;
1561 dev = get_device(vid);
1565 if (!dev->notify_ops) {
1566 dev->notify_ops = vhost_driver_callback_get(dev->ifname);
1567 if (!dev->notify_ops) {
1568 RTE_LOG(ERR, VHOST_CONFIG,
1569 "failed to get callback ops for driver %s\n",
1575 ret = read_vhost_message(fd, &msg);
1576 if (ret <= 0 || msg.request.master >= VHOST_USER_MAX) {
1578 RTE_LOG(ERR, VHOST_CONFIG,
1579 "vhost read message failed\n");
1581 RTE_LOG(INFO, VHOST_CONFIG,
1582 "vhost peer closed\n");
1584 RTE_LOG(ERR, VHOST_CONFIG,
1585 "vhost read incorrect message\n");
1591 if (msg.request.master != VHOST_USER_IOTLB_MSG)
1592 RTE_LOG(INFO, VHOST_CONFIG, "read message %s\n",
1593 vhost_message_str[msg.request.master]);
1595 RTE_LOG(DEBUG, VHOST_CONFIG, "read message %s\n",
1596 vhost_message_str[msg.request.master]);
1598 ret = vhost_user_check_and_alloc_queue_pair(dev, &msg);
1600 RTE_LOG(ERR, VHOST_CONFIG,
1601 "failed to alloc queue\n");
1606 * Note: we don't lock all queues on VHOST_USER_GET_VRING_BASE
1607 * and VHOST_USER_RESET_OWNER, since it is sent when virtio stops
1608 * and device is destroyed. destroy_device waits for queues to be
1609 * inactive, so it is safe. Otherwise taking the access_lock
1610 * would cause a dead lock.
1612 switch (msg.request.master) {
1613 case VHOST_USER_SET_FEATURES:
1614 case VHOST_USER_SET_PROTOCOL_FEATURES:
1615 case VHOST_USER_SET_OWNER:
1616 case VHOST_USER_SET_MEM_TABLE:
1617 case VHOST_USER_SET_LOG_BASE:
1618 case VHOST_USER_SET_LOG_FD:
1619 case VHOST_USER_SET_VRING_NUM:
1620 case VHOST_USER_SET_VRING_ADDR:
1621 case VHOST_USER_SET_VRING_BASE:
1622 case VHOST_USER_SET_VRING_KICK:
1623 case VHOST_USER_SET_VRING_CALL:
1624 case VHOST_USER_SET_VRING_ERR:
1625 case VHOST_USER_SET_VRING_ENABLE:
1626 case VHOST_USER_SEND_RARP:
1627 case VHOST_USER_NET_SET_MTU:
1628 case VHOST_USER_SET_SLAVE_REQ_FD:
1629 vhost_user_lock_all_queue_pairs(dev);
1630 unlock_required = 1;
1637 if (dev->extern_ops.pre_msg_handle) {
1638 uint32_t need_reply;
1640 ret = (*dev->extern_ops.pre_msg_handle)(dev->vid,
1641 (void *)&msg, &need_reply, &skip_master);
1646 send_vhost_reply(fd, &msg);
1649 goto skip_to_post_handle;
1652 switch (msg.request.master) {
1653 case VHOST_USER_GET_FEATURES:
1654 msg.payload.u64 = vhost_user_get_features(dev);
1655 msg.size = sizeof(msg.payload.u64);
1656 send_vhost_reply(fd, &msg);
1658 case VHOST_USER_SET_FEATURES:
1659 ret = vhost_user_set_features(dev, msg.payload.u64);
1664 case VHOST_USER_GET_PROTOCOL_FEATURES:
1665 vhost_user_get_protocol_features(dev, &msg);
1666 send_vhost_reply(fd, &msg);
1668 case VHOST_USER_SET_PROTOCOL_FEATURES:
1669 vhost_user_set_protocol_features(dev, msg.payload.u64);
1672 case VHOST_USER_SET_OWNER:
1673 vhost_user_set_owner();
1675 case VHOST_USER_RESET_OWNER:
1676 vhost_user_reset_owner(dev);
1679 case VHOST_USER_SET_MEM_TABLE:
1680 ret = vhost_user_set_mem_table(&dev, &msg);
1683 case VHOST_USER_SET_LOG_BASE:
1684 vhost_user_set_log_base(dev, &msg);
1686 /* it needs a reply */
1687 msg.size = sizeof(msg.payload.u64);
1688 send_vhost_reply(fd, &msg);
1690 case VHOST_USER_SET_LOG_FD:
1692 RTE_LOG(INFO, VHOST_CONFIG, "not implemented.\n");
1695 case VHOST_USER_SET_VRING_NUM:
1696 vhost_user_set_vring_num(dev, &msg);
1698 case VHOST_USER_SET_VRING_ADDR:
1699 vhost_user_set_vring_addr(&dev, &msg);
1701 case VHOST_USER_SET_VRING_BASE:
1702 vhost_user_set_vring_base(dev, &msg);
1705 case VHOST_USER_GET_VRING_BASE:
1706 vhost_user_get_vring_base(dev, &msg);
1707 msg.size = sizeof(msg.payload.state);
1708 send_vhost_reply(fd, &msg);
1711 case VHOST_USER_SET_VRING_KICK:
1712 vhost_user_set_vring_kick(&dev, &msg);
1714 case VHOST_USER_SET_VRING_CALL:
1715 vhost_user_set_vring_call(dev, &msg);
1718 case VHOST_USER_SET_VRING_ERR:
1719 if (!(msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK))
1721 RTE_LOG(INFO, VHOST_CONFIG, "not implemented\n");
1724 case VHOST_USER_GET_QUEUE_NUM:
1725 msg.payload.u64 = (uint64_t)vhost_user_get_queue_num(dev);
1726 msg.size = sizeof(msg.payload.u64);
1727 send_vhost_reply(fd, &msg);
1730 case VHOST_USER_SET_VRING_ENABLE:
1731 vhost_user_set_vring_enable(dev, &msg);
1733 case VHOST_USER_SEND_RARP:
1734 vhost_user_send_rarp(dev, &msg);
1737 case VHOST_USER_NET_SET_MTU:
1738 ret = vhost_user_net_set_mtu(dev, &msg);
1741 case VHOST_USER_SET_SLAVE_REQ_FD:
1742 ret = vhost_user_set_req_fd(dev, &msg);
1745 case VHOST_USER_IOTLB_MSG:
1746 ret = vhost_user_iotlb_msg(&dev, &msg);
1754 skip_to_post_handle:
1755 if (dev->extern_ops.post_msg_handle) {
1756 uint32_t need_reply;
1758 ret = (*dev->extern_ops.post_msg_handle)(
1759 dev->vid, (void *)&msg, &need_reply);
1764 send_vhost_reply(fd, &msg);
1768 if (unlock_required)
1769 vhost_user_unlock_all_queue_pairs(dev);
1771 if (msg.flags & VHOST_USER_NEED_REPLY) {
1772 msg.payload.u64 = !!ret;
1773 msg.size = sizeof(msg.payload.u64);
1774 send_vhost_reply(fd, &msg);
1777 if (!(dev->flags & VIRTIO_DEV_RUNNING) && virtio_is_ready(dev)) {
1778 dev->flags |= VIRTIO_DEV_READY;
1780 if (!(dev->flags & VIRTIO_DEV_RUNNING)) {
1781 if (dev->dequeue_zero_copy) {
1782 RTE_LOG(INFO, VHOST_CONFIG,
1783 "dequeue zero copy is enabled\n");
1786 if (dev->notify_ops->new_device(dev->vid) == 0)
1787 dev->flags |= VIRTIO_DEV_RUNNING;
1791 did = dev->vdpa_dev_id;
1792 vdpa_dev = rte_vdpa_get_device(did);
1793 if (vdpa_dev && virtio_is_ready(dev) &&
1794 !(dev->flags & VIRTIO_DEV_VDPA_CONFIGURED) &&
1795 msg.request.master == VHOST_USER_SET_VRING_ENABLE) {
1796 if (vdpa_dev->ops->dev_conf)
1797 vdpa_dev->ops->dev_conf(dev->vid);
1798 dev->flags |= VIRTIO_DEV_VDPA_CONFIGURED;
1799 if (vhost_user_host_notifier_ctrl(dev->vid, true) != 0) {
1800 RTE_LOG(INFO, VHOST_CONFIG,
1801 "(%d) software relay is used for vDPA, performance may be low.\n",
1809 static int process_slave_message_reply(struct virtio_net *dev,
1810 const VhostUserMsg *msg)
1812 VhostUserMsg msg_reply;
1815 if ((msg->flags & VHOST_USER_NEED_REPLY) == 0)
1818 if (read_vhost_message(dev->slave_req_fd, &msg_reply) < 0) {
1823 if (msg_reply.request.slave != msg->request.slave) {
1824 RTE_LOG(ERR, VHOST_CONFIG,
1825 "Received unexpected msg type (%u), expected %u\n",
1826 msg_reply.request.slave, msg->request.slave);
1831 ret = msg_reply.payload.u64 ? -1 : 0;
1834 rte_spinlock_unlock(&dev->slave_req_lock);
1839 vhost_user_iotlb_miss(struct virtio_net *dev, uint64_t iova, uint8_t perm)
1842 struct VhostUserMsg msg = {
1843 .request.slave = VHOST_USER_SLAVE_IOTLB_MSG,
1844 .flags = VHOST_USER_VERSION,
1845 .size = sizeof(msg.payload.iotlb),
1849 .type = VHOST_IOTLB_MISS,
1853 ret = send_vhost_message(dev->slave_req_fd, &msg, NULL, 0);
1855 RTE_LOG(ERR, VHOST_CONFIG,
1856 "Failed to send IOTLB miss message (%d)\n",
1864 static int vhost_user_slave_set_vring_host_notifier(struct virtio_net *dev,
1872 struct VhostUserMsg msg = {
1873 .request.slave = VHOST_USER_SLAVE_VRING_HOST_NOTIFIER_MSG,
1874 .flags = VHOST_USER_VERSION | VHOST_USER_NEED_REPLY,
1875 .size = sizeof(msg.payload.area),
1877 .u64 = index & VHOST_USER_VRING_IDX_MASK,
1884 msg.payload.area.u64 |= VHOST_USER_VRING_NOFD_MASK;
1890 ret = send_vhost_slave_message(dev, &msg, fdp, fd_num);
1892 RTE_LOG(ERR, VHOST_CONFIG,
1893 "Failed to set host notifier (%d)\n", ret);
1897 return process_slave_message_reply(dev, &msg);
1900 int vhost_user_host_notifier_ctrl(int vid, bool enable)
1902 struct virtio_net *dev;
1903 struct rte_vdpa_device *vdpa_dev;
1904 int vfio_device_fd, did, ret = 0;
1905 uint64_t offset, size;
1908 dev = get_device(vid);
1912 did = dev->vdpa_dev_id;
1916 if (!(dev->features & (1ULL << VIRTIO_F_VERSION_1)) ||
1917 !(dev->features & (1ULL << VHOST_USER_F_PROTOCOL_FEATURES)) ||
1918 !(dev->protocol_features &
1919 (1ULL << VHOST_USER_PROTOCOL_F_SLAVE_REQ)) ||
1920 !(dev->protocol_features &
1921 (1ULL << VHOST_USER_PROTOCOL_F_SLAVE_SEND_FD)) ||
1922 !(dev->protocol_features &
1923 (1ULL << VHOST_USER_PROTOCOL_F_HOST_NOTIFIER)))
1926 vdpa_dev = rte_vdpa_get_device(did);
1930 RTE_FUNC_PTR_OR_ERR_RET(vdpa_dev->ops->get_vfio_device_fd, -ENOTSUP);
1931 RTE_FUNC_PTR_OR_ERR_RET(vdpa_dev->ops->get_notify_area, -ENOTSUP);
1933 vfio_device_fd = vdpa_dev->ops->get_vfio_device_fd(vid);
1934 if (vfio_device_fd < 0)
1938 for (i = 0; i < dev->nr_vring; i++) {
1939 if (vdpa_dev->ops->get_notify_area(vid, i, &offset,
1945 if (vhost_user_slave_set_vring_host_notifier(dev, i,
1946 vfio_device_fd, offset, size) < 0) {
1953 for (i = 0; i < dev->nr_vring; i++) {
1954 vhost_user_slave_set_vring_host_notifier(dev, i, -1,