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 static const char *vhost_message_str[VHOST_USER_MAX] = {
55 [VHOST_USER_NONE] = "VHOST_USER_NONE",
56 [VHOST_USER_GET_FEATURES] = "VHOST_USER_GET_FEATURES",
57 [VHOST_USER_SET_FEATURES] = "VHOST_USER_SET_FEATURES",
58 [VHOST_USER_SET_OWNER] = "VHOST_USER_SET_OWNER",
59 [VHOST_USER_RESET_OWNER] = "VHOST_USER_RESET_OWNER",
60 [VHOST_USER_SET_MEM_TABLE] = "VHOST_USER_SET_MEM_TABLE",
61 [VHOST_USER_SET_LOG_BASE] = "VHOST_USER_SET_LOG_BASE",
62 [VHOST_USER_SET_LOG_FD] = "VHOST_USER_SET_LOG_FD",
63 [VHOST_USER_SET_VRING_NUM] = "VHOST_USER_SET_VRING_NUM",
64 [VHOST_USER_SET_VRING_ADDR] = "VHOST_USER_SET_VRING_ADDR",
65 [VHOST_USER_SET_VRING_BASE] = "VHOST_USER_SET_VRING_BASE",
66 [VHOST_USER_GET_VRING_BASE] = "VHOST_USER_GET_VRING_BASE",
67 [VHOST_USER_SET_VRING_KICK] = "VHOST_USER_SET_VRING_KICK",
68 [VHOST_USER_SET_VRING_CALL] = "VHOST_USER_SET_VRING_CALL",
69 [VHOST_USER_SET_VRING_ERR] = "VHOST_USER_SET_VRING_ERR",
70 [VHOST_USER_GET_PROTOCOL_FEATURES] = "VHOST_USER_GET_PROTOCOL_FEATURES",
71 [VHOST_USER_SET_PROTOCOL_FEATURES] = "VHOST_USER_SET_PROTOCOL_FEATURES",
72 [VHOST_USER_GET_QUEUE_NUM] = "VHOST_USER_GET_QUEUE_NUM",
73 [VHOST_USER_SET_VRING_ENABLE] = "VHOST_USER_SET_VRING_ENABLE",
74 [VHOST_USER_SEND_RARP] = "VHOST_USER_SEND_RARP",
83 ret = fstat(fd, &stat);
84 return ret == -1 ? (uint64_t)-1 : (uint64_t)stat.st_blksize;
88 free_mem_region(struct virtio_net *dev)
91 struct virtio_memory_region *reg;
93 if (!dev || !dev->mem)
96 for (i = 0; i < dev->mem->nregions; i++) {
97 reg = &dev->mem->regions[i];
98 if (reg->host_user_addr) {
99 munmap(reg->mmap_addr, reg->mmap_size);
106 vhost_backend_cleanup(struct virtio_net *dev)
109 free_mem_region(dev);
114 munmap((void *)(uintptr_t)dev->log_addr, dev->log_size);
120 * This function just returns success at the moment unless
121 * the device hasn't been initialised.
124 vhost_user_set_owner(void)
130 vhost_user_reset_owner(struct virtio_net *dev)
132 if (dev->flags & VIRTIO_DEV_RUNNING) {
133 dev->flags &= ~VIRTIO_DEV_RUNNING;
134 notify_ops->destroy_device(dev->vid);
137 cleanup_device(dev, 0);
143 * The features that we support are requested.
146 vhost_user_get_features(void)
148 return VHOST_FEATURES;
152 * We receive the negotiated features supported by us and the virtio device.
155 vhost_user_set_features(struct virtio_net *dev, uint64_t features)
157 if (features & ~VHOST_FEATURES)
160 dev->features = features;
162 ((1 << VIRTIO_NET_F_MRG_RXBUF) | (1ULL << VIRTIO_F_VERSION_1))) {
163 dev->vhost_hlen = sizeof(struct virtio_net_hdr_mrg_rxbuf);
165 dev->vhost_hlen = sizeof(struct virtio_net_hdr);
167 LOG_DEBUG(VHOST_CONFIG,
168 "(%d) mergeable RX buffers %s, virtio 1 %s\n",
170 (dev->features & (1 << VIRTIO_NET_F_MRG_RXBUF)) ? "on" : "off",
171 (dev->features & (1ULL << VIRTIO_F_VERSION_1)) ? "on" : "off");
177 * The virtio device sends us the size of the descriptor ring.
180 vhost_user_set_vring_num(struct virtio_net *dev,
181 struct vhost_vring_state *state)
183 dev->virtqueue[state->index]->size = state->num;
189 * Reallocate virtio_dev and vhost_virtqueue data structure to make them on the
190 * same numa node as the memory of vring descriptor.
192 #ifdef RTE_LIBRTE_VHOST_NUMA
193 static struct virtio_net*
194 numa_realloc(struct virtio_net *dev, int index)
196 int oldnode, newnode;
197 struct virtio_net *old_dev;
198 struct vhost_virtqueue *old_vq, *vq;
202 * vq is allocated on pairs, we should try to do realloc
203 * on first queue of one queue pair only.
205 if (index % VIRTIO_QNUM != 0)
209 vq = old_vq = dev->virtqueue[index];
211 ret = get_mempolicy(&newnode, NULL, 0, old_vq->desc,
212 MPOL_F_NODE | MPOL_F_ADDR);
214 /* check if we need to reallocate vq */
215 ret |= get_mempolicy(&oldnode, NULL, 0, old_vq,
216 MPOL_F_NODE | MPOL_F_ADDR);
218 RTE_LOG(ERR, VHOST_CONFIG,
219 "Unable to get vq numa information.\n");
222 if (oldnode != newnode) {
223 RTE_LOG(INFO, VHOST_CONFIG,
224 "reallocate vq from %d to %d node\n", oldnode, newnode);
225 vq = rte_malloc_socket(NULL, sizeof(*vq) * VIRTIO_QNUM, 0,
230 memcpy(vq, old_vq, sizeof(*vq) * VIRTIO_QNUM);
234 /* check if we need to reallocate dev */
235 ret = get_mempolicy(&oldnode, NULL, 0, old_dev,
236 MPOL_F_NODE | MPOL_F_ADDR);
238 RTE_LOG(ERR, VHOST_CONFIG,
239 "Unable to get dev numa information.\n");
242 if (oldnode != newnode) {
243 RTE_LOG(INFO, VHOST_CONFIG,
244 "reallocate dev from %d to %d node\n",
246 dev = rte_malloc_socket(NULL, sizeof(*dev), 0, newnode);
252 memcpy(dev, old_dev, sizeof(*dev));
257 dev->virtqueue[index] = vq;
258 dev->virtqueue[index + 1] = vq + 1;
259 vhost_devices[dev->vid] = dev;
264 static struct virtio_net*
265 numa_realloc(struct virtio_net *dev, int index __rte_unused)
272 * Converts QEMU virtual address to Vhost virtual address. This function is
273 * used to convert the ring addresses to our address space.
276 qva_to_vva(struct virtio_net *dev, uint64_t qva)
278 struct virtio_memory_region *reg;
281 /* Find the region where the address lives. */
282 for (i = 0; i < dev->mem->nregions; i++) {
283 reg = &dev->mem->regions[i];
285 if (qva >= reg->guest_user_addr &&
286 qva < reg->guest_user_addr + reg->size) {
287 return qva - reg->guest_user_addr +
296 * The virtio device sends us the desc, used and avail ring addresses.
297 * This function then converts these to our address space.
300 vhost_user_set_vring_addr(struct virtio_net *dev, struct vhost_vring_addr *addr)
302 struct vhost_virtqueue *vq;
304 if (dev->mem == NULL)
307 /* addr->index refers to the queue index. The txq 1, rxq is 0. */
308 vq = dev->virtqueue[addr->index];
310 /* The addresses are converted from QEMU virtual to Vhost virtual. */
311 vq->desc = (struct vring_desc *)(uintptr_t)qva_to_vva(dev,
312 addr->desc_user_addr);
314 RTE_LOG(ERR, VHOST_CONFIG,
315 "(%d) failed to find desc ring address.\n",
320 dev = numa_realloc(dev, addr->index);
321 vq = dev->virtqueue[addr->index];
323 vq->avail = (struct vring_avail *)(uintptr_t)qva_to_vva(dev,
324 addr->avail_user_addr);
325 if (vq->avail == 0) {
326 RTE_LOG(ERR, VHOST_CONFIG,
327 "(%d) failed to find avail ring address.\n",
332 vq->used = (struct vring_used *)(uintptr_t)qva_to_vva(dev,
333 addr->used_user_addr);
335 RTE_LOG(ERR, VHOST_CONFIG,
336 "(%d) failed to find used ring address.\n",
341 if (vq->last_used_idx != vq->used->idx) {
342 RTE_LOG(WARNING, VHOST_CONFIG,
343 "last_used_idx (%u) and vq->used->idx (%u) mismatches; "
344 "some packets maybe resent for Tx and dropped for Rx\n",
345 vq->last_used_idx, vq->used->idx);
346 vq->last_used_idx = vq->used->idx;
349 vq->log_guest_addr = addr->log_guest_addr;
351 LOG_DEBUG(VHOST_CONFIG, "(%d) mapped address desc: %p\n",
353 LOG_DEBUG(VHOST_CONFIG, "(%d) mapped address avail: %p\n",
354 dev->vid, vq->avail);
355 LOG_DEBUG(VHOST_CONFIG, "(%d) mapped address used: %p\n",
357 LOG_DEBUG(VHOST_CONFIG, "(%d) log_guest_addr: %" PRIx64 "\n",
358 dev->vid, vq->log_guest_addr);
364 * The virtio device sends us the available ring last used index.
367 vhost_user_set_vring_base(struct virtio_net *dev,
368 struct vhost_vring_state *state)
370 dev->virtqueue[state->index]->last_used_idx = state->num;
376 add_one_guest_page(struct virtio_net *dev, uint64_t guest_phys_addr,
377 uint64_t host_phys_addr, uint64_t size)
379 struct guest_page *page, *last_page;
381 if (dev->nr_guest_pages == dev->max_guest_pages) {
382 dev->max_guest_pages *= 2;
383 dev->guest_pages = realloc(dev->guest_pages,
384 dev->max_guest_pages * sizeof(*page));
387 if (dev->nr_guest_pages > 0) {
388 last_page = &dev->guest_pages[dev->nr_guest_pages - 1];
389 /* merge if the two pages are continuous */
390 if (host_phys_addr == last_page->host_phys_addr +
392 last_page->size += size;
397 page = &dev->guest_pages[dev->nr_guest_pages++];
398 page->guest_phys_addr = guest_phys_addr;
399 page->host_phys_addr = host_phys_addr;
404 add_guest_pages(struct virtio_net *dev, struct virtio_memory_region *reg,
407 uint64_t reg_size = reg->size;
408 uint64_t host_user_addr = reg->host_user_addr;
409 uint64_t guest_phys_addr = reg->guest_phys_addr;
410 uint64_t host_phys_addr;
413 host_phys_addr = rte_mem_virt2phy((void *)(uintptr_t)host_user_addr);
414 size = page_size - (guest_phys_addr & (page_size - 1));
415 size = RTE_MIN(size, reg_size);
417 add_one_guest_page(dev, guest_phys_addr, host_phys_addr, size);
418 host_user_addr += size;
419 guest_phys_addr += size;
422 while (reg_size > 0) {
423 host_phys_addr = rte_mem_virt2phy((void *)(uintptr_t)
425 add_one_guest_page(dev, guest_phys_addr, host_phys_addr,
428 host_user_addr += page_size;
429 guest_phys_addr += page_size;
430 reg_size -= page_size;
434 #ifdef RTE_LIBRTE_VHOST_DEBUG
435 /* TODO: enable it only in debug mode? */
437 dump_guest_pages(struct virtio_net *dev)
440 struct guest_page *page;
442 for (i = 0; i < dev->nr_guest_pages; i++) {
443 page = &dev->guest_pages[i];
445 RTE_LOG(INFO, VHOST_CONFIG,
446 "guest physical page region %u\n"
447 "\t guest_phys_addr: %" PRIx64 "\n"
448 "\t host_phys_addr : %" PRIx64 "\n"
449 "\t size : %" PRIx64 "\n",
451 page->guest_phys_addr,
452 page->host_phys_addr,
457 #define dump_guest_pages(dev)
461 vhost_user_set_mem_table(struct virtio_net *dev, struct VhostUserMsg *pmsg)
463 struct VhostUserMemory memory = pmsg->payload.memory;
464 struct virtio_memory_region *reg;
467 uint64_t mmap_offset;
472 /* Remove from the data plane. */
473 if (dev->flags & VIRTIO_DEV_RUNNING) {
474 dev->flags &= ~VIRTIO_DEV_RUNNING;
475 notify_ops->destroy_device(dev->vid);
479 free_mem_region(dev);
484 dev->nr_guest_pages = 0;
485 if (!dev->guest_pages) {
486 dev->max_guest_pages = 8;
487 dev->guest_pages = malloc(dev->max_guest_pages *
488 sizeof(struct guest_page));
491 dev->mem = rte_zmalloc("vhost-mem-table", sizeof(struct virtio_memory) +
492 sizeof(struct virtio_memory_region) * memory.nregions, 0);
493 if (dev->mem == NULL) {
494 RTE_LOG(ERR, VHOST_CONFIG,
495 "(%d) failed to allocate memory for dev->mem\n",
499 dev->mem->nregions = memory.nregions;
501 for (i = 0; i < memory.nregions; i++) {
503 reg = &dev->mem->regions[i];
505 reg->guest_phys_addr = memory.regions[i].guest_phys_addr;
506 reg->guest_user_addr = memory.regions[i].userspace_addr;
507 reg->size = memory.regions[i].memory_size;
510 mmap_offset = memory.regions[i].mmap_offset;
511 mmap_size = reg->size + mmap_offset;
513 /* mmap() without flag of MAP_ANONYMOUS, should be called
514 * with length argument aligned with hugepagesz at older
515 * longterm version Linux, like 2.6.32 and 3.2.72, or
516 * mmap() will fail with EINVAL.
518 * to avoid failure, make sure in caller to keep length
521 alignment = get_blk_size(fd);
522 if (alignment == (uint64_t)-1) {
523 RTE_LOG(ERR, VHOST_CONFIG,
524 "couldn't get hugepage size through fstat\n");
527 mmap_size = RTE_ALIGN_CEIL(mmap_size, alignment);
529 mmap_addr = mmap(NULL, mmap_size, PROT_READ | PROT_WRITE,
530 MAP_SHARED | MAP_POPULATE, fd, 0);
532 if (mmap_addr == MAP_FAILED) {
533 RTE_LOG(ERR, VHOST_CONFIG,
534 "mmap region %u failed.\n", i);
538 reg->mmap_addr = mmap_addr;
539 reg->mmap_size = mmap_size;
540 reg->host_user_addr = (uint64_t)(uintptr_t)mmap_addr +
543 add_guest_pages(dev, reg, alignment);
545 RTE_LOG(INFO, VHOST_CONFIG,
546 "guest memory region %u, size: 0x%" PRIx64 "\n"
547 "\t guest physical addr: 0x%" PRIx64 "\n"
548 "\t guest virtual addr: 0x%" PRIx64 "\n"
549 "\t host virtual addr: 0x%" PRIx64 "\n"
550 "\t mmap addr : 0x%" PRIx64 "\n"
551 "\t mmap size : 0x%" PRIx64 "\n"
552 "\t mmap align: 0x%" PRIx64 "\n"
553 "\t mmap off : 0x%" PRIx64 "\n",
555 reg->guest_phys_addr,
556 reg->guest_user_addr,
558 (uint64_t)(uintptr_t)mmap_addr,
564 dump_guest_pages(dev);
569 free_mem_region(dev);
576 vq_is_ready(struct vhost_virtqueue *vq)
578 return vq && vq->desc &&
579 vq->kickfd != VIRTIO_UNINITIALIZED_EVENTFD &&
580 vq->callfd != VIRTIO_UNINITIALIZED_EVENTFD;
584 virtio_is_ready(struct virtio_net *dev)
586 struct vhost_virtqueue *rvq, *tvq;
589 for (i = 0; i < dev->virt_qp_nb; i++) {
590 rvq = dev->virtqueue[i * VIRTIO_QNUM + VIRTIO_RXQ];
591 tvq = dev->virtqueue[i * VIRTIO_QNUM + VIRTIO_TXQ];
593 if (!vq_is_ready(rvq) || !vq_is_ready(tvq)) {
594 RTE_LOG(INFO, VHOST_CONFIG,
595 "virtio is not ready for processing.\n");
600 RTE_LOG(INFO, VHOST_CONFIG,
601 "virtio is now ready for processing.\n");
606 vhost_user_set_vring_call(struct virtio_net *dev, struct VhostUserMsg *pmsg)
608 struct vhost_vring_file file;
609 struct vhost_virtqueue *vq;
612 file.index = pmsg->payload.u64 & VHOST_USER_VRING_IDX_MASK;
613 if (pmsg->payload.u64 & VHOST_USER_VRING_NOFD_MASK)
614 file.fd = VIRTIO_INVALID_EVENTFD;
616 file.fd = pmsg->fds[0];
617 RTE_LOG(INFO, VHOST_CONFIG,
618 "vring call idx:%d file:%d\n", file.index, file.fd);
621 * FIXME: VHOST_SET_VRING_CALL is the first per-vring message
622 * we get, so we do vring queue pair allocation here.
624 cur_qp_idx = file.index / VIRTIO_QNUM;
625 if (cur_qp_idx + 1 > dev->virt_qp_nb) {
626 if (alloc_vring_queue_pair(dev, cur_qp_idx) < 0)
630 vq = dev->virtqueue[file.index];
636 vq->callfd = file.fd;
640 * In vhost-user, when we receive kick message, will test whether virtio
641 * device is ready for packet processing.
644 vhost_user_set_vring_kick(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 kick idx:%d file:%d\n", file.index, file.fd);
657 vq = dev->virtqueue[file.index];
660 vq->kickfd = file.fd;
662 if (virtio_is_ready(dev) && !(dev->flags & VIRTIO_DEV_RUNNING)) {
663 if (notify_ops->new_device(dev->vid) == 0)
664 dev->flags |= VIRTIO_DEV_RUNNING;
669 * when virtio is stopped, qemu will send us the GET_VRING_BASE message.
672 vhost_user_get_vring_base(struct virtio_net *dev,
673 struct vhost_vring_state *state)
675 /* We have to stop the queue (virtio) if it is running. */
676 if (dev->flags & VIRTIO_DEV_RUNNING) {
677 dev->flags &= ~VIRTIO_DEV_RUNNING;
678 notify_ops->destroy_device(dev->vid);
681 /* Here we are safe to get the last used index */
682 state->num = dev->virtqueue[state->index]->last_used_idx;
684 RTE_LOG(INFO, VHOST_CONFIG,
685 "vring base idx:%d file:%d\n", state->index, state->num);
687 * Based on current qemu vhost-user implementation, this message is
688 * sent and only sent in vhost_vring_stop.
689 * TODO: cleanup the vring, it isn't usable since here.
691 if (dev->virtqueue[state->index]->kickfd >= 0)
692 close(dev->virtqueue[state->index]->kickfd);
694 dev->virtqueue[state->index]->kickfd = VIRTIO_UNINITIALIZED_EVENTFD;
700 * when virtio queues are ready to work, qemu will send us to
701 * enable the virtio queue pair.
704 vhost_user_set_vring_enable(struct virtio_net *dev,
705 struct vhost_vring_state *state)
707 int enable = (int)state->num;
709 RTE_LOG(INFO, VHOST_CONFIG,
710 "set queue enable: %d to qp idx: %d\n",
711 enable, state->index);
713 if (notify_ops->vring_state_changed)
714 notify_ops->vring_state_changed(dev->vid, state->index, enable);
716 dev->virtqueue[state->index]->enabled = enable;
722 vhost_user_set_protocol_features(struct virtio_net *dev,
723 uint64_t protocol_features)
725 if (protocol_features & ~VHOST_USER_PROTOCOL_FEATURES)
728 dev->protocol_features = protocol_features;
732 vhost_user_set_log_base(struct virtio_net *dev, struct VhostUserMsg *msg)
734 int fd = msg->fds[0];
739 RTE_LOG(ERR, VHOST_CONFIG, "invalid log fd: %d\n", fd);
743 if (msg->size != sizeof(VhostUserLog)) {
744 RTE_LOG(ERR, VHOST_CONFIG,
745 "invalid log base msg size: %"PRId32" != %d\n",
746 msg->size, (int)sizeof(VhostUserLog));
750 size = msg->payload.log.mmap_size;
751 off = msg->payload.log.mmap_offset;
752 RTE_LOG(INFO, VHOST_CONFIG,
753 "log mmap size: %"PRId64", offset: %"PRId64"\n",
757 * mmap from 0 to workaround a hugepage mmap bug: mmap will
758 * fail when offset is not page size aligned.
760 addr = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
762 if (addr == MAP_FAILED) {
763 RTE_LOG(ERR, VHOST_CONFIG, "mmap log base failed!\n");
768 * Free previously mapped log memory on occasionally
769 * multiple VHOST_USER_SET_LOG_BASE.
772 munmap((void *)(uintptr_t)dev->log_addr, dev->log_size);
774 dev->log_addr = (uint64_t)(uintptr_t)addr;
775 dev->log_base = dev->log_addr + off;
776 dev->log_size = size;
782 * An rarp packet is constructed and broadcasted to notify switches about
783 * the new location of the migrated VM, so that packets from outside will
784 * not be lost after migration.
786 * However, we don't actually "send" a rarp packet here, instead, we set
787 * a flag 'broadcast_rarp' to let rte_vhost_dequeue_burst() inject it.
790 vhost_user_send_rarp(struct virtio_net *dev, struct VhostUserMsg *msg)
792 uint8_t *mac = (uint8_t *)&msg->payload.u64;
794 RTE_LOG(DEBUG, VHOST_CONFIG,
795 ":: mac: %02x:%02x:%02x:%02x:%02x:%02x\n",
796 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
797 memcpy(dev->mac.addr_bytes, mac, 6);
800 * Set the flag to inject a RARP broadcast packet at
801 * rte_vhost_dequeue_burst().
803 * rte_smp_wmb() is for making sure the mac is copied
804 * before the flag is set.
807 rte_atomic16_set(&dev->broadcast_rarp, 1);
812 /* return bytes# of read on success or negative val on failure. */
814 read_vhost_message(int sockfd, struct VhostUserMsg *msg)
818 ret = read_fd_message(sockfd, (char *)msg, VHOST_USER_HDR_SIZE,
819 msg->fds, VHOST_MEMORY_MAX_NREGIONS);
823 if (msg && msg->size) {
824 if (msg->size > sizeof(msg->payload)) {
825 RTE_LOG(ERR, VHOST_CONFIG,
826 "invalid msg size: %d\n", msg->size);
829 ret = read(sockfd, &msg->payload, msg->size);
832 if (ret != (int)msg->size) {
833 RTE_LOG(ERR, VHOST_CONFIG,
834 "read control message failed\n");
843 send_vhost_message(int sockfd, struct VhostUserMsg *msg)
850 msg->flags &= ~VHOST_USER_VERSION_MASK;
851 msg->flags |= VHOST_USER_VERSION;
852 msg->flags |= VHOST_USER_REPLY_MASK;
854 ret = send_fd_message(sockfd, (char *)msg,
855 VHOST_USER_HDR_SIZE + msg->size, NULL, 0);
861 vhost_user_msg_handler(int vid, int fd)
863 struct virtio_net *dev;
864 struct VhostUserMsg msg;
867 dev = get_device(vid);
871 ret = read_vhost_message(fd, &msg);
872 if (ret <= 0 || msg.request >= VHOST_USER_MAX) {
874 RTE_LOG(ERR, VHOST_CONFIG,
875 "vhost read message failed\n");
877 RTE_LOG(INFO, VHOST_CONFIG,
878 "vhost peer closed\n");
880 RTE_LOG(ERR, VHOST_CONFIG,
881 "vhost read incorrect message\n");
886 RTE_LOG(INFO, VHOST_CONFIG, "read message %s\n",
887 vhost_message_str[msg.request]);
888 switch (msg.request) {
889 case VHOST_USER_GET_FEATURES:
890 msg.payload.u64 = vhost_user_get_features();
891 msg.size = sizeof(msg.payload.u64);
892 send_vhost_message(fd, &msg);
894 case VHOST_USER_SET_FEATURES:
895 vhost_user_set_features(dev, msg.payload.u64);
898 case VHOST_USER_GET_PROTOCOL_FEATURES:
899 msg.payload.u64 = VHOST_USER_PROTOCOL_FEATURES;
900 msg.size = sizeof(msg.payload.u64);
901 send_vhost_message(fd, &msg);
903 case VHOST_USER_SET_PROTOCOL_FEATURES:
904 vhost_user_set_protocol_features(dev, msg.payload.u64);
907 case VHOST_USER_SET_OWNER:
908 vhost_user_set_owner();
910 case VHOST_USER_RESET_OWNER:
911 vhost_user_reset_owner(dev);
914 case VHOST_USER_SET_MEM_TABLE:
915 vhost_user_set_mem_table(dev, &msg);
918 case VHOST_USER_SET_LOG_BASE:
919 vhost_user_set_log_base(dev, &msg);
921 /* it needs a reply */
922 msg.size = sizeof(msg.payload.u64);
923 send_vhost_message(fd, &msg);
925 case VHOST_USER_SET_LOG_FD:
927 RTE_LOG(INFO, VHOST_CONFIG, "not implemented.\n");
930 case VHOST_USER_SET_VRING_NUM:
931 vhost_user_set_vring_num(dev, &msg.payload.state);
933 case VHOST_USER_SET_VRING_ADDR:
934 vhost_user_set_vring_addr(dev, &msg.payload.addr);
936 case VHOST_USER_SET_VRING_BASE:
937 vhost_user_set_vring_base(dev, &msg.payload.state);
940 case VHOST_USER_GET_VRING_BASE:
941 ret = vhost_user_get_vring_base(dev, &msg.payload.state);
942 msg.size = sizeof(msg.payload.state);
943 send_vhost_message(fd, &msg);
946 case VHOST_USER_SET_VRING_KICK:
947 vhost_user_set_vring_kick(dev, &msg);
949 case VHOST_USER_SET_VRING_CALL:
950 vhost_user_set_vring_call(dev, &msg);
953 case VHOST_USER_SET_VRING_ERR:
954 if (!(msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK))
956 RTE_LOG(INFO, VHOST_CONFIG, "not implemented\n");
959 case VHOST_USER_GET_QUEUE_NUM:
960 msg.payload.u64 = VHOST_MAX_QUEUE_PAIRS;
961 msg.size = sizeof(msg.payload.u64);
962 send_vhost_message(fd, &msg);
965 case VHOST_USER_SET_VRING_ENABLE:
966 vhost_user_set_vring_enable(dev, &msg.payload.state);
968 case VHOST_USER_SEND_RARP:
969 vhost_user_send_rarp(dev, &msg);