PPC: E500: Add FSL I2C controller and integrate RTC with it
[qemu/ar7.git] / hw / virtio / vhost-user.c
blob0d6c64e5ca44d796b9ce1c4364ca60bab668a9dd
1 /*
2 * vhost-user
4 * Copyright (c) 2013 Virtual Open Systems Sarl.
6 * This work is licensed under the terms of the GNU GPL, version 2 or later.
7 * See the COPYING file in the top-level directory.
9 */
11 #include "qemu/osdep.h"
12 #include "qapi/error.h"
13 #include "hw/virtio/vhost.h"
14 #include "hw/virtio/vhost-user.h"
15 #include "hw/virtio/vhost-backend.h"
16 #include "hw/virtio/virtio.h"
17 #include "hw/virtio/virtio-net.h"
18 #include "chardev/char-fe.h"
19 #include "sysemu/kvm.h"
20 #include "qemu/error-report.h"
21 #include "qemu/sockets.h"
22 #include "sysemu/cryptodev.h"
23 #include "migration/migration.h"
24 #include "migration/postcopy-ram.h"
25 #include "trace.h"
27 #include <sys/ioctl.h>
28 #include <sys/socket.h>
29 #include <sys/un.h>
31 #include "standard-headers/linux/vhost_types.h"
33 #ifdef CONFIG_LINUX
34 #include <linux/userfaultfd.h>
35 #endif
37 #define VHOST_MEMORY_MAX_NREGIONS 8
38 #define VHOST_USER_F_PROTOCOL_FEATURES 30
39 #define VHOST_USER_SLAVE_MAX_FDS 8
42 * Maximum size of virtio device config space
44 #define VHOST_USER_MAX_CONFIG_SIZE 256
46 enum VhostUserProtocolFeature {
47 VHOST_USER_PROTOCOL_F_MQ = 0,
48 VHOST_USER_PROTOCOL_F_LOG_SHMFD = 1,
49 VHOST_USER_PROTOCOL_F_RARP = 2,
50 VHOST_USER_PROTOCOL_F_REPLY_ACK = 3,
51 VHOST_USER_PROTOCOL_F_NET_MTU = 4,
52 VHOST_USER_PROTOCOL_F_SLAVE_REQ = 5,
53 VHOST_USER_PROTOCOL_F_CROSS_ENDIAN = 6,
54 VHOST_USER_PROTOCOL_F_CRYPTO_SESSION = 7,
55 VHOST_USER_PROTOCOL_F_PAGEFAULT = 8,
56 VHOST_USER_PROTOCOL_F_CONFIG = 9,
57 VHOST_USER_PROTOCOL_F_SLAVE_SEND_FD = 10,
58 VHOST_USER_PROTOCOL_F_HOST_NOTIFIER = 11,
59 VHOST_USER_PROTOCOL_F_MAX
62 #define VHOST_USER_PROTOCOL_FEATURE_MASK ((1 << VHOST_USER_PROTOCOL_F_MAX) - 1)
64 typedef enum VhostUserRequest {
65 VHOST_USER_NONE = 0,
66 VHOST_USER_GET_FEATURES = 1,
67 VHOST_USER_SET_FEATURES = 2,
68 VHOST_USER_SET_OWNER = 3,
69 VHOST_USER_RESET_OWNER = 4,
70 VHOST_USER_SET_MEM_TABLE = 5,
71 VHOST_USER_SET_LOG_BASE = 6,
72 VHOST_USER_SET_LOG_FD = 7,
73 VHOST_USER_SET_VRING_NUM = 8,
74 VHOST_USER_SET_VRING_ADDR = 9,
75 VHOST_USER_SET_VRING_BASE = 10,
76 VHOST_USER_GET_VRING_BASE = 11,
77 VHOST_USER_SET_VRING_KICK = 12,
78 VHOST_USER_SET_VRING_CALL = 13,
79 VHOST_USER_SET_VRING_ERR = 14,
80 VHOST_USER_GET_PROTOCOL_FEATURES = 15,
81 VHOST_USER_SET_PROTOCOL_FEATURES = 16,
82 VHOST_USER_GET_QUEUE_NUM = 17,
83 VHOST_USER_SET_VRING_ENABLE = 18,
84 VHOST_USER_SEND_RARP = 19,
85 VHOST_USER_NET_SET_MTU = 20,
86 VHOST_USER_SET_SLAVE_REQ_FD = 21,
87 VHOST_USER_IOTLB_MSG = 22,
88 VHOST_USER_SET_VRING_ENDIAN = 23,
89 VHOST_USER_GET_CONFIG = 24,
90 VHOST_USER_SET_CONFIG = 25,
91 VHOST_USER_CREATE_CRYPTO_SESSION = 26,
92 VHOST_USER_CLOSE_CRYPTO_SESSION = 27,
93 VHOST_USER_POSTCOPY_ADVISE = 28,
94 VHOST_USER_POSTCOPY_LISTEN = 29,
95 VHOST_USER_POSTCOPY_END = 30,
96 VHOST_USER_MAX
97 } VhostUserRequest;
99 typedef enum VhostUserSlaveRequest {
100 VHOST_USER_SLAVE_NONE = 0,
101 VHOST_USER_SLAVE_IOTLB_MSG = 1,
102 VHOST_USER_SLAVE_CONFIG_CHANGE_MSG = 2,
103 VHOST_USER_SLAVE_VRING_HOST_NOTIFIER_MSG = 3,
104 VHOST_USER_SLAVE_MAX
105 } VhostUserSlaveRequest;
107 typedef struct VhostUserMemoryRegion {
108 uint64_t guest_phys_addr;
109 uint64_t memory_size;
110 uint64_t userspace_addr;
111 uint64_t mmap_offset;
112 } VhostUserMemoryRegion;
114 typedef struct VhostUserMemory {
115 uint32_t nregions;
116 uint32_t padding;
117 VhostUserMemoryRegion regions[VHOST_MEMORY_MAX_NREGIONS];
118 } VhostUserMemory;
120 typedef struct VhostUserLog {
121 uint64_t mmap_size;
122 uint64_t mmap_offset;
123 } VhostUserLog;
125 typedef struct VhostUserConfig {
126 uint32_t offset;
127 uint32_t size;
128 uint32_t flags;
129 uint8_t region[VHOST_USER_MAX_CONFIG_SIZE];
130 } VhostUserConfig;
132 #define VHOST_CRYPTO_SYM_HMAC_MAX_KEY_LEN 512
133 #define VHOST_CRYPTO_SYM_CIPHER_MAX_KEY_LEN 64
135 typedef struct VhostUserCryptoSession {
136 /* session id for success, -1 on errors */
137 int64_t session_id;
138 CryptoDevBackendSymSessionInfo session_setup_data;
139 uint8_t key[VHOST_CRYPTO_SYM_CIPHER_MAX_KEY_LEN];
140 uint8_t auth_key[VHOST_CRYPTO_SYM_HMAC_MAX_KEY_LEN];
141 } VhostUserCryptoSession;
143 static VhostUserConfig c __attribute__ ((unused));
144 #define VHOST_USER_CONFIG_HDR_SIZE (sizeof(c.offset) \
145 + sizeof(c.size) \
146 + sizeof(c.flags))
148 typedef struct VhostUserVringArea {
149 uint64_t u64;
150 uint64_t size;
151 uint64_t offset;
152 } VhostUserVringArea;
154 typedef struct {
155 VhostUserRequest request;
157 #define VHOST_USER_VERSION_MASK (0x3)
158 #define VHOST_USER_REPLY_MASK (0x1<<2)
159 #define VHOST_USER_NEED_REPLY_MASK (0x1 << 3)
160 uint32_t flags;
161 uint32_t size; /* the following payload size */
162 } QEMU_PACKED VhostUserHeader;
164 typedef union {
165 #define VHOST_USER_VRING_IDX_MASK (0xff)
166 #define VHOST_USER_VRING_NOFD_MASK (0x1<<8)
167 uint64_t u64;
168 struct vhost_vring_state state;
169 struct vhost_vring_addr addr;
170 VhostUserMemory memory;
171 VhostUserLog log;
172 struct vhost_iotlb_msg iotlb;
173 VhostUserConfig config;
174 VhostUserCryptoSession session;
175 VhostUserVringArea area;
176 } VhostUserPayload;
178 typedef struct VhostUserMsg {
179 VhostUserHeader hdr;
180 VhostUserPayload payload;
181 } QEMU_PACKED VhostUserMsg;
183 static VhostUserMsg m __attribute__ ((unused));
184 #define VHOST_USER_HDR_SIZE (sizeof(VhostUserHeader))
186 #define VHOST_USER_PAYLOAD_SIZE (sizeof(VhostUserPayload))
188 /* The version of the protocol we support */
189 #define VHOST_USER_VERSION (0x1)
191 struct vhost_user {
192 struct vhost_dev *dev;
193 /* Shared between vhost devs of the same virtio device */
194 VhostUserState *user;
195 int slave_fd;
196 NotifierWithReturn postcopy_notifier;
197 struct PostCopyFD postcopy_fd;
198 uint64_t postcopy_client_bases[VHOST_MEMORY_MAX_NREGIONS];
199 /* Length of the region_rb and region_rb_offset arrays */
200 size_t region_rb_len;
201 /* RAMBlock associated with a given region */
202 RAMBlock **region_rb;
203 /* The offset from the start of the RAMBlock to the start of the
204 * vhost region.
206 ram_addr_t *region_rb_offset;
208 /* True once we've entered postcopy_listen */
209 bool postcopy_listen;
212 static bool ioeventfd_enabled(void)
214 return !kvm_enabled() || kvm_eventfds_enabled();
217 static int vhost_user_read(struct vhost_dev *dev, VhostUserMsg *msg)
219 struct vhost_user *u = dev->opaque;
220 CharBackend *chr = u->user->chr;
221 uint8_t *p = (uint8_t *) msg;
222 int r, size = VHOST_USER_HDR_SIZE;
224 r = qemu_chr_fe_read_all(chr, p, size);
225 if (r != size) {
226 error_report("Failed to read msg header. Read %d instead of %d."
227 " Original request %d.", r, size, msg->hdr.request);
228 goto fail;
231 /* validate received flags */
232 if (msg->hdr.flags != (VHOST_USER_REPLY_MASK | VHOST_USER_VERSION)) {
233 error_report("Failed to read msg header."
234 " Flags 0x%x instead of 0x%x.", msg->hdr.flags,
235 VHOST_USER_REPLY_MASK | VHOST_USER_VERSION);
236 goto fail;
239 /* validate message size is sane */
240 if (msg->hdr.size > VHOST_USER_PAYLOAD_SIZE) {
241 error_report("Failed to read msg header."
242 " Size %d exceeds the maximum %zu.", msg->hdr.size,
243 VHOST_USER_PAYLOAD_SIZE);
244 goto fail;
247 if (msg->hdr.size) {
248 p += VHOST_USER_HDR_SIZE;
249 size = msg->hdr.size;
250 r = qemu_chr_fe_read_all(chr, p, size);
251 if (r != size) {
252 error_report("Failed to read msg payload."
253 " Read %d instead of %d.", r, msg->hdr.size);
254 goto fail;
258 return 0;
260 fail:
261 return -1;
264 static int process_message_reply(struct vhost_dev *dev,
265 const VhostUserMsg *msg)
267 VhostUserMsg msg_reply;
269 if ((msg->hdr.flags & VHOST_USER_NEED_REPLY_MASK) == 0) {
270 return 0;
273 if (vhost_user_read(dev, &msg_reply) < 0) {
274 return -1;
277 if (msg_reply.hdr.request != msg->hdr.request) {
278 error_report("Received unexpected msg type."
279 "Expected %d received %d",
280 msg->hdr.request, msg_reply.hdr.request);
281 return -1;
284 return msg_reply.payload.u64 ? -1 : 0;
287 static bool vhost_user_one_time_request(VhostUserRequest request)
289 switch (request) {
290 case VHOST_USER_SET_OWNER:
291 case VHOST_USER_RESET_OWNER:
292 case VHOST_USER_SET_MEM_TABLE:
293 case VHOST_USER_GET_QUEUE_NUM:
294 case VHOST_USER_NET_SET_MTU:
295 return true;
296 default:
297 return false;
301 /* most non-init callers ignore the error */
302 static int vhost_user_write(struct vhost_dev *dev, VhostUserMsg *msg,
303 int *fds, int fd_num)
305 struct vhost_user *u = dev->opaque;
306 CharBackend *chr = u->user->chr;
307 int ret, size = VHOST_USER_HDR_SIZE + msg->hdr.size;
310 * For non-vring specific requests, like VHOST_USER_SET_MEM_TABLE,
311 * we just need send it once in the first time. For later such
312 * request, we just ignore it.
314 if (vhost_user_one_time_request(msg->hdr.request) && dev->vq_index != 0) {
315 msg->hdr.flags &= ~VHOST_USER_NEED_REPLY_MASK;
316 return 0;
319 if (qemu_chr_fe_set_msgfds(chr, fds, fd_num) < 0) {
320 error_report("Failed to set msg fds.");
321 return -1;
324 ret = qemu_chr_fe_write_all(chr, (const uint8_t *) msg, size);
325 if (ret != size) {
326 error_report("Failed to write msg."
327 " Wrote %d instead of %d.", ret, size);
328 return -1;
331 return 0;
334 static int vhost_user_set_log_base(struct vhost_dev *dev, uint64_t base,
335 struct vhost_log *log)
337 int fds[VHOST_MEMORY_MAX_NREGIONS];
338 size_t fd_num = 0;
339 bool shmfd = virtio_has_feature(dev->protocol_features,
340 VHOST_USER_PROTOCOL_F_LOG_SHMFD);
341 VhostUserMsg msg = {
342 .hdr.request = VHOST_USER_SET_LOG_BASE,
343 .hdr.flags = VHOST_USER_VERSION,
344 .payload.log.mmap_size = log->size * sizeof(*(log->log)),
345 .payload.log.mmap_offset = 0,
346 .hdr.size = sizeof(msg.payload.log),
349 if (shmfd && log->fd != -1) {
350 fds[fd_num++] = log->fd;
353 if (vhost_user_write(dev, &msg, fds, fd_num) < 0) {
354 return -1;
357 if (shmfd) {
358 msg.hdr.size = 0;
359 if (vhost_user_read(dev, &msg) < 0) {
360 return -1;
363 if (msg.hdr.request != VHOST_USER_SET_LOG_BASE) {
364 error_report("Received unexpected msg type. "
365 "Expected %d received %d",
366 VHOST_USER_SET_LOG_BASE, msg.hdr.request);
367 return -1;
371 return 0;
374 static int vhost_user_set_mem_table_postcopy(struct vhost_dev *dev,
375 struct vhost_memory *mem)
377 struct vhost_user *u = dev->opaque;
378 int fds[VHOST_MEMORY_MAX_NREGIONS];
379 int i, fd;
380 size_t fd_num = 0;
381 VhostUserMsg msg_reply;
382 int region_i, msg_i;
384 VhostUserMsg msg = {
385 .hdr.request = VHOST_USER_SET_MEM_TABLE,
386 .hdr.flags = VHOST_USER_VERSION,
389 if (u->region_rb_len < dev->mem->nregions) {
390 u->region_rb = g_renew(RAMBlock*, u->region_rb, dev->mem->nregions);
391 u->region_rb_offset = g_renew(ram_addr_t, u->region_rb_offset,
392 dev->mem->nregions);
393 memset(&(u->region_rb[u->region_rb_len]), '\0',
394 sizeof(RAMBlock *) * (dev->mem->nregions - u->region_rb_len));
395 memset(&(u->region_rb_offset[u->region_rb_len]), '\0',
396 sizeof(ram_addr_t) * (dev->mem->nregions - u->region_rb_len));
397 u->region_rb_len = dev->mem->nregions;
400 for (i = 0; i < dev->mem->nregions; ++i) {
401 struct vhost_memory_region *reg = dev->mem->regions + i;
402 ram_addr_t offset;
403 MemoryRegion *mr;
405 assert((uintptr_t)reg->userspace_addr == reg->userspace_addr);
406 mr = memory_region_from_host((void *)(uintptr_t)reg->userspace_addr,
407 &offset);
408 fd = memory_region_get_fd(mr);
409 if (fd > 0) {
410 trace_vhost_user_set_mem_table_withfd(fd_num, mr->name,
411 reg->memory_size,
412 reg->guest_phys_addr,
413 reg->userspace_addr, offset);
414 u->region_rb_offset[i] = offset;
415 u->region_rb[i] = mr->ram_block;
416 msg.payload.memory.regions[fd_num].userspace_addr =
417 reg->userspace_addr;
418 msg.payload.memory.regions[fd_num].memory_size = reg->memory_size;
419 msg.payload.memory.regions[fd_num].guest_phys_addr =
420 reg->guest_phys_addr;
421 msg.payload.memory.regions[fd_num].mmap_offset = offset;
422 assert(fd_num < VHOST_MEMORY_MAX_NREGIONS);
423 fds[fd_num++] = fd;
424 } else {
425 u->region_rb_offset[i] = 0;
426 u->region_rb[i] = NULL;
430 msg.payload.memory.nregions = fd_num;
432 if (!fd_num) {
433 error_report("Failed initializing vhost-user memory map, "
434 "consider using -object memory-backend-file share=on");
435 return -1;
438 msg.hdr.size = sizeof(msg.payload.memory.nregions);
439 msg.hdr.size += sizeof(msg.payload.memory.padding);
440 msg.hdr.size += fd_num * sizeof(VhostUserMemoryRegion);
442 if (vhost_user_write(dev, &msg, fds, fd_num) < 0) {
443 return -1;
446 if (vhost_user_read(dev, &msg_reply) < 0) {
447 return -1;
450 if (msg_reply.hdr.request != VHOST_USER_SET_MEM_TABLE) {
451 error_report("%s: Received unexpected msg type."
452 "Expected %d received %d", __func__,
453 VHOST_USER_SET_MEM_TABLE, msg_reply.hdr.request);
454 return -1;
456 /* We're using the same structure, just reusing one of the
457 * fields, so it should be the same size.
459 if (msg_reply.hdr.size != msg.hdr.size) {
460 error_report("%s: Unexpected size for postcopy reply "
461 "%d vs %d", __func__, msg_reply.hdr.size, msg.hdr.size);
462 return -1;
465 memset(u->postcopy_client_bases, 0,
466 sizeof(uint64_t) * VHOST_MEMORY_MAX_NREGIONS);
468 /* They're in the same order as the regions that were sent
469 * but some of the regions were skipped (above) if they
470 * didn't have fd's
472 for (msg_i = 0, region_i = 0;
473 region_i < dev->mem->nregions;
474 region_i++) {
475 if (msg_i < fd_num &&
476 msg_reply.payload.memory.regions[msg_i].guest_phys_addr ==
477 dev->mem->regions[region_i].guest_phys_addr) {
478 u->postcopy_client_bases[region_i] =
479 msg_reply.payload.memory.regions[msg_i].userspace_addr;
480 trace_vhost_user_set_mem_table_postcopy(
481 msg_reply.payload.memory.regions[msg_i].userspace_addr,
482 msg.payload.memory.regions[msg_i].userspace_addr,
483 msg_i, region_i);
484 msg_i++;
487 if (msg_i != fd_num) {
488 error_report("%s: postcopy reply not fully consumed "
489 "%d vs %zd",
490 __func__, msg_i, fd_num);
491 return -1;
493 /* Now we've registered this with the postcopy code, we ack to the client,
494 * because now we're in the position to be able to deal with any faults
495 * it generates.
497 /* TODO: Use this for failure cases as well with a bad value */
498 msg.hdr.size = sizeof(msg.payload.u64);
499 msg.payload.u64 = 0; /* OK */
500 if (vhost_user_write(dev, &msg, NULL, 0) < 0) {
501 return -1;
504 return 0;
507 static int vhost_user_set_mem_table(struct vhost_dev *dev,
508 struct vhost_memory *mem)
510 struct vhost_user *u = dev->opaque;
511 int fds[VHOST_MEMORY_MAX_NREGIONS];
512 int i, fd;
513 size_t fd_num = 0;
514 bool do_postcopy = u->postcopy_listen && u->postcopy_fd.handler;
515 bool reply_supported = virtio_has_feature(dev->protocol_features,
516 VHOST_USER_PROTOCOL_F_REPLY_ACK);
518 if (do_postcopy) {
519 /* Postcopy has enough differences that it's best done in it's own
520 * version
522 return vhost_user_set_mem_table_postcopy(dev, mem);
525 VhostUserMsg msg = {
526 .hdr.request = VHOST_USER_SET_MEM_TABLE,
527 .hdr.flags = VHOST_USER_VERSION,
530 if (reply_supported) {
531 msg.hdr.flags |= VHOST_USER_NEED_REPLY_MASK;
534 for (i = 0; i < dev->mem->nregions; ++i) {
535 struct vhost_memory_region *reg = dev->mem->regions + i;
536 ram_addr_t offset;
537 MemoryRegion *mr;
539 assert((uintptr_t)reg->userspace_addr == reg->userspace_addr);
540 mr = memory_region_from_host((void *)(uintptr_t)reg->userspace_addr,
541 &offset);
542 fd = memory_region_get_fd(mr);
543 if (fd > 0) {
544 if (fd_num == VHOST_MEMORY_MAX_NREGIONS) {
545 error_report("Failed preparing vhost-user memory table msg");
546 return -1;
548 msg.payload.memory.regions[fd_num].userspace_addr =
549 reg->userspace_addr;
550 msg.payload.memory.regions[fd_num].memory_size = reg->memory_size;
551 msg.payload.memory.regions[fd_num].guest_phys_addr =
552 reg->guest_phys_addr;
553 msg.payload.memory.regions[fd_num].mmap_offset = offset;
554 fds[fd_num++] = fd;
558 msg.payload.memory.nregions = fd_num;
560 if (!fd_num) {
561 error_report("Failed initializing vhost-user memory map, "
562 "consider using -object memory-backend-file share=on");
563 return -1;
566 msg.hdr.size = sizeof(msg.payload.memory.nregions);
567 msg.hdr.size += sizeof(msg.payload.memory.padding);
568 msg.hdr.size += fd_num * sizeof(VhostUserMemoryRegion);
570 if (vhost_user_write(dev, &msg, fds, fd_num) < 0) {
571 return -1;
574 if (reply_supported) {
575 return process_message_reply(dev, &msg);
578 return 0;
581 static int vhost_user_set_vring_addr(struct vhost_dev *dev,
582 struct vhost_vring_addr *addr)
584 VhostUserMsg msg = {
585 .hdr.request = VHOST_USER_SET_VRING_ADDR,
586 .hdr.flags = VHOST_USER_VERSION,
587 .payload.addr = *addr,
588 .hdr.size = sizeof(msg.payload.addr),
591 if (vhost_user_write(dev, &msg, NULL, 0) < 0) {
592 return -1;
595 return 0;
598 static int vhost_user_set_vring_endian(struct vhost_dev *dev,
599 struct vhost_vring_state *ring)
601 bool cross_endian = virtio_has_feature(dev->protocol_features,
602 VHOST_USER_PROTOCOL_F_CROSS_ENDIAN);
603 VhostUserMsg msg = {
604 .hdr.request = VHOST_USER_SET_VRING_ENDIAN,
605 .hdr.flags = VHOST_USER_VERSION,
606 .payload.state = *ring,
607 .hdr.size = sizeof(msg.payload.state),
610 if (!cross_endian) {
611 error_report("vhost-user trying to send unhandled ioctl");
612 return -1;
615 if (vhost_user_write(dev, &msg, NULL, 0) < 0) {
616 return -1;
619 return 0;
622 static int vhost_set_vring(struct vhost_dev *dev,
623 unsigned long int request,
624 struct vhost_vring_state *ring)
626 VhostUserMsg msg = {
627 .hdr.request = request,
628 .hdr.flags = VHOST_USER_VERSION,
629 .payload.state = *ring,
630 .hdr.size = sizeof(msg.payload.state),
633 if (vhost_user_write(dev, &msg, NULL, 0) < 0) {
634 return -1;
637 return 0;
640 static int vhost_user_set_vring_num(struct vhost_dev *dev,
641 struct vhost_vring_state *ring)
643 return vhost_set_vring(dev, VHOST_USER_SET_VRING_NUM, ring);
646 static void vhost_user_host_notifier_restore(struct vhost_dev *dev,
647 int queue_idx)
649 struct vhost_user *u = dev->opaque;
650 VhostUserHostNotifier *n = &u->user->notifier[queue_idx];
651 VirtIODevice *vdev = dev->vdev;
653 if (n->addr && !n->set) {
654 virtio_queue_set_host_notifier_mr(vdev, queue_idx, &n->mr, true);
655 n->set = true;
659 static void vhost_user_host_notifier_remove(struct vhost_dev *dev,
660 int queue_idx)
662 struct vhost_user *u = dev->opaque;
663 VhostUserHostNotifier *n = &u->user->notifier[queue_idx];
664 VirtIODevice *vdev = dev->vdev;
666 if (n->addr && n->set) {
667 virtio_queue_set_host_notifier_mr(vdev, queue_idx, &n->mr, false);
668 n->set = false;
672 static int vhost_user_set_vring_base(struct vhost_dev *dev,
673 struct vhost_vring_state *ring)
675 vhost_user_host_notifier_restore(dev, ring->index);
677 return vhost_set_vring(dev, VHOST_USER_SET_VRING_BASE, ring);
680 static int vhost_user_set_vring_enable(struct vhost_dev *dev, int enable)
682 int i;
684 if (!virtio_has_feature(dev->features, VHOST_USER_F_PROTOCOL_FEATURES)) {
685 return -1;
688 for (i = 0; i < dev->nvqs; ++i) {
689 struct vhost_vring_state state = {
690 .index = dev->vq_index + i,
691 .num = enable,
694 vhost_set_vring(dev, VHOST_USER_SET_VRING_ENABLE, &state);
697 return 0;
700 static int vhost_user_get_vring_base(struct vhost_dev *dev,
701 struct vhost_vring_state *ring)
703 VhostUserMsg msg = {
704 .hdr.request = VHOST_USER_GET_VRING_BASE,
705 .hdr.flags = VHOST_USER_VERSION,
706 .payload.state = *ring,
707 .hdr.size = sizeof(msg.payload.state),
710 vhost_user_host_notifier_remove(dev, ring->index);
712 if (vhost_user_write(dev, &msg, NULL, 0) < 0) {
713 return -1;
716 if (vhost_user_read(dev, &msg) < 0) {
717 return -1;
720 if (msg.hdr.request != VHOST_USER_GET_VRING_BASE) {
721 error_report("Received unexpected msg type. Expected %d received %d",
722 VHOST_USER_GET_VRING_BASE, msg.hdr.request);
723 return -1;
726 if (msg.hdr.size != sizeof(msg.payload.state)) {
727 error_report("Received bad msg size.");
728 return -1;
731 *ring = msg.payload.state;
733 return 0;
736 static int vhost_set_vring_file(struct vhost_dev *dev,
737 VhostUserRequest request,
738 struct vhost_vring_file *file)
740 int fds[VHOST_MEMORY_MAX_NREGIONS];
741 size_t fd_num = 0;
742 VhostUserMsg msg = {
743 .hdr.request = request,
744 .hdr.flags = VHOST_USER_VERSION,
745 .payload.u64 = file->index & VHOST_USER_VRING_IDX_MASK,
746 .hdr.size = sizeof(msg.payload.u64),
749 if (ioeventfd_enabled() && file->fd > 0) {
750 fds[fd_num++] = file->fd;
751 } else {
752 msg.payload.u64 |= VHOST_USER_VRING_NOFD_MASK;
755 if (vhost_user_write(dev, &msg, fds, fd_num) < 0) {
756 return -1;
759 return 0;
762 static int vhost_user_set_vring_kick(struct vhost_dev *dev,
763 struct vhost_vring_file *file)
765 return vhost_set_vring_file(dev, VHOST_USER_SET_VRING_KICK, file);
768 static int vhost_user_set_vring_call(struct vhost_dev *dev,
769 struct vhost_vring_file *file)
771 return vhost_set_vring_file(dev, VHOST_USER_SET_VRING_CALL, file);
774 static int vhost_user_set_u64(struct vhost_dev *dev, int request, uint64_t u64)
776 VhostUserMsg msg = {
777 .hdr.request = request,
778 .hdr.flags = VHOST_USER_VERSION,
779 .payload.u64 = u64,
780 .hdr.size = sizeof(msg.payload.u64),
783 if (vhost_user_write(dev, &msg, NULL, 0) < 0) {
784 return -1;
787 return 0;
790 static int vhost_user_set_features(struct vhost_dev *dev,
791 uint64_t features)
793 return vhost_user_set_u64(dev, VHOST_USER_SET_FEATURES, features);
796 static int vhost_user_set_protocol_features(struct vhost_dev *dev,
797 uint64_t features)
799 return vhost_user_set_u64(dev, VHOST_USER_SET_PROTOCOL_FEATURES, features);
802 static int vhost_user_get_u64(struct vhost_dev *dev, int request, uint64_t *u64)
804 VhostUserMsg msg = {
805 .hdr.request = request,
806 .hdr.flags = VHOST_USER_VERSION,
809 if (vhost_user_one_time_request(request) && dev->vq_index != 0) {
810 return 0;
813 if (vhost_user_write(dev, &msg, NULL, 0) < 0) {
814 return -1;
817 if (vhost_user_read(dev, &msg) < 0) {
818 return -1;
821 if (msg.hdr.request != request) {
822 error_report("Received unexpected msg type. Expected %d received %d",
823 request, msg.hdr.request);
824 return -1;
827 if (msg.hdr.size != sizeof(msg.payload.u64)) {
828 error_report("Received bad msg size.");
829 return -1;
832 *u64 = msg.payload.u64;
834 return 0;
837 static int vhost_user_get_features(struct vhost_dev *dev, uint64_t *features)
839 return vhost_user_get_u64(dev, VHOST_USER_GET_FEATURES, features);
842 static int vhost_user_set_owner(struct vhost_dev *dev)
844 VhostUserMsg msg = {
845 .hdr.request = VHOST_USER_SET_OWNER,
846 .hdr.flags = VHOST_USER_VERSION,
849 if (vhost_user_write(dev, &msg, NULL, 0) < 0) {
850 return -1;
853 return 0;
856 static int vhost_user_reset_device(struct vhost_dev *dev)
858 VhostUserMsg msg = {
859 .hdr.request = VHOST_USER_RESET_OWNER,
860 .hdr.flags = VHOST_USER_VERSION,
863 if (vhost_user_write(dev, &msg, NULL, 0) < 0) {
864 return -1;
867 return 0;
870 static int vhost_user_slave_handle_config_change(struct vhost_dev *dev)
872 int ret = -1;
874 if (!dev->config_ops) {
875 return -1;
878 if (dev->config_ops->vhost_dev_config_notifier) {
879 ret = dev->config_ops->vhost_dev_config_notifier(dev);
882 return ret;
885 static int vhost_user_slave_handle_vring_host_notifier(struct vhost_dev *dev,
886 VhostUserVringArea *area,
887 int fd)
889 int queue_idx = area->u64 & VHOST_USER_VRING_IDX_MASK;
890 size_t page_size = qemu_real_host_page_size;
891 struct vhost_user *u = dev->opaque;
892 VhostUserState *user = u->user;
893 VirtIODevice *vdev = dev->vdev;
894 VhostUserHostNotifier *n;
895 void *addr;
896 char *name;
898 if (!virtio_has_feature(dev->protocol_features,
899 VHOST_USER_PROTOCOL_F_HOST_NOTIFIER) ||
900 vdev == NULL || queue_idx >= virtio_get_num_queues(vdev)) {
901 return -1;
904 n = &user->notifier[queue_idx];
906 if (n->addr) {
907 virtio_queue_set_host_notifier_mr(vdev, queue_idx, &n->mr, false);
908 object_unparent(OBJECT(&n->mr));
909 munmap(n->addr, page_size);
910 n->addr = NULL;
913 if (area->u64 & VHOST_USER_VRING_NOFD_MASK) {
914 return 0;
917 /* Sanity check. */
918 if (area->size != page_size) {
919 return -1;
922 addr = mmap(NULL, page_size, PROT_READ | PROT_WRITE, MAP_SHARED,
923 fd, area->offset);
924 if (addr == MAP_FAILED) {
925 return -1;
928 name = g_strdup_printf("vhost-user/host-notifier@%p mmaps[%d]",
929 user, queue_idx);
930 memory_region_init_ram_device_ptr(&n->mr, OBJECT(vdev), name,
931 page_size, addr);
932 g_free(name);
934 if (virtio_queue_set_host_notifier_mr(vdev, queue_idx, &n->mr, true)) {
935 munmap(addr, page_size);
936 return -1;
939 n->addr = addr;
940 n->set = true;
942 return 0;
945 static void slave_read(void *opaque)
947 struct vhost_dev *dev = opaque;
948 struct vhost_user *u = dev->opaque;
949 VhostUserHeader hdr = { 0, };
950 VhostUserPayload payload = { 0, };
951 int size, ret = 0;
952 struct iovec iov;
953 struct msghdr msgh;
954 int fd[VHOST_USER_SLAVE_MAX_FDS];
955 char control[CMSG_SPACE(sizeof(fd))];
956 struct cmsghdr *cmsg;
957 int i, fdsize = 0;
959 memset(&msgh, 0, sizeof(msgh));
960 msgh.msg_iov = &iov;
961 msgh.msg_iovlen = 1;
962 msgh.msg_control = control;
963 msgh.msg_controllen = sizeof(control);
965 memset(fd, -1, sizeof(fd));
967 /* Read header */
968 iov.iov_base = &hdr;
969 iov.iov_len = VHOST_USER_HDR_SIZE;
971 size = recvmsg(u->slave_fd, &msgh, 0);
972 if (size != VHOST_USER_HDR_SIZE) {
973 error_report("Failed to read from slave.");
974 goto err;
977 if (msgh.msg_flags & MSG_CTRUNC) {
978 error_report("Truncated message.");
979 goto err;
982 for (cmsg = CMSG_FIRSTHDR(&msgh); cmsg != NULL;
983 cmsg = CMSG_NXTHDR(&msgh, cmsg)) {
984 if (cmsg->cmsg_level == SOL_SOCKET &&
985 cmsg->cmsg_type == SCM_RIGHTS) {
986 fdsize = cmsg->cmsg_len - CMSG_LEN(0);
987 memcpy(fd, CMSG_DATA(cmsg), fdsize);
988 break;
992 if (hdr.size > VHOST_USER_PAYLOAD_SIZE) {
993 error_report("Failed to read msg header."
994 " Size %d exceeds the maximum %zu.", hdr.size,
995 VHOST_USER_PAYLOAD_SIZE);
996 goto err;
999 /* Read payload */
1000 size = read(u->slave_fd, &payload, hdr.size);
1001 if (size != hdr.size) {
1002 error_report("Failed to read payload from slave.");
1003 goto err;
1006 switch (hdr.request) {
1007 case VHOST_USER_SLAVE_IOTLB_MSG:
1008 ret = vhost_backend_handle_iotlb_msg(dev, &payload.iotlb);
1009 break;
1010 case VHOST_USER_SLAVE_CONFIG_CHANGE_MSG :
1011 ret = vhost_user_slave_handle_config_change(dev);
1012 break;
1013 case VHOST_USER_SLAVE_VRING_HOST_NOTIFIER_MSG:
1014 ret = vhost_user_slave_handle_vring_host_notifier(dev, &payload.area,
1015 fd[0]);
1016 break;
1017 default:
1018 error_report("Received unexpected msg type.");
1019 ret = -EINVAL;
1022 /* Close the remaining file descriptors. */
1023 for (i = 0; i < fdsize; i++) {
1024 if (fd[i] != -1) {
1025 close(fd[i]);
1030 * REPLY_ACK feature handling. Other reply types has to be managed
1031 * directly in their request handlers.
1033 if (hdr.flags & VHOST_USER_NEED_REPLY_MASK) {
1034 struct iovec iovec[2];
1037 hdr.flags &= ~VHOST_USER_NEED_REPLY_MASK;
1038 hdr.flags |= VHOST_USER_REPLY_MASK;
1040 payload.u64 = !!ret;
1041 hdr.size = sizeof(payload.u64);
1043 iovec[0].iov_base = &hdr;
1044 iovec[0].iov_len = VHOST_USER_HDR_SIZE;
1045 iovec[1].iov_base = &payload;
1046 iovec[1].iov_len = hdr.size;
1048 size = writev(u->slave_fd, iovec, ARRAY_SIZE(iovec));
1049 if (size != VHOST_USER_HDR_SIZE + hdr.size) {
1050 error_report("Failed to send msg reply to slave.");
1051 goto err;
1055 return;
1057 err:
1058 qemu_set_fd_handler(u->slave_fd, NULL, NULL, NULL);
1059 close(u->slave_fd);
1060 u->slave_fd = -1;
1061 for (i = 0; i < fdsize; i++) {
1062 if (fd[i] != -1) {
1063 close(fd[i]);
1066 return;
1069 static int vhost_setup_slave_channel(struct vhost_dev *dev)
1071 VhostUserMsg msg = {
1072 .hdr.request = VHOST_USER_SET_SLAVE_REQ_FD,
1073 .hdr.flags = VHOST_USER_VERSION,
1075 struct vhost_user *u = dev->opaque;
1076 int sv[2], ret = 0;
1077 bool reply_supported = virtio_has_feature(dev->protocol_features,
1078 VHOST_USER_PROTOCOL_F_REPLY_ACK);
1080 if (!virtio_has_feature(dev->protocol_features,
1081 VHOST_USER_PROTOCOL_F_SLAVE_REQ)) {
1082 return 0;
1085 if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) == -1) {
1086 error_report("socketpair() failed");
1087 return -1;
1090 u->slave_fd = sv[0];
1091 qemu_set_fd_handler(u->slave_fd, slave_read, NULL, dev);
1093 if (reply_supported) {
1094 msg.hdr.flags |= VHOST_USER_NEED_REPLY_MASK;
1097 ret = vhost_user_write(dev, &msg, &sv[1], 1);
1098 if (ret) {
1099 goto out;
1102 if (reply_supported) {
1103 ret = process_message_reply(dev, &msg);
1106 out:
1107 close(sv[1]);
1108 if (ret) {
1109 qemu_set_fd_handler(u->slave_fd, NULL, NULL, NULL);
1110 close(u->slave_fd);
1111 u->slave_fd = -1;
1114 return ret;
1117 #ifdef CONFIG_LINUX
1119 * Called back from the postcopy fault thread when a fault is received on our
1120 * ufd.
1121 * TODO: This is Linux specific
1123 static int vhost_user_postcopy_fault_handler(struct PostCopyFD *pcfd,
1124 void *ufd)
1126 struct vhost_dev *dev = pcfd->data;
1127 struct vhost_user *u = dev->opaque;
1128 struct uffd_msg *msg = ufd;
1129 uint64_t faultaddr = msg->arg.pagefault.address;
1130 RAMBlock *rb = NULL;
1131 uint64_t rb_offset;
1132 int i;
1134 trace_vhost_user_postcopy_fault_handler(pcfd->idstr, faultaddr,
1135 dev->mem->nregions);
1136 for (i = 0; i < MIN(dev->mem->nregions, u->region_rb_len); i++) {
1137 trace_vhost_user_postcopy_fault_handler_loop(i,
1138 u->postcopy_client_bases[i], dev->mem->regions[i].memory_size);
1139 if (faultaddr >= u->postcopy_client_bases[i]) {
1140 /* Ofset of the fault address in the vhost region */
1141 uint64_t region_offset = faultaddr - u->postcopy_client_bases[i];
1142 if (region_offset < dev->mem->regions[i].memory_size) {
1143 rb_offset = region_offset + u->region_rb_offset[i];
1144 trace_vhost_user_postcopy_fault_handler_found(i,
1145 region_offset, rb_offset);
1146 rb = u->region_rb[i];
1147 return postcopy_request_shared_page(pcfd, rb, faultaddr,
1148 rb_offset);
1152 error_report("%s: Failed to find region for fault %" PRIx64,
1153 __func__, faultaddr);
1154 return -1;
1157 static int vhost_user_postcopy_waker(struct PostCopyFD *pcfd, RAMBlock *rb,
1158 uint64_t offset)
1160 struct vhost_dev *dev = pcfd->data;
1161 struct vhost_user *u = dev->opaque;
1162 int i;
1164 trace_vhost_user_postcopy_waker(qemu_ram_get_idstr(rb), offset);
1166 if (!u) {
1167 return 0;
1169 /* Translate the offset into an address in the clients address space */
1170 for (i = 0; i < MIN(dev->mem->nregions, u->region_rb_len); i++) {
1171 if (u->region_rb[i] == rb &&
1172 offset >= u->region_rb_offset[i] &&
1173 offset < (u->region_rb_offset[i] +
1174 dev->mem->regions[i].memory_size)) {
1175 uint64_t client_addr = (offset - u->region_rb_offset[i]) +
1176 u->postcopy_client_bases[i];
1177 trace_vhost_user_postcopy_waker_found(client_addr);
1178 return postcopy_wake_shared(pcfd, client_addr, rb);
1182 trace_vhost_user_postcopy_waker_nomatch(qemu_ram_get_idstr(rb), offset);
1183 return 0;
1185 #endif
1188 * Called at the start of an inbound postcopy on reception of the
1189 * 'advise' command.
1191 static int vhost_user_postcopy_advise(struct vhost_dev *dev, Error **errp)
1193 #ifdef CONFIG_LINUX
1194 struct vhost_user *u = dev->opaque;
1195 CharBackend *chr = u->user->chr;
1196 int ufd;
1197 VhostUserMsg msg = {
1198 .hdr.request = VHOST_USER_POSTCOPY_ADVISE,
1199 .hdr.flags = VHOST_USER_VERSION,
1202 if (vhost_user_write(dev, &msg, NULL, 0) < 0) {
1203 error_setg(errp, "Failed to send postcopy_advise to vhost");
1204 return -1;
1207 if (vhost_user_read(dev, &msg) < 0) {
1208 error_setg(errp, "Failed to get postcopy_advise reply from vhost");
1209 return -1;
1212 if (msg.hdr.request != VHOST_USER_POSTCOPY_ADVISE) {
1213 error_setg(errp, "Unexpected msg type. Expected %d received %d",
1214 VHOST_USER_POSTCOPY_ADVISE, msg.hdr.request);
1215 return -1;
1218 if (msg.hdr.size) {
1219 error_setg(errp, "Received bad msg size.");
1220 return -1;
1222 ufd = qemu_chr_fe_get_msgfd(chr);
1223 if (ufd < 0) {
1224 error_setg(errp, "%s: Failed to get ufd", __func__);
1225 return -1;
1227 qemu_set_nonblock(ufd);
1229 /* register ufd with userfault thread */
1230 u->postcopy_fd.fd = ufd;
1231 u->postcopy_fd.data = dev;
1232 u->postcopy_fd.handler = vhost_user_postcopy_fault_handler;
1233 u->postcopy_fd.waker = vhost_user_postcopy_waker;
1234 u->postcopy_fd.idstr = "vhost-user"; /* Need to find unique name */
1235 postcopy_register_shared_ufd(&u->postcopy_fd);
1236 return 0;
1237 #else
1238 error_setg(errp, "Postcopy not supported on non-Linux systems");
1239 return -1;
1240 #endif
1244 * Called at the switch to postcopy on reception of the 'listen' command.
1246 static int vhost_user_postcopy_listen(struct vhost_dev *dev, Error **errp)
1248 struct vhost_user *u = dev->opaque;
1249 int ret;
1250 VhostUserMsg msg = {
1251 .hdr.request = VHOST_USER_POSTCOPY_LISTEN,
1252 .hdr.flags = VHOST_USER_VERSION | VHOST_USER_NEED_REPLY_MASK,
1254 u->postcopy_listen = true;
1255 trace_vhost_user_postcopy_listen();
1256 if (vhost_user_write(dev, &msg, NULL, 0) < 0) {
1257 error_setg(errp, "Failed to send postcopy_listen to vhost");
1258 return -1;
1261 ret = process_message_reply(dev, &msg);
1262 if (ret) {
1263 error_setg(errp, "Failed to receive reply to postcopy_listen");
1264 return ret;
1267 return 0;
1271 * Called at the end of postcopy
1273 static int vhost_user_postcopy_end(struct vhost_dev *dev, Error **errp)
1275 VhostUserMsg msg = {
1276 .hdr.request = VHOST_USER_POSTCOPY_END,
1277 .hdr.flags = VHOST_USER_VERSION | VHOST_USER_NEED_REPLY_MASK,
1279 int ret;
1280 struct vhost_user *u = dev->opaque;
1282 trace_vhost_user_postcopy_end_entry();
1283 if (vhost_user_write(dev, &msg, NULL, 0) < 0) {
1284 error_setg(errp, "Failed to send postcopy_end to vhost");
1285 return -1;
1288 ret = process_message_reply(dev, &msg);
1289 if (ret) {
1290 error_setg(errp, "Failed to receive reply to postcopy_end");
1291 return ret;
1293 postcopy_unregister_shared_ufd(&u->postcopy_fd);
1294 close(u->postcopy_fd.fd);
1295 u->postcopy_fd.handler = NULL;
1297 trace_vhost_user_postcopy_end_exit();
1299 return 0;
1302 static int vhost_user_postcopy_notifier(NotifierWithReturn *notifier,
1303 void *opaque)
1305 struct PostcopyNotifyData *pnd = opaque;
1306 struct vhost_user *u = container_of(notifier, struct vhost_user,
1307 postcopy_notifier);
1308 struct vhost_dev *dev = u->dev;
1310 switch (pnd->reason) {
1311 case POSTCOPY_NOTIFY_PROBE:
1312 if (!virtio_has_feature(dev->protocol_features,
1313 VHOST_USER_PROTOCOL_F_PAGEFAULT)) {
1314 /* TODO: Get the device name into this error somehow */
1315 error_setg(pnd->errp,
1316 "vhost-user backend not capable of postcopy");
1317 return -ENOENT;
1319 break;
1321 case POSTCOPY_NOTIFY_INBOUND_ADVISE:
1322 return vhost_user_postcopy_advise(dev, pnd->errp);
1324 case POSTCOPY_NOTIFY_INBOUND_LISTEN:
1325 return vhost_user_postcopy_listen(dev, pnd->errp);
1327 case POSTCOPY_NOTIFY_INBOUND_END:
1328 return vhost_user_postcopy_end(dev, pnd->errp);
1330 default:
1331 /* We ignore notifications we don't know */
1332 break;
1335 return 0;
1338 static int vhost_user_backend_init(struct vhost_dev *dev, void *opaque)
1340 uint64_t features, protocol_features;
1341 struct vhost_user *u;
1342 int err;
1344 assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_USER);
1346 u = g_new0(struct vhost_user, 1);
1347 u->user = opaque;
1348 u->slave_fd = -1;
1349 u->dev = dev;
1350 dev->opaque = u;
1352 err = vhost_user_get_features(dev, &features);
1353 if (err < 0) {
1354 return err;
1357 if (virtio_has_feature(features, VHOST_USER_F_PROTOCOL_FEATURES)) {
1358 dev->backend_features |= 1ULL << VHOST_USER_F_PROTOCOL_FEATURES;
1360 err = vhost_user_get_u64(dev, VHOST_USER_GET_PROTOCOL_FEATURES,
1361 &protocol_features);
1362 if (err < 0) {
1363 return err;
1366 dev->protocol_features =
1367 protocol_features & VHOST_USER_PROTOCOL_FEATURE_MASK;
1369 if (!dev->config_ops || !dev->config_ops->vhost_dev_config_notifier) {
1370 /* Don't acknowledge CONFIG feature if device doesn't support it */
1371 dev->protocol_features &= ~(1ULL << VHOST_USER_PROTOCOL_F_CONFIG);
1372 } else if (!(protocol_features &
1373 (1ULL << VHOST_USER_PROTOCOL_F_CONFIG))) {
1374 error_report("Device expects VHOST_USER_PROTOCOL_F_CONFIG "
1375 "but backend does not support it.");
1376 return -1;
1379 err = vhost_user_set_protocol_features(dev, dev->protocol_features);
1380 if (err < 0) {
1381 return err;
1384 /* query the max queues we support if backend supports Multiple Queue */
1385 if (dev->protocol_features & (1ULL << VHOST_USER_PROTOCOL_F_MQ)) {
1386 err = vhost_user_get_u64(dev, VHOST_USER_GET_QUEUE_NUM,
1387 &dev->max_queues);
1388 if (err < 0) {
1389 return err;
1393 if (virtio_has_feature(features, VIRTIO_F_IOMMU_PLATFORM) &&
1394 !(virtio_has_feature(dev->protocol_features,
1395 VHOST_USER_PROTOCOL_F_SLAVE_REQ) &&
1396 virtio_has_feature(dev->protocol_features,
1397 VHOST_USER_PROTOCOL_F_REPLY_ACK))) {
1398 error_report("IOMMU support requires reply-ack and "
1399 "slave-req protocol features.");
1400 return -1;
1404 if (dev->migration_blocker == NULL &&
1405 !virtio_has_feature(dev->protocol_features,
1406 VHOST_USER_PROTOCOL_F_LOG_SHMFD)) {
1407 error_setg(&dev->migration_blocker,
1408 "Migration disabled: vhost-user backend lacks "
1409 "VHOST_USER_PROTOCOL_F_LOG_SHMFD feature.");
1412 err = vhost_setup_slave_channel(dev);
1413 if (err < 0) {
1414 return err;
1417 u->postcopy_notifier.notify = vhost_user_postcopy_notifier;
1418 postcopy_add_notifier(&u->postcopy_notifier);
1420 return 0;
1423 static int vhost_user_backend_cleanup(struct vhost_dev *dev)
1425 struct vhost_user *u;
1427 assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_USER);
1429 u = dev->opaque;
1430 if (u->postcopy_notifier.notify) {
1431 postcopy_remove_notifier(&u->postcopy_notifier);
1432 u->postcopy_notifier.notify = NULL;
1434 u->postcopy_listen = false;
1435 if (u->postcopy_fd.handler) {
1436 postcopy_unregister_shared_ufd(&u->postcopy_fd);
1437 close(u->postcopy_fd.fd);
1438 u->postcopy_fd.handler = NULL;
1440 if (u->slave_fd >= 0) {
1441 qemu_set_fd_handler(u->slave_fd, NULL, NULL, NULL);
1442 close(u->slave_fd);
1443 u->slave_fd = -1;
1445 g_free(u->region_rb);
1446 u->region_rb = NULL;
1447 g_free(u->region_rb_offset);
1448 u->region_rb_offset = NULL;
1449 u->region_rb_len = 0;
1450 g_free(u);
1451 dev->opaque = 0;
1453 return 0;
1456 static int vhost_user_get_vq_index(struct vhost_dev *dev, int idx)
1458 assert(idx >= dev->vq_index && idx < dev->vq_index + dev->nvqs);
1460 return idx;
1463 static int vhost_user_memslots_limit(struct vhost_dev *dev)
1465 return VHOST_MEMORY_MAX_NREGIONS;
1468 static bool vhost_user_requires_shm_log(struct vhost_dev *dev)
1470 assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_USER);
1472 return virtio_has_feature(dev->protocol_features,
1473 VHOST_USER_PROTOCOL_F_LOG_SHMFD);
1476 static int vhost_user_migration_done(struct vhost_dev *dev, char* mac_addr)
1478 VhostUserMsg msg = { };
1480 assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_USER);
1482 /* If guest supports GUEST_ANNOUNCE do nothing */
1483 if (virtio_has_feature(dev->acked_features, VIRTIO_NET_F_GUEST_ANNOUNCE)) {
1484 return 0;
1487 /* if backend supports VHOST_USER_PROTOCOL_F_RARP ask it to send the RARP */
1488 if (virtio_has_feature(dev->protocol_features,
1489 VHOST_USER_PROTOCOL_F_RARP)) {
1490 msg.hdr.request = VHOST_USER_SEND_RARP;
1491 msg.hdr.flags = VHOST_USER_VERSION;
1492 memcpy((char *)&msg.payload.u64, mac_addr, 6);
1493 msg.hdr.size = sizeof(msg.payload.u64);
1495 return vhost_user_write(dev, &msg, NULL, 0);
1497 return -1;
1500 static bool vhost_user_can_merge(struct vhost_dev *dev,
1501 uint64_t start1, uint64_t size1,
1502 uint64_t start2, uint64_t size2)
1504 ram_addr_t offset;
1505 int mfd, rfd;
1506 MemoryRegion *mr;
1508 mr = memory_region_from_host((void *)(uintptr_t)start1, &offset);
1509 mfd = memory_region_get_fd(mr);
1511 mr = memory_region_from_host((void *)(uintptr_t)start2, &offset);
1512 rfd = memory_region_get_fd(mr);
1514 return mfd == rfd;
1517 static int vhost_user_net_set_mtu(struct vhost_dev *dev, uint16_t mtu)
1519 VhostUserMsg msg;
1520 bool reply_supported = virtio_has_feature(dev->protocol_features,
1521 VHOST_USER_PROTOCOL_F_REPLY_ACK);
1523 if (!(dev->protocol_features & (1ULL << VHOST_USER_PROTOCOL_F_NET_MTU))) {
1524 return 0;
1527 msg.hdr.request = VHOST_USER_NET_SET_MTU;
1528 msg.payload.u64 = mtu;
1529 msg.hdr.size = sizeof(msg.payload.u64);
1530 msg.hdr.flags = VHOST_USER_VERSION;
1531 if (reply_supported) {
1532 msg.hdr.flags |= VHOST_USER_NEED_REPLY_MASK;
1535 if (vhost_user_write(dev, &msg, NULL, 0) < 0) {
1536 return -1;
1539 /* If reply_ack supported, slave has to ack specified MTU is valid */
1540 if (reply_supported) {
1541 return process_message_reply(dev, &msg);
1544 return 0;
1547 static int vhost_user_send_device_iotlb_msg(struct vhost_dev *dev,
1548 struct vhost_iotlb_msg *imsg)
1550 VhostUserMsg msg = {
1551 .hdr.request = VHOST_USER_IOTLB_MSG,
1552 .hdr.size = sizeof(msg.payload.iotlb),
1553 .hdr.flags = VHOST_USER_VERSION | VHOST_USER_NEED_REPLY_MASK,
1554 .payload.iotlb = *imsg,
1557 if (vhost_user_write(dev, &msg, NULL, 0) < 0) {
1558 return -EFAULT;
1561 return process_message_reply(dev, &msg);
1565 static void vhost_user_set_iotlb_callback(struct vhost_dev *dev, int enabled)
1567 /* No-op as the receive channel is not dedicated to IOTLB messages. */
1570 static int vhost_user_get_config(struct vhost_dev *dev, uint8_t *config,
1571 uint32_t config_len)
1573 VhostUserMsg msg = {
1574 .hdr.request = VHOST_USER_GET_CONFIG,
1575 .hdr.flags = VHOST_USER_VERSION,
1576 .hdr.size = VHOST_USER_CONFIG_HDR_SIZE + config_len,
1579 if (!virtio_has_feature(dev->protocol_features,
1580 VHOST_USER_PROTOCOL_F_CONFIG)) {
1581 return -1;
1584 if (config_len > VHOST_USER_MAX_CONFIG_SIZE) {
1585 return -1;
1588 msg.payload.config.offset = 0;
1589 msg.payload.config.size = config_len;
1590 if (vhost_user_write(dev, &msg, NULL, 0) < 0) {
1591 return -1;
1594 if (vhost_user_read(dev, &msg) < 0) {
1595 return -1;
1598 if (msg.hdr.request != VHOST_USER_GET_CONFIG) {
1599 error_report("Received unexpected msg type. Expected %d received %d",
1600 VHOST_USER_GET_CONFIG, msg.hdr.request);
1601 return -1;
1604 if (msg.hdr.size != VHOST_USER_CONFIG_HDR_SIZE + config_len) {
1605 error_report("Received bad msg size.");
1606 return -1;
1609 memcpy(config, msg.payload.config.region, config_len);
1611 return 0;
1614 static int vhost_user_set_config(struct vhost_dev *dev, const uint8_t *data,
1615 uint32_t offset, uint32_t size, uint32_t flags)
1617 uint8_t *p;
1618 bool reply_supported = virtio_has_feature(dev->protocol_features,
1619 VHOST_USER_PROTOCOL_F_REPLY_ACK);
1621 VhostUserMsg msg = {
1622 .hdr.request = VHOST_USER_SET_CONFIG,
1623 .hdr.flags = VHOST_USER_VERSION,
1624 .hdr.size = VHOST_USER_CONFIG_HDR_SIZE + size,
1627 if (!virtio_has_feature(dev->protocol_features,
1628 VHOST_USER_PROTOCOL_F_CONFIG)) {
1629 return -1;
1632 if (reply_supported) {
1633 msg.hdr.flags |= VHOST_USER_NEED_REPLY_MASK;
1636 if (size > VHOST_USER_MAX_CONFIG_SIZE) {
1637 return -1;
1640 msg.payload.config.offset = offset,
1641 msg.payload.config.size = size,
1642 msg.payload.config.flags = flags,
1643 p = msg.payload.config.region;
1644 memcpy(p, data, size);
1646 if (vhost_user_write(dev, &msg, NULL, 0) < 0) {
1647 return -1;
1650 if (reply_supported) {
1651 return process_message_reply(dev, &msg);
1654 return 0;
1657 static int vhost_user_crypto_create_session(struct vhost_dev *dev,
1658 void *session_info,
1659 uint64_t *session_id)
1661 bool crypto_session = virtio_has_feature(dev->protocol_features,
1662 VHOST_USER_PROTOCOL_F_CRYPTO_SESSION);
1663 CryptoDevBackendSymSessionInfo *sess_info = session_info;
1664 VhostUserMsg msg = {
1665 .hdr.request = VHOST_USER_CREATE_CRYPTO_SESSION,
1666 .hdr.flags = VHOST_USER_VERSION,
1667 .hdr.size = sizeof(msg.payload.session),
1670 assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_USER);
1672 if (!crypto_session) {
1673 error_report("vhost-user trying to send unhandled ioctl");
1674 return -1;
1677 memcpy(&msg.payload.session.session_setup_data, sess_info,
1678 sizeof(CryptoDevBackendSymSessionInfo));
1679 if (sess_info->key_len) {
1680 memcpy(&msg.payload.session.key, sess_info->cipher_key,
1681 sess_info->key_len);
1683 if (sess_info->auth_key_len > 0) {
1684 memcpy(&msg.payload.session.auth_key, sess_info->auth_key,
1685 sess_info->auth_key_len);
1687 if (vhost_user_write(dev, &msg, NULL, 0) < 0) {
1688 error_report("vhost_user_write() return -1, create session failed");
1689 return -1;
1692 if (vhost_user_read(dev, &msg) < 0) {
1693 error_report("vhost_user_read() return -1, create session failed");
1694 return -1;
1697 if (msg.hdr.request != VHOST_USER_CREATE_CRYPTO_SESSION) {
1698 error_report("Received unexpected msg type. Expected %d received %d",
1699 VHOST_USER_CREATE_CRYPTO_SESSION, msg.hdr.request);
1700 return -1;
1703 if (msg.hdr.size != sizeof(msg.payload.session)) {
1704 error_report("Received bad msg size.");
1705 return -1;
1708 if (msg.payload.session.session_id < 0) {
1709 error_report("Bad session id: %" PRId64 "",
1710 msg.payload.session.session_id);
1711 return -1;
1713 *session_id = msg.payload.session.session_id;
1715 return 0;
1718 static int
1719 vhost_user_crypto_close_session(struct vhost_dev *dev, uint64_t session_id)
1721 bool crypto_session = virtio_has_feature(dev->protocol_features,
1722 VHOST_USER_PROTOCOL_F_CRYPTO_SESSION);
1723 VhostUserMsg msg = {
1724 .hdr.request = VHOST_USER_CLOSE_CRYPTO_SESSION,
1725 .hdr.flags = VHOST_USER_VERSION,
1726 .hdr.size = sizeof(msg.payload.u64),
1728 msg.payload.u64 = session_id;
1730 if (!crypto_session) {
1731 error_report("vhost-user trying to send unhandled ioctl");
1732 return -1;
1735 if (vhost_user_write(dev, &msg, NULL, 0) < 0) {
1736 error_report("vhost_user_write() return -1, close session failed");
1737 return -1;
1740 return 0;
1743 static bool vhost_user_mem_section_filter(struct vhost_dev *dev,
1744 MemoryRegionSection *section)
1746 bool result;
1748 result = memory_region_get_fd(section->mr) >= 0;
1750 return result;
1753 VhostUserState *vhost_user_init(void)
1755 VhostUserState *user = g_new0(struct VhostUserState, 1);
1757 return user;
1760 void vhost_user_cleanup(VhostUserState *user)
1762 int i;
1764 for (i = 0; i < VIRTIO_QUEUE_MAX; i++) {
1765 if (user->notifier[i].addr) {
1766 object_unparent(OBJECT(&user->notifier[i].mr));
1767 munmap(user->notifier[i].addr, qemu_real_host_page_size);
1768 user->notifier[i].addr = NULL;
1773 const VhostOps user_ops = {
1774 .backend_type = VHOST_BACKEND_TYPE_USER,
1775 .vhost_backend_init = vhost_user_backend_init,
1776 .vhost_backend_cleanup = vhost_user_backend_cleanup,
1777 .vhost_backend_memslots_limit = vhost_user_memslots_limit,
1778 .vhost_set_log_base = vhost_user_set_log_base,
1779 .vhost_set_mem_table = vhost_user_set_mem_table,
1780 .vhost_set_vring_addr = vhost_user_set_vring_addr,
1781 .vhost_set_vring_endian = vhost_user_set_vring_endian,
1782 .vhost_set_vring_num = vhost_user_set_vring_num,
1783 .vhost_set_vring_base = vhost_user_set_vring_base,
1784 .vhost_get_vring_base = vhost_user_get_vring_base,
1785 .vhost_set_vring_kick = vhost_user_set_vring_kick,
1786 .vhost_set_vring_call = vhost_user_set_vring_call,
1787 .vhost_set_features = vhost_user_set_features,
1788 .vhost_get_features = vhost_user_get_features,
1789 .vhost_set_owner = vhost_user_set_owner,
1790 .vhost_reset_device = vhost_user_reset_device,
1791 .vhost_get_vq_index = vhost_user_get_vq_index,
1792 .vhost_set_vring_enable = vhost_user_set_vring_enable,
1793 .vhost_requires_shm_log = vhost_user_requires_shm_log,
1794 .vhost_migration_done = vhost_user_migration_done,
1795 .vhost_backend_can_merge = vhost_user_can_merge,
1796 .vhost_net_set_mtu = vhost_user_net_set_mtu,
1797 .vhost_set_iotlb_callback = vhost_user_set_iotlb_callback,
1798 .vhost_send_device_iotlb_msg = vhost_user_send_device_iotlb_msg,
1799 .vhost_get_config = vhost_user_get_config,
1800 .vhost_set_config = vhost_user_set_config,
1801 .vhost_crypto_create_session = vhost_user_crypto_create_session,
1802 .vhost_crypto_close_session = vhost_user_crypto_close_session,
1803 .vhost_backend_mem_section_filter = vhost_user_mem_section_filter,