4 * Copyright IBM, Corp. 2007
5 * Copyright (c) 2016 Red Hat, Inc.
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"
45 #include "libvhost-user.h"
47 /* usually provided by GLib */
49 #define MIN(x, y) ({ \
50 typeof(x) _min1 = (x); \
51 typeof(y) _min2 = (y); \
52 (void) (&_min1 == &_min2); \
53 _min1 < _min2 ? _min1 : _min2; })
56 #define VHOST_USER_HDR_SIZE offsetof(VhostUserMsg, payload.u64)
58 /* The version of the protocol we support */
59 #define VHOST_USER_VERSION 1
60 #define LIBVHOST_USER_DEBUG 0
64 if (LIBVHOST_USER_DEBUG) { \
65 fprintf(stderr, __VA_ARGS__); \
70 vu_request_to_string(unsigned int req)
72 #define REQ(req) [req] = #req
73 static const char *vu_request_str[] = {
75 REQ(VHOST_USER_GET_FEATURES),
76 REQ(VHOST_USER_SET_FEATURES),
77 REQ(VHOST_USER_SET_OWNER),
78 REQ(VHOST_USER_RESET_OWNER),
79 REQ(VHOST_USER_SET_MEM_TABLE),
80 REQ(VHOST_USER_SET_LOG_BASE),
81 REQ(VHOST_USER_SET_LOG_FD),
82 REQ(VHOST_USER_SET_VRING_NUM),
83 REQ(VHOST_USER_SET_VRING_ADDR),
84 REQ(VHOST_USER_SET_VRING_BASE),
85 REQ(VHOST_USER_GET_VRING_BASE),
86 REQ(VHOST_USER_SET_VRING_KICK),
87 REQ(VHOST_USER_SET_VRING_CALL),
88 REQ(VHOST_USER_SET_VRING_ERR),
89 REQ(VHOST_USER_GET_PROTOCOL_FEATURES),
90 REQ(VHOST_USER_SET_PROTOCOL_FEATURES),
91 REQ(VHOST_USER_GET_QUEUE_NUM),
92 REQ(VHOST_USER_SET_VRING_ENABLE),
93 REQ(VHOST_USER_SEND_RARP),
94 REQ(VHOST_USER_NET_SET_MTU),
95 REQ(VHOST_USER_SET_SLAVE_REQ_FD),
96 REQ(VHOST_USER_IOTLB_MSG),
97 REQ(VHOST_USER_SET_VRING_ENDIAN),
98 REQ(VHOST_USER_GET_CONFIG),
99 REQ(VHOST_USER_SET_CONFIG),
100 REQ(VHOST_USER_POSTCOPY_ADVISE),
101 REQ(VHOST_USER_POSTCOPY_LISTEN),
106 if (req < VHOST_USER_MAX) {
107 return vu_request_str[req];
114 vu_panic(VuDev *dev, const char *msg, ...)
120 if (vasprintf(&buf, msg, ap) < 0) {
126 dev->panic(dev, buf);
129 /* FIXME: find a way to call virtio_error? */
132 /* Translate guest physical address to our virtual address. */
134 vu_gpa_to_va(VuDev *dev, uint64_t *plen, uint64_t guest_addr)
142 /* Find matching memory region. */
143 for (i = 0; i < dev->nregions; i++) {
144 VuDevRegion *r = &dev->regions[i];
146 if ((guest_addr >= r->gpa) && (guest_addr < (r->gpa + r->size))) {
147 if ((guest_addr + *plen) > (r->gpa + r->size)) {
148 *plen = r->gpa + r->size - guest_addr;
150 return (void *)(uintptr_t)
151 guest_addr - r->gpa + r->mmap_addr + r->mmap_offset;
158 /* Translate qemu virtual address to our virtual address. */
160 qva_to_va(VuDev *dev, uint64_t qemu_addr)
164 /* Find matching memory region. */
165 for (i = 0; i < dev->nregions; i++) {
166 VuDevRegion *r = &dev->regions[i];
168 if ((qemu_addr >= r->qva) && (qemu_addr < (r->qva + r->size))) {
169 return (void *)(uintptr_t)
170 qemu_addr - r->qva + r->mmap_addr + r->mmap_offset;
178 vmsg_close_fds(VhostUserMsg *vmsg)
182 for (i = 0; i < vmsg->fd_num; i++) {
188 vu_message_read(VuDev *dev, int conn_fd, VhostUserMsg *vmsg)
190 char control[CMSG_SPACE(VHOST_MEMORY_MAX_NREGIONS * sizeof(int))] = { };
192 .iov_base = (char *)vmsg,
193 .iov_len = VHOST_USER_HDR_SIZE,
195 struct msghdr msg = {
198 .msg_control = control,
199 .msg_controllen = sizeof(control),
202 struct cmsghdr *cmsg;
206 rc = recvmsg(conn_fd, &msg, 0);
207 } while (rc < 0 && (errno == EINTR || errno == EAGAIN));
210 vu_panic(dev, "Error while recvmsg: %s", strerror(errno));
215 for (cmsg = CMSG_FIRSTHDR(&msg);
217 cmsg = CMSG_NXTHDR(&msg, cmsg))
219 if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) {
220 fd_size = cmsg->cmsg_len - CMSG_LEN(0);
221 vmsg->fd_num = fd_size / sizeof(int);
222 memcpy(vmsg->fds, CMSG_DATA(cmsg), fd_size);
227 if (vmsg->size > sizeof(vmsg->payload)) {
229 "Error: too big message request: %d, size: vmsg->size: %u, "
230 "while sizeof(vmsg->payload) = %zu\n",
231 vmsg->request, vmsg->size, sizeof(vmsg->payload));
237 rc = read(conn_fd, &vmsg->payload, vmsg->size);
238 } while (rc < 0 && (errno == EINTR || errno == EAGAIN));
241 vu_panic(dev, "Error while reading: %s", strerror(errno));
245 assert(rc == vmsg->size);
251 vmsg_close_fds(vmsg);
257 vu_message_write(VuDev *dev, int conn_fd, VhostUserMsg *vmsg)
260 uint8_t *p = (uint8_t *)vmsg;
261 char control[CMSG_SPACE(VHOST_MEMORY_MAX_NREGIONS * sizeof(int))] = { };
263 .iov_base = (char *)vmsg,
264 .iov_len = VHOST_USER_HDR_SIZE,
266 struct msghdr msg = {
269 .msg_control = control,
271 struct cmsghdr *cmsg;
273 memset(control, 0, sizeof(control));
274 assert(vmsg->fd_num <= VHOST_MEMORY_MAX_NREGIONS);
275 if (vmsg->fd_num > 0) {
276 size_t fdsize = vmsg->fd_num * sizeof(int);
277 msg.msg_controllen = CMSG_SPACE(fdsize);
278 cmsg = CMSG_FIRSTHDR(&msg);
279 cmsg->cmsg_len = CMSG_LEN(fdsize);
280 cmsg->cmsg_level = SOL_SOCKET;
281 cmsg->cmsg_type = SCM_RIGHTS;
282 memcpy(CMSG_DATA(cmsg), vmsg->fds, fdsize);
284 msg.msg_controllen = 0;
287 /* Set the version in the flags when sending the reply */
288 vmsg->flags &= ~VHOST_USER_VERSION_MASK;
289 vmsg->flags |= VHOST_USER_VERSION;
290 vmsg->flags |= VHOST_USER_REPLY_MASK;
293 rc = sendmsg(conn_fd, &msg, 0);
294 } while (rc < 0 && (errno == EINTR || errno == EAGAIN));
298 rc = write(conn_fd, vmsg->data, vmsg->size);
300 rc = write(conn_fd, p + VHOST_USER_HDR_SIZE, vmsg->size);
302 } while (rc < 0 && (errno == EINTR || errno == EAGAIN));
305 vu_panic(dev, "Error while writing: %s", strerror(errno));
312 /* Kick the log_call_fd if required. */
314 vu_log_kick(VuDev *dev)
316 if (dev->log_call_fd != -1) {
317 DPRINT("Kicking the QEMU's log...\n");
318 if (eventfd_write(dev->log_call_fd, 1) < 0) {
319 vu_panic(dev, "Error writing eventfd: %s", strerror(errno));
325 vu_log_page(uint8_t *log_table, uint64_t page)
327 DPRINT("Logged dirty guest page: %"PRId64"\n", page);
328 atomic_or(&log_table[page / 8], 1 << (page % 8));
332 vu_log_write(VuDev *dev, uint64_t address, uint64_t length)
336 if (!(dev->features & (1ULL << VHOST_F_LOG_ALL)) ||
337 !dev->log_table || !length) {
341 assert(dev->log_size > ((address + length - 1) / VHOST_LOG_PAGE / 8));
343 page = address / VHOST_LOG_PAGE;
344 while (page * VHOST_LOG_PAGE < address + length) {
345 vu_log_page(dev->log_table, page);
346 page += VHOST_LOG_PAGE;
353 vu_kick_cb(VuDev *dev, int condition, void *data)
355 int index = (intptr_t)data;
356 VuVirtq *vq = &dev->vq[index];
357 int sock = vq->kick_fd;
361 rc = eventfd_read(sock, &kick_data);
363 vu_panic(dev, "kick eventfd_read(): %s", strerror(errno));
364 dev->remove_watch(dev, dev->vq[index].kick_fd);
366 DPRINT("Got kick_data: %016"PRIx64" handler:%p idx:%d\n",
367 kick_data, vq->handler, index);
369 vq->handler(dev, index);
375 vu_get_features_exec(VuDev *dev, VhostUserMsg *vmsg)
378 1ULL << VHOST_F_LOG_ALL |
379 1ULL << VHOST_USER_F_PROTOCOL_FEATURES;
381 if (dev->iface->get_features) {
382 vmsg->payload.u64 |= dev->iface->get_features(dev);
385 vmsg->size = sizeof(vmsg->payload.u64);
388 DPRINT("Sending back to guest u64: 0x%016"PRIx64"\n", vmsg->payload.u64);
394 vu_set_enable_all_rings(VuDev *dev, bool enabled)
398 for (i = 0; i < VHOST_MAX_NR_VIRTQUEUE; i++) {
399 dev->vq[i].enable = enabled;
404 vu_set_features_exec(VuDev *dev, VhostUserMsg *vmsg)
406 DPRINT("u64: 0x%016"PRIx64"\n", vmsg->payload.u64);
408 dev->features = vmsg->payload.u64;
410 if (!(dev->features & VHOST_USER_F_PROTOCOL_FEATURES)) {
411 vu_set_enable_all_rings(dev, true);
414 if (dev->iface->set_features) {
415 dev->iface->set_features(dev, dev->features);
422 vu_set_owner_exec(VuDev *dev, VhostUserMsg *vmsg)
428 vu_close_log(VuDev *dev)
430 if (dev->log_table) {
431 if (munmap(dev->log_table, dev->log_size) != 0) {
432 perror("close log munmap() error");
435 dev->log_table = NULL;
437 if (dev->log_call_fd != -1) {
438 close(dev->log_call_fd);
439 dev->log_call_fd = -1;
444 vu_reset_device_exec(VuDev *dev, VhostUserMsg *vmsg)
446 vu_set_enable_all_rings(dev, false);
452 vu_set_mem_table_exec(VuDev *dev, VhostUserMsg *vmsg)
455 VhostUserMemory *memory = &vmsg->payload.memory;
457 for (i = 0; i < dev->nregions; i++) {
458 VuDevRegion *r = &dev->regions[i];
459 void *m = (void *) (uintptr_t) r->mmap_addr;
462 munmap(m, r->size + r->mmap_offset);
465 dev->nregions = memory->nregions;
467 DPRINT("Nregions: %d\n", memory->nregions);
468 for (i = 0; i < dev->nregions; i++) {
470 VhostUserMemoryRegion *msg_region = &memory->regions[i];
471 VuDevRegion *dev_region = &dev->regions[i];
473 DPRINT("Region %d\n", i);
474 DPRINT(" guest_phys_addr: 0x%016"PRIx64"\n",
475 msg_region->guest_phys_addr);
476 DPRINT(" memory_size: 0x%016"PRIx64"\n",
477 msg_region->memory_size);
478 DPRINT(" userspace_addr 0x%016"PRIx64"\n",
479 msg_region->userspace_addr);
480 DPRINT(" mmap_offset 0x%016"PRIx64"\n",
481 msg_region->mmap_offset);
483 dev_region->gpa = msg_region->guest_phys_addr;
484 dev_region->size = msg_region->memory_size;
485 dev_region->qva = msg_region->userspace_addr;
486 dev_region->mmap_offset = msg_region->mmap_offset;
488 /* We don't use offset argument of mmap() since the
489 * mapped address has to be page aligned, and we use huge
491 mmap_addr = mmap(0, dev_region->size + dev_region->mmap_offset,
492 PROT_READ | PROT_WRITE, MAP_SHARED,
495 if (mmap_addr == MAP_FAILED) {
496 vu_panic(dev, "region mmap error: %s", strerror(errno));
498 dev_region->mmap_addr = (uint64_t)(uintptr_t)mmap_addr;
499 DPRINT(" mmap_addr: 0x%016"PRIx64"\n",
500 dev_region->mmap_addr);
510 vu_set_log_base_exec(VuDev *dev, VhostUserMsg *vmsg)
513 uint64_t log_mmap_size, log_mmap_offset;
516 if (vmsg->fd_num != 1 ||
517 vmsg->size != sizeof(vmsg->payload.log)) {
518 vu_panic(dev, "Invalid log_base message");
523 log_mmap_offset = vmsg->payload.log.mmap_offset;
524 log_mmap_size = vmsg->payload.log.mmap_size;
525 DPRINT("Log mmap_offset: %"PRId64"\n", log_mmap_offset);
526 DPRINT("Log mmap_size: %"PRId64"\n", log_mmap_size);
528 rc = mmap(0, log_mmap_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd,
531 if (rc == MAP_FAILED) {
532 perror("log mmap error");
535 if (dev->log_table) {
536 munmap(dev->log_table, dev->log_size);
539 dev->log_size = log_mmap_size;
541 vmsg->size = sizeof(vmsg->payload.u64);
548 vu_set_log_fd_exec(VuDev *dev, VhostUserMsg *vmsg)
550 if (vmsg->fd_num != 1) {
551 vu_panic(dev, "Invalid log_fd message");
555 if (dev->log_call_fd != -1) {
556 close(dev->log_call_fd);
558 dev->log_call_fd = vmsg->fds[0];
559 DPRINT("Got log_call_fd: %d\n", vmsg->fds[0]);
565 vu_set_vring_num_exec(VuDev *dev, VhostUserMsg *vmsg)
567 unsigned int index = vmsg->payload.state.index;
568 unsigned int num = vmsg->payload.state.num;
570 DPRINT("State.index: %d\n", index);
571 DPRINT("State.num: %d\n", num);
572 dev->vq[index].vring.num = num;
578 vu_set_vring_addr_exec(VuDev *dev, VhostUserMsg *vmsg)
580 struct vhost_vring_addr *vra = &vmsg->payload.addr;
581 unsigned int index = vra->index;
582 VuVirtq *vq = &dev->vq[index];
584 DPRINT("vhost_vring_addr:\n");
585 DPRINT(" index: %d\n", vra->index);
586 DPRINT(" flags: %d\n", vra->flags);
587 DPRINT(" desc_user_addr: 0x%016llx\n", vra->desc_user_addr);
588 DPRINT(" used_user_addr: 0x%016llx\n", vra->used_user_addr);
589 DPRINT(" avail_user_addr: 0x%016llx\n", vra->avail_user_addr);
590 DPRINT(" log_guest_addr: 0x%016llx\n", vra->log_guest_addr);
592 vq->vring.flags = vra->flags;
593 vq->vring.desc = qva_to_va(dev, vra->desc_user_addr);
594 vq->vring.used = qva_to_va(dev, vra->used_user_addr);
595 vq->vring.avail = qva_to_va(dev, vra->avail_user_addr);
596 vq->vring.log_guest_addr = vra->log_guest_addr;
598 DPRINT("Setting virtq addresses:\n");
599 DPRINT(" vring_desc at %p\n", vq->vring.desc);
600 DPRINT(" vring_used at %p\n", vq->vring.used);
601 DPRINT(" vring_avail at %p\n", vq->vring.avail);
603 if (!(vq->vring.desc && vq->vring.used && vq->vring.avail)) {
604 vu_panic(dev, "Invalid vring_addr message");
608 vq->used_idx = vq->vring.used->idx;
610 if (vq->last_avail_idx != vq->used_idx) {
611 bool resume = dev->iface->queue_is_processed_in_order &&
612 dev->iface->queue_is_processed_in_order(dev, index);
614 DPRINT("Last avail index != used index: %u != %u%s\n",
615 vq->last_avail_idx, vq->used_idx,
616 resume ? ", resuming" : "");
619 vq->shadow_avail_idx = vq->last_avail_idx = vq->used_idx;
627 vu_set_vring_base_exec(VuDev *dev, VhostUserMsg *vmsg)
629 unsigned int index = vmsg->payload.state.index;
630 unsigned int num = vmsg->payload.state.num;
632 DPRINT("State.index: %d\n", index);
633 DPRINT("State.num: %d\n", num);
634 dev->vq[index].shadow_avail_idx = dev->vq[index].last_avail_idx = num;
640 vu_get_vring_base_exec(VuDev *dev, VhostUserMsg *vmsg)
642 unsigned int index = vmsg->payload.state.index;
644 DPRINT("State.index: %d\n", index);
645 vmsg->payload.state.num = dev->vq[index].last_avail_idx;
646 vmsg->size = sizeof(vmsg->payload.state);
648 dev->vq[index].started = false;
649 if (dev->iface->queue_set_started) {
650 dev->iface->queue_set_started(dev, index, false);
653 if (dev->vq[index].call_fd != -1) {
654 close(dev->vq[index].call_fd);
655 dev->vq[index].call_fd = -1;
657 if (dev->vq[index].kick_fd != -1) {
658 dev->remove_watch(dev, dev->vq[index].kick_fd);
659 close(dev->vq[index].kick_fd);
660 dev->vq[index].kick_fd = -1;
667 vu_check_queue_msg_file(VuDev *dev, VhostUserMsg *vmsg)
669 int index = vmsg->payload.u64 & VHOST_USER_VRING_IDX_MASK;
671 if (index >= VHOST_MAX_NR_VIRTQUEUE) {
672 vmsg_close_fds(vmsg);
673 vu_panic(dev, "Invalid queue index: %u", index);
677 if (vmsg->payload.u64 & VHOST_USER_VRING_NOFD_MASK ||
679 vmsg_close_fds(vmsg);
680 vu_panic(dev, "Invalid fds in request: %d", vmsg->request);
688 vu_set_vring_kick_exec(VuDev *dev, VhostUserMsg *vmsg)
690 int index = vmsg->payload.u64 & VHOST_USER_VRING_IDX_MASK;
692 DPRINT("u64: 0x%016"PRIx64"\n", vmsg->payload.u64);
694 if (!vu_check_queue_msg_file(dev, vmsg)) {
698 if (dev->vq[index].kick_fd != -1) {
699 dev->remove_watch(dev, dev->vq[index].kick_fd);
700 close(dev->vq[index].kick_fd);
701 dev->vq[index].kick_fd = -1;
704 if (!(vmsg->payload.u64 & VHOST_USER_VRING_NOFD_MASK)) {
705 dev->vq[index].kick_fd = vmsg->fds[0];
706 DPRINT("Got kick_fd: %d for vq: %d\n", vmsg->fds[0], index);
709 dev->vq[index].started = true;
710 if (dev->iface->queue_set_started) {
711 dev->iface->queue_set_started(dev, index, true);
714 if (dev->vq[index].kick_fd != -1 && dev->vq[index].handler) {
715 dev->set_watch(dev, dev->vq[index].kick_fd, VU_WATCH_IN,
716 vu_kick_cb, (void *)(long)index);
718 DPRINT("Waiting for kicks on fd: %d for vq: %d\n",
719 dev->vq[index].kick_fd, index);
725 void vu_set_queue_handler(VuDev *dev, VuVirtq *vq,
726 vu_queue_handler_cb handler)
728 int qidx = vq - dev->vq;
730 vq->handler = handler;
731 if (vq->kick_fd >= 0) {
733 dev->set_watch(dev, vq->kick_fd, VU_WATCH_IN,
734 vu_kick_cb, (void *)(long)qidx);
736 dev->remove_watch(dev, vq->kick_fd);
742 vu_set_vring_call_exec(VuDev *dev, VhostUserMsg *vmsg)
744 int index = vmsg->payload.u64 & VHOST_USER_VRING_IDX_MASK;
746 DPRINT("u64: 0x%016"PRIx64"\n", vmsg->payload.u64);
748 if (!vu_check_queue_msg_file(dev, vmsg)) {
752 if (dev->vq[index].call_fd != -1) {
753 close(dev->vq[index].call_fd);
754 dev->vq[index].call_fd = -1;
757 if (!(vmsg->payload.u64 & VHOST_USER_VRING_NOFD_MASK)) {
758 dev->vq[index].call_fd = vmsg->fds[0];
761 DPRINT("Got call_fd: %d for vq: %d\n", vmsg->fds[0], index);
767 vu_set_vring_err_exec(VuDev *dev, VhostUserMsg *vmsg)
769 int index = vmsg->payload.u64 & VHOST_USER_VRING_IDX_MASK;
771 DPRINT("u64: 0x%016"PRIx64"\n", vmsg->payload.u64);
773 if (!vu_check_queue_msg_file(dev, vmsg)) {
777 if (dev->vq[index].err_fd != -1) {
778 close(dev->vq[index].err_fd);
779 dev->vq[index].err_fd = -1;
782 if (!(vmsg->payload.u64 & VHOST_USER_VRING_NOFD_MASK)) {
783 dev->vq[index].err_fd = vmsg->fds[0];
790 vu_get_protocol_features_exec(VuDev *dev, VhostUserMsg *vmsg)
792 uint64_t features = 1ULL << VHOST_USER_PROTOCOL_F_LOG_SHMFD |
793 1ULL << VHOST_USER_PROTOCOL_F_SLAVE_REQ;
795 if (dev->iface->get_protocol_features) {
796 features |= dev->iface->get_protocol_features(dev);
799 vmsg->payload.u64 = features;
800 vmsg->size = sizeof(vmsg->payload.u64);
807 vu_set_protocol_features_exec(VuDev *dev, VhostUserMsg *vmsg)
809 uint64_t features = vmsg->payload.u64;
811 DPRINT("u64: 0x%016"PRIx64"\n", features);
813 dev->protocol_features = vmsg->payload.u64;
815 if (dev->iface->set_protocol_features) {
816 dev->iface->set_protocol_features(dev, features);
823 vu_get_queue_num_exec(VuDev *dev, VhostUserMsg *vmsg)
825 DPRINT("Function %s() not implemented yet.\n", __func__);
830 vu_set_vring_enable_exec(VuDev *dev, VhostUserMsg *vmsg)
832 unsigned int index = vmsg->payload.state.index;
833 unsigned int enable = vmsg->payload.state.num;
835 DPRINT("State.index: %d\n", index);
836 DPRINT("State.enable: %d\n", enable);
838 if (index >= VHOST_MAX_NR_VIRTQUEUE) {
839 vu_panic(dev, "Invalid vring_enable index: %u", index);
843 dev->vq[index].enable = enable;
848 vu_set_slave_req_fd(VuDev *dev, VhostUserMsg *vmsg)
850 if (vmsg->fd_num != 1) {
851 vu_panic(dev, "Invalid slave_req_fd message (%d fd's)", vmsg->fd_num);
855 if (dev->slave_fd != -1) {
856 close(dev->slave_fd);
858 dev->slave_fd = vmsg->fds[0];
859 DPRINT("Got slave_fd: %d\n", vmsg->fds[0]);
865 vu_get_config(VuDev *dev, VhostUserMsg *vmsg)
869 if (dev->iface->get_config) {
870 ret = dev->iface->get_config(dev, vmsg->payload.config.region,
871 vmsg->payload.config.size);
875 /* resize to zero to indicate an error to master */
883 vu_set_config(VuDev *dev, VhostUserMsg *vmsg)
887 if (dev->iface->set_config) {
888 ret = dev->iface->set_config(dev, vmsg->payload.config.region,
889 vmsg->payload.config.offset,
890 vmsg->payload.config.size,
891 vmsg->payload.config.flags);
893 vu_panic(dev, "Set virtio configuration space failed");
901 vu_set_postcopy_advise(VuDev *dev, VhostUserMsg *vmsg)
903 dev->postcopy_ufd = -1;
905 struct uffdio_api api_struct;
907 dev->postcopy_ufd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK);
911 if (dev->postcopy_ufd == -1) {
912 vu_panic(dev, "Userfaultfd not available: %s", strerror(errno));
917 api_struct.api = UFFD_API;
918 api_struct.features = 0;
919 if (ioctl(dev->postcopy_ufd, UFFDIO_API, &api_struct)) {
920 vu_panic(dev, "Failed UFFDIO_API: %s", strerror(errno));
921 close(dev->postcopy_ufd);
922 dev->postcopy_ufd = -1;
925 /* TODO: Stash feature flags somewhere */
929 /* Return a ufd to the QEMU */
931 vmsg->fds[0] = dev->postcopy_ufd;
932 return true; /* = send a reply */
936 vu_set_postcopy_listen(VuDev *dev, VhostUserMsg *vmsg)
938 vmsg->payload.u64 = -1;
939 vmsg->size = sizeof(vmsg->payload.u64);
942 vu_panic(dev, "Regions already registered at postcopy-listen");
945 dev->postcopy_listening = true;
947 vmsg->flags = VHOST_USER_VERSION | VHOST_USER_REPLY_MASK;
948 vmsg->payload.u64 = 0; /* Success */
952 vu_process_message(VuDev *dev, VhostUserMsg *vmsg)
956 /* Print out generic part of the request. */
957 DPRINT("================ Vhost user message ================\n");
958 DPRINT("Request: %s (%d)\n", vu_request_to_string(vmsg->request),
960 DPRINT("Flags: 0x%x\n", vmsg->flags);
961 DPRINT("Size: %d\n", vmsg->size);
966 for (i = 0; i < vmsg->fd_num; i++) {
967 DPRINT(" %d", vmsg->fds[i]);
972 if (dev->iface->process_msg &&
973 dev->iface->process_msg(dev, vmsg, &do_reply)) {
977 switch (vmsg->request) {
978 case VHOST_USER_GET_FEATURES:
979 return vu_get_features_exec(dev, vmsg);
980 case VHOST_USER_SET_FEATURES:
981 return vu_set_features_exec(dev, vmsg);
982 case VHOST_USER_GET_PROTOCOL_FEATURES:
983 return vu_get_protocol_features_exec(dev, vmsg);
984 case VHOST_USER_SET_PROTOCOL_FEATURES:
985 return vu_set_protocol_features_exec(dev, vmsg);
986 case VHOST_USER_SET_OWNER:
987 return vu_set_owner_exec(dev, vmsg);
988 case VHOST_USER_RESET_OWNER:
989 return vu_reset_device_exec(dev, vmsg);
990 case VHOST_USER_SET_MEM_TABLE:
991 return vu_set_mem_table_exec(dev, vmsg);
992 case VHOST_USER_SET_LOG_BASE:
993 return vu_set_log_base_exec(dev, vmsg);
994 case VHOST_USER_SET_LOG_FD:
995 return vu_set_log_fd_exec(dev, vmsg);
996 case VHOST_USER_SET_VRING_NUM:
997 return vu_set_vring_num_exec(dev, vmsg);
998 case VHOST_USER_SET_VRING_ADDR:
999 return vu_set_vring_addr_exec(dev, vmsg);
1000 case VHOST_USER_SET_VRING_BASE:
1001 return vu_set_vring_base_exec(dev, vmsg);
1002 case VHOST_USER_GET_VRING_BASE:
1003 return vu_get_vring_base_exec(dev, vmsg);
1004 case VHOST_USER_SET_VRING_KICK:
1005 return vu_set_vring_kick_exec(dev, vmsg);
1006 case VHOST_USER_SET_VRING_CALL:
1007 return vu_set_vring_call_exec(dev, vmsg);
1008 case VHOST_USER_SET_VRING_ERR:
1009 return vu_set_vring_err_exec(dev, vmsg);
1010 case VHOST_USER_GET_QUEUE_NUM:
1011 return vu_get_queue_num_exec(dev, vmsg);
1012 case VHOST_USER_SET_VRING_ENABLE:
1013 return vu_set_vring_enable_exec(dev, vmsg);
1014 case VHOST_USER_SET_SLAVE_REQ_FD:
1015 return vu_set_slave_req_fd(dev, vmsg);
1016 case VHOST_USER_GET_CONFIG:
1017 return vu_get_config(dev, vmsg);
1018 case VHOST_USER_SET_CONFIG:
1019 return vu_set_config(dev, vmsg);
1020 case VHOST_USER_NONE:
1022 case VHOST_USER_POSTCOPY_ADVISE:
1023 return vu_set_postcopy_advise(dev, vmsg);
1024 case VHOST_USER_POSTCOPY_LISTEN:
1025 return vu_set_postcopy_listen(dev, vmsg);
1027 vmsg_close_fds(vmsg);
1028 vu_panic(dev, "Unhandled request: %d", vmsg->request);
1035 vu_dispatch(VuDev *dev)
1037 VhostUserMsg vmsg = { 0, };
1038 int reply_requested;
1039 bool success = false;
1041 if (!vu_message_read(dev, dev->sock, &vmsg)) {
1045 reply_requested = vu_process_message(dev, &vmsg);
1046 if (!reply_requested) {
1051 if (!vu_message_write(dev, dev->sock, &vmsg)) {
1063 vu_deinit(VuDev *dev)
1067 for (i = 0; i < dev->nregions; i++) {
1068 VuDevRegion *r = &dev->regions[i];
1069 void *m = (void *) (uintptr_t) r->mmap_addr;
1070 if (m != MAP_FAILED) {
1071 munmap(m, r->size + r->mmap_offset);
1076 for (i = 0; i < VHOST_MAX_NR_VIRTQUEUE; i++) {
1077 VuVirtq *vq = &dev->vq[i];
1079 if (vq->call_fd != -1) {
1084 if (vq->kick_fd != -1) {
1089 if (vq->err_fd != -1) {
1097 if (dev->slave_fd != -1) {
1098 close(dev->slave_fd);
1102 if (dev->sock != -1) {
1111 vu_set_watch_cb set_watch,
1112 vu_remove_watch_cb remove_watch,
1113 const VuDevIface *iface)
1117 assert(socket >= 0);
1119 assert(remove_watch);
1123 memset(dev, 0, sizeof(*dev));
1127 dev->set_watch = set_watch;
1128 dev->remove_watch = remove_watch;
1130 dev->log_call_fd = -1;
1132 for (i = 0; i < VHOST_MAX_NR_VIRTQUEUE; i++) {
1133 dev->vq[i] = (VuVirtq) {
1134 .call_fd = -1, .kick_fd = -1, .err_fd = -1,
1135 .notification = true,
1141 vu_get_queue(VuDev *dev, int qidx)
1143 assert(qidx < VHOST_MAX_NR_VIRTQUEUE);
1144 return &dev->vq[qidx];
1148 vu_queue_enabled(VuDev *dev, VuVirtq *vq)
1154 vu_queue_started(const VuDev *dev, const VuVirtq *vq)
1159 static inline uint16_t
1160 vring_avail_flags(VuVirtq *vq)
1162 return vq->vring.avail->flags;
1165 static inline uint16_t
1166 vring_avail_idx(VuVirtq *vq)
1168 vq->shadow_avail_idx = vq->vring.avail->idx;
1170 return vq->shadow_avail_idx;
1173 static inline uint16_t
1174 vring_avail_ring(VuVirtq *vq, int i)
1176 return vq->vring.avail->ring[i];
1179 static inline uint16_t
1180 vring_get_used_event(VuVirtq *vq)
1182 return vring_avail_ring(vq, vq->vring.num);
1186 virtqueue_num_heads(VuDev *dev, VuVirtq *vq, unsigned int idx)
1188 uint16_t num_heads = vring_avail_idx(vq) - idx;
1190 /* Check it isn't doing very strange things with descriptor numbers. */
1191 if (num_heads > vq->vring.num) {
1192 vu_panic(dev, "Guest moved used index from %u to %u",
1193 idx, vq->shadow_avail_idx);
1197 /* On success, callers read a descriptor at vq->last_avail_idx.
1198 * Make sure descriptor read does not bypass avail index read. */
1206 virtqueue_get_head(VuDev *dev, VuVirtq *vq,
1207 unsigned int idx, unsigned int *head)
1209 /* Grab the next descriptor number they're advertising, and increment
1210 * the index we've seen. */
1211 *head = vring_avail_ring(vq, idx % vq->vring.num);
1213 /* If their number is silly, that's a fatal mistake. */
1214 if (*head >= vq->vring.num) {
1215 vu_panic(dev, "Guest says index %u is available", head);
1223 virtqueue_read_indirect_desc(VuDev *dev, struct vring_desc *desc,
1224 uint64_t addr, size_t len)
1226 struct vring_desc *ori_desc;
1229 if (len > (VIRTQUEUE_MAX_SIZE * sizeof(struct vring_desc))) {
1239 ori_desc = vu_gpa_to_va(dev, &read_len, addr);
1244 memcpy(desc, ori_desc, read_len);
1254 VIRTQUEUE_READ_DESC_ERROR = -1,
1255 VIRTQUEUE_READ_DESC_DONE = 0, /* end of chain */
1256 VIRTQUEUE_READ_DESC_MORE = 1, /* more buffers in chain */
1260 virtqueue_read_next_desc(VuDev *dev, struct vring_desc *desc,
1261 int i, unsigned int max, unsigned int *next)
1263 /* If this descriptor says it doesn't chain, we're done. */
1264 if (!(desc[i].flags & VRING_DESC_F_NEXT)) {
1265 return VIRTQUEUE_READ_DESC_DONE;
1268 /* Check they're not leading us off end of descriptors. */
1269 *next = desc[i].next;
1270 /* Make sure compiler knows to grab that: we don't want it changing! */
1274 vu_panic(dev, "Desc next is %u", next);
1275 return VIRTQUEUE_READ_DESC_ERROR;
1278 return VIRTQUEUE_READ_DESC_MORE;
1282 vu_queue_get_avail_bytes(VuDev *dev, VuVirtq *vq, unsigned int *in_bytes,
1283 unsigned int *out_bytes,
1284 unsigned max_in_bytes, unsigned max_out_bytes)
1287 unsigned int total_bufs, in_total, out_total;
1290 idx = vq->last_avail_idx;
1292 total_bufs = in_total = out_total = 0;
1293 if (unlikely(dev->broken) ||
1294 unlikely(!vq->vring.avail)) {
1298 while ((rc = virtqueue_num_heads(dev, vq, idx)) > 0) {
1299 unsigned int max, desc_len, num_bufs, indirect = 0;
1300 uint64_t desc_addr, read_len;
1301 struct vring_desc *desc;
1302 struct vring_desc desc_buf[VIRTQUEUE_MAX_SIZE];
1305 max = vq->vring.num;
1306 num_bufs = total_bufs;
1307 if (!virtqueue_get_head(dev, vq, idx++, &i)) {
1310 desc = vq->vring.desc;
1312 if (desc[i].flags & VRING_DESC_F_INDIRECT) {
1313 if (desc[i].len % sizeof(struct vring_desc)) {
1314 vu_panic(dev, "Invalid size for indirect buffer table");
1318 /* If we've got too many, that implies a descriptor loop. */
1319 if (num_bufs >= max) {
1320 vu_panic(dev, "Looped descriptor");
1324 /* loop over the indirect descriptor table */
1326 desc_addr = desc[i].addr;
1327 desc_len = desc[i].len;
1328 max = desc_len / sizeof(struct vring_desc);
1329 read_len = desc_len;
1330 desc = vu_gpa_to_va(dev, &read_len, desc_addr);
1331 if (unlikely(desc && read_len != desc_len)) {
1332 /* Failed to use zero copy */
1334 if (!virtqueue_read_indirect_desc(dev, desc_buf,
1341 vu_panic(dev, "Invalid indirect buffer table");
1348 /* If we've got too many, that implies a descriptor loop. */
1349 if (++num_bufs > max) {
1350 vu_panic(dev, "Looped descriptor");
1354 if (desc[i].flags & VRING_DESC_F_WRITE) {
1355 in_total += desc[i].len;
1357 out_total += desc[i].len;
1359 if (in_total >= max_in_bytes && out_total >= max_out_bytes) {
1362 rc = virtqueue_read_next_desc(dev, desc, i, max, &i);
1363 } while (rc == VIRTQUEUE_READ_DESC_MORE);
1365 if (rc == VIRTQUEUE_READ_DESC_ERROR) {
1370 total_bufs = num_bufs;
1380 *in_bytes = in_total;
1383 *out_bytes = out_total;
1388 in_total = out_total = 0;
1393 vu_queue_avail_bytes(VuDev *dev, VuVirtq *vq, unsigned int in_bytes,
1394 unsigned int out_bytes)
1396 unsigned int in_total, out_total;
1398 vu_queue_get_avail_bytes(dev, vq, &in_total, &out_total,
1399 in_bytes, out_bytes);
1401 return in_bytes <= in_total && out_bytes <= out_total;
1404 /* Fetch avail_idx from VQ memory only when we really need to know if
1405 * guest has added some buffers. */
1407 vu_queue_empty(VuDev *dev, VuVirtq *vq)
1409 if (unlikely(dev->broken) ||
1410 unlikely(!vq->vring.avail)) {
1414 if (vq->shadow_avail_idx != vq->last_avail_idx) {
1418 return vring_avail_idx(vq) == vq->last_avail_idx;
1422 bool has_feature(uint64_t features, unsigned int fbit)
1425 return !!(features & (1ULL << fbit));
1429 bool vu_has_feature(VuDev *dev,
1432 return has_feature(dev->features, fbit);
1436 vring_notify(VuDev *dev, VuVirtq *vq)
1441 /* We need to expose used array entries before checking used event. */
1444 /* Always notify when queue is empty (when feature acknowledge) */
1445 if (vu_has_feature(dev, VIRTIO_F_NOTIFY_ON_EMPTY) &&
1446 !vq->inuse && vu_queue_empty(dev, vq)) {
1450 if (!vu_has_feature(dev, VIRTIO_RING_F_EVENT_IDX)) {
1451 return !(vring_avail_flags(vq) & VRING_AVAIL_F_NO_INTERRUPT);
1454 v = vq->signalled_used_valid;
1455 vq->signalled_used_valid = true;
1456 old = vq->signalled_used;
1457 new = vq->signalled_used = vq->used_idx;
1458 return !v || vring_need_event(vring_get_used_event(vq), new, old);
1462 vu_queue_notify(VuDev *dev, VuVirtq *vq)
1464 if (unlikely(dev->broken) ||
1465 unlikely(!vq->vring.avail)) {
1469 if (!vring_notify(dev, vq)) {
1470 DPRINT("skipped notify...\n");
1474 if (eventfd_write(vq->call_fd, 1) < 0) {
1475 vu_panic(dev, "Error writing eventfd: %s", strerror(errno));
1480 vring_used_flags_set_bit(VuVirtq *vq, int mask)
1484 flags = (uint16_t *)((char*)vq->vring.used +
1485 offsetof(struct vring_used, flags));
1490 vring_used_flags_unset_bit(VuVirtq *vq, int mask)
1494 flags = (uint16_t *)((char*)vq->vring.used +
1495 offsetof(struct vring_used, flags));
1500 vring_set_avail_event(VuVirtq *vq, uint16_t val)
1502 if (!vq->notification) {
1506 *((uint16_t *) &vq->vring.used->ring[vq->vring.num]) = val;
1510 vu_queue_set_notification(VuDev *dev, VuVirtq *vq, int enable)
1512 vq->notification = enable;
1513 if (vu_has_feature(dev, VIRTIO_RING_F_EVENT_IDX)) {
1514 vring_set_avail_event(vq, vring_avail_idx(vq));
1515 } else if (enable) {
1516 vring_used_flags_unset_bit(vq, VRING_USED_F_NO_NOTIFY);
1518 vring_used_flags_set_bit(vq, VRING_USED_F_NO_NOTIFY);
1521 /* Expose avail event/used flags before caller checks the avail idx. */
1527 virtqueue_map_desc(VuDev *dev,
1528 unsigned int *p_num_sg, struct iovec *iov,
1529 unsigned int max_num_sg, bool is_write,
1530 uint64_t pa, size_t sz)
1532 unsigned num_sg = *p_num_sg;
1534 assert(num_sg <= max_num_sg);
1537 vu_panic(dev, "virtio: zero sized buffers are not allowed");
1544 if (num_sg == max_num_sg) {
1545 vu_panic(dev, "virtio: too many descriptors in indirect table");
1549 iov[num_sg].iov_base = vu_gpa_to_va(dev, &len, pa);
1550 if (iov[num_sg].iov_base == NULL) {
1551 vu_panic(dev, "virtio: invalid address for buffers");
1554 iov[num_sg].iov_len = len;
1563 /* Round number down to multiple */
1564 #define ALIGN_DOWN(n, m) ((n) / (m) * (m))
1566 /* Round number up to multiple */
1567 #define ALIGN_UP(n, m) ALIGN_DOWN((n) + (m) - 1, (m))
1570 virtqueue_alloc_element(size_t sz,
1571 unsigned out_num, unsigned in_num)
1573 VuVirtqElement *elem;
1574 size_t in_sg_ofs = ALIGN_UP(sz, __alignof__(elem->in_sg[0]));
1575 size_t out_sg_ofs = in_sg_ofs + in_num * sizeof(elem->in_sg[0]);
1576 size_t out_sg_end = out_sg_ofs + out_num * sizeof(elem->out_sg[0]);
1578 assert(sz >= sizeof(VuVirtqElement));
1579 elem = malloc(out_sg_end);
1580 elem->out_num = out_num;
1581 elem->in_num = in_num;
1582 elem->in_sg = (void *)elem + in_sg_ofs;
1583 elem->out_sg = (void *)elem + out_sg_ofs;
1588 vu_queue_pop(VuDev *dev, VuVirtq *vq, size_t sz)
1590 unsigned int i, head, max, desc_len;
1591 uint64_t desc_addr, read_len;
1592 VuVirtqElement *elem;
1593 unsigned out_num, in_num;
1594 struct iovec iov[VIRTQUEUE_MAX_SIZE];
1595 struct vring_desc desc_buf[VIRTQUEUE_MAX_SIZE];
1596 struct vring_desc *desc;
1599 if (unlikely(dev->broken) ||
1600 unlikely(!vq->vring.avail)) {
1604 if (vu_queue_empty(dev, vq)) {
1607 /* Needed after virtio_queue_empty(), see comment in
1608 * virtqueue_num_heads(). */
1611 /* When we start there are none of either input nor output. */
1612 out_num = in_num = 0;
1614 max = vq->vring.num;
1615 if (vq->inuse >= vq->vring.num) {
1616 vu_panic(dev, "Virtqueue size exceeded");
1620 if (!virtqueue_get_head(dev, vq, vq->last_avail_idx++, &head)) {
1624 if (vu_has_feature(dev, VIRTIO_RING_F_EVENT_IDX)) {
1625 vring_set_avail_event(vq, vq->last_avail_idx);
1629 desc = vq->vring.desc;
1630 if (desc[i].flags & VRING_DESC_F_INDIRECT) {
1631 if (desc[i].len % sizeof(struct vring_desc)) {
1632 vu_panic(dev, "Invalid size for indirect buffer table");
1635 /* loop over the indirect descriptor table */
1636 desc_addr = desc[i].addr;
1637 desc_len = desc[i].len;
1638 max = desc_len / sizeof(struct vring_desc);
1639 read_len = desc_len;
1640 desc = vu_gpa_to_va(dev, &read_len, desc_addr);
1641 if (unlikely(desc && read_len != desc_len)) {
1642 /* Failed to use zero copy */
1644 if (!virtqueue_read_indirect_desc(dev, desc_buf,
1651 vu_panic(dev, "Invalid indirect buffer table");
1657 /* Collect all the descriptors */
1659 if (desc[i].flags & VRING_DESC_F_WRITE) {
1660 virtqueue_map_desc(dev, &in_num, iov + out_num,
1661 VIRTQUEUE_MAX_SIZE - out_num, true,
1662 desc[i].addr, desc[i].len);
1665 vu_panic(dev, "Incorrect order for descriptors");
1668 virtqueue_map_desc(dev, &out_num, iov,
1669 VIRTQUEUE_MAX_SIZE, false,
1670 desc[i].addr, desc[i].len);
1673 /* If we've got too many, that implies a descriptor loop. */
1674 if ((in_num + out_num) > max) {
1675 vu_panic(dev, "Looped descriptor");
1677 rc = virtqueue_read_next_desc(dev, desc, i, max, &i);
1678 } while (rc == VIRTQUEUE_READ_DESC_MORE);
1680 if (rc == VIRTQUEUE_READ_DESC_ERROR) {
1684 /* Now copy what we have collected and mapped */
1685 elem = virtqueue_alloc_element(sz, out_num, in_num);
1687 for (i = 0; i < out_num; i++) {
1688 elem->out_sg[i] = iov[i];
1690 for (i = 0; i < in_num; i++) {
1691 elem->in_sg[i] = iov[out_num + i];
1700 vu_queue_rewind(VuDev *dev, VuVirtq *vq, unsigned int num)
1702 if (num > vq->inuse) {
1705 vq->last_avail_idx -= num;
1711 void vring_used_write(VuDev *dev, VuVirtq *vq,
1712 struct vring_used_elem *uelem, int i)
1714 struct vring_used *used = vq->vring.used;
1716 used->ring[i] = *uelem;
1717 vu_log_write(dev, vq->vring.log_guest_addr +
1718 offsetof(struct vring_used, ring[i]),
1719 sizeof(used->ring[i]));
1724 vu_log_queue_fill(VuDev *dev, VuVirtq *vq,
1725 const VuVirtqElement *elem,
1728 struct vring_desc *desc = vq->vring.desc;
1729 unsigned int i, max, min, desc_len;
1730 uint64_t desc_addr, read_len;
1731 struct vring_desc desc_buf[VIRTQUEUE_MAX_SIZE];
1732 unsigned num_bufs = 0;
1734 max = vq->vring.num;
1737 if (desc[i].flags & VRING_DESC_F_INDIRECT) {
1738 if (desc[i].len % sizeof(struct vring_desc)) {
1739 vu_panic(dev, "Invalid size for indirect buffer table");
1742 /* loop over the indirect descriptor table */
1743 desc_addr = desc[i].addr;
1744 desc_len = desc[i].len;
1745 max = desc_len / sizeof(struct vring_desc);
1746 read_len = desc_len;
1747 desc = vu_gpa_to_va(dev, &read_len, desc_addr);
1748 if (unlikely(desc && read_len != desc_len)) {
1749 /* Failed to use zero copy */
1751 if (!virtqueue_read_indirect_desc(dev, desc_buf,
1758 vu_panic(dev, "Invalid indirect buffer table");
1765 if (++num_bufs > max) {
1766 vu_panic(dev, "Looped descriptor");
1770 if (desc[i].flags & VRING_DESC_F_WRITE) {
1771 min = MIN(desc[i].len, len);
1772 vu_log_write(dev, desc[i].addr, min);
1777 (virtqueue_read_next_desc(dev, desc, i, max, &i)
1778 == VIRTQUEUE_READ_DESC_MORE));
1782 vu_queue_fill(VuDev *dev, VuVirtq *vq,
1783 const VuVirtqElement *elem,
1784 unsigned int len, unsigned int idx)
1786 struct vring_used_elem uelem;
1788 if (unlikely(dev->broken) ||
1789 unlikely(!vq->vring.avail)) {
1793 vu_log_queue_fill(dev, vq, elem, len);
1795 idx = (idx + vq->used_idx) % vq->vring.num;
1797 uelem.id = elem->index;
1799 vring_used_write(dev, vq, &uelem, idx);
1803 void vring_used_idx_set(VuDev *dev, VuVirtq *vq, uint16_t val)
1805 vq->vring.used->idx = val;
1807 vq->vring.log_guest_addr + offsetof(struct vring_used, idx),
1808 sizeof(vq->vring.used->idx));
1814 vu_queue_flush(VuDev *dev, VuVirtq *vq, unsigned int count)
1818 if (unlikely(dev->broken) ||
1819 unlikely(!vq->vring.avail)) {
1823 /* Make sure buffer is written before we update index. */
1828 vring_used_idx_set(dev, vq, new);
1830 if (unlikely((int16_t)(new - vq->signalled_used) < (uint16_t)(new - old))) {
1831 vq->signalled_used_valid = false;
1836 vu_queue_push(VuDev *dev, VuVirtq *vq,
1837 const VuVirtqElement *elem, unsigned int len)
1839 vu_queue_fill(dev, vq, elem, len, 0);
1840 vu_queue_flush(dev, vq, 1);