4 * Copyright IBM, Corp. 2007
5 * Copyright (c) 2016 Red Hat, Inc.
8 * Anthony Liguori <aliguori@us.ibm.com>
9 * Marc-André Lureau <mlureau@redhat.com>
10 * Victor Kaplansky <victork@redhat.com>
12 * This work is licensed under the terms of the GNU GPL, version 2 or
13 * later. See the COPYING file in the top-level directory.
16 /* this code avoids GLib dependency */
25 #include <sys/types.h>
26 #include <sys/socket.h>
27 #include <sys/eventfd.h>
29 #include "qemu/compiler.h"
31 #if defined(__linux__)
32 #include <sys/syscall.h>
34 #include <sys/ioctl.h>
35 #include <linux/vhost.h>
37 #ifdef __NR_userfaultfd
38 #include <linux/userfaultfd.h>
43 #include "qemu/atomic.h"
44 #include "qemu/osdep.h"
45 #include "qemu/memfd.h"
47 #include "libvhost-user.h"
49 /* usually provided by GLib */
51 #define MIN(x, y) ({ \
52 typeof(x) _min1 = (x); \
53 typeof(y) _min2 = (y); \
54 (void) (&_min1 == &_min2); \
55 _min1 < _min2 ? _min1 : _min2; })
58 /* Round number down to multiple */
59 #define ALIGN_DOWN(n, m) ((n) / (m) * (m))
61 /* Round number up to multiple */
62 #define ALIGN_UP(n, m) ALIGN_DOWN((n) + (m) - 1, (m))
64 /* Align each region to cache line size in inflight buffer */
65 #define INFLIGHT_ALIGNMENT 64
67 /* The version of inflight buffer */
68 #define INFLIGHT_VERSION 1
70 #define VHOST_USER_HDR_SIZE offsetof(VhostUserMsg, payload.u64)
72 /* The version of the protocol we support */
73 #define VHOST_USER_VERSION 1
74 #define LIBVHOST_USER_DEBUG 0
78 if (LIBVHOST_USER_DEBUG) { \
79 fprintf(stderr, __VA_ARGS__); \
84 bool has_feature(uint64_t features
, unsigned int fbit
)
87 return !!(features
& (1ULL << fbit
));
91 bool vu_has_feature(VuDev
*dev
,
94 return has_feature(dev
->features
, fbit
);
98 vu_request_to_string(unsigned int req
)
100 #define REQ(req) [req] = #req
101 static const char *vu_request_str
[] = {
102 REQ(VHOST_USER_NONE
),
103 REQ(VHOST_USER_GET_FEATURES
),
104 REQ(VHOST_USER_SET_FEATURES
),
105 REQ(VHOST_USER_SET_OWNER
),
106 REQ(VHOST_USER_RESET_OWNER
),
107 REQ(VHOST_USER_SET_MEM_TABLE
),
108 REQ(VHOST_USER_SET_LOG_BASE
),
109 REQ(VHOST_USER_SET_LOG_FD
),
110 REQ(VHOST_USER_SET_VRING_NUM
),
111 REQ(VHOST_USER_SET_VRING_ADDR
),
112 REQ(VHOST_USER_SET_VRING_BASE
),
113 REQ(VHOST_USER_GET_VRING_BASE
),
114 REQ(VHOST_USER_SET_VRING_KICK
),
115 REQ(VHOST_USER_SET_VRING_CALL
),
116 REQ(VHOST_USER_SET_VRING_ERR
),
117 REQ(VHOST_USER_GET_PROTOCOL_FEATURES
),
118 REQ(VHOST_USER_SET_PROTOCOL_FEATURES
),
119 REQ(VHOST_USER_GET_QUEUE_NUM
),
120 REQ(VHOST_USER_SET_VRING_ENABLE
),
121 REQ(VHOST_USER_SEND_RARP
),
122 REQ(VHOST_USER_NET_SET_MTU
),
123 REQ(VHOST_USER_SET_SLAVE_REQ_FD
),
124 REQ(VHOST_USER_IOTLB_MSG
),
125 REQ(VHOST_USER_SET_VRING_ENDIAN
),
126 REQ(VHOST_USER_GET_CONFIG
),
127 REQ(VHOST_USER_SET_CONFIG
),
128 REQ(VHOST_USER_POSTCOPY_ADVISE
),
129 REQ(VHOST_USER_POSTCOPY_LISTEN
),
130 REQ(VHOST_USER_POSTCOPY_END
),
131 REQ(VHOST_USER_GET_INFLIGHT_FD
),
132 REQ(VHOST_USER_SET_INFLIGHT_FD
),
133 REQ(VHOST_USER_GPU_SET_SOCKET
),
138 if (req
< VHOST_USER_MAX
) {
139 return vu_request_str
[req
];
146 vu_panic(VuDev
*dev
, const char *msg
, ...)
152 if (vasprintf(&buf
, msg
, ap
) < 0) {
158 dev
->panic(dev
, buf
);
161 /* FIXME: find a way to call virtio_error? */
164 /* Translate guest physical address to our virtual address. */
166 vu_gpa_to_va(VuDev
*dev
, uint64_t *plen
, uint64_t guest_addr
)
174 /* Find matching memory region. */
175 for (i
= 0; i
< dev
->nregions
; i
++) {
176 VuDevRegion
*r
= &dev
->regions
[i
];
178 if ((guest_addr
>= r
->gpa
) && (guest_addr
< (r
->gpa
+ r
->size
))) {
179 if ((guest_addr
+ *plen
) > (r
->gpa
+ r
->size
)) {
180 *plen
= r
->gpa
+ r
->size
- guest_addr
;
182 return (void *)(uintptr_t)
183 guest_addr
- r
->gpa
+ r
->mmap_addr
+ r
->mmap_offset
;
190 /* Translate qemu virtual address to our virtual address. */
192 qva_to_va(VuDev
*dev
, uint64_t qemu_addr
)
196 /* Find matching memory region. */
197 for (i
= 0; i
< dev
->nregions
; i
++) {
198 VuDevRegion
*r
= &dev
->regions
[i
];
200 if ((qemu_addr
>= r
->qva
) && (qemu_addr
< (r
->qva
+ r
->size
))) {
201 return (void *)(uintptr_t)
202 qemu_addr
- r
->qva
+ r
->mmap_addr
+ r
->mmap_offset
;
210 vmsg_close_fds(VhostUserMsg
*vmsg
)
214 for (i
= 0; i
< vmsg
->fd_num
; i
++) {
219 /* Set reply payload.u64 and clear request flags and fd_num */
220 static void vmsg_set_reply_u64(VhostUserMsg
*vmsg
, uint64_t val
)
222 vmsg
->flags
= 0; /* defaults will be set by vu_send_reply() */
223 vmsg
->size
= sizeof(vmsg
->payload
.u64
);
224 vmsg
->payload
.u64
= val
;
228 /* A test to see if we have userfault available */
232 #if defined(__linux__) && defined(__NR_userfaultfd) &&\
233 defined(UFFD_FEATURE_MISSING_SHMEM) &&\
234 defined(UFFD_FEATURE_MISSING_HUGETLBFS)
235 /* Now test the kernel we're running on really has the features */
236 int ufd
= syscall(__NR_userfaultfd
, O_CLOEXEC
| O_NONBLOCK
);
237 struct uffdio_api api_struct
;
242 api_struct
.api
= UFFD_API
;
243 api_struct
.features
= UFFD_FEATURE_MISSING_SHMEM
|
244 UFFD_FEATURE_MISSING_HUGETLBFS
;
245 if (ioctl(ufd
, UFFDIO_API
, &api_struct
)) {
258 vu_message_read(VuDev
*dev
, int conn_fd
, VhostUserMsg
*vmsg
)
260 char control
[CMSG_SPACE(VHOST_MEMORY_MAX_NREGIONS
* sizeof(int))] = { };
262 .iov_base
= (char *)vmsg
,
263 .iov_len
= VHOST_USER_HDR_SIZE
,
265 struct msghdr msg
= {
268 .msg_control
= control
,
269 .msg_controllen
= sizeof(control
),
272 struct cmsghdr
*cmsg
;
276 rc
= recvmsg(conn_fd
, &msg
, 0);
277 } while (rc
< 0 && (errno
== EINTR
|| errno
== EAGAIN
));
280 vu_panic(dev
, "Error while recvmsg: %s", strerror(errno
));
285 for (cmsg
= CMSG_FIRSTHDR(&msg
);
287 cmsg
= CMSG_NXTHDR(&msg
, cmsg
))
289 if (cmsg
->cmsg_level
== SOL_SOCKET
&& cmsg
->cmsg_type
== SCM_RIGHTS
) {
290 fd_size
= cmsg
->cmsg_len
- CMSG_LEN(0);
291 vmsg
->fd_num
= fd_size
/ sizeof(int);
292 memcpy(vmsg
->fds
, CMSG_DATA(cmsg
), fd_size
);
297 if (vmsg
->size
> sizeof(vmsg
->payload
)) {
299 "Error: too big message request: %d, size: vmsg->size: %u, "
300 "while sizeof(vmsg->payload) = %zu\n",
301 vmsg
->request
, vmsg
->size
, sizeof(vmsg
->payload
));
307 rc
= read(conn_fd
, &vmsg
->payload
, vmsg
->size
);
308 } while (rc
< 0 && (errno
== EINTR
|| errno
== EAGAIN
));
311 vu_panic(dev
, "Error while reading: %s", strerror(errno
));
315 assert(rc
== vmsg
->size
);
321 vmsg_close_fds(vmsg
);
327 vu_message_write(VuDev
*dev
, int conn_fd
, VhostUserMsg
*vmsg
)
330 uint8_t *p
= (uint8_t *)vmsg
;
331 char control
[CMSG_SPACE(VHOST_MEMORY_MAX_NREGIONS
* sizeof(int))] = { };
333 .iov_base
= (char *)vmsg
,
334 .iov_len
= VHOST_USER_HDR_SIZE
,
336 struct msghdr msg
= {
339 .msg_control
= control
,
341 struct cmsghdr
*cmsg
;
343 memset(control
, 0, sizeof(control
));
344 assert(vmsg
->fd_num
<= VHOST_MEMORY_MAX_NREGIONS
);
345 if (vmsg
->fd_num
> 0) {
346 size_t fdsize
= vmsg
->fd_num
* sizeof(int);
347 msg
.msg_controllen
= CMSG_SPACE(fdsize
);
348 cmsg
= CMSG_FIRSTHDR(&msg
);
349 cmsg
->cmsg_len
= CMSG_LEN(fdsize
);
350 cmsg
->cmsg_level
= SOL_SOCKET
;
351 cmsg
->cmsg_type
= SCM_RIGHTS
;
352 memcpy(CMSG_DATA(cmsg
), vmsg
->fds
, fdsize
);
354 msg
.msg_controllen
= 0;
358 rc
= sendmsg(conn_fd
, &msg
, 0);
359 } while (rc
< 0 && (errno
== EINTR
|| errno
== EAGAIN
));
364 rc
= write(conn_fd
, vmsg
->data
, vmsg
->size
);
366 rc
= write(conn_fd
, p
+ VHOST_USER_HDR_SIZE
, vmsg
->size
);
368 } while (rc
< 0 && (errno
== EINTR
|| errno
== EAGAIN
));
372 vu_panic(dev
, "Error while writing: %s", strerror(errno
));
380 vu_send_reply(VuDev
*dev
, int conn_fd
, VhostUserMsg
*vmsg
)
382 /* Set the version in the flags when sending the reply */
383 vmsg
->flags
&= ~VHOST_USER_VERSION_MASK
;
384 vmsg
->flags
|= VHOST_USER_VERSION
;
385 vmsg
->flags
|= VHOST_USER_REPLY_MASK
;
387 return vu_message_write(dev
, conn_fd
, vmsg
);
391 vu_process_message_reply(VuDev
*dev
, const VhostUserMsg
*vmsg
)
393 VhostUserMsg msg_reply
;
395 if ((vmsg
->flags
& VHOST_USER_NEED_REPLY_MASK
) == 0) {
399 if (!vu_message_read(dev
, dev
->slave_fd
, &msg_reply
)) {
403 if (msg_reply
.request
!= vmsg
->request
) {
404 DPRINT("Received unexpected msg type. Expected %d received %d",
405 vmsg
->request
, msg_reply
.request
);
409 return msg_reply
.payload
.u64
== 0;
412 /* Kick the log_call_fd if required. */
414 vu_log_kick(VuDev
*dev
)
416 if (dev
->log_call_fd
!= -1) {
417 DPRINT("Kicking the QEMU's log...\n");
418 if (eventfd_write(dev
->log_call_fd
, 1) < 0) {
419 vu_panic(dev
, "Error writing eventfd: %s", strerror(errno
));
425 vu_log_page(uint8_t *log_table
, uint64_t page
)
427 DPRINT("Logged dirty guest page: %"PRId64
"\n", page
);
428 atomic_or(&log_table
[page
/ 8], 1 << (page
% 8));
432 vu_log_write(VuDev
*dev
, uint64_t address
, uint64_t length
)
436 if (!(dev
->features
& (1ULL << VHOST_F_LOG_ALL
)) ||
437 !dev
->log_table
|| !length
) {
441 assert(dev
->log_size
> ((address
+ length
- 1) / VHOST_LOG_PAGE
/ 8));
443 page
= address
/ VHOST_LOG_PAGE
;
444 while (page
* VHOST_LOG_PAGE
< address
+ length
) {
445 vu_log_page(dev
->log_table
, page
);
453 vu_kick_cb(VuDev
*dev
, int condition
, void *data
)
455 int index
= (intptr_t)data
;
456 VuVirtq
*vq
= &dev
->vq
[index
];
457 int sock
= vq
->kick_fd
;
461 rc
= eventfd_read(sock
, &kick_data
);
463 vu_panic(dev
, "kick eventfd_read(): %s", strerror(errno
));
464 dev
->remove_watch(dev
, dev
->vq
[index
].kick_fd
);
466 DPRINT("Got kick_data: %016"PRIx64
" handler:%p idx:%d\n",
467 kick_data
, vq
->handler
, index
);
469 vq
->handler(dev
, index
);
475 vu_get_features_exec(VuDev
*dev
, VhostUserMsg
*vmsg
)
478 1ULL << VHOST_F_LOG_ALL
|
479 1ULL << VHOST_USER_F_PROTOCOL_FEATURES
;
481 if (dev
->iface
->get_features
) {
482 vmsg
->payload
.u64
|= dev
->iface
->get_features(dev
);
485 vmsg
->size
= sizeof(vmsg
->payload
.u64
);
488 DPRINT("Sending back to guest u64: 0x%016"PRIx64
"\n", vmsg
->payload
.u64
);
494 vu_set_enable_all_rings(VuDev
*dev
, bool enabled
)
498 for (i
= 0; i
< dev
->max_queues
; i
++) {
499 dev
->vq
[i
].enable
= enabled
;
504 vu_set_features_exec(VuDev
*dev
, VhostUserMsg
*vmsg
)
506 DPRINT("u64: 0x%016"PRIx64
"\n", vmsg
->payload
.u64
);
508 dev
->features
= vmsg
->payload
.u64
;
510 if (!(dev
->features
& VHOST_USER_F_PROTOCOL_FEATURES
)) {
511 vu_set_enable_all_rings(dev
, true);
514 if (dev
->iface
->set_features
) {
515 dev
->iface
->set_features(dev
, dev
->features
);
522 vu_set_owner_exec(VuDev
*dev
, VhostUserMsg
*vmsg
)
528 vu_close_log(VuDev
*dev
)
530 if (dev
->log_table
) {
531 if (munmap(dev
->log_table
, dev
->log_size
) != 0) {
532 perror("close log munmap() error");
535 dev
->log_table
= NULL
;
537 if (dev
->log_call_fd
!= -1) {
538 close(dev
->log_call_fd
);
539 dev
->log_call_fd
= -1;
544 vu_reset_device_exec(VuDev
*dev
, VhostUserMsg
*vmsg
)
546 vu_set_enable_all_rings(dev
, false);
552 vu_set_mem_table_exec_postcopy(VuDev
*dev
, VhostUserMsg
*vmsg
)
555 VhostUserMemory m
= vmsg
->payload
.memory
, *memory
= &m
;
556 dev
->nregions
= memory
->nregions
;
558 DPRINT("Nregions: %d\n", memory
->nregions
);
559 for (i
= 0; i
< dev
->nregions
; i
++) {
561 VhostUserMemoryRegion
*msg_region
= &memory
->regions
[i
];
562 VuDevRegion
*dev_region
= &dev
->regions
[i
];
564 DPRINT("Region %d\n", i
);
565 DPRINT(" guest_phys_addr: 0x%016"PRIx64
"\n",
566 msg_region
->guest_phys_addr
);
567 DPRINT(" memory_size: 0x%016"PRIx64
"\n",
568 msg_region
->memory_size
);
569 DPRINT(" userspace_addr 0x%016"PRIx64
"\n",
570 msg_region
->userspace_addr
);
571 DPRINT(" mmap_offset 0x%016"PRIx64
"\n",
572 msg_region
->mmap_offset
);
574 dev_region
->gpa
= msg_region
->guest_phys_addr
;
575 dev_region
->size
= msg_region
->memory_size
;
576 dev_region
->qva
= msg_region
->userspace_addr
;
577 dev_region
->mmap_offset
= msg_region
->mmap_offset
;
579 /* We don't use offset argument of mmap() since the
580 * mapped address has to be page aligned, and we use huge
582 * In postcopy we're using PROT_NONE here to catch anyone
583 * accessing it before we userfault
585 mmap_addr
= mmap(0, dev_region
->size
+ dev_region
->mmap_offset
,
586 PROT_NONE
, MAP_SHARED
,
589 if (mmap_addr
== MAP_FAILED
) {
590 vu_panic(dev
, "region mmap error: %s", strerror(errno
));
592 dev_region
->mmap_addr
= (uint64_t)(uintptr_t)mmap_addr
;
593 DPRINT(" mmap_addr: 0x%016"PRIx64
"\n",
594 dev_region
->mmap_addr
);
597 /* Return the address to QEMU so that it can translate the ufd
598 * fault addresses back.
600 msg_region
->userspace_addr
= (uintptr_t)(mmap_addr
+
601 dev_region
->mmap_offset
);
605 /* Send the message back to qemu with the addresses filled in */
607 if (!vu_send_reply(dev
, dev
->sock
, vmsg
)) {
608 vu_panic(dev
, "failed to respond to set-mem-table for postcopy");
612 /* Wait for QEMU to confirm that it's registered the handler for the
615 if (!vu_message_read(dev
, dev
->sock
, vmsg
) ||
616 vmsg
->size
!= sizeof(vmsg
->payload
.u64
) ||
617 vmsg
->payload
.u64
!= 0) {
618 vu_panic(dev
, "failed to receive valid ack for postcopy set-mem-table");
622 /* OK, now we can go and register the memory and generate faults */
623 for (i
= 0; i
< dev
->nregions
; i
++) {
624 VuDevRegion
*dev_region
= &dev
->regions
[i
];
626 #ifdef UFFDIO_REGISTER
627 /* We should already have an open ufd. Mark each memory
629 * Discard any mapping we have here; note I can't use MADV_REMOVE
630 * or fallocate to make the hole since I don't want to lose
631 * data that's already arrived in the shared process.
632 * TODO: How to do hugepage
634 ret
= madvise((void *)(uintptr_t)dev_region
->mmap_addr
,
635 dev_region
->size
+ dev_region
->mmap_offset
,
639 "%s: Failed to madvise(DONTNEED) region %d: %s\n",
640 __func__
, i
, strerror(errno
));
642 /* Turn off transparent hugepages so we dont get lose wakeups
643 * in neighbouring pages.
644 * TODO: Turn this backon later.
646 ret
= madvise((void *)(uintptr_t)dev_region
->mmap_addr
,
647 dev_region
->size
+ dev_region
->mmap_offset
,
650 /* Note: This can happen legally on kernels that are configured
651 * without madvise'able hugepages
654 "%s: Failed to madvise(NOHUGEPAGE) region %d: %s\n",
655 __func__
, i
, strerror(errno
));
657 struct uffdio_register reg_struct
;
658 reg_struct
.range
.start
= (uintptr_t)dev_region
->mmap_addr
;
659 reg_struct
.range
.len
= dev_region
->size
+ dev_region
->mmap_offset
;
660 reg_struct
.mode
= UFFDIO_REGISTER_MODE_MISSING
;
662 if (ioctl(dev
->postcopy_ufd
, UFFDIO_REGISTER
, ®_struct
)) {
663 vu_panic(dev
, "%s: Failed to userfault region %d "
664 "@%p + size:%zx offset: %zx: (ufd=%d)%s\n",
666 dev_region
->mmap_addr
,
667 dev_region
->size
, dev_region
->mmap_offset
,
668 dev
->postcopy_ufd
, strerror(errno
));
671 if (!(reg_struct
.ioctls
& ((__u64
)1 << _UFFDIO_COPY
))) {
672 vu_panic(dev
, "%s Region (%d) doesn't support COPY",
676 DPRINT("%s: region %d: Registered userfault for %"
677 PRIx64
" + %" PRIx64
"\n", __func__
, i
,
678 (uint64_t)reg_struct
.range
.start
,
679 (uint64_t)reg_struct
.range
.len
);
680 /* Now it's registered we can let the client at it */
681 if (mprotect((void *)(uintptr_t)dev_region
->mmap_addr
,
682 dev_region
->size
+ dev_region
->mmap_offset
,
683 PROT_READ
| PROT_WRITE
)) {
684 vu_panic(dev
, "failed to mprotect region %d for postcopy (%s)",
688 /* TODO: Stash 'zero' support flags somewhere */
696 vu_set_mem_table_exec(VuDev
*dev
, VhostUserMsg
*vmsg
)
699 VhostUserMemory m
= vmsg
->payload
.memory
, *memory
= &m
;
701 for (i
= 0; i
< dev
->nregions
; i
++) {
702 VuDevRegion
*r
= &dev
->regions
[i
];
703 void *m
= (void *) (uintptr_t) r
->mmap_addr
;
706 munmap(m
, r
->size
+ r
->mmap_offset
);
709 dev
->nregions
= memory
->nregions
;
711 if (dev
->postcopy_listening
) {
712 return vu_set_mem_table_exec_postcopy(dev
, vmsg
);
715 DPRINT("Nregions: %d\n", memory
->nregions
);
716 for (i
= 0; i
< dev
->nregions
; i
++) {
718 VhostUserMemoryRegion
*msg_region
= &memory
->regions
[i
];
719 VuDevRegion
*dev_region
= &dev
->regions
[i
];
721 DPRINT("Region %d\n", i
);
722 DPRINT(" guest_phys_addr: 0x%016"PRIx64
"\n",
723 msg_region
->guest_phys_addr
);
724 DPRINT(" memory_size: 0x%016"PRIx64
"\n",
725 msg_region
->memory_size
);
726 DPRINT(" userspace_addr 0x%016"PRIx64
"\n",
727 msg_region
->userspace_addr
);
728 DPRINT(" mmap_offset 0x%016"PRIx64
"\n",
729 msg_region
->mmap_offset
);
731 dev_region
->gpa
= msg_region
->guest_phys_addr
;
732 dev_region
->size
= msg_region
->memory_size
;
733 dev_region
->qva
= msg_region
->userspace_addr
;
734 dev_region
->mmap_offset
= msg_region
->mmap_offset
;
736 /* We don't use offset argument of mmap() since the
737 * mapped address has to be page aligned, and we use huge
739 mmap_addr
= mmap(0, dev_region
->size
+ dev_region
->mmap_offset
,
740 PROT_READ
| PROT_WRITE
, MAP_SHARED
,
743 if (mmap_addr
== MAP_FAILED
) {
744 vu_panic(dev
, "region mmap error: %s", strerror(errno
));
746 dev_region
->mmap_addr
= (uint64_t)(uintptr_t)mmap_addr
;
747 DPRINT(" mmap_addr: 0x%016"PRIx64
"\n",
748 dev_region
->mmap_addr
);
758 vu_set_log_base_exec(VuDev
*dev
, VhostUserMsg
*vmsg
)
761 uint64_t log_mmap_size
, log_mmap_offset
;
764 if (vmsg
->fd_num
!= 1 ||
765 vmsg
->size
!= sizeof(vmsg
->payload
.log
)) {
766 vu_panic(dev
, "Invalid log_base message");
771 log_mmap_offset
= vmsg
->payload
.log
.mmap_offset
;
772 log_mmap_size
= vmsg
->payload
.log
.mmap_size
;
773 DPRINT("Log mmap_offset: %"PRId64
"\n", log_mmap_offset
);
774 DPRINT("Log mmap_size: %"PRId64
"\n", log_mmap_size
);
776 rc
= mmap(0, log_mmap_size
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, fd
,
779 if (rc
== MAP_FAILED
) {
780 perror("log mmap error");
783 if (dev
->log_table
) {
784 munmap(dev
->log_table
, dev
->log_size
);
787 dev
->log_size
= log_mmap_size
;
789 vmsg
->size
= sizeof(vmsg
->payload
.u64
);
796 vu_set_log_fd_exec(VuDev
*dev
, VhostUserMsg
*vmsg
)
798 if (vmsg
->fd_num
!= 1) {
799 vu_panic(dev
, "Invalid log_fd message");
803 if (dev
->log_call_fd
!= -1) {
804 close(dev
->log_call_fd
);
806 dev
->log_call_fd
= vmsg
->fds
[0];
807 DPRINT("Got log_call_fd: %d\n", vmsg
->fds
[0]);
813 vu_set_vring_num_exec(VuDev
*dev
, VhostUserMsg
*vmsg
)
815 unsigned int index
= vmsg
->payload
.state
.index
;
816 unsigned int num
= vmsg
->payload
.state
.num
;
818 DPRINT("State.index: %d\n", index
);
819 DPRINT("State.num: %d\n", num
);
820 dev
->vq
[index
].vring
.num
= num
;
826 vu_set_vring_addr_exec(VuDev
*dev
, VhostUserMsg
*vmsg
)
828 struct vhost_vring_addr addr
= vmsg
->payload
.addr
, *vra
= &addr
;
829 unsigned int index
= vra
->index
;
830 VuVirtq
*vq
= &dev
->vq
[index
];
832 DPRINT("vhost_vring_addr:\n");
833 DPRINT(" index: %d\n", vra
->index
);
834 DPRINT(" flags: %d\n", vra
->flags
);
835 DPRINT(" desc_user_addr: 0x%016" PRIx64
"\n", vra
->desc_user_addr
);
836 DPRINT(" used_user_addr: 0x%016" PRIx64
"\n", vra
->used_user_addr
);
837 DPRINT(" avail_user_addr: 0x%016" PRIx64
"\n", vra
->avail_user_addr
);
838 DPRINT(" log_guest_addr: 0x%016" PRIx64
"\n", vra
->log_guest_addr
);
840 vq
->vring
.flags
= vra
->flags
;
841 vq
->vring
.desc
= qva_to_va(dev
, vra
->desc_user_addr
);
842 vq
->vring
.used
= qva_to_va(dev
, vra
->used_user_addr
);
843 vq
->vring
.avail
= qva_to_va(dev
, vra
->avail_user_addr
);
844 vq
->vring
.log_guest_addr
= vra
->log_guest_addr
;
846 DPRINT("Setting virtq addresses:\n");
847 DPRINT(" vring_desc at %p\n", vq
->vring
.desc
);
848 DPRINT(" vring_used at %p\n", vq
->vring
.used
);
849 DPRINT(" vring_avail at %p\n", vq
->vring
.avail
);
851 if (!(vq
->vring
.desc
&& vq
->vring
.used
&& vq
->vring
.avail
)) {
852 vu_panic(dev
, "Invalid vring_addr message");
856 vq
->used_idx
= vq
->vring
.used
->idx
;
858 if (vq
->last_avail_idx
!= vq
->used_idx
) {
859 bool resume
= dev
->iface
->queue_is_processed_in_order
&&
860 dev
->iface
->queue_is_processed_in_order(dev
, index
);
862 DPRINT("Last avail index != used index: %u != %u%s\n",
863 vq
->last_avail_idx
, vq
->used_idx
,
864 resume
? ", resuming" : "");
867 vq
->shadow_avail_idx
= vq
->last_avail_idx
= vq
->used_idx
;
875 vu_set_vring_base_exec(VuDev
*dev
, VhostUserMsg
*vmsg
)
877 unsigned int index
= vmsg
->payload
.state
.index
;
878 unsigned int num
= vmsg
->payload
.state
.num
;
880 DPRINT("State.index: %d\n", index
);
881 DPRINT("State.num: %d\n", num
);
882 dev
->vq
[index
].shadow_avail_idx
= dev
->vq
[index
].last_avail_idx
= num
;
888 vu_get_vring_base_exec(VuDev
*dev
, VhostUserMsg
*vmsg
)
890 unsigned int index
= vmsg
->payload
.state
.index
;
892 DPRINT("State.index: %d\n", index
);
893 vmsg
->payload
.state
.num
= dev
->vq
[index
].last_avail_idx
;
894 vmsg
->size
= sizeof(vmsg
->payload
.state
);
896 dev
->vq
[index
].started
= false;
897 if (dev
->iface
->queue_set_started
) {
898 dev
->iface
->queue_set_started(dev
, index
, false);
901 if (dev
->vq
[index
].call_fd
!= -1) {
902 close(dev
->vq
[index
].call_fd
);
903 dev
->vq
[index
].call_fd
= -1;
905 if (dev
->vq
[index
].kick_fd
!= -1) {
906 dev
->remove_watch(dev
, dev
->vq
[index
].kick_fd
);
907 close(dev
->vq
[index
].kick_fd
);
908 dev
->vq
[index
].kick_fd
= -1;
915 vu_check_queue_msg_file(VuDev
*dev
, VhostUserMsg
*vmsg
)
917 int index
= vmsg
->payload
.u64
& VHOST_USER_VRING_IDX_MASK
;
919 if (index
>= dev
->max_queues
) {
920 vmsg_close_fds(vmsg
);
921 vu_panic(dev
, "Invalid queue index: %u", index
);
925 if (vmsg
->payload
.u64
& VHOST_USER_VRING_NOFD_MASK
||
927 vmsg_close_fds(vmsg
);
928 vu_panic(dev
, "Invalid fds in request: %d", vmsg
->request
);
936 inflight_desc_compare(const void *a
, const void *b
)
938 VuVirtqInflightDesc
*desc0
= (VuVirtqInflightDesc
*)a
,
939 *desc1
= (VuVirtqInflightDesc
*)b
;
941 if (desc1
->counter
> desc0
->counter
&&
942 (desc1
->counter
- desc0
->counter
) < VIRTQUEUE_MAX_SIZE
* 2) {
950 vu_check_queue_inflights(VuDev
*dev
, VuVirtq
*vq
)
954 if (!has_feature(dev
->protocol_features
,
955 VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD
)) {
959 if (unlikely(!vq
->inflight
)) {
963 if (unlikely(!vq
->inflight
->version
)) {
964 /* initialize the buffer */
965 vq
->inflight
->version
= INFLIGHT_VERSION
;
969 vq
->used_idx
= vq
->vring
.used
->idx
;
970 vq
->resubmit_num
= 0;
971 vq
->resubmit_list
= NULL
;
974 if (unlikely(vq
->inflight
->used_idx
!= vq
->used_idx
)) {
975 vq
->inflight
->desc
[vq
->inflight
->last_batch_head
].inflight
= 0;
979 vq
->inflight
->used_idx
= vq
->used_idx
;
982 for (i
= 0; i
< vq
->inflight
->desc_num
; i
++) {
983 if (vq
->inflight
->desc
[i
].inflight
== 1) {
988 vq
->shadow_avail_idx
= vq
->last_avail_idx
= vq
->inuse
+ vq
->used_idx
;
991 vq
->resubmit_list
= malloc(sizeof(VuVirtqInflightDesc
) * vq
->inuse
);
992 if (!vq
->resubmit_list
) {
996 for (i
= 0; i
< vq
->inflight
->desc_num
; i
++) {
997 if (vq
->inflight
->desc
[i
].inflight
) {
998 vq
->resubmit_list
[vq
->resubmit_num
].index
= i
;
999 vq
->resubmit_list
[vq
->resubmit_num
].counter
=
1000 vq
->inflight
->desc
[i
].counter
;
1005 if (vq
->resubmit_num
> 1) {
1006 qsort(vq
->resubmit_list
, vq
->resubmit_num
,
1007 sizeof(VuVirtqInflightDesc
), inflight_desc_compare
);
1009 vq
->counter
= vq
->resubmit_list
[0].counter
+ 1;
1012 /* in case of I/O hang after reconnecting */
1013 if (eventfd_write(vq
->kick_fd
, 1)) {
1021 vu_set_vring_kick_exec(VuDev
*dev
, VhostUserMsg
*vmsg
)
1023 int index
= vmsg
->payload
.u64
& VHOST_USER_VRING_IDX_MASK
;
1025 DPRINT("u64: 0x%016"PRIx64
"\n", vmsg
->payload
.u64
);
1027 if (!vu_check_queue_msg_file(dev
, vmsg
)) {
1031 if (dev
->vq
[index
].kick_fd
!= -1) {
1032 dev
->remove_watch(dev
, dev
->vq
[index
].kick_fd
);
1033 close(dev
->vq
[index
].kick_fd
);
1034 dev
->vq
[index
].kick_fd
= -1;
1037 dev
->vq
[index
].kick_fd
= vmsg
->fds
[0];
1038 DPRINT("Got kick_fd: %d for vq: %d\n", vmsg
->fds
[0], index
);
1040 dev
->vq
[index
].started
= true;
1041 if (dev
->iface
->queue_set_started
) {
1042 dev
->iface
->queue_set_started(dev
, index
, true);
1045 if (dev
->vq
[index
].kick_fd
!= -1 && dev
->vq
[index
].handler
) {
1046 dev
->set_watch(dev
, dev
->vq
[index
].kick_fd
, VU_WATCH_IN
,
1047 vu_kick_cb
, (void *)(long)index
);
1049 DPRINT("Waiting for kicks on fd: %d for vq: %d\n",
1050 dev
->vq
[index
].kick_fd
, index
);
1053 if (vu_check_queue_inflights(dev
, &dev
->vq
[index
])) {
1054 vu_panic(dev
, "Failed to check inflights for vq: %d\n", index
);
1060 void vu_set_queue_handler(VuDev
*dev
, VuVirtq
*vq
,
1061 vu_queue_handler_cb handler
)
1063 int qidx
= vq
- dev
->vq
;
1065 vq
->handler
= handler
;
1066 if (vq
->kick_fd
>= 0) {
1068 dev
->set_watch(dev
, vq
->kick_fd
, VU_WATCH_IN
,
1069 vu_kick_cb
, (void *)(long)qidx
);
1071 dev
->remove_watch(dev
, vq
->kick_fd
);
1076 bool vu_set_queue_host_notifier(VuDev
*dev
, VuVirtq
*vq
, int fd
,
1077 int size
, int offset
)
1079 int qidx
= vq
- dev
->vq
;
1081 VhostUserMsg vmsg
= {
1082 .request
= VHOST_USER_SLAVE_VRING_HOST_NOTIFIER_MSG
,
1083 .flags
= VHOST_USER_VERSION
| VHOST_USER_NEED_REPLY_MASK
,
1084 .size
= sizeof(vmsg
.payload
.area
),
1086 .u64
= qidx
& VHOST_USER_VRING_IDX_MASK
,
1093 vmsg
.payload
.area
.u64
|= VHOST_USER_VRING_NOFD_MASK
;
1095 vmsg
.fds
[fd_num
++] = fd
;
1098 vmsg
.fd_num
= fd_num
;
1100 if ((dev
->protocol_features
& VHOST_USER_PROTOCOL_F_SLAVE_SEND_FD
) == 0) {
1104 if (!vu_message_write(dev
, dev
->slave_fd
, &vmsg
)) {
1108 return vu_process_message_reply(dev
, &vmsg
);
1112 vu_set_vring_call_exec(VuDev
*dev
, VhostUserMsg
*vmsg
)
1114 int index
= vmsg
->payload
.u64
& VHOST_USER_VRING_IDX_MASK
;
1116 DPRINT("u64: 0x%016"PRIx64
"\n", vmsg
->payload
.u64
);
1118 if (!vu_check_queue_msg_file(dev
, vmsg
)) {
1122 if (dev
->vq
[index
].call_fd
!= -1) {
1123 close(dev
->vq
[index
].call_fd
);
1124 dev
->vq
[index
].call_fd
= -1;
1127 dev
->vq
[index
].call_fd
= vmsg
->fds
[0];
1129 /* in case of I/O hang after reconnecting */
1130 if (eventfd_write(vmsg
->fds
[0], 1)) {
1134 DPRINT("Got call_fd: %d for vq: %d\n", vmsg
->fds
[0], index
);
1140 vu_set_vring_err_exec(VuDev
*dev
, VhostUserMsg
*vmsg
)
1142 int index
= vmsg
->payload
.u64
& VHOST_USER_VRING_IDX_MASK
;
1144 DPRINT("u64: 0x%016"PRIx64
"\n", vmsg
->payload
.u64
);
1146 if (!vu_check_queue_msg_file(dev
, vmsg
)) {
1150 if (dev
->vq
[index
].err_fd
!= -1) {
1151 close(dev
->vq
[index
].err_fd
);
1152 dev
->vq
[index
].err_fd
= -1;
1155 dev
->vq
[index
].err_fd
= vmsg
->fds
[0];
1161 vu_get_protocol_features_exec(VuDev
*dev
, VhostUserMsg
*vmsg
)
1163 uint64_t features
= 1ULL << VHOST_USER_PROTOCOL_F_MQ
|
1164 1ULL << VHOST_USER_PROTOCOL_F_LOG_SHMFD
|
1165 1ULL << VHOST_USER_PROTOCOL_F_SLAVE_REQ
|
1166 1ULL << VHOST_USER_PROTOCOL_F_HOST_NOTIFIER
|
1167 1ULL << VHOST_USER_PROTOCOL_F_SLAVE_SEND_FD
;
1169 if (have_userfault()) {
1170 features
|= 1ULL << VHOST_USER_PROTOCOL_F_PAGEFAULT
;
1173 if (dev
->iface
->get_config
&& dev
->iface
->set_config
) {
1174 features
|= 1ULL << VHOST_USER_PROTOCOL_F_CONFIG
;
1177 if (dev
->iface
->get_protocol_features
) {
1178 features
|= dev
->iface
->get_protocol_features(dev
);
1181 vmsg_set_reply_u64(vmsg
, features
);
1186 vu_set_protocol_features_exec(VuDev
*dev
, VhostUserMsg
*vmsg
)
1188 uint64_t features
= vmsg
->payload
.u64
;
1190 DPRINT("u64: 0x%016"PRIx64
"\n", features
);
1192 dev
->protocol_features
= vmsg
->payload
.u64
;
1194 if (dev
->iface
->set_protocol_features
) {
1195 dev
->iface
->set_protocol_features(dev
, features
);
1202 vu_get_queue_num_exec(VuDev
*dev
, VhostUserMsg
*vmsg
)
1204 vmsg_set_reply_u64(vmsg
, dev
->max_queues
);
1209 vu_set_vring_enable_exec(VuDev
*dev
, VhostUserMsg
*vmsg
)
1211 unsigned int index
= vmsg
->payload
.state
.index
;
1212 unsigned int enable
= vmsg
->payload
.state
.num
;
1214 DPRINT("State.index: %d\n", index
);
1215 DPRINT("State.enable: %d\n", enable
);
1217 if (index
>= dev
->max_queues
) {
1218 vu_panic(dev
, "Invalid vring_enable index: %u", index
);
1222 dev
->vq
[index
].enable
= enable
;
1227 vu_set_slave_req_fd(VuDev
*dev
, VhostUserMsg
*vmsg
)
1229 if (vmsg
->fd_num
!= 1) {
1230 vu_panic(dev
, "Invalid slave_req_fd message (%d fd's)", vmsg
->fd_num
);
1234 if (dev
->slave_fd
!= -1) {
1235 close(dev
->slave_fd
);
1237 dev
->slave_fd
= vmsg
->fds
[0];
1238 DPRINT("Got slave_fd: %d\n", vmsg
->fds
[0]);
1244 vu_get_config(VuDev
*dev
, VhostUserMsg
*vmsg
)
1248 if (dev
->iface
->get_config
) {
1249 ret
= dev
->iface
->get_config(dev
, vmsg
->payload
.config
.region
,
1250 vmsg
->payload
.config
.size
);
1254 /* resize to zero to indicate an error to master */
1262 vu_set_config(VuDev
*dev
, VhostUserMsg
*vmsg
)
1266 if (dev
->iface
->set_config
) {
1267 ret
= dev
->iface
->set_config(dev
, vmsg
->payload
.config
.region
,
1268 vmsg
->payload
.config
.offset
,
1269 vmsg
->payload
.config
.size
,
1270 vmsg
->payload
.config
.flags
);
1272 vu_panic(dev
, "Set virtio configuration space failed");
1280 vu_set_postcopy_advise(VuDev
*dev
, VhostUserMsg
*vmsg
)
1282 dev
->postcopy_ufd
= -1;
1284 struct uffdio_api api_struct
;
1286 dev
->postcopy_ufd
= syscall(__NR_userfaultfd
, O_CLOEXEC
| O_NONBLOCK
);
1290 if (dev
->postcopy_ufd
== -1) {
1291 vu_panic(dev
, "Userfaultfd not available: %s", strerror(errno
));
1296 api_struct
.api
= UFFD_API
;
1297 api_struct
.features
= 0;
1298 if (ioctl(dev
->postcopy_ufd
, UFFDIO_API
, &api_struct
)) {
1299 vu_panic(dev
, "Failed UFFDIO_API: %s", strerror(errno
));
1300 close(dev
->postcopy_ufd
);
1301 dev
->postcopy_ufd
= -1;
1304 /* TODO: Stash feature flags somewhere */
1308 /* Return a ufd to the QEMU */
1310 vmsg
->fds
[0] = dev
->postcopy_ufd
;
1311 return true; /* = send a reply */
1315 vu_set_postcopy_listen(VuDev
*dev
, VhostUserMsg
*vmsg
)
1317 if (dev
->nregions
) {
1318 vu_panic(dev
, "Regions already registered at postcopy-listen");
1319 vmsg_set_reply_u64(vmsg
, -1);
1322 dev
->postcopy_listening
= true;
1324 vmsg_set_reply_u64(vmsg
, 0);
1329 vu_set_postcopy_end(VuDev
*dev
, VhostUserMsg
*vmsg
)
1331 DPRINT("%s: Entry\n", __func__
);
1332 dev
->postcopy_listening
= false;
1333 if (dev
->postcopy_ufd
> 0) {
1334 close(dev
->postcopy_ufd
);
1335 dev
->postcopy_ufd
= -1;
1336 DPRINT("%s: Done close\n", __func__
);
1339 vmsg_set_reply_u64(vmsg
, 0);
1340 DPRINT("%s: exit\n", __func__
);
1344 static inline uint64_t
1345 vu_inflight_queue_size(uint16_t queue_size
)
1347 return ALIGN_UP(sizeof(VuDescStateSplit
) * queue_size
+
1348 sizeof(uint16_t), INFLIGHT_ALIGNMENT
);
1352 vu_get_inflight_fd(VuDev
*dev
, VhostUserMsg
*vmsg
)
1357 uint16_t num_queues
, queue_size
;
1359 if (vmsg
->size
!= sizeof(vmsg
->payload
.inflight
)) {
1360 vu_panic(dev
, "Invalid get_inflight_fd message:%d", vmsg
->size
);
1361 vmsg
->payload
.inflight
.mmap_size
= 0;
1365 num_queues
= vmsg
->payload
.inflight
.num_queues
;
1366 queue_size
= vmsg
->payload
.inflight
.queue_size
;
1368 DPRINT("set_inflight_fd num_queues: %"PRId16
"\n", num_queues
);
1369 DPRINT("set_inflight_fd queue_size: %"PRId16
"\n", queue_size
);
1371 mmap_size
= vu_inflight_queue_size(queue_size
) * num_queues
;
1373 addr
= qemu_memfd_alloc("vhost-inflight", mmap_size
,
1374 F_SEAL_GROW
| F_SEAL_SHRINK
| F_SEAL_SEAL
,
1378 vu_panic(dev
, "Failed to alloc vhost inflight area");
1379 vmsg
->payload
.inflight
.mmap_size
= 0;
1383 memset(addr
, 0, mmap_size
);
1385 dev
->inflight_info
.addr
= addr
;
1386 dev
->inflight_info
.size
= vmsg
->payload
.inflight
.mmap_size
= mmap_size
;
1387 dev
->inflight_info
.fd
= vmsg
->fds
[0] = fd
;
1389 vmsg
->payload
.inflight
.mmap_offset
= 0;
1391 DPRINT("send inflight mmap_size: %"PRId64
"\n",
1392 vmsg
->payload
.inflight
.mmap_size
);
1393 DPRINT("send inflight mmap offset: %"PRId64
"\n",
1394 vmsg
->payload
.inflight
.mmap_offset
);
1400 vu_set_inflight_fd(VuDev
*dev
, VhostUserMsg
*vmsg
)
1403 uint64_t mmap_size
, mmap_offset
;
1404 uint16_t num_queues
, queue_size
;
1407 if (vmsg
->fd_num
!= 1 ||
1408 vmsg
->size
!= sizeof(vmsg
->payload
.inflight
)) {
1409 vu_panic(dev
, "Invalid set_inflight_fd message size:%d fds:%d",
1410 vmsg
->size
, vmsg
->fd_num
);
1415 mmap_size
= vmsg
->payload
.inflight
.mmap_size
;
1416 mmap_offset
= vmsg
->payload
.inflight
.mmap_offset
;
1417 num_queues
= vmsg
->payload
.inflight
.num_queues
;
1418 queue_size
= vmsg
->payload
.inflight
.queue_size
;
1420 DPRINT("set_inflight_fd mmap_size: %"PRId64
"\n", mmap_size
);
1421 DPRINT("set_inflight_fd mmap_offset: %"PRId64
"\n", mmap_offset
);
1422 DPRINT("set_inflight_fd num_queues: %"PRId16
"\n", num_queues
);
1423 DPRINT("set_inflight_fd queue_size: %"PRId16
"\n", queue_size
);
1425 rc
= mmap(0, mmap_size
, PROT_READ
| PROT_WRITE
, MAP_SHARED
,
1428 if (rc
== MAP_FAILED
) {
1429 vu_panic(dev
, "set_inflight_fd mmap error: %s", strerror(errno
));
1433 if (dev
->inflight_info
.fd
) {
1434 close(dev
->inflight_info
.fd
);
1437 if (dev
->inflight_info
.addr
) {
1438 munmap(dev
->inflight_info
.addr
, dev
->inflight_info
.size
);
1441 dev
->inflight_info
.fd
= fd
;
1442 dev
->inflight_info
.addr
= rc
;
1443 dev
->inflight_info
.size
= mmap_size
;
1445 for (i
= 0; i
< num_queues
; i
++) {
1446 dev
->vq
[i
].inflight
= (VuVirtqInflight
*)rc
;
1447 dev
->vq
[i
].inflight
->desc_num
= queue_size
;
1448 rc
= (void *)((char *)rc
+ vu_inflight_queue_size(queue_size
));
1455 vu_process_message(VuDev
*dev
, VhostUserMsg
*vmsg
)
1459 /* Print out generic part of the request. */
1460 DPRINT("================ Vhost user message ================\n");
1461 DPRINT("Request: %s (%d)\n", vu_request_to_string(vmsg
->request
),
1463 DPRINT("Flags: 0x%x\n", vmsg
->flags
);
1464 DPRINT("Size: %d\n", vmsg
->size
);
1469 for (i
= 0; i
< vmsg
->fd_num
; i
++) {
1470 DPRINT(" %d", vmsg
->fds
[i
]);
1475 if (dev
->iface
->process_msg
&&
1476 dev
->iface
->process_msg(dev
, vmsg
, &do_reply
)) {
1480 switch (vmsg
->request
) {
1481 case VHOST_USER_GET_FEATURES
:
1482 return vu_get_features_exec(dev
, vmsg
);
1483 case VHOST_USER_SET_FEATURES
:
1484 return vu_set_features_exec(dev
, vmsg
);
1485 case VHOST_USER_GET_PROTOCOL_FEATURES
:
1486 return vu_get_protocol_features_exec(dev
, vmsg
);
1487 case VHOST_USER_SET_PROTOCOL_FEATURES
:
1488 return vu_set_protocol_features_exec(dev
, vmsg
);
1489 case VHOST_USER_SET_OWNER
:
1490 return vu_set_owner_exec(dev
, vmsg
);
1491 case VHOST_USER_RESET_OWNER
:
1492 return vu_reset_device_exec(dev
, vmsg
);
1493 case VHOST_USER_SET_MEM_TABLE
:
1494 return vu_set_mem_table_exec(dev
, vmsg
);
1495 case VHOST_USER_SET_LOG_BASE
:
1496 return vu_set_log_base_exec(dev
, vmsg
);
1497 case VHOST_USER_SET_LOG_FD
:
1498 return vu_set_log_fd_exec(dev
, vmsg
);
1499 case VHOST_USER_SET_VRING_NUM
:
1500 return vu_set_vring_num_exec(dev
, vmsg
);
1501 case VHOST_USER_SET_VRING_ADDR
:
1502 return vu_set_vring_addr_exec(dev
, vmsg
);
1503 case VHOST_USER_SET_VRING_BASE
:
1504 return vu_set_vring_base_exec(dev
, vmsg
);
1505 case VHOST_USER_GET_VRING_BASE
:
1506 return vu_get_vring_base_exec(dev
, vmsg
);
1507 case VHOST_USER_SET_VRING_KICK
:
1508 return vu_set_vring_kick_exec(dev
, vmsg
);
1509 case VHOST_USER_SET_VRING_CALL
:
1510 return vu_set_vring_call_exec(dev
, vmsg
);
1511 case VHOST_USER_SET_VRING_ERR
:
1512 return vu_set_vring_err_exec(dev
, vmsg
);
1513 case VHOST_USER_GET_QUEUE_NUM
:
1514 return vu_get_queue_num_exec(dev
, vmsg
);
1515 case VHOST_USER_SET_VRING_ENABLE
:
1516 return vu_set_vring_enable_exec(dev
, vmsg
);
1517 case VHOST_USER_SET_SLAVE_REQ_FD
:
1518 return vu_set_slave_req_fd(dev
, vmsg
);
1519 case VHOST_USER_GET_CONFIG
:
1520 return vu_get_config(dev
, vmsg
);
1521 case VHOST_USER_SET_CONFIG
:
1522 return vu_set_config(dev
, vmsg
);
1523 case VHOST_USER_NONE
:
1524 /* if you need processing before exit, override iface->process_msg */
1526 case VHOST_USER_POSTCOPY_ADVISE
:
1527 return vu_set_postcopy_advise(dev
, vmsg
);
1528 case VHOST_USER_POSTCOPY_LISTEN
:
1529 return vu_set_postcopy_listen(dev
, vmsg
);
1530 case VHOST_USER_POSTCOPY_END
:
1531 return vu_set_postcopy_end(dev
, vmsg
);
1532 case VHOST_USER_GET_INFLIGHT_FD
:
1533 return vu_get_inflight_fd(dev
, vmsg
);
1534 case VHOST_USER_SET_INFLIGHT_FD
:
1535 return vu_set_inflight_fd(dev
, vmsg
);
1537 vmsg_close_fds(vmsg
);
1538 vu_panic(dev
, "Unhandled request: %d", vmsg
->request
);
1545 vu_dispatch(VuDev
*dev
)
1547 VhostUserMsg vmsg
= { 0, };
1548 int reply_requested
;
1549 bool success
= false;
1551 if (!vu_message_read(dev
, dev
->sock
, &vmsg
)) {
1555 reply_requested
= vu_process_message(dev
, &vmsg
);
1556 if (!reply_requested
) {
1561 if (!vu_send_reply(dev
, dev
->sock
, &vmsg
)) {
1573 vu_deinit(VuDev
*dev
)
1577 for (i
= 0; i
< dev
->nregions
; i
++) {
1578 VuDevRegion
*r
= &dev
->regions
[i
];
1579 void *m
= (void *) (uintptr_t) r
->mmap_addr
;
1580 if (m
!= MAP_FAILED
) {
1581 munmap(m
, r
->size
+ r
->mmap_offset
);
1586 for (i
= 0; i
< dev
->max_queues
; i
++) {
1587 VuVirtq
*vq
= &dev
->vq
[i
];
1589 if (vq
->call_fd
!= -1) {
1594 if (vq
->kick_fd
!= -1) {
1599 if (vq
->err_fd
!= -1) {
1604 if (vq
->resubmit_list
) {
1605 free(vq
->resubmit_list
);
1606 vq
->resubmit_list
= NULL
;
1609 vq
->inflight
= NULL
;
1612 if (dev
->inflight_info
.addr
) {
1613 munmap(dev
->inflight_info
.addr
, dev
->inflight_info
.size
);
1614 dev
->inflight_info
.addr
= NULL
;
1617 if (dev
->inflight_info
.fd
> 0) {
1618 close(dev
->inflight_info
.fd
);
1619 dev
->inflight_info
.fd
= -1;
1623 if (dev
->slave_fd
!= -1) {
1624 close(dev
->slave_fd
);
1628 if (dev
->sock
!= -1) {
1638 uint16_t max_queues
,
1641 vu_set_watch_cb set_watch
,
1642 vu_remove_watch_cb remove_watch
,
1643 const VuDevIface
*iface
)
1647 assert(max_queues
> 0);
1648 assert(socket
>= 0);
1650 assert(remove_watch
);
1654 memset(dev
, 0, sizeof(*dev
));
1658 dev
->set_watch
= set_watch
;
1659 dev
->remove_watch
= remove_watch
;
1661 dev
->log_call_fd
= -1;
1663 dev
->max_queues
= max_queues
;
1665 dev
->vq
= malloc(max_queues
* sizeof(dev
->vq
[0]));
1667 DPRINT("%s: failed to malloc virtqueues\n", __func__
);
1671 for (i
= 0; i
< max_queues
; i
++) {
1672 dev
->vq
[i
] = (VuVirtq
) {
1673 .call_fd
= -1, .kick_fd
= -1, .err_fd
= -1,
1674 .notification
= true,
1682 vu_get_queue(VuDev
*dev
, int qidx
)
1684 assert(qidx
< dev
->max_queues
);
1685 return &dev
->vq
[qidx
];
1689 vu_queue_enabled(VuDev
*dev
, VuVirtq
*vq
)
1695 vu_queue_started(const VuDev
*dev
, const VuVirtq
*vq
)
1700 static inline uint16_t
1701 vring_avail_flags(VuVirtq
*vq
)
1703 return vq
->vring
.avail
->flags
;
1706 static inline uint16_t
1707 vring_avail_idx(VuVirtq
*vq
)
1709 vq
->shadow_avail_idx
= vq
->vring
.avail
->idx
;
1711 return vq
->shadow_avail_idx
;
1714 static inline uint16_t
1715 vring_avail_ring(VuVirtq
*vq
, int i
)
1717 return vq
->vring
.avail
->ring
[i
];
1720 static inline uint16_t
1721 vring_get_used_event(VuVirtq
*vq
)
1723 return vring_avail_ring(vq
, vq
->vring
.num
);
1727 virtqueue_num_heads(VuDev
*dev
, VuVirtq
*vq
, unsigned int idx
)
1729 uint16_t num_heads
= vring_avail_idx(vq
) - idx
;
1731 /* Check it isn't doing very strange things with descriptor numbers. */
1732 if (num_heads
> vq
->vring
.num
) {
1733 vu_panic(dev
, "Guest moved used index from %u to %u",
1734 idx
, vq
->shadow_avail_idx
);
1738 /* On success, callers read a descriptor at vq->last_avail_idx.
1739 * Make sure descriptor read does not bypass avail index read. */
1747 virtqueue_get_head(VuDev
*dev
, VuVirtq
*vq
,
1748 unsigned int idx
, unsigned int *head
)
1750 /* Grab the next descriptor number they're advertising, and increment
1751 * the index we've seen. */
1752 *head
= vring_avail_ring(vq
, idx
% vq
->vring
.num
);
1754 /* If their number is silly, that's a fatal mistake. */
1755 if (*head
>= vq
->vring
.num
) {
1756 vu_panic(dev
, "Guest says index %u is available", head
);
1764 virtqueue_read_indirect_desc(VuDev
*dev
, struct vring_desc
*desc
,
1765 uint64_t addr
, size_t len
)
1767 struct vring_desc
*ori_desc
;
1770 if (len
> (VIRTQUEUE_MAX_SIZE
* sizeof(struct vring_desc
))) {
1780 ori_desc
= vu_gpa_to_va(dev
, &read_len
, addr
);
1785 memcpy(desc
, ori_desc
, read_len
);
1795 VIRTQUEUE_READ_DESC_ERROR
= -1,
1796 VIRTQUEUE_READ_DESC_DONE
= 0, /* end of chain */
1797 VIRTQUEUE_READ_DESC_MORE
= 1, /* more buffers in chain */
1801 virtqueue_read_next_desc(VuDev
*dev
, struct vring_desc
*desc
,
1802 int i
, unsigned int max
, unsigned int *next
)
1804 /* If this descriptor says it doesn't chain, we're done. */
1805 if (!(desc
[i
].flags
& VRING_DESC_F_NEXT
)) {
1806 return VIRTQUEUE_READ_DESC_DONE
;
1809 /* Check they're not leading us off end of descriptors. */
1810 *next
= desc
[i
].next
;
1811 /* Make sure compiler knows to grab that: we don't want it changing! */
1815 vu_panic(dev
, "Desc next is %u", next
);
1816 return VIRTQUEUE_READ_DESC_ERROR
;
1819 return VIRTQUEUE_READ_DESC_MORE
;
1823 vu_queue_get_avail_bytes(VuDev
*dev
, VuVirtq
*vq
, unsigned int *in_bytes
,
1824 unsigned int *out_bytes
,
1825 unsigned max_in_bytes
, unsigned max_out_bytes
)
1828 unsigned int total_bufs
, in_total
, out_total
;
1831 idx
= vq
->last_avail_idx
;
1833 total_bufs
= in_total
= out_total
= 0;
1834 if (unlikely(dev
->broken
) ||
1835 unlikely(!vq
->vring
.avail
)) {
1839 while ((rc
= virtqueue_num_heads(dev
, vq
, idx
)) > 0) {
1840 unsigned int max
, desc_len
, num_bufs
, indirect
= 0;
1841 uint64_t desc_addr
, read_len
;
1842 struct vring_desc
*desc
;
1843 struct vring_desc desc_buf
[VIRTQUEUE_MAX_SIZE
];
1846 max
= vq
->vring
.num
;
1847 num_bufs
= total_bufs
;
1848 if (!virtqueue_get_head(dev
, vq
, idx
++, &i
)) {
1851 desc
= vq
->vring
.desc
;
1853 if (desc
[i
].flags
& VRING_DESC_F_INDIRECT
) {
1854 if (desc
[i
].len
% sizeof(struct vring_desc
)) {
1855 vu_panic(dev
, "Invalid size for indirect buffer table");
1859 /* If we've got too many, that implies a descriptor loop. */
1860 if (num_bufs
>= max
) {
1861 vu_panic(dev
, "Looped descriptor");
1865 /* loop over the indirect descriptor table */
1867 desc_addr
= desc
[i
].addr
;
1868 desc_len
= desc
[i
].len
;
1869 max
= desc_len
/ sizeof(struct vring_desc
);
1870 read_len
= desc_len
;
1871 desc
= vu_gpa_to_va(dev
, &read_len
, desc_addr
);
1872 if (unlikely(desc
&& read_len
!= desc_len
)) {
1873 /* Failed to use zero copy */
1875 if (!virtqueue_read_indirect_desc(dev
, desc_buf
,
1882 vu_panic(dev
, "Invalid indirect buffer table");
1889 /* If we've got too many, that implies a descriptor loop. */
1890 if (++num_bufs
> max
) {
1891 vu_panic(dev
, "Looped descriptor");
1895 if (desc
[i
].flags
& VRING_DESC_F_WRITE
) {
1896 in_total
+= desc
[i
].len
;
1898 out_total
+= desc
[i
].len
;
1900 if (in_total
>= max_in_bytes
&& out_total
>= max_out_bytes
) {
1903 rc
= virtqueue_read_next_desc(dev
, desc
, i
, max
, &i
);
1904 } while (rc
== VIRTQUEUE_READ_DESC_MORE
);
1906 if (rc
== VIRTQUEUE_READ_DESC_ERROR
) {
1911 total_bufs
= num_bufs
;
1921 *in_bytes
= in_total
;
1924 *out_bytes
= out_total
;
1929 in_total
= out_total
= 0;
1934 vu_queue_avail_bytes(VuDev
*dev
, VuVirtq
*vq
, unsigned int in_bytes
,
1935 unsigned int out_bytes
)
1937 unsigned int in_total
, out_total
;
1939 vu_queue_get_avail_bytes(dev
, vq
, &in_total
, &out_total
,
1940 in_bytes
, out_bytes
);
1942 return in_bytes
<= in_total
&& out_bytes
<= out_total
;
1945 /* Fetch avail_idx from VQ memory only when we really need to know if
1946 * guest has added some buffers. */
1948 vu_queue_empty(VuDev
*dev
, VuVirtq
*vq
)
1950 if (unlikely(dev
->broken
) ||
1951 unlikely(!vq
->vring
.avail
)) {
1955 if (vq
->shadow_avail_idx
!= vq
->last_avail_idx
) {
1959 return vring_avail_idx(vq
) == vq
->last_avail_idx
;
1963 vring_notify(VuDev
*dev
, VuVirtq
*vq
)
1968 /* We need to expose used array entries before checking used event. */
1971 /* Always notify when queue is empty (when feature acknowledge) */
1972 if (vu_has_feature(dev
, VIRTIO_F_NOTIFY_ON_EMPTY
) &&
1973 !vq
->inuse
&& vu_queue_empty(dev
, vq
)) {
1977 if (!vu_has_feature(dev
, VIRTIO_RING_F_EVENT_IDX
)) {
1978 return !(vring_avail_flags(vq
) & VRING_AVAIL_F_NO_INTERRUPT
);
1981 v
= vq
->signalled_used_valid
;
1982 vq
->signalled_used_valid
= true;
1983 old
= vq
->signalled_used
;
1984 new = vq
->signalled_used
= vq
->used_idx
;
1985 return !v
|| vring_need_event(vring_get_used_event(vq
), new, old
);
1989 vu_queue_notify(VuDev
*dev
, VuVirtq
*vq
)
1991 if (unlikely(dev
->broken
) ||
1992 unlikely(!vq
->vring
.avail
)) {
1996 if (!vring_notify(dev
, vq
)) {
1997 DPRINT("skipped notify...\n");
2001 if (eventfd_write(vq
->call_fd
, 1) < 0) {
2002 vu_panic(dev
, "Error writing eventfd: %s", strerror(errno
));
2007 vring_used_flags_set_bit(VuVirtq
*vq
, int mask
)
2011 flags
= (uint16_t *)((char*)vq
->vring
.used
+
2012 offsetof(struct vring_used
, flags
));
2017 vring_used_flags_unset_bit(VuVirtq
*vq
, int mask
)
2021 flags
= (uint16_t *)((char*)vq
->vring
.used
+
2022 offsetof(struct vring_used
, flags
));
2027 vring_set_avail_event(VuVirtq
*vq
, uint16_t val
)
2029 if (!vq
->notification
) {
2033 *((uint16_t *) &vq
->vring
.used
->ring
[vq
->vring
.num
]) = val
;
2037 vu_queue_set_notification(VuDev
*dev
, VuVirtq
*vq
, int enable
)
2039 vq
->notification
= enable
;
2040 if (vu_has_feature(dev
, VIRTIO_RING_F_EVENT_IDX
)) {
2041 vring_set_avail_event(vq
, vring_avail_idx(vq
));
2042 } else if (enable
) {
2043 vring_used_flags_unset_bit(vq
, VRING_USED_F_NO_NOTIFY
);
2045 vring_used_flags_set_bit(vq
, VRING_USED_F_NO_NOTIFY
);
2048 /* Expose avail event/used flags before caller checks the avail idx. */
2054 virtqueue_map_desc(VuDev
*dev
,
2055 unsigned int *p_num_sg
, struct iovec
*iov
,
2056 unsigned int max_num_sg
, bool is_write
,
2057 uint64_t pa
, size_t sz
)
2059 unsigned num_sg
= *p_num_sg
;
2061 assert(num_sg
<= max_num_sg
);
2064 vu_panic(dev
, "virtio: zero sized buffers are not allowed");
2071 if (num_sg
== max_num_sg
) {
2072 vu_panic(dev
, "virtio: too many descriptors in indirect table");
2076 iov
[num_sg
].iov_base
= vu_gpa_to_va(dev
, &len
, pa
);
2077 if (iov
[num_sg
].iov_base
== NULL
) {
2078 vu_panic(dev
, "virtio: invalid address for buffers");
2081 iov
[num_sg
].iov_len
= len
;
2091 virtqueue_alloc_element(size_t sz
,
2092 unsigned out_num
, unsigned in_num
)
2094 VuVirtqElement
*elem
;
2095 size_t in_sg_ofs
= ALIGN_UP(sz
, __alignof__(elem
->in_sg
[0]));
2096 size_t out_sg_ofs
= in_sg_ofs
+ in_num
* sizeof(elem
->in_sg
[0]);
2097 size_t out_sg_end
= out_sg_ofs
+ out_num
* sizeof(elem
->out_sg
[0]);
2099 assert(sz
>= sizeof(VuVirtqElement
));
2100 elem
= malloc(out_sg_end
);
2101 elem
->out_num
= out_num
;
2102 elem
->in_num
= in_num
;
2103 elem
->in_sg
= (void *)elem
+ in_sg_ofs
;
2104 elem
->out_sg
= (void *)elem
+ out_sg_ofs
;
2109 vu_queue_map_desc(VuDev
*dev
, VuVirtq
*vq
, unsigned int idx
, size_t sz
)
2111 struct vring_desc
*desc
= vq
->vring
.desc
;
2112 uint64_t desc_addr
, read_len
;
2113 unsigned int desc_len
;
2114 unsigned int max
= vq
->vring
.num
;
2115 unsigned int i
= idx
;
2116 VuVirtqElement
*elem
;
2117 unsigned int out_num
= 0, in_num
= 0;
2118 struct iovec iov
[VIRTQUEUE_MAX_SIZE
];
2119 struct vring_desc desc_buf
[VIRTQUEUE_MAX_SIZE
];
2122 if (desc
[i
].flags
& VRING_DESC_F_INDIRECT
) {
2123 if (desc
[i
].len
% sizeof(struct vring_desc
)) {
2124 vu_panic(dev
, "Invalid size for indirect buffer table");
2127 /* loop over the indirect descriptor table */
2128 desc_addr
= desc
[i
].addr
;
2129 desc_len
= desc
[i
].len
;
2130 max
= desc_len
/ sizeof(struct vring_desc
);
2131 read_len
= desc_len
;
2132 desc
= vu_gpa_to_va(dev
, &read_len
, desc_addr
);
2133 if (unlikely(desc
&& read_len
!= desc_len
)) {
2134 /* Failed to use zero copy */
2136 if (!virtqueue_read_indirect_desc(dev
, desc_buf
,
2143 vu_panic(dev
, "Invalid indirect buffer table");
2149 /* Collect all the descriptors */
2151 if (desc
[i
].flags
& VRING_DESC_F_WRITE
) {
2152 virtqueue_map_desc(dev
, &in_num
, iov
+ out_num
,
2153 VIRTQUEUE_MAX_SIZE
- out_num
, true,
2154 desc
[i
].addr
, desc
[i
].len
);
2157 vu_panic(dev
, "Incorrect order for descriptors");
2160 virtqueue_map_desc(dev
, &out_num
, iov
,
2161 VIRTQUEUE_MAX_SIZE
, false,
2162 desc
[i
].addr
, desc
[i
].len
);
2165 /* If we've got too many, that implies a descriptor loop. */
2166 if ((in_num
+ out_num
) > max
) {
2167 vu_panic(dev
, "Looped descriptor");
2169 rc
= virtqueue_read_next_desc(dev
, desc
, i
, max
, &i
);
2170 } while (rc
== VIRTQUEUE_READ_DESC_MORE
);
2172 if (rc
== VIRTQUEUE_READ_DESC_ERROR
) {
2173 vu_panic(dev
, "read descriptor error");
2177 /* Now copy what we have collected and mapped */
2178 elem
= virtqueue_alloc_element(sz
, out_num
, in_num
);
2180 for (i
= 0; i
< out_num
; i
++) {
2181 elem
->out_sg
[i
] = iov
[i
];
2183 for (i
= 0; i
< in_num
; i
++) {
2184 elem
->in_sg
[i
] = iov
[out_num
+ i
];
2191 vu_queue_inflight_get(VuDev
*dev
, VuVirtq
*vq
, int desc_idx
)
2193 if (!has_feature(dev
->protocol_features
,
2194 VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD
)) {
2198 if (unlikely(!vq
->inflight
)) {
2202 vq
->inflight
->desc
[desc_idx
].counter
= vq
->counter
++;
2203 vq
->inflight
->desc
[desc_idx
].inflight
= 1;
2209 vu_queue_inflight_pre_put(VuDev
*dev
, VuVirtq
*vq
, int desc_idx
)
2211 if (!has_feature(dev
->protocol_features
,
2212 VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD
)) {
2216 if (unlikely(!vq
->inflight
)) {
2220 vq
->inflight
->last_batch_head
= desc_idx
;
2226 vu_queue_inflight_post_put(VuDev
*dev
, VuVirtq
*vq
, int desc_idx
)
2228 if (!has_feature(dev
->protocol_features
,
2229 VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD
)) {
2233 if (unlikely(!vq
->inflight
)) {
2239 vq
->inflight
->desc
[desc_idx
].inflight
= 0;
2243 vq
->inflight
->used_idx
= vq
->used_idx
;
2249 vu_queue_pop(VuDev
*dev
, VuVirtq
*vq
, size_t sz
)
2253 VuVirtqElement
*elem
;
2255 if (unlikely(dev
->broken
) ||
2256 unlikely(!vq
->vring
.avail
)) {
2260 if (unlikely(vq
->resubmit_list
&& vq
->resubmit_num
> 0)) {
2261 i
= (--vq
->resubmit_num
);
2262 elem
= vu_queue_map_desc(dev
, vq
, vq
->resubmit_list
[i
].index
, sz
);
2264 if (!vq
->resubmit_num
) {
2265 free(vq
->resubmit_list
);
2266 vq
->resubmit_list
= NULL
;
2272 if (vu_queue_empty(dev
, vq
)) {
2276 * Needed after virtio_queue_empty(), see comment in
2277 * virtqueue_num_heads().
2281 if (vq
->inuse
>= vq
->vring
.num
) {
2282 vu_panic(dev
, "Virtqueue size exceeded");
2286 if (!virtqueue_get_head(dev
, vq
, vq
->last_avail_idx
++, &head
)) {
2290 if (vu_has_feature(dev
, VIRTIO_RING_F_EVENT_IDX
)) {
2291 vring_set_avail_event(vq
, vq
->last_avail_idx
);
2294 elem
= vu_queue_map_desc(dev
, vq
, head
, sz
);
2302 vu_queue_inflight_get(dev
, vq
, head
);
2308 vu_queue_detach_element(VuDev
*dev
, VuVirtq
*vq
, VuVirtqElement
*elem
,
2312 /* unmap, when DMA support is added */
2316 vu_queue_unpop(VuDev
*dev
, VuVirtq
*vq
, VuVirtqElement
*elem
,
2319 vq
->last_avail_idx
--;
2320 vu_queue_detach_element(dev
, vq
, elem
, len
);
2324 vu_queue_rewind(VuDev
*dev
, VuVirtq
*vq
, unsigned int num
)
2326 if (num
> vq
->inuse
) {
2329 vq
->last_avail_idx
-= num
;
2335 void vring_used_write(VuDev
*dev
, VuVirtq
*vq
,
2336 struct vring_used_elem
*uelem
, int i
)
2338 struct vring_used
*used
= vq
->vring
.used
;
2340 used
->ring
[i
] = *uelem
;
2341 vu_log_write(dev
, vq
->vring
.log_guest_addr
+
2342 offsetof(struct vring_used
, ring
[i
]),
2343 sizeof(used
->ring
[i
]));
2348 vu_log_queue_fill(VuDev
*dev
, VuVirtq
*vq
,
2349 const VuVirtqElement
*elem
,
2352 struct vring_desc
*desc
= vq
->vring
.desc
;
2353 unsigned int i
, max
, min
, desc_len
;
2354 uint64_t desc_addr
, read_len
;
2355 struct vring_desc desc_buf
[VIRTQUEUE_MAX_SIZE
];
2356 unsigned num_bufs
= 0;
2358 max
= vq
->vring
.num
;
2361 if (desc
[i
].flags
& VRING_DESC_F_INDIRECT
) {
2362 if (desc
[i
].len
% sizeof(struct vring_desc
)) {
2363 vu_panic(dev
, "Invalid size for indirect buffer table");
2366 /* loop over the indirect descriptor table */
2367 desc_addr
= desc
[i
].addr
;
2368 desc_len
= desc
[i
].len
;
2369 max
= desc_len
/ sizeof(struct vring_desc
);
2370 read_len
= desc_len
;
2371 desc
= vu_gpa_to_va(dev
, &read_len
, desc_addr
);
2372 if (unlikely(desc
&& read_len
!= desc_len
)) {
2373 /* Failed to use zero copy */
2375 if (!virtqueue_read_indirect_desc(dev
, desc_buf
,
2382 vu_panic(dev
, "Invalid indirect buffer table");
2389 if (++num_bufs
> max
) {
2390 vu_panic(dev
, "Looped descriptor");
2394 if (desc
[i
].flags
& VRING_DESC_F_WRITE
) {
2395 min
= MIN(desc
[i
].len
, len
);
2396 vu_log_write(dev
, desc
[i
].addr
, min
);
2401 (virtqueue_read_next_desc(dev
, desc
, i
, max
, &i
)
2402 == VIRTQUEUE_READ_DESC_MORE
));
2406 vu_queue_fill(VuDev
*dev
, VuVirtq
*vq
,
2407 const VuVirtqElement
*elem
,
2408 unsigned int len
, unsigned int idx
)
2410 struct vring_used_elem uelem
;
2412 if (unlikely(dev
->broken
) ||
2413 unlikely(!vq
->vring
.avail
)) {
2417 vu_log_queue_fill(dev
, vq
, elem
, len
);
2419 idx
= (idx
+ vq
->used_idx
) % vq
->vring
.num
;
2421 uelem
.id
= elem
->index
;
2423 vring_used_write(dev
, vq
, &uelem
, idx
);
2427 void vring_used_idx_set(VuDev
*dev
, VuVirtq
*vq
, uint16_t val
)
2429 vq
->vring
.used
->idx
= val
;
2431 vq
->vring
.log_guest_addr
+ offsetof(struct vring_used
, idx
),
2432 sizeof(vq
->vring
.used
->idx
));
2438 vu_queue_flush(VuDev
*dev
, VuVirtq
*vq
, unsigned int count
)
2442 if (unlikely(dev
->broken
) ||
2443 unlikely(!vq
->vring
.avail
)) {
2447 /* Make sure buffer is written before we update index. */
2452 vring_used_idx_set(dev
, vq
, new);
2454 if (unlikely((int16_t)(new - vq
->signalled_used
) < (uint16_t)(new - old
))) {
2455 vq
->signalled_used_valid
= false;
2460 vu_queue_push(VuDev
*dev
, VuVirtq
*vq
,
2461 const VuVirtqElement
*elem
, unsigned int len
)
2463 vu_queue_fill(dev
, vq
, elem
, len
, 0);
2464 vu_queue_inflight_pre_put(dev
, vq
, elem
->index
);
2465 vu_queue_flush(dev
, vq
, 1);
2466 vu_queue_inflight_post_put(dev
, vq
, elem
->index
);