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>
52 #include "vhost_user.h"
54 #define VIRTIO_MIN_MTU 68
55 #define VIRTIO_MAX_MTU 65535
57 static const char *vhost_message_str[VHOST_USER_MAX] = {
58 [VHOST_USER_NONE] = "VHOST_USER_NONE",
59 [VHOST_USER_GET_FEATURES] = "VHOST_USER_GET_FEATURES",
60 [VHOST_USER_SET_FEATURES] = "VHOST_USER_SET_FEATURES",
61 [VHOST_USER_SET_OWNER] = "VHOST_USER_SET_OWNER",
62 [VHOST_USER_RESET_OWNER] = "VHOST_USER_RESET_OWNER",
63 [VHOST_USER_SET_MEM_TABLE] = "VHOST_USER_SET_MEM_TABLE",
64 [VHOST_USER_SET_LOG_BASE] = "VHOST_USER_SET_LOG_BASE",
65 [VHOST_USER_SET_LOG_FD] = "VHOST_USER_SET_LOG_FD",
66 [VHOST_USER_SET_VRING_NUM] = "VHOST_USER_SET_VRING_NUM",
67 [VHOST_USER_SET_VRING_ADDR] = "VHOST_USER_SET_VRING_ADDR",
68 [VHOST_USER_SET_VRING_BASE] = "VHOST_USER_SET_VRING_BASE",
69 [VHOST_USER_GET_VRING_BASE] = "VHOST_USER_GET_VRING_BASE",
70 [VHOST_USER_SET_VRING_KICK] = "VHOST_USER_SET_VRING_KICK",
71 [VHOST_USER_SET_VRING_CALL] = "VHOST_USER_SET_VRING_CALL",
72 [VHOST_USER_SET_VRING_ERR] = "VHOST_USER_SET_VRING_ERR",
73 [VHOST_USER_GET_PROTOCOL_FEATURES] = "VHOST_USER_GET_PROTOCOL_FEATURES",
74 [VHOST_USER_SET_PROTOCOL_FEATURES] = "VHOST_USER_SET_PROTOCOL_FEATURES",
75 [VHOST_USER_GET_QUEUE_NUM] = "VHOST_USER_GET_QUEUE_NUM",
76 [VHOST_USER_SET_VRING_ENABLE] = "VHOST_USER_SET_VRING_ENABLE",
77 [VHOST_USER_SEND_RARP] = "VHOST_USER_SEND_RARP",
78 [VHOST_USER_NET_SET_MTU] = "VHOST_USER_NET_SET_MTU",
87 ret = fstat(fd, &stat);
88 return ret == -1 ? (uint64_t)-1 : (uint64_t)stat.st_blksize;
92 free_mem_region(struct virtio_net *dev)
95 struct virtio_memory_region *reg;
97 if (!dev || !dev->mem)
100 for (i = 0; i < dev->mem->nregions; i++) {
101 reg = &dev->mem->regions[i];
102 if (reg->host_user_addr) {
103 munmap(reg->mmap_addr, reg->mmap_size);
110 vhost_backend_cleanup(struct virtio_net *dev)
113 free_mem_region(dev);
118 munmap((void *)(uintptr_t)dev->log_addr, dev->log_size);
124 * This function just returns success at the moment unless
125 * the device hasn't been initialised.
128 vhost_user_set_owner(void)
134 vhost_user_reset_owner(struct virtio_net *dev)
136 if (dev->flags & VIRTIO_DEV_RUNNING) {
137 dev->flags &= ~VIRTIO_DEV_RUNNING;
138 dev->notify_ops->destroy_device(dev->vid);
141 cleanup_device(dev, 0);
147 * The features that we support are requested.
150 vhost_user_get_features(struct virtio_net *dev)
152 uint64_t features = 0;
154 rte_vhost_driver_get_features(dev->ifname, &features);
159 * We receive the negotiated features supported by us and the virtio device.
162 vhost_user_set_features(struct virtio_net *dev, uint64_t features)
164 uint64_t vhost_features = 0;
166 rte_vhost_driver_get_features(dev->ifname, &vhost_features);
167 if (features & ~vhost_features)
170 dev->features = features;
172 ((1 << VIRTIO_NET_F_MRG_RXBUF) | (1ULL << VIRTIO_F_VERSION_1))) {
173 dev->vhost_hlen = sizeof(struct virtio_net_hdr_mrg_rxbuf);
175 dev->vhost_hlen = sizeof(struct virtio_net_hdr);
177 LOG_DEBUG(VHOST_CONFIG,
178 "(%d) mergeable RX buffers %s, virtio 1 %s\n",
180 (dev->features & (1 << VIRTIO_NET_F_MRG_RXBUF)) ? "on" : "off",
181 (dev->features & (1ULL << VIRTIO_F_VERSION_1)) ? "on" : "off");
187 * The virtio device sends us the size of the descriptor ring.
190 vhost_user_set_vring_num(struct virtio_net *dev,
191 struct vhost_vring_state *state)
193 struct vhost_virtqueue *vq = dev->virtqueue[state->index];
195 vq->size = state->num;
197 if (dev->dequeue_zero_copy) {
199 vq->last_zmbuf_idx = 0;
200 vq->zmbuf_size = vq->size;
201 vq->zmbufs = rte_zmalloc(NULL, vq->zmbuf_size *
202 sizeof(struct zcopy_mbuf), 0);
203 if (vq->zmbufs == NULL) {
204 RTE_LOG(WARNING, VHOST_CONFIG,
205 "failed to allocate mem for zero copy; "
206 "zero copy is force disabled\n");
207 dev->dequeue_zero_copy = 0;
211 vq->shadow_used_ring = rte_malloc(NULL,
212 vq->size * sizeof(struct vring_used_elem),
213 RTE_CACHE_LINE_SIZE);
214 if (!vq->shadow_used_ring) {
215 RTE_LOG(ERR, VHOST_CONFIG,
216 "failed to allocate memory for shadow used ring.\n");
224 * Reallocate virtio_dev and vhost_virtqueue data structure to make them on the
225 * same numa node as the memory of vring descriptor.
227 #ifdef RTE_LIBRTE_VHOST_NUMA
228 static struct virtio_net*
229 numa_realloc(struct virtio_net *dev, int index)
231 int oldnode, newnode;
232 struct virtio_net *old_dev;
233 struct vhost_virtqueue *old_vq, *vq;
237 * vq is allocated on pairs, we should try to do realloc
238 * on first queue of one queue pair only.
240 if (index % VIRTIO_QNUM != 0)
244 vq = old_vq = dev->virtqueue[index];
246 ret = get_mempolicy(&newnode, NULL, 0, old_vq->desc,
247 MPOL_F_NODE | MPOL_F_ADDR);
249 /* check if we need to reallocate vq */
250 ret |= get_mempolicy(&oldnode, NULL, 0, old_vq,
251 MPOL_F_NODE | MPOL_F_ADDR);
253 RTE_LOG(ERR, VHOST_CONFIG,
254 "Unable to get vq numa information.\n");
257 if (oldnode != newnode) {
258 RTE_LOG(INFO, VHOST_CONFIG,
259 "reallocate vq from %d to %d node\n", oldnode, newnode);
260 vq = rte_malloc_socket(NULL, sizeof(*vq) * VIRTIO_QNUM, 0,
265 memcpy(vq, old_vq, sizeof(*vq) * VIRTIO_QNUM);
269 /* check if we need to reallocate dev */
270 ret = get_mempolicy(&oldnode, NULL, 0, old_dev,
271 MPOL_F_NODE | MPOL_F_ADDR);
273 RTE_LOG(ERR, VHOST_CONFIG,
274 "Unable to get dev numa information.\n");
277 if (oldnode != newnode) {
278 RTE_LOG(INFO, VHOST_CONFIG,
279 "reallocate dev from %d to %d node\n",
281 dev = rte_malloc_socket(NULL, sizeof(*dev), 0, newnode);
287 memcpy(dev, old_dev, sizeof(*dev));
292 dev->virtqueue[index] = vq;
293 dev->virtqueue[index + 1] = vq + 1;
294 vhost_devices[dev->vid] = dev;
299 static struct virtio_net*
300 numa_realloc(struct virtio_net *dev, int index __rte_unused)
307 * Converts QEMU virtual address to Vhost virtual address. This function is
308 * used to convert the ring addresses to our address space.
311 qva_to_vva(struct virtio_net *dev, uint64_t qva)
313 struct virtio_memory_region *reg;
316 /* Find the region where the address lives. */
317 for (i = 0; i < dev->mem->nregions; i++) {
318 reg = &dev->mem->regions[i];
320 if (qva >= reg->guest_user_addr &&
321 qva < reg->guest_user_addr + reg->size) {
322 return qva - reg->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 *dev, struct vhost_vring_addr *addr)
337 struct vhost_virtqueue *vq;
339 if (dev->mem == NULL)
342 /* addr->index refers to the queue index. The txq 1, rxq is 0. */
343 vq = dev->virtqueue[addr->index];
345 /* The addresses are converted from QEMU virtual to Vhost virtual. */
346 vq->desc = (struct vring_desc *)(uintptr_t)qva_to_vva(dev,
347 addr->desc_user_addr);
349 RTE_LOG(ERR, VHOST_CONFIG,
350 "(%d) failed to find desc ring address.\n",
355 dev = numa_realloc(dev, addr->index);
356 vq = dev->virtqueue[addr->index];
358 vq->avail = (struct vring_avail *)(uintptr_t)qva_to_vva(dev,
359 addr->avail_user_addr);
360 if (vq->avail == 0) {
361 RTE_LOG(ERR, VHOST_CONFIG,
362 "(%d) failed to find avail ring address.\n",
367 vq->used = (struct vring_used *)(uintptr_t)qva_to_vva(dev,
368 addr->used_user_addr);
370 RTE_LOG(ERR, VHOST_CONFIG,
371 "(%d) failed to find used ring address.\n",
376 if (vq->last_used_idx != vq->used->idx) {
377 RTE_LOG(WARNING, VHOST_CONFIG,
378 "last_used_idx (%u) and vq->used->idx (%u) mismatches; "
379 "some packets maybe resent for Tx and dropped for Rx\n",
380 vq->last_used_idx, vq->used->idx);
381 vq->last_used_idx = vq->used->idx;
382 vq->last_avail_idx = vq->used->idx;
385 vq->log_guest_addr = addr->log_guest_addr;
387 LOG_DEBUG(VHOST_CONFIG, "(%d) mapped address desc: %p\n",
389 LOG_DEBUG(VHOST_CONFIG, "(%d) mapped address avail: %p\n",
390 dev->vid, vq->avail);
391 LOG_DEBUG(VHOST_CONFIG, "(%d) mapped address used: %p\n",
393 LOG_DEBUG(VHOST_CONFIG, "(%d) log_guest_addr: %" PRIx64 "\n",
394 dev->vid, vq->log_guest_addr);
400 * The virtio device sends us the available ring last used index.
403 vhost_user_set_vring_base(struct virtio_net *dev,
404 struct vhost_vring_state *state)
406 dev->virtqueue[state->index]->last_used_idx = state->num;
407 dev->virtqueue[state->index]->last_avail_idx = state->num;
413 add_one_guest_page(struct virtio_net *dev, uint64_t guest_phys_addr,
414 uint64_t host_phys_addr, uint64_t size)
416 struct guest_page *page, *last_page;
418 if (dev->nr_guest_pages == dev->max_guest_pages) {
419 dev->max_guest_pages *= 2;
420 dev->guest_pages = realloc(dev->guest_pages,
421 dev->max_guest_pages * sizeof(*page));
424 if (dev->nr_guest_pages > 0) {
425 last_page = &dev->guest_pages[dev->nr_guest_pages - 1];
426 /* merge if the two pages are continuous */
427 if (host_phys_addr == last_page->host_phys_addr +
429 last_page->size += size;
434 page = &dev->guest_pages[dev->nr_guest_pages++];
435 page->guest_phys_addr = guest_phys_addr;
436 page->host_phys_addr = host_phys_addr;
441 add_guest_pages(struct virtio_net *dev, struct virtio_memory_region *reg,
444 uint64_t reg_size = reg->size;
445 uint64_t host_user_addr = reg->host_user_addr;
446 uint64_t guest_phys_addr = reg->guest_phys_addr;
447 uint64_t host_phys_addr;
450 host_phys_addr = rte_mem_virt2phy((void *)(uintptr_t)host_user_addr);
451 size = page_size - (guest_phys_addr & (page_size - 1));
452 size = RTE_MIN(size, reg_size);
454 add_one_guest_page(dev, guest_phys_addr, host_phys_addr, size);
455 host_user_addr += size;
456 guest_phys_addr += size;
459 while (reg_size > 0) {
460 size = RTE_MIN(reg_size, page_size);
461 host_phys_addr = rte_mem_virt2phy((void *)(uintptr_t)
463 add_one_guest_page(dev, guest_phys_addr, host_phys_addr, size);
465 host_user_addr += size;
466 guest_phys_addr += size;
471 #ifdef RTE_LIBRTE_VHOST_DEBUG
472 /* TODO: enable it only in debug mode? */
474 dump_guest_pages(struct virtio_net *dev)
477 struct guest_page *page;
479 for (i = 0; i < dev->nr_guest_pages; i++) {
480 page = &dev->guest_pages[i];
482 RTE_LOG(INFO, VHOST_CONFIG,
483 "guest physical page region %u\n"
484 "\t guest_phys_addr: %" PRIx64 "\n"
485 "\t host_phys_addr : %" PRIx64 "\n"
486 "\t size : %" PRIx64 "\n",
488 page->guest_phys_addr,
489 page->host_phys_addr,
494 #define dump_guest_pages(dev)
498 vhost_user_set_mem_table(struct virtio_net *dev, struct VhostUserMsg *pmsg)
500 struct VhostUserMemory memory = pmsg->payload.memory;
501 struct virtio_memory_region *reg;
504 uint64_t mmap_offset;
509 /* Remove from the data plane. */
510 if (dev->flags & VIRTIO_DEV_RUNNING) {
511 dev->flags &= ~VIRTIO_DEV_RUNNING;
512 dev->notify_ops->destroy_device(dev->vid);
516 free_mem_region(dev);
521 dev->nr_guest_pages = 0;
522 if (!dev->guest_pages) {
523 dev->max_guest_pages = 8;
524 dev->guest_pages = malloc(dev->max_guest_pages *
525 sizeof(struct guest_page));
528 dev->mem = rte_zmalloc("vhost-mem-table", sizeof(struct virtio_memory) +
529 sizeof(struct virtio_memory_region) * memory.nregions, 0);
530 if (dev->mem == NULL) {
531 RTE_LOG(ERR, VHOST_CONFIG,
532 "(%d) failed to allocate memory for dev->mem\n",
536 dev->mem->nregions = memory.nregions;
538 for (i = 0; i < memory.nregions; i++) {
540 reg = &dev->mem->regions[i];
542 reg->guest_phys_addr = memory.regions[i].guest_phys_addr;
543 reg->guest_user_addr = memory.regions[i].userspace_addr;
544 reg->size = memory.regions[i].memory_size;
547 mmap_offset = memory.regions[i].mmap_offset;
548 mmap_size = reg->size + mmap_offset;
550 /* mmap() without flag of MAP_ANONYMOUS, should be called
551 * with length argument aligned with hugepagesz at older
552 * longterm version Linux, like 2.6.32 and 3.2.72, or
553 * mmap() will fail with EINVAL.
555 * to avoid failure, make sure in caller to keep length
558 alignment = get_blk_size(fd);
559 if (alignment == (uint64_t)-1) {
560 RTE_LOG(ERR, VHOST_CONFIG,
561 "couldn't get hugepage size through fstat\n");
564 mmap_size = RTE_ALIGN_CEIL(mmap_size, alignment);
566 mmap_addr = mmap(NULL, mmap_size, PROT_READ | PROT_WRITE,
567 MAP_SHARED | MAP_POPULATE, fd, 0);
569 if (mmap_addr == MAP_FAILED) {
570 RTE_LOG(ERR, VHOST_CONFIG,
571 "mmap region %u failed.\n", i);
575 reg->mmap_addr = mmap_addr;
576 reg->mmap_size = mmap_size;
577 reg->host_user_addr = (uint64_t)(uintptr_t)mmap_addr +
580 if (dev->dequeue_zero_copy)
581 add_guest_pages(dev, reg, alignment);
583 RTE_LOG(INFO, VHOST_CONFIG,
584 "guest memory region %u, size: 0x%" PRIx64 "\n"
585 "\t guest physical addr: 0x%" PRIx64 "\n"
586 "\t guest virtual addr: 0x%" PRIx64 "\n"
587 "\t host virtual addr: 0x%" PRIx64 "\n"
588 "\t mmap addr : 0x%" PRIx64 "\n"
589 "\t mmap size : 0x%" PRIx64 "\n"
590 "\t mmap align: 0x%" PRIx64 "\n"
591 "\t mmap off : 0x%" PRIx64 "\n",
593 reg->guest_phys_addr,
594 reg->guest_user_addr,
596 (uint64_t)(uintptr_t)mmap_addr,
602 dump_guest_pages(dev);
607 free_mem_region(dev);
614 vq_is_ready(struct vhost_virtqueue *vq)
616 return vq && vq->desc &&
617 vq->kickfd != VIRTIO_UNINITIALIZED_EVENTFD &&
618 vq->callfd != VIRTIO_UNINITIALIZED_EVENTFD;
622 virtio_is_ready(struct virtio_net *dev)
624 struct vhost_virtqueue *rvq, *tvq;
627 for (i = 0; i < dev->virt_qp_nb; i++) {
628 rvq = dev->virtqueue[i * VIRTIO_QNUM + VIRTIO_RXQ];
629 tvq = dev->virtqueue[i * VIRTIO_QNUM + VIRTIO_TXQ];
631 if (!vq_is_ready(rvq) || !vq_is_ready(tvq)) {
632 RTE_LOG(INFO, VHOST_CONFIG,
633 "virtio is not ready for processing.\n");
638 RTE_LOG(INFO, VHOST_CONFIG,
639 "virtio is now ready for processing.\n");
644 vhost_user_set_vring_call(struct virtio_net *dev, struct VhostUserMsg *pmsg)
646 struct vhost_vring_file file;
647 struct vhost_virtqueue *vq;
649 file.index = pmsg->payload.u64 & VHOST_USER_VRING_IDX_MASK;
650 if (pmsg->payload.u64 & VHOST_USER_VRING_NOFD_MASK)
651 file.fd = VIRTIO_INVALID_EVENTFD;
653 file.fd = pmsg->fds[0];
654 RTE_LOG(INFO, VHOST_CONFIG,
655 "vring call idx:%d file:%d\n", file.index, file.fd);
657 vq = dev->virtqueue[file.index];
661 vq->callfd = file.fd;
665 * In vhost-user, when we receive kick message, will test whether virtio
666 * device is ready for packet processing.
669 vhost_user_set_vring_kick(struct virtio_net *dev, struct VhostUserMsg *pmsg)
671 struct vhost_vring_file file;
672 struct vhost_virtqueue *vq;
674 file.index = pmsg->payload.u64 & VHOST_USER_VRING_IDX_MASK;
675 if (pmsg->payload.u64 & VHOST_USER_VRING_NOFD_MASK)
676 file.fd = VIRTIO_INVALID_EVENTFD;
678 file.fd = pmsg->fds[0];
679 RTE_LOG(INFO, VHOST_CONFIG,
680 "vring kick idx:%d file:%d\n", file.index, file.fd);
682 vq = dev->virtqueue[file.index];
685 vq->kickfd = file.fd;
687 if (virtio_is_ready(dev)) {
688 dev->flags |= VIRTIO_DEV_READY;
690 if (!(dev->flags & VIRTIO_DEV_RUNNING)) {
691 if (dev->dequeue_zero_copy) {
692 RTE_LOG(INFO, VHOST_CONFIG,
693 "dequeue zero copy is enabled\n");
696 if (dev->notify_ops->new_device(dev->vid) == 0)
697 dev->flags |= VIRTIO_DEV_RUNNING;
703 free_zmbufs(struct vhost_virtqueue *vq)
705 struct zcopy_mbuf *zmbuf, *next;
707 for (zmbuf = TAILQ_FIRST(&vq->zmbuf_list);
708 zmbuf != NULL; zmbuf = next) {
709 next = TAILQ_NEXT(zmbuf, next);
711 rte_pktmbuf_free(zmbuf->mbuf);
712 TAILQ_REMOVE(&vq->zmbuf_list, zmbuf, next);
715 rte_free(vq->zmbufs);
719 * when virtio is stopped, qemu will send us the GET_VRING_BASE message.
722 vhost_user_get_vring_base(struct virtio_net *dev,
723 struct vhost_vring_state *state)
725 struct vhost_virtqueue *vq = dev->virtqueue[state->index];
727 /* We have to stop the queue (virtio) if it is running. */
728 if (dev->flags & VIRTIO_DEV_RUNNING) {
729 dev->flags &= ~VIRTIO_DEV_RUNNING;
730 dev->notify_ops->destroy_device(dev->vid);
733 dev->flags &= ~VIRTIO_DEV_READY;
735 /* Here we are safe to get the last used index */
736 state->num = vq->last_used_idx;
738 RTE_LOG(INFO, VHOST_CONFIG,
739 "vring base idx:%d file:%d\n", state->index, state->num);
741 * Based on current qemu vhost-user implementation, this message is
742 * sent and only sent in vhost_vring_stop.
743 * TODO: cleanup the vring, it isn't usable since here.
748 vq->kickfd = VIRTIO_UNINITIALIZED_EVENTFD;
750 if (dev->dequeue_zero_copy)
752 rte_free(vq->shadow_used_ring);
753 vq->shadow_used_ring = NULL;
759 * when virtio queues are ready to work, qemu will send us to
760 * enable the virtio queue pair.
763 vhost_user_set_vring_enable(struct virtio_net *dev,
764 struct vhost_vring_state *state)
766 int enable = (int)state->num;
768 RTE_LOG(INFO, VHOST_CONFIG,
769 "set queue enable: %d to qp idx: %d\n",
770 enable, state->index);
772 if (dev->notify_ops->vring_state_changed)
773 dev->notify_ops->vring_state_changed(dev->vid, state->index, enable);
775 dev->virtqueue[state->index]->enabled = enable;
781 vhost_user_set_protocol_features(struct virtio_net *dev,
782 uint64_t protocol_features)
784 if (protocol_features & ~VHOST_USER_PROTOCOL_FEATURES)
787 dev->protocol_features = protocol_features;
791 vhost_user_set_log_base(struct virtio_net *dev, struct VhostUserMsg *msg)
793 int fd = msg->fds[0];
798 RTE_LOG(ERR, VHOST_CONFIG, "invalid log fd: %d\n", fd);
802 if (msg->size != sizeof(VhostUserLog)) {
803 RTE_LOG(ERR, VHOST_CONFIG,
804 "invalid log base msg size: %"PRId32" != %d\n",
805 msg->size, (int)sizeof(VhostUserLog));
809 size = msg->payload.log.mmap_size;
810 off = msg->payload.log.mmap_offset;
811 RTE_LOG(INFO, VHOST_CONFIG,
812 "log mmap size: %"PRId64", offset: %"PRId64"\n",
816 * mmap from 0 to workaround a hugepage mmap bug: mmap will
817 * fail when offset is not page size aligned.
819 addr = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
821 if (addr == MAP_FAILED) {
822 RTE_LOG(ERR, VHOST_CONFIG, "mmap log base failed!\n");
827 * Free previously mapped log memory on occasionally
828 * multiple VHOST_USER_SET_LOG_BASE.
831 munmap((void *)(uintptr_t)dev->log_addr, dev->log_size);
833 dev->log_addr = (uint64_t)(uintptr_t)addr;
834 dev->log_base = dev->log_addr + off;
835 dev->log_size = size;
841 * An rarp packet is constructed and broadcasted to notify switches about
842 * the new location of the migrated VM, so that packets from outside will
843 * not be lost after migration.
845 * However, we don't actually "send" a rarp packet here, instead, we set
846 * a flag 'broadcast_rarp' to let rte_vhost_dequeue_burst() inject it.
849 vhost_user_send_rarp(struct virtio_net *dev, struct VhostUserMsg *msg)
851 uint8_t *mac = (uint8_t *)&msg->payload.u64;
853 RTE_LOG(DEBUG, VHOST_CONFIG,
854 ":: mac: %02x:%02x:%02x:%02x:%02x:%02x\n",
855 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
856 memcpy(dev->mac.addr_bytes, mac, 6);
859 * Set the flag to inject a RARP broadcast packet at
860 * rte_vhost_dequeue_burst().
862 * rte_smp_wmb() is for making sure the mac is copied
863 * before the flag is set.
866 rte_atomic16_set(&dev->broadcast_rarp, 1);
872 vhost_user_net_set_mtu(struct virtio_net *dev, struct VhostUserMsg *msg)
874 if (msg->payload.u64 < VIRTIO_MIN_MTU ||
875 msg->payload.u64 > VIRTIO_MAX_MTU) {
876 RTE_LOG(ERR, VHOST_CONFIG, "Invalid MTU size (%"PRIu64")\n",
882 dev->mtu = msg->payload.u64;
887 /* return bytes# of read on success or negative val on failure. */
889 read_vhost_message(int sockfd, struct VhostUserMsg *msg)
893 ret = read_fd_message(sockfd, (char *)msg, VHOST_USER_HDR_SIZE,
894 msg->fds, VHOST_MEMORY_MAX_NREGIONS);
898 if (msg && msg->size) {
899 if (msg->size > sizeof(msg->payload)) {
900 RTE_LOG(ERR, VHOST_CONFIG,
901 "invalid msg size: %d\n", msg->size);
904 ret = read(sockfd, &msg->payload, msg->size);
907 if (ret != (int)msg->size) {
908 RTE_LOG(ERR, VHOST_CONFIG,
909 "read control message failed\n");
918 send_vhost_message(int sockfd, struct VhostUserMsg *msg)
925 msg->flags &= ~VHOST_USER_VERSION_MASK;
926 msg->flags &= ~VHOST_USER_NEED_REPLY;
927 msg->flags |= VHOST_USER_VERSION;
928 msg->flags |= VHOST_USER_REPLY_MASK;
930 ret = send_fd_message(sockfd, (char *)msg,
931 VHOST_USER_HDR_SIZE + msg->size, NULL, 0);
937 * Allocate a queue pair if it hasn't been allocated yet
940 vhost_user_check_and_alloc_queue_pair(struct virtio_net *dev, VhostUserMsg *msg)
945 switch (msg->request) {
946 case VHOST_USER_SET_VRING_KICK:
947 case VHOST_USER_SET_VRING_CALL:
948 case VHOST_USER_SET_VRING_ERR:
949 vring_idx = msg->payload.u64 & VHOST_USER_VRING_IDX_MASK;
951 case VHOST_USER_SET_VRING_NUM:
952 case VHOST_USER_SET_VRING_BASE:
953 case VHOST_USER_SET_VRING_ENABLE:
954 vring_idx = msg->payload.state.index;
956 case VHOST_USER_SET_VRING_ADDR:
957 vring_idx = msg->payload.addr.index;
963 qp_idx = vring_idx / VIRTIO_QNUM;
964 if (qp_idx >= VHOST_MAX_QUEUE_PAIRS) {
965 RTE_LOG(ERR, VHOST_CONFIG,
966 "invalid vring index: %u\n", vring_idx);
970 if (dev->virtqueue[qp_idx * VIRTIO_QNUM])
973 return alloc_vring_queue_pair(dev, qp_idx);
977 vhost_user_msg_handler(int vid, int fd)
979 struct virtio_net *dev;
980 struct VhostUserMsg msg;
983 dev = get_device(vid);
987 if (!dev->notify_ops) {
988 dev->notify_ops = vhost_driver_callback_get(dev->ifname);
989 if (!dev->notify_ops) {
990 RTE_LOG(ERR, VHOST_CONFIG,
991 "failed to get callback ops for driver %s\n",
997 ret = read_vhost_message(fd, &msg);
998 if (ret <= 0 || msg.request >= VHOST_USER_MAX) {
1000 RTE_LOG(ERR, VHOST_CONFIG,
1001 "vhost read message failed\n");
1003 RTE_LOG(INFO, VHOST_CONFIG,
1004 "vhost peer closed\n");
1006 RTE_LOG(ERR, VHOST_CONFIG,
1007 "vhost read incorrect message\n");
1013 RTE_LOG(INFO, VHOST_CONFIG, "read message %s\n",
1014 vhost_message_str[msg.request]);
1016 ret = vhost_user_check_and_alloc_queue_pair(dev, &msg);
1018 RTE_LOG(ERR, VHOST_CONFIG,
1019 "failed to alloc queue\n");
1023 switch (msg.request) {
1024 case VHOST_USER_GET_FEATURES:
1025 msg.payload.u64 = vhost_user_get_features(dev);
1026 msg.size = sizeof(msg.payload.u64);
1027 send_vhost_message(fd, &msg);
1029 case VHOST_USER_SET_FEATURES:
1030 vhost_user_set_features(dev, msg.payload.u64);
1033 case VHOST_USER_GET_PROTOCOL_FEATURES:
1034 msg.payload.u64 = VHOST_USER_PROTOCOL_FEATURES;
1035 msg.size = sizeof(msg.payload.u64);
1036 send_vhost_message(fd, &msg);
1038 case VHOST_USER_SET_PROTOCOL_FEATURES:
1039 vhost_user_set_protocol_features(dev, msg.payload.u64);
1042 case VHOST_USER_SET_OWNER:
1043 vhost_user_set_owner();
1045 case VHOST_USER_RESET_OWNER:
1046 vhost_user_reset_owner(dev);
1049 case VHOST_USER_SET_MEM_TABLE:
1050 ret = vhost_user_set_mem_table(dev, &msg);
1053 case VHOST_USER_SET_LOG_BASE:
1054 vhost_user_set_log_base(dev, &msg);
1056 /* it needs a reply */
1057 msg.size = sizeof(msg.payload.u64);
1058 send_vhost_message(fd, &msg);
1060 case VHOST_USER_SET_LOG_FD:
1062 RTE_LOG(INFO, VHOST_CONFIG, "not implemented.\n");
1065 case VHOST_USER_SET_VRING_NUM:
1066 vhost_user_set_vring_num(dev, &msg.payload.state);
1068 case VHOST_USER_SET_VRING_ADDR:
1069 vhost_user_set_vring_addr(dev, &msg.payload.addr);
1071 case VHOST_USER_SET_VRING_BASE:
1072 vhost_user_set_vring_base(dev, &msg.payload.state);
1075 case VHOST_USER_GET_VRING_BASE:
1076 vhost_user_get_vring_base(dev, &msg.payload.state);
1077 msg.size = sizeof(msg.payload.state);
1078 send_vhost_message(fd, &msg);
1081 case VHOST_USER_SET_VRING_KICK:
1082 vhost_user_set_vring_kick(dev, &msg);
1084 case VHOST_USER_SET_VRING_CALL:
1085 vhost_user_set_vring_call(dev, &msg);
1088 case VHOST_USER_SET_VRING_ERR:
1089 if (!(msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK))
1091 RTE_LOG(INFO, VHOST_CONFIG, "not implemented\n");
1094 case VHOST_USER_GET_QUEUE_NUM:
1095 msg.payload.u64 = VHOST_MAX_QUEUE_PAIRS;
1096 msg.size = sizeof(msg.payload.u64);
1097 send_vhost_message(fd, &msg);
1100 case VHOST_USER_SET_VRING_ENABLE:
1101 vhost_user_set_vring_enable(dev, &msg.payload.state);
1103 case VHOST_USER_SEND_RARP:
1104 vhost_user_send_rarp(dev, &msg);
1107 case VHOST_USER_NET_SET_MTU:
1108 ret = vhost_user_net_set_mtu(dev, &msg);
1117 if (msg.flags & VHOST_USER_NEED_REPLY) {
1118 msg.payload.u64 = !!ret;
1119 msg.size = sizeof(msg.payload.u64);
1120 send_vhost_message(fd, &msg);