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
),
137 if (req
< VHOST_USER_MAX
) {
138 return vu_request_str
[req
];
145 vu_panic(VuDev
*dev
, const char *msg
, ...)
151 if (vasprintf(&buf
, msg
, ap
) < 0) {
157 dev
->panic(dev
, buf
);
160 /* FIXME: find a way to call virtio_error? */
163 /* Translate guest physical address to our virtual address. */
165 vu_gpa_to_va(VuDev
*dev
, uint64_t *plen
, uint64_t guest_addr
)
173 /* Find matching memory region. */
174 for (i
= 0; i
< dev
->nregions
; i
++) {
175 VuDevRegion
*r
= &dev
->regions
[i
];
177 if ((guest_addr
>= r
->gpa
) && (guest_addr
< (r
->gpa
+ r
->size
))) {
178 if ((guest_addr
+ *plen
) > (r
->gpa
+ r
->size
)) {
179 *plen
= r
->gpa
+ r
->size
- guest_addr
;
181 return (void *)(uintptr_t)
182 guest_addr
- r
->gpa
+ r
->mmap_addr
+ r
->mmap_offset
;
189 /* Translate qemu virtual address to our virtual address. */
191 qva_to_va(VuDev
*dev
, uint64_t qemu_addr
)
195 /* Find matching memory region. */
196 for (i
= 0; i
< dev
->nregions
; i
++) {
197 VuDevRegion
*r
= &dev
->regions
[i
];
199 if ((qemu_addr
>= r
->qva
) && (qemu_addr
< (r
->qva
+ r
->size
))) {
200 return (void *)(uintptr_t)
201 qemu_addr
- r
->qva
+ r
->mmap_addr
+ r
->mmap_offset
;
209 vmsg_close_fds(VhostUserMsg
*vmsg
)
213 for (i
= 0; i
< vmsg
->fd_num
; i
++) {
218 /* A test to see if we have userfault available */
222 #if defined(__linux__) && defined(__NR_userfaultfd) &&\
223 defined(UFFD_FEATURE_MISSING_SHMEM) &&\
224 defined(UFFD_FEATURE_MISSING_HUGETLBFS)
225 /* Now test the kernel we're running on really has the features */
226 int ufd
= syscall(__NR_userfaultfd
, O_CLOEXEC
| O_NONBLOCK
);
227 struct uffdio_api api_struct
;
232 api_struct
.api
= UFFD_API
;
233 api_struct
.features
= UFFD_FEATURE_MISSING_SHMEM
|
234 UFFD_FEATURE_MISSING_HUGETLBFS
;
235 if (ioctl(ufd
, UFFDIO_API
, &api_struct
)) {
248 vu_message_read(VuDev
*dev
, int conn_fd
, VhostUserMsg
*vmsg
)
250 char control
[CMSG_SPACE(VHOST_MEMORY_MAX_NREGIONS
* sizeof(int))] = { };
252 .iov_base
= (char *)vmsg
,
253 .iov_len
= VHOST_USER_HDR_SIZE
,
255 struct msghdr msg
= {
258 .msg_control
= control
,
259 .msg_controllen
= sizeof(control
),
262 struct cmsghdr
*cmsg
;
266 rc
= recvmsg(conn_fd
, &msg
, 0);
267 } while (rc
< 0 && (errno
== EINTR
|| errno
== EAGAIN
));
270 vu_panic(dev
, "Error while recvmsg: %s", strerror(errno
));
275 for (cmsg
= CMSG_FIRSTHDR(&msg
);
277 cmsg
= CMSG_NXTHDR(&msg
, cmsg
))
279 if (cmsg
->cmsg_level
== SOL_SOCKET
&& cmsg
->cmsg_type
== SCM_RIGHTS
) {
280 fd_size
= cmsg
->cmsg_len
- CMSG_LEN(0);
281 vmsg
->fd_num
= fd_size
/ sizeof(int);
282 memcpy(vmsg
->fds
, CMSG_DATA(cmsg
), fd_size
);
287 if (vmsg
->size
> sizeof(vmsg
->payload
)) {
289 "Error: too big message request: %d, size: vmsg->size: %u, "
290 "while sizeof(vmsg->payload) = %zu\n",
291 vmsg
->request
, vmsg
->size
, sizeof(vmsg
->payload
));
297 rc
= read(conn_fd
, &vmsg
->payload
, vmsg
->size
);
298 } while (rc
< 0 && (errno
== EINTR
|| errno
== EAGAIN
));
301 vu_panic(dev
, "Error while reading: %s", strerror(errno
));
305 assert(rc
== vmsg
->size
);
311 vmsg_close_fds(vmsg
);
317 vu_message_write(VuDev
*dev
, int conn_fd
, VhostUserMsg
*vmsg
)
320 uint8_t *p
= (uint8_t *)vmsg
;
321 char control
[CMSG_SPACE(VHOST_MEMORY_MAX_NREGIONS
* sizeof(int))] = { };
323 .iov_base
= (char *)vmsg
,
324 .iov_len
= VHOST_USER_HDR_SIZE
,
326 struct msghdr msg
= {
329 .msg_control
= control
,
331 struct cmsghdr
*cmsg
;
333 memset(control
, 0, sizeof(control
));
334 assert(vmsg
->fd_num
<= VHOST_MEMORY_MAX_NREGIONS
);
335 if (vmsg
->fd_num
> 0) {
336 size_t fdsize
= vmsg
->fd_num
* sizeof(int);
337 msg
.msg_controllen
= CMSG_SPACE(fdsize
);
338 cmsg
= CMSG_FIRSTHDR(&msg
);
339 cmsg
->cmsg_len
= CMSG_LEN(fdsize
);
340 cmsg
->cmsg_level
= SOL_SOCKET
;
341 cmsg
->cmsg_type
= SCM_RIGHTS
;
342 memcpy(CMSG_DATA(cmsg
), vmsg
->fds
, fdsize
);
344 msg
.msg_controllen
= 0;
348 rc
= sendmsg(conn_fd
, &msg
, 0);
349 } while (rc
< 0 && (errno
== EINTR
|| errno
== EAGAIN
));
354 rc
= write(conn_fd
, vmsg
->data
, vmsg
->size
);
356 rc
= write(conn_fd
, p
+ VHOST_USER_HDR_SIZE
, vmsg
->size
);
358 } while (rc
< 0 && (errno
== EINTR
|| errno
== EAGAIN
));
362 vu_panic(dev
, "Error while writing: %s", strerror(errno
));
370 vu_send_reply(VuDev
*dev
, int conn_fd
, VhostUserMsg
*vmsg
)
372 /* Set the version in the flags when sending the reply */
373 vmsg
->flags
&= ~VHOST_USER_VERSION_MASK
;
374 vmsg
->flags
|= VHOST_USER_VERSION
;
375 vmsg
->flags
|= VHOST_USER_REPLY_MASK
;
377 return vu_message_write(dev
, conn_fd
, vmsg
);
381 vu_process_message_reply(VuDev
*dev
, const VhostUserMsg
*vmsg
)
383 VhostUserMsg msg_reply
;
385 if ((vmsg
->flags
& VHOST_USER_NEED_REPLY_MASK
) == 0) {
389 if (!vu_message_read(dev
, dev
->slave_fd
, &msg_reply
)) {
393 if (msg_reply
.request
!= vmsg
->request
) {
394 DPRINT("Received unexpected msg type. Expected %d received %d",
395 vmsg
->request
, msg_reply
.request
);
399 return msg_reply
.payload
.u64
== 0;
402 /* Kick the log_call_fd if required. */
404 vu_log_kick(VuDev
*dev
)
406 if (dev
->log_call_fd
!= -1) {
407 DPRINT("Kicking the QEMU's log...\n");
408 if (eventfd_write(dev
->log_call_fd
, 1) < 0) {
409 vu_panic(dev
, "Error writing eventfd: %s", strerror(errno
));
415 vu_log_page(uint8_t *log_table
, uint64_t page
)
417 DPRINT("Logged dirty guest page: %"PRId64
"\n", page
);
418 atomic_or(&log_table
[page
/ 8], 1 << (page
% 8));
422 vu_log_write(VuDev
*dev
, uint64_t address
, uint64_t length
)
426 if (!(dev
->features
& (1ULL << VHOST_F_LOG_ALL
)) ||
427 !dev
->log_table
|| !length
) {
431 assert(dev
->log_size
> ((address
+ length
- 1) / VHOST_LOG_PAGE
/ 8));
433 page
= address
/ VHOST_LOG_PAGE
;
434 while (page
* VHOST_LOG_PAGE
< address
+ length
) {
435 vu_log_page(dev
->log_table
, page
);
436 page
+= VHOST_LOG_PAGE
;
443 vu_kick_cb(VuDev
*dev
, int condition
, void *data
)
445 int index
= (intptr_t)data
;
446 VuVirtq
*vq
= &dev
->vq
[index
];
447 int sock
= vq
->kick_fd
;
451 rc
= eventfd_read(sock
, &kick_data
);
453 vu_panic(dev
, "kick eventfd_read(): %s", strerror(errno
));
454 dev
->remove_watch(dev
, dev
->vq
[index
].kick_fd
);
456 DPRINT("Got kick_data: %016"PRIx64
" handler:%p idx:%d\n",
457 kick_data
, vq
->handler
, index
);
459 vq
->handler(dev
, index
);
465 vu_get_features_exec(VuDev
*dev
, VhostUserMsg
*vmsg
)
468 1ULL << VHOST_F_LOG_ALL
|
469 1ULL << VHOST_USER_F_PROTOCOL_FEATURES
;
471 if (dev
->iface
->get_features
) {
472 vmsg
->payload
.u64
|= dev
->iface
->get_features(dev
);
475 vmsg
->size
= sizeof(vmsg
->payload
.u64
);
478 DPRINT("Sending back to guest u64: 0x%016"PRIx64
"\n", vmsg
->payload
.u64
);
484 vu_set_enable_all_rings(VuDev
*dev
, bool enabled
)
488 for (i
= 0; i
< VHOST_MAX_NR_VIRTQUEUE
; i
++) {
489 dev
->vq
[i
].enable
= enabled
;
494 vu_set_features_exec(VuDev
*dev
, VhostUserMsg
*vmsg
)
496 DPRINT("u64: 0x%016"PRIx64
"\n", vmsg
->payload
.u64
);
498 dev
->features
= vmsg
->payload
.u64
;
500 if (!(dev
->features
& VHOST_USER_F_PROTOCOL_FEATURES
)) {
501 vu_set_enable_all_rings(dev
, true);
504 if (dev
->iface
->set_features
) {
505 dev
->iface
->set_features(dev
, dev
->features
);
512 vu_set_owner_exec(VuDev
*dev
, VhostUserMsg
*vmsg
)
518 vu_close_log(VuDev
*dev
)
520 if (dev
->log_table
) {
521 if (munmap(dev
->log_table
, dev
->log_size
) != 0) {
522 perror("close log munmap() error");
525 dev
->log_table
= NULL
;
527 if (dev
->log_call_fd
!= -1) {
528 close(dev
->log_call_fd
);
529 dev
->log_call_fd
= -1;
534 vu_reset_device_exec(VuDev
*dev
, VhostUserMsg
*vmsg
)
536 vu_set_enable_all_rings(dev
, false);
542 vu_set_mem_table_exec_postcopy(VuDev
*dev
, VhostUserMsg
*vmsg
)
545 VhostUserMemory m
= vmsg
->payload
.memory
, *memory
= &m
;
546 dev
->nregions
= memory
->nregions
;
548 DPRINT("Nregions: %d\n", memory
->nregions
);
549 for (i
= 0; i
< dev
->nregions
; i
++) {
551 VhostUserMemoryRegion
*msg_region
= &memory
->regions
[i
];
552 VuDevRegion
*dev_region
= &dev
->regions
[i
];
554 DPRINT("Region %d\n", i
);
555 DPRINT(" guest_phys_addr: 0x%016"PRIx64
"\n",
556 msg_region
->guest_phys_addr
);
557 DPRINT(" memory_size: 0x%016"PRIx64
"\n",
558 msg_region
->memory_size
);
559 DPRINT(" userspace_addr 0x%016"PRIx64
"\n",
560 msg_region
->userspace_addr
);
561 DPRINT(" mmap_offset 0x%016"PRIx64
"\n",
562 msg_region
->mmap_offset
);
564 dev_region
->gpa
= msg_region
->guest_phys_addr
;
565 dev_region
->size
= msg_region
->memory_size
;
566 dev_region
->qva
= msg_region
->userspace_addr
;
567 dev_region
->mmap_offset
= msg_region
->mmap_offset
;
569 /* We don't use offset argument of mmap() since the
570 * mapped address has to be page aligned, and we use huge
572 * In postcopy we're using PROT_NONE here to catch anyone
573 * accessing it before we userfault
575 mmap_addr
= mmap(0, dev_region
->size
+ dev_region
->mmap_offset
,
576 PROT_NONE
, MAP_SHARED
,
579 if (mmap_addr
== MAP_FAILED
) {
580 vu_panic(dev
, "region mmap error: %s", strerror(errno
));
582 dev_region
->mmap_addr
= (uint64_t)(uintptr_t)mmap_addr
;
583 DPRINT(" mmap_addr: 0x%016"PRIx64
"\n",
584 dev_region
->mmap_addr
);
587 /* Return the address to QEMU so that it can translate the ufd
588 * fault addresses back.
590 msg_region
->userspace_addr
= (uintptr_t)(mmap_addr
+
591 dev_region
->mmap_offset
);
595 /* Send the message back to qemu with the addresses filled in */
597 if (!vu_send_reply(dev
, dev
->sock
, vmsg
)) {
598 vu_panic(dev
, "failed to respond to set-mem-table for postcopy");
602 /* Wait for QEMU to confirm that it's registered the handler for the
605 if (!vu_message_read(dev
, dev
->sock
, vmsg
) ||
606 vmsg
->size
!= sizeof(vmsg
->payload
.u64
) ||
607 vmsg
->payload
.u64
!= 0) {
608 vu_panic(dev
, "failed to receive valid ack for postcopy set-mem-table");
612 /* OK, now we can go and register the memory and generate faults */
613 for (i
= 0; i
< dev
->nregions
; i
++) {
614 VuDevRegion
*dev_region
= &dev
->regions
[i
];
616 #ifdef UFFDIO_REGISTER
617 /* We should already have an open ufd. Mark each memory
619 * Discard any mapping we have here; note I can't use MADV_REMOVE
620 * or fallocate to make the hole since I don't want to lose
621 * data that's already arrived in the shared process.
622 * TODO: How to do hugepage
624 ret
= madvise((void *)dev_region
->mmap_addr
,
625 dev_region
->size
+ dev_region
->mmap_offset
,
629 "%s: Failed to madvise(DONTNEED) region %d: %s\n",
630 __func__
, i
, strerror(errno
));
632 /* Turn off transparent hugepages so we dont get lose wakeups
633 * in neighbouring pages.
634 * TODO: Turn this backon later.
636 ret
= madvise((void *)dev_region
->mmap_addr
,
637 dev_region
->size
+ dev_region
->mmap_offset
,
640 /* Note: This can happen legally on kernels that are configured
641 * without madvise'able hugepages
644 "%s: Failed to madvise(NOHUGEPAGE) region %d: %s\n",
645 __func__
, i
, strerror(errno
));
647 struct uffdio_register reg_struct
;
648 reg_struct
.range
.start
= (uintptr_t)dev_region
->mmap_addr
;
649 reg_struct
.range
.len
= dev_region
->size
+ dev_region
->mmap_offset
;
650 reg_struct
.mode
= UFFDIO_REGISTER_MODE_MISSING
;
652 if (ioctl(dev
->postcopy_ufd
, UFFDIO_REGISTER
, ®_struct
)) {
653 vu_panic(dev
, "%s: Failed to userfault region %d "
654 "@%p + size:%zx offset: %zx: (ufd=%d)%s\n",
656 dev_region
->mmap_addr
,
657 dev_region
->size
, dev_region
->mmap_offset
,
658 dev
->postcopy_ufd
, strerror(errno
));
661 if (!(reg_struct
.ioctls
& ((__u64
)1 << _UFFDIO_COPY
))) {
662 vu_panic(dev
, "%s Region (%d) doesn't support COPY",
666 DPRINT("%s: region %d: Registered userfault for %llx + %llx\n",
667 __func__
, i
, reg_struct
.range
.start
, reg_struct
.range
.len
);
668 /* Now it's registered we can let the client at it */
669 if (mprotect((void *)dev_region
->mmap_addr
,
670 dev_region
->size
+ dev_region
->mmap_offset
,
671 PROT_READ
| PROT_WRITE
)) {
672 vu_panic(dev
, "failed to mprotect region %d for postcopy (%s)",
676 /* TODO: Stash 'zero' support flags somewhere */
684 vu_set_mem_table_exec(VuDev
*dev
, VhostUserMsg
*vmsg
)
687 VhostUserMemory m
= vmsg
->payload
.memory
, *memory
= &m
;
689 for (i
= 0; i
< dev
->nregions
; i
++) {
690 VuDevRegion
*r
= &dev
->regions
[i
];
691 void *m
= (void *) (uintptr_t) r
->mmap_addr
;
694 munmap(m
, r
->size
+ r
->mmap_offset
);
697 dev
->nregions
= memory
->nregions
;
699 if (dev
->postcopy_listening
) {
700 return vu_set_mem_table_exec_postcopy(dev
, vmsg
);
703 DPRINT("Nregions: %d\n", memory
->nregions
);
704 for (i
= 0; i
< dev
->nregions
; i
++) {
706 VhostUserMemoryRegion
*msg_region
= &memory
->regions
[i
];
707 VuDevRegion
*dev_region
= &dev
->regions
[i
];
709 DPRINT("Region %d\n", i
);
710 DPRINT(" guest_phys_addr: 0x%016"PRIx64
"\n",
711 msg_region
->guest_phys_addr
);
712 DPRINT(" memory_size: 0x%016"PRIx64
"\n",
713 msg_region
->memory_size
);
714 DPRINT(" userspace_addr 0x%016"PRIx64
"\n",
715 msg_region
->userspace_addr
);
716 DPRINT(" mmap_offset 0x%016"PRIx64
"\n",
717 msg_region
->mmap_offset
);
719 dev_region
->gpa
= msg_region
->guest_phys_addr
;
720 dev_region
->size
= msg_region
->memory_size
;
721 dev_region
->qva
= msg_region
->userspace_addr
;
722 dev_region
->mmap_offset
= msg_region
->mmap_offset
;
724 /* We don't use offset argument of mmap() since the
725 * mapped address has to be page aligned, and we use huge
727 mmap_addr
= mmap(0, dev_region
->size
+ dev_region
->mmap_offset
,
728 PROT_READ
| PROT_WRITE
, MAP_SHARED
,
731 if (mmap_addr
== MAP_FAILED
) {
732 vu_panic(dev
, "region mmap error: %s", strerror(errno
));
734 dev_region
->mmap_addr
= (uint64_t)(uintptr_t)mmap_addr
;
735 DPRINT(" mmap_addr: 0x%016"PRIx64
"\n",
736 dev_region
->mmap_addr
);
746 vu_set_log_base_exec(VuDev
*dev
, VhostUserMsg
*vmsg
)
749 uint64_t log_mmap_size
, log_mmap_offset
;
752 if (vmsg
->fd_num
!= 1 ||
753 vmsg
->size
!= sizeof(vmsg
->payload
.log
)) {
754 vu_panic(dev
, "Invalid log_base message");
759 log_mmap_offset
= vmsg
->payload
.log
.mmap_offset
;
760 log_mmap_size
= vmsg
->payload
.log
.mmap_size
;
761 DPRINT("Log mmap_offset: %"PRId64
"\n", log_mmap_offset
);
762 DPRINT("Log mmap_size: %"PRId64
"\n", log_mmap_size
);
764 rc
= mmap(0, log_mmap_size
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, fd
,
767 if (rc
== MAP_FAILED
) {
768 perror("log mmap error");
771 if (dev
->log_table
) {
772 munmap(dev
->log_table
, dev
->log_size
);
775 dev
->log_size
= log_mmap_size
;
777 vmsg
->size
= sizeof(vmsg
->payload
.u64
);
784 vu_set_log_fd_exec(VuDev
*dev
, VhostUserMsg
*vmsg
)
786 if (vmsg
->fd_num
!= 1) {
787 vu_panic(dev
, "Invalid log_fd message");
791 if (dev
->log_call_fd
!= -1) {
792 close(dev
->log_call_fd
);
794 dev
->log_call_fd
= vmsg
->fds
[0];
795 DPRINT("Got log_call_fd: %d\n", vmsg
->fds
[0]);
801 vu_set_vring_num_exec(VuDev
*dev
, VhostUserMsg
*vmsg
)
803 unsigned int index
= vmsg
->payload
.state
.index
;
804 unsigned int num
= vmsg
->payload
.state
.num
;
806 DPRINT("State.index: %d\n", index
);
807 DPRINT("State.num: %d\n", num
);
808 dev
->vq
[index
].vring
.num
= num
;
814 vu_set_vring_addr_exec(VuDev
*dev
, VhostUserMsg
*vmsg
)
816 struct vhost_vring_addr addr
= vmsg
->payload
.addr
, *vra
= &addr
;
817 unsigned int index
= vra
->index
;
818 VuVirtq
*vq
= &dev
->vq
[index
];
820 DPRINT("vhost_vring_addr:\n");
821 DPRINT(" index: %d\n", vra
->index
);
822 DPRINT(" flags: %d\n", vra
->flags
);
823 DPRINT(" desc_user_addr: 0x%016" PRIx64
"\n", vra
->desc_user_addr
);
824 DPRINT(" used_user_addr: 0x%016" PRIx64
"\n", vra
->used_user_addr
);
825 DPRINT(" avail_user_addr: 0x%016" PRIx64
"\n", vra
->avail_user_addr
);
826 DPRINT(" log_guest_addr: 0x%016" PRIx64
"\n", vra
->log_guest_addr
);
828 vq
->vring
.flags
= vra
->flags
;
829 vq
->vring
.desc
= qva_to_va(dev
, vra
->desc_user_addr
);
830 vq
->vring
.used
= qva_to_va(dev
, vra
->used_user_addr
);
831 vq
->vring
.avail
= qva_to_va(dev
, vra
->avail_user_addr
);
832 vq
->vring
.log_guest_addr
= vra
->log_guest_addr
;
834 DPRINT("Setting virtq addresses:\n");
835 DPRINT(" vring_desc at %p\n", vq
->vring
.desc
);
836 DPRINT(" vring_used at %p\n", vq
->vring
.used
);
837 DPRINT(" vring_avail at %p\n", vq
->vring
.avail
);
839 if (!(vq
->vring
.desc
&& vq
->vring
.used
&& vq
->vring
.avail
)) {
840 vu_panic(dev
, "Invalid vring_addr message");
844 vq
->used_idx
= vq
->vring
.used
->idx
;
846 if (vq
->last_avail_idx
!= vq
->used_idx
) {
847 bool resume
= dev
->iface
->queue_is_processed_in_order
&&
848 dev
->iface
->queue_is_processed_in_order(dev
, index
);
850 DPRINT("Last avail index != used index: %u != %u%s\n",
851 vq
->last_avail_idx
, vq
->used_idx
,
852 resume
? ", resuming" : "");
855 vq
->shadow_avail_idx
= vq
->last_avail_idx
= vq
->used_idx
;
863 vu_set_vring_base_exec(VuDev
*dev
, VhostUserMsg
*vmsg
)
865 unsigned int index
= vmsg
->payload
.state
.index
;
866 unsigned int num
= vmsg
->payload
.state
.num
;
868 DPRINT("State.index: %d\n", index
);
869 DPRINT("State.num: %d\n", num
);
870 dev
->vq
[index
].shadow_avail_idx
= dev
->vq
[index
].last_avail_idx
= num
;
876 vu_get_vring_base_exec(VuDev
*dev
, VhostUserMsg
*vmsg
)
878 unsigned int index
= vmsg
->payload
.state
.index
;
880 DPRINT("State.index: %d\n", index
);
881 vmsg
->payload
.state
.num
= dev
->vq
[index
].last_avail_idx
;
882 vmsg
->size
= sizeof(vmsg
->payload
.state
);
884 dev
->vq
[index
].started
= false;
885 if (dev
->iface
->queue_set_started
) {
886 dev
->iface
->queue_set_started(dev
, index
, false);
889 if (dev
->vq
[index
].call_fd
!= -1) {
890 close(dev
->vq
[index
].call_fd
);
891 dev
->vq
[index
].call_fd
= -1;
893 if (dev
->vq
[index
].kick_fd
!= -1) {
894 dev
->remove_watch(dev
, dev
->vq
[index
].kick_fd
);
895 close(dev
->vq
[index
].kick_fd
);
896 dev
->vq
[index
].kick_fd
= -1;
903 vu_check_queue_msg_file(VuDev
*dev
, VhostUserMsg
*vmsg
)
905 int index
= vmsg
->payload
.u64
& VHOST_USER_VRING_IDX_MASK
;
907 if (index
>= VHOST_MAX_NR_VIRTQUEUE
) {
908 vmsg_close_fds(vmsg
);
909 vu_panic(dev
, "Invalid queue index: %u", index
);
913 if (vmsg
->payload
.u64
& VHOST_USER_VRING_NOFD_MASK
||
915 vmsg_close_fds(vmsg
);
916 vu_panic(dev
, "Invalid fds in request: %d", vmsg
->request
);
924 inflight_desc_compare(const void *a
, const void *b
)
926 VuVirtqInflightDesc
*desc0
= (VuVirtqInflightDesc
*)a
,
927 *desc1
= (VuVirtqInflightDesc
*)b
;
929 if (desc1
->counter
> desc0
->counter
&&
930 (desc1
->counter
- desc0
->counter
) < VIRTQUEUE_MAX_SIZE
* 2) {
938 vu_check_queue_inflights(VuDev
*dev
, VuVirtq
*vq
)
942 if (!has_feature(dev
->protocol_features
,
943 VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD
)) {
947 if (unlikely(!vq
->inflight
)) {
951 if (unlikely(!vq
->inflight
->version
)) {
952 /* initialize the buffer */
953 vq
->inflight
->version
= INFLIGHT_VERSION
;
957 vq
->used_idx
= vq
->vring
.used
->idx
;
958 vq
->resubmit_num
= 0;
959 vq
->resubmit_list
= NULL
;
962 if (unlikely(vq
->inflight
->used_idx
!= vq
->used_idx
)) {
963 vq
->inflight
->desc
[vq
->inflight
->last_batch_head
].inflight
= 0;
967 vq
->inflight
->used_idx
= vq
->used_idx
;
970 for (i
= 0; i
< vq
->inflight
->desc_num
; i
++) {
971 if (vq
->inflight
->desc
[i
].inflight
== 1) {
976 vq
->shadow_avail_idx
= vq
->last_avail_idx
= vq
->inuse
+ vq
->used_idx
;
979 vq
->resubmit_list
= malloc(sizeof(VuVirtqInflightDesc
) * vq
->inuse
);
980 if (!vq
->resubmit_list
) {
984 for (i
= 0; i
< vq
->inflight
->desc_num
; i
++) {
985 if (vq
->inflight
->desc
[i
].inflight
) {
986 vq
->resubmit_list
[vq
->resubmit_num
].index
= i
;
987 vq
->resubmit_list
[vq
->resubmit_num
].counter
=
988 vq
->inflight
->desc
[i
].counter
;
993 if (vq
->resubmit_num
> 1) {
994 qsort(vq
->resubmit_list
, vq
->resubmit_num
,
995 sizeof(VuVirtqInflightDesc
), inflight_desc_compare
);
997 vq
->counter
= vq
->resubmit_list
[0].counter
+ 1;
1000 /* in case of I/O hang after reconnecting */
1001 if (eventfd_write(vq
->kick_fd
, 1)) {
1009 vu_set_vring_kick_exec(VuDev
*dev
, VhostUserMsg
*vmsg
)
1011 int index
= vmsg
->payload
.u64
& VHOST_USER_VRING_IDX_MASK
;
1013 DPRINT("u64: 0x%016"PRIx64
"\n", vmsg
->payload
.u64
);
1015 if (!vu_check_queue_msg_file(dev
, vmsg
)) {
1019 if (dev
->vq
[index
].kick_fd
!= -1) {
1020 dev
->remove_watch(dev
, dev
->vq
[index
].kick_fd
);
1021 close(dev
->vq
[index
].kick_fd
);
1022 dev
->vq
[index
].kick_fd
= -1;
1025 dev
->vq
[index
].kick_fd
= vmsg
->fds
[0];
1026 DPRINT("Got kick_fd: %d for vq: %d\n", vmsg
->fds
[0], index
);
1028 dev
->vq
[index
].started
= true;
1029 if (dev
->iface
->queue_set_started
) {
1030 dev
->iface
->queue_set_started(dev
, index
, true);
1033 if (dev
->vq
[index
].kick_fd
!= -1 && dev
->vq
[index
].handler
) {
1034 dev
->set_watch(dev
, dev
->vq
[index
].kick_fd
, VU_WATCH_IN
,
1035 vu_kick_cb
, (void *)(long)index
);
1037 DPRINT("Waiting for kicks on fd: %d for vq: %d\n",
1038 dev
->vq
[index
].kick_fd
, index
);
1041 if (vu_check_queue_inflights(dev
, &dev
->vq
[index
])) {
1042 vu_panic(dev
, "Failed to check inflights for vq: %d\n", index
);
1048 void vu_set_queue_handler(VuDev
*dev
, VuVirtq
*vq
,
1049 vu_queue_handler_cb handler
)
1051 int qidx
= vq
- dev
->vq
;
1053 vq
->handler
= handler
;
1054 if (vq
->kick_fd
>= 0) {
1056 dev
->set_watch(dev
, vq
->kick_fd
, VU_WATCH_IN
,
1057 vu_kick_cb
, (void *)(long)qidx
);
1059 dev
->remove_watch(dev
, vq
->kick_fd
);
1064 bool vu_set_queue_host_notifier(VuDev
*dev
, VuVirtq
*vq
, int fd
,
1065 int size
, int offset
)
1067 int qidx
= vq
- dev
->vq
;
1069 VhostUserMsg vmsg
= {
1070 .request
= VHOST_USER_SLAVE_VRING_HOST_NOTIFIER_MSG
,
1071 .flags
= VHOST_USER_VERSION
| VHOST_USER_NEED_REPLY_MASK
,
1072 .size
= sizeof(vmsg
.payload
.area
),
1074 .u64
= qidx
& VHOST_USER_VRING_IDX_MASK
,
1081 vmsg
.payload
.area
.u64
|= VHOST_USER_VRING_NOFD_MASK
;
1083 vmsg
.fds
[fd_num
++] = fd
;
1086 vmsg
.fd_num
= fd_num
;
1088 if ((dev
->protocol_features
& VHOST_USER_PROTOCOL_F_SLAVE_SEND_FD
) == 0) {
1092 if (!vu_message_write(dev
, dev
->slave_fd
, &vmsg
)) {
1096 return vu_process_message_reply(dev
, &vmsg
);
1100 vu_set_vring_call_exec(VuDev
*dev
, VhostUserMsg
*vmsg
)
1102 int index
= vmsg
->payload
.u64
& VHOST_USER_VRING_IDX_MASK
;
1104 DPRINT("u64: 0x%016"PRIx64
"\n", vmsg
->payload
.u64
);
1106 if (!vu_check_queue_msg_file(dev
, vmsg
)) {
1110 if (dev
->vq
[index
].call_fd
!= -1) {
1111 close(dev
->vq
[index
].call_fd
);
1112 dev
->vq
[index
].call_fd
= -1;
1115 dev
->vq
[index
].call_fd
= vmsg
->fds
[0];
1117 /* in case of I/O hang after reconnecting */
1118 if (eventfd_write(vmsg
->fds
[0], 1)) {
1122 DPRINT("Got call_fd: %d for vq: %d\n", vmsg
->fds
[0], index
);
1128 vu_set_vring_err_exec(VuDev
*dev
, VhostUserMsg
*vmsg
)
1130 int index
= vmsg
->payload
.u64
& VHOST_USER_VRING_IDX_MASK
;
1132 DPRINT("u64: 0x%016"PRIx64
"\n", vmsg
->payload
.u64
);
1134 if (!vu_check_queue_msg_file(dev
, vmsg
)) {
1138 if (dev
->vq
[index
].err_fd
!= -1) {
1139 close(dev
->vq
[index
].err_fd
);
1140 dev
->vq
[index
].err_fd
= -1;
1143 dev
->vq
[index
].err_fd
= vmsg
->fds
[0];
1149 vu_get_protocol_features_exec(VuDev
*dev
, VhostUserMsg
*vmsg
)
1151 uint64_t features
= 1ULL << VHOST_USER_PROTOCOL_F_LOG_SHMFD
|
1152 1ULL << VHOST_USER_PROTOCOL_F_SLAVE_REQ
|
1153 1ULL << VHOST_USER_PROTOCOL_F_HOST_NOTIFIER
|
1154 1ULL << VHOST_USER_PROTOCOL_F_SLAVE_SEND_FD
;
1156 if (have_userfault()) {
1157 features
|= 1ULL << VHOST_USER_PROTOCOL_F_PAGEFAULT
;
1160 if (dev
->iface
->get_config
&& dev
->iface
->set_config
) {
1161 features
|= 1ULL << VHOST_USER_PROTOCOL_F_CONFIG
;
1164 if (dev
->iface
->get_protocol_features
) {
1165 features
|= dev
->iface
->get_protocol_features(dev
);
1168 vmsg
->payload
.u64
= features
;
1169 vmsg
->size
= sizeof(vmsg
->payload
.u64
);
1176 vu_set_protocol_features_exec(VuDev
*dev
, VhostUserMsg
*vmsg
)
1178 uint64_t features
= vmsg
->payload
.u64
;
1180 DPRINT("u64: 0x%016"PRIx64
"\n", features
);
1182 dev
->protocol_features
= vmsg
->payload
.u64
;
1184 if (dev
->iface
->set_protocol_features
) {
1185 dev
->iface
->set_protocol_features(dev
, features
);
1192 vu_get_queue_num_exec(VuDev
*dev
, VhostUserMsg
*vmsg
)
1194 DPRINT("Function %s() not implemented yet.\n", __func__
);
1199 vu_set_vring_enable_exec(VuDev
*dev
, VhostUserMsg
*vmsg
)
1201 unsigned int index
= vmsg
->payload
.state
.index
;
1202 unsigned int enable
= vmsg
->payload
.state
.num
;
1204 DPRINT("State.index: %d\n", index
);
1205 DPRINT("State.enable: %d\n", enable
);
1207 if (index
>= VHOST_MAX_NR_VIRTQUEUE
) {
1208 vu_panic(dev
, "Invalid vring_enable index: %u", index
);
1212 dev
->vq
[index
].enable
= enable
;
1217 vu_set_slave_req_fd(VuDev
*dev
, VhostUserMsg
*vmsg
)
1219 if (vmsg
->fd_num
!= 1) {
1220 vu_panic(dev
, "Invalid slave_req_fd message (%d fd's)", vmsg
->fd_num
);
1224 if (dev
->slave_fd
!= -1) {
1225 close(dev
->slave_fd
);
1227 dev
->slave_fd
= vmsg
->fds
[0];
1228 DPRINT("Got slave_fd: %d\n", vmsg
->fds
[0]);
1234 vu_get_config(VuDev
*dev
, VhostUserMsg
*vmsg
)
1238 if (dev
->iface
->get_config
) {
1239 ret
= dev
->iface
->get_config(dev
, vmsg
->payload
.config
.region
,
1240 vmsg
->payload
.config
.size
);
1244 /* resize to zero to indicate an error to master */
1252 vu_set_config(VuDev
*dev
, VhostUserMsg
*vmsg
)
1256 if (dev
->iface
->set_config
) {
1257 ret
= dev
->iface
->set_config(dev
, vmsg
->payload
.config
.region
,
1258 vmsg
->payload
.config
.offset
,
1259 vmsg
->payload
.config
.size
,
1260 vmsg
->payload
.config
.flags
);
1262 vu_panic(dev
, "Set virtio configuration space failed");
1270 vu_set_postcopy_advise(VuDev
*dev
, VhostUserMsg
*vmsg
)
1272 dev
->postcopy_ufd
= -1;
1274 struct uffdio_api api_struct
;
1276 dev
->postcopy_ufd
= syscall(__NR_userfaultfd
, O_CLOEXEC
| O_NONBLOCK
);
1280 if (dev
->postcopy_ufd
== -1) {
1281 vu_panic(dev
, "Userfaultfd not available: %s", strerror(errno
));
1286 api_struct
.api
= UFFD_API
;
1287 api_struct
.features
= 0;
1288 if (ioctl(dev
->postcopy_ufd
, UFFDIO_API
, &api_struct
)) {
1289 vu_panic(dev
, "Failed UFFDIO_API: %s", strerror(errno
));
1290 close(dev
->postcopy_ufd
);
1291 dev
->postcopy_ufd
= -1;
1294 /* TODO: Stash feature flags somewhere */
1298 /* Return a ufd to the QEMU */
1300 vmsg
->fds
[0] = dev
->postcopy_ufd
;
1301 return true; /* = send a reply */
1305 vu_set_postcopy_listen(VuDev
*dev
, VhostUserMsg
*vmsg
)
1307 vmsg
->payload
.u64
= -1;
1308 vmsg
->size
= sizeof(vmsg
->payload
.u64
);
1310 if (dev
->nregions
) {
1311 vu_panic(dev
, "Regions already registered at postcopy-listen");
1314 dev
->postcopy_listening
= true;
1316 vmsg
->flags
= VHOST_USER_VERSION
| VHOST_USER_REPLY_MASK
;
1317 vmsg
->payload
.u64
= 0; /* Success */
1322 vu_set_postcopy_end(VuDev
*dev
, VhostUserMsg
*vmsg
)
1324 DPRINT("%s: Entry\n", __func__
);
1325 dev
->postcopy_listening
= false;
1326 if (dev
->postcopy_ufd
> 0) {
1327 close(dev
->postcopy_ufd
);
1328 dev
->postcopy_ufd
= -1;
1329 DPRINT("%s: Done close\n", __func__
);
1333 vmsg
->payload
.u64
= 0;
1334 vmsg
->size
= sizeof(vmsg
->payload
.u64
);
1335 vmsg
->flags
= VHOST_USER_VERSION
| VHOST_USER_REPLY_MASK
;
1336 DPRINT("%s: exit\n", __func__
);
1340 static inline uint64_t
1341 vu_inflight_queue_size(uint16_t queue_size
)
1343 return ALIGN_UP(sizeof(VuDescStateSplit
) * queue_size
+
1344 sizeof(uint16_t), INFLIGHT_ALIGNMENT
);
1348 vu_get_inflight_fd(VuDev
*dev
, VhostUserMsg
*vmsg
)
1353 uint16_t num_queues
, queue_size
;
1355 if (vmsg
->size
!= sizeof(vmsg
->payload
.inflight
)) {
1356 vu_panic(dev
, "Invalid get_inflight_fd message:%d", vmsg
->size
);
1357 vmsg
->payload
.inflight
.mmap_size
= 0;
1361 num_queues
= vmsg
->payload
.inflight
.num_queues
;
1362 queue_size
= vmsg
->payload
.inflight
.queue_size
;
1364 DPRINT("set_inflight_fd num_queues: %"PRId16
"\n", num_queues
);
1365 DPRINT("set_inflight_fd queue_size: %"PRId16
"\n", queue_size
);
1367 mmap_size
= vu_inflight_queue_size(queue_size
) * num_queues
;
1369 addr
= qemu_memfd_alloc("vhost-inflight", mmap_size
,
1370 F_SEAL_GROW
| F_SEAL_SHRINK
| F_SEAL_SEAL
,
1374 vu_panic(dev
, "Failed to alloc vhost inflight area");
1375 vmsg
->payload
.inflight
.mmap_size
= 0;
1379 memset(addr
, 0, mmap_size
);
1381 dev
->inflight_info
.addr
= addr
;
1382 dev
->inflight_info
.size
= vmsg
->payload
.inflight
.mmap_size
= mmap_size
;
1383 dev
->inflight_info
.fd
= vmsg
->fds
[0] = fd
;
1385 vmsg
->payload
.inflight
.mmap_offset
= 0;
1387 DPRINT("send inflight mmap_size: %"PRId64
"\n",
1388 vmsg
->payload
.inflight
.mmap_size
);
1389 DPRINT("send inflight mmap offset: %"PRId64
"\n",
1390 vmsg
->payload
.inflight
.mmap_offset
);
1396 vu_set_inflight_fd(VuDev
*dev
, VhostUserMsg
*vmsg
)
1399 uint64_t mmap_size
, mmap_offset
;
1400 uint16_t num_queues
, queue_size
;
1403 if (vmsg
->fd_num
!= 1 ||
1404 vmsg
->size
!= sizeof(vmsg
->payload
.inflight
)) {
1405 vu_panic(dev
, "Invalid set_inflight_fd message size:%d fds:%d",
1406 vmsg
->size
, vmsg
->fd_num
);
1411 mmap_size
= vmsg
->payload
.inflight
.mmap_size
;
1412 mmap_offset
= vmsg
->payload
.inflight
.mmap_offset
;
1413 num_queues
= vmsg
->payload
.inflight
.num_queues
;
1414 queue_size
= vmsg
->payload
.inflight
.queue_size
;
1416 DPRINT("set_inflight_fd mmap_size: %"PRId64
"\n", mmap_size
);
1417 DPRINT("set_inflight_fd mmap_offset: %"PRId64
"\n", mmap_offset
);
1418 DPRINT("set_inflight_fd num_queues: %"PRId16
"\n", num_queues
);
1419 DPRINT("set_inflight_fd queue_size: %"PRId16
"\n", queue_size
);
1421 rc
= mmap(0, mmap_size
, PROT_READ
| PROT_WRITE
, MAP_SHARED
,
1424 if (rc
== MAP_FAILED
) {
1425 vu_panic(dev
, "set_inflight_fd mmap error: %s", strerror(errno
));
1429 if (dev
->inflight_info
.fd
) {
1430 close(dev
->inflight_info
.fd
);
1433 if (dev
->inflight_info
.addr
) {
1434 munmap(dev
->inflight_info
.addr
, dev
->inflight_info
.size
);
1437 dev
->inflight_info
.fd
= fd
;
1438 dev
->inflight_info
.addr
= rc
;
1439 dev
->inflight_info
.size
= mmap_size
;
1441 for (i
= 0; i
< num_queues
; i
++) {
1442 dev
->vq
[i
].inflight
= (VuVirtqInflight
*)rc
;
1443 dev
->vq
[i
].inflight
->desc_num
= queue_size
;
1444 rc
= (void *)((char *)rc
+ vu_inflight_queue_size(queue_size
));
1451 vu_process_message(VuDev
*dev
, VhostUserMsg
*vmsg
)
1455 /* Print out generic part of the request. */
1456 DPRINT("================ Vhost user message ================\n");
1457 DPRINT("Request: %s (%d)\n", vu_request_to_string(vmsg
->request
),
1459 DPRINT("Flags: 0x%x\n", vmsg
->flags
);
1460 DPRINT("Size: %d\n", vmsg
->size
);
1465 for (i
= 0; i
< vmsg
->fd_num
; i
++) {
1466 DPRINT(" %d", vmsg
->fds
[i
]);
1471 if (dev
->iface
->process_msg
&&
1472 dev
->iface
->process_msg(dev
, vmsg
, &do_reply
)) {
1476 switch (vmsg
->request
) {
1477 case VHOST_USER_GET_FEATURES
:
1478 return vu_get_features_exec(dev
, vmsg
);
1479 case VHOST_USER_SET_FEATURES
:
1480 return vu_set_features_exec(dev
, vmsg
);
1481 case VHOST_USER_GET_PROTOCOL_FEATURES
:
1482 return vu_get_protocol_features_exec(dev
, vmsg
);
1483 case VHOST_USER_SET_PROTOCOL_FEATURES
:
1484 return vu_set_protocol_features_exec(dev
, vmsg
);
1485 case VHOST_USER_SET_OWNER
:
1486 return vu_set_owner_exec(dev
, vmsg
);
1487 case VHOST_USER_RESET_OWNER
:
1488 return vu_reset_device_exec(dev
, vmsg
);
1489 case VHOST_USER_SET_MEM_TABLE
:
1490 return vu_set_mem_table_exec(dev
, vmsg
);
1491 case VHOST_USER_SET_LOG_BASE
:
1492 return vu_set_log_base_exec(dev
, vmsg
);
1493 case VHOST_USER_SET_LOG_FD
:
1494 return vu_set_log_fd_exec(dev
, vmsg
);
1495 case VHOST_USER_SET_VRING_NUM
:
1496 return vu_set_vring_num_exec(dev
, vmsg
);
1497 case VHOST_USER_SET_VRING_ADDR
:
1498 return vu_set_vring_addr_exec(dev
, vmsg
);
1499 case VHOST_USER_SET_VRING_BASE
:
1500 return vu_set_vring_base_exec(dev
, vmsg
);
1501 case VHOST_USER_GET_VRING_BASE
:
1502 return vu_get_vring_base_exec(dev
, vmsg
);
1503 case VHOST_USER_SET_VRING_KICK
:
1504 return vu_set_vring_kick_exec(dev
, vmsg
);
1505 case VHOST_USER_SET_VRING_CALL
:
1506 return vu_set_vring_call_exec(dev
, vmsg
);
1507 case VHOST_USER_SET_VRING_ERR
:
1508 return vu_set_vring_err_exec(dev
, vmsg
);
1509 case VHOST_USER_GET_QUEUE_NUM
:
1510 return vu_get_queue_num_exec(dev
, vmsg
);
1511 case VHOST_USER_SET_VRING_ENABLE
:
1512 return vu_set_vring_enable_exec(dev
, vmsg
);
1513 case VHOST_USER_SET_SLAVE_REQ_FD
:
1514 return vu_set_slave_req_fd(dev
, vmsg
);
1515 case VHOST_USER_GET_CONFIG
:
1516 return vu_get_config(dev
, vmsg
);
1517 case VHOST_USER_SET_CONFIG
:
1518 return vu_set_config(dev
, vmsg
);
1519 case VHOST_USER_NONE
:
1520 /* if you need processing before exit, override iface->process_msg */
1522 case VHOST_USER_POSTCOPY_ADVISE
:
1523 return vu_set_postcopy_advise(dev
, vmsg
);
1524 case VHOST_USER_POSTCOPY_LISTEN
:
1525 return vu_set_postcopy_listen(dev
, vmsg
);
1526 case VHOST_USER_POSTCOPY_END
:
1527 return vu_set_postcopy_end(dev
, vmsg
);
1528 case VHOST_USER_GET_INFLIGHT_FD
:
1529 return vu_get_inflight_fd(dev
, vmsg
);
1530 case VHOST_USER_SET_INFLIGHT_FD
:
1531 return vu_set_inflight_fd(dev
, vmsg
);
1533 vmsg_close_fds(vmsg
);
1534 vu_panic(dev
, "Unhandled request: %d", vmsg
->request
);
1541 vu_dispatch(VuDev
*dev
)
1543 VhostUserMsg vmsg
= { 0, };
1544 int reply_requested
;
1545 bool success
= false;
1547 if (!vu_message_read(dev
, dev
->sock
, &vmsg
)) {
1551 reply_requested
= vu_process_message(dev
, &vmsg
);
1552 if (!reply_requested
) {
1557 if (!vu_send_reply(dev
, dev
->sock
, &vmsg
)) {
1569 vu_deinit(VuDev
*dev
)
1573 for (i
= 0; i
< dev
->nregions
; i
++) {
1574 VuDevRegion
*r
= &dev
->regions
[i
];
1575 void *m
= (void *) (uintptr_t) r
->mmap_addr
;
1576 if (m
!= MAP_FAILED
) {
1577 munmap(m
, r
->size
+ r
->mmap_offset
);
1582 for (i
= 0; i
< VHOST_MAX_NR_VIRTQUEUE
; i
++) {
1583 VuVirtq
*vq
= &dev
->vq
[i
];
1585 if (vq
->call_fd
!= -1) {
1590 if (vq
->kick_fd
!= -1) {
1595 if (vq
->err_fd
!= -1) {
1600 if (vq
->resubmit_list
) {
1601 free(vq
->resubmit_list
);
1602 vq
->resubmit_list
= NULL
;
1605 vq
->inflight
= NULL
;
1608 if (dev
->inflight_info
.addr
) {
1609 munmap(dev
->inflight_info
.addr
, dev
->inflight_info
.size
);
1610 dev
->inflight_info
.addr
= NULL
;
1613 if (dev
->inflight_info
.fd
> 0) {
1614 close(dev
->inflight_info
.fd
);
1615 dev
->inflight_info
.fd
= -1;
1619 if (dev
->slave_fd
!= -1) {
1620 close(dev
->slave_fd
);
1624 if (dev
->sock
!= -1) {
1633 vu_set_watch_cb set_watch
,
1634 vu_remove_watch_cb remove_watch
,
1635 const VuDevIface
*iface
)
1639 assert(socket
>= 0);
1641 assert(remove_watch
);
1645 memset(dev
, 0, sizeof(*dev
));
1649 dev
->set_watch
= set_watch
;
1650 dev
->remove_watch
= remove_watch
;
1652 dev
->log_call_fd
= -1;
1654 for (i
= 0; i
< VHOST_MAX_NR_VIRTQUEUE
; i
++) {
1655 dev
->vq
[i
] = (VuVirtq
) {
1656 .call_fd
= -1, .kick_fd
= -1, .err_fd
= -1,
1657 .notification
= true,
1663 vu_get_queue(VuDev
*dev
, int qidx
)
1665 assert(qidx
< VHOST_MAX_NR_VIRTQUEUE
);
1666 return &dev
->vq
[qidx
];
1670 vu_queue_enabled(VuDev
*dev
, VuVirtq
*vq
)
1676 vu_queue_started(const VuDev
*dev
, const VuVirtq
*vq
)
1681 static inline uint16_t
1682 vring_avail_flags(VuVirtq
*vq
)
1684 return vq
->vring
.avail
->flags
;
1687 static inline uint16_t
1688 vring_avail_idx(VuVirtq
*vq
)
1690 vq
->shadow_avail_idx
= vq
->vring
.avail
->idx
;
1692 return vq
->shadow_avail_idx
;
1695 static inline uint16_t
1696 vring_avail_ring(VuVirtq
*vq
, int i
)
1698 return vq
->vring
.avail
->ring
[i
];
1701 static inline uint16_t
1702 vring_get_used_event(VuVirtq
*vq
)
1704 return vring_avail_ring(vq
, vq
->vring
.num
);
1708 virtqueue_num_heads(VuDev
*dev
, VuVirtq
*vq
, unsigned int idx
)
1710 uint16_t num_heads
= vring_avail_idx(vq
) - idx
;
1712 /* Check it isn't doing very strange things with descriptor numbers. */
1713 if (num_heads
> vq
->vring
.num
) {
1714 vu_panic(dev
, "Guest moved used index from %u to %u",
1715 idx
, vq
->shadow_avail_idx
);
1719 /* On success, callers read a descriptor at vq->last_avail_idx.
1720 * Make sure descriptor read does not bypass avail index read. */
1728 virtqueue_get_head(VuDev
*dev
, VuVirtq
*vq
,
1729 unsigned int idx
, unsigned int *head
)
1731 /* Grab the next descriptor number they're advertising, and increment
1732 * the index we've seen. */
1733 *head
= vring_avail_ring(vq
, idx
% vq
->vring
.num
);
1735 /* If their number is silly, that's a fatal mistake. */
1736 if (*head
>= vq
->vring
.num
) {
1737 vu_panic(dev
, "Guest says index %u is available", head
);
1745 virtqueue_read_indirect_desc(VuDev
*dev
, struct vring_desc
*desc
,
1746 uint64_t addr
, size_t len
)
1748 struct vring_desc
*ori_desc
;
1751 if (len
> (VIRTQUEUE_MAX_SIZE
* sizeof(struct vring_desc
))) {
1761 ori_desc
= vu_gpa_to_va(dev
, &read_len
, addr
);
1766 memcpy(desc
, ori_desc
, read_len
);
1776 VIRTQUEUE_READ_DESC_ERROR
= -1,
1777 VIRTQUEUE_READ_DESC_DONE
= 0, /* end of chain */
1778 VIRTQUEUE_READ_DESC_MORE
= 1, /* more buffers in chain */
1782 virtqueue_read_next_desc(VuDev
*dev
, struct vring_desc
*desc
,
1783 int i
, unsigned int max
, unsigned int *next
)
1785 /* If this descriptor says it doesn't chain, we're done. */
1786 if (!(desc
[i
].flags
& VRING_DESC_F_NEXT
)) {
1787 return VIRTQUEUE_READ_DESC_DONE
;
1790 /* Check they're not leading us off end of descriptors. */
1791 *next
= desc
[i
].next
;
1792 /* Make sure compiler knows to grab that: we don't want it changing! */
1796 vu_panic(dev
, "Desc next is %u", next
);
1797 return VIRTQUEUE_READ_DESC_ERROR
;
1800 return VIRTQUEUE_READ_DESC_MORE
;
1804 vu_queue_get_avail_bytes(VuDev
*dev
, VuVirtq
*vq
, unsigned int *in_bytes
,
1805 unsigned int *out_bytes
,
1806 unsigned max_in_bytes
, unsigned max_out_bytes
)
1809 unsigned int total_bufs
, in_total
, out_total
;
1812 idx
= vq
->last_avail_idx
;
1814 total_bufs
= in_total
= out_total
= 0;
1815 if (unlikely(dev
->broken
) ||
1816 unlikely(!vq
->vring
.avail
)) {
1820 while ((rc
= virtqueue_num_heads(dev
, vq
, idx
)) > 0) {
1821 unsigned int max
, desc_len
, num_bufs
, indirect
= 0;
1822 uint64_t desc_addr
, read_len
;
1823 struct vring_desc
*desc
;
1824 struct vring_desc desc_buf
[VIRTQUEUE_MAX_SIZE
];
1827 max
= vq
->vring
.num
;
1828 num_bufs
= total_bufs
;
1829 if (!virtqueue_get_head(dev
, vq
, idx
++, &i
)) {
1832 desc
= vq
->vring
.desc
;
1834 if (desc
[i
].flags
& VRING_DESC_F_INDIRECT
) {
1835 if (desc
[i
].len
% sizeof(struct vring_desc
)) {
1836 vu_panic(dev
, "Invalid size for indirect buffer table");
1840 /* If we've got too many, that implies a descriptor loop. */
1841 if (num_bufs
>= max
) {
1842 vu_panic(dev
, "Looped descriptor");
1846 /* loop over the indirect descriptor table */
1848 desc_addr
= desc
[i
].addr
;
1849 desc_len
= desc
[i
].len
;
1850 max
= desc_len
/ sizeof(struct vring_desc
);
1851 read_len
= desc_len
;
1852 desc
= vu_gpa_to_va(dev
, &read_len
, desc_addr
);
1853 if (unlikely(desc
&& read_len
!= desc_len
)) {
1854 /* Failed to use zero copy */
1856 if (!virtqueue_read_indirect_desc(dev
, desc_buf
,
1863 vu_panic(dev
, "Invalid indirect buffer table");
1870 /* If we've got too many, that implies a descriptor loop. */
1871 if (++num_bufs
> max
) {
1872 vu_panic(dev
, "Looped descriptor");
1876 if (desc
[i
].flags
& VRING_DESC_F_WRITE
) {
1877 in_total
+= desc
[i
].len
;
1879 out_total
+= desc
[i
].len
;
1881 if (in_total
>= max_in_bytes
&& out_total
>= max_out_bytes
) {
1884 rc
= virtqueue_read_next_desc(dev
, desc
, i
, max
, &i
);
1885 } while (rc
== VIRTQUEUE_READ_DESC_MORE
);
1887 if (rc
== VIRTQUEUE_READ_DESC_ERROR
) {
1892 total_bufs
= num_bufs
;
1902 *in_bytes
= in_total
;
1905 *out_bytes
= out_total
;
1910 in_total
= out_total
= 0;
1915 vu_queue_avail_bytes(VuDev
*dev
, VuVirtq
*vq
, unsigned int in_bytes
,
1916 unsigned int out_bytes
)
1918 unsigned int in_total
, out_total
;
1920 vu_queue_get_avail_bytes(dev
, vq
, &in_total
, &out_total
,
1921 in_bytes
, out_bytes
);
1923 return in_bytes
<= in_total
&& out_bytes
<= out_total
;
1926 /* Fetch avail_idx from VQ memory only when we really need to know if
1927 * guest has added some buffers. */
1929 vu_queue_empty(VuDev
*dev
, VuVirtq
*vq
)
1931 if (unlikely(dev
->broken
) ||
1932 unlikely(!vq
->vring
.avail
)) {
1936 if (vq
->shadow_avail_idx
!= vq
->last_avail_idx
) {
1940 return vring_avail_idx(vq
) == vq
->last_avail_idx
;
1944 vring_notify(VuDev
*dev
, VuVirtq
*vq
)
1949 /* We need to expose used array entries before checking used event. */
1952 /* Always notify when queue is empty (when feature acknowledge) */
1953 if (vu_has_feature(dev
, VIRTIO_F_NOTIFY_ON_EMPTY
) &&
1954 !vq
->inuse
&& vu_queue_empty(dev
, vq
)) {
1958 if (!vu_has_feature(dev
, VIRTIO_RING_F_EVENT_IDX
)) {
1959 return !(vring_avail_flags(vq
) & VRING_AVAIL_F_NO_INTERRUPT
);
1962 v
= vq
->signalled_used_valid
;
1963 vq
->signalled_used_valid
= true;
1964 old
= vq
->signalled_used
;
1965 new = vq
->signalled_used
= vq
->used_idx
;
1966 return !v
|| vring_need_event(vring_get_used_event(vq
), new, old
);
1970 vu_queue_notify(VuDev
*dev
, VuVirtq
*vq
)
1972 if (unlikely(dev
->broken
) ||
1973 unlikely(!vq
->vring
.avail
)) {
1977 if (!vring_notify(dev
, vq
)) {
1978 DPRINT("skipped notify...\n");
1982 if (eventfd_write(vq
->call_fd
, 1) < 0) {
1983 vu_panic(dev
, "Error writing eventfd: %s", strerror(errno
));
1988 vring_used_flags_set_bit(VuVirtq
*vq
, int mask
)
1992 flags
= (uint16_t *)((char*)vq
->vring
.used
+
1993 offsetof(struct vring_used
, flags
));
1998 vring_used_flags_unset_bit(VuVirtq
*vq
, int mask
)
2002 flags
= (uint16_t *)((char*)vq
->vring
.used
+
2003 offsetof(struct vring_used
, flags
));
2008 vring_set_avail_event(VuVirtq
*vq
, uint16_t val
)
2010 if (!vq
->notification
) {
2014 *((uint16_t *) &vq
->vring
.used
->ring
[vq
->vring
.num
]) = val
;
2018 vu_queue_set_notification(VuDev
*dev
, VuVirtq
*vq
, int enable
)
2020 vq
->notification
= enable
;
2021 if (vu_has_feature(dev
, VIRTIO_RING_F_EVENT_IDX
)) {
2022 vring_set_avail_event(vq
, vring_avail_idx(vq
));
2023 } else if (enable
) {
2024 vring_used_flags_unset_bit(vq
, VRING_USED_F_NO_NOTIFY
);
2026 vring_used_flags_set_bit(vq
, VRING_USED_F_NO_NOTIFY
);
2029 /* Expose avail event/used flags before caller checks the avail idx. */
2035 virtqueue_map_desc(VuDev
*dev
,
2036 unsigned int *p_num_sg
, struct iovec
*iov
,
2037 unsigned int max_num_sg
, bool is_write
,
2038 uint64_t pa
, size_t sz
)
2040 unsigned num_sg
= *p_num_sg
;
2042 assert(num_sg
<= max_num_sg
);
2045 vu_panic(dev
, "virtio: zero sized buffers are not allowed");
2052 if (num_sg
== max_num_sg
) {
2053 vu_panic(dev
, "virtio: too many descriptors in indirect table");
2057 iov
[num_sg
].iov_base
= vu_gpa_to_va(dev
, &len
, pa
);
2058 if (iov
[num_sg
].iov_base
== NULL
) {
2059 vu_panic(dev
, "virtio: invalid address for buffers");
2062 iov
[num_sg
].iov_len
= len
;
2072 virtqueue_alloc_element(size_t sz
,
2073 unsigned out_num
, unsigned in_num
)
2075 VuVirtqElement
*elem
;
2076 size_t in_sg_ofs
= ALIGN_UP(sz
, __alignof__(elem
->in_sg
[0]));
2077 size_t out_sg_ofs
= in_sg_ofs
+ in_num
* sizeof(elem
->in_sg
[0]);
2078 size_t out_sg_end
= out_sg_ofs
+ out_num
* sizeof(elem
->out_sg
[0]);
2080 assert(sz
>= sizeof(VuVirtqElement
));
2081 elem
= malloc(out_sg_end
);
2082 elem
->out_num
= out_num
;
2083 elem
->in_num
= in_num
;
2084 elem
->in_sg
= (void *)elem
+ in_sg_ofs
;
2085 elem
->out_sg
= (void *)elem
+ out_sg_ofs
;
2090 vu_queue_map_desc(VuDev
*dev
, VuVirtq
*vq
, unsigned int idx
, size_t sz
)
2092 struct vring_desc
*desc
= vq
->vring
.desc
;
2093 uint64_t desc_addr
, read_len
;
2094 unsigned int desc_len
;
2095 unsigned int max
= vq
->vring
.num
;
2096 unsigned int i
= idx
;
2097 VuVirtqElement
*elem
;
2098 unsigned int out_num
= 0, in_num
= 0;
2099 struct iovec iov
[VIRTQUEUE_MAX_SIZE
];
2100 struct vring_desc desc_buf
[VIRTQUEUE_MAX_SIZE
];
2103 if (desc
[i
].flags
& VRING_DESC_F_INDIRECT
) {
2104 if (desc
[i
].len
% sizeof(struct vring_desc
)) {
2105 vu_panic(dev
, "Invalid size for indirect buffer table");
2108 /* loop over the indirect descriptor table */
2109 desc_addr
= desc
[i
].addr
;
2110 desc_len
= desc
[i
].len
;
2111 max
= desc_len
/ sizeof(struct vring_desc
);
2112 read_len
= desc_len
;
2113 desc
= vu_gpa_to_va(dev
, &read_len
, desc_addr
);
2114 if (unlikely(desc
&& read_len
!= desc_len
)) {
2115 /* Failed to use zero copy */
2117 if (!virtqueue_read_indirect_desc(dev
, desc_buf
,
2124 vu_panic(dev
, "Invalid indirect buffer table");
2130 /* Collect all the descriptors */
2132 if (desc
[i
].flags
& VRING_DESC_F_WRITE
) {
2133 virtqueue_map_desc(dev
, &in_num
, iov
+ out_num
,
2134 VIRTQUEUE_MAX_SIZE
- out_num
, true,
2135 desc
[i
].addr
, desc
[i
].len
);
2138 vu_panic(dev
, "Incorrect order for descriptors");
2141 virtqueue_map_desc(dev
, &out_num
, iov
,
2142 VIRTQUEUE_MAX_SIZE
, false,
2143 desc
[i
].addr
, desc
[i
].len
);
2146 /* If we've got too many, that implies a descriptor loop. */
2147 if ((in_num
+ out_num
) > max
) {
2148 vu_panic(dev
, "Looped descriptor");
2150 rc
= virtqueue_read_next_desc(dev
, desc
, i
, max
, &i
);
2151 } while (rc
== VIRTQUEUE_READ_DESC_MORE
);
2153 if (rc
== VIRTQUEUE_READ_DESC_ERROR
) {
2154 vu_panic(dev
, "read descriptor error");
2158 /* Now copy what we have collected and mapped */
2159 elem
= virtqueue_alloc_element(sz
, out_num
, in_num
);
2161 for (i
= 0; i
< out_num
; i
++) {
2162 elem
->out_sg
[i
] = iov
[i
];
2164 for (i
= 0; i
< in_num
; i
++) {
2165 elem
->in_sg
[i
] = iov
[out_num
+ i
];
2172 vu_queue_inflight_get(VuDev
*dev
, VuVirtq
*vq
, int desc_idx
)
2174 if (!has_feature(dev
->protocol_features
,
2175 VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD
)) {
2179 if (unlikely(!vq
->inflight
)) {
2183 vq
->inflight
->desc
[desc_idx
].counter
= vq
->counter
++;
2184 vq
->inflight
->desc
[desc_idx
].inflight
= 1;
2190 vu_queue_inflight_pre_put(VuDev
*dev
, VuVirtq
*vq
, int desc_idx
)
2192 if (!has_feature(dev
->protocol_features
,
2193 VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD
)) {
2197 if (unlikely(!vq
->inflight
)) {
2201 vq
->inflight
->last_batch_head
= desc_idx
;
2207 vu_queue_inflight_post_put(VuDev
*dev
, VuVirtq
*vq
, int desc_idx
)
2209 if (!has_feature(dev
->protocol_features
,
2210 VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD
)) {
2214 if (unlikely(!vq
->inflight
)) {
2220 vq
->inflight
->desc
[desc_idx
].inflight
= 0;
2224 vq
->inflight
->used_idx
= vq
->used_idx
;
2230 vu_queue_pop(VuDev
*dev
, VuVirtq
*vq
, size_t sz
)
2234 VuVirtqElement
*elem
;
2236 if (unlikely(dev
->broken
) ||
2237 unlikely(!vq
->vring
.avail
)) {
2241 if (unlikely(vq
->resubmit_list
&& vq
->resubmit_num
> 0)) {
2242 i
= (--vq
->resubmit_num
);
2243 elem
= vu_queue_map_desc(dev
, vq
, vq
->resubmit_list
[i
].index
, sz
);
2245 if (!vq
->resubmit_num
) {
2246 free(vq
->resubmit_list
);
2247 vq
->resubmit_list
= NULL
;
2253 if (vu_queue_empty(dev
, vq
)) {
2257 * Needed after virtio_queue_empty(), see comment in
2258 * virtqueue_num_heads().
2262 if (vq
->inuse
>= vq
->vring
.num
) {
2263 vu_panic(dev
, "Virtqueue size exceeded");
2267 if (!virtqueue_get_head(dev
, vq
, vq
->last_avail_idx
++, &head
)) {
2271 if (vu_has_feature(dev
, VIRTIO_RING_F_EVENT_IDX
)) {
2272 vring_set_avail_event(vq
, vq
->last_avail_idx
);
2275 elem
= vu_queue_map_desc(dev
, vq
, head
, sz
);
2283 vu_queue_inflight_get(dev
, vq
, head
);
2289 vu_queue_detach_element(VuDev
*dev
, VuVirtq
*vq
, VuVirtqElement
*elem
,
2293 /* unmap, when DMA support is added */
2297 vu_queue_unpop(VuDev
*dev
, VuVirtq
*vq
, VuVirtqElement
*elem
,
2300 vq
->last_avail_idx
--;
2301 vu_queue_detach_element(dev
, vq
, elem
, len
);
2305 vu_queue_rewind(VuDev
*dev
, VuVirtq
*vq
, unsigned int num
)
2307 if (num
> vq
->inuse
) {
2310 vq
->last_avail_idx
-= num
;
2316 void vring_used_write(VuDev
*dev
, VuVirtq
*vq
,
2317 struct vring_used_elem
*uelem
, int i
)
2319 struct vring_used
*used
= vq
->vring
.used
;
2321 used
->ring
[i
] = *uelem
;
2322 vu_log_write(dev
, vq
->vring
.log_guest_addr
+
2323 offsetof(struct vring_used
, ring
[i
]),
2324 sizeof(used
->ring
[i
]));
2329 vu_log_queue_fill(VuDev
*dev
, VuVirtq
*vq
,
2330 const VuVirtqElement
*elem
,
2333 struct vring_desc
*desc
= vq
->vring
.desc
;
2334 unsigned int i
, max
, min
, desc_len
;
2335 uint64_t desc_addr
, read_len
;
2336 struct vring_desc desc_buf
[VIRTQUEUE_MAX_SIZE
];
2337 unsigned num_bufs
= 0;
2339 max
= vq
->vring
.num
;
2342 if (desc
[i
].flags
& VRING_DESC_F_INDIRECT
) {
2343 if (desc
[i
].len
% sizeof(struct vring_desc
)) {
2344 vu_panic(dev
, "Invalid size for indirect buffer table");
2347 /* loop over the indirect descriptor table */
2348 desc_addr
= desc
[i
].addr
;
2349 desc_len
= desc
[i
].len
;
2350 max
= desc_len
/ sizeof(struct vring_desc
);
2351 read_len
= desc_len
;
2352 desc
= vu_gpa_to_va(dev
, &read_len
, desc_addr
);
2353 if (unlikely(desc
&& read_len
!= desc_len
)) {
2354 /* Failed to use zero copy */
2356 if (!virtqueue_read_indirect_desc(dev
, desc_buf
,
2363 vu_panic(dev
, "Invalid indirect buffer table");
2370 if (++num_bufs
> max
) {
2371 vu_panic(dev
, "Looped descriptor");
2375 if (desc
[i
].flags
& VRING_DESC_F_WRITE
) {
2376 min
= MIN(desc
[i
].len
, len
);
2377 vu_log_write(dev
, desc
[i
].addr
, min
);
2382 (virtqueue_read_next_desc(dev
, desc
, i
, max
, &i
)
2383 == VIRTQUEUE_READ_DESC_MORE
));
2387 vu_queue_fill(VuDev
*dev
, VuVirtq
*vq
,
2388 const VuVirtqElement
*elem
,
2389 unsigned int len
, unsigned int idx
)
2391 struct vring_used_elem uelem
;
2393 if (unlikely(dev
->broken
) ||
2394 unlikely(!vq
->vring
.avail
)) {
2398 vu_log_queue_fill(dev
, vq
, elem
, len
);
2400 idx
= (idx
+ vq
->used_idx
) % vq
->vring
.num
;
2402 uelem
.id
= elem
->index
;
2404 vring_used_write(dev
, vq
, &uelem
, idx
);
2408 void vring_used_idx_set(VuDev
*dev
, VuVirtq
*vq
, uint16_t val
)
2410 vq
->vring
.used
->idx
= val
;
2412 vq
->vring
.log_guest_addr
+ offsetof(struct vring_used
, idx
),
2413 sizeof(vq
->vring
.used
->idx
));
2419 vu_queue_flush(VuDev
*dev
, VuVirtq
*vq
, unsigned int count
)
2423 if (unlikely(dev
->broken
) ||
2424 unlikely(!vq
->vring
.avail
)) {
2428 /* Make sure buffer is written before we update index. */
2433 vring_used_idx_set(dev
, vq
, new);
2435 if (unlikely((int16_t)(new - vq
->signalled_used
) < (uint16_t)(new - old
))) {
2436 vq
->signalled_used_valid
= false;
2441 vu_queue_push(VuDev
*dev
, VuVirtq
*vq
,
2442 const VuVirtqElement
*elem
, unsigned int len
)
2444 vu_queue_fill(dev
, vq
, elem
, len
, 0);
2445 vu_queue_inflight_pre_put(dev
, vq
, elem
->index
);
2446 vu_queue_flush(dev
, vq
, 1);
2447 vu_queue_inflight_post_put(dev
, vq
, elem
->index
);