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>
44 #ifdef RTE_LIBRTE_VHOST_NUMA
48 #include <rte_common.h>
49 #include <rte_malloc.h>
53 #include "vhost_user.h"
55 static const char *vhost_message_str[VHOST_USER_MAX] = {
56 [VHOST_USER_NONE] = "VHOST_USER_NONE",
57 [VHOST_USER_GET_FEATURES] = "VHOST_USER_GET_FEATURES",
58 [VHOST_USER_SET_FEATURES] = "VHOST_USER_SET_FEATURES",
59 [VHOST_USER_SET_OWNER] = "VHOST_USER_SET_OWNER",
60 [VHOST_USER_RESET_OWNER] = "VHOST_USER_RESET_OWNER",
61 [VHOST_USER_SET_MEM_TABLE] = "VHOST_USER_SET_MEM_TABLE",
62 [VHOST_USER_SET_LOG_BASE] = "VHOST_USER_SET_LOG_BASE",
63 [VHOST_USER_SET_LOG_FD] = "VHOST_USER_SET_LOG_FD",
64 [VHOST_USER_SET_VRING_NUM] = "VHOST_USER_SET_VRING_NUM",
65 [VHOST_USER_SET_VRING_ADDR] = "VHOST_USER_SET_VRING_ADDR",
66 [VHOST_USER_SET_VRING_BASE] = "VHOST_USER_SET_VRING_BASE",
67 [VHOST_USER_GET_VRING_BASE] = "VHOST_USER_GET_VRING_BASE",
68 [VHOST_USER_SET_VRING_KICK] = "VHOST_USER_SET_VRING_KICK",
69 [VHOST_USER_SET_VRING_CALL] = "VHOST_USER_SET_VRING_CALL",
70 [VHOST_USER_SET_VRING_ERR] = "VHOST_USER_SET_VRING_ERR",
71 [VHOST_USER_GET_PROTOCOL_FEATURES] = "VHOST_USER_GET_PROTOCOL_FEATURES",
72 [VHOST_USER_SET_PROTOCOL_FEATURES] = "VHOST_USER_SET_PROTOCOL_FEATURES",
73 [VHOST_USER_GET_QUEUE_NUM] = "VHOST_USER_GET_QUEUE_NUM",
74 [VHOST_USER_SET_VRING_ENABLE] = "VHOST_USER_SET_VRING_ENABLE",
75 [VHOST_USER_SEND_RARP] = "VHOST_USER_SEND_RARP",
84 ret = fstat(fd, &stat);
85 return ret == -1 ? (uint64_t)-1 : (uint64_t)stat.st_blksize;
89 free_mem_region(struct virtio_net *dev)
92 struct virtio_memory_region *reg;
94 if (!dev || !dev->mem)
97 for (i = 0; i < dev->mem->nregions; i++) {
98 reg = &dev->mem->regions[i];
99 if (reg->host_user_addr) {
100 munmap(reg->mmap_addr, reg->mmap_size);
107 vhost_backend_cleanup(struct virtio_net *dev)
110 free_mem_region(dev);
115 free(dev->guest_pages);
116 dev->guest_pages = NULL;
119 munmap((void *)(uintptr_t)dev->log_addr, dev->log_size);
125 * This function just returns success at the moment unless
126 * the device hasn't been initialised.
129 vhost_user_set_owner(void)
135 vhost_user_reset_owner(struct virtio_net *dev)
137 if (dev->flags & VIRTIO_DEV_RUNNING) {
138 dev->flags &= ~VIRTIO_DEV_RUNNING;
139 notify_ops->destroy_device(dev->vid);
142 cleanup_device(dev, 0);
148 * The features that we support are requested.
151 vhost_user_get_features(void)
153 return VHOST_FEATURES;
157 * We receive the negotiated features supported by us and the virtio device.
160 vhost_user_set_features(struct virtio_net *dev, uint64_t features)
162 if (features & ~VHOST_FEATURES)
165 dev->features = features;
167 ((1 << VIRTIO_NET_F_MRG_RXBUF) | (1ULL << VIRTIO_F_VERSION_1))) {
168 dev->vhost_hlen = sizeof(struct virtio_net_hdr_mrg_rxbuf);
170 dev->vhost_hlen = sizeof(struct virtio_net_hdr);
172 VHOST_LOG_DEBUG(VHOST_CONFIG,
173 "(%d) mergeable RX buffers %s, virtio 1 %s\n",
175 (dev->features & (1 << VIRTIO_NET_F_MRG_RXBUF)) ? "on" : "off",
176 (dev->features & (1ULL << VIRTIO_F_VERSION_1)) ? "on" : "off");
182 * The virtio device sends us the size of the descriptor ring.
185 vhost_user_set_vring_num(struct virtio_net *dev,
186 struct vhost_vring_state *state)
188 struct vhost_virtqueue *vq = dev->virtqueue[state->index];
190 vq->size = state->num;
192 if (dev->dequeue_zero_copy) {
194 vq->last_zmbuf_idx = 0;
195 vq->zmbuf_size = vq->size;
196 vq->zmbufs = rte_zmalloc(NULL, vq->zmbuf_size *
197 sizeof(struct zcopy_mbuf), 0);
198 if (vq->zmbufs == NULL) {
199 RTE_LOG(WARNING, VHOST_CONFIG,
200 "failed to allocate mem for zero copy; "
201 "zero copy is force disabled\n");
202 dev->dequeue_zero_copy = 0;
206 vq->shadow_used_ring = rte_malloc(NULL,
207 vq->size * sizeof(struct vring_used_elem),
208 RTE_CACHE_LINE_SIZE);
209 if (!vq->shadow_used_ring) {
210 RTE_LOG(ERR, VHOST_CONFIG,
211 "failed to allocate memory for shadow used ring.\n");
219 * Reallocate virtio_dev and vhost_virtqueue data structure to make them on the
220 * same numa node as the memory of vring descriptor.
222 #ifdef RTE_LIBRTE_VHOST_NUMA
223 static struct virtio_net*
224 numa_realloc(struct virtio_net *dev, int index)
226 int oldnode, newnode;
227 struct virtio_net *old_dev;
228 struct vhost_virtqueue *old_vq, *vq;
232 * vq is allocated on pairs, we should try to do realloc
233 * on first queue of one queue pair only.
235 if (index % VIRTIO_QNUM != 0)
239 vq = old_vq = dev->virtqueue[index];
241 ret = get_mempolicy(&newnode, NULL, 0, old_vq->desc,
242 MPOL_F_NODE | MPOL_F_ADDR);
244 /* check if we need to reallocate vq */
245 ret |= get_mempolicy(&oldnode, NULL, 0, old_vq,
246 MPOL_F_NODE | MPOL_F_ADDR);
248 RTE_LOG(ERR, VHOST_CONFIG,
249 "Unable to get vq numa information.\n");
252 if (oldnode != newnode) {
253 RTE_LOG(INFO, VHOST_CONFIG,
254 "reallocate vq from %d to %d node\n", oldnode, newnode);
255 vq = rte_malloc_socket(NULL, sizeof(*vq) * VIRTIO_QNUM, 0,
260 memcpy(vq, old_vq, sizeof(*vq) * VIRTIO_QNUM);
264 /* check if we need to reallocate dev */
265 ret = get_mempolicy(&oldnode, NULL, 0, old_dev,
266 MPOL_F_NODE | MPOL_F_ADDR);
268 RTE_LOG(ERR, VHOST_CONFIG,
269 "Unable to get dev numa information.\n");
272 if (oldnode != newnode) {
273 RTE_LOG(INFO, VHOST_CONFIG,
274 "reallocate dev from %d to %d node\n",
276 dev = rte_malloc_socket(NULL, sizeof(*dev), 0, newnode);
282 memcpy(dev, old_dev, sizeof(*dev));
287 dev->virtqueue[index] = vq;
288 dev->virtqueue[index + 1] = vq + 1;
289 vhost_devices[dev->vid] = dev;
294 static struct virtio_net*
295 numa_realloc(struct virtio_net *dev, int index __rte_unused)
302 * Converts QEMU virtual address to Vhost virtual address. This function is
303 * used to convert the ring addresses to our address space.
306 qva_to_vva(struct virtio_net *dev, uint64_t qva, uint64_t *len)
308 struct virtio_memory_region *r;
311 /* Find the region where the address lives. */
312 for (i = 0; i < dev->mem->nregions; i++) {
313 r = &dev->mem->regions[i];
315 if (qva >= r->guest_user_addr &&
316 qva < r->guest_user_addr + r->size) {
318 if (unlikely(*len > r->guest_user_addr + r->size - qva))
319 *len = r->guest_user_addr + r->size - qva;
321 return qva - r->guest_user_addr +
331 * The virtio device sends us the desc, used and avail ring addresses.
332 * This function then converts these to our address space.
335 vhost_user_set_vring_addr(struct virtio_net **pdev,
336 struct vhost_vring_addr *addr)
338 struct vhost_virtqueue *vq;
339 struct virtio_net *dev = *pdev;
340 uint64_t size, req_size;
342 if (dev->mem == NULL)
345 /* addr->index refers to the queue index. The txq 1, rxq is 0. */
346 vq = dev->virtqueue[addr->index];
348 /* The addresses are converted from QEMU virtual to Vhost virtual. */
349 req_size = sizeof(struct vring_desc) * vq->size;
351 vq->desc = (struct vring_desc *)(uintptr_t)qva_to_vva(dev,
352 addr->desc_user_addr, &size);
353 if (vq->desc == 0 || size != req_size) {
354 RTE_LOG(ERR, VHOST_CONFIG,
355 "(%d) failed to map desc ring address.\n",
360 dev = numa_realloc(dev, addr->index);
363 vq = dev->virtqueue[addr->index];
365 req_size = sizeof(struct vring_avail) + sizeof(uint16_t) * vq->size;
367 vq->avail = (struct vring_avail *)(uintptr_t)qva_to_vva(dev,
368 addr->avail_user_addr, &size);
369 if (vq->avail == 0 || size != req_size) {
370 RTE_LOG(ERR, VHOST_CONFIG,
371 "(%d) failed to find avail ring address.\n",
376 req_size = sizeof(struct vring_used);
377 req_size += sizeof(struct vring_used_elem) * vq->size;
379 vq->used = (struct vring_used *)(uintptr_t)qva_to_vva(dev,
380 addr->used_user_addr, &size);
381 if (vq->used == 0 || size != req_size) {
382 RTE_LOG(ERR, VHOST_CONFIG,
383 "(%d) failed to find used ring address.\n",
388 if (vq->last_used_idx != vq->used->idx) {
389 RTE_LOG(WARNING, VHOST_CONFIG,
390 "last_used_idx (%u) and vq->used->idx (%u) mismatches; "
391 "some packets maybe resent for Tx and dropped for Rx\n",
392 vq->last_used_idx, vq->used->idx);
393 vq->last_used_idx = vq->used->idx;
394 vq->last_avail_idx = vq->used->idx;
397 vq->log_guest_addr = addr->log_guest_addr;
399 VHOST_LOG_DEBUG(VHOST_CONFIG, "(%d) mapped address desc: %p\n",
401 VHOST_LOG_DEBUG(VHOST_CONFIG, "(%d) mapped address avail: %p\n",
402 dev->vid, vq->avail);
403 VHOST_LOG_DEBUG(VHOST_CONFIG, "(%d) mapped address used: %p\n",
405 VHOST_LOG_DEBUG(VHOST_CONFIG, "(%d) log_guest_addr: %" PRIx64 "\n",
406 dev->vid, vq->log_guest_addr);
412 * The virtio device sends us the available ring last used index.
415 vhost_user_set_vring_base(struct virtio_net *dev,
416 struct vhost_vring_state *state)
418 dev->virtqueue[state->index]->last_used_idx = state->num;
419 dev->virtqueue[state->index]->last_avail_idx = state->num;
425 add_one_guest_page(struct virtio_net *dev, uint64_t guest_phys_addr,
426 uint64_t host_phys_addr, uint64_t size)
428 struct guest_page *page, *last_page;
430 if (dev->nr_guest_pages == dev->max_guest_pages) {
431 dev->max_guest_pages *= 2;
432 dev->guest_pages = realloc(dev->guest_pages,
433 dev->max_guest_pages * sizeof(*page));
434 if (!dev->guest_pages) {
435 RTE_LOG(ERR, VHOST_CONFIG, "cannot realloc guest_pages\n");
440 if (dev->nr_guest_pages > 0) {
441 last_page = &dev->guest_pages[dev->nr_guest_pages - 1];
442 /* merge if the two pages are continuous */
443 if (host_phys_addr == last_page->host_phys_addr +
445 last_page->size += size;
450 page = &dev->guest_pages[dev->nr_guest_pages++];
451 page->guest_phys_addr = guest_phys_addr;
452 page->host_phys_addr = host_phys_addr;
459 add_guest_pages(struct virtio_net *dev, struct virtio_memory_region *reg,
462 uint64_t reg_size = reg->size;
463 uint64_t host_user_addr = reg->host_user_addr;
464 uint64_t guest_phys_addr = reg->guest_phys_addr;
465 uint64_t host_phys_addr;
468 host_phys_addr = rte_mem_virt2phy((void *)(uintptr_t)host_user_addr);
469 size = page_size - (guest_phys_addr & (page_size - 1));
470 size = RTE_MIN(size, reg_size);
472 if (add_one_guest_page(dev, guest_phys_addr, host_phys_addr, size) < 0)
475 host_user_addr += size;
476 guest_phys_addr += size;
479 while (reg_size > 0) {
480 size = RTE_MIN(reg_size, page_size);
481 host_phys_addr = rte_mem_virt2phy((void *)(uintptr_t)
483 if (add_one_guest_page(dev, guest_phys_addr, host_phys_addr,
487 host_user_addr += size;
488 guest_phys_addr += size;
495 #ifdef RTE_LIBRTE_VHOST_DEBUG
496 /* TODO: enable it only in debug mode? */
498 dump_guest_pages(struct virtio_net *dev)
501 struct guest_page *page;
503 for (i = 0; i < dev->nr_guest_pages; i++) {
504 page = &dev->guest_pages[i];
506 RTE_LOG(INFO, VHOST_CONFIG,
507 "guest physical page region %u\n"
508 "\t guest_phys_addr: %" PRIx64 "\n"
509 "\t host_phys_addr : %" PRIx64 "\n"
510 "\t size : %" PRIx64 "\n",
512 page->guest_phys_addr,
513 page->host_phys_addr,
518 #define dump_guest_pages(dev)
522 vhost_memory_changed(struct VhostUserMemory *new,
523 struct virtio_memory *old)
527 if (new->nregions != old->nregions)
530 for (i = 0; i < new->nregions; ++i) {
531 VhostUserMemoryRegion *new_r = &new->regions[i];
532 struct virtio_memory_region *old_r = &old->regions[i];
534 if (new_r->guest_phys_addr != old_r->guest_phys_addr)
536 if (new_r->memory_size != old_r->size)
538 if (new_r->userspace_addr != old_r->guest_user_addr)
546 vhost_user_set_mem_table(struct virtio_net *dev, struct VhostUserMsg *pmsg)
548 struct VhostUserMemory memory = pmsg->payload.memory;
549 struct virtio_memory_region *reg;
552 uint64_t mmap_offset;
557 if (dev->mem && !vhost_memory_changed(&memory, dev->mem)) {
558 RTE_LOG(INFO, VHOST_CONFIG,
559 "(%d) memory regions not changed\n", dev->vid);
561 for (i = 0; i < memory.nregions; i++)
567 /* Remove from the data plane. */
568 if (dev->flags & VIRTIO_DEV_RUNNING) {
569 dev->flags &= ~VIRTIO_DEV_RUNNING;
570 notify_ops->destroy_device(dev->vid);
574 free_mem_region(dev);
579 dev->nr_guest_pages = 0;
580 if (!dev->guest_pages) {
581 dev->max_guest_pages = 8;
582 dev->guest_pages = malloc(dev->max_guest_pages *
583 sizeof(struct guest_page));
586 dev->mem = rte_zmalloc("vhost-mem-table", sizeof(struct virtio_memory) +
587 sizeof(struct virtio_memory_region) * memory.nregions, 0);
588 if (dev->mem == NULL) {
589 RTE_LOG(ERR, VHOST_CONFIG,
590 "(%d) failed to allocate memory for dev->mem\n",
594 dev->mem->nregions = memory.nregions;
596 for (i = 0; i < memory.nregions; i++) {
598 reg = &dev->mem->regions[i];
600 reg->guest_phys_addr = memory.regions[i].guest_phys_addr;
601 reg->guest_user_addr = memory.regions[i].userspace_addr;
602 reg->size = memory.regions[i].memory_size;
605 mmap_offset = memory.regions[i].mmap_offset;
606 mmap_size = reg->size + mmap_offset;
608 /* mmap() without flag of MAP_ANONYMOUS, should be called
609 * with length argument aligned with hugepagesz at older
610 * longterm version Linux, like 2.6.32 and 3.2.72, or
611 * mmap() will fail with EINVAL.
613 * to avoid failure, make sure in caller to keep length
616 alignment = get_blk_size(fd);
617 if (alignment == (uint64_t)-1) {
618 RTE_LOG(ERR, VHOST_CONFIG,
619 "couldn't get hugepage size through fstat\n");
622 mmap_size = RTE_ALIGN_CEIL(mmap_size, alignment);
624 mmap_addr = mmap(NULL, mmap_size, PROT_READ | PROT_WRITE,
625 MAP_SHARED | MAP_POPULATE, fd, 0);
627 if (mmap_addr == MAP_FAILED) {
628 RTE_LOG(ERR, VHOST_CONFIG,
629 "mmap region %u failed.\n", i);
633 reg->mmap_addr = mmap_addr;
634 reg->mmap_size = mmap_size;
635 reg->host_user_addr = (uint64_t)(uintptr_t)mmap_addr +
638 if (dev->dequeue_zero_copy)
639 if (add_guest_pages(dev, reg, alignment) < 0) {
640 RTE_LOG(ERR, VHOST_CONFIG,
641 "adding guest pages to region %u failed.\n",
646 RTE_LOG(INFO, VHOST_CONFIG,
647 "guest memory region %u, size: 0x%" PRIx64 "\n"
648 "\t guest physical addr: 0x%" PRIx64 "\n"
649 "\t guest virtual addr: 0x%" PRIx64 "\n"
650 "\t host virtual addr: 0x%" PRIx64 "\n"
651 "\t mmap addr : 0x%" PRIx64 "\n"
652 "\t mmap size : 0x%" PRIx64 "\n"
653 "\t mmap align: 0x%" PRIx64 "\n"
654 "\t mmap off : 0x%" PRIx64 "\n",
656 reg->guest_phys_addr,
657 reg->guest_user_addr,
659 (uint64_t)(uintptr_t)mmap_addr,
665 dump_guest_pages(dev);
670 free_mem_region(dev);
677 vq_is_ready(struct vhost_virtqueue *vq)
679 return vq && vq->desc &&
680 vq->kickfd != VIRTIO_UNINITIALIZED_EVENTFD &&
681 vq->callfd != VIRTIO_UNINITIALIZED_EVENTFD;
685 virtio_is_ready(struct virtio_net *dev)
687 struct vhost_virtqueue *rvq, *tvq;
690 for (i = 0; i < dev->virt_qp_nb; i++) {
691 rvq = dev->virtqueue[i * VIRTIO_QNUM + VIRTIO_RXQ];
692 tvq = dev->virtqueue[i * VIRTIO_QNUM + VIRTIO_TXQ];
694 if (!vq_is_ready(rvq) || !vq_is_ready(tvq)) {
695 RTE_LOG(INFO, VHOST_CONFIG,
696 "virtio is not ready for processing.\n");
701 RTE_LOG(INFO, VHOST_CONFIG,
702 "virtio is now ready for processing.\n");
707 vhost_user_set_vring_call(struct virtio_net *dev, struct VhostUserMsg *pmsg)
709 struct vhost_vring_file file;
710 struct vhost_virtqueue *vq;
713 file.index = pmsg->payload.u64 & VHOST_USER_VRING_IDX_MASK;
714 if (pmsg->payload.u64 & VHOST_USER_VRING_NOFD_MASK)
715 file.fd = VIRTIO_INVALID_EVENTFD;
717 file.fd = pmsg->fds[0];
718 RTE_LOG(INFO, VHOST_CONFIG,
719 "vring call idx:%d file:%d\n", file.index, file.fd);
722 * FIXME: VHOST_SET_VRING_CALL is the first per-vring message
723 * we get, so we do vring queue pair allocation here.
725 cur_qp_idx = file.index / VIRTIO_QNUM;
726 if (cur_qp_idx + 1 > dev->virt_qp_nb) {
727 if (alloc_vring_queue_pair(dev, cur_qp_idx) < 0)
731 vq = dev->virtqueue[file.index];
737 vq->callfd = file.fd;
741 * In vhost-user, when we receive kick message, will test whether virtio
742 * device is ready for packet processing.
745 vhost_user_set_vring_kick(struct virtio_net *dev, struct VhostUserMsg *pmsg)
747 struct vhost_vring_file file;
748 struct vhost_virtqueue *vq;
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 vq = dev->virtqueue[file.index];
761 vq->kickfd = file.fd;
763 if (virtio_is_ready(dev) && !(dev->flags & VIRTIO_DEV_RUNNING)) {
764 if (dev->dequeue_zero_copy) {
765 RTE_LOG(INFO, VHOST_CONFIG,
766 "dequeue zero copy is enabled\n");
769 if (notify_ops->new_device(dev->vid) == 0)
770 dev->flags |= VIRTIO_DEV_RUNNING;
777 free_zmbufs(struct vhost_virtqueue *vq)
779 struct zcopy_mbuf *zmbuf, *next;
781 for (zmbuf = TAILQ_FIRST(&vq->zmbuf_list);
782 zmbuf != NULL; zmbuf = next) {
783 next = TAILQ_NEXT(zmbuf, next);
785 rte_pktmbuf_free(zmbuf->mbuf);
786 TAILQ_REMOVE(&vq->zmbuf_list, zmbuf, next);
789 rte_free(vq->zmbufs);
793 * when virtio is stopped, qemu will send us the GET_VRING_BASE message.
796 vhost_user_get_vring_base(struct virtio_net *dev,
797 struct vhost_vring_state *state)
799 struct vhost_virtqueue *vq = dev->virtqueue[state->index];
801 /* We have to stop the queue (virtio) if it is running. */
802 if (dev->flags & VIRTIO_DEV_RUNNING) {
803 dev->flags &= ~VIRTIO_DEV_RUNNING;
804 notify_ops->destroy_device(dev->vid);
807 /* Here we are safe to get the last used index */
808 state->num = vq->last_used_idx;
810 RTE_LOG(INFO, VHOST_CONFIG,
811 "vring base idx:%d file:%d\n", state->index, state->num);
813 * Based on current qemu vhost-user implementation, this message is
814 * sent and only sent in vhost_vring_stop.
815 * TODO: cleanup the vring, it isn't usable since here.
820 vq->kickfd = VIRTIO_UNINITIALIZED_EVENTFD;
822 if (dev->dequeue_zero_copy)
824 rte_free(vq->shadow_used_ring);
825 vq->shadow_used_ring = NULL;
831 * when virtio queues are ready to work, qemu will send us to
832 * enable the virtio queue pair.
835 vhost_user_set_vring_enable(struct virtio_net *dev,
836 struct vhost_vring_state *state)
838 int enable = (int)state->num;
840 RTE_LOG(INFO, VHOST_CONFIG,
841 "set queue enable: %d to qp idx: %d\n",
842 enable, state->index);
844 if (notify_ops->vring_state_changed)
845 notify_ops->vring_state_changed(dev->vid, state->index, enable);
847 dev->virtqueue[state->index]->enabled = enable;
853 vhost_user_set_protocol_features(struct virtio_net *dev,
854 uint64_t protocol_features)
856 if (protocol_features & ~VHOST_USER_PROTOCOL_FEATURES) {
857 RTE_LOG(ERR, VHOST_CONFIG,
858 "(%d) received invalid protocol features.\n",
863 dev->protocol_features = protocol_features;
868 vhost_user_set_log_base(struct virtio_net *dev, struct VhostUserMsg *msg)
870 int fd = msg->fds[0];
875 RTE_LOG(ERR, VHOST_CONFIG, "invalid log fd: %d\n", fd);
879 if (msg->size != sizeof(VhostUserLog)) {
880 RTE_LOG(ERR, VHOST_CONFIG,
881 "invalid log base msg size: %"PRId32" != %d\n",
882 msg->size, (int)sizeof(VhostUserLog));
886 size = msg->payload.log.mmap_size;
887 off = msg->payload.log.mmap_offset;
888 RTE_LOG(INFO, VHOST_CONFIG,
889 "log mmap size: %"PRId64", offset: %"PRId64"\n",
893 * mmap from 0 to workaround a hugepage mmap bug: mmap will
894 * fail when offset is not page size aligned.
896 addr = mmap(0, size + off, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
898 if (addr == MAP_FAILED) {
899 RTE_LOG(ERR, VHOST_CONFIG, "mmap log base failed!\n");
904 * Free previously mapped log memory on occasionally
905 * multiple VHOST_USER_SET_LOG_BASE.
908 munmap((void *)(uintptr_t)dev->log_addr, dev->log_size);
910 dev->log_addr = (uint64_t)(uintptr_t)addr;
911 dev->log_base = dev->log_addr + off;
912 dev->log_size = size;
918 * An rarp packet is constructed and broadcasted to notify switches about
919 * the new location of the migrated VM, so that packets from outside will
920 * not be lost after migration.
922 * However, we don't actually "send" a rarp packet here, instead, we set
923 * a flag 'broadcast_rarp' to let rte_vhost_dequeue_burst() inject it.
926 vhost_user_send_rarp(struct virtio_net *dev, struct VhostUserMsg *msg)
928 uint8_t *mac = (uint8_t *)&msg->payload.u64;
930 RTE_LOG(DEBUG, VHOST_CONFIG,
931 ":: mac: %02x:%02x:%02x:%02x:%02x:%02x\n",
932 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
933 memcpy(dev->mac.addr_bytes, mac, 6);
936 * Set the flag to inject a RARP broadcast packet at
937 * rte_vhost_dequeue_burst().
939 * rte_smp_wmb() is for making sure the mac is copied
940 * before the flag is set.
943 rte_atomic16_set(&dev->broadcast_rarp, 1);
948 /* return bytes# of read on success or negative val on failure. */
950 read_vhost_message(int sockfd, struct VhostUserMsg *msg)
954 ret = read_fd_message(sockfd, (char *)msg, VHOST_USER_HDR_SIZE,
955 msg->fds, VHOST_MEMORY_MAX_NREGIONS);
960 if (msg->size > sizeof(msg->payload)) {
961 RTE_LOG(ERR, VHOST_CONFIG,
962 "invalid msg size: %d\n", msg->size);
965 ret = read(sockfd, &msg->payload, msg->size);
968 if (ret != (int)msg->size) {
969 RTE_LOG(ERR, VHOST_CONFIG,
970 "read control message failed\n");
979 send_vhost_message(int sockfd, struct VhostUserMsg *msg)
986 msg->flags &= ~VHOST_USER_VERSION_MASK;
987 msg->flags |= VHOST_USER_VERSION;
988 msg->flags |= VHOST_USER_REPLY_MASK;
990 ret = send_fd_message(sockfd, (char *)msg,
991 VHOST_USER_HDR_SIZE + msg->size, NULL, 0);
997 vhost_user_lock_all_queue_pairs(struct virtio_net *dev)
1000 unsigned int vq_num = 0;
1002 while (vq_num < dev->virt_qp_nb * 2) {
1003 struct vhost_virtqueue *vq = dev->virtqueue[i];
1006 rte_spinlock_lock(&vq->access_lock);
1014 vhost_user_unlock_all_queue_pairs(struct virtio_net *dev)
1017 unsigned int vq_num = 0;
1019 while (vq_num < dev->virt_qp_nb * 2) {
1020 struct vhost_virtqueue *vq = dev->virtqueue[i];
1023 rte_spinlock_unlock(&vq->access_lock);
1031 vhost_user_msg_handler(int vid, int fd)
1033 struct virtio_net *dev;
1034 struct VhostUserMsg msg;
1036 int unlock_required = 0;
1038 dev = get_device(vid);
1042 ret = read_vhost_message(fd, &msg);
1043 if (ret <= 0 || msg.request >= VHOST_USER_MAX) {
1045 RTE_LOG(ERR, VHOST_CONFIG,
1046 "vhost read message failed\n");
1048 RTE_LOG(INFO, VHOST_CONFIG,
1049 "vhost peer closed\n");
1051 RTE_LOG(ERR, VHOST_CONFIG,
1052 "vhost read incorrect message\n");
1057 RTE_LOG(INFO, VHOST_CONFIG, "read message %s\n",
1058 vhost_message_str[msg.request]);
1061 * Note: we don't lock all queues on VHOST_USER_GET_VRING_BASE
1062 * and VHOST_USER_RESET_OWNER, since it is sent when virtio stops
1063 * and device is destroyed. destroy_device waits for queues to be
1064 * inactive, so it is safe. Otherwise taking the access_lock
1065 * would cause a dead lock.
1067 switch (msg.request) {
1068 case VHOST_USER_SET_FEATURES:
1069 case VHOST_USER_SET_PROTOCOL_FEATURES:
1070 case VHOST_USER_SET_OWNER:
1071 case VHOST_USER_SET_MEM_TABLE:
1072 case VHOST_USER_SET_LOG_BASE:
1073 case VHOST_USER_SET_LOG_FD:
1074 case VHOST_USER_SET_VRING_NUM:
1075 case VHOST_USER_SET_VRING_ADDR:
1076 case VHOST_USER_SET_VRING_BASE:
1077 case VHOST_USER_SET_VRING_KICK:
1078 case VHOST_USER_SET_VRING_CALL:
1079 case VHOST_USER_SET_VRING_ERR:
1080 case VHOST_USER_SET_VRING_ENABLE:
1081 case VHOST_USER_SEND_RARP:
1082 vhost_user_lock_all_queue_pairs(dev);
1083 unlock_required = 1;
1091 switch (msg.request) {
1092 case VHOST_USER_GET_FEATURES:
1093 msg.payload.u64 = vhost_user_get_features();
1094 msg.size = sizeof(msg.payload.u64);
1095 send_vhost_message(fd, &msg);
1097 case VHOST_USER_SET_FEATURES:
1098 ret = vhost_user_set_features(dev, msg.payload.u64);
1101 case VHOST_USER_GET_PROTOCOL_FEATURES:
1102 msg.payload.u64 = VHOST_USER_PROTOCOL_FEATURES;
1103 msg.size = sizeof(msg.payload.u64);
1104 send_vhost_message(fd, &msg);
1106 case VHOST_USER_SET_PROTOCOL_FEATURES:
1107 ret = vhost_user_set_protocol_features(dev, msg.payload.u64);
1110 case VHOST_USER_SET_OWNER:
1111 ret = vhost_user_set_owner();
1113 case VHOST_USER_RESET_OWNER:
1114 ret = vhost_user_reset_owner(dev);
1117 case VHOST_USER_SET_MEM_TABLE:
1118 vhost_user_set_mem_table(dev, &msg);
1121 case VHOST_USER_SET_LOG_BASE:
1122 ret = vhost_user_set_log_base(dev, &msg);
1126 * The spec is not clear about it (yet), but QEMU doesn't expect
1127 * any payload in the reply. But a reply is required.
1130 send_vhost_message(fd, &msg);
1132 case VHOST_USER_SET_LOG_FD:
1134 RTE_LOG(INFO, VHOST_CONFIG, "not implemented.\n");
1137 case VHOST_USER_SET_VRING_NUM:
1138 ret = vhost_user_set_vring_num(dev, &msg.payload.state);
1140 case VHOST_USER_SET_VRING_ADDR:
1141 ret = vhost_user_set_vring_addr(&dev, &msg.payload.addr);
1143 case VHOST_USER_SET_VRING_BASE:
1144 ret = vhost_user_set_vring_base(dev, &msg.payload.state);
1147 case VHOST_USER_GET_VRING_BASE:
1148 ret = vhost_user_get_vring_base(dev, &msg.payload.state);
1151 msg.size = sizeof(msg.payload.state);
1152 send_vhost_message(fd, &msg);
1155 case VHOST_USER_SET_VRING_KICK:
1156 ret = vhost_user_set_vring_kick(dev, &msg);
1158 case VHOST_USER_SET_VRING_CALL:
1159 vhost_user_set_vring_call(dev, &msg);
1162 case VHOST_USER_SET_VRING_ERR:
1163 if (!(msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK))
1165 RTE_LOG(INFO, VHOST_CONFIG, "not implemented\n");
1168 case VHOST_USER_GET_QUEUE_NUM:
1169 msg.payload.u64 = VHOST_MAX_QUEUE_PAIRS;
1170 msg.size = sizeof(msg.payload.u64);
1171 send_vhost_message(fd, &msg);
1174 case VHOST_USER_SET_VRING_ENABLE:
1175 ret = vhost_user_set_vring_enable(dev, &msg.payload.state);
1177 case VHOST_USER_SEND_RARP:
1178 ret = vhost_user_send_rarp(dev, &msg);
1186 if (unlock_required)
1187 vhost_user_unlock_all_queue_pairs(dev);
1190 RTE_LOG(ERR, VHOST_CONFIG,
1191 "vhost message handling failed.\n");