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;
347 vq->last_avail_idx = vq->used->idx;
350 vq->log_guest_addr = addr->log_guest_addr;
352 LOG_DEBUG(VHOST_CONFIG, "(%d) mapped address desc: %p\n",
354 LOG_DEBUG(VHOST_CONFIG, "(%d) mapped address avail: %p\n",
355 dev->vid, vq->avail);
356 LOG_DEBUG(VHOST_CONFIG, "(%d) mapped address used: %p\n",
358 LOG_DEBUG(VHOST_CONFIG, "(%d) log_guest_addr: %" PRIx64 "\n",
359 dev->vid, vq->log_guest_addr);
365 * The virtio device sends us the available ring last used index.
368 vhost_user_set_vring_base(struct virtio_net *dev,
369 struct vhost_vring_state *state)
371 dev->virtqueue[state->index]->last_used_idx = state->num;
372 dev->virtqueue[state->index]->last_avail_idx = state->num;
378 add_one_guest_page(struct virtio_net *dev, uint64_t guest_phys_addr,
379 uint64_t host_phys_addr, uint64_t size)
381 struct guest_page *page, *last_page;
383 if (dev->nr_guest_pages == dev->max_guest_pages) {
384 dev->max_guest_pages *= 2;
385 dev->guest_pages = realloc(dev->guest_pages,
386 dev->max_guest_pages * sizeof(*page));
389 if (dev->nr_guest_pages > 0) {
390 last_page = &dev->guest_pages[dev->nr_guest_pages - 1];
391 /* merge if the two pages are continuous */
392 if (host_phys_addr == last_page->host_phys_addr +
394 last_page->size += size;
399 page = &dev->guest_pages[dev->nr_guest_pages++];
400 page->guest_phys_addr = guest_phys_addr;
401 page->host_phys_addr = host_phys_addr;
406 add_guest_pages(struct virtio_net *dev, struct virtio_memory_region *reg,
409 uint64_t reg_size = reg->size;
410 uint64_t host_user_addr = reg->host_user_addr;
411 uint64_t guest_phys_addr = reg->guest_phys_addr;
412 uint64_t host_phys_addr;
415 host_phys_addr = rte_mem_virt2phy((void *)(uintptr_t)host_user_addr);
416 size = page_size - (guest_phys_addr & (page_size - 1));
417 size = RTE_MIN(size, reg_size);
419 add_one_guest_page(dev, guest_phys_addr, host_phys_addr, size);
420 host_user_addr += size;
421 guest_phys_addr += size;
424 while (reg_size > 0) {
425 host_phys_addr = rte_mem_virt2phy((void *)(uintptr_t)
427 add_one_guest_page(dev, guest_phys_addr, host_phys_addr,
430 host_user_addr += page_size;
431 guest_phys_addr += page_size;
432 reg_size -= page_size;
436 #ifdef RTE_LIBRTE_VHOST_DEBUG
437 /* TODO: enable it only in debug mode? */
439 dump_guest_pages(struct virtio_net *dev)
442 struct guest_page *page;
444 for (i = 0; i < dev->nr_guest_pages; i++) {
445 page = &dev->guest_pages[i];
447 RTE_LOG(INFO, VHOST_CONFIG,
448 "guest physical page region %u\n"
449 "\t guest_phys_addr: %" PRIx64 "\n"
450 "\t host_phys_addr : %" PRIx64 "\n"
451 "\t size : %" PRIx64 "\n",
453 page->guest_phys_addr,
454 page->host_phys_addr,
459 #define dump_guest_pages(dev)
463 vhost_user_set_mem_table(struct virtio_net *dev, struct VhostUserMsg *pmsg)
465 struct VhostUserMemory memory = pmsg->payload.memory;
466 struct virtio_memory_region *reg;
469 uint64_t mmap_offset;
474 /* Remove from the data plane. */
475 if (dev->flags & VIRTIO_DEV_RUNNING) {
476 dev->flags &= ~VIRTIO_DEV_RUNNING;
477 notify_ops->destroy_device(dev->vid);
481 free_mem_region(dev);
486 dev->nr_guest_pages = 0;
487 if (!dev->guest_pages) {
488 dev->max_guest_pages = 8;
489 dev->guest_pages = malloc(dev->max_guest_pages *
490 sizeof(struct guest_page));
493 dev->mem = rte_zmalloc("vhost-mem-table", sizeof(struct virtio_memory) +
494 sizeof(struct virtio_memory_region) * memory.nregions, 0);
495 if (dev->mem == NULL) {
496 RTE_LOG(ERR, VHOST_CONFIG,
497 "(%d) failed to allocate memory for dev->mem\n",
501 dev->mem->nregions = memory.nregions;
503 for (i = 0; i < memory.nregions; i++) {
505 reg = &dev->mem->regions[i];
507 reg->guest_phys_addr = memory.regions[i].guest_phys_addr;
508 reg->guest_user_addr = memory.regions[i].userspace_addr;
509 reg->size = memory.regions[i].memory_size;
512 mmap_offset = memory.regions[i].mmap_offset;
513 mmap_size = reg->size + mmap_offset;
515 /* mmap() without flag of MAP_ANONYMOUS, should be called
516 * with length argument aligned with hugepagesz at older
517 * longterm version Linux, like 2.6.32 and 3.2.72, or
518 * mmap() will fail with EINVAL.
520 * to avoid failure, make sure in caller to keep length
523 alignment = get_blk_size(fd);
524 if (alignment == (uint64_t)-1) {
525 RTE_LOG(ERR, VHOST_CONFIG,
526 "couldn't get hugepage size through fstat\n");
529 mmap_size = RTE_ALIGN_CEIL(mmap_size, alignment);
531 mmap_addr = mmap(NULL, mmap_size, PROT_READ | PROT_WRITE,
532 MAP_SHARED | MAP_POPULATE, fd, 0);
534 if (mmap_addr == MAP_FAILED) {
535 RTE_LOG(ERR, VHOST_CONFIG,
536 "mmap region %u failed.\n", i);
540 reg->mmap_addr = mmap_addr;
541 reg->mmap_size = mmap_size;
542 reg->host_user_addr = (uint64_t)(uintptr_t)mmap_addr +
545 add_guest_pages(dev, reg, alignment);
547 RTE_LOG(INFO, VHOST_CONFIG,
548 "guest memory region %u, size: 0x%" PRIx64 "\n"
549 "\t guest physical addr: 0x%" PRIx64 "\n"
550 "\t guest virtual addr: 0x%" PRIx64 "\n"
551 "\t host virtual addr: 0x%" PRIx64 "\n"
552 "\t mmap addr : 0x%" PRIx64 "\n"
553 "\t mmap size : 0x%" PRIx64 "\n"
554 "\t mmap align: 0x%" PRIx64 "\n"
555 "\t mmap off : 0x%" PRIx64 "\n",
557 reg->guest_phys_addr,
558 reg->guest_user_addr,
560 (uint64_t)(uintptr_t)mmap_addr,
566 dump_guest_pages(dev);
571 free_mem_region(dev);
578 vq_is_ready(struct vhost_virtqueue *vq)
580 return vq && vq->desc &&
581 vq->kickfd != VIRTIO_UNINITIALIZED_EVENTFD &&
582 vq->callfd != VIRTIO_UNINITIALIZED_EVENTFD;
586 virtio_is_ready(struct virtio_net *dev)
588 struct vhost_virtqueue *rvq, *tvq;
591 for (i = 0; i < dev->virt_qp_nb; i++) {
592 rvq = dev->virtqueue[i * VIRTIO_QNUM + VIRTIO_RXQ];
593 tvq = dev->virtqueue[i * VIRTIO_QNUM + VIRTIO_TXQ];
595 if (!vq_is_ready(rvq) || !vq_is_ready(tvq)) {
596 RTE_LOG(INFO, VHOST_CONFIG,
597 "virtio is not ready for processing.\n");
602 RTE_LOG(INFO, VHOST_CONFIG,
603 "virtio is now ready for processing.\n");
608 vhost_user_set_vring_call(struct virtio_net *dev, struct VhostUserMsg *pmsg)
610 struct vhost_vring_file file;
611 struct vhost_virtqueue *vq;
614 file.index = pmsg->payload.u64 & VHOST_USER_VRING_IDX_MASK;
615 if (pmsg->payload.u64 & VHOST_USER_VRING_NOFD_MASK)
616 file.fd = VIRTIO_INVALID_EVENTFD;
618 file.fd = pmsg->fds[0];
619 RTE_LOG(INFO, VHOST_CONFIG,
620 "vring call idx:%d file:%d\n", file.index, file.fd);
623 * FIXME: VHOST_SET_VRING_CALL is the first per-vring message
624 * we get, so we do vring queue pair allocation here.
626 cur_qp_idx = file.index / VIRTIO_QNUM;
627 if (cur_qp_idx + 1 > dev->virt_qp_nb) {
628 if (alloc_vring_queue_pair(dev, cur_qp_idx) < 0)
632 vq = dev->virtqueue[file.index];
638 vq->callfd = file.fd;
642 * In vhost-user, when we receive kick message, will test whether virtio
643 * device is ready for packet processing.
646 vhost_user_set_vring_kick(struct virtio_net *dev, struct VhostUserMsg *pmsg)
648 struct vhost_vring_file file;
649 struct vhost_virtqueue *vq;
651 file.index = pmsg->payload.u64 & VHOST_USER_VRING_IDX_MASK;
652 if (pmsg->payload.u64 & VHOST_USER_VRING_NOFD_MASK)
653 file.fd = VIRTIO_INVALID_EVENTFD;
655 file.fd = pmsg->fds[0];
656 RTE_LOG(INFO, VHOST_CONFIG,
657 "vring kick idx:%d file:%d\n", file.index, file.fd);
659 vq = dev->virtqueue[file.index];
662 vq->kickfd = file.fd;
664 if (virtio_is_ready(dev) && !(dev->flags & VIRTIO_DEV_RUNNING)) {
665 if (notify_ops->new_device(dev->vid) == 0)
666 dev->flags |= VIRTIO_DEV_RUNNING;
671 * when virtio is stopped, qemu will send us the GET_VRING_BASE message.
674 vhost_user_get_vring_base(struct virtio_net *dev,
675 struct vhost_vring_state *state)
677 /* We have to stop the queue (virtio) if it is running. */
678 if (dev->flags & VIRTIO_DEV_RUNNING) {
679 dev->flags &= ~VIRTIO_DEV_RUNNING;
680 notify_ops->destroy_device(dev->vid);
683 /* Here we are safe to get the last used index */
684 state->num = dev->virtqueue[state->index]->last_used_idx;
686 RTE_LOG(INFO, VHOST_CONFIG,
687 "vring base idx:%d file:%d\n", state->index, state->num);
689 * Based on current qemu vhost-user implementation, this message is
690 * sent and only sent in vhost_vring_stop.
691 * TODO: cleanup the vring, it isn't usable since here.
693 if (dev->virtqueue[state->index]->kickfd >= 0)
694 close(dev->virtqueue[state->index]->kickfd);
696 dev->virtqueue[state->index]->kickfd = VIRTIO_UNINITIALIZED_EVENTFD;
702 * when virtio queues are ready to work, qemu will send us to
703 * enable the virtio queue pair.
706 vhost_user_set_vring_enable(struct virtio_net *dev,
707 struct vhost_vring_state *state)
709 int enable = (int)state->num;
711 RTE_LOG(INFO, VHOST_CONFIG,
712 "set queue enable: %d to qp idx: %d\n",
713 enable, state->index);
715 if (notify_ops->vring_state_changed)
716 notify_ops->vring_state_changed(dev->vid, state->index, enable);
718 dev->virtqueue[state->index]->enabled = enable;
724 vhost_user_set_protocol_features(struct virtio_net *dev,
725 uint64_t protocol_features)
727 if (protocol_features & ~VHOST_USER_PROTOCOL_FEATURES)
730 dev->protocol_features = protocol_features;
734 vhost_user_set_log_base(struct virtio_net *dev, struct VhostUserMsg *msg)
736 int fd = msg->fds[0];
741 RTE_LOG(ERR, VHOST_CONFIG, "invalid log fd: %d\n", fd);
745 if (msg->size != sizeof(VhostUserLog)) {
746 RTE_LOG(ERR, VHOST_CONFIG,
747 "invalid log base msg size: %"PRId32" != %d\n",
748 msg->size, (int)sizeof(VhostUserLog));
752 size = msg->payload.log.mmap_size;
753 off = msg->payload.log.mmap_offset;
754 RTE_LOG(INFO, VHOST_CONFIG,
755 "log mmap size: %"PRId64", offset: %"PRId64"\n",
759 * mmap from 0 to workaround a hugepage mmap bug: mmap will
760 * fail when offset is not page size aligned.
762 addr = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
764 if (addr == MAP_FAILED) {
765 RTE_LOG(ERR, VHOST_CONFIG, "mmap log base failed!\n");
770 * Free previously mapped log memory on occasionally
771 * multiple VHOST_USER_SET_LOG_BASE.
774 munmap((void *)(uintptr_t)dev->log_addr, dev->log_size);
776 dev->log_addr = (uint64_t)(uintptr_t)addr;
777 dev->log_base = dev->log_addr + off;
778 dev->log_size = size;
784 * An rarp packet is constructed and broadcasted to notify switches about
785 * the new location of the migrated VM, so that packets from outside will
786 * not be lost after migration.
788 * However, we don't actually "send" a rarp packet here, instead, we set
789 * a flag 'broadcast_rarp' to let rte_vhost_dequeue_burst() inject it.
792 vhost_user_send_rarp(struct virtio_net *dev, struct VhostUserMsg *msg)
794 uint8_t *mac = (uint8_t *)&msg->payload.u64;
796 RTE_LOG(DEBUG, VHOST_CONFIG,
797 ":: mac: %02x:%02x:%02x:%02x:%02x:%02x\n",
798 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
799 memcpy(dev->mac.addr_bytes, mac, 6);
802 * Set the flag to inject a RARP broadcast packet at
803 * rte_vhost_dequeue_burst().
805 * rte_smp_wmb() is for making sure the mac is copied
806 * before the flag is set.
809 rte_atomic16_set(&dev->broadcast_rarp, 1);
814 /* return bytes# of read on success or negative val on failure. */
816 read_vhost_message(int sockfd, struct VhostUserMsg *msg)
820 ret = read_fd_message(sockfd, (char *)msg, VHOST_USER_HDR_SIZE,
821 msg->fds, VHOST_MEMORY_MAX_NREGIONS);
825 if (msg && msg->size) {
826 if (msg->size > sizeof(msg->payload)) {
827 RTE_LOG(ERR, VHOST_CONFIG,
828 "invalid msg size: %d\n", msg->size);
831 ret = read(sockfd, &msg->payload, msg->size);
834 if (ret != (int)msg->size) {
835 RTE_LOG(ERR, VHOST_CONFIG,
836 "read control message failed\n");
845 send_vhost_message(int sockfd, struct VhostUserMsg *msg)
852 msg->flags &= ~VHOST_USER_VERSION_MASK;
853 msg->flags |= VHOST_USER_VERSION;
854 msg->flags |= VHOST_USER_REPLY_MASK;
856 ret = send_fd_message(sockfd, (char *)msg,
857 VHOST_USER_HDR_SIZE + msg->size, NULL, 0);
863 vhost_user_msg_handler(int vid, int fd)
865 struct virtio_net *dev;
866 struct VhostUserMsg msg;
869 dev = get_device(vid);
873 ret = read_vhost_message(fd, &msg);
874 if (ret <= 0 || msg.request >= VHOST_USER_MAX) {
876 RTE_LOG(ERR, VHOST_CONFIG,
877 "vhost read message failed\n");
879 RTE_LOG(INFO, VHOST_CONFIG,
880 "vhost peer closed\n");
882 RTE_LOG(ERR, VHOST_CONFIG,
883 "vhost read incorrect message\n");
888 RTE_LOG(INFO, VHOST_CONFIG, "read message %s\n",
889 vhost_message_str[msg.request]);
890 switch (msg.request) {
891 case VHOST_USER_GET_FEATURES:
892 msg.payload.u64 = vhost_user_get_features();
893 msg.size = sizeof(msg.payload.u64);
894 send_vhost_message(fd, &msg);
896 case VHOST_USER_SET_FEATURES:
897 vhost_user_set_features(dev, msg.payload.u64);
900 case VHOST_USER_GET_PROTOCOL_FEATURES:
901 msg.payload.u64 = VHOST_USER_PROTOCOL_FEATURES;
902 msg.size = sizeof(msg.payload.u64);
903 send_vhost_message(fd, &msg);
905 case VHOST_USER_SET_PROTOCOL_FEATURES:
906 vhost_user_set_protocol_features(dev, msg.payload.u64);
909 case VHOST_USER_SET_OWNER:
910 vhost_user_set_owner();
912 case VHOST_USER_RESET_OWNER:
913 vhost_user_reset_owner(dev);
916 case VHOST_USER_SET_MEM_TABLE:
917 vhost_user_set_mem_table(dev, &msg);
920 case VHOST_USER_SET_LOG_BASE:
921 vhost_user_set_log_base(dev, &msg);
923 /* it needs a reply */
924 msg.size = sizeof(msg.payload.u64);
925 send_vhost_message(fd, &msg);
927 case VHOST_USER_SET_LOG_FD:
929 RTE_LOG(INFO, VHOST_CONFIG, "not implemented.\n");
932 case VHOST_USER_SET_VRING_NUM:
933 vhost_user_set_vring_num(dev, &msg.payload.state);
935 case VHOST_USER_SET_VRING_ADDR:
936 vhost_user_set_vring_addr(dev, &msg.payload.addr);
938 case VHOST_USER_SET_VRING_BASE:
939 vhost_user_set_vring_base(dev, &msg.payload.state);
942 case VHOST_USER_GET_VRING_BASE:
943 ret = vhost_user_get_vring_base(dev, &msg.payload.state);
944 msg.size = sizeof(msg.payload.state);
945 send_vhost_message(fd, &msg);
948 case VHOST_USER_SET_VRING_KICK:
949 vhost_user_set_vring_kick(dev, &msg);
951 case VHOST_USER_SET_VRING_CALL:
952 vhost_user_set_vring_call(dev, &msg);
955 case VHOST_USER_SET_VRING_ERR:
956 if (!(msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK))
958 RTE_LOG(INFO, VHOST_CONFIG, "not implemented\n");
961 case VHOST_USER_GET_QUEUE_NUM:
962 msg.payload.u64 = VHOST_MAX_QUEUE_PAIRS;
963 msg.size = sizeof(msg.payload.u64);
964 send_vhost_message(fd, &msg);
967 case VHOST_USER_SET_VRING_ENABLE:
968 vhost_user_set_vring_enable(dev, &msg.payload.state);
970 case VHOST_USER_SEND_RARP:
971 vhost_user_send_rarp(dev, &msg);