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 #include <qemu/osdep.h>
17 #include <sys/eventfd.h>
18 #include <linux/vhost.h>
20 #include "qemu/atomic.h"
22 #include "libvhost-user.h"
24 #define VHOST_USER_HDR_SIZE offsetof(VhostUserMsg, payload.u64)
26 /* The version of the protocol we support */
27 #define VHOST_USER_VERSION 1
28 #define LIBVHOST_USER_DEBUG 0
32 if (LIBVHOST_USER_DEBUG) { \
33 fprintf(stderr, __VA_ARGS__); \
38 vu_request_to_string(int req
)
40 #define REQ(req) [req] = #req
41 static const char *vu_request_str
[] = {
43 REQ(VHOST_USER_GET_FEATURES
),
44 REQ(VHOST_USER_SET_FEATURES
),
46 REQ(VHOST_USER_GET_FEATURES
),
47 REQ(VHOST_USER_SET_FEATURES
),
48 REQ(VHOST_USER_SET_OWNER
),
49 REQ(VHOST_USER_RESET_OWNER
),
50 REQ(VHOST_USER_SET_MEM_TABLE
),
51 REQ(VHOST_USER_SET_LOG_BASE
),
52 REQ(VHOST_USER_SET_LOG_FD
),
53 REQ(VHOST_USER_SET_VRING_NUM
),
54 REQ(VHOST_USER_SET_VRING_ADDR
),
55 REQ(VHOST_USER_SET_VRING_BASE
),
56 REQ(VHOST_USER_GET_VRING_BASE
),
57 REQ(VHOST_USER_SET_VRING_KICK
),
58 REQ(VHOST_USER_SET_VRING_CALL
),
59 REQ(VHOST_USER_SET_VRING_ERR
),
60 REQ(VHOST_USER_GET_PROTOCOL_FEATURES
),
61 REQ(VHOST_USER_SET_PROTOCOL_FEATURES
),
62 REQ(VHOST_USER_GET_QUEUE_NUM
),
63 REQ(VHOST_USER_SET_VRING_ENABLE
),
64 REQ(VHOST_USER_SEND_RARP
),
65 REQ(VHOST_USER_INPUT_GET_CONFIG
),
70 if (req
< VHOST_USER_MAX
) {
71 return vu_request_str
[req
];
78 vu_panic(VuDev
*dev
, const char *msg
, ...)
84 buf
= g_strdup_vprintf(msg
, ap
);
91 /* FIXME: find a way to call virtio_error? */
94 /* Translate guest physical address to our virtual address. */
96 vu_gpa_to_va(VuDev
*dev
, uint64_t guest_addr
)
100 /* Find matching memory region. */
101 for (i
= 0; i
< dev
->nregions
; i
++) {
102 VuDevRegion
*r
= &dev
->regions
[i
];
104 if ((guest_addr
>= r
->gpa
) && (guest_addr
< (r
->gpa
+ r
->size
))) {
105 return (void *)(uintptr_t)
106 guest_addr
- r
->gpa
+ r
->mmap_addr
+ r
->mmap_offset
;
113 /* Translate qemu virtual address to our virtual address. */
115 qva_to_va(VuDev
*dev
, uint64_t qemu_addr
)
119 /* Find matching memory region. */
120 for (i
= 0; i
< dev
->nregions
; i
++) {
121 VuDevRegion
*r
= &dev
->regions
[i
];
123 if ((qemu_addr
>= r
->qva
) && (qemu_addr
< (r
->qva
+ r
->size
))) {
124 return (void *)(uintptr_t)
125 qemu_addr
- r
->qva
+ r
->mmap_addr
+ r
->mmap_offset
;
133 vmsg_close_fds(VhostUserMsg
*vmsg
)
137 for (i
= 0; i
< vmsg
->fd_num
; i
++) {
143 vu_message_read(VuDev
*dev
, int conn_fd
, VhostUserMsg
*vmsg
)
145 char control
[CMSG_SPACE(VHOST_MEMORY_MAX_NREGIONS
* sizeof(int))] = { };
147 .iov_base
= (char *)vmsg
,
148 .iov_len
= VHOST_USER_HDR_SIZE
,
150 struct msghdr msg
= {
153 .msg_control
= control
,
154 .msg_controllen
= sizeof(control
),
157 struct cmsghdr
*cmsg
;
161 rc
= recvmsg(conn_fd
, &msg
, 0);
162 } while (rc
< 0 && (errno
== EINTR
|| errno
== EAGAIN
));
165 vu_panic(dev
, "Error while recvmsg: %s", strerror(errno
));
170 for (cmsg
= CMSG_FIRSTHDR(&msg
);
172 cmsg
= CMSG_NXTHDR(&msg
, cmsg
))
174 if (cmsg
->cmsg_level
== SOL_SOCKET
&& cmsg
->cmsg_type
== SCM_RIGHTS
) {
175 fd_size
= cmsg
->cmsg_len
- CMSG_LEN(0);
176 vmsg
->fd_num
= fd_size
/ sizeof(int);
177 memcpy(vmsg
->fds
, CMSG_DATA(cmsg
), fd_size
);
182 if (vmsg
->size
> sizeof(vmsg
->payload
)) {
184 "Error: too big message request: %d, size: vmsg->size: %u, "
185 "while sizeof(vmsg->payload) = %zu\n",
186 vmsg
->request
, vmsg
->size
, sizeof(vmsg
->payload
));
192 rc
= read(conn_fd
, &vmsg
->payload
, vmsg
->size
);
193 } while (rc
< 0 && (errno
== EINTR
|| errno
== EAGAIN
));
196 vu_panic(dev
, "Error while reading: %s", strerror(errno
));
200 assert(rc
== vmsg
->size
);
206 vmsg_close_fds(vmsg
);
212 vu_message_write(VuDev
*dev
, int conn_fd
, VhostUserMsg
*vmsg
)
215 uint8_t *p
= (uint8_t *)vmsg
;
217 /* Set the version in the flags when sending the reply */
218 vmsg
->flags
&= ~VHOST_USER_VERSION_MASK
;
219 vmsg
->flags
|= VHOST_USER_VERSION
;
220 vmsg
->flags
|= VHOST_USER_REPLY_MASK
;
223 rc
= write(conn_fd
, p
, VHOST_USER_HDR_SIZE
);
224 } while (rc
< 0 && (errno
== EINTR
|| errno
== EAGAIN
));
228 rc
= write(conn_fd
, vmsg
->data
, vmsg
->size
);
230 rc
= write(conn_fd
, p
+ VHOST_USER_HDR_SIZE
, vmsg
->size
);
232 } while (rc
< 0 && (errno
== EINTR
|| errno
== EAGAIN
));
235 vu_panic(dev
, "Error while writing: %s", strerror(errno
));
242 /* Kick the log_call_fd if required. */
244 vu_log_kick(VuDev
*dev
)
246 if (dev
->log_call_fd
!= -1) {
247 DPRINT("Kicking the QEMU's log...\n");
248 if (eventfd_write(dev
->log_call_fd
, 1) < 0) {
249 vu_panic(dev
, "Error writing eventfd: %s", strerror(errno
));
255 vu_log_page(uint8_t *log_table
, uint64_t page
)
257 DPRINT("Logged dirty guest page: %"PRId64
"\n", page
);
258 atomic_or(&log_table
[page
/ 8], 1 << (page
% 8));
262 vu_log_write(VuDev
*dev
, uint64_t address
, uint64_t length
)
266 if (!(dev
->features
& (1ULL << VHOST_F_LOG_ALL
)) ||
267 !dev
->log_table
|| !length
) {
271 assert(dev
->log_size
> ((address
+ length
- 1) / VHOST_LOG_PAGE
/ 8));
273 page
= address
/ VHOST_LOG_PAGE
;
274 while (page
* VHOST_LOG_PAGE
< address
+ length
) {
275 vu_log_page(dev
->log_table
, page
);
276 page
+= VHOST_LOG_PAGE
;
283 vu_kick_cb(VuDev
*dev
, int condition
, void *data
)
285 int index
= (intptr_t)data
;
286 VuVirtq
*vq
= &dev
->vq
[index
];
287 int sock
= vq
->kick_fd
;
291 rc
= eventfd_read(sock
, &kick_data
);
293 vu_panic(dev
, "kick eventfd_read(): %s", strerror(errno
));
294 dev
->remove_watch(dev
, dev
->vq
[index
].kick_fd
);
296 DPRINT("Got kick_data: %016"PRIx64
" handler:%p idx:%d\n",
297 kick_data
, vq
->handler
, index
);
299 vq
->handler(dev
, index
);
305 vu_get_features_exec(VuDev
*dev
, VhostUserMsg
*vmsg
)
308 1ULL << VHOST_F_LOG_ALL
|
309 1ULL << VHOST_USER_F_PROTOCOL_FEATURES
;
311 if (dev
->iface
->get_features
) {
312 vmsg
->payload
.u64
|= dev
->iface
->get_features(dev
);
315 vmsg
->size
= sizeof(vmsg
->payload
.u64
);
317 DPRINT("Sending back to guest u64: 0x%016"PRIx64
"\n", vmsg
->payload
.u64
);
323 vu_set_enable_all_rings(VuDev
*dev
, bool enabled
)
327 for (i
= 0; i
< VHOST_MAX_NR_VIRTQUEUE
; i
++) {
328 dev
->vq
[i
].enable
= enabled
;
333 vu_set_features_exec(VuDev
*dev
, VhostUserMsg
*vmsg
)
335 DPRINT("u64: 0x%016"PRIx64
"\n", vmsg
->payload
.u64
);
337 dev
->features
= vmsg
->payload
.u64
;
339 if (!(dev
->features
& VHOST_USER_F_PROTOCOL_FEATURES
)) {
340 vu_set_enable_all_rings(dev
, true);
343 if (dev
->iface
->set_features
) {
344 dev
->iface
->set_features(dev
, dev
->features
);
351 vu_set_owner_exec(VuDev
*dev
, VhostUserMsg
*vmsg
)
357 vu_close_log(VuDev
*dev
)
359 if (dev
->log_table
) {
360 if (munmap(dev
->log_table
, dev
->log_size
) != 0) {
361 perror("close log munmap() error");
364 dev
->log_table
= NULL
;
366 if (dev
->log_call_fd
!= -1) {
367 close(dev
->log_call_fd
);
368 dev
->log_call_fd
= -1;
373 vu_reset_device_exec(VuDev
*dev
, VhostUserMsg
*vmsg
)
375 vu_set_enable_all_rings(dev
, false);
381 vu_set_mem_table_exec(VuDev
*dev
, VhostUserMsg
*vmsg
)
384 VhostUserMemory
*memory
= &vmsg
->payload
.memory
;
385 dev
->nregions
= memory
->nregions
;
387 DPRINT("Nregions: %d\n", memory
->nregions
);
388 for (i
= 0; i
< dev
->nregions
; i
++) {
390 VhostUserMemoryRegion
*msg_region
= &memory
->regions
[i
];
391 VuDevRegion
*dev_region
= &dev
->regions
[i
];
393 DPRINT("Region %d\n", i
);
394 DPRINT(" guest_phys_addr: 0x%016"PRIx64
"\n",
395 msg_region
->guest_phys_addr
);
396 DPRINT(" memory_size: 0x%016"PRIx64
"\n",
397 msg_region
->memory_size
);
398 DPRINT(" userspace_addr 0x%016"PRIx64
"\n",
399 msg_region
->userspace_addr
);
400 DPRINT(" mmap_offset 0x%016"PRIx64
"\n",
401 msg_region
->mmap_offset
);
403 dev_region
->gpa
= msg_region
->guest_phys_addr
;
404 dev_region
->size
= msg_region
->memory_size
;
405 dev_region
->qva
= msg_region
->userspace_addr
;
406 dev_region
->mmap_offset
= msg_region
->mmap_offset
;
408 /* We don't use offset argument of mmap() since the
409 * mapped address has to be page aligned, and we use huge
411 mmap_addr
= mmap(0, dev_region
->size
+ dev_region
->mmap_offset
,
412 PROT_READ
| PROT_WRITE
, MAP_SHARED
,
415 if (mmap_addr
== MAP_FAILED
) {
416 vu_panic(dev
, "region mmap error: %s", strerror(errno
));
418 dev_region
->mmap_addr
= (uint64_t)(uintptr_t)mmap_addr
;
419 DPRINT(" mmap_addr: 0x%016"PRIx64
"\n",
420 dev_region
->mmap_addr
);
430 vu_set_log_base_exec(VuDev
*dev
, VhostUserMsg
*vmsg
)
433 uint64_t log_mmap_size
, log_mmap_offset
;
436 if (vmsg
->fd_num
!= 1 ||
437 vmsg
->size
!= sizeof(vmsg
->payload
.log
)) {
438 vu_panic(dev
, "Invalid log_base message");
443 log_mmap_offset
= vmsg
->payload
.log
.mmap_offset
;
444 log_mmap_size
= vmsg
->payload
.log
.mmap_size
;
445 DPRINT("Log mmap_offset: %"PRId64
"\n", log_mmap_offset
);
446 DPRINT("Log mmap_size: %"PRId64
"\n", log_mmap_size
);
448 rc
= mmap(0, log_mmap_size
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, fd
,
450 if (rc
== MAP_FAILED
) {
451 perror("log mmap error");
454 dev
->log_size
= log_mmap_size
;
456 vmsg
->size
= sizeof(vmsg
->payload
.u64
);
462 vu_set_log_fd_exec(VuDev
*dev
, VhostUserMsg
*vmsg
)
464 if (vmsg
->fd_num
!= 1) {
465 vu_panic(dev
, "Invalid log_fd message");
469 if (dev
->log_call_fd
!= -1) {
470 close(dev
->log_call_fd
);
472 dev
->log_call_fd
= vmsg
->fds
[0];
473 DPRINT("Got log_call_fd: %d\n", vmsg
->fds
[0]);
479 vu_set_vring_num_exec(VuDev
*dev
, VhostUserMsg
*vmsg
)
481 unsigned int index
= vmsg
->payload
.state
.index
;
482 unsigned int num
= vmsg
->payload
.state
.num
;
484 DPRINT("State.index: %d\n", index
);
485 DPRINT("State.num: %d\n", num
);
486 dev
->vq
[index
].vring
.num
= num
;
492 vu_set_vring_addr_exec(VuDev
*dev
, VhostUserMsg
*vmsg
)
494 struct vhost_vring_addr
*vra
= &vmsg
->payload
.addr
;
495 unsigned int index
= vra
->index
;
496 VuVirtq
*vq
= &dev
->vq
[index
];
498 DPRINT("vhost_vring_addr:\n");
499 DPRINT(" index: %d\n", vra
->index
);
500 DPRINT(" flags: %d\n", vra
->flags
);
501 DPRINT(" desc_user_addr: 0x%016llx\n", vra
->desc_user_addr
);
502 DPRINT(" used_user_addr: 0x%016llx\n", vra
->used_user_addr
);
503 DPRINT(" avail_user_addr: 0x%016llx\n", vra
->avail_user_addr
);
504 DPRINT(" log_guest_addr: 0x%016llx\n", vra
->log_guest_addr
);
506 vq
->vring
.flags
= vra
->flags
;
507 vq
->vring
.desc
= qva_to_va(dev
, vra
->desc_user_addr
);
508 vq
->vring
.used
= qva_to_va(dev
, vra
->used_user_addr
);
509 vq
->vring
.avail
= qva_to_va(dev
, vra
->avail_user_addr
);
510 vq
->vring
.log_guest_addr
= vra
->log_guest_addr
;
512 DPRINT("Setting virtq addresses:\n");
513 DPRINT(" vring_desc at %p\n", vq
->vring
.desc
);
514 DPRINT(" vring_used at %p\n", vq
->vring
.used
);
515 DPRINT(" vring_avail at %p\n", vq
->vring
.avail
);
517 if (!(vq
->vring
.desc
&& vq
->vring
.used
&& vq
->vring
.avail
)) {
518 vu_panic(dev
, "Invalid vring_addr message");
522 vq
->used_idx
= vq
->vring
.used
->idx
;
528 vu_set_vring_base_exec(VuDev
*dev
, VhostUserMsg
*vmsg
)
530 unsigned int index
= vmsg
->payload
.state
.index
;
531 unsigned int num
= vmsg
->payload
.state
.num
;
533 DPRINT("State.index: %d\n", index
);
534 DPRINT("State.num: %d\n", num
);
535 dev
->vq
[index
].shadow_avail_idx
= dev
->vq
[index
].last_avail_idx
= num
;
541 vu_get_vring_base_exec(VuDev
*dev
, VhostUserMsg
*vmsg
)
543 unsigned int index
= vmsg
->payload
.state
.index
;
545 DPRINT("State.index: %d\n", index
);
546 vmsg
->payload
.state
.num
= dev
->vq
[index
].last_avail_idx
;
547 vmsg
->size
= sizeof(vmsg
->payload
.state
);
549 dev
->vq
[index
].started
= false;
550 if (dev
->iface
->queue_set_started
) {
551 dev
->iface
->queue_set_started(dev
, index
, false);
554 if (dev
->vq
[index
].call_fd
!= -1) {
555 close(dev
->vq
[index
].call_fd
);
556 dev
->vq
[index
].call_fd
= -1;
558 if (dev
->vq
[index
].kick_fd
!= -1) {
559 dev
->remove_watch(dev
, dev
->vq
[index
].kick_fd
);
560 close(dev
->vq
[index
].kick_fd
);
561 dev
->vq
[index
].kick_fd
= -1;
568 vu_check_queue_msg_file(VuDev
*dev
, VhostUserMsg
*vmsg
)
570 int index
= vmsg
->payload
.u64
& VHOST_USER_VRING_IDX_MASK
;
572 if (index
>= VHOST_MAX_NR_VIRTQUEUE
) {
573 vmsg_close_fds(vmsg
);
574 vu_panic(dev
, "Invalid queue index: %u", index
);
578 if (vmsg
->payload
.u64
& VHOST_USER_VRING_NOFD_MASK
||
580 vmsg_close_fds(vmsg
);
581 vu_panic(dev
, "Invalid fds in request: %d", vmsg
->request
);
589 vu_set_vring_kick_exec(VuDev
*dev
, VhostUserMsg
*vmsg
)
591 int index
= vmsg
->payload
.u64
& VHOST_USER_VRING_IDX_MASK
;
593 DPRINT("u64: 0x%016"PRIx64
"\n", vmsg
->payload
.u64
);
595 if (!vu_check_queue_msg_file(dev
, vmsg
)) {
599 if (dev
->vq
[index
].kick_fd
!= -1) {
600 dev
->remove_watch(dev
, dev
->vq
[index
].kick_fd
);
601 close(dev
->vq
[index
].kick_fd
);
602 dev
->vq
[index
].kick_fd
= -1;
605 if (!(vmsg
->payload
.u64
& VHOST_USER_VRING_NOFD_MASK
)) {
606 dev
->vq
[index
].kick_fd
= vmsg
->fds
[0];
607 DPRINT("Got kick_fd: %d for vq: %d\n", vmsg
->fds
[0], index
);
610 dev
->vq
[index
].started
= true;
611 if (dev
->iface
->queue_set_started
) {
612 dev
->iface
->queue_set_started(dev
, index
, true);
615 if (dev
->vq
[index
].kick_fd
!= -1 && dev
->vq
[index
].handler
) {
616 dev
->set_watch(dev
, dev
->vq
[index
].kick_fd
, VU_WATCH_IN
,
617 vu_kick_cb
, (void *)(long)index
);
619 DPRINT("Waiting for kicks on fd: %d for vq: %d\n",
620 dev
->vq
[index
].kick_fd
, index
);
626 void vu_set_queue_handler(VuDev
*dev
, VuVirtq
*vq
,
627 vu_queue_handler_cb handler
)
629 int qidx
= vq
- dev
->vq
;
631 vq
->handler
= handler
;
632 if (vq
->kick_fd
>= 0) {
634 dev
->set_watch(dev
, vq
->kick_fd
, VU_WATCH_IN
,
635 vu_kick_cb
, (void *)(long)qidx
);
637 dev
->remove_watch(dev
, vq
->kick_fd
);
643 vu_set_vring_call_exec(VuDev
*dev
, VhostUserMsg
*vmsg
)
645 int index
= vmsg
->payload
.u64
& VHOST_USER_VRING_IDX_MASK
;
647 DPRINT("u64: 0x%016"PRIx64
"\n", vmsg
->payload
.u64
);
649 if (!vu_check_queue_msg_file(dev
, vmsg
)) {
653 if (dev
->vq
[index
].call_fd
!= -1) {
654 close(dev
->vq
[index
].call_fd
);
655 dev
->vq
[index
].call_fd
= -1;
658 if (!(vmsg
->payload
.u64
& VHOST_USER_VRING_NOFD_MASK
)) {
659 dev
->vq
[index
].call_fd
= vmsg
->fds
[0];
662 DPRINT("Got call_fd: %d for vq: %d\n", vmsg
->fds
[0], index
);
668 vu_set_vring_err_exec(VuDev
*dev
, VhostUserMsg
*vmsg
)
670 int index
= vmsg
->payload
.u64
& VHOST_USER_VRING_IDX_MASK
;
672 DPRINT("u64: 0x%016"PRIx64
"\n", vmsg
->payload
.u64
);
674 if (!vu_check_queue_msg_file(dev
, vmsg
)) {
678 if (dev
->vq
[index
].err_fd
!= -1) {
679 close(dev
->vq
[index
].err_fd
);
680 dev
->vq
[index
].err_fd
= -1;
683 if (!(vmsg
->payload
.u64
& VHOST_USER_VRING_NOFD_MASK
)) {
684 dev
->vq
[index
].err_fd
= vmsg
->fds
[0];
691 vu_get_protocol_features_exec(VuDev
*dev
, VhostUserMsg
*vmsg
)
693 uint64_t features
= 1ULL << VHOST_USER_PROTOCOL_F_LOG_SHMFD
;
695 if (dev
->iface
->get_protocol_features
) {
696 features
|= dev
->iface
->get_protocol_features(dev
);
699 vmsg
->payload
.u64
= features
;
700 vmsg
->size
= sizeof(vmsg
->payload
.u64
);
706 vu_set_protocol_features_exec(VuDev
*dev
, VhostUserMsg
*vmsg
)
708 uint64_t features
= vmsg
->payload
.u64
;
710 DPRINT("u64: 0x%016"PRIx64
"\n", features
);
712 dev
->protocol_features
= vmsg
->payload
.u64
;
714 if (dev
->iface
->set_protocol_features
) {
715 dev
->iface
->set_protocol_features(dev
, features
);
722 vu_get_queue_num_exec(VuDev
*dev
, VhostUserMsg
*vmsg
)
724 DPRINT("Function %s() not implemented yet.\n", __func__
);
729 vu_set_vring_enable_exec(VuDev
*dev
, VhostUserMsg
*vmsg
)
731 unsigned int index
= vmsg
->payload
.state
.index
;
732 unsigned int enable
= vmsg
->payload
.state
.num
;
734 DPRINT("State.index: %d\n", index
);
735 DPRINT("State.enable: %d\n", enable
);
737 if (index
>= VHOST_MAX_NR_VIRTQUEUE
) {
738 vu_panic(dev
, "Invalid vring_enable index: %u", index
);
742 dev
->vq
[index
].enable
= enable
;
747 vu_process_message(VuDev
*dev
, VhostUserMsg
*vmsg
)
751 /* Print out generic part of the request. */
752 DPRINT("================ Vhost user message ================\n");
753 DPRINT("Request: %s (%d)\n", vu_request_to_string(vmsg
->request
),
755 DPRINT("Flags: 0x%x\n", vmsg
->flags
);
756 DPRINT("Size: %d\n", vmsg
->size
);
761 for (i
= 0; i
< vmsg
->fd_num
; i
++) {
762 DPRINT(" %d", vmsg
->fds
[i
]);
767 if (dev
->iface
->process_msg
&&
768 dev
->iface
->process_msg(dev
, vmsg
, &do_reply
)) {
772 switch (vmsg
->request
) {
773 case VHOST_USER_GET_FEATURES
:
774 return vu_get_features_exec(dev
, vmsg
);
775 case VHOST_USER_SET_FEATURES
:
776 return vu_set_features_exec(dev
, vmsg
);
777 case VHOST_USER_GET_PROTOCOL_FEATURES
:
778 return vu_get_protocol_features_exec(dev
, vmsg
);
779 case VHOST_USER_SET_PROTOCOL_FEATURES
:
780 return vu_set_protocol_features_exec(dev
, vmsg
);
781 case VHOST_USER_SET_OWNER
:
782 return vu_set_owner_exec(dev
, vmsg
);
783 case VHOST_USER_RESET_OWNER
:
784 return vu_reset_device_exec(dev
, vmsg
);
785 case VHOST_USER_SET_MEM_TABLE
:
786 return vu_set_mem_table_exec(dev
, vmsg
);
787 case VHOST_USER_SET_LOG_BASE
:
788 return vu_set_log_base_exec(dev
, vmsg
);
789 case VHOST_USER_SET_LOG_FD
:
790 return vu_set_log_fd_exec(dev
, vmsg
);
791 case VHOST_USER_SET_VRING_NUM
:
792 return vu_set_vring_num_exec(dev
, vmsg
);
793 case VHOST_USER_SET_VRING_ADDR
:
794 return vu_set_vring_addr_exec(dev
, vmsg
);
795 case VHOST_USER_SET_VRING_BASE
:
796 return vu_set_vring_base_exec(dev
, vmsg
);
797 case VHOST_USER_GET_VRING_BASE
:
798 return vu_get_vring_base_exec(dev
, vmsg
);
799 case VHOST_USER_SET_VRING_KICK
:
800 return vu_set_vring_kick_exec(dev
, vmsg
);
801 case VHOST_USER_SET_VRING_CALL
:
802 return vu_set_vring_call_exec(dev
, vmsg
);
803 case VHOST_USER_SET_VRING_ERR
:
804 return vu_set_vring_err_exec(dev
, vmsg
);
805 case VHOST_USER_GET_QUEUE_NUM
:
806 return vu_get_queue_num_exec(dev
, vmsg
);
807 case VHOST_USER_SET_VRING_ENABLE
:
808 return vu_set_vring_enable_exec(dev
, vmsg
);
810 vmsg_close_fds(vmsg
);
811 vu_panic(dev
, "Unhandled request: %d", vmsg
->request
);
818 vu_dispatch(VuDev
*dev
)
820 VhostUserMsg vmsg
= { 0, };
822 bool success
= false;
824 if (!vu_message_read(dev
, dev
->sock
, &vmsg
)) {
828 reply_requested
= vu_process_message(dev
, &vmsg
);
829 if (!reply_requested
) {
834 if (!vu_message_write(dev
, dev
->sock
, &vmsg
)) {
846 vu_deinit(VuDev
*dev
)
850 for (i
= 0; i
< dev
->nregions
; i
++) {
851 VuDevRegion
*r
= &dev
->regions
[i
];
852 void *m
= (void *) (uintptr_t) r
->mmap_addr
;
853 if (m
!= MAP_FAILED
) {
854 munmap(m
, r
->size
+ r
->mmap_offset
);
859 for (i
= 0; i
< VHOST_MAX_NR_VIRTQUEUE
; i
++) {
860 VuVirtq
*vq
= &dev
->vq
[i
];
862 if (vq
->call_fd
!= -1) {
867 if (vq
->kick_fd
!= -1) {
872 if (vq
->err_fd
!= -1) {
881 if (dev
->sock
!= -1) {
890 vu_set_watch_cb set_watch
,
891 vu_remove_watch_cb remove_watch
,
892 const VuDevIface
*iface
)
898 assert(remove_watch
);
902 memset(dev
, 0, sizeof(*dev
));
906 dev
->set_watch
= set_watch
;
907 dev
->remove_watch
= remove_watch
;
909 dev
->log_call_fd
= -1;
910 for (i
= 0; i
< VHOST_MAX_NR_VIRTQUEUE
; i
++) {
911 dev
->vq
[i
] = (VuVirtq
) {
912 .call_fd
= -1, .kick_fd
= -1, .err_fd
= -1,
913 .notification
= true,
919 vu_get_queue(VuDev
*dev
, int qidx
)
921 assert(qidx
< VHOST_MAX_NR_VIRTQUEUE
);
922 return &dev
->vq
[qidx
];
926 vu_queue_enabled(VuDev
*dev
, VuVirtq
*vq
)
931 static inline uint16_t
932 vring_avail_flags(VuVirtq
*vq
)
934 return vq
->vring
.avail
->flags
;
937 static inline uint16_t
938 vring_avail_idx(VuVirtq
*vq
)
940 vq
->shadow_avail_idx
= vq
->vring
.avail
->idx
;
942 return vq
->shadow_avail_idx
;
945 static inline uint16_t
946 vring_avail_ring(VuVirtq
*vq
, int i
)
948 return vq
->vring
.avail
->ring
[i
];
951 static inline uint16_t
952 vring_get_used_event(VuVirtq
*vq
)
954 return vring_avail_ring(vq
, vq
->vring
.num
);
958 virtqueue_num_heads(VuDev
*dev
, VuVirtq
*vq
, unsigned int idx
)
960 uint16_t num_heads
= vring_avail_idx(vq
) - idx
;
962 /* Check it isn't doing very strange things with descriptor numbers. */
963 if (num_heads
> vq
->vring
.num
) {
964 vu_panic(dev
, "Guest moved used index from %u to %u",
965 idx
, vq
->shadow_avail_idx
);
969 /* On success, callers read a descriptor at vq->last_avail_idx.
970 * Make sure descriptor read does not bypass avail index read. */
978 virtqueue_get_head(VuDev
*dev
, VuVirtq
*vq
,
979 unsigned int idx
, unsigned int *head
)
981 /* Grab the next descriptor number they're advertising, and increment
982 * the index we've seen. */
983 *head
= vring_avail_ring(vq
, idx
% vq
->vring
.num
);
985 /* If their number is silly, that's a fatal mistake. */
986 if (*head
>= vq
->vring
.num
) {
987 vu_panic(dev
, "Guest says index %u is available", head
);
995 VIRTQUEUE_READ_DESC_ERROR
= -1,
996 VIRTQUEUE_READ_DESC_DONE
= 0, /* end of chain */
997 VIRTQUEUE_READ_DESC_MORE
= 1, /* more buffers in chain */
1001 virtqueue_read_next_desc(VuDev
*dev
, struct vring_desc
*desc
,
1002 int i
, unsigned int max
, unsigned int *next
)
1004 /* If this descriptor says it doesn't chain, we're done. */
1005 if (!(desc
[i
].flags
& VRING_DESC_F_NEXT
)) {
1006 return VIRTQUEUE_READ_DESC_DONE
;
1009 /* Check they're not leading us off end of descriptors. */
1010 *next
= desc
[i
].next
;
1011 /* Make sure compiler knows to grab that: we don't want it changing! */
1015 vu_panic(dev
, "Desc next is %u", next
);
1016 return VIRTQUEUE_READ_DESC_ERROR
;
1019 return VIRTQUEUE_READ_DESC_MORE
;
1023 vu_queue_get_avail_bytes(VuDev
*dev
, VuVirtq
*vq
, unsigned int *in_bytes
,
1024 unsigned int *out_bytes
,
1025 unsigned max_in_bytes
, unsigned max_out_bytes
)
1028 unsigned int total_bufs
, in_total
, out_total
;
1031 idx
= vq
->last_avail_idx
;
1033 total_bufs
= in_total
= out_total
= 0;
1034 if (unlikely(dev
->broken
) ||
1035 unlikely(!vq
->vring
.avail
)) {
1039 while ((rc
= virtqueue_num_heads(dev
, vq
, idx
)) > 0) {
1040 unsigned int max
, num_bufs
, indirect
= 0;
1041 struct vring_desc
*desc
;
1044 max
= vq
->vring
.num
;
1045 num_bufs
= total_bufs
;
1046 if (!virtqueue_get_head(dev
, vq
, idx
++, &i
)) {
1049 desc
= vq
->vring
.desc
;
1051 if (desc
[i
].flags
& VRING_DESC_F_INDIRECT
) {
1052 if (desc
[i
].len
% sizeof(struct vring_desc
)) {
1053 vu_panic(dev
, "Invalid size for indirect buffer table");
1057 /* If we've got too many, that implies a descriptor loop. */
1058 if (num_bufs
>= max
) {
1059 vu_panic(dev
, "Looped descriptor");
1063 /* loop over the indirect descriptor table */
1065 max
= desc
[i
].len
/ sizeof(struct vring_desc
);
1066 desc
= vu_gpa_to_va(dev
, desc
[i
].addr
);
1071 /* If we've got too many, that implies a descriptor loop. */
1072 if (++num_bufs
> max
) {
1073 vu_panic(dev
, "Looped descriptor");
1077 if (desc
[i
].flags
& VRING_DESC_F_WRITE
) {
1078 in_total
+= desc
[i
].len
;
1080 out_total
+= desc
[i
].len
;
1082 if (in_total
>= max_in_bytes
&& out_total
>= max_out_bytes
) {
1085 rc
= virtqueue_read_next_desc(dev
, desc
, i
, max
, &i
);
1086 } while (rc
== VIRTQUEUE_READ_DESC_MORE
);
1088 if (rc
== VIRTQUEUE_READ_DESC_ERROR
) {
1093 total_bufs
= num_bufs
;
1103 *in_bytes
= in_total
;
1106 *out_bytes
= out_total
;
1111 in_total
= out_total
= 0;
1116 vu_queue_avail_bytes(VuDev
*dev
, VuVirtq
*vq
, unsigned int in_bytes
,
1117 unsigned int out_bytes
)
1119 unsigned int in_total
, out_total
;
1121 vu_queue_get_avail_bytes(dev
, vq
, &in_total
, &out_total
,
1122 in_bytes
, out_bytes
);
1124 return in_bytes
<= in_total
&& out_bytes
<= out_total
;
1127 /* Fetch avail_idx from VQ memory only when we really need to know if
1128 * guest has added some buffers. */
1130 vu_queue_empty(VuDev
*dev
, VuVirtq
*vq
)
1132 if (unlikely(dev
->broken
) ||
1133 unlikely(!vq
->vring
.avail
)) {
1137 if (vq
->shadow_avail_idx
!= vq
->last_avail_idx
) {
1141 return vring_avail_idx(vq
) == vq
->last_avail_idx
;
1145 bool has_feature(uint64_t features
, unsigned int fbit
)
1148 return !!(features
& (1ULL << fbit
));
1152 bool vu_has_feature(VuDev
*dev
,
1155 return has_feature(dev
->features
, fbit
);
1159 vring_notify(VuDev
*dev
, VuVirtq
*vq
)
1164 /* We need to expose used array entries before checking used event. */
1167 /* Always notify when queue is empty (when feature acknowledge) */
1168 if (vu_has_feature(dev
, VIRTIO_F_NOTIFY_ON_EMPTY
) &&
1169 !vq
->inuse
&& vu_queue_empty(dev
, vq
)) {
1173 if (!vu_has_feature(dev
, VIRTIO_RING_F_EVENT_IDX
)) {
1174 return !(vring_avail_flags(vq
) & VRING_AVAIL_F_NO_INTERRUPT
);
1177 v
= vq
->signalled_used_valid
;
1178 vq
->signalled_used_valid
= true;
1179 old
= vq
->signalled_used
;
1180 new = vq
->signalled_used
= vq
->used_idx
;
1181 return !v
|| vring_need_event(vring_get_used_event(vq
), new, old
);
1185 vu_queue_notify(VuDev
*dev
, VuVirtq
*vq
)
1187 if (unlikely(dev
->broken
) ||
1188 unlikely(!vq
->vring
.avail
)) {
1192 if (!vring_notify(dev
, vq
)) {
1193 DPRINT("skipped notify...\n");
1197 if (eventfd_write(vq
->call_fd
, 1) < 0) {
1198 vu_panic(dev
, "Error writing eventfd: %s", strerror(errno
));
1203 vring_used_flags_set_bit(VuVirtq
*vq
, int mask
)
1207 flags
= (uint16_t *)((char*)vq
->vring
.used
+
1208 offsetof(struct vring_used
, flags
));
1213 vring_used_flags_unset_bit(VuVirtq
*vq
, int mask
)
1217 flags
= (uint16_t *)((char*)vq
->vring
.used
+
1218 offsetof(struct vring_used
, flags
));
1223 vring_set_avail_event(VuVirtq
*vq
, uint16_t val
)
1225 if (!vq
->notification
) {
1229 *((uint16_t *) &vq
->vring
.used
->ring
[vq
->vring
.num
]) = val
;
1233 vu_queue_set_notification(VuDev
*dev
, VuVirtq
*vq
, int enable
)
1235 vq
->notification
= enable
;
1236 if (vu_has_feature(dev
, VIRTIO_RING_F_EVENT_IDX
)) {
1237 vring_set_avail_event(vq
, vring_avail_idx(vq
));
1238 } else if (enable
) {
1239 vring_used_flags_unset_bit(vq
, VRING_USED_F_NO_NOTIFY
);
1241 vring_used_flags_set_bit(vq
, VRING_USED_F_NO_NOTIFY
);
1244 /* Expose avail event/used flags before caller checks the avail idx. */
1250 virtqueue_map_desc(VuDev
*dev
,
1251 unsigned int *p_num_sg
, struct iovec
*iov
,
1252 unsigned int max_num_sg
, bool is_write
,
1253 uint64_t pa
, size_t sz
)
1255 unsigned num_sg
= *p_num_sg
;
1257 assert(num_sg
<= max_num_sg
);
1260 vu_panic(dev
, "virtio: zero sized buffers are not allowed");
1264 iov
[num_sg
].iov_base
= vu_gpa_to_va(dev
, pa
);
1265 iov
[num_sg
].iov_len
= sz
;
1271 /* Round number down to multiple */
1272 #define ALIGN_DOWN(n, m) ((n) / (m) * (m))
1274 /* Round number up to multiple */
1275 #define ALIGN_UP(n, m) ALIGN_DOWN((n) + (m) - 1, (m))
1278 virtqueue_alloc_element(size_t sz
,
1279 unsigned out_num
, unsigned in_num
)
1281 VuVirtqElement
*elem
;
1282 size_t in_sg_ofs
= ALIGN_UP(sz
, __alignof__(elem
->in_sg
[0]));
1283 size_t out_sg_ofs
= in_sg_ofs
+ in_num
* sizeof(elem
->in_sg
[0]);
1284 size_t out_sg_end
= out_sg_ofs
+ out_num
* sizeof(elem
->out_sg
[0]);
1286 assert(sz
>= sizeof(VuVirtqElement
));
1287 elem
= malloc(out_sg_end
);
1288 elem
->out_num
= out_num
;
1289 elem
->in_num
= in_num
;
1290 elem
->in_sg
= (void *)elem
+ in_sg_ofs
;
1291 elem
->out_sg
= (void *)elem
+ out_sg_ofs
;
1296 vu_queue_pop(VuDev
*dev
, VuVirtq
*vq
, size_t sz
)
1298 unsigned int i
, head
, max
;
1299 VuVirtqElement
*elem
;
1300 unsigned out_num
, in_num
;
1301 struct iovec iov
[VIRTQUEUE_MAX_SIZE
];
1302 struct vring_desc
*desc
;
1305 if (unlikely(dev
->broken
) ||
1306 unlikely(!vq
->vring
.avail
)) {
1310 if (vu_queue_empty(dev
, vq
)) {
1313 /* Needed after virtio_queue_empty(), see comment in
1314 * virtqueue_num_heads(). */
1317 /* When we start there are none of either input nor output. */
1318 out_num
= in_num
= 0;
1320 max
= vq
->vring
.num
;
1321 if (vq
->inuse
>= vq
->vring
.num
) {
1322 vu_panic(dev
, "Virtqueue size exceeded");
1326 if (!virtqueue_get_head(dev
, vq
, vq
->last_avail_idx
++, &head
)) {
1330 if (vu_has_feature(dev
, VIRTIO_RING_F_EVENT_IDX
)) {
1331 vring_set_avail_event(vq
, vq
->last_avail_idx
);
1335 desc
= vq
->vring
.desc
;
1336 if (desc
[i
].flags
& VRING_DESC_F_INDIRECT
) {
1337 if (desc
[i
].len
% sizeof(struct vring_desc
)) {
1338 vu_panic(dev
, "Invalid size for indirect buffer table");
1341 /* loop over the indirect descriptor table */
1342 max
= desc
[i
].len
/ sizeof(struct vring_desc
);
1343 desc
= vu_gpa_to_va(dev
, desc
[i
].addr
);
1347 /* Collect all the descriptors */
1349 if (desc
[i
].flags
& VRING_DESC_F_WRITE
) {
1350 virtqueue_map_desc(dev
, &in_num
, iov
+ out_num
,
1351 VIRTQUEUE_MAX_SIZE
- out_num
, true,
1352 desc
[i
].addr
, desc
[i
].len
);
1355 vu_panic(dev
, "Incorrect order for descriptors");
1358 virtqueue_map_desc(dev
, &out_num
, iov
,
1359 VIRTQUEUE_MAX_SIZE
, false,
1360 desc
[i
].addr
, desc
[i
].len
);
1363 /* If we've got too many, that implies a descriptor loop. */
1364 if ((in_num
+ out_num
) > max
) {
1365 vu_panic(dev
, "Looped descriptor");
1367 rc
= virtqueue_read_next_desc(dev
, desc
, i
, max
, &i
);
1368 } while (rc
== VIRTQUEUE_READ_DESC_MORE
);
1370 if (rc
== VIRTQUEUE_READ_DESC_ERROR
) {
1374 /* Now copy what we have collected and mapped */
1375 elem
= virtqueue_alloc_element(sz
, out_num
, in_num
);
1377 for (i
= 0; i
< out_num
; i
++) {
1378 elem
->out_sg
[i
] = iov
[i
];
1380 for (i
= 0; i
< in_num
; i
++) {
1381 elem
->in_sg
[i
] = iov
[out_num
+ i
];
1390 vu_queue_rewind(VuDev
*dev
, VuVirtq
*vq
, unsigned int num
)
1392 if (num
> vq
->inuse
) {
1395 vq
->last_avail_idx
-= num
;
1401 void vring_used_write(VuDev
*dev
, VuVirtq
*vq
,
1402 struct vring_used_elem
*uelem
, int i
)
1404 struct vring_used
*used
= vq
->vring
.used
;
1406 used
->ring
[i
] = *uelem
;
1407 vu_log_write(dev
, vq
->vring
.log_guest_addr
+
1408 offsetof(struct vring_used
, ring
[i
]),
1409 sizeof(used
->ring
[i
]));
1414 vu_log_queue_fill(VuDev
*dev
, VuVirtq
*vq
,
1415 const VuVirtqElement
*elem
,
1418 struct vring_desc
*desc
= vq
->vring
.desc
;
1419 unsigned int i
, max
, min
;
1420 unsigned num_bufs
= 0;
1422 max
= vq
->vring
.num
;
1425 if (desc
[i
].flags
& VRING_DESC_F_INDIRECT
) {
1426 if (desc
[i
].len
% sizeof(struct vring_desc
)) {
1427 vu_panic(dev
, "Invalid size for indirect buffer table");
1430 /* loop over the indirect descriptor table */
1431 max
= desc
[i
].len
/ sizeof(struct vring_desc
);
1432 desc
= vu_gpa_to_va(dev
, desc
[i
].addr
);
1437 if (++num_bufs
> max
) {
1438 vu_panic(dev
, "Looped descriptor");
1442 if (desc
[i
].flags
& VRING_DESC_F_WRITE
) {
1443 min
= MIN(desc
[i
].len
, len
);
1444 vu_log_write(dev
, desc
[i
].addr
, min
);
1449 (virtqueue_read_next_desc(dev
, desc
, i
, max
, &i
)
1450 == VIRTQUEUE_READ_DESC_MORE
));
1454 vu_queue_fill(VuDev
*dev
, VuVirtq
*vq
,
1455 const VuVirtqElement
*elem
,
1456 unsigned int len
, unsigned int idx
)
1458 struct vring_used_elem uelem
;
1460 if (unlikely(dev
->broken
) ||
1461 unlikely(!vq
->vring
.avail
)) {
1465 vu_log_queue_fill(dev
, vq
, elem
, len
);
1467 idx
= (idx
+ vq
->used_idx
) % vq
->vring
.num
;
1469 uelem
.id
= elem
->index
;
1471 vring_used_write(dev
, vq
, &uelem
, idx
);
1475 void vring_used_idx_set(VuDev
*dev
, VuVirtq
*vq
, uint16_t val
)
1477 vq
->vring
.used
->idx
= val
;
1479 vq
->vring
.log_guest_addr
+ offsetof(struct vring_used
, idx
),
1480 sizeof(vq
->vring
.used
->idx
));
1486 vu_queue_flush(VuDev
*dev
, VuVirtq
*vq
, unsigned int count
)
1490 if (unlikely(dev
->broken
) ||
1491 unlikely(!vq
->vring
.avail
)) {
1495 /* Make sure buffer is written before we update index. */
1500 vring_used_idx_set(dev
, vq
, new);
1502 if (unlikely((int16_t)(new - vq
->signalled_used
) < (uint16_t)(new - old
))) {
1503 vq
->signalled_used_valid
= false;
1508 vu_queue_push(VuDev
*dev
, VuVirtq
*vq
,
1509 const VuVirtqElement
*elem
, unsigned int len
)
1511 vu_queue_fill(dev
, vq
, elem
, len
, 0);
1512 vu_queue_flush(dev
, vq
, 1);