4 * Copyright(c) 2010-2016 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 #include <sys/types.h>
43 #ifdef RTE_LIBRTE_VHOST_NUMA
47 #include <rte_common.h>
48 #include <rte_malloc.h>
53 #include "vhost_user.h"
55 #define VIRTIO_MIN_MTU 68
56 #define VIRTIO_MAX_MTU 65535
58 static const char *vhost_message_str[VHOST_USER_MAX] = {
59 [VHOST_USER_NONE] = "VHOST_USER_NONE",
60 [VHOST_USER_GET_FEATURES] = "VHOST_USER_GET_FEATURES",
61 [VHOST_USER_SET_FEATURES] = "VHOST_USER_SET_FEATURES",
62 [VHOST_USER_SET_OWNER] = "VHOST_USER_SET_OWNER",
63 [VHOST_USER_RESET_OWNER] = "VHOST_USER_RESET_OWNER",
64 [VHOST_USER_SET_MEM_TABLE] = "VHOST_USER_SET_MEM_TABLE",
65 [VHOST_USER_SET_LOG_BASE] = "VHOST_USER_SET_LOG_BASE",
66 [VHOST_USER_SET_LOG_FD] = "VHOST_USER_SET_LOG_FD",
67 [VHOST_USER_SET_VRING_NUM] = "VHOST_USER_SET_VRING_NUM",
68 [VHOST_USER_SET_VRING_ADDR] = "VHOST_USER_SET_VRING_ADDR",
69 [VHOST_USER_SET_VRING_BASE] = "VHOST_USER_SET_VRING_BASE",
70 [VHOST_USER_GET_VRING_BASE] = "VHOST_USER_GET_VRING_BASE",
71 [VHOST_USER_SET_VRING_KICK] = "VHOST_USER_SET_VRING_KICK",
72 [VHOST_USER_SET_VRING_CALL] = "VHOST_USER_SET_VRING_CALL",
73 [VHOST_USER_SET_VRING_ERR] = "VHOST_USER_SET_VRING_ERR",
74 [VHOST_USER_GET_PROTOCOL_FEATURES] = "VHOST_USER_GET_PROTOCOL_FEATURES",
75 [VHOST_USER_SET_PROTOCOL_FEATURES] = "VHOST_USER_SET_PROTOCOL_FEATURES",
76 [VHOST_USER_GET_QUEUE_NUM] = "VHOST_USER_GET_QUEUE_NUM",
77 [VHOST_USER_SET_VRING_ENABLE] = "VHOST_USER_SET_VRING_ENABLE",
78 [VHOST_USER_SEND_RARP] = "VHOST_USER_SEND_RARP",
79 [VHOST_USER_NET_SET_MTU] = "VHOST_USER_NET_SET_MTU",
80 [VHOST_USER_SET_SLAVE_REQ_FD] = "VHOST_USER_SET_SLAVE_REQ_FD",
81 [VHOST_USER_IOTLB_MSG] = "VHOST_USER_IOTLB_MSG",
90 ret = fstat(fd, &stat);
91 return ret == -1 ? (uint64_t)-1 : (uint64_t)stat.st_blksize;
95 free_mem_region(struct virtio_net *dev)
98 struct rte_vhost_mem_region *reg;
100 if (!dev || !dev->mem)
103 for (i = 0; i < dev->mem->nregions; i++) {
104 reg = &dev->mem->regions[i];
105 if (reg->host_user_addr) {
106 munmap(reg->mmap_addr, reg->mmap_size);
113 vhost_backend_cleanup(struct virtio_net *dev)
116 free_mem_region(dev);
121 free(dev->guest_pages);
122 dev->guest_pages = NULL;
125 munmap((void *)(uintptr_t)dev->log_addr, dev->log_size);
129 if (dev->slave_req_fd >= 0) {
130 close(dev->slave_req_fd);
131 dev->slave_req_fd = -1;
136 * This function just returns success at the moment unless
137 * the device hasn't been initialised.
140 vhost_user_set_owner(void)
146 vhost_user_reset_owner(struct virtio_net *dev)
148 if (dev->flags & VIRTIO_DEV_RUNNING) {
149 dev->flags &= ~VIRTIO_DEV_RUNNING;
150 dev->notify_ops->destroy_device(dev->vid);
153 cleanup_device(dev, 0);
159 * The features that we support are requested.
162 vhost_user_get_features(struct virtio_net *dev)
164 uint64_t features = 0;
166 rte_vhost_driver_get_features(dev->ifname, &features);
171 * We receive the negotiated features supported by us and the virtio device.
174 vhost_user_set_features(struct virtio_net *dev, uint64_t features)
176 uint64_t vhost_features = 0;
178 rte_vhost_driver_get_features(dev->ifname, &vhost_features);
179 if (features & ~vhost_features) {
180 RTE_LOG(ERR, VHOST_CONFIG,
181 "(%d) received invalid negotiated features.\n",
186 if ((dev->flags & VIRTIO_DEV_RUNNING) && dev->features != features) {
187 if (dev->notify_ops->features_changed)
188 dev->notify_ops->features_changed(dev->vid, features);
191 dev->features = features;
193 ((1 << VIRTIO_NET_F_MRG_RXBUF) | (1ULL << VIRTIO_F_VERSION_1))) {
194 dev->vhost_hlen = sizeof(struct virtio_net_hdr_mrg_rxbuf);
196 dev->vhost_hlen = sizeof(struct virtio_net_hdr);
198 LOG_DEBUG(VHOST_CONFIG,
199 "(%d) mergeable RX buffers %s, virtio 1 %s\n",
201 (dev->features & (1 << VIRTIO_NET_F_MRG_RXBUF)) ? "on" : "off",
202 (dev->features & (1ULL << VIRTIO_F_VERSION_1)) ? "on" : "off");
208 * The virtio device sends us the size of the descriptor ring.
211 vhost_user_set_vring_num(struct virtio_net *dev,
214 struct vhost_virtqueue *vq = dev->virtqueue[msg->payload.state.index];
216 vq->size = msg->payload.state.num;
218 if (dev->dequeue_zero_copy) {
220 vq->last_zmbuf_idx = 0;
221 vq->zmbuf_size = vq->size;
222 vq->zmbufs = rte_zmalloc(NULL, vq->zmbuf_size *
223 sizeof(struct zcopy_mbuf), 0);
224 if (vq->zmbufs == NULL) {
225 RTE_LOG(WARNING, VHOST_CONFIG,
226 "failed to allocate mem for zero copy; "
227 "zero copy is force disabled\n");
228 dev->dequeue_zero_copy = 0;
232 vq->shadow_used_ring = rte_malloc(NULL,
233 vq->size * sizeof(struct vring_used_elem),
234 RTE_CACHE_LINE_SIZE);
235 if (!vq->shadow_used_ring) {
236 RTE_LOG(ERR, VHOST_CONFIG,
237 "failed to allocate memory for shadow used ring.\n");
241 vq->batch_copy_elems = rte_malloc(NULL,
242 vq->size * sizeof(struct batch_copy_elem),
243 RTE_CACHE_LINE_SIZE);
244 if (!vq->batch_copy_elems) {
245 RTE_LOG(ERR, VHOST_CONFIG,
246 "failed to allocate memory for batching copy.\n");
254 * Reallocate virtio_dev and vhost_virtqueue data structure to make them on the
255 * same numa node as the memory of vring descriptor.
257 #ifdef RTE_LIBRTE_VHOST_NUMA
258 static struct virtio_net*
259 numa_realloc(struct virtio_net *dev, int index)
261 int oldnode, newnode;
262 struct virtio_net *old_dev;
263 struct vhost_virtqueue *old_vq, *vq;
267 vq = old_vq = dev->virtqueue[index];
269 ret = get_mempolicy(&newnode, NULL, 0, old_vq->desc,
270 MPOL_F_NODE | MPOL_F_ADDR);
272 /* check if we need to reallocate vq */
273 ret |= get_mempolicy(&oldnode, NULL, 0, old_vq,
274 MPOL_F_NODE | MPOL_F_ADDR);
276 RTE_LOG(ERR, VHOST_CONFIG,
277 "Unable to get vq numa information.\n");
280 if (oldnode != newnode) {
281 RTE_LOG(INFO, VHOST_CONFIG,
282 "reallocate vq from %d to %d node\n", oldnode, newnode);
283 vq = rte_malloc_socket(NULL, sizeof(*vq), 0, newnode);
287 memcpy(vq, old_vq, sizeof(*vq));
291 /* check if we need to reallocate dev */
292 ret = get_mempolicy(&oldnode, NULL, 0, old_dev,
293 MPOL_F_NODE | MPOL_F_ADDR);
295 RTE_LOG(ERR, VHOST_CONFIG,
296 "Unable to get dev numa information.\n");
299 if (oldnode != newnode) {
300 RTE_LOG(INFO, VHOST_CONFIG,
301 "reallocate dev from %d to %d node\n",
303 dev = rte_malloc_socket(NULL, sizeof(*dev), 0, newnode);
309 memcpy(dev, old_dev, sizeof(*dev));
314 dev->virtqueue[index] = vq;
315 vhost_devices[dev->vid] = dev;
318 vhost_user_iotlb_init(dev, index);
323 static struct virtio_net*
324 numa_realloc(struct virtio_net *dev, int index __rte_unused)
330 /* Converts QEMU virtual address to Vhost virtual address. */
332 qva_to_vva(struct virtio_net *dev, uint64_t qva)
334 struct rte_vhost_mem_region *reg;
337 /* Find the region where the address lives. */
338 for (i = 0; i < dev->mem->nregions; i++) {
339 reg = &dev->mem->regions[i];
341 if (qva >= reg->guest_user_addr &&
342 qva < reg->guest_user_addr + reg->size) {
343 return qva - reg->guest_user_addr +
353 * Converts ring address to Vhost virtual address.
354 * If IOMMU is enabled, the ring address is a guest IO virtual address,
355 * else it is a QEMU virtual address.
358 ring_addr_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
359 uint64_t ra, uint64_t size)
361 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)) {
364 vva = vhost_user_iotlb_cache_find(vq, ra,
365 &size, VHOST_ACCESS_RW);
367 vhost_user_iotlb_miss(dev, ra, VHOST_ACCESS_RW);
372 return qva_to_vva(dev, ra);
375 static struct virtio_net *
376 translate_ring_addresses(struct virtio_net *dev, int vq_index)
378 struct vhost_virtqueue *vq = dev->virtqueue[vq_index];
379 struct vhost_vring_addr *addr = &vq->ring_addrs;
381 /* The addresses are converted from QEMU virtual to Vhost virtual. */
382 if (vq->desc && vq->avail && vq->used)
385 vq->desc = (struct vring_desc *)(uintptr_t)ring_addr_to_vva(dev,
386 vq, addr->desc_user_addr, sizeof(struct vring_desc));
388 RTE_LOG(DEBUG, VHOST_CONFIG,
389 "(%d) failed to find desc ring address.\n",
394 dev = numa_realloc(dev, vq_index);
395 vq = dev->virtqueue[vq_index];
396 addr = &vq->ring_addrs;
398 vq->avail = (struct vring_avail *)(uintptr_t)ring_addr_to_vva(dev,
399 vq, addr->avail_user_addr, sizeof(struct vring_avail));
400 if (vq->avail == 0) {
401 RTE_LOG(DEBUG, VHOST_CONFIG,
402 "(%d) failed to find avail ring address.\n",
407 vq->used = (struct vring_used *)(uintptr_t)ring_addr_to_vva(dev,
408 vq, addr->used_user_addr, sizeof(struct vring_used));
410 RTE_LOG(DEBUG, VHOST_CONFIG,
411 "(%d) failed to find used ring address.\n",
416 if (vq->last_used_idx != vq->used->idx) {
417 RTE_LOG(WARNING, VHOST_CONFIG,
418 "last_used_idx (%u) and vq->used->idx (%u) mismatches; "
419 "some packets maybe resent for Tx and dropped for Rx\n",
420 vq->last_used_idx, vq->used->idx);
421 vq->last_used_idx = vq->used->idx;
422 vq->last_avail_idx = vq->used->idx;
425 vq->log_guest_addr = addr->log_guest_addr;
427 LOG_DEBUG(VHOST_CONFIG, "(%d) mapped address desc: %p\n",
429 LOG_DEBUG(VHOST_CONFIG, "(%d) mapped address avail: %p\n",
430 dev->vid, vq->avail);
431 LOG_DEBUG(VHOST_CONFIG, "(%d) mapped address used: %p\n",
433 LOG_DEBUG(VHOST_CONFIG, "(%d) log_guest_addr: %" PRIx64 "\n",
434 dev->vid, vq->log_guest_addr);
440 * The virtio device sends us the desc, used and avail ring addresses.
441 * This function then converts these to our address space.
444 vhost_user_set_vring_addr(struct virtio_net **pdev, VhostUserMsg *msg)
446 struct vhost_virtqueue *vq;
447 struct vhost_vring_addr *addr = &msg->payload.addr;
448 struct virtio_net *dev = *pdev;
450 if (dev->mem == NULL)
453 /* addr->index refers to the queue index. The txq 1, rxq is 0. */
454 vq = dev->virtqueue[msg->payload.addr.index];
457 * Rings addresses should not be interpreted as long as the ring is not
458 * started and enabled
460 memcpy(&vq->ring_addrs, addr, sizeof(*addr));
462 vring_invalidate(dev, vq);
464 if (vq->enabled && (dev->features &
465 (1ULL << VHOST_USER_F_PROTOCOL_FEATURES))) {
466 dev = translate_ring_addresses(dev, msg->payload.state.index);
477 * The virtio device sends us the available ring last used index.
480 vhost_user_set_vring_base(struct virtio_net *dev,
483 dev->virtqueue[msg->payload.state.index]->last_used_idx =
484 msg->payload.state.num;
485 dev->virtqueue[msg->payload.state.index]->last_avail_idx =
486 msg->payload.state.num;
492 add_one_guest_page(struct virtio_net *dev, uint64_t guest_phys_addr,
493 uint64_t host_phys_addr, uint64_t size)
495 struct guest_page *page, *last_page;
497 if (dev->nr_guest_pages == dev->max_guest_pages) {
498 dev->max_guest_pages *= 2;
499 dev->guest_pages = realloc(dev->guest_pages,
500 dev->max_guest_pages * sizeof(*page));
503 if (dev->nr_guest_pages > 0) {
504 last_page = &dev->guest_pages[dev->nr_guest_pages - 1];
505 /* merge if the two pages are continuous */
506 if (host_phys_addr == last_page->host_phys_addr +
508 last_page->size += size;
513 page = &dev->guest_pages[dev->nr_guest_pages++];
514 page->guest_phys_addr = guest_phys_addr;
515 page->host_phys_addr = host_phys_addr;
520 add_guest_pages(struct virtio_net *dev, struct rte_vhost_mem_region *reg,
523 uint64_t reg_size = reg->size;
524 uint64_t host_user_addr = reg->host_user_addr;
525 uint64_t guest_phys_addr = reg->guest_phys_addr;
526 uint64_t host_phys_addr;
529 host_phys_addr = rte_mem_virt2iova((void *)(uintptr_t)host_user_addr);
530 size = page_size - (guest_phys_addr & (page_size - 1));
531 size = RTE_MIN(size, reg_size);
533 add_one_guest_page(dev, guest_phys_addr, host_phys_addr, size);
534 host_user_addr += size;
535 guest_phys_addr += size;
538 while (reg_size > 0) {
539 size = RTE_MIN(reg_size, page_size);
540 host_phys_addr = rte_mem_virt2iova((void *)(uintptr_t)
542 add_one_guest_page(dev, guest_phys_addr, host_phys_addr, size);
544 host_user_addr += size;
545 guest_phys_addr += size;
550 #ifdef RTE_LIBRTE_VHOST_DEBUG
551 /* TODO: enable it only in debug mode? */
553 dump_guest_pages(struct virtio_net *dev)
556 struct guest_page *page;
558 for (i = 0; i < dev->nr_guest_pages; i++) {
559 page = &dev->guest_pages[i];
561 RTE_LOG(INFO, VHOST_CONFIG,
562 "guest physical page region %u\n"
563 "\t guest_phys_addr: %" PRIx64 "\n"
564 "\t host_phys_addr : %" PRIx64 "\n"
565 "\t size : %" PRIx64 "\n",
567 page->guest_phys_addr,
568 page->host_phys_addr,
573 #define dump_guest_pages(dev)
577 vhost_user_set_mem_table(struct virtio_net *dev, struct VhostUserMsg *pmsg)
579 struct VhostUserMemory memory = pmsg->payload.memory;
580 struct rte_vhost_mem_region *reg;
583 uint64_t mmap_offset;
589 free_mem_region(dev);
594 dev->nr_guest_pages = 0;
595 if (!dev->guest_pages) {
596 dev->max_guest_pages = 8;
597 dev->guest_pages = malloc(dev->max_guest_pages *
598 sizeof(struct guest_page));
599 if (dev->guest_pages == NULL) {
600 RTE_LOG(ERR, VHOST_CONFIG,
601 "(%d) failed to allocate memory "
602 "for dev->guest_pages\n",
608 dev->mem = rte_zmalloc("vhost-mem-table", sizeof(struct rte_vhost_memory) +
609 sizeof(struct rte_vhost_mem_region) * memory.nregions, 0);
610 if (dev->mem == NULL) {
611 RTE_LOG(ERR, VHOST_CONFIG,
612 "(%d) failed to allocate memory for dev->mem\n",
616 dev->mem->nregions = memory.nregions;
618 for (i = 0; i < memory.nregions; i++) {
620 reg = &dev->mem->regions[i];
622 reg->guest_phys_addr = memory.regions[i].guest_phys_addr;
623 reg->guest_user_addr = memory.regions[i].userspace_addr;
624 reg->size = memory.regions[i].memory_size;
627 mmap_offset = memory.regions[i].mmap_offset;
628 mmap_size = reg->size + mmap_offset;
630 /* mmap() without flag of MAP_ANONYMOUS, should be called
631 * with length argument aligned with hugepagesz at older
632 * longterm version Linux, like 2.6.32 and 3.2.72, or
633 * mmap() will fail with EINVAL.
635 * to avoid failure, make sure in caller to keep length
638 alignment = get_blk_size(fd);
639 if (alignment == (uint64_t)-1) {
640 RTE_LOG(ERR, VHOST_CONFIG,
641 "couldn't get hugepage size through fstat\n");
644 mmap_size = RTE_ALIGN_CEIL(mmap_size, alignment);
646 mmap_addr = mmap(NULL, mmap_size, PROT_READ | PROT_WRITE,
647 MAP_SHARED | MAP_POPULATE, fd, 0);
649 if (mmap_addr == MAP_FAILED) {
650 RTE_LOG(ERR, VHOST_CONFIG,
651 "mmap region %u failed.\n", i);
655 reg->mmap_addr = mmap_addr;
656 reg->mmap_size = mmap_size;
657 reg->host_user_addr = (uint64_t)(uintptr_t)mmap_addr +
660 if (dev->dequeue_zero_copy)
661 add_guest_pages(dev, reg, alignment);
663 RTE_LOG(INFO, VHOST_CONFIG,
664 "guest memory region %u, size: 0x%" PRIx64 "\n"
665 "\t guest physical addr: 0x%" PRIx64 "\n"
666 "\t guest virtual addr: 0x%" PRIx64 "\n"
667 "\t host virtual addr: 0x%" PRIx64 "\n"
668 "\t mmap addr : 0x%" PRIx64 "\n"
669 "\t mmap size : 0x%" PRIx64 "\n"
670 "\t mmap align: 0x%" PRIx64 "\n"
671 "\t mmap off : 0x%" PRIx64 "\n",
673 reg->guest_phys_addr,
674 reg->guest_user_addr,
676 (uint64_t)(uintptr_t)mmap_addr,
682 dump_guest_pages(dev);
687 free_mem_region(dev);
694 vq_is_ready(struct vhost_virtqueue *vq)
696 return vq && vq->desc && vq->avail && vq->used &&
697 vq->kickfd != VIRTIO_UNINITIALIZED_EVENTFD &&
698 vq->callfd != VIRTIO_UNINITIALIZED_EVENTFD;
702 virtio_is_ready(struct virtio_net *dev)
704 struct vhost_virtqueue *vq;
707 if (dev->nr_vring == 0)
710 for (i = 0; i < dev->nr_vring; i++) {
711 vq = dev->virtqueue[i];
713 if (!vq_is_ready(vq))
717 RTE_LOG(INFO, VHOST_CONFIG,
718 "virtio is now ready for processing.\n");
723 vhost_user_set_vring_call(struct virtio_net *dev, struct VhostUserMsg *pmsg)
725 struct vhost_vring_file file;
726 struct vhost_virtqueue *vq;
728 file.index = pmsg->payload.u64 & VHOST_USER_VRING_IDX_MASK;
729 if (pmsg->payload.u64 & VHOST_USER_VRING_NOFD_MASK)
730 file.fd = VIRTIO_INVALID_EVENTFD;
732 file.fd = pmsg->fds[0];
733 RTE_LOG(INFO, VHOST_CONFIG,
734 "vring call idx:%d file:%d\n", file.index, file.fd);
736 vq = dev->virtqueue[file.index];
740 vq->callfd = file.fd;
744 vhost_user_set_vring_kick(struct virtio_net **pdev, struct VhostUserMsg *pmsg)
746 struct vhost_vring_file file;
747 struct vhost_virtqueue *vq;
748 struct virtio_net *dev = *pdev;
750 file.index = pmsg->payload.u64 & VHOST_USER_VRING_IDX_MASK;
751 if (pmsg->payload.u64 & VHOST_USER_VRING_NOFD_MASK)
752 file.fd = VIRTIO_INVALID_EVENTFD;
754 file.fd = pmsg->fds[0];
755 RTE_LOG(INFO, VHOST_CONFIG,
756 "vring kick idx:%d file:%d\n", file.index, file.fd);
758 /* Interpret ring addresses only when ring is started. */
759 dev = translate_ring_addresses(dev, file.index);
765 vq = dev->virtqueue[file.index];
768 * When VHOST_USER_F_PROTOCOL_FEATURES is not negotiated,
769 * the ring starts already enabled. Otherwise, it is enabled via
770 * the SET_VRING_ENABLE message.
772 if (!(dev->features & (1ULL << VHOST_USER_F_PROTOCOL_FEATURES)))
777 vq->kickfd = file.fd;
781 free_zmbufs(struct vhost_virtqueue *vq)
783 struct zcopy_mbuf *zmbuf, *next;
785 for (zmbuf = TAILQ_FIRST(&vq->zmbuf_list);
786 zmbuf != NULL; zmbuf = next) {
787 next = TAILQ_NEXT(zmbuf, next);
789 rte_pktmbuf_free(zmbuf->mbuf);
790 TAILQ_REMOVE(&vq->zmbuf_list, zmbuf, next);
793 rte_free(vq->zmbufs);
797 * when virtio is stopped, qemu will send us the GET_VRING_BASE message.
800 vhost_user_get_vring_base(struct virtio_net *dev,
803 struct vhost_virtqueue *vq = dev->virtqueue[msg->payload.state.index];
805 /* We have to stop the queue (virtio) if it is running. */
806 if (dev->flags & VIRTIO_DEV_RUNNING) {
807 dev->flags &= ~VIRTIO_DEV_RUNNING;
808 dev->notify_ops->destroy_device(dev->vid);
811 dev->flags &= ~VIRTIO_DEV_READY;
813 /* Here we are safe to get the last used index */
814 msg->payload.state.num = vq->last_used_idx;
816 RTE_LOG(INFO, VHOST_CONFIG,
817 "vring base idx:%d file:%d\n", msg->payload.state.index,
818 msg->payload.state.num);
820 * Based on current qemu vhost-user implementation, this message is
821 * sent and only sent in vhost_vring_stop.
822 * TODO: cleanup the vring, it isn't usable since here.
827 vq->kickfd = VIRTIO_UNINITIALIZED_EVENTFD;
829 if (dev->dequeue_zero_copy)
831 rte_free(vq->shadow_used_ring);
832 vq->shadow_used_ring = NULL;
834 rte_free(vq->batch_copy_elems);
835 vq->batch_copy_elems = NULL;
841 * when virtio queues are ready to work, qemu will send us to
842 * enable the virtio queue pair.
845 vhost_user_set_vring_enable(struct virtio_net *dev,
848 int enable = (int)msg->payload.state.num;
850 RTE_LOG(INFO, VHOST_CONFIG,
851 "set queue enable: %d to qp idx: %d\n",
852 enable, msg->payload.state.index);
854 if (dev->notify_ops->vring_state_changed)
855 dev->notify_ops->vring_state_changed(dev->vid,
856 msg->payload.state.index, enable);
858 dev->virtqueue[msg->payload.state.index]->enabled = enable;
864 vhost_user_get_protocol_features(struct virtio_net *dev,
865 struct VhostUserMsg *msg)
867 uint64_t features, protocol_features = VHOST_USER_PROTOCOL_FEATURES;
869 rte_vhost_driver_get_features(dev->ifname, &features);
872 * REPLY_ACK protocol feature is only mandatory for now
873 * for IOMMU feature. If IOMMU is explicitly disabled by the
874 * application, disable also REPLY_ACK feature for older buggy
875 * Qemu versions (from v2.7.0 to v2.9.0).
877 if (!(features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)))
878 protocol_features &= ~(1ULL << VHOST_USER_PROTOCOL_F_REPLY_ACK);
880 msg->payload.u64 = protocol_features;
881 msg->size = sizeof(msg->payload.u64);
885 vhost_user_set_protocol_features(struct virtio_net *dev,
886 uint64_t protocol_features)
888 if (protocol_features & ~VHOST_USER_PROTOCOL_FEATURES)
891 dev->protocol_features = protocol_features;
895 vhost_user_set_log_base(struct virtio_net *dev, struct VhostUserMsg *msg)
897 int fd = msg->fds[0];
902 RTE_LOG(ERR, VHOST_CONFIG, "invalid log fd: %d\n", fd);
906 if (msg->size != sizeof(VhostUserLog)) {
907 RTE_LOG(ERR, VHOST_CONFIG,
908 "invalid log base msg size: %"PRId32" != %d\n",
909 msg->size, (int)sizeof(VhostUserLog));
913 size = msg->payload.log.mmap_size;
914 off = msg->payload.log.mmap_offset;
915 RTE_LOG(INFO, VHOST_CONFIG,
916 "log mmap size: %"PRId64", offset: %"PRId64"\n",
920 * mmap from 0 to workaround a hugepage mmap bug: mmap will
921 * fail when offset is not page size aligned.
923 addr = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
925 if (addr == MAP_FAILED) {
926 RTE_LOG(ERR, VHOST_CONFIG, "mmap log base failed!\n");
931 * Free previously mapped log memory on occasionally
932 * multiple VHOST_USER_SET_LOG_BASE.
935 munmap((void *)(uintptr_t)dev->log_addr, dev->log_size);
937 dev->log_addr = (uint64_t)(uintptr_t)addr;
938 dev->log_base = dev->log_addr + off;
939 dev->log_size = size;
945 * An rarp packet is constructed and broadcasted to notify switches about
946 * the new location of the migrated VM, so that packets from outside will
947 * not be lost after migration.
949 * However, we don't actually "send" a rarp packet here, instead, we set
950 * a flag 'broadcast_rarp' to let rte_vhost_dequeue_burst() inject it.
953 vhost_user_send_rarp(struct virtio_net *dev, struct VhostUserMsg *msg)
955 uint8_t *mac = (uint8_t *)&msg->payload.u64;
957 RTE_LOG(DEBUG, VHOST_CONFIG,
958 ":: mac: %02x:%02x:%02x:%02x:%02x:%02x\n",
959 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
960 memcpy(dev->mac.addr_bytes, mac, 6);
963 * Set the flag to inject a RARP broadcast packet at
964 * rte_vhost_dequeue_burst().
966 * rte_smp_wmb() is for making sure the mac is copied
967 * before the flag is set.
970 rte_atomic16_set(&dev->broadcast_rarp, 1);
976 vhost_user_net_set_mtu(struct virtio_net *dev, struct VhostUserMsg *msg)
978 if (msg->payload.u64 < VIRTIO_MIN_MTU ||
979 msg->payload.u64 > VIRTIO_MAX_MTU) {
980 RTE_LOG(ERR, VHOST_CONFIG, "Invalid MTU size (%"PRIu64")\n",
986 dev->mtu = msg->payload.u64;
992 vhost_user_set_req_fd(struct virtio_net *dev, struct VhostUserMsg *msg)
994 int fd = msg->fds[0];
997 RTE_LOG(ERR, VHOST_CONFIG,
998 "Invalid file descriptor for slave channel (%d)\n",
1003 dev->slave_req_fd = fd;
1009 is_vring_iotlb_update(struct vhost_virtqueue *vq, struct vhost_iotlb_msg *imsg)
1011 struct vhost_vring_addr *ra;
1012 uint64_t start, end;
1015 end = start + imsg->size;
1017 ra = &vq->ring_addrs;
1018 if (ra->desc_user_addr >= start && ra->desc_user_addr < end)
1020 if (ra->avail_user_addr >= start && ra->avail_user_addr < end)
1022 if (ra->used_user_addr >= start && ra->used_user_addr < end)
1029 is_vring_iotlb_invalidate(struct vhost_virtqueue *vq,
1030 struct vhost_iotlb_msg *imsg)
1032 uint64_t istart, iend, vstart, vend;
1034 istart = imsg->iova;
1035 iend = istart + imsg->size - 1;
1037 vstart = (uintptr_t)vq->desc;
1038 vend = vstart + sizeof(struct vring_desc) * vq->size - 1;
1039 if (vstart <= iend && istart <= vend)
1042 vstart = (uintptr_t)vq->avail;
1043 vend = vstart + sizeof(struct vring_avail);
1044 vend += sizeof(uint16_t) * vq->size - 1;
1045 if (vstart <= iend && istart <= vend)
1048 vstart = (uintptr_t)vq->used;
1049 vend = vstart + sizeof(struct vring_used);
1050 vend += sizeof(struct vring_used_elem) * vq->size - 1;
1051 if (vstart <= iend && istart <= vend)
1058 vhost_user_iotlb_msg(struct virtio_net **pdev, struct VhostUserMsg *msg)
1060 struct virtio_net *dev = *pdev;
1061 struct vhost_iotlb_msg *imsg = &msg->payload.iotlb;
1065 switch (imsg->type) {
1066 case VHOST_IOTLB_UPDATE:
1067 vva = qva_to_vva(dev, imsg->uaddr);
1071 for (i = 0; i < dev->nr_vring; i++) {
1072 struct vhost_virtqueue *vq = dev->virtqueue[i];
1074 vhost_user_iotlb_cache_insert(vq, imsg->iova, vva,
1075 imsg->size, imsg->perm);
1077 if (is_vring_iotlb_update(vq, imsg))
1078 *pdev = dev = translate_ring_addresses(dev, i);
1081 case VHOST_IOTLB_INVALIDATE:
1082 for (i = 0; i < dev->nr_vring; i++) {
1083 struct vhost_virtqueue *vq = dev->virtqueue[i];
1085 vhost_user_iotlb_cache_remove(vq, imsg->iova,
1088 if (is_vring_iotlb_invalidate(vq, imsg))
1089 vring_invalidate(dev, vq);
1093 RTE_LOG(ERR, VHOST_CONFIG, "Invalid IOTLB message type (%d)\n",
1101 /* return bytes# of read on success or negative val on failure. */
1103 read_vhost_message(int sockfd, struct VhostUserMsg *msg)
1107 ret = read_fd_message(sockfd, (char *)msg, VHOST_USER_HDR_SIZE,
1108 msg->fds, VHOST_MEMORY_MAX_NREGIONS);
1112 if (msg && msg->size) {
1113 if (msg->size > sizeof(msg->payload)) {
1114 RTE_LOG(ERR, VHOST_CONFIG,
1115 "invalid msg size: %d\n", msg->size);
1118 ret = read(sockfd, &msg->payload, msg->size);
1121 if (ret != (int)msg->size) {
1122 RTE_LOG(ERR, VHOST_CONFIG,
1123 "read control message failed\n");
1132 send_vhost_message(int sockfd, struct VhostUserMsg *msg)
1137 return send_fd_message(sockfd, (char *)msg,
1138 VHOST_USER_HDR_SIZE + msg->size, NULL, 0);
1142 send_vhost_reply(int sockfd, struct VhostUserMsg *msg)
1147 msg->flags &= ~VHOST_USER_VERSION_MASK;
1148 msg->flags &= ~VHOST_USER_NEED_REPLY;
1149 msg->flags |= VHOST_USER_VERSION;
1150 msg->flags |= VHOST_USER_REPLY_MASK;
1152 return send_vhost_message(sockfd, msg);
1156 * Allocate a queue pair if it hasn't been allocated yet
1159 vhost_user_check_and_alloc_queue_pair(struct virtio_net *dev, VhostUserMsg *msg)
1163 switch (msg->request.master) {
1164 case VHOST_USER_SET_VRING_KICK:
1165 case VHOST_USER_SET_VRING_CALL:
1166 case VHOST_USER_SET_VRING_ERR:
1167 vring_idx = msg->payload.u64 & VHOST_USER_VRING_IDX_MASK;
1169 case VHOST_USER_SET_VRING_NUM:
1170 case VHOST_USER_SET_VRING_BASE:
1171 case VHOST_USER_SET_VRING_ENABLE:
1172 vring_idx = msg->payload.state.index;
1174 case VHOST_USER_SET_VRING_ADDR:
1175 vring_idx = msg->payload.addr.index;
1181 if (vring_idx >= VHOST_MAX_VRING) {
1182 RTE_LOG(ERR, VHOST_CONFIG,
1183 "invalid vring index: %u\n", vring_idx);
1187 if (dev->virtqueue[vring_idx])
1190 return alloc_vring_queue(dev, vring_idx);
1194 vhost_user_msg_handler(int vid, int fd)
1196 struct virtio_net *dev;
1197 struct VhostUserMsg msg;
1200 dev = get_device(vid);
1204 if (!dev->notify_ops) {
1205 dev->notify_ops = vhost_driver_callback_get(dev->ifname);
1206 if (!dev->notify_ops) {
1207 RTE_LOG(ERR, VHOST_CONFIG,
1208 "failed to get callback ops for driver %s\n",
1214 ret = read_vhost_message(fd, &msg);
1215 if (ret <= 0 || msg.request.master >= VHOST_USER_MAX) {
1217 RTE_LOG(ERR, VHOST_CONFIG,
1218 "vhost read message failed\n");
1220 RTE_LOG(INFO, VHOST_CONFIG,
1221 "vhost peer closed\n");
1223 RTE_LOG(ERR, VHOST_CONFIG,
1224 "vhost read incorrect message\n");
1230 if (msg.request.master != VHOST_USER_IOTLB_MSG)
1231 RTE_LOG(INFO, VHOST_CONFIG, "read message %s\n",
1232 vhost_message_str[msg.request.master]);
1234 RTE_LOG(DEBUG, VHOST_CONFIG, "read message %s\n",
1235 vhost_message_str[msg.request.master]);
1237 ret = vhost_user_check_and_alloc_queue_pair(dev, &msg);
1239 RTE_LOG(ERR, VHOST_CONFIG,
1240 "failed to alloc queue\n");
1244 switch (msg.request.master) {
1245 case VHOST_USER_GET_FEATURES:
1246 msg.payload.u64 = vhost_user_get_features(dev);
1247 msg.size = sizeof(msg.payload.u64);
1248 send_vhost_reply(fd, &msg);
1250 case VHOST_USER_SET_FEATURES:
1251 vhost_user_set_features(dev, msg.payload.u64);
1254 case VHOST_USER_GET_PROTOCOL_FEATURES:
1255 vhost_user_get_protocol_features(dev, &msg);
1256 send_vhost_reply(fd, &msg);
1258 case VHOST_USER_SET_PROTOCOL_FEATURES:
1259 vhost_user_set_protocol_features(dev, msg.payload.u64);
1262 case VHOST_USER_SET_OWNER:
1263 vhost_user_set_owner();
1265 case VHOST_USER_RESET_OWNER:
1266 vhost_user_reset_owner(dev);
1269 case VHOST_USER_SET_MEM_TABLE:
1270 ret = vhost_user_set_mem_table(dev, &msg);
1273 case VHOST_USER_SET_LOG_BASE:
1274 vhost_user_set_log_base(dev, &msg);
1276 /* it needs a reply */
1277 msg.size = sizeof(msg.payload.u64);
1278 send_vhost_reply(fd, &msg);
1280 case VHOST_USER_SET_LOG_FD:
1282 RTE_LOG(INFO, VHOST_CONFIG, "not implemented.\n");
1285 case VHOST_USER_SET_VRING_NUM:
1286 vhost_user_set_vring_num(dev, &msg);
1288 case VHOST_USER_SET_VRING_ADDR:
1289 vhost_user_set_vring_addr(&dev, &msg);
1291 case VHOST_USER_SET_VRING_BASE:
1292 vhost_user_set_vring_base(dev, &msg);
1295 case VHOST_USER_GET_VRING_BASE:
1296 vhost_user_get_vring_base(dev, &msg);
1297 msg.size = sizeof(msg.payload.state);
1298 send_vhost_reply(fd, &msg);
1301 case VHOST_USER_SET_VRING_KICK:
1302 vhost_user_set_vring_kick(&dev, &msg);
1304 case VHOST_USER_SET_VRING_CALL:
1305 vhost_user_set_vring_call(dev, &msg);
1308 case VHOST_USER_SET_VRING_ERR:
1309 if (!(msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK))
1311 RTE_LOG(INFO, VHOST_CONFIG, "not implemented\n");
1314 case VHOST_USER_GET_QUEUE_NUM:
1315 msg.payload.u64 = VHOST_MAX_QUEUE_PAIRS;
1316 msg.size = sizeof(msg.payload.u64);
1317 send_vhost_reply(fd, &msg);
1320 case VHOST_USER_SET_VRING_ENABLE:
1321 vhost_user_set_vring_enable(dev, &msg);
1323 case VHOST_USER_SEND_RARP:
1324 vhost_user_send_rarp(dev, &msg);
1327 case VHOST_USER_NET_SET_MTU:
1328 ret = vhost_user_net_set_mtu(dev, &msg);
1331 case VHOST_USER_SET_SLAVE_REQ_FD:
1332 ret = vhost_user_set_req_fd(dev, &msg);
1335 case VHOST_USER_IOTLB_MSG:
1336 ret = vhost_user_iotlb_msg(&dev, &msg);
1345 if (msg.flags & VHOST_USER_NEED_REPLY) {
1346 msg.payload.u64 = !!ret;
1347 msg.size = sizeof(msg.payload.u64);
1348 send_vhost_reply(fd, &msg);
1351 if (!(dev->flags & VIRTIO_DEV_RUNNING) && virtio_is_ready(dev)) {
1352 dev->flags |= VIRTIO_DEV_READY;
1354 if (!(dev->flags & VIRTIO_DEV_RUNNING)) {
1355 if (dev->dequeue_zero_copy) {
1356 RTE_LOG(INFO, VHOST_CONFIG,
1357 "dequeue zero copy is enabled\n");
1360 if (dev->notify_ops->new_device(dev->vid) == 0)
1361 dev->flags |= VIRTIO_DEV_RUNNING;
1369 vhost_user_iotlb_miss(struct virtio_net *dev, uint64_t iova, uint8_t perm)
1372 struct VhostUserMsg msg = {
1373 .request.slave = VHOST_USER_SLAVE_IOTLB_MSG,
1374 .flags = VHOST_USER_VERSION,
1375 .size = sizeof(msg.payload.iotlb),
1379 .type = VHOST_IOTLB_MISS,
1383 ret = send_vhost_message(dev->slave_req_fd, &msg);
1385 RTE_LOG(ERR, VHOST_CONFIG,
1386 "Failed to send IOTLB miss message (%d)\n",